Python Forum
Thank you from a rookie with a question
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Thank you from a rookie with a question
#1
Good afternoon folks :) I have a question that's going to require a fairly deep technical review but as I am new to python (2 weeks) and getting further than my previous attempts at object oriented programming and did I mention I have a brain injury :) So I will ask kind of a general question to which I will get all righteously indignant if you yell at me that I need specific examples! :) I get it. Bear with me. Thanks in advance.

I have a class to represent a singular object - although this isn't my example, we'll just say "character"
Obviously, the character can have equipment.
Some of that equipment can be containers.
The contents of the containers can be a variety of objects.

My code currently instantiates the character object and creates an object attribute that is for example items{} in the constructor for the object.
My code also populates the character object with the rows of items{} successfully
Some of the character.items rows need character.items.contents

What I am struggling with is the notation necessary to create character.items.container.(attributes here)

In general what I am doing is
defining the character class
defining the items class
defining the containers

populating a new character object
getting the list of items in inventory
storing them as character.items{}
getting the contents of the containers
printing out the contents of the container
breaking down trying to store it

my method of adding a new object is to do addItemToInventory passing the parameter of the item object and then addingContentsToItem. The methods for adding define a new object set the attributes and returns an object :

(this is my example, obviously not RPG characters)

self.planets[_planetID]=Planet.addPlanetToChar(_solarSystemID, _planetID, _planetType, _ownerID, _lastUpdate, _upgradeLevel, _numPins)

This works and populate myChar.planets{}

So I iterate through the planets{} attempt to put a second iteration to add to the planets a "pin" record (a physical location on a map with equipment) so that I end up with myChar.planets{}.pins{]

This is where I bomb:
print("\n===>Pin data for planet: ", self.planets[row].planetID," has ", self.planets[row].numPins," rows\n")
	newFormatString = "\t{0:^10}\t{1:^10}\t{2:>2.2f}\t{3:>2.2f}"
	#print(self.planets[row].planetID)	
	for row2 in range(0,len(new_planetPins)):
		_pinID = new_planetPins[row2]['pin_id']
		_typeID = new_planetPins[row2]['type_id']
		_lon = new_planetPins[row2]['longitude']
		_lat = new_planetPins[row2]['latitude']								
		print(newFormatString.format(_pinID, _typeID, _lon, _lat))									
		self.planets[row].planetID[row2]=Pin.addPinToPlanet(_pinID, _typeID, _lon, _lat, self.planets[row].planetID)				
And as expected, I bomb trying to treat the integer index as an object and I'm not sure how to code around this

self.planets[row].planetID[row2]=Pin.addPinToPlanet(_pinID, _typeID, _lon, _lat, self.planets[row].planetID)
TypeError: 'int' object does not support item assignment

Again, thanks ahead of time and I will answer all questions just please don't mock my coding style, it's older than you.
Reply
#2
hi,
1st of all, what is items{} ? did u mean a dictionary variable?

2nd, it seems self.planets[row].planetID is an integer type... correct me if im wrong
swallow osama bin laden
Reply
#3
so yes, confusion over data types is driving part of my issue here.

You are correct, items is a dictionary (although I could use any data structure that supports multiple rows)
You are also correct that that integer index is what's led me to ask the question here, I got to that point and realized my data structures are not right.

My goal is to have items (planets) look like this

planet id (left side),planet type (5 more similar values)

each planet id has 1 to a bizzilion installations

So char has X planets
Each planet has Y installations
Each installation of a container type will have Z rows of contents

So my data structure plan was to include the planet IDs on the character object and have planet objects (array or whatever) that use the character name as part of the object (character name, planet id, blah blah blah)

So, I am completly open to suggestions, how to organize the object classes here is whats probably holding me back. I am not completely thinking in python yet. So either the class hierarchy carries the owner of the planet and each planet is the owner of its installations, and each installation is the owner of the installations contents.

I appreciate the feed back and understand we are a ways from a correct design.
Reply
#4
ok,think i'll just stick to your plan... just make sure the planetID attribute on your class is a compound type(list,tuple,dict etc) ... simple isnt? lack of code samples to look at so cud hardly suggest further
swallow osama bin laden
Reply
#5
I will take a whack at that - so I thought a dictionary would allow a value of a complex object type? thus I thought {} on the parent class would define a dictionary (key = planet id) and the right side would then be a list or the child object types)

Let me monkey with your suggestion and if that doesn't steer me the right way I'll get back with a full code example. To be clear, if I want to use a second class of object for the planets, would I make it a child class of the character (relationship based on character name) or would I simply make it a collection of objects of planet class but not any inheritance/class relationship?
Reply
#6
since char is the main instance , i think its more convenient/less confuse to make collection of instances from planet class (not inherited) , then add/append them into char's {} later. did i answer your question? havent been through class with games whatsover so i really dont know if this make any sense. Huh was just trying to help fixing that 'typeerror'
swallow osama bin laden
Reply
#7
Quite alright and yes, I understand the problem now, and can pursue a solution. I have worked in the computer industry (payment systems) for more than 35 years but every time I talk to a hot shot programmer they are like half my age and only understand game speak :) Thus the game analogy just to help settle the object constants.

I took on this little project to teach myself python but quickly found that my desire to program object oriented was finally in a language that made sense.

I'll keep you posted on results. I'm sure you'll be waiting with baited breath :) Peace.
Reply
#8
This effort isn't concluded yet, I am still working on this program and it has progressed a long way. I'll post when its finally complete.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020