Source code for data
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Read and write data to or from file.
:Date: 2019-07-29
.. module:: data
:platform: *nix, Windows
:synopsis: Handle data files.
.. moduleauthor:: Daniel Weschke <daniel.weschke@directbox.de>
"""
from __future__ import print_function
import pickle
[docs]def read(file_name, x_column, y_column, default=None, verbose=False):
"""Read ascii data file.
:param filename: file to read
:type filename: str
:param x_column: column index for the x data (first column is 0)
:type x_column: int
:param y_column: column index for the y data (first column is 0)
:type y_column: int
:param default: return object if data loading fails
:type default: object
:param verbose: verbose information (default = False)
:type verbose: bool
:returns: x and y
:rtype: tuple(list, list)
"""
import re
x = default
y = default
if verbose:
print('check if data is available')
try:
file = open(file_name)
x = []
y = []
for row in file:
fields = re.split(r'\s+', row.strip())
#print(filds)
x.append(float(fields[x_column]))
y.append(float(fields[y_column]))
file.close()
except IOError:
if verbose:
print('data file not found')
return x, y
[docs]def write(file_name, data):
"""Write ascii file.
:param file_name: file to write
:type file_name: str
:param data: data to write
:type data: str
"""
with open(file_name, 'w') as file:
file.write(data)
[docs]def load(file_name, default=None, verbose=False):
"""Load stored program objects from binary file.
:param file_name: file to load
:type file_name: str
:param default: return object if data loading fails
:type default: object
:param verbose: verbose information (default = False)
:type verbose: bool
:returns: loaded data
:rtype: object
"""
if verbose:
print('check if data is available')
try:
with open(file_name, 'rb') as input:
# one load for every dump is needed to load all the data
object_data = pickle.load(input)
if verbose:
print('found:')
print(object_data)
except IOError:
object_data = default
if verbose:
print('no saved datas found')
return object_data
[docs]def store(file_name, object_data):
"""Store program objects to binary file.
:param file_name: file to store
:type file_name: str
:param object_data: data to store
:type object_data: object
"""
with open(file_name, 'wb') as output:
# every dump needs a load
pickle.dump(object_data, output, pickle.HIGHEST_PROTOCOL)
[docs]def fold_list(lst, n):
"""Convert one-dimensional kx1 array (list) to two-dimensional mxn
array. m = k / n
:param lst: list to convert
:type lst: list
:param n: length of the second dimenson
:type n: int
:returns: two-dimensional array (list of lists)
:rtype: list
"""
k = len(lst)
if k % n == 0:
length = int(k/n)
return [lst[i*n:i*n+n] for i in range(length)]
[docs]def unique_ending(ids, n=1):
"""From id list get list with unique ending.
:param ids: ids
:type ids: list
:param n: minumum chars or ints
:type n: int
:returns: unique ending of ids
:rtype: list
"""
if ids is not None:
x = [idi[-n:] for idi in ids]
if len(x) > len(set(x)):
return unique_ending(ids, n+1)
else:
return x
[docs]def get_id(ids, uide):
"""Get full id from unique id ending.
:param ids: ids
:type ids: list
:param uide: unique id ending
:type uide: str
:returns: full id
:rtype: str or int
"""
# we know it is a unique ending
return [idi for idi in ids if idi.endswith(uide)][0]