Skip to main content

A typesafe list with more method chaining!

Project description

Slist

This is a drop in replacement for the built-in mutable python list

But with more post-fixed methods for chaining in a typesafe manner!!

Leverage the latest pyright features to spot errors during coding.

All these methods return a new list. They do not mutate the original list.

Not able to convince your colleagues to use immutable functional data structures? I understand.
This library lets you still have the benefits of typesafe chaining methods while letting your colleagues have their mutable lists!

pypi python Build Status

pip install slist

Quick Start

Easily spot errors when you call the wrong methods on your sequence with mypy.

from slist import Slist

many_strings = Slist(["Lucy, Damion, Jon"])  # Slist[str]
many_strings.sum()  # Mypy errors with 'Invalid self argument'. You can't sum a sequence of strings!

many_nums = Slist([1, 1.2])
assert many_nums.sum() == 2.2  # ok!

class CannotSortMe:
    def __init__(self, value: int):
        self.value: int = value

stuff = Slist([CannotSortMe(value=1), CannotSortMe(value=1)])
stuff.sort_by(lambda x: x)  # Mypy errors with 'Cannot be "CannotSortMe"'. There isn't a way to sort by the class itself
stuff.sort_by(lambda x: x.value)  # ok! You can sort by the value

Slist([{"i am a dict": "value"}]).distinct_by(
    lambda x: x
)  # Mypy errors with 'Cannot be Dict[str, str]. You can't hash a dict itself

Slist provides methods to easily flatten and infer the types of your data.

from slist import Slist, List
from typing import Optional

test_optional: Slist[Optional[int]] = Slist([-1, 0, 1]).map(
    lambda x: x if x >= 0 else None
)
# Mypy infers slist[int] correctly
test_flattened: Slist[int] = test_optional.flatten_option()


test_nested: Slist[List[str]] = Slist([["bob"], ["dylan", "chan"]])
# Mypy infers slist[str] correctly
test_flattened_str: Slist[str] = test_nested.flatten_list()

There are plenty more methods to explore!

from slist import Slist

result = (
    Slist([1, 2, 3])
    .repeat_until_size_or_raise(20)
    .grouped(2)
    .map(lambda inner_list: inner_list[0] + inner_list[1] if inner_list.length == 2 else inner_list[0])
    .flatten_option()
    .distinct_by(lambda x: x)
    .map(str)
    .reversed()
    .mk_string(sep=",")
)
assert result == "5,4,3"

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

slist-0.3.17.tar.gz (21.1 kB view details)

Uploaded Source

Built Distribution

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

slist-0.3.17-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file slist-0.3.17.tar.gz.

File metadata

  • Download URL: slist-0.3.17.tar.gz
  • Upload date:
  • Size: 21.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for slist-0.3.17.tar.gz
Algorithm Hash digest
SHA256 79dece0cbebcbe373e110769c11d00418d63092b7817f84809dc3dbcaefca600
MD5 5e5b5cfa92ea2e8e6bb41a55f353b53b
BLAKE2b-256 4f87927e03ff4f2f5d07f80ebe796062cf104c3ddc7c449a803782434eab20fd

See more details on using hashes here.

Provenance

The following attestation bundles were made for slist-0.3.17.tar.gz:

Publisher: release.yml on thejaminator/slist

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file slist-0.3.17-py3-none-any.whl.

File metadata

  • Download URL: slist-0.3.17-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for slist-0.3.17-py3-none-any.whl
Algorithm Hash digest
SHA256 1ba0e32cf825d3ec40323cc634032a6508fca109ab35840ef3a0947d8b09b142
MD5 b25db1dabb40be9b0d7628b88576abc6
BLAKE2b-256 9864450bef22a823f5dc9ef69bbe569e31c89e7aaa1e6d09702f97e7845869f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for slist-0.3.17-py3-none-any.whl:

Publisher: release.yml on thejaminator/slist

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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