<commit_before>#  Author: <NAME> <<EMAIL>>
#
#  Copyright (C) 2007-2015 by <NAME>
#
#  gtkmvc3 is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  gtkmvc3 is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor,
#  Boston, MA 02110, USA.
#
#  For more information on gtkmvc3 see <https://github.com/roboogle/gtkmvc3>
#  or email to the author <NAME> <<EMAIL>>.
#  Please report bugs to <https://github.com/roboogle/gtkmvc3/issues>
#  or to <<EMAIL>>.

import inspect
import os
import types

def getmembers(_object, _predicate):
    """This is an implementation of inspect.getmembers, as in some versions
    of python it may be buggy.
    See issue at http://bugs.python.org/issue1785"""
    # This should be:
    #return inspect.getmembers(_object, _predicate)

    # ... and it is re-implemented as:
    observers = []
    for key in dir(_object):
        try: m = getattr(_object, key)
        except AttributeError: continue
        if _predicate(m): observers.append((key, m))
        pass
    return observers


def cast_value(val, totype):
    """
    Take a value and return it in a different type. If not possible raise
    :exc:`TypeError`.

    *val* an arbitrary object.

    *totype* a class, e.g. :class:`str`.

    .. note::
       Casting the empty string to a numeric type returns zero.
    """
    t = type(val)
    if issubclass(t, totype): return val
    if issubclass(totype, bytes): return bytes(val)
    if issubclass(totype, str): return str(val)

    if (issubclass(totype, (int, float)) and
        issubclass(t, (bytes, str, int, float))):
            if val: return totype(float(val))
            else: return totype(0)

    raise TypeError("Not able to cast " + str(t) + " to " + str(totype))

# ======================================================================
# This is taken from python 2.6 (os.path.relpath is supported in 2.6)
#
# Copyright (c) 2001 Python Software Foundation; All Rights Reserved
# This code about relpath is covered by the Python Software Foundation
# (PSF) Agreement. See http://docs.python.org/license.html for details.
# ======================================================================
def __posix_relpath(path, start=os.curdir):
    """Return a relative version of a path"""

    if not path: raise ValueError("no path specified")

    start_list = os.path.abspath(start).split(os.sep)
    path_list = os.path.abspath(path).split(os.sep)

    # Work out how much of the filepath is shared by start and path.
    i = len(os.path.commonprefix([start_list, path_list]))

    rel_list = [os.pardir] * (len(start_list)-i) + path_list[i:]
    if not rel_list: return os.curdir
    return os.path.join(*rel_list)
<commit_msg>Fix in the way str are casted to bool<commit_after>#  Author: <NAME> <<EMAIL>>
#
#  Copyright (C) 2007-2015 by <NAME>
#
#  gtkmvc3 is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  gtkmvc3 is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor,
#  Boston, MA 02110, USA.
#
#  For more information on gtkmvc3 see <https://github.com/roboogle/gtkmvc3>
#  or email to the author <NAME> <<EMAIL>>.
#  Please report bugs to <https://github.com/roboogle/gtkmvc3/issues>
#  or to <<EMAIL>>.

import inspect
import os
import types

def getmembers(_object, _predicate):
    """This is an implementation of inspect.getmembers, as in some versions
    of python it may be buggy.
    See issue at http://bugs.python.org/issue1785"""
    # This should be:
    #return inspect.getmembers(_object, _predicate)

    # ... and it is re-implemented as:
    observers = []
    for key in dir(_object):
        try: m = getattr(_object, key)
        except AttributeError: continue
        if _predicate(m): observers.append((key, m))
        pass
    return observers


def cast_value(val, totype):
    """
    Take a value and return it in a different type. If not possible raise
    :exc:`TypeError`.

    *val* an arbitrary object.

    *totype* a class, e.g. :class:`str`.

    .. note::
       Casting the empty string to a numeric type returns zero.
    """
    t = type(val)
    if issubclass(t, totype):
        return val  # no cast needed
    if issubclass(totype, bytes):
        return bytes(val)
    if issubclass(totype, str):
        return str(val)
    if issubclass(totype, bool):
        if issubclass(t, str):
            _v = val.lower()
            if _v in ("true", "yes", "y"):
                return True
            if _v in ("false", "no", "n"):
                return False
            raise TypeError("Not able to cast value: " + str(val) +
                            " with " + str(t) +
                            " to " + str(totype))

    if (issubclass(totype, (int, float)) and
        issubclass(t, (bytes, str, int, float))):
        if val:
            return totype(float(val))
        else:
            return totype(0)

    raise TypeError("Not able to cast value:" + str(val) +
                    " with " + str(t) +
                    " to " + str(totype))


# ======================================================================
# This is taken from python 2.6 (os.path.relpath is supported in 2.6)
#
# Copyright (c) 2001 Python Software Foundation; All Rights Reserved
# This code about relpath is covered by the Python Software Foundation
# (PSF) Agreement. See http://docs.python.org/license.html for details.
# ======================================================================
def __posix_relpath(path, start=os.curdir):
    """Return a relative version of a path"""

    if not path: raise ValueError("no path specified")

    start_list = os.path.abspath(start).split(os.sep)
    path_list = os.path.abspath(path).split(os.sep)

    # Work out how much of the filepath is shared by start and path.
    i = len(os.path.commonprefix([start_list, path_list]))

    rel_list = [os.pardir] * (len(start_list)-i) + path_list[i:]
    if not rel_list: return os.curdir
    return os.path.join(*rel_list)

{'fraction_non_alphanumeric': 0.1409367482665912, 'fraction_numerical': 0.009622187632658838, 'mean_word_length': 3.0574052812858783, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 4, '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': '1188517', 'n_tokens_mistral': 2163, 'n_tokens_neox': 2079, 'n_words': 787}