<commit_before>#!/usr/bin/env python
'''
Cross-platform getch class.
References:
    http://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user
    http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/
Note:
    Might be able to better use https://pypi.python.org/pypi/getch
    as an alternative.
    Unusure of  Windows Support
    (module recommends msvcrt on Windows)
'''


def is_data(timeout=1):
    '''
    Checks to see if there is data ready to read on stdin.
    '''
    import sys
    import select

    rdy = select.select([sys.stdin], [], [], timeout) == ([sys.stdin], [], [])
    return rdy


class _Getch(object):
    '''
    Gets a single character from standard input.  Does not echo to the
    screen.
    '''
    def __init__(self, timeout=1):
        '''
        Figure out which class to actually be for _Getch.
        '''
        self.timeout = timeout
        try:
            self.impl = _GetchWindows(self.timeout)
        except ImportError:
            self.impl = _GetchUnix(self.timeout)

    def __call__(self):
        '''
        Grab from the imports
        '''
        return self.impl()


class _GetchUnix(object):
    '''
    Unix version of _Getch
    '''
    def __init__(self, timeout):
        '''
        Imports to make sure we can run
        '''
        import tty
        import sys
        import termios

        self.timeout = timeout

    def __call__(self):
        '''
        Get our character
        '''
        # select to prevent blocking
        import sys
        import tty
        import termios

        # Don't barf if we are not a tty, Just return nothing
        # because we won't expect a character anyhow
        if not sys.stdout.isatty():
            return None
        fd = sys.stdin
        old_settings = termios.tcgetattr(fd)
        ch = None
        try:
            tty.setcbreak(sys.stdin.fileno())
            if is_data(self.timeout):
                ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch


class _GetchWindows(object):
    '''
    Windows Version of _Getch
    Just pass msvcrt.getch() back
    '''
    def __init__(self, timeout):
        '''
        Import msvcrt to make sure we can load this class
        '''
        import msvcrt
<commit_msg>make sure timeout behaves the same even without tty to lower cpu usage<commit_after>#!/usr/bin/env python
'''
Cross-platform getch class.
References:
    http://stackoverflow.com/questions/510357/python-read-a-single-character-from-the-user
    http://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/
Note:
    Might be able to better use https://pypi.python.org/pypi/getch
    as an alternative.
    Unusure of  Windows Support
    (module recommends msvcrt on Windows)
'''


def is_data(timeout=1):
    '''
    Checks to see if there is data ready to read on stdin.
    '''
    import sys
    import select

    rdy = select.select([sys.stdin], [], [], timeout) == ([sys.stdin], [], [])
    return rdy


class _Getch(object):
    '''
    Gets a single character from standard input.  Does not echo to the
    screen.
    '''
    def __init__(self, timeout=1):
        '''
        Figure out which class to actually be for _Getch.
        '''
        self.timeout = timeout
        try:
            self.impl = _GetchWindows(self.timeout)
        except ImportError:
            self.impl = _GetchUnix(self.timeout)

    def __call__(self):
        '''
        Grab from the imports
        '''
        return self.impl()


class _GetchUnix(object):
    '''
    Unix version of _Getch
    '''
    def __init__(self, timeout):
        '''
        Imports to make sure we can run
        '''
        import tty
        import sys
        import termios

        self.timeout = timeout

    def __call__(self):
        '''
        Get our character
        '''
        # select to prevent blocking
        import sys
        import tty
        import termios
        import time

        # Don't barf if we are not a tty, Just return nothing
        # because we won't expect a character anyhow
        if not sys.stdout.isatty():
            time.sleep(timeout)
            return None
        fd = sys.stdin
        old_settings = termios.tcgetattr(fd)
        ch = None
        try:
            tty.setcbreak(sys.stdin.fileno())
            if is_data(self.timeout):
                ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
        return ch


class _GetchWindows(object):
    '''
    Windows Version of _Getch
    Just pass msvcrt.getch() back
    '''
    def __init__(self, timeout):
        '''
        Import msvcrt to make sure we can load this class
        '''
        import msvcrt

{'fraction_non_alphanumeric': 0.09845201238390093, 'fraction_numerical': 0.0037151702786377707, 'mean_word_length': 1.8965929468021518, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '2844971', 'n_tokens_mistral': 1471, 'n_tokens_neox': 1423, 'n_words': 417}