<commit_before>"""
Copyright 2011 <NAME>

This file is part of Microdrop.

Microdrop is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Microdrop 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with Microdrop.  If not, see <http://www.gnu.org/licenses/>.
"""

import sys
import re

from path import path


PROGRAM_LAUNCHED = False


def is_float(s):
    try: return (float(s), True)[1]
    except (ValueError, TypeError), e: return False


def is_int(s):
    try: return (int(s), True)[1]
    except (ValueError, TypeError), e: return False


def wrap_string(string, line_length=80, wrap_characters="\n"):    
    chars = 0
    wrapped_string = ""
    for word in string.split():
        if chars + len(word) > line_length:
            wrapped_string += wrap_characters + word + " "
            chars = len(word + wrap_characters)
        else:
            wrapped_string += word + " "
            chars += len(word) + 1
    return wrapped_string


def check_textentry(textentry, prev_value, type):
    val = textentry.get_text()
    if val and type is float:
        if is_float(val):
            return float(val)
    elif val and type is int:
        if is_int(val):
            return int(val)
    print "error" # TODO dialog error
    textentry.set_text(str(prev_value))
    return prev_value


def base_path():
    # When executing from a frozen (pyinstaller) executable...
    if hasattr(sys, 'frozen'):
        print 'FROZEN!'
        return path(sys.executable).parent
    else:
        print 'NOT FROZEN!'

    # Otherwise...
    try:
        script = path(__file__)
    except NameError:
        script = path(sys.argv[0])
    return script.parent.parent

class InvalidVersionString(Exception):
    pass

class InvalidVersion(Exception):
    pass

class Version:
    InvalidVersionString(Exception)
    
    def __init__(self, major=0, minor=0, micro=0):
        if type(major)!=int or type(minor)!=int or type(micro)!=int:
            raise InvalidVersion
        self.major = major
        self.minor = minor
        self.micro = micro

    @classmethod
    def fromstring(cls, string):
        "Initialize Version from a string"
        major, minor, micro = ('0','0','0')
        match = False<commit_msg>Replace InvalidVersion with TypeError<commit_after>"""
Copyright 2011 <NAME>

This file is part of Microdrop.

Microdrop is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Microdrop 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with Microdrop.  If not, see <http://www.gnu.org/licenses/>.
"""

import sys
import re

from path import path


PROGRAM_LAUNCHED = False


def is_float(s):
    try: return (float(s), True)[1]
    except (ValueError, TypeError), e: return False


def is_int(s):
    try: return (int(s), True)[1]
    except (ValueError, TypeError), e: return False


def wrap_string(string, line_length=80, wrap_characters="\n"):    
    chars = 0
    wrapped_string = ""
    for word in string.split():
        if chars + len(word) > line_length:
            wrapped_string += wrap_characters + word + " "
            chars = len(word + wrap_characters)
        else:
            wrapped_string += word + " "
            chars += len(word) + 1
    return wrapped_string


def check_textentry(textentry, prev_value, type):
    val = textentry.get_text()
    if val and type is float:
        if is_float(val):
            return float(val)
    elif val and type is int:
        if is_int(val):
            return int(val)
    print "error" # TODO dialog error
    textentry.set_text(str(prev_value))
    return prev_value


def base_path():
    # When executing from a frozen (pyinstaller) executable...
    if hasattr(sys, 'frozen'):
        print 'FROZEN!'
        return path(sys.executable).parent
    else:
        print 'NOT FROZEN!'

    # Otherwise...
    try:
        script = path(__file__)
    except NameError:
        script = path(sys.argv[0])
    return script.parent.parent


class InvalidVersionString(Exception):
    pass


class Version:
    InvalidVersionString(Exception)
    
    def __init__(self, major=0, minor=0, micro=0):
        if type(major)!=int or type(minor)!=int or type(micro)!=int:
            raise TypeError
        self.major = major
        self.minor = minor
        self.micro = micro

    @classmethod
    def fromstring(cls, string):
        "Initialize Version from a string"
        major, minor, micro = ('0','0','0')
        match = False
{'fraction_non_alphanumeric': 0.09137537993920973, 'fraction_numerical': 0.006838905775075988, 'mean_word_length': 2.8152173913043477, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7222678', 'n_tokens_mistral': 1569, 'n_tokens_neox': 1508, 'n_words': 551}