字符串派生的数据操作。

strdata的Python项目详细描述


用法

importstrdataimportfunctoolsreject=(None,)skip=reject.__contains__types=('feline','canis')pushers={# what the asset is going to be looked up by'name':(# what the unparsed value will be requested as'name',# what the parsed value will be set against'name',# the function used for transforming;# utils.apply uses the result of the first# function in the succeeding ones for validating itstrdata.utils.apply(strdata.pushers.string(),strdata.utils.apply(len,strdata.checks.range(2,32,left=False)))),'alive':('alive','alive',# raw pushers for no validationstrdata.pushers.boolean()),'gold coins':('gold_coins','gold_coins',strdata.utils.apply(strdata.pushers.decimal(),strdata.checks.range(0,6.5))),'interests':('likes','likes',strdata.utils.apply(strdata.pushers.array(strdata.utils.apply(functools.partial(strdata.pushers.string(),# we have to do this because# pushers require to know the# previous state of the value# they parse (even if sometimes# it's not really used for anything);# since we can't schemanticaly derive# this value from an array, we have# to explicitely pass it; (pushers.string# doesn't use the state like most pushers)None),strdata.utils.apply(len,strdata.checks.range(4,16)))),strdata.utils.apply(len,strdata.checks.range(1,6)))),'type':('type','type',strdata.utils.apply(strdata.pushers.string(),strdata.checks.include(types))),'status':('status','status',strdata.utils.apply(strdata.pushers.string(),strdata.utils.apply(len,strdata.checks.range(4,16))))}pullers={'name':('name','nick',# pullers need no validation;# their job is to convert any data type to strstrdata.pullers.string(),),'gold coins':('gold_coins','gold coins',strdata.pullers.decimal(point=2)),'type':('type','kind',strdata.pullers.string()),'alive':('alive','alive',strdata.pullers.boolean(options=('negative','positive'))),'likes':('likes','interests',strdata.pullers.array(strdata.pullers.string())),'status':('status','status',strdata.pullers.string())}data={'name':'kitty','type':'canis','alive':True,'gold_coins':4.234381,'likes':['pats','meows'],'status':'angry'}test=('name ''-and alive -set f ''-and type -set feline ''-and interests -set -add purrs -pop meows -add yarn ''-and gold coins -set 5.234 ''-and status -set ')print(test)# single is for display; pairs is for updatesingle,pairs=strdata.parsers.pair(test)extras,junk=zip(*pairs)# here we "pretend" our pair's keys have# also been passed with no -set to let the# program know we are also seeking to view# those (ie. let pullers parse and return)single.extend((extra,)forextrainextras)# need a getterget=data.__getitem__# this is our new dataresult=strdata.parsers.move(pushers,get,pairs)print('old: ',data)# update our store with itdata.update(result)print('new: ',data)# this is our new data, but all values are stringsresult=strdata.parsers.move(pullers,get,single)# most results are generators# yielding key-value tuple pairsfinal=dict(result)print('show:',final)

播放测试并检查值。
有推送和检查错误,可以提出!

安装

python3 -m pip install strdata

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

推荐PyPI第三方库


热门话题
字符串Java字母替换无效   java Spring Roo JPA MS SQL Server无法打开JPA EntityManager组织。冬眠例外GenericJDBCException:无法打开连接   在scala中使用JavaWS对大型数据文件进行java流式处理   Java编译器是否将字节和短字符识别为文本?   java无法查找符号错误,空指针   mongodb在Java中重用数据库连接   java将多个StringArray从字符串文件获取到活动中   java是一个变量,它只保存最后一次鼠标单击的坐标   c#尺寸有限;添加、删除和洗牌   java如何在Android中显示来自资产文件夹的文本文件中的文本   Android应用程序中的java Tensorflow Lite自定义对象检测模型错误   java如何在foreachloop中使用scanner将来自命令行的输入存储到数组中   java如何定义一个好的存储库接口   Android中的java解析动态json对象