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

Commit 04570bd

Browse files
committed
Made it a package
1 parent 4bb30d1 commit 04570bd

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

python_js_object/__init__.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"""MIT License
2+
3+
Copyright (c) 2021 Code Roller
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
22+
"""

python_js_object/jsobj.py

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

0 commit comments

Comments
 (0)