Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bot_data as global memory (V12 version of #1322) #1325

Merged
merged 20 commits into from
Feb 2, 2020
Merged

bot_data as global memory (V12 version of #1322) #1325

merged 20 commits into from
Feb 2, 2020

Conversation

Bibo-Joshi
Copy link
Member

This PR implements the global memory proposed in #1318 by providing a dictionary bot_data in addition to user_data and chat_data in a V12 compatible way (i.e. using context based callbacks, replacing #1322).

Integration of bot_data in the Dispatcher class is provided.
Since the bot_data is a global memory, it will always be added to a CallbackContext (i.e. every call

callback_context = CallbackContext.from_...(disptacher)

will add bot_data to callback_context).

Integration in persistence is also provided. Note, that in update_bot_data, the data is updated by

self.bot_data = data.copy()

in contrast to the current implentation for user/chat_data

self.user/chat_data[chat_id] = data

This is due to problems as described in #1297. However, this does not fix #1297 for bot_data. Whan a good way to handle those problems is found, it should be implemented for bot_data (e.g. usage of deepcopy as proposed in #1301)

In DictPersistence, I just used json.loads() to decode serialized data. For user_data and chat_data, a function from the helpers module is used to allow integer keys. imo, this is not really necessary and may lead to problems (e.g. when using '3' as a key, that will be decoded as 3), so I left it out. If I just didn't get the point of it, I will gladly add this.

Also, I've adjusted the test routines. Since I'm not familiar with the pytest module, please have a close look at those.

Copy link
Member

@jsmnbom jsmnbom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So far I've only done a quick look over, but it looks really good!
I added a few comments/questions that I'd like you to answer :)

And I think you did a good job with the pytest stuff :D

telegram/ext/callbackcontext.py Outdated Show resolved Hide resolved
telegram/ext/dictpersistence.py Outdated Show resolved Hide resolved
telegram/ext/dispatcher.py Outdated Show resolved Hide resolved
@jsmnbom
Copy link
Member

jsmnbom commented Jan 12, 2019

In terms of

In DictPersistence, I just used json.loads() to decode serialized data. For user_data and chat_data, a function from the helpers module is used to allow integer keys. imo, this is not really necessary and may lead to problems (e.g. when using '3' as a key, that will be decoded as 3), so I left it out. If I just didn't get the point of it, I will gladly add this.

I think the reason we do that, is that it's a user_data and chat_data is a defaultdict, with the ids (int) as the key.
So the way you've done here is good :)

Oh and in terms of failed tests.. Most of them you can ignore - we're working on fixing them, but progress is slow :)
It would be lovely if you could fix the flake8 errors (even though I realise that you're probably not the cause, but we changed the configs and you're the first to touch the file since then.)

@Bibo-Joshi
Copy link
Member Author

Thanks for the comments, @jsmnbom!

In terms of

In DictPersistence, I just used json.loads() to decode serialized data. For user_data and chat_data, a function from the helpers module is used to allow integer keys. imo, this is not really necessary and may lead to problems (e.g. when using '3' as a key, that will be decoded as 3), so I left it out. If I just didn't get the point of it, I will gladly add this.

I think the reason we do that, is that it's a user_data and chat_data is a defaultdict, with the ids (int) as the key.
So the way you've done here is good :)

Yeah, but something like user_data[user_id][123] = 'foo' is allowed as well.

It would be lovely if you could fix the flake8 errors (even though I realise that you're probably not the cause, but we changed the configs and you're the first to touch the file since then.)

No problem. I took the liberty and fixed it for commandhandler.py, too, which I didn't touch otherwise.

@jsmnbom
Copy link
Member

jsmnbom commented Feb 13, 2019

Please see my comments in #1301. Some of them apply here too. TLDR: not merging for inclusion in V12 beta just yet since I'm not 100% sure of the internals of persistence.
Also we seem to have a bunch of commits - probably cause the flake8 stuff got fixed elsewhere - sorry about that :)

@Eldinnie
Copy link
Member

I would like to pipe in here. I think this feature is a bit overdone. I don;t see why adding a simple dict to the bot class wouldn't serve the purpose looked for here. I know it ties in with persistence in this way, but I;m not a big fan of introducing new variable to the persistence. It's mainly created to make the ones we already had persistent.

I Appreciate the time you put in to help us here, but if it;s up to me I would like to not implement this feature...

@jsmnbom jsmnbom closed this Feb 14, 2019
@jsmnbom jsmnbom changed the base branch from V12 to master February 14, 2019 12:55
@jsmnbom jsmnbom reopened this Feb 14, 2019
@Bibo-Joshi
Copy link
Member Author

While I'd love to have this for some of my bots, the decision clearly is up to you :D
Just resolving conflicts in case you decide to merge.

@Bibo-Joshi
Copy link
Member Author

Added chances according to #1312.
Needs to be revisited, if #1462 is merged.

@Poolitzer
Copy link
Member

I see no reason for this. Providing chat/user data as a (temporary) storage is great and often used, if someone wants a global memory, they should setup a database.

@Bibo-Joshi
Copy link
Member Author

Maybe I have a different view on this since I always used user_data and chat_data in combination with Persistence and never knew them without …

IMHO, BasePersistence gives a neat way to give users access to persistence services without having to worry about the details and having to inherit from telegram.Bot to add a dict attribute is a bit tiresome …
If we had a Persistence class that works with a proper database (SQL or something), would you feel different about bot_data being a front end to that?

But again, this is my personal opinion and I'm not as experienced as most of @python-telegram-bot/maintainers. I have working code, still I'd love to have this feature natively.

@Poolitzer
Copy link
Member

Yes, I would feel way better. But I would also say that this is out of the scope of this project.

Normally, you would try to optimize how you build the database around your bot. Giving users a general interface would feel weird/wrong to me.

I am the counterpart to you. I used chat/user data years before persistence was a thing and would never use it as a permanent storage. Temporary, great. But for everything really persistent, I use a real database. Maybe Im just too conservative though.

@Bibo-Joshi
Copy link
Member Author

Bibo-Joshi commented Oct 18, 2019

Brought this PR up to date.

If merged, the this wiki page and this one need to be updated.

Copy link
Member

@Bibo-Joshi also the persistence page in wiki will need to be updated right?

@Poolitzer Poolitzer modified the milestones: V12, 12.3 Nov 11, 2019
@tsnoam tsnoam modified the milestones: 12.3, 12.4 Nov 15, 2019
@Eldinnie Eldinnie merged commit f6b663f into python-telegram-bot:master Feb 2, 2020
@Bibo-Joshi Bibo-Joshi deleted the bot-data-V12 branch February 2, 2020 21:28
Bibo-Joshi added a commit that referenced this pull request Feb 13, 2020
@github-actions github-actions bot locked and limited conversation to collaborators Aug 19, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PicklePersistence of user_data set within conversations
5 participants