Welcome to dataclass_dict’s documentation!

All this package does is create a dataclass so you can straight up use it as if it was a dictionary. Here’s a basic example:

from dataclass_dict import create_dataclass_dict

# Generate a instance
instance = create_dataclass_dict({"name": "Test", "value": 10})

# prints "Test"
print(instance.name)

# also prints "Test"
print(instance["name"])

# prints "Test" and deletes the field "name"
print(instance.pop("name")

Also, you can automatically generate a dataclass with a json like this:

from dataclass_dict import dataclass_from_json

json_code = """{
    "name": "Test",
    "value": 10
}"""

instance = dataclass_from_json(json_code)

If you’d like, you can load a json file straight from a url like this:

from dataclass_dict import dataclass_from_url

dataclass_from_url("json_url")

Plus, if you pass multiple parameters this way:

from dataclass_dict import dataclass_from_url

dataclass_from_url("json_url_1", "json_url_2")

They will be downloaded at the same time using threads.

Keep in mind that all parameters from the function json.dumps() works with the dataclass_from_json() and dataclass_from_url() so you can write special parsers.

API Reference

Dataclass Dict

created:2019-09-28
author:Leandro (Cerberus1746) Benedet Garcia
class dataclass_dict.DataclassDict(mapping)

Bases: collections.abc.MutableMapping, collections.abc.KeysView

The dataclass dict. It automatically transforms any class that inherits it into a dataclass.

__contains__(field_name: str) → bool

Return true if the field exist inside the input dataclass

Parameters:
  • dataclass_instance – The dataclass to check
  • field_name – The name of the field to check
__delitem__(field_name: str, default: Optional[Any] = None) → Any

Remove the field from the dataclass

Parameters:
  • dataclass_instance – The dataclass to delete the field from
  • field_name – The field name to delete
  • default – the value to be returned if the field doesn’t exist
Raises:

KeyError – If default is None and the field doesn’t exist

classmethod __init_subclass__(**kwargs)

This method is called when a class is subclassed.

The default implementation does nothing. It may be overridden to extend subclasses.

static __new__(cls, *_, **kwargs) → dataclass_dict.DataclassDict

Create and return a new object. See help(type) for accurate signature.

__setattr__(key: Union[str, int], value: Any)

Implement setattr(self, name, value).

pop(field_name: str, default: Optional[Any] = None) → Any

Remove the field from the dataclass

Parameters:
  • dataclass_instance – The dataclass to delete the field from
  • field_name – The field name to delete
  • default – the value to be returned if the field doesn’t exist
Raises:

KeyError – If default is None and the field doesn’t exist

update_from_json(json_input: str)

Exactly like update() but it loads data from a json string.

Parameters:json_input – The input json string.
dataclass_dict.add_field(dataclass_instance: object, field_name: str, field_type: Type[Any], field_value: Optional[Any] = None)

Create a new dataclass field

Parameters:
  • dataclass_instance – The input dataclass
  • field_name – The name of the field
  • field_type – The field type
  • field_value – The value of the field
dataclass_dict.check_field(dataclass_instance: object, field_name: str) → bool

Return true if the field exist inside the input dataclass

Parameters:
  • dataclass_instance – The dataclass to check
  • field_name – The name of the field to check
dataclass_dict.delete_field(dataclass_instance: object, field_name: str, default: Optional[Any] = None) → Any

Remove the field from the dataclass

Parameters:
  • dataclass_instance – The dataclass to delete the field from
  • field_name – The field name to delete
  • default – the value to be returned if the field doesn’t exist
Raises:

KeyError – If default is None and the field doesn’t exist

dataclass_dict.load_json_from_url(*urls, **kwargs) → Union[List[Union[Dict[str, Any], Any]], Dict[str, Any], List[Union[List[Union[Dict[str, Any], Any]], Dict[str, Any]]]]

Load one or more json from the urls

Parameters:urls – one or more urls to be loaded

Threading

dataclass_dict.threaded_request.load_url(*urls) → List[dataclass_dict.threaded_request.ThreadedGetData]

Load one or more urls executing them from threads

Parameters:urls – one or more urls to be loaded
dataclass_dict.threaded_request.load_json_from_url(*urls, **kwargs) → Union[List[Union[Dict[str, Any], Any]], Dict[str, Any], List[Union[List[Union[Dict[str, Any], Any]], Dict[str, Any]]]]

Load one or more json from the urls

Parameters:urls – one or more urls to be loaded

Utils

created:2019-07-29
author:Leandro (Cerberus1746) Benedet Garcia
dataclass_dict.utils.add_field(dataclass_instance: object, field_name: str, field_type: Type[Any], field_value: Optional[Any] = None)

Create a new dataclass field

Parameters:
  • dataclass_instance – The input dataclass
  • field_name – The name of the field
  • field_type – The field type
  • field_value – The value of the field
dataclass_dict.utils.check_field(dataclass_instance: object, field_name: str) → bool

Return true if the field exist inside the input dataclass

Parameters:
  • dataclass_instance – The dataclass to check
  • field_name – The name of the field to check
dataclass_dict.utils.delete_field(dataclass_instance: object, field_name: str, default: Optional[Any] = None) → Any

Remove the field from the dataclass

Parameters:
  • dataclass_instance – The dataclass to delete the field from
  • field_name – The field name to delete
  • default – the value to be returned if the field doesn’t exist
Raises:

KeyError – If default is None and the field doesn’t exist

dataclass_dict.utils.item_zip(*dicts_input)

Function to iterate across multiple dictionaries

An example in how to use this function is:

first_dict = {"first": 1}
second_dict = {"second": 2}

for first_key, first_var, second_key, second_var in item_zip(first_dict , second_dict):
    #prints first, 1
    print(first_key, first_var)
    #prints second, 2
    print(second_key, second_var)
dataclass_dict.utils.valid_variable(name)

Check if string is a valid keyword name

Parameters:name – the string to be checked

Changelogs

0.0.6

Changed

  • The software will now tell which variable has a invalid name.

0.0.5

Added

0.0.4

Fixed

  • Now, the package should be able to be installed normally. The package name was being identified as src

0.0.3

Added

  • Any attribute, including parent ones, that starts with underscore will be ignored.
  • item_zip() was added. It iterates between more than one dict.

Fixed

  • Now, indeed, anything started with underscore will be ignored.

0.0.2

Changed

  • Any attribute starting with a _ will not be added to the dataclass, but will be available normally

Fixed

0.0.1.1

Added

  • Added Tidelif information in the readme

Fixed

  • Fixed informations in the setup.py file such as descriptions and repository link.

0.0.1

Added

  • Package released