-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBunch.py
40 lines (29 loc) · 891 Bytes
/
Bunch.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
import os
class Bunch(object):
def __init__(self, **kwds):
self.__dict__.update(kwds)
def __eq__(self, other):
return self.__dict__ == other.__dict__
def __str__(self):
string = "BUNCH {" + str(self.__dict__)[2:]
string = string.replace("': ", ":")
string = string.replace(", '", ", ")
return string
def __repr__(self):
return str(self)
def to_file_name(self, folder=None, ext=None):
res = str(self.__dict__)[2:-1]
res = res.replace("'", "")
res = res.replace(": ", ".")
parts = res.split(', ')
res = '_'.join(sorted(parts))
if ext is not None:
res = '%s.%s' % (res, ext)
if folder is not None:
res = os.path.join(folder, res)
return res
if __name__ == '__main__':
b = Bunch(x=5, y='something', other=9.0)
print(b)
print(b.to_file_name())
print(b.to_file_name('./here', 'txt'))