Advertisement
Bkmz

Untitled

Sep 30th, 2011
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 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.  
  11. # global variables
  12.  
  13. # TODO: create dirty hack to work with 3 capacity during specification
  14. capacity    = 4
  15. cap         = capacity/2
  16. maxInt      = 2**(cap*8) - 1
  17. rewriteBin  = True
  18. bin         = "bin"
  19.  
  20. # amount of address cells
  21. N           = 4
  22. # amount of data cells
  23. D_count     = 8
  24.  
  25.  
  26. # end global variables
  27.  
  28. def convert(numb):
  29.     # getting HEX represent, in string, to use it when writing into binary file
  30.     hexNumb = '{0:x}'.format(numb)
  31.     out = ""
  32.    
  33.     # analyze input number, and compute
  34.     if not capacity == 0:
  35.         # checking power of two
  36.         if log(capacity, 2) % 1 == 0.0:
  37.             # we have valid capacity of our integer.
  38.             # now check the max number to given capacity, else raise an Exception
  39.             if numb >= (2**(capacity*8)):
  40.                 raise ValueError("Integer %s not filling in %s bytes capacity, Max INT: %s" %
  41.                                                             (numb, capacity, maxInt))
  42.            
  43.             # starting pushing to output
  44.             for i in _grouper(cap, hexNumb.zfill(capacity), '0'):
  45.                 # performing modification of izip_longest to string
  46.                 i = string.join(i, "")
  47. #                print i
  48.                 out += chr(int(i, 16))
  49.                 # using statement below when i want a generator
  50.                 # yield chr(int(i, 16))
  51.     return out
  52.  
  53. # return normalized data from convert function
  54. def unconvert(numb):
  55.     # using thih hack to avoid bug with zero
  56.     h = hexlify(numb)
  57.     print h
  58.     if not "0"*cap == h:
  59.         h = h.lstrip("0")
  60.        
  61.     return int(h, 16)
  62.  
  63. # grabbed from python recipes
  64. def _grouper(n, iterable, fillvalue=None):
  65.     "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
  66.     args = [iter(iterable)] * n
  67.     return izip_longest(fillvalue=fillvalue, *args)
  68.  
  69. # Code to provide working lab =)
  70.  
  71. def _writeDefaultData2Bin():
  72.     fd = open(bin, "wb")
  73.     # using it for code less
  74.     cv = convert
  75.  
  76.     fd.write(cv(N))
  77.     for i in xrange(1, N+1):
  78.         fd.write(cv(i))
  79.  
  80.     for i in xrange(0, D_count):
  81.         rnd = randint(0, maxInt)
  82. #        print rnd
  83.         fd.write(cv(rnd))
  84.    
  85.     fd.close()
  86.    
  87.  
  88. def showBinContent():
  89.     fd = open(bin, "rb")
  90.    
  91.     # use it for less code
  92.     uc = unconvert
  93.     # getting amount of addresses
  94.     binN = fd.read(cap)
  95.     binN = unconvert(binN)
  96.    
  97.     # list for storing already read data
  98.     addresses = []
  99.     data = []
  100.    
  101.    
  102.     for i in xrange(0,N):
  103.         addresses.append(uc(fd.read(cap)))
  104.        
  105.        
  106.     for i in xrange(0, D_count):
  107.         data.append(uc(fd.read(cap)))
  108.        
  109.     print addresses
  110.     print data
  111.        
  112.     fd.close()
  113.    
  114.  
  115. def InteractWithUser():
  116.     _writeDefaultData2Bin()
  117.     showBinContent()
  118. #    _movingLoader()
  119.     showBinContent()
  120.    
  121. def _movingLoader():
  122.     fd = io.open(bin, "r+b")
  123.     uc = unconvert
  124.    
  125.     fd.seek(cap+cap*N+cap*(1-1))
  126.    
  127.     data = uc(fd.read(cap))
  128.     print "Updating Data: %s" % data
  129.     data += 10
  130.     print "New Data: %s" % data
  131.    
  132.     # moving back
  133.     fd.seek(-cap, 1)
  134.    
  135.     fd.write(convert(data))
  136.    
  137.     fd.close()
  138.    
  139.  
  140.  
  141.    
  142. if __name__ == "__main__":
  143.     InteractWithUser()
  144. #    print unconvert(convert(0))
  145.    
  146.  
  147.    
  148.    
  149.    
  150.    
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement