w_rook_1 = ChessPiece('w_rook_1')
w_knight_1 = ChessPiece('w_knight_1')
w_bishop_1 = ChessPiece('w_bishop_1')
w_king = ChessPiece('w_king')
w_queen = ChessPiece('w_queen')
w_bishop_2 = ChessPiece('w_bishop_2')
w_knight_2 = ChessPiece('w_knight_2')
w_rook_2 = ChessPiece('w_rook_2')
w_pawn_1 = ChessPiece('w_pawn_1')
w_pawn_2 = ChessPiece('w_pawn_2')
w_pawn_3 = ChessPiece('w_pawn_3')
w_pawn_4 = ChessPiece('w_pawn_4')
w_pawn_5 = ChessPiece('w_pawn_5')
w_pawn_6 = ChessPiece('w_pawn_6')
w_pawn_7 = ChessPiece('w_pawn_7')
w_pawn_8 = ChessPiece('w_pawn_8')
Is there an easier way to do this? I would also like to be able to use the objects afterwards.
Here is a simple approach using a dictionary when dealing with this type of challenge.
I added some comments within the code, please read.
instance_names = ['w_rook_1',
'w_knight_1',
'w_bishop_1',
'w_king',
'w_queen',
'w_bishop_2',
'w_knight_2',
'w_knight_2']
class ChessPiece(object):
def __init__(self, name):
self.name = name
self.move = "moving {}".format(name)
chess_objs = {}
for obj in instance_names:
# insert instance names ex. 'w_rook_1' as the key
# the ChessPiece instance is set as the value
chess_objs.setdefault(obj, ChessPiece(obj))
# here just illustrates how to access fields
# bound to each object
print(chess_objs['w_bishop_1'].name)
print(chess_objs['w_bishop_1'].move)
outputs:
w_bishop_1
moving w_bishop_1
If you follow #kaya3's advice and redesign your ChessPiece class, you could use a list comprehension easily, something like this (using abbreviations and ignoring number):
color = 'W'
non_pawns = [ChessPiece(color, c) for c in 'RNBKQBNR']
pawns = [ChessPiece(color, 'P') for _ in range(8)]
Related
I have created a script that stores and edits meta-data in a system. I am now cleaning up my code by defining a class and methods, previously I only used separate functions.
In the script I am storing old and new values of certain types of metadata in lists, which I print out after the script has completed its run. I have defined multiple lists (16 to be exact), which I realized is quite a lot when passing them through a method. I was wondering what is the most pythonic way to approach this.
These are the following list variables that i define in the beginning. In the function/method I append values to them. In the end I print the stored valued out as a report.
split_name = []
split_name_new = []
name = []
name_new = []
meta = []
meta_new = []
series = []
series_new = []
product = []
owner = []
desc = []
desc_new = []
keywords = []
keywords_new = []
no_edit_page =[]
no_edit_page_name = []
In a class i figured it will look something like (if I define all the list separately)
class Metadata_editor():
def __init__(self,url):
self.split_name = []
self.split_name_new = []
self.name = []
self.name_new = []
self.meta = []
self.meta_new = []
self.series = []
self.series_new = []
self.product = []
self.owner = []
self.desc = []
self.desc_new = []
self.keywords = []
self.keywords_new = []
self.no_edit_page =[]
self.no_edit_page_name = []
#Ugly solution because the method gets crowded by all the variables passed through
def data_edit(self, split_name, split_name_new, name, name_new,.. etc):
#Not the whole method, but just to give some idea..
#Selenium function that locates meta
md = driver.find_element_by_xpath("//input[#name='metadata-name']")
meta_data = md.get_attribute("value")
#replace_words translate the word using a dictionary object
meta_data_new = replace_words(meta_data,c)
meta.append(meta_data)
meta_new.append(meta_data_new)
The solution above I realized would not be ideal.
I found an alternative way that I could use, which is I define a list of lists. The solution would then look something like this (see below). However 'data_list[10]' is not as self-explanatory as for say 'owner'. My question is, is this the 'best' way to solve this, or do you have any other suggestions? I don't really have anything against this solution, but was wondering if there is a more 'pythonic' way to approach this.
class Metadata_editor():
def __init__(self,url):
self.data_list=[[] for _ in range(16)] #Creates a list, that contains 16 lists
# More eloquent solution, as only one variable is passed through. However finding
# the right data from data_list is perhaps not as easy before
def data_edit(self, data_list):
md = driver.find_element_by_xpath("//input[#name='metadata-name']")
meta_data = md.get_attribute("value")
meta_data_new = replace_words(meta_data,c)
data_list[5].append(meta_data)
data_list[6].append(meta_data_new)
You could store it as a dictionary. That would have the advantage of being able to reference the keys by name rather than having to remember the indexes.
class Metadata_editor():
def __init__(self, url):
keys = [
'split_name', 'split_name_new', 'name', 'name_new' 'meta', 'meta_new',
'series', 'series_new', 'product', 'owner', 'desc', 'desc_new',
'keywords', 'keywords_new', 'no_edit_page', 'no_edit_page_name',
]
self.data_dict = dict((x, []) for x in keys)
def data_edit(self):
md = driver.find_element_by_xpath("//input[#name='metadata-name']")
meta_data = md.get_attribute("value")
meta_data_new = replace_words(meta_data,c)
self.data_dict['meta'].append(meta_data)
self.data_dict['meta_new'].append(meta_data_new)
A few extra points to note:
class names generally follow the UpperCaseCamelCase convention. So Metadata_editor would more conventionally be written as MetadataEditor
Using self sets an attribute on the class, it can be accessed in the class using self and the attribute does not need to be passed into the method. I have shown this in the example above, accessing self.data_dict in the data_edit method.
You can also use setattr to set attributes to the class as shown in some of the other answers.
You can initialize multiple lists as below:
class Metadata_editor():
def __init__(self,list_names):
[setattr(self,name,[]) for name in list_names]
me = Metadata_editor(['split_name','split_name_new']) # initialize two lists
me.split_name.append(5) # add value to a list
print(me.split_name, me.split_name_new)
>>[5], [ ]
Once set as part of the class via self.list_name, the list(s) can be accessed globally within the class - no longer requiring to be 'passed in'. To initialize lists to specific values, you can do:
def __init__(self,list_names,list_values):
[setattr(self,name,value) for name,value in zip(list_names,list_values)]
Use setattr:
...
def __init__(self, url):
names = '''split_name split_name_new name
name_new meta meta_new series series_new
product owner desc desc_new keywords
keywords_new no_edit_page
no_edit_page_name'''.split()
for name in names:
setattr(self, name, [])
...
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)
Very new to Python and could do with some help. How do I go about referencing members in a class?
I have two csv files. One contains a series of parts and associated material ID. The other is a material index that contains materials ID's and some information about that material.
My intention is to create a third file that contains all of the parts, their material Id's and the information if present in the material index.
I have created a class for the material index and am trying to access objects in this class using material Ids from the part file however, this is not working and I am unsure as to why. Any help is appreciated:
class material():
def __init__(self, name, ftu, e, nu):
self.name = name
self.ftu = ftu
self.e = e
self.nu = nu
def extract_FTU_Strain(input_file_parts,input_file_FTU,output_file):
parts = {}
materials = {}
for aline in open(input_file_FTU, 'r'):
comma_split = aline.strip().split(',')
name = comma_split[1]
ftu = comma_split[8]
e = comma_split[9]
nu = comma_split[7]
try:
materials[int(comma_split[0])] = material(comma_split[1],comma_split[8],comma_split[9],comma_split[7])
#materials[comma_split[0]] = material(comma_split[1],comma_split[8],comma_split[9],comma_split[7])
except:
pass
for i in open(input_file_parts, 'r'):
semicolon_split = i.strip().split(';')
material_id = semicolon_split[3]
part = semicolon_split[0]
part_id = semicolon_split[1]
material_name = materials[material_id].name
FTU = materials[material_id].ftu
Stress = materials[material_id].e
output.write(','.join([part,part_id,material_name,material_id,FTU,Stress]) + '\n')
output = open (output_file,'w')
output.write('Part Title, Part Id, Material Id, FTU, e' + '\n')
output.close()
import sys
input_file_parts = '/parttable.csv'
input_file_FTU = '/Material_Index.csv'
output_file = '/PYTHONTESTING123.csv'
extract_FTU_Strain(input_file_parts,input_file_FTU,output_file)
Since in the comments you said your error is in materials[material_id] make material_id an integer as it was an integer when you created the object.
You created it this way
materials[int(comma_split[0])]=...
But later called it without converting material_id to an int. Do this before calling it in your for loop to write in the output.
material_id = int(material_id)
I may have misinterpreted your question, but going off the line 'How do I go about referencing members in a class?' you can reference member variables like so:
class Demonstration:
def __init__(self, a, b):
self.a = a
self.b = b
def printMembers(self):
print self.a, self.b
So inside the class you can use self.someVariable to reference member variables.
If you want to access them outside of the class:
myclass.myvariable
I'll happily edit the answer if I have't quite understood your question or if there is a specific error you are getting.
I did not understand what error you have, could you put the traceback? Anyway, you are creating a class instance at the time of assignment. For more elegant programming, you could simply do:
m = materials(name, ftu, e, nu)
This way you can access the instance variables like this:
m.name
m.ftu
...
And try, except -> pass it's very dangerous
I really hope this is not a repeat...if it is, I can't find an answer anywhere, so I apologize.
Anyways, my question is, I'm writing code where, if the data I get requires a team instead of a player, I have a class (called Team) that holds two SinglePlayers (also a class), and then a few other attributes, just strings. The problem is, when I iterate through my loop, reading the xml data and filling up my "team" variable, it seems that all the info for the SinglePlayers doesn't get reset. This is a problem, because it is is changing that info every time I insert a new "team" into the list of "team" objects I have. The code is long, so I'm only going to post what's relevant.
I have only been working with python again for a few days. I have been working the past year in java and c++ so my brain has those concepts in my head of how variables and structures work. I know python is different, so if someone could please clarify why this doesn't work, that would be amazing. Thanks!
class SinglePlayer:
entry_code = ""
first_name = ""
last_name = ""
nation = ""
seed_rank_sgl = ""
seed_rank_dbl = ""
entry_rank_sgl = ""
entry_rank_dbl = ""
class Team:
top_player = SinglePlayer()
bottom_player = SinglePlayer()
entry_code = ""
seed_rank = ""
entry_rank = ""
def DoublesEvent(self, team_nodes):
#Create List to store team objects
teams_list = []
for k in range(0, team_nodes.length):
#Get the current node
teams_node = team_nodes.item(k)
team_node = team_nodes.item(k).getElementsByTagName("Player")
top_player_node = team_node.item(0)
bottom_player_node = team_node.item(1)
#Make a new team object to fill and add to teams_list
team = Team()
team.entry_code = teams_node.getAttribute("EntryCode")
#Top Player Info
team.top_player.first_name = top_player_node.getAttribute("FirstName")
team.top_player.last_name = top_player_node.getAttribute("LastName")
team.top_player.nation = top_player_node.getAttribute("Nation")
#Bottom Player Info
team.bottom_player.first_name = bottom_player_node.getAttribute("FirstName")
team.bottom_player.last_name = bottom_player_node.getAttribute("LastName")
team.bottom_player.nation = bottom_player_node.getAttribute("Nation")
eam.seed_rank = self.GetSeedRank(team)
team.entry_rank = self.GetEntryRank(team)
#Add the team to the list
teams_list.append(team)
return teams_list
Your class holds a reference to two SinglePlayer() instances, not your instances. Use a __init__ method to create new instances for every Team instance:
class Team:
entry_code = ""
seed_rank = ""
entry_rank = ""
def __init__(self):
self.top_player = SinglePlayer()
self.bottom_player = SinglePlayer()
As it happens, because you rebind each of the string attributes on the instances you create, you happen to create instance attributes for those. You'd be better off moving those into __init__ as well and making their relationship as instance attributes explicit:
class Team:
def __init__(self):
self.entry_code = ""
self.seed_rank = ""
self.entry_rank = ""
self.top_player = SinglePlayer()
self.bottom_player = SinglePlayer()
and do the same for your SinglePlayer class.
I've got a piece of code which contains a for loop to draw things from an XML file;
for evoNode in node.getElementsByTagName('evolution'):
evoName = getText(evoNode.getElementsByTagName( "type")[0].childNodes)
evoId = getText(evoNode.getElementsByTagName( "typeid")[0].childNodes)
evoLevel = getText(evoNode.getElementsByTagName( "level")[0].childNodes)
evoCost = getText(evoNode.getElementsByTagName("costperlevel")[0].childNodes)
evolutions.append("%s x %s" % (evoLevel, evoName))
Currently it outputs into a list called evolutions as it says in the last line of that code, for this and several other for functions with very similar functionality I need it to output into a class instead.
class evolutions:
def __init__(self, evoName, evoId, evoLevel, evoCost)
self.evoName = evoName
self.evoId = evoId
self.evoLevel = evoLevel
self.evoCost = evoCost
How to create a series of instances of this class, each of which is a response from that for function? Or what is a core practical solution? This one doesn't really need the class but one of the others really does.
A list comprehension might be a little cleaner. I'd also move the parsing logic to the constructor to clean up the implemenation:
class Evolution:
def __init__(self, node):
self.node = node
self.type = property("type")
self.typeid = property("typeid")
self.level = property("level")
self.costperlevel = property("costperlevel")
def property(self, prop):
return getText(self.node.getElementsByTagName(prop)[0].childNodes)
evolutionList = [Evolution(evoNode) for evoNode in node.getElementsByTagName('evolution')]
Alternatively, you could use map:
evolutionList = map(Evolution, node.getElementsByTagName('evolution'))
for evoNode in node.getElementsByTagName('evolution'):
evoName = getText(evoNode.getElementsByTagName("type")[0].childNodes)
evoId = getText(evoNode.getElementsByTagName("typeid")[0].childNodes)
evoLevel = getText(evoNode.getElementsByTagName("level")[0].childNodes)
evoCost = getText(evoNode.getElementsByTagName("costperlevel")[0].childNodes)
temporaryEvo = Evolutions(evoName, evoId, evoLevel, evoCost)
evolutionList.append(temporaryEvo)
# Or you can go with the 1 liner
evolutionList.append(Evolutions(evoName, evoId, evoLevel, evoCost))
I renamed your list because it shared the same name as your class and was confusing.