性能分析实用程序的集合

parserutils的Python项目详细描述


解析路标

Build StatusCoverage Status

这是一个实用函数库,旨在使开发人员的生活更轻松。

这个库中的函数被编写为performant和pythonic,并且与python 2.7到3.6兼容。 它们都有文档记录,并由单元测试完全覆盖,单元测试完全描述和证明了它们的行为。

一般来说,我的理念是,实用函数应该快速并处理边缘情况,这样调用者就不必采取各种预防措施或对结果进行类型检查。 因此,在这个库中,如果没有一个函数会中断函数,则只按原样返回;如果对值没有任何操作,则返回结果而不进行处理;否则,将成功处理值或返回标准异常。

但这只是一个起点。我欢迎反馈和其他功能的要求。

安装

pip install parserutils安装。

用法

下面是您可以对dict对象和其他集合执行的操作。

fromparserutilsimportcollectionscollections.accumulate_items([('key','val1'),('key','val2'),('key','val3')])# {'key': ['val1', 'val2', 'val3']}collections.accumulate_items([('key1','val1'),('key2','val2'),('key3','val3')],reduce_each=True# {'key1': 'val1', 'key2': 'val2', 'key3': 'val3'})collections.setdefaults({},'a.b')# {'a': {'b': None}}collections.setdefaults({},['a.b','a.c'])# {'a': {'b': None, 'c': None}}collections.setdefaults({},{'a.b':'bbb','a.c':'ccc'})# {'a': {'b': 'bbb', 'c': 'ccc'}}collections.filter_empty(xforxin(None,[],['a'],'',{'b'},'c'))# [['a'], {'b'}, 'c']collections.flatten_items(xforxin('abc',['a','b','c'],('d','e')))# ['abc', 'a', 'b', 'c', 'd', 'e']collections.remove_duplicates('abcdefabc')# 'abcdef'collections.remove_duplicates('abcdefabc',in_reverse=True)# 'defabc'collections.remove_duplicates(['a','b','c','a'])# ['a', 'b', 'c']collections.remove_duplicates(('a','b','c','a'),in_reverse=True)# ('b', 'c', 'a')collections.remove_duplicates(xforxin'abca')# ['a', 'b', 'c']collections.remove_duplicates((xforxin'abca'),in_reverse=True)# ['b', 'c', 'a']collections.remove_duplicates((set(x)forxin'abca'),is_hashable=True)# [{'a'}, {'b'}, {'c'}]collections.rindex('aba','a')# 2collections.rindex(['a','b','a'],'a')# 2collections.rindex(('a','b','a'),'a')# 2collections.rindex('xyz','a')# ValueErrorcollections.rindex([xforxin'xyz'],'a')# ValueErrorcollections.rfind('aba','a')# 2collections.rfind(['a','b','a'],'a')# 2collections.rfind(('a','b','a'),'a')# 2collections.rindex('xyz','a')# -1collections.rfind([xforxin'xyz'],'a')# -1collections.reduce_value(['abc'])# 'abc'collections.reduce_value(('abc',))# 'abc'collections.reduce_value({'abc'})# 'abc'collections.reduce_value('abc')# 'abc'collections.reduce_value({'a':'aaa'})# {'a': 'aaa'}collections.reduce_value([{'a':'aaa'}])# {'a': 'aaa'}collections.reduce_value(['a','b','c'])# ['a', 'b', 'c']collections.wrap_value(['abc'])# ['abc']collections.wrap_value(('abc',))# ('abc',)collections.wrap_value('abc')# ['abc']collections.wrap_value(xforxin'abc')# ['a', 'b', 'c']collections.wrap_value({'a':'aaa'})# [{'a': 'aaa'}]collections.wrap_value(['a','b','c'])# ['a', 'b', 'c']

这里有一点关于日期和数字。

fromparserutilsimportdatesfromparserutilsimportnumbers# Leverages dateutil in general, but also handles milliseconds and provides defaultsdates.parse_dates(None,default='today')# Today (default behavior)dates.parse_dates(None,default=None)# Returns Nonedates.parse_dates('nope',default=None)# Returns Nonedates.parse_dates(0)# 1970dates.parse_dates('<date_format>')# Behaves as described in dateutil library# Reliably handles all the usual casesnumbers.is_number(0)# Integer: Truenumbers.is_number(1.1)# Float: Truenumbers.is_number('2.2')# String: Truenumbers.is_number(False)# Boolean: False by defaultnumbers.is_number(False,if_bool=True)# Boolean: True if you need it tonumbers.is_number(float('inf'))# Infinite: Falsenumbers.is_number(float('nan'))# NaN: False

这里有一些关于字符串和url解析帮助程序的内容。

