comparison bin/pkldu.py @ 1436:35b56d794d09

added option to order descending /ascending the fields acording to their size
author Razvan Pascanu <r.pascanu@gmail.com>
date Tue, 22 Feb 2011 11:40:02 -0500
parents 3dd64c115657
children 4b27456d3bce
comparison
equal deleted inserted replaced
1435:3dd64c115657 1436:35b56d794d09
86 t2 = time.time() 86 t2 = time.time()
87 prev_t = t2 - t1 87 prev_t = t2 - t1
88 print 'load time: %6.2f %s'%prettyprint(prev_t, time_units, 88 print 'load time: %6.2f %s'%prettyprint(prev_t, time_units,
89 options.human) 89 options.human)
90 90
91 print_entries = []
91 if isinstance(orig_obj, dict): 92 if isinstance(orig_obj, dict):
92 print 'Object is a dictionary' 93 print 'Object is a dictionary'
93 keys = [ key for key in orig_obj.keys() ] 94 keys = [ key for key in orig_obj.keys() ]
94 for key in keys: 95 for key in keys:
95 key_name = format_string(key, 40) 96 key_name = format_string(key, 40)
97 new_bytes = len(s) 98 new_bytes = len(s)
98 t1 = time.time() 99 t1 = time.time()
99 x = cPickle.loads(s) 100 x = cPickle.loads(s)
100 t2 = time.time() 101 t2 = time.time()
101 new_t = t2 - t1 102 new_t = t2 - t1
102 print 'field: %40s %6.2f %s ( loads in %6.2f %s)'%( 103 print_entry = 'field: %40s %6.2f %s ( loads in %6.2f %s)'%(
103 (key_name,) + 104 (key_name,) +
104 prettyprint(new_bytes, space_units, options.human) + 105 prettyprint(new_bytes, space_units, options.human) +
105 prettyprint(new_t, time_units, options.human) ) 106 prettyprint(new_t, time_units, options.human) )
107 if options.order is not 'none':
108 print 'Processed', key_name
109 print_entries += [(new_bytes, print_entry)]
110 else:
111 print print_entry
106 112
107 elif isinstance(orig_obj, (tuple, list)): 113 elif isinstance(orig_obj, (tuple, list)):
108 print 'Object is a list/tuple of ', len(orig_obj), 'elements' 114 print 'Object is a list/tuple of ', len(orig_obj), 'elements'
109 for idx, v in enumerate(orig_obj): 115 for idx, v in enumerate(orig_obj):
110 s = cPickle.dumps(v) 116 s = cPickle.dumps(v)
111 new_bytes = len(s) 117 new_bytes = len(s)
112 t1 = time.time() 118 t1 = time.time()
113 x = cPickle.loads(s) 119 x = cPickle.loads(s)
114 t2 = time.time() 120 t2 = time.time()
115 new_t = t2 - t1 121 new_t = t2 - t1
116 print 'entry: %03d \t\t\t\t %6.2f %s ( loads in %6.2f %s)' %( 122 print_entry = 'entry: %03d \t\t\t\t %6.2f %s ( loads in %6.2f %s)' %(
117 (idx,)+ 123 (idx,)+
118 prettyprint(new_bytes, space_units, options.human) + 124 prettyprint(new_bytes, space_units, options.human) +
119 prettyprint(new_t, time_units, options.human) ) 125 prettyprint(new_t, time_units, options.human) )
126
127 if options.order is not 'none':
128 print 'Processed entry number ', idx
129 print_entries += [(new_bytes, print_entry)]
130 else:
131 print print_entry
120 else: 132 else:
121 print 'Object is a '+str(type(orig_obj)) 133 print 'Object is a '+str(type(orig_obj))
122 for field in dir(orig_obj): 134 for field in dir(orig_obj):
123 field_name = format_string( field, 40) 135 field_name = format_string( field, 40)
124 if field.startswith('__') and not options.reserved: 136 if field.startswith('__') and not options.reserved:
129 new_bytes = len(s) 141 new_bytes = len(s)
130 t1 = time.time() 142 t1 = time.time()
131 x = cPickle.loads(s) 143 x = cPickle.loads(s)
132 t2 = time.time() 144 t2 = time.time()
133 new_t = t2 - t1 145 new_t = t2 - t1
134 print 'field: %40s %6.2f %s ( loads in %6.2f %s)' %( 146 print_entry ='field: %40s %6.2f %s ( loads in %6.2f %s)' %(
135 (field_name,)+ 147 (field_name,)+
136 prettyprint(new_bytes, space_units, options.human) + 148 prettyprint(new_bytes, space_units, options.human) +
137 prettyprint(new_t, time_units, options.human) ) 149 prettyprint(new_t, time_units, options.human) )
150
151 if options.order is not 'none':
152 print 'Processed field ', field_name
153 print_entries += [(new_bytes, print_entry)]
154 else:
155 print print_entry
138 except: 156 except:
139 print 'Could not pickle field', field_name 157 print 'Could not pickle field', field_name
158 if options.order in ('desc','asc'):
159 reverse = False
160 if options.order == 'desc':
161 reverse = True
162 print_entries = sorted(print_entries
163 , key = lambda x:x[0]
164 , reverse = reverse)
165 for entry in print_entries:
166 print entry[1]
167
140 168
141 def process_options(): 169 def process_options():
142 170
143 parser = optparse.OptionParser(usage) 171 parser = optparse.OptionParser(usage)
144 172
158 , default=False 186 , default=False
159 , help = (' If information about python reserved ' 187 , help = (' If information about python reserved '
160 ' fields (i.e. starting with `__`) ' 188 ' fields (i.e. starting with `__`) '
161 ' should be displayed' ) 189 ' should be displayed' )
162 ) 190 )
191
192
193 parser.add_option( '-o'
194 , "--order-fields"
195 , dest = 'order'
196 , default= 'none'
197 , help = (' Order fields acording the their size.'
198 ' Possible values are {none, desc, asc}')
199 )
200
163 return parser.parse_args() 201 return parser.parse_args()
164 202
165 203
166 if __name__ == '__main__': 204 if __name__ == '__main__':
167 (options,args) = process_options() 205 (options,args) = process_options()