semvec/pypi-packages huggingface dataset:
decoratools 0.0.19
Collection of useful decorators
License: MIT
Requires Python: >=3.11
Keywords: decorator,utility,library

Collection of useful decorators

-----

# Installation

```bash
pip install decoratools
```

# Usage

## `capi`

Interface with [`statx(2)`](https://man7.org/linux/man-pages/man2/statx.2.html):

```python
import ctypes
from argparse import ArgumentParser

from decoratools.capi import ARRAY, POINTER, structure, unwraps

LIBC = ctypes.CDLL("libc.so.6")
AT_FDCDW = -100
STATX_BASIC_STATS = 0x000007FF


@structure
class StatxTimestamp:
    tv_sec: ctypes.c_int64
    tv_nsec: ctypes.c_uint32
    __statx_timestamp_pad1: ctypes.c_int32


@structure
class Statx:
    stx_mask: ctypes.c_uint32
    stx_blksize: ctypes.c_uint32
    stx_attributes: ctypes.c_uint64
    stx_nlink: ctypes.c_uint32
    stx_uid: ctypes.c_uint32
    stx_gid: ctypes.c_uint32
    stx_mode: ctypes.c_uint16
    __statx_pad1: ctypes.c_uint16
    stx_ino: ctypes.c_uint64
    stx_size: ctypes.c_uint64
    stx_blocks: ctypes.c_uint64
    stx_attributes_mask: ctypes.c_uint64

    stx_atime: StatxTimestamp
    stx_btime: StatxTimestamp
    stx_ctime: StatxTimestamp
    stx_mtime: StatxTimestamp

    stx_rdev_major: ctypes.c_uint32
    stx_rdev_minor: ctypes.c_uint32

    stx_dev_major: ctypes.c_uint32
    stx_dev_minor: ctypes.c_uint32

    stx_mnt_id: ctypes.c_uint64

    stx_dio_mem_align: ctypes.c_uint32
    stx_dio_offset_align: ctypes.c_uint32

    __statx_pad2: ARRAY[ctypes.c_uint64, 12]


@unwraps(LIBC.statx)
def statx(
    dirfd: ctypes.c_int, pathname: ctypes.c_char_p, flags: ctypes.c_int, mask: ctypes.c_int, statxbuf: POINTER[Statx]
) -> int:
    ...


if __name__ == "__main__":
    parser = ArgumentParser()
    parser.add_argument("path", help="path to statx")
    args = parser.parse_args()

    statxbuf = Statx()
    rc = statx(AT_FDCDW, args.path.encode(), 0, STATX_BASIC_STATS, ctypes.byref(statxbuf))
    if rc != 0:
        raise RuntimeError("statx: failed")
    print(statxbuf)
```

```console
$ python statx.py /dev/null
Statx(stx_mask=6143, stx_blksize=4096, stx_attributes=0, stx_nlink=1, stx_uid=0, stx_gid=0, stx_mode=8630, _Statx__statx_pad1=0, stx_ino=4, stx_size=0, stx_blocks=0, stx_attributes_mask=2109552, stx_atime=StatxTimestamp(tv_sec=1672527600, tv_nsec=0, _StatxTimestamp__statx_timestamp_pad1=0), stx_btime=StatxTimestamp(tv_sec=0, tv_nsec=0, _StatxTimestamp__statx_timestamp_pad1=0), stx_ctime=StatxTimestamp(tv_sec=1672527600, tv_nsec=0, _StatxTimestamp__statx_timestamp_pad1=0), stx_mtime=StatxTimestamp(tv_sec=1672527600, tv_nsec=0, _StatxTimestamp__statx_timestamp_pad1=0), stx_rdev_major=1, stx_rdev_minor=3, stx_dev_major=0, stx_dev_minor=5, stx_mnt_id=22, stx_dio_mem_align=0, stx_dio_offset_align=0, _Statx__statx_pad2=<decoratools.capi.c_ulong_Array_12 object at 0x7efe94be0a70>)
```
iteriotools 0.0.4
Python3.10+ utilities to translate iterators of bytes into file-like objects
License: MIT
Requires Python: >=3.10
Keywords: io,iterators,generators,itertools,utility

Ever wanted to stream large JSON objects with [`FastAPI`](https://fastapi.tiangolo.com/) asynchrounously?
Look no more, `iteriotools` is the solution you've been looking for!

It provides a `RawIterIO` class which can wrap an iterator of bytes into a read-only file-like object.

# Example

```bash
cd example
```

To run the example project's application:

```bash
pipenv run uvicorn --reload project:app
```

To demonstrate the impact of `RawIterIO` on streaming large JSON objects, first emulate a "liveness probe" by running
the following:

```bash
while time curl --silent http://127.0.0.1:8000/health > /dev/null; do :; done
```

Then, while still having the probe running, emulate a few clients `GET`-ing some large JSON object with:

```bash
(
    trap -- 'kill 0' EXIT
    for _ in {0..31}; do
        while curl --silent http://127.0.0.1:8000/users/batched > /dev/null; do :; done &
    done
    wait
)
```

You should notice a significant increase in response time on the "liveness probe" (e.g. 0.003 seconds --> 25 seconds).

Now, with the version that utilizes `RawIterIO` to stream JSON chunk by chunk:

```bash
(
    trap -- 'kill 0' EXIT
    for _ in {0..31}; do
        while curl --silent http://127.0.0.1:8000/users/streamed > /dev/null; do :; done &
    done
    wait
)
```

You should still notice a significant increase in response time, but way less extreme (e.g. 0.003 seconds -> 2
seconds), and that numbers scales better with the size of the JSON object being streamed instead of being batched.