changeset 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
files bin/pkldu.py
diffstat 1 files changed, 41 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/bin/pkldu.py	Tue Feb 22 11:23:32 2011 -0500
+++ b/bin/pkldu.py	Tue Feb 22 11:40:02 2011 -0500
@@ -88,6 +88,7 @@
     print 'load time: %6.2f %s'%prettyprint(prev_t, time_units,
                                             options.human)
 
+    print_entries = []
     if isinstance(orig_obj, dict):
         print 'Object is a dictionary'
         keys = [ key for key in orig_obj.keys() ]
@@ -99,10 +100,15 @@
             x = cPickle.loads(s)
             t2 = time.time()
             new_t = t2 - t1
-            print 'field: %40s %6.2f %s ( loads in %6.2f %s)'%(
+            print_entry = 'field: %40s %6.2f %s ( loads in %6.2f %s)'%(
                 (key_name,) +
                 prettyprint(new_bytes, space_units, options.human) +
                 prettyprint(new_t, time_units, options.human) )
+            if options.order is not 'none':
+                print 'Processed', key_name
+                print_entries += [(new_bytes, print_entry)]
+            else:
+                print print_entry
 
     elif isinstance(orig_obj, (tuple, list)):
         print 'Object is a list/tuple of ', len(orig_obj), 'elements'
@@ -113,10 +119,16 @@
             x = cPickle.loads(s)
             t2 = time.time()
             new_t = t2 - t1
-            print 'entry: %03d \t\t\t\t %6.2f %s ( loads in %6.2f %s)' %(
+            print_entry = 'entry: %03d \t\t\t\t %6.2f %s ( loads in %6.2f %s)' %(
                 (idx,)+
                 prettyprint(new_bytes, space_units, options.human) +
                 prettyprint(new_t, time_units, options.human) )
+
+            if options.order is not 'none':
+                print 'Processed entry number ', idx
+                print_entries += [(new_bytes, print_entry)]
+            else:
+                print print_entry
     else:
         print 'Object is a '+str(type(orig_obj))
         for field in dir(orig_obj):
@@ -131,12 +143,28 @@
                 x = cPickle.loads(s)
                 t2 = time.time()
                 new_t = t2 - t1
-                print 'field: %40s %6.2f %s ( loads in %6.2f %s)' %(
+                print_entry ='field: %40s %6.2f %s ( loads in %6.2f %s)' %(
                     (field_name,)+
                     prettyprint(new_bytes, space_units, options.human) +
                     prettyprint(new_t, time_units, options.human) )
+
+                if options.order is not 'none':
+                    print 'Processed field ', field_name
+                    print_entries += [(new_bytes, print_entry)]
+                else:
+                    print print_entry
             except:
                 print 'Could not pickle field', field_name
+    if options.order in ('desc','asc'):
+        reverse = False
+        if options.order == 'desc':
+            reverse = True
+        print_entries = sorted(print_entries
+                               , key = lambda x:x[0]
+                               , reverse = reverse)
+        for entry in print_entries:
+            print entry[1]
+
 
 def process_options():
 
@@ -160,6 +188,16 @@
                                   ' fields (i.e. starting with `__`) '
                                   ' should be displayed' )
                      )
+
+
+    parser.add_option( '-o'
+                     , "--order-fields"
+                     , dest    = 'order'
+                      , default= 'none'
+                     , help    = (' Order fields acording the their size.'
+                                  ' Possible values are {none, desc, asc}')
+                     )
+
     return parser.parse_args()