Skip to main content

A collection of Ruby behaviour ported to Python.

Project description

Pyby

PyPI PyPI - Python Version PyPI - Status PyPI - License Python package

Lecture material for implementing certain Ruby-like behaviours in Python.

NB: This is heavily under development and subject to change. Expect breaking changes until the 1.0.0 release.

Current Functionality

The links in the list of available modules, classes, methods, and functions below link to the corresponding Ruby documentation.

RObject (object.py)

A base class to enrich Python objects with additional functionality.

respond_to

Determines whether an object has a certain callable property or not.
Also available as a standalone function, rather than a method.


Enumerable (enumerable.py)

A base class meant to be subclassed by an iterable.
The iterable must implement __each__, __into__, and to_enum in order to unlock the rest of the functionality. In addition, the iterable may implement __to_tuple__ in order to support predicate and mapping functions with a higher arity than one. A prime example would be EnumerableDict in combination with select where the

__each__ (internal)

Returns an iterator to be used internally.
Must be implemented by the subclass.

__into__ (internal)

Returns a constructor that accepts an iterable for the given method name.
Must be implemented by the subclass.

__to_tuple__ (internal)

Transforms a single element of an enumerable to a tuple.
Used internally to uniformly handle predicate and mapping functions with a higher arity than one.
May be implemented by the subclass.

as_enum (internal)

A decorator used internally to enable the return type of a method to be configured by the collection class inheriting from Enumerable. Relys on __into__.

collect, map

Returns the result of mapping a function over the elements.
The mapping function takes a single argument for sequences and two arguments for mappings.

compact

Returns an enumerable of the elements with None values removed.

each

Given a function, calls the function once for each item in the enumerable.
For sequences this will typically be the same as iterating over the elements, whereas for mappings this will be same as iterating over the items.

Without a function, returns an enumerator by calling to_enum.

filter, select

Returns the elements for which the predicate function is truthy.
Without a function, returns an enumerator by calling to_enum.

The predicate function takes a single argument for sequences and two arguments for mappings.

first

Returns the first element or a given number of elements.
With no argument, returns the first element, or None if there is none.
With a number of elements requested, returns as many elements as possible.

take

Returns the number of elements requested or as many elements as possible.

to_enum

Returns an Enumerator for the enumerable.
Must be implemented by the subclass due to cyclical dependencies.


Enumerator (enumerator.py)

A class which allows both internal and external iteration.
An enumerator is in turn an enumerable.

next

Returns the next object in the enumeration sequence.
If going beyond the enumeration, StopIteration is raised.

peek

Returns the current object in the enumeration sequence without advancing the enumeration.
If going beyond the enumeration, StopIteration is raised.

rewind

Rewinds the enumeration sequence to the beginning.

Note that this may not be possible to do for underlying iterables that can be exhausted.

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

pyby-0.2.1.tar.gz (7.0 kB view details)

Uploaded Source

File details

Details for the file pyby-0.2.1.tar.gz.

File metadata

  • Download URL: pyby-0.2.1.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.10.0 keyring/23.4.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for pyby-0.2.1.tar.gz
Algorithm Hash digest
SHA256 51cd56273eb0a7e4ad94aade06301c6b455b184180265637b62b55c69e7f4167
MD5 4b4a3c0bbfc852d0a9aa05ec03a38507
BLAKE2b-256 358c6d7113d036f7268e7ac3f1e41a566cd837c86aecf72819f049ea25616c41

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