在python中,在snake case、camel case和pascal case之间转换字符串(和字典键)。以节点驼峰为灵感

pyhumps的Python项目详细描述


Humps logo

pypipypi

在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')

通用api响应返回管理器
fromfunctoolsimportwrapsimportenumimporthumpsclassFlags(enum.Enum):RAW=1JSON=2STATUS_CODE=4OK=8DECAMELIZE=16defreturning(api_exception=Exception):defdecorator(fn):@wraps(fn)defwrapper(*args,**kwargs):flags=[]if'returning'inkwargs:returning=kwargs.pop('returning')ifisinstance(returning,Flags):flags.append(returning)else:flags.extend(returning)flags.extend([aforainargsifisinstance(a,Flags)])args=[aforainargsifnotisinstance(a,Flags)]resp=fn(*args,**kwargs)is_json=resp.headers.get('Content-Type')=='application/json'ifnotflagsorFlags.RAWinflags:returnrespifFlags.OKinflags:returnresp.okifFlags.STATUS_CODEinflags:returnresp.status_codeifFlags.JSONinflags:ifnotresp.ok:raiseapi_exception(resp.json()ifis_jsonelseresp.text)ifFlags.DECAMELIZEinflags:returnhumps.decamelize(resp.json())else:returnresp.json()returnwrapperreturndecorator# usageimportrequests@returning()defget_todo(todo_id):returnrequests.get('https://jsonplaceholder.typicode.com/posts/1')get_todo(1)# <Response [200]> (true method return)get_todo(1,Flags.JSON)# {'userId': 1, 'id': 1, 'title': '...'}get_todo(1,Flags.JSON,Flags.DECAMELIZE)# {'user_id': 1, 'id': 1, 'title': '...'}get_todo(1,Flags.OK)# Trueget_todo(1,Flags.STATUS_CODE)# 200

烧瓶式静止自适应响应

# I will post a code snippet for this soon. It's a decorator that checks if# the request arguments were passed as camelcase or snake_case, it then# rewrites the response to match the consumer's preferred casing style.

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

推荐PyPI第三方库


热门话题
java如何将jaxb插件扩展与gradlejaxbplugin一起使用   java Hibernate列表<Object[]>到特定对象   java使用多态性显示arraylist的输出   java水平堆叠卡,带有一定偏移量   java错误:找不到符号方法liesInt()   java客户机/服务器文件收发中的多线程流管理   在java中可以基于访问重载方法吗?   包含空元素的java排序数组   swing Java按钮/网格布局   java BottomNavigationView getmaxitemcount   java空指针异常字符串生成器   java Xamarin升级导致“类文件版本错误52.0,应为50.0”错误   java我正在尝试打印它,而不只是对每一行进行println   Tomcat7中的java是否需要复制上下文。将xml转换为conf/Catalina/locahost以使其生效   带有注入服务的java REST端点在何处引发自定义WebServiceException?   在Java中使用GPS数据   java如何将JFreeChart ChartPanel导出到包含添加的CrosshairOverlay的图像对象?   内置Eclipse期间的Java 8堆栈溢出   java在GWT编译的JavaScript中如何表示BigDecimal