No project description provided
Project description
Nonstring Collections
This package provides utilities for working with non-string iterable containers.
Module Functions
unique
This function will remove repeated items from arguments while preserving order.
>>> from nonstring import unique
>>> unique(1, 2, 3, 2, 3, 3, 1, 4, 2, 5, 5)
[1, 2, 3, 4, 5]
>>> unique(5, 5, 2, 4, 1, 3, 3, 2, 3, 2, 1)
[5, 2, 4, 1, 3]
It will treat a single argument as atomic (i.e., unseparable) unless the
separable keyword is set to true.
>>> unique('aabac')
['aabac']
>>> unique('aabac', separable=True)
['a', 'b', 'c']
unwrap
This function will remove redundant outer lists and tuples.
It can unwrap numbers enclosed in increasingly deeper lists:
>>> from nonstring import unwrap
>>> cases = [[1], [[2]], [[[3]]], [[[[4]]]]]
>>> for case in cases:
... print(unwrap(case))
...
1
2
3
4
It preserves numbers and strings that are already unwrapped:
>>> unwrap(42)
42
>>> unwrap('string')
'string'
Passing a type to newtype ensures a result of that type:
>>> unwrap(42, newtype=tuple)
(42,)
>>> unwrap(42, newtype=list)
[42]
>>> unwrap([42], newtype=list)
[42]
>>> unwrap(([(42,)],), newtype=list)
[42]
It works with multiple wrapped elements:
>>> unwrap([1, 2])
[1, 2]
>>> unwrap([[1, 2]])
[1, 2]
>>> unwrap(['one', 'two'])
['one', 'two']
>>> unwrap([['one', 'two']])
['one', 'two']
It stops at an empty list or tuple:
>>> unwrap([])
[]
>>> unwrap(())
()
>>> unwrap(list())
[]
>>> unwrap(tuple())
()
>>> unwrap([[]])
[]
>>> unwrap([()])
()
>>> unwrap([], newtype=tuple)
()
wrap
This function will wrap the argument in a list, if necessary.
>>> from nonstring import wrap
>>> wrap(1)
[1]
>>> wrap([1])
[1]
>>> wrap((1,))
[1]
>>> wrap([[1]])
[[1]]
isseparable
This function will return True if the argument is iterable but is not string-like.
>>> from nonstring import isseparable
>>> isseparable('ab')
False
>>> isseparable(['a', 'b'])
True
Wrapper
An instance of this class represents an iterable collection with members that
have meaning independent of any other members. When initialized with a
"separable" object (e.g., a list, tuple, or set), the new instance will
behave like the equivalent tuple. When initialized with a non-"separable"
object, the new instance will behave like a tuple containing that object.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nonstring-0.3.0.tar.gz.
File metadata
- Download URL: nonstring-0.3.0.tar.gz
- Upload date:
- Size: 5.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.13 Linux/5.15.0-112-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
987ee664208ef1163dcdc8fc6f2428f0ca9704db1ea1bd931c12699a4d078701
|
|
| MD5 |
1a0fd2c3302a7d2803297e40dfa8570f
|
|
| BLAKE2b-256 |
509ecb1be4f870cf4fd4e453e3a295298211bcafe5c930c0d20e89cfb8f8aef6
|
File details
Details for the file nonstring-0.3.0-py3-none-any.whl.
File metadata
- Download URL: nonstring-0.3.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.1 CPython/3.10.13 Linux/5.15.0-112-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cda4dfe54488978186d336286d27e4a9c3b78ca4426465efcd86f8a08e29030e
|
|
| MD5 |
f6ab81a7958bbe3222be155a3e007363
|
|
| BLAKE2b-256 |
f26c216bea2d9a5c025e5e430966d1371579969311ece26ffe1287be61f87f31
|