Skip to main content

Utility to randomly sample lines from a file

Project description

LineSample

Utility to randomly sample lines from an input file.

This module implements a command-line utility that randomly samples lines from an input file. It is possible to sample lines by the probability of each line being selected or by giving an overall number of lines. In either case, the lines can be sampled with a single pass through the file, and the ordering of lines in the original file is preserved. For reproducibility, this module also supports seeding of the pseudorandom number generator used.

Only modules from the Python 3 standard library are used in this code. Type hints (from the typing module), which at the time of writing is still a provisional API, are also used.

Installation

Currently, this module is only available for Python 3. You can install it with pip install linesample.

Usage

To sample half of all lines from an input file foo.txt, run

linesample -f0.5 foo.txt

To sample up to 100 lines from bar.txt, run

linesample -n100 bar.txt

You must use one of the forms above. Specifying neither or both options will result in an error.

By default, the utility uses the system time as the seed for its pseudorandom number generator. You can choose your own seed using a form like this:

linesample -s42 -n100 baz.txt

For reference, the usage string is as follows:

usage: ['linesample.py', '-h'] [-h] [-s SEED] [-f FRACTION | -n NUMBER] infile

positional arguments:
  infile                input file

optional arguments:
  -h, --help            show this help message and exit
  -s SEED, --seed SEED  seed for random number generator
  -f FRACTION, --fraction FRACTION
                        fraction of lines to select
  -n NUMBER, --number NUMBER
                        number of lines to select

License

This module is distributed under the MIT License, whose text is listed both in the LICENSE file provided with this package and immediately below.

Copyright (c) 2019 Steve Matsumoto

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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

linesample-0.1.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

linesample-0.1.1-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file linesample-0.1.1.tar.gz.

File metadata

  • Download URL: linesample-0.1.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for linesample-0.1.1.tar.gz
Algorithm Hash digest
SHA256 aabd9f6c303a00fc5ab405557e803cd32adffaf02ceaaa549ddfd42a74b6104b
MD5 00f326c4745e5933192afc619edea61a
BLAKE2b-256 c7f8ab5993ea74e2a2ca00a195f9d7a2fd87d2bfcecc9c2ce041ab4dc28a068d

See more details on using hashes here.

File details

Details for the file linesample-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: linesample-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.7.3

File hashes

Hashes for linesample-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 38aab3fdcb979d903945b33fca2173730a07248917832d26f4fe06e110ac8144
MD5 ab502b96432f8c353c451ee1cbc0eb38
BLAKE2b-256 58f381fce917ea77bf95c46fac6fc54dcae0d8d6ec0e5aaf84c779fa2eaf724c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page