Cannot get the value from dogpile.cache from different modules.
xeon Mailinglist
xeonmailinglist at gmail.com
Tue Dec 29 17:57:39 EST 2015
On Tuesday, December 29, 2015 at 7:23:40 PM UTC, Mark Lawrence wrote:
> On 29/12/2015 17:27, xeon Mailinglist wrote:
> > On Tuesday, December 29, 2015 at 5:15:24 PM UTC, Mark Lawrence wrote:
> >> On 29/12/2015 15:20, xeon Mailinglist wrote:
> >>> On Tuesday, December 29, 2015 at 11:16:10 AM UTC, xeon Mailinglist wrote:
> >>>> 1. How do I create a global variable that can be accessed by all classes?
> >>>>
> >>>> 2. I am using `dogpile.cache` to store data in the cache [1], but if I set and get the same key from different modules, I don't get the value. Here is an example in [2]. The value than I get is `NO_VALUE.NO_VALUE`. Why this happens?
> >>>>
> >>>> setter is the setter.py
> >>>> getter is the getter.py
> >>>> Memoize is the file in [1].
> >>>>
> >>>>
> >>>> [1] my dogpile class `Memoize.py`
> >>>>
> >>>> from dogpile.cache import make_region
> >>>>
> >>>> region = make_region().configure('dogpile.cache.memory')
> >>>>
> >>>> def save(key, value):
> >>>> """
> >>>> general purpose method to save data (value) in the cache
> >>>>
> >>>> :param key (string) key of the value to be saved in cache
> >>>> :param value (any type) the value to be saved
> >>>> """
> >>>> region.set(key, value)
> >>>>
> >>>>
> >>>> def get(key):
> >>>> """
> >>>> general purpose method to get data from the cache
> >>>>
> >>>> :param key (string) key of the data to be fetched
> >>>> :return value (any type) data to be returned from the cache
> >>>> """
> >>>> return region.get(key)
> >>>>
> >>>>
> >>>> [2] My python example
> >>>>
> >>>> `setter.py`
> >>>>
> >>>> def myset(value):
> >>>> Memoize.save("myvalue", value)
> >>>>
> >>>> `getter.py`
> >>>>
> >>>> def myget():
> >>>> return Memoize.get("myvalue") <- this value is NO_VALUE. NO_VALUE
> >>>>
> >>>> My class:
> >>>>
> >>>> setter.myset(123)
> >>>> getter.myget()
> >>>
> >>> The idea that I get from dogpile, is that in each module (getter.py, or setter.py) there is a dictionary where the values are stored in the backend. Hence, getter.py has its dictionary and setter.py has its dictionary also. In the end, there is not a single dictionary where all the values should be put. And I want a single dictionary.
> >>>
> >>
> >> Then put everything in one file. Three files for the amount of code you
> >> show above is nonsensical. You might like to read
> >> http://dirtsimple.org/2004/12/python-is-not-java.html and in response to
> >> that http://dirtsimple.org/2004/12/java-is-not-python-either.html
> >>
> >> --
> >> My fellow Pythonistas, ask not what our language can do for you, ask
> >> what you can do for our language.
> >>
> >> Mark Lawrence
> >
> > Here is the full class that I use to store the data.
> >
> > from dogpile.cache import make_region
> >
> >
> > # my_dictionary = {}
> > region = make_region().configure('dogpile.cache.memory')
> > # arguments={"cache_dict":my_dictionary})
> > class Cache:
> >
> > @staticmethod
> > def save(key, value):
> > """
> > general purpose method to save data (value) in the cache
> >
> > :param key (string) key of the value to be saved in cache
> > :param value (any type) the value to be saved
> > """
> > region.set(key, value)
> >
> > @staticmethod
> > def get(key):
> > """
> > general purpose method to get data from the cache
> >
> > :param key (string) key of the data to be fetched
> > :return value (any type) data to be returned from the cache
> > """
> > return region.get(key)
> >
> >
> > @staticmethod
> > def get_or_create(key):
> > """
> > General purpose method to get data from the cache. If the value does not exist, it creates a list
> >
> > :param: key (string) key of the data to be fetched
> > :return value (any type) data to be returned from the cache
> > """
> > return region.get_or_create(key, list)
> >
> > @staticmethod
> > def set_job_predictions(rank_list):
> > Cache.save("job_predictions", rank_list)
> >
> > @staticmethod
> > def get_job_predictions():
> > return Cache.get("job_predictions")
> >
>
> I get the strong impression that you're reinventing wheels and doing it
> so badly that they're triangular. Have you tried pypi
> https://pypi.python.org/pypi for proven code that could do the job for you?
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask
> what you can do for our language.
>
> Mark Lawrence
@Mark
I don't understand your remark. Pypi is a repository. What this has to do with my problem?
@Peter
I have solve the problem, although it is a little bit unclear to me what is the reason. I start to think that this is a problem related to packages and sub-packages. Eg. method1() is in the mypkg/file1.py, and method2() is in mypkg/scheduler/file2.py.
More information about the Python-list
mailing list