Reputation: 21
I have a problem with making discord bot on repl.it, my list is 'ObservedList' and i dont know what to do with this, when I watch some tutorials its always just a normal list
from replit import db
db['fruits'] = ['apple','banana']
print(db['fruits'])
Output:
ObservedList(value=['apple', 'banana'])
Upvotes: 0
Views: 2912
Reputation: 39
When poking around in the link provided in the previous answer, I found some interesting things in the __init__ function.
def __init__(
self, on_mutate: Callable[[List], None], value: Optional[List] = None
) -> None:
self._on_mutate_handler = on_mutate
if value is None:
self.value = []
else:
self.value = value
If you want to get the list in an ObserverdList called foo, use foo.value to do so.
For example:
output = [“Apple”, “Banana”] + db[“foo”].value
Assuming the ObservedList in db[“foo”]
is just [“Cantaloupe”]
, the new value of output would be [“Apple”, “Banana”, “Cantaloupe”]
Upvotes: 3
Reputation: 11
At the moment you are asking the bot to print the entire list. If you change your code to:
print(db['fruits'][0])
Output would then be
apple
This is quite an old questions but I hope this answered the question simply.
Upvotes: 1
Reputation: 14273
ObservedList
is a class in the replit package
As stated in the docstring it is
A list that calls a function every time it is mutated.
There is also ObservedDict
class.
There is this tutorial that shed some more light in Advanced Usage section at the bottom:
Another problem you might encounter is related to the mutation feature. Under the hood, this feature works by replacing the primitive list and dict classes with special replacements that listen for mutation, namely replit.database.database.ObservedList and replit.database.ObservedDict.
To JSON encode these values, use the replit.database.dump method. For JSON responses in the web framework, this is done automatically.
To convert these classes to their primitive equivalent, access the value attribute. A function that automatically does this is provided: replit.database.to_primitive.
To avoid this behavior entirely, use the get_raw and set_raw methods instead.
Upvotes: 2