This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

Library containing useful functions for manipulating iterables.

Note

This code is deprecated, don’t use it! Use toolz.itertoolz instead.

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.
Release History

Release History

0.1.0

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
jk-0.1.0.tar.gz (14.3 kB) Copy SHA256 Checksum SHA256 Source Nov 22, 2014

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting