def format(obj,width=79,max_seq_length=py.No("auto get-set 'ipy.pformat.max_seq_length' ",no_raise=1)): ''' IPython.lib.pretty.pretty( obj, verbose=False, max_width=79, newline='\n', max_seq_length=1000, ) /IPython/core/formatters.py: format(obj, include=None, exclude=None) Docstring: Return a format data dict for an object. By default all format types will be computed. The following MIME types are usually implemented: * text/plain * text/html * text/markdown * text/latex * application/json * application/javascript * application/pdf * image/png * image/jpeg * image/svg+xml Parameters ---------- obj : object The Python object whose format data will be computed. include : list, tuple or set; optional A list of format type strings (MIME types) to include in the format data dict. If this is set *only* the format types included in this list will be computed. exclude : list, tuple or set; optional A list of format type string (MIME types) to exclude in the format C:\QGB\Anaconda3\lib\site-packages\IPython\core\formatters.py :89 ''' import IPython.lib.pretty if not max_seq_length: max_seq_length=U.get_or_set('ipy.pformat.max_seq_length',2000) U.set('ipy.pformat.max_seq_length',max_seq_length) return IPython.lib.pretty.pretty(obj,max_width=width,max_seq_length=max_seq_length) from IPython.core.interactiveshell import InteractiveShell r= InteractiveShell.instance().display_formatter.format(obj) if py.len(r)!=2 or py.len(r[1])!=0 or not py.isdict(r[0]):raise EnvironmentError() return U.get_dict_value(r[0])
def test_dir_recursively(obj,*a): U=py.importU() if py.len(a)>TRY_MAX_LAYER:return r=U.dir(obj) for n,k,v in r: r[n][2]=test_dir_recursively(v,*a,k) or v return r
def dill_dump_bytes(obj,file=None,protocol=None,dill_ext='.dill'): ''' #TODO file=0 Not write '../0.dill' dill.dump(obj, file, protocol=None, byref=None, fmode=None, recurse=None) dill.dumps(obj, protocol=None, byref=None, fmode=None, recurse=None) ValueError: pickle protocol must be <= 4 r=request.get ... F.readableSize(len(F.dill_dump(protocol=None,obj=r) ) )#'14.192 KiB' F.readableSize(len(F.dill_dump(protocol=0,obj=r) ) ) #'15.773 KiB' F.readableSize(len(F.dill_dump(protocol=1,obj=r) ) ) #'19.177 KiB' F.readableSize(len(F.dill_dump(protocol=2,obj=r) ) ) #'18.972 KiB' F.readableSize(len(F.dill_dump(protocol=3,obj=r) ) ) #'14.192 KiB' F.readableSize(len(F.dill_dump(protocol=4,obj=r) ) ) #'13.694 KiB' ''' import dill if file: if py.istr(obj) and py.len(obj)<333 and '.dill' in obj: if not py.istr(file) or '.dill' not in file: file,obj=obj,file file=auto_path(file,ext=dill_ext) with py.open(file,'wb') as f: dill.dump(obj=obj,file=f,protocol=protocol) return file else: return dill.dumps(obj=obj,protocol=protocol)
def dill_load(filename,return_value=False,set_user_ns=True): # F=py.importF() if filename.lower()[-5:] not in ['.dill','ickle']: for f in F.ls(U.gst,f=1,d=0): if filename in f: py.input('continue or ctrl+c using: '+f) filename=f break vars=[] dnv={} if U.all_in([',','='],filename): vars=T.regex_match_all(F.getNameWithoutExt(filename),T.RE_vars_separated_by_commas)[0].split(',') if vars: for n,v in py.enumerate(F.dill_load(filename,dill_ext='')): dnv[vars[n]]=v else: varname='' for c in F.getNameWithoutExt(filename): if c not in T.alphanumeric_:break varname+=c dnv[varname]=F.dill_load(filename,dill_ext='') if py.len(dnv)==1: r=[py.list(dnv)[0],filename] else: r=[py.list(dnv),filename] U.pln(r) if set_user_ns: for name,v in dnv.items(): gipy.user_ns[name]=v if return_value: return r
def get(p=False, edit=False, edit_prompt='', only_edit_firstline=True, **ka): '''win32con. CF_DSPTEXT ', 129], CF_OEMTEXT ', 7], CF_RETEXTOBJ ', 'RichEdit Text and Objects'], CF_TEXT ', 1], CF_UNICODETEXT ', 13], in py3 win32con.CF_TEXT return b' ' prompt [präm(p)t 不发音p] 提示 ; promte 提升 ''' U = py.importU() if U.istermux(): return U.cmd('termux-clipboard-get') p = U.get_duplicated_kargs(ka, '_print', 'print_', 'show', 'PRINT', default=p) edit = U.get_duplicated_kargs(ka, 'e', 'E', 'input_edit', 'input', 'edit_clipboard', default=edit) w.OpenClipboard() d = w.GetClipboardData(win32con.CF_UNICODETEXT) w.CloseClipboard() if p: U.pln(d) if edit: type = U.get_duplicated_kargs(ka, 'type', 'edit_type', 't') edit_prompt = U.get_duplicated_kargs(ka, 'title', 'msg', 'edit_msg', 'edit_prompt', 'prompt', 'promte', 'promot', 'promote', default=edit_prompt) only_edit_firstline = U.get_duplicated_kargs( ka, 'edit_firstline', 'firstline', 'fl', 'line0', 'l0', default=only_edit_firstline) if not edit_prompt: if py.istr(edit): edit_prompt = edit else: edit_prompt = '' if only_edit_firstline and py.len(d.splitlines()) > 0: d = d.splitlines()[0] d = U.input(edit_prompt, default=d, type=type) return d
def write(file,data,mod='w',encoding='utf-8',mkdir=False,autoArgs=True,pretty=True,seek=None): '''py3 open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None) py2 open(name[, mode[, buffering]]) pretty=True Format a Python object into a pretty-printed representation. ''' U=py.importU() try: if autoArgs: if py.istr(data) and py.len(file)>py.len(data)>0: if '.' in data and '.' not in file and isFileName(data): file,data=data,file U.warring('F.write fn,data but seems data,fn auto corrected(v 纠正') except:pass # try: file=autoPath(file) if not encoding:encoding=U.encoding if mkdir:makeDirs(file,isFile=True) # if 'b' not in mod and py.isbytes(data):mod+='b'# 自动检测 data与 mod 是否匹配 if 'b' not in mod: #强制以 byte 写入 mod+='b' f=py.open(file,mod) #f.write(强制unicode) 本来只适用 py.is3() ,但 py2 中 有 from io import open if py.isint(seek): f.seek(seek) # with open(file,mod) as f: if py.isbyte(data):#istr(data) or (py.is3() and py.isinstance(data,py.bytes) ) : f.write(data) elif (py.is2() and py.isinstance(data,py.unicode)) : f.write(data.encode(encoding)) elif (py.is3() and py.istr(data)): # if 'b' in mod.lower(): f.write(data.encode(encoding)) # else:f.write(data)#*** UnicodeEncodeError: 'gbk' codec can't encode character '\xa9' in else: # if py.is2():print >>f,data # else: if pretty: data=U.pformat(data) U.pln(data,file=f) f.close() return f.name
def copy(src,dst,src_base='',skip=''): r''' src : sFilePath , list ,or \n strs dst:sPath ''' from shutil import copy as _copy if not py.istr(dst):raise py.ArgumentError('dst must be str') if py.istr(src): if '\n' in src: src=src.splitlines() return copy(src,dst) if src[-1] in ['/','\\']: src=F.ls(src,r=1) else: return _copy(src,dst) if not src_base: U,T,N,F=py.importUTNF() dl=U.unique(U.len(*src),ct=1) min=py.min(*dl) f=[i for i in src if py.len(i)==min][0] if f[-1] in ['/','\\']:f=f[:-1] # Path(f).absolute().parent.absolute().__str__() src_base=f[:py.len(T.sub_last(f.replace('\\','/'),'','/') )+1] src_base_len=py.len(src_base) print('src_base: %r'%src_base,'len(src)==%s'%py.len(src)) while dst[-1] not in ['/','\\']: dst=U.input('not dir! rewrite dst:',default=dst) if py.iterable(src): fns=[] skips=[] for i in src: if skip and skip in i: skips.append(i) continue # fn=getName(i) # if fn in fns: # fn=T.fileName(i) fn=i[src_base_len:] if fn[-1] in ['/','\\']: mkdir(dst+fn) else: _copy(i,dst+fn) fns.append(fn) if skips:return skips,fns return fns raise py.ArgumentUnsupported(src)
def dill_dump(*vars,len=True): ''' pip install astor ''' T=py.importT() import ast co=sys._getframe().f_back.f_code dvars={} for index_a,e in enumerate(ast.walk(U.getAST(co) ) ): da={i:getattr(e,i) for i in dir(e) if not i.startswith('_') } # if index==1:break if ('value' in da) and py.isinstance( e.value,ast.Call): #ipy.*dump*() if U.getattr(e.value,'func','value','id')=='ipy' and \ ('dump' in U.getattr(e.value,'func','attr').lower() ): # print(index_a, da,'\n================\n') # U.set(e.value.args) for i,var in py.enumerate(e.value.args): # if py.isinstance( var,(ast.Name,ast.Subscript) ):# var : Name(lineno=1, col_offset=12, id='In', ctx=Load()), dvars[U.ast_to_code(var,EOL=False) ]=vars[i] if py.len(vars)==1 and not len: return F.dill_dump(obj=vars[0],file=py.list(dvars)[0]) r=[] for n,i in py.enumerate(dvars): il=U.len(dvars[i]) if il: if py.len(dvars)> 1:il='%-2s:%s'%(n,il) if py.len(dvars)==1:il='%-4s'%(il) else: il='%-2s'%n r.append( [ i, il ] ) if py.len(r)> 1:f='(%s) = {%s}' if py.len(r)==1: f='%s-%s' vars=vars[0] f=f % ( ','.join([i[0] for i in r]) ,','.join([i[1] for i in r])) f=f.replace('"""',"'") f=T.fileName(f) return F.dill_dump(obj=vars,file=f)
def size_single_file(f): f=nt_path(f) size =0 #0L SyntaxError in 3 if not _p.exists(f): return py.No('{} NOT EXISTS!'.format(f)) if _p.isdir(f): # if f=='' , _p use pwd return py.No('{} is dir !'.format(f)) try: size= _p.getsize(f) if size<=0: size=py.len(read_bytes(f)) except Exception as e: return py.No('unexcept err',e,f)
def hexToBytes(a,split='',ignoreNonHex=True): a=a.upper();r=b'' if ignoreNonHex:a=''.join([i for i in a if i in '0123456789ABCDEF']) it=2 if len(split)>0:it+=len(split) if it==2 and len(a) % it!=0:return () if it>2: a=a.split(split) if len(a[-1]) == 0:a=a[:-1] for i in a: r+=py.byte(DHI[ i ]) return r for i in range(py.int(py.len(a)/2 )): r+=py.byte(DHI[a[i*2:i*2+2]]) return r
def move(source,target,edit_target=False,mkdir=True,remove_invalid_char=True,**ka): ''' * in target == source ? in target == source_fn // in target == path(exclude source_fn) # os.rename(source, target) #OSError: [WinError 17] 系统无法将文件移到不同的磁盘驱动器。: 'C: os.rename target文件名(包不包括路径都没关系)包括扩展名,最大不能超过241 ''' U,T,N,F=py.importUTNF() edit_target=U.get_duplicated_kargs(ka,'e','edit','editarget',default=edit_target) remove_invalid_char=U.get_duplicated_kargs(ka,'remove_char','delChar', 'del_char','remove_illegal','del__illegal','del_invalid','remove_invalid', 'del_invalid_char','del__illegal_char','remove_illegal_char',default=remove_invalid_char) source=F.autoPath(source) source_fn=F.get_filename_from_full_path(source) source_path=source[:-py.len(source_fn)] if target.endswith('/'): target+=source_fn # if '*' in target: target=target.replace('*',source) target=target.replace('?',source_fn) target=target.replace('//',source_path) target=F.autoPath(target) if edit_target:target=U.input(default=target) if remove_invalid_char: if U.is_windows(): target=T.replacey(target,T.NOT_PATH_NAME_WINDOWS,'') if not target.endswith('/') and F.isDir(target): target=target+'/' if mkdir:F.mkdir(F.get_dir(target)) # 要先有文件夹,不然shutil.move有可能找不到 import shutil try: return shutil.move(source, target) # return target except (FileNotFoundError,FileExistsError) as e: # parentheses required, otherwise invalid syntax return py.No(e,source,target) if F.isDir(target): r=target+source_fn else: r=target return F.exist(r)
def read_csv(file,encoding=None,delimiter=',',keep_default_na=False,): ''' keep_default_na : use float nan ,not empty string '' the na_filter=False can change your columns type to object ''' file=autoPath(file) if not encoding:encoding=detectEncoding(file) import pandas as pd df = pd.read_csv(file, delimiter=delimiter,encoding=encoding,keep_default_na=keep_default_na) r=[] is1=False if py.len(df.columns)==1:is1=True for i in df.values: if is1: r.append(i[0]) else: r.append(tuple(i)) return r
def mouse_click(x=None, y=None, *a, _1=False): ''' click(*xy+(2,3)) == click(x,y,2,3) == click(x+2,y+3) ''' import win32api, win32con if not x and not py.isint(x): x = get_cursor_pos()[0] if not y and not py.isint(y): y = get_cursor_pos()[1] x, y = py.int(x), py.int(y) if not _1: if x == -1 or y == -1: U = py.importU() return x, U.IntRepr(y, repr='%s # -1 not click! #' % y) if a: if py.len(a) != 2: raise py.ArgumentError('a must be 2 int tuple') x = x + a[0] y = y + a[1] win32api.SetCursorPos((x, y)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0) return py.importU().object_custom_repr( (x, y), repr='Win.click({0},{1})'.format(x, y))
def try_dill_dump_recursively(obj,*a,): global U if not U:U=py.importU() if py.len(a)>TRY_MAX_LAYER:return try: b=dill_dump_bytes(obj) return (*a,U.size(b)) except Exception as e: if py.islist(obj) or py.istuple(obj) or py.isset(obj): r=[] for n,v in py.enumerate(obj): r.append([n,try_dill_dump_recursively(v,*a,n)]) return r elif py.isdict(obj): d={} for n,(k,v) in py.enumerate( obj.items()): d[n]=try_dill_dump_recursively(kv,*a,n) return d r=[] for n,k,v in U.dir(obj): r.append([k,try_dill_dump_recursively(v,*a,k)])
def auto_path(fn,ext='',default='',is_dir=False,p=False): ''' default is U.gst if fn.startswith("."): 如果路径中所有文件夹存在,则可以写入读取。否则无法写入读取。file io 都是这个规则吧 FileNotFoundError: [Errno 2] No such file or directory: '. #TODO #BUG# F.auto_path('\\\\\\C:\\test\\clipboard',default='C:/test/',)== '///C:/test/clipboard' ''' U=py.importU() if not gbAutoPath: pass else: if default: default=default.replace('\\','/') if not default.endswith('/'):default+='/' else: default=U.set_test_path(U.gst) # 防止 U.gst 被改变没被保存 fn=str(fn) fn=fn.replace('\\','/') if ext and not ext.startswith('.'):ext='.'+ext if not fn.lower().endswith(ext.lower()):fn+=ext if fn.startswith("~/"): import os if U.isnix(): home=os.getenv('HOME') else: home=os.getenv('USERPROFILE')# 'C:/Users/Administrator' not cyg home os.getenv('HOME') # else: home=os.getenv('HOMEPATH')# HOMEPATH=\Users\Administrator fn= home+fn[1:]; #TODO to avoid dos max_path , must \ full path 2019年12月19日 不记得什么意思? if (not fn.startswith(".")) and (not isAbs(fn)) : while fn.startswith('/'):fn=fn[1:] fn= default + fn if py.len(fn)>=260 and U.iswin(): fn=nt_path(fn) if(is_dir and not fn.endswith('/')):fn+='/' if p:print(fn) return fn
def save(file=None,lines=-1,tryExcept=False,out=False,columns=70,overide=True,del_other=False,out_max=9999,**ka): '''file is str or (mod a) 在没有ipython实例时,不能get_ipython() 当file被指定时,overide 参数无效 #BUG #TODO 出现输入记录不同步的现象,应该添加一个报警 In [302]: _i265 Out[302]: '[i for i in sku if i[0] in [0.03, 0.04, 0.05, 0.06, 0.07, 0.18] ]' In [303]: In[265] Out[303]: 'page=pa=await tb.get_or_new_page(U.cbg(e=1))' ''' del_other=U.get_duplicated_kargs(ka,'delOther','delete','delattr',default=del_other) if ka:raise py.ArgumentError('没有处理这个ka,是不是多传了参数',ka) try: if py.isint(lines) and lines>0: # lsta,lend=0,lines lsta,lend=lines,-1 elif len(lines)==2: lsta,lend=lines lsta=lsta if lsta>0 else 0 else:raise Exception except: lsta,lend=0,gIn.__len__() if file:#当指定file 名时,总是 overide if T.istr(file): file=T.filename(file)[:255-3]# 如果用 pathname 不好处理 含有 /斜杠的问题 if U.is_linux(): while py.len(file.encode('utf-8'))> 255-3: # 256-3 too long ! file=file[:-1] file=F.autoPath(file,ext='.py',default=gsavePath) #255-3(.py) 防止文件名过长 OSError: [Errno 22] Invalid argument: "\\\\?\\C:\\test F.new(file) if py.is2():file=open(file,'a') else:file=open(file,'a',encoding="utf8") elif py.isfile(file): if file.mode!='a':raise Exception('file mode should be "a" ') else: raise Exception('invalid argument file') else: if gdTimeName and overide: file=gdTimeName[py.list(gdTimeName.keys() )[-1]]#is3:TypeError: 'dict_keys' object does not support indexing file=F.autoPath(file,ext='.py',default=gsavePath) if py.is2():file=open(file,'w') else:file=open(file,'w',encoding="utf8") # last=-1 # for t in gdTimeName: # if t>last:file,last=name,d # last=gdTimeName.values() # last.sort()#从小到大排序,ACS正序, DESC倒序 Ascending and Descending # file=[n for n,d in gdTimeName.items() if d==last[-1]][0] else: file='{0}{1}.py'.format(gsavePath,U.stime()) if py.is2():file=open(file,'a') else:file=open(file,'a',encoding="utf8") U.pln(gshead,file=file) ####### get exec_lines to gsqgb gsqgb=U.main(display=False) #join( <traitlets.config.loader.LazyConfigValue at 0x20066295400> ) ### TypeError: can only join an iterable gsexec_lines=U.getNestedValue(gipy.config,'InteractiveShellApp','exec_lines') #_class%20'traitlets.config.loader.LazyConfigValue'_.html if not py.iterable(gsexec_lines): gsexec_lines=[] gsexec_lines=' ;'.join(gsexec_lines) if gsexec_lines: if not 'from qgb import' in gsexec_lines: gsqgb='{0}\n{1}'.format(gsqgb,gsexec_lines) else:gsqgb=gsexec_lines U.pln(gsqgb+'\n',file=file ) # print >>file,'import sys;sys.path.append('{0}');from qgb import *'.format(gspath) #using single quote for cmd line #-4 为了去除 /qgb #ipython --InteractiveShellApp.exec_lines=['%qp%'] 不会改变In[0],始终为'' for i,v in enumerate(gIn[lsta:lend]): skip=False if i==0 and lsta==0:continue i=lsta+i v=v.strip() # U.isSyntaxError(u'_{0}={1}'.format(i,v) ) : # pass if i in gOut: if i==1 and py.istr(gOut[1]) and gOut[1].endswith(':/QGB/babun/cygwin/lib/python2.7/'): pass else: v=u'_{0}={1}'.format(i,v) if U.isSyntaxError(v) or U.multin(gIgnoreIn,v): # or u'from qgb import *' in v or sum(map(lambda a:v.startswith(a),gIgnoreStart) ): v=u'#'+v skip=True if tryExcept and (not skip): v='#########################\n\t'+v if py.is2():v=gsTryExcept.format(i,v).encode('utf-8') else:v=gsTryExcept.format(i,v) U.p(v,file=file ) else: if py.is2():v=v.encode('utf-8') else:pass U.p(v,' '*(columns-len(v.splitlines()[-1])),file=file ) if out and (i in gOut) and (not skip): pout=pformat(gOut[i]) if py.len(pout) > out_max: pout='#Warning# len(pout)={} > out_max'.format(py.len(pout)) print( '#ipy.save In[{}]{}# len(pout)={} > out_max'.format(i,gIn[i],py.len(pout)) ) U.pln(';"""#{0}'.format(i),file=file ) # U.pln('"""',file=file ) U.pln(pout,file=file ) U.pln('"""',file=file ) else: U.pln('#',i,file=file ) # if i in [14]:import pdb;pdb.set_trace()#U.repl() # gipy.magic(u'save save.py 0-115') # U.pln(gIn # U.pln(gOut.keys() file.close() gdTimeName[U.time()]=file.name if del_other: for t,f in gdTimeName.items(): if f == file.name:continue print('del:',U.stime(t),F.delete(f) or [f,False]) return '{0} {1} success!'.format(save.name,file.name)
def get_nt_short_path_name(long_name,max=250): import win32api if py.len(long_name)<max:return long_name if not long_name.startswith( u"\\\\?\\"): long_name=u"\\\\?\\"+long_name return win32api.GetShortPathName(long_name)
def list(ap='.',type='',t='',r=False,d=False,dir=False,f=False, file=False,include='',exclude='',timeout=None,print_result=False,**ka): '''Parms:bool r recursion str (type,t) '(d,f,a,r)' default return all''' U=py.importU() print_result=U.get_duplicated_kargs(ka,'print_r','print','p',default=print_result) if dir:d=True if file:f=True if t and not type:type=t if 'd' in type:d=True if 'f' in type:f=True if 'a' in type:d=True;f=True if 'r' in type:r=True if d or dir or f or file:pass else:d=f=True #default return all if not py.istr(ap) or py.len(ap)<1: setErr('F.list arguments ap error') ap='.' # if len(ap)==2 and ap.endswith(':'):ap+='/' ap=ap.replace('\\','/') if not ap.endswith('/'):#U.inMuti(ap,'/','\\',f=str.endswith): if isDir(ap): ap+='/' else: return [exists(ap)] # if not ap.endswith('/'):ap+='/' # else:ap+='\\' # U.repl() ########## below r is result rls=[] try:r3=py.list(walk(ap)) except Exception as ew: # pln ap;raise ew return py.No(ew) if ap=='./':ap='' # U.repl() r3[1]=[ap+i+'/' for i in r3[1]] #dirs r3[2]=[ap+i for i in r3[2]] #files if d:rls.extend(r3[1]) # if r: for i in r3[1]:rls.extend(list(i,r=r,d=d,f=f)) if f:rls.extend(r3[2]) if include:rls=[i for i in rls if include in i] if exclude:rls=[i for i in rls if exclude not in i] if print_result: U.pprint(rls) return rls