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

Commit

Permalink
Made it a package
Browse files Browse the repository at this point in the history
  • Loading branch information
lainq committed Mar 10, 2021
1 parent 4bb30d1 commit 04570bd
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
22 changes: 22 additions & 0 deletions python_js_object/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""MIT License
Copyright (c) 2021 Code Roller
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
40 changes: 40 additions & 0 deletions python_js_object/jsobj.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class JsObjectInstance(object):
pass

class JsObject:
def __init__(self, template: dict):
self.template = template
self.obj: JsObjectInstance = JsObjectInstance()



def addAttr(self, attr: str, value: any):
if attr not in self.template.keys():
self.template[attr] = value
self.obj.__setattr__(attr, value)
else:
print("Attribute already taken, please use JsObject.setAttr().")

def addMethod(self, method):
if method.__name__ not in self.template.keys():
self.template[method.__name__] = method
self.obj.__setattr__(method.__name__, method)
else:
print("Method already present.")

def delAttr(self, attr: str):
del self.template[attr]
delattr(self.obj, attr)

def setAttr(self, attr: str, value: any):
if attr in self.template.keys():
self.template[attr] = value
setattr(self.obj, attr, value)
else:
print("Attribute not added, please use JsObject.addAttr().")

def get(self):
return self.obj

def __repr__(self):
return repr(self.template)

0 comments on commit 04570bd

Please sign in to comment.