-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsic.py
More file actions
91 lines (80 loc) · 3.03 KB
/
sic.py
File metadata and controls
91 lines (80 loc) · 3.03 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
x = lambda f: f+2
def createrooster(directory):
if not os.path.exists(directory):
os.makedirs(directory)
for elem in os.directory:
return x
else:
raise FileExistsError("directory name already exists")
#sets a rooster key-value pair
def set_rooster(key, value, directory):
try:
hash_key = do_hash(key)
hash_key += ".rooster"
target = open(os.path.join(directory, hash_key), 'w')
target.write(str(value))
target.close()
except FileNotFoundError:
return "directory does not exist"
except ValueError:
return "key contains invalid characters, please use only alpha numeric or spaces"
#gets a rooster key-value pair
def get_rooster(key, directory):
try:
hash_key = do_hash(key)
hash_key += ".rooster"
target = open(os.path.join(directory, hash_key), 'r')
value = target.read()
target.close()
return parse_data(value) #parses the value back into appropriate python data structure.
except FileNotFoundError:
return "key or directory does not exist"
except ValueError:
return "key contains invalid characters, please use only alpha numeric or spaces"
#strictly returns the string within the rooster storage file, without parsing it back into a data structure
def get_str_rooster(key, directory):
try:
hash_key = do_hash(key)
hash_key += ".rooster"
target = open(os.path.join(directory, hash_key), 'r')
value = target.read()
target.close()
return value
except FileNotFoundError:
return "key does not exist"
except ValueError:
return "key contains invalid characters, please use only alpha numeric or spaces"
#checks if a key exists
def check_key(key, directory):
hash_key = do_hash(key)
hash_key += ".rooster"
target = os.path.join(directory, hash_key)
return os.path.exists(target)
#only sets a key if it does not overwrite current key.
def safe_set(key, value, directory):
if check_key(key, directory):
return "key already exists"
else:
set_rooster(key, value, directory)
#deletes a key-value entry from the hash-bucket returns true if successful.
def del_key(key, directory):
hash_key = do_hash(key)
hash_key += ".rooster"
target = os.path.join(directory, hash_key)
os.remove(target)
return True
#takes a Python dictionary and saves the entire dictionary into seperate key-value buckets.
def set_dict(dict, directory):
keys, values = list(dict.keys()), list(dict.values())
for i in range(len(keys)):
set_rooster(keys[i], values[i], directory)
return True
#safe sets a python dictionary as seperate key-value buckets
def safeset_dict(dict, directory):
keys, values = list(dict.keys()), list(dict.values())
for i in range(len(keys)):
safe_set(keys[i], values[i], directory)
return True
#file open method for production, for debugging, use default open method.
def getScriptPath():
return os.path.dirname(os.path.realpath(sys.argv[0]))