Skip to main content

Method variant decorator

Project description

varmeth

Method Variant Decorator

Python Versions PyPI version github actions License: GPLv3 Code style: black

Simple library that allows a method to choose dynamically implementation at runtime depending on the context via decorators.

Varmeth was originally part of ManageIQ Integration test library.

Installation and usage

Installation

varmeth can be installed by running pip install varmeth

Usage

Below example can show you how to use varmeth. You can see a different method variant. You’ll have to decorate default method with variable and register it with different method variant. You can also register variant with multiple names.

The following code snippet shows how to use varmeth in a real world example:

In this example, the tiger method will change it's implementation based on the context at runtime, so it must be annotated with the @variable annotation in order to do so. It will be the variable method.

The body of this method will be the default implementation - the implementation used for this method when no context is explicitly used.

The siberian_tiger and bengal_tiger are two different implementations for the tiger method, and need to be annotated with @tiger.variant("variant-name") annotations, where variant-name is a string identifier that will be used at runtime to select the required variant implementation. These will be the variants.

Note that the variable method can be associated with multiple implementations or variants.

from varmeth import variable

class CatFamily(object):
   @variable
   def tiger(self):
       print("Default Method!")
       print("Tiger")

   @tiger.variant("siberian")
   def siberian_tiger(self):
       print("Siberian Tiger")

   @tiger.variant("indian", "bengal")
   def bengal_tiger(self):
       print("Bengal Tiger")

To choose between the different variants , the method parameter is used to select the proper context, using the proper variant-name as a value.

In [1]: cat = CatFamily()

In [2]: cat.tiger()
Default Method!
Tiger

In [3]: cat.tiger(method="siberian")
Siberian Tiger

In [4]: cat.tiger(method="indian")
Bengal Tiger

In [5]: cat.tiger(method="bengal")
Bengal Tiger

You can also add and alias name to the default method using the alias parameter, though note that only one default method is allowed.

from varmeth import variable

class Reptiles(object):
   @variable(alias="python")
   def snake(self):
       print("Python Snake")

   @snake.variant("kobra")
   def kobra_snake(self):
       print("Kobra Snake")
In [1]: rep = Reptiles()

In [2]: rep.snake()
Python Snake

In [3]: rep.snake(method="python")
Python Snake

In [4]: rep.snake(method="kobra")
Kobra Snake

Using Varmeth against plain Python implementation

The following example shows an Entity class that supports the delete operation for two different contexts, the UI (front-end) and the REST (back-end) contexts. As you can infer, each context requires very different implementations to get the entity removed.

Using vanilla Python implementation you will have to call the proper method for each context, explicitly. Instead, you can simply call the same method and provide the context, and Varmeth will do the rest.

Plain Python Varmeth
class Entity(object):
    def delete_ui(self):
        print("Delete with UI!")

    def delete_rest(self):
        print("Delete with REST!")

entity = Entity()
entity.delete_ui()      # >> Delete with UI!
entity.delete_rest()    # >> Delete with REST!
from varmeth import variable

class Entity(object):
    @variable(alias="ui")
    def delete(self):
        print("Delete with UI!")

    @delete.variant("rest")
    def delete_rest(self):
        print("Delete with REST!")

entity = Entity()
entity.delete()                 # >> Delete with UI!
entity.delete(method="ui")      # >> Delete with UI!
entity.delete(method="rest")    # >> Delete with REST!

As you can see, Varmeth provides a very convenient context switcher interface, which some may find handy when implementing integration tests designed to follow test parametrization patterns, like some popular test frameworks such as Pytest. offer. The following is an example of how to do exactly that with Pytest using Varmeth: we can easily parametrize the context under test using UI and REST as parameters.

import pytest

@pytest.mark.parametrize("context", ["ui", "rest"])
def test_delete_entity(context):
   entity = Entity()
   entity.delete(method=context)

Contribute

Feel free to create Issues if you find bugs, or go ahead and submit your own Pull Requests.

Please note: When submitting new PRs, ensure your code passes all checks.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

varmeth-0.0.2.tar.gz (21.2 kB view details)

Uploaded Source

Built Distribution

varmeth-0.0.2-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file varmeth-0.0.2.tar.gz.

File metadata

  • Download URL: varmeth-0.0.2.tar.gz
  • Upload date:
  • Size: 21.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for varmeth-0.0.2.tar.gz
Algorithm Hash digest
SHA256 f0e17d62503b3a076f115e83fa698098418801a166755bdb2166b6243f7b6438
MD5 5c560408873abb230a1dc992c450742e
BLAKE2b-256 672b4776db6a34291c85c1e08913ac3a09f4340de64212f96c0b55584970605d

See more details on using hashes here.

File details

Details for the file varmeth-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: varmeth-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for varmeth-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 5b34f17c82c5ee97fa0d899d0db9238b88d19b5118e58391f1c17a93d3f67d5e
MD5 d383c1070e9144f58273d1b993d4f118
BLAKE2b-256 0165548ebe1f90822c17c613d1972ee2ec74215946c41246f24172f4d8c5cb52

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page