mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-11-04 01:47:36 +03:00 
			
		
		
		
	examples codestyle fixes
This commit is contained in:
		
							parent
							
								
									d48616736b
								
							
						
					
					
						commit
						4055b42e59
					
				| 
						 | 
					@ -1,40 +1,35 @@
 | 
				
			||||||
"""
 | 
					"""Callable provider examples."""
 | 
				
			||||||
Callable provider examples.
 | 
					
 | 
				
			||||||
"""
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
from objects import AbstractCatalog
 | 
					from objects import AbstractCatalog
 | 
				
			||||||
from objects.providers import (
 | 
					
 | 
				
			||||||
    Singleton,
 | 
					from objects.providers import Singleton
 | 
				
			||||||
    Callable,
 | 
					from objects.providers import Callable
 | 
				
			||||||
)
 | 
					
 | 
				
			||||||
from objects.injections import (
 | 
					from objects.injections import InitArg
 | 
				
			||||||
    Injection,
 | 
					from objects.injections import Attribute
 | 
				
			||||||
    InitArg,
 | 
					from objects.injections import Injection
 | 
				
			||||||
    Attribute,
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import sqlite3
 | 
					import sqlite3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Some example function.
 | 
					 | 
				
			||||||
def consuming_function(arg, db):
 | 
					def consuming_function(arg, db):
 | 
				
			||||||
 | 
					    """Example function that has input arg and dependency on database."""
 | 
				
			||||||
    return arg, db
 | 
					    return arg, db
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Catalog of objects providers.
 | 
					 | 
				
			||||||
class Catalog(AbstractCatalog):
 | 
					class Catalog(AbstractCatalog):
 | 
				
			||||||
    """
 | 
					
 | 
				
			||||||
    Objects catalog.
 | 
					    """Catalog of objects providers."""
 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    database = Singleton(sqlite3.Connection,
 | 
					    database = Singleton(sqlite3.Connection,
 | 
				
			||||||
                         InitArg('database', ':memory:'),
 | 
					                         InitArg('database', ':memory:'),
 | 
				
			||||||
                         Attribute('row_factory', sqlite3.Row))
 | 
					                         Attribute('row_factory', sqlite3.Row))
 | 
				
			||||||
    """ :type: (objects.Provider) -> sqlite3.Connection """
 | 
					    """:type: (objects.Provider) -> sqlite3.Connection"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    consuming_function = Callable(consuming_function,
 | 
					    consuming_function = Callable(consuming_function,
 | 
				
			||||||
                                  Injection('db', database))
 | 
					                                  Injection('db', database))
 | 
				
			||||||
    """ :type: (objects.Provider) -> consuming_function """
 | 
					    """:type: (objects.Provider) -> consuming_function"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Some calls.
 | 
					# Some calls.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,51 +1,53 @@
 | 
				
			||||||
"""
 | 
					"""Concept example of objects catalogs."""
 | 
				
			||||||
Concept example of objects catalogs.
 | 
					
 | 
				
			||||||
"""
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
from objects import AbstractCatalog
 | 
					from objects import AbstractCatalog
 | 
				
			||||||
from objects.providers import (
 | 
					
 | 
				
			||||||
    Singleton,
 | 
					from objects.providers import Singleton
 | 
				
			||||||
    NewInstance,
 | 
					from objects.providers import NewInstance
 | 
				
			||||||
)
 | 
					
 | 
				
			||||||
from objects.injections import (
 | 
					from objects.injections import InitArg
 | 
				
			||||||
    InitArg,
 | 
					from objects.injections import Attribute
 | 
				
			||||||
    Attribute,
 | 
					 | 
				
			||||||
)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import sqlite3
 | 
					import sqlite3
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Some example classes.
 | 
					 | 
				
			||||||
class ObjectA(object):
 | 
					class ObjectA(object):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    """Example class ObjectA, that has dependency on database."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, db):
 | 
					    def __init__(self, db):
 | 
				
			||||||
 | 
					        """Initializer."""
 | 
				
			||||||
        self.db = db
 | 
					        self.db = db
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class ObjectB(object):
 | 
					class ObjectB(object):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    """Example class ObjectB, that has dependencies on ObjectA and database."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def __init__(self, a, db):
 | 
					    def __init__(self, a, db):
 | 
				
			||||||
 | 
					        """Initializer."""
 | 
				
			||||||
        self.a = a
 | 
					        self.a = a
 | 
				
			||||||
        self.db = db
 | 
					        self.db = db
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Catalog of objects providers.
 | 
					 | 
				
			||||||
class Catalog(AbstractCatalog):
 | 
					class Catalog(AbstractCatalog):
 | 
				
			||||||
    """
 | 
					
 | 
				
			||||||
    Objects catalog.
 | 
					    """Catalog of objects providers."""
 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    database = Singleton(sqlite3.Connection,
 | 
					    database = Singleton(sqlite3.Connection,
 | 
				
			||||||
                         InitArg('database', ':memory:'),
 | 
					                         InitArg('database', ':memory:'),
 | 
				
			||||||
                         Attribute('row_factory', sqlite3.Row))
 | 
					                         Attribute('row_factory', sqlite3.Row))
 | 
				
			||||||
    """ :type: (objects.Provider) -> sqlite3.Connection """
 | 
					    """:type: (objects.Provider) -> sqlite3.Connection"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    object_a = NewInstance(ObjectA,
 | 
					    object_a = NewInstance(ObjectA,
 | 
				
			||||||
                           InitArg('db', database))
 | 
					                           InitArg('db', database))
 | 
				
			||||||
    """ :type: (objects.Provider) -> ObjectA """
 | 
					    """:type: (objects.Provider) -> ObjectA"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    object_b = NewInstance(ObjectB,
 | 
					    object_b = NewInstance(ObjectB,
 | 
				
			||||||
                           InitArg('a', object_a),
 | 
					                           InitArg('a', object_a),
 | 
				
			||||||
                           InitArg('db', database))
 | 
					                           InitArg('db', database))
 | 
				
			||||||
    """ :type: (objects.Provider) -> ObjectB """
 | 
					    """:type: (objects.Provider) -> ObjectB"""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Catalog static provides.
 | 
					# 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.
 | 
					# Dependencies injection (The Python Way) into class.
 | 
				
			||||||
