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
(*_, **kwargs: Dict[str, Any])¶ Bases:
collections.abc.MutableMapping
,collections.abc.KeysView
The dataclass dict. It automatically transforms any class that inherits it into a dataclass.
-
__contains__
(field_name)¶ Return true if the field exist inside the input dataclass
-
__delitem__
(field_name, default=None)¶ Remove the field from the dataclass
-
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)¶ Create and return a new object. See help(type) for accurate signature.
- Return type
-
__setattr__
(key, value)¶ Implement setattr(self, name, value).
-
pop
(field_name, default=None)¶ Remove the field from the dataclass
-
-
dataclass_dict.
add_field
(dataclass_instance, field_name, field_type, field_value=None)¶ Create a new dataclass field
-
dataclass_dict.
check_field
(dataclass_instance, field_name)¶ Return true if the field exist inside the input dataclass
-
dataclass_dict.
delete_field
(dataclass_instance, field_name, default=None)¶ Remove the field from the dataclass
Threading¶
-
dataclass_dict.threaded_request.
load_json_from_url
(*urls, **kwargs)¶ Load one or more json from the urls
Utils¶
- created
2019-07-29
- author
Leandro (Cerberus1746) Benedet Garcia
-
dataclass_dict.utils.
add_field
(dataclass_instance, field_name, field_type, field_value=None)¶ Create a new dataclass field
-
dataclass_dict.utils.
check_field
(dataclass_instance, field_name)¶ Return true if the field exist inside the input dataclass
-
dataclass_dict.utils.
delete_field
(dataclass_instance, field_name, default=None)¶ Remove the field from the dataclass
-
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.5¶
Added¶
If the creation dictionary have a string that is not a valid variable name, it will raise an
AssertionError
valid_variable()
was 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 onedict
.
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¶
__new__()
and__init_subclass__()
now calls their parents withsuper()
If a class inherited
DataclassDict
and it didn’t have a field with annotations, it would raise an error. That’s fixed now.