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.

append(obj)[source]

Append an object to the list.

Parameters:

obj (Any) – The object to append.

clear()[source]

Clear all elements from 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:

List

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.

reverse(*args, **kwargs)[source]

Reverse the elements of the list in place.

Parameters:
  • args – Additional arguments.

  • kwargs – Additional keyword arguments.

sort(*args, **kwargs)[source]

Sort the elements of the list in place.

Parameters:
  • args – Additional arguments.

  • kwargs – Additional keyword arguments.

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.PropertiesExistResult(value=None)[source]

Bases: object

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:

Tuple

count(value)[source]

Return the number of occurrences of value in the tuple.

Parameters:

value (Any) – The value to count in the tuple.

Returns:

The number of occurrences of value.

Return type:

int

index(value)[source]

Return the first index of value in the tuple.

Raises ValueError if the value is not present.

Parameters:

value (Any) – The value to find in the tuple.

Returns:

The first index of the value.

Return type:

int

Raises:

ValueError – If the value is not present.

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_copy(obj, deep=True)[source]

Create a shallow or deep copy of the object.

Parameters:
  • obj (Object or Any) – The object to copy.

  • deep (bool) – True if the object should be deep copied, False otherwise.

Returns:

A copy of the object.

Return type:

Object or 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.

Parameters:
  • obj (Object or Any) – The object to get the item from.

  • item (Any) – The item to get.

  • args – Additional arguments.

  • kwargs – Additional keyword arguments.

Returns:

The value of the item.

Return type:

Object or Any

jsify.jsify.jsified_items(obj)[source]

Return a new view of the dictionary’s items (key, value pairs).

Parameters:

obj (Object, List, Tuple or Any) – The object to get the items from.

Returns:

A view object displaying a list of the objects’s items (in case of list or tuple these would be values with keys being their indexes in string format).

Return type:

Iterator

jsify.jsify.jsified_keys(obj)[source]

Return a new view of the dictionary’s keys.

Parameters:

obj (Object, List, Tuple or Any) – The object to get the keys from.

Returns:

A view object displaying a list of the object’s keys (in case of list or tuple these would be the indexes in string format).

Return type:

Iterator

jsify.jsify.jsified_pop(obj, item, *args, **kwargs)[source]

Remove the specified item from the object and return its value.

Parameters:
  • obj (Object or Any) – The object to pop the item from.

  • item (Any) – The item to pop.

  • args – Additional arguments.

  • kwargs – Additional keyword arguments.

Returns:

The value of the popped item.

Return type:

Object or Any

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.

Parameters:
  • obj (Object or Any) – The object to set the default value in.

  • value (Any) – The key to set the default value for.

  • default (Any) – The default value to set.

  • args – Additional arguments.

  • kwargs – Additional keyword arguments.

Returns:

The value of the key.

Return type:

Object or Any

jsify.jsify.jsified_update(obj, value, *args, **kwargs)[source]

Update the dictionary with the key-value pairs from another dictionary.

Parameters:
  • obj (Object or Any) – The object to update.

  • value (dict) – The dictionary to update from.

  • args – Additional arguments.

  • kwargs – Additional keyword arguments.

Returns:

The updated object.

Return type:

Object or Any

jsify.jsify.jsified_values(obj)[source]

Return a new view of the dictionary’s values.

Parameters:

obj (Object or Any) – The object to get the values from.

Returns:

A view object displaying a list of the dictionary’s values.

Return type:

Iterator

jsify.jsify.jsify(o, **kwargs)[source]

Convert a dictionary, list, or tuple to its corresponding Object.

Parameters:
  • o (dict, list, tuple or Any) – The object to convert.

  • kwargs – Additional keyword arguments.

Returns:

The converted Object.

Return type:

Object, Dict, List, or Tuple

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

jsify.jsify.unjsify(obj)[source]

Convert a Object to its original representation.

Parameters:

obj (Object or Any) – The object to convert.

Returns:

The original object if obj is a Object, otherwise returns obj.

Return type:

Any