Beispiel #1
0
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
Beispiel #2
0
def msgbox(s='', st='title', *a):
    '''
__import__('ctypes').windll.user32.MessageBoxW(0, 'text', 'title', 0)	
	
	'''
    if (a):
        a = py.list(a)
        a.insert(0, s)
        a.insert(1, st)
        st = 'length %s' % len(a)
        s = str(a)
    # s=str(s)+ ','+str(a)#[1:-2]
    if py.is2():
        return user32.MessageBoxA(0, str(s), str(st), 0)
    else:
        return user32.MessageBoxW(0, str(s), str(st), 0)
Beispiel #3
0
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)
Beispiel #4
0
def get_cursor_pos_color(x=None, y=None, **ka):
    ''' x or y arg 优先
	
	return [x,y],color  '''
    U, T, N, F = py.importUTNF()

    if not py.isint(x) and y == None:
        pos = x
    if x == None or y == None:
        pos = U.get_duplicated_kargs(ka, 'pos', 'xy')
        if not pos:
            pos = get_cursor_pos()
    else:
        pos = [x, y]
    pos = py.list(pos)
    if py.isint(x): pos[0] = x
    if py.isint(y): pos[1] = y

    import win32gui
    color = win32gui.GetPixel(win32gui.GetDC(win32gui.GetActiveWindow()), *pos)
    if color < 256: color = 256 * 256 * color
    color = U.IntCustomRepr(color, repr=U.rgb_name(color))
    return pos, color
Beispiel #5
0
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)
Beispiel #6
0
Datei: F.py Projekt: QGB/QPSU
def csvAsList(fn):
	import csv
	with py.open(fn, 'rb') as f:
		reader = csv.reader(f)
		return py.list(reader)
Beispiel #7
0
Datei: F.py Projekt: QGB/QPSU
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
Beispiel #8
0
Datei: F.py Projekt: QGB/QPSU
def glob(path='./', pattern='**/*'):
	'''pattern='**/*'   : all files
	'**/*.txt'   : all txt files'''
	import pathlib
	return py.list(pathlib.Path(path).glob(pattern))
Beispiel #9
0
Datei: F.py Projekt: QGB/QPSU
def read_xls_sheets_name(file):
	''' XLRDError: Excel xlsx file; not supported'''
	import xlrd                         
	w=xlrd.open_workbook(file)           
	return py.list(py.enumerate( w.sheet_names() )  )