def detect_file_encoding(file,confidence=0.7,default=py.No('not have default encoding'),buffer_size=9999,p=True,**ka): U,T,N,F=py.importUTNF() p=U.get_duplicated_kargs(ka,'print_file_encoding','print_detect_encoding','print',default=p,no_pop=True) if py.istr(file): with py.open(file,'rb') as f: b=f.read(buffer_size) elif py.isfile(file): if 'b' not in file.mode:raise py.ArgumentError("'b' not in file.mode",file) i=file.tell() b=file.read(buffer_size) file.seek(i) else:raise py.ArgumentError('need str or file') c= T.detect(b,confidence=confidence,default=default) if p:print(file,c) #TODO U.get_or_set('detect_file_encoding.p',True) return c
def mouse_drag(x, y, x2=0, y2=0, time=1.1): import pyautogui U = py.importU() if U.len(x) == 2: if U.len(y) == 2: if x2 or y2: raise py.ArgumentError('duplicated x2,y2', x2, y2) x2, y2 = y x, y = x pyautogui.moveTo(x, y) pyautogui.mouseDown(button='left') pyautogui.dragTo(x2, y2, 1, button='left') U.sleep(time) pyautogui.mouseUp(button='left')
def jupyter_password(passphrase='',salt='0'*12,algorithm='sha1'): import hashlib from ipython_genutils.py3compat import cast_bytes,str_to_bytes if py.isbytes(salt): bsalt=salt salt=bsalt.decode('ascii') elif py.istr(salt): bsalt=str_to_bytes(salt, 'ascii') else: raise py.ArgumentError(salt) h=hashlib.new(algorithm) h.update(cast_bytes(passphrase, 'utf-8') + bsalt) return ':'.join((algorithm, salt, h.hexdigest()))
def deSerialize(obj=None,file=None): '''The protocol version of the pickle is detected automatically, so no protocol argument is needed. Bytes past the pickled object's representation are ignored. ''' if not py.isbyte(obj) and not file:raise py.ArgumentError('need bytes or file=str ') import pickle if py.istr(obj): file=obj obj=None U.log('autoArgs file=%s'%file) if py.isbyte(obj): return pickle.loads(obj) else: file=autoPath(file) with py.open(file,'rb') as f: return pickle.load(f)
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 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 read_xlsx(file,sheet=0): ''' 加载一个8M多xlsx文件,很慢,''' import openpyxl if py.isinstance(file,openpyxl.workbook.workbook.Workbook): wb=file elif py.istr(file): wb = openpyxl.load_workbook(file)# 这句很耗时 else: raise py.ArgumentUnsupported(file) if py.isint(sheet): ws=wb[wb.sheetnames[sheet]] elif py.istr(sheet): ws=wb[sheet] else:raise py.ArgumentError('sheet is index or sheet_names') r=[] for row in ws.values:#<Worksheet "1 lemmas"> ri=[] for value in row: ri.append(value) r.append(ri) return r
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 SetForegroundWindow(title=None, handle=None, pid=None, process_name='', raise_error=0, retry=99, **ka): U, T, N, F = py.importUTNF() if py.isint(title) and not handle: handle, title = title, '' if not title: title = U.get_duplicated_kargs( ka, 't', ) if not handle: handle = U.get_duplicated_kargs(ka, 'hwnd', 'h') if not process_name: process_name = U.get_duplicated_kargs(ka, 'name', 'pn', 'process') no_raise = U.get_duplicated_kargs(ka, 'no_raise', 'noRaise', 'no_raise_err', default=not raise_error) raise_error = (not no_raise) or U.get_duplicated_kargs( ka, 'err', 'error', 'exp', 'exception', 'Exception', 'raise_err', 'raise_error', 'raiseError', 'raiseErr', 'raise_EnvironmentError', 'EnvironmentError', 'raiseEnvironmentError', default=raise_error) if not handle and not title and not pid and not process_name: handle = get_current_cmd_windows() if not handle: from qgb import Win for h, t, p in Win.getAllWindows(): if t == title or p == pid: handle = h break if process_name and process_name == U.get_process_name_by_pid(p): handle = h break else: if (not title) and (not process_name): raise py.ArgumentError(py.locals()) for h, t, p in Win.getAllWindows(): if title and title in t: handle = h break if process_name and process_name in U.get_process_name_by_pid( p): handle = h break else: if raise_error: raise py.EnvironmentError( 'cannot find ForegroundWindow title : ' + a) # if py.isint(title): # handle=title # else: # raise py.ArgumentError('foreground,a',row) import win32gui # if not win32gui.IsWindowVisible(handle): #先不考虑 try: # win32gui.SetForegroundWindow(handle) import win32gui, win32com.client, win32con, pythoncom pythoncom.CoInitialize( ) #加上这句解决 #pywintypes.com_error: (-2147221008, '尚未调用 CoInitialize。', None, None) shell = win32com.client.Dispatch("WScript.Shell") shell.SendKeys('%') ##For ALT prefix with % win32gui.ShowWindow(handle, win32con.SW_SHOW) win32gui.SetForegroundWindow(handle) except Exception as e: #BUG 窗口在后台,通过 http_rpc 调用此函数,第一次总会出错:,第二次才成功? # error(0, 'SetForegroundWindow', 'No error message is available') if 'No error message is available' in repr(e): for i in py.range(1, retry): try: if i % 9 == 1: U.sleep(0.01) # sleep一下有奇效,为什么? win32gui.SetForegroundWindow(handle) return U.IntCustomRepr( handle, repr='Win.set_foreground(%r) #retry:%s' % (handle, i)) except: pass # return py.No(e,'') if raise_error: raise return py.No(e) return U.IntCustomRepr(handle, repr='Win.set_foreground(%r)' % handle)