ypeError: decoding str is not supported
Hi, I have some code comes from python 2 like the following: str('a', encoding='utf-8') But for python 3, this will fail as follows: >>> str('a', encoding='utf-8') Traceback (most recent call last): File "", line 1, in TypeError: decoding str is not supported How to fix it? -- https://mail.python.org/mailman/listinfo/python-list
Re: ypeError: decoding str is not supported
Hongyi Zhao wrote: > Hi, > > I have some code comes from python 2 like the following: > > str('a', encoding='utf-8') This fails in Python 2 >>> str("a", encoding="utf-8") Traceback (most recent call last): File "", line 1, in TypeError: str() takes at most 1 argument (2 given) ...unless you have redefined str, e. g. with >>> str = unicode >>> str("a", encoding="utf-8") u'a' > But for python 3, this will fail as follows: > str('a', encoding='utf-8') > Traceback (most recent call last): > File "", line 1, in > TypeError: decoding str is not supported > > > How to fix it? Don' try to decode an already decoded string; use it directly: "a" -- https://mail.python.org/mailman/listinfo/python-list
Re: ypeError: decoding str is not supported
On Sat, 28 Sep 2019 at 10:53, Peter Otten <__pete...@web.de> wrote: > > Hongyi Zhao wrote: > > > Hi, > > > > I have some code comes from python 2 like the following: > > > > str('a', encoding='utf-8') > > This fails in Python 2 > > >>> str("a", encoding="utf-8") > Traceback (most recent call last): > File "", line 1, in > TypeError: str() takes at most 1 argument (2 given) > > ...unless you have redefined str, e. g. with > > >>> str = unicode > >>> str("a", encoding="utf-8") > u'a' > > > But for python 3, this will fail as follows: > > > str('a', encoding='utf-8') > > Traceback (most recent call last): > > File "", line 1, in > > TypeError: decoding str is not supported > > > > > > How to fix it? > > Don' try to decode an already decoded string; use it directly: > > "a" To explain a little further, one of the biggest differences between Python 2 and Python 3 is that you *have* to be clear in Python 3 on which data is encoded byte sequences (which need a decode to turn them into text strings, but cannot be encoded, because they already are) and which are text strings (which don't need to be, and can't be, decoded, but which can be encoded if you want to get a byte sequence). If you're not clear whether some data is a byte string or a text string, you will get in a muddle, and Python 2 won't help you (but it will sometimes produce mojibake without generating an error) whereas Python 3 will tend to throw errors flagging the issue (but it may sometimes be stricter than you are used to). Thinking that saying `str = unicode` is a reasonable thing to do is a pretty strong indication that you're not clear on whether your data is text or bytes - either that or you're hoping to make a "quick fix". But as you've found, quick fixes tend to result in a cascade of further issues that *also* need quick fixes. The right solution here (and by far the cleanest one) is to review your code as a whole, and have a clear separation between bytes data and text data. The usual approach people use for this is to decode bytes into text as soon as it's read into your program, and only ever use genuine text data within your program - so you should only ever be using encode/decode in the I/O portion of your application, where it's pretty clear when you have encoded bytes coming in or going out. Hope this helps, Paul -- https://mail.python.org/mailman/listinfo/python-list
Re: NEWBIE: how to get text onto 2 lines on a 16x2 lcd display
On 28/09/2019 15:59, Dennis Lee Bieber wrote: On Fri, 27 Sep 2019 19:46:58 +0100, RobH declaimed the following: Thanks for all that information, but first of all using just import adafruit blinka did not work as it returned bas: import: command not found. Were you in the Python3 interpreter? That error (I presume it was "bash:...") means you typed the line at the console shell, not into a from: can't read /var/mail/Python3from shell console Python interpreter. pi@raspberrypi:~$ #from shell console pi@raspberrypi:~$ import adafruit_blinka -bash: import: command not found pi@raspberrypi:~$ #from Python3 interactive interpreter pi@raspberrypi:~$ python3 Python 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. import adafruit_blinka exit() pi@raspberrypi:~$ I should say that this is on a raspberry pi zero w, if it makes any difference. It doesn't... All R-Pi system use a default OS of Raspbian (a Debian modified for R-Pi specific booting and support) {caveat: if using a NOOBS installer with active WiFi one does have the option of installing any one of half a dozen OSes, though some are quite specific in usage: Windows IoT, media player...} It doesn't even matter if one is using a BeagleBone Black, since that also runs Debian (though R-Pi has updated to "Buster" while the BBB is still on "Stretch" [ver 10 vs ver 9]). debian@beaglebone:~$ #from shell console debian@beaglebone:~$ import adafruit_blinka -bash: import: command not found debian@beaglebone:~$ #from Python3 interactive interpreter debian@beaglebone:~$ python3 Python 3.5.3 (default, Sep 27 2018, 17:25:39) [GCC 6.3.0 20170516] on linux from: can't read /var/mail/Python3from shell console Type "help", "copyright", "credits" or "license" for more information. import adafruit_blinka exit() debian@beaglebone:~$ It does matter if you try from a system that doesn't support embedded programming -- Debian 10 running in a VirtualBox instance on a Window10 computer... Actually -- it did do something wulfraed@debian:~$ import adafruit_blinka wulfraed@debian:~$ wulfraed@debian:~$ ls -l total 11704 -rwxrwx--- 1 wulfraed vboxsf 11909039 Sep 28 10:48 adafruit_blinka drwxrwxrwx 2 wulfraed wulfraed 4096 Sep 13 15:56 Desktop drwxrwxrwx 3 wulfraed wulfraed 4096 Sep 13 14:20 Documents drwxrwxrwx 2 wulfraed wulfraed 4096 Sep 13 13:53 Downloads drwxr-xr-x 3 wulfraed wulfraed 4096 Sep 13 15:18 eclipse drwxr-xr-x 4 wulfraed wulfraed 4096 Sep 13 15:25 eclipse-workspace drwxrwxrwx 5 wulfraed wulfraed 4096 Sep 11 11:33 ga drwxrwxrwx 7 wulfraed wulfraed 4096 Sep 11 13:58 Mail drwxrwxrwx 2 wulfraed wulfraed 4096 Sep 11 10:43 Music drwxrwxrwx 2 wulfraed wulfraed 4096 Sep 11 10:43 Pictures drwxrwxrwx 2 wulfraed wulfraed 4096 Sep 11 10:43 Public drwxrwxrwx 3 wulfraed wulfraed 4096 Sep 17 14:33 Scratch drwxrwxrwx 3 wulfraed wulfraed 4096 Sep 13 15:15 temp drwxrwxrwx 2 wulfraed wulfraed 4096 Sep 11 10:43 Templates -rwxrwxrwx 1 wulfraed wulfraed 8984 Sep 13 13:49 testcross -rwxrwxrwx 1 wulfraed wulfraed 124 Sep 13 13:32 testcross.cpp drwxrwxrwx 2 wulfraed wulfraed 4096 Sep 11 10:43 Videos It created a large file that, on examination, is an EPS image file of my Debian desktop! "import" appears to be a command supplied as part of "ImageMagick" conversion utility. After removing the file, the Python3 interpreter reports wulfraed@debian:~$ python3 Python 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. import adafruit_blinka Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'adafruit_blinka' exit() wulfraed@debian:~$ as expected, since the AdaFruit libraries are not installed on a desktop machine. No I wasn't in the Python3 interpreter, and typing Python3 gets me into, I assume. Then typing: import adafruit_blinka seemed to work as there were no errors. Thanks -- https://mail.python.org/mailman/listinfo/python-list
Synchronous and Asynchronous callbacks
Hello, I'm looking for a solution to a problem which is described best as an observer pattern issue I guess. The scenario is the following, client code can register a callback for one or a list of notifications. This is currently solved by having a dictionary with the possible notifications as the keys and lists of methods being the values. Notification sent, triggers the list of method to be called. Nothing fancy. Now, where it gets trickier is that the client should be able to register synchronous and asynchronous callbacks because some of the actions can't be done in a synchronous callback like updating the statusbar of the UI as the main app would overwrite it. I've read that this isn't a good idea of having synchronous and asynchronous callbacks but to be honest, I don't see how this could be solved otherwise, hence the question what to do in such a case? And as I currently think that there is no other solution then to provide sync and async callback registration possibility how would a solution look like? Two dicts, one for sync and the other for async registrations? That would mean that each notification handler needs to execute one dict after the other. In an ideal world, the notification handler wouldn't know about the difference at all and would call its list of methods(objects?) Any tips for me? Just to be sure as I don't know whether this is relevant, each notification handler provides a different set of arguments to the registered callback functions, hence the multiple notification handlers. Thank you Eren -- https://mail.python.org/mailman/listinfo/python-list