We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Don't use builtin function names as variables
There is e.g. `all` in `PicklePersistence` and I think also `type` somewhere. Also `id`

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `telegram/ext/picklepersistence.py`
Content:
```
1 #!/usr/bin/env python
2 #
3 # A library that provides a Python interface to the Telegram Bot API
4 # Copyright (C) 2015-2020
5 # Leandro Toledo de Souza <devs@python-telegram-bot.org>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU Lesser Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser Public License
18 # along with this program.  If not, see [http://www.gnu.org/licenses/].
19 """This module contains the PicklePersistence class."""
20 import pickle
21 from collections import defaultdict
22 from copy import deepcopy
23 
24 from telegram.ext import BasePersistence
25 
26 
27 class PicklePersistence(BasePersistence):
28     """Using python's builtin pickle for making you bot persistent.
29 
30     Attributes:
31         filename (:obj:`str`): The filename for storing the pickle files. When :attr:`single_file`
32             is false this will be used as a prefix.
33         store_user_data (:obj:`bool`): Optional. Whether user_data should be saved by this
34             persistence class.
35         store_chat_data (:obj:`bool`): Optional. Whether user_data should be saved by this
36             persistence class.
37         store_bot_data (:obj:`bool`): Optional. Whether bot_data should be saved by this
38             persistence class.
39         single_file (:obj:`bool`): Optional. When ``False`` will store 3 sperate files of
40             `filename_user_data`, `filename_chat_data` and `filename_conversations`. Default is
41             ``True``.
42         on_flush (:obj:`bool`, optional): When ``True`` will only save to file when :meth:`flush`
43             is called and keep data in memory until that happens. When ``False`` will store data
44             on any transaction *and* on call fo :meth:`flush`. Default is ``False``.
45 
46     Args:
47         filename (:obj:`str`): The filename for storing the pickle files. When :attr:`single_file`
48             is false this will be used as a prefix.
49         store_user_data (:obj:`bool`, optional): Whether user_data should be saved by this
50             persistence class. Default is ``True``.
51         store_chat_data (:obj:`bool`, optional): Whether user_data should be saved by this
52             persistence class. Default is ``True``.
53         store_bot_data (:obj:`bool`, optional): Whether bot_data should be saved by this
54             persistence class. Default is ``True`` .
55         single_file (:obj:`bool`, optional): When ``False`` will store 3 sperate files of
56             `filename_user_data`, `filename_chat_data` and `filename_conversations`. Default is
57             ``True``.
58         on_flush (:obj:`bool`, optional): When ``True`` will only save to file when :meth:`flush`
59             is called and keep data in memory until that happens. When ``False`` will store data
60             on any transaction *and* on call fo :meth:`flush`. Default is ``False``.
61     """
62 
63     def __init__(self, filename,
64                  store_user_data=True,
65                  store_chat_data=True,
66                  store_bot_data=True,
67                  single_file=True,
68                  on_flush=False):
69         super(PicklePersistence, self).__init__(store_user_data=store_user_data,
70                                                 store_chat_data=store_chat_data,
71                                                 store_bot_data=store_bot_data)
72         self.filename = filename
73         self.single_file = single_file
74         self.on_flush = on_flush
75         self.user_data = None
76         self.chat_data = None
77         self.bot_data = None
78         self.conversations = None
79 
80     def load_singlefile(self):
81         try:
82             filename = self.filename
83             with open(self.filename, "rb") as f:
84                 all = pickle.load(f)
85                 self.user_data = defaultdict(dict, all['user_data'])
86                 self.chat_data = defaultdict(dict, all['chat_data'])
87                 # For backwards compatibility with files not containing bot data
88                 self.bot_data = all.get('bot_data', {})
89                 self.conversations = all['conversations']
90         except IOError:
91             self.conversations = {}
92             self.user_data = defaultdict(dict)
93             self.chat_data = defaultdict(dict)
94             self.bot_data = {}
95         except pickle.UnpicklingError:
96             raise TypeError("File {} does not contain valid pickle data".format(filename))
97         except Exception:
98             raise TypeError("Something went wrong unpickling {}".format(filename))
99 
100     def load_file(self, filename):
101         try:
102             with open(filename, "rb") as f:
103                 return pickle.load(f)
104         except IOError:
105             return None
106         except pickle.UnpicklingError:
107             raise TypeError("File {} does not contain valid pickle data".format(filename))
108         except Exception:
109             raise TypeError("Something went wrong unpickling {}".format(filename))
110 
111     def dump_singlefile(self):
112         with open(self.filename, "wb") as f:
113             all = {'conversations': self.conversations, 'user_data': self.user_data,
114                    'chat_data': self.chat_data, 'bot_data': self.bot_data}
115             pickle.dump(all, f)
116 
117     def dump_file(self, filename, data):
118         with open(filename, "wb") as f:
119             pickle.dump(data, f)
120 
121     def get_user_data(self):
122         """Returns the user_data from the pickle file if it exsists or an empty defaultdict.
123 
124         Returns:
125             :obj:`defaultdict`: The restored user data.
126         """
127         if self.user_data:
128             pass
129         elif not self.single_file:
130             filename = "{}_user_data".format(self.filename)
131             data = self.load_file(filename)
132             if not data:
133                 data = defaultdict(dict)
134             else:
135                 data = defaultdict(dict, data)
136             self.user_data = data
137         else:
138             self.load_singlefile()
139         return deepcopy(self.user_data)
140 
141     def get_chat_data(self):
142         """Returns the chat_data from the pickle file if it exsists or an empty defaultdict.
143 
144         Returns:
145             :obj:`defaultdict`: The restored chat data.
146         """
147         if self.chat_data:
148             pass
149         elif not self.single_file:
150             filename = "{}_chat_data".format(self.filename)
151             data = self.load_file(filename)
152             if not data:
153                 data = defaultdict(dict)
154             else:
155                 data = defaultdict(dict, data)
156             self.chat_data = data
157         else:
158             self.load_singlefile()
159         return deepcopy(self.chat_data)
160 
161     def get_bot_data(self):
162         """Returns the bot_data from the pickle file if it exsists or an empty dict.
163 
164         Returns:
165             :obj:`defaultdict`: The restored bot data.
166         """
167         if self.bot_data:
168             pass
169         elif not self.single_file:
170             filename = "{}_bot_data".format(self.filename)
171             data = self.load_file(filename)
172             if not data:
173                 data = {}
174             self.bot_data = data
175         else:
176             self.load_singlefile()
177         return deepcopy(self.bot_data)
178 
179     def get_conversations(self, name):
180         """Returns the conversations from the pickle file if it exsists or an empty defaultdict.
181 
182         Args:
183             name (:obj:`str`): The handlers name.
184 
185         Returns:
186             :obj:`dict`: The restored conversations for the handler.
187         """
188         if self.conversations:
189             pass
190         elif not self.single_file:
191             filename = "{}_conversations".format(self.filename)
192             data = self.load_file(filename)
193             if not data:
194                 data = {name: {}}
195             self.conversations = data
196         else:
197             self.load_singlefile()
198         return self.conversations.get(name, {}).copy()
199 
200     def update_conversation(self, name, key, new_state):
201         """Will update the conversations for the given handler and depending on :attr:`on_flush`
202         save the pickle file.
203 
204         Args:
205             name (:obj:`str`): The handlers name.
206             key (:obj:`tuple`): The key the state is changed for.
207             new_state (:obj:`tuple` | :obj:`any`): The new state for the given key.
208         """
209         if self.conversations.setdefault(name, {}).get(key) == new_state:
210             return
211         self.conversations[name][key] = new_state
212         if not self.on_flush:
213             if not self.single_file:
214                 filename = "{}_conversations".format(self.filename)
215                 self.dump_file(filename, self.conversations)
216             else:
217                 self.dump_singlefile()
218 
219     def update_user_data(self, user_id, data):
220         """Will update the user_data (if changed) and depending on :attr:`on_flush` save the
221         pickle file.
222 
223         Args:
224             user_id (:obj:`int`): The user the data might have been changed for.
225             data (:obj:`dict`): The :attr:`telegram.ext.dispatcher.user_data` [user_id].
226         """
227         if self.user_data.get(user_id) == data:
228             return
229         self.user_data[user_id] = data
230         if not self.on_flush:
231             if not self.single_file:
232                 filename = "{}_user_data".format(self.filename)
233                 self.dump_file(filename, self.user_data)
234             else:
235                 self.dump_singlefile()
236 
237     def update_chat_data(self, chat_id, data):
238         """Will update the chat_data (if changed) and depending on :attr:`on_flush` save the
239         pickle file.
240 
241         Args:
242             chat_id (:obj:`int`): The chat the data might have been changed for.
243             data (:obj:`dict`): The :attr:`telegram.ext.dispatcher.chat_data` [chat_id].
244         """
245         if self.chat_data.get(chat_id) == data:
246             return
247         self.chat_data[chat_id] = data
248         if not self.on_flush:
249             if not self.single_file:
250                 filename = "{}_chat_data".format(self.filename)
251                 self.dump_file(filename, self.chat_data)
252             else:
253                 self.dump_singlefile()
254 
255     def update_bot_data(self, data):
256         """Will update the bot_data (if changed) and depending on :attr:`on_flush` save the
257         pickle file.
258 
259         Args:
260             data (:obj:`dict`): The :attr:`telegram.ext.dispatcher.bot_data`.
261         """
262         if self.bot_data == data:
263             return
264         self.bot_data = data.copy()
265         if not self.on_flush:
266             if not self.single_file:
267                 filename = "{}_bot_data".format(self.filename)
268                 self.dump_file(filename, self.bot_data)
269             else:
270                 self.dump_singlefile()
271 
272     def flush(self):
273         """ Will save all data in memory to pickle file(s).
274         """
275         if self.single_file:
276             if self.user_data or self.chat_data or self.conversations:
277                 self.dump_singlefile()
278         else:
279             if self.user_data:
280                 self.dump_file("{}_user_data".format(self.filename), self.user_data)
281             if self.chat_data:
282                 self.dump_file("{}_chat_data".format(self.filename), self.chat_data)
283             if self.bot_data:
284                 self.dump_file("{}_bot_data".format(self.filename), self.bot_data)
285             if self.conversations:
286                 self.dump_file("{}_conversations".format(self.filename), self.conversations)
287
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/telegram/ext/picklepersistence.py b/telegram/ext/picklepersistence.py
--- a/telegram/ext/picklepersistence.py
+++ b/telegram/ext/picklepersistence.py
@@ -81,12 +81,12 @@
         try:
             filename = self.filename
             with open(self.filename, "rb") as f:
-                all = pickle.load(f)
-                self.user_data = defaultdict(dict, all['user_data'])
-                self.chat_data = defaultdict(dict, all['chat_data'])
+                data = pickle.load(f)
+                self.user_data = defaultdict(dict, data['user_data'])
+                self.chat_data = defaultdict(dict, data['chat_data'])
                 # For backwards compatibility with files not containing bot data
-                self.bot_data = all.get('bot_data', {})
-                self.conversations = all['conversations']
+                self.bot_data = data.get('bot_data', {})
+                self.conversations = data['conversations']
         except IOError:
             self.conversations = {}
             self.user_data = defaultdict(dict)
@@ -110,9 +110,9 @@
 
     def dump_singlefile(self):
         with open(self.filename, "wb") as f:
-            all = {'conversations': self.conversations, 'user_data': self.user_data,
-                   'chat_data': self.chat_data, 'bot_data': self.bot_data}
-            pickle.dump(all, f)
+            data = {'conversations': self.conversations, 'user_data': self.user_data,
+                    'chat_data': self.chat_data, 'bot_data': self.bot_data}
+            pickle.dump(data, f)
 
     def dump_file(self, filename, data):
         with open(filename, "wb") as f:

{"golden_diff": "diff --git a/telegram/ext/picklepersistence.py b/telegram/ext/picklepersistence.py\n--- a/telegram/ext/picklepersistence.py\n+++ b/telegram/ext/picklepersistence.py\n@@ -81,12 +81,12 @@\n         try:\n             filename = self.filename\n             with open(self.filename, \"rb\") as f:\n-                all = pickle.load(f)\n-                self.user_data = defaultdict(dict, all['user_data'])\n-                self.chat_data = defaultdict(dict, all['chat_data'])\n+                data = pickle.load(f)\n+                self.user_data = defaultdict(dict, data['user_data'])\n+                self.chat_data = defaultdict(dict, data['chat_data'])\n                 # For backwards compatibility with files not containing bot data\n-                self.bot_data = all.get('bot_data', {})\n-                self.conversations = all['conversations']\n+                self.bot_data = data.get('bot_data', {})\n+                self.conversations = data['conversations']\n         except IOError:\n             self.conversations = {}\n             self.user_data = defaultdict(dict)\n@@ -110,9 +110,9 @@\n \n     def dump_singlefile(self):\n         with open(self.filename, \"wb\") as f:\n-            all = {'conversations': self.conversations, 'user_data': self.user_data,\n-                   'chat_data': self.chat_data, 'bot_data': self.bot_data}\n-            pickle.dump(all, f)\n+            data = {'conversations': self.conversations, 'user_data': self.user_data,\n+                    'chat_data': self.chat_data, 'bot_data': self.bot_data}\n+            pickle.dump(data, f)\n \n     def dump_file(self, filename, data):\n         with open(filename, \"wb\") as f:\n", "issue": "Don't use builtin function names as variables\nThere is e.g. `all` in `PicklePersistence` and I think also `type` somewhere. Also `id`\n", "before_files": [{"content": "#!/usr/bin/env python\n#\n# A library that provides a Python interface to the Telegram Bot API\n# Copyright (C) 2015-2020\n# Leandro Toledo de Souza <devs@python-telegram-bot.org>\n#\n# This program is free software: you can redistribute it and/or modify\n# it under the terms of the GNU Lesser Public License as published by\n# the Free Software Foundation, either version 3 of the License, or\n# (at your option) any later version.\n#\n# This program is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU Lesser Public License for more details.\n#\n# You should have received a copy of the GNU Lesser Public License\n# along with this program.  If not, see [http://www.gnu.org/licenses/].\n\"\"\"This module contains the PicklePersistence class.\"\"\"\nimport pickle\nfrom collections import defaultdict\nfrom copy import deepcopy\n\nfrom telegram.ext import BasePersistence\n\n\nclass PicklePersistence(BasePersistence):\n    \"\"\"Using python's builtin pickle for making you bot persistent.\n\n    Attributes:\n        filename (:obj:`str`): The filename for storing the pickle files. When :attr:`single_file`\n            is false this will be used as a prefix.\n        store_user_data (:obj:`bool`): Optional. Whether user_data should be saved by this\n            persistence class.\n        store_chat_data (:obj:`bool`): Optional. Whether user_data should be saved by this\n            persistence class.\n        store_bot_data (:obj:`bool`): Optional. Whether bot_data should be saved by this\n            persistence class.\n        single_file (:obj:`bool`): Optional. When ``False`` will store 3 sperate files of\n            `filename_user_data`, `filename_chat_data` and `filename_conversations`. Default is\n            ``True``.\n        on_flush (:obj:`bool`, optional): When ``True`` will only save to file when :meth:`flush`\n            is called and keep data in memory until that happens. When ``False`` will store data\n            on any transaction *and* on call fo :meth:`flush`. Default is ``False``.\n\n    Args:\n        filename (:obj:`str`): The filename for storing the pickle files. When :attr:`single_file`\n            is false this will be used as a prefix.\n        store_user_data (:obj:`bool`, optional): Whether user_data should be saved by this\n            persistence class. Default is ``True``.\n        store_chat_data (:obj:`bool`, optional): Whether user_data should be saved by this\n            persistence class. Default is ``True``.\n        store_bot_data (:obj:`bool`, optional): Whether bot_data should be saved by this\n            persistence class. Default is ``True`` .\n        single_file (:obj:`bool`, optional): When ``False`` will store 3 sperate files of\n            `filename_user_data`, `filename_chat_data` and `filename_conversations`. Default is\n            ``True``.\n        on_flush (:obj:`bool`, optional): When ``True`` will only save to file when :meth:`flush`\n            is called and keep data in memory until that happens. When ``False`` will store data\n            on any transaction *and* on call fo :meth:`flush`. Default is ``False``.\n    \"\"\"\n\n    def __init__(self, filename,\n                 store_user_data=True,\n                 store_chat_data=True,\n                 store_bot_data=True,\n                 single_file=True,\n                 on_flush=False):\n        super(PicklePersistence, self).__init__(store_user_data=store_user_data,\n                                                store_chat_data=store_chat_data,\n                                                store_bot_data=store_bot_data)\n        self.filename = filename\n        self.single_file = single_file\n        self.on_flush = on_flush\n        self.user_data = None\n        self.chat_data = None\n        self.bot_data = None\n        self.conversations = None\n\n    def load_singlefile(self):\n        try:\n            filename = self.filename\n            with open(self.filename, \"rb\") as f:\n                all = pickle.load(f)\n                self.user_data = defaultdict(dict, all['user_data'])\n                self.chat_data = defaultdict(dict, all['chat_data'])\n                # For backwards compatibility with files not containing bot data\n                self.bot_data = all.get('bot_data', {})\n                self.conversations = all['conversations']\n        except IOError:\n            self.conversations = {}\n            self.user_data = defaultdict(dict)\n            self.chat_data = defaultdict(dict)\n            self.bot_data = {}\n        except pickle.UnpicklingError:\n            raise TypeError(\"File {} does not contain valid pickle data\".format(filename))\n        except Exception:\n            raise TypeError(\"Something went wrong unpickling {}\".format(filename))\n\n    def load_file(self, filename):\n        try:\n            with open(filename, \"rb\") as f:\n                return pickle.load(f)\n        except IOError:\n            return None\n        except pickle.UnpicklingError:\n            raise TypeError(\"File {} does not contain valid pickle data\".format(filename))\n        except Exception:\n            raise TypeError(\"Something went wrong unpickling {}\".format(filename))\n\n    def dump_singlefile(self):\n        with open(self.filename, \"wb\") as f:\n            all = {'conversations': self.conversations, 'user_data': self.user_data,\n                   'chat_data': self.chat_data, 'bot_data': self.bot_data}\n            pickle.dump(all, f)\n\n    def dump_file(self, filename, data):\n        with open(filename, \"wb\") as f:\n            pickle.dump(data, f)\n\n    def get_user_data(self):\n        \"\"\"Returns the user_data from the pickle file if it exsists or an empty defaultdict.\n\n        Returns:\n            :obj:`defaultdict`: The restored user data.\n        \"\"\"\n        if self.user_data:\n            pass\n        elif not self.single_file:\n            filename = \"{}_user_data\".format(self.filename)\n            data = self.load_file(filename)\n            if not data:\n                data = defaultdict(dict)\n            else:\n                data = defaultdict(dict, data)\n            self.user_data = data\n        else:\n            self.load_singlefile()\n        return deepcopy(self.user_data)\n\n    def get_chat_data(self):\n        \"\"\"Returns the chat_data from the pickle file if it exsists or an empty defaultdict.\n\n        Returns:\n            :obj:`defaultdict`: The restored chat data.\n        \"\"\"\n        if self.chat_data:\n            pass\n        elif not self.single_file:\n            filename = \"{}_chat_data\".format(self.filename)\n            data = self.load_file(filename)\n            if not data:\n                data = defaultdict(dict)\n            else:\n                data = defaultdict(dict, data)\n            self.chat_data = data\n        else:\n            self.load_singlefile()\n        return deepcopy(self.chat_data)\n\n    def get_bot_data(self):\n        \"\"\"Returns the bot_data from the pickle file if it exsists or an empty dict.\n\n        Returns:\n            :obj:`defaultdict`: The restored bot data.\n        \"\"\"\n        if self.bot_data:\n            pass\n        elif not self.single_file:\n            filename = \"{}_bot_data\".format(self.filename)\n            data = self.load_file(filename)\n            if not data:\n                data = {}\n            self.bot_data = data\n        else:\n            self.load_singlefile()\n        return deepcopy(self.bot_data)\n\n    def get_conversations(self, name):\n        \"\"\"Returns the conversations from the pickle file if it exsists or an empty defaultdict.\n\n        Args:\n            name (:obj:`str`): The handlers name.\n\n        Returns:\n            :obj:`dict`: The restored conversations for the handler.\n        \"\"\"\n        if self.conversations:\n            pass\n        elif not self.single_file:\n            filename = \"{}_conversations\".format(self.filename)\n            data = self.load_file(filename)\n            if not data:\n                data = {name: {}}\n            self.conversations = data\n        else:\n            self.load_singlefile()\n        return self.conversations.get(name, {}).copy()\n\n    def update_conversation(self, name, key, new_state):\n        \"\"\"Will update the conversations for the given handler and depending on :attr:`on_flush`\n        save the pickle file.\n\n        Args:\n            name (:obj:`str`): The handlers name.\n            key (:obj:`tuple`): The key the state is changed for.\n            new_state (:obj:`tuple` | :obj:`any`): The new state for the given key.\n        \"\"\"\n        if self.conversations.setdefault(name, {}).get(key) == new_state:\n            return\n        self.conversations[name][key] = new_state\n        if not self.on_flush:\n            if not self.single_file:\n                filename = \"{}_conversations\".format(self.filename)\n                self.dump_file(filename, self.conversations)\n            else:\n                self.dump_singlefile()\n\n    def update_user_data(self, user_id, data):\n        \"\"\"Will update the user_data (if changed) and depending on :attr:`on_flush` save the\n        pickle file.\n\n        Args:\n            user_id (:obj:`int`): The user the data might have been changed for.\n            data (:obj:`dict`): The :attr:`telegram.ext.dispatcher.user_data` [user_id].\n        \"\"\"\n        if self.user_data.get(user_id) == data:\n            return\n        self.user_data[user_id] = data\n        if not self.on_flush:\n            if not self.single_file:\n                filename = \"{}_user_data\".format(self.filename)\n                self.dump_file(filename, self.user_data)\n            else:\n                self.dump_singlefile()\n\n    def update_chat_data(self, chat_id, data):\n        \"\"\"Will update the chat_data (if changed) and depending on :attr:`on_flush` save the\n        pickle file.\n\n        Args:\n            chat_id (:obj:`int`): The chat the data might have been changed for.\n            data (:obj:`dict`): The :attr:`telegram.ext.dispatcher.chat_data` [chat_id].\n        \"\"\"\n        if self.chat_data.get(chat_id) == data:\n            return\n        self.chat_data[chat_id] = data\n        if not self.on_flush:\n            if not self.single_file:\n                filename = \"{}_chat_data\".format(self.filename)\n                self.dump_file(filename, self.chat_data)\n            else:\n                self.dump_singlefile()\n\n    def update_bot_data(self, data):\n        \"\"\"Will update the bot_data (if changed) and depending on :attr:`on_flush` save the\n        pickle file.\n\n        Args:\n            data (:obj:`dict`): The :attr:`telegram.ext.dispatcher.bot_data`.\n        \"\"\"\n        if self.bot_data == data:\n            return\n        self.bot_data = data.copy()\n        if not self.on_flush:\n            if not self.single_file:\n                filename = \"{}_bot_data\".format(self.filename)\n                self.dump_file(filename, self.bot_data)\n            else:\n                self.dump_singlefile()\n\n    def flush(self):\n        \"\"\" Will save all data in memory to pickle file(s).\n        \"\"\"\n        if self.single_file:\n            if self.user_data or self.chat_data or self.conversations:\n                self.dump_singlefile()\n        else:\n            if self.user_data:\n                self.dump_file(\"{}_user_data\".format(self.filename), self.user_data)\n            if self.chat_data:\n                self.dump_file(\"{}_chat_data\".format(self.filename), self.chat_data)\n            if self.bot_data:\n                self.dump_file(\"{}_bot_data\".format(self.filename), self.bot_data)\n            if self.conversations:\n                self.dump_file(\"{}_conversations\".format(self.filename), self.conversations)\n", "path": "telegram/ext/picklepersistence.py"}], "after_files": [{"content": "#!/usr/bin/env python\n#\n# A library that provides a Python interface to the Telegram Bot API\n# Copyright (C) 2015-2020\n# Leandro Toledo de Souza <devs@python-telegram-bot.org>\n#\n# This program is free software: you can redistribute it and/or modify\n# it under the terms of the GNU Lesser Public License as published by\n# the Free Software Foundation, either version 3 of the License, or\n# (at your option) any later version.\n#\n# This program is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU Lesser Public License for more details.\n#\n# You should have received a copy of the GNU Lesser Public License\n# along with this program.  If not, see [http://www.gnu.org/licenses/].\n\"\"\"This module contains the PicklePersistence class.\"\"\"\nimport pickle\nfrom collections import defaultdict\nfrom copy import deepcopy\n\nfrom telegram.ext import BasePersistence\n\n\nclass PicklePersistence(BasePersistence):\n    \"\"\"Using python's builtin pickle for making you bot persistent.\n\n    Attributes:\n        filename (:obj:`str`): The filename for storing the pickle files. When :attr:`single_file`\n            is false this will be used as a prefix.\n        store_user_data (:obj:`bool`): Optional. Whether user_data should be saved by this\n            persistence class.\n        store_chat_data (:obj:`bool`): Optional. Whether user_data should be saved by this\n            persistence class.\n        store_bot_data (:obj:`bool`): Optional. Whether bot_data should be saved by this\n            persistence class.\n        single_file (:obj:`bool`): Optional. When ``False`` will store 3 sperate files of\n            `filename_user_data`, `filename_chat_data` and `filename_conversations`. Default is\n            ``True``.\n        on_flush (:obj:`bool`, optional): When ``True`` will only save to file when :meth:`flush`\n            is called and keep data in memory until that happens. When ``False`` will store data\n            on any transaction *and* on call fo :meth:`flush`. Default is ``False``.\n\n    Args:\n        filename (:obj:`str`): The filename for storing the pickle files. When :attr:`single_file`\n            is false this will be used as a prefix.\n        store_user_data (:obj:`bool`, optional): Whether user_data should be saved by this\n            persistence class. Default is ``True``.\n        store_chat_data (:obj:`bool`, optional): Whether user_data should be saved by this\n            persistence class. Default is ``True``.\n        store_bot_data (:obj:`bool`, optional): Whether bot_data should be saved by this\n            persistence class. Default is ``True`` .\n        single_file (:obj:`bool`, optional): When ``False`` will store 3 sperate files of\n            `filename_user_data`, `filename_chat_data` and `filename_conversations`. Default is\n            ``True``.\n        on_flush (:obj:`bool`, optional): When ``True`` will only save to file when :meth:`flush`\n            is called and keep data in memory until that happens. When ``False`` will store data\n            on any transaction *and* on call fo :meth:`flush`. Default is ``False``.\n    \"\"\"\n\n    def __init__(self, filename,\n                 store_user_data=True,\n                 store_chat_data=True,\n                 store_bot_data=True,\n                 single_file=True,\n                 on_flush=False):\n        super(PicklePersistence, self).__init__(store_user_data=store_user_data,\n                                                store_chat_data=store_chat_data,\n                                                store_bot_data=store_bot_data)\n        self.filename = filename\n        self.single_file = single_file\n        self.on_flush = on_flush\n        self.user_data = None\n        self.chat_data = None\n        self.bot_data = None\n        self.conversations = None\n\n    def load_singlefile(self):\n        try:\n            filename = self.filename\n            with open(self.filename, \"rb\") as f:\n                data = pickle.load(f)\n                self.user_data = defaultdict(dict, data['user_data'])\n                self.chat_data = defaultdict(dict, data['chat_data'])\n                # For backwards compatibility with files not containing bot data\n                self.bot_data = data.get('bot_data', {})\n                self.conversations = data['conversations']\n        except IOError:\n            self.conversations = {}\n            self.user_data = defaultdict(dict)\n            self.chat_data = defaultdict(dict)\n            self.bot_data = {}\n        except pickle.UnpicklingError:\n            raise TypeError(\"File {} does not contain valid pickle data\".format(filename))\n        except Exception:\n            raise TypeError(\"Something went wrong unpickling {}\".format(filename))\n\n    def load_file(self, filename):\n        try:\n            with open(filename, \"rb\") as f:\n                return pickle.load(f)\n        except IOError:\n            return None\n        except pickle.UnpicklingError:\n            raise TypeError(\"File {} does not contain valid pickle data\".format(filename))\n        except Exception:\n            raise TypeError(\"Something went wrong unpickling {}\".format(filename))\n\n    def dump_singlefile(self):\n        with open(self.filename, \"wb\") as f:\n            data = {'conversations': self.conversations, 'user_data': self.user_data,\n                    'chat_data': self.chat_data, 'bot_data': self.bot_data}\n            pickle.dump(data, f)\n\n    def dump_file(self, filename, data):\n        with open(filename, \"wb\") as f:\n            pickle.dump(data, f)\n\n    def get_user_data(self):\n        \"\"\"Returns the user_data from the pickle file if it exsists or an empty defaultdict.\n\n        Returns:\n            :obj:`defaultdict`: The restored user data.\n        \"\"\"\n        if self.user_data:\n            pass\n        elif not self.single_file:\n            filename = \"{}_user_data\".format(self.filename)\n            data = self.load_file(filename)\n            if not data:\n                data = defaultdict(dict)\n            else:\n                data = defaultdict(dict, data)\n            self.user_data = data\n        else:\n            self.load_singlefile()\n        return deepcopy(self.user_data)\n\n    def get_chat_data(self):\n        \"\"\"Returns the chat_data from the pickle file if it exsists or an empty defaultdict.\n\n        Returns:\n            :obj:`defaultdict`: The restored chat data.\n        \"\"\"\n        if self.chat_data:\n            pass\n        elif not self.single_file:\n            filename = \"{}_chat_data\".format(self.filename)\n            data = self.load_file(filename)\n            if not data:\n                data = defaultdict(dict)\n            else:\n                data = defaultdict(dict, data)\n            self.chat_data = data\n        else:\n            self.load_singlefile()\n        return deepcopy(self.chat_data)\n\n    def get_bot_data(self):\n        \"\"\"Returns the bot_data from the pickle file if it exsists or an empty dict.\n\n        Returns:\n            :obj:`defaultdict`: The restored bot data.\n        \"\"\"\n        if self.bot_data:\n            pass\n        elif not self.single_file:\n            filename = \"{}_bot_data\".format(self.filename)\n            data = self.load_file(filename)\n            if not data:\n                data = {}\n            self.bot_data = data\n        else:\n            self.load_singlefile()\n        return deepcopy(self.bot_data)\n\n    def get_conversations(self, name):\n        \"\"\"Returns the conversations from the pickle file if it exsists or an empty defaultdict.\n\n        Args:\n            name (:obj:`str`): The handlers name.\n\n        Returns:\n            :obj:`dict`: The restored conversations for the handler.\n        \"\"\"\n        if self.conversations:\n            pass\n        elif not self.single_file:\n            filename = \"{}_conversations\".format(self.filename)\n            data = self.load_file(filename)\n            if not data:\n                data = {name: {}}\n            self.conversations = data\n        else:\n            self.load_singlefile()\n        return self.conversations.get(name, {}).copy()\n\n    def update_conversation(self, name, key, new_state):\n        \"\"\"Will update the conversations for the given handler and depending on :attr:`on_flush`\n        save the pickle file.\n\n        Args:\n            name (:obj:`str`): The handlers name.\n            key (:obj:`tuple`): The key the state is changed for.\n            new_state (:obj:`tuple` | :obj:`any`): The new state for the given key.\n        \"\"\"\n        if self.conversations.setdefault(name, {}).get(key) == new_state:\n            return\n        self.conversations[name][key] = new_state\n        if not self.on_flush:\n            if not self.single_file:\n                filename = \"{}_conversations\".format(self.filename)\n                self.dump_file(filename, self.conversations)\n            else:\n                self.dump_singlefile()\n\n    def update_user_data(self, user_id, data):\n        \"\"\"Will update the user_data (if changed) and depending on :attr:`on_flush` save the\n        pickle file.\n\n        Args:\n            user_id (:obj:`int`): The user the data might have been changed for.\n            data (:obj:`dict`): The :attr:`telegram.ext.dispatcher.user_data` [user_id].\n        \"\"\"\n        if self.user_data.get(user_id) == data:\n            return\n        self.user_data[user_id] = data\n        if not self.on_flush:\n            if not self.single_file:\n                filename = \"{}_user_data\".format(self.filename)\n                self.dump_file(filename, self.user_data)\n            else:\n                self.dump_singlefile()\n\n    def update_chat_data(self, chat_id, data):\n        \"\"\"Will update the chat_data (if changed) and depending on :attr:`on_flush` save the\n        pickle file.\n\n        Args:\n            chat_id (:obj:`int`): The chat the data might have been changed for.\n            data (:obj:`dict`): The :attr:`telegram.ext.dispatcher.chat_data` [chat_id].\n        \"\"\"\n        if self.chat_data.get(chat_id) == data:\n            return\n        self.chat_data[chat_id] = data\n        if not self.on_flush:\n            if not self.single_file:\n                filename = \"{}_chat_data\".format(self.filename)\n                self.dump_file(filename, self.chat_data)\n            else:\n                self.dump_singlefile()\n\n    def update_bot_data(self, data):\n        \"\"\"Will update the bot_data (if changed) and depending on :attr:`on_flush` save the\n        pickle file.\n\n        Args:\n            data (:obj:`dict`): The :attr:`telegram.ext.dispatcher.bot_data`.\n        \"\"\"\n        if self.bot_data == data:\n            return\n        self.bot_data = data.copy()\n        if not self.on_flush:\n            if not self.single_file:\n                filename = \"{}_bot_data\".format(self.filename)\n                self.dump_file(filename, self.bot_data)\n            else:\n                self.dump_singlefile()\n\n    def flush(self):\n        \"\"\" Will save all data in memory to pickle file(s).\n        \"\"\"\n        if self.single_file:\n            if self.user_data or self.chat_data or self.conversations:\n                self.dump_singlefile()\n        else:\n            if self.user_data:\n                self.dump_file(\"{}_user_data\".format(self.filename), self.user_data)\n            if self.chat_data:\n                self.dump_file(\"{}_chat_data\".format(self.filename), self.chat_data)\n            if self.bot_data:\n                self.dump_file(\"{}_bot_data\".format(self.filename), self.bot_data)\n            if self.conversations:\n                self.dump_file(\"{}_conversations\".format(self.filename), self.conversations)\n", "path": "telegram/ext/picklepersistence.py"}]}