<commit_before># -*- coding: utf-8 -*-
"""
This module contains code for creating subclasses of TraitMenu
using information about their options & flags found in a json file.

e.g., see ./data/dmenu_opts.json
"""
import logging as _logging
_logr = _logging.getLogger(__name__)
_logr.addHandler(_logging.NullHandler())

from dynmen.common import Option, Flag
from collections import namedtuple


def _prep_dict(d):
    flag = d['flag']
    d_new = {}
    # type(u'') is unicode or str type depending on python version
    if not isinstance(flag, (str, type(u''))):
        flag = max(flag, key=len)
    d_new['flag'] = flag
    d_new['name'] = flag.lstrip('-').replace('-', '_')
    try:
        d_new['help'] = d['info']
    except KeyError:
        pass
    try:
        d_new['info_text'] = d['arg']
    except KeyError:
        pass
    if d.get('arg', ''):
        d_new['klass'] = Option
    else:
        d_new['klass'] = Flag
    _logr.debug('Transformed option dictionary %r into %r', d, d_new)
    return d_new

NamedDescriptor = namedtuple('NamedDescriptor', 'name descriptor')


def dict_to_descriptor(d):
    d = _prep_dict(d)
    cls = d.pop('klass')
    name = d.pop('name')
    res = NamedDescriptor(name, cls(**d))
    _logr.debug('Created descriptor %r', res)
    return res


class AddOptions(object):
    """
    A class decorator for TraitMenus which
    adds options to the menu
    """

    def __init__(self, *options):
        """Create AddOptions decorator."""
        self.options = options

    def __call__(self, cls):
        for opt in self.options:
            if getattr(cls, opt.name, None) is None:
                setattr(cls, opt.name, opt.descriptor)
                opt.descriptor.class_init(cls, opt.name)
        return cls


def load_options(filepath):
    import json
    with open(filepath, mode='rt') as fp:
        return [dict_to_descriptor(x) for x in json.load(fp)]<commit_msg>Reduce number of logging calls in generator.py<commit_after># -*- coding: utf-8 -*-
"""
This module contains code for creating subclasses of TraitMenu
using information about their options & flags found in a json file.

e.g., see ./data/dmenu_opts.json
"""
import logging as _logging
_logr = _logging.getLogger(__name__)
_logr.addHandler(_logging.NullHandler())

from dynmen.common import Option, Flag
from collections import namedtuple


def _prep_dict(d):
    flag = d['flag']
    d_new = {}
    # type(u'') is unicode or str type depending on python version
    if not isinstance(flag, (str, type(u''))):
        flag = max(flag, key=len)
    d_new['flag'] = flag
    d_new['name'] = flag.lstrip('-').replace('-', '_')
    try:
        d_new['help'] = d['info']
    except KeyError:
        pass
    try:
        d_new['info_text'] = d['arg']
    except KeyError:
        pass
    if d.get('arg', ''):
        d_new['klass'] = Option
    else:
        d_new['klass'] = Flag
    return d_new

NamedDescriptor = namedtuple('NamedDescriptor', 'name descriptor')


def dict_to_descriptor(d):
    d = _prep_dict(d)
    cls = d.pop('klass')
    name = d.pop('name')
    res = NamedDescriptor(name, cls(**d))
    return res


class AddOptions(object):
    """
    A class decorator for TraitMenus which
    adds options to the menu
    """

    def __init__(self, *options):
        """Create AddOptions decorator."""
        self.options = options

    def __call__(self, cls):
        _logr.debug('Adding %r options to %r', len(self.options), cls.__name__)
        for opt in self.options:
            if getattr(cls, opt.name, None) is None:
                setattr(cls, opt.name, opt.descriptor)
                opt.descriptor.class_init(cls, opt.name)
        return cls


def load_options(filepath):
    import json
    _logr.debug('Loading menu options from %r', filepath)
    with open(filepath, mode='rt') as fp:
        return [dict_to_descriptor(x) for x in json.load(fp)]
{'fraction_non_alphanumeric': 0.13041250320266462, 'fraction_numerical': 0.0005124263387138099, 'mean_word_length': 2.83874139626352, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2097015', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1239, 'n_words': 356}