# refdatatypes
Pyton basic datatypes as a references. Solve problems with static class immutable datatypes.
## Installation
```python
pip3 install refdatatypes
```
## Problem
```python
class A:
static = 1
class B(A):
pass
print(f"int {A.static}") # get 1 correctly
print(f"int {B.static}") # get 1 correctly
A.static = 5
print(f"int {A.static}") # get 5 correctly
print(f"int {B.static}") # get 5 correctly
B.static = 6
print(f"int {A.static}") # expected 6, but get 5 incorrectly
print(f"int {B.static}") # get 6 correctly
A.static = 7
print(f"int {A.static}") # get 7 correctly
print(f"int {B.static}") # expected 7, but get unchanged 6, incorrectly
```
## Solution
```python
from refdatatypes.refint import RefInt
class AAA:
static = RefInt(1)
class BBB(AAA):
pass
print(f"refint {AAA.static.value}") # get 1 correctly
print(f"refint {BBB.static.value}") # get 1 correctly
AAA.static.value = 5
print(f"refint {AAA.static.value}") # get 5 correctly
print(f"refint {BBB.static.value}") # get 5 correctly
BBB.static.value = 6
print(f"refint {AAA.static.value}") # get 6 correctly
print(f"refint {BBB.static.value}") # get 6 correctly
AAA.static.value = 7
print(f"refint {AAA.static.value}") # get 7 correctly
print(f"refint {BBB.static.value}") # get 7 correctly
```
More details you can find in included examples [static_class_attribute_problem.py](https://gitlab.com/alda78/refdatatypes/-/blob/main/examples/static_class_attribute_problem.py)
and [static_class_attribute_solution.py](https://gitlab.com/alda78/refdatatypes/-/blob/main/examples/static_class_attribute_solution.py) .
## Safe datatypes
`safedatatypes` is simple set of function and classes which enables
you to work safely with base python datatypes without error falls during
convert or item access.
### example
```python
from refdatatypes.safedatatypes import safe_int
my_int = safe_int("None") # no error
print(my_int) # prints: `0`
```
### example 2
```python
from refdatatypes.safedatatypes import SafeDict
my_dict = SafeDict()
my_dict["a"] = 1
print(my_dict["a"]) # prints: `1`
print(my_dict["b"]) # prints: `None` with no error
print(my_dict) # prints: `{'a': 1}`
my_dict = SafeDict({"a": 1}, default_value=-1, autoset=True)
print(my_dict["a"]) # prints: `1`
print(my_dict["b"]) # prints: `-1` with no error
print(my_dict) # prints: `{'a': 1, 'b': -1}`
```
### example 3
```python
# expected dict structure
my_dict = {"a": 1, "b": {"bb": 2}}
# but structure like this occured
my_dict = {"a": 1, "b": None}
# safe handle of this structure
result = my_dict.get("b") or {}
result = result.get("bb") or 0
print(result) # prints: `0` with no error
```
Solution with SafeDict
```python
from refdatatypes.safedatatypes import SafeDict
# expected dict structure
my_dict = SafeDict({"a": 1, "b": {"bb": 2}})
# but structure like this occured
my_dict = SafeDict({"a": 1, "b": None})
# safe handle of this structure
result = my_dict.get("b", SafeDict(), if_none=True).get("bb", 0)
print(result) # prints: `0` with no error
```
## Utils
### dict_item_must_be_list
This utility checks if item in combined structure of lists and dicts is realy list. If not, it converts it to list.
This utility is useful when you are working with `xmltodict` library.
You expect list in some dict structure place, but if there is only one item, it is converted to dict not into list,
because for `xmltodict` is not possible know it should be list.
```python
from refdatatypes.utils import dict_item_must_be_list
d = {
"a": 1,
"b": {
"bb": [
{"ccc": {"ddd": 4}},
{"ccc": [{"ddd": 4}, {"ddd": 4}]},
{"ccc": 4},
{"ccc": None},
{"ccc": [1, 2, [11, 22]]},
]
},
}
dict_item_must_be_list(d, "b.bb.ccc")
print(d)
```
```python
{
'a': 1,
'b': {
'bb': [
{'ccc': [{'ddd': 4}]},
{'ccc': [{'ddd': 4}, {'ddd': 4}]},
{'ccc': [4]},
{'ccc': []},
{'ccc': [1, 2, [11, 22]]}
]
}
}
```
Raw data
{
"_id": null,
"home_page": "https://gitlab.com/alda78/refdatatypes",
"name": "refdatatypes",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": null,
"keywords": null,
"author": "Ales Adamek",
"author_email": "alda78@seznam.cz",
"download_url": "https://files.pythonhosted.org/packages/c7/e3/7942956b38006cf53fc50f02209e1863b253379085a02a185492963da6a6/refdatatypes-1.4.0.tar.gz",
"platform": null,
"description": "# refdatatypes\n\nPyton basic datatypes as a references. Solve problems with static class immutable datatypes.\n\n## Installation\n```python\npip3 install refdatatypes\n```\n\n## Problem\n\n```python\nclass A:\n static = 1\n\n\nclass B(A):\n pass\n\n\nprint(f\"int {A.static}\") # get 1 correctly\nprint(f\"int {B.static}\") # get 1 correctly\n\nA.static = 5\nprint(f\"int {A.static}\") # get 5 correctly\nprint(f\"int {B.static}\") # get 5 correctly\n\nB.static = 6\nprint(f\"int {A.static}\") # expected 6, but get 5 incorrectly\nprint(f\"int {B.static}\") # get 6 correctly\n\nA.static = 7\nprint(f\"int {A.static}\") # get 7 correctly\nprint(f\"int {B.static}\") # expected 7, but get unchanged 6, incorrectly\n```\n\n## Solution\n```python\nfrom refdatatypes.refint import RefInt\n\n\nclass AAA:\n static = RefInt(1)\n\n\nclass BBB(AAA):\n pass\n\n\nprint(f\"refint {AAA.static.value}\") # get 1 correctly\nprint(f\"refint {BBB.static.value}\") # get 1 correctly\n\nAAA.static.value = 5\nprint(f\"refint {AAA.static.value}\") # get 5 correctly\nprint(f\"refint {BBB.static.value}\") # get 5 correctly\n\nBBB.static.value = 6\nprint(f\"refint {AAA.static.value}\") # get 6 correctly\nprint(f\"refint {BBB.static.value}\") # get 6 correctly\n\nAAA.static.value = 7\nprint(f\"refint {AAA.static.value}\") # get 7 correctly\nprint(f\"refint {BBB.static.value}\") # get 7 correctly\n```\n\nMore details you can find in included examples [static_class_attribute_problem.py](https://gitlab.com/alda78/refdatatypes/-/blob/main/examples/static_class_attribute_problem.py) \nand [static_class_attribute_solution.py](https://gitlab.com/alda78/refdatatypes/-/blob/main/examples/static_class_attribute_solution.py) .\n\n## Safe datatypes\n`safedatatypes` is simple set of function and classes which enables\nyou to work safely with base python datatypes without error falls during\nconvert or item access.\n\n### example\n```python\nfrom refdatatypes.safedatatypes import safe_int\n\nmy_int = safe_int(\"None\") # no error\nprint(my_int) # prints: `0`\n```\n\n### example 2\n```python\nfrom refdatatypes.safedatatypes import SafeDict\n\nmy_dict = SafeDict()\nmy_dict[\"a\"] = 1\n\nprint(my_dict[\"a\"]) # prints: `1` \nprint(my_dict[\"b\"]) # prints: `None` with no error\nprint(my_dict) # prints: `{'a': 1}`\n\nmy_dict = SafeDict({\"a\": 1}, default_value=-1, autoset=True)\nprint(my_dict[\"a\"]) # prints: `1` \nprint(my_dict[\"b\"]) # prints: `-1` with no error\nprint(my_dict) # prints: `{'a': 1, 'b': -1}`\n```\n\n### example 3\n```python\n# expected dict structure\nmy_dict = {\"a\": 1, \"b\": {\"bb\": 2}}\n# but structure like this occured\nmy_dict = {\"a\": 1, \"b\": None}\n# safe handle of this structure\nresult = my_dict.get(\"b\") or {}\nresult = result.get(\"bb\") or 0\nprint(result) # prints: `0` with no error\n```\nSolution with SafeDict\n\n```python\nfrom refdatatypes.safedatatypes import SafeDict\n\n# expected dict structure\nmy_dict = SafeDict({\"a\": 1, \"b\": {\"bb\": 2}})\n# but structure like this occured\nmy_dict = SafeDict({\"a\": 1, \"b\": None})\n# safe handle of this structure\nresult = my_dict.get(\"b\", SafeDict(), if_none=True).get(\"bb\", 0)\nprint(result) # prints: `0` with no error\n```\n\n## Utils\n### dict_item_must_be_list\nThis utility checks if item in combined structure of lists and dicts is realy list. If not, it converts it to list.\nThis utility is useful when you are working with `xmltodict` library.\nYou expect list in some dict structure place, but if there is only one item, it is converted to dict not into list, \nbecause for `xmltodict` is not possible know it should be list.\n```python\nfrom refdatatypes.utils import dict_item_must_be_list\n\nd = {\n \"a\": 1,\n \"b\": {\n \"bb\": [\n {\"ccc\": {\"ddd\": 4}},\n {\"ccc\": [{\"ddd\": 4}, {\"ddd\": 4}]},\n {\"ccc\": 4},\n {\"ccc\": None},\n {\"ccc\": [1, 2, [11, 22]]},\n ]\n },\n}\ndict_item_must_be_list(d, \"b.bb.ccc\")\nprint(d)\n```\n```python\n{\n 'a': 1,\n 'b': {\n 'bb': [\n {'ccc': [{'ddd': 4}]},\n {'ccc': [{'ddd': 4}, {'ddd': 4}]},\n {'ccc': [4]},\n {'ccc': []},\n {'ccc': [1, 2, [11, 22]]}\n ]\n }\n}\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Pyton basic datatypes as a references.",
"version": "1.4.0",
"project_urls": {
"Homepage": "https://gitlab.com/alda78/refdatatypes"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "44c75a938c5db447d4a9969a9ad5aec6861131a1b0ec6c5d87f658d571c812ed",
"md5": "82b9607a1596bc95250794f4bee63b85",
"sha256": "1c23726bfb003f9cf0ef6174fd8296efca00078dcfd23b3fdcb4eef39ae22cee"
},
"downloads": -1,
"filename": "refdatatypes-1.4.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "82b9607a1596bc95250794f4bee63b85",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 8304,
"upload_time": "2024-07-30T13:35:19",
"upload_time_iso_8601": "2024-07-30T13:35:19.233292Z",
"url": "https://files.pythonhosted.org/packages/44/c7/5a938c5db447d4a9969a9ad5aec6861131a1b0ec6c5d87f658d571c812ed/refdatatypes-1.4.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c7e37942956b38006cf53fc50f02209e1863b253379085a02a185492963da6a6",
"md5": "13f6e29fa73770d415cdb7a04acf1886",
"sha256": "c90de03c13eb71fbf8dfbd6ec0e5ec7ab0fbc3c46b1e97387f6a0b259cc7f5ea"
},
"downloads": -1,
"filename": "refdatatypes-1.4.0.tar.gz",
"has_sig": false,
"md5_digest": "13f6e29fa73770d415cdb7a04acf1886",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 6977,
"upload_time": "2024-07-30T13:35:21",
"upload_time_iso_8601": "2024-07-30T13:35:21.848882Z",
"url": "https://files.pythonhosted.org/packages/c7/e3/7942956b38006cf53fc50f02209e1863b253379085a02a185492963da6a6/refdatatypes-1.4.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-30 13:35:21",
"github": false,
"gitlab": true,
"bitbucket": false,
"codeberg": false,
"gitlab_user": "alda78",
"gitlab_project": "refdatatypes",
"lcname": "refdatatypes"
}