From bc708b4200e0e91db216a6847df8a885035bd469 Mon Sep 17 00:00:00 2001 From: marco cammarata Date: Thu, 24 Nov 2016 18:01:50 +0100 Subject: [PATCH] changed Object<->Dictionary functions and added function about free memory --- mcutils.py | 62 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/mcutils.py b/mcutils.py index 58db3fc..17d318f 100644 --- a/mcutils.py +++ b/mcutils.py @@ -9,6 +9,8 @@ import string import scipy.signal import functools import types +import sys +import collections import os import pylab as plt from itertools import chain @@ -453,7 +455,27 @@ class FFTfilter(object): 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): def __init__(self,cmd): @@ -1188,16 +1210,34 @@ def insertInSortedArray(a,v): ### Objects ### -def objToDict(o): - """ convert a dropObject to a dictionary (useful for saving); it should work for other objects too """ - if isinstance(o,dict): return o +def objToDict(o,recursive=True): + """ convert a DictWrap to a dictionary (useful for saving); it should work for other objects too """ + if isinstance(o,(dict,collections.OrderedDict)): return o 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 -class dropObject(object): - pass +class DictWrap(object): + """ 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): return self.__dict__[x] @@ -1209,7 +1249,7 @@ class dropObject(object): self.__dict__[name]=data def __str__(self): - return "dropObject with keys %s" % self.keys() + return "DictWrap with keys %s" % self.keys() def keys(self): # 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): """Return a class that has same attributes/values and 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 #define a dummy class - c = dropObject() + c = DictWrap() for elem in d.keys(): key = elem if cleanNames: @@ -1254,7 +1296,7 @@ def Hdf5ToObj(h5): h5hande = h5 else: h5hande = h5py.File(h5,"r") - ret = dropObject() + ret = DictWrap() for h in h5hande: name = h.replace(":","_") name = name.replace(".","_")