Skip to main content

A toolset for piping values between functions, with optional shell-like text-processing tools.

Project description

A toolset to enable unix-style value piping (now with text processing!)

Introduction

pypes is a Python package designed to simplify the process of piping values between functions and provide optional shell-like text-processing tools.

The PipableMixin class provides features to make any class pipable, in the style of traditional shells such as Bourne and Zsh.

The package's second most important feature is the Receiver, which allows you to easily chain functions and pass values between them.

Additionally, the pypes.text module offers utilities for shell-like text processing and manipulation.

Installation

To install pypes, use pip:

pip install pypes

Usage

PipableMixin

The PipableMixin is the most important tool in pypes. It allows add pipe-based chaining to all methods of the inheriting class. Here's an example of how to use it:

from pypes.mixins import Pipable
from pypes.typing import wrap_object

def double(value):
    return value * 2

# Pipe the value 5 to the "double" function
result = wrap_object(5, Pipable) | double
# result = 5 * 2 = 10

Receiver Class

The Receiver class is used to create a "receiving" function call. By placing a receiver object on the right side of a pipe, the callable defined in the receiver can be deferred rather than called at time of evaluation. This enables any function or method to receive the value from the left side of a pipe, without needing to create a pipable object.

Here's an example of how to use it:

from pypes.mixins import Receiver

# Create a "receiver-function" by returning a Receiver with the desired operation
def raise_power(value, power):
    return value ** power

# Pipe the value 5 to the receiver object
result = 5 | Receiver(raise_power, 2)

# result = 5 ** 2 = 25

Alternatively, a Receiver can be assigned to a variable, and that variable used as the target of a pipe:

from pypes.mixins import Receiver

def add(value, other):
    return value + other

# Create a receiver object with the `add` callable and a fixed value
add_three = Receiver(add, 3)

# Pipe the value 5 to the receiver object
result = 5 | add_three
# result = 5 + 3 = 8

Text Module

The pypes.text module provides utility functions for text processing and manipulation.

from pypes.text import cat, grep, sed

jabberwocky = cat('jabberwocky.txt')
# “Beware the Jabberwock, my son!
# The jaws that bite, the claws that catch!
# Beware the Jubjub bird, and shun
# The frumious Bandersnatch!” 

# Pipe the Text object through the grep function with the argument 'beware'
#   then replace the RegEx pattern `,?\s` with a single underscore
result = jabberwocky | grep('beware') | sed(',?\s', '_') > 'beware.txt'

# The result will be saved into a new file 'beware.txt'
# Output: Beware_the_Jabberwock_my_son!
#         Beware_the_Jubjub_bird_and_shun

Printer Utilities

The pypes.printers module provides some simple methods for printing and decorating multi-line strings. Here's an example of how to use it:

from pypes.printers import mprint

result = mprint("""
    This is a
        multi-line
    string.
    """)
# This is a
#     multi-line
# string.

In this example, we also show that the mprint has a unique style of dedenting that uses the whitespace of the final line ( """) as a guide for how much whitespace to remove. If the final line does not consist solely of horizontal whitespace, then the textwrap.dedent() function is used instead.

pypes Typing

Finally, the pypes.typing module provides a few additional tools that may be useful outside of piping context.

Some of the available functions include:

  • isinstance(obj, types): A replacement for the built-in isinstance, this function accepts subscripted types and type-tuples.
  • get_parent_class(obj): Returns the class that an instance belongs to. This works even on builtins and nested classes. This is especially useful for determining the parent class of a method, where the __class__ attribute would typically return a method_descriptor type.
  • wrap_object(obj, *mixins, attrs): Dynamically cast an object as a subclass that includes the mixins.
  • class_path(obj): Get the fully qualified name of the class that __obj belongs to, e.g. class_path(Text()) returns "pypes.text.Text".

Here's an example of how to use these functions:

<-- TODO: add some examples -->

License

pypes is licensed under the MIT License. See the LICENSE file for more information.

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

pypes-0.3.1.tar.gz (23.6 kB view details)

Uploaded Source

Built Distribution

pypes-0.3.1-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file pypes-0.3.1.tar.gz.

File metadata

  • Download URL: pypes-0.3.1.tar.gz
  • Upload date:
  • Size: 23.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for pypes-0.3.1.tar.gz
Algorithm Hash digest
SHA256 8df00e688942d63bd1606c6734031647dceabf7e36c549a6ecbe733e08e18baa
MD5 f2af75e21b4629028d4f0cebb8278b98
BLAKE2b-256 9c0eb7ee6ae80870aecb84eb2d4db2069fe84bb9ffc4d9bfbd1f7b1082c6d3a4

See more details on using hashes here.

File details

Details for the file pypes-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: pypes-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 24.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.27.0

File hashes

Hashes for pypes-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 788b48e4672a1f3c49af6e2c661d721751ecba3d0bd641af8977783f2d76b4be
MD5 f2a525f8e780a1b46cd85b262a1309cb
BLAKE2b-256 119f62df63e31c2e30914a2950fff5b6f595ddfc5c09c655a19c8b8b407799bc

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