-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bumps version to 0.5.0. Adds is_data_attr and classproperty
- Loading branch information
Showing
7 changed files
with
151 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2017 the Pockets team, see AUTHORS. | ||
# Licensed under the BSD License, see LICENSE for details. | ||
|
||
"""A pocket full of useful decorators!""" | ||
|
||
|
||
__all__ = ['classproperty'] | ||
|
||
|
||
def classproperty(cls): | ||
""" | ||
Decorator to create a read-only class property similar to classmethod. | ||
For whatever reason, the @property decorator isn't smart enough to | ||
recognize @classmethods and behaves differently on them than on instance | ||
methods. This decorator may be used like to create a class-level property, | ||
useful for singletons and other one-per-class properties. | ||
Note: | ||
Class properties created by @classproperty are read-only. Any attempts | ||
to write to the property will erase the @classproperty, and the | ||
behavior of the underlying method will be lost. | ||
>>> class MyClass(object): | ||
... @classproperty | ||
... def myproperty(cls): | ||
... return '{0}.myproperty'.format(cls.__name__) | ||
>>> MyClass.myproperty | ||
'MyClass.myproperty' | ||
""" | ||
class _classproperty(property): | ||
def __get__(self, cls, owner): | ||
return self.fget.__get__(None, owner)() | ||
|
||
def getter(self, fget): | ||
raise AttributeError('@classproperty.getter is not supported') | ||
|
||
def setter(self, fset): | ||
raise AttributeError('@classproperty.setter is not supported') | ||
|
||
def deleter(self, fdel): | ||
raise AttributeError('@classproperty.deleter is not supported') | ||
return _classproperty(classmethod(cls)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2017 the Pockets team, see AUTHORS. | ||
# Licensed under the BSD License, see LICENSE for details. | ||
|
||
"""Tests for :mod:`pockets.decorators` module.""" | ||
|
||
from __future__ import absolute_import | ||
import pytest | ||
from pockets.decorators import classproperty | ||
|
||
|
||
class A(object): | ||
@classproperty | ||
def prop(cls): | ||
return '{0}.prop'.format(cls.__name__) | ||
|
||
def meth(cls): | ||
pass | ||
|
||
|
||
def test_classproperty(): | ||
assert A.prop == 'A.prop' | ||
assert A().prop == 'A.prop' | ||
pytest.raises(AttributeError, classproperty(A.meth).getter, A.meth) | ||
pytest.raises(AttributeError, classproperty(A.meth).setter, A.meth) | ||
pytest.raises(AttributeError, classproperty(A.meth).deleter, A.meth) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters