examples codestyle fixes

This commit is contained in:
Roman Mogilatov 2015-03-10 00:54:05 +02:00
parent d48616736b
commit 4055b42e59
3 changed files with 56 additions and 49 deletions

View File

@ -1,40 +1,35 @@
"""
Callable provider examples.
"""
"""Callable provider examples."""
from objects import AbstractCatalog
from objects.providers import (
Singleton,
Callable,
)
from objects.injections import (
Injection,
InitArg,
Attribute,
)
from objects.providers import Singleton
from objects.providers import Callable
from objects.injections import InitArg
from objects.injections import Attribute
from objects.injections import Injection
import sqlite3
# Some example function.
def consuming_function(arg, db):
"""Example function that has input arg and dependency on database."""
return arg, 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"""
consuming_function = Callable(consuming_function,
Injection('db', database))
""" :type: (objects.Provider) -> consuming_function """
""":type: (objects.Provider) -> consuming_function"""
# Some calls.

View File

@ -1,51 +1,53 @@
"""
Concept example of objects catalogs.
"""
"""Concept example of objects catalogs."""
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):
"""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 = 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 = NewInstance(ObjectB,
InitArg('a', object_a),
InitArg('db', database))
""" :type: (objects.Provider) -> ObjectB """
""":type: (objects.Provider) -> ObjectB"""
# Catalog static provides.
@ -61,10 +63,13 @@ 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):
"""Example consumer class."""
dependencies = Catalog(Catalog.object_a,
Catalog.object_b)
def test(self):
def example(self):
"""Example method."""
a1 = self.dependencies.object_a()
a2 = self.dependencies.object_a()
@ -83,12 +88,14 @@ class Consumer(object):
else:
raise Exception('Database is not listed as a dependency')
Consumer().test()
Consumer().example()
# Dependencies injection (The Python Way) into a callback.
def consumer_callback(dependencies=Catalog(Catalog.object_a,
Catalog.object_b)):
"""Example function."""
a1 = dependencies.object_a()
a2 = dependencies.object_a()

View File

@ -1,6 +1,4 @@
"""
CLI Commands.
"""
"""CLI Commands."""
import os
from setup import version
@ -12,9 +10,7 @@ manager = Manager()
@manager.command
def publish(with_tag=True):
"""
Publishes current version to PyPi.
"""
"""Publish current version to PyPi."""
os.system('python setup.py sdist upload')
if with_tag:
tag()
@ -22,12 +18,21 @@ def publish(with_tag=True):
@manager.command
def tag():
"""
Makes tag from current version.
"""
"""Make tag from current version."""
os.system('git tag -a {0} -m \'version {0}\''.format(version))
os.system('git push --tags')
@manager.command
def check():
"""Check `objects` library and examples with code analyzers."""
os.system('pylint objects/')
os.system('flake8 objects/')
os.system('pep257 objects/')
os.system('pylint examples/')
os.system('flake8 examples/')
os.system('pep257 examples/')
if __name__ == '__main__':
manager.main()