Skip to main content

dumper and loader for various file formats.

Project description

# Anyloadump

[![Build Status](https://travis-ci.org/knknkn1162/anyloadump.svg?branch=master)](https://travis-ci.org/knknkn1162/anyloadump)
[![Coverage Status](https://coveralls.io/repos/github/knknkn1162/anyloadump/badge.svg?branch=master)](https://coveralls.io/github/knknkn1162/anyloadump?branch=master)
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](LICENSE)

The Python library anyloadump helps to load a file or to dump to a file briefly in various file formats (e.g. json, pickle, yaml, toml..) with importing modules dynamically.

## Motivation

If the multiple types of file are loaded or dumped, boilerplate code will be generated and scattered as follows:

```python
# import each module.
import json, pickle
with open(json_file, "r") as f:
json_obj = json.load(f)

## do something useful

# almost the same as above! Some feel this annoying.
with open(pickle_file, "rb") as f:
pickle_obj = pickle.load(f)
```

This code goes against DRY(Don't repeat yourself) principle. Let's apply DRY in the problem.

```python
# import json, pickle,... <= unnecessary expression because of dynamic importing
# what we want to do
def generalized_load(file):
with open(file, get_mode()) as fp:
obj = get_module().load(fp)

# or dump version
def generalized_dump(obj, file):
with open(file, get_mode()) as fp:
get_module().dump(obj, fp)
```

Anyloadump helps to load a file or to dump to a file briefly in various file types, and provides dump/load/dumps/loads method like [uiri/toml](https://github.com/uiri/toml).

## Installation

To install the latest release on PyPi, simply run:

```python
pip install anyloadump
```

## Tutorial

Here is quick tutorial and is so easy to use:)

```python
import anyloadump as ald

json_file = "sample.json"
obj = ald.load(json_file)

# binary file can be also loaded properly.
pickle_file = "sample.pickle"
obj = ald.load(pickle_file)

# use library from PyPI. `pip install pyyaml` in advance.
yaml_file = "sample.yaml"
obj = ald.load(yaml_file)

# set encoding option in open method
pickle_file = "sample.pickle"
obj = ald.load(pickle_file, encoding="utf-8")

# set default_flow_style=False, allow_unicode=True in yaml
yaml_file = "sample.yaml"
obj = ald.load(yaml_file, default_flow_style=False, allow_unicode=True)

# ---

obj = get_obj()
# Of course, you can use dump method likewise:
json_file = "sample.json"
ald.dump(obj, json_file)

# binary file can be also loaded properly.
pickle_file = "sample.pickle"
ald.dump(obj, pickle_file)

# when use loads/dumps, fmt argument is required
ald.dumps(obj, fmt="json")
```

## Requirements

Anyloadump makes use of the art of duck typing to modules, and the imported module meets a few requirements and options:

1. `load(fp, **kwargs)` # fp : file object.
2. `dump(obj, fp, **kwargs)` # obj : Python object, fp : file object
3. (Optional) `loads(s, **kwargs)` # serialized bytes or strs
4. (Optional) `dumps(obj, **kwargs)` # obj : Python object

Note)
1. argument names (obj, fp) are arbitrary.
2. kwargs are the keyword arguments that are optional arguments of load/dump/loads/dumps method.

Check it out that `json, pickle, toml` modules have 1\~4 respectively, by contrast `pyyaml` library only 1\~2.
If you run `anyloadump.loads("sample.yaml")`, CharsetNotInferredError is raised.



## Note

+ Anyloadump imports module dynamically with `importlib.import_module` method. So if you want to use external library such as [yaml/pyyaml](https://github.com/yaml/pyyaml) or [uiri/toml](https://github.com/uiri/toml), run `pip install` in advance. Or raise ImportError/ModuleNotFoundError(new in Python3.6).


Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

anyloadump-0.1.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

anyloadump-0.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file anyloadump-0.1.tar.gz.

File metadata

  • Download URL: anyloadump-0.1.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for anyloadump-0.1.tar.gz
Algorithm Hash digest
SHA256 73bc90514869f6a515f7e90f701c36b4f6284a4a53cb390ce7fcd707f0dda7e5
MD5 6c27e2bf95e6c0fbc64af38e0577f101
BLAKE2b-256 94fa398002ff135abe64a26b84bf67927848e97532bda547a1d22d01feda6e6c

See more details on using hashes here.

File details

Details for the file anyloadump-0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for anyloadump-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3a4b727e875309bd38b20bc6f358aefb49f535ee335366331330e2f4085b0a00
MD5 85852e056b76f0459a02a78d302cd7b1
BLAKE2b-256 967a2d3aac0e48162838e9a5630b7a0da98ecd257a8441a135555c12340b8c56

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page