-
Notifications
You must be signed in to change notification settings - Fork 0
Home
read the readme.md to understand the basic concepts
IJsonLikeObj (pyjsv.classes.interfaces.py)
An abstract class that implements a base interface:
-
statics:
- from_string(str) -> Self
- from_dict(dict) -> Self
- from_file(os.PathLike) -> Self
-
methods:
- save_file(os.PathLike) -> None // saves the file of its extension to the given path
- get_dict() -> dict
SimpleJson(_IJsonLikeObj) (pyjsv.classes.jsv_json.py)
- implement _IJsonLikeObj with no changes
SimpleXml (pyjsv.classes.jsv_xml.py)
-
statics:
- from_string(str, text_key: str, attr_prefix: str) -> Self
- from_dict(dict, text_key: str, attr_prefix: str) -> Self
- from_file(os.PathLike, text_key: str, attr_prefix: str) -> Self
-
methods:
- get_xml() -> str // return xml
- save_file(os.PathLike) -> None // saves the file of its extension to the given path
- get_dict() -> dict
SimpleToml (pyjsv.classes.jsv_toml.py)
- implement _IJsonLikeObj with no changes + get_toml method, return str
You can convert xml into dict
import requests
from pyjsv import SimpleJson, SimpleXml, SimpleToml
url = "https://iss.moex.com/iss/history/engines/stock/markets/index/securities.xml?date=2022.12.12"
my_data = SimpleXml.from_string(requests.get(url).text)
print(my_data.get_dict())
And dict into xml
import requests
from pyjsv import SimpleJson, SimpleXml, SimpleToml
url = "https://iss.moex.com/iss/history/engines/stock/markets/index/securities.json?date=2022.12.12"
my_data = SimpleXml.from_dict(requests.get(url).json())
print(my_data)
Xml into toml
import requests
from pyjsv import SimpleJson, SimpleXml, SimpleToml
url = "https://iss.moex.com/iss/history/engines/stock/markets/index/securities.xml?date=2022.12.12"
my_data = SimpleXml.from_string(requests.get(url).text)
my_toml = SimpleToml.from_dict(my_data.get_dict())
print(my_toml.get_toml())
(This example shows that our library will cope with even the most difficult volumes)
Let's imagine that we have a config.toml file, and we want to get a dictionary to work with it.
[authors]
[authors.boolmano]
name = "boolmano"
id = 5
jobs = []
[authors.boolmano2]
name = "clone of boolmano"
id = 6
jobs = []
[metadata]
wiki_version = 1.0
url = "https://github.com/BoolmanO/pyjsv/wiki/"
Imagine we want to change wiki_version to 2.0
from pyjsv import SimpleToml
path_to_file = "dev.toml"
st = SimpleToml.from_file(path_to_file)
my_toml_in_dict = st.get_dict()
my_toml_in_dict["metadata"]["wiki_version"] = 2.0
st = SimpleToml.from_dict(my_toml_in_dict)
st.save_file(path_to_file)
As a result, we get an updated dev.toml
[authors]
[authors.boolmano]
name = "boolmano"
id = 5
jobs = []
[authors.boolmano2]
name = "clone of boolmano"
id = 6
jobs = []
[metadata]
wiki_version = 2.0
url = "https://github.com/BoolmanO/pyjsv/wiki/"
A: text_key defines the name of a dictionary key that stores data with the text of a specific tag
A: attr_prefix is an attribute prefix, it is necessary in order to be able to distinguish between the attributes of the tag itself and its children
A: Suppose you have the following dict:
{"data":
{"developers": [
{"country": "USA", "rank": "Senior", "name": "Jony"},
{"country": "Russia", "rank": "Senior", "name": "Boris"}
]
}
}
Are you expecting to receive:
<?xml version="1.0" encoding="utf-8"?>
<data>
<developers country="USA" rank="Senior">Jony</developers>
<developers country="Russia" rank="Senior">Boris</developers>
</data>
To serialize correctly and place for example the name field in the text value of the tag, you must specify text_key and add a prefix to the expected attributes in the dictionary, by default @
updated dict (let's assume it is in some_values variable):
{"data":
{"developers": [
{"@country": "USA", "@rank": "Senior", "name": "Jony"},
{"@country": "Russia", "@rank": "Senior", "name": "Boris"}]
}
}
python:
sx = SimpleXml.from_dict(some_values, text_key="name")