Showing posts with label words. Show all posts
Showing posts with label words. Show all posts

26 June 2013

464. bytes2words -- python script

This script does something I could easily do myself in e.g. bc, and so is a bit of a waste of time. However, because I enjoy writing short python scripts, I did it anyway.

Usage:
/bytes2words -i 8gib -o MW
Input: 8gib Output: mw Assuming 64 bit word size Result: 8 gib is 1073 mw
bytes2words:
#!/usr/bin/python
# Converts bytes to words
# The impetus comes from the use of MW as the default memory unit in many computational pieces of software.

import sys

def split_text(s): # from http://stackoverflow.com/questions/12409894
    from itertools import groupby
    for k,g in groupby(s, str.isalpha):
        yield ''.join(list(g))

def getargs(arguments):
 switches={}

 version='0.1'
 
 try:
  if "-i" in arguments:
   switches['i']=arguments[arguments.index('-i')+1]
   print 'Input: %s'% switches['i']

   inputted=list(split_text(switches['i']))
   
   if len(inputted)>1:
    switches['i']=inputted[0]
    switches['unit']=inputted[len(inputted)-1].lower()
    
    if switches['unit'] == 'kib':
     switches['if']=1024
    elif switches['unit'] == 'kb':
     switches['if']=1000
    elif switches['unit'] == 'mb':
     switches['if']=1000*1000
    elif switches['unit'] == 'mib':
     switches['if']=1024*1024
    elif switches['unit'] == 'gb':
     switches['if']=1000*1000*1000
    elif switches['unit'] == 'gib':
     switches['if']=1024*1024*1024
   else:
    switches['if']=1
   
   if len(inputted)>2:
    print 'Illegal input: %s'% inputted
    arguments="--help" 
  else:
   arguments="--help";
 except:
  arguments="--help";
  
 try:
  if "-o" in arguments:
   switches['o']=arguments[arguments.index('-o')+1].lower()
   print 'Output: %s'% switches['o']
   if switches['o'] == 'w':
    switches['of']=1
   elif switches['o'] == 'kiw':
    switches['of']=1024
   elif switches['o'] == 'kw':
    switches['of']=1000
   elif switches['o'] == 'mw':
    switches['of']=1000*1000
   elif switches['o'] == 'miw':
    switches['of']=1024*1024
   elif switches['o'] == 'gw':
    switches['of']=1000*1000*1000
   elif switches['o'] == 'giw':
    switches['of']=1024*1024*1024
   else:
    print 'illegal output argument' % switches['o']
  else:
   arguments="--help";
 except:
  arguments="--help";
 version='0.1'

 try:
  if "-b" in arguments:
   switches['b']=int(arguments[arguments.index('-b')+1])
   print 'Word size: %i bits'% switches['b']
  else:
   print 'Assuming 64 bit word size'
   switches['b']=64;
 except:
  switches['b']=64;

 doexit=0
 try:
  if ("-h" in arguments) or ("--help" in arguments):
   print '\t\t bytes2words version %s' % version
   print ' \t-i\t input in words with units, e.g. 200kb'
   print ' \t-o\t output unit (w,kw,mw,gw)'
   print ' \t-b\t word size (32 or 64 (bit))'
   print 'Exiting'
   doexit=1
 except:
  a=0 # do nothing
 if doexit==1:
  sys.exit(0)
 return switches
 
if __name__=="__main__":
 bitsperbyte=8
 arguments=sys.argv[1:len(sys.argv)]
 switches=getargs(arguments)
 
 print 'Result: %s %s  is %s %s' % ( (switches['i']),switches['unit'],int(switches['if']*(float(switches['i'])/float(switches['b']/bitsperbyte))/float(switches['of'])),switches['o'])