I am trying to go through a list of class objects in my program, and access functions belonging to the objects.
I am using this method :
# - - - Grid Updating
def update_grid(self):
for i in range(len(self.populationList)):
self.populationList[i].operate
To access this object type :
class Dotian():
goal = '#'
log = []
position = (0,0)
def __init__(self):
self.name = ops.randomLine('data/names')
def operate(self):
print 'here'
The list is populated like so :
# - - - Spawning Dotian
def spawn_dotian(self):
coord = ops.randomCoordinates(0, self.width-1)
if self.grid[coord[0]][coord[1]] == 0 :
spawnDot = Dotian
spawnDot.position = coord
self.populationList.append(spawnDot)
self.grid[coord[0]][coord[1]] = 1
else :
self.spawn_dotian()
The program should print here but it doesn't print anything.
Using python 2.7.3
Related
I am trying to add new objects to a class(emne) but the new instances of the class needs to be created using user input. So i need a way to be able to chose the name for the object and set some of the values of the objects with user input.
I have already tried to create a function that passes the value of the user input into a x = emner(x) to create it but it only returns:
AttributeError: 'str' object has no attribute 'fagKode'
so i think my issue is that the value of the input is created as a string so that it is not understood as a way to create the function
emne=[]
class Emne:
def __init__(self,fagKode):
self.fagKode = fagKode
self.karakter = ""
emne.append(self)
def leggTilEmne():
nyttEmne = input("test:")
nyttEmne=Emne(nyttEmne)
expected result is that the code creates a new instance of the class.
If by choosing a name you mean your fagKode attribute, what you need is:
fagKode = input('Enter code: ')
Emne(fagKode)
You're adding the instances of Enme to the list in the constructor, so you don't need to save them to a variable.
Alternatively, you can handle that in the function:
emne=[]
class Emne:
def __init__(self,fagKode):
self.fagKode = fagKode
self.karakter = ""
def leggTilEmne():
nyttEmne = input("test:")
enme.append(Emne(nyttEmne))
I'm not sure what exactly you are asking, since you haven't responded to the comments. So,
emne=[]
class Emne:
def __init__(self,fagKode):
self.fagKode = fagKode
self.karakter = ""
emne.append(self)
def leggTilEmne(self, value): # <--- is this what you want
self.nyttEmne= Emne(value)
This is an example of when to use a class method. __init__ should not be appending to a global variable, though. Either 1) have the class method append to a class attribute, or 2) have it return the object and let the caller maintain a global list.
emne = []
class Emne:
emne = []
def __init__(self, fag_kode):
self.fag_kode = fag_kode
self.karakter = ""
#classmethod
def legg_til_emne_1(cls):
nytt_emne = input("test:")
cls.emne.append(cls(nytt_emne))
#classmethod
def legg_til_emne_2(cls):
nyttEmne = input("test:")
return cls(nyttEmne)
Emne.legg_til_emne_1() # Add to Emne.emne
e = Emne.legg_til_emne_2()
emne.append(e)
Bellow is my current code, I am pretty new to Python. I am trying to create a list of Photo instances, where each Photo instance uses the data from each tuple in the tups_list. and save that list in a variable photo_insts. Currently I am not receiving an error, literally, nothing is happening in terminal when I try to run the file.
photo_insts = []
tups_list = [("Portrait 2","Gordon Parks",["chicago", "society"]),("Children in School","Dorothea Lange",["children","school","1930s"]),("Airplanes","Margaret Bourke-White",["war","sky","landscape"])]
class Photo2(object):
def __init__(self, title_str, photo_by,tags_list):
self.title = title_str
self.artist = photo_by
self.tags = tags_list
for i in tups_list:
photo_tuple = (i[0],i[1],i[2])
photo_insts.append(photo_tuple)
print i
Below are tests to run to check for diffrent values:
class Phototest(unittest.TestCase):
def test_photo_insts1(self):
self.assertEqual(type(photo_insts),type([]))
def test_photo_insts2(self):
self.assertEqual(type(photo_insts[0]),type(Photo("Photo2","Photo Student",["multiple","tags"])))
def test_photo_insts3(self):
self.assertEqual([x.title for x in photo_insts],["Portrait 2", "Children in School", "Airplanes"])
def test_photo_insts4(self):
self.assertEqual([x.artist for x in photo_insts],["Gordon Parks","Dorothea Lange","Margaret Bourke-White"])
def test_photo_insts5(self):
self.assertEqual([x.tags for x in photo_insts],[["chicago","society"],["children", "school","1930s"],["war","sky","landscape"]])
I guess this is a typo:
photo_tuple = (i[0],i[1],i[2])
=>
photo_tuple = Photo2 (i[0],i[1],i[2])
Function __init__ is called on creation of an instance Photo2.
If you call Photo2 () within the function __init__ then you get a recursion !
=> The code should look like :
class Photo2(object):
def __init__(self, title_str, photo_by, tags_list):
self.title = title_str
self.artist = photo_by
self.tags = tags_list
# end class
tups_list = [
("Portrait 2","Gordon Parks",["chicago", "society"])
,("Children in School","Dorothea Lange",["children","school","1930s"])
,("Airplanes","Margaret Bourke-White",["war","sky","landscape"])
]
photo_insts = []
for i in tups_list :
photo_tuple = Photo2 (i[0],i[1],i[2])
photo_insts.append(photo_tuple)
print i[0]
for p in photo_insts :
print repr (p)
Output in console :
Portrait 2
Children in School
Airplanes
<__main__.Photo2 object at 0xb7082cac>
<__main__.Photo2 object at 0xb7082ccc>
<__main__.Photo2 object at 0xb7082cec>
I'm developing an application that reads a message input from telegram with a set of variables, and then starts a game with the user. So I created a class that represents an instance of the game, making one game per chat possible:
class Battle:
def __init__(self, mainchat):
self.mainchat = mainchat
print('Instance of battle started on chat %s' % self.mainchat)
pcount = 0
team1 = []
team2 = []
p1 = ()
p2 = ()
p1score = 0
p2score = 0
battlechoicep1 = -1
battlechoicep2 = -1
so, as soon as I get a message, I start an instance of a battle based on user inputes, e.g.
battle = Battle(chat_id)
battle.p1 = 'Paul'
battle.battlechoicep1 = 4
...
this way has been working fine right now, but every time I want to reset the battle, I go through a function that does this:
battle.pcount = 0
battle.team1 = []
battle.team2 = []
battle.p1 = ()
battle.p2 = ()
battle.p1score = 0
battle.p2score = 0
battle.battlechoicep1 = -1
battle.battlechoicep2 = -1
save() # outside function that saves the scores into a pickle file
return
So, I would like to make it so this is a function inside my class, so everytime I call battle.reset it would call something like this
def reset():
battle.pcount = 0
battle.team1 = []
battle.team2 = []
battle.p1 = ()
battle.p2 = ()
battle.p1score = 0
battle.p2score = 0
battle.battlechoicep1 = -1
battle.battlechoicep2 = -1
save() # outside function that saves the scores into a pickle file
return
I don't know how is the right approach to this problem, I don't even know if what I've been doing up to now is 'correct' (it is working at least).
Creating the function inside the class (like def reset(self):) seems to have no effect.
You're on the right track with def reset(self). You just need to change the instances of battle to self in the method itself. NOTE: This needs to be a method of the Battle class.
def reset(self):
self.pcount = 0
... # etc
save() # outside function that saves the scores into a pickle file
When you pass in self as the first parameter of a class method, it allows the method to work on the instance of the class that you've called it on. If you just do def reset(self) without changing the battle to self, it will try to modify a variable in the current scope called battle, which in this case probably doesn't exist.
The other thing you could do if you just want reset to create a completely new object without preserving any of the attributes, you can just do:
def reset(self):
return Battle()
You're almost there!
class Battle:
def __init__(self, mainchat):
self.mainchat = mainchat
print('Instance of battle started on chat %s' % self.mainchat)
self.reset()
def reset(self):
self.team1, self.team2 = [], []
self.p1 = self.p2 = () #New tuples will be assigned and overwritten
self.pcount = self.p1score = self.p2score = 0
self.battlechoicep1 = self.battlechoicep2 = -1
save() # outside function that saves the scores into a pickle file
So when you need to reset, just call battle.reset()! Maybe the save function can also be a class method as well, just follow the same format.
I'm writing some code to create a toolbar that edits a map in ArcMap and I'm having some issues with getting variable values from other functions inside other classes that I'm using.
All the functions are predefined so I can't change the int arguments or the code will throw an error. I checked the dir() and none of the variables I define using self are in the functions. I don't think I've made a syntax error and the code inside the other classes works correctly.
Here is my code:
import arcpy
import math
import pythonaddins
class findingCoordinates(object):
"""Implementation for leetScripts_addin.tool (Tool)"""
def __init__(self):
self.enabled = True
self.shape = "NONE"
def onMouseDownMap(self, x, y, button, shift):
print "onMouseDowMap executing"
#this is where I declared the first two variables using self
self.x = x
self.y = y
print "Selected point is at %r, %r" % (self.x, self.y)
pass
class squareFeetInput(object):
"""Implementation for leetScripts_addin.combobox (ComboBox)"""
def __init__(self):
self.editable = True
self.enabled = True
#self.dropdownWidth = 'WWWWWW'
self.width = 'WWWWWW'
def onEditChange(self, text):
squareFeet = text
#this is the other variable I defined that I need to use later
self.buffDist = (math.sqrt(float(squareFeet))/2)
print "Square size: %r ft^2 Buffer Distance: %r ft^2" % (squareFeet,self.buffDist)
print "self.buffdist is a %r type" % self.buffDist
return self.buffDist
pass
class buildingTool(object):
"""Implementation for leetScripts_addin.button (Button)"""
def __init__(self):
self.enabled = True
self.checked = False
def onClick(self):
print "building tool is executing"
#shows im_self, but no x or y
print "%r" % dir(findingCoordinates.onMouseDownMap)
# Get arguments:
# Input point feature class
# Output polygon feature class
# Buffer distance
# Boolean type: Maintain fields and field values of the input in the output
#This is where the problem is. I can't get these values from the previous functions.
inPoints = (findingCoordinates.onMouseDownMap.x,findingCoordinates.onMouseDownMap.y)
outPolys = "U:\JackBuildingFootprints.gdb\BuildingFootprintsCopy"
bufDist = squareFeetInput.buffDist
keepFields = true
# Prepare the output based on whether field and field values are desired in the output
#
if keepFields:
# Create empty output polygon feature class that includes fields of the input
#
arcpy.CreateFeatureClass(os.path.dirname(outPolys), os.path.basename(outPolys), "POLYGON",
inPoints, "", "", inPoints)
# Create a short list of fields to ignore when moving fields values from
# input to output
#
ignoreFields = []
# Use Describe properties to identify the shapeFieldName and OIDFieldName
#
desc = arcpy.Describe(inPoints)
ignoreFields.append(desc.shapeFieldName)
ignoreFields.append(desc.OIDFieldName)
# Create a list of fields to use when moving field values from input to output
#
fields = arcpy.ListFields(inPoints)
fieldList = []
for field in fields:
if field.name not in ignoreFields:
fieldList.append(field.name)
else:
# Create empty output polygon feature class without fields of the input
#
arcpy.CreateFeatureclass(os.path.dirname(outPolys), os.path.basename(outPolys), "POLYGON",
"", "", "", inPoints)
# Open searchcursor
#
inRows = arcpy.SearchCursor(inPoints)
# Open insertcursor
#
outRows = arcpy.InsertCursor(outPolys)
# Create point and array objects
#
pntObj = arcpy.Point()
arrayObj = arcpy.Array()
for inRow in inRows: # One output feature for each input point feature
inShape = inRow.shape
pnt = inShape.getPart(0)
# Need 5 vertices for square buffer: upper right, upper left, lower left,
# lower right, upper right. Add and subtract distance from coordinates of
# input point as appropriate.
for vertex in [0,1,2,3,4]:
pntObj.ID = vertex
if vertex in [0,3,4]:
pntObj.X = pnt.X + bufDist
else:
pntObj.X = pnt.X - bufDist
if vertex in [0,1,5]:
pntObj.Y = pnt.Y + bufDist
else:
pntObj.Y = pnt.Y - bufDist
arrayObj.add(pntObj)
# Create new row for output feature
#
feat = outRows.newRow()
# Shift attributes from input to output
#
if keepFields == "true":
for fieldName in fieldList:
feat.setValue(fieldName, inRow.getValue(fieldName))
# Assign array of points to output feature
#
feat.shape = arrayObj
# Insert the feature
#
outRows.insertRow(feat)
# Clear array of points
#
arrayObj.removeAll()
# Delete inputcursor
#
del outRows
pass
What am I doing wrong? Is this one of the rare occasions where I should use a global variable? Why is the directory not showing the variables I defined using self?
Edit:
I made this post a while ago and I just wanted to clear some things up now that I know more.
First:
This is code that is designed to be use with python_add_in. Python add in creates a toolbar based on some parameters you give it when you set it up, and whatever python code you put into a template it makes as a result of those parameters. That essentially means that all of the classes in the script above are events that occur when buttons and other toolbar objects are clicked or used in the toolbar.
Second:
The solution to this problem actually isn't in the accepted answer, my bad.
The root cause of the problem is that I was using class names that I declared in my script, findingCoordinates for example. python_add_in doesn't recognize these class names as the names of the classes it expects to receive based on the template you fill out before you start coding.
With that in mind, the issue was that I was trying to call classes that just didn't exist as far as python_add_in was concerned. The solution is to just go ahead and use the class names python_add_in tool expects you to use. These names are in the docstring located below the class definition so where I have findingCoordinates I should have Tool.
I hope this helps.
self refers to an instance of the class that you've defined, so to access those values, you need to create an instance of the class, call the method, and then access the values from the instance.
For example:
In [9]: %paste
class findingCoordinates(object):
"""Implementation for leetScripts_addin.tool (Tool)"""
def __init__(self):
self.enabled = True
self.shape = "NONE"
def onMouseDownMap(self, x, y, button, shift):
print "onMouseDowMap executing"
#this is where I declared the first two variables using self
self.x = x
self.y = y
print "Selected point is at %r, %r" % (self.x, self.y)
pass
## -- End pasted text --
In [10]: f = findingCoordinates()
In [11]: f.onMouseDownMap(x=1, y=2, button="button", shift="shift")
onMouseDowMap executing
Selected point is at 1, 2
In [12]: f.x
Out[12]: 1
In [13]: f.y
Out[13]: 2
EDIT: It seems like you've had some confusion about scoping/namespaces as well. There's no x or y defined globally; they just exist within the class instances. That will also allow you to have separate x and y values for different instances of the class.
In [14]: x
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-14-401b30e3b8b5> in <module>()
----> 1 x
NameError: name 'x' is not defined
In [15]: y
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-15-009520053b00> in <module>()
----> 1 y
NameError: name 'y' is not defined
In [16]: g = findingCoordinates()
In [17]: g.onMouseDownMap(100,200,0,0)
onMouseDowMap executing
Selected point is at 100, 200
In [18]: f.x, f.y
Out[18]: (1, 2)
In [19]: g.x, g.y
Out[19]: (100, 200)
I am trying to get the variable - clipFileInfo in which it came from an import module. I run the following code:
from Library import libmaya
publishClip = libmaya.ClipPublish()
clip = publishClip.getClip()
print clip.clipFileInfo
But it will give me an error saying that # AttributeError: 'list' object has no attribute 'clipFileInfo' #
This is the portion of code that I am deriving from
class ClipPublish( lib.ClipPublish ):
...
...
def __getclipFileInfo( self ):
'''
Return list of dicts to pass through to writeClip function
'''
clipFileInfo = []
for rig in self.rigList.Rigs():
actor = rig.pop( 'actor', None )
if actor:
clipFileInfo = {}
clipFileInfo['actor'] = actor
clipFileInfo['rig'] = rig
clipFileInfo['name'] = self.__unit.get( rig['name'] )
clipFileInfo.append( clipFileInfo )
return clipFileInfo
def getClip( self ):
clipFileInfo = self.__getclipFileInfo()
if clipFileInfo:
start = self.frameRange.startFrame()
end = self.frameRange.endFrame()
clipFile = writeC.writeclip( clipFileInfo, start, end )
if clipFile == None:
return None
return clipFile[0] if self.isSingle() else clipFile
return []
Is this possible to do so in the first place?
It looks like you are trying to pull a local variable out of a function. Unless the function returns this local variable, it is not possible.
Instead, as the comment says, you should call publishClip.__getclipFileInfo() to get the value of that variable, since that function does return it.
To be more explicit, try the following code.
from Library import libmaya
publishClip = libmaya.ClipPublish()
info = publishClip.__getclipFileInfo()
print info