Skip to main content

Slinkie

Method chains for Python 3.6 and up.

Installation

Requirements

  • Python 3.6 and up

$ pip install slinkie

Usage

Example

In this example we have a collection of animals. We go through all of them, and pat only the good dogs.

from slinkie import Slinkie

animals = [lassie, skippy, scooby, cerberus, garfield]

(
    Slinkie(animals)
        .filter(lambda animal: animal.type == 'dog')
        .filter(lambda dog: dog.is_good)
        .map(lambda dog: dog.pat())
        .consume()
)

In this example we have a collection of animals. We go through and select only the good dogs, and collect them in a list.

from slinkie import Slinkie

animals = [lassie, skippy, scooby, cerberus, garfield]

good_dogs = (
    Slinkie(animals)
        .filter(lambda animal: animal.type == 'dog')
        .filter(lambda dog: dog.is_good)
        .list() 
)

Functions

  • all(key=None): Consumes the whole slinkie to find if every item is truthy.
  • any(key=None): Consumes the slinkie until it finds a truthy item.
  • between(a, b, key=None): Returns items between a and b. (Inclusive).
  • consume(n=None): Consume n items. If n is None, consume everything.
  • count(): Consumes all items to produce a count.
  • dict(key, transform=None): Returns a dict of all items.
  • exclude(items, key=None): Excludes all items based on either their identity, or a key function.
  • extend(items): Yields all the items from this._items, followed by the items supplied to this function.
  • filter(key): Filter the items.
  • first(key=None): Take the first item if key is None, otherwise take the first item where key(item) returns true. If there are no objects, StopIteration is raised.
  • first_or_none(key=None): Take the first item if key is None, otherwise take the first item where key(item) returns true. If there are no objects, None is returned.
  • flatten(): Flatten a two-dimensional result set into a single dimension.
  • foldl(fn, default=None): Fold left. Same as reduce.
  • foldr(fn, default=None): Fold right.
  • group(key): Groups all items on key.
  • intersperse(divider): Intersperses the items with the divider. Slinkie([1, 2, 3]).intersperse('x').list() -> [1, 'x', 2, 'x', 3].
  • intersperse_items(dividers): Intersperses the items with the dividers, one by one. Slinkie([1, 2, 3, 4, 5]).intersperse_items(['x', 'y']).list() -> [1, 'x', 2, 'y', 3, 'x', 4, 'y', 5].
  • join(glue=''): Joins the items by glue, where glue is a string. Calls glue.join.
  • last(key=None): Take the last item if key is None, otherwise take the first item where key(item) returns true. If there are no objects, StopIteration is raised.
  • last_or_none(key=None): Take the last item if key is None, otherwise take the last item where key(item) returns true. If there are no matching objects, None is returned.
  • len(): Consumes all items to produce a count.
  • list(): Returns a list of all items.
  • map(transform, with_index=False): Map the items.
  • not_none(): Returns all items except None.
  • parallelize(fn, number_of_threads=None): Parallelize a function call. Number of threads defaults to your cpu count + 1.
  • partition(n): Takes n items and returns them in a new Slinkie. Does so until the items are consumed.
  • reduce(fn, default=None): Fold left. Same as reduce.
  • reverse(): Reverses the order of the Slinkie.
  • select(transform, with_index=False): Map the items.
  • set(): Returns a set of all items.
  • sfilter(key): Filter the items, uses the splat operator on the key function, just like smap.
  • skip(n): Skip n items.
  • smap(transform): Map the splat of the items.
  • sort(key=None, reverse=False): Sorts the items by key.
  • split(number_of_slinkies=2): Split the items into smaller slinkies. Items are divided using round robin.
  • str(glue=''): Joins the items by glue, where glue is a string. Calls glue.join.
  • sweep(width, step=1): Similar to itertools' pairwise, this will hand out width number of items at a time, with an offset of step. Slinkie(range(11)).sweep(2) yields the same result as itertools.pairwise, while .sweep(3) would give you (0, 1, 2), (1, 2, 3), ... (8, 9, 10). The last item may be None-padded if there were not step items left in the Slinkie.
  • take(n): Take n items.
  • tee(display=None): Every item that falls through the tee function will be displayed using the display function. If none is supplied, print is used.
  • then(fn): Takes a function that takes a slinkie as its only argument, and returns a collection. The collection is then wrapped in another slinkie.
  • transpose(): Transposes the contents of a Slinkie.
  • tuple(): Returns a tuple of all items.
  • unique(key=None): Filter out items that aren't considered unique. You can optionally supply a key function to determine the identity.
  • where(key): Filter the items.

Development

There are no external dependencies at all, by design. I still recommend making a virtualenv to work in, however.

$ virtualenv venv
$ source venv/bin/activate
$ pip install -e .

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update (or write) tests as appropriate.

License

BSD 2-Clause License

Release files for slinkie 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for slinkie 1.1.0
File Size Uploaded
slinkie-1.1.0.tar.gz 7.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for slinkie 1.1.0
File Interpreter ABI Platform
slinkie-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.4 kB

Release files / slinkie-1.1.0.tar.gz

Download URL slinkie-1.1.0.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
0f7389819e149dd5dd6cba33def105aa4e8fbf90a482b3330a1bc95e5b430a94
BLAKE2b-256 checksum
How to use checksums
dcb612a6d2062db36a35cb979fac5e922b0f3ebfdaabd8e4164f9df5a3c5d28d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.5

Release files / slinkie-1.1.0-py3-none-any.whl

Download URL slinkie-1.1.0-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
170fe2fec70cc0e84f9019f487d4474c66425a391bbd8f910153ab7a216333e7
BLAKE2b-256 checksum
How to use checksums
8296a32dd49b28c1639439fe0633927a69a38740de7a476086f6fd6ab4fb6b80
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.7.5

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.11

2 release files

0.1.10

2 release files

0.1.9

2 release files

0.1.8

2 release files

0.1.7

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.0

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page