Skip to main content

NoneSafe-EX

A None-safe wrapper for Python containers, preventing KeyError, IndexError, and AttributeError when accessing missing or None values.

** Warn: NoneLike is not None. Please do not use NoneLike is None to check if a value is None. == None or is NoneLike is recommended.**

Features

  • NoneSafeDict: None-safe dictionary - returns NoneLike instead of raising KeyError
  • NoneSafeList: None-safe list - returns NoneLike for out-of-bounds access
  • NoneLike: Sentinel object that supports arbitrary chained access without raising exceptions

Installation

pip install nonesafe_ex

Usage

NoneSafeDict

from nonesafe_ex import NoneSafeDict

data = NoneSafeDict({"name": "Alice", "info": {"age": 25}})

# Access existing key
print(data["name"])  # "Alice"

# Access non-existing key - returns NoneLike instead of KeyError
print(data["non_existing"])  # None
print(bool(data["non_existing"]))  # False

# Nested access with automatic wrapping
print(data["info"]["age"])  # 25
print(data["info"]["non_existing"])  # None (NoneLike)

# Safe chained access
result = data["info"]["address"]["city"]
print(result)  # None

NoneSafeList

from nonesafe import NoneSafeList

items = NoneSafeList([1, 2, {"value": 3}])

# Access existing index
print(items[0])  # 1

# Out-of-bounds access - returns NoneLike instead of IndexError
print(items[100])  # None
print(items[-100])  # None

# Nested access with automatic wrapping
print(items[2]["value"])  # 3
print(items[2]["non_existing"])  # None

# Iteration with automatic wrapping
for item in items:
    print(item)

NoneLike

from nonesafe import NoneLike

# Supports arbitrary chained access
result = NoneLike.foo.bar.baz()["key"]
print(result)  # None
print(bool(result))  # False

# Safe for any operation
print(NoneLike + 1)  # None
print(NoneLike * 2)  # None
print(len(NoneLike))  # 0

API

NoneSafeDict

  • __getitem__(key) - Returns wrapped value or NoneLike if key not found
  • get(key, default=None) - Returns wrapped value or wrapped default
  • values() - Returns wrapped values view
  • items() - Returns items view with wrapped values
  • pop(key, default=None) - Removes and returns wrapped value
  • copy() - Returns new NoneSafeDict

NoneSafeList

  • __getitem__(index) - Returns wrapped value or NoneLike if out of bounds
  • pop(index=-1) - Removes and returns wrapped value
  • copy() - Returns new NoneSafeList
  • first(default=None) - Returns first item or wrapped default
  • last(default=None) - Returns last item or wrapped default

NoneLike

  • Supports chained attribute access: NoneLike.attr.subattr
  • Supports chained item access: NoneLike["key"]["subkey"]
  • Supports function calls: NoneLike.method()
  • Boolean value is False
  • String representation is "None"

License

MIT

Release files for nonesafe_ex 0.1.2

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

Source distribution (sdist)

Source distribution for nonesafe_ex 0.1.2
File Size Uploaded
nonesafe_ex-0.1.2.tar.gz 4.7 kB Details

Built distribution (wheel)

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

Total release size: 11.5 kB

Release files / nonesafe_ex-0.1.2.tar.gz

Download URL nonesafe_ex-0.1.2.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
7454024f9996653dd976ead505742568054f223a5c70c2789ea3264171352f7a
BLAKE2b-256 checksum
How to use checksums
6f2d88ed1cef38d2e5c9c15f0c9c318fbbe070419cba3dffd60b5a2cc061ec31
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.11.9 Windows/10

Release files / nonesafe_ex-0.1.2-py3-none-any.whl

Download URL nonesafe_ex-0.1.2-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2b1260ecbba6838493de18f1beff8bdc77cbb2b80604004bac323912d2c7a92f
BLAKE2b-256 checksum
How to use checksums
fd29582e2c58c56139e0f813529f0ded8ebebb4175f8e74236f8f29e90032f6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/2.3.2 CPython/3.11.9 Windows/10

Release history Release notifications | RSS feed

This release

0.1.2 This release

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