blob: 2651e32a9b2f6fa95298d63aa5ccf14066495756 (
plain)
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
|
#!/usr/bin/env python
class numberThing:
def __init__(self):
#make a dict to hold data
self.users = dict()
'''define some variable'''
self.looping = True
#what is the file?
self.list_file = "thelist"
#map some function with a dict
self.functions = {"q":self.quit,
"?":self.print_help,
"h":self.print_help,
"i":self.insert_user,
"l":self.list_users,
"s":self.save}
#what is the help string?
self.help_string='''
--Usage--
=========
?: this help message
h: this help message
i: insert a user
l: list users
s: save
q: quit
'''
#read the list file
self.read_list()
#start the shitty loop
while self.looping:
#get some input from the user
user_input = raw_input("what do you want to do (? or h for help):")
function = self.functions.get(user_input)
if function:
function()
else:
#there was no mapped function
self.print_help()
def quit(self):
print "quit"
self.looping=False
def read_list(self):
try:
f = open(self.list_file)
for line in f:
strippedline = line.strip()
if strippedline!='':
(name,number) = strippedline.split("::")
self.users[name]=number
f.close()
print "file has been read"
except Exception as e:
print "there is no file"
def insert_user(self):
name = self.get_string_from_user("Enter the Name")
number = self.get_string_from_user("What is %s's number" % (name) )
self.users[name]=number
def list_users(self):
print "Users"
for user in self.users:
print "%s: %s" % (user, self.users[user])
def get_string_from_user(self,string):
valid_string = False
while not valid_string:
user_input = raw_input("%s:" % (string)).strip()
if len(string)>0:
valid_string = True
return user_input
def print_help(self):
#print the help
print self.help_string
def save(self):
f = open(self.list_file, 'w')
for user in self.users:
f.write("%s::%s\n" % (user, self.users[user]))
f.close()
if __name__=="__main__":
p = numberThing()
|