Skip to content

Commit

Permalink
Add basic ORM example
Browse files Browse the repository at this point in the history
  • Loading branch information
RadoRado committed Feb 8, 2016
1 parent 8970d4e commit c795424
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions week11/materials/orm.py
@@ -0,0 +1,70 @@
import inspect

TYPES = {
int: 'INTEGER',
str: 'VARCHAR'
}


def map_python_to_sql(py_type):
if py_type not in TYPES:
return TYPES[str]

return TYPES[py_type]


def varchar():
return str


def integer():
return int


def pluralize(string):
plural_forms = ['s', 'es']

for form in plural_forms:
if string.endswith(form):
return string

# FIXME: Not a proper english
return string + 's'


def extract_dict_from_class(cls):
members = [m for m in inspect.getmembers(cls)
if '__' not in m[0]]
attributes = {m[0]: m[1] for m in members}
return (cls.__name__, attributes)


def class_to_table(name, attributes):
create_sql = """
CREATE TABLE {name}(
id INTEGER PRIMARY KEY AUTO INCRAMENT,
{columns}
);
"""
data = {
"name": pluralize(name),
"columns": ""
}

columns = []
for attr in attributes:
value_type = attributes[attr]
columns.append("{} {}".format(attr,
map_python_to_sql(value_type)))

data['columns'] = ",\n".join(columns)
return create_sql.format(**data)


class Person:
email = varchar()
password = varchar()
age = integer()

print(class_to_table(*extract_dict_from_class(Person)))

0 comments on commit c795424

Please sign in to comment.