semvec/pypi-packages huggingface dataset:
abstractfactory 1.0.3
Abstract Factory functionality for Python
Keywords: abstract-factory,patterns

Abstract Factory for Python
===========================

Copyright (c) 2016-2017 David Betz

|Build Status| |PyPI version|

See test\_provider.py unit test for usage.

Basically an implementation of an abstract factory pattern.

In one system where I use this, I create factories for eachs type of
thing in my system. So, SearchFactory, CloudStorageFactory,
QueueFactory, AristotleFactory, etc... These would implement for ID
interface like ICloudStorageProvider (in Node, it's just a class).

Each of these would have their own switch/case (or whatever) to create
the factory for it. So, for example, I may have config in a YAML file
specifying that I want to use Mongo for my Aristotle provider
("Aristotle" is what most people incorrectly call "NoSQL").

To begin, create the factory (do this one for the entirety of your
system):

::

    abstractFactory = AbstractFactory()

Then, add your factories:

::

        abstractFactory.set(SearchFactory)
        abstractFactory.set(CloudStorageFactory)
        abstractFactory.set(QueueFactory)
        abstractFactory.set(AristotleFactory)

When the time comes, just ask for your provider:

::

    provider = abstractFactory.resolve(IAristotleProvider)

Your code SHOULD. NOT. CARE. ABOUT. MONGO. It should the your
configuration or something handle that. Don't tightly couple your
providers.

Also note that the resolver also accepts various arguments for extra
flexibility:

::

    provider = abstractFactory.resolve(IAristotleProvider, "alternateConnectionString", collection="log") 

Despite what random bloggers say, service locators are awesome and
provide excellent decoupling.

Look at the ``Mock`` examples provided with the tests; they're rather
extensive.

.. |Build Status| image:: https://travis-ci.org/davidbetz/pyabstractfactory.svg?branch=master
   :target: https://travis-ci.org/davidbetz/pyabstractfactory
.. |PyPI version| image:: https://badge.fury.io/py/abstractfactory.svg
   :target: https://badge.fury.io/py/abstractfactory
middleware 1.2.3
Python module to add general middleware
Keywords: middleware

Middleware for Python
=====================

**Copyright (c) 2016 David Betz**

Installation
------------

::

    pip install middleware

|Build Status| |PyPI version|

Compatibility
-------------

Python 2 and 3

Purpose
-------

Most everyone needs a concept of middleware.

Following are examples of using this, see ``test_middleware.py`` for
full examples.

::

    class AdditionMiddleware1(Middleware):
        def create(self):
            def func(mwa, context):
                try:
                    counter = context['counter']
                except:
                    counter = 0
                context['counter'] = counter + 1

                return next(mwa)

            return func

When using a class, add a ``create`` function which returns a function.
This inner fuction accepts the middleware array and the data context and
returns ``next(mwa)`` to create a middleware chain.

For this example, I'll add two more:

::

    class AdditionMiddleware2(AdditionMiddleware1):
        pass


    class AdditionMiddleware3(AdditionMiddleware2):
        pass

Now to run it. Use ``set`` to set an array of middleware and ``add`` to
add one to the array. ``set`` overwrites everything. That's just what
``set`` means.

::

        handler = Handler()
        handler.set([AdditionMiddleware1, AdditionMiddleware2])
        handler.add(AdditionMiddleware3)
        handler.execute()

        # handler['counter'] == 3

In this case, there is no initial context and each of the three
middleware increment a counter ending with ``handler['counter'] == 3``.

You can skip the entire class stuff too:

::

    handler = Handler()
    def inline(wma, context):
        context['myvalue'] = 12
    handler.add(inline)
    handler.execute()
    # handler['myvalue'] == 12

Use the following to send initial context:

::

    handler = Handler(counter=1)

It's actually ``kwargs``, so you can load it up:

::

    handler = Handler(**{'a': 1, 'b': 2})
    def inline(wma, context):
        context['a'] = context['a'] + context['b']
    handler.add(inline)
    handler.execute()

.. |Build Status| image:: https://travis-ci.org/davidbetz/pymiddleware.svg?branch=master
   :target: https://travis-ci.org/davidbetz/pymiddleware
.. |PyPI version| image:: https://badge.fury.io/py/middleware.svg
   :target: https://badge.fury.io/py/middleware