I am new to python. How to instantiate object from parsing a file?
I got a error.
File "/home/Bunny.py", line 23, in main
mapgrid[i].append(Spot(myLine[j] == 'B'))
TypeError: 'module' object is not callable
The first line of input is the number of simulations.
The next line isthe number of minutes for
a single reproductive cycle.The next line is the number of rows(x), followed by a single space, and followed by number of columns (y).
The next group of y lines will have x number of characters, with a single period ('.') representing a blank space and a single capitol “A” representing a starting Bunny.
Thanks for your help.
Input.txt
2 # 2 simulations
5 # 5 minutes/cycle
3 3 # 3*3 map
...
.B.
...
1
3 3
B.B
...
B.B
My code
Spot.py
#!/usr/bin/env python
class Spot(object):
isBunny = bool()
nextCycle = 0
UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3
SLEEP = 4
def __init__(self, newIsBunny):
self.isBunny = newIsBunny
self.nextCycle = self.UP
def setNextCycle(self):
if (self.nextCycle != self.SLEEP):
self.nextCycle += 1
def getNextCycle(self):
return self.nextCycle
def getIsBunny(self):
return self.isBunny
def makeBunny(self):
if not self.isBunny:
self.nextCycle = self.UP
self.isBunny = True
Bunny.py
#!/usr/bin/env python
import Spot
import os
class Bunny(object):
@classmethod
def main(cls, args):
with open(os.path.expanduser('~/Desktop/input.txt')) as f:
numSims = int(f.readline())
mapgrid = []
print numSims
minPerCycle= int(f.readline())
print minPerCycle
for k in xrange(numSims):
xyLine= f.readline()
row = int(xyLine.split()[0])
col = int(xyLine.split()[1])
print row,col
for i in xrange(row):
myLine = f.readline()
mapgrid.append([])
for j in xrange(col):
mapgrid[i].append(Spot(myLine[j] == 'B'))
# store this map somewhere?
cls.myMaps.append(mapgrid)
numCycles = 1
if cls.isFilled():
numCycles = 0
while not cls.isFilled():
numCycles += 1
## for-while
j = 0
while j < cls.myMap.length:
## for-while
k = 0
while k < cls.myMap[j].length:
if cls.myMap[j][k].getIsBunny():
if cls.myMap[j][k].getNextCycle() == 0:
cls.myMap[j - 1][k].makeBunny()
break
elif cls.myMap[j][k].getNextCycle() == 1:
cls.myMap[j][k + 1].makeBunny()
break
elif cls.myMap[j][k].getNextCycle() == 2:
cls.myMap[j + 1][k].makeBunny()
break
elif cls.myMap[j][k].getNextCycle() == 3:
cls.myMap[j][k - 1].makeBunny()
cls.myMap[j][k].setNextCycle()
k += 1
j += 1
time = numCycles * minPerCycle
print "It took " + time + " minutes for the bunnies to take over the world!\n"
i += 1
f.close()
@classmethod
def isFilled(cls):
cls.isFilled = True
## for-while
i = 0
while i < cls.myMap.length:
## for-while
j = 0
while j < cls.myMap[i].length:
if not cls.myMap[i][j].getIsBunny():
cls.isFilled = False
j += 1
i += 1
return cls.isFilled
if __name__ == '__main__':
import sys
Bunny.main(sys.argv)