A utility for defining metadata for data types and formats.
Project description
Metaform is a package for hierarchical and nested data normalization.
pip install metaform
import metaform
Basic Usage
Let’s say we have some data:
data = {
'hello': 1.0,
'world': 2,
'how': ['is', {'are': {'you': 'doing'}}]
}
We can get the template for defining schema, by metaform.template:
metaform.template(data)
{‘*’: ’’, ’hello’: {’*’: ’‘}, ’how’: [{‘*’: ’’, ’are’: {’you’: {’*’: ’’}}}], ‘world’: {’*‘:’’}}
This provides an opportunity to specify metadata for each key and the object itself. For example:
schema = {
'*': 'greeting',
'hello': {'*': 'length'},
'world': {'*': 'atoms'},
'how': [
{'*': 'method',
'are': {
'*': 'yup',
'you': {'*': 'me'}}
}
]}
metaform.normalize(data, schema)
{‘atoms’: 2, ‘length’: 1.0, ‘method’: [‘is’, {‘yup’: {‘me’: ‘doing’}}]}
We recommend saving schemas you create for normalizations for data analytics and driver projects in dot-folders .schema, in a JSON or YAML files in that folder.
So, we have access to all keys, and can specify, what to do with them:
schema = {
'*': 'greeting',
'hello': {'*': 'length|lambda x: x+5.'},
'world': {'*': 'atoms|lambda x: str(x)+"ABC"'},
'how': [
{'*': 'method',
'are': {
'*': 'yup',
'you': {'*': 'me|lambda x: "-".join(list(x))'}}
}
]}
metaform.normalize(data, schema)
{‘atoms’: ‘2ABC’, ‘length’: 6.0, ‘method’: [‘is’, {‘yup’: {‘me’: ‘d-o-i-n-g’}}]}
And suppose, we want to define a more complex function, inconvenient via lambdas:
from metaform import converters
def some_func(x):
a = 123
b = 345
return (b-a)*x
converters.func = some_func
schema = {
'*': 'greeting',
'hello': {'*': 'length|converters.func'},
'world': {'*': 'atoms|lambda x: str(x)+"ABC"'},
'how': [
{'*': 'method',
'are': {
'*': 'yup',
'you': {'*': 'me|lambda x: "-".join(list(x))'}}
}
]}
metaform.normalize(data, schema)
{‘atoms’: ‘2ABC’, ‘length’: 222.0, ‘method’: [‘is’, {‘yup’: {‘me’: ‘d-o-i-n-g’}}]}
We just renamed the keys, and normalized values! What else could we want?
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.