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 Convert an Element’s Type

To change the data type of individual elements, use the map() method to apply a type to each element.

In the following example, we convert string elements into the float type, map(float):

>>> import squint
>>>
>>> select = squint.Select('example.csv')
>>>
>>> select('C').map(float)
Query(<squint.Select object at 0x7fcaac15>, ['C']).map(float)
---- preview ----
[20.0, 30.0, 10.0, 20.0, 10.0, 10.0]

In the preview above, we see that every element in column C has been converted into a float value.

How To Convert a Container’s Type

While you can control a container’s type during selection, there are times when you will want to convert a container’s type after selection. To do this, use the apply() method to apply a container type to the entire group of elements.

In the following example, we convert a list of elements into a tuple of elements, apply(tuple):

>>> import squint
>>>
>>> select = squint.Select('example.csv')
>>>
>>> select('A').apply(tuple)
Query(<squint.Select object at 0x7f8ed8b6>, ['A']).apply(tuple)
---- preview ----
('x', 'x', 'y', 'y', 'z', 'z')

In the preview above, we see that our query returns a tuple instead of a list.

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.).