From d48616736b82f993a0412ea4dc256c56e18875c4 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Tue, 10 Mar 2015 00:43:23 +0200 Subject: [PATCH] updating examples codestyle --- examples/config_provider.py | 74 +++++++++++++++++---------------- examples/delegate.py | 42 ++++++++++--------- examples/external_dependency.py | 28 ++++++++----- examples/overrides.py | 51 ++++++++++------------- examples/scoped_provider.py | 33 +++++++-------- objects/catalog.py | 1 + 6 files changed, 117 insertions(+), 112 deletions(-) diff --git a/examples/config_provider.py b/examples/config_provider.py index 21cb3c2c..2dedc0d8 100644 --- a/examples/config_provider.py +++ b/examples/config_provider.py @@ -1,69 +1,71 @@ -""" -Config provider examples. -""" +"""Config provider examples.""" + from objects import AbstractCatalog -from objects.providers import ( - Config, - NewInstance, -) + +from objects.providers import Config +from objects.providers import NewInstance + from objects.injections import InitArg -# Some example class. class ObjectA(object): - def __init__(self, setting_one, setting_two, setting_three): - self.setting_one = setting_one - self.setting_two = setting_two - self.setting_three = setting_three + + """Example class ObjectA, that has dependencies on some setting values.""" + + def __init__(self, fee, price, timezone): + """Initializer.""" + self.fee = fee + self.price = price + self.timezone = timezone -# Catalog of objects providers. class Catalog(AbstractCatalog): - """ - Objects catalog. - """ + + """Catalog of objects providers.""" config = Config() - """ :type: (objects.Config) """ + """:type: (objects.Config)""" object_a = NewInstance(ObjectA, - InitArg('setting_one', config.SETTING_ONE), - InitArg('setting_two', config.SETTING_TWO), - InitArg('setting_three', config.GLOBAL.SETTING_THREE)) - """ :type: (objects.Provider) -> ObjectA """ + InitArg('fee', config.FEE), + InitArg('price', config.PRICE), + InitArg('timezone', config.GLOBAL.TIMEZONE)) + """:type: (objects.Provider) -> ObjectA""" # Setting config value and making some tests. Catalog.config.update_from({ - 'SETTING_ONE': 1, - 'SETTING_TWO': 2, + 'FEE': 1.25, + 'PRICE': 2.99, 'GLOBAL': { - 'SETTING_THREE': 3 + 'TIMEZONE': 'US/Eastern' } }) object_a1 = Catalog.object_a() -assert object_a1.setting_one == 1 -assert object_a1.setting_two == 2 -assert object_a1.setting_three == 3 +assert object_a1.fee == 1.25 +assert object_a1.price == 2.99 +assert object_a1.timezone == 'US/Eastern' # Changing config value one more time and making some tests. Catalog.config.update_from({ - 'SETTING_ONE': 11, - 'SETTING_TWO': 22, + 'FEE': 5.25, + 'PRICE': 19.99, 'GLOBAL': { - 'SETTING_THREE': 33 + 'TIMEZONE': 'US/Western' } }) object_a2 = Catalog.object_a() -assert object_a2.setting_one == 11 -assert object_a2.setting_two == 22 -assert object_a2.setting_three == 33 +# New one ObjectA has new config values. +assert object_a2.fee == 5.25 +assert object_a2.price == 19.99 +assert object_a2.timezone == 'US/Western' -assert object_a1.setting_one == 1 -assert object_a1.setting_two == 2 -assert object_a1.setting_three == 3 +# And old one has old ones. +assert object_a1.fee == 1.25 +assert object_a1.price == 2.99 +assert object_a1.timezone == 'US/Eastern' diff --git a/examples/delegate.py b/examples/delegate.py index 2b5c83bd..036167f8 100644 --- a/examples/delegate.py +++ b/examples/delegate.py @@ -1,52 +1,56 @@ -"""Example of providers delegate.""" +"""Provider delegation example.""" + from objects import AbstractCatalog -from objects.providers import ( - Singleton, - NewInstance, -) -from objects.injections import ( - InitArg, - Attribute, -) +from objects.providers import Singleton +from objects.providers import NewInstance + +from objects.injections import InitArg +from objects.injections import Attribute import sqlite3 -# Some example classes. class ObjectA(object): + + """Example class ObjectA, that has dependency on database.""" + def __init__(self, db): + """Initializer.""" self.db = db class ObjectB(object): - def __init__(self, a_factory): - self.a_factory = a_factory + + """Example class ObjectB, that has dependency on ObjectA provider.""" + + def __init__(self, a_provider): + """Initializer.""" + self.a_provider = a_provider -# Catalog of objects providers. class Catalog(AbstractCatalog): - """Objects catalog.""" + """Catalog of objects providers.""" database = Singleton(sqlite3.Connection, InitArg('database', ':memory:'), Attribute('row_factory', sqlite3.Row)) - """ :type: (objects.Provider) -> sqlite3.Connection """ + """:type: (objects.Provider) -> sqlite3.Connection""" object_a = NewInstance(ObjectA, InitArg('db', database)) - """ :type: (objects.Provider) -> ObjectA """ + """:type: (objects.Provider) -> ObjectA""" object_b = Singleton(ObjectB, - InitArg('a_factory', object_a.delegate())) - """ :type: (objects.Provider) -> ObjectB """ + InitArg('a_provider', object_a.delegate())) + """:type: (objects.Provider) -> ObjectB""" # Catalog static provides. b = Catalog.object_b() -a1, a2 = b.a_factory(), b.a_factory() +a1, a2 = b.a_provider(), b.a_provider() # Some asserts. assert a1 is not a2 diff --git a/examples/external_dependency.py b/examples/external_dependency.py index df8875b3..e8eb8181 100644 --- a/examples/external_dependency.py +++ b/examples/external_dependency.py @@ -1,46 +1,52 @@ -""" -Concept example of objects catalogs. -""" +"""External dependency example.""" + from objects import AbstractCatalog + from objects.providers import Singleton from objects.providers import NewInstance from objects.providers import ExternalDependency + from objects.injections import InitArg from objects.injections import Attribute import sqlite3 -# Some example classes. class ObjectA(object): + + """Example class ObjectA, that has dependency on database.""" + def __init__(self, db): + """Initializer.""" self.db = db class ObjectB(object): + + """Example class ObjectB, that has dependencies on ObjectA and database.""" + def __init__(self, a, db): + """Initializer.""" self.a = a self.db = db -# Catalog of objects providers. class Catalog(AbstractCatalog): - """ - Objects catalog. - """ + + """Catalog of objects providers.""" database = ExternalDependency(instance_of=sqlite3.Connection) - """ :type: (objects.Provider) -> sqlite3.Connection """ + """:type: (objects.Provider) -> sqlite3.Connection""" object_a = NewInstance(ObjectA, InitArg('db', database)) - """ :type: (objects.Provider) -> ObjectA """ + """:type: (objects.Provider) -> ObjectA""" object_b = NewInstance(ObjectB, InitArg('a', object_a), InitArg('db', database)) - """ :type: (objects.Provider) -> ObjectB """ + """:type: (objects.Provider) -> ObjectB""" # Satisfaction of external dependency. diff --git a/examples/overrides.py b/examples/overrides.py index d5de879c..429d3c63 100644 --- a/examples/overrides.py +++ b/examples/overrides.py @@ -1,61 +1,54 @@ -""" -Concept example of objects overrides. -""" +"""Override example.""" -from objects import ( - AbstractCatalog, - overrides, -) -from objects.providers import ( - Singleton, - NewInstance, -) -from objects.injections import ( - InitArg, - Attribute, -) +from objects import AbstractCatalog +from objects import overrides + +from objects.providers import Singleton +from objects.providers import NewInstance + +from objects.injections import InitArg +from objects.injections import Attribute import sqlite3 -# Some example class. class ObjectA(object): + + """Example class ObjectA, that has dependency on database.""" + def __init__(self, db): + """Initializer.""" self.db = db -# Mock of example class. class ObjectAMock(ObjectA): - pass + + """Mock of ObjectA example class.""" -# Catalog of objects providers. class Catalog(AbstractCatalog): - """ - Objects catalog. - """ + + """Catalog of objects providers.""" database = Singleton(sqlite3.Connection, InitArg('database', ':memory:'), Attribute('row_factory', sqlite3.Row)) - """ :type: (objects.Provider) -> sqlite3.Connection """ + """:type: (objects.Provider) -> sqlite3.Connection""" object_a = NewInstance(ObjectA, InitArg('db', database)) - """ :type: (objects.Provider) -> ObjectA """ + """:type: (objects.Provider) -> ObjectA""" -# Overriding Catalog by SandboxCatalog with some mocks. @overrides(Catalog) class SandboxCatalog(Catalog): - """ - Sandbox objects catalog with some mocks. - """ + + """Sandbox objects catalog with some mocks that overrides Catalog.""" object_a = NewInstance(ObjectAMock, InitArg('db', Catalog.database)) - """ :type: (objects.Provider) -> ObjectA """ + """:type: (objects.Provider) -> ObjectA""" # Catalog static provides. diff --git a/examples/scoped_provider.py b/examples/scoped_provider.py index a85aab84..003dc4ca 100644 --- a/examples/scoped_provider.py +++ b/examples/scoped_provider.py @@ -1,39 +1,38 @@ -""" -Scoped provider examples. -""" +"""Scoped provider examples.""" + from objects import AbstractCatalog -from objects.providers import ( - Singleton, - Scoped, -) -from objects.injections import ( - InitArg, - Attribute, -) + +from objects.providers import Singleton +from objects.providers import Scoped + +from objects.injections import InitArg +from objects.injections import Attribute import sqlite3 class ObjectA(object): + + """Example class ObjectA, that has dependency on database.""" + def __init__(self, db): + """Initializer.""" self.db = db -# Catalog of objects providers. class Catalog(AbstractCatalog): - """ - Objects catalog. - """ + + """Catalog of objects providers.""" database = Singleton(sqlite3.Connection, InitArg('database', ':memory:'), Attribute('row_factory', sqlite3.Row)) - """ :type: (objects.Provider) -> sqlite3.Connection """ + """:type: (objects.Provider) -> sqlite3.Connection""" object_a = Scoped(ObjectA, InitArg('db', database)) - """ :type: (objects.Provider) -> ObjectA """ + """:type: (objects.Provider) -> ObjectA""" # Making scope using `with` statement. diff --git a/objects/catalog.py b/objects/catalog.py index f81272b0..c0f28435 100644 --- a/objects/catalog.py +++ b/objects/catalog.py @@ -49,6 +49,7 @@ class AbstractCatalog(object): def overrides(catalog): """Catalog overriding decorator.""" def decorator(overriding_catalog): + """Overriding decorator.""" catalog.__override___(overriding_catalog) return overriding_catalog return decorator