add default objects if loading fails
This commit is contained in:
42
src/data.py
42
src/data.py
@@ -2,6 +2,8 @@
|
|||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
"""Read and write data to or from file.
|
"""Read and write data to or from file.
|
||||||
|
|
||||||
|
:Date: 2019-06-12
|
||||||
|
|
||||||
.. module:: data
|
.. module:: data
|
||||||
:platform: *nix, Windows
|
:platform: *nix, Windows
|
||||||
:synopsis: Handle data files.
|
:synopsis: Handle data files.
|
||||||
@@ -11,7 +13,7 @@
|
|||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
import pickle
|
import pickle
|
||||||
|
|
||||||
def data_read(file_name, x_column, y_column):
|
def data_read(file_name, x_column, y_column, default=None, verbose=False):
|
||||||
"""Read ascii data file.
|
"""Read ascii data file.
|
||||||
|
|
||||||
:param filename: file to read
|
:param filename: file to read
|
||||||
@@ -20,27 +22,43 @@ def data_read(file_name, x_column, y_column):
|
|||||||
:type x_column: int
|
:type x_column: int
|
||||||
:param y_column: column index for the y data (first column is 0)
|
:param y_column: column index for the y data (first column is 0)
|
||||||
:type y_column: int
|
: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
|
:returns: x and y
|
||||||
:rtype: tuple(list, list)
|
:rtype: tuple(list, list)
|
||||||
"""
|
"""
|
||||||
import re
|
import re
|
||||||
file = open(file_name)
|
|
||||||
x = []
|
x = default
|
||||||
y = []
|
y = default
|
||||||
for row in file:
|
|
||||||
fields = re.split(r'\s+', row.strip())
|
if verbose:
|
||||||
#print(filds)
|
print('check if data is available')
|
||||||
x.append(float(fields[x_column]))
|
try:
|
||||||
y.append(float(fields[y_column]))
|
file = open(file_name)
|
||||||
file.close()
|
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
|
return x, y
|
||||||
|
|
||||||
def data_load(file_name, verbose=False):
|
def data_load(file_name, default=None, verbose=False):
|
||||||
"""Load stored program objects from binary file.
|
"""Load stored program objects from binary file.
|
||||||
|
|
||||||
:param file_name: file to load
|
:param file_name: file to load
|
||||||
:type file_name: str
|
:type file_name: str
|
||||||
|
:param default: return object if data loading fails
|
||||||
|
:type default: object
|
||||||
:param verbose: verbose information (default = False)
|
:param verbose: verbose information (default = False)
|
||||||
:type verbose: bool
|
:type verbose: bool
|
||||||
|
|
||||||
@@ -56,7 +74,7 @@ def data_load(file_name, verbose=False):
|
|||||||
print('found:')
|
print('found:')
|
||||||
print(object_data)
|
print(object_data)
|
||||||
except IOError:
|
except IOError:
|
||||||
object_data = None
|
object_data = default
|
||||||
if verbose:
|
if verbose:
|
||||||
print('no saved datas found')
|
print('no saved datas found')
|
||||||
return object_data
|
return object_data
|
||||||
|
|||||||
Reference in New Issue
Block a user