Skip to main content

A Railway pattern based operation package

Project description

Operation

Railway Oriented Programming implementation for Python.

Basic Usage

  1. Create a class that inherit from Operation, with phases method:
from operation import Operation


class SomeOperation(Operation):
    def phases(self):
        return [
            'phase_one',
            'phase_two',
            'phase_three'
        ]

    def phase_one(self):
        self.options['option_for_second_phase'] = True

    def phase_two(self):
        if 'option_for_second_phase' in self.options:
            return 'an option got passed from first phase, cool!'

    def phase_three(self):
        self.options['reached_third_phase'] = True
  1. in the operation manager, use SomeOperation(options).run(), where options are the input to the operation flow.

Example

For example, if you create an operation that first connects to a database, then fetches the a document by an id, validates it, and finally send a post request of the entry's user_id, it can look like this:

import requests
from pymongo import MongoClient

from operation import Operation


class RetrieveDataAndPost(Operation):
    def phases(self):
        return [
            'connect_to_mongo',
            'fetch_document',
            'validate_document',
            'post_results'
        ]

    def connect_to_mongo(self):
        self.options['albums_collection'] = MongoClient('<MongoDB_URL>').albums

    def fetch_document(self):
        album_id = self.options.get('album_id')
        self.options['album'] = self.options['albums_collection'].find_one({ 'id': album_id })

    def validate_document(self):
        if 'artist_name' not in self.options['album']:
            self.fail_operation()

    def post_results(self):
        selected_artist =  self.options.get('album').get('artist_name')
        requests.post('<Artists_Service_URL>', data={'selected_artist': selected_artist})

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

operation-0.0.2.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

operation-0.0.2-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: operation-0.0.2.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for operation-0.0.2.tar.gz
Algorithm Hash digest
SHA256 8ed235bd719febf187a611ce3025c2575e7ca225c7480702b1262697bdf4960c
MD5 11292a1f4a950f6a76cf26e9b8609a62
BLAKE2b-256 93c5f161cee3f50efc0ee216fe63b7f8a7960ef501f47438783e12a16c1512f0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: operation-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for operation-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b27765da86215536b8a09f955d61a5ec376adc32396729571daf9528d38947ef
MD5 89414d03416d646e12a3ca5fac6a4861
BLAKE2b-256 5e0b9dc36f911304a04a26bacc6c2c93b123f2cd2957484d1afcf1fbdf2fa2cf

See more details on using hashes here.

Supported by

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