<commit_before>#!/usr/bin/env python
#-*- coding: utf-8 -*-

__author__ = "<NAME>"
__version__ = "0.0.1a"

import os
import sys

python_version = sys.version()
python_version_string = ".".join(sys.version())
package_root = os.path.abspath(os.path.dirname(__file__))
python3x = python_version >= (3, 0)
python2x = python_version < (3, 0)


def join_paths(*paths):
    path = os.path.abspath(paths[0])
    for next_path in paths[1:]:
        path = os.path.join(path, next_path)
    return path


def join_root(*paths):
    path = package_root
    for next_path in paths:
        path = os.path.join(path, next_path)
    return path


def config_dict(config_file=[], auto_find=False, **config_parser_options):
    """
    Return configuration options as dictionary. Accepts either a single
    config file or a list of files. Auto find will search for all .cfg, .config
    and .ini in the execution directory and package root (unsafe but handy).
    """

    import glob
    if python2x:
        import ConfigParser as configparser
    elif python3x:
        import configparser

    cfg_parser = configparser.ConfigParser(**config_parser_options)

    cfg_files = []

    if auto_find:
        cfg_files.extend(glob.glob("*.cfg"))
        cfg_files.extend(glob.glob("*.config"))
        cfg_files.extend(glob.glob("*.ini"))
        cfg_files.extend(join_root(glob.glob("*.cfg")))
        cfg_files.extend(join_root(glob.glob("*.config")))
        cfg_files.extend(join_root(glob.glob("*.ini")))

    if not isinstance(cfg_files, list):
        if isinstance(cfg_files, str):
            cfg_files.append(cfg_files)
        else:
            raise TypeError("config_files must be a list or a string")
    else:
        cfg_files.extend(config_file)

    cfg_parser.read(cfg_files)

    return {section: {k: v for k, v in cfg_parser.items(section)}
            for section in cfg_parser.sections()}<commit_msg>Added explanations. Added a simple sort wrapper.<commit_after>#!/usr/bin/env python
#-*- coding: utf-8 -*-

__author__ = "<NAME>"
__version__ = "0.0.1"

import os
import sys

python_version = sys.version_info[0:3]
python_version_string = ".".join([str(x) for x in python_version])
package_root = os.path.abspath(os.path.dirname(__file__))
python3x = python_version >= (3, 0)
python2x = python_version < (3, 0)


def join_paths(*paths):
    """
    Join multiple paths together and return the absolute path of them.
    """
    path = os.path.abspath(paths[0])
    for next_path in paths[1:]:
        path = os.path.join(path, next_path)
    return path


def join_root(*paths):
    """
    Join any path or paths as a sub directory of the current file's directory.
    """
    path = package_root
    for next_path in paths:
        path = os.path.join(path, next_path)
    return path


def config_dict(config_file=[], auto_find=False, **config_parser_options):
    """
    Return configuration options as dictionary. Accepts either a single
    config file or a list of files. Auto find will search for all .cfg, .config
    and .ini in the execution directory and package root (unsafe but handy).
    """

    #TODO add verify path option
    import glob
    if python2x:
        import ConfigParser as configparser
    elif python3x:
        import configparser

    cfg_parser = configparser.ConfigParser(**config_parser_options)

    cfg_files = []

    if auto_find:
        cfg_files.extend(glob.glob("*.cfg"))
        cfg_files.extend(glob.glob("*.config"))
        cfg_files.extend(glob.glob("*.ini"))
        cfg_files.extend(join_root(glob.glob("*.cfg")))
        cfg_files.extend(join_root(glob.glob("*.config")))
        cfg_files.extend(join_root(glob.glob("*.ini")))

    if not isinstance(cfg_files, list):
        if isinstance(cfg_files, str):
            cfg_files.append(cfg_files)
        else:
            raise TypeError("config_files must be a list or a string")
    else:
        cfg_files.extend(config_file)

    cfg_parser.read(cfg_files)

    return {section: {k: v for k, v in cfg_parser.items(section)}
            for section in cfg_parser.sections()}


def sort_by(unordered_list, key):
    """
    Sort a list of dicts, tuples or lists by the provided dict key, or list/
    tuple position.
    """
    return sorted(unordered_list, key=lambda x: x[key])
{'fraction_non_alphanumeric': 0.11845473586222946, 'fraction_numerical': 0.006981615080288574, 'mean_word_length': 3.0894386298763084, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1323682', 'n_tokens_mistral': 1375, 'n_tokens_neox': 1339, 'n_words': 362}