Beispiel #1
0
Datei: F.py Projekt: QGB/QPSU
def int_to_size_str(size,b1024=True,zero='0 B',less_than_zero='%s', ):
	'''Convert a file size to human-readable form.
	Keyword arguments:
	size -- file size in bytes
	b1024 -- if True (default), use multiples of 1024
	if False, use multiples of 1000
	Returns: string
	test git
	'''
	U,T,N,F=py.importUTNF()	
	if py.istr(size) or py.isbyte(size):
		size=U.len(size)
	size=py.int(size)
	if size < 0:
		if py.callable(less_than_zero):
			return less_than_zero(size)
		if py.istr(less_than_zero):
			if '%' in less_than_zero:
				return less_than_zero%size
			if '{' in less_than_zero and '}' in less_than_zero:
				return less_than_zero.format(size)
		return less_than_zero
	if size==0:return zero
		# raise ValueError('number must be non-negative')

	multiple = 1024.0 if b1024 else 1000.0 #another if statement style
	
	if size<=multiple:return py.str(size)+' B'
	
	for suffix in SUFFIXES[multiple]:
		size /= multiple
		if size < multiple:
			return '{0:.3f} {1}'.format(size, suffix)
	raise ValueError('number too large')
Beispiel #2
0
Datei: F.py Projekt: QGB/QPSU
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)
Beispiel #3
0
Datei: F.py Projekt: QGB/QPSU
	def __new__(cls, *a, **ka):
	#int() argument must be a string, a bytes-like object or a number, not 
		
		if py.istr(a[0]) or py.isbyte(a[0]) or py.isnumber(a[0]):
			self= py.int.__new__(cls, *a)
		else:
			self= a[0]
		self.ka=ka
		return self
Beispiel #4
0
Datei: F.py Projekt: QGB/QPSU
def include(file,keyword):
	if py.isbyte(keyword):mod='rb'
	else:mod='r'
	try:
		with py.open(file,mod) as f:
			for i in f:
				if keyword in i:return True
	except Exception as e:
		return py.No(e)
	return False
Beispiel #5
0
Datei: reg.py Projekt: QGB/QPSU
def set(skey,
        name,
        value,
        root=HKEY_CURRENT_USER,
        type='auto,or REG_TYPE int',
        returnType=True):
    r = OpenKey(root, skey, 0, KEY_SET_VALUE)
    if not py.isint(type):
        if py.isint(value): type = 4
        if py.istr(value): type = 1
        if py.isbyte(value): type = 3  #TODO test,and add more rule

    SetValueEx(r, 'ProxyEnable', 0, type, value)
    if get(skey, name, root=root, returnType=False) == value:
        return 'reg.set [{}] {}={} sucess!'.format(skey[-55:], name, value)
    else:
        return 'reg.set [{}] {}={} Failed !'.format(skey, name, value)
Beispiel #6
0
Datei: F.py Projekt: QGB/QPSU
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