Re: help me in a program in python to implement Railway Reservation System using file handling technique.
On Saturday, 24 November 2018 14:33:29 UTC+8, jasmin amrutia wrote: > hello all, > please hepl me in the above program. python to implement Railway Reservation > System using file handling technique. > > System should perform below operations. > a. Reserve a ticket for a passenger. > b. List information all reservations done for today’s trains. You could get better response by offering a reward or payment. -- https://mail.python.org/mailman/listinfo/python-list
how to match list members in py3.x
for each word in each line how can we check to see if a word is already present in a list and if it is not how to append that word to a new list -- https://mail.python.org/mailman/listinfo/python-list
Re: how to match list members in py3.x
On Sun, 25 Nov 2018 07:43:42 -0800, Muhammad Rizwan wrote: > for each word in each line how can we check to see if a word is already > present in a list and if it is not how to append that word to a new list the 1st step is to stay awake in class & listen to your instructor, then try to apply the principle involved We don't do homework for you when you have produced a program of you own (not necessarily working) if you post it here then we will be more than happy to provide constructive criticism -- "Show business is just like high school, except you get paid." - Martin Mull -- https://mail.python.org/mailman/listinfo/python-list
Re: how to match list members in py3.x
IF YOU CAN'T HELP BETTER IGNORE THE POST AND DON'T TRY TO BE A SMART ASS. -- https://mail.python.org/mailman/listinfo/python-list
Re: how to match list members in py3.x
On Sun, 2018-11-25 at 07:43 -0800, Muhammad Rizwan wrote: > for each word in each line how can we check to see if a word is already > present in a list and if it is not how to append that word to a new list For your problem consider a set. https://en.wikipedia.org/wiki/Set_theory For the python syntax consider the py3 tutorial. https://docs.python.org/3/tutorial/index.html HTH -- https://mail.python.org/mailman/listinfo/python-list
Re: how to match list members in py3.x
On Sun, Nov 25, 2018 at 11:36 AM Muhammad Rizwan wrote: > > IF YOU CAN'T HELP BETTER IGNORE THE POST AND DON'T TRY TO BE A SMART ASS. > > > -- > https://mail.python.org/mailman/listinfo/python-list That isn't a productive way to get assistance. It is true that people here are often very knowledgeable to help with python problems, but as a rule, doing someone's homework isn't something people like to do. On to your problem. You should understand lists and sets, and for loops. Try some code and come back with what you have. Then you will get help to go further -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays -- https://mail.python.org/mailman/listinfo/python-list
Re: how to match list members in py3.x
Thank you -- https://mail.python.org/mailman/listinfo/python-list
Re: how to match list members in py3.x
On Sunday, 25 November 2018 11:41:56 UTC-5, Joel Goldstick wrote: > On Sun, Nov 25, 2018 at 11:36 AM Muhammad Rizwan > <> wrote: > > > > IF YOU CAN'T HELP BETTER IGNORE THE POST AND DON'T TRY TO BE A SMART ASS. > > > > > > -- > > https://mail.python.org/mailman/listinfo/python-list > > That isn't a productive way to get assistance. It is true that > people here are often very knowledgeable to help with python problems, > but as a rule, doing someone's homework isn't something people like > to do. > > On to your problem. You should understand lists and sets, and for > loops. Try some code and come back with what you have. Then you will > get help to go further > > -- > Joel Goldstick > http://joelgoldstick.com/blog > http://cc-baseballstats.info/stats/birthdays I have been trying since morning but it doesn't work. -- https://mail.python.org/mailman/listinfo/python-list
Re: how to match list members in py3.x
On Sunday, 25 November 2018 10:59:46 UTC-5, Alister wrote: > On Sun, 25 Nov 2018 07:43:42 -0800, Muhammad Rizwan wrote: > > > for each word in each line how can we check to see if a word is already > > present in a list and if it is not how to append that word to a new list > > the 1st step is to stay awake in class & listen to your instructor, then > try to apply the principle involved > > We don't do homework for you > > when you have produced a program of you own (not necessarily working) if > you post it here then we will be more than happy to provide constructive > criticism > > > > > -- > "Show business is just like high school, except you get paid." > - Martin Mull IF YOU CAN'T HELP BETTER IGNORE THE POST AND DON'T TRY TO BE A SMART ASS. -- https://mail.python.org/mailman/listinfo/python-list
Re: how to match list members in py3.x
On 25/11/2018 17:30, Muhammad Rizwan wrote: > IF YOU CAN'T HELP BETTER IGNORE THE POST AND DON'T TRY TO BE A SMART ASS. > > Why would anyone want to help you? -- https://mail.python.org/mailman/listinfo/python-list
Issue in using "subprocess.Popen" for parsing the command output
Dear Python Experts Team, As am newbie still learning the python syntax from past 2 weeks, Excuse me, If this might be silly question, As I am trying to execute shell command (ie, nmcli) using "subprocess.Popen". 1. Am trying to improve the below code with "try" and "exception", could you please help me how "try" and "exception" can be used on the below code snippet. I hope in my code with try and exception, seems to be a bug. 2. As I am trying to execute shell commands using "subprocess.Popen", I am trying to parse the strings output by "cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)" command as below, but it is throwing the below error as shown in "Output error logs:" Could you please let me to fix the bug in the below code snippet, where I need the collect the strings of the command output and later how to be parsed after execution of the command for example, I need to parse the string "Connection activation failed: " and compare it with the command output, could you please help me how this can be achieved? *Command:* :~$ nmcli device wifi connect 'Apartment 18' password '40672958689850014685abcdf' Error: Connection activation failed: (7) Secrets were required, but not provided. :~$ *Code:* *import sys* *import subprocess* *interface = "wlan0"* *def main(ssid, pw):* *# cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)* *#* *# proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)* *# stdout, stderr = proc.communicate()* *# retcode = proc.returncode* *#* *# print("printing stdout!!", stdout)* *# print("printing retcode!!", retcode)* *try:* *cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)* *proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)* *stdout, stderr = proc.communicate()* *retcode = proc.returncode* *print("printing stdout!!", stdout)* *print("printing retcode!!", retcode)* *except subprocess.CalledProcessError as e:* *s = """While executing '{}' something went wrong.* *Return code == '{}'* *Return output:\n'{}'* *""".format(cmd, e.returncode, e.output, shell=enable_shell)* *raise AssertionError(s)* *return proc.strip().decode("utf-8")* *main("Apartment 18", "40672958689850014685")* *Output error logs:* /home/srinivasan/Downloads/wifidisconnectissuenov23/qa/venv/bin/python /home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py Traceback (most recent call last): File "/home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py", line 38, in printing stdout!! printing retcode!! 0 main("Apartment 18", "40672958689850014685") File "/home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py", line 36, in main return proc.strip().decode("utf-8") AttributeError: 'Popen' object has no attribute 'strip' Process finished with exit code 1 Kindly do the needful as am stuck with this issue from 2 days Many Thanks in advance, -- https://mail.python.org/mailman/listinfo/python-list
Re: how to match list members in py3.x
On Sun, Nov 25, 2018 at 11:51 AM Muhammad Rizwan wrote: > > On Sunday, 25 November 2018 11:41:56 UTC-5, Joel Goldstick wrote: > > On Sun, Nov 25, 2018 at 11:36 AM Muhammad Rizwan > > <> wrote: > > > > > > IF YOU CAN'T HELP BETTER IGNORE THE POST AND DON'T TRY TO BE A SMART ASS. > > > > > > > > > -- > > > https://mail.python.org/mailman/listinfo/python-list > > > > That isn't a productive way to get assistance. It is true that > > people here are often very knowledgeable to help with python problems, > > but as a rule, doing someone's homework isn't something people like > > to do. > > > > On to your problem. You should understand lists and sets, and for > > loops. Try some code and come back with what you have. Then you will > > get help to go further > > > > -- > > Joel Goldstick > > http://joelgoldstick.com/blog > > http://cc-baseballstats.info/stats/birthdays > > I have been trying since morning but it doesn't work. The way to use this mailing list is to explain your problem, which you have done. Then, show the code you have written with the results it produces. Its good to include the operating system you are using (windows, linux, osx), and if your code produces a traceback, copy and paste it here. Its common to be frustrated learning to write and writing code, but if you want help you have to show something for others to help you with > -- > https://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays -- https://mail.python.org/mailman/listinfo/python-list
Re: how to match list members in py3.x
On Sunday, 25 November 2018 12:14:37 UTC-5, Joel Goldstick wrote: > On Sun, Nov 25, 2018 at 11:51 AM Muhammad Rizwan > <> wrote: > > > > On Sunday, 25 November 2018 11:41:56 UTC-5, Joel Goldstick wrote: > > > On Sun, Nov 25, 2018 at 11:36 AM Muhammad Rizwan > > > <> wrote: > > > > > > > > IF YOU CAN'T HELP BETTER IGNORE THE POST AND DON'T TRY TO BE A SMART > > > > ASS. > > > > > > > > > > > > -- > > > > https://mail.python.org/mailman/listinfo/python-list > > > > > > That isn't a productive way to get assistance. It is true that > > > people here are often very knowledgeable to help with python problems, > > > but as a rule, doing someone's homework isn't something people like > > > to do. > > > > > > On to your problem. You should understand lists and sets, and for > > > loops. Try some code and come back with what you have. Then you will > > > get help to go further > > > > > > -- > > > Joel Goldstick > > > http://joelgoldstick.com/blog > > > http://cc-baseballstats.info/stats/birthdays > > > > I have been trying since morning but it doesn't work. > > The way to use this mailing list is to explain your problem, which you > have done. Then, show the code you have written with the results it > produces. Its good to include the operating system you are using > (windows, linux, osx), and if your code produces a traceback, copy and > paste it here. > > Its common to be frustrated learning to write and writing code, but if > you want help you have to show something for others to help you with > > -- > > https://mail.python.org/mailman/listinfo/python-list > > > > -- > Joel Goldstick > http://joelgoldstick.com/blog > http://cc-baseballstats.info/stats/birthdays Below is my approach which I am sure is wrong somewhere and it doesn't work the way expected, the append doesn't do anything in my code nor does it returns any traceback all i see is a empty list printed at the end, I am using win10 x64 python 3.x atom-editor. Please take a look if you can tell me what am I doing wrong here. I want to check each list word by word if the word is not repeated in the same list it should be appended to a 'newlist'. Thanks in advance handle = open('file.txt') newlist = list() for line in handle: #line.rstrip() linewords = line.split() print(linewords) for word in linewords: newlist.append(word) if word not in linewords: continue print('new: ',newlist) -- https://mail.python.org/mailman/listinfo/python-list
Re: Issue in using "subprocess.Popen" for parsing the command output
On Sun, 25 Nov 2018 22:43:10 +0530, srinivasan wrote: > Dear Python Experts Team, > > As am newbie still learning the python syntax from past 2 weeks, Excuse me, > If this might be silly question, As I am trying to execute shell command > (ie, nmcli) using "subprocess.Popen". > > *return proc.strip().decode("utf-8")* return proc.decode("utf-8").strip() -- GNU/Linux user #557453 The cow died so I don't need your bull! -- https://mail.python.org/mailman/listinfo/python-list
Re: how to match list members in py3.x
On 11/25/18 12:28 PM, Muhammad Rizwan wrote: > Below is my approach which I am sure is wrong somewhere and it doesn't work > the way expected, the append doesn't do anything in my code nor does it > returns any traceback all i see is a empty list printed at the end, I am > using win10 x64 python 3.x atom-editor. Please take a look if you can tell me > what am I doing wrong here. I want to check each list word by word if the > word is not repeated in the same list it should be appended to a 'newlist'. > Thanks in advance > > handle = open('file.txt') > newlist = list() > for line in handle: > #line.rstrip() > linewords = line.split() > print(linewords) > > for word in linewords: > newlist.append(word) > if word not in linewords: > continue > > > > print('new: ',newlist) The first thing I note in looking at this is that the second loop will run AFTER the first loop completes, not for each step of the loop, because you outdented the code. Also, you unconditionally add the word, and THEN check if it is in the list. -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list
Aw: Issue in using "subprocess.Popen" for parsing the command output
> Kindly do the needful as am stuck with this issue from 2 days How about YOU do the needful and post code that can actually be tested. Karsten -- https://mail.python.org/mailman/listinfo/python-list
Re: help me in a program in python to implement Railway Reservation System using file handling technique.
On Nov 24, 2018 1:35 AM, wrote: > > hello all, > please hepl me in the above program. What do you mean by "the above program"? I don't see any. python to implement Railway Reservation System using file handling technique. > > System should perform below operations. > a. Reserve a ticket for a passenger. > b. List information all reservations done for today’s trains. > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Issue in using "subprocess.Popen" for parsing the command output
On 2018-11-25 17:13, srinivasan wrote: Dear Python Experts Team, As am newbie still learning the python syntax from past 2 weeks, Excuse me, If this might be silly question, As I am trying to execute shell command (ie, nmcli) using "subprocess.Popen". 1. Am trying to improve the below code with "try" and "exception", could you please help me how "try" and "exception" can be used on the below code snippet. I hope in my code with try and exception, seems to be a bug. 2. As I am trying to execute shell commands using "subprocess.Popen", I am trying to parse the strings output by "cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)" command as below, but it is throwing the below error as shown in "Output error logs:" Could you please let me to fix the bug in the below code snippet, where I need the collect the strings of the command output and later how to be parsed after execution of the command for example, I need to parse the string "Connection activation failed: " and compare it with the command output, could you please help me how this can be achieved? *Command:* :~$ nmcli device wifi connect 'Apartment 18' password '40672958689850014685abcdf' Error: Connection activation failed: (7) Secrets were required, but not provided. :~$ *Code:* *import sys* *import subprocess* *interface = "wlan0"* *def main(ssid, pw):* *# cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)* *#* *# proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)* *# stdout, stderr = proc.communicate()* *# retcode = proc.returncode* *#* *# print("printing stdout!!", stdout)* *# print("printing retcode!!", retcode)* *try:* *cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)* *proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True)* *stdout, stderr = proc.communicate()* *retcode = proc.returncode* *print("printing stdout!!", stdout)* *print("printing retcode!!", retcode)* *except subprocess.CalledProcessError as e:* *s = """While executing '{}' something went wrong.* *Return code == '{}'* *Return output:\n'{}'* *""".format(cmd, e.returncode, e.output, shell=enable_shell)* *raise AssertionError(s)* *return proc.strip().decode("utf-8")* *main("Apartment 18", "40672958689850014685")* *Output error logs:* /home/srinivasan/Downloads/wifidisconnectissuenov23/qa/venv/bin/python /home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py Traceback (most recent call last): File "/home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py", line 38, in printing stdout!! printing retcode!! 0 main("Apartment 18", "40672958689850014685") File "/home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py", line 36, in main return proc.strip().decode("utf-8") AttributeError: 'Popen' object has no attribute 'strip' Process finished with exit code 1 Kindly do the needful as am stuck with this issue from 2 days Many Thanks in advance, Look carefully at the traceback. It's actually telling you what the problem is. You're trying to do .strip() on proc, but proc is the process itself. Instead, what you want is to do .strip() on the string that it output. -- https://mail.python.org/mailman/listinfo/python-list
Re: Issue in using "subprocess.Popen" for parsing the command output
Hope now I have changed on the string output as below, could you please correct me if am still wrong? import sys import subprocess interface = "wlan0" def main(ssid, pw): try: cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw) proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True) stdout, stderr = proc.communicate() retcode = proc.returncode print("printing stdout!!", stdout) print("printing retcode!!", retcode) except subprocess.CalledProcessError as e: s = """While executing '{}' something went wrong. Return code == '{}' Return output:\n'{}' """.format(cmd, e.returncode, e.output, shell=True) raise AssertionError(s) #return proc.strip().decode("utf-8") *return proc.decode("utf-8").strip()* main("Apartment 18", "40672958689850014685ad") Error: /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/venv/bin/python /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py Traceback (most recent call last): File "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", line 30, in main("Apartment 18", "40672958689850014685") * File "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", line 28, in main* *return proc.decode("utf-8").strip()* *AttributeError: 'Popen' object has no attribute 'decode'* printing stdout!! printing retcode!! 0 Process finished with exit code 1 On Sun, Nov 25, 2018 at 11:19 PM MRAB wrote: > On 2018-11-25 17:13, srinivasan wrote: > > Dear Python Experts Team, > > > > As am newbie still learning the python syntax from past 2 weeks, Excuse > me, > > If this might be silly question, As I am trying to execute shell command > > (ie, nmcli) using "subprocess.Popen". > > > > 1. Am trying to improve the below code with "try" and "exception", could > > you please help me how "try" and "exception" can be used on the below > code > > snippet. I hope in my code with try and exception, seems to be a bug. > > > > 2. As I am trying to execute shell commands using "subprocess.Popen", I > am > > trying to parse the strings output by "cmd = "nmcli device wifi connect > > '%s' password '%s'" % (ssid, pw)" command as below, but it is throwing > the > > below error as shown in "Output error logs:" > > > > Could you please let me to fix the bug in the below code snippet, > where I > > need the collect the strings of the command output and later how to be > > parsed after execution of the command for example, I need to parse the > > string "Connection activation failed: " and compare it with the command > > output, could you please help me how this can be achieved? > > > > *Command:* > > :~$ nmcli device wifi connect 'Apartment 18' password > > '40672958689850014685abcdf' > > Error: Connection activation failed: (7) Secrets were required, but not > > provided. > > :~$ > > > > *Code:* > > *import sys* > > *import subprocess* > > > > *interface = "wlan0"* > > > > > > *def main(ssid, pw):* > > > > *# cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw)* > > *#* > > *# proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, > > stderr=subprocess.PIPE, shell=True, universal_newlines=True)* > > *# stdout, stderr = proc.communicate()* > > *# retcode = proc.returncode* > > *#* > > *# print("printing stdout!!", stdout)* > > *# print("printing retcode!!", retcode)* > > > > *try:* > > *cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, > pw)* > > > > *proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, > > stderr=subprocess.PIPE, shell=True, universal_newlines=True)* > > *stdout, stderr = proc.communicate()* > > *retcode = proc.returncode* > > > > *print("printing stdout!!", stdout)* > > *print("printing retcode!!", retcode)* > > > > *except subprocess.CalledProcessError as e:* > > *s = """While executing '{}' something went wrong.* > > *Return code == '{}'* > > *Return output:\n'{}'* > > *""".format(cmd, e.returncode, e.output, > > shell=enable_shell)* > > *raise AssertionError(s)* > > > > *return proc.strip().decode("utf-8")* > > > > *main("Apartment 18", "40672958689850014685")* > > > > *Output error logs:* > > > > /home/srinivasan/Downloads/wifidisconnectissuenov23/qa/venv/bin/python > > > /home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py > > Traceback (most recent call last): > >File > > > "/home/srinivasan/Downloads/wifidisconnectissuenov23/qa/test_library/test4.py", > > line 38, in > > printing stdout!! > > printing retcode!! 0 > > main("Apartmen
Python2.7 unicode conundrum
Hi folks, what semmingly started out as a weird database character encoding mix-up could be boiled down to a few lines of pure Python. The source-code below is real utf8 (as evidenced by the UTF code point 'c3 a4' in the third line of the hexdump). When just printed, the string "s" is displayed correctly as 'ä' (a umlaut), but the string representation shows that it seems to have been converted to latin-1 'e4' somewhere on the way. How can this be avoided? dh@jenna:~/python$ cat unicode.py # -*- encoding: utf8 -*- s = u'ä' print(s) print((s, )) dh@jenna:~/python$ hd unicode.py 23 20 2d 2a 2d 20 65 6e 63 6f 64 69 6e 67 3a 20 |# -*- encoding: | 0010 75 74 66 38 20 2d 2a 2d 0a 0a 73 20 3d 20 75 27 |utf8 -*-..s = u'| 0020 c3 a4 27 0a 0a 70 72 69 6e 74 28 73 29 0a 70 72 |..'..print(s).pr| 0030 69 6e 74 28 28 73 2c 20 29 29 0a 0a |int((s,))..| 003c dh@jenna:~/python$ python unicode.py ä (u'\xe4',) dh@jenna:~/python$ -- https://mail.python.org/mailman/listinfo/python-list
Re: Issue in using "subprocess.Popen" for parsing the command output
Even only with "*proc.decode("utf-8")"* in the above code still it seems to throw the error #return proc.strip().decode("utf-8") #return proc.decode("utf-8").strip() *return proc.decode("utf-8")* Error: /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/venv/bin/python /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py printing stdout!! printing retcode!! 0 Traceback (most recent call last): File "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", line 31, in main("Apartment 18", "40672958689850014685") * File "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", line 29, in main* *return proc.decode("utf-8")* *AttributeError: 'Popen' object has no attribute 'decode'* Process finished with exit code 1 On Sun, Nov 25, 2018 at 11:24 PM srinivasan wrote: > Hope now I have changed on the string output as below, could you please > correct me if am still wrong? > > import sys > import subprocess > > interface = "wlan0" > > > def main(ssid, pw): > > try: > cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw) > > proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, > stderr=subprocess.PIPE, shell=True, universal_newlines=True) > stdout, stderr = proc.communicate() > retcode = proc.returncode > > print("printing stdout!!", stdout) > print("printing retcode!!", retcode) > > except subprocess.CalledProcessError as e: > s = """While executing '{}' something went wrong. > Return code == '{}' > Return output:\n'{}' > """.format(cmd, e.returncode, e.output, shell=True) > raise AssertionError(s) > > #return proc.strip().decode("utf-8") > *return proc.decode("utf-8").strip()* > > main("Apartment 18", "40672958689850014685ad") > > Error: > > /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/venv/bin/python > /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py > Traceback (most recent call last): > File > "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", > line 30, in > main("Apartment 18", "40672958689850014685") > * File > "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", > line 28, in main* > *return proc.decode("utf-8").strip()* > *AttributeError: 'Popen' object has no attribute 'decode'* > printing stdout!! > printing retcode!! 0 > > Process finished with exit code 1 > > > > On Sun, Nov 25, 2018 at 11:19 PM MRAB wrote: > >> On 2018-11-25 17:13, srinivasan wrote: >> > Dear Python Experts Team, >> > >> > As am newbie still learning the python syntax from past 2 weeks, Excuse >> me, >> > If this might be silly question, As I am trying to execute shell command >> > (ie, nmcli) using "subprocess.Popen". >> > >> > 1. Am trying to improve the below code with "try" and "exception", could >> > you please help me how "try" and "exception" can be used on the below >> code >> > snippet. I hope in my code with try and exception, seems to be a bug. >> > >> > 2. As I am trying to execute shell commands using "subprocess.Popen", I >> am >> > trying to parse the strings output by "cmd = "nmcli device wifi connect >> > '%s' password '%s'" % (ssid, pw)" command as below, but it is throwing >> the >> > below error as shown in "Output error logs:" >> > >> > Could you please let me to fix the bug in the below code snippet, >> where I >> > need the collect the strings of the command output and later how to be >> > parsed after execution of the command for example, I need to parse the >> > string "Connection activation failed: " and compare it with the command >> > output, could you please help me how this can be achieved? >> > >> > *Command:* >> > :~$ nmcli device wifi connect 'Apartment 18' password >> > '40672958689850014685abcdf' >> > Error: Connection activation failed: (7) Secrets were required, but not >> > provided. >> > :~$ >> > >> > *Code:* >> > *import sys* >> > *import subprocess* >> > >> > *interface = "wlan0"* >> > >> > >> > *def main(ssid, pw):* >> > >> > *# cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, >> pw)* >> > *#* >> > *# proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, >> > stderr=subprocess.PIPE, shell=True, universal_newlines=True)* >> > *# stdout, stderr = proc.communicate()* >> > *# retcode = proc.returncode* >> > *#* >> > *# print("printing stdout!!", stdout)* >> > *# print("printing retcode!!", retcode)* >> > >> > *try:* >> > *cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, >> pw)* >> > >> > *proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, >> > stderr=subprocess.PIPE, shell=True, universal_newlines=True)* >> > *s
Re: Python2.7 unicode conundrum
On 25/11/2018 18:51, Robert Latest via Python-list wrote: > Hi folks, > what semmingly started out as a weird database character encoding mix-up > could be boiled down to a few lines of pure Python. The source-code > below is real utf8 (as evidenced by the UTF code point 'c3 a4' in the > third line of the hexdump). When just printed, the string "s" is > displayed correctly as 'ä' (a umlaut), but the string representation > shows that it seems to have been converted to latin-1 'e4' somewhere on > the way. It's not being converted to latin-1. It's a unicode string, as evidences by the 'u'. u'\xe4' is a unicode string with one character, U+00E4 (ä) > How can this be avoided? > > dh@jenna:~/python$ cat unicode.py > # -*- encoding: utf8 -*- > > s = u'ä' > > print(s) > print((s, )) > > dh@jenna:~/python$ hd unicode.py > 23 20 2d 2a 2d 20 65 6e 63 6f 64 69 6e 67 3a 20 |# -*- encoding: | > 0010 75 74 66 38 20 2d 2a 2d 0a 0a 73 20 3d 20 75 27 |utf8 -*-..s = u'| > 0020 c3 a4 27 0a 0a 70 72 69 6e 74 28 73 29 0a 70 72 |..'..print(s).pr| > 0030 69 6e 74 28 28 73 2c 20 29 29 0a 0a |int((s,))..| > 003c > dh@jenna:~/python$ python unicode.py > ä > (u'\xe4',) > dh@jenna:~/python$ > > > -- https://mail.python.org/mailman/listinfo/python-list
Re: Python2.7 unicode conundrum
On 11/25/18 12:51 PM, Robert Latest via Python-list wrote: > Hi folks, > what semmingly started out as a weird database character encoding mix-up > could be boiled down to a few lines of pure Python. The source-code > below is real utf8 (as evidenced by the UTF code point 'c3 a4' in the > third line of the hexdump). When just printed, the string "s" is > displayed correctly as 'ä' (a umlaut), but the string representation > shows that it seems to have been converted to latin-1 'e4' somewhere on > the way. > How can this be avoided? > > dh@jenna:~/python$ cat unicode.py > # -*- encoding: utf8 -*- > > s = u'ä' > > print(s) > print((s, )) > > dh@jenna:~/python$ hd unicode.py > 23 20 2d 2a 2d 20 65 6e 63 6f 64 69 6e 67 3a 20 |# -*- encoding: | > 0010 75 74 66 38 20 2d 2a 2d 0a 0a 73 20 3d 20 75 27 |utf8 -*-..s = u'| > 0020 c3 a4 27 0a 0a 70 72 69 6e 74 28 73 29 0a 70 72 |..'..print(s).pr| > 0030 69 6e 74 28 28 73 2c 20 29 29 0a 0a |int((s,))..| > 003c > dh@jenna:~/python$ python unicode.py > ä > (u'\xe4',) > dh@jenna:~/python$ > > > Why do you say it has been convert to 'Latin'. The string prints as being Unicode. Internally Python doesn't store strings as UTF-8, but as plain Unicode (UCS-2 or UCS-4 as needed), and code-point E4 is the character you want. The encoding statement tells python how your source file is encoded. -- Richard Damon -- https://mail.python.org/mailman/listinfo/python-list
Re: Issue in using "subprocess.Popen" for parsing the command output
On 2018-11-25 17:54, srinivasan wrote: Hope now I have changed on the string output as below, could you please correct me if am still wrong? import sys import subprocess interface = "wlan0" def main(ssid, pw): try: cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw) proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True) stdout, stderr = proc.communicate() retcode = proc.returncode print("printing stdout!!", stdout) print("printing retcode!!", retcode) except subprocess.CalledProcessError as e: s = """While executing '{}' something went wrong. Return code == '{}' Return output:\n'{}' """.format(cmd, e.returncode, e.output, shell=True) raise AssertionError(s) #return proc.strip().decode("utf-8") * return proc.decode("utf-8").strip()* [snip] No. As I said in _my_ post, 'proc' is the process itself. What you want is the string that it output, which, in your code, is 'stdout', so: return stdout.strip().decode("utf-8") -- https://mail.python.org/mailman/listinfo/python-list
Aw: Re: Issue in using "subprocess.Popen" for parsing the command output
> Even only with "*proc.decode("utf-8")"* in the above code still it seems to > throw the error No it does not. It throws the same TYPE of error due to the same SORT of mistake you made. You need to read carefully and try to think about what you read. Karsten -- https://mail.python.org/mailman/listinfo/python-list
Re: Issue in using "subprocess.Popen" for parsing the command output
Thanks a lot for your quick responses Mrab and appreciate the same. I changed the snippet as below, still it seems to be an issue, I am using python3 version: def main(ssid, pw): try: cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw) proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True, universal_newlines=True) stdout, stderr = proc.communicate() retcode = proc.returncode print("printing stdout!!", stdout) print("printing retcode!!", retcode) except subprocess.CalledProcessError as e: s = """While executing '{}' something went wrong. Return code == '{}' Return output:\n'{}' """.format(cmd, e.returncode, e.output, shell=True) raise AssertionError(s) #return proc.strip().decode("utf-8") #return proc.decode("utf-8").strip() #return proc.decode("utf-8") *return stdout.strip().decode("utf-8")* main("Apartment 18", "40672958689850014685") /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/venv/bin/python /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py printing stdout!! printing retcode!! 0 Traceback (most recent call last): File "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", line 37, in main("Apartment 18", "40672958689850014685") File "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", line 34, in main return stdout.strip().decode("utf-8") *AttributeError: 'str' object has no attribute 'decode'* Process finished with exit code 1 On Sun, Nov 25, 2018 at 11:49 PM MRAB wrote: > On 2018-11-25 17:54, srinivasan wrote: > > Hope now I have changed on the string output as below, could you > > please correct me if am still wrong? > > > > import sys > > import subprocess > > > > interface = "wlan0" > > > > > > def main(ssid, pw): > > > > try: > > cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw) > > > > proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, > > stderr=subprocess.PIPE, shell=True, universal_newlines=True) > > stdout, stderr = proc.communicate() > > retcode = proc.returncode > > > > print("printing stdout!!", stdout) > > print("printing retcode!!", retcode) > > > > except subprocess.CalledProcessError as e: > > s = """While executing '{}' something went wrong. > > Return code == '{}' > > Return output:\n'{}' > > """.format(cmd, e.returncode, e.output, > > shell=True) > > raise AssertionError(s) > > > > #return proc.strip().decode("utf-8") > > * return proc.decode("utf-8").strip()* > > > [snip] > > No. As I said in _my_ post, 'proc' is the process itself. What you want > is the string that it output, which, in your code, is 'stdout', so: > > return stdout.strip().decode("utf-8") > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Issue in using "subprocess.Popen" for parsing the command output
Dear Mrab, Even with "return stdout.strip().decode("utf-8")", it still seems to be an issue, I am using python 3.6, is this causing a issue? /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/venv/bin/python /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py printing stdout!! printing retcode!! 0 Traceback (most recent call last): File "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", line 37, in main("Apartment 18", "40672958689850014685") File "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", line 34, in main return stdout.strip().decode("utf-8") AttributeError: 'str' object has no attribute 'decode' Process finished with exit code 1 Many Thanks in advance On Sun, Nov 25, 2018 at 11:49 PM MRAB wrote: > On 2018-11-25 17:54, srinivasan wrote: > > Hope now I have changed on the string output as below, could you > > please correct me if am still wrong? > > > > import sys > > import subprocess > > > > interface = "wlan0" > > > > > > def main(ssid, pw): > > > > try: > > cmd = "nmcli device wifi connect '%s' password '%s'" % (ssid, pw) > > > > proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, > > stderr=subprocess.PIPE, shell=True, universal_newlines=True) > > stdout, stderr = proc.communicate() > > retcode = proc.returncode > > > > print("printing stdout!!", stdout) > > print("printing retcode!!", retcode) > > > > except subprocess.CalledProcessError as e: > > s = """While executing '{}' something went wrong. > > Return code == '{}' > > Return output:\n'{}' > > """.format(cmd, e.returncode, e.output, > > shell=True) > > raise AssertionError(s) > > > > #return proc.strip().decode("utf-8") > > * return proc.decode("utf-8").strip()* > > > [snip] > > No. As I said in _my_ post, 'proc' is the process itself. What you want > is the string that it output, which, in your code, is 'stdout', so: > > return stdout.strip().decode("utf-8") > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: Re: Issue in using "subprocess.Popen" for parsing the command output
Dear Karsten, With the help of Mrab Inputs, I tried Even with "return stdout.strip().decode("utf-8")", it still seems to be an issue, I am using python 3.6, is this causing a issue? /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/venv/bin/python /home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py printing stdout!! printing retcode!! 0 Traceback (most recent call last): File "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", line 37, in main("Apartment 18", "40672958689850014685") File "/home/srinivasan/Downloads/wifidisconnectissuenov23_homework/qa/test_library/test4.py", line 34, in main return stdout.strip().decode("utf-8") AttributeError: 'str' object has no attribute 'decode' Process finished with exit code 1 Many Thanks in advance On Sun, Nov 25, 2018 at 11:57 PM Karsten Hilbert wrote: > > Even only with "*proc.decode("utf-8")"* in the above code still it seems > to > > throw the error > > No it does not. It throws the same TYPE of error due to the same > SORT of mistake you made. > > You need to read carefully and try to think about what you read. > > Karsten > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list
Re: how to match list members in py3.x
On Sun, 25 Nov 2018 08:49:03 -0800, Muhammad Rizwan wrote: > On Sunday, 25 November 2018 10:59:46 UTC-5, Alister wrote: >> On Sun, 25 Nov 2018 07:43:42 -0800, Muhammad Rizwan wrote: >> >> > for each word in each line how can we check to see if a word is >> > already present in a list and if it is not how to append that word to >> > a new list >> >> the 1st step is to stay awake in class & listen to your instructor, >> then try to apply the principle involved >> >> We don't do homework for you >> >> when you have produced a program of you own (not necessarily working) >> if you post it here then we will be more than happy to provide >> constructive criticism >> >> >> >> >> -- >> "Show business is just like high school, except you get paid." >> - Martin Mull > > IF YOU CAN'T HELP BETTER IGNORE THE POST AND DON'T TRY TO BE A SMART > ASS. with an attitude like that I will go one better & ignore ALL your posts -- Good news. Ten weeks from Friday will be a pretty good day. -- https://mail.python.org/mailman/listinfo/python-list
Re: how to match list members in py3.x
On Sunday, 25 November 2018 13:48:42 UTC-5, Alister wrote: > On Sun, 25 Nov 2018 08:49:03 -0800, Muhammad Rizwan wrote: > > > On Sunday, 25 November 2018 10:59:46 UTC-5, Alister wrote: > >> On Sun, 25 Nov 2018 07:43:42 -0800, Muhammad Rizwan wrote: > >> > >> > for each word in each line how can we check to see if a word is > >> > already present in a list and if it is not how to append that word to > >> > a new list > >> > >> the 1st step is to stay awake in class & listen to your instructor, > >> then try to apply the principle involved > >> > >> We don't do homework for you > >> > >> when you have produced a program of you own (not necessarily working) > >> if you post it here then we will be more than happy to provide > >> constructive criticism > >> > >> > >> > >> > >> -- > >> "Show business is just like high school, except you get paid." > >> - Martin Mull > > > > IF YOU CAN'T HELP BETTER IGNORE THE POST AND DON'T TRY TO BE A SMART > > ASS. > > with an attitude like that I will go one better & ignore ALL your posts > > > > -- > Good news. Ten weeks from Friday will be a pretty good day. Yes please ignore. Don't need any help from you either. -- https://mail.python.org/mailman/listinfo/python-list
Re: Re: Issue in using "subprocess.Popen" for parsing the command output
On Mon, Nov 26, 2018 at 5:40 AM srinivasan wrote: > > Dear Karsten, > > With the help of Mrab Inputs, I tried Even with "return > stdout.strip().decode("utf-8")", it still seems to be an issue, I am using > python 3.6, is this causing a issue? No, it isn't. Two things are causing most of your issues. 1) You are trying to *get your homework done*, rather than actually gain competence. Slow down. Read some documentation. Don't fire off another request to the mailing list the moment you've tried one thing; instead, do some research. 2) You don't currently understand what decode() actually means. I don't fault you for that; a lot of people don't understand it, and it was thrown at you in a suggestion without any explanation. But encode() and decode() are not magical incantations to be inserted randomly until your code works. Read these: https://nedbatchelder.com/text/unipain.html https://www.joelonsoftware.com/2003/10/08/the-absolute-minimum-every-software-developer-absolutely-positively-must-know-about-unicode-and-character-sets-no-excuses/ I'll leave you with those. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Question on difference between LambdaType and FunctionType
Hello, Can someone please provide a use-case for having LambdaType separate to FunctionType? Since they are equal (LambdaType == FunctionType) it seems a bit superfluous to me. For example, I cannot do the following: if type(foo) is FunctionType and not type(foo) is LambdaType: ... What am I missing? Sincerely, Iwo Herka -- https://mail.python.org/mailman/listinfo/python-list
Re: Question on difference between LambdaType and FunctionType
On Mon, Nov 26, 2018 at 6:06 AM Iwo Herka wrote: > > Hello, > > Can someone please provide a use-case for having LambdaType > separate to FunctionType? Clarification: This is talking about the 'types' module. > Since they are equal > (LambdaType == FunctionType) it seems a bit superfluous to me. > For example, I cannot do the following: > > if type(foo) is FunctionType and not type(foo) is LambdaType: > ... > I was actually unaware of LambdaType even existing. Obviously it has to be the same as FunctionType (even more strongly than you put it: "types.LambdaType is types.FunctionType"), so I agree, it's superfluous. Maybe it's in case some other Python implementation might distinguish?? https://docs.python.org/3/library/types.html#types.FunctionType That said, though: if you want to distinguish a lambda function from a def function, you can do so with reasonable reliability using the function's name: >>> def is_lambda(f): return f.__name__ == "" ... >>> is_lambda(is_lambda) False >>> is_lambda(lambda x: x+1) True ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Question on difference between LambdaType and FunctionType
> That said, though: if you want to distinguish a lambda function from a > def function, you can do so with reasonable reliability using the > function's name: That's what I'm using currently, thank you for the suggestion. Nonetheless, it feels hacky - that's why I tried to use LambdaType and, to my surprise, it didn't do the job. Sincerely, Iwo Herka niedz., 25 lis 2018 o 20:41 Chris Angelico napisał(a): > > On Mon, Nov 26, 2018 at 6:06 AM Iwo Herka wrote: > > > > Hello, > > > > Can someone please provide a use-case for having LambdaType > > separate to FunctionType? > > Clarification: This is talking about the 'types' module. > > > Since they are equal > > (LambdaType == FunctionType) it seems a bit superfluous to me. > > For example, I cannot do the following: > > > > if type(foo) is FunctionType and not type(foo) is LambdaType: > > ... > > > > I was actually unaware of LambdaType even existing. Obviously it has > to be the same as FunctionType (even more strongly than you put it: > "types.LambdaType is types.FunctionType"), so I agree, it's > superfluous. Maybe it's in case some other Python implementation might > distinguish?? > > https://docs.python.org/3/library/types.html#types.FunctionType > > That said, though: if you want to distinguish a lambda function from a > def function, you can do so with reasonable reliability using the > function's name: > > >>> def is_lambda(f): return f.__name__ == "" > ... > >>> is_lambda(is_lambda) > False > >>> is_lambda(lambda x: x+1) > True > > ChrisA > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Question on difference between LambdaType and FunctionType
On Mon, Nov 26, 2018 at 6:54 AM Iwo Herka wrote: > > > That said, though: if you want to distinguish a lambda function from a > > def function, you can do so with reasonable reliability using the > > function's name: > > That's what I'm using currently, thank you for the suggestion. > Nonetheless, it feels hacky - that's why I tried to use LambdaType and, > to my surprise, it didn't do the job. > Yeah, not surprised you got surprised by that! But the real question is: why do you care about the difference? What is it in your code that cares about whether a function was created with "def" or with "lambda"? My suspicion is that, in most cases, the difference actually *is* the name (for instance, you might identify 'def' functions by name, but 'lambda' functions by identity), so it's not hacky to use the __name__ to distinguish them. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Question on difference between LambdaType and FunctionType
Chris Angelico wrote: > [...] why do you care about the difference? What is it in your code that > cares about whether a function was created with "def" or with "lambda"? In answer to my previous question on the list, (https://mail.python.org/pipermail/python-list/2018-November/738151.html), where I asked about possible implementation of immutability for user-defined types, dieter suggested a solution using metaclasses and instrumentation of the __init__method: https://mail.python.org/pipermail/python-list/2018-November/738182.html. In this solution, every __init__ method (be it in the class itself or somewhere in super), sets the flag before it starts executing its body and unsets it after it's finished. The flag then is used in __setattr__ to determine whether to raise TypeError. Therefore, I'm writing a piece of code to directly modify __init__'s AST by inserting two ast.Assign's into the tree (via the ast.NodeTransformer). (Wrapping __init__ with decorator doesn't do the job.) Everything works fine, except that I have to cover the following: foo = lambda self: None class Foo: __init__ = foo Since I'm treating FunctionType and LambdaType differently (I don't have to instrument lambdas, I can just return them), I have to know which one is it. Note: I know that there is a strong argument against using all this hackery, however, I'm partly experimenting in an attempt to learn something new. :) Sincerely, Iwo Herka -- https://mail.python.org/mailman/listinfo/python-list
Re: Question on difference between LambdaType and FunctionType
On Mon, Nov 26, 2018 at 7:45 AM Iwo Herka wrote: > > Everything works fine, except that I have to cover the following: > > foo = lambda self: None > > class Foo: > __init__ = foo > > Since I'm treating FunctionType and LambdaType differently (I don't have > to instrument lambdas, I can just return them), I have to know which one > is it. And this is exactly what I was wondering about - how a lambda function needs to be treated differently. So is there a difference between: class Foo: def __init__(self): ... and class Foo: def setup(self): ... __init__ = lambda self: self.setup() ? If so, what is the difference? Why are lambda functions not instrumented? ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Question on difference between LambdaType and FunctionType
> Why are lambda functions not instrumented? Because, if I didn't forget about anything, assignment in lambdas is invalid. That is, lambda self. self.x = 1 or lambda self: (self.x = 1) will throw SyntaxError. Therefore, I don't have to worry that someone will attempt to mutate the object from lambda. Sincerely, Iwo Herka -- https://mail.python.org/mailman/listinfo/python-list
Re: Question on difference between LambdaType and FunctionType
On Mon, Nov 26, 2018 at 7:55 AM Iwo Herka wrote: > > > class Foo: > >def setup(self): ... > >__init__ = lambda self: self.setup() > > Sorry, didn't address this. This is fine too, since I'm assuming that > only methods named __init__ are allowed to mutate the object. > Because 'setup' is not '__init__' it's disqualified. > Ahh, okay. In that case, yeah, you can safely ignore lambda functions... until Python 3.8, when assignment expressions become a thing. (See PEP 572.) So if you want to do something hacky like using the __name__, go for it. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Question on difference between LambdaType and FunctionType
> class Foo: >def setup(self): ... >__init__ = lambda self: self.setup() Sorry, didn't address this. This is fine too, since I'm assuming that only methods named __init__ are allowed to mutate the object. Because 'setup' is not '__init__' it's disqualified. Sincerely, Iwo Herka -- https://mail.python.org/mailman/listinfo/python-list
Re: Question on difference between LambdaType and FunctionType
On Mon, Nov 26, 2018 at 7:52 AM Iwo Herka wrote: > > > Why are lambda functions not instrumented? > > Because, if I didn't forget about anything, assignment > in lambdas is invalid. That is, > > lambda self. self.x = 1 > > or > > lambda self: (self.x = 1) > > will throw SyntaxError. Therefore, I don't have to worry > that someone will attempt to mutate the object from lambda. > Ahh, so this is an optimization. The question is: Do you need to worry about the lambda function calling another function? See my example above. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Question on difference between LambdaType and FunctionType
On Mon, Nov 26, 2018 at 7:56 AM Chris Angelico wrote: > > On Mon, Nov 26, 2018 at 7:55 AM Iwo Herka wrote: > > > > > class Foo: > > >def setup(self): ... > > >__init__ = lambda self: self.setup() > > > > Sorry, didn't address this. This is fine too, since I'm assuming that > > only methods named __init__ are allowed to mutate the object. > > Because 'setup' is not '__init__' it's disqualified. > > > > Ahh, okay. In that case, yeah, you can safely ignore lambda > functions... until Python 3.8, when assignment expressions become a > thing. (See PEP 572.) So if you want to do something hacky like using > the __name__, go for it. > Actually... it kinda depends on the point of the optimization. If you want a quick check, look at __name__. If it's okay to spend a bit more time figuring out whether to optimize or not, look for a STORE_ATTR opcode. If there aren't any, there can't be any "self.foo = bar". (You can't be certain of the converse, as "other_object.foo = bar" will also show up as STORE_ATTR.) This is a bit more work, but it's fundamentally non-hacky, because you're actually looking for the thing you care about. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: Question on difference between LambdaType and FunctionType
Chris Angelico wrote: > look for a STORE_ATTR opcode. If there aren't any, there can't > be any "self.foo = bar". (You can't be certain of the converse, as > "other_object.foo = bar" will also show up as STORE_ATTR.) This very useful, I will look into it, thanks. Sincerely, Iwo Herka -- https://mail.python.org/mailman/listinfo/python-list