蟒蛇基本用途
eic-utils的Python项目详细描述
EIC实用程序
pythonx 3的一些基本实用程序
轻微存储 基于sqlite3的存储
黑色、红色(R)、绿色(G)、黄色(Y)、蓝色(B)、洋红、青色、白色(W)要求
操作系统、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'''
类数据库
函数
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)
类彩色打印
颜色列表
示例
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='')
课堂程序
示例
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):)(#)'
推荐PyPI第三方库