easyvvuq.utils.helpers
1from ast import literal_eval 2import dill 3import base64 4 5 6def easyvvuq_serialize(obj): 7 """Takes an object and returns a string with that object serialized. 8 9 Parameters 10 ---------- 11 obj: obj 12 An arbitrary Python object. 13 14 Returns 15 ------- 16 str: 17 A string representation of obj. 18 """ 19 return base64.b64encode(dill.dumps(obj)).decode('utf-8') 20 21 22def easyvvuq_deserialize(s): 23 """Takes a serialized objected and reconstructs it. 24 25 Parameters 26 ---------- 27 s: str 28 A serialized Python object. 29 30 Returns 31 ------- 32 obj: 33 A previously serialized Python object. 34 """ 35 if isinstance(s, bytes): 36 return dill.loads(base64.b64decode(s)) 37 else: 38 return dill.loads(base64.b64decode(s.encode('utf-8'))) 39 40 41def multi_index_tuple_parser(lst): 42 """ 43 Parses a list of strings to tuples if they represent tuples, otherwise 44 leaves them as is. 45 46 Parameters 47 __________ 48 lst : list of strings 49 50 Returns 51 _______ 52 a tuple consisting of a list of tuples and/or strings and a bool indicating if 53 the lst contains any tuples 54 """ 55 contains_tuples = True 56 if len(lst) == 0: 57 raise RuntimeError('multi_index_tuple_parser needs a non-empty list of strings') 58 for name in lst: 59 if not isinstance(name, str): 60 raise RuntimeError('multi_index_tuple_parser needs a list of strings') 61 result = [] 62 for name in lst: 63 if name.strip()[0] == '(' and name.strip()[-1] == ')': 64 name = literal_eval(name) 65 else: 66 contains_tuples = False 67 result.append(name) 68 return result, contains_tuples 69 70 71def remove_start_of_file(filename, delimiter): 72 """Overwrite a file leaving only the portion of it after the 73 delimiter string. 74 75 Parameters 76 ---------- 77 filename : str 78 the name (and path) of the file to processs 79 80 delimiter : str 81 the string after which the file is copied over 82 """ 83 with open(filename, 'r') as fd: 84 lines = fd.readlines() 85 remaining = [] 86 after_delimiter = False 87 for line in lines: 88 if after_delimiter: 89 remaining.append(line) 90 if line.strip() == delimiter: 91 after_delimiter = True 92 with open(filename, 'w') as fd: 93 for line in remaining: 94 fd.write(line)
def
easyvvuq_serialize(obj):
7def easyvvuq_serialize(obj): 8 """Takes an object and returns a string with that object serialized. 9 10 Parameters 11 ---------- 12 obj: obj 13 An arbitrary Python object. 14 15 Returns 16 ------- 17 str: 18 A string representation of obj. 19 """ 20 return base64.b64encode(dill.dumps(obj)).decode('utf-8')
Takes an object and returns a string with that object serialized.
Parameters
- obj (obj): An arbitrary Python object.
Returns
- str:: A string representation of obj.
def
easyvvuq_deserialize(s):
23def easyvvuq_deserialize(s): 24 """Takes a serialized objected and reconstructs it. 25 26 Parameters 27 ---------- 28 s: str 29 A serialized Python object. 30 31 Returns 32 ------- 33 obj: 34 A previously serialized Python object. 35 """ 36 if isinstance(s, bytes): 37 return dill.loads(base64.b64decode(s)) 38 else: 39 return dill.loads(base64.b64decode(s.encode('utf-8')))
Takes a serialized objected and reconstructs it.
Parameters
- s (str): A serialized Python object.
Returns
- obj:: A previously serialized Python object.
def
multi_index_tuple_parser(lst):
42def multi_index_tuple_parser(lst): 43 """ 44 Parses a list of strings to tuples if they represent tuples, otherwise 45 leaves them as is. 46 47 Parameters 48 __________ 49 lst : list of strings 50 51 Returns 52 _______ 53 a tuple consisting of a list of tuples and/or strings and a bool indicating if 54 the lst contains any tuples 55 """ 56 contains_tuples = True 57 if len(lst) == 0: 58 raise RuntimeError('multi_index_tuple_parser needs a non-empty list of strings') 59 for name in lst: 60 if not isinstance(name, str): 61 raise RuntimeError('multi_index_tuple_parser needs a list of strings') 62 result = [] 63 for name in lst: 64 if name.strip()[0] == '(' and name.strip()[-1] == ')': 65 name = literal_eval(name) 66 else: 67 contains_tuples = False 68 result.append(name) 69 return result, contains_tuples
Parses a list of strings to tuples if they represent tuples, otherwise leaves them as is.
Parameters
lst : list of strings
Returns
a tuple consisting of a list of tuples and/or strings and a bool indicating if the lst contains any tuples
def
remove_start_of_file(filename, delimiter):
72def remove_start_of_file(filename, delimiter): 73 """Overwrite a file leaving only the portion of it after the 74 delimiter string. 75 76 Parameters 77 ---------- 78 filename : str 79 the name (and path) of the file to processs 80 81 delimiter : str 82 the string after which the file is copied over 83 """ 84 with open(filename, 'r') as fd: 85 lines = fd.readlines() 86 remaining = [] 87 after_delimiter = False 88 for line in lines: 89 if after_delimiter: 90 remaining.append(line) 91 if line.strip() == delimiter: 92 after_delimiter = True 93 with open(filename, 'w') as fd: 94 for line in remaining: 95 fd.write(line)
Overwrite a file leaving only the portion of it after the delimiter string.
Parameters
- filename (str): the name (and path) of the file to processs
- delimiter (str): the string after which the file is copied over