This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlindex.py
56 lines (50 loc) · 1.9 KB
/
lindex.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/env python3
class lindex(dict):
# Pretty Print Dictionary
def pprint(self, EmptyString: str = " ") -> bool:
def PrintSubsection(dictionary: dict, EmptyString: str, SpaceAmnt: int = 0):
if type(dictionary) == type(self) or type(dictionary) == dict:
return "\n".join( EmptyString * SpaceAmnt + f"{entry}:\n" + PrintSubsection(dictionary[entry], EmptyString, SpaceAmnt + 1) for entry in dictionary )
else:
return EmptyString * SpaceAmnt + str(dictionary)
print(PrintSubsection(self, EmptyString))
return True
# Add num to self[arg[0]][arg[1]][arg2]...
def add(self, *args, num: int = None) -> bool:
if num is None:
num = args[-1]
args = args[:-1]
OldNum = self.RTN(*args)
if (type(OldNum) == int or type(OldNum) == float) and (type(num) == int or type(num) == float) :
self.set(*args, num = num + OldNum)
return True
else:
return False
# Sets self[arg[0]][arg[1]][arg2]... to num
def set(self, *args, num = None) -> bool:
if num is None:
num = args[-1]
args = args[:-1]
def Carve(PointsDict: dict, Indexes: tuple, FinalState, States: list = []) -> list:
States.append(PointsDict)
if len(Indexes) == 0:
States[-1] = FinalState
return States
if Indexes[0] not in PointsDict:
PointsDict[Indexes[0]] = {}
return Carve(PointsDict[Indexes[0]], Indexes[1:], FinalState, States)
def Write(Indexes: tuple, States: list) -> dict:
for i in range(len(States) - 1):
States[-i-2][Indexes[-i-1]] = States[-i-1]
return States[0]
self = super().__init__(Write(args, Carve(self, args, num)))
return True
# Returns self[arg[0]][arg[1]][arg2]...
def RTN(self, *args):
def Carve(dictionary: dict, Indexes: tuple):
if len(Indexes) == 0:
return dictionary
if Indexes[0] not in dictionary:
return False
return Carve(dictionary[Indexes[0]], Indexes[1:])
return Carve(self, args)