Skip to main content

A package that can be used to create a build process

Project description

py-build

A package that includes a class for building applications using python

Usage

The builder uses decorators to designate build steps:

import py_build

# Create the builder object
builder = py_build.Builder()

# Add steps to the builder
@builder.build_step()
def step1():
    return "Output from step 1"

@builder.build_step()
def step2():
    return "Output from step 2"

Call each step to run the build:

step1()
step2()

Capturing build step results

The output from build steps (or other processes from inside a build step) can be captured using the Builder().capture_results decorator method.

This method accepts a list of Callable's that will be called in the order given with the return value from function decorated.

The following code will print the results from each build step on screen:

import py_build

def output_fnc(output: str):
    print(output)

# Create the builder object
builder = py_build.Builder()

# Add steps to the builder
@builder.build_step()
@builder.capture_results(output_fnc)
def step1():
    return "Output from step 1"

@builder.build_step()
@builder.capture_results(output_fnc)
def step2():
    return "Output from step 2"

step1()
step2()

# Prints:
# Output from step 1
# Output from step 2

Progress Reporting

The progress of the build can be captured as well using the Builder().capture_progress decorator method. This method accepts a list of Callable's that accept a float between 0 and 1

The following code will print the progress of the build in percentile:

import py_build

def progress_fnc(progress: float):
    print(round(progress * 100))

# Create the builder object
builder = py_build.Builder()

# Add steps to the builder
@builder.build_step()
@builder.capture_progress(progress_fnc)
def step1():
    return "Output from step 1"

@builder.build_step()
@builder.capture_progress(progress_fnc)
def step2():
    return "Output from step 2"

step1()
step2()

# Prints:
# 50
# 100

Composed Decorators

The builder can also combine decorators into a single decorator. This can be useful for preventing repeat code like in the examples above. It can also be used to differentiate main steps from substeps and the like.

The following code will print the results and progress from each step:

import py_build

def progress_fnc(progress: float):
    print(round(progress * 100))

def output_fnc(output: str):
    print(output)

# Create the builder object
builder = py_build.Builder()

main_step = builder.composed(
    @builder.build_step(),
    @builder.capture_progress(progress_fnc),
    @builder.capture_results(output_fnc)
)

# Add steps to the builder
@main_step
def step1():
    return "Output from step 1"

@main_step
def step2():
    return "Output from step 2"

step1()
step2()

# Prints:
# Output from step 1
# 50
# Output from step 2
# 100

The Builder().build() method

We can also make the builder run steps sequentially with a single method call.

This example does the same thing as the previous example, but uses the Builder().build() method to execute all the build steps:

import py_build

def progress_fnc(progress: float):
    print(round(progress * 100))

def output_fnc(output: str):
    print(output)

# Create the builder object
builder = py_build.Builder()

main_step = builder.composed(
    @builder.build_step(),
    @builder.capture_progress(progress_fnc),
    @builder.capture_results(output_fnc)
)

# Add steps to the builder
@main_step
def step1():
    return "Output from step 1"

@main_step
def step2():
    return "Output from step 2"

builder.build()

# Prints:
# Output from step 1
# 50
# Output from step 2
# 100

There are a few caveats to note about the above example:

  • The Builder().build_step() must be the first decorator for a function for the example to work correctly
  • Build steps that require arguments should be defined as keyword arguments at definition
  • Builder instances are callable, so calling Builder()() is the same as calling Builder().build().

Sub steps

The Builder().build() method can also handle steps that have a sub step. These can be designed as such:

import py_build

def progress_fnc(progress: float):
    print(round(progress * 100))

def output_fnc(output: str):
    print(output)

# Create the builder object
builder = py_build.Builder()

main_step = builder.composed(
    @builder.build_step(),
    @builder.capture_progress(progress_fnc),
    @builder.capture_results(output_fnc)
)

@main_step
def get_hello():
    """Get the salulation"""
    return "Hello"

@main_step
def sub_step():
    new_builder = py_build.Builder()
    substep = new_builder.composed(
        new_builder.build_step(),
        new_builder.capture_progress(progress_fnc), # We will use the same progress reporting method as in main steps
        new_builder.capture_results(output_fnc), # We will use the same result reporting method as in main steps
    )
    @substep
    def sub_step_1():
        """Get the sub step"""
        return "Sub step 1"
    @substep
    def sub_step_2():
        """Get the sub step"""
        return "Sub step 2"
    # Returning a builder object indicates that the step has its own steps
    return new_builder

@main_step
def get_name(name='Cory'):
    """Get the name"""
    return name

builder.build()

# Prints:
# Hello
# 33
# Sub step 1
# 50
# Sub step 2
# 67
# Cory
# 100

Release Notes

There are several changes that may break compatibility between version 0.1.0 and 0.2.0 as outlined below:

  • Builder().composed() has been renamed to Builder().composed_step() and its functionality has changed

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

py-build-0.2.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

py_build-0.2.0-py3-none-any.whl (9.6 kB view details)

Uploaded Python 3

File details

Details for the file py-build-0.2.0.tar.gz.

File metadata

  • Download URL: py-build-0.2.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.3

File hashes

Hashes for py-build-0.2.0.tar.gz
Algorithm Hash digest
SHA256 164ad027859d3386f62418321a774cc2aa548764f38f654bbf2beb28ce006bcf
MD5 dbf10963160f689f27391d4740b9d8bb
BLAKE2b-256 9574c72df09816d54f3e1cec99f70dd79b42e89dfee8531cba77deb1418165a8

See more details on using hashes here.

File details

Details for the file py_build-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: py_build-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 9.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.3

File hashes

Hashes for py_build-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5b3a915fde0bafc58bc6e2b499fd3e8a2fb6bd7379465c078d3776e284a58458
MD5 24cf1c9a74d49e6f0f2244601cf35850
BLAKE2b-256 c40ff67635e77adc227509fc5a11e3ce9ffe66cdeb27246ad0c3ce3a12e04851

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