Displaying content of a RevertableSet()
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
-
- Regular
- Posts: 164
- Joined: Wed Nov 08, 2017 4:32 pm
- Projects: HSS
- Deviantart: studioerrilhl
- Github: studioerrilhl
- Contact:
Displaying content of a RevertableSet()
Is there a way, in Ren'Py (or using regular python) that I can see WHAT a revertable set contains? A complete dump of the content?
-
- Regular
- Posts: 28
- Joined: Tue Mar 14, 2017 7:15 pm
- Contact:
Re: Displaying content of a RevertableSet()
I've honestly never heard of a revertable set until your question and google only provides a few hits (none of which explain much). Can you explain what you're trying to do, if only for my own education?
-
- Regular
- Posts: 164
- Joined: Wed Nov 08, 2017 4:32 pm
- Projects: HSS
- Deviantart: studioerrilhl
- Github: studioerrilhl
- Contact:
Re: Displaying content of a RevertableSet()
Basically, this code:
If I do "[backpack]" I get this:
I would like a complete list of what is in the backpack-container
Code: Select all
default schoolbooks_item = Item('schoolbooks',5)
default toolbox_item = Item('toolbox',10)
default roses_item = Item('roses',.5)
default backpack = set()
# $ backpack.add(schoolbooks_item)
# $ backpack.add(toolbox_item)
$ backpack.add(roses_item)
Code: Select all
RevertableSet([<store.Item object at 0x1A6AF4F0>])
-
- Regular
- Posts: 111
- Joined: Fri Oct 11, 2013 12:46 am
- Contact:
Re: Displaying content of a RevertableSet()
What you're seeing is a complete print out of what's in the set you called "backpack".
It's showing you the Item objects in your set. So it already works.
Is there a particular piece of information you're looking for?
It's showing you the Item objects in your set. So it already works.
Is there a particular piece of information you're looking for?
-
- Regular
- Posts: 164
- Joined: Wed Nov 08, 2017 4:32 pm
- Projects: HSS
- Deviantart: studioerrilhl
- Github: studioerrilhl
- Contact:
Re: Displaying content of a RevertableSet()
Well, I would like a detailed info about what is in those objects. For instance, from the one above, it contains a toolbox, and roses (I would like to see how it's stored in the set, if possible)
- PyTom
- Ren'Py Creator
- Posts: 16096
- Joined: Mon Feb 02, 2004 10:58 am
- Completed: Moonlight Walks
- Projects: Ren'Py
- IRC Nick: renpytom
- Github: renpytom
- itch: renpytom
- Location: Kings Park, NY
- Contact:
Re: Displaying content of a RevertableSet()
You need to define a __repr__ on item, and have it return a string representing the Item. For example, if an item had a name and price field, you could od:
(In Python, the {!r} code means to get the repr of the next argument to the format method.)
Code: Select all
class Item(object):
def __init__(self, name, price):
self.name = name
self.price = price
def __repr__(self):
return "Item({!r}, {!r})".format(self.name, self.price)
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom(When was the last time you backed up your game?)
Software > Drama • https://www.patreon.com/renpytom
-
- Regular
- Posts: 28
- Joined: Tue Mar 14, 2017 7:15 pm
- Contact:
Re: Displaying content of a RevertableSet()
I see. Tom's answer works, of course. But in the long run you might be better off making Backpack a class with a contents list (or set if you prefer for some reason) to which you can add Items, then writing a simple method to print out the contents.
-
- Regular
- Posts: 164
- Joined: Wed Nov 08, 2017 4:32 pm
- Projects: HSS
- Deviantart: studioerrilhl
- Github: studioerrilhl
- Contact:
Re: Displaying content of a RevertableSet()
I know all that - and it's not a problem showing specific items. What I want it a way to see the items and relations AS THEY ARE IN THE LIST. Something like
without having to iterate through the content. I just want the same thing as var_dump(array) does in PHP :/ This is not for use, this is for debugging.
Code: Select all
list-item [
["toolbox",1],
["roses",2]
]
-
- Regular
- Posts: 111
- Joined: Fri Oct 11, 2013 12:46 am
- Contact:
Re: Displaying content of a RevertableSet()
You say you want the items as they are in the list, and that's exactly what you're getting.
What your desired data format shows isn't the items in the list. It shows 2 attributes of every Item instance in the backpack set, inside a nested list.
In that case, what you want is something like:
http://www.pythonforbeginners.com/basic ... -in-python
If you want every attribute in each Item instance:
https://docs.python.org/2/library/functions.html#dir
What your desired data format shows isn't the items in the list. It shows 2 attributes of every Item instance in the backpack set, inside a nested list.
In that case, what you want is something like:
Code: Select all
item_list = [[i.name, i.price] for i in backpack]
If you want every attribute in each Item instance:
Code: Select all
item_list = [dir(i) for i in backpack]
-
- Regular
- Posts: 164
- Joined: Wed Nov 08, 2017 4:32 pm
- Projects: HSS
- Deviantart: studioerrilhl
- Github: studioerrilhl
- Contact:
Re: Displaying content of a RevertableSet()
Aha. Okay, I think part of the problem here is my general loathing of Python, and its... weird data-structures and multiple built-in ways of doing almost the same thing I did not at all realise that a set() is not even close to a list, or dict, or tuples... but still is. Oh, well. I'm gonna see if I can get the inventory-system I have been trying to get up and running, with proper classes and such.
Re: Displaying content of a RevertableSet()
As an aside, RevertableSet is simply a Ren'py specific implementation of set() that works with rollback. Most python data structures are "translated" into Revertable types behind the scenes automatically.
I'm not sure you understand what the issue is, still. You would get the same output if you put those items in any other array (list, dict, etc.). A set is just an unordered array that doesn't allow duplicates. The issue is not set; the issue is that you expect objects to be printed in a way that they're not, which is why everyone is pointing you towards ways of getting the data you want from inside the objects.
Try the following as an exercise.
I'm not sure you understand what the issue is, still. You would get the same output if you put those items in any other array (list, dict, etc.). A set is just an unordered array that doesn't allow duplicates. The issue is not set; the issue is that you expect objects to be printed in a way that they're not, which is why everyone is pointing you towards ways of getting the data you want from inside the objects.
Try the following as an exercise.
Code: Select all
$ a = [toolbox_item, "roses"]
"[a]"
-
- Regular
- Posts: 164
- Joined: Wed Nov 08, 2017 4:32 pm
- Projects: HSS
- Deviantart: studioerrilhl
- Github: studioerrilhl
- Contact:
Re: Displaying content of a RevertableSet()
Yes - the issue is Pythons ridiculous lack of variable dumps and treating everything as an object without outputting the actual content of said object, but instead references to the object.
No, I get the point, and I think I've found a solution - it is, however, some weird restrictions on Python, like the fact that you can't create a list with named keys (at least it seems so)... why in the world would you not create an array-type object with named keys... and so on and so forth. Python is new to me, and I'm still very much learning its quirks. When you're used to loosely typed languages, there is also the issue with different types not working together and so on. Frustrating for me, but probably makes sense for those who have been doing Python for a while.
No, I get the point, and I think I've found a solution - it is, however, some weird restrictions on Python, like the fact that you can't create a list with named keys (at least it seems so)... why in the world would you not create an array-type object with named keys... and so on and so forth. Python is new to me, and I'm still very much learning its quirks. When you're used to loosely typed languages, there is also the issue with different types not working together and so on. Frustrating for me, but probably makes sense for those who have been doing Python for a while.
-
- Regular
- Posts: 111
- Joined: Fri Oct 11, 2013 12:46 am
- Contact:
Re: Displaying content of a RevertableSet()
Python has variable dumps. I showed you dir(), there's also vars(), globals(), and locals().
You can also easily create a container with named keys:
https://pymotw.com/2/collections/namedtuple.html
You can also easily create a container with named keys:
https://pymotw.com/2/collections/namedtuple.html
-
- Regular
- Posts: 164
- Joined: Wed Nov 08, 2017 4:32 pm
- Projects: HSS
- Deviantart: studioerrilhl
- Github: studioerrilhl
- Contact:
Re: Displaying content of a RevertableSet()
Hm, that is helpful, actually. Thanks
I know they exist, I just wish there was a simple "take whatever type, object, variable, no matter type, instance or whatever" and output EVERYTHING without having to think about what it is and how you would like to interprete the data - ie, something like var_dump() in PHP, which works on pretty much whatever you want to use it on, and tells you type information, length, content, etc etc.
But, again, this is just different ways of doing things, and different thinking, and I'm not used to Python's thinking yet.
I know they exist, I just wish there was a simple "take whatever type, object, variable, no matter type, instance or whatever" and output EVERYTHING without having to think about what it is and how you would like to interprete the data - ie, something like var_dump() in PHP, which works on pretty much whatever you want to use it on, and tells you type information, length, content, etc etc.
But, again, this is just different ways of doing things, and different thinking, and I'm not used to Python's thinking yet.
Who is online
Users browsing this forum: No registered users