Merge pull request #97 from pre-commit/use_asottile_cached_property

Use asottile.cached_property.
This commit is contained in:
Anthony Sottile 2014-06-01 16:52:42 -07:00
commit 3baae2fd1e
7 changed files with 5 additions and 45 deletions

View file

@ -1,8 +1,8 @@
import os.path
from asottile.cached_property import cached_property
import pre_commit.constants as C
from pre_commit.clientlib.validate_manifest import load_manifest
from pre_commit.util import cached_property
class Manifest(object):

View file

@ -1,9 +1,9 @@
from asottile.cached_property import cached_property
from asottile.ordereddict import OrderedDict
from pre_commit.languages.all import languages
from pre_commit.manifest import Manifest
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
from pre_commit.util import cached_property
class Repository(object):

View file

@ -1,12 +1,12 @@
import os
import os.path
from asottile.cached_property import cached_property
import pre_commit.constants as C
from pre_commit import git
from pre_commit.clientlib.validate_config import load_config
from pre_commit.repository import Repository
from pre_commit.store import Store
from pre_commit.util import cached_property
class Runner(object):

View file

@ -5,10 +5,10 @@ import logging
import os
import os.path
import tempfile
from asottile.cached_property import cached_property
from plumbum import local
from pre_commit.prefixed_command_runner import PrefixedCommandRunner
from pre_commit.util import cached_property
from pre_commit.util import clean_path_on_failure

View file

@ -6,23 +6,6 @@ import shutil
import sys
class cached_property(object):
"""Like @property, but caches the value."""
def __init__(self, func):
self.__name__ = func.__name__
self.__module__ = func.__module__
self.__doc__ = func.__doc__
self._func = func
def __get__(self, obj, cls):
if obj is None:
return self
value = self._func(obj)
obj.__dict__[self.__name__] = value
return value
def memoize_by_cwd(func):
"""Memoize a function call based on os.getcwd()."""
@functools.wraps(func)

View file

@ -29,6 +29,7 @@ setup(
},
install_requires=[
'argparse',
'asottile.cached_property',
'asottile.ordereddict',
'asottile.yaml',
'jsonschema',

View file

@ -6,35 +6,11 @@ import random
import sys
from plumbum import local
from pre_commit.util import cached_property
from pre_commit.util import clean_path_on_failure
from pre_commit.util import entry
from pre_commit.util import memoize_by_cwd
@pytest.fixture
def class_with_cached_property():
class Foo(object):
@cached_property
def my_property(self):
return "Foo" + str(random.getrandbits(64))
return Foo
def test_cached_property(class_with_cached_property):
instance = class_with_cached_property()
val = instance.my_property
val2 = instance.my_property
assert val is val2
def test_unbound_cached_property(class_with_cached_property):
# Make sure we don't blow up when accessing the property unbound
prop = class_with_cached_property.my_property
assert isinstance(prop, cached_property)
@pytest.fixture
def memoized_by_cwd():
@memoize_by_cwd