一些Python属性

kingston的Python项目详细描述


我使用优秀的Funcy库 对Python来说很多。这是我设计的一些额外的东西 与funcy紧密合作。Funcy Kingston(参考资料,参见 here)。在

Run on Repl.it

Kingston是使用自动格式化的 yapf。在

使用扩展的dict的模式匹配

match.Match对象是使用dict语义的可调用对象 它还根据调用参数的类型匹配调用:

>>>fromkingstonimportmatch>>>foo=match.TypeMatcher({...int:lambdax:x*100,...str:lambdax:f'Hello {x}'...})>>>foo(10)1000>>>foo('bar')'Hello bar'>>>
^{pr2}$

您可以使用typing.Any作为通配符:

>>>fromtypingimportAny>>>fromkingstonimportmatch>>>foo=match.TypeMatcher({...int:lambdax:x*100,...str:(lambdax:f"Hello {x}"),...(int,Any):(lambdanum,x:num*x)...})>>>foo(10)1000>>>foo('bar')'Hello bar'>>>foo(3,'X')'XXX'>>>foo(10,10)100>>>

还可以将类型匹配器子类化,并使用修饰符声明事例 作为方法:

>>>fromkingston.matchimportMatcher,TypeMatcher,case>>>fromnumbersimportNumber>>>classNumberDescriber(TypeMatcher):...@case...defdescribe_one_int(self,one:int)->str:...return"One integer"......@case...defdescribe_two_ints(self,one:int,two:int)->str:...return"Two integers"......@case...defdescribe_one_float(self,one:float)->str:...return"One float">>>my_num_matcher:Matcher[Number,str]=NumberDescriber()>>>my_num_matcher(1)'One integer'>>>my_num_matcher(1,2)'Two integers'>>>my_num_matcher(1.0)'One float'>>>

输入模式匹配器

match.Match对象可以使用Python的标准进行类型化 typing机制。它 使用完成 Generics

这两个子类型是[参数类型,返回类型]。在

>>>fromkingstonimportmatch>>>foo:match.Matcher[int,int]=match.TypeMatcher({...int:lambdax:x+1,...str:lambdax:'hello'})>>>foo(10)11>>>foo('bar')# fails on mypy but would be ok at runtime'hello'>>>

按值匹配

match.ValueMatcher将使用参数的来执行 与match.Match相同:

>>>fromkingstonimportmatch>>>foo=match.ValueMatcher({'x':(lambda:'An x!'),('x','y'):(lambdax,y:3*(x+y))})>>>foo('x')'An x!'>>>foo('x','y')'xyxyxy'>>>

{$4通配符与上面的^匹配器相同 还有价值匹配器:

>>>fromkingstonimportmatch>>>fromtypingimportAny>>>foo=match.ValueMatcher({...'x':lambdax:'An X!',...('y',Any):lambdax,y:3*(x+y)...})>>>foo('x')'An X!'>>>foo('y','x')'yxyxyx'>>>

您也可以在自定义^{tt9}中将case声明为方法$ 子类。在

使用函数value_case()声明值大小写。注意:作为速记导入:

>>>fromkingston.matchimportMatcher,ValueMatcher>>>fromkingston.matchimportvalue_caseascase>>>classSimplestEval(ValueMatcher):...@case(Any,'+',Any)...def_add(self,a,op,b)->int:...returna+b......@case(Any,'-',Any)...def_sub(self,a,op,b)->int:...returna-b>>>simpl_eval=SimplestEval()>>>simpl_eval(1,'+',2)3>>>simpl_eval(10,'-',5)5>>>

好东西

从几乎任何对象获取深层价值。有点受CSS的启发 选择器,但不是很完整。API的这一部分不稳定 将来(希望)会进一步发展。在

>>>fromkingstonimportdig>>>dig.xget((1,2,3),1)2>>>dig.xget({'foo':'bar'},'foo')'bar'>>>dig.dig({'foo':1,'bar':[1,2,3]},'bar.1')2>>>dig.dig({'foo':1,'bar':[1,{'baz':'jox'},3]},'bar.1.baz')'jox'>>>

dig.dig()funcy.get_in()之间的区别在于 可以使用类似shell的blob模式来获取由相似的 姓名:

>>>fromkingstonimportdig>>>res=dig.dig({'foo':1,'foop':2},'f*')>>>res[foo=1:int,foop=2:int]>>># (textual representation of an indexable object)>>>res[0]foo=1:int>>>res[1]foop=2:int>>>

测试工具

金斯顿也有一些测试工具。另外,由于金斯敦的 固执己见的天性,他们只针对 pytest。在

缩写pytest.mark.parametrize

我倾向于使用pytest.mark.parametize参数化到处都是同样的形式。因此 我已经实现了这个简短的表单:

>>>fromkingston.testingimportfixture>>>@fixture.params(..."a, b",...(1,1),...(2,2),...)...deftest_dummy_compare(a,b):...asserta==b>>>

作为固定装置的医生

有一个测试装饰器可以从函数生成pytest fixture 或者一个物体。这样使用:

>>>defmy_doctested_func():..."""
...   >>> 1 + 1
...   2
...   >>> mystring = 'abc'
...   >>> mystring
...   'abc'
...   """...pass>>>fromkingston.testingimportfixture>>>@fixture.doctest(my_doctested_func)...deftest_doctest_my_doctested(doctest):# fixture name always 'doctest'...res=doctest()...assertres=='',res>>>

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

推荐PyPI第三方库


热门话题
构造函数的java条件调用   类Dog中的java构造函数Dog不能应用于给定类型   java jsch和运行“sudo su”   java将队列和堆栈相互复制   java如何在netbeans项目的文件夹中添加库   java While循环在我的代码中不存在   如何在XML中使用java方法的返回值   java是否可以在不写入文件的情况下将字符串/字节数组作为文件发布?   java为什么这些字符串不相等?   sockets客户机-服务器java编程,用户可选择   java如何在SpringMVC和hibernate中保存模型返回视图的列表   java如何修复组织。openqa。硒。WebDriverException:未知错误   Java,Ant错误:编码Cp1252的不可映射字符   JAVAlang.ClassCastException:[Ljava.lang.String;与java.lang.String不兼容   java如何使用JDK8(可选)为空字段创建自定义IntelliJ getter模板   java Tomcat6响应。sendRedirect()404错误