Skip to main content

Library containing useful functions for manipulating iterables

Project description

https://badge.fury.io/py/jk.png https://travis-ci.org/eliasdorneles/jk.png?branch=master https://pypip.in/d/jk/badge.png

Library containing useful functions for manipulating iterables.

Inspiration was drawn from Python itertools’ recipes and Ruby’s Enumerable API.

Works on Python 2.6+ and 3.x.

Features

each_cons

  • each_cons(sequence, size)

Iterates lazily through a sequence, yielding a sliding window with the given size for each iteration.

Examples:

Calculating quarterly sales report:

>>> import jk
>>> month_sales = [123.45, 54.3, 428.1, 144.2, 245.45, 197.3]
>>> for a, b, c in jk.each_cons(month_sales, 3):
...     print '%0.2f' % ((a + b + c)/3)
...
201.95
208.87
272.58
195.65

Find duplicated lines in a file:

>>> lines = """here is a simple
... file for us to test.
... this line repeats
... this line repeats
... -- this one does not
... this one repeats too
... this one repeats too
... okay, we're done here""".split('\n')
>>>
>>> for ln, (a, b) in enumerate(jk.each_cons(lines, 2), 1):
...     if a == b:
...         print (ln, a)
...
(3, 'this line repeats')
(6, 'this one repeats too')

slice_before and slice_after

  • slice_before(predicate, sequence)

  • slice_after(predicate, sequence)

These functions are useful when you have a stream that has some sort of delimiter. Handy for parsing log files, for example.

They iterate lazily through a sequence, yielding a tuple containing the elements sliced just before (or after) the predicate evaluates to True.

The predicate argument can also be a string or a regular expression pattern to be matched against the sequence elements.

Examples:

Grouping numbers until reaching zero:

>>> numbers = [1, 2, 3, 0, 4, 5, 0, 6, 0, 7, 8]
>>> print list(jk.slice_after(lambda x: x == 0, numbers))
[(1, 2, 3, 0), (4, 5, 0), (6, 0), (7, 8)]

Reading entries of a fantasy multiline log file:

>>> log_lines = """START: initiating...
... kernel found
... EVENT: started
... moving on
... EVENT: something happened
... EVENT: another thing happened""".split('\n')
>>>
>>> for entry in jk.slice_before('^(START|EVENT):', log_lines):
...     print entry
...
('START: initiating...', 'kernel found')
('EVENT: started', 'moving on')
('EVENT: something happened',)
('EVENT: another thing happened',)

first, second and nth

  • first(sequence, default=None)

Returns the first element of a sequence (or a default value if the sequence is empty).

  • second(sequence, default=None)

Returns the second element of a sequence (or a default value if not exists).

  • nth(sequence, n, default=None)

Returns the nth element of a sequence (or a default value if not exists).

Note that the argument n is not a zero-based index: it is a ordinal number, so n=1 means the first element, n=4 means the fourth and so on.

History

0.1.0 (2014-11-23)

  • First release on PyPI.

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

jk-0.1.0.tar.gz (14.3 kB view details)

Uploaded Source

File details

Details for the file jk-0.1.0.tar.gz.

File metadata

  • Download URL: jk-0.1.0.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for jk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8d9d1cd49a2ebc9b03c312a582fb5c339881362ceb8ff82463ac189fdc014c47
MD5 c956717951dc880b22718ae81e9987ac
BLAKE2b-256 2b03058205838ea6739b3ed9cf67485bc6b28a197450c26a06c0aa47a6c27d38

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