> "R. Alan Monroe" <amon...@columbus.rr.com> wrote
>> Neither of these seem like they'd scale very well (say, up to the
>> resolution of your screen, with one block per pixel). The end goal
>> is
>> just a basic do-nothing light show that simulates
>> fragmentation/defragmentation as eye candy.
> For that purpose the basic style you are using will be adequate.
> You are only talking about 2-5 million pixels at most on
> typical monitors. Usually more like 1.5 million on a laptop.
> And you are not too worried about speed, in fact too fast a
> display would just be wasted as most chanmges would
> never be seen!
Here's my first cut. One thing that surprised me is that none of the
earlier generation (single and double digit filenames) ever survive to
the 80th generation. My expectation was that I would end up with a
wider range of generations in the end.
Alan
class Disk(list):
def __init__(self, disksize):
self.size = disksize
self.free = disksize
self.files = {}
for i in range(self.size):
self.append(None)
def __str__(self):
s = int(math.sqrt(len(self)))
outstr = ""
for row in range(s):
for col in range(s):
if self[row*s + col]==None:
outstr += ".... "
else:
outstr += "%4d " % (self[row*s + col],)
outstr += "\n"
for i in self.files.items():
outstr += str(i)
outstr += '\n'
return outstr
def fwrite(self, filename, filelen):
if self.free >= filelen:
self.files[filename] = []
block = 0
for i in range(filelen):
while self[block] != None:
block += 1
self[block]=filename
self.files[filename].append(block)
self.free -= filelen
else:
raise IndexError
def fdel(self, filename):
for i in self.files[filename]:
self[i] = None
self.free += len(self.files[filename])
del self.files[filename]
import math
import random
#DISKSIZE = 64
#DISKSIZE = 100
#DISKSIZE = 144
#DISKSIZE = 256
#DISKSIZE = 1024
DISKSIZE = 2048
disk = Disk(DISKSIZE)
countup = 0
for i in range(80):
while disk.free / float(DISKSIZE) > 0.05: #fill er up
try:
countup += 1
attemptsize = int(random.paretovariate(1)) + 1
disk.fwrite(countup, attemptsize)
except IndexError:
print "full", countup, attemptsize
while disk.free / float(DISKSIZE) < 0.20: #trim er down
attemptdel = random.choice(disk.files.keys())
disk.fdel(attemptdel)
print disk, disk.free
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor