Skip to content

Instantly share code, notes, and snippets.

@NrI3
Last active March 28, 2020 02:32
Show Gist options
  • Save NrI3/53c7e4c40e00e8ab36b8195a2b322128 to your computer and use it in GitHub Desktop.
Save NrI3/53c7e4c40e00e8ab36b8195a2b322128 to your computer and use it in GitHub Desktop.
Python - Ruby Way!
# Ruby way for python
# By [email protected]
from datetime import datetime
import types
class Dynamic:
@staticmethod
def get_obj(hclass,value,class_name=None):
if not class_name:
class_name = 'G'+hclass.__name__.capitalize()
return type(class_name,(hclass,),{})(value)
class GFunc:
def __init__(self, func):
self._fn = func
self.attr = Attribute(self)
def __call__(self, *args, **kargs):
self._fn(*args,**kargs)
class Attribute:
def __init__(self, parent):
self._parent = parent
@property
def add(self):
pass
@add.setter
def add(self, value):
# tuple
if isinstance(value, tuple):
if isinstance(value[0], str):
setattr(self._parent , value[0], value[1])
# str
elif isinstance(value, str):
setattr(self._parent , value, '')
class Object:
def __init__(self):
self.attr = Attribute(self)
def __setattr__(self, name, value):
# Attribute
if name == 'attr':
self.__dict__[name] = value
# Function
elif isinstance(value, types.FunctionType):
o = Dynamic.GFunc(value)
self.__dict__[name] = o
# Object
else:
o = Dynamic.get_obj(value.__class__, value)
setattr(o,'attr', Attribute(o))
self.__dict__[name] = o
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment