Skip to main content
# 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).


Metadata

Release files for anyloadump 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for anyloadump 0.1
File Size Uploaded Provenance Actions
anyloadump-0.1.tar.gz 4.7 kB Without provenance Details for anyloadump-0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for anyloadump 0.1
File Interpreter ABI Platform Actions
anyloadump-0.1-py3-none-any.whl Python 3 none any Details for anyloadump-0.1-py3-none-any.whl

Total release size: 12.3 kB

Release files / anyloadump-0.1.tar.gz

Download URL anyloadump-0.1.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
73bc90514869f6a515f7e90f701c36b4f6284a4a53cb390ce7fcd707f0dda7e5
BLAKE2b-256 checksum
How to use checksums
94fa398002ff135abe64a26b84bf67927848e97532bda547a1d22d01feda6e6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / anyloadump-0.1-py3-none-any.whl

Download URL anyloadump-0.1-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a4b727e875309bd38b20bc6f358aefb49f535ee335366331330e2f4085b0a00
BLAKE2b-256 checksum
How to use checksums
967a2d3aac0e48162838e9a5630b7a0da98ecd257a8441a135555c12340b8c56
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for anyloadump 0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page