蟒蛇基本用途

eic-utils的Python项目详细描述


EIC实用程序

pythonx 3的一些基本实用程序

要求

  1. python3.x环境
  2. python3.x包:
    操作系统、pickle、json、sqlite3

安装和升级

来自Pypi

安装

$ pip install eic_utils

升级

$ pip install eic_utils --upgrade

git子模块

安装

$ # git init 
$ git submodule add https://github.com/indestinee/utils eic_utils
$ git submodule update --init --recursive

部件

类缓存

轻微存储

示例

fromeic_utils.cacheimportCachecache=Cache('./data.pkl')'''class Cache(object):    def __init__(self, path, default='pkl'):	@params:		path (str): path to store cache		default (str): default value 'pkl'			choices: ['bin', 'pkl', 'json', 'str'] 	@returns:		instance of class'''cache.dump(data={'hi','hello'},name='greeting.pkl',force=True)cache.dump(data={'hi','hello'},name='greeting.bin',force=True,file_type='bin')greeting=cache_pkl.load(name='greeting.pkl')print(greeting)# {'hello', 'hi'}''' file tree	./greeting.pkl	# pickle file	./greeting.bin	# bin file'''cache.remove('greeting.pkl')''' file tree	./greeting.bin	# bin file'''

类数据库

基于sqlite3的存储

函数

def__init__(self,path,name,tables)'''     @params:        path(str): path to store        name(str): database name        tables(list/dict): consists of one or several dicts        table(dict): keys: 'name', 'attr', 'extra'            name(str): name of table            attr(list): each item is a dict                item(dict): describe a column in table, keys: 'key'(column name), 'db_type'(type)            extra(list): extra informs    '''defdump(self)# @save to diskdefexecute(self,cmd,*args,dump)'''    @params:        cmd(str): command        args(list): params for command        dump(bool): dump to disk if true else memory only            *we have two database, one in memory, the other in disk*            *if you only read from the database, use dump=False, otherwise, dump=True*    '''deflist_table(self)# return a list, table namesdefdestroy(self)# destroy databasedefdrop_table(self,name)'''    @params:        name(str): table name    '''defadd_tabls(self,tables):'''    @params:        tables(list/dict): same as tables@init    '''defadd_row(self,table_name,data)'''    @params:        table_name(str): table name        data(dict): keys, values for the row    '''defdel_row(self,table_name,limitation)'''    @params:        table_name(str): table name        limitation(dict, None, str): WHERE key_1=value_1, key_2=value_2, ...    '''defupd_row(self,table_name,data,limitation)'''    @params:        table_name(str): table name        data(dict): keys, values for the row, SET key_1=value_1, key_2=value_2, ...        limitation(dict, None, str): WHERE key_1=value_1, key_2=value_2, ...    '''defselect(self,table_name,data,limitation,keys)'''    @params:        table_name(str): table name        data(dict): keys, values for the row        limitation(dict, None, str): WHERE key_1=value_1, key_2=value_2, ...        keys(str, list): interest of columns    '''defcount(self,table_name,limitation=None)'''    SELECT COUNT(*) WHERE ...    '''

示例

# init tables=[{'name':'user','attr':[{'key':'id','db_type':'INTEGER PRIMARY KEY AUTOINCREMENT',},{'key':'username','db_type':'TEXT UNIQUE NOT NULL',},{'key':'interest','db_type':'INTEGER',}],'extra':['FOREIGN KEY (interest) REFERENCES interest(id)',],},{'name':'interest','attr':[{'key':'id','db_type':'INTEGER PRIMARY KEY AUTOINCREMENT',},{'key':'description','db_type':'TEXT UNIQUE NOT NULL'}]},]db=DataBase(path='data',name='mydata.db',tables=tables)# store in ./data/mydata.dbdb.add_row('interest',data={'description':'play soccer'})db.add_row('interest',data={'description':'play baseball'})db.add_row('interest',data={'description':'swimming'})data=db.execute('SELECT * FROM interest').fetchall()print('raw use:',data)data=db.select('interest',return_dict=True)print('dict:',data)data=db.select('interest',return_dict=True,limitation='id<3')print('id<3, dict:',data)data=db.select('interest',return_dict=True,limitation={'id':'3'},keys={'description'})print('id=3, return name only, dict:',data)

image

类彩色打印

颜色列表

黑色、红色(R)、绿色(G)、黄色(Y)、蓝色(B)、洋红、青色、白色(W)

示例

fromeic_utils.colorful_printimportcpcp('(#[COLOR in Color List])[Your Sentence](#)')cp('hi')# same as print('hi')cp('(#g)hi(#)')# print a green 'hi'cp('(#g)hi, ops, forgot to reset color to default')# (#) means reset color to defautprint('-'*32)s=cp.trans('(#y)hi(#) ')# cp.trans return strprint(s,end='')print('is equals to ',end='')cp('(#y)hi(#)')print('-'*32)cp.wrn('this is a warning msg')# print warning with yellowcp.suc('this is a successful msg')# print warning with greencp.log('this is a log msg')# print warning with bluecp.err('this is an (#r)error(#) msg')# print warning with redprint('-'*32)hint=cp.trans('(#b)input: (#g)',auto_end=False)x=input(hint)cp('',end='')

image

课堂程序

示例

withprocedure('waiting for 5 sec and overwrite the line',same_line=True)asp:time.sleep(5)withprocedure('waiting for 5 sec',same_line=False)asp:time.sleep(5)p.msg+=' (#g):)(#)'

imageimage

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

推荐PyPI第三方库


热门话题
Java如何添加排除某个数字的随机生成器?   在Eclipse中将所有包集中在一个包中(对于Java项目)   java在部署我的应用程序后无法连接到MySQL   Ntier应用程序中的java NoClassDefFoundException   java泛型类型转换?   使用buildr编译期间的java jar插件   java查找排序数组中重复值的计数   运行Spring Boot cmdline的java示例给出了“找不到run()方法”`   java为什么不调用windowClosed?   java jsch身份验证在没有私钥的情况下失败   java如何在我的jtable中获得100多行?   java在遗留Android Studio项目中创建测试文件夹   java 安卓。所容纳之物res.Resources$NotFoundException:资源ID#0x7f080087   java Eclipse自动格式化变量初始化