A collection of Ruby behaviour ported to Python.
Project description
Pyby
A library 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.
Installation
Install the pyby
package from PyPI.
pip install pyby
Current Functionality
The links in the list of available modules, classes, methods, and functions below link to the corresponding Ruby documentation.
Currently, Ruby version 3.1.1 is used as a basis for the mimiced functionality.
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.
send
Calls the property identified by name, passing it any arguments specified. If the property is not callable and no arguments are specified, the property is instead returned.
Enumerable
(enumerable.py)
A base class meant to be subclassed by an iterable (henceforth referred to as an enumerable).
The enumerable must implement __each__
in order to unlock the rest of the functionality.
To return something else than an EnumerableList
, the enumerable can override __into__
. For example, EnumerableDict
returns another EnumerableDict
when its compact
method is called.
In addition, the enumerable may override __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 predicate function should expect the key-value pair split into two arguments rather than a single tuple. In this case, __to_tuple__
should return the key-value pair as a two-element tuple.
__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.
By default imports and returns EnumerableList
.
May 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.
By default returns the item wrapped in a single-element tuple.
May be implemented by the subclass.
configure
(internal)
A decorator enabling the return type of a method, as well as the number of arguments predicate and mapping functions are to be called with, to be configured by the collection class inheriting from Enumerable.
If enumerator_without_func
is set, the decorator skips calling the decorated method if no arguments have been passed and instead returns an Enumerator based on the enumerable.
Relys on the enumerable's implementation of __into__
and __to_tuple__
.
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.
count
Returns the number of elements in the enumerable.
Optionally accepts an argument.
Given a non-callable argument, counts the number of equivalent elements.
Given a callable predicate, counts the elements for which the predicate is truthy.
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.
The predicate function takes a single argument for sequences and two arguments for mappings.
Without a predicate function, returns an enumerator by calling to_enum.
find
Returns the first element for which the predicate function is truthy. If no matching element is found, returns None
, unless a default callback has been provided, in which turn that callback function will be invoked and its result returned. The predicate function takes a single argument for sequences and two arguments for mappings.
If called with a single argument, treats it as the predicate function.
If called with two arguments, the first is treated as the default callback function and the second argument acts as the predicate function.
Without a predicate function, returns an enumerator by calling to_enum.
inject
, reduce
Performs a reduction operation much like functools.reduce
.
If called with a single argument, treats it as the reduction function.
If called with two arguments, the first is treated as the initial value for the reduction and the second argument acts as the reduction function.
Also available as the alias reduce
.
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.
reject
Returns the elements for which the predicate function is falsy.
Without a predicate function, returns an enumerator by calling to_enum.
take
Returns the number of elements requested or as many elements as possible.
to_enum
Returns an Enumerator
for the enumerable.
Requires an iterable subclass.
EnumerableDict
(enumerable_dict.py)
A subclass of Enumerable
that mimics some of Ruby's Hash
while still behaving like a Python dict
.
EnumerableList
(enumerable_list.py)
A subclass of Enumerable
that mimics some of Ruby's Array
while still behaving like a Python list
.
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
Built Distribution
File details
Details for the file pyby-0.4.0.tar.gz
.
File metadata
- Download URL: pyby-0.4.0.tar.gz
- Upload date:
- Size: 9.5 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd791fd3a5e076a702f0ea87a1bda9753b3f35e3ebe9ea2c51b59fe03b2060ec |
|
MD5 | d8657dc74c5f5b99ef684d59c88a2baa |
|
BLAKE2b-256 | 8a815f6b19caa161e568b7dcd5cf00609f65485111b0b84881b9653f135799fb |
File details
Details for the file pyby-0.4.0-py3-none-any.whl
.
File metadata
- Download URL: pyby-0.4.0-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- 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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c8eeffda1affb4bea0a3081e7116093af6f86ba5d7096b76d1718ec71d4bc12 |
|
MD5 | bfb737724960394dc607aa012bfafc6f |
|
BLAKE2b-256 | 219f43fd1ca0c856ce7d7713c722dff02497a273a4791ff41c5419b782ebdd3f |