mirror of
https://github.com/LCPQ/quantum_package
synced 2024-12-22 20:35:19 +01:00
Add qp_utils.py for cache decorator, remove pickl in ei_handler.py
This commit is contained in:
parent
ec26b457b3
commit
7ccfee69a5
@ -62,6 +62,9 @@ import ConfigParser
|
||||
from collections import defaultdict
|
||||
from collections import namedtuple
|
||||
|
||||
|
||||
from qp_utils import cache
|
||||
|
||||
Type = namedtuple('Type', 'fancy ocaml fortran')
|
||||
|
||||
|
||||
@ -78,6 +81,7 @@ def is_bool(str_):
|
||||
raise TypeError
|
||||
|
||||
|
||||
@cache
|
||||
def get_type_dict():
|
||||
"""
|
||||
This function makes the correspondance between the type of value read in
|
||||
@ -89,17 +93,7 @@ def get_type_dict():
|
||||
# ~#~#~#~#~ #
|
||||
# P i c l e #
|
||||
# ~#~#~#~#~ #
|
||||
|
||||
import cPickle as pickle
|
||||
|
||||
from os import listdir
|
||||
|
||||
qpackage_root = os.environ['QPACKAGE_ROOT']
|
||||
fancy_type_pickle = qpackage_root + "/scripts/ezfio_interface/fancy_type.p"
|
||||
|
||||
if fancy_type_pickle in listdir(os.getcwd()):
|
||||
fancy_type = pickle.load(open(fancy_type_pickle, "rb"))
|
||||
return fancy_type
|
||||
|
||||
# ~#~#~#~ #
|
||||
# I n i t #
|
||||
@ -148,9 +142,7 @@ def get_type_dict():
|
||||
b = r.find('let untouched = "')
|
||||
e = r.find(';;', b)
|
||||
|
||||
l_un = [
|
||||
i for i in r[
|
||||
b:e].splitlines() if i.strip().startswith("module")]
|
||||
l_un = [i for i in r[b:e].splitlines() if i.strip().startswith("module")]
|
||||
|
||||
# ~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~#~ #
|
||||
# q p _ t y p e s _ g e n e r a t e #
|
||||
@ -174,9 +166,6 @@ def get_type_dict():
|
||||
# ~#~#~#~#~#~#~#~ #
|
||||
# F i n a l i z e #
|
||||
# ~#~#~#~#~#~#~#~ #
|
||||
|
||||
pickle.dump(dict(fancy_type), open(fancy_type_pickle, "wb"))
|
||||
|
||||
return dict(fancy_type)
|
||||
|
||||
|
||||
|
@ -24,24 +24,7 @@ from docopt import docopt
|
||||
import os
|
||||
import sys
|
||||
import os.path
|
||||
from functools import wraps
|
||||
|
||||
|
||||
def cache(func):
|
||||
"""
|
||||
A decorator for lazy evaluation off true function
|
||||
"""
|
||||
saved = {}
|
||||
|
||||
@wraps(func)
|
||||
def newfunc(*args):
|
||||
if args in saved:
|
||||
return saved[args]
|
||||
|
||||
result = func(*args)
|
||||
saved[args] = result
|
||||
return result
|
||||
return newfunc
|
||||
from qp_utils import cache
|
||||
|
||||
|
||||
@cache
|
||||
|
17
scripts/qp_utils.py
Normal file
17
scripts/qp_utils.py
Normal file
@ -0,0 +1,17 @@
|
||||
from functools import wraps
|
||||
|
||||
def cache(func):
|
||||
"""
|
||||
A decorator for lazy evaluation off true function
|
||||
"""
|
||||
saved = {}
|
||||
|
||||
@wraps(func)
|
||||
def newfunc(*args):
|
||||
if args in saved:
|
||||
return saved[args]
|
||||
|
||||
result = func(*args)
|
||||
saved[args] = result
|
||||
return result
|
||||
return newfunc
|
@ -16,10 +16,6 @@ default: ezfio
|
||||
|
||||
veryclean:
|
||||
$(QPACKAGE_ROOT)/scripts/module/clean_modules.sh $(ALL_MODULES)
|
||||
# Define the dict [type in EZFIO.cfg] = ocaml type , f90 type
|
||||
# If you change the qptypes_generator.ml, you need to rm this
|
||||
# For simplicity add this to the veryclean rule
|
||||
rm -f $(QPACKAGE_ROOT)/scripts/ezfio_interface/fancy_type.p
|
||||
|
||||
$(ALL_MODULES): ezfio
|
||||
$(QPACKAGE_ROOT)/scripts/module/build_modules.sh $@
|
||||
|
Loading…
Reference in New Issue
Block a user