Skip to main content

A simple decorator for returning a small sample of items from a list.

Project description

S A M P L R

@samplr is a tiny decorator that provides options for returning a subset of items from a list. It also prints useful information about this sampling process to the terminal for debugging.

Installation

Install samplr using pip:

pip install samplr

Usage

To use, decorate a function that returns a list with @samplr(). For example:

from samplr import samplr

@samplr(count=3)
def get_data():
    # ... code that returns a list of 10 items of data ...

get_data() will now return a list containing just the first 3 items of the original return list, instead of all 100.

The arguments of samplr() are:

  • count (optional, default 'all'): The number of items to return. This can be an integer or the string 'all'.

    • If count is a positive integer, the decorated function's returned list will be that length.

    • If count is 'all', all items will be returned.

    • If count is a negative integer that many items will be ommited from the returned list.

    Also note:

    • If count > len(original_list), count = 'all'

    • If count < -len(original_list), count = 0

  • sample_type (optional, default 'head'): The method for selecting the subset of items. The available options are:

    • 'head': Select the first count items in the list.

    • 'tail': Select the last count items in the list.

    • 'random': Sample a random subset of count items from the list.

  • expand (optional, default True): Whether to expand each item in the console print output. Helps with the readability of deeply nested objects.

  • info (optional, default True): Whether to output to the console additional information about the sampling process.

Examples

  1. An example of using @samplr to select a random subset of 3 items from a list returned by a function:
from samplr import samplr

@samplr(count=3, sample_type='random')
def get_data():
    return [
        {'name': 'Alice', 'age': 32},
        {'name': 'Bob', 'age': 25},
        {'name': 'Charlie', 'age': 41},
        {'name': 'David', 'age': 19},
        {'name': 'Eve', 'age': 56},
        {'name': 'Frank', 'age': 28},
        {'name': 'Grace', 'age': 39},
        {'name': 'Heidi', 'age': 22},
        {'name': 'Isaac', 'age': 47},
        {'name': 'Jen', 'age': 31},
    ]

sampled_data = get_data()

This will return a list of items and output something like this to the terminal:

        S A M P L R        

 @samplr(count = 3, sample_type = random)
  get_data()
 items returned: 3/10

 0: : {
    "name": "Alice",
    "age": 32
}
 3: : {
    "name": "David",
    "age": 19
}
 4: : {
    "name": "Eve",
    "age": 56
}

        end        

Note the enumeration in the printout shows each item's index in the original list.


  1. An example of using @samplr to exclude the last 2 items (with expand and info set to False):
@samplr(count=-2, sample_type='tail', expand=False, info=False)
def get_data():
    return ["zero", "one", "two", "three", "four", "five", "six"]

This returns a list of length 5, and outputs a shorter statement to the terminal:

 0: : "zero"
 1: : "one"
 2: : "two"
 3: : "three"
 4: : "four"

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

samplr-1.0.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

samplr-1.0.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file samplr-1.0.0.tar.gz.

File metadata

  • Download URL: samplr-1.0.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for samplr-1.0.0.tar.gz
Algorithm Hash digest
SHA256 aa35e9e5487b8e0f70d74e97ca61120f46c0e1885a55dc36d308527ec8e2ff07
MD5 7a6e37406091d0ce4522f0968806d709
BLAKE2b-256 cdba64f8415827f7be8f4ee6cff4cc38f4a835f5d0b1bf05ec394b6c647b69dd

See more details on using hashes here.

File details

Details for the file samplr-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: samplr-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for samplr-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9276a633a746c5ac17cd07fa1417e1d77c46a7584334114b675ad69928fd2c73
MD5 4b2337837ef39f376177dee746849d27
BLAKE2b-256 f793e4d02ff3516c5cc727feee70ef6732f14f3bf533cebaa157aac9ce63d670

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