字符串的实用程序。

stroke的Python项目详细描述


安装

python3 -m pip install stroke

有助于创建简单的基于命令的界面

importstrokecommands=stroke.State()# create command@commands.sub('echo')defcommands_0(content):print(content)# this is a subcommand@commands_0.sub('loud')defcommands_0_0(content):content=content.upper()print(content)# another subcommand with aliases@commands_0.sub('pretty','aesthetic')defcommands_0_1(content):content=' '.join(iter(content))print(content)defmain(content):# analyse into names separated by .# anything after a space is the argument# use the names to find the invoke# KeyError will be raised on invalid trail(names,argument,invoke)=commands.analyse(content)invoke(argument)main('echo marco')# not polomain('echo.loud whoa')# > WHOAmain('echo.aesthetic fancy words')# chill vibes

用于将参数解析为部分

importstrokecommands=stroke.State()figure_0={'-and':'int'}@commands.sub('add')defcommands_0(content):# list of (flag, value)arguments=stroke.craft(figure_0,content)value=0# flag is always -andfor(flag,argument)inarguments:value+=int(argument)print('result',value)figure_1={'-upper':'numerator','-lower':'denominator'}@commands.sub('div')defcommands_1(content):# same thingarguments=stroke.craft(figure_1,content)# ignore multiple flagsarguments=dict(arguments)# we need every flag for this onevalues=(arguments[key]forkeyinfigure_1)# convert and unpack(upper,lower)=map(int,values)# handle errors on demandvalue=upper/lowerprint('result',value)figure_2={'-and':{'-op':'name','-co':'args'}}@commands.sub('calc')defcommands_2(content):# this is actually a generatorarguments=stroke.craft(figure_2,content)# here, argument is also onefor(flag,argument)inarguments:# we only need one invokeinvoke=None# can be executed multiple timescontents=[]for(flag,argument)inargument:# signal for nameifflag=='-op':# ignore emptiesifnotargument:continue# only firstifnotinvoke:# get the invoke from nameinvoke=commands.trail(argument)continue# no need to check if -cocontents.append(argument)# execute commandsforcontentincontents:invoke(content)defmain(content):(names,argument,invoke)=commands.analyse(content)invoke(argument)print('>',stroke.draw(figure_0))main('add 5 -and 9 -and -3')# 11print('>',stroke.draw(figure_1,full=True))main('div -upper 20 -lower 2 -upper 15')# 7.5print('>',stroke.draw(figure_2,full=True))main('calc add -co 1 \-and 2 -and div -co -upper 1 -lower 4')# 3 and 0.25

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

推荐PyPI第三方库


热门话题
java在ArrayList中比较数字   java在Kotlin中使异步调用同步   让“Scala编程”junit示例在IntelliJ中工作的java问题   java Servlet侦听器未在ContextListener中设置属性   将Microsoft SQL Server数据库连接到我的Java项目   加载资源时出现java“需要注册工厂”异常   java如何使用POI检查excel中的重复记录?   java如何更改机器生成的代码   java如何确保重写的方法是同步的   用Spring编写Hibernate时的java XML奥秘   java管理mysql数据库中存储的用户权限   java如何运行。来自Javascript的jar方法   java我想在Web应用程序中进行身份验证&对桌面应用程序使用相同的凭据。我该怎么做?