简单的方式来投射你的数据。

datacast的Python项目详细描述


datacast是一个验证和转换数据的python包。


Latest version released on PyPIMinimal Python versionTest coveragePackage license


基本用法

使用pip安装:

pip install datacast

定义模式(可以是任何带有注释的类)并使用cast函数。

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}

规则很简单:

  • 不带注释的参数将被忽略。
  • 注释是一个caster,将使用提供的值调用它, 例如bool(0)
  • casterany可调用的。函数、lambdas、类等
  • 它也可以是list或tuple(或另一个iterable)。 然后它就像一个casters的链,例如int('0')-> bool(0) -> False
  • 如果没有默认值-param是必需的,并且 如果不提供,将筹集RequiredFieldError
  • None在注释中表示不强制转换。

配置

您可以使用Config类,该类的行为类似于模式并存储结果数据。

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}

还有EnvironConfig从环境中加载输入数据, 将字符串强制转换为适当的类型并忽略额外的变量。

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==None
Valid ^{tt5}$ strings:
^{tt9}$
Valid ^{tt10}$ strings:
^{tt11}$
Valid ^{tt12}$ strings:
^{tt13}$

案子不重要。

设置

您可以指定各种设置并以各种不同的方式应用它们。

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}...

设置列表

NameDefaultDescription
on_extra^{tt14}$What to do with values that absent from schema.
on_invalid^{tt15}$What to do when casting has failed.
on_missing^{tt15}$What to do when value is missing but required.
missing_value^{tt5}$What to store when value is missing.
store_callables^{tt12}$If ^{tt12}$ - execute callable value on store.
result_class^{tt20}$Class which stores result data.
precasters^{tt21}$Prepend additional casters.
postcasters^{tt21}$Append additional casters.
cast_defaults^{tt12}$Cast default values with full casters chain.
raise_original^{tt12}$Raise original exception instead of CastError.

选项用于“on_extra”、“on_invalid”和“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 of ^{tt25}$ it will store ^{tt26}$.
raise:Corresponding exception will be raised.
cast:Value will be casted with precasters, postcasters and then stored. Works only with ^{tt27}$!

使用precasterspostcasters,您将转换中的每个施法者 架构成一个链,链以那些施法者开始和/或结束。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java实现双锁并发队列   java如何更改SpringWS 2.4.4版或更高版本中maxOccurs的默认限制?   java Selenium RC如何处理动态行?   java何时引入Javac StringBuilder/StringBuffer优化?   java mediaplayer在R.raw Android上失败   java JPA2:不区分大小写,就像在任何地方匹配一样   如何从JDK15中提取java预览类文件?   java与mySQL删除查询不一致   从Shutdownow()返回的java列表<Runnable>无法转换为提交的Runnable   java如何从回调接口获取对象值?   java如何获得视图的高度,在xml中可见性和高度定义为wrap_内容?   postgresql Mybatis在启动时遇到“由以下原因引起:java.net.UnknownHostException:localhost”,为什么?   带有实例调用的方法调用的java intellij格式化程序换行   由于特定的第三方库,java Proguard产生运行时错误   动画JAVA JPanel同时滑出和滑入