Jsify module
The jsify module provides a set of classes and functions designed to wrap standard Python data structures (such as dictionaries, lists, and tuples) with JSON-like behavior. These wrapped objects, known as Object, Dict, List, and Tuple, allow for attribute-style access, dynamic nesting, and additional functionality that is commonly required when working with JSON data. The core component of this module is the Object class, which provides a flexible and dynamic interface for accessing and manipulating underlying data. The Dict, List, and Tuple classes extend Object to offer more specific behaviors for dictionaries, lists, and tuples, respectively. Additionally, the module offers a series of utility functions such as jsify for converting standard Python objects into their JSON-like counterparts, and unjsify for reversing this transformation.
- class jsify.jsify.Dict(o, **kwargs)[source]
Bases:
Object
A JSON-like dictionary object that extends Object and provides dictionary-specific methods.
- Parameters:
o (dict or object) – The original dictionary or object to wrap.
kwargs – Additional key-value pairs to include in the dictionary.
- class jsify.jsify.Iterator(obj)[source]
Bases:
Iterator
An iterator for objects that supports jsifying and unjsifying.
- class jsify.jsify.List(o, *args)[source]
Bases:
Object
A JSON-like list object that extends Object and provides list-specific methods.
- Parameters:
o (list or object) – The original list or object to wrap.
args – Additional elements to include in the list.
- copy(deep=False)[source]
Create a shallow or deep copy of the list.
- Parameters:
deep (bool) – True if the object should be deep copied, False otherwise.
- Returns:
A copy of the list.
- Return type:
- count(value)[source]
Return the number of occurrences of a value in the list.
- Parameters:
value (Any) – The value to count.
- Returns:
The number of occurrences of the value.
- Return type:
int
- extend(obj)[source]
Extend the list by appending elements from the iterable.
- Parameters:
obj (Iterable) – The iterable to extend the list with.
- index(value)[source]
Return the first index of a value in the list.
- Parameters:
value (Any) – The value to find.
- Returns:
The first index of the value.
- Return type:
int
- Raises:
ValueError – If the value is not present.
- insert(index, obj)[source]
Insert an object at a specified index in the list.
- Parameters:
index (int) – The index to insert the object at.
obj (Any) – The object to insert.
- pop(index=-1)[source]
Remove and return the item at the specified index in the list.
- Parameters:
index (int, optional) – The index to remove the item from. Defaults to -1 (the last item).
- Returns:
The removed item.
- Return type:
Any
- remove(value)[source]
Remove the first occurrence of a value from the list.
- Parameters:
value (Any) – The value to remove.
- class jsify.jsify.Object(o)[source]
Bases:
object
The Object class is designed to provide convenient access to object properties using dot notation instead of square brackets, enhancing code readability and ease of use. It acts as a wrapper around original objects, preserving their data without duplication or alteration. This wrapper supports nested access to properties, allowing deeper navigation into the original data using dot notation. Essentially, Object facilitates intuitive and simplified interaction with JSON-like objects, while maintaining the integrity and structure of the original data.
- class jsify.jsify.Tuple(o, *args)[source]
Bases:
Object
A JSON-like tuple object that extends Object and provides tuple-specific methods.
- Parameters:
o (tuple or object) – The original tuple or object to wrap.
args – Additional elements to include in the tuple.
- copy(deep=False)[source]
Create a shallow or deep copy of the tuple.
- Parameters:
deep (bool) – True if the object should be deep copied, False otherwise.
- Returns:
A copy of the tuple.
- Return type:
- jsify.jsify.deep_unjsify(obj)[source]
Convert a Object to its original representation, performing a deep conversion.
- Parameters:
obj (Object or Any) – The object to convert.
- Returns:
The original object if obj is a Object, otherwise returns obj.
- Return type:
Any
- jsify.jsify.jsified_get(obj, item, *args, **kwargs)[source]
Get an item from the object, returning a default value if the item does not exist.
- jsify.jsify.jsified_items(obj)[source]
Return a new view of the dictionary’s items (key, value pairs).
- jsify.jsify.jsified_pop(obj, item, *args, **kwargs)[source]
Remove the specified item from the object and return its value.
- jsify.jsify.jsified_popitem(obj, *args, **kwargs)[source]
Remove and return a (key, value) pair from the object.
- Parameters:
obj (Object or Any) – The object to pop the item from.
args – Additional arguments.
kwargs – Additional keyword arguments.
- Returns:
The popped (key, value) pair.
- Return type:
tuple
- jsify.jsify.jsified_setdefault(obj, value, default, *args, **kwargs)[source]
Insert a key with a default value if the key is not in the dictionary.
- jsify.jsify.jsified_update(obj, value, *args, **kwargs)[source]
Update the dictionary with the key-value pairs from another dictionary.
- jsify.jsify.jsify(o, **kwargs)[source]
Convert a dictionary, list, or tuple to its corresponding Object.
- jsify.jsify.properties_exist(obj, *path)[source]
Check if a series of properties exist in the JSON object.
- Parameters:
obj (Object or Any) – The JSON object to check.
path (str) – The sequence of properties to check.
- Returns:
True if the properties exist, False otherwise.
- Return type:
bool or PropertiesExistResult