Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2
- # -*- coding: utf-8 -*-
- from itertools import izip_longest
- import string
- from binascii import hexlify
- from math import log
- from random import randint
- import io
- import argparse
- import sys
- # global variables
- # TODO: create dirty hack to work with 3 capacity during specification
- cap = 1
- capacity = cap*2
- maxInt = 2**(cap*8) - 1
- rewriteBin = True
- bin = "bin"
- print "MaxINT: %s" % maxInt
- #maxInt = int(maxInt)
- cap = int(cap)
- # amount of address cells
- N = 4
- # amount of data cells
- D_count = 8
- # end global variables
- def convert(numb):
- # getting HEX represent, in string, to use it when writing into binary file
- hexNumb = '{0:x}'.format(numb)
- out = ""
- # analyze input number, and compute
- if not capacity == 0:
- # checking power of two
- if log(capacity, 2) % 1 == 0.0 or True:
- # we have valid capacity of our integer.
- # now check the max number to given capacity, else raise an Exception
- if numb > maxInt:
- raise ValueError("Integer %s not filling in %s bytes capacity, Max INT: %s" %
- (numb, capacity, maxInt))
- # starting pushing to output
- for i in _grouper(2, hexNumb.zfill(capacity), '0'):
- # performing modification of izip_longest to string
- i = string.join(i, "")
- # print i
- out += chr(int(i, 16))
- # using statement below when i want a generator
- # yield chr(int(i, 16))
- else:
- raise ValueError("Capacity number must be a log of 2")
- return out
- # return normalized data from convert function
- def unconvert(numb):
- # using this hack to avoid bug with zero
- h = hexlify(numb)
- # print h
- if not "0"*cap == h:
- h = h.lstrip("0")
- return int(h, 16)
- # grabbed from python recipes
- def _grouper(n, iterable, fillvalue=None):
- "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
- args = [iter(iterable)] * n
- return izip_longest(fillvalue=fillvalue, *args)
- # Code to provide working lab =)
- def _writeDefaultData2Bin():
- fd = open(bin, "wb")
- # using it for code less
- cv = convert
- fd.write(cv(N))
- for i in xrange(1, N+1):
- fd.write(cv(i))
- for i in xrange(0, D_count):
- rnd = randint(0, maxInt)
- # print rnd
- fd.write(cv(rnd))
- fd.close()
- def showBinContent():
- fd = open(bin, "rb")
- # use it for less code
- uc = unconvert
- # getting amount of addresses
- binN = fd.read(cap)
- binN = unconvert(binN)
- # list for storing already read data
- addresses = []
- data = []
- for i in xrange(0,N):
- addresses.append(uc(fd.read(cap)))
- for i in xrange(0, D_count):
- data.append(uc(fd.read(cap)))
- print "Addresses:"
- print addresses
- print "Data:"
- print data
- fd.close()
- def InteractWithUser():
- # _writeDefaultData2Bin()
- # showBinContent()
- # _movingLoader()
- while True:
- print "Bin file contains:"
- showBinContent()
- new_rnd = raw_input("Do you want to generate new random data?(y) Or write personally?(w)\n")
- if not new_rnd == "y" and not new_rnd == "w":
- break
- elif new_rnd == "y":
- _writeDefaultData2Bin()
- elif new_rnd == "w":
- _userEditing()
- break
- def _movingLoader():
- moveon = 2
- fd = io.open(bin, "r+b")
- uc = unconvert
- cv = convert
- # fd.seek(cap+cap*N+cap*(1-1))
- # data = uc(fd.read(cap))
- # print "Updating Data: %s" % data
- # data += 10
- # print "New Data: %s" % data
- # moving back
- # fd.seek(-cap, 1)
- # fd.write(convert(data))
- adds = list()
- N = fd.read(cap)
- N = uc(N)
- posAd = cap
- posData = cap+(cap*N)
- fd.seek(posAd)
- for i in xrange(1,N+1):
- adds.append(uc(fd.read(cap)))
- print adds
- fd.seek(posData)
- n = 1
- for a in adds:
- vl = uc(fd.read(cap))
- if n in adds:
- fd.seek(-cap, 1)
- fd.write(cv(vl+moveon))
- n+=1
- fd.close()
- if __name__ == "__main__":
- # InteractWithUser()
- # _userEditing()
- # print unconvert(convert(1))
- # _writeDefaultData2Bin()
- # showBinContent()
- showBinContent()
- print ""
- _movingLoader()
- print ""
- showBinContent()
- exit(0)
- if len(sys.argv) == 3:
- if sys.argv[1] == "-m":
- try:
- move = int(sys.argv[2])
- print move
- except:
- print "Unexpected error"
- else:
- print "Error in args"
- else:
- print "No such args"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement