Browse Source

publish muk_utils - 12.0

pull/9/head
MuK IT GmbH 6 years ago
parent
commit
75a2dcac38
  1. 2
      muk_utils/__manifest__.py
  2. 3
      muk_utils/tools/__init__.py
  3. 11
      muk_utils/tools/cache.py
  4. 33
      muk_utils/tools/types.py

2
muk_utils/__manifest__.py

@ -20,7 +20,7 @@
{
"name": "MuK Utils",
"summary": """Utility Features""",
"version": '12.0.1.0.16',
"version": '12.0.1.0.17',
"category": 'Extra Tools',
"license": "AGPL-3",
"author": "MuK IT",

3
muk_utils/tools/__init__.py

@ -19,4 +19,5 @@
from . import file
from . import json
from . import cache
from . import cache
from . import types

11
muk_utils/tools/cache.py

@ -18,8 +18,13 @@
###################################################################################
import time
import logging
import functools
_logger = logging.getLogger(__name__)
class memoize(object):
_caches = {}
_timeouts = {}
@ -38,16 +43,20 @@ class memoize(object):
def __call__(self, func):
self.cache = self._caches[func] = {}
self._timeouts[func] = self.timeout
@functools.wraps(func)
def wrapper(*args, **kwargs):
current_time = time.time()
kw = sorted(kwargs.items())
key = (args, tuple(kw))
try:
print("TRY CACHE")
value = self.cache[key]
print("VALUE")
if (current_time - value[1]) > self.timeout:
print("TIMEOUT")
raise KeyError
except KeyError:
print("NEW")
value = self.cache[key] = (func(*args,**kwargs), current_time)
return value[0]
wrapper.func_name = func.__name__
return wrapper

33
muk_utils/tools/types.py

@ -0,0 +1,33 @@
###################################################################################
#
# Copyright (C) 2018 MuK IT GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###################################################################################
import logging
_logger = logging.getLogger(__name__)
class Singleton(type):
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
Loading…
Cancel
Save