How-to Guide

Many of the following sections use the example CSV from the tutorials. You can download it here:

How to Install Squint

The Squint package is tested on Python 2.7, 3.4 through 3.8, PyPy, and PyPy3; and is freely available under the Apache License, version 2.

The easiest way to install squint is to use pip:

pip install squint

To upgrade an existing installation, use the “--upgrade” option:

pip install --upgrade squint

The development repository for squint is hosted on GitHub. If you need bug-fixes or features that are not available in the current stable release, you can “pip install” the development version directly from GitHub:

pip install --upgrade https://github.com/shawnbrown/squint/archive/master.zip

All of the usual caveats for a development install should apply—only use this version if you can risk some instability or if you know exactly what you’re doing. While care is taken to never break the build, it can happen.

How To Select Single-Item Inner-Containers

To specify a single-item inner-container, you must provide both inner- and outer-types explicitly.

For example, select single-item sets of elements from column B, [{'B'}]:

>>> import squint
>>>
>>> select = squint.Select('example.csv')
>>>
>>> select([{'B'}])
Query(<squint.Select object at 0x7ff9292f>, [{'B'}])
---- preview ----
[{'foo'}, {'foo'}, {'foo'}, {'bar'}, {'bar'}, {'bar'}]

This is necessary because a single-item container—when used by itself—specifies an outer-container type. You cannot use the implicit list shorthand demonstrated elsewhere in the documentation.

How To Select Exotic Data Types

Most examples demonstrate the use of squint’s Select class with list, tuple and set types, but it’s possible to use a wide variety of other containers, too. For instance, frozensets, deques, namedtuples, etc. can be used the same way you would use any of the previously mentioned types.

For example, select a deque of namedtuple elements from columns A and B, deque([ntup('A', 'B')]):

>>> from collections import deque
>>> from collections import namedtuple
>>> import squint
>>>
>>> select = squint.Select('example.csv')
>>>
>>> ntup = namedtuple('ntup', ['first', 'second'])
>>>
>>> select(deque([ntup('A', 'B')]))
Query(<squint.Select object at 0x7f4cf01c>, deque([ntup(first='A', second='B')]))
---- preview ----
deque([ntup(first='x', second='foo'), ntup(first='x', second='foo'),
       ntup(first='y', second='foo'), ntup(first='y', second='bar'),
       ntup(first='z', second='bar'), ntup(first='z', second='bar')])

Note

You can mix and match container types as desired, but the normal object limitations still apply. For example, sets and dictionary keys can only contain immutable types (like str, tuple, frozenset, etc.).