mirror of
https://github.com/galaxyproject/galaxy.git
synced 2026-09-24 16:30:27 +08:00
Merge pull request #1351 from jmchilton/util_clean_0
Remove some unused stuff from galaxy.util.
This commit is contained in:
@@ -1,68 +0,0 @@
|
||||
|
||||
import inspect
|
||||
import time
|
||||
|
||||
import logging
|
||||
log = logging.getLogger( __name__ )
|
||||
|
||||
|
||||
def stack_trace_string( max_depth=None, line_format="{index}:{file}:{function}:{line}" ):
|
||||
"""
|
||||
Returns a string representation of the current stack.
|
||||
|
||||
:param depth: positive integer to control how many levels of the stack are
|
||||
returned. max_depth=None returns the entire stack (default).
|
||||
"""
|
||||
stack_list = []
|
||||
for index, caller in enumerate( inspect.stack() ):
|
||||
# don't include this function
|
||||
if index == 0:
|
||||
continue
|
||||
if max_depth and index > max_depth:
|
||||
break
|
||||
|
||||
caller_data = {
|
||||
'index' : str( index ),
|
||||
'file' : caller[1],
|
||||
'function' : caller[3],
|
||||
'line' : caller[2]
|
||||
}
|
||||
stack_list.append( line_format.format( **caller_data ) )
|
||||
|
||||
return '\n'.join( stack_list )
|
||||
|
||||
|
||||
class SimpleProfiler( object ):
|
||||
"""
|
||||
Simple profiler that captures the duration between calls to `report`
|
||||
and stores the results in a list.
|
||||
"""
|
||||
REPORT_FORMAT = '%20f: %s'
|
||||
|
||||
def __init__( self, log=None ):
|
||||
self.log = log
|
||||
self.start_time = 0
|
||||
self.reports = []
|
||||
|
||||
def start( self, msg=None ):
|
||||
msg = msg or 'Start'
|
||||
self.start_time = time.time()
|
||||
report = self.REPORT_FORMAT % ( self.start_time, msg )
|
||||
self.reports.append( report )
|
||||
if self.log:
|
||||
self.log( report )
|
||||
|
||||
def report( self, msg ):
|
||||
if not self.start_time:
|
||||
self.start()
|
||||
duration = time.time() - self.start_time
|
||||
report = self.REPORT_FORMAT % ( duration, msg )
|
||||
self.reports.append( report )
|
||||
if self.log:
|
||||
self.log( report )
|
||||
|
||||
def get_reports( self ):
|
||||
return self.reports
|
||||
|
||||
def __str__( self ):
|
||||
return '\n'.join( self.reports )
|
||||
@@ -5,22 +5,12 @@ introduced hashlib which replaced sha in Python 2.4 and previous versions.
|
||||
|
||||
import logging
|
||||
|
||||
# Use hashlib module if for Python 2.5+, fall back on old sha and md5 modules
|
||||
# ./lib/galaxy/web/framework/helpers/__init__.py is using the md5 module
|
||||
# sha1 requires explicit calls to new if also being passed to hmac (!)
|
||||
try:
|
||||
import hashlib
|
||||
sha1 = hashlib.sha1
|
||||
sha = sha1
|
||||
md5 = hashlib.md5
|
||||
except ImportError, e:
|
||||
from sha import new as sha1
|
||||
import sha
|
||||
from md5 import new as md5
|
||||
import hashlib
|
||||
sha1 = hashlib.sha1
|
||||
sha = sha1
|
||||
md5 = hashlib.md5
|
||||
import hmac
|
||||
|
||||
__all__ = ['md5', 'hashlib', 'sha1', 'sha', 'new_secure_hash', 'hmac_new', 'is_hashable']
|
||||
|
||||
log = logging.getLogger( __name__ )
|
||||
|
||||
|
||||
@@ -45,3 +35,5 @@ def is_hashable( value ):
|
||||
except:
|
||||
return False
|
||||
return True
|
||||
|
||||
__all__ = ['md5', 'hashlib', 'sha1', 'sha', 'new_secure_hash', 'hmac_new', 'is_hashable']
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
"""
|
||||
Kanwei Li, 03/2010
|
||||
|
||||
Simple LRU cache that uses a dictionary to store a specified number of objects
|
||||
at a time.
|
||||
"""
|
||||
|
||||
|
||||
class LRUCache:
|
||||
def clear(self):
|
||||
''' Clears/initiates storage variables'''
|
||||
self.key_ary = []
|
||||
self.obj_cache = {}
|
||||
|
||||
def __init__(self, num_elements):
|
||||
self.num_elements = num_elements
|
||||
self.clear()
|
||||
|
||||
def __getitem__(self, key):
|
||||
''' Return value of key, or None if key is not in cache '''
|
||||
if key not in self.key_ary:
|
||||
return None
|
||||
# Move this key to the end
|
||||
self.key_ary.remove(key)
|
||||
self.key_ary.append(key)
|
||||
return self.obj_cache[key]
|
||||
|
||||
def __setitem__(self, key, value):
|
||||
''' Sets a new value to a key '''
|
||||
if key not in self.obj_cache:
|
||||
if len(self.key_ary) >= self.num_elements:
|
||||
deleted_key = self.key_ary.pop(0) # Remove first element
|
||||
del self.obj_cache[deleted_key]
|
||||
self.key_ary.append(key)
|
||||
self.obj_cache[key] = value
|
||||
return value
|
||||
|
||||
if __name__ == "__main__":
|
||||
import unittest
|
||||
|
||||
class TestLRUCache(unittest.TestCase):
|
||||
def test_lru(self):
|
||||
lru = LRUCache(2)
|
||||
for i in range(0, 4): # Insert 4 numbers
|
||||
lru[i] = i
|
||||
self.assertEqual( lru[0], None )
|
||||
self.assertEqual( lru[1], None )
|
||||
self.assertEqual( lru[2], 2 )
|
||||
self.assertEqual( lru[3], 3 )
|
||||
|
||||
self.assertEqual( lru.__setitem__("hello", "world"), "world")
|
||||
self.assertEqual( lru[2], None )
|
||||
|
||||
lru.clear()
|
||||
self.assertEqual( lru["hello"], None )
|
||||
self.assertEqual( lru[3], None )
|
||||
|
||||
# Test if recently used item is kept
|
||||
lru[0] = 0
|
||||
lru[1] = 1
|
||||
# Now saturated
|
||||
lru[2] = 2
|
||||
# Should keep 0, delete 1
|
||||
self.assertEqual( lru[0], 0 )
|
||||
self.assertEqual( lru[1], None )
|
||||
self.assertEqual( lru[2], 2 )
|
||||
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user