Displaying content of a RevertableSet()

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
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.
Post Reply
Message
Author
Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Displaying content of a RevertableSet()

#1 Post by Errilhl »

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?
Currently working on: Image

expenseroso
Regular
Posts: 28
Joined: Tue Mar 14, 2017 7:15 pm
Contact:

Re: Displaying content of a RevertableSet()

#2 Post by expenseroso »

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?

Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Re: Displaying content of a RevertableSet()

#3 Post by Errilhl »

Basically, this code:

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)
If I do "[backpack]" I get this:

Code: Select all

RevertableSet([<store.Item object at 0x1A6AF4F0>])
I would like a complete list of what is in the backpack-container :)
Currently working on: Image

Human Bolt Diary
Regular
Posts: 111
Joined: Fri Oct 11, 2013 12:46 am
Contact:

Re: Displaying content of a RevertableSet()

#4 Post by Human Bolt Diary »

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?

Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Re: Displaying content of a RevertableSet()

#5 Post by Errilhl »

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)
Currently working on: Image

User avatar
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()

#6 Post by PyTom »

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:

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)
(In Python, the {!r} code means to get the repr of the next argument to the format method.)
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

expenseroso
Regular
Posts: 28
Joined: Tue Mar 14, 2017 7:15 pm
Contact:

Re: Displaying content of a RevertableSet()

#7 Post by expenseroso »

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.

Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Re: Displaying content of a RevertableSet()

#8 Post by Errilhl »

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

Code: Select all

list-item [
["toolbox",1],
["roses",2]
]
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.
Currently working on: Image

Human Bolt Diary
Regular
Posts: 111
Joined: Fri Oct 11, 2013 12:46 am
Contact:

Re: Displaying content of a RevertableSet()

#9 Post by Human Bolt Diary »

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:

Code: Select all

item_list = [[i.name, i.price] for i in backpack]
http://www.pythonforbeginners.com/basic ... -in-python

If you want every attribute in each Item instance:

Code: Select all

item_list = [dir(i) for i in backpack]
https://docs.python.org/2/library/functions.html#dir

Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Re: Displaying content of a RevertableSet()

#10 Post by Errilhl »

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 :D 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.
Currently working on: Image

philat
Eileen-Class Veteran
Posts: 1912
Joined: Wed Dec 04, 2013 12:33 pm
Contact:

Re: Displaying content of a RevertableSet()

#11 Post by philat »

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.

Code: Select all

$ a = [toolbox_item, "roses"]
"[a]"

Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Re: Displaying content of a RevertableSet()

#12 Post by Errilhl »

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.
Currently working on: Image

Human Bolt Diary
Regular
Posts: 111
Joined: Fri Oct 11, 2013 12:46 am
Contact:

Re: Displaying content of a RevertableSet()

#13 Post by Human Bolt Diary »

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

Errilhl
Regular
Posts: 164
Joined: Wed Nov 08, 2017 4:32 pm
Projects: HSS
Deviantart: studioerrilhl
Github: studioerrilhl
Contact:

Re: Displaying content of a RevertableSet()

#14 Post by Errilhl »

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.
Currently working on: Image

Post Reply

Who is online

Users browsing this forum: No registered users