专否 写文章

jerkzhang,stay hungry, stay foolish

Oct 17, 2018
Follow

分享一个只有4个函数的python库

这个python库的名字叫jerk。顾名思义,这种奇怪的库只能是我写的。很多年前用python做opencv视觉识别与numpy科学运算时,经常遇到一些比较麻烦的需求,所以就写了这个库,其实很简单,内部就是递归,然后把其封装成四个函数,基本上可以应对大部分情况了。jerk库开源在github上了,其实已经荒废挺久了,中间我还更新了一次,不过我也记不得有没有更新成功。其实确实是一个没啥大用的库,不过偶尔某些场景还是挺方便的。自己有时还在用。



jerk

A simple python library to make python more python. Jerkpy is a simple and jerkful library to do some easy function more easily and more jerkfully.

Now he has only four APIs, welcome to develop him together.

His mind is just to pay attention to summing up some tips from our daily coding life and make it true to be a esier using funcitonal APIs, when we are in troubles, they maybe come in handy.

Installation

It is recommended to use pip to install jerkpy.

$ pip install jerk

Another way is to download it from this git or pypi, and then to install it manually via $ python setup.py install



Get started

how to import it, like this below:

from jerk import *




References

xlen

xlen( lst, start=0, step=1 )

In python, we can use "enumerate" to get both index and element of a list,

but for an obsessive-compulsive programmer he need an easy function to get only the index in an easy ways, which is just what "xlen" is doing.

Most of this api is the function combination of "xrange()" and "len()". (The result of "xrange()" is a generator which can supply the same need in function and so that is better than "range()" of which result is a list.)

[ lst ] is an argument at class of <"list"> to mean which index generator will be output;
[ start ] can set the start number of the indexs. The default value of this optional argument is 0.
[ step ] is an optional argument with the default value 1, which can set the step of the indexs.

Example:

>>> from jerk import *
>>> 
>>> lst_test = ['k','u','f','c']
>>> 
>>> for i in xlen( lst_test ):
...     print i
...
>>> 0
>>> 1
>>> 2
>>> 3
>>> 
>>> for i in xlen( lst_test,1 ):
...     print i
...
>>> 1
>>> 2
>>> 3
>>> 4
>>> 
>>> for i in xlen( lst_test,1,2 ):
...     print i
...
>>> 1
>>> 3


xint

xint( a )

In python, if we want to get a nearest int number from a float number, we must use int( round( object<"float"> ) ). For coding less letters and brackets, xint(a) has given into birth.

In addtion, if we want to make all the float numbers in a set or a dict or a list or a tuple to be transformed as int numbers, int( round( object<"float"> ) ) can't do the job because his object input must be only one float numbers. But xlen(a) can do with all the things except a cyclic tree dict of course.

Examples:

from jerk import *

a = 0.93
print "a = {},\nxint(a)={}\n".format( a, xint (a) )

a = [ 0.93, (291.1232332, 23.2323311) ]
print "a = {},\nxint(a)={}\n".format( a, xint (a) )

a = ( 0.93, 23.23, 54, [23.2, 3], '23' )
print "a = {},\nxint(a)={}\n".format( a, xint (a) )

a = {      0.93:239, \
      'jerk.py':(0.93, 23.2234333, 54, [23.2, 3.23323, {2.2:-0.1}]), \
           '23':-0.999 }
print "a = {},\nxint(a)={}\n".format( a, xint (a) )

>>> a = 0.93,
>>> xint(a)=1
>>> 
>>> a = [0.93, (291.1232332, 23.2323311)]
>>> xint(a)=[1, (291, 23)]
>>> 
>>> a = (0.93, 23.23, 54, [23.2, 3], '23')
>>> xint(a)=(1, 23, 54, [23, 3], '23')
>>> 
>>> a = {0.93: 239, 'jerk.py': (0.93, 23.2234333, 54, [23.2, 3.23323, {2.2: -0.1}]), '23': -0.999}
>>> xint(a)={1: 239, 'jerk.py': (1, 23, 54, [23, 3, {2: 0}]), '23': -1}


xlist

xlist( a )

In python, if we want to get a list from a tuple, we can use list(). But it can not do with the elements in the tuple. So xlist() is recommended to use.

Examples:

>>> from jerk import *
>>> 
>>> a = (0.93, (291.1232332, 23.2323311), (3,33), (3, (3, 3)), {3: 'tutorial'}, set([1, 2, 3]))
>>> print xlist(a)
>>> [0.93, [291.1232332, 23.2323311], [3, 33], [3, [3, 3]], {3: 'tutorial'}, set([1, 2, 3])]


xtuple

xtuple( a )

In python, if we want to get a tuple from a list, we can use tuple(). But it can not do with the elements in the list. So xtuple() is recommended to use.

Examples:

>>> from jerk import *
>>>
>>> a = [0.93, [291.1232332, 23.2323311], [3, 33], [3, [3, 3]], {3: 'tutorial'}, set([1, 2, 3])]
>>> print xtuple(a)
>>>(0.93, (291.1232332, 23.2323311), (3, 33), (3, (3, 3)), {3: 'tutorial'}, set([1, 2, 3]))



It only has four APIs now. Welcome to discuss which function def can make python esier and faster coding. Python is a very human language, which will be learned by every children in future.

喜欢这个文章 | 分享 | 新建跟帖