Skip to content

Commit

Permalink
Factorized
Browse files Browse the repository at this point in the history
  • Loading branch information
benbaror committed Apr 6, 2017
1 parent c9afee6 commit 7a23b61
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/replotlib/replot.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,39 +120,32 @@ def __init__(self, data_file, ax=None, file_type='json', style=None,
self._action_number = 0
self.file_type = file_type
self.data_file = data_file
self._style = style if style else {}
self._rcParams = rcParams if rcParams else {}

if erase:
try:
os.remove(self.data_file)
except OSError:
pass
self.clean()

if file_type == 'json':
if self.file_type == 'json':
self.io = JsonIO(self.data_file)
elif file_type == 'hdf5':
elif self.file_type == 'hdf5':
self.io = HDF5IO(self.data_file)
else:
raise NotImplementedError(self.file_type)

try:
style = self.io.read()['style']
style.update(self._style)
self._style = style
except (IOError, KeyError):
pass

try:
rcParams = self.io.read()['rcParams']
rcParams.update(self._rcParams)
self._rcParams = rcParams
except (IOError, KeyError):
pass
self._style = self.update('style', style)
self._rcParams = self.update('rcParams', rcParams)

plt.rcParams.update(self.rcParams)
self._ax = ax if ax else plt.gca()

def clean(self):
"""
Delete the file
"""
try:
os.remove(self.data_file)
except OSError:
pass

@property
def action_number(self):
"""Number of action called."""
Expand Down Expand Up @@ -228,3 +221,15 @@ def apply_style(self, kwargs, extra_styles=None):

except KeyError:
pass

def update(self, ex_dict, new_dict):
"""
Updated existing dictionary named `ex_dict` in database with `new_dict`
"""
new_dict = new_dict if new_dict else {}
try:
dct = self.io.read()[ex_dict]
dct.update(new_dict)
return dct
except (IOError, KeyError):
return new_dict

0 comments on commit 7a23b61

Please sign in to comment.