Package: dripper
Cleaning your messy data.ContentsGetting startedInstallationBasicsAdvancedCHANGESGetting startedConsider cleaning up some messy data.
Here is a deep nested dictionary containing lots of unnecessary nesting and tuple.some_messy_data={"body":{"article":{"articlesbody":{"articlesmeta":{"articles_meta_3":"Monty Python",}}},},"published":{"datetime":("2014-11-05","23:00:00"),}}Values you want are'Monty Python'and'2014-11-05',
should be named'title'and'published_date'Now let the hack begin with thedripper.DefiledeclarationdictionaryCreate dripper object bydripper.dripper_factoryDrip essential data# Definedeclaration={"title":("body","article","articlesbody","articlesmeta","articles_meta_3"),"published_date":("published","datetime",0)}# Createimportdripperd=dripper.dripper_factory(declaration)# And dripdripped=d(some_messy_data)assertdripped=={"title":"Monty Python","published_date":"2014-11-05",}InstallationJust use pip to installpip install dripperRequirementsdripperwon’t require any kind of outer libraries.
Supporting Python versions are:Python 2.7Python 3.3Python 3.4Python 3.5BasicsAbove example is not all features ofdripper.
It is created to handle various data to clean up.As valuefromdripperimportdripper_factorydeclaration={"title":("meta","meta1")})d=dripper_factory(declaration)d({"meta":{"meta1":"Monty Python"}})=={"title":"Monty Python"}Also you can specify string or integer directly.
It is as same as one-element tuple.fromdripperimportdripper_factorydeclaration={"title":"meta"})d=dripper_factory(declaration)d({"meta":"Monty Python"})=={"title":"Monty Python"}As dictdrippercan define nested dictionary.
Just pass nested dictionary todripper_factory.fromdripperimportdripper_factorydeclaration={"article":{"title":["meta","meta1"],}})d=dripper_factory(declaration)d({"meta":{"meta1":"Monty Python",},})=={"article":{"title":"Monty Python",}}You can apply'__source_root__'to set root path for dripping.declaration={"article":{"__source_root__":("body","meta"),..."title":"meta1","author":("meta2","meta22"),}})d=dripper_factory(declaration)d({"body":{"meta":{"meta1":"Monty Python","meta2":{"meta22":"John Due"}}}})=={"article":{"title":"Monty Python","author":"John Due",}}Technically, outermost dictionary ofdeclarationis as same as inner dictionaries.
So you can specify'__source_root__'the dictionary.As listdrippercan define list of dictionaries.
You need to apply'__type__': 'list'.fromdripperimportdripper_factorydeclaration={"articles":{"__type__":"list","__source_root__":"articles",..."title":"meta1","author":["meta2","meta22"],}})d=dripper_factory(declaration)d({"articles":[{"meta1":"Monty Python","meta2":{"meta22":"John Doe"}},{"meta1":"Flying Circus","meta2":{"meta22":"Jane Doe"}},]})=={"articles":[{"title":"Monty Python","author":"John Doe"},{"title":"Flying Circus","author":"Jane Doe"},]}AdvancedConvertingUsedripper.ValueDripperto pass converter function.importdripperdeclaration={"title":dripper.ValueDripper(["title"],converter=lambdas:s.lower())}d=dripper.dripper_factory(declaration)d({"title":"TITLE"})=={"title":"title"}Technically, each ends (list) will be replaced by instance ofdripper.ValueDripper.default valueSpecifydefaultkeyword argument to change default value.Nonewill be applied as default.importdripperdeclaration={"title":dripper.ValueDripper(["title"],default="default")}d=dripper.dripper_factory(declaration)d({})=={"title":"default"}Technically, each ends (list) will be replaced by instance ofdripper.ValueDripper.CombiningBy combiningdripper.ValueDripper, result value of that key will be combined.importdripperdeclaration={"fullname":(dripper.ValueDripper(["firstname"])+dripper.ValueDripper(["lastname"]))}d=dripper.dripper_factory(declaration)d({"firstname":"Hrioki","lastname":"Kiyohara"})=={"fullname":"HriokiKiyohara"}CHANGES1.2Avoid deepcopy to improve speedhttps://github.com/hirokiky/dripper/pull/6Thanks @afiram1.1Noneis default value ofValueDripperBefore this changeValueDripperwithoutdefaultkeyword argument
will raiseDrippingErrorIn order to this behaviorDictDripperwill return empty dict
when inner value dripper could not dig out valuesThanks for @bungoume to suggest this behavior1.0Officially supported Python 3.50.3.1ValueDripper now acceptsdefaultargument.0.3Fixed to accept string or integer directly as source_root.0.2Improved error handling.Added MixDripper.0.1Initial version