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.3.tar.gz (3.4 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.3-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: operation-0.0.3.tar.gz
  • Upload date:
  • Size: 3.4 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.3.tar.gz
Algorithm Hash digest
SHA256 f8e32882c88cd47d9a82e0299a7863e27c789b8509279f72d75eda7da55ad0a7
MD5 3af45b30ca4095fa31fc746b8677218c
BLAKE2b-256 d1fd7d113654597af15cdbfc26a06f7ec64753287b7b8601d2b59225b7be7751

See more details on using hashes here.

File details

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

File metadata

  • Download URL: operation-0.0.3-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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2b165b0f0dea8a1e1f1d3574fafc83069f3b0044b557afb187ec217391ce7b55
MD5 e64c289a85b006be6d50aa5b5c5eb778
BLAKE2b-256 73a4162f90fedb0ed5621202161dbdeed48e3b14a1c7c8ab30416722bb7a598a

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