-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
173 lines (151 loc) · 5.46 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import sys, json
import settings
monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
###### Converting text month (eg Jul-23) back and forth to number
def getMonthTxt(monthIndex):
year = monthIndex // 12
month = monthNames[monthIndex % 12]
return month +'-'+ '%02d'%year
def dateTimeToMonthTxt(dt):
# python datetime to string 31-Mar-2024
dt = str(dt)
#print('====', dt)
tok = dt.split()
if len(tok) == 2:
tok = tok[0].split('-')
year = tok[0]
month = monthNames[int(tok[1])-1]
day = tok[2]
#print('2--', year, month, day)
else:
tok = dt.split('/')
day = tok[0]
month = monthNames[int(tok[1])-1]
year = tok[2][-2:] # reduces 2022 to 22
#print('1--', year, month, day)
monthTxt = '%02s-%03s-%4s' % (day, month, year)
return monthTxt
def getMonthIndex(monthTxt):
monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
tok = monthTxt.split('-')
if len(tok) == 3: # day-month-year
day = int(tok[0])
month = monthNames.index(tok[1])
year = int(tok[2])
if year > 2000: year -= 2000
if day > 20:
month += 1
#print('3--', day, month, year)
else:
day = 1
month = monthNames.index(tok[0])
year = int(tok[1])
#print('2--', day, month, year)
return year*12 + month # month number with Jan 2000 = 1
###### The start and end of the run
class run():
def __init__(self, start_month, end_month):
self.istart = getMonthIndex(start_month)
self.iend = getMonthIndex(end_month)
self.nmonth = self.iend - self.istart
def print(self):
start = getMonthTxt(self.istart)
end = getMonthTxt(self.iend)
print('Run is from %s to %s (%d months)' % (start, end, self.nmonth))
###### Searching for names in crap
class nameSearcher():
def __init__(self, people_filename):
people_data = json.loads(open(people_filename).read())
self.people = people_data['people']
def findName(self, hint):
line = hint.strip().lower()
scoreDict = {}
for name in self.people.keys():
staffNumber = self.people[name]['staffNumber']
if name.lower() in line or str(staffNumber) in line:
scoreDict[name] = 1
if 'otherNames' in self.people[name]:
for otherName in self.people[name]['otherNames']:
if otherName.lower() in line:
if name in scoreDict.keys():
scoreDict[name] += 1
else:
scoreDict[name] = 1
person_name = None
maxScore = 0
for name,score in scoreDict.items():
if score > maxScore:
person_name = name
maxScore = score
return person_name
###### Changing P&M expense categories to WFAU categories
# 'Salary', 'Consumables', 'Travel', 'Equipment',
# and ignoring those we arent interested in
category_ignore = [
'nan',
'Total Award',
'Grant Income',
'Financial Resources',
'Directly Allocated - Co-Principal Investigator Staff',
'Directly Allocated - Estates Costs',
'Directly Allocated - Infrastructure Technician Costs',
'Directly Allocated - Principal Investigator Staff',
'Directly Allocated - Co-Principal Investigator Staff',
'Directly Allocated Research Administration Staff',
'Indirect Costs',
]
category_consumables = [
'Consumables - Research Other Costs',
'Consumables - IT',
'Consumables - Telephone and Communication',
'Consumables - Printing Postage and Stationery',
'Consumables - Research Consumables',
'Staff Conf Course and Seminar Fees',
'Other Directly Incurred',
]
category_salary = [
'Research Investigator',
'Research Assistant'
]
category_travel = ['Travel and Subsistence']
category_equipment = ['Equipment']
def categories():
return ['Salary', 'Travel', 'Equipment', 'Consumables']
def my_category(category):
if category in category_ignore:
return None
if category in category_consumables:
return 'Consumables'
if category in category_salary:
return 'Salary'
if category in category_travel:
return 'Travel'
if category in category_equipment:
return 'Equipment'
print('ERROR Unknown expenditure category! "%s"' % category)
return None
def print_settings():
print('mygrantsjson is ', settings.MYGRANTS, ' dated ', settings.GRANTS_DATE)
print('projects spreadsheet is ', settings.PROJECTS)
print('people.json is ', settings.PEOPLE)
print('assign.json is ', settings.ASSIGN)
print('transactions spreadsheet is', settings.TRANSACTIONS)
print('This run is from', settings.RUN_START, 'to', settings.RUN_END)
##########
if __name__ == '__main__':
monthTxt = 'Dec-22'
print(monthTxt)
imonth = getMonthIndex(monthTxt)
print(imonth)
monthTxt = getMonthTxt(imonth)
print(monthTxt)
lines = [
'Oct-22 Payroll to Projects - Emp No 129091 (0.00 FTE)',
'Jun-23 Payroll to Projects - Emp No 124393 Cross, Nicholas James (0.58 FTE)',
'5_2021_1801226_Nov 21 100% G Blow Fm Epcc'
]
ns = nameSearcher(settings.PEOPLE)
for line in lines:
print(line)
name = ns.findName(line)
print('--->', name)