[](https://travis-ci.org/ramonski/underpy)
```
_
_ _ _ __ __| | ___ _ __ _ __ _ _
| | | | '_ \ / _` |/ _ \ '__| '_ \| | | |
| |_| | | | | (_| | __/ | | |_) | |_| |
\__,_|_| |_|\__,_|\___|_| | .__/ \__, |
|_| |___/
functional helpers for python
```
## Functions
``` python
alias(col, mapping)
Returns a collection of dictionaries with the keys renamed according to
the mapping
>>> libraries = [{"isbn": 1, "ed": 1}, {"isbn": 2, "ed": 2}]
>>> alias(libraries, {"ed": "edition"})
[{'edition': 1, 'isbn': 1}, {'edition': 2, 'isbn': 2}]
>>> alias({"a": 1}, {"a": "b"})
[{'b': 1}]
convert(value, converter)
Converts a value with a given converter function.
>>> convert("1", to_int)
1
>>> convert("0", to_int)
0
>>> convert("a", to_int)
fail(error)
Raises a RuntimeError with the given error Message
>>> fail("This failed badly")
Traceback (most recent call last):
...
RuntimeError: This failed badly
falsy(thing)
checks if a value is False or None
>>> falsy(0)
False
>>> falsy({})
False
>>> falsy([])
False
>>> falsy(None)
True
>>> falsy(False)
True
first(lst, n=None)
get the first element of a list
>>> lst = [1, 2, 3, 4, 5]
>>> first(lst)
1
>>> first(lst, 3)
[1, 2, 3]
is_dict(thing)
checks if an object is a dictionary type
>>> is_dict({})
True
>>> is_dict(dict())
True
>>> is_dict("{}")
False
>>> is_dict([])
False
is_digit(thing)
checks if an object is a digit
>>> is_digit(1)
True
>>> is_digit("1")
True
>>> is_digit("a")
False
>>> is_digit([])
False
is_list(thing)
checks if an object is a list type
>>> is_list([])
True
>>> is_list(list())
True
>>> is_list("[]")
False
>>> is_list({})
False
is_string(thing)
checks if an object is a string/unicode type
>>> is_string("")
True
>>> is_string(u"")
True
>>> is_string(str())
True
>>> is_string(unicode())
True
>>> is_string(1)
False
is_tuple(thing)
checks if an object is a tuple type
>>> is_tuple(())
True
>>> is_tuple(tuple())
True
>>> is_tuple("()")
False
>>> is_tuple([])
False
omit(dct, *keys)
Returns a copy of the dictionary filtered to omit the blacklisted keys
(or list of keys)
>>> omit({"name": "moe", "age": 50, "userid": "moe1"}, "userid", "age")
{'name': 'moe'}
pick(dct, *keys)
Returns a copy of the dictionary filtered to only have values for the
whitelisted keys (or list of valid keys)
>>> pick({"name": "moe", "age": 50, "userid": "moe1"}, "name", "age")
{'age': 50, 'name': 'moe'}
pluck(col, key, default=None)
Extracts a list of values from a collection of dictionaries
>>> stooges = [{"name": "moe", "age": 40},
... {"name": "larry", "age": 50},
... {"name": "curly", "age": 60}]
>>> pluck(stooges, "name")
['moe', 'larry', 'curly']
It only works with collections
>>> curly = stooges.pop()
>>> pluck(curly, "age")
Traceback (most recent call last):
...
RuntimeError: First argument must be a list or tuple
rename(dct, mapping)
Rename the keys of a dictionary with the given mapping
>>> rename({"a": 1, "BBB": 2}, {"a": "AAA"})
{'AAA': 1, 'BBB': 2}
to_int(thing)
coverts an object to int
>>> to_int("0")
0
>>> to_int(1)
1
>>> to_int("1")
1
>>> to_int("a")
to_list(thing)
converts an object to a list
>>> to_list(1)
[1]
>>> to_list([1,2,3])
[1, 2, 3]
>>> to_list(("a", "b", "c"))
['a', 'b', 'c']
>>> to_list(dict(a=1, b=2))
[{'a': 1, 'b': 2}]
to_string(thing)
coverts an object to string
>>> to_string(1)
'1'
>>> to_string([])
'[]'
>>> to_string(u"a")
'a'
to_iso_date(thing):
converts an object to a iso date string
>>> to_iso_date("")
''
>>> dt = datetime.date.fromtimestamp(1387452665)
>>> to_iso_date(dt)
'2013-12-19'
truthy(thing)
checks if a value is True or not None
>>> truthy(0)
True
>>> truthy({})
True
>>> truthy([])
True
>>> truthy(None)
False
>>> truthy(False)
False
```
## Running the tests
```
python setup.py nosetests --with-doctest
```
## License
MIT
Metadata
Release files for underpy 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| underpy-0.1.2.tar.gz | 130.7 kB | Without provenance | Details for underpy-0.1.2.tar.gz |
Release files / underpy-0.1.2.tar.gz
| Download URL | underpy-0.1.2.tar.gz |
|---|---|
| Size | 130.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b4d460eabf3f8a6ddfc378eca47a03a98f3be2eb18a105adb3b1b1cecb29b34f
|
|
BLAKE2b-256 checksum How to use checksums |
5c4a23e464fae0ba7134ae4a613e59647697e7bb71af7da693b3999fa71d4379
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for underpy 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.