Skip to content
This repository was archived by the owner on Mar 29, 2021. It is now read-only.

Commit a782391

Browse files
author
JavaCode7
committed
Add dictionary and tether to object
1 parent 09c1c3d commit a782391

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

‎demo.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from jsobj import JsObject
2+
3+
idealApple = JsObject({
4+
"crunchy": True,
5+
"juicy": True,
6+
"will_make_teeth_fall_out": False,
7+
"juice_level": 100
8+
})
9+
10+
idealApple.addAttr("green", True)
11+
print(idealApple)
12+
13+
idealApple.setAttr("juice_level", 75)
14+
print(idealApple)
15+
16+
idealApple.delAttr("will_make_teeth_fall_out")
17+
print(idealApple)
18+
19+
print(idealApple.getObj())

‎jsobj.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class JsObjectInstance(object):
2+
pass
3+
4+
class JsObject:
5+
def __init__(self, template: dict):
6+
self.template = template
7+
self.obj: Object = JsObjectInstance()
8+
i = 0
9+
while i < len(self.template.keys()):
10+
setattr(self.obj, list(self.template.keys())[i], list(self.template.values())[i])
11+
i += 1
12+
13+
def addAttr(self, attr: str, value: any):
14+
if attr not in self.template.keys():
15+
self.template[attr] = value
16+
self.obj.__setattr__(attr, value)
17+
else:
18+
print("Attribute already taken, please use JsObject.setAttr()")
19+
20+
def delAttr(self, attr: str):
21+
del self.template[attr]
22+
delattr(self.obj, attr)
23+
24+
def setAttr(self, attr: str, value: any):
25+
if attr in self.template.keys():
26+
self.template[attr] = value
27+
setattr(self.obj, attr, value)
28+
else:
29+
print("Attribute not added, please use JsObject.addAttr()")
30+
31+
def getObj(self):
32+
return self.obj
33+
34+
def __repr__(self):
35+
return repr(self.template)

0 commit comments

Comments
 (0)