ebbe


Nameebbe JSON
Version 1.13.2 PyPI version JSON
download
home_pagehttp://github.com/Yomguithereal/ebbe
SummaryCollection of typical helper functions for python.
upload_time2023-07-17 15:40:19
maintainer
docs_urlNone
authorGuillaume Plique
requires_python>=3.5
licenseMIT
keywords iter
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![Build Status](https://github.com/Yomguithereal/ebbe/workflows/Tests/badge.svg)](https://github.com/Yomguithereal/ebbe/actions)

# Ebbe

A collection of typical helper functions for python that cannot be found in the however great standard library.

## Installation

You can install `ebbe` with pip with the following command:

```
pip install ebbe
```

## Usage

*Iterator functions*

* [as_chunks](#as_chunks)
* [as_grams](#as_grams)
* [fail_fast](#fail_fast)
* [uniq](#uniq)
* [distinct](#distinct)
* [with_prev](#with_prev)
* [with_prev_and_next](#with_prev_and_next)
* [with_next](#with_next)
* [with_is_first](#with_is_first)
* [with_is_last](#with_is_last)
* [without_first](#without_first)
* [without_last](#without_last)

*Utilities*

* [get](#get)
* [getter](#getter)
* [getpath](#getpath)
* [pathgetter](#pathgetter)
* [indexed](#indexed)
* [grouped](#grouped)
* [partitioned](#partitioned)
* [sorted_uniq](#sorted_uniq)
* [pick](#pick)
* [omit](#omit)

*Functional Programming*

* [noop](#noop)
* [compose](#compose)
* [rcompose](#rcompose)

*Formatting*

* [and_join](#and_join)
* [format_int](#format_int)
* [format_time](#format_time)

TODO: *format_repr*, *format_filesize*

*Decorators*

* [decorators.fail_fast](#decoratorsfail_fast)
* [decorators.with_defer](#decoratorswith_defer)

*Benchmarking*

* [Timer](#timer)

### as_chunks

Iterate over chunks of the desired size by grouping items as we iterate over them.

```python
from ebbe import as_chunks

list(as_chunks(3, [1, 2, 3, 4, 5]))
>>> [[1, 2, 3], [4, 5]]
```

### as_grams

Iterate over grams (sometimes called n-grams or q-grams etc.) of the given iterable. It works with strings, lists and other sized sequences as well as with lazy iterables without consuming any superfluous memory while doing so.

```python
from ebbe import as_grams

list(as_grams(3, 'hello'))
>>> ['hel', 'ell', 'llo']

list(as_grams(2, (i * 2 for i in range(5))))
>>> [(0, 2), (2, 4), (4, 6), (6, 8)]
```

### fail_fast

Take an iterable (but this has been geared towards generators, mostly), and tries to access the first value to see if an Exception will be raised before returning an equivalent iterator.

This is useful with some badly-conceived generators that checks arguments and raise if they are not valid, for instance, and if you don't want to wrap the whole iteration block within a try/except.

This logic is also available as a [decorator](#failfastdecorator).

```python
from ebbe import fail_fast

def hellraiser(n):
  if n > 10:
    raise TypeError

  yield from range(n)

# You will need to do this to catch the error:
gen = hellraiser(15)

try:
  for i in gen:
    print(i)
except TypeError:
  print('Something went wrong when creating the generator')

# With fail_fast
try:
  gen = fail_fast(hellraiser(15))
except TypeError:
  print('Something went wrong when creating the generator')

for i in gen:
  print(i)
```

### uniq

Filter repeated items, optionally by key, seen next to each other in the given iterator.

```python
from ebbe import uniq

list(uniq([1, 1, 1, 2, 3, 4, 4, 5, 5, 6]))
>>> [1, 2, 3, 4, 5, 6]

# BEWARE: it does not try to remember items (like the `uniq` command)
list(uniq([1, 2, 2, 3, 2]))
>>> [1, 2, 3, 2]

# Using a key
list(uniq([(1, 2), (1, 3), (2, 4)], key=lambda x: x[0]))
>>> [(1, 2), (2, 4)]
```

### distinct

Filter repeated items, optionally by key, in the given iterator.

```python
from ebbe import distinct

list(distinct([0, 3, 4, 4, 1, 0, 3]))
>>> [0, 3, 4, 1]

list(distinct(range(6), key=lambda x: x % 2))
>>> [0, 1]
```

### with_prev

Iterate over items along with the previous one.

```python
from ebbe import with_prev

for previous_item, item in with_prev(iterable):
  print(previous_item, 'came before', item)

list(with_prev([1, 2, 3]))
>>> [(None, 1), (1, 2), (2, 3)]
```

### with_prev_and_next

Iterate over items along with the previous and the next one.

```python
from ebbe import with_prev_and_next

for previous_item, item, next_item in with_prev_and_next(iterable):
  print(previous_item, 'came before', item)
  print(next_item, 'will come after', item)

list(with_prev_and_next([1, 2, 3]))
>>> [(None, 1, 2), (1, 2, 3), (2, 3, None)]
```

### with_next

Iterate over items along with the next one.

```python
from ebbe import with_next

for item, next_item in with_next(iterable):
  print(next_item, 'will come after', item)

list(with_next([1, 2, 3]))
>>> [(1, 2), (2, 3), (3, None)]
```

### with_is_first

Iterate over items along with the information that the current item is the first one or not.

```python
from ebbe import with_is_first

for is_first, item in with_is_first(iterable):
  if is_first:
    print(item, 'is first')
  else:
    print(item, 'is not first')

list(with_is_first([1, 2, 3]))
>>> [(True, 1), (False, 2), (False, 3)]
```

### with_is_last

Iterate over items along with the information that the current item is the last one or not.

```python
from ebbe import with_is_last

for is_last, item in with_is_last(iterable):
  if is_last:
    print(item, 'is last')
  else:
    print(item, 'is not last')

list(with_is_last([1, 2, 3]))
>>> [(False, 1), (False, 2), (True, 3)]
```

### without_first

Iterate over the given iterator after skipping its first item. Can be useful if you want to skip headers of a CSV file for instance.

```python
from ebbe import without_first

list(without_first([1, 2, 3]))
>>> [2, 3]

for row in without_first(csv.reader(f)):
  print(row)
```

### without_last

Iterate over the given iterator but skipping its last item.

```python
from ebbe import without_last

list(without_last([1, 2, 3]))
>>> [1, 2]
```

### get

Operator function similar to `operator.getitem` but able to take a default value.

```python
from ebbe import get

get([1, 2, 3], 1)
>>> 2

get([1, 2, 3], 4)
>>> None

# With default value
get([1, 2, 3], 4, 35)
>>> 35
```

### getter

Operator factory similar to `operator.itemgetter` but able to take a default value.

```python
from ebbe import getter

get_second_or_thirty = getter(1, 30)

get_second_or_thirty([1, 2, 3])
>>> 2

get_second_or_thirty([1])
>>> 30

# Overriding default on the spot
get_second_or_thirty([1], 76)
>>> 76
```

### getpath

Operator function used to retrieve a value at given path in a nested structure or a default value if this value cannot be found.

```python
from ebbe import getpath

data = {'a': {'b': [{'c': 34}, 'test'], 'd': 'hello'}}

getpath(data, ['a', 'b', 0, 'c'])
>>> 34

getpath(data, ['t', 'e', 's', 't'])
>>> None

# Using a default return value
getpath(data, ['t', 'e', 's', 't'], 45)
>>> 45

# Using a string path
getpath(data, 'a.b.d', split_char='.')
>>> 'hello'
```

*Arguments*

* **target** *any*: target object.
* **path** *iterable*: path to get.
* **default** *?any* [`None`]: default value to return.
* **items** *?bool* [`True`]: whether to attempt to traverse keys and indices.
* **attributes** *?bool* [`False`]: whether to attempt to traverse attributes.
* **split_char** *?str*: if given, will split strings passed as path instead of raising `TypeError`.
* **parse_indices** *?bool* [`False`]: whether to parse integer indices when splitting string paths.

### pathgetter

Function returning a getter function working as [getpath](#getpath) and partially applied to use the provided path or paths.

```python
from ebbe import pathgetter

data = {'a': {'b': [{'c': 34}, 'test'], 'd': 'hello'}}

getter = pathgetter(['a', 'b', 0, 'c'])
getter(data)
>>> 34

getter = pathgetter(['t', 'e', 's', 't'])
getter(data)
>>> None

# Using a default return value
getter = pathgetter(['t', 'e', 's', 't'])
getter(data, 45)
>>> 45

# Using a string path
getter = pathgetter('a.b.d', split_char='.')
getter(data)
>>> 'hello'

# Using multiple paths
getter = pathgetter(
  ['a', 'b', 0, 'c'],
  ['t', 'e', 's', 't'],
  ['a', 'b', 'd']
)
getter(data)
>>> (34, None, 'hello')
```

*Arguments*

* **paths** *list*: paths to get.
* **items** *?bool* [`True`]: whether to attempt to traverse keys and indices.
* **attributes** *?bool* [`False`]: whether to attempt to traverse attributes.
* **split_char** *?str*: if given, will split strings passed as path instead of raising `TypeError`.
* **parse_indices** *?bool* [`False`]: whether to parse integer indices when splitting string paths.

*Getter arguments*

* **target** *any*: target object.
* **default** *?any* [`None`]: default value to return.

### indexed

Function indexing the given iterable in a dict-like structure. This is basically just some functional sugar over a `dict` constructor.

```python
from ebbe import indexed

indexed(range(3), key=lambda x: x * 10)
>>> {
  0: 0,
  10: 1,
  20: 2
}
```

### grouped

Function grouping the given iterable by a key.

```python
from ebbe import grouped

grouped(range(4), key=lambda x: x % 2)
>>> {
  0: [0, 2],
  1: [1, 3]
}

# Using an optional value
grouped(range(4), key=lambda x: x % 2, value=lambda x: x * 10)
>>> {
  0: [0, 20],
  1: [10, 30]
}

# Using the items variant
from ebbe import grouped_items

grouped_items((x % 2, x * 10) for i in range(4))
>>> {
  0: [0, 20],
  1: [10, 30]
}
```

### partitioned

Function partitioning the given iterable by key.

```python
from ebbe import partitioned

partitioned(range(4), key=lambda x: x % 2)
>>> [
  [0, 2],
  [1, 3]
]

# Using an optional value
partitioned(range(4), key=lambda x: x % 2, value=lambda x: x * 10)
>>> [
  [0, 20],
  [10, 30]
]

# Using the items variant
from ebbe import partitioned_items

partitioned_items((x % 2, x * 10) for i in range(4))
>>> [
  [0, 20],
  [10, 30]
]
```

### sorted_uniq

Function sorting the given iterable then dropping its duplicate through a single linear pass over the data.

```python
from ebbe import sorted_uniq

numbers = [3, 17, 3, 4, 1, 4, 5, 5, 1, -1, 5]
sorted_uniq(numbers)
>>> [-1, 1, 3, 4, 5, 17]

# It accepts all of `sorted` kwargs:
sorted_uniq(numbers, reverse=True)
>>> [17, 5, 4, 3, 1, -1]
```

### pick

Function returning the given dictionary with only the selected keys.

```python
from ebbe import pick

# Selected keys must be an iterable:
pick({'a': 1, 'b': 2, 'c': 3}, ['a', 'c'])
>>> {'a': 1, 'c': 3}

# If you need the function to raise if one of the picked keys is not found:
pick({'a': 1, 'b': 2, 'c': 3}, ['a', 'd'], strict=True)
>>> KeyError: 'd'
```

### omit

Function returning the given dictionary without the selected keys.

```python
from ebbe import omit

# Selected keys must be a container:
omit({'a': 1, 'b': 2, 'c': 3}, ['a', 'c'])
>>> {'b': 2}

# If need to select large numbers of keys, use a set:
omit({'a': 1, 'b': 2, 'c': 3}, {'a', 'c'})
>>> {'b': 2}
```

### noop

Noop function (a function that can be called with any arguments and does nothing). Useful as a default to avoid complicating code sometimes.

```python
from ebbe import noop

noop() # Does nothing...
noop(4, 5) # Still does nothing...
noop(4, index=65) # Nothing yet again...
```

### compose

Function returning the composition function of its variadic arguments.

```python
def times_2(x):
  return x * 2

def plus_5(x):
  return x + 5

compose(times_2, plus_5)(10)
>>> 30

# Reverse order
compose(times_2, plus_5, reverse=True)(10)
>>> 25
```

### rcompose

Function returning the reverse composition function of its variadic arguments.

```python
def times_2(x):
  return x * 2

def plus_5(x):
  return x + 5

rcompose(times_2, plus_5)(10)
>>> 25
```

### and_join

Join function able to group the last items with a custom copula such as "and".

```python
from ebbe import and_join

and_join(['1', '2', '3'])
>>> '1, 2 and 3'

and_join(['1', '2', '3'], separator=';', copula="y")
>>> '1; 2 y 3'
```

### format_int

Format given number as an int with thousands separator.

```python
from ebbe import format_int

format_int(4500)
>>> '4,500'

format_int(10000, separator=' ')
>>> '10 000'
```

### format_time

Format time with custom precision and unit from years to nanoseconds.

```python
from ebbe import format_time

format_time(57309)
>>> "57 microseconds and 309 nanoseconds"

format_time(57309, precision="microseconds")
>>> "57 microseconds

format_time(78, unit="seconds")
>>> "1 minute and 18 seconds"

format_time(4865268458795)
>>> "1 hour, 21 minutes, 5 seconds, 268 milliseconds, 458 microseconds and 795 nanoseconds"

assert format_time(4865268458795, max_items=2)
>>> "1 hour and 21 minutes"

format_time(4865268458795, short=True)
>>> "1h, 21m, 5s, 268ms, 458µs, 795ns"
```

### decorators.fail_fast

Decorate a generator function by wrapping it into another generator function that will fail fast if some validation is run before executing the iteration logic so that exceptions can be caught early.

This logic is also available as a [function](#failfast).

```python
from ebbe.decorators import fail_fast

def hellraiser(n):
  if n > 10:
    raise TypeError

  yield from range(n)

# This will not raise until you consume `gen`
gen = hellraiser(15)

@fail_fast()
def hellraiser(n):
  if n > 10:
    raise TypeError

  yield from range(n)

# This will raise immediately
gen = hellraiser(15)
```

### decorators.with_defer

Decorates a function calling it with a `defer` kwarg working a bit like Go's [defer statement](https://gobyexample.com/defer) so that you can "defer" actions to be done by the end of the function or when an exception is raised to cleanup or tear down things.

This relies on an [ExitStack](https://docs.python.org/3/library/contextlib.html#contextlib.ExitStack) and can of course be also accomplished by context managers but this way of declaring things to defer can be useful sometimes to avoid nesting in complex functions.

```python
from ebbe.decorators import with_defer

@with_defer()
def main(content, *, defer):
  f = open('./output.txt', 'w')
  defer(f.close)

  f.write(content)
```

### Timer

Context manager printing the time (to stderr by default) it took to execute wrapped code. Very useful to run benchmarks.

```python
from ebbe import Timer

with Timer():
  some_costly_operation()
# Will print "Timer: ...s etc." on exit

# To display a custom message:
with Timer('my operation'):
  ...

# To print to stdout
import sys

with Timer(file=sys.stdout):
  ...
```



            

Raw data

            {
    "_id": null,
    "home_page": "http://github.com/Yomguithereal/ebbe",
    "name": "ebbe",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.5",
    "maintainer_email": "",
    "keywords": "iter",
    "author": "Guillaume Plique",
    "author_email": "kropotkinepiotr@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/4f/c4/a67d685b835c004fcf1d180e4a1b7a01e5ee02587438fb599f6d8fe12330/ebbe-1.13.2.tar.gz",
    "platform": null,
    "description": "[![Build Status](https://github.com/Yomguithereal/ebbe/workflows/Tests/badge.svg)](https://github.com/Yomguithereal/ebbe/actions)\n\n# Ebbe\n\nA collection of typical helper functions for python that cannot be found in the however great standard library.\n\n## Installation\n\nYou can install `ebbe` with pip with the following command:\n\n```\npip install ebbe\n```\n\n## Usage\n\n*Iterator functions*\n\n* [as_chunks](#as_chunks)\n* [as_grams](#as_grams)\n* [fail_fast](#fail_fast)\n* [uniq](#uniq)\n* [distinct](#distinct)\n* [with_prev](#with_prev)\n* [with_prev_and_next](#with_prev_and_next)\n* [with_next](#with_next)\n* [with_is_first](#with_is_first)\n* [with_is_last](#with_is_last)\n* [without_first](#without_first)\n* [without_last](#without_last)\n\n*Utilities*\n\n* [get](#get)\n* [getter](#getter)\n* [getpath](#getpath)\n* [pathgetter](#pathgetter)\n* [indexed](#indexed)\n* [grouped](#grouped)\n* [partitioned](#partitioned)\n* [sorted_uniq](#sorted_uniq)\n* [pick](#pick)\n* [omit](#omit)\n\n*Functional Programming*\n\n* [noop](#noop)\n* [compose](#compose)\n* [rcompose](#rcompose)\n\n*Formatting*\n\n* [and_join](#and_join)\n* [format_int](#format_int)\n* [format_time](#format_time)\n\nTODO: *format_repr*, *format_filesize*\n\n*Decorators*\n\n* [decorators.fail_fast](#decoratorsfail_fast)\n* [decorators.with_defer](#decoratorswith_defer)\n\n*Benchmarking*\n\n* [Timer](#timer)\n\n### as_chunks\n\nIterate over chunks of the desired size by grouping items as we iterate over them.\n\n```python\nfrom ebbe import as_chunks\n\nlist(as_chunks(3, [1, 2, 3, 4, 5]))\n>>> [[1, 2, 3], [4, 5]]\n```\n\n### as_grams\n\nIterate over grams (sometimes called n-grams or q-grams etc.) of the given iterable. It works with strings, lists and other sized sequences as well as with lazy iterables without consuming any superfluous memory while doing so.\n\n```python\nfrom ebbe import as_grams\n\nlist(as_grams(3, 'hello'))\n>>> ['hel', 'ell', 'llo']\n\nlist(as_grams(2, (i * 2 for i in range(5))))\n>>> [(0, 2), (2, 4), (4, 6), (6, 8)]\n```\n\n### fail_fast\n\nTake an iterable (but this has been geared towards generators, mostly), and tries to access the first value to see if an Exception will be raised before returning an equivalent iterator.\n\nThis is useful with some badly-conceived generators that checks arguments and raise if they are not valid, for instance, and if you don't want to wrap the whole iteration block within a try/except.\n\nThis logic is also available as a [decorator](#failfastdecorator).\n\n```python\nfrom ebbe import fail_fast\n\ndef hellraiser(n):\n  if n > 10:\n    raise TypeError\n\n  yield from range(n)\n\n# You will need to do this to catch the error:\ngen = hellraiser(15)\n\ntry:\n  for i in gen:\n    print(i)\nexcept TypeError:\n  print('Something went wrong when creating the generator')\n\n# With fail_fast\ntry:\n  gen = fail_fast(hellraiser(15))\nexcept TypeError:\n  print('Something went wrong when creating the generator')\n\nfor i in gen:\n  print(i)\n```\n\n### uniq\n\nFilter repeated items, optionally by key, seen next to each other in the given iterator.\n\n```python\nfrom ebbe import uniq\n\nlist(uniq([1, 1, 1, 2, 3, 4, 4, 5, 5, 6]))\n>>> [1, 2, 3, 4, 5, 6]\n\n# BEWARE: it does not try to remember items (like the `uniq` command)\nlist(uniq([1, 2, 2, 3, 2]))\n>>> [1, 2, 3, 2]\n\n# Using a key\nlist(uniq([(1, 2), (1, 3), (2, 4)], key=lambda x: x[0]))\n>>> [(1, 2), (2, 4)]\n```\n\n### distinct\n\nFilter repeated items, optionally by key, in the given iterator.\n\n```python\nfrom ebbe import distinct\n\nlist(distinct([0, 3, 4, 4, 1, 0, 3]))\n>>> [0, 3, 4, 1]\n\nlist(distinct(range(6), key=lambda x: x % 2))\n>>> [0, 1]\n```\n\n### with_prev\n\nIterate over items along with the previous one.\n\n```python\nfrom ebbe import with_prev\n\nfor previous_item, item in with_prev(iterable):\n  print(previous_item, 'came before', item)\n\nlist(with_prev([1, 2, 3]))\n>>> [(None, 1), (1, 2), (2, 3)]\n```\n\n### with_prev_and_next\n\nIterate over items along with the previous and the next one.\n\n```python\nfrom ebbe import with_prev_and_next\n\nfor previous_item, item, next_item in with_prev_and_next(iterable):\n  print(previous_item, 'came before', item)\n  print(next_item, 'will come after', item)\n\nlist(with_prev_and_next([1, 2, 3]))\n>>> [(None, 1, 2), (1, 2, 3), (2, 3, None)]\n```\n\n### with_next\n\nIterate over items along with the next one.\n\n```python\nfrom ebbe import with_next\n\nfor item, next_item in with_next(iterable):\n  print(next_item, 'will come after', item)\n\nlist(with_next([1, 2, 3]))\n>>> [(1, 2), (2, 3), (3, None)]\n```\n\n### with_is_first\n\nIterate over items along with the information that the current item is the first one or not.\n\n```python\nfrom ebbe import with_is_first\n\nfor is_first, item in with_is_first(iterable):\n  if is_first:\n    print(item, 'is first')\n  else:\n    print(item, 'is not first')\n\nlist(with_is_first([1, 2, 3]))\n>>> [(True, 1), (False, 2), (False, 3)]\n```\n\n### with_is_last\n\nIterate over items along with the information that the current item is the last one or not.\n\n```python\nfrom ebbe import with_is_last\n\nfor is_last, item in with_is_last(iterable):\n  if is_last:\n    print(item, 'is last')\n  else:\n    print(item, 'is not last')\n\nlist(with_is_last([1, 2, 3]))\n>>> [(False, 1), (False, 2), (True, 3)]\n```\n\n### without_first\n\nIterate over the given iterator after skipping its first item. Can be useful if you want to skip headers of a CSV file for instance.\n\n```python\nfrom ebbe import without_first\n\nlist(without_first([1, 2, 3]))\n>>> [2, 3]\n\nfor row in without_first(csv.reader(f)):\n  print(row)\n```\n\n### without_last\n\nIterate over the given iterator but skipping its last item.\n\n```python\nfrom ebbe import without_last\n\nlist(without_last([1, 2, 3]))\n>>> [1, 2]\n```\n\n### get\n\nOperator function similar to `operator.getitem` but able to take a default value.\n\n```python\nfrom ebbe import get\n\nget([1, 2, 3], 1)\n>>> 2\n\nget([1, 2, 3], 4)\n>>> None\n\n# With default value\nget([1, 2, 3], 4, 35)\n>>> 35\n```\n\n### getter\n\nOperator factory similar to `operator.itemgetter` but able to take a default value.\n\n```python\nfrom ebbe import getter\n\nget_second_or_thirty = getter(1, 30)\n\nget_second_or_thirty([1, 2, 3])\n>>> 2\n\nget_second_or_thirty([1])\n>>> 30\n\n# Overriding default on the spot\nget_second_or_thirty([1], 76)\n>>> 76\n```\n\n### getpath\n\nOperator function used to retrieve a value at given path in a nested structure or a default value if this value cannot be found.\n\n```python\nfrom ebbe import getpath\n\ndata = {'a': {'b': [{'c': 34}, 'test'], 'd': 'hello'}}\n\ngetpath(data, ['a', 'b', 0, 'c'])\n>>> 34\n\ngetpath(data, ['t', 'e', 's', 't'])\n>>> None\n\n# Using a default return value\ngetpath(data, ['t', 'e', 's', 't'], 45)\n>>> 45\n\n# Using a string path\ngetpath(data, 'a.b.d', split_char='.')\n>>> 'hello'\n```\n\n*Arguments*\n\n* **target** *any*: target object.\n* **path** *iterable*: path to get.\n* **default** *?any* [`None`]: default value to return.\n* **items** *?bool* [`True`]: whether to attempt to traverse keys and indices.\n* **attributes** *?bool* [`False`]: whether to attempt to traverse attributes.\n* **split_char** *?str*: if given, will split strings passed as path instead of raising `TypeError`.\n* **parse_indices** *?bool* [`False`]: whether to parse integer indices when splitting string paths.\n\n### pathgetter\n\nFunction returning a getter function working as [getpath](#getpath) and partially applied to use the provided path or paths.\n\n```python\nfrom ebbe import pathgetter\n\ndata = {'a': {'b': [{'c': 34}, 'test'], 'd': 'hello'}}\n\ngetter = pathgetter(['a', 'b', 0, 'c'])\ngetter(data)\n>>> 34\n\ngetter = pathgetter(['t', 'e', 's', 't'])\ngetter(data)\n>>> None\n\n# Using a default return value\ngetter = pathgetter(['t', 'e', 's', 't'])\ngetter(data, 45)\n>>> 45\n\n# Using a string path\ngetter = pathgetter('a.b.d', split_char='.')\ngetter(data)\n>>> 'hello'\n\n# Using multiple paths\ngetter = pathgetter(\n  ['a', 'b', 0, 'c'],\n  ['t', 'e', 's', 't'],\n  ['a', 'b', 'd']\n)\ngetter(data)\n>>> (34, None, 'hello')\n```\n\n*Arguments*\n\n* **paths** *list*: paths to get.\n* **items** *?bool* [`True`]: whether to attempt to traverse keys and indices.\n* **attributes** *?bool* [`False`]: whether to attempt to traverse attributes.\n* **split_char** *?str*: if given, will split strings passed as path instead of raising `TypeError`.\n* **parse_indices** *?bool* [`False`]: whether to parse integer indices when splitting string paths.\n\n*Getter arguments*\n\n* **target** *any*: target object.\n* **default** *?any* [`None`]: default value to return.\n\n### indexed\n\nFunction indexing the given iterable in a dict-like structure. This is basically just some functional sugar over a `dict` constructor.\n\n```python\nfrom ebbe import indexed\n\nindexed(range(3), key=lambda x: x * 10)\n>>> {\n  0: 0,\n  10: 1,\n  20: 2\n}\n```\n\n### grouped\n\nFunction grouping the given iterable by a key.\n\n```python\nfrom ebbe import grouped\n\ngrouped(range(4), key=lambda x: x % 2)\n>>> {\n  0: [0, 2],\n  1: [1, 3]\n}\n\n# Using an optional value\ngrouped(range(4), key=lambda x: x % 2, value=lambda x: x * 10)\n>>> {\n  0: [0, 20],\n  1: [10, 30]\n}\n\n# Using the items variant\nfrom ebbe import grouped_items\n\ngrouped_items((x % 2, x * 10) for i in range(4))\n>>> {\n  0: [0, 20],\n  1: [10, 30]\n}\n```\n\n### partitioned\n\nFunction partitioning the given iterable by key.\n\n```python\nfrom ebbe import partitioned\n\npartitioned(range(4), key=lambda x: x % 2)\n>>> [\n  [0, 2],\n  [1, 3]\n]\n\n# Using an optional value\npartitioned(range(4), key=lambda x: x % 2, value=lambda x: x * 10)\n>>> [\n  [0, 20],\n  [10, 30]\n]\n\n# Using the items variant\nfrom ebbe import partitioned_items\n\npartitioned_items((x % 2, x * 10) for i in range(4))\n>>> [\n  [0, 20],\n  [10, 30]\n]\n```\n\n### sorted_uniq\n\nFunction sorting the given iterable then dropping its duplicate through a single linear pass over the data.\n\n```python\nfrom ebbe import sorted_uniq\n\nnumbers = [3, 17, 3, 4, 1, 4, 5, 5, 1, -1, 5]\nsorted_uniq(numbers)\n>>> [-1, 1, 3, 4, 5, 17]\n\n# It accepts all of `sorted` kwargs:\nsorted_uniq(numbers, reverse=True)\n>>> [17, 5, 4, 3, 1, -1]\n```\n\n### pick\n\nFunction returning the given dictionary with only the selected keys.\n\n```python\nfrom ebbe import pick\n\n# Selected keys must be an iterable:\npick({'a': 1, 'b': 2, 'c': 3}, ['a', 'c'])\n>>> {'a': 1, 'c': 3}\n\n# If you need the function to raise if one of the picked keys is not found:\npick({'a': 1, 'b': 2, 'c': 3}, ['a', 'd'], strict=True)\n>>> KeyError: 'd'\n```\n\n### omit\n\nFunction returning the given dictionary without the selected keys.\n\n```python\nfrom ebbe import omit\n\n# Selected keys must be a container:\nomit({'a': 1, 'b': 2, 'c': 3}, ['a', 'c'])\n>>> {'b': 2}\n\n# If need to select large numbers of keys, use a set:\nomit({'a': 1, 'b': 2, 'c': 3}, {'a', 'c'})\n>>> {'b': 2}\n```\n\n### noop\n\nNoop function (a function that can be called with any arguments and does nothing). Useful as a default to avoid complicating code sometimes.\n\n```python\nfrom ebbe import noop\n\nnoop() # Does nothing...\nnoop(4, 5) # Still does nothing...\nnoop(4, index=65) # Nothing yet again...\n```\n\n### compose\n\nFunction returning the composition function of its variadic arguments.\n\n```python\ndef times_2(x):\n  return x * 2\n\ndef plus_5(x):\n  return x + 5\n\ncompose(times_2, plus_5)(10)\n>>> 30\n\n# Reverse order\ncompose(times_2, plus_5, reverse=True)(10)\n>>> 25\n```\n\n### rcompose\n\nFunction returning the reverse composition function of its variadic arguments.\n\n```python\ndef times_2(x):\n  return x * 2\n\ndef plus_5(x):\n  return x + 5\n\nrcompose(times_2, plus_5)(10)\n>>> 25\n```\n\n### and_join\n\nJoin function able to group the last items with a custom copula such as \"and\".\n\n```python\nfrom ebbe import and_join\n\nand_join(['1', '2', '3'])\n>>> '1, 2 and 3'\n\nand_join(['1', '2', '3'], separator=';', copula=\"y\")\n>>> '1; 2 y 3'\n```\n\n### format_int\n\nFormat given number as an int with thousands separator.\n\n```python\nfrom ebbe import format_int\n\nformat_int(4500)\n>>> '4,500'\n\nformat_int(10000, separator=' ')\n>>> '10 000'\n```\n\n### format_time\n\nFormat time with custom precision and unit from years to nanoseconds.\n\n```python\nfrom ebbe import format_time\n\nformat_time(57309)\n>>> \"57 microseconds and 309 nanoseconds\"\n\nformat_time(57309, precision=\"microseconds\")\n>>> \"57 microseconds\n\nformat_time(78, unit=\"seconds\")\n>>> \"1 minute and 18 seconds\"\n\nformat_time(4865268458795)\n>>> \"1 hour, 21 minutes, 5 seconds, 268 milliseconds, 458 microseconds and 795 nanoseconds\"\n\nassert format_time(4865268458795, max_items=2)\n>>> \"1 hour and 21 minutes\"\n\nformat_time(4865268458795, short=True)\n>>> \"1h, 21m, 5s, 268ms, 458\u00b5s, 795ns\"\n```\n\n### decorators.fail_fast\n\nDecorate a generator function by wrapping it into another generator function that will fail fast if some validation is run before executing the iteration logic so that exceptions can be caught early.\n\nThis logic is also available as a [function](#failfast).\n\n```python\nfrom ebbe.decorators import fail_fast\n\ndef hellraiser(n):\n  if n > 10:\n    raise TypeError\n\n  yield from range(n)\n\n# This will not raise until you consume `gen`\ngen = hellraiser(15)\n\n@fail_fast()\ndef hellraiser(n):\n  if n > 10:\n    raise TypeError\n\n  yield from range(n)\n\n# This will raise immediately\ngen = hellraiser(15)\n```\n\n### decorators.with_defer\n\nDecorates a function calling it with a `defer` kwarg working a bit like Go's [defer statement](https://gobyexample.com/defer) so that you can \"defer\" actions to be done by the end of the function or when an exception is raised to cleanup or tear down things.\n\nThis relies on an [ExitStack](https://docs.python.org/3/library/contextlib.html#contextlib.ExitStack) and can of course be also accomplished by context managers but this way of declaring things to defer can be useful sometimes to avoid nesting in complex functions.\n\n```python\nfrom ebbe.decorators import with_defer\n\n@with_defer()\ndef main(content, *, defer):\n  f = open('./output.txt', 'w')\n  defer(f.close)\n\n  f.write(content)\n```\n\n### Timer\n\nContext manager printing the time (to stderr by default) it took to execute wrapped code. Very useful to run benchmarks.\n\n```python\nfrom ebbe import Timer\n\nwith Timer():\n  some_costly_operation()\n# Will print \"Timer: ...s etc.\" on exit\n\n# To display a custom message:\nwith Timer('my operation'):\n  ...\n\n# To print to stdout\nimport sys\n\nwith Timer(file=sys.stdout):\n  ...\n```\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Collection of typical helper functions for python.",
    "version": "1.13.2",
    "project_urls": {
        "Homepage": "http://github.com/Yomguithereal/ebbe"
    },
    "split_keywords": [
        "iter"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "47694425066c9dacae4fff906a6a6b63a6b7b41513e6ba6b9071061efaeb06f8",
                "md5": "d25c22d11391abd91ce21b443ef63de0",
                "sha256": "e7fb59abf9024df8cf188470e44d0e7d7b5b7da6698f9d511586a46f5fb766b2"
            },
            "downloads": -1,
            "filename": "ebbe-1.13.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d25c22d11391abd91ce21b443ef63de0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.5",
            "size": 14363,
            "upload_time": "2023-07-17T15:40:17",
            "upload_time_iso_8601": "2023-07-17T15:40:17.717814Z",
            "url": "https://files.pythonhosted.org/packages/47/69/4425066c9dacae4fff906a6a6b63a6b7b41513e6ba6b9071061efaeb06f8/ebbe-1.13.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4fc4a67d685b835c004fcf1d180e4a1b7a01e5ee02587438fb599f6d8fe12330",
                "md5": "4c9a985ec0fc3d391405fda54489f975",
                "sha256": "183725d75484ac919839250b00cdd2d92bb3b52a1447a8618570dd34dfcf3db3"
            },
            "downloads": -1,
            "filename": "ebbe-1.13.2.tar.gz",
            "has_sig": false,
            "md5_digest": "4c9a985ec0fc3d391405fda54489f975",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.5",
            "size": 17527,
            "upload_time": "2023-07-17T15:40:19",
            "upload_time_iso_8601": "2023-07-17T15:40:19.136920Z",
            "url": "https://files.pythonhosted.org/packages/4f/c4/a67d685b835c004fcf1d180e4a1b7a01e5ee02587438fb599f6d8fe12330/ebbe-1.13.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-17 15:40:19",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Yomguithereal",
    "github_project": "ebbe",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "ebbe"
}
        
Elapsed time: 0.10642s