Python 3, list of objects? New to python - python-3.x

New to python and self-taught so I'm probably going about this horribly wrong but I'm trying to find the best way to list out objects while also placing them in a list. I was advised to find a way to do this by a friend to avoid double entry of creating my object then typing my object's name in. I'm open to any critiques and advice, thanks!
The way I have it set up now is giving me the error
line 16, in <module>
Starters.append(Botamon = Digi("Botamon",0,1,1,1,1,[""]))
TypeError: list.append() takes no keyword arguments"
#Class
class Digi:
def __init__(self,mon,age,offense,defense,speed,brains,evo):
self.mon = mon
self.age = age
self.offense = offense
self.defense = defense
self.speed = speed
self.brains = brains
self.evo = evo
#Digilist
Starters = []
Starters.append(Botamon = Digi("Botamon",0,1,1,1,1,[""]))
Starters.append(Poyomon = Digi("Poyomon",0,1,1,1,1,[""]))
Starters.append(Punimon = Digi("Punimon",0,1,1,1,1,[""]))
Starters.append(Yuramon = Digi("Yuramon",0,1,1,1,1,[""]))
Digilist = []
Digilist.append(Koromon = Digi("Koromon",1,3,3,3,3,["Botamon"]))
Digilist.append(Tokomon = Digi("Tokomon",1,3,3,3,3,["Poyomon"]))
Digilist.append(Tsunomon = Digi("Tsunomon",1,3,3,3,3,["Punimon"]))
Digilist.append(Tanemon = Digi("Tanemon",1,3,3,3,3,["Yuramon"]))
#Starter
self = random.choice(Starters)
name = self.mon

The problem is you are naming your variables while appending them. If you are never going to access them by their name just do it like so:
Starters = []
Starters.append(Digi("Botamon",0,1,1,1,1,[""]))
Starters.append(Digi("Poyomon",0,1,1,1,1,[""]))
Starters.append(Digi("Punimon",0,1,1,1,1,[""]))
Starters.append(Digi("Yuramon",0,1,1,1,1,[""]))
If you have to access them by name later on, create them then append them:
Botamon = Digi("Botamon",0,1,1,1,1,[""])
Starters.append(Botamon)
and so on... However in the code example you have provided it looks like you will not have to access them by their variable name.

Related

How to pass ChoiceField choices to formset?

The name works fine, but I can figure out how to pass the list of choices in the same way. The fields for those come up blank. In debugging, the choices appear properly set up.
forms.py
class MatchSheets(forms.Form):
""" Match sheets """
name = forms.CharField()
propertyuser = forms.ChoiceField(choices=(), required=False)
SheetSet = formset_factory(
MatchSheets,
extra=0
)
views.py
sheets = PropSheetNames.objects.filter(owner=request.user,
sponsoruser=sponsoru_id)
props = SponsorsUsers.objects.filter(owner=request.user,
id=sponsoru_id).all()
initial_set = []
choiceset = (((prop.id), (prop.alias)) for prop in props[0].properties_user.all())
for sh in sheets:
initial_set.append(
{'name': sh.name,
'propertyuser.choices': choiceset}
)
form = SheetSet(request.POST or None, initial=initial_set)
I know someone will point out this could be done better with a modelformset_factory for the whole thing, or modelselect for the propertyuser, but I ran into issues with both, and just doing it manually gave me more flexibility.
First, this was wrong (corrected):
choiceset = [((prop.id), (prop.alias)) for prop in props[0].properties_user.all()]
Then added this below form=
for f in form:
f.fields['propertyuser'].choices = choiceset
Was able to take it further, defaulting the choices to the value of the name field as well:
initial_set = []
nameset = [prop.alias for prop in props[0].properties_user.all()]
choiceset = [((prop.alias), (prop.alias)) for prop in props[0].properties_user.all()]
choiceset.append(('', '----'))
and then
for f in form:
f.fields['propertyuser'].choices = choiceset
if f.initial['name'] is not None and f.initial['name'] in nameset:
f.fields['propertyuser'].initial = f.initial['name']
Now the user just needs to handle the mismatched pairs, and done. These were the reasons I was pushed out of using Model options, at least at my ability level.

