[docs]defis_iterable(some_stuff):""" this function chels wether or not a variable contains an iterrable Parameters ---------- some_stuff : variable to check Returns ------- False if a string or a number, True if iterrable (table, dict, tupple, numpy array...) """try:_=(aforainsome_stuff)ifisinstance(some_stuff,str):flag=Falseelse:flag=TrueexceptTypeError:flag=Falsereturnflag
[docs]defrmv_ext(fname):""" return filename without extension Parameters ---------- fname : str file name with or without extention Returns ------- fname : str file name without extention """ifisinstance(fname,str):i=fname.rfind(".")ifi>0:fname=fname[:i]returnfname
[docs]defgenerate_new_fname(fname):""" Prevent overwriting existing files. if the filename exists, add a number or add one to the number at the end of the filename Parameters ---------- fname : str name of the file to check """ifos.path.isfile(fname):foriinrange(len(fname)):iffname[-i-1]==".":try:fname=(fname[:-i-2]+str(1+int(fname[-i-2]))+fname[-i-1:])except:fname=fname[:-i-1]+"0"+fname[-i-1:]returngenerate_new_fname(fname)returnfname
####################################### Folder and archive related code #######################################
[docs]defcreate_folder(foldername,access_rights=0o755):""" create a folder with controled access rights. Parameters ---------- foldername : str name of the folder to create access_rights : int unix like rights """try:os.mkdir(foldername,access_rights)exceptOSError:pass_info("Creation of the directory %s failed, this folder may already exist"%foldername)
######################### JSON related code #########################
[docs]defcheck_json_fname(fname):""" Add ".json" extension is missing at the end of the file name and check if it exists. Parameters ---------- fname : str name of the file Retruns ------- fname : str name of the file with the ".json" extension added if required Errors ------ NRV_Error rised if fname does not exist """iffname[-5:]!=".json":fname+=".json"ifos.path.isfile(fname):returnfnameelse:rise_error(fname+" not found cannot be load")
[docs]defjson_dump(results,filename):""" save stuff as a json file Parameters ---------- results : stuff to save filename : str name of the file where results are saved """withopen(filename,"w")asfile_to_save:json.dump(results,file_to_save,cls=NRV_Encoder)
[docs]defjson_load(filename):""" Load stuff from a json file Parameters ---------- filename : str name of the file where results are stored Returns ------- results : dictionary stuff from file """withopen(check_json_fname(filename),"r")asfile_to_read:results=json.load(file_to_read)returnresults
## TODO add NRV_decoder to simplify NRV_class save/load methode
[docs]classNRV_Encoder(json.JSONEncoder):""" Json encoding class, specific for NRV2 axon prevent from type error due to np.arrays solution taken as this from askpython.com """
[docs]defdefault(self,obj):# If the object is a numpy arrayifisinstance(obj,np.integer):result=int(obj)elifisinstance(obj,np.floating):result=float(obj)elifisinstance(obj,np.ndarray):result=obj.tolist()elifisinstance(obj,DataFrame):result=obj.to_dict()elifisinstance(obj,set):result=list(obj)else:# Let the base class Encoder handle the objectresult=json.JSONEncoder.default(self,obj)returnresult
######################## DXF related code ########################
[docs]defload_dxf_file(filename):""" UNDER DEV """doc=Nonetry:doc=ezdxf.readfile(filename)exceptIOError:rise_error("Not a DXF file or a generic I/O error.")exceptezdxf.DXFStructureError:rise_error("Invalid or corrupted DXF file.",out=2)returndoc