Skip to content

Commit

Permalink
move reify() into its own file
Browse files Browse the repository at this point in the history
  • Loading branch information
kneufeld committed Dec 31, 2018
1 parent df15f40 commit c10433b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 44 deletions.
43 changes: 0 additions & 43 deletions alkali/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,46 +13,3 @@
from .query import Query
from .utils import tznow, tzadd, fromts
from . import fields

class reify(object):
""" Use as a class method decorator. It operates almost exactly like the
Python ``@property`` decorator, but it puts the result of the method it
decorates into the instance dict after the first call, effectively
replacing the function it decorates with an instance variable. It is, in
Python parlance, a non-data descriptor. The following is an example and
its usage:
.. doctest::
>>> from pyramid.decorator import reify
>>> class Foo(object):
... @reify
... def jammy(self):
... print('jammy called')
... return 1
>>> f = Foo()
>>> v = f.jammy
jammy called
>>> print(v)
1
>>> f.jammy
1
>>> # jammy func not called the second time; it replaced itself with 1
>>> # Note: reassignment is possible
>>> f.jammy = 2
>>> f.jammy
2
"""
def __init__(self, wrapped):
self.wrapped = wrapped
from functools import update_wrapper
update_wrapper(self, wrapped)

def __get__(self, inst, objtype=None):
if inst is None:
return self
val = self.wrapped(inst)
setattr(inst, self.wrapped.__name__, val)
return val
1 change: 0 additions & 1 deletion alkali/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ class OneToOneField(ForeignKey):
"""
"""
# TODO maybe use reify
# https://docs.pylonsproject.org/projects/pyramid/en/latest/_modules/pyramid/decorator.html#reify
# I forsee a problem where you load the primary table and either
# create the OneToOneField table entries and replace the as the real file is loaded
# or maybe you have some wierd race condition in the other direction
Expand Down
46 changes: 46 additions & 0 deletions alkali/reify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# from: https://docs.pylonsproject.org/projects/pyramid/en/latest/_modules/pyramid/decorator.html#reify

from functools import update_wrapper

class reify:
"""
Use as a class method decorator. It operates almost exactly like
the Python ``@property`` decorator, but it puts the result of the
method it decorates into the instance dict after the first call,
effectively replacing the function it decorates with an instance
variable. It is, in Python parlance, a non-data descriptor. The
following is an example and its usage:
.. doctest::
>>> from pyramid.decorator import reify
>>> class Foo(object):
... @reify
... def jammy(self):
... print('jammy called')
... return 1
>>> f = Foo()
>>> v = f.jammy
jammy called
>>> print(v)
1
>>> f.jammy
1
>>> # jammy func not called the second time; it replaced itself with 1
>>> # Note: reassignment is possible
>>> f.jammy = 2
>>> f.jammy
2
"""
def __init__(self, wrapped):
self.wrapped = wrapped
update_wrapper(self, wrapped)

def __get__(self, inst, objtype=None):
if inst is None:
return self
val = self.wrapped(inst)
setattr(inst, self.wrapped.__name__, val)
return val
17 changes: 17 additions & 0 deletions alkali/tests/test_reify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import unittest

from alkali.reify import reify

class Class:

@reify
def x(self):
return list(range(5))

class TestReify( unittest.TestCase ):

def test_1(self):
Class.x # for test coverage

c = Class()
self.assertTrue(isinstance(c.x, list))

0 comments on commit c10433b

Please sign in to comment.