-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElement.py
85 lines (64 loc) · 2.87 KB
/
Element.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
import csv
class Element:
def __init__(self, csvrow: dict):
self.payroll_name = csvrow['Payroll Name'].strip()
self.costing_name = csvrow['Costing Name'].strip()
self.payroll_category = csvrow['Payroll Category'].strip()
self.costing_category = csvrow['Costing Category'].strip()
self.debit_accounts = [int(x.strip()) for x in csvrow['Debit Account'].split(';')]
self.credit_accounts = [int(x.strip()) for x in csvrow['Credit Account'].split(';')]
self.should_cost = True if csvrow['Should Cost'].strip().casefold() == 'TRUE'.casefold() else False
def __eq__(self, other) -> bool:
return True if isinstance(other, Element) and self.payroll_name == other.payroll_name else False
def __hash__(self):
return hash(self.payroll_name)
def __str__(self):
s = self.payroll_category + '\t'
s += self.payroll_name + '\t'
s += self.costing_category + '\t'
s += self.costing_name + '\t'
s += str(self.debit_accounts) + '\t'
s += str(self.credit_accounts)
return s
def __repr__(self):
return self.__str__()
class ElementTable:
def __init__(self):
self.elements = set()
self.__payroll_name_lookup__ = dict()
self.__costing_name_lookup__ = dict()
def __iter__(self):
return iter(self.elements)
def add(self, element: Element) -> None:
if not isinstance(element, Element):
raise TypeError('Arg passed to ElementTable.add() is not an Element object.')
self.elements.add(element)
self.__payroll_name_lookup__[element.payroll_name] = element
self.__costing_name_lookup__[element.costing_name] = element
def find_by_payroll_name(self, name: str) -> Element:
return self.__payroll_name_lookup__.get(name, None)
def find_by_costing_name(self, name: str) -> Element:
return self.__costing_name_lookup__.get(name, None)
def __getitem__(self, name: str):
element = self.__payroll_name_lookup__.get(name, None)
return element if element else self.__costing_name_lookup__.get(name, None)
class Parser:
fieldnames = set([
'Costing Category',
'Costing Name',
'Payroll Category',
'Payroll Name',
'Debit Account',
'Credit Account'
])
#input_file = r'\\ihsnas1.net.inova.org\smitchris\MYDOCS\Projects\Payroll costing audit\config files\elements.csv'
@staticmethod
def parse(filename: str) -> dict:
elements = ElementTable()
with open(filename, 'r', newline='') as csvfile:
reader = csv.DictReader(csvfile)
if not Parser.fieldnames.issubset(set(reader.fieldnames)):
raise SyntaxError('Elements input file does not contain the correct headers.')
for row in reader:
elements.add(Element(row))
return elements