Skip to content

Commit

Permalink
More magic and clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Debith committed Apr 26, 2015
1 parent bf376cc commit 60646b9
Show file tree
Hide file tree
Showing 13 changed files with 474 additions and 82 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
Changelog
=========

0.13.0 (2015-04-25)
-------------------
- New feature: Decorator type_safe to check function arguments
- New feature: combine_class function takes name for new class as first argument
- Refactoring magic.py to look less like black magic
- Improving errors.py exception class creation to accept custom messages

0.12.0 (2015-04-22)
-------------------
- New feature: Rename of composed traits
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from setuptools import find_packages
from setuptools import setup


def read(*names, **kwargs):
return io.open(
join(dirname(__file__), *names),
Expand All @@ -22,7 +23,7 @@ def read(*names, **kwargs):

setup(
name='py3traits',
version='0.10.0',
version='0.13.0',
license='Apache License 2',
description='Trait support for Python 3',
long_description='%s\n%s' % (read('README.rst'), re.sub(':obj:`~?(.*?)`', r'``\1``', read('CHANGELOG.rst'))),
Expand Down
7 changes: 4 additions & 3 deletions src/pytraits/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
limitations under the License.
'''

from pytraits.core import Singleton
from pytraits.core import Singleton, type_safe, type_converted
from pytraits.combiner import combine_class
from pytraits.extendable import extendable
from pytraits.trait_composer import add_traits

__version__ = "0.1.0"
__all__ = ["Singleton", "combine_class", "extendable", "add_traits"]
__version__ = "0.13.0"
__all__ = ["Singleton", "combine_class", "extendable", "add_traits",
"type_safe", "type_converted"]
9 changes: 6 additions & 3 deletions src/pytraits/combiner.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from pytraits.trait_composer import add_traits


def combine_class(*traits, **resolved_conflicts):
def combine_class(class_name, *traits, **resolved_conflicts):
"""
This function composes new class out of any number of traits.
Expand All @@ -32,12 +32,15 @@ def combine_class(*traits, **resolved_conflicts):
>>> class Three:
... def third(self): return 3
...
>>> Combination = combine_class(One, Two, Three)
>>> Combination = combine_class("Combination", One, Two, Three)
>>> instance = Combination()
>>> instance.first(), instance.second(), instance.third()
(1, 2, 3)
>>> instance.__class__.__name__
'Combination'
"""
NewClass = type("NewClass", (object,), {})
NewClass = type(class_name, (object,), {})
add_traits(NewClass, *traits)
return NewClass

Expand Down
4 changes: 2 additions & 2 deletions src/pytraits/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@

from pytraits.core.singleton import Singleton
from pytraits.core.utils import flatten
from pytraits.core.magic import type_converted
from pytraits.core.magic import type_safe, type_converted

__all__ = ["Singleton", "flatten", "type_converted"]
__all__ = ["Singleton", "flatten", "type_safe", "type_converted"]
10 changes: 6 additions & 4 deletions src/pytraits/core/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@

# Exceptions
UnextendableObjectError = "Target context can be only class or instance of class"
InvalidAssignmentError = "Not possible to assign a key"
SingletonError = 'Singletons are immutable'
SingletonError = 'Singletons are immutable!'
BuiltinSourceError = 'Built-in objects can not used as traits!'
PropertySourceError = 'Properties can not be extended!'
TypeConversionError = 'Conversion impossible!'


# Convert strings to exception objects
for exception, message in dict(globals()).items():
if not exception.endswith('Error'):
continue

bases = (Exception,)
attrs = {'_MSG': message,
'__str__': lambda self: self._MSG}
attrs = {'__default_msg': message,
'__init__': lambda self, msg=None: setattr(self, '__msg', msg),
'__str__': lambda self: self.__msg or self.__default_msg}
globals()[exception] = type(exception, bases, attrs)

0 comments on commit 60646b9

Please sign in to comment.