Skip to content
Prev Previous commit
Next Next commit
Added a class to manage collecting/applying the kwarg dictionaries.
Includes dumping to/loading from json files.
  • Loading branch information
tacaswell committed Dec 2, 2013
commit 5963b00b05e4c82bb2e7eec8c020792bd69edcab
117 changes: 116 additions & 1 deletion lib/matplotlib/rcparam_ng.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)
import six
from collections import namedtuple
from collections import namedtuple, defaultdict
from copy import copy
import json

from functools import wraps

import matplotlib

_kw_dict_nm = '_kw_defaults'
_kw_entry = namedtuple('_kw_entry', ['orig_funtion', 'kw_dict'])



def set_defaults(cls, key, new_defaults):
"""
Set a set of default kwargs for the function `key` on
Expand Down Expand Up @@ -143,3 +147,114 @@ def reset_defaults(cls, key):
orig_fun, old_dict = kw_dict.pop(key)
# reset to the original function
setattr(cls, key, orig_fun)

def string_to_class(klass):
"""
Turns a string -> a class object
"""
last_level = matplotlib
# split the string
split_klass = klass.split('.')
# strip the matplotlib off the front
if split_klass[0] == 'matplotlib':
split_klass.pop(0)

for _k in split_klass:
if not hasattr(last_level, _k):
raise ValueError("not valid, make msg better")
last_level = getattr(last_level, _k)
if not isinstance(last_level, type):
raise ValueError("not valid, make msg better")

return last_level

class RcParamsNG(object):
"""
A class for keeping track of default values
"""
def __init__(self, input_dict=None):
"""
Parameters
----------
input_dict : dict
a dict of dicts. Top level keys are strings from hte classes
inner keys are function names, inner values are kwarg diccts

"""
self.core_dict = defaultdict(dict)
if input_dict is not None:
self.core_dict.update(input_dict)

def store_default(self, klass, key, new_defaults):
"""
Adds an entry to the core for the given values

Parameters
----------
klass : str
string name of class to set defaults for

key : str
function to set the defaults for

new_defaults : dict
dict containing the new defaults (kwarg pairs)
"""
self.core_dict[klass][key] = new_defaults

def set_defaults(self):
"""
Set the defaults contained in this object. Use `set_defaults`
which removes any existing defaults, leaving only the values
in this object in place.
"""
# loop over the core dictionary
for klass, kw_pair in six.iteritems(self.core_dict):
# turn the string into a class
cls = string_to_class(klass)
# look over the list of keys and set the defaults
for key, default_dict in six.iteritems(kw_pair):
set_defaults(cls, key, default_dict)

def update_defaults(self):
"""
Update to the default values contained in this object.
Use `update_defaults` which leaves non-conflicting defaults
in place.
"""
# loop over the core dictionary
for klass, kw_pair in six.iteritems(self.core_dict):
# turn the string into a class
cls = string_to_class(klass)
# look over the list of keys and set the defaults
for key, default_dict in six.iteritems(kw_pair):
update_defaults(cls, key, default_dict)

def to_json(self, out_file_path):
"""
Dumps default values to json file. Use `from_json` to
recover.

Parameters
----------
out_file_path : str
A valid path to save the json file to. Will overwrite
any existing file at path
"""
with open(out_file_path, 'w') as fout:
json.dump(self.core_dict, fout, ensure_ascii=False)

@classmethod
def from_json(cls, in_file_path):
"""
Creates a new RcParamsNG object from a json file. (see
`to_json` to dump to json).

Parameters
----------
in_file_path : str
Path to a json file to load.
"""
with open(in_file_path, 'r') as fin:
in_dict = json.load(fin)
return cls(in_dict)