-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatatypes.py
More file actions
142 lines (100 loc) · 3.28 KB
/
Copy pathdatatypes.py
File metadata and controls
142 lines (100 loc) · 3.28 KB
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
#
# datatypes.py
# Created by Paul Dodd
# 08/24/2014
#
import json
import os
import sys
import time
import uuid
uuid_null = uuid.UUID(bytes='\x00'*16);
time_format = "%Y-%m-%d";
# class time_struct:
# def __init__(self):
# self.year = 0;
# self.month = 0;
# self.day = 0;
# def to_time_string(self):
# tstr = "";
# return tstr;
# def to_time_struct(self):
# return time.;
# use some machine learing to automatically classify categories.
class categoryManager:
def __init__(self, path):
self.categories = ["None"] # Add default categories here.
if os.path.exists(path):
self.loadFromFile(path);
else:
pass
def loadFromFile(self, path):
pass
def saveToFile(self, path):
pass
#### end categoryManager ####
class transaction:
def __init__(self, date = time.strftime(time_format, time.gmtime(0)), category = "None", amt = 0.0, desc = "", tid = uuid_null):
self.__dict__ = {"date": time.strptime(date, time_format), "category":category, "amount": amt, "description": desc, "__transaction__":tid };
def __str__(self):
return json.dumps(self.toJSON()); # indent = 4 ?
def __getitem__(self, name):
return self.__dict__[name];
def toJSON(self):
temp = self.__dict__.copy();
temp["date"] = time.strftime(time_format, temp["date"]);
temp["__transaction__"] = str(temp["__transaction__"]);
return temp;
def fromJSON(self, temp):
self.__dict__ = temp.copy();
self.__dict__["date"] = time.strptime(temp["date"], time_format);
self.__dict__["__transaction__"] = uuid.UUID(temp["__transaction__"]);
class TransactionEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, transaction):
return obj.toJSON();
# Let the base class default method raise the TypeError
return json.JSONEncoder.default(self, obj)
@staticmethod
def as_transaction(dct):
if "__transaction__" in dct:
t = transaction();
t.fromJSON(dct)
return t;
return dct;
#### end transaction ####
class configuration:
def __init__(self):
self.path = "";
self.bInit = False;
self.__dict__ = {};
def __setitem__(self, name, val):
self.__dict__[name] = val;
def __getitem__(self, name):
return self.__dict__[name];
def __str__(self):
return json.dumps(self.__dict__);
def loadFromFile(self):
try:
if not os.path.exists("config.json"):
print "could not load configuration file"
return False;
else:
cfile = open("config.json", "r");
self.__dict__ = json.loads(cfile.read());
cfile.close();
self.bInit = True;
return True;
except:
print "Error reading config file"
return False;
def saveToFile(self):
try:
cfile = open("config.json", "w");
cfile.write(json.dumps(self.__dict__));
cfile.close();
return True;
except:
print "Error writing config file"
return False;
#### end configuration ####