Package: data-mapper
data-mapperA declarative data mapperDisclaimerI don't know if anyone uses this library except for me so I'm not
putting much effort in writing good documentation. If you are using it
or just thinking about using it, give me a sign of your attention (e.g.
star this repo, or open an issue) and this will definitely motivate me
to do it ;)DescriptionMost projects work with different representations of the same data.
The code that is written every time to morph data between its representations
is mostly very repetitive. More over, it is always a deposit of bugs and issues
which requires a developer or tester to unit-test it.This package is an attempt to solve these problems... well, at least the most
common ones.And to make a developer job easier it is primarily designed to be used
in declarative fashion: describe what you want and get it right after.Use Cases and FeaturesHere are examples of the most common use-cases and features:Different Naming SchemesThis mapper looks for propertiesfirst_nameandlast_namein the data.
For propertyfirst_nameit tries to resolve it by the first key'first_name', if not found it tries the second key'name'.
The similar process goes for propertylast_name.fromdata_mapper.mappersimportMapperfromdata_mapper.propertiesimportPropertyclassPersonMapper(Mapper):first_name=Property('first_name','name')last_name=Property('last_name','surname')mapper=PersonMapper()assertmapper.get({'first_name':'Ivan','surname':'Bogush',})=={'first_name':'Ivan','last_name':'Bogush',}assertmapper.get({'name':'Ivan','surname':'Bogush',})=={'first_name':'Ivan','last_name':'Bogush',}This use-case has a story :)It was the first issue I wanted to solve in my other project. I had different
naming schemes in different data sources, and in my databases. All of them used
different names for product categories: 'categories', 'category', 'categoryId'.
I found it very boring to write repeatable code to convert the same data.Arbitrary functions on resolved valuesFull Name String ConstructionThis one resolves propertiesfirst_name,middle_name[optionally] andlast_nameand combines them into a single string —full_name.fromdata_mapper.shortcutsimportF,Str,Lfull_name=F(' '.join,L(Str('first_name'),Str('middle_name',required=False),Str('last_name'),skip_none=True,),)assert'Anton Pavlovich Chekhov'==full_name.get(dict(first_name='Anton',middle_name='Pavlovich',last_name='Chekhov',))assert'Anton Chekhov'==full_name.get(dict(first_name='Anton',last_name='Chekhov',))Object mappingDict toObjectLet's assume we have a classPerson:classPerson:def__init__(self,id_:int,first_name:str,last_name:str,middle_name:str=None,):self.id=id_self.first_name=first_nameself.last_name=last_nameself.middle_name=middle_nameA mapper from dict with corresponding keys to an instance of classPersoncould be defined by subclassingObjectMapper:fromdata_mapper.mappers.objectimportObjectMapperfromdata_mapper.propertiesimport(CompoundProperty,CompoundListProperty,IntegerProperty,StringProperty,)classPersonMapper(ObjectMapper):init=Personargs=CompoundListProperty(IntegerProperty('id'),StringProperty('first_name'),StringProperty('last_name'),)kwargs=CompoundProperty(middle_name=StringProperty(required=False),)first,middle,last='Iosif Aleksandrovich Brodsky'.split()person=PersonMapper().get(dict(id=1940,first_name=first,middle_name=middle,last_name=last,))assertisinstance(person,Person)assertperson.id==1940assertperson.first_name==firstassertperson.middle_name==middleassertperson.last_name==lastExactly the same can be done by instantiating theObjectMapper:fromdata_mapper.mappers.objectimportObjectMapperfromdata_mapper.propertiesimport(CompoundProperty,CompoundListProperty,IntegerProperty,StringProperty,)mapper=ObjectMapper(init=Person,args=CompoundListProperty(IntegerProperty('id'),StringProperty('first_name'),StringProperty('last_name'),),kwargs=CompoundProperty(middle_name=StringProperty(required=False),),)first,middle,last='Iosif Aleksandrovich Brodsky'.split()person=mapper.get(dict(id=1940,first_name=first,middle_name=middle,last_name=last,))assertisinstance(person,Person)assertperson.id==1940assertperson.first_name==firstassertperson.middle_name==middleassertperson.last_name==last