Guest User

Untitled

a guest
Dec 14th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. import dill
  2. import lmfit
  3. from collections import OrderedDict as od
  4.  
  5.  
  6. #%% 1) this works ####################################
  7. class test(object):
  8.  
  9. def inside_func(*args):
  10. return
  11.  
  12. def __init__(self):
  13. # check OrderedDict dill-pickleable (should be)
  14. self.d = od([])
  15. self.d.update({'func': self.inside_func})
  16. return
  17.  
  18. t = test()
  19.  
  20. with open('test.pkl', 'wb') as f:
  21. dill.dump(t, f)
  22.  
  23.  
  24. #%% 2) this also works ###############################
  25.  
  26. def outside_func(*args):
  27. return
  28.  
  29. class test(object):
  30.  
  31. def __init__(self):
  32. # some dummy Parameters set
  33. self.p = lmfit.Parameters()
  34. self.p.add('var1')
  35. self.p.add('var2')
  36.  
  37. # addition of 'func' to _asteval's symtable from outside class scope
  38. self.p._asteval.symtable['func'] = outside_func
  39. return
  40.  
  41. t = test()
  42.  
  43. with open('test.pkl', 'wb') as f:
  44. dill.dump(t, f)
  45.  
  46.  
  47.  
  48. #%% 3) this doesn't work ###############################
  49.  
  50. class test(object):
  51. def inside_func(*args):
  52. return
  53.  
  54. def __init__(self):
  55. # some dummy Parmaeters set
  56. self.p = lmfit.Parameters()
  57. self.p.add('var1')
  58. self.p.add('var2')
  59.  
  60. # addition of 'func' to _asteval's symtable from inside class scope
  61. if not any('func' == x for x in self.p._asteval.symtable.keys()):
  62. self.p._asteval.symtable.update({'func': self.inside_func})
  63. return
  64.  
  65. t = test()
  66.  
  67. with open('test.pkl', 'wb') as f:
  68. dill.dump(t, f)
Add Comment
Please, Sign In to add comment