class Consumer(object):
 | 
					class Consumer(object):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    """Example consumer class."""
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    dependencies = Catalog(Catalog.object_a,
 | 
					    dependencies = Catalog(Catalog.object_a,
 | 
				
			||||||
                           Catalog.object_b)
 | 
					                           Catalog.object_b)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    def test(self):
 | 
					    def example(self):
 | 
				
			||||||
 | 
					        """Example method."""
 | 
				
			||||||
        a1 = self.dependencies.object_a()
 | 
					        a1 = self.dependencies.object_a()
 | 
				
			||||||
        a2 = self.dependencies.object_a()
 | 
					        a2 = self.dependencies.object_a()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -83,12 +88,14 @@ class Consumer(object):
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            raise Exception('Database is not listed as a dependency')
 | 
					            raise Exception('Database is not listed as a dependency')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Consumer().test()
 | 
					
 | 
				
			||||||
 | 
					Consumer().example()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Dependencies injection (The Python Way) into a callback.
 | 
					# Dependencies injection (The Python Way) into a callback.
 | 
				
			||||||
def consumer_callback(dependencies=Catalog(Catalog.object_a,
 | 
					def consumer_callback(dependencies=Catalog(Catalog.object_a,
 | 
				
			||||||
                                           Catalog.object_b)):
 | 
					                                           Catalog.object_b)):
 | 
				
			||||||
 | 
					    """Example function."""
 | 
				
			||||||
    a1 = dependencies.object_a()
 | 
					    a1 = dependencies.object_a()
 | 
				
			||||||
    a2 = dependencies.object_a()
 | 
					    a2 = dependencies.object_a()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										23
									
								
								manage.py
									
									
									
									
									
								
							
							
						
						
									
										23
									
								
								manage.py
									
									
									
									
									
								
							| 
						 | 
					@ -1,6 +1,4 @@
 | 
				
			||||||
"""
 | 
					"""CLI Commands."""
 | 
				
			||||||
CLI Commands.
 | 
					 | 
				
			||||||
"""
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
from setup import version
 | 
					from setup import version
 | 
				
			||||||
| 
						 | 
					@ -12,9 +10,7 @@ manager = Manager()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@manager.command
 | 
					@manager.command
 | 
				
			||||||
def publish(with_tag=True):
 | 
					def publish(with_tag=True):
 | 
				
			||||||
    """
 | 
					    """Publish current version to PyPi."""
 | 
				
			||||||
    Publishes current version to PyPi.
 | 
					 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
    os.system('python setup.py sdist upload')
 | 
					    os.system('python setup.py sdist upload')
 | 
				
			||||||
    if with_tag:
 | 
					    if with_tag:
 | 
				
			||||||
        tag()
 | 
					        tag()
 | 
				
			||||||
| 
						 | 
					@ -22,12 +18,21 @@ def publish(with_tag=True):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@manager.command
 | 
					@manager.command
 | 
				
			||||||
def tag():
 | 
					def tag():
 | 
				
			||||||
    """
 | 
					    """Make tag from current version."""
 | 
				
			||||||
    Makes tag from current version.
 | 
					 | 
				
			||||||
    """
 | 
					 | 
				
			||||||
    os.system('git tag -a {0} -m \'version {0}\''.format(version))
 | 
					    os.system('git tag -a {0} -m \'version {0}\''.format(version))
 | 
				
			||||||
    os.system('git push --tags')
 | 
					    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__':
 | 
					if __name__ == '__main__':
 | 
				
			||||||
    manager.main()
 | 
					    manager.main()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user