From 7cfe37ad1a9bc198eee717dd0e4cae66c48dfa34 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Fri, 23 Nov 2018 21:59:27 +0100 Subject: [PATCH 01/33] Make `cached_property` derive from `property` --- cached_property.py | 77 +++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 38 deletions(-) diff --git a/cached_property.py b/cached_property.py index 67fa01a..487fba8 100644 --- a/cached_property.py +++ b/cached_property.py @@ -14,7 +14,7 @@ asyncio = None -class cached_property(object): +class cached_property(property): """ A property that is only computed once per instance and then replaces itself with an ordinary attribute. Deleting the attribute resets the property. @@ -22,57 +22,61 @@ class cached_property(object): """ # noqa def __init__(self, func): - self.__doc__ = getattr(func, "__doc__") + self.value = self._sentinel = object() self.func = func def __get__(self, obj, cls): if obj is None: return self - if asyncio and asyncio.iscoroutinefunction(self.func): return self._wrap_in_coroutine(obj) + if self.value is self._sentinel: + self.value = self.func(obj) + return self.value - value = obj.__dict__[self.func.__name__] = self.func(obj) - return value + def __set__(self, obj, value): + self.value = value + + def __delete__(self, obj): + self.value = self._sentinel def _wrap_in_coroutine(self, obj): @asyncio.coroutine def wrapper(): - future = asyncio.ensure_future(self.func(obj)) - obj.__dict__[self.func.__name__] = future - return future + if self.value is self._sentinel: + self.value = asyncio.ensure_future(self.func(obj)) + return self.value return wrapper() -class threaded_cached_property(object): +class threaded_cached_property(cached_property): """ A cached_property version for use in environments where multiple threads might concurrently try to access the property. """ def __init__(self, func): - self.__doc__ = getattr(func, "__doc__") - self.func = func + super(threaded_cached_property, self).__init__(func) self.lock = threading.RLock() def __get__(self, obj, cls): if obj is None: return self - obj_dict = obj.__dict__ - name = self.func.__name__ with self.lock: - try: - # check if the value was computed before the lock was acquired - return obj_dict[name] + return super(threaded_cached_property, self).__get__(obj, cls) - except KeyError: - # if not, do the calculation and release the lock - return obj_dict.setdefault(name, self.func(obj)) + def __set__(self, obj, value): + with self.lock: + super(threaded_cached_property, self).__set__(obj, value) + + def __delete__(self, obj): + with self.lock: + super(threaded_cached_property, self).__delete__(obj) -class cached_property_with_ttl(object): +class cached_property_with_ttl(cached_property): """ A property that is only computed once per instance and then replaces itself with an ordinary attribute. Setting the ttl to a number expresses how long @@ -97,31 +101,20 @@ def __get__(self, obj, cls): return self now = time() - obj_dict = obj.__dict__ - name = self.__name__ - try: - value, last_updated = obj_dict[name] - except KeyError: - pass - else: - ttl_expired = self.ttl and self.ttl < now - last_updated - if not ttl_expired: + if self.value is not self._sentinel: + value, last_updated = self.value + if not self.ttl or self.ttl > now - last_updated: return value - value = self.func(obj) - obj_dict[name] = (value, now) + self.value = value, _ = (self.func(obj), now) return value - def __delete__(self, obj): - obj.__dict__.pop(self.__name__, None) - def __set__(self, obj, value): - obj.__dict__[self.__name__] = (value, time()) + super(cached_property_with_ttl, self).__set__(obj, (value, time())) def _prepare_func(self, func): - self.func = func + super(cached_property_with_ttl, self).__init__(func) if func: - self.__doc__ = func.__doc__ self.__name__ = func.__name__ self.__module__ = func.__module__ @@ -131,7 +124,7 @@ def _prepare_func(self, func): timed_cached_property = cached_property_with_ttl -class threaded_cached_property_with_ttl(cached_property_with_ttl): +class threaded_cached_property_with_ttl(cached_property_with_ttl, threaded_cached_property): """ A cached_property version for use in environments where multiple threads might concurrently try to access the property. @@ -145,6 +138,14 @@ def __get__(self, obj, cls): with self.lock: return super(threaded_cached_property_with_ttl, self).__get__(obj, cls) + def __set__(self, obj, value): + with self.lock: + return super(threaded_cached_property_with_ttl, self).__set__(obj, value) + + def __delete__(self, obj): + with self.lock: + return super(threaded_cached_property_with_ttl, self).__delete__(obj) + # Alias to make threaded_cached_property_with_ttl easier to use threaded_cached_property_ttl = threaded_cached_property_with_ttl From 63d91a20376b22f6f2ebfb3d02c26cee082aa25a Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Fri, 23 Nov 2018 22:18:12 +0100 Subject: [PATCH 02/33] Extract magic attributes from `cached_property.func` when needed --- cached_property.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/cached_property.py b/cached_property.py index 487fba8..db9ec86 100644 --- a/cached_property.py +++ b/cached_property.py @@ -34,6 +34,11 @@ def __get__(self, obj, cls): self.value = self.func(obj) return self.value + def __getattribute__(self, name): + if name in ('__doc__', '__name__', '__module__'): + return getattr(self.func, name) + return super(cached_property, self).__getattribute__(name) + def __set__(self, obj, value): self.value = value @@ -63,7 +68,6 @@ def __init__(self, func): def __get__(self, obj, cls): if obj is None: return self - with self.lock: return super(threaded_cached_property, self).__get__(obj, cls) @@ -90,10 +94,10 @@ def __init__(self, ttl=None): else: func = None self.ttl = ttl - self._prepare_func(func) + super(cached_property_with_ttl, self).__init__(func) def __call__(self, func): - self._prepare_func(func) + super(cached_property_with_ttl, self).__init__(func) return self def __get__(self, obj, cls): @@ -112,12 +116,6 @@ def __get__(self, obj, cls): def __set__(self, obj, value): super(cached_property_with_ttl, self).__set__(obj, (value, time())) - def _prepare_func(self, func): - super(cached_property_with_ttl, self).__init__(func) - if func: - self.__name__ = func.__name__ - self.__module__ = func.__module__ - # Aliases to make cached_property_with_ttl easier to use cached_property_ttl = cached_property_with_ttl From b30a434880d73c5728a8de0aea4556f83b14cfee Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Fri, 23 Nov 2018 22:29:02 +0100 Subject: [PATCH 03/33] Add self (@althonos) to AUTHORS.rst --- AUTHORS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index 4e5f411..eadff81 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -18,3 +18,4 @@ Contributors * Ionel Cristian Mărieș (@ionelmc) * Malyshev Artem (@proofit404) * Volker Braun (@vbraun) +* Martin Larralde (@althonos) From 5b2d0cc0fb6e36aeedcb7770cca0d916ee2885c6 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Fri, 23 Nov 2018 22:24:25 +0100 Subject: [PATCH 04/33] Use `functools.wraps` to extract attributes from wrapped function --- cached_property.py | 5 +++++ tests/test_cached_property.py | 8 ++++++++ 2 files changed, 13 insertions(+) diff --git a/cached_property.py b/cached_property.py index db9ec86..6a22033 100644 --- a/cached_property.py +++ b/cached_property.py @@ -6,6 +6,7 @@ __license__ = "BSD" from time import time +import functools import threading try: @@ -24,6 +25,10 @@ class cached_property(property): def __init__(self, func): self.value = self._sentinel = object() self.func = func + functools.wraps(func, self) + # self.__name__ = getattr(func, '__name__', None) + # self.__doc__ = getattr(func, '__doc__', None) + # self.__module__ = getattr(func, '__module__', None) def __get__(self, obj, cls): if obj is None: diff --git a/tests/test_cached_property.py b/tests/test_cached_property.py index 5082416..fa19563 100644 --- a/tests/test_cached_property.py +++ b/tests/test_cached_property.py @@ -27,6 +27,8 @@ def add_control(self): @cached_property_decorator def add_cached(self): + """A cached adder. + """ if threadsafe: time.sleep(1) # Need to guard this since += isn't atomic. @@ -68,6 +70,12 @@ def assert_cached(self, check, expected): self.assertEqual(check.add_cached, expected) self.assertEqual(check.cached_total, expected) + def test_magic_attributes(self): + Check = CheckFactory(self.cached_property_factory) + self.assertEqual(Check.add_cached.__doc__.strip(), "A cached adder.") + self.assertEqual(Check.add_cached.__name__.strip(), "add_cached") + self.assertEqual(Check.add_cached.__module__, __name__) + def test_cached_property(self): Check = CheckFactory(self.cached_property_factory) check = Check() From 47e20fe053ddc80e18d3080b42a2d298dce6e6d8 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Fri, 23 Nov 2018 22:28:06 +0100 Subject: [PATCH 05/33] Run `black` on `cached_property.py` --- cached_property.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cached_property.py b/cached_property.py index 6a22033..1838f8d 100644 --- a/cached_property.py +++ b/cached_property.py @@ -40,7 +40,7 @@ def __get__(self, obj, cls): return self.value def __getattribute__(self, name): - if name in ('__doc__', '__name__', '__module__'): + if name in ("__doc__", "__name__", "__module__"): return getattr(self.func, name) return super(cached_property, self).__getattribute__(name) @@ -127,7 +127,9 @@ def __set__(self, obj, value): timed_cached_property = cached_property_with_ttl -class threaded_cached_property_with_ttl(cached_property_with_ttl, threaded_cached_property): +class threaded_cached_property_with_ttl( + cached_property_with_ttl, threaded_cached_property +): """ A cached_property version for use in environments where multiple threads might concurrently try to access the property. From f63dc5026ce43c8a1884d3fefb73cad519bacb3e Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Fri, 23 Nov 2018 22:41:08 +0100 Subject: [PATCH 06/33] Implement `__set_name__` on `cached_property` objects --- cached_property.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/cached_property.py b/cached_property.py index 1838f8d..2a0f06f 100644 --- a/cached_property.py +++ b/cached_property.py @@ -5,9 +5,9 @@ __version__ = "1.5.1" __license__ = "BSD" -from time import time import functools import threading +from time import time try: import asyncio @@ -26,9 +26,6 @@ def __init__(self, func): self.value = self._sentinel = object() self.func = func functools.wraps(func, self) - # self.__name__ = getattr(func, '__name__', None) - # self.__doc__ = getattr(func, '__doc__', None) - # self.__module__ = getattr(func, '__module__', None) def __get__(self, obj, cls): if obj is None: @@ -39,10 +36,8 @@ def __get__(self, obj, cls): self.value = self.func(obj) return self.value - def __getattribute__(self, name): - if name in ("__doc__", "__name__", "__module__"): - return getattr(self.func, name) - return super(cached_property, self).__getattribute__(name) + def __set_name__(self, owner, name): + self.__name__ = name def __set__(self, obj, value): self.value = value From b97832a92ebe64d51209c1d88157fc1fbc09a234 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Fri, 23 Nov 2018 22:57:07 +0100 Subject: [PATCH 07/33] Fix `cached_property` sharing cache for all objects --- cached_property.py | 35 ++++++++++++++++++++++------------- tests/test_cached_property.py | 17 +++++++++++++++++ 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/cached_property.py b/cached_property.py index 2a0f06f..75aca08 100644 --- a/cached_property.py +++ b/cached_property.py @@ -7,6 +7,7 @@ import functools import threading +import weakref from time import time try: @@ -22,35 +23,43 @@ class cached_property(property): Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76 """ # noqa + _sentinel = object() + def __init__(self, func): - self.value = self._sentinel = object() + self.cache = weakref.WeakKeyDictionary() self.func = func - functools.wraps(func, self) + functools.update_wrapper(self, func) def __get__(self, obj, cls): if obj is None: return self + if asyncio and asyncio.iscoroutinefunction(self.func): return self._wrap_in_coroutine(obj) - if self.value is self._sentinel: - self.value = self.func(obj) - return self.value + + value = self.cache.get(obj, self._sentinel) + if value is self._sentinel: + value = self.cache[obj] = self.func(obj) + + return value def __set_name__(self, owner, name): self.__name__ = name def __set__(self, obj, value): - self.value = value + self.cache[obj] = value def __delete__(self, obj): - self.value = self._sentinel + del self.cache[obj] def _wrap_in_coroutine(self, obj): + @asyncio.coroutine def wrapper(): - if self.value is self._sentinel: - self.value = asyncio.ensure_future(self.func(obj)) - return self.value + value = self.cache.get(obj, self._sentinel) + if value is self._sentinel: + self.cache[obj] = value = asyncio.ensure_future(self.func(obj)) + return value return wrapper() @@ -105,12 +114,12 @@ def __get__(self, obj, cls): return self now = time() - if self.value is not self._sentinel: - value, last_updated = self.value + if obj in self.cache: + value, last_updated = self.cache[obj] if not self.ttl or self.ttl > now - last_updated: return value - self.value = value, _ = (self.func(obj), now) + value, _ = self.cache[obj] = (self.func(obj), now) return value def __set__(self, obj, value): diff --git a/tests/test_cached_property.py b/tests/test_cached_property.py index fa19563..73d87d7 100644 --- a/tests/test_cached_property.py +++ b/tests/test_cached_property.py @@ -151,6 +151,23 @@ def test_threads(self): self.assert_cached(check, num_threads) self.assert_cached(check, num_threads) + def test_object_independent(self): + Check = CheckFactory(self.cached_property_factory) + check1 = Check() + check2 = Check() + + self.assert_cached(check1, 1) + self.assert_cached(check1, 1) + self.assert_cached(check2, 1) + self.assert_cached(check2, 1) + + del check1.add_cached + + self.assert_cached(check1, 2) + self.assert_cached(check1, 2) + self.assert_cached(check2, 1) + self.assert_cached(check2, 1) + class TestThreadedCachedProperty(TestCachedProperty): """Tests for threaded_cached_property""" From e5779507f554cdbb8ca1d06935b8654dd0db9b7c Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Fri, 23 Nov 2018 23:09:42 +0100 Subject: [PATCH 08/33] Add @VadimPushtaev to AUTHORS.rst --- AUTHORS.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.rst b/AUTHORS.rst index eadff81..388dc59 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -18,4 +18,5 @@ Contributors * Ionel Cristian Mărieș (@ionelmc) * Malyshev Artem (@proofit404) * Volker Braun (@vbraun) +* Vadim Pushtaev (@VadimPushtaev) * Martin Larralde (@althonos) From 2c2e11d44d491ccf03f8aae42667c2ba8df27a34 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Fri, 23 Nov 2018 23:21:53 +0100 Subject: [PATCH 09/33] Fix `update_wrapper` failing on missing attributes in Py2 --- cached_property.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/cached_property.py b/cached_property.py index 75aca08..338c797 100644 --- a/cached_property.py +++ b/cached_property.py @@ -6,6 +6,7 @@ __license__ = "BSD" import functools +import sys import threading import weakref from time import time @@ -25,10 +26,21 @@ class cached_property(property): _sentinel = object() + if sys.version_info[0] < 3: + + def _update_wrapper(self, func): + self.__doc__ = getattr(func, "__doc__", None) + self.__module__ = getattr(func, "__module__", None) + self.__name__ = getattr(func, "__name__", None) + + else: + + _update_wrapper = functools.update_wrapper + def __init__(self, func): self.cache = weakref.WeakKeyDictionary() self.func = func - functools.update_wrapper(self, func) + self._update_wrapper(func) def __get__(self, obj, cls): if obj is None: @@ -53,7 +65,7 @@ def __delete__(self, obj): del self.cache[obj] def _wrap_in_coroutine(self, obj): - + @asyncio.coroutine def wrapper(): value = self.cache.get(obj, self._sentinel) From c2b6182a1bcdb0d70580ad98650def84d3d66214 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Sat, 24 Nov 2018 00:03:29 +0100 Subject: [PATCH 10/33] Add test to make sure the property cache is freed as expected --- tests/test_cached_property.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/tests/test_cached_property.py b/tests/test_cached_property.py index 73d87d7..791cddd 100644 --- a/tests/test_cached_property.py +++ b/tests/test_cached_property.py @@ -151,6 +151,18 @@ def test_threads(self): self.assert_cached(check, num_threads) self.assert_cached(check, num_threads) + def test_garbage_collection(self): + Check = CheckFactory(self.cached_property_factory) + check = Check() + check.add_cached = "foo" + + # check the instance is in the cache + self.assertIn(check, Check.add_cached.cache) + # remove the only reference to the Check instance + del check + # make sure the cache of the deleted object was removed + self.assertEqual(Check.add_cached.cache, {}) + def test_object_independent(self): Check = CheckFactory(self.cached_property_factory) check1 = Check() From 2fd46da3b9da6c593ebf0f51595f2c98e038c259 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Sat, 24 Nov 2018 00:16:20 +0100 Subject: [PATCH 11/33] Use `len` to check cache emptiness in `tests.test_cached_property` --- tests/test_cached_property.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cached_property.py b/tests/test_cached_property.py index 791cddd..11fc7ed 100644 --- a/tests/test_cached_property.py +++ b/tests/test_cached_property.py @@ -161,7 +161,7 @@ def test_garbage_collection(self): # remove the only reference to the Check instance del check # make sure the cache of the deleted object was removed - self.assertEqual(Check.add_cached.cache, {}) + self.assertEqual(len(Check.add_cached.cache), 0) def test_object_independent(self): Check = CheckFactory(self.cached_property_factory) From 55763c14ee62972042d728e95b3d51f117b0d878 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Sat, 24 Nov 2018 00:20:55 +0100 Subject: [PATCH 12/33] Skip `test_garbage_collection` in implementations other than CPython --- tests/test_cached_property.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_cached_property.py b/tests/test_cached_property.py index 11fc7ed..a448d30 100644 --- a/tests/test_cached_property.py +++ b/tests/test_cached_property.py @@ -1,5 +1,6 @@ # -*- coding: utf-8 -*- +import platform import time import unittest from threading import Lock, Thread @@ -151,6 +152,8 @@ def test_threads(self): self.assert_cached(check, num_threads) self.assert_cached(check, num_threads) + @unittest.skipUnless(platform.python_implementation() == "CPython", + "unknow garbage collection mechanism") def test_garbage_collection(self): Check = CheckFactory(self.cached_property_factory) check = Check() @@ -161,7 +164,7 @@ def test_garbage_collection(self): # remove the only reference to the Check instance del check # make sure the cache of the deleted object was removed - self.assertEqual(len(Check.add_cached.cache), 0) + self.assertEqual(Check.add_cached.cache, {}) def test_object_independent(self): Check = CheckFactory(self.cached_property_factory) From 84f2515697135247a28fc6f694a350bc28817abc Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Sat, 20 Jul 2019 22:31:23 +0000 Subject: [PATCH 13/33] Rename to `property-cached` to release fork --- LICENSE | 3 ++- cached_property.py => property_cached.py | 0 setup.py | 12 ++++++------ tests/test_async_cached_property.py | 2 +- tests/test_cached_property.py | 2 +- tests/test_coroutine_cached_property.py | 2 +- 6 files changed, 11 insertions(+), 10 deletions(-) rename cached_property.py => property_cached.py (100%) diff --git a/LICENSE b/LICENSE index a181761..aac9837 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,5 @@ -Copyright (c) 2015, Daniel Greenfeld +Copyright (c) 2018-2019, Martin Larralde +Copyright (c) 2015-2018, Daniel Greenfeld All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: diff --git a/cached_property.py b/property_cached.py similarity index 100% rename from cached_property.py rename to property_cached.py diff --git a/setup.py b/setup.py index af68a07..37dc940 100755 --- a/setup.py +++ b/setup.py @@ -30,18 +30,18 @@ def read(fname): sys.exit() setup( - name="cached-property", + name="property-cached", version=__version__, - description="A decorator for caching properties in classes.", + description="A decorator for caching properties in classes (forked from cached-property).", long_description=readme + "\n\n" + history, - author="Daniel Greenfeld", - author_email="pydanny@gmail.com", - url="https://github.com/pydanny/cached-property", + author="Martin Larralde", + author_email="martin.larralde@ens-paris-saclay.fr", + url="https://github.com/althonos/property-cached", py_modules=["cached_property"], include_package_data=True, license="BSD", zip_safe=False, - keywords="cached-property", + keywords=["cached-property"], classifiers=[ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", diff --git a/tests/test_async_cached_property.py b/tests/test_async_cached_property.py index f4f93c7..1af139d 100644 --- a/tests/test_async_cached_property.py +++ b/tests/test_async_cached_property.py @@ -4,7 +4,7 @@ import unittest from threading import Lock, Thread from freezegun import freeze_time -import cached_property +import property_cached as cached_property def unittest_run_loop(f): diff --git a/tests/test_cached_property.py b/tests/test_cached_property.py index a448d30..ff5f297 100644 --- a/tests/test_cached_property.py +++ b/tests/test_cached_property.py @@ -6,7 +6,7 @@ from threading import Lock, Thread from freezegun import freeze_time -import cached_property +import property_cached as _property def CheckFactory(cached_property_decorator, threadsafe=False): diff --git a/tests/test_coroutine_cached_property.py b/tests/test_coroutine_cached_property.py index 88790a0..9dfa792 100644 --- a/tests/test_coroutine_cached_property.py +++ b/tests/test_coroutine_cached_property.py @@ -9,7 +9,7 @@ import asyncio from freezegun import freeze_time -import cached_property +import property_cached as cached_property def unittest_run_loop(f): From 3ff44f1ff36ce7de72345b3f7e6f1558165c226c Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Sat, 20 Jul 2019 22:38:37 +0000 Subject: [PATCH 14/33] Start reorganizing code --- Makefile | 57 ------------------- conftest.py | 20 ------- .../__init__.py | 8 +-- property_cached/_version.txt | 1 + tests/test_coroutine_cached_property.py | 2 +- tox.ini | 10 ---- 6 files changed, 6 insertions(+), 92 deletions(-) delete mode 100644 Makefile delete mode 100644 conftest.py rename property_cached.py => property_cached/__init__.py (98%) create mode 100644 property_cached/_version.txt delete mode 100644 tox.ini diff --git a/Makefile b/Makefile deleted file mode 100644 index 2446d51..0000000 --- a/Makefile +++ /dev/null @@ -1,57 +0,0 @@ -.PHONY: clean-pyc clean-build docs clean - -help: - @echo "clean-build - remove build artifacts" - @echo "clean-pyc - remove Python file artifacts" - @echo "lint - check style with flake8" - @echo "test - run tests quickly with the default Python" - @echo "test-all - run tests on every Python version with tox" - @echo "coverage - check code coverage quickly with the default Python" - @echo "docs - generate Sphinx HTML documentation, including API docs" - @echo "release - package and upload a release" - @echo "dist - package" - -clean: clean-build clean-pyc - rm -fr htmlcov/ - -clean-build: - rm -fr build/ - rm -fr dist/ - rm -fr *.egg-info - -clean-pyc: - find . -name '*.pyc' -exec rm -f {} + - find . -name '*.pyo' -exec rm -f {} + - find . -name '*~' -exec rm -f {} + - -lint: - flake8 cached_property.py tests - -test: - py.test - -test-all: - tox - -coverage: - py.test --cov cached_property - coverage report -m - coverage html - open htmlcov/index.html - -docs: - rm -f docs/cached-property.rst - rm -f docs/modules.rst - sphinx-apidoc -o docs/ cached-property - $(MAKE) -C docs clean - $(MAKE) -C docs html - open docs/_build/html/index.html - -release: clean - python setup.py sdist upload - python setup.py bdist_wheel upload - -dist: clean - python setup.py sdist - python setup.py bdist_wheel - ls -l dist \ No newline at end of file diff --git a/conftest.py b/conftest.py deleted file mode 100644 index 0563f64..0000000 --- a/conftest.py +++ /dev/null @@ -1,20 +0,0 @@ - -import sys - -# Whether "import asyncio" works -has_asyncio = sys.version_info[0] == 3 and sys.version_info[1] >= 4 - -# Whether the async and await keywords work -has_async_await = sys.version_info[0] == 3 and sys.version_info[1] >= 5 - - -print("conftest.py", has_asyncio, has_async_await) - - -collect_ignore = [] - -if not has_asyncio: - collect_ignore.append("tests/test_coroutine_cached_property.py") - -if not has_async_await: - collect_ignore.append("tests/test_async_cached_property.py") diff --git a/property_cached.py b/property_cached/__init__.py similarity index 98% rename from property_cached.py rename to property_cached/__init__.py index 338c797..c891e98 100644 --- a/property_cached.py +++ b/property_cached/__init__.py @@ -1,9 +1,9 @@ # -*- coding: utf-8 -*- -__author__ = "Daniel Greenfeld" -__email__ = "pydanny@gmail.com" -__version__ = "1.5.1" +__author__ = "Martin Larralde" +__email__ = "martin.larralde@ens-paris-saclay.fr" __license__ = "BSD" +__version__ = "1.5.1" import functools import sys @@ -65,7 +65,7 @@ def __delete__(self, obj): del self.cache[obj] def _wrap_in_coroutine(self, obj): - + @asyncio.coroutine def wrapper(): value = self.cache.get(obj, self._sentinel) diff --git a/property_cached/_version.txt b/property_cached/_version.txt new file mode 100644 index 0000000..dc1e644 --- /dev/null +++ b/property_cached/_version.txt @@ -0,0 +1 @@ +1.6.0 diff --git a/tests/test_coroutine_cached_property.py b/tests/test_coroutine_cached_property.py index 9dfa792..40e443b 100644 --- a/tests/test_coroutine_cached_property.py +++ b/tests/test_coroutine_cached_property.py @@ -9,7 +9,7 @@ import asyncio from freezegun import freeze_time -import property_cached as cached_property +import property_cached as cached_property def unittest_run_loop(f): diff --git a/tox.ini b/tox.ini deleted file mode 100644 index 9d7c8b3..0000000 --- a/tox.ini +++ /dev/null @@ -1,10 +0,0 @@ -[tox] -envlist = py27, py34, py35, py36, py37 - -[testenv] -setenv = - PYTHONPATH = {toxinidir}:{toxinidir}/cached-property -commands = pytest tests/ -deps = - pytest - freezegun From c59cbeb2f207a93dd47a435a3a2434f8dba42290 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Sat, 20 Jul 2019 22:39:09 +0000 Subject: [PATCH 15/33] Generate new `.gitignore` file using `gitignore.io` --- .gitignore | 142 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 110 insertions(+), 32 deletions(-) diff --git a/.gitignore b/.gitignore index b0df7f7..ac7a9b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,53 +1,131 @@ + +# Created by https://www.gitignore.io/api/python +# Edit at https://www.gitignore.io/?templates=python + +### Python ### +# Byte-compiled / optimized / DLL files +__pycache__/ *.py[cod] +*$py.class # C extensions *.so -# Packages -*.egg -*.egg-info -dist -build -eggs -parts -bin -var -sdist -develop-eggs +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +pip-wheel-metadata/ +share/python-wheels/ +*.egg-info/ .installed.cfg -lib -lib64 +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec # Installer logs pip-log.txt +pip-delete-this-directory.txt # Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ .coverage -.tox -nosetests.xml -htmlcov +.coverage.* .cache -.pytest_cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ # Translations *.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject -# Mr Developer -.mr.developer.cfg -.project -.pydevproject +# Rope project settings +.ropeproject -# Complexity -output/*.html -output/*/index.html +# mkdocs documentation +/site -# Sphinx -docs/_build +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json -# IntelliJ IDEA -/.idea/ -/out/ -*.iml +# Pyre type checker +.pyre/ -# Local virtualenv directories -/venv*/ \ No newline at end of file +# End of https://www.gitignore.io/api/python From c02be3a4481741850f9eca9a082acd9826f99820 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Sat, 20 Jul 2019 22:39:33 +0000 Subject: [PATCH 16/33] Move test requirements file to `tests` folder --- requirements.txt => tests/requirements.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename requirements.txt => tests/requirements.txt (100%) diff --git a/requirements.txt b/tests/requirements.txt similarity index 100% rename from requirements.txt rename to tests/requirements.txt From ed6e2851608aa1812f5e36c660add0d82ed9cef3 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Sat, 20 Jul 2019 22:51:11 +0000 Subject: [PATCH 17/33] Update reference to `property-cached` in documentation --- CONTRIBUTING.rst | 33 +++++++++++++------------- LICENSE | 2 +- README.rst | 44 +++++++++++++++++------------------ setup.py | 10 +------- tests/test_cached_property.py | 2 +- 5 files changed, 40 insertions(+), 51 deletions(-) diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 7451fcb..0bf6190 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -3,7 +3,7 @@ Contributing ============ Contributions are welcome, and they are greatly appreciated! Every -little bit helps, and credit will always be given. +little bit helps, and credit will always be given. You can contribute in many ways: @@ -13,7 +13,7 @@ Types of Contributions Report Bugs ~~~~~~~~~~~ -Report bugs at https://github.com/pydanny/cached-property/issues. +Report bugs at https://github.com/althonos/property-cached/issues. If you are reporting a bug, please include: @@ -36,14 +36,14 @@ is open to whoever wants to implement it. Write Documentation ~~~~~~~~~~~~~~~~~~~ -cached-property could always use more documentation, whether as part of the +property-cached could always use more documentation, whether as part of the official cached-property docs, in docstrings, or even on the web in blog posts, articles, and such. Submit Feedback ~~~~~~~~~~~~~~~ -The best way to send feedback is to file an issue at https://github.com/pydanny/cached-property/issues. +The best way to send feedback is to file an issue at https://github.com/althonos/property-cached/issues. If you are proposing a feature: @@ -55,37 +55,37 @@ If you are proposing a feature: Get Started! ------------ -Ready to contribute? Here's how to set up `cached-property` for local development. +Ready to contribute? Here's how to set up ``property-cached`` for local development. -1. Fork the `cached-property` repo on GitHub. +1. Fork the ``property-cached`` repo on GitHub. 2. Clone your fork locally:: - $ git clone git@github.com:your_name_here/cached-property.git + $ git clone git@github.com:your_name_here/property-cached.git 3. Install your local copy into a virtualenv. Assuming you have virtualenvwrapper installed, this is how you set up your fork for local development:: - $ mkvirtualenv cached-property - $ cd cached-property/ + $ mkvirtualenv property-cached + $ cd property-cached/ $ python setup.py develop 4. Create a branch for local development:: $ git checkout -b name-of-your-bugfix-or-feature - + Now you can make your changes locally. - + 5. Clean up the formatting (must be running at least Python 3.6):: - + $ pip install -U black $ black . - + 6. When you're done making changes, check that your changes pass the tests, including testing other Python versions with tox:: $ pytest tests/ $ tox - To get tox, just pip install it into your virtualenv. + To get tox, just pip install it into your virtualenv. 7. Commit your changes and push your branch to GitHub:: @@ -104,8 +104,8 @@ Before you submit a pull request, check that it meets these guidelines: 2. If the pull request adds functionality, the docs should be updated. Put your new functionality into a function with a docstring, and add the feature to the list in README.rst. -3. The pull request should work for Python 2.7, and 3.3, 3.4, 3.5, 3.6 and for PyPy. Check - https://travis-ci.org/pydanny/cached-property/pull_requests +3. The pull request should work for Python 2.7, and 3.3, 3.4, 3.5, 3.6 and for PyPy. Check + https://travis-ci.org/althonos/property-cached/pull_requests and make sure that the tests pass for all supported Python versions. Tips @@ -114,4 +114,3 @@ Tips To run a subset of tests:: $ python -m unittest tests.test_cached-property - diff --git a/LICENSE b/LICENSE index aac9837..a21d57b 100644 --- a/LICENSE +++ b/LICENSE @@ -8,6 +8,6 @@ Redistribution and use in source and binary forms, with or without modification, * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. -* Neither the name of cached-property nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. +* Neither the name of cached-property or property-cached nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.rst b/README.rst index 9c57d00..33c523c 100644 --- a/README.rst +++ b/README.rst @@ -1,19 +1,27 @@ =============================== -cached-property +property-cached =============================== -.. image:: https://img.shields.io/pypi/v/cached-property.svg - :target: https://pypi.python.org/pypi/cached-property +.. image:: https://img.shields.io/pypi/v/property-cached.svg&style=flat-square + :target: https://pypi.python.org/pypi/property-cached -.. image:: https://img.shields.io/travis/pydanny/cached-property/master.svg - :target: https://travis-ci.org/pydanny/cached-property - -.. image:: https://img.shields.io/badge/code%20style-black-000000.svg +.. image:: https://img.shields.io/travis/althonos/property-cached/master.svg&style=flat-square + :target: https://travis-ci.org/althonos/property-cached + +.. image:: https://img.shields.io/badge/code%20style-black-000000.svg&style=flat-square :target: https://github.com/ambv/black - :alt: Code style: black + :alt: Code style: black + + +A decorator for caching properties in classes (forked from ``cached-property``). +This library was forked from the upstream library ``cached-property`` since its +developer does not seem to be maintaining it anymore. It works as a drop-in +replacement with fully compatible API (import ``property_cached`` instead of +``cached_property`` in your code and *voilà*). In case development resumes on +the original library, this one is likely to be deprecated. -A decorator for caching properties in classes. +*Original ``README`` included below:* Why? ----- @@ -232,22 +240,12 @@ is why they are broken out into seperate tools. See https://github.com/pydanny/c Credits -------- +* ``@pydanny`` for the original ``cached-property`` implementation. * Pip, Django, Werkzueg, Bottle, Pyramid, and Zope for having their own implementations. This package originally used an implementation that matched the Bottle version. * Reinout Van Rees for pointing out the `cached_property` decorator to me. -* My awesome wife `@audreyr`_ who created `cookiecutter`_, which meant rolling this out took me just 15 minutes. -* @tinche for pointing out the threading issue and providing a solution. -* @bcho for providing the time-to-expire feature +* ``@audreyr``_ who created ``cookiecutter``_, which meant rolling this out took ``@pydanny`` just 15 minutes. +* ``@tinche`` for pointing out the threading issue and providing a solution. +* ``@bcho`` for providing the time-to-expire feature .. _`@audreyr`: https://github.com/audreyr .. _`cookiecutter`: https://github.com/audreyr/cookiecutter - -Support This Project ---------------------------- - -This project is maintained by volunteers. Support their efforts by spreading the word about: - -.. image:: https://cdn.shopify.com/s/files/1/0304/6901/t/2/assets/logo.png?8399580890922549623 - :name: Two Scoops Press - :align: center - :alt: Two Scoops Press - :target: https://www.twoscoopspress.com diff --git a/setup.py b/setup.py index 37dc940..69770d9 100755 --- a/setup.py +++ b/setup.py @@ -18,22 +18,14 @@ def read(fname): os.path.join(os.path.dirname(__file__), fname), "r", "utf-8" ).read() - readme = read("README.rst") history = read("HISTORY.rst").replace(".. :changelog:", "") -if sys.argv[-1] == "publish": - os.system("python setup.py sdist bdist_wheel") - os.system("twine upload dist/*") - os.system("git tag -a %s -m 'version %s'" % (__version__, __version__)) - os.system("git push --tags") - sys.exit() - setup( name="property-cached", version=__version__, description="A decorator for caching properties in classes (forked from cached-property).", - long_description=readme + "\n\n" + history, + long_description="\n\n".join([readme, history]), author="Martin Larralde", author_email="martin.larralde@ens-paris-saclay.fr", url="https://github.com/althonos/property-cached", diff --git a/tests/test_cached_property.py b/tests/test_cached_property.py index ff5f297..8351807 100644 --- a/tests/test_cached_property.py +++ b/tests/test_cached_property.py @@ -6,7 +6,7 @@ from threading import Lock, Thread from freezegun import freeze_time -import property_cached as _property +import property_cached as cached_property def CheckFactory(cached_property_decorator, threadsafe=False): From e261a67bf290aba6dc4f2398e6ccc482b1ebf4b8 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Mon, 22 Jul 2019 08:56:06 +0000 Subject: [PATCH 18/33] Use `setup.cfg` for build configuration instead of `setup.py` --- .travis.yml | 32 ++++++----- AUTHORS.rst | 5 ++ HISTORY.rst => CHANGELOG.rst | 0 CONTRIBUTING.rst | 2 +- MANIFEST.in | 2 + README.rst | 15 +++--- property_cached/__init__.py | 12 +++-- setup.cfg | 101 ++++++++++++++++++++++++++++++++++- setup.py | 49 +---------------- 9 files changed, 138 insertions(+), 80 deletions(-) rename HISTORY.rst => CHANGELOG.rst (100%) diff --git a/.travis.yml b/.travis.yml index 1cf7fa4..785dc3f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,23 +1,29 @@ # Config file for automatic testing at travis-ci.org sudo: false +dist: xenial language: python -env: - global: - - CC_TEST_REPORTER_ID=56a0691204179e8edcdde4c7ecab73b3693706aa1186ed018ca78acc663520cf +python: + - 2.7 + - 3.4 + - 3.5 + - 3.6 + - 3.7 before_script: # code coverage tool - - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter - - chmod +x ./cc-test-reporter - - ./cc-test-reporter before-build + - pip install -r tests/requirements.txt + +install: + - pip install . # command to run tests and save coverage script: - - py.test --cov cached_property + - python -m coverage run -m unittest discover -v after_script: - - coverage report -m - - coverage xml + - python -m coverage report -m + - python -m coverage xml + - ./cc-test-reporter format-coverage --input-type coverage.py --debug - ./cc-test-reporter upload-coverage --debug python: @@ -27,11 +33,3 @@ python: - "2.7" - "pypy" -matrix: - include: - - python: 3.7 - dist: xenial - sudo: true - -# command to install dependencies, e.g. pip install -r requirements.txt --use-mirrors -install: pip install -r requirements.txt diff --git a/AUTHORS.rst b/AUTHORS.rst index 388dc59..75fc596 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -8,6 +8,11 @@ Development Lead * Daniel Roy Greenfeld (@pydanny) * Audrey Roy Greenfeld (@audreyr) +Maintainer +---------- + +* Martin Larralde (@althonos) + Contributors ------------ diff --git a/HISTORY.rst b/CHANGELOG.rst similarity index 100% rename from HISTORY.rst rename to CHANGELOG.rst diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 0bf6190..4c364a5 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -37,7 +37,7 @@ Write Documentation ~~~~~~~~~~~~~~~~~~~ property-cached could always use more documentation, whether as part of the -official cached-property docs, in docstrings, or even on the web in blog posts, +official property-cached docs, in docstrings, or even on the web in blog posts, articles, and such. Submit Feedback diff --git a/MANIFEST.in b/MANIFEST.in index 7e355c4..97e7701 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -4,6 +4,8 @@ include HISTORY.rst include LICENSE include README.rst +recursive-include * _version.txt + recursive-include tests * recursive-exclude * __pycache__ recursive-exclude * *.py[co] diff --git a/README.rst b/README.rst index 33c523c..ffd160c 100644 --- a/README.rst +++ b/README.rst @@ -2,15 +2,14 @@ property-cached =============================== -.. image:: https://img.shields.io/pypi/v/property-cached.svg&style=flat-square - :target: https://pypi.python.org/pypi/property-cached +.. image:: https://img.shields.io/pypi/v/property-cached.svg?style=flat-square + :target: https://pypi.python.org/pypi/property-cached -.. image:: https://img.shields.io/travis/althonos/property-cached/master.svg&style=flat-square - :target: https://travis-ci.org/althonos/property-cached +.. image:: https://img.shields.io/travis/althonos/property-cached/master.svg?style=flat-square + :target: https://travis-ci.org/althonos/property-cached -.. image:: https://img.shields.io/badge/code%20style-black-000000.svg&style=flat-square - :target: https://github.com/ambv/black - :alt: Code style: black +.. image:: https://img.shields.io/badge/code%20style-black-000000.svg?style=flat-square + :target: https://github.com/ambv/black A decorator for caching properties in classes (forked from ``cached-property``). @@ -21,7 +20,7 @@ replacement with fully compatible API (import ``property_cached`` instead of ``cached_property`` in your code and *voilà*). In case development resumes on the original library, this one is likely to be deprecated. -*Original ``README`` included below:* +*Original readme included below:* Why? ----- diff --git a/property_cached/__init__.py b/property_cached/__init__.py index c891e98..0cbb1bd 100644 --- a/property_cached/__init__.py +++ b/property_cached/__init__.py @@ -1,11 +1,7 @@ # -*- coding: utf-8 -*- -__author__ = "Martin Larralde" -__email__ = "martin.larralde@ens-paris-saclay.fr" -__license__ = "BSD" -__version__ = "1.5.1" - import functools +import pkg_resources import sys import threading import weakref @@ -17,6 +13,12 @@ asyncio = None +__author__ = "Martin Larralde" +__email__ = "martin.larralde@ens-paris-saclay.fr" +__license__ = "BSD" +__version__ = pkg_resources.resource_string(__name__, "_version.txt").decode('utf-8').strip() + + class cached_property(property): """ A property that is only computed once per instance and then replaces itself diff --git a/setup.cfg b/setup.cfg index 0a8df87..fea63bd 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,2 +1,99 @@ -[wheel] -universal = 1 \ No newline at end of file +[metadata] +name = property-cached +version = file: property_cached/_version.txt +author = Martin Larralde +author-email = martin.larralde@ens-paris-saclay.fr +home-page = https://github.com/althonos/property-cached/ +description = A decorator for caching properties in classes (forked from cached-property). +long-description = file: README.rst, CHANGELOG.rst +license = BSD +license-file = LICENSE +platform = any +keywords = cached-property, cache, property +classifiers = + Development Status :: 5 - Production/Stable + Intended Audience :: Developers + License :: OSI Approved :: BSD License + Natural Language :: English + Operating System :: OS Independent + Programming Language :: Python + Programming Language :: Python :: 2 + Programming Language :: Python :: 2.7 + Programming Language :: Python :: 3 + Programming Language :: Python :: 3.4 + Programming Language :: Python :: 3.5 + Programming Language :: Python :: 3.6 + Programming Language :: Python :: 3.7 + Topic :: Scientific/Engineering :: Bio-Informatics +project_urls = + Bug Reports = https://github.com/althonos/moclo/issues + +[options] +zip_safe = true +include_package_data = true +python_requires = >= 2.7, != 3.0.*, != 3.1.*, != 3.2.*, != 3.3.* +packages = property_cached +test_suite = tests +setup_requires = + setuptools >=39.2 +install_requires = + biopython ~=1.71 + cached-property ~=1.4 + fs ~=2.1 + six ~=1.10 + typing ~=3.6 ; python_version < '3.6' + bz2file ~=0.98 ; python_version < '3' + +[bdist_wheel] +universal = 1 + +[check] +metadata = true +restructuredtext = true +strict = true + +[sdist] +formats = zip + +[coverage:report] +show_missing = true +exclude_lines = + pragma: no cover + if typing.TYPE_CHECKING: + @abc.abstractmethod + @abc.abstractproperty + raise NotImplementedError + return NotImplemented + +[green] +file-pattern = test_*.py +verbose = 2 +no-skip-report = true +quiet-stdout = true +run-coverage = true + +[pydocstyle] +match-dir = (?!tests)(?!resources)(?!docs)[^\.].* +match = (?!test)(?!setup)[^\._].*\.py +inherit = false +ignore = D200, D203, D213, D406, D407 # Google conventions + +[flake8] +max-line-length = 99 +doctests = True +exclude = .git, .eggs, __pycache__, tests/, docs/, build/, dist/ +ignore = D200, D203, D213, D406, D407 # Google conventions + +[mypy] +ignore_missing_imports = true + +[mypy-moclo.*] +disallow_any_decorated = false +disallow_any_generics = false +disallow_any_unimported = true +disallow_subclassing_any = true +disallow_untyped_calls = false +disallow_untyped_defs = false +ignore_missing_imports = false +warn_unused_ignores = false +warn_return_any = false diff --git a/setup.py b/setup.py index 69770d9..18d7a07 100755 --- a/setup.py +++ b/setup.py @@ -1,50 +1,5 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import os -import sys -import codecs - -try: - from setuptools import setup -except ImportError: - from distutils.core import setup - -__version__ = "1.5.1" - - -def read(fname): - return codecs.open( - os.path.join(os.path.dirname(__file__), fname), "r", "utf-8" - ).read() - -readme = read("README.rst") -history = read("HISTORY.rst").replace(".. :changelog:", "") - -setup( - name="property-cached", - version=__version__, - description="A decorator for caching properties in classes (forked from cached-property).", - long_description="\n\n".join([readme, history]), - author="Martin Larralde", - author_email="martin.larralde@ens-paris-saclay.fr", - url="https://github.com/althonos/property-cached", - py_modules=["cached_property"], - include_package_data=True, - license="BSD", - zip_safe=False, - keywords=["cached-property"], - classifiers=[ - "Development Status :: 5 - Production/Stable", - "Intended Audience :: Developers", - "License :: OSI Approved :: BSD License", - "Natural Language :: English", - "Programming Language :: Python :: 2", - "Programming Language :: Python :: 2.7", - "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.4", - "Programming Language :: Python :: 3.5", - "Programming Language :: Python :: 3.6", - "Programming Language :: Python :: 3.7", - ], -) +import setuptools +setuptools.setup() From 142d962a926eb9fbd731e6b84b53f879fe0e2380 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Mon, 22 Jul 2019 09:10:31 +0000 Subject: [PATCH 19/33] Switch to `green` to run tests in Travis-CI --- .travis.yml | 44 +++++++++++++++++++++++------------------- tests/requirements.txt | 5 ++--- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/.travis.yml b/.travis.yml index 785dc3f..50daa5e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,33 +3,37 @@ sudo: false dist: xenial language: python -python: - - 2.7 - - 3.4 - - 3.5 - - 3.6 - - 3.7 +git: + depth: false -before_script: # code coverage tool +matrix: + include: + - python: pypy + env: TESTS=tests.test_cached_property + - python: 2.7 + env: TESTS=tests.test_cached_property + - python: 3.4 + env: TESTS=tests.test_cached_property + - python: 3.5 + env: TESTS=tests.test_cached_property + - python: 3.6 + env: TESTS=tests + - python: 3.7 + env: TESTS=tests + +before_script: - pip install -r tests/requirements.txt install: - pip install . -# command to run tests and save coverage script: - - python -m coverage run -m unittest discover -v + - green $TESTS -after_script: - - python -m coverage report -m +after_success: - python -m coverage xml + - bash <(curl -s https://codecov.io/bash) - - ./cc-test-reporter format-coverage --input-type coverage.py --debug - - ./cc-test-reporter upload-coverage --debug -python: - - "3.6" - - "3.5" - - "3.4" - - "2.7" - - "pypy" - +notifications: + email: + - althonosdev@gmail.com diff --git a/tests/requirements.txt b/tests/requirements.txt index 1433eb3..b89fb99 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -1,7 +1,6 @@ # Testing and deployment packages. coverage==4.4.2 -pytest==3.8.2 -pytest-cov==2.6.0 freezegun==0.3.10 twine==1.12.1 -wheel==0.32.0 \ No newline at end of file +wheel==0.32.0 +green==2.16.1 From 65cc8df9e23300f1944cb1b4760b31705d927db9 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Mon, 22 Jul 2019 09:15:48 +0000 Subject: [PATCH 20/33] Setup deployment from Travis-CI using `twine` --- .travis.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.travis.yml b/.travis.yml index 50daa5e..7e5e2b8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -34,6 +34,19 @@ after_success: - python -m coverage xml - bash <(curl -s https://codecov.io/bash) +before_deploy: + - python setup.py sdist bdist_wheel + - twine check dist/* + +deploy: + provider: script + on: + python: 3.7 + tags: true + repo: althonos/property-cached + skip_cleanup: true + script: twine upload --skip-existing dist/* + notifications: email: - althonosdev@gmail.com From 45cecff59ba1e9385e7c213e0ad567cfe7badf5f Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Mon, 22 Jul 2019 09:19:14 +0000 Subject: [PATCH 21/33] Release v1.6.0 --- CHANGELOG.rst | 9 +++++++++ README.rst | 9 ++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index e7dbf47..3879104 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,6 +3,15 @@ History ------- +1.6.0 (2019-07-22) +++++++++++++++++++ + +* Fixed class hierarchy, ``cached_property`` now inherits from ``property`` +* Add support for slotted classes and stop using the object ``__dict__``. +* Improve function wrapping using ``functools.update_wrapper``. +* Implement the ``__set_name__`` magic method available since Python 3.6. + + 1.5.1 (2018-08-05) ++++++++++++++++++ diff --git a/README.rst b/README.rst index ffd160c..fe666a9 100644 --- a/README.rst +++ b/README.rst @@ -2,12 +2,15 @@ property-cached =============================== -.. image:: https://img.shields.io/pypi/v/property-cached.svg?style=flat-square - :target: https://pypi.python.org/pypi/property-cached - .. image:: https://img.shields.io/travis/althonos/property-cached/master.svg?style=flat-square :target: https://travis-ci.org/althonos/property-cached +.. image:: https://img.shields.io/codecov/c/gh/althonos/property-cached.svg?style=flat-square + :target: https://codecov.io/gh/althonos/property-cached + +.. image:: https://img.shields.io/pypi/v/property-cached.svg?style=flat-square + :target: https://pypi.python.org/pypi/property-cached + .. image:: https://img.shields.io/badge/code%20style-black-000000.svg?style=flat-square :target: https://github.com/ambv/black From 59f99d8377b807caf49e8963e9527033439f158b Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Mon, 22 Jul 2019 09:30:11 +0000 Subject: [PATCH 22/33] Release v1.6.1 --- CHANGELOG.rst | 6 +++++- property_cached/_version.txt | 2 +- setup.cfg | 12 +----------- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 3879104..4fbe227 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,6 +3,11 @@ History ------- +1.6.1 (2019-07-22) +++++++++++++++++++ + +* Fix unneeded dependencies being present in ``setup.cfg``. + 1.6.0 (2019-07-22) ++++++++++++++++++ @@ -11,7 +16,6 @@ History * Improve function wrapping using ``functools.update_wrapper``. * Implement the ``__set_name__`` magic method available since Python 3.6. - 1.5.1 (2018-08-05) ++++++++++++++++++ diff --git a/property_cached/_version.txt b/property_cached/_version.txt index dc1e644..9c6d629 100644 --- a/property_cached/_version.txt +++ b/property_cached/_version.txt @@ -1 +1 @@ -1.6.0 +1.6.1 diff --git a/setup.cfg b/setup.cfg index fea63bd..70c5b33 100644 --- a/setup.cfg +++ b/setup.cfg @@ -24,9 +24,6 @@ classifiers = Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 - Topic :: Scientific/Engineering :: Bio-Informatics -project_urls = - Bug Reports = https://github.com/althonos/moclo/issues [options] zip_safe = true @@ -36,13 +33,6 @@ packages = property_cached test_suite = tests setup_requires = setuptools >=39.2 -install_requires = - biopython ~=1.71 - cached-property ~=1.4 - fs ~=2.1 - six ~=1.10 - typing ~=3.6 ; python_version < '3.6' - bz2file ~=0.98 ; python_version < '3' [bdist_wheel] universal = 1 @@ -87,7 +77,7 @@ ignore = D200, D203, D213, D406, D407 # Google conventions [mypy] ignore_missing_imports = true -[mypy-moclo.*] +[mypy-property_cached.*] disallow_any_decorated = false disallow_any_generics = false disallow_any_unimported = true From df4616f2042bbcf2e83f6c2a6c6c7e57bb5583c3 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Mon, 22 Jul 2019 12:15:18 +0000 Subject: [PATCH 23/33] Revert metadata to original author and add self as maintainer --- CHANGELOG.rst | 5 +++++ setup.cfg | 6 ++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 4fbe227..384aa97 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,6 +3,11 @@ History ------- +Unreleased +++++++++++ + +* Fix metadata to keep original author and add @althonos as maintainer. + 1.6.1 (2019-07-22) ++++++++++++++++++ diff --git a/setup.cfg b/setup.cfg index 70c5b33..f809b84 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,8 +1,10 @@ [metadata] name = property-cached version = file: property_cached/_version.txt -author = Martin Larralde -author-email = martin.larralde@ens-paris-saclay.fr +author = Daniel Greenfeld +author-email = pydanny@gmail.com +maintainer = Martin Larralde +maintainer-email = martin.larralde@ens-paris-saclay.fr home-page = https://github.com/althonos/property-cached/ description = A decorator for caching properties in classes (forked from cached-property). long-description = file: README.rst, CHANGELOG.rst From 385d6b908dc1efde86fac4697e1147f96d1747f7 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Mon, 22 Jul 2019 12:26:52 +0000 Subject: [PATCH 24/33] Release v1.6.2 --- CHANGELOG.rst | 15 ++++++++------- property_cached/_version.txt | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 384aa97..b8635b0 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,23 +3,24 @@ History ------- -Unreleased -++++++++++ -* Fix metadata to keep original author and add @althonos as maintainer. +1.6.2 (2019-07-22) +++++++++++++++++++ + +* Fix metadata to keep original author and add @althonos as maintainer 1.6.1 (2019-07-22) ++++++++++++++++++ -* Fix unneeded dependencies being present in ``setup.cfg``. +* Fix unneeded dependencies being present in ``setup.cfg`` 1.6.0 (2019-07-22) ++++++++++++++++++ * Fixed class hierarchy, ``cached_property`` now inherits from ``property`` -* Add support for slotted classes and stop using the object ``__dict__``. -* Improve function wrapping using ``functools.update_wrapper``. -* Implement the ``__set_name__`` magic method available since Python 3.6. +* Add support for slotted classes and stop using the object ``__dict__`` +* Improve function wrapping using ``functools.update_wrapper`` +* Implement the ``__set_name__`` magic method available since Python 3.6 1.5.1 (2018-08-05) ++++++++++++++++++ diff --git a/property_cached/_version.txt b/property_cached/_version.txt index 9c6d629..fdd3be6 100644 --- a/property_cached/_version.txt +++ b/property_cached/_version.txt @@ -1 +1 @@ -1.6.1 +1.6.2 From 490937626f53e6d08b4d4e3b20c1c475e32f70c1 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Sat, 7 Sep 2019 17:39:33 +0200 Subject: [PATCH 25/33] Release v1.6.3 --- CHANGELOG.rst | 4 ++++ property_cached/_version.txt | 2 +- tests/requirements.txt | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index b8635b0..382f804 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,6 +3,10 @@ History ------- +1.6.3 (2019-09-07) +++++++++++++++++++ + +* Resolve `cached_property` docstring not showing (`#171 `_). 1.6.2 (2019-07-22) ++++++++++++++++++ diff --git a/property_cached/_version.txt b/property_cached/_version.txt index fdd3be6..266146b 100644 --- a/property_cached/_version.txt +++ b/property_cached/_version.txt @@ -1 +1 @@ -1.6.2 +1.6.3 diff --git a/tests/requirements.txt b/tests/requirements.txt index b89fb99..51a0916 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -4,3 +4,4 @@ freezegun==0.3.10 twine==1.12.1 wheel==0.32.0 green==2.16.1 +lxml==4.3.5 ; python_version=="3.4" From 95ba597912cf6a4421361b56999ab4fed299f140 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sat, 5 Oct 2019 15:38:38 +0000 Subject: [PATCH 26/33] Bump green from 2.16.1 to 3.0.0 (#5) --- tests/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/requirements.txt b/tests/requirements.txt index 51a0916..a1941be 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -3,5 +3,5 @@ coverage==4.4.2 freezegun==0.3.10 twine==1.12.1 wheel==0.32.0 -green==2.16.1 +green==3.0.0 lxml==4.3.5 ; python_version=="3.4" From 525ddf90dc6fa39b314c605dfb03da918bb8c345 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sat, 5 Oct 2019 15:57:22 +0000 Subject: [PATCH 27/33] Bump wheel from 0.32.0 to 0.33.6 (#4) --- tests/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/requirements.txt b/tests/requirements.txt index a1941be..c9e4dd9 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -2,6 +2,6 @@ coverage==4.4.2 freezegun==0.3.10 twine==1.12.1 -wheel==0.32.0 +wheel==0.33.6 green==3.0.0 lxml==4.3.5 ; python_version=="3.4" From e7ffba9eba9671349278ed2a46a66553e0359744 Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Wed, 8 Jan 2020 14:07:39 +0100 Subject: [PATCH 28/33] Drop Python 3.4 and 2.7 explicit support --- .travis.yml | 6 +----- setup.cfg | 6 ++---- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7e5e2b8..873e8a8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,11 +8,7 @@ git: matrix: include: - - python: pypy - env: TESTS=tests.test_cached_property - - python: 2.7 - env: TESTS=tests.test_cached_property - - python: 3.4 + - python: pypy3.5 env: TESTS=tests.test_cached_property - python: 3.5 env: TESTS=tests.test_cached_property diff --git a/setup.cfg b/setup.cfg index f809b84..8924192 100644 --- a/setup.cfg +++ b/setup.cfg @@ -19,18 +19,16 @@ classifiers = Natural Language :: English Operating System :: OS Independent Programming Language :: Python - Programming Language :: Python :: 2 - Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 - Programming Language :: Python :: 3.4 Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 + Programming Language :: Python :: 3.8 [options] zip_safe = true include_package_data = true -python_requires = >= 2.7, != 3.0.*, != 3.1.*, != 3.2.*, != 3.3.* +python_requires = >= 3.5 packages = property_cached test_suite = tests setup_requires = From 108d189cb9318684cf1bd4b6230a27dcc83dcaad Mon Sep 17 00:00:00 2001 From: Sean Aubin Date: Fri, 6 Mar 2020 10:24:00 -0500 Subject: [PATCH 29/33] Remove Python 2 support (#25) * asyncio has been included since 3.3 * remove python 2 functools check * remove obsolete utf-8 header * add myself to authors * remove Python 2 from README Co-authored-by: Sean Aubin --- AUTHORS.rst | 1 + README.rst | 3 +-- property_cached/__init__.py | 21 ++------------------- 3 files changed, 4 insertions(+), 21 deletions(-) diff --git a/AUTHORS.rst b/AUTHORS.rst index ef147f0..8df0ebd 100644 --- a/AUTHORS.rst +++ b/AUTHORS.rst @@ -26,3 +26,4 @@ Contributors * Vadim Pushtaev (@VadimPushtaev) * Martin Larralde (@althonos) * Qudus Oladipo (@stikks) +* Sean Aubin (@seanny123) diff --git a/README.rst b/README.rst index fe666a9..b202b7c 100644 --- a/README.rst +++ b/README.rst @@ -23,14 +23,13 @@ replacement with fully compatible API (import ``property_cached`` instead of ``cached_property`` in your code and *voilà*). In case development resumes on the original library, this one is likely to be deprecated. -*Original readme included below:* +*Slightly modified README included below:* Why? ----- * Makes caching of time or computational expensive properties quick and easy. * Because I got tired of copy/pasting this code from non-web project to non-web project. -* I needed something really simple that worked in Python 2 and 3. How to use it -------------- diff --git a/property_cached/__init__.py b/property_cached/__init__.py index c2da214..3353048 100644 --- a/property_cached/__init__.py +++ b/property_cached/__init__.py @@ -1,17 +1,10 @@ -# -*- coding: utf-8 -*- - +import asyncio import functools import pkg_resources -import sys import threading import weakref from time import time -try: - import asyncio -except (ImportError, SyntaxError): - asyncio = None - __author__ = "Martin Larralde" __email__ = "martin.larralde@ens-paris-saclay.fr" @@ -27,17 +20,7 @@ class cached_property(property): """ # noqa _sentinel = object() - - if sys.version_info[0] < 3: - - def _update_wrapper(self, func): - self.__doc__ = getattr(func, "__doc__", None) - self.__module__ = getattr(func, "__module__", None) - self.__name__ = getattr(func, "__name__", None) - - else: - - _update_wrapper = functools.update_wrapper + _update_wrapper = functools.update_wrapper def __init__(self, func): self.cache = weakref.WeakKeyDictionary() From 9707421e1a357b29db5dedc4bc28147d6e30c3ad Mon Sep 17 00:00:00 2001 From: Martin Larralde Date: Fri, 6 Mar 2020 16:28:14 +0100 Subject: [PATCH 30/33] Release v1.6.4 --- CHANGELOG.rst | 5 +++++ LICENSE | 2 +- property_cached/_version.txt | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 382f804..8770117 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -3,6 +3,11 @@ History ------- +1.6.4 (2020-03-06) +++++++++++++++++++ + +* Fix some remaining Python 2 support code (`#25 `_) + 1.6.3 (2019-09-07) ++++++++++++++++++ diff --git a/LICENSE b/LICENSE index a21d57b..a9f5694 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright (c) 2018-2019, Martin Larralde +Copyright (c) 2018-2020, Martin Larralde Copyright (c) 2015-2018, Daniel Greenfeld All rights reserved. diff --git a/property_cached/_version.txt b/property_cached/_version.txt index 266146b..9edc58b 100644 --- a/property_cached/_version.txt +++ b/property_cached/_version.txt @@ -1 +1 @@ -1.6.3 +1.6.4 From 19b54091c8e351f1e9a9a5cf80bc5cab78f9efa0 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Mon, 13 Apr 2020 12:21:56 +0000 Subject: [PATCH 31/33] Bump coverage from 4.4.2 to 5.1 (#27) --- tests/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/requirements.txt b/tests/requirements.txt index c9e4dd9..dcfd042 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -1,5 +1,5 @@ # Testing and deployment packages. -coverage==4.4.2 +coverage==5.1 freezegun==0.3.10 twine==1.12.1 wheel==0.33.6 From 2663fdea1f2a3c71dc90fe2fb05ab2e96f5f6bca Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Tue, 14 Apr 2020 11:49:55 +0000 Subject: [PATCH 32/33] Bump wheel from 0.33.6 to 0.34.2 (#23) --- tests/requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/requirements.txt b/tests/requirements.txt index dcfd042..e0cadd9 100644 --- a/tests/requirements.txt +++ b/tests/requirements.txt @@ -2,6 +2,6 @@ coverage==5.1 freezegun==0.3.10 twine==1.12.1 -wheel==0.33.6 +wheel==0.34.2 green==3.0.0 lxml==4.3.5 ; python_version=="3.4" From f2b6e5e9f6f03223be886335d2e2a3f1e4298e39 Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Sun, 27 Jun 2021 15:12:24 +0200 Subject: [PATCH 33/33] Upgrade to GitHub-native Dependabot (#61) Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com> --- .github/dependabot.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..d318b22 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,16 @@ +version: 2 +updates: +- package-ecosystem: pip + directory: "/" + schedule: + interval: daily + time: "04:00" + open-pull-requests-limit: 10 + ignore: + - dependency-name: twine + versions: + - 3.3.0 + - 3.4.0 + - dependency-name: coverage + versions: + - "5.4"