changed Object<->Dictionary functions and added function about free memory
This commit is contained in:
parent
4c6418d794
commit
bc708b4200
62
mcutils.py
62
mcutils.py
|
@ -9,6 +9,8 @@ import string
|
||||||
import scipy.signal
|
import scipy.signal
|
||||||
import functools
|
import functools
|
||||||
import types
|
import types
|
||||||
|
import sys
|
||||||
|
import collections
|
||||||
import os
|
import os
|
||||||
import pylab as plt
|
import pylab as plt
|
||||||
from itertools import chain
|
from itertools import chain
|
||||||
|
@ -453,7 +455,27 @@ class FFTfilter(object):
|
||||||
return np.fft.ifft(s*self.filter)
|
return np.fft.ifft(s*self.filter)
|
||||||
|
|
||||||
|
|
||||||
### PROMPT,PROCESS,TIME,DATE ... ETC. ###
|
### OS, PROMPT,PROCESS,TIME,DATE ... ETC. ###
|
||||||
|
|
||||||
|
def bytesToHuman(bytes,units="auto",fmt="%.2f %s"):
|
||||||
|
_units = dict( B = 0, KB = 1, MB = 2, GB = 3, TB = 4, PT = 5 )
|
||||||
|
_symb = {v: k for k, v in _units.items()}
|
||||||
|
bytes = float(bytes)
|
||||||
|
if units == "auto":
|
||||||
|
u = np.log(bytes)//np.log(1024)
|
||||||
|
units = _symb[u]
|
||||||
|
else:
|
||||||
|
u = _units[units]
|
||||||
|
value = bytes/1024**u
|
||||||
|
return fmt % (value,units)
|
||||||
|
|
||||||
|
|
||||||
|
def memAvailable(asHuman=True):
|
||||||
|
import psutil
|
||||||
|
m = psutil.virtual_memory(); # return bytes
|
||||||
|
m = m.available
|
||||||
|
if asHuman: m = bytesToHuman(m)
|
||||||
|
return m
|
||||||
|
|
||||||
class procCom(object):
|
class procCom(object):
|
||||||
def __init__(self,cmd):
|
def __init__(self,cmd):
|
||||||
|
@ -1188,16 +1210,34 @@ def insertInSortedArray(a,v):
|
||||||
|
|
||||||
### Objects ###
|
### Objects ###
|
||||||
|
|
||||||
def objToDict(o):
|
def objToDict(o,recursive=True):
|
||||||
""" convert a dropObject to a dictionary (useful for saving); it should work for other objects too """
|
""" convert a DictWrap to a dictionary (useful for saving); it should work for other objects too """
|
||||||
if isinstance(o,dict): return o
|
if isinstance(o,(dict,collections.OrderedDict)): return o
|
||||||
d = dict()
|
d = dict()
|
||||||
for k in o.__dict__.keys(): d[k] = getattr(o,k)
|
for k in o.keys():
|
||||||
|
try:
|
||||||
|
d[k] = objToDict(getattr(o,k))
|
||||||
|
except:
|
||||||
|
d[k] = o[k]
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
|
||||||
class dropObject(object):
|
class DictWrap(object):
|
||||||
pass
|
""" convert d = dict( a=1,b=np.arange(10),c=dict(d=5,e=dict(f=(3,4)) ))
|
||||||
|
to object recursively
|
||||||
|
r = DictWrap(d); r.c.e.f = (3,4) """
|
||||||
|
def __init__(self,d=None,recursive=True):
|
||||||
|
if d is not None:
|
||||||
|
if sys.version_info.major == 2:
|
||||||
|
for k,v in d.iteritems():
|
||||||
|
if recursive and isinstance(v,(dict,collections.OrderedDict)):
|
||||||
|
v = DictWrap(v,recursive=True)
|
||||||
|
setattr(self, k, v)
|
||||||
|
else:
|
||||||
|
for k,v in d.items():
|
||||||
|
if recursive and isinstance(v,(dict,collections.OrderedDict)):
|
||||||
|
v = DictWrap(v,recursive=True)
|
||||||
|
setattr(self, k, v)
|
||||||
|
|
||||||
def __getitem__(self,x):
|
def __getitem__(self,x):
|
||||||
return self.__dict__[x]
|
return self.__dict__[x]
|
||||||
|
@ -1209,7 +1249,7 @@ class dropObject(object):
|
||||||
self.__dict__[name]=data
|
self.__dict__[name]=data
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "dropObject with keys %s" % self.keys()
|
return "DictWrap with keys %s" % self.keys()
|
||||||
|
|
||||||
def keys(self):
|
def keys(self):
|
||||||
# list is needed because in py3 keys() returs a generator (and the sorting
|
# list is needed because in py3 keys() returs a generator (and the sorting
|
||||||
|
@ -1230,10 +1270,12 @@ class dropObject(object):
|
||||||
def dictToObj(d,recursive=True,cleanNames=True):
|
def dictToObj(d,recursive=True,cleanNames=True):
|
||||||
"""Return a class that has same attributes/values and
|
"""Return a class that has same attributes/values and
|
||||||
dictionaries key/value
|
dictionaries key/value
|
||||||
|
DictWrap can do recursively but this function cleans up the names allowing
|
||||||
|
easier tab completion
|
||||||
"""
|
"""
|
||||||
if not isinstance(d,dict): return None
|
if not isinstance(d,dict): return None
|
||||||
#define a dummy class
|
#define a dummy class
|
||||||
c = dropObject()
|
c = DictWrap()
|
||||||
for elem in d.keys():
|
for elem in d.keys():
|
||||||
key = elem
|
key = elem
|
||||||
if cleanNames:
|
if cleanNames:
|
||||||
|
@ -1254,7 +1296,7 @@ def Hdf5ToObj(h5):
|
||||||
h5hande = h5
|
h5hande = h5
|
||||||
else:
|
else:
|
||||||
h5hande = h5py.File(h5,"r")
|
h5hande = h5py.File(h5,"r")
|
||||||
ret = dropObject()
|
ret = DictWrap()
|
||||||
for h in h5hande:
|
for h in h5hande:
|
||||||
name = h.replace(":","_")
|
name = h.replace(":","_")
|
||||||
name = name.replace(".","_")
|
name = name.replace(".","_")
|
||||||
|
|
Loading…
Reference in New Issue