• This repository has been archived on 04/Oct/2019
  • Stars
    star
    159
  • Rank 234,533 (Top 5 %)
  • Language
    Python
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 5 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Import configuration file for Pythonista

ezcf

Build Status Supported Python versions PyPI version Coverage Status

ezcf stands for easy configuration, it allows you to import JSON/YAML/INI/XML like .py files. It is useful whenever you need to read from these formats, especially for reading configuration files.

OK, stop talking, show us some code!

On the left is what you'll normally do, on the right is the ezcf way.
All you need is import ezcf first, then import filename without extension. Nothing else!

For instance, here we want to load file config.json. With a single line of code from config import *, everything is done and you're happy.

Install

pip install ezcf

If you run into error: yaml.h: No such file or directory, don't worry, you can still use ezcf without any problem.

Supported File Types

ezcf supports JSON, YAML, INI and XML with extension json, yaml, yml, ini, xml.

Sample Usage

Let's start with an easy case:

├── sample1.py
└── sample1.json  

sample1.py and sample1.json are in the same directory. We want to read sample1.json in sample1.py, here's how:

"""
# sample1.json
{
    "hello": "world"
}
"""

# sample1.py
import ezcf
from sample1 import hello

print(hello)  # 'world'

It's that easy.


That's cool, but we usually put config files in a separate folder. Can ezcf deal with that?
├── conf
│   ├── __init__.py
│   └── sample2.yaml
└── sample2.py

Why not?

"""sample2.yaml
---
Time: 2001-11-23 15:02:31 -5
User: ed
warning:
  This is a warning.
---
Stack:
  - file: TopClass.py
    line: 23
    code: |
      x = MoreObject("345\n")
  - file: MoreClass.py
    line: 58
    code: |-
      foo = bar
"""

# sample2.py
import ezcf
from conf.sample2 import Time, User, warning, Stack

Time  # datetime.datetime(2001, 11, 23, 20, 2, 31)
User  # ed
warning  # This is a warning.
Stack  # [{'line': 23, 'code': 'x = MoreObject("345\\n")\n', 'file': 'TopClass.py'}, {'line': 58, 'code': 'foo = bar', 'file': 'MoreClass.py'}]

ezcf supports all kinds of valid import statements. These statements are equivalent:

from conf.sample2 import Time, User, warning, Stack
from conf.sample2 import *
import conf.sample2  # then use conf.sample2.Time/User/warning/Stack
import conf.sample2 as cs  # then use cs.Time/User/warning/Stack

In a word, you can assume they're just regular python files.(Currently ezcf only supports files with utf-8 encoding)

What about relative import? Yes, ezcf supports relative import, as long as you use it correctly.

Note

  1. Be careful importing YAML which contains multiple documents: if there exists keys with the same name, only one of them will be loaded. So it's better not to use multiple documents;
  2. All values in .ini files are kept as it is and loaded as a string;
  3. Since XML only allows single root, the whole xml will be loaded as one dict with root's name as variable name;
  4. Use valid variable names, this means key strings in JSON/YAML/INI/XML should be valid Python variable name. Invalid variable name won't do any harm to your program nor will it crash, but you can't use them as expected.

Run Tests

python setup.py test

Roadmap

  • Use dot to seperate folder/subfolder/file
  • Unicode support
  • JSON support
  • YAML support
  • INI support
  • XML support
  • Auto encoding detect?
  • CI
  • coverage
  • pypi

License

MIT

More Repositories

1

Cyberbrain

Python debugging, redefined.
Python
2,507
star
2

pdir2

Pretty dir() printing with joy🍺
Python
1,325
star
3

Python-Type-Challenges

Master Python typing (type hints) with interactive online exercises!
Python
435
star
4

PyPunchP2P

Python实现NAT穿透+STUN+TURN+P2P聊天 | Python P2P chat
Python
428
star
5

My_Blog

My Django Blog
HTML
209
star
6

Cyberbrain-Deprecated

[Deprecation] This project has been deprecated. Development moved to github.com/laike9m/Cyberbrain
Python
166
star
7

zhihu-card

用卡片在个人网站上展示知乎账户
CSS
121
star
8

rsshub-zhihu-helper

如果你希望通过 RSSHub 浏览知乎,那么这个项目或许可以帮到你。
Python
70
star
9

Bad-Apple-Console

ASCII Art: Bad 🍎 in Windows console
C++
41
star
10

logseq-chatgpt

Talk to ChatGPT directly from Logseq
JavaScript
34
star
11

f

Log to a file without modifying code, with just one line 📝
Python
19
star
12

split_p

漫画图片分割 | split images for manga reading
Python
12
star
13

peerjs-with-nodewebkit-tutorial

How to integrate PeerJs into node-webkit/NW.js app
JavaScript
11
star
14

all_in_utf8

Recursively find and convert files to utf8 encoding
Python
5
star
15

VideoChat

elgg video chat plugin / elgg 视频通话插件
PHP
4
star
16

learn_socket

Learn Python socket programming
Python
3
star
17

CU_login

ChinaUnicom 自动登录断线重连
Python
3
star
18

logseq-twitter-sync

JavaScript
2
star
19

MusicService

读取mp3生成markdown格式的音乐列表
Python
2
star
20

Xylect

TypeScript
2
star
21

DataMining

DataMining Project
Python
1
star
22

spk_cmp2methods

compare 2 methods of speaker change detection
Python
1
star
23

h2-playground

Python
1
star
24

mysite1

My Django Project,consisting of my personal blog and the Mike Hilbert's tutorial things
Python
1
star
25

train

Python
1
star
26

Algorithm_homework_code

Python
1
star
27

pipAlwaysLatest

A wrapper around pip, install/generate requirements with the latest version
Python
1
star
28

Slides

HTML
1
star
29

cyberbrain-examples

Python
1
star
30

DumpRenrenPosts2Markdown

人人日志导出工具/Dump Renren Posts to Markdown
Python
1
star