How to use an object orientated approach to creating a character for a text based game

I am attempting to learn object-orientated programming with python in creating a simple text-based game. I am currently struggling as a newcomer to this coding approach.
So my idea was to create a sub-class of character to include things like the "stats" as shown in the code below, which would be determined by the "Race" of the character during character creation. For context, the Races will included Dwarf, Elf, Human, Orc.
As seen by my failed attempt I'm clearly missing a vital piece of information to fix what I am doing here. Unfortunately, I imagined the internet to be full of python games yet most don't use an object-orientated approach of which I am trying to learn.
class Character:
def __init__(self, playerName, playerGender, playerRace, playerFaction):
self.playerName = playerName
self.playerGender = playerGender
self.playerRace = playerRace
self.playerFaction = playerFaction
self.playerStats = []
def setPlayerStats(self, mining, manufacturing, exploration, invention, trading):
self.mining = 0
self.manufacturing = 0
self.exploration = 0
self.invention = 0
self.trading = 0
self.playerStats.append(setPlayerStats)
I`m looking for an object-orientated way to group the "Stats" to the character class but unfortunately, my research (probably to do with my lack of understanding) has stumped me and any guidance would be greatly appreciated.
If I understood your question correctly, you will need to modify your base class and do something in the lines of the following (attribute names are changed to better conform to PEP8):
class Character:
def __init__(self, name, gender, faction):
self.player_name = name
self.gender = gender
self.faction = faction
self.set_player_stats()
def _set_player_stats(self, mining, manufacturing, exploration, invention, trading):
self.mining = mining
self.manufacturing = manufacturing
self.exploration = exploration
self.invention = invention
self.trading = trading
def set_player_stats(self):
raise NotImplementedError()
class Dwarf(Character):
def set_player_stats(self):
self._set_player_stats(3, 1, 0, 1, 0)
then you can do the following:
>>> john_the_dwarf = Dwarf("John Doe", "M", "Foo Faction")
>>> john_the_dwarf.mining
3

How to use a variable "holding" a string value as list name?

One of the parameter the I want to pass to a Class init is a variable with a string value. this string value is a name of a list. The init should use this string value to append the class object to that list. what I'm doing wrong?
I've tried using the locals() and globals() but it ended with "TypeError: unhashable type: 'list'". tried also vars() with no use as well.
refList = []
cmpList = []
class Part(object):
def __init__(self, origin, name, type, listName):
self.origin = origin
self.name = name
self.type = type
partListName = locals()[listName]
partListName.append(self)
#... some file parsing..
part1 = Part((str(origin), str(name) ,str(type), 'refList')
# ... some other file parsing ...
part2 = Part((str(origin), str(name) ,str(type), 'cmpList')
Welcome to SO Ram! I think you should rethink your code because this is not a good approach to do it always. It is better for example pass directly the list or something related.
However your issue with your code is that you should use the globals() function. I really recommend you see the next post in order to get more knowledge about how/when use this useful functionality of Python3.
Also, you must declarate your variables with global keywoard because you are going to reference this variables from your Part class.
global refList = []
global cmpList = []
Said all this, your critical code line should look like:
partListName = globals()[listName]

How do you retrieve an objects name from a list and pass it to a function to retrieve that objects property? 'str' object has no attribute

New to python and OOP. Hopefully I'm using the correct terms. I'm using a list to hold all of my objects. I want to reference this list to get the name of the object that I would like to get a property value for. I then want to pass this name to a function to get one or more properties. But I'm getting a string error (because the list is returning a string of the object name and not the actual object).
Here is the code:
class creature():
def __init__(self, name, legs):
self.name = name
self.legs = legs
rat = creature("rat",4)
mouse = creature("mouse",4)
beaver = creature("beaver",4)
squirrel = creature("squirrel",4)
chimpanzee = creature("chimpanzee",2)
gorilla = creature("gorilla",2)
orangutan = creature("orangutan",2)
spider_monkey = creature("spider_monkey",2)
black_widow = creature("black_widow",8)
recluse = creature("recluse",8)
wolf_spider = creature("wolf_spider",8)
daddy_long_leg = creature("daddy_long_leg",8)
def checkLegs(critter):
nbrLegs = critter.legs
return success
animals = [
['rat', 'mouse', 'beaver', 'squirrel'],
['chimpanzee','gorilla','orangutan','spider_monkey'],
['black_widow','recluse','wolf_spider','daddy_long_leg']
]
numberOfLegs = checkLegs(recluse)
print("The Recluse has: ")
print(numberOfLegs)
print(" legs")
Here is the response:
The test animal is: orangutan
Traceback (most recent call last):
File "/Python37/help.py", line 32, in <module>
numberOfLegs = checkLegs(testAnimal)
File "Python37/help.py", line 20, in checkLegs
nbrLegs = critter.legs
AttributeError: 'str' object has no attribute 'legs'
There's a couple things here that are preventing this from working. Look at your checkLegs function. It is returning something called success...however, that's not being used anywhere, and since you haven't wrapped it in ""s (I assume you were trying to return the word success to check if the function works) it is trying to return it as a variable, but of course it's undefined. You want the function to return the result of the code you use the function to execute. In this case you want to return nbrLegs. You also need to check the indentation of your constructor. You also don't need the array of animals since you're already defining them in your class. Other than that you were pretty close. Here's your code, with the fixes implemented:
class creature():
def __init__(self, name, legs):
self.name = name
self.legs = legs
rat = creature("rat",4)
mouse = creature("mouse",4)
beaver = creature("beaver",4)
squirrel = creature("squirrel",4)
chimpanzee = creature("chimpanzee",2)
gorilla = creature("gorilla",2)
orangutan = creature("orangutan",2)
spider_monkey = creature("spider_monkey",2)
black_widow = creature("black_widow",8)
recluse = creature("recluse",8)
wolf_spider = creature("wolf_spider",8)
daddy_long_leg = creature("daddy_long_leg",8)
def checkLegs(critter):
nbrLegs = critter.legs
return nbrLegs
numberOfLegs = checkLegs(recluse)
print("The Recluse has: " + str(numberOfLegs) + " legs")

py2neo: Get the get the end nodes from relationships and all incoming relationships without cypher

I having trouble finding the node that an node is in a relationship with.
I want to be able to find the nodes with a relationship with selected node from the selected node.
Here is an example graph
Here is the code for the example graph:
from py2neo import Node, Relationship, Graph, NodeSelector, Path
from py2neo.ogm import *
graph = Graph(user = 'neo4j', password = 'neo4j')
graph.delete_all()
class Man(GraphObject):
__primarykey__ = "name"
name = Property("name")
likes = RelatedTo("Woman", "LIKES")
class Woman(GraphObject):
__primarykey__ = "name"
name = Property("name")
likes = RelatedTo("Man", "LIKES")
new_man = Man()
new_man.name = "John"
graph.push(new_man)
new_woman = Woman()
new_woman.name = "Sarah"
new_woman.likes.add(Man.select(graph, primary_value="John").first())
graph.push(new_woman)
new_man = Man()
new_man.name = "Joe"
new_man.likes.add(Woman.select(graph, primary_value="Sarah").first())
graph.push(new_man)
My attempt at getting the name of who Sarah likes:
sarah = Woman.select(graph, primary_value="Sarah").first()
sarah.likes._related_objects[0][0].name
# returns "John"
# or
list(sarah.__ogm__.related.values())[0]._related_objects[0][0].name
# returns "John"
I was unable to find any way of getting the name of who likes Sarah without looking at the other nodes. Is this possible or am I wasting my time? Are there better ways to do any of this?
Am I stuck with:
def get_who_likes_sarah():
names = []
for m in Man.select(graph):
try:
name = m.likes._related_objects[0][0].name
if name == "Sarah":
names.append(m.name)
except:
pass
return names
You should get it by doing this:
for rel in graph.match(start_node=sarah, rel_type="LIKES"):
names.append(rel.end_node()["name"])

Resources