def storage_table(self, shorten_len=40, action_dict={}): "Display Store in table format." if not self._storage.keys(): return None table = restTable(["ID"] + list(self._store_keys)) for sid in self._storage.keys(): row = [sid] _dict = self._storage[sid] for k in _dict.keys(): # get value v = _dict[k] # Apply action for (key, action) in action_dict.iteritems(): if k == key: Logger.debug('STORE: Performing action on {}'.format(k)) v = action(v) # Check on length if (len(str(v)) > shorten_len): row += ["{}*".format(v[:shorten_len])] else: row += [v] table.add_row(row) return table
def test_restTable(self): rst = restTable(['1', '2', '3']) rst.add_row(('a', 'c', 'e')) rst.add_row(('ab', 'cd', 'ef')) rst_g = """ == == == 1 2 3 == == == a c e ab cd ef == == ==""" r = str(rst).encode('string_escape') g = str(rst_g).encode('string_escape') self.assertEqual(r, g, "\nR:\n{}\nG:\n{}".format(r, g))
def display_table(self, show_indx=False): """ View Application environment constants. (useful for realtime debugging) Usage: env """ table = Cache.get(category='envcache', key='table') if not table: Logger.info("ENV: Generating environment table.") constants = self.list_constants(show_indx=show_indx) if constants: if show_indx: table = restTable(["S.NO", "CONSTANT", "VALUE"]) else: table = restTable(["CONSTANT", "VALUE"]) for c in constants: table.add_row(c) else: return "No environment variables defined." # Cache table for next run Logger.info("ENV: Caching table.") Cache.append(category='envcache', key='table', obj=table) else: Logger.info("ENV: Retrieving cached table.") return """ Environment Constants:\n To see value use: 'eko <constant name>'\n \n{} """.format(table)
def test_speed(self): pr = cProfile.Profile() rst = restTable(['Title1', 'Title2', 'Title3']) pr.enable() for _ in xrange(0, 100000): col1 = "".join(random.choice(string.ascii_uppercase + string.digits) for _ in xrange(random.randint(1, 20))) col2 = "".join(random.choice(string.ascii_uppercase + string.digits) for _ in xrange(random.randint(1, 20))) col3 = random.randint(1, 99999) rst.add_row([col1, col2, col3]) repr(rst) pr.disable() s= StringIO.StringIO() sortby = "cumulative" ps = pstats.Stats(pr, stream=s).sort_stats(sortby) ps.print_stats() print s.getvalue()