Skip to main content

Stuff to do with counters, sequences and iterables.

Function first(iterable)

Return the first item from an iterable; raise IndexError on empty iterables.

Function get0(iterable, default=None)

Return first element of an iterable, or the default.

Function imerge(*iters, **kw)

Merge an iterable of ordered iterables in order.

Parameters:

  • iters: an iterable of iterators
  • reverse: if true, yield items in reverse order. This requires the iterables themselves to also be in reversed order.

This function relies on the source iterables being ordered and their elements being comparable, through slightly misordered iterables (for example, as extracted from web server logs) will produce only slightly misordered results, as the merging is done on the basis of the front elements of each iterable.

Function isordered(s, reverse=False, strict=False)

Test whether an iterable is ordered. Note that the iterable is iterated, so this is a destructive test for nonsequences.

Function last(iterable)

Return the last item from an iterable; raise IndexError on empty iterables.

Function onetomany(func)

A decorator for a method of a sequence to merge the results of passing every element of the sequence to the function, expecting multiple values back. Example: class X(list): @onetomany def chars(self, item): return item strs = X(['Abc', 'Def']) all_chars = X.chars()

Function onetoone(func)

A decorator for a method of a sequence to merge the results of passing every element of the sequence to the function, expecting a single value back. Example: class X(list): @onetoone def lower(self, item): return item.lower() strs = X(['Abc', 'Def']) lower_strs = X.lower()

Class Seq

A thread safe wrapper for itertools.count().

Function seq()

Return a new sequential value.

Class StatefulIterator

A trivial iterator which wraps another iterator to expose some tracking state.

This has 2 attributes: .it, the internal iterator which should yield (item, new_state) .state, the last state value from the internal iterator

The originating use case is resuse of an iterator by independent calls that are typically sequential, specificly the .read method of file like objects. Naive sequential reads require the underlying storage to locate the data on every call, even though the previous call has just performed this task for the previous read. Saving the iterator used from the preceeding call allows the iterator to pick up directly if the file offset hasn't been fiddled in the meantime.

Function tee(iterable, *Qs)

A generator yielding the items from an iterable which also copies those items to a series of queues.

Parameters:

  • iterable: the iterable to copy
  • Qs: the queues, objects accepting a .put method.

Note: the item is .put onto every queue before being yielded from this generator.

Function the(iterable, context=None)

Returns the first element of an iterable, but requires there to be exactly one.

Class TrackingCounter

A wrapper for a counter which can be incremented and decremented. A facility is provided to wait for the counter to reach a specific value. The .inc and .dec methods also accept a tag argument to keep individual counts based on the tag to aid debugging. TODO: add strict option to error and abort if any counter tries to go below zero.

Release files for cs-seq 20190101

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

Source distribution (sdist)

Source distribution for cs-seq 20190101
File Size Uploaded
cs.seq-20190101.tar.gz 5.2 kB Details

Release files / cs.seq-20190101.tar.gz

Download URL cs.seq-20190101.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
21ebe01d38bf9ac9089ddb6ad0702fa062b18f7ea5768895e3236a4183162c85
BLAKE2b-256 checksum
How to use checksums
5f260ccd0daf14880a5ced724442f395cd74ed8cbfaf125414ef64d24fe2ca94
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.11.0 pkginfo/1.4.2 requests/2.18.4 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.23.0 CPython/3.6.6
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