-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmethods_pnextword.py
36 lines (27 loc) · 1.02 KB
/
methods_pnextword.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# -*- coding: utf-8 -*-
import os
from os.path import join
import pickle
import matplotlib.pyplot as plt
def cache_object(obj, name, folder="cache"):
pickle.dump(obj, open(join(folder, name+'.pickle'), 'wb'), pickle.HIGHEST_PROTOCOL)
def load_object(name, folder='cache'):
return pickle.load(open(join(folder, name + '.pickle'), 'rb'))
def is_object_cached(name, folder='cache'):
return os.path.exists(join(folder, name + '.pickle'))
#sort a dictionary
def sort_dict(d, i='values', m='desc'):
if type(i) is str:
i = 0 if i == 'keys' else 1
if type(m) is str:
m = 1 if m == 'asc' else -1
return {k:v for k,v in sorted(d.items(), key=lambda item: m*item[i])}
#dict to plot
def plot_dict(d, l='', source=None, **kwargs):
if source is None: source = plt
d = sort_dict(d,0,1)
if l != '':
source.plot(d.keys(), d.values(), label=l, **kwargs)
source.legend(loc="upper left")
else:
source.plot(d.keys(), d.values(), **kwargs)