Esempio n. 1
0
def search_bench(word, text):

    iterations = Tools.trange(COUNT)
    print ('Searching for all occurences of %r using ...' % word)

    t0 = time.time()
    so = TextTools.TextSearch(word)
    for i in iterations:
        l = so.findall(text)
    t1 = time.time()
    count = len(l)

    print (' - mx.TextSearch.TextSearch().findall(): %5.3f ms (%i)' %
           ((t1 - t0) / COUNT * 1000.0, count))

    t0 = time.time()
    so = re.compile(word)
    for i in iterations:
        l = so.findall(text)
    t1 = time.time()
    count = len(l)
    
    print (' - re.compile().findall(): %5.3f ms (%i)' %
           ((t1 - t0) / COUNT * 1000.0, count))

    t0 = time.time()
    for i in iterations:
        count = text.count(word)
    t1 = time.time()
    
    print (' - text.count(): %5.3f ms (%i)' %
           ((t1 - t0) / COUNT * 1000.0, count))
Esempio n. 2
0
    def feed_objects(self, objects, getattr=getattr):
        """ Feeds a sequence of objects which is converted to CSV. 

            For each object the set column names are interpreted as
            object attributes and used as basis for the CSV data.

            None values are converted to empty strings, all other
            attributes are added stringified.

        """
        columns = self.columns
        if not columns:
            raise Error, 'no output columns set'
        rowlen = len(columns)

        # Create an emtpy table
        rows = len(objects)
        rowindices = Tools.trange(rows)
        t = [None] * rows
        for i in rowindices:
            t[i] = [None] * rowlen

        # Fill the table
        icols = Tools.irange(columns)
        for i in rowindices:
            obj = objects[i]
            for j, name in icols:
                t[i][j] = str(getattr(obj, name))

        # Quote and join lines
        t = [self.separator.join(self._quote(x)) for x in t]

        # Add final CRLF and store CSV text
        t.append('')
        self.text = self.text + self.lineend.join(t)
Esempio n. 3
0
def print_sequence(obj,
                   file=_sys.stdout,
                   indent='',
                   levels=2,
                   nonrecursive=()):

    l = []
    unfold = 0
    try:
        length = len(obj)
    except (AttributeError, ValueError, TypeError):
        return

    for i in Tools.trange(min(length, _VALUE_LEN_LIMIT)):
        try:
            value = obj[i]
        except:
            break
        try:
            r = repr(value)
        except:
            r = '*repr()-error*'

        # Truncate
        if len(r) > _VALUE_LEN_LIMIT:
            r = r[:_VALUE_LEN_LIMIT] + '...'

        # Write value
        l.append((value, r))

        # Only unfold sequences that have non-string items or string items
        # with more than on character
        if not is_string(value) or len(value) > 1:
            unfold = 1

    if len(obj) > _VALUE_LEN_LIMIT:
        l.append(('...', '...truncated...'))

    # Unfold value object
    if unfold:
        for i, (value, rvalue) in irange(l):
            file.write('%s%-15s = %s\n' % (indent, '[%i]' % i, rvalue))
            if levels > 1:
                print_recursive(value,
                                file,
                                indent + '  ',
                                levels - 1,
                                nonrecursive=nonrecursive)
Esempio n. 4
0
    def feed_dict(self,table,rows=None):

        """ Feeds a table (dict of lists) which is converted
            to CSV. 

            Only the keys set as column names are used to form the CSV
            data.

            All lists in the dictionary must have equal length or at
            least rows number of entries, if rows is given. None
            entries are converted to empty strings, all other objects
            are stringified.

        """
        columns = self.columns
        if not columns:
            raise Error,'no output columns set'
        rowlen = len(columns)

        # Create an emtpy table
        if not rows:
            rows = 0
            for column in columns:
                nrows = len(table[column])
                if nrows > rows:
                    rows = nrows
        rowindices = Tools.trange(rows)
        t = [None] * rows
        for i in rowindices:
            t[i] = [None] * rowlen
            
        # Fill the table
        for j,k in Tools.irange(columns):
            for i in rowindices:
                t[i][j] = table[k][i]
                
        # Quote and join lines
        t = [self.separator.join(self._quote(x)) for x in t]

        # Add final CRLF and store CSV text
        t.append('')
        self.text = self.text + self.lineend.join(t)
Esempio n. 5
0
    def feed_dict(self,table,rows=None):

        """ Feeds a table (dict of lists) which is converted
            to CSV. 

            Only the keys set as column names are used to form the CSV
            data.

            All lists in the dictionary must have equal length or at
            least rows number of entries, if rows is given. None
            entries are converted to empty strings, all other objects
            are stringified.

        """
        columns = self.columns
        if not columns:
            raise Error,'no output columns set'
        rowlen = len(columns)

        # Create an emtpy table
        if not rows:
            rows = 0
            for column in columns:
                nrows = len(table[column])
                if nrows > rows:
                    rows = nrows
        rowindices = Tools.trange(rows)
        t = [None] * rows
        for i in rowindices:
            t[i] = [None] * rowlen
            
        # Fill the table
        for j,k in Tools.irange(columns):
            for i in rowindices:
                t[i][j] = table[k][i]
                
        # Quote and join lines
        t = [self.separator.join(self._quote(x)) for x in t]

        # Add final CRLF and store CSV text
        t.append('')
        self.text = self.text + self.lineend.join(t)
Esempio n. 6
0
def print_sequence(obj,file=_sys.stdout,indent='',levels=2,
                   nonrecursive=()):

    l = []
    unfold = 0
    try:
        length = len(obj)
    except (AttributeError, ValueError, TypeError):
        return
    
    for i in Tools.trange(min(length,_VALUE_LEN_LIMIT)):
        try:
            value = obj[i]
        except:
            break
        try:
            r = repr(value)
        except:
            r = '*repr()-error*'

        # Truncate
        if len(r) > _VALUE_LEN_LIMIT:
            r = r[:_VALUE_LEN_LIMIT] + '...'

        # Write value
        l.append((value,r))

        # Only unfold sequences that have non-string items or string items
        # with more than on character
        if not is_string(value) or len(value) > 1:
            unfold = 1
            
    if len(obj) > _VALUE_LEN_LIMIT:
        l.append(('...','...truncated...'))

    # Unfold value object
    if unfold:
        for i,(value,rvalue) in irange(l):
            file.write('%s%-15s = %s\n' % (indent, '[%i]' % i, rvalue))
            if levels > 1:
                print_recursive(value,file,indent + '  ',levels-1,
                                nonrecursive=nonrecursive)
Esempio n. 7
0
    def feed_objects(self,objects,

                     getattr=getattr):

        """ Feeds a sequence of objects which is converted to CSV. 

            For each object the set column names are interpreted as
            object attributes and used as basis for the CSV data.

            None values are converted to empty strings, all other
            attributes are added stringified.

        """
        columns = self.columns
        if not columns:
            raise Error,'no output columns set'
        rowlen = len(columns)

        # Create an emtpy table
        rows = len(objects)
        rowindices = Tools.trange(rows)
        t = [None] * rows
        for i in rowindices:
            t[i] = [None] * rowlen
            
        # Fill the table
        icols = Tools.irange(columns)
        for i in rowindices:
            obj = objects[i]
            for j,name in icols:
                t[i][j] = str(getattr(obj, name))
                
        # Quote and join lines
        t = [self.separator.join(self._quote(x)) for x in t]

        # Add final CRLF and store CSV text
        t.append('')
        self.text = self.text + self.lineend.join(t)