From sinless199 at gmail.com Fri Feb 1 08:15:28 2019 From: sinless199 at gmail.com (sinless199 at gmail.com) Date: Fri, 1 Feb 2019 05:15:28 -0800 (PST) Subject: Instance of 'dict' has no 'replace' member Message-ID: <09f2664c-563d-47d1-b47d-3260f4b40434@googlegroups.com> Hello guys can you help me to solve problem when i compile proram got error like this "Instance of 'dict' has no 'replace' member[no member](67;14)". here you can see my code. url = "http://mapa.um.warszawa.pl/mapviewer/foi" querystring = {"request":"getfoi","version":"1.0","bbox":"${xmin}:${ymin}:${xmax}:${ymax}","width":"${imageWidth}","height":"${imageHeight}","theme":"dane_wawa.NMZ_WUP_WODA","clickable":"no","area":"yes","dstsrid":"2178","wholeimage":"yes","renderlabels":"yes","tid":"653_1461267","aw":"no"} payload = "" headers = { 'cache-control': "no-cache", 'Postman-Token': "d23cc8e3-423e-43a0-9d54-0cae251a4de9" } if not os.path.exists(targetDir): os.makedirs(targetDir) ###################### # Main loop ########## ###################### for y in range(fullExtent['ymax'], fullExtent['ymin'], - tileSizeY): for x in range(fullExtent['xmin'], fullExtent['xmax'], tileSizeX): targetFile = open(targetDir + "\\" + str(x) + "_" + str(y) + ".png", "wb") url_link = querystring.replace("${ymin}", str(y - tileSizeY)).replace("${xmin}", str(x)).replace("${xmax}", str(x + tileSizeX)).replace("${ymax}", str(y)).replace("${imageWidth}", str(imageWidth)).replace("${imageHeight}",str(imageHeight)) targetWorldFile = open(targetDir + "\\" + str(x) + "_" + str(y) + ".pgw", "w") print ("URL: " + url + "\n") for tryNum in range(0, retryTimes): try: response_get = requests.get(url, data=payload, headers=headers, params=url_link) req = urllib2.Request(response_get.text[66:131], None, {'User-Agent': userAgentString}) response = urllib2.urlopen(req) responseText = response.read() targetFile.write(responseText) targetWorldFile.write(str(pixelSizeX) + "\n" + str(0.0000000) + "\n" + str(0.0000000)+ "\n" + str(-pixelSizeY) + "\n" + str(y - tileSizeY) + "\n" + str(x + tileSizeX)) except Exception: img = Image.new('RGBA', (imageWidth, imageHeight)) img.save(targetFile) pass From 2QdxY4RzWzUUiLuE at potatochowder.com Fri Feb 1 08:57:26 2019 From: 2QdxY4RzWzUUiLuE at potatochowder.com (Dan Sommers) Date: Fri, 1 Feb 2019 07:57:26 -0600 Subject: Instance of 'dict' has no 'replace' member In-Reply-To: <09f2664c-563d-47d1-b47d-3260f4b40434@googlegroups.com> References: <09f2664c-563d-47d1-b47d-3260f4b40434@googlegroups.com> Message-ID: <4deb8437-dec9-f36a-edc9-2279c22a390c@potatochowder.com> On 2/1/19 7:15 AM, sinless199 at gmail.com wrote: > Hello guys can you help me to solve problem when i compile proram got error like this "Instance of 'dict' has no 'replace' member[no member](67;14)". Python dicts don't have a replace method. It looks like you're trying to replace strings inside particular dict entries; perhaps you meant something like this: querystring["key"] = querystring["key"].replace("placeholder", "value") HTH, Dan From bgailer at gmail.com Fri Feb 1 10:29:56 2019 From: bgailer at gmail.com (Bob Gailer) Date: Fri, 1 Feb 2019 10:29:56 -0500 Subject: OT - need help with PHP In-Reply-To: References: Message-ID: Trying to setup PHP on Windows 10 using the curl extension to run standalone. Why? I am trying to use an API where the only coding examples are written in PHP. My goal is to use python, and the place where I'm stuck is: the examples use Curl to post requests; my attempts to translate this to urllib. request have failed. I can't even figure out how to sign up for a PHP email list. Help with either of the above would be welcome. Bob Gailer From larry.martell at gmail.com Fri Feb 1 10:33:49 2019 From: larry.martell at gmail.com (Larry Martell) Date: Fri, 1 Feb 2019 10:33:49 -0500 Subject: OT - need help with PHP In-Reply-To: References: Message-ID: On Fri, Feb 1, 2019 at 10:30 AM Bob Gailer wrote: > I can't even figure out how to sign up for a PHP email list. http://php.net/manual/en/faq.mailinglist.php From rosuav at gmail.com Fri Feb 1 10:36:06 2019 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 2 Feb 2019 02:36:06 +1100 Subject: OT - need help with PHP In-Reply-To: References: Message-ID: On Sat, Feb 2, 2019 at 2:31 AM Bob Gailer wrote: > > Trying to setup PHP on Windows 10 using the curl extension to run > standalone. Why? I am trying to use an API where the only coding examples > are written in PHP. My goal is to use python, and the place where I'm stuck > is: the examples use Curl to post requests; my attempts to translate this > to urllib. request have failed. > If it's an HTTP-based API, it's probably easier to translate the examples to use the 'requests' library than to first set up PHP and then translate everything to urllib after that. http://docs.python-requests.org/en/master/ Feel free to post questions here of the form "here's an example of doing something in PHP - how can I do that in Python", as that would be 100% on topic. ChrisA From none at invalid.com Fri Feb 1 14:06:36 2019 From: none at invalid.com (mm0fmf) Date: Fri, 01 Feb 2019 19:06:36 +0000 Subject: OT - need help with PHP In-Reply-To: References: Message-ID: On 01/02/2019 15:36, Chris Angelico wrote: > On Sat, Feb 2, 2019 at 2:31 AM Bob Gailer wrote: >> >> Trying to setup PHP on Windows 10 using the curl extension to run >> standalone. Why? I am trying to use an API where the only coding examples >> are written in PHP. My goal is to use python, and the place where I'm stuck >> is: the examples use Curl to post requests; my attempts to translate this >> to urllib. request have failed. >> > > If it's an HTTP-based API, it's probably easier to translate the > examples to use the 'requests' library than to first set up PHP and > then translate everything to urllib after that. > > http://docs.python-requests.org/en/master/ > > Feel free to post questions here of the form "here's an example of > doing something in PHP - how can I do that in Python", as that would > be 100% on topic. > > ChrisA > +1 for using requests. From bgailer at gmail.com Fri Feb 1 15:07:01 2019 From: bgailer at gmail.com (bob gailer) Date: Fri, 1 Feb 2019 15:07:01 -0500 Subject: OT - need help with PHP In-Reply-To: References: Message-ID: Thank you for your various responses.? All helpful and encouraging. RE mailing list: I followed the instructions at http://php.net/manual/en/faq.mailinglist.php. I have had no response. Shouldn't I get something either welcoming me to the list or requesting a confirmation? Here are the instructions from the api vendor: (somewhat lengthy) HOW TO CALL A FUNCTION USING VOIP.MS REST/JSON API The following samples show how to get all Servers Information from our database and how to select a specific Server for your display purposes. Please Note: - When using our REST/JSON API you need to send the Method to be used and the Required Parameters as part of the URL. - By default the output Content-Type is "text/html". - If you want the output Content-Type to be "application/json", add the following to your URL: &content_type=json PHP - Using cURL GET - Sample Code |$ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt($ch, CURLOPT_URL, "https://voip.ms/api/v1/rest.php?api_username=john at domain.com&api_password=password&method=getServersInfo&server_pop=1"); $result1 = curl_exec($ch); curl_close($ch); $data1=json_decode($result1,true); print_r($data1);| PHP - Using cURL POST - Sample Code |$postfields = array( 'api_username'=>'john at domain.com', 'api_password'=>'password', 'method'=>'getServersInfo', 'server_pop'=>'1'); $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt($ch, CURLOPT_POST, true ); curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); curl_setopt($ch, CURLOPT_URL, "https://voip.ms/api/v1/rest.php"); $result = curl_exec($ch); curl_close($ch); $data=json_decode($result,true); print_r($data); | Note: I have edited these examples by removing unnecessary stuff, to make their size reasonable. On the Python side, I can make the GET version work using urllib.request. I will try requests soon. When I try to run the post example using php -r "path-to-php-progran"; It just prints the program. Any attempt at converting that to POST get me a Bad Request response. One of my needs is to upload a .wav file. The vendor requires the file to be encoded into base64? and the result string included in the POST data, which can lead to enormously long POST data. I can successfully use GET to send very short .wav files, but the url length limit is quickly reached for a reasonable length recording. Trying to use the POST ability that allows me to specify the file by path fails at the vendor side. Apology for long post, but I don't know what to omit. Again any help is welcome. -- Bob Gailer --- This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus From rosuav at gmail.com Fri Feb 1 15:16:07 2019 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 2 Feb 2019 07:16:07 +1100 Subject: OT - need help with PHP In-Reply-To: References: Message-ID: On Sat, Feb 2, 2019 at 7:08 AM bob gailer wrote: > HOW TO CALL A FUNCTION USING VOIP.MS REST/JSON API > The following samples show how to get all Servers Information from our > database and how to select a specific Server for your display purposes. > > Please Note: > - When using our REST/JSON API you need to send the Method to be used > and the Required Parameters as part of the URL. > - By default the output Content-Type is "text/html". > - If you want the output Content-Type to be "application/json", add the > following to your URL: &content_type=json Cool, this is the most important information. > PHP - Using cURL POST - Sample Code > > |$postfields = array( 'api_username'=>'john at domain.com', > 'api_password'=>'password', 'method'=>'getServersInfo', > 'server_pop'=>'1'); $ch = curl_init(); curl_setopt($ch, > CURLOPT_RETURNTRANSFER, true ); curl_setopt($ch, CURLOPT_POST, true ); > curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields); curl_setopt($ch, > CURLOPT_URL, "https://voip.ms/api/v1/rest.php"); $result = > curl_exec($ch); curl_close($ch); $data=json_decode($result,true); > print_r($data); | The PHP array can be converted easily into a Python dictionary: postfields = { "api_username": "john at domain.com", "api_password": "password", "method": "getServersInfo", "server_pop": "1", } I don't know what the CURLOPT_RETURNTRANSFER option does, so I'm going to ignore it. Since there's nothing obvious about the format of POST data, I'm going to assume that it's meant to be form encoded. Give this a try and see if it works. r = requests.post("https://voip.ms/api/v1/rest.php", data=postfields) Many APIs use JSON rather than form encoding, in which case you'd say "json=" instead of "data=", but that seems unlikely in this case. > Note: I have edited these examples by removing unnecessary stuff, to > make their size reasonable. Assuming you haven't omitted anything important, the translation to requests should be fairly straight-forward. > One of my needs is to upload a .wav file. The vendor requires the file > to be encoded into base64 and the result string included in the POST > data, which can lead to enormously long POST data. I can successfully > use GET to send very short .wav files, but the url length limit is > quickly reached for a reasonable length recording. Trying to use the > POST ability that allows me to specify the file by path fails at the > vendor side. http://docs.python-requests.org/en/master/user/quickstart/#post-a-multipart-encoded-file You'd need to match the PHP examples to the way Requests does file uploads, but it ought to work. ChrisA From vergos.nikolas at gmail.com Sat Feb 2 00:57:35 2019 From: vergos.nikolas at gmail.com (vergos.nikolas at gmail.com) Date: Fri, 1 Feb 2019 21:57:35 -0800 (PST) Subject: Selection based ob variables issue Message-ID: <40f5e5ca-f4ac-4b54-8b4a-ae2eab8bf16f@googlegroups.com> Hello, iam tryting to execute mysql queries based on python variables. name = request.args.get('name') month = request.args.get('month') year = request.args.get('year') try: if '=' not in name + month + year: cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, month, year) ) elif '=' not in name + year: cur.execute( '''SELECT * FROM jobs WHERE clientID = (SELECT id FROM clients WHERE name = %s) and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (name, year) ) elif '=' not in month + year: cur.execute( '''SELECT * FROM jobs WHERE MONTH(lastvisit) = %s and YEAR(lastvisit) = %s ORDER BY lastvisit DESC''', (month, year) ) elif '=' not in year: cur.execute( '''SELECT * FROM jobs WHERE YEAR(lastvisit) = %s ORDER BY lastvisit ASC''', year ) else: Problem is that only the first clause works as expected and not the rest. '=' means variables contains no value. How can i make it work? From bgailer at gmail.com Sat Feb 2 09:41:19 2019 From: bgailer at gmail.com (Bob Gailer) Date: Sat, 2 Feb 2019 09:41:19 -0500 Subject: OT - need help with PHP In-Reply-To: References: Message-ID: Thank you. I will get back to you on that shortly. Bob From 12345678mb1541def at gmail.com Fri Feb 1 10:49:32 2019 From: 12345678mb1541def at gmail.com (mb1541def 0) Date: Fri, 1 Feb 2019 10:49:32 -0500 Subject: cant use def command Message-ID: <5c546a8d.1c69fb81.13d58.6d76@mx.google.com> Hello, I need help on the def command. My script: Import os Test def Test(): print(?test?) os.system(?pause?) someone please help,it gives me an error in python 3. I did everything right. --mb1541def From tjreedy at udel.edu Sat Feb 2 16:10:44 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 2 Feb 2019 16:10:44 -0500 Subject: cant use def command In-Reply-To: <5c546a8d.1c69fb81.13d58.6d76@mx.google.com> References: <5c546a8d.1c69fb81.13d58.6d76@mx.google.com> Message-ID: On 2/1/2019 10:49 AM, mb1541def 0 wrote: > > Hello, > I need help on the def command. > > My script: > > Import os > Test > > def Test(): > print(?test?) > os.system(?pause?) > > someone please help,it gives me an error in python 3. The python authors already did by providing you with a traceback that explains what you did wrong. Read is carefully. If you do not understand a trackback, copy and paste into your question. > I did everything right. Your code has 3 errors. When you fix the first, you will get a traceback for the 2nd. When you fix that, you will get a traceback for the 3rd. You should re-read the fine tutorial. -- Terry Jan Reedy From python at mrabarnett.plus.com Sat Feb 2 16:35:45 2019 From: python at mrabarnett.plus.com (MRAB) Date: Sat, 2 Feb 2019 21:35:45 +0000 Subject: cant use def command In-Reply-To: <5c546a8d.1c69fb81.13d58.6d76@mx.google.com> References: <5c546a8d.1c69fb81.13d58.6d76@mx.google.com> Message-ID: <470fb728-9d04-70e2-9d07-d391706ef589@mrabarnett.plus.com> On 2019-02-01 15:49, mb1541def 0 wrote: > > Hello, > I need help on the def command. > > My script: > > Import os > Test > > def Test(): > print(?test?) > os.system(?pause?) > > someone please help,it gives me an error in python 3. > I did everything right. > > --mb1541def > You didn't say what the error was, so I'll just comment on what I can see. Some of what I'm seeing might be due to whatever software or forum you're using to post the message, messing it up. 1. Capitalization: it should be 'import', not 'Import'. 2. Indentation: the body of the function 'Test' should be indented more than its 'def' line. 3. You're referring to 'Test' before it's defined. 4. You're not calling 'Test', but merely referring to it by name. To call a function, you need to add parentheses, even if they are empty. 5. Quotes: only plain quotation marks should be used; I'm seeing 'smart' quotes. This should work: import os def Test(): print("test") os.system("pause") Test() From john at doe.com Sat Feb 2 18:03:14 2019 From: john at doe.com (John Doe) Date: Sat, 2 Feb 2019 23:03:14 -0000 (UTC) Subject: cant use def command References: <5c546a8d.1c69fb81.13d58.6d76@mx.google.com> Message-ID: On 2019-02-01, mb1541def 0 <12345678mb1541def at gmail.com> wrote: > > Hello, > I need help on the def command. > > My script: > > Import os > Test > > def Test(): > print(?test?) > os.system(?pause?) > > someone please help,it gives me an error in python 3. > I did everything right. > On 2019-02-01, mb1541def 0 <12345678mb1541def at gmail.com> wrote: > > Hello, > I need help on the def command. > > I did everything right. > Nah, You didn't. I recommend you to put your codelines in interpreter so you see what exactly is wrong wrong, step by step. As Terry wrote earlier there're 3 basic error in your code, fix one and you'll get to another one then another. BTW for the future, please write/paste in post what is the error. From flebber.crue at gmail.com Sat Feb 2 19:47:35 2019 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Sat, 2 Feb 2019 16:47:35 -0800 (PST) Subject: Implement C's Switch in Python 3 Message-ID: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> Hi I am trying to convert a switch statement from C into Python. (why? practising). This is the C code. printf("Dated this %d", day); switch (day) { case 1: case 21: case 31: printf("st"); break; case 2: case 22: printf("nd"); break; case 3: case 23: printf("rd"); break; default: printf("th"); break; } printf(" day of "); #Premise if the use enter an int as the date 21 for example it would print 21st. It appends the correct suffix onto a date. Reading and trying to implement a function that uses a dictionary. Not sure how to supply list into it to keep it brief and with default case of 'th'. This is my current code. def f(x): return { [1, 21, 31]: "st", [2, 22]: "nd", [3, 23]: "rd", }.get(x, "th") print(f(21)) I have an unhashable type list. Whats the best way to go? Cheers Sayth From skip.montanaro at gmail.com Sat Feb 2 20:12:56 2019 From: skip.montanaro at gmail.com (Skip Montanaro) Date: Sat, 2 Feb 2019 19:12:56 -0600 Subject: Implement C's Switch in Python 3 In-Reply-To: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> Message-ID: > > I have an unhashable type list. > Try replacing the list with a tuple. Also, read up on immutable v mutable types and dictionary keys for a bit of background on why a list won't work. Skip > From rosuav at gmail.com Sat Feb 2 20:23:24 2019 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 3 Feb 2019 12:23:24 +1100 Subject: Implement C's Switch in Python 3 In-Reply-To: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> Message-ID: On Sun, Feb 3, 2019 at 11:51 AM Sayth Renshaw wrote: > > Hi > > I am trying to convert a switch statement from C into Python. (why? practising). > > This is the C code. > > printf("Dated this %d", day); > switch (day) { > case 1: case 21: case 31: > printf("st"); break; > case 2: case 22: > printf("nd"); break; > case 3: case 23: > printf("rd"); break; > default: printf("th"); break; > > } > printf(" day of "); > > #Premise if the use enter an int as the date 21 for example it would print 21st. It appends the correct suffix onto a date. > > Reading and trying to implement a function that uses a dictionary. Not sure how to supply list into it to keep it brief and with default case of 'th'. > > This is my current code. > > def f(x): > return { > [1, 21, 31]: "st", > [2, 22]: "nd", > [3, 23]: "rd", > }.get(x, "th") > > > print(f(21)) > > I have an unhashable type list. Whats the best way to go? What you really want there is for 1 to map to "st", and 21 to separately map to "st", not for some combined key [1, 21, 31] to map to "st". There's no easy syntax for this, and maybe a helper function would, well, help. def mapper(*plans): plans = iter(plans) d = {} while "moar plans": try: keys = next(plans) except StopIteration: return d value = next(plans) for key in keys: d[key] = value def f(x): return mapper( [1, 21, 31], "st", [2, 22], "nd", [3, 23], "rd", ).get(x, "th") Of course, you can also precompute this: day_ordinal = mapper( [1, 21, 31], "st", [2, 22], "nd", [3, 23], "rd", ) def f(x): return day_ordinal.get(x, "th") ChrisA From cs at cskk.id.au Sat Feb 2 20:31:06 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 3 Feb 2019 12:31:06 +1100 Subject: Implement C's Switch in Python 3 In-Reply-To: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> Message-ID: <20190203013106.GA84610@cskk.homeip.net> On 02Feb2019 16:47, Sayth Renshaw wrote: >I am trying to convert a switch statement from C into Python. (why? >practising). > >This is the C code. > >printf("Dated this %d", day); > switch (day) { > case 1: case 21: case 31: > printf("st"); break; > case 2: case 22: > printf("nd"); break; > case 3: case 23: > printf("rd"); break; > default: printf("th"); break; > > } > printf(" day of "); > >#Premise if the use enter an int as the date 21 for example it would print 21st. It appends the correct suffix onto a date. >Reading and trying to implement a function that uses a dictionary. Not >sure how to supply list into it to keep it brief and with default case >of 'th'. > >This is my current code. > >def f(x): > return { > [1, 21, 31]: "st", > [2, 22]: "nd", > [3, 23]: "rd", > }.get(x, "th") > > >print(f(21)) > >I have an unhashable type list. Whats the best way to go? Skip has commented on lists being unhashable. We can elaborate on that if you like. However, even if you went to tuples (which would let you construct the dict you lay out above), there is another problem. You're looking up "x" in the dict. But the keys of the dict are not integers, they are lists (or tuples) or integers, so they won't match. You _could_ do this: return { 1: "st", 21: "st", 31: "st", 2: "nd", 22: "nd", 3: "rd", 23: "rd", }.get(x, "th") which makes distinct entries for each integer value of interest. The conventional approach would normally be: if x in (1, 21, 31): return "st" if x in (2, 22): return "nd" if x in (3, 23): return "rd" return "th" While this works for a small number of choices, if you had a huge dict with lots of possible values you could return to your dict-keyed-on-tuples approach. You would need to try each tuple in turn: mapping = { (1, 21, 31): "st", (2, 22): "nd", (3, 23): "rd", } for key, suffix in mapping.items(): if x in key: return suffix return "th" However, for big dictionaries (with many keys) you loose a key strength of dicts: constant time lookup. You can see the above code (and the earlier "if" code) are rather linear, with run time going up linearly with the number of keys. You're better with the int->string single value dict version. Cheers, Cameron Simpson From flebber.crue at gmail.com Sat Feb 2 20:52:38 2019 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Sat, 2 Feb 2019 17:52:38 -0800 (PST) Subject: Implement C's Switch in Python 3 In-Reply-To: References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> Message-ID: <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> > >I am trying to convert a switch statement from C into Python. (why? > >practising). > > > >This is the C code. > > > >printf("Dated this %d", day); > > switch (day) { > > case 1: case 21: case 31: > > printf("st"); break; > > case 2: case 22: > > printf("nd"); break; > > case 3: case 23: > > printf("rd"); break; > > default: printf("th"); break; > > > > } > > printf(" day of "); > > > >#Premise if the use enter an int as the date 21 for example it would print 21st. It appends the correct suffix onto a date. > >Reading and trying to implement a function that uses a dictionary. Not > >sure how to supply list into it to keep it brief and with default case > >of 'th'. > > > >This is my current code. > > > >def f(x): > > return { > > [1, 21, 31]: "st", > > [2, 22]: "nd", > > [3, 23]: "rd", > > }.get(x, "th") > > > > > >print(f(21)) > > > >I have an unhashable type list. Whats the best way to go? > > Skip has commented on lists being unhashable. We can elaborate on that > if you like. > > However, even if you went to tuples (which would let you construct the > dict you lay out above), there is another problem. > > You're looking up "x" in the dict. But the keys of the dict are not > integers, they are lists (or tuples) or integers, so they won't match. > > You _could_ do this: > > return { > 1: "st", 21: "st", 31: "st", > 2: "nd", 22: "nd", > 3: "rd", 23: "rd", > }.get(x, "th") > > which makes distinct entries for each integer value of interest. > > The conventional approach would normally be: > > if x in (1, 21, 31): > return "st" > if x in (2, 22): > return "nd" > if x in (3, 23): > return "rd" > return "th" > > While this works for a small number of choices, if you had a huge dict > with lots of possible values you could return to your > dict-keyed-on-tuples approach. You would need to try each tuple in turn: > > mapping = { > (1, 21, 31): "st", > (2, 22): "nd", > (3, 23): "rd", > } > for key, suffix in mapping.items(): > if x in key: > return suffix > return "th" > > However, for big dictionaries (with many keys) you loose a key strength > of dicts: constant time lookup. You can see the above code (and the > earlier "if" code) are rather linear, with run time going up linearly > with the number of keys. You're better with the int->string single value > dict version. > > Cheers, > Cameron Simpson It seems odd with C having switch that its cleaner and more efficient than python where we are having to implement our own functions to recreate switch everytime. Or perhaps use a 3rd party library like https://github.com/mikeckennedy/python-switch You have both given good options, it seems there are no standard approaches in this case. Cheers Sayth From skip.montanaro at gmail.com Sat Feb 2 21:21:30 2019 From: skip.montanaro at gmail.com (Skip Montanaro) Date: Sat, 2 Feb 2019 20:21:30 -0600 Subject: Implement C's Switch in Python 3 In-Reply-To: <20190203013106.GA84610@cskk.homeip.net> References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> Message-ID: Cameron wrote: > Skip has commented on lists being unhashable. We can elaborate on that > if you like. > > However, even if you went to tuples (which would let you construct the > dict you lay out above), there is another problem. > > You're looking up "x" in the dict. But the keys of the dict are not > integers, they are lists (or tuples) or integers, so they won't match. Yeah, I solved the immediate issue, but not the ultimate problem. I was replying from my phone. That's my story and I'm sticking to it. Durn smart-ass phones. They kill your attention span. I had to get back to the cat videos. :-) Sayth wrote: > It seems odd with C having switch that its cleaner and more efficient than python where we are having to implement our own functions to recreate switch everytime. Python's development team has considered the possibility of adding a switch/case statement to Python, more than once I think. Various alternatives have been considered and rejected. It's all documented in PEP 3103: https://www.python.org/dev/peps/pep-3103/ Skip From avigross at verizon.net Sat Feb 2 21:51:20 2019 From: avigross at verizon.net (Avi Gross) Date: Sat, 2 Feb 2019 21:51:20 -0500 Subject: Implement C's Switch in Python 3 In-Reply-To: <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> Message-ID: <004501d4bb6b$57b743b0$0725cb10$@verizon.net> I may be missing something, but the focus seems to be only on the rightmost digit. You can get that with str(day)[-1] or with day % 10 Problem: print 1..31 using suffixes such as 1st, 2nd, 3rd, 4th ... So your dictionary needs entries for "1" or 1 and "2" or "2" and of course 3 becoming 3rd. And, frankly, you don't even need a dictionary as a one-liner will do. Here is an example using a helper function: """ Use last digit to determine suffix """ def nthSuffix(day): nth = day % 10 suffix = "st" if nth == 1 else ("nd" if nth == 2 else ("rd" if nth == 3 else "th")) return str(day) + suffix Output: >>> for day in range(1, 32): print( nthSuffix(day)) 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11st 12nd 13rd 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 26th 27th 28th 29th 30th 31st -----Original Message----- From: Python-list On Behalf Of Sayth Renshaw Sent: Saturday, February 2, 2019 8:53 PM To: python-list at python.org Subject: Re: Implement C's Switch in Python 3 > >I am trying to convert a switch statement from C into Python. (why? > >practising). > > > >This is the C code. > > > >printf("Dated this %d", day); > > switch (day) { > > case 1: case 21: case 31: > > printf("st"); break; > > case 2: case 22: > > printf("nd"); break; > > case 3: case 23: > > printf("rd"); break; > > default: printf("th"); break; > > > > } > > printf(" day of "); > > > >#Premise if the use enter an int as the date 21 for example it would print 21st. It appends the correct suffix onto a date. > >Reading and trying to implement a function that uses a dictionary. > >Not sure how to supply list into it to keep it brief and with default > >case of 'th'. > > > >This is my current code. > > > >def f(x): > > return { > > [1, 21, 31]: "st", > > [2, 22]: "nd", > > [3, 23]: "rd", > > }.get(x, "th") > > > > > >print(f(21)) > > > >I have an unhashable type list. Whats the best way to go? > > Skip has commented on lists being unhashable. We can elaborate on that > if you like. > > However, even if you went to tuples (which would let you construct the > dict you lay out above), there is another problem. > > You're looking up "x" in the dict. But the keys of the dict are not > integers, they are lists (or tuples) or integers, so they won't match. > > You _could_ do this: > > return { > 1: "st", 21: "st", 31: "st", > 2: "nd", 22: "nd", > 3: "rd", 23: "rd", > }.get(x, "th") > > which makes distinct entries for each integer value of interest. > > The conventional approach would normally be: > > if x in (1, 21, 31): > return "st" > if x in (2, 22): > return "nd" > if x in (3, 23): > return "rd" > return "th" > > While this works for a small number of choices, if you had a huge dict > with lots of possible values you could return to your > dict-keyed-on-tuples approach. You would need to try each tuple in turn: > > mapping = { > (1, 21, 31): "st", > (2, 22): "nd", > (3, 23): "rd", > } > for key, suffix in mapping.items(): > if x in key: > return suffix > return "th" > > However, for big dictionaries (with many keys) you loose a key > strength of dicts: constant time lookup. You can see the above code > (and the earlier "if" code) are rather linear, with run time going up > linearly with the number of keys. You're better with the int->string > single value dict version. > > Cheers, > Cameron Simpson It seems odd with C having switch that its cleaner and more efficient than python where we are having to implement our own functions to recreate switch everytime. Or perhaps use a 3rd party library like https://github.com/mikeckennedy/python-switch You have both given good options, it seems there are no standard approaches in this case. Cheers Sayth -- https://mail.python.org/mailman/listinfo/python-list From python at mrabarnett.plus.com Sat Feb 2 22:06:13 2019 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 3 Feb 2019 03:06:13 +0000 Subject: Implement C's Switch in Python 3 In-Reply-To: <004501d4bb6b$57b743b0$0725cb10$@verizon.net> References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> Message-ID: <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> On 2019-02-03 02:51, Avi Gross wrote: > I may be missing something, but the focus seems to be only on the rightmost > digit. You can get that with > I had the same thought, but came across a problem. "11st", "12nd", "13rd"? [snip] > > Output: > >>>> for day in range(1, 32): > print( nthSuffix(day)) > > 1st > 2nd > 3rd > 4th > 5th > 6th > 7th > 8th > 9th > 10th > 11st > 12nd > 13rd > 14th > 15th > 16th > 17th > 18th > 19th > 20th > 21st > 22nd > 23rd > 24th > 25th > 26th > 27th > 28th > 29th > 30th > 31st > [snip] From ikorot01 at gmail.com Sat Feb 2 10:16:07 2019 From: ikorot01 at gmail.com (Igor Korot) Date: Sat, 2 Feb 2019 09:16:07 -0600 Subject: Implement C's Switch in Python 3 In-Reply-To: <004501d4bb6b$57b743b0$0725cb10$@verizon.net> References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> Message-ID: Hi, On Sat, Feb 2, 2019 at 8:54 PM Avi Gross wrote: > > I may be missing something, but the focus seems to be only on the rightmost > digit. You can get that with > > > str(day)[-1] > or with > day % 10 > > Problem: print 1..31 using suffixes such as 1st, 2nd, 3rd, 4th ... > > So your dictionary needs entries for "1" or 1 and "2" or "2" and of course 3 > becoming 3rd. > > And, frankly, you don't even need a dictionary as a one-liner will do. > > Here is an example using a helper function: > > """ Use last digit to determine suffix """ > > def nthSuffix(day): > nth = day % 10 > suffix = "st" if nth == 1 else ("nd" if nth == 2 else ("rd" if nth == 3 > else "th")) > return str(day) + suffix > > > Output: > > >>> for day in range(1, 32): > print( nthSuffix(day)) > > 1st > 2nd > 3rd > 4th > 5th > 6th > 7th > 8th > 9th > 10th > 11st Shouldn't this be 11th? And then 12th and 13th? Thank you. > 12nd > 13rd > 14th > 15th > 16th > 17th > 18th > 19th > 20th > 21st > 22nd > 23rd > 24th > 25th > 26th > 27th > 28th > 29th > 30th > 31st > > -----Original Message----- > From: Python-list On > Behalf Of Sayth Renshaw > Sent: Saturday, February 2, 2019 8:53 PM > To: python-list at python.org > Subject: Re: Implement C's Switch in Python 3 > > > > >I am trying to convert a switch statement from C into Python. (why? > > >practising). > > > > > >This is the C code. > > > > > >printf("Dated this %d", day); > > > switch (day) { > > > case 1: case 21: case 31: > > > printf("st"); break; > > > case 2: case 22: > > > printf("nd"); break; > > > case 3: case 23: > > > printf("rd"); break; > > > default: printf("th"); break; > > > > > > } > > > printf(" day of "); > > > > > >#Premise if the use enter an int as the date 21 for example it would > print 21st. It appends the correct suffix onto a date. > > >Reading and trying to implement a function that uses a dictionary. > > >Not sure how to supply list into it to keep it brief and with default > > >case of 'th'. > > > > > >This is my current code. > > > > > >def f(x): > > > return { > > > [1, 21, 31]: "st", > > > [2, 22]: "nd", > > > [3, 23]: "rd", > > > }.get(x, "th") > > > > > > > > >print(f(21)) > > > > > >I have an unhashable type list. Whats the best way to go? > > > > Skip has commented on lists being unhashable. We can elaborate on that > > if you like. > > > > However, even if you went to tuples (which would let you construct the > > dict you lay out above), there is another problem. > > > > You're looking up "x" in the dict. But the keys of the dict are not > > integers, they are lists (or tuples) or integers, so they won't match. > > > > You _could_ do this: > > > > return { > > 1: "st", 21: "st", 31: "st", > > 2: "nd", 22: "nd", > > 3: "rd", 23: "rd", > > }.get(x, "th") > > > > which makes distinct entries for each integer value of interest. > > > > The conventional approach would normally be: > > > > if x in (1, 21, 31): > > return "st" > > if x in (2, 22): > > return "nd" > > if x in (3, 23): > > return "rd" > > return "th" > > > > While this works for a small number of choices, if you had a huge dict > > with lots of possible values you could return to your > > dict-keyed-on-tuples approach. You would need to try each tuple in turn: > > > > mapping = { > > (1, 21, 31): "st", > > (2, 22): "nd", > > (3, 23): "rd", > > } > > for key, suffix in mapping.items(): > > if x in key: > > return suffix > > return "th" > > > > However, for big dictionaries (with many keys) you loose a key > > strength of dicts: constant time lookup. You can see the above code > > (and the earlier "if" code) are rather linear, with run time going up > > linearly with the number of keys. You're better with the int->string > > single value dict version. > > > > Cheers, > > Cameron Simpson > > It seems odd with C having switch that its cleaner and more efficient than > python where we are having to implement our own functions to recreate switch > everytime. > > Or perhaps use a 3rd party library like > https://github.com/mikeckennedy/python-switch > > You have both given good options, it seems there are no standard approaches > in this case. > > Cheers > > Sayth > > -- > https://mail.python.org/mailman/listinfo/python-list > > -- > https://mail.python.org/mailman/listinfo/python-list From avigross at verizon.net Sat Feb 2 22:30:52 2019 From: avigross at verizon.net (Avi Gross) Date: Sat, 2 Feb 2019 22:30:52 -0500 Subject: Implement C's Switch in Python 3 In-Reply-To: <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> Message-ID: <004c01d4bb70$ddf15920$99d40b60$@verizon.net> Yes, you caught the usual flaw in the often irregular English language. The 11th, 12th and 13th do all begin with 1 so there is a simple fix in the function version by checking if day//10 is 1. Revised example: """ Use last digit to determine suffix handling teens well """ def nthSuffix(day): if (day // 10 == 1): suffix = "th" else: nth = day % 10 suffix = "st" if nth == 1 else ("nd" if nth == 2 else ("rd" if nth == 3 else "th")) return str(day) + suffix >>> [ nthSuffix(day) for day in range(1,32)] ['1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th', '11th', '12th', '13th', '14th', '15th', '16th', '17th', '18th', '19th', '20th', '21st', '22nd', '23rd', '24th', '25th', '26th', '27th', '28th', '29th', '30th', '31st'] Want a dictionary version? Use the above to make a full dictionary: >>> chooseFrom = { day : nthSuffix(day) for day in range(1,32)} >>> chooseFrom {1: '1st', 2: '2nd', 3: '3rd', 4: '4th', 5: '5th', 6: '6th', 7: '7th', 8: '8th', 9: '9th', 10: '10th', 11: '11th', 12: '12th', 13: '13th', 14: '14th', 15: '15th', 16: '16th', 17: '17th', 18: '18th', 19: '19th', 20: '20th', 21: '21st', 22: '22nd', 23: '23rd', 24: '24th', 25: '25th', 26: '26th', 27: '27th', 28: '28th', 29: '29th', 30: '30th', 31: '31st'} >>> chooseFrom[1] '1st' >>> chooseFrom[11] '11th' >>> chooseFrom[21] '21st' -----Original Message----- From: Python-list On Behalf Of MRAB Sent: Saturday, February 2, 2019 10:06 PM To: python-list at python.org Subject: Re: Implement C's Switch in Python 3 On 2019-02-03 02:51, Avi Gross wrote: > I may be missing something, but the focus seems to be only on the > rightmost digit. You can get that with > I had the same thought, but came across a problem. "11st", "12nd", "13rd"? [snip] > > Output: > >>>> for day in range(1, 32): > print( nthSuffix(day)) > > 1st > 2nd > 3rd > 4th > 5th > 6th > 7th > 8th > 9th > 10th > 11st > 12nd > 13rd > 14th > 15th > 16th > 17th > 18th > 19th > 20th > 21st > 22nd > 23rd > 24th > 25th > 26th > 27th > 28th > 29th > 30th > 31st > [snip] -- https://mail.python.org/mailman/listinfo/python-list From flebber.crue at gmail.com Sun Feb 3 01:55:23 2019 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Sat, 2 Feb 2019 22:55:23 -0800 (PST) Subject: Implement C's Switch in Python 3 In-Reply-To: References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> Message-ID: <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> On Sunday, 3 February 2019 14:31:14 UTC+11, Avi Gross wrote: > Yes, you caught the usual flaw in the often irregular English language. > > The 11th, 12th and 13th do all begin with 1 so there is a simple fix in the > function version by checking if day//10 is 1. > > Revised example: > > """ Use last digit to determine suffix handling teens well """ > > def nthSuffix(day): > if (day // 10 == 1): > suffix = "th" > else: > nth = day % 10 > suffix = "st" if nth == 1 else ("nd" if nth == 2 else ("rd" if nth > == 3 else "th")) > > return str(day) + suffix > > >>> [ nthSuffix(day) for day in range(1,32)] > ['1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th', > '11th', '12th', '13th', '14th', '15th', '16th', '17th', '18th', '19th', > '20th', '21st', '22nd', '23rd', '24th', '25th', '26th', '27th', '28th', > '29th', '30th', '31st'] > > Want a dictionary version? Use the above to make a full dictionary: > > >>> chooseFrom = { day : nthSuffix(day) for day in range(1,32)} > >>> chooseFrom > {1: '1st', 2: '2nd', 3: '3rd', 4: '4th', 5: '5th', 6: '6th', 7: '7th', 8: > '8th', 9: '9th', 10: '10th', 11: '11th', 12: '12th', 13: '13th', 14: '14th', > 15: '15th', 16: '16th', 17: '17th', 18: '18th', 19: '19th', 20: '20th', 21: > '21st', 22: '22nd', 23: '23rd', 24: '24th', 25: '25th', 26: '26th', 27: > '27th', 28: '28th', 29: '29th', 30: '30th', 31: '31st'} > >>> chooseFrom[1] > '1st' > >>> chooseFrom[11] > '11th' > >>> chooseFrom[21] > '21st' > > Subject: Re: Implement C's Switch in Python 3 > > On 2019-02-03 02:51, Avi Gross wrote: > > I may be missing something, but the focus seems to be only on the > > rightmost digit. You can get that with > > > I had the same thought, but came across a problem. "11st", "12nd", "13rd"? > > [snip] > > > > Output: > > > >>>> for day in range(1, 32): > > print( nthSuffix(day)) > > > > 1st > > 2nd > > 3rd > > 4th > > 5th > > 6th > > 7th > > 8th > > 9th > > 10th > > 11st > > 12nd > > 13rd > > 14th > > 15th > > 16th > > 17th > > 18th > > 19th > > 20th > > 21st > > 22nd > > 23rd > > 24th > > 25th > > 26th > > 27th > > 28th > > 29th > > 30th > > 31st > > > [snip] > -- I do like this. Want a dictionary version? Use the above to make a full dictionary: >>> chooseFrom = { day : nthSuffix(day) for day in range(1,32)} >>> chooseFrom {1: '1st', 2: '2nd', 3: '3rd', 4: '4th', 5: '5th', 6: '6th', 7: '7th', 8: '8th', 9: '9th', 10: '10th', 11: '11th', 12: '12th', 13: '13th', 14: '14th', 15: '15th', 16: '16th', 17: '17th', 18: '18th', 19: '19th', 20: '20th', 21: '21st', 22: '22nd', 23: '23rd', 24: '24th', 25: '25th', 26: '26th', 27: '27th', 28: '28th', 29: '29th', 30: '30th', 31: '31st'} >>> chooseFrom[1] '1st' >>> chooseFrom[11] '11th' >>> chooseFrom[21] '21st' Not having a default case as in switch forced you to write out all possible combinations. I think the intent and readbility of switch statements is a bit nicer. Cheers Sayth From stefan_ml at behnel.de Sun Feb 3 03:29:28 2019 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 3 Feb 2019 09:29:28 +0100 Subject: Implement C's Switch in Python 3 In-Reply-To: References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> Message-ID: Chris Angelico schrieb am 03.02.19 um 02:23: > Of course, you can also precompute this: > > day_ordinal = mapper( > [1, 21, 31], "st", > [2, 22], "nd", > [3, 23], "rd", > ) > def f(x): return day_ordinal.get(x, "th") ? in which case I would also 'precompute' the ".get" and give the resulting callable a properly readable name all-together: find_special_day_ordinal = mapper( [1, 21, 31], "st", [2, 22], "nd", [3, 23], "rd", ).get print(find_special_day_ordinal(x, "th")) Stefan From PythonList at DancesWithMice.info Sun Feb 3 03:32:43 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Sun, 3 Feb 2019 21:32:43 +1300 Subject: Implement C's Switch in Python 3 In-Reply-To: <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> Message-ID: <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> When a client demanded his way on this issue, the action we took was, as below, to create a list (called ordinal) and to use the dd (day) value as an index. >>>>> [ nthSuffix(day) for day in range(1,32)] >> ['1st', '2nd', '3rd', '4th', '5th', '6th', '7th', '8th', '9th', '10th', >> '11th', '12th', '13th', '14th', '15th', '16th', '17th', '18th', '19th', >> '20th', '21st', '22nd', '23rd', '24th', '25th', '26th', '27th', '28th', >> '29th', '30th', '31st'] I'm not sure if employing a dict with hashed retrieval would be any more efficient than indexing into a list. > Not having a default case as in switch forced you to write out all possible combinations. > > I think the intent and readbility of switch statements is a bit nicer. Perhaps not. A list look-up is self-documenting, and the fact that every choice is covered reduces error-risk. The criticism above, "demanded", is justified by the international standard for dates (ISO 8601). This would normally see us coding "2019-02-03". The arrangement of larger to ever more precise time-units is very useful in databases and applications such as file-names, because it sequences logically. However, that is not the way 'normal people' like to write their dates. The trouble with 'common practice' though, is that it is localised. There is great confusion between the way different cultures (even staying within the English-speaking world) express dates. Is 3/2/2019 referring to "3rd February" or "March 2nd"? Not part of the standard, but given the differences between US and European commonly-used date formats, I used to insist upon dd-mmm-yy or ccyy, thus 3-Feb-2019. Before handy PSL date utilities, this raised the need for another list - of month names/abbreviations. Why lists? The other advantage is realised when we move out of English. It is easy to have multiple lists - one for each language (assuming Gregorian calendar - given that other calendar systems are quite another discussion!) and thus select by date and by location. Now back to ordinal dates - the "st", "th", etc suffixes only work in English. You'd need another list (but no great coding complexity) to cope with a second, third, ... language! (ACK: the OP didn't specify such extensive needs) -- Regards =dn From rosuav at gmail.com Sun Feb 3 03:45:14 2019 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 3 Feb 2019 19:45:14 +1100 Subject: Implement C's Switch in Python 3 In-Reply-To: <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> Message-ID: On Sun, Feb 3, 2019 at 7:40 PM DL Neil wrote: > This would normally see us coding "2019-02-03". The arrangement of > larger to ever more precise time-units is very useful in databases and > applications such as file-names, because it sequences logically. > > However, that is not the way 'normal people' like to write their dates. > The trouble with 'common practice' though, is that it is localised. > There is great confusion between the way different cultures (even > staying within the English-speaking world) express dates. Is 3/2/2019 > referring to "3rd February" or "March 2nd"? Which is why I always write dates in sorted format, usually eschewing delimiters: //CJA 20160511: Is this still happening? I don't remember seeing it in three parts of forever. That said, I am aware that I am not in any way a "normal person". Using month names as per your other example is probably a fair compromise with other humans. ChrisA From PythonList at DancesWithMice.info Sun Feb 3 04:08:52 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Sun, 3 Feb 2019 22:08:52 +1300 Subject: Implement C's Switch in Python 3 In-Reply-To: References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> Message-ID: <7ecb5f4a-3013-b3a5-b184-9e3b647591df@DancesWithMice.info> Chris, On 3/02/19 9:45 PM, Chris Angelico wrote: > On Sun, Feb 3, 2019 at 7:40 PM DL Neil wrote: >> This would normally see us coding "2019-02-03". The arrangement of >> larger to ever more precise time-units is very useful in databases and >> applications such as file-names, because it sequences logically. >> >> However, that is not the way 'normal people' like to write their dates. >> The trouble with 'common practice' though, is that it is localised. >> There is great confusion between the way different cultures (even >> staying within the English-speaking world) express dates. Is 3/2/2019 >> referring to "3rd February" or "March 2nd"? > > Which is why I always write dates in sorted format, usually eschewing > delimiters: > > //CJA 20160511: Is this still happening? I don't remember seeing it in > three parts of forever. Sure is. It is an acceptable alternative under the ISO standard. Some would say it is more sensible to use when storing data because it removes the dash/hyphen separators in exchange for implying the fixed-format. (more bytes/characters saved if extend to include the time) I'm not going there - recalling folk from these memory-is-cheap times being less able to understand why we used to save 'expensive' storage space by using yy-years (instead of ccyy) and thus 'causing' "the millennium bug" aka Y2K! I wouldn't use it in a 'visible' situation though, eg a fileNM. Yes, it is shorter, but as my eyes age (they are already older than my teeth!), I find it much slower to decode than reading the same with embedded separators! > That said, I am aware that I am not in any way a "normal person". > Using month names as per your other example is probably a fair > compromise with other humans. There's normal and there's normal - like it's tomato or tomato? -- Regards =dn From rosuav at gmail.com Sun Feb 3 04:16:45 2019 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 3 Feb 2019 20:16:45 +1100 Subject: Implement C's Switch in Python 3 In-Reply-To: <7ecb5f4a-3013-b3a5-b184-9e3b647591df@DancesWithMice.info> References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> <7ecb5f4a-3013-b3a5-b184-9e3b647591df@DancesWithMice.info> Message-ID: On Sun, Feb 3, 2019 at 8:09 PM DL Neil wrote: > > On 3/02/19 9:45 PM, Chris Angelico wrote: > > Which is why I always write dates in sorted format, usually eschewing > > delimiters: > > > > //CJA 20160511: Is this still happening? I don't remember seeing it in > > three parts of forever. > > Sure is. It is an acceptable alternative under the ISO standard. Not sure if you're responding to the content of the comment there; that was just one that I lifted straight from some of my source code. It's part of a bug note regarding something that happens *extremely* sporadically, and all I could do was to write down everything I knew and then wait for the next occurrence. Back in May of 2016, I added that comment, and it's still there to this day, because I haven't actually proven the bug gone. > Some would say it is more sensible to use when storing data because it > removes the dash/hyphen separators in exchange for implying the > fixed-format. (more bytes/characters saved if extend to include the time) > > I'm not going there - recalling folk from these memory-is-cheap times > being less able to understand why we used to save 'expensive' storage > space by using yy-years (instead of ccyy) and thus 'causing' "the > millennium bug" aka Y2K! Skipping the delimiter isn't about saving space, it's about consistency. If I say "non-delimited sorted date", you can almost certainly write out a character-for-character identical date - handy if you want to search a bunch of files, for instance. Having delimiters leaves people free to dispute whether they should be slashes, hyphens, dots, or maybe something else. > I wouldn't use it in a 'visible' situation though, eg a fileNM. Yes, it > is shorter, but as my eyes age (they are already older than my teeth!), Guess your teeth better work on catching up... > I find it much slower to decode than reading the same with embedded > separators! Sure. I mainly use it in contexts where the most important information is simply "that's a date", rather than actually caring what the date *is*. > > That said, I am aware that I am not in any way a "normal person". > > Using month names as per your other example is probably a fair > > compromise with other humans. > > There's normal and there's normal - like it's tomato or tomato? > I dunno. I'm the kind of normal that likes tomatoes (not to be confused with tomatoes). Does that help? ChrisA From frank at chagford.com Sun Feb 3 02:14:46 2019 From: frank at chagford.com (Frank Millman) Date: Sun, 3 Feb 2019 09:14:46 +0200 Subject: Implement C's Switch in Python 3 In-Reply-To: <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> Message-ID: "Sayth Renshaw" wrote in message news:73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7 at googlegroups.com... >>> chooseFrom = { day : nthSuffix(day) for day in range(1,32)} >>> chooseFrom {1: '1st', 2: '2nd', 3: '3rd', 4: '4th', 5: '5th', 6: '6th', 7: '7th', 8: '8th', 9: '9th', 10: '10th', 11: '11th', 12: '12th', 13: '13th', 14: '14th', 15: '15th', 16: '16th', 17: '17th', 18: '18th', 19: '19th', 20: '20th', 21: '21st', 22: '22nd', 23: '23rd', 24: '24th', 25: '25th', 26: '26th', 27: '27th', 28: '28th', 29: '29th', 30: '30th', 31: '31st'} >>> chooseFrom[1] '1st' >>> chooseFrom[11] '11th' >>> chooseFrom[21] '21st' > Not having a default case as in switch forced you to write out all > possible combinations. > I think the intent and readbility of switch statements is a bit nicer. I have not been following this thread in detail, but how about this - >>> choose = {1: 'st', 2: 'nd', 3: 'rd', 21: 'st', 22: 'nd', 23: 'rd', 31: >>> 'st'} >>> for x in range(1, 32): ... print('{}{}'.format(x, choose.get(x, 'th')), end = ' ') ... 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 26th 27th 28th 29th 30th 31st >>> Frank Millman From avigross at verizon.net Sun Feb 3 08:13:07 2019 From: avigross at verizon.net (Avi Gross) Date: Sun, 3 Feb 2019 08:13:07 -0500 Subject: Implement C's Switch in Python 3 In-Reply-To: References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> <7ecb5f4a-3013-b3a5-b184-9e3b647591df@DancesWithMice.info> Message-ID: <002701d4bbc2$3449fa60$9cddef20$@verizon.net> The discussion strictly sets a limit of 31 for the largest number of days in a month and asks for suffixes used to make ordinal numbers like 31st. But in reality, you can go to 99th and beyond for other purposes albeit the pattern for making 101 and on seems to repeat. The last algorithm I wrote would work up to 99 in terms of the function I wrote. To make it work higher, you would simply start by taking the original number mod 100 before continuing and then use the original number at the end. But if the goal is to make a list or dictionary out of it, it gets a tad large for the 8423rd and not manageable too far above. If you will be using whatever technique just once, precomputing a list or dict is extra effort. For MANY uses in the same program, it makes sense to make a more static data structure with rapid access. So what if you only compile lower numbers into the structure but call the function version for higher ones? From diego.avesani at gmail.com Sun Feb 3 10:56:30 2019 From: diego.avesani at gmail.com (diego.avesani at gmail.com) Date: Sun, 3 Feb 2019 07:56:30 -0800 (PST) Subject: mask and proper index Message-ID: <2b40907c-f883-4031-a2f5-15f50c9827b6@googlegroups.com> Dear all, I am trying to apply a mask to my dataframe: mask = (df['datatime'] > start_date) & (df['datatime'] <= end_date) df = df.loc[mask] It seems to work pretty well. After that I crate the cumulative of its element as: PP_cumPP = np.cumsum(df[PP_station]) However, I am not able to compute PP_cumPP last element. Indeed, when I do len(PP_cumPP) I get 8783 and when I try to do: PP_cumPP[len(PP_cumPP)-1] I get an error. What I am doing wrong? Thanks a lot for any kind of help Diedro From __peter__ at web.de Sun Feb 3 13:18:53 2019 From: __peter__ at web.de (Peter Otten) Date: Sun, 03 Feb 2019 19:18:53 +0100 Subject: mask and proper index References: <2b40907c-f883-4031-a2f5-15f50c9827b6@googlegroups.com> Message-ID: diego.avesani at gmail.com wrote: > Dear all, > > I am trying to apply a mask to my dataframe: > > mask = (df['datatime'] > start_date) & (df['datatime'] <= end_date) > df = df.loc[mask] > > > It seems to work pretty well. > > After that I crate the cumulative of its element as: > > > PP_cumPP = np.cumsum(df[PP_station]) > > > However, I am not able to compute PP_cumPP last element. Indeed, when I do > > len(PP_cumPP) > > I get > > 8783 > > and when I try to do: > > PP_cumPP[len(PP_cumPP)-1] > > I get an error. The ultimate problem description ;) You can certainly do better than that. Please do always try to make your example self-contained so that a reader can run it and see the same error as you do. At the very least cut and paste the traceback and the error message you are getting. > What I am doing wrong? My steps to reproduce what is probably your problem: >>> df = pd.DataFrame([[1],[2],[3]], columns=["foo"]) >>> odd = df.loc[df["foo"] & 1] >>> odd foo 0 1 2 3 [2 rows x 1 columns] >>> odd["foo"][1] Traceback (most recent call last): [snip] KeyError: 1 OK, let's try something else: >>> odd["foo"][2] 3 It looks like you have to use the original indices. But google sure can find a way to get rid of these: >>> odd = odd.reset_index(drop=True) >>> odd["foo"][1] 3 > Thanks a lot for any kind of help > > Diedro From PythonList at DancesWithMice.info Sun Feb 3 15:35:41 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Mon, 4 Feb 2019 09:35:41 +1300 Subject: Implement C's Switch in Python 3 In-Reply-To: References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> <7ecb5f4a-3013-b3a5-b184-9e3b647591df@DancesWithMice.info> Message-ID: <1a8ce044-889d-8ac1-e0b7-b0bdc96c8e71@DancesWithMice.info> On 3/02/19 10:16 PM, Chris Angelico wrote: > On Sun, Feb 3, 2019 at 8:09 PM DL Neil wrote: >> On 3/02/19 9:45 PM, Chris Angelico wrote: >>> Which is why I always write dates in sorted format, usually eschewing >>> delimiters: >>> //CJA 20160511: Is this still happening? I don't remember seeing it in >>> three parts of forever. >> Sure is. It is an acceptable alternative under the ISO standard. > > Not sure if you're responding to the content of the comment there; Comment? I don't see no #, ''', or """! (am teasing) [ccyymmdd cf ccyy-mm-dd] >> Some would say it is more sensible to use when storing data because it >> removes the dash/hyphen separators in exchange for implying the >> fixed-format. (more bytes/characters saved if extend to include the time) >> >> I'm not going there - recalling folk from these memory-is-cheap times >> being less able to understand why we used to save 'expensive' storage >> space by using yy-years (instead of ccyy) and thus 'causing' "the >> millennium bug" aka Y2K! > > Skipping the delimiter isn't about saving space, it's about > consistency. If I say "non-delimited sorted date", you can almost > certainly write out a character-for-character identical date - handy > if you want to search a bunch of files, for instance. Having > delimiters leaves people free to dispute whether they should be > slashes, hyphens, dots, or maybe something else. This logic indisputable. However, the whole purpose of an ISO standard is to remove "dispute", locally and internationally! Thus, if not sufficiently-well stated earlier, the standard is actually for information interchange purposes. >> I find it much slower to decode than reading the same with embedded >> separators! > Sure. I mainly use it in contexts where the most important information > is simply "that's a date", rather than actually caring what the date > *is*. [paras re-ordered] +1 In case other readers are following-along-at-home, and the (above) purpose of the standard was insufficiently obvious, I did a bit of review 'homework': - ISO standards are still not $free - a good write-up from the ISO appears on the Wayback machine at https://web.archive.org/web/20110614235056/http://www.iso.org/iso/support/faqs/faqs_widely_used_standards/widely_used_standards_other/date_and_time_format.htm - the above makes the point about "interchange" and offers similar examples of date-confusion to those 'here'/earlier - mention is made of formats including/excluding delimiters (I haven't been able to check this, but can't find any evidence that separators other than "-" are allowed (in dates) ) - in lieu of the ISO text, those of us working over the Internet will turn to RFC 3339 - this is a slight simplification of the ISO standard - the ABNF appendix *requires* a dash/hyphen as (date) separator - Markus Kuhn at Cambridge (British university) provides a readable and thought-provoking summary at https://www.cl.cam.ac.uk/~mgk25/iso-time.html - uses dashes/hyphens - discusses international considerations (I've just learned that the Chinese date notation preceded the ISO order, as did the conventions of a number of other countries) - supports the compact/'no debate' format "The hyphens can be omitted if compactness of the representation is more important than human readability" (as well as caring for my fading vision) >> I wouldn't use it in a 'visible' situation though, eg a fileNM. Yes, it >> is shorter, but as my eyes age (they are already older than my teeth!), > Guess your teeth better work on catching up... Can't put them under the pressure of thinking it is a race - they could decide to drop out! >>> That said, I am aware that I am not in any way a "normal person". >>> Using month names as per your other example is probably a fair >>> compromise with other humans. In this life, one does have to make allowances... >> There's normal and there's normal - like it's tomato or tomato? > I dunno. I'm the kind of normal that likes tomatoes (not to be > confused with tomatoes). Does that help? If you like tomatoes, and tomatoes are fruit, do you (normally) chug tomato sauce (ketchup) as if it is fruit juice? -- Regards =dn From bob at mellowood.ca Sun Feb 3 16:01:24 2019 From: bob at mellowood.ca (Bob van der Poel) Date: Sun, 3 Feb 2019 14:01:24 -0700 Subject: Implement C's Switch in Python 3 In-Reply-To: <1a8ce044-889d-8ac1-e0b7-b0bdc96c8e71@DancesWithMice.info> References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> <7ecb5f4a-3013-b3a5-b184-9e3b647591df@DancesWithMice.info> <1a8ce044-889d-8ac1-e0b7-b0bdc96c8e71@DancesWithMice.info> Message-ID: On Sun, Feb 3, 2019 at 1:35 PM DL Neil wrote: > On 3/02/19 10:16 PM, Chris Angelico wrote: > > On Sun, Feb 3, 2019 at 8:09 PM DL Neil > wrote: > >> On 3/02/19 9:45 PM, Chris Angelico wrote: > >>> Which is why I always write dates in sorted format, usually eschewing > >>> delimiters: > >>> //CJA 20160511: Is this still happening? I don't remember seeing it in > >>> three parts of forever. > >> Sure is. It is an acceptable alternative under the ISO standard. > > > > Not sure if you're responding to the content of the comment there; > > Comment? I don't see no #, ''', or """! > (am teasing) > > > [ccyymmdd cf ccyy-mm-dd] > >> Some would say it is more sensible to use when storing data because it > >> removes the dash/hyphen separators in exchange for implying the > >> fixed-format. (more bytes/characters saved if extend to include the > time) > >> > >> I'm not going there - recalling folk from these memory-is-cheap times > >> being less able to understand why we used to save 'expensive' storage > >> space by using yy-years (instead of ccyy) and thus 'causing' "the > >> millennium bug" aka Y2K! > > > > Skipping the delimiter isn't about saving space, it's about > > consistency. If I say "non-delimited sorted date", you can almost > > certainly write out a character-for-character identical date - handy > > if you want to search a bunch of files, for instance. Having > > delimiters leaves people free to dispute whether they should be > > slashes, hyphens, dots, or maybe something else. > > This logic indisputable. > > However, the whole purpose of an ISO standard is to remove "dispute", > locally and internationally! Thus, if not sufficiently-well stated > earlier, the standard is actually for information interchange purposes. > > > >> I find it much slower to decode than reading the same with embedded > >> separators! > > Sure. I mainly use it in contexts where the most important information > > is simply "that's a date", rather than actually caring what the date > > *is*. > [paras re-ordered] > > +1 > > > In case other readers are following-along-at-home, and the (above) > purpose of the standard was insufficiently obvious, I did a bit of > review 'homework': > > - ISO standards are still not $free > > - a good write-up from the ISO appears on the Wayback machine at > > https://web.archive.org/web/20110614235056/http://www.iso.org/iso/support/faqs/faqs_widely_used_standards/widely_used_standards_other/date_and_time_format.htm > - the above makes the point about "interchange" and offers similar > examples of date-confusion to those 'here'/earlier > - mention is made of formats including/excluding delimiters > (I haven't been able to check this, but can't find any evidence that > separators other than "-" are allowed (in dates) ) > > - in lieu of the ISO text, those of us working over the Internet will > turn to RFC 3339 > - this is a slight simplification of the ISO standard > - the ABNF appendix *requires* a dash/hyphen as (date) separator > > - Markus Kuhn at Cambridge (British university) provides a readable and > thought-provoking summary at https://www.cl.cam.ac.uk/~mgk25/iso-time.html > - uses dashes/hyphens > - discusses international considerations (I've just learned that the > Chinese date notation preceded the ISO order, as did the conventions of > a number of other countries) > - supports the compact/'no debate' format "The hyphens can be omitted if > compactness of the representation is more important than human > readability" (as well as caring for my fading vision) > > > >> I wouldn't use it in a 'visible' situation though, eg a fileNM. Yes, it > >> is shorter, but as my eyes age (they are already older than my teeth!), > > Guess your teeth better work on catching up... > > Can't put them under the pressure of thinking it is a race - they could > decide to drop out! > > > >>> That said, I am aware that I am not in any way a "normal person". > >>> Using month names as per your other example is probably a fair > >>> compromise with other humans. > > In this life, one does have to make allowances... > > > >> There's normal and there's normal - like it's tomato or tomato? > > I dunno. I'm the kind of normal that likes tomatoes (not to be > > confused with tomatoes). Does that help? > > If you like tomatoes, and tomatoes are fruit, do you (normally) chug > tomato sauce (ketchup) as if it is fruit juice? > I'm surprised that no one has yet addressed the year 10000 problem. Hopefully we're doing numeric, not alpha sorts on the stuff before the 1st '-'. And, the compact versions will really screw up :). -- **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars **** Bob van der Poel ** Wynndel, British Columbia, CANADA ** EMAIL: bob at mellowood.ca WWW: http://www.mellowood.ca From rosuav at gmail.com Sun Feb 3 16:02:58 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 4 Feb 2019 08:02:58 +1100 Subject: Implement C's Switch in Python 3 In-Reply-To: <1a8ce044-889d-8ac1-e0b7-b0bdc96c8e71@DancesWithMice.info> References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> <7ecb5f4a-3013-b3a5-b184-9e3b647591df@DancesWithMice.info> <1a8ce044-889d-8ac1-e0b7-b0bdc96c8e71@DancesWithMice.info> Message-ID: On Mon, Feb 4, 2019 at 7:35 AM DL Neil wrote: > > On 3/02/19 10:16 PM, Chris Angelico wrote: > >> There's normal and there's normal - like it's tomato or tomato? > > I dunno. I'm the kind of normal that likes tomatoes (not to be > > confused with tomatoes). Does that help? > > If you like tomatoes, and tomatoes are fruit, do you (normally) chug > tomato sauce (ketchup) as if it is fruit juice? > I'm not one for drinking fruit juice, but I would definitely recommend putting tom sauce on your fruit salad. ChrisA From auriocus at gmx.de Sun Feb 3 16:00:29 2019 From: auriocus at gmx.de (Christian Gollwitzer) Date: Sun, 3 Feb 2019 22:00:29 +0100 Subject: Implement C's Switch in Python 3 In-Reply-To: References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> Message-ID: Am 03.02.19 um 09:32 schrieb DL Neil: > Now back to ordinal dates - the "st", "th", etc suffixes only work in > English. You'd need another list (but no great coding complexity) to > cope with a second, third, ... language! Only for some languages. In other languages there can be, for example, cases (inflections). Then the suffix not only depends on the number, but also on the case, which is governed by e.g. a preposition or the use in the sentence. Slavic languages have 6 or 7 cases. Of course you can make the list two-dimensional to cover that, but then another language will appear which has yet another different thing.... Basically you'll need to rewrite the whole thing when going to a completely different language. Christian From rosuav at gmail.com Sun Feb 3 16:15:03 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 4 Feb 2019 08:15:03 +1100 Subject: Implement C's Switch in Python 3 In-Reply-To: References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> <7ecb5f4a-3013-b3a5-b184-9e3b647591df@DancesWithMice.info> <1a8ce044-889d-8ac1-e0b7-b0bdc96c8e71@DancesWithMice.info> Message-ID: On Mon, Feb 4, 2019 at 8:02 AM Bob van der Poel wrote: > > I'm surprised that no one has yet addressed the year 10000 problem. Hopefully we're doing numeric, not alpha sorts on the stuff before the 1st '-'. And, the compact versions will really screw up :). > Compact versions? You mean non-delimited dates? They sort just fine numerically: 20190204 99991231 100000101 They also sort just fine if you add a two-digit serial number: 2017081702 2017120801 2019011300 and while it's true that this format will overflow in two thousand years if stored in a 32-bit number, it's fine for arbitrary future dates if stored in bignums. ChrisA From bob at mellowood.ca Sun Feb 3 17:02:56 2019 From: bob at mellowood.ca (Bob van der Poel) Date: Sun, 3 Feb 2019 15:02:56 -0700 Subject: Implement C's Switch in Python 3 In-Reply-To: References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> <7ecb5f4a-3013-b3a5-b184-9e3b647591df@DancesWithMice.info> <1a8ce044-889d-8ac1-e0b7-b0bdc96c8e71@DancesWithMice.info> Message-ID: On Sun, Feb 3, 2019 at 2:15 PM Chris Angelico wrote: > On Mon, Feb 4, 2019 at 8:02 AM Bob van der Poel wrote: > > > > I'm surprised that no one has yet addressed the year 10000 problem. > Hopefully we're doing numeric, not alpha sorts on the stuff before the 1st > '-'. And, the compact versions will really screw up :). > > > > Compact versions? You mean non-delimited dates? They sort just fine > numerically: > > 20190204 > 99991231 > 100000101 > > They also sort just fine if you add a two-digit serial number: > > 2017081702 > 2017120801 > 2019011300 > > and while it's true that this format will overflow in two thousand > years if stored in a 32-bit number, it's fine for arbitrary future > dates if stored in bignums. > Yes, 99991231 100000101 will wort properly if numeric. But, if we use the ISO recommended 9999-12-31 10000-01-01 then we'd most likely do an alpha sort ... opps, you're right! That works fine as well. Guess there is no year 10000 problem :) -- **** Listen to my FREE CD at http://www.mellowood.ca/music/cedars **** Bob van der Poel ** Wynndel, British Columbia, CANADA ** EMAIL: bob at mellowood.ca WWW: http://www.mellowood.ca From avigross at verizon.net Sun Feb 3 18:40:42 2019 From: avigross at verizon.net (Avi Gross) Date: Sun, 3 Feb 2019 18:40:42 -0500 Subject: Switch function Message-ID: <00e201d4bc19$e065f6d0$a131e470$@verizon.net> Message asking about a fairly simple way to implement a switch in python as per the ongoing discussion. I wrote a function that might emulate a fairly simple general use of switch. A function would take N+2 arguments of the form: 1: something to switch based on 2,3: something to match to the first and if matched, return the second. 4,5: another pair of match and response . N: an OPTIONAL singleton to serve as a default. The function will be shown next but in English, it accepts the one to search for and pairs of key/value to compare and if matched return. As soon as anything matches, return it. If all matches fail and if there is one argument left, return that. Else, return None. Now this should work well only with relatively fixed objects that are evaluated whether used or not. Again, just a suggestion to discuss, not necessarily something to implement although this seems trivial enough that something like it is likely out there. def switcheroo(*args): theOne, *rest = args while len(rest) > 1: matching, returning, *rest = rest if theOne == matching: return returning if rest: # default return rest[0] else: return None I used python 3.7.1 so note the *rest notation may not work on very old versions. You can use other methods like popping off a list though. Now for the specific case of days 1:31, here is how you can call it: >>> switcheroo(3, 1, "st", 2, "nd", 3, "rd", 21, "st", 31, "st", 22, "nd", 23, "rd", "th" ) 'rd' With no default, you get an invisible None. Since normal people don't like to type that way, here is how you might use it. >>> pairs = [1,"st", 2,"nd", 3,"rd", 21,"st", 31,"st", 22,"nd", 22,"nd", 23,"rd"] >>> switcheroo(2, *pairs, "th") 'nd' >>> switcheroo(5, *pairs, "th") 'th' You can make many variations on this theme including using dictionaries and the **kwargs notation, or moving the default up near the front and having a None placeholder. As far as I know, nothing stops the things being matched or returned from being objects of any kind or functions. Final note. I repeat. This method has all arguments evaluated before the function is called even if they are not the ones being matched. There is no delayed lazy evaluation as in R. See the following examples: >>> switcheroo(1, -int((math.e**(math.pi*complex(0,1))).real), "st", 2, "nd", 1+2, "rd", 21, "st", 31, "st", 2*11, "nd", 23, "rd", "th" ) 'st' >>> switcheroo(23, -int((math.e**(math.pi*complex(0,1))).real), "st", 2, "nd", 1+2, "rd", 21, "st", 31, "st", 2*11, "nd", 23, "rd", "th" ) 'rd' Bottom line, does anyone bother using anything like this? It is actually a bunch of hidden IF statements matched in order but may meet many needs. From avigross at verizon.net Sun Feb 3 18:52:14 2019 From: avigross at verizon.net (Avi Gross) Date: Sun, 3 Feb 2019 18:52:14 -0500 Subject: Implement C's Switch in Python 3 In-Reply-To: References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> <7ecb5f4a-3013-b3a5-b184-9e3b647591df@DancesWithMice.info> <1a8ce044-889d-8ac1-e0b7-b0bdc96c8e71@DancesWithMice.info> Message-ID: <00e701d4bc1b$7cfd7030$76f85090$@verizon.net> Comment at end: -----Original Message----- From: Python-list On Behalf Of Bob van der Poel Sent: Sunday, February 3, 2019 4:01 PM To: DL Neil Cc: Python Subject: Re: Implement C's Switch in Python 3 I'm surprised that no one has yet addressed the year 10000 problem. Hopefully we're doing numeric, not alpha sorts on the stuff before the 1st '-'. And, the compact versions will really screw up :). [[The obvious solution is to list all dates in the sortable format like this for January 2nd, 2020: 0Q20200102 Without the 0Q we might just assume it was written in base 3, or trinary. OK, turning sarcasm mode off. It is very bad form to have ambiguous compressed formats. Even if you include a slash or minus sign or period or the delimiter of your choice, I sometimes see this: 01/02/2020 And I wonder if it is meant to be January 2nd or February 1st. Clearly 30/01/2020 cannot be the 30th month so it must be an alternate format. And, years from now, we may get a year that might confuse us more if compressed like: 21121221 Is that 2112 as the year an Dec 21? In the interest of clarity, how is this different than including a time zone alongside a time value? Maybe '0Q" is silly, but a prefix or suffix or even a new delimiter might be a good idea. Are any available? 2020|12|02 ??? The end]] From rosuav at gmail.com Sun Feb 3 19:08:41 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 4 Feb 2019 11:08:41 +1100 Subject: Implement C's Switch in Python 3 In-Reply-To: <00e701d4bc1b$7cfd7030$76f85090$@verizon.net> References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> <7ecb5f4a-3013-b3a5-b184-9e3b647591df@DancesWithMice.info> <1a8ce044-889d-8ac1-e0b7-b0bdc96c8e71@DancesWithMice.info> <00e701d4bc1b$7cfd7030$76f85090$@verizon.net> Message-ID: On Mon, Feb 4, 2019 at 10:53 AM Avi Gross wrote: > It is very bad form to have ambiguous compressed formats. Even if you include a slash or minus sign or period or the delimiter of your choice, I sometimes see this: > > 01/02/2020 > > And I wonder if it is meant to be January 2nd or February 1st. > > Clearly 30/01/2020 cannot be the 30th month so it must be an alternate format. And, years from now, we may get a year that might confuse us more if compressed like: The only sane way is to have the components in some sort of reasonable order. Day-Month-Year is better than Month-Day-Year, though Year-Month-Day is better than both. > 21121221 > > Is that 2112 as the year an Dec 21? Sure it is. What else would it be? Without delimiters, the only recognized format is year-month-day. > In the interest of clarity, how is this different than including a time zone alongside a time value? Maybe '0Q" is silly, but a prefix or suffix or even a new delimiter might be a good idea. Are any available? > Attach a "Z" at the very end (after the time, which you'll probably have if you care about timezones). ISO 8601 format. If you need to attach some *other* time zone (which should be rare - ONLY do this if you absolutely cannot translate to UTC), write the date and time, then add a space, and the IANA tzdata in the "Region/City" format eg "Australia/Melbourne" or "America/New_York". Avoid the abbreviations like "EST" as they are ambiguous (horrifically so in the cases of "CST" and "BST"). ChrisA From avigross at verizon.net Sun Feb 3 16:22:36 2019 From: avigross at verizon.net (Avi Gross) Date: Sun, 3 Feb 2019 16:22:36 -0500 Subject: Ordered Ordinal number methods Message-ID: <006501d4bc06$95cb95c0$c162c140$@verizon.net> [NOTE: message is a tad long as it discusses multiple possible solutions and concerns including code.] The original question was how to do some reasonable translation from something like the "switch" statement in languages that have it, including C and R. Other languages use their own variants like cond in LISP. Some have nothing quite like it. They also differ in many ways such as the ability to handle arbitrary conditions or bundle multiple conditions to result in the same result. AS noted, Python deliberately omitted this feature entirely and it is suggested that one method is to use multiple "elif" clauses. I did a search to see if some module may play games with that notation to solve the specific issue relating to how to produce strings from the numbers in the range of 1:31 used in months on the calendar used in much of the world. (there are calendars limited to about 29 I use regularly.) Here is a page where a method is shown using that construction: https://codereview.stackexchange.com/questions/41298/producing-ordinal-numbe rs def ordinal(self, num): """ Returns ordinal number string from int, e.g. 1, 2, 3 becomes 1st, 2nd, 3rd, etc. """ self.num = num n = int(self.num) if 4 <= n <= 20: suffix = 'th' elif n == 1 or (n % 10) == 1: suffix = 'st' elif n == 2 or (n % 10) == 2: suffix = 'nd' elif n == 3 or (n % 10) == 3: suffix = 'rd' elif n < 100: suffix = 'th' ord_num = str(n) + suffix return ord_num The above is not my code. It is an example of how someone else solved a similar problem. It is clearly not a free function but a method. You can easily modify it and I annotate it below that way as a simple function to ask some dumb questions. But first, the IF is followed by a sequence of ELIF and no ELSE at the end. It works but is not written in a style I might choose. It does illustrate a sort of way to do cases where a switch might be used elsewhere. My earlier examples using inline and nested IF statements realistically could be expanded into something more like this, too. Here is the altered code with comments: def ordinal(num): """ Returns ordinal number string from int, e.g. 1, 2, 3 becomes 1st, 2nd, 3rd, etc. """ n = int(num) if 4 <= n <= 20: suffix = 'th' elif n == 1 or (n % 10) == 1: suffix = 'st' elif n == 2 or (n % 10) == 2: suffix = 'nd' elif n == 3 or (n % 10) == 3: suffix = 'rd' elif n < 100: suffix = 'th' ord_num = str(n) + suffix return ord_num I do like the way the above function accepts any kind of argument that can be converted to an int. It does have a serious flaw in that ordinal(100) and beyond generate an error. It does return 0th on 0 and the slightly odd -1th, -2th and so on. My approach was to look for patterns and note we cared only as a first approximation at numbers ending with 1,2,3 as special and even then, only if the preceding column was a 1. The teens are an exception. The above uses a different paradigm using inequalities so anything between 4 and 20 (inclusive) is noted as a "th" then a slightly redundant check for ending in 1 is checked as the sole exception of 11 has already been handled earlier. Now is the following redundant or efficient? elif n == 1 or (n % 10) == 1: The reality is that "1 % 10 " also matches n being 1. The OR though is a short-circuit so in the case that n == 1, the mod operator and comparison is skipped. Going on, a similar check is made for numbers ending in 2 and then 3. The last condition requires the number to be less than 100. Strictly speaking, the 100th would also be fine. And, as noted in another message, simply taking any positive number modulo 100 gets the same result so 103 would follow the rule for 3 and become 103rd. But if you really wanted to emulate a more strict model, a set of elif with exact integers would be reasonable using 31 conditions. if num == 1: suff = "st" elif num == 2: suff = "nd" . # 3 through 30 elif num == 31: suff = "st" That is horrible code and in the worst case does 31 comparisons. Not sure if the "in" operator is cheap enough for this version: if num in [1,21,31]: suff = "st" elif num in [2,22]: suff = "nd" elif num in [3,23]: suff = "rd" else: suff = "th" That has fewer comparisons albeit the "in" operator does additional searches. And, it does not scale up well as continuing to 100 means adding items like 32 and 33 and 41 to the lists. I already showed ways to make a dictionary version but there seem to be an indefinite number of variations on how to solve ANYTHING in python despite the founding lie that it is designed to do everything ONE way when possible. I thought to use functions in the dictionary instead, but as these are called without any known way to include arguments, I see no advantage in making 4 functions that each just return a string like "th" when I can just store and return the same string. If something long like a chapter of a book was wanted, yes, copying that umpteen times might be an annoyance. Nested dictionaries might make some sense. Again, like everything here, this is speculating on ONE way not suggesting this is great or elegant. I want to be able to break up my day number like 12 into a 1 and a 2. You can use str(day) or sprint() or many other methods and then ask for the last digit and optionally the digit before that. However you do it, you index this way using an int in my example although '1' and '2' could easily be used if altered.: >>> dictByTwo[1][2] 'th' >>> dictByTwo[2][2] 'nd' To do the above, I made a special case for the first (tens) number to be optionally a zero, the null string, or the None or Ellipsis unique objects. Here is the definition of the required dictionaries: dictNorm = { 0: "th", 1: "st", 2: "nd", 3: "rd", 4: "th", 5: "th", 6: "th", 7: "th", 8: "th", 9: "th" } dictTeens = {0: "th", 1: "th", 2: "th", 3: "th", 4: "th", 5: "th", 6: "th", 7: "th", 8: "th", 9: "th" } dictByTwo = {'': dictNorm, None: dictNorm, ...: dictNorm, 0: dictNorm, 1: dictTeens, 2: dictNorm, 3: dictNorm, 4: dictNorm, 5: dictNorm, 6: dictNorm, 7: dictNorm, 8: dictNorm, 9: dictNorm } The latter dictionary actually expands out if shown to contain copies of the other dictionaries: {'': {0: 'th', 1: 'st', 2: 'nd', 3: 'rd', 4: 'th', 5: 'th', 6: 'th', 7: 'th', 8: 'th', 9: 'th'}, None: {0: 'th', 1: 'st', 2: 'nd', 3: 'rd', 4: 'th', 5: 'th', 6: 'th', 7: 'th', 8: 'th', 9: 'th'}, Ellipsis: {0: 'th', 1: 'st', 2: 'nd', 3: 'rd', 4: 'th', 5: 'th', 6: 'th', 7: 'th', 8: 'th', 9: 'th'}, 0: {0: 'th', 1: 'st', 2: 'nd', 3: 'rd', 4: 'th', 5: 'th', 6: 'th', 7: 'th', 8: 'th', 9: 'th'}, 1: {0: 'th', 1: 'th', 2: 'th', 3: 'th', 4: 'th', 5: 'th', 6: 'th', 7: 'th', 8: 'th', 9: 'th'}, 2: {0: 'th', 1: 'st', 2: 'nd', 3: 'rd', 4: 'th', 5: 'th', 6: 'th', 7: 'th', 8: 'th', 9: 'th'}, 3: {0: 'th', 1: 'st', 2: 'nd', 3: 'rd', 4: 'th', 5: 'th', 6: 'th', 7: 'th', 8: 'th', 9: 'th'}, 4: {0: 'th', 1: 'st', 2: 'nd', 3: 'rd', 4: 'th', 5: 'th', 6: 'th', 7: 'th', 8: 'th', 9: 'th'}, 5: {0: 'th', 1: 'st', 2: 'nd', 3: 'rd', 4: 'th', 5: 'th', 6: 'th', 7: 'th', 8: 'th', 9: 'th'}, 6: {0: 'th', 1: 'st', 2: 'nd', 3: 'rd', 4: 'th', 5: 'th', 6: 'th', 7: 'th', 8: 'th', 9: 'th'}, 7: {0: 'th', 1: 'st', 2: 'nd', 3: 'rd', 4: 'th', 5: 'th', 6: 'th', 7: 'th', 8: 'th', 9: 'th'}, 8: {0: 'th', 1: 'st', 2: 'nd', 3: 'rd', 4: 'th', 5: 'th', 6: 'th', 7: 'th', 8: 'th', 9: 'th'}, 9: {0: 'th', 1: 'st', 2: 'nd', 3: 'rd', 4: 'th', 5: 'th', 6: 'th', 7: 'th', 8: 'th', 9: 'th'}} But who sees that, normally? LOL! Here are some more runs: >>> dictByTwo[''][1] 'st' >>> dictByTwo[None][2] 'nd' >>> dictByTwo[...][3] 'rd' >>> dictByTwo[6][9] 'th' >>> str(69) + dictByTwo[6][9] '69th' >>> str(63) + dictByTwo[6][3] '63rd' Again, not a serious suggestion. I prefer a function method to do it all. As an enhancement, so that multiple calls are fast, you can use some method that retains memory such as a function closure or a callable class. I am thinking a bit further out of the box here for any functionality that does significant calculations but has a great chance to reuse previously computed results. So what if you are asked to process a LONG list of dates in a DataFrame object and print them out as January 1st and January 2nd and January 3rd and January 4th and so on? (I hope the flattened file showed suffixes.) The plan would be for the function or class that has access to ongoing memory to create dictionaries like the above or of any other kinds or any sub-functions or methods when used first time. When invoked again, it skips that and uses what it created. Even worse, it may keep track of what it has been called with. If I call it with say the day 5 once, it can compute "5th" and store it in a dictionary or list. I mean as the fifth item in the list or the key value pair in a dict. If called later with the same number, a constant-time lookup is done first and only if that fails do you do another calculation and store that. Note the newer generator functions can be used this way as they can accept a new value on the "yield" command. So, yet another python object that retains memory so it can use these techniques, perhaps. From rosuav at gmail.com Sun Feb 3 19:15:53 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 4 Feb 2019 11:15:53 +1100 Subject: Implement C's Switch in Python 3 In-Reply-To: References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> <7ecb5f4a-3013-b3a5-b184-9e3b647591df@DancesWithMice.info> <1a8ce044-889d-8ac1-e0b7-b0bdc96c8e71@DancesWithMice.info> <00e701d4bc1b$7cfd7030$76f85090$@verizon.net> Message-ID: On Mon, Feb 4, 2019 at 11:08 AM Chris Angelico wrote: > If you need to attach some *other* time zone (which should be rare - > ONLY do this if you absolutely cannot translate to UTC) BTW, there are some legit reasons for keeping something in a different timezone. If you're representing an instant in time, use UTC; but if you're representing a recurring event in a civil context, you may want to say something like "every Thursday at 5pm Europe/Prague". Or if you're dealing with future events, it might be important to have the recorded time change if civil time changes. But for event timestamping ("this is when this HTTP request came in"), just convert it to UTC on arrival and store it with the "Z" suffix. ChrisA From 2QdxY4RzWzUUiLuE at potatochowder.com Sun Feb 3 20:28:41 2019 From: 2QdxY4RzWzUUiLuE at potatochowder.com (Dan Sommers) Date: Sun, 3 Feb 2019 19:28:41 -0600 Subject: Switch function In-Reply-To: <00e201d4bc19$e065f6d0$a131e470$@verizon.net> References: <00e201d4bc19$e065f6d0$a131e470$@verizon.net> Message-ID: <848e1243-8265-be5c-114b-98daf817d0a7@potatochowder.com> On 2/3/19 5:40 PM, Avi Gross wrote: > Bottom line, does anyone bother using anything like this? It is actually a > bunch of hidden IF statements matched in order but may meet many needs. I sure don't. In the rare case that I might use a switch statement in another language, I just use a series of elifs, or maybe a dictionary I build once and use over and over (a habit left over from the old days of extremely constrained systems, when medium-to-large lookup tables and simple logic was better than many kinds of runtime computation). If it gets unweidly, I think harder about how to isolate the "switching" from the results, and usually end up with a collection of smaller functions to do the work, or high order functions, or maybe some sort of object oriented polymorphism as a last resort. From avigross at verizon.net Sun Feb 3 22:03:27 2019 From: avigross at verizon.net (Avi Gross) Date: Sun, 3 Feb 2019 22:03:27 -0500 Subject: Switch function In-Reply-To: <848e1243-8265-be5c-114b-98daf817d0a7@potatochowder.com> References: <00e201d4bc19$e065f6d0$a131e470$@verizon.net> <848e1243-8265-be5c-114b-98daf817d0a7@potatochowder.com> Message-ID: <000c01d4bc36$33872de0$9a9589a0$@verizon.net> Dan, I agree and have felt no need to do a method like this. Most of the time a switch is easy to avoid or even forget that it exists. I rarely needed to use these years ago when they were available in C and C++ , JAVA, JavaScript, PERL or R or under other *names* but similar functionality like COND in versions of LISP or CASE in PASCAL. The times I would use it would not be when were a relatively few cases. It makes more sense for example when you are accepting a one-character command in the very old style from the user. I repeat, OLD style. Your main loop may largely consist of a switch statement where every possible keystroke calls one of a set of functions. I mean things like D is for Delete and I is for insert and A is for Append and Q is for QUIT and some are absolutely not mnemonic like V is for Paste. I have read books that suggest having a long function is a sign of clutter or even insanity and I feel that is not realistic in many cases. The example I show above could in many cases be done as you describe but what are you gaining? I mean if I subtract the integer representation of a keyboard alphabetic letter (ASCII for the example) from letter 'a' or 'A' then A maps to 0 and B maps to 1 and ... Z maps to 26. So, you could make a list of 26 functions (some may be absent) and your entire switch statement looks like: funcs=[zeroeth,first,other,...,last] # list of function handles var=input("Enter a Command: ") if ord('A') <= ord(var[0]) <= ord('Z'): result = funcs[ord(var[0]) - ord('A')]() Is that short enough? Mind you, that is not the same as a switch statement form that allows arbitrary code after each choice and lets you access variables at that level and change them, as well as call functions with specific arguments that make sense at that time. Yes, judicious use of the global or nonlocal statement can dangerously get you some of that functionality. A reasonable goal might be to have the code at each case be relatively brief. Maybe a few lines adjusting local variables then calling a function that lets more code be elsewhere by passing in additional things needed in that context or passing in mutable arguments. The indentation should make it reasonable to follow as if you are looking at N different functions where each one is not too large even if it is all in one large switch. Now this sounds quite a bit like the dictionary trick but note many of the above languages do not have a built-in dictionary concept and some may not support lists of functions or objects. I have seen monstrosities like this: functionTop is: If var == 'A': # do something else: functionB(var) Guess what functionB says? Yep, if it is 'B" do something else call functionC. So a mere 26 functions, each short and stacked. Ugh! It may be a matter of taste and I have seen places such as statistical calculations where such an approach is even needed. In very brief, some tests require a choice of TWO so one choice is A versus non-A and then you do something with the non-A that are B versus non-B. Your comment about object polymorphism is interesting. I am picturing how each choice somehow delivers an object that automatically is set up to do the right thing. Having said that, I realize how there is something in python quite similar to a case statement! Picture my example where you have a try statement that raises at least 26 error objects followed by up to 26 catch statements: try: something that raises a designer exception catch errorA as var: pass catch errorb, errorB: pass ... catch errorZ: pass That looks like a switch in many ways and lets you combine multiple choices into one case. Heck, there is also a finally and else clause you can use for fine tuning as in setting a default. Again, see how can you write a COMPLICATED try command that captures many things including real errors? I know I keep repeating this but experience tells me that if not mentioned, someone will make believe I meant something other than what I say. The above is NOT a suggestion of what to do. It is an example of another feature that IS in the language that in some ways is much like the switch statement. Not always a good idea to abuse it when it can be done with IF variants or a dict jump or many other gimmicks. In the above the *raise* statement within whatever you try can do the same trick as before and create a small integer offset from letter 'A' to use in making a slightly different object for each case in one of many ways. I will stop here. -----Original Message----- From: Python-list On Behalf Of Dan Sommers Sent: Sunday, February 3, 2019 8:29 PM To: python-list at python.org Subject: Re: Switch function On 2/3/19 5:40 PM, Avi Gross wrote: > Bottom line, does anyone bother using anything like this? It is > actually a bunch of hidden IF statements matched in order but may meet many needs. I sure don't. In the rare case that I might use a switch statement in another language, I just use a series of elifs, or maybe a dictionary I build once and use over and over (a habit left over from the old days of extremely constrained systems, when medium-to-large lookup tables and simple logic was better than many kinds of runtime computation). If it gets unweidly, I think harder about how to isolate the "switching" from the results, and usually end up with a collection of smaller functions to do the work, or high order functions, or maybe some sort of object oriented polymorphism as a last resort. -- https://mail.python.org/mailman/listinfo/python-list From PythonList at DancesWithMice.info Sun Feb 3 22:11:19 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Mon, 4 Feb 2019 16:11:19 +1300 Subject: Implement C's Switch in Python 3 In-Reply-To: References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> Message-ID: <954aff2d-e584-228b-9094-285095842447@DancesWithMice.info> Christian, On 4/02/19 10:00 AM, Christian Gollwitzer wrote: > Am 03.02.19 um 09:32 schrieb DL Neil: >> Now back to ordinal dates - the "st", "th", etc suffixes only work in >> English. You'd need another list (but no great coding complexity) to >> cope with a second, third, ... language! > > Only for some languages. In other languages there can be, for example, > cases (inflections). Then the suffix not only depends on the number, but > also on the case, which is governed by e.g. a preposition or the use in > the sentence. Slavic languages have 6 or 7 cases. I was unaware of that - not having had to cope with any of the Slavic languages, to-date. Are multiple cases/inflections used for dates? French has two, including both male and female ordinals, but only one applies to dates! > Of course you can make the list two-dimensional to cover that, but then > another language will appear which has yet another different thing.... > > Basically you'll need to rewrite the whole thing when going to a > completely different language. Ouch! However, such limitation also applies to the 'case solution', and worse, if that additional language/those languages' exceptions apply to different values, eg 1st, 11th, 2nd, 3rd which are "hard-coded" in the English convention. Not good science, but I have been sampling my correspondence today, inspecting letters, emails, etc, for date-formats. Guess how many use ordinals? Stuff from the US tends to use numerics in the mm/dd/ccyy convention. Stuff from the UK and British Commonwealth (excluding Australia) tends to use month-words or abbreviations (but not ordinals). -- Regards =dn From 2QdxY4RzWzUUiLuE at potatochowder.com Sun Feb 3 22:44:04 2019 From: 2QdxY4RzWzUUiLuE at potatochowder.com (Dan Sommers) Date: Sun, 3 Feb 2019 21:44:04 -0600 Subject: Switch function In-Reply-To: <000c01d4bc36$33872de0$9a9589a0$@verizon.net> References: <00e201d4bc19$e065f6d0$a131e470$@verizon.net> <848e1243-8265-be5c-114b-98daf817d0a7@potatochowder.com> <000c01d4bc36$33872de0$9a9589a0$@verizon.net> Message-ID: <03aa2958-852a-f622-c4b4-f98ec8cc41b7@potatochowder.com> On 2/3/19 9:03 PM, Avi Gross wrote: > The example I show above could in many cases be done as you describe > but what are you gaining? > > I mean if I subtract the integer representation of a keyboard > alphabetic letter (ASCII for the example) from letter 'a' or 'A' then > A maps to 0 and B maps to 1 and ... Z maps to 26. So, you could make > a list of 26 functions (some may be absent) and your entire switch > statement looks like: > > funcs=[zeroeth,first,other,...,last] # list of function handles > var=input("Enter a Command: ") > if ord('A') <= ord(var[0]) <= ord('Z'): > result = funcs[ord(var[0]) - ord('A')]() > > Is that short enough? It's not a matter of shortness. Your code encapsulates the idea of dispatching to a 0-airity function based on an input. In Python, though, I'd still use a dictionary, which would be more flexible (what if I start to use digits or other characters as commands?) and less error prone. > Your comment about object polymorphism is interesting. I am picturing > how each choice somehow delivers an object that automatically is set > up to do the right thing. Disclaimer: I speak OO as a second language, and only when there is an obvious and compelling advantage over some other paradigm. That said: If the mapping from input to function is more complex than A -> func1, B -> func2, etc., then a factory function that builds an object with an execute method is a good way of isolating the mapping and keeping the main code clean and clear. > Again, see how can you write a COMPLICATED try command that captures > many things including real errors? Don't do that. Why are you writing COMPLICATED try commands? Did you have a Python question? ;-) From auriocus at gmx.de Mon Feb 4 03:18:24 2019 From: auriocus at gmx.de (Christian Gollwitzer) Date: Mon, 4 Feb 2019 09:18:24 +0100 Subject: Implement C's Switch in Python 3 [OT languages] In-Reply-To: References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> <954aff2d-e584-228b-9094-285095842447@DancesWithMice.info> Message-ID: Am 04.02.19 um 04:11 schrieb DL Neil: > On 4/02/19 10:00 AM, Christian Gollwitzer wrote: >> Am 03.02.19 um 09:32 schrieb DL Neil: >>> Now back to ordinal dates - the "st", "th", etc suffixes only work in >>> English. You'd need another list (but no great coding complexity) to >>> cope with a second, third, ... language! >> >> Only for some languages. In other languages there can be, for example, >> cases (inflections). Then the suffix not only depends on the number, >> but also on the case, which is governed by e.g. a preposition or the >> use in the sentence. Slavic languages have 6 or 7 cases. > > I was unaware of that - not having had to cope with any of the Slavic > languages, to-date. Are multiple cases/inflections used for dates? > French has two, including both male and female ordinals, but only one > applies to dates! > There is good and bad news ;) Bad news first: The Slavic languages have a noun inflection system close to the Proto-Indoeuropean one. There are 3 genders (male, female, neutral), two numbers (singular, plural) and 6 or 7 cases, depending on the language (indicating the function, nominative = subject, accusative = mostly object, locative = indicating a position in space, ....). A numeral or ordinal can/must take any of those 3x2x7 forms in accordance to the noun it refers to. Now the good news: for dates usually there is a fixed format which significantly reduces this. I am fluent in Czech, and there, almost exclusively the genitve case is used for dates. "prvniho cervence", literally translating "of the first July", is used almost anywhere, like "at the first of July", "prvniho cervence" "until the first of July" = "az do prvniho cervence", "from the first of July on" = "od prvnhio cervence". Only in rare circumstances, the first case is used "Prvni cervenec je den meho narozeni" = "the first of July is my birthday" Now the even better news: In written form, spelling out the ordinals is very unusual. Usually, it is just indicated by a period, so all of the above could be written "1. 6.". This is also the standard way to write it in German. But it becomes a mess if you want to attach the day of the week, because then you need to take into account the preposition and the position in the sentence. I think English is quite "unique" with writing out the ending of the ordinals attached to arabic numerals. > However, such limitation also applies to the 'case solution', and worse, > if that additional language/those languages' exceptions apply to > different values, eg 1st, 11th, 2nd, 3rd which are "hard-coded" in the > English convention. yes of course. True I18N is very hard, I'm glad that we just don't need to do it ;) Christian From auriocus at gmx.de Mon Feb 4 03:25:35 2019 From: auriocus at gmx.de (Christian Gollwitzer) Date: Mon, 4 Feb 2019 09:25:35 +0100 Subject: Implement C's Switch in Python 3 [OT languages] In-Reply-To: References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> <954aff2d-e584-228b-9094-285095842447@DancesWithMice.info> Message-ID: Am 04.02.19 um 09:18 schrieb Christian Gollwitzer: > I think English is quite "unique" with writing out the ending of the > ordinals attached to arabic numerals. > Of course, there is a Wikipedia page about it: https://en.wikipedia.org/wiki/Ordinal_indicator So I was wrong and the abbreviated suffix is as common as the period over the European languages. Christian From diego.avesani at gmail.com Mon Feb 4 04:17:52 2019 From: diego.avesani at gmail.com (Diego Avesani) Date: Mon, 4 Feb 2019 01:17:52 -0800 (PST) Subject: mask and proper index In-Reply-To: <2b40907c-f883-4031-a2f5-15f50c9827b6@googlegroups.com> References: <2b40907c-f883-4031-a2f5-15f50c9827b6@googlegroups.com> Message-ID: <44ffcf65-aa07-4b6b-8b7c-2741decd2a43@googlegroups.com> Dear all, Dear Peter, thanks for you suggestions. Next time, I will try to set-up a proper example in order to post it and explain better my problem. You understood perfectly what was my problem. Thanks a lot, indeed it seems to work. If I can ask, due to the fact that I am new to thins kind of forum. In this moment, I have another problem related to the data I am working on. Should I write another post? Should I do something else related to this post? Thanks a lot again, you are my lifesaver On Sunday, 3 February 2019 16:56:44 UTC+1, Diego Avesani wrote: > Dear all, > > I am trying to apply a mask to my dataframe: > > mask = (df['datatime'] > start_date) & (df['datatime'] <= end_date) > df = df.loc[mask] > > > It seems to work pretty well. > > After that I crate the cumulative of its element as: > > > PP_cumPP = np.cumsum(df[PP_station]) > > > However, I am not able to compute PP_cumPP last element. Indeed, when I do > > len(PP_cumPP) > > I get > > 8783 > > and when I try to do: > > PP_cumPP[len(PP_cumPP)-1] > > I get an error. > > > What I am doing wrong? > > Thanks a lot for any kind of help > > Diedro From __peter__ at web.de Mon Feb 4 06:02:52 2019 From: __peter__ at web.de (Peter Otten) Date: Mon, 04 Feb 2019 12:02:52 +0100 Subject: mask and proper index References: <2b40907c-f883-4031-a2f5-15f50c9827b6@googlegroups.com> <44ffcf65-aa07-4b6b-8b7c-2741decd2a43@googlegroups.com> Message-ID: Diego Avesani wrote: > If I can ask, due to the fact that I am new to thins kind of forum. > In this moment, I have another problem related to the data I am working > on. Should I write another post? You should start a new thread for a new problem, even if for you it is part of the same project or script. > Should I do something else related to this post? Telling us that your problem is solved is nice. If you have to modify a proposed solution or found a better one elsewhere you should also mention that for further reference. I think you already did that with > You understood perfectly what was my problem. Thanks a lot, indeed it seems to work. No further action needed :) From diego.avesani at gmail.com Mon Feb 4 07:58:03 2019 From: diego.avesani at gmail.com (Diego Avesani) Date: Mon, 4 Feb 2019 04:58:03 -0800 (PST) Subject: format number is not recognized in a cycle Message-ID: Dear all, I have this dataframe: datatime,T,RH,PSFC,DIR,VEL10,PREC,RAD,CC,FOG 2012-01-01 06:00, 0.4,100, 911,321, 2.5, 0.0, 0, 0,0 2012-01-01 07:00, 0.8,100, 911,198, 0.8, 0.0, 0, 22,0 2012-01-01 08:00, 0.6,100, 912, 44, 1.2, 0.0, 30, 22,0 2012-01-01 09:00, 3.1, 76, 912, 22, 0.8, 0.0, 134, 44,0 2012-01-01 10:00, 3.4, 77, 912, 37, 0.5, 0.0, 191, 67,0 2012-01-01 11:00, 3.5,100, 912,349, 0.4, 0.0, 277, 44,0 2012-01-01 12:00, 3.6,100, 912, 17, 0.9, 0.0, 292, 22,0 2012-01-01 13:00, 3.5,100, 912, 28, 0.3, 0.0, 219, 44,0 2012-01-01 14:00, 3.3, 68, 912, 42, 0.5, 0.0, 151, 22,0 .... .... I would like to plot and analyse different part of it. Consequently I have created the following variables in order to manage different period: start_date = np.array(['2012-01-01 06:00','2013-01-01 06:00','2014-01-01 06:00']) end_date = np.array(['2013-01-01 05:00','2014-01-01 05:00','2015-01-01 05:00']) However, if I try to perform the following cycle: for ii in range(0, 1): pd.read_csv('dati.csv',delimiter=',',header=0,parse_dates=True, na_values=-999) df['datatime'] = df['datatime'].map(lambda x: datetime.strptime(str(x), "%Y-%m-%d %H:%M")) # mask = (df['datatime'] > start_date[ii]) & (df['datatime'] <= end_date[ii]) I get the following error: return _get_dtype_type(np.dtype(arr_or_dtype)) File "/usr/lib/python2.7/dist-packages/numpy/core/_internal.py", line 173, in _commastring (len(result)+1, astr)) ValueError: format number 1 of "2012-01-01 06:00" is not recognized On the contrary, if I use the simple variables: start_date = 2012-01-01 06:00 end_date = 2013-01-01 05:00 without the cycle, it works. I really do not understand why. Thanks a lot, Diego From __peter__ at web.de Mon Feb 4 09:51:09 2019 From: __peter__ at web.de (Peter Otten) Date: Mon, 04 Feb 2019 15:51:09 +0100 Subject: format number is not recognized in a cycle References: Message-ID: Diego Avesani wrote: > Dear all, > > I have this dataframe: > > datatime,T,RH,PSFC,DIR,VEL10,PREC,RAD,CC,FOG > 2012-01-01 06:00, 0.4,100, 911,321, 2.5, 0.0, 0, 0,0 > 2012-01-01 07:00, 0.8,100, 911,198, 0.8, 0.0, 0, 22,0 > 2012-01-01 08:00, 0.6,100, 912, 44, 1.2, 0.0, 30, 22,0 > 2012-01-01 09:00, 3.1, 76, 912, 22, 0.8, 0.0, 134, 44,0 > 2012-01-01 10:00, 3.4, 77, 912, 37, 0.5, 0.0, 191, 67,0 > 2012-01-01 11:00, 3.5,100, 912,349, 0.4, 0.0, 277, 44,0 > 2012-01-01 12:00, 3.6,100, 912, 17, 0.9, 0.0, 292, 22,0 > 2012-01-01 13:00, 3.5,100, 912, 28, 0.3, 0.0, 219, 44,0 > 2012-01-01 14:00, 3.3, 68, 912, 42, 0.5, 0.0, 151, 22,0 > > .... > .... > > I would like to plot and analyse different part of it. Consequently I have > created the following variables in order to manage different period: > > start_date = np.array(['2012-01-01 06:00','2013-01-01 06:00','2014-01-01 > 06:00']) > end_date = np.array(['2013-01-01 05:00','2014-01-01 05:00','2015-01-01 > 05:00']) > > However, if I try to perform the following cycle: > > for ii in range(0, 1): > pd.read_csv('dati.csv',delimiter=',',header=0,parse_dates=True, > na_values=-999) > df['datatime'] = df['datatime'].map(lambda x: datetime.strptime(str(x), > "%Y-%m-%d %H:%M")) > # > mask = (df['datatime'] > start_date[ii]) & (df['datatime'] <= > end_date[ii]) > > I get the following error: > > return _get_dtype_type(np.dtype(arr_or_dtype)) > File "/usr/lib/python2.7/dist-packages/numpy/core/_internal.py", line > 173, in _commastring > (len(result)+1, astr)) > ValueError: format number 1 of "2012-01-01 06:00" is not recognized > > > > On the contrary, if I use the simple variables: > start_date = 2012-01-01 06:00 > end_date = 2013-01-01 05:00 > > without the cycle, it works. > > I really do not understand why. Nor do I. Also, I cannot run the code above, and when I fill in the missing parts using guesswork I cannot reproduce the error. Would you mind posting a small script, inserted via cut and paste into your message, that we can run without any additions? You may want to read http://www.sscce.org/ first. From diego.avesani at gmail.com Mon Feb 4 09:50:37 2019 From: diego.avesani at gmail.com (Diego Avesani) Date: Mon, 4 Feb 2019 06:50:37 -0800 (PST) Subject: pandas read dataframe and sum all value same month and year Message-ID: Dear all, I am reading the following data-frame: datatime,T,RH,PSFC,DIR,VEL10,PREC,RAD,CC,FOG 2012-01-01 06:00, -0.1,100, 815,313, 2.6, 0.0, 0, 0,0 2012-01-01 07:00, -1.2, 93, 814,314, 4.8, 0.0, 0, 0,0 2012-01-01 08:00, 1.7, 68, 815,308, 7.5, 0.0, 41, 11,0 2012-01-01 09:00, 2.4, 65, 815,308, 7.4, 0.0, 150, 33,0 2012-01-01 10:00, 3.0, 64, 816,305, 8.4, 0.0, 170, 44,0 2012-01-01 11:00, 2.6, 65, 816,303, 6.3, 0.0, 321, 22,0 2012-01-01 12:00, 2.0, 72, 816,278, 1.3, 0.0, 227, 22,0 2012-01-01 13:00, -0.0, 72, 816,124, 0.1, 0.0, 169, 22,0 2012-01-01 14:00, -0.1, 68, 816,331, 1.4, 0.0, 139, 33,0 2012-01-01 15:00, -4.0, 85, 816,170, 0.6, 0.0, 49, 0,0 .... .... I read the data frame as: df = pd.read_csv('dati.csv',delimiter=',',header=0,parse_dates=True,na_values=-999) df['datatime'] = df['datatime'].map(lambda x: datetime.strptime(str(x), "%Y-%m-%d %H:%M")) # mask = (df['datatime'] > str(start_date[ii])) & (df['datatime'] <= str(end_date[ii])) df = df.loc[mask] df = df.reset_index(drop=True) I would to create an array with the sum of all the PREC value in the same month. I have tried with: df.groupby(pd.TimeGrouper('M')).sum() But as always, it seems that I have same problems with the indexes. Indeed, I get: 'an instance of %r' % type(ax).__name__) TypeError: axis must be a DatetimeIndex, but got an instance of 'Int64Index' thanks for any kind of help, Really Really thanks Diego From diego.avesani at gmail.com Mon Feb 4 10:02:50 2019 From: diego.avesani at gmail.com (Diego Avesani) Date: Mon, 4 Feb 2019 07:02:50 -0800 (PST) Subject: format number is not recognized in a cycle In-Reply-To: References: Message-ID: Dear Peter, thanks a lot for your patience. this is the code: import pandas as pd import numpy as np from datetime import datetime #input: start_date = np.array(["2012-01-01 06:00",'2013-01-01 06:00','2014-01-01 06:00']) end_date = np.array(["2013-01-01 05:00",'2014-01-01 05:00','2015-01-01 05:00']) yearfolder = np.array(['2012','2013','2014']) for ii in range(0, 1): df = pd.read_csv('dati.csv',delimiter=',',header=0,parse_dates=True,na_values=-999) df['datatime'] = df['datatime'].map(lambda x: datetime.strptime(str(x), "%Y-%m-%d %H:%M")) mask = (df['datatime'] > start_date[ii]) & (df['datatime'] <= end_date[ii]) df = df.loc[mask] and here a piece of my dati.csv file datatime,T,RH,PSFC,DIR,VEL10,PREC,RAD,CC,FOG 2012-01-01 06:00, -0.1,100, 815,313, 2.6, 0.0, 0, 0,0 2012-01-01 07:00, -1.2, 93, 814,314, 4.8, 0.0, 0, 0,0 2012-01-01 08:00, 1.7, 68, 815,308, 7.5, 0.0, 41, 11,0 2012-01-01 09:00, 2.4, 65, 815,308, 7.4, 0.0, 150, 33,0 2012-01-01 10:00, 3.0, 64, 816,305, 8.4, 0.0, 170, 44,0 2012-01-01 11:00, 2.6, 65, 816,303, 6.3, 0.0, 321, 22,0 2012-01-01 12:00, 2.0, 72, 816,278, 1.3, 0.0, 227, 22,0 2012-01-01 13:00, -0.0, 72, 816,124, 0.1, 0.0, 169, 22,0 2012-01-01 14:00, -0.1, 68, 816,331, 1.4, 0.0, 139, 33,0 2012-01-01 15:00, -4.0, 85, 816,170, 0.6, 0.0, 49, 0,0 2012-01-01 16:00, -5.6, 92, 816,168, 0.8, 0.0, 0, 0,0 2012-01-01 17:00, -6.4, 96, 817,109, 0.4, 0.0, 0, 0,0 2012-01-01 18:00, -6.9,100, 817,116, 0.6, 0.0, 0, 0,0 2012-01-01 19:00, -7.5,100, 817,127, 0.8, 0.0, 0, 0,0 2012-01-01 20:00, -7.7,100, 817,136, 0.6, 0.0, 0, 0,0 2012-01-01 21:00, -7.7,100, 818,118, 0.7, 0.0, 0, 0,0 2012-01-01 22:00, -7.8,100, 817,130, 0.5, 0.0, 0, 0,0 2012-01-01 23:00, -7.9,100, 816,160, 0.6, 0.0, 0, 0,0 2012-01-02 00:00, -8.3,100, 816,123, 0.6, 0.0, 0, 0,0 2012-01-02 01:00, -8.6,100, 815,119, 0.8, 0.0, 0, 11,0 2012-01-02 02:00, -9.1,100, 814,118, 1.1, 0.0, 0, 33,0 thanks, again, a lot On Monday, 4 February 2019 15:51:41 UTC+1, Peter Otten wrote: > Diego Avesani wrote: > > > Dear all, > > > > I have this dataframe: > > > > datatime,T,RH,PSFC,DIR,VEL10,PREC,RAD,CC,FOG > > 2012-01-01 06:00, 0.4,100, 911,321, 2.5, 0.0, 0, 0,0 > > 2012-01-01 07:00, 0.8,100, 911,198, 0.8, 0.0, 0, 22,0 > > 2012-01-01 08:00, 0.6,100, 912, 44, 1.2, 0.0, 30, 22,0 > > 2012-01-01 09:00, 3.1, 76, 912, 22, 0.8, 0.0, 134, 44,0 > > 2012-01-01 10:00, 3.4, 77, 912, 37, 0.5, 0.0, 191, 67,0 > > 2012-01-01 11:00, 3.5,100, 912,349, 0.4, 0.0, 277, 44,0 > > 2012-01-01 12:00, 3.6,100, 912, 17, 0.9, 0.0, 292, 22,0 > > 2012-01-01 13:00, 3.5,100, 912, 28, 0.3, 0.0, 219, 44,0 > > 2012-01-01 14:00, 3.3, 68, 912, 42, 0.5, 0.0, 151, 22,0 > > > > .... > > .... > > > > I would like to plot and analyse different part of it. Consequently I have > > created the following variables in order to manage different period: > > > > start_date = np.array(['2012-01-01 06:00','2013-01-01 06:00','2014-01-01 > > 06:00']) > > end_date = np.array(['2013-01-01 05:00','2014-01-01 05:00','2015-01-01 > > 05:00']) > > > > However, if I try to perform the following cycle: > > > > for ii in range(0, 1): > > pd.read_csv('dati.csv',delimiter=',',header=0,parse_dates=True, > > na_values=-999) > > df['datatime'] = df['datatime'].map(lambda x: datetime.strptime(str(x), > > "%Y-%m-%d %H:%M")) > > # > > mask = (df['datatime'] > start_date[ii]) & (df['datatime'] <= > > end_date[ii]) > > > > I get the following error: > > > > return _get_dtype_type(np.dtype(arr_or_dtype)) > > File "/usr/lib/python2.7/dist-packages/numpy/core/_internal.py", line > > 173, in _commastring > > (len(result)+1, astr)) > > ValueError: format number 1 of "2012-01-01 06:00" is not recognized > > > > > > > > On the contrary, if I use the simple variables: > > start_date = 2012-01-01 06:00 > > end_date = 2013-01-01 05:00 > > > > without the cycle, it works. > > > > I really do not understand why. > > Nor do I. > > Also, I cannot run the code above, and when I fill in the missing parts > using guesswork I cannot reproduce the error. > > Would you mind posting a small script, inserted via cut and paste into your > message, that we can run without any additions? > > You may want to read http://www.sscce.org/ first. From lukasz at langa.pl Mon Feb 4 06:32:25 2019 From: lukasz at langa.pl (=?utf-8?Q?=C5=81ukasz_Langa?=) Date: Mon, 4 Feb 2019 12:32:25 +0100 Subject: [RELEASE] Python 3.8.0a1 is now available for testing Message-ID: I packaged my first release. *wipes sweat off of face* Go get it here: https://www.python.org/downloads/release/python-380a1/ Python 3.8.0a1 is the first of four planned alpha releases of Python 3.8, the next feature release of Python. During the alpha phase, Python 3.8 remains under heavy development: additional features will be added and existing features may be modified or deleted. Please keep in mind that this is a preview release and its use is not recommended for production environments. The next preview release, 3.8.0a2, is planned for 2019-02-24. Apart from building the Mac installers, Ned helped me a lot with the process, thank you! Ernest was super quick providing me with all required access and fixing a Unicode problem I found in Salt, thank you! Finally, this release was made on a train to D?sseldorf. There's a PyPy sprint there. The train is pretty cool, makes this "Wasm! Wasm!" sound. - ? -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: Message signed with OpenPGP URL: From __peter__ at web.de Mon Feb 4 14:00:07 2019 From: __peter__ at web.de (Peter Otten) Date: Mon, 04 Feb 2019 20:00:07 +0100 Subject: format number is not recognized in a cycle References: Message-ID: Diego Avesani wrote: > this is the code: While the example is fine now it runs without error over here, on rather old versions of pandas (0.13.1) and numpy (1.8.2). Therefore I'm out of the debugging cycle for now. From diego.avesani at gmail.com Mon Feb 4 14:31:42 2019 From: diego.avesani at gmail.com (Diego Avesani) Date: Mon, 4 Feb 2019 11:31:42 -0800 (PST) Subject: format number is not recognized in a cycle In-Reply-To: References: Message-ID: <14ca0f1f-7d23-47a6-9792-b24491d3c21c@googlegroups.com> Dear Peter, Deal all, Trying different options, I manage this solution: mask = (df['datatime'] > str(start_date[ii])) & (df['datatime'] <= str(end_date[ii])) As you can notice, I have put str before start_date[ii]) and end_date[ii]. What do you think? Thanks On Monday, 4 February 2019 20:00:32 UTC+1, Peter Otten wrote: > Diego Avesani wrote: > > > this is the code: > > While the example is fine now it runs without error over here, on rather old > versions of pandas (0.13.1) and numpy (1.8.2). > > Therefore I'm out of the debugging cycle for now. From diego.avesani at gmail.com Mon Feb 4 14:38:12 2019 From: diego.avesani at gmail.com (Diego Avesani) Date: Mon, 4 Feb 2019 11:38:12 -0800 (PST) Subject: pandas read dataframe and sum all value same month and year In-Reply-To: References: Message-ID: Deal all, following Peter's suggestion, I put the example code: import pandas as pd import numpy as np from datetime import datetime #input: start_date = np.array(["2012-01-01 06:00",'2013-01-01 06:00','2014-01-01 06:00']) end_date = np.array(["2013-01-01 05:00",'2014-01-01 05:00','2015-01-01 05:00']) yearfolder = np.array(['2012','2013','2014']) for ii in range(0, 1): df = pd.read_csv('dati.csv',delimiter=',',header=0,parse_dates=True,na_values=-999) df['datatime'] = df['datatime'].map(lambda x: datetime.strptime(str(x), "%Y-%m-%d %H:%M")) mask = (df['datatime'] > str(start_date[ii])) & (df['datatime'] <= str(end_date[ii])) df = df.loc[mask] df = df.reset_index(drop=True) # df.groupby(pd.TimeGrouper('m')).sum() and the example of file: datatime,T,RH,PSFC,DIR,VEL10,PREC,RAD,CC,FOG 2012-01-01 06:00, 0.4,100, 911,321, 2.5, 0.0, 0, 0,0 2012-01-01 07:00, 0.8,100, 911,198, 0.8, 0.0, 0, 22,0 2012-01-01 08:00, 0.6,100, 912, 44, 1.2, 0.0, 30, 22,0 2012-01-01 09:00, 3.1, 76, 912, 22, 0.8, 0.0, 134, 44,0 2012-01-01 10:00, 3.4, 77, 912, 37, 0.5, 0.0, 191, 67,0 2012-01-01 11:00, 3.5,100, 912,349, 0.4, 0.0, 277, 44,0 2012-01-01 12:00, 3.6,100, 912, 17, 0.9, 0.0, 292, 22,0 2012-01-01 13:00, 3.5,100, 912, 28, 0.3, 0.0, 219, 44,0 2012-01-01 14:00, 3.3, 68, 912, 42, 0.5, 0.0, 151, 22,0 Hope this could help in finding a way to sum value belonging to the same month. Thanks again, a lot Diego On Monday, 4 February 2019 15:50:52 UTC+1, Diego Avesani wrote: > Dear all, > > I am reading the following data-frame: > > datatime,T,RH,PSFC,DIR,VEL10,PREC,RAD,CC,FOG > 2012-01-01 06:00, -0.1,100, 815,313, 2.6, 0.0, 0, 0,0 > 2012-01-01 07:00, -1.2, 93, 814,314, 4.8, 0.0, 0, 0,0 > 2012-01-01 08:00, 1.7, 68, 815,308, 7.5, 0.0, 41, 11,0 > 2012-01-01 09:00, 2.4, 65, 815,308, 7.4, 0.0, 150, 33,0 > 2012-01-01 10:00, 3.0, 64, 816,305, 8.4, 0.0, 170, 44,0 > 2012-01-01 11:00, 2.6, 65, 816,303, 6.3, 0.0, 321, 22,0 > 2012-01-01 12:00, 2.0, 72, 816,278, 1.3, 0.0, 227, 22,0 > 2012-01-01 13:00, -0.0, 72, 816,124, 0.1, 0.0, 169, 22,0 > 2012-01-01 14:00, -0.1, 68, 816,331, 1.4, 0.0, 139, 33,0 > 2012-01-01 15:00, -4.0, 85, 816,170, 0.6, 0.0, 49, 0,0 > .... > .... > > I read the data frame as: > > df = pd.read_csv('dati.csv',delimiter=',',header=0,parse_dates=True,na_values=-999) > df['datatime'] = df['datatime'].map(lambda x: datetime.strptime(str(x), "%Y-%m-%d %H:%M")) > # > mask = (df['datatime'] > str(start_date[ii])) & (df['datatime'] <= str(end_date[ii])) > df = df.loc[mask] > df = df.reset_index(drop=True) > > I would to create an array with the sum of all the PREC value in the same month. > > I have tried with: > > df.groupby(pd.TimeGrouper('M')).sum() > > But as always, it seems that I have same problems with the indexes. Indeed, I get: > 'an instance of %r' % type(ax).__name__) > TypeError: axis must be a DatetimeIndex, but got an instance of 'Int64Index' > > thanks for any kind of help, > Really Really thanks > > Diego From __peter__ at web.de Mon Feb 4 14:59:37 2019 From: __peter__ at web.de (Peter Otten) Date: Mon, 04 Feb 2019 20:59:37 +0100 Subject: format number is not recognized in a cycle References: <14ca0f1f-7d23-47a6-9792-b24491d3c21c@googlegroups.com> Message-ID: Diego Avesani wrote: > Dear Peter, Deal all, > > Trying different options, I manage this solution: > > mask = (df['datatime'] > str(start_date[ii])) & (df['datatime'] <= > str(end_date[ii])) > > As you can notice, I have put str before start_date[ii]) and end_date[ii]. > > What do you think? Don't use your workaround unless you have ensured that you get the correct result. Comparisons between arbitrary types are dangerous in Python 2: >>> "10" > 1 True >>> "1" > 1 True >>> "0" > 1 True Perhaps you can find a better approach when you post your problem in a forum dedicated to pandas. From Gronicus at SGA.Ninja Mon Feb 4 15:20:58 2019 From: Gronicus at SGA.Ninja (Steve) Date: Mon, 4 Feb 2019 15:20:58 -0500 Subject: Python program to phone? Message-ID: <000201d4bcc7$2411d810$6c358830$@SGA.Ninja> I have written my first python program (600 lines!) to help control my blood sugar and it has been so successful that my A1c dropped form 9.3 to an impressive 6.4. It will be much more useful if I had it on my phone. (MotoG, Android) The .py file reads/writes to two txt files. About a year ago, I installed Kivy and managed to transfer the "Hello World" app to my phone and it worked. I am not on a different computer and believe that I got all the way through the installation but I do not see how to invoke it. I am sure that I can go through the tutorial again and use my program instead. How do I figure out what is wrong and might there ne a better way to get the program onto my phone? Steve P.S. Yes, I tried to post this about two weeks ago but could not seem to respond to the replies I received. I could contact one or two individuals but apparently not the masses. How do I find out how this list works? Foornote: There's 99 bugs in the code, in the code. 99 bugs in the code. Take one down and patch it all around. Now there's 117 bugs in the code. From diego.avesani at gmail.com Mon Feb 4 16:22:52 2019 From: diego.avesani at gmail.com (Diego Avesani) Date: Mon, 4 Feb 2019 13:22:52 -0800 (PST) Subject: format number is not recognized in a cycle In-Reply-To: References: Message-ID: <4f2127d8-995a-4e31-bccb-461498d55c9b@googlegroups.com> Dear all, Dear Peter, could you suggest me one? Thanks a lot, Diego On Monday, 4 February 2019 13:58:17 UTC+1, Diego Avesani wrote: > Dear all, > > I have this dataframe: > > datatime,T,RH,PSFC,DIR,VEL10,PREC,RAD,CC,FOG > 2012-01-01 06:00, 0.4,100, 911,321, 2.5, 0.0, 0, 0,0 > 2012-01-01 07:00, 0.8,100, 911,198, 0.8, 0.0, 0, 22,0 > 2012-01-01 08:00, 0.6,100, 912, 44, 1.2, 0.0, 30, 22,0 > 2012-01-01 09:00, 3.1, 76, 912, 22, 0.8, 0.0, 134, 44,0 > 2012-01-01 10:00, 3.4, 77, 912, 37, 0.5, 0.0, 191, 67,0 > 2012-01-01 11:00, 3.5,100, 912,349, 0.4, 0.0, 277, 44,0 > 2012-01-01 12:00, 3.6,100, 912, 17, 0.9, 0.0, 292, 22,0 > 2012-01-01 13:00, 3.5,100, 912, 28, 0.3, 0.0, 219, 44,0 > 2012-01-01 14:00, 3.3, 68, 912, 42, 0.5, 0.0, 151, 22,0 > > .... > .... > > I would like to plot and analyse different part of it. Consequently I have created the following variables in order to manage different period: > > start_date = np.array(['2012-01-01 06:00','2013-01-01 06:00','2014-01-01 06:00']) > end_date = np.array(['2013-01-01 05:00','2014-01-01 05:00','2015-01-01 05:00']) > > However, if I try to perform the following cycle: > > for ii in range(0, 1): > pd.read_csv('dati.csv',delimiter=',',header=0,parse_dates=True, > na_values=-999) > df['datatime'] = df['datatime'].map(lambda x: datetime.strptime(str(x), "%Y-%m-%d %H:%M")) > # > mask = (df['datatime'] > start_date[ii]) & (df['datatime'] <= end_date[ii]) > > I get the following error: > > return _get_dtype_type(np.dtype(arr_or_dtype)) > File "/usr/lib/python2.7/dist-packages/numpy/core/_internal.py", line 173, in _commastring > (len(result)+1, astr)) > ValueError: format number 1 of "2012-01-01 06:00" is not recognized > > > > On the contrary, if I use the simple variables: > start_date = 2012-01-01 06:00 > end_date = 2013-01-01 05:00 > > without the cycle, it works. > > I really do not understand why. > > Thanks a lot, > Diego From avigross at verizon.net Mon Feb 4 18:19:34 2019 From: avigross at verizon.net (Avi Gross) Date: Mon, 4 Feb 2019 18:19:34 -0500 Subject: pandas read dataframe and sum all value same month and year In-Reply-To: References: Message-ID: <005f01d4bce0$17317380$45945a80$@verizon.net> Diego, If your goal is to sum data by month, there are more general methods than you making lists of starting and ending dates. ONE way to consider in your case that is easy to understand is to add a derived column/feature to your data such as the first 7 characters of the date as perhaps a routine string. I mean 2012-01-01 06:00 Becomes: "2012-01" Once you have such a column, you can aggregate your data by that column using functionality in pandas and it will be done for all items with that field being the same. The extra column can be temporary and there are other ways but it is conceptually simple. Of course to do the above does involve lots of details and you don't seem to need it now, so I am leaving this as an academic hint. -----Original Message----- From: Python-list On Behalf Of Diego Avesani Sent: Monday, February 4, 2019 2:38 PM To: python-list at python.org Subject: Re: pandas read dataframe and sum all value same month and year Deal all, following Peter's suggestion, I put the example code: import pandas as pd import numpy as np from datetime import datetime #input: start_date = np.array(["2012-01-01 06:00",'2013-01-01 06:00','2014-01-01 06:00']) end_date = np.array(["2013-01-01 05:00",'2014-01-01 05:00','2015-01-01 05:00']) yearfolder = np.array(['2012','2013','2014']) for ii in range(0, 1): df = pd.read_csv('dati.csv',delimiter=',',header=0,parse_dates=True,na_values=-99 9) df['datatime'] = df['datatime'].map(lambda x: datetime.strptime(str(x), "%Y-%m-%d %H:%M")) mask = (df['datatime'] > str(start_date[ii])) & (df['datatime'] <= str(end_date[ii])) df = df.loc[mask] df = df.reset_index(drop=True) # df.groupby(pd.TimeGrouper('m')).sum() and the example of file: datatime,T,RH,PSFC,DIR,VEL10,PREC,RAD,CC,FOG 2012-01-01 06:00, 0.4,100, 911,321, 2.5, 0.0, 0, 0,0 2012-01-01 07:00, 0.8,100, 911,198, 0.8, 0.0, 0, 22,0 2012-01-01 08:00, 0.6,100, 912, 44, 1.2, 0.0, 30, 22,0 2012-01-01 09:00, 3.1, 76, 912, 22, 0.8, 0.0, 134, 44,0 2012-01-01 10:00, 3.4, 77, 912, 37, 0.5, 0.0, 191, 67,0 2012-01-01 11:00, 3.5,100, 912,349, 0.4, 0.0, 277, 44,0 2012-01-01 12:00, 3.6,100, 912, 17, 0.9, 0.0, 292, 22,0 2012-01-01 13:00, 3.5,100, 912, 28, 0.3, 0.0, 219, 44,0 2012-01-01 14:00, 3.3, 68, 912, 42, 0.5, 0.0, 151, 22,0 Hope this could help in finding a way to sum value belonging to the same month. Thanks again, a lot Diego On Monday, 4 February 2019 15:50:52 UTC+1, Diego Avesani wrote: > Dear all, > > I am reading the following data-frame: > > datatime,T,RH,PSFC,DIR,VEL10,PREC,RAD,CC,FOG > 2012-01-01 06:00, -0.1,100, 815,313, 2.6, 0.0, 0, 0,0 > 2012-01-01 07:00, -1.2, 93, 814,314, 4.8, 0.0, 0, 0,0 > 2012-01-01 08:00, 1.7, 68, 815,308, 7.5, 0.0, 41, 11,0 > 2012-01-01 09:00, 2.4, 65, 815,308, 7.4, 0.0, 150, 33,0 > 2012-01-01 10:00, 3.0, 64, 816,305, 8.4, 0.0, 170, 44,0 > 2012-01-01 11:00, 2.6, 65, 816,303, 6.3, 0.0, 321, 22,0 > 2012-01-01 12:00, 2.0, 72, 816,278, 1.3, 0.0, 227, 22,0 > 2012-01-01 13:00, -0.0, 72, 816,124, 0.1, 0.0, 169, 22,0 > 2012-01-01 14:00, -0.1, 68, 816,331, 1.4, 0.0, 139, 33,0 > 2012-01-01 15:00, -4.0, 85, 816,170, 0.6, 0.0, 49, 0,0 .... > .... > > I read the data frame as: > > df = pd.read_csv('dati.csv',delimiter=',',header=0,parse_dates=True,na_values=-99 9) > df['datatime'] = df['datatime'].map(lambda x: datetime.strptime(str(x), "%Y-%m-%d %H:%M")) > # > mask = (df['datatime'] > str(start_date[ii])) & (df['datatime'] <= str(end_date[ii])) > df = df.loc[mask] > df = df.reset_index(drop=True) > > I would to create an array with the sum of all the PREC value in the same month. > > I have tried with: > > df.groupby(pd.TimeGrouper('M')).sum() > > But as always, it seems that I have same problems with the indexes. Indeed, I get: > 'an instance of %r' % type(ax).__name__) > TypeError: axis must be a DatetimeIndex, but got an instance of 'Int64Index' > > thanks for any kind of help, > Really Really thanks > > Diego -- https://mail.python.org/mailman/listinfo/python-list From chris_roysmith at internode.on.net Mon Feb 4 18:12:55 2019 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Tue, 5 Feb 2019 10:12:55 +1100 Subject: Python program to phone? In-Reply-To: <000201d4bcc7$2411d810$6c358830$@SGA.Ninja> References: <000201d4bcc7$2411d810$6c358830$@SGA.Ninja> Message-ID: On 5/2/19 7:20 am, Steve wrote: > I have written my first python program (600 lines!) to help control my blood > sugar and it has been so successful that my A1c dropped form 9.3 to an > impressive 6.4. It will be much more useful if I had it on my phone. > (MotoG, Android) The .py file reads/writes to two txt files. > P.S. Yes, I tried to post this about two weeks ago but could not seem to > respond to the replies I received. I could contact one or two individuals > but apparently not the masses. How do I find out how this list works? > Hi Steve, to reply to the newsgroup use "followup" NOT "reply" Sorry I can't help you on your initial problem Regards, Chris Roy-Smith From PythonList at DancesWithMice.info Mon Feb 4 20:20:35 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Tue, 5 Feb 2019 14:20:35 +1300 Subject: Implement C's Switch in Python 3 [OT languages] In-Reply-To: References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> <954aff2d-e584-228b-9094-285095842447@DancesWithMice.info> Message-ID: <5eba3686-dfe6-9adb-7a9e-623c95de2dff@DancesWithMice.info> On 4/02/19 9:25 PM, Christian Gollwitzer wrote: > Am 04.02.19 um 09:18 schrieb Christian Gollwitzer: >> I think English is quite "unique" with writing out the ending of the >> ordinals attached to arabic numerals. >> > > Of course, there is a Wikipedia page about it: > > ????https://en.wikipedia.org/wiki/Ordinal_indicator > > So I was wrong and the abbreviated suffix is as common as the period > over the European languages. > > ????Christian That's a comprehensive read. Thanks! Left me with a few thoughts: 1 the Irish is "syncopated". No wonder that accent presents me with difficulties! 2 this is all very difficult, and 3 the OP must be re-thinking his choices (or questioning his sanity) by now! Following your earlier contribution and its use of words such as "rare", "unusual", "mess", and "unique" I wondered just how relevant the ordinal might be? Herewith a few minutes of very un-scientific/statistically-irrelevant discovery - wherein I learned that English investors are leery of Alphabet because of their rate of investment (!?) and the Portuguese are concerned that their schools' computers are aged-rubbish. I surveyed a number of the better-known European news-papers' web-sites to see what they do (under the (perhaps mistaken) assumption that they represent their country's use of dates):- Great Britain The Times (of London) February 5 2019, 12:01am, French (translations: 'published on-site at...', 'published on the...') Le Monde avec AFP Publi? hier ? 08h06, mis ? jour hier ? 12h08 Publi? le 14 janvier 2019 ? 15h04 - Mis ? jour le 14 janvier 2019 ? 16h03 Le Figaro: Publi? le 04/02/2019 ? 19:45 Germany Handelsblatt Dienstag, 5. Februar 2019 (also FAZ) Welt Stand: 01.02.2019 | Lesedauer: 4 Minuten Bild 05.02.2019 Italy la Repubblica 05 Febbraio 2019 Portugal Di?rio de Not?cias Ter?a-feira, 5 de fevereiro de 2019 Publico 4 de Fevereiro de 2019 Spain El Pa?s 5 FEB 2019 El Mundo martes, 5 de febrero de 2019 So, even with the French making their dates into sentences, not a single one uses ordinals! - did the computer people in all these languages/cultures decide that the more numeric approach was better/easier/... (ie simpler/less-complex) Not even the convention/use of title-case is consistent! After all this, I think I need to quietly go away and sleep it off... -- Regards =dn From PythonList at DancesWithMice.info Mon Feb 4 23:29:09 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Tue, 5 Feb 2019 17:29:09 +1300 Subject: Loop with else clause Message-ID: <46df1a21-f04c-d064-def1-5258480175f6@etelligence.info> What is the pythonic way to handle the situation where if a condition exists the loop should be executed, but if it does not something else should be done? Why am I asking? Today's code review included a for...else structure. I've rarely seen such a thing, and even knowing it exists, cannot recall ever using it! The coder intended to implement the scenario (above) but did not realise that the else-clause means 'execute if the loop ended without using break'. She thought it meant 'if there's nothing in the iterable, execute the else clause' (per if...then...else... ie the two clauses are mutually-exclusive*) - which one assumes is the reason why the BDfL is claimed to have said it should never have been implemented (this way). She neglected to test the exception properly, and was lulled into a false sense of security by the coverage reporting 100%. Oops! *see also the more commonly-used try...except...else...[finally...] When/how does this occur? Our client is more than a little commercially-sensitive. So as a really simple scenario, imagine a report is required, naming people who have become eligible for something, eg students qualified to enter an advanced class, Oscar film award nominees, entrants who have fulfilled the requirements of a competition from which a winner will be randomly selected... The names all appear in a list, so the most frequent use-case is trivial: print( "And the winners are:" ) for name in list: print( name ) but, if no-one actually qualifies, a warning message is required, eg print( "Sorry, no-one is eligible" ) Possible solution: To make anything more than the trivial case readable, I think I'd put the list processing into one function, and the exception into another (except that this case is so trivial), ie if list: process_list() #the heading and for-loop, as above else: print( "Sorry... Others wanted to add a semaphore/flag inside the loop to indicate if it was executed at least once. Yes, could even use the else clause then! The ideas went (rapidly) down-hill from there... Is there another, more pythonic, approach to conditional (for/while) loop processing? -- Regards, =dn From Gronicus at SGA.Ninja Tue Feb 5 02:12:16 2019 From: Gronicus at SGA.Ninja (Steve) Date: Tue, 5 Feb 2019 02:12:16 -0500 Subject: Loop with else clause In-Reply-To: <46df1a21-f04c-d064-def1-5258480175f6@etelligence.info> References: <46df1a21-f04c-d064-def1-5258480175f6@etelligence.info> Message-ID: <008201d4bd22$20bdbbb0$62393310$@SGA.Ninja> Would it be a hyphythonitical question? ================= Footnote: Zamboni locks up after running into large patch of loose teeth. -----Original Message----- From: Python-list On Behalf Of DL Neil Sent: Monday, February 4, 2019 11:29 PM To: 'Python' Subject: Loop with else clause What is the pythonic way to handle the situation where if a condition exists the loop should be executed, but if it does not something else should be done? Why am I asking? Today's code review included a for...else structure. I've rarely seen such a thing, and even knowing it exists, cannot recall ever using it! The coder intended to implement the scenario (above) but did not realise that the else-clause means 'execute if the loop ended without using break'. She thought it meant 'if there's nothing in the iterable, execute the else clause' (per if...then...else... ie the two clauses are mutually-exclusive*) - which one assumes is the reason why the BDfL is claimed to have said it should never have been implemented (this way). She neglected to test the exception properly, and was lulled into a false sense of security by the coverage reporting 100%. Oops! *see also the more commonly-used try...except...else...[finally...] When/how does this occur? Our client is more than a little commercially-sensitive. So as a really simple scenario, imagine a report is required, naming people who have become eligible for something, eg students qualified to enter an advanced class, Oscar film award nominees, entrants who have fulfilled the requirements of a competition from which a winner will be randomly selected... The names all appear in a list, so the most frequent use-case is trivial: print( "And the winners are:" ) for name in list: print( name ) but, if no-one actually qualifies, a warning message is required, eg print( "Sorry, no-one is eligible" ) Possible solution: To make anything more than the trivial case readable, I think I'd put the list processing into one function, and the exception into another (except that this case is so trivial), ie if list: process_list() #the heading and for-loop, as above else: print( "Sorry... Others wanted to add a semaphore/flag inside the loop to indicate if it was executed at least once. Yes, could even use the else clause then! The ideas went (rapidly) down-hill from there... Is there another, more pythonic, approach to conditional (for/while) loop processing? -- Regards, =dn -- https://mail.python.org/mailman/listinfo/python-list From PythonList at DancesWithMice.info Tue Feb 5 02:33:24 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Tue, 5 Feb 2019 20:33:24 +1300 Subject: Loop with else clause In-Reply-To: <008201d4bd22$20bdbbb0$62393310$@SGA.Ninja> References: <46df1a21-f04c-d064-def1-5258480175f6@etelligence.info> <008201d4bd22$20bdbbb0$62393310$@SGA.Ninja> Message-ID: <2bfa3448-6d63-ce19-1c4d-5a7294d64bbb@DancesWithMice.info> On 5/02/19 8:12 PM, Steve wrote: > Would it be a hyphythonitical question? Is that one of the new meta-classes in release 3.99, or a perhaps a project to remove the GIL and take advantage of multi-core architectures? As well as embarrassing the poor coder, this question vexed quite a few minds this afternoon (even allowing for the gathering mood - tomorrow being a public holiday!). Even though it seemed so straightforward to me, others felt it reasonable that there would be some way to tell that a loop never executed - there must be a simpler, more pythonic, construct... > ================= > Footnote: > Zamboni locks up after running into large patch of loose teeth. > -----Original Message----- > From: Python-list On > Behalf Of DL Neil > Sent: Monday, February 4, 2019 11:29 PM > To: 'Python' > Subject: Loop with else clause > > What is the pythonic way to handle the situation where if a condition exists > the loop should be executed, but if it does not something else should be > done? > > > Why am I asking? > Today's code review included a for...else structure. I've rarely seen such a > thing, and even knowing it exists, cannot recall ever using it! > The coder intended to implement the scenario (above) but did not realise > that the else-clause means 'execute if the loop ended without using break'. > She thought it meant 'if there's nothing in the iterable, execute the else > clause' (per if...then...else... ie the two clauses are > mutually-exclusive*) - which one assumes is the reason why the BDfL is > claimed to have said it should never have been implemented (this way). > She neglected to test the exception properly, and was lulled into a false > sense of security by the coverage reporting 100%. Oops! > > *see also the more commonly-used try...except...else...[finally...] > > > When/how does this occur? > Our client is more than a little commercially-sensitive. So as a really > simple scenario, imagine a report is required, naming people who have become > eligible for something, eg students qualified to enter an advanced class, > Oscar film award nominees, entrants who have fulfilled the requirements of a > competition from which a winner will be randomly selected... > > The names all appear in a list, so the most frequent use-case is trivial: > > print( "And the winners are:" ) > for name in list: > print( name ) > > but, if no-one actually qualifies, a warning message is required, eg > > print( "Sorry, no-one is eligible" ) > > > Possible solution: > To make anything more than the trivial case readable, I think I'd put the > list processing into one function, and the exception into another (except > that this case is so trivial), ie > > if list: > process_list() #the heading and for-loop, as above > else: > print( "Sorry... > > > Others wanted to add a semaphore/flag inside the loop to indicate if it was > executed at least once. Yes, could even use the else clause then! > > The ideas went (rapidly) down-hill from there... > > > Is there another, more pythonic, approach to conditional (for/while) loop > processing? > > -- > Regards, > =dn > -- > https://mail.python.org/mailman/listinfo/python-list > -- Regards =dn From ben+python at benfinney.id.au Tue Feb 5 03:12:03 2019 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 05 Feb 2019 19:12:03 +1100 Subject: Loop with else clause References: <46df1a21-f04c-d064-def1-5258480175f6@etelligence.info> Message-ID: <868syud57w.fsf@benfinney.id.au> DL Neil writes: > Possible solution: > To make anything more than the trivial case readable, I think I'd put > the list processing into one function, and the exception into another > (except that this case is so trivial), ie > > if list: > process_list() #the heading and for-loop, as above > else: > print( "Sorry... (As an aside: It's best to avoid choosing names like ?list? that clobber built-in names; your code examples are harder to read that way. I'll assume a different name, ?ipsums?.) One aspect of that example I would prefer to avoid: It scatters the handling of the list to different locations in the code. It's not obvious from the purpose of ?process_list? whether that function should be handling an empty list; this could lead to double-handling in different locations. An alternative to consider:: if ipsums: for item in ipsums: process_item(item) else: print("Sorry...") An advantage of this is that the handling of the list is all in the same place, where changing that logic later will be easier. The ?process_item? then just assumes some other code has decided which items to handle; it becomes correspondingly simpler. -- \ ?Two possibilities exist: Either we are alone in the Universe | `\ or we are not. Both are equally terrifying.? ?Arthur C. Clarke, | _o__) 1999 | Ben Finney From __peter__ at web.de Tue Feb 5 03:43:54 2019 From: __peter__ at web.de (Peter Otten) Date: Tue, 05 Feb 2019 09:43:54 +0100 Subject: Loop with else clause References: <46df1a21-f04c-d064-def1-5258480175f6@etelligence.info> Message-ID: DL Neil wrote: > What is the pythonic way to handle the situation where if a condition > exists the loop should be executed, but if it does not something else > should be done? > Possible solution: > To make anything more than the trivial case readable, I think I'd put > the list processing into one function, and the exception into another > (except that this case is so trivial), ie > > if list: > process_list() #the heading and for-loop, as above > else: > print( "Sorry... > > > Others wanted to add a semaphore/flag inside the loop to indicate if it > was executed at least once. Yes, could even use the else clause then! An argument in favour of the flag is that it works with arbitrary iterables whereas if ...: fails: >>> numbered = enumerate([]) >>> if numbered: ... print("the winners are") ... for ix in numbered: print(*ix) ... the winners are > The ideas went (rapidly) down-hill from there... > > > Is there another, more pythonic, approach to conditional (for/while) > loop processing? I'm not aware of such an approach. From mojimo2000 at gmail.com Tue Feb 5 06:28:31 2019 From: mojimo2000 at gmail.com (mojimo2000 at gmail.com) Date: Tue, 5 Feb 2019 03:28:31 -0800 (PST) Subject: python is going to die! =( In-Reply-To: References: Message-ID: <58be3d0b-d8c8-4dae-83bf-db1726a379fe@googlegroups.com> python is alive From acharbly at gmail.com Tue Feb 5 06:43:21 2019 From: acharbly at gmail.com (Prahallad Achar) Date: Tue, 5 Feb 2019 17:13:21 +0530 Subject: python is going to die! =( In-Reply-To: <58be3d0b-d8c8-4dae-83bf-db1726a379fe@googlegroups.com> References: <58be3d0b-d8c8-4dae-83bf-db1726a379fe@googlegroups.com> Message-ID: Python is the only immortal On Tue, 5 Feb 2019, 17:01 python is alive > -- > https://mail.python.org/mailman/listinfo/python-list > From arj.python at gmail.com Tue Feb 5 06:55:53 2019 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Tue, 5 Feb 2019 15:55:53 +0400 Subject: Python program to phone? In-Reply-To: <000201d4bcc7$2411d810$6c358830$@SGA.Ninja> References: <000201d4bcc7$2411d810$6c358830$@SGA.Ninja> Message-ID: see pydroid and qpython on play store.2 Abdur-Rahmaan Janhangeer http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ Mauritius From rhodri at kynesim.co.uk Tue Feb 5 06:49:52 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Tue, 5 Feb 2019 11:49:52 +0000 Subject: Python program to phone? In-Reply-To: References: <000201d4bcc7$2411d810$6c358830$@SGA.Ninja> Message-ID: On 04/02/2019 23:12, Chris Roy-Smith wrote: > On 5/2/19 7:20 am, Steve wrote: >> P.S.? Yes, I tried to post this about two weeks ago but could not seem to >> respond to the replies I received.? I could contact one or two >> individuals >> but apparently not the masses.? How do I find out how this list works? > > Hi Steve, to reply to the newsgroup use "followup" NOT "reply" If you're using the mailing list rather than the newsgroup, reply to the list (python-list at python.org) instead of the Reply-To address. Most mail programs have an appropriate reply option: on Thunderbird it's the "Reply List" button. > Sorry I can't help you on your initial problem Likewise :-( -- Rhodri James *-* Kynesim Ltd From nimbiotics at gmail.com Tue Feb 5 08:58:09 2019 From: nimbiotics at gmail.com (Mario R. Osorio) Date: Tue, 5 Feb 2019 05:58:09 -0800 (PST) Subject: Python program to phone? In-Reply-To: References: <000201d4bcc7$2411d810$6c358830$@SGA.Ninja> Message-ID: Hi there Steve. Did you check BeeWare? (https://pybee.org/) From David.Raymond at tomtom.com Tue Feb 5 09:31:20 2019 From: David.Raymond at tomtom.com (David Raymond) Date: Tue, 5 Feb 2019 14:31:20 +0000 Subject: Loop with else clause In-Reply-To: References: <46df1a21-f04c-d064-def1-5258480175f6@etelligence.info> Message-ID: As mentioned somewhere, "readability counts", so why not just go with exactly what you said... if len(nominees) > 0: #if a condition exists for nominee in nominees: #the loop should be executed print(nominee) else: #but if not print("D'oh! No one is worthy.") #something else should be done The fancy features of the language can be cool and all, but forcing yourself to use them just for the sake of using them and "being pythonic" can result in weird situations, and wrong or unclear assumptions or intents. When someone else reads it later it may hide what you were actually intending the check to look for. Hmm, "if nominees:" Did they make it like that to look for a 0 length list, or to make sure they didn't get None by accident, or both, or something different? Hmm, "if len(nominees) > 0:" Ahh ok, we want to make sure there's something in there. So my 2 cents: Feel free to be non-pythonic if it makes things clear and works. -----Original Message----- From: Python-list [mailto:python-list-bounces+david.raymond=tomtom.com at python.org] On Behalf Of Peter Otten Sent: Tuesday, February 05, 2019 3:44 AM To: python-list at python.org Subject: Re: Loop with else clause DL Neil wrote: > What is the pythonic way to handle the situation where if a condition > exists the loop should be executed, but if it does not something else > should be done? > Possible solution: > To make anything more than the trivial case readable, I think I'd put > the list processing into one function, and the exception into another > (except that this case is so trivial), ie > > if list: > process_list() #the heading and for-loop, as above > else: > print( "Sorry... > > > Others wanted to add a semaphore/flag inside the loop to indicate if it > was executed at least once. Yes, could even use the else clause then! An argument in favour of the flag is that it works with arbitrary iterables whereas if ...: fails: >>> numbered = enumerate([]) >>> if numbered: ... print("the winners are") ... for ix in numbered: print(*ix) ... the winners are > The ideas went (rapidly) down-hill from there... > > > Is there another, more pythonic, approach to conditional (for/while) > loop processing? I'm not aware of such an approach. -- https://mail.python.org/mailman/listinfo/python-list From renting at astron.nl Tue Feb 5 09:42:03 2019 From: renting at astron.nl (Adriaan Renting) Date: Tue, 05 Feb 2019 15:42:03 +0100 Subject: Loop with else clause In-Reply-To: <46df1a21-f04c-d064-def1-5258480175f6@etelligence.info> References: <46df1a21-f04c-d064-def1-5258480175f6@etelligence.info> Message-ID: <5C59A0BB0200001B00028EDB@smtp1.astron.nl> I don't know if it is very Pythonic, but I would do something like if no_oscar_nominees: print ("Sorry ...") else: print_list_of_oscar_nominees() And yes, that for/else construct can be confusing. Adriaan. >>> On 5-2-2019 at 5:29, DL Neil wrote: > What is the pythonic way to handle the situation where if a condition > exists the loop should be executed, but if it does not something else > should be done? > > > Why am I asking? > Today's code review included a for...else structure. I've rarely seen > such a thing, and even knowing it exists, cannot recall ever using it! > The coder intended to implement the scenario (above) but did not realise > that the else-clause means 'execute if the loop ended without using > break'. She thought it meant 'if there's nothing in the iterable, > execute the else clause' (per if...then...else... ie the two clauses are > mutually-exclusive*) - which one assumes is the reason why the BDfL is > claimed to have said it should never have been implemented (this way). > She neglected to test the exception properly, and was lulled into a > false sense of security by the coverage reporting 100%. Oops! > > *see also the more commonly-used try...except...else...[finally...] > > > When/how does this occur? > Our client is more than a little commercially-sensitive. So as a really > simple scenario, imagine a report is required, naming people who have > become eligible for something, eg students qualified to enter an > advanced class, Oscar film award nominees, entrants who have fulfilled > the requirements of a competition from which a winner will be randomly > selected... > > The names all appear in a list, so the most frequent use-case is trivial: > > print( "And the winners are:" ) > for name in list: > print( name ) > > but, if no-one actually qualifies, a warning message is required, eg > > print( "Sorry, no-one is eligible" ) > > > Possible solution: > To make anything more than the trivial case readable, I think I'd put > the list processing into one function, and the exception into another > (except that this case is so trivial), ie > > if list: > process_list() #the heading and for-loop, as above > else: > print( "Sorry... > > > Others wanted to add a semaphore/flag inside the loop to indicate if it > was executed at least once. Yes, could even use the else clause then! > > The ideas went (rapidly) down-hill from there... > > > Is there another, more pythonic, approach to conditional (for/while) > loop processing? From avigross at verizon.net Tue Feb 5 11:55:26 2019 From: avigross at verizon.net (Avi Gross) Date: Tue, 5 Feb 2019 11:55:26 -0500 Subject: Loop with else clause In-Reply-To: References: <46df1a21-f04c-d064-def1-5258480175f6@etelligence.info> Message-ID: <009201d4bd73$97f1c540$c7d54fc0$@verizon.net> The topic is how to deal with a python loop that may not be run if you want something else to happen in that case. Multiple solutions are presented along with this request: > Is there another, more pythonic, approach to conditional (for/while) > loop processing? Has anyone considered looking at this from the perspective of the CONDITION being evaluated? If the condition is something simple like is the "list" empty, then any method will work as it can be evaluated before trying the loop with no side effects. But what if the item in question is complex and constructed dynamically as by zipping things together or calling functions and might even produce side effects? It might be far from trivial to do that before the loop and check and you might inadvertently change things. Using a sentinel approach might be safer. And what about loops that start but exit immediately without doing anything? If you iterate on something whose first value is wrong in some way and the loop starts with an "if ... break" then do you want to do the ELSE condition because the loop sort of did not run as anticipated? A similar example is if the loop starts with a "try" and the error handler does a break. From dvl at psu.edu Tue Feb 5 17:28:29 2019 From: dvl at psu.edu (Christman, Roger Graydon) Date: Tue, 5 Feb 2019 22:28:29 +0000 Subject: Loop with Else Clause Message-ID: -----Original Message----- From: Python-list On Behalf Of DL Neil Sent: Monday, February 4, 2019 11:29 PM To: 'Python' Subject: Loop with else clause What is the pythonic way to handle the situation where if a condition exists the loop should be executed, but if it does not something else should be done? ------ Just reading this by itself without seeing the illustrative problem, my first gut reaction was "while loop!" but then it became more obvious that this was a one-off condition to decide to get started, as opposed to being the loop condition itself. That being said, I would agree that you really cannot do better than the if-else already presented, since it is clearly a binary choice: execute the complete loop, or don't if you can't. I personally try to avoid the else part on both while loops and for loops because of this sort of confusion. All other things being equal, having an else section is fundamentally the same thing as simply omitting the 'else:' and out-denting the consequence, and makes it very clear just what is going on. The difference, of course, appears when you have a 'break' within that loop that exits prematurely. And that's where the structured-programming purist in me starts to gibber in fear and loathing. My purist self says that if 'break' is applicable, than exception handling is just as applicable with a much cleaner model, since you can control where to go next, whether to continue repeating, etc. If I don't expect to get all the way through a for loop, I simply don't write a for loop -- I'll substitute with a while, moving the logical negation of the break condition into the while loop condition, making it very clear that I don't plan to make it to the end. Unfortunately, the for loop is such a common Pythonic thing to do that I really can't say 'for/break' is unPythonic -- I can only substitute Lovecraftian adjectives (in my opinion, of course) to describe that construct. But this whole little tangent of mine doesn't seem to apply to your situation at all, and I don't there is a 'single' language element in any programming language I can think of that does what you ask -- so stick with the cleaner: if (__): # loop; else: #don't loop Roger Christman Pennsylvania State University From jldsand at skybeam.com Tue Feb 5 19:25:54 2019 From: jldsand at skybeam.com (John Sanders) Date: Tue, 5 Feb 2019 16:25:54 -0800 (PST) Subject: Implement C's Switch in Python 3 In-Reply-To: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> Message-ID: On Saturday, February 2, 2019 at 6:47:49 PM UTC-6, Sayth Renshaw wrote: > Hi > > I am trying to convert a switch statement from C into Python. (why? practising). > > This is the C code. > > printf("Dated this %d", day); > switch (day) { > case 1: case 21: case 31: > printf("st"); break; > case 2: case 22: > printf("nd"); break; > case 3: case 23: > printf("rd"); break; > default: printf("th"); break; > > } > printf(" day of "); > > #Premise if the use enter an int as the date 21 for example it would print 21st. It appends the correct suffix onto a date. > > Reading and trying to implement a function that uses a dictionary. Not sure how to supply list into it to keep it brief and with default case of 'th'. > > This is my current code. > > def f(x): > return { > [1, 21, 31]: "st", > [2, 22]: "nd", > [3, 23]: "rd", > }.get(x, "th") > > > print(f(21)) > > I have an unhashable type list. Whats the best way to go? > > Cheers > > Sayth My best, readable attempt good for 1 - 99: def sufx( day ): if (day % 10) in [0,4,5,6,7,8,9] or (day / 10) == 1: return 'th' return {1: 'st', 2: 'nd', 3: 'rd'}[day % 10] From python at mrabarnett.plus.com Tue Feb 5 19:48:43 2019 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 6 Feb 2019 00:48:43 +0000 Subject: Implement C's Switch in Python 3 In-Reply-To: References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> Message-ID: On 2019-02-06 00:25, John Sanders wrote: > On Saturday, February 2, 2019 at 6:47:49 PM UTC-6, Sayth Renshaw wrote: >> Hi >> >> I am trying to convert a switch statement from C into Python. (why? practising). >> >> This is the C code. >> >> printf("Dated this %d", day); >> switch (day) { >> case 1: case 21: case 31: >> printf("st"); break; >> case 2: case 22: >> printf("nd"); break; >> case 3: case 23: >> printf("rd"); break; >> default: printf("th"); break; >> >> } >> printf(" day of "); >> >> #Premise if the use enter an int as the date 21 for example it would print 21st. It appends the correct suffix onto a date. >> >> Reading and trying to implement a function that uses a dictionary. Not sure how to supply list into it to keep it brief and with default case of 'th'. >> >> This is my current code. >> >> def f(x): >> return { >> [1, 21, 31]: "st", >> [2, 22]: "nd", >> [3, 23]: "rd", >> }.get(x, "th") >> >> >> print(f(21)) >> >> I have an unhashable type list. Whats the best way to go? >> >> Cheers >> >> Sayth > > My best, readable attempt good for 1 - 99: > > def sufx( day ): > if (day % 10) in [0,4,5,6,7,8,9] or (day / 10) == 1: > return 'th' > return {1: 'st', 2: 'nd', 3: 'rd'}[day % 10] > In Python 3, 11/10 == 1.1, not 1. From alister.ware at ntlworld.com Wed Feb 6 05:32:25 2019 From: alister.ware at ntlworld.com (Alister) Date: Wed, 06 Feb 2019 10:32:25 GMT Subject: Python program to phone? References: <000201d4bcc7$2411d810$6c358830$@SGA.Ninja> Message-ID: On Mon, 04 Feb 2019 15:20:58 -0500, Steve wrote: > I have written my first python program (600 lines!) to help control my > blood sugar and it has been so successful that my A1c dropped form 9.3 > to an impressive 6.4. It will be much more useful if I had it on my > phone. (MotoG, Android) The .py file reads/writes to two txt files. > > > > About a year ago, I installed Kivy and managed to transfer the "Hello > World" > app to my phone and it worked. I am not on a different computer and > believe that I got all the way through the installation but I do not see > how to invoke it. I am sure that I can go through the tutorial again > and use my program instead. > > > > How do I figure out what is wrong and might there ne a better way to get > the program onto my phone? > > > > Steve > > > > P.S. Yes, I tried to post this about two weeks ago but could not seem > to respond to the replies I received. I could contact one or two > individuals but apparently not the masses. How do I find out how this > list works? > > > > > > Foornote: > There's 99 bugs in the code, in the code. > > 99 bugs in the code. > > Take one down and patch it all around. > > Now there's 117 bugs in the code. as a fellow diabetic I have to say well done we use a different measure in the uk but i have managed to drop from an initial 107 (11.9) down to arround 39 (5.7) 6.4 is 46 which is still below the diabetic star point of 48 keep up the good work, sorry cant help with the phone question -- If you are honest because honesty is the best policy, your honesty is corrupt. From PythonList at DancesWithMice.info Thu Feb 7 03:42:17 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Thu, 7 Feb 2019 21:42:17 +1300 Subject: Loop with else clause In-Reply-To: <009201d4bd73$97f1c540$c7d54fc0$@verizon.net> References: <46df1a21-f04c-d064-def1-5258480175f6@etelligence.info> <009201d4bd73$97f1c540$c7d54fc0$@verizon.net> Message-ID: Further to our discussion of how to improve a code review's discovery of the mistaken handling of a for...else... construct:- Yesterday was a national holiday, but today gave some opportunity to research. Way back in 2009 there was spirited discussion over on the Python Ideas list (warning, even the mailing list's index covers multiple screen-lengths): - this confusion is not new by any measure, herewith a list of previous occasions "fists were raised concerning for..else." https://mail.python.org/pipermail/python-ideas/2009-October/006164.html - an excellent summary of the 2009 debate which offers no less than six ways to 'improve' for... else... https://mail.python.org/pipermail/python-ideas/2009-October/006155.html - (as mentioned earlier) the BDFL weighed-in a couple of times. His regret is: "That's a flaw, and I don't quite know what to do about it. It's about 20 years too late to remove or rename it. But we probably should not do more of these. That's a lesson." (OK, so make that thirty years - older than the coder who the code-review noticed falling into this coding 'gotcha'!) https://mail.python.org/pipermail/python-ideas/2009-October/006083.html - herewith a (rather complicated) suggestion, and critique https://mail.python.org/pipermail/python-ideas/2009-October/006054.html - one rather hopeful option (actual words to be used notwithstanding) for i in SEQ: A except: B else: C appears here: https://mail.python.org/pipermail/python-ideas/2009-October/006044.html Somewhat related, PEP 548 proposed an "More Flexible Loop Control". This was addressing the confusion caused by break within a loop. It was rejected. Each of the above addresses issues 'within', that is to say happenings during iteration - whether the entire loop or iteration cut-short by a break (and thus the idea that "else" might be re-worded to indicate 'after a break'). However, as mentioned by one contributor, the specific use-case our team faced was an issue that arises prior to the loop. Alternately-expressed: that according to Python's logic, prevents even a single iteration of that loop. Thus, any 'solution' would reside outside of for and while statements because they only consider if a loop should continue or terminate - not handling the question of whether it should start at all! PEP 315 is the only discussion (I've found) which looks 'outside' or 'before' the loop itself. It proposed an "Enhanced While Loop", attempting to separate 'setup' or loop control from loop content. It was rejected. So, reading-around brought nothing much useful. Back to the code-face... Thank you to the several folk who responded with ideas to express/improve: if list: process_list() #the heading and for-loop, as above else: print( "Sorry... NB this is a constructed 'toy example' attempting to be the shortest illustration of use-cases and invented purely to communicate the need and structure. It was expected to be interpreted as pseudo-python-code. (you'd not use/allow "process_list" as the name of a function, would you?) (With apologies as necessary) one of the dangers of 'toy examples' is the reader taking them at face value, instead of as (over-)simplified illustrations. In 'real life' the loop code and the no-loop exception are both considerably longer than a single line. Accordingly, using a function would be a good way to summarise and self-document the activity, ie the if statement's two code-blocks would make the whole statement very/too-long (readability)! The "if list:" expression is overly-simplistic. The recommendation of "if len(list):" is absolutely sound, for reasons of polymorphism. In-lieu of a Python construct, there are definitely situations when use of a sentinel makes better sense. However, given their risk, in many ways Python tries to avoid using such, eg consuming iterators until a StopIteration exception is returned. (includes files, is subsumed by ContextManagers...), thus "pythonic". That said, the classic use of for... else... is in searching for a particular element within an iterable which has all the hallmarks of a "sentinel". Today, kicking ideas around, I coded three other possible 'solutions' for the review team's discussions: One of these involves coding three functions: the decision (yielding a boolean), the expected-case, and the unusual-case. The satisfaction in this was readability factors with a simple if statement.. Somewhat more complex, and I feel a bit OTT, was to sub-class list() and write a method which would indicate an empty list, plus include much of both the expected-case and the empty-list methods therein. Somehow "class ListShouldNotBeEmpty" doesn't seem a catchy (if descriptive) title - however, it works! The decision 'function' could also/then be made @property and (perhaps) thus contribute to readability. Lastly, I 'remembered' conditional expressions. Thus, furthering the idea of two/three functions: process_list() if list_has_contents() \ else process_empty_list() Report card: Functional = yes. Pythonic = yes. Readable = much debated/relates to the expertise of the coder/reader Your thoughts? -- Regards =dn From gabriele1NOSPAM at hotmail.com Thu Feb 7 06:29:44 2019 From: gabriele1NOSPAM at hotmail.com (^Bart) Date: Thu, 7 Feb 2019 12:29:44 +0100 Subject: The sum of ten numbers inserted from the user Message-ID: I thought something like it but doesn't work... for n in range(1, 11): x = input("Insert a number: ") for y in range(x): sum = y print ("The sum is: ",y) From joel.goldstick at gmail.com Thu Feb 7 06:50:31 2019 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 7 Feb 2019 06:50:31 -0500 Subject: The sum of ten numbers inserted from the user In-Reply-To: References: Message-ID: On Thu, Feb 7, 2019 at 6:31 AM ^Bart wrote: > > I thought something like it but doesn't work... > > for n in range(1, 11): > x = input("Insert a number: ") The above, keeps replacing x with each input value. You don't want that. Think about appending the input value to a list > > for y in range(x): > sum = y > Here again you are replacing sum with each value of y. And think further about what the value of y is for each iteration. > print ("The sum is: ",y) You should really be asking this sort of question in the python-tutor mailing list. You should also use print function in your loops to learn what is really going on. > -- > https://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From lezen at vanwingerde.nl Wed Feb 6 09:27:06 2019 From: lezen at vanwingerde.nl (Jaap van Wingerde) Date: Wed, 6 Feb 2019 15:27:06 +0100 Subject: timezones Message-ID: <20190206152706.1128013c@jaap.custard.shrl.nl> I made a small script to practise with timezones: #!/usr/bin/env python3 # -*- coding: utf_8 -*- from datetime import datetime, timedelta from pytz import timezone import pytz amsterdam_datetime = datetime(2018, 12, 17, 11, 31, 26, tzinfo=timezone('Europe/Amsterdam')) print(amsterdam_datetime) utc_datetime = amsterdam_datetime.astimezone(pytz.utc) print(utc_datetime) amsterdam_datetime = datetime(2018, 6, 17, 11, 31, 26, tzinfo=timezone('Europe/Amsterdam')) print(amsterdam_datetime) utc_datetime = amsterdam_datetime.astimezone(pytz.utc) print(utc_datetime) The output of the script is: 2018-12-17 11:31:26+00:20 2018-12-17 11:11:26+00:00 2018-06-17 11:31:26+00:20 2018-06-17 11:11:26+00:00 I respected: 2018-12-17 11:31:26+01:00 2018-12-17 10:31:26+00:00 2018-06-17 11:31:26+02:00 2018-06-17 09:31:26+00:00 I need this functionality for adjusting wrong timestamps in Android JPG-images as the 'Exif.GPSInfo.GPSTimeStamp' and 'Exif.GPSInfo.GPSDateStamp' are missing. Why I get this unrespected results? Kind regards, Jaap. -- Jaap van Wingerde e-mail: 1234567890 at vanwingerde.nl From auriocus at gmx.de Tue Feb 5 03:22:12 2019 From: auriocus at gmx.de (Christian Gollwitzer) Date: Tue, 5 Feb 2019 09:22:12 +0100 Subject: Implement C's Switch in Python 3 [OT languages] In-Reply-To: References: <9af572e7-63e7-43e4-a1d9-a4725b00f59a@googlegroups.com> <20190203013106.GA84610@cskk.homeip.net> <20e49114-372a-4836-aab7-45be628fbe46@googlegroups.com> <004501d4bb6b$57b743b0$0725cb10$@verizon.net> <93f85971-7a1a-5820-5aa0-19997217b8f1@mrabarnett.plus.com> <004c01d4bb70$ddf15920$99d40b60$@verizon.net> <73a1c64c-7fb1-4fc8-98a2-b6939e82a0b7@googlegroups.com> <434a9d1c-137d-cdeb-5980-9f462d520160@DancesWithMice.info> <954aff2d-e584-228b-9094-285095842447@DancesWithMice.info> <5eba3686-dfe6-9adb-7a9e-623c95de2dff@DancesWithMice.info> Message-ID: Am 05.02.19 um 02:20 schrieb DL Neil: > So, even with the French making their dates into sentences, not a single > one uses ordinals! > - did the computer people in all these languages/cultures decide that > the more numeric approach was better/easier/... > (ie simpler/less-complex) :) For the two languages I know well, German (mother tongue) and Czech (studied for 5 years, 1 year abroad in Prague) I can assure you that the numeric ordinals were not dicated by the computer people, but that's been the typographic tradition since ever. You write the number with a period, also for dates. In regular sentences, something like "he achieved the eigth placement in a tournament" it depends on the size of the number, below ten or twelve one usually spells it out (with inflection and everything) and above it's typically written with numbers and a period. But I read a funny story at the Czech language instutute's page about the influence of MS Word on Czech spelling. In dates, the name of the month is written in lowercase "1. prosince". Howver, MS Word thinks that the period is a full-stop which starts a new sentence, and it autocorrects it to "1. Prosince". This has happened so often that they brought it up as a special topic, that this autocorrection is wrong. > Not even the convention/use of title-case is consistent! Title case? In German, there are intricate rules about the capitalization in regular sentences; there is no such thing as title case. Only very rarely ALL CAPS are used. Have a good sleep ;) Christian From David.Raymond at tomtom.com Thu Feb 7 12:56:21 2019 From: David.Raymond at tomtom.com (David Raymond) Date: Thu, 7 Feb 2019 17:56:21 +0000 Subject: timezones In-Reply-To: <20190206152706.1128013c@jaap.custard.shrl.nl> References: <20190206152706.1128013c@jaap.custard.shrl.nl> Message-ID: I'd say if the documentation mentions it, but doesn't say why, then we're not gonna be able to do much better for you as far as "why" goes. http://pytz.sourceforge.net/ "Unfortunately using the tzinfo argument of the standard datetime constructors "does not work" with pytz for many timezones." But it looks like they suggest something along the lines of... timezone('Europe/Amsterdam').localize(datetime(2018, 12, 17, 11, 31, 26)) From the examples on http://pytz.sourceforge.net/#problems-with-localtime ... >>> eastern = timezone('US/Eastern') >>> fmt = '%Y-%m-%d %H:%M:%S %Z%z' >>> loc_dt = datetime(2002, 10, 27, 1, 30, 00) >>> est_dt = eastern.localize(loc_dt, is_dst=True) >>> edt_dt = eastern.localize(loc_dt, is_dst=False) >>> print(est_dt.strftime(fmt) + ' / ' + edt_dt.strftime(fmt)) 2002-10-27 01:30:00 EDT-0400 / 2002-10-27 01:30:00 EST-0500 Browse through their examples and see if you can find something similar that works for you. -----Original Message----- From: Python-list [mailto:python-list-bounces+david.raymond=tomtom.com at python.org] On Behalf Of Jaap van Wingerde Sent: Wednesday, February 06, 2019 9:27 AM To: python-list at python.org Subject: timezones I made a small script to practise with timezones: #!/usr/bin/env python3 # -*- coding: utf_8 -*- from datetime import datetime, timedelta from pytz import timezone import pytz amsterdam_datetime = datetime(2018, 12, 17, 11, 31, 26, tzinfo=timezone('Europe/Amsterdam')) print(amsterdam_datetime) utc_datetime = amsterdam_datetime.astimezone(pytz.utc) print(utc_datetime) amsterdam_datetime = datetime(2018, 6, 17, 11, 31, 26, tzinfo=timezone('Europe/Amsterdam')) print(amsterdam_datetime) utc_datetime = amsterdam_datetime.astimezone(pytz.utc) print(utc_datetime) The output of the script is: 2018-12-17 11:31:26+00:20 2018-12-17 11:11:26+00:00 2018-06-17 11:31:26+00:20 2018-06-17 11:11:26+00:00 I respected: 2018-12-17 11:31:26+01:00 2018-12-17 10:31:26+00:00 2018-06-17 11:31:26+02:00 2018-06-17 09:31:26+00:00 I need this functionality for adjusting wrong timestamps in Android JPG-images as the 'Exif.GPSInfo.GPSTimeStamp' and 'Exif.GPSInfo.GPSDateStamp' are missing. Why I get this unrespected results? Kind regards, Jaap. -- Jaap van Wingerde e-mail: 1234567890 at vanwingerde.nl -- https://mail.python.org/mailman/listinfo/python-list From Joseph.Schachner at Teledyne.com Thu Feb 7 13:18:49 2019 From: Joseph.Schachner at Teledyne.com (Schachner, Joseph) Date: Thu, 7 Feb 2019 18:18:49 +0000 Subject: The sum of ten numbers inserted from the user In-Reply-To: References: Message-ID: <66bad16981b94c1caf3e9d7c0c34a9e1@Teledyne.com> Well of course that doesn't work. For starters, x is an int or a float value. After the loop It holds the 10th value. It might hold 432.7 ... It is not a list. The default start for range is 0. The stop value, as you already know, is not part of the range. So I will use range(10). In the second loop, I think you thought x would be a list and you I'm sure you wanted to do for y in x: but instead you did for y in range(x) and remember x might be a very large number. So the second loop would loop that many times, and each pass it would assign y (which has first value of 0 and last value of whatever x-1 is) to sum. Even though its name is "sum" it is not a sum. After the loop it would hold x-1. You wanted to do sum += y. Or sum = sum + y. I prefer the former, but that could reflect my history as a C and C++ programmer. And then you printed y, but you really want to print sum (assuming that sum was actually the sum). Putting all of the above together, you want to do this: vallist =[] # an empty list, so we can append to it for n in range(10): x = input("Insert a number: ") # get 1 number into x vallist.append(x) # append it to our list # Now vallist holds 10 values sum = 0 # No need to start sum as a float, in Python if we add a float to an integer the result will be float. It doesn't matter in the program if sum is int or float. for y in vallist: sum += y print( "The sum is: ", sum) -----Original Message----- From: ^Bart Sent: Thursday, February 7, 2019 6:30 AM To: python-list at python.org Subject: The sum of ten numbers inserted from the user I thought something like it but doesn't work... for n in range(1, 11): x = input("Insert a number: ") for y in range(x): sum = y print ("The sum is: ",y) From ianhclark510 at gmail.com Thu Feb 7 13:27:28 2019 From: ianhclark510 at gmail.com (Ian Clark) Date: Thu, 7 Feb 2019 10:27:28 -0800 Subject: The sum of ten numbers inserted from the user In-Reply-To: <66bad16981b94c1caf3e9d7c0c34a9e1@Teledyne.com> References: <66bad16981b94c1caf3e9d7c0c34a9e1@Teledyne.com> Message-ID: This is my whack at it, I can't wait to hear about it being the wrong big o notation! numbers=[] while len(numbers) < 10: try: chip = int(input('please enter an integer: ')) except ValueError: print('that is not a number, try again') else: numbers.append(chip) print(sum(numbers)) On Thu, Feb 7, 2019 at 10:23 AM Schachner, Joseph < Joseph.Schachner at teledyne.com> wrote: > Well of course that doesn't work. For starters, x is an int or a float > value. After the loop It holds the 10th value. It might hold 432.7 ... > It is not a list. > The default start for range is 0. The stop value, as you already know, is > not part of the range. So I will use range(10). > > In the second loop, I think you thought x would be a list and you I'm sure > you wanted to do > for y in x: > but instead you did > for y in range(x) > and remember x might be a very large number. So the second loop would > loop that many times, and each pass it would assign y (which has first > value of 0 and last value of whatever x-1 is) to sum. Even though its name > is "sum" it is not a sum. After the loop it would hold x-1. You wanted to > do sum += y. Or sum = sum + y. I prefer the former, but that could > reflect my history as a C and C++ programmer. > > And then you printed y, but you really want to print sum (assuming that > sum was actually the sum). > > Putting all of the above together, you want to do this: > > vallist =[] # an empty list, so we can append to it > for n in range(10): > x = input("Insert a number: ") # get 1 number into x > vallist.append(x) # append it to our list > > # Now vallist holds 10 values > > sum = 0 # No need to start sum as a float, in Python if we add a float > to an integer the result will be float. It doesn't matter in the program if > sum is int or float. > for y in vallist: > sum += y > > print( "The sum is: ", sum) > > > > -----Original Message----- > From: ^Bart > Sent: Thursday, February 7, 2019 6:30 AM > To: python-list at python.org > Subject: The sum of ten numbers inserted from the user > > I thought something like it but doesn't work... > > for n in range(1, 11): > x = input("Insert a number: ") > > for y in range(x): > sum = y > > print ("The sum is: ",y) > > -- > https://mail.python.org/mailman/listinfo/python-list > From Joseph.Schachner at Teledyne.com Thu Feb 7 14:17:45 2019 From: Joseph.Schachner at Teledyne.com (Schachner, Joseph) Date: Thu, 7 Feb 2019 19:17:45 +0000 Subject: The sum of ten numbers inserted from the user In-Reply-To: References: <66bad16981b94c1caf3e9d7c0c34a9e1@Teledyne.com> Message-ID: <2ecde35a89454e1593d5e83dff480b54@Teledyne.com> I just realized that input has changed in Python 3 and I was using Python 2.7.13 with from __future__ import print_function and some others, but not that. In Python 3 the int( ) or float( ) cast is necessary because input( ) does what raw_input( ) did in Python 2; raw_input( ) name is therefore removed. --- Joe S. From: Ian Clark Sent: Thursday, February 7, 2019 1:27 PM To: Schachner, Joseph Cc: python-list at python.org Subject: Re: The sum of ten numbers inserted from the user This is my whack at it, I can't wait to hear about it being the wrong big o notation! numbers=[] while len(numbers) < 10: try: chip = int(input('please enter an integer: ')) except ValueError: print('that is not a number, try again') else: numbers.append(chip) print(sum(numbers)) On Thu, Feb 7, 2019 at 10:23 AM Schachner, Joseph > wrote: Well of course that doesn't work. For starters, x is an int or a float value. After the loop It holds the 10th value. It might hold 432.7 ... It is not a list. The default start for range is 0. The stop value, as you already know, is not part of the range. So I will use range(10). In the second loop, I think you thought x would be a list and you I'm sure you wanted to do for y in x: but instead you did for y in range(x) and remember x might be a very large number. So the second loop would loop that many times, and each pass it would assign y (which has first value of 0 and last value of whatever x-1 is) to sum. Even though its name is "sum" it is not a sum. After the loop it would hold x-1. You wanted to do sum += y. Or sum = sum + y. I prefer the former, but that could reflect my history as a C and C++ programmer. And then you printed y, but you really want to print sum (assuming that sum was actually the sum). Putting all of the above together, you want to do this: vallist =[] # an empty list, so we can append to it for n in range(10): x = input("Insert a number: ") # get 1 number into x vallist.append(x) # append it to our list # Now vallist holds 10 values sum = 0 # No need to start sum as a float, in Python if we add a float to an integer the result will be float. It doesn't matter in the program if sum is int or float. for y in vallist: sum += y print( "The sum is: ", sum) -----Original Message----- From: ^Bart > Sent: Thursday, February 7, 2019 6:30 AM To: python-list at python.org Subject: The sum of ten numbers inserted from the user I thought something like it but doesn't work... for n in range(1, 11): x = input("Insert a number: ") for y in range(x): sum = y print ("The sum is: ",y) -- https://mail.python.org/mailman/listinfo/python-list From ben.usenet at bsb.me.uk Thu Feb 7 15:45:58 2019 From: ben.usenet at bsb.me.uk (Ben Bacarisse) Date: Thu, 07 Feb 2019 20:45:58 +0000 Subject: The sum of ten numbers inserted from the user References: <66bad16981b94c1caf3e9d7c0c34a9e1@Teledyne.com> Message-ID: <87imxve395.fsf@bsb.me.uk> Ian Clark writes: > This is my whack at it, I can't wait to hear about it being the wrong big o > notation! > > numbers=[] > > while len(numbers) < 10: > try: > chip = int(input('please enter an integer: ')) > except ValueError: > print('that is not a number, try again') > else: > numbers.append(chip) > > print(sum(numbers)) Why would you not keep a running total, rather than list of the numbers? I've not been following assiduously, so maybe I missed some other requirement... -- Ben. From grant.b.edwards at gmail.com Thu Feb 7 16:11:10 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Thu, 7 Feb 2019 21:11:10 -0000 (UTC) Subject: The sum of ten numbers inserted from the user References: <66bad16981b94c1caf3e9d7c0c34a9e1@Teledyne.com> <87imxve395.fsf@bsb.me.uk> Message-ID: On 2019-02-07, Ben Bacarisse wrote: > Ian Clark writes: > >> This is my whack at it, I can't wait to hear about it being the wrong big o >> notation! >> >> numbers=[] >> >> while len(numbers) < 10: >> try: >> chip = int(input('please enter an integer: ')) >> except ValueError: >> print('that is not a number, try again') >> else: >> numbers.append(chip) >> >> print(sum(numbers)) > > Why would you not keep a running total, rather than list of the numbers? > I've not been following assiduously, so maybe I missed some other > requirement... Because you think it likely that tomorrow the marketing people are going to say "oh, and we want to know the min, max, mode, median, standard deviation, and count of odd vs. even. OTOH, they may instead say, "Oh, it's not 10 numbers, it's 10 million numbers." -- Grant Edwards grant.b.edwards Yow! I selected E5 ... but at I didn't hear "Sam the Sham gmail.com and the Pharoahs"! From tdldev at gmail.com Thu Feb 7 17:27:09 2019 From: tdldev at gmail.com (Jack Dangler) Date: Thu, 7 Feb 2019 17:27:09 -0500 Subject: Python program to phone? In-Reply-To: <000201d4bcc7$2411d810$6c358830$@SGA.Ninja> References: <000201d4bcc7$2411d810$6c358830$@SGA.Ninja> Message-ID: On 2/4/19 3:20 PM, Steve wrote: > I have written my first python program (600 lines!) to help control my blood > sugar and it has been so successful that my A1c dropped form 9.3 to an > impressive 6.4. It will be much more useful if I had it on my phone. > (MotoG, Android) The .py file reads/writes to two txt files. > > > > About a year ago, I installed Kivy and managed to transfer the "Hello World" > app to my phone and it worked. I am not on a different computer and believe > that I got all the way through the installation but I do not see how to > invoke it. I am sure that I can go through the tutorial again and use my > program instead. > > > > How do I figure out what is wrong and might there ne a better way to get the > program onto my phone? > > > > Steve > > > > P.S. Yes, I tried to post this about two weeks ago but could not seem to > respond to the replies I received. I could contact one or two individuals > but apparently not the masses. How do I find out how this list works? > > > > > > Foornote: > There's 99 bugs in the code, in the code. > > 99 bugs in the code. > > Take one down and patch it all around. > > Now there's 117 bugs in the code. > > > Steve I'm not sure but I think there is an Android lib for py that is available to help convert your app to a mobile platform. Also, if you're of a mind, I'd love to see the code you wrote for blood sugar. I need to lower my A1C as well and could use all the help I can get. Regards Jack From ben.usenet at bsb.me.uk Thu Feb 7 18:56:45 2019 From: ben.usenet at bsb.me.uk (Ben Bacarisse) Date: Thu, 07 Feb 2019 23:56:45 +0000 Subject: The sum of ten numbers inserted from the user References: <66bad16981b94c1caf3e9d7c0c34a9e1@Teledyne.com> <87imxve395.fsf@bsb.me.uk> Message-ID: <87imxvcfuq.fsf@bsb.me.uk> Grant Edwards writes: > On 2019-02-07, Ben Bacarisse wrote: >> Ian Clark writes: >> >>> This is my whack at it, I can't wait to hear about it being the wrong big o >>> notation! >>> >>> numbers=[] >>> >>> while len(numbers) < 10: >>> try: >>> chip = int(input('please enter an integer: ')) >>> except ValueError: >>> print('that is not a number, try again') >>> else: >>> numbers.append(chip) >>> >>> print(sum(numbers)) >> >> Why would you not keep a running total, rather than list of the numbers? >> I've not been following assiduously, so maybe I missed some other >> requirement... > > Because you think it likely that tomorrow the marketing people are > going to say "oh, and we want to know the min, max, mode, median, > standard deviation, and count of odd vs. even. > > OTOH, they may instead say, "Oh, it's not 10 numbers, it's 10 million > numbers." Sure. I can think of reasons for all sorts of designs. Has there been any hint? It looks like a beginners' exercise with little extra to go on. -- Ben. From ben.usenet at bsb.me.uk Thu Feb 7 18:56:56 2019 From: ben.usenet at bsb.me.uk (Ben Bacarisse) Date: Thu, 07 Feb 2019 23:56:56 +0000 Subject: The sum of ten numbers inserted from the user References: <66bad16981b94c1caf3e9d7c0c34a9e1@Teledyne.com> <87imxve395.fsf@bsb.me.uk> Message-ID: <87ftszcfuf.fsf@bsb.me.uk> Bart writes: > On 07/02/2019 20:45, Ben Bacarisse wrote: >> Ian Clark writes: >> >>> This is my whack at it, I can't wait to hear about it being the wrong big o >>> notation! >>> >>> numbers=[] >>> >>> while len(numbers) < 10: >>> try: >>> chip = int(input('please enter an integer: ')) >>> except ValueError: >>> print('that is not a number, try again') >>> else: >>> numbers.append(chip) >>> >>> print(sum(numbers)) >> >> Why would you not keep a running total, rather than list of the numbers? >> I've not been following assiduously, so maybe I missed some other >> requirement... > > Because it separates the two tasks: (A) capture N values; (B) perform > some operation on those values, in this case summing (and taking > advantage here of the built-in sum()). Sure. Did I miss some hint that this program might need to be more flexible like that? It looks like a beginners' exercise. If the purpose is to be flexible, why is an array the right option rather than, say, accumulating a value over an iterable? You need some idea of the future direction or what the exercise is intended to reinforce. -- Ben. From Gronicus at SGA.Ninja Thu Feb 7 23:00:52 2019 From: Gronicus at SGA.Ninja (Steve) Date: Thu, 7 Feb 2019 23:00:52 -0500 Subject: Python program to phone? In-Reply-To: References: <000201d4bcc7$2411d810$6c358830$@SGA.Ninja> Message-ID: <003701d4bf62$e3215620$a9640260$@SGA.Ninja> BeeWare looks as if it requires Java, does it? Is it exclusively java? ================= Footnote: Zamboni locks up after running into large patch of loose teeth. -----Original Message----- From: Python-list On Behalf Of Mario R. Osorio Sent: Tuesday, February 5, 2019 8:58 AM To: python-list at python.org Subject: Re: Python program to phone? Hi there Steve. Did you check BeeWare? (https://pybee.org/) -- https://mail.python.org/mailman/listinfo/python-list From torriem at gmail.com Thu Feb 7 23:42:27 2019 From: torriem at gmail.com (Michael Torrie) Date: Thu, 7 Feb 2019 21:42:27 -0700 Subject: Python program to phone? In-Reply-To: <003701d4bf62$e3215620$a9640260$@SGA.Ninja> References: <000201d4bcc7$2411d810$6c358830$@SGA.Ninja> <003701d4bf62$e3215620$a9640260$@SGA.Ninja> Message-ID: <6be9e2eb-d2b4-0c65-644a-e4923b22a1bf@gmail.com> On 02/07/2019 09:00 PM, Steve wrote: > BeeWare looks as if it requires Java, does it? > Is it exclusively java? Kind of. You do your coding in Python, then that's compiled to python byte code, which is then translated to Java byte code. You'll need the Android SDK, even if you're not using Java directly. Unfortunately Google no longer lets you download just the SDK by itself. Instead you have to download the entire 1GB Android Studio. I haven't done anything with PyBee, but it looks promising for your purposes. Instead of using text files, you'll probably need to use sqlite data stores. Not a huge deal if PyBee abstracts this for you. Recently I made an android app using V-play.net, which is built on QtQuick. Basically it was all javascript. This might work for your app also. It's not Python, but the learning curve isn't huge. It took me far longer to figure out the GUI layout than it did to figure out the Javascript. From kusalakalani at gmail.com Fri Feb 8 01:45:07 2019 From: kusalakalani at gmail.com (Kaka) Date: Thu, 7 Feb 2019 22:45:07 -0800 (PST) Subject: how to exit from a nested loop in python Message-ID: <50bffdd2-fb84-45f7-ba98-a32415ef443c@googlegroups.com> for i in range(len(A.hp)): for j in range(len(run_parameters.bits_Mod)): req_slots[j] = math.ceil((A.T[i]) for g in Temp[i]["Available_ranges"][j]: for s in range(g[0], g[-1]): if (s+req_slots[j]-1) <= g[-1]: if (Temp[i]['cost'][j] <= (run_parameters.PSD): ------ When this condition is true i want to break the nested loop and start from the begining served_count +=1 A.T[i]["First_index"]= s A.T[i]["Last_index"]= s+req_slots[j]-1 A.T[i]["Status"]= 1 A.T[i]["Selected_MOD"] = j break I have this code. When the last "if" condition is satisfied, i want to break all the loops and start the nested loop for next i. else, the loop should continue. can anyone help me? From __peter__ at web.de Fri Feb 8 03:31:41 2019 From: __peter__ at web.de (Peter Otten) Date: Fri, 08 Feb 2019 09:31:41 +0100 Subject: how to exit from a nested loop in python References: <50bffdd2-fb84-45f7-ba98-a32415ef443c@googlegroups.com> Message-ID: Kaka wrote: > for i in range(len(A.hp)): > > for j in range(len(run_parameters.bits_Mod)): > req_slots[j] = math.ceil((A.T[i]) > > for g in Temp[i]["Available_ranges"][j]: > for s in range(g[0], g[-1]): > if (s+req_slots[j]-1) <= g[-1]: > if (Temp[i]['cost'][j] <= (run_parameters.PSD): ------ > When this condition is true i want to break the nested > loop and start from the begining > served_count +=1 > A.T[i]["First_index"]= s > A.T[i]["Last_index"]= s+req_slots[j]-1 > A.T[i]["Status"]= 1 > A.T[i]["Selected_MOD"] = j > break Thou shalt indent four spaces. One shalt thou not indent excepting that thou then proceed to four. > I have this code. When the last "if" condition is satisfied, i want to > break all the loops and start the nested loop for next i. else, the loop > should continue. > > can anyone help me? Option 1: Collaps your inner loops into a generator: gen(a): for b in ...: for c in ...: yield a, b, c for a in ...: for b, c in gen(a): if f(a, b, c): ... break Option 2: If your code is too messy for option 1, use a helper function: def process(a): for b in ...: for c in ...: if f(a, b, c): ... return for a in ...: process(a) From gabriele1NOSPAM at hotmail.com Fri Feb 8 03:58:07 2019 From: gabriele1NOSPAM at hotmail.com (^Bart) Date: Fri, 8 Feb 2019 09:58:07 +0100 Subject: The sum of ten numbers inserted from the user References: Message-ID: > x = 0 > for jnk in range(10): > x += int(input("Enter a number: ") > print(x) It works, there's a missed ) A colleague did: total=0 for n in range(10): n= int(input("Enter a number: ")) total=total+n print(total) I understood your code is more clean! ^Bart From renting at astron.nl Fri Feb 8 07:30:11 2019 From: renting at astron.nl (Adriaan Renting) Date: Fri, 08 Feb 2019 13:30:11 +0100 Subject: Loop with else clause In-Reply-To: References: <46df1a21-f04c-d064-def1-5258480175f6@etelligence.info> <009201d4bd73$97f1c540$c7d54fc0$@verizon.net> Message-ID: <5C5D76530200001B000290A0@smtp1.astron.nl> Wow, you dug deep. My example was the reverse of the "toy example"'s you mention as I find that often code becomes much clearer if you order it such that specific cases, sanity checking and exceptions go first, and then the default case at the end. So my general suggestion would be to handle your empty list/no valid candidates cases first, and then do the for loop at the end. If you style this with if/elif/else, or put return statements in certain places, I have no opinion about and will depend on the specific problem. Cheers, Adriaan Renting. >>> On 7-2-2019 at 9:42, DL Neil wrote: > Further to our discussion of how to improve a code review's discovery of > the mistaken handling of a for...else... construct:- > > > Yesterday was a national holiday, but today gave some opportunity to > research. Way back in 2009 there was spirited discussion over on the > Python Ideas list (warning, even the mailing list's index covers > multiple screen-lengths): > > - this confusion is not new by any measure, herewith a list of previous > occasions "fists were raised concerning for..else." > https://mail.python.org/pipermail/python-ideas/2009-October/006164.html > > - an excellent summary of the 2009 debate which offers no less than six > ways to 'improve' for... else... > https://mail.python.org/pipermail/python-ideas/2009-October/006155.html > > - (as mentioned earlier) the BDFL weighed-in a couple of times. His > regret is: "That's a flaw, and I don't quite know what to do about it. > It's about 20 years too late to remove or rename it. But we probably > should not do more of these. That's a lesson." > (OK, so make that thirty years - older than the coder who the > code-review noticed falling into this coding 'gotcha'!) > https://mail.python.org/pipermail/python-ideas/2009-October/006083.html > > - herewith a (rather complicated) suggestion, and critique > https://mail.python.org/pipermail/python-ideas/2009-October/006054.html > > - one rather hopeful option (actual words to be used notwithstanding) > for i in SEQ: > A > except: > B > else: > C > appears here: > https://mail.python.org/pipermail/python-ideas/2009-October/006044.html > > > Somewhat related, PEP 548 proposed an "More Flexible Loop Control". This > was addressing the confusion caused by break within a loop. It was rejected. > > > Each of the above addresses issues 'within', that is to say happenings > during iteration - whether the entire loop or iteration cut-short by a > break (and thus the idea that "else" might be re-worded to indicate > 'after a break'). > > However, as mentioned by one contributor, the specific use-case our team > faced was an issue that arises prior to the loop. Alternately-expressed: > that according to Python's logic, prevents even a single iteration of > that loop. Thus, any 'solution' would reside outside of for and while > statements because they only consider if a loop should continue or > terminate - not handling the question of whether it should start at all! > > PEP 315 is the only discussion (I've found) which looks 'outside' or > 'before' the loop itself. It proposed an "Enhanced While Loop", > attempting to separate 'setup' or loop control from loop content. It was > rejected. > > > So, reading-around brought nothing much useful. Back to the code-face... > > Thank you to the several folk who responded with ideas to express/improve: > > if list: > process_list() #the heading and for-loop, as above > else: > print( "Sorry... > > NB this is a constructed 'toy example' attempting to be the shortest > illustration of use-cases and invented purely to communicate the need > and structure. It was expected to be interpreted as pseudo-python-code. > (you'd not use/allow "process_list" as the name of a function, would you?) > > (With apologies as necessary) one of the dangers of 'toy examples' is > the reader taking them at face value, instead of as (over-)simplified > illustrations. In 'real life' the loop code and the no-loop exception > are both considerably longer than a single line. Accordingly, using a > function would be a good way to summarise and self-document the > activity, ie the if statement's two code-blocks would make the whole > statement very/too-long (readability)! > > The "if list:" expression is overly-simplistic. The recommendation of > "if len(list):" is absolutely sound, for reasons of polymorphism. > > In-lieu of a Python construct, there are definitely situations when use > of a sentinel makes better sense. However, given their risk, in many > ways Python tries to avoid using such, eg consuming iterators until a > StopIteration exception is returned. (includes files, is subsumed by > ContextManagers...), thus "pythonic". That said, the classic use of > for... else... is in searching for a particular element within an > iterable which has all the hallmarks of a "sentinel". > > > Today, kicking ideas around, I coded three other possible 'solutions' > for the review team's discussions: > > One of these involves coding three functions: the decision (yielding a > boolean), the expected-case, and the unusual-case. The satisfaction in > this was readability factors with a simple if statement.. > > Somewhat more complex, and I feel a bit OTT, was to sub-class list() and > write a method which would indicate an empty list, plus include much of > both the expected-case and the empty-list methods therein. Somehow > "class ListShouldNotBeEmpty" doesn't seem a catchy (if descriptive) > title - however, it works! The decision 'function' could also/then be > made @property and (perhaps) thus contribute to readability. > > Lastly, I 'remembered' conditional expressions. Thus, furthering the > idea of two/three functions: > > process_list() if list_has_contents() \ > else process_empty_list() > > Report card: > Functional = yes. > Pythonic = yes. > Readable = much debated/relates to the expertise of the coder/reader > > > Your thoughts? From lezen at vanwingerde.nl Fri Feb 8 10:07:22 2019 From: lezen at vanwingerde.nl (Jaap van Wingerde) Date: Fri, 8 Feb 2019 16:07:22 +0100 Subject: timezones In-Reply-To: References: <20190206152706.1128013c@jaap.custard.shrl.nl> Message-ID: <20190208160722.5f0e54d1@jaap.custard.shrl.nl> The blog of Paul Gansele [1] made me trying an other approach. #!/usr/bin/env python3 # -*- coding: utf_8 -*- ### tested with python3.5 from datetime import datetime, timedelta, timezone from dateutil import tz amsterdam = tz.gettz('Europe/Amsterdam') utc = tz.gettz('utc') amsterdam_datetime = datetime(2018, 12, 17, 11, 31, 26, tzinfo=amsterdam) print(amsterdam_datetime) tuple = amsterdam_datetime.utctimetuple() utc_datetime = datetime(tuple[0], tuple[1], tuple[2], tuple[3], tuple[4], tuple[5]) print(utc_datetime) amsterdam_datetime = datetime(2018, 6, 17, 11, 31, 26, tzinfo=amsterdam) print(amsterdam_datetime) tuple = amsterdam_datetime.utctimetuple() utc_datetime = datetime(tuple[0], tuple[1], tuple[2], tuple[3], tuple[4], tuple[5]) print(utc_datetime) Output: 2018-12-17 11:31:26+01:00 2018-12-17 10:31:26 2018-06-17 11:31:26+02:00 2018-06-17 09:31:26 [1] pytz: The Fastest Footgun in the West 2018-03-19 https://blog.ganssle.io/articles/2018/03/pytz-fastest-footgun.html Op 2019-02-07T17:56:21+0000 schreef David Raymond in bericht , inzake: het volgende. > I'd say if the documentation mentions it, but doesn't say why, then > we're not gonna be able to do much better for you as far as "why" > goes. > > http://pytz.sourceforge.net/ > > "Unfortunately using the tzinfo argument of the standard datetime > constructors "does not work" with pytz for many timezones." > > But it looks like they suggest something along the lines of... > > timezone('Europe/Amsterdam').localize(datetime(2018, 12, 17, 11, 31, > 26)) > > > From the examples on > http://pytz.sourceforge.net/#problems-with-localtime ... > > >>> eastern = timezone('US/Eastern') > >>> fmt = '%Y-%m-%d %H:%M:%S %Z%z' > >>> loc_dt = datetime(2002, 10, 27, 1, 30, 00) > >>> est_dt = eastern.localize(loc_dt, is_dst=True) > >>> edt_dt = eastern.localize(loc_dt, is_dst=False) > >>> print(est_dt.strftime(fmt) + ' / ' + edt_dt.strftime(fmt)) > 2002-10-27 01:30:00 EDT-0400 / 2002-10-27 01:30:00 EST-0500 > > > Browse through their examples and see if you can find something > similar that works for you. > > > -----Original Message----- > From: Python-list > [mailto:python-list-bounces+david.raymond=tomtom.com at python.org] On > Behalf Of Jaap van Wingerde Sent: Wednesday, February 06, 2019 9:27 > AM To: python-list at python.org Subject: timezones > > I made a small script to practise with timezones: > > #!/usr/bin/env python3 > # -*- coding: utf_8 -*- > from datetime import datetime, timedelta > from pytz import timezone > import pytz > amsterdam_datetime = datetime(2018, 12, 17, 11, 31, 26, > tzinfo=timezone('Europe/Amsterdam')) > print(amsterdam_datetime) > utc_datetime = amsterdam_datetime.astimezone(pytz.utc) > print(utc_datetime) > amsterdam_datetime = datetime(2018, 6, 17, 11, 31, 26, > tzinfo=timezone('Europe/Amsterdam')) > print(amsterdam_datetime) > utc_datetime = amsterdam_datetime.astimezone(pytz.utc) > print(utc_datetime) > > The output of the script is: > 2018-12-17 11:31:26+00:20 > 2018-12-17 11:11:26+00:00 > 2018-06-17 11:31:26+00:20 > 2018-06-17 11:11:26+00:00 > > I respected: > 2018-12-17 11:31:26+01:00 > 2018-12-17 10:31:26+00:00 > 2018-06-17 11:31:26+02:00 > 2018-06-17 09:31:26+00:00 > > I need this functionality for adjusting wrong timestamps in Android > JPG-images as the 'Exif.GPSInfo.GPSTimeStamp' and > 'Exif.GPSInfo.GPSDateStamp' are missing. > > Why I get this unrespected results? > > Kind regards, > Jaap. From nimbiotics at gmail.com Fri Feb 8 11:06:30 2019 From: nimbiotics at gmail.com (Mario R. Osorio) Date: Fri, 8 Feb 2019 11:06:30 -0500 Subject: Python program to phone? In-Reply-To: <003701d4bf62$e3215620$a9640260$@SGA.Ninja> References: <000201d4bcc7$2411d810$6c358830$@SGA.Ninja> <003701d4bf62$e3215620$a9640260$@SGA.Ninja> Message-ID: You will need to have java. BeeWare's VOC tool, a transpiler from python to java, will do all the work for you so you don't even have know anything about java, except installing and setting it up for your environment Dtb/Gby ======= Mario R. Osorio B.A.S. of Information Technology A.S. of Computer Programming and Analysis Web page: *http;//mario.osorio.solutions * Email: *mario at osorio.solutions* *Just Choose Python!* ?If I had asked people what they wanted, they would have said faster horses.? ? Henry Ford On Thu, Feb 7, 2019 at 11:00 PM Steve wrote: > BeeWare looks as if it requires Java, does it? > Is it exclusively java? > > ================= > Footnote: > Zamboni locks up after running into large patch of loose teeth. > > -----Original Message----- > From: Python-list On > Behalf Of Mario R. Osorio > Sent: Tuesday, February 5, 2019 8:58 AM > To: python-list at python.org > Subject: Re: Python program to phone? > > > Hi there Steve. Did you check BeeWare? (https://pybee.org/) > > -- > https://mail.python.org/mailman/listinfo/python-list > > From nimbiotics at gmail.com Fri Feb 8 11:08:32 2019 From: nimbiotics at gmail.com (Mario R. Osorio) Date: Fri, 8 Feb 2019 11:08:32 -0500 Subject: Python program to phone? In-Reply-To: References: <000201d4bcc7$2411d810$6c358830$@SGA.Ninja> <003701d4bf62$e3215620$a9640260$@SGA.Ninja> Message-ID: I am not an expert in BeeWare (I've never used it) but I've read a good portion of their documentation and find it very interesting to say the least. I am looking forward using it in the very near future. On Fri, Feb 8, 2019 at 11:06 AM Mario R. Osorio wrote: > You will need to have java. BeeWare's VOC tool, a transpiler from python > to java, will do all the work for you so you don't even have know anything > about java, except installing and setting it up for your environment > > Dtb/Gby > ======= > Mario R. Osorio > B.A.S. of Information Technology > A.S. of Computer Programming and Analysis > Web page: *http;//mario.osorio.solutions > * > Email: *mario at osorio.solutions* > *Just Choose Python!* > > ?If I had asked people what they wanted, they would have said faster > horses.? > ? Henry Ford > > > > > > > > > On Thu, Feb 7, 2019 at 11:00 PM Steve wrote: > >> BeeWare looks as if it requires Java, does it? >> Is it exclusively java? >> >> ================= >> Footnote: >> Zamboni locks up after running into large patch of loose teeth. >> >> -----Original Message----- >> From: Python-list On >> Behalf Of Mario R. Osorio >> Sent: Tuesday, February 5, 2019 8:58 AM >> To: python-list at python.org >> Subject: Re: Python program to phone? >> >> >> Hi there Steve. Did you check BeeWare? (https://pybee.org/) >> >> -- >> https://mail.python.org/mailman/listinfo/python-list >> >> From rurpy at yahoo.com Fri Feb 8 11:15:17 2019 From: rurpy at yahoo.com (rurpy at yahoo.com) Date: Fri, 8 Feb 2019 08:15:17 -0800 (PST) Subject: how to exit from a nested loop in python In-Reply-To: <50bffdd2-fb84-45f7-ba98-a32415ef443c@googlegroups.com> References: <50bffdd2-fb84-45f7-ba98-a32415ef443c@googlegroups.com> Message-ID: <10d10c86-2c9a-4460-99e9-fde4777ab0fa@googlegroups.com> On Thursday, February 7, 2019 at 11:45:23 PM UTC-7, Kaka wrote: > for i in range(len(A.hp)): > > for j in range(len(run_parameters.bits_Mod)): > req_slots[j] = math.ceil((A.T[i]) > > for g in Temp[i]["Available_ranges"][j]: > for s in range(g[0], g[-1]): > if (s+req_slots[j]-1) <= g[-1]: > if (Temp[i]['cost'][j] <= (run_parameters.PSD): ------ When this condition is true i want to break the nested loop and start from the begining > served_count +=1 > A.T[i]["First_index"]= s > A.T[i]["Last_index"]= s+req_slots[j]-1 > A.T[i]["Status"]= 1 > A.T[i]["Selected_MOD"] = j > break > > I have this code. When the last "if" condition is satisfied, i want to break all the loops and start the nested loop for next i. else, the loop should continue. > > can anyone help me? In addition to the two methods Peter posted there is also the obvious way of using a flag (but perhaps this is what you were trying to avoid?) for i in (1,2,3): nexti = False for g in (1,2,3): for s in (1,2,3): print (i,g,s) if (g,s) == (2,2): nexti = True break if nexti: break You can also use exceptions to do a multi-level break: class Next_i (Exception): pass for i in (1,2,3): try: for g in (1,2,3): for s in (1,2,3): print (i,g,s) if (g,s) == (2,2): raise Next_i except Next_i: continue From auriocus at gmx.de Sat Feb 9 04:23:05 2019 From: auriocus at gmx.de (Christian Gollwitzer) Date: Sat, 9 Feb 2019 10:23:05 +0100 Subject: The sum of ten numbers inserted from the user In-Reply-To: References: Message-ID: Am 08.02.19 um 09:58 schrieb ^Bart: > A colleague did: > > total=0 > for n in range(10): > > ??? n= int(input("Enter a number: ")) Here, you are reusing "n" for two different things: 1. The loop index, indicating which number you ask for 2. The number entered from the user This is avery bad thing. Christian From piet-l at vanoostrum.org Sat Feb 9 08:29:57 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Sat, 09 Feb 2019 14:29:57 +0100 Subject: The slash "/" as used in the documentation References: Message-ID: ram at zedat.fu-berlin.de (Stefan Ram) writes: > The slash ?/? as used in the documentation > > f( x, /, y ) > > is so ugly, it will disappear. Especially since it consumes > a comma as it it was a parameter itself. > > Possible alternatives include: > > A newline: > > f( x, > y ) > > A separate specification of the first non-positional parameter: > > f( x, y ) > # y > > (use "(" or ")" when all parameters are non-positional or > positional, respectively). > > A semicolon: > > f( x; y ) > > (the actual call still would use a comma there). > What are you talking about? What documentation? It seems to me you are talking about a completely different programming language, not python. -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From auriocus at gmx.de Sat Feb 9 13:25:00 2019 From: auriocus at gmx.de (Christian Gollwitzer) Date: Sat, 9 Feb 2019 19:25:00 +0100 Subject: The slash "/" as used in the documentation In-Reply-To: References: Message-ID: Am 09.02.19 um 14:40 schrieb Stefan Ram: > Piet van Oostrum writes: >> ram at zedat.fu-berlin.de (Stefan Ram) writes: >>> The slash ?/? as used in the documentation >>> f( x, /, y ) >> What are you talking about? What documentation? It seems to >> me you are talking about a completely different programming >> language, not python. > > |>>> __import__( 'sys' ).version > |'3.7.0 ~~~' > |>>> help( __import__( 'math' ).sin ) > |Help on built-in function sin in module math: > | > |sin(x, /) > | Return the sine of x (measured in radians). > > ~~~ = a part of the transcript that was omitted by me Is this a new thing in 3.7? Because here I get: Apfelkiste:Tcl2018 chris$ python3 Python 3.6.1 |Anaconda 4.4.0 (x86_64)| (default, May 11 2017, 13:04:09) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> __import__( 'sys' ).version '3.6.1 |Anaconda 4.4.0 (x86_64)| (default, May 11 2017, 13:04:09) \n[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]' >>> help( __import__( 'math' ).sin ) Help on built-in function sin in module math: sin(...) sin(x) Return the sine of x (measured in radians). The help is actually not written out to the terminal but, displayed in the pager (less), and the first sin(...) is typeset in boldface. Christian From tjreedy at udel.edu Sat Feb 9 14:06:30 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 9 Feb 2019 14:06:30 -0500 Subject: The slash "/" as used in the documentation In-Reply-To: References: Message-ID: On 2/9/2019 8:29 AM, Piet van Oostrum wrote: > ram at zedat.fu-berlin.de (Stefan Ram) writes: >> The slash ?/? as used in the documentation >> >> f( x, /, y ) >> >> is so ugly, it will disappear. Especially since it consumes >> a comma as it it was a parameter itself. >> >> Possible alternatives include: >> >> A newline: >> >> f( x, >> y ) >> >> A separate specification of the first non-positional parameter: >> >> f( x, y ) >> # y >> >> (use "(" or ")" when all parameters are non-positional or >> positional, respectively). >> >> A semicolon: >> >> f( x; y ) >> >> (the actual call still would use a comma there). '/' is no uglier than, and directly analogous to, and as easy to produce and comprehend, as '*'. It was selected after considerable discussion of how to indicate that certain parameters are, at least in CPython, positional only. The discussion of options included at least some of those given above. It is very unlikely to go away or be changed. > What are you talking about? What documentation? CPython has an internal 'Argument Clinic' function that is gradually being applied to more and more builtin C-coded functions. (It requires a slight rewrite of the function code.) It adds a __text_signature__ attribute to the function object. The text signature includes '/' when appropriate. For instance >>> dict.fromkeys.__text_signature__ '($type, iterable, value=None, /)' inspect.signature creates an inspect.Signature object from this attribute. The __str__ method turns it back to the text needed for display of the signature. >>> s = inspect.signature(dict.fromkeys) >>> type(s) >>> str(s) '(iterable, value=None, /)' The interactive help function uses this text. >>> help(dict.fromkeys) ... fromkeys(iterable, value=None, /) method ... IDLE tool tips present the same signature string. AFAIK, '/' is not yet used in the reference docs. Perhaps the reason is that limiting arg passing to position is a CPython limitation rather than a language requirement. (AFAIK, pypy could allow iterable and value to be passed by keyword.) The help function is not part of the language definition, so I think the CPython version can document the CPython builtins. The dict doc in the library reference has classmethod fromkeys(iterable[, value]) The default value for 'value' is buried in the entry because 'value=None' in the signature, without a following '/', would falsely imply that 'value=0' in a call should work. There is no indication of the limitation. > It seems to me you are talking about a completely different programming language, not python. Documentation is not the language. Whether to allow '/' in def statements signatures has been discussed. The conclusion so far is that the C limitation is not needed in Python itself. -- Terry Jan Reedy From piet-l at vanoostrum.org Sat Feb 9 14:10:40 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Sat, 09 Feb 2019 20:10:40 +0100 Subject: The slash "/" as used in the documentation References: Message-ID: Christian Gollwitzer writes: >>>> __import__( 'sys' ).version > '3.6.1 |Anaconda 4.4.0 (x86_64)| (default, May 11 2017, 13:04:09) \n[GCC > 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]' >>>> help( __import__( 'math' ).sin ) > > > Help on built-in function sin in module math: > > sin(...) > sin(x) > > Return the sine of x (measured in radians). > > > The help is actually not written out to the terminal but, displayed in > the pager (less), and the first sin(...) is typeset in boldface. > > Christian Even simpler: >>> help(len) help(len) Help on built-in function len in module builtins: len(obj, /) Return the number of items in a container. both in python 3.6 and 3.7. This is weird. -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From tjreedy at udel.edu Sat Feb 9 14:18:32 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 9 Feb 2019 14:18:32 -0500 Subject: The sum of ten numbers inserted from the user In-Reply-To: References: Message-ID: On 2/9/2019 4:23 AM, Christian Gollwitzer wrote: > Am 08.02.19 um 09:58 schrieb ^Bart: >> A colleague did: >> >> total=0 >> for n in range(10): >> >> ???? n= int(input("Enter a number: ")) > > Here, you are reusing "n" for two different things: > 1. The loop index, indicating which number you ask for > 2. The number entered from the user > > This is avery bad thing. It is not as bad as "int = int(input('Number? '))" -- Terry Jan Reedy From kiyimbagodfrey at outlook.com Fri Feb 8 04:37:20 2019 From: kiyimbagodfrey at outlook.com (Kiyimba Godfrey) Date: Fri, 8 Feb 2019 09:37:20 +0000 Subject: My appreciation for python's great work to my country. Message-ID: I take this opportunity to thank python for having designed such a wonderful program which has enabled computer scientists and programmers in my country Uganda, understand, design and create other computer application programs . Yours, Lubega Tom Sent from Mail for Windows 10 From tjreedy at udel.edu Sat Feb 9 15:16:32 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 9 Feb 2019 15:16:32 -0500 Subject: The slash "/" as used in the documentation In-Reply-To: References: Message-ID: On 2/9/2019 2:10 PM, Piet van Oostrum wrote: > Christian Gollwitzer writes: > >>>>> __import__( 'sys' ).version >> '3.6.1 |Anaconda 4.4.0 (x86_64)| (default, May 11 2017, 13:04:09) \n[GCC >> 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]' >>>>> help( __import__( 'math' ).sin ) >> >> >> Help on built-in function sin in module math: >> >> sin(...) >> sin(x) >> >> Return the sine of x (measured in radians). >> >> >> The help is actually not written out to the terminal but, displayed in >> the pager (less), and the first sin(...) is typeset in boldface. >> >> Christian > > Even simpler: > >>>> help(len) > help(len) > Help on built-in function len in module builtins: > > len(obj, /) > Return the number of items in a container. > > both in python 3.6 and 3.7. > > This is weird. This is the result of Python being a project of mostly unpaid volunteers. See my response in this thread explaining how '/' appears in help output and IDLE calltips. '/' only appears for CPython C-coded functions that have been modified to use Argument Clinic. A.C. was added, I believe, in 3.5. Simple builtins like len would have been the first to be converted. The math module was converted for 3.7. There are some new conversions for 3.8 and likely some will be left for future versions. -- Terry Jan Reedy From tjreedy at udel.edu Sat Feb 9 15:31:00 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 9 Feb 2019 15:31:00 -0500 Subject: My appreciation for python's great work to my country. In-Reply-To: References: Message-ID: On 2/8/2019 4:37 AM, Kiyimba Godfrey wrote: > I take this opportunity to thank python for having designed such a wonderful program which has enabled computer scientists and programmers in my country Uganda, understand, design and create other computer application programs . You're welcome, and I appreciate your comment. I donate time to development of Python and IDLE *because* they are particularly useful to beginners and people in less developed countries. -- Terry Jan Reedy From PythonList at DancesWithMice.info Sat Feb 9 17:52:18 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Sun, 10 Feb 2019 11:52:18 +1300 Subject: Loop with else clause In-Reply-To: <5C5D76530200001B000290A0@smtp1.astron.nl> References: <46df1a21-f04c-d064-def1-5258480175f6@etelligence.info> <009201d4bd73$97f1c540$c7d54fc0$@verizon.net> <5C5D76530200001B000290A0@smtp1.astron.nl> Message-ID: Possibly the final contribution:- On 9/02/19 1:30 AM, Adriaan Renting wrote: > > Wow, you dug deep. =Thank you. It started to 'bug' me, so I really was 'scratching an itch'! > My example was the reverse of the "toy example"'s you mention as I find > that often code becomes much clearer if you order it such that > specific cases, sanity checking and exceptions go first, and then the > default case at the end. =we have also had this discussion during code reviews. Do you think that choice/code sequence/prioritisation is related to one's facility with TDD? (some might claim it is to do with experience and/or professionalism) =My observation is that the TDD-people tend to do it 'your way', but those who 'first get it working' and then 'go looking for potential trouble' do the action-first, and exceptions-later. =presumption: that the TDD people have thought more about definitions of 'working' and 'failing' and like to implement all the 'fails' (get them 'out of the way') in order to leave the 'pure' solution. (no, let's not disappear too deeply down another 'rabbit hole'!) > So my general suggestion would be to handle your empty list/no valid > candidates cases first, > and then do the for loop at the end. > > If you style this with if/elif/else, or put return statements in > certain places, I have no opinion about and will depend on the specific > problem. ... =I offered the three ideas to the original coder of the actual application:- >> Today, kicking ideas around, I coded three other possible 'solutions' > >> for the review team's discussions: >> >> One of these involves coding three functions: the decision (yielding > a >> boolean), the expected-case, and the unusual-case. The satisfaction > in >> this was readability factors with a simple if statement.. >> >> Somewhat more complex, and I feel a bit OTT, was to sub-class list() > and >> write a method which would indicate an empty list, plus include much > of >> both the expected-case and the empty-list methods therein. Somehow >> "class ListShouldNotBeEmpty" doesn't seem a catchy (if descriptive) >> title - however, it works! The decision 'function' could also/then be > >> made @property and (perhaps) thus contribute to readability. >> >> Lastly, I 'remembered' conditional expressions. Thus, furthering the > >> idea of two/three functions: >> >> process_list() if list_has_contents() \ >> else process_empty_list() =she has grabbed the class construct and started to 'run with it'. At first I feared that I had 'created a monster'. However, once she'd improved my outline all manner of other code was filling-in the class. It turns-out that the zero-data consideration was not the only way in which this data/list varies from the standard. So, she is now monitoring how many entries are expected on the list, cf how many have been captured, and is planning to evaluate them with a sort & key=. =Thus my solution, to sub-class from list and (only) modify __getitem__(), ie the "lazy" approach, has been changed to monitor the creation of the list. She no longer uses anything obvious such as len() but is keeping track with several booleans: 1 that there is actually some data, 2 how many elements are in the list, 3 how many elements are required for the application. (perhaps someone who favored semaphores is going to say "ahah!"?) =Using the 'toy' example of the film Oscar awards: 1. is exactly as discussed 'here' 2. the traditional use of len() 3. only the top four nominees are mentioned (thus the sort, enables a 'cursor' featuring only the first four elements), before moving into the grand "and the winner is..." (the film receiving the most votes ie element[ 0 ] ) =Am almost looking forward to the follow-up code review... Many thanks (to all) for contributing ideas and advice! -- Regards =dn From PythonList at DancesWithMice.info Sat Feb 9 17:59:35 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Sun, 10 Feb 2019 11:59:35 +1300 Subject: how to exit from a nested loop in python In-Reply-To: <50bffdd2-fb84-45f7-ba98-a32415ef443c@googlegroups.com> References: <50bffdd2-fb84-45f7-ba98-a32415ef443c@googlegroups.com> Message-ID: <65ba402a-50f4-24d5-1ef1-a07f9a190478@DancesWithMice.info> On 8/02/19 7:45 PM, Kaka wrote: > for i in range(len(A.hp)): > > for j in range(len(run_parameters.bits_Mod)): > req_slots[j] = math.ceil((A.T[i]) > > for g in Temp[i]["Available_ranges"][j]: > for s in range(g[0], g[-1]): > if (s+req_slots[j]-1) <= g[-1]: > if (Temp[i]['cost'][j] <= (run_parameters.PSD): ------ When this condition is true i want to break the nested loop and start from the begining > served_count +=1 > A.T[i]["First_index"]= s > A.T[i]["Last_index"]= s+req_slots[j]-1 > A.T[i]["Status"]= 1 > A.T[i]["Selected_MOD"] = j > break > > I have this code. When the last "if" condition is satisfied, i want to break all the loops and start the nested loop for next i. else, the loop should continue. Put the nest inside a try...except block and raise an exception (illustrated in many web refs) -- Regards =dn From barry at barrys-emacs.org Sun Feb 10 06:59:16 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Sun, 10 Feb 2019 11:59:16 +0000 Subject: C API version of str(exception) is not the same as pure python version Message-ID: <28195810.4nJTtaLN7d@varric.chelsea.private> When I use the C API to get the str() of an execption I see this text: But python reports the following for the same exception: TypeError: unsupported operand type(s) for +: 'int' and 'str' What do I need to do in the C API to get the the same text for the exception? All the code I'm using is below. I have been debugging this on Fedora 29 with python 3.7.2. To repoduce the problem put the three files in a folder and run: $ py3 setup.py build $ PYTHONPATH=build/lib.linux-x86_64-3.7 python3 simple_test.py Which will output: Error: Traceback (most recent call last): File "simple_test.py", line 7, in simple.test_error() File "simple_test.py", line 5, in simple_eval return eval( arg ) File "", line 1, in TypeError: unsupported operand type(s) for +: 'int' and 'str' ---- setup.py --- import os os.environ['CFLAGS'] = "-O0 -g" from distutils.core import setup, Extension setup(name = 'simple', version = '1.0', ext_modules = [Extension('simple', ['simple.c'])]) --- simple.c --- #include #include // Yes I know the ref counts are not decremented static PyObject *test_error(PyObject *self, PyObject *args) { PyObject *m = PyImport_AddModule( "__main__" ); PyObject *m_dict = PyModule_GetDict( m ); PyObject *func_args = PyTuple_New( 1 ); PyObject *str = PyUnicode_FromString( "1 + 'q'" ); PyTuple_SetItem( func_args, 0, str); PyObject *func = PyMapping_GetItemString( m_dict, "simple_eval" ); assert( func != 0 ); PyObject *result = PyObject_CallObject( func, func_args ); PyObject *err = PyErr_Occurred(); if( err != 0 ) { PyObject *u_why = PyObject_Str( err ); PyObject *b_why = PyUnicode_AsEncodedString( u_why, "utf-8", "strict" ); int size = PyBytes_Size( b_why ); char *why = PyBytes_AsString( b_why ); printf("Error: %*s\n", size, why ); return 0; } return result; } static PyMethodDef myMethods[] = { { "test_error", test_error, METH_NOARGS, "test eval with error" }, { NULL, NULL, 0, NULL } }; static struct PyModuleDef simple = { PyModuleDef_HEAD_INIT, "simple", "Simple Module", -1, myMethods }; // Initializes our module using our above struct PyMODINIT_FUNC PyInit_simple(void) { return PyModule_Create(&simple); } --- simple_test.py --- import sys import simple def simple_eval( arg ): return eval( arg ) simple.test_error() --- end -- Barry From barry at barrys-emacs.org Sun Feb 10 07:08:18 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Sun, 10 Feb 2019 12:08:18 +0000 Subject: C API PyObject_GetAttrString returns not the object I expected Message-ID: <7432942.9YVUmlyeYX@varric.chelsea.private> After calling PyObject_GetAttrString() I expected to get a PyObject string back but I found that I had been given a instead. (gdb) p *args_o $4 = What is going on and how do I get from the to the object I want? Barry From phil at riverbankcomputing.com Sun Feb 10 08:22:34 2019 From: phil at riverbankcomputing.com (Phil Thompson) Date: Sun, 10 Feb 2019 13:22:34 +0000 Subject: C API PyObject_GetAttrString returns not the object I expected In-Reply-To: <7432942.9YVUmlyeYX@varric.chelsea.private> References: <7432942.9YVUmlyeYX@varric.chelsea.private> Message-ID: <4C850D38-FA3F-407B-BFE2-099940E8FAC8@riverbankcomputing.com> On 10 Feb 2019, at 12:08 pm, Barry Scott wrote: > > After calling PyObject_GetAttrString() I expected to get a PyObject string > back but I found that I had been given a instead. > > (gdb) p *args_o > $4 = > > What is going on and how do I get from the to the object I > want? The "String" part of "PyObject_GetAttrString()" refers to the way you provide the attribute name and not the type of the object returned. Phil From stefan_ml at behnel.de Sun Feb 10 08:58:57 2019 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sun, 10 Feb 2019 14:58:57 +0100 Subject: C API PyObject_GetAttrString returns not the object I expected In-Reply-To: <7432942.9YVUmlyeYX@varric.chelsea.private> References: <7432942.9YVUmlyeYX@varric.chelsea.private> Message-ID: Barry Scott schrieb am 10.02.19 um 13:08: > After calling PyObject_GetAttrString() I expected to get a PyObject string > back but I found that I had been given a instead. > > (gdb) p *args_o > $4 = > > What is going on and how do I get from the to the object I > want? Phil is right about the function itself, but my guess is that you called GetAttr() on a class instead of an instance. Read up on Python descriptors to understand what difference that makes. https://docs.python.org/3/howto/descriptor.html Basically, you got something like a property object back, but not the value that the property maintaines. If you look up the attribute on the instance, the property (or descriptor) will hand it to you. The same applies to method lookups and other special attributes that may also be implemented as descriptors. Also take a look at Cython, which is designed to keep users from having to learn all these things and instead lets you do them in Python. https://cython.org/ Stefan From gisdudester at gmail.com Sun Feb 10 10:05:19 2019 From: gisdudester at gmail.com (GISDude) Date: Sun, 10 Feb 2019 07:05:19 -0800 (PST) Subject: print console out to a txt or csv file Message-ID: <536f3c4b-1d7c-43a2-88f1-a1df8cc15820@googlegroups.com> Hi all, I have been working on some code to list the files of a folder that has .pdf extension. I have the basic code to create a list, but it prints that list to the console. I'd like my code to write a txt file that contains that list (to later import into excel). ###A script to list pdf files in a folder ###gisdude 02/09/19 ### import os, sys, glob, csv, pathlib ###First, let's change the dir with os os.chdir("C:\\Users\\Randy\\Documents\\BOOKS") ###Second, let's now get the list of files in this directory files = glob.glob('*.pdf') for file in glob.glob("*.pdf"): print (file) ###This prints to the IDLE console ###But, I want to print to a csv file ####for filename in glob.iglob ('*.pdf'): ###with open('Listofpdf', 'filename') as csvfile: ###writer = csv.writer(csvfile, delimter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL) ###writer.writerrow([('{}\t.format(elem))]) from pathlib import Path searchdir = "C:\\Users\\Randy\\Documents\\BOOKS" csvfilename = "listofpdf.txt" with Path(csvfilename).open(mode="w+") as p: files = Path(searchdir).glob('*.py') p.write(f"{' '.join(str(file) for file in files)}\n") At this point, the pathlib mod seems like it should create the file? I'm on WINDOWS 10 and IDLE 3.7. Thanks for any help, R` From jon+usenet at unequivocal.eu Sun Feb 10 10:15:32 2019 From: jon+usenet at unequivocal.eu (Jon Ribbens) Date: Sun, 10 Feb 2019 15:15:32 -0000 (UTC) Subject: The slash "/" as used in the documentation References: Message-ID: On 2019-02-09, Terry Reedy wrote: > '/' is no uglier than, and directly analogous to, and as easy to produce > and comprehend, as '*'. It was selected after considerable discussion > of how to indicate that certain parameters are, at least in CPython, > positional only. The discussion of options included at least some of > those given above. It is very unlikely to go away or be changed. Ok, but what does it *mean*? As an aside, how is 'math.sin' actually implemented? mathmodule.c refers to the function 'math_sin' but that name is not defined anywhere in the Python source code. I'm a bit mystified as to how CPython manages to compile! From rosuav at gmail.com Sun Feb 10 10:34:24 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 11 Feb 2019 02:34:24 +1100 Subject: The slash "/" as used in the documentation In-Reply-To: References: Message-ID: On Mon, Feb 11, 2019 at 2:21 AM Jon Ribbens wrote: > > On 2019-02-09, Terry Reedy wrote: > > '/' is no uglier than, and directly analogous to, and as easy to produce > > and comprehend, as '*'. It was selected after considerable discussion > > of how to indicate that certain parameters are, at least in CPython, > > positional only. The discussion of options included at least some of > > those given above. It is very unlikely to go away or be changed. > > Ok, but what does it *mean*? It means "everything prior to this point is positional-only". > As an aside, how is 'math.sin' actually implemented? mathmodule.c > refers to the function 'math_sin' but that name is not defined > anywhere in the Python source code. I'm a bit mystified as to how > CPython manages to compile! A lot of those sorts of functions are hyperthin wrappers around the C math library. A bit of digging takes me to line 1176 of mathmodule.c (in my source tree; exact line number may vary), which uses the #define of FUNC1 from line 1032, and the actual code is up at line 876, with a good block comment. ChrisA From ian.g.kelly at gmail.com Sun Feb 10 10:47:30 2019 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 10 Feb 2019 08:47:30 -0700 Subject: The slash "/" as used in the documentation In-Reply-To: References: Message-ID: On Sat, Feb 9, 2019 at 1:19 PM Terry Reedy wrote: > > This is the result of Python being a project of mostly unpaid volunteers. > > See my response in this thread explaining how '/' appears in help output > and IDLE calltips. '/' only appears for CPython C-coded functions that > have been modified to use Argument Clinic. A.C. was added, I believe, > in 3.5. Simple builtins like len would have been the first to be > converted. The math module was converted for 3.7. There are some new > conversions for 3.8 and likely some will be left for future versions. I'm sure there are good reasons for it like most things Python does, but I can't help but wonder if working on removing the positional limitation from CPython would be a better use of time. From tagrain at gmail.com Sun Feb 10 10:45:16 2019 From: tagrain at gmail.com (Thomas Grainger) Date: Sun, 10 Feb 2019 15:45:16 +0000 Subject: Fwd: improve TypeError for AsyncIterables when attempting to sync iterate In-Reply-To: References: Message-ID: Currently when attempting to sync iterate an async iterable you get a TypeError: TypeError: 'SomeType' object is not iterable When attempting to iterate an async iterable (eg an object with a __aiter__ method the error could be something like: TypeError: 'SomeType' object is not iterable, it is however an AsyncIterable. Use `async for` instead. From jon+usenet at unequivocal.eu Sun Feb 10 10:55:06 2019 From: jon+usenet at unequivocal.eu (Jon Ribbens) Date: Sun, 10 Feb 2019 15:55:06 -0000 (UTC) Subject: The slash "/" as used in the documentation References: Message-ID: On 2019-02-10, Chris Angelico wrote: > On Mon, Feb 11, 2019 at 2:21 AM Jon Ribbens wrote: >> On 2019-02-09, Terry Reedy wrote: >> > '/' is no uglier than, and directly analogous to, and as easy to produce >> > and comprehend, as '*'. It was selected after considerable discussion >> > of how to indicate that certain parameters are, at least in CPython, >> > positional only. The discussion of options included at least some of >> > those given above. It is very unlikely to go away or be changed. >> >> Ok, but what does it *mean*? > > It means "everything prior to this point is positional-only". > >> As an aside, how is 'math.sin' actually implemented? mathmodule.c >> refers to the function 'math_sin' but that name is not defined >> anywhere in the Python source code. I'm a bit mystified as to how >> CPython manages to compile! > > A lot of those sorts of functions are hyperthin wrappers around the C > math library. A bit of digging takes me to line 1176 of mathmodule.c > (in my source tree; exact line number may vary), which uses the > #define of FUNC1 from line 1032, and the actual code is up at line > 876, with a good block comment. Ah, it's using C preprocessor string concatenation to build the function name, which is why grepping for 'math_sin' didn't find it. Many thanks for your helpful answers. From __peter__ at web.de Sun Feb 10 11:02:40 2019 From: __peter__ at web.de (Peter Otten) Date: Sun, 10 Feb 2019 17:02:40 +0100 Subject: print console out to a txt or csv file References: <536f3c4b-1d7c-43a2-88f1-a1df8cc15820@googlegroups.com> Message-ID: GISDude wrote: > Hi all, > I have been working on some code to list the files of a folder that has > .pdf extension. I have the basic code to create a list, but it prints that > list to the console. I'd like my code to write a txt file that contains > that list (to later import into excel). > > ###A script to list pdf files in a folder > ###gisdude 02/09/19 > ### > import os, sys, glob, csv, pathlib > > ###First, let's change the dir with os > > os.chdir("C:\\Users\\Randy\\Documents\\BOOKS") I have come to the conclusion that keeping track of the full path is much easier than keeping track of what directory you are in at the moment. I recommend that you never use os.chdir(). > ###Second, let's now get the list of files in this directory > > files = glob.glob('*.pdf') > for file in glob.glob("*.pdf"): > print (file) > > ###This prints to the IDLE console > ###But, I want to print to a csv file > > ####for filename in glob.iglob ('*.pdf'): > ###with open('Listofpdf', 'filename') as > ###csvfile: writer = csv.writer(csvfile, > ###delimter=' ', quotechar='|', > ###quoting=csv.QUOTE_MINIMAL) > ###writer.writerrow([('{}\t.format(elem))]) > from pathlib import Path > searchdir = "C:\\Users\\Randy\\Documents\\BOOKS" > csvfilename = "listofpdf.txt" > > with Path(csvfilename).open(mode="w+") as p: > files = Path(searchdir).glob('*.py') > p.write(f"{' '.join(str(file) for file in files)}\n") > > At this point, the pathlib mod seems like it should create the file? > > I'm on WINDOWS 10 and IDLE 3.7. > > Thanks for any help, > R` This looks rather messy, mainly because you are keeping every attempt you made. I recommend that you remove the code you don't use or that doesn't work as soon as possible. Here are two ways to write the list of filenames to a csv file: (1) Using traditional file path manipulation routines: PDFFOLDER = "C:\\Users\\Randy\\Documents\\BOOKS" CSVFILE = "files.txt" filenames = glob.glob(os.path.join(PDFFOLDER, "*.pdf")) with open(CSVFILE, "w", newline="") as outstream: writer = csv.writer(outstream) writer.writerows([path] for path in filenames) Note that you are actually writing a list of lists; omitting the inner list leads to writing one column per character in the filename. (2) Using pathlib's Path objects: PDFFOLDER = pathlib.Path("C:\\Users\\Randy\\Documents\\BOOKS") CSVFILENAME = pathlib.Path("files.txt") filenames = PDFFOLDER.glob("*.pdf") with CSVFILENAME.open("w", newline="") as outstream: writer = csv.writer(outstream) writer.writerows([path] for path in filenames) If you want the filename without directory replace path in the single item lists with os.path.basename(path) or path.name respectively. From barry at barrys-emacs.org Sun Feb 10 10:42:56 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Sun, 10 Feb 2019 15:42:56 +0000 Subject: [solved] C API version of str(exception) is not the same as pure python version In-Reply-To: <28195810.4nJTtaLN7d@varric.chelsea.private> References: <28195810.4nJTtaLN7d@varric.chelsea.private> Message-ID: <11248160.4SVhGpdHPz@varric.chelsea.private> On Sunday, 10 February 2019 11:59:16 GMT Barry Scott wrote: > When I use the C API to get the str() of an execption I see this text: > > > > But python reports the following for the same exception: > > TypeError: unsupported operand type(s) for +: 'int' and 'str' > > What do I need to do in the C API to get the the same text for the > exception? > > PyObject *err = PyErr_Occurred(); > if( err != 0 ) > { > PyObject *u_why = PyObject_Str( err ); > PyObject *b_why = PyUnicode_AsEncodedString( u_why, "utf-8", > "strict" ); > int size = PyBytes_Size( b_why ); > char *why = PyBytes_AsString( b_why ); > printf("Error: %*s\n", size, why ); > return 0; > } Using this code fixes the problem as Stefan pointed out I had the type of the exception and not the value of the exception. if( PyErr_Occurred() ) { PyObject *type, *value, *trace; PyErr_Fetch( &type, &value, &trace ); PyObject *u_why = PyObject_Str( value ); PyObject *b_why = PyUnicode_AsEncodedString( u_why, "utf-8", "strict" ); int size = PyBytes_Size( b_why ); char *why = PyBytes_AsString( b_why ); printf("Error: %*s\n", size, why ); return 0; } From barry at barrys-emacs.org Sun Feb 10 10:34:24 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Sun, 10 Feb 2019 15:34:24 +0000 Subject: C API PyObject_GetAttrString returns not the object I expected In-Reply-To: References: <7432942.9YVUmlyeYX@varric.chelsea.private> Message-ID: <1615374.9NcNtW1ol6@varric.chelsea.private> On Sunday, 10 February 2019 13:58:57 GMT Stefan Behnel wrote: > Barry Scott schrieb am 10.02.19 um 13:08: > > After calling PyObject_GetAttrString() I expected to get a PyObject string > > back but I found that I had been given a instead. > > > > (gdb) p *args_o > > $4 = > > > > What is going on and how do I get from the to the > > object I want? > > Phil is right about the function itself, but my guess is that you called > GetAttr() on a class instead of an instance. Read up on Python descriptors > to understand what difference that makes. > > https://docs.python.org/3/howto/descriptor.html > > Basically, you got something like a property object back, but not the value > that the property maintaines. If you look up the attribute on the instance, > the property (or descriptor) will hand it to you. The same applies to > method lookups and other special attributes that may also be implemented as > descriptors. Thanks that the clue I needed. I had assumed that PyErr_Occurred() returned the instance, but it returns the type and as you explained that gives you the get_set_descriptor. I need to use PyErr_Fetch to get the instance. > Also take a look at Cython, which is designed to keep users from having to > learn all these things and instead lets you do them in Python. > > https://cython.org/ I'm adding better code to look at exceptions to PyCXX and hit this issue. Barry PyCXX maintainer. From rosuav at gmail.com Sun Feb 10 11:31:57 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 11 Feb 2019 03:31:57 +1100 Subject: The slash "/" as used in the documentation In-Reply-To: References: Message-ID: On Mon, Feb 11, 2019 at 2:49 AM Ian Kelly wrote: > > On Sat, Feb 9, 2019 at 1:19 PM Terry Reedy wrote: > > > > This is the result of Python being a project of mostly unpaid volunteers. > > > > See my response in this thread explaining how '/' appears in help output > > and IDLE calltips. '/' only appears for CPython C-coded functions that > > have been modified to use Argument Clinic. A.C. was added, I believe, > > in 3.5. Simple builtins like len would have been the first to be > > converted. The math module was converted for 3.7. There are some new > > conversions for 3.8 and likely some will be left for future versions. > > I'm sure there are good reasons for it like most things Python does, but I > can't help but wonder if working on removing the positional limitation from > CPython would be a better use of time. Do you ACTUALLY want to call math.sin(x=1.234) or is it purely for the sake of consistency? Aside from questions about the help format, what is actually lost by the inability to pass those arguments by name? ChrisA From barry at barrys-emacs.org Sun Feb 10 10:53:32 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Sun, 10 Feb 2019 15:53:32 +0000 Subject: where is math_sin defined? In-Reply-To: References: Message-ID: <2882349.ObpKs5aGNZ@varric.chelsea.private> On Sunday, 10 February 2019 15:15:32 GMT Jon Ribbens wrote: > As an aside, how is 'math.sin' actually implemented? mathmodule.c > refers to the function 'math_sin' but that name is not defined > anywhere in the Python source code. I'm a bit mystified as to how > CPython manages to compile! I used gdb to find it: Breakpoint 1, math_sin (self=, args=0) at / usr/src/debug/python3-3.7.2-4.fc29.x86_64/Modules/mathmodule.c:1176 1176 FUNC1(sin, sin, 0, (gdb) l 1171 "remainder($module, x, y, /)\n--\n\n" 1172 "Difference between x and the closest integer multiple of y. \n\n" 1173 "Return x - n*y where n*y is the closest integer multiple of y. \n" 1174 "In the case where x is exactly halfway between two multiples of\n" 1175 "y, the nearest even value of n is used. The result is always exact.") 1176 FUNC1(sin, sin, 0, 1177 "sin($module, x, /)\n--\n\n" 1178 "Return the sine of x (measured in radians).") 1179 FUNC1(sinh, sinh, 1, 1180 "sinh($module, x, /)\n--\n\n" (gdb) Barry From jadenfigger at gmail.com Sun Feb 10 11:39:30 2019 From: jadenfigger at gmail.com (jadenfigger at gmail.com) Date: Sun, 10 Feb 2019 08:39:30 -0800 (PST) Subject: Im trying to replicate the youtube video Creating my own customized celebrities with AI. Message-ID: The video gives the source code and the necessary things to download with it. But I'm new to python and don't understand how to install any of the files. The files include: Python 3, pip, tenserflow, pygame, scipy, and numby. Could anyone help me install all of this to run the AI. Thank you From rosuav at gmail.com Sun Feb 10 11:43:04 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 11 Feb 2019 03:43:04 +1100 Subject: where is math_sin defined? In-Reply-To: <2882349.ObpKs5aGNZ@varric.chelsea.private> References: <2882349.ObpKs5aGNZ@varric.chelsea.private> Message-ID: On Mon, Feb 11, 2019 at 3:37 AM Barry Scott wrote: > > On Sunday, 10 February 2019 15:15:32 GMT Jon Ribbens wrote: > > As an aside, how is 'math.sin' actually implemented? mathmodule.c > > refers to the function 'math_sin' but that name is not defined > > anywhere in the Python source code. I'm a bit mystified as to how > > CPython manages to compile! > > I used gdb to find it: > Effective, if a little tedious. My technique was to first confirm that there was nothing saying "math_sin" anywhere in the repo (trust but verify - doesn't hurt to do a quick "git grep"), then to search mathmodule.c for "sin(", since searching for "sin" on its own gave way too many hits. That led me to the definition of sinpi(), then to asin() and sin(), both being defined using the FUNC1 template. ChrisA From bgailer at gmail.com Sun Feb 10 12:06:51 2019 From: bgailer at gmail.com (Bob Gailer) Date: Sun, 10 Feb 2019 12:06:51 -0500 Subject: Im trying to replicate the youtube video Creating my own customized celebrities with AI. In-Reply-To: References: Message-ID: On Feb 10, 2019 11:40 AM, wrote: > > The video I don't see any video here. If you attached one the attachment did not come through. > gives the source code and the necessary things to download with it. But I'm new to python and don't understand how to install any of the files. The files include: Python 3 Which you can download from python.org - just follow the link to downloads. > pip is automatically installed when you install python > tenserflow, pygame, scipy, and numby All of these are probably installable using pip. By the way did you mean numpy? At a command prompt type pip install packagename. I suggest you switch from list at python.org to help at python.org. Bob Gailer From avigross at verizon.net Sun Feb 10 16:15:17 2019 From: avigross at verizon.net (Avi Gross) Date: Sun, 10 Feb 2019 16:15:17 -0500 Subject: The slash "/" as used in the documentation In-Reply-To: References: Message-ID: <005501d4c185$b8b9ea30$2a2dbe90$@verizon.net> Chris, I would appreciate another pointer to the documentation explaining what was done and why as I deleted the earlier discussion. You ask: > Aside from questions about the help format, what is actually lost by the inability > to pass those arguments by name? I am not sure how python implements some of the functionality it does as compared to other languages with similar features. But I note that there are rules, presumably some for efficiency, such as requiring all keyword arguments to be placed after the last positional argument. I mean I can call func(a,b,c,d=1,e=2) but not func(a,b,d=1, c, e=2). So if you allowed a keyword to optionally be used for any positional argument other than the last, c, would it not require a change in this rule? I mean func(a="invalid",b,c,d=1,e=2) would violate the rule and so would making b a keyword version. In my example, and for this reason only, maybe c could work. And note the impact IF ALLOWED on the existing and new programs that allow a variable number of arguments of the form func(*args, **kwargs) when they evaluate. The previous model was that args would be a sequence of the arguments you could index so args[0] would be the first, or that you can pop off the front and use. If any of the previously un-named arguments can now entered with a keyword, are they now placed in args or in the dictionary kwargs? If in kwargs, the program now needs to know to look there in addition to the command line. If it was the first argument and is no longer in args, the second and further arguments would either be shifted over and be used wrong or you need a placeholder. The original motivation for keyword arguments included the concept of specifying a default if not used. Positional arguments have no default. Similarly, they are required if explicitly named in the function definition. So we are intermingling multiple concepts in the previous design. I won't go on except to say it would break lots of existing code and potentially complicate new code. Let me add a dumb suggestion. Anyone who desperately wants to name the parameters has a rather strange option they can do now. Very imperfect but consider the function prototype I have been using for illustration: func(a,b,c,d=1,e=2) It requires three positional arguments. What if you implement your code so some special values mean that you are going to pass along "a" as "key_a" instead. You can use something like None or the ellipsis(...) to signal this as in: def func(a, b, c, d=1, e=2, key_a=None): if a == ... : a = key_a print(a) The above does nothing but illustrates a WAY to allow a keyword implementation by using one or more placeholders as you can do the same gimmick for b and c. Here is how it runs if you use the positional arg, or an ellipsis and then a keyword or an ellipsis and accept the default for the keyword: >>> func(1,2,3) 1 >>> func(...,2,3,key_a="keyed up") keyed up >>> func(...,2,3) None So could you re-implement processing in a NEW language to allow different handling. I am guessing you could. Again, other languages currently do things their own way. But for compatibility, I can see why they may be patching and documenting what IS. And note you could create an amazingly annoying language where each parameter is specified as having umpteen attributes like it has to be the second un-named argument and of a restricted number of types and if it has a keyword it can be abbreviated as short as some string and then should it be placed in position 2 and does it have a default and who knows what more. Avi -----Original Message----- From: Python-list On Behalf Of Chris Angelico Sent: Sunday, February 10, 2019 11:32 AM To: Python Subject: Re: The slash "/" as used in the documentation On Mon, Feb 11, 2019 at 2:49 AM Ian Kelly wrote: > > On Sat, Feb 9, 2019 at 1:19 PM Terry Reedy wrote: > > > > This is the result of Python being a project of mostly unpaid volunteers. > > > > See my response in this thread explaining how '/' appears in help > > output and IDLE calltips. '/' only appears for CPython C-coded > > functions that have been modified to use Argument Clinic. A.C. was > > added, I believe, in 3.5. Simple builtins like len would have been > > the first to be converted. The math module was converted for 3.7. > > There are some new conversions for 3.8 and likely some will be left for future versions. > > I'm sure there are good reasons for it like most things Python does, > but I can't help but wonder if working on removing the positional > limitation from CPython would be a better use of time. Do you ACTUALLY want to call math.sin(x=1.234) or is it purely for the sake of consistency? Aside from questions about the help format, what is actually lost by the inability to pass those arguments by name? ChrisA -- https://mail.python.org/mailman/listinfo/python-list From avigross at verizon.net Sun Feb 10 16:28:24 2019 From: avigross at verizon.net (Avi Gross) Date: Sun, 10 Feb 2019 16:28:24 -0500 Subject: Im trying to replicate the youtube video Creating my own customized celebrities with AI. In-Reply-To: References: Message-ID: <005601d4c187$8e1ba870$aa52f950$@verizon.net> Bob, >> tenserflow, pygame, scipy, and numby > All of these are probably installable using pip. By the way did you mean numpy? > At a command prompt type pip install packagename. While you are correcting the spelling of downloads as the downloader is quite picky about exact spelling, please mention that the tenser flow should be tensorflow. I think numby and perhaps tense is how you feel as you read to the end of the documentation on numpy. More seriously, it helps if you have some idea of why these modules get names. NUMeric PYthon is numpy. Something like SCIentific PYthon is scipy. I am not as sure about TensorFlow but suspect the mathematical concept of a Tensor along with how it improves workflow. Haven't had a need to use it yet. Neither have I had a need for pygame which seems to be a way to make games in python. -----Original Message----- From: Python-list On Behalf Of Bob Gailer Sent: Sunday, February 10, 2019 12:07 PM To: jadenfigger at gmail.com Cc: python list Subject: Re: Im trying to replicate the youtube video Creating my own customized celebrities with AI. On Feb 10, 2019 11:40 AM, wrote: > > The video I don't see any video here. If you attached one the attachment did not come through. > gives the source code and the necessary things to download with it. > But I'm new to python and don't understand how to install any of the files. The files include: Python 3 Which you can download from python.org - just follow the link to downloads. > pip is automatically installed when you install python > tenserflow, pygame, scipy, and numby All of these are probably installable using pip. By the way did you mean numpy? At a command prompt type pip install packagename. I suggest you switch from list at python.org to help at python.org. Bob Gailer -- https://mail.python.org/mailman/listinfo/python-list From gabriele1NOSPAM at hotmail.com Sun Feb 10 16:42:57 2019 From: gabriele1NOSPAM at hotmail.com (^Bart) Date: Sun, 10 Feb 2019 22:42:57 +0100 Subject: The use of type() Message-ID: I need to print something like "this variable is int" or "this variable is string" n1 = 10 n2 = 23 print ("Total of n1+n2 is: ",n1+n2," the type is", type(n1+n2)) When I run it I have: Total of n1+n2 is: 33 the type is >>> I'd like to read "the type is int" and NOT "the type is , how could I solve it? ^Bart From rosuav at gmail.com Sun Feb 10 16:49:25 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 11 Feb 2019 08:49:25 +1100 Subject: The use of type() In-Reply-To: References: Message-ID: On Mon, Feb 11, 2019 at 8:46 AM ^Bart wrote: > > I need to print something like "this variable is int" or "this variable > is string" > > n1 = 10 > n2 = 23 > > print ("Total of n1+n2 is: ",n1+n2," the type is", type(n1+n2)) > > When I run it I have: > > Total of n1+n2 is: 33 the type is > >>> > > I'd like to read "the type is int" and NOT "the type is , > how could I solve it? > Many things in Python, including functions and types, have inherent names. >>> print(sys.__name__, int.__name__, print.__name__) sys int print Enjoy! ChrisA From avigross at verizon.net Sun Feb 10 17:42:55 2019 From: avigross at verizon.net (Avi Gross) Date: Sun, 10 Feb 2019 17:42:55 -0500 Subject: The use of type() In-Reply-To: References: Message-ID: <000b01d4c191$f6f66ab0$e4e34010$@verizon.net> Without using regular expressions, if you just want to extract the word "int" or "float" you can substring the results by converting what type says to a string: >>> a = 5 >>> str(type(a))[8:11] 'int' >>> a=5.0 >>> str(type(a))[8:13] 'float' Since the format and length vary, this may not meet your needs. You could search for the first index where there is a single quote and then the next and take what is in between. You can run this in-line or make a function that might work for at least the basic types: >>> a = 5 >>> text = str(type(a)) >>> first = text.find("'") >>> first += 1 >>> second = text.find("'", first) >>> first, second (8, 11) >>> text[first : second] 'int' >>> print(text[first : second]) Int If I do the same with a float like 5.0: >>> a=5.0 >>> text = str(type(a)) >>> first = text.find("'") >>> first += 1 >>> second = text.find("'", first) >>> print(text[first : second]) float For a list: >>> a = ["list", "of", "anything"] ... >>> print(text[first : second]) list Of course this is so simple it must be out there in some module. -----Original Message----- From: Python-list On Behalf Of ^Bart Sent: Sunday, February 10, 2019 4:43 PM To: python-list at python.org Subject: The use of type() I need to print something like "this variable is int" or "this variable is string" n1 = 10 n2 = 23 print ("Total of n1+n2 is: ",n1+n2," the type is", type(n1+n2)) When I run it I have: Total of n1+n2 is: 33 the type is >>> I'd like to read "the type is int" and NOT "the type is , how could I solve it? ^Bart -- https://mail.python.org/mailman/listinfo/python-list From avigross at verizon.net Sun Feb 10 17:48:36 2019 From: avigross at verizon.net (Avi Gross) Date: Sun, 10 Feb 2019 17:48:36 -0500 Subject: The use of type() In-Reply-To: <000b01d4c191$f6f66ab0$e4e34010$@verizon.net> References: <000b01d4c191$f6f66ab0$e4e34010$@verizon.net> Message-ID: <000c01d4c192$c285ed90$4791c8b0$@verizon.net> Follow on to below. I was right and there is a fairly trivial and portable way to just show 'int' for the type of probably many types: No need to call the type() function at all. If "a" is an integer object containing "5" then you can ask for the class of it and then within that for the name like this: >>> a = 5 >>> print(a.__class__.__name__) int >>> b = 5.0 >>> print(b.__class__.__name__) float -----Original Message----- From: Python-list On Behalf Of Avi Gross Sent: Sunday, February 10, 2019 5:43 PM To: python-list at python.org Subject: RE: The use of type() Without using regular expressions, if you just want to extract the word "int" or "float" you can substring the results by converting what type says to a string: >>> a = 5 >>> str(type(a))[8:11] 'int' >>> a=5.0 >>> str(type(a))[8:13] 'float' Since the format and length vary, this may not meet your needs. You could search for the first index where there is a single quote and then the next and take what is in between. You can run this in-line or make a function that might work for at least the basic types: >>> a = 5 >>> text = str(type(a)) >>> first = text.find("'") >>> first += 1 >>> second = text.find("'", first) >>> first, second (8, 11) >>> text[first : second] 'int' >>> print(text[first : second]) Int If I do the same with a float like 5.0: >>> a=5.0 >>> text = str(type(a)) >>> first = text.find("'") >>> first += 1 >>> second = text.find("'", first) >>> print(text[first : second]) float For a list: >>> a = ["list", "of", "anything"] .. >>> print(text[first : second]) list Of course this is so simple it must be out there in some module. -----Original Message----- From: Python-list On Behalf Of ^Bart Sent: Sunday, February 10, 2019 4:43 PM To: python-list at python.org Subject: The use of type() I need to print something like "this variable is int" or "this variable is string" n1 = 10 n2 = 23 print ("Total of n1+n2 is: ",n1+n2," the type is", type(n1+n2)) When I run it I have: Total of n1+n2 is: 33 the type is >>> I'd like to read "the type is int" and NOT "the type is , how could I solve it? ^Bart -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list From ian.g.kelly at gmail.com Sun Feb 10 23:32:33 2019 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 10 Feb 2019 21:32:33 -0700 Subject: The slash "/" as used in the documentation In-Reply-To: References: Message-ID: On Sun, Feb 10, 2019 at 9:34 AM Chris Angelico wrote: > > On Mon, Feb 11, 2019 at 2:49 AM Ian Kelly wrote: > > > > On Sat, Feb 9, 2019 at 1:19 PM Terry Reedy wrote: > > > > > > This is the result of Python being a project of mostly unpaid volunteers. > > > > > > See my response in this thread explaining how '/' appears in help output > > > and IDLE calltips. '/' only appears for CPython C-coded functions that > > > have been modified to use Argument Clinic. A.C. was added, I believe, > > > in 3.5. Simple builtins like len would have been the first to be > > > converted. The math module was converted for 3.7. There are some new > > > conversions for 3.8 and likely some will be left for future versions. > > > > I'm sure there are good reasons for it like most things Python does, but I > > can't help but wonder if working on removing the positional limitation from > > CPython would be a better use of time. > > Do you ACTUALLY want to call math.sin(x=1.234) or is it purely for the > sake of consistency? Aside from questions about the help format, what > is actually lost by the inability to pass those arguments by name? No, but I wouldn't object to d.get(key='foo', default=12). It's partly about consistency, but mostly I was reacting to Terry's comments in that a) positional-only arguments are a "limitation" and there is no desire for the rest of Python to match CPython's behavior in this instance, and b) it sounds like a great deal of effort is being spent on updating all the C function signatures one at a time so that the help string can be updated to a form that is unfamiliar and not intuitive and not normally accessible to pure Python functions without some arm twisting. If every function has to be updated anyway, why not update them by fixing their signatures? From ian.g.kelly at gmail.com Mon Feb 11 01:45:32 2019 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 10 Feb 2019 23:45:32 -0700 Subject: The slash "/" as used in the documentation In-Reply-To: <005501d4c185$b8b9ea30$2a2dbe90$@verizon.net> References: <005501d4c185$b8b9ea30$2a2dbe90$@verizon.net> Message-ID: On Sun, Feb 10, 2019 at 2:18 PM Avi Gross wrote: > I am not sure how python implements some of the functionality it does as > compared to other languages with similar features. But I note that there are > rules, presumably some for efficiency, such as requiring all keyword > arguments to be placed after the last positional argument. I mean I can call > func(a,b,c,d=1,e=2) but not func(a,b,d=1, c, e=2). > > So if you allowed a keyword to optionally be used for any positional > argument other than the last, c, would it not require a change in this rule? > I mean func(a="invalid",b,c,d=1,e=2) would violate the rule and so would > making b a keyword version. In my example, and for this reason only, maybe c > could work. My suggestion was not to allow keyword arguments to arbitrarily replace any positional parameter, or to otherwise change argument-passing in any way. The suggestion was to drop positional-only arguments from functions implemented in C instead of just documenting them better. In the example above, if you want to pass a by keyword, you would have to pass b and c by keyword as well. That would still be true for functions implemented in C if a, b, and c are no longer positional-only. > The original motivation for keyword arguments included the concept of > specifying a default if not used. Positional arguments have no default. > Similarly, they are required if explicitly named in the function definition. > So we are intermingling multiple concepts in the previous design. Positional arguments are allowed to have defaults, and keyword-only arguments are allowed to not have defaults. These are all valid syntax: # Allows a and b to be passed either positionally or by keyword def foo(a=1, b=2): pass # b is keyword-only def foo(a=1, *, b=2): pass # Allows a and b to be passed either positionally or by keyword def foo(a, b): pass # b is keyword-only and has no default def foo(a, *, b): pass Positional-ONLY arguments are not directly supported by the language, but they exist in the C API and can also have defaults. For example, dict.get takes two positional-only arguments. The second argument is optional, and its default is None. My point is that the axis of positional-only versus positional-or-keyword versus keyword-only, and the axis of required versus optional are entirely separate. > I won't go on except to say it would break lots of existing code and > potentially complicate new code. Can you give an example of something that would be broken by updating C API functions to name positional-only arguments instead of just updating them to document that they're positional-only? From tjreedy at udel.edu Mon Feb 11 02:16:44 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 11 Feb 2019 02:16:44 -0500 Subject: The slash "/" as used in the documentation In-Reply-To: References: Message-ID: On 2/10/2019 10:47 AM, Ian Kelly wrote: > On Sat, Feb 9, 2019 at 1:19 PM Terry Reedy wrote: >> >> This is the result of Python being a project of mostly unpaid volunteers. >> >> See my response in this thread explaining how '/' appears in help output >> and IDLE calltips. '/' only appears for CPython C-coded functions that >> have been modified to use Argument Clinic. A.C. was added, I believe, >> in 3.5. Simple builtins like len would have been the first to be >> converted. The math module was converted for 3.7. There are some new >> conversions for 3.8 and likely some will be left for future versions. > > I'm sure there are good reasons for it like most things Python does, but I > can't help but wonder if working on removing the positional limitation from > CPython would be a better use of time. The pass-by-position limitation is not in CPython, it is the behavior of C functions, which is the behavior of function calls in probably every assembly and machine language. Allowing the flexibility of Python function calls take extra code and slows function calls. math.sin, for instance, is a fast-as-possible wrapper around the C math library sin function. It extracts the C double from the Python float, calls C sin, and returns the returned C double wrapped as a Python float. Coredevs decided that being able to call math.sin(x=.33333) is not worth the resulting slowdown. I am rather sure that heavy users of the math module would agree. -- Terry Jan Reedy From gabriele1NOSPAM at hotmail.com Mon Feb 11 02:37:45 2019 From: gabriele1NOSPAM at hotmail.com (^Bart) Date: Mon, 11 Feb 2019 08:37:45 +0100 Subject: The use of type() References: <000b01d4c191$f6f66ab0$e4e34010$@verizon.net> <000c01d4c192$c285ed90$4791c8b0$@verizon.net> Message-ID: >>>> a = 5 >>>> print(a.__class__.__name__) > int >>>> b = 5.0 >>>> print(b.__class__.__name__) > float Thank you very much! :) ^Bart From ian.g.kelly at gmail.com Mon Feb 11 02:47:14 2019 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 11 Feb 2019 00:47:14 -0700 Subject: The slash "/" as used in the documentation In-Reply-To: References: Message-ID: On Mon, Feb 11, 2019 at 12:19 AM Terry Reedy wrote: > The pass-by-position limitation is not in CPython, it is the behavior of > C functions, which is the behavior of function calls in probably every > assembly and machine language. Allowing the flexibility of Python > function calls take extra code and slows function calls. > > math.sin, for instance, is a fast-as-possible wrapper around the C math > library sin function. It extracts the C double from the Python float, > calls C sin, and returns the returned C double wrapped as a Python > float. Coredevs decided that being able to call math.sin(x=.33333) is > not worth the resulting slowdown. I am rather sure that heavy users of > the math module would agree. For math.sin, sure, but what about, say, list.index? Here's the start of the implementation: static PyObject * listindex(PyListObject *self, PyObject *args) { Py_ssize_t i, start=0, stop=Py_SIZE(self); PyObject *v; if (!PyArg_ParseTuple(args, "O|O&O&:index", &v, _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &stop)) This is already paying the cost of not using C-style positional function arguments, but then it only takes an argument tuple, so it can't take keyword arguments anyway. Why? Wouldn't it be nice to be able to write: mylist.index(42, start=10, stop=99) instead of: mylist.index(42, 10, 99) This would also allow stop to be passed alone: mylist.index(42, stop=99) rather than having to pass the default value for start explicitly just so we can pass stop: mylist.index(42, 0, 99) From tjreedy at udel.edu Mon Feb 11 02:50:03 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 11 Feb 2019 02:50:03 -0500 Subject: The slash "/" as used in the documentation In-Reply-To: References: Message-ID: On 2/10/2019 11:32 PM, Ian Kelly wrote: > On Sun, Feb 10, 2019 at 9:34 AM Chris Angelico wrote: >> Do you ACTUALLY want to call math.sin(x=1.234) or is it purely for the >> sake of consistency? Aside from questions about the help format, what >> is actually lost by the inability to pass those arguments by name? > > No, but I wouldn't object to d.get(key='foo', default=12). I don't see what dict acccess has to do with function calls. But moving on. > It's partly > about consistency, but mostly I was reacting to Terry's comments in > that a) positional-only arguments are a "limitation" If one thinks of Python's flexible signatures as 'normal', which they aren't, than positional-only seems like a limitation. (Some other languages have somewhat similar flexibility.) From a C/assembler/machine perspective, positional is normal. > and there is no > desire for the rest of Python to match CPython's behavior in this > instance, For reasons of complexity and speed, without a countervailing argument, there is no desire to add to C-coded functions a flexibility that will mostly not be used. > and b) it sounds like a great deal of effort is being spent > on updating all the C function signatures one at a time It tends to be done a module per PR. I don't know what effort is needed since I have never done it. > so that the > help string can be updated to a form that is unfamiliar and not > intuitive '*' is not 'intuitive either. As I said elsewhere, multiple options were considered. '/' was the favorite because it is 'connected' to '*' and more intuitive for dividing a list into two sublists. Any, IDLE, but not help(), adds a comment to explain. "['/' marks preceding arguments as positional only]" The downside is that this and a following blank line add 2 lines to a calltip that is usually 2 lines, making it more obtrusive. And once '/' is understood, the comment is extra noise. So I am thinking of condensing it somehow. > and not normally accessible to pure Python functions without > some arm twisting. In my first response on this thread I explained and demonstrated how to access signature strings from Python, as done by both help() and IDLE. Please read if you are concerned about this. There is hardly any extra difficulty. I did not discuss how to process the signature string returned by str(signature) nor how to access the information in Signature objects otherwise, but the str and Signature docs cover these. It is possible the a sentence about '/' should be added somewhere. > If every function has to be updated anyway, why not > update them by fixing their signatures? I proposed this once and the answer was that it would be too costly. -- Terry Jan Reedy From rosuav at gmail.com Mon Feb 11 02:55:32 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 11 Feb 2019 18:55:32 +1100 Subject: The slash "/" as used in the documentation In-Reply-To: References: Message-ID: On Mon, Feb 11, 2019 at 6:49 PM Ian Kelly wrote: > > On Mon, Feb 11, 2019 at 12:19 AM Terry Reedy wrote: > > The pass-by-position limitation is not in CPython, it is the behavior of > > C functions, which is the behavior of function calls in probably every > > assembly and machine language. Allowing the flexibility of Python > > function calls take extra code and slows function calls. > > > > math.sin, for instance, is a fast-as-possible wrapper around the C math > > library sin function. It extracts the C double from the Python float, > > calls C sin, and returns the returned C double wrapped as a Python > > float. Coredevs decided that being able to call math.sin(x=.33333) is > > not worth the resulting slowdown. I am rather sure that heavy users of > > the math module would agree. > > For math.sin, sure, but what about, say, list.index? Here's the start > of the implementation: > > static PyObject * > listindex(PyListObject *self, PyObject *args) > { > Py_ssize_t i, start=0, stop=Py_SIZE(self); > PyObject *v; > > if (!PyArg_ParseTuple(args, "O|O&O&:index", &v, > _PyEval_SliceIndex, &start, > _PyEval_SliceIndex, &stop)) > > > This is already paying the cost of not using C-style positional > function arguments, but then it only takes an argument tuple, so it > can't take keyword arguments anyway. Why? How much of a performance penalty are you willing to accept for this? It would apply to every C-implemented function, including tiny and common ones like 1+2, just in case someone wanted to write int.__add__(self=1, value=2). Try to pitch this to sysadmins and app authors: "CPython 3.8 will be 3% slower, but you can pass keyword args to dunders now!" ChrisA From rosuav at gmail.com Mon Feb 11 03:01:30 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 11 Feb 2019 19:01:30 +1100 Subject: The slash "/" as used in the documentation In-Reply-To: References: Message-ID: On Mon, Feb 11, 2019 at 6:51 PM Terry Reedy wrote: > > and not normally accessible to pure Python functions without > > some arm twisting. > > In my first response on this thread I explained and demonstrated how to > access signature strings from Python, as done by both help() and IDLE. > Please read if you are concerned about this. There is hardly any extra > difficulty. I did not discuss how to process the signature string > returned by str(signature) nor how to access the information in > Signature objects otherwise, but the str and Signature docs cover these. The feature of positional-only args isn't available to pure Python functions at the moment, other than by taking *args and then mutating your own sig. Hence the slash is unfamiliar to people. If we want arbitrary consistency, it might be better to work that way: to let pure Python functions take pos-only args. I doubt it would be of particularly great benefit, but it wouldn't hurt. ChrisA From tjreedy at udel.edu Mon Feb 11 03:15:52 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 11 Feb 2019 03:15:52 -0500 Subject: The slash "/" as used in the documentation In-Reply-To: References: Message-ID: On 2/11/2019 2:47 AM, Ian Kelly wrote: > For math.sin, sure, but what about, say, list.index? Special-case conversion is a different issue from blanket conversion. Some C functions have been converted to accept some or all args by keyword. I don't know the status of list method conversion: discussed? accepted? rejected? The people involved with conversion of documenting mostly don't read this list. -- Terry Jan Reedy From hello at ezaquarii.com Sun Feb 10 18:33:34 2019 From: hello at ezaquarii.com (Chris Narkiewicz) Date: Sun, 10 Feb 2019 23:33:34 +0000 Subject: Can't run setup.py offline due to setup_requires - setup.py calls home Message-ID: <026b03c8-7eb6-6166-3efc-6e73b9d103fb@ezaquarii.com> Hi, I'm trying to build a debian package in offline environment (build server). To build this package, I need to ship all python dependencies as source packages and build them there. This is no problem for all, except one package that has build-time dependencies: Automat-0.70. debian/rules calls this pip to install all requirements from local package collection: pip3 install --log=... --no-cache --no-index --find-links=pypi --no-binary=":all:" -r requirements.txt Directory pypi contains ALL dependencies required for build. This works ok when build server has connection to network, but fails for offline builds. I pinpointed the problem to package Automat, that specifies some dependencies via setup_requires=[...]: setup( ..., setup_requires=[ 'setuptools-scm', 'm2r', ], ... ) Trying to build Automat locally by calling setup.py fails immediately in my offline environment: $ python setup.py ... distutils.errors.DistutilsError: Download error for https://files.pythonhosted.org/packages/39/e7/9fae11a45f5e1a3a21d8a98d02948e597c4afd7848a0dbe1a1ebd235f13e/m2r-0.2.1.tar.gz#sha256=bf90bad66cda1164b17e5ba4a037806d2443f2a4d5ddc9f6a5554a0322aaed99: [Errno 111] Connection refused Is there any way to stop Distutils from calling home? Best regards, Chris Narkiewicz From mystirk at gmail.com Mon Feb 11 09:37:51 2019 From: mystirk at gmail.com (Alex Kaye) Date: Mon, 11 Feb 2019 07:37:51 -0700 Subject: My appreciation for python's great work to my country. In-Reply-To: References: Message-ID: Kiyimba, In my community in Arizona ( pop 7000) I am the only one using Linux and the only one who is studying Python, no one is coding. So spread your knowledge among the youth of your commun ity. It is good for their future. Alex Kaye On Sat, Feb 9, 2019 at 1:34 PM Terry Reedy wrote: > On 2/8/2019 4:37 AM, Kiyimba Godfrey wrote: > > I take this opportunity to thank python for having designed such a > wonderful program which has enabled computer scientists and programmers in > my country Uganda, understand, design and create other computer application > programs . > > You're welcome, and I appreciate your comment. I donate time to > development of Python and IDLE *because* they are particularly useful to > beginners and people in less developed countries. > > > -- > Terry Jan Reedy > > -- > https://mail.python.org/mailman/listinfo/python-list > From ndbecker2 at gmail.com Mon Feb 11 10:25:37 2019 From: ndbecker2 at gmail.com (Neal Becker) Date: Mon, 11 Feb 2019 10:25:37 -0500 Subject: exit 2 levels of if/else and execute common code Message-ID: I have code with structure: ``` if cond1: [some code] if cond2: #where cond2 depends on the above [some code] [ more code] else: [ do xxyy ] else: [ do the same xxyy as above ] ``` So what's the best style to handle this? As coded, it violates DRY. Try/except could be used with a custom exception, but that seems a bit heavy handed. Suggestions? From rhodri at kynesim.co.uk Mon Feb 11 10:38:26 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Mon, 11 Feb 2019 15:38:26 +0000 Subject: exit 2 levels of if/else and execute common code In-Reply-To: References: Message-ID: <6dcd2575-4445-6488-65eb-abadfab14675@kynesim.co.uk> On 11/02/2019 15:25, Neal Becker wrote: > I have code with structure: > ``` > if cond1: > [some code] > if cond2: #where cond2 depends on the above [some code] > [ more code] > > else: > [ do xxyy ] > else: > [ do the same xxyy as above ] > ``` > > So what's the best style to handle this? As coded, it violates DRY. > Try/except could be used with a custom exception, but that seems a bit heavy > handed. Suggestions? If it's trivial, ignore DRY. That's making work for the sake of making work in such a situation. If it isn't trivial, is there any reason not to put the common code in a function? -- Rhodri James *-* Kynesim Ltd From ndbecker2 at gmail.com Mon Feb 11 10:43:11 2019 From: ndbecker2 at gmail.com (Neal Becker) Date: Mon, 11 Feb 2019 10:43:11 -0500 Subject: exit 2 levels of if/else and execute common code References: <6dcd2575-4445-6488-65eb-abadfab14675@kynesim.co.uk> Message-ID: Rhodri James wrote: > On 11/02/2019 15:25, Neal Becker wrote: >> I have code with structure: >> ``` >> if cond1: >> [some code] >> if cond2: #where cond2 depends on the above [some code] >> [ more code] >> >> else: >> [ do xxyy ] >> else: >> [ do the same xxyy as above ] >> ``` >> >> So what's the best style to handle this? As coded, it violates DRY. >> Try/except could be used with a custom exception, but that seems a bit >> heavy >> handed. Suggestions? > > If it's trivial, ignore DRY. That's making work for the sake of making > work in such a situation. > > If it isn't trivial, is there any reason not to put the common code in a > function? > Well the common code is 2 lines. From rosuav at gmail.com Mon Feb 11 10:44:44 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 12 Feb 2019 02:44:44 +1100 Subject: exit 2 levels of if/else and execute common code In-Reply-To: References: Message-ID: On Tue, Feb 12, 2019 at 2:27 AM Neal Becker wrote: > > I have code with structure: > ``` > if cond1: > [some code] > if cond2: #where cond2 depends on the above [some code] > [ more code] > > else: > [ do xxyy ] > else: > [ do the same xxyy as above ] > ``` > > So what's the best style to handle this? As coded, it violates DRY. > Try/except could be used with a custom exception, but that seems a bit heavy > handed. Suggestions? One common way to do this is to toss a "return" after the cond2 block. Means this has to be the end of a function, but that's usually not hard. Or, as Rhodri suggested, refactor xxyy into a function, which you then call twice. ChrisA From ben+python at benfinney.id.au Mon Feb 11 10:57:30 2019 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 12 Feb 2019 02:57:30 +1100 Subject: Can't run setup.py offline due to setup_requires - setup.py calls home References: <026b03c8-7eb6-6166-3efc-6e73b9d103fb@ezaquarii.com> Message-ID: <864l9a8gid.fsf@benfinney.id.au> Chris Narkiewicz via Python-list writes: > debian/rules calls this pip to install all requirements from local > package collection: > > pip3 install --log=... --no-cache --no-index --find-links=pypi > --no-binary=":all:" -r requirements.txt As you have observed, this fails because Setuptools does not correctly handle the ?setup_requires? option . > Directory pypi contains ALL dependencies required for build. All of the build dependencies, *including* the ones specified in ?setup_requires?? > This works ok when build server has connection to network, but fails for > offline builds. I pinpointed the problem to package Automat, that > specifies some dependencies via setup_requires=[...]: > > setup( > ..., > setup_requires=[ > 'setuptools-scm', > 'm2r', > ], > ... > ) To avoid the Setuptools bug, the PyPA recommends dropping the ?setup_requires? option and instead specifying build dependencies in a PEP 518 formatted metadata file . You could create such a specification (by adding a metadata file), ensure those dependencies are also present locally, and meanwhile present the maintainers of this project with your merge request to add that metadata file. -- \ ?What do religious fundamentalists and big media corporations | `\ have in common? They believe that they own culture, they are so | _o__) self-righteous about it ?? ?Nina Paley, 2011 | Ben Finney From 2QdxY4RzWzUUiLuE at potatochowder.com Mon Feb 11 10:45:09 2019 From: 2QdxY4RzWzUUiLuE at potatochowder.com (Dan Sommers) Date: Mon, 11 Feb 2019 09:45:09 -0600 Subject: exit 2 levels of if/else and execute common code In-Reply-To: References: Message-ID: On 2/11/19 9:25 AM, Neal Becker wrote: > I have code with structure: > ``` > if cond1: > [some code] > if cond2: #where cond2 depends on the above [some code] > [ more code] > > else: > [ do xxyy ] > else: > [ do the same xxyy as above ] > ``` > > So what's the best style to handle this? As coded, it violates DRY. > Try/except could be used with a custom exception, but that seems a bit heavy > handed. Suggestions? Put some code, cond2, more code, and xxyy into separate functions, and leave it the way it is. We all have our own levels of acceptable inelegance, and this one fits well within mine. Optionally, instead of separate calls to xxyy, set a flag and only call xxyy at the end if the flag is set. Dan From ndbecker2 at gmail.com Mon Feb 11 11:19:24 2019 From: ndbecker2 at gmail.com (Neal Becker) Date: Mon, 11 Feb 2019 11:19:24 -0500 Subject: exit 2 levels of if/else and execute common code References: Message-ID: Chris Angelico wrote: > On Tue, Feb 12, 2019 at 2:27 AM Neal Becker wrote: >> >> I have code with structure: >> ``` >> if cond1: >> [some code] >> if cond2: #where cond2 depends on the above [some code] >> [ more code] >> >> else: >> [ do xxyy ] >> else: >> [ do the same xxyy as above ] >> ``` >> >> So what's the best style to handle this? As coded, it violates DRY. >> Try/except could be used with a custom exception, but that seems a bit >> heavy >> handed. Suggestions? > > One common way to do this is to toss a "return" after the cond2 block. > Means this has to be the end of a function, but that's usually not > hard. Or, as Rhodri suggested, refactor xxyy into a function, which > you then call twice. > > ChrisA Not bad, but turns out it would be the same return statement for both the normal return path (cond1 and cond2 satisfied) as well as the abnormal return, so not really much of an improvement. From Irv at furrypants.com Mon Feb 11 11:17:20 2019 From: Irv at furrypants.com (Irv Kalb) Date: Mon, 11 Feb 2019 08:17:20 -0800 Subject: exit 2 levels of if/else and execute common code In-Reply-To: References: Message-ID: > On Feb 11, 2019, at 7:25 AM, Neal Becker wrote: > > I have code with structure: > ``` > if cond1: > [some code] > if cond2: #where cond2 depends on the above [some code] > [ more code] > > else: > [ do xxyy ] > else: > [ do the same xxyy as above ] > ``` > > So what's the best style to handle this? As coded, it violates DRY. > Try/except could be used with a custom exception, but that seems a bit heavy > handed. Suggestions? > I like the additional function approach others have mentioned. But if you want a different approach, you could do this: runExtraCode = True # set some Boolean if cond1: [some code] if cond2: [more code] runExtraCode = False # both conditions met, no need to run the extra code if runExtraCode: [do xxyy] Irv From rosuav at gmail.com Mon Feb 11 11:22:43 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 12 Feb 2019 03:22:43 +1100 Subject: exit 2 levels of if/else and execute common code In-Reply-To: References: Message-ID: On Tue, Feb 12, 2019 at 3:21 AM Neal Becker wrote: > > Chris Angelico wrote: > > > On Tue, Feb 12, 2019 at 2:27 AM Neal Becker wrote: > >> > >> I have code with structure: > >> ``` > >> if cond1: > >> [some code] > >> if cond2: #where cond2 depends on the above [some code] > >> [ more code] > >> > >> else: > >> [ do xxyy ] > >> else: > >> [ do the same xxyy as above ] > >> ``` > >> > >> So what's the best style to handle this? As coded, it violates DRY. > >> Try/except could be used with a custom exception, but that seems a bit > >> heavy > >> handed. Suggestions? > > > > One common way to do this is to toss a "return" after the cond2 block. > > Means this has to be the end of a function, but that's usually not > > hard. Or, as Rhodri suggested, refactor xxyy into a function, which > > you then call twice. > > > > ChrisA > > Not bad, but turns out it would be the same return statement for both the > normal return path (cond1 and cond2 satisfied) as well as the abnormal > return, so not really much of an improvement. Not sure what you mean there. The result would be something like this: def frobnicate(): if cond1: do_stuff() if cond2: do_more_stuff() return do_other_stuff() ChrisA From ndbecker2 at gmail.com Mon Feb 11 11:34:03 2019 From: ndbecker2 at gmail.com (Neal Becker) Date: Mon, 11 Feb 2019 11:34:03 -0500 Subject: exit 2 levels of if/else and execute common code References: Message-ID: Chris Angelico wrote: > On Tue, Feb 12, 2019 at 3:21 AM Neal Becker wrote: >> >> Chris Angelico wrote: >> >> > On Tue, Feb 12, 2019 at 2:27 AM Neal Becker >> > wrote: >> >> >> >> I have code with structure: >> >> ``` >> >> if cond1: >> >> [some code] >> >> if cond2: #where cond2 depends on the above [some code] >> >> [ more code] >> >> >> >> else: >> >> [ do xxyy ] >> >> else: >> >> [ do the same xxyy as above ] >> >> ``` >> >> >> >> So what's the best style to handle this? As coded, it violates DRY. >> >> Try/except could be used with a custom exception, but that seems a bit >> >> heavy >> >> handed. Suggestions? >> > >> > One common way to do this is to toss a "return" after the cond2 block. >> > Means this has to be the end of a function, but that's usually not >> > hard. Or, as Rhodri suggested, refactor xxyy into a function, which >> > you then call twice. >> > >> > ChrisA >> >> Not bad, but turns out it would be the same return statement for both the >> normal return path (cond1 and cond2 satisfied) as well as the abnormal >> return, so not really much of an improvement. > > Not sure what you mean there. The result would be something like this: > > def frobnicate(): > if cond1: > do_stuff() > if cond2: > do_more_stuff() > return > do_other_stuff() > > ChrisA sorry, I left out the return: if cond1: [some code] if cond2: #where cond2 depends on the above [some code] [ more code] else: [ do xxyy ] else: [ do the same xxyy as above ] return a, b, c So if we return normally, or return via some other path, the return statement is the same, and would be duplicated. From tjreedy at udel.edu Mon Feb 11 13:18:34 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 11 Feb 2019 13:18:34 -0500 Subject: Zato blog post: A successful Python 3 migration story Message-ID: The migration was from 2.7 to 2.7 and 3.x, rather than 3.x only. I think it worth reading for anyone interested in the subject. https://zato.io/blog/posts/python-3-migration-success-story.html 60,000 lines of Python and Cython, 130 external dependencies (but only 10 not already 3.x ready) took 2 people 80 hours total. Their head start was to write the 2.7 modules, from the beginning, with the following at the top. from __future__ import absolute_import, division, print_function, unicode_literals -- Terry Jan Reedy From barry at barrys-emacs.org Mon Feb 11 14:04:13 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Mon, 11 Feb 2019 19:04:13 +0000 Subject: where is math_sin defined? In-Reply-To: References: <2882349.ObpKs5aGNZ@varric.chelsea.private> Message-ID: > On 10 Feb 2019, at 16:43, Chris Angelico wrote: > > On Mon, Feb 11, 2019 at 3:37 AM Barry Scott wrote: >> >> On Sunday, 10 February 2019 15:15:32 GMT Jon Ribbens wrote: >>> As an aside, how is 'math.sin' actually implemented? mathmodule.c >>> refers to the function 'math_sin' but that name is not defined >>> anywhere in the Python source code. I'm a bit mystified as to how >>> CPython manages to compile! >> >> I used gdb to find it: >> > > Effective, if a little tedious. Tedious? Its fast and accurate only took 20s and got the answer right first time. I had spent a few minutes reading and greping the code and my grep fu found to many false positives. Barry > > My technique was to first confirm that there was nothing saying > "math_sin" anywhere in the repo (trust but verify - doesn't hurt to do > a quick "git grep"), then to search mathmodule.c for "sin(", since > searching for "sin" on its own gave way too many hits. That led me to > the definition of sinpi(), then to asin() and sin(), both being > defined using the FUNC1 template. > > ChrisA > -- > https://mail.python.org/mailman/listinfo/python-list > From hjp-python at hjp.at Mon Feb 11 14:01:20 2019 From: hjp-python at hjp.at (Peter J. Holzer) Date: Mon, 11 Feb 2019 20:01:20 +0100 Subject: Im trying to replicate the youtube video Creating my own customized celebrities with AI. In-Reply-To: <005601d4c187$8e1ba870$aa52f950$@verizon.net> References: <005601d4c187$8e1ba870$aa52f950$@verizon.net> Message-ID: <20190211190120.45ate5tnflwr7za4@hjp.at> On 2019-02-10 16:28:24 -0500, Avi Gross wrote: > >> tenserflow, pygame, scipy, and numby [...] > please mention that the tenser flow should be tensorflow. Eight, sir; seven, sir; Six, sir; five, sir; Four, sir; three, sir; Two, sir; one! Tenser, said the Tensor. Tenser, said the Tensor. Tension, apprehension, And dissention have begun. SCNR, hp -- _ | Peter J. Holzer | we build much bigger, better disasters now |_|_) | | because we have much more sophisticated | | | hjp at hjp.at | management tools. __/ | http://www.hjp.at/ | -- Ross Anderson -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: not available URL: From jadenfigger at gmail.com Mon Feb 11 14:12:31 2019 From: jadenfigger at gmail.com (jadenfigger at gmail.com) Date: Mon, 11 Feb 2019 11:12:31 -0800 (PST) Subject: Replicating YouTube video AI code in python Message-ID: I'm trying to replicate the YouTube video, https://m.youtube.com/watch?v=NTlXEJjfsQU. The videos git hub address is, https://github.com/carykh/alignedCelebFaces. The video says I have to download python 3 and using pip download tensorflow, numpy, scipy, and pygame. I've tried downloading this but keep getting errors when trying to download the packages with pip. If anyone could show me a good tutorial or show me the steps that would be great. Thank you From arj.python at gmail.com Mon Feb 11 14:20:37 2019 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Mon, 11 Feb 2019 23:20:37 +0400 Subject: Replicating YouTube video AI code in python In-Reply-To: References: Message-ID: what python version are you using? what errors are you getting? Abdur-Rahmaan Janhangeer http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ Mauritius From hello at ezaquarii.com Mon Feb 11 14:30:08 2019 From: hello at ezaquarii.com (Chris Narkiewicz) Date: Mon, 11 Feb 2019 19:30:08 +0000 Subject: Can't run setup.py offline due to setup_requires - setup.py calls home In-Reply-To: <864l9a8gid.fsf@benfinney.id.au> References: <026b03c8-7eb6-6166-3efc-6e73b9d103fb@ezaquarii.com> <864l9a8gid.fsf@benfinney.id.au> Message-ID: <70496937-39d1-b833-005b-732aba4e6b2d@ezaquarii.com> On 11/02/2019 15:57, Ben Finney wrote: > All of the build dependencies, *including* the ones specified in > ?setup_requires?? Yes. easy_install simply doesn't look there. If I provide ~/.pydistutils.cfg with a path to find_links, it works ok. Config file in $HOME however is no-go for a CI or build servers, as I have no control over automated build environment (launchpad.net in this case). > To avoid the Setuptools bug, the PyPA recommends dropping the > ?setup_requires? option and instead specifying build dependencies in a > PEP 518 formatted metadata file > . Ok, I took Automat-0.7.tar.gz package and I modified it: 1) removed setup_requires 2) added pyproject.toml with content: [build-system] requires = ["setuptools-scm", "m2r"] 3) package is dropped into pypi directory with all dependencies. However, when I try to install Automat from source, it doesn't work. (venv)$ pip3 install --no-index --find-links=pypi --no-binary=':all:' --no-cache Automat I see that those build-time dependencies are not installed and build complains about missing scm and m2r packages. Is there any extra step I have to take? Best regards, Chris -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: OpenPGP digital signature URL: From felix at epepm.cupet.cu Mon Feb 11 14:29:59 2019 From: felix at epepm.cupet.cu (Felix Lazaro Carbonell) Date: Mon, 11 Feb 2019 14:29:59 -0500 Subject: more pythonic way Message-ID: <000a01d4c240$2e4a3e20$8adeba60$@epepm.cupet.cu> Hello to everyone: Could you please tell me wich way of writing this method is more pythonic: .. def find_monthly_expenses(month=None, year=None): month = month or datetime.date.today() .. Or it should better be: ... if not month: month = datetime.date.today() .. Cheers, Felix. From grant.b.edwards at gmail.com Mon Feb 11 14:45:37 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Mon, 11 Feb 2019 19:45:37 -0000 (UTC) Subject: more pythonic way References: <000a01d4c240$2e4a3e20$8adeba60$@epepm.cupet.cu> Message-ID: On 2019-02-11, Felix Lazaro Carbonell wrote: > Could you please tell me wich way of writing this method is more pythonic: > > def find_monthly_expenses(month=None, year=None): > month = month or datetime.date.today() > > Or it should better be: > > if not month: > month = datetime.date.today() The most pythonic way is to do this: def find_monthly_expenses(month=datetime.date.today().month, year=datetime.date.today().year): ... And then start a month-long argument on the mailing list about how the behavior of parameter default values is wrong and needs be changed. ;) -- Grant Edwards grant.b.edwards Yow! I always have fun at because I'm out of my gmail.com mind!!! From felix at epepm.cupet.cu Mon Feb 11 14:46:05 2019 From: felix at epepm.cupet.cu (Felix Lazaro Carbonell) Date: Mon, 11 Feb 2019 14:46:05 -0500 Subject: more pythonic way In-Reply-To: <000a01d4c240$2e4a3e20$8adeba60$@epepm.cupet.cu> References: <000a01d4c240$2e4a3e20$8adeba60$@epepm.cupet.cu> Message-ID: <001d01d4c242$6d32c1a0$479844e0$@epepm.cupet.cu> Sorry I meant .. def find_monthly_expenses(month=None, year=None): month = month or datetime.date.today().month .. Or it should better be: ... if not month: month = datetime.date.today().month .. Cheers, Felix. -- https://mail.python.org/mailman/listinfo/python-list From felix at epepm.cupet.cu Mon Feb 11 15:00:43 2019 From: felix at epepm.cupet.cu (Felix Lazaro Carbonell) Date: Mon, 11 Feb 2019 15:00:43 -0500 Subject: more pythonic way In-Reply-To: References: <000a01d4c240$2e4a3e20$8adeba60$@epepm.cupet.cu> Message-ID: <002001d4c244$78b92030$6a2b6090$@epepm.cupet.cu> -----Mensaje original----- De: Python-list [mailto:python-list-bounces+felix=epepm.cupet.cu at python.org] En nombre de Grant Edwards Enviado el: lunes, 11 de febrero de 2019 02:46 p.m. Para: python-list at python.org Asunto: Re: more pythonic way On 2019-02-11, Felix Lazaro Carbonell wrote: > Could you please tell me wich way of writing this method is more pythonic: > > def find_monthly_expenses(month=None, year=None): > month = month or datetime.date.today() > > Or it should better be: > > if not month: > month = datetime.date.today() >The most pythonic way is to do this: > > def find_monthly_expenses(month=datetime.date.today().month, year=datetime.date.today().year): > ... > >And then start a month-long argument on the mailing list about how the behavior of parameter default values is wrong and needs be changed. > >;) > >-- >Grant Edwards grant.b.edwards Yow! I always have fun > at because I'm out of my > gmail.com mind!!! > >-- Thanks Grant: but now I think I should have mentioned that this is a method in a Django model, and default arguments are evaluated once when the method is defined, not each time the method is called. So, your way, wil yield the date when Django was started and not the date in wich this method is called, and the date I intend to get is the one when the method is called. I think that I shouldn't call datetime.date.today() as a default value for the method's parameters. Cheers, Felix. From David.Raymond at tomtom.com Mon Feb 11 15:15:29 2019 From: David.Raymond at tomtom.com (David Raymond) Date: Mon, 11 Feb 2019 20:15:29 +0000 Subject: more pythonic way In-Reply-To: <000a01d4c240$2e4a3e20$8adeba60$@epepm.cupet.cu> References: <000a01d4c240$2e4a3e20$8adeba60$@epepm.cupet.cu> Message-ID: My non-expert vote is for if month is None: month = datetime.date.today().month Because you're checking for your default value, not whether the boolean version of what they did give you is True or False. It's explicit, it's not reliant on any __bool__() function implementations or overrides, etc. -----Original Message----- From: Python-list [mailto:python-list-bounces+david.raymond=tomtom.com at python.org] On Behalf Of Felix Lazaro Carbonell Sent: Monday, February 11, 2019 2:30 PM To: python-list at python.org Subject: more pythonic way Hello to everyone: Could you please tell me wich way of writing this method is more pythonic: .. def find_monthly_expenses(month=None, year=None): month = month or datetime.date.today() .. Or it should better be: ... if not month: month = datetime.date.today() .. Cheers, Felix. -- https://mail.python.org/mailman/listinfo/python-list From avigross at verizon.net Mon Feb 11 15:24:33 2019 From: avigross at verizon.net (Avi Gross) Date: Mon, 11 Feb 2019 15:24:33 -0500 Subject: The slash "/" as used in the documentation Message-ID: <004a01d4c247$ccf3e6f0$66dbb4d0$@verizon.net> Ian, I want to make sure we are talking about the same things in the same ways. I will thus limit my comments in this message. If efficiency is your major consideration, then using only positional arguments of known types you can place on the stack and optimize at compile time may be a great way to go. It is not the way python generally goes as it supports objects at run time that can be of any type and in many cases allows any number of arguments to a function. Python was designed till recently in a way that valued some more innovative methods and that included allowing a combination of positional and keyword arguments. The positional arguments can still be called with keyword but only if moved beyond any other positional arguments. What evolved is NOT the only way I have seen this done. But what I see now is not what you are describing in some ways. If you want to talk about recent or planned changes, fine. But make that clear. I was talking about how in the past positional arguments did not have defaults available at the def statement level. I was talking about how use of the symbol "=" in the context of defining function parameters had multiple meanings. It not only established that the parameter accepted a keyword but also provided a default. The allowed syntax required a default, even if it was None. I mean the following fails: >>> def func(a,b=): pass SyntaxError: invalid syntax If in the future (or some other language) you want to allow some way of assigning a default to positional arguments, fine. What I see today does not. So if I understand you, there is a proposal or even plan to change some of the current functionality. I still have not seen anyone post a reference as requested. I am very open to seeing what people are considering or maybe even implementing and in what ways it may not be compatible with present functionality. -----Original Message----- From: Python-list On Behalf Of Ian Kelly Sent: Monday, February 11, 2019 1:46 AM To: Python Subject: Re: The slash "/" as used in the documentation On Sun, Feb 10, 2019 at 2:18 PM Avi Gross wrote: > I am not sure how python implements some of the functionality it does > as compared to other languages with similar features. But I note that > there are rules, presumably some for efficiency, such as requiring all > keyword arguments to be placed after the last positional argument. I > mean I can call > func(a,b,c,d=1,e=2) but not func(a,b,d=1, c, e=2). > > So if you allowed a keyword to optionally be used for any positional > argument other than the last, c, would it not require a change in this rule? > I mean func(a="invalid",b,c,d=1,e=2) would violate the rule and so > would making b a keyword version. In my example, and for this reason > only, maybe c could work. My suggestion was not to allow keyword arguments to arbitrarily replace any positional parameter, or to otherwise change argument-passing in any way. The suggestion was to drop positional-only arguments from functions implemented in C instead of just documenting them better. In the example above, if you want to pass a by keyword, you would have to pass b and c by keyword as well. That would still be true for functions implemented in C if a, b, and c are no longer positional-only. > The original motivation for keyword arguments included the concept of > specifying a default if not used. Positional arguments have no default. > Similarly, they are required if explicitly named in the function definition. > So we are intermingling multiple concepts in the previous design. Positional arguments are allowed to have defaults, and keyword-only arguments are allowed to not have defaults. These are all valid syntax: # Allows a and b to be passed either positionally or by keyword def foo(a=1, b=2): pass # b is keyword-only def foo(a=1, *, b=2): pass # Allows a and b to be passed either positionally or by keyword def foo(a, b): pass # b is keyword-only and has no default def foo(a, *, b): pass Positional-ONLY arguments are not directly supported by the language, but they exist in the C API and can also have defaults. For example, dict.get takes two positional-only arguments. The second argument is optional, and its default is None. My point is that the axis of positional-only versus positional-or-keyword versus keyword-only, and the axis of required versus optional are entirely separate. > I won't go on except to say it would break lots of existing code and > potentially complicate new code. Can you give an example of something that would be broken by updating C API functions to name positional-only arguments instead of just updating them to document that they're positional-only? -- https://mail.python.org/mailman/listinfo/python-list From avigross at verizon.net Mon Feb 11 15:25:30 2019 From: avigross at verizon.net (Avi Gross) Date: Mon, 11 Feb 2019 15:25:30 -0500 Subject: The slash "/" as used in the documentation References: <005501d4c185$b8b9ea30$2a2dbe90$@verizon.net> Message-ID: <004b01d4c247$ef0b78c0$cd226a40$@verizon.net> Ian, I now assume we are no longer talking about the past or even the present but some planned future. In that future we are talking about how to define a function with added or changed functionality. So nothing I wrote earlier really applies because I was talking of how things did work in the absence of the changes needed to make new functionality possible. So let me make sure I understood you. We are talking about the function prototype as in def fun(...) and perhaps the lambda equivalent. The user of the function would only see changes in the help files or other documentation but no special symbols would be used when they invoke a function, albeit new error messages may also happen if done wrong. The earlier messages talked about using a forward slash but you seem to use an asterisk for the same purpose. You can use the very overloaded "*" character as a delimiter between the parameters mentioned to the left and any remaining ones to the right. The "=" symbol is thus now allowed on either side of the divide and now ONLY means there is a default. I thought the discussion was about python, not the C API that arguably is used as much of python is in C directly or indirectly. I thought we were talking about updating everything at the top interpreted python levels. Please make it clear if we are not. Will the above functionality, if understood, break (or modify how it works) existing python code? I mean code that only uses the asterisk to denote multiplication, exponentiation, list expansion and dictionary expansion and so on. Just based on what you wrote, maybe not. It depends on the new meaning of not having a sole asterisk somewhere in the parameter list. If that means evaluate old style, great. If it means something else, I won't speculate but can picture problems. I will talk about your C API question in another message. -----Original Message----- From: Python-list On Behalf Of Ian Kelly Sent: Monday, February 11, 2019 1:46 AM To: Python Subject: Re: The slash "/" as used in the documentation On Sun, Feb 10, 2019 at 2:18 PM Avi Gross wrote: > I am not sure how python implements some of the functionality it does > as compared to other languages with similar features. But I note that > there are rules, presumably some for efficiency, such as requiring all > keyword arguments to be placed after the last positional argument. I > mean I can call > func(a,b,c,d=1,e=2) but not func(a,b,d=1, c, e=2). > > So if you allowed a keyword to optionally be used for any positional > argument other than the last, c, would it not require a change in this rule? > I mean func(a="invalid",b,c,d=1,e=2) would violate the rule and so > would making b a keyword version. In my example, and for this reason > only, maybe c could work. My suggestion was not to allow keyword arguments to arbitrarily replace any positional parameter, or to otherwise change argument-passing in any way. The suggestion was to drop positional-only arguments from functions implemented in C instead of just documenting them better. In the example above, if you want to pass a by keyword, you would have to pass b and c by keyword as well. That would still be true for functions implemented in C if a, b, and c are no longer positional-only. > The original motivation for keyword arguments included the concept of > specifying a default if not used. Positional arguments have no default. > Similarly, they are required if explicitly named in the function definition. > So we are intermingling multiple concepts in the previous design. Positional arguments are allowed to have defaults, and keyword-only arguments are allowed to not have defaults. These are all valid syntax: # Allows a and b to be passed either positionally or by keyword def foo(a=1, b=2): pass # b is keyword-only def foo(a=1, *, b=2): pass # Allows a and b to be passed either positionally or by keyword def foo(a, b): pass # b is keyword-only and has no default def foo(a, *, b): pass Positional-ONLY arguments are not directly supported by the language, but they exist in the C API and can also have defaults. For example, dict.get takes two positional-only arguments. The second argument is optional, and its default is None. My point is that the axis of positional-only versus positional-or-keyword versus keyword-only, and the axis of required versus optional are entirely separate. > I won't go on except to say it would break lots of existing code and > potentially complicate new code. Can you give an example of something that would be broken by updating C API functions to name positional-only arguments instead of just updating them to document that they're positional-only? -- https://mail.python.org/mailman/listinfo/python-list From avigross at verizon.net Mon Feb 11 15:29:18 2019 From: avigross at verizon.net (Avi Gross) Date: Mon, 11 Feb 2019 15:29:18 -0500 Subject: The slash "/" as used in the documentation In-Reply-To: References: <005501d4c185$b8b9ea30$2a2dbe90$@verizon.net> Message-ID: <004e01d4c248$76d436c0$647ca440$@verizon.net> Ian, Again, not having read whatever documentation we may be discussing, I may be very wrong. The topic is the C API. I started using C at Bell Laboratories in 1982 replacing other languages I had used before. I haven't felt a reason to use it in the last few decades as I moved on to yet other languages but am quite aware that many of those languages are largely interpreters written in languages like C or C++ and then regularly patched by adding C-like code to replace slower functionality. I assume the C API you are mentioning refers to the process of how you write and compile and invoke library functions so that the function called will properly place python data structures in some kind of stack so that the C library can properly take it and perform computations. Similarly, when the function returns, some additional mapping may be done. The above is meant to be general and obviously the details matter. C as a stand-alone language used to have pretty much positional arguments and often a fixed number of them. We often had multiple functions we could call with slightly different names if we wanted special effects like not requiring a third argument. Clearly some approaches were less efficient if the two-argument version simply turned around and called the three-argument version with a third argument set to a default. So, often you played other games like requiring a third argument (or in some languages a dangling comma) which the function might choose to replace with a default internally if it is something like -1. Obviously C libraries that are external must be used as-is while internal ones you have written might allow changes. Some languages I have used do not so much support doing lots of command-line level pre-processing of command-line arguments but provide helper functions that can be called within the function call that take all the arguments and do further processing and return a form that the function can more easily use. This works even better when evaluation is lazy and you can access the exact original text the programmer or user typed in before it is evaluated. Python now allows a limited form of that if you ask for *args and **kwargs. So is this mainly about efficiency and C libraries or more? You can easily make a C function that expects positional arguments in a proper order and then make a wrapper in python (or C or FORTRAN or whatever) with a slightly different name that does preprocessing and/or postprocessing. Python is loaded with such functionality that tends to allow more complex things to be done less efficiently. So if the wrapper can evaluate your arguments and figure out what to do with positional arguments, great. The wrapper function might support an optional argument that specifies whether other argument units are in feet, miles, meters or even parsecs and also accept keyword arguments for those measures and rescale them and only then call the efficient C function with all the right arguments in the right positions. If you as a programmer find that slows things down, you can make sure you use the right units for the function and call the underlying function directly with everything in place according to those rules. Ending with this. Is the reality that we are now talking about gradual changes in documentation as individual C functions are updated but not at this point about what normal python users are seeing in terms of the code? If so, I repeat, I was not talking about that and my comments are not applicable and I can go back to doing more useful things away from this forum. -----Original Message----- From: Python-list On Behalf Of Ian Kelly Sent: Monday, February 11, 2019 1:46 AM To: Python Subject: Re: The slash "/" as used in the documentation On Sun, Feb 10, 2019 at 2:18 PM Avi Gross wrote: > I am not sure how python implements some of the functionality it does > as compared to other languages with similar features. But I note that > there are rules, presumably some for efficiency, such as requiring all > keyword arguments to be placed after the last positional argument. I > mean I can call > func(a,b,c,d=1,e=2) but not func(a,b,d=1, c, e=2). > > So if you allowed a keyword to optionally be used for any positional > argument other than the last, c, would it not require a change in this rule? > I mean func(a="invalid",b,c,d=1,e=2) would violate the rule and so > would making b a keyword version. In my example, and for this reason > only, maybe c could work. My suggestion was not to allow keyword arguments to arbitrarily replace any positional parameter, or to otherwise change argument-passing in any way. The suggestion was to drop positional-only arguments from functions implemented in C instead of just documenting them better. In the example above, if you want to pass a by keyword, you would have to pass b and c by keyword as well. That would still be true for functions implemented in C if a, b, and c are no longer positional-only. > The original motivation for keyword arguments included the concept of > specifying a default if not used. Positional arguments have no default. > Similarly, they are required if explicitly named in the function definition. > So we are intermingling multiple concepts in the previous design. Positional arguments are allowed to have defaults, and keyword-only arguments are allowed to not have defaults. These are all valid syntax: # Allows a and b to be passed either positionally or by keyword def foo(a=1, b=2): pass # b is keyword-only def foo(a=1, *, b=2): pass # Allows a and b to be passed either positionally or by keyword def foo(a, b): pass # b is keyword-only and has no default def foo(a, *, b): pass Positional-ONLY arguments are not directly supported by the language, but they exist in the C API and can also have defaults. For example, dict.get takes two positional-only arguments. The second argument is optional, and its default is None. My point is that the axis of positional-only versus positional-or-keyword versus keyword-only, and the axis of required versus optional are entirely separate. > I won't go on except to say it would break lots of existing code and > potentially complicate new code. Can you give an example of something that would be broken by updating C API functions to name positional-only arguments instead of just updating them to document that they're positional-only? -- https://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Mon Feb 11 15:31:54 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 12 Feb 2019 07:31:54 +1100 Subject: The slash "/" as used in the documentation In-Reply-To: <004a01d4c247$ccf3e6f0$66dbb4d0$@verizon.net> References: <004a01d4c247$ccf3e6f0$66dbb4d0$@verizon.net> Message-ID: On Tue, Feb 12, 2019 at 7:26 AM Avi Gross wrote: > If you want to talk about recent or planned changes, fine. But make that > clear. I was talking about how in the past positional arguments did not have > defaults available at the def statement level. I was talking about how use > of the symbol "=" in the context of defining function parameters had > multiple meanings. It not only established that the parameter accepted a > keyword but also provided a default. When was this the case? Positional arguments with defaults is a concept known in MANY languages, including C. The equals sign has nothing to do with keyword arguments. Calling on the D'Aprano Collection of Ancient Pythons for confirmation here, but I strongly suspect that positional arguments with defaults go back all the way to 1.x. ChrisA From tjreedy at udel.edu Mon Feb 11 15:35:31 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 11 Feb 2019 15:35:31 -0500 Subject: more pythonic way In-Reply-To: <001d01d4c242$6d32c1a0$479844e0$@epepm.cupet.cu> References: <000a01d4c240$2e4a3e20$8adeba60$@epepm.cupet.cu> <001d01d4c242$6d32c1a0$479844e0$@epepm.cupet.cu> Message-ID: On 2/11/2019 2:46 PM, Felix Lazaro Carbonell wrote: > def find_monthly_expenses(month=None, year=None): > month = month or datetime.date.today().month > > Or it should better be: > if not month: > month = datetime.date.today().month As a 20+ year veteran, I would be fine either way. -- Terry Jan Reedy From sivan at vitakka.co Mon Feb 11 15:36:18 2019 From: sivan at vitakka.co (=?UTF-8?Q?Sivan_Gr=C3=BCnberg?=) Date: Mon, 11 Feb 2019 22:36:18 +0200 Subject: more pythonic way In-Reply-To: References: <000a01d4c240$2e4a3e20$8adeba60$@epepm.cupet.cu> Message-ID: +1 with David Raymond, it's nice to use condensed style when it leaves things readable and logic. But if in doubt: "Explicit is better than implicit. Simple is better than complex." :) -Sivan On Mon, Feb 11, 2019 at 10:19 PM David Raymond wrote: > My non-expert vote is for > > if month is None: > month = datetime.date.today().month > > Because you're checking for your default value, not whether the boolean > version of what they did give you is True or False. It's explicit, it's not > reliant on any __bool__() function implementations or overrides, etc. > > > -----Original Message----- > From: Python-list [mailto:python-list-bounces+david.raymond= > tomtom.com at python.org] On Behalf Of Felix Lazaro Carbonell > Sent: Monday, February 11, 2019 2:30 PM > To: python-list at python.org > Subject: more pythonic way > > > > Hello to everyone: > > Could you please tell me wich way of writing this method is more pythonic: > > > > .. > > def find_monthly_expenses(month=None, year=None): > > month = month or datetime.date.today() > > .. > > > > Or it should better be: > > ... > > if not month: > > month = datetime.date.today() > > .. > > > > Cheers, > > Felix. > > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- > https://mail.python.org/mailman/listinfo/python-list > -- Sivan Greenberg Co founder & CTO Vitakka Consulting From __peter__ at web.de Mon Feb 11 15:40:14 2019 From: __peter__ at web.de (Peter Otten) Date: Mon, 11 Feb 2019 21:40:14 +0100 Subject: more pythonic way References: <000a01d4c240$2e4a3e20$8adeba60$@epepm.cupet.cu> Message-ID: Grant Edwards wrote: > On 2019-02-11, Felix Lazaro Carbonell wrote: > >> Could you please tell me wich way of writing this method is more >> pythonic: >> >> def find_monthly_expenses(month=None, year=None): >> month = month or datetime.date.today() >> >> Or it should better be: >> >> if not month: >> month = datetime.date.today() > > The most pythonic way is to do this: > > def find_monthly_expenses(month=datetime.date.today().month, > year=datetime.date.today().year): > ... > > And then start a month-long argument on the mailing list about how the > behavior of parameter default values is wrong and needs be changed. > > ;) > As far as arguments go I was thinking more about how this code can try and find the december expenses when it's only january ;) From robertvstepp at gmail.com Mon Feb 11 15:51:59 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Mon, 11 Feb 2019 14:51:59 -0600 Subject: The slash "/" as used in the documentation In-Reply-To: References: <004a01d4c247$ccf3e6f0$66dbb4d0$@verizon.net> Message-ID: On Mon, Feb 11, 2019 at 2:34 PM Chris Angelico wrote: > Calling on the D'Aprano Collection of Ancient Pythons for confirmation > here, but I strongly suspect that positional arguments with defaults > go back all the way to 1.x. Has Steve's banishment ended yet? The only postings I have recently seen from him have been on the Tutor list. -- boB From ian.g.kelly at gmail.com Mon Feb 11 15:52:24 2019 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 11 Feb 2019 13:52:24 -0700 Subject: The slash "/" as used in the documentation In-Reply-To: References: <004a01d4c247$ccf3e6f0$66dbb4d0$@verizon.net> Message-ID: On Mon, Feb 11, 2019 at 1:35 PM Chris Angelico wrote: > > On Tue, Feb 12, 2019 at 7:26 AM Avi Gross wrote: > > If you want to talk about recent or planned changes, fine. But make that > > clear. I was talking about how in the past positional arguments did not have > > defaults available at the def statement level. I was talking about how use > > of the symbol "=" in the context of defining function parameters had > > multiple meanings. It not only established that the parameter accepted a > > keyword but also provided a default. > > When was this the case? Positional arguments with defaults is a > concept known in MANY languages, including C. The equals sign has > nothing to do with keyword arguments. > > Calling on the D'Aprano Collection of Ancient Pythons for confirmation > here, but I strongly suspect that positional arguments with defaults > go back all the way to 1.x. The archived documentation shows that both parameter defaults and keyword arguments have been around since at least 1.4. I can't directly confirm that required parameters could be passed by keyword, but I see nothing in there that says they can't, or that appears to conflate parameters having defaults with keyword arguments, which is as one would expect -- whether an argument is passed positionally or by keyword is a property of the function *call* expression, whereas whether a parameter has a default or not is a property of the function *definition*. They happen to both use the = symbol followed by an expression, but in different syntactical environments, and that is the only correlation between them. From __peter__ at web.de Mon Feb 11 15:54:34 2019 From: __peter__ at web.de (Peter Otten) Date: Mon, 11 Feb 2019 21:54:34 +0100 Subject: more pythonic way References: <000a01d4c240$2e4a3e20$8adeba60$@epepm.cupet.cu> Message-ID: Felix Lazaro Carbonell wrote: > Hello to everyone: > Could you please tell me wich way of writing this method is more pythonic: > def find_monthly_expenses(month=None, year=None): > > month = month or datetime.date.today() > Or it should better be: > if not month: > month = datetime.date.today() Personally I would avoid a default because I'm unsure whether the current or the previous month is the right default. Also, the default month combined with a specific year doesn't make much sense to me, and someone who specifies find_monthly_expenses(month=6) in May probably wants the June of the past year... Keep the function simple an make the arguments non-optional. If you can come up with a nice heuristic put it in a separate function. From ian.g.kelly at gmail.com Mon Feb 11 16:00:13 2019 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Mon, 11 Feb 2019 14:00:13 -0700 Subject: The slash "/" as used in the documentation In-Reply-To: References: <004a01d4c247$ccf3e6f0$66dbb4d0$@verizon.net> Message-ID: On Mon, Feb 11, 2019 at 1:56 PM boB Stepp wrote: > > On Mon, Feb 11, 2019 at 2:34 PM Chris Angelico wrote: > > > Calling on the D'Aprano Collection of Ancient Pythons for confirmation > > here, but I strongly suspect that positional arguments with defaults > > go back all the way to 1.x. > > Has Steve's banishment ended yet? The only postings I have recently > seen from him have been on the Tutor list. It should have ended some time in December. Perhaps he's decided not to return. From hello at ezaquarii.com Mon Feb 11 16:01:34 2019 From: hello at ezaquarii.com (Chris Narkiewicz) Date: Mon, 11 Feb 2019 21:01:34 +0000 Subject: Can't run setup.py offline due to setup_requires - setup.py calls home In-Reply-To: <70496937-39d1-b833-005b-732aba4e6b2d@ezaquarii.com> References: <026b03c8-7eb6-6166-3efc-6e73b9d103fb@ezaquarii.com> <864l9a8gid.fsf@benfinney.id.au> <70496937-39d1-b833-005b-732aba4e6b2d@ezaquarii.com> Message-ID: <2fb037ba-b0fe-1e0a-8b1e-f56febb65674@ezaquarii.com> On 11/02/2019 19:30, Chris Narkiewicz via Python-list wrote: > Is there any extra step I have to take? Ok, I'll respond to myself, as this was really silly. Debian ships hopelessly obsolete pip 9.PEP 518 is supported in pip 10+. Cheers, Chris -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: OpenPGP digital signature URL: From ijkl at netc.fr Mon Feb 11 14:48:44 2019 From: ijkl at netc.fr (Jimmy Girardet) Date: Mon, 11 Feb 2019 20:48:44 +0100 Subject: more pythonic way In-Reply-To: <000a01d4c240$2e4a3e20$8adeba60$@epepm.cupet.cu> References: <000a01d4c240$2e4a3e20$8adeba60$@epepm.cupet.cu> Message-ID: The first one is used very often. Less verbose Le 11 f?vr. 2019 ? 20:41, ? 20:41, Felix Lazaro Carbonell a ?crit: > > >Hello to everyone: > >Could you please tell me wich way of writing this method is more >pythonic: > > > >.. > > def find_monthly_expenses(month=None, year=None): > > month = month or datetime.date.today() > >.. > > > >Or it should better be: > >... > > if not month: > > month = datetime.date.today() > >.. > > > >Cheers, > >Felix. > > > >-- >https://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Mon Feb 11 16:21:11 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 12 Feb 2019 08:21:11 +1100 Subject: The slash "/" as used in the documentation In-Reply-To: <005501d4c24e$97d037b0$c770a710$@verizon.net> References: <004a01d4c247$ccf3e6f0$66dbb4d0$@verizon.net> <005501d4c24e$97d037b0$c770a710$@verizon.net> Message-ID: On Tue, Feb 12, 2019 at 8:13 AM Avi Gross wrote: > > > Just Chris, Can we keep things on the list please? > I am thinking I missed the point of this discussion thus what I say makes no > sense. Not sure. You were fairly specific with your statements about how things supposedly were in the past. What point of the discussion did you miss that led you to say that? > Yes, the equals is THE way you supply defaults for things you optionally > want the user to be able to override. I seem to be talking top-level python > and you guys are elsewhere so I must be in another discussion. > > On Tue, Feb 12, 2019 at 7:26 AM Avi Gross wrote: > > If you want to talk about recent or planned changes, fine. But make > > that clear. I was talking about how in the past positional arguments > > did not have defaults available at the def statement level. I was > > talking about how use of the symbol "=" in the context of defining > > function parameters had multiple meanings. It not only established > > that the parameter accepted a keyword but also provided a default. You state this as fact. That's what I responded to. What do you mean by "top level Python" that might make your statement true? ChrisA From jadenfigger at gmail.com Mon Feb 11 17:30:39 2019 From: jadenfigger at gmail.com (jadenfigger at gmail.com) Date: Mon, 11 Feb 2019 14:30:39 -0800 (PST) Subject: Replicating YouTube video AI code in python In-Reply-To: References: Message-ID: <9368755d-afe0-4209-8e59-92e7848760a9@googlegroups.com> The error I get typing pip install -U tensorflow into the command terminal Could not find a version that satisfies the requirement tensorflow (from versions: ) No matching distribution found for tensorflow. Ive been able to install numpy, scipy, and pygame. Tensorflow is the only package that gives this error. I've tried using python 3.7 and 3.6 From cs at cskk.id.au Mon Feb 11 20:53:07 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 12 Feb 2019 12:53:07 +1100 Subject: exit 2 levels of if/else and execute common code In-Reply-To: References: Message-ID: <20190212015307.GA45753@cskk.homeip.net> On 11Feb2019 08:17, Irv Kalb wrote: >> On Feb 11, 2019, at 7:25 AM, Neal Becker wrote: >> I have code with structure: >> ``` >> if cond1: >> [some code] >> if cond2: #where cond2 depends on the above [some code] >> [ more code] >> >> else: >> [ do xxyy ] >> else: >> [ do the same xxyy as above ] >> ``` >> >> So what's the best style to handle this? As coded, it violates DRY. >> Try/except could be used with a custom exception, but that seems a bit heavy >> handed. Suggestions? >> > >I like the additional function approach others have mentioned. But if you want a different approach, you could do this: > >runExtraCode = True # set some Boolean >if cond1: > [some code] > if cond2: > [more code] > runExtraCode = False # both conditions met, no need to run the extra code > >if runExtraCode: > [do xxyy] I want to second this. As others have said, there's a trivialness threshold where you wouldn't bother, and that is subjective. But when you do, the above is often useful. If "runExtraCode" bothers you, consider "need_task", with a suitable "task" name. I've got several things in the above form. The most recent I fiddled with was last night, in a monitor script which updates some information. It takes the form: updated = False while True: time.sleep(INTERSCAN_DELAY) for rpath, dirnames, filenames in os.walk(...): ... for filename in filenames: if new file found: scan file ... updated = true if updated: update top level record ... updated = False ... which updates a state file on a per subdirectory frequency if something new is found. This falls into exactly the pattern Irv describes. You'll notice my example starts with the flag variable being False, and makes it True if the extra code should run. I personally prefer that pattern (I like things to generally start False, or zero, or empty), but what values you use will depend on what you're doing. Cheers, Cameron Simpson From jayapriya.besant at gmail.com Tue Feb 12 00:54:33 2019 From: jayapriya.besant at gmail.com (Jaya Priya) Date: Mon, 11 Feb 2019 21:54:33 -0800 (PST) Subject: comp.lang.python Message-ID: <28e19ec3-c00a-4584-801b-745a6296f3f7@googlegroups.com> The comp.lang.python.announce newsgroup (or c.l.py.a for short) has been created in early 1998 as a companion newsgroup for comp.lang.python focused on Python-related announcements. ... other items of general interest to the Python community. https://www.gangboard.com/big-data-training/data-science-training From hobson42 at gmail.com Tue Feb 12 05:57:55 2019 From: hobson42 at gmail.com (Ian Hobson) Date: Tue, 12 Feb 2019 10:57:55 +0000 Subject: comp.lang.python In-Reply-To: <28e19ec3-c00a-4584-801b-745a6296f3f7@googlegroups.com> References: <28e19ec3-c00a-4584-801b-745a6296f3f7@googlegroups.com> Message-ID: On 12/02/2019 05:54, Jaya Priya wrote: > The comp.lang.python.announce newsgroup (or c.l.py.a for short) has been created in early 1998 as a companion newsgroup for comp.lang.python focused on Python-related announcements. ... other items of general interest to the Python community. > https://www.gangboard.com/big-data-training/data-science-training > Wow! I know Nagels algorithm backed off, but over 20 years! Impressive! Ian From elchino at cnn.cn Tue Feb 12 07:03:44 2019 From: elchino at cnn.cn (ElChino) Date: Tue, 12 Feb 2019 13:03:44 +0100 Subject: Intel Distribution for Python Message-ID: I just got an Intel newsletter describing "Intel Distribution for Python". Quote: With Intel? Distribution for Python, it?s not unusual to see speed-ups of 20x (or more) for numerically intensive Python codes like those that use the Numpy* and SciPy* stack. Sounds very exiting; 20 times speed-up! Has anybody tried it and found this claim to be true? Ref: https://software.intel.com/en-us/distribution-for-python/ From Gronicus at SGA.Ninja Tue Feb 12 13:56:27 2019 From: Gronicus at SGA.Ninja (Steve) Date: Tue, 12 Feb 2019 13:56:27 -0500 Subject: A limit to writing to a file from a loop? Message-ID: <000001d4c304$a8fac800$faf05800$@SGA.Ninja> My program reads from a text file (A), modifies the data, and writes to another file (B). This works until I reach about 300 writes and no more lines are written to file (B). I had to create a Counter and increment it to 250 when it gets reset. Upon reset, I close the file (B) being written and reopen it for append. Then it accepts the entire list of lines of data. Bizarre? CycleCounter += 1 if CycleCounter > 250: CycleCounter = 1 DateReadings.close() DateReadings=open("Date-ReadingsAndDoses.txt", "a") DateReadings.write("{0:15} {1:>8} {2:>8} {3:>8} {4:<2} {5:>8} {6:>8} {7:>10}".format (ThisTimeDate, ThisReading, ThisDose1, ThisSensor, ThisTrend, ThisTS, ThisPercent, SensorNumberDay2) + "\n") From rosuav at gmail.com Tue Feb 12 14:09:31 2019 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 13 Feb 2019 06:09:31 +1100 Subject: A limit to writing to a file from a loop? In-Reply-To: <000001d4c304$a8fac800$faf05800$@SGA.Ninja> References: <000001d4c304$a8fac800$faf05800$@SGA.Ninja> Message-ID: On Wed, Feb 13, 2019 at 5:58 AM Steve wrote: > > My program reads from a text file (A), modifies the data, and writes to another file (B). > This works until I reach about 300 writes and no more lines are written to file (B). > > I had to create a Counter and increment it to 250 when it gets reset. > > Upon reset, I close the file (B) being written and reopen it for append. > > Then it accepts the entire list of lines of data. > > > > Bizarre? Without knowing what else is going on, we have no idea what the problem is. All we see is your workaround. ChrisA From PythonList at DancesWithMice.info Tue Feb 12 14:12:43 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Wed, 13 Feb 2019 08:12:43 +1300 Subject: A limit to writing to a file from a loop? In-Reply-To: <000001d4c304$a8fac800$faf05800$@SGA.Ninja> References: <000001d4c304$a8fac800$faf05800$@SGA.Ninja> Message-ID: Steve, On 13/02/19 7:56 AM, Steve wrote: > My program reads from a text file (A), modifies the data, and writes to another file (B). > This works until I reach about 300 writes and no more lines are written to file (B). > I had to create a Counter and increment it to 250 when it gets reset. > Upon reset, I close the file (B) being written and reopen it for append. > Then it accepts the entire list of lines of data. > Bizarre? > > > CycleCounter += 1 > if CycleCounter > 250: > CycleCounter = 1 > DateReadings.close() > DateReadings=open("Date-ReadingsAndDoses.txt", "a") > DateReadings.write("{0:15} {1:>8} {2:>8} {3:>8} {4:<2} {5:>8} {6:>8} {7:>10}".format > (ThisTimeDate, ThisReading, ThisDose1, ThisSensor, ThisTrend, > ThisTS, ThisPercent, SensorNumberDay2) + "\n") Perhaps part of the transfer to email, but won't this code only output once per 250 records, cf 250 records per cycle? Some suggestions:- Test one: Prove that there is valid input - is the code successfully reading more than '250' records? Test two: Check the actual data - is there anything strange about the '250th' record (the one that first exhibits the problem)? Test three: Prove that it is not some Python<->file system issue - what happens if the first input record is written (to a 'junk file') 250+ times? Test four: (possibly unnecessary after the above) Disprove a size issue - what happens if you output a simple/hello world string (of the same number of bytes) instead of the incoming data, 250+ times? Report back. If there are still problems, please show the original code and exception report... -- Regards =dn From hjp-python at hjp.at Tue Feb 12 14:17:25 2019 From: hjp-python at hjp.at (Peter J. Holzer) Date: Tue, 12 Feb 2019 20:17:25 +0100 Subject: The slash "/" as used in the documentation In-Reply-To: References: <004a01d4c247$ccf3e6f0$66dbb4d0$@verizon.net> Message-ID: <20190212191725.ggja3tso64v77bk5@hjp.at> On 2019-02-12 07:31:54 +1100, Chris Angelico wrote: > Positional arguments with defaults is a concept known in MANY > languages, True. > including C. Nope. At least not until C99, and I can't find anything in C11 either. Maybe they'll add it in C2x. hp -- _ | Peter J. Holzer | we build much bigger, better disasters now |_|_) | | because we have much more sophisticated | | | hjp at hjp.at | management tools. __/ | http://www.hjp.at/ | -- Ross Anderson -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: not available URL: From Gronicus at SGA.Ninja Tue Feb 12 14:19:48 2019 From: Gronicus at SGA.Ninja (Steve) Date: Tue, 12 Feb 2019 14:19:48 -0500 Subject: I cannot seem to write time/date to the file. Message-ID: <000701d4c307$ec9e9430$c5dbbc90$@SGA.Ninja> I am using nowTimeDate2 elsewhere in my program and it can be printed to the screen. The file exists and the program successfully opens the file, writes to it, and closes the file. All is working as designed. However, Now I want to place an entry into the first line of the file to note the time/date the file was created. It doesn?t show up in the file. How do I fix this? from time import gmtime, strftime nowTimeDate2=strftime("%Y %a %b %d %H:%M") print(nowTimeDate2) #This works DateReading=open("Date-ReadingsAndDoses.txt", "w") DateReading.write = (nowTimeDate2 + "\n") DateReading.close() I also tried to append but that did not work either. Interestingly enough, this is a feature that really has no real purpose in the final product yet it is hounding me. Footnote: Ultrasound Technician Asks Pregnant Woman If She?d Like To Know Baby?s Name From arj.python at gmail.com Tue Feb 12 14:22:05 2019 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Tue, 12 Feb 2019 23:22:05 +0400 Subject: Replicating YouTube video AI code in python In-Reply-To: <9368755d-afe0-4209-8e59-92e7848760a9@googlegroups.com> References: <9368755d-afe0-4209-8e59-92e7848760a9@googlegroups.com> Message-ID: try 3.6.6 Abdur-Rahmaan Janhangeer http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ Mauritius From David.Raymond at tomtom.com Tue Feb 12 14:28:22 2019 From: David.Raymond at tomtom.com (David Raymond) Date: Tue, 12 Feb 2019 19:28:22 +0000 Subject: I cannot seem to write time/date to the file. In-Reply-To: <000701d4c307$ec9e9430$c5dbbc90$@SGA.Ninja> References: <000701d4c307$ec9e9430$c5dbbc90$@SGA.Ninja> Message-ID: DateReading.write = (nowTimeDate2 + "\n") You're re-assigning the write method to be a string here. You want to call the method with the string as the argument. DateReading.write(nowTimeDate2 + "\n") -----Original Message----- From: Python-list [mailto:python-list-bounces+david.raymond=tomtom.com at python.org] On Behalf Of Steve Sent: Tuesday, February 12, 2019 2:20 PM To: python-list at python.org Subject: I cannot seem to write time/date to the file. I am using nowTimeDate2 elsewhere in my program and it can be printed to the screen. The file exists and the program successfully opens the file, writes to it, and closes the file. All is working as designed. However, Now I want to place an entry into the first line of the file to note the time/date the file was created. It doesn?t show up in the file. How do I fix this? from time import gmtime, strftime nowTimeDate2=strftime("%Y %a %b %d %H:%M") print(nowTimeDate2) #This works DateReading=open("Date-ReadingsAndDoses.txt", "w") DateReading.write = (nowTimeDate2 + "\n") DateReading.close() I also tried to append but that did not work either. Interestingly enough, this is a feature that really has no real purpose in the final product yet it is hounding me. Footnote: Ultrasound Technician Asks Pregnant Woman If She?d Like To Know Baby?s Name -- https://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Tue Feb 12 14:30:04 2019 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 13 Feb 2019 06:30:04 +1100 Subject: The slash "/" as used in the documentation In-Reply-To: <20190212191725.ggja3tso64v77bk5@hjp.at> References: <004a01d4c247$ccf3e6f0$66dbb4d0$@verizon.net> <20190212191725.ggja3tso64v77bk5@hjp.at> Message-ID: On Wed, Feb 13, 2019 at 6:18 AM Peter J. Holzer wrote: > > On 2019-02-12 07:31:54 +1100, Chris Angelico wrote: > > Positional arguments with defaults is a concept known in MANY > > languages, > > True. > > > including C. > > Nope. At least not until C99, and I can't find anything in C11 either. > Maybe they'll add it in C2x. > Huh, my bad. C++ has it, but C still doesn't. Anyhow, defaults on function arguments are supported in WAY more languages than keyword arguments are. ChrisA From Gronicus at SGA.Ninja Tue Feb 12 15:33:32 2019 From: Gronicus at SGA.Ninja (Steve) Date: Tue, 12 Feb 2019 15:33:32 -0500 Subject: A limit to writing to a file from a loop? In-Reply-To: References: <000001d4c304$a8fac800$faf05800$@SGA.Ninja> Message-ID: <000c01d4c312$3912fb80$ab38f280$@SGA.Ninja> Very good points. In short, I wrote two test programs. One to write 400 lines of data into a file. Another to read it, modify it, and write to a new file. With some questionable spacing in the final file, I was able to read/write 400 lines of code disproving my original suspicion. Back to the drawing board but at least I can write all the lines using my work around in the original program. The question is why is there a difference?. I will post again if I find something. The plot thickens. TTKMAWAN... >>> Test one: Prove that there is valid input >>> - is the code successfully reading more than '250' records? Yes, when I close the file and reopen it, all 500 records are read/written. I placed an additional line of data into the file being written which should have pushed an offending line of data to the 251st position. 250 lines were read/written and the original 250th line was not written. >>> Test two: Check the actual data >>> - is there anything strange about the '250th' record (the one that first >>> exhibits the problem)? I placed an additional line of data at the beginning of file being read, the writing of said data should have pushed an offending line of data being written to the 251st position. 250 lines were read/written and the original 250th line was not written. >>> Test three: Prove that it is not some Python<->file system issue >>> - what happens if the first input record is written (to a 'junk file') >>> 250+ times? >>> Test four: (possibly unnecessary after the above) Disprove a size issue >>> - what happens if you output a simple/hello world string (of the same >>> number of bytes) instead of the incoming data, 250+ times? Footnote: Ultrasound Technician Asks Pregnant Woman If She?d Like to Know Baby?s Name... -----Original Message----- From: DL Neil Sent: Tuesday, February 12, 2019 2:13 PM To: Steve ; python-list at python.org Subject: Re: A limit to writing to a file from a loop? Steve, On 13/02/19 7:56 AM, Steve wrote: > My program reads from a text file (A), modifies the data, and writes to another file (B). > This works until I reach about 300 writes and no more lines are written to file (B). > I had to create a Counter and increment it to 250 when it gets reset. > Upon reset, I close the file (B) being written and reopen it for append. > Then it accepts the entire list of lines of data. > Bizarre? > > > CycleCounter += 1 > if CycleCounter > 250: > CycleCounter = 1 > DateReadings.close() > DateReadings=open("Date-ReadingsAndDoses.txt", "a") > DateReadings.write("{0:15} {1:>8} {2:>8} {3:>8} {4:<2} {5:>8} {6:>8} {7:>10}".format > (ThisTimeDate, ThisReading, ThisDose1, ThisSensor, ThisTrend, > ThisTS, ThisPercent, SensorNumberDay2) > + "\n") Perhaps part of the transfer to email, but won't this code only output once per 250 records, cf 250 records per cycle? Some suggestions:- Test one: Prove that there is valid input - is the code successfully reading more than '250' records? Test two: Check the actual data - is there anything strange about the '250th' record (the one that first exhibits the problem)? Test three: Prove that it is not some Python<->file system issue - what happens if the first input record is written (to a 'junk file') 250+ times? Test four: (possibly unnecessary after the above) Disprove a size issue - what happens if you output a simple/hello world string (of the same number of bytes) instead of the incoming data, 250+ times? Report back. If there are still problems, please show the original code and exception report... -- Regards =dn Footnote: Ultrasound Technician Asks Pregnant Woman If She?d Like To Know Baby?s Name -----Original Message----- From: DL Neil Sent: Tuesday, February 12, 2019 2:13 PM To: Steve ; python-list at python.org Subject: Re: A limit to writing to a file from a loop? Steve, On 13/02/19 7:56 AM, Steve wrote: > My program reads from a text file (A), modifies the data, and writes to another file (B). > This works until I reach about 300 writes and no more lines are written to file (B). > I had to create a Counter and increment it to 250 when it gets reset. > Upon reset, I close the file (B) being written and reopen it for append. > Then it accepts the entire list of lines of data. > Bizarre? > > > CycleCounter += 1 > if CycleCounter > 250: > CycleCounter = 1 > DateReadings.close() > DateReadings=open("Date-ReadingsAndDoses.txt", "a") > DateReadings.write("{0:15} {1:>8} {2:>8} {3:>8} {4:<2} {5:>8} {6:>8} {7:>10}".format > (ThisTimeDate, ThisReading, ThisDose1, ThisSensor, ThisTrend, > ThisTS, ThisPercent, SensorNumberDay2) > + "\n") Perhaps part of the transfer to email, but won't this code only output once per 250 records, cf 250 records per cycle? Some suggestions:- Test one: Prove that there is valid input - is the code successfully reading more than '250' records? Test two: Check the actual data - is there anything strange about the '250th' record (the one that first exhibits the problem)? Test three: Prove that it is not some Python<->file system issue - what happens if the first input record is written (to a 'junk file') 250+ times? Test four: (possibly unnecessary after the above) Disprove a size issue - what happens if you output a simple/hello world string (of the same number of bytes) instead of the incoming data, 250+ times? Report back. If there are still problems, please show the original code and exception report... -- Regards =dn From Gronicus at SGA.Ninja Tue Feb 12 15:37:12 2019 From: Gronicus at SGA.Ninja (Steve) Date: Tue, 12 Feb 2019 15:37:12 -0500 Subject: I cannot seem to write time/date to the file. In-Reply-To: References: <000701d4c307$ec9e9430$c5dbbc90$@SGA.Ninja> Message-ID: <002701d4c312$bc674860$3535d920$@SGA.Ninja> That was it, will wonders never cease? Thanks Footnote: Ultrasound Technician Asks Pregnant Woman If She?d Like To Know Baby?s Name -----Original Message----- From: Python-list On Behalf Of David Raymond Sent: Tuesday, February 12, 2019 2:28 PM To: python-list at python.org Subject: RE: I cannot seem to write time/date to the file. DateReading.write = (nowTimeDate2 + "\n") You're re-assigning the write method to be a string here. You want to call the method with the string as the argument. DateReading.write(nowTimeDate2 + "\n") -----Original Message----- From: Python-list [mailto:python-list-bounces+david.raymond=tomtom.com at python.org] On Behalf Of Steve Sent: Tuesday, February 12, 2019 2:20 PM To: python-list at python.org Subject: I cannot seem to write time/date to the file. I am using nowTimeDate2 elsewhere in my program and it can be printed to the screen. The file exists and the program successfully opens the file, writes to it, and closes the file. All is working as designed. However, Now I want to place an entry into the first line of the file to note the time/date the file was created. It doesn?t show up in the file. How do I fix this? from time import gmtime, strftime nowTimeDate2=strftime("%Y %a %b %d %H:%M") print(nowTimeDate2) #This works DateReading=open("Date-ReadingsAndDoses.txt", "w") DateReading.write = (nowTimeDate2 + "\n") DateReading.close() I also tried to append but that did not work either. Interestingly enough, this is a feature that really has no real purpose in the final product yet it is hounding me. Footnote: Ultrasound Technician Asks Pregnant Woman If She?d Like To Know Baby?s Name -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list From __peter__ at web.de Wed Feb 13 03:10:09 2019 From: __peter__ at web.de (Peter Otten) Date: Wed, 13 Feb 2019 09:10:09 +0100 Subject: A limit to writing to a file from a loop? References: <000001d4c304$a8fac800$faf05800$@SGA.Ninja> Message-ID: Steve wrote: > My program reads from a text file (A), modifies the data, and writes to > another file (B). This works until I reach about 300 writes and no more > lines are written to file (B). > > I had to create a Counter and increment it to 250 when it gets reset. > > Upon reset, I close the file (B) being written and reopen it for append. > > Then it accepts the entire list of lines of data. > > > > Bizarre? Maybe a misdiagnosis. If you are reading the file contents while it is still open some output may reside in a buffer that is invisible to the reading file object. > CycleCounter += 1 > > if CycleCounter > 250: > > CycleCounter = 1 Try replacing the following two lines > DateReadings.close() > DateReadings=open("Date-ReadingsAndDoses.txt", "a") with DateReadings.flush() If that has the same effect as the close()/open() dance remove the flush(), too, and ensure that the file is closed before you check its contents. The with statement is the established way to achieve that. Instead of file = open(...) do_stuff_with(file) file.close() # not called if there is an exception write with open(...) as file: do_stuff_with(file) # at this point the file is guaranteed to be closed. > > DateReadings.write("{0:15} {1:>8} {2:>8} {3:>8} {4:<2} > {5:>8} {6:>8} {7:>10}".format > > (ThisTimeDate, ThisReading, ThisDose1, > ThisSensor, ThisTrend, > > ThisTS, ThisPercent, SensorNumberDay2) + > "\n") From none at gmail.com Wed Feb 13 08:21:01 2019 From: none at gmail.com (ast) Date: Wed, 13 Feb 2019 14:21:01 +0100 Subject: Why float('Nan') == float('Nan') is False Message-ID: <5c6419bf$0$6565$426a74cc@news.free.fr> Hello >>> float('Nan') == float('Nan') False Why ? Regards From 2QdxY4RzWzUUiLuE at potatochowder.com Wed Feb 13 08:34:29 2019 From: 2QdxY4RzWzUUiLuE at potatochowder.com (Dan Sommers) Date: Wed, 13 Feb 2019 07:34:29 -0600 Subject: Why float('Nan') == float('Nan') is False In-Reply-To: <5c6419bf$0$6565$426a74cc@news.free.fr> References: <5c6419bf$0$6565$426a74cc@news.free.fr> Message-ID: <425b3491-24f8-2f96-80a4-45be442defb9@potatochowder.com> On 2/13/19 7:21 AM, ast wrote: > Hello > > >>> float('Nan') == float('Nan') > False > > Why ? Because the IEEE-754 Standard demands it, and people smarter than I worked on the IEEE-754 Standard. is a quick starting point for a deeper dive. From grant.b.edwards at gmail.com Wed Feb 13 13:03:25 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Wed, 13 Feb 2019 18:03:25 -0000 (UTC) Subject: Why float('Nan') == float('Nan') is False References: <5c6419bf$0$6565$426a74cc@news.free.fr> Message-ID: On 2019-02-13, ast wrote: > Hello > > >>> float('Nan') == float('Nan') > False If you think that's odd, how about this? >>> n = float('nan') >>> n nan >>> n is n True >>> n == n False >>> > Why ? IEEE says so. -- Grant Edwards grant.b.edwards Yow! Like I always say at -- nothing can beat gmail.com the BRATWURST here in DUSSELDORF!! From avigross at verizon.net Wed Feb 13 14:00:57 2019 From: avigross at verizon.net (Avi Gross) Date: Wed, 13 Feb 2019 14:00:57 -0500 Subject: Why float('Nan') == float('Nan') is False In-Reply-To: References: <5c6419bf$0$6565$426a74cc@news.free.fr> Message-ID: <005e01d4c3ce$7403c370$5c0b4a50$@verizon.net> I won't speak for the IEEE but NOT A NUMBER does not tell you what something IS. If "Hello, World!" is not a number as in an int or a float and we throw away the content and simply call it a NaN or something and then we notice that an object that is a list of fruits is also not a number so we call it a NaN too, then should they be equal? A NaN is a bit like a black hole. Anything thrown in disappears and that is about all we know about it. No two black holes are the same even if they seem to have the same mass, spin and charge. All they share is that we don't know what is in them. When variable "a" is a Nan then it is sort of a pointer to a concept. The pointer IS itself but the concepts may not be. -----Original Message----- From: Python-list On Behalf Of Grant Edwards Sent: Wednesday, February 13, 2019 1:03 PM To: python-list at python.org Subject: Re: Why float('Nan') == float('Nan') is False On 2019-02-13, ast wrote: > Hello > > >>> float('Nan') == float('Nan') > False If you think that's odd, how about this? >>> n = float('nan') >>> n nan >>> n is n True >>> n == n False >>> > Why ? IEEE says so. -- Grant Edwards grant.b.edwards Yow! Like I always say at -- nothing can beat gmail.com the BRATWURST here in DUSSELDORF!! -- https://mail.python.org/mailman/listinfo/python-list From pfeiffer at cs.nmsu.edu Wed Feb 13 14:33:31 2019 From: pfeiffer at cs.nmsu.edu (Joe Pfeiffer) Date: Wed, 13 Feb 2019 12:33:31 -0700 Subject: Why float('Nan') == float('Nan') is False References: <5c6419bf$0$6565$426a74cc@news.free.fr> Message-ID: <1b1s4bcwl0.fsf@pfeifferfamily.net> ast writes: > Hello > >>>> float('Nan') == float('Nan') > False > > Why ? > > Regards Others have given the real answer -- IEEE says so, and the people who wrote the standard are smarter than me. All the same, this is my take on the reason for it: NaN is specifically a representation for "this has no value". The == operator compares the values of its operands; something that has no value can't == anything, including itself. From Joseph.Schachner at Teledyne.com Wed Feb 13 14:36:34 2019 From: Joseph.Schachner at Teledyne.com (Schachner, Joseph) Date: Wed, 13 Feb 2019 19:36:34 +0000 Subject: Why float('Nan') == float('Nan') is False In-Reply-To: <5c6419bf$0$6565$426a74cc@news.free.fr> References: <5c6419bf$0$6565$426a74cc@news.free.fr> Message-ID: <79378e7b67564f1d968290b3fd2f3f4d@Teledyne.com> Because all comparisons with NAN return false, that's the spec. is NAN > 0? False. Is NAN< 0? False. Is NAN == 0? False. Is NAN == ? False. So: Is NAN == NAN? False. And one more: Is NAN < 1.0e18? False This makes some sense because NAN is Not A Number, so any comparison to a number fails. --- Joseph S. -----Original Message----- From: ast Sent: Wednesday, February 13, 2019 8:21 AM To: python-list at python.org Subject: Why float('Nan') == float('Nan') is False Hello >>> float('Nan') == float('Nan') False Why ? Regards From grant.b.edwards at gmail.com Wed Feb 13 14:53:43 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Wed, 13 Feb 2019 19:53:43 -0000 (UTC) Subject: Why float('Nan') == float('Nan') is False References: <5c6419bf$0$6565$426a74cc@news.free.fr> <79378e7b67564f1d968290b3fd2f3f4d@Teledyne.com> Message-ID: On 2019-02-13, Schachner, Joseph wrote: > This makes some sense because NAN is Not A Number, so any comparison > to a number fails. Ah, but you now seem to be conflating "comparison fails" with "comparison has a boolean value of False". The alternative to (nan == nan) => False is probably not (nan == nan) => True: it's (nan == nan) => exception. Towards that end, the IEEE standard provides for something called a "signalling NaN". I'm not aware of any Python implementations that support signalling NaNs. Floating point is sort of the quantum mechanics of computer science. At first glance, it seems sort of weird. But after you work with it a while, it gets even worse. -- Grant Edwards grant.b.edwards Yow! Jesuit priests are at DATING CAREER DIPLOMATS!! gmail.com From rosuav at gmail.com Wed Feb 13 14:57:36 2019 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 14 Feb 2019 06:57:36 +1100 Subject: Why float('Nan') == float('Nan') is False In-Reply-To: <79378e7b67564f1d968290b3fd2f3f4d@Teledyne.com> References: <5c6419bf$0$6565$426a74cc@news.free.fr> <79378e7b67564f1d968290b3fd2f3f4d@Teledyne.com> Message-ID: On Thu, Feb 14, 2019 at 6:40 AM Schachner, Joseph wrote: > > Because all comparisons with NAN return false, that's the spec. Apart from !=, because it would be insane (I mean, even more insane than it is) to have nan == nan be false AND nan != nan. IEEE NAN has several purposes, including representing the concept of "could be any number but we have no idea what", and also the concept of "there is truly no value here". And sometimes both at once - imagine getting per-capita statistics partitioned by age group and postal code, and having nobody in a particular age/postcode, so you end up with both "there is no value here" and "what do you get when you divide zero by zero" (zero whatevers divided by zero people). So there ARE some operations involving nan that produce real results: >>> 1 ** nan 1.0 but as a general rule, you have to assume that nan truly isn't a number. Oh, and if you want TRULY mind-melty fun, look into the SQL "NULL" value, which is a value, except when it isn't. Comparisons with NULL don't return false, they return NULL. Most of the time, if you take a column function over a nullable column (say, taking the sum or average of the values in a field), you just ignore any rows that are NULL; but if *every* value is NULL, the sum is not 0, but NULL. Now try mapping SQL's NULL to Python's float("nan"), and performing operations on both sides. Endless fun. ChrisA From rosuav at gmail.com Wed Feb 13 14:58:52 2019 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 14 Feb 2019 06:58:52 +1100 Subject: Why float('Nan') == float('Nan') is False In-Reply-To: References: <5c6419bf$0$6565$426a74cc@news.free.fr> <79378e7b67564f1d968290b3fd2f3f4d@Teledyne.com> Message-ID: On Thu, Feb 14, 2019 at 6:55 AM Grant Edwards wrote: > > On 2019-02-13, Schachner, Joseph wrote: > > > This makes some sense because NAN is Not A Number, so any comparison > > to a number fails. > > Ah, but you now seem to be conflating "comparison fails" with > "comparison has a boolean value of False". > > The alternative to (nan == nan) => False is probably not (nan == nan) > => True: it's (nan == nan) => exception. Or (nan == nan) => nan, which is what "infections nan" would be like. (Compare NULL.) ChrisA From 2QdxY4RzWzUUiLuE at potatochowder.com Wed Feb 13 15:05:05 2019 From: 2QdxY4RzWzUUiLuE at potatochowder.com (Dan Sommers) Date: Wed, 13 Feb 2019 14:05:05 -0600 Subject: Why float('Nan') == float('Nan') is False In-Reply-To: References: <5c6419bf$0$6565$426a74cc@news.free.fr> <79378e7b67564f1d968290b3fd2f3f4d@Teledyne.com> Message-ID: <0d47ee2d-84ae-a7b6-2939-07350d32e559@potatochowder.com> On 2/13/19 1:53 PM, Grant Edwards wrote: > Floating point is sort of the quantum mechanics of computer science. > At first glance, it seems sort of weird. But after you work with it a > while, it gets even worse. Yep! :-) From onlinejudge95 at gmail.com Wed Feb 13 14:27:40 2019 From: onlinejudge95 at gmail.com (Test Bot) Date: Thu, 14 Feb 2019 00:57:40 +0530 Subject: Why float('Nan') == float('Nan') is False In-Reply-To: <005e01d4c3ce$7403c370$5c0b4a50$@verizon.net> References: <5c6419bf$0$6565$426a74cc@news.free.fr> <005e01d4c3ce$7403c370$5c0b4a50$@verizon.net> Message-ID: This definition of NaN is much better in mentally visualizing all the so called bizarreness of IEEE. This also makes intuitive that no 2 NaN will be equal just as no 2 infinities would be equal. I believe in a hypothesis(of my own creation) that any arithmetic on a data type of NaN would be similar to any set of operations on the set of Infinities. On Thu, Feb 14, 2019, 12:33 AM Avi Gross I won't speak for the IEEE but NOT A NUMBER does not tell you what > something > IS. > > If "Hello, World!" is not a number as in an int or a float and we throw > away > the content and simply call it a NaN or something and then we notice that > an > object that is a list of fruits is also not a number so we call it a NaN > too, then should they be equal? > > A NaN is a bit like a black hole. Anything thrown in disappears and that is > about all we know about it. No two black holes are the same even if they > seem to have the same mass, spin and charge. All they share is that we > don't > know what is in them. > > When variable "a" is a Nan then it is sort of a pointer to a concept. The > pointer IS itself but the concepts may not be. > > -----Original Message----- > From: Python-list On > Behalf Of Grant Edwards > Sent: Wednesday, February 13, 2019 1:03 PM > To: python-list at python.org > Subject: Re: Why float('Nan') == float('Nan') is False > > On 2019-02-13, ast wrote: > > Hello > > > > >>> float('Nan') == float('Nan') > > False > > If you think that's odd, how about this? > > >>> n = float('nan') > >>> n > nan > >>> n is n > True > >>> n == n > False > >>> > > > Why ? > > IEEE says so. > > -- > Grant Edwards grant.b.edwards Yow! Like I always say > at -- nothing can beat > gmail.com the BRATWURST here in > DUSSELDORF!! > > -- > https://mail.python.org/mailman/listinfo/python-list > > -- > https://mail.python.org/mailman/listinfo/python-list > From rosuav at gmail.com Wed Feb 13 15:19:00 2019 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 14 Feb 2019 07:19:00 +1100 Subject: Why float('Nan') == float('Nan') is False In-Reply-To: References: <5c6419bf$0$6565$426a74cc@news.free.fr> <005e01d4c3ce$7403c370$5c0b4a50$@verizon.net> Message-ID: On Thu, Feb 14, 2019 at 7:12 AM Test Bot wrote: > > This definition of NaN is much better in mentally visualizing all the so > called bizarreness of IEEE. This also makes intuitive that no 2 NaN will be > equal just as no 2 infinities would be equal. I believe in a hypothesis(of > my own creation) that any arithmetic on a data type of NaN would be similar > to any set of operations on the set of Infinities. > Why would no two infinities be equal? In mathematics, there's one best-known infinity (aleph null, aka the number of counting numbers), and many many infinities are provably equal to it. (Others are provably NOT equal to it, like the number of real numbers.) There's nothing wrong with saying "there are as many prime numbers as there are odd numbers", or equivalently that "the size/cardinality of the set of primes is equal to the size of the set of odd numbers" [1]. And both Python and IEEE agree: >>> float("inf") == float("inf") True NaN and infinity are quite different concepts, and they behave very differently. ChrisA [1] I'm sure someone will point out something pedantically wrong in what I said, but certainly the sets have the same cardinality. From marko at pacujo.net Wed Feb 13 15:32:18 2019 From: marko at pacujo.net (Marko Rauhamaa) Date: Wed, 13 Feb 2019 22:32:18 +0200 Subject: Why float('Nan') == float('Nan') is False References: <5c6419bf$0$6565$426a74cc@news.free.fr> <005e01d4c3ce$7403c370$5c0b4a50$@verizon.net> Message-ID: <878syjpgz1.fsf@elektro.pacujo.net> "Avi Gross" : > A NaN is a bit like a black hole. Anything thrown in disappears and > that is about all we know about it. No two black holes are the same > even if they seem to have the same mass, spin and charge. All they > share is that we don't know what is in them. Then, how do you explain: >>> float("nan") != float("nan") True Why's that not False? Marko From rgaddi at highlandtechnology.invalid Wed Feb 13 16:07:03 2019 From: rgaddi at highlandtechnology.invalid (Rob Gaddi) Date: Wed, 13 Feb 2019 13:07:03 -0800 Subject: Why float('Nan') == float('Nan') is False In-Reply-To: <878syjpgz1.fsf@elektro.pacujo.net> References: <5c6419bf$0$6565$426a74cc@news.free.fr> <005e01d4c3ce$7403c370$5c0b4a50$@verizon.net> <878syjpgz1.fsf@elektro.pacujo.net> Message-ID: On 2/13/19 12:32 PM, Marko Rauhamaa wrote: > "Avi Gross" : > >> A NaN is a bit like a black hole. Anything thrown in disappears and >> that is about all we know about it. No two black holes are the same >> even if they seem to have the same mass, spin and charge. All they >> share is that we don't know what is in them. > > Then, how do you explain: > > >>> float("nan") != float("nan") > True > > Why's that not False? > > > Marko > Because IEEE-754 decided that it was non-optional that (x != y) was equal to not (x == y). Which is not the case for the ordering operators, since ordering is inherently undefined. In part, these decisions were made to make it possible to detect a NaN in C in the absence of an isnan() function. If (x != x), then x must be a NaN. -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix. From travisgriggs at gmail.com Wed Feb 13 16:20:23 2019 From: travisgriggs at gmail.com (Travis Griggs) Date: Wed, 13 Feb 2019 13:20:23 -0800 Subject: How do/can I generate a PKCS#12 file the cryptography module? Message-ID: <88B461DB-DDF6-435E-BA03-30D30BEB1299@gmail.com> I?m using the cryptography module (https://cryptography.io/en/latest/) to try and generate some cert/key/identities. It's pretty easy using said module to generate the contents of .pem file for a private key: keyPEMBytes = privateKey.private_bytes( encoding=serialization.Encoding.PEM, format=serialization.PrivateFormat.TraditionalOpenSSL, encryption_algorithm=serialization.NoEncryption()) It?s also easy to generate the contents of a .cer/.pem file for an associated cert: certBytes = certificate.public_bytes(encoding=serialization.Encoding.PEM) But I need them (and their chain) balled up on a single .p12 (PKCS12) file. Said module documents how to parse/consume PKCS12 formats, but nothing (that I can find) about how one can generate them. My understanding of PKI stuff is hit and miss though, so maybe I'm just not searching the right keyword in the documentation? I can create the .p12 file at the command line on Linux using openssl pkcs12 -export -out myIdentity.p12 -inkey myPrivKey.pem -in myCert.crt -certfile myCertChain.crt So I could just wrap calls like this with subprocess/cmd and mess with tempfiles/pipes. I was hoping to keep it all in memory/python though. Is there a different python TLS library that I should be considering, that can do this? (stack overflow version if you?re into the points and all that: https://stackoverflow.com/questions/54677841/how-do-can-i-generate-a-pkcs12-file-using-python-and-the-cryptography-module) From uri at speedy.net Wed Feb 13 16:27:47 2019 From: uri at speedy.net (=?UTF-8?B?15DXldeo15k=?=) Date: Wed, 13 Feb 2019 23:27:47 +0200 Subject: Why float('Nan') == float('Nan') is False In-Reply-To: References: <5c6419bf$0$6565$426a74cc@news.free.fr> <005e01d4c3ce$7403c370$5c0b4a50$@verizon.net> Message-ID: ???? uri at speedy.net On Wed, Feb 13, 2019 at 10:20 PM Chris Angelico wrote: > On Thu, Feb 14, 2019 at 7:12 AM Test Bot wrote: > > > > This definition of NaN is much better in mentally visualizing all the so > > called bizarreness of IEEE. This also makes intuitive that no 2 NaN will > be > > equal just as no 2 infinities would be equal. I believe in a > hypothesis(of > > my own creation) that any arithmetic on a data type of NaN would be > similar > > to any set of operations on the set of Infinities. > > > > Why would no two infinities be equal? In mathematics, there's one > best-known infinity (aleph null, aka the number of counting numbers), > and many many infinities are provably equal to it. (Others are > provably NOT equal to it, like the number of real numbers.) There's > nothing wrong with saying "there are as many prime numbers as there > are odd numbers", or equivalently that "the size/cardinality of the > set of primes is equal to the size of the set of odd numbers" [1]. And > both Python and IEEE agree: > There are more integers than odd numbers, and more odd numbers than prime numbers. An infinite set may be a subset of another infinite set although they may both have the same cardinality. Or in other words, the number of elements in each set is not equal. One has more elements than the other. AND, by induction you can also prove that the other one has more elements than the first one. So the number of elements in two infinite sets can't be equal. Even, if you compare the same set to itself. > > >>> float("inf") == float("inf") > True > > NaN and infinity are quite different concepts, and they behave very > differently. > > ChrisA > > [1] I'm sure someone will point out something pedantically wrong in > what I said, but certainly the sets have the same cardinality. > -- > https://mail.python.org/mailman/listinfo/python-list > From rosuav at gmail.com Wed Feb 13 16:33:44 2019 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 14 Feb 2019 08:33:44 +1100 Subject: Why float('Nan') == float('Nan') is False In-Reply-To: References: <5c6419bf$0$6565$426a74cc@news.free.fr> <005e01d4c3ce$7403c370$5c0b4a50$@verizon.net> Message-ID: ?On Thu, Feb 14, 2019 at 8:24 AM ??????? wrote:? > On Wed, Feb 13, 2019 at 10:20 PM Chris Angelico wrote: >> >> Why would no two infinities be equal? In mathematics, there's one >> best-known infinity (aleph null, aka the number of counting numbers), >> and many many infinities are provably equal to it. (Others are >> provably NOT equal to it, like the number of real numbers.) There's >> nothing wrong with saying "there are as many prime numbers as there >> are odd numbers", or equivalently that "the size/cardinality of the >> set of primes is equal to the size of the set of odd numbers" [1]. And >> both Python and IEEE agree: > > > There are more integers than odd numbers, and more odd numbers than prime numbers. An infinite set may be a subset of another infinite set although they may both have the same cardinality. Or in other words, the number of elements in each set is not equal. One has more elements than the other. AND, by induction you can also prove that the other one has more elements than the first one. So the number of elements in two infinite sets can't be equal. Even, if you compare the same set to itself. > You can enumerate the odd numbers easily. The first odd number is 1, the second odd number is 3, the third is 5, the fourth is 7, etc, etc. Or if you want to include negative odd numbers, the first is 1, the second is -1, the third is 3, the fourth is -3, the fifth is 5, the sixth is -5, etc. Thus there is a clear and easy bijection between odd numbers and counting numbers. You aren't going to run out of counting numbers before you run out of odd numbers or vice versa. The same is true of prime numbers. The first prime number is 2, the second is 3, the third is 5, the fourth 7, the fifth 11, the sixth 13. You can never "run out" of prime numbers [1], so you can enumerate them all. Thus there's the same bijection between prime numbers and counting numbers, and thus there are equally many. Therefore, since you can take any odd number and find its position in the list, and then find the same position in the list of primes, it would be possible (albeit computationally impractical) to find the corresponding prime number for any odd number. There are just as many prime numbers as odd numbers. ChrisA [1] Proof by contradiction: if there were finitely many primes, you could multiply them all together and add one. The result is greater than any number on your list, and can't be a multiple of any of the primes, ergo it's either prime, or a product of primes not on your list. QED. From avigross at verizon.net Wed Feb 13 17:07:20 2019 From: avigross at verizon.net (Avi Gross) Date: Wed, 13 Feb 2019 22:07:20 +0000 (UTC) Subject: Why float('Nan') == float('Nan') is False References: <374021259.575838.1550095640185.ref@mail.yahoo.com> Message-ID: <374021259.575838.1550095640185@mail.yahoo.com> I think we should realize that Nan and NA and so on are human constructs people Define in programming languages. Some have subdivisions as in not an int as compared to not a float. Python also has an Inf as well as a -Inf that are abstractions and not a real, so to speak. Number. Mathematics is arguably a human discovery when it comes to infinities since Cantor but the rules are not the same. CARDINALITY is not a number. It is a logical range of partitions. There obviously are more integers than prime numbers as you count upwards to a thousand or a billion. BUT in the context of infinity, at the Aleph Null level, they sort of map into the same category. So the primes or squares or odd numbers are not equal but in the limit as you approach whatever infinity means, they seem effectively equal as there is always another.? You can sort of make a 1:1 correspondence or mapping. But that means that normal mathematics is warped. Double it and/or add a quadrillion and it remains in the same bucket.? But there are other buckets and everything in them is not so much the same as in some sense larger than the previous bucket but smaller than the next higher bucket. The continuum hypothesis remains open as to whether there is an intermediate bucket you can describe between Aleph Null and Aleph one. Meaning there is no reasonable definition of a bucket the new infinite construct fits in while not fitting existing ones. Back to python. Or is it IEEE? In type int the number can be arbitrarily large as long as you have enough computer memory and resources. Yet the special concepts of Inf and negative Inf can be compared with other ints but not fruitfully with Nan, right? There are helper functions you can call to ask if a variable is an Inf or a Nan. Doubles have size limits so anything big enough might as well be an Inf. I note some things like numpy use a fixed size int as well. Some of the confusion may come when a language uses a concept in many ways. In R all an NA can mean is Not Available. Some data structures, just like a numpy array in python, insist on holding objects of one unified type and NA normally matches any such type as a placeholder. Sort of a NULL. You normally do not want to include an NA in calculations as it infects everything. So you often filter it out implicitly or explicitly. It is not necessarily an error but if you use it in calculating a mean, then the result is sort of an error. You can ask if a variable is an NA as in is.na(x) which returns a Boolean. So to test if x and y are both NA or have the same status by both not being NA, use the functions. Python has similar functionality. Just don't compare the naked singularities to each other! Sent from AOL Mobile Mail On Wednesday, February 13, 2019 Chris Angelico wrote: On Thu, Feb 14, 2019 at 7:12 AM Test Bot wrote: > > This definition of NaN is much better in mentally visualizing all the so > called bizarreness of IEEE. This also makes intuitive that no 2 NaN will be > equal just as no 2 infinities would be equal. I believe in a hypothesis(of > my own creation) that any arithmetic on a data type of NaN would be similar > to any set of operations on the set of Infinities. > Why would no two infinities be equal? In mathematics, there's one best-known infinity (aleph null, aka the number of counting numbers), and many many infinities are provably equal to it. (Others are provably NOT equal to it, like the number of real numbers.) There's nothing wrong with saying "there are as many prime numbers as there are odd numbers", or equivalently that "the size/cardinality of the set of primes is equal to the size of the set of odd numbers" [1]. And both Python and IEEE agree: >>> float("inf") == float("inf") True NaN and infinity are quite different concepts, and they behave very differently. ChrisA [1] I'm sure someone will point out something pedantically wrong in what I said, but certainly the sets have the same cardinality. -- https://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Wed Feb 13 17:10:02 2019 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 14 Feb 2019 09:10:02 +1100 Subject: Why float('Nan') == float('Nan') is False In-Reply-To: <374021259.575838.1550095640185@mail.yahoo.com> References: <374021259.575838.1550095640185.ref@mail.yahoo.com> <374021259.575838.1550095640185@mail.yahoo.com> Message-ID: On Thu, Feb 14, 2019 at 9:07 AM Avi Gross wrote: > But that means that normal mathematics is warped. Well.... yes. Yes, it is. That's why people think "Alice's Adventures in Wonderland" is the result of a drug-induced dream - in actual fact, it's the result of the Dean of Mathematics telling stories to young children. Of course mathematics is warped. ChrisA From grant.b.edwards at gmail.com Wed Feb 13 18:06:33 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Wed, 13 Feb 2019 23:06:33 -0000 (UTC) Subject: What's up with Activestate Python? Message-ID: For many, many years I've always installed ActiveState's ActivePython Community edition when forced to use Windows. It has always included all of the "extra" libraries that I didn't wan't to install (or couldn't because I didn't have a C compiler for Windows). I recently decided to upgrade my Win7 machine from ActivePython 3.5.4 to 3.6. ... and all of apps stopped working. ActivePython 3.6 appears to be a minimal install that includes nothing but CPython. Comparing the download sizes makes this obvious: -rw-r--r-- 1 grante users 223056832 Mar 26 2018 ActivePython-2.7.14.2717-win64-x64-404905.exe -rw-r--r-- 1 grante users 225065576 May 29 2018 ActivePython-3.5.4.3504-win64-x64-404899.exe -rw-r--r-- 1 grante users 30297136 Feb 13 16:28 ActivePython-3.6.0.3600-win64-x64-401834.exe I've searched the ActiveState web site, and the fact that they've stopped including "extra" libraries doesn't seem to be documented anywhere. I guess it's time to switch to Anaconda or ??? -- Grant Edwards grant.b.edwards Yow! Is this going to at involve RAW human ecstasy? gmail.com From songbird at anthive.com Wed Feb 13 16:40:21 2019 From: songbird at anthive.com (songbird) Date: Wed, 13 Feb 2019 16:40:21 -0500 Subject: Why float('Nan') == float('Nan') is False References: <5c6419bf$0$6565$426a74cc@news.free.fr> <005e01d4c3ce$7403c370$5c0b4a50$@verizon.net> Message-ID: <5ffejf-qq7.ln1@anthive.com> Chris Angelico wrote: > On Thu, Feb 14, 2019 at 7:12 AM Test Bot wrote: >> >> This definition of NaN is much better in mentally visualizing all the so >> called bizarreness of IEEE. This also makes intuitive that no 2 NaN will be >> equal just as no 2 infinities would be equal. I believe in a hypothesis(of >> my own creation) that any arithmetic on a data type of NaN would be similar >> to any set of operations on the set of Infinities. >> > > Why would no two infinities be equal? In mathematics, there's one > best-known infinity (aleph null, aka the number of counting numbers), > and many many infinities are provably equal to it. (Others are > provably NOT equal to it, like the number of real numbers.) There's > nothing wrong with saying "there are as many prime numbers as there > are odd numbers", or equivalently that "the size/cardinality of the > set of primes is equal to the size of the set of odd numbers" [1]. And > both Python and IEEE agree: ... > [1] I'm sure someone will point out something pedantically wrong in > what I said, but certainly the sets have the same cardinality. all such proofs i have ever seen are based upon the assumptions that there are infinite numbers of such things like primes. this only makes sense in theory. alas, we don't really know if the universe is infinitely subdivisible (as the reals seem to represent) or infinitely large (even if it isn't infinitely subdivisible)... so to me every one of those proofs is conditional upon assumptions (which also drags the p = np question into such assumptions). it's fun to think about. :) songbird From python at bdurham.com Wed Feb 13 19:01:52 2019 From: python at bdurham.com (Malcolm Greene) Date: Wed, 13 Feb 2019 17:01:52 -0700 Subject: Best way to remove unused pip installed modules/module dependencies from a virtual env? Message-ID: <1550102512.1582315.1657573184.2E9E0C03@webmail.messagingengine.com> Looking for advice on the best way to remove unused modules from a Python virtual environment. My setup is Python 3.6.6 running on macOS although I believe my use case is OS independent. Background: Long running project that, over the course of time, pip installed modules that are no longer used by the code. I'm looking for a way to identity unused modules and remove them. Here's my back-of-napkin strategy to do this. Wondering if there are holes in this approach or if there's an off-the-shelf solution for my use case? 1. pip freeze > modules.txt 2. build a list of all import statements, extract out module names 3. remove these module names from modules.txt and add to used- modules.txt4. modules that remain in modules.txt are either module dependencies of directly imported modules or no longer used5. remove my virtual env and recreate it again to start with a fresh env6. reinstall each directly imported module (from list in used- modules.txt); this will pull in dependencies again7. pip freeze > requirements.txt <--- this should be the exact modules used by our code Thank you, Malcolm From python at bdurham.com Wed Feb 13 19:27:35 2019 From: python at bdurham.com (Malcolm Greene) Date: Wed, 13 Feb 2019 17:27:35 -0700 Subject: Best way to remove unused pip installed modules/module dependencies from a virtual env? In-Reply-To: <1550102512.1582315.1657573184.2E9E0C03@webmail.messagingengine.com> References: <1550102512.1582315.1657573184.2E9E0C03@webmail.messagingengine.com> Message-ID: <1550104055.1592329.1657590992.1C5E67E0@webmail.messagingengine.com> [Reformatted as original post got mangled] Looking for advice on the best way to remove unused modules from a Python virtual environment. My setup is Python 3.6.6 running on macOS although I believe my use case is OS independent. Background: Long running project that, over the course of time, pip installed modules that are no longer used by the code. I'm looking for a way to identity unused modules and remove them. Here's my back-of-napkin strategy to do this. Wondering if there are holes in this approach or if there's an off-the-shelf solution for my use case? 1. pip freeze > modules.txt 2. build a list of all import statements, extract out module names 3. remove these module names from modules.txt and add to used-modules.txt 4. modules that remain in modules.txt are either module dependencies of directly imported modules or no longer used 5. remove my virtual env and recreate it again to start with a fresh env 6. reinstall each directly imported module (from list in used-modules.txt); this will pull in dependencies again 7. pip freeze > requirements.txt <--- this should be the exact modules used by our code Thank you, Malcolm From rosuav at gmail.com Wed Feb 13 19:29:12 2019 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 14 Feb 2019 11:29:12 +1100 Subject: Why float('Nan') == float('Nan') is False In-Reply-To: <5ffejf-qq7.ln1@anthive.com> References: <5c6419bf$0$6565$426a74cc@news.free.fr> <005e01d4c3ce$7403c370$5c0b4a50$@verizon.net> <5ffejf-qq7.ln1@anthive.com> Message-ID: On Thu, Feb 14, 2019 at 11:01 AM songbird wrote: > all such proofs i have ever seen are based upon the > assumptions that there are infinite numbers of such > things like primes. I posted an abbreviated proof of that in a footnote. It's a proof by contradiction. First, assume that there are, in fact, a finite number of primes. If that's the case, then all primes must be integers between 2 and some number p, the highest prime. Take the product of all primes - call it x. When you take the product of positive integers, the result must always be at least as large as any of the factors, so x >= p. Also, x must be a multiple of every prime, which in turn means that x+1 cannot possibly be a multiple of any such prime. Thus the value x+1 must either be prime, or be the product of prime numbers that aren't in your collection of primes; therefore the collection of primes cannot possibly be complete. Therefore there are indeed an infinite number of primes. So it's not an assumption; it's a proven point. The subdividability of the universe is actually irrelevant. Perhaps the universe, at some level, becomes indivisible; but numbers don't. For any two non-equal real numbers, it is always possible to find another number in between them. (This is NOT true of floating-point numbers or any other fixed-size representation.) Numbers are actually extremely convenient like that. This is largely off-topic for Python, but do consider: thanks to bignum integers and the Fraction type, we can represent any rational number, assuming we have enough storage space. Or do we..... ChrisA From pfeiffer at cs.nmsu.edu Wed Feb 13 20:11:43 2019 From: pfeiffer at cs.nmsu.edu (Joe Pfeiffer) Date: Wed, 13 Feb 2019 18:11:43 -0700 Subject: Why float('Nan') == float('Nan') is False References: <5c6419bf$0$6565$426a74cc@news.free.fr> <005e01d4c3ce$7403c370$5c0b4a50$@verizon.net> <5ffejf-qq7.ln1@anthive.com> Message-ID: <1bef8byy0g.fsf@pfeifferfamily.net> songbird writes: > Chris Angelico wrote: >> On Thu, Feb 14, 2019 at 7:12 AM Test Bot wrote: >>> >>> This definition of NaN is much better in mentally visualizing all the so >>> called bizarreness of IEEE. This also makes intuitive that no 2 NaN will be >>> equal just as no 2 infinities would be equal. I believe in a hypothesis(of >>> my own creation) that any arithmetic on a data type of NaN would be similar >>> to any set of operations on the set of Infinities. >>> >> >> Why would no two infinities be equal? In mathematics, there's one >> best-known infinity (aleph null, aka the number of counting numbers), >> and many many infinities are provably equal to it. (Others are >> provably NOT equal to it, like the number of real numbers.) There's >> nothing wrong with saying "there are as many prime numbers as there >> are odd numbers", or equivalently that "the size/cardinality of the >> set of primes is equal to the size of the set of odd numbers" [1]. And >> both Python and IEEE agree: > ... >> [1] I'm sure someone will point out something pedantically wrong in >> what I said, but certainly the sets have the same cardinality. > > all such proofs i have ever seen are based upon the > assumptions that there are infinite numbers of such > things like primes. > > this only makes sense in theory. > > alas, we don't really know if the universe is infinitely > subdivisible (as the reals seem to represent) or infinitely > large (even if it isn't infinitely subdivisible)... so to > me every one of those proofs is conditional upon assumptions > (which also drags the p = np question into such assumptions). > > it's fun to think about. :) It doesn't depend upon assumptions, it depends on definitions and logic. You don't need to assume there are an infinite number of primes, it's been proven. It doesn't matter whether the universe is infinitely subdivisible or infinitely large, the set of real number is (assuming I'm interpreting your "subdivisible" correctly). I've got no idea what P and NP have to do with this. From pfeiffer at cs.nmsu.edu Wed Feb 13 20:20:26 2019 From: pfeiffer at cs.nmsu.edu (Joe Pfeiffer) Date: Wed, 13 Feb 2019 18:20:26 -0700 Subject: Why float('Nan') == float('Nan') is False References: <5c6419bf$0$6565$426a74cc@news.free.fr> <005e01d4c3ce$7403c370$5c0b4a50$@verizon.net> Message-ID: <1b4l97yxlx.fsf@pfeifferfamily.net> uri at speedy.net writes: > There are more integers than odd numbers, and more odd numbers than prime > numbers. An infinite set may be a subset of another infinite set although > they may both have the same cardinality. Or in other words, the number of > elements in each set is not equal. One has more elements than the other. > AND, by induction you can also prove that the other one has more elements > than the first one. So the number of elements in two infinite sets can't be > equal. Even, if you compare the same set to itself. You would expect that to be true, but it is not. There are in fact the same number of odd integers as integers, and the same number of primes as integers. Counterintuitive but true. From none at gmail.com Thu Feb 14 02:00:15 2019 From: none at gmail.com (ast) Date: Thu, 14 Feb 2019 08:00:15 +0100 Subject: Why float('Nan') == float('Nan') is False In-Reply-To: <5c6419bf$0$6565$426a74cc@news.free.fr> References: <5c6419bf$0$6565$426a74cc@news.free.fr> Message-ID: <5c651200$0$21611$426a34cc@news.free.fr> Le 13/02/2019 ? 14:21, ast a ?crit?: > Hello > > >>> float('Nan') == float('Nan') > False > > Why ? > > Regards > Thank you for answers. If you wonder how I was trapped with it, here is the failing program. r = float('Nan') while r==float('Nan'): inp = input("Enter a number\n") try: r = float(inp) except ValueError: r = float('Nan') From vergos.nikolas at gmail.com Thu Feb 14 11:37:20 2019 From: vergos.nikolas at gmail.com (vergos.nikolas at gmail.com) Date: Thu, 14 Feb 2019 08:37:20 -0800 (PST) Subject: Convert a list with wrong encoding to utf8 Message-ID: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> Hello, i have tried the following to chnage encoding to utf8 because for some reason it has changed regarding list names [python] #populate client listing into list names.append( name ) .... .... names.append( '' ) names.sort() for name in names: name = name.encode('latin1').decode('utf8') [/python] and the error that was presented was: [output] UnicodeEncodeError('latin-1', '???? ???????', 0, 4, 'ordinal not in range(256)') [/output] Why it cannot encode in latin nad decode in utf8 normally? And since 'names' are being fetced from mysql database, which they were stored as utf8 strings WHY/HOW the 'names' enrolled in latin-1? From rosuav at gmail.com Thu Feb 14 11:44:47 2019 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 15 Feb 2019 03:44:47 +1100 Subject: Convert a list with wrong encoding to utf8 In-Reply-To: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> References: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> Message-ID: On Fri, Feb 15, 2019 at 3:41 AM wrote: > > Hello, i have tried the following to chnage encoding to utf8 because for some reason it has changed regarding list names > > [python] > #populate client listing into list > names.append( name ) > .... > .... > names.append( '' ) > names.sort() > > for name in names: > name = name.encode('latin1').decode('utf8') > [/python] > > and the error that was presented was: > > [output] > UnicodeEncodeError('latin-1', '???? ???????', 0, 4, 'ordinal not in range(256)') > [/output] > > Why it cannot encode in latin nad decode in utf8 normally? > And since 'names' are being fetced from mysql database, which they were stored as utf8 strings WHY/HOW the 'names' enrolled in latin-1? You're going to have to figure out what encoding they are ACTUALLY in, or (since it looks like you're working with strings) what encoding they were decoded using. Without that information, all you're doing is taking stabs in the dark. BTW, you should probably fix your encodings *before* attempting to sort the names. It doesn't make much sense to sort by byte values in mojibake. ChrisA From cspealma at redhat.com Thu Feb 14 11:44:59 2019 From: cspealma at redhat.com (Calvin Spealman) Date: Thu, 14 Feb 2019 11:44:59 -0500 Subject: Convert a list with wrong encoding to utf8 In-Reply-To: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> References: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> Message-ID: You can only decode FROM the same encoding you've encoded TO. Any decoding must know the input it receives follows the rules of its encoding scheme. latin1 is not utf8. However, in your case, you aren't seeing problem with the decoding. That step is never reached. It is failing to encode the string as latin1 because it is not compatible with the latin1 scheme. Your string contains characters which cannot be represented in latin1. It really is not clear what you're trying to accomplish here. The string encoding was already handled when you pulled this out of the database and you should not need to do anything like this at all. You already have a decoded string, because in python ALL strings are decoded already. Encoding is only a process of converting strings to raw bytes for storage or transmission, which you don't appear to be doing here. On Thu, Feb 14, 2019 at 11:40 AM wrote: > Hello, i have tried the following to chnage encoding to utf8 because for > some reason it has changed regarding list names > > [python] > #populate client listing into list > names.append( name ) > .... > .... > names.append( '' ) > names.sort() > > for name in names: > name = name.encode('latin1').decode('utf8') > [/python] > > and the error that was presented was: > > [output] > UnicodeEncodeError('latin-1', '???? ???????', 0, 4, 'ordinal not in > range(256)') > [/output] > > Why it cannot encode in latin nad decode in utf8 normally? > And since 'names' are being fetced from mysql database, which they were > stored as utf8 strings WHY/HOW the 'names' enrolled in latin-1? > -- > https://mail.python.org/mailman/listinfo/python-list > -- CALVIN SPEALMAN SENIOR QUALITY ENGINEER cspealma at redhat.com M: +1.336.210.5107 TRIED. TESTED. TRUSTED. From pfeiffer at cs.nmsu.edu Thu Feb 14 11:52:30 2019 From: pfeiffer at cs.nmsu.edu (Joe Pfeiffer) Date: Thu, 14 Feb 2019 09:52:30 -0700 Subject: Why float('Nan') == float('Nan') is False References: <5c6419bf$0$6565$426a74cc@news.free.fr> <5c651200$0$21611$426a34cc@news.free.fr> Message-ID: <1bva1mxqgh.fsf@pfeifferfamily.net> ast writes: > Le 13/02/2019 ? 14:21, ast a ?crit?: >> Hello >> >> >>> float('Nan') == float('Nan') >> False >> >> Why ? >> >> Regards >> > > Thank you for answers. > > If you wonder how I was trapped with it, here > is the failing program. > > > r = float('Nan') > > while r==float('Nan'): > inp = input("Enter a number\n") > try: > r = float(inp) > except ValueError: > r = float('Nan') import math while math.isnan(r) : will do what you're looking for. If you're using python 3.5 or higher, you can also use math.nan instead of float('nan'). From rosuav at gmail.com Thu Feb 14 11:58:03 2019 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 15 Feb 2019 03:58:03 +1100 Subject: Why float('Nan') == float('Nan') is False In-Reply-To: <1bva1mxqgh.fsf@pfeifferfamily.net> References: <5c6419bf$0$6565$426a74cc@news.free.fr> <5c651200$0$21611$426a34cc@news.free.fr> <1bva1mxqgh.fsf@pfeifferfamily.net> Message-ID: On Fri, Feb 15, 2019 at 3:56 AM Joe Pfeiffer wrote: > > ast writes: > > > Le 13/02/2019 ? 14:21, ast a ?crit : > >> Hello > >> > >> >>> float('Nan') == float('Nan') > >> False > >> > >> Why ? > >> > >> Regards > >> > > > > Thank you for answers. > > > > If you wonder how I was trapped with it, here > > is the failing program. > > > > > > r = float('Nan') > > > > while r==float('Nan'): > > inp = input("Enter a number\n") > > try: > > r = float(inp) > > except ValueError: > > r = float('Nan') > > import math > while math.isnan(r) : > > will do what you're looking for. > > If you're using python 3.5 or higher, you can also use math.nan instead > of float('nan'). Or even better, use None instead of nan. There's nothing in Python says you have to (ab)use a floating-point value as a signal. Or use "while True" and add a break if the exception isn't thrown. ChrisA From vergos.nikolas at gmail.com Thu Feb 14 12:14:50 2019 From: vergos.nikolas at gmail.com (vergos.nikolas at gmail.com) Date: Thu, 14 Feb 2019 09:14:50 -0800 (PST) Subject: Convert a list with wrong encoding to utf8 In-Reply-To: References: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> Message-ID: <60648959-b1f1-4db6-94b7-458a31cad008@googlegroups.com> ?? ??????, 14 ??????????? 2019 - 6:45:29 ?.?. UTC+2, ? ??????? Calvin Spealman ??????: > You can only decode FROM the same encoding you've encoded TO. Any decoding > must know the input it receives follows the rules of its encoding scheme. > latin1 is not utf8. > > However, in your case, you aren't seeing problem with the decoding. That > step is never reached. It is failing to encode the string as latin1 because > it is not compatible with the latin1 scheme. Your string contains > characters which cannot be represented in latin1. > > It really is not clear what you're trying to accomplish here. The string > encoding was already handled when you pulled this out of the database and > you should not need to do anything like this at all. You already have a > decoded string, because in python ALL strings are decoded already. Encoding > is only a process of converting strings to raw bytes for storage or > transmission, which you don't appear to be doing here. Names in database are stored in utf8 When the script runs it reads them and handles them as utf8, right? If it like this, then why when i print 'names' list i see bytes in hexadecimal format? '\xce\x86\xce\xba\xce\xb7\xcf\x82 \xce\xa4\xcf\x83\xce\xb9\xce\xac\xce\xbc\xce\xb7\xcf\x82' And only if i for name in names: print( name.encode('latin1').decode('utf8') ) i can see the values of 'name' list correctly in Greek. But where did the latin-iso took in place? And aparrt for printing the name like above how can i store them in proper utf ? From uri at speedy.net Thu Feb 14 12:38:20 2019 From: uri at speedy.net (=?UTF-8?B?15DXldeo15k=?=) Date: Thu, 14 Feb 2019 19:38:20 +0200 Subject: Why float('Nan') == float('Nan') is False In-Reply-To: <1b4l97yxlx.fsf@pfeifferfamily.net> References: <5c6419bf$0$6565$426a74cc@news.free.fr> <005e01d4c3ce$7403c370$5c0b4a50$@verizon.net> <1b4l97yxlx.fsf@pfeifferfamily.net> Message-ID: > Or even better, use None instead of nan. ++ On Thu, Feb 14, 2019 at 3:26 AM Joe Pfeiffer wrote: > uri at speedy.net writes: > > > There are more integers than odd numbers, and more odd numbers than prime > > numbers. An infinite set may be a subset of another infinite set although > > they may both have the same cardinality. Or in other words, the number of > > elements in each set is not equal. One has more elements than the other. > > AND, by induction you can also prove that the other one has more elements > > than the first one. So the number of elements in two infinite sets can't > be > > equal. Even, if you compare the same set to itself. > > You would expect that to be true, but it is not. There are in fact the > same number of odd integers as integers, and the same number of primes > as integers. Counterintuitive but true. > I know it's a Python mailing list and not Math, and this thread is off-topic. But anyway, it depends how you define "more". There are infinite integers and odd numbers, and as I said you can't compare infinite "numbers" of elements. But, the set of odd numbers is a subset of the set of integers. If you take any big range, for example from 0 to google (10**100) - there are more integers in this range than odd numbers. There are integers which are not odd numbers but there are no odd numbers which are not integers. The set of integers which are not odd numbers is infinite. So in this sense, there are more integers than odd numbers. And also, there are more odd numbers than prime numbers (there is one prime number which is not odd, and many many odd numbers which are not prime). > There are in fact the same number of odd integers as integers, and the same number of primes as integers. If you mean that the "number" of odd integers is equal to the "number" of integers, it is not. They are both infinite and infinity is not a number. Two sets can have the same cardinality even if one set contains more elements than the other. At least in the sense I define "more". A cardinality is equal to the number of elements only for finite sets. For infinite sets the cardinality is not the number of elements, the number of elements is infinite. > -- > https://mail.python.org/mailman/listinfo/python-list > From digitalfantasy.it86559 at digitalfantasy.it Thu Feb 14 12:45:17 2019 From: digitalfantasy.it86559 at digitalfantasy.it (Liste guru) Date: Thu, 14 Feb 2019 18:45:17 +0100 Subject: What's up with Activestate Python? In-Reply-To: References: Message-ID: Il 14/02/2019 00:06, Grant Edwards ha scritto: > For many, many years I've always installed ActiveState's ActivePython > Community edition when forced to use Windows. It has always included > ... > I guess it's time to switch to Anaconda or ??? ??? I've also used the ActiveState python, expecially for the 2.7.x series, mainly for the oflline docs and the pywin32 libraries. ??? Now the situation is better and with pip is really easy to have an updated python with a lot of libs so there is less need for the ActiveState distribution. ??? My 2 cent. ??? Daniele Forghieri From pfeiffer at cs.nmsu.edu Thu Feb 14 12:54:38 2019 From: pfeiffer at cs.nmsu.edu (Joe Pfeiffer) Date: Thu, 14 Feb 2019 10:54:38 -0700 Subject: Why float('Nan') == float('Nan') is False References: <5c6419bf$0$6565$426a74cc@news.free.fr> <5c651200$0$21611$426a34cc@news.free.fr> <1bva1mxqgh.fsf@pfeifferfamily.net> Message-ID: <1br2caxnkx.fsf@pfeifferfamily.net> Chris Angelico writes: > > Or even better, use None instead of nan. There's nothing in Python > says you have to (ab)use a floating-point value as a signal. Or use > "while True" and add a break if the exception isn't thrown. Good point. From grant.b.edwards at gmail.com Thu Feb 14 13:10:03 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Thu, 14 Feb 2019 18:10:03 -0000 (UTC) Subject: What's up with Activestate Python? References: Message-ID: On 2019-02-14, Liste guru wrote: > Il 14/02/2019 00:06, Grant Edwards ha scritto: >> For many, many years I've always installed ActiveState's ActivePython >> Community edition when forced to use Windows. It has always included >> ... >> I guess it's time to switch to Anaconda or ??? > > I've also used the ActiveState python, expecially for the 2.7.x > series, mainly for the oflline docs and the pywin32 libraries. > > Now the situation is better and with pip is really easy to have an > updated python with a lot of libs so there is less need for the > ActiveState distribution. How does that work for libraries that require a C compiler? Does pip know how to download and install any required C/C++ toolchains? -- Grant Edwards grant.b.edwards Yow! When you get your at PH.D. will you get able to gmail.com work at BURGER KING? From rosuav at gmail.com Thu Feb 14 13:14:11 2019 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 15 Feb 2019 05:14:11 +1100 Subject: What's up with Activestate Python? In-Reply-To: References: Message-ID: On Fri, Feb 15, 2019 at 5:11 AM Grant Edwards wrote: > > On 2019-02-14, Liste guru wrote: > > Il 14/02/2019 00:06, Grant Edwards ha scritto: > >> For many, many years I've always installed ActiveState's ActivePython > >> Community edition when forced to use Windows. It has always included > >> ... > >> I guess it's time to switch to Anaconda or ??? > > > > I've also used the ActiveState python, expecially for the 2.7.x > > series, mainly for the oflline docs and the pywin32 libraries. > > > > Now the situation is better and with pip is really easy to have an > > updated python with a lot of libs so there is less need for the > > ActiveState distribution. > > How does that work for libraries that require a C compiler? Does pip > know how to download and install any required C/C++ toolchains? > Ideally, it'll be downloading prebuilt wheels. Whether that's always possible, I don't know. ChrisA From cspealma at redhat.com Thu Feb 14 13:16:12 2019 From: cspealma at redhat.com (Calvin Spealman) Date: Thu, 14 Feb 2019 13:16:12 -0500 Subject: Convert a list with wrong encoding to utf8 In-Reply-To: <60648959-b1f1-4db6-94b7-458a31cad008@googlegroups.com> References: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> <60648959-b1f1-4db6-94b7-458a31cad008@googlegroups.com> Message-ID: If you see something like this '\xce\x86\xce\xba\xce\xb7\xcf\x82 \xce\xa4\xcf\x83\xce\xb9\xce\xac\xce\xbc\xce\xb7\xcf\x82' then you don't have a string, you have raw bytes. You don't "encode" bytes, you decode them. If you know this is already encoded as UTF-8 then you just need the decode('utf8') part and *not* the encode('latin1') step. encode() is something that turns text into bytes decode() is something that turns bytes into text So, if you already have bytes and you need text, you should only want to be doing a decode() and you just need to specific the correct encoding. On Thu, Feb 14, 2019 at 12:15 PM wrote: > ?? ??????, 14 ??????????? 2019 - 6:45:29 ?.?. UTC+2, ? ??????? Calvin > Spealman ??????: > > You can only decode FROM the same encoding you've encoded TO. Any > decoding > > must know the input it receives follows the rules of its encoding scheme. > > latin1 is not utf8. > > > > However, in your case, you aren't seeing problem with the decoding. That > > step is never reached. It is failing to encode the string as latin1 > because > > it is not compatible with the latin1 scheme. Your string contains > > characters which cannot be represented in latin1. > > > > It really is not clear what you're trying to accomplish here. The string > > encoding was already handled when you pulled this out of the database and > > you should not need to do anything like this at all. You already have a > > decoded string, because in python ALL strings are decoded already. > Encoding > > is only a process of converting strings to raw bytes for storage or > > transmission, which you don't appear to be doing here. > > Names in database are stored in utf8 > When the script runs it reads them and handles them as utf8, right? > > If it like this, then why when i print 'names' list i see bytes in > hexadecimal format? > > '\xce\x86\xce\xba\xce\xb7\xcf\x82 > \xce\xa4\xcf\x83\xce\xb9\xce\xac\xce\xbc\xce\xb7\xcf\x82' > > And only if i > > for name in names: > print( name.encode('latin1').decode('utf8') ) > > i can see the values of 'name' list correctly in Greek. > > But where did the latin-iso took in place? And aparrt for printing the > name like above how can i store them in proper utf ? > -- > https://mail.python.org/mailman/listinfo/python-list > -- CALVIN SPEALMAN SENIOR QUALITY ENGINEER cspealma at redhat.com M: +1.336.210.5107 TRIED. TESTED. TRUSTED. From python at mrabarnett.plus.com Thu Feb 14 13:56:04 2019 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 14 Feb 2019 18:56:04 +0000 Subject: Convert a list with wrong encoding to utf8 In-Reply-To: References: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> <60648959-b1f1-4db6-94b7-458a31cad008@googlegroups.com> Message-ID: <72f4ed24-c871-702d-efbb-18554c91da3d@mrabarnett.plus.com> On 2019-02-14 18:16, Calvin Spealman wrote: > If you see something like this > > '\xce\x86\xce\xba\xce\xb7\xcf\x82 > \xce\xa4\xcf\x83\xce\xb9\xce\xac\xce\xbc\xce\xb7\xcf\x82' > > then you don't have a string, you have raw bytes. You don't "encode" bytes, > you decode them. If you know this is already encoded as UTF-8 then you just > need the decode('utf8') part and *not* the encode('latin1') step. > > encode() is something that turns text into bytes > decode() is something that turns bytes into text > > So, if you already have bytes and you need text, you should only want to be > doing a decode() and you just need to specific the correct encoding. > It doesn't have a 'b' prefix, so either it's Python 2 or it's a Unicode string that was decoded wrongly from the bytes. > On Thu, Feb 14, 2019 at 12:15 PM wrote: > >> ?? ??????, 14 ??????????? 2019 - 6:45:29 ?.?. UTC+2, ? ??????? Calvin >> Spealman ??????: >> > You can only decode FROM the same encoding you've encoded TO. Any >> decoding >> > must know the input it receives follows the rules of its encoding scheme. >> > latin1 is not utf8. >> > >> > However, in your case, you aren't seeing problem with the decoding. That >> > step is never reached. It is failing to encode the string as latin1 >> because >> > it is not compatible with the latin1 scheme. Your string contains >> > characters which cannot be represented in latin1. >> > >> > It really is not clear what you're trying to accomplish here. The string >> > encoding was already handled when you pulled this out of the database and >> > you should not need to do anything like this at all. You already have a >> > decoded string, because in python ALL strings are decoded already. >> Encoding >> > is only a process of converting strings to raw bytes for storage or >> > transmission, which you don't appear to be doing here. >> >> Names in database are stored in utf8 >> When the script runs it reads them and handles them as utf8, right? >> >> If it like this, then why when i print 'names' list i see bytes in >> hexadecimal format? >> >> '\xce\x86\xce\xba\xce\xb7\xcf\x82 >> \xce\xa4\xcf\x83\xce\xb9\xce\xac\xce\xbc\xce\xb7\xcf\x82' >> >> And only if i >> >> for name in names: >> print( name.encode('latin1').decode('utf8') ) >> >> i can see the values of 'name' list correctly in Greek. >> >> But where did the latin-iso took in place? And aparrt for printing the >> name like above how can i store them in proper utf ? >> From vergos.nikolas at gmail.com Thu Feb 14 14:02:14 2019 From: vergos.nikolas at gmail.com (vergos.nikolas at gmail.com) Date: Thu, 14 Feb 2019 11:02:14 -0800 (PST) Subject: Convert a list with wrong encoding to utf8 In-Reply-To: References: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> <60648959-b1f1-4db6-94b7-458a31cad008@googlegroups.com> Message-ID: ?? ??????, 14 ??????????? 2019 - 8:16:40 ?.?. UTC+2, ? ??????? Calvin Spealman ??????: > If you see something like this > > '\xce\x86\xce\xba\xce\xb7\xcf\x82 > \xce\xa4\xcf\x83\xce\xb9\xce\xac\xce\xbc\xce\xb7\xcf\x82' > > then you don't have a string, you have raw bytes. You don't "encode" bytes, > you decode them. If you know this is already encoded as UTF-8 then you just > need the decode('utf8') part and *not* the encode('latin1') step. > > encode() is something that turns text into bytes > decode() is something that turns bytes into text > > So, if you already have bytes and you need text, you should only want to be > doing a decode() and you just need to specific the correct encoding. I Agree but I don't know in what encoding the string is encoded into. I just tried names = tuple( [s.decode('utf8') for s in names] ) but i get the error of: AttributeError("'str' object has no attribute 'decode'",) but why it says s is a string object? Since we have names in raw bytes is should be a bytes object? How can i turn names from raw bytes to utf-8 strings? ps. Who encoded them in raw bytes anyways? Since they fetced directly from the database shouldn't python3 have them stored in names as utf-8 strings? why raw bytes instead? From ikorot01 at gmail.com Thu Feb 14 14:13:34 2019 From: ikorot01 at gmail.com (Igor Korot) Date: Thu, 14 Feb 2019 13:13:34 -0600 Subject: Convert a list with wrong encoding to utf8 In-Reply-To: References: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> <60648959-b1f1-4db6-94b7-458a31cad008@googlegroups.com> Message-ID: Hi, On Thu, Feb 14, 2019 at 1:10 PM wrote: > > ?? ??????, 14 ??????????? 2019 - 8:16:40 ?.?. UTC+2, ? ??????? Calvin Spealman ??????: > > If you see something like this > > > > '\xce\x86\xce\xba\xce\xb7\xcf\x82 > > \xce\xa4\xcf\x83\xce\xb9\xce\xac\xce\xbc\xce\xb7\xcf\x82' > > > > then you don't have a string, you have raw bytes. You don't "encode" bytes, > > you decode them. If you know this is already encoded as UTF-8 then you just > > need the decode('utf8') part and *not* the encode('latin1') step. > > > > encode() is something that turns text into bytes > > decode() is something that turns bytes into text > > > > So, if you already have bytes and you need text, you should only want to be > > doing a decode() and you just need to specific the correct encoding. > > I Agree but I don't know in what encoding the string is encoded into. > > I just tried > > names = tuple( [s.decode('utf8') for s in names] ) > > but i get the error of: > > AttributeError("'str' object has no attribute 'decode'",) > > but why it says s is a string object? Since we have names in raw bytes is should be a bytes object? > > How can i turn names from raw bytes to utf-8 strings? > > ps. Who encoded them in raw bytes anyways? Since they fetced directly from the database shouldn't python3 have them stored in names as utf-8 strings? why raw bytes instead? What DBMS? How do you access the DB? Maybe the field is BLOB? Thank you. > -- > https://mail.python.org/mailman/listinfo/python-list From acharbly at gmail.com Wed Feb 13 22:29:19 2019 From: acharbly at gmail.com (Prahallad Achar) Date: Thu, 14 Feb 2019 08:59:19 +0530 Subject: Problem : Generator Message-ID: How to implement reverse generator It is only passing data in reverse or how it is Yeild always returns next value and is question valid? Thanks and Regards Prahallad From vergos.nikolas at gmail.com Thu Feb 14 14:18:14 2019 From: vergos.nikolas at gmail.com (vergos.nikolas at gmail.com) Date: Thu, 14 Feb 2019 11:18:14 -0800 (PST) Subject: Convert a list with wrong encoding to utf8 In-Reply-To: References: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> <60648959-b1f1-4db6-94b7-458a31cad008@googlegroups.com> Message-ID: <218e5afa-4919-4eb7-a698-d44359a73089@googlegroups.com> ?? ??????, 14 ??????????? 2019 - 9:14:08 ?.?. UTC+2, ? ??????? Igor Korot ??????: > Hi, > > On Thu, Feb 14, 2019 at 1:10 PM wrote: > > > > ?? ??????, 14 ??????????? 2019 - 8:16:40 ?.?. UTC+2, ? ??????? Calvin Spealman ??????: > > > If you see something like this > > > > > > '\xce\x86\xce\xba\xce\xb7\xcf\x82 > > > \xce\xa4\xcf\x83\xce\xb9\xce\xac\xce\xbc\xce\xb7\xcf\x82' > > > > > > then you don't have a string, you have raw bytes. You don't "encode" bytes, > > > you decode them. If you know this is already encoded as UTF-8 then you just > > > need the decode('utf8') part and *not* the encode('latin1') step. > > > > > > encode() is something that turns text into bytes > > > decode() is something that turns bytes into text > > > > > > So, if you already have bytes and you need text, you should only want to be > > > doing a decode() and you just need to specific the correct encoding. > > > > I Agree but I don't know in what encoding the string is encoded into. > > > > I just tried > > > > names = tuple( [s.decode('utf8') for s in names] ) > > > > but i get the error of: > > > > AttributeError("'str' object has no attribute 'decode'",) > > > > but why it says s is a string object? Since we have names in raw bytes is should be a bytes object? > > > > How can i turn names from raw bytes to utf-8 strings? > > > > ps. Who encoded them in raw bytes anyways? Since they fetced directly from the database shouldn't python3 have them stored in names as utf-8 strings? why raw bytes instead? > > What DBMS? How do you access the DB? > Maybe the field is BLOB? No, the fields are all 'varchar' i use pymysql and i utilize it like so to grab the values from the db as utf-8. con = pymysql.connect( db = 'clientele', user = 'vergos', passwd = '******', charset = 'utf8' ) cur = con.cursor() From vergos.nikolas at gmail.com Thu Feb 14 14:15:35 2019 From: vergos.nikolas at gmail.com (=?UTF-8?B?zp3Or866zr/PgiDOks6tz4HOs86/z4I=?=) Date: Thu, 14 Feb 2019 21:15:35 +0200 Subject: Convert a list with wrong encoding to utf8 In-Reply-To: References: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> <60648959-b1f1-4db6-94b7-458a31cad008@googlegroups.com> Message-ID: con = pymysql.connect( db = 'clientele', user = 'vergos', passwd = '*******', charset = 'utf8' ) cur = con.cursor() ???? ???, 14 ??? 2019 ???? 9:13 ?.?., ?/? Igor Korot ??????: > Hi, > > On Thu, Feb 14, 2019 at 1:10 PM wrote: > > > > ?? ??????, 14 ??????????? 2019 - 8:16:40 ?.?. UTC+2, ? ??????? Calvin > Spealman ??????: > > > If you see something like this > > > > > > '\xce\x86\xce\xba\xce\xb7\xcf\x82 > > > \xce\xa4\xcf\x83\xce\xb9\xce\xac\xce\xbc\xce\xb7\xcf\x82' > > > > > > then you don't have a string, you have raw bytes. You don't "encode" > bytes, > > > you decode them. If you know this is already encoded as UTF-8 then you > just > > > need the decode('utf8') part and *not* the encode('latin1') step. > > > > > > encode() is something that turns text into bytes > > > decode() is something that turns bytes into text > > > > > > So, if you already have bytes and you need text, you should only want > to be > > > doing a decode() and you just need to specific the correct encoding. > > > > I Agree but I don't know in what encoding the string is encoded into. > > > > I just tried > > > > names = tuple( [s.decode('utf8') for s in names] ) > > > > but i get the error of: > > > > AttributeError("'str' object has no attribute 'decode'",) > > > > but why it says s is a string object? Since we have names in raw bytes > is should be a bytes object? > > > > How can i turn names from raw bytes to utf-8 strings? > > > > ps. Who encoded them in raw bytes anyways? Since they fetced directly > from the database shouldn't python3 have them stored in names as utf-8 > strings? why raw bytes instead? > > What DBMS? How do you access the DB? > Maybe the field is BLOB? > > Thank you. > > > -- > > https://mail.python.org/mailman/listinfo/python-list > From vergos.nikolas at gmail.com Thu Feb 14 14:55:38 2019 From: vergos.nikolas at gmail.com (vergos.nikolas at gmail.com) Date: Thu, 14 Feb 2019 11:55:38 -0800 (PST) Subject: Convert a list with wrong encoding to utf8 In-Reply-To: References: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> <60648959-b1f1-4db6-94b7-458a31cad008@googlegroups.com> <72f4ed24-c871-702d-efbb-18554c91da3d@mrabarnett.plus.com> Message-ID: <193fee5d-fee4-4a6f-9e0c-8cade01b1461@googlegroups.com> ?? ??????, 14 ??????????? 2019 - 8:56:31 ?.?. UTC+2, ? ??????? MRAB ??????: > It doesn't have a 'b' prefix, so either it's Python 2 or it's a Unicode > string that was decoded wrongly from the bytes. Yes it doesnt have the 'b' prefix so that hexadecimal are representation of strings and not representation of bytes. I just tried: names = tuple( [s.encode('latin1').decode('utf8') for s in names] ) but i get UnicodeEncodeError('latin-1', '???? ???????', 0, 4, 'ordinal not in range(256)') '???? ???????' is a valid name but even so it gives an error. Is it possible that Python3 a Unicode had the string wrongly decoded from the bytes ? What can i do to get the names?! From vergos.nikolas at gmail.com Thu Feb 14 15:11:40 2019 From: vergos.nikolas at gmail.com (vergos.nikolas at gmail.com) Date: Thu, 14 Feb 2019 12:11:40 -0800 (PST) Subject: Convert a list with wrong encoding to utf8 In-Reply-To: <193fee5d-fee4-4a6f-9e0c-8cade01b1461@googlegroups.com> References: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> <60648959-b1f1-4db6-94b7-458a31cad008@googlegroups.com> <72f4ed24-c871-702d-efbb-18554c91da3d@mrabarnett.plus.com> <193fee5d-fee4-4a6f-9e0c-8cade01b1461@googlegroups.com> Message-ID: <33474eb5-8230-4c77-b6e5-8a35207ff73d@googlegroups.com> I'm using Python3 and pymysql and already have charset presnt [python] con = pymysql.connect( db = 'clientele', user = 'vergos', passwd = '******', charset = 'utf8' ) cur = con.cursor() [/python] >From that i understand that the names being fetched from the db to pyhton script are being fetced as utf8, right? I dont convert, format the string in the meanwhile. Python3 handles the encoidng and i dont know from where latin iso get into the middle but when i [python]names = tuple( [s.encode('latin1').decode('utf8') for s in names] )[/python] [output]UnicodeEncodeError('latin-1', '???? ???????', 0, 4, 'ordinal not in range(256)')[/output] which is a perfectly valid names but still gives an error. Also the strings produced '\xce\x86\xce\xba\xce\xb7\xcf\x82 \xce\xa4\xcf\x83\xce\xb9\xce\xac\xce\xbc\xce\xb7\xcf\x82' are strings not raw bytes. WHY Python3 instead of fetching the values from the db as 'utf8' it stores the values in hex representation? From David.Raymond at tomtom.com Thu Feb 14 15:17:49 2019 From: David.Raymond at tomtom.com (David Raymond) Date: Thu, 14 Feb 2019 20:17:49 +0000 Subject: Convert a list with wrong encoding to utf8 In-Reply-To: <193fee5d-fee4-4a6f-9e0c-8cade01b1461@googlegroups.com> References: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> <60648959-b1f1-4db6-94b7-458a31cad008@googlegroups.com> <72f4ed24-c871-702d-efbb-18554c91da3d@mrabarnett.plus.com> <193fee5d-fee4-4a6f-9e0c-8cade01b1461@googlegroups.com> Message-ID: Next question is how did you _insert_ those names into the database previously? Are the names showing up ok using any other tool to look at them? The error might have been on insert and you're just seeing weird stuff now because of that. Maybe, where instead of giving it the text and letting the module deal with encodings, you gave it the raw UTF-8 encoding, and the module or db server said "let me encode that into the field or database defined default of latin-1 for you"... or something like that. -----Original Message----- From: Python-list [mailto:python-list-bounces+david.raymond=tomtom.com at python.org] On Behalf Of vergos.nikolas at gmail.com Sent: Thursday, February 14, 2019 2:56 PM To: python-list at python.org Subject: Re: Convert a list with wrong encoding to utf8 ?? ??????, 14 ??????????? 2019 - 8:56:31 ?.?. UTC+2, ? ??????? MRAB ??????: > It doesn't have a 'b' prefix, so either it's Python 2 or it's a Unicode > string that was decoded wrongly from the bytes. Yes it doesnt have the 'b' prefix so that hexadecimal are representation of strings and not representation of bytes. I just tried: names = tuple( [s.encode('latin1').decode('utf8') for s in names] ) but i get UnicodeEncodeError('latin-1', '???? ???????', 0, 4, 'ordinal not in range(256)') '???? ???????' is a valid name but even so it gives an error. Is it possible that Python3 a Unicode had the string wrongly decoded from the bytes ? What can i do to get the names?! -- https://mail.python.org/mailman/listinfo/python-list From torriem at gmail.com Thu Feb 14 15:41:51 2019 From: torriem at gmail.com (Michael Torrie) Date: Thu, 14 Feb 2019 13:41:51 -0700 Subject: Convert a list with wrong encoding to utf8 In-Reply-To: References: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> <60648959-b1f1-4db6-94b7-458a31cad008@googlegroups.com> Message-ID: <33a482c6-56ec-b2c4-3f77-ba38e50072de@gmail.com> On 02/14/2019 12:02 PM, vergos.nikolas at gmail.com wrote: > ?? ??????, 14 ??????????? 2019 - 8:16:40 ?.?. UTC+2, ? ??????? Calvin Spealman ??????: >> If you see something like this >> >> '\xce\x86\xce\xba\xce\xb7\xcf\x82 >> \xce\xa4\xcf\x83\xce\xb9\xce\xac\xce\xbc\xce\xb7\xcf\x82' >> >> then you don't have a string, you have raw bytes. You don't "encode" bytes, >> you decode them. If you know this is already encoded as UTF-8 then you just >> need the decode('utf8') part and *not* the encode('latin1') step. >> >> encode() is something that turns text into bytes >> decode() is something that turns bytes into text >> >> So, if you already have bytes and you need text, you should only want to be >> doing a decode() and you just need to specific the correct encoding. > > I Agree but I don't know in what encoding the string is encoded into. > > I just tried > > names = tuple( [s.decode('utf8') for s in names] ) > > but i get the error of: > > AttributeError("'str' object has no attribute 'decode'",) Strictly speaking, that's correct. A Python 3 string object is already decoded unicode. It cannot be decoded again. > but why it says s is a string object? Since we have names in raw bytes is should be a bytes object? It's clearly not raw bytes. > How can i turn names from raw bytes to utf-8 strings? They apparently aren't raw bytes. If they were, you could use .decode() > ps. Who encoded them in raw bytes anyways? Since they fetced directly from the database shouldn't > python3 have them stored in names as utf-8 strings? why raw bytes instead? Something very strange is going on with your database and/or your queries. The pymysql api should be already decoding the utf-8 bytes for you and returning a unicode string. I have no idea why you're getting a unicode string that consists of code points that are the same as the utf-8 bytes. You'll have to post a little bit more of your code, like a simple, complete query example (a few lines of code) that shows absolutely everything you're trying to do to the string. Also you will want to use the mysql command-line utilities to try your queries and see what kind of data you're getting out. Because if mysql is told to use utf-8 for varchar, and if you're inserting the data using correctly-formed utf-8 encoded byte strings, it should come back out in Python as unicode. From ben+python at benfinney.id.au Thu Feb 14 17:09:06 2019 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 15 Feb 2019 09:09:06 +1100 Subject: Problem : Generator References: Message-ID: <86ftsq6n0d.fsf@benfinney.id.au> Prahallad Achar writes: > How to implement reverse generator Welcome to the Python forum! That sounds like an interesting problem. Can you describe it more precisely? What should a ?reverse generator? actually do (and not do)? Ideally, give an example: * Some code you would maybe expect to create a "reverse generator", that you have already tried but doesn't work. * Exactly what you *expect* the resulting object to do; what is its expected behaviour? What is its expected output? -- \ ?Software patents provide one more means of controlling access | `\ to information. They are the tool of choice for the internet | _o__) highwayman.? ?Anthony Taylor | Ben Finney From avigross at verizon.net Thu Feb 14 17:49:41 2019 From: avigross at verizon.net (Avi Gross) Date: Thu, 14 Feb 2019 17:49:41 -0500 Subject: FW: Why float('Nan') == float('Nan') is False References: <5c6419bf$0$6565$426a74cc@news.free.fr> <5c651200$0$21611$426a34cc@news.free.fr> Message-ID: <008e01d4c4b7$92c4d760$b84e8620$@verizon.net> Other people have replied well enough with better ways to do this but I am stuck on WHY this was seen as a way to do this at all. The code was: r = float('Nan') while r==float('Nan'): inp = input("Enter a number\n") try: r = float(inp) except ValueError: r = float('Nan') But with a single exception, what the user types in to the input statement is either a valid number OR it throws an exception. The exception is a string like "nan" which does indeed return something unusual: >>> a = float('nan') >>> a Nan With that exception, any sentinel is legitimate for such use such as a Boolean valid = False that you reset to true if float(inp) is working. So what if the user types in some variant of "nan" perhaps preceded or followed by whitespace or more? I experimented a bit: >>> float("nan ") nan >>> float(" nan") Nan >>> float(" nAn") nan >>> float("nan ny") Traceback (most recent call last): File "", line 1, in float("nan ny") ValueError: could not convert string to float: 'nan ny' It seems the algorithm strips whitespace on both sides and converts the text to upper or lower and is happy if what is left is three characters corresponding to n a and n. So if you want a normal number, you have choices. One is to check the string explicitly before trying to float it. if text.strip().lower() == 'nan' : ... Another is to float it unquestioned and if exception is thrown, check the proper way if it is a NaN as in: math.isnan(x) As noted earlier, and avoiding another mathematical exploration not needed further in this forum, some things in python are best not to be touched directly but with specially created access functions that can handle them well. -----Original Message----- From: Python-list On Behalf Of ast Sent: Thursday, February 14, 2019 2:00 AM To: python-list at python.org Subject: Re: Why float('Nan') == float('Nan') is False Le 13/02/2019 ? 14:21, ast a ?crit : > Hello > > >>> float('Nan') == float('Nan') > False > > Why ? > > Regards > Thank you for answers. If you wonder how I was trapped with it, here is the failing program. r = float('Nan') while r==float('Nan'): inp = input("Enter a number\n") try: r = float(inp) except ValueError: r = float('Nan') -- https://mail.python.org/mailman/listinfo/python-list From grant.b.edwards at gmail.com Thu Feb 14 18:14:43 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Thu, 14 Feb 2019 23:14:43 -0000 (UTC) Subject: FW: Why float('Nan') == float('Nan') is False References: <5c6419bf$0$6565$426a74cc@news.free.fr> <5c651200$0$21611$426a34cc@news.free.fr> <008e01d4c4b7$92c4d760$b84e8620$@verizon.net> Message-ID: On 2019-02-14, Avi Gross wrote: > I experimented a bit: > >>>> float("nan ") > nan >>>> float(" nan") > Nan >>>> float(" nAn") > nan That's curious. I've never seen "Nan" before. What version of Python are you using? -- Grant Edwards grant.b.edwards Yow! NANCY!! Why is at everything RED?! gmail.com From avigross at verizon.net Thu Feb 14 22:36:26 2019 From: avigross at verizon.net (Avi Gross) Date: Thu, 14 Feb 2019 22:36:26 -0500 Subject: FW: Why float('Nan') == float('Nan') is False In-Reply-To: References: <5c6419bf$0$6565$426a74cc@news.free.fr> <5c651200$0$21611$426a34cc@news.free.fr> <008e01d4c4b7$92c4d760$b84e8620$@verizon.net> Message-ID: <00dd01d4c4df$a1775350$e465f9f0$@verizon.net> Grant, I can see why you may be wondering. You see the nan concept as having a specific spelling using all lowercase and to an extent you are right. As I pointed out. In the context of a string to convert to a float, any upper/lower-case spelling of NaN is accepted. But, to answer you anyway, I actually use many versions of python as I have loaded direct versions as well as through Cygwin and the Anaconda distribution. Should not be relevant as explained. But when displaying a value that is of that not-quite-type, all versions have a print definition as all lower case as in 'nan' I think. But here is a curiosity. The numpy add-on package has a nan that is UNIQUE so two copies are the same. Read this transcript and see if it might sometimes even be useful while perhaps confusing the heck out of people who assume all nans are the same, or is it all nans are different? >>> floata = float('nan') >>> floatb = float('nan') >>> floata, floatb (nan, nan) >>> floata == floatb False >>> floata is floatb False >>> numpya = numpy.nan >>> numpyb = numpy.nan >>> numpya, numpyb (nan, nan) >>> numpya == numpyb False >>> numpya is numpyb True -----Original Message----- From: Python-list On Behalf Of Grant Edwards Sent: Thursday, February 14, 2019 6:15 PM To: python-list at python.org Subject: Re: FW: Why float('Nan') == float('Nan') is False On 2019-02-14, Avi Gross wrote: > I experimented a bit: > >>>> float("nan ") > nan >>>> float(" nan") > Nan >>>> float(" nAn") > nan That's curious. I've never seen "Nan" before. What version of Python are you using? -- Grant Edwards grant.b.edwards Yow! NANCY!! Why is at everything RED?! gmail.com -- https://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Thu Feb 14 23:11:08 2019 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 15 Feb 2019 15:11:08 +1100 Subject: FW: Why float('Nan') == float('Nan') is False In-Reply-To: <00dd01d4c4df$a1775350$e465f9f0$@verizon.net> References: <5c6419bf$0$6565$426a74cc@news.free.fr> <5c651200$0$21611$426a34cc@news.free.fr> <008e01d4c4b7$92c4d760$b84e8620$@verizon.net> <00dd01d4c4df$a1775350$e465f9f0$@verizon.net> Message-ID: On Fri, Feb 15, 2019 at 2:37 PM Avi Gross wrote: > But here is a curiosity. The numpy add-on package has a nan that is UNIQUE > so two copies are the same. Read this transcript and see if it might > sometimes even be useful while perhaps confusing the heck out of people who > assume all nans are the same, or is it all nans are different? > > >>> floata = float('nan') > >>> floatb = float('nan') > >>> floata, floatb > (nan, nan) > >>> floata == floatb > False > >>> floata is floatb > False > > >>> numpya = numpy.nan > >>> numpyb = numpy.nan > >>> numpya, numpyb > (nan, nan) > >>> numpya == numpyb > False > >>> numpya is numpyb > True > You shouldn't be testing floats for identity. >>> x = 2.0 >>> y, z = x+x, x*x >>> y == z True >>> y is z False If nan identity is confusing people, other float identity should be just as confusing. Or, just don't test value types for identity unless you're actually trying to see if they're the same object. ChrisA From avigross at verizon.net Fri Feb 15 00:15:26 2019 From: avigross at verizon.net (Avi Gross) Date: Fri, 15 Feb 2019 00:15:26 -0500 Subject: FW: Why float('Nan') == float('Nan') is False In-Reply-To: References: <5c6419bf$0$6565$426a74cc@news.free.fr> <5c651200$0$21611$426a34cc@news.free.fr> <008e01d4c4b7$92c4d760$b84e8620$@verizon.net> <00dd01d4c4df$a1775350$e465f9f0$@verizon.net> Message-ID: <000201d4c4ed$7636e7b0$62a4b710$@verizon.net> Chris, I don't wish to continue belaboring this topic but will answer you and then ignore anything non-essential. You said: > You shouldn't be testing floats for identity. I am not suggesting anyone compare floats. I repeat that a nan is not anything. Now as a technicality, it is considered a float by the type command as there is no easy way to make an int that is a nan: Here are multiple ways to make a nan: >>> f = float("nan") >>> type(f) Oddly you can make a complex nan, sort of: >>> c = complex("nan") >>> c (nan+0j) >>> type(c) >>> c+c (nan+0j) >>> c+5 (nan+0j) >>> c + 5j (nan+5j) The above makes me suspect that the underlying implementation sees a complex number as the combination of two floats. Now for a deeper anomaly and please don't tell me I shouldn't do this. There is also a math.nan that seems to behave the same as numpy.nan with a little twist. It too is unique but not the same anyway. I mean there are two objects out there in the python world that are implemented seemingly independently as well as a third that may also be a fourth and fifth and ... I will now make three kinds of nan, twice, and show how they inter-relate today in the version of python I am using at this moment. Version 3.71 hosted by IDLE under Cygwin under the latest Windblows. I suspect my other versions would do the same. >>> nanfloat1 = float("nan") >>> nanfloat2 = float("nan") >>> nanmath1 = math.nan >>> nanmath2 = math.nan >>> nannumpy1 = numpy.nan >>> nannumpy2 = numpy.nan >>> nanfloat1 is nanfloat2 False >>> nanmath1 is nanmath2 True >>> nannumpy1 is nannumpy2 True >>> nanfloat1 is nanmath1 False >>> nanfloat1 is nannumpy1 False >>> nanmath1 is nannumpy1 False This seems a tad inconsistent but perhaps completely understandable. Yet all three claim to float ... >>> list(map(type, [ nanfloat1, nanmath1, nannumpy1 ] )) [, , ] Now granted comparing floats is iffy if the floats are computed and often fails because of the internal bit representation and rounding. But asking if a copy of a float variable to a new name points to the same internal does work: >>> a = 2.0 >>> b = 2.0 >>> a is b False >>> c = a >>> a is c True What I see happening here is that math.nan is a real object of some sorts that is instantiated by the math module at a specific location and presumable setting anything to it just copies that, sort of. >>> str(math.nan) 'nan' >>> dir(math.nan) ['__abs__', '__add__', '__bool__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__int__', '__le__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__pow__', '__radd__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__round__', '__rpow__', '__rsub__', '__rtruediv__', '__set_format__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real'] >>> id(math.nan) 51774064 Oddly, every copy of it gets another address but the same other address which hints at some indirection in the way it was set up. >>> m = math.nan >>> id(m) 51774064 >>> n = math.nan >>> id(n) 51774064 >>> o = m >>> id(o) 51774064 Now do the same for the numpy.nan implementation: >>> str(numpy.nan) 'nan' >>> dir(numpy.nan) ['__abs__', '__add__', '__bool__', '__class__', '__delattr__', '__dir__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getformat__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__int__', '__le__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__pos__', '__pow__', '__radd__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__round__', '__rpow__', '__rsub__', '__rtruediv__', '__set_format__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', '__trunc__', 'as_integer_ratio', 'conjugate', 'fromhex', 'hex', 'imag', 'is_integer', 'real'] >>> id(numpy.nan) 57329632 This time that same address is reused: >>> m = numpy.nan >>> id(m) 57329632 >>> n = numpy.nan >>> id(n) 57329632 So the numpy nan is unique. The math nan is something else but confusingly generates a new but same copy. You may be getting the address o f a proxy one time and the real one another. >>> m is n True But >>> m is math.nan False Should I give up? No, the above makes some sense as the id() function shows there ware two addresses involved in one case and not the other. A truly clean implementation might have one copy system-wide as happens with None or Ellipsis (...) but it seems the development in python went in multiple directions and is no longer joined. A similar test (not shown) with numpy.nan shows the m and n above are each other as well as what they copied because they share an ID. The solution is to NOT look at nan except using the appropriate functions. >>> [ (math.isnan(nothing), numpy.isnan(nothing)) for nothing in [ float("nan"), math.nan, numpy.nan ] ] [(True, True), (True, True), (True, True)] It seems that at least those two nan checkers work the same on all Not A Number variants I have tried. So seems safe to stick with it. Let us hope nobody tells us we have yet other implementations of nan out there! Avi -----Original Message----- From: Python-list On Behalf Of Chris Angelico Sent: Thursday, February 14, 2019 11:11 PM To: Python Subject: Re: FW: Why float('Nan') == float('Nan') is False On Fri, Feb 15, 2019 at 2:37 PM Avi Gross wrote: > But here is a curiosity. The numpy add-on package has a nan that is > UNIQUE so two copies are the same. Read this transcript and see if it > might sometimes even be useful while perhaps confusing the heck out of > people who assume all nans are the same, or is it all nans are different? > > >>> floata = float('nan') > >>> floatb = float('nan') > >>> floata, floatb > (nan, nan) > >>> floata == floatb > False > >>> floata is floatb > False > > >>> numpya = numpy.nan > >>> numpyb = numpy.nan > >>> numpya, numpyb > (nan, nan) > >>> numpya == numpyb > False > >>> numpya is numpyb > True > You shouldn't be testing floats for identity. >>> x = 2.0 >>> y, z = x+x, x*x >>> y == z True >>> y is z False If nan identity is confusing people, other float identity should be just as confusing. Or, just don't test value types for identity unless you're actually trying to see if they're the same object. ChrisA -- https://mail.python.org/mailman/listinfo/python-list From dieter at handshake.de Fri Feb 15 02:02:01 2019 From: dieter at handshake.de (dieter) Date: Fri, 15 Feb 2019 08:02:01 +0100 Subject: Problem : Generator References: Message-ID: <87o97d4jrq.fsf@handshake.de> Prahallad Achar writes: > How to implement reverse generator A generator generates a sequence of values. The notion "reverse generator" suggests that you have a sequence of values and want to produce it in reverse order. This is not always possible. Consider: def natural(): i = 0 while True: yield i; i += 1 This generator generates all natural numbers (up to the memory limit). However, there is no "reverse generator" for the sequence of natural numbers. If you have a finate sequence of values, you can turn it into a list, reverse this list and iterate over it. This will give you a reverse generator for your sequence of values (though maybe an inefficient one). From rosuav at gmail.com Fri Feb 15 02:13:17 2019 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 15 Feb 2019 18:13:17 +1100 Subject: FW: Why float('Nan') == float('Nan') is False In-Reply-To: <000201d4c4ed$7636e7b0$62a4b710$@verizon.net> References: <5c6419bf$0$6565$426a74cc@news.free.fr> <5c651200$0$21611$426a34cc@news.free.fr> <008e01d4c4b7$92c4d760$b84e8620$@verizon.net> <00dd01d4c4df$a1775350$e465f9f0$@verizon.net> <000201d4c4ed$7636e7b0$62a4b710$@verizon.net> Message-ID: On Fri, Feb 15, 2019 at 4:15 PM Avi Gross wrote: > > > You shouldn't be testing floats for identity. > > I am not suggesting anyone compare floats. I repeat that a nan is not > anything. Now as a technicality, it is considered a float by the type > command as there is no easy way to make an int that is a nan: You've been working with float("nan") all this time. It is, big surprise, a float. This is not a technicality. It *is* a float. > Now for a deeper anomaly and please don't tell me I shouldn't do this. Actually... you're back to comparing floats by identity. So you shouldn't do this, apart from probing the interpreter itself. This is not anomalous, it's just the way that Python's immutable types work. > >>> nanfloat1 = float("nan") > >>> nanfloat2 = float("nan") > >>> nanmath1 = math.nan > >>> nanmath2 = math.nan > >>> nannumpy1 = numpy.nan > >>> nannumpy2 = numpy.nan > >>> nanfloat1 is nanfloat2 > False > >>> nanmath1 is nanmath2 > True > >>> nannumpy1 is nannumpy2 > True > >>> nanfloat1 is nanmath1 > False > >>> nanfloat1 is nannumpy1 > False > >>> nanmath1 is nannumpy1 > False > > This seems a tad inconsistent but perhaps completely understandable. Yet all > three claim to float ... > > >>> list(map(type, [ nanfloat1, nanmath1, nannumpy1 ] )) > [, , ] Well... yes. Every one of them IS a float, and every one of them DOES carry the value of "nan". and they're not identical. So? You can do the same with other values of floats, too. > Now granted comparing floats is iffy if the floats are computed and often > fails because of the internal bit representation and rounding. But asking if > a copy of a float variable to a new name points to the same internal does > work: Nope, this is nothing to do with rounding. > What I see happening here is that math.nan is a real object of some sorts > that is instantiated by the math module at a specific location and > presumable setting anything to it just copies that, sort of. Okay, I think I see the problem here. You're expecting Python objects to have locations (they don't, but they have identities) and to be copied (they aren't, they're referenced), and you're expecting nan to not be a value (it is). Python's object model demands that math.nan be a real object. Otherwise you wouldn't be able to do anything at all with it. > >>> id(math.nan) > 51774064 > > Oddly, every copy of it gets another address but the same other address > which hints at some indirection in the way it was set up. > > >>> m = math.nan > >>> id(m) > 51774064 > >>> n = math.nan > >>> id(n) > 51774064 > >>> o = m > >>> id(o) > 51774064 That isn't an address, it's an integer representing the object's identity. And you could do this with literally ANY Python object. That is the entire definition of assignment in Python. When you assign an expression to a name, and then look up the object via that name, you get... that object. That is how most modern high level languages work. > Now do the same for the numpy.nan implementation: > This time that same address is reused: > > >>> m = numpy.nan > >>> id(m) > 57329632 > >>> n = numpy.nan > >>> id(n) > 57329632 > > So the numpy nan is unique. The math nan is something else but confusingly > generates a new but same copy. You may be getting the address o f a proxy > one time and the real one another. No, numpy.nan and math.nan behave exactly the same way. They are distinct objects in the versions of Python and numpy that you're using, although other versions would be legitimately able to reuse the same object if they chose. Everything you do with assignment is going to behave the same way. No matter what name you assign that object to, it's the same object, and has the same ID. > >>> m is n > True > > But > > >>> m is math.nan > False > > Should I give up? No, the above makes some sense as the id() function shows > there ware two addresses involved in one case and not the other. Not addresses, identities, and yes, there are two distinct objects here. > A truly clean implementation might have one copy system-wide as happens with > None or Ellipsis (...) but it seems the development in python went in > multiple directions and is no longer joined. True, and an equally clean implementation could guarantee that two equal strings are stored at the same place in memory. Some languages guarantee this. Others don't. Python doesn't make this guarantee, and CPython doesn't behave that way, but it'd be perfectly valid for a Python implementation to do exactly this. There isn't much benefit in mandating this for floats, though; they don't take up much space. > A similar test (not shown) with numpy.nan shows the m and n above are each > other as well as what they copied because they share an ID. > > The solution is to NOT look at nan except using the appropriate functions. > > >>> [ (math.isnan(nothing), numpy.isnan(nothing)) > for nothing in [ float("nan"), math.nan, numpy.nan ] ] > > [(True, True), (True, True), (True, True)] Well... yes. That's what I meant when I said you shouldn't be comparing floats for identity. You shouldn't ask if this float IS that float, you should ask if this one is a nan. > It seems that at least those two nan checkers work the same on all Not A > Number variants I have tried. So seems safe to stick with it. I would hope so. That's their job. They look at the *value*, not the identity, and tell you whether or not it is a nan. Do you understand now what I meant when I said you shouldn't be comparing floats for identity? If not, research Python's object model and get a clearer understanding of identity vs value. ChrisA From greg.ewing at canterbury.ac.nz Fri Feb 15 02:27:34 2019 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 15 Feb 2019 20:27:34 +1300 Subject: Convert a list with wrong encoding to utf8 In-Reply-To: <193fee5d-fee4-4a6f-9e0c-8cade01b1461@googlegroups.com> References: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> <60648959-b1f1-4db6-94b7-458a31cad008@googlegroups.com> <72f4ed24-c871-702d-efbb-18554c91da3d@mrabarnett.plus.com> <193fee5d-fee4-4a6f-9e0c-8cade01b1461@googlegroups.com> Message-ID: vergos.nikolas at gmail.com wrote: > I just tried: > > names = tuple( [s.encode('latin1').decode('utf8') for s in names] ) > > but i get > UnicodeEncodeError('latin-1', '???? ???????', 0, 4, 'ordinal not in range(256)') This suggests that the string you're getting from the database *has* already been correctly decoded, and there is no need to go through the latin1 re-coding step. What do you get if you do print(names) immediately *before* trying to re-code them? What *may* be happening is that most of your data is stored in the database encoded as utf-8, but some of it is actually using a different encoding, and you're getting confused by the resulting inconsistencies. I suggest you look carefully at *all* the names in the list, straight after getting them from the database. If some of them look okay and some of them look like mojibake, then you have bad data in the database in the form of inconsistent encodings. -- Greg From acharbly at gmail.com Fri Feb 15 02:55:23 2019 From: acharbly at gmail.com (Prahallad Achar) Date: Fri, 15 Feb 2019 13:25:23 +0530 Subject: Problem : Generator In-Reply-To: <87o97d4jrq.fsf@handshake.de> References: <87o97d4jrq.fsf@handshake.de> Message-ID: How about this List1=[ 1,2,3,4] Rever_gen = ( x*x for x in list1, reversed = True) Rever_gen gets generator object and iterating it now gets reverse order.. Am I correct here? Suggest me On Fri, 15 Feb 2019, 12:33 dieter Prahallad Achar writes: > > How to implement reverse generator > > A generator generates a sequence of values. > > The notion "reverse generator" suggests that you have a sequence > of values and want to produce it in reverse order. > This is not always possible. > > Consider: > def natural(): > i = 0 > while True: yield i; i += 1 > > This generator generates all natural numbers (up to the memory limit). > However, there is no "reverse generator" for the sequence of natural > numbers. > > If you have a finate sequence of values, you can turn it into > a list, reverse this list and iterate over it. This will give > you a reverse generator for your sequence of values (though maybe > an inefficient one). > > -- > https://mail.python.org/mailman/listinfo/python-list > From greg.ewing at canterbury.ac.nz Fri Feb 15 03:08:18 2019 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 15 Feb 2019 21:08:18 +1300 Subject: Convert a list with wrong encoding to utf8 In-Reply-To: <33474eb5-8230-4c77-b6e5-8a35207ff73d@googlegroups.com> References: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> <60648959-b1f1-4db6-94b7-458a31cad008@googlegroups.com> <72f4ed24-c871-702d-efbb-18554c91da3d@mrabarnett.plus.com> <193fee5d-fee4-4a6f-9e0c-8cade01b1461@googlegroups.com> <33474eb5-8230-4c77-b6e5-8a35207ff73d@googlegroups.com> Message-ID: vergos.nikolas at gmail.com wrote: > [python] con = pymysql.connect( db = 'clientele', user = 'vergos', passwd = > '******', charset = 'utf8' ) cur = con.cursor() [/python] > > From that i understand that the names being fetched from the db to pyhton > script are being fetced as utf8, right? No, I don't think so. As far as I can tell from a brief reading of the MySQL docs, that only sets the *connection* encoding, which is concerned with transferring data over the connection between the client and the server. It has no bearing on the encoding used to decode data fetched from the database. That's determined by metadata stored in the database itself. It seems that MySQL lets you specify database encodings at three different levels: for the database as a whole, for a specific table, and for a specific field of a table. What I think is happening is that the column you're reading the names from is tagged in the database as being encoded in latin1, *but* this is incorrect for some of the names, which are actually encoded in utf8. This would explain why some of the data you looked at was printed with hex escapes, and why name.encode('latin1').decode('utf8') appeared to fix it. The encode('latin1') gets back the original raw bytes, and the decode('utf8') decodes them again using the correct encoding. However, not *all* of the data is like this -- some of it is correctly stored in the database, and is coming back already correctly decoded with no further processing needed. So, when you blindly try to re-code all the names in the list, it fails on the first correctly-decoded one it encounters. Again, try printing out the whole list of names, and post it here (or a good chunk of it if it's very long). It will give us a better idea of what's going on. If this theory is correct, then there isn't really any "right" way to deal with it -- the fundamental problem is that the data in the database is corrupted. The best long-term solution would be to clean up the database. But if you have to deal with it as it is, you'll need to use some kind of heuristic to decide when a particular string needs "fixing", and what needs to be done to fix it. -- Greg From rosuav at gmail.com Fri Feb 15 03:25:58 2019 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 15 Feb 2019 19:25:58 +1100 Subject: Problem : Generator In-Reply-To: References: <87o97d4jrq.fsf@handshake.de> Message-ID: On Fri, Feb 15, 2019 at 6:57 PM Prahallad Achar wrote: > > How about this > List1=[ 1,2,3,4] > Rever_gen = ( x*x for x in list1, reversed = True) > > Rever_gen gets generator object and iterating it now gets reverse order.. > > Am I correct here? Suggest me > How about reversed(list1) ? ChrisA From greg.ewing at canterbury.ac.nz Fri Feb 15 03:50:34 2019 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Fri, 15 Feb 2019 21:50:34 +1300 Subject: FW: Why float('Nan') == float('Nan') is False In-Reply-To: References: <5c6419bf$0$6565$426a74cc@news.free.fr> <5c651200$0$21611$426a34cc@news.free.fr> <008e01d4c4b7$92c4d760$b84e8620$@verizon.net> <00dd01d4c4df$a1775350$e465f9f0$@verizon.net> Message-ID: Avi Gross wrote: > I can see why you may be wondering. You see the nan concept as having a > specific spelling using all lowercase and to an extent you are right. No, he's talking about this particular line from the transcript you posted: >>>float(" nan") > Nan This suggests that the interpreter printed out that particular nan value as "Nan" with a capital N. But that's not what my Python 3.5.1 interpreter does: Python 3.5.1 (default, Jun 1 2016, 13:15:26) [GCC 4.2.1 (Apple Inc. build 5664)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> float(" nan") nan Grant was asking whether that's *really* what your interpreter printed out, and if so, which version of Python it was, because it's quite a surprising thing for it to do. Personally I think it's more likely that the N got capitalised somehow on the way from your terminal window to the mail message. -- Greg From acharbly at gmail.com Fri Feb 15 04:27:08 2019 From: acharbly at gmail.com (Prahallad Achar) Date: Fri, 15 Feb 2019 14:57:08 +0530 Subject: Problem : Generator In-Reply-To: References: <87o97d4jrq.fsf@handshake.de> Message-ID: I get list object instead gen obj On Fri, 15 Feb 2019, 13:57 Chris Angelico On Fri, Feb 15, 2019 at 6:57 PM Prahallad Achar > wrote: > > > > How about this > > List1=[ 1,2,3,4] > > Rever_gen = ( x*x for x in list1, reversed = True) > > > > Rever_gen gets generator object and iterating it now gets reverse order.. > > > > Am I correct here? Suggest me > > > > How about reversed(list1) ? > > ChrisA > -- > https://mail.python.org/mailman/listinfo/python-list > From jfong at ms4.hinet.net Fri Feb 15 05:58:25 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Fri, 15 Feb 2019 02:58:25 -0800 (PST) Subject: Problem : Generator In-Reply-To: References: <87o97d4jrq.fsf@handshake.de> Message-ID: <019d068d-0263-41e3-bc09-1d6e9a4812d8@googlegroups.com> Prahallad Achar? 2019?2?15???? UTC+8??5?27?36???? > I get list object instead gen obj > > On Fri, 15 Feb 2019, 13:57 Chris Angelico > > On Fri, Feb 15, 2019 at 6:57 PM Prahallad Achar > > wrote: > > > > > > How about this > > > List1=[ 1,2,3,4] > > > Rever_gen = ( x*x for x in list1, reversed = True) > > > > > > Rever_gen gets generator object and iterating it now gets reverse order.. > > > > > > Am I correct here? Suggest me > > > > > > > How about reversed(list1) ? > > > > ChrisA > > -- > > https://mail.python.org/mailman/listinfo/python-list > > Rever_gen = ( x*x for x in reversed(list1)) From grant.b.edwards at gmail.com Fri Feb 15 11:56:11 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Fri, 15 Feb 2019 16:56:11 -0000 (UTC) Subject: What's up with Activestate Python? References: Message-ID: On 2019-02-13, Grant Edwards wrote: > For many, many years I've always installed ActiveState's ActivePython > Community edition when forced to use Windows. It has always included > all of the "extra" libraries that I didn't wan't to install (or > couldn't because I didn't have a C compiler for Windows). > > I recently decided to upgrade my Win7 machine from ActivePython 3.5.4 > to 3.6. > > ... and all of apps stopped working. ActivePython 3.6 appears to be a > minimal install that includes nothing but CPython. Comparing the > download sizes makes this obvious: > > -rw-r--r-- 1 grante users 223056832 Mar 26 2018 ActivePython-2.7.14.2717-win64-x64-404905.exe > -rw-r--r-- 1 grante users 225065576 May 29 2018 ActivePython-3.5.4.3504-win64-x64-404899.exe > -rw-r--r-- 1 grante users 30297136 Feb 13 16:28 ActivePython-3.6.0.3600-win64-x64-401834.exe > > I've searched the ActiveState web site, and the fact that they've > stopped including "extra" libraries doesn't seem to be documented > anywhere. FWIW, it's just the 3.6.0 release that was minimal. The 3.6.6 releases contain the normal set of libraries. However, the 3.6.6 release for Windows isn't quite out yet... -- Grant Edwards grant.b.edwards Yow! Thank god!! ... It's at HENNY YOUNGMAN!! gmail.com From piet-l at vanoostrum.org Fri Feb 15 14:46:39 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Fri, 15 Feb 2019 20:46:39 +0100 Subject: Convert a list with wrong encoding to utf8 References: <00d35954-a1a4-44a3-aa96-adad34f1294c@googlegroups.com> <60648959-b1f1-4db6-94b7-458a31cad008@googlegroups.com> <72f4ed24-c871-702d-efbb-18554c91da3d@mrabarnett.plus.com> <193fee5d-fee4-4a6f-9e0c-8cade01b1461@googlegroups.com> Message-ID: vergos.nikolas at gmail.com writes: > ?? ??????, 14 ??????????? 2019 - 8:56:31 ?.?. UTC+2, ? ??????? MRAB ??????: > >> It doesn't have a 'b' prefix, so either it's Python 2 or it's a Unicode >> string that was decoded wrongly from the bytes. > > Yes it doesnt have the 'b' prefix so that hexadecimal are representation of strings and not representation of bytes. > > I just tried: > > names = tuple( [s.encode('latin1').decode('utf8') for s in names] ) > > but i get > UnicodeEncodeError('latin-1', '???? ???????', 0, 4, 'ordinal not in range(256)') > > '???? ???????' is a valid name but even so it gives an error. > > Is it possible that Python3 a Unicode had the string wrongly decoded from the bytes ? > > What can i do to get the names?! python3 >>> x = '\xce\x86\xce\xba\xce\xb7\xcf\x82 \xce\xa4\xcf\x83\xce\xb9\xce\xac\xce\xbc\xce\xb7\xcf\x82' >>> b = bytes(ord(c) for c in x) >>> b.decode('utf-8') '???? ???????' >>> -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From avigross at verizon.net Fri Feb 15 19:53:43 2019 From: avigross at verizon.net (Avi Gross) Date: Fri, 15 Feb 2019 19:53:43 -0500 Subject: FW: Why float('Nan') == float('Nan') is False In-Reply-To: References: <5c6419bf$0$6565$426a74cc@news.free.fr> <5c651200$0$21611$426a34cc@news.free.fr> <008e01d4c4b7$92c4d760$b84e8620$@verizon.net> <00dd01d4c4df$a1775350$e465f9f0$@verizon.net> Message-ID: <00be01d4c592$10f202e0$32d608a0$@verizon.net> Greg, Good eye. You are correct! Yes, that is a side effect I did not intend when I cut and paste and the darn spell-checker saw it as useful to make my code act like the start of a normal text sentence. I just replicated it: >>> float(" nan") Nan As I watched, "nan" went to "Nan" So, indeed, the transcript lied. I will be more careful. -----Original Message----- From: Python-list On Behalf Of Gregory Ewing Sent: Friday, February 15, 2019 3:51 AM To: python-list at python.org Subject: Re: FW: Why float('Nan') == float('Nan') is False Avi Gross wrote: > I can see why you may be wondering. You see the nan concept as having > a specific spelling using all lowercase and to an extent you are right. No, he's talking about this particular line from the transcript you posted: >>>float(" nan") > Nan This suggests that the interpreter printed out that particular nan value as "Nan" with a capital N. But that's not what my Python 3.5.1 interpreter does: Python 3.5.1 (default, Jun 1 2016, 13:15:26) [GCC 4.2.1 (Apple Inc. build 5664)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> float(" nan") nan Grant was asking whether that's *really* what your interpreter printed out, and if so, which version of Python it was, because it's quite a surprising thing for it to do. Personally I think it's more likely that the N got capitalised somehow on the way from your terminal window to the mail message. -- Greg -- https://mail.python.org/mailman/listinfo/python-list From dieter at handshake.de Sat Feb 16 01:47:02 2019 From: dieter at handshake.de (dieter) Date: Sat, 16 Feb 2019 07:47:02 +0100 Subject: Problem : Generator References: <87o97d4jrq.fsf@handshake.de> Message-ID: <87r2c8kz6h.fsf@handshake.de> Prahallad Achar writes: > I get list object instead gen obj If you have a list "l" and want a generator, you can use ( x for x in l) or simpler "iter(l)" - which gives you an interator over "l". An "iterator" is slightly more general than a generator (every generator is also an iterator). From avigross at verizon.net Sat Feb 16 02:23:28 2019 From: avigross at verizon.net (Avi Gross) Date: Sat, 16 Feb 2019 02:23:28 -0500 Subject: Problem : Generator In-Reply-To: <87r2c8kz6h.fsf@handshake.de> References: <87o97d4jrq.fsf@handshake.de> <87r2c8kz6h.fsf@handshake.de> Message-ID: <000a01d4c5c8$83946aa0$8abd3fe0$@verizon.net> Just want to point out you can make any function into a generator by having a yield statement like this: >>> def previous(listing): while listing: yield listing.pop() >>> for num in previous([1,2,3,4]): print(num) 4 3 2 1 The above is an EXAMPLE, not a particularly great way to do this. The point is if you have an iterable you want to do reversed, you could do it without an explicit reversal. Variations on the above that do not alter the list would be to use an index based on the length of the list and count backward as you return what is at that index. -----Original Message----- From: Python-list On Behalf Of dieter Sent: Saturday, February 16, 2019 1:47 AM To: python-list at python.org Subject: Re: Problem : Generator Prahallad Achar writes: > I get list object instead gen obj If you have a list "l" and want a generator, you can use ( x for x in l) or simpler "iter(l)" - which gives you an interator over "l". An "iterator" is slightly more general than a generator (every generator is also an iterator). -- https://mail.python.org/mailman/listinfo/python-list From acharbly at gmail.com Sat Feb 16 03:04:31 2019 From: acharbly at gmail.com (Prahallad Achar) Date: Sat, 16 Feb 2019 13:34:31 +0530 Subject: Problem : Generator In-Reply-To: <000a01d4c5c8$83946aa0$8abd3fe0$@verizon.net> References: <87o97d4jrq.fsf@handshake.de> <87r2c8kz6h.fsf@handshake.de> <000a01d4c5c8$83946aa0$8abd3fe0$@verizon.net> Message-ID: Woww.. This solves the problem.. Thank you very much On Sat, 16 Feb 2019, 12:54 Avi Gross Just want to point out you can make any function into a generator by having > a yield statement like this: > > >>> def previous(listing): > while listing: yield listing.pop() > > > >>> for num in previous([1,2,3,4]): print(num) > > 4 > 3 > 2 > 1 > > The above is an EXAMPLE, not a particularly great way to do this. The point > is if you have an iterable you want to do reversed, you could do it without > an explicit reversal. Variations on the above that do not alter the list > would be to use an index based on the length of the list and count backward > as you return what is at that index. > > -----Original Message----- > From: Python-list On > Behalf Of dieter > Sent: Saturday, February 16, 2019 1:47 AM > To: python-list at python.org > Subject: Re: Problem : Generator > > Prahallad Achar writes: > > > I get list object instead gen obj > > If you have a list "l" and want a generator, you can use > ( x for x in l) > or simpler "iter(l)" - which gives you an interator over "l". > > An "iterator" is slightly more general than a generator (every generator is > also an iterator). > > > > > -- > https://mail.python.org/mailman/listinfo/python-list > > -- > https://mail.python.org/mailman/listinfo/python-list > From acharbly at gmail.com Sat Feb 16 03:04:48 2019 From: acharbly at gmail.com (Prahallad Achar) Date: Sat, 16 Feb 2019 13:34:48 +0530 Subject: Problem : Generator In-Reply-To: <87r2c8kz6h.fsf@handshake.de> References: <87o97d4jrq.fsf@handshake.de> <87r2c8kz6h.fsf@handshake.de> Message-ID: Yupee.. Thanks for the knowledge sharing. Regards Prahallad On Sat, 16 Feb 2019, 12:18 dieter Prahallad Achar writes: > > > I get list object instead gen obj > > If you have a list "l" and want a generator, you can use > ( x for x in l) > or simpler "iter(l)" - which gives you an interator over "l". > > An "iterator" is slightly more general than a generator > (every generator is also an iterator). > > > > > -- > https://mail.python.org/mailman/listinfo/python-list > From barry at barrys-emacs.org Sat Feb 16 03:35:05 2019 From: barry at barrys-emacs.org (Barry) Date: Sat, 16 Feb 2019 08:35:05 +0000 Subject: more pythonic way In-Reply-To: <002001d4c244$78b92030$6a2b6090$@epepm.cupet.cu> References: <000a01d4c240$2e4a3e20$8adeba60$@epepm.cupet.cu> <002001d4c244$78b92030$6a2b6090$@epepm.cupet.cu> Message-ID: On 11 Feb 2019, at 20:00, Felix Lazaro Carbonell wrote: >> The most pythonic way is to do this: >> >> def find_monthly_expenses(month=datetime.date.today().month, > year=datetime.date.today().year): >> ... This has subtle bugs. The default is calculated at import time and not at function call time. Also the defaults for month and year are not calculated at the same moment in time. Import at 23:59:59.99 on 31 december 2018 and you might get month as 12 and year as 2019. Run this example to see the problem: import datetime import time def test_defaults(prefix, now=datetime.datetime.now()): print( prefix, now ) test_defaults('one') time.sleep( 1 ) test_defaults('two') time.sleep( 1 ) test_defaults('three', datetime.datetime.now()) Which outputs: one 2019-02-16 08:30:54.819466 two 2019-02-16 08:30:54.819466 three 2019-02-16 08:30:56.821453 Notice that the time for 'one' and 'two' is the same. Only the overridden default chanse. Barry From grant.b.edwards at gmail.com Sat Feb 16 08:33:37 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Sat, 16 Feb 2019 13:33:37 -0000 (UTC) Subject: more pythonic way References: <000a01d4c240$2e4a3e20$8adeba60$@epepm.cupet.cu> <002001d4c244$78b92030$6a2b6090$@epepm.cupet.cu> Message-ID: On 2019-02-16, Barry wrote: > On 11 Feb 2019, at 20:00, Felix Lazaro Carbonell wrote: > >>> The most pythonic way is to do this: >>> >>> def find_monthly_expenses(month=datetime.date.today().month, >> year=datetime.date.today().year): >>> ... > > This has subtle bugs. > The default is calculated at import time and not at function call time. Indeed. That was the entire point of that posting: it was an example of a not-uncommon misunderstanding about when intializer values are determined. Unfortunately the subsequent remark about initializers not working that way has been lost in the shuffle. > Also the defaults for month and year are not calculated at the same > moment in time. Yep. The race condition between the evaluation of the two initializer expressions is a bit more subtle than the fact that the evaluations only happen once at compile time. -- Grant From cseberino at gmail.com Sat Feb 16 15:22:31 2019 From: cseberino at gmail.com (Christian Seberino) Date: Sat, 16 Feb 2019 12:22:31 -0800 (PST) Subject: Why this curl command works in shell but NOT when I do subprocess.getoutput(["curl", .....]) ?? Message-ID: <2af4c686-8432-4b9b-a3b3-77a240d98294@googlegroups.com> Why this curl command works in shell but NOT when I use subprocess as in below?..... UL_URL = "https://auphonic.com/api/simple/productions.json" ul_output = subprocess.getoutput(["curl", "-X", "POST", UL_URL, "-u", "seberino:XXXXXXX", "-F", "input_file=@" + sys.argv[1], "-F", "denoise=true", "-F", "action=start"]) There is some difference in how shell versus Python process the info. Thanks, cs From cseberino at gmail.com Sat Feb 16 15:50:34 2019 From: cseberino at gmail.com (Christian Seberino) Date: Sat, 16 Feb 2019 12:50:34 -0800 (PST) Subject: Why this curl command works in shell but NOT when I do subprocess.getoutput(["curl", .....]) ?? In-Reply-To: <2af4c686-8432-4b9b-a3b3-77a240d98294@googlegroups.com> References: <2af4c686-8432-4b9b-a3b3-77a240d98294@googlegroups.com> Message-ID: <221c08a1-df31-4e16-946f-5fa3dcbbce35@googlegroups.com> Nevermind...appears to get arguments like this you need to use subprocess.run rather than subprocess.getoutput (with capture_output = True). cs From benjamin at python.org Sat Feb 16 20:10:48 2019 From: benjamin at python.org (Benjamin Peterson) Date: Sat, 16 Feb 2019 20:10:48 -0500 Subject: [RELEASE] Python 2.7.16 release candidate 1 Message-ID: <715ded96-a728-42ec-8bb7-72c3f7e1695b@www.fastmail.com> I'm pleased to announce the immediate availability of Python 2.7.16 release candidate 1. This is a prerelease for yet another bug fix release in the Python 2.7.x series. It includes over 100 fixes over Python 2.7.15. See the changelog at https://raw.githubusercontent.com/python/cpython/baacaac06f93dd624c9d7b3bac0e13fbe34f2d8c/Misc/NEWS.d/2.7.16rc1.rst for full details. Downloads are at: https://www.python.org/downloads/release/python-2716rc1/ Please test your software against the new release and report any issues to https://bugs.python.org/ If all goes according to plan, Python 2.7.16 final will be released on March 2. All the best, Benjamin From shakti.shrivastava13 at gmail.com Sat Feb 16 21:23:58 2019 From: shakti.shrivastava13 at gmail.com (Shakti Kumar) Date: Sun, 17 Feb 2019 07:53:58 +0530 Subject: [RELEASE] Python 2.7.16 release candidate 1 In-Reply-To: <715ded96-a728-42ec-8bb7-72c3f7e1695b@www.fastmail.com> References: <715ded96-a728-42ec-8bb7-72c3f7e1695b@www.fastmail.com> Message-ID: On Sun, 17 Feb 2019 at 6:44 AM Benjamin Peterson wrote: > I'm pleased to announce the immediate availability of Python 2.7.16 > release candidate 1. This is a prerelease for yet another bug fix release > in the Python 2.7.x series. It includes over 100 fixes over Python 2.7.15. > See the changelog at > https://raw.githubusercontent.com/python/cpython/baacaac06f93dd624c9d7b3bac0e13fbe34f2d8c/Misc/NEWS.d/2.7.16rc1.rst (Clipped for brevity) > > > If all goes according to plan, Python 2.7.16 final will be released on > March 2. > Just a small doubt (out of curiosity), python 2.7 is nearing end of support right? -- Sent from Shakti?s iPhone From python at mrabarnett.plus.com Sat Feb 16 22:11:45 2019 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 17 Feb 2019 03:11:45 +0000 Subject: [RELEASE] Python 2.7.16 release candidate 1 In-Reply-To: References: <715ded96-a728-42ec-8bb7-72c3f7e1695b@www.fastmail.com> Message-ID: <29ce4b01-4b8b-1ba5-7662-fa9d138a94a5@mrabarnett.plus.com> On 2019-02-17 02:23, Shakti Kumar wrote: > On Sun, 17 Feb 2019 at 6:44 AM Benjamin Peterson > wrote: > >> I'm pleased to announce the immediate availability of Python 2.7.16 >> release candidate 1. This is a prerelease for yet another bug fix release >> in the Python 2.7.x series. It includes over 100 fixes over Python 2.7.15. >> See the changelog at > > >> https://raw.githubusercontent.com/python/cpython/baacaac06f93dd624c9d7b3bac0e13fbe34f2d8c/Misc/NEWS.d/2.7.16rc1.rst > > (Clipped for brevity) > >> >> >> If all goes according to plan, Python 2.7.16 final will be released on >> March 2. > > >> Just a small doubt (out of curiosity), python 2.7 is nearing end of > support right? > Support by the Python Software Foundation for Python 2.7 will end on 1 January 2020. From jaybraun2.0 at gmail.com Sat Feb 16 23:53:00 2019 From: jaybraun2.0 at gmail.com (jaybraun2.0 at gmail.com) Date: Sat, 16 Feb 2019 20:53:00 -0800 (PST) Subject: flask problem Message-ID: <44226626-55d7-40d6-ae61-0663d4ac9729@googlegroups.com> I have used flask before with no problem. Now I am getting very mysterious results, which I have reduced to a very small test case. Here is a flask server, in Python 3, which waits for a message on port 8002 (the port is open): from flask import Flask, Response import json app = Flask(__name__) @app.route('/jay', methods=['PUT']) def analyze(): print("hello") resp = Response(json.dumps({'status':'Good'}), status=200, mimetype='application/json') return resp if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=8002) And here is the simple client: import requests r = requests.put('http://13.57.96.64:8002/jay', data='{ "test": "Jay" }', headers={"Content-Type": "application/json"}, verify=False) print(r) I bring up the server, and as expected, it outputs: * Running on http://0.0.0.0:8002/ (Press CTRL+C to quit) * Restarting with stat * Debugger is active! * Debugger PIN: 292-073-960 and waits. Then I run the client and see: So the connection is fine. But the server does not print to the terminal. (In my actual application, it needs to do much more, but does nothing.) Without the server running, the client fails to make a connection, as expected. So the only effect of the server is to create the REST endpoint -- but it does not execute the logic that a message is supposed to trigger. Hopefully, another pair of eyes will see the problem. Thanks. Jay From tjreedy at udel.edu Sun Feb 17 01:56:27 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 17 Feb 2019 01:56:27 -0500 Subject: [RELEASE] Python 2.7.16 release candidate 1 In-Reply-To: <715ded96-a728-42ec-8bb7-72c3f7e1695b@www.fastmail.com> References: <715ded96-a728-42ec-8bb7-72c3f7e1695b@www.fastmail.com> Message-ID: On 2/16/2019 8:10 PM, Benjamin Peterson wrote: > I'm pleased to announce the immediate availability of Python 2.7.16 release candidate 1. This is a prerelease for yet another bug fix release in the Python 2.7.x series. It includes over 100 fixes over Python 2.7.15. See the changelog at > > https://raw.githubusercontent.com/python/cpython/baacaac06f93dd624c9d7b3bac0e13fbe34f2d8c/Misc/NEWS.d/2.7.16rc1.rst > > for full details. > > Downloads are at: > > https://www.python.org/downloads/release/python-2716rc1/ > > Please test your software against the new release Something changed in either Windows or the Windows installer. Since I just upgraded to 1809, I suspect the former. As a result, I got this unfriendly message. " Windows Defender SmartScreen prevented an unrecognized app from starting. Running this app might put your PC at risk. App: python-2.7.16rc1.amd64.msi " Open an issue? -- Terry Jan Reedy From acharbly at gmail.com Sun Feb 17 04:52:01 2019 From: acharbly at gmail.com (Prahallad Achar) Date: Sun, 17 Feb 2019 15:22:01 +0530 Subject: Python threading - event Message-ID: Hello Friends, I got an requirement as stated below, 1. main thread will be running and other event should run parallel In more detail One function will be keep dumping data and other event function should trigger at some event but dumping data should be keep running. Sorry, I can not give any example as I couldn't come up with solution hence am writing here. Kindly support. Regards Prahallad From gabriele1NOSPAM at hotmail.com Sun Feb 17 11:22:08 2019 From: gabriele1NOSPAM at hotmail.com (^Bart) Date: Sun, 17 Feb 2019 17:22:08 +0100 Subject: Sum of few numbers by using for and range Message-ID: Hello! I need to do what I wrote in the subject but... I don't understand how could I fix my code... :\ number1 = int( input("Insert the first number:")) number2 = int( input("Insert the second number:")) number3 = int( input("Insert the third number:")) print("Total amount is:") for x in range(number1,number3): y = x+x print(y) Regards. ^Bart From rosuav at gmail.com Sun Feb 17 11:30:01 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 18 Feb 2019 03:30:01 +1100 Subject: Sum of few numbers by using for and range In-Reply-To: References: Message-ID: On Mon, Feb 18, 2019 at 3:26 AM ^Bart wrote: > > Hello! > > I need to do what I wrote in the subject but... I don't understand how > could I fix my code... :\ > > number1 = int( input("Insert the first number:")) > number2 = int( input("Insert the second number:")) > number3 = int( input("Insert the third number:")) > > print("Total amount is:") > > for x in range(number1,number3): > y = x+x > print(y) > When you want homework help, it is extremely useful to post the entire challenge you were given, not just a one-line summary. What are you actually expected to do? What are the restrictions? I suspect, from your last few posts, that you should not be taking particular challenges, but should instead go back to the basics of how Python works. Reread the earliest material in your course and make sure you truly understand what is going on. Try to piece together what a section of code is doing, step by step. Write down on a piece of paper what the variables are at each point in the program. Then, AFTER doing the work manually, run the code and see if you were right. ChrisA From python at mrabarnett.plus.com Sun Feb 17 12:29:50 2019 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 17 Feb 2019 17:29:50 +0000 Subject: Python threading - event In-Reply-To: References: Message-ID: On 2019-02-17 09:52, Prahallad Achar wrote: > Hello Friends, > I got an requirement as stated below, > > 1. main thread will be running and other event should run parallel > In more detail > One function will be keep dumping data and other event function should > trigger at some event but dumping data should be keep running. > > Sorry, I can not give any example as I couldn't come up with solution hence > am writing here. > Use a queue. It's defined in the queue module. From PythonList at DancesWithMice.info Sun Feb 17 14:10:40 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Mon, 18 Feb 2019 08:10:40 +1300 Subject: Sum of few numbers by using for and range In-Reply-To: References: Message-ID: <48b338bd-7f1a-03a7-9e3a-9611dd3ddef2@DancesWithMice.info> ^Bart, Which course are you attempting? What are you using as learning material? (it seems ineffectual) - further comments interspersed, below:- On 18/02/19 5:30 AM, Chris Angelico wrote: > On Mon, Feb 18, 2019 at 3:26 AM ^Bart wrote: >> I need to do what I wrote in the subject but... I don't understand how >> could I fix my code... :\ > When you want homework help, it is extremely useful to post the entire > challenge you were given, not just a one-line summary. What are you > actually expected to do? What are the restrictions? Also, are you aware that there is a separate email list "for folks who want to ask questions regarding how to learn computer programming with the Python language and its standard library."? (https://mail.python.org/mailman/listinfo/tutor) > I suspect, from your last few posts, that you should not be taking > particular challenges, but should instead go back to the basics of how > Python works. Reread the earliest material in your course and make +1 As explained earlier, asking helpful people 'here' to complete the task for you helps no-one! - you haven't really learned, and are unlikely to have mastered techniques necessary to pursue employment or hobby-interests, in future - you've asked community-minded folk to give-up their time - your trainer is unlikely to be fooled for long... > sure you truly understand what is going on. Try to piece together what > a section of code is doing, step by step. Write down on a piece of > paper what the variables are at each point in the program. Then, AFTER > doing the work manually, run the code and see if you were right. +1 I recently audited a set of courses (last one yet to be released) on Coursera, out of U.Mich (Michigan, USA): Python 3 Programming Specialization (https://www.coursera.org/specializations/python-3-programming). I'm not recommending it particularly - it doesn't fit with my personal approach (which may say more about me!) NB Coursera uses a 'freemium' business model. In my context "audit" meant 'evaluation', but in Coursera-jargon it means $free. Paying for such a course adds invitations to participate in project work, (?)final exam, and one hopes, earn a certificate. The reason this course caught my attention (and which is relevant to you, per Chris' and Dennis' recent advice) is that the course revolves around an 'active textbook'. This intersperses learning material with mastery exercises, and pertinently, uses a 'widget' which steps through code, line-by-line, showing exactly what is happening to each variable. I was impressed! In 'the good old days', we used to talk about having a 'paper computer'. In appearance, this was almost exactly like the above - where we could 'see' exactly (what we thought) was happening inside the CPU. It was a testing/debugging tool before the first line of code hit the Python (or whichever) interpreter/compiler. (who says Test-driven Development is a "new" idea?). In short, it is an excellent tool for visualising learning! NB There is a Python Debugger tool which operates similarly, but the learning tool is illustrative in nature, cf diagnostic. Having mentioned U.Mich/Coursera, one of the most famous Python courses available over-the-Internet is Dr Chuck's "Python for Everyone". I'm not aware if he has upgraded those to use the same active textbook tool. That said, it is an enormously-popular avenue for exploring Python! -- Regards =dn From rosuav at gmail.com Sun Feb 17 14:32:11 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 18 Feb 2019 06:32:11 +1100 Subject: Sum of few numbers by using for and range In-Reply-To: <48b338bd-7f1a-03a7-9e3a-9611dd3ddef2@DancesWithMice.info> References: <48b338bd-7f1a-03a7-9e3a-9611dd3ddef2@DancesWithMice.info> Message-ID: On Mon, Feb 18, 2019 at 6:12 AM DL Neil wrote: > > sure you truly understand what is going on. Try to piece together what > > a section of code is doing, step by step. Write down on a piece of > > paper what the variables are at each point in the program. Then, AFTER > > doing the work manually, run the code and see if you were right. > > +1 > > The reason this course caught my attention (and which is relevant to > you, per Chris' and Dennis' recent advice) is that the course revolves > around an 'active textbook'. This intersperses learning material with > mastery exercises, and pertinently, uses a 'widget' which steps through > code, line-by-line, showing exactly what is happening to each variable. > I was impressed! That sounds like an EXCELLENT way to do the second part - running the code to see if you were right. I would still recommend doing it 100% manually first, *writing down* your expectations, and only *then* letting the computer do it. It's easy to watch the computer do something and go "yes, of course that's what happens", but to still not be able to replicate it yourself. True comprehension means being able to predict what will happen. Consider it like a falsifiable hypothesis in scientific research. "I expect that, when I do X, Y, and Z, the result will be Q." Then you actually perform those steps, and see what the result is. Were you right? If not, how do you modify your expectations/hypothesis to correct it? It's the last step that is the most interesting, because that's where you truly learn. (And sometimes, that learning is expanding the corpus of human knowledge. It's only when you disprove your expectations that you can begin to pin down something like "oh so time flows at different rates depending on gravity" or "huh, so it turns out black-body radiation doesn't behave the way all the math said it would".) ChrisA From PythonList at DancesWithMice.info Sun Feb 17 14:48:51 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Mon, 18 Feb 2019 08:48:51 +1300 Subject: Sum of few numbers by using for and range In-Reply-To: References: <48b338bd-7f1a-03a7-9e3a-9611dd3ddef2@DancesWithMice.info> Message-ID: On 18/02/19 8:32 AM, Chris Angelico wrote: > On Mon, Feb 18, 2019 at 6:12 AM DL Neil wrote: >> The reason this course caught my attention (and which is relevant to >> you, per Chris' and Dennis' recent advice) is that the course revolves >> around an 'active textbook'. This intersperses learning material with >> mastery exercises, and pertinently, uses a 'widget' which steps through >> code, line-by-line, showing exactly what is happening to each variable. >> I was impressed! > > That sounds like an EXCELLENT way to do the second part - running the > code to see if you were right. I would still recommend doing it 100% > manually first, *writing down* your expectations, and only *then* > letting the computer do it. It's easy to watch the computer do > something and go "yes, of course that's what happens", but to still > not be able to replicate it yourself. True comprehension means being > able to predict what will happen. > > Consider it like a falsifiable hypothesis in scientific research. "I > expect that, when I do X, Y, and Z, the result will be Q." Then you > actually perform those steps, and see what the result is. Were you > right? If not, how do you modify your expectations/hypothesis to > correct it? It's the last step that is the most interesting, because > that's where you truly learn. (And sometimes, that learning is > expanding the corpus of human knowledge. It's only when you disprove > your expectations that you can begin to pin down something like "oh so > time flows at different rates depending on gravity" or "huh, so it > turns out black-body radiation doesn't behave the way all the math > said it would".) +1 For reference Chris (et al): The 'active textbook' offers 'windows' which seem to be Idle, but in a controlled and prescribed environment. So, the learner first writes his/her code, and only then can run and/or step-through, as described. +for ^Bart: IIRC the early stages of the U.Mich/Coursera Py3 Pgmg course included coverage of "accumulators", which concept you have yet to learn - and certainly lists and ranges appear. Thus dealing with both of the conceptual errors causing grief in the original code-snippet. All the best! -- Regards =dn From rshepard at appl-ecosys.com Sun Feb 17 16:32:00 2019 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Sun, 17 Feb 2019 13:32:00 -0800 (PST) Subject: Access control class? Message-ID: I'm developing an application which benefits from access control. By searching the web I've found a couple of data-entry-with-password classes yet I've no idea how to evaluate them for security. I'm looking for advice from those of you who have experience with access control. Thanks in advance, Rich From songbird at anthive.com Sun Feb 17 21:46:02 2019 From: songbird at anthive.com (songbird) Date: Sun, 17 Feb 2019 21:46:02 -0500 Subject: revisiting the "What am I running on?" question Message-ID: having worked on some other things for a while i didn't put much emphasis on working on this until i had the other bugs taken care of. so now back into it we can go... :) what i came up with (sorry, i hate yet another not invented here thing, but this is just where i ended up after some pondering). simply put. if i'm running on a computer and i don't easily know why kind of computer how can i answer this in a quick way without getting overly complicated that also will cover most of the easy cases? i came up with this: comments? additions? clarifications? i don't have a windows system to test this on, does it work? thanks :) ===== import re import tempfile def sysprobe (): sysprobetmp = tempfile.gettempdir() print ("Temp directory : -->" + sysprobetmp + "<--\n") result = re.search("^/(tmp)|(var)|(usr)|(opt)|(home)", sysprobetmp) try: print ("Result : -->" + result.group(0) + "<--\n") return ("posix") except: pass result = re.search("^[A-Za-z]:", sysprobetmp) try: print ("Result : -->" + result.group(0) + "<--\n") return ("windows") except: pass return ("unknown") def main (): print (sysprobe()) if __name__ == "__main__": main() ===== on my system i get: ===== (env) me at ant(39)~/src/salsa/bits/sysprobe$ python3 sysprobe.py Temp directory : -->/tmp<-- Result : -->/tmp<-- posix ===== songbird From songbird at anthive.com Sun Feb 17 22:08:46 2019 From: songbird at anthive.com (songbird) Date: Sun, 17 Feb 2019 22:08:46 -0500 Subject: revisiting the "What am I running on?" question References: Message-ID: songbird wrote: ... > result = re.search("^/(tmp)|(var)|(usr)|(opt)|(home)", sysprobetmp) i changed that line to: result = re.search("^/((tmp)|(var)|(usr)|(opt)|(home))", sysprobetmp) just to make sure the leading slash is not just grouped with the first string. i like to be more explicit with my precedence if i'm not sure what the language might actually do... :) songbird From 2QdxY4RzWzUUiLuE at potatochowder.com Sun Feb 17 22:10:20 2019 From: 2QdxY4RzWzUUiLuE at potatochowder.com (Dan Sommers) Date: Sun, 17 Feb 2019 21:10:20 -0600 Subject: revisiting the "What am I running on?" question In-Reply-To: References: Message-ID: <7d52f95b-66fd-9919-da2f-25208a57ea85@potatochowder.com> On 2/17/19 8:46 PM, songbird wrote: > simply put. if i'm running on a computer and i > don't easily know why kind of computer how can i > answer this in a quick way without getting overly > complicated that also will cover most of the easy > cases? > > i came up with this: > > comments? additions? clarifications? [...] > (env) me at ant(39)~/src/salsa/bits/sysprobe$ python3 sysprobe.py > Temp directory : -->/tmp<-- > > Result : -->/tmp<-- > > posix Python 3.7.2 (default, Jan 10 2019, 23:51:51) [GCC 8.2.1 20181127] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sys >>> sys.platform 'linux' From python at mrabarnett.plus.com Sun Feb 17 23:11:02 2019 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 18 Feb 2019 04:11:02 +0000 Subject: revisiting the "What am I running on?" question In-Reply-To: References: Message-ID: <42f0bf8c-ae4a-d983-0642-e472f2504ce7@mrabarnett.plus.com> On 2019-02-18 02:46, songbird wrote: > > having worked on some other things for a while i > didn't put much emphasis on working on this until i > had the other bugs taken care of. > > so now back into it we can go... :) > > what i came up with (sorry, i hate yet another not > invented here thing, but this is just where i ended up > after some pondering). > > simply put. if i'm running on a computer and i > don't easily know why kind of computer how can i > answer this in a quick way without getting overly > complicated that also will cover most of the easy > cases? > > i came up with this: > > comments? additions? clarifications? > > i don't have a windows system to test this on, > does it work? > > thanks :) > > > ===== > import re > import tempfile > > > def sysprobe (): > > sysprobetmp = tempfile.gettempdir() > > print ("Temp directory : -->" + sysprobetmp + "<--\n") > > result = re.search("^/(tmp)|(var)|(usr)|(opt)|(home)", sysprobetmp) > try: > print ("Result : -->" + result.group(0) + "<--\n") > > return ("posix") > except: > pass > > result = re.search("^[A-Za-z]:", sysprobetmp) > try: > print ("Result : -->" + result.group(0) + "<--\n") > return ("windows") > except: > pass > Don't use a bare except, it'll catch _any_ exception. If the regex matches, re.search will return a match object; if it doesn't match, it'll return None. In any case, +1 to Dan's answer. [snip] From tjol at tjol.eu Mon Feb 18 03:49:02 2019 From: tjol at tjol.eu (Thomas Jollans) Date: Mon, 18 Feb 2019 09:49:02 +0100 Subject: What's up with Activestate Python? In-Reply-To: References: Message-ID: <561e20ce-876e-1950-cc52-9781adf82ed1@tjol.eu> On 14/02/2019 19:10, Grant Edwards wrote: > On 2019-02-14, Liste guru wrote: >> Il 14/02/2019 00:06, Grant Edwards ha scritto: >>> For many, many years I've always installed ActiveState's ActivePython >>> Community edition when forced to use Windows. It has always included >>> ... >>> I guess it's time to switch to Anaconda or ??? >> >> I've also used the ActiveState python, expecially for the 2.7.x >> series, mainly for the oflline docs and the pywin32 libraries. >> >> Now the situation is better and with pip is really easy to have an >> updated python with a lot of libs so there is less need for the >> ActiveState distribution. > > How does that work for libraries that require a C compiler? Does pip > know how to download and install any required C/C++ toolchains? > Most extension modules on PyPI are distributed with binary wheels these days, so there's no need for a C compiler. This is certainly the case for the usual suspects (numpy etc). You probably won't have any trouble using the python.org version. In a pinch, the Microsoft compiler isn't hard to install (and free to download). Anaconda also has its moments, and has some packages that PyPI doesn't (for my use case, this is primarily PyQt5). I don't think I've used ActiveState since the Python 2.3 days, so I can't speak for that. From barry at barrys-emacs.org Mon Feb 18 04:21:49 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Mon, 18 Feb 2019 09:21:49 +0000 Subject: What's up with Activestate Python? In-Reply-To: <561e20ce-876e-1950-cc52-9781adf82ed1@tjol.eu> References: <561e20ce-876e-1950-cc52-9781adf82ed1@tjol.eu> Message-ID: <2E3FAC50-A3C5-4DAD-B1E7-417993F93AEA@barrys-emacs.org> > On 18 Feb 2019, at 08:49, Thomas Jollans wrote: > > Anaconda also has its moments, and has some packages that PyPI doesn't > (for my use case, this is primarily PyQt5). Odd I use PyQt5 from PyPI all the time and have for a few years now. Barry From tjol at tjol.eu Mon Feb 18 04:29:59 2019 From: tjol at tjol.eu (Thomas Jollans) Date: Mon, 18 Feb 2019 10:29:59 +0100 Subject: What's up with Activestate Python? In-Reply-To: <2E3FAC50-A3C5-4DAD-B1E7-417993F93AEA@barrys-emacs.org> References: <561e20ce-876e-1950-cc52-9781adf82ed1@tjol.eu> <2E3FAC50-A3C5-4DAD-B1E7-417993F93AEA@barrys-emacs.org> Message-ID: <9c683da4-2406-bbcd-7718-f9bf3026ebd9@tjol.eu> On 18/02/2019 10.21, Barry Scott wrote: > > >> On 18 Feb 2019, at 08:49, Thomas Jollans wrote: >> >> Anaconda also has its moments, and has some packages that PyPI doesn't >> (for my use case, this is primarily PyQt5). > > Odd I use PyQt5 from PyPI all the time and have for a few years now. It would appear that my information is outdated! From tjol at tjol.eu Mon Feb 18 04:29:59 2019 From: tjol at tjol.eu (Thomas Jollans) Date: Mon, 18 Feb 2019 10:29:59 +0100 Subject: What's up with Activestate Python? In-Reply-To: <2E3FAC50-A3C5-4DAD-B1E7-417993F93AEA@barrys-emacs.org> References: <561e20ce-876e-1950-cc52-9781adf82ed1@tjol.eu> <2E3FAC50-A3C5-4DAD-B1E7-417993F93AEA@barrys-emacs.org> Message-ID: On 18/02/2019 10.21, Barry Scott wrote: > > >> On 18 Feb 2019, at 08:49, Thomas Jollans wrote: >> >> Anaconda also has its moments, and has some packages that PyPI doesn't >> (for my use case, this is primarily PyQt5). > > Odd I use PyQt5 from PyPI all the time and have for a few years now. It would appear that my information is outdated! From alister.ware at ntlworld.com Mon Feb 18 08:28:50 2019 From: alister.ware at ntlworld.com (Alister) Date: Mon, 18 Feb 2019 13:28:50 GMT Subject: What's the address for? References: Message-ID: On Mon, 18 Feb 2019 11:59:18 +0000, Stefan Ram wrote: > When one prints a function, one might get something like: > > > > . The participants of my basic course asked me what the address is > for. I did not know. > > What's so important about the (presumed) address of a function that it > is shown on every stringification of each function? it is the internal id of the function - Not necessarily an address, that is an implementation detail. it is not intended for use within a program & has (almost) no practical use. -- Microsoft Zen - Become one with the blue screen. -- From a Slashdot.org post From Gronicus at SGA.Ninja Mon Feb 18 12:30:20 2019 From: Gronicus at SGA.Ninja (Steve) Date: Mon, 18 Feb 2019 12:30:20 -0500 Subject: New rules for scope in a function? Message-ID: <001d01d4c7af$9fa54640$deefd2c0$@SGA.Ninja> I have been programming for more years than I want to admit and believe that I have a good understanding when it comes to the Scope of Variables when using functions. Are the rules different when using python? It looks as if I have to pass all variables to and from the function before it works. Are variables used in the main program not also visible for use within the function? Do these variables have to be declared as global? Steve Footnote: Ultrasound Technician Asks Pregnant Woman If She?d Like To Know Baby?s Name From rosuav at gmail.com Mon Feb 18 12:40:21 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 19 Feb 2019 04:40:21 +1100 Subject: New rules for scope in a function? In-Reply-To: <001d01d4c7af$9fa54640$deefd2c0$@SGA.Ninja> References: <001d01d4c7af$9fa54640$deefd2c0$@SGA.Ninja> Message-ID: On Tue, Feb 19, 2019 at 4:36 AM Steve wrote: > > I have been programming for more years than I want to admit and believe that I have a good understanding when it comes to the Scope of Variables when using functions. Are the rules different when using python? > > It looks as if I have to pass all variables to and from the function before it works. Are variables used in the main program not also visible for use within the function? Do these variables have to be declared as global? > > Steve > Functions in Python have access to everything in the surrounding scopes, usually a module. Any name that's ever *assigned to* in the function (including its parameters) is local to that function, and anything that you look up without assigning comes from the outer scope. x = 1 def f(y): print(x, y) This function is happily able to see its own parameter (a local) and the name from outside it (a global). It's also able to see the "print" function, which comes from the builtins - that's just another scope. ChrisA From Gronicus at SGA.Ninja Mon Feb 18 13:14:26 2019 From: Gronicus at SGA.Ninja (Steve) Date: Mon, 18 Feb 2019 13:14:26 -0500 Subject: New rules for scope in a function? In-Reply-To: References: <001d01d4c7af$9fa54640$deefd2c0$@SGA.Ninja> Message-ID: <002401d4c7b5$c8ba0830$5a2e1890$@SGA.Ninja> OK, I wrote: x = "A" print("x = " + x) def f(y): print("x= " + x + " y= "+ y) f(x) and it worked, inspite of what I was seeing in my own program. How quick I am to blame Python. (: When I isolate my function that stumped me, I will post it. Thanks. Steve Footnote: Ultrasound Technician Asks Pregnant Woman If She'd Like To Know Baby's Name -----Original Message----- From: Python-list On Behalf Of Chris Angelico Sent: Monday, February 18, 2019 12:40 PM To: Python Subject: Re: New rules for scope in a function? On Tue, Feb 19, 2019 at 4:36 AM Steve wrote: > > I have been programming for more years than I want to admit and believe that I have a good understanding when it comes to the Scope of Variables when using functions. Are the rules different when using python? > > It looks as if I have to pass all variables to and from the function before it works. Are variables used in the main program not also visible for use within the function? Do these variables have to be declared as global? > > Steve > Functions in Python have access to everything in the surrounding scopes, usually a module. Any name that's ever *assigned to* in the function (including its parameters) is local to that function, and anything that you look up without assigning comes from the outer scope. x = 1 def f(y): print(x, y) This function is happily able to see its own parameter (a local) and the name from outside it (a global). It's also able to see the "print" function, which comes from the builtins - that's just another scope. ChrisA -- https://mail.python.org/mailman/listinfo/python-list From drake.gossi at gmail.com Mon Feb 18 02:48:48 2019 From: drake.gossi at gmail.com (Drake Gossi) Date: Sun, 17 Feb 2019 23:48:48 -0800 Subject: Scraping multiple web pages help Message-ID: Hello everyone, For a research project, I need to scrape a lot of comments from regulations.gov https://www.regulations.gov/docketBrowser?rpp=25&so=DESC&sb=commentDueDate&po=0&dct=PS&D=ED-2018-OCR-0064 But partly what's throwing me is the url addresses of the comments. They aren't consistent. I mean, there's some consistency insofar as the numbers that differentiate the pages all begin after that 0064 number in the url listed above. But the differnetiating numbers aren't even all the same amount of numbers. Some are 4 (say, 4019) whereas others are 5 (say, 50343). But I dont think they go over 5. So this is a problem. I dont know how to write the code to access the multiple pages. I should also mention I'm new to programing, so that's also a problem (if you cant already tell by the way I'm describing my problem). I should also mention that, I think, there's an API on regulations.gov, but I'm such a beginner that I dont evem really know where to find it, or even what to do with it once I do. That's how helpless am right now. Any help anyone could offer would be much appreciated. D From marcgala at hotmail.com Sun Feb 17 18:34:58 2019 From: marcgala at hotmail.com (Marcin G) Date: Sun, 17 Feb 2019 23:34:58 +0000 Subject: Trying to compile Python 3.5 on Linux Mint 19, getting compiler warnings and failing tests Message-ID: My boss wants my code to run on Python 3.5, so I thought I'd install 3.5 to be able to ascertain this. But Linux Mint 19 ships with Python 3.6 and python.org only provides source code for 3.5.6. So I thought I'd try compiling 3.5.6 myself. This produced compiler warnings about: comparison between signed and unsigned, switches falling though, use of deprecated macros in glibc headers and too big object sizes leading to overflows in memcpy. This worries me the most, because it looks like undefined behavior to my untrained eye. I wonder if my system is misconfigured? Or if the configure script picks wrong sizes for some reason? Is there incompatibility between Python 3.5 and newer glibc headers? In addition, make test shows up one failing test: test_ssl. All I want is to have an installation that is not broken. If the above failures are false positives, I can gladly ignore them; however, since (unsupressed) warnings and failing tests are usually a sign of serious problems, I wonder what should I do get a functional installation? I do believe I should have all dependencies installed. First, I kept installing libraries until `make` stopped showing me info about missing bits for optional modules. Then, just to make sure, I did sudo apt-get build-dep python3. Problem is still not solved, however. Pastes: Full log. Just warnings. Just failing tests. From lyssdod at gmail.com Sat Feb 16 15:29:03 2019 From: lyssdod at gmail.com (Alexander Revin) Date: Sat, 16 Feb 2019 21:29:03 +0100 Subject: (yet another) PEP idea to tackle binary wheels problem efficiently Message-ID: Hi all, I've been thoroughly reading various discussions, such as [1], [2] and related ones regarding PEP 425, PEP 491 and PEP 513. I also happen to use musl sometimes, so as discussed here [3] I thought it would be a good idea to submit a new PEP regarding musl compatibility. It's not a secret that everyday wheel usage on Linux is far from perfect. Some packages are trying to compile when there's no compiler on the system available, some rely on 3rd-party deps and explode when they cannot find required headers installed and so on. Add to this cross-compilation support (quite complicated) and distros like Alpine or just something not using x86 (how many piwheels.org-like should emerge for each non-x86 platform?). For example, I would like to seamlessly install numpy, pandas and scikit onto ppc machine running Gentoo musl and not waste 1 hour for compilation, or "just" use them in x86 standard alpine-based docker image (basically what [3] is all about). Long story short, current wheel filename format: {distribution}-{version}(-{build tag})?-{python tag}-{abi tag}-{platform tag}.whl. Doesn't not properly express package expectation. Let's take a look at pandas wheels ([4]): pandas-0.24.1-cp36-cp36m-manylinux1_x86_64.whl pandas-0.24.1-cp36-cp36m-win_amd64.whl pandas-0.24.1-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl I see issues with each of them: 1. First one won't work on Alpine or any musl-based distro; 2. Second ? how amd64 is different from x86_64? 3. Third's platform tag is just a nightmare. More of that, if you open the last one and inspect one of the libraries, you'll find that: $ file _libs/algos.cpython-36m-darwin.so _libs/algos.cpython-36m-darwin.so: Mach-O universal binary with 2 architectures: [i386:Mach-O bundle i386] [x86_64] _libs/algos.cpython-36m-darwin.so (for architecture i386): Mach-O bundle i386 _libs/algos.cpython-36m-darwin.so (for architecture x86_64): Mach-O 64-bit bundle x86_64 It's universal library! So not x86_64 only, as mentioned in the quite long macosx_10_various platform tag. TL;DR What my solution? To use something widely called "Target Triplet" [5], omitting usual "vendor" field, so {platform tag} from PEP 435 will have the format of _[_]: pandas-0.24.1-cp36-cp36m-x86_64_linux_gnu.whl pandas-0.24.1-cp36-cp36m-x86_64_linux_musl.whl pandas-0.24.1-cp36-cp36m-x86_windows.whl pandas-0.24.1-cp36-cp36m-x86_64_windows_msvs2010.whl pandas-0.24.1-cp36-cp36m-x86_macosx_10_6.whl <-- won't be used for anything Mojave+, see [6] pandas-0.24.1-cp36-cp36m_aarch64_netbsd.whl Primary concerns here: - Arch and os are specified more consistently; - Environment is specified only when needed; - Lots of possible combinations of os and env are possible :) Since most runtimes are hardcoded during build time anyway and changed for each Python release, explicit versioning shouldn't be a problem. JavaScript and Rustlang [7] use similar naming scheme. Though I don't like both of them, at least portability of extensions looks less broken than of Python (I've worked on native Nodejs extension in the past). What do you think? Thanks, Alex [1] https://mail.python.org/archives/list/distutils-sig at python.org/thread/KCLRIN4PTUGZLLL7GOUM23S46ZZ2D4FU/ [2] https://github.com/pypa/packaging-problems/issues/69 [3] https://github.com/pypa/manylinux/issues/37 [4] https://pypi.org/project/pandas/#files [5] https://wiki.osdev.org/Target_Triplet [6] https://support.apple.com/en-us/HT208436 [7] https://doc.rust-lang.org/reference/conditional-compilation.html From lyssdod at gmail.com Sat Feb 16 15:58:34 2019 From: lyssdod at gmail.com (Alexander Revin) Date: Sat, 16 Feb 2019 21:58:34 +0100 Subject: (yet another) PEP idea to tackle binary wheels problem efficiently In-Reply-To: References: Message-ID: Two minor typos: platform tag should be separated by "-", not "_". Also it makes sense to use "amd64" instead of "x86_64", so platform can be just split by "_" On Sat, Feb 16, 2019 at 9:29 PM Alexander Revin wrote: > > Hi all, > > I've been thoroughly reading various discussions, such as [1], [2] and > related ones regarding PEP 425, PEP 491 and PEP 513. I also happen to > use musl sometimes, so as discussed here [3] I thought it would be a > good idea to submit a new PEP regarding musl compatibility. > > It's not a secret that everyday wheel usage on Linux is far from > perfect. Some packages are trying to compile when there's no compiler > on the system available, some rely on 3rd-party deps and explode when > they cannot find required headers installed and so on. Add to this > cross-compilation support (quite complicated) and distros like Alpine > or just something not using x86 (how many piwheels.org-like should > emerge for each non-x86 platform?). For example, I would like to > seamlessly install numpy, pandas and scikit onto ppc machine running > Gentoo musl and not waste 1 hour for compilation, or "just" use them > in x86 standard alpine-based docker image (basically what [3] is all > about). > > Long story short, current wheel filename format: > > {distribution}-{version}(-{build tag})?-{python tag}-{abi > tag}-{platform tag}.whl. > > Doesn't not properly express package expectation. Let's take a look at > pandas wheels ([4]): > > pandas-0.24.1-cp36-cp36m-manylinux1_x86_64.whl > pandas-0.24.1-cp36-cp36m-win_amd64.whl > pandas-0.24.1-cp36-cp36m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl > > I see issues with each of them: > 1. First one won't work on Alpine or any musl-based distro; > 2. Second ? how amd64 is different from x86_64? > 3. Third's platform tag is just a nightmare. > > More of that, if you open the last one and inspect one of the > libraries, you'll find that: > $ file _libs/algos.cpython-36m-darwin.so > _libs/algos.cpython-36m-darwin.so: Mach-O universal binary with 2 > architectures: [i386:Mach-O bundle i386] [x86_64] > _libs/algos.cpython-36m-darwin.so (for architecture i386): Mach-O bundle i386 > _libs/algos.cpython-36m-darwin.so (for architecture x86_64): Mach-O > 64-bit bundle x86_64 > > It's universal library! So not x86_64 only, as mentioned in the quite > long macosx_10_various platform tag. > > TL;DR What my solution? To use something widely called "Target > Triplet" [5], omitting usual "vendor" field, so > {platform tag} from PEP 435 will have the format of _[_]: > > pandas-0.24.1-cp36-cp36m-x86_64_linux_gnu.whl > pandas-0.24.1-cp36-cp36m-x86_64_linux_musl.whl > pandas-0.24.1-cp36-cp36m-x86_windows.whl > pandas-0.24.1-cp36-cp36m-x86_64_windows_msvs2010.whl > pandas-0.24.1-cp36-cp36m-x86_macosx_10_6.whl <-- won't be used for > anything Mojave+, see [6] > pandas-0.24.1-cp36-cp36m_aarch64_netbsd.whl > > Primary concerns here: > - Arch and os are specified more consistently; > - Environment is specified only when needed; > - Lots of possible combinations of os and env are possible :) > > Since most runtimes are hardcoded during build time anyway and changed > for each Python release, explicit versioning shouldn't be a problem. > > JavaScript and Rustlang [7] use similar naming scheme. Though I don't > like both of them, at least portability of extensions looks less > broken than of Python (I've worked on native Nodejs extension in the > past). > > > What do you think? > > Thanks, > Alex > > [1] https://mail.python.org/archives/list/distutils-sig at python.org/thread/KCLRIN4PTUGZLLL7GOUM23S46ZZ2D4FU/ > [2] https://github.com/pypa/packaging-problems/issues/69 > [3] https://github.com/pypa/manylinux/issues/37 > [4] https://pypi.org/project/pandas/#files > [5] https://wiki.osdev.org/Target_Triplet > [6] https://support.apple.com/en-us/HT208436 > [7] https://doc.rust-lang.org/reference/conditional-compilation.html From rosuav at gmail.com Mon Feb 18 13:48:57 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 19 Feb 2019 05:48:57 +1100 Subject: Trying to compile Python 3.5 on Linux Mint 19, getting compiler warnings and failing tests In-Reply-To: References: Message-ID: On Tue, Feb 19, 2019 at 5:23 AM Marcin G wrote: > > My boss wants my code to run on Python 3.5, so I thought I'd install 3.5 to be able to ascertain this. > > But Linux Mint 19 ships with Python 3.6 and python.org only provides source code for 3.5.6. So I thought I'd try compiling 3.5.6 myself. > TBH it's highly unlikely that the difference between 3.5.x and 3.5.y will be significant here. To test that your code will run on 3.5, you can almost certainly just grab any 3.5. > This produced compiler warnings about: comparison between signed and unsigned, switches falling though, use of deprecated macros in glibc headers and too big object sizes leading to overflows in memcpy. This worries me the most, because it looks like undefined behavior to my untrained eye. I wonder if my system is misconfigured? Or if the configure script picks wrong sizes for some reason? Is there incompatibility between Python 3.5 and newer glibc headers? > Unfortunately a LOT of large projects spew warnings. :( It would be awesome if we could confidently expect that a correctly-configured system would build any released Python without warnings, but we can't, so I would just look for actual errors. > In addition, make test shows up one failing test: test_ssl. > Hmm. From looking at your full log (THANK YOU for posting that, btw - so many people don't), it looks like an issue with certificate checking. Might be a bug in the test itself. Does the same thing happen with a more recent Python build? It could be a weirdness with specific versions of OpenSSL. I don't think it's going to be a killer problem. It's a failure in a narrow part of SSL handling, one that most code won't hit. > All I want is to have an installation that is not broken. If the above failures are false positives, I can gladly ignore them; however, since (unsupressed) warnings and failing tests are usually a sign of serious problems, I wonder what should I do get a functional installation? > > I do believe I should have all dependencies installed. First, I kept installing libraries until `make` stopped showing me info about missing bits for optional modules. Then, just to make sure, I did sudo apt-get build-dep python3. Problem is still not solved, however. > TBH I wouldn't use that as a final step - I'd use it as the first step. It's usually going to get all, or at least nearly all, the libraries you need; way easier than manually grabbing libraries until make stops complaining. Summary: I'd say you do currently have a 3.5.6 build that's viable for testing, but you could also have picked up any other 3.5.x that might be available in packaged form, and it would also have been test-worthy. The test failure might be something to dive into separately, but I believe it's okay to ignore it here. ChrisA From avigross at verizon.net Mon Feb 18 14:26:44 2019 From: avigross at verizon.net (Avi Gross) Date: Mon, 18 Feb 2019 14:26:44 -0500 Subject: The NaNny State Message-ID: <004001d4c7bf$e2559ed0$a700dc70$@verizon.net> This is not a complaint about python. It is about the recent discussion about the concept and word "nan" as used in python and elsewhere. As noted, the correct spelling in python is all lower case as in "nan" with a minor exception that creating a nan using float(string) allows any combination of cases such as string="nAN". I like to learn and contrast languages to try to make some sense of things and see different ways to conceptualize things. As discussed, there are subtle uses of concepts such as NOT A NUMBER versus NOT AVAILABLE that often get conflated. In Python, the math and numpy modules also include a nan constant as in math.nan. What is bothersome is the ways other places in a language spell the same or related concept leading to confusion. If you read a CSV file using the pandas module with pandas.read_csv() it shows empty items as "NaN". The programming language JAVA spells it the same way to mean the result of dividing zero by zero as Not a Number. The programming language R uses NA for Not Available and uses NaN for Not a Number but also has constants that test more specifically such as NA_integer_ and NA_character_ and you have test functions like is.na() and is.nan() but just to be confusing is.na() also returns TRUE when looking at a NaN! I am sure we can look at other languages and get even more confused! Now I am not saying which implementation is in some ways better, just admiring the diversity of ways of looking at things that seem to happen as many languages develop. I see other anomalies such as languages that have a concept of both a negative and positive zero or different infinity types for float versus double. There are so many themes you can look at across languages that indicate there is no one totally obvious way. So when people wonder why python does things in a way they are not seeing as right, they need to wonder if dropping some preconceptions may let them learn what the language DOES and adjust rather than fight it. Don't let the NaNny state dictate what must be. From sivan at vitakka.co Mon Feb 18 14:50:40 2019 From: sivan at vitakka.co (=?UTF-8?Q?Sivan_Gr=C3=BCnberg?=) Date: Mon, 18 Feb 2019 21:50:40 +0200 Subject: Scraping multiple web pages help In-Reply-To: References: Message-ID: Hi there Drake, A quick google search revealed: - https://regulationsgov.github.io/developers/ This seems particulriy useful: - https://regulationsgov.github.io/developers/console/ And to fetch stuff from the API, there's Python requests that has a rather wonderful doc: - http://docs.python-requests.org/en/master/ HTH, -Sivan On Mon, Feb 18, 2019 at 8:22 PM Drake Gossi wrote: > Hello everyone, > > For a research project, I need to scrape a lot of comments from > regulations.gov > > > https://www.regulations.gov/docketBrowser?rpp=25&so=DESC&sb=commentDueDate&po=0&dct=PS&D=ED-2018-OCR-0064 > > But partly what's throwing me is the url addresses of the comments. They > aren't consistent. I mean, there's some consistency insofar as the numbers > that differentiate the pages all begin after that 0064 number in the url > listed above. But the differnetiating numbers aren't even all the same > amount of numbers. Some are 4 (say, 4019) whereas others are 5 (say, > 50343). But I dont think they go over 5. So this is a problem. I dont know > how to write the code to access the multiple pages. > > I should also mention I'm new to programing, so that's also a problem (if > you cant already tell by the way I'm describing my problem). > > > I should also mention that, I think, there's an API on regulations.gov, > but > I'm such a beginner that I dont evem really know where to find it, or even > what to do with it once I do. That's how helpless am right now. > > Any help anyone could offer would be much appreciated. > > D > -- > https://mail.python.org/mailman/listinfo/python-list > -- Sivan Greenberg Co founder & CTO Vitakka Consulting From avigross at verizon.net Mon Feb 18 15:02:49 2019 From: avigross at verizon.net (Avi Gross) Date: Mon, 18 Feb 2019 15:02:49 -0500 Subject: What's the address for? References: Message-ID: <004801d4c7c4$ecadaf30$c6090d90$@verizon.net> Alister wrote about the meaning of the id number often displayed about a python object: > it is the internal id of the function - Not necessarily an address, that is an implementation detail. > it is not intended for use within a program & has (almost) no practical use. I hear that it is implementation dependent. But are there any requirements on the implementation that allow it to have meaning? I mean is the ID guaranteed to be unique and not reused within a session? If two things concurrently show the same ID, are they the same in some way? On the implementation I am using, the ID changes if I do this: >>> a = 1 >>> id(a) 271178896 >>> a = 2 >>> id(a) 271178912 >>> a = 1 >>> id(a) 271178896 It looks like the ID of "a" can change depending on its contents. But when I set it back to the first content, I get the same ID. Again, on MY IMPLEMENTATION, I did an experiment to see if the constants 1 and 2 share that ID. >>> id(1) 271178896 >>> id(2) 271178912 It seems they do. So I tried setting variable b several ways: >>> b = 1 >>> id(b) 271178896 >>> b = a >>> id(b), id(a) (271178896, 271178896) >>> a = 2 >>> (271178896, 271178896) (271178896, 271178896) So it does seem in this artificial case of looking at something easily cached like an integer, that ID has these qualities. I tried using complex and the results are a bit complex: >>> import math >>> a = complex("1+2j") >>> a (1+2j) >>> id(a) 48066432 >>> id(complex("1+2j")) 55084360 >>> b = complex("1+2j") >>> id(b) 55084360 >>> a = b >>> id(a) 55084360 >>> a = complex("1+2j") >>> id(a) 48066432 >>> c = complex(1,2) >>> id(c) 55086880 We seem to go between two values although I am not sure why. How about functions? Even more confusing: >>> def a(): pass >>> def b(): pass >>> id(a), id(b) (13123280, 13201816) >>> b=a >>> id(a), id(b) (13123280, 13123280) >>> def a(): return True >>> id(a), id(b) (13201816, 13123280) The above shows that making two independent (albeit otherwise minimally identical) functions makes two different id and copying one to the other makes them the same but then changing one gets back the same ID reused! So, I would be hesitant about assigning much meaning to an ID. It might be useful within an implementation when using a debugger but perhaps not much more. So I decided to do what maybe should be done first. Find some documentation! If what I read is accurate, given the source, it sounds like for Cpython an ID may be a memory address and the guarantee is that once an ID is assigned to something it will not be reassigned to something else as long as the object remains in effect. Well, I assume that means that anything else set to be the same as the original as in "a = b" will share that ID. Once an object is gone, the ID can be reused. This may help understand some of the above results especially since memory management may not rapidly get rid of something. I concur that there is often not much point in using id() or even its cousin is. -----Original Message----- From: Python-list On Behalf Of Alister via Python-list Sent: Monday, February 18, 2019 8:29 AM To: python-list at python.org Subject: Re: What's the address for? On Mon, 18 Feb 2019 11:59:18 +0000, Stefan Ram wrote: > When one prints a function, one might get something like: > > > > . The participants of my basic course asked me what the address is > for. I did not know. > > What's so important about the (presumed) address of a function that it > is shown on every stringification of each function? it is the internal id of the function - Not necessarily an address, that is an implementation detail. it is not intended for use within a program & has (almost) no practical use. -- Microsoft Zen - Become one with the blue screen. -- From a Slashdot.org post -- https://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Mon Feb 18 15:06:27 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 19 Feb 2019 07:06:27 +1100 Subject: What's the address for? In-Reply-To: <004801d4c7c4$ecadaf30$c6090d90$@verizon.net> References: <004801d4c7c4$ecadaf30$c6090d90$@verizon.net> Message-ID: On Tue, Feb 19, 2019 at 7:04 AM Avi Gross wrote: > > Alister wrote about the meaning of the id number often displayed about a > python object: > > > it is the internal id of the function - Not necessarily an address, that > is an implementation detail. > > > it is not intended for use within a program & has (almost) no practical > use. > > I hear that it is implementation dependent. But are there any requirements > on the implementation that allow it to have meaning? I mean is the ID > guaranteed to be unique and not reused within a session? If two things > concurrently show the same ID, are they the same in some way? Have you considered reading the docs? https://docs.python.org/3/library/functions.html#id """Return the ?identity? of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same id() value.""" I'm fairly sure that answers all your questions. ChrisA From rosuav at gmail.com Mon Feb 18 15:10:06 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 19 Feb 2019 07:10:06 +1100 Subject: What's the address for? In-Reply-To: <004801d4c7c4$ecadaf30$c6090d90$@verizon.net> References: <004801d4c7c4$ecadaf30$c6090d90$@verizon.net> Message-ID: On Tue, Feb 19, 2019 at 7:04 AM Avi Gross wrote: > How about functions? > > Even more confusing: > > >>> def a(): pass > > >>> def b(): pass > > >>> id(a), id(b) > (13123280, 13201816) > >>> b=a > >>> id(a), id(b) > (13123280, 13123280) > >>> def a(): return True > > >>> id(a), id(b) > (13201816, 13123280) > > The above shows that making two independent (albeit otherwise minimally > identical) functions makes two different id and copying one to the other > makes them the same but then changing one gets back the same ID reused! Actually, this part isn't explained by the id() docs. To understand this, you need to go back to something even more basic: the meaning of assignment. You did not copy one to the other. You simply changed name bindings. Here. Read. https://nedbatchelder.com/text/names1.html ChrisA From marcgala at hotmail.com Mon Feb 18 15:35:26 2019 From: marcgala at hotmail.com (Marcin G) Date: Mon, 18 Feb 2019 20:35:26 +0000 Subject: Trying to compile Python 3.5 on Linux Mint 19, getting compiler warnings and failing tests In-Reply-To: References: , Message-ID: Hmm. From looking at your full log (THANK YOU for posting that, btw - so many people don't), it looks like an issue with certificate checking. Might be a bug in the test itself. Does the same thing happen with a more recent Python build? It could be a weirdness with specific versions of OpenSSL. Hmm, my Googling brought me this: https://bugs.python.org/issue30714 Apparently, the test fails with newer versions of openssl and the test was fixed in 3.7, 3.6 and 2.7, but for some reason 3.5 was removed from the versions field of this bug report. From rosuav at gmail.com Mon Feb 18 15:47:49 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 19 Feb 2019 07:47:49 +1100 Subject: Trying to compile Python 3.5 on Linux Mint 19, getting compiler warnings and failing tests In-Reply-To: References: Message-ID: On Tue, Feb 19, 2019 at 7:36 AM Marcin G wrote: > > Hmm. From looking at your full log (THANK YOU for posting that, btw - > so many people don't), it looks like an issue with certificate > checking. Might be a bug in the test itself. Does the same thing > happen with a more recent Python build? It could be a weirdness with > specific versions of OpenSSL. > > Hmm, my Googling brought me this: > > https://bugs.python.org/issue30714 > > Apparently, the test fails with newer versions of openssl and the test was fixed in 3.7, 3.6 and 2.7, but for some reason 3.5 was removed from the versions field of this bug report. > Good find! Yep, looks like it's just a test failure, so you can proceed to use your built Python. Not sure why 3.5 was removed; presumably the release manager (Larry Hastings, in this case) decided against it, but I didn't see any explanation of why. ChrisA From grant.b.edwards at gmail.com Mon Feb 18 15:53:35 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Mon, 18 Feb 2019 20:53:35 -0000 (UTC) Subject: Trying to compile Python 3.5 on Linux Mint 19, getting compiler warnings and failing tests References: Message-ID: On 2019-02-18, Chris Angelico wrote: > Hmm. From looking at your full log (THANK YOU for posting that, btw - > so many people don't), it looks like an issue with certificate > checking. Might be a bug in the test itself. Does the same thing > happen with a more recent Python build? It could be a weirdness with > specific versions of OpenSSL. If your app involves SSL and certificates, it's never going to work right anyway. Even if it does, nobody will ever use it correctly. Even if they do, whoever created the certificates will have screwed them up. Even if they didn't, they'll forget to renew/replace them before they expire. Even if your app and certificates are OK, the app on the other end of the network connection won't work right. I'm only half joking... -- Grant Edwards grant.b.edwards Yow! They collapsed at ... like nuns in the gmail.com street ... they had no teen appeal! From rosuav at gmail.com Mon Feb 18 16:00:22 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 19 Feb 2019 08:00:22 +1100 Subject: Trying to compile Python 3.5 on Linux Mint 19, getting compiler warnings and failing tests In-Reply-To: References: Message-ID: On Tue, Feb 19, 2019 at 7:55 AM Grant Edwards wrote: > > On 2019-02-18, Chris Angelico wrote: > > > Hmm. From looking at your full log (THANK YOU for posting that, btw - > > so many people don't), it looks like an issue with certificate > > checking. Might be a bug in the test itself. Does the same thing > > happen with a more recent Python build? It could be a weirdness with > > specific versions of OpenSSL. > > If your app involves SSL and certificates, it's never going to work > right anyway. Even if it does, nobody will ever use it correctly. > Even if they do, whoever created the certificates will have screwed > them up. Even if they didn't, they'll forget to renew/replace them > before they expire. Even if your app and certificates are OK, the app > on the other end of the network connection won't work right. > > I'm only half joking... > No, I think you've accurately described internet security. With one omission: Nobody knows that it isn't working correctly. ChrisA From ben.usenet at bsb.me.uk Mon Feb 18 16:04:02 2019 From: ben.usenet at bsb.me.uk (Ben Bacarisse) Date: Mon, 18 Feb 2019 21:04:02 +0000 Subject: New rules for scope in a function? References: <001d01d4c7af$9fa54640$deefd2c0$@SGA.Ninja> <002401d4c7b5$c8ba0830$5a2e1890$@SGA.Ninja> Message-ID: <87zhqs95bx.fsf@bsb.me.uk> "Steve" writes: > OK, I wrote: > > x = "A" > print("x = " + x) > > def f(y): > print("x= " + x + " y= "+ y) > > f(x) > > and it worked, inspite of what I was seeing in my own program. > How quick I am to blame Python. (: > When I isolate my function that stumped me, I will post it. This small variation might point the way. If you can explain it, you'll be most of the way there. x = "A" print("x = " + x) def f(y): x = "B" print("In f, x = " + x + " y = "+ y) f(x) print("x = " + x) The material I cut did include the explanation so go back and read the message you replied to here if you don't follow. -- Ben. From ben+python at benfinney.id.au Mon Feb 18 17:07:44 2019 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 19 Feb 2019 09:07:44 +1100 Subject: The NaNny State References: <004001d4c7bf$e2559ed0$a700dc70$@verizon.net> Message-ID: <86a7is698v.fsf@benfinney.id.au> "Avi Gross" writes: > It is about the recent discussion about the concept and word "nan" as used > in python and elsewhere. As noted, the correct spelling in python is all > lower case as in "nan" with a minor exception that creating a nan using > float(string) allows any combination of cases such as string="nAN". Who says that the ?correct spelling in python is all lower case "nan"?? The text representation of a Python ?float? NaN object is 'nan'. That is what the object emits as its text representation; it is not the same thing as "the correct spelling". As you note, the ?float? type accepts several spellings as input to create a NaN object, all of them correct spelling. Similarly, I can spell the number one thousand ?1000.0?, ?1.0e+3? ?1.000e+3?, ?1000.00000?, and so on. Those are all correct (and, as it happens, they all result in equal ?float? values). The resulting object will, when I interrogate it, represent itself *by default* as ?1000.0?; Python is not showing *the* correct spelling, just one possible correct spelling. -- \ ?I wrote a song, but I can't read music so I don't know what it | `\ is. Every once in a while I'll be listening to the radio and I | _o__) say, ?I think I might have written that.?? ?Steven Wright | Ben Finney From avigross at verizon.net Mon Feb 18 18:24:48 2019 From: avigross at verizon.net (Avi Gross) Date: Mon, 18 Feb 2019 18:24:48 -0500 Subject: The NaNny State In-Reply-To: <86a7is698v.fsf@benfinney.id.au> References: <004001d4c7bf$e2559ed0$a700dc70$@verizon.net> <86a7is698v.fsf@benfinney.id.au> Message-ID: <000c01d4c7e1$24140d90$6c3c28b0$@verizon.net> [DISCLAIMER: I can read documentation and have. The following is more of a demo showing step by step what I find experimentally along with some discussion.] Ben asked: > Who says that the ?correct spelling in python is all lower case "nan"?? Fair enough. Except for reserved words in the language, all we have is hints of what the designers used. The reality is that whoever codes the __str__ and/or __repr__ chose lower case and so did the persons who made math.nan and numpy.nan and obviously we can over-ride these decisions in our own derived customized classes. >>> import math, numpy >>> nantucket, nanometer, nanoseconds = float("NAN"), math.nan, numpy.nan >>> nantucket, nanometer, nanoseconds (nan, nan, nan) >>> [ spelled.__str__() for spelled in [nantucket, nanometer, nanoseconds]] ['nan', 'nan', 'nan'] >>> [ spelled.__repr__() for spelled in [nantucket, nanometer, nanoseconds]] ['nan', 'nan', 'nan'] I have a hard time in thinking of anywhere else you normally would type "nan" or a variant in native python other than the float example and a similar one such as complex("NAN", 5). >>> math.isnan(nanometer) True >>> numpy.isnan(nanoseconds) True So I decided to look and see what some python extensions (modules) that I am aware of choose to display. I assume many of them rely on the str/repr of the underlying "float" and I see numpy does: >>> nannite = numpy.array([1, float("NAN"), 2, math.nan, 3, numpy.nan]) >>> nannite array([ 1., nan, 2., nan, 3., nan]) I mentioned earlier that the pandas module has an exception, of sorts. >>> import pandas >>> nanism = pandas.DataFrame( {'FORWARD': nannite, 'BACKWARD': nannite[::-1]}) >>> nanism FORWARD BACKWARD 0 1.0 NaN 1 NaN 3.0 2 2.0 NaN 3 NaN 2.0 4 3.0 NaN 5 NaN 1.0 So, as I said, at least one extension chose differently and uses NaN. BUT if I explicitly request the one item on the second row and first column it says 'nan' as it is no longer being handled by the same decision: >>> nanism.at[1,'FORWARD'] nan I have no doubt that if you search all over the place, you will find additional spellings say in a machine the sklearn module which is probably using pandas below it. And, yes, I tried many other things I will not trouble you with. I repeat. I concede there is not necessarily any enforced spelling for 'nan' in the language. So, anyone want to decide how to spell inf properly? >>> nanism.at[0,'FORWARD'] = math.inf >>> nanism.at[1,'BACKWARD'] = float("-InF") >>> nanism FORWARD BACKWARD 0 inf NaN 1 NaN -inf 2 2.000000 NaN 3 NaN 2.000000 4 3.000000 NaN 5 NaN 1.000000 I think it is time for me stop talking about what is not. ? -----Original Message----- From: Python-list On Behalf Of Ben Finney Sent: Monday, February 18, 2019 5:08 PM To: python-list at python.org Subject: Re: The NaNny State "Avi Gross" writes: > It is about the recent discussion about the concept and word "nan" as > used in python and elsewhere. As noted, the correct spelling in python > is all lower case as in "nan" with a minor exception that creating a > nan using > float(string) allows any combination of cases such as string="nAN". Who says that the ?correct spelling in python is all lower case "nan"?? The text representation of a Python ?float? NaN object is 'nan'. That is what the object emits as its text representation; it is not the same thing as "the correct spelling". As you note, the ?float? type accepts several spellings as input to create a NaN object, all of them correct spelling. Similarly, I can spell the number one thousand ?1000.0?, ?1.0e+3? ?1.000e+3?, ?1000.00000?, and so on. Those are all correct (and, as it happens, they all result in equal ?float? values). The resulting object will, when I interrogate it, represent itself *by default* as ?1000.0?; Python is not showing *the* correct spelling, just one possible correct spelling. -- \ ?I wrote a song, but I can't read music so I don't know what it | `\ is. Every once in a while I'll be listening to the radio and I | _o__) say, ?I think I might have written that.?? ?Steven Wright | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list From adrian.ordona at gmail.com Mon Feb 18 19:06:07 2019 From: adrian.ordona at gmail.com (Adrian Ordona) Date: Mon, 18 Feb 2019 16:06:07 -0800 Subject: WebScrapping Message-ID: <8A2C4E91-98A3-4D00-AEE4-1087493E8632@gmail.com> Hi, I?m learning how to code and interested in web scrapping to gather data. I?m running on Mac OS X 10.9.5 and python 3.7 terminal. I?m trying to capture the name of the brand and price but i keep getting an error (see below). Traceback (most recent call last): File "", line 1, in File "/anaconda3/lib/python3.7/site-packages/bs4/element.py", line 1884, in __getattr__ "ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()? Here?s what i got and thanks for the help import bs4 from urllib.request import urlopen as uReq from bs4 import BeautifulSoup as soup my_url = 'https://www.newegg.com/Desktop-Graphics-Cards/SubCategory/ID-48?Tid=7709' uClient = uReq(my_url) page_html = uClient.read() uClient.close() page_soup = soup(page_html, "html.parser") records = [] containers = page_soup.findAll("div",{"class":"item-container"}) for container in containers: brand = container.find('div', attrs={'class':'item-branding'}) price = container.find('div', attrs={'class':'item-action'}) records.append((brand, price)) From ben+python at benfinney.id.au Mon Feb 18 20:33:07 2019 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 19 Feb 2019 12:33:07 +1100 Subject: What's the address for? References: <004801d4c7c4$ecadaf30$c6090d90$@verizon.net> Message-ID: <865ztg5zqk.fsf@benfinney.id.au> "Avi Gross" writes: > I hear that [the ?id(foo)? return value] is implementation dependent. > But are there any requirements on the implementation that allow it to > have meaning? The requirements are that `id(foo)` should satisfy the documented API for that function : Return the ?identity? of an object. This is an integer which is guaranteed to be unique and constant for this object during its lifetime. Two objects with non-overlapping lifetimes may have the same `id()` value. > I mean is the ID guaranteed to be unique and not reused within a > session? No. The identity of an object is guaranteed to be unique only during the lifetime of that object. This implies that *outside* the lifetime of that object (before it exists; after it is destroyed) the same value is allowed to be the identity of some other object. > If two things concurrently show the same ID, are they the same in some > way? Yes, querying the identity of two references concurrently will return the same identity value only if those two references refer to the same object. That is the essential meaning of an object identity: you can compare it with some other identity value and see whether that came from the same object. Other than object identity, there is pretty much no guarantee (and hence almost no purpose for the value you get from ?id(foo)?). That is useful enough, of course. > On the implementation I am using, the ID changes if I do this: You are creating new objects and binding the name ?a? to different objects in succession. Those different objects will each have different identities. > It looks like the ID of "a" can change depending on its contents. That's because a name is not a container, it is a reference. Names don't know *anything* about the object; they have no type, no identity, nothing except the ability to refer to some object at a particular point in time. > So I decided to do what maybe should be done first. Find some > documentation! Yes (especially the documentation of the function you're using, ?id?). Also helpful: Learn the actual behaviour of references in Python. They do not behave like ?variables? in some other languages (I avoid talking about Python ?variables? at all, for this reason). References are not containers, and thinking of them that way will frequently lead you to the wrong conclusion. -- \ ?Theology is the effort to explain the unknowable in terms of | `\ the not worth knowing.? ?Henry L. Mencken | _o__) | Ben Finney From ijbrewster at alaska.edu Mon Feb 18 15:50:16 2019 From: ijbrewster at alaska.edu (Israel Brewster) Date: Mon, 18 Feb 2019 11:50:16 -0900 Subject: Multiprocessing performance question Message-ID: <9643E929-DF79-4DE5-B3BC-A1814DEC5F3C@alaska.edu> I have the following code running in python 3.7: def create_box(x_y): return geometry.box(x_y[0] - 1, x_y[1], x_y[0], x_y[1] - 1) x_range = range(1, 1001) y_range = range(1, 801) x_y_range = list(itertools.product(x_range, y_range)) grid = list(map(create_box, x_y_range)) Which creates and populates an 800x1000 ?grid? (represented as a flat list at this point) of ?boxes?, where a box is a shapely.geometry.box(). This takes about 10 seconds to run. Looking at this, I am thinking it would lend itself well to parallelization. Since the box at each ?coordinate" is independent of all others, it seems I should be able to simply split the list up into chunks and process each chunk in parallel on a separate core. To that end, I created a multiprocessing pool: pool = multiprocessing.Pool() And then called pool.map() rather than just ?map?. Somewhat to my surprise, the execution time was virtually identical. Given the simplicity of my code, and the presumable ease with which it should be able to be parallelized, what could explain why the performance did not improve at all when moving from the single-process map() to the multiprocess map()? I am aware that in python3, the map function doesn?t actually produce a result until needed, but that?s why I wrapped everything in calls to list(), at least for testing. --- Israel Brewster Software Engineer Alaska Volcano Observatory Geophysical Institute - UAF 2156 Koyukuk Drive Fairbanks AK 99775-7320 Work: 907-474-5172 cell: 907-328-9145 From ben+python at benfinney.id.au Mon Feb 18 22:37:23 2019 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 19 Feb 2019 14:37:23 +1100 Subject: Multiprocessing performance question References: <9643E929-DF79-4DE5-B3BC-A1814DEC5F3C@alaska.edu> Message-ID: <861s445tzg.fsf@benfinney.id.au> I don't have anything to add regarding your experiments with multiprocessing, but: Israel Brewster writes: > Which creates and populates an 800x1000 ?grid? (represented as a flat > list at this point) of ?boxes?, where a box is a > shapely.geometry.box(). This takes about 10 seconds to run. This seems like the kind of task NumPy is designed to address: Generating and manipulating large-to-huge arrays of numbers, especially numbers that are representable directly in the machine's basic number types (such as moderate-scale integers). Have you tried using that library and timing the result? -- \ ?You don't need a book of any description to help you have some | `\ kind of moral awareness.? ?Dr. Francesca Stavrakoloulou, bible | _o__) scholar, 2011-05-08 | Ben Finney From ijbrewster at alaska.edu Mon Feb 18 23:34:28 2019 From: ijbrewster at alaska.edu (Israel Brewster) Date: Mon, 18 Feb 2019 19:34:28 -0900 Subject: Multiprocessing performance question In-Reply-To: <861s445tzg.fsf@benfinney.id.au> References: <9643E929-DF79-4DE5-B3BC-A1814DEC5F3C@alaska.edu> <861s445tzg.fsf@benfinney.id.au> Message-ID: > On Feb 18, 2019, at 6:37 PM, Ben Finney wrote: > > I don't have anything to add regarding your experiments with > multiprocessing, but: > > Israel Brewster writes: > >> Which creates and populates an 800x1000 ?grid? (represented as a flat >> list at this point) of ?boxes?, where a box is a >> shapely.geometry.box(). This takes about 10 seconds to run. > > This seems like the kind of task NumPy is > designed to address: Generating and manipulating large-to-huge arrays of > numbers, especially numbers that are representable directly in the > machine's basic number types (such as moderate-scale integers). > > Have you tried using that library and timing the result? Sort of. I am using that library, and in fact once I get the result I am converting it to a NumPy array for further use/processing, however I am still a NumPy newbie and have not been able to find a function that generates a numpy array from a function. There is the numpy.fromfunction() command, of course, but ??the function is called with ? each parameter representing the coordinates of the array varying along a specific axis??, which basically means (if my understanding/inital testing is correct) that my function would need to work with *arrays* of x,y coordinates. But the geometry.box() function needs individual x,y coordinates, not arrays, so I?d have to loop through the arrays and append to a new one or something to produce the output that numpy needs, which puts me back pretty much to the same code I already have. There may be a way to make it work, but so far I haven?t been able to figure it out any better than the code I?ve got followed by converting to a numpy array. You do bring up a good point though: there is quite possibly a better way to do this, and knowing that would be just as good as knowing why multiprocessing doesn?t improve performance. Thanks! --- Israel Brewster Software Engineer Alaska Volcano Observatory Geophysical Institute - UAF 2156 Koyukuk Drive Fairbanks AK 99775-7320 Work: 907-474-5172 cell: 907-328-9145 > > -- > \ ?You don't need a book of any description to help you have some | > `\ kind of moral awareness.? ?Dr. Francesca Stavrakoloulou, bible | > _o__) scholar, 2011-05-08 | > Ben Finney > > -- > https://mail.python.org/mailman/listinfo/python-list From tjreedy at udel.edu Mon Feb 18 23:42:25 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 18 Feb 2019 23:42:25 -0500 Subject: Trying to compile Python 3.5 on Linux Mint 19, getting compiler warnings and failing tests In-Reply-To: References: Message-ID: On 2/18/2019 3:35 PM, Marcin G wrote: > Hmm. From looking at your full log (THANK YOU for posting that, btw - > so many people don't), it looks like an issue with certificate > checking. Might be a bug in the test itself. Does the same thing > happen with a more recent Python build? It could be a weirdness with > specific versions of OpenSSL. > > Hmm, my Googling brought me this: > > https://bugs.python.org/issue30714 > > Apparently, the test fails with newer versions of openssl and the test was fixed in 3.7, 3.6 and 2.7, but for some reason 3.5 was removed from the versions field of this bug report. At the time of the fix, 3.5 had moved to security-fix only status. That is now the status of 3.6. -- Terry Jan Reedy From drake.gossi at gmail.com Tue Feb 19 00:50:57 2019 From: drake.gossi at gmail.com (Drake Gossi) Date: Mon, 18 Feb 2019 21:50:57 -0800 Subject: trying to begin a code for web scraping Message-ID: Hi everyone, I'm trying to write code to scrape this website ( regulations.gov) of its comments, but I'm having trouble figuring out what to link onto in the inspect page (like when I right click on inspect with the mouse). Although I need to write code to scrape all 11,000ish of the comments related to this event (by putting a code in a loop?), I'm still at the stage of looking at individual comments. So, for example, with this comment , I know enough to right click on inspect and to look at the xml? (This is how much of a beginner I am--what am I looking at when I right click inspect?) Then, I control F to find where the comment is in the code. For that comment, the word I used control F on was "troubling." So, I found the comment buried in the xml But my issue is this. I don't know what to link onto to scrape the comment (and I assume that this same sequence of letters would apply to scraping all of the comments in general). I assume what I grab is GIY1LSJISD. I'm watching this video, and the person is linking onto "tr" and "td," but mine is not that easy. In other words, what is the most essential language (bit of xml? code), the copying of which would allow me to extract not only this comment, but all of the comments, were I to put this bit of language(/xml?) my code? ... ... soup.findALL ('?') In sum, what I need to know is, how do I tell my Python code to ignore all of the surrounding code and go straight in and grab the comment. Of course, I need to grab other things too like the name, category, date, and so on, but I haven't gotten that far yet. Right now, I'm just trying to figure out what I need to insert into my code so that I can get the comment. Help! I'm trying to learn code on the fly. I'm an experienced researcher but am new to coding. Any help you could give me would be tremendously awesome. Best, Drake From greg.ewing at canterbury.ac.nz Tue Feb 19 01:09:14 2019 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Tue, 19 Feb 2019 19:09:14 +1300 Subject: What's the address for? In-Reply-To: References: Message-ID: Stefan Ram wrote: > What's so important about the (presumed) address of a > function that it is shown on every stringification of > each function? Its value isn't important at all. It's just a way of distinguishing different objects in debugging output. -- Greg From arj.python at gmail.com Tue Feb 19 02:08:06 2019 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Tue, 19 Feb 2019 11:08:06 +0400 Subject: ajax with pyside webview Message-ID: greetings, i'm using pyside webview to view a localhost app, all works fine, just ajax via jquery does not work. other requests work fine. ajax works fine in web browser though. any ideas? Abdur-Rahmaan Janhangeer http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ Mauritius From mnl.post at gmail.com Mon Feb 18 23:15:19 2019 From: mnl.post at gmail.com (mnl.post at gmail.com) Date: Mon, 18 Feb 2019 23:15:19 -0500 Subject: Feature suggestions: "Using declarations" i.e. context managers ("with" blocks) tied to scope/lifetime of the variable rather than to nesting Message-ID: I thought this new C# feature would be a good thing to add to Python: https://vcsjones.com/2019/01/30/csharp-8-using-declarations/ I find the nesting required by context managers to be at odds with the broad intent. The code indentation in python should reflect the program logic and flow as much as possible, but for thing, the context manager generally doesn't apply to most of the code it forces to be indented, but only to a few lines using the resource; for another, even for those lines, the context manager is usually not reflective of the flow anyway (unlike the classic control statements, such as for/next, if/else, and while). for example: with xxxxxx.open() as logfile: do this do that logfile.write() do this do that logfile.write() This becomes more ugly if multiple withs get nested. From rhodri at kynesim.co.uk Tue Feb 19 06:26:19 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Tue, 19 Feb 2019 11:26:19 +0000 Subject: Feature suggestions: "Using declarations" i.e. context managers ("with" blocks) tied to scope/lifetime of the variable rather than to nesting In-Reply-To: References: Message-ID: <9eebdac4-8e7b-f973-a559-6cea4a942b9c@kynesim.co.uk> This is probably better discussed on python-ideas, but here goes: On 19/02/2019 04:15, mnl.post at gmail.com wrote: > I thought this new C# feature would be a good thing to add to Python: > https://vcsjones.com/2019/01/30/csharp-8-using-declarations/ > > I find the nesting required by context managers to be at odds with the > broad intent. You're going to have to expand on this somewhat, because at the moment I 100% disagree with you. I would much rather be explicit about the code that a context manager applies to than have it magically add itself to some other block. -- Rhodri James *-* Kynesim Ltd From piet-l at vanoostrum.org Tue Feb 19 07:38:01 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Tue, 19 Feb 2019 13:38:01 +0100 Subject: WebScrapping References: <8A2C4E91-98A3-4D00-AEE4-1087493E8632@gmail.com> Message-ID: Adrian Ordona writes: > Hi, > > I?m learning how to code and interested in web scrapping to gather data. > I?m running on Mac OS X 10.9.5 and python 3.7 terminal. > I?m trying to capture the name of the brand and price but i keep getting an error (see below). > Traceback (most recent call last): > File "", line 1, in > File "/anaconda3/lib/python3.7/site-packages/bs4/element.py", line 1884, in __getattr__ > "ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key > AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()? > > > Here?s what i got and thanks for the help > > import bs4 > from urllib.request import urlopen as uReq > from bs4 import BeautifulSoup as soup > > my_url = 'https://www.newegg.com/Desktop-Graphics-Cards/SubCategory/ID-48?Tid=7709' > > uClient = uReq(my_url) > page_html = uClient.read() > uClient.close() > page_soup = soup(page_html, "html.parser") > records = [] > > containers = page_soup.findAll("div",{"class":"item-container"}) > > for container in containers: > brand = container.find('div', attrs={'class':'item-branding'}) > price = container.find('div', attrs={'class':'item-action'}) > records.append((brand, price)) > When I put this in a python file, and run it under python3.7, it works. It seems you were running this line by line interactively, tight? Could it be that you accidentally entered brand = containers.find('div', attrs={'class':'item-branding'}) i.e containers rather than container, because that would generate the error that you copied. -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From stephane at wirtel.be Tue Feb 19 08:23:21 2019 From: stephane at wirtel.be (Stephane Wirtel) Date: Tue, 19 Feb 2019 14:23:21 +0100 Subject: How to detect if a file is executable on Windows? Message-ID: <20190219132321.GA2707@xps> Hi all, on Linux/Unix/BSD, we can detect if a file is an executable with os.access(path, os.X_OK) but this is not the case on Windows. Do you have an idea? Have a nice day, St?phane -- St?phane Wirtel - https://wirtel.be - @matrixise From joel.goldstick at gmail.com Tue Feb 19 08:31:01 2019 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 19 Feb 2019 08:31:01 -0500 Subject: trying to begin a code for web scraping In-Reply-To: References: Message-ID: On Tue, Feb 19, 2019 at 12:52 AM Drake Gossi wrote: > > Hi everyone, > > I'm trying to write code to scrape this website > ( > regulations.gov) of its comments, but I'm having trouble figuring out what > to link onto in the inspect page (like when I right click on inspect with > the mouse). > > Although I need to write code to scrape all 11,000ish of the comments > related to this event (by putting a code in a loop?), I'm still at the > stage of looking at individual comments. So, for example, with this comment > , I know > enough to right click on inspect and to look at the xml? (This is how much > of a beginner I am--what am I looking at when I right click inspect?) Then, > I control F to find where the comment is in the code. For that comment, the > word I used control F on was "troubling." So, I found the comment buried in > the xml > > But my issue is this. I don't know what to link onto to scrape the comment > (and I assume that this same sequence of letters would apply to scraping > all of the comments in general). I assume what I grab is GIY1LSJISD. I'm > watching this video, and the person is linking onto "tr" and "td," but mine > is not that easy. In other words, what is the most essential language (bit > of xml? code), the copying of which would allow me to extract not only this > comment, but all of the comments, were I to put this bit of language(/xml?) > my code? ... ... soup.findALL ('?') > > In sum, what I need to know is, how do I tell my Python code to ignore all > of the surrounding code and go straight in and grab the comment. Of course, > I need to grab other things too like the name, category, date, and so on, > but I haven't gotten that far yet. Right now, I'm just trying to figure out > what I need to insert into my code so that I can get the comment. > > Help! I'm trying to learn code on the fly. I'm an experienced researcher > but am new to coding. Any help you could give me would be tremendously > awesome. > > Best, > Drake > -- > https://mail.python.org/mailman/listinfo/python-list Beautiful soup is your friend here. It can analyze the data within the html tags on your scraped page. But often javascript is used on 'modern' web pages so the page is actually not just html, but javascript that changes the html. For this you need another tool -- i think one is called scrapy. Others here probably have experience with that. Show a small snippet of your code that demonstrates at least one of your coding problems. -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From ikorot01 at gmail.com Mon Feb 18 20:46:31 2019 From: ikorot01 at gmail.com (Igor Korot) Date: Mon, 18 Feb 2019 19:46:31 -0600 Subject: How to detect if a file is executable on Windows? In-Reply-To: <20190219132321.GA2707@xps> References: <20190219132321.GA2707@xps> Message-ID: Hi, On Tue, Feb 19, 2019 at 7:26 AM Stephane Wirtel wrote: > > Hi all, > > on Linux/Unix/BSD, we can detect if a file is an executable with > os.access(path, os.X_OK) but this is not the case on Windows. > > Do you have an idea? Check file extension? Thank you. > > Have a nice day, > > St?phane > > -- > St?phane Wirtel - https://wirtel.be - @matrixise > -- > https://mail.python.org/mailman/listinfo/python-list From Gronicus at SGA.Ninja Tue Feb 19 08:45:33 2019 From: Gronicus at SGA.Ninja (Steve) Date: Tue, 19 Feb 2019 08:45:33 -0500 Subject: ajax with pyside webview In-Reply-To: References: Message-ID: <000001d4c859$63555bd0$2a001370$@SGA.Ninja> Try BonAmi. Footnote: Ultrasound Technician Asks Pregnant Woman If She'd Like To Know Baby's Name -----Original Message----- From: Python-list On Behalf Of Abdur-Rahmaan Janhangeer Sent: Tuesday, February 19, 2019 2:08 AM To: Python Subject: ajax with pyside webview greetings, i'm using pyside webview to view a localhost app, all works fine, just ajax via jquery does not work. other requests work fine. ajax works fine in web browser though. any ideas? Abdur-Rahmaan Janhangeer http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ Mauritius -- https://mail.python.org/mailman/listinfo/python-list From stephane at wirtel.be Tue Feb 19 08:47:55 2019 From: stephane at wirtel.be (Stephane Wirtel) Date: Tue, 19 Feb 2019 14:47:55 +0100 Subject: How to detect if a file is executable on Windows? In-Reply-To: References: <20190219132321.GA2707@xps> Message-ID: <20190219134755.GA5683@xps> And based on a determinist solution? Yes, you can check the extension but sometimes I prefer to be sure. For example, the API of Win32 has the GetTypeBinaryW function, this one could be used but I need to create a mapping, with CPython api, ctypes or CFFI. I could also use the 2 first bytes of a file and determine if the file is a binary because on Windows, the executable files start with b'MZ'. But I wanted an elegant and minimalist solution. Thank you for your proposal. Have a nice day, St?phane On 02/18, Igor Korot wrote: >Hi, > >On Tue, Feb 19, 2019 at 7:26 AM Stephane Wirtel wrote: >> >> Hi all, >> >> on Linux/Unix/BSD, we can detect if a file is an executable with >> os.access(path, os.X_OK) but this is not the case on Windows. >> >> Do you have an idea? > >Check file extension? > >Thank you. > >> >> Have a nice day, >> >> St?phane >> >> -- >> St?phane Wirtel - https://wirtel.be - @matrixise >> -- >> https://mail.python.org/mailman/listinfo/python-list -- St?phane Wirtel - https://wirtel.be - @matrixise From arj.python at gmail.com Tue Feb 19 08:49:23 2019 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Tue, 19 Feb 2019 17:49:23 +0400 Subject: ajax with pyside webview In-Reply-To: <000001d4c859$63555bd0$2a001370$@SGA.Ninja> References: <000001d4c859$63555bd0$2a001370$@SGA.Ninja> Message-ID: you mean? Abdur-Rahmaan Janhangeer http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ Mauritius From jureq at nowhere.no Tue Feb 19 08:58:18 2019 From: jureq at nowhere.no (jureq) Date: 19 Feb 2019 13:58:18 GMT Subject: How to detect if a file is executable on Windows? References: <20190219132321.GA2707@xps> <20190219134755.GA5683@xps> Message-ID: <5c6c0b7a$0$500$65785112@news.neostrada.pl> > I could also use the 2 first bytes of a file and determine if the file > is a binary because on Windows, the executable files start with b'MZ'. Is .bat executable? From rasmussen.bryan at gmail.com Tue Feb 19 09:08:45 2019 From: rasmussen.bryan at gmail.com (bryan rasmussen) Date: Tue, 19 Feb 2019 15:08:45 +0100 Subject: How to detect if a file is executable on Windows? In-Reply-To: <20190219134755.GA5683@xps> References: <20190219132321.GA2707@xps> <20190219134755.GA5683@xps> Message-ID: Well, I used to know the answer to this question but I haven't done this kind of thing since 2009, so hopefully what I suggest is not out of date, so for everything I say here assume I put provisos in like it used to be or whatever. Anyway if the type of a file is executable , for example a file with extension .xyz, is determined by the registry in windows read here and google onward https://docs.microsoft.com/en-us/windows/desktop/shell/app-registration#finding-an-application-executable As I recall somewhere in PyWin32 http://timgolden.me.uk/pywin32-docs/contents.html there was a method to find out if an individual file was an executable. Hope it helps, because that means I still have some usable knowledge in that area! Best Regards, Bryan Rasmussen On Tue, Feb 19, 2019 at 2:49 PM Stephane Wirtel wrote: > And based on a determinist solution? > > Yes, you can check the extension but sometimes I prefer to be sure. > > For example, the API of Win32 has the GetTypeBinaryW function, this one > could be used but I need to create a mapping, with CPython api, ctypes > or CFFI. > > I could also use the 2 first bytes of a file and determine if the file > is a binary because on Windows, the executable files start with b'MZ'. > > But I wanted an elegant and minimalist solution. > > Thank you for your proposal. > > Have a nice day, > > St?phane > > On 02/18, Igor Korot wrote: > >Hi, > > > >On Tue, Feb 19, 2019 at 7:26 AM Stephane Wirtel > wrote: > >> > >> Hi all, > >> > >> on Linux/Unix/BSD, we can detect if a file is an executable with > >> os.access(path, os.X_OK) but this is not the case on Windows. > >> > >> Do you have an idea? > > > >Check file extension? > > > >Thank you. > > > >> > >> Have a nice day, > >> > >> St?phane > >> > >> -- > >> St?phane Wirtel - https://wirtel.be - @matrixise > >> -- > >> https://mail.python.org/mailman/listinfo/python-list > > -- > St?phane Wirtel - https://wirtel.be - @matrixise > -- > https://mail.python.org/mailman/listinfo/python-list > From songbird at anthive.com Tue Feb 19 09:35:39 2019 From: songbird at anthive.com (songbird) Date: Tue, 19 Feb 2019 09:35:39 -0500 Subject: revisiting the "What am I running on?" question References: <7d52f95b-66fd-9919-da2f-25208a57ea85@potatochowder.com> Message-ID: Dan Sommers wrote: ... > Python 3.7.2 (default, Jan 10 2019, 23:51:51) > [GCC 8.2.1 20181127] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> import sys > >>> sys.platform > 'linux' this doesn't help me for the rest of the platforms available. ===== from the docs: if sys.platform.startswith('freebsd'): # FreeBSD-specific code here... elif sys.platform.startswith('linux'): # Linux-specific code here... For other systems, the values are: System platform value Linux 'linux' Windows 'win32' Windows/Cygwin 'cygwin' Mac OS X 'darwin' ===== no win64? no arm(s)? no legs(a joke :)... the above is missing a lot of devices. i looked around for something more complete and didn't come across anything more definitive that looked to be up to date. songbird From songbird at anthive.com Tue Feb 19 09:07:33 2019 From: songbird at anthive.com (songbird) Date: Tue, 19 Feb 2019 09:07:33 -0500 Subject: revisiting the "What am I running on?" question References: Message-ID: <56ftjf-ui1.ln1@anthive.com> Rick Johnson wrote: ... > The standard idiom for regexp match objects is to test for truthiness, not to use an except-clause. > > m = re.search("^[A-Za-z]:", sysprobetmp) > if m: > ... ok, thanks! :) songbird From songbird at anthive.com Tue Feb 19 09:09:24 2019 From: songbird at anthive.com (songbird) Date: Tue, 19 Feb 2019 09:09:24 -0500 Subject: revisiting the "What am I running on?" question References: <42f0bf8c-ae4a-d983-0642-e472f2504ce7@mrabarnett.plus.com> Message-ID: MRAB wrote: ... > Don't use a bare except, it'll catch _any_ exception. that's ok with this code IMO, but i see your point. > If the regex matches, re.search will return a match object; if it > doesn't match, it'll return None. > > In any case, +1 to Dan's answer. > > [snip] ok, thanks! songbird From songbird at anthive.com Tue Feb 19 09:06:30 2019 From: songbird at anthive.com (songbird) Date: Tue, 19 Feb 2019 09:06:30 -0500 Subject: Gtk. GUI app pgming References: Message-ID: <64ftjf-ui1.ln1@anthive.com> nn wrote: > > any gtk+ III. buffs around ? > > am gonna launch a hot new newsreader soon. works like a charm. i used python 3, gtk3 (PyGObject which brings you pycairo) and pyglet for a quick game and it went ok. the issues i have now are with the actual distribution to all the platforms because i don't have a Windows and a few other architectures to generate my own binary wheels for uploading or to test on. in the meantime people have to install python 3 along with a C compiler and i don't think that really is easy for people who are at the plain "user level" of understanding. i have a few glitches because of the mixing of window layers going on (the application using one idea of windowing things and priorities that gtk may be using) but it isn't bad enough that i've dug into it to sort it out yet. it mostly works. :) https://pypi.org/project/ngfp/ the python 3 code itself is rather atrocious in parts but that's a whole different issue... lol (i'm still obviously learning :) ) songbird From rosuav at gmail.com Tue Feb 19 09:59:48 2019 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 20 Feb 2019 01:59:48 +1100 Subject: revisiting the "What am I running on?" question In-Reply-To: References: <7d52f95b-66fd-9919-da2f-25208a57ea85@potatochowder.com> Message-ID: On Wed, Feb 20, 2019 at 1:51 AM songbird wrote: > For other systems, the values are: > System platform value > Linux 'linux' > Windows 'win32' > Windows/Cygwin 'cygwin' > Mac OS X 'darwin' > > ===== > > no win64? The value "win32" means Windows (or, more technically, "Windows NT family", as opposed to Win95/Win98 - but since Win XP, that's the only type of Windows there is). If you actually care about whether it's a 32-bit or 64-bit OS, you can look at sys.maxsize. > no arm(s)? That's a CPU architecture. What OS would you be running on your ARM? If it's Windows, "win32". If it's Linux, "linux". Etc. ChrisA From rosuav at gmail.com Tue Feb 19 10:04:00 2019 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 20 Feb 2019 02:04:00 +1100 Subject: revisiting the "What am I running on?" question In-Reply-To: References: <42f0bf8c-ae4a-d983-0642-e472f2504ce7@mrabarnett.plus.com> Message-ID: On Wed, Feb 20, 2019 at 1:59 AM songbird wrote: > > MRAB wrote: > ... > > Don't use a bare except, it'll catch _any_ exception. > > that's ok with this code IMO, but i see your point. > Not really, no. It means that ANY bug (barring an outright syntax error) inside the try block will silently move you on to the next check, possibly after printing out the message. Suppose you misspelled "return". result = re.search("^/(tmp)|(var)|(usr)|(opt)|(home)", sysprobetmp) try: print ("Result : -->" + result.group(0) + "<--\n") retun ("posix") except: pass It'll print out the result, bomb with a NameError, swallow the NameError, and silently move on. The *only* times you should use a bare except clause are with a "raise" inside it, or with a log-and-return boundary marker (where "inner" code isn't allowed to crash "outer" code, eg in a web app) - and the "log" part is absolutely essential. ChrisA From rosuav at gmail.com Tue Feb 19 10:06:20 2019 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 20 Feb 2019 02:06:20 +1100 Subject: revisiting the "What am I running on?" question In-Reply-To: References: <42f0bf8c-ae4a-d983-0642-e472f2504ce7@mrabarnett.plus.com> Message-ID: On Wed, Feb 20, 2019 at 2:04 AM Chris Angelico wrote: > > On Wed, Feb 20, 2019 at 1:59 AM songbird wrote: > > > > MRAB wrote: > > ... > > > Don't use a bare except, it'll catch _any_ exception. > > > > that's ok with this code IMO, but i see your point. > > > > Not really, no. It means that ANY bug (barring an outright syntax > error) inside the try block will silently move you on to the next > check, possibly after printing out the message. Oh, and not just bugs either. If the user hits Ctrl-C at just the right moment, KeyboardInterrupt will be raised. You'll swallow that exception silently, preventing the user-requested halt, and going and doing the wrong thing. Don't use a bare except clause even if your code is 100% perfect every time. (And anyone who thinks their code is perfect hasn't tested it.) ChrisA From rhodri at kynesim.co.uk Tue Feb 19 10:06:49 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Tue, 19 Feb 2019 15:06:49 +0000 Subject: revisiting the "What am I running on?" question In-Reply-To: References: <7d52f95b-66fd-9919-da2f-25208a57ea85@potatochowder.com> Message-ID: <72bf72fc-e7fc-8080-167c-17fb2cb04b5f@kynesim.co.uk> On 19/02/2019 14:35, songbird wrote: > Dan Sommers wrote: > ... >> Python 3.7.2 (default, Jan 10 2019, 23:51:51) >> [GCC 8.2.1 20181127] on linux >> Type "help", "copyright", "credits" or "license" for more information. >>>>> import sys >>>>> sys.platform >> 'linux' > > this doesn't help me for the rest of the platforms > available. > > > ===== from the docs: > > if sys.platform.startswith('freebsd'): > # FreeBSD-specific code here... > elif sys.platform.startswith('linux'): > # Linux-specific code here... > > For other systems, the values are: > System platform value > Linux 'linux' > Windows 'win32' > Windows/Cygwin 'cygwin' > Mac OS X 'darwin' > > ===== > > no win64? > no arm(s)? > > no legs(a joke :)... > > the above is missing a lot of devices. It isn't listing devices, it's listing platforms. Linux could be running on an ARM, an x64 chip, or probably a few other chips too. What *specifically* do you want, and why? -- Rhodri James *-* Kynesim Ltd From neilc at norwich.edu Tue Feb 19 10:42:00 2019 From: neilc at norwich.edu (Neil Cerutti) Date: 19 Feb 2019 15:42:00 GMT Subject: Feature suggestions: "Using declarations" i.e. context managers ("with" blocks) tied to scope/lifetime of the variable rather than to nesting References: Message-ID: On 2019-02-19, mnl.post at gmail.com wrote: > I thought this new C# feature would be a good thing to add to Python: > https://vcsjones.com/2019/01/30/csharp-8-using-declarations/ > > I find the nesting required by context managers to be at odds with the > broad intent. > > The code indentation in python should reflect the program logic and > flow as much as possible, but for thing, the context manager generally > doesn't apply to most of the code it forces to be indented, but only > to a few lines using the resource; for another, even for those lines, > the context manager is usually not reflective of the flow anyway > (unlike the classic control statements, such as for/next, if/else, and > while). > for example: > with xxxxxx.open() as logfile: > do this > do that > logfile.write() > do this > do that > logfile.write() > > This becomes more ugly if multiple withs get nested. You don't have to nest them. Check out contextlib.ExitStack. ExitStack is designed to handle situations where you don't always want to enter some context, or you are entering a large number of them. -- Neil Cerutti From grant.b.edwards at gmail.com Tue Feb 19 11:18:51 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Tue, 19 Feb 2019 16:18:51 -0000 (UTC) Subject: What's the address for? References: <004801d4c7c4$ecadaf30$c6090d90$@verizon.net> <865ztg5zqk.fsf@benfinney.id.au> Message-ID: On 2019-02-19, Ben Finney wrote: >> On the implementation I am using, the ID changes if I do this: > > You are creating new objects and binding the name ?a? to different > objects in succession. Those different objects will each have different > identities. Those object _may_ each have different identies. Is it required that they do? If an assignment is done on name referring to an object with no other references, would it be allowed that the "old" object is destroyed and its ID reused for a new object being created by the RHS of the assignment? Admittedly, that would be a rather odd way to do things. One would expect that first the RHS is evaluated (perhaps creating a new object), then the name is rebound. -- Grant Edwards grant.b.edwards Yow! I wish I was a at sex-starved manicurist gmail.com found dead in the Bronx!! From grant.b.edwards at gmail.com Tue Feb 19 11:25:01 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Tue, 19 Feb 2019 16:25:01 -0000 (UTC) Subject: revisiting the "What am I running on?" question References: <42f0bf8c-ae4a-d983-0642-e472f2504ce7@mrabarnett.plus.com> Message-ID: On 2019-02-19, Chris Angelico wrote: > Oh, and not just bugs either. If the user hits Ctrl-C at just the > right moment, KeyboardInterrupt will be raised. You'll swallow that > exception silently, preventing the user-requested halt, and going and > doing the wrong thing. Don't use a bare except clause even if your > code is 100% perfect every time. (And anyone who thinks their code is > perfect hasn't tested it.) IMO, you're allowed to use a bare except clause to do cleanup or logging as long as the execption handler ends with the line raise Something like this: -- Grant Edwards grant.b.edwards Yow! I just went below the at poverty line! gmail.com From rhodri at kynesim.co.uk Tue Feb 19 06:20:52 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Tue, 19 Feb 2019 11:20:52 +0000 Subject: The NaNny State In-Reply-To: <000c01d4c7e1$24140d90$6c3c28b0$@verizon.net> References: <004001d4c7bf$e2559ed0$a700dc70$@verizon.net> <86a7is698v.fsf@benfinney.id.au> <000c01d4c7e1$24140d90$6c3c28b0$@verizon.net> Message-ID: <8a4d02ef-a7f3-b9b9-06d4-562eb98379da@kynesim.co.uk> On 18/02/2019 23:24, Avi Gross wrote: > Ben asked: > >> Who says that the ?correct spelling in python is all lower case "nan"?? > Fair enough. Except for reserved words in the language, all we have is hints of what the designers used. The reality is that whoever codes the __str__ and/or __repr__ chose lower case and so did the persons who made math.nan and numpy.nan and obviously we can over-ride these decisions in our own derived customized classes. [cut large amount of experimentation showing nothing much] You still seem to be hung up on the concept of "correct spelling". In this case, it really isn't helpful. -- Rhodri James *-* Kynesim Ltd From tjol at tjol.eu Tue Feb 19 06:46:46 2019 From: tjol at tjol.eu (Thomas Jollans) Date: Tue, 19 Feb 2019 12:46:46 +0100 Subject: Feature suggestions: "Using declarations" i.e. context managers ("with" blocks) tied to scope/lifetime of the variable rather than to nesting In-Reply-To: References: Message-ID: <50e2b3c6-d5dc-d9e8-98d9-ecf4fd12db12@tjol.eu> On 19/02/2019 05.15, mnl.post at gmail.com wrote: > This becomes more ugly if multiple withs get nested. > This is what contextlib.ExitStack is for. From rosuav at gmail.com Tue Feb 19 12:58:52 2019 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 20 Feb 2019 04:58:52 +1100 Subject: What's the address for? In-Reply-To: References: <004801d4c7c4$ecadaf30$c6090d90$@verizon.net> <865ztg5zqk.fsf@benfinney.id.au> Message-ID: On Wed, Feb 20, 2019 at 3:23 AM Grant Edwards wrote: > > On 2019-02-19, Ben Finney wrote: > > >> On the implementation I am using, the ID changes if I do this: > > > > You are creating new objects and binding the name ?a? to different > > objects in succession. Those different objects will each have different > > identities. > > Those object _may_ each have different identies. Is it required that > they do? If an assignment is done on name referring to an object with > no other references, would it be allowed that the "old" object is > destroyed and its ID reused for a new object being created by the RHS > of the assignment? > > Admittedly, that would be a rather odd way to do things. One would > expect that first the RHS is evaluated (perhaps creating a new > object), then the name is rebound. > Correct, the RHS is evaluated first. So the old object is still around (important for statements like "x = x.lower()"). ChrisA From rosuav at gmail.com Tue Feb 19 13:07:42 2019 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 20 Feb 2019 05:07:42 +1100 Subject: revisiting the "What am I running on?" question In-Reply-To: References: <42f0bf8c-ae4a-d983-0642-e472f2504ce7@mrabarnett.plus.com> Message-ID: On Wed, Feb 20, 2019 at 3:26 AM Grant Edwards wrote: > > On 2019-02-19, Chris Angelico wrote: > > > Oh, and not just bugs either. If the user hits Ctrl-C at just the > > right moment, KeyboardInterrupt will be raised. You'll swallow that > > exception silently, preventing the user-requested halt, and going and > > doing the wrong thing. Don't use a bare except clause even if your > > code is 100% perfect every time. (And anyone who thinks their code is > > perfect hasn't tested it.) > > IMO, you're allowed to use a bare except clause to do cleanup or > logging as long as the execption handler ends with the line > > raise > Yeah, I was clearer about that in the first email, to which that was a followup, but I'll give two examples of valid bare-except constructs: 1) Handle-and-raise try: weird stuff except: show failure state raise 2) Log-and-continue while True: try: thing = get_next_thing() except NoMoreThings, KeyboardInterrupt: break try: response = some_handler(thing) except: response = 500 log_error() try: thing.respond(response) except OSError: pass The implication of the second one is that (a) bugs in some_handler() shouldn't break the whole program, and (b) attempting to halt some_handler() with Ctrl-C shouldn't halt the whole program. It creates a boundary. But you should ALWAYS either reraise or log the exception. ALWAYS. Otherwise, be specific about what you're swallowing. ChrisA From rosuav at gmail.com Tue Feb 19 13:13:04 2019 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 20 Feb 2019 05:13:04 +1100 Subject: How to detect if a file is executable on Windows? In-Reply-To: References: <20190219132321.GA2707@xps> <20190219134755.GA5683@xps> <5c6c0b7a$0$500$65785112@news.neostrada.pl> Message-ID: On Wed, Feb 20, 2019 at 5:05 AM Dennis Lee Bieber wrote: > > On 19 Feb 2019 13:58:18 GMT, jureq declaimed the > following: > > >> I could also use the 2 first bytes of a file and determine if the file > >> is a binary because on Windows, the executable files start with b'MZ'. > > > >Is .bat executable? > > Or any of the extensions on > PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.py;.pyw > I guess you have to define the question better for Windows, since there's no single definition of "executable". If you mean "typing just the base name of this file at the shell will result in it being run", then PATHEXT is the correct answer. If you mean "this thing is actually inherently executable", then you probably want to check if it begins MZ, but that's not certain (COM files still seem to be supported, and they have no header whatsoever). If you mean "double-clicking this thing will run it", I think there are tools that allow you to do the registry lookup conveniently to see if something's associated. ChrisA From grant.b.edwards at gmail.com Tue Feb 19 13:26:12 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Tue, 19 Feb 2019 18:26:12 -0000 (UTC) Subject: What's the address for? References: <004801d4c7c4$ecadaf30$c6090d90$@verizon.net> <865ztg5zqk.fsf@benfinney.id.au> Message-ID: On 2019-02-19, Chris Angelico wrote: > On Wed, Feb 20, 2019 at 3:23 AM Grant Edwards wrote: > >> Those object _may_ each have different identies. Is it required that >> they do? If an assignment is done on name referring to an object with >> no other references, would it be allowed that the "old" object is >> destroyed and its ID reused for a new object being created by the RHS >> of the assignment? >> >> Admittedly, that would be a rather odd way to do things. One would >> expect that first the RHS is evaluated (perhaps creating a new >> object), then the name is rebound. > > Correct, the RHS is evaluated first. So the old object is still > around (important for statements like "x = x.lower()"). Not doing the unbinding until after the RHS evaluation is also important for preventing surprises due to exceptions raised while the RHS is evaluated. One would reasonably expect that if that happens the original binding of the name on the LHS is unchanged. Dunno if that's actually specified anywhere... -- Grant Edwards grant.b.edwards Yow! Someone in DAYTON, at Ohio is selling USED gmail.com CARPETS to a SERBO-CROATIAN From grant.b.edwards at gmail.com Tue Feb 19 13:31:14 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Tue, 19 Feb 2019 18:31:14 -0000 (UTC) Subject: How to detect if a file is executable on Windows? References: <20190219132321.GA2707@xps> <20190219134755.GA5683@xps> <5c6c0b7a$0$500$65785112@news.neostrada.pl> Message-ID: On 2019-02-19, Chris Angelico wrote: > On Wed, Feb 20, 2019 at 5:05 AM Dennis Lee Bieber wrote: >> >> On 19 Feb 2019 13:58:18 GMT, jureq declaimed the >> following: >> >> >> I could also use the 2 first bytes of a file and determine if the file >> >> is a binary because on Windows, the executable files start with b'MZ'. >> > >> >Is .bat executable? >> >> Or any of the extensions on >> PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.py;.pyw > > I guess you have to define the question better for Windows, since > there's no single definition of "executable". If you mean "typing just > the base name of this file at the shell will result in it being run", > then PATHEXT is the correct answer. If you mean "this thing is > actually inherently executable", then you probably want to check if it > begins MZ, but that's not certain (COM files still seem to be > supported, and they have no header whatsoever). If you mean > "double-clicking this thing will run it", I think there are tools that > allow you to do the registry lookup conveniently to see if something's > associated. FWIW, I've noticed that afer downloading a .exe under Linux and scp'ing the file to a Windows machine, it wont run when double-clicked until I fire up a Cygwin shell and do a chmod +x .exe [I assume there's native Windows point-and-grunt means for doing that as well.] So, in addition to the suffix and associations, there's some sort of file-system meta-data that determines whether a file is "executable" in some contexts. -- Grant Edwards grant.b.edwards Yow! I'm having a at quadrophonic sensation gmail.com of two winos alone in a steel mill! From rosuav at gmail.com Tue Feb 19 13:32:09 2019 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 20 Feb 2019 05:32:09 +1100 Subject: What's the address for? In-Reply-To: References: <004801d4c7c4$ecadaf30$c6090d90$@verizon.net> <865ztg5zqk.fsf@benfinney.id.au> Message-ID: On Wed, Feb 20, 2019 at 5:28 AM Grant Edwards wrote: > > On 2019-02-19, Chris Angelico wrote: > > On Wed, Feb 20, 2019 at 3:23 AM Grant Edwards wrote: > > > >> Those object _may_ each have different identies. Is it required that > >> they do? If an assignment is done on name referring to an object with > >> no other references, would it be allowed that the "old" object is > >> destroyed and its ID reused for a new object being created by the RHS > >> of the assignment? > >> > >> Admittedly, that would be a rather odd way to do things. One would > >> expect that first the RHS is evaluated (perhaps creating a new > >> object), then the name is rebound. > > > > Correct, the RHS is evaluated first. So the old object is still > > around (important for statements like "x = x.lower()"). > > Not doing the unbinding until after the RHS evaluation is also > important for preventing surprises due to exceptions raised while the > RHS is evaluated. One would reasonably expect that if that happens > the original binding of the name on the LHS is unchanged. > > Dunno if that's actually specified anywhere... > It is, yes: https://docs.python.org/3/reference/expressions.html#evaluation-order ChrisA From bgailer at gmail.com Tue Feb 19 13:46:47 2019 From: bgailer at gmail.com (Bob Gailer) Date: Tue, 19 Feb 2019 13:46:47 -0500 Subject: ajax with pyside webview In-Reply-To: References: Message-ID: I assume you are referring to QT webview. "ajax via jquery does not work." Can you be more specific? what is the evidence that it's not working? It sounds like you've got a server running on your Local Host. What does the server tell you about its side of that Ajax request? From Karsten.Hilbert at gmx.net Tue Feb 19 14:03:06 2019 From: Karsten.Hilbert at gmx.net (Karsten Hilbert) Date: Tue, 19 Feb 2019 20:03:06 +0100 Subject: Aw: Re: How to detect if a file is executable on Windows? In-Reply-To: References: <20190219132321.GA2707@xps> <20190219134755.GA5683@xps> <5c6c0b7a$0$500$65785112@news.neostrada.pl> Message-ID: > I guess you have to define the question better for Windows, since > there's no single definition of "executable". If you mean "typing just > the base name of this file at the shell will result in it being run", > then PATHEXT is the correct answer. If you mean "this thing is > actually inherently executable", then you probably want to check if it > begins MZ, but that's not certain (COM files still seem to be > supported, and they have no header whatsoever). If you mean > "double-clicking this thing will run it", I think there are tools that > allow you to do the registry lookup conveniently to see if something's > associated. As far as that goes, pretty much the same is true for UNIX (sans the registry thingy, unless you count in binfmt for that). The executable flag doesn't mean it is actually executable, only that it is meant to, and that the kernel is supposed to try. Karsten From arj.python at gmail.com Tue Feb 19 14:06:57 2019 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Tue, 19 Feb 2019 23:06:57 +0400 Subject: ajax with pyside webview In-Reply-To: References: Message-ID: Great, yes Qt webview with pyside. well for backend, flask for front end using Jquery to fetch items i implemented an instant search. on the browser when i type, i get results. on the webview, as i type, i get no results. serverside logs tell no ajax request has been made, i assume a js block somewhere maybe? Abdur-Rahmaan Janhangeer http://www.pythonmembers.club | https://github.com/Abdur-rahmaanJ Mauritius > From python at mrabarnett.plus.com Tue Feb 19 14:07:12 2019 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 19 Feb 2019 19:07:12 +0000 Subject: How to detect if a file is executable on Windows? In-Reply-To: References: <20190219132321.GA2707@xps> <20190219134755.GA5683@xps> <5c6c0b7a$0$500$65785112@news.neostrada.pl> Message-ID: On 2019-02-19 18:31, Grant Edwards wrote: > On 2019-02-19, Chris Angelico wrote: >> On Wed, Feb 20, 2019 at 5:05 AM Dennis Lee Bieber wrote: >>> >>> On 19 Feb 2019 13:58:18 GMT, jureq declaimed the >>> following: >>> >>> >> I could also use the 2 first bytes of a file and determine if the file >>> >> is a binary because on Windows, the executable files start with b'MZ'. >>> > >>> >Is .bat executable? >>> >>> Or any of the extensions on >>> PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.py;.pyw >> >> I guess you have to define the question better for Windows, since >> there's no single definition of "executable". If you mean "typing just >> the base name of this file at the shell will result in it being run", >> then PATHEXT is the correct answer. If you mean "this thing is >> actually inherently executable", then you probably want to check if it >> begins MZ, but that's not certain (COM files still seem to be >> supported, and they have no header whatsoever). If you mean >> "double-clicking this thing will run it", I think there are tools that >> allow you to do the registry lookup conveniently to see if something's >> associated. > > FWIW, I've noticed that afer downloading a .exe under Linux and > scp'ing the file to a Windows machine, it wont run when double-clicked > until I fire up a Cygwin shell and do a > > chmod +x .exe > > [I assume there's native Windows point-and-grunt means for doing that > as well.] > > So, in addition to the suffix and associations, there's some sort of > file-system meta-data that determines whether a file is "executable" > in some contexts. > I've never heard of a Windows equivalent of "chmod +x". If I copy the contents of a .exe into a new file and give it a .exe extension, it just works. From adrian.ordona at gmail.com Tue Feb 19 14:20:56 2019 From: adrian.ordona at gmail.com (Adrian Ordona) Date: Tue, 19 Feb 2019 11:20:56 -0800 Subject: WebScrapping In-Reply-To: References: <8A2C4E91-98A3-4D00-AEE4-1087493E8632@gmail.com> Message-ID: <4330B342-2815-4856-A52A-5D41C0624F77@gmail.com> Thanks Piet for the quick response. Yes, i was running it line by line. When i ran >>> len(containers), the output is giving me 37 but when i ran the code as a py file, it was only showing one result. Thanks in advance. Adrian On Feb 19, 2019, at 4:38 AM, Piet van Oostrum wrote: > Adrian Ordona writes: > >> Hi, >> >> I?m learning how to code and interested in web scrapping to gather data. >> I?m running on Mac OS X 10.9.5 and python 3.7 terminal. >> I?m trying to capture the name of the brand and price but i keep getting an error (see below). >> Traceback (most recent call last): >> File "", line 1, in >> File "/anaconda3/lib/python3.7/site-packages/bs4/element.py", line 1884, in __getattr__ >> "ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key >> AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()? >> >> >> Here?s what i got and thanks for the help >> >> import bs4 >> from urllib.request import urlopen as uReq >> from bs4 import BeautifulSoup as soup >> >> my_url = 'https://www.newegg.com/Desktop-Graphics-Cards/SubCategory/ID-48?Tid=7709' >> >> uClient = uReq(my_url) >> page_html = uClient.read() >> uClient.close() >> page_soup = soup(page_html, "html.parser") >> records = [] >> >> containers = page_soup.findAll("div",{"class":"item-container"}) >> >> for container in containers: >> brand = container.find('div', attrs={'class':'item-branding'}) >> price = container.find('div', attrs={'class':'item-action'}) >> records.append((brand, price)) >> > When I put this in a python file, and run it under python3.7, it works. > It seems you were running this line by line interactively, tight? > > Could it be that you accidentally entered > brand = containers.find('div', attrs={'class':'item-branding'}) > i.e containers rather than container, because that would generate the error that you copied. > -- > Piet van Oostrum > WWW: http://piet.vanoostrum.org/ > PGP key: [8DAE142BE17999C4] > -- > https://mail.python.org/mailman/listinfo/python-list From tjreedy at udel.edu Tue Feb 19 14:27:54 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 19 Feb 2019 14:27:54 -0500 Subject: revisiting the "What am I running on?" question In-Reply-To: References: <42f0bf8c-ae4a-d983-0642-e472f2504ce7@mrabarnett.plus.com> Message-ID: On 2/19/2019 10:06 AM, Chris Angelico wrote: > On Wed, Feb 20, 2019 at 2:04 AM Chris Angelico wrote: >> >> On Wed, Feb 20, 2019 at 1:59 AM songbird wrote: >>> >>> MRAB wrote: >>> ... >>>> Don't use a bare except, it'll catch _any_ exception. >>> >>> that's ok with this code IMO, but i see your point. >>> >> >> Not really, no. It means that ANY bug (barring an outright syntax >> error) inside the try block will silently move you on to the next >> check, possibly after printing out the message. > > Oh, and not just bugs either. If the user hits Ctrl-C at just the > right moment, KeyboardInterrupt will be raised. You'll swallow that > exception silently, preventing the user-requested halt, and going and > doing the wrong thing. KeyboardInterrupt, SystemExit, and GeneratorExit are BaseExceptions but not Exceptions -- Terry Jan Reedy From rosuav at gmail.com Tue Feb 19 14:34:43 2019 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 20 Feb 2019 06:34:43 +1100 Subject: revisiting the "What am I running on?" question In-Reply-To: References: <42f0bf8c-ae4a-d983-0642-e472f2504ce7@mrabarnett.plus.com> Message-ID: On Wed, Feb 20, 2019 at 6:29 AM Terry Reedy wrote: > > On 2/19/2019 10:06 AM, Chris Angelico wrote: > > On Wed, Feb 20, 2019 at 2:04 AM Chris Angelico wrote: > >> > >> On Wed, Feb 20, 2019 at 1:59 AM songbird wrote: > >>> > >>> MRAB wrote: > >>> ... > >>>> Don't use a bare except, it'll catch _any_ exception. > >>> > >>> that's ok with this code IMO, but i see your point. > >>> > >> > >> Not really, no. It means that ANY bug (barring an outright syntax > >> error) inside the try block will silently move you on to the next > >> check, possibly after printing out the message. > > > > Oh, and not just bugs either. If the user hits Ctrl-C at just the > > right moment, KeyboardInterrupt will be raised. You'll swallow that > > exception silently, preventing the user-requested halt, and going and > > doing the wrong thing. > > KeyboardInterrupt, SystemExit, and GeneratorExit are BaseExceptions but > not Exceptions > Yes, and "except:" will catch them. Of them, KeyboardInterrupt is the easiest example, as it can be caused by something outside the Python program's control, has specific user-facing meaning, and can happen literally anywhere (IIRC it's "between any two Python bytecodes", in CPython). Suppressing KeyboardInterrupt can be done explicitly, but should not be done accidentally. ChrisA From grant.b.edwards at gmail.com Tue Feb 19 14:38:00 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Tue, 19 Feb 2019 19:38:00 -0000 (UTC) Subject: How to detect if a file is executable on Windows? References: <20190219132321.GA2707@xps> <20190219134755.GA5683@xps> <5c6c0b7a$0$500$65785112@news.neostrada.pl> Message-ID: On 2019-02-19, MRAB wrote: > On 2019-02-19 18:31, Grant Edwards wrote: > >> FWIW, I've noticed that afer downloading a .exe under Linux and >> scp'ing the file to a Windows machine, it wont run when double-clicked >> until I fire up a Cygwin shell and do a >> >> chmod +x .exe >> >> [I assume there's native Windows point-and-grunt means for doing that >> as well.] >> >> So, in addition to the suffix and associations, there's some sort of >> file-system meta-data that determines whether a file is "executable" >> in some contexts. >> > I've never heard of a Windows equivalent of "chmod +x". Modern Windows filesystems have a complex set of file-level access control mechanisms. > If I copy the contents of a .exe into a new file and give it a .exe > extension, it just works. I assume that what I see is a side-effect of the fact that the sshd in question is a Cygwin app, and it is creating files w/o the Posix "x" mode bits set. In the Cygwin libraries, the Posix permission bits for a directory entry get translated into the Windows ACLs for each file: https://superuser.com/questions/954211/how-does-chmod-x-work-in-cygwin -- Grant Edwards grant.b.edwards Yow! BARBARA STANWYCK makes at me nervous!! gmail.com From ben+python at benfinney.id.au Tue Feb 19 23:42:59 2019 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 20 Feb 2019 15:42:59 +1100 Subject: Feature suggestions: "Using declarations" i.e. context managers ("with" blocks) tied to scope/lifetime of the variable rather than to nesting References: Message-ID: <86wolv3wa4.fsf@benfinney.id.au> "mnl.post at gmail.com" writes: > with xxxxxx.open() as logfile: > do this > do that > logfile.write() > do this > do that > logfile.write() That's a good sign you have identified a discrete collection of statements that should be in a function. The function accepts ?logfile? as a (probably named) parameter, so that it is explit the opening and closing of that file is someone else's responsibility This, as you say, simplifies the code in those statements. Bonus: you can name the function to summarise its entire purpose:: with foo.open() as logfile: frobnicate_the_whelk(logfile=logfile) -- \ ?I don't want to live peacefully with difficult realities, and | `\ I see no virtue in savoring excuses for avoiding a search for | _o__) real answers.? ?Paul Z. Myers, 2009-09-12 | Ben Finney From eryksun at gmail.com Wed Feb 20 01:39:29 2019 From: eryksun at gmail.com (eryk sun) Date: Wed, 20 Feb 2019 00:39:29 -0600 Subject: How to detect if a file is executable on Windows? In-Reply-To: References: <20190219132321.GA2707@xps> <20190219134755.GA5683@xps> <5c6c0b7a$0$500$65785112@news.neostrada.pl> Message-ID: On 2/19/19, Chris Angelico wrote: > > I guess you have to define the question better for Windows, since > there's no single definition of "executable". If you mean "typing just > the base name of this file at the shell will result in it being run", > then PATHEXT is the correct answer. If you mean "this thing is PATHEXT is the list of extensions that the shell appends automatically when searching PATH. Otherwise it has nothing to do with executability. I think older versions of PowerShell confused this. But I just checked PowerShell in Windows 10, and I can run a .txt file found in PATH without requiring .TXT in PATHEXT so long as I search for the name including the extension. CMD has worked like this forever. Where CMD and PowerShell differ is with respect to execute access. In CMD, when CreateProcess fails due to access denied, it gives up instead of trying ShellExecuteEx. PowerShell tries ShellExecuteEx even if CreateProcess fails with access denied. What CMD does makes more sense to me. But neither is as sensible as a POSIX shell, which continues searching PATH if a match doesn't have execute access. > actually inherently executable", then you probably want to check if it > begins MZ, but that's not certain (COM files still seem to be > supported, and they have no header whatsoever). If you mean PE executables can have any extension. CreateProcess doesn't care, but when searching for the executable, it only tries appending .EXE if we omit the extension. In other words, if we run "spam", it will find "spam.exe" but not "spam.com". Note that the extensions in PATHEXT are a high-level shell feature, as discussed above. This variable isn't used by the base Windows API. Windows comes with a few programs that use the .COM extension for backward compatibility: chcp.com, format.com, mode.com, more.com, and tree.com. In 64-bit Windows, these are 64-bit PE executables. Actual 16-bit MS-DOS files cannot run in vanilla 64-bit Windows since the NT Virtual DOS Machine (ntvdm.exe) isn't supported. If we want to know whether CreateProcess will succeed without actually calling it, then we need to check for execute access via AccessCheck, which requires the security descriptor from GetNamedSecurityInfo (OWNER, DACL and LABEL info) and current access token from OpenProcessToken, or OpenThreadToken if impersonating. Call GetTokenInformation on the token to determine whether we have admin access, which may be required. Verify that it's a valid image via GetBinaryType. Once we know we have a PE image, map it as a data file via LoadLibraryEx and get the embedded manifest via FindResource/LoadResource in order to determine whether admin access is required (i.e. requestedExecutionLevel, requireAdministrator). If there's no embedded manifest, check for one beside it named .manifest, e.g. "spam.exe.,manifest". Or we can simply call CreateProcess with the flag CREATE_SUSPENDED (4). Let the OS do all of the work for us. If it succeeds, call TerminateProcess, and close the process and thread handles. > "double-clicking this thing will run it", I think there are tools that > allow you to do the registry lookup conveniently to see if something's > associated. Call AssocQueryString to have the shell comb through the rat's nest of registry definitions to determine the ASSOCSTR_COMMAND template. (Whatever we think we know about the shell's use of the registry, we probably don't know the half of it. It is a nightmare.) Use the flag ASSOCF_INIT_IGNOREUNKNOWN to prevent returning the "unknown" progid that runs openwith.exe. We can split the template into the CreateProcess parameters lpApplicationName and lpCommandLine via SHEvaluateSystemCommandTemplate. We still have to implement our own template parameter substitution for the "%1" (i.e. "%l" or "%L") target and the %* remaining command-line arguments. From eryksun at gmail.com Wed Feb 20 02:13:01 2019 From: eryksun at gmail.com (eryk sun) Date: Wed, 20 Feb 2019 01:13:01 -0600 Subject: revisiting the "What am I running on?" question In-Reply-To: References: <7d52f95b-66fd-9919-da2f-25208a57ea85@potatochowder.com> Message-ID: On 2/19/19, Chris Angelico wrote: > > The value "win32" means Windows (or, more technically, "Windows NT > family", as opposed to Win95/Win98 - but since Win XP, that's the only > type of Windows there is). If you actually care about whether it's a > 32-bit or 64-bit OS, you can look at sys.maxsize. I didn't start using Python until long after I stopped using Windows 9x. Do you recall what sys.platform was for Windows 9x? It was also the Win32 API, but layered over a 32-bit VMM that extended/replaced 16-bit DOS. There were many differences between it and the NT implementation of Win32, such as no security or Unicode, and a completely different console subsystem. IMO, "win32" was never the right platform name, but we're stuck with it. The correct platform name is simply "windows". Win32 is also the legacy API name. Nowadays the preferred name is the Windows API, or WINAPI for short. Win32 is also the legacy 32-bit ABI name. It's an okay name, but I'd prefer if we could change it to "win-ia32", which would parallel the 64-bit ABI name "win-amd64". From tjol at tjol.eu Wed Feb 20 06:48:22 2019 From: tjol at tjol.eu (Thomas Jollans) Date: Wed, 20 Feb 2019 12:48:22 +0100 Subject: revisiting the "What am I running on?" question In-Reply-To: References: <7d52f95b-66fd-9919-da2f-25208a57ea85@potatochowder.com> Message-ID: <272619c5-9a99-dbb9-2f7e-939baf563a5b@tjol.eu> On 20/02/2019 08.13, eryk sun wrote: > On 2/19/19, Chris Angelico wrote: >> >> The value "win32" means Windows (or, more technically, "Windows NT >> family", as opposed to Win95/Win98 - but since Win XP, that's the only >> type of Windows there is). If you actually care about whether it's a >> 32-bit or 64-bit OS, you can look at sys.maxsize. > > I didn't start using Python until long after I stopped using Windows > 9x. Do you recall what sys.platform was for Windows 9x? It was also > the Win32 API, but layered over a 32-bit VMM that extended/replaced > 16-bit DOS. There were many differences between it and the NT > implementation of Win32, such as no security or Unicode, and a > completely different console subsystem. > > IMO, "win32" was never the right platform name, but we're stuck with > it. The correct platform name is simply "windows". Win32 is also the > legacy API name. Nowadays the preferred name is the Windows API, or > WINAPI for short. Win32 is also the legacy 32-bit ABI name. It's an > okay name, but I'd prefer if we could change it to "win-ia32", which > would parallel the 64-bit ABI name "win-amd64". I'm fairly sure "win32" was used on W9x as well. In any case it *was* correct at the time, as early versions of Python also ran on DOS and Windows 3.1. "windows" would not have been suitable. But yeah, we're stuck with it. There are obviously good reasons that it wasn't changed when we moved to amd64, but it is annoying. -- Thomas From subhashvalla at gmail.com Tue Feb 19 20:07:41 2019 From: subhashvalla at gmail.com (Subash Netha) Date: Tue, 19 Feb 2019 20:07:41 -0500 Subject: Unable to Install Python 3.7.2 64 bit version on my Windows 8.1 Message-ID: Hello Team, I'm trying to install Python 3.7.2 64 bit version on my Windows 8.1 Operating system with Robot framework ride with Selenium Libraries. However, Python is getting installed in local drive installed of C drive and unable to open the ride.py. Please let me know, if there is work around for the same or any documentation would be greatly appreciated. Hoping to hear from you soon. Thanks, Subash From vandnamohan at gmail.com Wed Feb 20 03:36:55 2019 From: vandnamohan at gmail.com (Vandna Mahendroo) Date: Wed, 20 Feb 2019 14:06:55 +0530 Subject: not installing properly Message-ID: Dear ... I am trying to instaal python but not working properly. i have downloaded on exe file ..but it is not starting please help me.. -- Regards: Vandna mahendroo From prasis0103 at gmail.com Wed Feb 20 07:09:32 2019 From: prasis0103 at gmail.com (prasis poudel) Date: Wed, 20 Feb 2019 17:54:32 +0545 Subject: pip not working Message-ID: From sithmiamalka at gmail.com Wed Feb 20 07:53:09 2019 From: sithmiamalka at gmail.com (Sithmi Assalaarachchi) Date: Wed, 20 Feb 2019 18:23:09 +0530 Subject: my idle doesn't work Message-ID: <5c6d4dbb.1c69fb81.6e65b.8249@mx.google.com> Sent from Mail for Windows 10 From caridad.diaz.rivero at gmail.com Wed Feb 20 04:20:57 2019 From: caridad.diaz.rivero at gmail.com (=?UTF-8?Q?Caridad_D=C3=ADaz_Rivero?=) Date: Wed, 20 Feb 2019 10:20:57 +0100 Subject: f-string not referenced in library Message-ID: Hi, I don?t have too much experience using the official python documentation, and I was looking about f-string and I didn?t find anythings in /3/library/string.html and I find it in 3/reference/lexical_analysis.html#f-strings and I didn?t now Why it is not referenced in the documentation of String Library? Regards, Caridad D?az From eryksun at gmail.com Wed Feb 20 08:26:59 2019 From: eryksun at gmail.com (eryk sun) Date: Wed, 20 Feb 2019 07:26:59 -0600 Subject: revisiting the "What am I running on?" question In-Reply-To: <272619c5-9a99-dbb9-2f7e-939baf563a5b@tjol.eu> References: <7d52f95b-66fd-9919-da2f-25208a57ea85@potatochowder.com> <272619c5-9a99-dbb9-2f7e-939baf563a5b@tjol.eu> Message-ID: On 2/20/19, Thomas Jollans wrote: > > I'm fairly sure "win32" was used on W9x as well. In any case it *was* > correct at the time, as early versions of Python also ran on DOS and > Windows 3.1. "windows" would not have been suitable. DOS Python would be a separate platform. But if there was really a Windows 3.x version of Python, I wouldn't have a problem calling it a "Windows" platform. We could differentiate implementations via os.name ("win3", "win4", and "winnt" -- not "nt"). On a technical note, the NT subsystem name is "Windows", not "Win32", so there's that. > But yeah, we're stuck with it. There are obviously good reasons that it > wasn't changed when we moved to amd64, but it is annoying. The win-amd64 ABI is significantly different, but at the API level there isn't a drastic difference between 32-bit and 64-bit Windows, so there's no cognitive burden with perpetuating the Win32 name. The official API name was actually changed to "Windows API" or WINAPI (or WinAPI). But it would require a massive effort to change the culture. There's no pressing need to expend that much time and energy over a name. From songbird at anthive.com Wed Feb 20 08:53:15 2019 From: songbird at anthive.com (songbird) Date: Wed, 20 Feb 2019 08:53:15 -0500 Subject: revisiting the "What am I running on?" question References: <7d52f95b-66fd-9919-da2f-25208a57ea85@potatochowder.com> <272619c5-9a99-dbb9-2f7e-939baf563a5b@tjol.eu> Message-ID: Thomas Jollans wrote: ... > I'm fairly sure "win32" was used on W9x as well. In any case it *was* > correct at the time, as early versions of Python also ran on DOS and > Windows 3.1. "windows" would not have been suitable. > > But yeah, we're stuck with it. There are obviously good reasons that it > wasn't changed when we moved to amd64, but it is annoying. to me it is confusing and looks outdated so it did not give me much confidence i had found the right thing to use and so i kept looking further. if someone could put a note in the docs about this it will likely help others. in this age where things look to me very fragmented and then abandoned it is hard to know what is and isn't being maintained without looking for issue trackers and bugs and seeing if anyone is actively fixing things or not (multiply this by the number of modules being used and it drains more time). songbird From songbird at anthive.com Wed Feb 20 08:39:58 2019 From: songbird at anthive.com (songbird) Date: Wed, 20 Feb 2019 08:39:58 -0500 Subject: revisiting the "What am I running on?" question References: <7d52f95b-66fd-9919-da2f-25208a57ea85@potatochowder.com> Message-ID: Chris Angelico wrote: > songbird wrote: ... >> no win64? > > The value "win32" means Windows (or, more technically, "Windows NT > family", as opposed to Win95/Win98 - but since Win XP, that's the only > type of Windows there is). If you actually care about whether it's a > 32-bit or 64-bit OS, you can look at sys.maxsize. i'm not sure what system my code will actually run on or not. i got rid of my old hardware as part of a cleanup so i can't test things like that anymore without some kind of emulator or other kind people. at present i just want to know what kind of system i am running on so i can put my configuration file and saved games in the correct places. if i can't determine what kind of system i am running on then i won't run at all (instead of risking creating a directory or file on a system that doesn't have such ways of doing that - i really don't know what people may attempt after all). >> no arm(s)? > > That's a CPU architecture. What OS would you be running on your ARM? > If it's Windows, "win32". If it's Linux, "linux". Etc. ok. since i never have such things to try i don't always know what they run or how it looks to a python3 program. anyways, one thing i do like about an actual probe of the temporary kind is that i can answer the question of: "Do I have temporary directory and file creation permissions on this system or not?" a more accurate answer for any longer term storage would tell me if i have more permanent directory and file creation permissions or not, but my program doesn't require those since it will run without a configuration file or any saved games. songbird From rosuav at gmail.com Wed Feb 20 09:00:38 2019 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 21 Feb 2019 01:00:38 +1100 Subject: revisiting the "What am I running on?" question In-Reply-To: References: <7d52f95b-66fd-9919-da2f-25208a57ea85@potatochowder.com> <272619c5-9a99-dbb9-2f7e-939baf563a5b@tjol.eu> Message-ID: On Thu, Feb 21, 2019 at 12:56 AM songbird wrote: > > Thomas Jollans wrote: > ... > > I'm fairly sure "win32" was used on W9x as well. In any case it *was* > > correct at the time, as early versions of Python also ran on DOS and > > Windows 3.1. "windows" would not have been suitable. > > > > But yeah, we're stuck with it. There are obviously good reasons that it > > wasn't changed when we moved to amd64, but it is annoying. > > to me it is confusing and looks outdated so it > did not give me much confidence i had found the > right thing to use and so i kept looking further. > if someone could put a note in the docs about > this it will likely help others. Put a note somewhere in Microsoft's docs or something - this isn't a Python-specific thing. > in this age where things look to me very fragmented > and then abandoned it is hard to know what is and > isn't being maintained without looking for issue > trackers and bugs and seeing if anyone is actively > fixing things or not (multiply this by the number > of modules being used and it drains more time). > I'm not sure what you're asking for. You want evidence that things are changing, but without looking at the place where changes get changed? ChrisA From rosuav at gmail.com Wed Feb 20 09:02:12 2019 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 21 Feb 2019 01:02:12 +1100 Subject: revisiting the "What am I running on?" question In-Reply-To: References: <7d52f95b-66fd-9919-da2f-25208a57ea85@potatochowder.com> Message-ID: On Thu, Feb 21, 2019 at 1:00 AM songbird wrote: > anyways, one thing i do like about an actual > probe of the temporary kind is that i can answer > the question of: > > "Do I have temporary directory and file creation > permissions on this system or not?" Then ask that question instead! And the answer might well be here: https://docs.python.org/3/library/tempfile.html ChrisA From cspealma at redhat.com Wed Feb 20 09:21:37 2019 From: cspealma at redhat.com (Calvin Spealman) Date: Wed, 20 Feb 2019 09:21:37 -0500 Subject: f-string not referenced in library In-Reply-To: References: Message-ID: The string library is a utility set of functions with common string operations and that documentation is about the specific library, not the concept of strings or their syntax and other features in the language itself. For more about f-strings and other string behaviors, read the documentation on strings, the type, and other literals here https://docs.python.org/3.7/reference/lexical_analysis.html#literals That said, I do think you make a good point about the discovery of documentation. If a reader would reasonably find themselves in the wrong location while looking for information, maybe more can be done to guide them to the right place. On Wed, Feb 20, 2019 at 8:34 AM Caridad D?az Rivero < caridad.diaz.rivero at gmail.com> wrote: > Hi, > I don?t have too much experience using the official python documentation, > and I was looking about f-string and I didn?t find anythings in > /3/library/string.html > and > I find it in 3/reference/lexical_analysis.html#f-strings > and > I > didn?t now Why it is not referenced in the documentation of String Library? > Regards, > Caridad D?az > -- > https://mail.python.org/mailman/listinfo/python-list > -- CALVIN SPEALMAN SENIOR QUALITY ENGINEER cspealma at redhat.com M: +1.336.210.5107 TRIED. TESTED. TRUSTED. From cspealma at redhat.com Wed Feb 20 09:21:59 2019 From: cspealma at redhat.com (Calvin Spealman) Date: Wed, 20 Feb 2019 09:21:59 -0500 Subject: pip not working In-Reply-To: References: Message-ID: Can you give any more information than that? On Wed, Feb 20, 2019 at 8:30 AM prasis poudel wrote: > > -- > https://mail.python.org/mailman/listinfo/python-list > -- CALVIN SPEALMAN SENIOR QUALITY ENGINEER cspealma at redhat.com M: +1.336.210.5107 TRIED. TESTED. TRUSTED. From cspealma at redhat.com Wed Feb 20 09:22:46 2019 From: cspealma at redhat.com (Calvin Spealman) Date: Wed, 20 Feb 2019 09:22:46 -0500 Subject: not installing properly In-Reply-To: References: Message-ID: What EXE file have you downloaded? How are you trying to run it? What are you expecting it to do and what is it doing instead? You'll need to provide more information in order to help us help you. On Wed, Feb 20, 2019 at 8:26 AM Vandna Mahendroo wrote: > Dear ... > > I am trying to instaal python but not working properly. > > i have downloaded on exe file ..but it is not starting > > please help me.. > > > > > > -- > Regards: > > Vandna mahendroo > -- > https://mail.python.org/mailman/listinfo/python-list > -- CALVIN SPEALMAN SENIOR QUALITY ENGINEER cspealma at redhat.com M: +1.336.210.5107 TRIED. TESTED. TRUSTED. From cspealma at redhat.com Wed Feb 20 09:28:22 2019 From: cspealma at redhat.com (Calvin Spealman) Date: Wed, 20 Feb 2019 09:28:22 -0500 Subject: Unable to Install Python 3.7.2 64 bit version on my Windows 8.1 In-Reply-To: References: Message-ID: In what way is it "unable to open the ride.py" file? Can you give details about how you're trying to open or run that, what you expect to happen, and what's happening instead? On Wed, Feb 20, 2019 at 8:22 AM Subash Netha wrote: > Hello Team, > > I'm trying to install Python 3.7.2 64 bit version on my Windows 8.1 > Operating system with Robot framework ride with Selenium Libraries. > > However, Python is getting installed in local drive installed of C drive > and unable to open the ride.py. > > Please let me know, if there is work around for the same or any > documentation would be greatly appreciated. > > Hoping to hear from you soon. > > Thanks, > Subash > -- > https://mail.python.org/mailman/listinfo/python-list > -- CALVIN SPEALMAN SENIOR QUALITY ENGINEER cspealma at redhat.com M: +1.336.210.5107 TRIED. TESTED. TRUSTED. From Gronicus at SGA.Ninja Wed Feb 20 09:58:08 2019 From: Gronicus at SGA.Ninja (Steve) Date: Wed, 20 Feb 2019 09:58:08 -0500 Subject: Unable to Install Python 3.7.2 64 bit version on my Windows 8.1 In-Reply-To: References: Message-ID: <000001d4c92c$b1c99870$155cc950$@SGA.Ninja> Is your .py file represented with the python icon? If not then you probably have a path problem. You can check to see if python is working if you copy a .py file into the same folder where python.exe exists. If you can execute the .py file from that folder then and not elsewhere, it is probably a path problem. >From my bad experience installing Python, apparently the installation did not handle the path modification very well. I had to add the python path into the environment manually. To check this, open a CMD screen and enter "path" as a command. Check to see if the path to Python is listed. I struggled with this in my first installation of Python until I learned how to manually modify the environment path. I had to install python on a second computer but did not remember what magic I had to do to get the system to remember where the python.exe was located. So that second computer still does not have it working properly. Footnote: Ultrasound Technician Asks Pregnant Woman If She'd Like To Know Baby's Name -----Original Message----- From: Python-list On Behalf Of Subash Netha Sent: Tuesday, February 19, 2019 8:08 PM To: python-list at python.org Subject: Unable to Install Python 3.7.2 64 bit version on my Windows 8.1 Hello Team, I'm trying to install Python 3.7.2 64 bit version on my Windows 8.1 Operating system with Robot framework ride with Selenium Libraries. However, Python is getting installed in local drive installed of C drive and unable to open the ride.py. Please let me know, if there is work around for the same or any documentation would be greatly appreciated. Hoping to hear from you soon. Thanks, Subash -- https://mail.python.org/mailman/listinfo/python-list From Gronicus at SGA.Ninja Wed Feb 20 10:06:57 2019 From: Gronicus at SGA.Ninja (Steve) Date: Wed, 20 Feb 2019 10:06:57 -0500 Subject: my idle doesn't work In-Reply-To: <5c6d4dbb.1c69fb81.6e65b.8249@mx.google.com> References: <5c6d4dbb.1c69fb81.6e65b.8249@mx.google.com> Message-ID: <000201d4c92d$ece57900$c6b06b00$@SGA.Ninja> I had this once. I uninstalled/reinstalled python and IDLE worked. Footnote: Ultrasound Technician Asks Pregnant Woman If She'd Like To Know Baby's Name -----Original Message----- From: Python-list On Behalf Of Sithmi Assalaarachchi Sent: Wednesday, February 20, 2019 7:53 AM To: python-list at python.org Subject: my idle doesn't work Sent from Mail for Windows 10 -- https://mail.python.org/mailman/listinfo/python-list From edmer2210 at gmail.com Wed Feb 20 13:56:11 2019 From: edmer2210 at gmail.com (edmer2210 at gmail.com) Date: Wed, 20 Feb 2019 10:56:11 -0800 (PST) Subject: How to play with Python in an existing app Message-ID: <1d12903e-e3ef-40b0-9e15-6afd4786b39d@googlegroups.com> So I'm a newbie to Python. I got access to an existing RavenDB and am supposed to install it on aws.amazon.com. If I succeed in setting this up, I am supposed to write some Python code. But how can I play with Python and test some querries without f#cking up the database? Because the RavenDB is working with an app that's currently being used by a few hundreds of people. Should I ask for a copy of the RavenDB database and test it locally on my PC with PyCharm, for example? Thanks for any suggestions on how to setup a safe working environment for an app that's already in use, so a beginner can play, learn and grow without damaging something :-) From rhodri at kynesim.co.uk Wed Feb 20 14:08:32 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Wed, 20 Feb 2019 19:08:32 +0000 Subject: How to play with Python in an existing app In-Reply-To: <1d12903e-e3ef-40b0-9e15-6afd4786b39d@googlegroups.com> References: <1d12903e-e3ef-40b0-9e15-6afd4786b39d@googlegroups.com> Message-ID: <89aa19f6-9196-8437-84e6-47b60194826b@kynesim.co.uk> On 20/02/2019 18:56, edmer2210 at gmail.com wrote: > So I'm a newbie to Python. I got access to an existing RavenDB and am supposed to install it on aws.amazon.com. If I succeed in setting this up, I am supposed to write some Python code. > > But how can I play with Python and test some querries without f#cking up the database? Because the RavenDB is working with an app that's currently being used by a few hundreds of people. These two statements are a bit inconsistent. If you are installing an existing RavenDB on aws, other people cannot be using it on aws, surely? At any rate, I think your instincts are right. The only safe context for experimenting in is an isolated one. Your problem may be that a local instance of RavenDB may act differently to a remote one, but that's a bridge to cross when you come to it. If you can get an isolated instance on AWS, that would be ideal (but expensive). -- Rhodri James *-* Kynesim Ltd From PythonList at DancesWithMice.info Wed Feb 20 14:26:46 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Thu, 21 Feb 2019 08:26:46 +1300 Subject: How to play with Python in an existing app In-Reply-To: <1d12903e-e3ef-40b0-9e15-6afd4786b39d@googlegroups.com> References: <1d12903e-e3ef-40b0-9e15-6afd4786b39d@googlegroups.com> Message-ID: <33e054ac-f8dc-bc7b-a268-4dce4aeb5db9@DancesWithMice.info> Edmer, On 21/02/19 7:56 AM, edmer2210 at gmail.com wrote: > So I'm a newbie to Python. I got access to an existing RavenDB and am supposed to install it on aws.amazon.com. If I succeed in setting this up, I am supposed to write some Python code. > > But how can I play with Python and test some querries without f#cking up the database? Because the RavenDB is working with an app that's currently being used by a few hundreds of people. > > Should I ask for a copy of the RavenDB database and test it locally on my PC with PyCharm, for example? > > Thanks for any suggestions on how to setup a safe working environment for an app that's already in use, so a beginner can play, learn and grow without damaging something :-) Won't an entirely separate environment, whether on AWS or your local PC, solve your problem and prevent 'accidents'. NEVER test on 'live'! Not being familiar with RavenDB, how easy is it to replicate the DBMS? Worst case: how difficult would it be to create a second set of DBs for dev/testing; and then what differences would there be between the code used for testing and that used in production (the more changes needed, the more likely something will go wrong at the last step of 'going live'!)? -- Regards =dn From roel at roelschroeven.net Wed Feb 20 15:43:18 2019 From: roel at roelschroeven.net (Roel Schroeven) Date: Wed, 20 Feb 2019 21:43:18 +0100 Subject: revisiting the "What am I running on?" question In-Reply-To: <272619c5-9a99-dbb9-2f7e-939baf563a5b@tjol.eu> References: <7d52f95b-66fd-9919-da2f-25208a57ea85@potatochowder.com> <272619c5-9a99-dbb9-2f7e-939baf563a5b@tjol.eu> Message-ID: Thomas Jollans schreef op 20/02/2019 om 12:48: > I'm fairly sure "win32" was used on W9x as well. Indeed, it was used for all 32-bit Windows versions: both the 9x line and the NT line. There were differences, of course. For example there are many calls that take SECURITY_ATTRIBUTES parameters. On 9x those parameters simply didn't do anything. > In any case it *was* > correct at the time, as early versions of Python also ran on DOS and > Windows 3.1. "windows" would not have been suitable. The API in Windows 3.1 (and 3.0 and 3.11 I suppose) was called the Windows API at the time, later renamed to Win16 to distinguish it from Win32 in the newer 32-bit Windows versions. And then there was also Win32s, a subset of the real Win32 used on Windows 3.1. And WinCE, but let's not go there. -- "Honest criticism is hard to take, particularly from a relative, a friend, an acquaintance, or a stranger." -- Franklin P. Jones Roel Schroeven From listsen at 0x906.com Wed Feb 20 14:16:42 2019 From: listsen at 0x906.com (0x906) Date: Wed, 20 Feb 2019 11:16:42 -0800 Subject: How to play with Python in an existing app In-Reply-To: <1d12903e-e3ef-40b0-9e15-6afd4786b39d@googlegroups.com> References: <1d12903e-e3ef-40b0-9e15-6afd4786b39d@googlegroups.com> Message-ID: A rule of thumb to keep your sanity and maybe your job is to never touch a production system in any way. Use a copy of that db and do all your research there. > On Feb 20, 2019, at 10:56 AM, edmer2210 at gmail.com wrote: > > So I'm a newbie to Python. I got access to an existing RavenDB and am supposed to install it on aws.amazon.com. If I succeed in setting this up, I am supposed to write some Python code. > > But how can I play with Python and test some querries without f#cking up the database? Because the RavenDB is working with an app that's currently being used by a few hundreds of people. > > Should I ask for a copy of the RavenDB database and test it locally on my PC with PyCharm, for example? > > Thanks for any suggestions on how to setup a safe working environment for an app that's already in use, so a beginner can play, learn and grow without damaging something :-) > -- > https://mail.python.org/mailman/listinfo/python-list From george.trojan at gmail.com Wed Feb 20 19:15:40 2019 From: george.trojan at gmail.com (george trojan) Date: Thu, 21 Feb 2019 00:15:40 +0000 Subject: Multiprocessing performance question Message-ID: def create_box(x_y): return geometry.box(x_y[0] - 1, x_y[1], x_y[0], x_y[1] - 1) x_range = range(1, 1001) y_range = range(1, 801) x_y_range = list(itertools.product(x_range, y_range)) grid = list(map(create_box, x_y_range)) Which creates and populates an 800x1000 ?grid? (represented as a flat list at this point) of ?boxes?, where a box is a shapely.geometry.box(). This takes about 10 seconds to run. Looking at this, I am thinking it would lend itself well to parallelization. Since the box at each ?coordinate" is independent of all others, it seems I should be able to simply split the list up into chunks and process each chunk in parallel on a separate core. To that end, I created a multiprocessing pool: pool = multiprocessing.Pool() And then called pool.map() rather than just ?map?. Somewhat to my surprise, the execution time was virtually identical. Given the simplicity of my code, and the presumable ease with which it should be able to be parallelized, what could explain why the performance did not improve at all when moving from the single-process map() to the multiprocess map()? I am aware that in python3, the map function doesn?t actually produce a result until needed, but that?s why I wrapped everything in calls to list(), at least for testing. The reason multiprocessing does not speed things up is the overhead of pickling/unpickling objects. Here are results on my machine, running Jupyter notebook: def create_box(xy): return geometry.box(xy[0]-1, xy[1], xy[0], xy[1]-1) nx = 1000 ny = 800 xrange = range(1, nx+1) yrange = range(1, ny+1) xyrange = list(itertools.product(xrange, yrange)) %%time grid1 = list(map(create_box, xyrange)) CPU times: user 9.88 s, sys: 2.09 s, total: 12 s Wall time: 10 s %%time pool = multiprocessing.Pool() grid2 = list(pool.map(create_box, xyrange)) CPU times: user 8.48 s, sys: 1.39 s, total: 9.87 s Wall time: 10.6 s Results exactly as yours. To see what is going on, I rolled out my own chunking that allowed me to add some print statements. %%time def myfun(chunk): g = list(map(create_box, chunk)) print('chunk', chunk[0], datetime.now().isoformat()) return g pool = multiprocessing.Pool() chunks = [xyrange[i:i+100*ny] for i in range(0, nx*ny, 100*ny)] print('starting', datetime.now().isoformat()) gridlist = list(pool.map(myfun, chunks)) grid3 = list(itertools.chain(*gridlist)) print('done', datetime.now().isoformat()) starting 2019-02-20T23:03:50.883180 chunk (1, 1) 2019-02-20T23:03:51.674046 chunk (701, 1) 2019-02-20T23:03:51.748765 chunk (201, 1) 2019-02-20T23:03:51.772458 chunk (401, 1) 2019-02-20T23:03:51.798917 chunk (601, 1) 2019-02-20T23:03:51.805113 chunk (501, 1) 2019-02-20T23:03:51.807163 chunk (301, 1) 2019-02-20T23:03:51.818911 chunk (801, 1) 2019-02-20T23:03:51.974715 chunk (101, 1) 2019-02-20T23:03:52.086421 chunk (901, 1) 2019-02-20T23:03:52.692573 done 2019-02-20T23:04:02.477317 CPU times: user 8.4 s, sys: 1.7 s, total: 10.1 s Wall time: 12.9 s All ten subprocesses finished within 2 seconds. It took about 10 seconds to get back and assemble the partial results. The objects have to be packed, sent through network and unpacked. Unpacking is done by the main (i.e. single) process. This takes almost the same time as creating the objects from scratch. Essentially the process does the following: %%time def f(b): g1 = b[0].__new__(b[0]) g1.__setstate__(b[2]) return g1 buf = [g.__reduce__() for g in grid1] grid4 = [f(b) for b in buf] CPU times: user 20 s, sys: 411 ms, total: 20.4 s Wall time: 20.3 s The first line creates the pickle (not exactly, as pickled data is a single string, not a list). The second line is what pickle.loads() does. I do not think numpy will help here. The Python function box() has to be called 800k times. This will take time. np.vectorize(), as the documentation states, is provided only for convenience, it is implemented with a for loop. IMO vectorization would have to be done on C level. Greetings from Anchorage George From PythonList at DancesWithMice.info Wed Feb 20 20:30:20 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Thu, 21 Feb 2019 14:30:20 +1300 Subject: Multiprocessing performance question In-Reply-To: References: Message-ID: <111a206c-0d34-49cb-aee7-ac1a2dfc0f47@DancesWithMice.info> George On 21/02/19 1:15 PM, george trojan wrote: > def create_box(x_y): > return geometry.box(x_y[0] - 1, x_y[1], x_y[0], x_y[1] - 1) > > x_range = range(1, 1001) > y_range = range(1, 801) > x_y_range = list(itertools.product(x_range, y_range)) > > grid = list(map(create_box, x_y_range)) > > Which creates and populates an 800x1000 ?grid? (represented as a flat list > at this point) of ?boxes?, where a box is a shapely.geometry.box(). This > takes about 10 seconds to run. > > Looking at this, I am thinking it would lend itself well to > parallelization. Since the box at each ?coordinate" is independent of all > others, it seems I should be able to simply split the list up into chunks > and process each chunk in parallel on a separate core. To that end, I > created a multiprocessing pool: I recall a similar discussion when folk were being encouraged to move away from monolithic and straight-line processing to modular functions - it is more (CPU-time) efficient to run in a straight line; than it is to repeatedly call, set-up, execute, and return-from a function or sub-routine! ie there is an over-head to many/all constructs! Isn't the 'problem' that it is a 'toy example'? That the amount of computing within each parallel process is small in relation to the inherent 'overhead'. Thus, if the code performed a reasonable analytical task within each box after it had been defined (increased CPU load), would you then notice the expected difference between the single- and multi-process implementations? From AKL to AK -- Regards =dn From george.trojan at gmail.com Wed Feb 20 20:54:05 2019 From: george.trojan at gmail.com (george trojan) Date: Thu, 21 Feb 2019 01:54:05 +0000 Subject: Multiprocessing performance question In-Reply-To: <111a206c-0d34-49cb-aee7-ac1a2dfc0f47@DancesWithMice.info> References: <111a206c-0d34-49cb-aee7-ac1a2dfc0f47@DancesWithMice.info> Message-ID: I don't know whether this is a toy example, having grid of this size is not uncommon. True, it would make more sense to do distribute more work on each box, if there was any. One has to find a proper balance, as with many other things in life. I simply responded to a question by the OP. George On Thu, 21 Feb 2019 at 01:30, DL Neil wrote: > George > > On 21/02/19 1:15 PM, george trojan wrote: > > def create_box(x_y): > > return geometry.box(x_y[0] - 1, x_y[1], x_y[0], x_y[1] - 1) > > > > x_range = range(1, 1001) > > y_range = range(1, 801) > > x_y_range = list(itertools.product(x_range, y_range)) > > > > grid = list(map(create_box, x_y_range)) > > > > Which creates and populates an 800x1000 ?grid? (represented as a flat > list > > at this point) of ?boxes?, where a box is a shapely.geometry.box(). This > > takes about 10 seconds to run. > > > > Looking at this, I am thinking it would lend itself well to > > parallelization. Since the box at each ?coordinate" is independent of all > > others, it seems I should be able to simply split the list up into chunks > > and process each chunk in parallel on a separate core. To that end, I > > created a multiprocessing pool: > > > I recall a similar discussion when folk were being encouraged to move > away from monolithic and straight-line processing to modular functions - > it is more (CPU-time) efficient to run in a straight line; than it is to > repeatedly call, set-up, execute, and return-from a function or > sub-routine! ie there is an over-head to many/all constructs! > > Isn't the 'problem' that it is a 'toy example'? That the amount of > computing within each parallel process is small in relation to the > inherent 'overhead'. > > Thus, if the code performed a reasonable analytical task within each box > after it had been defined (increased CPU load), would you then notice > the expected difference between the single- and multi-process > implementations? > > > > From AKL to AK > -- > Regards =dn > From jfong at ms4.hinet.net Wed Feb 20 22:28:22 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Wed, 20 Feb 2019 19:28:22 -0800 (PST) Subject: Ask for help on using asynctk package Message-ID: The file below is a simple script to test this package. ------------- # test_asynctk.py import asynctk as atk root = atk.AsyncTk() def delay5S(): btn0.after(5000) def inc1(): msgs = btn1.cget('text').split() n = int(msgs[1]) msg = msgs[0] + ' ' + str(n+1) btn1.config(text=msg) btn0 = atk.AsyncButton(root, text='delay 5s', command=delay5S) btn0.pack() btn1 = atk.AsyncButton(root, text='increment 0', command=inc1) btn1.pack() if __name__ == "__main__": root.mainloop() ------------- After import this script and press the increment button, the following error appears. No idea how to use it correctly. Any help? Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 14:05:16) [MSC v.1915 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import test_asynctk # press the button >>> Exception in Tkinter callback Traceback (most recent call last): File "H:\WPy-3710Zero\python-3.7.1\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "H:\Works\Python34\asynctk-master\asynctk\_lib.py", line 1615, in kw["command"] = lambda *i: asyncio.ensure_future(kw["command"](*i)) File "H:\Works\Python34\asynctk-master\asynctk\_lib.py", line 1615, in kw["command"] = lambda *i: asyncio.ensure_future(kw["command"](*i)) File "H:\Works\Python34\asynctk-master\asynctk\_lib.py", line 1615, in kw["command"] = lambda *i: asyncio.ensure_future(kw["command"](*i)) [Previous line repeated 995 more times] RecursionError: maximum recursion depth exceeded --Jach From robin at reportlab.com Thu Feb 21 04:52:18 2019 From: robin at reportlab.com (Robin Becker) Date: Thu, 21 Feb 2019 09:52:18 +0000 Subject: using zip for transpose Message-ID: In conversion of pandas dataframe to reportlab table I suggested using this expression [list(x) for x in map(list,zip(*[df[i].values for i in df]))] which effectively transposes the dataframe. However, it's not clear that this works for a large number of rows. Is the argument *A for A a large list just copied into the zip *args; I suppose calling zip(A[0],A[1],......A[len(A)-1]) cannot be how this is done. -- Robin Becker From otex911 at gmail.com Thu Feb 21 07:12:26 2019 From: otex911 at gmail.com (Evi1 T1me) Date: Thu, 21 Feb 2019 04:12:26 -0800 (PST) Subject: Help, Can't find the default proxy in requests by config Message-ID: <90aae607-591b-4de7-8ff2-a23a117180da@googlegroups.com> ```bash ~ python3 Python 3.7.0 (default, Oct 22 2018, 14:54:27) [Clang 10.0.0 (clang-1000.11.45.2)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import requests >>> r = requests.get('https://www.baidu.com') Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 159, in _new_conn (self._dns_host, self.port), self.timeout, **extra_kw) File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 80, in create_connection raise err File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 70, in create_connection sock.connect(sa) ConnectionRefusedError: [Errno 61] Connection refused During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 594, in urlopen self._prepare_proxy(conn) File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 805, in _prepare_proxy conn.connect() File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 301, in connect conn = self._new_conn() File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 168, in _new_conn self, "Failed to establish a new connection: %s" % e) urllib3.exceptions.NewConnectionError: : Failed to establish a new connection: [Errno 61] Connection refused During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 449, in send timeout=timeout File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 638, in urlopen _stacktrace=sys.exc_info()[2]) File "/usr/local/lib/python3.7/site-packages/urllib3/util/retry.py", line 398, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [Errno 61] Connection refused'))) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 75, in get return request('get', url, params=params, **kwargs) File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 60, in request return session.request(method=method, url=url, **kwargs) File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 533, in request resp = self.send(prep, **send_kwargs) File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 646, in send r = adapter.send(request, **kwargs) File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 510, in send raise ProxyError(e, request=request) requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [Errno 61] Connection refused'))) ``` Check the proxy ```bash >>> print(requests.utils.get_environ_proxies('https://www.baidu.com')) {'http': 'http://127.0.0.1:8888', 'https': 'http://127.0.0.1:8888'} ``` Check bash environment ```bash ~ set | grep proxy ``` Nothing output. ```bash ? ~ netstat -ant | grep 8888 tcp4 5 0 127.0.0.1.54437 127.0.0.1.8888 CLOSE_WAIT tcp4 653 0 127.0.0.1.54436 127.0.0.1.8888 CLOSE_WAIT tcp4 5 0 127.0.0.1.54434 127.0.0.1.8888 CLOSE_WAIT ``` ```bash ? ~ lsof -i:8888 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME JavaAppli 77714 zerop 54u IPv6 0x975257a323b5690f 0t0 TCP localhost:54434->localhost:ddi-tcp-1 (CLOSE_WAIT) JavaAppli 77714 zerop 55u IPv6 0x975257a33daa290f 0t0 TCP localhost:54436->localhost:ddi-tcp-1 (CLOSE_WAIT) JavaAppli 77714 zerop 56u IPv6 0x975257a3366b600f 0t0 TCP localhost:54437->localhost:ddi-tcp-1 (CLOSE_WAIT) ``` ```bash ? ~ ps -ef | grep 77714 501 77714 1 0 11:17AM ?? 3:33.55 /Applications/Burp Suite Community Edition.app/Contents/MacOS/JavaApplicationStub 501 84408 82855 0 5:54AM ttys002 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn 77714 ``` Restart the application BurpSuite Community and then run the command `lsof -i:8888`, nothing out ```bash ? ~ lsof -i:8888 ``` Check `HTTP_SERVER` and `HTTP_PROXY` ```bash ? ~ echo $HTTP_SERVER ? ~ echo $HTTP_PROXY ? ~ echo $HTTPS_SERVER ``` Restart the computer and try to access Baidu as the following. ```bash >>> r = requests.get('https://www.baidu.com') Traceback (most recent call last): File "", line 1, in NameError: name 'requests' is not defined >>> import requests >>> r = requests.get('https://www.baidu.com') Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 159, in _new_conn (self._dns_host, self.port), self.timeout, **extra_kw) File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 80, in create_connection raise err File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 70, in create_connection sock.connect(sa) ConnectionRefusedError: [Errno 61] Connection refused During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 594, in urlopen self._prepare_proxy(conn) File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 805, in _prepare_proxy conn.connect() File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 301, in connect conn = self._new_conn() File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 168, in _new_conn self, "Failed to establish a new connection: %s" % e) urllib3.exceptions.NewConnectionError: : Failed to establish a new connection: [Errno 61] Connection refused During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 449, in send timeout=timeout File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 638, in urlopen _stacktrace=sys.exc_info()[2]) File "/usr/local/lib/python3.7/site-packages/urllib3/util/retry.py", line 398, in increment raise MaxRetryError(_pool, url, error or ResponseError(cause)) urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [Errno 61] Connection refused'))) During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 75, in get return request('get', url, params=params, **kwargs) File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 60, in request return session.request(method=method, url=url, **kwargs) File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 533, in request resp = self.send(prep, **send_kwargs) File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 646, in send r = adapter.send(request, **kwargs) File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 510, in send raise ProxyError(e, request=request) requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [Errno 61] Connection refused'))) ``` And then check the default proxy with `urllib.request.getproxies()` ```bash >>> import urllib >>> print(urllib.getproxies()) Traceback (most recent call last): File "", line 1, in AttributeError: module 'urllib' has no attribute 'getproxies' >>> print(urllib.request.getproxies()) {'http': 'http://127.0.0.1:8888', 'https': 'http://127.0.0.1:8888'} ``` From __peter__ at web.de Thu Feb 21 08:49:27 2019 From: __peter__ at web.de (Peter Otten) Date: Thu, 21 Feb 2019 14:49:27 +0100 Subject: using zip for transpose References: Message-ID: Robin Becker wrote: > In conversion of pandas dataframe to reportlab table I suggested using > this expression > > [list(x) for x in map(list,zip(*[df[i].values for i in df]))] > > which effectively transposes the dataframe. However, it's not clear that > this works for a large number of rows. Is the argument *A for A a large > list just copied into the zip *args; I suppose calling > zip(A[0],A[1],......A[len(A)-1]) cannot be how this is done. Isn't df.values a numpy array? Then try the more direct and likely more efficient df.values.tolist() or, if you ever want to transpose df.values.T.tolist() The first seems to achieve what your sample code does. (In addition it also converts the numpy type to the corresponding python builtin, i. e. numpy.float64 becomes float etc.) From felix at epepm.cupet.cu Thu Feb 21 09:00:03 2019 From: felix at epepm.cupet.cu (Felix Lazaro Carbonell) Date: Thu, 21 Feb 2019 09:00:03 -0500 Subject: python3.7.2 won't compile with SSL support Message-ID: <003901d4c9ed$be26c130$3a744390$@epepm.cupet.cu> Hello: I'm trying to install python3.7.2 from source in debian9.8 but it doesn't compile with SSL. I already installed openssl And ./configure -with-openssl=/usr/include/openssl/ yields: checking for openssl/ssl.h in /usr/include/openssl/... no and ssl.h is certainly in /usr/include/openssl/ any ideas please? Thanks in advance, Felix. From robin at reportlab.com Thu Feb 21 09:39:06 2019 From: robin at reportlab.com (Robin Becker) Date: Thu, 21 Feb 2019 14:39:06 +0000 Subject: using zip for transpose In-Reply-To: References: Message-ID: <4162fe93-6ef1-305a-b63a-44383b6d4595@chamonix.reportlab.co.uk> On 21/02/2019 13:49, Peter Otten wrote: > Robin Becker wrote: > >....... > Isn't df.values a numpy array? Then try the more direct and likely more > efficient > > df.values.tolist() > > or, if you ever want to transpose > > df.values.T.tolist() > > The first seems to achieve what your sample code does. (In addition it also > converts the numpy type to the corresponding python builtin, i. e. > numpy.float64 becomes float etc.) > Thanks for the pointer. In fact we were working through all the wrong methods eg iterrows (slow) or iterating over columns which created the need for a transpose. However, df.values.tolist() seems to create a list of row lists which is what is actually needed and it is the fastest. So to convert df to something for reportlab table this seems most efficient rlab_table_data=[['Mean','Max','Min','TestA','TestB']]+df.values.tolist() thanks again -- Robin Becker From otex911 at gmail.com Thu Feb 21 10:03:06 2019 From: otex911 at gmail.com (Evi1 T1me) Date: Thu, 21 Feb 2019 07:03:06 -0800 (PST) Subject: Help, Can't find the default proxy in requests by config In-Reply-To: <90aae607-591b-4de7-8ff2-a23a117180da@googlegroups.com> References: <90aae607-591b-4de7-8ff2-a23a117180da@googlegroups.com> Message-ID: <3d1856fd-3f3a-4bed-9c5f-d21e213855b0@googlegroups.com> On Thursday, February 21, 2019 at 7:12:40 AM UTC-5, Evi1 T1me wrote: > ```bash > ~ python3 > Python 3.7.0 (default, Oct 22 2018, 14:54:27) > [Clang 10.0.0 (clang-1000.11.45.2)] on darwin > Type "help", "copyright", "credits" or "license" for more information. > >>> import requests > >>> r = requests.get('https://www.baidu.com') > Traceback (most recent call last): > File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 159, in _new_conn > (self._dns_host, self.port), self.timeout, **extra_kw) > File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 80, in create_connection > raise err > File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 70, in create_connection > sock.connect(sa) > ConnectionRefusedError: [Errno 61] Connection refused > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 594, in urlopen > self._prepare_proxy(conn) > File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 805, in _prepare_proxy > conn.connect() > File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 301, in connect > conn = self._new_conn() > File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 168, in _new_conn > self, "Failed to establish a new connection: %s" % e) > urllib3.exceptions.NewConnectionError: : Failed to establish a new connection: [Errno 61] Connection refused > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 449, in send > timeout=timeout > File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 638, in urlopen > _stacktrace=sys.exc_info()[2]) > File "/usr/local/lib/python3.7/site-packages/urllib3/util/retry.py", line 398, in increment > raise MaxRetryError(_pool, url, error or ResponseError(cause)) > urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [Errno 61] Connection refused'))) > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > File "", line 1, in > File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 75, in get > return request('get', url, params=params, **kwargs) > File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 60, in request > return session.request(method=method, url=url, **kwargs) > File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 533, in request > resp = self.send(prep, **send_kwargs) > File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 646, in send > r = adapter.send(request, **kwargs) > File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 510, in send > raise ProxyError(e, request=request) > requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [Errno 61] Connection refused'))) > ``` > > Check the proxy > > ```bash > >>> print(requests.utils.get_environ_proxies('https://www.baidu.com')) > {'http': 'http://127.0.0.1:8888', 'https': 'http://127.0.0.1:8888'} > ``` > > Check bash environment > > ```bash > ~ set | grep proxy > ``` > Nothing output. > > ```bash > ? ~ netstat -ant | grep 8888 > tcp4 5 0 127.0.0.1.54437 127.0.0.1.8888 CLOSE_WAIT > tcp4 653 0 127.0.0.1.54436 127.0.0.1.8888 CLOSE_WAIT > tcp4 5 0 127.0.0.1.54434 127.0.0.1.8888 CLOSE_WAIT > ``` > > ```bash > ? ~ lsof -i:8888 > COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME > JavaAppli 77714 zerop 54u IPv6 0x975257a323b5690f 0t0 TCP localhost:54434->localhost:ddi-tcp-1 (CLOSE_WAIT) > JavaAppli 77714 zerop 55u IPv6 0x975257a33daa290f 0t0 TCP localhost:54436->localhost:ddi-tcp-1 (CLOSE_WAIT) > JavaAppli 77714 zerop 56u IPv6 0x975257a3366b600f 0t0 TCP localhost:54437->localhost:ddi-tcp-1 (CLOSE_WAIT) > ``` > > ```bash > ? ~ ps -ef | grep 77714 > 501 77714 1 0 11:17AM ?? 3:33.55 /Applications/Burp Suite Community Edition.app/Contents/MacOS/JavaApplicationStub > 501 84408 82855 0 5:54AM ttys002 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn 77714 > ``` > > Restart the application BurpSuite Community and then run the command `lsof -i:8888`, nothing out > ```bash > ? ~ lsof -i:8888 > ``` > > Check `HTTP_SERVER` and `HTTP_PROXY` > > ```bash > ? ~ echo $HTTP_SERVER > > ? ~ echo $HTTP_PROXY > > ? ~ echo $HTTPS_SERVER > ``` > > Restart the computer and try to access Baidu as the following. > ```bash > >>> r = requests.get('https://www.baidu.com') > Traceback (most recent call last): > File "", line 1, in > NameError: name 'requests' is not defined > >>> import requests > >>> r = requests.get('https://www.baidu.com') > Traceback (most recent call last): > File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 159, in _new_conn > (self._dns_host, self.port), self.timeout, **extra_kw) > File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 80, in create_connection > raise err > File "/usr/local/lib/python3.7/site-packages/urllib3/util/connection.py", line 70, in create_connection > sock.connect(sa) > ConnectionRefusedError: [Errno 61] Connection refused > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 594, in urlopen > self._prepare_proxy(conn) > File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 805, in _prepare_proxy > conn.connect() > File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 301, in connect > conn = self._new_conn() > File "/usr/local/lib/python3.7/site-packages/urllib3/connection.py", line 168, in _new_conn > self, "Failed to establish a new connection: %s" % e) > urllib3.exceptions.NewConnectionError: : Failed to establish a new connection: [Errno 61] Connection refused > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 449, in send > timeout=timeout > File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 638, in urlopen > _stacktrace=sys.exc_info()[2]) > File "/usr/local/lib/python3.7/site-packages/urllib3/util/retry.py", line 398, in increment > raise MaxRetryError(_pool, url, error or ResponseError(cause)) > urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [Errno 61] Connection refused'))) > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > File "", line 1, in > File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 75, in get > return request('get', url, params=params, **kwargs) > File "/usr/local/lib/python3.7/site-packages/requests/api.py", line 60, in request > return session.request(method=method, url=url, **kwargs) > File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 533, in request > resp = self.send(prep, **send_kwargs) > File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 646, in send > r = adapter.send(request, **kwargs) > File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 510, in send > raise ProxyError(e, request=request) > requests.exceptions.ProxyError: HTTPSConnectionPool(host='www.baidu.com', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError(': Failed to establish a new connection: [Errno 61] Connection refused'))) > ``` > > And then check the default proxy with `urllib.request.getproxies()` > ```bash > >>> import urllib > >>> print(urllib.getproxies()) > Traceback (most recent call last): > File "", line 1, in > AttributeError: module 'urllib' has no attribute 'getproxies' > >>> print(urllib.request.getproxies()) > {'http': 'http://127.0.0.1:8888', 'https': 'http://127.0.0.1:8888'} > ``` Solved. Check the system proxy and change it after checking all the shell env. From none at gmail.com Thu Feb 21 11:40:42 2019 From: none at gmail.com (ast) Date: Thu, 21 Feb 2019 17:40:42 +0100 Subject: sys.modules Message-ID: <5c6ed48c$0$20311$426a74cc@news.free.fr> Hello Is it normal to have 151 entries in dictionary sys.modules just after starting IDLE or something goes wrong ? >>> import sys >>> len(sys.modules) 151 Most of common modules seems to be already there, os, itertools, random .... I thought that sys.modules was containing loaded modules with import command only. From ijbrewster at alaska.edu Thu Feb 21 12:04:06 2019 From: ijbrewster at alaska.edu (Israel Brewster) Date: Thu, 21 Feb 2019 08:04:06 -0900 Subject: Multiprocessing performance question In-Reply-To: <111a206c-0d34-49cb-aee7-ac1a2dfc0f47@DancesWithMice.info> References: <111a206c-0d34-49cb-aee7-ac1a2dfc0f47@DancesWithMice.info> Message-ID: Actually not a ?toy example? at all. It is simply the first step in gridding some data I am working with - a problem that is solved by tools like SatPy, but unfortunately I can?t use SatPy because it doesn?t recognize my file format, and you can?t load data directly. Writing a custom file importer for SatPy is probably my next step. That said, the entire process took around 60 seconds to run. As this step was taking 10, I figured it would be low-hanging fruit for speeding up the process. Obviously I was wrong. For what it?s worth, I did manage to re-factor the code, so instead of generating the entire grid up-front, I generate the boxes as needed to calculate the overlap with the data grid. This brought the processing time down to around 40 seconds, so a definite improvement there. --- Israel Brewster Software Engineer Alaska Volcano Observatory Geophysical Institute - UAF 2156 Koyukuk Drive Fairbanks AK 99775-7320 Work: 907-474-5172 cell: 907-328-9145 > On Feb 20, 2019, at 4:30 PM, DL Neil wrote: > > George > > On 21/02/19 1:15 PM, george trojan wrote: >> def create_box(x_y): >> return geometry.box(x_y[0] - 1, x_y[1], x_y[0], x_y[1] - 1) >> x_range = range(1, 1001) >> y_range = range(1, 801) >> x_y_range = list(itertools.product(x_range, y_range)) >> grid = list(map(create_box, x_y_range)) >> Which creates and populates an 800x1000 ?grid? (represented as a flat list >> at this point) of ?boxes?, where a box is a shapely.geometry.box(). This >> takes about 10 seconds to run. >> Looking at this, I am thinking it would lend itself well to >> parallelization. Since the box at each ?coordinate" is independent of all >> others, it seems I should be able to simply split the list up into chunks >> and process each chunk in parallel on a separate core. To that end, I >> created a multiprocessing pool: > > > I recall a similar discussion when folk were being encouraged to move away from monolithic and straight-line processing to modular functions - it is more (CPU-time) efficient to run in a straight line; than it is to repeatedly call, set-up, execute, and return-from a function or sub-routine! ie there is an over-head to many/all constructs! > > Isn't the 'problem' that it is a 'toy example'? That the amount of computing within each parallel process is small in relation to the inherent 'overhead'. > > Thus, if the code performed a reasonable analytical task within each box after it had been defined (increased CPU load), would you then notice the expected difference between the single- and multi-process implementations? > > > > From AKL to AK > -- > Regards =dn > -- > https://mail.python.org/mailman/listinfo/python-list From mnl.post at gmail.com Thu Feb 21 13:35:53 2019 From: mnl.post at gmail.com (mnl.post at gmail.com) Date: Thu, 21 Feb 2019 13:35:53 -0500 Subject: Feature suggestion: "Using declarations" i.e. context managers ("with" blocks) tied to scope/lifetime of the variable rather than to nesting In-Reply-To: References: Message-ID: (I sent this a few days ago but got bounced without a reason?don?t see it posted, so I?m trying one more time.) I thought this new C# feature would be a good thing to add to Python: https://vcsjones.com/2019/01/30/csharp-8-using-declarations/ The nesting required by context managers can be at odds with a program?s real structure. The code indentation in python should reflect the program logic and flow as much as possible, but for one thing, the context manager generally doesn't apply to most of the code it forces to be indented, but only to a few lines using the resource; for another, even for those lines, the context manager is usually not reflective of the flow anyway (unlike the classic control statements, such as for/next, if/else, and while). for example: with xxxxxx.open() as logfile: do this do that logfile.write() with xxxx as dbs_conn: do this do that logfile.write() Things like dbs_conn are functioning as just a variable (albeit one wants assurance its object will be determinstically destroyed)?the indentation reflects the span of lines the variable is being used (held open), but shouldn?t change the structure for that reason. (This is uglier as additional withs get nested.) From PythonList at DancesWithMice.info Thu Feb 21 13:34:14 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Fri, 22 Feb 2019 07:34:14 +1300 Subject: Multiprocessing performance question In-Reply-To: References: <111a206c-0d34-49cb-aee7-ac1a2dfc0f47@DancesWithMice.info> Message-ID: George: apologies for mis-identifying yourself as OP. Israel: On 22/02/19 6:04 AM, Israel Brewster wrote: > Actually not a ?toy example? at all. It is simply the first step in > gridding some data I am working with - a problem that is solved by tools > like SatPy, but unfortunately I can?t use SatPy because it doesn?t > recognize my file format, and you can?t load data directly. Writing a > custom file importer for SatPy is probably my next step. Not to focus on the word "toy", the governing issue is of setup cost cf the acceleration afforded by the parallel processing. In this case, the former is/was more-or-less as high as the latter, and your efforts were insufficiently rewarded. That said, if the computer was concurrently performing this task and a number of others, the number of cores available to you would decrease. At which point, speeds start heading backwards! This is largely speculation because only you know the task, objectives, and circumstances - however, for those 'playing along at home' and learning from your experiment... > That said, the entire process took around 60 seconds to run. As this > step was taking 10, I figured it would be low-hanging fruit for speeding > up the process. Obviously I was wrong. For what it?s worth, I did manage > to re-factor the code, so instead of generating the entire grid > up-front, I generate the boxes as needed to calculate the overlap with > the data grid. This brought the processing time down to around 40 > seconds, so a definite improvement there. Doing it on-demand. Now you're talking! Plus, if you're able to 'fit' the data into each box as it is created, that will help justify the setup/tear-down overhead cost for each async process. Well done! > --- > Israel Brewster > Software Engineer > Alaska Volcano Observatory > Geophysical Institute - UAF > 2156 Koyukuk Drive > Fairbanks AK 99775-7320 > Work: 907-474-5172 > cell: ?907-328-9145 > >> On Feb 20, 2019, at 4:30 PM, DL Neil > > wrote: >> >> George >> >> On 21/02/19 1:15 PM, george trojan wrote: >>> def create_box(x_y): >>> ????return geometry.box(x_y[0] - 1, x_y[1], ?x_y[0], x_y[1] - 1) >>> x_range = range(1, 1001) >>> y_range = range(1, 801) >>> x_y_range = list(itertools.product(x_range, y_range)) >>> grid = list(map(create_box, x_y_range)) >>> Which creates and populates an 800x1000 ?grid? (represented as a flat >>> list >>> at this point) of ?boxes?, where a box is a shapely.geometry.box(). This >>> takes about 10 seconds to run. >>> Looking at this, I am thinking it would lend itself well to >>> parallelization. Since the box at each ?coordinate" is independent of all >>> others, it seems I should be able to simply split the list up into chunks >>> and process each chunk in parallel on a separate core. To that end, I >>> created a multiprocessing pool: >> >> >> I recall a similar discussion when folk were being encouraged to move >> away from monolithic and straight-line processing to modular functions >> - it is more (CPU-time) efficient to run in a straight line; than it >> is to repeatedly call, set-up, execute, and return-from a function or >> sub-routine! ie there is an over-head to many/all constructs! >> >> Isn't the 'problem' that it is a 'toy example'? That the amount of >> computing within each parallel process is small in relation to the >> inherent 'overhead'. >> >> Thus, if the code performed a reasonable analytical task within each >> box after it had been defined (increased CPU load), would you then >> notice the expected difference between the single- and multi-process >> implementations? >> >> >> >> From AKL to AK >> -- >> Regards =dn >> -- >> https://mail.python.org/mailman/listinfo/python-list > -- Regards =dn From rhodri at kynesim.co.uk Thu Feb 21 13:48:36 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Thu, 21 Feb 2019 18:48:36 +0000 Subject: Feature suggestion: "Using declarations" i.e. context managers ("with" blocks) tied to scope/lifetime of the variable rather than to nesting In-Reply-To: References: Message-ID: <4cd2b4ac-8d8f-f5d0-f6f3-8383525cc433@kynesim.co.uk> On 21/02/2019 18:35, mnl.post at gmail.com wrote: > (I sent this a few days ago but got bounced without a reason?don?t see it > posted, so I?m trying one more time.) It was posted, and commented on. You can see the thread in the mailing list archives, if you don't believe me: https://mail.python.org/pipermail/python-list/2019-February/739548.html > I thought this new C# feature would be a good thing to add to Python: > https://vcsjones.com/2019/01/30/csharp-8-using-declarations/ > > The nesting required by context managers can be at odds with a program?s > real structure. Really? I thought in your example, particularly as revised here, the context managers showed up the real structure of the code nicely. -- Rhodri James *-* Kynesim Ltd From kwpolska at gmail.com Thu Feb 21 14:02:13 2019 From: kwpolska at gmail.com (Chris Warrick) Date: Thu, 21 Feb 2019 20:02:13 +0100 Subject: sys.modules In-Reply-To: <5c6ed48c$0$20311$426a74cc@news.free.fr> References: <5c6ed48c$0$20311$426a74cc@news.free.fr> Message-ID: On Thu, 21 Feb 2019 at 18:57, ast wrote: > > Hello > > Is it normal to have 151 entries in dictionary sys.modules > just after starting IDLE or something goes wrong ? > > >>> import sys > >>> len(sys.modules) > 151 > > Most of common modules seems to be already there, > os, itertools, random .... > > I thought that sys.modules was containing loaded modules > with import command only. sys.modules contains all modules that have been imported in the current session. Some of those imports happen in the background, without your knowledge ? for example, because these modules are required by the interpreter itself, or are part of IDLE. The number you see depends on the environment (I got 530 in ipython3, 34 in python3, 45 in python2) and is not in any way important. -- Chris Warrick PGP: 5EAAEA16 From tjol at tjol.eu Thu Feb 21 13:46:23 2019 From: tjol at tjol.eu (Thomas Jollans) Date: Thu, 21 Feb 2019 19:46:23 +0100 Subject: Feature suggestion: "Using declarations" i.e. context managers ("with" blocks) tied to scope/lifetime of the variable rather than to nesting In-Reply-To: References: Message-ID: On 21/02/2019 19:35, mnl.post at gmail.com wrote: > (I sent this a few days ago but got bounced without a reason?don?t see it > posted, so I?m trying one more time.) No, it got through. And it's in the archive: https://mail.python.org/pipermail/python-list/2019-February/739548.html From kushal at locationd.net Thu Feb 21 14:02:17 2019 From: kushal at locationd.net (Kushal Kumaran) Date: Thu, 21 Feb 2019 11:02:17 -0800 Subject: python3.7.2 won't compile with SSL support In-Reply-To: <003901d4c9ed$be26c130$3a744390$@epepm.cupet.cu> (Felix Lazaro Carbonell's message of "Thu, 21 Feb 2019 09:00:03 -0500") References: <003901d4c9ed$be26c130$3a744390$@epepm.cupet.cu> Message-ID: <875ztdart2.fsf@copper.locationd.net> "Felix Lazaro Carbonell" writes: > Hello: > > > > I'm trying to install python3.7.2 from source in debian9.8 but it doesn't > compile with SSL. > > > > I already installed openssl > > > > And ./configure -with-openssl=/usr/include/openssl/ yields: > > > > checking for openssl/ssl.h in /usr/include/openssl/... no > > > > and ssl.h is certainly in /usr/include/openssl/ > Looks like it is appending openssl/ssl.h to /usr/include/openssl/ and failing to find the header. It's been a long time since I attemped to build python by hand; perhaps you need to not specify the --with-openssl argument at all, if the headers are installed in the expected places. Have you seen https://github.com/pyenv/pyenv? -- regards, kushal From rosuav at gmail.com Thu Feb 21 14:10:43 2019 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 22 Feb 2019 06:10:43 +1100 Subject: sys.modules In-Reply-To: References: <5c6ed48c$0$20311$426a74cc@news.free.fr> Message-ID: On Fri, Feb 22, 2019 at 6:03 AM Chris Warrick wrote: > > On Thu, 21 Feb 2019 at 18:57, ast wrote: > > > > Hello > > > > Is it normal to have 151 entries in dictionary sys.modules > > just after starting IDLE or something goes wrong ? > > > > >>> import sys > > >>> len(sys.modules) > > 151 > > > > Most of common modules seems to be already there, > > os, itertools, random .... > > > > I thought that sys.modules was containing loaded modules > > with import command only. > > sys.modules contains all modules that have been imported in the > current session. Some of those imports happen in the background, > without your knowledge ? for example, because these modules are > required by the interpreter itself, or are part of IDLE. The number > you see depends on the environment (I got 530 in ipython3, 34 in > python3, 45 in python2) and is not in any way important. > The OP is technically correct in that they have been loaded with the "import" statement (or equivalent). What may not be obvious is that many modules will import other modules, which also get cached. Generally this is pretty insignificant, unless you're trying to benchmark startup performance or something; the module cache just magically speeds everything up for you. ChrisA From PythonList at DancesWithMice.info Thu Feb 21 14:32:19 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Fri, 22 Feb 2019 08:32:19 +1300 Subject: sys.modules In-Reply-To: <5c6ed48c$0$20311$426a74cc@news.free.fr> References: <5c6ed48c$0$20311$426a74cc@news.free.fr> Message-ID: <3d319d45-49dc-cee2-f57e-682638310cd2@DancesWithMice.info> Hello, On 22/02/19 5:40 AM, ast wrote: > Is it normal to have 151 entries in dictionary sys.modules > just after starting IDLE or something goes wrong ? > >>> import sys > >>> len(sys.modules) > 151 I don't use Idle. Written in python, doesn't it require various packages to run before it even talks to you, eg tkinter? Thus am not sure if they are also being counted. However:- After firing-up Python 3.7 from the cmdLN, my system reported only 60. Remember also, that the sys Run-time service is described as "System-specific parameters and functions", which will account for (some) differences by itself. > Most of common modules seems to be already there, > os, itertools, random .... > I thought that sys.modules was containing loaded modules > with import command only. Not quite true! Whereas the manual says <<< sys.modules This is a dictionary that maps module names to modules which have already been loaded. >>> also remember that import is not the only way modules are "loaded"! Built-in types (etc) are/must be loaded as part of python, otherwise nothing would work, eg float, int, list, contextlib, collections, functools, ... This is the modular/boot-strap method that is a feature of python. Web-refs: https://docs.python.org/3/tutorial/modules.html#standard-modules https://stackoverflow.com/questions/7643809/what-are-default-modules-in-python-which-are-imported-when-we-run-python-as-for -- Regards =dn From felix at epepm.cupet.cu Thu Feb 21 10:29:55 2019 From: felix at epepm.cupet.cu (Felix Lazaro Carbonell) Date: Thu, 21 Feb 2019 10:29:55 -0500 Subject: python3.7.2 won't compile with SSL support (solved) Message-ID: <004801d4c9fa$4c061120$e4123360$@epepm.cupet.cu> Incredibly: ./configure --with-ssl=/usr/include/openssl/ Made the trick!! Although --with-ssl is not documented in ./configure --help. Cheers, Felix. From tjreedy at udel.edu Thu Feb 21 22:54:14 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 21 Feb 2019 22:54:14 -0500 Subject: sys.modules In-Reply-To: <5c6ed48c$0$20311$426a74cc@news.free.fr> References: <5c6ed48c$0$20311$426a74cc@news.free.fr> Message-ID: On 2/21/2019 11:40 AM, ast wrote: > Hello > > Is it normal to have 151 entries in dictionary sys.modules > just after starting IDLE or something goes wrong ? The is the right number. When Python starts, it imports around 50 modules. When it runs IDLE, most of idlelib modules are imported, plus about 50 stdlib modules, including 4 or 5 tkinter modules. IDLE used to import many more modules, but I managed to delay some until needed, and reduced the startup time by about 25%. > >>> import sys > >>> len(sys.modules) > 151 Since this is your code, the report is for the user-code execution process, as opposed to the IDLE GUI process. Both processes are running when you get the prompt. -- Terry Jan Reedy From kibtes2 at gmail.com Fri Feb 22 04:44:39 2019 From: kibtes2 at gmail.com (kibtes2 at gmail.com) Date: Fri, 22 Feb 2019 01:44:39 -0800 (PST) Subject: Custom logging code(s) / numbers to the log level Error Message-ID: <1614d208-2568-4aeb-84cb-f91a0854412b@googlegroups.com> I wanted to write/extend the logging library to have a custom Error number for each exception (error) the code has. What's the best approach for this? If possible can someone please provide a good starting code snippet. Thanks From songbird at anthive.com Fri Feb 22 07:47:36 2019 From: songbird at anthive.com (songbird) Date: Fri, 22 Feb 2019 07:47:36 -0500 Subject: revisiting the "What am I running on?" question References: <7d52f95b-66fd-9919-da2f-25208a57ea85@potatochowder.com> <272619c5-9a99-dbb9-2f7e-939baf563a5b@tjol.eu> Message-ID: <8k75kf-om1.ln1@anthive.com> Chris Angelico wrote: > On Thu, Feb 21, 2019 at 12:56 AM songbird wrote: >> Thomas Jollans wrote: >> ... >> > I'm fairly sure "win32" was used on W9x as well. In any case it *was* >> > correct at the time, as early versions of Python also ran on DOS and >> > Windows 3.1. "windows" would not have been suitable. >> > >> > But yeah, we're stuck with it. There are obviously good reasons that it >> > wasn't changed when we moved to amd64, but it is annoying. >> >> to me it is confusing and looks outdated so it >> did not give me much confidence i had found the >> right thing to use and so i kept looking further. >> if someone could put a note in the docs about >> this it will likely help others. > > Put a note somewhere in Microsoft's docs or something - this isn't a > Python-specific thing. the document i quoted is a python document. songbird From songbird at anthive.com Fri Feb 22 07:55:55 2019 From: songbird at anthive.com (songbird) Date: Fri, 22 Feb 2019 07:55:55 -0500 Subject: revisiting the "What am I running on?" question References: <7d52f95b-66fd-9919-da2f-25208a57ea85@potatochowder.com> <272619c5-9a99-dbb9-2f7e-939baf563a5b@tjol.eu> Message-ID: eryk sun wrote: ... > The win-amd64 ABI is significantly different, but at the API level > there isn't a drastic difference between 32-bit and 64-bit Windows, so > there's no cognitive burden with perpetuating the Win32 name. The > official API name was actually changed to "Windows API" or WINAPI (or > WinAPI). But it would require a massive effort to change the culture. > There's no pressing need to expend that much time and energy over a > name. just adding a comment to the documentation that win32 also covers win64 would be a help IMO. for those who are using the startswith syntax and checking for "win" alone then you could change the string returned to "win32-win64" or some other string with the prefix "win" and it would still work with no changes needed. songbird From songbird at anthive.com Fri Feb 22 07:50:52 2019 From: songbird at anthive.com (songbird) Date: Fri, 22 Feb 2019 07:50:52 -0500 Subject: revisiting the "What am I running on?" question References: <7d52f95b-66fd-9919-da2f-25208a57ea85@potatochowder.com> Message-ID: Chris Angelico wrote: >songbird wrote: ... >> "Do I have temporary directory and file creation >> permissions on this system or not?" > > Then ask that question instead! And the answer might well be here: > > https://docs.python.org/3/library/tempfile.html if you recall my original post/code it uses tempfile and i asked for comments which i've gotten. thanks, songbird From Karsten.Hilbert at gmx.net Fri Feb 22 15:21:07 2019 From: Karsten.Hilbert at gmx.net (Karsten Hilbert) Date: Fri, 22 Feb 2019 21:21:07 +0100 Subject: confusion with os.chmod() and follow_symlinks Message-ID: <20190222202106.GA1416@hermes.hilbert.loc> Am I confused ? ncq at hermes:~$ python3 Python 3.7.2+ (default, Feb 2 2019, 14:31:48) [GCC 8.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> print(os.supports_follow_symlinks) {, , , , } >>> os.chmod in os.supports_follow_symlinks False >>> os.chmod('/tmp/test', 0o0700, follow_symlinks = False) Traceback (most recent call last): File "", line 1, in NotImplementedError: chmod: follow_symlinks unavailable on this platform >>> I would only have expected this exception when I actually request the unavailable functionality, like so: os.chmod('/tmp/test', 0o0700, follow_symlinks = True) This, however, works: os.chmod('/tmp/test', 0o0700) DESPITE the documentation saying os.chmod(path, mode, *, dir_fd=None, follow_symlinks=True) IOW, the default for being "True", which is certainly illogical to succeed when it is not even supported on this platform. Where is my reasoning going astray ? Thanks, Karsten -- GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B From Karsten.Hilbert at gmx.net Fri Feb 22 15:46:01 2019 From: Karsten.Hilbert at gmx.net (Karsten Hilbert) Date: Fri, 22 Feb 2019 21:46:01 +0100 Subject: confusion with os.chmod() and follow_symlinks In-Reply-To: <20190222202106.GA1416@hermes.hilbert.loc> References: <20190222202106.GA1416@hermes.hilbert.loc> Message-ID: <20190222204601.GB1416@hermes.hilbert.loc> On Fri, Feb 22, 2019 at 09:21:07PM +0100, Karsten Hilbert wrote: > Am I confused ? > > ncq at hermes:~$ python3 > Python 3.7.2+ (default, Feb 2 2019, 14:31:48) > [GCC 8.2.0] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> import os > >>> print(os.supports_follow_symlinks) > {, , , , } > >>> os.chmod in os.supports_follow_symlinks > False > >>> os.chmod('/tmp/test', 0o0700, follow_symlinks = False) > Traceback (most recent call last): > File "", line 1, in > NotImplementedError: chmod: follow_symlinks unavailable on this platform > >>> > > I would only have expected this exception when I actually > request the unavailable functionality, like so: > > os.chmod('/tmp/test', 0o0700, follow_symlinks = True) > > This, however, works: > > os.chmod('/tmp/test', 0o0700) > > DESPITE the documentation saying > > os.chmod(path, mode, *, dir_fd=None, follow_symlinks=True) > > IOW, the default for being "True", which is > certainly illogical to succeed when it is not even supported > on this platform. Because, naively, I'd have assumed this to work: os.chmod(directory, mode, follow_symlinks = (os.chmod in os.supports_follow_symlinks)) Karsten -- GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B From David.Raymond at tomtom.com Fri Feb 22 15:53:53 2019 From: David.Raymond at tomtom.com (David Raymond) Date: Fri, 22 Feb 2019 20:53:53 +0000 Subject: confusion with os.chmod() and follow_symlinks In-Reply-To: <20190222204601.GB1416@hermes.hilbert.loc> References: <20190222202106.GA1416@hermes.hilbert.loc> <20190222204601.GB1416@hermes.hilbert.loc> Message-ID: Not sure, but the way I read it follow_symlinks = True is the default behavior of systems that don't allow you to set it, and being able to set it to False is the special bit. So "allowing follow_symlinks" means it "allows you to change it to whatever you want", not "allows it to be True" Under https://docs.python.org/3.7/library/os.html#files-and-directories it even lists the bullet point as _not_ following symlinks -----Original Message----- From: Python-list [mailto:python-list-bounces+david.raymond=tomtom.com at python.org] On Behalf Of Karsten Hilbert Sent: Friday, February 22, 2019 3:46 PM To: python-list at python.org Subject: Re: confusion with os.chmod() and follow_symlinks On Fri, Feb 22, 2019 at 09:21:07PM +0100, Karsten Hilbert wrote: > Am I confused ? > > ncq at hermes:~$ python3 > Python 3.7.2+ (default, Feb 2 2019, 14:31:48) > [GCC 8.2.0] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> import os > >>> print(os.supports_follow_symlinks) > {, , , , } > >>> os.chmod in os.supports_follow_symlinks > False > >>> os.chmod('/tmp/test', 0o0700, follow_symlinks = False) > Traceback (most recent call last): > File "", line 1, in > NotImplementedError: chmod: follow_symlinks unavailable on this platform > >>> > > I would only have expected this exception when I actually > request the unavailable functionality, like so: > > os.chmod('/tmp/test', 0o0700, follow_symlinks = True) > > This, however, works: > > os.chmod('/tmp/test', 0o0700) > > DESPITE the documentation saying > > os.chmod(path, mode, *, dir_fd=None, follow_symlinks=True) > > IOW, the default for being "True", which is > certainly illogical to succeed when it is not even supported > on this platform. Because, naively, I'd have assumed this to work: os.chmod(directory, mode, follow_symlinks = (os.chmod in os.supports_follow_symlinks)) Karsten -- GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B -- https://mail.python.org/mailman/listinfo/python-list From __peter__ at web.de Fri Feb 22 16:03:16 2019 From: __peter__ at web.de (Peter Otten) Date: Fri, 22 Feb 2019 22:03:16 +0100 Subject: confusion with os.chmod() and follow_symlinks References: <20190222202106.GA1416@hermes.hilbert.loc> Message-ID: Karsten Hilbert wrote: > Am I confused ? > > ncq at hermes:~$ python3 > Python 3.7.2+ (default, Feb 2 2019, 14:31:48) > [GCC 8.2.0] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> import os > >>> print(os.supports_follow_symlinks) > {, , access>, , } >>> os.chmod > in os.supports_follow_symlinks False > >>> os.chmod('/tmp/test', 0o0700, follow_symlinks = False) > Traceback (most recent call last): > File "", line 1, in > NotImplementedError: chmod: follow_symlinks unavailable on this platform > >>> > > I would only have expected this exception when I actually > request the unavailable functionality, like so: > > os.chmod('/tmp/test', 0o0700, follow_symlinks = True) > > This, however, works: > > os.chmod('/tmp/test', 0o0700) > > DESPITE the documentation saying > > os.chmod(path, mode, *, dir_fd=None, follow_symlinks=True) > > IOW, the default for being "True", which is > certainly illogical to succeed when it is not even supported > on this platform. > > Where is my reasoning going astray ? The special feature is to change permissions of the symlink proper rather than those of its target. I think an inverted flag would indeed be clearer, e. g. os.chmod(..., modify_symlink=False) # instead of follow_symlinks=True os.chmod(..., modify_symlink=True) # instead of follow_symlinks=False; # may raise NotImplementedError From __peter__ at web.de Fri Feb 22 17:00:49 2019 From: __peter__ at web.de (Peter Otten) Date: Fri, 22 Feb 2019 23:00:49 +0100 Subject: Custom logging code(s) / numbers to the log level Error References: <1614d208-2568-4aeb-84cb-f91a0854412b@googlegroups.com> Message-ID: kibtes2 at gmail.com wrote: > I wanted to write/extend the logging library to have a custom Error number > for each exception (error) the code has. What's the best approach for > this? If possible can someone please provide a good starting code snippet. > Thanks You can pass arbitrary data via the `extra` parameter: $ cat log_errno.py import logging logger = logging.getLogger() logging.basicConfig(format="ERROR #%(errno)s: %(message)s") logger.error("First", extra=dict(errno=42)) logger.error("Second", extra=dict(errno=123)) $ python3 log_errno.py ERROR #42: First ERROR #123: Second If that's not you want you have to invest more time into a better description of your goal, with example code and output. From tjreedy at udel.edu Fri Feb 22 20:16:29 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Fri, 22 Feb 2019 20:16:29 -0500 Subject: revisiting the "What am I running on?" question In-Reply-To: References: <7d52f95b-66fd-9919-da2f-25208a57ea85@potatochowder.com> <272619c5-9a99-dbb9-2f7e-939baf563a5b@tjol.eu> Message-ID: On 2/22/2019 7:55 AM, songbird wrote: > eryk sun wrote: > ... >> The win-amd64 ABI is significantly different, but at the API level >> there isn't a drastic difference between 32-bit and 64-bit Windows, so >> there's no cognitive burden with perpetuating the Win32 name. The >> official API name was actually changed to "Windows API" or WINAPI (or >> WinAPI). But it would require a massive effort to change the culture. >> There's no pressing need to expend that much time and energy over a >> name. > > just adding a comment to the documentation that > win32 also covers win64 would be a help IMO. Could you open an issue on the tracker suggesting a specific edit at a specific place? -- Terry Jan Reedy From pedicularis at mail.com Fri Feb 22 22:45:35 2019 From: pedicularis at mail.com (Frank Miles) Date: Sat, 23 Feb 2019 03:45:35 -0000 (UTC) Subject: python3.7 installation failing - so why? Message-ID: I have a Debian/Linux machine that I just upgraded to the newer "testing" distribution. I'd done that earlier to another machine and all went well. With the latest machine, python2 is OK but python3 can barely run at all. For example: $ python3 Python 3.7.2+ (default, Feb 2 2019, 14:31:48) [GCC 8.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> help() Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.7/_sitebuiltins.py", line 102, in __call__ import pydoc File "/usr/lib/python3.7/pydoc.py", line 66, in import inspect File "/usr/lib/python3.7/inspect.py", line 40, in import linecache File "/usr/lib/python3.7/linecache.py", line 11, in import tokenize File "/usr/lib/python3.7/tokenize.py", line 33, in import re File "/usr/lib/python3.7/re.py", line 143, in class RegexFlag(enum.IntFlag): AttributeError: module 'enum' has no attribute 'IntFlag' >>> Question: how can I determine what has gone wrong? Reinstalling most of the packages hasn't done any good. I did a similar upgrade to another machine -- and it doesn't have this problem. Running md5sum on the above modules as well as /usr/bin/python3.7 generates the same numbers (comparing working and nonfunctional machines). Any clues/hints/ideas would be appreciated. There's a whole lot broken on this machine with python3 inoperable :( -F From rosuav at gmail.com Fri Feb 22 22:56:03 2019 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 23 Feb 2019 14:56:03 +1100 Subject: python3.7 installation failing - so why? In-Reply-To: References: Message-ID: On Sat, Feb 23, 2019 at 2:51 PM Frank Miles wrote: > > I have a Debian/Linux machine that I just upgraded to the newer "testing" > distribution. I'd done that earlier to another machine and all went > well. With the latest machine, python2 is OK but python3 can barely run > at all. For example: > > $ python3 > Python 3.7.2+ (default, Feb 2 2019, 14:31:48) > [GCC 8.2.0] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> help() > Traceback (most recent call last): > File "", line 1, in > File "/usr/lib/python3.7/_sitebuiltins.py", line 102, in __call__ > import pydoc > File "/usr/lib/python3.7/pydoc.py", line 66, in > import inspect > File "/usr/lib/python3.7/inspect.py", line 40, in > import linecache > File "/usr/lib/python3.7/linecache.py", line 11, in > import tokenize > File "/usr/lib/python3.7/tokenize.py", line 33, in > import re > File "/usr/lib/python3.7/re.py", line 143, in > class RegexFlag(enum.IntFlag): > AttributeError: module 'enum' has no attribute 'IntFlag' > >>> > > Question: how can I determine what has gone wrong? Hmm. I'd start with: $ which python3 $ dpkg -S `which python3` and from inside Python: >>> import sys; sys.path >>> import enum; enum.__file__ My best guess at the moment is that your "enum" package is actually a compatibility shim for earlier Python versions, less functional than the one provided by Python 3.7. You may need to *uninstall* a shim package. But I could well be wrong, and maybe there'd be a clue in your paths. ChrisA From pedicularis at mail.com Sat Feb 23 00:38:11 2019 From: pedicularis at mail.com (Frank Miles) Date: Sat, 23 Feb 2019 05:38:11 -0000 (UTC) Subject: python3.7 installation failing - so why? References: Message-ID: On Sat, 23 Feb 2019 14:56:03 +1100, Chris Angelico wrote: > On Sat, Feb 23, 2019 at 2:51 PM Frank Miles > wrote: >> >> I have a Debian/Linux machine that I just upgraded to the newer >> "testing" >> distribution. I'd done that earlier to another machine and all went >> well. With the latest machine, python2 is OK but python3 can barely >> run at all. For example: >> >> $ python3 Python 3.7.2+ (default, Feb 2 2019, 14:31:48) >> [GCC 8.2.0] on linux Type "help", "copyright", "credits" or "license" >> for more information. >> >>> help() >> Traceback (most recent call last): >> File "", line 1, in >> File "/usr/lib/python3.7/_sitebuiltins.py", line 102, in __call__ >> import pydoc >> File "/usr/lib/python3.7/pydoc.py", line 66, in >> import inspect >> File "/usr/lib/python3.7/inspect.py", line 40, in >> import linecache >> File "/usr/lib/python3.7/linecache.py", line 11, in >> import tokenize >> File "/usr/lib/python3.7/tokenize.py", line 33, in >> import re >> File "/usr/lib/python3.7/re.py", line 143, in >> class RegexFlag(enum.IntFlag): >> AttributeError: module 'enum' has no attribute 'IntFlag' >> >>> >> >>> >> Question: how can I determine what has gone wrong? > > Hmm. I'd start with: > > $ which python3 $ dpkg -S `which python3` > > and from inside Python: >>>> import sys; sys.path import enum; enum.__file__ > > My best guess at the moment is that your "enum" package is actually a > compatibility shim for earlier Python versions, less functional than the > one provided by Python 3.7. You may need to *uninstall* a shim package. > But I could well be wrong, and maybe there'd be a clue in your paths. > > ChrisA Whoopee! You nailed it! The path included /usr/local/lib/python3.7/dist-packages, which included an enum file as you suggested. The 'import enum; enum.__file__' (gonna have to look up that syntax) provided with the path to that directory. Many thanks Chris for a most helpful suggestion! -Frank From rosuav at gmail.com Sat Feb 23 00:44:17 2019 From: rosuav at gmail.com (Chris Angelico) Date: Sat, 23 Feb 2019 16:44:17 +1100 Subject: python3.7 installation failing - so why? In-Reply-To: References: Message-ID: On Sat, Feb 23, 2019 at 4:41 PM Frank Miles wrote: > > On Sat, 23 Feb 2019 14:56:03 +1100, Chris Angelico wrote: > > > On Sat, Feb 23, 2019 at 2:51 PM Frank Miles > > wrote: > >> > >> I have a Debian/Linux machine that I just upgraded to the newer > >> "testing" > >> distribution. I'd done that earlier to another machine and all went > >> well. With the latest machine, python2 is OK but python3 can barely > >> run at all. For example: > >> > >> $ python3 Python 3.7.2+ (default, Feb 2 2019, 14:31:48) > >> [GCC 8.2.0] on linux Type "help", "copyright", "credits" or "license" > >> for more information. > >> >>> help() > >> Traceback (most recent call last): > >> File "", line 1, in > >> File "/usr/lib/python3.7/_sitebuiltins.py", line 102, in __call__ > >> import pydoc > >> File "/usr/lib/python3.7/pydoc.py", line 66, in > >> import inspect > >> File "/usr/lib/python3.7/inspect.py", line 40, in > >> import linecache > >> File "/usr/lib/python3.7/linecache.py", line 11, in > >> import tokenize > >> File "/usr/lib/python3.7/tokenize.py", line 33, in > >> import re > >> File "/usr/lib/python3.7/re.py", line 143, in > >> class RegexFlag(enum.IntFlag): > >> AttributeError: module 'enum' has no attribute 'IntFlag' > >> >>> > >> >>> > >> Question: how can I determine what has gone wrong? > > > > Hmm. I'd start with: > > > > $ which python3 $ dpkg -S `which python3` > > > > and from inside Python: > >>>> import sys; sys.path import enum; enum.__file__ > > > > My best guess at the moment is that your "enum" package is actually a > > compatibility shim for earlier Python versions, less functional than the > > one provided by Python 3.7. You may need to *uninstall* a shim package. > > But I could well be wrong, and maybe there'd be a clue in your paths. > > > > ChrisA > > Whoopee! You nailed it! > The path included /usr/local/lib/python3.7/dist-packages, which included > an enum file as you suggested. The 'import enum; enum.__file__' (gonna > have to look up that syntax) provided with the path to that directory. > > Many thanks Chris for a most helpful suggestion! Cool! Glad that's working. There's nothing very special about the syntax per se - it's just looking up an attribute on the 'enum' module. What's special is that every module has an attribute called "__file__" which says which file it was loaded from (if any - there's no "sys.__file__" because the sys module comes from deep inside Python's core). Extremely handy. ChrisA From songbird at anthive.com Sat Feb 23 11:14:27 2019 From: songbird at anthive.com (songbird) Date: Sat, 23 Feb 2019 11:14:27 -0500 Subject: revisiting the "What am I running on?" question References: <7d52f95b-66fd-9919-da2f-25208a57ea85@potatochowder.com> <272619c5-9a99-dbb9-2f7e-939baf563a5b@tjol.eu> Message-ID: <3488kf-l04.ln1@anthive.com> Terry Reedy wrote: > On 2/22/2019 7:55 AM, songbird wrote: >> eryk sun wrote: >> ... >>> The win-amd64 ABI is significantly different, but at the API level >>> there isn't a drastic difference between 32-bit and 64-bit Windows, so >>> there's no cognitive burden with perpetuating the Win32 name. The >>> official API name was actually changed to "Windows API" or WINAPI (or >>> WinAPI). But it would require a massive effort to change the culture. >>> There's no pressing need to expend that much time and energy over a >>> name. >> >> just adding a comment to the documentation that >> win32 also covers win64 would be a help IMO. > > Could you open an issue on the tracker suggesting a specific edit at a > specific place? yes, i will next week when i can get back to this. songbird From tjreedy at udel.edu Sat Feb 23 12:42:51 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 23 Feb 2019 12:42:51 -0500 Subject: python3.7 installation failing - so why? In-Reply-To: References: Message-ID: On 2/22/2019 10:56 PM, Chris Angelico wrote: > On Sat, Feb 23, 2019 at 2:51 PM Frank Miles wrote: >> Question: how can I determine what has gone wrong? > > Hmm. I'd start with: > > $ which python3 > $ dpkg -S `which python3` > > and from inside Python: > >>> import sys; sys.path Adding ', sys.executable' is often handy and works on all systems with python. > >>> import enum; enum.__file__ I should remember to suggest this when an import runs but the imported module does not work right. -- Terry Jan Reedy From rosuav at gmail.com Sat Feb 23 14:16:01 2019 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 24 Feb 2019 06:16:01 +1100 Subject: python3.7 installation failing - so why? In-Reply-To: References: Message-ID: On Sun, Feb 24, 2019 at 4:44 AM Terry Reedy wrote: > > On 2/22/2019 10:56 PM, Chris Angelico wrote: > > On Sat, Feb 23, 2019 at 2:51 PM Frank Miles wrote: > > >> Question: how can I determine what has gone wrong? > > > > Hmm. I'd start with: > > > > $ which python3 > > $ dpkg -S `which python3` > > > > and from inside Python: > > >>> import sys; sys.path > > Adding ', sys.executable' is often handy and works on all systems with > python. True. Though I also was curious as to the package it came from (dpkg -S will tell you "oh, that file was installed from package XYZ"), and the OP did state that it was a Debian system ergo dpkg-managed. But for general questions, absolutely, sys.executable is handy. > > >>> import enum; enum.__file__ > > I should remember to suggest this when an import runs but the imported > module does not work right. > It's most often helpful when you get someone trying out regular expressions by writing a test file called "re.py" or something. :) ChrisA From martin.sand.christensen at gmail.com Sat Feb 23 18:28:03 2019 From: martin.sand.christensen at gmail.com (Martin Sand Christensen) Date: Sun, 24 Feb 2019 00:28:03 +0100 Subject: Best way to (re)load test data in Mongo DB Message-ID: <878sy62igs.fsf@gmail.com> Hi! I'm toying around with Pyramid and am using Mongo via MongoEngine for storage. I'm new to both Pyramid and MongoEngine. For every test case in the part of my suite that tests the data access layer I want to reload the database from scratch, but it feels like there should be a better and faster way than what I'm doing now. I have two distinct issues: 1. What's the fastest way of resetting the database to a clean state? 2. How do I load data with Mongo's internal _id being kept persistent? For issue #1: First of all I'd very much prefer to avoid having to use external client programs such as mongoimport to keep the number of dependencies minimal. Thus if there's a good way to do it through MongoEngine or PyMongo, that'd be preferable. My first shot at populating the database was simply to load data from a JSON file, use this to create my model objects (based on MongoEngine.Document) and save them to the DB. With a single-digit number of test cases and very limited data, this approach already takes close to a second, so I'm thinking there should be a faster way. It's Mongo, after all, not Oracle. My second version uses the underlying PyMongo module's insert_many() function to add all the documents for each collection in one go, but for this small amount of data it doesn't seem any faster. Which brings us to issue #2: For both of these strategies I'm unable to insert the Mongo ObjectId type _id. I haven't made _id properties part of my models, because they seem a bit... alien. I'd rather not include them solely to be able to load my test data properly. How can I populate _id as an ObjectId, not just as a string? (I'm assuming there's a difference, but it's never come up until now.) Am I being too difficult? I haven't been able to find much written about this topic: discussions about mocking drown out everything else the moment you mention 'mongo' and 'test' in the same search. Martin From martin.sand.christensen at gmail.com Sun Feb 24 05:52:39 2019 From: martin.sand.christensen at gmail.com (Martin Sand Christensen) Date: Sun, 24 Feb 2019 11:52:39 +0100 Subject: Best way to (re)load test data in Mongo DB References: <878sy62igs.fsf@gmail.com> <3d20f96f-b38d-4073-98e9-00091d0433b9@googlegroups.com> Message-ID: <87zhql1mrs.fsf@gmail.com> breamoreboy at gmail.com writes: > I was going to suggest mocking. I'm no expert so I suggest that you > search for "python test mock database" and go from there. Try to run > tests with a real db and you're likely to be at it until domesday. Mocking is definitely the order of the day for most tests, but I'd like to test the data layer itself, too, and I want a number of comprehensive function tests as well, and these need to exercise the whole stack. Mocking is great so long as you also remember to test the things that you mock. The point of this exercise is to eventually release it as a sort of example project of how to build a complex web application. Testing is particularly important to me since it's too often being overlooked in tutorials, or it only deals with trivial examples. Martin From gandalf at shopzeus.com Sun Feb 24 09:02:56 2019 From: gandalf at shopzeus.com (=?UTF-8?Q?Nagy_L=c3=a1szl=c3=b3_Zsolt?=) Date: Sun, 24 Feb 2019 15:02:56 +0100 Subject: Best way to (re)load test data in Mongo DB In-Reply-To: <878sy62igs.fsf@gmail.com> References: <878sy62igs.fsf@gmail.com> Message-ID: <89b742ec-57e8-19cb-119a-096bd123859e@shopzeus.com> ? Hello! We have a system where we have to create an exact copy of the original database for testing. The database size is over 800GB. We have found that using zfs snapshots and zfs clone is the best option. In order to do this, you have to enable journaling in mongodb. Then you can take a snapshot of the live database at any time, then clone that snapshot via "zfs clone" and finally start up a new mongodb instance on the clone. (We are using docker containers for this, but it doesn't really matter.) Using zfs clones has a performance penality (we have measured 80% performance), but it is still much better than restoring such a huge database with mongoimport. (8 hours vs. 10 seconds) Using zfs for mongodb is not recommended anyway, but you can keep a separate replica set member just for this purpose. For much smaller databases, you can (of course) use pure python code to insert test data into a test database. If it only takes seconds, then it is not a problem, right? I believe that for small tests (e.g. unit tests), using python code to populate a test database is just fine. There is no "best" way to do this, and there is always a tradefoff. For good tests, you need more data. If you want to restore more data, then it either takes more time, or you have to use external tools. Regarding question #2, you can always directly give an _id for documents if you want: https://api.mongodb.com/python/current/api/bson/objectid.html#bson.objectid.ObjectId This will let you create a database with known _id values, and run repeatable tests on that database. Best, ?? Laszlo > Hi! > > I'm toying around with Pyramid and am using Mongo via MongoEngine for > storage. I'm new to both Pyramid and MongoEngine. For every test case in > the part of my suite that tests the data access layer I want to reload > the database from scratch, but it feels like there should be a better > and faster way than what I'm doing now. > > I have two distinct issues: > 1. What's the fastest way of resetting the database to a clean state? > 2. How do I load data with Mongo's internal _id being kept persistent? > > For issue #1: > First of all I'd very much prefer to avoid having to use external client > programs such as mongoimport to keep the number of dependencies minimal. > Thus if there's a good way to do it through MongoEngine or PyMongo, > that'd be preferable. > > My first shot at populating the database was simply to load data from a > JSON file, use this to create my model objects (based on > MongoEngine.Document) and save them to the DB. With a single-digit > number of test cases and very limited data, this approach already takes > close to a second, so I'm thinking there should be a faster way. It's > Mongo, after all, not Oracle. > > My second version uses the underlying PyMongo module's insert_many() > function to add all the documents for each collection in one go, but for > this small amount of data it doesn't seem any faster. > > Which brings us to issue #2: > For both of these strategies I'm unable to insert the Mongo ObjectId > type _id. I haven't made _id properties part of my models, because they > seem a bit... alien. I'd rather not include them solely to be able to > load my test data properly. How can I populate _id as an ObjectId, not > just as a string? (I'm assuming there's a difference, but it's never > come up until now.) > > > Am I being too difficult? I haven't been able to find much written about > this topic: discussions about mocking drown out everything else the > moment you mention 'mongo' and 'test' in the same search. > > > Martin From rshepard at appl-ecosys.com Sun Feb 24 12:09:22 2019 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Sun, 24 Feb 2019 09:09:22 -0800 (PST) Subject: tkinter: variable width widgets in a grid? Message-ID: When placing widgets (e.g., Entry, Spinbox) in a grid layout can a length (visible width) be specified? For example, a telephone extension is a shorter string than the number itself. What's a good resource (other than this mail list) for such questions? TIA, Rich From zleap at disroot.org Sun Feb 24 12:28:21 2019 From: zleap at disroot.org (Paul Sutton) Date: Sun, 24 Feb 2019 17:28:21 +0000 Subject: tkinter: variable width widgets in a grid? In-Reply-To: References: Message-ID: On 24/02/2019 17:09, Rich Shepard wrote: > When placing widgets (e.g., Entry, Spinbox) in a grid layout can a length > (visible width) be specified? For example, a telephone extension is a > shorter > string than the number itself. > > What's a good resource (other than this mail list) for such questions? > > TIA, > > Rich Hi Not sure if this is helpful. I wrote a small application in python-tk, and you can indeed do this #define boxes namebx = Text(window, height=1, width=20) officebx = Text(window, height=1, width=20) With the main project (parts are work in progress) here https://github.com/zleap/AboutMe hope this helps regards Paul From vergos.nikolas at gmail.com Sun Feb 24 13:03:14 2019 From: vergos.nikolas at gmail.com (vergos.nikolas at gmail.com) Date: Sun, 24 Feb 2019 10:03:14 -0800 (PST) Subject: How to format a datetime MySQL database field to local using strftime() Message-ID: pymydb.execute( '''SELECT host, ref, location, useros, browser, visits, hits, downloads, authuser FROM guests WHERE pagesID = (SELECT ID FROM pages WHERE url = %s) ORDER BY visits DESC''', page ) data = pymydb.fetchall() for visit in visits: visit = visit.strftime('%A %e %b, %I:%M %p') 'visit' is being returned from database containing a MySQL datatime field that i want to change to another format which is ('%A %e %b, %I:%M %p') thats why i'm using that function. If not convert or comment out then results are not appearing normally. From best_lay at yahoo.com Sun Feb 24 13:11:06 2019 From: best_lay at yahoo.com (Wildman) Date: Sun, 24 Feb 2019 12:11:06 -0600 Subject: tkinter: variable width widgets in a grid? References: Message-ID: <0MmdncM52vqnQ-_BnZ2dnUU7-e-dnZ2d@giganews.com> On Sun, 24 Feb 2019 09:09:22 -0800, Rich Shepard wrote: > When placing widgets (e.g., Entry, Spinbox) in a grid layout can a length > (visible width) be specified? For example, a telephone extension is a shorter > string than the number itself. The height and width can be specified in the command that creates the wedget. > What's a good resource (other than this mail list) for such questions? http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html > TIA, > > Rich -- GNU/Linux user #557453 The cow died so I don't need your bull! From rshepard at appl-ecosys.com Sun Feb 24 13:24:42 2019 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Sun, 24 Feb 2019 10:24:42 -0800 (PST) Subject: tkinter: variable width widgets in a grid? In-Reply-To: References: Message-ID: On Sun, 24 Feb 2019, Paul Sutton wrote: > Not sure if this is helpful. I wrote a small application in python-tk, > and you can indeed do this ... Paul, Thank you. Regards, Rich From rshepard at appl-ecosys.com Sun Feb 24 13:28:08 2019 From: rshepard at appl-ecosys.com (Rich Shepard) Date: Sun, 24 Feb 2019 10:28:08 -0800 (PST) Subject: tkinter: variable width widgets in a grid? In-Reply-To: <0MmdncM52vqnQ-_BnZ2dnUU7-e-dnZ2d@giganews.com> References: <0MmdncM52vqnQ-_BnZ2dnUU7-e-dnZ2d@giganews.com> Message-ID: On Sun, 24 Feb 2019, Wildman via Python-list wrote: > The height and width can be specified in the command that creates the > wedget. Wildman, I thought so, but these were not mentioned in Alan Moore's book which is my reference for learning tkinter. Thus, my second question: >> What's a good resource (other than this mail list) for such questions? > http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html Excellent; I missed this in my web searchs. Thanks very much, Rich From PythonList at DancesWithMice.info Sun Feb 24 13:51:18 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Mon, 25 Feb 2019 07:51:18 +1300 Subject: How to format a datetime MySQL database field to local using strftime() In-Reply-To: References: Message-ID: <09bb3576-47b2-5723-832a-81c9ca41146d@DancesWithMice.info> Vergos, Please provide more information and show how you've debugged the code so far... On 25/02/19 7:03 AM, vergos.nikolas at gmail.com wrote: > pymydb.execute( '''SELECT host, ref, location, useros, browser, visits, hits, downloads, authuser FROM guests > WHERE pagesID = (SELECT ID FROM pages WHERE url = %s) ORDER BY visits DESC''', page ) > data = pymydb.fetchall() > for visit in visits: > visit = visit.strftime('%A %e %b, %I:%M %p') Is this the actual code? How do we get from the collection of tuples/dicts called "data", to an iterable called "visits"? Also: %e should likely be %w or %d. Which 'connector' are you using between MySQL and Python? (may not be the same as the one I favor) > 'visit' is being returned from database containing a MySQL datatime field that i want to change to another format which is ('%A %e %b, %I:%M %p') thats why i'm using that function. If not convert or comment out then results are not appearing normally. Where are these actual results? (might they help us to help you?) What do you see, as the same row/col, when using the MySQL cmdLN shell or MySQL-Workbench (etc)? Have you shown us what comes back as the first row's value for "visit"? Please check its type() before the code processes it further - is it coming back as a Python date or time format, or is it a string? Remember logging or even debug print()-s are your friend! -- Regards =dn From himanshuyadav9630994925 at gmail.com Sat Feb 23 23:21:02 2019 From: himanshuyadav9630994925 at gmail.com (Himanshu Yadav) Date: Sun, 24 Feb 2019 09:51:02 +0530 Subject: Dictionary Message-ID: fibs={0:0,1:1} def rfib(n): global fibs if not fibs.get(n): fibs[n]=rfib(n-2)+rfib(n-1) return fibs[n] Why it is gives error?? From __peter__ at web.de Sun Feb 24 14:12:09 2019 From: __peter__ at web.de (Peter Otten) Date: Sun, 24 Feb 2019 20:12:09 +0100 Subject: Dictionary References: Message-ID: Himanshu Yadav wrote: > fibs={0:0,1:1} > def rfib(n): > global fibs > if not fibs.get(n): > fibs[n]=rfib(n-2)+rfib(n-1) > return fibs[n] Please use cut and paste to make sure you are not introducing new errors like the inconsistent indentation above. > Why it is gives error?? Do not leave us guessing, always describe the error you encounter as precise as you can. Assuming indentation is not the problem in your actual code, here's one hint: > if not fibs.get(n): Will the if suite be executed or skipped for n == 0? Another hint: Python's dicts support containment tests directly, with the 'in' and the 'not in' operator. A few examples: >>> "a" in {"a": 42, "b": 24} True >>> "x" in {"a": 42, "b": 24} False >>> "a" not in {"a": 42, "b": 24} False >>> "x" not in {"a": 42, "b": 24} True From python at mrabarnett.plus.com Sun Feb 24 14:17:26 2019 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 24 Feb 2019 19:17:26 +0000 Subject: Dictionary In-Reply-To: References: Message-ID: On 2019-02-24 04:21, Himanshu Yadav wrote: > fibs={0:0,1:1} > def rfib(n): > global fibs > if not fibs.get(n): > fibs[n]=rfib(n-2)+rfib(n-1) > return fibs[n] > > Why it is gives error?? > What error? From vergos.nikolas at gmail.com Sun Feb 24 17:38:31 2019 From: vergos.nikolas at gmail.com (vergos.nikolas at gmail.com) Date: Sun, 24 Feb 2019 14:38:31 -0800 (PST) Subject: How to format a datetime MySQL database field to local using strftime() In-Reply-To: References: <09bb3576-47b2-5723-832a-81c9ca41146d@DancesWithMice.info> Message-ID: <5caf3a32-0f90-401a-be39-e26be1917f0e@googlegroups.com> ?? ???????, 24 ??????????? 2019 - 8:52:03 ?.?. UTC+2, ? ??????? DL Neil ??????: > Vergos, > > Please provide more information and show how you've debugged the code so > far... > > > On 25/02/19 7:03 AM, vergos.nikolas at gmail.com wrote: > > pymydb.execute( '''SELECT host, ref, location, useros, browser, visits, hits, downloads, authuser FROM guests > > WHERE pagesID = (SELECT ID FROM pages WHERE url = %s) ORDER BY visits DESC''', page ) > > data = pymydb.fetchall() > > for visit in visits: > > visit = visit.strftime('%A %e %b, %I:%M %p') > > Is this the actual code? How do we get from the collection of > tuples/dicts called "data", to an iterable called "visits"? > > Also: %e should likely be %w or %d. > > Which 'connector' are you using between MySQL and Python? > (may not be the same as the one I favor) > > > > 'visit' is being returned from database containing a MySQL datatime field that i want to change to another format which is ('%A %e %b, %I:%M %p') thats why i'm using that function. If not convert or comment out then results are not appearing normally. > > Where are these actual results? (might they help us to help you?) > What do you see, as the same row/col, when using the MySQL cmdLN shell > or MySQL-Workbench (etc)? > > Have you shown us what comes back as the first row's value for "visit"? > Please check its type() before the code processes it further - is it > coming back as a Python date or time format, or is it a string? > Remember logging or even debug print()-s are your friend! > > -- > Regards =dn The 'connector' that i'am using between MySQL and Python is 'bottle-pymysql' In the following code: def coalesce( data ): newdata = [] seen = {} for host, ref, location, useros, browser, visits, hits, downloads, authuser in data: # Here i have to decide how to group the rows together # I want an html row for every unique combination of (host) and that hits should be summed together key = host if key not in seen: newdata.append( [ [host], [ref], location, useros, browser, [visits], hits, [downloads], authuser ] ) seen[key] = len( newdata ) - 1 # Save index (for 'newdata') of this row else: # This row is a duplicate row with a different referrer & visit datetime & torrent download rowindex = seen[key] newdata[rowindex][0].append( host ) newdata[rowindex][1].append( ref ) newdata[rowindex][5].append( visits ) newdata[rowindex][6] += hits newdata[rowindex][7].append( downloads ) return newdata pymydb.execute( '''SELECT host, ref, location, useros, browser, visits, hits, downloads, authuser FROM guests WHERE pagesID = (SELECT ID FROM pages WHERE url = %s) ORDER BY visits DESC''', page ) data = pymydb.fetchall() newdata = coalesce( data ) for row in newdata: (hosts, refs, location, useros, browser, visits, hits, downloads, authuser) = row # start of table pdata = pdata + '' pdata = pdata + '
%s ' % hosts[0] pdata = pdata + '
' for item in (location, useros, browser): pdata = pdata + '
%s ' % item print( visits ) pdata = pdata + '
' if i try to print 'visits' filed before and during the loop the results is multiple [Mon Feb 25 00:23:55.165094 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits [Mon Feb 25 00:23:55.165098 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits [Mon Feb 25 00:23:55.165102 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits [Mon Feb 25 00:23:55.165107 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits [Mon Feb 25 00:23:55.165111 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits [Mon Feb 25 00:23:55.165115 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits [Mon Feb 25 00:23:55.165119 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits [Mon Feb 25 00:23:55.165123 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits [Mon Feb 25 00:23:55.165127 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits [Mon Feb 25 00:23:55.165132 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits [Mon Feb 25 00:23:55.165136 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits and actually all the variables that are being selected from the query are NOT being interpolated to their corresponding values. if for example i try to print: print( hosts, refs, location, useros, browser, visits, hits, downloads, authuser ) and i check the error_log i get long results like: [Mon Feb 25 00:33:52.768914 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] [['host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host'], ['ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref' [Mon Feb 25 00:33:52.769014 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits [Mon Feb 25 00:33:52.769020 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits [Mon Feb 25 00:33:52.769025 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits [Mon Feb 25 00:33:52.769029 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits [Mon Feb 25 00:33:52.769033 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits [Mon Feb 25 00:33:52.769037 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits [Mon Feb 25 00:33:52.769042 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits [Mon Feb 25 00:33:52.769046 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits [Mon Feb 25 00:33:52.769050 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits [Mon Feb 25 00:33:52.769054 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits [Mon Feb 25 00:33:52.769058 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits [Mon Feb 25 00:33:52.769063 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits [Mon Feb 25 00:33:52.769067 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits [Mon Feb 25 00:33:52.769071 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits The exact same code works WITHOUT error if i use the 'pymysql' connector instead of 'bottle-pymysql' Is this a bug in the connector or somethng else? From vergos.nikolas at gmail.com Sun Feb 24 17:53:41 2019 From: vergos.nikolas at gmail.com (vergos.nikolas at gmail.com) Date: Sun, 24 Feb 2019 14:53:41 -0800 (PST) Subject: How to format a datetime MySQL database field to local using strftime() In-Reply-To: <5caf3a32-0f90-401a-be39-e26be1917f0e@googlegroups.com> References: <09bb3576-47b2-5723-832a-81c9ca41146d@DancesWithMice.info> <5caf3a32-0f90-401a-be39-e26be1917f0e@googlegroups.com> Message-ID: ?? ???????, 25 ??????????? 2019 - 12:38:43 ?.?. UTC+2, ? ??????? vergos.... at gmail.com ??????: > ?? ???????, 24 ??????????? 2019 - 8:52:03 ?.?. UTC+2, ? ??????? DL Neil ??????: > > Vergos, > > > > Please provide more information and show how you've debugged the code so > > far... > > > > > > On 25/02/19 7:03 AM, vergos.nikolas at gmail.com wrote: > > > pymydb.execute( '''SELECT host, ref, location, useros, browser, visits, hits, downloads, authuser FROM guests > > > WHERE pagesID = (SELECT ID FROM pages WHERE url = %s) ORDER BY visits DESC''', page ) > > > data = pymydb.fetchall() > > > for visit in visits: > > > visit = visit.strftime('%A %e %b, %I:%M %p') > > > > Is this the actual code? How do we get from the collection of > > tuples/dicts called "data", to an iterable called "visits"? > > > > Also: %e should likely be %w or %d. > > > > Which 'connector' are you using between MySQL and Python? > > (may not be the same as the one I favor) > > > > > > > 'visit' is being returned from database containing a MySQL datatime field that i want to change to another format which is ('%A %e %b, %I:%M %p') thats why i'm using that function. If not convert or comment out then results are not appearing normally. > > > > Where are these actual results? (might they help us to help you?) > > What do you see, as the same row/col, when using the MySQL cmdLN shell > > or MySQL-Workbench (etc)? > > > > Have you shown us what comes back as the first row's value for "visit"? > > Please check its type() before the code processes it further - is it > > coming back as a Python date or time format, or is it a string? > > Remember logging or even debug print()-s are your friend! > > > > -- > > Regards =dn > > The 'connector' that i'am using between MySQL and Python is 'bottle-pymysql' > > In the following code: > > def coalesce( data ): > newdata = [] > seen = {} > for host, ref, location, useros, browser, visits, hits, downloads, authuser in data: > # Here i have to decide how to group the rows together > # I want an html row for every unique combination of (host) and that hits should be summed together > key = host > if key not in seen: > newdata.append( [ [host], [ref], location, useros, browser, [visits], hits, [downloads], authuser ] ) > seen[key] = len( newdata ) - 1 # Save index (for 'newdata') of this row > else: # This row is a duplicate row with a different referrer & visit datetime & torrent download > rowindex = seen[key] > newdata[rowindex][0].append( host ) > newdata[rowindex][1].append( ref ) > newdata[rowindex][5].append( visits ) > newdata[rowindex][6] += hits > newdata[rowindex][7].append( downloads ) > return newdata > > > pymydb.execute( '''SELECT host, ref, location, useros, browser, visits, hits, downloads, authuser FROM guests > WHERE pagesID = (SELECT ID FROM pages WHERE url = %s) ORDER BY visits DESC''', page ) > data = pymydb.fetchall() > > > newdata = coalesce( data ) > for row in newdata: > (hosts, refs, location, useros, browser, visits, hits, downloads, authuser) = row > > # start of table > pdata = pdata + '' > > pdata = pdata + '
%s ' % hosts[0] > > pdata = pdata + '
' > > for item in (location, useros, browser): > pdata = pdata + '
%s ' % item > > print( visits ) > pdata = pdata + '
' > > > if i try to print 'visits' filed before and during the loop the results is multiple > > [Mon Feb 25 00:23:55.165094 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits > [Mon Feb 25 00:23:55.165098 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits > [Mon Feb 25 00:23:55.165102 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits > [Mon Feb 25 00:23:55.165107 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits > [Mon Feb 25 00:23:55.165111 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits > [Mon Feb 25 00:23:55.165115 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits > [Mon Feb 25 00:23:55.165119 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits > [Mon Feb 25 00:23:55.165123 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits > [Mon Feb 25 00:23:55.165127 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits > [Mon Feb 25 00:23:55.165132 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits > [Mon Feb 25 00:23:55.165136 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits > > and actually all the variables that are being selected from the query are NOT being interpolated to their corresponding values. > > > if for example i try to print: > > print( hosts, refs, location, useros, browser, visits, hits, downloads, authuser ) > > and i check the error_log i get long results like: > > > [Mon Feb 25 00:33:52.768914 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] [['host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host'], ['ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref' > [Mon Feb 25 00:33:52.769014 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits > [Mon Feb 25 00:33:52.769020 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits > [Mon Feb 25 00:33:52.769025 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits > [Mon Feb 25 00:33:52.769029 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits > [Mon Feb 25 00:33:52.769033 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits > [Mon Feb 25 00:33:52.769037 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits > [Mon Feb 25 00:33:52.769042 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits > [Mon Feb 25 00:33:52.769046 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits > [Mon Feb 25 00:33:52.769050 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits > [Mon Feb 25 00:33:52.769054 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits > [Mon Feb 25 00:33:52.769058 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits > [Mon Feb 25 00:33:52.769063 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits > [Mon Feb 25 00:33:52.769067 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits > [Mon Feb 25 00:33:52.769071 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits > > The exact same code works WITHOUT error if i use the 'pymysql' connector instead of 'bottle-pymysql' > > Is this a bug in the connector or somethng else? after printing data and newdata i noticed that 'data' hold all values correctly while 'newdata' has the output i posted in my previous post. Something gone wrong when calling coalesce funtion, and row dont have the unpcked values. newdata = coalesce( data ) for row in newdata: (hosts, refs, location, useros, browser, visits, hits, downloads, authuser) = row From PythonList at DancesWithMice.info Sun Feb 24 20:20:19 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Mon, 25 Feb 2019 14:20:19 +1300 Subject: How to format a datetime MySQL database field to local using strftime() In-Reply-To: References: <09bb3576-47b2-5723-832a-81c9ca41146d@DancesWithMice.info> <5caf3a32-0f90-401a-be39-e26be1917f0e@googlegroups.com> Message-ID: <716cb07f-ff3f-caa3-ed59-82687abb13fa@DancesWithMice.info> Vergos, On 25/02/19 11:53 AM, vergos.nikolas at gmail.com wrote: > ?? ???????, 25 ??????????? 2019 - 12:38:43 ?.?. UTC+2, ? ??????? vergos.... at gmail.com ??????: >> ?? ???????, 24 ??????????? 2019 - 8:52:03 ?.?. UTC+2, ? ??????? DL Neil ??????: >>> Vergos, >>> >>> Please provide more information and show how you've debugged the code so >>> far... >>> >>> >>> On 25/02/19 7:03 AM, vergos.nikolas at gmail.com wrote: >>>> pymydb.execute( '''SELECT host, ref, location, useros, browser, visits, hits, downloads, authuser FROM guests >>>> WHERE pagesID = (SELECT ID FROM pages WHERE url = %s) ORDER BY visits DESC''', page ) >>>> data = pymydb.fetchall() >>>> for visit in visits: >>>> visit = visit.strftime('%A %e %b, %I:%M %p') >>> >>> Is this the actual code? How do we get from the collection of >>> tuples/dicts called "data", to an iterable called "visits"? >>> >>> Also: %e should likely be %w or %d. >>> >>> Which 'connector' are you using between MySQL and Python? >>> (may not be the same as the one I favor) >>> >>> >>>> 'visit' is being returned from database containing a MySQL datatime field that i want to change to another format which is ('%A %e %b, %I:%M %p') thats why i'm using that function. If not convert or comment out then results are not appearing normally. >>> >>> Where are these actual results? (might they help us to help you?) >>> What do you see, as the same row/col, when using the MySQL cmdLN shell >>> or MySQL-Workbench (etc)? >>> >>> Have you shown us what comes back as the first row's value for "visit"? >>> Please check its type() before the code processes it further - is it >>> coming back as a Python date or time format, or is it a string? >>> Remember logging or even debug print()-s are your friend! >>> >>> -- >>> Regards =dn >> >> The 'connector' that i'am using between MySQL and Python is 'bottle-pymysql' >> >> In the following code: >> >> def coalesce( data ): >> newdata = [] >> seen = {} >> for host, ref, location, useros, browser, visits, hits, downloads, authuser in data: >> # Here i have to decide how to group the rows together >> # I want an html row for every unique combination of (host) and that hits should be summed together >> key = host >> if key not in seen: >> newdata.append( [ [host], [ref], location, useros, browser, [visits], hits, [downloads], authuser ] ) >> seen[key] = len( newdata ) - 1 # Save index (for 'newdata') of this row >> else: # This row is a duplicate row with a different referrer & visit datetime & torrent download >> rowindex = seen[key] >> newdata[rowindex][0].append( host ) >> newdata[rowindex][1].append( ref ) >> newdata[rowindex][5].append( visits ) >> newdata[rowindex][6] += hits >> newdata[rowindex][7].append( downloads ) >> return newdata >> >> >> pymydb.execute( '''SELECT host, ref, location, useros, browser, visits, hits, downloads, authuser FROM guests >> WHERE pagesID = (SELECT ID FROM pages WHERE url = %s) ORDER BY visits DESC''', page ) >> data = pymydb.fetchall() >> >> >> newdata = coalesce( data ) >> for row in newdata: >> (hosts, refs, location, useros, browser, visits, hits, downloads, authuser) = row >> >> # start of table >> pdata = pdata + '' >> >> pdata = pdata + '
%s ' % hosts[0] >> >> pdata = pdata + '
' >> >> for item in (location, useros, browser): >> pdata = pdata + '
%s ' % item >> >> print( visits ) >> pdata = pdata + '
' >> >> >> if i try to print 'visits' filed before and during the loop the results is multiple >> >> [Mon Feb 25 00:23:55.165094 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits >> [Mon Feb 25 00:23:55.165098 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits >> [Mon Feb 25 00:23:55.165102 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits >> [Mon Feb 25 00:23:55.165107 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits >> [Mon Feb 25 00:23:55.165111 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits >> [Mon Feb 25 00:23:55.165115 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits >> [Mon Feb 25 00:23:55.165119 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits >> [Mon Feb 25 00:23:55.165123 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits >> [Mon Feb 25 00:23:55.165127 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits >> [Mon Feb 25 00:23:55.165132 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits >> [Mon Feb 25 00:23:55.165136 2019] [wsgi:error] [pid 15158] [remote 46.103.69.193:5068] visits >> >> and actually all the variables that are being selected from the query are NOT being interpolated to their corresponding values. >> >> >> if for example i try to print: >> >> print( hosts, refs, location, useros, browser, visits, hits, downloads, authuser ) >> >> and i check the error_log i get long results like: >> >> >> [Mon Feb 25 00:33:52.768914 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] [['host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host', 'host'], ['ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref', 'ref' >> [Mon Feb 25 00:33:52.769014 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits >> [Mon Feb 25 00:33:52.769020 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits >> [Mon Feb 25 00:33:52.769025 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits >> [Mon Feb 25 00:33:52.769029 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits >> [Mon Feb 25 00:33:52.769033 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits >> [Mon Feb 25 00:33:52.769037 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits >> [Mon Feb 25 00:33:52.769042 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits >> [Mon Feb 25 00:33:52.769046 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits >> [Mon Feb 25 00:33:52.769050 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits >> [Mon Feb 25 00:33:52.769054 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits >> [Mon Feb 25 00:33:52.769058 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits >> [Mon Feb 25 00:33:52.769063 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits >> [Mon Feb 25 00:33:52.769067 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits >> [Mon Feb 25 00:33:52.769071 2019] [wsgi:error] [pid 15545] [remote 46.103.69.193:5109] visits >> >> The exact same code works WITHOUT error if i use the 'pymysql' connector instead of 'bottle-pymysql' >> >> Is this a bug in the connector or somethng else? > > after printing data and newdata i noticed that 'data' hold all values correctly while 'newdata' has the output i posted in my previous post. > > Something gone wrong when calling coalesce funtion, and row dont have the unpcked values. > > newdata = coalesce( data ) > for row in newdata: > (hosts, refs, location, useros, browser, visits, hits, downloads, authuser) = row > Repeating the suggestion that the first row of data returned from the DB-query be closely analysed to ensure that its format and type(s) are exactly what you expect! The first row retrieved from the DB must populate newdata, by definition. So, temporarily add a break at the end of the foreach-loop in coalesce() - or at the end of the 'then' clause. Thus that function will return newdata as a list containing precisely one element. That element is itself a list (newdata being a list of lists). Some of the elements of that single inner-list are themselves lists. Because only one query-result row has been entered, each list-element will also be only a single element in length. Now you know the exact size and shape of the data! (and if it was difficult to read the preceding paragraph, keeping separate in your mind so many lists within other lists; perhaps that indicates why you're having such a hard time?) Now, instead of immediately passing newdata into the 'mainline' foreach-loop, pretty-print it. Does this output match your expectations? If that doesn't reveal clues about how to proceed, extend the pretty-print[ing] code to examine the inner-most element of each list, and confirm that its type() is str[ing] (or something else). (then we will find ourselves back on-track to solving the format of those 'date' fields) PS I had to think about it - might it be more 'readable' to keep a running counter to populate seen[]? Thus 'the number of unique hosts found (so far)' becomes a simple increment cf len( newdata ) - 1. Please let us know how you get on... -- Regards =dn From nathanntkou123 at gmail.com Sun Feb 24 21:13:59 2019 From: nathanntkou123 at gmail.com (nathanntkou123 at gmail.com) Date: Sun, 24 Feb 2019 18:13:59 -0800 (PST) Subject: how to setup for localhost:8000 In-Reply-To: <5ea1e31b-09ba-4b6a-be8b-58886c5c5f81@googlegroups.com> References: <5ea1e31b-09ba-4b6a-be8b-58886c5c5f81@googlegroups.com> Message-ID: <72c49ba6-1b1d-47b2-8ba4-5e6450898214@googlegroups.com> On Thursday, April 14, 2016 at 1:47:03 PM UTC-4, wrh... at gmail.com wrote: > Hi, > > I am working on window 7 and Python 3.5 to setup a localhost:8000 but it did not get through as shown below: > > python -m http.server > Serving HTTP on 0.0.0.0 port 8000 ... > > But it did not show the results. > > Can someone help me how to setup the localhost? > > Thanks, > Wen-Ruey From listsen at 0x906.com Sun Feb 24 22:00:22 2019 From: listsen at 0x906.com (0x906) Date: Sun, 24 Feb 2019 19:00:22 -0800 Subject: how to setup for localhost:8000 In-Reply-To: <72c49ba6-1b1d-47b2-8ba4-5e6450898214@googlegroups.com> References: <5ea1e31b-09ba-4b6a-be8b-58886c5c5f81@googlegroups.com> <72c49ba6-1b1d-47b2-8ba4-5e6450898214@googlegroups.com> Message-ID: There is a chance that I missed something with the 0.0.0.0. but I am pretty sure that the localhost IP is 127.0.0.1. > On Feb 24, 2019, at 6:13 PM, nathanntkou123 at gmail.com wrote: > > On Thursday, April 14, 2016 at 1:47:03 PM UTC-4, wrh... at gmail.com wrote: >> Hi, >> >> I am working on window 7 and Python 3.5 to setup a localhost:8000 but it did not get through as shown below: >>> python -m http.server >> Serving HTTP on 0.0.0.0 port 8000 ... >> >> But it did not show the results. >> >> Can someone help me how to setup the localhost? >> >> Thanks, >> Wen-Ruey > -- > https://mail.python.org/mailman/listinfo/python-list From cs at cskk.id.au Sun Feb 24 22:46:16 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Mon, 25 Feb 2019 14:46:16 +1100 Subject: how to setup for localhost:8000 In-Reply-To: References: Message-ID: <20190225034616.GA20568@cskk.homeip.net> On 24Feb2019 19:00, 0x906 wrote: >>> I am working on window 7 and Python 3.5 to setup a localhost:8000 >>> but it did not get through as shown below: >>>> python -m http.server >>> Serving HTTP on 0.0.0.0 port 8000 ... >>> But it did not show the results. >>> Can someone help me how to setup the localhost? > >There is a chance that I missed something with the 0.0.0.0. but I am pretty sure that the localhost IP is 127.0.0.1. Yeah. 0.0.0.0 is the wildcard address: the server will be listening on all the available addresses (127.0.0.1 and also any LAN address). Wen-Ruey may simply be missing that it is just running a web server. To actually see anything she/he needs to hit it with a web browser, for example with a URL like: http://127.0.0.1:8000/ Cheers, Cameron Simpson From ingy at ingy.net Mon Feb 25 00:20:57 2019 From: ingy at ingy.net (Ingy dot Net) Date: Sun, 24 Feb 2019 21:20:57 -0800 Subject: [ANN] PyYAML-5.1b1: YAML parser and emitter for Python Message-ID: ======================= Announcing PyYAML-5.1b1 (First beta release) ======================= The first beta release of PyYAML-5.1 has been uploaded to pypi.org. The final release is expected to land in the next 2 weeks. Normally we would only announce the final release, but this one has been a long time coming and has major changes, so we want people to know about the release process early on. A new MAJOR RELEASE of PyYAML is now available: https://pypi.org/project/PyYAML/ This is the first major release of PyYAML under the new maintenance team. Among the many changes listed below, this release specifically addresses the arbitrary code execution issue raised by: https://nvd.nist.gov/vuln/detail/CVE-2017-18342 (See https://github.com/yaml/pyyaml/wiki/PyYAML-yaml.load(input)-Deprecation for complete details). The PyYAML project is now maintained by the YAML and Python communities. Planning happens on the #yaml-dev, #pyyaml and #libyaml IRC channels on irc.freenode.net. Changes ======= * https://github.com/yaml/pyyaml/pull/35 -- Some modernization of the test running * https://github.com/yaml/pyyaml/pull/42 -- Install tox in a virtualenv * https://github.com/yaml/pyyaml/pull/45 -- Allow colon in a plain scalar in a flow context * https://github.com/yaml/pyyaml/pull/48 -- Fix typos * https://github.com/yaml/pyyaml/pull/55 -- Improve RepresenterError creation * https://github.com/yaml/pyyaml/pull/59 -- Resolves #57, update readme issues link * https://github.com/yaml/pyyaml/pull/60 -- Document and test Python 3.6 support * https://github.com/yaml/pyyaml/pull/61 -- Use Travis CI built in pip cache support * https://github.com/yaml/pyyaml/pull/62 -- Remove tox workaround for Travis CI * https://github.com/yaml/pyyaml/pull/63 -- Adding support to Unicode characters over codepoint 0xffff * https://github.com/yaml/pyyaml/pull/65 -- Support unicode literals over codepoint 0xffff * https://github.com/yaml/pyyaml/pull/75 -- add 3.12 changelog * https://github.com/yaml/pyyaml/pull/76 -- Fallback to Pure Python if Compilation fails * https://github.com/yaml/pyyaml/pull/84 -- Drop unsupported Python 3.3 * https://github.com/yaml/pyyaml/pull/102 -- Include license file in the generated wheel package * https://github.com/yaml/pyyaml/pull/105 -- Removed Python 2.6 & 3.3 support * https://github.com/yaml/pyyaml/pull/111 -- Remove commented out Psyco code * https://github.com/yaml/pyyaml/pull/129 -- Remove call to `ord` in lib3 emitter code * https://github.com/yaml/pyyaml/pull/143 -- Allow to turn off sorting keys in Dumper * https://github.com/yaml/pyyaml/pull/149 -- Test on Python 3.7-dev * https://github.com/yaml/pyyaml/pull/158 -- Support escaped slash in double quotes "\/" * https://github.com/yaml/pyyaml/pull/256 -- Make default_flow_style=False * https://github.com/yaml/pyyaml/pull/257 -- Deprecate yaml.load and add FullLoader and UnsafeLoader classes Resources ========= PyYAML IRC Channel: #pyyaml on irc.freenode.net PyYAML homepage: https://github.com/yaml/pyyaml PyYAML documentation: http://pyyaml.org/wiki/PyYAMLDocumentation Source and binary installers: https://pypi.org/project/PyYAML/ GitHub repository: https://github.com/yaml/pyyaml/ Bug tracking: https://github.com/yaml/pyyaml/issues YAML homepage: http://yaml.org/ YAML-core mailing list: http://lists.sourceforge.net/lists/listinfo/yaml-core About PyYAML ============ YAML is a data serialization format designed for human readability and interaction with scripting languages. PyYAML is a YAML parser and emitter for Python. PyYAML features a complete YAML 1.1 parser, Unicode support, pickle support, capable extension API, and sensible error messages. PyYAML supports standard YAML tags and provides Python-specific tags that allow to represent an arbitrary Python object. PyYAML is applicable for a broad range of tasks from complex configuration files to object serialization and persistence. Example ======= >>> import yaml >>> yaml.load(""" ... name: PyYAML ... description: YAML parser and emitter for Python ... homepage: https://github.com/yaml/pyyaml ... keywords: [YAML, serialization, configuration, persistence, pickle] ... """) {'keywords': ['YAML', 'serialization', 'configuration', 'persistence', 'pickle'], 'homepage': 'https://github.com/yaml/pyyaml', 'description': 'YAML parser and emitter for Python', 'name': 'PyYAML'} >>> print yaml.dump(_) name: PyYAML homepage: https://github.com/yaml/pyyaml description: YAML parser and emitter for Python keywords: [YAML, serialization, configuration, persistence, pickle] Maintainers =========== The following people are currently responsible for maintaining PyYAML: * Ingy d?t Net * Tina Mueller * Matt Davis and many thanks to all who have contribributed! See: https://github.com/yaml/pyyaml/pulls Copyright ========= Copyright (c) 2017-2019 Ingy d?t Net Copyright (c) 2006-2016 Kirill Simonov The PyYAML module was written by Kirill Simonov . It is currently maintained by the YAML and Python communities. PyYAML is released under the MIT license. See the file LICENSE for more details. From none at gmail.com Mon Feb 25 11:05:09 2019 From: none at gmail.com (ast) Date: Mon, 25 Feb 2019 17:05:09 +0100 Subject: Dictionary In-Reply-To: References: Message-ID: <5c741237$0$15176$426a74cc@news.free.fr> Le 24/02/2019 ? 05:21, Himanshu Yadav a ?crit?: > fibs={0:0,1:1} > def rfib(n): > global fibs > if not fibs.get(n): > fibs[n]=rfib(n-2)+rfib(n-1) > return fibs[n] > > Why it is gives error?? > Nothing to do with the malfunction, but you dont need to define fibs as global since you dont remap "fibs" in side the function. As explained by Peter, when n equals 0, not fibs.get(n) will return True and recursivity goes on with n=-1, -2 until you reach the maximum allowed number of recursion depth From none at gmail.com Mon Feb 25 11:25:22 2019 From: none at gmail.com (ast) Date: Mon, 25 Feb 2019 17:25:22 +0100 Subject: Quirk difference between classes and functions Message-ID: <5c7416f4$0$21614$426a74cc@news.free.fr> Hello I noticed a quirk difference between classes and functions >>> x=0 >>> >>> class Test: x = x+1 print(x) x = x+1 print(x) 1 2 >>> print(x) 0 Previous code doesn't generate any errors. x at the right of = in first "x = x+1" line is the global one (x=0), then x becomes local within a function, this is not allowed >>> x = 0 >>> >>> def f(): x = x+1 >>> f() UnboundLocalError: local variable 'x' referenced before assignment Since x is written inside the function, it is considered as a local variable and x in x+1 is undefined so this throw an exception Any comment ? From PythonList at DancesWithMice.info Mon Feb 25 14:02:51 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Tue, 26 Feb 2019 08:02:51 +1300 Subject: How to format a datetime MySQL database field to local using strftime() In-Reply-To: <7td87e5kld47lkeir4usvj4q1jhi44d0h7@4ax.com> References: <09bb3576-47b2-5723-832a-81c9ca41146d@DancesWithMice.info> <5caf3a32-0f90-401a-be39-e26be1917f0e@googlegroups.com> <716cb07f-ff3f-caa3-ed59-82687abb13fa@DancesWithMice.info> <7td87e5kld47lkeir4usvj4q1jhi44d0h7@4ax.com> Message-ID: <02b39386-102b-46c0-6ccc-7ef512fa8fc6@DancesWithMice.info> On 26/02/19 7:47 AM, Dennis Lee Bieber wrote: > On Mon, 25 Feb 2019 13:00:03 -0500, Dennis Lee Bieber > declaimed the following: > > > My apologies for the mis-attribution -- due to spam, I tend to filter > out @gmail.com posts (the one flaw with Forte Agent -- it only filters news > groups on subject or author, whereas it can filter email on many other > headers -- but I don't use Agent for email ). I didn't realize I was > replying to a reply... No problem - have done same myself. I noted that "Wulfraed" answered a very similar question, right 'here', a few years back - so I was half-waiting for you to jump in! At this stage, the OP doesn't seem to have mastered converting MySQL results into Python (a pain in and of itself). Accordingly, whilst a pythonic improvement was suggested along with debugging methods, had deferred SQL improvements until the root-problem was solved. Thanks! -- Regards =dn From PythonList at DancesWithMice.info Mon Feb 25 14:56:25 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Tue, 26 Feb 2019 08:56:25 +1300 Subject: Quirk difference between classes and functions In-Reply-To: <5c7416f4$0$21614$426a74cc@news.free.fr> References: <5c7416f4$0$21614$426a74cc@news.free.fr> Message-ID: <642864ac-9ff4-f749-f18a-cacf2048f5e7@DancesWithMice.info> On 26/02/19 5:25 AM, ast wrote: > I noticed a quirk difference between classes and functions > >>> x=0 > >>> class Test: > ??????? x = x+1 > ??????? print(x) > ??????? x = x+1 > ??????? print(x) ... > Previous code doesn't generate any errors. > x at the right of = in first "x = x+1" line is > the global one (x=0), then x becomes local > within a function, this is not allowed > >>> x = 0 > >>> def f(): > ??????? x = x+1 > >>> f() > UnboundLocalError: local variable 'x' referenced before assignment > Since x is written inside the function, it is considered as a local > variable and x in x+1 is undefined so this throw an exception > Any comment ? At first I misunderstood the question, and even now I'm slightly mystified:- Is the observation to do with the principles of "closure" (being applied to the function) compared with the differences between class and instance variables? For a little more fun, try expanding Test with: def __init__( self ): self.x = "something else" What happens when you look at Test.__dict__? What happens after instantiation: t = Test() print( t.__dict__ ) For extra credit: how does this change if we try modifying attributes from 'outside' the class definition? t.x = 3.333333 Are they all 'the same' or 'all different'? A question from me: (I'm not an O-O 'native' having taken to it long after first learning 'programming') I've used class attributes to hold 'constants', eg MAXIMUM_WEIGHT = 100 Thus only an assignment to which reference/assertions will be made 'later'. I have initialised a counter to zero and then incremented within the instantiated object's __init__(), ie keeping track of how many of 'these objects' have been instantiated. So, I can imagine taking a value from outside the class' namespace, modifying it in some way (per the first "x = x+1") and then retaining the result as a class attribute (a calculation performed once - when the class code is compiled). However, (after that first calculation/definition of the class attribute) why would one (normally) want to redefine the same class attribute (the second x = x+1), at the 'class level'? (perhaps just done for its amusement value?) -- Regards =dn From lukasz at langa.pl Mon Feb 25 15:05:41 2019 From: lukasz at langa.pl (=?utf-8?Q?=C5=81ukasz_Langa?=) Date: Mon, 25 Feb 2019 21:05:41 +0100 Subject: [RELEASE] Python 3.8.0a1 is now available for testing Message-ID: <8AFF29B7-D3DB-4DE1-BAF7-CAE6F4017378@langa.pl> I packaged another release. Go get it here: https://www.python.org/downloads/release/python-380a2/ Python 3.8.0a2 is the second of four planned alpha releases of Python 3.8, the next feature release of Python. During the alpha phase, Python 3.8 remains under heavy development: additional features will be added and existing features may be modified or deleted. Please keep in mind that this is a preview release and its use is not recommended for production environments. The next preview release, 3.8.0a3, is planned for 2019-03-25. This time around the stable buildbots were a bit less green than they should have. This early in the cycle, I didn't postpone the release and I didn't use the revert hammer. But soon enough, I will. Let's make sure future changes keep the buildbots happy. - ? -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: Message signed with OpenPGP URL: From rosuav at gmail.com Mon Feb 25 15:15:56 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 26 Feb 2019 07:15:56 +1100 Subject: Quirk difference between classes and functions In-Reply-To: <642864ac-9ff4-f749-f18a-cacf2048f5e7@DancesWithMice.info> References: <5c7416f4$0$21614$426a74cc@news.free.fr> <642864ac-9ff4-f749-f18a-cacf2048f5e7@DancesWithMice.info> Message-ID: On Tue, Feb 26, 2019 at 6:58 AM DL Neil wrote: > > On 26/02/19 5:25 AM, ast wrote: > > I noticed a quirk difference between classes and functions > > >>> x=0 > > >>> class Test: > > x = x+1 > > print(x) > > x = x+1 > > print(x) > ... > > > Previous code doesn't generate any errors. > > x at the right of = in first "x = x+1" line is > > the global one (x=0), then x becomes local > > within a function, this is not allowed > > >>> x = 0 > > >>> def f(): > > x = x+1 > > >>> f() > > UnboundLocalError: local variable 'x' referenced before assignment > > Since x is written inside the function, it is considered as a local > > variable and x in x+1 is undefined so this throw an exception > > Any comment ? > > > At first I misunderstood the question, and even now I'm slightly mystified:- > > Is the observation to do with the principles of "closure" (being applied > to the function) compared with the differences between class and > instance variables? Classes and functions behave differently. Inside a function, a name is local if it's ever assigned to; but in a class, this is not the case. (Honestly, I'm not sure why this is, but it's hardly ever significant; metaprogramming seldom runs into this kind of thing.) > A question from me: (I'm not an O-O 'native' having taken to it long > after first learning 'programming') I've used class attributes to hold > 'constants', eg > > MAXIMUM_WEIGHT = 100 > > Thus only an assignment to which reference/assertions will be made 'later'. > > I have initialised a counter to zero and then incremented within the > instantiated object's __init__(), ie keeping track of how many of 'these > objects' have been instantiated. Once you're inside a method, you would reference it with a dot - either "self.MAXIMUM_WEIGHT" or "cls.MAXIMUM_WEIGHT". So it's now an attribute, not a local name. > So, I can imagine taking a value from outside the class' namespace, > modifying it in some way (per the first "x = x+1") and then retaining > the result as a class attribute (a calculation performed once - when the > class code is compiled). Yes, this is perfectly legitimate. Not common but legit. > However, (after that first calculation/definition of the class > attribute) why would one (normally) want to redefine the same class > attribute (the second x = x+1), at the 'class level'? > (perhaps just done for its amusement value?) That would not be common either. If you're redefining a variable multiple times within a class block, you probably don't also have it at top level. (For instance, you can put a "for" loop inside a class statement to create attributes/methods, but the loop variable is unlikely to also be a global.) But hey! It's fun! :) ChrisA From martin.sand.christensen at gmail.com Mon Feb 25 15:34:37 2019 From: martin.sand.christensen at gmail.com (Martin Sand Christensen) Date: Mon, 25 Feb 2019 21:34:37 +0100 Subject: Best way to (re)load test data in Mongo DB References: <878sy62igs.fsf@gmail.com> <89b742ec-57e8-19cb-119a-096bd123859e@shopzeus.com> Message-ID: <87va171uaq.fsf@gmail.com> Nagy L?szl? Zsolt writes: > We have a system where we have to create an exact copy of the original > database for testing. The database size is over 800GB. [...] That all sounds pretty cool, but it's precisely the opposite of what I'm trying to acheive: keeping things as simple as possible. Snapshotting is neat for testing, especially for the type of snapshots that writes deltas on top of some base. ZopeDB offers precisely this sort of feature directly, I've read; that's what I'd wish from every database. > For much smaller databases, you can (of course) use pure python code to > insert test data into a test database. If it only takes seconds, then it > is not a problem, right? I believe that for small tests (e.g. unit > tests), using python code to populate a test database is just fine. Yeah... I'd just hoped to push it further down given that I only have about a handful entries for each collection. > Regarding question #2, you can always directly give an _id for documents > if you want: > > https://api.mongodb.com/python/current/api/bson/objectid.html#bson.objectid.ObjectId Cheers. I'll give it another go. Martin From ssorgent at hotmail.com Mon Feb 25 20:15:32 2019 From: ssorgent at hotmail.com (Scott Sorgent) Date: Tue, 26 Feb 2019 01:15:32 +0000 Subject: Connector/Python, MySQL Workbench Issue In-Reply-To: References: Message-ID: I was trying to install MySQL Workbench and it asked me to install the Connector/Python 3.7. I installed Python 3.7.2, restarted the computer and tried to install MySQL workbench again and it told me again that I needed to install Connector/Python 3.7. I found the download for MySQL Connector/Python 3.7 then downloaded and tried installing, but immediately, I get a popup saying Python v3.7 not installed. Why is this happening and what should I do to get everything installed and working? Thanks, Scott From PythonList at DancesWithMice.info Mon Feb 25 20:50:12 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Tue, 26 Feb 2019 14:50:12 +1300 Subject: Connector/Python, MySQL Workbench Issue In-Reply-To: References: Message-ID: <4bb75919-e066-40f0-59c0-45bddd112f68@DancesWithMice.info> Scott, On 26/02/19 2:15 PM, Scott Sorgent wrote: > > I was trying to install MySQL Workbench and it asked me to install the Connector/Python 3.7. I installed Python 3.7.2, restarted the computer and tried to install MySQL workbench again and it told me again that I needed to install Connector/Python 3.7. I found the download for MySQL Connector/Python 3.7 then downloaded and tried installing, but immediately, I get a popup saying Python v3.7 not installed. Why is this happening and what should I do to get everything installed and working? Can we start with the name and version of the Operating System you are using? Are you installing the three products as a user or root/an administrator? -- Regards =dn From PythonList at DancesWithMice.info Mon Feb 25 21:32:18 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Tue, 26 Feb 2019 15:32:18 +1300 Subject: Connector/Python, MySQL Workbench Issue In-Reply-To: References: <4bb75919-e066-40f0-59c0-45bddd112f68@DancesWithMice.info> Message-ID: <357bf5e8-fa2c-867b-3a72-a6b52c6992fc@DancesWithMice.info> Scott, Someone else will be better able to assist - I use Linux. In the mean-time, may I suggest combing-through the MySQL docs for installing on Windows - the suspicion about Administrator cf user remains... Yes, I often find MySQL-Workbench a useful tool when interacting with RDBMSes on remote machines and VPSes. Also, have been hung-up on mismatching v5.n or v8.n between my m/c and the other. Sigh! On 26/02/19 3:16 PM, Scott Sorgent wrote: > OS:? Windows 10 Home version 1803, 64 bit, x64 processor, Intel Core I7 > Processor > > I believe I'm just installing as an individual user.? I opted for the > Full Installation on the MySQL Workbench program though which tries to > install everything.? I'm going to be using for a big MySQL database > through AWS. > ------------------------------------------------------------------------ > *From:* Python-list > on behalf of DL > Neil > *Sent:* Monday, February 25, 2019 5:50 PM > *To:* 'Python' > *Subject:* Re: Connector/Python, MySQL Workbench Issue > Scott, > > On 26/02/19 2:15 PM, Scott Sorgent wrote: >> >> I was trying to install MySQL Workbench and it asked me to install the Connector/Python 3.7.? I installed Python 3.7.2, restarted the computer and tried to install MySQL workbench again and it told me again that I needed to install Connector/Python 3.7. I found the download for MySQL Connector/Python 3.7 then downloaded and > tried installing, but immediately, I get a popup saying Python v3.7 not > installed.? Why is this happening and what should I do to get everything > installed and working? > > > Can we start with the name and version of the Operating System you are > using? > > Are you installing the three products as a user or root/an administrator? > > -- > Regards =dn > -- > https://mail.python.org/mailman/listinfo/python-list -- Regards =dn From Gronicus at SGA.Ninja Mon Feb 25 20:55:25 2019 From: Gronicus at SGA.Ninja (Steve) Date: Mon, 25 Feb 2019 20:55:25 -0500 Subject: Connector/Python, MySQL Workbench Issue In-Reply-To: References: Message-ID: <000001d4cd76$57f3af90$07db0eb0$@SGA.Ninja> Check to see if the path was updated. Footnote: Ultrasound Technician Asks Pregnant Woman If She'd Like To Know Baby's Name -----Original Message----- From: Python-list On Behalf Of Scott Sorgent Sent: Monday, February 25, 2019 8:16 PM To: python-list at python.org Subject: Connector/Python, MySQL Workbench Issue I was trying to install MySQL Workbench and it asked me to install the Connector/Python 3.7. I installed Python 3.7.2, restarted the computer and tried to install MySQL workbench again and it told me again that I needed to install Connector/Python 3.7. I found the download for MySQL Connector/Python 3.7 then downloaded and tried installing, but immediately, I get a popup saying Python v3.7 not installed. Why is this happening and what should I do to get everything installed and working? Thanks, Scott -- https://mail.python.org/mailman/listinfo/python-list From sharingkapoting at gmail.com Mon Feb 25 22:07:45 2019 From: sharingkapoting at gmail.com (Ravindranath Barathy) Date: Mon, 25 Feb 2019 19:07:45 -0800 (PST) Subject: Encrypting username and password within yaml file Message-ID: <8362b5c1-7724-4ed3-84a3-31558540126b@googlegroups.com> Im trying to create a flask app that can take in a yaml file with the following values, info: url: http://something.com/api username: user1 password: secret This yaml file is then read by the flask app and uses the info to interact with the api. Everything works file in dev but, when I want to put this in prod i dont want the username and password to be in clear text. Any recommendation where I can use encrypted values in the yaml and decrypt then in the server side? What are also some other ways to achieve this if passing the username and password in the yaml is not recommended. From rosuav at gmail.com Mon Feb 25 23:28:12 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 26 Feb 2019 15:28:12 +1100 Subject: Encrypting username and password within yaml file In-Reply-To: <8362b5c1-7724-4ed3-84a3-31558540126b@googlegroups.com> References: <8362b5c1-7724-4ed3-84a3-31558540126b@googlegroups.com> Message-ID: On Tue, Feb 26, 2019 at 2:11 PM Ravindranath Barathy wrote: > > Im trying to create a flask app that can take in a yaml file with the following values, > > info: > url: http://something.com/api > username: user1 > password: secret > > > This yaml file is then read by the flask app and uses the info to interact with the api. Everything works file in dev but, when I want to put this in prod i dont want the username and password to be in clear text. Any recommendation where I can use encrypted values in the yaml and decrypt then in the server side? > > What are also some other ways to achieve this if passing the username and password in the yaml is not recommended. Sounds to me like the API requires the password in clear text, which means that, one way or another, it's going to be clear text. Correct me if I'm wrong on that point. You CAN avoid having the password in your repository, though. Look into the ways that your production platform can provide environment variables to your app. For instance, Heroku allows you to set config variables, or a self-hosted service run through a systemd script can have Environment= directives. You can then put the password in there, and have it available in os.environ[]. There are still opportunities for leakage, but far fewer than if you have a file in your source repository with the clear text. ChrisA From asimejaz104 at gmail.com Mon Feb 25 23:15:14 2019 From: asimejaz104 at gmail.com (asimejaz104 at gmail.com) Date: Mon, 25 Feb 2019 20:15:14 -0800 (PST) Subject: Two Tier Cloud Network Message-ID: <1e935daa-a168-4df9-83a3-901bb346bd37@googlegroups.com> I am a beginner in Python, I need your help in Python coding. I want to create a two-tier cloud network which consists of the cloudlets and DataCenters, cloudlets and DataCenters have virtual machines which are attached to each other. Thanks in advance From ben+python at benfinney.id.au Tue Feb 26 00:25:50 2019 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 26 Feb 2019 16:25:50 +1100 Subject: Two Tier Cloud Network References: <1e935daa-a168-4df9-83a3-901bb346bd37@googlegroups.com> Message-ID: <86a7ij3yu9.fsf@benfinney.id.au> asimejaz104 at gmail.com writes: > I am a beginner in Python, I need your help in Python coding. Welcome! You are in the right place to discuss Python. > I want to create a two-tier cloud network which consists of the > cloudlets and DataCenters, cloudlets and DataCenters have virtual > machines which are attached to each other. Thanks in advance That sounds like an interesting problem. It's very large and vaguely described though. I assume you are smart enough to not ask for strangers to write a big program to your specifications just because you ask, right? What is the Python code you have already written? What do you expect your code to do, and what does it do instead? What is the exact error output? -- \ ?? a Microsoft Certified System Engineer is to information | `\ technology as a McDonalds Certified Food Specialist is to the | _o__) culinary arts.? ?Michael Bacarella | Ben Finney From greg.ewing at canterbury.ac.nz Tue Feb 26 01:00:27 2019 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Tue, 26 Feb 2019 19:00:27 +1300 Subject: Quirk difference between classes and functions In-Reply-To: References: <5c7416f4$0$21614$426a74cc@news.free.fr> <642864ac-9ff4-f749-f18a-cacf2048f5e7@DancesWithMice.info> Message-ID: Chris Angelico wrote: > Classes and functions behave differently. Inside a function, a name is > local if it's ever assigned to; but in a class, this is not the case. Actually, it is. Assigning to a name in a class body makes it part of the class namespace, which is the local namespace at the time the class body is executed. The unusual thing about a class namespace is that it doesn't form part of the scope of closures created within the class. So methods, for example, can't directy see attributes of the class they're defined in. -- Greg From rosuav at gmail.com Tue Feb 26 01:11:57 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 26 Feb 2019 17:11:57 +1100 Subject: Quirk difference between classes and functions In-Reply-To: References: <5c7416f4$0$21614$426a74cc@news.free.fr> <642864ac-9ff4-f749-f18a-cacf2048f5e7@DancesWithMice.info> Message-ID: On Tue, Feb 26, 2019 at 5:06 PM Gregory Ewing wrote: > > Chris Angelico wrote: > > Classes and functions behave differently. Inside a function, a name is > > local if it's ever assigned to; but in a class, this is not the case. > > Actually, it is. Assigning to a name in a class body makes it part > of the class namespace, which is the local namespace at the time > the class body is executed. The significant part here is "ever". Consider: >>> x = 1 >>> def f(): ... print(x) ... x = 2 ... print(x) ... >>> f() Traceback (most recent call last): File "", line 1, in File "", line 2, in f UnboundLocalError: local variable 'x' referenced before assignment >>> class X: ... print(x) ... x = 2 ... print(x) ... 1 2 >>> In the function, since x *is* assigned to at some point, it is deemed a local name, which means that referencing it bombs. In the class, that isn't the case; until it is actually assigned to, it isn't part of the class's namespace, so the global is visible. So, yes, it's a difference between function namespaces and other namespaces (modules work the same way as classes). ChrisA From jfong at ms4.hinet.net Tue Feb 26 03:45:52 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Tue, 26 Feb 2019 00:45:52 -0800 (PST) Subject: Quirk difference between classes and functions In-Reply-To: <5c7416f4$0$21614$426a74cc@news.free.fr> References: <5c7416f4$0$21614$426a74cc@news.free.fr> Message-ID: <7cadc4ec-b788-40c7-9ee3-b8f2202c4085@googlegroups.com> ast? 2019?2?26???? UTC+8??12?25?40???? > Hello > > I noticed a quirk difference between classes and functions > > >>> x=0 > >>> > >>> class Test: > x = x+1 > print(x) > x = x+1 > print(x) > > 1 > 2 > >>> print(x) > 0 > > Previous code doesn't generate any errors. > x at the right of = in first "x = x+1" line is > the global one (x=0), then x becomes local > > within a function, this is not allowed > > >>> x = 0 > >>> > >>> def f(): > x = x+1 > > >>> f() > UnboundLocalError: local variable 'x' referenced before assignment > > Since x is written inside the function, it is considered as a local > variable and x in x+1 is undefined so this throw an exception > > Any comment ? May I say that the LEGB rule apply to run time, not compile time? >>> x = 1 >>> def f(): ... print(x) ... print(locals()) ... >>> f() 1 {} >>> def g(): ... print(x) ... x = 2 ... print(locals()) ... >>> g() Traceback (most recent call last): File "", line 1, in File "", line 2, in g UnboundLocalError: local variable 'x' referenced before assignment >>> class X: ... print(x) ... x = 2 ... 1 >>> The print(x) difference between class and function was caused by that one was executed and the other was compiled. The LEGB rule must apply to run time to make the language dynamic. Any comment:-)? --Jach From tjol at tjol.eu Tue Feb 26 04:23:44 2019 From: tjol at tjol.eu (Thomas Jollans) Date: Tue, 26 Feb 2019 10:23:44 +0100 Subject: Quirk difference between classes and functions In-Reply-To: References: <5c7416f4$0$21614$426a74cc@news.free.fr> <642864ac-9ff4-f749-f18a-cacf2048f5e7@DancesWithMice.info> Message-ID: On 25/02/2019 21.15, Chris Angelico wrote: > On Tue, Feb 26, 2019 at 6:58 AM DL Neil wrote: >> >> On 26/02/19 5:25 AM, ast wrote: >>> I noticed a quirk difference between classes and functions >>> >>> x=0 >>> >>> class Test: >>> x = x+1 >>> print(x) >>> x = x+1 >>> print(x) >> ... >> >>> Previous code doesn't generate any errors. >>> x at the right of = in first "x = x+1" line is >>> the global one (x=0), then x becomes local >>> within a function, this is not allowed >>> >>> x = 0 >>> >>> def f(): >>> x = x+1 >>> >>> f() >>> UnboundLocalError: local variable 'x' referenced before assignment >>> Since x is written inside the function, it is considered as a local >>> variable and x in x+1 is undefined so this throw an exception >>> Any comment ? >> >> >> At first I misunderstood the question, and even now I'm slightly mystified:- >> >> Is the observation to do with the principles of "closure" (being applied >> to the function) compared with the differences between class and >> instance variables? > > Classes and functions behave differently. Inside a function, a name is > local if it's ever assigned to; but in a class, this is not the case. > (Honestly, I'm not sure why this is, but it's hardly ever significant; > metaprogramming seldom runs into this kind of thing.) I imagine there's a justification for the difference in behaviour to do with the fact that the body of a class is only ever executed once, while the body of a function is executed multiple times. But I don't quite see it. > >> A question from me: (I'm not an O-O 'native' having taken to it long >> after first learning 'programming') I've used class attributes to hold >> 'constants', eg >> >> MAXIMUM_WEIGHT = 100 >> >> Thus only an assignment to which reference/assertions will be made 'later'. >> >> I have initialised a counter to zero and then incremented within the >> instantiated object's __init__(), ie keeping track of how many of 'these >> objects' have been instantiated. > > Once you're inside a method, you would reference it with a dot - > either "self.MAXIMUM_WEIGHT" or "cls.MAXIMUM_WEIGHT". So it's now an > attribute, not a local name. > >> So, I can imagine taking a value from outside the class' namespace, >> modifying it in some way (per the first "x = x+1") and then retaining >> the result as a class attribute (a calculation performed once - when the >> class code is compiled). > > Yes, this is perfectly legitimate. Not common but legit. > >> However, (after that first calculation/definition of the class >> attribute) why would one (normally) want to redefine the same class >> attribute (the second x = x+1), at the 'class level'? >> (perhaps just done for its amusement value?) > > That would not be common either. If you're redefining a variable > multiple times within a class block, you probably don't also have it > at top level. (For instance, you can put a "for" loop inside a class > statement to create attributes/methods, but the loop variable is > unlikely to also be a global.) > > But hey! It's fun! :) > > ChrisA > From vergos.nikolas at gmail.com Tue Feb 26 08:26:16 2019 From: vergos.nikolas at gmail.com (vergos.nikolas at gmail.com) Date: Tue, 26 Feb 2019 05:26:16 -0800 (PST) Subject: How to format a datetime MySQL database field to local using strftime() In-Reply-To: References: <09bb3576-47b2-5723-832a-81c9ca41146d@DancesWithMice.info> <5caf3a32-0f90-401a-be39-e26be1917f0e@googlegroups.com> <716cb07f-ff3f-caa3-ed59-82687abb13fa@DancesWithMice.info> <7td87e5kld47lkeir4usvj4q1jhi44d0h7@4ax.com> <02b39386-102b-46c0-6ccc-7ef512fa8fc6@DancesWithMice.info> Message-ID: Can you help me rewrite this function, which when iam using 'pymysql' conncector works normally, it does not when iam using 'bottle_pymysql' def coalesce( data ): newdata = [] seen = {} for host, ref, location, useros, browser, visits, hits, downloads, authuser in data: # Here i have to decide how to group the rows together # I want an html row for every unique combination of (host) and that hits should be summed together key = host if key not in seen: newdata.append( [ [host], [ref], location, useros, browser, [visits], hits, [downloads], authuser ] ) seen[key] = len( newdata ) - 1 # Save index (for 'newdata') of this row else: # This row is a duplicate row with a different referrer & visit datetime & torrent download rowindex = seen[key] newdata[rowindex][0].append( host ) newdata[rowindex][1].append( ref ) newdata[rowindex][5].append( visits ) newdata[rowindex][6] += hits newdata[rowindex][7].append( downloads ) return newdata From vergos.nikolas at gmail.com Tue Feb 26 08:30:45 2019 From: vergos.nikolas at gmail.com (vergos.nikolas at gmail.com) Date: Tue, 26 Feb 2019 05:30:45 -0800 (PST) Subject: How to format a datetime MySQL database field to local using strftime() In-Reply-To: References: <09bb3576-47b2-5723-832a-81c9ca41146d@DancesWithMice.info> <5caf3a32-0f90-401a-be39-e26be1917f0e@googlegroups.com> <716cb07f-ff3f-caa3-ed59-82687abb13fa@DancesWithMice.info> <7td87e5kld47lkeir4usvj4q1jhi44d0h7@4ax.com> <02b39386-102b-46c0-6ccc-7ef512fa8fc6@DancesWithMice.info> Message-ID: <8ba4207c-52c2-4cb3-8ba4-dd1fc47e4c97@googlegroups.com> ?? ?????, 26 ??????????? 2019 - 3:26:29 ?.?. UTC+2, ? ??????? vergos.... at gmail.com ??????: > Can you help me rewrite this function, which when iam using 'pymysql' conncector works normally, it does not when iam using 'bottle_pymysql' > > def coalesce( data ): > newdata = [] > seen = {} > for host, ref, location, useros, browser, visits, hits, downloads, authuser in data: > # Here i have to decide how to group the rows together > # I want an html row for every unique combination of (host) and that hits should be summed together > key = host > if key not in seen: > newdata.append( [ [host], [ref], location, useros, browser, [visits], hits, [downloads], authuser ] ) > seen[key] = len( newdata ) - 1 # Save index (for 'newdata') of this row > else: # This row is a duplicate row with a different referrer & visit datetime & torrent download > rowindex = seen[key] > newdata[rowindex][0].append( host ) > newdata[rowindex][1].append( ref ) > newdata[rowindex][5].append( visits ) > newdata[rowindex][6] += hits > newdata[rowindex][7].append( downloads ) > return newdata For some reason in 'bottle-pymysql' pagehit = cur.fetchone()[0] does not work while reffering by name of the field pagehit = cur.fetchone()['hits'] does work. Please help me write the baove function in a similar way. From vergos.nikolas at gmail.com Tue Feb 26 10:10:46 2019 From: vergos.nikolas at gmail.com (vergos.nikolas at gmail.com) Date: Tue, 26 Feb 2019 07:10:46 -0800 (PST) Subject: missing 1 required positional argument error Message-ID: <45a6ada8-21dd-410c-ba40-069f4eded409@googlegroups.com> I'm receiving the following error: Traceback (most recent call last): File "/usr/lib64/python3.6/site-packages/bottle.py", line 862, in _handle return route.call(**args) File "/usr/lib64/python3.6/site-packages/bottle.py", line 1740, in wrapper rv = callback(*a, **ka) File "/usr/lib64/python3.6/site-packages/bottle.py", line 2690, in wrapper return func(*a, **ka) TypeError: listall() missing 1 required positional argument: 'pymydb' and this is my route definition: @app.route( '/' ) @auth_basic(counters.is_authenticated_user) def listall( pymydb ): The way i understand this error is that i'am trying to call 'listall()' without giving it an argument while in its definition i do have 'pymydb' as a parameter. BUT from inside my script i do NOT call listall at all, so how can it miss an argument? From cspealma at redhat.com Tue Feb 26 10:20:45 2019 From: cspealma at redhat.com (Calvin Spealman) Date: Tue, 26 Feb 2019 10:20:45 -0500 Subject: missing 1 required positional argument error In-Reply-To: <45a6ada8-21dd-410c-ba40-069f4eded409@googlegroups.com> References: <45a6ada8-21dd-410c-ba40-069f4eded409@googlegroups.com> Message-ID: you call it by visiting the route you've mapped to it, so when you run this app and navigate to the root / URL it gets called. What do you expect this parameter to be and where did you expect it to come from when you wrote the listall() function? On Tue, Feb 26, 2019 at 10:15 AM wrote: > I'm receiving the following error: > > Traceback (most recent call last): > File "/usr/lib64/python3.6/site-packages/bottle.py", line 862, in _handle > return route.call(**args) > File "/usr/lib64/python3.6/site-packages/bottle.py", line 1740, in > wrapper > rv = callback(*a, **ka) > File "/usr/lib64/python3.6/site-packages/bottle.py", line 2690, in > wrapper > return func(*a, **ka) > TypeError: listall() missing 1 required positional argument: 'pymydb' > > and this is my route definition: > > @app.route( '/' ) > @auth_basic(counters.is_authenticated_user) > def listall( pymydb ): > > The way i understand this error is that i'am trying to call 'listall()' > without giving it an argument while in its definition i do have 'pymydb' as > a parameter. > > BUT from inside my script i do NOT call listall at all, so how can it miss > an argument? > -- > https://mail.python.org/mailman/listinfo/python-list > -- CALVIN SPEALMAN SENIOR QUALITY ENGINEER cspealma at redhat.com M: +1.336.210.5107 TRIED. TESTED. TRUSTED. From zleap at disroot.org Tue Feb 26 10:51:38 2019 From: zleap at disroot.org (Paul Sutton) Date: Tue, 26 Feb 2019 15:51:38 +0000 Subject: Trying to read in data for a file to a python application Message-ID: Hi I have been trying to write a small application that is essentially user information application. https://raw.githubusercontent.com/zleap/AboutMe/master/Aboutme.py So far I have managed to write the data generated to a file, what I want to do now, is read this data back in when the user opens the program. Trying to use https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list with open(fname) as f: content = f.readlines() # you may also want to remove whitespace characters like `\n` at the end of each line content = [x.strip() for x in content] Trying to use this as a guide (hence my code is a little different). But am really stuck So far the GUI window appears but no widgets. So something is clearly getting stalled. To begin with If I can read the text in, and just display in the console this is a start, I can then see if I can figure out how to take that and insert the data in to the right places. Can anyone help please. Thanks Paul From vergos.nikolas at gmail.com Tue Feb 26 10:54:41 2019 From: vergos.nikolas at gmail.com (vergos.nikolas at gmail.com) Date: Tue, 26 Feb 2019 07:54:41 -0800 (PST) Subject: missing 1 required positional argument error In-Reply-To: References: <45a6ada8-21dd-410c-ba40-069f4eded409@googlegroups.com> Message-ID: <4a8917d7-cd05-4ea6-a25f-a634202ac4ef@googlegroups.com> ?? ?????, 26 ??????????? 2019 - 5:21:14 ?.?. UTC+2, ? ??????? Calvin Spealman ??????: > you call it by visiting the route you've mapped to it, so when you run this > app and navigate to the root / URL it gets called. What do you expect this > parameter to be and where did you expect it to come from when you wrote the > listall() function? > > On Tue, Feb 26, 2019 at 10:15 AM wrote: > > > I'm receiving the following error: > > > > Traceback (most recent call last): > > File "/usr/lib64/python3.6/site-packages/bottle.py", line 862, in _handle > > return route.call(**args) > > File "/usr/lib64/python3.6/site-packages/bottle.py", line 1740, in > > wrapper > > rv = callback(*a, **ka) > > File "/usr/lib64/python3.6/site-packages/bottle.py", line 2690, in > > wrapper > > return func(*a, **ka) > > TypeError: listall() missing 1 required positional argument: 'pymydb' > > > > and this is my route definition: > > > > @app.route( '/' ) > > @auth_basic(counters.is_authenticated_user) > > def listall( pymydb ): > > > > The way i understand this error is that i'am trying to call 'listall()' > > without giving it an argument while in its definition i do have 'pymydb' as > > a parameter. > > > > BUT from inside my script i do NOT call listall at all, so how can it miss > > an argument? Yes, '/' route is being called when i visir it with my browser. Ok, but that function needs to work with the 'pymydb' 'bottle-pymysql' keyword so it must have it as a parameter. In another script of mine i have mapped '/' to def index( pymydb ) and its working without error. How come it works there and not work here? They are both '/' routes. From vergos.nikolas at gmail.com Tue Feb 26 10:57:10 2019 From: vergos.nikolas at gmail.com (vergos.nikolas at gmail.com) Date: Tue, 26 Feb 2019 07:57:10 -0800 (PST) Subject: missing 1 required positional argument error In-Reply-To: References: <45a6ada8-21dd-410c-ba40-069f4eded409@googlegroups.com> Message-ID: ?? ?????, 26 ??????????? 2019 - 5:21:14 ?.?. UTC+2, ? ??????? Calvin Spealman ??????: > you call it by visiting the route you've mapped to it, so when you run this > app and navigate to the root / URL it gets called. What do you expect this > parameter to be and where did you expect it to come from when you wrote the > listall() function? > > On Tue, Feb 26, 2019 at 10:15 AM wrote: > > > I'm receiving the following error: > > > > Traceback (most recent call last): > > File "/usr/lib64/python3.6/site-packages/bottle.py", line 862, in _handle > > return route.call(**args) > > File "/usr/lib64/python3.6/site-packages/bottle.py", line 1740, in > > wrapper > > rv = callback(*a, **ka) > > File "/usr/lib64/python3.6/site-packages/bottle.py", line 2690, in > > wrapper > > return func(*a, **ka) > > TypeError: listall() missing 1 required positional argument: 'pymydb' > > > > and this is my route definition: > > > > @app.route( '/' ) > > @auth_basic(counters.is_authenticated_user) > > def listall( pymydb ): > > > > The way i understand this error is that i'am trying to call 'listall()' > > without giving it an argument while in its definition i do have 'pymydb' as > > a parameter. > > > > BUT from inside my script i do NOT call listall at all, so how can it miss > > an argument? I expect the parameter to be pass from the top of my script where i have this line: # dbhost is optional, default is localhost plugin = bottle_pymysql.Plugin( dbuser='nikos', dbpass='*****', dbname='clientele', dictrows=False ) app.install(plugin) It works as with another script of mine does. From vergos.nikolas at gmail.com Tue Feb 26 11:01:58 2019 From: vergos.nikolas at gmail.com (vergos.nikolas at gmail.com) Date: Tue, 26 Feb 2019 08:01:58 -0800 (PST) Subject: How to format a datetime MySQL database field to local using strftime() In-Reply-To: <8ba4207c-52c2-4cb3-8ba4-dd1fc47e4c97@googlegroups.com> References: <09bb3576-47b2-5723-832a-81c9ca41146d@DancesWithMice.info> <5caf3a32-0f90-401a-be39-e26be1917f0e@googlegroups.com> <716cb07f-ff3f-caa3-ed59-82687abb13fa@DancesWithMice.info> <7td87e5kld47lkeir4usvj4q1jhi44d0h7@4ax.com> <02b39386-102b-46c0-6ccc-7ef512fa8fc6@DancesWithMice.info> <8ba4207c-52c2-4cb3-8ba4-dd1fc47e4c97@googlegroups.com> Message-ID: Actually i just found it has a directive: dictrows: Whether or not to support dict-like access to row objects (default: True). so i just did: plugin = bottle_pymysql.Plugin( dbuser='nikos', dbpass='*****', dbname='counters', dictrows=False ) and now it works with indexes as integers not as strings. From pkpearson at nowhere.invalid Tue Feb 26 11:25:54 2019 From: pkpearson at nowhere.invalid (Peter Pearson) Date: 26 Feb 2019 16:25:54 GMT Subject: Trying to read in data for a file to a python application References: Message-ID: On Tue, 26 Feb 2019 15:51:38 +0000, Paul Sutton wrote: > Hi > > I have been trying to write a small application that is essentially user > information application. > > https://raw.githubusercontent.com/zleap/AboutMe/master/Aboutme.py > > So far I have managed to write the data generated to a file, what I want > to do now, is read this data back in when the user opens the program. > [snip] > > So far the GUI window appears but no widgets. So something is clearly > getting stalled. > > To begin with If I can read the text in, and just display in the console > this is a start, I can then see if I can figure out how to take that and > insert the data in to the right places. I'd suggest writing a terminal-based, non-GUI program to read your data file and do something simple with it like displaying it on the terminal. The strategy is to tackle your problem (reading back the data) in the simplest context possible. -- To email me, substitute nowhere->runbox, invalid->com. From zleap at disroot.org Tue Feb 26 13:12:13 2019 From: zleap at disroot.org (Paul Sutton) Date: Tue, 26 Feb 2019 18:12:13 +0000 Subject: Trying to read in data for a file to a python application In-Reply-To: References: Message-ID: On 26/02/2019 15:51, Paul Sutton wrote: > Hi > > I have been trying to write a small application that is essentially user > information application. > > https://raw.githubusercontent.com/zleap/AboutMe/master/Aboutme.py > > So far I have managed to write the data generated to a file, what I want > to do now, is read this data back in when the user opens the program. > > Trying to use > > https://stackoverflow.com/questions/3277503/how-to-read-a-file-line-by-line-into-a-list > > > with open(fname) as f: > content = f.readlines() > # you may also want to remove whitespace characters like `\n` at the end > of each line > content = [x.strip() for x in content] > > Trying to use this as a guide (hence my code is a little different). > But am really stuck > > So far the GUI window appears but no widgets. So something is clearly > getting stalled. > > To begin with If I can read the text in, and just display in the console > this is a start, I can then see if I can figure out how to take that and > insert the data in to the right places. > > Can anyone help please. > > Thanks > > Paul > Hi All Thank you for the comments and adding some 'sanity' to my problem solving. I will do as suggested and create a Python program to read / write then move on to integrating that in to the main program. Thanks again Paul From greg.ewing at canterbury.ac.nz Tue Feb 26 16:26:35 2019 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Wed, 27 Feb 2019 10:26:35 +1300 Subject: Quirk difference between classes and functions In-Reply-To: References: <5c7416f4$0$21614$426a74cc@news.free.fr> <642864ac-9ff4-f749-f18a-cacf2048f5e7@DancesWithMice.info> Message-ID: Thomas Jollans wrote: > I imagine there's a justification for the difference in behaviour to do > with the fact that the body of a class is only ever executed once, while > the body of a function is executed multiple times. I suspect there isn't any deep reason for it, rather it's just something that fell out of the implementation, in particular the decision to optimise local variable access in functions but not other scopes. When compiling a function, the compiler needs to know which variables are local so that it can allocate slots for them in the stack frame. But when executing a class body, the locals are kept in a dict and are looked up dynamically. The compiler *could* be made to treat class bodies the same way as functions in this regard, but it would be extra work for little or no benefit. Most code in class bodies just defines new names without referring to anything else in the same scope. -- Greg From Gronicus at SGA.Ninja Tue Feb 26 16:48:33 2019 From: Gronicus at SGA.Ninja (Steve) Date: Tue, 26 Feb 2019 16:48:33 -0500 Subject: Quirk difference between classes and functions In-Reply-To: References: <5c7416f4$0$21614$426a74cc@news.free.fr> <642864ac-9ff4-f749-f18a-cacf2048f5e7@DancesWithMice.info> Message-ID: <005201d4ce1d$057f9dd0$107ed970$@SGA.Ninja> I have been a silent reader on this and am interested in understanding more about the scope of variables in Python. They do not seem to behave as I have experienced in other programming languages. I have used functions in python but was not aware of class. It would benefit me very well if someone could post a simple example of each on which I might experiment. Steve ---------------------------------------------------------------------------- ---------------- Footnote: There's 99 bugs in the code, in the code. 99 bugs in the code. Take one down and patch it all around. Now there's 117 bugs in the code. -----Original Message----- From: Python-list On Behalf Of Gregory Ewing Sent: Tuesday, February 26, 2019 4:27 PM To: python-list at python.org Subject: Re: Quirk difference between classes and functions Thomas Jollans wrote: > I imagine there's a justification for the difference in behaviour to > do with the fact that the body of a class is only ever executed once, > while the body of a function is executed multiple times. I suspect there isn't any deep reason for it, rather it's just something that fell out of the implementation, in particular the decision to optimise local variable access in functions but not other scopes. When compiling a function, the compiler needs to know which variables are local so that it can allocate slots for them in the stack frame. But when executing a class body, the locals are kept in a dict and are looked up dynamically. The compiler *could* be made to treat class bodies the same way as functions in this regard, but it would be extra work for little or no benefit. Most code in class bodies just defines new names without referring to anything else in the same scope. -- Greg -- https://mail.python.org/mailman/listinfo/python-list From marko at pacujo.net Tue Feb 26 16:54:00 2019 From: marko at pacujo.net (Marko Rauhamaa) Date: Tue, 26 Feb 2019 23:54:00 +0200 Subject: Lifetime of a local reference Message-ID: <8736oai5c7.fsf@elektro.pacujo.net> Consider this function: def fun(): f = open("lock") flock.flock(f, fcntl.LOCK_EX) do_stuff() sys.exit(0) Question: can a compliant Python implementation close f (and, consequently, release the file lock) before/while do_stuff() is executed? I couldn't find an immediate answer in the documentation. Marko From rosuav at gmail.com Tue Feb 26 17:04:38 2019 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 27 Feb 2019 09:04:38 +1100 Subject: Lifetime of a local reference In-Reply-To: <8736oai5c7.fsf@elektro.pacujo.net> References: <8736oai5c7.fsf@elektro.pacujo.net> Message-ID: On Wed, Feb 27, 2019 at 9:00 AM Marko Rauhamaa wrote: > Consider this function: > > def fun(): > f = open("lock") > flock.flock(f, fcntl.LOCK_EX) > do_stuff() > sys.exit(0) > > Question: can a compliant Python implementation close f (and, > consequently, release the file lock) before/while do_stuff() is > executed? > > I couldn't find an immediate answer in the documentation. The variable is alive, so the object is alive. Common sense should make this clear, but there's probably some details in the documentation if you actually need a citation. The name "f" is part of the function's locals until that function returns or the name is explicitly unbound. ChrisA From info at tundraware.com Tue Feb 26 17:08:05 2019 From: info at tundraware.com (Tim Daneliuk) Date: Tue, 26 Feb 2019 16:08:05 -0600 Subject: Lifetime of a local reference In-Reply-To: <8736oai5c7.fsf@elektro.pacujo.net> References: <8736oai5c7.fsf@elektro.pacujo.net> Message-ID: <5vpgkf-2j03.ln1@oceanview.tundraware.com> On 2/26/19 3:54 PM, Marko Rauhamaa wrote: > Consider this function: > > def fun(): > f = open("lock") > flock.flock(f, fcntl.LOCK_EX) > do_stuff() > sys.exit(0) > > Question: can a compliant Python implementation close f (and, > consequently, release the file lock) before/while do_stuff() is > executed? > > I couldn't find an immediate answer in the documentation. Not quite sure what you are asking. Are you asking if the file handle can be closed before (or concurrently if do_stuff() is a thread) and do_stuff() can continue to make use of the handle? From tjreedy at udel.edu Tue Feb 26 20:16:06 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 26 Feb 2019 20:16:06 -0500 Subject: missing 1 required positional argument error In-Reply-To: <45a6ada8-21dd-410c-ba40-069f4eded409@googlegroups.com> References: <45a6ada8-21dd-410c-ba40-069f4eded409@googlegroups.com> Message-ID: On 2/26/2019 10:10 AM, vergos.nikolas at gmail.com wrote: > I'm receiving the following error: > > Traceback (most recent call last): > File "/usr/lib64/python3.6/site-packages/bottle.py", line 862, in _handle > return route.call(**args) > File "/usr/lib64/python3.6/site-packages/bottle.py", line 1740, in wrapper > rv = callback(*a, **ka) > File "/usr/lib64/python3.6/site-packages/bottle.py", line 2690, in wrapper > return func(*a, **ka) > TypeError: listall() missing 1 required positional argument: 'pymydb' > > and this is my route definition: > > @app.route( '/' ) > @auth_basic(counters.is_authenticated_user) > def listall( pymydb ): > > The way i understand this error is that i'am trying to call 'listall()' without giving it an argument while in its definition i do have 'pymydb' as a parameter. > > BUT from inside my script i do NOT call listall at all, so how can it miss an argument? Follow the traceback. The error occurred when executing "func(*a, *ka)" in bottle.py. Clearly, at that time, func = listall, a = [], and ka = {}. To see why this is so, work back up the stack. -- Terry Jan Reedy From jfong at ms4.hinet.net Tue Feb 26 20:16:56 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Tue, 26 Feb 2019 17:16:56 -0800 (PST) Subject: Quirk difference between classes and functions In-Reply-To: References: <5c7416f4$0$21614$426a74cc@news.free.fr> <642864ac-9ff4-f749-f18a-cacf2048f5e7@DancesWithMice.info> Message-ID: Gregory Ewing at 2019/2/27 AM 5:26:49 wrote: > Thomas Jollans wrote: > > I imagine there's a justification for the difference in behaviour to do > > with the fact that the body of a class is only ever executed once, while > > the body of a function is executed multiple times. > > I suspect there isn't any deep reason for it, rather it's just > something that fell out of the implementation, in particular > the decision to optimise local variable access in functions > but not other scopes. > > When compiling a function, the compiler needs to know which > variables are local so that it can allocate slots for them in > the stack frame. But when executing a class body, the locals > are kept in a dict and are looked up dynamically. If the compiler can do any decision on the variable's name, when it goes to line of print, how it handle it? x = 0 def f(): print(x) def g(): print(x) x = 1 print(y) --Jach > The compiler *could* be made to treat class bodies the same > way as functions in this regard, but it would be extra work > for little or no benefit. Most code in class bodies just > defines new names without referring to anything else in the > same scope. > > -- > Greg From eryksun at gmail.com Tue Feb 26 20:22:33 2019 From: eryksun at gmail.com (eryk sun) Date: Tue, 26 Feb 2019 19:22:33 -0600 Subject: Quirk difference between classes and functions In-Reply-To: References: <5c7416f4$0$21614$426a74cc@news.free.fr> <642864ac-9ff4-f749-f18a-cacf2048f5e7@DancesWithMice.info> Message-ID: On 2/26/19, Gregory Ewing wrote: > Thomas Jollans wrote: >> I imagine there's a justification for the difference in behaviour to do >> with the fact that the body of a class is only ever executed once, while >> the body of a function is executed multiple times. > > I suspect there isn't any deep reason for it, rather it's just > something that fell out of the implementation, in particular > the decision to optimise local variable access in functions > but not other scopes. At the module level, this goes unnoticed since globals and locals are the same dict, but we can observe it in an exec() call if we use separate globals and locals . For example: >>> x = 0 >>> locals = {} >>> exec('x = x + 1', globals(), locals) >>> x 0 >>> locals {'x': 1} In terms of implementation, the LOAD_NAME instruction that's used in unoptimized code looks in the locals, globals, and builtins scopes, in that order. The intent is to allow locals to shadow globals and builtins, and globals to shadow builtins. It also allows for temporary shadowing. Optimized code supports the former (via LOAD_GLOBAL), but not the latter. For example: unoptimized: >>> exec(r''' ... len = lambda x: 42 ... print(len('spam')) ... del len ... print(len('spam')) ... ''') 42 4 optimized: >>> def f(): ... len = lambda x: 42 ... print(len('spam')) ... del len ... print(len('spam')) ... >>> f() 42 Traceback (most recent call last): File "", line 1, in File "", line 5, in f UnboundLocalError: local variable 'len' referenced before assignment From rosuav at gmail.com Tue Feb 26 20:24:46 2019 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 27 Feb 2019 12:24:46 +1100 Subject: Quirk difference between classes and functions In-Reply-To: References: <5c7416f4$0$21614$426a74cc@news.free.fr> <642864ac-9ff4-f749-f18a-cacf2048f5e7@DancesWithMice.info> Message-ID: On Wed, Feb 27, 2019 at 12:21 PM wrote: > > Gregory Ewing at 2019/2/27 AM 5:26:49 wrote: > > Thomas Jollans wrote: > > > I imagine there's a justification for the difference in behaviour to do > > > with the fact that the body of a class is only ever executed once, while > > > the body of a function is executed multiple times. > > > > I suspect there isn't any deep reason for it, rather it's just > > something that fell out of the implementation, in particular > > the decision to optimise local variable access in functions > > but not other scopes. > > > > When compiling a function, the compiler needs to know which > > variables are local so that it can allocate slots for them in > > the stack frame. But when executing a class body, the locals > > are kept in a dict and are looked up dynamically. > > If the compiler can do any decision on the variable's name, when it goes to line of print, how it handle it? > > x = 0 > def f(): > print(x) > > def g(): > print(x) > x = 1 > print(y) > Not sure what you mean by "decision", but the definition is that since x is assigned to in g(), it is local to g(). It's not local to f(), so the global is visible. (Incidentally, "print" is handled exactly the same way. Since neither function assigns to print, it's not local, so the built-in is found.) ChrisA From alan at csail.mit.edu Tue Feb 26 22:14:30 2019 From: alan at csail.mit.edu (Alan Bawden) Date: 26 Feb 2019 22:14:30 -0500 Subject: Lifetime of a local reference References: <8736oai5c7.fsf@elektro.pacujo.net> Message-ID: <86pnrd2a95.fsf@richard.bawden.org> Marko Rauhamaa writes: > def fun(): > f = open("lock") > flock.flock(f, fcntl.LOCK_EX) > do_stuff() > sys.exit(0) > > Question: can a compliant Python implementation close f (and, > consequently, release the file lock) before/while do_stuff() is > executed? A correct-but-fails-to-answer-your-real-question answer would be: "If you _care_ about when f gets closed, then just call f.close() yourself." So if you want to be _sure_ the lock stays locked while you "do stuff", you should write: def fun(): f = open("lock") flock.flock(f, fcntl.LOCK_EX) do_stuff() f.close() sys.exit(0) And now you don't have to worry about the details of variable lifetimes. But I appreciate that that isn't the true question that you wanted to ask! You are wondering if a Python implementation is _permitted_ to treat the code you wrote _as if_ you had written: def fun(): f = open("lock") flock.flock(f, fcntl.LOCK_EX) del f do_stuff() sys.exit(0) which deletes the variable f from the local environment at a point where it will never be used again. (Which could cause the lock to be released before do_stuff is even called.) This is an interesting question, and one that any garbage collected language should probably address somehow. For example, the Java Language Specification contains the following language: Optimizing transformations of a program can be designed that reduce the number of objects that are reachable to be less than those which would naively be considered reachable. For example, a Java compiler or code generator may choose to set a variable or parameter that will no longer be used to null to cause the storage for such an object to be potentially reclaimable sooner. (This is from section 12.6.1 of the Java 8 version, which is what I had handy.) So you're not crazy to ask such a question. > I couldn't find an immediate answer in the documentation. I suspect that given the history of Python, pretty much everybody has always assumed that a Python implementation will not delete local variables early. But I agree with you that the Python Language Reference does not appear to address this question anywhere! -- Alan Bawden From jfong at ms4.hinet.net Tue Feb 26 22:15:16 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Tue, 26 Feb 2019 19:15:16 -0800 (PST) Subject: Quirk difference between classes and functions In-Reply-To: References: <5c7416f4$0$21614$426a74cc@news.free.fr> <642864ac-9ff4-f749-f18a-cacf2048f5e7@DancesWithMice.info> Message-ID: <3a7dd391-a1e7-4bfb-877b-5b2e3a8de946@googlegroups.com> Chris Angelico? 2019?2?27???? UTC+8??9?25?11???? > On Wed, Feb 27, 2019 at 12:21 PM wrote: > > > > Gregory Ewing at 2019/2/27 AM 5:26:49 wrote: > > > Thomas Jollans wrote: > > > > I imagine there's a justification for the difference in behaviour to do > > > > with the fact that the body of a class is only ever executed once, while > > > > the body of a function is executed multiple times. > > > > > > I suspect there isn't any deep reason for it, rather it's just > > > something that fell out of the implementation, in particular > > > the decision to optimise local variable access in functions > > > but not other scopes. > > > > > > When compiling a function, the compiler needs to know which > > > variables are local so that it can allocate slots for them in > > > the stack frame. But when executing a class body, the locals > > > are kept in a dict and are looked up dynamically. > > > > If the compiler can do any decision on the variable's name, when it goes to line of print, how it handle it? > > > > x = 0 > > def f(): > > print(x) > > > > def g(): > > print(x) > > x = 1 > > print(y) > > > > Not sure what you mean by "decision", but the definition is that since > x is assigned to in g(), it is local to g(). It's not local to f(), so > the global is visible. So, may I say that the Python compiler is a multi-pass one? --Jach > > (Incidentally, "print" is handled exactly the same way. Since neither > function assigns to print, it's not local, so the built-in is found.) > > ChrisA From rosuav at gmail.com Tue Feb 26 22:28:37 2019 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 27 Feb 2019 14:28:37 +1100 Subject: Quirk difference between classes and functions In-Reply-To: <3a7dd391-a1e7-4bfb-877b-5b2e3a8de946@googlegroups.com> References: <5c7416f4$0$21614$426a74cc@news.free.fr> <642864ac-9ff4-f749-f18a-cacf2048f5e7@DancesWithMice.info> <3a7dd391-a1e7-4bfb-877b-5b2e3a8de946@googlegroups.com> Message-ID: On Wed, Feb 27, 2019 at 2:21 PM wrote: > > Chris Angelico? 2019?2?27???? UTC+8??9?25?11???? > > On Wed, Feb 27, 2019 at 12:21 PM wrote: > > > > > > Gregory Ewing at 2019/2/27 AM 5:26:49 wrote: > > > > Thomas Jollans wrote: > > > > > I imagine there's a justification for the difference in behaviour to do > > > > > with the fact that the body of a class is only ever executed once, while > > > > > the body of a function is executed multiple times. > > > > > > > > I suspect there isn't any deep reason for it, rather it's just > > > > something that fell out of the implementation, in particular > > > > the decision to optimise local variable access in functions > > > > but not other scopes. > > > > > > > > When compiling a function, the compiler needs to know which > > > > variables are local so that it can allocate slots for them in > > > > the stack frame. But when executing a class body, the locals > > > > are kept in a dict and are looked up dynamically. > > > > > > If the compiler can do any decision on the variable's name, when it goes to line of print, how it handle it? > > > > > > x = 0 > > > def f(): > > > print(x) > > > > > > def g(): > > > print(x) > > > x = 1 > > > print(y) > > > > > > > Not sure what you mean by "decision", but the definition is that since > > x is assigned to in g(), it is local to g(). It's not local to f(), so > > the global is visible. > > So, may I say that the Python compiler is a multi-pass one? > At this point, you're veering away from "Python-the-language" and towards "CPython-the-implementation" (or whichever other implementation you want to explore). But I would say that, yes, most or all Python implementations will use multi-pass compilation. It's the easiest way to guarantee correct behaviour. For instance, when you reach a "try" statement, you don't know whether there'll be an "except" block after it (there might just be a "finally"); the bytecode that CPython produces is different for setting up a finally-only block than for setting up a try/except. Easiest to find out that sort of thing by running multiple passes. Plus, depending on how you define "pass", there are additional steps such as constant folding and peephole optimization that can be done after everything else. So, yeah, it's highly unlikely that there are any performant implementations of Python that run a single compilation pass. ChrisA From jfong at ms4.hinet.net Tue Feb 26 22:58:14 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Tue, 26 Feb 2019 19:58:14 -0800 (PST) Subject: Quirk difference between classes and functions In-Reply-To: References: <5c7416f4$0$21614$426a74cc@news.free.fr> <642864ac-9ff4-f749-f18a-cacf2048f5e7@DancesWithMice.info> <3a7dd391-a1e7-4bfb-877b-5b2e3a8de946@googlegroups.com> Message-ID: <2104fc1a-b39d-412a-927d-5e0d82bf31d0@googlegroups.com> Chris Angelico? 2019?2?27???? UTC+8??11?29?04???? > On Wed, Feb 27, 2019 at 2:21 PM wrote: > > > > Chris Angelico? 2019?2?27???? UTC+8??9?25?11???? > > > On Wed, Feb 27, 2019 at 12:21 PM wrote: > > > > > > > > Gregory Ewing at 2019/2/27 AM 5:26:49 wrote: > > > > > Thomas Jollans wrote: > > > > > > I imagine there's a justification for the difference in behaviour to do > > > > > > with the fact that the body of a class is only ever executed once, while > > > > > > the body of a function is executed multiple times. > > > > > > > > > > I suspect there isn't any deep reason for it, rather it's just > > > > > something that fell out of the implementation, in particular > > > > > the decision to optimise local variable access in functions > > > > > but not other scopes. > > > > > > > > > > When compiling a function, the compiler needs to know which > > > > > variables are local so that it can allocate slots for them in > > > > > the stack frame. But when executing a class body, the locals > > > > > are kept in a dict and are looked up dynamically. > > > > > > > > If the compiler can do any decision on the variable's name, when it goes to line of print, how it handle it? > > > > > > > > x = 0 > > > > def f(): > > > > print(x) > > > > > > > > def g(): > > > > print(x) > > > > x = 1 > > > > print(y) > > > > > > > > > > Not sure what you mean by "decision", but the definition is that since > > > x is assigned to in g(), it is local to g(). It's not local to f(), so > > > the global is visible. > > > > So, may I say that the Python compiler is a multi-pass one? > > > > At this point, you're veering away from "Python-the-language" and > towards "CPython-the-implementation" (or whichever other > implementation you want to explore). Yes, it's a little away:-) I was curious about how the compiled print(x) can be influenced by x = 1 after it. Anyway, thank you for your confirmation. --Jach > But I would say that, yes, most > or all Python implementations will use multi-pass compilation. It's > the easiest way to guarantee correct behaviour. For instance, when you > reach a "try" statement, you don't know whether there'll be an > "except" block after it (there might just be a "finally"); the > bytecode that CPython produces is different for setting up a > finally-only block than for setting up a try/except. Easiest to find > out that sort of thing by running multiple passes. > > Plus, depending on how you define "pass", there are additional steps > such as constant folding and peephole optimization that can be done > after everything else. So, yeah, it's highly unlikely that there are > any performant implementations of Python that run a single compilation > pass. > > ChrisA From rustompmody at gmail.com Wed Feb 27 00:42:16 2019 From: rustompmody at gmail.com (Rustom Mody) Date: Tue, 26 Feb 2019 21:42:16 -0800 (PST) Subject: Multi meaning of label: Doc bug Message-ID: Ref: This stackexchange post: https://unix.stackexchange.com/q/503241/323121 Context: Theres this guy who's really struggling with disk partitioning LVM etc concepts. That point is not directly relevant to this question. My answer on that post tries to clarify that 'label' can mean 3 things at least; and that just in the context of disks, partitions etc. Quite confusing!! My question: In his parted output he has Error: /dev/mapper/lubuntu--vg-home: unrecognised disk label In which sense is this 'label' used?? Is it the Open/Free-BSD sense? Or the partition label sense? From rustompmody at gmail.com Wed Feb 27 00:43:21 2019 From: rustompmody at gmail.com (Rustom Mody) Date: Tue, 26 Feb 2019 21:43:21 -0800 (PST) Subject: Multi meaning of label: Doc bug In-Reply-To: References: Message-ID: <306dca11-2f7b-40f9-b851-2175d57dd622@googlegroups.com> On Wednesday, February 27, 2019 at 11:12:28 AM UTC+5:30, Rustom Mody wrote: > Ref: This stackexchange post: > https://unix.stackexchange.com/q/503241/323121 > > Context: Theres this guy who's really struggling with disk partitioning LVM etc concepts. That point is not directly relevant to this question. > > My answer on that post tries to clarify that 'label' can mean 3 things at least; > and that just in the context of disks, partitions etc. Quite confusing!! > > My question: In his parted output he has > > Error: /dev/mapper/lubuntu--vg-home: unrecognised disk label > > In which sense is this 'label' used?? > > Is it the Open/Free-BSD sense? Or the partition label sense? OOps wrong list! Sorry! From marko at pacujo.net Wed Feb 27 01:56:32 2019 From: marko at pacujo.net (Marko Rauhamaa) Date: Wed, 27 Feb 2019 08:56:32 +0200 Subject: Lifetime of a local reference References: <8736oai5c7.fsf@elektro.pacujo.net> <86pnrd2a95.fsf@richard.bawden.org> Message-ID: <87y361hg7z.fsf@elektro.pacujo.net> Alan Bawden : > Marko Rauhamaa writes: >> def fun(): >> f = open("lock") >> flock.flock(f, fcntl.LOCK_EX) >> do_stuff() >> sys.exit(0) >> >> Question: can a compliant Python implementation close f (and, >> consequently, release the file lock) before/while do_stuff() is >> executed? > > A correct-but-fails-to-answer-your-real-question answer would be: "If > you _care_ about when f gets closed, then just call f.close() > yourself." So if you want to be _sure_ the lock stays locked while you > "do stuff", you should write: > > def fun(): > f = open("lock") > flock.flock(f, fcntl.LOCK_EX) > do_stuff() > f.close() > sys.exit(0) > > And now you don't have to worry about the details of variable > lifetimes. Yes, although the operating system closes all files (and releases the associated file locks) on process exit. > But I appreciate that that isn't the true question that you wanted to ask! > You are wondering if a Python implementation is _permitted_ to treat the > code you wrote _as if_ you had written: > > def fun(): > f = open("lock") > flock.flock(f, fcntl.LOCK_EX) > del f > do_stuff() > sys.exit(0) > > which deletes the variable f from the local environment at a point where it > will never be used again. (Which could cause the lock to be released > before do_stuff is even called.) > > This is an interesting question, and one that any garbage collected > language should probably address somehow. For example, the Java Language > Specification contains the following language: > > Optimizing transformations of a program can be designed that reduce the > number of objects that are reachable to be less than those which would > naively be considered reachable. For example, a Java compiler or code > generator may choose to set a variable or parameter that will no longer be > used to null to cause the storage for such an object to be potentially > reclaimable sooner. > > (This is from section 12.6.1 of the Java 8 version, which is what I had > handy.) C compilers do similar things, which is why Guile documentation mentions a special mechanism to prevent premature garbage collection: > I suspect that given the history of Python, pretty much everybody has > always assumed that a Python implementation will not delete local > variables early. But I agree with you that the Python Language > Reference does not appear to address this question anywhere! Then there's the question of a sufficient way to prevent premature garbage collection: def fun(): f = open("lock") flock.flock(f, fcntl.LOCK_EX) do_stuff() f.close() sys.exit(0) def fun(): f = open("lock") flock.flock(f, fcntl.LOCK_EX) do_stuff() f.close sys.exit(0) def fun(): f = open("lock") flock.flock(f, fcntl.LOCK_EX) do_stuff() f sys.exit(0) def fun(): f = open("lock") flock.flock(f, fcntl.LOCK_EX) do_stuff() sys.exit(0) Marko From onlinejudge95 at gmail.com Wed Feb 27 03:20:32 2019 From: onlinejudge95 at gmail.com (Test Bot) Date: Wed, 27 Feb 2019 13:50:32 +0530 Subject: Lifetime of a local reference In-Reply-To: <8736oai5c7.fsf@elektro.pacujo.net> References: <8736oai5c7.fsf@elektro.pacujo.net> Message-ID: Just to add on regarding file I/O. It would be more pythonic to use. with open(path): do_stuff() On Wed, Feb 27, 2019, 3:31 AM Marko Rauhamaa wrote: > > Consider this function: > > def fun(): > f = open("lock") > flock.flock(f, fcntl.LOCK_EX) > do_stuff() > sys.exit(0) > > Question: can a compliant Python implementation close f (and, > consequently, release the file lock) before/while do_stuff() is > executed? > > I couldn't find an immediate answer in the documentation. > > > Marko > -- > https://mail.python.org/mailman/listinfo/python-list > From tjol at tjol.eu Wed Feb 27 04:30:06 2019 From: tjol at tjol.eu (Thomas Jollans) Date: Wed, 27 Feb 2019 10:30:06 +0100 Subject: Lifetime of a local reference In-Reply-To: <86pnrd2a95.fsf@richard.bawden.org> References: <8736oai5c7.fsf@elektro.pacujo.net> <86pnrd2a95.fsf@richard.bawden.org> Message-ID: <33442b3b-1aac-df09-84ab-a24d9ca1c7aa@tjol.eu> On 27/02/2019 04.14, Alan Bawden wrote: > Marko Rauhamaa writes: >> I couldn't find an immediate answer in the documentation. > > I suspect that given the history of Python, pretty much everybody has > always assumed that a Python implementation will not delete local variables > early. But I agree with you that the Python Language Reference does not > appear to address this question anywhere! > That's probably right. However, due to the nature of Python, things like this are possible: >>> v = 'a' >>> def f(): ... a, b, c = 1, 2, 3 ... return eval(v) ... >>> f() 1 >>> v = 'b' >>> f() 2 >>> What I mean to say is that it's not in general trivial (or even possible) to tell if a local is referred to later in the function body. I think we can say that a Python interpreter can't delete a local before having executed any statement that could *possibly* access it. If the inspect module's stack frame inspection machinery is supported, then any function call might access any local... (though I don't think a compliant Python implementation necessarily has to support the inspect module fully). And we're back to where we started. -- Thomas From rhodri at kynesim.co.uk Wed Feb 27 09:18:37 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Wed, 27 Feb 2019 14:18:37 +0000 Subject: Lifetime of a local reference In-Reply-To: <87y361hg7z.fsf@elektro.pacujo.net> References: <8736oai5c7.fsf@elektro.pacujo.net> <86pnrd2a95.fsf@richard.bawden.org> <87y361hg7z.fsf@elektro.pacujo.net> Message-ID: On 27/02/2019 06:56, Marko Rauhamaa wrote: > Alan Bawden : >> But I appreciate that that isn't the true question that you wanted to ask! >> You are wondering if a Python implementation is _permitted_ to treat the >> code you wrote _as if_ you had written: >> >> def fun(): >> f = open("lock") >> flock.flock(f, fcntl.LOCK_EX) >> del f >> do_stuff() >> sys.exit(0) >> >> which deletes the variable f from the local environment at a point where it >> will never be used again. (Which could cause the lock to be released >> before do_stuff is even called.) >> >> This is an interesting question, and one that any garbage collected >> language should probably address somehow. Interesting indeed. My gut instinct was "Hell, no!", but as Marko points out C optimisers do exactly that, and I don't find it particularly surprising in practice. I don't think that there is anything in the docs that says a compliant implementation couldn't delete variables early. The nearest you get is repeated reminders that you can't make assumptions about when destructors will be run. > Then there's the question of a sufficient way to prevent premature > garbage collection: > > def fun(): > f = open("lock") > flock.flock(f, fcntl.LOCK_EX) > do_stuff() > f.close() > sys.exit(0) > > def fun(): > f = open("lock") > flock.flock(f, fcntl.LOCK_EX) > do_stuff() > f.close > sys.exit(0) > > def fun(): > f = open("lock") > flock.flock(f, fcntl.LOCK_EX) > do_stuff() > f > sys.exit(0) > > def fun(): > f = open("lock") > flock.flock(f, fcntl.LOCK_EX) > do_stuff() > sys.exit(0) I would go with: def fun(): with open("lock") as f: flock.flock(f, fcntl.LOCK_EX) do_stuff() sys.exit(0) The description of the with statement does explicitly say that the context manager's __exit__() method won't be called until the suite has been executed, so the reference to the open file must exist for at least that long. -- Rhodri James *-* Kynesim Ltd From dboland9 at offilive.com Wed Feb 27 10:10:45 2019 From: dboland9 at offilive.com (Dave) Date: Wed, 27 Feb 2019 10:10:45 -0500 Subject: ANN: Creating GUI Applications with wxPython References: Message-ID: On 1/14/19 2:08 PM, Mike Driscoll wrote: > Hi, > > I just thought I would let you all know that I am working on my 2nd wxPython book, "Creating GUI Applications with wxPython". This one will be about actually creating small runnable applications instead of just recipes like my Cookbook did. I hope to have 8-10 working applications included with the book. > > You can read more about it here if you are interested: https://www.blog.pythonlibrary.org/2019/01/14/creating-gui-applications-with-wxpython-kickstarter/ > > Feel free to ask me questions about it too. > > Thanks, > Mike > Mike, I have two Python 3 (3.6) apps that will get the full GUI treatment very soon. I'm in the process of choosing a GUI, and that may be where you/your book can help. Seems this is not a trivial effort (wishing that Python was like VB6 from the 90's). Anyway, here is what I am looking for - hopefully it helps guide you. * GUI relatively easy to understand and implement. easyGUI is truly easy in all areas, but fails some of my other requirements. The QT/PyQT/PySide2 situation is a mess - which one to use, why, any implementation differences, etc. * Performance is very good. Users hate to wait - for anything! * Lots of widgets! I'll need a spreadsheet-like widget, a form widget to enter information on parts, activities, etc., splash screen that the code can talk to while the app is loading, and the big one - a cross-platform printer widget that works with Windows and Mac/Unix/CUPS. * GUI must support all desktops with a native look and feel. Kivy fails this one. Will have mobile apps later in the year, so it would be nice if one GUI fits all, but am ok with 2 gui's if needed. * A great book taking me from beginner to expert. Dave, From dboland9 at offilive.com Wed Feb 27 10:37:02 2019 From: dboland9 at offilive.com (Dave) Date: Wed, 27 Feb 2019 10:37:02 -0500 Subject: ANN: Creating GUI Applications with wxPython References: Message-ID: <653d5469-56ef-cef5-72a6-9dfb20da27ea@offilive.com> On 1/14/19 2:08 PM, Mike Driscoll wrote: > Hi, > > I just thought I would let you all know that I am working on my 2nd wxPython book, "Creating GUI Applications with wxPython". This one will be about actually creating small runnable applications instead of just recipes like my Cookbook did. I hope to have 8-10 working applications included with the book. > > You can read more about it here if you are interested: https://www.blog.pythonlibrary.org/2019/01/14/creating-gui-applications-with-wxpython-kickstarter/ > > Feel free to ask me questions about it too. > > Thanks, > Mike > Mike, I have two Python 3 (3.6) apps that will get the full GUI treatment very soon. I'm in the process of choosing a GUI, and that may be where you/your book can help. Seems this is not a trivial effort (wishing that Python was like VB6 from the 90's). Anyway, here is what I am looking for - hopefully it helps guide you. * GUI relatively easy to understand and implement. easyGUI is truly easy in all areas, but fails some of my other requirements. The QT/PyQT/PySide2 situation is a mess - which one to use, why, any implementation differences, etc. * Performance is very good. Users hate to wait - for anything! * Lots of widgets! I'll need a spreadsheet-like widget, a form widget to enter information on parts, activities, etc., splash screen that the code can talk to while the app is loading, and the big one - a cross-platform printer widget that works with Windows and Mac/Unix/CUPS. A network widget may also be good. * A GUI designer may be good. Currently there is Glade for GTK, and QT-Designer for QT. * GUI must support all desktops with a native look and feel. Kivy fails this one. Will have mobile apps later in the year, so it would be nice if one GUI fits all, but am ok with 2 gui's if needed. * A great book taking me from beginner to expert. Dave, From dboland9 at offilive.com Wed Feb 27 10:38:25 2019 From: dboland9 at offilive.com (Dave) Date: Wed, 27 Feb 2019 10:38:25 -0500 Subject: ANN: Creating GUI Applications with wxPython References: Message-ID: On 1/14/19 2:08 PM, Mike Driscoll wrote: > Hi, > > I just thought I would let you all know that I am working on my 2nd wxPython book, "Creating GUI Applications with wxPython". This one will be about actually creating small runnable applications instead of just recipes like my Cookbook did. I hope to have 8-10 working applications included with the book. > > You can read more about it here if you are interested: https://www.blog.pythonlibrary.org/2019/01/14/creating-gui-applications-with-wxpython-kickstarter/ > > Feel free to ask me questions about it too. > > Thanks, > Mike > Mike, I have two Python 3 (3.6) apps that will get the full GUI treatment very soon. I'm in the process of choosing a GUI, and that may be where you/your book can help. Seems this is not a trivial effort (wishing that Python was like VB6 from the 90's). Anyway, here is what I am looking for - hopefully it helps guide you. * GUI relatively easy to understand and implement. easyGUI is truly easy in all areas, but fails some of my other requirements. The QT/PyQT/PySide2 situation is a mess - which one to use, why, any implementation differences, etc. * Performance is very good. Users hate to wait - for anything! * Lots of widgets! I'll need a spreadsheet-like widget, a form widget to enter information on parts, activities, etc., splash screen that the code can talk to while the app is loading, and the big one - a cross-platform printer widget that works with Windows and Mac/Unix/CUPS. A network widget may also be good. * A GUI designer may be good. Currently there is Glade for GTK, and QT-Designer for QT. * GUI must support all desktops with a native look and feel. Kivy fails this one. Will have mobile apps later in the year, so it would be nice if one GUI fits all, but am ok with 2 gui's if needed. * A great book taking me from beginner to expert. Dave, From marko at pacujo.net Wed Feb 27 10:41:45 2019 From: marko at pacujo.net (Marko Rauhamaa) Date: Wed, 27 Feb 2019 17:41:45 +0200 Subject: Lifetime of a local reference References: <8736oai5c7.fsf@elektro.pacujo.net> <86pnrd2a95.fsf@richard.bawden.org> <87y361hg7z.fsf@elektro.pacujo.net> Message-ID: <874l8pqlvq.fsf@elektro.pacujo.net> Rhodri James : > On 27/02/2019 06:56, Marko Rauhamaa wrote: >> Then there's the question of a sufficient way to prevent premature >> garbage collection: >> >> def fun(): >> f = open("lock") >> flock.flock(f, fcntl.LOCK_EX) >> do_stuff() >> f.close() >> sys.exit(0) >> >> def fun(): >> f = open("lock") >> flock.flock(f, fcntl.LOCK_EX) >> do_stuff() >> f.close >> sys.exit(0) >> >> def fun(): >> f = open("lock") >> flock.flock(f, fcntl.LOCK_EX) >> do_stuff() >> f >> sys.exit(0) >> >> def fun(): >> f = open("lock") >> flock.flock(f, fcntl.LOCK_EX) >> do_stuff() >> sys.exit(0) > > I would go with: > > def fun(): > with open("lock") as f: > flock.flock(f, fcntl.LOCK_EX) > do_stuff() > sys.exit(0) > > The description of the with statement does explicitly say that the > context manager's __exit__() method won't be called until the suite > has been executed, so the reference to the open file must exist for at > least that long. Yeah, but the *true* answer, of course, is: def fun(): f = os.open("lock", os.O_RDONLY) flock.flock(f, fcntl.LOCK_EX) do_stuff() sys.exit(0) Collect that! ;-) Marko From dboland9 at offilive.com Wed Feb 27 10:41:53 2019 From: dboland9 at offilive.com (Dave) Date: Wed, 27 Feb 2019 10:41:53 -0500 Subject: ANN: Creating GUI Applications with wxPython References: Message-ID: Sorry about the duplicate messages - bad hair day! Dave, On 2/27/19 10:38 AM, Dave wrote: > On 1/14/19 2:08 PM, Mike Driscoll wrote: >> Hi, >> >> I just thought I would let you all know that I am working on my 2nd >> wxPython book, "Creating GUI Applications with wxPython". This one >> will be about actually creating small runnable applications instead of >> just recipes like my Cookbook did. I hope to have 8-10 working >> applications included with the book. >> >> You can read more about it here if you are interested: >> https://www.blog.pythonlibrary.org/2019/01/14/creating-gui-applications-with-wxpython-kickstarter/ >> >> >> Feel free to ask me questions about it too. >> >> Thanks, >> Mike >> > > Mike, > > I have two Python 3 (3.6) apps that will get the full GUI treatment very > soon.? I'm in the process of choosing a GUI, and that may be where > you/your book can help.? Seems this is not a trivial effort (wishing > that Python was like VB6 from the 90's). > > Anyway, here is what I am looking for - hopefully it helps guide you. > > * GUI relatively easy to understand and implement.? easyGUI is truly > easy in all areas, but fails some of my other requirements.? The > QT/PyQT/PySide2 situation is a mess - which one to use, why, any > implementation differences, etc. > > * Performance is very good.? Users hate to wait - for anything! > > * Lots of widgets!? I'll need a spreadsheet-like widget, a form widget > to enter information on parts, activities, etc., splash screen that the > code can talk to while the app is loading, and the big one - a > cross-platform printer widget that works with Windows and Mac/Unix/CUPS. > ?A network widget may also be good. > > * A GUI designer may be good.? Currently there is Glade for GTK, and > QT-Designer for QT. > > * GUI must support all desktops with a native look and feel.? Kivy fails > this one.? Will have mobile apps later in the year, so it would be nice > if one GUI fits all, but am ok with 2 gui's if needed. > > * A great book taking me from beginner to expert. > > Dave, > > From tjol at tjol.eu Wed Feb 27 10:55:51 2019 From: tjol at tjol.eu (Thomas Jollans) Date: Wed, 27 Feb 2019 16:55:51 +0100 Subject: Lifetime of a local reference In-Reply-To: <874l8pqlvq.fsf@elektro.pacujo.net> References: <8736oai5c7.fsf@elektro.pacujo.net> <86pnrd2a95.fsf@richard.bawden.org> <87y361hg7z.fsf@elektro.pacujo.net> <874l8pqlvq.fsf@elektro.pacujo.net> Message-ID: <83818246-d883-0863-58a7-149c4004ae05@tjol.eu> On 27/02/2019 16.41, Marko Rauhamaa wrote: > Rhodri James : >> The description of the with statement does explicitly say that the >> context manager's __exit__() method won't be called until the suite >> has been executed, so the reference to the open file must exist for at >> least that long. > > Yeah, but the *true* answer, of course, is: > > def fun(): > f = os.open("lock", os.O_RDONLY) > flock.flock(f, fcntl.LOCK_EX) > do_stuff() > sys.exit(0) > > Collect that! No problemo! try: fun() except SystemExit: print("Oh no you don't!") Collect this: def fun(): f = os.open("lock", os.O_RDONLY) flock.flock(f, fcntl.LOCK_EX) do_stuff() os.kill(os.getpid(), 9) ;-) ? Thomas From robin at reportlab.com Wed Feb 27 11:24:47 2019 From: robin at reportlab.com (Robin Becker) Date: Wed, 27 Feb 2019 16:24:47 +0000 Subject: argparse namespace clashes Message-ID: <5d2c526f-1c22-7932-09c3-a38330d93859@chamonix.reportlab.co.uk> After converting a previously working optparse I found that I should not be using 'args' as a destination as it seemed to break django 1.11 management command execution. I changed both the flag name '--args' to '--task-args' and the destination 'args' to 'task-args' and that seemed to fix things. Is there a recommendation anywhere for names and detinations to avoid? -- Robin Becker From rhodri at kynesim.co.uk Wed Feb 27 11:38:02 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Wed, 27 Feb 2019 16:38:02 +0000 Subject: ANN: Creating GUI Applications with wxPython In-Reply-To: <653d5469-56ef-cef5-72a6-9dfb20da27ea@offilive.com> References: <653d5469-56ef-cef5-72a6-9dfb20da27ea@offilive.com> Message-ID: <57fd10d8-fe13-1371-46c1-2150cb8613ce@kynesim.co.uk> On 27/02/2019 15:37, Dave wrote: > * GUI must support all desktops with a native look and feel.? Kivy fails > this one.? Will have mobile apps later in the year, so it would be nice > if one GUI fits all, but am ok with 2 gui's if needed. This requirement will cause you endless pain, particularly when mobiles enter the picture. I'm not convinced it's a completely good idea; a sensible UI for a desktop probably won't be sensible for a phone and vice versa -- Rhodri James *-* Kynesim Ltd From dboland9 at offilive.com Wed Feb 27 11:45:57 2019 From: dboland9 at offilive.com (Dave) Date: Wed, 27 Feb 2019 11:45:57 -0500 Subject: ANN: Creating GUI Applications with wxPython References: <653d5469-56ef-cef5-72a6-9dfb20da27ea@offilive.com> <57fd10d8-fe13-1371-46c1-2150cb8613ce@kynesim.co.uk> Message-ID: On 2/27/19 11:38 AM, Rhodri James wrote: > On 27/02/2019 15:37, Dave wrote: >> * GUI must support all desktops with a native look and feel.? Kivy >> fails this one.? Will have mobile apps later in the year, so it would >> be nice if one GUI fits all, but am ok with 2 gui's if needed. > > This requirement will cause you endless pain, particularly when mobiles > enter the picture.? I'm not convinced it's a completely good idea; a > sensible UI for a desktop probably won't be sensible for a phone and > vice versa > Agreed - that is why I'm open to two GUI kits. Dave From ikorot01 at gmail.com Wed Feb 27 12:59:17 2019 From: ikorot01 at gmail.com (Igor Korot) Date: Wed, 27 Feb 2019 11:59:17 -0600 Subject: ANN: Creating GUI Applications with wxPython In-Reply-To: References: <653d5469-56ef-cef5-72a6-9dfb20da27ea@offilive.com> <57fd10d8-fe13-1371-46c1-2150cb8613ce@kynesim.co.uk> Message-ID: Hi, On Wed, Feb 27, 2019 at 10:53 AM Dave wrote: > > On 2/27/19 11:38 AM, Rhodri James wrote: > > On 27/02/2019 15:37, Dave wrote: > >> * GUI must support all desktops with a native look and feel. Kivy > >> fails this one. Will have mobile apps later in the year, so it would > >> be nice if one GUI fits all, but am ok with 2 gui's if needed. > > > > This requirement will cause you endless pain, particularly when mobiles > > enter the picture. I'm not convinced it's a completely good idea; a > > sensible UI for a desktop probably won't be sensible for a phone and > > vice versa > > > > Agreed - that is why I'm open to two GUI kits. It is not about the toolkit, but more about the design. If something works properly on the desktop, there is no guarantee it will look sensible and normal on the mobile device. Besides UI guidelines for Android/iOS are very different than for the desktop application. Hopefully with the wxQt having more and more development we might have a wxQt/Android port getting attention. Thank you. > > Dave > -- > https://mail.python.org/mailman/listinfo/python-list From Karsten.Hilbert at gmx.net Wed Feb 27 13:36:31 2019 From: Karsten.Hilbert at gmx.net (Karsten Hilbert) Date: Wed, 27 Feb 2019 19:36:31 +0100 Subject: ANN: Creating GUI Applications with wxPython In-Reply-To: References: Message-ID: <20190227183631.GA1530@hermes.hilbert.loc> On Wed, Feb 27, 2019 at 10:38:25AM -0500, Dave wrote: > * GUI relatively easy to understand and implement. easyGUI is truly easy in > all areas, but fails some of my other requirements. The QT/PyQT/PySide2 > situation is a mess - which one to use, why, any implementation differences, > etc. Python has been made a first-tier supported language in Qt just recently if I understood the news correctly. So, PySide2. > * Performance is very good. Users hate to wait - for anything! That's rarely a concern in chosing a GUI toolkit. > * Lots of widgets! I'll need a spreadsheet-like widget, a form widget to > enter information on parts, activities, etc., splash screen that the code > can talk to while the app is loading, and the big one - a cross-platform > printer widget that works with Windows and Mac/Unix/CUPS. A network widget > may also be good. Usable built-in printer support (and "network widget would be good") takes wxPython out of the equation. Karsten PS: Myself using wxPython, so no bashing there. -- GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B From bluethundr at gmail.com Tue Feb 26 15:06:14 2019 From: bluethundr at gmail.com (Tim Dunphy) Date: Tue, 26 Feb 2019 15:06:14 -0500 Subject: Python 3 Boto Unable to parse pagination error Message-ID: Hi all, I'm trying to create a python 3 script that takes a list of AWS instance IDs. It then prints out some information about the instances and then terminates them. I am very new to python, so still working through some basic issues. This is the error I get when I run the script: python3 .\aws_ec2_termiante_instances.py Enter an instance ID separated by commas: i-0463897140af217f8,i-0634fc5fa453462fb enter code here Deleting Instance IDs: i-0463897140af217f8 i-0634fc5fa453462fb Traceback (most recent call last): File ".\aws_ec2_termiante_instances.py", line 37, in NextToken='string' File "C:\Users\tdunphy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\botocore\client.py", line 357, in _api_call return self._make_api_call(operation_name, kwargs) File "C:\Users\tdunphy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\botocore\client.py", line 661, in _make_api_call raise error_class(parsed_response, operation_name) botocore.exceptions.ClientError: An error occurred (InvalidParameterValue) when calling the DescribeInstances operation: Unable to parse pagination token This is my script that produces the above output: from collections import defaultdict import boto3 # Connect to EC2 ec2 = boto3.client('ec2') instance_id_list = input("Enter an instance ID separated by commas: ") instance_ids_text = instance_id_list.split(",") print("Deleting Instance IDs:") for instance_id in instance_ids_text: print(instance_id) ec2info = defaultdict() for instance_id in instance_ids_text: instance = ec2.describe_instances( Filters=[ { 'Name': 'string', 'Values': [ 'string', ] }, ], InstanceIds=[ 'instance_id', ], MaxResults=123, NextToken='string' ) for tag in instance.tags: if 'Name'in tag['Key']: name = tag['Value'] # Add instance info to a dictionary ec2info[instance.id] = { 'Name': name, 'Instance ID': instance.id, 'Type': instance.instance_type, 'State': instance.state['Name'], 'Private IP': instance.private_ip_address, 'Public IP': instance.public_ip_address, 'Launch Time': instance.launch_time } attributes = ['Name', 'Instance ID', 'Type', 'State', 'Private IP', 'Public IP', 'Launch Time'] for instance_id, instance in ec2info.items(): for key in attributes: print("{0}: {1}".format(key, instance[key])) print("------") ec2.instances.filter(InstanceIds=instance.id).stop() ec2.instances.filter(InstanceIds=instance.id).terminate() To recap, what I would like to do is give the script a list of instance IDs, print out the list of instance IDs, then print some info about those instances. Then terminate them. How can I get past this error and produce that result? Thank you, Tim -- GPG me!! gpg --keyserver pool.sks-keyservers.net --recv-keys F186197B From psam1304 at gmail.com Tue Feb 26 23:54:16 2019 From: psam1304 at gmail.com (Phu Sam) Date: Tue, 26 Feb 2019 20:54:16 -0800 Subject: Dictionary In-Reply-To: References: Message-ID: The condition 'if not fibs.get(n):' will not work because n = 0 fibs.get(0) is 0 so not 0 is 1 Here is the modified code that works: fibs={0:0,1:1} def rfib(n): if n == 0 or n == 1: return fibs[n] else: fibs[n]=rfib(n-2)+rfib(n-1) return fibs[n] >>> rfib(8) 21 >>> fibs {0: 0, 1: 1, 2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21} Cheers, Phu On Sun, Feb 24, 2019 at 10:56 AM Himanshu Yadav < himanshuyadav9630994925 at gmail.com> wrote: > fibs={0:0,1:1} > def rfib(n): > global fibs > if not fibs.get(n): > fibs[n]=rfib(n-2)+rfib(n-1) > return fibs[n] > > Why it is gives error?? > -- > https://mail.python.org/mailman/listinfo/python-list > From psam1304 at gmail.com Wed Feb 27 00:28:13 2019 From: psam1304 at gmail.com (Phu Sam) Date: Tue, 26 Feb 2019 21:28:13 -0800 Subject: Scraping multiple web pages help In-Reply-To: References: Message-ID: You need to obtain a key for API first - from https://regulationsgov.github.io/developers/ The Regulations.gov API is taking action to conserve system resources. Beginning immediately, we will limit access to one account per organization, and require approval for enabling accounts.* Please contact the * *Regulations.gov Help Desk, if you would like to request an API key. * Please provide your name, email address, organization, and intended use of the API. Send email to: regulations at erulemakinghelpdesk.com Good luck, Phu On Mon, Feb 18, 2019 at 10:19 AM Drake Gossi wrote: > Hello everyone, > > For a research project, I need to scrape a lot of comments from > regulations.gov > > > https://www.regulations.gov/docketBrowser?rpp=25&so=DESC&sb=commentDueDate&po=0&dct=PS&D=ED-2018-OCR-0064 > > But partly what's throwing me is the url addresses of the comments. They > aren't consistent. I mean, there's some consistency insofar as the numbers > that differentiate the pages all begin after that 0064 number in the url > listed above. But the differnetiating numbers aren't even all the same > amount of numbers. Some are 4 (say, 4019) whereas others are 5 (say, > 50343). But I dont think they go over 5. So this is a problem. I dont know > how to write the code to access the multiple pages. > > I should also mention I'm new to programing, so that's also a problem (if > you cant already tell by the way I'm describing my problem). > > > I should also mention that, I think, there's an API on regulations.gov, > but > I'm such a beginner that I dont evem really know where to find it, or even > what to do with it once I do. That's how helpless am right now. > > Any help anyone could offer would be much appreciated. > > D > -- > https://mail.python.org/mailman/listinfo/python-list > From python at mrabarnett.plus.com Wed Feb 27 14:11:44 2019 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 27 Feb 2019 19:11:44 +0000 Subject: Python 3 Boto Unable to parse pagination error In-Reply-To: References: Message-ID: <3b935f0a-719c-3b3c-c960-0fa1dc254b96@mrabarnett.plus.com> On 2019-02-26 20:06, Tim Dunphy wrote: > Hi all, > > I'm trying to create a python 3 script that takes a list of AWS instance > IDs. It then prints out some information about the instances and then > terminates them. > > I am very new to python, so still working through some basic issues. > > This is the error I get when I run the script: > > python3 .\aws_ec2_termiante_instances.py > Enter an instance ID separated by commas: > i-0463897140af217f8,i-0634fc5fa453462fb > enter code here > Deleting Instance IDs: > i-0463897140af217f8 > i-0634fc5fa453462fb > Traceback (most recent call last): > File ".\aws_ec2_termiante_instances.py", line 37, in > NextToken='string' > File > "C:\Users\tdunphy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\botocore\client.py", > line 357, in _api_call > return self._make_api_call(operation_name, kwargs) > File > "C:\Users\tdunphy\AppData\Local\Programs\Python\Python37-32\lib\site-packages\botocore\client.py", > line 661, in _make_api_call > raise error_class(parsed_response, operation_name) > botocore.exceptions.ClientError: An error occurred > (InvalidParameterValue) when calling the DescribeInstances operation: > Unable to parse pagination token > > This is my script that produces the above output: > > from collections import defaultdict > import boto3 > # Connect to EC2 > ec2 = boto3.client('ec2') > > instance_id_list = input("Enter an instance ID separated by commas: ") > instance_ids_text = instance_id_list.split(",") instance_id_list is a string and instance_ids_text is a list of strings, so the names are somewhat misleading. > print("Deleting Instance IDs:") > for instance_id in instance_ids_text: > print(instance_id) > > ec2info = defaultdict() > for instance_id in instance_ids_text: > instance = ec2.describe_instances( > Filters=[ > { > 'Name': 'string', > 'Values': [ > 'string', > ] > }, > ], > InstanceIds=[ > 'instance_id', Shouldn't this be the actual id, not the string 'instance_id'? > ], > MaxResults=123, > NextToken='string' > ) > for tag in instance.tags: > if 'Name'in tag['Key']: > name = tag['Value'] > # Add instance info to a dictionary > ec2info[instance.id] = { > 'Name': name, > 'Instance ID': instance.id, > 'Type': instance.instance_type, > 'State': instance.state['Name'], > 'Private IP': instance.private_ip_address, > 'Public IP': instance.public_ip_address, > 'Launch Time': instance.launch_time > } > > attributes = ['Name', 'Instance ID', 'Type', 'State', > 'Private IP', 'Public IP', 'Launch Time'] > for instance_id, instance in ec2info.items(): > for key in attributes: > print("{0}: {1}".format(key, instance[key])) > print("------") > ec2.instances.filter(InstanceIds=instance.id).stop() > ec2.instances.filter(InstanceIds=instance.id).terminate() > > To recap, what I would like to do is give the script a list of instance > IDs, print out the list of instance IDs, then print some info about those > instances. Then terminate them. > > How can I get past this error and produce that result? > From maillist at schwertberger.de Wed Feb 27 13:50:45 2019 From: maillist at schwertberger.de (Dietmar Schwertberger) Date: Wed, 27 Feb 2019 19:50:45 +0100 Subject: ANN: Creating GUI Applications with wxPython In-Reply-To: References: Message-ID: <17cf0842-185f-fd3c-f286-c99fd06ffe34@schwertberger.de> On 27.02.2019 16:10, Dave wrote: > I have two Python 3 (3.6) apps that will get the full GUI treatment > very soon.? I'm in the process of choosing a GUI, and that may be > where you/your book can help.? Seems this is not a trivial effort > (wishing that Python was like VB6 from the 90's). IMHO what comes closest to VB6 is wxPython with wxGlade. The focus of wxGlade is on frames and dialogs with standard widgets, though. Graphics and printing is always a bit more effort. Regards, Dietmar From torriem at gmail.com Wed Feb 27 14:16:36 2019 From: torriem at gmail.com (Michael Torrie) Date: Wed, 27 Feb 2019 12:16:36 -0700 Subject: ANN: Creating GUI Applications with wxPython In-Reply-To: <20190227183631.GA1530@hermes.hilbert.loc> References: <20190227183631.GA1530@hermes.hilbert.loc> Message-ID: <59c13996-c606-12e8-545b-bb18c220e511@gmail.com> On 02/27/2019 11:36 AM, Karsten Hilbert wrote: > On Wed, Feb 27, 2019 at 10:38:25AM -0500, Dave wrote: > >> * GUI relatively easy to understand and implement. easyGUI is truly easy in >> all areas, but fails some of my other requirements. The QT/PyQT/PySide2 >> situation is a mess - which one to use, why, any implementation differences, >> etc. > > Python has been made a first-tier supported language in Qt > just recently if I understood the news correctly. So, PySide2. Except PySide2 doesn't yet support all of the core Qt class API, such as QtSerialPort. Of course there's PySerial in PyPi, but that's not integrated with the Qt main loop. So for me it's PyQt, but hoping PySide2 will catch up eventually. Other than missing bits, with a small wrapper module I wrote, it's actually quite trivial to make code that works without change on PyQt or PySide2. So it's not the mess that Dave makes it out to be. Back in PySide days, I found a couple of occasions where references to C++ objects would be dropped, causing segfaults. This was due to an impedance mismatch between Python and C++ in terms of references and object lifecycles. Haven't encountered that in PySide2 though. >> * Performance is very good. Users hate to wait - for anything! > > That's rarely a concern in chosing a GUI toolkit. Agreed. Waiting isn't because the UI doesn't have good performance. It's because you blocked the main loop. Learn how to work with your UI framework's event loop and you won't have this problem. >> * Lots of widgets! I'll need a spreadsheet-like widget, a form widget to >> enter information on parts, activities, etc., splash screen that the code >> can talk to while the app is loading, and the big one - a cross-platform >> printer widget that works with Windows and Mac/Unix/CUPS. A network widget >> may also be good. > > Usable built-in printer support (and "network widget would be > good") takes wxPython out of the equation. wxWidgets does seem to have a wide variety of third-party widgets for doing all kinds of things like Dave wants, but how much of that is exposed via Python wrappers? > Karsten > PS: Myself using wxPython, so no bashing there. The future of Qt is QtQuick, and the advantage of it is that whatever fancy visualization widgets come along, they will be exposed to all language bindings without any special wrappers. The downside for me is the heavy use of Javascript for UI logic and event handling. From 2QdxY4RzWzUUiLuE at potatochowder.com Wed Feb 27 14:34:21 2019 From: 2QdxY4RzWzUUiLuE at potatochowder.com (Dan Sommers) Date: Wed, 27 Feb 2019 13:34:21 -0600 Subject: ANN: Creating GUI Applications with wxPython In-Reply-To: <653d5469-56ef-cef5-72a6-9dfb20da27ea@offilive.com> References: <653d5469-56ef-cef5-72a6-9dfb20da27ea@offilive.com> Message-ID: On 2/27/19 9:37 AM, Dave wrote: > I have two Python 3 (3.6) apps that will get the full GUI treatment very > soon. I'm in the process of choosing a GUI, and that may be where > you/your book can help. Seems this is not a trivial effort (wishing > that Python was like VB6 from the 90's). > > Anyway, here is what I am looking for - hopefully it helps guide you. > > A network widget may also be good. What is a "network widget" in this context? Application users don't usually interact with "the network" directly, and networks are usually on the opposite end of applications from the GUI. What would your hypothetical network widget do? What cross-platform differences would it abstract/hide? What do your applications do to/with/against "the network"? From __peter__ at web.de Wed Feb 27 14:50:22 2019 From: __peter__ at web.de (Peter Otten) Date: Wed, 27 Feb 2019 20:50:22 +0100 Subject: Dictionary References: Message-ID: Phu Sam wrote: > The condition 'if not fibs.get(n):' will not work because > n = 0 > fibs.get(0) is 0 so not 0 is 1 > > Here is the modified code that works: > > fibs={0:0,1:1} > def rfib(n): > if n == 0 or n == 1: > return fibs[n] > else: > fibs[n]=rfib(n-2)+rfib(n-1) > return fibs[n] > >>>> rfib(8) > 21 >>>> fibs > {0: 0, 1: 1, 2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21} > > > Cheers, The problem with this implementation is that it recalculates the results for n > 1 over and over again. You construct that nice lookup table and then you don't use it. Compare: $ cat rfib.py fibs={0:0,1:1} def rfib(n): if n == 0 or n == 1: return fibs[n] else: fibs[n]=rfib(n-2)+rfib(n-1) return fibs[n] sfibs = {0: 0, 1: 1} def sfib(n): try: return sfibs[n] except KeyError: pass result = sfibs[n] = sfib(n-1) + sfib(n-2) return result for i in range(10): assert rfib(i) == sfib(i) $ python3 -m timeit -s 'import rfib' 'rfib.rfib(25)' 10 loops, best of 3: 102 msec per loop $ python3 -m timeit -s 'import rfib' 'rfib.sfib(25)' 1000000 loops, best of 3: 0.371 usec per loop From torriem at gmail.com Wed Feb 27 15:18:32 2019 From: torriem at gmail.com (Michael Torrie) Date: Wed, 27 Feb 2019 13:18:32 -0700 Subject: ANN: Creating GUI Applications with wxPython In-Reply-To: References: <653d5469-56ef-cef5-72a6-9dfb20da27ea@offilive.com> <57fd10d8-fe13-1371-46c1-2150cb8613ce@kynesim.co.uk> Message-ID: On 02/27/2019 10:59 AM, Igor Korot wrote: > Hopefully with the wxQt having more and more development we might have > a wxQt/Android port getting attention. I don't think Qt has any plans to support Android with the traditional Qt framework. They've instead focused on their next generation system, which is QtQuick. From maillist at schwertberger.de Wed Feb 27 15:17:01 2019 From: maillist at schwertberger.de (Dietmar Schwertberger) Date: Wed, 27 Feb 2019 21:17:01 +0100 Subject: ANN: Creating GUI Applications with wxPython In-Reply-To: References: <653d5469-56ef-cef5-72a6-9dfb20da27ea@offilive.com> Message-ID: <29447405-990b-b74c-d66e-8bf195465a04@schwertberger.de> On 27.02.2019 20:34, Dan Sommers wrote: > What is a "network widget" in this context?? Application > users don't usually interact with "the network" directly, > and networks are usually on the opposite end of applications > from the GUI.? What would your hypothetical network widget > do?? What cross-platform differences would it abstract/hide? > What do your applications do to/with/against "the network"? Probably an integration of sockets with the main loop to allow for asynchroneous communication. Qt has a socket class that will deliver events when data is received or transmitted. It's a long time that I had a look at this, but I think it's available from PyQt as well. Alternatively, I have successfully used Quamash, which is an integration of the Qt event loop with asyncio. For new projects, I would always recommend to use asyncio as this is available in the standard library. For wxPython there is some code to integrate with asyncio, but I have never used this myself. Alternatively, the communication can always be placed into a separate thread, posting events to the main application / frontend. Regards, Dietmar From roel at roelschroeven.net Wed Feb 27 16:39:05 2019 From: roel at roelschroeven.net (Roel Schroeven) Date: Wed, 27 Feb 2019 22:39:05 +0100 Subject: Lifetime of a local reference In-Reply-To: References: <8736oai5c7.fsf@elektro.pacujo.net> <86pnrd2a95.fsf@richard.bawden.org> <87y361hg7z.fsf@elektro.pacujo.net> Message-ID: Rhodri James schreef op 27/02/2019 om 15:18: > On 27/02/2019 06:56, Marko Rauhamaa wrote: >> Alan Bawden : >>> But I appreciate that that isn't the true question that you wanted to ask! >>> You are wondering if a Python implementation is _permitted_ to treat the >>> code you wrote _as if_ you had written: >>> >>> def fun(): >>> f = open("lock") >>> flock.flock(f, fcntl.LOCK_EX) >>> del f >>> do_stuff() >>> sys.exit(0) >>> >>> which deletes the variable f from the local environment at a point where it >>> will never be used again. (Which could cause the lock to be released >>> before do_stuff is even called.) >>> >>> This is an interesting question, and one that any garbage collected >>> language should probably address somehow. > > Interesting indeed. My gut instinct was "Hell, no!", but as Marko > points out C optimisers do exactly that, and I don't find it > particularly surprising in practice. I don't think that there is > anything in the docs that says a compliant implementation couldn't > delete variables early. The nearest you get is repeated reminders that > you can't make assumptions about when destructors will be run. Aren't we overthinking this? I think it's pretty clear that a variable is never deleted before it goes out of scope. A quick search in the documentation points me to (https://docs.python.org/3/reference/datamodel.html#objects-values-and-types): "Objects are never explicitly destroyed; however, when they become unreachable they may be garbage-collected. An implementation is allowed to postpone garbage collection or omit it altogether ? it is a matter of implementation quality how garbage collection is implemented, *as long as no objects are collected that are still reachable*." (emphasis mine) In the original example (without del), f is reachable everywhere in the function after the initial binding, so it can not be deleted. Comparisons with C on this point don't seem very relevant: C doesn't have destructors or garbage collection. I don't even see what the C equivalent for "del f" could be. You could perhaps compare with C++, where destructors are also not called before the object goes out of scope (a difference is that in C++ the destructor explicitly always is called at that moment). Regards, Roel -- "Honest criticism is hard to take, particularly from a relative, a friend, an acquaintance, or a stranger." -- Franklin P. Jones Roel Schroeven From tjol at tjol.eu Wed Feb 27 18:39:31 2019 From: tjol at tjol.eu (Thomas Jollans) Date: Thu, 28 Feb 2019 00:39:31 +0100 Subject: Lifetime of a local reference In-Reply-To: References: <8736oai5c7.fsf@elektro.pacujo.net> <86pnrd2a95.fsf@richard.bawden.org> <87y361hg7z.fsf@elektro.pacujo.net> Message-ID: <97cb096b-c3e0-0d9e-c6e8-abedc3f04a2f@tjol.eu> On 27/02/2019 22:39, Roel Schroeven wrote: > Aren't we overthinking this? No, it's just that nobody had found the evidence you did. > > I think it's pretty clear that a variable is never deleted before it > goes out of scope. A quick search in the documentation points me to > (https://docs.python.org/3/reference/datamodel.html#objects-values-and-types): > > > "Objects are never explicitly destroyed; however, when they become > unreachable they may be garbage-collected. An implementation is allowed > to postpone garbage collection or omit it altogether ? it is a matter of > implementation quality how garbage collection is implemented, *as long > as no objects are collected that are still reachable*." (emphasis mine) > > In the original example (without del), f is reachable everywhere in the > function after the initial binding, so it can not be deleted. Thanks! -- Thomas From jfong at ms4.hinet.net Wed Feb 27 21:09:14 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Wed, 27 Feb 2019 18:09:14 -0800 (PST) Subject: Quirk difference between classes and functions In-Reply-To: <7cadc4ec-b788-40c7-9ee3-b8f2202c4085@googlegroups.com> References: <5c7416f4$0$21614$426a74cc@news.free.fr> <7cadc4ec-b788-40c7-9ee3-b8f2202c4085@googlegroups.com> Message-ID: <8e16af7f-788e-45c3-88ff-daf26a04327c@googlegroups.com> jf... at ms4.hinet.net? 2019?2?26???? UTC+8??4?46?04???? > ast? 2019?2?26???? UTC+8??12?25?40???? > > Hello > > > > I noticed a quirk difference between classes and functions > > > > >>> x=0 > > >>> > > >>> class Test: > > x = x+1 > > print(x) > > x = x+1 > > print(x) > > > > 1 > > 2 > > >>> print(x) > > 0 > > > > Previous code doesn't generate any errors. > > x at the right of = in first "x = x+1" line is > > the global one (x=0), then x becomes local > > > > within a function, this is not allowed > > > > >>> x = 0 > > >>> > > >>> def f(): > > x = x+1 > > > > >>> f() > > UnboundLocalError: local variable 'x' referenced before assignment > > > > Since x is written inside the function, it is considered as a local > > variable and x in x+1 is undefined so this throw an exception > > > > Any comment ? > > May I say that the LEGB rule apply to run time, not compile time? No. The LEGB apply to compile time where only name and scope are involved. > > >>> x = 1 > >>> def f(): > ... print(x) > ... print(locals()) > ... > >>> f() > 1 > {} > >>> def g(): > ... print(x) > ... x = 2 > ... print(locals()) > ... > >>> g() > Traceback (most recent call last): > File "", line 1, in > File "", line 2, in g > UnboundLocalError: local variable 'x' referenced before assignment > >>> class X: > ... print(x) > ... x = 2 > ... > 1 > >>> > The print(x) difference between class and function was caused by that one was executed and the other was compiled. The LEGB rule must apply to run time to make the language dynamic. At run time, the binding between name and object can be changed. It's the word "dynamic" comes from. What happens in class X when print(x) was encountered? I have no idea how the LEGB involves there yet:-( --Jach > > Any comment:-)? > > --Jach From greg.ewing at canterbury.ac.nz Thu Feb 28 00:12:57 2019 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Thu, 28 Feb 2019 18:12:57 +1300 Subject: Lifetime of a local reference In-Reply-To: <86pnrd2a95.fsf@richard.bawden.org> References: <8736oai5c7.fsf@elektro.pacujo.net> <86pnrd2a95.fsf@richard.bawden.org> Message-ID: Alan Bawden wrote: > the Java Language > Specification contains the following language: > > Optimizing transformations of a program can be designed that reduce the > number of objects that are reachable to be less than those which would > naively be considered reachable. For example, a Java compiler or code > generator may choose to set a variable or parameter that will no longer be > used to null to cause the storage for such an object to be potentially > reclaimable sooner. However, it only makes sense to do that if the compiler can be sure that reclaiming the object can't possibly have any side effects. That's certainly not true of things like file objects that reference resources outside of the program. I'd be pretty upset if a Java implementation prematurely closed my files on the basis of this clause. Similar considerations apply to Python. Even more so, because its dynamic nature makes it next to impossible for the compiler to prove much of anything about side effects. So I wouldn't expect a Python implementation to even try to drop any references early. If it does, I would hope it darn well knows what it's doing. Summary: You don't have to worry about it. -- Greg From greg.ewing at canterbury.ac.nz Thu Feb 28 00:23:54 2019 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Thu, 28 Feb 2019 18:23:54 +1300 Subject: Lifetime of a local reference In-Reply-To: References: <8736oai5c7.fsf@elektro.pacujo.net> <86pnrd2a95.fsf@richard.bawden.org> <33442b3b-1aac-df09-84ab-a24d9ca1c7aa@tjol.eu> Message-ID: Thomas Jollans wrote: > If the inspect module's stack frame inspection machinery is supported, > then any function call might access any local... (though I don't think a > compliant Python implementation necessarily has to support the inspect > module fully). You can be devious even without using the expect module: def fun(): f = open("lock.txt", "w") do_stuff(innocent_argument) do_stuff = exec innocent_argument = "f.write('foo')" -- Greg From alan at csail.mit.edu Thu Feb 28 02:04:42 2019 From: alan at csail.mit.edu (Alan Bawden) Date: 28 Feb 2019 02:04:42 -0500 Subject: Lifetime of a local reference References: <8736oai5c7.fsf@elektro.pacujo.net> <86pnrd2a95.fsf@richard.bawden.org> Message-ID: <86k1hk1jhx.fsf@richard.bawden.org> Gregory Ewing writes: > Alan Bawden wrote: > > the Java Language > > Specification contains the following language: > > Optimizing transformations of a program can be designed that reduce > > the number of objects that are reachable to be less than those which > > would naively be considered reachable. For example, a Java compiler > > or code generator may choose to set a variable or parameter that > > will no longer be used to null to cause the storage for such an > > object to be potentially reclaimable sooner. > > However, it only makes sense to do that if the compiler can be > sure that reclaiming the object can't possibly have any side > effects. That's certainly not true of things like file objects > that reference resources outside of the program. I'd be pretty > upset if a Java implementation prematurely closed my files on > the basis of this clause. The Java compiler has no way to know whether a variable references an object with a finalize() method that has side effects, so that quote from the Specification licenses a Java implementation to do exactly the thing you say will make you upset. -- Alan Bawden From hjp-python at hjp.at Thu Feb 28 02:38:17 2019 From: hjp-python at hjp.at (Peter J. Holzer) Date: Thu, 28 Feb 2019 08:38:17 +0100 Subject: Quirk difference between classes and functions In-Reply-To: References: <3a7dd391-a1e7-4bfb-877b-5b2e3a8de946@googlegroups.com> Message-ID: <20190228073817.75ri3xxcryxq3kvp@hjp.at> On 2019-02-27 12:34:37 -0500, Dennis Lee Bieber wrote: > On Tue, 26 Feb 2019 19:15:16 -0800 (PST), jfong at ms4.hinet.net declaimed the > following: > > >So, may I say that the Python compiler is a multi-pass one? > > No... that is implementation dependent... True, but > The common Python is a byte-code compiler pass (how many is undefined) > which then feeds a byte-code INTERPRETER. He's only talking about the byte-code compiler here, not the interpreter. > The compiler pass "sees" the local assignment, and creates a run-time > data structure for the function which "holds" the local data. Yup, but in def foo(): print(bar) bar = 1 the compiler sees the local assignment AFTER the print() call. So it can't generate code to access the local variable for the print call because it doesn't yet know that bar is a local variable. The obvious way around this is a two pass compiler: In the first pass you parse the whole function and convert it into an AST. While you do this you can also collect local variables. In the second pass you convert the AST to byte-code. I think it should be possible to do it in a single pass, although at the cost of generating less efficient code: For each variable access, produce code to check for a local variable first, then a global one. Collect information on local variables at the same time. When you are done, generate the code to set up the local variables and arrange for it to be executed before any other code in the function. hp -- _ | Peter J. Holzer | we build much bigger, better disasters now |_|_) | | because we have much more sophisticated | | | hjp at hjp.at | management tools. __/ | http://www.hjp.at/ | -- Ross Anderson -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 833 bytes Desc: not available URL: From marko at pacujo.net Thu Feb 28 03:49:01 2019 From: marko at pacujo.net (Marko Rauhamaa) Date: Thu, 28 Feb 2019 10:49:01 +0200 Subject: Lifetime of a local reference References: <8736oai5c7.fsf@elektro.pacujo.net> <86pnrd2a95.fsf@richard.bawden.org> <86k1hk1jhx.fsf@richard.bawden.org> Message-ID: <87va14pabm.fsf@elektro.pacujo.net> Alan Bawden : > Gregory Ewing writes: > >> Alan Bawden wrote: >> > the Java Language >> > Specification contains the following language: >> > Optimizing transformations of a program can be designed that reduce >> > the number of objects that are reachable to be less than those which >> > would naively be considered reachable. For example, a Java compiler >> > or code generator may choose to set a variable or parameter that >> > will no longer be used to null to cause the storage for such an >> > object to be potentially reclaimable sooner. >> >> However, it only makes sense to do that if the compiler can be >> sure that reclaiming the object can't possibly have any side >> effects. That's certainly not true of things like file objects >> that reference resources outside of the program. I'd be pretty >> upset if a Java implementation prematurely closed my files on >> the basis of this clause. > > The Java compiler has no way to know whether a variable references an > object with a finalize() method that has side effects, so that quote > from the Specification licenses a Java implementation to do exactly > the thing you say will make you upset. And that's not only theoretical. Hotspot (as well as gcc on the C side) has been very aggressive in taking liberties awarded by the standards. What's trickier is that Hotspot's JIT optimizations kick in only when a code snippet is executed numerous times. So often these effects don't come up during functional testing but may make their way to production. Marko From anthony.flury at btinternet.com Thu Feb 28 04:18:22 2019 From: anthony.flury at btinternet.com (Anthony Flury) Date: Thu, 28 Feb 2019 09:18:22 +0000 Subject: Using PyArg_ParseTuple to with optional fields. Message-ID: <60be3faf-37fe-64c6-3456-f8b5718f9aa3@btinternet.com> I am trying to write an extension module with a function (actually an __init__ method, but I am not sure that matters) where the function can be called as either : ??? my_func() or ??? my_func( a, b, c, d) - where a,b,c,d are all doubles. I would prefer not to allow the function to be called with some arguments specified - it is either all four - or none. I have tried the following format strings to PyArg_ParseTuple : * "dddd" - but this a mandatory 4 doubles - doesn't allow for the no argument scenario * "|dddd" - allows no arguments - but also allows only one, or just two, or three - i.e. not all or nothing * |(dddd) - allows no argument, but passing 4 arguments expects a 4-tuple - i.e. my_func( (a,b,c,d) ) which is unnecessarily complex Can I do what I want with a single call to PyArg_ParseTuple? ? -- Anthony Flury *Email* : anthony.flury at btinternet.com *Twitter* : @TonyFlury From stefan_ml at behnel.de Thu Feb 28 04:47:54 2019 From: stefan_ml at behnel.de (Stefan Behnel) Date: Thu, 28 Feb 2019 10:47:54 +0100 Subject: Using PyArg_ParseTuple to with optional fields. In-Reply-To: <60be3faf-37fe-64c6-3456-f8b5718f9aa3@btinternet.com> References: <60be3faf-37fe-64c6-3456-f8b5718f9aa3@btinternet.com> Message-ID: Anthony Flury via Python-list schrieb am 28.02.19 um 10:18: > I am trying to write an extension module with a function (actually an > __init__ method, but I am not sure that matters) where the function can be > called as either : > > ??? my_func() > > or > > ??? my_func( a, b, c, d) - where a,b,c,d are all doubles. > > I would prefer not to allow the function to be called with some arguments > specified - it is either all four - or none. You could do the argument checking yourself, after extracting them. But (without knowing what you're actually up to here) ? are you sure the above is a good user interface? Did you consider splitting the function into two, one that takes arguments and one that takes none? Stefan From rhodri at kynesim.co.uk Thu Feb 28 07:09:59 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Thu, 28 Feb 2019 12:09:59 +0000 Subject: Lifetime of a local reference In-Reply-To: References: <8736oai5c7.fsf@elektro.pacujo.net> <86pnrd2a95.fsf@richard.bawden.org> <87y361hg7z.fsf@elektro.pacujo.net> Message-ID: On 27/02/2019 21:39, Roel Schroeven wrote: > Rhodri James schreef op 27/02/2019 om 15:18: > Aren't we overthinking this? > > I think it's pretty clear that a variable is never deleted before it > goes out of scope. A quick search in the documentation points me to > (https://docs.python.org/3/reference/datamodel.html#objects-values-and-types): > > > "Objects are never explicitly destroyed; however, when they become > unreachable they may be garbage-collected. An implementation is allowed > to postpone garbage collection or omit it altogether ? it is a matter of > implementation quality how garbage collection is implemented, *as long > as no objects are collected that are still reachable*." (emphasis mine) > > In the original example (without del), f is reachable everywhere in the > function after the initial binding, so it can not be deleted. > > Comparisons with C on this point don't seem very relevant: C doesn't > have destructors or garbage collection. I don't even see what the C > equivalent for "del f" could be. You could perhaps compare with C++, > where destructors are also not called before the object goes out of > scope (a difference is that in C++ the destructor explicitly always is > called at that moment). I think you're making the mistake of equating names ("variables") and objects here. It is not clear to me that a name can't be removed from the dictionary of locals before it goes out of scope, so long as it doesn't get referred to again, just like C optimisers can note that a local doesn't get used again and reclaim the storage. At that point the object can become unreachable, and will in Marko's example. The equivalent to "del f" that you can't imagine is the optimiser at work. -- Rhodri James *-* Kynesim Ltd From rosuav at gmail.com Thu Feb 28 07:21:36 2019 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 28 Feb 2019 23:21:36 +1100 Subject: Lifetime of a local reference In-Reply-To: References: <8736oai5c7.fsf@elektro.pacujo.net> <86pnrd2a95.fsf@richard.bawden.org> <87y361hg7z.fsf@elektro.pacujo.net> Message-ID: On Thu, Feb 28, 2019 at 11:13 PM Rhodri James wrote: > > On 27/02/2019 21:39, Roel Schroeven wrote: > > Rhodri James schreef op 27/02/2019 om 15:18: > > Aren't we overthinking this? > > > > I think it's pretty clear that a variable is never deleted before it > > goes out of scope. A quick search in the documentation points me to > > (https://docs.python.org/3/reference/datamodel.html#objects-values-and-types): > > > > > > "Objects are never explicitly destroyed; however, when they become > > unreachable they may be garbage-collected. An implementation is allowed > > to postpone garbage collection or omit it altogether ? it is a matter of > > implementation quality how garbage collection is implemented, *as long > > as no objects are collected that are still reachable*." (emphasis mine) > > > > In the original example (without del), f is reachable everywhere in the > > function after the initial binding, so it can not be deleted. > > > > Comparisons with C on this point don't seem very relevant: C doesn't > > have destructors or garbage collection. I don't even see what the C > > equivalent for "del f" could be. You could perhaps compare with C++, > > where destructors are also not called before the object goes out of > > scope (a difference is that in C++ the destructor explicitly always is > > called at that moment). > > I think you're making the mistake of equating names ("variables") and > objects here. It is not clear to me that a name can't be removed from > the dictionary of locals before it goes out of scope, so long as it > doesn't get referred to again, just like C optimisers can note that a > local doesn't get used again and reclaim the storage. At that point the > object can become unreachable, and will in Marko's example. > > The equivalent to "del f" that you can't imagine is the optimiser at work. > What if an exception gets raised at some point before the function has returned? The exception object will give full access to the function's locals. ChrisA From marko at pacujo.net Thu Feb 28 10:59:47 2019 From: marko at pacujo.net (Marko Rauhamaa) Date: Thu, 28 Feb 2019 17:59:47 +0200 Subject: Lifetime of a local reference References: <8736oai5c7.fsf@elektro.pacujo.net> <86pnrd2a95.fsf@richard.bawden.org> <87y361hg7z.fsf@elektro.pacujo.net> Message-ID: <87r2brq4y4.fsf@elektro.pacujo.net> Chris Angelico : > What if an exception gets raised at some point before the function has > returned? The exception object will give full access to the function's > locals. It wouldn't hurt for the Python gods to make an explicit ruling on the matter. Marko From arnaud at sphaero.org Thu Feb 28 06:08:56 2019 From: arnaud at sphaero.org (Arnaud Loonstra) Date: Thu, 28 Feb 2019 12:08:56 +0100 Subject: Embedding Python (3.7) on OSX crash on Py_Initialize when run as bundle Message-ID: <0b0adaa6-c084-3877-dcf6-14438fd2efd1@sphaero.org> Hi, I'm trying to wrap my head around an OSX app which has python embedded. This works fine if I run the build on the same machine as it is build on. However when using Travis to build the app I can only get it to run from the terminal. A double click on the appdir (or using open) results in a crash. trace: 0 libsystem_kernel.dylib 0x00007fffb9d6cd42 __pthread_kill + 10 1 libsystem_pthread.dylib 0x00007fffb9e5a457 pthread_kill + 90 2 libsystem_c.dylib 0x00007fffb9cd24bb __abort + 140 3 libsystem_c.dylib 0x00007fffb9cd242f abort + 144 4 com.your-company-name.www 0x0000000108c6674e fatal_error + 606 5 com.your-company-name.www 0x0000000108c65b3a _Py_FatalInitError + 26 6 com.your-company-name.www 0x0000000108c65bbe Py_Initialize + 126 I can't figure out what is going wrong. I can only guess that's there's something preventing it to access the python files? The build log on travis is here: https://travis-ci.org/hku-ect/NatNet2OSCbridge/jobs/499348594 It's artefact is here: https://pong.hku.nl/~buildbot/nightly/NatNet2OSCbridge_osx_7ceb.zip Python is build with the following flags: ./configure --prefix $HOME/openFrameworks/apps/devApps/$APPNAME/bin/python --disable-shared --with-openssl=$(brew --prefix openssl); Anybody any pointers or advice? Rg, Arnaud From none at gmail.com Thu Feb 28 11:09:17 2019 From: none at gmail.com (ast) Date: Thu, 28 Feb 2019 17:09:17 +0100 Subject: dash/underscore on name of package uploaded on pypi Message-ID: <5c7807b0$0$19262$426a34cc@news.free.fr> Hello I just uploaded a package on pypi, whose name is "arith_lib" The strange thing is that on pypi the package is renamed "arith-lib" The underscore is substitued with a dash If we search for this package: pip search arith arith-lib (2.0.0) - A set of functions for miscellaneous arithmetic (so a dash) For installation both: pip install -U arith_lib pip install -U arith-lib are working well and in both case I got a directory with an underscore C:\Program Files\Python36-32\Lib\site-packages 28/02/2019 16:57 arith_lib 28/02/2019 16:57 arith_lib-2.0.0.dist-info What happens ? From PythonList at DancesWithMice.info Thu Feb 28 13:36:25 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Fri, 1 Mar 2019 07:36:25 +1300 Subject: Using PyArg_ParseTuple to with optional fields. In-Reply-To: <60be3faf-37fe-64c6-3456-f8b5718f9aa3@btinternet.com> References: <60be3faf-37fe-64c6-3456-f8b5718f9aa3@btinternet.com> Message-ID: Anthony, On 28/02/19 10:18 PM, Anthony Flury via Python-list wrote: > I am trying to write an extension module with a function (actually an > __init__ method, but I am not sure that matters) where the function can > be called as either : > ??? my_func() > or > ??? my_func( a, b, c, d) - where a,b,c,d are all doubles. > I would prefer not to allow the function to be called with some > arguments specified - it is either all four - or none. Perhaps it does matter - __init__() only ever returns None, which may/not be of-concern at the 'top end'. During the last few days, either 'here' or over on Python-Tutor, there has been a discussion about alternate ways to instantiate an object, ie with different numbers of arguments. > I have tried the following format strings to PyArg_ParseTuple : > ?* "dddd" - but this a mandatory 4 doubles - doesn't allow for the no ...> Can I do what I want with a single call to PyArg_ParseTuple? ? +1 for using provided (and optimised) standard facilities! What advantages does PyArg provide? Has its use become 'blinkers' - a straight-jacket to your thinking? If the data is coming from outside Python directly, is it not possible to use named-keyword arguments, ie pick-up the 'optional' auto-magically? Somewhere in the existing code there's probably already some coding for the 'empty args' option, inserting default values... How about *args (or *kwargs)? Remember that the "args" (sans asterisk) becomes a tuple inside the method. It can be empty or filled. That leaves you with two tests: len() zero or four, and (optionally) a loop to check that the four arg-tuple elements are all() floats. -- Regards =dn From p.f.moore at gmail.com Thu Feb 28 14:18:31 2019 From: p.f.moore at gmail.com (Paul Moore) Date: Thu, 28 Feb 2019 19:18:31 +0000 Subject: dash/underscore on name of package uploaded on pypi In-Reply-To: <5c7807b0$0$19262$426a34cc@news.free.fr> References: <5c7807b0$0$19262$426a34cc@news.free.fr> Message-ID: On Thu, 28 Feb 2019 at 16:14, ast wrote: > > Hello > > I just uploaded a package on pypi, whose name is "arith_lib" > > The strange thing is that on pypi the package is renamed "arith-lib" > The underscore is substitued with a dash The version with a dash is the normalised form of the name - see https://www.python.org/dev/peps/pep-0503/#normalized-names Paul From mihir.kothari at gmail.com Thu Feb 28 11:19:27 2019 From: mihir.kothari at gmail.com (Mihir Kothari) Date: Thu, 28 Feb 2019 11:19:27 -0500 Subject: Need help with Python3.4 and Avro Message-ID: Hi, I am not sure if this is the right list, but trying to get help from experienced python users for my issue. I have posted the issue in stackoverflow, but yet to see any response. I am trying my luck here. I have my question here: https://stackoverflow.com/q/54833834/3165644 If needed, I can give the details in this email too, please let me know. Regards, Mihir. From roel at roelschroeven.net Thu Feb 28 15:23:41 2019 From: roel at roelschroeven.net (Roel Schroeven) Date: Thu, 28 Feb 2019 21:23:41 +0100 Subject: Lifetime of a local reference In-Reply-To: References: <8736oai5c7.fsf@elektro.pacujo.net> <86pnrd2a95.fsf@richard.bawden.org> <87y361hg7z.fsf@elektro.pacujo.net> Message-ID: Rhodri James schreef op 28/02/2019 om 13:09: > On 27/02/2019 21:39, Roel Schroeven wrote: >> Rhodri James schreef op 27/02/2019 om 15:18: >> Aren't we overthinking this? >> >> I think it's pretty clear that a variable is never deleted before it >> goes out of scope. A quick search in the documentation points me to >> (https://docs.python.org/3/reference/datamodel.html#objects-values-and-types): >> >> >> "Objects are never explicitly destroyed; however, when they become >> unreachable they may be garbage-collected. An implementation is allowed >> to postpone garbage collection or omit it altogether ? it is a matter of >> implementation quality how garbage collection is implemented, *as long >> as no objects are collected that are still reachable*." (emphasis mine) >> >> In the original example (without del), f is reachable everywhere in the >> function after the initial binding, so it can not be deleted. >> >> Comparisons with C on this point don't seem very relevant: C doesn't >> have destructors or garbage collection. I don't even see what the C >> equivalent for "del f" could be. You could perhaps compare with C++, >> where destructors are also not called before the object goes out of >> scope (a difference is that in C++ the destructor explicitly always is >> called at that moment). > > I think you're making the mistake of equating names ("variables") and > objects here. Yes, on second reading that quote from the documentation is not very relevant. > It is not clear to me that a name can't be removed from > the dictionary of locals before it goes out of scope, so long as it > doesn't get referred to again, It is said that an assignment binds a name to an object in the current local namespace (if there is no 'global' or 'nonlocal'). It is said that deletion (the del statement) of a name removes the binding of that name from the local namespace (again, if there is no 'global' or 'nonlocal'). In the absence of any other mention of bindings being removed, to me it seems clear that bindings are not automatically removed. Otherwise many things become ambiguous. Example: the documentation for dicts defines "d[key] = value" as "Set d[key] to value". Does that mean it can get unset later behind your back? Of course not. > just like C optimisers can note that a > local doesn't get used again and reclaim the storage. At that point the > object can become unreachable, and will in Marko's example. > The equivalent to "del f" that you can't imagine is the optimiser at work. But note that reclaiming storage in C has no side effects. No locks are released, no files are closed. No destructor is run, since C has no such thing. It's purely an implementation detail. As long as the visible behavior of the program complies with the language specification, the compiler can do as it wishes. In C's relative C++, variables going out of scope *do* have side effects, and there it *is* guaranteed that that only happes at the end of the scope (however the compiler sees fit to implement that). (Also note that the concept of "unreachable" does not exist in C; C does not have names and objects like Python). Python's "del" on the other hand most certainly has a visible effect. It is not purely an implementation detail. -- "Honest criticism is hard to take, particularly from a relative, a friend, an acquaintance, or a stranger." -- Franklin P. Jones Roel Schroeven From tjreedy at udel.edu Thu Feb 28 15:47:18 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 28 Feb 2019 15:47:18 -0500 Subject: Need help with Python3.4 and Avro In-Reply-To: References: Message-ID: On 2/28/2019 11:19 AM, Mihir Kothari wrote: > I am not sure if this is the right list, but trying to get help from > experienced python users for my issue. This is a good place to get a somewhat different set of such people. > I have posted the issue in stackoverflow, but yet to see any response. I am > trying my luck here. > > I have my question here: https://stackoverflow.com/q/54833834/3165644 You should edit the title and body of your SO question. Titles should get the attention of people who might be able to answer, and not push them away. Including '3.4' in the title implies that specific practical knowledge of 3.4 is essential. The fraction of 'experienced Python users' using 3.4 is around .01 You can mention in the body that you are currently using 3.4, but I do not expect that this is completely essential to your problem, or an answer. An answer given for 3.7, say, should very likely run on 3.4, or be modifiable to do. In the title, I would change 'Avro' to the more specific 'Apache Avro'. Copy and paste the complete traceback in the body. Format it as code. > If needed, I can give the details in this email too, please let me know. I believe most people consider stackoverflow to be a safe link to click on. However, a brief summary would more likely get such a click. "When trying to convert an Avro schema to JSON, I get AvroError(sp?): Tha datum is not the example of schema" ['AvroError is hypothesized since not given. The spelling error indicates that the message was re-typed instead of pasted.] -- Terry Jan Reedy From tjreedy at udel.edu Thu Feb 28 16:07:36 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Thu, 28 Feb 2019 16:07:36 -0500 Subject: dash/underscore on name of package uploaded on pypi In-Reply-To: <5c7807b0$0$19262$426a34cc@news.free.fr> References: <5c7807b0$0$19262$426a34cc@news.free.fr> Message-ID: On 2/28/2019 11:09 AM, ast wrote: > Hello > > I just uploaded a package on pypi, whose name is "arith_lib" > > The strange thing is that on pypi the package is renamed "arith-lib" > The underscore is substitued with a dash > > If we search for this package: > > pip search arith > > arith-lib (2.0.0) - A set of functions for miscellaneous arithmetic > (so a dash) > > For installation both: > > pip install -U arith_lib > pip install -U arith-lib > > are working well > > and in both case I got a directory with an underscore > > C:\Program Files\Python36-32\Lib\site-packages > > 28/02/2019? 16:57??? ????????? arith_lib > 28/02/2019? 16:57??? ????????? arith_lib-2.0.0.dist-info > > What happens ? To expand on Paul's answer. English uses '-' both as a connector for compound names and as a subtraction operator. Context usually makes the choice obvious. But context-free parsers must choose just one, and for computation, subtraction wins. 'arith-lib' is parsed as (arith) - (lib). Many algorithm languages use '_' instead of '-' as the compounder for identifiers (object names). In addition, Python uses filenames -(minus) '.py' as identifiers for imported modules. So if the repository allows '-' in package names, installers must convert '-' to '_'. But if the repository allows 'arith_lib' and 'arith-lib' to be distinct names for different packages, both would be installed with the same file name. So the repository standardizes on one form, and it went with English instead of Pythonese. -- Terry Jan Reedy From marko at pacujo.net Thu Feb 28 17:25:01 2019 From: marko at pacujo.net (Marko Rauhamaa) Date: Fri, 01 Mar 2019 00:25:01 +0200 Subject: Lifetime of a local reference References: <8736oai5c7.fsf@elektro.pacujo.net> <86pnrd2a95.fsf@richard.bawden.org> <87y361hg7z.fsf@elektro.pacujo.net> Message-ID: <8736o7h7pe.fsf@elektro.pacujo.net> Roel Schroeven : > In the absence of any other mention of bindings being removed, to me > it seems clear that bindings are not automatically removed. Otherwise > many things become ambiguous. Example: the documentation for dicts > defines "d[key] = value" as "Set d[key] to value". Does that mean it > can get unset later behind your back? Of course not. Nothing is so obvious here. A Python implementation is free to do anything at all that complies with correct semantics. Sometimes correct behavior is unspecified. In your case, "d[key] = value", the binding can disappear if the name d becomes "stale". d[key] will be bound if anybody should look but if the compiler or runtime environment can prove nobody will every look, it might be able to optimize it away. If you accept that open("xyz") can silently close the file immediately, sometime later or never, you should have no problem believing that f = open("xyz") could behave the same way. > It's purely an implementation detail. As long as the visible behavior > of the program complies with the language specification, the compiler > can do as it wishes. And that's the key: what is specified for Python? It would appear nothing has been stated explicitly so it would be dangerous for a Python application to rely on assumed semantics. Marko From rosuav at gmail.com Thu Feb 28 18:01:02 2019 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 1 Mar 2019 10:01:02 +1100 Subject: Lifetime of a local reference In-Reply-To: <8736o7h7pe.fsf@elektro.pacujo.net> References: <8736oai5c7.fsf@elektro.pacujo.net> <86pnrd2a95.fsf@richard.bawden.org> <87y361hg7z.fsf@elektro.pacujo.net> <8736o7h7pe.fsf@elektro.pacujo.net> Message-ID: On Fri, Mar 1, 2019 at 9:31 AM Marko Rauhamaa wrote: > > Roel Schroeven : > > In the absence of any other mention of bindings being removed, to me > > it seems clear that bindings are not automatically removed. Otherwise > > many things become ambiguous. Example: the documentation for dicts > > defines "d[key] = value" as "Set d[key] to value". Does that mean it > > can get unset later behind your back? Of course not. > > Nothing is so obvious here. > > A Python implementation is free to do anything at all that complies with > correct semantics. Sometimes correct behavior is unspecified. > > In your case, "d[key] = value", the binding can disappear if the name d > becomes "stale". d[key] will be bound if anybody should look but if the > compiler or runtime environment can prove nobody will every look, it > might be able to optimize it away. The point was that so long as d exists, so does d[key]. There is no way for d[key] to legitimately become "stale" and expired. There is also no way for a function local name to become "stale" while it's running. They are attributes of the function's context. > If you accept that > > open("xyz") > > can silently close the file immediately, sometime later or never, you > should have no problem believing that > > f = open("xyz") > > could behave the same way. No, because 'f' is a name. > > It's purely an implementation detail. As long as the visible behavior > > of the program complies with the language specification, the compiler > > can do as it wishes. > > And that's the key: what is specified for Python? It would appear > nothing has been stated explicitly so it would be dangerous for a Python > application to rely on assumed semantics. Not true. ChrisA From anthony.flury at btinternet.com Thu Feb 28 20:19:04 2019 From: anthony.flury at btinternet.com (Anthony Flury) Date: Fri, 1 Mar 2019 01:19:04 +0000 Subject: Extension Module for Python 3.6 + Message-ID: <5b2bcbc6-e5e4-bd56-ab99-1a2eb1d57856@btinternet.com> In my brave and noble quest to get to grips with the CAPI - I am trying to write a C extension module which provides a new class The exact details are not important, but what is important is that instances of my new class are imutable, and therefore from time to time, my extension module needs to build a new instance of my extension class. The documentation is pretty sparse here, and suggests using PyObject_New & PyObject_Init but gives no examples. An internet search suggests using PyObject_CallObject in some way - but the examples here are call backs to Python functions. What is the canonical way to create New Instances of the Extension Type from within the Extension Module - even if someone can point me to a working example that would be great. Another option would be to call the extension class new and init functions directly - is there a reason that is not allowed ? PS - I think I also need to use Py_BuildValue to 'encapsulate' the initial arguments for the new instance - or could I just build a 'blank' instance and then directly set the fields as neccessary ? -- Anthony Flury *Email* : anthony.flury at btinternet.com *Twitter* : @TonyFlury