在python中,在snake case、camel case和pascal case之间转换字符串(和字典键)。以节点驼峰为灵感
pyhumps的Python项目详细描述
在python中,在snake case、camel case和pascal case之间转换字符串(和字典键)。灵感来源于node的Humps。
安装
要安装驼峰,只需使用pipenv(当然也可以是pip):
$ pipenv install pyhumps
用法
转换字符串
importhumpshumps.camelize('jack_in_the_box')# jackInTheBoxhumps.decamelize('rubyTuesdays')# ruby_tuesdayshumps.pascalize('red_robin')# RedRobin
转换字典键
importhumpsarray=[{'attrOne':'foo'},{'attrOne':'bar'}]humps.decamelize(array)# [{'attr_one': 'foo'}, {'attr_one': 'bar'}]array=[{'attr_one':'foo'},{'attr_one':'bar'}]humps.camelize(array)# [{'attrOne': 'foo'}, {'attrOne': 'bar'}]array=[{'attr_one':'foo'},{'attr_one':'bar'}]humps.pascalize(array)# [{'AttrOne': 'foo'}, {'AttrOne': 'bar'}]
检查字符大小写
importhumpshumps.is_camelcase('illWearYourGranddadsClothes')# Truehumps.is_pascalcase('ILookIncredible')# Truehumps.is_snakecase('im_in_this_big_ass_coat')# Truehumps.is_camelcase('from_that_thrift_shop')# Falsehumps.is_snakecase('downTheRoad')# False# what about abbrevations, acronyms, and initialisms? No problem!humps.decamelize('APIResponse')# api_response
驼峰食谱
pythonic boto3 api包装器
# aws.pyimporthumpsimportboto3defapi(service,decamelize=True,*args,**kwargs):service,func=service.split(':')client=boto3.client(service)kwargs=humps.pascalize(kwargs)response=getattr(client,func)(*args,**kwargs)return(depascalize(response)ifdecamelizeelseresponse)# usageapi('s3:download_file',bucket='bucket',key='hello.png',filename='hello.png')