Skip to content

Commit

Permalink
Merge pull request #2 from Himenon/develop
Browse files Browse the repository at this point in the history
v0.0.2 OK.
  • Loading branch information
Himenon committed Jul 6, 2016
2 parents bc9bc85 + 6a4b0ae commit 2e37f35
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 57 deletions.
8 changes: 8 additions & 0 deletions .coveragerc
@@ -0,0 +1,8 @@
[run]
branch = True
omit = '*/site-packages/*'

[report]
omit =
*/python?.?/*
*/site-packages/nose/*
16 changes: 9 additions & 7 deletions .travis.yml
Expand Up @@ -2,12 +2,14 @@ sudo: false

language: python

python:
- "2.7"
- "3.5"

env:
matrix:
- TOXENV=py27
- TOXENV=py33
- TOXENV=py34
install:
- pip install tox

script:
- tox
- pip install coveralls
script: tox
after_script:
- coveralls
65 changes: 39 additions & 26 deletions OrderedFormat/formatter.py
Expand Up @@ -16,50 +16,47 @@ def iter_depth(arr):
return iter_depth(list(arr))
return 0


def load_dict_val(name):
ext = os.path.splitext(name)[1]
with open(name, 'r') as stream:
return parse_string_to_dict(stream.read(), ext)


def parse_string_to_dict(string_data, type=None):
def parse_string_to_dict(string_data, load_type=None):
if not isinstance(string_data, str):
raise ValueError("Input Data is not 'String'")
if not type:
if not load_type:
raise ValueError("Not found data type. You must choose type 'yaml' or 'json'")

json_flag = re.match("\.json|json", type)
yaml_flag = re.match("yml|\.yml|yaml|\.yaml", type)
json_flag = re.match("\.json|json", load_type)
yaml_flag = re.match("yml|\.yml|yaml|\.yaml", load_type)

if yaml_flag:
return yaml.load(string_data)
if json_flag:
return json.loads(string_data)

def load_dict_val(filename):
ext = os.path.splitext(filename)[1]
with open(filename, 'r') as stream:
return parse_string_to_dict(stream.read(), ext)

def load_ordered_keys(key_data, filename=None, ext=None):
def load_ordered_keys(filename, raw_txt=None, load_type=None):

if key_data and filename:
if raw_txt and filename:
return -1

if filename:
if not os.path.exists(filename):
raise IOError("Not found '{filename}'".format(filename=filename))
if not ext:
ext = os.path.splitext(filename)[1]
if not load_type:
load_type = os.path.splitext(filename)[1]

if not ext:
if not load_type:
raise ValueError("You must set value for 'ext' field!")

json_flag = re.match("\.json|json", ext)
yaml_flag = re.match("yml|\.yml|yaml|\.yaml", ext)
json_flag = re.match("\.json|json", load_type)
yaml_flag = re.match("yml|\.yml|yaml|\.yaml", load_type)

if isinstance(key_data, str):
if isinstance(raw_txt, str):
if json_flag:
return json.JSONDecoder(object_pairs_hook=OrderedDict).decode(key_data)
return json.JSONDecoder(object_pairs_hook=OrderedDict).decode(raw_txt)
if yaml_flag:
return yaml.load(key_data, Loader=yamlordereddictloader.Loader)
return yaml.load(raw_txt, Loader=yamlordereddictloader.Loader)

if json_flag:
with open(filename) as stream:
Expand All @@ -68,14 +65,14 @@ def load_ordered_keys(key_data, filename=None, ext=None):
return yaml.load(open(filename), Loader=yamlordereddictloader.Loader)


def kflatten(dict_value, ordered_keys, type=None):
def kflatten(dict_value, ordered_keys, load_type=None):
_local_dict_value = dict_value
if isinstance(dict_value, str):
dict_value = parse_string_to_dict(dict_value, type=type)
_local_dict_value = parse_string_to_dict(dict_value, load_type=load_type)

ordered_and_flatten_list = []
def _kflatten(_dict_value, _ordered_keys):
if isinstance(_ordered_keys, OrderedDict):
# Todo Python 2, 3
if six.PY2:
map(lambda sub_key: _kflatten(_dict_value[sub_key], _ordered_keys[sub_key]), _ordered_keys)
else:
Expand All @@ -87,7 +84,7 @@ def _kflatten(_dict_value, _ordered_keys):
if isinstance(key, str):
ordered_and_flatten_list.append(_dict_value[key])

_kflatten(dict_value, ordered_keys)
_kflatten(_local_dict_value, ordered_keys)

if iter_depth(ordered_and_flatten_list) > 1:
return tuple(chain.from_iterable(ordered_and_flatten_list))
Expand All @@ -96,7 +93,23 @@ def _kflatten(_dict_value, _ordered_keys):


if __name__ == '__main__':
ordered_keys = load_ordered_keys(None, filename="../tests/src/test_keys.yml")
ordered_keys = load_ordered_keys("../tests/src/test_keys.yml")
dict_data = load_dict_val("../tests/src/test_data.yml")

print(kflatten(dict_data, ordered_keys))

yml_data = """
human:
name: John
age: 22
"""

key_data = """
human:
- name
- age
- name
"""

raw_keys = load_ordered_keys(None, raw_txt=key_data, load_type="yaml")
kflatten(yml_data, raw_keys, load_type="yaml")
49 changes: 30 additions & 19 deletions README.rst
Expand Up @@ -2,49 +2,57 @@
OrderedFormat
=============

|buildstatus|_
|buildstatus|_ |coverage|_

Ordered value getter from Dictionary type value.

Requirements
============

Python 2.6 upper and 3.x
Python 2.7 upper and 3.x

From a dictionary type, this can acquire a value in order of a methodical keys.

Example
Install
=======

::

pip install OrderedFormat

Usage
=====

QuickStart ::

import OrderedFormat as odf
import OrderedFormat.formatter as odf

yml_data = """
human:
name: John
age: 22
"""
yml_txt_data = """
human:
name: John
age: 22
"""

key_data = """
human:
- name
- age
- name
"""
yml_key_txt = """
human:
- name
- age
- name
"""

ordered_keys = odf.get_ordered_keys(key_data=key_data, ext="yml")
ordered_keys = odf.load_ordered_keys(None, raw_txt=yml_key_txt, load_type="yaml")
ordered_data = odf.kflatten(yml_txt_data, ordered_keys, load_type="yaml")

# ordered_data = ("John", "John", 22, "John")

ordered_data = odf.kflatten(yml_data, ordered_keys, type="yaml")
# ordered_data = ("John", "John", 22, "John")

Currentry, ``kflatten`` method return flat tuple value.

When you load keys value or dict value (json/yaml file type), you can use two method.

Using Load File Data ::

ordered_keys = odf.load_ordered_keys(key_data=None, filename="<keys_file.json | keys_file.yaml>")
ordered_keys = odf.load_ordered_keys("<keys_file.json | keys_file.yaml>")
dict_data = odf.load_dict_val("<dict_file.json | dict_file.yaml>")

odf.kflatten(dict_data, ordered_keys)
Expand All @@ -53,3 +61,6 @@ Available extension is ".yaml" , ".yml" and "json".

.. |buildstatus| image:: https://travis-ci.org/Himenon/OrderedFormat.svg?branch=master
.. _buildstatus: https://travis-ci.org/Himenon/OrderedFormat

.. |coverage| image:: https://coveralls.io/repos/github/Himenon/OrderedFormat/badge.svg?branch=master
.. _coverage: https://coveralls.io/github/Himenon/OrderedFormat
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -3,7 +3,7 @@
from codecs import open
from os import path

version = "0.0.1"
version = "0.0.2"
short_description = "Ordered value getter from Dictionary type value."
here = path.abspath(path.dirname(__file__))

Expand Down
4 changes: 2 additions & 2 deletions tests/test_formatter.py
Expand Up @@ -33,8 +33,8 @@ def test_kflatten_simple(self):
- name
"""

ordered_keys = load_ordered_keys(key_data=key_data, ext="yml")
data = kflatten(yml_data, ordered_keys, type="yaml")
ordered_keys = load_ordered_keys(None, raw_txt=key_data, load_type="yml")
data = kflatten(yml_data, ordered_keys, load_type="yaml")

eq_(data[0], "John")
eq_(data[1], "John")
5 changes: 3 additions & 2 deletions tox.ini
@@ -1,9 +1,10 @@
[tox]
envlist=py27,py350
envlist = py279

[testenv]
deps = -rrequirements.txt
nose
commands = python -V
easy_install six
nosetests
pip install coveralls
nosetests --with-coverage --cover-package=OrderedFormat

0 comments on commit 2e37f35

Please sign in to comment.