From 5462bb91c4123b46a22700834af1da0c4bad6931 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Sun, 18 Jan 2015 02:28:41 +0200 Subject: [PATCH] Renaming Catalog class to AbstractCatalog --- README.md | 40 ++++++++++++++++++++++------------------ VERSION | 2 +- examples/concept.py | 20 +++++++++++--------- examples/overrides.py | 20 +++++++++++--------- objects/__init__.py | 4 ++-- objects/catalog.py | 10 +++++----- 6 files changed, 52 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 0c92a574..e7461fba 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,9 @@ Example of objects catalog definition and usage: Concept example of objects catalogs. """ -from objects import Catalog, Singleton, NewInstance, InitArg, Attribute +from objects import AbstractCatalog +from objects.providers import Singleton, NewInstance +from objects.injections import InitArg, Attribute import sqlite3 @@ -28,7 +30,7 @@ class ObjectB(object): # Catalog of objects providers. -class AppCatalog(Catalog): +class Catalog(AbstractCatalog): """ Objects catalog. """ @@ -49,20 +51,20 @@ class AppCatalog(Catalog): # Catalog static provides. -a1, a2 = AppCatalog.object_a(), AppCatalog.object_a() -b1, b2 = AppCatalog.object_b(), AppCatalog.object_b() +a1, a2 = Catalog.object_a(), Catalog.object_a() +b1, b2 = Catalog.object_b(), Catalog.object_b() # Some asserts. assert a1 is not a2 assert b1 is not b2 -assert a1.db is a2.db is b1.db is b2.db is AppCatalog.database() +assert a1.db is a2.db is b1.db is b2.db is Catalog.database() # Dependencies injection (The Python Way) into class. class Consumer(object): - dependencies = AppCatalog(AppCatalog.object_a, - AppCatalog.object_b) + dependencies = Catalog(Catalog.object_a, + Catalog.object_b) def test(self): a1 = self.dependencies.object_a() @@ -87,8 +89,8 @@ Consumer().test() # Dependencies injection (The Python Way) into a callback. -def consumer_callback(dependencies=AppCatalog(AppCatalog.object_a, - AppCatalog.object_b)): +def consumer_callback(dependencies=Catalog(Catalog.object_a, + Catalog.object_b)): a1 = dependencies.object_a() a2 = dependencies.object_a() @@ -116,7 +118,9 @@ Concept example of objects overrides. """ -from objects import Catalog, Singleton, NewInstance, InitArg, Attribute, overrides +from objects import AbstractCatalog, overrides +from objects.providers import Singleton, NewInstance +from objects.injections import InitArg, Attribute import sqlite3 @@ -131,7 +135,7 @@ class ObjectAMock(ObjectA): # Catalog of objects providers. -class AppCatalog(Catalog): +class Catalog(AbstractCatalog): """ Objects catalog. """ @@ -146,25 +150,25 @@ class AppCatalog(Catalog): """ :type: (objects.Provider) -> ObjectA """ -# Overriding AppCatalog by SandboxCatalog with some mocks. -@overrides(AppCatalog) -class SandboxCatalog(AppCatalog): +# Overriding Catalog by SandboxCatalog with some mocks. +@overrides(Catalog) +class SandboxCatalog(Catalog): """ Sandbox objects catalog with some mocks. """ object_a = NewInstance(ObjectAMock, - InitArg('db', AppCatalog.database)) + InitArg('db', Catalog.database)) """ :type: (objects.Provider) -> ObjectA """ # Catalog static provides. -a1 = AppCatalog.object_a() -a2 = AppCatalog.object_a() +a1 = Catalog.object_a() +a2 = Catalog.object_a() # Some asserts. assert isinstance(a1, ObjectAMock) assert isinstance(a2, ObjectAMock) assert a1 is not a2 -assert a1.db is a2.db is AppCatalog.database() +assert a1.db is a2.db is Catalog.database() ``` diff --git a/VERSION b/VERSION index 1750564f..6e8bf73a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.6 +0.1.0 diff --git a/examples/concept.py b/examples/concept.py index 2b807f5f..34714fd1 100644 --- a/examples/concept.py +++ b/examples/concept.py @@ -2,7 +2,9 @@ Concept example of objects catalogs. """ -from objects import Catalog, Singleton, NewInstance, InitArg, Attribute +from objects import AbstractCatalog +from objects.providers import Singleton, NewInstance +from objects.injections import InitArg, Attribute import sqlite3 @@ -19,7 +21,7 @@ class ObjectB(object): # Catalog of objects providers. -class AppCatalog(Catalog): +class Catalog(AbstractCatalog): """ Objects catalog. """ @@ -40,20 +42,20 @@ class AppCatalog(Catalog): # Catalog static provides. -a1, a2 = AppCatalog.object_a(), AppCatalog.object_a() -b1, b2 = AppCatalog.object_b(), AppCatalog.object_b() +a1, a2 = Catalog.object_a(), Catalog.object_a() +b1, b2 = Catalog.object_b(), Catalog.object_b() # Some asserts. assert a1 is not a2 assert b1 is not b2 -assert a1.db is a2.db is b1.db is b2.db is AppCatalog.database() +assert a1.db is a2.db is b1.db is b2.db is Catalog.database() # Dependencies injection (The Python Way) into class. class Consumer(object): - dependencies = AppCatalog(AppCatalog.object_a, - AppCatalog.object_b) + dependencies = Catalog(Catalog.object_a, + Catalog.object_b) def test(self): a1 = self.dependencies.object_a() @@ -78,8 +80,8 @@ Consumer().test() # Dependencies injection (The Python Way) into a callback. -def consumer_callback(dependencies=AppCatalog(AppCatalog.object_a, - AppCatalog.object_b)): +def consumer_callback(dependencies=Catalog(Catalog.object_a, + Catalog.object_b)): a1 = dependencies.object_a() a2 = dependencies.object_a() diff --git a/examples/overrides.py b/examples/overrides.py index d884e1ea..6a6d5ebb 100644 --- a/examples/overrides.py +++ b/examples/overrides.py @@ -3,7 +3,9 @@ Concept example of objects overrides. """ -from objects import Catalog, Singleton, NewInstance, InitArg, Attribute, overrides +from objects import AbstractCatalog, overrides +from objects.providers import Singleton, NewInstance +from objects.injections import InitArg, Attribute import sqlite3 @@ -18,7 +20,7 @@ class ObjectAMock(ObjectA): # Catalog of objects providers. -class AppCatalog(Catalog): +class Catalog(AbstractCatalog): """ Objects catalog. """ @@ -33,24 +35,24 @@ class AppCatalog(Catalog): """ :type: (objects.Provider) -> ObjectA """ -# Overriding AppCatalog by SandboxCatalog with some mocks. -@overrides(AppCatalog) -class SandboxCatalog(AppCatalog): +# Overriding Catalog by SandboxCatalog with some mocks. +@overrides(Catalog) +class SandboxCatalog(Catalog): """ Sandbox objects catalog with some mocks. """ object_a = NewInstance(ObjectAMock, - InitArg('db', AppCatalog.database)) + InitArg('db', Catalog.database)) """ :type: (objects.Provider) -> ObjectA """ # Catalog static provides. -a1 = AppCatalog.object_a() -a2 = AppCatalog.object_a() +a1 = Catalog.object_a() +a2 = Catalog.object_a() # Some asserts. assert isinstance(a1, ObjectAMock) assert isinstance(a2, ObjectAMock) assert a1 is not a2 -assert a1.db is a2.db is AppCatalog.database() +assert a1.db is a2.db is Catalog.database() diff --git a/objects/__init__.py b/objects/__init__.py index fdafea69..70e3e631 100644 --- a/objects/__init__.py +++ b/objects/__init__.py @@ -2,13 +2,13 @@ `Objects` library. """ -from .catalog import Catalog, overrides +from .catalog import AbstractCatalog, overrides from .providers import (Provider, NewInstance, Singleton, Class, Object, Function, Value) from .injections import InitArg, Attribute, Method -__all__ = ['Catalog', 'overrides', +__all__ = ['AbstractCatalog', 'overrides', # Providers 'Provider', 'NewInstance', 'Singleton', 'Class', diff --git a/objects/catalog.py b/objects/catalog.py index d008fa3f..bd265052 100644 --- a/objects/catalog.py +++ b/objects/catalog.py @@ -5,9 +5,9 @@ Catalog module. from .providers import Provider -class Catalog(object): +class AbstractCatalog(object): """ - Object provides catalog. + Abstract object provides catalog. """ def __init__(self, *used_providers): @@ -23,7 +23,7 @@ class Catalog(object): :param item: :return: """ - attribute = super(Catalog, self).__getattribute__(item) + attribute = super(AbstractCatalog, self).__getattribute__(item) if item in ('__used_providers__',): return attribute @@ -38,7 +38,7 @@ class Catalog(object): Returns set of all class providers. """ providers = set() - for attr_name in set(dir(cls)) - set(dir(Catalog)): + for attr_name in set(dir(cls)) - set(dir(AbstractCatalog)): provider = getattr(cls, attr_name) if not isinstance(provider, Provider): continue @@ -50,7 +50,7 @@ class Catalog(object): """ Overrides current catalog providers by overriding catalog providers. - :param overriding: Catalog + :param overriding: AbstractCatalog """ overriden = overriding.__all_providers__() - cls.__all_providers__() for name, provider in overriden: