Skip to main content

refdatatypes

Pyton basic datatypes as a references. Solve problems with static class immutable datatypes.

Installation

pip3 install refdatatypes

Problem

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

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 and 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

from refdatatypes.safedatatypes import safe_int

my_int = safe_int("None")  # no error
print(my_int)  # prints: `0`

example 2

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

# 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

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.

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)
{
    'a': 1,
    'b': {
        'bb': [
            {'ccc': [{'ddd': 4}]},
            {'ccc': [{'ddd': 4}, {'ddd': 4}]},
            {'ccc': [4]},
            {'ccc': []},
            {'ccc': [1, 2, [11, 22]]}
        ]
    }
}

Metadata

Release files for refdatatypes 1.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for refdatatypes 1.4.1
File Size Uploaded
refdatatypes-1.4.1.tar.gz 7.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for refdatatypes 1.4.1
File Interpreter ABI Platform
refdatatypes-1.4.1-py3-none-any.whl Python 3 none any Details

Total release size: 15.3 kB

Release files / refdatatypes-1.4.1.tar.gz

Download URL refdatatypes-1.4.1.tar.gz
Size 7.0 kB
Tags Source
SHA-256 checksum
How to use checksums
26ba197d4f182002ce186152a604263bdb4c4ee164e21e003fab1d6df36e1935
BLAKE2b-256 checksum
How to use checksums
c41d22807fdd70ba0ed5c4b011af03c3ec844848dfddc82a2113f4aad6ef74f9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.9

Release files / refdatatypes-1.4.1-py3-none-any.whl

Download URL refdatatypes-1.4.1-py3-none-any.whl
Size 8.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
172dae6eaa9b9e52a580099edf2cb5f4cd946cacf2f39fe488b06ad3ca201bb9
BLAKE2b-256 checksum
How to use checksums
c82b0b4f0cf83911a336892c56b62d0d823320248fd3f28fc9c6b27495e32043
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.9

Release history Release notifications | RSS feed

This release

1.4.1 This release

2 release files

1.4.0

2 release files

1.3.1

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page