fromparserutilsimportstringsfromparserutilsimporturls# These string conversions are written to be fast and reliablestrings.camel_to_constant('toConstant')# TO_CONSTANTstrings.camel_to_constant('XMLConstant')# XML_CONSTANTstrings.camel_to_constant('withNumbers1And2')# WITH_NUMBERS1_AND2strings.camel_to_snake('toSnake')# to_snakestrings.camel_to_snake('withXMLAbbreviation')# with_xml_abbreviationstrings.camel_to_snake('withNumbers3And4')# with_numbers3_and4strings.snake_to_camel('from_snake')# fromSnakestrings.snake_to_camel('_leading_and_trailing_')# leadingAndTrailingstrings.snake_to_camel('extra___underscores')# extraUnderscoresstrings.find_all('ab??ca??bc??','??')# [2, 6, 10]strings.find_all('ab??ca??bc??','??',reverse=True)# [10, 6, 2]strings.find_all('ab??ca??bc??','??',limit=2,reverse=True)# [10, 6]strings.find_all('ab??ca??bc??','??',start=4)# [6, 10]strings.find_all('ab??ca??bc??','??',end=8)# [2, 6]strings.find_all('ab??ca??bc??','??',start=4,end=8)# [6]strings.splitany('ab:ca:bc',',')# Same as 'ab:ca:bc'.split(':')strings.splitany('ab:ca:bc',',',1)# Same as 'ab:ca:bc'.split(':', 1)strings.splitany('ab|ca:bc','|:')# ['ab', 'ca', 'bc']strings.splitany('ab|ca:bc',':|',1)# ['ab', 'ca:bc']strings.splitany('0<=3<5',['<','<='])# ['0', '3', '5']strings.splitany('0<=3<5',['<','<='],1)# ['0', '3<5']strings.to_ascii_equivalent('smart quotes, etc.')# Replaces with ascii quotes, etc.# URL manipulation leverages urllib, but spares you the extra codeurls.get_base_url('http://www.params.com?a=aaa')# 'http://www.params.com/'urls.get_base_url('http://www.path.com/test')# 'http://www.path.com/'urls.get_base_url('http://www.path.com/test',include_path=True)# 'http://www.path.com/test/'urls.get_base_url('http://www.params.com/test?a=aaa',True)# 'http://www.params.com/test/'urls.update_url_params('http://www.params.com?a=aaa',a='aaa')# 'http://www.params.com?a=aaa'urls.update_url_params('http://www.params.com?a=aaa',a='xxx')# 'http://www.params.com?a=xxx'urls.update_url_params('http://www.params.com',b='bbb')# 'http://www.params.com?b=bbb'urls.update_url_params('http://www.params.com',c=['c','cc'])# 'http://www.params.com?c=c&c=cc'# Helpers to parse urls to and from parts: parses path as list and params as dicturls.url_to_parts('http://www.params.com/test/path?a=aaa')# SplitResult(..., path=['test', 'path'], query={'a': 'aaa'})urls.parts_to_url({'netloc':'www.params.com','query':{'a':'aaa'}# 'http://www.params.com?a=aaa')urls.parts_to_url(urls.url_to_parts('http://www.params.com/test/path?a=aaa')# 'http://www.params.com/test/path?a=aaa')

最后,还支持XML解析,使用CelementTree和DefusedXML库来提高性能和安全性

fromparserutilsimportelements# First convert an XML string to an Element objectxml_string='<root><parent><child>one</child><child>two</child><uglyChild>yuck</uglyChild></parent></root>'xml_element=elements.get_element(xml_string)# Update the XML string and print it back outelements.set_element_text(xml_element,'parent/child','child text')elements.set_element_attributes(xml_element,childHas='child attribute')elements.remove_element(xml_element,'parent/uglyChild')elements.element_to_string(xml_element)# Conversion from string to Element, to dict, and then back to stringconverted=elements.element_to_dict(xml_string,recurse=True)reverted=elements.dict_to_element(converted)reverted=elements.get_element(converted)xml_string==elements.element_to_string(converted)# Conversion to flattened dict objectroot,obj=elements.element_to_object(converted)obj=={'root':{'parent':{'child':['one','two'],'uglyChild':'yuck'}}}# Read in an XML file and write it elsewherewithopen('/path/to/file.xml','wb')asxml:xml_from_file=elements.get_element(xml)elements.write_element(xml_from_file,'/path/to/updated/file.xml')# Write a local file from a remote location (via URL)xml_from_web=elements.get_remote_element('http://en.wikipedia.org/wiki/XML')elements.write_element(xml_from_web,'/path/to/new/file.xml')# Read content at a local file path to a stringxml_from_path=elements.get_remote_element('/path/to/file.xml')elements.element_to_string(xml_from_path)

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

推荐PyPI第三方库


热门话题
Java Android Studio从XML中洗牌2D数组列   安卓 rx java obsever pojo模型的变化   java如何在安卓应用程序中以编程方式打开google教室   java如何将充满令牌的对象[]转换为整数数组?   java Minify Maven插件抛出“不支持JavaScript引擎”错误   java如何检查调用应用程序的用户是否具有绑定特权端口的权限?   java无法向firestore发送数据   jpanel Java向ScrollPane中嵌入的面板添加JLabel   将Bash脚本移植到Java   JavaSpring显示错误消息   java如何为这些路径表达式编写正则表达式   java如何通过编程在Android手机上获得时间?   Java:捕获未检查的异常与已检查的异常