Re: [Tutor] Python script errors
On Wed, Dec 12, 2018 at 06:57:09AM -0600, Ravi Kumar wrote: > I know this occurs when the api response is nulls but how do I handle this? if response is None: handle None case else: handle non-None case -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python script errors
Ravi Kumar wrote: > Hi, > > I have developed a python script to get api calls for meraki > clientlogevents Thanks for all the help previously I am facing few errors > such as > > Json_string=r.json() > > raw_decode > raise JSONDecodeError("Expecting value", s, err.value) from None > json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1) > > '{0:20} {1:30} {2:16} {3:18} {4:10} {5:11} '.format( > deviceserial, type, macaddress,occurredAt, details)) > TypeError: unsupported format string passed to NoneType.__format__ > > I know this occurs when the api response is nulls but how do I handle > this? Say x can be a string or None. >>> for x in ["a string", None]: ... "{:20}".format(x) ... 'a string' Traceback (most recent call last): File "", line 2, in TypeError: unsupported format string passed to NoneType.__format__ The string can be padded while None does not support that. The best solution is usually to replace None with a string that cannot occur normally, and then to use that instead of None. Example: >>> for x in ["a string", None]: ... "{:20}".format("#NONE" if x is None else x) ... 'a string' '#NONE ' Slightly more convenient: you can force string conversion before padding: >>> for x in ["a string", None]: ... "{!s:20}".format(x) ... 'a string' 'None' ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python script errors
On 12/12/2018 12:57, Ravi Kumar wrote: > '{0:20} {1:30} {2:16} {3:18} {4:10} {5:11} '.format( > deviceserial, type, macaddress,occurredAt, details)) > TypeError: unsupported format string passed to NoneType.__format__ You have 6 format holders in your string but you only pass 5 values. Also you seem to have a spurious closing parens at the end of the call to format (after details). But without seeing the full code or error message it is hard to know what is going on. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Python script errors
Hi, I have developed a python script to get api calls for meraki clientlogevents Thanks for all the help previously I am facing few errors such as Json_string=r.json() raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1) >>> '{0:20} {1:30} {2:16} {3:18} {4:10} {5:11} '.format( deviceserial, type, macaddress,occurredAt, details)) TypeError: unsupported format string passed to NoneType.__format__ I know this occurs when the api response is nulls but how do I handle this? Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Increase performance of the script
Steven D'Aprano wrote: > [...] >> In python 2.6 print statement work as print "Solution" >> however after import collection I have to use print with >> print("Solution") is this a known issue ? > > As Peter says, you must have run > > from __future__ import print_function > > to see this behaviour. This has nothing to do with import collection. > You can debug that for yourself by exiting the interactive interpreter, > starting it up again, and trying to print before and after importing > collection. To be fair to Asad -- I sneaked in the __future__ import into my sample code. I did it to be able to write Python 3 code that would still run in his 2.6 interpreter. In hindsight that was not a good idea as it can confuse someone who has never seen it, and the OP has yet to learn other more important things. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python_About "Guess my number" program
Hi Dat, and welcome! On Wed, Dec 12, 2018 at 11:26:30AM +0600, Hoang Quoc Dat wrote: [...] > After a while, I went on and search on the internet and > find out 1 similar program but the coding logic is like: The computer keeps > guessing the middle number of the range and narrow down the range by > focusing on only the upper/lower part of the range after receving our > answer. Yes, that is the classic algorithm. It is a variant of binary search. > I somehow have come to believe that the Python language and our logical > thinking can do better than that option of choosing the middle number all > the time but I still cannot find out the proper answer (probably because I > am very much new to this). Could someone provide answer for this program? If the only information you have is whether the secret number is "higher" or "lower", then no, there is no better algorithm. If you had more information, like "red hot, hot, warmer, warm, cool, cold, colder, ice cold" to indicate how close the guess is, then you might be able to do better, if you had some objective rule for deciding how close "warm" etc was. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python_About "Guess my number" program
On 12/12/2018 05:26, Hoang Quoc Dat wrote: > find out 1 similar program but the coding logic is like: The computer keeps > guessing the middle number of the range and narrow down the range by > focusing on only the upper/lower part of the range after receiving our > answer. > > I somehow have come to believe that the Python language and our logical > thinking can do better than that option of choosing the middle number all > the time but I still cannot find out the proper answer Actually, that is quite an efficient algorithm. It even has a name: the "binary chop". But can you think of a better one? Forget about the computer. Ask how you would do it using pen and paper. How would you guess the number? Once you understand that you should be able to program Python to do the same thing. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Increase performance of the script
On Wed, Dec 12, 2018 at 12:52:09AM -0500, Avi Gross wrote: > Asad, > > I wonder if an import from __future__ happened, perhaps in the version of > collections you used. Later versions of 2.x allow optional use of the 3.x > style of print. The effect of __future__ imports, like any other import, is only within the module that actually does the import. Even in the unlikely event that collections did such a future import, it would only effect print within that module, not globally or in the interactive interpreter. Here's a demo: # prfunc_demo.py from __future__ import print_function try: exec("print 123") except SyntaxError: print("old style print failed, as expected") print("as print is now a function") And importing it into the interactive interpreter shows that the effect of the future import is localised: [steve@ando ~]$ python2.6 Python 2.6.7 (r267:88850, Mar 10 2012, 12:32:58) [GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2 Type "help", "copyright", "credits" or "license" for more information. py> import prfunc_demo old style print failed, as expected as print is now a function py> print "But here in the REPL, nothing has changed." But here in the REPL, nothing has changed. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Python_About "Guess my number" program
Dear all, This is Dat, I am a newbie in learning Python. My recent attempt is to read and practice the book of Michael Dawson-Python Programming for the Absolute Beginner 3rd Edition. At the end of Chapter 3 there is a challenge of creating "Guess my number" program where the computer guess your number from 1-100. I have been trying to get it through but stuck while attempting to figure out the loop of how the computer will understand and narrow down the guessing range. After a while, I went on and search on the internet and find out 1 similar program but the coding logic is like: The computer keeps guessing the middle number of the range and narrow down the range by focusing on only the upper/lower part of the range after receving our answer. I somehow have come to believe that the Python language and our logical thinking can do better than that option of choosing the middle number all the time but I still cannot find out the proper answer (probably because I am very much new to this). Could someone provide answer for this program? or some sample code on how to make the computer understand it like human do? Thank you in advance and have a nice day everyone. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Increase performance of the script
Asad, I wonder if an import from __future__ happened, perhaps in the version of collections you used. Later versions of 2.x allow optional use of the 3.x style of print. When you redefine print, the old statement style is hidden or worse. -Original Message- From: Tutor On Behalf Of Asad Sent: Tuesday, December 11, 2018 10:38 AM To: tutor@python.org Subject: [Tutor] Increase performance of the script Hi All, I used your solution , however found a strange issue with deque : I am using python 2.6.6: >>> import collections >>> d = collections.deque('abcdefg') >>> print 'Deque:', d File "", line 1 print 'Deque:', d ^ SyntaxError: invalid syntax >>> print ('Deque:', d) Deque: deque(['a', 'b', 'c', 'd', 'e', 'f', 'g']) >>> print d File "", line 1 print d ^ SyntaxError: invalid syntax >>> print (d) deque(['a', 'b', 'c', 'd', 'e', 'f', 'g']) In python 2.6 print statement work as print "Solution" however after import collection I have to use print with print("Solution") is this a known issue ? Please let me know . Thanks, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor