class TestBase(unittest.TestCase):
    def setUp(self):
        self.task_name = 'a task'
        self.other_task_name = 'another task'
        self.third_task_name = 'yet another task'
        self.tk = TimeKeeper()
        self._fakeTask(self.task_name)
        self._fakeTask(self.task_name)
        self._fakeTask(self.other_task_name)
        self._fakeTask(self.other_task_name)
        self._fakeTask(self.other_task_name)
        self._fakeTask(self.third_task_name)

    def _fakeTask(self, name):
        self.tk.start(name)
        j = 0
        for i in range(10000): j+=i%2
        self.tk.end(name)
示例#2
0
import chatterbot_func
import chatterbot
import random

from timekeeper import TimeKeeper

print "Say something to our chatterbot!"

bots = [
    chatterbot.Bot("Etta"),
    chatterbot_func.create_bot("Gertrude"),
]

keeper = TimeKeeper()
keeper.start()

while True:
    inp = raw_input()
    print chatterbot_func.chat_loud(random.choice(bots), inp)
    print "Time elapsed: %d seconds" % int(keeper.elapsed())
class Reshaper(object):
    '''
    Base Class for the PyReshaper general operations.  Specific operations
    should be defined as derived types.
    '''

    def __init__(self, specifier, serial=False, verbosity=1, once=False):
        '''
        Constructor

        @param specifier  An instance of the Specifier class, defining the
                          input specification for this reshaper operation.

        @param serial     True or False, indicating whether the operation
                          should be performed in serial (True) or parallel
                          (False).  The default is to assume parallel operation
                          (but serial will be chosen if the mpi4py cannot be
                          found when trying to initialize decomposition.

        @param verbosity  Level of printed output (stdout).  A value of 0 means
                          no output, and a higher value means more output.  The
                          default value is 1.

        @param once       True or False, indicating whether the Reshaper should
                          write all metadata to a 'once' file (separately).
        '''

        # Check types
        if (not isinstance(specifier, Specifier)):
            err_msg = "Input must be given in the form of a Specifier object"
            raise TypeError(err_msg)
        if (type(serial) is not bool):
            err_msg = "Serial indicator must be True or False."
            raise TypeError(err_msg)
        if (type(verbosity) is not int):
            err_msg = "Verbosity level must be an integer."
            raise TypeError(err_msg)
        if (type(once) is not bool):
            err_msg = "Once-file indicator must be True or False."
            raise TypeError(err_msg)

        ## Whether to write a once file
        self._use_once_file = once

        ## Internal timer data
        self._timer = TimeKeeper()

        ## Dictionary storing read/write data amounts
        self.assumed_block_size = float(4 * 1024 * 1024)
        self._byte_counts = {}

        self._timer.start('Initializing Messenger')
        ## Pointer to the messenger
        self._messenger = create_messenger(serial=serial)
        self._timer.stop('Initializing Messenger')

        # Set the verbosity in the messenger
        self._messenger.verbosity = verbosity

        # Debug output starting
        self._messenger.print_once('Initializing Reshaper', vlevel=1)

        ## Input specification
        self._specifier = specifier

        # Validate the user input data
        self._timer.start('Specifier Validation')
        self._specifier.validate()
        self._timer.stop('Specifier Validation')
        self._messenger.print_once('Specifier validated', vlevel=1)

    def convert(self, output_limit=0):
        '''
        Method to perform the Reshaper's designated operation.

        @param output_limit Limit on the number of output files to write
                            during the convert() operation.  If set to 0,
                            no limit is placed.  This limits the number of
                            output files produced by each processor in a
                            parallel run.
        '''
        pass

    def print_diagnostics(self):
        '''
        Print out timing and I/O information collected up to this point
        in the Reshaper's operation.
        '''

        # Get all totals and maxima
        my_times = self._timer.get_all_times()
        #print str(self._messenger.get_rank()) + ': all_times =', my_times
        max_times = self._messenger.max(my_times)
        #print str(self._messenger.get_rank()) + ': max_times =', max_times
        my_bytes = self._byte_counts
        #print str(self._messenger.get_rank()) + ': byte_counts =', my_bytes
        total_bytes = self._messenger.sum(my_bytes)
        #print str(self._messenger.get_rank()) + ': total_bytes =', total_bytes

        # Synchronize
        self._messenger.sync()

        # Print timing maxima
        o = self._timer.get_order()
        time_table_str = _pprint_dictionary('TIMING DATA', max_times, order=o)
        self._messenger.print_once(time_table_str, vlevel=0)

        # Convert byte count to MB
        for name in total_bytes:
            total_bytes[name] = total_bytes[name] / float(1024 * 1024)

        # Print byte count totals
        byte_count_str = _pprint_dictionary('BYTE COUNTS (MB)', total_bytes)
        self._messenger.print_once(byte_count_str, vlevel=0)
class Reshaper(object):
    '''
    Base Class for the PyReshaper general operations.  Specific operations
    should be defined as derived types.
    '''
    def __init__(self, specifier, serial=False, verbosity=1, once=False):
        '''
        Constructor

        @param specifier  An instance of the Specifier class, defining the
                          input specification for this reshaper operation.

        @param serial     True or False, indicating whether the operation
                          should be performed in serial (True) or parallel
                          (False).  The default is to assume parallel operation
                          (but serial will be chosen if the mpi4py cannot be
                          found when trying to initialize decomposition.

        @param verbosity  Level of printed output (stdout).  A value of 0 means
                          no output, and a higher value means more output.  The
                          default value is 1.

        @param once       True or False, indicating whether the Reshaper should
                          write all metadata to a 'once' file (separately).
        '''

        # Check types
        if (not isinstance(specifier, Specifier)):
            err_msg = "Input must be given in the form of a Specifier object"
            raise TypeError(err_msg)
        if (type(serial) is not bool):
            err_msg = "Serial indicator must be True or False."
            raise TypeError(err_msg)
        if (type(verbosity) is not int):
            err_msg = "Verbosity level must be an integer."
            raise TypeError(err_msg)
        if (type(once) is not bool):
            err_msg = "Once-file indicator must be True or False."
            raise TypeError(err_msg)

        ## Whether to write a once file
        self._use_once_file = once

        ## Internal timer data
        self._timer = TimeKeeper()

        ## Dictionary storing read/write data amounts
        self.assumed_block_size = float(4 * 1024 * 1024)
        self._byte_counts = {}

        self._timer.start('Initializing Messenger')
        ## Pointer to the messenger
        self._messenger = create_messenger(serial=serial)
        self._timer.stop('Initializing Messenger')

        # Set the verbosity in the messenger
        self._messenger.verbosity = verbosity

        # Debug output starting
        self._messenger.print_once('Initializing Reshaper', vlevel=1)

        ## Input specification
        self._specifier = specifier

        # Validate the user input data
        self._timer.start('Specifier Validation')
        self._specifier.validate()
        self._timer.stop('Specifier Validation')
        self._messenger.print_once('Specifier validated', vlevel=1)

    def convert(self, output_limit=0):
        '''
        Method to perform the Reshaper's designated operation.

        @param output_limit Limit on the number of output files to write
                            during the convert() operation.  If set to 0,
                            no limit is placed.  This limits the number of
                            output files produced by each processor in a
                            parallel run.
        '''
        pass

    def print_diagnostics(self):
        '''
        Print out timing and I/O information collected up to this point
        in the Reshaper's operation.
        '''

        # Get all totals and maxima
        my_times = self._timer.get_all_times()
        #print str(self._messenger.get_rank()) + ': all_times =', my_times
        max_times = self._messenger.max(my_times)
        #print str(self._messenger.get_rank()) + ': max_times =', max_times
        my_bytes = self._byte_counts
        #print str(self._messenger.get_rank()) + ': byte_counts =', my_bytes
        total_bytes = self._messenger.sum(my_bytes)
        #print str(self._messenger.get_rank()) + ': total_bytes =', total_bytes

        # Synchronize
        self._messenger.sync()

        # Print timing maxima
        o = self._timer.get_order()
        time_table_str = _pprint_dictionary('TIMING DATA', max_times, order=o)
        self._messenger.print_once(time_table_str, vlevel=0)

        # Convert byte count to MB
        for name in total_bytes:
            total_bytes[name] = total_bytes[name] / float(1024 * 1024)

        # Print byte count totals
        byte_count_str = _pprint_dictionary('BYTE COUNTS (MB)', total_bytes)
        self._messenger.print_once(byte_count_str, vlevel=0)