From 113f770634fb847a4ced0084daad46ead3cb26d0 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Wed, 28 Jan 2015 00:26:38 +0200 Subject: [PATCH] updating readme and examples --- README.md | 79 +++++++++++++++++++++++++++++++++ examples/concept.py | 1 + examples/external_dependency.py | 1 + examples/overrides.py | 1 + examples/scoped_provider.py | 4 ++ 5 files changed, 86 insertions(+) diff --git a/README.md b/README.md index fd8fdf1f..293e649e 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ Concept example of objects catalogs. from objects import AbstractCatalog from objects.providers import Singleton, NewInstance from objects.injections import InitArg, Attribute + import sqlite3 @@ -121,6 +122,7 @@ Concept example of objects overrides. from objects import AbstractCatalog, overrides from objects.providers import Singleton, NewInstance from objects.injections import InitArg, Attribute + import sqlite3 @@ -183,6 +185,7 @@ Concept example of objects catalogs. from objects import AbstractCatalog from objects.providers import Singleton, NewInstance, ExternalDependency from objects.injections import InitArg, Attribute + import sqlite3 @@ -231,3 +234,79 @@ assert a1 is not a2 assert b1 is not b2 assert a1.db is a2.db is b1.db is b2.db is Catalog.database() ``` + +Example of objects catalog with scoped provider: + +```python +""" +Scoped provider examples. +""" + +from objects import AbstractCatalog +from objects.providers import Singleton, Scoped +from objects.injections import InitArg, Attribute + +import sqlite3 + + +class ObjectA(object): + def __init__(self, db): + self.db = db + + +# Catalog of objects providers. +class Catalog(AbstractCatalog): + """ + Objects catalog. + """ + + database = Singleton(sqlite3.Connection, + InitArg('database', ':memory:'), + Attribute('row_factory', sqlite3.Row)) + """ :type: (objects.Provider) -> sqlite3.Connection """ + + object_a = Scoped(ObjectA, + InitArg('db', database)) + """ :type: (objects.Provider) -> ObjectA """ + + +# Making scope using `with` statement. +with Catalog.object_a as object_a_provider: + object_a1 = object_a_provider() + object_a2 = object_a_provider() + + assert object_a1 is object_a2 + assert object_a1.db is object_a2.db + +# Making another one scope using `with` statement. +with Catalog.object_a as object_a_provider: + object_a3 = object_a_provider() + object_a4 = object_a_provider() + + assert object_a3 is object_a4 + assert object_a3.db is object_a4.db + + assert (object_a1 is not object_a3) and \ + (object_a1 is not object_a4) + assert (object_a2 is not object_a3) and \ + (object_a2 is not object_a4) + + +# Making one more scope using provider methods. +Catalog.object_a.in_scope() + +object_a5 = Catalog.object_a() +object_a6 = Catalog.object_a() + +assert object_a5 is object_a6 +assert object_a5.db is object_a6.db + +assert (object_a1 is not object_a3) and \ + (object_a1 is not object_a4) and \ + (object_a1 is not object_a5) and \ + (object_a1 is not object_a6) +assert (object_a2 is not object_a3) and \ + (object_a2 is not object_a4) and \ + (object_a2 is not object_a5) and \ + (object_a2 is not object_a6) +``` diff --git a/examples/concept.py b/examples/concept.py index 34714fd1..f44eb8fe 100644 --- a/examples/concept.py +++ b/examples/concept.py @@ -5,6 +5,7 @@ Concept example of objects catalogs. from objects import AbstractCatalog from objects.providers import Singleton, NewInstance from objects.injections import InitArg, Attribute + import sqlite3 diff --git a/examples/external_dependency.py b/examples/external_dependency.py index a19e0c95..b5ae4cc4 100644 --- a/examples/external_dependency.py +++ b/examples/external_dependency.py @@ -5,6 +5,7 @@ Concept example of objects catalogs. from objects import AbstractCatalog from objects.providers import Singleton, NewInstance, ExternalDependency from objects.injections import InitArg, Attribute + import sqlite3 diff --git a/examples/overrides.py b/examples/overrides.py index 6a6d5ebb..242ec45a 100644 --- a/examples/overrides.py +++ b/examples/overrides.py @@ -6,6 +6,7 @@ Concept example of objects overrides. from objects import AbstractCatalog, overrides from objects.providers import Singleton, NewInstance from objects.injections import InitArg, Attribute + import sqlite3 diff --git a/examples/scoped_provider.py b/examples/scoped_provider.py index 1a007bd0..45dd602c 100644 --- a/examples/scoped_provider.py +++ b/examples/scoped_provider.py @@ -5,6 +5,7 @@ Scoped provider examples. from objects import AbstractCatalog from objects.providers import Singleton, Scoped from objects.injections import InitArg, Attribute + import sqlite3 @@ -29,6 +30,7 @@ class Catalog(AbstractCatalog): """ :type: (objects.Provider) -> ObjectA """ +# Making scope using `with` statement. with Catalog.object_a as object_a_provider: object_a1 = object_a_provider() object_a2 = object_a_provider() @@ -36,6 +38,7 @@ with Catalog.object_a as object_a_provider: assert object_a1 is object_a2 assert object_a1.db is object_a2.db +# Making another one scope using `with` statement. with Catalog.object_a as object_a_provider: object_a3 = object_a_provider() object_a4 = object_a_provider() @@ -49,6 +52,7 @@ with Catalog.object_a as object_a_provider: (object_a2 is not object_a4) +# Making one more scope using provider methods. Catalog.object_a.in_scope() object_a5 = Catalog.object_a()