Advertisement
Bkmz

Untitled

Oct 6th, 2011
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.08 KB | None | 0 0
  1. #!/usr/bin/env python2
  2. # -*- coding: utf-8 -*-
  3.  
  4. from itertools import izip_longest
  5. import string
  6. from binascii import hexlify
  7. from math import log
  8. from random import randint
  9. import io
  10. import argparse
  11. import sys
  12.  
  13. # global variables
  14.  
  15. # TODO: create dirty hack to work with 3 capacity during specification
  16. cap         = 1
  17. capacity    = cap*2
  18. maxInt      = 2**(cap*8) - 1
  19. rewriteBin  = True
  20. bin         = "bin"
  21.  
  22. print "MaxINT: %s" % maxInt
  23. #maxInt = int(maxInt)
  24. cap = int(cap)
  25.  
  26. # amount of address cells
  27. N           = 4
  28. # amount of data cells
  29. D_count     = 8
  30.  
  31.  
  32. # end global variables
  33.  
  34. def convert(numb):
  35.     # getting HEX represent, in string, to use it when writing into binary file
  36.     hexNumb = '{0:x}'.format(numb)
  37.     out = ""
  38.    
  39.     # analyze input number, and compute
  40.     if not capacity == 0:
  41.         # checking power of two
  42.         if log(capacity, 2) % 1 == 0.0 or True:
  43.             # we have valid capacity of our integer.
  44.             # now check the max number to given capacity, else raise an Exception
  45.             if numb > maxInt:
  46.                 raise ValueError("Integer %s not filling in %s bytes capacity, Max INT: %s" %
  47.                                                             (numb, capacity, maxInt))
  48.            
  49.             # starting pushing to output
  50.             for i in _grouper(2, hexNumb.zfill(capacity), '0'):
  51.                 # performing modification of izip_longest to string
  52.                 i = string.join(i, "")
  53. #                print i
  54.                 out += chr(int(i, 16))
  55.                 # using statement below when i want a generator
  56.                 # yield chr(int(i, 16))
  57.         else:
  58.             raise ValueError("Capacity number must be a log of 2")
  59.        
  60.     return out
  61.  
  62. # return normalized data from convert function
  63. def unconvert(numb):
  64.     # using this hack to avoid bug with zero
  65.     h = hexlify(numb)
  66. #    print h
  67.     if not "0"*cap == h:
  68.         h = h.lstrip("0")
  69.        
  70.     return int(h, 16)
  71.  
  72. # grabbed from python recipes
  73. def _grouper(n, iterable, fillvalue=None):
  74.     "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
  75.     args = [iter(iterable)] * n
  76.     return izip_longest(fillvalue=fillvalue, *args)
  77.  
  78. # Code to provide working lab =)
  79.  
  80. def _writeDefaultData2Bin():
  81.     fd = open(bin, "wb")
  82.     # using it for code less
  83.     cv = convert
  84.  
  85.     fd.write(cv(N))
  86.     for i in xrange(1, N+1):
  87.         fd.write(cv(i))
  88.  
  89.  
  90.     for i in xrange(0, D_count):
  91.         rnd = randint(0, maxInt)
  92. #        print rnd
  93.         fd.write(cv(rnd))
  94.    
  95.     fd.close()
  96.    
  97.  
  98. def showBinContent():
  99.     fd = open(bin, "rb")
  100.    
  101.     # use it for less code
  102.     uc = unconvert
  103.     # getting amount of addresses
  104.     binN = fd.read(cap)
  105.     binN = unconvert(binN)
  106.    
  107.     # list for storing already read data
  108.     addresses = []
  109.     data = []
  110.    
  111.    
  112.     for i in xrange(0,N):
  113.         addresses.append(uc(fd.read(cap)))
  114.        
  115.        
  116.     for i in xrange(0, D_count):
  117.         data.append(uc(fd.read(cap)))
  118.        
  119.     print "Addresses:"
  120.     print addresses
  121.     print "Data:"
  122.     print data
  123.        
  124.     fd.close()
  125.    
  126.  
  127. def InteractWithUser():
  128. #    _writeDefaultData2Bin()
  129. #    showBinContent()
  130. #    _movingLoader()
  131.    
  132.     while True:
  133.         print "Bin file contains:"
  134.         showBinContent()
  135.         new_rnd = raw_input("Do you want to generate new random data?(y) Or write personally?(w)\n")
  136.         if not new_rnd == "y" and not  new_rnd == "w":
  137.             break
  138.         elif new_rnd == "y":
  139.             _writeDefaultData2Bin()
  140.         elif new_rnd == "w":
  141.             _userEditing()
  142.             break
  143.            
  144.            
  145.        
  146. def _movingLoader():
  147.     moveon = 2
  148.     fd = io.open(bin, "r+b")
  149.     uc = unconvert
  150.     cv = convert
  151.    
  152. #    fd.seek(cap+cap*N+cap*(1-1))
  153.    
  154. #    data = uc(fd.read(cap))
  155. #    print "Updating Data: %s" % data
  156. #    data += 10
  157. #    print "New Data: %s" % data
  158.    
  159.     # moving back
  160. #    fd.seek(-cap, 1)
  161.    
  162. #    fd.write(convert(data))
  163.  
  164.     adds = list()
  165.  
  166.     N = fd.read(cap)
  167.     N = uc(N)
  168.    
  169.     posAd   = cap
  170.     posData = cap+(cap*N)
  171.    
  172.     fd.seek(posAd)
  173.    
  174.     for i in xrange(1,N+1):
  175.         adds.append(uc(fd.read(cap)))
  176.    
  177.    
  178.     print adds
  179.    
  180.     fd.seek(posData)
  181.     n = 1
  182.     for a in adds:
  183.         vl = uc(fd.read(cap))
  184.         if n in adds:
  185.             fd.seek(-cap, 1)
  186.             fd.write(cv(vl+moveon))
  187.            
  188.         n+=1
  189.        
  190.  
  191.    
  192.     fd.close()
  193.    
  194.  
  195.  
  196.    
  197. if __name__ == "__main__":
  198. #    InteractWithUser()
  199. #    _userEditing()
  200. #    print unconvert(convert(1))
  201. #    _writeDefaultData2Bin()
  202. #    showBinContent()
  203.  
  204.     showBinContent()
  205.     print ""
  206.    
  207.     _movingLoader()
  208.     print ""
  209.     showBinContent()
  210.     exit(0)
  211.     if len(sys.argv) == 3:
  212.         if sys.argv[1] == "-m":
  213.             try:
  214.                 move = int(sys.argv[2])
  215.                 print move
  216.             except:
  217.                 print "Unexpected error"
  218.         else:
  219.             print "Error in args"
  220.                
  221.     else:
  222.         print "No such args"
  223.  
  224.    
  225.  
  226.    
  227.    
  228.    
  229.    
  230.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement