Giter VIP home page Giter VIP logo

data-converter-package's Introduction

DevelopersToolbox logo
Github Build Status License Created
Release Released Commits since release

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}")

data-converter-package's People

Contributors

dependabot[bot] avatar tgwolf avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.