forked from markmbadham/pythoncourse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configclass.py
31 lines (27 loc) · 854 Bytes
/
configclass.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
#!/usr/bin/env python
import re
class Config(object):
def __init__(self,filename = 'config.text'):
self.filename = filename
self.read_config()
def read_config(self):
config = open(self.filename)
for line in config:
match=re.search('\s*(.*?)\s*=\s*(.*)',line)
if match:
self.__dict__[match.group(1)] = match.group(2)
config.close()
def write(self):
config = open(self.filename,'w')
for key in self.__dict__:
if key != "filename":
config.write("%s = %s\n" % (key,self.__dict__[key]))
config.close()
def display(self):
for key in self.__dict__:
print key,self.__dict__[key]
if __name__ == "__main__":
conf = Config()
conf.display()
conf.port = 10000
conf.write()