Skip to content

Instantly share code, notes, and snippets.

@cham11ng
Created August 29, 2019 16:26
Show Gist options
  • Save cham11ng/2b7c3adbcd6720f760f4832c7d7a593b to your computer and use it in GitHub Desktop.
Save cham11ng/2b7c3adbcd6720f760f4832c7d7a593b to your computer and use it in GitHub Desktop.
Python Model Script
text = """
_id = StringField(db_column='id', null=False)
_name = StringField(db_column='name', null=False)
_site_id = StringField(db_column='site_id', null=False)
_city = StringField(db_column='city', null=False)
_region = StringField(db_column='region', null=False)
_state = StringField(db_column='state', null=False)
_street_address = StringField(db_column='street_address', null=False)
_tenant = StringField(db_column='tenant', null=False)
_observation_tag = StringField(db_column='observation_tag', null=False)
_organization_id = StringField(db_column='organization_id', null=False)
_longitude = FloatField(db_column='longitude', null=False)
_latitude = FloatField(db_column='latitude', null=False)
_tower_height = FloatField(db_column='tower_height', null=False)
_asset_class_id = IntegerField(db_column='asset_class_id', null=False)
_tower_type_id = IntegerField(db_column='tower_type_id', null=False)
_is_deleted = BoolField(db_column='is_deleted', default=False)
_created_at = TimeStampField(db_column='created_at', null=False, default=time())
_created_by = StringField(db_column='created_by', null=False)
_updated_at = TimeStampField(db_column='updated_at')
_updated_by = StringField(db_column='updated_by'
"""
def split_field(prop_array):
"""Split right hand property."""
return [prop_array[0], prop_array[1].split('(')[:1][0]]
def generate_getter_setter(arg):
"""Generate getter and setter."""
template = """
@property
@{1}.get
def {0}(self):
return self._{0}
@{0}.setter
@{1}.set
def {0}(self, {0}):
self._{0} = {0}
"""
properties = [prop.strip()
for prop in arg.split('\n') if prop is not '']
arg = [split_field(prop.split(' = '))
for prop in properties]
return ''.join([template.format(row[0][1:], row[1]) for row in arg])
print generate_getter_setter(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment