Name | pysj JSON |
Version |
1.1.1
JSON |
| download |
home_page | None |
Summary | Pysj makes Python development more comfortable, with utils, classes and helper |
upload_time | 2024-12-16 07:20:09 |
maintainer | None |
docs_url | None |
author | Sondre S. Ødegård |
requires_python | >=3.10 |
license | None |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# Pysj
This package contains utils, classes and helper functions I find myself reimplementing in several projects. As of now all functions are importable from the top level module.
The name is a commonly used shortened version of the Norwegian word "pysjamas" (in english: pajamas/pyjamas).
Coding in your pysjamas/pajamas/pyjamas is comfortable. This package is an attempt to make Python development a bit more comfortable as well.
This is an ongoing project and so far just a few functions are implemented. Most time have been spent on project structure, tests and packaging.
## Installation
```bash
pip install pysj
```
## Usage
### Importing
```python
# We import everything here for all of the examples
from pysj import (
md5,
sha256,
ExtendedJSONDecoder,
ExtendedJSONEncoder,
Timer,
chunk,
first,
flatten,
isotime,
moving_average,
n_wise,
paginate,
seconds,
take,
transpose
)
```
### Extended JSON encoding / decoding
```python
# Serializing to json with datetime objects
json.dumps(
{
"timestamp": datetime.datetime.fromisoformat("2021-12-01T04:50:00.123456")
},
cls=ExtendedJSONEncoder,
)
# and back again
json.loads('{"timestamp": "2021-12-01T04:50:00"}',
cls=ExtendedJSONDecoder,
)
```
### Some functional stuff
#### Flatten
Flattens an iterable, depth first.
```python
>>> flatten([range(10), (1,2,3), "HELLO", [[["42"]], []], ["y0l0"]])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 'HELLO', '42', 'y0l0']
```
#### First
Get first element of an iterable
```python
>>> first([1, 2, 3])
1
```
#### Take
Get first *n* elements from an iterable
```python
data = range(100)
>>> list(take(4, ))
[0, 1, 2, 3]
```
#### Chunk
Chunks an iterable into an iterable yield a list with *n* items at a time.
Takes an optional *fillvalue*, defaults to **None**,
```python
data = range(10)
>>> list(chunk(take(4, data, fillvalue=":D")))
[
[0, 1, 2, 3],
[4, 5, 6, 7],
[9, 10, ":D", ":D"],
]
```
#### Transpose
```python
>>> transpose(
[
[1,2,3],
[1,2,3],
[1,2,3],
]
)
[
[1, 1, 1],
[2, 2, 2],
[3, 3, 3]
]
```
#### N wise
Yields *n* items from data like a gliding window, as long as there is enough elements in data to yield the full window.
Like itertools.pairwise, but generalized to *n* items. (New in Python 3.10)
```python
>>> list(n_wise(3, [1,2,3,4,5]))
[(1,2,3), (2,3,4), (3,4,5)]
```
#### Moving average
```python
>>> list(moving_average(3, (1,2,3,4,5)))
[2.0, 3.0, 4.0]
```
### Other
#### Isotime
Returns an isoformated string of the current time (or supplied datetime *dt*) to the given *precision*, defaults to 'd' (day).
```python
>>>isotime()
'2023-01-01'
>>>isotime('s')
'2023-01-01T00:00:00'
>>>isotime('m', datetime(2022, 2, 2, 2, 2))
'2022-02-02T02:02'
# Only the first character of *precision* is used, so for readability you can write it out fully.
>>>isotime('minutes', datetime(2022, 2, 2, 2, 2))
'2022-02-02T02:02'
```
#### Stopwatch
```python
>>> with Timer():
>>> # Do stuff
>>> sleep(1)
Starting timer
Elapsed time 1.0007134879997466 s.
```
#### Simple hashing
```python
>>> print(sha256("test"))
9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
>>> print(md5("test"))
098f6bcd4621d373cade4e832627b4f6
```
Raw data
{
"_id": null,
"home_page": null,
"name": "pysj",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.10",
"maintainer_email": null,
"keywords": null,
"author": "Sondre S. \u00d8deg\u00e5rd",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/dc/98/ed508b123ee9df908818e375d8b96644e165cb9f135d152cbd6bf30666df/pysj-1.1.1.tar.gz",
"platform": null,
"description": "# Pysj\n\nThis package contains utils, classes and helper functions I find myself reimplementing in several projects. As of now all functions are importable from the top level module.\n\nThe name is a commonly used shortened version of the Norwegian word \"pysjamas\" (in english: pajamas/pyjamas).\nCoding in your pysjamas/pajamas/pyjamas is comfortable. This package is an attempt to make Python development a bit more comfortable as well.\n\nThis is an ongoing project and so far just a few functions are implemented. Most time have been spent on project structure, tests and packaging.\n\n\n## Installation\n```bash\npip install pysj\n```\n\n## Usage\n\n### Importing\n```python\n# We import everything here for all of the examples\nfrom pysj import (\n md5, \n sha256,\n ExtendedJSONDecoder,\n ExtendedJSONEncoder,\n Timer,\n chunk,\n first,\n flatten,\n isotime,\n moving_average,\n n_wise,\n paginate,\n seconds,\n take,\n transpose\n)\n```\n\n\n### Extended JSON encoding / decoding\n```python\n# Serializing to json with datetime objects\njson.dumps(\n {\n \"timestamp\": datetime.datetime.fromisoformat(\"2021-12-01T04:50:00.123456\")\n },\n cls=ExtendedJSONEncoder,\n)\n\n# and back again\njson.loads('{\"timestamp\": \"2021-12-01T04:50:00\"}',\n cls=ExtendedJSONDecoder,\n)\n```\n\n### Some functional stuff\n#### Flatten\nFlattens an iterable, depth first.\n```python\n>>> flatten([range(10), (1,2,3), \"HELLO\", [[[\"42\"]], []], [\"y0l0\"]])\n[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 'HELLO', '42', 'y0l0']\n```\n\n#### First\nGet first element of an iterable\n```python\n>>> first([1, 2, 3])\n1\n```\n\n#### Take\nGet first *n* elements from an iterable\n```python\ndata = range(100)\n>>> list(take(4, ))\n[0, 1, 2, 3]\n```\n\n#### Chunk\nChunks an iterable into an iterable yield a list with *n* items at a time.\n\nTakes an optional *fillvalue*, defaults to **None**,\n```python\ndata = range(10)\n>>> list(chunk(take(4, data, fillvalue=\":D\")))\n[\n [0, 1, 2, 3],\n [4, 5, 6, 7],\n [9, 10, \":D\", \":D\"],\n]\n```\n\n#### Transpose\n\n```python\n>>> transpose(\n [\n [1,2,3],\n [1,2,3],\n [1,2,3],\n ]\n)\n[\n [1, 1, 1],\n [2, 2, 2],\n [3, 3, 3]\n]\n```\n\n#### N wise\nYields *n* items from data like a gliding window, as long as there is enough elements in data to yield the full window.\n\nLike itertools.pairwise, but generalized to *n* items. (New in Python 3.10)\n```python\n>>> list(n_wise(3, [1,2,3,4,5]))\n[(1,2,3), (2,3,4), (3,4,5)]\n```\n\n\n#### Moving average\n```python\n>>> list(moving_average(3, (1,2,3,4,5)))\n[2.0, 3.0, 4.0]\n```\n\n### Other\n\n#### Isotime\nReturns an isoformated string of the current time (or supplied datetime *dt*) to the given *precision*, defaults to 'd' (day).\n\n```python\n>>>isotime()\n'2023-01-01'\n\n>>>isotime('s')\n'2023-01-01T00:00:00'\n\n>>>isotime('m', datetime(2022, 2, 2, 2, 2))\n'2022-02-02T02:02'\n\n# Only the first character of *precision* is used, so for readability you can write it out fully.\n>>>isotime('minutes', datetime(2022, 2, 2, 2, 2))\n'2022-02-02T02:02'\n```\n\n#### Stopwatch\n```python\n>>> with Timer():\n>>> # Do stuff\n>>> sleep(1)\nStarting timer\nElapsed time 1.0007134879997466 s.\n```\n\n#### Simple hashing\n```python\n>>> print(sha256(\"test\"))\n9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08\n>>> print(md5(\"test\"))\n098f6bcd4621d373cade4e832627b4f6\n```",
"bugtrack_url": null,
"license": null,
"summary": "Pysj makes Python development more comfortable, with utils, classes and helper",
"version": "1.1.1",
"project_urls": {
"Documentation": "https://github.com/sondreod/pysj",
"Source": "https://github.com/sondreod/pysj"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "d68eeb8b2e5ad330d7596458a6532e1eb533d3df2888552527835893ecdb8596",
"md5": "6f15d9bcd38c09313eb6a44ada5c29b1",
"sha256": "7ee591aae82d7d29040b9d6c10e021b8863c08bb663b1b826259e65911437bf5"
},
"downloads": -1,
"filename": "pysj-1.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "6f15d9bcd38c09313eb6a44ada5c29b1",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.10",
"size": 10506,
"upload_time": "2024-12-16T07:20:07",
"upload_time_iso_8601": "2024-12-16T07:20:07.412151Z",
"url": "https://files.pythonhosted.org/packages/d6/8e/eb8b2e5ad330d7596458a6532e1eb533d3df2888552527835893ecdb8596/pysj-1.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "dc98ed508b123ee9df908818e375d8b96644e165cb9f135d152cbd6bf30666df",
"md5": "4e0d09f3a565b8c9ff95b7920f46d05e",
"sha256": "a1eba174908a7fba421bc4a493e7e5c6ae06c8e920150a820b3ab30657da8dc0"
},
"downloads": -1,
"filename": "pysj-1.1.1.tar.gz",
"has_sig": false,
"md5_digest": "4e0d09f3a565b8c9ff95b7920f46d05e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.10",
"size": 16908,
"upload_time": "2024-12-16T07:20:09",
"upload_time_iso_8601": "2024-12-16T07:20:09.157815Z",
"url": "https://files.pythonhosted.org/packages/dc/98/ed508b123ee9df908818e375d8b96644e165cb9f135d152cbd6bf30666df/pysj-1.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-16 07:20:09",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "sondreod",
"github_project": "pysj",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "pysj"
}