Using the power of varname and a class column like the following:
from varname import varname
class column(str):
def __new__(cls, dtype: type = str, alias: str = None, key: bool = False):
if alias is None:
alias = str(varname())
return str.__new__(cls, alias)
def __init__(self, dtype: type = str, alias: str = None, key: bool = False):
self.dtype = dtype
self.key = key
We can define schemas in nested classes inside the table. For example:
class MyTable(Table):
class schema:
id = column(int)
name = column()
See some examples of tests that are all passing:
from deleteme2 import column
def test_column_with_type():
class schema:
id = column(int)
assert schema.id == "id"
assert schema.id.dtype == int
assert schema.id.key == False
def test_column_with_alias():
class schema:
id = column(int, alias="identity")
assert schema.id == "identity"
assert schema.id.dtype == int
assert schema.id.key == False
def test_column_with_key():
class schema:
id = column(key=True)
assert schema.id == "id"
assert schema.id.dtype == str
assert schema.id.key == True
def test_column_default_params():
class schema:
id = column()
assert schema.id == "id"
assert schema.id.dtype == str
assert schema.id.key == False
def test_nested_schema():
class schema:
id = column()
class nested:
id_nested = column()
assert schema.id == "id"
assert schema.nested.__name__ == "nested"
assert schema.nested.id_nested == "id_nested"
Using the power of varname and a class column like the following:
We can define schemas in nested classes inside the table. For example:
See some examples of tests that are all passing: