Package: datacast
Datacastis a Python package that validates and converts your data.Basic UsageInstall with pip:pipinstalldatacastDefine schema (can be any class with annotations) and usecastfunction.fromdatacastimportcastclassSimpleSchema:one:inttwo:strthree:(lambdax:x**2)zero:(int,bool)four:float=0.4five:None='five'cast({'one':1,'two':2,'three':3,'zero':'0','five':5},SimpleSchema)# {'one': 1, 'two': '2', 'three': 9, 'zero': False, 'four': 0.4, 'five': 5}Rules are simple:Params without annotations will be ignored.Annotation is acaster, which will be called with the provided value,
eg.bool(0).Casterisanycallable. Functions, lambdas, classes etc.It also can be list or tuple (or another iterable).
Then it acts like a chain ofcasters, eg.int('0')->bool(0)->False.If there is no default value - param is required and
will raiseRequiredFieldErrorif not provided.Nonein annotation means no casting.ConfigYou can useConfigclass which acts like a schema AND stores result data.fromdatacastimportConfigclassSimpleConfig(Config):spam:boolham:Nonerabbit:float=Noneconfig=SimpleConfig({'spam':0,'ham':1})assertconfig.spam==Falseassertconfig.ham==1assertconfig.rabbit==Noneassertconfig._asdict()=={'spam':False,'ham':1,'rabbit':None}Also there isEnvironConfigwhich loads input data from environment,
casts strings to appropriate types and ignores extra vars.fromdatacastimportEnvironConfigclassSimpleEnvironConfig(EnvironConfig):SPAM:boolHAM:intRABBIT:strNONE_VAL:Noneos.environ['SPAM']='0'os.environ['HAM']='1'os.environ['RABBIT']='2'os.environ['NONE_VAL']='null'config=SimpleEnvironConfig()assertconfig.SPAM==Falseassertconfig.HAM==1assertconfig.RABBIT=='2'assertconfig.NONE_VAL==NoneValidNonestrings:'none', 'null', 'nil'ValidTruestrings:'true', 't', 'yes', 'y', 'on', '1'ValidFalsestrings:'false', 'f', 'no', 'n', 'off', '0', ''Case doesn’t matter.SettingsYou can specify various settings and apply them in a bunch of different ways.fromdatacastimportapply_settings,Settings@apply_settings(on_missing='store',missing_value=False)classSimpleSchema:...# ORclassSimpleSettings(Settings):on_missing='store'missing_value=False@apply_settings(SimpleSettings)classSimpleSchema:...# OR pass it to the cast function or Config creationcast(input_data,SimpleSchema,settings=SimpleSettings)cast(input_data,SimpleSchema,on_missing='store',missing_value=False)Config(input_data,settings=SimpleSettings)Config(input_data,on_missing='store',missing_value=False)# OR use class attributeclassSimpleSchema:__settings__=SimpleSettings# OR__settings__={'on_missing':'store','missing_value':False}...List of settingsNameDefaultDescriptionon_extra'ignore'What to do with values that absent from schema.on_invalid'raise'What to do when casting has failed.on_missing'raise'What to do when value is missing but required.missing_valueNoneWhat to store when value is missing.store_callablesFalseIfFalse- execute callable value on store.result_classdictClass which stores result data.precasters()Prepend additional casters.postcasters()Append additional casters.cast_defaultsFalseCast default values with full casters chain.raise_originalFalseRaise original exception instead of CastError.Options for ‘on_extra’, ‘on_invalid’ and ‘on_missing’ignore:Value will be ignored and not be stored in the result.store:Value will be stored in the result as is. In case ofon_missingit
will storemissing_value.raise:Corresponding exception will be raised.cast:Value will be casted with precasters, postcasters and then stored.
Works only withon_extra!Withprecastersandpostcastersyou will transform every caster in
schema into a chain, which starts and/or ends with those casters.