Overview
The wolfsoftware.data-converter module provides functionalities for converting data between JSON, XML, and YAML formats.
It includes methods to convert dictionaries to and from these formats, making it a versatile tool for data transformation in Python applications.
Installation
First, install the package using pip:
pip install wolfsoftware.data-converter
Usage
Importing the Module
To use the DataConverter class, import it from the wolfsoftware.data_converter package:
from wolfsoftware.data_converter import DataConverter, DataConverterError
Initializing the DataConverter
The DataConverter class requires data and its type as parameters during initialization. Supported data types are json, xml, dict, and yaml.
# JSON data example
json_data = '{"name": "John", "age": 30}'
converter = DataConverter(json_data, data_type='json')
# XML data example
xml_data = '<person><name>John</name><age>30</age></person>'
converter = DataConverter(xml_data, data_type='xml')
# Dictionary data example
dict_data = {'name': 'John', 'age': 30}
converter = DataConverter(dict_data, data_type='dict')
# YAML data example
yaml_data = 'name: John\nage: 30\n'
converter = DataConverter(yaml_data, data_type='yaml')
Converting Data
To JSON
Convert the data to JSON format using the to_json method:
json_output = converter.to_json()
print(json_output)
To XML
Convert the data to XML format using the to_xml method:
xml_output = converter.to_xml()
print(xml_output)
To YAML
Convert the data to YAML format using the to_yaml method:
yaml_output = converter.to_yaml()
print(yaml_output)
Example Usage
Here's a complete example demonstrating how to convert a dictionary to JSON, XML, and YAML formats:
from wolfsoftware.data_converter import DataConverter, DataConverterError
# Dictionary data
data = {'name': 'John', 'age': 30}
# Initialize DataConverter
converter = DataConverter(data, data_type='dict')
# Convert to JSON
json_output = converter.to_json()
print("JSON Output:")
print(json_output)
# Convert to XML
xml_output = converter.to_xml()
print("XML Output:")
print(xml_output)
# Convert to YAML
yaml_output = converter.to_yaml()
print("YAML Output:")
print(yaml_output)
Handling Unsupported Data Types
If an unsupported data type is provided, a DataConverterError will be raised:
try:
converter = DataConverter(data, data_type='unsupported')
except DataConverterError as e:
print(f"Error: {e}")
Release files for wolfsoftware.data-converter 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| wolfsoftware_data_converter-0.1.1.tar.gz | 8.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| wolfsoftware.data_converter-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.6 kB
Release files / wolfsoftware_data_converter-0.1.1.tar.gz
| Download URL | wolfsoftware_data_converter-0.1.1.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e554410b1227ac3db18c455eb44afebb82e16f68f17b1c275c926eb45bebc1d2
|
|
BLAKE2b-256 checksum How to use checksums |
5dcb0c85c934d6e5e6e9cde04d7209f0614250c8fdb31567d81a0802427b7495
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.4
|
Release files / wolfsoftware.data_converter-0.1.1-py3-none-any.whl
| Download URL | wolfsoftware.data_converter-0.1.1-py3-none-any.whl |
|---|---|
| Size | 6.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c91b25cb8e60c7f3a50094eb198ec434463f797e9bae150b9d9eae061493cc22
|
|
BLAKE2b-256 checksum How to use checksums |
52a4c7be500ac43be18ed3c84cf8d1381ef14fcb7dd43c5f1b9c6fa0b0a95cb0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.12.4
|