From hongyi.zhao at gmail.com Sun Sep 1 00:13:44 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Sun, 1 Sep 2019 04:13:44 +0000 (UTC) Subject: Append some stuff into a file with only the last appended line reserved. Message-ID: Hi: I want to append some log of pycurl's downloading info to file, and I only want to reserve the last appended line when write. How to do this? From hongyi.zhao at gmail.com Sun Sep 1 02:12:16 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Sun, 1 Sep 2019 06:12:16 +0000 (UTC) Subject: ``if var'' and ``if var is not None'' Message-ID: Hi, The following two forms are always equivalent: ``if var'' and ``if var is not None'' Regards From rosuav at gmail.com Sun Sep 1 02:26:01 2019 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 1 Sep 2019 16:26:01 +1000 Subject: ``if var'' and ``if var is not None'' In-Reply-To: References: Message-ID: On Sun, Sep 1, 2019 at 4:16 PM Hongyi Zhao wrote: > > Hi, > > The following two forms are always equivalent: > > ``if var'' and ``if var is not None'' > Ahhhhhh... False. I'll go False. I'll be honest, I might have heard that one before, though. Sort of cheating. ChrisA From frank at chagford.com Sun Sep 1 02:33:46 2019 From: frank at chagford.com (Frank Millman) Date: Sun, 1 Sep 2019 08:33:46 +0200 Subject: ``if var'' and ``if var is not None'' In-Reply-To: References: Message-ID: <0e631247-3dee-2acc-0cd5-12ebbd987883@chagford.com> On 2019-09-01 8:12 AM, Hongyi Zhao wrote: > Hi, > > The following two forms are always equivalent: > > ``if var'' and ``if var is not None'' > > Regards > Not so. Here is an example - >>> var = [] >>> bool(var) False >>> bool(var is not None) True >>> Frank Millman From frank at chagford.com Sun Sep 1 02:33:46 2019 From: frank at chagford.com (Frank Millman) Date: Sun, 1 Sep 2019 08:33:46 +0200 Subject: ``if var'' and ``if var is not None'' In-Reply-To: References: Message-ID: <0e631247-3dee-2acc-0cd5-12ebbd987883@chagford.com> On 2019-09-01 8:12 AM, Hongyi Zhao wrote: > Hi, > > The following two forms are always equivalent: > > ``if var'' and ``if var is not None'' > > Regards > Not so. Here is an example - >>> var = [] >>> bool(var) False >>> bool(var is not None) True >>> Frank Millman From piet-l at vanoostrum.org Sun Sep 1 02:55:25 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Sun, 01 Sep 2019 08:55:25 +0200 Subject: ``if var'' and ``if var is not None'' References: Message-ID: Hongyi Zhao writes: > Hi, > > The following two forms are always equivalent: > > ``if var'' and ``if var is not None'' > > Regards In [1]: var = 0 In [2]: if var: ...: print('True') ...: else: ...: print('False') False In [3]: if var is not None: ...: print('True') ...: else: ...: print('False') True -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From tjreedy at udel.edu Sun Sep 1 10:54:53 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 1 Sep 2019 10:54:53 -0400 Subject: ``if var'' and ``if var is not None'' In-Reply-To: References: Message-ID: On 9/1/2019 2:12 AM, Hongyi Zhao wrote: > The following two forms are always equivalent: > ``if var'' and ``if var is not None'' Aside from the fact that this is false, why would you post such a thing? Trolling? Did you hit [Send] prematurely? -- Terry Jan Reedy From ian.g.kelly at gmail.com Sun Sep 1 11:58:13 2019 From: ian.g.kelly at gmail.com (Ian Kelly) Date: Sun, 1 Sep 2019 09:58:13 -0600 Subject: ``if var'' and ``if var is not None'' In-Reply-To: References: Message-ID: On Sun, Sep 1, 2019, 8:58 AM Terry Reedy wrote: > On 9/1/2019 2:12 AM, Hongyi Zhao wrote: > > > The following two forms are always equivalent: > > ``if var'' and ``if var is not None'' > > Aside from the fact that this is false, why would you post such a thing? > Trolling? Did you hit [Send] prematurely? > I suspect it was posted as a question despite not being phrased as such. > From barry at barrys-emacs.org Sun Sep 1 11:46:44 2019 From: barry at barrys-emacs.org (Barry) Date: Sun, 1 Sep 2019 16:46:44 +0100 Subject: open, close In-Reply-To: <20190831164136.68d0ea41@arcor.com> References: <20190831142205.668cc583@arcor.com> <20190831164136.68d0ea41@arcor.com> Message-ID: <6DA66180-6C55-49C7-AE0E-836F86907DDD@barrys-emacs.org> > On 31 Aug 2019, at 15:41, Manfred Lotz wrote: > > When you say COULD this sounds like it is a matter of luck. My thinking > was that USUALLY the file will be closed after the statement because > then the file handle goes out of scope. It all depends on the way any python implementation does its garbage collection. The file is closed as a side effect of deleting the file object to reclaiming the memory of the file object. At the start of python 3 people where suprised when files and other resources where not released at the same time that python 2 released them. Barry From ml_news at posteo.de Sun Sep 1 12:41:07 2019 From: ml_news at posteo.de (Manfred Lotz) Date: Sun, 1 Sep 2019 18:41:07 +0200 Subject: open, close References: <20190831142205.668cc583@arcor.com> <20190831164136.68d0ea41@arcor.com> <6DA66180-6C55-49C7-AE0E-836F86907DDD@barrys-emacs.org> Message-ID: <20190901184107.6922b4b5@arcor.com> On Sun, 1 Sep 2019 16:46:44 +0100 Barry wrote: > > On 31 Aug 2019, at 15:41, Manfred Lotz wrote: > > > > When you say COULD this sounds like it is a matter of luck. My > > thinking was that USUALLY the file will be closed after the > > statement because then the file handle goes out of scope. > > It all depends on the way any python implementation does its garbage > collection. The file is closed as a side effect of deleting the file > object to reclaiming the memory of the file object. > > At the start of python 3 people where suprised when files and other > resources where not released at the same time that python 2 released > them. > Thanks for this. Very interesting. -- Manfred From python at mrabarnett.plus.com Sun Sep 1 12:57:41 2019 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 1 Sep 2019 17:57:41 +0100 Subject: open, close In-Reply-To: <6DA66180-6C55-49C7-AE0E-836F86907DDD@barrys-emacs.org> References: <20190831142205.668cc583@arcor.com> <20190831164136.68d0ea41@arcor.com> <6DA66180-6C55-49C7-AE0E-836F86907DDD@barrys-emacs.org> Message-ID: <203dff7e-b71b-71ad-fe7f-676300277aa9@mrabarnett.plus.com> On 2019-09-01 16:46, Barry wrote: > > >> On 31 Aug 2019, at 15:41, Manfred Lotz wrote: >> >> When you say COULD this sounds like it is a matter of luck. My thinking >> was that USUALLY the file will be closed after the statement because >> then the file handle goes out of scope. > > It all depends on the way any python implementation does its garbage collection. The file is closed as a side effect of deleting the file object to reclaiming the memory of the file object. > > At the start of python 3 people where suprised when files and other resources where not released at the same time that python 2 released them. > Is that true? I thought that it was because other implementations of Python, such as Jython and IronPython, don't use reference counting, so files and other resources aren't necessarily released as soon as an object loses its last reference, and, moreover, it's not required to do so by the language definition. Adding the 'with' statement added determinism. See PEP 343 -- The "with" Statement From rosuav at gmail.com Sun Sep 1 13:04:28 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 2 Sep 2019 03:04:28 +1000 Subject: open, close In-Reply-To: <203dff7e-b71b-71ad-fe7f-676300277aa9@mrabarnett.plus.com> References: <20190831142205.668cc583@arcor.com> <20190831164136.68d0ea41@arcor.com> <6DA66180-6C55-49C7-AE0E-836F86907DDD@barrys-emacs.org> <203dff7e-b71b-71ad-fe7f-676300277aa9@mrabarnett.plus.com> Message-ID: On Mon, Sep 2, 2019 at 3:02 AM MRAB wrote: > > On 2019-09-01 16:46, Barry wrote: > > > > > >> On 31 Aug 2019, at 15:41, Manfred Lotz wrote: > >> > >> When you say COULD this sounds like it is a matter of luck. My thinking > >> was that USUALLY the file will be closed after the statement because > >> then the file handle goes out of scope. > > > > It all depends on the way any python implementation does its garbage collection. The file is closed as a side effect of deleting the file object to reclaiming the memory of the file object. > > > > At the start of python 3 people where suprised when files and other resources where not released at the same time that python 2 released them. > > > Is that true? > > I thought that it was because other implementations of Python, such as > Jython and IronPython, don't use reference counting, so files and other > resources aren't necessarily released as soon as an object loses its > last reference, and, moreover, it's not required to do so by the > language definition. > Yeah. Given that the PEP introducing the 'with' statement dates back well before Python 3, I very much doubt it has anything to do with the 3.0 changes. ChrisA From barry at barrys-emacs.org Sun Sep 1 13:31:29 2019 From: barry at barrys-emacs.org (Barry) Date: Sun, 1 Sep 2019 18:31:29 +0100 Subject: open, close In-Reply-To: <203dff7e-b71b-71ad-fe7f-676300277aa9@mrabarnett.plus.com> References: <20190831142205.668cc583@arcor.com> <20190831164136.68d0ea41@arcor.com> <6DA66180-6C55-49C7-AE0E-836F86907DDD@barrys-emacs.org> <203dff7e-b71b-71ad-fe7f-676300277aa9@mrabarnett.plus.com> Message-ID: <464B4572-227F-4A61-A0B6-57084DA98525@barrys-emacs.org> > On 1 Sep 2019, at 17:57, MRAB wrote: > > On 2019-09-01 16:46, Barry wrote: >>> On 31 Aug 2019, at 15:41, Manfred Lotz wrote: >>> When you say COULD this sounds like it is a matter of luck. My thinking >>> was that USUALLY the file will be closed after the statement because >>> then the file handle goes out of scope. >> It all depends on the way any python implementation does its garbage collection. The file is closed as a side effect of deleting the file object to reclaiming the memory of the file object. >> At the start of python 3 people where suprised when files and other resources where not released at the same time that python 2 released them. > Is that true? Yes. I recalling having to fix some code, but that was over 10 years ago. I have been using python 3 since the alphas of 3.0. > > I thought that it was because other implementations of Python, such as Jython and IronPython, don't use reference counting, so files and other resources aren't necessarily released as soon as an object loses its last reference, and, moreover, it's not required to do so by the language definition. You can also see delayed rsource freeing with cpython. I think it was an fd leak and lsof showed the files where not closing. Adding with fixed it. But it was so long ago... > > Adding the 'with' statement added determinism. Barry > > See PEP 343 -- The "with" Statement > -- > https://mail.python.org/mailman/listinfo/python-list > From anoushka8728 at gmail.com Sun Sep 1 01:06:35 2019 From: anoushka8728 at gmail.com (Anoushka) Date: Sun, 1 Sep 2019 10:36:35 +0530 Subject: IDLE not working Message-ID: <5d6b51db.1c69fb81.975eb.bc26@mx.google.com> Even after repairing my python IDLE, my IDLE is not working. It says sub process connection error. Kindly help me to resolve. I want to recover my previous work. Sent from Mail for Windows 10 From yuxuan.dong at outlook.com Sun Sep 1 04:19:35 2019 From: yuxuan.dong at outlook.com (YuXuan Dong) Date: Sun, 1 Sep 2019 08:19:35 +0000 Subject: Need help: integrating unittest with setuptools Message-ID: <02BDA799-EB4C-44B2-9F54-B874D491799C@outlook.com> Hi, everybody: I have met a problem while I ran `python setup.py test`: unittest.case.SkipTest: No module named 'winreg' I ran the command in MacOS and my project is written for only UNIX-like systems. I don't use any Windows-specified API. How dose `winreg` come here? In my `setup.py`: test_suite="test" In my `test/test.py`: import unittest class TestAll(unittest.TestCase): def testall(self): return None It works if I ran `python -m uniittest test.py` alone but raises the above exception if I ran `python setup.py test`. I'm working on this for the whole day, searching for every keywords I can think of with Google but can't find why or how. Could you help me? Thanks. -- YX. D. From alexezhilarasu at gmail.com Sun Sep 1 06:53:39 2019 From: alexezhilarasu at gmail.com (Alex) Date: Sun, 1 Sep 2019 16:23:39 +0530 Subject: IDLE missing ! Message-ID: <5d6ba335.1c69fb81.fc91f.d8c3@mx.google.com> I tried downloading the 3.7.4 version of python and and then tried to install tensorflow but found that TF is only available for version 3.5.x. Thus uninstalled the 3.7.4. version and tried downloading multiple 3.5.x version while all of them downloaded but I couldn?d find the IDLE shell with them. They do get installed into the system but the IDLE is not installed. what should I do about it ? Regrards, Alex Sent from Mail for Windows 10 From mehfurkannn at gmail.com Sun Sep 1 12:15:11 2019 From: mehfurkannn at gmail.com (=?utf-8?Q?Mehmet_Furkan_=C3=87OLAK?=) Date: Sun, 1 Sep 2019 19:15:11 +0300 Subject: Issue About Install pyinstaller Message-ID: <5d6bee91.1c69fb81.29700.ccaf@mx.google.com> Have a good day sir, I use python so much, I try to install pyinstaller in order to covert .py files to .exe files I did ?cmd > pip install pyinstaller > enter? from C:\Users\Furkan ?OLAK\AppData\Local\Programs\Python\Python37-32 but there isnt Script folder. ?n cmd I see this kind of ERROR 'pip' is not recognized as an internal or external command, operable program or batch file. So in order to install pyinstaller what should I do? Best regards From PythonList at DancesWithMice.info Sun Sep 1 15:20:56 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Mon, 2 Sep 2019 07:20:56 +1200 Subject: "Edit With Python" option missing In-Reply-To: References: Message-ID: Hi Aakash, On 31/08/19 8:54 PM, Akash verma wrote: > "Edit With Python" option missing from message context when right clicked > with mouse . There are two stages to working with a Python program: editing the source-code, and executing the program. For the first, most use a text editor/IDE, eg Sublime Text, PyCharm. Although Python comes with "Idle" as a built-in editor, plus the REPL. Accordingly, you will want to set the (right-click) context menu for the .py file-type/extension to open your chosen editor (if it's not already). In the second case, it depends very much upon which operating system/editor combination you are using. Most of the better editors have a 'magic button' to do the job. In case you are using MS-Windows (I don't!) please review https://docs.python.org/3.3/using/windows.html In the case of executing a program without editing the code, most of us utilise 'the command line', eg python3 source.py -- Regards =dn From __peter__ at web.de Sun Sep 1 15:26:37 2019 From: __peter__ at web.de (Peter Otten) Date: Sun, 01 Sep 2019 21:26:37 +0200 Subject: Using exec with embedded python interpreter 3.7 References: <3526187c-0128-4705-ab87-f103fb848d45@googlegroups.com> Message-ID: Eko palypse wrote: > I've already sent this through mail yesterday but it doesn't appear here, > maybe because of the help word in the content. Please execute in case it > appears a second time. > > > Hello, > I'm creating a notepad++ plugin which hosts an embedded python interpreter > by using cffi to build the dll. So far so good. One obstacle I've found is > that I'm not able to use exec(help(object)) in order to get the wanted > info from the object. > > The error I get is: > > Traceback (most recent call last): > File "", line 131, in run_code > File "", line 1, in > File "D:\...\Python\Python37_64\Lib\_sitebuiltins.py", line 103, in > __call__ > return pydoc.help(*args, **kwds) > File "D:\...\Python\Python37_64\Lib\pydoc.py", line 1895, in __call__ > self.help(request) > File "D:\...\Python\Python37_64\Lib\pydoc.py", line 1954, in help > else: doc(request, 'Help on %s:', output=self._output) > File "D:\...\Python\Python37_64\Lib\pydoc.py", line 1674, in doc > pager(render_doc(thing, title, forceload)) > File "D:\...\Python\Python37_64\Lib\pydoc.py", line 1451, in pager > pager(text) > File "D:\...\Python\Python37_64\Lib\pydoc.py", line 1576, in plainpager > sys.stdout.write(plain(_escape_stdout(text))) > File "D:\...\Python\Python37_64\Lib\pydoc.py", line 1528, in > _escape_stdout > return text.encode(encoding, 'backslashreplace').decode(encoding) > TypeError: encode() argument 1 must be str, not method The traceback suggests that sys.stdout was replaced with a custom object that has a method stdout.encoding() rather than the expected stdout.encoding attribute of type str. > If I encase the object with quotes then I do get the info that no > documentation is found for that object. > > 1. It doesn't feel right to encase the object with quotes. I don't have to > do this using the standard python interpreter shell. What did I miss? > > 2. If it is needed to use the encased way, why don't it show the help > info? The object class contains the module, class and functions doc > strings, what did I miss here? > > Thank you > Eren From python at mrabarnett.plus.com Sun Sep 1 15:30:06 2019 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 1 Sep 2019 20:30:06 +0100 Subject: Python 3.7.4 on win 7 In-Reply-To: References: Message-ID: On 2019-08-30 13:25, Mohammad Arif Samana wrote: > Dear sir, > I am installing the above version of python on my pc with win7 as my > operating system. But I am constantly receiving error messages. What could > be the problem and remedy for that. Kindly update me as I am facing > problems for my studies. Also brief me about Anaconda3. Its not installing > on window 7, is there any bugs. > What do the error messages say? From python at mrabarnett.plus.com Sun Sep 1 15:44:35 2019 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 1 Sep 2019 20:44:35 +0100 Subject: IDLE missing ! In-Reply-To: <5d6ba335.1c69fb81.fc91f.d8c3@mx.google.com> References: <5d6ba335.1c69fb81.fc91f.d8c3@mx.google.com> Message-ID: <14c463f3-d55e-f727-510b-fe46b2db08cf@mrabarnett.plus.com> On 2019-09-01 11:53, Alex wrote: > I tried downloading the 3.7.4 version of python and and then tried to install tensorflow but found that TF is only available for version 3.5.x. > Thus uninstalled the 3.7.4. version and tried downloading multiple 3.5.x version while all of them downloaded but I couldn?d find the IDLE shell with them. > They do get installed into the system but the IDLE is not installed. > what should I do about it ? > It should be in a subfolder of Python's folder, for example: C:\Python35\Lib\idlelib\idle.bat From python at mrabarnett.plus.com Sun Sep 1 15:39:39 2019 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 1 Sep 2019 20:39:39 +0100 Subject: Using exec with embedded python interpreter 3.7 In-Reply-To: <3526187c-0128-4705-ab87-f103fb848d45@googlegroups.com> References: <3526187c-0128-4705-ab87-f103fb848d45@googlegroups.com> Message-ID: <600aeed4-abee-ce5e-d472-604bd6fff3bb@mrabarnett.plus.com> On 2019-08-31 13:16, Eko palypse wrote: > I've already sent this through mail yesterday but it doesn't appear here, maybe because of the help word in the content. Please execute in case it appears a second time. > > > Hello, > I'm creating a notepad++ plugin which hosts an embedded python interpreter by using cffi to build the dll. > So far so good. One obstacle I've found is that I'm not able to use exec(help(object)) in order to get the wanted info from the object. > > The error I get is: > > Traceback (most recent call last): > File "", line 131, in run_code > File "", line 1, in > File "D:\...\Python\Python37_64\Lib\_sitebuiltins.py", line 103, in __call__ > return pydoc.help(*args, **kwds) > File "D:\...\Python\Python37_64\Lib\pydoc.py", line 1895, in __call__ > self.help(request) > File "D:\...\Python\Python37_64\Lib\pydoc.py", line 1954, in help > else: doc(request, 'Help on %s:', output=self._output) > File "D:\...\Python\Python37_64\Lib\pydoc.py", line 1674, in doc > pager(render_doc(thing, title, forceload)) > File "D:\...\Python\Python37_64\Lib\pydoc.py", line 1451, in pager > pager(text) > File "D:\...\Python\Python37_64\Lib\pydoc.py", line 1576, in plainpager > sys.stdout.write(plain(_escape_stdout(text))) > File "D:\...\Python\Python37_64\Lib\pydoc.py", line 1528, in _escape_stdout > return text.encode(encoding, 'backslashreplace').decode(encoding) > TypeError: encode() argument 1 must be str, not method > > If I encase the object with quotes then I do get the info that no documentation > is found for that object. > > 1. It doesn't feel right to encase the object with quotes. I don't have to do this using the standard python interpreter shell. What did I miss? > > 2. If it is needed to use the encased way, why don't it show the help info? > The object class contains the module, class and functions doc strings, what did I miss here? > I don't understand the "exec(help(object))" bit. help(object) print the help and returns None, so why exec(None)? From python at mrabarnett.plus.com Sun Sep 1 15:36:13 2019 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 1 Sep 2019 20:36:13 +0100 Subject: 'python' is not recognized as an internal or external command In-Reply-To: References: Message-ID: On 2019-08-31 10:13, G Kishore wrote: > Hi Team, > > I was installing python in my new windows 10 machine and cpuld able to access python from command window when the location is where I have installed python. > > The same I have tried to enter the command from Desktop location but "'python' is not recognized as an internal or external command," error is thrown. Done the path variable in the environmental variables as the location where python is installed. > Try using the Python launcher instead. It's called "py". From python at mrabarnett.plus.com Sun Sep 1 15:48:31 2019 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 1 Sep 2019 20:48:31 +0100 Subject: Issue About Install pyinstaller In-Reply-To: <5d6bee91.1c69fb81.29700.ccaf@mx.google.com> References: <5d6bee91.1c69fb81.29700.ccaf@mx.google.com> Message-ID: On 2019-09-01 17:15, Mehmet Furkan ?OLAK wrote: > Have a good day sir, > I use python so much, I try to install pyinstaller in order to covert .py files to .exe files > I did ?cmd > pip install pyinstaller > enter? > > from C:\Users\Furkan ?OLAK\AppData\Local\Programs\Python\Python37-32 > but there isnt Script folder. > > ?n cmd I see this kind of ERROR > 'pip' is not recognized as an internal or external command, > operable program or batch file. > > So in order to install pyinstaller what should I do? > Best regards > > Try running the pip module with the Python launcher instead: py -m pip install pyinstaller From spencerchief at googlemail.com Sun Sep 1 15:58:18 2019 From: spencerchief at googlemail.com (Spencer Du) Date: Sun, 1 Sep 2019 12:58:18 -0700 (PDT) Subject: How to create list for stuff inside mqtt and GUI?. Message-ID: <2da8a0d8-e484-4e57-b69a-0560c70359ce@googlegroups.com> Hi I have code for GUI and MQTT. In GUI.py I have "def loadGUI" which loads up a GUI file if the file exists in current directory. I want to add the file name to a list when a file is imported and for each subsequent file that is imported I want the file name to be imported to the same list and print the list or create a new list but with the imported file named added to list which has the existing file names that have already been imported. I was wondering how I do this. By the way run GUI.py to test this. GUI.py import logging from datetime import timedelta import time from thespian.actors import * from transitions import Machine import paho.mqtt.client as mqtt import importlib import os.path import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5 import QtWidgets, uic from mqtt import * import json class MainWindow(QtWidgets.QMainWindow): def __init__(self,parent = None): QMainWindow.__init__(self) super(MainWindow, self).__init__(parent) self.mdi = QMdiArea() self.setCentralWidget(self.mdi) self.setMinimumSize(QSize(800, 600)) self.setWindowTitle("PyQt button example - pythonprogramminglanguage.com") pybutton = QPushButton('Add device', self) pybutton.clicked.connect(self.importbutton) pybutton.move(100, 400) pybutton.resize(150, 32) self.textbox = QLineEdit(self) self.textbox.move(100,350) self.textbox.resize(100, 32) self.fileName_UI = "" def importbutton(self): self.fileName_UI = self.textbox.text() self.loadGUI() def getGUIFilename(self): return self.fileName_UI def loadGUI(self): print("Searching file", self.fileName_UI) try: module = __import__(self.fileName_UI) my_class = getattr(module, "SubWindow") sub = QMdiSubWindow() sub.setWidget(my_class()) sub.setWindowTitle("New GUI: " + self.fileName_UI) self.mdi.addSubWindow(sub) sub.show() print("creating new instance " + self.fileName_UI) client = device("Device") client.run() client.loop_start() # start the loop device_message = self.fileName_UI time.sleep(2) print("Subscribing to topic", "microscope/light_sheet_microscope/UI") client.subscribe("microscope/light_sheet_microscope/UI") print("Publishing message to topic", "microscope/light_sheet_microscope/UI") client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": self.fileName_UI, "cmd": "adding device"}}, indent=2)) time.sleep(1) # wait client.loop_stop() # stop the loop print("Device added" + "\n") listofdevice = [] listofdevice.append(self.fileName_UI) print(listofdevice) except: print("creating new instance " + self.fileName_UI) client = device("Device") client.run() client.loop_start() # start the loop device_message = self.fileName_UI time.sleep(2) print("Subscribing to topic", "microscope/light_sheet_microscope/UI") client.subscribe("microscope/light_sheet_microscope/UI") print("Publishing message to topic", "microscope/light_sheet_microscope/UI") client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": self.fileName_UI}}, indent=2)) time.sleep(2) # wait client.loop_stop() # stop the loop print(device_message + ".py " + "file doesn't exist") print("Device not added") if __name__ == "__main__": app = QApplication(sys.argv) mainWin = MainWindow() mainWin.show() publishedMessage = mainWin.getGUIFilename() sys.exit(app.exec_()) MQTT.py import logging from datetime import timedelta import time from thespian.actors import * from transitions import Machine import paho.mqtt.client as mqtt import importlib import os.path import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5 import QtWidgets, uic class device(mqtt.Client): def on_connect(self, mqttc, obj, flags, rc): if rc == 0: print("Connected to broker") else: print("Connection failed") # mqttc.subscribe("microscope/light_sheet_microscope/UI") def on_message(self, mqttc, userdata, message): msg = str(message.payload.decode("utf-8")) print("message recieved= " + msg) # print("File which you want to import(with .py extension)") print("message topic=", message.topic) print("message qos=", message.qos) print("message retain flag=", message.retain) def run(self): self.connect("broker.hivemq.com", 1883, 60) Thanks. From spencerdu at hotmail.co.uk Sun Sep 1 16:02:45 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Sun, 1 Sep 2019 13:02:45 -0700 (PDT) Subject: How to create a list and append to list inside a mqtt and GUI program? Message-ID: <58b4c5bd-05b1-45f3-9ef4-831e8cddc99a@googlegroups.com> Hi I have code for GUI and MQTT. In GUI.py I have "def loadGUI" which loads up a GUI file if the file exists in current directory. I want to add the file name to a list when a file is imported and for each subsequent file that is imported I want the file name to be imported to the same list and print the list or create a new list but with the imported file named added to list which has the existing file names that have already been imported. I was wondering how I do this. By the way run GUI.py to test this and test1.py and test2.py are the files which can be used to import GUI . GUI.py import logging from datetime import timedelta import time from thespian.actors import * from transitions import Machine import paho.mqtt.client as mqtt import importlib import os.path import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5 import QtWidgets, uic from mqtt import * import json class MainWindow(QtWidgets.QMainWindow): def __init__(self,parent = None): QMainWindow.__init__(self) super(MainWindow, self).__init__(parent) self.mdi = QMdiArea() self.setCentralWidget(self.mdi) self.setMinimumSize(QSize(800, 600)) self.setWindowTitle("PyQt button example - pythonprogramminglanguage.com") pybutton = QPushButton('Add device', self) pybutton.clicked.connect(self.importbutton) pybutton.move(100, 400) pybutton.resize(150, 32) self.textbox = QLineEdit(self) self.textbox.move(100,350) self.textbox.resize(100, 32) self.fileName_UI = "" def importbutton(self): self.fileName_UI = self.textbox.text() self.loadGUI() def getGUIFilename(self): return self.fileName_UI def loadGUI(self): print("Searching file", self.fileName_UI) try: module = __import__(self.fileName_UI) my_class = getattr(module, "SubWindow") sub = QMdiSubWindow() sub.setWidget(my_class()) sub.setWindowTitle("New GUI: " + self.fileName_UI) self.mdi.addSubWindow(sub) sub.show() print("creating new instance " + self.fileName_UI) client = device("Device") client.run() client.loop_start() # start the loop device_message = self.fileName_UI time.sleep(2) print("Subscribing to topic", "microscope/light_sheet_microscope/UI") client.subscribe("microscope/light_sheet_microscope/UI") print("Publishing message to topic", "microscope/light_sheet_microscope/UI") client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": self.fileName_UI, "cmd": "adding device"}}, indent=2)) time.sleep(1) # wait client.loop_stop() # stop the loop print("Device added" + "\n") listofdevice = [] listofdevice.append(self.fileName_UI) print(listofdevice) except: print("creating new instance " + self.fileName_UI) client = device("Device") client.run() client.loop_start() # start the loop device_message = self.fileName_UI time.sleep(2) print("Subscribing to topic", "microscope/light_sheet_microscope/UI") client.subscribe("microscope/light_sheet_microscope/UI") print("Publishing message to topic", "microscope/light_sheet_microscope/UI") client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": self.fileName_UI}}, indent=2)) time.sleep(2) # wait client.loop_stop() # stop the loop print(device_message + ".py " + "file doesn't exist") print("Device not added") if __name__ == "__main__": app = QApplication(sys.argv) mainWin = MainWindow() mainWin.show() publishedMessage = mainWin.getGUIFilename() sys.exit(app.exec_()) MQTT.py import logging from datetime import timedelta import time from thespian.actors import * from transitions import Machine import paho.mqtt.client as mqtt import importlib import os.path import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5 import QtWidgets, uic class device(mqtt.Client): def on_connect(self, mqttc, obj, flags, rc): if rc == 0: print("Connected to broker") else: print("Connection failed") # mqttc.subscribe("microscope/light_sheet_microscope/UI") def on_message(self, mqttc, userdata, message): msg = str(message.payload.decode("utf-8")) print("message recieved= " + msg) # print("File which you want to import(with .py extension)") print("message topic=", message.topic) print("message qos=", message.qos) print("message retain flag=", message.retain) def run(self): self.connect("broker.hivemq.com", 1883, 60) test1.py from PyQt5 import QtCore, QtGui, QtWidgets import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5 import QtWidgets, uic from mqtt import * class SubWindow(QWidget): def __init__(self, parent = None): super(SubWindow, self).__init__(parent) self.setMinimumSize(QSize(300, 200)) label = QLabel("Laser", self) self.modeButton = QtWidgets.QPushButton("Click me",self) self.modeButton.setGeometry(QtCore.QRect(10, 40, 81, 23)) self.modeButton.setObjectName("Turn on") self.modeButton.clicked.connect(self.modFun) def modFun(self): print("creating new instance " + "Laser") client = device("Laser") client.run() client.loop_start() # start the loop device_message = "ON" time.sleep(2) print("Subscribing to topic", "microscope/light_sheet_microscope/UI") client.subscribe("microscope/light_sheet_microscope/UI") print("Publishing message to topic", "microscope/light_sheet_microscope/UI") client.publish("microscope/light_sheet_microscope/UI", device_message) time.sleep(2) # wait client.loop_stop() # stop the loop def closeEvent(self, event): self.close() test2.py from PyQt5 import QtCore, QtGui, QtWidgets import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5 import QtWidgets, uic class SubWindow(QWidget): def __init__(self, parent = None): super(SubWindow, self).__init__(parent) self.setMinimumSize(QSize(300, 250)) label = QLabel("Sub Window", self) self.modeButton = QtWidgets.QPushButton("Click me",self) self.modeButton.setGeometry(QtCore.QRect(10, 40, 81, 23)) self.modeButton.clicked.connect(self.modFun) def modFun(self): print("Hello there i'm Click me") def closeEvent(self, event): self.close() Thanks. From piet-l at vanoostrum.org Sun Sep 1 17:12:20 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Sun, 01 Sep 2019 23:12:20 +0200 Subject: IDLE missing ! References: <5d6ba335.1c69fb81.fc91f.d8c3@mx.google.com> Message-ID: Alex writes: > I tried downloading the 3.7.4 version of python and and then tried to > install tensorflow but found that TF is only available for version > 3.5.x. > Thus uninstalled the 3.7.4. version and tried downloading multiple 3.5.x > version while all of them downloaded but I couldn?d find the IDLE shell > with them. Well, on MacOS 10.13.6 (High Sierra) tensorflow 1.14.0 does install on Python 3.7.4, and it also runs. It does give some warnings about deprecations, however. There is also a version 2.0RC which installs and runs without warnings. -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From tjreedy at udel.edu Sun Sep 1 17:39:38 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 1 Sep 2019 17:39:38 -0400 Subject: "Edit With Python" option missing In-Reply-To: References: Message-ID: On 8/31/2019 4:54 AM, Akash verma wrote: > "Edit With Python" option missing from message context when right clicked > with mouse . 'Edit with Python' does not make much sense since Python is not an editor. What OS? If Windows, do you possible mean 'Edit with IDLE'? If so, that requires installing IDLE. -- Terry Jan Reedy From tjreedy at udel.edu Sun Sep 1 17:42:47 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 1 Sep 2019 17:42:47 -0400 Subject: IDLE not working In-Reply-To: <5d6b51db.1c69fb81.975eb.bc26@mx.google.com> References: <5d6b51db.1c69fb81.975eb.bc26@mx.google.com> Message-ID: On 9/1/2019 1:06 AM, Anoushka wrote: > Even after repairing my python IDLE, my IDLE is not working. It says sub process connection error. Kindly help me to resolve. I want to recover my previous work. > > Sent from Mail for Windows 10 Select Help => IDLE Help and read the Startup Failure section. -- Terry Jan Reedy From flebber.crue at gmail.com Sun Sep 1 20:00:03 2019 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Sun, 1 Sep 2019 17:00:03 -0700 (PDT) Subject: How to create a list and append to list inside a mqtt and GUI program? In-Reply-To: <58b4c5bd-05b1-45f3-9ef4-831e8cddc99a@googlegroups.com> References: <58b4c5bd-05b1-45f3-9ef4-831e8cddc99a@googlegroups.com> Message-ID: On Monday, 2 September 2019 06:02:58 UTC+10, Spencer Du wrote: > Hi > > I have code for GUI and MQTT. In GUI.py I have "def loadGUI" which loads up a GUI file if the file exists in current directory. I want to add the file name to a list when a file is imported and for each subsequent file that is imported I want the file name to be imported to the same list and print the list or create a new list but with the imported file named added to list which has the existing file names that have already been imported. I was wondering how I do this. By the way run GUI.py to test this and test1.py and test2.py are the files which can be used to import GUI . > > GUI.py > > import logging > from datetime import timedelta > import time > from thespian.actors import * > from transitions import Machine > import paho.mqtt.client as mqtt > import importlib > import os.path > import sys > from PyQt5.QtWidgets import * > from PyQt5.QtCore import * > from PyQt5 import QtWidgets, uic > from mqtt import * > import json > > class MainWindow(QtWidgets.QMainWindow): > def __init__(self,parent = None): > QMainWindow.__init__(self) > super(MainWindow, self).__init__(parent) > self.mdi = QMdiArea() > self.setCentralWidget(self.mdi) > > self.setMinimumSize(QSize(800, 600)) > self.setWindowTitle("PyQt button example - pythonprogramminglanguage.com") > > pybutton = QPushButton('Add device', self) > > pybutton.clicked.connect(self.importbutton) > > pybutton.move(100, 400) > pybutton.resize(150, 32) > > self.textbox = QLineEdit(self) > self.textbox.move(100,350) > self.textbox.resize(100, 32) > > self.fileName_UI = "" > > def importbutton(self): > self.fileName_UI = self.textbox.text() > self.loadGUI() > > def getGUIFilename(self): > return self.fileName_UI > > def loadGUI(self): > print("Searching file", self.fileName_UI) > try: > module = __import__(self.fileName_UI) > my_class = getattr(module, "SubWindow") > > sub = QMdiSubWindow() > > sub.setWidget(my_class()) > sub.setWindowTitle("New GUI: " + self.fileName_UI) > self.mdi.addSubWindow(sub) > sub.show() > > print("creating new instance " + self.fileName_UI) > client = device("Device") > client.run() > > client.loop_start() # start the loop > device_message = self.fileName_UI > time.sleep(2) > print("Subscribing to topic", "microscope/light_sheet_microscope/UI") > client.subscribe("microscope/light_sheet_microscope/UI") > print("Publishing message to topic", "microscope/light_sheet_microscope/UI") > client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": self.fileName_UI, "cmd": "adding device"}}, indent=2)) > time.sleep(1) # wait > client.loop_stop() # stop the loop > print("Device added" + "\n") > listofdevice = [] > listofdevice.append(self.fileName_UI) > print(listofdevice) > except: > print("creating new instance " + self.fileName_UI) > client = device("Device") > client.run() > > client.loop_start() # start the loop > device_message = self.fileName_UI > time.sleep(2) > print("Subscribing to topic", "microscope/light_sheet_microscope/UI") > client.subscribe("microscope/light_sheet_microscope/UI") > print("Publishing message to topic", "microscope/light_sheet_microscope/UI") > client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": self.fileName_UI}}, indent=2)) > time.sleep(2) # wait > client.loop_stop() # stop the loop > print(device_message + ".py " + "file doesn't exist") > print("Device not added") > if __name__ == "__main__": > app = QApplication(sys.argv) > mainWin = MainWindow() > mainWin.show() > publishedMessage = mainWin.getGUIFilename() > sys.exit(app.exec_()) > > MQTT.py > import logging > from datetime import timedelta > import time > from thespian.actors import * > from transitions import Machine > import paho.mqtt.client as mqtt > import importlib > import os.path > import sys > from PyQt5.QtWidgets import * > from PyQt5.QtCore import * > from PyQt5 import QtWidgets, uic > > class device(mqtt.Client): > def on_connect(self, mqttc, obj, flags, rc): > if rc == 0: > print("Connected to broker") > else: > print("Connection failed") > > # mqttc.subscribe("microscope/light_sheet_microscope/UI") > > def on_message(self, mqttc, userdata, message): > msg = str(message.payload.decode("utf-8")) > print("message recieved= " + msg) > # print("File which you want to import(with .py extension)") > print("message topic=", message.topic) > print("message qos=", message.qos) > print("message retain flag=", message.retain) > > def run(self): > self.connect("broker.hivemq.com", 1883, 60) > > test1.py > > from PyQt5 import QtCore, QtGui, QtWidgets > import sys > from PyQt5.QtWidgets import * > from PyQt5.QtCore import * > from PyQt5 import QtWidgets, uic > from mqtt import * > > class SubWindow(QWidget): > def __init__(self, parent = None): > super(SubWindow, self).__init__(parent) > self.setMinimumSize(QSize(300, 200)) > label = QLabel("Laser", self) > > self.modeButton = QtWidgets.QPushButton("Click me",self) > self.modeButton.setGeometry(QtCore.QRect(10, 40, 81, 23)) > self.modeButton.setObjectName("Turn on") > self.modeButton.clicked.connect(self.modFun) > > def modFun(self): > print("creating new instance " + "Laser") > client = device("Laser") > client.run() > > client.loop_start() # start the loop > device_message = "ON" > time.sleep(2) > print("Subscribing to topic", "microscope/light_sheet_microscope/UI") > client.subscribe("microscope/light_sheet_microscope/UI") > print("Publishing message to topic", "microscope/light_sheet_microscope/UI") > client.publish("microscope/light_sheet_microscope/UI", device_message) > time.sleep(2) # wait > client.loop_stop() # stop the loop > > def closeEvent(self, event): > self.close() > > test2.py > > from PyQt5 import QtCore, QtGui, QtWidgets > import sys > from PyQt5.QtWidgets import * > from PyQt5.QtCore import * > from PyQt5 import QtWidgets, uic > > class SubWindow(QWidget): > def __init__(self, parent = None): > super(SubWindow, self).__init__(parent) > self.setMinimumSize(QSize(300, 250)) > label = QLabel("Sub Window", self) > > self.modeButton = QtWidgets.QPushButton("Click me",self) > self.modeButton.setGeometry(QtCore.QRect(10, 40, 81, 23)) > > self.modeButton.clicked.connect(self.modFun) > > > > def modFun(self): > print("Hello there i'm Click me") > > def closeEvent(self, event): > self.close() > > Thanks. list_of_file_name = [] my_file = getGUIFilename() list.append(my_file) Sayth From flebber.crue at gmail.com Sun Sep 1 20:05:20 2019 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Sun, 1 Sep 2019 17:05:20 -0700 (PDT) Subject: Need help: integrating unittest with setuptools In-Reply-To: References: <02BDA799-EB4C-44B2-9F54-B874D491799C@outlook.com> Message-ID: <52146889-2855-48d9-8ab0-090512c5409b@googlegroups.com> On Monday, 2 September 2019 04:44:29 UTC+10, YuXuan Dong wrote: > Hi, everybody: > > I have met a problem while I ran `python setup.py test`: > > unittest.case.SkipTest: No module named 'winreg' > > I ran the command in MacOS and my project is written for only UNIX-like systems. I don't use any Windows-specified API. How dose `winreg` come here? > > In my `setup.py`: > > test_suite="test" > > In my `test/test.py`: > > import unittest > > class TestAll(unittest.TestCase): > def testall(self): > return None > > It works if I ran `python -m uniittest test.py` alone but raises the above exception if I ran `python setup.py test`. > > I'm working on this for the whole day, searching for every keywords I can think of with Google but can't find why or how. Could you help me? Thanks. > > -- > YX. D. Does this help? https://stackoverflow.com/questions/4320761/importerror-no-module-named-winreg-python3 Sayth From python at mrabarnett.plus.com Sun Sep 1 20:26:55 2019 From: python at mrabarnett.plus.com (MRAB) Date: Mon, 2 Sep 2019 01:26:55 +0100 Subject: How to create a list and append to list inside a mqtt and GUI program? In-Reply-To: <58b4c5bd-05b1-45f3-9ef4-831e8cddc99a@googlegroups.com> References: <58b4c5bd-05b1-45f3-9ef4-831e8cddc99a@googlegroups.com> Message-ID: <30f675e2-04bc-7958-816b-26edba89efba@mrabarnett.plus.com> On 2019-09-01 21:02, Spencer Du wrote: > Hi > > I have code for GUI and MQTT. In GUI.py I have "def loadGUI" which loads up a GUI file if the file exists in current directory. I want to add the file name to a list when a file is imported and for each subsequent file that is imported I want the file name to be imported to the same list and print the list or create a new list but with the imported file named added to list which has the existing file names that have already been imported. I was wondering how I do this. By the way run GUI.py to test this and test1.py and test2.py are the files which can be used to import GUI . > > GUI.py [snip] import json > [snip] > def loadGUI(self): > print("Searching file", self.fileName_UI) > try: > module = __import__(self.fileName_UI) > my_class = getattr(module, "SubWindow") > > sub = QMdiSubWindow() > > sub.setWidget(my_class()) > sub.setWindowTitle("New GUI: " + self.fileName_UI) > self.mdi.addSubWindow(sub) > sub.show() > > print("creating new instance " + self.fileName_UI) > client = device("Device") > client.run() > > client.loop_start() # start the loop > device_message = self.fileName_UI > time.sleep(2) > print("Subscribing to topic", "microscope/light_sheet_microscope/UI") > client.subscribe("microscope/light_sheet_microscope/UI") > print("Publishing message to topic", "microscope/light_sheet_microscope/UI") > client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": self.fileName_UI, "cmd": "adding device"}}, indent=2)) > time.sleep(1) # wait > client.loop_stop() # stop the loop > print("Device added" + "\n") This is a local name, local to the method. > listofdevice = [] > listofdevice.append(self.fileName_UI) > print(listofdevice) You could store the list in a file: read the current list from the file and then write the modified list to the file. The file won't exist initially, so you can treat that as the list being empty. You're already importing the json module, so you could read/write it as JSON. Also, _don't_ use a "bare" except like here: > except: because catches _all_ exceptions. Instead, catch only those you're prepared to handle. [snip] From Richard at Damon-Family.org Sun Sep 1 20:44:45 2019 From: Richard at Damon-Family.org (Richard Damon) Date: Sun, 1 Sep 2019 20:44:45 -0400 Subject: How to create a list and append to list inside a mqtt and GUI program? In-Reply-To: <58b4c5bd-05b1-45f3-9ef4-831e8cddc99a@googlegroups.com> References: <58b4c5bd-05b1-45f3-9ef4-831e8cddc99a@googlegroups.com> Message-ID: On 9/1/19 4:02 PM, Spencer Du wrote: > Hi > > I have code for GUI and MQTT. In GUI.py I have "def loadGUI" which loads up a GUI file if the file exists in current directory. I want to add the file name to a list when a file is imported and for each subsequent file that is imported I want the file name to be imported to the same list and print the list or create a new list but with the imported file named added to list which has the existing file names that have already been imported. I was wondering how I do this. By the way run GUI.py to test this and test1.py and test2.py are the files which can be used to import GUI . To make the list persistent you need to create it as a module global or as a class member of a persistent object (like your MainWindow) -- Richard Damon From alan at csail.mit.edu Sun Sep 1 22:33:27 2019 From: alan at csail.mit.edu (Alan Bawden) Date: 01 Sep 2019 22:33:27 -0400 Subject: a,b = 2,3 and [a,b] = [2,3] References: <782d5cad-8e74-424a-97db-1192168dd487@googlegroups.com> Message-ID: <864l1vpgug.fsf@richard.bawden.org> Eko palypse writes: > Am Montag, 2. September 2019 00:49:05 UTC+2 schrieb Hongyi Zhao: > > Hi, > > > > What's differences: > > > > a,b = 2,3 and [a,b] = [2,3] > > > > Regards > > In this example the result is the same but the second one > builds, internally, an additional list, therefore isn't as sufficient > as the first one. It looks to me like they generate identical code. The first one calls the construction of a tuple, where the second one calls for the construction of a list. It would be surprising if the compiler optimized the tuple away, but failed to optimize the list away! -- Alan Bawden From rosuav at gmail.com Sun Sep 1 22:48:28 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 2 Sep 2019 12:48:28 +1000 Subject: a,b = 2,3 and [a,b] = [2,3] In-Reply-To: <864l1vpgug.fsf@richard.bawden.org> References: <782d5cad-8e74-424a-97db-1192168dd487@googlegroups.com> <864l1vpgug.fsf@richard.bawden.org> Message-ID: On Mon, Sep 2, 2019 at 12:36 PM Alan Bawden wrote: > > Eko palypse writes: > > > Am Montag, 2. September 2019 00:49:05 UTC+2 schrieb Hongyi Zhao: > > > Hi, > > > > > > What's differences: > > > > > > a,b = 2,3 and [a,b] = [2,3] > > > > > > Regards > > > > In this example the result is the same but the second one > > builds, internally, an additional list, therefore isn't as sufficient > > as the first one. > > It looks to me like they generate identical code. The first one calls the > construction of a tuple, where the second one calls for the construction of > a list. It would be surprising if the compiler optimized the tuple > away, but failed to optimize the list away! > Well, you can find out with the 'dis' module. >>> def f(): ... a, b = 2, 3 ... a, b = [2, 3] ... >>> dis.dis(f) 2 0 LOAD_CONST 1 ((2, 3)) 2 UNPACK_SEQUENCE 2 4 STORE_FAST 0 (a) 6 STORE_FAST 1 (b) 3 8 LOAD_CONST 2 (2) 10 LOAD_CONST 3 (3) 12 BUILD_LIST 2 14 UNPACK_SEQUENCE 2 16 STORE_FAST 0 (a) 18 STORE_FAST 1 (b) 20 LOAD_CONST 0 (None) 22 RETURN_VALUE This is with CPython 3.9. It's entirely possible that other Pythons and/or other versions of CPython may give different results, but with this particular interpreter, the list is not optimized away. Tuples get optimized away in quite a number of situations. Exchanging is done on the stack: >>> def f(): ... x, y = y, x ... >>> dis.dis(f) 2 0 LOAD_FAST 0 (y) 2 LOAD_FAST 1 (x) 4 ROT_TWO 6 STORE_FAST 1 (x) 8 STORE_FAST 0 (y) 10 LOAD_CONST 0 (None) 12 RETURN_VALUE There are a lot of assumptions the interpreter can make about them, including that they cannot nest recursively [1], and that, when constructed from constants, they are themselves constant. Any time you think it'll make no difference, go with a tuple. ChrisA [1] Technically they can, but you have to use the CPython API or equivalent - vanilla Python code can't do it. From dieter at handshake.de Mon Sep 2 01:05:14 2019 From: dieter at handshake.de (dieter) Date: Mon, 02 Sep 2019 07:05:14 +0200 Subject: Problem while integrating unittest with setuptools References: <010F3B60-6AE1-420F-94BA-63C3CC3723A8@contoso.com> Message-ID: <87mufn8f05.fsf@handshake.de> YuXuan Dong writes: > I met a problem while I ran `python setup.py test`: > > unittest.case.SkipTest: No module named 'winreg' > ... no windows modules should be necessary ... I know apparently unexplainable "no module named ..." messages as a side effect of the use of "six". "six" is used to facilitate the development of components usable for both Python 2 and Python 3. Among others, it contains the module "six.moves" which uses advance Python features to allow the import of Python modules from different locations in the package hierarchy and also handles name changes. This can confuse other components using introspection (they see modules in "six.move" which are not really available). To find out if something like this is the cause of your problem, please try to get a traceback. It might be necessary to use a different test runner for this (I like much "zope.testrunner" and my wrapper "dm.zopepatches.ztest"). From alan at csail.mit.edu Mon Sep 2 04:29:31 2019 From: alan at csail.mit.edu (Alan Bawden) Date: 02 Sep 2019 04:29:31 -0400 Subject: a,b = 2,3 and [a,b] = [2,3] References: <782d5cad-8e74-424a-97db-1192168dd487@googlegroups.com> <864l1vpgug.fsf@richard.bawden.org> Message-ID: <86y2z7nlsk.fsf@richard.bawden.org> Chris Angelico writes: > On Mon, Sep 2, 2019 at 12:36 PM Alan Bawden wrote: ... > > > > a,b = 2,3 and [a,b] = [2,3] ... > > It looks to me like they generate identical code. The first one calls the > > construction of a tuple, where the second one calls for the construction of > > a list. It would be surprising if the compiler optimized the tuple > > away, but failed to optimize the list away! > > > > Well, you can find out with the 'dis' module. Actually, when I wrote "It looks to me", I was in fact looking at the results of calling `dis.dis'! But I had unconciously changed what the OP wrote to make it more realistic. I tested: >>> def f(x, y): x, y = y, x [x, y] = [y, x] >>> dis.dis(f) 2 0 LOAD_FAST 1 (y) 3 LOAD_FAST 0 (x) 6 ROT_TWO 7 STORE_FAST 0 (x) 10 STORE_FAST 1 (y) 3 13 LOAD_FAST 1 (y) 16 LOAD_FAST 0 (x) 19 ROT_TWO 20 STORE_FAST 0 (x) 23 STORE_FAST 1 (y) 26 LOAD_CONST 0 (None) 29 RETURN_VALUE And I observed that whatever peephole optimization got rid of the tuple also got rid of the list. Clearly "BUILD_LIST 2" or "BUILD_TUPLE 2" followed by "UNPACK_SEQUENCE 2" can be replaced by "ROT_TWO", and the compiler knew that! It never occured to me that the case where all the elements of the sequence to the right of the "=" were constants would change that, but yes, you are right about that (but read on...): > >>> def f(): > ... a, b = 2, 3 > ... a, b = [2, 3] > ... > >>> dis.dis(f) > 2 0 LOAD_CONST 1 ((2, 3)) > 2 UNPACK_SEQUENCE 2 > 4 STORE_FAST 0 (a) > 6 STORE_FAST 1 (b) And indeed, if they are all constants, then the tuple itself is a constant, and the obvious peephole optimization is no longer available! But wait... > 3 8 LOAD_CONST 2 (2) > 10 LOAD_CONST 3 (3) > 12 BUILD_LIST 2 > 14 UNPACK_SEQUENCE 2 > 16 STORE_FAST 0 (a) > 18 STORE_FAST 1 (b) Dang! There is exactly the instruction sequence I just argued could be optimized away still sitting right there. So maybe my belief that this is being done by peephole optimization is in fact incorrect? So I went and tried again: bash-4.2$ python3 -E Python 3.6.6 (default, Aug 13 2018, 18:24:23) [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> def f(): ... a, b = 2, 3 ... a, b = [2, 3] ... a, b = b, a ... a, b = [b, a] ... >>> import dis >>> dis.dis(f) 2 0 LOAD_CONST 3 ((2, 3)) 2 UNPACK_SEQUENCE 2 4 STORE_FAST 0 (a) 6 STORE_FAST 1 (b) 3 8 LOAD_CONST 1 (2) 10 LOAD_CONST 2 (3) 12 ROT_TWO 14 STORE_FAST 0 (a) 16 STORE_FAST 1 (b) 4 18 LOAD_FAST 1 (b) 20 LOAD_FAST 0 (a) 22 ROT_TWO 24 STORE_FAST 0 (a) 26 STORE_FAST 1 (b) 5 28 LOAD_FAST 1 (b) 30 LOAD_FAST 0 (a) 32 ROT_TWO 34 STORE_FAST 0 (a) 36 STORE_FAST 1 (b) 38 LOAD_CONST 0 (None) 40 RETURN_VALUE OK, now I'm confused. How come I'm not seeing the BUILD_LIST/UNPACK_SEQUENCE sequence that you're seeing? > This is with CPython 3.9. It's entirely possible that other Pythons > and/or other versions of CPython may give different results, but with > this particular interpreter, the list is not optimized away. I actually did try this with several different versions of CPython going back to 2.4 and up to 3.6, and they all behave this way for me. Maybe something changed after 3.6? Weird... -- Alan Bawden From rosuav at gmail.com Mon Sep 2 05:24:15 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 2 Sep 2019 19:24:15 +1000 Subject: a,b = 2,3 and [a,b] = [2,3] In-Reply-To: <86y2z7nlsk.fsf@richard.bawden.org> References: <782d5cad-8e74-424a-97db-1192168dd487@googlegroups.com> <864l1vpgug.fsf@richard.bawden.org> <86y2z7nlsk.fsf@richard.bawden.org> Message-ID: On Mon, Sep 2, 2019 at 6:31 PM Alan Bawden wrote: > > Dang! There is exactly the instruction sequence I just argued could be > optimized away still sitting right there. So maybe my belief that this is > being done by peephole optimization is in fact incorrect? So I went and > tried again: > > bash-4.2$ python3 -E > Python 3.6.6 (default, Aug 13 2018, 18:24:23) > [GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> def f(): > ... a, b = 2, 3 > ... a, b = [2, 3] > ... a, b = b, a > ... a, b = [b, a] > ... > >>> import dis > >>> dis.dis(f) > 2 0 LOAD_CONST 3 ((2, 3)) > 2 UNPACK_SEQUENCE 2 > 4 STORE_FAST 0 (a) > 6 STORE_FAST 1 (b) > > 3 8 LOAD_CONST 1 (2) > 10 LOAD_CONST 2 (3) > 12 ROT_TWO > 14 STORE_FAST 0 (a) > 16 STORE_FAST 1 (b) > > 4 18 LOAD_FAST 1 (b) > 20 LOAD_FAST 0 (a) > 22 ROT_TWO > 24 STORE_FAST 0 (a) > 26 STORE_FAST 1 (b) > > 5 28 LOAD_FAST 1 (b) > 30 LOAD_FAST 0 (a) > 32 ROT_TWO > 34 STORE_FAST 0 (a) > 36 STORE_FAST 1 (b) > 38 LOAD_CONST 0 (None) > 40 RETURN_VALUE > > OK, now I'm confused. How come I'm not seeing the > BUILD_LIST/UNPACK_SEQUENCE sequence that you're seeing? > > > This is with CPython 3.9. It's entirely possible that other Pythons > > and/or other versions of CPython may give different results, but with > > this particular interpreter, the list is not optimized away. > > I actually did try this with several different versions of CPython going > back to 2.4 and up to 3.6, and they all behave this way for me. Maybe > something changed after 3.6? Weird... This is indeed fascinating. Something DID indeed change. I tried this slightly shorter version in a few different Pythons: def f(): a, b = b, a a, b = [b, a] import dis, sys print(sys.version) dis.dis(f) # Show the code identically on 2.x and 3.x print(repr(f.__code__.co_code).lstrip("b")) Here's what I learned: CPython 2.7: ROT_TWO, bytecode CPython 3.4: ROT_TWO, bytecode CPython 3.5: ROT_TWO, bytecode CPython 3.6: ROT_TWO, wordcode CPython 3.7: UNPACK_SEQUENCE CPython 3.8: UNPACK_SEQUENCE CPython 3.9: UNPACK_SEQUENCE PyPy 5.6 (2.7): out-of-order LOAD/STORE PyPyJS (2.7.9): out-of-order LOAD/STORE Jython 2.5.3: unable to disassemble MicroPython 3.4: no 'dis' module or __code__ attr Brython: unable to disassemble CPython 3.6 made the change to wordcode. If you have a 3.5 hanging around, you should be able to see this easily in the disassembly, because the LOAD_FAST operations require three bytes each in 3.5, but only one (two byte) operation in 3.6, but the ROT_TWO is a single byte in 3.5 and now requires two in 3.6. PyPy can reorder operations knowing that it won't affect anything, and thus optimizes it down to "load b, load a, store b, store a" regardless of the syntax. But the curious difference happens in 3.7. I don't know what changed to cause this, but from there on, the list gets built and then unpacked. This may represent a performance regression. Alternatively, just take it as a recommendation to always do your variable exchanges WITHOUT square brackets, and you'll be fine. ChrisA From ekopalypse at gmail.com Sun Sep 1 17:07:53 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Sun, 1 Sep 2019 14:07:53 -0700 (PDT) Subject: Using exec with embedded python interpreter 3.7 In-Reply-To: References: <3526187c-0128-4705-ab87-f103fb848d45@googlegroups.com> Message-ID: <3f39500d-84fb-464c-9666-d8a5de29c995@googlegroups.com> Just saw, that I replied to you directly instead to python list, sorry. That did it, changed encoding from function to property and now I'm able to call help(object) Thank you. From ekopalypse at gmail.com Sun Sep 1 17:10:54 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Sun, 1 Sep 2019 14:10:54 -0700 (PDT) Subject: Using exec with embedded python interpreter 3.7 In-Reply-To: References: <3526187c-0128-4705-ab87-f103fb848d45@googlegroups.com> <600aeed4-abee-ce5e-d472-604bd6fff3bb@mrabarnett.plus.com> Message-ID: <6ef2c440-cae5-499c-9ab1-d3ddcc74a4b7@googlegroups.com> @MRAB, I'm building a notepad++ plugin which can execute the written code and if one writes help(os) it gets executed via exec(editor.getText()) and output redirected to the plugin console window. Sorry to you as well as I have also replied to you directly. Thank you From hongyi.zhao at gmail.com Sun Sep 1 18:48:53 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Sun, 1 Sep 2019 22:48:53 +0000 (UTC) Subject: a,b = 2,3 and [a,b] = [2,3] Message-ID: Hi, What's differences: a,b = 2,3 and [a,b] = [2,3] Regards From ekopalypse at gmail.com Sun Sep 1 19:46:15 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Sun, 1 Sep 2019 16:46:15 -0700 (PDT) Subject: a,b = 2,3 and [a,b] = [2,3] In-Reply-To: References: Message-ID: <782d5cad-8e74-424a-97db-1192168dd487@googlegroups.com> Am Montag, 2. September 2019 00:49:05 UTC+2 schrieb Hongyi Zhao: > Hi, > > What's differences: > > a,b = 2,3 and [a,b] = [2,3] > > Regards In this example the result is the same but the second one builds, internally, an additional list, therefore isn't as sufficient as the first one. From yuxuan.dong at outlook.com Sun Sep 1 23:44:37 2019 From: yuxuan.dong at outlook.com (YuXuan Dong) Date: Mon, 2 Sep 2019 03:44:37 +0000 Subject: Need help: integrating unittest with setuptools In-Reply-To: <52146889-2855-48d9-8ab0-090512c5409b@googlegroups.com> References: <02BDA799-EB4C-44B2-9F54-B874D491799C@outlook.com> <52146889-2855-48d9-8ab0-090512c5409b@googlegroups.com> Message-ID: <006AF681-1E78-48EA-A3CF-D806A73B9B94@outlook.com> No, it doesn't. The stackoverflow question you posted is about the renaming of `winreg`. `_winreg` is renamed to `winreg`. That's why the poster can't find the module. My program is written for and running on unix-like systems. I think `winreg` should not appear here. I have tried running `pip3 install winreg` on MacOS and I got: `Could not find a version that satisfies the requirement winreg`. ?On 2019/9/2, 08:11, "Python-list on behalf of Sayth Renshaw" wrote: On Monday, 2 September 2019 04:44:29 UTC+10, YuXuan Dong wrote: > Hi, everybody: > > I have met a problem while I ran `python setup.py test`: > > unittest.case.SkipTest: No module named 'winreg' > > I ran the command in MacOS and my project is written for only UNIX-like systems. I don't use any Windows-specified API. How dose `winreg` come here? > > In my `setup.py`: > > test_suite="test" > > In my `test/test.py`: > > import unittest > > class TestAll(unittest.TestCase): > def testall(self): > return None > > It works if I ran `python -m uniittest test.py` alone but raises the above exception if I ran `python setup.py test`. > > I'm working on this for the whole day, searching for every keywords I can think of with Google but can't find why or how. Could you help me? Thanks. > > -- > YX. D. Does this help? https://stackoverflow.com/questions/4320761/importerror-no-module-named-winreg-python3 Sayth -- https://mail.python.org/mailman/listinfo/python-list From yuxuan.dong at outlook.com Mon Sep 2 05:28:21 2019 From: yuxuan.dong at outlook.com (YuXuan Dong) Date: Mon, 2 Sep 2019 09:28:21 +0000 Subject: Problem while integrating unittest with setuptools In-Reply-To: <87mufn8f05.fsf@handshake.de> References: <010F3B60-6AE1-420F-94BA-63C3CC3723A8@contoso.com> <87mufn8f05.fsf@handshake.de> Message-ID: Thank you. It helps. I have uninstalled `six` using `pip uninstall six` but the problem is still there. As you suggested, I have checked the traceback and found the exception is caused by `/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/test/test_winreg.py` on my machine with homebrew-installed Python3. I have realized that the problem may be caused of `test-suite=test` in my `setup.py`. `setuptools` will find the `test` package. But the package it found is not in my project. It found `/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/test`. To verify my guess, I renamed the `test` folder in the above folder. As expected, another exception is raised. The problem now is that, how could I config my `setup.py` to work properly? On 2019/9/2, 13:07, "Python-list on behalf of dieter" wrote: YuXuan Dong writes: > I met a problem while I ran `python setup.py test`: > > unittest.case.SkipTest: No module named 'winreg' > ... no windows modules should be necessary ... I know apparently unexplainable "no module named ..." messages as a side effect of the use of "six". "six" is used to facilitate the development of components usable for both Python 2 and Python 3. Among others, it contains the module "six.moves" which uses advance Python features to allow the import of Python modules from different locations in the package hierarchy and also handles name changes. This can confuse other components using introspection (they see modules in "six.move" which are not really available). To find out if something like this is the cause of your problem, please try to get a traceback. It might be necessary to use a different test runner for this (I like much "zope.testrunner" and my wrapper "dm.zopepatches.ztest"). -- https://mail.python.org/mailman/listinfo/python-list From mal at europython.eu Mon Sep 2 06:06:05 2019 From: mal at europython.eu (M.-A. Lemburg) Date: Mon, 2 Sep 2019 12:06:05 +0200 Subject: EuroPython 2019: Please send in your feedback Message-ID: <7ef05c7d-a6ad-074b-cb60-7a82a62a6ea0@europython.eu> EuroPython 2019 is over now and so it?s time to ask around for what we can improve next year. If you attended EuroPython 2019, please take a few moments and fill in our feedback form, if you haven?t already done so: EuroPython 2019 Feedback Form * https://www.europython-society.org/feedback-2019 * We will leave the feedback form online for a few weeks and then use the information as basis for the work on EuroPython 2020 and also intend to post a summary of the multiple choice questions (not the comments to protect your privacy) on our website. Many thanks in advance. Help spread the word -------------------- Please help us spread this message by sharing it on your social networks as widely as possible. Thank you ! Link to the blog post: https://blog.europython.eu/post/187438617892/europython-2019-please-send-in-your-feedback Tweet: https://twitter.com/europython/status/1168464170716717057 Enjoy, -- EuroPython 2019 Team https://ep2019.europython.eu/ https://www.europython-society.org/ From spencerdu at hotmail.co.uk Mon Sep 2 06:32:33 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Mon, 2 Sep 2019 03:32:33 -0700 (PDT) Subject: Hi how do I import files inside a txt file? Message-ID: <23056c2a-bd63-4ee8-af2a-16a3c1aceb73@googlegroups.com> Hi How do i import files inside a txt file if they exist in the current directory? Here is the current code but I dont know how to do it correctly. import paho.mqtt.client as mqtt from mqtt import * import importlib import os import os.path # from stateMachine import * with open("list_of_devices.txt", "r") as reader: for item in reader: try: os.getcwd() print("hi") except: print("error") This is "list_of_devices.txt": test1,test2 Each name refers to a python file. Thanks Spencer From pankaj.jangid at gmail.com Mon Sep 2 07:35:50 2019 From: pankaj.jangid at gmail.com (Pankaj Jangid) Date: Mon, 02 Sep 2019 17:05:50 +0530 Subject: Hi how do I import files inside a txt file? References: <23056c2a-bd63-4ee8-af2a-16a3c1aceb73@googlegroups.com> Message-ID: Spencer Du writes: > How do i import files inside a txt file if they exist in the current directory? > > Here is the current code but I dont know how to do it correctly. > > import paho.mqtt.client as mqtt > from mqtt import * > import importlib > import os > import os.path > # from stateMachine import * > > with open("list_of_devices.txt", "r") as reader: > for item in reader: > try: > os.getcwd() > print("hi") > except: > print("error") > > This is "list_of_devices.txt": > test1,test2 > > Each name refers to a python file. > My interpretation is that you want to read a file (list_of_devices.txt) and this file contains names of other files and you want to read those files as well and do something with them (read or print or whatever). You can approach it like this: write a function to read a file and work on it. Like this, def fn(fname): with open(fname, "r") as f: try: # work with f except: print("error") Then use this function in your code that you have writen. Like this with open("list_of_devices.txt", "r") as reader: for item in reader: try: fn(item) except: print("error") In the example that you gave, you have written contents of "list_of_devices.txt" as test1,test2 Take care to read them as comma separated. Or if you have control then write them on separate lines. Regards. -- Pankaj Jangid From spencerdu at hotmail.co.uk Mon Sep 2 08:42:20 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Mon, 2 Sep 2019 05:42:20 -0700 (PDT) Subject: Hi how do I import files inside a txt file? In-Reply-To: References: <23056c2a-bd63-4ee8-af2a-16a3c1aceb73@googlegroups.com> Message-ID: <515d16ad-403e-4616-be97-d4302b36833a@googlegroups.com> On Monday, 2 September 2019 13:36:06 UTC+2, Pankaj Jangid wrote: > Spencer Du writes: > > > How do i import files inside a txt file if they exist in the current directory? > > > > Here is the current code but I dont know how to do it correctly. > > > > import paho.mqtt.client as mqtt > > from mqtt import * > > import importlib > > import os > > import os.path > > # from stateMachine import * > > > > with open("list_of_devices.txt", "r") as reader: > > for item in reader: > > try: > > os.getcwd() > > print("hi") > > except: > > print("error") > > > > This is "list_of_devices.txt": > > test1,test2 > > > > Each name refers to a python file. > > > My interpretation is that you want to read a file (list_of_devices.txt) > and this file contains names of other files and you want to read those > files as well and do something with them (read or print or whatever). > > You can approach it like this: write a function to read a file and work > on it. Like this, > > def fn(fname): > with open(fname, "r") as f: > try: > # work with f > except: > print("error") > > Then use this function in your code that you have writen. Like this > > with open("list_of_devices.txt", "r") as reader: > for item in reader: > try: > fn(item) > except: > print("error") > > In the example that you gave, you have written contents of > "list_of_devices.txt" as > > test1,test2 > > Take care to read them as comma separated. Or if you have control then > write them on separate lines. > > Regards. > -- > Pankaj Jangid Hi Pankaj I dont understand so what is complete code then? Thanks Spencer From spencerdu at hotmail.co.uk Mon Sep 2 08:44:39 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Mon, 2 Sep 2019 05:44:39 -0700 (PDT) Subject: Hi how do I import files inside a txt file? In-Reply-To: References: <23056c2a-bd63-4ee8-af2a-16a3c1aceb73@googlegroups.com> Message-ID: <88392fa3-8c2a-482a-8af3-35eb0171bb75@googlegroups.com> On Monday, 2 September 2019 13:36:06 UTC+2, Pankaj Jangid wrote: > Spencer Du writes: > > > How do i import files inside a txt file if they exist in the current directory? > > > > Here is the current code but I dont know how to do it correctly. > > > > import paho.mqtt.client as mqtt > > from mqtt import * > > import importlib > > import os > > import os.path > > # from stateMachine import * > > > > with open("list_of_devices.txt", "r") as reader: > > for item in reader: > > try: > > os.getcwd() > > print("hi") > > except: > > print("error") > > > > This is "list_of_devices.txt": > > test1,test2 > > > > Each name refers to a python file. > > > My interpretation is that you want to read a file (list_of_devices.txt) > and this file contains names of other files and you want to read those > files as well and do something with them (read or print or whatever). > > You can approach it like this: write a function to read a file and work > on it. Like this, > > def fn(fname): > with open(fname, "r") as f: > try: > # work with f > except: > print("error") > > Then use this function in your code that you have writen. Like this > > with open("list_of_devices.txt", "r") as reader: > for item in reader: > try: > fn(item) > except: > print("error") > > In the example that you gave, you have written contents of > "list_of_devices.txt" as > > test1,test2 > > Take care to read them as comma separated. Or if you have control then > write them on separate lines. > > Regards. > -- > Pankaj Jangid Hi I dont really understand this. So what would be the complete code? Also I want to import files if it exists based on what is in the txt file. Thanks Spencer From joel.goldstick at gmail.com Mon Sep 2 09:03:22 2019 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 2 Sep 2019 09:03:22 -0400 Subject: Hi how do I import files inside a txt file? In-Reply-To: <515d16ad-403e-4616-be97-d4302b36833a@googlegroups.com> References: <23056c2a-bd63-4ee8-af2a-16a3c1aceb73@googlegroups.com> <515d16ad-403e-4616-be97-d4302b36833a@googlegroups.com> Message-ID: On Mon, Sep 2, 2019 at 8:46 AM Spencer Du wrote: > > On Monday, 2 September 2019 13:36:06 UTC+2, Pankaj Jangid wrote: > > Spencer Du writes: > > > > > How do i import files inside a txt file if they exist in the current directory? > > > > > > Here is the current code but I dont know how to do it correctly. > > > > > > import paho.mqtt.client as mqtt > > > from mqtt import * > > > import importlib > > > import os > > > import os.path > > > # from stateMachine import * > > > > > > with open("list_of_devices.txt", "r") as reader: > > > for item in reader: > > > try: > > > os.getcwd() > > > print("hi") > > > except: > > > print("error") > > > > > > This is "list_of_devices.txt": > > > test1,test2 > > > > > > Each name refers to a python file. > > > > > My interpretation is that you want to read a file (list_of_devices.txt) > > and this file contains names of other files and you want to read those > > files as well and do something with them (read or print or whatever). > > > > You can approach it like this: write a function to read a file and work > > on it. Like this, > > > > def fn(fname): > > with open(fname, "r") as f: > > try: > > # work with f > > except: > > print("error") > > > > Then use this function in your code that you have writen. Like this > > > > with open("list_of_devices.txt", "r") as reader: > > for item in reader: > > try: > > fn(item) > > except: > > print("error") > > > > In the example that you gave, you have written contents of > > "list_of_devices.txt" as > > > > test1,test2 > > > > Take care to read them as comma separated. Or if you have control then > > write them on separate lines. > > > > Regards. > > -- > > Pankaj Jangid > > Hi Pankaj > > I dont understand so what is complete code then? > > Thanks > Spencer > -- > https://mail.python.org/mailman/listinfo/python-list Pardon me for guessing, but your question seems to imply that you know how you want to do something .. but I'm not sure you have tackled your problem correctly. My guess is: Depending upon the names listed in a text file, you want to do different imports into your program. You don't yet know how to read a file with python. First, when you run your program, python compiles it in order. Since you don't know what you want to import until after you run your program, you can't import those modules. You may be able to run a program to read the module list, then have it output to a new file the code you eventually want to run based on the modules you discovered. That sounds cute in a way, but probably not in a good way. You could also surround import statements with try/except code that will import what it can, and alert you when it can't Can you give us the bigger picture of what you want to accomplish? This might lead to a better solution than the one you are thinking of now -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From spencerdu at hotmail.co.uk Mon Sep 2 09:17:11 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Mon, 2 Sep 2019 06:17:11 -0700 (PDT) Subject: Hi how do I import files inside a txt file? In-Reply-To: References: <23056c2a-bd63-4ee8-af2a-16a3c1aceb73@googlegroups.com> <515d16ad-403e-4616-be97-d4302b36833a@googlegroups.com> Message-ID: <3edcbeed-96b7-4c6e-be87-a3bf5fed8efd@googlegroups.com> On Monday, 2 September 2019 15:03:52 UTC+2, Joel Goldstick wrote: > On Mon, Sep 2, 2019 at 8:46 AM Spencer Du wrote: > > > > On Monday, 2 September 2019 13:36:06 UTC+2, Pankaj Jangid wrote: > > > Spencer Du writes: > > > > > > > How do i import files inside a txt file if they exist in the current directory? > > > > > > > > Here is the current code but I dont know how to do it correctly. > > > > > > > > import paho.mqtt.client as mqtt > > > > from mqtt import * > > > > import importlib > > > > import os > > > > import os.path > > > > # from stateMachine import * > > > > > > > > with open("list_of_devices.txt", "r") as reader: > > > > for item in reader: > > > > try: > > > > os.getcwd() > > > > print("hi") > > > > except: > > > > print("error") > > > > > > > > This is "list_of_devices.txt": > > > > test1,test2 > > > > > > > > Each name refers to a python file. > > > > > > > My interpretation is that you want to read a file (list_of_devices.txt) > > > and this file contains names of other files and you want to read those > > > files as well and do something with them (read or print or whatever). > > > > > > You can approach it like this: write a function to read a file and work > > > on it. Like this, > > > > > > def fn(fname): > > > with open(fname, "r") as f: > > > try: > > > # work with f > > > except: > > > print("error") > > > > > > Then use this function in your code that you have writen. Like this > > > > > > with open("list_of_devices.txt", "r") as reader: > > > for item in reader: > > > try: > > > fn(item) > > > except: > > > print("error") > > > > > > In the example that you gave, you have written contents of > > > "list_of_devices.txt" as > > > > > > test1,test2 > > > > > > Take care to read them as comma separated. Or if you have control then > > > write them on separate lines. > > > > > > Regards. > > > -- > > > Pankaj Jangid > > > > Hi Pankaj > > > > I dont understand so what is complete code then? > > > > Thanks > > Spencer > > -- > > https://mail.python.org/mailman/listinfo/python-list > > Pardon me for guessing, but your question seems to imply that you know > how you want to do something .. but I'm not sure you have tackled your > problem correctly. > > My guess is: Depending upon the names listed in a text file, you want > to do different imports into your program. You don't yet know how to > read a file with python. > > First, when you run your program, python compiles it in order. Since > you don't know what you want to import until after you run your > program, you can't import those modules. You may be able to run a > program to read the module list, then have it output to a new file the > code you eventually want to run based on the modules you discovered. > That sounds cute in a way, but probably not in a good way. You could > also surround import statements with try/except code that will import > what it can, and alert you when it can't > > Can you give us the bigger picture of what you want to accomplish? > This might lead to a better solution than the one you are thinking of > now > > -- > Joel Goldstick > http://joelgoldstick.com/blog > http://cc-baseballstats.info/stats/birthdays Hi I have a txt file which contains the names of files. They are .py files. I want to import them into a python file if they exists in current directory and if the name of file does not exist then print out error and not import. How do I do this? Thanks Spencer From hongyi.zhao at gmail.com Mon Sep 2 09:20:25 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Mon, 2 Sep 2019 13:20:25 +0000 (UTC) Subject: Execute complex shell commands within python and obtain the output. Message-ID: Hi, I try to execute some complex shell commands with in python and obtain the output, I tried the following method: For python 3.x: import subprocess cmd= some_complex_command_with_pipe_and_others ps = subprocess.Popen (cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT) output = ps.communicate()[0].decode('utf8') Is this the correct usage for this case? Regards From joel.goldstick at gmail.com Mon Sep 2 09:28:40 2019 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 2 Sep 2019 09:28:40 -0400 Subject: Hi how do I import files inside a txt file? In-Reply-To: <3edcbeed-96b7-4c6e-be87-a3bf5fed8efd@googlegroups.com> References: <23056c2a-bd63-4ee8-af2a-16a3c1aceb73@googlegroups.com> <515d16ad-403e-4616-be97-d4302b36833a@googlegroups.com> <3edcbeed-96b7-4c6e-be87-a3bf5fed8efd@googlegroups.com> Message-ID: On Mon, Sep 2, 2019 at 9:21 AM Spencer Du wrote: > > On Monday, 2 September 2019 15:03:52 UTC+2, Joel Goldstick wrote: > > On Mon, Sep 2, 2019 at 8:46 AM Spencer Du wrote: > > > > > > On Monday, 2 September 2019 13:36:06 UTC+2, Pankaj Jangid wrote: > > > > Spencer Du writes: > > > > > > > > > How do i import files inside a txt file if they exist in the current directory? > > > > > > > > > > Here is the current code but I dont know how to do it correctly. > > > > > > > > > > import paho.mqtt.client as mqtt > > > > > from mqtt import * > > > > > import importlib > > > > > import os > > > > > import os.path > > > > > # from stateMachine import * > > > > > > > > > > with open("list_of_devices.txt", "r") as reader: > > > > > for item in reader: > > > > > try: > > > > > os.getcwd() > > > > > print("hi") > > > > > except: > > > > > print("error") > > > > > > > > > > This is "list_of_devices.txt": > > > > > test1,test2 > > > > > > > > > > Each name refers to a python file. > > > > > > > > > My interpretation is that you want to read a file (list_of_devices.txt) > > > > and this file contains names of other files and you want to read those > > > > files as well and do something with them (read or print or whatever). > > > > > > > > You can approach it like this: write a function to read a file and work > > > > on it. Like this, > > > > > > > > def fn(fname): > > > > with open(fname, "r") as f: > > > > try: > > > > # work with f > > > > except: > > > > print("error") > > > > > > > > Then use this function in your code that you have writen. Like this > > > > > > > > with open("list_of_devices.txt", "r") as reader: > > > > for item in reader: > > > > try: > > > > fn(item) > > > > except: > > > > print("error") > > > > > > > > In the example that you gave, you have written contents of > > > > "list_of_devices.txt" as > > > > > > > > test1,test2 > > > > > > > > Take care to read them as comma separated. Or if you have control then > > > > write them on separate lines. > > > > > > > > Regards. > > > > -- > > > > Pankaj Jangid > > > > > > Hi Pankaj > > > > > > I dont understand so what is complete code then? > > > > > > Thanks > > > Spencer > > > -- > > > https://mail.python.org/mailman/listinfo/python-list > > > > Pardon me for guessing, but your question seems to imply that you know > > how you want to do something .. but I'm not sure you have tackled your > > problem correctly. > > > > My guess is: Depending upon the names listed in a text file, you want > > to do different imports into your program. You don't yet know how to > > read a file with python. > > > > First, when you run your program, python compiles it in order. Since > > you don't know what you want to import until after you run your > > program, you can't import those modules. You may be able to run a > > program to read the module list, then have it output to a new file the > > code you eventually want to run based on the modules you discovered. > > That sounds cute in a way, but probably not in a good way. You could > > also surround import statements with try/except code that will import > > what it can, and alert you when it can't > > > > Can you give us the bigger picture of what you want to accomplish? > > This might lead to a better solution than the one you are thinking of > > now > > > > -- > > Joel Goldstick > > http://joelgoldstick.com/blog > > http://cc-baseballstats.info/stats/birthdays > > Hi > > I have a txt file which contains the names of files. They are .py files. I want to import them into a python file if they exists in current directory and if the name of file does not exist then print out error and not import. How do I do this? > > Thanks > Spencer Here is a discussion on Stack overflow that lays out how you can dynamically import files. This should get you started in the right direction. First, see if you can write code to read the file, and retrieve the names of the modules you want to import. Come back if you stumble with your code for that https://stackoverflow.com/questions/301134/how-to-import-a-module-given-its-name-as-string > -- > https://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From spencerdu at hotmail.co.uk Mon Sep 2 09:48:13 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Mon, 2 Sep 2019 06:48:13 -0700 (PDT) Subject: Hi how do I import files inside a txt file? In-Reply-To: References: <23056c2a-bd63-4ee8-af2a-16a3c1aceb73@googlegroups.com> <515d16ad-403e-4616-be97-d4302b36833a@googlegroups.com> <3edcbeed-96b7-4c6e-be87-a3bf5fed8efd@googlegroups.com> Message-ID: On Monday, 2 September 2019 15:29:07 UTC+2, Joel Goldstick wrote: > On Mon, Sep 2, 2019 at 9:21 AM Spencer Du wrote: > > > > On Monday, 2 September 2019 15:03:52 UTC+2, Joel Goldstick wrote: > > > On Mon, Sep 2, 2019 at 8:46 AM Spencer Du wrote: > > > > > > > > On Monday, 2 September 2019 13:36:06 UTC+2, Pankaj Jangid wrote: > > > > > Spencer Du writes: > > > > > > > > > > > How do i import files inside a txt file if they exist in the current directory? > > > > > > > > > > > > Here is the current code but I dont know how to do it correctly. > > > > > > > > > > > > import paho.mqtt.client as mqtt > > > > > > from mqtt import * > > > > > > import importlib > > > > > > import os > > > > > > import os.path > > > > > > # from stateMachine import * > > > > > > > > > > > > with open("list_of_devices.txt", "r") as reader: > > > > > > for item in reader: > > > > > > try: > > > > > > os.getcwd() > > > > > > print("hi") > > > > > > except: > > > > > > print("error") > > > > > > > > > > > > This is "list_of_devices.txt": > > > > > > test1,test2 > > > > > > > > > > > > Each name refers to a python file. > > > > > > > > > > > My interpretation is that you want to read a file (list_of_devices.txt) > > > > > and this file contains names of other files and you want to read those > > > > > files as well and do something with them (read or print or whatever). > > > > > > > > > > You can approach it like this: write a function to read a file and work > > > > > on it. Like this, > > > > > > > > > > def fn(fname): > > > > > with open(fname, "r") as f: > > > > > try: > > > > > # work with f > > > > > except: > > > > > print("error") > > > > > > > > > > Then use this function in your code that you have writen. Like this > > > > > > > > > > with open("list_of_devices.txt", "r") as reader: > > > > > for item in reader: > > > > > try: > > > > > fn(item) > > > > > except: > > > > > print("error") > > > > > > > > > > In the example that you gave, you have written contents of > > > > > "list_of_devices.txt" as > > > > > > > > > > test1,test2 > > > > > > > > > > Take care to read them as comma separated. Or if you have control then > > > > > write them on separate lines. > > > > > > > > > > Regards. > > > > > -- > > > > > Pankaj Jangid > > > > > > > > Hi Pankaj > > > > > > > > I dont understand so what is complete code then? > > > > > > > > Thanks > > > > Spencer > > > > -- > > > > https://mail.python.org/mailman/listinfo/python-list > > > > > > Pardon me for guessing, but your question seems to imply that you know > > > how you want to do something .. but I'm not sure you have tackled your > > > problem correctly. > > > > > > My guess is: Depending upon the names listed in a text file, you want > > > to do different imports into your program. You don't yet know how to > > > read a file with python. > > > > > > First, when you run your program, python compiles it in order. Since > > > you don't know what you want to import until after you run your > > > program, you can't import those modules. You may be able to run a > > > program to read the module list, then have it output to a new file the > > > code you eventually want to run based on the modules you discovered. > > > That sounds cute in a way, but probably not in a good way. You could > > > also surround import statements with try/except code that will import > > > what it can, and alert you when it can't > > > > > > Can you give us the bigger picture of what you want to accomplish? > > > This might lead to a better solution than the one you are thinking of > > > now > > > > > > -- > > > Joel Goldstick > > > http://joelgoldstick.com/blog > > > http://cc-baseballstats.info/stats/birthdays > > > > Hi > > > > I have a txt file which contains the names of files. They are .py files. I want to import them into a python file if they exists in current directory and if the name of file does not exist then print out error and not import. How do I do this? > > > > Thanks > > Spencer > > Here is a discussion on Stack overflow that lays out how you can > dynamically import files. This should get you started in the right > direction. First, see if you can write code to read the file, and > retrieve the names of the modules you want to import. Come back if > you stumble with your code for that > > https://stackoverflow.com/questions/301134/how-to-import-a-module-given-its-name-as-string > > -- > > https://mail.python.org/mailman/listinfo/python-list > > > > -- > Joel Goldstick > http://joelgoldstick.com/blog > http://cc-baseballstats.info/stats/birthdays Ok I have this code to retrieve the names of modules I want to import. Now how do I check if they exist in current directory and if they exist import them into the python program. Thanks. with open("list_of_devices.txt", "r") as f: for item in f: print(item) From dieter at handshake.de Mon Sep 2 10:28:50 2019 From: dieter at handshake.de (dieter) Date: Mon, 02 Sep 2019 16:28:50 +0200 Subject: Problem while integrating unittest with setuptools References: <010F3B60-6AE1-420F-94BA-63C3CC3723A8@contoso.com> <87mufn8f05.fsf@handshake.de> Message-ID: <87a7bmlql9.fsf@handshake.de> YuXuan Dong writes: > I have uninstalled `six` using `pip uninstall six` but the problem is still there. Your traceback shows that `six` does not cause your problem. It is quite obvious that a `test_winreg` will want to load the `wingreg` module. > As you suggested, I have checked the traceback and found the exception is caused by `/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/test/test_winreg.py` on my machine with homebrew-installed Python3. > > I have realized that the problem may be caused of `test-suite=test` in my `setup.py`. `setuptools` will find the `test` package. But the package it found is not in my project. It found `/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/test`. Yes. Apparently, you run the Python test suite - and depending on platform and available infrastructure some of its tests fail. In my package `dm.xmlsec.binding` I use `test_suite='dm.xmlsec.binding.tests.testsuite'`. Maybe, you can ensure to get the correct test suite in a similar way for your package. From spencerdu at hotmail.co.uk Mon Sep 2 11:05:14 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Mon, 2 Sep 2019 08:05:14 -0700 (PDT) Subject: Help needed urgently for running some code!!!! Message-ID: <50e14ca8-bf6a-4443-8c7d-74b4d1069247@googlegroups.com> Hi I want to execute "from devicesEmbedded import *": in GUI.py after all code in GUI.py is run. Also how do I make the devicesEmbedded.py reload and run when a txt file is created in the name of "list_of_devices.txt" in the GUI.py python program. GUI.py import logging from datetime import timedelta import time from thespian.actors import * from transitions import Machine import paho.mqtt.client as mqtt import importlib import os import os.path import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5 import QtWidgets, uic from mqtt import * # from devicesEmbedded import * import json class MainWindow(QtWidgets.QMainWindow): def __init__(self,parent = None): QMainWindow.__init__(self) super(MainWindow, self).__init__(parent) self.mdi = QMdiArea() self.setCentralWidget(self.mdi) self.setMinimumSize(QSize(800, 600)) self.setWindowTitle("PyQt button example - pythonprogramminglanguage.com") pybutton = QPushButton('Add device', self) pybutton.clicked.connect(self.importbutton) pybutton.move(100, 400) pybutton.resize(150, 32) self.textbox = QLineEdit(self) self.textbox.move(100,350) self.textbox.resize(100, 32) self.fileName_UI = "" def importbutton(self): self.fileName_UI = self.textbox.text() self.loadGUI() def getGUIFilename(self): return self.fileName_UI def loadGUI(self): print("Searching file", self.fileName_UI) try: module = __import__(self.fileName_UI) my_class = getattr(module, "SubWindow") sub = QMdiSubWindow() sub.setWidget(my_class()) sub.setWindowTitle("New GUI: " + self.fileName_UI) self.mdi.addSubWindow(sub) sub.show() print("creating new instance " + self.fileName_UI) client = device("Device") client.run() client.loop_start() # start the loop # device_message = self.fileName_UI time.sleep(2) print("Subscribing to topic", "microscope/light_sheet_microscope/UI") client.subscribe("microscope/light_sheet_microscope/UI") print("Publishing message to topic", "microscope/light_sheet_microscope/UI/list_of_devices") client.publish("microscope/light_sheet_microscope/UI/list_of_devices", json.dumps({"type": "device", "payload":{"name": self.fileName_UI, "cmd": "adding device"}}, indent=2)) time.sleep(1) # wait client.loop_stop() # stop the loop print("Device added" + "\n") client.run() client.loop_start() time.sleep(2) print("Subscribing to topic", "microscope/light_sheet_microscope/UI/list_of_devices") client.subscribe("microscope/light_sheet_microscope/UI/list_of_devices") print("Publishing message to topic", "microscope/light_sheet_microscope/UI/list_of_devices") client.publish("microscope/light_sheet_microscope/UI/list_of_devices", self.fileName_UI + " added to device list") time.sleep(1) client.loop_stop() listofdevices = [] listofdevices.append(self.fileName_UI) with open("list_of_devices.txt", "a+") as myfile: for item in listofdevices: myfile.write(item + ",") print(item) print(listofdevices) print("Device added to list") except: print("creating new instance " + self.fileName_UI) client = device("Device") client.run() client.loop_start() # start the loop device_message = self.fileName_UI time.sleep(2) print("Subscribing to topic", "microscope/light_sheet_microscope/UI") client.subscribe("microscope/light_sheet_microscope/UI") print("Publishing message to topic", "microscope/light_sheet_microscope/UI") client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": self.fileName_UI}}, indent=2)) time.sleep(2) # wait client.loop_stop() # stop the loop print(device_message + ".py " + "file doesn't exist") print("Device not added") if __name__ == "__main__": app = QApplication(sys.argv) mainWin = MainWindow() a = mainWin.show() try: mainWin.show() os.remove("list_of_devices.txt") print("Awaiting devices to be launched") except: print("Awaiting devices to be launched") publishedMessage = mainWin.getGUIFilename() sys.exit(app.exec_()) devicesEmbedded.py: import random import asyncio from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference from mqtt import * # from GUI import * client = device("Device") client.run() client.loop_start() time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI/list_of_devices") client.subscribe("microscope/light_sheet_microscope/UI/list_of_devices") client.loop_stop() # stop the loop def readFile(fname): try: with open(fname, "r") as f: for item in f: print(item) except: print("No devices added yet") readFile("list_of_devices.txt") mqtt.py import logging from datetime import timedelta import time from thespian.actors import * from transitions import Machine import paho.mqtt.client as mqtt import importlib import os.path import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5 import QtWidgets, uic class device(mqtt.Client): def on_connect(self, mqttc, obj, flags, rc): if rc == 0: print("Connected to broker") else: print("Connection failed") # mqttc.subscribe("microscope/light_sheet_microscope/UI") def on_message(self, mqttc, userdata, message): msg = str(message.payload.decode("utf-8")) print("message recieved= " + msg) # print("File which you want to import(with .py extension)") print("message topic=", message.topic) print("message qos=", message.qos) print("message retain flag=", message.retain) def run(self): self.connect("broker.hivemq.com", 1883, 60) Thanks From spencerdu at hotmail.co.uk Mon Sep 2 11:47:48 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Mon, 2 Sep 2019 08:47:48 -0700 (PDT) Subject: Help needed to run some code!!!! Message-ID: <4815c2dc-1800-4aca-bd0c-81fcfc3bfea1@googlegroups.com> Hi How can I execute "from devicesEmbedded import *" after this: "print("Device added to list")" in GUI.py because currently if I have the import added at the top of GUI.py file it always executes first before the GUI.py file is executed. I want the devicesEmbedded.py to execute after GUI.py has executed first. Thanks Spencer GUI.py import logging from datetime import timedelta import time from thespian.actors import * from transitions import Machine import paho.mqtt.client as mqtt import importlib import os import os.path import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5 import QtWidgets, uic from mqtt import * # from devicesEmbedded import * import json class MainWindow(QtWidgets.QMainWindow): def __init__(self,parent = None): QMainWindow.__init__(self) super(MainWindow, self).__init__(parent) self.mdi = QMdiArea() self.setCentralWidget(self.mdi) self.setMinimumSize(QSize(800, 600)) self.setWindowTitle("PyQt button example - pythonprogramminglanguage.com") pybutton = QPushButton('Add device', self) pybutton.clicked.connect(self.importbutton) pybutton.move(100, 400) pybutton.resize(150, 32) self.textbox = QLineEdit(self) self.textbox.move(100,350) self.textbox.resize(100, 32) self.fileName_UI = "" def importbutton(self): self.fileName_UI = self.textbox.text() self.loadGUI() def getGUIFilename(self): return self.fileName_UI def loadGUI(self): print("Searching file", self.fileName_UI) try: module = __import__(self.fileName_UI) my_class = getattr(module, "SubWindow") sub = QMdiSubWindow() sub.setWidget(my_class()) sub.setWindowTitle("New GUI: " + self.fileName_UI) self.mdi.addSubWindow(sub) sub.show() print("creating new instance " + self.fileName_UI) client = device("Device") client.run() client.loop_start() # start the loop # device_message = self.fileName_UI time.sleep(2) print("Subscribing to topic", "microscope/light_sheet_microscope/UI") client.subscribe("microscope/light_sheet_microscope/UI") print("Publishing message to topic", "microscope/light_sheet_microscope/UI/list_of_devices") client.publish("microscope/light_sheet_microscope/UI/list_of_devices", json.dumps({"type": "device", "payload":{"name": self.fileName_UI, "cmd": "adding device"}}, indent=2)) time.sleep(1) # wait client.loop_stop() # stop the loop print("Device added" + "\n") client.run() client.loop_start() time.sleep(2) print("Subscribing to topic", "microscope/light_sheet_microscope/UI/list_of_devices") client.subscribe("microscope/light_sheet_microscope/UI/list_of_devices") print("Publishing message to topic", "microscope/light_sheet_microscope/UI/list_of_devices") client.publish("microscope/light_sheet_microscope/UI/list_of_devices", self.fileName_UI + " added to device list") time.sleep(1) client.loop_stop() listofdevices = [] listofdevices.append(self.fileName_UI) with open("list_of_devices.txt", "a+") as myfile: for item in listofdevices: myfile.write(item + ",") print(item) print(listofdevices) print("Device added to list") except: print("creating new instance " + self.fileName_UI) client = device("Device") client.run() client.loop_start() # start the loop device_message = self.fileName_UI time.sleep(2) print("Subscribing to topic", "microscope/light_sheet_microscope/UI") client.subscribe("microscope/light_sheet_microscope/UI") print("Publishing message to topic", "microscope/light_sheet_microscope/UI") client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": "device", "payload":{"name": self.fileName_UI}}, indent=2)) time.sleep(2) # wait client.loop_stop() # stop the loop print(device_message + ".py " + "file doesn't exist") print("Device not added") if __name__ == "__main__": app = QApplication(sys.argv) mainWin = MainWindow() a = mainWin.show() try: mainWin.show() os.remove("list_of_devices.txt") print("Awaiting devices to be launched") except: print("Awaiting devices to be launched") publishedMessage = mainWin.getGUIFilename() sys.exit(app.exec_()) devicesEmbedded.py: import random import asyncio from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference from mqtt import * # from GUI import * client = device("Device") client.run() client.loop_start() time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI/list_of_devices") client.subscribe("microscope/light_sheet_microscope/UI/list_of_devices") client.loop_stop() # stop the loop def readFile(fname): try: with open(fname, "r") as f: for item in f: print(item) except: print("No devices added yet") readFile("list_of_devices.txt") mqtt.py import logging from datetime import timedelta import time from thespian.actors import * from transitions import Machine import paho.mqtt.client as mqtt import importlib import os.path import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5 import QtWidgets, uic class device(mqtt.Client): def on_connect(self, mqttc, obj, flags, rc): if rc == 0: print("Connected to broker") else: print("Connection failed") # mqttc.subscribe("microscope/light_sheet_microscope/UI") def on_message(self, mqttc, userdata, message): msg = str(message.payload.decode("utf-8")) print("message recieved= " + msg) # print("File which you want to import(with .py extension)") print("message topic=", message.topic) print("message qos=", message.qos) print("message retain flag=", message.retain) def run(self): self.connect("broker.hivemq.com", 1883, 60) Thanks From PythonList at DancesWithMice.info Mon Sep 2 14:02:58 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Tue, 3 Sep 2019 06:02:58 +1200 Subject: Hi how do I import files inside a txt file? In-Reply-To: References: <23056c2a-bd63-4ee8-af2a-16a3c1aceb73@googlegroups.com> <515d16ad-403e-4616-be97-d4302b36833a@googlegroups.com> <3edcbeed-96b7-4c6e-be87-a3bf5fed8efd@googlegroups.com> Message-ID: On 3/09/19 1:48 AM, Spencer Du wrote: > On Monday, 2 September 2019 15:29:07 UTC+2, Joel Goldstick wrote: >> On Mon, Sep 2, 2019 at 9:21 AM Spencer Du wrote: >>> >>> On Monday, 2 September 2019 15:03:52 UTC+2, Joel Goldstick wrote: >>>> On Mon, Sep 2, 2019 at 8:46 AM Spencer Du wrote: >>>>> >>>>> On Monday, 2 September 2019 13:36:06 UTC+2, Pankaj Jangid wrote: >>>>>> Spencer Du writes: >>>>>> >>>>>>> How do i import files inside a txt file if they exist in the current directory? >>>>>>> >>>>>>> Here is the current code but I dont know how to do it correctly. >>>>>>> >>>>>>> import paho.mqtt.client as mqtt >>>>>>> from mqtt import * >>>>>>> import importlib >>>>>>> import os >>>>>>> import os.path >>>>>>> # from stateMachine import * >>>>>>> >>>>>>> with open("list_of_devices.txt", "r") as reader: >>>>>>> for item in reader: >>>>>>> try: >>>>>>> os.getcwd() >>>>>>> print("hi") >>>>>>> except: >>>>>>> print("error") >>>>>>> >>>>>>> This is "list_of_devices.txt": >>>>>>> test1,test2 >>>>>>> >>>>>>> Each name refers to a python file. >>>>>>> >>>>>> My interpretation is that you want to read a file (list_of_devices.txt) >>>>>> and this file contains names of other files and you want to read those >>>>>> files as well and do something with them (read or print or whatever). >>>>>> >>>>>> You can approach it like this: write a function to read a file and work >>>>>> on it. Like this, >>>>>> >>>>>> def fn(fname): >>>>>> with open(fname, "r") as f: >>>>>> try: >>>>>> # work with f >>>>>> except: >>>>>> print("error") >>>>>> >>>>>> Then use this function in your code that you have writen. Like this >>>>>> >>>>>> with open("list_of_devices.txt", "r") as reader: >>>>>> for item in reader: >>>>>> try: >>>>>> fn(item) >>>>>> except: >>>>>> print("error") >>>>>> >>>>>> In the example that you gave, you have written contents of >>>>>> "list_of_devices.txt" as >>>>>> >>>>>> test1,test2 >>>>>> >>>>>> Take care to read them as comma separated. Or if you have control then >>>>>> write them on separate lines. >>>>>> >>>>>> Regards. >>>>>> -- >>>>>> Pankaj Jangid >>>>> >>>>> Hi Pankaj >>>>> >>>>> I dont understand so what is complete code then? >>>>> >>>>> Thanks >>>>> Spencer >>>>> -- >>>>> https://mail.python.org/mailman/listinfo/python-list >>>> >>>> Pardon me for guessing, but your question seems to imply that you know >>>> how you want to do something .. but I'm not sure you have tackled your >>>> problem correctly. >>>> >>>> My guess is: Depending upon the names listed in a text file, you want >>>> to do different imports into your program. You don't yet know how to >>>> read a file with python. >>>> >>>> First, when you run your program, python compiles it in order. Since >>>> you don't know what you want to import until after you run your >>>> program, you can't import those modules. You may be able to run a >>>> program to read the module list, then have it output to a new file the >>>> code you eventually want to run based on the modules you discovered. >>>> That sounds cute in a way, but probably not in a good way. You could >>>> also surround import statements with try/except code that will import >>>> what it can, and alert you when it can't >>>> >>>> Can you give us the bigger picture of what you want to accomplish? >>>> This might lead to a better solution than the one you are thinking of >>>> now >>>> >>>> -- >>>> Joel Goldstick >>>> http://joelgoldstick.com/blog >>>> http://cc-baseballstats.info/stats/birthdays >>> >>> Hi >>> >>> I have a txt file which contains the names of files. They are .py files. I want to import them into a python file if they exists in current directory and if the name of file does not exist then print out error and not import. How do I do this? >>> >>> Thanks >>> Spencer >> >> Here is a discussion on Stack overflow that lays out how you can >> dynamically import files. This should get you started in the right >> direction. First, see if you can write code to read the file, and >> retrieve the names of the modules you want to import. Come back if >> you stumble with your code for that >> >> https://stackoverflow.com/questions/301134/how-to-import-a-module-given-its-name-as-string >>> -- >>> https://mail.python.org/mailman/listinfo/python-list >> >> >> >> -- >> Joel Goldstick >> http://joelgoldstick.com/blog >> http://cc-baseballstats.info/stats/birthdays > > Ok I have this code to retrieve the names of modules I want to import. Now how do I check if they exist in current directory and if they exist import them into the python program. Thanks. > > with open("list_of_devices.txt", "r") as f: > for item in f: > print(item) Perhaps it is time to slow-down and take a breather? The answer to this step has already been covered in this thread! Many languages require one to *anticipate* every contingency - anything that could go 'wrong'. For example, that a file does not exist and cannot be imported. However, Python follows a philosophy that it is "easier to ask forgiveness than it is to get permission"*. In other words, to use the try...except construct - in this case, to attempt an import (the "try") and if that fails (probably because the file does not exist) then to defend/react accordingly (the "except" - "except" = "exception"). In other words, only worry about 'the problem' (post fact), should it arise. This works quite neatly for your use-case. Such is what is called "a Python idiom" - the way things are done in Python (which may be fundamentally different from other programming language(s) you use). It is a mistake to attempt to 'translate' from one programming language into another merely by replacing one "syntax" with the other. For example a Python for-loop is actually a 'foreach' construct and thus not the same as the C/C++, FORTRAN, BASIC, etc, etc, for-loop - no matter what the words say! Learning (and absorbing) idioms and language philosophies is what separates better programmers from the mediocre! Are you taking the time to research? Have you reviewed the Python docs*? If you must check for the presence of a file, entering "python file exists" into a search engine produces numbers of 'hits'! The hard way (the syntax-only 'translation', and non-Pythonic approach), is that the PSL offers "11.2. os.path ? Common pathname manipulations"* * WebRefs: https://en.wikiquote.org/wiki/Grace_Hopper https://docs.python.org/3.6/index.html [adjust versionNR to suit] https://stackabuse.com/python-check-if-a-file-or-directory-exists/ -- Regards =dn From eryksun at gmail.com Mon Sep 2 15:49:03 2019 From: eryksun at gmail.com (Eryk Sun) Date: Mon, 2 Sep 2019 14:49:03 -0500 Subject: Issue About Install pyinstaller In-Reply-To: <5d6bee91.1c69fb81.29700.ccaf@mx.google.com> References: <5d6bee91.1c69fb81.29700.ccaf@mx.google.com> Message-ID: On 9/1/19, Mehmet Furkan ?OLAK wrote: > > I did ?cmd > pip install pyinstaller > enter? > > from C:\Users\Furkan ?OLAK\AppData\Local\Programs\Python\Python37-32 > but there isnt Script folder. > > ?n cmd I see this kind of ERROR > 'pip' is not recognized as an internal or external command, > operable program or batch file. CMD failed to find a `pip` command when it searched the directories in the PATH environment variable for "pip" plus each file extension in the PATHEXT environment variable. To locate all of the "pip.exe" files in the "%LocalAppData%\Programs" tree, use dir /b /s "%LocalAppData%\Programs\pip.exe" or where.exe /r "%LocalAppData%\Programs" pip.exe Then either run pip.exe using its fully-qualified path, or temporarily add the parent directory to PATH. For example: set PATH=%PATH%;%LocalAppData%\Programs\Python\Python37-32\Scripts You can permanently modify PATH using the system environment-variable editor (e.g. via control panel -> system -> advanced system settings -> environment variables). Note that directories in PATH should never be quoted. Also, Python's installer has an advanced option to add the scripts directory to PATH (i.e. "add Python to environment variables"). You can rerun the installer and modify the installation to select this option. From eryksun at gmail.com Mon Sep 2 16:24:23 2019 From: eryksun at gmail.com (Eryk Sun) Date: Mon, 2 Sep 2019 15:24:23 -0500 Subject: "Edit With Python" option missing In-Reply-To: References: Message-ID: On 8/31/19, Akash verma wrote: > "Edit With Python" option missing from message context when right clicked > with mouse . If you mean "Edit with IDLE", this means that you've changed the file association. It's no longer using the default "Python.File" program identifier (progid). Create an empty .py script on your desktop. Right click it, and select "Open with" -> "Choose another app". Select the "Python" app that has the Python logo with a rocket on it. At the bottom of the dialog, select the option to "Always use this app to open .py files". Click "OK". This can happen if some other program grabbed the file association -- perhaps automatically, or perhaps you approved the change and don't remember it. It can also be the case that a user mistakenly tries to fix this by selecting the "Look for an app on this PC" option in the "Open with" dialog, which auto-generates a progid for the file association. (1) You probably do not want an auto-generated progid because you'll lose installed features of the application. In the case of Python, that's the "Edit with IDLE" menu and the drop-handler shell extension that enables dragging and dropping files on a script in Explorer. Also, if you end up selecting an installed "python.exe" instead of the "py.exe" launcher, you'll lose shebang support in scripts. (2) Windows creates a progid that's intended for opening a data file. It doesn't have `*%` in the template, so passing command-line arguments won't work. That said, if you do end up associating .py files with an auto-generated progid, at least now you know a simple way to switch back to the original progid. From cs at cskk.id.au Mon Sep 2 18:24:17 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 3 Sep 2019 08:24:17 +1000 Subject: Execute complex shell commands within python and obtain the output. In-Reply-To: References: Message-ID: <20190902222417.GA27782@cskk.homeip.net> On 02Sep2019 13:20, Hongyi Zhao wrote: >I try to execute some complex shell commands with in python and obtain >the output, I tried the following method: > >For python 3.x: > >import subprocess >cmd= some_complex_command_with_pipe_and_others >ps = subprocess.Popen >(cmd,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT) >output = ps.communicate()[0].decode('utf8') > >Is this the correct usage for this case? It seems reasonable to me, but I would not use stderr=subprocess.STDOUT. Why did you do that? The stderr stream pretty much exists to avoid polluting stdout with error messages. For really complex stuff you're often better writing a shell script and invoking that script, and avoiding shell=True. This has the advantages that: (a) it avoids shell=True, a common source of accidents (b) you don't have to embed shell punctuation in a Python string, which itself has punctuation (c) you can run the script yourself by hand for testing purposes, outside the Python programme (d) you're not restricted to the shell; the script might be in awk or any number of other languages Finally, the .decode('utf8') assumes your locale is UTF8 based. It probably is, but if it isn't then you may get mojibake. Cheers, Cameron Simpson From cs at cskk.id.au Mon Sep 2 18:28:14 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 3 Sep 2019 08:28:14 +1000 Subject: PYTHON DIDNT DETECTED In-Reply-To: <1567276258.150346035@f117.i.mail.ru> References: <1567276258.150346035@f117.i.mail.ru> Message-ID: <20190902222814.GA72467@cskk.homeip.net> On 31Aug2019 21:30, ????? ??????? wrote: >Traceback (most recent call last): >File "", line 1, in >NameError: name 'python' is not defined and more? You seem to have left off the command which gave this error message. >C:\Users\VeNoMD>python -v [..."python" from your command prompt works...] I would guess that you hare put the command "python" _inside_ a python script. The command "python" is for use _outside_ the script, to invoke the script. We'd need to see what you did, and your script, to offer better advice. Cheers, Cameron Simpson From cs at cskk.id.au Mon Sep 2 18:29:31 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 3 Sep 2019 08:29:31 +1000 Subject: Append some stuff into a file with only the last appended line reserved. In-Reply-To: References: Message-ID: <20190902222931.GA80784@cskk.homeip.net> On 01Sep2019 04:13, Hongyi Zhao wrote: >I want to append some log of pycurl's downloading info to file, and I >only want to reserve the last appended line when write. How to do this? Please describe this in more detail. Present a little pycurl output and then explain what portion of it should land in the log file. Cheers, Cameron Simpson From rmlibre at riseup.net Mon Sep 2 23:28:40 2019 From: rmlibre at riseup.net (rmlibre at riseup.net) Date: Mon, 02 Sep 2019 20:28:40 -0700 Subject: GPG wrapper, ECC 25519 compatible? Message-ID: <7cf14a1add0468bcf0193fa6c6cf7f03@riseup.net> I'm looking for a non-gui GPG wrapper that supports elliptic-curve 25519 for development. The only one's I've seen that support ECC, only support the p-XYZ curves created by NIST. I've tried monkey-patching python-gnupg to add 25519 support, but I can't get my head around that codebase. Or if you have solid advice as to how I'd go about writing my own, that'd be wonderful. Since, I've also tried many versions of creating a new wrapper package. But I just don't understand how to get and pass information back to the gpg command line prompts at all, not to mention automating the process. On linux, you can see the command line prompts I'm trying to work with by running: >gpg2 --expert --full-gen-key The default responses I'd like to give are: >11 >Q >1 >0 >y >username >username at user.net >none >O Any tips that don't include using a different curve? From hongyi.zhao at gmail.com Mon Sep 2 23:59:06 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Tue, 3 Sep 2019 03:59:06 +0000 (UTC) Subject: Execute complex shell commands within python and obtain the output. References: <20190902222417.GA27782@cskk.homeip.net> Message-ID: On Tue, 03 Sep 2019 08:24:17 +1000, Cameron Simpson wrote: > It seems reasonable to me, but I would not use stderr=subprocess.STDOUT. > Why did you do that? The stderr stream pretty much exists to avoid > polluting stdout with error messages. Thanks for your suggestion. > > For really complex stuff you're often better writing a shell script and > invoking that script, and avoiding shell=True. This has the advantages > that: > > (a) it avoids shell=True, a common source of accidents > > (b) you don't have to embed shell punctuation in a Python string, which > itself has punctuation > > (c) you can run the script yourself by hand for testing purposes, > outside the Python programme > > (d) you're not restricted to the shell; the script might be in awk or > any number of other languages > > Finally, the .decode('utf8') assumes your locale is UTF8 based. It > probably is, but if it isn't then you may get mojibake. Nowadays, most of the os use utf8 as the default locale. Am I wrong? From yuxuan.dong at outlook.com Mon Sep 2 14:08:00 2019 From: yuxuan.dong at outlook.com (YuXuan Dong) Date: Mon, 2 Sep 2019 18:08:00 +0000 Subject: Problem while integrating unittest with setuptools In-Reply-To: <87a7bmlql9.fsf@handshake.de> References: <010F3B60-6AE1-420F-94BA-63C3CC3723A8@contoso.com> <87mufn8f05.fsf@handshake.de> <87a7bmlql9.fsf@handshake.de> Message-ID: Finally I found why my setup.py dosen't work. I didn't put a `__init__.py` in my `test` folder, thus Python dosen't think it's a package. That's why it found the wrong package. Thank you. On Mon, Sep 02, 2019 at 04:28:50PM +0200, dieter wrote: > YuXuan Dong writes: > > I have uninstalled `six` using `pip uninstall six` but the problem is still there. > > Your traceback shows that `six` does not cause your problem. > It is quite obvious that a `test_winreg` will want to load the > `wingreg` module. > > > As you suggested, I have checked the traceback and found the exception is caused by `/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/test/test_winreg.py` on my machine with homebrew-installed Python3. > > > > I have realized that the problem may be caused of `test-suite=test` in my `setup.py`. `setuptools` will find the `test` package. But the package it found is not in my project. It found `/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/test`. > > Yes. Apparently, you run the Python test suite - and depending on > platform and available infrastructure some of its tests fail. > > In my package `dm.xmlsec.binding` I use > `test_suite='dm.xmlsec.binding.tests.testsuite'`. > Maybe, you can ensure to get the correct test suite in a similar > way for your package. > > -- > https://mail.python.org/mailman/listinfo/python-list From barry at barrys-emacs.org Mon Sep 2 11:25:15 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Mon, 2 Sep 2019 16:25:15 +0100 Subject: Announcing Scm Workbench 0.9.3 for Git, Mercurial and Subversion Message-ID: <33E77953-B655-4521-B558-814E1ED5B1AA@barrys-emacs.org> SCM Workbench features ? Support Subversion (svn), Mercurial (hg) and Git projects. ? Experimental support for Perforce (P4) ? Easy to learn and use ? Builtin User Guide describes the operation and features of the application. ? Add project wizard can scan for all your existing projects. ? All subversion client operations in a GUI ? Many Git client operations in a GUI ? GUI git rebase ? Some mercurial (hg) client operations in a GUI ? Enhanced operations (subversion rename of modified files etc) ? Support for Dark mode ? Support software development workflow ? Builtin GUI diff showing line and character diffs ? Ability to diff between revisions in a files history ? Runs on Windows, Mac OS X and Unix platforms Please visit http://scm-workbench.barrys-emacs.org/ for downloads, git source, user guide and further information on SCM Workbench. New in 0.9.3 ? Lots of improvement since the last release ? Update to use python3.7, PyQt5 5.12 and pysvn with svn 1.12 Barry From dfnsonfsduifb at gmx.de Tue Sep 3 02:42:54 2019 From: dfnsonfsduifb at gmx.de (Johannes Bauer) Date: Tue, 3 Sep 2019 08:42:54 +0200 Subject: GPG wrapper, ECC 25519 compatible? In-Reply-To: References: <7cf14a1add0468bcf0193fa6c6cf7f03@riseup.net> Message-ID: On 03.09.19 05:28, rmlibre at riseup.net wrote: > But I just don't understand how to get > and pass information back to the gpg command line prompts at all, not to > mention automating the process. The manpage describes how to enable the machine-parsable interface, which is exactly what you want. Then: import subprocess inputs = [ "11", "Q", "1", "0", "y", "username", "username at user.net", "none", "O", ] input_data = ("\n".join(inputs) + "\n").encode() subprocess.check_output([ "gpg2", "--expert", "--full-gen-key", "--with-colons", "--command-fd", "0", "--status-fd", "1" ], input = input_data) Cheers, Joe -- "Performance ist nicht das Problem, es l?uft ja nachher beides auf der selben Hardware." -- Hans-Peter Diettrich in d.s.e. From cs at cskk.id.au Tue Sep 3 03:27:59 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 3 Sep 2019 17:27:59 +1000 Subject: Execute complex shell commands within python and obtain the output. In-Reply-To: References: Message-ID: <20190903072759.GA26645@cskk.homeip.net> On 03Sep2019 03:59, Hongyi Zhao wrote: >On Tue, 03 Sep 2019 08:24:17 +1000, Cameron Simpson wrote: >> Finally, the .decode('utf8') assumes your locale is UTF8 based. It >> probably is, but if it isn't then you may get mojibake. > >Nowadays, most of the os use utf8 as the default locale. Am I wrong? You are not wrong. So it is pretty safe to do what you did. It was merely that it is hardwired in your code (and you need to wire in something). Just keep in mind that _if_ this script runs in a non-utf8 environment the decoding may be wrong. Unlikely to actually happen though. Cheers, Cameron Simpson From storchaka at gmail.com Tue Sep 3 03:51:44 2019 From: storchaka at gmail.com (Serhiy Storchaka) Date: Tue, 3 Sep 2019 10:51:44 +0300 Subject: a,b = 2,3 and [a,b] = [2,3] In-Reply-To: References: <782d5cad-8e74-424a-97db-1192168dd487@googlegroups.com> <864l1vpgug.fsf@richard.bawden.org> <86y2z7nlsk.fsf@richard.bawden.org> Message-ID: 02.09.19 12:24, Chris Angelico ????: > But the curious difference happens in 3.7. I don't know what changed > to cause this, but from there on, the list gets built and then > unpacked. This was a side effect of moving the optimization for `x in [a, b]` from the peepholer to the AST optimizer. From rosuav at gmail.com Tue Sep 3 04:02:34 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 3 Sep 2019 18:02:34 +1000 Subject: a,b = 2,3 and [a,b] = [2,3] In-Reply-To: References: <782d5cad-8e74-424a-97db-1192168dd487@googlegroups.com> <864l1vpgug.fsf@richard.bawden.org> <86y2z7nlsk.fsf@richard.bawden.org> Message-ID: On Tue, Sep 3, 2019 at 5:53 PM Serhiy Storchaka wrote: > > 02.09.19 12:24, Chris Angelico ????: > > But the curious difference happens in 3.7. I don't know what changed > > to cause this, but from there on, the list gets built and then > > unpacked. > > This was a side effect of moving the optimization for `x in [a, b]` from > the peepholer to the AST optimizer. > Ah ha. Thank you. Is it worth trying to reinstate that? On the one hand, there's no reason to build the list, and this technically is a (performance) regression. On the other hand, it's not code people actually write, since you can do the same thing with a tuple and it IS optimized. ChrisA From storchaka at gmail.com Tue Sep 3 04:43:25 2019 From: storchaka at gmail.com (Serhiy Storchaka) Date: Tue, 3 Sep 2019 11:43:25 +0300 Subject: a,b = 2,3 and [a,b] = [2,3] In-Reply-To: References: <782d5cad-8e74-424a-97db-1192168dd487@googlegroups.com> <864l1vpgug.fsf@richard.bawden.org> <86y2z7nlsk.fsf@richard.bawden.org> Message-ID: 03.09.19 11:02, Chris Angelico ????: > On Tue, Sep 3, 2019 at 5:53 PM Serhiy Storchaka wrote: >> >> 02.09.19 12:24, Chris Angelico ????: >>> But the curious difference happens in 3.7. I don't know what changed >>> to cause this, but from there on, the list gets built and then >>> unpacked. >> >> This was a side effect of moving the optimization for `x in [a, b]` from >> the peepholer to the AST optimizer. >> > > Ah ha. Thank you. > > Is it worth trying to reinstate that? On the one hand, there's no > reason to build the list, and this technically is a (performance) > regression. On the other hand, it's not code people actually write, > since you can do the same thing with a tuple and it IS optimized. I tried this. Adding such optimization on the AST level will add around 30 lines of code, but will allow to optimize more complex cases like `[a, b] = [c, d] = [1, 2]` or `[a, *b] = [*c, d] = [1, 2]`. Reintroducing it in the peepholer needs less changes, but will optimize only narrow set of special cases. In any case the benefit looks too small. It may even be that the original optimization was not intended, but a side effect of the optimization for `x in [a, b]`. From rhodri at kynesim.co.uk Tue Sep 3 08:46:31 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Tue, 3 Sep 2019 13:46:31 +0100 Subject: if STREAM.isatty(): In-Reply-To: References: <413b4f52-e79c-00dc-e125-bf0b4364541d@kynesim.co.uk> <2e0a8015-f053-40d0-b3a8-7d5fd393c2f5@kynesim.co.uk> <1c557fe8-dbb6-383c-285a-363e86047084@kynesim.co.uk> Message-ID: <19aafc4e-c0d7-cd47-41d5-ce401d02fa9a@kynesim.co.uk> On 30/08/2019 12:05, Hongyi Zhao wrote: > On Fri, 30 Aug 2019 18:42:51 +1000, Chris Angelico wrote: > >> There is no magic here. It is simply asking a question, and then making >> a decision based on the answer. > > What's your mean by saying this? Sorry for my poor English. "No 'magic'" (?? if Google is to be trusted :-) means here that there is nothing complicated and strange going on in the background. "STREAM.isatty()" just asks a question -- is this a tty? -- and the answer will always be the same for the same STREAM. Either it is a tty or it isn't, it will not change from one to the other. It's exactly like "STRING.isnumeric()"; for the same string it will always give the same answer. -- Rhodri James *-* Kynesim Ltd From cspealma at redhat.com Tue Sep 3 09:09:47 2019 From: cspealma at redhat.com (Calvin Spealman) Date: Tue, 3 Sep 2019 09:09:47 -0400 Subject: Help needed to run some code!!!! In-Reply-To: <4815c2dc-1800-4aca-bd0c-81fcfc3bfea1@googlegroups.com> References: <4815c2dc-1800-4aca-bd0c-81fcfc3bfea1@googlegroups.com> Message-ID: It sounds like you have module-level behavior you don't want to happen during normal import-time. If that is the case, move that behavior into a class or function you can invoke at the correct time, rather than trying to do your imports at strange times. On Mon, Sep 2, 2019 at 11:50 AM Spencer Du wrote: > Hi > > How can I execute "from devicesEmbedded import *" after this: > "print("Device added to list")" in GUI.py because currently if I have the > import added at the top of GUI.py file it always executes first before the > GUI.py file is executed. I want the devicesEmbedded.py to execute after > GUI.py has executed first. > > Thanks > Spencer > > GUI.py > > import logging > from datetime import timedelta > import time > from thespian.actors import * > from transitions import Machine > import paho.mqtt.client as mqtt > import importlib > import os > import os.path > import sys > from PyQt5.QtWidgets import * > from PyQt5.QtCore import * > from PyQt5 import QtWidgets, uic > from mqtt import * > # from devicesEmbedded import * > import json > > class MainWindow(QtWidgets.QMainWindow): > def __init__(self,parent = None): > QMainWindow.__init__(self) > super(MainWindow, self).__init__(parent) > self.mdi = QMdiArea() > self.setCentralWidget(self.mdi) > > self.setMinimumSize(QSize(800, 600)) > self.setWindowTitle("PyQt button example - > pythonprogramminglanguage.com") > > pybutton = QPushButton('Add device', self) > > pybutton.clicked.connect(self.importbutton) > > pybutton.move(100, 400) > pybutton.resize(150, 32) > > self.textbox = QLineEdit(self) > self.textbox.move(100,350) > self.textbox.resize(100, 32) > > self.fileName_UI = "" > > def importbutton(self): > self.fileName_UI = self.textbox.text() > self.loadGUI() > > def getGUIFilename(self): > return self.fileName_UI > > def loadGUI(self): > print("Searching file", self.fileName_UI) > try: > module = __import__(self.fileName_UI) > my_class = getattr(module, "SubWindow") > > sub = QMdiSubWindow() > > sub.setWidget(my_class()) > sub.setWindowTitle("New GUI: " + self.fileName_UI) > self.mdi.addSubWindow(sub) > sub.show() > > print("creating new instance " + self.fileName_UI) > client = device("Device") > client.run() > > client.loop_start() # start the loop > # device_message = self.fileName_UI > time.sleep(2) > print("Subscribing to topic", > "microscope/light_sheet_microscope/UI") > client.subscribe("microscope/light_sheet_microscope/UI") > print("Publishing message to topic", > "microscope/light_sheet_microscope/UI/list_of_devices") > > client.publish("microscope/light_sheet_microscope/UI/list_of_devices", > json.dumps({"type": "device", "payload":{"name": self.fileName_UI, "cmd": > "adding device"}}, indent=2)) > time.sleep(1) # wait > client.loop_stop() # stop the loop > print("Device added" + "\n") > > client.run() > client.loop_start() > time.sleep(2) > print("Subscribing to topic", > "microscope/light_sheet_microscope/UI/list_of_devices") > > client.subscribe("microscope/light_sheet_microscope/UI/list_of_devices") > print("Publishing message to topic", > "microscope/light_sheet_microscope/UI/list_of_devices") > > client.publish("microscope/light_sheet_microscope/UI/list_of_devices", > self.fileName_UI + " added to device list") > time.sleep(1) > client.loop_stop() > > listofdevices = [] > listofdevices.append(self.fileName_UI) > with open("list_of_devices.txt", "a+") as myfile: > for item in listofdevices: > myfile.write(item + ",") > > > print(item) > print(listofdevices) > print("Device added to list") > except: > print("creating new instance " + self.fileName_UI) > client = device("Device") > client.run() > > client.loop_start() # start the loop > device_message = self.fileName_UI > time.sleep(2) > print("Subscribing to topic", > "microscope/light_sheet_microscope/UI") > client.subscribe("microscope/light_sheet_microscope/UI") > print("Publishing message to topic", > "microscope/light_sheet_microscope/UI") > client.publish("microscope/light_sheet_microscope/UI", > json.dumps({"type": "device", "payload":{"name": self.fileName_UI}}, > indent=2)) > time.sleep(2) # wait > client.loop_stop() # stop the loop > print(device_message + ".py " + "file doesn't exist") > print("Device not added") > if __name__ == "__main__": > app = QApplication(sys.argv) > mainWin = MainWindow() > a = mainWin.show() > try: > mainWin.show() > os.remove("list_of_devices.txt") > print("Awaiting devices to be launched") > except: > print("Awaiting devices to be launched") > publishedMessage = mainWin.getGUIFilename() > sys.exit(app.exec_()) > > devicesEmbedded.py: > > import random > import asyncio > from actorio import Actor, Message, DataMessage, ask, EndMainLoop, > Reference > from mqtt import * > # from GUI import * > > client = device("Device") > client.run() > client.loop_start() > time.sleep(1) > > print("Subscribing to topic", > "microscope/light_sheet_microscope/UI/list_of_devices") > client.subscribe("microscope/light_sheet_microscope/UI/list_of_devices") > client.loop_stop() # stop the loop > > def readFile(fname): > try: > with open(fname, "r") as f: > for item in f: > print(item) > except: > print("No devices added yet") > readFile("list_of_devices.txt") > > mqtt.py > > import logging > from datetime import timedelta > import time > from thespian.actors import * > from transitions import Machine > import paho.mqtt.client as mqtt > import importlib > import os.path > import sys > from PyQt5.QtWidgets import * > from PyQt5.QtCore import * > from PyQt5 import QtWidgets, uic > > class device(mqtt.Client): > def on_connect(self, mqttc, obj, flags, rc): > if rc == 0: > print("Connected to broker") > else: > print("Connection failed") > > # mqttc.subscribe("microscope/light_sheet_microscope/UI") > > def on_message(self, mqttc, userdata, message): > msg = str(message.payload.decode("utf-8")) > print("message recieved= " + msg) > # print("File which you want to import(with .py extension)") > print("message topic=", message.topic) > print("message qos=", message.qos) > print("message retain flag=", message.retain) > > def run(self): > self.connect("broker.hivemq.com", 1883, 60) > > Thanks > -- > https://mail.python.org/mailman/listinfo/python-list > -- CALVIN SPEALMAN SENIOR QUALITY ENGINEER cspealma at redhat.com M: +1.336.210.5107 [image: https://red.ht/sig] TRIED. TESTED. TRUSTED. From cspealma at redhat.com Tue Sep 3 09:11:35 2019 From: cspealma at redhat.com (Calvin Spealman) Date: Tue, 3 Sep 2019 09:11:35 -0400 Subject: problem occurring in operating python , after typing only 5-6 programs only is causing problem it has stoped working In-Reply-To: <5d6a7825.1c69fb81.d1719.cdbf@mx.google.com> References: <5d6a7825.1c69fb81.d1719.cdbf@mx.google.com> Message-ID: You will need to provide more information: What version of Python are running? What sort of error message you receiving (paste the entire error)? What code are you attempting to run which causes this problem (paste the failing code)? On Sun, Sep 1, 2019 at 3:01 PM best web site wrote: > > > Sent from Mail for Windows 10 > > -- > https://mail.python.org/mailman/listinfo/python-list > -- CALVIN SPEALMAN SENIOR QUALITY ENGINEER cspealma at redhat.com M: +1.336.210.5107 [image: https://red.ht/sig] TRIED. TESTED. TRUSTED. From hongyi.zhao at gmail.com Tue Sep 3 09:33:27 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Tue, 3 Sep 2019 13:33:27 +0000 (UTC) Subject: Execute complex shell commands within python and obtain the output. References: <20190903072759.GA26645@cskk.homeip.net> Message-ID: On Tue, 03 Sep 2019 17:27:59 +1000, Cameron Simpson wrote: > It was merely that it is hardwired in your code (and you need to wire in > something). Just keep in mind that _if_ this script runs in a non-utf8 > environment the decoding may be wrong. Unlikely to actually happen > though. Thanks, fixed with the following: import subprocess,locale a=locale.getdefaultlocale() cmd=... ps=subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE) output =ps.communicate()[0].decode(a[1]) Regards From voodoo.bender at gmail.com Tue Sep 3 12:02:09 2019 From: voodoo.bender at gmail.com (alberto) Date: Tue, 3 Sep 2019 09:02:09 -0700 (PDT) Subject: TypeError: loadtxt() got an unexpected keyword argument 'max_rows' Message-ID: Hi, I produce a script to elaborate data but command line $ python3.4 PlotnhvsvdBTP1.py I have this error Traceback (most recent call last): File "PlotnhvsvdBTP1.py", line 31, in UCvol = np.loadtxt(outputtransfile,skiprows=26,max_rows=1,usecols=[1]) TypeError: loadtxt() got an unexpected keyword argument 'max_rows' How could fix it? I attacched my files https://drive.google.com/file/d/1PgOcuEMFsaAuKTsbU0i0gwg04mDCJJoK/view?usp=sharing https://drive.google.com/file/d/13E7vcGQtrOS1lw9RupGThGQ2vSGRfTFG/view?usp=sharing https://drive.google.com/file/d/1Z6GKYtHthAyPO3wFHUFK10QweRpclu29/view?usp=sharing regard Alberto From joel.goldstick at gmail.com Tue Sep 3 12:24:51 2019 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 3 Sep 2019 12:24:51 -0400 Subject: TypeError: loadtxt() got an unexpected keyword argument 'max_rows' In-Reply-To: References: Message-ID: On Tue, Sep 3, 2019 at 12:06 PM alberto wrote: > > Hi, > I produce a script to elaborate data > > but command line $ python3.4 PlotnhvsvdBTP1.py > > I have this error > > Traceback (most recent call last): > File "PlotnhvsvdBTP1.py", line 31, in > UCvol = np.loadtxt(outputtransfile,skiprows=26,max_rows=1,usecols=[1]) > TypeError: loadtxt() got an unexpected keyword argument 'max_rows' > > How could fix it? > > I attacched my files > > https://drive.google.com/file/d/1PgOcuEMFsaAuKTsbU0i0gwg04mDCJJoK/view?usp=sharing > https://drive.google.com/file/d/13E7vcGQtrOS1lw9RupGThGQ2vSGRfTFG/view?usp=sharing > https://drive.google.com/file/d/1Z6GKYtHthAyPO3wFHUFK10QweRpclu29/view?usp=sharing > > regard > > Alberto Please don't attach files. Cut and paste code as text into email body along with copy and pasted traceback. That said I found this: .. versionadded:: 1.14.0 max_rows : int, optional Check your version of loadtxt()? > -- > https://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From rhodri at kynesim.co.uk Tue Sep 3 12:26:21 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Tue, 3 Sep 2019 17:26:21 +0100 Subject: TypeError: loadtxt() got an unexpected keyword argument 'max_rows' In-Reply-To: References: Message-ID: <221e9fc0-198b-e421-08bb-7eaf3372d51a@kynesim.co.uk> On 03/09/2019 17:02, alberto wrote: > I have this error > > Traceback (most recent call last): > File "PlotnhvsvdBTP1.py", line 31, in > UCvol = np.loadtxt(outputtransfile,skiprows=26,max_rows=1,usecols=[1]) > TypeError: loadtxt() got an unexpected keyword argument 'max_rows' > > How could fix it? A quick google search suggests that you aren't using a recent enough version of numpy. The "max_rows" keyword argument was added to loadtxt() in version 1.16.0; you must be using an older version than that. Your options are to upgrade the version of numpy you have (don't ask me how, I've never used it) or delete the "max_rows=1" and accept the inefficiency. -- Rhodri James *-* Kynesim Ltd From tjreedy at udel.edu Tue Sep 3 12:29:31 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 3 Sep 2019 12:29:31 -0400 Subject: TypeError: loadtxt() got an unexpected keyword argument 'max_rows' In-Reply-To: References: Message-ID: On 9/3/2019 12:02 PM, alberto wrote: > Hi, > I produce a script to elaborate data > > but command line $ python3.4 PlotnhvsvdBTP1.py > > I have this error > > Traceback (most recent call last): > File "PlotnhvsvdBTP1.py", line 31, in > UCvol = np.loadtxt(outputtransfile,skiprows=26,max_rows=1,usecols=[1]) > TypeError: loadtxt() got an unexpected keyword argument 'max_rows' > > How could fix it? Don't do what the message says is the wrong thing to do. Really. In particular, don't pass 'max_rows=1'. To find out what *to* do, read the doc for the loadtxt function. From 'skiprows' and 'usecols', with no underscore, I suspect you should pass 'maxrows=1'. -- Terry Jan Reedy From python at mrabarnett.plus.com Tue Sep 3 12:37:57 2019 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 3 Sep 2019 17:37:57 +0100 Subject: TypeError: loadtxt() got an unexpected keyword argument 'max_rows' In-Reply-To: References: Message-ID: <447bcbaa-a98b-ae9d-e261-c27f9334eab9@mrabarnett.plus.com> On 2019-09-03 17:02, alberto wrote: > Hi, > I produce a script to elaborate data > > but command line $ python3.4 PlotnhvsvdBTP1.py > > I have this error > > Traceback (most recent call last): > File "PlotnhvsvdBTP1.py", line 31, in > UCvol = np.loadtxt(outputtransfile,skiprows=26,max_rows=1,usecols=[1]) > TypeError: loadtxt() got an unexpected keyword argument 'max_rows' > > How could fix it? > > I attacched my files > > https://drive.google.com/file/d/1PgOcuEMFsaAuKTsbU0i0gwg04mDCJJoK/view?usp=sharing > https://drive.google.com/file/d/13E7vcGQtrOS1lw9RupGThGQ2vSGRfTFG/view?usp=sharing > https://drive.google.com/file/d/1Z6GKYtHthAyPO3wFHUFK10QweRpclu29/view?usp=sharing > The "max_rows" keyword argument is new in numpy version 1.16.0. Which version do you have? Try: import numpy print(numpy.__version__) to find out. From python at mrabarnett.plus.com Tue Sep 3 13:11:54 2019 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 3 Sep 2019 18:11:54 +0100 Subject: TypeError: loadtxt() got an unexpected keyword argument 'max_rows' In-Reply-To: References: Message-ID: On 2019-09-03 17:29, Terry Reedy wrote: > On 9/3/2019 12:02 PM, alberto wrote: >> Hi, >> I produce a script to elaborate data >> >> but command line $ python3.4 PlotnhvsvdBTP1.py >> >> I have this error >> >> Traceback (most recent call last): >> File "PlotnhvsvdBTP1.py", line 31, in >> UCvol = np.loadtxt(outputtransfile,skiprows=26,max_rows=1,usecols=[1]) >> TypeError: loadtxt() got an unexpected keyword argument 'max_rows' >> >> How could fix it? > > Don't do what the message says is the wrong thing to do. Really. > > In particular, don't pass 'max_rows=1'. To find out what *to* do, read > the doc for the loadtxt function. From 'skiprows' and 'usecols', with > no underscore, I suspect you should pass 'maxrows=1'. > No, the documentation says "max_rows". Apparently, consistency, like the argument, is optional. :-) From ijbrewster at alaska.edu Tue Sep 3 13:17:36 2019 From: ijbrewster at alaska.edu (Israel Brewster) Date: Tue, 3 Sep 2019 09:17:36 -0800 Subject: Proper way to pass Queue to process when using multiprocessing.imap()? Message-ID: When using pool.imap to apply a function over a list of values, what is the proper way to pass additional arguments to the function, specifically in my case a Queue that the process can use to communicate back to the main thread (for the purpose of reporting progress)? I have seen suggestions of using starmap, but this doesn?t appear to have a ?lazy? variant, which I have found to be very beneficial in my use case. The Queue is the same one for all processes, if that makes a difference. I could just make the Queue global, but I have always been told not too. Perhaps this is an exception? --- 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 rgaddi at highlandtechnology.invalid Tue Sep 3 13:27:08 2019 From: rgaddi at highlandtechnology.invalid (Rob Gaddi) Date: Tue, 3 Sep 2019 10:27:08 -0700 Subject: Proper way to pass Queue to process when using multiprocessing.imap()? In-Reply-To: References: Message-ID: On 9/3/19 10:17 AM, Israel Brewster wrote: > When using pool.imap to apply a function over a list of values, what is the proper way to pass additional arguments to the function, specifically in my case a Queue that the process can use to communicate back to the main thread (for the purpose of reporting progress)? I have seen suggestions of using starmap, but this doesn?t appear to have a ?lazy? variant, which I have found to be very beneficial in my use case. The Queue is the same one for all processes, if that makes a difference. > > I could just make the Queue global, but I have always been told not too. Perhaps this is an exception? > > --- > 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 > The first rule is to never use global variables. The second is to never put too much stock in sweeping generalizations. So long as you can keep that Queue's usage pattern fairly well constrained, go ahead and make it global. One thing to think about that might make this all easier though; have you looked at the concurrent.futures module? I find it does a fantastic job of handling this sort of parallelization in a straightforward way. -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix. From joel.goldstick at gmail.com Tue Sep 3 14:03:15 2019 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 3 Sep 2019 14:03:15 -0400 Subject: Help needed urgently for running some code!!!! In-Reply-To: References: <50e14ca8-bf6a-4443-8c7d-74b4d1069247@googlegroups.com> Message-ID: On Tue, Sep 3, 2019 at 1:38 PM Dennis Lee Bieber wrote: > > On Mon, 2 Sep 2019 08:05:14 -0700 (PDT), Spencer Du > declaimed the following: > > >Hi > > > >I want to execute > > > >"from devicesEmbedded import *": in GUI.py after all code in GUI.py is run. Also how do I make the devicesEmbedded.py reload and run when a txt file is created in the name of "list_of_devices.txt" in the GUI.py python program. > > > Reload is one of the least effective features. Especially when you have > bound names locally within modules. "... import *" binds names locally. > Reloading a changed module creates a new module entry in Python's run-time, > but any local names still refer to the old module contents. > > Please consider reading the documentation: > https://docs.python.org/3/library/importlib.html > > """ > importlib.reload(module) > > Reload a previously imported module. The argument must be a module > object, so it must have been successfully imported before. This is useful > if you have edited the module source file using an external editor and want > to try out the new version without leaving the Python interpreter. The > return value is the module object (which can be different if re-importing > causes a different object to be placed in sys.modules). > > When reload() is executed: > > Python module?s code is recompiled and the module-level code > re-executed, defining a new set of objects which are bound to names in the > module?s dictionary by reusing the loader which originally loaded the > module. The init function of extension modules is not called a second time. > > As with all other objects in Python the old objects are only > reclaimed after their reference counts drop to zero. > > The names in the module namespace are updated to point to any new > or changed objects. > > Other references to the old objects (such as names external to the > module) are not rebound to refer to the new objects and must be updated in > each namespace where they occur if that is desired. > """ > > NOTE the last paragraph. This means you should NOT be using "... import *" > syntax for anything you might want to have reloaded. Instead use "import > modulename" and everywhere you access something in that module use the long > form "modulename.something". This way, when it is reloaded, "modulename" > will reference the new contents. > > > As for detecting file changes... You will have to periodically check > the time-stamp (modification time) of the file, and if it changes, reread > the file. > > > > > > -- > Wulfraed Dennis Lee Bieber AF6VN > wlfraed at ix.netcom.com http://wlfraed.microdiversity.freeddns.org/ > > -- > https://mail.python.org/mailman/listinfo/python-list Spencer, for the past month (almost) you have been sending "urgent" requests for people here to write code for you. You've already decided what needs to be done, but you haven't really explained what you want to do. You don't respond to other's responses for further clarification, or with your progress. You basically repeat the same urgent request. Please don't do this. There are many python experts here who will help you, but getting someone to write your code for you when you can't really explain what your goal is will get you nowhere, especially since you claim this is for your job. To me, its an odd thing to want to do imports depending on a file's contents. But maybe its a good idea. However, you can just import all of the modules you might expect to need, and ignore if they aren't present with a suitable try/except block. Try explaining the bigger picture of what you are trying to do, and maybe you can gain some incite into a better way of proceeding. I'm afraid it looks like you have been cutting and pasting without understanding what your code's design is all about. I could be totally wrong. good luck -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From __peter__ at web.de Tue Sep 3 14:49:20 2019 From: __peter__ at web.de (Peter Otten) Date: Tue, 03 Sep 2019 20:49:20 +0200 Subject: Proper way to pass Queue to process when using multiprocessing.imap()? References: Message-ID: Israel Brewster wrote: > When using pool.imap to apply a function over a list of values, what is > the proper way to pass additional arguments to the function, specifically > in my case a Queue that the process can use to communicate back to the > main thread (for the purpose of reporting progress)? I have seen > suggestions of using starmap, but this doesn?t appear to have a ?lazy? > variant, which I have found to be very beneficial in my use case. The > Queue is the same one for all processes, if that makes a difference. > > I could just make the Queue global, but I have always been told not too. > Perhaps this is an exception? How about wrapping the function into another function that takes only one argument? A concise way is to do that with functools.partial(): def f(value, queue): ... pool.imap(partial(f, queue=...), values) > > --- > 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 ijbrewster at alaska.edu Tue Sep 3 15:09:25 2019 From: ijbrewster at alaska.edu (Israel Brewster) Date: Tue, 3 Sep 2019 11:09:25 -0800 Subject: Proper way to pass Queue to process when using multiprocessing.imap()? In-Reply-To: References: Message-ID: > > On Sep 3, 2019, at 10:49 AM, Peter Otten <__peter__ at web.de> wrote: > > Israel Brewster wrote: > >> When using pool.imap to apply a function over a list of values, what is >> the proper way to pass additional arguments to the function, specifically >> in my case a Queue that the process can use to communicate back to the >> main thread (for the purpose of reporting progress)? I have seen >> suggestions of using starmap, but this doesn?t appear to have a ?lazy? >> variant, which I have found to be very beneficial in my use case. The >> Queue is the same one for all processes, if that makes a difference. >> >> I could just make the Queue global, but I have always been told not too. >> Perhaps this is an exception? > > How about wrapping the function into another function that takes only one > argument? A concise way is to do that with functools.partial(): > > def f(value, queue): ... > > pool.imap(partial(f, queue=...), values) That looks like exactly what I was looking for. I?ll give it a shot. 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 > > > >> >> --- >> 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 >> > > > -- > https://mail.python.org/mailman/listinfo/python-list From ijbrewster at alaska.edu Tue Sep 3 15:10:33 2019 From: ijbrewster at alaska.edu (Israel Brewster) Date: Tue, 3 Sep 2019 11:10:33 -0800 Subject: Proper way to pass Queue to process when using multiprocessing.imap()? In-Reply-To: References: Message-ID: <318D9742-4BEA-4FD7-BADB-87BDBED1C25D@alaska.edu> > > On Sep 3, 2019, at 9:27 AM, Rob Gaddi wrote: > > On 9/3/19 10:17 AM, Israel Brewster wrote: >> When using pool.imap to apply a function over a list of values, what is the proper way to pass additional arguments to the function, specifically in my case a Queue that the process can use to communicate back to the main thread (for the purpose of reporting progress)? I have seen suggestions of using starmap, but this doesn?t appear to have a ?lazy? variant, which I have found to be very beneficial in my use case. The Queue is the same one for all processes, if that makes a difference. >> I could just make the Queue global, but I have always been told not too. Perhaps this is an exception? >> --- >> 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 > > The first rule is to never use global variables. The second is to never put too much stock in sweeping generalizations. So long as you can keep that Queue's usage pattern fairly well constrained, go ahead and make it global. > > One thing to think about that might make this all easier though; have you looked at the concurrent.futures module? I find it does a fantastic job of handling this sort of parallelization in a straightforward way. I?ve only briefly looked at it in other situations. I?ll go ahead and take another look for this one. Thanks for the suggestion! --- 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 > > -- > Rob Gaddi, Highland Technology -- www.highlandtechnology.com > Email address domain is currently out of order. See above to fix. > -- > https://mail.python.org/mailman/listinfo/python-list From hongyi.zhao at gmail.com Tue Sep 3 18:20:32 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Tue, 3 Sep 2019 22:20:32 +0000 (UTC) Subject: Append some stuff into a file with only the last appended line reserved. References: <20190902222931.GA80784@cskk.homeip.net> Message-ID: On Tue, 03 Sep 2019 08:29:31 +1000, Cameron Simpson wrote: > Please describe this in more detail. Present a little pycurl output and > then explain what portion of it should land in the log file. See the following code from here: http://pycurl.io/docs/latest/callbacks.html#xferinfofunction ---------------- ## Callback function invoked when download/upload has progress def progress(download_t, download_d, upload_t, upload_d): print "Total to download", download_t print "Total downloaded", download_d print "Total to upload", upload_t print "Total uploaded", upload_d c = pycurl.Curl() c.setopt(c.URL, "http://slashdot.org/") c.setopt(c.NOPROGRESS, False) c.setopt(c.XFERINFOFUNCTION, progress) c.perform() -------------------- If I want to print these info ( or write them into log ) only when some specific events occur, say: pycurl.ABORTED_BY_CALLBACK pycurl.OPERTATION_TIMEOUT How to do it? From ekopalypse at gmail.com Tue Sep 3 08:57:20 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Tue, 3 Sep 2019 05:57:20 -0700 (PDT) Subject: How to annotate an IntEnum value Message-ID: <6ecbc0a8-b223-4e49-9721-711b439b602a@googlegroups.com> Suppose the following from enum import IntEnum class ENDOFLINE(IntEnum): CRLF = 0 CR = 1 LF = 2 def get_eol() -> ??: return ENDOFLINE.CRLF def set_eol(eol_value) -> None: pass How should the return value from get_eol be annotated? As ENDOFLINE? The same question for set_eol function, is it assumed to see the following declaration? def set_eol(value: ENDOFLINE) -> None: pass I've tried to see what pep484 suggests but either I haven't understood it or it doesn't go into detail in such a case. From simbarashem at oldmutual.co.zw Wed Sep 4 02:37:20 2019 From: simbarashem at oldmutual.co.zw (Mangwendeza Simbarashe) Date: Wed, 4 Sep 2019 06:37:20 +0000 Subject: failed to install anaconda packages Message-ID: <8CCF8584100DD045A95D25788EBB49130409A91D8E@omsvr-mb1.oldmutual.local> From antoon.pardon at vub.be Wed Sep 4 10:21:00 2019 From: antoon.pardon at vub.be (Antoon Pardon) Date: Wed, 4 Sep 2019 16:21:00 +0200 Subject: How do I give a decorator acces to the class of a decorated function Message-ID: What I am trying to do is the following. class MyClass (...) : @register def MyFunction(...) ... What I would want is for the register decorator to somehow create/mutate class variable(s) of MyClass. Is that possible or do I have to rethink my approach? -- Antoon Pardon. From rhodri at kynesim.co.uk Wed Sep 4 10:58:00 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Wed, 4 Sep 2019 15:58:00 +0100 Subject: How do I give a decorator acces to the class of a decorated function In-Reply-To: References: Message-ID: On 04/09/2019 15:21, Antoon Pardon wrote: > What I am trying to do is the following. > > class MyClass (...) : > @register > def MyFunction(...) > ... > > What I would want is for the register decorator to somehow create/mutate > class variable(s) of MyClass. > > Is that possible or do I have to rethink my approach? I can't see a way of doing that directly, but you could cheat by putting the "class variables" in a global dictionary? And perhaps referencing that global from a class variable? Something like: my_class_registry = {} class MyClass(...): MyClassRegistry = my_class_registry @register(my_class_registry) def MyFunction(...): ... Or you may be able to achieve what you want with dir() and some careful naming? -- Rhodri James *-* Kynesim Ltd From rosuav at gmail.com Wed Sep 4 11:03:35 2019 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 5 Sep 2019 01:03:35 +1000 Subject: How do I give a decorator acces to the class of a decorated function In-Reply-To: References: Message-ID: On Thu, Sep 5, 2019 at 12:23 AM Antoon Pardon wrote: > > What I am trying to do is the following. > > class MyClass (...) : > @register > def MyFunction(...) > ... > > What I would want is for the register decorator to somehow create/mutate > class variable(s) of MyClass. > > Is that possible or do I have to rethink my approach? > At the time when the decorator runs, the class doesn't actually exist. But if you're wrapping MyFunction (which appears to be a method), then your wrapper function will receive 'self' as its first parameter, and can access the class from that. Doesn't help at decoration time, though. ChrisA From spencerdu at hotmail.co.uk Wed Sep 4 11:08:06 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Wed, 4 Sep 2019 08:08:06 -0700 (PDT) Subject: How to remove a string from a txt file? Message-ID: <03e69f5d-328f-4732-9c8c-72e828c96d94@googlegroups.com> Hi I want to remove a string from a txt file and then print out what I have removed. How do I do this. The txt file is in this format and should be kept in this format. txt.txt: laser,cameras, Thanks From aishan0403 at gmail.com Wed Sep 4 11:16:31 2019 From: aishan0403 at gmail.com (A S) Date: Wed, 4 Sep 2019 08:16:31 -0700 (PDT) Subject: How to only read words within brackets/ parentheses (in .txt file) using Python Message-ID: I understand that reading lines in .txt files would look something like this in Python: with open('filename','r') as fd: lines = fd.readlines() However, how do I run my code to only read the words in my .txt files that are within each balanced parenthesis? I am not sure how to go about it, let's say my .txt file contents lines like this: kkkkk; select xx("xE'", PUT(xx.xxxx.),"'") jdfjhf:jhfjj from xxxx_x_xx_L ; quit; /* 1.xxxxx FROM xxxx_x_Ex_x */ proc sql; "TRUuuuth"); hhhjhfjs as fdsjfsj: select * from djfkjd to jfkjs (SELECT abc AS abc1, abc_2_ AS efg, abc_fg, fkdkfj_vv, jjsflkl_ff, fjkdsf_jfkj FROM &xxx..xxx_xxx_xxE where (xxx(xx_ix as format 'xxxx-xx') gff &jfjfsj_jfjfj.) and (xxx(xx_ix as format 'xxxx-xx') lec &jgjsd_vnv.) ); The main idea is to read only these portions of the .txt file (i.e. Those within parentheses): ("xE'", PUT(xx.xxxx.),"'") jdfjhf:jhfjj from xxxx_x_xx_L ; quit; /* 1.xxxxx FROM xxxx_x_Ex_x */ proc sql; "TRUuuuth") (SELECT abc AS abc1, abc_2_ AS efg, abc_fg, fkdkfj_vv, jjsflkl_ff, fjkdsf_jfkj FROM &xxx..xxx_xxx_xxE where (xxx(xx_ix as format 'xxxx-xx') gff &jfjfsj_jfjfj.) and (xxx(xx_ix as format 'xxxx-xx') lec &jgjsd_vnv.) ) Any help will be truly appreciated From __peter__ at web.de Wed Sep 4 11:46:50 2019 From: __peter__ at web.de (Peter Otten) Date: Wed, 04 Sep 2019 17:46:50 +0200 Subject: How do I give a decorator acces to the class of a decorated function References: Message-ID: Antoon Pardon wrote: > What I am trying to do is the following. > > class MyClass (...) : > @register > def MyFunction(...) > ... > > What I would want is for the register decorator to somehow create/mutate > class variable(s) of MyClass. > > Is that possible or do I have to rethink my approach? If you are willing to delegate the actual work to the metaclass call: def register(f): f.registered = True return f def registered(name, bases, namespace): namespace["my_cool_functions"] = [ n for n, v in namespace.items() if getattr(v, "registered", False) ] return type(name, bases, namespace) class MyClass(metaclass=registered) : @register def foo(self): pass @register def bar(self): pass def other(self): pass print(MyClass.my_cool_functions) From __peter__ at web.de Wed Sep 4 11:59:20 2019 From: __peter__ at web.de (Peter Otten) Date: Wed, 04 Sep 2019 17:59:20 +0200 Subject: How to only read words within brackets/ parentheses (in .txt file) using Python References: Message-ID: A S wrote: > I understand that reading lines in .txt files would look something like > this in Python: > > > with open('filename','r') as fd: > lines = fd.readlines() > > > However, how do I run my code to only read the words in my .txt files that > are within each balanced parenthesis? > > I am not sure how to go about it, let's say my .txt file contents lines > like this: > > kkkkk; > > select xx("xE'", PUT(xx.xxxx.),"'") jdfjhf:jhfjj from xxxx_x_xx_L ; > quit; > The main idea is to read only these portions of the .txt file (i.e. Those > within parentheses): > > ("xE'", PUT(xx.xxxx.),"'") jdfjhf:jhfjj from xxxx_x_xx_L ; > quit; But jdfjh... is not within parens... and what about quoted parens "("? Do they count? You probably need a tokenizer for the SQL dialect used in your "text" file. But first: can you give a non-technical description of what problem you are trying to solve instead of how you want to solve it? Perhaps someone here knows of a better approach than counting parens. From ijbrewster at alaska.edu Wed Sep 4 12:00:05 2019 From: ijbrewster at alaska.edu (Israel Brewster) Date: Wed, 4 Sep 2019 08:00:05 -0800 Subject: Proper way to pass Queue to process when using multiprocessing.imap()? In-Reply-To: References: Message-ID: <81C55440-3586-4356-9E66-85097A5FF1CC@alaska.edu> > > On Sep 3, 2019, at 11:09 AM, Israel Brewster wrote: > >> >> On Sep 3, 2019, at 10:49 AM, Peter Otten <__peter__ at web.de> wrote: >> >> Israel Brewster wrote: >> >>> When using pool.imap to apply a function over a list of values, what is >>> the proper way to pass additional arguments to the function, specifically >>> in my case a Queue that the process can use to communicate back to the >>> main thread (for the purpose of reporting progress)? I have seen >>> suggestions of using starmap, but this doesn?t appear to have a ?lazy? >>> variant, which I have found to be very beneficial in my use case. The >>> Queue is the same one for all processes, if that makes a difference. >>> >>> I could just make the Queue global, but I have always been told not too. >>> Perhaps this is an exception? >> >> How about wrapping the function into another function that takes only one >> argument? A concise way is to do that with functools.partial(): >> >> def f(value, queue): ... >> >> pool.imap(partial(f, queue=...), values) > > That looks like exactly what I was looking for. I?ll give it a shot. Thanks! So as it turns out, this doesn?t work after all. I get an error stating that ?Queue objects should only be shared between processes through inheritance?. Still a good technique to know though! --- 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 > > --- > 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 > >> >> >> >>> >>> --- >>> 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 >>> >> >> >> -- >> https://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Wed Sep 4 12:09:51 2019 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 5 Sep 2019 02:09:51 +1000 Subject: Proper way to pass Queue to process when using multiprocessing.imap()? In-Reply-To: <81C55440-3586-4356-9E66-85097A5FF1CC@alaska.edu> References: <81C55440-3586-4356-9E66-85097A5FF1CC@alaska.edu> Message-ID: On Thu, Sep 5, 2019 at 2:07 AM Israel Brewster wrote: > > > > > On Sep 3, 2019, at 11:09 AM, Israel Brewster wrote: > > > >> > >> On Sep 3, 2019, at 10:49 AM, Peter Otten <__peter__ at web.de> wrote: > >> > >> Israel Brewster wrote: > >> > >>> When using pool.imap to apply a function over a list of values, what is > >>> the proper way to pass additional arguments to the function, specifically > >>> in my case a Queue that the process can use to communicate back to the > >>> main thread (for the purpose of reporting progress)? I have seen > >>> suggestions of using starmap, but this doesn?t appear to have a ?lazy? > >>> variant, which I have found to be very beneficial in my use case. The > >>> Queue is the same one for all processes, if that makes a difference. > >>> > >>> I could just make the Queue global, but I have always been told not too. > >>> Perhaps this is an exception? > >> > >> How about wrapping the function into another function that takes only one > >> argument? A concise way is to do that with functools.partial(): > >> > >> def f(value, queue): ... > >> > >> pool.imap(partial(f, queue=...), values) > > > > That looks like exactly what I was looking for. I?ll give it a shot. Thanks! > > So as it turns out, this doesn?t work after all. I get an error stating that ?Queue objects should only be shared between processes through inheritance?. Still a good technique to know though! > Globals aren't as bad as some people think. In this case, a module-level variable seems like the correct way to do things. ChrisA From dboland9 at protonmail.com Wed Sep 4 13:12:06 2019 From: dboland9 at protonmail.com (Dave) Date: Wed, 4 Sep 2019 13:12:06 -0400 Subject: Formatting floating point Message-ID: All, I have been going in circles trying to format a floating point number so there is only 1 decimal place. In reading all of the gobble-gook that passes for Python advice, it looked like I should have done this: numStr = '3.14159' num = float(numstr) # OK so far numFmt = format(num, '{0:.1f}') # Errors on format string # --- Alternative that also does not work numFmt = float("{0:.1f}".format(numStr)) # --- numFmt = format(num, '0.1f') # Does work My question is why, and where do I find a reliable source of information on formatting numbers? Not interested in replacement values like '{} {}'.format(1, 2). Thanks, Dave From rosuav at gmail.com Wed Sep 4 13:25:55 2019 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 5 Sep 2019 03:25:55 +1000 Subject: Formatting floating point In-Reply-To: References: Message-ID: On Thu, Sep 5, 2019 at 3:16 AM Dave via Python-list wrote: > > All, > > I have been going in circles trying to format a floating point number so > there is only 1 decimal place. In reading all of the gobble-gook that > passes for Python advice, it looked like I should have done this: > > numStr = '3.14159' > num = float(numstr) # OK so far > numFmt = format(num, '{0:.1f}') # Errors on format string > # --- Alternative that also does not work > numFmt = float("{0:.1f}".format(numStr)) > # --- > numFmt = format(num, '0.1f') # Does work > Let's start by eliminating a few possibilities here. Don't try formatting the original string, and don't call float() on the result afterwards; just start with a floating-point value, and then create a formatted string. If you think about starting with a value (which in this case is a number) and the need to make a displayable version (which implies that it's a string), your options basically look like this: num = 3.14159 num_fmt = format(num, ".1f") num_fmt = "{0:.1f}".format(num) num_fmt = f"{num:.1f}" All of these will give you back the string "3.1". All of them involve a format string of ".1f", and they differ only in how they're choosing which value to put there. Hopefully that will clear things up a bit. ChrisA From rhodri at kynesim.co.uk Wed Sep 4 13:38:36 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Wed, 4 Sep 2019 18:38:36 +0100 Subject: Formatting floating point In-Reply-To: References: Message-ID: On 04/09/2019 18:12, Dave via Python-list wrote: > My question is why, and where do I find a reliable source of information > on formatting numbers?? Not interested in replacement values like '{} > {}'.format(1, 2). Here: https://docs.python.org/3/library/string.html#format-specification-mini-language I suspect the thing you were overlooking was that format() expects to be given a format specification, and you gave it a format string. Instead of format(num, "{0:.1f}"), you wanted format(num, ".1f"). If you had tried "{0:.1f}".format(num) instead, you would have found that worked too. -- Rhodri James *-* Kynesim Ltd From dboland9 at protonmail.com Wed Sep 4 13:52:24 2019 From: dboland9 at protonmail.com (Dave) Date: Wed, 4 Sep 2019 13:52:24 -0400 Subject: Formatting floating point References: Message-ID: On 9/4/19 1:25 PM, Chris Angelico wrote: > On Thu, Sep 5, 2019 at 3:16 AM Dave via Python-list > wrote: >> >> All, >> >> I have been going in circles trying to format a floating point number so >> there is only 1 decimal place. In reading all of the gobble-gook that >> passes for Python advice, it looked like I should have done this: >> >> numStr = '3.14159' >> num = float(numstr) # OK so far >> numFmt = format(num, '{0:.1f}') # Errors on format string >> # --- Alternative that also does not work >> numFmt = float("{0:.1f}".format(numStr)) >> # --- >> numFmt = format(num, '0.1f') # Does work >> > > Let's start by eliminating a few possibilities here. Don't try > formatting the original string, and don't call float() on the result > afterwards; just start with a floating-point value, and then create a > formatted string. If you think about starting with a value (which in > this case is a number) and the need to make a displayable version > (which implies that it's a string), your options basically look like > this: > > num = 3.14159 > num_fmt = format(num, ".1f") > num_fmt = "{0:.1f}".format(num) > num_fmt = f"{num:.1f}" > > All of these will give you back the string "3.1". All of them involve > a format string of ".1f", and they differ only in how they're choosing > which value to put there. > > Hopefully that will clear things up a bit. > > ChrisA > That helps! Thanks Chris. Dave, From dboland9 at protonmail.com Wed Sep 4 13:53:26 2019 From: dboland9 at protonmail.com (Dave) Date: Wed, 4 Sep 2019 13:53:26 -0400 Subject: Formatting floating point References: Message-ID: On 9/4/19 1:38 PM, Rhodri James wrote: > On 04/09/2019 18:12, Dave via Python-list wrote: >> My question is why, and where do I find a reliable source of >> information on formatting numbers?? Not interested in replacement >> values like '{} {}'.format(1, 2). > > Here: > https://docs.python.org/3/library/string.html#format-specification-mini-language > > > I suspect the thing you were overlooking was that format() expects to be > given a format specification, and you gave it a format string.? Instead > of format(num, "{0:.1f}"), you wanted format(num, ".1f").? If you had > tried "{0:.1f}".format(num) instead, you would have found that worked too. > Thanks Rhodri Dave, From barry at barrys-emacs.org Wed Sep 4 13:36:16 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Wed, 4 Sep 2019 18:36:16 +0100 Subject: CVE-2019-9636 - Can this be exploit over the wire? Message-ID: <436E9A7D-A829-4B2D-B892-834DF008A7B9@barrys-emacs.org> I have been looking into CVE-2019-9636 and I'm not sure that python code that works in bytes is vulnerable to this. The "trick" that to make the CVE dangerous assumes that you have a unicode string with \uff03 (FULLWIDTH NUMBER SIGN') that under NFKC turns into '#'. The discussion in https://bugs.python.org/issue36216 all the explaination starts with unicode string. What I'm interested in what happens if you get the URL as part of a HTML page or other mechanism. To that end I made a URL that if the vulnerability is triggered will change which search engine is visited. b'http://google.xn--combing-xr93b.com/fred' And when I use urlsplit() I get this: print( urlparse.urlsplit('http://google.xn--combing-xr93b.com/fred') ) SplitResult(scheme='http', netloc='google.xn--combing-xr93b.com', path='/fred', query='', fragment='') The netloc is still IDNA encoded so the "trick" did not trigger. If code then uses that netloc its going to fail to return anything as no domain name registrar should have register a name with illegal \uff03 in it. Also this raises an exception: 'google.xn--combing-xr93b.com'.decode('idna') Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python2.7/encodings/idna.py", line 193, in decode result.append(ToUnicode(label)) File "/usr/lib64/python2.7/encodings/idna.py", line 139, in ToUnicode raise UnicodeError("IDNA does not round-trip", label, label2) UnicodeError: ('IDNA does not round-trip', 'xn--combing-xr93b', 'com#bing') The conclusion I reached is that the CVE only applies to client code that allows a URL in unicode to be entered. Have I missed something important in the analysis? Barry From joel.goldstick at gmail.com Wed Sep 4 16:00:06 2019 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 4 Sep 2019 16:00:06 -0400 Subject: How to remove a string from a txt file? In-Reply-To: <03e69f5d-328f-4732-9c8c-72e828c96d94@googlegroups.com> References: <03e69f5d-328f-4732-9c8c-72e828c96d94@googlegroups.com> Message-ID: On Wed, Sep 4, 2019 at 11:11 AM Spencer Du wrote: > > Hi > > I want to remove a string from a txt file and then print out what I have removed. How do I do this. > > The txt file is in this format and should be kept in this format. > > txt.txt: > laser,cameras, > > Thanks > -- > https://mail.python.org/mailman/listinfo/python-list read the file line by line. if it doesn't contain your string, write it to a new file. for each line, use string functions to find the string you want to remove. One way is to split on ',' or use csvreader to get a list of words between comma. Print the one you like and join the values on each side of the text you are looking for. Write that to a file Lather, rinse and repeat -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From PythonList at DancesWithMice.info Wed Sep 4 16:00:47 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Thu, 5 Sep 2019 08:00:47 +1200 Subject: How to remove a string from a txt file? In-Reply-To: <03e69f5d-328f-4732-9c8c-72e828c96d94@googlegroups.com> References: <03e69f5d-328f-4732-9c8c-72e828c96d94@googlegroups.com> Message-ID: On 5/09/19 3:08 AM, Spencer Du wrote: > Hi > > I want to remove a string from a txt file and then print out what I have removed. How do I do this. > > The txt file is in this format and should be kept in this format. > > txt.txt: > laser,cameras, Is this a homework assignment? What code do you have so far? How to identify the beginning of the sub-string to be removed? How to identify the end of the sub-string? How does one "remove a string" AND "kept in this format"? -- Regards =dn From toby at tobiah.org Wed Sep 4 16:15:11 2019 From: toby at tobiah.org (Tobiah) Date: Wed, 4 Sep 2019 13:15:11 -0700 Subject: How to remove a string from a txt file? References: <03e69f5d-328f-4732-9c8c-72e828c96d94@googlegroups.com> Message-ID: On 9/4/19 8:08 AM, Spencer Du wrote: > Hi > > I want to remove a string from a txt file and then print out what I have removed. How do I do this. > > The txt file is in this format and should be kept in this format. > > txt.txt: > laser,cameras, > > Thanks > Do you want to remove one of the fields by using an argument? Like: my_py_solution txt.txt cameras Its not clear what you are trying to achieve. From PythonList at DancesWithMice.info Wed Sep 4 18:05:19 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Thu, 5 Sep 2019 10:05:19 +1200 Subject: Formatting floating point In-Reply-To: References: Message-ID: On 5/09/19 5:12 AM, Dave via Python-list wrote: ... > My question is why, and where do I find a reliable source of information > on formatting numbers?? Not interested in replacement values like '{} > {}'.format(1, 2). Agreed: there's ton(ne)s of information 'out there', much of it old, eg Python2, "formatter" (deprecated since v3.4), methods pre-dating f-strings, etc; and more of it rather casually thrown-out-there. Learning from StackOverflow (etc) has its limits/perils! Authoritative Python docs: https://docs.python.org/3/ The "Mini-Language": https://docs.python.org/3/library/string.html#formatspec Current advice (v3.6+) is to study the newer f-strings (formally "formatted string literals"): https://docs.python.org/3/tutorial/inputoutput.html which came from PEP 498: https://www.python.org/dev/peps/pep-0498/ -- Regards =dn From flebber.crue at gmail.com Wed Sep 4 21:46:15 2019 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Wed, 4 Sep 2019 18:46:15 -0700 (PDT) Subject: pandas loc on str lower for column comparison In-Reply-To: <812057cd-9f21-462c-b071-2e8f8d23bd12@googlegroups.com> References: <05e75ce6-34f3-41c3-a04e-3a900b98488a@googlegroups.com> <812057cd-9f21-462c-b071-2e8f8d23bd12@googlegroups.com> Message-ID: <7a516845-7daf-447e-84cd-64c9bc315c9f@googlegroups.com> On Sunday, 1 September 2019 10:48:54 UTC+10, Sayth Renshaw wrote: > I've created a share doc same structure anon data from my google drive. > > https://drive.google.com/file/d/0B28JfFTPNr_lckxQRnFTRF9UTEFYRUVqRWxCNVd1VEZhcVNr/view?usp=sharing > > Sayth I tried creating the df1 dataframe by using iloc instead of loc to avoid any column naming issues. So i created a list of integers for iloc representing the columns in current example. df1 = df.iloc[[0,1,5,6,7]] However, I ust be misunderstanding the docs https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html#pandas.DataFrame.iloc Allowed inputs are: An integer, e.g. 5. A list or array of integers, e.g. [4, 3, 0]. Because while it works I appear to grab all columns 13 when I requested 5. UID Name FTE Agent ID Current Leader New Leader Current Team New Team Current Site New Site Unnamed: 10 Unnamed: 11 Unnamed: 12 How do I misunderstand iloc? Thanks, Sayth From dieter at handshake.de Thu Sep 5 00:44:55 2019 From: dieter at handshake.de (dieter) Date: Thu, 05 Sep 2019 06:44:55 +0200 Subject: How do I give a decorator acces to the class of a decorated function References: Message-ID: <87blvzl5bs.fsf@handshake.de> Antoon Pardon writes: > What I am trying to do is the following. > > class MyClass (...) : > @register > def MyFunction(...) > ... > > What I would want is for the register decorator to somehow create/mutate > class variable(s) of MyClass. > > Is that possible or do I have to rethink my approach? As others have already explained: the decoration works an the function (not the method) level. A function knows nothing of a class. Others have already pointed out work arounds. I add an additional one: Instead of: class C: ... @decorate def f(...): ... ... you can use: class C: ... def f(...): ... ... decorate(C, C.f) In Python 2, "C.f" returns a method (an object with a reference to the class and the function) - there, you would not need the class parameter for "decorate". In Python 3, however, "C.f" is the function (without any reference to the class. From antoon.pardon at vub.be Thu Sep 5 04:00:19 2019 From: antoon.pardon at vub.be (Antoon Pardon) Date: Thu, 5 Sep 2019 10:00:19 +0200 Subject: How do I give a decorator acces to the class of a decorated function In-Reply-To: References: Message-ID: <1c109790-8043-2f3b-6fb2-7e1e7b8727ec@vub.be> On 4/09/19 17:46, Peter Otten wrote: > Antoon Pardon wrote: > >> What I am trying to do is the following. >> >> class MyClass (...) : >> @register >> def MyFunction(...) >> ... >> >> What I would want is for the register decorator to somehow create/mutate >> class variable(s) of MyClass. >> >> Is that possible or do I have to rethink my approach? > If you are willing to delegate the actual work to the metaclass call: > > def register(f): > f.registered = True > return f > > def registered(name, bases, namespace): > namespace["my_cool_functions"] = [ > n for n, v in namespace.items() > if getattr(v, "registered", False) > ] > return type(name, bases, namespace) > > class MyClass(metaclass=registered) : > @register > def foo(self): > pass > @register > def bar(self): > pass > def other(self): > pass > > print(MyClass.my_cool_functions) Thanks for this idea. I think I can make this work for me. -- Antoon. From skauser at rocketsoftware.com Thu Sep 5 04:48:32 2019 From: skauser at rocketsoftware.com (Saba Kauser) Date: Thu, 5 Sep 2019 08:48:32 +0000 Subject: "How to protect the python code" Message-ID: Hello Experts, I am looking for ways available to protect the python source code from being available to users for write/modify. Is it a good idea to think that python source code can be protected? I am aware that there are ways available to generate extensions like in C(.pyd files) and module the sensitive code likewise, but are there any better options available for protecting the native python source code itself. My requirement is to have the application/source in native python and not intermediate to other languages. Please share your thoughts. Thanks! -------------------------------------------------------------------- Saba Kauser E: skauser at rocketsoftware.com --------------------------------------------------------------------- ================================ Rocket Software, Inc. and subsidiaries ? 77 Fourth Avenue, Waltham MA 02451 ? Main Office Toll Free Number: +1 855.577.4323 Contact Customer Support: https://my.rocketsoftware.com/RocketCommunity/RCEmailSupport Unsubscribe from Marketing Messages/Manage Your Subscription Preferences - http://www.rocketsoftware.com/manage-your-email-preferences Privacy Policy - http://www.rocketsoftware.com/company/legal/privacy-policy ================================ This communication and any attachments may contain confidential information of Rocket Software, Inc. All unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please notify Rocket Software immediately and destroy all copies of this communication. Thank you. From rosuav at gmail.com Thu Sep 5 05:04:17 2019 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 5 Sep 2019 19:04:17 +1000 Subject: "How to protect the python code" In-Reply-To: References: Message-ID: On Thu, Sep 5, 2019 at 6:50 PM Saba Kauser wrote: > > Hello Experts, > > I am looking for ways available to protect the python source code from being available to users for write/modify. Run it on a server and don't let them see the source code. > Is it a good idea to think that python source code can be protected? Nope. If you're going to ship Python code for people to run on their own systems - or, honestly, *any* code - you should assume that they can read it themselves. The only way is to NOT ship the code, which (in today's world) usually means publishing it as a web app. ChrisA From antoon.pardon at vub.be Thu Sep 5 06:13:33 2019 From: antoon.pardon at vub.be (Antoon Pardon) Date: Thu, 5 Sep 2019 12:13:33 +0200 Subject: How do I give a decorator acces to the class of a decorated function In-Reply-To: References: Message-ID: <9708f7a6-5bee-b8ec-e49b-fd16a31bedd6@vub.be> On 4/09/19 17:46, Peter Otten wrote: > Antoon Pardon wrote: > >> What I am trying to do is the following. >> >> class MyClass (...) : >> @register >> def MyFunction(...) >> ... >> >> What I would want is for the register decorator to somehow create/mutate >> class variable(s) of MyClass. >> >> Is that possible or do I have to rethink my approach? > If you are willing to delegate the actual work to the metaclass call: > > def register(f): > f.registered = True > return f > > def registered(name, bases, namespace): > namespace["my_cool_functions"] = [ > n for n, v in namespace.items() > if getattr(v, "registered", False) > ] > return type(name, bases, namespace) > > class MyClass(metaclass=registered) : > @register > def foo(self): > pass > @register > def bar(self): > pass > def other(self): > pass > > print(MyClass.my_cool_functions) I have been playing with this idea and it looks promising. I was wondering about two points. 1) I guess I can add extra methods to my class through the metaclass by having something like the following in the registered function: def registered(name, bases, namespace): namespace["my_cool_functions"] = [ n for n, v in namespace.items() if getattr(v, "registered", False) ] namespace["__getitem__"] = lambda self, index: self.my_cool_functions[index] 2) Is it possible to make MyClass automatically a subclass of an other class through the metaclass? -- Antoon. From pankaj.jangid at gmail.com Thu Sep 5 06:10:36 2019 From: pankaj.jangid at gmail.com (pankaj.jangid at gmail.com) Date: Thu, 05 Sep 2019 15:40:36 +0530 Subject: How to only read words within brackets/ parentheses (in .txt file) using Python References: Message-ID: A S writes: > I understand that reading lines in .txt files would look something like this in Python: > > > with open('filename','r') as fd: > lines = fd.readlines() > > > However, how do I run my code to only read the words in my .txt files that are within each balanced parenthesis? > > I am not sure how to go about it, let's say my .txt file contents lines like this: > > kkkkk; > > select xx("xE'", PUT(xx.xxxx.),"'") jdfjhf:jhfjj from xxxx_x_xx_L ; > quit; > > The main idea is to read only these portions of the .txt file (i.e. Those within parentheses): > This should work for the outer parenthesis: import re p = re.compile(r"\((.+)\)", re.VERBOSE) with open('filename','r') as fd: lines = fd.readlines() for line in lines: m = p.findall(line) for s in m: print(s) -- Pankaj Jangid From __peter__ at web.de Thu Sep 5 09:30:56 2019 From: __peter__ at web.de (Peter Otten) Date: Thu, 05 Sep 2019 15:30:56 +0200 Subject: How do I give a decorator acces to the class of a decorated function References: <9708f7a6-5bee-b8ec-e49b-fd16a31bedd6@vub.be> Message-ID: Antoon Pardon wrote: > On 4/09/19 17:46, Peter Otten wrote: >> Antoon Pardon wrote: >> >>> What I am trying to do is the following. >>> >>> class MyClass (...) : >>> @register >>> def MyFunction(...) >>> ... >>> >>> What I would want is for the register decorator to somehow create/mutate >>> class variable(s) of MyClass. >>> >>> Is that possible or do I have to rethink my approach? >> If you are willing to delegate the actual work to the metaclass call: >> >> def register(f): >> f.registered = True >> return f >> >> def registered(name, bases, namespace): >> namespace["my_cool_functions"] = [ >> n for n, v in namespace.items() >> if getattr(v, "registered", False) >> ] >> return type(name, bases, namespace) >> >> class MyClass(metaclass=registered) : >> @register >> def foo(self): >> pass >> @register >> def bar(self): >> pass >> def other(self): >> pass >> >> print(MyClass.my_cool_functions) > > I have been playing with this idea and it looks promising. I was wondering > about two points. > > 1) I guess I can add extra methods to my class through the metaclass by > having something like the following in the registered function: > > def registered(name, bases, namespace): > namespace["my_cool_functions"] = [ > n for n, v in namespace.items() > if getattr(v, "registered", False) > ] > namespace["__getitem__"] = lambda self, index: > self.my_cool_functions[index] Methods are just functions as class attributes, so yes. Problems may arise with __private attributes and super(). > > 2) Is it possible to make MyClass automatically a subclass of an other > class > through the metaclass? > While you can modify `bases` before passing it on to `type` this starts to get a bit messy. Maybe you need a real metaclass which unlike the registered function above is shared by the subclasses... import random def register(f): f.registered = True return f class Foo: pass class Bar: pass class RegisterMeta(type): def __new__(cls, name, bases, namespace): namespace["my_cool_functions"] = [ n for n, v in namespace.items() if getattr(v, "registered", False) ] return type.__new__( cls, name, bases + (random.choice([Foo, Bar]),), namespace ) class RegisterBase(metaclass=RegisterMeta): def __getitem__(self, i): return self.my_cool_functions[i] class MyClass(RegisterBase): @register def foo(self): pass @register def bar(self): pass def other(self): pass print(MyClass.my_cool_functions) print(MyClass()[0]) print(MyClass.my_cool_functions is RegisterBase.my_cool_functions) # False print(MyClass.__bases__, RegisterBase.__bases__) ...or something else entirely. Can you provide some context? From antoon.pardon at vub.be Thu Sep 5 10:11:22 2019 From: antoon.pardon at vub.be (Antoon Pardon) Date: Thu, 5 Sep 2019 16:11:22 +0200 Subject: How do I give a decorator acces to the class of a decorated function In-Reply-To: References: <9708f7a6-5bee-b8ec-e49b-fd16a31bedd6@vub.be> Message-ID: On 5/09/19 15:30, Peter Otten wrote: >> 2) Is it possible to make MyClass automatically a subclass of an other >> class >> through the metaclass? >> > While you can modify `bases` before passing it on to `type` this starts to > get a bit messy. Maybe you need a real metaclass which unlike the registered > function above is shared by the subclasses... > > import random > > def register(f): > f.registered = True > return f > > class Foo: pass > class Bar: pass > > class RegisterMeta(type): > def __new__(cls, name, bases, namespace): > namespace["my_cool_functions"] = [ > n for n, v in namespace.items() > if getattr(v, "registered", False) > ] > return type.__new__( > cls, name, > bases + (random.choice([Foo, Bar]),), > namespace > ) > > class RegisterBase(metaclass=RegisterMeta): > def __getitem__(self, i): > return self.my_cool_functions[i] > > class MyClass(RegisterBase): > @register > def foo(self): > pass > @register > def bar(self): > pass > def other(self): > pass > > print(MyClass.my_cool_functions) > print(MyClass()[0]) > print(MyClass.my_cool_functions is RegisterBase.my_cool_functions) # False > print(MyClass.__bases__, RegisterBase.__bases__) > > > ...or something else entirely. Can you provide some context? Sure I am researching the possibility of writing an easy to use lexing/parsing tool. The idea is to write your lexer/parser as follows: class Calculator(metaclass = ...): def __init__(self): self.names = set() self.table = {} @token(r'\d+') def NUMBER(self, st): return int(st) @token(r'\w+') def VAR(self, st): self.names.add(st) return st @production(r"VAR '=' NUMBER") def assign(self, prd): name = prd[0] val = prd[1] if name in self.names: self.table[name] = value else: raise CalcError("variable (%s) not available" % name) calc = Calculator() calc("a = 7") So the token en production decorators register a regex/prodcution with a specific method to be called in specific circumstances when parsing a string. So I need the lexing and parsing algorithms available to this class, either by adding methods to the class or by making a subclass of the class where they are implemented. -- Antoon Pardon. From __peter__ at web.de Thu Sep 5 10:31:04 2019 From: __peter__ at web.de (Peter Otten) Date: Thu, 05 Sep 2019 16:31:04 +0200 Subject: How do I give a decorator acces to the class of a decorated function References: <9708f7a6-5bee-b8ec-e49b-fd16a31bedd6@vub.be> Message-ID: Antoon Pardon wrote: > On 5/09/19 15:30, Peter Otten wrote: >> Can you provide some context? > > Sure I am researching the possibility of writing an easy to use > lexing/parsing tool. The idea is to write your lexer/parser as > follows: > > class Calculator(metaclass = ...): > def __init__(self): > self.names = set() > self.table = {} > > @token(r'\d+') > def NUMBER(self, st): > return int(st) > > @token(r'\w+') > def VAR(self, st): > self.names.add(st) > return st > > @production(r"VAR '=' NUMBER") > def assign(self, prd): > name = prd[0] > val = prd[1] > if name in self.names: > self.table[name] = value > else: > raise CalcError("variable (%s) not available" % name) > > calc = Calculator() > calc("a = 7") > > So the token en production decorators register a regex/prodcution with > a specific method to be called in specific circumstances when parsing > a string. > > So I need the lexing and parsing algorithms available to this class, > either by adding methods to the class or by making a subclass of the > class where they are implemented. Ok, that looks like a nice interface to me, and I don't expect the metaclass dance to make it harder to implement than necessary ;) From blmadhavan at gmail.com Thu Sep 5 01:41:17 2019 From: blmadhavan at gmail.com (Madhavan Bomidi) Date: Wed, 4 Sep 2019 22:41:17 -0700 (PDT) Subject: Wind Rose Plotting in Python Message-ID: <84e08c61-90b4-4e2d-a5d5-cfb9f5aaed38@googlegroups.com> Hi, Can someone help me on how to make the wind rose plotting (similar to the figure 2 in the paper: https://agupubs.onlinelibrary.wiley.com/doi/epdf/10.1029/2011JD016386) in Python? The input file contains the data in 4 columns: [date, time, wind_speed, wind_direction] Look forward to your support and suggestions. From random832 at fastmail.com Thu Sep 5 11:05:10 2019 From: random832 at fastmail.com (Random832) Date: Thu, 05 Sep 2019 11:05:10 -0400 Subject: CVE-2019-9636 - Can this be exploit over the wire? In-Reply-To: <436E9A7D-A829-4B2D-B892-834DF008A7B9@barrys-emacs.org> References: <436E9A7D-A829-4B2D-B892-834DF008A7B9@barrys-emacs.org> Message-ID: On Wed, Sep 4, 2019, at 13:36, Barry Scott wrote: > I have been looking into CVE-2019-9636 and I'm not sure that > python code that works in bytes is vulnerable to this. I'm not convinced that the CVE (or, at least, the description in the bug report... it's also unclear to me whether this is an accurate example of the CVE) is valid at all. That is, I don't think its suggestion that browsers generally use compatibility normalization in decomposing URLs is correct. I tried the given address "https://example.com\uff03 at bing.com" (with actual \uff03 character) in Firefox, Chrome, and Edge, and they all accessed bing.com. From random832 at fastmail.com Thu Sep 5 11:18:46 2019 From: random832 at fastmail.com (Random832) Date: Thu, 05 Sep 2019 11:18:46 -0400 Subject: CVE-2019-9636 - Can this be exploit over the wire? In-Reply-To: <436E9A7D-A829-4B2D-B892-834DF008A7B9@barrys-emacs.org> References: <436E9A7D-A829-4B2D-B892-834DF008A7B9@barrys-emacs.org> Message-ID: <51434a80-b677-4fa5-9cb2-29602da3c156@www.fastmail.com> On Wed, Sep 4, 2019, at 13:36, Barry Scott wrote: > The conclusion I reached is that the CVE only applies to client code > that allows a URL in unicode to be entered. > > Have I missed something important in the analysis? While as I mentioned in my other post I'm not sure if the CVE's analysis of URL behavior is correct generally, you have missed the fact that an HTML page can provide URLs in unicode, either with the page itself encoded in UTF-8, or with whatever characters escaped as XML character references... not only as bytes in IDNA or percent-escaped hex. The same principle applies to other formats in which URLs might be interchanged as encoded unicode strings, such as JSON. The fact that accessing such a URL requires converting the non-ASCII parts to IDNA (for the domain part) or percent-escaped hex (for other parts) doesn't limit this to user input. like this From rzzzwilson at gmail.com Thu Sep 5 11:49:01 2019 From: rzzzwilson at gmail.com (Ross Wilson) Date: Thu, 5 Sep 2019 22:49:01 +0700 Subject: Wind Rose Plotting in Python In-Reply-To: <84e08c61-90b4-4e2d-a5d5-cfb9f5aaed38@googlegroups.com> References: <84e08c61-90b4-4e2d-a5d5-cfb9f5aaed38@googlegroups.com> Message-ID: On Thu, 5 Sep 2562 at 22:00 Madhavan Bomidi wrote: > Hi, > > Can someone help me on how to make the wind rose plotting (similar to the > figure 2 in the paper: > https://agupubs.onlinelibrary.wiley.com/doi/epdf/10.1029/2011JD016386) in > Python? > > The input file contains the data in 4 columns: > > [date, time, wind_speed, wind_direction] > > Look forward to your support and suggestions. > Hi Madhavan, I can only see the first page of the paper you linked, so this might not help. Try looking at the "windrose" third-party library for matplotlib at https://github.com/python-windrose/windrose . The matplotlib page of third-party libraries is at https://matplotlib.org/3.1.1/thirdpartypackages/index.html#windrose . Ross From skauser at rocketsoftware.com Thu Sep 5 12:19:54 2019 From: skauser at rocketsoftware.com (Saba Kauser) Date: Thu, 5 Sep 2019 16:19:54 +0000 Subject: "How to protect the python code" In-Reply-To: References: Message-ID: Thanks Chris. Makes sense! -----Original Message----- From: Chris Angelico Sent: Thursday, September 5, 2019 2:34 PM To: python-list at python.org Subject: Re: "How to protect the python code" On Thu, Sep 5, 2019 at 6:50 PM Saba Kauser wrote: > > Hello Experts, > > I am looking for ways available to protect the python source code from being available to users for write/modify. Run it on a server and don't let them see the source code. > Is it a good idea to think that python source code can be protected? Nope. If you're going to ship Python code for people to run on their own systems - or, honestly, *any* code - you should assume that they can read it themselves. The only way is to NOT ship the code, which (in today's world) usually means publishing it as a web app. ChrisA ================================ Rocket Software, Inc. and subsidiaries ? 77 Fourth Avenue, Waltham MA 02451 ? Main Office Toll Free Number: +1 855.577.4323 Contact Customer Support: https://my.rocketsoftware.com/RocketCommunity/RCEmailSupport Unsubscribe from Marketing Messages/Manage Your Subscription Preferences - http://www.rocketsoftware.com/manage-your-email-preferences Privacy Policy - http://www.rocketsoftware.com/company/legal/privacy-policy ================================ This communication and any attachments may contain confidential information of Rocket Software, Inc. All unauthorized use, disclosure or distribution is prohibited. If you are not the intended recipient, please notify Rocket Software immediately and destroy all copies of this communication. Thank you. From torriem at gmail.com Thu Sep 5 14:03:27 2019 From: torriem at gmail.com (Michael Torrie) Date: Thu, 5 Sep 2019 12:03:27 -0600 Subject: "How to protect the python code" In-Reply-To: References: Message-ID: <16083357-38c8-69da-4bd1-86773865a134@gmail.com> On 9/5/19 2:48 AM, Saba Kauser wrote: > I am looking for ways available to protect the python source code > from being available to users for write/modify. Is it a good idea to > think that python source code can be protected? In general, no, not with an interpreted language. Intellectual property is protected by copyright and patents. The Python code you write is copyright by you and by few rights are granted to the end user by default, as governed by the license you subject them to. Copyright holds whether a person can view the source code or not. And of course in the case of patents that means everything must be published for all to see, so these are open by nature. (They aren't secret to begin with.) Most end users have very little curiosity about reading your Python code. In fact they don't even know or care that it is Python. Trying to hide or obfuscate the code is pointless in my opinion, and a waste of time and money. I've never understood why companies are so obsessed with preventing users from modifying their programs. If a program doesn't quite meet the needs of the end user, and it is possible for him to modify it to do so, I say go for it. As long as he doesn't distribute a a derivative product and violate the copyright, I don't see the problem. On one occasion I was using a proprietary Java application (compiled of course) that had a silly thing where it would refuse to run under some graphics conditions, such as 16-bit color over RDP. So I decompiled the class where the check was and changed it. There were good reasons for this check, but in my case it didn't matter. From grant.b.edwards at gmail.com Thu Sep 5 14:24:02 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Thu, 5 Sep 2019 18:24:02 -0000 (UTC) Subject: "How to protect the python code" References: <16083357-38c8-69da-4bd1-86773865a134@gmail.com> Message-ID: On 2019-09-05, Michael Torrie wrote: > I've never understood why companies are so obsessed with preventing > users from modifying their programs. Because they'll change it, the result won't behave correctly, and then the user will waste a lot of tech-support hours or ruin the company's reputation among others who are trying to use the now-broken program? -- Grant Edwards grant.b.edwards Yow! Is this TERMINAL fun? at gmail.com From barry at barrys-emacs.org Thu Sep 5 13:57:43 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Thu, 5 Sep 2019 18:57:43 +0100 Subject: CVE-2019-9636 - Can this be exploit over the wire? In-Reply-To: <51434a80-b677-4fa5-9cb2-29602da3c156@www.fastmail.com> References: <436E9A7D-A829-4B2D-B892-834DF008A7B9@barrys-emacs.org> <51434a80-b677-4fa5-9cb2-29602da3c156@www.fastmail.com> Message-ID: <29135C8F-2ED4-4C1E-8E6D-DB1234421F2D@barrys-emacs.org> > On 5 Sep 2019, at 16:18, Random832 wrote: Thanks for taking the time to reply. > > On Wed, Sep 4, 2019, at 13:36, Barry Scott wrote: >> The conclusion I reached is that the CVE only applies to client code >> that allows a URL in unicode to be entered. >> >> Have I missed something important in the analysis? > > While as I mentioned in my other post I'm not sure if the CVE's analysis of URL behavior is correct generally, Agreed, would have liked to have had more details and context. > you have missed the fact that an HTML page can provide URLs in unicode, either with the page itself encoded in UTF-8, or with whatever characters escaped as XML character references... not only as bytes in IDNA or percent-escaped hex. The same principle applies to other formats in which URLs might be interchanged as encoded unicode strings, such as JSON. The fact that accessing such a URL requires converting the non-ASCII parts to IDNA (for the domain part) or percent-escaped hex (for other parts) doesn't limit this to user input. > > like this That gets the unicode version into the app and then the bug can be triggered. In my case this is not a way in as the code does not parse web pages. Barry > -- > https://mail.python.org/mailman/listinfo/python-list > From storchaka at gmail.com Thu Sep 5 14:32:08 2019 From: storchaka at gmail.com (Serhiy Storchaka) Date: Thu, 5 Sep 2019 21:32:08 +0300 Subject: How do I give a decorator acces to the class of a decorated function In-Reply-To: References: Message-ID: 04.09.19 17:21, Antoon Pardon ????: > What I am trying to do is the following. > > class MyClass (...) : > @register > def MyFunction(...) > ... > > What I would want is for the register decorator to somehow create/mutate > class variable(s) of MyClass. > > Is that possible or do I have to rethink my approach? > You can make register() returning a descriptor with the __set_name__() method. A bit of black magic: class register: def __init__(self, func): self.func = func def __get__(self, instance, owner): return self.func.__get__(instance, owner) def __set_name__(self, owner, name): if not hasattr(owner, 'my_cool_functions'): owner.my_cool_functions = [] owner.my_cool_functions.append(name) setattr(owner, name, self.func) From mal at europython.eu Thu Sep 5 14:31:50 2019 From: mal at europython.eu (M.-A. Lemburg) Date: Thu, 5 Sep 2019 20:31:50 +0200 Subject: EuroPython 2019 - Videos for Wednesday available Message-ID: We are pleased to announce the first batch of cut videos from EuroPython 2019 in Basel, Switzerland. * EuroPython 2019 YouTube Channel * http://europython.tv/ In this batch, we have included all videos for Wednesday, July 10 2019, the first conference day. In the coming two weeks we will publish videos for the next two conference days. In total, we will have more than 130 videos available for you to watch. All EuroPython videos, including the ones from previous conferences, are available on our EuroPython YouTube Channel. Help spread the word -------------------- Please help us spread this message by sharing it on your social networks as widely as possible. Thank you ! Link to the blog post: https://blog.europython.eu/post/187487578492/europython-2019-videos-for-wednesday-available Tweet: https://twitter.com/europython/status/1169233806764728320 Enjoy, -- EuroPython 2019 Team https://ep2019.europython.eu/ https://www.europython-society.org/ From rosuav at gmail.com Thu Sep 5 15:00:09 2019 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 6 Sep 2019 05:00:09 +1000 Subject: How do I give a decorator acces to the class of a decorated function In-Reply-To: References: Message-ID: On Fri, Sep 6, 2019 at 4:33 AM Serhiy Storchaka wrote: > > 04.09.19 17:21, Antoon Pardon ????: > > What I am trying to do is the following. > > > > class MyClass (...) : > > @register > > def MyFunction(...) > > ... > > > > What I would want is for the register decorator to somehow create/mutate > > class variable(s) of MyClass. > > > > Is that possible or do I have to rethink my approach? > > > > You can make register() returning a descriptor with the __set_name__() > method. Was not aware of that. Cool! For those not familiar with it: https://docs.python.org/3/reference/datamodel.html#object.__set_name__ ChrisA From venugopal554 at gmail.com Thu Sep 5 17:20:58 2019 From: venugopal554 at gmail.com (venu gopal reddy) Date: Thu, 5 Sep 2019 17:20:58 -0400 Subject: Fwd: Installation Errors In-Reply-To: References: Message-ID: Hi Python Support Team, I have subscribed now and re-sending my query. Please suggest. I have an Issue installing python 3.7 on my work computer. It says core.msi package not found during installation. Can you please help me with steps to resolve this issue? Thank you, Venu From piet-l at vanoostrum.org Thu Sep 5 17:44:03 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Thu, 05 Sep 2019 23:44:03 +0200 Subject: pandas loc on str lower for column comparison References: <05e75ce6-34f3-41c3-a04e-3a900b98488a@googlegroups.com> <812057cd-9f21-462c-b071-2e8f8d23bd12@googlegroups.com> <7a516845-7daf-447e-84cd-64c9bc315c9f@googlegroups.com> Message-ID: Sayth Renshaw writes: > On Sunday, 1 September 2019 10:48:54 UTC+10, Sayth Renshaw wrote: >> I've created a share doc same structure anon data from my google drive. >> >> https://drive.google.com/file/d/0B28JfFTPNr_lckxQRnFTRF9UTEFYRUVqRWxCNVd1VEZhcVNr/view?usp=sharing >> >> Sayth > > I tried creating the df1 dataframe by using iloc instead of loc to avoid any column naming issues. > > So i created a list of integers for iloc representing the columns in current example. > > df1 = df.iloc[[0,1,5,6,7]] > > However, I ust be misunderstanding the docs > https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.iloc.html#pandas.DataFrame.iloc > Allowed inputs are: > > An integer, e.g. 5. > A list or array of integers, e.g. [4, 3, 0]. > > Because while it works I appear to grab all columns 13 when I requested 5. > UID Name FTE Agent ID Current Leader New Leader Current Team New > Team Current Site New Site Unnamed: 10 Unnamed: 11 Unnamed: 12 > > How do I misunderstand iloc? > That would select ROWS 0,1,5,6,7, not columns. To select columns 0,1,5,6,7, use two-dimensional indexes df1 = df.iloc[:, [0,1,5,6,7]] : selects all rows. -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From piet-l at vanoostrum.org Thu Sep 5 17:52:43 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Thu, 05 Sep 2019 23:52:43 +0200 Subject: pandas loc on str lower for column comparison References: <05e75ce6-34f3-41c3-a04e-3a900b98488a@googlegroups.com> <812057cd-9f21-462c-b071-2e8f8d23bd12@googlegroups.com> <7a516845-7daf-447e-84cd-64c9bc315c9f@googlegroups.com> Message-ID: Piet van Oostrum writes: > That would select ROWS 0,1,5,6,7, not columns. > To select columns 0,1,5,6,7, use two-dimensional indexes > > df1 = df.iloc[:, [0,1,5,6,7]] > > : selects all rows. And that also solves your original problem. This statement: df1['Difference'] = df1.loc['Current Team'].str.lower().str.strip() == df1.loc['New Team'].str.lower().str.strip() should not use .loc, because then you are selecting rows, not columns, but: df1['Difference'] = df1['Current Team'].str.lower().str.strip() == df1['New Team'].str.lower().str.strip() -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From rgaddi at highlandtechnology.invalid Thu Sep 5 19:05:23 2019 From: rgaddi at highlandtechnology.invalid (Rob Gaddi) Date: Thu, 5 Sep 2019 16:05:23 -0700 Subject: Renaming an import Message-ID: I'm trying to figure out how to rename an import globally for an entire package. Something like: pkg/__init__.py: import graphing_module_b as graph pkg/foobar.py: from .graph import plot, axis The point being that, if at some point I decide to change from graphing_module_b to graphing_module_a, that decision is made at a single central point in my package rather than scattered through 30 different import statements in a dozen files. Any ideas? -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix. From flebber.crue at gmail.com Thu Sep 5 19:24:10 2019 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Thu, 5 Sep 2019 16:24:10 -0700 (PDT) Subject: pandas loc on str lower for column comparison In-Reply-To: References: <05e75ce6-34f3-41c3-a04e-3a900b98488a@googlegroups.com> <812057cd-9f21-462c-b071-2e8f8d23bd12@googlegroups.com> <7a516845-7daf-447e-84cd-64c9bc315c9f@googlegroups.com> Message-ID: That is actually consistent with Excel row, column. Can see why it works that way then. Thanks From knomenet at gmail.com Thu Sep 5 20:31:47 2019 From: knomenet at gmail.com (Michael Speer) Date: Thu, 5 Sep 2019 20:31:47 -0400 Subject: Renaming an import In-Reply-To: References: Message-ID: pkg/graph.py: from graphing_module_b import plot, axis pkg/foobar.py: from .graph import plot, axis Would it be sufficient to use a file for indirection? On Thu, Sep 5, 2019 at 7:11 PM Rob Gaddi wrote: > I'm trying to figure out how to rename an import globally for an entire > package. > Something like: > > pkg/__init__.py: > import graphing_module_b as graph > > pkg/foobar.py: > from .graph import plot, axis > > The point being that, if at some point I decide to change from > graphing_module_b > to graphing_module_a, that decision is made at a single central point in > my > package rather than scattered through 30 different import statements in a > dozen > files. > > Any ideas? > > -- > Rob Gaddi, Highland Technology -- www.highlandtechnology.com > Email address domain is currently out of order. See above to fix. > -- > https://mail.python.org/mailman/listinfo/python-list > From cs at cskk.id.au Thu Sep 5 23:19:27 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Fri, 6 Sep 2019 13:19:27 +1000 Subject: Renaming an import In-Reply-To: References: Message-ID: <20190906031927.GA38980@cskk.homeip.net> On 05Sep2019 20:31, Michael Speer wrote: >pkg/graph.py: > from graphing_module_b import plot, axis > >pkg/foobar.py: > from .graph import plot, axis > >Would it be sufficient to use a file for indirection? Or without a stub file and only slightly less conveniently: pkg/__init__.py import graphing_module_b as graph pkg/foobar.py from . import graph plot = graph.plot axis = graph.axis Alternatively: pkg/__init__.py import graphing_module_b as graph plot = graph.plot axis = graph.axis pkg/foobar.py from . import graph, plot, axis Cheers, Cameron Simpson From dieter at handshake.de Fri Sep 6 01:13:55 2019 From: dieter at handshake.de (dieter) Date: Fri, 06 Sep 2019 07:13:55 +0200 Subject: Renaming an import References: Message-ID: <87ef0u57n0.fsf@handshake.de> Rob Gaddi writes: > I'm trying to figure out how to rename an import globally for an > entire package. Something like: > > pkg/__init__.py: > import graphing_module_b as graph > > pkg/foobar.py: > from .graph import plot, axis > > The point being that, if at some point I decide to change from > graphing_module_b to graphing_module_a, that decision is made at a > single central point in my package rather than scattered through 30 > different import statements in a dozen files. > > Any ideas? You might have a look at `zope.deferredimport`. Its primary task is to allow graceful handling of a change in package/module structure: the Zope ecosystem consists of a huge number of independent packages which should get time to adapt to such changes. `zope.deferredimport` allows to define proxy modules as placeholders for the original modules which relay imports to the new locations. In addition, it generates warnings of the form "please import from ..." to inform about the new structure. That said: if you change the module structure, then I would recommend that you change your own code accordingly and do not rely on advanced features to keep the old names/locations: this makes understanding of the code significantly more difficult. From __peter__ at web.de Fri Sep 6 02:36:56 2019 From: __peter__ at web.de (Peter Otten) Date: Fri, 06 Sep 2019 08:36:56 +0200 Subject: Renaming an import References: Message-ID: Rob Gaddi wrote: > I'm trying to figure out how to rename an import globally for an entire > package. > Something like: > > pkg/__init__.py: > import graphing_module_b as graph If you want to go low-level: sys.modules["pkg.graph"] = graph will make > pkg/foobar.py: > from .graph import plot, axis work. > The point being that, if at some point I decide to change from > graphing_module_b to graphing_module_a, that decision is made at a single > central point in my package rather than scattered through 30 different > import statements in a dozen files. > > Any ideas? > From dobedani at gmx.net Fri Sep 6 09:05:01 2019 From: dobedani at gmx.net (Dobedani) Date: Fri, 6 Sep 2019 06:05:01 -0700 (PDT) Subject: Where can I find libtiff binary for 64-bit windows - for use in conjunction with Python package libtiff Message-ID: <70871d3a-ab8c-40fa-ba89-c44f0338f734@googlegroups.com> Hi there! I'm using Python package libtiff on Windows (version 0.4.2) - see also https://github.com/pearu/pylibtiff. For this package to work well, it requires you to have a libtiff.dll in your PATH. For 32-bits I got my DLL here: http://gnuwin32.sourceforge.net/packages/tiff.htm (version 3.8.2) and I'm running my code with success. I would like to run my Python code also with a 64-bit version of Python, so I need a 64-bit of the above-mentioned binary / DLL. My problem is that I can't find any reliable site where it's available for download. Okay, I have come across several sites where one can download Visual Studio projects to compile libtiff by yourself - not really my piece of cake. Only from one site I was able to download some binary files: https://www.bruot.org/hp/libraries/ (libtiff binaries, version 4.0.7). However, the archive contains 2 files in folder lib: tiff.dll and tiff.lib. The Python libtiff code does not recognise these binaries. When I rename then to libtiff.dll and libtiff.lib, then it does not also work: AttributeError: function 'TIFFGetVersion' not found. Am I missing something? What am I doing wrong? From fabiofz at gmail.com Fri Sep 6 09:25:07 2019 From: fabiofz at gmail.com (Fabio Zadrozny) Date: Fri, 6 Sep 2019 10:25:07 -0300 Subject: "How to protect the python code" In-Reply-To: References: Message-ID: On Thu, Sep 5, 2019 at 5:49 AM Saba Kauser wrote: > Hello Experts, > > I am looking for ways available to protect the python source code from > being available to users for write/modify. > Is it a good idea to think that python source code can be protected? > > I am aware that there are ways available to generate extensions like in > C(.pyd files) and module the sensitive code likewise, but are there any > better options available for protecting the native python source code > itself. My requirement is to have the application/source in native python > and not intermediate to other languages. > > Please share your thoughts. > As far as I know, the best way to protect your code is to actually use something as Nuitka or Cython to compile your pure-python code to native extensions. Now, if you don't want that, you can try to obfuscate your code (just google "python obfuscate code" -- I haven't actually used any of those, but I guess they may be what you want)... -- Fabio From python at mrabarnett.plus.com Fri Sep 6 12:29:04 2019 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 6 Sep 2019 17:29:04 +0100 Subject: Where can I find libtiff binary for 64-bit windows - for use in conjunction with Python package libtiff In-Reply-To: <70871d3a-ab8c-40fa-ba89-c44f0338f734@googlegroups.com> References: <70871d3a-ab8c-40fa-ba89-c44f0338f734@googlegroups.com> Message-ID: On 2019-09-06 14:05, Dobedani wrote: > Hi there! > I'm using Python package libtiff on Windows (version 0.4.2) - see also https://github.com/pearu/pylibtiff. For this package to work well, it requires you to have a libtiff.dll in your PATH. For 32-bits I got my DLL here: http://gnuwin32.sourceforge.net/packages/tiff.htm (version 3.8.2) and I'm running my code with success. > > I would like to run my Python code also with a 64-bit version of Python, so I need a 64-bit of the above-mentioned binary / DLL. My problem is that I can't find any reliable site where it's available for download. > > Okay, I have come across several sites where one can download Visual Studio projects to compile libtiff by yourself - not really my piece of cake. > > Only from one site I was able to download some binary files: https://www.bruot.org/hp/libraries/ (libtiff binaries, version 4.0.7). However, the archive contains 2 files in folder lib: tiff.dll and tiff.lib. The Python libtiff code does not recognise these binaries. When I rename then to libtiff.dll and libtiff.lib, then it does not also work: AttributeError: function 'TIFFGetVersion' not found. Am I missing something? What am I doing wrong? > Have you tried Christoph Gohlke's site? https://www.lfd.uci.edu/~gohlke/pythonlibs/ From dobedani at gmx.net Fri Sep 6 12:37:33 2019 From: dobedani at gmx.net (Dobedani) Date: Fri, 6 Sep 2019 09:37:33 -0700 (PDT) Subject: Where can I find libtiff binary for 64-bit windows - for use in conjunction with Python package libtiff In-Reply-To: References: <70871d3a-ab8c-40fa-ba89-c44f0338f734@googlegroups.com> Message-ID: <0a24c778-13ed-4571-aa6c-7eeb27bc5868@googlegroups.com> Thanks for the answer. That site provides wheels for installing the Python libtiff package - great if "pip install libtiff" does not work out well. Besides that package, you'll need to have a 64-bits DLL on your system for it to work. That's the DLL I'm looking for. From spencerdu at hotmail.co.uk Fri Sep 6 13:11:06 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Fri, 6 Sep 2019 10:11:06 -0700 (PDT) Subject: Help me fix a problem Message-ID: Hi I want to print yes in gui.py but it does not get printed because of the json. How do I fix this. Execute embedded.py and then gui.py to test. embedded.py import paho.mqtt.client as mqtt from mqtt import * client = mqtt.Client() client.connect("broker.hivemq.com",1883,60) client.on_connect = on_connect client.subscribe("topic/test") client.on_subscribe = on_subscribe print("Subscribing to topic", "topic/test") client.on_message = on_message client.loop_forever() gui.py import paho.mqtt.client as mqtt from mqtt import * import json # This is the Publisher client = mqtt.Client() client.connect("broker.hivemq.com",1883,60) print("Publishing message (name: Hello world!) to topic", "topic/test") client.publish("topic/test",json.dumps({"name": "Hello world!"})); client.loop_forever(); mqtt.py import logging import paho.mqtt.client as mqtt def on_connect(client, userdata, flags, rc): print("Connecting to broker") # client.subscribe("topic/test") def on_subscribe(client, userdata, mid, granted_qos): print("I've subscribed to topic") def on_message(client, userdata, msg): print("message recieved= " + msg.payload.decode()) # print("File which you want to import(with .py extension)") print("message topic=", msg.topic) print("message qos=", msg.qos) print("message retain flag=", msg.retain) if msg.payload[name] == "Hello world!": print("Yes!") Regards Spencer From python at mrabarnett.plus.com Fri Sep 6 14:15:23 2019 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 6 Sep 2019 19:15:23 +0100 Subject: Help me fix a problem In-Reply-To: References: Message-ID: <24e1253c-57f0-31a4-76ce-d185a959c596@mrabarnett.plus.com> On 2019-09-06 18:11, Spencer Du wrote: > Hi > > I want to print yes in gui.py but it does not get printed because of the json. How do I fix this. Execute embedded.py and then gui.py to test. > > def on_message(client, userdata, msg): > print("message recieved= " + msg.payload.decode()) > # print("File which you want to import(with .py extension)") > print("message topic=", msg.topic) > print("message qos=", msg.qos) > print("message retain flag=", msg.retain) > > if msg.payload[name] == "Hello world!": > print("Yes!") > What is the value of the variable called 'name'? Or did you intend that to be a string? if msg.payload["name"] == "Hello world!": print("Yes!") From spencerdu at hotmail.co.uk Fri Sep 6 15:47:08 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Fri, 6 Sep 2019 12:47:08 -0700 (PDT) Subject: Help me fix a problem In-Reply-To: References: <24e1253c-57f0-31a4-76ce-d185a959c596@mrabarnett.plus.com> Message-ID: <03e10b6d-9ba9-493a-9a21-90bd6c9e3419@googlegroups.com> On Friday, 6 September 2019 20:15:40 UTC+2, MRAB wrote: > On 2019-09-06 18:11, Spencer Du wrote: > > Hi > > > > I want to print yes in gui.py but it does not get printed because of the json. How do I fix this. Execute embedded.py and then gui.py to test. > > > > def on_message(client, userdata, msg): > > print("message recieved= " + msg.payload.decode()) > > # print("File which you want to import(with .py extension)") > > print("message topic=", msg.topic) > > print("message qos=", msg.qos) > > print("message retain flag=", msg.retain) > > > > if msg.payload[name] == "Hello world!": > > print("Yes!") > > > What is the value of the variable called 'name'? Or did you intend that > to be a string? > > if msg.payload["name"] == "Hello world!": > print("Yes!") {"name": "Hello world!"} is a key value pair. From spencerdu at hotmail.co.uk Fri Sep 6 15:48:53 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Fri, 6 Sep 2019 12:48:53 -0700 (PDT) Subject: Help me fix a problem In-Reply-To: References: <24e1253c-57f0-31a4-76ce-d185a959c596@mrabarnett.plus.com> Message-ID: On Friday, 6 September 2019 20:15:40 UTC+2, MRAB wrote: > On 2019-09-06 18:11, Spencer Du wrote: > > Hi > > > > I want to print yes in gui.py but it does not get printed because of the json. How do I fix this. Execute embedded.py and then gui.py to test. > > > > def on_message(client, userdata, msg): > > print("message recieved= " + msg.payload.decode()) > > # print("File which you want to import(with .py extension)") > > print("message topic=", msg.topic) > > print("message qos=", msg.qos) > > print("message retain flag=", msg.retain) > > > > if msg.payload[name] == "Hello world!": > > print("Yes!") > > > What is the value of the variable called 'name'? Or did you intend that > to be a string? > > if msg.payload["name"] == "Hello world!": > print("Yes!") "name" is part of {"name": "Hello world!"} which is a key value pair dictionary and json. From spencerdu at hotmail.co.uk Fri Sep 6 15:49:56 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Fri, 6 Sep 2019 12:49:56 -0700 (PDT) Subject: Help me fix a problem In-Reply-To: References: <24e1253c-57f0-31a4-76ce-d185a959c596@mrabarnett.plus.com> Message-ID: <6f7d2345-e186-4e52-8e0c-edccf38d8d89@googlegroups.com> On Friday, 6 September 2019 20:15:40 UTC+2, MRAB wrote: > On 2019-09-06 18:11, Spencer Du wrote: > > Hi > > > > I want to print yes in gui.py but it does not get printed because of the json. How do I fix this. Execute embedded.py and then gui.py to test. > > > > def on_message(client, userdata, msg): > > print("message recieved= " + msg.payload.decode()) > > # print("File which you want to import(with .py extension)") > > print("message topic=", msg.topic) > > print("message qos=", msg.qos) > > print("message retain flag=", msg.retain) > > > > if msg.payload[name] == "Hello world!": > > print("Yes!") > > > What is the value of the variable called 'name'? Or did you intend that > to be a string? > > if msg.payload["name"] == "Hello world!": > print("Yes!") "name" is part of {"name": "Hello world!"} which is a key value pair dictionary json. From Ralf_M at t-online.de Fri Sep 6 16:48:22 2019 From: Ralf_M at t-online.de (Ralf M.) Date: Fri, 6 Sep 2019 22:48:22 +0200 Subject: Multidimensional dicts Message-ID: Recently I wrote a quick and dirty script to do some counting and statistics. When I re-read it a bit later I noticed that I had been using two different ways to create two-dimensional (default-)dicts. Now I'm wondering whether one of them is "better" or more pythonic than the other. What I did: ddd_a = collections.defaultdict(set) ddd_a[(key1, key2)].add(foo) ddd_b = collections.defaultdict(lambda: collections.defaultdict(set)) ddd_b[key1][key2].add(foo) Both work as expected. Trying to think about differences I only noticed that ddd_a more easily generalises to more dimensions, and ddd_b has the benefit that ddd_b[key1] is a dict, which might help if one "row" needs to be fed to a function that expects a dict. More general ddd_a looks more symmetric (key1 and key2 are exchangeable, if done consistently) and ddd_b looks more hierarchic (like a tree traversed from root to leaves where key1, key2 etc. determine which way to go at each level). ddd_b also is more simmilar to how two-dimensional lists are done in python. Any recommendations / comments as to which to prefer? From cs at cskk.id.au Fri Sep 6 19:13:12 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sat, 7 Sep 2019 09:13:12 +1000 Subject: Multidimensional dicts In-Reply-To: References: Message-ID: <20190906231312.GA8868@cskk.homeip.net> On 06Sep2019 22:48, Ralf M. wrote: >Recently I wrote a quick and dirty script to do some counting and >statistics. When I re-read it a bit later I noticed that I had been >using two different ways to create two-dimensional (default-)dicts. >Now I'm wondering whether one of them is "better" or more pythonic >than the other. > >What I did: > >ddd_a = collections.defaultdict(set) >ddd_a[(key1, key2)].add(foo) > >ddd_b = collections.defaultdict(lambda: collections.defaultdict(set)) >ddd_b[key1][key2].add(foo) > >Both work as expected. > >Trying to think about differences I only noticed that ddd_a more >easily generalises to more dimensions, and ddd_b has the benefit that >ddd_b[key1] is a dict, which might help if one "row" needs to be fed >to a function that expects a dict. > >More general ddd_a looks more symmetric (key1 and key2 are >exchangeable, if done consistently) and ddd_b looks more hierarchic >(like a tree traversed from root to leaves where key1, key2 etc. >determine which way to go at each level). ddd_b also is more simmilar >to how two-dimensional lists are done in python. > >Any recommendations / comments as to which to prefer? As you'd imagine, it depends on what yuou're doing. If (key1,key2) are a Cartesian-like "space" of value, for example the domain of key2 values it the same regardless of key1, I lean toward (key1,key2). If (key1,key2) are a tree like structure such as the clause names and field values form a .ini config file: [clause1] field1 = 1 field2 = 3 [clause2] field2 = 9 I lean towards the ddd_b[key1][key2] approach. So: are they a "flat" space or a tree structure? The is my normal rule of thumb for deciding how to key things. In particular, if you need to ask "what are the key2 values for key1==x?" then you might want a tree structure. The ddd_a (key1,key2) approach is easier to manage in terms of creating new nodes. OTOH, using a nested defaultdict can handle that work for you: ddd_d = defaultdict(lambda: defaultdict(int)) (Pick a suitable type in place of "int" maybe.) Cheers, Cameron Simpson From none at gmail.com Sat Sep 7 05:51:36 2019 From: none at gmail.com (ast) Date: Sat, 7 Sep 2019 11:51:36 +0200 Subject: How python knows where non standard libraries are stored ? Message-ID: <5d737dac$0$20341$426a74cc@news.free.fr> Hello List sys.path contains all paths where python shall look for libraries. Eg on my system, here is the content of sys.path: >>> import sys >>> sys.path ['', 'C:\\Users\\jean-marc\\Desktop\\python', 'C:\\Program Files\\Python36-32\\python36.zip', 'C:\\Program Files\\Python36-32\\DLLs', 'C:\\Program Files\\Python36-32\\lib', 'C:\\Program Files\\Python36-32', 'C:\\Program Files\\Python36-32\\lib\\site-packages'] The last path is used as a location to store libraries you install yourself. If I am using a virtual environment (with venv) this last path is different 'C:\\Users\\jean-marc\\Desktop\\myenv\\lib\\site-packages' I looked for windows environment variables to tell python how to fill sys.path at startup but I didn't found. So how does it work ? From dieter at handshake.de Sat Sep 7 07:25:38 2019 From: dieter at handshake.de (dieter) Date: Sat, 07 Sep 2019 13:25:38 +0200 Subject: How python knows where non standard libraries are stored ? References: <5d737dac$0$20341$426a74cc@news.free.fr> Message-ID: <874l1o4abx.fsf@handshake.de> ast writes: > I looked for windows environment variables to tell python > how to fill sys.path at startup but I didn't found. > > So how does it work ? Read the (so called) docstring at the beginning of the module "site.py". Either locate the module source in the file system and read it in an editor or in an interactive Python do: import site help(site) From eryksun at gmail.com Sat Sep 7 09:31:26 2019 From: eryksun at gmail.com (Eryk Sun) Date: Sat, 7 Sep 2019 08:31:26 -0500 Subject: How python knows where non standard libraries are stored ? In-Reply-To: <5d737dac$0$20341$426a74cc@news.free.fr> References: <5d737dac$0$20341$426a74cc@news.free.fr> Message-ID: On 9/7/19, ast wrote: > > Eg on my system, here is the content of sys.path: > > >>> import sys > >>> sys.path > ['', In the REPL, "" is added for loading modules from the current directory. When executing a script, this would be the script directory. > 'C:\\Users\\jean-marc\\Desktop\\python', Probably this directory is in your %PYTHONPATH% environment variable, which gets inserted here, normally ahead of everything else except for the script directory. > 'C:\\Program Files\\Python36-32\\python36.zip', The zipped standard-library location is assumed to be beside the DLL or EXE. Next the interpreter adds the PythonPath directories from the registry. These are found in subkeys of r"[HKLM|HKCU]\Python\PythonCore\3.6-32\PythonPath". The base key has default core paths for the standard library, which normally are ignored unless the interpreter can't find its home directory. > 'C:\\Program Files\\Python36-32\\DLLs', > 'C:\\Program Files\\Python36-32\\lib', These two are derived from the default core standard-library paths, which are hard-coded in the C macro, PYTHONPATH: #define PYTHONPATH L".\\DLLs;.\\lib" At startup the interpreter searches for its home directory if PYTHONHOME isn't set. (Normally it should not be set.) If the zipped standard library exists, its directory is used as the home directory. Otherwise it checks for the landmark module "lib/os.py" in the application directory (i.e. argv0_path), and its ancestor directories down to the drive root. (If we're executing a virtual environment, the argv0_path gets set from the "home" value in its pyvenv.cfg file.) Normally the home directory is argv0_path. The home directory is used to resolve the "." components in the hard-coded PYTHONPATH string. If no home directory has been found, the interpreter uses the default core paths from the "PythonPath" registry key as discussed above. If even that isn't found, it just adds the relative paths, ".\\DLLs" and ".\\lib". > 'C:\\Program Files\\Python36-32', Windows Python has this peculiar addition. It always adds argv0_path (typically the application directory). Perhaps at some time in the past it was necessary because extension modules were located here. AFAIK, this is vestigial now, unless some embedding applications rely on it. At this point if it still hasn't found the home directory, the interpreter checks for the "lib/os.py" landmark in all of the directories that have been added to the module search path. This is a last-ditch effort to find the standard library and set sys.prefix. > 'C:\\Program Files\\Python36-32\\lib\\site-packages'] Now we're into the site module additions, including .pth files, which is pretty well documented via help(site) and the docs: https://docs.python.org/3/library/site.html The -S command-line option prevents importing the site module at startup. From kangalioo654 at gmail.com Sat Sep 7 10:40:51 2019 From: kangalioo654 at gmail.com (kangalioo654 at gmail.com) Date: Sat, 7 Sep 2019 07:40:51 -0700 (PDT) Subject: Which PyQt-compatible, performant graphing library should I use? Message-ID: <20b4c193-c849-4650-88bf-1c0d46f0c0d0@googlegroups.com> Hi, Currently I'm making a statistics tool for a game I'm playing with PyQt5. I'm not happy with my current graphing library though. In the beginning I've used matplotlib, which was way too laggy for my use case. Currently I have pyqtgraph, which is snappy, but is missing useful features. The Python graphing library selection is overwhelming, which is why I'm asking here for a recommendation. Things that I need the library to support: * PyQt5 integration * plot layout in a grid * performant navigation * scatter plots, simple and stacked bar charts Things that I don't strictly *require*, but would be really useful: * log scale support (specifically for y-axis) * tooltip support, or alternatively click callback support * plot legend * datetime axes support (like in matplotlib) * configurable colors, scatter spot sizes, bar widths, etc. Here are some screenshots how my application currently looks like with pyqtgraph: https://i.redd.it/rx423arbw5l31.png https://i.redd.it/r68twvfmw5l31.png I would be really grateful for some recommendations! From jsf80238 at gmail.com Sat Sep 7 11:12:19 2019 From: jsf80238 at gmail.com (Jason Friedman) Date: Sat, 7 Sep 2019 09:12:19 -0600 Subject: fileinput module not yielding expected results Message-ID: import csv import fileinput import sys print("Version: " + str(sys.version_info)) print("Files: " + str(sys.argv[1:])) with fileinput.input(sys.argv[1:]) as f: for line in f: print(f"File number: {fileinput.fileno()}") print(f"Is first line: {fileinput.isfirstline()}") I run this: $ python3 program.py ~/Section*.csv > ~/result I get this: $ grep "^Version" ~/result Version: sys.version_info(major=3, minor=7, micro=1, releaselevel='final', serial=0) $ grep "^Files" ~/result Files: ['/home/jason/Section01.csv', '/home/jason/Section02.csv', '/home/jason/Section03.csv', '/home/jason/Section04.csv', '/home/jason/Section05.csv', '/home/jason/Section06.csv'] $ grep -c "True" ~/result 6 That all makes sense to me, but this does not: $ grep "File number" ~/result | sort | uniq File number: 3 I expected that last grep to yield: File number: 1 File number: 2 File number: 3 File number: 4 File number: 5 File number: 6 My ultimate goal is as follows. I have multiple CSV files, each with the same header line. I want to read the header line from the first file and ignore it for subsequent files. Thank you From 2QdxY4RzWzUUiLuE at potatochowder.com Sat Sep 7 11:33:46 2019 From: 2QdxY4RzWzUUiLuE at potatochowder.com (Dan Sommers) Date: Sat, 7 Sep 2019 11:33:46 -0400 Subject: fileinput module not yielding expected results In-Reply-To: References: Message-ID: <45ffe620-0185-88fd-d133-c812af5eb311@potatochowder.com> On 9/7/19 11:12 AM, Jason Friedman wrote: > $ grep "File number" ~/result | sort | uniq > File number: 3 > > I expected that last grep to yield: > File number: 1 > File number: 2 > File number: 3 > File number: 4 > File number: 5 > File number: 6 As per https://docs.python.org/3/library/fileinput.html#fileinput.fileno, fileno is the underlying file descriptor of the file, and not at all what you're looking for. > My ultimate goal is as follows. I have multiple CSV files, each with the > same header line. I want to read the header line from the first file and > ignore it for subsequent files. If you're certain that the headers are the same in each file, then there's no harm and much simplicity in reading them each time they come up. with fileinput ...: for line in f: if fileinput.isfirstline(): headers = extract_headers(line) else: pass # process a non-header line here Yes, the program will take slightly longer to run. No, you won't notice it. From barry at barrys-emacs.org Sat Sep 7 12:17:54 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Sat, 7 Sep 2019 17:17:54 +0100 Subject: fileinput module not yielding expected results In-Reply-To: <45ffe620-0185-88fd-d133-c812af5eb311@potatochowder.com> References: <45ffe620-0185-88fd-d133-c812af5eb311@potatochowder.com> Message-ID: <545FD8AF-4D65-400D-A565-FCC8D7CFC63F@barrys-emacs.org> > On 7 Sep 2019, at 16:33, Dan Sommers <2QdxY4RzWzUUiLuE at potatochowder.com> wrote: > > with fileinput ...: > for line in f: > if fileinput.isfirstline(): > headers = extract_headers(line) > else: > pass # process a non-header line here If you always know you can skip the first line I use this pattern with fileinput ...: next(f) # skip header for line in f: # process a non-header line here Barry From jsf80238 at gmail.com Sat Sep 7 12:44:30 2019 From: jsf80238 at gmail.com (Jason Friedman) Date: Sat, 7 Sep 2019 10:44:30 -0600 Subject: fileinput module not yielding expected results In-Reply-To: <45ffe620-0185-88fd-d133-c812af5eb311@potatochowder.com> References: <45ffe620-0185-88fd-d133-c812af5eb311@potatochowder.com> Message-ID: > > If you're certain that the headers are the same in each file, > then there's no harm and much simplicity in reading them each > time they come up. > > with fileinput ...: > for line in f: > if fileinput.isfirstline(): > headers = extract_headers(line) > else: > pass # process a non-header line here > > Yes, the program will take slightly longer to run. No, you won't > notice it. > > Ah, thank you Dan. I followed your advice ... the working code: with fileinput.input(sys.argv[1:]) as f: reader = csv.DictReader(f) for row in reader: if fileinput.isfirstline(): continue for key, value in row.items(): pass #processing I was hung up on that continue on firstline, because I thought that would skip the header of the first file. I think now the csv.DictReader(f) command consumes it first. From tjreedy at udel.edu Sat Sep 7 15:31:07 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 7 Sep 2019 15:31:07 -0400 Subject: 2to3, str, and basestring Message-ID: 2to3 converts syntactically valid 2.x code to syntactically valid 3.x code. It cannot, however, guarantee semantic correctness. A particular problem is that str is semantically ambiguous in 2.x, as it is used both for text encoded as bytes and binary data. To resolve the ambiguity for conversions to 3.x, 2.6 introduced 'bytes' as a synonym for 'str'. The intention is that one use 'bytes' to create or refer to 2.x bytes that should remain bytes in 3.x and use 'str' to create or refer to 2.x text bytes that should become or will be unicode in 3.x. 3.x and hence 2to3 *assume* that one is using 'bytes' and 'str' this way, so that 'unicode' becomes an unneeded synonym for 'str' and 2to3 changes 'unicode' to 'str'. If one does not use 'str' and 'bytes' as intended, 2to3 may produce semantically different code. 2.3 introduced abstract superclass 'basestring', which can be viewed as Union(unicode, str). "isinstance(value, basestring)" is defined as "isinstance(value, (unicode, str))" I believe the intended meaning was 'text, whether unicode or encoded bytes'. Certainly, any code following if isinstance(value, basestring): would likely only make sense if that were true. In any case, after 2.6, one should only use 'basestring' when the 'str' part has its restricted meaning of 'unicode in 3.x'. "(unicode, bytes)" is semantically different from "basestring" and "(unicode, str)" when used in isinstance. 2to3 converts then to "(std, bytes)", 'str', and '(str, str)' (the same as 'str' when used in isinstance). If one uses 'basestring' when one means '(unicode, bytes)', 2to3 may produce semantically different code. Example based on https://bugs.python.org/issue38003: if isinstance(value, basestring): if not isinstance(value, unicode): value = value.decode(encoding) process_text(value) else: process_nontext(value) 2to3 produces if isinstance(value, str): if not isinstance(value, str): value = value.decode(encoding) process_text(value) else: process_nontext(value) If, in 3.x, value is always unicode, then the inner conditional is dead and can be removed. But if, in 3.x, value might be byte-encoded text, it will not be decoded and the code is wrong. Fixes: 1. Instead of decoding value after the check, do it before the check. I think this is best for new code. if isinstance(value, bytes): value = value.decode(encoding) ... if isinstance(value, unicode): process_text(value) else: process_nontext(value) 2. Replace 'basestring' with '(unicode, bytes)'. This is easier with existing code. if isinstance(value, basestring): if not isinstance(value, unicode): value = value.decode(encoding) process_text(value) else: process_nontext(value) (I believe but have not tested that) 2to3 produces correct 3.x code from either 1 or 2 after replacing 'unicode' with 'str'. In both cases, the 'unicode' to 'str' replacement should result in correct 3.x code. 3. Edit Lib/lib2to3/fixes/fix_basestring.py to replace 'basestring' with '(str, bytes)' instead of 'str'. This should be straightforward if one understands the ast format. Note that 2to3 is not meant for 2&3 code using exception tricks and six/future imports. Turning 2&3 code into idiomatic 3-only code is a separate subject. -- Terry Jan Reedy From tjreedy at udel.edu Sat Sep 7 15:44:57 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 7 Sep 2019 15:44:57 -0400 Subject: How python knows where non standard libraries are stored ? In-Reply-To: <5d737dac$0$20341$426a74cc@news.free.fr> References: <5d737dac$0$20341$426a74cc@news.free.fr> Message-ID: On 9/7/2019 5:51 AM, ast wrote: > 'C:\\Program Files\\Python36-32\\lib\\site-packages'] > > The last path is used as a location to store libraries > you install yourself. > > If I am using a virtual environment (with venv) this last > path is different > > 'C:\\Users\\jean-marc\\Desktop\\myenv\\lib\\site-packages' > > I looked for windows environment variables to tell python > how to fill sys.path at startup but I didn't found. > > So how does it work ? I believe that the short answer, skipping the gory details provided by Eryk, is that the result is the same as os.path.dirname(sys.executable) + r"\lib\site-packages" You can check if this works for the venv. -- Terry Jan Reedy From tjreedy at udel.edu Sat Sep 7 20:12:35 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 7 Sep 2019 20:12:35 -0400 Subject: 3 cubes that sum to 42 Message-ID: >>> (-80538738812075974)**3 + 80435758145817515**3 + 12602123297335631**3 == 42 True # Impressively quickly, in a blink of an eye. This is the last number < 100, not theoretically excluded, to be solved. Compute power provided by CharityEngine. For more, see Numberphile... https://www.youtube.com/watch?v=zyG8Vlw5aAw -- Terry Jan Reedy From sharan.basappa at gmail.com Sat Sep 7 20:19:36 2019 From: sharan.basappa at gmail.com (Sharan Basappa) Date: Sat, 7 Sep 2019 17:19:36 -0700 (PDT) Subject: issue in handling CSV data Message-ID: <64e71dbd-7a55-4230-84d0-19d8aa8944c4@googlegroups.com> I am trying to read a log file that is in CSV format. The code snippet is below: ############################### import matplotlib.pyplot as plt import seaborn as sns; sns.set() import numpy as np import pandas as pd import os import csv from numpy import genfromtxt # read the CSV and get into X array os.chdir(r'D:\Users\sharanb\OneDrive - HCL Technologies Ltd\Projects\MyBackup\Projects\Initiatives\machine learning\programs\constraints') X = [] #with open("constraints.csv", 'rb') as csvfile: # reader = csv.reader(csvfile) # data_as_list = list(reader) #myarray = np.asarray(data_as_list) my_data = genfromtxt('constraints.csv', delimiter = ',', dtype=None) print (my_data) my_data_1 = np.delete(my_data, 0, axis=1) print (my_data_1) my_data_2 = np.delete(my_data_1, 0, axis=1) print (my_data_2) my_data_3 = my_data_2.astype(np.float) ################################ Here is how print (my_data_2) looks like: ############################## [['"\t"81' '"\t5c'] ['"\t"04' '"\t11'] ['"\t"e1' '"\t17'] ['"\t"6a' '"\t6c'] ['"\t"53' '"\t69'] ['"\t"98' '"\t87'] ['"\t"5c' '"\t4b'] ############################## Finally, I am trying to get rid of the strings and get array of numbers using Numpy's astype function. At this stage, I get an error. This is the error: my_data_3 = my_data_2.astype(np.float) could not convert string to float: " "81 As you can see, the string "\t"81 is causing the error. It seems to be due to char "\t". I don't know how to resolve this. Thanks for your help. From joel.goldstick at gmail.com Sat Sep 7 20:28:11 2019 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sat, 7 Sep 2019 20:28:11 -0400 Subject: issue in handling CSV data In-Reply-To: <64e71dbd-7a55-4230-84d0-19d8aa8944c4@googlegroups.com> References: <64e71dbd-7a55-4230-84d0-19d8aa8944c4@googlegroups.com> Message-ID: On Sat, Sep 7, 2019 at 8:21 PM Sharan Basappa wrote: > > I am trying to read a log file that is in CSV format. > > The code snippet is below: > > ############################### > import matplotlib.pyplot as plt > import seaborn as sns; sns.set() > import numpy as np > import pandas as pd > import os > import csv > from numpy import genfromtxt > > # read the CSV and get into X array > os.chdir(r'D:\Users\sharanb\OneDrive - HCL Technologies Ltd\Projects\MyBackup\Projects\Initiatives\machine learning\programs\constraints') > X = [] > #with open("constraints.csv", 'rb') as csvfile: > # reader = csv.reader(csvfile) > # data_as_list = list(reader) > #myarray = np.asarray(data_as_list) > > my_data = genfromtxt('constraints.csv', delimiter = ',', dtype=None) > print (my_data) > > my_data_1 = np.delete(my_data, 0, axis=1) > print (my_data_1) > > my_data_2 = np.delete(my_data_1, 0, axis=1) > print (my_data_2) > > my_data_3 = my_data_2.astype(np.float) > ################################ > > Here is how print (my_data_2) looks like: > ############################## > [['"\t"81' '"\t5c'] > ['"\t"04' '"\t11'] > ['"\t"e1' '"\t17'] > ['"\t"6a' '"\t6c'] > ['"\t"53' '"\t69'] > ['"\t"98' '"\t87'] > ['"\t"5c' '"\t4b'] > ############################## > > Finally, I am trying to get rid of the strings and get array of numbers using Numpy's astype function. At this stage, I get an error. > > This is the error: > my_data_3 = my_data_2.astype(np.float) > could not convert string to float: " "81 > > As you can see, the string "\t"81 is causing the error. > It seems to be due to char "\t". > > I don't know how to resolve this. > > Thanks for your help. > > -- > https://mail.python.org/mailman/listinfo/python-list how about (strip(my_data_2).astype(np.float)) I haven't used numpy, but if your theory is correct, this will clean up the string -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From joel.goldstick at gmail.com Sat Sep 7 20:30:18 2019 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sat, 7 Sep 2019 20:30:18 -0400 Subject: issue in handling CSV data In-Reply-To: References: <64e71dbd-7a55-4230-84d0-19d8aa8944c4@googlegroups.com> Message-ID: On Sat, Sep 7, 2019 at 8:28 PM Joel Goldstick wrote: > > On Sat, Sep 7, 2019 at 8:21 PM Sharan Basappa wrote: > > > > I am trying to read a log file that is in CSV format. > > > > The code snippet is below: > > > > ############################### > > import matplotlib.pyplot as plt > > import seaborn as sns; sns.set() > > import numpy as np > > import pandas as pd > > import os > > import csv > > from numpy import genfromtxt > > > > # read the CSV and get into X array > > os.chdir(r'D:\Users\sharanb\OneDrive - HCL Technologies Ltd\Projects\MyBackup\Projects\Initiatives\machine learning\programs\constraints') > > X = [] > > #with open("constraints.csv", 'rb') as csvfile: > > # reader = csv.reader(csvfile) > > # data_as_list = list(reader) > > #myarray = np.asarray(data_as_list) > > > > my_data = genfromtxt('constraints.csv', delimiter = ',', dtype=None) > > print (my_data) > > > > my_data_1 = np.delete(my_data, 0, axis=1) > > print (my_data_1) > > > > my_data_2 = np.delete(my_data_1, 0, axis=1) > > print (my_data_2) > > > > my_data_3 = my_data_2.astype(np.float) > > ################################ > > > > Here is how print (my_data_2) looks like: > > ############################## > > [['"\t"81' '"\t5c'] > > ['"\t"04' '"\t11'] > > ['"\t"e1' '"\t17'] > > ['"\t"6a' '"\t6c'] > > ['"\t"53' '"\t69'] > > ['"\t"98' '"\t87'] > > ['"\t"5c' '"\t4b'] > > ############################## > > > > Finally, I am trying to get rid of the strings and get array of numbers using Numpy's astype function. At this stage, I get an error. > > > > This is the error: > > my_data_3 = my_data_2.astype(np.float) > > could not convert string to float: " "81 > > > > As you can see, the string "\t"81 is causing the error. > > It seems to be due to char "\t". > > > > I don't know how to resolve this. > > > > Thanks for your help. > > > > -- > > https://mail.python.org/mailman/listinfo/python-list > > how about (strip(my_data_2).astype(np.float)) > > I haven't used numpy, but if your theory is correct, this will clean > up the string > oops, I think I was careless at looking at your data. so this doesn't seem like such a good idea > -- > Joel Goldstick > http://joelgoldstick.com/blog > http://cc-baseballstats.info/stats/birthdays -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From python at mrabarnett.plus.com Sat Sep 7 21:14:47 2019 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 8 Sep 2019 02:14:47 +0100 Subject: issue in handling CSV data In-Reply-To: <64e71dbd-7a55-4230-84d0-19d8aa8944c4@googlegroups.com> References: <64e71dbd-7a55-4230-84d0-19d8aa8944c4@googlegroups.com> Message-ID: <1e674055-5187-8b87-d95d-801c9597113c@mrabarnett.plus.com> On 2019-09-08 01:19, Sharan Basappa wrote: > I am trying to read a log file that is in CSV format. > > The code snippet is below: > > ############################### > import matplotlib.pyplot as plt > import seaborn as sns; sns.set() > import numpy as np > import pandas as pd > import os > import csv > from numpy import genfromtxt > > # read the CSV and get into X array > os.chdir(r'D:\Users\sharanb\OneDrive - HCL Technologies Ltd\Projects\MyBackup\Projects\Initiatives\machine learning\programs\constraints') > X = [] > #with open("constraints.csv", 'rb') as csvfile: > # reader = csv.reader(csvfile) > # data_as_list = list(reader) > #myarray = np.asarray(data_as_list) > > my_data = genfromtxt('constraints.csv', delimiter = ',', dtype=None) > print (my_data) > > my_data_1 = np.delete(my_data, 0, axis=1) > print (my_data_1) > > my_data_2 = np.delete(my_data_1, 0, axis=1) > print (my_data_2) > > my_data_3 = my_data_2.astype(np.float) > ################################ > > Here is how print (my_data_2) looks like: > ############################## > [['"\t"81' '"\t5c'] > ['"\t"04' '"\t11'] > ['"\t"e1' '"\t17'] > ['"\t"6a' '"\t6c'] > ['"\t"53' '"\t69'] > ['"\t"98' '"\t87'] > ['"\t"5c' '"\t4b'] > ############################## > > Finally, I am trying to get rid of the strings and get array of numbers using Numpy's astype function. At this stage, I get an error. > > This is the error: > my_data_3 = my_data_2.astype(np.float) > could not convert string to float: " "81 > > As you can see, the string "\t"81 is causing the error. > It seems to be due to char "\t". > > I don't know how to resolve this. > > Thanks for your help. > Are you sure it's CSV (Comma-Separated Value) and not TSV (Tab-Separated Value)? Also the values look like hexadecimal to me. I think that .astype(np.float) assumes that the values are decimal. I'd probably start by reading them using the csv module, convert the values to decimal, and then pass them on to numpy. From jfong at ms4.hinet.net Sat Sep 7 21:44:53 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Sat, 7 Sep 2019 18:44:53 -0700 (PDT) Subject: Is it 'fine' to instantiate a widget without parent parameter? Message-ID: <20b356eb-1343-4fe8-97be-5c1bbc45e18d@googlegroups.com> I know it is valid, according to the Tkinter source, every widget constructor has a 'master=None' default. What happens on doing this? In what circumstance, we do it this way? and will it cause any trouble? --Jach From sharan.basappa at gmail.com Sat Sep 7 22:12:05 2019 From: sharan.basappa at gmail.com (Sharan Basappa) Date: Sat, 7 Sep 2019 19:12:05 -0700 (PDT) Subject: issue in handling CSV data In-Reply-To: References: <64e71dbd-7a55-4230-84d0-19d8aa8944c4@googlegroups.com> <1e674055-5187-8b87-d95d-801c9597113c@mrabarnett.plus.com> Message-ID: On Saturday, 7 September 2019 21:18:11 UTC-4, MRAB wrote: > On 2019-09-08 01:19, Sharan Basappa wrote: > > I am trying to read a log file that is in CSV format. > > > > The code snippet is below: > > > > ############################### > > import matplotlib.pyplot as plt > > import seaborn as sns; sns.set() > > import numpy as np > > import pandas as pd > > import os > > import csv > > from numpy import genfromtxt > > > > # read the CSV and get into X array > > os.chdir(r'D:\Users\sharanb\OneDrive - HCL Technologies Ltd\Projects\MyBackup\Projects\Initiatives\machine learning\programs\constraints') > > X = [] > > #with open("constraints.csv", 'rb') as csvfile: > > # reader = csv.reader(csvfile) > > # data_as_list = list(reader) > > #myarray = np.asarray(data_as_list) > > > > my_data = genfromtxt('constraints.csv', delimiter = ',', dtype=None) > > print (my_data) > > > > my_data_1 = np.delete(my_data, 0, axis=1) > > print (my_data_1) > > > > my_data_2 = np.delete(my_data_1, 0, axis=1) > > print (my_data_2) > > > > my_data_3 = my_data_2.astype(np.float) > > ################################ > > > > Here is how print (my_data_2) looks like: > > ############################## > > [['"\t"81' '"\t5c'] > > ['"\t"04' '"\t11'] > > ['"\t"e1' '"\t17'] > > ['"\t"6a' '"\t6c'] > > ['"\t"53' '"\t69'] > > ['"\t"98' '"\t87'] > > ['"\t"5c' '"\t4b'] > > ############################## > > > > Finally, I am trying to get rid of the strings and get array of numbers using Numpy's astype function. At this stage, I get an error. > > > > This is the error: > > my_data_3 = my_data_2.astype(np.float) > > could not convert string to float: " "81 > > > > As you can see, the string "\t"81 is causing the error. > > It seems to be due to char "\t". > > > > I don't know how to resolve this. > > > > Thanks for your help. > > > Are you sure it's CSV (Comma-Separated Value) and not TSV (Tab-Separated > Value)? > > Also the values look like hexadecimal to me. I think that > .astype(np.float) assumes that the values are decimal. > > I'd probably start by reading them using the csv module, convert the > values to decimal, and then pass them on to numpy. yes. it is CSV. The commas are gone once csv.reader processed the csv file. The tabs seem to be there also which seem to be causing the issue. Thanks for your response From a at d-amo.re Sun Sep 8 04:35:24 2019 From: a at d-amo.re (Andrea D'Amore) Date: Sun, 8 Sep 2019 10:35:24 +0200 Subject: issue in handling CSV data In-Reply-To: <64e71dbd-7a55-4230-84d0-19d8aa8944c4@googlegroups.com> References: <64e71dbd-7a55-4230-84d0-19d8aa8944c4@googlegroups.com> Message-ID: On Sun, 8 Sep 2019 at 02:19, Sharan Basappa wrote: This is the error: > my_data_3 = my_data_2.astype(np.float) > could not convert string to float: " "81 > As you can see, the string "\t"81 is causing the error. > It seems to be due to char "\t". It is not clear what format do you expect to be in the file. You say "it is CSV" so your actual payload seems to be a pair of three bytes (a tab and two hex digits in ASCII) per line. Can you paste a hexdump of the first three lines of the input file and say what you expect to get once the data has been processed? -- Andrea From tjreedy at udel.edu Sun Sep 8 05:31:10 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 8 Sep 2019 05:31:10 -0400 Subject: Is it 'fine' to instantiate a widget without parent parameter? In-Reply-To: <20b356eb-1343-4fe8-97be-5c1bbc45e18d@googlegroups.com> References: <20b356eb-1343-4fe8-97be-5c1bbc45e18d@googlegroups.com> Message-ID: On 9/7/2019 9:44 PM, jfong at ms4.hinet.net wrote: > I know it is valid, according to the Tkinter source, every widget constructor has a 'master=None' default. What happens on doing this? Tkinter creates a default Tk object and uses that as the master. >>> t = tkinter.Text() >>> t.master > In what circumstance, we do it this way? and will it cause any trouble? I believe it is OK if you always do it that way within a single application. But I prefer to have an explicit reference and use that for .after calls and some others. -- Terry Jan Reedy From pankaj.jangid at gmail.com Sun Sep 8 05:54:27 2019 From: pankaj.jangid at gmail.com (Pankaj Jangid) Date: Sun, 08 Sep 2019 15:24:27 +0530 Subject: local variable 'p' referenced before assignment Message-ID: Why this code is giving local variable access error when I am accessing a global variable? p = 0 def visit(): m = 1 if m > p: p = m visit() print(p) If I change the variable assignment inside the function to q = m then it works fine. Like this p = 0 def visit(): m = 1 if m > p: q = m # Changed 'p' to 'q' visit() print(p) -- Pankaj Jangid From bouncingcats at gmail.com Sun Sep 8 06:23:51 2019 From: bouncingcats at gmail.com (David) Date: Sun, 8 Sep 2019 20:23:51 +1000 Subject: local variable 'p' referenced before assignment In-Reply-To: References: Message-ID: On Sun, 8 Sep 2019 at 19:55, Pankaj Jangid wrote: > > Why this code is giving local variable access error when I am accessing > a global variable? > p = 0 > def visit(): > m = 1 > if m > p: > p = m https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python " If a variable is assigned a value anywhere within the function?s body, it?s assumed to be a local unless explicitly declared as global." So to avoid causing this error, inside your function you must explicitly declare p as global before you try to assign to it. Inside your function, you need a statement: global p before you try to assign to p. From jfong at ms4.hinet.net Sun Sep 8 06:20:26 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Sun, 8 Sep 2019 03:20:26 -0700 (PDT) Subject: Is it 'fine' to instantiate a widget without parent parameter? In-Reply-To: References: <20b356eb-1343-4fe8-97be-5c1bbc45e18d@googlegroups.com> Message-ID: <712ccffb-8e13-4fba-9f0e-76ff4cd1bd63@googlegroups.com> Terry Reedy? 2019?9?8???? UTC+8??5?31?34???? > On 9/7/2019 9:44 PM, jfong at ms4.hinet.net wrote: > > I know it is valid, according to the Tkinter source, every widget constructor has a 'master=None' default. What happens on doing this? > > Tkinter creates a default Tk object and uses that as the master. > > >>> t = tkinter.Text() > >>> t.master > > > > In what circumstance, we do it this way? and will it cause any trouble? > > I believe it is OK if you always do it that way within a single > application. But I prefer to have an explicit reference and use that > for .after calls and some others. > > > -- > Terry Jan Reedy If I have two widgets created this way: t0 = tkinter.Text() t1 = tkinter.Text() How many Tk objects will there be? --Jach From bouncingcats at gmail.com Sun Sep 8 06:44:28 2019 From: bouncingcats at gmail.com (David) Date: Sun, 8 Sep 2019 20:44:28 +1000 Subject: Is it 'fine' to instantiate a widget without parent parameter? In-Reply-To: <712ccffb-8e13-4fba-9f0e-76ff4cd1bd63@googlegroups.com> References: <20b356eb-1343-4fe8-97be-5c1bbc45e18d@googlegroups.com> <712ccffb-8e13-4fba-9f0e-76ff4cd1bd63@googlegroups.com> Message-ID: On Sun, 8 Sep 2019 at 20:25, wrote: > > If I have two widgets created this way: > t0 = tkinter.Text() > t1 = tkinter.Text() > How many Tk objects will there be? $ python3 Python 3.5.3 (default, Sep 27 2018, 17:25:39) [GCC 6.3.0 20170516] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import tkinter >>> t0 = tkinter.Text() >>> t1 = tkinter.Text() >>> t0 is t1 False >>> t0 >>> t1 >>> From jfong at ms4.hinet.net Sun Sep 8 07:01:04 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Sun, 8 Sep 2019 04:01:04 -0700 (PDT) Subject: Is it 'fine' to instantiate a widget without parent parameter? In-Reply-To: References: <20b356eb-1343-4fe8-97be-5c1bbc45e18d@googlegroups.com> <712ccffb-8e13-4fba-9f0e-76ff4cd1bd63@googlegroups.com> Message-ID: <8ffda1b2-021b-468e-a568-5a9c813c36bc@googlegroups.com> David? 2019?9?8???? UTC+8??6?44?55???? > On Sun, 8 Sep 2019 at 20:25, wrote: > > > > If I have two widgets created this way: > > t0 = tkinter.Text() > > t1 = tkinter.Text() > > How many Tk objects will there be? > > $ python3 > Python 3.5.3 (default, Sep 27 2018, 17:25:39) > [GCC 6.3.0 20170516] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> import tkinter > >>> t0 = tkinter.Text() > >>> t1 = tkinter.Text() > >>> t0 is t1 > False > >>> t0 > > >>> t1 > > >>> Sorry, didn't make it clear. I mean --Jach From bouncingcats at gmail.com Sun Sep 8 08:13:36 2019 From: bouncingcats at gmail.com (David) Date: Sun, 8 Sep 2019 22:13:36 +1000 Subject: Is it 'fine' to instantiate a widget without parent parameter? In-Reply-To: <8ffda1b2-021b-468e-a568-5a9c813c36bc@googlegroups.com> References: <20b356eb-1343-4fe8-97be-5c1bbc45e18d@googlegroups.com> <712ccffb-8e13-4fba-9f0e-76ff4cd1bd63@googlegroups.com> <8ffda1b2-021b-468e-a568-5a9c813c36bc@googlegroups.com> Message-ID: On Sun, 8 Sep 2019 at 21:05, wrote: > David? 2019?9?8???? UTC+8??6?44?55???? > > On Sun, 8 Sep 2019 at 20:25, wrote: > > > If I have two widgets created this way: > > > t0 = tkinter.Text() > > > t1 = tkinter.Text() > > > How many Tk objects will there be? > Sorry, didn't make it clear. I mean Sorry I didn't read more carefully. But I think that the method I demonstrated can give the answer to your question. >>> import tkinter >>> t0 = tkinter.Text() >>> t1 = tkinter.Text() >>> t0.master >>> t1.master >>> t0.master is t1.master True >>> From sharan.basappa at gmail.com Sun Sep 8 08:41:07 2019 From: sharan.basappa at gmail.com (Sharan Basappa) Date: Sun, 8 Sep 2019 05:41:07 -0700 (PDT) Subject: issue in handling CSV data In-Reply-To: References: <64e71dbd-7a55-4230-84d0-19d8aa8944c4@googlegroups.com> Message-ID: <976337c0-ce9f-49b6-974e-062079cc0483@googlegroups.com> On Sunday, 8 September 2019 04:56:29 UTC-4, Andrea D'Amore wrote: > On Sun, 8 Sep 2019 at 02:19, Sharan Basappa wrote: > This is the error: > > my_data_3 = my_data_2.astype(np.float) > > could not convert string to float: " "81 > > > As you can see, the string "\t"81 is causing the error. > > It seems to be due to char "\t". > > It is not clear what format do you expect to be in the file. > You say "it is CSV" so your actual payload seems to be a pair of three > bytes (a tab and two hex digits in ASCII) per line. > > Can you paste a hexdump of the first three lines of the input file and > say what you expect to get once the data has been processed? Andrea, The issue seems to be presence of tabs along with the numbers in a single string. So, when I try to convert strings to numbers, it fails due to presence of tabs. Here is the hex dump: 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 38 31 2c 22 09 35 63 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 30 34 2c 22 09 31 31 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 65 31 2c 22 09 31 37 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 36 61 2c 22 09 36 63 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 35 33 2c 22 09 36 39 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 39 38 2c 22 09 38 37 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 35 63 2c 22 09 34 62 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 32 38 2c 22 09 33 36 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 36 33 2c 22 09 35 30 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 32 34 2c 22 09 32 31 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 64 66 2c 22 09 39 61 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 68 2c 22 09 22 61 62 2c 22 09 62 39 0d 0a 22 61 64 64 72 65 73 73 2c 22 09 22 From michael.stemper at gmail.com Sun Sep 8 09:08:09 2019 From: michael.stemper at gmail.com (Michael F. Stemper) Date: Sun, 8 Sep 2019 08:08:09 -0500 Subject: 3 cubes that sum to 42 In-Reply-To: References: Message-ID: On 07/09/2019 19.12, Terry Reedy wrote: >>>> (-80538738812075974)**3 + 80435758145817515**3 + > 12602123297335631**3 == 42 > True? # Impressively quickly, in a blink of an eye. Yeah. When I saw the video, I tried it as well. Python's arbitrary-sized integer arithmetic is truly amazing! In fact, I ended up writing a tiny program to cube its arguments and report the sum. Took about 40 ms to run, no matter the size of the arguments, which tells me that the only cost is the fixed overhead. -- Michael F. Stemper This sentence no verb. From uri at speedy.net Sun Sep 8 09:26:10 2019 From: uri at speedy.net (=?UTF-8?B?15DXldeo15k=?=) Date: Sun, 8 Sep 2019 16:26:10 +0300 Subject: 3 cubes that sum to 42 In-Reply-To: References: Message-ID: I tried to add one: >>> (-80538738812075975)**3 + 80435758145817515**3 + 12602123297335631**3 -19459465348319378856503251080373909 ???? uri at speedy.net On Sun, Sep 8, 2019 at 3:14 AM Terry Reedy wrote: > >>> (-80538738812075974)**3 + 80435758145817515**3 + > 12602123297335631**3 == 42 > True # Impressively quickly, in a blink of an eye. > > This is the last number < 100, not theoretically excluded, to be solved. > Compute power provided by CharityEngine. For more, see Numberphile... > https://www.youtube.com/watch?v=zyG8Vlw5aAw > > -- > Terry Jan Reedy > > -- > https://mail.python.org/mailman/listinfo/python-list > From sharan.basappa at gmail.com Sun Sep 8 10:57:41 2019 From: sharan.basappa at gmail.com (Sharan Basappa) Date: Sun, 8 Sep 2019 07:57:41 -0700 (PDT) Subject: numpy array - convert hex to int Message-ID: <0b3c5c58-bdb6-4f36-b91c-5e3a667a7833@googlegroups.com> I have a numpy array that has data in the form of hex. I would like to convert that into decimal/integer. Need suggestions please. From luciano at ramalho.org Sun Sep 8 11:16:26 2019 From: luciano at ramalho.org (Luciano Ramalho) Date: Sun, 8 Sep 2019 12:16:26 -0300 Subject: numpy array - convert hex to int In-Reply-To: <0b3c5c58-bdb6-4f36-b91c-5e3a667a7833@googlegroups.com> References: <0b3c5c58-bdb6-4f36-b91c-5e3a667a7833@googlegroups.com> Message-ID: >>> int('C0FFEE', 16) 12648430 There you go! On Sun, Sep 8, 2019 at 12:02 PM Sharan Basappa wrote: > > I have a numpy array that has data in the form of hex. > I would like to convert that into decimal/integer. > Need suggestions please. > -- > https://mail.python.org/mailman/listinfo/python-list -- Luciano Ramalho | Author of Fluent Python (O'Reilly, 2015) | http://shop.oreilly.com/product/0636920032519.do | Technical Principal at ThoughtWorks | Twitter: @ramalhoorg From aishan0403 at gmail.com Sun Sep 8 12:02:55 2019 From: aishan0403 at gmail.com (A S) Date: Sun, 8 Sep 2019 09:02:55 -0700 (PDT) Subject: Finding lines in .txt file that contain keywords from two different set() Message-ID: <3598f0bb-5854-4d16-ab9c-56d1b0784970@googlegroups.com> My problem is seemingly profound but I hope to make it sound as simplified as possible.....Let me unpack the details..: 1. I have one folder of Excel (.xlsx) files that serve as a data dictionary. -In Cell A1, the data source name is written in between brackets -In Cols C:D, it contains the data field names (It could be in either col C or D in my actual Excel sheet. So I had to search both columns -*Important: I need to know which data source the field names come from 2. I have another folder of Text (.txt) files that I need to parse through to find these keywords. These are the folders used for a better reference ( https://drive.google.com/open?id=1_LcceqcDhHnWW3Nrnwf5RkXPcnDfesq ). The files are found in the folder. This is the code I have thus far...: import os, sys from os.path import join import re import xlrd from xlrd import open_workbook import openpyxl from openpyxl.reader.excel import load_workbook import xlsxwriter #All the paths dict_folder = 'C:/Users/xxxx/Documents/xxxx/Test Excel' text_folder = 'C:/Users/xxxx/Documents/xxxx/Text' words = set() fieldset = set() for file in os.listdir(dict_folder): if file.endswith(".xlsx"): wb1 = load_workbook(join(dict_folder, file), data_only = True) ws = wb1.active #Here I am reading and printing all the data source names set(words) in the excel dictionaries: cellvalues = ws["A1"].value wordsextract = re.findall(r"\((.+?)\)", str(cellvalues)) results = wordsextract[0] words.add(results) print(results) for rowofcellobj in ws["C" : "D"]: for cellobj in rowofcellobj: #2. Here I am printing all the field names in col C & D in the excel dictionaries: data = re.findall(r"\w+_.*?\w+", str(cellobj.value)) if data != []: fields = data[0] fieldset.add(fields) print(fieldset) #listing = str.remove("") #print(listing) #Here I am reading the name of each .txt file to the separate .xlsx file: for r, name in enumerate(os.listdir(text_folder)): if name.endswith(".txt"): print(name) #Reading .txt file and trying to make the sentence into words instead of lines so that I can compare the individual .txt file words with the .xlsx file txtfilespath = os.chdir("C:/Users/xxxx/Documents/xxxx/Text") #Here I am reading and printing all the words in the .txt files and compare with the excel Cell A1: for name in os.listdir(txtfilespath): if name.endswith(".txt"): with open (name, "r") as texts: # Read each line of the file: s = texts.read() print(s) #if .txt files contain.....() or select or from or words from sets..search that sentence and extract the common fields result1 = [] parens = 0 buff = "" for line in s: if line == "(": parens += 1 if parens > 0: buff += line if line == ")": parens -= 1 if not parens and buff: result1.append(buff) buff = "" set(result1) #Here, I include other keywords other than those found in the Excel workbooks checkhere = set() checkhere.add("Select") checkhere.add("From") checkhere.add("select") checkhere.add("from") checkhere.add("SELECT") checkhere.add("FROM") # k = list(checkhere) # print(k) #I only want to read/ extract the lines containing brackets () as well as the keywords in the checkhere set. So that I can check capture the source and field in each line: #I tried this but nothing was printed...... for element in checkhere: if element in result1: print(result1) My desired output for the code that could not be printed when I tried is: (/* 1.select_no., biiiiiyyyy FROM apple_x_Ex_x */ proc sql; "TRUuuuth") (/* 1.xxxxx FROM xxxxx*/ proc sql; "TRUuuuth") (SELECT abc AS abc1, ab33_2_ AS mon, a_rr, iirir_vf, jk_ff, sfa_jfkj FROM &orange..xxx_xxx_xxE where (asre(kkk_ix as format 'xxxx-xx') gff &bcbcb_hhaha.) and (axx(xx_ix as format 'xxxx-xx') lec &jgjsd_vnv.) ) (/* 1.select_no. FROM apple_x_Ex_x */ proc sql; "TRUuuuth") (SELECT abc AS kfcccc, mcfg_2_ AS dokn, b_rr, jjhj_vf, jjjk_hj, fjjh_jhjkj FROM &bfbd..pear_xxx_xxE where (afdfe(kkffk_ix as format 'xxxxd-xx') gdaff &bcdadabcb_hdahaha.) and (axx(xx_ix as format 'xxxx-xx') lec &jgjsdfdf_vnv.) ) After which, if I'm able to get the desired output above, I will then compare these lines against the word set() and the fieldset set(). Any help would really be appreciated here..thank you From hjp-python at hjp.at Sun Sep 8 12:39:47 2019 From: hjp-python at hjp.at (Peter J. Holzer) Date: Sun, 8 Sep 2019 18:39:47 +0200 Subject: issue in handling CSV data In-Reply-To: <976337c0-ce9f-49b6-974e-062079cc0483@googlegroups.com> References: <64e71dbd-7a55-4230-84d0-19d8aa8944c4@googlegroups.com> <976337c0-ce9f-49b6-974e-062079cc0483@googlegroups.com> Message-ID: <20190908163947.GA22749@hjp.at> On 2019-09-08 05:41:07 -0700, Sharan Basappa wrote: > On Sunday, 8 September 2019 04:56:29 UTC-4, Andrea D'Amore wrote: > > On Sun, 8 Sep 2019 at 02:19, Sharan Basappa wrote: > > > As you can see, the string "\t"81 is causing the error. > > > It seems to be due to char "\t". > > > > It is not clear what format do you expect to be in the file. > > You say "it is CSV" so your actual payload seems to be a pair of three > > bytes (a tab and two hex digits in ASCII) per line. > > The issue seems to be presence of tabs along with the numbers in a single string. So, when I try to convert strings to numbers, it fails due to presence of tabs. > > Here is the hex dump: > > 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 > 74 68 2c 22 09 22 38 31 2c 22 09 35 63 0d 0a 22 > 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 ... This looks like this: "address," "length," "81," 5c "address," "length," "04," 11 "address," "length," "e1," 17 "address," "length," "6a," 6c ... Note that the commas are within the quotes. I'd say Andrea is correct: This is a tab-separated file, not a comma-separated file. But for some reason all fields except the last end with a comma. I would a) try to convince the person producing the file to clean up the mess b) if that is not successful, use the csv module to read the file with separator tab and then discard the trailing commas. 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 rosuav at gmail.com Sun Sep 8 14:47:35 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 9 Sep 2019 04:47:35 +1000 Subject: [Python-ideas] Re: Automatic translation of Python to assembly language In-Reply-To: <156796623670.4866.17888928515216262502@mail.python.org> References: <20190908195150.6327991b@fsol> <156796623670.4866.17888928515216262502@mail.python.org> Message-ID: On Mon, Sep 9, 2019 at 4:12 AM Mark @pysoniq wrote: > > "I don't think the ctypes wrapper in itself is very interesting." > > Well, we disagree on that! I think that automatic generation of a ctypes wrapper to connect Python to assembly is interesting and a huge timesaver. > > "I don't know where to find those blog entries." > > The blogs can be reached directly at: https://pysoniq.com/text_16.htm and there is a link "Blog" on the home page. That link should light up when you go to the link I've just provided. > > If you read the 3rd & 4th entries -- Assembly Optimizations in Complex Calculations ? Part 1 of 2 and Assembly Optimizations in Complex Calculations ? Part 2 of 2, those blog posts contain a step-by-step breakdown of translating Python source to assembly language. And the NASM source is linked there and is also available at the Resources link, so you can follow along between the Python source and the assembly listing. > > Your questions are helpful! > Redirecting this to python-list as this is not discussing any actual Python language or interpreter ideas. Please remove python-ideas from cc for future replies. Your blog breaks the browser's Back button. Please don't do that; if you want people to read your content, make it easy for us to do so. How does your assembly translation cope with the possibility that "round(h, 2)" might not call the Python standard library round() function? You go to great lengths to show how you can optimize a specific type of calculation that appears to be specifically relating to floating-point math, including a number of steps that are basically the same as a peephole optimizer could do - or, for that matter, any programmer could easily do manually (I don't see a lot of loops in the real world that go "for a in n" and then "v0 = a" with no other use of a). What would happen if integers were involved? Remember, Python's integers can happily grow beyond any CPU register - will your transformation maintain the semantics of Python, or will it assume that everything is floating-point? > This line is self-explanatory. It takes the input array "n" and loops through each data point. The input array "n" is 64-bit double-precision floating point. How do you ensure that this is an array of 64-bit floating-point values? ChrisA From pankaj.jangid at gmail.com Sun Sep 8 14:50:44 2019 From: pankaj.jangid at gmail.com (Pankaj Jangid) Date: Mon, 09 Sep 2019 00:20:44 +0530 Subject: local variable 'p' referenced before assignment References: Message-ID: David writes: > https://docs.python.org/3/faq/programming.html#what-are-the-rules-for-local-and-global-variables-in-python > > " If a variable is assigned a value anywhere within the function?s body, > it?s assumed to be a local unless explicitly declared as global." > Coming with a baggage of other languages. :-) I should have searched it. Thanks a lot for sharing this. Regards. -- Pankaj Jangid From skip.montanaro at gmail.com Sun Sep 8 15:17:52 2019 From: skip.montanaro at gmail.com (Skip Montanaro) Date: Sun, 8 Sep 2019 14:17:52 -0500 Subject: [Python-ideas] Re: Automatic translation of Python to assembly language In-Reply-To: References: <20190908195150.6327991b@fsol> <156796623670.4866.17888928515216262502@mail.python.org> Message-ID: ChrisA> Your blog breaks the browser's Back button. Please don't do that; if ChrisA> you want people to read your content, make it easy for us to do so. Mark, I didn't even go that far. If you want me to read your blog, please load the individual essays into separate pages, not a narrow scrolling window. Heck, most/all links should just open a new page. Why the narrow scrolling widget? Further, as far as I can tell, you don't provide any details about how well it conforms to existing CPython semantics. For example: * What version(s) of Python are you targeting? * Does the entire test suite pass? If not, what fails? Skip From python at mrabarnett.plus.com Sun Sep 8 15:37:53 2019 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 8 Sep 2019 20:37:53 +0100 Subject: Finding lines in .txt file that contain keywords from two different set() In-Reply-To: <3598f0bb-5854-4d16-ab9c-56d1b0784970@googlegroups.com> References: <3598f0bb-5854-4d16-ab9c-56d1b0784970@googlegroups.com> Message-ID: <7f008d2f-e8af-422d-53b3-b59cb532e392@mrabarnett.plus.com> On 2019-09-08 17:02, A S wrote: > My problem is seemingly profound but I hope to make it sound as simplified as possible.....Let me unpack the details..: > > 1. I have one folder of Excel (.xlsx) files that serve as a data dictionary. > > -In Cell A1, the data source name is written in between brackets > > -In Cols C:D, it contains the data field names (It could be in either col C or D in my actual Excel sheet. So I had to search both columns > > -*Important: I need to know which data source the field names come from > > 2. I have another folder of Text (.txt) files that I need to parse through to find these keywords. > > These are the folders used for a better reference ( https://drive.google.com/open?id=1_LcceqcDhHnWW3Nrnwf5RkXPcnDfesq ). The files are found in the folder. > > This is the code I have thus far...: > [snip] result1 contains a list of strings. None of the words in 'checkhere' are in that list. I think what you intended to do was to check whether any of those words are in any of the strings of result1: for element in checkhere: for r in result1: if element in r: print(r) # Found one, so stop looking for more. break From PythonList at DancesWithMice.info Sun Sep 8 15:41:59 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Mon, 9 Sep 2019 07:41:59 +1200 Subject: Finding lines in .txt file that contain keywords from two different set() In-Reply-To: <3598f0bb-5854-4d16-ab9c-56d1b0784970@googlegroups.com> References: <3598f0bb-5854-4d16-ab9c-56d1b0784970@googlegroups.com> Message-ID: On 9/09/19 4:02 AM, A S wrote: > My problem is seemingly profound but I hope to make it sound as simplified as possible.....Let me unpack the details..: ... > These are the folders used for a better reference ( https://drive.google.com/open?id=1_LcceqcDhHnWW3Nrnwf5RkXPcnDfesq ). The files are found in the folder. The link resulted in a 404 page (for me - but then I don't use Google). So, without any sample data... > 1. I have one folder of Excel (.xlsx) files that serve as a data dictionary. > > -In Cell A1, the data source name is written in between brackets > > -In Cols C:D, it contains the data field names (It could be in either col C or D in my actual Excel sheet. So I had to search both columns > > -*Important: I need to know which data source the field names come from > > 2. I have another folder of Text (.txt) files that I need to parse through to find these keywords. Recommend you start with a set of test data/directories. For the first run, have one of each type of file, where the keywords correlate. Thus prove that the system works when you know it should. Next, try the opposite, to ensure that it equally-happily ignores, when it should. Then expand to having multiple records, so that you can see what happens when some files correlate, and some don't. ie take a large problem and break it down into smaller units. This is a "top-down" method. An alternate design approach (which works very well in Python - see also "PyTest") is to embrace the principles of TDD (Test-Driven Development). This is a process that builds 'from the ground, up'. In this, we design a small part of the process - let's call it a function/method: first we code some test data *and* the expected answer, eg if one input is 1 and another is 2 is their addition 3? (running such a test at this stage will fail - badly!); and then we write some code - and keep perfecting it until it passes the test. Repeat, stage-by-stage, to build the complete program - meantime, every change you make to the code should be tested against not just 'its own' test, but all of the tests which originally related to some other smaller unit of the whole. In this way, 'new code' can be shown to break (or not - hopefully) previously implemented, tested, and 'proven' code! Notice how you have broken-down the larger problem in the description (points 1 to 5, above)! Design the tests similarly, to *only* test one small piece of the puzzle (often you will have to 'fake' or "mock" data-inputs to the process, particularly if code to produce that unit's input has yet to be written, but regardless 'mock data' is thoroughly controlled and thus produces (more) predictable results) - plus, it's much easier to spot errors and omissions when you don't have to wade through a mass of print-outs that (attempt to) cover *everything*! (IMHO) Plus, when a problem is well-confined, there's less example code and data to insert into list questions, and the responses will be equally-focussed! Referring back to the question: it seems that the issue is either that the keywords are not being (correctly) picked-out of the sets of files (easy tests - for *only* those small section of the code!), or that the logic linking the key-words is faulty (another *small* test, easily coded - and at first fed with 'fake' key-words which prove the various test cases, and thus, when run, (attempt to) prove your logic and code!) -- Regards =dn From ekopalypse at gmail.com Sun Sep 8 15:58:28 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Sun, 8 Sep 2019 12:58:28 -0700 (PDT) Subject: Color representation as rgb, int and hex Message-ID: I'm confused about the following import sys print(tuple(bytes.fromhex('282C34'))) print(tuple((0x282C34).to_bytes(3, byteorder=sys.byteorder))) which results in (40, 44, 52) (52, 44, 40) on my machine. Shouldn't I expect the same result? Thank you Eren From python at mrabarnett.plus.com Sun Sep 8 16:18:18 2019 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 8 Sep 2019 21:18:18 +0100 Subject: Color representation as rgb, int and hex In-Reply-To: References: Message-ID: On 2019-09-08 20:58, Eko palypse wrote: > I'm confused about the following > > import sys > print(tuple(bytes.fromhex('282C34'))) > print(tuple((0x282C34).to_bytes(3, byteorder=sys.byteorder))) > > which results in > > (40, 44, 52) > (52, 44, 40) > > on my machine. Shouldn't I expect the same result? > You haven't said whether your machine is big-endian or little-endian. On a little-endian machine a 4-byte integer 0x282C34 is stored as: 34 2C 28 00 -> increasing address -> On a big-endian machine a 4-byte integer 0x282C34 is stored as: 00 28 2C 34 -> increasing address -> From roel at roelschroeven.net Sun Sep 8 16:18:18 2019 From: roel at roelschroeven.net (Roel Schroeven) Date: Sun, 8 Sep 2019 22:18:18 +0200 Subject: [Python-ideas] Re: Automatic translation of Python to assembly language In-Reply-To: References: <20190908195150.6327991b@fsol> <156796623670.4866.17888928515216262502@mail.python.org> Message-ID: Skip Montanaro schreef op 8/09/2019 om 21:17: > ChrisA> Your blog breaks the browser's Back button. Please don't do that; if > ChrisA> you want people to read your content, make it easy for us to do so. > > Mark, > > I didn't even go that far. If you want me to read your blog, please > load the individual essays into separate pages, not a narrow scrolling > window. Heck, most/all links should just open a new page. Why the > narrow scrolling widget? It seems to be a badly tuned responsive design. If I use Firefox's zoom-in, things get a bit better and more readable. -- "Honest criticism is hard to take, particularly from a relative, a friend, an acquaintance, or a stranger." -- Franklin P. Jones Roel Schroeven From rosuav at gmail.com Sun Sep 8 16:31:27 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 9 Sep 2019 06:31:27 +1000 Subject: Color representation as rgb, int and hex In-Reply-To: References: Message-ID: On Mon, Sep 9, 2019 at 6:01 AM Eko palypse wrote: > > I'm confused about the following > > import sys > print(tuple(bytes.fromhex('282C34'))) > print(tuple((0x282C34).to_bytes(3, byteorder=sys.byteorder))) > > which results in > > (40, 44, 52) > (52, 44, 40) > > on my machine. Shouldn't I expect the same result? Your first example is a sequence of three bytes: 28 in the first position, then 2C, then 34 in the last position. Your second example has 28 in the most-significant byte, 2C in the middle, and 34 in the least-significant byte. For those to come out identical, the concepts of "most-significant byte" and "first position" have to mean the same, which means you want big-endian, which is also referred to as "network byte order". So don't use sys.byteorder - just explicitly ask for big-endianness: print(tuple((0x282C34).to_bytes(3, "big"))) (40, 44, 52) ChrisA From ekopalypse at gmail.com Sun Sep 8 16:33:21 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Sun, 8 Sep 2019 13:33:21 -0700 (PDT) Subject: Color representation as rgb, int and hex In-Reply-To: References: Message-ID: <6146df9d-0e23-4ec8-9905-8edbea1cb2c4@googlegroups.com> > You haven't said whether your machine is big-endian or little-endian. Correct, it is little but I'm wondering why this isn't taking into account. I thought a method called fromhex would imply that bytes for an integer should be created and as that it would use the proper byte order to create it. But it seems that it treats the string literally. Isn't that confusing? Eren From ekopalypse at gmail.com Sun Sep 8 16:40:27 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Sun, 8 Sep 2019 13:40:27 -0700 (PDT) Subject: Color representation as rgb, int and hex In-Reply-To: References: Message-ID: <73081192-1972-44c5-92c3-2315cbc26d3d@googlegroups.com> > ChrisA You are correct, but, sorry for not being clear what confused me. I assumed it would use the sys.byteorder but I guess this is simply a AssumedError exception. :-) From rosuav at gmail.com Sun Sep 8 17:30:30 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 9 Sep 2019 07:30:30 +1000 Subject: Color representation as rgb, int and hex In-Reply-To: <6146df9d-0e23-4ec8-9905-8edbea1cb2c4@googlegroups.com> References: <6146df9d-0e23-4ec8-9905-8edbea1cb2c4@googlegroups.com> Message-ID: On Mon, Sep 9, 2019 at 6:37 AM Eko palypse wrote: > > > You haven't said whether your machine is big-endian or little-endian. > > Correct, it is little but I'm wondering why this isn't taking into account. > I thought a method called fromhex would imply that bytes for an integer > should be created and as that it would use the proper byte order to create it. > But it seems that it treats the string literally. > Isn't that confusing? > No, constructing a bytes literal from hex digits implies that they follow the sequence in the string of digits. It's nothing to do with the endinanness of integers. ChrisA From jfong at ms4.hinet.net Sun Sep 8 20:40:29 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Sun, 8 Sep 2019 17:40:29 -0700 (PDT) Subject: Is it 'fine' to instantiate a widget without parent parameter? In-Reply-To: References: <20b356eb-1343-4fe8-97be-5c1bbc45e18d@googlegroups.com> <712ccffb-8e13-4fba-9f0e-76ff4cd1bd63@googlegroups.com> <8ffda1b2-021b-468e-a568-5a9c813c36bc@googlegroups.com> Message-ID: <3498162b-676f-4173-b5e5-0719e6dbf745@googlegroups.com> David? 2019?9?8???? UTC+8??8?14?03???? > On Sun, 8 Sep 2019 at 21:05, wrote: > > David? 2019?9?8???? UTC+8??6?44?55???? > > > On Sun, 8 Sep 2019 at 20:25, wrote: > > > > > If I have two widgets created this way: > > > > t0 = tkinter.Text() > > > > t1 = tkinter.Text() > > > > How many Tk objects will there be? > > > Sorry, didn't make it clear. I mean > > Sorry I didn't read more carefully. > But I think that the method I demonstrated can give > the answer to your question. > > >>> import tkinter > >>> t0 = tkinter.Text() > >>> t1 = tkinter.Text() > >>> t0.master > > >>> t1.master > > >>> t0.master is t1.master > True > >>> Thank you. After a quick trace to find out the reason, I found that Tkinter prevents Tk() be called more than once from widget constructors, so only one Tk object exists:-) --Jach From tjreedy at udel.edu Mon Sep 9 03:06:04 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 9 Sep 2019 03:06:04 -0400 Subject: Is it 'fine' to instantiate a widget without parent parameter? In-Reply-To: <3498162b-676f-4173-b5e5-0719e6dbf745@googlegroups.com> References: <20b356eb-1343-4fe8-97be-5c1bbc45e18d@googlegroups.com> <712ccffb-8e13-4fba-9f0e-76ff4cd1bd63@googlegroups.com> <8ffda1b2-021b-468e-a568-5a9c813c36bc@googlegroups.com> <3498162b-676f-4173-b5e5-0719e6dbf745@googlegroups.com> Message-ID: On 9/8/2019 8:40 PM, jfong at ms4.hinet.net wrote: > Thank you. After a quick trace to find out the reason, I found that Tkinter prevents Tk() be called more than once from widget constructors, so only one Tk object exists:-) There will only be one default Tk object, but there can be multiple Tk objects. >>> import tkinter as tk >>> r1 = tk.Tk() >>> r2 = tk.Tk() >>> r1.tk <_tkinter.tkapp object at 0x000001F90F2F1D30> >>> r2.tk <_tkinter.tkapp object at 0x000001F90F328930> -- Terry Jan Reedy From greg.ewing at canterbury.ac.nz Mon Sep 9 05:30:25 2019 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Mon, 09 Sep 2019 21:30:25 +1200 Subject: [Python-ideas] Re: Automatic translation of Python to assembly language In-Reply-To: References: <20190908195150.6327991b@fsol> <156796623670.4866.17888928515216262502@mail.python.org> Message-ID: Mark at PysoniQ.com wrote: > an extension (.dll or .so) is not generally included in a > makefile because it's dynamically linked and not incorporated into an > executable -- which Python doesn't have. If I change the source, the .dll or .so needs to be re-created. That's a build step, and as such I'm going to want to automate it. Whether I do that using a Makefile or some other tool isn't the point. The point is that if I'm required to use a point-and-click interface, then I can't automate it. If there is an alternative interface that can be automated, that's fine. But either way, the point and click interface is useless to me, and therefore not a selling point for me in any way. I suspect the same is true for many other people on the Python lists. -- Greg From greg.ewing at canterbury.ac.nz Mon Sep 9 05:35:30 2019 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Mon, 09 Sep 2019 21:35:30 +1200 Subject: Color representation as rgb, int and hex In-Reply-To: <6146df9d-0e23-4ec8-9905-8edbea1cb2c4@googlegroups.com> References: <6146df9d-0e23-4ec8-9905-8edbea1cb2c4@googlegroups.com> Message-ID: Eko palypse wrote: > I thought a method called fromhex would imply that bytes for an integer > should be created Why should it imply that? You're asking it to create some bytes from a string of hex digits -- no mention of integers. The obvious thing to do is to put the bytes in the order they apper in the string. -- Greg From ekopalypse at gmail.com Mon Sep 9 06:59:07 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Mon, 9 Sep 2019 03:59:07 -0700 (PDT) Subject: Color representation as rgb, int and hex In-Reply-To: References: <6146df9d-0e23-4ec8-9905-8edbea1cb2c4@googlegroups.com> Message-ID: <11b41b2a-367d-45ce-bfd5-08125a5a9357@googlegroups.com> > No, constructing a bytes literal from hex digits implies that they > follow the sequence in the string of digits. It's nothing to do with > the endinanness of integers. > ChrisA > Why should it imply that? You're asking it to create some bytes > from a string of hex digits -- no mention of integers. The obvious > thing to do is to put the bytes in the order they apper in the > string. > Greg @Greg, ChrisA Agreed, as I said this came into my mind first as I saw the name of the method but your arguments make sense, absolutely. Thank you Eren From info at wingware.com Mon Sep 9 10:34:30 2019 From: info at wingware.com (Wingware) Date: Mon, 09 Sep 2019 10:34:30 -0400 Subject: ANN: Wing Python IDE 7.1.1 Message-ID: <5D7662F6.6040902@wingware.com> Wing Python IDE version 7.1.1 has been released. This release avoids slowing and dropping of remote development connections, fixes showing Pandas DataFrame and Series values, makes OS Commands work on remote hosts with Python 3, inspects remote extension modules with non-ascii characters in the interface, adds __init__ arguments to the auto-completer, allows ignoring exceptions in frozen importlib files, fixes line numbers shown in pytest exception tracebacks, and fixes other minor usability issues. == Some Highlights of Wing 7.1 == * Support for Python 3.8: Wing 7.1 supports editing, testing, and debugging code written for Python 3.8, so you can take advantage of assignment expressions and other improvements introduced in this new version of Python. * Improved Code Warnings: Wing 7.1 adds unused symbol warnings for imports, variables, and arguments found in Python code. This release also improves code warnings configuration, making it easier to disable unwanted warnings. * Cosmetic Improvements: Wing 7.1 improves the auto-completer, project tool, and code browser with redesigned icons that make use of Wing's icon color configuration. This release also improves text display on some Linux systems, supports Dark Mode on macOS, and improves display of Python code and icons found in documentation. * And More: Wing 7.1 also adds support for Windows 10 native OpenSSH installations for remote development, and makes a number of other minor improvements. This release drops support for macOS 10.11. System requirements remain unchanged on Windows and Linux. For details see the change log: https://wingware.com/pub/wingpro/7.1.1.0/CHANGELOG.txt For a complete list of new features in Wing 7, see What's New in Wing 7: https://wingware.com/wingide/whatsnew == Downloads == Wing Pro: https://wingware.com/downloads/wing-pro/7.1/binaries Wing Personal: https://wingware.com/downloads/wing-personal/7.1/binaries Wing 101: https://wingware.com/downloads/wing-101/7.1/binaries Compare Products: https://wingware.com/downloads See Upgrading https://wingware.com/doc/install/upgrading for details on upgrading from Wing 6 and earlier, and Migrating from Older Versions https://wingware.com/doc/install/migrating for a list of compatibility notes. From larry at hastings.org Mon Sep 9 10:39:48 2019 From: larry at hastings.org (Larry Hastings) Date: Mon, 9 Sep 2019 15:39:48 +0100 Subject: [RELEASED] Python 3.5.8rc1 is released Message-ID: On behalf of the Python development community, I'm chuffed to announce the availability of Python 3.5.8rc1. Python 3.5 is in "security fixes only" mode.? This new version only contains security fixes, not conventional bug fixes, and it is a source-only release. You can find Python 3.5.8rc1 here: https://www.python.org/downloads/release/python-358rc1/ I think Python 3.5 may just barely outlive 2.7, //arry/ From toby at tobiah.org Mon Sep 9 13:23:57 2019 From: toby at tobiah.org (Tobiah) Date: Mon, 9 Sep 2019 10:23:57 -0700 Subject: [OT(?)] Ubuntu 18 now defaults to 4-space tabs Message-ID: We upgraded a server to 18.04 and now when I start typing a python file (seems to be triggered by the .py extension) the tabs default to 4 spaces. We have decades of code that use tab characters, and it has not been our intention to change that. I found a /usr/share/vim/vim80/indent/python.vim and tried moving it out of the way, but the behavior was still there. This is more of a vim question perhaps, but I'm already subscribed here and I figured someone would know what to do. Thanks! From * at eli.users.panix.com Mon Sep 9 16:30:54 2019 From: * at eli.users.panix.com (Eli the Bearded) Date: Mon, 9 Sep 2019 20:30:54 +0000 (UTC) Subject: [OT(?)] Ubuntu 18 vim now defaults to 4-space tabs References: Message-ID: In comp.lang.python, Tobiah wrote: > We upgraded a server to 18.04 and now when I start typing Your subject missed a critical word: vim. There are a lot of editors in Ubuntu, and probably they don't all do that. > This is more of a vim question perhaps, but I'm already > subscribed here and I figured someone would know what > to do. Run vim. Then ':set' to see what's set different than default. Then, if it is tabstop you want to know about, ':verbose set tabstop?' will tell you where that setting was last altered. I'm not seeing tabstops changed on my Ubuntu 18.04, but I may have vim installed with different packages. I prefer vim configured in a closer to vi-compatible way than defaults. Elijah ------ expects `ed` and `nano` still work the same From best_lay at yahoo.com Mon Sep 9 16:42:11 2019 From: best_lay at yahoo.com (Wildman) Date: Mon, 09 Sep 2019 15:42:11 -0500 Subject: [OT(?)] Ubuntu 18 now defaults to 4-space tabs References: Message-ID: On Mon, 09 Sep 2019 10:23:57 -0700, Tobiah wrote: > We upgraded a server to 18.04 and now when I start typing > a python file (seems to be triggered by the .py extension) > the tabs default to 4 spaces. We have decades of code that > use tab characters, and it has not been our intention to > change that. > > I found a /usr/share/vim/vim80/indent/python.vim and tried > moving it out of the way, but the behavior was still there. > > This is more of a vim question perhaps, but I'm already > subscribed here and I figured someone would know what > to do. > > > Thanks! There are quite a few vim users that frequent the alt.os.linux newsgroups, i.e.; alt.os.linux alt.os.linux.debian alt.os.linux.mint alt.os.linux.ubuntu -- GNU/Linux user #557453 The early bird might get the worm but it is the second mouse that gets the cheese. From best_lay at yahoo.com Mon Sep 9 17:03:30 2019 From: best_lay at yahoo.com (Wildman) Date: Mon, 09 Sep 2019 16:03:30 -0500 Subject: [OT(?)] Ubuntu 18 now defaults to 4-space tabs References: Message-ID: On Mon, 09 Sep 2019 10:23:57 -0700, Tobiah wrote: > We upgraded a server to 18.04 and now when I start typing > a python file (seems to be triggered by the .py extension) > the tabs default to 4 spaces. We have decades of code that > use tab characters, and it has not been our intention to > change that. > > I found a /usr/share/vim/vim80/indent/python.vim and tried > moving it out of the way, but the behavior was still there. > > This is more of a vim question perhaps, but I'm already > subscribed here and I figured someone would know what > to do. > > > Thanks! A quick search may have revealed your problem. Look for an entry named 'expandtab' in vimrc. You should find vimrc in /etc/vim or /usr/share/vim or both. Also if you have both, one may be a link to the other. If the entry is there, place a quote mark in front of it to comment it out... "set expandtab If you don't have the entry then it is a different problem and I would point you back to my original post as my knowledge of vim is very limited. -- GNU/Linux user #557453 "There are only 10 types of people in the world... those who understand Binary and those who don't." -Spike From jfong at ms4.hinet.net Mon Sep 9 20:30:01 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Mon, 9 Sep 2019 17:30:01 -0700 (PDT) Subject: Is it 'fine' to instantiate a widget without parent parameter? In-Reply-To: References: <20b356eb-1343-4fe8-97be-5c1bbc45e18d@googlegroups.com> <712ccffb-8e13-4fba-9f0e-76ff4cd1bd63@googlegroups.com> <8ffda1b2-021b-468e-a568-5a9c813c36bc@googlegroups.com> <3498162b-676f-4173-b5e5-0719e6dbf745@googlegroups.com> Message-ID: <60b94304-5f6c-4a6d-8de2-4b2b55ba29f1@googlegroups.com> Terry Reedy? 2019?9?9???? UTC+8??3?06?27???? > On 9/8/2019 8:40 PM, jfong at ms4.hinet.net wrote: > > > Thank you. After a quick trace to find out the reason, I found that Tkinter prevents Tk() be called more than once from widget constructors, so only one Tk object exists:-) > > There will only be one default Tk object, but there can be multiple Tk > objects. > > >>> import tkinter as tk > >>> r1 = tk.Tk() > >>> r2 = tk.Tk() > >>> r1.tk > <_tkinter.tkapp object at 0x000001F90F2F1D30> > >>> r2.tk > <_tkinter.tkapp object at 0x000001F90F328930> > > > -- > Terry Jan Reedy >>> import tkinter as tk >>> f0 = tk.Frame() >>> root0 = tk.Tk() >>> f0.master >>> root0 >>> >>> import tkinter as tk >>> root0 = tk.Tk() >>> f0 = tk.Frame() >>> f0.master >>> root0 >>> Why? PS. Maybe there is no why, just it is what it is:-) --Jach From flebber.crue at gmail.com Mon Sep 9 22:56:23 2019 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Mon, 9 Sep 2019 19:56:23 -0700 (PDT) Subject: pandas loc on str lower for column comparison In-Reply-To: References: <05e75ce6-34f3-41c3-a04e-3a900b98488a@googlegroups.com> <812057cd-9f21-462c-b071-2e8f8d23bd12@googlegroups.com> <7a516845-7daf-447e-84cd-64c9bc315c9f@googlegroups.com> Message-ID: <32d818a9-8565-47d6-8487-1571b4048c01@googlegroups.com> On Friday, 6 September 2019 07:52:56 UTC+10, Piet van Oostrum wrote: > Piet van Oostrum <> writes: > > > That would select ROWS 0,1,5,6,7, not columns. > > To select columns 0,1,5,6,7, use two-dimensional indexes > > > > df1 = df.iloc[:, [0,1,5,6,7]] > > > > : selects all rows. > > And that also solves your original problem. > > This statement: > > df1['Difference'] = df1.loc['Current Team'].str.lower().str.strip() == df1.loc['New Team'].str.lower().str.strip() > > should not use .loc, because then you are selecting rows, not columns, but: > > df1['Difference'] = df1['Current Team'].str.lower().str.strip() == df1['New Team'].str.lower().str.strip() > -- > Piet van Oostrum <> > WWW: http://piet.vanoostrum.org/ > PGP key: [8DAE142BE17999C4] That actually creates another error. A value is trying to be set on a copy of a slice from a DataFrame. Try using .loc[row_indexer,col_indexer] = value instead See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy So tried this df['c'] = df.apply(lambda df1: df1['Current Team'].str.lower().str.strip() == df1['New Team'].str.lower().str.strip(), axis=1) Based on this SO answer https://stackoverflow.com/a/46570641 Thoughts? Sayth From sharan.basappa at gmail.com Mon Sep 9 23:07:08 2019 From: sharan.basappa at gmail.com (Sharan Basappa) Date: Mon, 9 Sep 2019 20:07:08 -0700 (PDT) Subject: issue in handling CSV data In-Reply-To: References: <64e71dbd-7a55-4230-84d0-19d8aa8944c4@googlegroups.com> <976337c0-ce9f-49b6-974e-062079cc0483@googlegroups.com> <20190908163947.GA22749@hjp.at> Message-ID: On Sunday, 8 September 2019 12:45:45 UTC-4, Peter J. Holzer wrote: > On 2019-09-08 05:41:07 -0700, Sharan Basappa wrote: > > On Sunday, 8 September 2019 04:56:29 UTC-4, Andrea D'Amore wrote: > > > On Sun, 8 Sep 2019 at 02:19, Sharan Basappa wrote: > > > > As you can see, the string "\t"81 is causing the error. > > > > It seems to be due to char "\t". > > > > > > It is not clear what format do you expect to be in the file. > > > You say "it is CSV" so your actual payload seems to be a pair of three > > > bytes (a tab and two hex digits in ASCII) per line. > > > > The issue seems to be presence of tabs along with the numbers in a single string. So, when I try to convert strings to numbers, it fails due to presence of tabs. > > > > Here is the hex dump: > > > > 22 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 > > 74 68 2c 22 09 22 38 31 2c 22 09 35 63 0d 0a 22 > > 61 64 64 72 65 73 73 2c 22 09 22 6c 65 6e 67 74 > ... > > This looks like this: > > "address," "length," "81," 5c > "address," "length," "04," 11 > "address," "length," "e1," 17 > "address," "length," "6a," 6c > ... > > Note that the commas are within the quotes. I'd say Andrea is correct: > This is a tab-separated file, not a comma-separated file. But for some > reason all fields except the last end with a comma. > > I would > > a) try to convince the person producing the file to clean up the mess > > b) if that is not successful, use the csv module to read the file with > separator tab and then discard the trailing commas. > Hi Peter, I respectfully disagree that it is not a comma separated. Let me explain why. If you look the following line in the code, it specifies comma as the delimiter: ######################## my_data = genfromtxt('constraints.csv', delimiter = ',', dtype=None) ######################## Now, if you see the print after getting the data, it looks like this: ############################## [['"\t"81' '"\t5c'] ?['"\t"04' '"\t11'] ?['"\t"e1' '"\t17'] ?['"\t"6a' '"\t6c'] ?['"\t"53' '"\t69'] ?['"\t"98' '"\t87'] ?['"\t"5c' '"\t4b'] ############################## if you observe, the commas have disappeared. That, I think, is because it actually treated this as a CSV file. Anyway, I am checking to see if I can discard the tabs and process this. I will keep everyone posted. From sharan.basappa at gmail.com Mon Sep 9 23:19:03 2019 From: sharan.basappa at gmail.com (Sharan Basappa) Date: Mon, 9 Sep 2019 20:19:03 -0700 (PDT) Subject: numpy array - convert hex to int In-Reply-To: References: <0b3c5c58-bdb6-4f36-b91c-5e3a667a7833@googlegroups.com> Message-ID: <150b969f-cc4e-474e-bffe-91b27fa30d63@googlegroups.com> On Sunday, 8 September 2019 11:16:52 UTC-4, Luciano Ramalho wrote: > >>> int('C0FFEE', 16) > 12648430 > > There you go! > > On Sun, Sep 8, 2019 at 12:02 PM Sharan Basappa wrote: > > > > I have a numpy array that has data in the form of hex. > > I would like to convert that into decimal/integer. > > Need suggestions please. > > -- I am sorry. I forgot to mention that I have the data in a numpy array. So, when I try to convert to int, I get the following error. sample code here ##################### my_data_3 = int(my_data_2) my_data_4 = my_data_3.astype(np.float) ##################### Error here ############### #np.core.defchararray.replace(my_data_2,",'') 27 ---> 28 my_data_3 = int(my_data_2) 29 30 my_data_4 = my_data_3.astype(np.float) TypeError: only length-1 arrays can be converted to Python scalars ######################### From tjreedy at udel.edu Mon Sep 9 23:42:43 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Mon, 9 Sep 2019 23:42:43 -0400 Subject: Is it 'fine' to instantiate a widget without parent parameter? In-Reply-To: <60b94304-5f6c-4a6d-8de2-4b2b55ba29f1@googlegroups.com> References: <20b356eb-1343-4fe8-97be-5c1bbc45e18d@googlegroups.com> <712ccffb-8e13-4fba-9f0e-76ff4cd1bd63@googlegroups.com> <8ffda1b2-021b-468e-a568-5a9c813c36bc@googlegroups.com> <3498162b-676f-4173-b5e5-0719e6dbf745@googlegroups.com> <60b94304-5f6c-4a6d-8de2-4b2b55ba29f1@googlegroups.com> Message-ID: On 9/9/2019 8:30 PM, jfong at ms4.hinet.net wrote: > Terry Reedy? 2019?9?9???? UTC+8??3?06?27???? >> There will only be one default Tk object, but there can be multiple Tk >> objects. >>>> import tkinter as tk >>>> f0 = tk.Frame() This causes creation of a default root >>>> root0 = tk.Tk() This creates another, hence two different objects. >>>> f0.master > >>>> root0 > >>>> import tkinter as tk >>>> root0 = tk.Tk() This creates a root that is set as the default because there was not one already. >>>> f0 = tk.Frame() The uses the default root which is root0, hence 1 object. >>>> f0.master > >>>> root0 > -- Terry Jan Reedy From jfong at ms4.hinet.net Mon Sep 9 23:49:52 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Mon, 9 Sep 2019 20:49:52 -0700 (PDT) Subject: How to correctly use 'in_' argument in tkinter grid()? Message-ID: <61c8563b-ec5f-4ab9-b3f0-c4b906064ac8@googlegroups.com> I had tried the following script test.py: -------- import tkinter as tk class Demo(tk.Frame): def __init__(self): tk.Frame.__init__(self, name='demo') self.pack() panel = tk.Frame(self, name='panel') panel.pack() start = tk.Button(text='Start', name='start') start.grid(in_=panel) btn = self.nametowidget('panel.start') btn.config(state='disabled') Demo().mainloop() -------- It fails on nametowidget() function. My intention is to use 'in_' to change the parent of 'start' widget from the default Tk object to 'panel', but failed with KeyError: 'start'. below is part of the snapshot in pdb, ... > d:\works\python\test.py(11)__init__() -> start = tk.Button(text='Start', name='start') (Pdb) !panel.winfo_parent() '.demo' (Pdb) next > d:\works\python\test.py(12)__init__() -> start.grid(in_=panel) (Pdb) !start.winfo_parent() '.' (Pdb) next > d:\works\python\test.py(14)__init__() -> btn = self.nametowidget('panel.start') (Pdb) !start.winfo_parent() '.' --Jach From jfong at ms4.hinet.net Tue Sep 10 00:02:32 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Mon, 9 Sep 2019 21:02:32 -0700 (PDT) Subject: Is it 'fine' to instantiate a widget without parent parameter? In-Reply-To: References: <20b356eb-1343-4fe8-97be-5c1bbc45e18d@googlegroups.com> <712ccffb-8e13-4fba-9f0e-76ff4cd1bd63@googlegroups.com> <8ffda1b2-021b-468e-a568-5a9c813c36bc@googlegroups.com> <3498162b-676f-4173-b5e5-0719e6dbf745@googlegroups.com> <60b94304-5f6c-4a6d-8de2-4b2b55ba29f1@googlegroups.com> Message-ID: <1941ba24-0ed6-4ddb-ad9d-6fae4b7e9cf2@googlegroups.com> Terry Reedy? 2019?9?10???? UTC+8??11?43?05???? > On 9/9/2019 8:30 PM, jfong at ms4.hinet.net wrote: > > Terry Reedy? 2019?9?9???? UTC+8??3?06?27???? > > >> There will only be one default Tk object, but there can be multiple Tk > >> objects. > > >>>> import tkinter as tk > >>>> f0 = tk.Frame() > > This causes creation of a default root > > >>>> root0 = tk.Tk() > > This creates another, hence two different objects. > > >>>> f0.master > > > >>>> root0 > > > > >>>> import tkinter as tk > >>>> root0 = tk.Tk() > > This creates a root that is set as the default because there was not one > already. > > >>>> f0 = tk.Frame() > > The uses the default root which is root0, hence 1 object. > > >>>> f0.master > > > >>>> root0 > > > > -- > Terry Jan Reedy Got it. The first Tk object is always the default one no matter where it was created. The default one is always the one which the widget constructor refer to when required. --Jach From flebber.crue at gmail.com Tue Sep 10 00:04:24 2019 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Mon, 9 Sep 2019 21:04:24 -0700 (PDT) Subject: pandas loc on str lower for column comparison In-Reply-To: <32d818a9-8565-47d6-8487-1571b4048c01@googlegroups.com> References: <05e75ce6-34f3-41c3-a04e-3a900b98488a@googlegroups.com> <812057cd-9f21-462c-b071-2e8f8d23bd12@googlegroups.com> <7a516845-7daf-447e-84cd-64c9bc315c9f@googlegroups.com> <32d818a9-8565-47d6-8487-1571b4048c01@googlegroups.com> Message-ID: <5b34ce1c-3a0d-437f-946f-0d6f442aff88@googlegroups.com> On Tuesday, 10 September 2019 12:56:36 UTC+10, Sayth Renshaw wrote: > On Friday, 6 September 2019 07:52:56 UTC+10, Piet van Oostrum wrote: > > Piet van Oostrum <> writes: > > > > > That would select ROWS 0,1,5,6,7, not columns. > > > To select columns 0,1,5,6,7, use two-dimensional indexes > > > > > > df1 = df.iloc[:, [0,1,5,6,7]] > > > > > > : selects all rows. > > > > And that also solves your original problem. > > > > This statement: > > > > df1['Difference'] = df1.loc['Current Team'].str.lower().str.strip() == df1.loc['New Team'].str.lower().str.strip() > > > > should not use .loc, because then you are selecting rows, not columns, but: > > > > df1['Difference'] = df1['Current Team'].str.lower().str.strip() == df1['New Team'].str.lower().str.strip() > > -- > > Piet van Oostrum <> > > WWW: http://piet.vanoostrum.org/ > > PGP key: [8DAE142BE17999C4] > > That actually creates another error. > > A value is trying to be set on a copy of a slice from a DataFrame. > Try using .loc[row_indexer,col_indexer] = value instead > > See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy > > So tried this > df['c'] = df.apply(lambda df1: df1['Current Team'].str.lower().str.strip() == df1['New Team'].str.lower().str.strip(), axis=1) > > Based on this SO answer https://stackoverflow.com/a/46570641 > > Thoughts? > > Sayth This works on an individual row df2 = df1.loc[(df1['Current Team'] == df1['New Team']),'D'] = 'Wow' But how do I apply it to the whole new column and return the new dataset? Trying to use lambda but it cannot contain assigment df2 = df1.apply(lambda df1: [ (df1['Current Team'] == df1['New Team']) ]['D'] = 'succeed') df2 Confused Sayth From none at gmail.com Tue Sep 10 04:00:34 2019 From: none at gmail.com (ast) Date: Tue, 10 Sep 2019 10:00:34 +0200 Subject: UserList from module collections Message-ID: <5d775823$0$6455$426a74cc@news.free.fr> Hello I read in a course that class UserList from module collections can be used to create our own custom list Example >>> from collections import UserList >>> class MyList(UserList): ... def head(self): ... return self.data[0] ... def queue(self): ... return self.data[1:] ... >>> l = MyList([1, 2, 3]) >>> l.append(4) >>> l [1, 2, 3, 4] >>> l.head() 1 >>> l.queue() [2, 3, 4] But I can do exactly the same without UserList >>> class MyList(list): def head(self): return self[0] def queue(self): return self[1:] >>> l = MyList([1, 2, 3]) >>> l.append(4) >>> l [1, 2, 3, 4] >>> l.head() 1 >>> l.queue() [2, 3, 4] So what UserList is used for ? From greg.ewing at canterbury.ac.nz Tue Sep 10 04:06:36 2019 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Tue, 10 Sep 2019 20:06:36 +1200 Subject: issue in handling CSV data In-Reply-To: References: <64e71dbd-7a55-4230-84d0-19d8aa8944c4@googlegroups.com> <976337c0-ce9f-49b6-974e-062079cc0483@googlegroups.com> <20190908163947.GA22749@hjp.at> Message-ID: Sharan Basappa wrote: > Now, if you see the print after getting the data, it looks like this: > > ############################## > [['"\t"81' '"\t5c'] > ['"\t"04' '"\t11'] > ['"\t"e1' '"\t17'] > ['"\t"6a' '"\t6c'] > ['"\t"53' '"\t69'] > ['"\t"98' '"\t87'] > ['"\t"5c' '"\t4b'] > ############################## But now you have weird things such as unmatched single quotes. It seems that whichever way you try to interpret it -- tab delimited or comma delimited -- it doesn't entirely make sense. That leads me to believe it has been corrupted somehow. -- Greg From greg.ewing at canterbury.ac.nz Tue Sep 10 04:12:13 2019 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Tue, 10 Sep 2019 20:12:13 +1200 Subject: UserList from module collections In-Reply-To: <5d775823$0$6455$426a74cc@news.free.fr> References: <5d775823$0$6455$426a74cc@news.free.fr> Message-ID: ast wrote: > So what UserList is used for ? It's mostly a leftover from the days when you couldn't subclass built-in types. But it can still be useful if you want a custom sequence object that doesn't inherit all of the built-in list type's behaviour. -- Greg From __peter__ at web.de Tue Sep 10 05:04:03 2019 From: __peter__ at web.de (Peter Otten) Date: Tue, 10 Sep 2019 11:04:03 +0200 Subject: How to correctly use 'in_' argument in tkinter grid()? References: <61c8563b-ec5f-4ab9-b3f0-c4b906064ac8@googlegroups.com> Message-ID: jfong at ms4.hinet.net wrote: > I had tried the following script test.py: > -------- > import tkinter as tk > > class Demo(tk.Frame): > def __init__(self): > tk.Frame.__init__(self, name='demo') > self.pack() > > panel = tk.Frame(self, name='panel') > panel.pack() > > start = tk.Button(text='Start', name='start') > start.grid(in_=panel) > > btn = self.nametowidget('panel.start') > btn.config(state='disabled') > > Demo().mainloop() > -------- > > It fails on nametowidget() function. My intention is to use 'in_' to > change the parent of 'start' widget from the default Tk object to 'panel', > but failed with KeyError: 'start'. > > below is part of the snapshot in pdb, > ... >> d:\works\python\test.py(11)__init__() > -> start = tk.Button(text='Start', name='start') > (Pdb) !panel.winfo_parent() > '.demo' > (Pdb) next >> d:\works\python\test.py(12)__init__() > -> start.grid(in_=panel) > (Pdb) !start.winfo_parent() > '.' > (Pdb) next >> d:\works\python\test.py(14)__init__() > -> btn = self.nametowidget('panel.start') > (Pdb) !start.winfo_parent() > '.' > > --Jach I think that the `in_` argument is used correctly. It's just that your expectation that the name is changed to reflect the layout hierarchy is wrong. To manipulate the start button you can use the `start` variable directly: start.config(state='disabled') To find all slaves of the panel use panel.grid_slaves() $ cat grid_in.py import tkinter as tk class Demo(tk.Frame): def __init__(self): tk.Frame.__init__(self, name='demo') self.pack() self.panel = panel = tk.Frame(self, name='panel') panel.pack() start = tk.Button(text='Start', name='start') start.grid(in_=panel) for btn in panel.grid_slaves(): print("disabling", btn._w) btn.config(state='disabled') Demo() #.mainloop() $ python3 grid_in.py disabling .start $ From tjreedy at udel.edu Tue Sep 10 05:15:57 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Tue, 10 Sep 2019 05:15:57 -0400 Subject: Is it 'fine' to instantiate a widget without parent parameter? In-Reply-To: <1941ba24-0ed6-4ddb-ad9d-6fae4b7e9cf2@googlegroups.com> References: <20b356eb-1343-4fe8-97be-5c1bbc45e18d@googlegroups.com> <712ccffb-8e13-4fba-9f0e-76ff4cd1bd63@googlegroups.com> <8ffda1b2-021b-468e-a568-5a9c813c36bc@googlegroups.com> <3498162b-676f-4173-b5e5-0719e6dbf745@googlegroups.com> <60b94304-5f6c-4a6d-8de2-4b2b55ba29f1@googlegroups.com> <1941ba24-0ed6-4ddb-ad9d-6fae4b7e9cf2@googlegroups.com> Message-ID: On 9/10/2019 12:02 AM, jfong at ms4.hinet.net wrote: > Got it. The first Tk object is always the default one no matter where it was created. The default one is always the one which the widget constructor refer to when required. Or one can call tkinter.NoDefaultRoot() and not worry about default roots. -- Terry Jan Reedy From piet-l at vanoostrum.org Tue Sep 10 05:34:12 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Tue, 10 Sep 2019 11:34:12 +0200 Subject: issue in handling CSV data References: <64e71dbd-7a55-4230-84d0-19d8aa8944c4@googlegroups.com> <976337c0-ce9f-49b6-974e-062079cc0483@googlegroups.com> <20190908163947.GA22749@hjp.at> Message-ID: Sharan Basappa writes: >> >> Note that the commas are within the quotes. I'd say Andrea is correct: >> This is a tab-separated file, not a comma-separated file. But for some >> reason all fields except the last end with a comma. >> However, genfromtxt is not a full-fledged CSV parser. It does not obey quotes. So the commas inside the quotes ARE treated as separators. > Hi Peter, > > I respectfully disagree that it is not a comma separated. Let me explain why. > If you look the following line in the code, it specifies comma as the delimiter: > > ######################## > my_data = genfromtxt('constraints.csv', delimiter = ',', dtype=None) > ######################## > > Now, if you see the print after getting the data, it looks like this: > > ############################## > [['"\t"81' '"\t5c'] > ?['"\t"04' '"\t11'] > ?['"\t"e1' '"\t17'] > ?['"\t"6a' '"\t6c'] > ?['"\t"53' '"\t69'] > ?['"\t"98' '"\t87'] > ?['"\t"5c' '"\t4b'] > ############################## 1) Where did the other fields (address, length) go? > > if you observe, the commas have disappeared. That, I think, is because > it actually treated this as a CSV file. 2) As I said above, if you choose ',' as separator, these will disappear. Similarly, if you choose TAB as seperator, the TABs will disappear. As the format is a strange mixture of the two, you can use either one. But if it would be read with a real CSV-reader, that obeys the quote convention, than using ',' as seperator will not work. Only TAB will work. But in both cases you would have to do some pre- or post-processing to get the data as you want them. > Anyway, I am checking to see if I can discard the tabs and process this. > I will keep everyone posted. -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From jfong at ms4.hinet.net Tue Sep 10 06:26:22 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Tue, 10 Sep 2019 03:26:22 -0700 (PDT) Subject: Is it 'fine' to instantiate a widget without parent parameter? In-Reply-To: References: <20b356eb-1343-4fe8-97be-5c1bbc45e18d@googlegroups.com> <712ccffb-8e13-4fba-9f0e-76ff4cd1bd63@googlegroups.com> <8ffda1b2-021b-468e-a568-5a9c813c36bc@googlegroups.com> <3498162b-676f-4173-b5e5-0719e6dbf745@googlegroups.com> <60b94304-5f6c-4a6d-8de2-4b2b55ba29f1@googlegroups.com> <1941ba24-0ed6-4ddb-ad9d-6fae4b7e9cf2@googlegroups.com> Message-ID: <0a755224-7e03-4611-99c4-86ec32e30edf@googlegroups.com> Terry Reedy? 2019?9?10???? UTC+8??5?16?19???? > On 9/10/2019 12:02 AM, jfong at ms4.hinet.net wrote: > > > Got it. The first Tk object is always the default one no matter where it was created. The default one is always the one which the widget constructor refer to when required. > > Or one can call tkinter.NoDefaultRoot() and not worry about default roots. > > -- > Terry Jan Reedy May I say this call has to be at the very beginning, before any widget was created using default root? And after this call, the 'parent' argument has to be explicitly assigned in the widget constructor? --Jach From jfong at ms4.hinet.net Tue Sep 10 06:28:25 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Tue, 10 Sep 2019 03:28:25 -0700 (PDT) Subject: How to correctly use 'in_' argument in tkinter grid()? In-Reply-To: References: <61c8563b-ec5f-4ab9-b3f0-c4b906064ac8@googlegroups.com> Message-ID: Peter Otten? 2019?9?10???? UTC+8??5?04?27???? > jfong at ms4.hinet.net wrote: > > > I had tried the following script test.py: > > -------- > > import tkinter as tk > > > > class Demo(tk.Frame): > > def __init__(self): > > tk.Frame.__init__(self, name='demo') > > self.pack() > > > > panel = tk.Frame(self, name='panel') > > panel.pack() > > > > start = tk.Button(text='Start', name='start') > > start.grid(in_=panel) > > > > btn = self.nametowidget('panel.start') > > btn.config(state='disabled') > > > > Demo().mainloop() > > -------- > > > > It fails on nametowidget() function. My intention is to use 'in_' to > > change the parent of 'start' widget from the default Tk object to 'panel', > > but failed with KeyError: 'start'. > > > > below is part of the snapshot in pdb, > > ... > >> d:\works\python\test.py(11)__init__() > > -> start = tk.Button(text='Start', name='start') > > (Pdb) !panel.winfo_parent() > > '.demo' > > (Pdb) next > >> d:\works\python\test.py(12)__init__() > > -> start.grid(in_=panel) > > (Pdb) !start.winfo_parent() > > '.' > > (Pdb) next > >> d:\works\python\test.py(14)__init__() > > -> btn = self.nametowidget('panel.start') > > (Pdb) !start.winfo_parent() > > '.' > > > > --Jach > > I think that the `in_` argument is used correctly. It's just that your > expectation that the name is changed to reflect the layout hierarchy is > wrong. > > To manipulate the start button you can use the `start` variable directly: > > start.config(state='disabled') > > To find all slaves of the panel use > > panel.grid_slaves() > > $ cat grid_in.py > import tkinter as tk > > class Demo(tk.Frame): > def __init__(self): > tk.Frame.__init__(self, name='demo') > self.pack() > > self.panel = panel = tk.Frame(self, name='panel') > panel.pack() > > start = tk.Button(text='Start', name='start') > start.grid(in_=panel) > > for btn in panel.grid_slaves(): > print("disabling", btn._w) > btn.config(state='disabled') > > Demo() #.mainloop() > $ python3 grid_in.py > disabling .start > $ Oh, I was misunderstanding the purpose of 'in_'. No wonder it's showing up in the .grid() manager:-) Thank you. --Jach From piet-l at vanoostrum.org Tue Sep 10 07:40:42 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Tue, 10 Sep 2019 13:40:42 +0200 Subject: numpy array - convert hex to int References: <0b3c5c58-bdb6-4f36-b91c-5e3a667a7833@googlegroups.com> <150b969f-cc4e-474e-bffe-91b27fa30d63@googlegroups.com> Message-ID: Sharan Basappa writes: > On Sunday, 8 September 2019 11:16:52 UTC-4, Luciano Ramalho wrote: >> >>> int('C0FFEE', 16) >> 12648430 >> >> There you go! >> >> On Sun, Sep 8, 2019 at 12:02 PM Sharan Basappa wrote: >> > >> > I have a numpy array that has data in the form of hex. >> > I would like to convert that into decimal/integer. >> > Need suggestions please. >> > -- > > I am sorry. I forgot to mention that I have the data in a numpy array. > So, when I try to convert to int, I get the following error. > > sample code here > ##################### > my_data_3 = int(my_data_2) > > my_data_4 = my_data_3.astype(np.float) > ##################### > > Error here > ############### > #np.core.defchararray.replace(my_data_2,",'') > 27 > ---> 28 my_data_3 = int(my_data_2) > 29 > 30 my_data_4 = my_data_3.astype(np.float) > TypeError: only length-1 arrays can be converted to Python scalars > ######################### >>> my_data_2 = numpy.array(['0a', '2f', '38', 'ff']) >>> >>> a_toint = np.frompyfunc(lambda x: int(x, 16), 1, 1) >>> my_data_3 = a_toint(my_data_2) >>> my_data_3 array([10, 47, 56, 255], dtype=object) -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From piet-l at vanoostrum.org Tue Sep 10 07:46:12 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Tue, 10 Sep 2019 13:46:12 +0200 Subject: phyton References: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> Message-ID: tim.gast at quicknet.nl writes: > Hi everybody, > > For school i need to write the right code to get the following outcome. > Can someone help me with this.... > I can't find a solution to link the word high to 1.21. > > 11 print(add_vat(101, 'high')) > 12 print(add_vat(101, 'low')) > > Outcome: > > 122.21 > 110.09 > > Thanks! You could use a dictionary to connect the words to the values. As this is homework you have to do it yourself. Learn about dictionaries. Otherwise just use 'if'. -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From piet-l at vanoostrum.org Tue Sep 10 08:00:23 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Tue, 10 Sep 2019 14:00:23 +0200 Subject: pandas loc on str lower for column comparison References: <05e75ce6-34f3-41c3-a04e-3a900b98488a@googlegroups.com> <812057cd-9f21-462c-b071-2e8f8d23bd12@googlegroups.com> <7a516845-7daf-447e-84cd-64c9bc315c9f@googlegroups.com> <32d818a9-8565-47d6-8487-1571b4048c01@googlegroups.com> <5b34ce1c-3a0d-437f-946f-0d6f442aff88@googlegroups.com> Message-ID: Sayth Renshaw writes: >> >> That actually creates another error. >> >> A value is trying to be set on a copy of a slice from a DataFrame. >> Try using .loc[row_indexer,col_indexer] = value instead >> >> See the caveats in the documentation: >> http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy >> >> So tried this >> df['c'] = df.apply(lambda df1: df1['Current >> Team'].str.lower().str.strip() == df1['New >> Team'].str.lower().str.strip(), axis=1) >> >> Based on this SO answer https://stackoverflow.com/a/46570641 >> >> Thoughts? >> >> Sayth > > This works on an individual row > df2 = df1.loc[(df1['Current Team'] == df1['New Team']),'D'] = 'Wow' > > But how do I apply it to the whole new column and return the new dataset? > > Trying to use lambda but it cannot contain assigment > df2 = df1.apply(lambda df1: [ (df1['Current Team'] == df1['New Team']) ]['D'] = 'succeed') > df2 > > Confused > > Sayth df1['Difference'] = df1['Current Team'].str.lower().str.strip() == df1['New Team'].str.lower().str.strip() works on whole columns, not only on an individual row. xls = pd.ExcelFile("Melbourne.xlsx") df = xls.parse('Sheet1', skiprows= 4) df1 = df[['UID','Name','New Leader','Current Team', 'New Team']].copy() df1['Difference'] = df1['Current Team'].str.lower().str.strip() == df1['New Team'].str.lower().str.strip() print(df1.head()) -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From tim.gast at quicknet.nl Tue Sep 10 08:04:28 2019 From: tim.gast at quicknet.nl (tim.gast at quicknet.nl) Date: Tue, 10 Sep 2019 05:04:28 -0700 (PDT) Subject: phyton In-Reply-To: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> References: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> Message-ID: <4e6170a4-0732-49ce-b1f0-e2d70ec1d22e@googlegroups.com> Op dinsdag 10 september 2019 13:03:46 UTC+2 schreef tim... at quicknet.nl: > Hi everybody, > > For school i need to write the right code to get the following outcome. > Can someone help me with this.... > I can't find a solution to link the word high to 1.21. > > 11 print(add_vat(101, 'high')) > 12 print(add_vat(101, 'low')) > > Outcome: > > 122.21 > 110.09 > > Thanks! I have tried to get it with the dictonary but it doesn't work.... You are right that it is homework and i am trying to figure it out but i cant find anything on the internet that can help me. What am i doing wrong. my_dict('high':21,'low':5) def add_vat(amount, vat_rate): berekening = amount * (1+vat_rate) return round(berekening,2) print(add_vat(101, 'high')) From joel.goldstick at gmail.com Tue Sep 10 08:19:16 2019 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 10 Sep 2019 08:19:16 -0400 Subject: phyton In-Reply-To: <4e6170a4-0732-49ce-b1f0-e2d70ec1d22e@googlegroups.com> References: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> <4e6170a4-0732-49ce-b1f0-e2d70ec1d22e@googlegroups.com> Message-ID: On Tue, Sep 10, 2019 at 8:10 AM tim.gast--- via Python-list wrote: > > Op dinsdag 10 september 2019 13:03:46 UTC+2 schreef tim... at quicknet.nl: > > Hi everybody, > > > > For school i need to write the right code to get the following outcome. > > Can someone help me with this.... > > I can't find a solution to link the word high to 1.21. > > > > 11 print(add_vat(101, 'high')) > > 12 print(add_vat(101, 'low')) > > > > Outcome: > > > > 122.21 > > 110.09 > > > > Thanks! > > I have tried to get it with the dictonary but it doesn't work.... > You are right that it is homework and i am trying to figure it out but i cant find anything on the internet that can help me. > What am i doing wrong. > > my_dict('high':21,'low':5) > > def add_vat(amount, vat_rate): > berekening = amount * (1+vat_rate) > return round(berekening,2) > > print(add_vat(101, 'high')) > -- > https://mail.python.org/mailman/listinfo/python-list These code snippets don't run Can you paste your complete code that runs, with the output you get? -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From tim.gast at quicknet.nl Tue Sep 10 08:29:09 2019 From: tim.gast at quicknet.nl (tim.gast at quicknet.nl) Date: Tue, 10 Sep 2019 05:29:09 -0700 (PDT) Subject: phyton In-Reply-To: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> References: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> Message-ID: <57885b8b-3164-4ad9-95bf-97c8e719f506@googlegroups.com> Op dinsdag 10 september 2019 13:03:46 UTC+2 schreef tim... at quicknet.nl: > Hi everybody, > > For school i need to write the right code to get the following outcome. > Can someone help me with this.... > I can't find a solution to link the word high to 1.21. > > 11 print(add_vat(101, 'high')) > 12 print(add_vat(101, 'low')) > > Outcome: > > 122.21 > 110.09 > > Thanks! my_dict('high':21,'low':5) def add_vat(amount, vat_rate): berekening = amount * (1+vat_rate) return round(berekening,2) print(add_vat(101, 'high')) outcome: File "", line 3 def add_vat(amount, vat_rate({'high':21,'low':5})): ^ SyntaxError: invalid syntax From inhahe at gmail.com Tue Sep 10 08:31:50 2019 From: inhahe at gmail.com (inhahe) Date: Tue, 10 Sep 2019 08:31:50 -0400 Subject: phyton In-Reply-To: <4e6170a4-0732-49ce-b1f0-e2d70ec1d22e@googlegroups.com> References: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> <4e6170a4-0732-49ce-b1f0-e2d70ec1d22e@googlegroups.com> Message-ID: On Tue, Sep 10, 2019 at 8:10 AM tim.gast--- via Python-list < python-list at python.org> wrote: > Op dinsdag 10 september 2019 13:03:46 UTC+2 schreef tim... at quicknet.nl: > > Hi everybody, > > > > For school i need to write the right code to get the following outcome. > > Can someone help me with this.... > > I can't find a solution to link the word high to 1.21. > > > > 11 print(add_vat(101, 'high')) > > 12 print(add_vat(101, 'low')) > > > > Outcome: > > > > 122.21 > > 110.09 > > > > Thanks! > > I have tried to get it with the dictonary but it doesn't work.... > You are right that it is homework and i am trying to figure it out but i > cant find anything on the internet that can help me. > What am i doing wrong. > > my_dict('high':21,'low':5) > > def add_vat(amount, vat_rate): > berekening = amount * (1+vat_rate) > return round(berekening,2) > > print(add_vat(101, 'high')) > -- > https://mail.python.org/mailman/listinfo/python-list I'm not sure exactly what you're trying to do (since 101+21 would give you 122, not 122.21), but I think your problem is that you're not actually using the dictionary in your add_vat function. You're just adding 1 to vat_rate which is 'high'. Adding an integer to a string should be an error. Instead of "vat_rate" in "1+vat_rate" you need to do a dictionary lookup. From inhahe at gmail.com Tue Sep 10 08:35:11 2019 From: inhahe at gmail.com (inhahe) Date: Tue, 10 Sep 2019 08:35:11 -0400 Subject: phyton In-Reply-To: References: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> <4e6170a4-0732-49ce-b1f0-e2d70ec1d22e@googlegroups.com> Message-ID: On Tue, Sep 10, 2019 at 8:31 AM inhahe wrote: > > > On Tue, Sep 10, 2019 at 8:10 AM tim.gast--- via Python-list < > python-list at python.org> wrote: > >> Op dinsdag 10 september 2019 13:03:46 UTC+2 schreef tim... at quicknet.nl: >> > Hi everybody, >> > >> > For school i need to write the right code to get the following outcome. >> > Can someone help me with this.... >> > I can't find a solution to link the word high to 1.21. >> > >> > 11 print(add_vat(101, 'high')) >> > 12 print(add_vat(101, 'low')) >> > >> > Outcome: >> > >> > 122.21 >> > 110.09 >> > >> > Thanks! >> >> I have tried to get it with the dictonary but it doesn't work.... >> You are right that it is homework and i am trying to figure it out but i >> cant find anything on the internet that can help me. >> What am i doing wrong. >> >> my_dict('high':21,'low':5) >> >> def add_vat(amount, vat_rate): >> berekening = amount * (1+vat_rate) >> return round(berekening,2) >> >> print(add_vat(101, 'high')) >> -- >> https://mail.python.org/mailman/listinfo/python-list > > > I'm not sure exactly what you're trying to do (since 101+21 would give you > 122, not 122.21), but I think your problem is that you're not actually > using the dictionary in your add_vat function. You're just adding 1 to > vat_rate which is 'high'. Adding an integer to a string should be an error. > Instead of "vat_rate" in "1+vat_rate" you need to do a dictionary lookup. > Oh, and my_dict('high':21, 'low':5) isn't actually how you set dictionary values. Unless you have some weird function my_dict in your code that does that that you're not listing From pankaj.jangid at gmail.com Tue Sep 10 08:36:00 2019 From: pankaj.jangid at gmail.com (Pankaj Jangid) Date: Tue, 10 Sep 2019 18:06:00 +0530 Subject: phyton References: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> Message-ID: tim.gast at quicknet.nl writes: > For school i need to write the right code to get the following outcome. > Can someone help me with this.... > I can't find a solution to link the word high to 1.21. > > 11 print(add_vat(101, 'high')) > 12 print(add_vat(101, 'low')) > > Outcome: > > 122.21 > 110.09 > You can do something like this ;-) import math def add_vat(a, b): return math.ceil(100*(a * 0.57 + sum([ord(c) for c in list(b)]) * 0.15538))/100 print(add_vat(101, 'high')) print(add_vat(101, 'low')) -- Pankaj Jangid From tim.gast at quicknet.nl Tue Sep 10 08:40:26 2019 From: tim.gast at quicknet.nl (tim.gast at quicknet.nl) Date: Tue, 10 Sep 2019 05:40:26 -0700 (PDT) Subject: phyton In-Reply-To: References: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> Message-ID: <3cfa123c-10b9-49df-bfa0-36766ab1ae28@googlegroups.com> Op dinsdag 10 september 2019 14:36:13 UTC+2 schreef Pankaj Jangid: > tim.gast at quicknet.nl writes: > > > For school i need to write the right code to get the following outcome. > > Can someone help me with this.... > > I can't find a solution to link the word high to 1.21. > > > > 11 print(add_vat(101, 'high')) > > 12 print(add_vat(101, 'low')) > > > > Outcome: > > > > 122.21 > > 110.09 > > > You can do something like this ;-) > > > import math > > def add_vat(a, b): > return math.ceil(100*(a * 0.57 + sum([ord(c) for c in list(b)]) * 0.15538))/100 > > print(add_vat(101, 'high')) > print(add_vat(101, 'low')) > > -- > Pankaj Jangid Wow No idea what you did there :O Thanks but I think I need to use the dictionary.... From inhahe at gmail.com Tue Sep 10 08:45:32 2019 From: inhahe at gmail.com (inhahe) Date: Tue, 10 Sep 2019 08:45:32 -0400 Subject: phyton In-Reply-To: References: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> Message-ID: On Tue, Sep 10, 2019 at 8:41 AM Pankaj Jangid wrote: > tim.gast at quicknet.nl writes: > > > For school i need to write the right code to get the following outcome. > > Can someone help me with this.... > > I can't find a solution to link the word high to 1.21. > > > > 11 print(add_vat(101, 'high')) > > 12 print(add_vat(101, 'low')) > > > > Outcome: > > > > 122.21 > > 110.09 > > > You can do something like this ;-) > > > import math > > def add_vat(a, b): > return math.ceil(100*(a * 0.57 + sum([ord(c) for c in list(b)]) * > 0.15538))/100 > > print(add_vat(101, 'high')) > print(add_vat(101, 'low')) > > -- > Pankaj Jangid > -- > https://mail.python.org/mailman/listinfo/python-list BAD BAD BAD don't do Pankaj's suggestion, tim, it's DEVILSPEAK From tim.gast at quicknet.nl Tue Sep 10 08:49:25 2019 From: tim.gast at quicknet.nl (tim.gast at quicknet.nl) Date: Tue, 10 Sep 2019 05:49:25 -0700 (PDT) Subject: phyton In-Reply-To: References: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> Message-ID: <5ffc044f-bc1f-400c-bbfd-37d69635ec71@googlegroups.com> Op dinsdag 10 september 2019 14:45:58 UTC+2 schreef inhahe: > On Tue, Sep 10, 2019 at 8:41 AM Pankaj Jangid > wrote: > > > tim.gast at quicknet.nl writes: > > > > > For school i need to write the right code to get the following outcome. > > > Can someone help me with this.... > > > I can't find a solution to link the word high to 1.21. > > > > > > 11 print(add_vat(101, 'high')) > > > 12 print(add_vat(101, 'low')) > > > > > > Outcome: > > > > > > 122.21 > > > 110.09 > > > > > You can do something like this ;-) > > > > > > import math > > > > def add_vat(a, b): > > return math.ceil(100*(a * 0.57 + sum([ord(c) for c in list(b)]) * > > 0.15538))/100 > > > > print(add_vat(101, 'high')) > > print(add_vat(101, 'low')) > > > > -- > > Pankaj Jangid > > -- > > https://mail.python.org/mailman/listinfo/python-list > > > BAD BAD BAD don't do Pankaj's suggestion, tim, it's DEVILSPEAK oeii thanks... What will happen when i copy it? From frank at chagford.com Tue Sep 10 08:50:23 2019 From: frank at chagford.com (Frank Millman) Date: Tue, 10 Sep 2019 14:50:23 +0200 Subject: phyton In-Reply-To: <57885b8b-3164-4ad9-95bf-97c8e719f506@googlegroups.com> References: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> <57885b8b-3164-4ad9-95bf-97c8e719f506@googlegroups.com> Message-ID: On 2019-09-10 2:29 PM, tim.gast--- via Python-list wrote: > Op dinsdag 10 september 2019 13:03:46 UTC+2 schreef tim... at quicknet.nl: >> Hi everybody, >> >> For school i need to write the right code to get the following outcome. >> Can someone help me with this.... >> I can't find a solution to link the word high to 1.21. >> >> 11 print(add_vat(101, 'high')) >> 12 print(add_vat(101, 'low')) >> >> Outcome: >> >> 122.21 >> 110.09 >> >> Thanks! > > my_dict('high':21,'low':5) > > def add_vat(amount, vat_rate): > berekening = amount * (1+vat_rate) > return round(berekening,2) > > print(add_vat(101, 'high')) > > outcome: > File "", line 3 > def add_vat(amount, vat_rate({'high':21,'low':5})): > ^ > SyntaxError: invalid syntax > First point - 122.21 is 101 + 21%, so 'high' could be 21, but 110.09 is 101 + 9%, so I think 'low' should be 9. Second point, I sympathise, but you do need to understand the basics of dictionaries before you can start using them. Check the tutorial, and experiment at the ipython prompt. I am using the normal python interpreter here, but the principle is the same - >>> my_dict = dict() >>> my_dict {} >>> my_dict = {} # this does the same, but is shorter >>> my_dict {} >>> my_dict['high'] = 21 >>> my_dict {'high': 21} >>> Try that, and report back with any questions Frank Millman From frank at chagford.com Tue Sep 10 08:50:23 2019 From: frank at chagford.com (Frank Millman) Date: Tue, 10 Sep 2019 14:50:23 +0200 Subject: phyton In-Reply-To: <57885b8b-3164-4ad9-95bf-97c8e719f506@googlegroups.com> References: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> <57885b8b-3164-4ad9-95bf-97c8e719f506@googlegroups.com> Message-ID: On 2019-09-10 2:29 PM, tim.gast--- via Python-list wrote: > Op dinsdag 10 september 2019 13:03:46 UTC+2 schreef tim... at quicknet.nl: >> Hi everybody, >> >> For school i need to write the right code to get the following outcome. >> Can someone help me with this.... >> I can't find a solution to link the word high to 1.21. >> >> 11 print(add_vat(101, 'high')) >> 12 print(add_vat(101, 'low')) >> >> Outcome: >> >> 122.21 >> 110.09 >> >> Thanks! > > my_dict('high':21,'low':5) > > def add_vat(amount, vat_rate): > berekening = amount * (1+vat_rate) > return round(berekening,2) > > print(add_vat(101, 'high')) > > outcome: > File "", line 3 > def add_vat(amount, vat_rate({'high':21,'low':5})): > ^ > SyntaxError: invalid syntax > First point - 122.21 is 101 + 21%, so 'high' could be 21, but 110.09 is 101 + 9%, so I think 'low' should be 9. Second point, I sympathise, but you do need to understand the basics of dictionaries before you can start using them. Check the tutorial, and experiment at the ipython prompt. I am using the normal python interpreter here, but the principle is the same - >>> my_dict = dict() >>> my_dict {} >>> my_dict = {} # this does the same, but is shorter >>> my_dict {} >>> my_dict['high'] = 21 >>> my_dict {'high': 21} >>> Try that, and report back with any questions Frank Millman From lukasz at langa.pl Tue Sep 10 10:23:03 2019 From: lukasz at langa.pl (=?utf-8?Q?=C5=81ukasz_Langa?=) Date: Tue, 10 Sep 2019 15:23:03 +0100 Subject: Python library maintainers: PEP 602 needs your feedback Message-ID: <8515EBF9-B0A3-40A0-B578-178566B81C92@langa.pl> Hey there, Python library maintainers! Python is looking into increasing its release cadence. You can read the current proposal here: https://python.org/dev/peps/pep-0602/ More importantly, we need your input. Read the PEP and please let us know what you think via this form: https://forms.gle/fA1FDhtna9sFnckk9 Free-form discussion is happening here: https://discuss.python.org/t/pep-602-annual-release-cycle-for-python/2296/ Thanks in advance, ? Python 3.8 and 3.9 Release Manager -------------- 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 ekopalypse at gmail.com Tue Sep 10 11:29:04 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Tue, 10 Sep 2019 08:29:04 -0700 (PDT) Subject: Metaclasses and classproperties Message-ID: I'm fairly new when it comes to metaclass programming and therefore the question whether the following makes sense or not. The goal is to have two additional class properties which return a dictionary name:class_attribute and value:class_attribute for an IntEnum class and after reading about it I came to the conclusion that the following code might do what I want, and it does do the job BUT does it make sense also? Meaning, my ultimate goal would be to be able to decide myself if this is a good or bad idea doing this, so what do I need to read/understand in order to achieve such a goal. from enum import EnumMeta, IntEnum class EnhancedIntEnum(EnumMeta): @property def names(cls): return {k: v for k, v in cls.__members__.items()} @property def values(cls): return {v.value: v for k, v in cls.__members__.items()} class Ordinal(IntEnum, metaclass=EnhancedIntEnum): NORTH = 0 SOUTH = 1 EAST = 2 WEST = 3 print(Ordinal.names) print(Ordinal.values) Thank you Eren From tim.gast at quicknet.nl Tue Sep 10 07:03:33 2019 From: tim.gast at quicknet.nl (tim.gast at quicknet.nl) Date: Tue, 10 Sep 2019 04:03:33 -0700 (PDT) Subject: phyton Message-ID: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> Hi everybody, For school i need to write the right code to get the following outcome. Can someone help me with this.... I can't find a solution to link the word high to 1.21. 11 print(add_vat(101, 'high')) 12 print(add_vat(101, 'low')) Outcome: 122.21 110.09 Thanks! From tim.gast at quicknet.nl Tue Sep 10 08:38:53 2019 From: tim.gast at quicknet.nl (tim.gast at quicknet.nl) Date: Tue, 10 Sep 2019 05:38:53 -0700 (PDT) Subject: phyton In-Reply-To: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> References: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> Message-ID: Op dinsdag 10 september 2019 13:03:46 UTC+2 schreef tim... at quicknet.nl: > Hi everybody, > > For school i need to write the right code to get the following outcome. > Can someone help me with this.... > I can't find a solution to link the word high to 1.21. > > 11 print(add_vat(101, 'high')) > 12 print(add_vat(101, 'low')) > > Outcome: > > 122.21 > 110.09 > > Thanks! Hi inhahe, Yeah the 21 was just an example the calculation will come later. First i need to figure this out... Is this what you mean with adding a dictionary? berekening = amount * (1+(vat_rate={'high':21, 'low':5}) From toby at tobiah.org Tue Sep 10 12:05:16 2019 From: toby at tobiah.org (Tobiah) Date: Tue, 10 Sep 2019 09:05:16 -0700 Subject: [OT(?)] Ubuntu 18 vim now defaults to 4-space tabs References: Message-ID: > Your subject missed a critical word: vim. It's there! > Run vim. Then ':set' to see what's set different than default. Then, > if it is tabstop you want to know about, ':verbose set tabstop?' will > tell you where that setting was last altered. Nothing that seems to point to space indent: background=dark hlsearch ruler smartcase ttyfast wildmenu helplang=C. ignorecase scroll=36 smartindent ttymouse=xterm2 nowrap hidden modified showcmd nostartofline wildcharm=^Z I'll check with a vim specific group. Thanks! Toby From __peter__ at web.de Tue Sep 10 12:42:59 2019 From: __peter__ at web.de (Peter Otten) Date: Tue, 10 Sep 2019 18:42:59 +0200 Subject: Metaclasses and classproperties References: Message-ID: Eko palypse wrote: > I'm fairly new when it comes to metaclass programming and therefore the > question whether the following makes sense or not. > > The goal is to have two additional class properties which return a > dictionary name:class_attribute and value:class_attribute for an IntEnum > class and after reading about it I came to the conclusion that the > following code might do what I want, and it does do the job BUT does it > make sense also? > > Meaning, my ultimate goal would be to be able to decide myself > if this is a good or bad idea doing this, so what do I need to > read/understand in order to achieve such a goal. My objections have nothing to do with metaclass technicalities, but > from enum import EnumMeta, IntEnum > > class EnhancedIntEnum(EnumMeta): > @property > def names(cls): > return {k: v for k, v in cls.__members__.items()} this is basically a writeable copy of __members__. Using Ordinal.__members__ directly should be better in most cases, and if you really need a copy Ordinal.__members__.copy() or dict(Ordinal.__members__) is pretty clear and concise enough. If you want to convert a str use Ordinal["WEST"] rather than Ordinal.names["WEST"]. > @property > def values(cls): > return {v.value: v for k, v in cls.__members__.items()} Again, I don't see the use case; if you want to convert an int to your `Ordinal`, just use the constructor Ordinal(some_int) > class Ordinal(IntEnum, metaclass=EnhancedIntEnum): > NORTH = 0 > SOUTH = 1 > EAST = 2 > WEST = 3 > > print(Ordinal.names) > print(Ordinal.values) > > Thank you > Eren From inhahe at gmail.com Tue Sep 10 12:47:38 2019 From: inhahe at gmail.com (inhahe) Date: Tue, 10 Sep 2019 12:47:38 -0400 Subject: phyton In-Reply-To: References: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> Message-ID: On Tue, Sep 10, 2019 at 12:06 PM tim.gast--- via Python-list < python-list at python.org> wrote: > Op dinsdag 10 september 2019 13:03:46 UTC+2 schreef tim... at quicknet.nl: > > Hi everybody, > > > > For school i need to write the right code to get the following outcome. > > Can someone help me with this.... > > I can't find a solution to link the word high to 1.21. > > > > 11 print(add_vat(101, 'high')) > > 12 print(add_vat(101, 'low')) > > > > Outcome: > > > > 122.21 > > 110.09 > > > > Thanks! > > Hi inhahe, > > Yeah the 21 was just an example the calculation will come later. > First i need to figure this out... > Is this what you mean with adding a dictionary? > berekening = amount * (1+(vat_rate={'high':21, 'low':5}) > -- > https://mail.python.org/mailman/listinfo/python-list No, I mean add a dictionary lookup, meaning instead of vat_rate, use a value that you look up in the dictionary, by using the syntax for looking up values in dictionaries. What you wrote above would result in a syntax error. Hope this isn't considered doing your homework for you, but tell you how to make a dictionary and look up a value from it. #making the dictionary: primeNumberIndexes = {179:41, 181:42, 191:43, 193:44} #looking up a value in it: IndexOf179 = primeNumberIndexes[179] #IndexOf179 is now 41 Of course you can put a variable (actually a "name" in python terminology, I think) in the brackets instead of a direct value, which you'll want to do. From spencerdu at hotmail.co.uk Tue Sep 10 12:59:27 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Tue, 10 Sep 2019 09:59:27 -0700 (PDT) Subject: Why is "Subscribing to topic topic/test" printed in console log before connect message? Message-ID: <58e428d5-ff9d-4c8b-91d1-eae1fe47bf6c@googlegroups.com> Hi I have code for publish and subscribe over mqtt. In the console log I have "Subscribing to topic topic/test" printed before connect message why is this? I want to this to be printed after the connect message. How do I fix this problem. Please run gui.py to test. gui.py import paho.mqtt.client as mqtt from mqtt import * import json import time client = mqtt.Client() client.connect("broker.hivemq.com",1883,60) client.on_connect = on_connect client.on_message = on_message client.loop_start() print("Subscribing to topic", "topic/test") client.subscribe("topic/test") client.publish("topic/test", "Hello world!") time.sleep(1) client.loop_stop() mqtt.py import paho.mqtt.client as mqtt def on_connect(client, userdata, flags, rc): print("Connected with result code "+str(rc)) def on_message(client, userdata, msg): if msg.payload.decode() == "Hello world!": print("Yes!") Thanks Spencer From python at mrabarnett.plus.com Tue Sep 10 13:16:53 2019 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 10 Sep 2019 18:16:53 +0100 Subject: Why is "Subscribing to topic topic/test" printed in console log before connect message? In-Reply-To: <58e428d5-ff9d-4c8b-91d1-eae1fe47bf6c@googlegroups.com> References: <58e428d5-ff9d-4c8b-91d1-eae1fe47bf6c@googlegroups.com> Message-ID: <921fc663-1d73-a5e4-5a17-437bd4472db9@mrabarnett.plus.com> On 2019-09-10 17:59, Spencer Du wrote: > Hi > > I have code for publish and subscribe over mqtt. In the console log I have "Subscribing to topic topic/test" printed before connect message why is this? I want to this to be printed after the connect message. How do I fix this problem. Please run gui.py to test. > > gui.py > > import paho.mqtt.client as mqtt > from mqtt import * > import json > import time > > client = mqtt.Client() > client.connect("broker.hivemq.com",1883,60) > client.on_connect = on_connect > client.on_message = on_message > > client.loop_start() > print("Subscribing to topic", "topic/test") > client.subscribe("topic/test") > client.publish("topic/test", "Hello world!") > time.sleep(1) > client.loop_stop() > > mqtt.py > > import paho.mqtt.client as mqtt > > def on_connect(client, userdata, flags, rc): > print("Connected with result code "+str(rc)) > > def on_message(client, userdata, msg): > if msg.payload.decode() == "Hello world!": > print("Yes!") > It might just be a timing issue. I note, however, that you're attaching the 'on_connect' and 'on_message' callbacks _after_ asking it to connect, which seems like a strange thing to do. If you want it to callback when it connects, it makes more sense to attach the callbacks _before_ asking it to connect. From rosuav at gmail.com Tue Sep 10 14:07:41 2019 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 11 Sep 2019 04:07:41 +1000 Subject: phyton In-Reply-To: <5ffc044f-bc1f-400c-bbfd-37d69635ec71@googlegroups.com> References: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> <5ffc044f-bc1f-400c-bbfd-37d69635ec71@googlegroups.com> Message-ID: On Tue, Sep 10, 2019 at 10:54 PM tim.gast--- via Python-list wrote: > > Op dinsdag 10 september 2019 14:45:58 UTC+2 schreef inhahe: > > On Tue, Sep 10, 2019 at 8:41 AM Pankaj Jangid > > wrote: > > > > > tim.gast at quicknet.nl writes: > > > > > > > For school i need to write the right code to get the following outcome. > > > > Can someone help me with this.... > > > > I can't find a solution to link the word high to 1.21. > > > > > > > > 11 print(add_vat(101, 'high')) > > > > 12 print(add_vat(101, 'low')) > > > > > > > > Outcome: > > > > > > > > 122.21 > > > > 110.09 > > > > > > > You can do something like this ;-) > > > > > > > > > import math > > > > > > def add_vat(a, b): > > > return math.ceil(100*(a * 0.57 + sum([ord(c) for c in list(b)]) * > > > 0.15538))/100 > > > > > > print(add_vat(101, 'high')) > > > print(add_vat(101, 'low')) > > > > > > -- > > > Pankaj Jangid > > > -- > > > https://mail.python.org/mailman/listinfo/python-list > > > > > > BAD BAD BAD don't do Pankaj's suggestion, tim, it's DEVILSPEAK > > oeii thanks... > What will happen when i copy it? It won't break your computer or anything. But if you submit this to your teacher, s/he will know for sure that you copied and pasted code from the internet without understanding it :) It achieves its result in one of the most ridiculous ways possible in a single line of code. (And having said that, I know that someone's going to post an even more ridiculous way, just to prove it's possible.) ChrisA From rosuav at gmail.com Tue Sep 10 14:10:33 2019 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 11 Sep 2019 04:10:33 +1000 Subject: Why is "Subscribing to topic topic/test" printed in console log before connect message? In-Reply-To: <58e428d5-ff9d-4c8b-91d1-eae1fe47bf6c@googlegroups.com> References: <58e428d5-ff9d-4c8b-91d1-eae1fe47bf6c@googlegroups.com> Message-ID: On Wed, Sep 11, 2019 at 3:01 AM Spencer Du wrote: > > Hi > > I have code for publish and subscribe over mqtt. In the console log I have "Subscribing to topic topic/test" printed before connect message why is this? I want to this to be printed after the connect message. How do I fix this problem. Please run gui.py to test. > > client = mqtt.Client() > client.connect("broker.hivemq.com",1883,60) > client.on_connect = on_connect > > client.loop_start() > print("Subscribing to topic", "topic/test") > client.subscribe("topic/test") > client.publish("topic/test", "Hello world!") > > def on_connect(client, userdata, flags, rc): > print("Connected with result code "+str(rc)) > Your code is asynchronous. I would strongly recommend moving your subscribe action into your on_connect, such that it happens ONLY once you are successfully connected. ChrisA From pankaj.jangid at gmail.com Tue Sep 10 14:10:21 2019 From: pankaj.jangid at gmail.com (Pankaj Jangid) Date: Tue, 10 Sep 2019 23:40:21 +0530 Subject: phyton References: <401a5a7e-66cf-497f-a9fd-ac77bea00a02@googlegroups.com> Message-ID: inhahe writes: > On Tue, Sep 10, 2019 at 8:41 AM Pankaj Jangid > wrote: > >> You can do something like this ;-) >> >> >> import math >> >> def add_vat(a, b): >> return math.ceil(100*(a * 0.57 + sum([ord(c) for c in list(b)]) * >> 0.15538))/100 >> >> print(add_vat(101, 'high')) >> print(add_vat(101, 'low')) >> >> -- >> Pankaj Jangid >> -- >> https://mail.python.org/mailman/listinfo/python-list > > > BAD BAD BAD don't do Pankaj's suggestion, tim, it's DEVILSPEAK :-( what did I do? This is giving correct ans. hahahaha -- Pankaj Jangid From ekopalypse at gmail.com Tue Sep 10 14:36:55 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Tue, 10 Sep 2019 11:36:55 -0700 (PDT) Subject: Metaclasses and classproperties In-Reply-To: References: Message-ID: Thank you for your thoughts. I'm trying to migrating en existing python2 api, which has been build using boost::python with pure python3 code. All of the py2 classes do have these additional properties names and values. You are right, using dict(Ordinal.__members__) for names could have been used as well but I thought, if someone else wants to change something it might be easier to understand if both properties look the same, maybe a wrong assumption. (something I seem to do often :-) Eren From * at eli.users.panix.com Tue Sep 10 14:46:05 2019 From: * at eli.users.panix.com (Eli the Bearded) Date: Tue, 10 Sep 2019 18:46:05 +0000 (UTC) Subject: [OT(?)] Ubuntu 18 vim now defaults to 4-space tabs References: Message-ID: In comp.lang.python, Tobiah wrote: >> Your subject missed a critical word: vim. > It's there! I added it. > > Run vim. Then ':set' to see what's set different than default. Then, > > if it is tabstop you want to know about, ':verbose set tabstop?' will > > tell you where that setting was last altered. > > Nothing that seems to point to space indent: > > > background=dark hlsearch ruler smartcase ttyfast > wildmenu > helplang=C. ignorecase scroll=36 smartindent > ttymouse=xterm2 nowrap > hidden modified showcmd nostartofline wildcharm=^Z Since it does not appear to have "filetype=python" in there, maybe I should have specified "Run vim with a .py filename". I tried vim on a new account on a NetBSD machine and saw much different settings today. :set --- Options --- autoindent langnoremap suffixesadd=.py comments=b:#,fb:- nolangremap syntax=python display=truncate nrformats=bin,hex ttimeout expandtab ruler ttimeoutlen=100 filetype=python scroll=11 ttyfast helplang=en scrolloff=5 ttymouse=xterm history=200 shiftwidth=4 wildignore=*.pyc incsearch showcmd wildmenu keywordprg=pydoc softtabstop=4 [... long options omitted ...] :verbose set shiftwidth? softtabstop? expandtab? shiftwidth Last set from /[...]/vim/vim81/ftplugin/python.vim line 118 softtabstop=4 Last set from /[...]/vim/vim81/ftplugin/python.vim line 118 expandtab Last set from /[...]/vim/vim81/ftplugin/python.vim line 118 Looks like the culprit there. > I'll check with a vim specific group. Thanks! I see you posted to the vim-users list. For those following along, they suggested it's newer vim "no .vimrc file" defaults kicking in. That's something that I usually manage to avoid by having a vimrc which is possibly why I was unable to duplicate it on my regular accounts. Elijah ------ really dislikes vim's mouse handling From billsix at gmail.com Tue Sep 10 13:18:36 2019 From: billsix at gmail.com (Bill Six) Date: Tue, 10 Sep 2019 13:18:36 -0400 Subject: Announcing Methodfinder Message-ID: Sometimes you know the inputs and outputs for a procedure, but you don't remember the name. methodfinder.find tries to find the name. https://github.com/billsix/methodfinder >>> import methodfinder >>> methodfinder.find([]) == 0 len([]) sum([]) >>> methodfinder.find([]) == False any([]) bool([]) callable([]) >>> methodfinder.find(3) == "3" ascii(3) format(3) repr(3) str(3) >>> methodfinder.find([1,2,6,7], 6) == True 6 in [1, 2, 6, 7] [1, 2, 6, 7].__contains__(6) >>> methodfinder.find(" ",["foo", "bar"]) == "foo bar" ' '.join(['foo', 'bar']) >>> methodfinder.find([1,2,3]) == 6 sum([1, 2, 3]) >>> methodfinder.find([1,2,3]) == 7 >>> methodfinder.find('1 + 1') == 2 eval('1 + 1') >>> methodfinder.find(0) == 1 0.denominator math.factorial(0) >>> methodfinder.find(0.0) == 1.0 math.cos(0.0) math.cosh(0.0) math.erfc(0.0) math.exp(0.0) >>> methodfinder.find([1,2]) == [[1,2],[2,1]] itertools.permutations([1, 2]) >>> methodfinder.find([1,2], [3,4]) == [[1,3],[2,4]] itertools.zip_longest([1, 2], [3, 4]) zip([1, 2], [3, 4]) >>> methodfinder.find([1,2], lambda x, y: x + y) == 3 functools.reduce( at 0x7efca8f8f4d0>, [1, 2]) >>> methodfinder.find(-1,3) == 2 -1%3 -1+3 3+-1 >>> methodfinder.find(3,2) == 1.5 3/2 >>> methodfinder.find(-1) == 1 -(-1) -1.bit_length() -1.denominator abs(-1) >>> methodfinder.find(1,2) == 3 1+2 1^2 1|2 2+1 2^1 2|1 >>> methodfinder.find(1,1) == 1 1&1 1**1 1*1 1.__class__(1) 1.denominator 1.numerator 1.real 1//1 1|1 math.gcd(1, 1) max(1, 1) min(1, 1) pow(1, 1) round(1, 1) >>> methodfinder.find([1,2], '__iter__') == True hasattr([1, 2], '__iter__') Bill Six From toby at tobiah.org Tue Sep 10 17:27:42 2019 From: toby at tobiah.org (Tobiah) Date: Tue, 10 Sep 2019 14:27:42 -0700 Subject: [OT(?)] Ubuntu 18 vim now defaults to 4-space tabs References: Message-ID: > Since it does not appear to have "filetype=python" in there, maybe I > should have specified "Run vim with a .py filename". Yes, that was a bit that took me a while to figure out! So I used your trick and did: :verbose set shiftwidth? and it revealed: cd /usr/share/vim/vim80/ftplugin And in that file I fount the clause: if !exists("g:python_recommended_style") || g:python_recommended_style != 0 " As suggested by PEP8. setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8 endif So there it is. I put let g:python_recommended_style = 0 in my ~/.vimrc and my problem was elegantly solved. I continued here with the answer so that those that find my original post by Googling the same question would not be left hanging. Tobiah From flebber.crue at gmail.com Wed Sep 11 06:11:07 2019 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Wed, 11 Sep 2019 03:11:07 -0700 (PDT) Subject: Get Count of function arguments passed in Message-ID: <32c4a990-c54b-4f56-8a4e-fb3390a4c6a2@googlegroups.com> Hi I want to allow as many lists as needed to be passed into a function. But how can I determine how many lists have been passed in? I expected this to return 3 but it only returned 1. matrix1 = [[1, -2], [-3, 4],] matrix2 = [[2, -1], [0, -1]] matrix3 = [[2, -1], [0, -1]] # print(add(matrix1, matrix2)) def add(*matrix): print(len(locals())) add(matrix1,matrix2,matrix3) Cheers Sayth From flebber.crue at gmail.com Wed Sep 11 06:25:17 2019 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Wed, 11 Sep 2019 03:25:17 -0700 (PDT) Subject: Get Count of function arguments passed in In-Reply-To: <32c4a990-c54b-4f56-8a4e-fb3390a4c6a2@googlegroups.com> References: <32c4a990-c54b-4f56-8a4e-fb3390a4c6a2@googlegroups.com> Message-ID: <8d78140a-982f-454c-8792-de9174881c44@googlegroups.com> On Wednesday, 11 September 2019 20:11:21 UTC+10, Sayth Renshaw wrote: > Hi > > I want to allow as many lists as needed to be passed into a function. > But how can I determine how many lists have been passed in? > > I expected this to return 3 but it only returned 1. > > matrix1 = [[1, -2], [-3, 4],] > matrix2 = [[2, -1], [0, -1]] > matrix3 = [[2, -1], [0, -1]] > # print(add(matrix1, matrix2)) > > def add(*matrix): > print(len(locals())) > > add(matrix1,matrix2,matrix3) > > Cheers > > Sayth Tried creating a list of the arguments, however I am creating too many positional arguments. matrix1 = [[1, -2], [-3, 4],] matrix2 = [[2, -1], [0, -1]] matrix3 = [[2, -1], [0, -1]] matrix = [] def add_many(a = list(*matrix)): for num in a: for i in range(len(matrix[num])): for j in range(len(matrix[num])): print(matrix[num][i][j] + matrix[num][i][j]) add_many(matrix1,matrix2) From flebber.crue at gmail.com Wed Sep 11 06:38:01 2019 From: flebber.crue at gmail.com (Sayth Renshaw) Date: Wed, 11 Sep 2019 03:38:01 -0700 (PDT) Subject: Get Count of function arguments passed in In-Reply-To: <8d78140a-982f-454c-8792-de9174881c44@googlegroups.com> References: <32c4a990-c54b-4f56-8a4e-fb3390a4c6a2@googlegroups.com> <8d78140a-982f-454c-8792-de9174881c44@googlegroups.com> Message-ID: On Wednesday, 11 September 2019 20:25:32 UTC+10, Sayth Renshaw wrote: > On Wednesday, 11 September 2019 20:11:21 UTC+10, Sayth Renshaw wrote: > > Hi > > > > I want to allow as many lists as needed to be passed into a function. > > But how can I determine how many lists have been passed in? > > > > I expected this to return 3 but it only returned 1. > > > > matrix1 = [[1, -2], [-3, 4],] > > matrix2 = [[2, -1], [0, -1]] > > matrix3 = [[2, -1], [0, -1]] > > # print(add(matrix1, matrix2)) > > > > def add(*matrix): > > print(len(locals())) > > > > add(matrix1,matrix2,matrix3) > > > > Cheers > > > > Sayth > > Tried creating a list of the arguments, however I am creating too many positional arguments. > > matrix1 = [[1, -2], [-3, 4],] > matrix2 = [[2, -1], [0, -1]] > matrix3 = [[2, -1], [0, -1]] > > matrix = [] > def add_many(a = list(*matrix)): > for num in a: > for i in range(len(matrix[num])): > for j in range(len(matrix[num])): > print(matrix[num][i][j] + matrix[num][i][j]) > > add_many(matrix1,matrix2) Last failure for the moment matrix1 = [[1, -2], [-3, 4],] matrix2 = [[2, -1], [0, -1]] matrix3 = [[2, -1], [0, -1]] matrix = [] def add_many(*matrix): for num in range(add_many().func_code.co_argcount): for i in range(len(matrix[num])): for j in range(len(matrix[num])): print(matrix[i][j] + matrix[i][j]) add_many(matrix1,matrix2) Sayth From none at gmail.com Wed Sep 11 07:12:55 2019 From: none at gmail.com (ast) Date: Wed, 11 Sep 2019 13:12:55 +0200 Subject: Get Count of function arguments passed in In-Reply-To: <32c4a990-c54b-4f56-8a4e-fb3390a4c6a2@googlegroups.com> References: <32c4a990-c54b-4f56-8a4e-fb3390a4c6a2@googlegroups.com> Message-ID: <5d78d6b9$0$3524$426a74cc@news.free.fr> Le 11/09/2019 ? 12:11, Sayth Renshaw a ?crit?: > Hi > > I want to allow as many lists as needed to be passed into a function. > But how can I determine how many lists have been passed in? > > I expected this to return 3 but it only returned 1. > > matrix1 = [[1, -2], [-3, 4],] > matrix2 = [[2, -1], [0, -1]] > matrix3 = [[2, -1], [0, -1]] > # print(add(matrix1, matrix2)) > > def add(*matrix): > print(len(locals())) > > add(matrix1,matrix2,matrix3) > > Cheers > > Sayth > It returns 1 because there is only 1 local variable inside function add. It's a list matrix which contains the 3 matrix If you want the number of arguments passed, then just call: len(matrix) def add(*matrix): print(len(matrix)) From david at lowryduda.com Wed Sep 11 12:19:30 2019 From: david at lowryduda.com (David Lowry-Duda) Date: Wed, 11 Sep 2019 12:19:30 -0400 Subject: Get Count of function arguments passed in In-Reply-To: <32c4a990-c54b-4f56-8a4e-fb3390a4c6a2@googlegroups.com> References: <32c4a990-c54b-4f56-8a4e-fb3390a4c6a2@googlegroups.com> Message-ID: <20190911161930.GA20240@mail.lowryduda.com> > I expected this to return 3 but it only returned 1. > > matrix1 = [[1, -2], [-3, 4],] > matrix2 = [[2, -1], [0, -1]] > matrix3 = [[2, -1], [0, -1]] > > def add(*matrix): > print(len(locals())) > > print(add(matrix1, matrix2)) In this case, locals will be a dictionary with exactly one key. The key will be "matrix", and its value will be a tuple of the two matrices matrix1 and matrix2. One solution (the wrong solution) to your problem would be to instead have # Don't do this --- this is to illustrate what locals is doing def add(*matrix): print(len(locals()['matrix'])) Now let's get to the right way to do this. Python stores the arguments specified by *matrix as a tuple of the name `matrix`. Of course, this is what locals what telling us, but there is no need to go through locals. Instead, you can do def add(*matrix): print(len(matrix)) and this will tell you how many arguments were passed in. Good luck! David Lowry-Duda From manasizpaul007 at gmail.com Wed Sep 11 12:48:37 2019 From: manasizpaul007 at gmail.com (Manasiz Paul) Date: Wed, 11 Sep 2019 22:18:37 +0530 Subject: Issue with Python installation Message-ID: Dear Sir/Madam, I have installed the latest version of Python but while running it, I am facing this issue continuously. [image: Untitled.png] Kindly let me know how to resolve this issue. From roel at roelschroeven.net Wed Sep 11 13:47:24 2019 From: roel at roelschroeven.net (Roel Schroeven) Date: Wed, 11 Sep 2019 19:47:24 +0200 Subject: Get Count of function arguments passed in In-Reply-To: <32c4a990-c54b-4f56-8a4e-fb3390a4c6a2@googlegroups.com> References: <32c4a990-c54b-4f56-8a4e-fb3390a4c6a2@googlegroups.com> Message-ID: Sayth Renshaw schreef op 11/09/2019 om 12:11: > I want to allow as many lists as needed to be passed into a function. > But how can I determine how many lists have been passed in? > > I expected this to return 3 but it only returned 1. > > matrix1 = [[1, -2], [-3, 4],] > matrix2 = [[2, -1], [0, -1]] > matrix3 = [[2, -1], [0, -1]] > # print(add(matrix1, matrix2)) > > def add(*matrix): > print(len(locals())) May I suggest renaming matrix to matrices or matrix_list or something? I find that much clearer to read and understand: singular for one object, plural for a collection of objects. def add(*matrices): print(len(matrices)) -- "Honest criticism is hard to take, particularly from a relative, a friend, an acquaintance, or a stranger." -- Franklin P. Jones Roel Schroeven From rhodri at kynesim.co.uk Wed Sep 11 14:03:26 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Wed, 11 Sep 2019 19:03:26 +0100 Subject: Issue with Python installation In-Reply-To: References: Message-ID: <01fda4bd-1c70-2a66-fc2a-01f45fc49018@kynesim.co.uk> On 11/09/2019 17:48, Manasiz Paul wrote: > Dear Sir/Madam, > > I have installed the latest version of Python but while running it, I am > facing this issue continuously. > [image: Untitled.png] I'm afraid the mailing list stripped off your attachment. Please copy and paste the error messages into the text of your email, that way we will get to see them! (The temptation to say "Use your words" is oh so strong :-) -- Rhodri James *-* Kynesim Ltd From PythonList at DancesWithMice.info Wed Sep 11 16:24:09 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Thu, 12 Sep 2019 08:24:09 +1200 Subject: WedWonder: Scripts and Modules Message-ID: In this day-and-age do you have a script in live/production-use, which is also a module? What is the justification/use case? (discounting distutils and similar installation tools, or unit testing methodology) There are over 500 questions on StackOverflow which refer to Python's if __name__ == __main__: construct. Even more if you include the idea of a main() multiple entry-point. This construct enables code to distinguish between being "run" as a "script", and being imported as a "module". (which is not in question) It intrigues me how often (per SO, quoted above) this causes anything from a pause to complete consternation amongst Python neophytes. In my own case, I accepted it as a "Pythonic" idiom, adopted its use in my scripts, and moved on. Until I adopted unittest/pytest and took-on the closer definitions of TDD, I used to use the script/module switch as a means of testing modules (see also 'good, old days', below). These days, pytest requires a different approach and splits 'test code' from (production) "delivered code". Oh yeah! However, I can't recall ever gaining similar benefit from using the 'switch' within code designed to be a "script"! Ages ago some beginner asked me the 'script/module switch' question, and I gave the standard/formulaic/Pythonic answer. However, reversing the question in my mind led me to ask (myself): how many scripts do I have (in "production use") which are ever used (also) as a module by some other script? I think the answer is/was: "none"! Accordingly, (spoiler alert: this statement may be heresy) I stopped using the "if". Back in the ?good, old days of mainframes we wrote "monolithic" programs. Then we developed "modular programming" and started to split code into functional units, ie functions, subroutines, paragraphs, procedures. Thus "code re-use" was born. (see what a new idea it is! Subroutine libraries on mag-tape, anyone?) We distinguished a subroutine or "called code" (importable module in Python) from the calling-code by calling the latter the "main-line" (nothing to do with/say no to drugs). We even developed little "stub" programs; which would exercise or test specific subroutines to ensure that they worked (wow, how new is (much of) Test-Driven Development?) So (putting nostalgia aside), these days my Python *scripts* are pure "main-line" code. Faced with the perennial "main/name" question again yesterday, led me to review the above policy/laziness, and thus this "Wednesday Wondering": - modules aside, how often do we write multiple-entry code these days, as opposed to having multiple scripts/main-lines which call re-usable functional modules, as-and-when? I still don't have a single file containing a combination script/module amongst my applications. Do you? Why/who/how/when? -- Regards, =dn From rosuav at gmail.com Wed Sep 11 16:43:34 2019 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 12 Sep 2019 06:43:34 +1000 Subject: WedWonder: Scripts and Modules In-Reply-To: References: Message-ID: On Thu, Sep 12, 2019 at 6:34 AM DL Neil via Python-list wrote: > > In this day-and-age do you have a script in live/production-use, which > is also a module? What is the justification/use case? > Yes, absolutely. It's the easiest way to share code between two scripts. Here's an example that I created recently: https://github.com/Rosuav/shed/blob/master/BL1_find_items.py https://github.com/Rosuav/shed/blob/master/BL2_find_items.py These programs do similar jobs on very different formats of file, so there's a small amount of common code and a large amount that isn't common. One of the common sections is the FunctionArg class, which ties in with argparse; it's implemented in BL1_find_items, and then imported into BL2_find_items. Of course I could break this out into its own dedicated file... but why bother? It's not significant enough to warrant its own module, and I don't see any value in an importable file of "all the stuff that I might feel like importing"; it's just these two files that will need this. Basically, the script/module distinction is a convenient way to simplify a common situation that doesn't need the overhead of anything else. If the code starts getting used in lots more places, it'll eventually get promoted to actual module. ChrisA From PythonList at DancesWithMice.info Wed Sep 11 17:32:42 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Thu, 12 Sep 2019 09:32:42 +1200 Subject: WedWonder: Scripts and Modules In-Reply-To: References: Message-ID: <456dee41-44c6-45f2-f9d5-1f59d09bc918@DancesWithMice.info> On 12/09/19 8:43 AM, Chris Angelico wrote: > On Thu, Sep 12, 2019 at 6:34 AM DL Neil via Python-list > wrote: >> >> In this day-and-age do you have a script in live/production-use, which >> is also a module? What is the justification/use case? > Yes, absolutely. It's the easiest way to share code between two > scripts. Here's an example that I created recently: > > https://github.com/Rosuav/shed/blob/master/BL1_find_items.py > https://github.com/Rosuav/shed/blob/master/BL2_find_items.py > > These programs do similar jobs on very different formats of file, so > there's a small amount of common code and a large amount that isn't > common. One of the common sections is the FunctionArg class, which > ties in with argparse; it's implemented in BL1_find_items, and then > imported into BL2_find_items. > > Of course I could break this out into its own dedicated file... but > why bother? It's not significant enough to warrant its own module, and > I don't see any value in an importable file of "all the stuff that I > might feel like importing"; it's just these two files that will need > this. > > Basically, the script/module distinction is a convenient way to > simplify a common situation that doesn't need the overhead of anything > else. If the code starts getting used in lots more places, it'll > eventually get promoted to actual module. Thanks for such a rapid response! Interestingly, we appear to have opposite approaches to this situation - as soon as I think the word "common" it morphs immediately into "module". I have also needed to scan a directory/tree recently, but took the course of refactoring a previous implementation's 'directory-walk' code into its own (generator) function, and thus the mainline of the newer application calls the utility function and chooses whether or not to deal with each 'found' file/dir in-turn. The previous script using the code was similarly refactored. (and there are no doubt ?many more scripts 'lurking' in my code-base which could be similarly 'improved' - such frequent commonality leading to my preference). My bias (I'm not criticising/complaining about/decrying the choices you have illustrated) probably comes out of "separation of concerns". An issue which has 'bitten' me, more than once... For example both BL1 and BL2 feature: def money(savefile): savefile.money[0] += 5000000 # Add more dollars - a minor issue, agreed (and 'picking on it' purely to make a point). The "s-o-c" is, that one day it will be possible to decide that the unit of addition should change, but only (remember to) amend the code in one of the two scripts! (when it comes to trusting my memory, I'm an 'old git' with history, but not an old git repo with "history"!) Which brings me back to the preference for 'encapsulation' (just to prove I can speak "OOP"), and why I would have written quite separate 'main-lines' and extracted money() (and presumably a lot more) into a module which could then be called by both. Aside1: However, you've started me thinking about a related consideration/philosophy (Danger Will Robinson!) - but please let me cogitate on that for a day or so... Aside2: Meantime, thanks for the opportunity to review your code. I was thinking of considering "data classes" (new in v3.7, IIRC) in relation to an SQL interface on today's ToDo list - constructing the application's queries and the data transfers 'in' and 'out', without the 'weight' of SQLAlchemy. Perhaps? -- Regards =dn From rosuav at gmail.com Wed Sep 11 17:50:48 2019 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 12 Sep 2019 07:50:48 +1000 Subject: WedWonder: Scripts and Modules In-Reply-To: <456dee41-44c6-45f2-f9d5-1f59d09bc918@DancesWithMice.info> References: <456dee41-44c6-45f2-f9d5-1f59d09bc918@DancesWithMice.info> Message-ID: On Thu, Sep 12, 2019 at 7:34 AM DL Neil via Python-list wrote: > > On 12/09/19 8:43 AM, Chris Angelico wrote: > > Yes, absolutely. It's the easiest way to share code between two > > scripts. Here's an example that I created recently: > > > > https://github.com/Rosuav/shed/blob/master/BL1_find_items.py > > https://github.com/Rosuav/shed/blob/master/BL2_find_items.py > > Interestingly, we appear to have opposite approaches to this situation - > as soon as I think the word "common" it morphs immediately into "module". I think we actually both agree here. The only difference is that in my view, a module that's needed by only one other script can remain as the script it is, whereas you instantly make it *exclusively* an importable module. BL1_find_items *is* a module as well as a script, and it has to be implemented with that in mind. > I have also needed to scan a directory/tree recently, but took the > course of refactoring a previous implementation's 'directory-walk' code > into its own (generator) function, and thus the mainline of the newer > application calls the utility function and chooses whether or not to > deal with each 'found' file/dir in-turn. The previous script using the > code was similarly refactored. > (and there are no doubt ?many more scripts 'lurking' in my code-base > which could be similarly 'improved' - such frequent commonality leading > to my preference). > > My bias (I'm not criticising/complaining about/decrying the choices you > have illustrated) probably comes out of "separation of concerns". An > issue which has 'bitten' me, more than once... > > For example both BL1 and BL2 feature: > > def money(savefile): savefile.money[0] += 5000000 # Add more dollars > > - a minor issue, agreed (and 'picking on it' purely to make a point). Actually no they don't, and that's part of the subtlety of working with the two distinctly different file formats. In BL2, money is an array (dollars, Eridium, Seraph tokens, etc), but in BL1, it's a single value. So the BL2 version is as you posted, but the BL1 one is slightly different: def money(savefile): savefile.money += 5000000 Both files follow the same structure of synthesizers (hence the common code in the FunctionArg class), so they will look similar, but they're not identical enough to actually share. :( > The "s-o-c" is, that one day it will be possible to decide that the unit > of addition should change, but only (remember to) amend the code in one > of the two scripts! If I cared that the amount added be the same, then I could put that into a constant, I guess; if anything, what I might do is parameterize it, but it's a fairly arbitrary figure and doesn't really matter much. > Which brings me back to the preference for 'encapsulation' (just to > prove I can speak "OOP"), and why I would have written quite separate > 'main-lines' and extracted money() (and presumably a lot more) into a > module which could then be called by both. When I started the BL1 project, I thought about sharing heaps of code with BL2, but it just didn't work out. > Aside2: > Meantime, thanks for the opportunity to review your code. I was thinking > of considering "data classes" (new in v3.7, IIRC) in relation to an SQL > interface on today's ToDo list - constructing the application's queries > and the data transfers 'in' and 'out', without the 'weight' of > SQLAlchemy. Perhaps? > Data classes are awesome. Also, this is a great demonstration of why annotations shouldn't be restricted to any particular definition of "type" - it's wonderfully elegant to define a data structure with constants for signatures, types for most values, and then use range(256) or range(65536) to mean 1-byte or 2-byte integers. Within the context of the parser/encoder here, these things ARE types, but in a general Python context, they aren't :) Incidentally, data classes can be used pre-3.7 if you pip install the backport module. So it doesn't have to be a hard requirement. ChrisA From alan at csail.mit.edu Wed Sep 11 18:37:35 2019 From: alan at csail.mit.edu (Alan Bawden) Date: 11 Sep 2019 18:37:35 -0400 Subject: WedWonder: Scripts and Modules References: Message-ID: <86sgp2o3ww.fsf@richard.bawden.org> DL Neil writes: > ... However, reversing the question in my mind led me to ask (myself): > how many scripts do I have (in "production use") which are ever used > (also) as a module by some other script? I think the answer is/was: > "none"! Accordingly, (spoiler alert: this statement may be heresy) I > stopped using the "if". I found that the problem with doing this is that `pydoc' now _runs_ my scripts, when all I wanted was a quick look at the constants and procedures defined in them. It's pretty handy sometimes to fire up `pydoc' in http server mode in a directory full of random Python code (some scripts and some modules), and use it to browse around to figure out what everything does. Scripts that aren't protected with the usual `if __name__' are more likely to crash in that situation, often crashing `pydoc' in the process. In fact, I have been known to add `if __name__' to my colleagues' Python scripts, just so that I can safely `pydoc' them. -- Alan Bawden From cs at cskk.id.au Wed Sep 11 21:53:06 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Thu, 12 Sep 2019 11:53:06 +1000 Subject: WedWonder: Scripts and Modules In-Reply-To: References: Message-ID: <20190912015306.GA16335@cskk.homeip.net> On 12Sep2019 08:24, DL Neil wrote: >In this day-and-age do you have a script in live/production-use, >which is also a module? What is the justification/use case? Many. Many many. 1: Many of my modules run their unit tests if invoked as the main programme. 2: Several modules are their own utility programme. I've got a heap of these - anything that provides useful access to something can often be usefully used from the command line. A quick grep from my bin directory shows stuff like this: [~/hg/css/bin(hg:default)]fleet*> grep ' -m cs.' * beyonwiz:exec python3 -m cs.app.beyonwiz ${1+"$@"} calibre:exec py3 -m cs.app.calibre ${1+"$@"} haproxy-tool:exec python2 -m cs.app.haproxy ${1+"$@"} iphoto:exec python3 -m cs.app.osx.iphoto ${1+"$@"} maildb:exec python3 -m cs.app.maildb ${1+"$@"} mailfiler:exec python3 -m cs.app.mailfiler ${1+"$@"} mklinks:exec python -m cs.app.mklinks ${1+"$@"} myke:exec python3 -m cs.app.myke ${1+"$@"} nodedb:exec python -m cs.nodedb.__init__ "$CS_NODEDB_URL" "$op" ${1+"$@"} pilfer:exec python3 -m cs.app.pilfer ${1+"$@"} portfwd:exec python3 -m cs.app.portfwd ${1+"$@"} s3-clone-website: set -- python3 -m cs.app.aws s3 "$s3bucket" sync-up -D -% . svcd:exec python3 -m cs.app.svcd ${1+"$@"} vbox:exec python -m cs.app.virtualbox ${1+"$@"} vt:exec python3 -m cs.vt ${1+"$@"} wol:exec python -m cs.wol ${1+"$@"} Consider: if you write a package, would it have a __main__.py? Well, if the answer is ever "yes" then the same applies to ordinary modules, simple enough to not be worth splitting onto a package. So, yes, for me this is really really common. Even for my current client project, which is largely a package, several of the individual modules within the package have their own main programmes for testing and for various utility tasks dealing solely with that particular subsystem. There's an overarching shell script to set up the environment and then do various things from the command line, and it directly invokes particular modules for some operations that act only on one subsystem. Cheers, Cameron Simpson From nivas.p77 at gmail.com Thu Sep 12 01:06:42 2019 From: nivas.p77 at gmail.com (Srinivas Pullabhotla) Date: Wed, 11 Sep 2019 22:06:42 -0700 (PDT) Subject: Email messages from grouped email using IMAPClient in Python. Message-ID: <30119356-d8e9-4006-ac5b-56417d5061ae@googlegroups.com> Hello, I am trying to fetch email messages from a gmail inbox. So, there will be 1000s of messages sent to Inbox and since they are 1000s, the emails are grouped 100 per each email item. When I tried this method, the program only fetches some but not all and especially it happens with grouped email messages. How can I get all the messages from that grouped email. import email, time, sys from imapclient import IMAPClient with IMAPClient(HOST) as server: server.login(USERNAME, PASSWORD) server.select_folder('INBOX', readonly=True) messages = server.search(['ALL', 'UNSEEN']) for uid, message_data in server.fetch(messages, 'RFC822').items(): email_message = email.message_from_bytes(message_data[b'RFC822']) print(email_message.get_payload(None, True)) The program should fetch all the email messages from the grouped email and output to the file (in my case I am grabbing the href links). How best I can achieve this ? Appreciate thoughts and suggestions. From PythonList at DancesWithMice.info Thu Sep 12 01:26:57 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Thu, 12 Sep 2019 17:26:57 +1200 Subject: Email messages from grouped email using IMAPClient in Python. In-Reply-To: <30119356-d8e9-4006-ac5b-56417d5061ae@googlegroups.com> References: <30119356-d8e9-4006-ac5b-56417d5061ae@googlegroups.com> Message-ID: <0a74679f-c957-9e8a-e62f-6ee6c53133e1@DancesWithMice.info> On 12/09/19 5:06 PM, Srinivas Pullabhotla wrote: > Hello, > > I am trying to fetch email messages from a gmail inbox. So, there will be 1000s of messages sent to Inbox and since they are 1000s, the emails are grouped 100 per each email item. > > When I tried this method, the program only fetches some but not all and especially it happens with grouped email messages. How can I get all the messages from that grouped email. > > import email, time, sys > from imapclient import IMAPClient > > with IMAPClient(HOST) as server: > server.login(USERNAME, PASSWORD) > server.select_folder('INBOX', readonly=True) > > messages = server.search(['ALL', 'UNSEEN']) > for uid, message_data in server.fetch(messages, 'RFC822').items(): > email_message = email.message_from_bytes(message_data[b'RFC822']) > print(email_message.get_payload(None, True)) > > > The program should fetch all the email messages from the grouped email and output to the file (in my case I am grabbing the href links). How best I can achieve this ? Appreciate thoughts and suggestions. First debug is to assign the server.select_folder() return values, and subsequently print them for inspection. (know what you are dealing with/that you have been given something to work with) The server.search() doesn't request ALL of the msgs. Are you sure those missing from the 'group' are not marked as 'read'? So, second debug would be to remove the 'UNSEEN' criteria and observe any difference. -- Regards =dn From PythonList at DancesWithMice.info Thu Sep 12 01:30:26 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Thu, 12 Sep 2019 17:30:26 +1200 Subject: WedWonder: Scripts and Modules In-Reply-To: <86sgp2o3ww.fsf@richard.bawden.org> References: <86sgp2o3ww.fsf@richard.bawden.org> Message-ID: On 12/09/19 10:37 AM, Alan Bawden wrote: > DL Neil writes: > >> ... However, reversing the question in my mind led me to ask (myself): >> how many scripts do I have (in "production use") which are ever used >> (also) as a module by some other script? I think the answer is/was: >> "none"! Accordingly, (spoiler alert: this statement may be heresy) I >> stopped using the "if". > > I found that the problem with doing this is that `pydoc' now _runs_ my > scripts, when all I wanted was a quick look at the constants and procedures > defined in them. I haven't experienced this. However, I'll make a note to test... -- Regards =dn From barry at barrys-emacs.org Thu Sep 12 04:22:58 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Thu, 12 Sep 2019 09:22:58 +0100 Subject: WedWonder: Scripts and Modules In-Reply-To: References: Message-ID: > On 11 Sep 2019, at 21:24, DL Neil via Python-list wrote: > > In this day-and-age do you have a script in live/production-use, which is also a module? What is the justification/use case? > > (discounting distutils and similar installation tools, or unit testing methodology) > > > There are over 500 questions on StackOverflow which refer to Python's > > if __name__ == __main__: > > construct. Even more if you include the idea of a main() multiple entry-point. > > This construct enables code to distinguish between being "run" as a "script", and being imported as a "module". (which is not in question) > In my mind this is a question about what side-effects of importing a module are desireable and which are not. A trivia script does not need the __name__ == '__main__' as its all about its side effects. As scripts become more complex having it run on import might make debugging harder and prevents reuse. For example I will import a script at the REPL and examine it and call function in it to help me understand and fix problems. Having a __name__ == '__main__' is important to allow this. I often have modules that are part of a larger program that have their own main() functions to unittest or give access to parsers etc. In large projects with many modules import with side-effect can make for a maintenance burden. Barry > -- > Regards, > =dn > -- > https://mail.python.org/mailman/listinfo/python-list > From barry at barrys-emacs.org Thu Sep 12 04:32:23 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Thu, 12 Sep 2019 09:32:23 +0100 Subject: Email messages from grouped email using IMAPClient in Python. In-Reply-To: <30119356-d8e9-4006-ac5b-56417d5061ae@googlegroups.com> References: <30119356-d8e9-4006-ac5b-56417d5061ae@googlegroups.com> Message-ID: <1B04D736-44B2-417A-BBDA-3B3471EE0DA8@barrys-emacs.org> > On 12 Sep 2019, at 06:06, Srinivas Pullabhotla wrote: > > Hello, > > I am trying to fetch email messages from a gmail inbox. So, there will be 1000s of messages sent to Inbox and since they are 1000s, the emails are grouped 100 per each email item. > > When I tried this method, the program only fetches some but not all and especially it happens with grouped email messages. How can I get all the messages from that grouped email. > > import email, time, sys > from imapclient import IMAPClient > > with IMAPClient(HOST) as server: > server.login(USERNAME, PASSWORD) > server.select_folder('INBOX', readonly=True) > > messages = server.search(['ALL', 'UNSEEN']) > for uid, message_data in server.fetch(messages, 'RFC822').items(): > email_message = email.message_from_bytes(message_data[b'RFC822']) > print(email_message.get_payload(None, True)) > > > The program should fetch all the email messages from the grouped email and output to the file (in my case I am grabbing the href links). How best I can achieve this ? Appreciate thoughts and suggestions. If you have a very large number of messages then you will need to add: # default of 10000 is too small for SEARCH results imaplib._MAXLINE = 200000 If you want to use the MOVE command you will need this: # MOVE is not available by default imaplib.Commands['MOVE'] = ('SELECTED',) As DL said you need to print out what you get back from the search() and check that it is what you expect. Barry > -- > https://mail.python.org/mailman/listinfo/python-list > From rosuav at gmail.com Thu Sep 12 05:47:34 2019 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 12 Sep 2019 19:47:34 +1000 Subject: WedWonder: Scripts and Modules In-Reply-To: References: Message-ID: On Thu, Sep 12, 2019 at 6:57 PM Barry Scott wrote: > As scripts become more complex having it run on import might make debugging harder > and prevents reuse. > > For example I will import a script at the REPL and examine it and call function in it to > help me understand and fix problems. Having a __name__ == '__main__' is important > to allow this. If the script is fairly simple, and hasn't been built to operate as a module, it's often effective enough to just run "python3 -i scriptname.py" and have it do its stuff and immediately drop to REPL. ChrisA From outlook_55EF5EA005DCB00A at outlook.com Thu Sep 12 05:22:44 2019 From: outlook_55EF5EA005DCB00A at outlook.com (Courtney Mark) Date: Thu, 12 Sep 2019 09:22:44 +0000 Subject: Error Message-ID: Hi I am a brand new user. I cannot type onto a shell window or a code window. Kind Regards Courtney Sent from Mail for Windows 10 From smilesonisamal at gmail.com Thu Sep 12 09:53:34 2019 From: smilesonisamal at gmail.com (Pradeep Patra) Date: Thu, 12 Sep 2019 19:23:34 +0530 Subject: numpy results in segmentation fault Message-ID: Hi , I was trying to solve the hackerrank and was using python 3.7.x. https://www.hackerrank.com/challenges/np-concatenate/problem While running the code sometimes I get success result and sometimes it fails with "Segmentation Fault" at Hacker rank UI. I dont have any clue why the code is crashing ? Does anyone have any idea? Regards Pradeep import numpy n,m,p = map(int,input().split()) tgt_arr1 = [] for i in range(n): row = list(map(int,input().split())) tgt_arr1.append(row) tgt_arr2 = [] for j in range(m): row = list(map(int,input().split())) tgt_arr2.append(row) num_arr1 = numpy.array(tgt_arr1,int) num_arr2 = numpy.array(tgt_arr2,int) print(numpy.concatenate((num_arr1,num_arr2),axis=0)) From tjol at tjol.eu Thu Sep 12 10:28:44 2019 From: tjol at tjol.eu (Thomas Jollans) Date: Thu, 12 Sep 2019 16:28:44 +0200 Subject: numpy results in segmentation fault In-Reply-To: References: Message-ID: On 12/09/2019 15.53, Pradeep Patra wrote: > Hi , > > I was trying to solve the hackerrank and was using python 3.7.x. > https://www.hackerrank.com/challenges/np-concatenate/problem > > While running the code sometimes I get success result and sometimes it > fails with "Segmentation Fault" at Hacker rank UI. I dont have any clue why > the code is crashing ? Does anyone have any idea? Are you sure it's your code that's crashing, and not something beyond your control? (Such as the software that is starting Python for you) Does it depend on the input? Can you reproduce the issue in a controlled environment (i.e. on your own PC)? > > Regards > Pradeep > > import numpy > > n,m,p = map(int,input().split()) > tgt_arr1 = [] > for i in range(n): > row = list(map(int,input().split())) > tgt_arr1.append(row) > tgt_arr2 = [] > for j in range(m): > row = list(map(int,input().split())) > tgt_arr2.append(row) > > num_arr1 = numpy.array(tgt_arr1,int) > num_arr2 = numpy.array(tgt_arr2,int) > > print(numpy.concatenate((num_arr1,num_arr2),axis=0)) From grant.b.edwards at gmail.com Thu Sep 12 10:48:32 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Thu, 12 Sep 2019 14:48:32 -0000 (UTC) Subject: cx_freeze bdist_msi: shortcut in startmenu sub-folder? Message-ID: I've got a couple tk apps I distribute for windows using cx_feeze. Currently I use Inno Setup to create a setup.exe, but would like to switch to using cx_freeze's msi feature. But, I'm stumped trying to figure out how to create an entry in a folder underneath the Windows startmenu. The cx_freeze initial_target_dir option allows you to specify a folder path like this: "initial_target_dir": r"[ProgramFilesFolder]\my-company-name\my-app-name" But, cx_freeze's shortcutDir option doesn't seem to accept a path like that, so I'm only able to create an entry in the top-level start menu. Can anybody point me to an example or clue on how to create a shortcut in a folder underneath the start menu with cx_freeze bdist_msi? -- Grant Edwards grant.b.edwards Yow! We are now enjoying at total mutual interaction in gmail.com an imaginary hot tub ... From info at cmcc.it Thu Sep 12 11:49:08 2019 From: info at cmcc.it (CMCC Info) Date: Thu, 12 Sep 2019 17:49:08 +0200 Subject: Scientific Software Developers | Job positions at CMCC Foundation - Euro-Mediterranean Center on Climate Change, Italy Message-ID: <62a37e0c-f30e-f9d7-8a5e-bb8021fc35b3@cmcc.it> /Please, feel free to circulate //to anyone you think may be interested./// -- Open positions at CMCC Foundation: *Software Developers for the Digital Ocean (Research Associate) * [Job Opening Code: 9195] The location is the CMCC ? Ocean Predictions and Applications (OPA) office in Lecce, Italy. _Deadline: 15/09/2019 _ *Scientific Software Developer ***[Job Opening Code: 9209] The location is CMCC Headquarters in Lecce, Italy. Remote working as well as part-time could be suitable options. _Deadline: 20/09/2019 _ *Software Developer for the Digital Ocean (Research Associate) * [Job Opening Code: 9213] The location is CMCC ? Ocean Predictions and Applications (OPA) office in Bologna (Italy) _Deadline: 20/09/2019 _ For further information on CMCC Job Opportunities, please visit our website https://www.cmcc.it/work-with-us . -- Fondazione Centro Euro-Mediterraneo sui Cambiamenti Climatici Web: www.cmcc.it - Contact us: info at cmcc.it From duncan at invalid.invalid Thu Sep 12 13:07:20 2019 From: duncan at invalid.invalid (duncan smith) Date: Thu, 12 Sep 2019 18:07:20 +0100 Subject: duck typing / library functions Message-ID: Hello, The problem I have relates to writing algorithmic code that can handle types with a given API, but where some of the required functionality is implemented as library functions rather than methods. Specifically I have code that uses numpy arrays, but I want to adapt it to use sparse arrays that have a (sufficiently) similar API. For the most part the existing code will work without alteration, but wherever I have a numpy function (rather than array method) I have an issue. In some cases I can find a corresponding method, but not for e.g. numpy.isnan. Roughly the structure of the existing code is: import numpy class Algorithm(object): def __init__(self, arrays): self.arrays = arrays def run(self): shape = a shape calculated from info in self.arrays arr = numpy.ones(shape) # other stuff using array methods and numpy functions I could replace "numpy" above with "sparse" and it would work fine (as long as the arrays passed to __init__ were of the appropriate type). I could move the import inside the class and make it conditional on the types in self.arrays. I could potentially replace the function calls with something based only on method calls (although that turns out to be awkward for some functions). But I'm thinking about something more introspective, maybe identifying the relevant module / package from the array type and importing the relevant module on the fly? Any ideas? TIA. Duncan p.s. Ideally the solution should work with Python 3 and recent versions of Python 2. From PythonList at DancesWithMice.info Thu Sep 12 16:40:02 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Fri, 13 Sep 2019 08:40:02 +1200 Subject: WedWonder: Scripts and Modules In-Reply-To: <20190911225944.GA51821@cskk.homeip.net> References: <20190911225944.GA51821@cskk.homeip.net> Message-ID: On 12/09/19 10:59 AM, Cameron Simpson wrote: > On 12Sep2019 08:24, DL Neil wrote: >> In this day-and-age do you have a script in live/production-use, which >> is also a module? What is the justification/use case? > > Many. Many many. > > 1: Many of my modules run their unit tests if invoked as the main > programme. I used to do this, but now prefer to keep tests in separate modules - and separate directories. > 2: Several modules are their own utility programme. I've got a heap of > these - anything that provides useful access to something can often be > usefully used from the command line. This is an very interesting idea - you're way ahead of me on this! Would it be fair to describe these as more Python for systems programming/system administration, than an application for (simple) users? (what other kind is there???) > Consider: if you write a package, would it have a __main__.py? > > Well, if the answer is ever "yes" then the same applies to ordinary > modules, simple enough to not be worth splitting onto a package. May not properly appreciate this point... > So, yes, for me this is really really common. > > Even for my current client project, which is largely a package, several > of the individual modules within the package have their own main > programmes for testing and for various utility tasks dealing solely with > that particular subsystem. There's an overarching shell script to set up > the environment and then do various things from the command line, and it > directly invokes particular modules for some operations that act only on > one subsystem. Interesting! Gives me pause for thought: perhaps I lean too heavily on putting 'stuff' in the test routines (and view the application from that 'direction' too often). Multiple entry-point systems seem relatively unusual these days - perhaps a natural flow-on effect of the rise of gui- and menu-based systems? With one client, over the years, we've developed a number of (basically) statistical analyses. Each analysis was born from a separate project. Each lives in its own directory (tree). There are some common modules held in a separate 'utility' library/package/directory. Whilst it has been suggested, the idea of an "overarching" menu-type top-level controller/distributor has never risen up the ranks of the "backlog" (sensible criticism: the money would be better spent on other priorities) - they (or at least the older ones) are quite happy to 'drop' to the command line and type the required command+args. I think that's the closest I come to what you have described. (out of interest) Would you characterise it as a common project structure? -- Regards =dn From PythonList at DancesWithMice.info Thu Sep 12 17:11:57 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Fri, 13 Sep 2019 09:11:57 +1200 Subject: WedWonder: Scripts and Modules In-Reply-To: References: Message-ID: <0cc09b51-11e3-401c-d82d-429aca63271a@DancesWithMice.info> On 12/09/19 8:22 PM, Barry Scott wrote: > > >> On 11 Sep 2019, at 21:24, DL Neil via Python-list wrote: >> >> In this day-and-age do you have a script in live/production-use, which is also a module? What is the justification/use case? >> >> (discounting distutils and similar installation tools, or unit testing methodology) >> >> >> There are over 500 questions on StackOverflow which refer to Python's >> >> if __name__ == __main__: >> >> construct. Even more if you include the idea of a main() multiple entry-point. >> >> This construct enables code to distinguish between being "run" as a "script", and being imported as a "module". (which is not in question) >> > > In my mind this is a question about what side-effects of importing a module are > desireable and which are not. Precise description! > A trivia script does not need the __name__ == '__main__' as its all about its side effects. > > As scripts become more complex having it run on import might make debugging harder > and prevents reuse. Is this an informal distinction: that modules are written for re-use but main-lines to be unique/single-purpose? > For example I will import a script at the REPL and examine it and call function in it to > help me understand and fix problems. Having a __name__ == '__main__' is important > to allow this. Why? If we import sys (from the PSL, purely as an example), we don't expect/need any execution phase, can immediately follow (in the REPL) with help(sys) and similar, and can debug/explore from there: >>> import sys >>> help(sys) >>> sys.path ['', '/usr/lib64/python37.zip', '/usr/lib64/python3.7', '/usr/lib64/python3.7/lib-dynload', '/usr/lib64/python3.7/site-packages', '/usr/lib/python3.7/site-packages'] > I often have modules that are part of a larger program that have their own main() functions > to unittest or give access to parsers etc. and the main() is the sole content of the if __name__ etc structure? > In large projects with many modules import with side-effect can make for a maintenance > burden. You seem to have unmasked an assumption under which I operate. (well done - you know what 'they' say about assumptions!) As you say, (non-designed) side-effects are undesirable. My 'rule' is that modules only contain definitions, eg classes and functions. Thus, *nothing* executes upon import. During a code review, some eagle-eye, noticed (and questioned) I had re-factored some 'constants' which control a "switch" structure from out of the module-space, and into the class where they 'belonged', without having any other 'good reason'. I have no recollection of the history or rationale for this policy/practice, nor can I remember whether it 'belongs to Python' or some other language from which I've imported it (hah, punny joke!) Perhaps it comes from my preference to from...import... rather than import...as... which largely eschews the module namespace? (or perhaps I'm just a closet control-freak?) That said, I'm sure there must be modules of my code which break this 'rule', somewhere (certainly in the 'quick and dirty' department). -- Regards =dn From cs at cskk.id.au Thu Sep 12 18:27:09 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Fri, 13 Sep 2019 08:27:09 +1000 Subject: WedWonder: Scripts and Modules In-Reply-To: References: Message-ID: <20190912222709.GA49681@cskk.homeip.net> On 13Sep2019 08:40, DL Neil wrote: >On 12/09/19 10:59 AM, Cameron Simpson wrote: >>On 12Sep2019 08:24, DL Neil wrote: >>>In this day-and-age do you have a script in live/production-use, >>>which is also a module? What is the justification/use case? >> >>Many. Many many. >> >>1: Many of my modules run their unit tests if invoked as the main >>programme. > >I used to do this, but now prefer to keep tests in separate modules - >and separate directories. My tests are also in a separate module, but I can invoke them from the primary module. Just convenience. >>2: Several modules are their own utility programme. I've got a heap of >>these - anything that provides useful access to something can often be >>usefully used from the command line. > >This is an very interesting idea - you're way ahead of me on this! > >Would it be fair to describe these as more Python for systems >programming/system administration, than an application for (simple) >users? Perhaps. I'm not sure this is a meaningful distinction, except in so far as simple users generally expecting a GUI, or programmers expecting an API instead of a command line. Having a command line, particularly one doing the common pattern of "command op args..." with various "op"s for common simple tasks makes it easier to use the module in a script, such as a non-Python script such as the shell. That said, half these "op"s tend to be admin tasks or simple integration-like tests. So "make a new app user record in the database" => "command new-user ...". But also "test this application function" ==> "command do-thing ..." where "do-thing" is something I'm debugging or testing which is normally painfully buried in the module or at the end of some tedious user interaction. And of course "command selftest" to run the unit tests :-) >Gives me pause for thought: perhaps I lean too heavily on putting >'stuff' in the test routines (and view the application from that >'direction' too often). My weakness lies in the other direction - not enough test driven development. IMO, my test writing ability is weak. So I've got this chunk of code and now (==later) I want to test some part of it. Things I am finding useful recently include: - doctests (which have the advantage that I can also use them as example code in the docstrings) which are nice for laying out common use and also specific corner cases - the icontract Python module, which lets me put @require and @ensure decorators on functions to express preconditions and postconditions; really great for catching misuse, especially if you also put some insinstance type checks in there for getting parameters misordered Both of these lack the cumbersomeness of a unit test suite. OTOH, they do not lend themselves to complex tests. >Multiple entry-point systems seem relatively unusual these days - >perhaps a natural flow-on effect of the rise of gui- and menu-based >systems? Unsure. Quite possibly. >With one client, over the years, we've developed a number of >(basically) statistical analyses. Each analysis was born from a >separate project. Each lives in its own directory (tree). There are >some common modules held in a separate 'utility' >library/package/directory. Whilst it has been suggested, the idea of >an "overarching" menu-type top-level controller/distributor has never >risen up the ranks of the "backlog" (sensible criticism: the money >would be better spent on other priorities) - they (or at least the >older ones) are quite happy to 'drop' to the command line and type the >required command+args. I think that's the closest I come to what you >have described. (out of interest) Would you characterise it as a common >project structure? It certainly seems reasonable. And as you suggest, there's you've got a bunch of main programmes associated with distinct modules/packages. Cheers, Cameron Simpson From Richard at Damon-Family.org Thu Sep 12 22:38:31 2019 From: Richard at Damon-Family.org (Richard Damon) Date: Thu, 12 Sep 2019 22:38:31 -0400 Subject: WedWonder: Scripts and Modules In-Reply-To: <0cc09b51-11e3-401c-d82d-429aca63271a@DancesWithMice.info> References: <0cc09b51-11e3-401c-d82d-429aca63271a@DancesWithMice.info> Message-ID: <4679d62b-4e3a-6be0-1a29-8289dd89bda8@Damon-Family.org> On 9/12/19 5:11 PM, DL Neil via Python-list wrote: > On 12/09/19 8:22 PM, Barry Scott wrote: > >> In large projects with many modules import with side-effect can make >> for a maintenance >> burden. > > > You seem to have unmasked an assumption under which I operate. (well > done - you know what 'they' say about assumptions!) > > As you say, (non-designed) side-effects are undesirable. > > My 'rule' is that modules only contain definitions, eg classes and > functions. Thus, *nothing* executes upon import. > > During a code review, some eagle-eye, noticed (and questioned) I had > re-factored some 'constants' which control a "switch" structure from > out of the module-space, and into the class where they 'belonged', > without having any other 'good reason'. > > > I have no recollection of the history or rationale for this > policy/practice, nor can I remember whether it 'belongs to Python' or > some other language from which I've imported it (hah, punny joke!) > Perhaps it comes from my preference to from...import... rather than > import...as... which largely eschews the module namespace? > (or perhaps I'm just a closet control-freak?) > > That said, I'm sure there must be modules of my code which break this > 'rule', somewhere (certainly in the 'quick and dirty' department). > Well technically, def and class are statements that are executed when the module is imported, you need to execute them to add the definitions into the appropriate namespace. Perhaps the 'rule' needs to be somewhat restated to a form where the only thing that should execute when importing a module are the statements needed to create the definitions for the module. -- Richard Damon From PythonList at DancesWithMice.info Thu Sep 12 23:58:17 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Fri, 13 Sep 2019 15:58:17 +1200 Subject: Friday Finking: 'main-lines' are best kept short Message-ID: (this follows some feedback from the recent thread: "WedWonder: Scripts and Modules" and commences a somewhat-related topic/invitation to debate/correct/educate) Is it a good idea to keep a system's main-line* code as short as possible, essentially consigning all of 'the action' to application and external packages and modules? * my choice of term: "main-line", may be taken to mean: - the contents of main(), - the 'then clause' of an if __name__ == __main__: construct, - a __main__.py script. In a previous thread I related some ?good, old days stories. When we tried to break monolithic programs down into modular units, a 'rule of thumb' was "one page-length" per module (back in the mainframe days our code was 'displayed' on lineflo(w) (continuous stationery) which was 66 - call it 60, lines per page - and back-then we could force a page-break where it suited us!). Then when we moved to time-share screens (80 characters by 24 lines), we thought that a good module-length should conform to screen-size. These days I have a large screen mounted in 'portrait mode', so on that basis I'm probably back to 50~60 lines (yes, these old eyes prefer a larger font - cue yet more cheeky, age-ist comments coming from my colleagues...) Likely I have also picked-up and taken-to-heart the *nix mantra of code doing 'one job, and doing it well' (and hence the extensive powers of redirects, piping, etc - in Python we 'chain' code-units together with "import"). Accordingly, I tend to err on the side of short units of code, and thus more methods/functions than others might write. In "Mastering Object-oriented Python" the author discusses "Designing a main script and the __main__ module" (Ch17): <<< A top-level main script will execute our application. In some cases, we may have multiple main scripts because our application does several things. We have three general approaches to writing the top-level main script: ? For very small applications, we can run the application with python3.3 some_script.py . This is the style that we've shown you in most examples. ? For some larger applications, we'll have one or more files that we mark as executable with the OS chmod +x command. We can put these executable files into Python's scripts directory with our setup.py installation. We run these applications with some_script.py at the command line. ? For complex applications, we might add a __main__.py module in the application's package. To provide a tidy interface, the standard library offers the runpy module and the -m command-line option that will use this specially named module. We can run this with python3.3 -m some_app. [explanation of "shebang" line - the second approach, above] Creating a __main__ module To work with the runpy interface, we have a simple implementation. We add a small __main__.py module to our application's top-level package. We have emphasized the design of this top-level executable script file. We should always permit refactoring an application to build a larger, more sophisticated composite application. If there's functionality buried in __main__.py , we need to pull this into a module with a clear, importable name so that it can be used by other applications. A __main__.py module should be something small like the following code: import simulation with simulation.Logging_Config(): with simulation.Application_Config() as config: main= simulation.Simulate_Command() main.config= config main.run() We've done the minimum to create the working contexts for our application. All of the real processing is imported from the package. Also, we've assumed that this __main__.py module will never be imported. This is about all that should be in a __main__ module. Our goal is to maximize the reuse potential of our application. [example] We shouldn't need to create composite Python applications via the command-line API. In order to create a sensible composition of the existing applications, we might be forced to refactor stats/__main__.py to remove any definitions from this module and push them up into the package as a whole. >>> Doesn't the author thus suggest that the script (main-line of the program) should be seen as non-importable? Doesn't he also suggest that the script not contain anything that might be re-usable? Accordingly, the script calls packages/modules which are both importable and re-usable. None of which discounts the possibility of having other 'main-lines' to execute sub-components of the (total) application, should that be appropriate. An issue with 'main-line' scripts is that they can become difficult to test - or to build, using TDD and pytest (speaking personally). Pytest is great for unit tests, and can be used for integration testing, but the 'higher up' the testing pyramid we go, the less effectual it becomes (please don't shoot me, pytest is still an indispensable tool!) Accordingly, if 'the action' is pushed up/out to modules, this will ease the testing, by access and by context! To round things out, I seem to be structuring projects as: .projectV2 -- README -- LICENSE -- docs (sub-directory) -- .git (sub-directory) -- etc -- __main__.py -- project (some prefer "src") sub-directory -- -- package directory/ies -- -- modules directory/ies (obviously others will be import-ed from wherever pip3 (etc) installed them) -- test (sub-directory) -- -- test modules (although sometimes it seems easier to add a test sub-directory to the individual package directory, above) I like the idea that whilst coding, the editor only needs to show the project sub-directory, because (much of) the rest is irrelevant - I have a separate window for the test code, and thus the same distraction-free virtue also applies. Part of making the top-level "projectV2" directory almost-irrelevant in day-to-day dev-work is that __main__.py contains very little, typically three stages: 1 config (including start logging, etc, as appropriate) 2 create the applications central/action object 3 terminate Nary an if __name__ == __main__ in sight (per my last "Wednesday Wondering"), because "the plan" says there is zero likelihood of the "main-line" being treated as a (re-usable) module! (and any refactoring would, in any case, involve pushing such code out to a (re-usable) module! When it comes to execution, the command (excluding any switches/options) becomes: [~/Projects]$ python3 projectV2 Which would also distinguish between project-versions, if relevant. More importantly, changes to application version numbers do not require any changes to import statements! (and when users don't wish to be expected to remember version numbers "as well", use symlinks - just as we do with python/python2/python3/python3.7... Note that it has become unnecessary to add the -m switch! Accordingly, very short entry-point scripts have been working for me. (recognising that Python is used in many more application-areas than do I!) Do you see a reason/circumstance when this practice might break-down? Ref: Mastering Object-oriented Python, S Lott Copyright ? 2014 Packt Publishing -- Regards, =dn From larry.martell at gmail.com Fri Sep 13 01:53:08 2019 From: larry.martell at gmail.com (Larry Martell) Date: Fri, 13 Sep 2019 06:53:08 +0100 Subject: TechRepublicDEVELOPERCXO JPMorgan's Athena has 35 million lines of Python code, and won't be updated to Python 3 in time Message-ID: https://www.techrepublic.com/google-amp/article/jpmorgans-athena-has-35-million-lines-of-python-code-and-wont-be-updated-to-python-3-in-time/ From skip.montanaro at gmail.com Fri Sep 13 08:37:14 2019 From: skip.montanaro at gmail.com (Skip Montanaro) Date: Fri, 13 Sep 2019 07:37:14 -0500 Subject: TechRepublicDEVELOPERCXO JPMorgan's Athena has 35 million lines of Python code, and won't be updated to Python 3 in time In-Reply-To: References: Message-ID: > https://www.techrepublic.com/google-amp/article/jpmorgans-athena-has-35-million-lines-of-python-code-and-wont-be-updated-to-python-3-in-time/ I doubt this is unusual, and presume JP Morgan is big enough to handle the change of status, either by managing security releases in-house or relying on third-party releases (say, Anaconda). When I retired from Citadel recently, most Python was still 2.7 (though the group I worked in was well on the way to converting to 3.x, and no new applications were written against 2.7). Bank of America has an enterprise-wide system called Quartz. I wouldn't be surprised if it was still running Python 2.7 (though I don't know for sure). Skip From jasonanyilian at gmail.com Fri Sep 13 15:17:38 2019 From: jasonanyilian at gmail.com (CrazyVideoGamez) Date: Fri, 13 Sep 2019 12:17:38 -0700 (PDT) Subject: Weird Python Bug Message-ID: <5900595c-0caa-407c-86f5-8fda47797eb2@googlegroups.com> For some reason, if you put in the code def odd_ones_out(numbers): for num in numbers: count = numbers.count(num) if not count % 2 == 0: for i in range(count): numbers.remove(num) return numbers nums = [72, 4, 82, 67, 67] print(odd_ones_out(nums)) For some reason, it outputs [4, 67, 67] when it should have deleted the 4 because it was odd. Another interesting thing is that when you put print(num) in the for loop, the number 4 never shows up and neither does the last 67. Help! From David.Raymond at tomtom.com Fri Sep 13 15:51:04 2019 From: David.Raymond at tomtom.com (David Raymond) Date: Fri, 13 Sep 2019 19:51:04 +0000 Subject: Weird Python Bug In-Reply-To: <5900595c-0caa-407c-86f5-8fda47797eb2@googlegroups.com> References: <5900595c-0caa-407c-86f5-8fda47797eb2@googlegroups.com> Message-ID: 2 comments: First: Deleting from a list while you're iterating over it is a bad idea. Your first iteration gives nums[0] which is 72. But then you delete that and (in effect) everything moves up. So now the 4 is in the nums[0] slot. Your second iteration returns nums[1] which is now the 82 meaning you skipped over the 4... etc etc etc., Second: You are altering the original list that you're passing to the function, you're not working on a copy of it. Make sure that that is what you want, and that you didn't intend to leave the original thing alone. >>> nums = [72, 4, 82, 67, 67] >>> odd_ones_out(nums) [4, 67, 67] >>> nums [4, 67, 67] >>> -----Original Message----- From: Python-list On Behalf Of CrazyVideoGamez Sent: Friday, September 13, 2019 3:18 PM To: python-list at python.org Subject: Weird Python Bug For some reason, if you put in the code def odd_ones_out(numbers): for num in numbers: count = numbers.count(num) if not count % 2 == 0: for i in range(count): numbers.remove(num) return numbers nums = [72, 4, 82, 67, 67] print(odd_ones_out(nums)) For some reason, it outputs [4, 67, 67] when it should have deleted the 4 because it was odd. Another interesting thing is that when you put print(num) in the for loop, the number 4 never shows up and neither does the last 67. Help! -- https://mail.python.org/mailman/listinfo/python-list From python at mrabarnett.plus.com Fri Sep 13 15:57:17 2019 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 13 Sep 2019 20:57:17 +0100 Subject: Weird Python Bug In-Reply-To: <5900595c-0caa-407c-86f5-8fda47797eb2@googlegroups.com> References: <5900595c-0caa-407c-86f5-8fda47797eb2@googlegroups.com> Message-ID: <326b8862-366c-1f60-d062-bdf2032326eb@mrabarnett.plus.com> On 2019-09-13 20:17, CrazyVideoGamez wrote: > For some reason, if you put in the code > > def odd_ones_out(numbers): > for num in numbers: > count = numbers.count(num) > if not count % 2 == 0: > for i in range(count): > numbers.remove(num) > return numbers > > nums = [72, 4, 82, 67, 67] > print(odd_ones_out(nums)) > > For some reason, it outputs [4, 67, 67] when it should have deleted the 4 because it was odd. Another interesting thing is that when you put print(num) in the for loop, the number 4 never shows up and neither does the last 67. Help! > Here's a simpler example to show what's going on: >>> numbers = [1, 2, 3] >>> >>> for n in numbers: ... numbers.remove(n) ... >>> print(numbers) [2] The 'for' loop steps through the list, one item at a time: first, second, third, etc. In the first iteration, the first item is 1. The body of the loop removes that item, and the following items are shifted down, leaving [2, 3]. In the second iteration, the second item is 3. (2 is now the first item, and as far as it's concerned it has already done the first item.) The body of the loop removes that item, and the following items are shifted down, leaving [2]. It has now reached the end of the list. And the moral is: don't change a list while you're iterating over it. Iterate over the "input" list and build a new "output" list to hold the results. From cs at cskk.id.au Fri Sep 13 19:02:22 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sat, 14 Sep 2019 09:02:22 +1000 Subject: Friday Finking: 'main-lines' are best kept short In-Reply-To: References: Message-ID: <20190913230222.GA52927@cskk.homeip.net> On 13Sep2019 15:58, DL Neil wrote: >Is it a good idea to keep a system's main-line* code as short as >possible, essentially consigning all of 'the action' to application and >external packages and modules? Generally yes. >* my choice of term: "main-line", may be taken to mean: >- the contents of main(), >- the 'then clause' of an if __name__ == __main__: construct, >- a __main__.py script. Taking these out of order: I don't like "if __name__ == '__main__':" to be more than a few lines. If it gets past about 4 or 5 then I rip it out into a main() function and use: if __name__ == '__main__': sys.exit(main(sys.argv)) and put "def main(argv):" at the top of the module (where it is glaringly obvious). Once at that stage, where you have a __main__.py or a "def main()" is based _entirely_ on whether this is a module or a package. There is no other criterion for me. [... snip ...] >Doesn't the author thus suggest that the script (main-line of the >program) should be seen as non-importable? __main__.py is generally something you would never import, any more than you would want to import the _body_ of a main() function. Particularly because it will run things that have side effects; a normal import should not. >Doesn't he also suggest that the script not contain anything that >might be re-usable? That is a very similar statement, or at least tightly tied in. If you can't import __main__.py because it actually runs the main programme, then you can't import it to make use of resuable things. Therefore reusable things should not have their definitions in __main__.py. >Accordingly, the script calls packages/modules which are both >importable and re-usable. > >None of which discounts the possibility of having other 'main-lines' >to execute sub-components of the (total) application, should that be >appropriate. > >An issue with 'main-line' scripts is that they can become difficult to >test - or to build, using TDD and pytest (speaking personally). Pytest >is great for unit tests, and can be used for integration testing, but >the 'higher up' the testing pyramid we go, the less effectual it >becomes (please don't shoot me, pytest is still an indispensable >tool!) Accordingly, if 'the action' is pushed up/out to modules, this >will ease the testing, by access and by context! Yes. So ideally your "main" should be fairly skeletal, calling out to components defined elsewhere. >To round things out, I seem to be structuring projects as: > >.projectV2 >-- README >-- LICENSE >-- docs (sub-directory) >-- .git (sub-directory) >-- etc >-- __main__.py [...] I don't have a top level __main__.py in the project source tree; I _hate_ having python scripts in the top level because they leak into the import namespace courtesy of Python's sys.path including the current directory. __main__.py belongs in the package, and that is down a level (or so) from the main source tree. [...] >Part of making the top-level "projectV2" directory almost-irrelevant in >day-to-day dev-work is that __main__.py contains very little, typically >three stages: > 1 config (including start logging, etc, as appropriate) > 2 create the applications central/action object > 3 terminate > >Nary an if __name__ == __main__ in sight (per my last "Wednesday >Wondering"), because "the plan" says there is zero likelihood of the >"main-line" being treated as a (re-usable) module! (and any >refactoring would, in any case, involve pushing such code out to a >(re-usable) module! As alluded to earlier, the "if __main__ == '__main__':" is entirely an idiom to support main-programme semantics in a module. In a package you have a __main__.py and no need for the idiom. >When it comes to execution, the command (excluding any >switches/options) becomes: > > [~/Projects]$ python3 projectV2 And there's your use case for the top level __main__.py. I prefer: python3 -m projectv2 where the projectv2 package is found via the sys.path. In production projectv2 would be installed somewhere sensible, and in development I've a little "dev" shell function which presumes it is in the project top level and sets $PATH, $PYTHPATH etc to allow "python3 -m projectv2" to find the package. So in dev I go: dev python3 -m projectv2 The advantage here is that if I don't prefix things with "dev" I get the official installed projectv2 (whatever that means - it couldeasily be my personal ~/bin etc), and with the "dev" prefix I get the version in my development tree. So that I don'trun the dev stuff by accident (which is one reason I eschew the virtualenv "activate" script - my command line environment should not be using the dev environment inless I say so, because "dev" might be broken^Wincompatible). >Which would also distinguish between project-versions, if relevant. >More importantly, changes to application version numbers do not >require any changes to import statements! (and when users don't wish >to be expected to remember version numbers "as well", use symlinks - >just as we do with python/python2/python3/python3.7... > >Note that it has become unnecessary to add the -m switch! The -m switch is my friend. It says "obey the sys.path", so that I can control things courtesy of the sys.path/$PYTHONPATH. Cheers, Cameron Simpson From hongyi.zhao at gmail.com Fri Sep 13 21:22:19 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Sat, 14 Sep 2019 01:22:19 +0000 (UTC) Subject: what's the differences: None and null? Message-ID: what's the differences: None and null? From random832 at fastmail.com Fri Sep 13 21:40:13 2019 From: random832 at fastmail.com (Random832) Date: Fri, 13 Sep 2019 21:40:13 -0400 Subject: what's the differences: None and null? In-Reply-To: References: Message-ID: On Fri, Sep 13, 2019, at 21:22, Hongyi Zhao wrote: > what's the differences: None and null? null isn't really a concept that exists in Python... while None fills many of the same roles that null does in some other languages, it is a proper object, with __str__ and __repr__ methods (that return 'None'), __hash__ (that returns an arbitrary value), etc. NULL, the C/C++ null pointer, is used in CPython for things like error returns, things like slots or cell variables that are not initialized (as opposed to being set to None), etc. This is an implementation detail, and should never come up in Python code. From hongyi.zhao at gmail.com Fri Sep 13 22:18:46 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Sat, 14 Sep 2019 02:18:46 +0000 (UTC) Subject: The differences between json.dumps and json.loads. Message-ID: I'm very confusing on the the differences between json.dumps and json.loads. Any hints? From cs at cskk.id.au Fri Sep 13 22:25:30 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sat, 14 Sep 2019 12:25:30 +1000 Subject: The differences between json.dumps and json.loads. In-Reply-To: References: Message-ID: <20190914022530.GA81595@cskk.homeip.net> On 14Sep2019 02:18, Hongyi Zhao wrote: >I'm very confusing on the the differences between json.dumps and >json.loads. Any hints? The direction. json.dumps takes an object and transcribes it as a string. "Dump to string." json.loads takes a string and decodes it as JSON and returns an object. "Load from string". Cheers, Cameron Simpson (formerly cs at zip.com.au) From oscar.j.benjamin at gmail.com Fri Sep 13 22:26:24 2019 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sat, 14 Sep 2019 03:26:24 +0100 Subject: itertools product(infinite iterator) hangs Message-ID: I've been staring at this for a little while: from itertools import product class Naturals: def __iter__(self): i = 1 while True: yield i i += 1 N = Naturals() print(iter(N)) print(product(N)) # <--- hangs When I run the above the call to product hangs but I can't see why. I would expect that since I'm not iterating over the product it would just call iter(N) but clearly not since iter(N) returns a generator instantly where as product(N) hangs. What am I missing? Oscar From robertvstepp at gmail.com Fri Sep 13 23:51:08 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 13 Sep 2019 22:51:08 -0500 Subject: Friday Finking: 'main-lines' are best kept short In-Reply-To: References: Message-ID: On Thu, Sep 12, 2019 at 10:59 PM DL Neil via Python-list wrote: > Ref: > Mastering Object-oriented Python, S Lott > Copyright ? 2014 Packt Publishing Side note: When I looked this up I saw on Amazon that there is a second edition out targeting Python 3.7. It was published June of this year. -- boB From bluebox03 at gmail.com Fri Sep 13 23:58:45 2019 From: bluebox03 at gmail.com (tommy yama) Date: Sat, 14 Sep 2019 12:58:45 +0900 Subject: TechRepublicDEVELOPERCXO JPMorgan's Athena has 35 million lines of Python code, and won't be updated to Python 3 in time In-Reply-To: References: Message-ID: "35 million lines of python code" it is insane. On Fri, Sep 13, 2019 at 9:39 PM Skip Montanaro wrote: > > > https://www.techrepublic.com/google-amp/article/jpmorgans-athena-has-35-million-lines-of-python-code-and-wont-be-updated-to-python-3-in-time/ > > I doubt this is unusual, and presume JP Morgan is big enough to handle > the change of status, either by managing security releases in-house or > relying on third-party releases (say, Anaconda). When I retired from > Citadel recently, most Python was still 2.7 (though the group I worked > in was well on the way to converting to 3.x, and no new applications > were written against 2.7). Bank of America has an enterprise-wide > system called Quartz. I wouldn't be surprised if it was still running > Python 2.7 (though I don't know for sure). > > Skip > -- > https://mail.python.org/mailman/listinfo/python-list > From none at gmail.com Sat Sep 14 02:18:07 2019 From: none at gmail.com (ast) Date: Sat, 14 Sep 2019 08:18:07 +0200 Subject: itertools product(infinite iterator) hangs In-Reply-To: References: Message-ID: <5d7c8622$0$31421$426a34cc@news.free.fr> Le 14/09/2019 ? 04:26, Oscar Benjamin a ?crit?: > I've been staring at this for a little while: > > from itertools import product > > class Naturals: > def __iter__(self): > i = 1 > while True: > yield i > i += 1 > > N = Naturals() > print(iter(N)) > print(product(N)) # <--- hangs > > When I run the above the call to product hangs but I can't see why. I > would expect that since I'm not iterating over the product it would > just call iter(N) but clearly not since iter(N) returns a generator > instantly where as product(N) hangs. > > What am I missing? > > Oscar > here is a pseudo code for product: def product(*args, repeat=1): # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111 pools = [tuple(pool) for pool in args] * repeat result = [[]] for pool in pools: result = [x+[y] for x in result for y in pool] for prod in result: yield tuple(prod) clearly "tuple(pool)" hangs with an infinite iterable pool From larry.martell at gmail.com Sat Sep 14 04:37:14 2019 From: larry.martell at gmail.com (Larry Martell) Date: Sat, 14 Sep 2019 09:37:14 +0100 Subject: TechRepublicDEVELOPERCXO JPMorgan's Athena has 35 million lines of Python code, and won't be updated to Python 3 in time In-Reply-To: References: Message-ID: On Fri, Sep 13, 2019 at 1:37 PM Skip Montanaro wrote: > > > https://www.techrepublic.com/google-amp/article/jpmorgans-athena-has-35-million-lines-of-python-code-and-wont-be-updated-to-python-3-in-time/ > > I doubt this is unusual, and presume JP Morgan is big enough to handle > the change of status, either by managing security releases in-house or > relying on third-party releases (say, Anaconda). When I retired from > Citadel recently, most Python was still 2.7 (though the group I worked > in was well on the way to converting to 3.x, and no new applications > were written against 2.7). Bank of America has an enterprise-wide > system called Quartz. I wouldn't be surprised if it was still running > Python 2.7 (though I don't know for sure). Yes Quartz is 2.7. As I?ve said before here, I know a lot of companies running large apps in 2.7 and they have no intention of moving to 3. > > From o1bigtenor at gmail.com Sat Sep 14 08:11:56 2019 From: o1bigtenor at gmail.com (o1bigtenor) Date: Sat, 14 Sep 2019 07:11:56 -0500 Subject: TechRepublicDEVELOPERCXO JPMorgan's Athena has 35 million lines of Python code, and won't be updated to Python 3 in time In-Reply-To: References: Message-ID: On Sat, Sep 14, 2019 at 3:39 AM Larry Martell wrote: > > On Fri, Sep 13, 2019 at 1:37 PM Skip Montanaro > wrote: > > > > > > https://www.techrepublic.com/google-amp/article/jpmorgans-athena-has-35-million-lines-of-python-code-and-wont-be-updated-to-python-3-in-time/ > > > > I doubt this is unusual, and presume JP Morgan is big enough to handle > > the change of status, either by managing security releases in-house or > > relying on third-party releases (say, Anaconda). When I retired from > > Citadel recently, most Python was still 2.7 (though the group I worked > > in was well on the way to converting to 3.x, and no new applications > > were written against 2.7). Bank of America has an enterprise-wide > > system called Quartz. I wouldn't be surprised if it was still running > > Python 2.7 (though I don't know for sure). > > > > Yes Quartz is 2.7. As I?ve said before here, I know a lot of companies > running large apps in 2.7 and they have no intention of moving to 3. > Likely quite true - - - - - until a security flaw connected to the older version is exploited - - - - (not saying its likely) - - - then watch for the then declared crucial to do it right now scramble. Regards From gheskett at shentel.net Sat Sep 14 08:30:06 2019 From: gheskett at shentel.net (Gene Heskett) Date: Sat, 14 Sep 2019 08:30:06 -0400 Subject: TechRepublicDEVELOPERCXO JPMorgan's Athena has 35 million lines of Python code, and won't be updated to Python 3 in time In-Reply-To: References: Message-ID: <201909140830.06979.gheskett@shentel.net> On Saturday 14 September 2019 04:37:14 Larry Martell wrote: > On Fri, Sep 13, 2019 at 1:37 PM Skip Montanaro > > > wrote: > > https://www.techrepublic.com/google-amp/article/jpmorgans-athena-has > >-35-million-lines-of-python-code-and-wont-be-updated-to-python-3-in-t > >ime/ > > > > I doubt this is unusual, and presume JP Morgan is big enough to > > handle the change of status, either by managing security releases > > in-house or relying on third-party releases (say, Anaconda). When I > > retired from Citadel recently, most Python was still 2.7 (though the > > group I worked in was well on the way to converting to 3.x, and no > > new applications were written against 2.7). Bank of America has an > > enterprise-wide system called Quartz. I wouldn't be surprised if it > > was still running Python 2.7 (though I don't know for sure). > > Yes Quartz is 2.7. As I?ve said before here, I know a lot of companies > running large apps in 2.7 and they have no intention of moving to 3. And I, Larry, have little doubt that the hackers have a hole into a 2.7 install, all squirreled away, and waiting until 2.7 security support goes away. It's the nature of the thing. They will get hacked. Its like asking if concrete will crack as you are watching it being poured, will is the wrong question, when is far more correct. And it will cost them trillions in the long haul. The courts, adjudicating damages, will not be kind to the foot dragger's who think they are saving money. History sure seems to be pointing in that direction recently. Its a puzzle to me, why so-called sane MBA's cannot understand that the best defense is spending money on the offense by updateing their in-house operating code. Or the OS under it. Cheers, Gene Heskett -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) If we desire respect for the law, we must first make the law respectable. - Louis D. Brandeis Genes Web page From tjreedy at udel.edu Sat Sep 14 11:46:50 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 14 Sep 2019 11:46:50 -0400 Subject: TechRepublicDEVELOPERCXO JPMorgan's Athena has 35 million lines of Python code, and won't be updated to Python 3 in time In-Reply-To: References: Message-ID: On 9/14/2019 4:37 AM, Larry Martell wrote: > On Fri, Sep 13, 2019 at 1:37 PM Skip Montanaro > wrote: >> https://www.techrepublic.com/google-amp/article/jpmorgans-athena-has-35-million-lines-of-python-code-and-wont-be-updated-to-python-3-in-time/ >> >> I doubt this is unusual, and presume JP Morgan is big enough to handle >> the change of status, either by managing security releases in-house or >> relying on third-party releases (say, Anaconda). When I retired from >> Citadel recently, most Python was still 2.7 (though the group I worked >> in was well on the way to converting to 3.x, and no new applications >> were written against 2.7). Bank of America has an enterprise-wide >> system called Quartz. I wouldn't be surprised if it was still running >> Python 2.7 (though I don't know for sure). > Yes Quartz is 2.7. As I?ve said before here, I know a lot of companies > running large apps in 2.7 and they have no intention of moving to 3. This is not JPMorgan. From the article "JPMorgan's roadmap puts "most strategic components" compatible with Python 3 by the end of Q1 2020?that is, three months after the end of security patches?with "all legacy Python 2.7 components" planned for compatibility with Python 3 by Q4 2020." So they must be working on it now. The 'end of Q1 2020' is about when the final release, 2.7.18, will be and Q3 2020 is about when the next release, 2.7.19 would be if we did not stop free support. As far as core developers are concerned, risk judgements are the business of private businesses and some of us anticipate 2.7 being used for at least another decade. We *have* nudged some library developers a bit, especially in the scientific stack, especially numpy and scipy,to release 3.x versions so that new code can be written in 3.x. -- Terry Jan Reedy From gheskett at shentel.net Sat Sep 14 12:05:48 2019 From: gheskett at shentel.net (Gene Heskett) Date: Sat, 14 Sep 2019 12:05:48 -0400 Subject: TechRepublicDEVELOPERCXO JPMorgan's Athena has 35 million lines of Python code, and won't be updated to Python 3 in time In-Reply-To: References: Message-ID: <201909141205.48806.gheskett@shentel.net> On Saturday 14 September 2019 11:46:50 Terry Reedy wrote: > On 9/14/2019 4:37 AM, Larry Martell wrote: > > On Fri, Sep 13, 2019 at 1:37 PM Skip Montanaro > > > > > > wrote: > >> https://www.techrepublic.com/google-amp/article/jpmorgans-athena-ha > >>s-35-million-lines-of-python-code-and-wont-be-updated-to-python-3-in > >>-time/ > >> > >> I doubt this is unusual, and presume JP Morgan is big enough to > >> handle the change of status, either by managing security releases > >> in-house or relying on third-party releases (say, Anaconda). When I > >> retired from Citadel recently, most Python was still 2.7 (though > >> the group I worked in was well on the way to converting to 3.x, and > >> no new applications were written against 2.7). Bank of America has > >> an enterprise-wide system called Quartz. I wouldn't be surprised if > >> it was still running Python 2.7 (though I don't know for sure). > > > > Yes Quartz is 2.7. As I?ve said before here, I know a lot of > > companies running large apps in 2.7 and they have no intention of > > moving to 3. > > This is not JPMorgan. From the article "JPMorgan's roadmap puts "most > strategic components" compatible with Python 3 by the end of Q1 > 2020?that is, three months after the end of security patches?with "all > legacy Python 2.7 components" planned for compatibility with Python 3 > by Q4 2020." So they must be working on it now. > > The 'end of Q1 2020' is about when the final release, 2.7.18, will be > and Q3 2020 is about when the next release, 2.7.19 would be if we did > not stop free support. > > As far as core developers are concerned, risk judgements are the > business of private businesses and some of us anticipate 2.7 being > used for at least another decade. We *have* nudged some library > developers a bit, especially in the scientific stack, especially numpy > and scipy,to release 3.x versions so that new code can be written in > 3.x. > > -- > Terry Jan Reedy I don't have an oar in this water, Terry, other than my bank no doubt has some python in its system, and its track record of bugs in the interface I'm being forced to use, which just Wednesday resulted in my calling one to their attention but I'd say that nudge needs to be set in a crontab, to repeat that nudge often enough to be effective. I suspect what I experienced Wednesday was python3 growing pains, which the fact that they are working on it ahead of time, is encouraging. Cheers, Gene Heskett -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) If we desire respect for the law, we must first make the law respectable. - Louis D. Brandeis Genes Web page From cseberino at gmail.com Sat Sep 14 21:03:05 2019 From: cseberino at gmail.com (Christian Seberino) Date: Sat, 14 Sep 2019 18:03:05 -0700 (PDT) Subject: What about idea of making a "Pythonic Lisp"...i.e. a Lisp that more closely resembles the syntax of Python? Message-ID: <4bf8659c-b04b-4147-9c76-551c2b535bf7@googlegroups.com> Python is my goto main language. However, sometimes I'm tempted to play with a Lisp like language just for fun. Clojure looks pretty solid but its syntax is different than Python's. Since Lisp's make it so easy to modify the language, what about the idea of developing a few macros to make a modified Clojure dialect that more closely matched Python keywords and so was more comfy for Pythonistas? Chris From lvalence at example.com Sat Sep 14 22:19:32 2019 From: lvalence at example.com (Louis Valence) Date: Sat, 14 Sep 2019 23:19:32 -0300 Subject: What about idea of making a "Pythonic Lisp"...i.e. a Lisp that more closely resembles the syntax of Python? References: <4bf8659c-b04b-4147-9c76-551c2b535bf7@googlegroups.com> Message-ID: <86ef0ie1xn.fsf@example.com> Christian Seberino writes: > Python is my goto main language. However, sometimes I'm tempted to > play with a Lisp like language just for fun. > > Clojure looks pretty solid but its syntax is different than Python's. > > Since Lisp's make it so easy to modify the language, what about the idea > of developing a few macros to make a modified Clojure dialect that > more closely matched Python keywords and so was more comfy for Pythonistas? I had to read this twice. It confused the hell out of me. Anyhow, I suppose you should take a look at https://github.com/hylang/hy Enjoy! From sinardyxing at gmail.com Sun Sep 15 01:00:30 2019 From: sinardyxing at gmail.com (Sinardy Gmail) Date: Sun, 15 Sep 2019 13:00:30 +0800 Subject: Document Entire Apps Message-ID: <5005627D-460D-409E-9978-D68448AE3265@gmail.com> Hi Python Gurus, I am in earlier stage in my apps development, What tools or how is the Python community standard good practice in documenting the apps especially the packages that import another packages. I understand that we can use pydoc to document procedures how about the relationship between packages and dependencies ? Thanks Sin From a at d-amo.re Sun Sep 15 02:54:12 2019 From: a at d-amo.re (Andrea D'Amore) Date: Sun, 15 Sep 2019 08:54:12 +0200 Subject: what's the differences: None and null? In-Reply-To: References: Message-ID: On Sat, 14 Sep 2019 at 03:40, Random832 wrote: > On Fri, Sep 13, 2019, at 21:22, Hongyi Zhao wrote: > > what's the differences: None and null? > null isn't really a concept that exists in Python... I'd say the opposite, according to [1] "None" is just the name of the null object. [1]: https://docs.python.org/3/library/stdtypes.html?highlight=null#the-null-object -- Andrea On Sat, 14 Sep 2019 at 03:40, Random832 wrote: > > On Fri, Sep 13, 2019, at 21:22, Hongyi Zhao wrote: > > what's the differences: None and null? > > null isn't really a concept that exists in Python... while None fills many of the same roles that null does in some other languages, it is a proper object, with __str__ and __repr__ methods (that return 'None'), __hash__ (that returns an arbitrary value), etc. > > NULL, the C/C++ null pointer, is used in CPython for things like error returns, things like slots or cell variables that are not initialized (as opposed to being set to None), etc. This is an implementation detail, and should never come up in Python code. > -- > https://mail.python.org/mailman/listinfo/python-list -- Andrea From hongyi.zhao at gmail.com Sun Sep 15 05:15:30 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Sun, 15 Sep 2019 09:15:30 +0000 (UTC) Subject: Bool variable with json.dumps Message-ID: Hi, var = True when json.dumps on it, it will become the form `true' Why? From cs at cskk.id.au Sun Sep 15 05:36:20 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 15 Sep 2019 19:36:20 +1000 Subject: Bool variable with json.dumps In-Reply-To: References: Message-ID: <20190915093620.GA47438@cskk.homeip.net> On 15Sep2019 09:15, Hongyi Zhao wrote: >var = True > >when json.dumps on it, it will become the form `true' > >Why? Because that's how the true value is spelt in JavaScript. You _are_ aware that JSON is "JavaScript Object Notation", are you not? So json.dumps translates Python values into JavaScript syntax. This is likely to be the same reason you were wondering about None vs null; broadly, the Python None value serves the same purpose as the JavaScript null value, and therefore that is how it is expressed in JSON. Cheers, Cameron Simpson From hongyi.zhao at gmail.com Sun Sep 15 07:48:02 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Sun, 15 Sep 2019 11:48:02 +0000 (UTC) Subject: Bool variable with json.dumps References: <20190915093620.GA47438@cskk.homeip.net> Message-ID: On Sun, 15 Sep 2019 19:36:20 +1000, Cameron Simpson wrote: > Because that's how the true value is spelt in JavaScript. You _are_ > aware that JSON is "JavaScript Object Notation", are you not? So > json.dumps translates Python values into JavaScript syntax. > > This is likely to be the same reason you were wondering about None vs > null; broadly, the Python None value serves the same purpose as the > JavaScript null value, and therefore that is how it is expressed in > JSON. Thanks a lot for your notes. From none at gmail.com Sun Sep 15 09:17:54 2019 From: none at gmail.com (ast) Date: Sun, 15 Sep 2019 15:17:54 +0200 Subject: what's the differences: None and null? In-Reply-To: References: Message-ID: <5d7e3a04$0$6476$426a74cc@news.free.fr> Le 14/09/2019 ? 03:40, Random832 a ?crit?: > On Fri, Sep 13, 2019, at 21:22, Hongyi Zhao wrote: >> what's the differences: None and null? > > null isn't really a concept that exists in Python... while None fills many of the same roles that null does in some other languages, it is a proper object, with __str__ and __repr__ methods (that return 'None'), __hash__ (that returns an arbitrary value), etc. > I add that None is a singleton. There is only one None object. So testing if a variable is None is done with identity check: if var is None: pass From hongyi.zhao at gmail.com Sun Sep 15 10:45:13 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Sun, 15 Sep 2019 14:45:13 +0000 (UTC) Subject: # type: a Message-ID: Hi, In pycharm? when I commented out the following line: # type: a The pycharm still told me that: Unresolved reference 'a' Till I commented like the following, the warning will disappear: # type: # a Why? From torriem at gmail.com Sun Sep 15 11:03:06 2019 From: torriem at gmail.com (Michael Torrie) Date: Sun, 15 Sep 2019 09:03:06 -0600 Subject: What about idea of making a "Pythonic Lisp"...i.e. a Lisp that more closely resembles the syntax of Python? In-Reply-To: <86ef0ie1xn.fsf@example.com> References: <4bf8659c-b04b-4147-9c76-551c2b535bf7@googlegroups.com> <86ef0ie1xn.fsf@example.com> Message-ID: <7b2d3c08-266e-e1c6-8b1f-5bf072f11f60@gmail.com> On 9/14/19 8:19 PM, Louis Valence wrote: > I had to read this twice. It confused the hell out of me. Anyhow, I > suppose you should take a look at > > https://github.com/hylang/hy Yup that's probably exactly the opposite of what the OP was asking about. Neat, though. From cseberino at gmail.com Sun Sep 15 11:10:37 2019 From: cseberino at gmail.com (Christian Seberino) Date: Sun, 15 Sep 2019 08:10:37 -0700 (PDT) Subject: What about idea of making a "Pythonic Lisp"...i.e. a Lisp that more closely resembles the syntax of Python? In-Reply-To: <87d0g21fqt.fsf@nightsong.com> References: <4bf8659c-b04b-4147-9c76-551c2b535bf7@googlegroups.com> <87d0g21fqt.fsf@nightsong.com> Message-ID: <1c3b76f2-7a62-4b84-a681-297ea5967b7d@googlegroups.com> > Python vs Clojure's syntax difference is superficial compared to their > other differences, like the Clojure's immutable data structures and > having to deal with the JVM. Well there's ClojureScript to run this hypothetical Pythonic Lisp in the browser. > I also don't think it's really practical > to Pythonize Lisp syntax using macros as opposed to using Lisp as a > compiler backend for something with a non-Lisp syntax. Say if I may ask a tangential question...I've always wondered whether it would be not too hard to compile Python source code to a Lisp like source code? How hard would it be to say compile Python source to Clojure source? cs From cseberino at gmail.com Sun Sep 15 11:14:07 2019 From: cseberino at gmail.com (Christian Seberino) Date: Sun, 15 Sep 2019 08:14:07 -0700 (PDT) Subject: What about idea of making a "Pythonic Lisp"...i.e. a Lisp that more closely resembles the syntax of Python? In-Reply-To: <86ef0ie1xn.fsf@example.com> References: <4bf8659c-b04b-4147-9c76-551c2b535bf7@googlegroups.com> <86ef0ie1xn.fsf@example.com> Message-ID: > I had to read this twice. It confused the hell out of me. Lol. Certainly didn't mean to be confusing! Hy bring Lisp to Python. I was more interested in making a Lisp that had trivial similarities to Python like using some of the same keywords. A related interested of mine is converting Python source code to Clojure source. Imagine instead compiling Python source to an intermediate Lisp flavor that was closer to Python. Then, later converting that "Pythonic Lisp" to Clojure later. That "Pythonic Lisp" is what I was thinking about. Cheers, Chris From uri at speedy.net Sun Sep 15 11:15:03 2019 From: uri at speedy.net (=?UTF-8?B?15DXldeo15k=?=) Date: Sun, 15 Sep 2019 18:15:03 +0300 Subject: # type: a In-Reply-To: References: Message-ID: Try to wait a few minutes, or close and re-open PyCharm. If the problem persists, send a screenshot. ???? uri at speedy.net On Sun, Sep 15, 2019 at 5:51 PM Hongyi Zhao wrote: > Hi, > > In pycharm? when I commented out the following line: > > # type: a > > The pycharm still told me that: > > Unresolved reference 'a' > > Till I commented like the following, the warning will disappear: > > # type: # a > > > Why? > > -- > https://mail.python.org/mailman/listinfo/python-list > From luciano at ramalho.org Sun Sep 15 11:20:31 2019 From: luciano at ramalho.org (Luciano Ramalho) Date: Sun, 15 Sep 2019 12:20:31 -0300 Subject: What about idea of making a "Pythonic Lisp"...i.e. a Lisp that more closely resembles the syntax of Python? In-Reply-To: References: <4bf8659c-b04b-4147-9c76-551c2b535bf7@googlegroups.com> <86ef0ie1xn.fsf@example.com> Message-ID: Take a look at this, Christian: https://github.com/lihaoyi/macropy Best, Luciano On Sun, Sep 15, 2019 at 12:17 PM Christian Seberino wrote: > > > > I had to read this twice. It confused the hell out of me. > > Lol. Certainly didn't mean to be confusing! Hy bring Lisp to Python. I was > more interested in making a Lisp that had trivial similarities to Python like using some of the same keywords. > > A related interested of mine is converting Python source code to Clojure source. Imagine instead compiling Python source to an intermediate Lisp flavor that was closer to Python. Then, later converting that "Pythonic Lisp" to Clojure later. That "Pythonic Lisp" is what I was thinking about. > > Cheers, > > Chris > > -- > https://mail.python.org/mailman/listinfo/python-list -- Luciano Ramalho | Author of Fluent Python (O'Reilly, 2015) | http://shop.oreilly.com/product/0636920032519.do | Technical Principal at ThoughtWorks | Twitter: @ramalhoorg From kwpolska at gmail.com Sun Sep 15 11:22:32 2019 From: kwpolska at gmail.com (Chris Warrick) Date: Sun, 15 Sep 2019 17:22:32 +0200 Subject: # type: a In-Reply-To: References: Message-ID: On Sun, 15 Sep 2019 at 16:53, Hongyi Zhao wrote: > > Hi, > > In pycharm? when I commented out the following line: > > # type: a > > The pycharm still told me that: > > Unresolved reference 'a' PyCharm interprets PEP 484 type annotations and type comments, as well as PEP 526 variable annotations. The comment you wrote is a type comment that says 'a' is the expected type of something. The 'type:' part is special here, `# foo: a` won?t trigger this warning. PyCharm expects stuff in type comments to exist, because otherwise, there?s no way to know what type you mean. Type comments are now best replaced by Python 3.6+ variable annotations, but the comments are still valid, and can be used in some contexts where annotations are not supported. https://www.python.org/dev/peps/pep-0484/#type-comments -- Chris Warrick PGP: 5EAAEA16 From python at mrabarnett.plus.com Sun Sep 15 11:36:22 2019 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 15 Sep 2019 16:36:22 +0100 Subject: # type: a In-Reply-To: References: Message-ID: <2988fbca-7e02-3026-879e-55838c6f24d2@mrabarnett.plus.com> On 2019-09-15 15:45, Hongyi Zhao wrote: > Hi, > > In pycharm? when I commented out the following line: > > # type: a > > The pycharm still told me that: > > Unresolved reference 'a' > > Till I commented like the following, the warning will disappear: > > # type: # a > > > Why? > PyCharm has comment-based type hints, and: # type: a looks to PyCharm like such a type hint, so it looks for the name 'a', but doesn't find it. Adding the second # stops it looking like a type hint. From spencer.graves at effectivedefense.org Sat Sep 14 09:10:50 2019 From: spencer.graves at effectivedefense.org (Spencer Graves) Date: Sat, 14 Sep 2019 08:10:50 -0500 Subject: TechRepublicDEVELOPERCXO JPMorgan's Athena has 35 million lines of Python code, and won't be updated to Python 3 in time In-Reply-To: <201909140830.06979.gheskett@shentel.net> References: <201909140830.06979.gheskett@shentel.net> Message-ID: On 2019-09-14 07:30, Gene Heskett wrote: > On Saturday 14 September 2019 04:37:14 Larry Martell wrote: > >> On Fri, Sep 13, 2019 at 1:37 PM Skip Montanaro >> >> >> wrote: >>> https://www.techrepublic.com/google-amp/article/jpmorgans-athena-has >>> -35-million-lines-of-python-code-and-wont-be-updated-to-python-3-in-t >>> ime/ >>> >>> I doubt this is unusual, and presume JP Morgan is big enough to >>> handle the change of status, either by managing security releases >>> in-house or relying on third-party releases (say, Anaconda). When I >>> retired from Citadel recently, most Python was still 2.7 (though the >>> group I worked in was well on the way to converting to 3.x, and no >>> new applications were written against 2.7). Bank of America has an >>> enterprise-wide system called Quartz. I wouldn't be surprised if it >>> was still running Python 2.7 (though I don't know for sure). >> Yes Quartz is 2.7. As I?ve said before here, I know a lot of companies >> running large apps in 2.7 and they have no intention of moving to 3. > And I, Larry, have little doubt that the hackers have a hole into a 2.7 > install, all squirreled away, and waiting until 2.7 security support > goes away. It's the nature of the thing. > > They will get hacked. Its like asking if concrete will crack as you are > watching it being poured, will is the wrong question, when is far more > correct. > > And it will cost them trillions in the long haul. The courts, > adjudicating damages, will not be kind to the foot dragger's who think > they are saving money. History sure seems to be pointing in that > direction recently. > > Its a puzzle to me, why so-called sane MBA's cannot understand that the > best defense is spending money on the offense by updateing their > in-house operating code. Or the OS under it. ????? Is anyone interested in contacting these companies -- or the companies from which they buy cybersecurity insurance -- and inviting them to provide paid staff to maintain 2.7 and to offer further offer consulting services to help these clients inventory what they have and how much it would cost to migrate? ????? For example, how much would it cost to write and maintain an emulator for 2.7.16 in 3.7.4? ????? The Python Software Foundation does not want to maintain 2.7 for free anymore, but if there is sufficient demand, they should be thrilled to make a handsome profit off of it -- while providing high quality, good paying jobs for smart Pythonistas. ????? As I'm thinking about it, the companies that provide cybersecurity insurance could be the best points of leverage for this, because they think about these kinds of things all the time. Insurance companies for decades and probably well over 100 years have required their commercial clients to employ night watch crews, who make the rounds of a facility collecting time stamps from different points in the facility, which they provide to insurer(s) in exchange for reduced rates -- on as a condition of getting insurance in the first place.? This is conceptually and practically the same kind of thing. ????? Spencer Graves > Cheers, Gene Heskett From hjp-python at hjp.at Sun Sep 15 16:03:26 2019 From: hjp-python at hjp.at (Peter J. Holzer) Date: Sun, 15 Sep 2019 22:03:26 +0200 Subject: TechRepublicDEVELOPERCXO JPMorgan's Athena has 35 million lines of Python code, and won't be updated to Python 3 in time In-Reply-To: References: <201909140830.06979.gheskett@shentel.net> Message-ID: <20190915200326.GA28682@hjp.at> On 2019-09-14 08:10:50 -0500, Spencer Graves wrote: > ????? As I'm thinking about it, the companies that provide cybersecurity > insurance could be the best points of leverage for this, because they think > about these kinds of things all the time. Insurance companies for decades I wouldn't set my hopes too high. Bruce Schneier recently quoted from https://tylermoore.utulsa.edu/govins20.pdf (which I haven't read yet): | Cyber insurance appears to be a weak form of governanceat present. | Insurers writing cyber insurance focus more on organisational | procedures than technical controls, rarely include basic security | procedures in contracts, and offer discounts that only offer a | marginal incentive to in-vest in security. However, the cost of | external response services is covered, which suggests insurers believe | ex-post responses to be more effective than ex-ante mitiga-tion. | (Alternatively, they can more easily translate the costs associated | with ex-post responses into manageable claims.) 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 torriem at gmail.com Sun Sep 15 17:27:35 2019 From: torriem at gmail.com (Michael Torrie) Date: Sun, 15 Sep 2019 15:27:35 -0600 Subject: What about idea of making a "Pythonic Lisp"...i.e. a Lisp that more closely resembles the syntax of Python? In-Reply-To: <1c3b76f2-7a62-4b84-a681-297ea5967b7d@googlegroups.com> References: <4bf8659c-b04b-4147-9c76-551c2b535bf7@googlegroups.com> <87d0g21fqt.fsf@nightsong.com> <1c3b76f2-7a62-4b84-a681-297ea5967b7d@googlegroups.com> Message-ID: <05e0a21e-32c3-5e81-ec81-cdfa524e4eab@gmail.com> On 9/15/19 9:10 AM, Christian Seberino wrote: > Say if I may ask a tangential question...I've always wondered whether it would be not too hard to compile Python source code to a Lisp like source code? How hard would it be to say compile Python source to Clojure source? I'm sure a compiler could take a non-dynamic subset of Python and transpile it into another language. Or you could implement an interpreter in Clojure. A project called Nuitka turns Python code into a compiled binary. I'm not quite sure how it works since the dynamic parts of Python have to be interpreted at runtime still. From morphex at gmail.com Sun Sep 15 18:07:42 2019 From: morphex at gmail.com (Morten W. Petersen) Date: Mon, 16 Sep 2019 00:07:42 +0200 Subject: What about idea of making a "Pythonic Lisp"...i.e. a Lisp that more closely resembles the syntax of Python? In-Reply-To: <4bf8659c-b04b-4147-9c76-551c2b535bf7@googlegroups.com> References: <4bf8659c-b04b-4147-9c76-551c2b535bf7@googlegroups.com> Message-ID: goto main; Blogging at http://blogologue.com Tweeting at https://twitter.com/blogologue On Instagram https://instagram.com/morphexx s?n. 15. sep. 2019, 03.07 skrev Christian Seberino : > Python is my goto main language. However, sometimes I'm tempted to > play with a Lisp like language just for fun. > > Clojure looks pretty solid but its syntax is different than Python's. > > Since Lisp's make it so easy to modify the language, what about the idea > of developing a few macros to make a modified Clojure dialect that > more closely matched Python keywords and so was more comfy for Pythonistas? > > Chris > -- > https://mail.python.org/mailman/listinfo/python-list > From hongyi.zhao at gmail.com Sun Sep 15 18:48:50 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Sun, 15 Sep 2019 22:48:50 +0000 (UTC) Subject: ``pipenv + pycharm'' refrash/rescan packages index without restart pycharm. Message-ID: Hi, I use pycharm with pipenv for env management. When I do some changes on the packages, say, add/remove some of them. How to let pycharm efrash/ rescan packages index without restart it? From oscar.j.benjamin at gmail.com Sun Sep 15 19:51:12 2019 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 16 Sep 2019 00:51:12 +0100 Subject: itertools product(infinite iterator) hangs In-Reply-To: References: Message-ID: On Sat, 14 Sep 2019 at 03:26, Oscar Benjamin wrote: > > I've been staring at this for a little while: > > from itertools import product > > class Naturals: > def __iter__(self): > i = 1 > while True: > yield i > i += 1 > > N = Naturals() > print(iter(N)) > print(product(N)) # <--- hangs > > When I run the above the call to product hangs but I can't see why. There is already an issue for this: https://bugs.python.org/issue10109 It seems product always consumes all of its iterators entirely before yielding anything (in fact before even iter is called). That seemed surprising to me when looking at the trivial case of a product of one iterable since in *that* case it is clearly unnecessary. I wrote my own versions that get around this. This is a simple version: def iproduct_simple(*iterables): '''Returns the Cartesian product of iterables but doesn't hang for infinite iterators''' if len(iterables) == 0: yield () return first, others = iterables[0], iterables[1:] for f in first: for o in iprod(*others): yield (f,) + o This gives output in normal order for finite inputs and doesn't hang in the case of infinite inputs. However if any of the inputs is infinite iproduct_simple won't actually yield all elements. So I made a more complicated version that yields all possibilities (analogous to enumerating rational numbers): def iproduct(*iterables): '''Returns Cartesian product of iterables. Yields every element eventually''' if len(iterables) == 0: yield () return elif len(iterables) == 1: for e in iterables[0]: yield (e,) elif len(iterables) == 2: for e12 in iproduct2(*iterables): yield e12 else: first, others = iterables[0], iterables[1:] for ef, eo in iproduct2(first, iprod(*others)): yield (ef,) + eo def iproduct2(iterable1, iterable2): '''Cartesian product of two possibly infinite iterables''' it1 = iter(iterable1) it2 = iter(iterable2) elems1 = [] elems2 = [] sentinel = object() def append(it, elems): e = next(it, sentinel) if e is not sentinel: elems.append(e) n = 0 append(it1, elems1) append(it2, elems2) while n <= len(elems1) + len(elems2): for m in range(n-len(elems1)+1, len(elems2)): yield (elems1[n-m], elems2[m]) n += 1 append(it1, elems1) append(it2, elems2) -- Oscar From oscar.j.benjamin at gmail.com Sun Sep 15 19:55:56 2019 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 16 Sep 2019 00:55:56 +0100 Subject: itertools product(infinite iterator) hangs In-Reply-To: <5d7c8622$0$31421$426a34cc@news.free.fr> References: <5d7c8622$0$31421$426a34cc@news.free.fr> Message-ID: On Sat, 14 Sep 2019 at 07:22, ast wrote: > > Le 14/09/2019 ? 04:26, Oscar Benjamin a ?crit : > > > > What am I missing? > > here is a pseudo code for product: > > def product(*args, repeat=1): > # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy > # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111 > pools = [tuple(pool) for pool in args] * repeat > result = [[]] > for pool in pools: > result = [x+[y] for x in result for y in pool] > for prod in result: > yield tuple(prod) > > clearly "tuple(pool)" hangs with an infinite iterable pool Thanks for this response. I also realised this myself after a while. Actually I didn't see your message until I posted my own followup reply because gmail had moved it into spam. Apparently it doesn't like the headers in your email for some reason: "Gmail could not verify that it actually came from gmail.com. Avoid clicking links, downloading attachments or replying with personal information." Oscar From sharan.basappa at gmail.com Sun Sep 15 22:20:52 2019 From: sharan.basappa at gmail.com (Sharan Basappa) Date: Sun, 15 Sep 2019 19:20:52 -0700 (PDT) Subject: kmeans clustering Message-ID: <8ab44614-79bb-42de-bd9f-e890c78b31a0@googlegroups.com> Can someone please help me to clarify the different between fit and predict functions of kmeans? From pankaj.jangid at gmail.com Mon Sep 16 02:58:23 2019 From: pankaj.jangid at gmail.com (Pankaj Jangid) Date: Mon, 16 Sep 2019 12:28:23 +0530 Subject: kmeans clustering References: <8ab44614-79bb-42de-bd9f-e890c78b31a0@googlegroups.com> Message-ID: Sharan Basappa writes: > Can someone please help me to clarify the different between fit and > predict functions of kmeans? > What is the significance of `predict' in K-means? It is an unsupervised clustering algorithm. My intuition says that the cluster composition itself might change if you add a new example and re-run K-means. On the other hand if you don't want to change the cluster compositions and just want to find a cluster for a new example then it is not K-means. In that case it is Knearest classifier. -- Pankaj Jangid From smilesonisamal at gmail.com Mon Sep 16 02:58:25 2019 From: smilesonisamal at gmail.com (Pradeep Patra) Date: Mon, 16 Sep 2019 12:28:25 +0530 Subject: numpy results in segmentation fault In-Reply-To: References: Message-ID: Yes it is crashing in the hackerrank site and the testcases fails with segmentation fault. I tried to install numpy with 3.7.3 and it is for some reason not working and after import when I run import numpy at python console and press enter I get >>? i,e its not working properly. Can you please help letting me know the python and numpy compatibility matrix or I am missing anything? I tried some of the numpy code from the other github and it also fails with the segmentation fault :-(. I am guessing some numpy version compatility issue or some environment issue. On Thu, Sep 12, 2019 at 8:00 PM Thomas Jollans wrote: > On 12/09/2019 15.53, Pradeep Patra wrote: > > Hi , > > > > I was trying to solve the hackerrank and was using python 3.7.x. > > https://www.hackerrank.com/challenges/np-concatenate/problem > > > > While running the code sometimes I get success result and sometimes it > > fails with "Segmentation Fault" at Hacker rank UI. I dont have any clue > why > > the code is crashing ? Does anyone have any idea? > > > Are you sure it's your code that's crashing, and not something beyond > your control? (Such as the software that is starting Python for you) > Does it depend on the input? Can you reproduce the issue in a controlled > environment (i.e. on your own PC)? > > > > > > Regards > > Pradeep > > > > import numpy > > > > n,m,p = map(int,input().split()) > > tgt_arr1 = [] > > for i in range(n): > > row = list(map(int,input().split())) > > tgt_arr1.append(row) > > tgt_arr2 = [] > > for j in range(m): > > row = list(map(int,input().split())) > > tgt_arr2.append(row) > > > > num_arr1 = numpy.array(tgt_arr1,int) > > num_arr2 = numpy.array(tgt_arr2,int) > > > > print(numpy.concatenate((num_arr1,num_arr2),axis=0)) > > > -- > https://mail.python.org/mailman/listinfo/python-list > From skip.montanaro at gmail.com Mon Sep 16 07:56:31 2019 From: skip.montanaro at gmail.com (Skip Montanaro) Date: Mon, 16 Sep 2019 06:56:31 -0500 Subject: OT: Using a fake Gmail address is probably not a good idea Message-ID: (I would have sent this off-list, but for obvious reasons I couldn't.) Mails for someone here who goes by the handle "ast" with a fake address of none at gmail.com keep landing in my Gmail spam folder. I suspect the same is true for all people subscribed to python-list who use Gmail. Gmail (correctly, I think) can't verify that the mail I received actually originated there. It appears this user is actually posting via Usenet through the server news.free.fr (apparently ProxAd). I check my spam folder regularly for false positives, but I'm sure many people don't. Whoever you are, ast, you might want to reconsider your choice of fake return address. Skip From tjol at tjol.eu Mon Sep 16 08:02:25 2019 From: tjol at tjol.eu (Thomas Jollans) Date: Mon, 16 Sep 2019 14:02:25 +0200 Subject: Fwd: numpy results in segmentation fault In-Reply-To: References: Message-ID: <67402bc7-31a1-ba86-bf1b-7708454fcca1@tjol.eu> Please reply on-list. (both of you) -------- Forwarded Message -------- Subject: Re: numpy results in segmentation fault Date: Mon, 16 Sep 2019 17:04:57 +0530 From: Test Bot To: Pradeep Patra CC: Thomas Jollans Firstly, in response to this " I tried to install numpy with 3.7.3 and it is for some reason not working and after import when I run import numpy at python console and press enter I get >>? i,e its not working properly. " the >> prompt after import numpy signifies that the numpy module has been loaded and is available?in the session. Can you please provide the traceback you are getting along with the input. PS - Also, you have some coins like thing on hackerrank I guess to reveal the test cases, in case everything else fails. On Mon, Sep 16, 2019 at 3:08 PM Pradeep Patra > wrote: Yes it is crashing in the hackerrank site and the testcases fails with segmentation fault. I tried to install numpy with 3.7.3 and it is for some reason not working and after import when I run import numpy at python console and press enter I get >>? i,e its not working properly. Can you please help letting me know the python and numpy compatibility matrix or I am missing anything? I tried some of the numpy code from the other github and it also fails with the segmentation fault :-(. I am guessing some numpy version compatility issue or some environment issue. On Thu, Sep 12, 2019 at 8:00 PM Thomas Jollans > wrote: > On 12/09/2019 15.53, Pradeep Patra wrote: > > Hi , > > > > I was trying to solve the hackerrank and was using python 3.7.x. > > https://www.hackerrank.com/challenges/np-concatenate/problem > > > > While running the code sometimes I get success result and sometimes it > > fails with "Segmentation Fault" at Hacker rank UI. I dont have any clue > why > > the code is crashing ? Does anyone have any idea? > > > Are you sure it's your code that's crashing, and not something beyond > your control? (Such as the software that is starting Python for you) > Does it depend on the input? Can you reproduce the issue in a controlled > environment (i.e. on your own PC)? > > > > > > Regards > > Pradeep > > > > import numpy > > > > n,m,p = map(int,input().split()) > > tgt_arr1 = [] > > for i in range(n): > >? ? ?row = list(map(int,input().split())) > >? ? ?tgt_arr1.append(row) > > tgt_arr2 = [] > > for j in range(m): > >? ? ?row = list(map(int,input().split())) > >? ? ?tgt_arr2.append(row) > > > > num_arr1 = numpy.array(tgt_arr1,int) > > num_arr2 = numpy.array(tgt_arr2,int) > > > > print(numpy.concatenate((num_arr1,num_arr2),axis=0)) > > > -- > https://mail.python.org/mailman/listinfo/python-list > -- https://mail.python.org/mailman/listinfo/python-list From hongyi.zhao at gmail.com Mon Sep 16 08:21:46 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Mon, 16 Sep 2019 12:21:46 +0000 (UTC) Subject: What is the Difference Between quit() and exit() commands in Python? Message-ID: What is the Difference Between quit() and exit() commands in Python? From __peter__ at web.de Mon Sep 16 08:51:19 2019 From: __peter__ at web.de (Peter Otten) Date: Mon, 16 Sep 2019 14:51:19 +0200 Subject: What is the Difference Between quit() and exit() commands in Python? References: Message-ID: Hongyi Zhao wrote: > What is the Difference Between quit() and exit() commands in Python? They are instances of the same type >>> import inspect >>> type(quit) is type(exit) True >>> print(inspect.getsource(type(quit))) class Quitter(object): def __init__(self, name, eof): self.name = name self.eof = eof def __repr__(self): return 'Use %s() or %s to exit' % (self.name, self.eof) def __call__(self, code=None): # Shells like IDLE catch the SystemExit, but listen when their # stdin wrapper is closed. try: sys.stdin.close() except: pass raise SystemExit(code) There is no difference, except for the name attribute and the repr() text: >>> exit.name, exit.eof, exit ('exit', 'Ctrl-D (i.e. EOF)', Use exit() or Ctrl-D (i.e. EOF) to exit) >>> quit.name, quit.eof, quit ('quit', 'Ctrl-D (i.e. EOF)', Use quit() or Ctrl-D (i.e. EOF) to exit) From none at gmail.com Mon Sep 16 09:01:26 2019 From: none at gmail.com (ast) Date: Mon, 16 Sep 2019 15:01:26 +0200 Subject: Strange Class definition Message-ID: <5d7f87a7$0$15487$426a74cc@news.free.fr> Hello Following syntax doesn't generate any errors: >>> foo=0 >>> Class Foo: foo But class Foo seems empty Is it equivalent to ? >>> class Foo: pass From eryksun at gmail.com Mon Sep 16 09:11:07 2019 From: eryksun at gmail.com (Eryk Sun) Date: Mon, 16 Sep 2019 08:11:07 -0500 Subject: What is the Difference Between quit() and exit() commands in Python? In-Reply-To: References: Message-ID: On 9/16/19, Hongyi Zhao wrote: > > What is the Difference Between quit() and exit() commands in Python? They're different instances of the Quitter class, which is available if site.py is imported (i.e. not with the -S command-line option). They're created by site.setquit(): def setquit(): """Define new builtins 'quit' and 'exit'. These are objects which make the interpreter exit when called. The repr of each object contains a hint at how it works. """ if os.sep == '\\': eof = 'Ctrl-Z plus Return' else: eof = 'Ctrl-D (i.e. EOF)' builtins.quit = _sitebuiltins.Quitter('quit', eof) builtins.exit = _sitebuiltins.Quitter('exit', eof) exit(code) or quit(code) closes sys.stdin and raises SystemExit(code): >>> quit.__class__ >>> print(inspect.getsource(type(quit))) class Quitter(object): def __init__(self, name, eof): self.name = name self.eof = eof def __repr__(self): return 'Use %s() or %s to exit' % (self.name, self.eof) def __call__(self, code=None): # Shells like IDLE catch the SystemExit, but listen when their # stdin wrapper is closed. try: sys.stdin.close() except: pass raise SystemExit(code) For example: >>> try: ... quit(42) ... except BaseException as e: ... print(repr(e)) ... SystemExit(42,) >>> sys.stdin.closed True Alternatively, sys.exit is a builtin function that's equivalent to `raise SystemExit(code)` but does not close sys.stdin. For example: >>> try: ... sys.exit(42) ... except BaseException as e: ... print(repr(e)) ... SystemExit(42,) >>> sys.stdin.closed False From __peter__ at web.de Mon Sep 16 11:46:59 2019 From: __peter__ at web.de (Peter Otten) Date: Mon, 16 Sep 2019 17:46:59 +0200 Subject: Strange Class definition References: <5d7f87a7$0$15487$426a74cc@news.free.fr> Message-ID: ast wrote: > Hello > > Following syntax doesn't generate any errors: > > >>> foo=0 > >>> Class Foo: > foo > > But class Foo seems empty > > Is it equivalent to ? > > >>> class Foo: > pass The resulting class is equivalent, but the expression `foo` is actually evaluated during class creation. You can easily see this when you replace foo with a print call, say: >>> for foo in "first", "second": ... class A: print(foo) ... first second If you compare the byte code you may note a small difference to def f(): foo >>> import dis >>> c = compile("class A: foo", ">> dis.dis(c) 1 0 LOAD_BUILD_CLASS 1 LOAD_CONST 0 () 4 LOAD_CONST 1 ('A') 7 MAKE_FUNCTION 0 10 LOAD_CONST 1 ('A') 13 CALL_FUNCTION 2 (2 positional, 0 keyword pair) 16 STORE_NAME 0 (A) 19 LOAD_CONST 2 (None) 22 RETURN_VALUE >>> c.co_consts[0] >>> dis.dis(c.co_consts[0]) 1 0 LOAD_NAME 0 (__name__) 3 STORE_NAME 1 (__module__) 6 LOAD_CONST 0 ('A') 9 STORE_NAME 2 (__qualname__) The relevant instruction: 12 LOAD_NAME 3 (foo) 15 POP_TOP 16 LOAD_CONST 1 (None) 19 RETURN_VALUE For comparison a function: >>> def f(): foo ... >>> dis.dis(f) 1 0 LOAD_GLOBAL 0 (foo) 3 POP_TOP 4 LOAD_CONST 0 (None) 7 RETURN_VALUE In the class body LOAD_NAME is used instead of LOAD_GLOBAL. This allows dynamical scoping >>> foo = "outer" >>> class A: ... print(foo) ... foo = "inner" ... print(foo) ... outer inner >>> A.foo 'inner' whereas in the function you get an error: >>> def f(): ... print(foo) ... foo = "inner" ... print(foo) ... >>> f() Traceback (most recent call last): File "", line 1, in File "", line 2, in f UnboundLocalError: local variable 'foo' referenced before assignment From onlinejudge95 at gmail.com Mon Sep 16 12:19:11 2019 From: onlinejudge95 at gmail.com (Test Bot) Date: Mon, 16 Sep 2019 21:49:11 +0530 Subject: numpy results in segmentation fault In-Reply-To: <67402bc7-31a1-ba86-bf1b-7708454fcca1@tjol.eu> References: <67402bc7-31a1-ba86-bf1b-7708454fcca1@tjol.eu> Message-ID: Firstly, in response to this " I tried to install numpy with 3.7.3 and it is for some reason not working and after import when I run import numpy at python console and press enter I get >>? i,e its not working properly. " the >> prompt after import numpy signifies that the numpy module has been loaded and is available in the session. Can you please provide the traceback you are getting along with the input. PS - Also, you have some coins like thing on hackerrank I guess to reveal the test cases, in case everything else fails. On Mon, Sep 16, 2019 at 5:32 PM Thomas Jollans wrote: > Please reply on-list. (both of you) > > > -------- Forwarded Message -------- > Subject: Re: numpy results in segmentation fault > Date: Mon, 16 Sep 2019 17:04:57 +0530 > From: Test Bot > To: Pradeep Patra > CC: Thomas Jollans > > Firstly, in response to this > " > I tried to install numpy with 3.7.3 and it is for some > reason not working and after import when I run import numpy at python > console and press enter I get >>? i,e its not working properly. > " > > the >> prompt after import numpy signifies that the numpy module has been > loaded and is available in the session. > > Can you please provide the traceback you are getting along with the input. > > PS - Also, you have some coins like thing on hackerrank I guess to reveal > the test cases, in case everything else fails. > > On Mon, Sep 16, 2019 at 3:08 PM Pradeep Patra > wrote: > >> Yes it is crashing in the hackerrank site and the testcases fails with >> segmentation fault. I tried to install numpy with 3.7.3 and it is for some >> reason not working and after import when I run import numpy at python >> console and press enter I get >>? i,e its not working properly. >> >> Can you please help letting me know the python and numpy compatibility >> matrix or I am missing anything? >> >> I tried some of the numpy code from the other github and it also fails >> with >> the segmentation fault :-(. I am guessing some numpy version compatility >> issue or some environment issue. >> >> On Thu, Sep 12, 2019 at 8:00 PM Thomas Jollans wrote: >> >> > On 12/09/2019 15.53, Pradeep Patra wrote: >> > > Hi , >> > > >> > > I was trying to solve the hackerrank and was using python 3.7.x. >> > > https://www.hackerrank.com/challenges/np-concatenate/problem >> > > >> > > While running the code sometimes I get success result and sometimes it >> > > fails with "Segmentation Fault" at Hacker rank UI. I dont have any >> clue >> > why >> > > the code is crashing ? Does anyone have any idea? >> > >> > >> > Are you sure it's your code that's crashing, and not something beyond >> > your control? (Such as the software that is starting Python for you) >> > Does it depend on the input? Can you reproduce the issue in a controlled >> > environment (i.e. on your own PC)? >> > >> > >> > > >> > > Regards >> > > Pradeep >> > > >> > > import numpy >> > > >> > > n,m,p = map(int,input().split()) >> > > tgt_arr1 = [] >> > > for i in range(n): >> > > row = list(map(int,input().split())) >> > > tgt_arr1.append(row) >> > > tgt_arr2 = [] >> > > for j in range(m): >> > > row = list(map(int,input().split())) >> > > tgt_arr2.append(row) >> > > >> > > num_arr1 = numpy.array(tgt_arr1,int) >> > > num_arr2 = numpy.array(tgt_arr2,int) >> > > >> > > print(numpy.concatenate((num_arr1,num_arr2),axis=0)) >> > >> > >> > -- >> > https://mail.python.org/mailman/listinfo/python-list >> > >> -- >> https://mail.python.org/mailman/listinfo/python-list >> > From rosuav at gmail.com Sun Sep 15 15:51:48 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 16 Sep 2019 05:51:48 +1000 Subject: TechRepublicDEVELOPERCXO JPMorgan's Athena has 35 million lines of Python code, and won't be updated to Python 3 in time In-Reply-To: References: <201909140830.06979.gheskett@shentel.net> Message-ID: On Mon, Sep 16, 2019 at 4:38 AM Spencer Graves wrote: > > Is anyone interested in contacting these companies -- or the > companies from which they buy cybersecurity insurance -- and inviting > them to provide paid staff to maintain 2.7 and to offer further offer > consulting services to help these clients inventory what they have and > how much it would cost to migrate? > > > For example, how much would it cost to write and maintain an > emulator for 2.7.16 in 3.7.4? > > > The Python Software Foundation does not want to maintain 2.7 for > free anymore, but if there is sufficient demand, they should be thrilled > to make a handsome profit off of it -- while providing high quality, > good paying jobs for smart Pythonistas. > That's not really the PSF's job, but if you're looking at this from a viewpoint of "wouldn't it be nice if there were jobs available supporting 2.7", then do the rounds of the commercial Python distributors. Anaconda, Enthought, ActiveState, and possibly folks like Red Hat, have an interest in making money off Python, and they're not in any way obliged to stop working with Py2 as of 2020. Each company is free to make its own commercial decision regarding which versions they'll support. ChrisA From wesley at freenetmail.de Mon Sep 16 08:43:38 2019 From: wesley at freenetmail.de (wesley at freenetmail.de) Date: Mon, 16 Sep 2019 12:43:38 GMT Subject: What is the Difference Between quit() and exit() commands in Python? In-Reply-To: References: Message-ID: <272cbfd2cfd672f04f4ddbd056fb9ea0@api.mail.freenet.de> Hi exit (http://docs.python.org/2/library/constants.html#exit" rel="noreferrer) is an alias for quit (or vice-versa). They exist together simply to make Python more user-friendly. please refer: https://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used (https://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used) > -----ursprüngliche Nachricht----- > Von: hongyi.zhao at gmail.com (mailto:hongyi.zhao at gmail.com) > Gesendet: 16.09.2019 14:35 Uhr > An: python-list at python.org (mailto:python-list at python.org) > Betreff: What is the Difference Between quit() and exit() commands in Python? > What is the Difference Between quit() and exit() commands in Python? > -- > mail.python.org/mailman/listinfo/python-list (https://mail.python.org/mailman/listinfo/python-list" target="_blank" rel="noopener) > -----ursprüngliche Nachricht Ende----- From max at zettlmeissl.de Mon Sep 16 10:37:56 2019 From: max at zettlmeissl.de (=?UTF-8?Q?Max_Zettlmei=C3=9Fl?=) Date: Mon, 16 Sep 2019 16:37:56 +0200 Subject: OT: Using a fake Gmail address is probably not a good idea In-Reply-To: References: Message-ID: On Mon, Sep 16, 2019 at 1:56 PM Skip Montanaro wrote: > Mails for someone here who goes by the handle "ast" with a fake > address of none at gmail.com keep landing in my Gmail spam folder. I > suspect the same is true for all people subscribed to python-list who > use Gmail. Gmail (correctly, I think) can't verify that the mail I > received actually originated there. It is true for any server that is applying DMARC policies. And having to deal with his messages is also very annoying to me. Ast should use a proper invalid email address (E.g. anything ending in .invalid, but there are also other reserved domains for such purposes.) if he does not want to reveal his real address, instead of making up a possibly valid address. From tintin at free.fr Mon Sep 16 17:32:05 2019 From: tintin at free.fr (Tintin) Date: Mon, 16 Sep 2019 23:32:05 +0200 Subject: Something faster than turtle Message-ID: <5d7fff55$0$20331$426a74cc@news.free.fr> Hi, I'm novice in Python. I'm trying to draw with turtle but it's really slow (even with speed("fastest")). Comparing to Scratch, it's really slow. 1/ Are there solutions to get things faster ? 2/ Are there any other tools such as turtle but (really) faster ? Thanks. From jasonanyilian at gmail.com Mon Sep 16 21:31:48 2019 From: jasonanyilian at gmail.com (CrazyVideoGamez) Date: Mon, 16 Sep 2019 18:31:48 -0700 (PDT) Subject: re not working Message-ID: <55b5508a-510e-4d6f-9428-8bf496ac7f8a@googlegroups.com> For some reason these are different: pattern = r'[0-9]{4,6}' And pattern2 = r'[0-9][0-9][0-9][0-9]([0-9]){0,2}' And when I try to match them import re re.search(pattern, '1234') and import re re.search(pattern2, '1234') are different. Help? From arequipeno at gmail.com Mon Sep 16 21:37:53 2019 From: arequipeno at gmail.com (Ian Pilcher) Date: Mon, 16 Sep 2019 20:37:53 -0500 Subject: Irritating bytearray behavior Message-ID: I am using a bytearray to construct a very simple message, that will be sent across the network. The message should always be 20 bytes: 2 bytes - address family (AF_INET or AF_INET6) - network byte order 2 bytes - (padding) 4 or 16 bytes - IP address The size of the IP address is dependent on whether it is an IPv4 address (4 bytes) or an IPv6 address (16 bytes). In the IPv4 case, it should be followed by 12 bytes of padding, to keep the message size consistent. Na?vely, I thought that I could do this: ip = ipaddress.ip_address(unicode(addr)) msg = bytearray(20) msg[1] = socket.AF_INET if ip.version == 4 else socket.AF_INET6 msg[4:] = ip.packed sock.sendto(msg, dest) This doesn't work in the IPv4 case, because the bytearray gets truncated to only 8 bytes (4 bytes plus the size of ip.packed). Is there a way to avoid this behavior copy the contents of ip.packed into the bytearray without changing its size? TIA -- ======================================================================== Ian Pilcher arequipeno at gmail.com -------- "I grew up before Mark Zuckerberg invented friendship" -------- ======================================================================== From python at mrabarnett.plus.com Mon Sep 16 22:01:32 2019 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 17 Sep 2019 03:01:32 +0100 Subject: re not working In-Reply-To: <55b5508a-510e-4d6f-9428-8bf496ac7f8a@googlegroups.com> References: <55b5508a-510e-4d6f-9428-8bf496ac7f8a@googlegroups.com> Message-ID: <07a5f84c-dee0-3960-b192-4bf0fd78078d@mrabarnett.plus.com> On 2019-09-17 02:31, CrazyVideoGamez wrote: > For some reason these are different: > > pattern = r'[0-9]{4,6}' > > And > > pattern2 = r'[0-9][0-9][0-9][0-9]([0-9]){0,2}' > > And when I try to match them > > import re > re.search(pattern, '1234') > > and > > import re > re.search(pattern2, '1234') > > are different. Help? > Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import re >>> pattern = r'[0-9]{4,6}' >>> pattern2 = r'[0-9][0-9][0-9][0-9]([0-9]){0,2}' >>> re.search(pattern, '1234') >>> re.search(pattern2, '1234') They look the same to me. From cs at cskk.id.au Mon Sep 16 22:09:35 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 17 Sep 2019 12:09:35 +1000 Subject: Irritating bytearray behavior In-Reply-To: References: Message-ID: <20190917020935.GA52120@cskk.homeip.net> On 16Sep2019 20:37, Ian Pilcher wrote: > msg[4:] = ip.packed > sock.sendto(msg, dest) > >This doesn't work in the IPv4 case, because the bytearray gets truncated >to only 8 bytes (4 bytes plus the size of ip.packed). > >Is there a way to avoid this behavior copy the contents of ip.packed >into the bytearray without changing its size? Sure: msg[4:4+len(ip.packed)] = ip.packed What you did was ask to replace all the bytes from 4 onward. What you should do is replace just the correct bytes. Cheers, Cameron Simpson From python at mrabarnett.plus.com Mon Sep 16 22:13:15 2019 From: python at mrabarnett.plus.com (MRAB) Date: Tue, 17 Sep 2019 03:13:15 +0100 Subject: Irritating bytearray behavior In-Reply-To: References: Message-ID: On 2019-09-17 02:37, Ian Pilcher wrote: > I am using a bytearray to construct a very simple message, that will be > sent across the network. The message should always be 20 bytes: > > 2 bytes - address family (AF_INET or AF_INET6) - network byte order > 2 bytes - (padding) > 4 or 16 bytes - IP address > > The size of the IP address is dependent on whether it is an IPv4 address > (4 bytes) or an IPv6 address (16 bytes). In the IPv4 case, it should be > followed by 12 bytes of padding, to keep the message size consistent. > > Na?vely, I thought that I could do this: > > ip = ipaddress.ip_address(unicode(addr)) > msg = bytearray(20) > msg[1] = socket.AF_INET if ip.version == 4 else socket.AF_INET6 > msg[4:] = ip.packed > sock.sendto(msg, dest) > > This doesn't work in the IPv4 case, because the bytearray gets truncated > to only 8 bytes (4 bytes plus the size of ip.packed). > > Is there a way to avoid this behavior copy the contents of ip.packed > into the bytearray without changing its size? > The truncation you're seeing is perfectly normal. It's the same as for lists. Try this: msg[4 : 4 + len(ip.packed)] = ip.packed Alternatively, you could build the message a bytestrings and then pad with the .ljust method. From pahome.chen at mirlab.org Tue Sep 17 00:13:57 2019 From: pahome.chen at mirlab.org (lampahome) Date: Tue, 17 Sep 2019 12:13:57 +0800 Subject: python3 subprocess run sudo cmd in remote failed Message-ID: Hello, I use python3.5 and found no way to solve this problem >from subprocess import Popen, PIPE >ps = Popen('ssh -o \'StrictHostKeyChecking no\' hello at 192.168.80.11 \'sudo sysctl -w vm.drop_caches=3\', stdin=PIPE, stdout=PIPE, stderr=PIPE, bufsize=0, shell=True) > hello at 192.168.80.11's password: what I tried many times like enter password, but it failed. I just want to use ps.stdin.write(password) to send password, but it always jump password prompt immediately. How to solve this From cs at cskk.id.au Tue Sep 17 00:34:56 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 17 Sep 2019 14:34:56 +1000 Subject: python3 subprocess run sudo cmd in remote failed In-Reply-To: References: Message-ID: <20190917043456.GA81627@cskk.homeip.net> On 17Sep2019 12:13, lampahome wrote: >Hello, I use python3.5 and found no way to solve this problem > >>from subprocess import Popen, PIPE >>ps = Popen('ssh -o \'StrictHostKeyChecking no\' hello at 192.168.80.11 \'sudo >sysctl -w vm.drop_caches=3\', stdin=PIPE, stdout=PIPE, stderr=PIPE, >bufsize=0, shell=True) >> hello at 192.168.80.11's password: > >what I tried many times like enter password, but it failed. >I just want to use ps.stdin.write(password) to send password, but it always >jump password prompt immediately. Well, there's a Python library called "paramiko" which implements ssh. That might help. But really you should avoid using password authentication altogether. Generate a public/private keypair using ssh-keygen and install the public half on the target machine. Then all you need to do is to deal with sudo's password. Note also that since stdin and stdout are pipes and not the terminal then ssh will not be interactive, and will not allocate a tty at the far end either. You can get ssh to open a remote tty with the -t option. But I suspect you don't want stdin=PIPE or stdout=PIPE at all. Why are they there? Also, try doing this without shell=True - it is an invitation for mistakes and shell injection (depending on your uses). Cheers, Cameron Simpson From pahome.chen at mirlab.org Tue Sep 17 01:02:18 2019 From: pahome.chen at mirlab.org (lampahome) Date: Tue, 17 Sep 2019 13:02:18 +0800 Subject: python3 subprocess run sudo cmd in remote failed In-Reply-To: <20190917043456.GA81627@cskk.homeip.net> References: <20190917043456.GA81627@cskk.homeip.net> Message-ID: > > Well, there's a Python library called "paramiko" which implements ssh. > That might help. > > Later I will try lol. > Note also that since stdin and stdout are pipes and not the terminal > then ssh will not be interactive, and will not allocate a tty at the far > end either. You can get ssh to open a remote tty with the -t option. > > But I suspect you don't want stdin=PIPE or stdout=PIPE at all. Why are > they there? > > I thought I can use ps.stdin.write(password), so I make stdin and stdout be pipe as input and output. Here are I tried: >from subprocess import Popen, PIPE >ps = Popen('ssh -o \'StrictHostKeyChecking no\' hello at 192.168.80.11 \'sudo sysctl -w vm.drop_caches=3\', shell=True) > hello at 192.168.80.11's password: >from subprocess import Popen, PIPE >ps = Popen(['ssh', '-o \'StrictHostKeyChecking no\'', ' hello at 192.168.80.11', '\'sudo sysctl -w vm.drop_caches=3\'']) > hello at 192.168.80.11's password: It always prompt immediately, that make me hard to enter password. Maybe I should try paramiko... From cs at cskk.id.au Tue Sep 17 01:24:11 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 17 Sep 2019 15:24:11 +1000 Subject: python3 subprocess run sudo cmd in remote failed In-Reply-To: References: Message-ID: <20190917052411.GA26000@cskk.homeip.net> On 17Sep2019 13:02, lampahome wrote: >> Note also that since stdin and stdout are pipes and not the terminal >> then ssh will not be interactive, and will not allocate a tty at the far >> end either. You can get ssh to open a remote tty with the -t option. >> >> But I suspect you don't want stdin=PIPE or stdout=PIPE at all. Why are >> they there? > > I thought I can use ps.stdin.write(password), so I make stdin and > stdout be pipe as input and output. The trouble here is that ssh will only accept a password from a terminal. As soon as you connect a pipe it refuses to prompt. This is partly security (having a terminal is a proxy for "talking to a human"), and partly because ssh normally passes stdin to the remote process once authentication is complete, so things get fiddly. You can give it a terminal by obtaining a pty and associating the subprocess with that. You could install the pexpect module with "pip install pexpect" and use that to manage this interaction. See the docs: https://pexpect.readthedocs.io/en/stable/ for further info. However, I repeat my recommendation to use a keypair for the authentication, as it avoids needing interactive passwords (and having your programme know the password has its own suite of problems to do with where that password comes from). >Here are I tried: >>from subprocess import Popen, PIPE >>ps = Popen('ssh -o \'StrictHostKeyChecking no\' hello at 192.168.80.11 \'sudo >sysctl -w vm.drop_caches=3\', shell=True) >> hello at 192.168.80.11's password: > >>from subprocess import Popen, PIPE >>ps = Popen(['ssh', '-o \'StrictHostKeyChecking no\'', ' >hello at 192.168.80.11', '\'sudo sysctl -w vm.drop_caches=3\'']) >> hello at 192.168.80.11's password: > >It always prompt immediately, that make me hard to enter password. Well ssh will be connected to your terminal. Do things work if you hand type the password at that point? > Maybe I should try paramiko... Or pexpect. But use a keypair - it will simplify your life, and generally be far more secure anyway. Cheers, Cameron Simpson From rosuav at gmail.com Tue Sep 17 02:11:56 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 17 Sep 2019 16:11:56 +1000 Subject: python3 subprocess run sudo cmd in remote failed In-Reply-To: <20190917052411.GA26000@cskk.homeip.net> References: <20190917052411.GA26000@cskk.homeip.net> Message-ID: On Tue, Sep 17, 2019 at 3:25 PM Cameron Simpson wrote: > However, I repeat my recommendation to use a keypair for the > authentication, as it avoids needing interactive passwords (and having > your programme know the password has its own suite of problems to do > with where that password comes from). Agreed; using a password that's embedded into the script is worse than useless. The same goes for sudo passwords, if that's a thing; arrange it so the user that you SSH in as has the power to run that command without a password (you can do that in the sudoers file, even if it needs a password for all other usage). If necessary, create a dedicated SSH keypair *just* for this script. It's still easier to protect an SSH private key than a password. ChrisA From tintin at free.fr Tue Sep 17 03:05:58 2019 From: tintin at free.fr (Tintin) Date: Tue, 17 Sep 2019 09:05:58 +0200 Subject: Something faster than turtle In-Reply-To: References: <5d7fff55$0$20331$426a74cc@news.free.fr> Message-ID: <5d8085d7$0$15508$426a34cc@news.free.fr> Thank you very much Stefan, I'll have a look at it. Le 17/09/2019 ? 01:37, Stefan Ram a ?crit?: > tracer( 0 )/update() From hongyi.zhao at gmail.com Tue Sep 17 06:41:04 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Tue, 17 Sep 2019 10:41:04 +0000 (UTC) Subject: Using a usb to boot and install the Debian os on my harddisk. Message-ID: Hi, Is some python tools to make a bootable usb and then using it to install the following dvd-iso into my harddisk: https://cdimage.debian.org/debian-cd/current/amd64/iso-dvd/debian-10.1.0- amd64-DVD-1.iso Regards From nospam_2019 at efbe.prima.de Tue Sep 17 07:09:00 2019 From: nospam_2019 at efbe.prima.de (nospam_2019 at efbe.prima.de) Date: Tue, 17 Sep 2019 13:09:00 +0200 Subject: Using a usb to boot and install the Debian os on my harddisk. In-Reply-To: References: Message-ID: Am 17.09.19 um 12:41 schrieb Hongyi Zhao: > Hi, > > Is some python tools to make a bootable usb and then using it to install > the following dvd-iso into my harddisk: > > https://cdimage.debian.org/debian-cd/current/amd64/iso-dvd/debian-10.1.0- > amd64-DVD-1.iso No python tool needed, just copy the iso onto the usbstick. https://www.debian.org/releases/stable/installmanual From piet-l at vanoostrum.org Tue Sep 17 07:25:27 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Tue, 17 Sep 2019 13:25:27 +0200 Subject: not working References: <55b5508a-510e-4d6f-9428-8bf496ac7f8a@googlegroups.com> Message-ID: ram at zedat.fu-berlin.de (Stefan Ram) writes: > Supersedes: > > MRAB writes: >> >>> import re >> >>> pattern = r'[0-9]{4,6}' >> >>> pattern2 = r'[0-9][0-9][0-9][0-9]([0-9]){0,2}' >> >>> re.search(pattern, '1234') >> >> >>> re.search(pattern2, '1234') >> >>They look the same to me. > > |>>> import re > |>>> pattern = r'[0-9]{4,6}' > |>>> pattern2 = r'[0-9][0-9][0-9][0-9]([0-9]){0,2}' > |>>> re.search( pattern, '1234' ).group( 1 ) > |IndexError: no such group > |>>> re.search( pattern2, '1234' ).group( 1 ) > |>>> > The second pattern has parentheses, hence a group. The first doesn't. -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From grant.b.edwards at gmail.com Tue Sep 17 10:18:18 2019 From: grant.b.edwards at gmail.com (Grant Edwards) Date: Tue, 17 Sep 2019 14:18:18 -0000 (UTC) Subject: Irritating bytearray behavior References: Message-ID: On 2019-09-17, Ian Pilcher wrote: > I am using a bytearray to construct a very simple message, that will be > sent across the network. The message should always be 20 bytes: > > 2 bytes - address family (AF_INET or AF_INET6) - network byte order > 2 bytes - (padding) > 4 or 16 bytes - IP address You probably already know this and have rejected it for some reason, but the struct module is very useful for this sort of thing... -- Grant Edwards grant.b.edwards Yow! I wonder if I should at put myself in ESCROW!! gmail.com From hongyi.zhao at gmail.com Tue Sep 17 11:09:04 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Tue, 17 Sep 2019 15:09:04 +0000 (UTC) Subject: Obtain the file's path. Message-ID: See the following two methods for obtaining the file's path: os.path.realpath(file) or os.path.abspath(os.path.expanduser(file)) Which is more robust? From ekopalypse at gmail.com Tue Sep 17 12:45:51 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Tue, 17 Sep 2019 09:45:51 -0700 (PDT) Subject: Python3.7 singleton is not unique anymore Message-ID: <8d99417c-bdd1-48e9-91ca-10c7c49611ae@googlegroups.com> Using the following code in Python3.7 doesn't make FOO to be singleton anymore. import sys class Singleton(type): _instances = {} def __call__(cls, *args, **kwargs): if cls not in cls._instances: cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) return cls._instances[cls] if sys.version_info.major == 2: class FOO(): __metaclass__ = Singleton def __init__(self): self.id = None def setid(self, id): self.id = id def getid(self): return self.id else: class FOO(): def __init__(self, metaclass=Singleton): self.id = None def setid(self, id): self.id = id def getid(self): return self.id x1 = FOO() x1.setid(1) x2 = FOO() x2.setid(2) x = x1 y = FOO() print(x1, x1.id) print(x2, x2.id) print(x, x.id) print(y, y.id) python2 results in something like <__main__.FOO object at 0x0000000007D9C978>, 2 <__main__.FOO object at 0x0000000007D9C978>, 2 <__main__.FOO object at 0x0000000007D9C978>, 2 <__main__.FOO object at 0x0000000007D9C978>, 2 whereas python3.7 result in <__main__.FOO object at 0x00000000072C2EB8> 1 <__main__.FOO object at 0x0000000007357080> 2 <__main__.FOO object at 0x00000000072C2EB8> 1 <__main__.FOO object at 0x00000000072C2160> None What did I miss? Thank you Eren From ethan at stoneleaf.us Tue Sep 17 13:14:18 2019 From: ethan at stoneleaf.us (Ethan Furman) Date: Tue, 17 Sep 2019 10:14:18 -0700 Subject: Python3.7 singleton is not unique anymore In-Reply-To: <8d99417c-bdd1-48e9-91ca-10c7c49611ae@googlegroups.com> References: <8d99417c-bdd1-48e9-91ca-10c7c49611ae@googlegroups.com> Message-ID: On 09/17/2019 09:45 AM, Eko palypse wrote: > else: > class FOO(): > def __init__(self, metaclass=Singleton): In your test code, the `metaclass=Singleton` should be in `class Foo`: class FOO(metaclass=Singleton): ... -- ~Ethan~ From ekopalypse at gmail.com Tue Sep 17 13:23:42 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Tue, 17 Sep 2019 10:23:42 -0700 (PDT) Subject: Python3.7 singleton is not unique anymore In-Reply-To: References: <8d99417c-bdd1-48e9-91ca-10c7c49611ae@googlegroups.com> Message-ID: <7db88649-79b8-45e9-84b8-ca54c1c110ca@googlegroups.com> Shame on me :-) Thank you very much Ethan. From ml_news at posteo.de Tue Sep 17 14:59:47 2019 From: ml_news at posteo.de (Manfred Lotz) Date: Tue, 17 Sep 2019 20:59:47 +0200 Subject: Spread a statement over various lines Message-ID: <20190917205947.6de1383f@arcor.com> I have a function like follows def regex_from_filepat(fpat): rfpat = fpat.replace('.', '\\.') \ .replace('%', '.') \ .replace('*', '.*') return '^' + rfpat + '$' As I don't want to have the replace() functions in one line my question is if it is ok to spread the statement over various lines as shown above, or if there is a better way? Thanks. -- Manfred From martin.schoon at gmail.com Tue Sep 17 15:10:29 2019 From: martin.schoon at gmail.com (Martin =?UTF-8?Q?Sch=C3=B6=C3=B6n?=) Date: 17 Sep 2019 19:10:29 GMT Subject: How do I purge pip intsall --user packages? Message-ID: I have installed a bunch of packages using pip install --user and I went for a non-standard location for the install. Now I regret this and would like to wipe this and start all over again using the standard location. Is it enough to delete the folder I specified or am I missing something? Having to uninstall each and every package would be tedious... /Martin From ml_news at posteo.de Tue Sep 17 15:10:58 2019 From: ml_news at posteo.de (Manfred Lotz) Date: Tue, 17 Sep 2019 21:10:58 +0200 Subject: Spread a statement over various lines References: <20190917205947.6de1383f@arcor.com> Message-ID: <20190917211059.3b5e494e@arcor.com> On Tue, 17 Sep 2019 20:59:47 +0200 Manfred Lotz wrote: > I have a function like follows > > def regex_from_filepat(fpat): > rfpat = fpat.replace('.', '\\.') \ > .replace('%', '.') \ Not related to my question but the second replace must be: .replace('?', '.') From ml_news at posteo.de Tue Sep 17 15:59:16 2019 From: ml_news at posteo.de (Manfred Lotz) Date: Tue, 17 Sep 2019 21:59:16 +0200 Subject: How do I purge pip intsall --user packages? References: Message-ID: <20190917215916.73dad100@arcor.com> On 17 Sep 2019 19:10:29 GMT Martin Sch??n wrote: > I have installed a bunch of packages using pip install --user and > I went for a non-standard location for the install. Now I regret > this and would like to wipe this and start all over again using > the standard location. Is it enough to delete the folder I > specified or am I missing something? Having to uninstall each > and every package would be tedious... > > /Martin Do you want to uninstall all per user installed packages? Then you could try: pip3 list --user | tail -n +3 | while read p rest; do pip3 uninstall $p;done From * at eli.users.panix.com Tue Sep 17 16:46:24 2019 From: * at eli.users.panix.com (Eli the Bearded) Date: Tue, 17 Sep 2019 20:46:24 +0000 (UTC) Subject: Unicode UCS2, UCS4 and ... UCS1 References: Message-ID: In comp.lang.python, moi wrote: > I hope, one day, for those who are interested in Unicode, > they find a book, publication, ... which will explain > what is UCS1. There isn't anything called UCS1. There is a UTF-1, but don't use it. UTF-8 is better in every way. https://en.wikipedia.org/wiki/Universal_Coded_Character_Set If you want it in book form, look for the "Create a book" link in the side bar. I'd suggest https://en.wikipedia.org/wiki/Unicode https://en.wikipedia.org/wiki/Comparison_of_Unicode_encodings https://en.wikipedia.org/wiki/UTF-8 https://en.wikipedia.org/wiki/UTF-16 https://en.wikipedia.org/wiki/UTF-32 As other things to include in your book. Elijah ------ doesn't think there is a character encoding newsgroup From 2QdxY4RzWzUUiLuE at potatochowder.com Tue Sep 17 16:51:04 2019 From: 2QdxY4RzWzUUiLuE at potatochowder.com (Dan Sommers) Date: Tue, 17 Sep 2019 16:51:04 -0400 Subject: Spread a statement over various lines In-Reply-To: <20190917205947.6de1383f@arcor.com> References: <20190917205947.6de1383f@arcor.com> Message-ID: <4a07c161-9680-58bc-d023-8286acf531e9@potatochowder.com> On 9/17/19 2:59 PM, Manfred Lotz wrote: > def regex_from_filepat(fpat): > rfpat = fpat.replace('.', '\\.') \ > .replace('%', '.') \ > .replace('*', '.*') > > return '^' + rfpat + '$' > > As I don't want to have the replace() functions in one line my > question is if it is ok to spread the statement over various lines as > shown above, or if there is a better way? Is that way okay? Sure. Are there other ways? Sure. To isolate each replace() function on its own line, and to eliminate the clutter of the backslashes, consider this: rfpat = (fpat .replace('.', '\\.') .replace('%', '.') .replace('*', '.*') ) "Better" is going to depend on your initial reason for not wanting all those function calls on one line. What is that reason? From rosuav at gmail.com Tue Sep 17 17:00:41 2019 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 18 Sep 2019 07:00:41 +1000 Subject: Unicode UCS2, UCS4 and ... UCS1 In-Reply-To: References: Message-ID: On Wed, Sep 18, 2019 at 6:51 AM Eli the Bearded <*@eli.users.panix.com> wrote: > > In comp.lang.python, moi wrote: > > I hope, one day, for those who are interested in Unicode, > > they find a book, publication, ... which will explain > > what is UCS1. > > There isn't anything called UCS1. There is a UTF-1, but don't use it. > UTF-8 is better in every way. > > https://en.wikipedia.org/wiki/Universal_Coded_Character_Set > Don't waste your time talking to jmf. He doesn't listen. Most of us don't see his posts, as they're blocked by the news-mailinglist gateway, and a lot of newsgroup readers have killfiled him. I recommend it. ChrisA From * at eli.users.panix.com Tue Sep 17 19:34:37 2019 From: * at eli.users.panix.com (Eli the Bearded) Date: Tue, 17 Sep 2019 23:34:37 +0000 (UTC) Subject: python3 subprocess run sudo cmd in remote failed References: Message-ID: In comp.lang.python, lampahome wrote: > what I tried many times like enter password, but it failed. > I just want to use ps.stdin.write(password) to send password, but it always > jump password prompt immediately. Passwords are frequently read from stderr, not stdin, so that tools can get a human answered password from inside a pipleline providing stdin to something downstream. > How to solve this Use some sort of out-of-band authentication. Jenkins (when I looked at it) used ssh-agent. Ansible used sshpass. And you've already had key-pairs suggested in a different reply. Elijah ------ likes ssh-agent and key-pairs From ml_news at posteo.de Tue Sep 17 22:39:27 2019 From: ml_news at posteo.de (Manfred Lotz) Date: Wed, 18 Sep 2019 04:39:27 +0200 Subject: Spread a statement over various lines References: <20190917205947.6de1383f@arcor.com> <4a07c161-9680-58bc-d023-8286acf531e9@potatochowder.com> Message-ID: <20190918043927.613e241b@arcor.com> On Tue, 17 Sep 2019 16:51:04 -0400 Dan Sommers <2QdxY4RzWzUUiLuE at potatochowder.com> wrote: > On 9/17/19 2:59 PM, Manfred Lotz wrote: > > > def regex_from_filepat(fpat): > > rfpat = fpat.replace('.', '\\.') \ > > .replace('%', '.') \ > > .replace('*', '.*') > > > > return '^' + rfpat + '$' > > > > As I don't want to have the replace() functions in one line my > > question is if it is ok to spread the statement over various lines > > as shown above, or if there is a better way? > > Is that way okay? Sure. Are there other ways? Sure. > > To isolate each replace() function on its own line, and to eliminate > the clutter of the backslashes, consider this: > > rfpat = (fpat > .replace('.', '\\.') > .replace('%', '.') > .replace('*', '.*') > ) > > "Better" is going to depend on your initial reason for not wanting all > those function calls on one line. What is that reason? Well, I don't want those functions on a single line because having them multiline the code looks clearer. I asked here because I don't like the backslashes. Wrapping them in () looks like a good solution. From cs at cskk.id.au Wed Sep 18 00:13:43 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Wed, 18 Sep 2019 14:13:43 +1000 Subject: Obtain the file's path. In-Reply-To: References: Message-ID: <20190918041343.GA7687@cskk.homeip.net> On 17Sep2019 15:09, Hongyi Zhao wrote: >See the following two methods for obtaining the file's path: > >os.path.realpath(file) >or >os.path.abspath(os.path.expanduser(file)) > >Which is more robust? They're probably equally robust (BTW, you need the expanduser in the realpath call as well, if your filename might start with a tilde). realpath will resolve symlinks and get you a path that does not pass through one. abspath is more lexical and just gets you a path which may traverse a symlink. Both return a valid absolute path (provided that file is an existing file). My inclination is often to use abspath, because it may better express the "intent" of the filename by not "undoing" the effects of symlinks. Consider the path "~/media/foo.mp4". In my home directory, "media" may be a symlink; on at least one machine it is a symlink into our larger RAID array. So abspath(expanduser("~/media/foo.mp4")) might expand to "/home/cameron/media/foo.mp4". Conversely, realpath(expanduser("~/media/foo.mp4")) might expand to "/raid_volume/cameron/media/foo.mp4". If I rearrange things, for example by moving the media directory _and_ adjusting the ~/media symlink, then the old result of abspath will still work because it traverses the symlink "media". The old result of realpath will no longer be correct. If you just want this for your running program's internals this may not matter, but if you're recording the result somewhere then abspath might get you a more "stable" path in the above scenario. You might ask yourself, why do you need to know the absolute path at all? A relative path is usually just fine; it isn't like it won't work unless you use it from another directory. Cheers, Cameron Simpson From __peter__ at web.de Wed Sep 18 02:30:08 2019 From: __peter__ at web.de (Peter Otten) Date: Wed, 18 Sep 2019 08:30:08 +0200 Subject: Spread a statement over various lines References: <20190917205947.6de1383f@arcor.com> Message-ID: Manfred Lotz wrote: > I have a function like follows > > def regex_from_filepat(fpat): > rfpat = fpat.replace('.', '\\.') \ > .replace('%', '.') \ > .replace('*', '.*') > > return '^' + rfpat + '$' > > > As I don't want to have the replace() functions in one line my > question is if it is ok to spread the statement over various lines as > shown above, or if there is a better way? Sometimes you can avoid method-chaining: >>> REP = str.maketrans({".": "\\.", "%": ".", "*": ".*"}) >>> def regex_from_filepat(fpat): ... return fpat.translate(REP) ... >>> regex_from_filepat("*foo.%") '.*foo\\..' Generally speaking a long statement may be a hint that there is an alternative spelling. From wolfgang.maier at biologie.uni-freiburg.de Wed Sep 18 03:52:21 2019 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Wed, 18 Sep 2019 09:52:21 +0200 Subject: Spread a statement over various lines In-Reply-To: <20190917205947.6de1383f@arcor.com> References: <20190917205947.6de1383f@arcor.com> Message-ID: <41541ea5-28a5-4622-42a7-1b57bc8bbc00@biologie.uni-freiburg.de> On 17.09.19 20:59, Manfred Lotz wrote: > I have a function like follows > > def regex_from_filepat(fpat): > rfpat = fpat.replace('.', '\\.') \ > .replace('%', '.') \ > .replace('*', '.*') > > return '^' + rfpat + '$' > > > As I don't want to have the replace() functions in one line my > question is if it is ok to spread the statement over various lines as > shown above, or if there is a better way? > One problem with explicit line continuation using \ is that it is dependent on the backslash being the last character on the line, i.e. a single space at the end of the line will result in a SyntaxError. This is why implicit line continuation relying on opened parentheses, brackets or curly braces is often preferred, and recommended over backslash continuation in PEP8 (https://www.python.org/dev/peps/pep-0008/#maximum-line-length). To use implicit line continuation you could either introduce extra surrounding parentheses as suggested by others, or you may make use of the parentheses you have already, like so: def regex_from_filepat(fpat): rfpat = fpat.replace( '.', '\\.' ).replace( '%', '.' ).replace( '*', '.*' ) return '^' + rfpat + '$' From eryksun at gmail.com Wed Sep 18 04:36:06 2019 From: eryksun at gmail.com (Eryk Sun) Date: Wed, 18 Sep 2019 03:36:06 -0500 Subject: Obtain the file's path. In-Reply-To: <20190918041343.GA7687@cskk.homeip.net> References: <20190918041343.GA7687@cskk.homeip.net> Message-ID: On 9/17/19, Cameron Simpson wrote: > > If you just want this for your running program's internals this may not > matter, but if you're recording the result somewhere then abspath might > get you a more "stable" path in the above scenario. If a path has ".." components, the abspath() result may be wrong if it resolves them by removing a parent symlink. The absolute() method of pathlib.Path does this right by retaining ".." components. >>> os.path.abspath('/foo/symlink/../bar') '/foo/bar' >>> pathlib.Path('/foo/symlink/../bar').absolute() PosixPath('/foo/symlink/../bar') abspath() is also the wrong choice if we're computing the target path for a relative symlink via relpath(). A relative symlink is evaluated from the parsed path of its parent directory. From pankaj.jangid at gmail.com Wed Sep 18 04:42:43 2019 From: pankaj.jangid at gmail.com (Pankaj Jangid) Date: Wed, 18 Sep 2019 14:12:43 +0530 Subject: Obtain the file's path. References: <20190918041343.GA7687@cskk.homeip.net> Message-ID: Cameron Simpson writes: > On 17Sep2019 15:09, Hongyi Zhao wrote: >>See the following two methods for obtaining the file's path: >> >>os.path.realpath(file) >>or >>os.path.abspath(os.path.expanduser(file)) >> >>Which is more robust? > > My inclination is often to use abspath, because it may better express > the "intent" of the filename by not "undoing" the effects of symlinks. > > So abspath(expanduser("~/media/foo.mp4")) might expand to > "/home/cameron/media/foo.mp4". > > Conversely, realpath(expanduser("~/media/foo.mp4")) might expand to > "/raid_volume/cameron/media/foo.mp4". > > You might ask yourself, why do you need to know the absolute path at > all? A relative path is usually just fine; it isn't like it won't work > unless you use it from another directory. > Somewhat related to the OP's question. So what is a good strategy in an application? I am now inclined to use relative path and working directory both. And when there is change of runtime context just change the working directory and assemble the absolute path at runtime. And to save the working directory use "abspath" as suggested by Cameron. -- Pankaj Jangid From cs at cskk.id.au Wed Sep 18 05:38:21 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Wed, 18 Sep 2019 19:38:21 +1000 Subject: Obtain the file's path. In-Reply-To: References: Message-ID: <20190918093821.GA77244@cskk.homeip.net> On 18Sep2019 03:36, eryk sun wrote: >On 9/17/19, Cameron Simpson wrote: >> >> If you just want this for your running program's internals this may not >> matter, but if you're recording the result somewhere then abspath might >> get you a more "stable" path in the above scenario. > >If a path has ".." components, the abspath() result may be wrong if it >resolves them by removing a parent symlink. The absolute() method of >pathlib.Path does this right by retaining ".." components. > > >>> os.path.abspath('/foo/symlink/../bar') > '/foo/bar' > > >>> pathlib.Path('/foo/symlink/../bar').absolute() > PosixPath('/foo/symlink/../bar') > >abspath() is also the wrong choice if we're computing the target path >for a relative symlink via relpath(). A relative symlink is evaluated >from the parsed path of its parent directory. For the record, I agree entirely with Eryk here. Cheers, Cameron Simpson From ml_news at posteo.de Wed Sep 18 05:39:41 2019 From: ml_news at posteo.de (Manfred Lotz) Date: Wed, 18 Sep 2019 11:39:41 +0200 Subject: Spread a statement over various lines References: <20190917205947.6de1383f@arcor.com> <41541ea5-28a5-4622-42a7-1b57bc8bbc00@biologie.uni-freiburg.de> Message-ID: <20190918113941.20640675@arcor.com> On Wed, 18 Sep 2019 09:52:21 +0200 Wolfgang Maier wrote: > On 17.09.19 20:59, Manfred Lotz wrote: > > I have a function like follows > > > > def regex_from_filepat(fpat): > > rfpat = fpat.replace('.', '\\.') \ > > .replace('%', '.') \ > > .replace('*', '.*') > > > > return '^' + rfpat + '$' > > > > > > As I don't want to have the replace() functions in one line my > > question is if it is ok to spread the statement over various lines > > as shown above, or if there is a better way? > > > > One problem with explicit line continuation using \ is that it is > dependent on the backslash being the last character on the line, i.e. > a single space at the end of the line will result in a SyntaxError. > This is why implicit line continuation relying on opened parentheses, > brackets or curly braces is often preferred, and recommended over > backslash continuation in PEP8 > (https://www.python.org/dev/peps/pep-0008/#maximum-line-length). > To use implicit line continuation you could either introduce extra > surrounding parentheses as suggested by others, or you may make use > of the parentheses you have already, like so: > > def regex_from_filepat(fpat): > rfpat = fpat.replace( > '.', '\\.' > ).replace( > '%', '.' > ).replace( > '*', '.*' > ) > > return '^' + rfpat + '$' > > Thanks for showing. Good to be aware of this possiblity. So, it seems it doesn't hurt to read PEP8. From ml_news at posteo.de Wed Sep 18 08:04:48 2019 From: ml_news at posteo.de (Manfred Lotz) Date: Wed, 18 Sep 2019 14:04:48 +0200 Subject: Spread a statement over various lines References: <20190917205947.6de1383f@arcor.com> Message-ID: <20190918140448.278e5f35@arcor.com> On Wed, 18 Sep 2019 08:30:08 +0200 Peter Otten <__peter__ at web.de> wrote: > Manfred Lotz wrote: > > > I have a function like follows > > > > def regex_from_filepat(fpat): > > rfpat = fpat.replace('.', '\\.') \ > > .replace('%', '.') \ > > .replace('*', '.*') > > > > return '^' + rfpat + '$' > > > > > > As I don't want to have the replace() functions in one line my > > question is if it is ok to spread the statement over various lines > > as shown above, or if there is a better way? > > Sometimes you can avoid method-chaining: > > >>> REP = str.maketrans({".": "\\.", "%": ".", "*": ".*"}) > >>> def regex_from_filepat(fpat): > ... return fpat.translate(REP) > ... > >>> regex_from_filepat("*foo.%") > '.*foo\\..' > Very interesting. Thanks for this. > Generally speaking a long statement may be a hint that there is an > alternative spelling. > > I'll keep it in my mind. From Ralf_M at t-online.de Wed Sep 18 16:01:34 2019 From: Ralf_M at t-online.de (Ralf M.) Date: Wed, 18 Sep 2019 22:01:34 +0200 Subject: Spread a statement over various lines In-Reply-To: <20190917205947.6de1383f@arcor.com> References: <20190917205947.6de1383f@arcor.com> Message-ID: <06054bfb-073d-14fd-0441-664b1fdc4348@t-online.de> Am 17.09.2019 um 20:59 schrieb Manfred Lotz: > I have a function like follows > > def regex_from_filepat(fpat): > rfpat = fpat.replace('.', '\\.') \ > .replace('%', '.') \ > .replace('*', '.*') > > return '^' + rfpat + '$' > > > As I don't want to have the replace() functions in one line my > question is if it is ok to spread the statement over various lines as > shown above, or if there is a better way? > > Thanks. > Not related to your question, but: You seem to try to convert a Windows wildcard pattern to a regex pattern. However, wildcards sometimes behave a bit different than what you assume. I know for instance that *.* matches any filename, even if the filename doesn't contain a dot. Out of curiosity I played around a bit, details below. As you can see, there are other wildcard strangenesses, e.g. - ? does not match a dot - ???? between letters etc. matches exactly 4 characters, but ???? at the end or directly before a dot matches at most 4 characters I don't know the exact rules of Windows wildcards, so there may be even more cases of unexpected behavior. If anyone knows where to find the complete rules (or a python module that implements them), I would be interested. Regards, Ralf ----- Details (Win 7 home SP1) ----- C:\tmp>more pydirb.py #!/usr/bin/env python3 import os, re, sys def regex_from_filepat(fpat): rfpat = fpat.replace('.', '\\.') \ .replace('?', '.') \ .replace('*', '.*') return '^' + rfpat + '$' regexfilepat = re.compile(regex_from_filepat(sys.argv[1])) for name in os.listdir(): if regexfilepat.match(name): print(name) C:\tmp>dir /b * foo foo.bar foo.bar.c foo.c pydirb.py C:\tmp>pydirb * foo foo.bar foo.bar.c foo.c pydirb.py C:\tmp>dir /b *.* foo foo.bar foo.bar.c foo.c pydirb.py C:\tmp>pydirb *.* foo.bar foo.bar.c foo.c pydirb.py C:\tmp>dir /b *.*.*.*.* foo foo.bar foo.bar.c foo.c pydirb.py C:\tmp>pydirb *.*.*.*.* C:\tmp>dir /b foo.? foo foo.c C:\tmp>pydirb foo.? foo.c C:\tmp>dir /b foo.???? foo foo.bar foo.c C:\tmp>pydirb foo.???? C:\tmp>dir /b foo?bar Datei nicht gefunden C:\tmp>pydirb foo?bar foo.bar C:\tmp>dir /b f?o.bar foo.bar C:\tmp>pydirb f?o.bar foo.bar C:\tmp>dir /b f??o.bar Datei nicht gefunden C:\tmp>pydirb f??o.bar C:\tmp>dir /b fo?.bar foo.bar C:\tmp>pydirb fo?.bar foo.bar C:\tmp>dir /b fo??.bar foo.bar C:\tmp>pydirb fo??.bar C:\tmp>dir /b foo??.bar foo.bar C:\tmp>pydirb foo??.bar From rosuav at gmail.com Wed Sep 18 16:22:10 2019 From: rosuav at gmail.com (Chris Angelico) Date: Thu, 19 Sep 2019 06:22:10 +1000 Subject: Spread a statement over various lines In-Reply-To: <06054bfb-073d-14fd-0441-664b1fdc4348@t-online.de> References: <20190917205947.6de1383f@arcor.com> <06054bfb-073d-14fd-0441-664b1fdc4348@t-online.de> Message-ID: On Thu, Sep 19, 2019 at 6:20 AM Ralf M. wrote: > > Am 17.09.2019 um 20:59 schrieb Manfred Lotz: > > I have a function like follows > > > > def regex_from_filepat(fpat): > > rfpat = fpat.replace('.', '\\.') \ > > .replace('%', '.') \ > > .replace('*', '.*') > > > > return '^' + rfpat + '$' > > > > > > As I don't want to have the replace() functions in one line my > > question is if it is ok to spread the statement over various lines as > > shown above, or if there is a better way? > > > > Thanks. > > > > Not related to your question, but: > You seem to try to convert a Windows wildcard pattern to a regex > pattern. However, wildcards sometimes behave a bit different than what > you assume. I know for instance that *.* matches any filename, even if > the filename doesn't contain a dot. Hmm, why do you assume it's a Windows wildcard pattern specifically? ChrisA From abrault at mapgears.com Wed Sep 18 16:24:28 2019 From: abrault at mapgears.com (Alexandre Brault) Date: Wed, 18 Sep 2019 16:24:28 -0400 Subject: Spread a statement over various lines In-Reply-To: <06054bfb-073d-14fd-0441-664b1fdc4348@t-online.de> References: <20190917205947.6de1383f@arcor.com> <06054bfb-073d-14fd-0441-664b1fdc4348@t-online.de> Message-ID: On 2019-09-18 4:01 p.m., Ralf M. wrote: > Am 17.09.2019 um 20:59 schrieb Manfred Lotz: >> I have a function like follows >> >> def regex_from_filepat(fpat): >> ???? rfpat = fpat.replace('.', '\\.') \ >> ?????????????????????? .replace('%', '.')? \ >> ?????????????????????? .replace('*', '.*') >> >> ???? return '^' + rfpat + '$' >> >> >> As I don't want to have the replace() functions in one line my >> question is if it is ok to spread the statement over various lines as >> shown above, or if there is a better way? >> >> Thanks. >> > > Not related to your question, but: > You seem to try to convert a Windows wildcard pattern to a regex > pattern. However, wildcards sometimes behave a bit different than what > you assume. I know for instance that *.* matches any filename, even if > the filename doesn't contain a dot. > > Out of curiosity I played around a bit, details below. > As you can see, there are other wildcard strangenesses, e.g. > - ? does not match a dot > - ???? between letters etc. matches exactly 4 characters, but > ? ???? at the end or directly before a dot matches at most 4 characters > > I don't know the exact rules of Windows wildcards, so there may be > even more cases of unexpected behavior. > If anyone knows where to find the complete rules (or a python module > that implements them), I would be interested. > fnmatch in the standard library has a translate function that transforms a glob pattern to a regex https://docs.python.org/3.7/library/fnmatch.html#fnmatch.translate Alex From ekopalypse at gmail.com Wed Sep 18 17:47:09 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Wed, 18 Sep 2019 14:47:09 -0700 (PDT) Subject: exec and globals and locals ... Message-ID: Why does f1 work? I've expected an exception as no global dict has been provided, and why does throw f3 an exception if it does, more or less, the same as f1? x += 5 def f1(): exec("x += 1; print('f1 in:', x)") return x print('f1 out', f1()) # result => f1 in: 6 # result => f1 out 5 x = 5 def f2(): exec("x += 1; print('f2 in:', x)", globals()) return x print('f2 out', f2()) # result => f2 in: 6 # result => f2 out 6 exec('import test01', globals()) print('f3 out', x) # result exception, expected but because f1 didn't throw an exception # I'm confused. module test01 has only this two lines x += 1 print('f3 in:', x) Thank you Eren From codewizard at gmail.com Wed Sep 18 19:18:00 2019 From: codewizard at gmail.com (codewizard at gmail.com) Date: Wed, 18 Sep 2019 16:18:00 -0700 (PDT) Subject: Spread a statement over various lines In-Reply-To: <20190918140448.278e5f35@arcor.com> References: <20190917205947.6de1383f@arcor.com> <20190918140448.278e5f35@arcor.com> Message-ID: On Wednesday, September 18, 2019 at 9:01:21 AM UTC-4, Manfred Lotz wrote: > On Wed, 18 Sep 2019 08:30:08 +0200 > Peter Otten <__peter__ at web.de> wrote: > > > Manfred Lotz wrote: > > > > > I have a function like follows > > > > > > def regex_from_filepat(fpat): > > > rfpat = fpat.replace('.', '\\.') \ > > > .replace('%', '.') \ > > > .replace('*', '.*') > > > > > > return '^' + rfpat + '$' > > > > > > > > > As I don't want to have the replace() functions in one line my > > > question is if it is ok to spread the statement over various lines > > > as shown above, or if there is a better way? > > > > Sometimes you can avoid method-chaining: > > > > >>> REP = str.maketrans({".": "\\.", "%": ".", "*": ".*"}) > > >>> def regex_from_filepat(fpat): > > ... return fpat.translate(REP) > > ... > > >>> regex_from_filepat("*foo.%") > > '.*foo\\..' > > > > Very interesting. Thanks for this. While I think that str.translate() is the right tool for this job, here's another way to avoid line continuations (not necessarily better): def regex_from_filepat(fpat): replacements = [ ('.', '\\.'), ('%', '.'), ('*', '.*'), ] rfpat = fpat for old, new in replacements: rfpat = rfpat.replace(old, new) return '^' + rfpat + '$' From ml_news at posteo.de Wed Sep 18 23:13:29 2019 From: ml_news at posteo.de (Manfred Lotz) Date: Thu, 19 Sep 2019 05:13:29 +0200 Subject: Spread a statement over various lines References: <20190917205947.6de1383f@arcor.com> <06054bfb-073d-14fd-0441-664b1fdc4348@t-online.de> Message-ID: <20190919051329.747f1007@arcor.com> On Wed, 18 Sep 2019 22:01:34 +0200 "Ralf M." wrote: > Am 17.09.2019 um 20:59 schrieb Manfred Lotz: > > I have a function like follows > > > > def regex_from_filepat(fpat): > > rfpat = fpat.replace('.', '\\.') \ > > .replace('%', '.') \ > > .replace('*', '.*') > > > > return '^' + rfpat + '$' > > > > > > As I don't want to have the replace() functions in one line my > > question is if it is ok to spread the statement over various lines > > as shown above, or if there is a better way? > > > > Thanks. > > > > Not related to your question, but: > You seem to try to convert a Windows wildcard pattern to a regex > pattern. No, I'm on Linux. Shortly, after I had posted the question I discovered fnmatch() in the standard library, and I changed my code accordingly. Nevertheless, I was happy to have asked the question as the following discussion was very interesting for me. -- Manfred From dieter at handshake.de Thu Sep 19 01:04:27 2019 From: dieter at handshake.de (dieter) Date: Thu, 19 Sep 2019 07:04:27 +0200 Subject: exec and globals and locals ... References: Message-ID: <87muf0op0k.fsf@handshake.de> Eko palypse writes: > Why does f1 work? I've expected an exception as no global dict has been provided > ... >def f1(...): > exec("...") >... The documentation ("https://docs.python.org/3/library/functions.html#exec") tells you: exec(object[, globals[, locals]]) ... In all cases, if the optional parts are omitted, the code is executed in the current scope. ... You can see from it that "globals" is optional. And that, if "globals" is missing, then "exec" is executed in the current scope ("f1" in your case). From __peter__ at web.de Thu Sep 19 02:36:04 2019 From: __peter__ at web.de (Peter Otten) Date: Thu, 19 Sep 2019 08:36:04 +0200 Subject: Spread a statement over various lines References: <20190917205947.6de1383f@arcor.com> <06054bfb-073d-14fd-0441-664b1fdc4348@t-online.de> <20190919051329.747f1007@arcor.com> Message-ID: Manfred Lotz wrote: >> Not related to your question, but: >> You seem to try to convert a Windows wildcard pattern to a regex >> pattern. > > No, I'm on Linux. > > Shortly, after I had posted the question I discovered fnmatch() in the > standard library, and I changed my code accordingly. I would have pointed to fnmatch, but I didn't recognize the '%' that you used instead of the usual '?', and fnmatch doesn't either, I think. Where does '%' come from? From __peter__ at web.de Thu Sep 19 02:47:43 2019 From: __peter__ at web.de (Peter Otten) Date: Thu, 19 Sep 2019 08:47:43 +0200 Subject: exec and globals and locals ... References: Message-ID: Eko palypse wrote: > exec('import test01', globals()) > print('f3 out', x) > > # result exception, expected but because f1 didn't throw an exception > # I'm confused. module test01 has only this two lines > x += 1 > print('f3 in:', x) The lines above run in the test01's global namespace, not in the global namespace of the importing module. > exec('import test01', globals()) Think of import as a function like exec("test01 = load_module('test01')", globals()) Then it should be clear that the name 'test01' is put into globals(), if load_module() doesn't throw an exception. No sharing or nesting of namespaces takes place. From smilesonisamal at gmail.com Thu Sep 19 03:50:15 2019 From: smilesonisamal at gmail.com (Pradeep Patra) Date: Thu, 19 Sep 2019 13:20:15 +0530 Subject: regular expressions help Message-ID: Hi all, I was playing around with regular expressions and testing the simple regular expression and its notworking for some reason. I want to search "my-dog" at any of the place in a string and return the index but its not working. I tried both in python 3.7.3 and 2.7.x. Can anyone please help? I tried re.search, re.finditer, re.findall and none of them is not working for me. import re mystr= "where is my-dog" pattern=re.compile(r'^my\-dog$') matches = re.search(mystr) print(matches) In the above example both cases(match/not match) the matches returns "None" I tried re.finditer() and then a loop to find all the occurences of the pattern in the string but even if there is no error but i could not find the match. Can anyone help me in this regard? Regards Pradeep From bouncingcats at gmail.com Thu Sep 19 03:54:13 2019 From: bouncingcats at gmail.com (David) Date: Thu, 19 Sep 2019 17:54:13 +1000 Subject: regular expressions help In-Reply-To: References: Message-ID: On Thu, 19 Sep 2019 at 17:51, Pradeep Patra wrote: > > pattern=re.compile(r'^my\-dog$') > matches = re.search(mystr) > > In the above example both cases(match/not match) the matches returns "None" Hi, do you know what the '^' character does in your pattern? From smilesonisamal at gmail.com Thu Sep 19 04:41:52 2019 From: smilesonisamal at gmail.com (Pradeep Patra) Date: Thu, 19 Sep 2019 14:11:52 +0530 Subject: regular expressions help In-Reply-To: References: Message-ID: I am using python 2.7.6 but I also tried on python 3.7.3. On Thursday, September 19, 2019, Pradeep Patra wrote: > Beginning of the string. But I tried removing that as well and it still > could not find it. When I tested at www.regex101.com and it matched > successfully whereas I may be wrong. Could you please help here? > > On Thursday, September 19, 2019, David wrote: > >> On Thu, 19 Sep 2019 at 17:51, Pradeep Patra >> wrote: >> > >> > pattern=re.compile(r'^my\-dog$') >> > matches = re.search(mystr) >> > >> > In the above example both cases(match/not match) the matches returns >> "None" >> >> Hi, do you know what the '^' character does in your pattern? >> > From ml_news at posteo.de Thu Sep 19 04:59:12 2019 From: ml_news at posteo.de (Manfred Lotz) Date: Thu, 19 Sep 2019 10:59:12 +0200 Subject: Spread a statement over various lines References: <20190917205947.6de1383f@arcor.com> <06054bfb-073d-14fd-0441-664b1fdc4348@t-online.de> <20190919051329.747f1007@arcor.com> Message-ID: <20190919105912.0cc0b01e@arcor.com> On Thu, 19 Sep 2019 08:36:04 +0200 Peter Otten <__peter__ at web.de> wrote: > Manfred Lotz wrote: > > >> Not related to your question, but: > >> You seem to try to convert a Windows wildcard pattern to a regex > >> pattern. > > > > No, I'm on Linux. > > > > Shortly, after I had posted the question I discovered fnmatch() in > > the standard library, and I changed my code accordingly. > > I would have pointed to fnmatch, but I didn't recognize the '%' that > you used instead of the usual '?', and fnmatch doesn't either, I > think. > > Where does '%' come from? > '%' was a mistake as I had replied myself to my initial question. From bouncingcats at gmail.com Thu Sep 19 05:12:34 2019 From: bouncingcats at gmail.com (David) Date: Thu, 19 Sep 2019 19:12:34 +1000 Subject: regular expressions help In-Reply-To: References: Message-ID: On Thu, 19 Sep 2019 at 18:41, Pradeep Patra wrote: > On Thursday, September 19, 2019, Pradeep Patra wrote: >> On Thursday, September 19, 2019, David wrote: >>> On Thu, 19 Sep 2019 at 17:51, Pradeep Patra wrote: >>> > pattern=re.compile(r'^my\-dog$') >>> > matches = re.search(mystr) >>> > In the above example both cases(match/not match) the matches returns "None" >>> Hi, do you know what the '^' character does in your pattern? >> Beginning of the string. But I tried removing that as well and it still could not find it. When I tested at www.regex101.com and it matched successfully whereas I may be wrong. Could you please help here? > I am using python 2.7.6 but I also tried on python 3.7.3. $ python2 Python 2.7.13 (default, Sep 26 2018, 18:42:22) [GCC 6.3.0 20170516] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import re >>> mystr= "where is my-dog" >>> pattern=re.compile(r'my-dog$') >>> matches = re.search(mystr) # this is syntax error, but it is what you showed above Traceback (most recent call last): File "", line 1, in TypeError: search() takes at least 2 arguments (1 given) >>> matches = re.search(pattern, mystr) >>> matches.group(0) 'my-dog' >>> From smilesonisamal at gmail.com Thu Sep 19 05:34:12 2019 From: smilesonisamal at gmail.com (Pradeep Patra) Date: Thu, 19 Sep 2019 15:04:12 +0530 Subject: regular expressions help In-Reply-To: References: Message-ID: Thanks David for your quick help. Appreciate it. When I tried on python 2.7.3 the same thing you did below I got the error after matches.group(0) as follows: AttributeError: NoneType object has no attribute 'group'. I tried to check 'None' for no match for re.search as the documentation says but it's not working. Unfortunately I cannot update the python version now to 2.7.13 as other programs are using this version and need to test all and it requires more testing. Any idea how I can fix this ? I am ok to use any other re method(not only tied to re.search) as long as it works. On Thursday, September 19, 2019, David wrote: > On Thu, 19 Sep 2019 at 18:41, Pradeep Patra > wrote: > > On Thursday, September 19, 2019, Pradeep Patra > wrote: > >> On Thursday, September 19, 2019, David wrote: > >>> On Thu, 19 Sep 2019 at 17:51, Pradeep Patra > wrote: > > >>> > pattern=re.compile(r'^my\-dog$') > >>> > matches = re.search(mystr) > > >>> > In the above example both cases(match/not match) the matches returns > "None" > > >>> Hi, do you know what the '^' character does in your pattern? > > >> Beginning of the string. But I tried removing that as well and it still > could not find it. When I tested at www.regex101.com and it matched > successfully whereas I may be wrong. Could you please help here? > > > I am using python 2.7.6 but I also tried on python 3.7.3. > > $ python2 > Python 2.7.13 (default, Sep 26 2018, 18:42:22) > [GCC 6.3.0 20170516] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> import re > >>> mystr= "where is my-dog" > >>> pattern=re.compile(r'my-dog$') > >>> matches = re.search(mystr) # this is syntax error, but it is what you > showed above > Traceback (most recent call last): > File "", line 1, in > TypeError: search() takes at least 2 arguments (1 given) > >>> matches = re.search(pattern, mystr) > >>> matches.group(0) > 'my-dog' > >>> > -- > https://mail.python.org/mailman/listinfo/python-list > From bouncingcats at gmail.com Thu Sep 19 06:15:34 2019 From: bouncingcats at gmail.com (David) Date: Thu, 19 Sep 2019 20:15:34 +1000 Subject: regular expressions help In-Reply-To: References: Message-ID: On Thu, 19 Sep 2019 at 19:34, Pradeep Patra wrote: > Thanks David for your quick help. Appreciate it. When I tried on python 2.7.3 the same thing you did below I got the error after matches.group(0) as follows: > > AttributeError: NoneType object has no attribute 'group'. > > I tried to check 'None' for no match for re.search as the documentation says but it's not working. > > Unfortunately I cannot update the python version now to 2.7.13 as other programs are using this version and need to test all and it requires more testing. Any idea how I can fix this ? I am ok to use any other re method(not only tied to re.search) as long as it works. Hi again Pradeep, We are now on email number seven, so I am going to try to give you some good advice ... When you ask on a forum like this for help, it is very important to show people exactly what you did. Everything that you did. In the shortest possible way that demonstrates whatever issue you are facing. It is best to give us a recipe that we can follow exactly that shows every step that you do when you have the problem that you need help with. And the best way to do that is for you to learn how to cut and paste between where you run your problem code, and where you send your email message to us. Please observe the way that I communicated with you last time. I sent you an exact cut and paste from my terminal, to help you by allowing you to duplicate exactly every step that I made. You should communicate with us in the same way. Because when you write something like your most recent message > I got the error after matches.group(0) as follows: > AttributeError: NoneType object has no attribute 'group'. this tells us nothing useful!! Because we cannot see everything you did leading up to that, so we cannot reproduce your problem. For us to help you, you need to show all the steps, the same way I did. Now, to help you, I found the same old version of Python 2 that you have, to prove to you that it works on your version. So you talking about updating Python is not going to help. Instead, you need to work out what you are doing that is causing your problem. Again, I cut and paste my whole session to show you, see below. Notice that the top lines show that it is the same version that you have. If you cut and paste my commands into your Python then it should work the same way for you too. If it does not work for you, then SHOW US THE WHOLE SESSION, EVERY STEP, so that we can reproduce your problem. Run your python in a terminal, and copy and paste the output you get into your message. $ python Python 2.7.3 (default, Jun 20 2016, 16:18:47) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import re >>> mystr = "where is my-dog" >>> pattern = re.compile(r'my-dog$') >>> matches = re.search(pattern, mystr) >>> matches.group(0) 'my-dog' >>> I hope you realise that the re module has been used by thousands of programmers, for many years. So it's extremely unlikely that it "doesn't work" in a way that gets discovered by someone who hardly knows how to use it. From ekopalypse at gmail.com Thu Sep 19 06:16:42 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Thu, 19 Sep 2019 03:16:42 -0700 (PDT) Subject: exec and globals and locals ... In-Reply-To: References: <87muf0op0k.fsf@handshake.de> Message-ID: <4d91f2c4-d251-4d6f-97d1-88dadf13111b@googlegroups.com> > In all cases, if the optional parts are omitted, the code is executed in the current scope. ... > > > You can see from it that "globals" is optional. > And that, if "globals" is missing, then > "exec" is executed in the current scope ("f1" in your case). Thank you for your answer, and that is exactly what confuses me? Where does x come from? If I only would read x then I would understand why it can be found/read but I alter it and as such I either have to provide the info that this is a global variable, declare it inside of f1 or provide the globals dict to exec. But I don't do any of it. Why is exec able to use the global x? Eren From ekopalypse at gmail.com Thu Sep 19 06:20:29 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Thu, 19 Sep 2019 03:20:29 -0700 (PDT) Subject: exec and globals and locals ... In-Reply-To: References: Message-ID: <89b80aee-aee9-49a6-b10b-de57f9af37d0@googlegroups.com> > Then it should be clear that the name 'test01' is put into globals(), if > load_module() doesn't throw an exception. No sharing or nesting of > namespaces takes place. Thank you too for your answer. Ok, that means that in every case when exec imports something it has its own global namespace, right? Is there a way to manipulate this namespace before any code from that module gets executed? Eren From __peter__ at web.de Thu Sep 19 06:39:29 2019 From: __peter__ at web.de (Peter Otten) Date: Thu, 19 Sep 2019 12:39:29 +0200 Subject: Spread a statement over various lines References: <20190917205947.6de1383f@arcor.com> <06054bfb-073d-14fd-0441-664b1fdc4348@t-online.de> <20190919051329.747f1007@arcor.com> <20190919105912.0cc0b01e@arcor.com> Message-ID: Manfred Lotz wrote: >> Where does '%' come from? >> > > '%' was a mistake as I had replied myself to my initial question. Oh, sorry. I missed that. From Richard at Damon-Family.org Thu Sep 19 06:45:16 2019 From: Richard at Damon-Family.org (Richard Damon) Date: Thu, 19 Sep 2019 06:45:16 -0400 Subject: exec and globals and locals ... In-Reply-To: <4d91f2c4-d251-4d6f-97d1-88dadf13111b@googlegroups.com> References: <87muf0op0k.fsf@handshake.de> <4d91f2c4-d251-4d6f-97d1-88dadf13111b@googlegroups.com> Message-ID: <4bbf7019-828c-3602-e157-de233b473a67@Damon-Family.org> On 9/19/19 6:16 AM, Eko palypse wrote: >> In all cases, if the optional parts are omitted, the code is executed in the current scope. ... >> >> >> You can see from it that "globals" is optional. >> And that, if "globals" is missing, then >> "exec" is executed in the current scope ("f1" in your case). > Thank you for your answer, and that is exactly what confuses me? > Where does x come from? If I only would read x then I would understand why > it can be found/read but I alter it and as such I either have to provide the > info that this is a global variable, declare it inside of f1 or provide > the globals dict to exec. But I don't do any of it. Why is exec able to use > the global x? > > Eren I think the issue is that x += 1 isn't exactly like x = x + 1, and this is one case that shows it. x = x + 1 is an assignment to the symbol x, which makes x a local, and thus the read becomes an undefined symbol. x += 1 is different, it isn't a plain assignment so doesn't create the local. The read of x is inherently tied to the writing of x so x stays referring to the global. -- Richard Damon From ekopalypse at gmail.com Thu Sep 19 06:52:46 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Thu, 19 Sep 2019 03:52:46 -0700 (PDT) Subject: exec and globals and locals ... In-Reply-To: References: <87muf0op0k.fsf@handshake.de> <4d91f2c4-d251-4d6f-97d1-88dadf13111b@googlegroups.com> <4bbf7019-828c-3602-e157-de233b473a67@Damon-Family.org> Message-ID: <53d6aab4-74e8-4452-ab7c-ab1df4a7d360@googlegroups.com> Am Donnerstag, 19. September 2019 12:45:35 UTC+2 schrieb Richard Damon: > On 9/19/19 6:16 AM, Eko palypse wrote: > >> In all cases, if the optional parts are omitted, the code is executed in the current scope. ... > >> > >> > >> You can see from it that "globals" is optional. > >> And that, if "globals" is missing, then > >> "exec" is executed in the current scope ("f1" in your case). > > Thank you for your answer, and that is exactly what confuses me? > > Where does x come from? If I only would read x then I would understand why > > it can be found/read but I alter it and as such I either have to provide the > > info that this is a global variable, declare it inside of f1 or provide > > the globals dict to exec. But I don't do any of it. Why is exec able to use > > the global x? > > > > Eren > > I think the issue is that x += 1 isn't exactly like x = x + 1, and this > is one case that shows it. x = x + 1 is an assignment to the symbol x, > which makes x a local, and thus the read becomes an undefined symbol. x > += 1 is different, it isn't a plain assignment so doesn't create the > local. The read of x is inherently tied to the writing of x so x stays > referring to the global. > > -- > Richard Damon Thank you that would never have come to my mind. I thought +=1 is just syntactic sugar which clearly isn't. If I do the regular x = x + 1 then I do get the expected exception. Thank you Eren From __peter__ at web.de Thu Sep 19 06:56:36 2019 From: __peter__ at web.de (Peter Otten) Date: Thu, 19 Sep 2019 12:56:36 +0200 Subject: exec and globals and locals ... References: <89b80aee-aee9-49a6-b10b-de57f9af37d0@googlegroups.com> Message-ID: Eko palypse wrote: >> Then it should be clear that the name 'test01' is put into globals(), if >> load_module() doesn't throw an exception. No sharing or nesting of >> namespaces takes place. > > Thank you too for your answer. Ok, that means that in every case when exec > imports something it has its own global namespace, right? > Is there a way to manipulate this namespace before any code from that > module gets executed? Create a module object, preload its namespace, then exec the module's code, like: $ python3 Python 3.4.3 (default, Nov 12 2018, 22:25:49) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sys, types >>> module = types.ModuleType("module") >>> module.x = 42 >>> exec("print(x)\ndef f(): return x * x", module.__dict__) 42 >>> sys.modules["module"] = module >>> import module >>> module.f() 1764 The importlib probably has an official way, but I've yet to wrap my head around that part of Python. From ekopalypse at gmail.com Thu Sep 19 07:05:02 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Thu, 19 Sep 2019 04:05:02 -0700 (PDT) Subject: exec and globals and locals ... In-Reply-To: References: <89b80aee-aee9-49a6-b10b-de57f9af37d0@googlegroups.com> Message-ID: Am Donnerstag, 19. September 2019 12:56:59 UTC+2 schrieb Peter Otten: > Eko palypse wrote: > > >> Then it should be clear that the name 'test01' is put into globals(), if > >> load_module() doesn't throw an exception. No sharing or nesting of > >> namespaces takes place. > > > > Thank you too for your answer. Ok, that means that in every case when exec > > imports something it has its own global namespace, right? > > Is there a way to manipulate this namespace before any code from that > > module gets executed? > > Create a module object, preload its namespace, then exec the module's code, > like: > > $ python3 > Python 3.4.3 (default, Nov 12 2018, 22:25:49) > [GCC 4.8.4] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> import sys, types > >>> module = types.ModuleType("module") > >>> module.x = 42 > >>> exec("print(x)\ndef f(): return x * x", module.__dict__) > 42 > >>> sys.modules["module"] = module > >>> import module > >>> module.f() > 1764 > > The importlib probably has an official way, but I've yet to wrap my head > around that part of Python. Thank you, I'm currently investigating importlib and read that __builtins__ might be another alternative. My ultimate goal would be to have objects available without the need to import them, regardless whether used in a script directly or used in an imported module. Eren From Richard at Damon-Family.org Thu Sep 19 07:40:44 2019 From: Richard at Damon-Family.org (Richard Damon) Date: Thu, 19 Sep 2019 07:40:44 -0400 Subject: exec and globals and locals ... In-Reply-To: <53d6aab4-74e8-4452-ab7c-ab1df4a7d360@googlegroups.com> References: <87muf0op0k.fsf@handshake.de> <4d91f2c4-d251-4d6f-97d1-88dadf13111b@googlegroups.com> <4bbf7019-828c-3602-e157-de233b473a67@Damon-Family.org> <53d6aab4-74e8-4452-ab7c-ab1df4a7d360@googlegroups.com> Message-ID: <5e3e8f5a-e018-a240-897a-09757ed35f40@Damon-Family.org> On 9/19/19 6:52 AM, Eko palypse wrote: > Am Donnerstag, 19. September 2019 12:45:35 UTC+2 schrieb Richard Damon: >> On 9/19/19 6:16 AM, Eko palypse wrote: >>>> In all cases, if the optional parts are omitted, the code is executed in the current scope. ... >>>> >>>> >>>> You can see from it that "globals" is optional. >>>> And that, if "globals" is missing, then >>>> "exec" is executed in the current scope ("f1" in your case). >>> Thank you for your answer, and that is exactly what confuses me? >>> Where does x come from? If I only would read x then I would understand why >>> it can be found/read but I alter it and as such I either have to provide the >>> info that this is a global variable, declare it inside of f1 or provide >>> the globals dict to exec. But I don't do any of it. Why is exec able to use >>> the global x? >>> >>> Eren >> I think the issue is that x += 1 isn't exactly like x = x + 1, and this >> is one case that shows it. x = x + 1 is an assignment to the symbol x, >> which makes x a local, and thus the read becomes an undefined symbol. x >> += 1 is different, it isn't a plain assignment so doesn't create the >> local. The read of x is inherently tied to the writing of x so x stays >> referring to the global. >> >> -- >> Richard Damon > Thank you that would never have come to my mind. > I thought +=1 is just syntactic sugar which clearly isn't. > If I do the regular x = x + 1 then I do get the expected exception. > > Thank you > Eren For some objects, += even (possibly) calls a different function __radd__ instead of __add__ (if __radd__ doesn't exist then python will fall back to using __add__) This can have a big difference in the case of sharing mutable objects. x = [1, 2] y = x x += [3] # Now x and y are [1, 2, 3] x = x + [4] # Now x = [1, 2, 3, 4] but y is still [1, 2, 3] -- Richard Damon From countryone77 at gmail.com Thu Sep 19 08:44:28 2019 From: countryone77 at gmail.com (Bev In TX) Date: Thu, 19 Sep 2019 07:44:28 -0500 Subject: exec and globals and locals ... In-Reply-To: <4bbf7019-828c-3602-e157-de233b473a67@Damon-Family.org> References: <87muf0op0k.fsf@handshake.de> <4d91f2c4-d251-4d6f-97d1-88dadf13111b@googlegroups.com> <4bbf7019-828c-3602-e157-de233b473a67@Damon-Family.org> Message-ID: <1EB0761D-71EF-40E1-82CA-489B3C7702A6@gmail.com> I?m not the OP, but I want to thank you for that clarification. I had previously not understood the ramifications of the following in section ?7. Simple statements? in ?The Python Language Reference?: ?An augmented assignment expression like x += 1 can be rewritten as x = x + 1 to achieve a similar, but not exactly equal effect. In the augmented version, x is only evaluated once. Also, when possible, the actual operation is performed in-place, meaning that rather than creating a new object and assigning that to the target, the old object is modified instead.? Thanks again. Bev > On Sep 19, 2019, at 5:45 AM, Richard Damon wrote: > > I think the issue is that x += 1 isn't exactly like x = x + 1, and this > is one case that shows it. x = x + 1 is an assignment to the symbol x, > which makes x a local, and thus the read becomes an undefined symbol. x > += 1 is different, it isn't a plain assignment so doesn't create the > local. The read of x is inherently tied to the writing of x so x stays > referring to the global. From __peter__ at web.de Thu Sep 19 09:33:44 2019 From: __peter__ at web.de (Peter Otten) Date: Thu, 19 Sep 2019 15:33:44 +0200 Subject: exec and globals and locals ... References: <87muf0op0k.fsf@handshake.de> <4d91f2c4-d251-4d6f-97d1-88dadf13111b@googlegroups.com> <4bbf7019-828c-3602-e157-de233b473a67@Damon-Family.org> Message-ID: Richard Damon wrote: > On 9/19/19 6:16 AM, Eko palypse wrote: >>> In all cases, if the optional parts are omitted, the code is executed in >>> the current scope. ... >>> >>> >>> You can see from it that "globals" is optional. >>> And that, if "globals" is missing, then >>> "exec" is executed in the current scope ("f1" in your case). >> Thank you for your answer, and that is exactly what confuses me? >> Where does x come from? If I only would read x then I would understand >> why it can be found/read but I alter it and as such I either have to >> provide the info that this is a global variable, declare it inside of f1 >> or provide the globals dict to exec. But I don't do any of it. Why is >> exec able to use the global x? >> >> Eren > > I think the issue is that x += 1 isn't exactly like x = x + 1, and this > is one case that shows it. x = x + 1 is an assignment to the symbol x, > which makes x a local, and thus the read becomes an undefined symbol. x > += 1 is different, it isn't a plain assignment so doesn't create the > local. The read of x is inherently tied to the writing of x so x stays > referring to the global. I think you are wrong; both x += 1 and x = x + 1 turn x into a local variable: >>> def f(): ... x += 1 ... >>> x = 42 >>> f() Traceback (most recent call last): File "", line 1, in File "", line 2, in f UnboundLocalError: local variable 'x' referenced before assignment >>> def g(): ... x = x + 1 ... >>> g() Traceback (most recent call last): File "", line 1, in File "", line 2, in g UnboundLocalError: local variable 'x' referenced before assignment The difference is that x = x + 1 is evaluated as x = x.__add__(1) while x += 1 becomes x = x.__iadd__(1) For mutable x this allows modifying x in place with consequences likely to surprise when you see it for the first time: >>> t = ["Nobody expects "], >>> t[0] = t[0] + ["the Spanish inquisition"] Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment >>> t (['Nobody expects '],) The above creates a new list which cannot become the first item of the (immutable) tuple. >>> t[0] += ["the Spanish inquisition"] Traceback (most recent call last): File "", line 1, in TypeError: 'tuple' object does not support item assignment The above changes the first item of the tuple in place, but afterwards the attempt to rebind t[0] still fails. From __peter__ at web.de Thu Sep 19 09:49:08 2019 From: __peter__ at web.de (Peter Otten) Date: Thu, 19 Sep 2019 15:49:08 +0200 Subject: exec and globals and locals ... References: <89b80aee-aee9-49a6-b10b-de57f9af37d0@googlegroups.com> Message-ID: Eko palypse wrote: > Thank you, I'm currently investigating importlib and read that > __builtins__ might be another alternative. > My ultimate goal would be to have objects available without the need to > import them, regardless whether used in a script directly or used in an > imported module. I'm not sure this is a good idea. Or rather, I'm pretty sure this isn't a good idea ;) To quote the Zen of Python >>> import this The Zen of Python, by Tim Peters [...] Namespaces are one honking great idea -- let's do more of those! It looks like you are replacing that line with "Let's put everything into builtins -- so convenient, no!" From smilesonisamal at gmail.com Thu Sep 19 11:05:55 2019 From: smilesonisamal at gmail.com (Pradeep Patra) Date: Thu, 19 Sep 2019 20:35:55 +0530 Subject: regular expressions help In-Reply-To: References: Message-ID: Thanks David /Anthony for your help. I figured out the issue myself. I dont need any ^, $ etc to the regex pattern and the plain string (for exp my-dog) works fine. I am looking at creating a generic method so that instead of passing my-dog i can pass my-cat or blah blah. I am thinking of creating a list of probable combinations to search from the list. Anybody have better ideas? On Thu, Sep 19, 2019 at 3:46 PM David wrote: > On Thu, 19 Sep 2019 at 19:34, Pradeep Patra > wrote: > > > Thanks David for your quick help. Appreciate it. When I tried on python > 2.7.3 the same thing you did below I got the error after matches.group(0) > as follows: > > > > AttributeError: NoneType object has no attribute 'group'. > > > > I tried to check 'None' for no match for re.search as the documentation > says but it's not working. > > > > Unfortunately I cannot update the python version now to 2.7.13 as other > programs are using this version and need to test all and it requires more > testing. Any idea how I can fix this ? I am ok to use any other re > method(not only tied to re.search) as long as it works. > > Hi again Pradeep, > > We are now on email number seven, so I am > going to try to give you some good advice ... > > When you ask on a forum like this for help, it is very > important to show people exactly what you did. > Everything that you did. In the shortest possible > way that demonstrates whatever issue you are > facing. > > It is best to give us a recipe that we can follow > exactly that shows every step that you do when > you have the problem that you need help with. > > And the best way to do that is for you to learn > how to cut and paste between where you run > your problem code, and where you send your > email message to us. > > Please observe the way that I communicated with > you last time. I sent you an exact cut and paste > from my terminal, to help you by allowing you to > duplicate exactly every step that I made. > > You should communicate with us in the same > way. Because when you write something like > your most recent message > > > I got the error after matches.group(0) as follows: > > AttributeError: NoneType object has no attribute 'group'. > > this tells us nothing useful!! Because we cannot > see everything you did leading up to that, so we > cannot reproduce your problem. > > For us to help you, you need to show all the steps, > the same way I did. > > Now, to help you, I found the same old version of > Python 2 that you have, to prove to you that it works > on your version. > > So you talking about updating Python is not going > to help. Instead, you need to work out what you > are doing that is causing your problem. > > Again, I cut and paste my whole session to show > you, see below. Notice that the top lines show that > it is the same version that you have. > > If you cut and paste my commands into > your Python then it should work the same way > for you too. > > If it does not work for you, then SHOW US THE > WHOLE SESSION, EVERY STEP, so that we can > reproduce your problem. Run your python in a terminal, > and copy and paste the output you get into your message. > > $ python > Python 2.7.3 (default, Jun 20 2016, 16:18:47) > [GCC 4.7.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> import re > >>> mystr = "where is my-dog" > >>> pattern = re.compile(r'my-dog$') > >>> matches = re.search(pattern, mystr) > >>> matches.group(0) > 'my-dog' > >>> > > I hope you realise that the re module has been used > by thousands of programmers, for many years. > So it's extremely unlikely that it "doesn't work" in a way that > gets discovered by someone who hardly knows how to use it. > -- > https://mail.python.org/mailman/listinfo/python-list > From greg.ewing at canterbury.ac.nz Thu Sep 19 04:55:16 2019 From: greg.ewing at canterbury.ac.nz (Gregory Ewing) Date: Thu, 19 Sep 2019 20:55:16 +1200 Subject: Unicode UCS2, UCS4 and ... UCS1 In-Reply-To: References: Message-ID: Eli the Bearded wrote: > There isn't anything called UCS1. Apparently there is, but it's not a character set, it's a loudspeaker. https://www.bhphotovideo.com/c/product/1205978-REG/yorkville_sound_ucs1_1200w_15_horn_loaded.html -- Greg From cdoare.ext at orange.com Thu Sep 19 03:34:59 2019 From: cdoare.ext at orange.com (cdoare.ext at orange.com) Date: Thu, 19 Sep 2019 07:34:59 +0000 Subject: Unable to start Python with Windows 7 Message-ID: <21843_1568878500_5D832FA4_21843_2_1_A2B41E5C09C6EB4D89E3D9622E69391E38A79C0B@OPEXCAUBMA1.corporate.adroot.infra.ftgroup> Hi, I am no more able to start Python from Windows 7 environment. I have the following message : "The Application was unable to start correctly, (0xC0000142). Click OK to close the application" Do you have any idea where the problem is ? Bests regards, ___________________________ Christian Doar? (Groupe Open) T?l : +33 2 96 07 02 63 Mobile : +33 6 33 51 41 15 cdoare.ext at orange.com _________________________________________________________________________________________________________________________ Ce message et ses pieces jointes peuvent contenir des informations confidentielles ou privilegiees et ne doivent donc pas etre diffuses, exploites ou copies sans autorisation. Si vous avez recu ce message par erreur, veuillez le signaler a l'expediteur et le detruire ainsi que les pieces jointes. Les messages electroniques etant susceptibles d'alteration, Orange decline toute responsabilite si ce message a ete altere, deforme ou falsifie. Merci. This message and its attachments may contain confidential or privileged information that may be protected by law; they should not be distributed, used or copied without authorisation. If you have received this email in error, please notify the sender and delete this message and its attachments. As emails may be altered, Orange is not liable for messages that have been modified, changed or falsified. Thank you. From ekopalypse at gmail.com Thu Sep 19 06:59:35 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Thu, 19 Sep 2019 03:59:35 -0700 (PDT) Subject: exec and globals and locals ... In-Reply-To: <53d6aab4-74e8-4452-ab7c-ab1df4a7d360@googlegroups.com> References: <87muf0op0k.fsf@handshake.de> <4d91f2c4-d251-4d6f-97d1-88dadf13111b@googlegroups.com> <4bbf7019-828c-3602-e157-de233b473a67@Damon-Family.org> <53d6aab4-74e8-4452-ab7c-ab1df4a7d360@googlegroups.com> Message-ID: No, I have to correct myself x = 5 def f1(): exec("x = x + 1; print('f1 in:', x)") return x print('f1 out', f1()) results in the same, for me confusing, results. f1 in: 6 f1 out 5 Eren From ekopalypse at gmail.com Thu Sep 19 12:05:46 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Thu, 19 Sep 2019 09:05:46 -0700 (PDT) Subject: Unable to start Python with Windows 7 In-Reply-To: References: <21843_1568878500_5D832FA4_21843_2_1_A2B41E5C09C6EB4D89E3D9622E69391E38A79C0B@OPEXCAUBMA1.corporate.adroot.infra.ftgroup> Message-ID: <8a3dbcde-16c3-4304-83cb-db2eec5164e8@googlegroups.com> Am Donnerstag, 19. September 2019 17:52:48 UTC+2 schrieb cdoa... at orange.com: > Hi, > I am no more able to start Python from Windows 7 environment. > I have the following message : > "The Application was unable to start correctly, (0xC0000142). Click OK to close the application" > > Do you have any idea where the problem is ? > > Bests regards, > A shot in the dark, you do have Win7 32bit and tried to start python 64bit Eren From __peter__ at web.de Thu Sep 19 12:31:11 2019 From: __peter__ at web.de (Peter Otten) Date: Thu, 19 Sep 2019 18:31:11 +0200 Subject: exec and globals and locals ... References: <87muf0op0k.fsf@handshake.de> <4d91f2c4-d251-4d6f-97d1-88dadf13111b@googlegroups.com> <4bbf7019-828c-3602-e157-de233b473a67@Damon-Family.org> <53d6aab4-74e8-4452-ab7c-ab1df4a7d360@googlegroups.com> Message-ID: Eko palypse wrote: > No, I have to correct myself > > x = 5 > def f1(): > exec("x = x + 1; print('f1 in:', x)") > return x > print('f1 out', f1()) > > results in the same, for me confusing, results. > > f1 in: 6 > f1 out 5 Inside a function exec assignments go to a *copy* of the local namespace. Also LOAD_NAME is used to look up names. Therefore you can read and then shade a global name with its local namesake. Inside a function the namespace is determined statically. As f1() has no assignment to x (code inside exec(...) is not considered) x is looked up in directly the global namespace using LOAD_GLOBAL. If you want to access the local namespace used by exec() you have to provide one explicitly: >>> x = 5 >>> def f(): ... ns = {} ... exec("x += 1", globals(), ns) ... return ns["x"] ... >>> f() 6 >>> x 5 By the way, in Python 2 where exec was a statement the local namespace is shared: >>> x = 5 >>> def f(): ... exec "x += 1" ... return x ... >>> f() 6 >>> x 5 From ekopalypse at gmail.com Thu Sep 19 12:38:04 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Thu, 19 Sep 2019 09:38:04 -0700 (PDT) Subject: exec and globals and locals ... In-Reply-To: References: <89b80aee-aee9-49a6-b10b-de57f9af37d0@googlegroups.com> Message-ID: First thank you for all the answers, very much appreciated. I assume the root cause might be explained by the zen of python as well. If the implementation is hard to explain, it's a bad idea. Maybe I need to rethink my implementation :-) Eren From ekopalypse at gmail.com Thu Sep 19 12:41:59 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Thu, 19 Sep 2019 09:41:59 -0700 (PDT) Subject: exec and globals and locals ... In-Reply-To: References: <87muf0op0k.fsf@handshake.de> <4d91f2c4-d251-4d6f-97d1-88dadf13111b@googlegroups.com> <4bbf7019-828c-3602-e157-de233b473a67@Damon-Family.org> <53d6aab4-74e8-4452-ab7c-ab1df4a7d360@googlegroups.com> Message-ID: Am Donnerstag, 19. September 2019 18:31:43 UTC+2 schrieb Peter Otten: > Eko palypse wrote: > > > No, I have to correct myself > > > > x = 5 > > def f1(): > > exec("x = x + 1; print('f1 in:', x)") > > return x > > print('f1 out', f1()) > > > > results in the same, for me confusing, results. > > > > f1 in: 6 > > f1 out 5 > > Inside a function exec assignments go to a *copy* of the local namespace. > Also LOAD_NAME is used to look up names. Therefore you can read and then > shade a global name with its local namesake. > > Inside a function the namespace is determined statically. As f1() has no > assignment to x (code inside exec(...) is not considered) x is looked up in > directly the global namespace using LOAD_GLOBAL. > > If you want to access the local namespace used by exec() you have to provide > one explicitly: > > >>> x = 5 > >>> def f(): > ... ns = {} > ... exec("x += 1", globals(), ns) > ... return ns["x"] > ... > >>> f() > 6 > >>> x > 5 > > By the way, in Python 2 where exec was a statement the local namespace is > shared: > > >>> x = 5 > >>> def f(): > ... exec "x += 1" > ... return x > ... > >>> f() > 6 > >>> x > 5 Sorry, missed that. Thank you, may I ask you how I could have come myself to that explanation? What do I have to read to get that understanding? Hopefully you don't say read the C code, because that is something I tried but failed miserably. Thank you Eren From python at mrabarnett.plus.com Thu Sep 19 13:33:02 2019 From: python at mrabarnett.plus.com (MRAB) Date: Thu, 19 Sep 2019 18:33:02 +0100 Subject: Unicode UCS2, UCS4 and ... UCS1 In-Reply-To: References: Message-ID: <9e447eb6-bfa6-1b63-54c4-a6148937426e@mrabarnett.plus.com> On 2019-09-19 09:55, Gregory Ewing wrote: > Eli the Bearded wrote: >> There isn't anything called UCS1. > > Apparently there is, but it's not a character set, it's a loudspeaker. > > https://www.bhphotovideo.com/c/product/1205978-REG/yorkville_sound_ucs1_1200w_15_horn_loaded.html > The OP might mean Py_UCS1, which is an implementation detail of the Flexible String Representation. From rosuav at gmail.com Thu Sep 19 14:17:16 2019 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 20 Sep 2019 04:17:16 +1000 Subject: regular expressions help In-Reply-To: References: Message-ID: On Fri, Sep 20, 2019 at 1:07 AM Pradeep Patra wrote: > > Thanks David /Anthony for your help. I figured out the issue myself. I > dont need any ^, $ etc to the regex pattern and the plain string (for exp > my-dog) works fine. I am looking at creating a generic method so that > instead of passing my-dog i can pass my-cat or blah blah. I am thinking of > creating a list of probable combinations to search from the list. Anybody > have better ideas? > If you just want to find a string in another string, don't use regular expressions at all! Just ask Python directly: >>> print("my-cat" in "This is where you can find my-cat, look, see!") True ChrisA From __peter__ at web.de Thu Sep 19 14:24:21 2019 From: __peter__ at web.de (Peter Otten) Date: Thu, 19 Sep 2019 20:24:21 +0200 Subject: exec and globals and locals ... References: <87muf0op0k.fsf@handshake.de> <4d91f2c4-d251-4d6f-97d1-88dadf13111b@googlegroups.com> <4bbf7019-828c-3602-e157-de233b473a67@Damon-Family.org> <53d6aab4-74e8-4452-ab7c-ab1df4a7d360@googlegroups.com> Message-ID: Eko palypse wrote: > Am Donnerstag, 19. September 2019 18:31:43 UTC+2 schrieb Peter Otten: >> Eko palypse wrote: >> >> > No, I have to correct myself >> > >> > x = 5 >> > def f1(): >> > exec("x = x + 1; print('f1 in:', x)") >> > return x >> > print('f1 out', f1()) >> > >> > results in the same, for me confusing, results. >> > >> > f1 in: 6 >> > f1 out 5 >> >> Inside a function exec assignments go to a *copy* of the local namespace. >> Also LOAD_NAME is used to look up names. Therefore you can read and then >> shade a global name with its local namesake. >> >> Inside a function the namespace is determined statically. As f1() has no >> assignment to x (code inside exec(...) is not considered) x is looked up >> in directly the global namespace using LOAD_GLOBAL. >> >> If you want to access the local namespace used by exec() you have to >> provide one explicitly: >> >> >>> x = 5 >> >>> def f(): >> ... ns = {} >> ... exec("x += 1", globals(), ns) >> ... return ns["x"] >> ... >> >>> f() >> 6 >> >>> x >> 5 >> >> By the way, in Python 2 where exec was a statement the local namespace is >> shared: >> >> >>> x = 5 >> >>> def f(): >> ... exec "x += 1" >> ... return x >> ... >> >>> f() >> 6 >> >>> x >> 5 > > Sorry, missed that. > Thank you, may I ask you how I could have come myself > to that explanation? What do I have to read to get that understanding? > Hopefully you don't say read the C code, because that is something > I tried but failed miserably. https://docs.python.org/3/library/functions.html#exec https://docs.python.org/3/reference/executionmodel.html#naming-and-binding (I had to google for the second link.) I usually experiment with code and the disassembler. I find its output quite readable, >>> def f(): x += 1 ... >>> import dis >>> dis.dis(f) 1 0 LOAD_FAST 0 (x) 3 LOAD_CONST 1 (1) 6 INPLACE_ADD 7 STORE_FAST 0 (x) 10 LOAD_CONST 0 (None) 13 RETURN_VALUE >>> dis.dis(compile("x += 1", "", "exec")) 1 0 LOAD_NAME 0 (x) 3 LOAD_CONST 0 (1) 6 INPLACE_ADD 7 STORE_NAME 0 (x) 10 LOAD_CONST 1 (None) 13 RETURN_VALUE and you can limit yourself to small snippets. From ekopalypse at gmail.com Thu Sep 19 15:04:41 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Thu, 19 Sep 2019 12:04:41 -0700 (PDT) Subject: exec and globals and locals ... In-Reply-To: References: <87muf0op0k.fsf@handshake.de> <4d91f2c4-d251-4d6f-97d1-88dadf13111b@googlegroups.com> <4bbf7019-828c-3602-e157-de233b473a67@Damon-Family.org> <53d6aab4-74e8-4452-ab7c-ab1df4a7d360@googlegroups.com> Message-ID: <7071f640-0102-49cf-9544-277e8969e744@googlegroups.com> Am Donnerstag, 19. September 2019 20:24:49 UTC+2 schrieb Peter Otten: > Eko palypse wrote: > > > Am Donnerstag, 19. September 2019 18:31:43 UTC+2 schrieb Peter Otten: > >> Eko palypse wrote: > >> > >> > No, I have to correct myself > >> > > >> > x = 5 > >> > def f1(): > >> > exec("x = x + 1; print('f1 in:', x)") > >> > return x > >> > print('f1 out', f1()) > >> > > >> > results in the same, for me confusing, results. > >> > > >> > f1 in: 6 > >> > f1 out 5 > >> > >> Inside a function exec assignments go to a *copy* of the local namespace. > >> Also LOAD_NAME is used to look up names. Therefore you can read and then > >> shade a global name with its local namesake. > >> > >> Inside a function the namespace is determined statically. As f1() has no > >> assignment to x (code inside exec(...) is not considered) x is looked up > >> in directly the global namespace using LOAD_GLOBAL. > >> > >> If you want to access the local namespace used by exec() you have to > >> provide one explicitly: > >> > >> >>> x = 5 > >> >>> def f(): > >> ... ns = {} > >> ... exec("x += 1", globals(), ns) > >> ... return ns["x"] > >> ... > >> >>> f() > >> 6 > >> >>> x > >> 5 > >> > >> By the way, in Python 2 where exec was a statement the local namespace is > >> shared: > >> > >> >>> x = 5 > >> >>> def f(): > >> ... exec "x += 1" > >> ... return x > >> ... > >> >>> f() > >> 6 > >> >>> x > >> 5 > > > > Sorry, missed that. > > Thank you, may I ask you how I could have come myself > > to that explanation? What do I have to read to get that understanding? > > Hopefully you don't say read the C code, because that is something > > I tried but failed miserably. > > https://docs.python.org/3/library/functions.html#exec > https://docs.python.org/3/reference/executionmodel.html#naming-and-binding > > (I had to google for the second link.) > > I usually experiment with code and the disassembler. I find its output quite > readable, > > >>> def f(): x += 1 > ... > >>> import dis > >>> dis.dis(f) > 1 0 LOAD_FAST 0 (x) > 3 LOAD_CONST 1 (1) > 6 INPLACE_ADD > 7 STORE_FAST 0 (x) > 10 LOAD_CONST 0 (None) > 13 RETURN_VALUE > >>> dis.dis(compile("x += 1", "", "exec")) > 1 0 LOAD_NAME 0 (x) > 3 LOAD_CONST 0 (1) > 6 INPLACE_ADD > 7 STORE_NAME 0 (x) > 10 LOAD_CONST 1 (None) > 13 RETURN_VALUE > > and you can limit yourself to small snippets. Thank you very much, really, very much appreciated. Normally I do use docs.python.org as my first resource to look for an answer to my questions but I'm not always convinced that my understanding/interpretation is correct. Then I research on the web to see if there are similar questions and try to understand these explanations but ... and in this particular case I wasn't even able to find an answer. The dis module is nice to see differences but I assume more useful to software developers than to hobby programmers like me. At least not in the current state of knowledge I have. I was under the impression that I have a good understanding of the python language before I started my current project but now ... Enough whining :-) Once again, Thank you very much Eren From Cecil at decebal.nl Thu Sep 19 15:07:51 2019 From: Cecil at decebal.nl (Cecil Westerhof) Date: Thu, 19 Sep 2019 21:07:51 +0200 Subject: What about idea of making a "Pythonic Lisp"...i.e. a Lisp that more closely resembles the syntax of Python? References: <4bf8659c-b04b-4147-9c76-551c2b535bf7@googlegroups.com> <87d0g21fqt.fsf@nightsong.com> <1c3b76f2-7a62-4b84-a681-297ea5967b7d@googlegroups.com> <87pnjwfx3h.fsf@nightsong.com> Message-ID: <871rwcgl4o.fsf@munus.decebal.nl> Paul Rubin writes: > Python 3.7.3 (default, Apr 3 2019, 05:39:12) > Type "help", "copyright", "credits" or "license" for more information. > >>> a = range(10) > >>> b = reversed(a) > >>> sum(a) == sum(b) > True > >>> sum(b) == sum(a) > False Why does this happen? By the way, when you change the last statement to: sum(a) == sum(b) you also get False. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof From rosuav at gmail.com Thu Sep 19 15:20:38 2019 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 20 Sep 2019 05:20:38 +1000 Subject: What about idea of making a "Pythonic Lisp"...i.e. a Lisp that more closely resembles the syntax of Python? In-Reply-To: <871rwcgl4o.fsf@munus.decebal.nl> References: <4bf8659c-b04b-4147-9c76-551c2b535bf7@googlegroups.com> <87d0g21fqt.fsf@nightsong.com> <1c3b76f2-7a62-4b84-a681-297ea5967b7d@googlegroups.com> <87pnjwfx3h.fsf@nightsong.com> <871rwcgl4o.fsf@munus.decebal.nl> Message-ID: On Fri, Sep 20, 2019 at 5:16 AM Cecil Westerhof wrote: > > Paul Rubin writes: > > > Python 3.7.3 (default, Apr 3 2019, 05:39:12) > > Type "help", "copyright", "credits" or "license" for more information. > > >>> a = range(10) > > >>> b = reversed(a) > > >>> sum(a) == sum(b) > > True > > >>> sum(b) == sum(a) > > False > > Why does this happen? > > By the way, when you change the last statement to: > sum(a) == sum(b) > > you also get False. >>> sum(range(10)) == sum(reversed(range(10))) True If you actually want a reversed range, use slice notation instead of the reversed() function, which is more parallel to iter(). >>> a = range(10) >>> b = a[::-1] >>> sum(a) == sum(b) True >>> sum(b) == sum(a) True Now you actually have a range that runs the other direction, instead of an iterator that runs through the range backwards. ChrisA From Ralf_M at t-online.de Thu Sep 19 16:44:37 2019 From: Ralf_M at t-online.de (Ralf M.) Date: Thu, 19 Sep 2019 22:44:37 +0200 Subject: Spread a statement over various lines In-Reply-To: References: <20190917205947.6de1383f@arcor.com> <06054bfb-073d-14fd-0441-664b1fdc4348@t-online.de> Message-ID: <06a4c84c-39b5-ba24-3c14-3fa929052158@t-online.de> Am 18.09.2019 um 22:22 schrieb Chris Angelico: > On Thu, Sep 19, 2019 at 6:20 AM Ralf M. wrote: >> >> Am 17.09.2019 um 20:59 schrieb Manfred Lotz: >>> I have a function like follows >>> >>> def regex_from_filepat(fpat): >>> rfpat = fpat.replace('.', '\\.') \ >>> .replace('%', '.') \ >>> .replace('*', '.*') >>> >>> return '^' + rfpat + '$' >>> >>> >>> As I don't want to have the replace() functions in one line my >>> question is if it is ok to spread the statement over various lines as >>> shown above, or if there is a better way? >>> >>> Thanks. >>> >> >> Not related to your question, but: >> You seem to try to convert a Windows wildcard pattern to a regex >> pattern. However, wildcards sometimes behave a bit different than what >> you assume. I know for instance that *.* matches any filename, even if >> the filename doesn't contain a dot. > > Hmm, why do you assume it's a Windows wildcard pattern specifically? > > ChrisA > I think I jumped to that conclusion because the example didn't treat [ ] as special characters. [ ] are special in a unix shell, but not at a cmd prompt. Thinking it over, [ ] would need a much differnt treatment and might be left out of the example for that reason, though. Also I may be biased: I mostly use Windows, Linux only occasionally. Ralf M. From Ralf_M at t-online.de Thu Sep 19 17:10:40 2019 From: Ralf_M at t-online.de (Ralf M.) Date: Thu, 19 Sep 2019 23:10:40 +0200 Subject: Spread a statement over various lines In-Reply-To: References: <20190917205947.6de1383f@arcor.com> <06054bfb-073d-14fd-0441-664b1fdc4348@t-online.de> Message-ID: Am 18.09.2019 um 22:24 schrieb Alexandre Brault: > > On 2019-09-18 4:01 p.m., Ralf M. wrote: >> >> I don't know the exact rules of Windows wildcards, so there may be >> even more cases of unexpected behavior. >> If anyone knows where to find the complete rules (or a python module >> that implements them), I would be interested. >> > > fnmatch in the standard library has a translate function that transforms > a glob pattern to a regex > > https://docs.python.org/3.7/library/fnmatch.html#fnmatch.translate > > Alex > Thank you for the pointer. However, from the documentation of module fnmatch: "This module provides support for Unix shell-style wildcards" And Unix shell-style wildcards differ from Windows cmd wildcards. For one, [ ] are special in Unix shells, but not in Windows cmd. For another, cmd wildcards have several quirks, e.g. *.* matching filenames that don't contain a dot, or "a???" matching "ab". Several years ago, when I needed DOS-style globbing, I copied fnmatch.py and glob.py from the standard library and modified them to treat [ ] as not special. However, that didn't help with the cmd quirks, and I don't even know all the rules about cmd wildcards. Ralf From jfong at ms4.hinet.net Thu Sep 19 23:23:43 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Thu, 19 Sep 2019 20:23:43 -0700 (PDT) Subject: exec and globals and locals ... In-Reply-To: References: <87muf0op0k.fsf@handshake.de> <4d91f2c4-d251-4d6f-97d1-88dadf13111b@googlegroups.com> <4bbf7019-828c-3602-e157-de233b473a67@Damon-Family.org> <53d6aab4-74e8-4452-ab7c-ab1df4a7d360@googlegroups.com> Message-ID: <2655eaa5-d3ff-4e24-a980-e5d8ad25cfc1@googlegroups.com> >>> x = 3 >>> def foo(): ... exec("print(globals(), locals()); x = x + 1; print(globals(), locals())") ... >>> foo() {'foo': , '__package__': None, '__builtins__': , '__loader__': , '__doc__': None, '__name__': '__main__', '__spec__': None, 'x': 3} {} {'foo': , '__package__': None, '__builtins__': , '__loader__': , '__doc__': None, '__name__': '__main__', '__spec__': None, 'x': 3} {'x': 4} >>> def goo(): ... print(globals(), locals()) ... x = x + 1 ... print(globals(), locals()) ... >>> goo() {'foo': , '__package__': None, '__builtins__': , '__loader__': , '__doc__': None, '__name__': '__main__', '__spec__': None, 'goo': , 'x': 3} {} Traceback (most recent call last): File "", line 1, in File "", line 3, in goo UnboundLocalError: local variable 'x' referenced before assignment >>> Can't figure out what makes it different:-( --Jach From aldwinaldwindev at gmail.com Fri Sep 20 00:08:14 2019 From: aldwinaldwindev at gmail.com (Aldwin Pollefeyt) Date: Fri, 20 Sep 2019 12:08:14 +0800 Subject: numpy results in segmentation fault In-Reply-To: References: Message-ID: use: num_arr1 = numpy.array(tgt_arr1, dtype=int) num_arr2 = numpy.array(tgt_arr2, dtype=int) On Mon, Sep 16, 2019 at 5:36 PM Pradeep Patra wrote: > Yes it is crashing in the hackerrank site and the testcases fails with > segmentation fault. I tried to install numpy with 3.7.3 and it is for some > reason not working and after import when I run import numpy at python > console and press enter I get >>? i,e its not working properly. > > Can you please help letting me know the python and numpy compatibility > matrix or I am missing anything? > > I tried some of the numpy code from the other github and it also fails with > the segmentation fault :-(. I am guessing some numpy version compatility > issue or some environment issue. > > On Thu, Sep 12, 2019 at 8:00 PM Thomas Jollans wrote: > > > On 12/09/2019 15.53, Pradeep Patra wrote: > > > Hi , > > > > > > I was trying to solve the hackerrank and was using python 3.7.x. > > > https://www.hackerrank.com/challenges/np-concatenate/problem > > > > > > While running the code sometimes I get success result and sometimes it > > > fails with "Segmentation Fault" at Hacker rank UI. I dont have any clue > > why > > > the code is crashing ? Does anyone have any idea? > > > > > > Are you sure it's your code that's crashing, and not something beyond > > your control? (Such as the software that is starting Python for you) > > Does it depend on the input? Can you reproduce the issue in a controlled > > environment (i.e. on your own PC)? > > > > > > > > > > Regards > > > Pradeep > > > > > > import numpy > > > > > > n,m,p = map(int,input().split()) > > > tgt_arr1 = [] > > > for i in range(n): > > > row = list(map(int,input().split())) > > > tgt_arr1.append(row) > > > tgt_arr2 = [] > > > for j in range(m): > > > row = list(map(int,input().split())) > > > tgt_arr2.append(row) > > > > > > num_arr1 = numpy.array(tgt_arr1,int) > > > num_arr2 = numpy.array(tgt_arr2,int) > > > > > > print(numpy.concatenate((num_arr1,num_arr2),axis=0)) > > > > > > -- > > https://mail.python.org/mailman/listinfo/python-list > > > -- > https://mail.python.org/mailman/listinfo/python-list > From __peter__ at web.de Fri Sep 20 03:31:09 2019 From: __peter__ at web.de (Peter Otten) Date: Fri, 20 Sep 2019 09:31:09 +0200 Subject: exec and globals and locals ... References: <87muf0op0k.fsf@handshake.de> <4d91f2c4-d251-4d6f-97d1-88dadf13111b@googlegroups.com> <4bbf7019-828c-3602-e157-de233b473a67@Damon-Family.org> <53d6aab4-74e8-4452-ab7c-ab1df4a7d360@googlegroups.com> <2655eaa5-d3ff-4e24-a980-e5d8ad25cfc1@googlegroups.com> Message-ID: jfong at ms4.hinet.net wrote: >>>> x = 3 >>>> def foo(): > ... exec("print(globals(), locals()); x = x + 1; print(globals(), > locals())") ... >>>> foo() > {'foo': , '__package__': None, '__builtins__': > {, '__loader__': {'_frozen_importlib.BuiltinImporter'>, '__doc__': None, '__name__': > {'__main__', '__spec__': None, 'x': 3} {} 'foo': {0x021C3468>, '__package__': None, '__builtins__': {(built-in)>, '__loader__': , > {'__doc__': None, '__name__': '__main__', '__spec__': None, 'x': 3} {'x': > {4} >>>> def goo(): > ... print(globals(), locals()) > ... x = x + 1 > ... print(globals(), locals()) > ... >>>> goo() > {'foo': , '__package__': None, '__builtins__': > {, '__loader__': {'_frozen_importlib.BuiltinImporter'>, '__doc__': None, '__name__': > {'__main__', '__spec__': None, 'goo': , 'x': > {3} {} > Traceback (most recent call last): > File "", line 1, in > File "", line 3, in goo > UnboundLocalError: local variable 'x' referenced before assignment >>>> > > Can't figure out what makes it different:-( (0) exec() and class definitions use the LOAD_NAME opcode which looks into the local namespace first and falls back to the global namespace. Therefore x = x may look up a global x and assign to a local one The function body uses either (1) if there is a name binding operation (assignment, augmented assignment, def, ...) LOAD_FAST which only looks into the local namespace. Thus x = x will only succeed if x is already defined in the local namespace. (2) if there is no binding operation (name appears in an expression, x[...] = ... or attribute assignment) LOAD_GLOBAL which only looks into the local namespace. Your first example is case 0, your second is case 1. From jfong at ms4.hinet.net Fri Sep 20 04:28:25 2019 From: jfong at ms4.hinet.net (jfong at ms4.hinet.net) Date: Fri, 20 Sep 2019 01:28:25 -0700 (PDT) Subject: exec and globals and locals ... In-Reply-To: References: <87muf0op0k.fsf@handshake.de> <4d91f2c4-d251-4d6f-97d1-88dadf13111b@googlegroups.com> <4bbf7019-828c-3602-e157-de233b473a67@Damon-Family.org> <53d6aab4-74e8-4452-ab7c-ab1df4a7d360@googlegroups.com> <2655eaa5-d3ff-4e24-a980-e5d8ad25cfc1@googlegroups.com> Message-ID: Peter Otten? 2019?9?20???? UTC+8??3?31?48???? > jfong at ms4.hinet.net wrote: > > >>>> x = 3 > >>>> def foo(): > > ... exec("print(globals(), locals()); x = x + 1; print(globals(), > > locals())") ... > >>>> foo() > > {'foo': , '__package__': None, '__builtins__': > > {, '__loader__': > {'_frozen_importlib.BuiltinImporter'>, '__doc__': None, '__name__': > > {'__main__', '__spec__': None, 'x': 3} {} 'foo': > {0x021C3468>, '__package__': None, '__builtins__': > {(built-in)>, '__loader__': , > > {'__doc__': None, '__name__': '__main__', '__spec__': None, 'x': 3} {'x': > > {4} > >>>> def goo(): > > ... print(globals(), locals()) > > ... x = x + 1 > > ... print(globals(), locals()) > > ... > >>>> goo() > > {'foo': , '__package__': None, '__builtins__': > > {, '__loader__': > {'_frozen_importlib.BuiltinImporter'>, '__doc__': None, '__name__': > > {'__main__', '__spec__': None, 'goo': , 'x': > > {3} {} > > Traceback (most recent call last): > > File "", line 1, in > > File "", line 3, in goo > > UnboundLocalError: local variable 'x' referenced before assignment > >>>> > > > > Can't figure out what makes it different:-( > > (0) exec() and class definitions use the LOAD_NAME opcode which looks into > the local namespace first and falls back to the global namespace. Therefore > x = x may look up a global x and assign to a local one > > The function body uses either > > (1) if there is a name binding operation (assignment, augmented assignment, > def, ...) LOAD_FAST which only looks into the local namespace. Thus > x = x will only succeed if x is already defined in the local namespace. > > (2) if there is no binding operation (name appears in an expression, x[...] > = ... or attribute assignment) LOAD_GLOBAL which only looks into the local > namespace. > > Your first example is case 0, your second is case 1. Hmm... exec() seems follow a more "LEGB" rule:-) Thank you. From fredvincent at live.ca Thu Sep 19 22:59:32 2019 From: fredvincent at live.ca (Fred Vincent) Date: Fri, 20 Sep 2019 02:59:32 +0000 Subject: Won't Uninstall Message-ID: Python 3.7.4 won?t uninstall, I have tried doing what I can such as going to the control panel and uninstalling the program there, but that did not work. Since I am unable to delete it I am unable to download a different version of python. How do I fix this and fully uninstall python? Sent from Mail for Windows 10 From python at mrabarnett.plus.com Fri Sep 20 13:49:45 2019 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 20 Sep 2019 18:49:45 +0100 Subject: Won't Uninstall In-Reply-To: References: Message-ID: <36a322af-102b-9e8a-61a4-17c68436e1b0@mrabarnett.plus.com> On 2019-09-20 03:59, Fred Vincent wrote: > Python 3.7.4 won?t uninstall, I have tried doing what I can such as going to the control panel and uninstalling the program there, but that did not work. Since I am unable to delete it I am unable to download a different version of python. How do I fix this and fully uninstall python? > You can have multiple versions of Python installed at the same time. For testing purposes, I have Python 2.7 and from Python 3.5 to a release candidate of the upcoming Python 3.8, both 32-bit and 64-bit for all of them. From barry at barrys-emacs.org Fri Sep 20 14:02:10 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Fri, 20 Sep 2019 19:02:10 +0100 Subject: regular expressions help In-Reply-To: References: Message-ID: When I'm debugging a regex I make the regex shorter and shorter to figure out what the problem is. Try starting with re.compile(r'm') and then add the chars one by one seeing what happens as the string gets longer. Barry > On 19 Sep 2019, at 09:41, Pradeep Patra wrote: > > I am using python 2.7.6 but I also tried on python 3.7.3. > > On Thursday, September 19, 2019, Pradeep Patra > wrote: > >> Beginning of the string. But I tried removing that as well and it still >> could not find it. When I tested at www.regex101.com and it matched >> successfully whereas I may be wrong. Could you please help here? >> >> On Thursday, September 19, 2019, David wrote: >> >>> On Thu, 19 Sep 2019 at 17:51, Pradeep Patra >>> wrote: >>>> >>>> pattern=re.compile(r'^my\-dog$') >>>> matches = re.search(mystr) >>>> >>>> In the above example both cases(match/not match) the matches returns >>> "None" >>> >>> Hi, do you know what the '^' character does in your pattern? >>> >> > -- > https://mail.python.org/mailman/listinfo/python-list > From dschwartz0705 at gmail.com Sat Sep 21 11:53:00 2019 From: dschwartz0705 at gmail.com (Dave Martin) Date: Sat, 21 Sep 2019 08:53:00 -0700 (PDT) Subject: Python shows error on line 15 that i cant fix Message-ID: <33f981d1-3907-4a8b-80dd-a89e40eb4756@googlegroups.com> # starAbsMags=df['radial_velocity'] #GaiaPandasEscapeVelocityCode import pandas as pd import numpy as np from astropy.io import fits import astropy import matplotlib.pyplot as plt #get the combined data and load the fits files fits_filename="Gaia_DR2/gaiadr2_100pc.fits" df=pd.DataFrame() with fits.open(fits_filename) as data: df=pd.DataFrame(data[1].data) df.columns=[c.lower() for c in df.columns] print("Columns.") print(df.columns.values) print("n/n") #print out some data meta info to see what we're working with print("Number of stars:") nstars=len(df) print(nstars) distances = (df['parallax']/1000) starAbsMags =df['phot_g_mean_mag'] df = df[(df.parallax_over_error > 10 ) ] print("Left after filter: " +str(len(df)/float(nstars)*100)+" %") df.hist(column='radial_velocity') #fastdf=df[(df.radial_velocity > 200) | (df.radial_velocity < -200)] fastdf=df[(df.radial_velocity > 550)|(df.radial_velocity<-550)] print(len(fastdf)) #print(fastdf)# starTemps=df['astrometric_weight_al'] # df.plot.scatter("radial_velocity", "astrometric_weight_al", s=1, c="radial_velocity", colormap="plasma") # #df=df[(df.radial_velocity>=-550)] # #plt.axis([0,400,-800,-550]) # #plt.axis([0,400,550,800]) # plt.xlabel('weight(Au)') # plt.ylabel('Speed') # plt.title('Gaia Speed vs Weight') From dschwartz0705 at gmail.com Sat Sep 21 11:55:15 2019 From: dschwartz0705 at gmail.com (Dave Martin) Date: Sat, 21 Sep 2019 08:55:15 -0700 (PDT) Subject: python is bugging Message-ID: <02148621-4c27-4751-8035-d6d0e85ecc00@googlegroups.com> what does expected an indented block From dschwartz0705 at gmail.com Sat Sep 21 11:57:23 2019 From: dschwartz0705 at gmail.com (Dave Martin) Date: Sat, 21 Sep 2019 08:57:23 -0700 (PDT) Subject: python is bugging In-Reply-To: <02148621-4c27-4751-8035-d6d0e85ecc00@googlegroups.com> References: <02148621-4c27-4751-8035-d6d0e85ecc00@googlegroups.com> Message-ID: <8b57e52e-3e00-4e77-9196-5e8b01e52f2c@googlegroups.com> On Saturday, September 21, 2019 at 11:55:29 AM UTC-4, Dave Martin wrote: > what does expected an indented block *what does an indented block mean? From brian.j.oney at googlemail.com Sat Sep 21 12:44:08 2019 From: brian.j.oney at googlemail.com (Brian Oney) Date: Sat, 21 Sep 2019 18:44:08 +0200 Subject: python is bugging In-Reply-To: <8b57e52e-3e00-4e77-9196-5e8b01e52f2c@googlegroups.com> References: <02148621-4c27-4751-8035-d6d0e85ecc00@googlegroups.com> <8b57e52e-3e00-4e77-9196-5e8b01e52f2c@googlegroups.com> Message-ID: On Sat, 2019-09-21 at 08:57 -0700, Dave Martin wrote: > On Saturday, September 21, 2019 at 11:55:29 AM UTC-4, Dave Martin > wrote: > > what does expected an indented block > > *what does an indented block mean? It means that the line of code belongs to a certain body as defined above its position. Please follow the tutorial. https://docs.python.org/3/tutorial/index.html From dschwartz0705 at gmail.com Sat Sep 21 13:08:05 2019 From: dschwartz0705 at gmail.com (Dave Martin) Date: Sat, 21 Sep 2019 10:08:05 -0700 (PDT) Subject: python is bugging In-Reply-To: References: <02148621-4c27-4751-8035-d6d0e85ecc00@googlegroups.com> <8b57e52e-3e00-4e77-9196-5e8b01e52f2c@googlegroups.com> Message-ID: On Saturday, September 21, 2019 at 12:44:27 PM UTC-4, Brian Oney wrote: > On Sat, 2019-09-21 at 08:57 -0700, Dave Martin wrote: > > On Saturday, September 21, 2019 at 11:55:29 AM UTC-4, Dave Martin > > wrote: > > > what does expected an indented block > > > > *what does an indented block mean? > > It means that the line of code belongs to a certain body as defined > above its position. > > Please follow the tutorial. > > https://docs.python.org/3/tutorial/index.html df.to_csv(r"faststars.csv", index=None,header=True) # starAbsMags=df['radial_velocity'] #GaiaPandasEscapeVelocityCode import pandas as pd import numpy as np from astropy.io import fits import astropy import matplotlib.pyplot as plt #get the combined data and load the fits files fits_filename="Gaia_DR2/gaiadr2_100pc.fits" df=pd.DataFrame() with fits.open(fits_filename) as data: df=pd.DataFrame(data[1].data) df.columns=[c.lower() for c in df.columns] print("Columns.") print(df.columns.values) print("n/n") #print out some data meta info to see what we're working with print("Number of stars:") nstars=len(df) print(nstars) distances = (df['parallax']/1000) starAbsMags =df['phot_g_mean_mag'] df = df[(df.parallax_over_error > 10 ) ] print("Left after filter: " +str(len(df)/float(nstars)*100)+" %") df.hist(column='radial_velocity') #fastdf=df[(df.radial_velocity > 200) | (df.radial_velocity < -200)] fastdf=df[(df.radial_velocity > 550)|(df.radial_velocity<-550)] print(len(fastdf)) #print(fastdf)# starTemps=df['astrometric_weight_al'] # df.plot.scatter("radial_velocity", "astrometric_weight_al", s=1, c="radial_velocity", colormap="plasma") # #df=df[(df.radial_velocity>=-550)] # #plt.axis([0,400,-800,-550]) # #plt.axis([0,400,550,800]) # plt.xlabel('weight(Au)') # plt.ylabel('Speed') # plt.title('Gaia Speed vs Weight') this is my code the error is on line 15 From tjreedy at udel.edu Sat Sep 21 13:32:38 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Sat, 21 Sep 2019 13:32:38 -0400 Subject: Python shows error on line 15 that i cant fix In-Reply-To: <33f981d1-3907-4a8b-80dd-a89e40eb4756@googlegroups.com> References: <33f981d1-3907-4a8b-80dd-a89e40eb4756@googlegroups.com> Message-ID: On 9/21/2019 11:53 AM, Dave Martin wrote: > > # starAbsMags=df['radial_velocity'] > > #GaiaPandasEscapeVelocityCode > > import pandas as pd > import numpy as np > from astropy.io import fits > import astropy > import matplotlib.pyplot as plt > > > #get the combined data and load the fits files > > fits_filename="Gaia_DR2/gaiadr2_100pc.fits" > df=pd.DataFrame() > with fits.open(fits_filename) as data: > df=pd.DataFrame(data[1].data) A 'with' statement is a compound statement. It must be followed by a 'suite', which usually consists of an indented block of statements. This is line 17 from the first non-blank line you posted. Please stop spamming the list with multiple posts. Do spend a few hours reading the tutorial until you understand my answer. https://docs.python.org/3/tutorial/index.html Also read https://stackoverflow.com/help/minimal-reproducible-example so you can ask better questions. I presume you got "SyntaxError: expected an indented block". A minimal example getting this error is, for instance, while True: a = 1 -- Terry Jan Reedy From dschwartz0705 at gmail.com Sat Sep 21 13:57:15 2019 From: dschwartz0705 at gmail.com (Dave Martin) Date: Sat, 21 Sep 2019 10:57:15 -0700 (PDT) Subject: Python shows error on line 15 that i cant fix In-Reply-To: References: <33f981d1-3907-4a8b-80dd-a89e40eb4756@googlegroups.com> Message-ID: <45a92ec1-f0f5-4298-b357-a015797680e0@googlegroups.com> On Saturday, September 21, 2019 at 1:33:12 PM UTC-4, Terry Reedy wrote: > On 9/21/2019 11:53 AM, Dave Martin wrote: > > > > # starAbsMags=df['radial_velocity'] > > > > #GaiaPandasEscapeVelocityCode > > > > import pandas as pd > > import numpy as np > > from astropy.io import fits > > import astropy > > import matplotlib.pyplot as plt > > > > > > #get the combined data and load the fits files > > > > fits_filename="Gaia_DR2/gaiadr2_100pc.fits" > > df=pd.DataFrame() > > with fits.open(fits_filename) as data: > > df=pd.DataFrame(data[1].data) > > A 'with' statement is a compound statement. It must be followed by a > 'suite', which usually consists of an indented block of statements. > This is line 17 from the first non-blank line you posted. > > Please stop spamming the list with multiple posts. Do spend a few hours > reading the tutorial until you understand my answer. > https://docs.python.org/3/tutorial/index.html Also read > https://stackoverflow.com/help/minimal-reproducible-example > so you can ask better questions. > > I presume you got "SyntaxError: expected an indented block". > A minimal example getting this error is, for instance, > > while True: > a = 1 > > -- > Terry Jan Reedy Can you provide an example of how to use the suite feature. Thank you. From robertvstepp at gmail.com Sat Sep 21 14:45:47 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 21 Sep 2019 13:45:47 -0500 Subject: Python shows error on line 15 that i cant fix In-Reply-To: <45a92ec1-f0f5-4298-b357-a015797680e0@googlegroups.com> References: <33f981d1-3907-4a8b-80dd-a89e40eb4756@googlegroups.com> <45a92ec1-f0f5-4298-b357-a015797680e0@googlegroups.com> Message-ID: On Sat, Sep 21, 2019 at 1:01 PM Dave Martin wrote: > > On Saturday, September 21, 2019 at 1:33:12 PM UTC-4, Terry Reedy wrote: > > On 9/21/2019 11:53 AM, Dave Martin wrote: [...] > > > #get the combined data and load the fits files > > > > > > fits_filename="Gaia_DR2/gaiadr2_100pc.fits" > > > df=pd.DataFrame() > > > with fits.open(fits_filename) as data: > > > df=pd.DataFrame(data[1].data) > > > > A 'with' statement is a compound statement. It must be followed by a > > 'suite', which usually consists of an indented block of statements. > > This is line 17 from the first non-blank line you posted. [...] > Can you provide an example of how to use the suite feature. Thank you. Dave, you seem to have some expectation that you should be given the answer. That is not how help is given in this forum. You are expected to be doing the needed to work before being helped further. You have been referred to the tutorial multiple times. Please read it! Indentation is so fundamental to structuring Python code that it is clear that you need grounding in Python fundamentals. Otherwise you are essentially Easter-egging through a code sample that you have no true understanding of. If you must continue to Easter-egg Python instead of reading the tutorial (or something equivalent) then check the section of the tutorial on files. You will find examples of the use of "with" there. -- boB From dschwartz0705 at gmail.com Sat Sep 21 14:51:55 2019 From: dschwartz0705 at gmail.com (Dave Martin) Date: Sat, 21 Sep 2019 11:51:55 -0700 (PDT) Subject: Python shows error on line 15 that i cant fix In-Reply-To: References: <33f981d1-3907-4a8b-80dd-a89e40eb4756@googlegroups.com> <45a92ec1-f0f5-4298-b357-a015797680e0@googlegroups.com> Message-ID: On Saturday, September 21, 2019 at 2:46:15 PM UTC-4, boB Stepp wrote: > On Sat, Sep 21, 2019 at 1:01 PM Dave Martin wrote: > > > > On Saturday, September 21, 2019 at 1:33:12 PM UTC-4, Terry Reedy wrote: > > > On 9/21/2019 11:53 AM, Dave Martin wrote: > [...] > > > > #get the combined data and load the fits files > > > > > > > > fits_filename="Gaia_DR2/gaiadr2_100pc.fits" > > > > df=pd.DataFrame() > > > > with fits.open(fits_filename) as data: > > > > df=pd.DataFrame(data[1].data) > > > > > > A 'with' statement is a compound statement. It must be followed by a > > > 'suite', which usually consists of an indented block of statements. > > > This is line 17 from the first non-blank line you posted. > [...] > > > Can you provide an example of how to use the suite feature. Thank you. > > Dave, you seem to have some expectation that you should be given the > answer. That is not how help is given in this forum. You are > expected to be doing the needed to work before being helped further. > You have been referred to the tutorial multiple times. Please read > it! Indentation is so fundamental to structuring Python code that it > is clear that you need grounding in Python fundamentals. Otherwise > you are essentially Easter-egging through a code sample that you have > no true understanding of. > > If you must continue to Easter-egg Python instead of reading the > tutorial (or something equivalent) then check the section of the > tutorial on files. You will find examples of the use of "with" there. > > > -- > boB Bob, You seem to have the expectation that you know more about coding than me and that you can insult me without me retaliating. If I were you, I would leave this forum and never respond to another person question again, if you think that you can rudely ransack your way through what is supposed to be a helpful tool. -Dave From mirkok.lists at googlemail.com Sat Sep 21 15:03:17 2019 From: mirkok.lists at googlemail.com (Mirko) Date: Sat, 21 Sep 2019 21:03:17 +0200 Subject: Python shows error on line 15 that i cant fix In-Reply-To: <45a92ec1-f0f5-4298-b357-a015797680e0@googlegroups.com> References: <33f981d1-3907-4a8b-80dd-a89e40eb4756@googlegroups.com> <45a92ec1-f0f5-4298-b357-a015797680e0@googlegroups.com> Message-ID: <5D8673F5.2000306@googlemail.com> Am 21.09.2019 um 19:57 schrieb Dave Martin: > Can you provide an example of how to use the suite feature. Thank you. > There is no suite feature, Terry just tried to explain indented blocks to you in simple words. Really, indented blocks are one of the most basic aspects of Python. You *need* to read the tutorial as it has been suggested three times now. Anyway, given the following code: if name == "Dave": print("Hello Dave") print("How are you?) Programming languages in general cannot exactly understand what that code means. It could mean: "Say 'Hello Dave' and then 'How are you?" if the name is Dave. But it could also mean: "Say 'Hello Dave' if the name is Dave and then say "How are you?" what ever the name is. So, we need to tell Python which command should be executed if the name is Dave and which not. Some languages solves this with block markers: if name == "Dave" then print("Hello Dave") print("How are you?) endif Or for the other meaning: if name == "Dave" then print("Hello Dave") endif print("How are you?) Python uses indented blocks to make clear which commands belong together. Indentations are runs of whitespaces (of equal length) at the beginning of the line: if name == "Dave": print("Hello Dave") print("How are you?") Or for the other meaning: if name == "Dave": print("Hello Dave") print("How ar you"?) For your code that means, that you need to indent the lines that belong to the 'with' block This is wrong: with fits.open(fits_filename) as data: df=pd.DataFrame(data[1].data) ... What you need is this: with fits.open(fits_filename) as data: df=pd.DataFrame(data[1].data) ... ^-- See these spaces in front of the commands. That are indentations and all consecutive indented lines are an indented block. Please read the tutorial at https://docs.python.org/3/tutorial/index.html (fourth time now ;-) ) From rosuav at gmail.com Sat Sep 21 15:05:22 2019 From: rosuav at gmail.com (Chris Angelico) Date: Sun, 22 Sep 2019 05:05:22 +1000 Subject: Python shows error on line 15 that i cant fix In-Reply-To: References: <33f981d1-3907-4a8b-80dd-a89e40eb4756@googlegroups.com> <45a92ec1-f0f5-4298-b357-a015797680e0@googlegroups.com> Message-ID: On Sun, Sep 22, 2019 at 4:56 AM Dave Martin wrote: > > On Saturday, September 21, 2019 at 2:46:15 PM UTC-4, boB Stepp wrote: > > On Sat, Sep 21, 2019 at 1:01 PM Dave Martin wrote: > > > > > > On Saturday, September 21, 2019 at 1:33:12 PM UTC-4, Terry Reedy wrote: > > > > On 9/21/2019 11:53 AM, Dave Martin wrote: > > [...] > > > > > #get the combined data and load the fits files > > > > > > > > > > fits_filename="Gaia_DR2/gaiadr2_100pc.fits" > > > > > df=pd.DataFrame() > > > > > with fits.open(fits_filename) as data: > > > > > df=pd.DataFrame(data[1].data) > > > > > > > > A 'with' statement is a compound statement. It must be followed by a > > > > 'suite', which usually consists of an indented block of statements. > > > > This is line 17 from the first non-blank line you posted. > > [...] > > > > > Can you provide an example of how to use the suite feature. Thank you. > > > > Dave, you seem to have some expectation that you should be given the > > answer. That is not how help is given in this forum. You are > > expected to be doing the needed to work before being helped further. > > You have been referred to the tutorial multiple times. Please read > > it! Indentation is so fundamental to structuring Python code that it > > is clear that you need grounding in Python fundamentals. Otherwise > > you are essentially Easter-egging through a code sample that you have > > no true understanding of. > > > > If you must continue to Easter-egg Python instead of reading the > > tutorial (or something equivalent) then check the section of the > > tutorial on files. You will find examples of the use of "with" there. > > > You seem to have the expectation that you know more about coding than me and that you can insult me without me retaliating. If I were you, I would leave this forum and never respond to another person question again, if you think that you can rudely ransack your way through what is supposed to be a helpful tool. > When you ask for help on a public forum, it's usually best to start by reading the tutorials yourself. http://www.catb.org/~esr/faqs/smart-questions.html boB is absolutely correct here: you need to learn the basics before further questions will be truly productive. We are not here to read aloud from the tutorial to you. Once you have a basic understanding of Python's structure, you will be far better able to ask questions and understand the answers. ChrisA From PythonList at DancesWithMice.info Sat Sep 21 15:55:10 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Sun, 22 Sep 2019 07:55:10 +1200 Subject: python is bugging In-Reply-To: References: <02148621-4c27-4751-8035-d6d0e85ecc00@googlegroups.com> <8b57e52e-3e00-4e77-9196-5e8b01e52f2c@googlegroups.com> Message-ID: <06bbd5ad-c5ef-717e-b6fa-5f003c1c4c24@DancesWithMice.info> On 22/09/19 5:08 AM, Dave Martin wrote: > On Saturday, September 21, 2019 at 12:44:27 PM UTC-4, Brian Oney wrote: >> On Sat, 2019-09-21 at 08:57 -0700, Dave Martin wrote: >>> On Saturday, September 21, 2019 at 11:55:29 AM UTC-4, Dave Martin >>> wrote: >>>> what does expected an indented block >>> >>> *what does an indented block mean? >> >> It means that the line of code belongs to a certain body as defined >> above its position. >> >> Please follow the tutorial. ... > this is my code the error is on line 15 Did you follow the tutorial? Is it easy (for *volunteer* helpers) to work-out which is line 15? Back to the original question: Is the (loop) code actually indented? -- Regards =dn From sjeik_appie at hotmail.com Sat Sep 21 16:13:48 2019 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Sat, 21 Sep 2019 20:13:48 +0000 Subject: Document Entire Apps In-Reply-To: <5005627D-460D-409E-9978-D68448AE3265@gmail.com> Message-ID: On 15 Sep 2019 07:00, Sinardy Gmail wrote: I understand that we can use pydoc to document procedures how about the relationship between packages and dependencies ? ==? Check out snakefood to generate dependency graphs: http://furius.ca/snakefood/. Also, did you discover sphinx already? From piet-l at vanoostrum.org Sat Sep 21 17:43:22 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Sat, 21 Sep 2019 23:43:22 +0200 Subject: python is bugging References: <02148621-4c27-4751-8035-d6d0e85ecc00@googlegroups.com> <8b57e52e-3e00-4e77-9196-5e8b01e52f2c@googlegroups.com> Message-ID: Dave Martin writes: > On Saturday, September 21, 2019 at 11:55:29 AM UTC-4, Dave Martin wrote: >> what does expected an indented block > > *what does an indented block mean? >From the tutorial, https://docs.python.org/3/tutorial/ 3.2. First Steps Towards Programming: The body of the loop is indented: indentation is Python?s way of grouping statements. At the interactive prompt, you have to type a tab or space(s) for each indented line. In practice you will prepare more complicated input for Python with a text editor; all decent text editors have an auto-indent facility. When a compound statement is entered interactively, it must be followed by a blank line to indicate completion (since the parser cannot guess when you have typed the last line). Note that each line within a basic block must be indented by the same amount. -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From piet-l at vanoostrum.org Sat Sep 21 17:47:53 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Sat, 21 Sep 2019 23:47:53 +0200 Subject: python is bugging References: <02148621-4c27-4751-8035-d6d0e85ecc00@googlegroups.com> <8b57e52e-3e00-4e77-9196-5e8b01e52f2c@googlegroups.com> Message-ID: Dave Martin writes: > On Saturday, September 21, 2019 at 12:44:27 PM UTC-4, Brian Oney wrote: >> On Sat, 2019-09-21 at 08:57 -0700, Dave Martin wrote: >> > On Saturday, September 21, 2019 at 11:55:29 AM UTC-4, Dave Martin >> > wrote: >> > > what does expected an indented block >> > >> > *what does an indented block mean? >> >> It means that the line of code belongs to a certain body as defined >> above its position. >> >> Please follow the tutorial. >> >> https://docs.python.org/3/tutorial/index.html > > df.to_csv(r"faststars.csv", index=None,header=True) > # starAbsMags=df['radial_velocity'] > > #GaiaPandasEscapeVelocityCode > > import pandas as pd > import numpy as np > from astropy.io import fits > import astropy > import matplotlib.pyplot as plt > > > #get the combined data and load the fits files > > fits_filename="Gaia_DR2/gaiadr2_100pc.fits" > df=pd.DataFrame() > with fits.open(fits_filename) as data: > df=pd.DataFrame(data[1].data) > df.columns=[c.lower() for c in df.columns] > print("Columns.") > print(df.columns.values) > print("n/n") > #print out some data meta info to see what we're working with > print("Number of stars:") > nstars=len(df) > print(nstars) > distances = (df['parallax']/1000) > starAbsMags =df['phot_g_mean_mag'] > df = df[(df.parallax_over_error > 10 ) ] > print("Left after filter: " +str(len(df)/float(nstars)*100)+" %") > df.hist(column='radial_velocity') > #fastdf=df[(df.radial_velocity > 200) | (df.radial_velocity < -200)] > fastdf=df[(df.radial_velocity > 550)|(df.radial_velocity<-550)] > print(len(fastdf)) > #print(fastdf)# starTemps=df['astrometric_weight_al'] > # df.plot.scatter("radial_velocity", "astrometric_weight_al", s=1, > c="radial_velocity", colormap="plasma") > # #df=df[(df.radial_velocity>=-550)] > # #plt.axis([0,400,-800,-550]) > # #plt.axis([0,400,550,800]) > # plt.xlabel('weight(Au)') > # plt.ylabel('Speed') > # plt.title('Gaia Speed vs Weight') > > this is my code the error is on line 15 1) What is line 15? 2) Always copy/paste the complete error message with your question. 3) Your with body is not indented: with fits.open(fits_filename) as data: df=pd.DataFrame(data[1].data) df.columns=[c.lower() for c in df.columns] print("Columns.") print(df.columns.values) But how should WE know how many lines belong to the body of the with statements? You should know that and indicate that with the indentation as described in the tutorial. And then there's also a strange line: c="radial_velocity", colormap="plasma") Probably meant to be a continuation of the previous, commented line, but as written it isn't. -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From markos at c2o.pro.br Sat Sep 21 19:42:31 2019 From: markos at c2o.pro.br (Markos) Date: Sat, 21 Sep 2019 20:42:31 -0300 Subject: Most efficient way to replace "," with "." in a array and/or dataframe Message-ID: Hi, I have a table.csv file with the following structure: , Polyarene conc ,, mg L-1 ,,,,,,, Spectrum, Py, Ace, Anth, 1, "0,456", "0,120", "0,168" 2, "0,456", "0,040", "0,280" 3, "0,152", "0,200", "0,280" I open as dataframe with the command: data = pd.read_csv ('table.csv', sep = ',', skiprows = 1) and the variable "data" has the structure: Spectrum,? Py,? Ace, Anth, 0? 1???????? 0,456? 0,120? 0,168 1? 2???????? 0,456 0,040 0,280 2? 3???????? 0,152 0,200 0,280 I copy the numeric fields to an array with the command: data_array = data.values [:, 1:] And the data_array variable gets the fields in string format: [['0,456' '0,120' '0,168'] ['0,456' '0,040' '0,280'] ['0,152' '0,200' '0,280']] The only way I found to change comma "," to dot "." was using the method replace(): for i, line in enumerate (data_array): data_array [i] = ([float (element.replace (',', '.')) for element in data_array [i]]) But I'm wondering if there is another, more "efficient" way to make this change without having to "iterate" all elements of the array with a loop "for". Also I'm also wondering if there would be any benefit of making this modification in dataframe before extracting the numeric fields to the array. Please, any comments or tip? Thanks you, Markos From hatanduc at gmail.com Sat Sep 21 21:08:46 2019 From: hatanduc at gmail.com (Duc T. Ha) Date: Sat, 21 Sep 2019 18:08:46 -0700 (PDT) Subject: Book for Long Short-Term Memory for Sequential Data Message-ID: <95c99244-5814-4fa3-bdd0-c78b9acc6458@googlegroups.com> Please, help me the title of a book about Deep Learning with the Recurrent Neural Network network structure using Long Short-term Memory for Sequential Data (time-series data). The R or Python language is OK. I need a book like hand-on because I do not work in information technology. Thank you so much for your help. From python at mrabarnett.plus.com Sat Sep 21 21:50:54 2019 From: python at mrabarnett.plus.com (MRAB) Date: Sun, 22 Sep 2019 02:50:54 +0100 Subject: Most efficient way to replace "," with "." in a array and/or dataframe In-Reply-To: References: Message-ID: On 2019-09-22 00:42, Markos wrote: > Hi, > > I have a table.csv file with the following structure: > > , Polyarene conc ,, mg L-1 ,,,,,,, > Spectrum, Py, Ace, Anth, > 1, "0,456", "0,120", "0,168" > 2, "0,456", "0,040", "0,280" > 3, "0,152", "0,200", "0,280" > > I open as dataframe with the command: > > data = pd.read_csv ('table.csv', sep = ',', skiprows = 1) > > and the variable "data" has the structure: > > Spectrum,? Py,? Ace, Anth, > 0? 1???????? 0,456? 0,120? 0,168 > 1? 2???????? 0,456 0,040 0,280 > 2? 3???????? 0,152 0,200 0,280 > > I copy the numeric fields to an array with the command: > > data_array = data.values [:, 1:] > > And the data_array variable gets the fields in string format: > > [['0,456' '0,120' '0,168'] > ['0,456' '0,040' '0,280'] > ['0,152' '0,200' '0,280']] > > The only way I found to change comma "," to dot "." was using the method > replace(): > > for i, line in enumerate (data_array): > data_array [i] = ([float (element.replace (',', '.')) for element in > data_array [i]]) > > But I'm wondering if there is another, more "efficient" way to make this > change without having to "iterate" all elements of the array with a loop > "for". > > Also I'm also wondering if there would be any benefit of making this > modification in dataframe before extracting the numeric fields to the array. > > Please, any comments or tip? > I'd suggest doing all of the replacements in the CSV file first, something like this: import re with open('table.csv') as file: csv_data = file.read() # Convert the decimal points and also make them look numeric. csv_data = re.sub(r'"(-?\d+),(\d+)"', r'\1.\2', csv_data) with open('fixed_table.csv', 'w') as file: file.write(csv_data) From torriem at gmail.com Sat Sep 21 21:59:47 2019 From: torriem at gmail.com (Michael Torrie) Date: Sat, 21 Sep 2019 19:59:47 -0600 Subject: Python shows error on line 15 that i cant fix In-Reply-To: References: <33f981d1-3907-4a8b-80dd-a89e40eb4756@googlegroups.com> <45a92ec1-f0f5-4298-b357-a015797680e0@googlegroups.com> Message-ID: <1183aa0f-eb01-4689-47c7-af5f9dd725e0@gmail.com> On 9/21/19 12:51 PM, Dave Martin wrote: > You seem to have the expectation that you know more about coding than > me and that you can insult me without me retaliating. If I were you, > I would leave this forum and never respond to another person question > again, if you think that you can rudely ransack your way through what > is supposed to be a helpful tool. Not so. He was not rude nor was he insulting. In fact he was downright patient. You've been asked several times to read the tutorial (or relevant parts of it) as that teaches you some things you have to know in order to use Python. I'm sorry that rather than do that you chose to react poorly to the good advice you were given. From cs at cskk.id.au Sat Sep 21 22:27:18 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 22 Sep 2019 12:27:18 +1000 Subject: [Tutor] Most efficient way to replace ", " with "." in a array and/or dataframe In-Reply-To: References: Message-ID: <20190922022718.GA96894@cskk.homeip.net> On 21Sep2019 20:42, Markos wrote: >I have a table.csv file with the following structure: > >, Polyarene conc ,, mg L-1 ,,,,,,, >Spectrum, Py, Ace, Anth, >1, "0,456", "0,120", "0,168" >2, "0,456", "0,040", "0,280" >3, "0,152", "0,200", "0,280" > >I open as dataframe with the command: >data = pd.read_csv ('table.csv', sep = ',', skiprows = 1) [...] >And the data_array variable gets the fields in string format: >[['0,456' '0,120' '0,168'] [...] Please see the documentation for the read_csv function here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html?highlight=read_csv#pandas.read_csv In particular, because you have values formatted in the European style with "," for the decimal marker (and possibly "." for the thousands marker), you want to set the "decimal=" parameter of read-csv to ",". This is better than trying to mangle the data yourself, better to just correctly specify the dialect (i.e. set decimal= in your call). Cheers, Cameron Simpson From robertvstepp at gmail.com Sun Sep 22 01:24:11 2019 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 22 Sep 2019 00:24:11 -0500 Subject: Python shows error on line 15 that i cant fix In-Reply-To: <1183aa0f-eb01-4689-47c7-af5f9dd725e0@gmail.com> References: <33f981d1-3907-4a8b-80dd-a89e40eb4756@googlegroups.com> <45a92ec1-f0f5-4298-b357-a015797680e0@googlegroups.com> <1183aa0f-eb01-4689-47c7-af5f9dd725e0@gmail.com> Message-ID: On Sat, Sep 21, 2019 at 9:01 PM Michael Torrie wrote: > > On 9/21/19 12:51 PM, Dave Martin wrote: > > You seem to have the expectation that you know more about coding than > > me and that you can insult me without me retaliating. If I were you, > > I would leave this forum and never respond to another person question > > again, if you think that you can rudely ransack your way through what > > is supposed to be a helpful tool. > > Not so. He was not rude nor was he insulting. In fact he was downright > patient. You've been asked several times to read the tutorial (or > relevant parts of it) as that teaches you some things you have to know > in order to use Python. I'm sorry that rather than do that you chose to > react poorly to the good advice you were given. Thank you very much Michael and Chris! That was very kind of you. -- boB From sjeik_appie at hotmail.com Sun Sep 22 03:39:48 2019 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Sun, 22 Sep 2019 07:39:48 +0000 Subject: [Tutor] Most efficient way to replace ", " with "." in a array and/or dataframe In-Reply-To: <20190922022718.GA96894@cskk.homeip.net> Message-ID: On 22 Sep 2019 04:27, Cameron Simpson wrote: On 21Sep2019 20:42, Markos wrote: >I have a table.csv file with the following structure: > >, Polyarene conc ,, mg L-1 ,,,,,,, >Spectrum, Py, Ace, Anth, >1, "0,456", "0,120", "0,168" >2, "0,456", "0,040", "0,280" >3, "0,152", "0,200", "0,280" > >I open as dataframe with the command: >data = pd.read_csv ('table.csv', sep = ',', skiprows = 1) [...] >And the data_array variable gets the fields in string format: >[['0,456' '0,120' '0,168'] [...] >Please see the documentation for the >read_csv function here: > https://pandas.pydata.org/pandas >docs/stable/reference/api/pandas.read_cs> v.html?highlight=read_csv#pandas.read_csv Do you think it's a deliberate design choice that decimal and thousands where used here as params, and not a 'locale' param? It seems nice to be able to specify e.g. locale='dutch' and then all the right lc_numeric, lc_monetary, lc_time where used. Or even locale='nl_NL.1252' and you also wouldn't need 'encoding' as a separate param. Or might that be bad on windows where there's no locale-gen? Just wondering... Albert-Jan From cs at cskk.id.au Sun Sep 22 04:20:52 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Sun, 22 Sep 2019 18:20:52 +1000 Subject: [Tutor] Most efficient way to replace ", " with "." in a array and/or dataframe In-Reply-To: References: Message-ID: <20190922082052.GA77760@cskk.homeip.net> On 22Sep2019 07:39, Albert-Jan Roskam wrote: >On 22 Sep 2019 04:27, Cameron Simpson wrote: >On 21Sep2019 20:42, Markos wrote: >>I have a table.csv file with the following structure: >> >>, Polyarene conc ,, mg L-1 ,,,,,,, >>Spectrum, Py, Ace, Anth, >>1, "0,456", "0,120", "0,168" >>2, "0,456", "0,040", "0,280" >>3, "0,152", "0,200", "0,280" >> >>I open as dataframe with the command: >>data = pd.read_csv ('table.csv', sep = ',', skiprows = 1) >[...] >>And the data_array variable gets the fields in string format: >>[['0,456' '0,120' '0,168'] >[...] > >>Please see the documentation for the >read_csv function here: > >> https://pandas.pydata.org/pandas > >>docs/stable/reference/api/pandas.read_cs> v.html?highlight=read_csv#pandas.read_csv > >Do you think it's a deliberate design choice that decimal and thousands >where used here as params, and not a 'locale' param? It seems nice to >be able to specify e.g. locale='dutch' and then all the right >lc_numeric, lc_monetary, lc_time where used. Or even >locale='nl_NL.1252' and you also wouldn't need 'encoding' as a separate >param. Or might that be bad on windows where there's no locale-gen? >Just wondering... Locales are tricky; I don't know enough. A locale parameter might be convenient for some things, but such things are table driven. From an arbitrary Linux box nearby: % locale -a C C.UTF-8 POSIX en_AU.utf8 No "dutch" or similar there. I doubt pandas would ship with such a thing. And the OP probably doesn't know the originating locale anyway. Nor do _we_ know that those values themselves were driven from some well known locale table. The advantage of specifical decimal= and thousands= parameters is that they do exactly what they say, rather than looking up a locale and hoping for a specific side effect. So the specific parameters offer better control. The thousands= itself is a little parachial (for example, in India a factor of 100 is a common division point[1]), but it may merely be used to strip this character from the left portion of the number. [1] https://en.wikipedia.org/wiki/Indian_numbering_system So while I am not a pandas person, I would expect that decimal= and thousands= are useful parameters for specific lexical situations (like the OP's CSV data) and work regardless of any locale knowledge. Cheers, Cameron Simpson From eryksun at gmail.com Sun Sep 22 05:40:57 2019 From: eryksun at gmail.com (Eryk Sun) Date: Sun, 22 Sep 2019 04:40:57 -0500 Subject: [Tutor] Most efficient way to replace ", " with "." in a array and/or dataframe In-Reply-To: References: <20190922022718.GA96894@cskk.homeip.net> Message-ID: On 9/22/19, Albert-Jan Roskam wrote: > > Do you think it's a deliberate design choice that decimal and thousands > where used here as params, and not a 'locale' param? It seems nice to be > able to specify e.g. locale='dutch' and then all the right lc_numeric, > lc_monetary, lc_time where used. Or even locale='nl_NL.1252' and you also > wouldn't need 'encoding' as a separate param. Or might that be bad on > windows where there's no locale-gen? Just wondering... FYI, while Windows is distributed with many locales and also supports custom locales (not something I've had to work with), at least based on standard locale data, "nl_NL.1252" is not a valid locale for use with C setlocale in Windows. Classically, the C runtime in Windows supports "C" (but not "POSIX") and locales based on a language name or its non-standard three-letter abbreviation. A locale can also include a country/region name (full or three-letter abbreviation), plus an optional codepage. If the latter is omitted, it defaults to the ANSI codepage of the language, or of the system locale if the language has no ANSI codepage . Examples: >>> locale.setlocale(0, 'dutch') 'Dutch_Netherlands.1252' >>> locale.setlocale(0, 'nld') 'Dutch_Netherlands.1252' >>> locale.setlocale(0, 'nld_NLD.850') 'Dutch_Netherlands.850' There are also a few compatibility locales such as "american" and "canadian": >>> locale.setlocale(0, 'american') 'English_United States.1252' >>> locale.setlocale(0, 'canadian') 'English_Canada.1252' Classically, the Windows API represents locales not as language/region strings but as numeric locale identifiers (LCIDs). However, back in 2006, Windows Vista introduced locale names, plus a new set of functions that use locale names instead of LCIDs (e.g. GetLocaleInfoEx). Locale names are based on BCP-47 language tags, which include at least an ISO 639 language code. They can also include an optional ISO 15924 script code (e.g. "Latn" or "Cyrl") and an optional ISO 3166-1 region code. Strictly speaking, the codes in a BCP-47 language tag are delimited only by hyphens, but newer versions of Windows in most cases also allow underscore. The Universal CRT (used by Python 3.5+) supports Vista locale names. Recently it also supports using underscore instead of hyphen, plus an optional ".utf8" or ".utf-8" encoding. Only UTF-8 can be specified for a BCP-47 locale. If the encoding is not specified, BCP-47 locales use the language's ANSI codepage, or UTF-8 if the language has no ANSI codepage (e.g. "hi_IN"). Examples: >>> locale.setlocale(0, 'nl_NL') 'nl_NL' >>> locale.setlocale(0, 'nl_NL.utf8') 'nl_NL.utf8' Older versions of the Universal CRT do not support UTF-8 or underscore in BCP-47 locale names, so make sure a system is updated if you need these features. From phd at phdru.name Sun Sep 22 09:13:11 2019 From: phd at phdru.name (Oleg Broytman) Date: Sun, 22 Sep 2019 15:13:11 +0200 Subject: SQLObject 3.7.3 Message-ID: <20190922131311.d3vyoxxmtjalagg4@phdru.name> Hello! I'm pleased to announce version 3.7.3, a bugfix release of branch 3.7 of SQLObject. What's new in SQLObject ======================= Bug fixes --------- * Avoid excessive parentheses around ``ALL/ANY/SOME()``. Tests ----- * Add tests for cascade deletion. * Add tests for ``sqlbuilder.ALL/ANY/SOME()``. * Fix calls to ``pytest.mark.skipif`` - make conditions bool instead of str. * Fix module-level calls to ``pytest.mark.skip`` - add reasons. * Fix escape sequences ``'\%'`` -> ``'\\%'``. CI -- * Reduce the number of virtual machines/containers: one OS, one DB, one python version, many drivers per VM. * Fix sqlite test under Python 3.7+ at AppVeyor. Contributors for this release are For a more complete list, please see the news: http://sqlobject.org/News.html What is SQLObject ================= SQLObject is an object-relational mapper. Your database tables are described as classes, and rows are instances of those classes. SQLObject is meant to be easy to use and quick to get started with. SQLObject supports a number of backends: MySQL, PostgreSQL, SQLite, Firebird, Sybase, MSSQL and MaxDB (also known as SAPDB). Python 2.7 or 3.4+ is required. Where is SQLObject ================== Site: http://sqlobject.org Development: http://sqlobject.org/devel/ Mailing list: https://lists.sourceforge.net/mailman/listinfo/sqlobject-discuss Download: https://pypi.org/project/SQLObject/3.7.3 News and changes: http://sqlobject.org/News.html StackOverflow: https://stackoverflow.com/questions/tagged/sqlobject Example ======= Create a simple class that wraps a table:: >>> from sqlobject import * >>> >>> sqlhub.processConnection = connectionForURI('sqlite:/:memory:') >>> >>> class Person(SQLObject): ... fname = StringCol() ... mi = StringCol(length=1, default=None) ... lname = StringCol() ... >>> Person.createTable() Use the object:: >>> p = Person(fname="John", lname="Doe") >>> p >>> p.fname 'John' >>> p.mi = 'Q' >>> p2 = Person.get(1) >>> p2 >>> p is p2 True Queries:: >>> p3 = Person.selectBy(lname="Doe")[0] >>> p3 >>> pc = Person.select(Person.q.lname=="Doe").count() >>> pc 1 Oleg. -- Oleg Broytman https://phdru.name/ phd at phdru.name Programmers don't die, they just GOSUB without RETURN. From hongyi.zhao at gmail.com Sun Sep 22 10:13:34 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Sun, 22 Sep 2019 14:13:34 +0000 (UTC) Subject: Obtain all the content from https://free-ss.site/ by pycurl/requests. Message-ID: Hi, I try to obtain the content from https://free-ss.site/ by pycurl/requests. But it seems that the above tools cannot get all of the content as the stuff obtained by the firefox. Is is possible to get all of the content just as the results via firefox for this website by using pycurl/requests? Regards From phd at phdru.name Sun Sep 22 10:36:59 2019 From: phd at phdru.name (Oleg Broytman) Date: Sun, 22 Sep 2019 16:36:59 +0200 Subject: Cheetah 3.2.4 Message-ID: <20190922143659.2ynjir4jskiyaw2k@phdru.name> Hello! I'm pleased to announce version 3.2.4, a bugfix release of branch 3.2 of CheetahTemplate3. What's new in CheetahTemplate3 ============================== Minor features: - Import from ``collections`` for Python 2, from ``collections.abc`` for Python 3. Bug fixes: - Fixed infinite recursion in ``ImportManager`` on importing module ``_bootlocale`` inside ``open()``. What is CheetahTemplate3 ======================== Cheetah3 is a free and open source template engine. It's a fork of the original CheetahTemplate library. Python 2.7 or 3.4+ is required. Where is CheetahTemplate3 ========================= Site: https://cheetahtemplate.org/ Development: https://github.com/CheetahTemplate3 Download: https://pypi.org/project/Cheetah3/3.2.4 News and changes: https://cheetahtemplate.org/news.html StackOverflow: https://stackoverflow.com/questions/tagged/cheetah Example ======= Below is a simple example of some Cheetah code, as you can see it's practically Python. You can import, inherit and define methods just like in a regular Python module, since that's what your Cheetah templates are compiled to :) :: #from Cheetah.Template import Template #extends Template #set $people = [{'name' : 'Tom', 'mood' : 'Happy'}, {'name' : 'Dick', 'mood' : 'Sad'}, {'name' : 'Harry', 'mood' : 'Hairy'}] How are you feeling?
    #for $person in $people
  • $person['name'] is $person['mood']
  • #end for
Oleg. -- Oleg Broytman https://phdru.name/ phd at phdru.name Programmers don't die, they just GOSUB without RETURN. From piet-l at vanoostrum.org Sun Sep 22 12:10:23 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Sun, 22 Sep 2019 18:10:23 +0200 Subject: Most efficient way to replace ", " with "." in a array and/or dataframe References: Message-ID: Markos writes: > Hi, > > I have a table.csv file with the following structure: > > , Polyarene conc ,, mg L-1 ,,,,,,, > Spectrum, Py, Ace, Anth, > 1, "0,456", "0,120", "0,168" > 2, "0,456", "0,040", "0,280" > 3, "0,152", "0,200", "0,280" > > I open as dataframe with the command: > > data = pd.read_csv ('table.csv', sep = ',', skiprows = 1) > [snip] > Also I'm also wondering if there would be any benefit of making this > modification in dataframe before extracting the numeric fields to the > array. > > Please, any comments or tip? data = pd.read_csv ('table.csv', sep = ',', skiprows = 1, decimal=b',', skipinitialspace=True) -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From markos at c2o.pro.br Sun Sep 22 19:05:38 2019 From: markos at c2o.pro.br (Markos) Date: Sun, 22 Sep 2019 20:05:38 -0300 Subject: Most efficient way to replace ", " with "." in a array and/or dataframe In-Reply-To: References: Message-ID: <85835c7d-44be-8c91-3c67-92ecb85b6342@c2o.pro.br> Em 22-09-2019 13:10, Piet van Oostrum escreveu: > Markos writes: > >> Hi, >> >> I have a table.csv file with the following structure: >> >> , Polyarene conc ,, mg L-1 ,,,,,,, >> Spectrum, Py, Ace, Anth, >> 1, "0,456", "0,120", "0,168" >> 2, "0,456", "0,040", "0,280" >> 3, "0,152", "0,200", "0,280" >> >> I open as dataframe with the command: >> >> data = pd.read_csv ('table.csv', sep = ',', skiprows = 1) >> > [snip] > >> Also I'm also wondering if there would be any benefit of making this >> modification in dataframe before extracting the numeric fields to the >> array. >> >> Please, any comments or tip? > data = pd.read_csv ('table.csv', sep = ',', skiprows = 1, decimal=b',', skipinitialspace=True) > Thank you for the tip. I didn't realize that I could avoid formatting problems in the dataframe or array simply by using the read_csv command with the correct parameters (sep and decimal). I searched for information about the meaning of the letter "b" in the parameter decimal=b','? but didn't find. I found that it also works without the letter b. Best Regards, Markos From Richard at Damon-Family.org Sun Sep 22 19:40:34 2019 From: Richard at Damon-Family.org (Richard Damon) Date: Sun, 22 Sep 2019 19:40:34 -0400 Subject: Most efficient way to replace ", " with "." in a array and/or dataframe In-Reply-To: <85835c7d-44be-8c91-3c67-92ecb85b6342@c2o.pro.br> References: <85835c7d-44be-8c91-3c67-92ecb85b6342@c2o.pro.br> Message-ID: On 9/22/19 7:05 PM, Markos wrote: > > Em 22-09-2019 13:10, Piet van Oostrum escreveu: >> Markos writes: >> >>> Hi, >>> >>> I have a table.csv file with the following structure: >>> >>> , Polyarene conc ,, mg L-1 ,,,,,,, >>> Spectrum, Py, Ace, Anth, >>> 1, "0,456", "0,120", "0,168" >>> 2, "0,456", "0,040", "0,280" >>> 3, "0,152", "0,200", "0,280" >>> >>> I open as dataframe with the command: >>> >>> data = pd.read_csv ('table.csv', sep = ',', skiprows = 1) >>> >> [snip] >> >>> Also I'm also wondering if there would be any benefit of making this >>> modification in dataframe before extracting the numeric fields to the >>> array. >>> >>> Please, any comments or tip? >> data = pd.read_csv ('table.csv', sep = ',', skiprows = 1, >> decimal=b',', skipinitialspace=True) >> > Thank you for the tip. > > I didn't realize that I could avoid formatting problems in the > dataframe or array simply by using the read_csv command with the > correct parameters (sep and decimal). > > I searched for information about the meaning of the letter "b" in the > parameter decimal=b','? but didn't find. > > I found that it also works without the letter b. > > Best Regards, > Markos The b indicates that the string is a 'bytes' string vs a text string. This isn't so important to differentiate for ASCII characters, but can make a difference if you have extended characters and the file might not be in Unicode. -- Richard Damon From markos at c2o.pro.br Sun Sep 22 19:16:54 2019 From: markos at c2o.pro.br (Markos) Date: Sun, 22 Sep 2019 20:16:54 -0300 Subject: Most efficient way to replace ", " with "." in a array and/or dataframe In-Reply-To: References: Message-ID: <7412adf8-413c-ccfd-9285-95f52e0f1aca@c2o.pro.br> Em 22-09-2019 13:10, Piet van Oostrum escreveu: > Markos writes: > >> Hi, >> >> I have a table.csv file with the following structure: >> >> , Polyarene conc ,, mg L-1 ,,,,,,, >> Spectrum, Py, Ace, Anth, >> 1, "0,456", "0,120", "0,168" >> 2, "0,456", "0,040", "0,280" >> 3, "0,152", "0,200", "0,280" >> >> I open as dataframe with the command: >> >> data = pd.read_csv ('table.csv', sep = ',', skiprows = 1) >> > [snip] > >> Also I'm also wondering if there would be any benefit of making this >> modification in dataframe before extracting the numeric fields to the >> array. >> >> Please, any comments or tip? > data = pd.read_csv ('table.csv', sep = ',', skiprows = 1, decimal=b',', skipinitialspace=True) > Thank you very much for all the contributions. I didn't realize that I could avoid formatting problems in the dataframe or array simply by using the read_csv command with the correct parameters (sep and decimal). I searched for information about the meaning of the letter b in the parameter but did not find it. I found that it also works without the letter b. Best Regards, Markos From shane at pngbd.com Sun Sep 22 20:50:24 2019 From: shane at pngbd.com (Shane Thomas) Date: Sun, 22 Sep 2019 17:50:24 -0700 (PDT) Subject: Remote Work Wanted - Junior Python Developer Message-ID: <06113b16-a59c-418b-a2f7-e87f5203ecb3@googlegroups.com> Hi, I would like to start working remotely programming in Python. I have an IT Degree and English is my first Language. I currently live in New Zealand, thus the remote work is the only option. I have 15+ years experience as a Delphi Developer, mainly working with business database applications. I have worked with Oracle, MySQL, SQL Server, Interbase, Firebird and many other database backends as well. I would like to join a team of developers that could help my crossover to Python. I think I can hit the ground running if pointed in the right direction and helped over any time consuming hurdles. As far as money goes, I am open to reasonable offers. cheers Shane From rosuav at gmail.com Sun Sep 22 21:20:55 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 23 Sep 2019 11:20:55 +1000 Subject: Remote Work Wanted - Junior Python Developer In-Reply-To: <06113b16-a59c-418b-a2f7-e87f5203ecb3@googlegroups.com> References: <06113b16-a59c-418b-a2f7-e87f5203ecb3@googlegroups.com> Message-ID: On Mon, Sep 23, 2019 at 10:56 AM Shane Thomas wrote: > > Hi, > > I would like to start working remotely programming in Python. > > I have an IT Degree and English is my first Language. I currently live in New Zealand, thus the remote work is the only option. > > I have 15+ years experience as a Delphi Developer, mainly working with business database applications. I have worked with Oracle, MySQL, SQL Server, Interbase, Firebird and many other database backends as well. > > I would like to join a team of developers that could help my crossover to Python. > > I think I can hit the ground running if pointed in the right direction and helped over any time consuming hurdles. > > As far as money goes, I am open to reasonable offers. > Cool! This list/newsgroup isn't a job board, but there are a number of good places to go looking. I'd start here, although IME remote jobs are a fairly small proportion of the listings: https://www.python.org/jobs/ Another solid place to look is Stack Overflow Jobs: https://stackoverflow.com/jobs I've no idea how many of the linked sites are of value, but there's a page on the Python Wiki specifically for sites with job postings about Python: https://wiki.python.org/moin/PythonJobs Hopefully that'll help! ChrisA From piet-l at vanoostrum.org Mon Sep 23 03:43:02 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Mon, 23 Sep 2019 09:43:02 +0200 Subject: Most efficient way to replace ", " with "." in a array and/or dataframe References: <85835c7d-44be-8c91-3c67-92ecb85b6342@c2o.pro.br> Message-ID: Markos writes: [...] >>> Please, any comments or tip? >> data = pd.read_csv ('table.csv', sep = ',', skiprows = 1, decimal=b',', skipinitialspace=True) >> > Thank you for the tip. > > I didn't realize that I could avoid formatting problems in the dataframe > or array simply by using the read_csv command with the correct > parameters (sep and decimal). > > I searched for information about the meaning of the letter "b" in the > parameter decimal=b','? but didn't find. > > I found that it also works without the letter b. > I added the b because the default in the definition of read_csv is b'.', but you are right, it works with just ','. -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From cdoare.ext at orange.com Mon Sep 23 04:05:56 2019 From: cdoare.ext at orange.com (cdoare.ext at orange.com) Date: Mon, 23 Sep 2019 08:05:56 +0000 Subject: Unable to start Python with Windows 7 In-Reply-To: <8a3dbcde-16c3-4304-83cb-db2eec5164e8@googlegroups.com> References: <21843_1568878500_5D832FA4_21843_2_1_A2B41E5C09C6EB4D89E3D9622E69391E38A79C0B@OPEXCAUBMA1.corporate.adroot.infra.ftgroup> <8a3dbcde-16c3-4304-83cb-db2eec5164e8@googlegroups.com> Message-ID: <9230_1569225957_5D887CE5_9230_215_1_A2B41E5C09C6EB4D89E3D9622E69391E38A7C2C1@OPEXCAUBMA1.corporate.adroot.infra.ftgroup> Hi Eren, I have Win 64bit and run Python also in 64 bits. What is strange is that I can run Python, but only with a short program ! Best regards, Christian -----Message d'origine----- De?: Python-list [mailto:python-list-bounces+cdoare.ext=orange.com at python.org] De la part de Eko palypse Envoy??: jeudi 19 septembre 2019 18:06 ??: python-list at python.org Objet?: Re: Unable to start Python with Windows 7 Am Donnerstag, 19. September 2019 17:52:48 UTC+2 schrieb cdoa... at orange.com: > Hi, > I am no more able to start Python from Windows 7 environment. > I have the following message : > "The Application was unable to start correctly, (0xC0000142). Click OK to close the application" > > Do you have any idea where the problem is ? > > Bests regards, > A shot in the dark, you do have Win7 32bit and tried to start python 64bit Eren -- https://mail.python.org/mailman/listinfo/python-list _________________________________________________________________________________________________________________________ Ce message et ses pieces jointes peuvent contenir des informations confidentielles ou privilegiees et ne doivent donc pas etre diffuses, exploites ou copies sans autorisation. Si vous avez recu ce message par erreur, veuillez le signaler a l'expediteur et le detruire ainsi que les pieces jointes. Les messages electroniques etant susceptibles d'alteration, Orange decline toute responsabilite si ce message a ete altere, deforme ou falsifie. Merci. This message and its attachments may contain confidential or privileged information that may be protected by law; they should not be distributed, used or copied without authorisation. If you have received this email in error, please notify the sender and delete this message and its attachments. As emails may be altered, Orange is not liable for messages that have been modified, changed or falsified. Thank you. From ekopalypse at gmail.com Mon Sep 23 07:44:04 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Mon, 23 Sep 2019 04:44:04 -0700 (PDT) Subject: Unable to start Python with Windows 7 In-Reply-To: References: <21843_1568878500_5D832FA4_21843_2_1_A2B41E5C09C6EB4D89E3D9622E69391E38A79C0B@OPEXCAUBMA1.corporate.adroot.infra.ftgroup> <8a3dbcde-16c3-4304-83cb-db2eec5164e8@googlegroups.com> <9230_1569225957_5D887CE5_9230_215_1_A2B41E5C09C6EB4D89E3D9622E69391E38A7C2C1@OPEXCAUBMA1.corporate.adroot.infra.ftgroup> Message-ID: >What is strange is that I can run Python, but only with a short program ! Hi Crhistian, hard to tell, what might help is if you can copy/paste code which does not run (an example should be as small as possible). In addition, isn't there any output you get when the program doesn't run? A traceback etc... and how do you run those programs, by double clicking .py files via cmd like python myfile.py, via an editor ... Eren From cdoare.ext at orange.com Mon Sep 23 10:32:09 2019 From: cdoare.ext at orange.com (cdoare.ext at orange.com) Date: Mon, 23 Sep 2019 14:32:09 +0000 Subject: Unable to start Python with Windows 7 In-Reply-To: References: <21843_1568878500_5D832FA4_21843_2_1_A2B41E5C09C6EB4D89E3D9622E69391E38A79C0B@OPEXCAUBMA1.corporate.adroot.infra.ftgroup> <8a3dbcde-16c3-4304-83cb-db2eec5164e8@googlegroups.com> <9230_1569225957_5D887CE5_9230_215_1_A2B41E5C09C6EB4D89E3D9622E69391E38A7C2C1@OPEXCAUBMA1.corporate.adroot.infra.ftgroup> Message-ID: <19653_1569249130_5D88D76A_19653_83_1_A2B41E5C09C6EB4D89E3D9622E69391E38A7C37A@OPEXCAUBMA1.corporate.adroot.infra.ftgroup> Hi Eren, Thanks for your help. But I tried another way. I am now on Linux and it works fine ! I don't to want to lose any more time on windows ;-) Christian -----Message d'origine----- De?: Python-list [mailto:python-list-bounces+cdoare.ext=orange.com at python.org] De la part de Eko palypse Envoy??: lundi 23 septembre 2019 13:44 ??: python-list at python.org Objet?: Re: Unable to start Python with Windows 7 >What is strange is that I can run Python, but only with a short program ! Hi Crhistian, hard to tell, what might help is if you can copy/paste code which does not run (an example should be as small as possible). In addition, isn't there any output you get when the program doesn't run? A traceback etc... and how do you run those programs, by double clicking .py files via cmd like python myfile.py, via an editor ... Eren -- https://mail.python.org/mailman/listinfo/python-list _________________________________________________________________________________________________________________________ Ce message et ses pieces jointes peuvent contenir des informations confidentielles ou privilegiees et ne doivent donc pas etre diffuses, exploites ou copies sans autorisation. Si vous avez recu ce message par erreur, veuillez le signaler a l'expediteur et le detruire ainsi que les pieces jointes. Les messages electroniques etant susceptibles d'alteration, Orange decline toute responsabilite si ce message a ete altere, deforme ou falsifie. Merci. This message and its attachments may contain confidential or privileged information that may be protected by law; they should not be distributed, used or copied without authorisation. If you have received this email in error, please notify the sender and delete this message and its attachments. As emails may be altered, Orange is not liable for messages that have been modified, changed or falsified. Thank you. From jamesbtobin at gmail.com Mon Sep 23 03:02:33 2019 From: jamesbtobin at gmail.com (James Tobin) Date: Mon, 23 Sep 2019 08:02:33 +0100 Subject: JOB | Front End Architect (London, UK) Message-ID: Hello, I'm working with an employer that is looking to hire a permanent front end architect to join their London office. You should have experience with Javascript and frameworks such as (but not only) React. Consequently, I had hoped that some members of this mailing list may like to discuss further off-list using "JamesBTobin (at) Gmail (dot) Com". Kind regards, James From vijaykumar.ppsg at gmail.com Mon Sep 23 14:00:56 2019 From: vijaykumar.ppsg at gmail.com (Vijay Kumar Kamannavar) Date: Mon, 23 Sep 2019 23:30:56 +0530 Subject: How to get only valid python package index Message-ID: Hellom As per https://pypi.org/simple/ we have ~2,00,000 packages. i feel there are lot of packages found to be dummy/Experimental. Where can we get the properly maintained package list for python? If not, atleast please let me know what kind of strategy i shoud use to decide whether package is valid or not? Thanks, Vijay From p.f.moore at gmail.com Mon Sep 23 15:44:31 2019 From: p.f.moore at gmail.com (Paul Moore) Date: Mon, 23 Sep 2019 20:44:31 +0100 Subject: How to get only valid python package index In-Reply-To: References: Message-ID: On Mon, 23 Sep 2019 at 19:15, Vijay Kumar Kamannavar wrote: > > Hellom > > As per https://pypi.org/simple/ we have ~2,00,000 packages. i feel there > are lot of packages found to be dummy/Experimental. Where can we get the > properly maintained package list for python? There is no "properly maintained package list" in the sense that I suspect you mean it, i.e. a curated list where the maintainers guarantee a particular level of quality or support for the available packages. PyPI is an open index and anyone can register an account and upload packages, without restriction. > If not, atleast please let me know what kind of strategy i shoud use to > decide whether package is valid or not? The responsibility for reviewing and assessing the quality of packages lies with the user, so you'll need to assess each package for yourself, in much the same way that you would assess any other open source package - you can look at existing code, blog posts or articles to get a sense of what packages are considered good, or "best of breed", or you can assess the code and documentation against whatever standards you wish to apply. It shouldn't take long if you read some articles to get a sense of some of the more well-known packages (things like requests, numpy, pandas, matplotlib, django, ...) but what is best for you depends entirely on what you are trying to do with Python. Hope this helps, Paul From briannkorirbk at gmail.com Mon Sep 23 17:12:21 2019 From: briannkorirbk at gmail.com (Brian Korir) Date: Tue, 24 Sep 2019 00:12:21 +0300 Subject: python installation. Message-ID: I receive a 'not a valid Win32...' after a few minutes of installing python 3.7.4. I have reinstalled it several times with the same results. My laptop uses windows 7, 64-bit. What can I do to solve this? Thank you. From cs at cskk.id.au Mon Sep 23 22:36:17 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 24 Sep 2019 12:36:17 +1000 Subject: python installation. In-Reply-To: References: Message-ID: <20190924023617.GA61921@cskk.homeip.net> On 24Sep2019 00:12, Brian Korir wrote: >I receive a 'not a valid Win32...' after a few minutes of installing python >3.7.4. I have reinstalled it several times with the same results. My laptop >uses windows 7, 64-bit. What can I do to solve this? Thank you. Are you using the appropriate installer? In particular (guessing from the message) might you have fetched a 32 bit install instead of the x86-64 installer? Please check this page: https://www.python.org/downloads/windows/ for installers, and if you've definitely got the right installer please post details about what installer you chose. Disclaimer: not a Windows person. Cheers, Cameron Simpson From briannkorirbk at gmail.com Tue Sep 24 00:17:55 2019 From: briannkorirbk at gmail.com (Brian Korir) Date: Tue, 24 Sep 2019 07:17:55 +0300 Subject: python installation. In-Reply-To: <20190924023617.GA61921@cskk.homeip.net> References: <20190924023617.GA61921@cskk.homeip.net> Message-ID: I used Windows x86-64 executable installer the first two times and Windows x86-64 web-based installer. They both yielded the same result. On Tue, 24 Sep 2019 05:46 Cameron Simpson, wrote: > On 24Sep2019 00:12, Brian Korir wrote: > >I receive a 'not a valid Win32...' after a few minutes of installing > python > >3.7.4. I have reinstalled it several times with the same results. My > laptop > >uses windows 7, 64-bit. What can I do to solve this? Thank you. > > Are you using the appropriate installer? In particular (guessing from > the message) might you have fetched a 32 bit install instead of the > x86-64 installer? > > Please check this page: > > https://www.python.org/downloads/windows/ > > for installers, and if you've definitely got the right installer please > post details about what installer you chose. > > Disclaimer: not a Windows person. > > Cheers, > Cameron Simpson > From dieter at handshake.de Tue Sep 24 01:11:22 2019 From: dieter at handshake.de (dieter) Date: Tue, 24 Sep 2019 07:11:22 +0200 Subject: How to get only valid python package index References: Message-ID: <87wodynurp.fsf@handshake.de> Vijay Kumar Kamannavar writes: > As per https://pypi.org/simple/ we have ~2,00,000 packages. i feel there > are lot of packages found to be dummy/Experimental. Where can we get the > properly maintained package list for python? In addition of what Paul already wrote: "https://pypi.org/simple" is an interface for programs (such as "pip" or "buildout") not for human users. Use "pypi.org" directly for an interface for humans. "pypi.org" allows you to filter by "classifier". There are many kinds of "classifier"s, among them "Development Status". It allows you to restrict searches to "stable" releases (to exclude e.g. "experimental" versions). You can also take into account the version number. Often, an "a" (= "Alpha") or "b" (= "Beta") in the version number indicates, that the author does not yet consider the version fully/reliably/generally operational. As Paul pointed out: all information on "PyPI" is provided by the authors and, therefore, is not necessarily reliable. That said, I have met only isolated packages which were not usable (according to my standards - which may be very different from yours). From dfnsonfsduifb at gmx.de Tue Sep 24 05:50:49 2019 From: dfnsonfsduifb at gmx.de (Johannes Bauer) Date: Tue, 24 Sep 2019 11:50:49 +0200 Subject: Handling of disconnecting clients in asyncio Message-ID: Hi group, I'm trying to get into async programming using Python. Concretely I open a UNIX socket server in my application. The UNIX socket server generates events and also receives commands/responds to them. I do this by: async def _create_local_server(self): await asyncio.start_unix_server(self._local_server_tasks, path = "foo")) And then gather the command/response and event tasks: async def _local_server_tasks(self, reader, writer): await asyncio.gather( self._local_server_commands(reader, writer), self._local_server_events(reader, writer), ) I believe so far this is okay, right? If not, please tell me. Anyways, the event loop as an example: async def _local_server_events(self, reader, writer): while True: await asyncio.sleep(1) writer.write(b"event\n") And the command/response loop, obviously simplified: async def _local_server_commands(self, reader, writer): while True: msg = await reader.readline() writer.write(msg) Now I'm having the following issue: A client connects to my server and then properly disconnects (shutdown/RDWR, close). This causes the await reader.readline() to return an empty message (after which I can properly end the _local_server_commands loop). However, the _local_server_events loop does get no such notificiation. Nor does writer.write() throw an exception that I could catch (and exit as a consequence). Instead, I get this on stderr: socket.send() raised exception. socket.send() raised exception. socket.send() raised exception. socket.send() raised exception. [...] My questions are: 1. Is the design generally sane or is this usually done differently? I.e., am I making any obvious beginner mistakes? 2. What is the proper way of discovering a peer has disconnected and exiting cleanly? Thanks in advance, All the best, Johannes -- "Performance ist nicht das Problem, es l?uft ja nachher beides auf der selben Hardware." -- Hans-Peter Diettrich in d.s.e. From none at gmail.com Tue Sep 24 09:03:35 2019 From: none at gmail.com (ast) Date: Tue, 24 Sep 2019 15:03:35 +0200 Subject: Exception Message-ID: <5d8a142a$0$15482$426a34cc@news.free.fr> Hi It is not clear to me why the following code generates 2 exceptions, ZeroDivisionError and ArithmeticError. Since ZeroDivisionError is catched, it shoud not bubble out. Found here: https://www.pythonsheets.com/notes/python-new-py3.html >>> def func(): ... try: ... 1 / 0 ... except ZeroDivisionError: ... raise ArithmeticError ... >>> func() Traceback (most recent call last): File "", line 3, in func ZeroDivisionError: division by zero During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "", line 5, in func ArithmeticError There is a work around (python >= 3.3) >>> def func(): ... try: ... 1 / 0 ... except ZeroDivisionError: ... raise ArithmeticError from None ... >>> func() Traceback (most recent call last): File "", line 1, in File "", line 5, in func ArithmeticError From David.Raymond at tomtom.com Tue Sep 24 09:34:36 2019 From: David.Raymond at tomtom.com (David Raymond) Date: Tue, 24 Sep 2019 13:34:36 +0000 Subject: Exception In-Reply-To: <5d8a142a$0$15482$426a34cc@news.free.fr> References: <5d8a142a$0$15482$426a34cc@news.free.fr> Message-ID: I believe the idea is that previously, if you were handling an exception, and your handling code caused its own exception, then you would get no info about the original exception. So all useful information about the original problem would get lost because of an oopsie in the handling code. So it got changed to show both the original exception as well as the final one to be more useful/helpful. The raise ... from None is basically you explicitly saying "I am raising a different exception, so don't worry about the old one" Or such is my limited understanding anyway. -----Original Message----- From: Python-list On Behalf Of ast Sent: Tuesday, September 24, 2019 9:04 AM To: python-list at python.org Subject: Exception Hi It is not clear to me why the following code generates 2 exceptions, ZeroDivisionError and ArithmeticError. Since ZeroDivisionError is catched, it shoud not bubble out. Found here: https://www.pythonsheets.com/notes/python-new-py3.html >>> def func(): ... try: ... 1 / 0 ... except ZeroDivisionError: ... raise ArithmeticError ... >>> func() Traceback (most recent call last): File "", line 3, in func ZeroDivisionError: division by zero During handling of the above exception, another exception occurred: Traceback (most recent call last): File "", line 1, in File "", line 5, in func ArithmeticError There is a work around (python >= 3.3) >>> def func(): ... try: ... 1 / 0 ... except ZeroDivisionError: ... raise ArithmeticError from None ... >>> func() Traceback (most recent call last): File "", line 1, in File "", line 5, in func ArithmeticError -- https://mail.python.org/mailman/listinfo/python-list From uri at speedy.net Tue Sep 24 09:51:10 2019 From: uri at speedy.net (=?UTF-8?B?15DXldeo15k=?=) Date: Tue, 24 Sep 2019 16:51:10 +0300 Subject: Exception In-Reply-To: <5d8a142a$0$15482$426a34cc@news.free.fr> References: <5d8a142a$0$15482$426a34cc@news.free.fr> Message-ID: https://stackoverflow.com/a/24752607/1412564 ???? uri at speedy.net On Tue, Sep 24, 2019 at 4:07 PM ast wrote: > Hi > > It is not clear to me why the following code > generates 2 exceptions, ZeroDivisionError and > ArithmeticError. Since ZeroDivisionError is > catched, it shoud not bubble out. > > Found here: > https://www.pythonsheets.com/notes/python-new-py3.html > > >>> def func(): > ... try: > ... 1 / 0 > ... except ZeroDivisionError: > ... raise ArithmeticError > ... > >>> func() > Traceback (most recent call last): > File "", line 3, in func > ZeroDivisionError: division by zero > > During handling of the above exception, another exception occurred: > > Traceback (most recent call last): > File "", line 1, in > File "", line 5, in func > ArithmeticError > > > There is a work around (python >= 3.3) > > >>> def func(): > ... try: > ... 1 / 0 > ... except ZeroDivisionError: > ... raise ArithmeticError from None > ... > >>> func() > Traceback (most recent call last): > File "", line 1, in > File "", line 5, in func > ArithmeticError > -- > https://mail.python.org/mailman/listinfo/python-list > From none at gmail.com Tue Sep 24 11:19:06 2019 From: none at gmail.com (ast) Date: Tue, 24 Sep 2019 17:19:06 +0200 Subject: Exception In-Reply-To: References: <5d8a142a$0$15482$426a34cc@news.free.fr> Message-ID: <5d8a33ed$0$20307$426a34cc@news.free.fr> Le 24/09/2019 ? 15:51, ???? a ?crit?: > https://stackoverflow.com/a/24752607/1412564 thank you for link. it's clear now From exarkun at twistedmatrix.com Tue Sep 24 12:58:47 2019 From: exarkun at twistedmatrix.com (Jean-Paul Calderone) Date: Tue, 24 Sep 2019 12:58:47 -0400 Subject: Tahoe-LAFS on Python 3 - Call for Porters Message-ID: Hello Pythonistas, Earlier this year a number of Tahoe-LAFS community members began an effort to port Tahoe-LAFS from Python 2 to Python 3. Around five people are currently involved in a part-time capacity. We wish to accelerate the effort to ensure a Python 3-compatible release of Tahoe-LAFS can be made before the end of upstream support for CPython 2.x. Tahoe-LAFS is a Free and Open system for private, secure, decentralized storage. It encrypts and distributes your data across multiple servers. If some of the servers fail or are taken over by an attacker, the entire file store continues to function correctly, preserving your privacy and security. Foolscap , a dependency of Tahoe-LAFS, is also being ported. Foolscap is an object-capability-based RPC protocol with flexible serialization. Some details of the porting effort are available in a milestone on the Tahoe-LAFS trac instance . For this help, we are hoping to find a person/people with significant prior Python 3 porting experience and, preferably, some familiarity with Twisted, though in general the Tahoe-LAFS project welcomes contributors of all backgrounds and skill levels. We would prefer someone to start with us as soon as possible and no later than October 15th. If you are interested in this opportunity, please send us any questions you have, as well as details of your availability and any related work you have done previously (GitHub, LinkedIn links, etc). If you would like to find out more about this opportunity, please contact us at jessielisbetfrance at gmail (dot) com or on IRC in #tahoe-lafs on Freenode. Jean-Paul From mihir.kothari at gmail.com Tue Sep 24 15:55:25 2019 From: mihir.kothari at gmail.com (Mihir Kothari) Date: Tue, 24 Sep 2019 15:55:25 -0400 Subject: CSV reader ignore brackets Message-ID: Hi Team, I am using python 3.4. I have a CSV file as below: ABC,PQR,(TEST1,TEST2) FQW,RTE,MDE Basically comma-separated rows, where some rows have a data in column which is array like i.e. in brackets. So I need to read the file and treat such columns as one i.e. do not separate based on comma if it is inside the bracket. In short I need to read a CSV file where separator inside the brackets needs to be ignored. Output: Column: 1 2 3 Row1: ABC PQR (TEST1,TEST2) Row2: FQW RTE MDE Can you please help with the snippet? Regards, Mihir. From cs at cskk.id.au Tue Sep 24 19:09:02 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Wed, 25 Sep 2019 09:09:02 +1000 Subject: CSV reader ignore brackets In-Reply-To: References: Message-ID: <20190924230902.GA61342@cskk.homeip.net> On 24Sep2019 15:55, Mihir Kothari wrote: >I am using python 3.4. I have a CSV file as below: > >ABC,PQR,(TEST1,TEST2) >FQW,RTE,MDE Really? No quotes around the (TEST1,TEST2) column value? I would have said this is invalid data, but that does not help you. >Basically comma-separated rows, where some rows have a data in column which >is array like i.e. in brackets. >So I need to read the file and treat such columns as one i.e. do not >separate based on comma if it is inside the bracket. > >In short I need to read a CSV file where separator inside the brackets >needs to be ignored. > >Output: >Column: 1 2 3 >Row1: ABC PQR (TEST1,TEST2) >Row2: FQW RTE MDE > >Can you please help with the snippet? I would be reaching for a regular expression. If you partition your values into 2 types: those starting and ending in a bracket, and those not, you could write a regular expression for the former: \([^)]*\) which matches a string like (.....) (with, importantly, no embedded brackets, only those at the beginning and end. And you can write a regular expression like: [^,]* for a value containing no commas i.e. all the other values. Test the bracketed one first, because the second one always matches something. Then you would not use the CSV module (which expects better formed data than you have) and instead write a simple parser for a line of text which tries to match one of these two expressions repeatedly to consume the line. Something like this (UNTESTED): bracketed_re = re.compile(r'\([^)]*\)') no_commas_re = re.compile(r'[^,]*') def split_line(line): line = line.rstrip() # drop trailing whitespace/newline fields = [] offset = 0 while offset < len(line): m = bracketed_re.match(line, offset) if m: field = m.group() else: m = no_commas_re.match(line, offset) # this always matches field = m.group() fields.append(field) offset += len(field) if line.startswith(',', offset): # another column offset += 1 elif offset < len(line): raise ValueError( "incomplete parse at offset %d, line=%r" % (offset, line)) return fields Then read the lines of the file and split them into fields: row = [] with open(datafilename) as f: for line in f: fields = split_line(line) rows.append(fields) So basicly you're writing a little parser. If you have nested brackets things get harder. Cheers, Cameron Simpson From python at mrabarnett.plus.com Tue Sep 24 19:50:40 2019 From: python at mrabarnett.plus.com (MRAB) Date: Wed, 25 Sep 2019 00:50:40 +0100 Subject: CSV reader ignore brackets In-Reply-To: <20190924230902.GA61342@cskk.homeip.net> References: <20190924230902.GA61342@cskk.homeip.net> Message-ID: <57502407-0033-41d5-100a-6bc7540e2ed0@mrabarnett.plus.com> On 2019-09-25 00:09, Cameron Simpson wrote: > On 24Sep2019 15:55, Mihir Kothari wrote: >>I am using python 3.4. I have a CSV file as below: >> >>ABC,PQR,(TEST1,TEST2) >>FQW,RTE,MDE > > Really? No quotes around the (TEST1,TEST2) column value? I would have > said this is invalid data, but that does not help you. > >>Basically comma-separated rows, where some rows have a data in column which >>is array like i.e. in brackets. >>So I need to read the file and treat such columns as one i.e. do not >>separate based on comma if it is inside the bracket. >> >>In short I need to read a CSV file where separator inside the brackets >>needs to be ignored. >> >>Output: >>Column: 1 2 3 >>Row1: ABC PQR (TEST1,TEST2) >>Row2: FQW RTE MDE >> >>Can you please help with the snippet? > > I would be reaching for a regular expression. If you partition your > values into 2 types: those starting and ending in a bracket, and those > not, you could write a regular expression for the former: > > \([^)]*\) > > which matches a string like (.....) (with, importantly, no embedded > brackets, only those at the beginning and end. > > And you can write a regular expression like: > > [^,]* > > for a value containing no commas i.e. all the other values. > > Test the bracketed one first, because the second one always matches > something. > > Then you would not use the CSV module (which expects better formed data > than you have) and instead write a simple parser for a line of text > which tries to match one of these two expressions repeatedly to consume > the line. Something like this (UNTESTED): > > bracketed_re = re.compile(r'\([^)]*\)') > no_commas_re = re.compile(r'[^,]*') > > def split_line(line): > line = line.rstrip() # drop trailing whitespace/newline > fields = [] > offset = 0 > while offset < len(line): > m = bracketed_re.match(line, offset) > if m: > field = m.group() > else: > m = no_commas_re.match(line, offset) # this always matches > field = m.group() > fields.append(field) > offset += len(field) > if line.startswith(',', offset): > # another column > offset += 1 > elif offset < len(line): > raise ValueError( > "incomplete parse at offset %d, line=%r" % (offset, line)) > return fields > > Then read the lines of the file and split them into fields: > > row = [] > with open(datafilename) as f: > for line in f: > fields = split_line(line) > rows.append(fields) > > So basicly you're writing a little parser. If you have nested brackets > things get harder. > You can simplify that somewhat to this: import re rows = [] with open(datafilename) as f: for line in f: rows.append(re.findall(r'(\([^)]*\)|(?=.)[^,\n]*),?', line)) From skip.montanaro at gmail.com Tue Sep 24 20:02:46 2019 From: skip.montanaro at gmail.com (Skip Montanaro) Date: Tue, 24 Sep 2019 19:02:46 -0500 Subject: CSV reader ignore brackets In-Reply-To: <57502407-0033-41d5-100a-6bc7540e2ed0@mrabarnett.plus.com> References: <20190924230902.GA61342@cskk.homeip.net> <57502407-0033-41d5-100a-6bc7540e2ed0@mrabarnett.plus.com> Message-ID: How about just replacing *\(([^)]*)\)* with *"\1"* in a wrapper class's line reading method? (I think I have the re syntax approximately right.) The csv reader will "just work". Again, nesting parens not allowed. Skip From cs at cskk.id.au Tue Sep 24 21:09:28 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Wed, 25 Sep 2019 11:09:28 +1000 Subject: CSV reader ignore brackets In-Reply-To: References: Message-ID: <20190925010928.GA89934@cskk.homeip.net> On 24Sep2019 19:02, Skip Montanaro wrote: >How about just replacing *\(([^)]*)\)* with *"\1"* in a wrapper class's >line reading method? Will that work if the OP's (TEST1,TEST2) term itself contains quotes? Not that his example data did, but example data are usually incomplete :-) Also, that would match FOO(TEST1,TEST2)BAH as well (making FOO"(TEST1,TEST2)"BAH. Which might be wanted, or be not wanted or be bad data (including but not restricted to csv module unparsable data). I was deliberately being very conservative and kind of treating brackets like quotes (needest at start and end) but not trying to hit things in one go. Better to match exactly the special case you expect and then scour of mismatches than to incorrectly match and have that mistake buried in the data. >(I think I have the re syntax approximately right.) >The csv reader will "just work". Again, nesting parens not allowed. Otherwise, a neat idea. Besides, the point isn't the shortest code but to illustrate the idea of handling special syntax. Cheers, Cameron Simpson From frank at chagford.com Wed Sep 25 02:37:32 2019 From: frank at chagford.com (Frank Millman) Date: Wed, 25 Sep 2019 08:37:32 +0200 Subject: Python in The Economist Message-ID: The latest Technology Quarterly in The Economist is about "The Internet Of Things". Python gets a mention in an article on "How to build a disposable microchip". It is quite a long article, so here are the relevant extracts. "The goal is to produce a robust, bendable, mass-producible computer, complete with sensors and the ability to communicate with the outside world, for less than $0.01 apiece. A prototype version, shown off at Arm's headquarters in Cambridge, looks like a stiffer-than-usual piece of tape festooned with circuit traces." "The chip uses a simple form of machine learning called a Bayesian classifier. Flexibility of use was sacrificed: to keep thinks as cheap and simple as possible the algorithm is etched directly into the plastic, meaning the chips are not reprogrammable." "Since chip design is expensive, and chip designers scarce, he and his team have been working on software tools to simplify that task. The idea is to describe a new algorithm in Python, a widely used programming language, and then have software turn it into a circuit diagram that can be fed into Pragmatic's chipmaking machines. That approach has attracted interest from DARPA ..." Hope this is of interest. Frank Millman From ml at fam-goebel.de Wed Sep 25 07:04:38 2019 From: ml at fam-goebel.de (Ulrich Goebel) Date: Wed, 25 Sep 2019 13:04:38 +0200 Subject: Function to call a extern command as a filter Message-ID: <681c6d1f-0389-b39c-9607-af26f0f3c3dc@fam-goebel.de> Hello, what I want: I want a function which converts a markdown-formatted string to a latex-formatted string, like def markdown_to_latex (m : string) make a latex-formatted string l from the given markdown-formatted string m return (l) What I have: I have a extern tool pandoc which does exactly what I want, but it works on files. This tool is able to work as a pipe, so it uses the standard input and standard outpu. What I could do: def markdown_to_latex (m : string) write the string m to a file call pandoc to work on that file read the outputfile into the string l return (l) What would be nice: I would like to avoid the extra steps writing an reading extern files. Can anybody help me? Thanks Ulrich -- Ulrich Goebel Am B?chel 57, 53173 Bonn From rhodri at kynesim.co.uk Wed Sep 25 07:27:29 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Wed, 25 Sep 2019 12:27:29 +0100 Subject: Function to call a extern command as a filter In-Reply-To: <681c6d1f-0389-b39c-9607-af26f0f3c3dc@fam-goebel.de> References: <681c6d1f-0389-b39c-9607-af26f0f3c3dc@fam-goebel.de> Message-ID: <60f18e9a-c180-8ba9-82c2-789236aa9342@kynesim.co.uk> On 25/09/2019 12:04, Ulrich Goebel wrote: > Hello, > > what I want: > > I want a function which converts a markdown-formatted string to a > latex-formatted string [snip] > I have a extern tool pandoc which does exactly what I want, but it > works on files. This tool is able to work as a pipe, so it uses > the standard input and standard outpu. > > What I could do: > > def markdown_to_latex (m : string) > ????write the string m to a file > ????call pandoc to work on that file > ????read the outputfile into the string l > ????return (l) > > What would be nice: > > I would like to avoid the extra steps writing an reading extern files. subprocess is your friend here. Something like: import subprocess def mardown_to_latex(markdown_string): latex = subprocess.run(cmd_string_for_pandoc, input=markdown_string, string=True, capture_output=True) return latex.output The above is completely untested and not in the least bit robust, but that's the area of the standard library you should be looking at. -- Rhodri James *-* Kynesim Ltd From rhodri at kynesim.co.uk Wed Sep 25 07:08:02 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Wed, 25 Sep 2019 12:08:02 +0100 Subject: Python in The Economist In-Reply-To: References: Message-ID: <4922570b-9c14-3369-cb2b-30c87e89dd6f@kynesim.co.uk> On 25/09/2019 07:37, Frank Millman wrote: > "Since chip design is expensive, and chip designers scarce, he and his > team have been working on software tools to simplify that task. The idea > is to describe a new algorithm in Python, a widely used programming > language, and then have software turn it into a circuit diagram that can > be fed into Pragmatic's chipmaking machines. That approach has attracted > interest from DARPA ..." It is an interesting approach. Our experience as IoT consultants is that clients want what they want, and chip manufacturers produce what they produce, and the overlap isn't as big as you would hope. Making chip design simpler would certainly help. -- Rhodri James *-* Kynesim Ltd From rosuav at gmail.com Wed Sep 25 08:27:32 2019 From: rosuav at gmail.com (Chris Angelico) Date: Wed, 25 Sep 2019 22:27:32 +1000 Subject: Python in The Economist In-Reply-To: <4922570b-9c14-3369-cb2b-30c87e89dd6f@kynesim.co.uk> References: <4922570b-9c14-3369-cb2b-30c87e89dd6f@kynesim.co.uk> Message-ID: On Wed, Sep 25, 2019 at 9:33 PM Rhodri James wrote: > Our experience as IoT consultants is > that clients want what they want, and chip manufacturers produce what > they produce, and the overlap isn't as big as you would hope. Thank you for validating my inherent cynicism :) ChrisA From gheskett at shentel.net Wed Sep 25 09:33:09 2019 From: gheskett at shentel.net (Gene Heskett) Date: Wed, 25 Sep 2019 09:33:09 -0400 Subject: Python in The Economist In-Reply-To: References: <4922570b-9c14-3369-cb2b-30c87e89dd6f@kynesim.co.uk> Message-ID: <201909250933.09573.gheskett@shentel.net> On Wednesday 25 September 2019 08:27:32 Chris Angelico wrote: > On Wed, Sep 25, 2019 at 9:33 PM Rhodri James wrote: > > Our experience as IoT consultants is > > that clients want what they want, and chip manufacturers produce > > what they produce, and the overlap isn't as big as you would hope. > > Thank you for validating my inherent cynicism :) > > ChrisA You have this disparity demonstrated every time you go to the grocery store. After about a year, all the time wasteing are you sure crap has been removed from the card processing. Stuff that never should have been in the code path in the first place. Then a new card processor with his own card readers wins the next contract, and we repeat the same rodeo all over again. Except he bought the readers from a BBLB maker, and the failure rate is, predictably, astronomical. Cheers, Gene Heskett -- "There are four boxes to be used in defense of liberty: soap, ballot, jury, and ammo. Please use in that order." -Ed Howdershelt (Author) If we desire respect for the law, we must first make the law respectable. - Louis D. Brandeis Genes Web page From skip.montanaro at gmail.com Wed Sep 25 13:50:18 2019 From: skip.montanaro at gmail.com (Skip Montanaro) Date: Wed, 25 Sep 2019 12:50:18 -0500 Subject: CSV reader ignore brackets In-Reply-To: <20190925010928.GA89934@cskk.homeip.net> References: <20190925010928.GA89934@cskk.homeip.net> Message-ID: > Besides, the point isn't the shortest code but to illustrate the idea of handling special syntax. In my defense, I was typing on my phone while watching a show on Netflix. I was hardly in a position to test any code. :-) As you indicated though, the problem is under-specified (nesting?, presence of quotation marks?, newlines between balanced parens? input size?, etc). It probably does little good to try and cook up a comprehensive solution to such a problem. Better to just toss out some ideas for the OP and let them mull it over, maybe try to solve the problem themselves. Skip From lain.mailinglist at protonmail.com Wed Sep 25 13:00:05 2019 From: lain.mailinglist at protonmail.com (lain.mailinglist) Date: Wed, 25 Sep 2019 17:00:05 +0000 Subject: Function to call a extern command as a filter In-Reply-To: <60f18e9a-c180-8ba9-82c2-789236aa9342@kynesim.co.uk> References: <681c6d1f-0389-b39c-9607-af26f0f3c3dc@fam-goebel.de> <60f18e9a-c180-8ba9-82c2-789236aa9342@kynesim.co.uk> Message-ID: > On 25/09/2019 12:04, Ulrich Goebel wrote: > > > Hello, > > what I want: > > I want a function which converts a markdown-formatted string to a > > latex-formatted string > > [snip] > > > I have a extern tool pandoc which does exactly what I want, but it > > works on files. This tool is able to work as a pipe, so it uses > > the standard input and standard outpu. > > What I could do: > > def markdown_to_latex (m : string) > > ????write the string m to a file > > ????call pandoc to work on that file > > ????read the outputfile into the string l > > ????return (l) > > What would be nice: > > I would like to avoid the extra steps writing an reading extern files. > > subprocess is your friend here. Something like: > > import subprocess > def mardown_to_latex(markdown_string): > latex = subprocess.run(cmd_string_for_pandoc, > input=markdown_string, > string=True, > capture_output=True) > return latex.output > > The above is completely untested and not in the least bit robust, but > that's the area of the standard library you should be looking at. > > -- > > Rhodri James - Kynesim Ltd > > -- > > https://mail.python.org/mailman/listinfo/python-list You can also use the io.StringIO class of the stdlib, which is more suitable for this use case. From rhodri at kynesim.co.uk Wed Sep 25 14:03:36 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Wed, 25 Sep 2019 19:03:36 +0100 Subject: Function to call a extern command as a filter In-Reply-To: References: <681c6d1f-0389-b39c-9607-af26f0f3c3dc@fam-goebel.de> <60f18e9a-c180-8ba9-82c2-789236aa9342@kynesim.co.uk> Message-ID: <71638582-6f15-dab7-575e-4f44fa562d5e@kynesim.co.uk> On 25/09/2019 18:00, lain.mailinglist via Python-list wrote: > >> On 25/09/2019 12:04, Ulrich Goebel wrote: >> >>> Hello, >>> what I want: >>> I want a function which converts a markdown-formatted string to a >>> latex-formatted string >> >> [snip] >> >>> I have a extern tool pandoc which does exactly what I want, but it >>> works on files. This tool is able to work as a pipe, so it uses >>> the standard input and standard outpu. >>> What I could do: >>> def markdown_to_latex (m : string) >>> ????write the string m to a file >>> ????call pandoc to work on that file >>> ????read the outputfile into the string l >>> ????return (l) >>> What would be nice: >>> I would like to avoid the extra steps writing an reading extern files. >> >> subprocess is your friend here. Something like: [snip myself] > > You can also use the io.StringIO class of the stdlib, which is more suitable for this use case. Not for running external tools, surely? -- Rhodri James *-* Kynesim Ltd From tjreedy at udel.edu Wed Sep 25 15:30:33 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Wed, 25 Sep 2019 15:30:33 -0400 Subject: Function to call a extern command as a filter In-Reply-To: <60f18e9a-c180-8ba9-82c2-789236aa9342@kynesim.co.uk> References: <681c6d1f-0389-b39c-9607-af26f0f3c3dc@fam-goebel.de> <60f18e9a-c180-8ba9-82c2-789236aa9342@kynesim.co.uk> Message-ID: On 9/25/2019 7:27 AM, Rhodri James wrote: > On 25/09/2019 12:04, Ulrich Goebel wrote: >> Hello, >> >> what I want: >> >> I want a function which converts a markdown-formatted string to a >> latex-formatted string > [snip] >> I have a extern tool pandoc which does exactly what I want, but it >> works on files. This tool is able to work as a pipe, so it uses >> the standard input and standard outpu. I was not familiar with pandoc, but it seems to be a major m to n (where n > m) markup translator. https://pandoc.org/index.html https://github.com/jgm/pandoc (search 'pandoc github') says "Pandoc is a Haskell library for converting from one markup format to another, and a command-line tool that uses this library. " If you were writing in Haskell or if pandoc were written in Python or if pandoc were wrapped as a Python library, you could use it as an importable library. The third is true, as it is for so many useful libraries written in other languages. https://pypi.org/project/pypandoc/ (https://github.com/bebraw/pypandod) (search 'python pandoc') says it can use an existing pandoc installation or pandoc included with its Windows and Mac wheels (for pip install). https://github.com/applecrazy/pyandoc is another wrapping. One can write custom filters in Python (and other languages). https://pandoc.org/scripting-1.12.html https://github.com/jgm/pandocfilters https://pypi.org/project/pandocfilters/ >> What I could do: >> >> def markdown_to_latex (m : string) >> ?????write the string m to a file >> ?????call pandoc to work on that file >> ?????read the outputfile into the string l >> ?????return (l) >> >> What would be nice: >> >> I would like to avoid the extra steps writing an reading extern files. > > subprocess is your friend here.? Something like: Short of using pypandoc or pyandoc, this is probably the best thing. > import subprocess > def mardown_to_latex(markdown_string): > ? latex = subprocess.run(cmd_string_for_pandoc, > ???????????????????????? input=markdown_string, > ???????????????????????? string=True, > ???????????????????????? capture_output=True) > ? return latex.output > > The above is completely untested and not in the least bit robust, but > that's the area of the standard library you should be looking at. > -- Terry Jan Reedy From arshadd at rediffmail.com Wed Sep 25 14:42:11 2019 From: arshadd at rediffmail.com (arshad ali) Date: 25 Sep 2019 18:42:11 -0000 Subject: =?utf-8?B?SEVMUCAgTkVFREVEIGFwcGxpY2F0aW9uIGVycm9yIDB4YzAwMDAwNQ==?= Message-ID: <1569408337.S.1575.1079.f4mail-235-222.rediffmail.com.1569436931.23257@webmail.rediffmail.com> Note: Forwarded message attached -- Original Message -- From: "arshad ali"arshadd at rediffmail.com To: python-list at python.org Subject: HELP NEEDED application error 0xc000005 -------------- next part -------------- An embedded message was scrubbed... From: "arshad ali" Subject: HELP NEEDED application error 0xc000005 Date: no date Size: 837 URL: From none at gmail.com Thu Sep 26 02:34:43 2019 From: none at gmail.com (ast) Date: Thu, 26 Sep 2019 08:34:43 +0200 Subject: Funny code Message-ID: <5d8c5c06$0$15192$426a34cc@news.free.fr> Hello A line of code which produce itself when executed >>> s='s=%r;print(s%%s)';print(s%s) s='s=%r;print(s%%s)';print(s%s) Thats funny ! From auriocus at gmx.de Thu Sep 26 04:28:58 2019 From: auriocus at gmx.de (Christian Gollwitzer) Date: Thu, 26 Sep 2019 10:28:58 +0200 Subject: Funny code In-Reply-To: <5d8c5c06$0$15192$426a34cc@news.free.fr> References: <5d8c5c06$0$15192$426a34cc@news.free.fr> Message-ID: Am 26.09.19 um 08:34 schrieb ast: > Hello > > A line of code which produce itself when executed > > >>> s='s=%r;print(s%%s)';print(s%s) > s='s=%r;print(s%%s)';print(s%s) > > Thats funny ! This is called a "quine" and there exist several methods how to do this. https://en.wikipedia.org/wiki/Quine_(computing) Christian From spencerdu at hotmail.co.uk Thu Sep 26 04:52:28 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Thu, 26 Sep 2019 01:52:28 -0700 (PDT) Subject: How to publish a message of the qspinbox value when the qspinbox sets the slider with corresponding value. Message-ID: Hi How do I publish a message of the qspinbox value when the qspinbox sets the slider with corresponding value. Thanks # -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'laser.ui' # # Created by: PyQt5 UI code generator 5.13.0 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets from pyqtconfig import ConfigManager class Ui_Laser(object): def setupUi(self, Laser): self.config = ConfigManager() Laser.setObjectName("Laser") Laser.resize(379, 274) Laser.setMinimumSize(QtCore.QSize(379, 268)) self.config.set_defaults({ 'number': 13, 'number2': 0, 'number3': 0, 'number4': 0, 'number5': 0, 'number6': 0, 'on': True, }) self.centralwidget = QtWidgets.QWidget(Laser) self.centralwidget.setObjectName("centralwidget") self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget) self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 70, 371, 181)) self.gridLayoutWidget.setObjectName("gridLayoutWidget") self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget) self.gridLayout.setContentsMargins(0, 0, 0, 0) self.gridLayout.setObjectName("gridLayout") self.spinBox_6 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_6.setMaximum(100) self.spinBox_6.setObjectName("spinBox_6") self.gridLayout.addWidget(self.spinBox_6, 1, 5, 1, 1) self.config.add_handler('number', self.spinBox_6) self.verticalSlider_2 = QtWidgets.QSlider(self.gridLayoutWidget) self.verticalSlider_2.setOrientation(QtCore.Qt.Vertical) self.verticalSlider_2.setObjectName("verticalSlider_2") self.gridLayout.addWidget(self.verticalSlider_2, 0, 1, 1, 1) self.verticalSlider_5 = QtWidgets.QSlider(self.gridLayoutWidget) self.verticalSlider_5.setOrientation(QtCore.Qt.Vertical) self.verticalSlider_5.setObjectName("verticalSlider_5") self.gridLayout.addWidget(self.verticalSlider_5, 0, 4, 1, 1) self.spinBox = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox.setMaximum(100) self.spinBox.setObjectName("spinBox") self.gridLayout.addWidget(self.spinBox, 1, 0, 1, 1) self.verticalSlider = QtWidgets.QSlider(self.gridLayoutWidget) self.verticalSlider.setOrientation(QtCore.Qt.Vertical) self.verticalSlider.setObjectName("verticalSlider") self.gridLayout.addWidget(self.verticalSlider, 0, 0, 1, 1) self.spinBox_2 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_2.setMaximum(100) self.spinBox_2.setObjectName("spinBox_2") self.gridLayout.addWidget(self.spinBox_2, 1, 1, 1, 1) self.verticalSlider_3 = QtWidgets.QSlider(self.gridLayoutWidget) self.verticalSlider_3.setOrientation(QtCore.Qt.Vertical) self.verticalSlider_3.setObjectName("verticalSlider_3") self.gridLayout.addWidget(self.verticalSlider_3, 0, 2, 1, 1) self.spinBox_5 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_5.setMaximum(100) self.spinBox_5.setObjectName("spinBox_5") self.gridLayout.addWidget(self.spinBox_5, 1, 4, 1, 1) self.verticalSlider_6 = QtWidgets.QSlider(self.gridLayoutWidget) self.verticalSlider_6.setOrientation(QtCore.Qt.Vertical) self.verticalSlider_6.setObjectName("verticalSlider_6") self.gridLayout.addWidget(self.verticalSlider_6, 0, 5, 1, 1) self.spinBox_4 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_4.setMaximum(100) self.spinBox_4.setObjectName("spinBox_4") self.gridLayout.addWidget(self.spinBox_4, 1, 3, 1, 1) self.verticalSlider_4 = QtWidgets.QSlider(self.gridLayoutWidget) self.verticalSlider_4.setOrientation(QtCore.Qt.Vertical) self.verticalSlider_4.setObjectName("verticalSlider_4") self.gridLayout.addWidget(self.verticalSlider_4, 0, 3, 1, 1) self.verticalSlider_7 = QtWidgets.QSlider(self.gridLayoutWidget) self.verticalSlider_7.setOrientation(QtCore.Qt.Vertical) self.verticalSlider_7.setObjectName("verticalSlider_7") self.gridLayout.addWidget(self.verticalSlider_7, 0, 6, 1, 1) self.spinBox_3 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_3.setMaximum(100) self.spinBox_3.setObjectName("spinBox_3") self.gridLayout.addWidget(self.spinBox_3, 1, 2, 1, 1) self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(0, 50, 41, 19)) self.pushButton.setCheckable(True) self.pushButton.setObjectName("pushButton") self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_2.setGeometry(QtCore.QRect(50, 50, 41, 19)) self.pushButton_2.setCheckable(True) self.pushButton_2.setObjectName("pushButton_2") self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_3.setGeometry(QtCore.QRect(110, 50, 41, 19)) self.pushButton_3.setCheckable(True) self.pushButton_3.setObjectName("pushButton_3") self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_4.setGeometry(QtCore.QRect(160, 50, 41, 19)) self.pushButton_4.setCheckable(True) self.pushButton_4.setObjectName("pushButton_4") self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_5.setGeometry(QtCore.QRect(220, 50, 41, 19)) self.pushButton_5.setCheckable(True) self.pushButton_5.setObjectName("pushButton_5") self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_6.setGeometry(QtCore.QRect(270, 50, 41, 19)) self.pushButton_6.setCheckable(True) self.pushButton_6.setObjectName("pushButton_6") self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_7.setGeometry(QtCore.QRect(330, 50, 41, 19)) self.pushButton_7.setCheckable(True) self.pushButton_7.setObjectName("pushButton_7") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(0, 40, 39, 11)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(50, 40, 39, 11)) self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(self.centralwidget) self.label_3.setGeometry(QtCore.QRect(110, 40, 39, 11)) self.label_3.setObjectName("label_3") self.label_4 = QtWidgets.QLabel(self.centralwidget) self.label_4.setGeometry(QtCore.QRect(160, 40, 39, 11)) self.label_4.setObjectName("label_4") self.label_5 = QtWidgets.QLabel(self.centralwidget) self.label_5.setGeometry(QtCore.QRect(220, 40, 39, 11)) self.label_5.setObjectName("label_5") self.label_6 = QtWidgets.QLabel(self.centralwidget) self.label_6.setGeometry(QtCore.QRect(270, 40, 39, 11)) self.label_6.setObjectName("label_6") self.label_7 = QtWidgets.QLabel(self.centralwidget) self.label_7.setGeometry(QtCore.QRect(330, 40, 39, 11)) self.label_7.setObjectName("label_7") self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_8.setGeometry(QtCore.QRect(10, 10, 62, 19)) self.pushButton_8.setCheckable(True) self.pushButton_8.setChecked(True) self.pushButton_8.setObjectName("pushButton_8") Laser.setCentralWidget(self.centralwidget) self.statusbar = QtWidgets.QStatusBar(Laser) self.statusbar.setObjectName("statusbar") Laser.setStatusBar(self.statusbar) self.retranslateUi(Laser) self.spinBox.valueChanged['int'].connect(self.verticalSlider.setValue) print("Value " + str(self.verticalSlider.value())) self.spinBox_2.valueChanged['int'].connect(self.verticalSlider_2.setValue) self.spinBox_3.valueChanged['int'].connect(self.verticalSlider_3.setValue) self.spinBox_4.valueChanged['int'].connect(self.verticalSlider_4.setValue) self.spinBox_5.valueChanged['int'].connect(self.verticalSlider_5.setValue) self.spinBox_6.valueChanged['int'].connect(self.verticalSlider_6.setValue) QtCore.QMetaObject.connectSlotsByName(Laser) def retranslateUi(self, Laser): _translate = QtCore.QCoreApplication.translate Laser.setWindowTitle(_translate("Laser", "MainWindow")) self.pushButton.setText(_translate("Laser", "ON")) self.pushButton_2.setText(_translate("Laser", "ON")) self.pushButton_3.setText(_translate("Laser", "ON")) self.pushButton_4.setText(_translate("Laser", "ON")) self.pushButton_5.setText(_translate("Laser", "ON")) self.pushButton_6.setText(_translate("Laser", "ON")) self.pushButton_7.setText(_translate("Laser", "ON")) self.label.setText(_translate("Laser", "445nm")) self.label_2.setText(_translate("Laser", "488nm")) self.label_3.setText(_translate("Laser", "515nm")) self.label_4.setText(_translate("Laser", "561nm")) self.label_5.setText(_translate("Laser", "594nm")) self.label_6.setText(_translate("Laser", "638nm")) self.label_7.setText(_translate("Laser", "LED")) self.pushButton_8.setText(_translate("Laser", "ON")) self.config.add_handler('on', self.pushButton_8) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Laser = QtWidgets.QMainWindow() ui = Ui_Laser() ui.setupUi(Laser) Laser.show() sys.exit(app.exec_()) From rob at despammer.com Thu Sep 26 05:14:40 2019 From: rob at despammer.com (RobH) Date: Thu, 26 Sep 2019 10:14:40 +0100 Subject: NEWBIE: how to get text onto 2 lines on a 16x2 lcd display Message-ID: I have some sample/demo python code for scrolling and outputting text onto a 16x2 lcd display. I would like to put my own message or text outputting to the lcd on 2 lines. I have tried using lcd.message('my message',1) and lcd.message('my message', 2), but the output says: TypeError: message() takes exactly 2 arguments (3 given) I have also seen this on the, stackexchange site: lcd_string("your text " + str(yourVar), 1) But what is str(yourVar), as I assume it means a variable. If I could have a working example please, that would be great. Thanks From PythonList at DancesWithMice.info Thu Sep 26 06:08:57 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Thu, 26 Sep 2019 22:08:57 +1200 Subject: NEWBIE: how to get text onto 2 lines on a 16x2 lcd display In-Reply-To: References: Message-ID: <35ad4830-b8f8-e503-2273-a6249c5bf1c1@DancesWithMice.info> On 26/09/19 9:14 PM, RobH wrote: > I have some sample/demo python code for scrolling and outputting text > onto a 16x2 lcd display. > > I would like to put my own message or text outputting to the lcd on 2 > lines. I have tried using lcd.message('my message',1) and > lcd.message('my message', 2), but the output says: > > TypeError: message() takes exactly 2 arguments (3 given) > > I have also seen this on the, stackexchange site: > lcd_string("your text " + str(yourVar), 1) > > But what is str(yourVar), as I assume it means a variable. > If I could have a working example please, that would be great. I'm wondering if "lcd_string" should be "lcd.string" (per "lcd.message") - would it be better to post the actual (not) working code? Suggest you fire-up the Python REPR: python3 # on a Linux terminal then: import ***whatever the LCD package is called *** help( ***whatever...called *** ) The output from this will tell you the names of all the entities within the package. Within that you will be able to check for the pertinent class (from which lcd was derived) and see what it says about arguments for the message() and/or string() methods - particularly the number of arguments and their data-type(s). -- Regards =dn From rob at despammer.com Thu Sep 26 06:58:15 2019 From: rob at despammer.com (RobH) Date: Thu, 26 Sep 2019 11:58:15 +0100 Subject: NEWBIE: how to get text onto 2 lines on a 16x2 lcd display In-Reply-To: References: <35ad4830-b8f8-e503-2273-a6249c5bf1c1@DancesWithMice.info> Message-ID: On 26/09/2019 11:08, DL Neil wrote: > On 26/09/19 9:14 PM, RobH wrote: >> I have some sample/demo python code for scrolling and outputting text >> onto a 16x2 lcd display. >> >> I would like to put my own message or text outputting to the lcd on 2 >> lines. I have tried using lcd.message('my message',1) and >> lcd.message('my message', 2), but the output says: >> >> TypeError: message() takes exactly 2 arguments (3 given) >> >> I have also seen this on the, stackexchange site: >> lcd_string("your text " + str(yourVar), 1) >> >> But what is str(yourVar), as I assume it means a variable. >> If I could have a working example please, that would be great. > > > I'm wondering if "lcd_string" should be "lcd.string" (per "lcd.message") > ?- would it be better to post the actual (not) working code? > > Suggest you fire-up the Python REPR: > > ????python3??????? # on a Linux terminal > > then: > > ????import ***whatever the LCD package is called *** > ????help( ***whatever...called *** ) > > The output from this will tell you the names of all the entities within > the package. Within that you will be able to check for the pertinent > class (from which lcd was derived) and see what it says about arguments > for the message() and/or string() methods - particularly the number of > arguments and their data-type(s). Thanks, but was is Python REPR. This my adaptation of non working code. Bodged from the char_lcd.py code: GNU nano 2.7.4 File: char_lcd.py #!/usr/bin/python # Example using a character LCD connected to a Raspberry Pi or BeagleBone Black. import time import Adafruit_CharLCD as LCD # Raspberry Pi pin configuration: lcd_rs = 27 # Note this might need to be changed to 21 for older revision Pi's. lcd_en = 22 lcd_d4 = 25 lcd_d5 = 24 lcd_d6 = 23 lcd_d7 = 18 lcd_backlight = 4 # BeagleBone Black configuration: # lcd_rs = 'P8_8' # lcd_en = 'P8_10' # lcd_d4 = 'P8_18' # lcd_d5 = 'P8_16' # lcd_d6 = 'P8_14' # lcd_d7 = 'P8_12' # lcd_backlight = 'P8_7' # Define LCD column and row size for 16x2 LCD. lcd_columns = 16 lcd_rows = 2 # Alternatively specify a 20x4 LCD. # lcd_columns = 20 # lcd_rows = 4 # Initialize the LCD using the pins above. lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows, lcd_backlight) # Print a two line message # lcd.message('Hello\nworld!') lcd.message( "Hello" 1) # Wait 5 seconds time.sleep(5.0) # Demo showing the cursor. #lcd.clear() # Wait 5 seconds time.sleep(5.0) # Demo showing the cursor. #lcd.clear() #lcd.show_cursor(True) #lcd.message('Show cursor') lcd.message("Your dental",1) lcd.message("appointment is",2) time.sleep(5.0) # Demo showing the blinking cursor. lcd.clear() From sjeik_appie at hotmail.com Thu Sep 26 07:32:06 2019 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Thu, 26 Sep 2019 11:32:06 +0000 Subject: Funny code In-Reply-To: Message-ID: On 26 Sep 2019 10:28, Christian Gollwitzer wrote: Am 26.09.19 um 08:34 schrieb ast: > Hello > > A line of code which produce itself when executed > > >>> s='s=%r;print(s%%s)';print(s%s) > s='s=%r;print(s%%s)';print(s%s) > > Thats funny ! ==> Also impressive, a 128-language quine: https://github.com/mame/quine-relay From none at gmail.com Thu Sep 26 08:20:45 2019 From: none at gmail.com (ast) Date: Thu, 26 Sep 2019 14:20:45 +0200 Subject: __init__ is not invoked Message-ID: <5d8cad21$0$15163$426a74cc@news.free.fr> Hello In the following code found here: https://www.pythonsheets.com/notes/python-object.html __init__ is not invoked when we create an object with "o = ClassB("Hello")". I don't understand why. I know the correct way to define __new__ is to write "return object.__new__(cls, arg)" and not "return object" >>> class ClassB(object): ... def __new__(cls, arg): ... print('__new__ ' + arg) ... return object ... def __init__(self, arg): ... print('__init__ ' + arg) ... >>> o = ClassB("Hello") __new__ Hello Normaly, when running "o = ClassB("Hello")", we first run __call__ from type of ClassB which is type. This __call__ method is supposed to launch __new__ from ClassB and then __init__ from classB too. The output of __new__ is mapped to self parameter of __init__. But it seems things don't work like that here. Why ? From rhodri at kynesim.co.uk Thu Sep 26 08:36:54 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Thu, 26 Sep 2019 13:36:54 +0100 Subject: __init__ is not invoked In-Reply-To: <5d8cad21$0$15163$426a74cc@news.free.fr> References: <5d8cad21$0$15163$426a74cc@news.free.fr> Message-ID: <9288cab4-dbf2-d82e-e9af-40d0d4022fb0@kynesim.co.uk> On 26/09/2019 13:20, ast wrote: > Hello > > In the following code found here: > https://www.pythonsheets.com/notes/python-object.html > > __init__ is not invoked when we create an object > with "o = ClassB("Hello")". I don't understand why. > I know the correct way to define __new__ is to write > "return object.__new__(cls, arg)" and not "return object" > > > >>> class ClassB(object): > ...???? def __new__(cls, arg): > ...???????? print('__new__ ' + arg) > ...???????? return object > ...???? def __init__(self, arg): > ...???????? print('__init__ ' + arg) > ... > > >>> o = ClassB("Hello") > __new__ Hello > > > Normaly, when running "o = ClassB("Hello")", we first run > __call__ from type of ClassB which is type. This __call__ > method is supposed to launch __new__ from ClassB and then > __init__ from classB too. The output of __new__ is mapped > to self parameter of __init__. > But it seems things don't work like that here. Why ? One more command to the REPL helps make things clearer: >>> o I would venture that o.__init__() is called, but object's __init__ doesn't do anything. On top of that, the language definition states that "The return value of __new__() should be the new object instance (usually an instance of cls)". You have returned the class object, not an instance of anything, so on the whole it's lucky that object.__init__() does nothing! -- Rhodri James *-* Kynesim Ltd From none at gmail.com Thu Sep 26 08:38:44 2019 From: none at gmail.com (ast) Date: Thu, 26 Sep 2019 14:38:44 +0200 Subject: __init__ is not invoked In-Reply-To: <5d8cad21$0$15163$426a74cc@news.free.fr> References: <5d8cad21$0$15163$426a74cc@news.free.fr> Message-ID: <5d8cb157$0$3378$426a74cc@news.free.fr> Le 26/09/2019 ? 14:20, ast a ?crit?: > Hello > > In the following code found here: > https://www.pythonsheets.com/notes/python-object.html > > __init__ is not invoked when we create an object > with "o = ClassB("Hello")". I don't understand why. > I know the correct way to define __new__ is to write > "return object.__new__(cls, arg)" and not "return object" > > > >>> class ClassB(object): > ...???? def __new__(cls, arg): > ...???????? print('__new__ ' + arg) > ...???????? return object > ...???? def __init__(self, arg): > ...???????? print('__init__ ' + arg) > ... > > >>> o = ClassB("Hello") > __new__ Hello > > > Normaly, when running "o = ClassB("Hello")", we first run > __call__ from type of ClassB which is type. This __call__ > method is supposed to launch __new__ from ClassB and then > __init__ from classB too. The output of __new__ is mapped > to self parameter of __init__. > But it seems things don't work like that here. Why ? Maybe __init__ is called by object.__new__ call and not by type.__call_ as I thought. So of course here without any call to object.__new__, __init__ is not called From rhodri at kynesim.co.uk Thu Sep 26 07:55:50 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Thu, 26 Sep 2019 12:55:50 +0100 Subject: NEWBIE: how to get text onto 2 lines on a 16x2 lcd display In-Reply-To: References: <35ad4830-b8f8-e503-2273-a6249c5bf1c1@DancesWithMice.info> Message-ID: <257cc1c5-38ae-9525-17ee-80262e7f774c@kynesim.co.uk> On 26/09/2019 11:58, RobH wrote: > Thanks, but was is Python REPR. DL was referring to the interactive program you get when you type "python" at a Linux or Windows command prompt. Here's an example, copied from my Linux box: rhodri at scrote:~$ python Python 2.7.15+ (default, Jul 9 2019, 16:51:35) [GCC 7.4.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import cgi >>> help(cgi) [[screeds of documentation for the cgi module cut]] >>> [[type control-D to get out]] rhodri at scrote:~$ (The bits in [[double square brackets]] are my comments, not something either I or Python typed!) > This my adaptation of non working code. Bodged from the char_lcd.py code: > > ? GNU nano 2.7.4 ???????????????????????? File: char_lcd.py > > #!/usr/bin/python > # Example using a character LCD connected to a Raspberry Pi or > BeagleBone Black. > import time > > import Adafruit_CharLCD as LCD Aha, that's the critical information we were lacking earlier. I found the Adafruit_CharLCD library after a little googling. It's marked as deprecated (the author now uses something else), but if it works for you you might as well keep on using it. [[Much set-up for the Pi cut for brevity]] > # Initialize the LCD using the pins above. > lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, lcd_d7, > ?????????????????????????? lcd_columns, lcd_rows, lcd_backlight) > > # Print a two line message > # lcd.message('Hello\nworld!') > lcd.message( "Hello" 1) It looks like lcd.message() takes a text string and displays on the LCD, starting at the current cursor position (I'll get back to that). You optimistically added an extra argument, the line number to start at (you're missing a comma, but that's an easy typo to make and I assume you already caught it). Unfortunately lcd.message() only takes the one argument, the text string. This is where that error message comes from: "TypeError: message() takes exactly 2 arguments (3 given)" means that you gave the function more arguments than it knew what to do with. (Why 2 and 3 instead of 1 and 2? "lcd" itself is an argument to message(), telling it which lcd to send the message to if you had more than one. When you look at the function definition, you'll see that it start with "def message(self, text):" which makes that a bit more explicit. Anyway, that's beside the point.) So how do you control where you start your message on the LCD? It looks like the answer is the lcd.set_cursor() function. The cursor is the point from which your printing starts. In your text editor it's probably a blinking vertical line (or underscore, or blob... editors vary). On your LCD display it's probably invisible, though it looks like there's another function to change that. Anyway, you give set_cursor() a column number and a row number in that order. I think they start from 0, so to write "Hello" to the second line of your display you would write: lcd.set_cursor(0, 1) lcd.message("Hello") The gotcha here is that message() is only going to write the characters you tell it to, and won't overwrite anything else. If for example you followed up the two lines above with: lcd.set_cursor(0, 1) # Back to the start of the line lcd.message("Bye") you would end up with "Byelo" displayed on the LCD. You would need to put extra spaces on the end of your message to overwrite the characters you don't want any more, but not so many that you overwrite what you want to keep. Working out how many spaces that is for any given message could be quite tedious. You may well find it easier to lcd.clear() the whole display and rewrite everything when you want to change anything. Anyway, I hope that helps. -- Rhodri James *-* Kynesim Ltd From __peter__ at web.de Thu Sep 26 09:17:09 2019 From: __peter__ at web.de (Peter Otten) Date: Thu, 26 Sep 2019 15:17:09 +0200 Subject: __init__ is not invoked References: <5d8cad21$0$15163$426a74cc@news.free.fr> Message-ID: ast wrote: > Hello > > In the following code found here: > https://www.pythonsheets.com/notes/python-object.html > > __init__ is not invoked when we create an object > with "o = ClassB("Hello")". I don't understand why. > I know the correct way to define __new__ is to write > "return object.__new__(cls, arg)" and not "return object" > > > >>> class ClassB(object): > ... def __new__(cls, arg): > ... print('__new__ ' + arg) > ... return object > ... def __init__(self, arg): > ... print('__init__ ' + arg) > ... > > >>> o = ClassB("Hello") > __new__ Hello > > > Normaly, when running "o = ClassB("Hello")", we first run > __call__ from type of ClassB which is type. This __call__ > method is supposed to launch __new__ from ClassB and then > __init__ from classB too. The output of __new__ is mapped > to self parameter of __init__. > But it seems things don't work like that here. Why ? __init__ is called only if __new__ returns an instance of ClassB: """ /* If the returned object is not an instance of type, it won't be initialized. */ if (!PyType_IsSubtype(Py_TYPE(obj), type)) return obj; type = Py_TYPE(obj); if (type->tp_init != NULL) { int res = type->tp_init(obj, args, kwds); """ https://github.com/python/cpython/blob/master/Objects/typeobject.c#L982 From gisle.vanem at gmail.com Thu Sep 26 10:08:46 2019 From: gisle.vanem at gmail.com (Gisle Vanem) Date: Thu, 26 Sep 2019 16:08:46 +0200 Subject: PIP question Message-ID: <2ac90a03-7a5d-f17b-dc23-4270d22928e2@gmail.com> Hello list. This little program should print the location of all my installed Python packages: ---------- 8< -- 8< --------------- import pip try: packages = pip.get_installed_distributions (local_only=False, skip=()) except AttributeError: import pkg_resources # Python3 packages = pkg_resources.working_set package_list = [] for p in sorted (packages): print ("%-20s -> %s" % (p.key, p.location)) ---------- 8< -- 8< --------------- But for my Python 3.6 installation, it claims I have an '-ip' package: -ip -> f:\programfiler\python36\lib\site-packages I fail to find one, but I do have a: f:\programfiler\python36\lib\site-packages\~ip-19.1.1.dist-info directory with some possible leftovers. It there a connection between this mysterious '-ip' package and this directory? -- --gv From cdoare.ext at orange.com Thu Sep 26 10:12:58 2019 From: cdoare.ext at orange.com (cdoare.ext at orange.com) Date: Thu, 26 Sep 2019 14:12:58 +0000 Subject: Pysmnp : setCmd doesn't modify the object value Message-ID: <17670_1569507179_5D8CC76B_17670_225_7_A2B41E5C09C6EB4D89E3D9622E69391E38A7C955@OPEXCAUBMA1.corporate.adroot.infra.ftgroup> Hello, I tried to set a value from setCmd. Execution doesn't return any error but the snmp request is not sent ! g = setCmd(SnmpEngine() , CommunityData('public', mpModel=1) , UdpTransportTarget(('x.x.x.x', 161)) , ContextData() , ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.7.364904448'), int('1'))) According to the MIB information, the value can be 1, 2 or 3 (integer type). I assume that the type value set (int('1)) is not the right one, but I don't find the solution to pass the right type. Thanks for your help. Christian ___________________________ Christian Doar? (Groupe Open) T?l : +33 2 96 07 02 63 Mobile : +33 6 33 51 41 15 cdoare.ext at orange.com _________________________________________________________________________________________________________________________ Ce message et ses pieces jointes peuvent contenir des informations confidentielles ou privilegiees et ne doivent donc pas etre diffuses, exploites ou copies sans autorisation. Si vous avez recu ce message par erreur, veuillez le signaler a l'expediteur et le detruire ainsi que les pieces jointes. Les messages electroniques etant susceptibles d'alteration, Orange decline toute responsabilite si ce message a ete altere, deforme ou falsifie. Merci. This message and its attachments may contain confidential or privileged information that may be protected by law; they should not be distributed, used or copied without authorisation. If you have received this email in error, please notify the sender and delete this message and its attachments. As emails may be altered, Orange is not liable for messages that have been modified, changed or falsified. Thank you. From rosuav at gmail.com Thu Sep 26 10:22:37 2019 From: rosuav at gmail.com (Chris Angelico) Date: Fri, 27 Sep 2019 00:22:37 +1000 Subject: NEWBIE: how to get text onto 2 lines on a 16x2 lcd display In-Reply-To: References: <35ad4830-b8f8-e503-2273-a6249c5bf1c1@DancesWithMice.info> Message-ID: On Thu, Sep 26, 2019 at 9:01 PM RobH wrote: > > import Adafruit_CharLCD as LCD > > > # Raspberry Pi pin configuration: Ah, it's an RPi with Adafruit. That's the same library that my brother uses. I don't know much of the details, but in case it's helpful, here's the code that currently runs his subscriber appreciation board: https://github.com/stephenangelico/SubBoard/blob/master/lcd_char.py You might be able to borrow some ideas from that. ChrisA From oscar.j.benjamin at gmail.com Thu Sep 26 10:22:58 2019 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 26 Sep 2019 15:22:58 +0100 Subject: __init__ is not invoked In-Reply-To: References: <5d8cad21$0$15163$426a74cc@news.free.fr> Message-ID: On Thu, 26 Sep 2019 at 14:19, Peter Otten <__peter__ at web.de> wrote: > > __init__ is called only if __new__ returns an instance of ClassB: > > """ > /* If the returned object is not an instance of type, > it won't be initialized. */ > if (!PyType_IsSubtype(Py_TYPE(obj), type)) > return obj; > > type = Py_TYPE(obj); > if (type->tp_init != NULL) { > int res = type->tp_init(obj, args, kwds); > """ > > https://github.com/python/cpython/blob/master/Objects/typeobject.c#L982 Interesting. I hadn't realised that was how it works. I tested with this: """ class A: def __new__(cls, arg): if arg == 'a': return object.__new__(A) elif arg == 'b': return object.__new__(B) elif arg == 'c': return object.__new__(C) elif arg == 'd': return D('foo') def __init__(self, arg): print('A.__init__', arg) class B: def __init__(self, arg): print('B.__init__', arg) class C(A): def __init__(self, arg): print('C.__init__', arg) class D(A): def __new__(cls, arg): return object.__new__(cls) def __init__(self, arg): print('D.__init__', arg) A('a') A('b') A('c') A('d') """ Output: $ python tmp.py A.__init__ a C.__init__ c D.__init__ foo D.__init__ d So B.__init__ is never called and D.__init__ is called twice. I wonder if there is a sensible way of organising this. Certainly I've always taken the approach that you either use __new__ or __init__ and don't mix them up. -- Oscar From oscar.j.benjamin at gmail.com Thu Sep 26 10:35:55 2019 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 26 Sep 2019 15:35:55 +0100 Subject: __init__ is not invoked In-Reply-To: <9288cab4-dbf2-d82e-e9af-40d0d4022fb0@kynesim.co.uk> References: <5d8cad21$0$15163$426a74cc@news.free.fr> <9288cab4-dbf2-d82e-e9af-40d0d4022fb0@kynesim.co.uk> Message-ID: On Thu, 26 Sep 2019 at 13:39, Rhodri James wrote: > > On 26/09/2019 13:20, ast wrote: > > > > >>> class ClassB(object): > > ... def __new__(cls, arg): > > ... print('__new__ ' + arg) > > ... return object > > ... def __init__(self, arg): > > ... print('__init__ ' + arg) > > ... > > > > >>> o = ClassB("Hello") > > __new__ Hello > You have returned the class object, not > an instance of anything, Well object is an instance of object as well as of type: >>> isinstance(object, object) True >>> isinstance(object, type) True >>> isinstance(type, object) True >>> isinstance(type, type) True >>> issubclass(object, type) False >>> issubclass(type, object) True >>> type.mro(type) [, ] >>> type.mro(object) [] >>> type(object) >>> type(type) -- Oscar From rhodri at kynesim.co.uk Thu Sep 26 10:44:25 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Thu, 26 Sep 2019 15:44:25 +0100 Subject: __init__ is not invoked In-Reply-To: References: <5d8cad21$0$15163$426a74cc@news.free.fr> <9288cab4-dbf2-d82e-e9af-40d0d4022fb0@kynesim.co.uk> Message-ID: <23d1c298-f921-87b2-9fed-1ba58c42e28c@kynesim.co.uk> On 26/09/2019 15:35, Oscar Benjamin wrote: > On Thu, 26 Sep 2019 at 13:39, Rhodri James wrote: >> You have returned the class object, not >> an instance of anything, > Well object is an instance of object as well as of type: Fair point. I keep forgetting that. -- Rhodri James *-* Kynesim Ltd From none at gmail.com Thu Sep 26 11:13:30 2019 From: none at gmail.com (ast) Date: Thu, 26 Sep 2019 17:13:30 +0200 Subject: __init__ is not invoked In-Reply-To: References: <5d8cad21$0$15163$426a74cc@news.free.fr> Message-ID: <5d8cd59d$0$21613$426a74cc@news.free.fr> Le 26/09/2019 ? 15:17, Peter Otten a ?crit?: > ast wrote: > > > __init__ is called only if __new__ returns an instance of ClassB: > I was ignoring that. thank you From rob at despammer.com Thu Sep 26 11:34:50 2019 From: rob at despammer.com (RobH) Date: Thu, 26 Sep 2019 16:34:50 +0100 Subject: NEWBIE: how to get text onto 2 lines on a 16x2 lcd display In-Reply-To: References: <35ad4830-b8f8-e503-2273-a6249c5bf1c1@DancesWithMice.info> <257cc1c5-38ae-9525-17ee-80262e7f774c@kynesim.co.uk> Message-ID: On 26/09/2019 12:55, Rhodri James wrote: > On 26/09/2019 11:58, RobH wrote: >> Thanks, but was is Python REPR. > > DL was referring to the interactive program you get when you type > "python" at a Linux or Windows command prompt.? Here's an example, > copied from my Linux box: > > rhodri at scrote:~$ python > Python 2.7.15+ (default, Jul? 9 2019, 16:51:35) > [GCC 7.4.0] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> import cgi > >>> help(cgi) > [[screeds of documentation for the cgi module cut]] > >>> [[type control-D to get out]] > rhodri at scrote:~$ > > (The bits in [[double square brackets]] are my comments, not something > either I or Python typed!) > >> This my adaptation of non working code. Bodged from the char_lcd.py code: >> >> ?? GNU nano 2.7.4 ???????????????????????? File: char_lcd.py >> >> #!/usr/bin/python >> # Example using a character LCD connected to a Raspberry Pi or >> BeagleBone Black. >> import time >> >> import Adafruit_CharLCD as LCD > > Aha, that's the critical information we were lacking earlier.? I found > the Adafruit_CharLCD library after a little googling.? It's marked as > deprecated (the author now uses something else), but if it works for you > you might as well keep on using it. > > [[Much set-up for the Pi cut for brevity]] > >> # Initialize the LCD using the pins above. >> lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6, >> lcd_d7, >> ??????????????????????????? lcd_columns, lcd_rows, lcd_backlight) >> >> # Print a two line message >> # lcd.message('Hello\nworld!') >> lcd.message( "Hello" 1) > > It looks like lcd.message() takes a text string and displays on the LCD, > starting at the current cursor position (I'll get back to that).? You > optimistically added an extra argument, the line number to start at > (you're missing a comma, but that's an easy typo to make and I assume > you already caught it).? Unfortunately lcd.message() only takes the one > argument, the text string.? This is where that error message comes from: > "TypeError: message() takes exactly 2 arguments (3 given)" means that > you gave the function more arguments than it knew what to do with. > > (Why 2 and 3 instead of 1 and 2?? "lcd" itself is an argument to > message(), telling it which lcd to send the message to if you had more > than one.? When you look at the function definition, you'll see that it > start with "def message(self, text):" which makes that a bit more > explicit.? Anyway, that's beside the point.) > > So how do you control where you start your message on the LCD?? It looks > like the answer is the lcd.set_cursor() function.? The cursor is the > point from which your printing starts.? In your text editor it's > probably a blinking vertical line (or underscore, or blob... editors > vary).? On your LCD display it's probably invisible, though it looks > like there's another function to change that. > > Anyway, you give set_cursor() a column number and a row number in that > order.? I think they start from 0, so to write "Hello" to the second > line of your display you would write: > > ? lcd.set_cursor(0, 1) > ? lcd.message("Hello") > > The gotcha here is that message() is only going to write the characters > you tell it to, and won't overwrite anything else.? If for example you > followed up the two lines above with: > > ? lcd.set_cursor(0, 1)? # Back to the start of the line > ? lcd.message("Bye") > > you would end up with "Byelo" displayed on the LCD.? You would need to > put extra spaces on the end of your message to overwrite the characters > you don't want any more, but not so many that you overwrite what you > want to keep.? Working out how many spaces that is for any given message > could be quite tedious.? You may well find it easier to lcd.clear() the > whole display and rewrite everything when you want to change anything. > > Anyway, I hope that helps. > Thank you, that does help me just great. So simple as well. Actually I have worked quite a lot with Arduinos and the code or sketch for an lcd there is just the same to write to the 2nd line. I didn't think it would work for python, but it does! Other lines like lcd.delay(1000) and lcd.begin(16,2), do not work in python, but I thought to try them to find out. I am using a Raspberry Pi Zero for this little project, which now does what I want for now. Thanks again. From piet-l at vanoostrum.org Thu Sep 26 11:54:45 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Thu, 26 Sep 2019 17:54:45 +0200 Subject: CSV reader ignore brackets References: <20190924230902.GA61342@cskk.homeip.net> <57502407-0033-41d5-100a-6bc7540e2ed0@mrabarnett.plus.com> Message-ID: Skip Montanaro writes: > How about just replacing *\(([^)]*)\)* with *"\1"* in a wrapper class's > line reading method? (I think I have the re syntax approximately right.) > The csv reader will "just work". Again, nesting parens not allowed. > > Skip here is some working code: def PReader(csvfile): import re for line in csvfile: line = re.sub(r'\(.*?\)', '"\g<0>"', line) yield line import csv with open('testcsv.csv') as csvfile: reader = csv.reader(PReader(csvfile), quotechar='"') for row in reader: print(row) -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From rob at despammer.com Thu Sep 26 12:26:36 2019 From: rob at despammer.com (RobH) Date: Thu, 26 Sep 2019 17:26:36 +0100 Subject: NEWBIE: how to get text onto 2 lines on a 16x2 lcd display In-Reply-To: References: <35ad4830-b8f8-e503-2273-a6249c5bf1c1@DancesWithMice.info> Message-ID: On 26/09/2019 15:22, Chris Angelico wrote: > On Thu, Sep 26, 2019 at 9:01 PM RobH wrote: >> >> import Adafruit_CharLCD as LCD >> >> >> # Raspberry Pi pin configuration: > > Ah, it's an RPi with Adafruit. That's the same library that my brother > uses. I don't know much of the details, but in case it's helpful, > here's the code that currently runs his subscriber appreciation board: > > https://github.com/stephenangelico/SubBoard/blob/master/lcd_char.py > > You might be able to borrow some ideas from that. > > ChrisA > Thanks and yes it is the adafruit char_lcd.py file I was using, but now a modded one to suit my needs. I see that lcd_char.py is a modded version as well, and I will download it to see what I can use from it. Thanks From rob at despammer.com Thu Sep 26 15:21:27 2019 From: rob at despammer.com (RobH) Date: Thu, 26 Sep 2019 20:21:27 +0100 Subject: NEWBIE: how to get text onto 2 lines on a 16x2 lcd display In-Reply-To: References: <35ad4830-b8f8-e503-2273-a6249c5bf1c1@DancesWithMice.info> <92ppoe5pbb4kp4jle55559olho46hkv9g0@4ax.com> Message-ID: On 26/09/2019 17:51, Dennis Lee Bieber wrote: > On Thu, 26 Sep 2019 11:58:15 +0100, RobH declaimed the > following: > > >> >> import Adafruit_CharLCD as LCD >> > > FYI: from Adafruit's download site: > https://github.com/adafruit/Adafruit_Python_CharLCD > """ > DEPRECATED LIBRARY. Adafruit Python CharLCD > > This library has been deprecated! We are leaving this up for historical and > research purposes but archiving the repository. > > We are now only supporting the use of our CircuitPython libraries for use > with Python. > > Check out this guide for info on using character LCDs with the > CircuitPython library: > https://learn.adafruit.com/character-lcds/python-circuitpython > """ > > >> # Print a two line message >> # lcd.message('Hello\nworld!') > > The example "two-line message" relies upon an embedded new-line > character. > >> lcd.message( "Hello" 1) > > This should produce a syntax error > > .message(string) only takes one argument -- a string to display. If said > string contains a newline, the library invokes a .set_cursor(col, row) to > move to the start of the next line (though line is limited to the defined > configuration maximum). > > If you want to manually position text, you'll need to invoke > .set_cursor(col, row) to do that, then invoke .message(string) to provide > the text. > > As mentioned, the library you are using is no longer supported by > AdaFruit. They would prefer you to install the "adafruit_blinka" library > which provides an interface to invoke CircuitPython libraries from boards > running full Python (CircuitPython runs on microcontrollers like AdaFruit's > Metro cards). (You'd then have to also install the CircuitPython libraries) > > https://learn.adafruit.com/circuitpython-on-raspberrypi-linux > (also applies to BeagleBone Black, as I recall) > > > Thanks for that, as I didn't realise it was deprecated, and have downloaded the circuitpython charLCD files. Also, I note on the site from the link for circuitpython, there is information and examples of how to put text on 2 lines, using the embedded newline character. As I said I am a newbie with python and I did not realise that this would do what I wanted, doh! # Print a two line message # lcd.message('Hello\nworld!'). Thanks again. From rob at despammer.com Thu Sep 26 18:04:15 2019 From: rob at despammer.com (RobH) Date: Thu, 26 Sep 2019 23:04:15 +0100 Subject: NEWBIE: how to get text onto 2 lines on a 16x2 lcd display In-Reply-To: References: <35ad4830-b8f8-e503-2273-a6249c5bf1c1@DancesWithMice.info> <92ppoe5pbb4kp4jle55559olho46hkv9g0@4ax.com> Message-ID: On 26/09/2019 20:21, RobH wrote: > On 26/09/2019 17:51, Dennis Lee Bieber wrote: >> On Thu, 26 Sep 2019 11:58:15 +0100, RobH declaimed >> the >> following: >> >> >>> >>> import Adafruit_CharLCD as LCD >>> >> >> ????FYI: from Adafruit's download site: >> https://github.com/adafruit/Adafruit_Python_CharLCD >> """ >> DEPRECATED LIBRARY. Adafruit Python CharLCD >> >> This library has been deprecated! We are leaving this up for >> historical and >> research purposes but archiving the repository. >> >> We are now only supporting the use of our CircuitPython libraries for use >> with Python. >> >> Check out this guide for info on using character LCDs with the >> CircuitPython library: >> https://learn.adafruit.com/character-lcds/python-circuitpython >> """ >> >> >>> # Print a two line message >>> # lcd.message('Hello\nworld!') >> >> ????The example "two-line message" relies upon an embedded new-line >> character. >> >>> lcd.message( "Hello" 1) >> >> ????This should produce a syntax error >> >> .message(string) only takes one argument -- a string to display. If said >> string contains a newline, the library invokes a .set_cursor(col, row) to >> move to the start of the next line (though line is limited to the defined >> configuration maximum). >> >> ????If you want to manually position text, you'll need to invoke >> .set_cursor(col, row) to do that, then invoke .message(string) to provide >> the text. >> >> ????As mentioned, the library you are using is no longer supported by >> AdaFruit. They would prefer you to install the "adafruit_blinka" library >> which provides an interface to invoke CircuitPython libraries from boards >> running full Python (CircuitPython runs on microcontrollers like >> AdaFruit's >> Metro cards). (You'd then have to also install the CircuitPython >> libraries) >> >> https://learn.adafruit.com/circuitpython-on-raspberrypi-linux >> (also applies to BeagleBone Black, as I recall) >> >> >> > Thanks for that, as I didn't realise it was deprecated, and have > downloaded the circuitpython charLCD files. > > Also, I note on the site from the link for circuitpython, there is > information and examples of how to put text on 2 lines, using the > embedded newline character. > > As I said I am a newbie with python and I did not realise that this > would do what I wanted, doh! > > # Print a two line message > # lcd.message('Hello\nworld!'). > > Thanks again. > As I said, I have downloaded the circuitpython chalcd files from the link using pip3 install, but after downloading I can't find any Adafruit folders on my pi zero. Doing a search for adafruit does not show anything. You don't happen to know where it goes to do you. Thanks From PythonList at DancesWithMice.info Thu Sep 26 20:12:56 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Fri, 27 Sep 2019 12:12:56 +1200 Subject: NEWBIE: how to get text onto 2 lines on a 16x2 lcd display In-Reply-To: References: <35ad4830-b8f8-e503-2273-a6249c5bf1c1@DancesWithMice.info> <92ppoe5pbb4kp4jle55559olho46hkv9g0@4ax.com> Message-ID: On 27/09/19 7:21 AM, RobH wrote: > On 26/09/2019 17:51, Dennis Lee Bieber wrote: >> On Thu, 26 Sep 2019 11:58:15 +0100, RobH declaimed >> the >> following: ... >> Check out this guide for info on using character LCDs with the >> CircuitPython library: >> https://learn.adafruit.com/character-lcds/python-circuitpython >> """ ... > As I said I am a newbie with python and I did not realise that this > would do what I wanted, doh! We all have to start somewhere! As it happens, I was hoping to work on a Raspberry Pi4 project, and have been looking at adding cameras and displays. Sadly/ironically I will be working on a different project which involves an nVidia Jetson Nano - so I'll have to look-up CUDA (apparently it is not a car, nor is it a fish that I'd be happy to help you eat...) The question I wanted to ask/suggestion to make: is there a 'development environment' which enables development on a 'bigger PC', for later delivery=download to the SBC? It might make coding and unit-testing a bit easier (although perhaps not when specific (display) hardware enters the picture). -- Regards =dn From hongyi.zhao at gmail.com Thu Sep 26 22:14:00 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Fri, 27 Sep 2019 02:14:00 +0000 (UTC) Subject: NameError: name 'msvcrt' is not defined Message-ID: Hi, I use python on Debian, when running some python codes, I meet the following error: ----------- input_username.py", line 18, in read_input if msvcrt.kbhit(): NameError: name 'msvcrt' is not defined ------------ How to solve this issue? From python at mrabarnett.plus.com Thu Sep 26 23:05:13 2019 From: python at mrabarnett.plus.com (MRAB) Date: Fri, 27 Sep 2019 04:05:13 +0100 Subject: NameError: name 'msvcrt' is not defined In-Reply-To: References: Message-ID: <6a69a12f-2f2e-b93f-48dd-fbdea905f93e@mrabarnett.plus.com> On 2019-09-27 03:14, Hongyi Zhao wrote: > Hi, > > I use python on Debian, when running some python codes, I meet the > following error: > > ----------- > input_username.py", line 18, in read_input > if msvcrt.kbhit(): > NameError: name 'msvcrt' is not defined > ------------ > > > How to solve this issue? > That code detects whether a key is being pressed on the Windows operating system. You need to find the equivalent for Linux. From dieter at handshake.de Fri Sep 27 01:21:20 2019 From: dieter at handshake.de (dieter) Date: Fri, 27 Sep 2019 07:21:20 +0200 Subject: PIP question References: <2ac90a03-7a5d-f17b-dc23-4270d22928e2@gmail.com> Message-ID: <875zleco1b.fsf@handshake.de> Gisle Vanem writes: > ... pip list ... > But for my Python 3.6 installation, it claims I have > an '-ip' package: > -ip -> f:\programfiler\python36\lib\site-packages > > I fail to find one, but I do have a: > f:\programfiler\python36\lib\site-packages\~ip-19.1.1.dist-info > > directory with some possible leftovers. It there a connection > between this mysterious '-ip' package and this directory? This is possible. A so called "distribution" can install packages of a different name (for example, the distribution "Zope" installs (among others) a package "ZPublisher"). In addition, the actual installation typically has lost metadata information (e.g. the version number). Therefore, "pip" may use "*.dist-info" or "*.egg-info" directories to provide this metadata. Look into those directories to find out which packages are installed for the corresponding distribution. You can then check whether those packages are truely available (and if not delete the *-info* directory). From blmadhavan at gmail.com Fri Sep 27 05:13:31 2019 From: blmadhavan at gmail.com (Madhavan Bomidi) Date: Fri, 27 Sep 2019 02:13:31 -0700 (PDT) Subject: Angular distribution rose diagram in Python Message-ID: <249f6e94-3c61-46cd-852d-acca38c323c2@googlegroups.com> Hi, Can someone help me to make python code (with some arbitrary data) for the angular distribution rose diagram as shown in figure 7 in the paper accessible through the web-link: https://www.nat-hazards-earth-syst-sci.net/17/1425/2017/nhess-17-1425-2017.pdf Thanks in advance Madhavan From rob at despammer.com Fri Sep 27 05:48:29 2019 From: rob at despammer.com (RobH) Date: Fri, 27 Sep 2019 10:48:29 +0100 Subject: NEWBIE: how to get text onto 2 lines on a 16x2 lcd display In-Reply-To: References: <35ad4830-b8f8-e503-2273-a6249c5bf1c1@DancesWithMice.info> <92ppoe5pbb4kp4jle55559olho46hkv9g0@4ax.com> <260roe5rd196qs4hsk3o6uv65itr9t10ul@4ax.com> Message-ID: On 27/09/2019 04:51, Dennis Lee Bieber wrote: > On Thu, 26 Sep 2019 23:04:15 +0100, RobH declaimed the > following: > > >>> >> As I said, I have downloaded the circuitpython chalcd files from the >> link using pip3 install, but after downloading I can't find any Adafruit >> folders on my pi zero. Doing a search for adafruit does not show anything. >> > > Did you also install the adafruit-blinka library? > > On a Beaglebone Black (my R-Pi is in the depth of major apt-get > update/upgrade cycle -- it feels like it's updating 50% of the OS given how > long it's been running, and that's on a 3B+ quadcore compared to the slower > single core BBB) I find blinka in > > debian at beaglebone:~$ sudo find / -iname "*blinka*" > /usr/local/lib/python3.5/dist-packages/Adafruit_Blinka-2.5.0-py3.5.egg-info > /usr/local/lib/python3.5/dist-packages/adafruit_blinka > debian at beaglebone:~$ > > Note: I ran the installs using "sudo pip3 ..." to make things act > globally; if you ran without sudo the files might be in a hidden directory > of the "pi" account. > > Okay, an incomplete search of the R-Pi, stealing cycles from the > upgrade processing) > > pi at raspberrypi:~$ sudo find / -iname "*adafruit*" > > /usr/local/lib/python3.7/dist-packages/Adafruit_PureIO > /usr/local/lib/python3.7/dist-packages/adafruit_blinka > /usr/local/lib/python3.7/dist-packages/Adafruit_PlatformDetect-1.3.4.dist-info > /usr/local/lib/python3.7/dist-packages/Adafruit_PureIO-0.2.3.dist-info > /usr/local/lib/python3.7/dist-packages/Adafruit_Blinka-2.5.1.dist-info > /usr/local/lib/python3.7/dist-packages/adafruit_platformdetect > > > Ok, the adafruit_character_lcd is in the same directory as yours, and so is Blinka and Purio. It seems to be a bit of a long path to type to get to the Adafruit_Charlcd directory, but is there a shortcut way of getting to the said directory. Thanks From none at gmail.com Fri Sep 27 07:54:19 2019 From: none at gmail.com (ast) Date: Fri, 27 Sep 2019 13:54:19 +0200 Subject: Recursive method in class Message-ID: <5d8df86e$0$3370$426a74cc@news.free.fr> Hello Is it feasible to define a recursive method in a class ? (I don't need it, it's just a trial) Here are failing codes: class Test: def fib(self, n): if n < 2: return n return fib(self, n-2) + fib(self, n-1) t = Test() t.fib(6) --------------------- Traceback (most recent call last): return fib(self, n-2) + fib(self, n-1) NameError: name 'fib' is not defined --------------------- An other try: class Test: @staticmethod def fib(n): if n < 2: return n return fib(n-2) + fib(n-1) t = Test() t.fib(6) ------------------------ Traceback (most recent call last): return fib(n-2) + fib(n-1) NameError: name 'fib' is not defined ------------------------- From spencerdu at hotmail.co.uk Fri Sep 27 08:11:14 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Fri, 27 Sep 2019 05:11:14 -0700 (PDT) Subject: How to publish a message of the qspinbox value when the qspinbox sets the slider with corresponding value. Message-ID: <64269894-7234-4c33-91df-bc572ec9bb7e@googlegroups.com> Hi When the values of the spinboxes is set a message should be published from the GUI containing the value from the spinbox which then sets the qlabel with the value. After this if I close and reload the gui the widgets should be set with the values set from the previous time. When the embedded.py file closes/stops running the gui is reset to its default state when no spinbox and qlabel are set with values. First execute the embedded.py file. Enter 'laser' when 'devices to be activated' appears. Once this executed a publish and subscribe should happen and a laser actor python file starts which launches the laser device on the embedded.py file. Once this is complete launch GUI.py in a separate command line and click add device for the device in the combo box. Here are the files needed. embedded.py import paho.mqtt.client as mqtt from mqtt import * import os import time import json def start(): try: os.remove("list_of_devices_currently_active.txt") print("Awaiting devices to be activated") except: print("Awaiting devices to be activated") start() devices = list(map(str,input("Devices to be activated: ").split(","))) client = device() client.run() client.loop_start() print("Connected to broker") time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI") client.subscribe("microscope/light_sheet_microscope/UI") print("Publishing message to topic", "microscope/light_sheet_microscope/UI") client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": "system", "payload":{"name": devices, "cmd": "activating devices"}}, indent=2)) time.sleep(1) def active_devices(): for item in devices: device = (item + "Embedded") deviceImport = __import__(device) with open("list_of_devices_currently_active.txt", "a+") as myfile: for item in devices: myfile.write(item + "\n") active_devices() def readFile(fname): print("List of devices currently active:") try: with open(fname, "r") as f: for item in f: print(item.rstrip("\n")) except: print("No devices added yet") readFile("list_of_devices_currently_active.txt") client = device() client.run() client.loop_start() print("Connected to broker") time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI/states") client.subscribe("microscope/light_sheet_microscope/UI/states") client.loop_forever() mqtt.py import logging import paho.mqtt.client as mqtt import json class device(mqtt.Client): def on_connect(self, mqtt, obj, flags, rc): pass def on_message(self, mqtt, userdata, message): m_decode = str(message.payload.decode("utf-8")) print("message recieved= " + m_decode) # print("File which you want to import(with .py extension)") print("message topic=", message.topic) print("message qos=", message.qos) print("message retain flag=", message.retain) m_in = json.loads(m_decode) def run(self): self.connect("localhost", 1883, 60) GUI.py import paho.mqtt.client as mqtt import os import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5 import QtWidgets, uic from mqtt import * import json import time class MainWindow(QtWidgets.QMainWindow): def __init__(self,parent = None): QMainWindow.__init__(self) super(MainWindow, self).__init__(parent) self.mdi = QMdiArea() self.setCentralWidget(self.mdi) self.setMinimumSize(QSize(800, 600)) self.setWindowTitle("PyQt button example - pythonprogramminglanguage.com") client = device() client.run() client.loop_start() print("Connected to broker") time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI/devices") client.subscribe("microscope/light_sheet_microscope/UI/devices") print("Publishing message to topic", "microscope/light_sheet_microscope/UI/devices") client.publish("microscope/light_sheet_microscope/UI/devices", json.dumps({"type": "system", "payload":{"cmd": "get all devices"}}, indent=2)) time.sleep(1) pybutton = QPushButton('Add device', self) pybutton.clicked.connect(self.importbutton) pybutton.move(100, 400) pybutton.resize(150, 32) self.combo = QComboBox(self) self.combo.move(100,350) self.combo.resize(100, 32) def readFile(fname): try: with open(fname, "r") as f: for item in f: self.combo.addItem(item) except: print("No devices active") readFile("list_of_devices_currently_active.txt") def importbutton(self): client = device() client.run() client.loop_start() print("Connected to broker") time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI/add device") client.subscribe("microscope/light_sheet_microscope/UI/add device") print("Publishing message to topic", "microscope/light_sheet_microscope/UI/add device") client.publish("microscope/light_sheet_microscope/UI/add device", json.dumps({"type": "system", "payload":{"cmd": "init device panel"}}, indent=2)) time.sleep(1) client.loop_stop() self.fileName_UI = self.combo.currentText() self.loadGUI() print("Device panel initialised" + "\n") def loadGUI(self): module = __import__(self.fileName_UI.rstrip("\n")) my_class = getattr(module, "SubWindow") sub = QMdiSubWindow() sub.setWidget(my_class()) sub.setWindowTitle(self.fileName_UI) self.mdi.addSubWindow(sub) sub.show() if __name__ == "__main__": app = QApplication(sys.argv) mainWin = MainWindow() mainWin.show() # publishedMessage = mainWin.getGUIFilename() sys.exit(app.exec_()) laser.py from PyQt5 import QtCore, QtGui, QtWidgets, uic from PyQt5.QtWidgets import * from PyQt5.QtCore import * from laser2 import * from mqtt import * import json import time import sys class SubWindow(QtWidgets.QMainWindow): def __init__(self, parent = None): super(SubWindow, self).__init__(parent) self.setMinimumSize(QSize(379, 268)) self.ui = Ui_Laser() self.ui.setupUi(self) def main(): app = QtWidgets.QApplication(sys.argv) application = SubWindow() application.show() sys.exit(app.exec_()) if __name__ == "__main__": main() laser2.py from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Laser(object): def setupUi(self, Laser): Laser.setObjectName("Laser") Laser.resize(379, 274) Laser.setMinimumSize(QtCore.QSize(379, 268)) self.centralwidget = QtWidgets.QWidget(Laser) self.centralwidget.setObjectName("centralwidget") self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget) self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 70, 371, 181)) self.gridLayoutWidget.setObjectName("gridLayoutWidget") self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget) self.gridLayout.setContentsMargins(0, 0, 0, 0) self.gridLayout.setObjectName("gridLayout") self.spinBox_2 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_2.setMaximum(100) self.spinBox_2.setObjectName("spinBox_2") self.gridLayout.addWidget(self.spinBox_2, 1, 1, 1, 1) self.spinBox_7 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_7.setObjectName("spinBox_7") self.gridLayout.addWidget(self.spinBox_7, 1, 3, 1, 1) self.label_13 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_13.setObjectName("label_13") self.gridLayout.addWidget(self.label_13, 0, 5, 1, 1) self.label_11 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_11.setObjectName("label_11") self.gridLayout.addWidget(self.label_11, 0, 3, 1, 1) self.spinBox_4 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_4.setMaximum(100) self.spinBox_4.setObjectName("spinBox_4") self.gridLayout.addWidget(self.spinBox_4, 1, 4, 1, 1) self.spinBox_5 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_5.setMaximum(100) self.spinBox_5.setObjectName("spinBox_5") self.gridLayout.addWidget(self.spinBox_5, 1, 5, 1, 1) self.spinBox = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox.setMaximum(100) self.spinBox.setObjectName("spinBox") self.gridLayout.addWidget(self.spinBox, 1, 0, 1, 1) self.label_12 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_12.setObjectName("label_12") self.gridLayout.addWidget(self.label_12, 0, 4, 1, 1) self.label_14 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_14.setObjectName("label_14") self.gridLayout.addWidget(self.label_14, 0, 6, 1, 1) self.spinBox_6 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_6.setMaximum(100) self.spinBox_6.setObjectName("spinBox_6") self.gridLayout.addWidget(self.spinBox_6, 1, 6, 1, 1) self.label_10 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_10.setObjectName("label_10") self.gridLayout.addWidget(self.label_10, 0, 2, 1, 1) self.spinBox_3 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_3.setMaximum(100) self.spinBox_3.setObjectName("spinBox_3") self.gridLayout.addWidget(self.spinBox_3, 1, 2, 1, 1) self.label_9 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_9.setObjectName("label_9") self.gridLayout.addWidget(self.label_9, 0, 1, 1, 1) self.label_8 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_8.setObjectName("label_8") self.gridLayout.addWidget(self.label_8, 0, 0, 1, 1) self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(0, 50, 41, 19)) self.pushButton.setCheckable(True) self.pushButton.setObjectName("pushButton") self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_2.setGeometry(QtCore.QRect(50, 50, 41, 19)) self.pushButton_2.setCheckable(True) self.pushButton_2.setObjectName("pushButton_2") self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_3.setGeometry(QtCore.QRect(110, 50, 41, 19)) self.pushButton_3.setCheckable(True) self.pushButton_3.setObjectName("pushButton_3") self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_4.setGeometry(QtCore.QRect(160, 50, 41, 19)) self.pushButton_4.setCheckable(True) self.pushButton_4.setObjectName("pushButton_4") self.pushButton_5 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_5.setGeometry(QtCore.QRect(220, 50, 41, 19)) self.pushButton_5.setCheckable(True) self.pushButton_5.setObjectName("pushButton_5") self.pushButton_6 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_6.setGeometry(QtCore.QRect(270, 50, 41, 19)) self.pushButton_6.setCheckable(True) self.pushButton_6.setObjectName("pushButton_6") self.pushButton_7 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_7.setGeometry(QtCore.QRect(330, 50, 41, 19)) self.pushButton_7.setCheckable(True) self.pushButton_7.setObjectName("pushButton_7") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(0, 40, 39, 11)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(50, 40, 39, 11)) self.label_2.setObjectName("label_2") self.label_3 = QtWidgets.QLabel(self.centralwidget) self.label_3.setGeometry(QtCore.QRect(110, 40, 39, 11)) self.label_3.setObjectName("label_3") self.label_4 = QtWidgets.QLabel(self.centralwidget) self.label_4.setGeometry(QtCore.QRect(160, 40, 39, 11)) self.label_4.setObjectName("label_4") self.label_5 = QtWidgets.QLabel(self.centralwidget) self.label_5.setGeometry(QtCore.QRect(220, 40, 39, 11)) self.label_5.setObjectName("label_5") self.label_6 = QtWidgets.QLabel(self.centralwidget) self.label_6.setGeometry(QtCore.QRect(270, 40, 39, 11)) self.label_6.setObjectName("label_6") self.label_7 = QtWidgets.QLabel(self.centralwidget) self.label_7.setGeometry(QtCore.QRect(330, 40, 39, 11)) self.label_7.setObjectName("label_7") self.pushButton_8 = QtWidgets.QPushButton(self.centralwidget) self.pushButton_8.setGeometry(QtCore.QRect(10, 10, 62, 19)) self.pushButton_8.setCheckable(True) self.pushButton_8.setChecked(True) self.pushButton_8.setObjectName("pushButton_8") Laser.setCentralWidget(self.centralwidget) self.statusbar = QtWidgets.QStatusBar(Laser) self.statusbar.setObjectName("statusbar") Laser.setStatusBar(self.statusbar) self.retranslateUi(Laser) self.spinBox.valueChanged['int'].connect(self.hi) self.spinBox_2.valueChanged['int'].connect(self.label_9.setNum) self.spinBox_3.valueChanged['int'].connect(self.label_10.setNum) self.spinBox_7.valueChanged['int'].connect(self.label_11.setNum) self.spinBox_4.valueChanged['int'].connect(self.label_12.setNum) self.spinBox_5.valueChanged['int'].connect(self.label_13.setNum) self.spinBox_6.valueChanged['int'].connect(self.label_14.setNum) QtCore.QMetaObject.connectSlotsByName(Laser) def hi(self): self.spinBox.valueChanged['int'].connect(self.hi) def retranslateUi(self, Laser): _translate = QtCore.QCoreApplication.translate Laser.setWindowTitle(_translate("Laser", "MainWindow")) self.label_13.setText(_translate("Laser", " %")) self.label_11.setText(_translate("Laser", " %")) self.label_12.setText(_translate("Laser", " %")) self.label_14.setText(_translate("Laser", " %")) self.label_10.setText(_translate("Laser", " %")) self.label_9.setText(_translate("Laser", " %")) self.label_8.setText(_translate("Laser", " %")) self.pushButton.setText(_translate("Laser", "ON")) self.pushButton_2.setText(_translate("Laser", "ON")) self.pushButton_3.setText(_translate("Laser", "ON")) self.pushButton_4.setText(_translate("Laser", "ON")) self.pushButton_5.setText(_translate("Laser", "ON")) self.pushButton_6.setText(_translate("Laser", "ON")) self.pushButton_7.setText(_translate("Laser", "ON")) self.label.setText(_translate("Laser", "445nm")) self.label_2.setText(_translate("Laser", "488nm")) self.label_3.setText(_translate("Laser", "515nm")) self.label_4.setText(_translate("Laser", "561nm")) self.label_5.setText(_translate("Laser", "594nm")) self.label_6.setText(_translate("Laser", "638nm")) self.label_7.setText(_translate("Laser", "LED")) self.pushButton_8.setText(_translate("Laser", "ON")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Laser = QtWidgets.QMainWindow() ui = Ui_Laser() ui.setupUi(Laser) Laser.show() sys.exit(app.exec_()) laserEmbedded.py import random import asyncio from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference class Laser(Actor): async def handle_message(self, message: Message): print("Laser") await asyncio.sleep(2) print("Unitialised") await asyncio.sleep(2) print("Initialising") await asyncio.sleep(2) print("Initialised") await asyncio.sleep(2) print("Configuring") await asyncio.sleep(2) print("Configured") await asyncio.sleep(2) await message.sender.tell(DataMessage(data="Hello World Im a laser!" +"\n", sender=self)) async def main(): # Let's create an instance of a Greeter actor and start it. async with Laser() as laser: # Then we'll just send it an empty message and wait for a response reply : DataMessage = await ask(laser, Message()) print(reply.data) asyncio.get_event_loop().run_until_complete(main()) Regards Spencer From spencerdu at hotmail.co.uk Fri Sep 27 08:12:56 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Fri, 27 Sep 2019 05:12:56 -0700 (PDT) Subject: How to publish a message of the qspinbox value when the qspinbox sets the slider with corresponding value. Message-ID: Hi When the values of the spinboxes is set a message should be published from the GUI containing the value from the spinbox which then sets the qlabel with the value. After this if I close and reload the gui the widgets should be set with the values set from the previous time. When the embedded.py file closes/stops running the gui is reset to its default state when no spinbox and qlabel are set with values. First execute the embedded.py file. Enter 'laser' when 'devices to be activated' appears. Once this executed a publish and subscribe should happen and a laser actor python file starts which launches the laser device on the embedded.py file. Once this is complete launch GUI.py in a separate command line and click add device for the device in the combo box. Here are the files needed. embedded.py import paho.mqtt.client as mqtt from mqtt import * import os import time import json def start(): try: os.remove("list_of_devices_currently_active.txt") print("Awaiting devices to be activated") except: print("Awaiting devices to be activated") start() devices = list(map(str,input("Devices to be activated: ").split(","))) client = device() client.run() client.loop_start() print("Connected to broker") time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI") client.subscribe("microscope/light_sheet_microscope/UI") print("Publishing message to topic", "microscope/light_sheet_microscope/UI") client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": "system", "payload":{"name": devices, "cmd": "activating devices"}}, indent=2)) time.sleep(1) def active_devices(): for item in devices: device = (item + "Embedded") deviceImport = __import__(device) with open("list_of_devices_currently_active.txt", "a+") as myfile: for item in devices: myfile.write(item + "\n") active_devices() def readFile(fname): print("List of devices currently active:") try: with open(fname, "r") as f: for item in f: print(item.rstrip("\n")) except: print("No devices added yet") readFile("list_of_devices_currently_active.txt") client = device() client.run() client.loop_start() print("Connected to broker") time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI/states") client.subscribe("microscope/light_sheet_microscope/UI/states") client.loop_forever() mqtt.py import logging import paho.mqtt.client as mqtt import json class device(mqtt.Client): def on_connect(self, mqtt, obj, flags, rc): pass def on_message(self, mqtt, userdata, message): m_decode = str(message.payload.decode("utf-8")) print("message recieved= " + m_decode) # print("File which you want to import(with .py extension)") print("message topic=", message.topic) print("message qos=", message.qos) print("message retain flag=", message.retain) m_in = json.loads(m_decode) def run(self): self.connect("localhost", 1883, 60) GUI.py import paho.mqtt.client as mqtt import os import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5 import QtWidgets, uic from mqtt import * import json import time class MainWindow(QtWidgets.QMainWindow): def __init__(self,parent = None): QMainWindow.__init__(self) super(MainWindow, self).__init__(parent) self.mdi = QMdiArea() self.setCentralWidget(self.mdi) self.setMinimumSize(QSize(800, 600)) self.setWindowTitle("PyQt button example - pythonprogramminglanguage.com") client = device() client.run() client.loop_start() print("Connected to broker") time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI/devices") client.subscribe("microscope/light_sheet_microscope/UI/devices") print("Publishing message to topic", "microscope/light_sheet_microscope/UI/devices") client.publish("microscope/light_sheet_microscope/UI/devices", json.dumps({"type": "system", "payload":{"cmd": "get all devices"}}, indent=2)) time.sleep(1) pybutton = QPushButton('Add device', self) pybutton.clicked.connect(self.importbutton) pybutton.move(100, 400) pybutton.resize(150, 32) self.combo = QComboBox(self) self.combo.move(100,350) self.combo.resize(100, 32) def readFile(fname): try: with open(fname, "r") as f: for item in f: self.combo.addItem(item) except: print("No devices active") readFile("list_of_devices_currently_active.txt") def importbutton(self): client = device() client.run() client.loop_start() print("Connected to broker") time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI/add device") client.subscribe("microscope/light_sheet_microscope/UI/add device") print("Publishing message to topic", "microscope/light_sheet_microscope/UI/add device") client.publish("microscope/light_sheet_microscope/UI/add device", json.dumps({"type": "system", "payload":{"cmd": "init device panel"}}, indent=2)) time.sleep(1) client.loop_stop() self.fileName_UI = self.combo.currentText() self.loadGUI() print("Device panel initialised" + "\n") def loadGUI(self): module = __import__(self.fileName_UI.rstrip("\n")) my_class = getattr(module, "SubWindow") sub = QMdiSubWindow() sub.setWidget(my_class()) sub.setWindowTitle(self.fileName_UI) self.mdi.addSubWindow(sub) sub.show() if __name__ == "__main__": app = QApplication(sys.argv) mainWin = MainWindow() mainWin.show() # publishedMessage = mainWin.getGUIFilename() sys.exit(app.exec_()) laser.py from PyQt5 import QtCore, QtGui, QtWidgets, uic from PyQt5.QtWidgets import * from PyQt5.QtCore import * from laser2 import * from mqtt import * import json import time import sys class SubWindow(QtWidgets.QMainWindow): def __init__(self, parent = None): super(SubWindow, self).__init__(parent) self.setMinimumSize(QSize(379, 268)) self.ui = Ui_Laser() self.ui.setupUi(self) def main(): app = QtWidgets.QApplication(sys.argv) application = SubWindow() application.show() sys.exit(app.exec_()) if __name__ == "__main__": main() laser2.py from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Laser(object): def setupUi(self, Laser): Laser.setObjectName("Laser") Laser.resize(379, 274) Laser.setMinimumSize(QtCore.QSize(379, 268)) self.centralwidget = QtWidgets.QWidget(Laser) self.centralwidget.setObjectName("centralwidget") self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget) self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 70, 371, 181)) self.gridLayoutWidget.setObjectName("gridLayoutWidget") self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget) self.gridLayout.setContentsMargins(0, 0, 0, 0) self.gridLayout.setObjectName("gridLayout") self.spinBox_2 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_2.setMaximum(100) self.spinBox_2.setObjectName("spinBox_2") self.gridLayout.addWidget(self.spinBox_2, 1, 1, 1, 1) self.spinBox_7 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_7.setObjectName("spinBox_7") self.gridLayout.addWidget(self.spinBox_7, 1, 3, 1, 1) self.label_13 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_13.setObjectName("label_13") self.gridLayout.addWidget(self.label_13, 0, 5, 1, 1) self.label_11 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_11.setObjectName("label_11") self.gridLayout.addWidget(self.label_11, 0, 3, 1, 1) self.spinBox_4 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_4.setMaximum(100) self.spinBox_4.setObjectName("spinBox_4") self.gridLayout.addWidget(self.spinBox_4, 1, 4, 1, 1) self.spinBox_5 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_5.setMaximum(100) self.spinBox_5.setObjectName("spinBox_5") self.gridLayout.addWidget(self.spinBox_5, 1, 5, 1, 1) self.spinBox = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox.setMaximum(100) self.spinBox.setObjectName("spinBox") self.gridLayout.addWidget(self.spinBox, 1, 0, 1, 1) self.label_12 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_12.setObjectName("label_12") self.gridLayout.addWidget(self.label_12, 0, 4, 1, 1) self.label_14 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_14.setObjectName("label_14") self.gridLayout.addWidget(self.label_14, 0, 6, 1, 1) self.spinBox_6 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_6.setMaximum(100) self.spinBox_6.setObjectName("spinBox_6") self.gridLayout.addWidget(self.spinBox_6, 1, 6, 1, 1) self.label_10 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_10.setObjectName("label_10") self.gridLayout.addWidget(self.label_10, 0, 2, 1, 1) self.spinBox_3 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_3.setMaximum(100) self.spinBox_3.setObjectName("spinBox_3") self.gridLayout.addWidget(self.spinBox_3, 1, 2, 1, 1) self.label_9 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_9.setObjectName("label_9") self.gridLayout.addWidget(self.label_9, 0, 1, 1, 1) self.label_8 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_8.setObjectName("label_8") self.gridLayout.addWidget(self.label_8, 0, 0, 1, 1) - show quoted text - self.spinBox.valueChanged['int'].connect(self.hi) self.spinBox_2.valueChanged['int'].connect(self.label_9.setNum) self.spinBox_3.valueChanged['int'].connect(self.label_10.setNum) self.spinBox_7.valueChanged['int'].connect(self.label_11.setNum) self.spinBox_4.valueChanged['int'].connect(self.label_12.setNum) self.spinBox_5.valueChanged['int'].connect(self.label_13.setNum) self.spinBox_6.valueChanged['int'].connect(self.label_14.setNum) QtCore.QMetaObject.connectSlotsByName(Laser) def hi(self): self.spinBox.valueChanged['int'].connect(self.hi) def retranslateUi(self, Laser): _translate = QtCore.QCoreApplication.translate Laser.setWindowTitle(_translate("Laser", "MainWindow")) self.label_13.setText(_translate("Laser", " %")) self.label_11.setText(_translate("Laser", " %")) self.label_12.setText(_translate("Laser", " %")) self.label_14.setText(_translate("Laser", " %")) self.label_10.setText(_translate("Laser", " %")) self.label_9.setText(_translate("Laser", " %")) self.label_8.setText(_translate("Laser", " %")) self.pushButton.setText(_translate("Laser", "ON")) self.pushButton_2.setText(_translate("Laser", "ON")) self.pushButton_3.setText(_translate("Laser", "ON")) self.pushButton_4.setText(_translate("Laser", "ON")) self.pushButton_5.setText(_translate("Laser", "ON")) self.pushButton_6.setText(_translate("Laser", "ON")) self.pushButton_7.setText(_translate("Laser", "ON")) self.label.setText(_translate("Laser", "445nm")) self.label_2.setText(_translate("Laser", "488nm")) self.label_3.setText(_translate("Laser", "515nm")) self.label_4.setText(_translate("Laser", "561nm")) self.label_5.setText(_translate("Laser", "594nm")) self.label_6.setText(_translate("Laser", "638nm")) self.label_7.setText(_translate("Laser", "LED")) self.pushButton_8.setText(_translate("Laser", "ON")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Laser = QtWidgets.QMainWindow() ui = Ui_Laser() ui.setupUi(Laser) Laser.show() sys.exit(app.exec_()) laserEmbedded.py import random import asyncio from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference class Laser(Actor): async def handle_message(self, message: Message): print("Laser") await asyncio.sleep(2) print("Unitialised") await asyncio.sleep(2) print("Initialising") await asyncio.sleep(2) print("Initialised") await asyncio.sleep(2) print("Configuring") await asyncio.sleep(2) print("Configured") await asyncio.sleep(2) await message.sender.tell(DataMessage(data="Hello World Im a laser!" +"\n", sender=self)) async def main(): # Let's create an instance of a Greeter actor and start it. async with Laser() as laser: # Then we'll just send it an empty message and wait for a response reply : DataMessage = await ask(laser, Message()) print(reply.data) asyncio.get_event_loop().run_until_complete(main()) Regards Spencer From spencerdu at hotmail.co.uk Fri Sep 27 08:17:06 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Fri, 27 Sep 2019 05:17:06 -0700 (PDT) Subject: How to publish a message of the qspinbox value when the qspinbox sets the slider with corresponding value. Message-ID: <954c9329-73b8-4e9a-aeec-9c4afce3d788@googlegroups.com> Hi When the values of the spinboxes is set a message should be published from the GUI containing the value from the spinbox which then sets the qlabel with the value. After this if I close and reload the gui the widgets should be set with the values set from the previous time. When the embedded.py file closes/stops running the gui is reset to its default state when no spinbox and qlabel are set with values. First execute the embedded.py file. Enter 'laser' when 'devices to be activated' appears. Once this executed a publish and subscribe should happen and a laser actor python file starts which launches the laser device on the embedded.py file. Once this is complete launch GUI.py in a separate command line and click add device for the device in the combo box. Here are the files needed. embedded.py import paho.mqtt.client as mqtt from mqtt import * import os import time import json def start(): try: os.remove("list_of_devices_currently_active.txt") print("Awaiting devices to be activated") except: print("Awaiting devices to be activated") start() devices = list(map(str,input("Devices to be activated: ").split(","))) client = device() client.run() client.loop_start() print("Connected to broker") time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI") client.subscribe("microscope/light_sheet_microscope/UI") print("Publishing message to topic", "microscope/light_sheet_microscope/UI") client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": "system", "payload":{"name": devices, "cmd": "activating devices"}}, indent=2)) time.sleep(1) def active_devices(): for item in devices: device = (item + "Embedded") deviceImport = __import__(device) with open("list_of_devices_currently_active.txt", "a+") as myfile: for item in devices: myfile.write(item + "\n") active_devices() def readFile(fname): print("List of devices currently active:") try: with open(fname, "r") as f: for item in f: print(item.rstrip("\n")) except: print("No devices added yet") readFile("list_of_devices_currently_active.txt") client = device() client.run() client.loop_start() print("Connected to broker") time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI/states") client.subscribe("microscope/light_sheet_microscope/UI/states") client.loop_forever() mqtt.py import logging import paho.mqtt.client as mqtt import json class device(mqtt.Client): def on_connect(self, mqtt, obj, flags, rc): pass def on_message(self, mqtt, userdata, message): m_decode = str(message.payload.decode("utf-8")) print("message recieved= " + m_decode) # print("File which you want to import(with .py extension)") print("message topic=", message.topic) print("message qos=", message.qos) print("message retain flag=", message.retain) m_in = json.loads(m_decode) def run(self): self.connect("localhost", 1883, 60) GUI.py import paho.mqtt.client as mqtt import os import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5 import QtWidgets, uic from mqtt import * import json import time class MainWindow(QtWidgets.QMainWindow): def __init__(self,parent = None): QMainWindow.__init__(self) super(MainWindow, self).__init__(parent) self.mdi = QMdiArea() self.setCentralWidget(self.mdi) self.setMinimumSize(QSize(800, 600)) self.setWindowTitle("PyQt button example - pythonprogramminglanguage.com") client = device() client.run() client.loop_start() print("Connected to broker") time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI/devices") client.subscribe("microscope/light_sheet_microscope/UI/devices") print("Publishing message to topic", "microscope/light_sheet_microscope/UI/devices") client.publish("microscope/light_sheet_microscope/UI/devices", json.dumps({"type": "system", "payload":{"cmd": "get all devices"}}, indent=2)) time.sleep(1) pybutton = QPushButton('Add device', self) pybutton.clicked.connect(self.importbutton) pybutton.move(100, 400) pybutton.resize(150, 32) self.combo = QComboBox(self) self.combo.move(100,350) self.combo.resize(100, 32) def readFile(fname): try: with open(fname, "r") as f: for item in f: self.combo.addItem(item) except: print("No devices active") readFile("list_of_devices_currently_active.txt") def importbutton(self): client = device() client.run() client.loop_start() print("Connected to broker") time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI/add device") client.subscribe("microscope/light_sheet_microscope/UI/add device") print("Publishing message to topic", "microscope/light_sheet_microscope/UI/add device") client.publish("microscope/light_sheet_microscope/UI/add device", json.dumps({"type": "system", "payload":{"cmd": "init device panel"}}, indent=2)) time.sleep(1) client.loop_stop() self.fileName_UI = self.combo.currentText() self.loadGUI() print("Device panel initialised" + "\n") def loadGUI(self): module = __import__(self.fileName_UI.rstrip("\n")) my_class = getattr(module, "SubWindow") sub = QMdiSubWindow() sub.setWidget(my_class()) sub.setWindowTitle(self.fileName_UI) self.mdi.addSubWindow(sub) sub.show() if __name__ == "__main__": app = QApplication(sys.argv) mainWin = MainWindow() mainWin.show() # publishedMessage = mainWin.getGUIFilename() sys.exit(app.exec_()) laser.py from PyQt5 import QtCore, QtGui, QtWidgets, uic from PyQt5.QtWidgets import * from PyQt5.QtCore import * from laser2 import * from mqtt import * import json import time import sys class SubWindow(QtWidgets.QMainWindow): def __init__(self, parent = None): super(SubWindow, self).__init__(parent) self.setMinimumSize(QSize(379, 268)) self.ui = Ui_Laser() self.ui.setupUi(self) def main(): app = QtWidgets.QApplication(sys.argv) application = SubWindow() application.show() sys.exit(app.exec_()) if __name__ == "__main__": main() laser2.py from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Laser(object): def setupUi(self, Laser): Laser.setObjectName("Laser") Laser.resize(379, 274) Laser.setMinimumSize(QtCore.QSize(379, 268)) self.centralwidget = QtWidgets.QWidget(Laser) self.centralwidget.setObjectName("centralwidget") self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget) self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 70, 371, 181)) self.gridLayoutWidget.setObjectName("gridLayoutWidget") self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget) self.gridLayout.setContentsMargins(0, 0, 0, 0) self.gridLayout.setObjectName("gridLayout") self.spinBox_2 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_2.setMaximum(100) self.spinBox_2.setObjectName("spinBox_2") self.gridLayout.addWidget(self.spinBox_2, 1, 1, 1, 1) self.spinBox_7 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_7.setObjectName("spinBox_7") self.gridLayout.addWidget(self.spinBox_7, 1, 3, 1, 1) self.label_13 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_13.setObjectName("label_13") self.gridLayout.addWidget(self.label_13, 0, 5, 1, 1) self.label_11 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_11.setObjectName("label_11") self.gridLayout.addWidget(self.label_11, 0, 3, 1, 1) self.spinBox_4 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_4.setMaximum(100) self.spinBox_4.setObjectName("spinBox_4") self.gridLayout.addWidget(self.spinBox_4, 1, 4, 1, 1) self.spinBox_5 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_5.setMaximum(100) self.spinBox_5.setObjectName("spinBox_5") self.gridLayout.addWidget(self.spinBox_5, 1, 5, 1, 1) self.spinBox = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox.setMaximum(100) self.spinBox.setObjectName("spinBox") self.gridLayout.addWidget(self.spinBox, 1, 0, 1, 1) self.label_12 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_12.setObjectName("label_12") self.gridLayout.addWidget(self.label_12, 0, 4, 1, 1) self.label_14 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_14.setObjectName("label_14") self.gridLayout.addWidget(self.label_14, 0, 6, 1, 1) self.spinBox_6 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_6.setMaximum(100) self.spinBox_6.setObjectName("spinBox_6") self.gridLayout.addWidget(self.spinBox_6, 1, 6, 1, 1) self.label_10 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_10.setObjectName("label_10") self.gridLayout.addWidget(self.label_10, 0, 2, 1, 1) self.spinBox_3 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_3.setMaximum(100) self.spinBox_3.setObjectName("spinBox_3") self.gridLayout.addWidget(self.spinBox_3, 1, 2, 1, 1) self.label_9 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_9.setObjectName("label_9") self.gridLayout.addWidget(self.label_9, 0, 1, 1, 1) self.label_8 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_8.setObjectName("label_8") self.gridLayout.addWidget(self.label_8, 0, 0, 1, 1) - show quoted text - self.spinBox.valueChanged['int'].connect(self.hi) self.spinBox_2.valueChanged['int'].connect(self.label_9.setNum) self.spinBox_3.valueChanged['int'].connect(self.label_10.setNum) self.spinBox_7.valueChanged['int'].connect(self.label_11.setNum) self.spinBox_4.valueChanged['int'].connect(self.label_12.setNum) self.spinBox_5.valueChanged['int'].connect(self.label_13.setNum) self.spinBox_6.valueChanged['int'].connect(self.label_14.setNum) QtCore.QMetaObject.connectSlotsByName(Laser) def hi(self): self.spinBox.valueChanged['int'].connect(self.hi) def retranslateUi(self, Laser): _translate = QtCore.QCoreApplication.translate Laser.setWindowTitle(_translate("Laser", "MainWindow")) self.label_13.setText(_translate("Laser", " %")) self.label_11.setText(_translate("Laser", " %")) self.label_12.setText(_translate("Laser", " %")) self.label_14.setText(_translate("Laser", " %")) self.label_10.setText(_translate("Laser", " %")) self.label_9.setText(_translate("Laser", " %")) self.label_8.setText(_translate("Laser", " %")) self.pushButton.setText(_translate("Laser", "ON")) self.pushButton_2.setText(_translate("Laser", "ON")) self.pushButton_3.setText(_translate("Laser", "ON")) self.pushButton_4.setText(_translate("Laser", "ON")) self.pushButton_5.setText(_translate("Laser", "ON")) self.pushButton_6.setText(_translate("Laser", "ON")) self.pushButton_7.setText(_translate("Laser", "ON")) self.label.setText(_translate("Laser", "445nm")) self.label_2.setText(_translate("Laser", "488nm")) self.label_3.setText(_translate("Laser", "515nm")) self.label_4.setText(_translate("Laser", "561nm")) self.label_5.setText(_translate("Laser", "594nm")) self.label_6.setText(_translate("Laser", "638nm")) self.label_7.setText(_translate("Laser", "LED")) self.pushButton_8.setText(_translate("Laser", "ON")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Laser = QtWidgets.QMainWindow() ui = Ui_Laser() ui.setupUi(Laser) Laser.show() sys.exit(app.exec_()) laserEmbedded.py import random import asyncio from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference class Laser(Actor): async def handle_message(self, message: Message): print("Laser") await asyncio.sleep(2) print("Unitialised") await asyncio.sleep(2) print("Initialising") await asyncio.sleep(2) print("Initialised") await asyncio.sleep(2) print("Configuring") await asyncio.sleep(2) print("Configured") await asyncio.sleep(2) await message.sender.tell(DataMessage(data="Hello World Im a laser!" +"\n", sender=self)) async def main(): # Let's create an instance of a Greeter actor and start it. async with Laser() as laser: # Then we'll just send it an empty message and wait for a response reply : DataMessage = await ask(laser, Message()) print(reply.data) asyncio.get_event_loop().run_until_complete(main()) Regards Spencer From spencerdu at hotmail.co.uk Fri Sep 27 08:19:49 2019 From: spencerdu at hotmail.co.uk (Spencer Du) Date: Fri, 27 Sep 2019 05:19:49 -0700 (PDT) Subject: How to publish a message of the qspinbox value when the qspinbox sets the slider with corresponding value. Message-ID: Hi When the values of the spinboxes is set a message should be published from the GUI containing the value from the spinbox which then sets the qlabel with the value. After this if I close and reload the gui the widgets should be set with the values set from the previous time. When the embedded.py file closes/stops running the gui is reset to its default state when no spinbox and qlabel are set with values. First execute the embedded.py file. Enter 'laser' when 'devices to be activated' appears. Once this executed a publish and subscribe should happen and a laser actor python file starts which launches the laser device on the embedded.py file. Once this is complete launch GUI.py in a separate command line and click add device for the device in the combo box. Here are the files needed. embedded.py import paho.mqtt.client as mqtt from mqtt import * import os import time import json def start(): try: os.remove("list_of_devices_currently_active.txt") print("Awaiting devices to be activated") except: print("Awaiting devices to be activated") start() devices = list(map(str,input("Devices to be activated: ").split(","))) client = device() client.run() client.loop_start() print("Connected to broker") time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI") client.subscribe("microscope/light_sheet_microscope/UI") print("Publishing message to topic", "microscope/light_sheet_microscope/UI") client.publish("microscope/light_sheet_microscope/UI", json.dumps({"type": "system", "payload":{"name": devices, "cmd": "activating devices"}}, indent=2)) time.sleep(1) def active_devices(): for item in devices: device = (item + "Embedded") deviceImport = __import__(device) with open("list_of_devices_currently_active.txt", "a+") as myfile: for item in devices: myfile.write(item + "\n") active_devices() def readFile(fname): print("List of devices currently active:") try: with open(fname, "r") as f: for item in f: print(item.rstrip("\n")) except: print("No devices added yet") readFile("list_of_devices_currently_active.txt") client = device() client.run() client.loop_start() print("Connected to broker") time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI/states") client.subscribe("microscope/light_sheet_microscope/UI/states") client.loop_forever() mqtt.py import logging import paho.mqtt.client as mqtt import json class device(mqtt.Client): def on_connect(self, mqtt, obj, flags, rc): pass def on_message(self, mqtt, userdata, message): m_decode = str(message.payload.decode("utf-8")) print("message recieved= " + m_decode) # print("File which you want to import(with .py extension)") print("message topic=", message.topic) print("message qos=", message.qos) print("message retain flag=", message.retain) m_in = json.loads(m_decode) def run(self): self.connect("localhost", 1883, 60) GUI.py import paho.mqtt.client as mqtt import os import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5 import QtWidgets, uic from mqtt import * import json import time class MainWindow(QtWidgets.QMainWindow): def __init__(self,parent = None): QMainWindow.__init__(self) super(MainWindow, self).__init__(parent) self.mdi = QMdiArea() self.setCentralWidget(self.mdi) self.setMinimumSize(QSize(800, 600)) self.setWindowTitle("PyQt button example - pythonprogramminglanguage.com") client = device() client.run() client.loop_start() print("Connected to broker") time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI/devices") client.subscribe("microscope/light_sheet_microscope/UI/devices") print("Publishing message to topic", "microscope/light_sheet_microscope/UI/devices") client.publish("microscope/light_sheet_microscope/UI/devices", json.dumps({"type": "system", "payload":{"cmd": "get all devices"}}, indent=2)) time.sleep(1) pybutton = QPushButton('Add device', self) pybutton.clicked.connect(self.importbutton) pybutton.move(100, 400) pybutton.resize(150, 32) self.combo = QComboBox(self) self.combo.move(100,350) self.combo.resize(100, 32) def readFile(fname): try: with open(fname, "r") as f: for item in f: self.combo.addItem(item) except: print("No devices active") readFile("list_of_devices_currently_active.txt") def importbutton(self): client = device() client.run() client.loop_start() print("Connected to broker") time.sleep(1) print("Subscribing to topic", "microscope/light_sheet_microscope/UI/add device") client.subscribe("microscope/light_sheet_microscope/UI/add device") print("Publishing message to topic", "microscope/light_sheet_microscope/UI/add device") client.publish("microscope/light_sheet_microscope/UI/add device", json.dumps({"type": "system", "payload":{"cmd": "init device panel"}}, indent=2)) time.sleep(1) client.loop_stop() self.fileName_UI = self.combo.currentText() self.loadGUI() print("Device panel initialised" + "\n") def loadGUI(self): module = __import__(self.fileName_UI.rstrip("\n")) my_class = getattr(module, "SubWindow") sub = QMdiSubWindow() sub.setWidget(my_class()) sub.setWindowTitle(self.fileName_UI) self.mdi.addSubWindow(sub) sub.show() if __name__ == "__main__": app = QApplication(sys.argv) mainWin = MainWindow() mainWin.show() # publishedMessage = mainWin.getGUIFilename() sys.exit(app.exec_()) laser.py from PyQt5 import QtCore, QtGui, QtWidgets, uic from PyQt5.QtWidgets import * from PyQt5.QtCore import * from laser2 import * from mqtt import * import json import time import sys class SubWindow(QtWidgets.QMainWindow): def __init__(self, parent = None): super(SubWindow, self).__init__(parent) self.setMinimumSize(QSize(379, 268)) self.ui = Ui_Laser() self.ui.setupUi(self) def main(): app = QtWidgets.QApplication(sys.argv) application = SubWindow() application.show() sys.exit(app.exec_()) if __name__ == "__main__": main() laser2.py from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Laser(object): def setupUi(self, Laser): Laser.setObjectName("Laser") Laser.resize(379, 274) Laser.setMinimumSize(QtCore.QSize(379, 268)) self.centralwidget = QtWidgets.QWidget(Laser) self.centralwidget.setObjectName("centralwidget") self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget) self.gridLayoutWidget.setGeometry(QtCore.QRect(0, 70, 371, 181)) self.gridLayoutWidget.setObjectName("gridLayoutWidget") self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget) self.gridLayout.setContentsMargins(0, 0, 0, 0) self.gridLayout.setObjectName("gridLayout") self.spinBox_2 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_2.setMaximum(100) self.spinBox_2.setObjectName("spinBox_2") self.gridLayout.addWidget(self.spinBox_2, 1, 1, 1, 1) self.spinBox_7 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_7.setObjectName("spinBox_7") self.gridLayout.addWidget(self.spinBox_7, 1, 3, 1, 1) self.label_13 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_13.setObjectName("label_13") self.gridLayout.addWidget(self.label_13, 0, 5, 1, 1) self.label_11 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_11.setObjectName("label_11") self.gridLayout.addWidget(self.label_11, 0, 3, 1, 1) self.spinBox_4 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_4.setMaximum(100) self.spinBox_4.setObjectName("spinBox_4") self.gridLayout.addWidget(self.spinBox_4, 1, 4, 1, 1) self.spinBox_5 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_5.setMaximum(100) self.spinBox_5.setObjectName("spinBox_5") self.gridLayout.addWidget(self.spinBox_5, 1, 5, 1, 1) self.spinBox = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox.setMaximum(100) self.spinBox.setObjectName("spinBox") self.gridLayout.addWidget(self.spinBox, 1, 0, 1, 1) self.label_12 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_12.setObjectName("label_12") self.gridLayout.addWidget(self.label_12, 0, 4, 1, 1) self.label_14 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_14.setObjectName("label_14") self.gridLayout.addWidget(self.label_14, 0, 6, 1, 1) self.spinBox_6 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_6.setMaximum(100) self.spinBox_6.setObjectName("spinBox_6") self.gridLayout.addWidget(self.spinBox_6, 1, 6, 1, 1) self.label_10 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_10.setObjectName("label_10") self.gridLayout.addWidget(self.label_10, 0, 2, 1, 1) self.spinBox_3 = QtWidgets.QSpinBox(self.gridLayoutWidget) self.spinBox_3.setMaximum(100) self.spinBox_3.setObjectName("spinBox_3") self.gridLayout.addWidget(self.spinBox_3, 1, 2, 1, 1) self.label_9 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_9.setObjectName("label_9") self.gridLayout.addWidget(self.label_9, 0, 1, 1, 1) self.label_8 = QtWidgets.QLabel(self.gridLayoutWidget) self.label_8.setObjectName("label_8") self.gridLayout.addWidget(self.label_8, 0, 0, 1, 1) - show quoted text - self.spinBox.valueChanged['int'].connect(self.hi) self.spinBox_2.valueChanged['int'].connect(self.label_9.setNum) self.spinBox_3.valueChanged['int'].connect(self.label_10.setNum) self.spinBox_7.valueChanged['int'].connect(self.label_11.setNum) self.spinBox_4.valueChanged['int'].connect(self.label_12.setNum) self.spinBox_5.valueChanged['int'].connect(self.label_13.setNum) self.spinBox_6.valueChanged['int'].connect(self.label_14.setNum) QtCore.QMetaObject.connectSlotsByName(Laser) def hi(self): self.spinBox.valueChanged['int'].connect(self.hi) def retranslateUi(self, Laser): _translate = QtCore.QCoreApplication.translate Laser.setWindowTitle(_translate("Laser", "MainWindow")) self.label_13.setText(_translate("Laser", " %")) self.label_11.setText(_translate("Laser", " %")) self.label_12.setText(_translate("Laser", " %")) self.label_14.setText(_translate("Laser", " %")) self.label_10.setText(_translate("Laser", " %")) self.label_9.setText(_translate("Laser", " %")) self.label_8.setText(_translate("Laser", " %")) self.pushButton.setText(_translate("Laser", "ON")) self.pushButton_2.setText(_translate("Laser", "ON")) self.pushButton_3.setText(_translate("Laser", "ON")) self.pushButton_4.setText(_translate("Laser", "ON")) self.pushButton_5.setText(_translate("Laser", "ON")) self.pushButton_6.setText(_translate("Laser", "ON")) self.pushButton_7.setText(_translate("Laser", "ON")) self.label.setText(_translate("Laser", "445nm")) self.label_2.setText(_translate("Laser", "488nm")) self.label_3.setText(_translate("Laser", "515nm")) self.label_4.setText(_translate("Laser", "561nm")) self.label_5.setText(_translate("Laser", "594nm")) self.label_6.setText(_translate("Laser", "638nm")) self.label_7.setText(_translate("Laser", "LED")) self.pushButton_8.setText(_translate("Laser", "ON")) if __name__ == "__main__": import sys app = QtWidgets.QApplication(sys.argv) Laser = QtWidgets.QMainWindow() ui = Ui_Laser() ui.setupUi(Laser) Laser.show() sys.exit(app.exec_()) laserEmbedded.py import random import asyncio from actorio import Actor, Message, DataMessage, ask, EndMainLoop, Reference class Laser(Actor): async def handle_message(self, message: Message): print("Laser") await asyncio.sleep(2) print("Unitialised") await asyncio.sleep(2) print("Initialising") await asyncio.sleep(2) print("Initialised") await asyncio.sleep(2) print("Configuring") await asyncio.sleep(2) print("Configured") await asyncio.sleep(2) await message.sender.tell(DataMessage(data="Hello World Im a laser!" +"\n", sender=self)) async def main(): # Let's create an instance of a Greeter actor and start it. async with Laser() as laser: # Then we'll just send it an empty message and wait for a response reply : DataMessage = await ask(laser, Message()) print(reply.data) asyncio.get_event_loop().run_until_complete(main()) Regards Spencer From rhodri at kynesim.co.uk Fri Sep 27 08:29:40 2019 From: rhodri at kynesim.co.uk (Rhodri James) Date: Fri, 27 Sep 2019 13:29:40 +0100 Subject: Recursive method in class In-Reply-To: <5d8df86e$0$3370$426a74cc@news.free.fr> References: <5d8df86e$0$3370$426a74cc@news.free.fr> Message-ID: On 27/09/2019 12:54, ast wrote: > Hello > > Is it feasible to define a recursive method in a class ? > (I don't need it, it's just a trial) > > Here are failing codes: > > > class Test: > ??? def fib(self, n): > ??????? if n < 2: return n > ??????? return fib(self, n-2) + fib(self, n-1) Try self.fib(...) instead of fib(self, ...) both times. -- Rhodri James *-* Kynesim Ltd From balglaas at dds.nl Fri Sep 27 08:26:57 2019 From: balglaas at dds.nl (Jan van den Broek) Date: Fri, 27 Sep 2019 12:26:57 +0000 (UTC) Subject: Recursive method in class References: <5d8df86e$0$3370$426a74cc@news.free.fr> Message-ID: On 2019-09-27, ast wrote: > Is it feasible to define a recursive method in a class ? > (I don't need it, it's just a trial) > > Here are failing codes: > > > class Test: > def fib(self, n): > if n < 2: return n > return fib(self, n-2) + fib(self, n-1) self.fib(...) [Schnipp] -- Jan v/d Broek balglaas at dds.nl From l.mohanphy at gmail.com Fri Sep 27 08:41:37 2019 From: l.mohanphy at gmail.com (Mohan L) Date: Fri, 27 Sep 2019 20:41:37 +0800 Subject: Help to understand the data structure Message-ID: Hi All, I am using get_devices_list method from this module: https://github.com/Tufin/pytos/blob/master/pytos/securetrack/helpers.py. Here is my two like code and output looks like: https://pastebin.com/K9KBeqYL I am not able to manage to further loop the data to print below output: devicename1 10 devicename2 11 devicename3 12 I spend quit some time still not able to figure out how to parse. Can some one through some light on how to phrase it. -- Thanks & Regards Mohan L From specially at processed.almost.meat Fri Sep 27 08:43:18 2019 From: specially at processed.almost.meat (Roy Hann) Date: Fri, 27 Sep 2019 12:43:18 +0000 (UTC) Subject: Generate simple image on a standalone Raspberrry Pi Message-ID: I am designing a mobile application to run on a Raspberry Pi 3 model B. It will not have any Internet access. I need to generate a static image consisting of a simple arc representing (say) a speedometer or a pressure gauge. The image will need to be regenerated every 5 seconds. The image must be displayed in a web browser (served by gunicorn running on the Pi). I prefer it to be a PNG but that is only a preference. Plotly is amazing but big and slow. Pygal looked acceptable but Internet access is required to render the SVG. Has anyone any suggestion for a lightweight solution? Roy From tjol at tjol.eu Fri Sep 27 08:51:56 2019 From: tjol at tjol.eu (Thomas Jollans) Date: Fri, 27 Sep 2019 14:51:56 +0200 Subject: Generate simple image on a standalone Raspberrry Pi In-Reply-To: References: Message-ID: <989f5ace-ba9a-c549-d869-b32c4db62616@tjol.eu> On 27/09/2019 14.43, Roy Hann wrote: > I am designing a mobile application to run on a Raspberry Pi 3 model B. > It will not have any Internet access. I need to generate a static image > consisting of a simple arc representing (say) a speedometer or a > pressure gauge. The image will need to be regenerated every 5 seconds. > The image must be displayed in a web browser (served by gunicorn > running on the Pi). I prefer it to be a PNG but that is only a > preference. > > Plotly is amazing but big and slow. Pygal looked acceptable but > Internet access is required to render the SVG. There's always matplotlib. Depending on how simple it needs to be, pillow might do the trick. > > Has anyone any suggestion for a lightweight solution? > > Roy From l.mohanphy at gmail.com Fri Sep 27 10:09:34 2019 From: l.mohanphy at gmail.com (Mohan L) Date: Fri, 27 Sep 2019 22:09:34 +0800 Subject: Help to understand the data structure In-Reply-To: References: Message-ID: Hi All, Please ignore it, I was able to figure out it. for dev in devlist: print (dev.name, dev.id) -- Thanks & Regards Mohan L On Fri, Sep 27, 2019 at 8:41 PM Mohan L wrote: > Hi All, > > I am using get_devices_list method from this module: > https://github.com/Tufin/pytos/blob/master/pytos/securetrack/helpers.py. > Here is my two like code and output looks like: > https://pastebin.com/K9KBeqYL > > > I am not able to manage to further loop the data to print below output: > > > devicename1 10 > > devicename2 11 > > devicename3 12 > > > I spend quit some time still not able to figure out how to parse. Can some > one through some light on how to phrase it. > > > -- > Thanks & Regards > Mohan L > > > From bluebox03 at gmail.com Fri Sep 27 10:14:25 2019 From: bluebox03 at gmail.com (tommy yama) Date: Fri, 27 Sep 2019 23:14:25 +0900 Subject: Help Wanted for the error Message-ID: Hi , Has anyone seen similar errors previously? I just encountered the error below and need to fix asap. File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/google/protobuf/descriptor_pool.py", line 175, in _CheckConflictRegister raise TypeError(error_msg) TypeError: Conflict register for file "shared/mempool_status.proto": mempool.Valid is already defined in file "mempool_status.proto". Please fix the conflict by adding package name on the proto file, or use different name for the duplication. Thanks a lot From 2QdxY4RzWzUUiLuE at potatochowder.com Fri Sep 27 08:07:47 2019 From: 2QdxY4RzWzUUiLuE at potatochowder.com (Dan Sommers) Date: Fri, 27 Sep 2019 08:07:47 -0400 Subject: Recursive method in class In-Reply-To: <5d8df86e$0$3370$426a74cc@news.free.fr> References: <5d8df86e$0$3370$426a74cc@news.free.fr> Message-ID: <382eed1a-0283-ee2b-5b6d-f29f01f91839@potatochowder.com> On 9/27/19 7:54 AM, ast wrote: > Hello > > Is it feasible to define a recursive method in a class ? > (I don't need it, it's just a trial) > > Here are failing codes: > > > class Test: > def fib(self, n): > if n < 2: return n > return fib(self, n-2) + fib(self, n-1) return self.fib(n - 2) + self.fib(n - 1) (1) This is a horribly ineffective way to compute Fibonacci numbers, but that's not immediately relevant. (2) Recursive instance methods only make sense when there's some sort of state being maintained, which in this case there is not. But that's also not immediately relevant. From smilesonisamal at gmail.com Fri Sep 27 11:32:05 2019 From: smilesonisamal at gmail.com (Pradeep Patra) Date: Fri, 27 Sep 2019 21:02:05 +0530 Subject: itertools query Message-ID: Hi all, I have written a small program to generate all the combinations of a and b of the array. I want (6,7) tuple also included. Can anybody suggest what change I should make to get 6,7 included in my output? Any suggestions Output: [(5,), (6,), (5, 6), (7,), (8,), (7, 8)] from itertools import chain, combinations a = [5,6] b = [7,8] ar=[] br=[] def all_subsets(ss): return chain(*map(lambda x: combinations(ss, x), range(1, len(ss)+1))) for subset in all_subsets(a): print(subset) ar.append(subset) for subset in all_subsets(b): print(subset) br.append(subset) fr=ar+br print(fr) From __peter__ at web.de Fri Sep 27 11:47:47 2019 From: __peter__ at web.de (Peter Otten) Date: Fri, 27 Sep 2019 17:47:47 +0200 Subject: itertools query References: Message-ID: Pradeep Patra wrote: > Hi all, > > I have written a small program to generate all the combinations of a and b > of the array. I want (6,7) tuple also included. Can anybody suggest what > change I should make to get 6,7 included in my output? Any suggestions The spec is not clear to me. If you don't care about the source list you can use list(all_subsets(a + b)) but that will include many more combinations, including those consisting of three or four elements. > Output: > [(5,), (6,), (5, 6), (7,), (8,), (7, 8)] > > from itertools import chain, combinations > > a = [5,6] > b = [7,8] > ar=[] > br=[] > > def all_subsets(ss): > return chain(*map(lambda x: combinations(ss, x), range(1, len(ss)+1))) > > for subset in all_subsets(a): > print(subset) > ar.append(subset) > > for subset in all_subsets(b): > print(subset) > br.append(subset) > > fr=ar+br > print(fr) From pkpearson at nowhere.invalid Fri Sep 27 12:36:55 2019 From: pkpearson at nowhere.invalid (Peter Pearson) Date: 27 Sep 2019 16:36:55 GMT Subject: Angular distribution rose diagram in Python References: <249f6e94-3c61-46cd-852d-acca38c323c2@googlegroups.com> Message-ID: On Fri, 27 Sep 2019 02:13:31 -0700 (PDT), Madhavan Bomidi wrote: > > Can someone help me to make python code (with some arbitrary data) for > the angular distribution rose diagram as shown in figure 7 in the > paper accessible through the web-link: > > https://www.nat-hazards-earth-syst-sci.net/17/1425/2017/nhess-17-1425-2017.pdf Googling "matplotlib rose diagram" (without the quotation marks) produced some promising results. This one even has sample code: http://geologyandpython.com/structural_geology.html I'm no graphics expert, but Matplotlib generally does the job for me. -- To email me, substitute nowhere->runbox, invalid->com. From rob at despammer.com Fri Sep 27 14:46:58 2019 From: rob at despammer.com (RobH) Date: Fri, 27 Sep 2019 19:46:58 +0100 Subject: NEWBIE: how to get text onto 2 lines on a 16x2 lcd display In-Reply-To: References: <35ad4830-b8f8-e503-2273-a6249c5bf1c1@DancesWithMice.info> <92ppoe5pbb4kp4jle55559olho46hkv9g0@4ax.com> <260roe5rd196qs4hsk3o6uv65itr9t10ul@4ax.com> Message-ID: On 27/09/2019 15:28, Dennis Lee Bieber wrote: > On Fri, 27 Sep 2019 10:48:29 +0100, RobH declaimed the > following: > >> Ok, the adafruit_character_lcd is in the same directory as yours, and so >> is Blinka and Purio. It seems to be a bit of a long path to type to get >> to the Adafruit_Charlcd directory, but is there a shortcut way of >> getting to the said directory. >> > > Other than to examine the source code of the library you have no need > to specify the directory... Within a Python script, a plain "import" should > work. > > pi at raspberrypi:~$ python3 > Python 3.7.3 (default, Apr 3 2019, 05:39:12) > [GCC 8.2.0] on linux > Type "help", "copyright", "credits" or "license" for more information. >>>> import adafruit_blinka >>>> import adafruit_character_lcd as acl >>>> dir(acl) > ['__doc__', '__file__', '__loader__', '__name__', '__package__', > '__path__', '__spec__'] >>>> help(acl) > > > Help on package adafruit_character_lcd: > > NAME > adafruit_character_lcd > > PACKAGE CONTENTS > character_lcd > character_lcd_i2c > character_lcd_rgb_i2c > character_lcd_spi > > FILE > (built-in) > > (END) >>>> from adafruit_character_lcd import character_lcd as cl >>>> help(cl) > Help on module adafruit_character_lcd.character_lcd in > adafruit_character_lcd: > > NAME > adafruit_character_lcd.character_lcd > > DESCRIPTION > `adafruit_character_lcd.character_lcd` > ==================================================== > > Module for interfacing with monochromatic character LCDs > > * Author(s): Kattni Rembor, Brent Rubell, Asher Lieber, > Tony DiCola (original python charLCD library) > > Implementation Notes > -------------------- > > **Hardware:** > > "* `Adafruit Character LCDs `_" > > **Software and Dependencies:** > > * Adafruit CircuitPython firmware: > https://github.com/adafruit/circuitpython/releases > * Adafruit's Bus Device library (when using I2C/SPI): > https://github.com/adafruit/Adafruit_CircuitPython_BusDevice > > CLASSES > {AND MUCH MORE} > > Thanks for all that information, but first of all using just import adafruit blinka did not work as it returned bas: import: command not found. I should say that this is on a raspberry pi zero w, if it makes any difference. From * at eli.users.panix.com Fri Sep 27 16:32:39 2019 From: * at eli.users.panix.com (Eli the Bearded) Date: Fri, 27 Sep 2019 20:32:39 +0000 (UTC) Subject: Generate simple image on a standalone Raspberrry Pi References: Message-ID: In comp.lang.python, Roy Hann wrote: > I am designing a mobile application to run on a Raspberry Pi 3 model B. > It will not have any Internet access. I need to generate a static image > consisting of a simple arc representing (say) a speedometer or a > pressure gauge. The image will need to be regenerated every 5 seconds. > The image must be displayed in a web browser (served by gunicorn > running on the Pi). I prefer it to be a PNG but that is only a > preference. The browser can probably display SVG. So generating that may be easier. Is the browser running on the same system? You say no "Internet access", but that's not the same as "no network access". A browser running on a different local system might have more CPU oomph to handle the "SVG to bitmap for display" step. I've used libpng from C and chunky_png from ruby and not found PNG generation too onerous. That makes me think pypng wouldn't be bad either. For the case of a speedometer / pressure gauge you can likely have a blank gauge file you read in each time through the loop and then only write the changes needed (eg, the needle). Alternatively the guage could be a static image in the browser with an overlaid dynamic needle image using a transparent background. The PNG format will handle that easily. Elijah ------ thinks there are a lot of unstated parts to this problem From piet-l at vanoostrum.org Fri Sep 27 17:06:23 2019 From: piet-l at vanoostrum.org (Piet van Oostrum) Date: Fri, 27 Sep 2019 23:06:23 +0200 Subject: itertools query References: Message-ID: Pradeep Patra writes: > Hi all, > > I have written a small program to generate all the combinations of a and b > of the array. I want (6,7) tuple also included. Can anybody suggest what > change I should make to get 6,7 included in my output? Any suggestions > Why (6,7)? What about (5,7), (5,8) and (6,8)? -- Piet van Oostrum WWW: http://piet.vanoostrum.org/ PGP key: [8DAE142BE17999C4] From gisle.vanem at gmail.com Fri Sep 27 18:41:27 2019 From: gisle.vanem at gmail.com (Gisle Vanem) Date: Sat, 28 Sep 2019 00:41:27 +0200 Subject: PIP question In-Reply-To: <875zleco1b.fsf@handshake.de> References: <2ac90a03-7a5d-f17b-dc23-4270d22928e2@gmail.com> <875zleco1b.fsf@handshake.de> Message-ID: <62639385-eb53-2865-1fb5-465f6d5c957b@gmail.com> dieter wrote: >> directory with some possible leftovers. It there a connection >> between this mysterious '-ip' package and this directory? > > This is possible. A so called "distribution" can install > packages of a different name (for example, the distribution "Zope" > installs (among others) a package "ZPublisher"). In addition, the > actual installation typically has lost metadata information (e.g. > the version number). Therefore, "pip" may use "*.dist-info" or > "*.egg-info" directories to provide this metadata. Look into those > directories to find out which packages are installed for the > corresponding distribution. You can then check whether those packages > are truely available (and if not delete the *-info* directory). Thanks for this info. After deleting the f:\programfiler\python36\lib\site-packages\~ip-19.1.1.dist-info directory and running my script again, the "-ip" is gone. All this *.dist-info stuff is alien stuff to me, but I guess 'pip install --upgrade' uses these? BTW, there is no command 'pip uninstall --orphans'. Any other tool that does the same? -- --gv From smilesonisamal at gmail.com Fri Sep 27 22:59:22 2019 From: smilesonisamal at gmail.com (Pradeep Patra) Date: Sat, 28 Sep 2019 08:29:22 +0530 Subject: itertools query In-Reply-To: References: Message-ID: I don't need other combination except 6,7 On Saturday, September 28, 2019, Piet van Oostrum wrote: > Pradeep Patra writes: > > > Hi all, > > > > I have written a small program to generate all the combinations of a and > b > > of the array. I want (6,7) tuple also included. Can anybody suggest what > > change I should make to get 6,7 included in my output? Any suggestions > > > > Why (6,7)? What about (5,7), (5,8) and (6,8)? > -- > Piet van Oostrum > WWW: http://piet.vanoostrum.org/ > PGP key: [8DAE142BE17999C4] > -- > https://mail.python.org/mailman/listinfo/python-list > From dieter at handshake.de Sat Sep 28 01:02:50 2019 From: dieter at handshake.de (dieter) Date: Sat, 28 Sep 2019 07:02:50 +0200 Subject: PIP question References: <2ac90a03-7a5d-f17b-dc23-4270d22928e2@gmail.com> <875zleco1b.fsf@handshake.de> <62639385-eb53-2865-1fb5-465f6d5c957b@gmail.com> Message-ID: <877e5t57yd.fsf@handshake.de> Gisle Vanem writes: > ... > All this *.dist-info stuff is alien stuff to me, but I guess > 'pip install --upgrade' uses these? Most of "pip" is using this. They contain important meta information (e.g. version number, dependencies, overview information, ...) about a distribution not directly used by Python. From __peter__ at web.de Sat Sep 28 02:35:31 2019 From: __peter__ at web.de (Peter Otten) Date: Sat, 28 Sep 2019 08:35:31 +0200 Subject: itertools query References: Message-ID: > Pradeep Patra wrote: > My idea is to include the last element of array a and first element of second array b in the final array. fr.append((a[-1], b[0])) From hongyi.zhao at gmail.com Sat Sep 28 04:55:13 2019 From: hongyi.zhao at gmail.com (Hongyi Zhao) Date: Sat, 28 Sep 2019 08:55:13 +0000 (UTC) Subject: ypeError: decoding str is not supported Message-ID: Hi, I have some code comes from python 2 like the following: str('a', encoding='utf-8') But for python 3, this will fail as follows: >>> str('a', encoding='utf-8') Traceback (most recent call last): File "", line 1, in TypeError: decoding str is not supported How to fix it? From __peter__ at web.de Sat Sep 28 05:50:39 2019 From: __peter__ at web.de (Peter Otten) Date: Sat, 28 Sep 2019 11:50:39 +0200 Subject: ypeError: decoding str is not supported References: Message-ID: Hongyi Zhao wrote: > Hi, > > I have some code comes from python 2 like the following: > > str('a', encoding='utf-8') This fails in Python 2 >>> str("a", encoding="utf-8") Traceback (most recent call last): File "", line 1, in TypeError: str() takes at most 1 argument (2 given) ...unless you have redefined str, e. g. with >>> str = unicode >>> str("a", encoding="utf-8") u'a' > But for python 3, this will fail as follows: > >>>> str('a', encoding='utf-8') > Traceback (most recent call last): > File "", line 1, in > TypeError: decoding str is not supported > > > How to fix it? Don' try to decode an already decoded string; use it directly: "a" From p.f.moore at gmail.com Sat Sep 28 06:47:46 2019 From: p.f.moore at gmail.com (Paul Moore) Date: Sat, 28 Sep 2019 11:47:46 +0100 Subject: ypeError: decoding str is not supported In-Reply-To: References: Message-ID: On Sat, 28 Sep 2019 at 10:53, Peter Otten <__peter__ at web.de> wrote: > > Hongyi Zhao wrote: > > > Hi, > > > > I have some code comes from python 2 like the following: > > > > str('a', encoding='utf-8') > > This fails in Python 2 > > >>> str("a", encoding="utf-8") > Traceback (most recent call last): > File "", line 1, in > TypeError: str() takes at most 1 argument (2 given) > > ...unless you have redefined str, e. g. with > > >>> str = unicode > >>> str("a", encoding="utf-8") > u'a' > > > But for python 3, this will fail as follows: > > > >>>> str('a', encoding='utf-8') > > Traceback (most recent call last): > > File "", line 1, in > > TypeError: decoding str is not supported > > > > > > How to fix it? > > Don' try to decode an already decoded string; use it directly: > > "a" To explain a little further, one of the biggest differences between Python 2 and Python 3 is that you *have* to be clear in Python 3 on which data is encoded byte sequences (which need a decode to turn them into text strings, but cannot be encoded, because they already are) and which are text strings (which don't need to be, and can't be, decoded, but which can be encoded if you want to get a byte sequence). If you're not clear whether some data is a byte string or a text string, you will get in a muddle, and Python 2 won't help you (but it will sometimes produce mojibake without generating an error) whereas Python 3 will tend to throw errors flagging the issue (but it may sometimes be stricter than you are used to). Thinking that saying `str = unicode` is a reasonable thing to do is a pretty strong indication that you're not clear on whether your data is text or bytes - either that or you're hoping to make a "quick fix". But as you've found, quick fixes tend to result in a cascade of further issues that *also* need quick fixes. The right solution here (and by far the cleanest one) is to review your code as a whole, and have a clear separation between bytes data and text data. The usual approach people use for this is to decode bytes into text as soon as it's read into your program, and only ever use genuine text data within your program - so you should only ever be using encode/decode in the I/O portion of your application, where it's pretty clear when you have encoded bytes coming in or going out. Hope this helps, Paul From rob at despammer.com Sat Sep 28 15:17:33 2019 From: rob at despammer.com (RobH) Date: Sat, 28 Sep 2019 20:17:33 +0100 Subject: NEWBIE: how to get text onto 2 lines on a 16x2 lcd display In-Reply-To: References: <92ppoe5pbb4kp4jle55559olho46hkv9g0@4ax.com> <260roe5rd196qs4hsk3o6uv65itr9t10ul@4ax.com> <5sruoepigt1d8m6uq0i23a7gf83qrbn8hi@4ax.com> Message-ID: On 28/09/2019 15:59, Dennis Lee Bieber wrote: > On Fri, 27 Sep 2019 19:46:58 +0100, RobH declaimed the > following: > >> >> Thanks for all that information, but first of all using just import >> adafruit blinka did not work as it returned bas: import: command not found. >> > > Were you in the Python3 interpreter? That error (I presume it was > "bash:...") means you typed the line at the console shell, not into a from: can't read /var/mail/Python3from shell console > Python interpreter. > > pi at raspberrypi:~$ #from shell console > pi at raspberrypi:~$ import adafruit_blinka > -bash: import: command not found > pi at raspberrypi:~$ #from Python3 interactive interpreter > pi at raspberrypi:~$ python3 > Python 3.7.3 (default, Apr 3 2019, 05:39:12) > [GCC 8.2.0] on linux > Type "help", "copyright", "credits" or "license" for more information. >>>> import adafruit_blinka >>>> exit() > pi at raspberrypi:~$ > > >> I should say that this is on a raspberry pi zero w, if it makes any >> difference. > > It doesn't... All R-Pi system use a default OS of Raspbian (a Debian > modified for R-Pi specific booting and support) {caveat: if using a NOOBS > installer with active WiFi one does have the option of installing any one > of half a dozen OSes, though some are quite specific in usage: Windows IoT, > media player...} > > It doesn't even matter if one is using a BeagleBone Black, since that > also runs Debian (though R-Pi has updated to "Buster" while the BBB is > still on "Stretch" [ver 10 vs ver 9]). > > debian at beaglebone:~$ #from shell console > debian at beaglebone:~$ import adafruit_blinka > -bash: import: command not found > debian at beaglebone:~$ #from Python3 interactive interpreter > debian at beaglebone:~$ python3 > Python 3.5.3 (default, Sep 27 2018, 17:25:39) > [GCC 6.3.0 20170516] on linux from: can't read /var/mail/Python3from shell console > Type "help", "copyright", "credits" or "license" for more information. >>>> import adafruit_blinka >>>> exit() > debian at beaglebone:~$ > > It does matter if you try from a system that doesn't support embedded > programming -- Debian 10 running in a VirtualBox instance on a Window10 > computer... > > Actually -- it did do something > > wulfraed at debian:~$ import adafruit_blinka > wulfraed at debian:~$ > wulfraed at debian:~$ ls -l > total 11704 > -rwxrwx--- 1 wulfraed vboxsf 11909039 Sep 28 10:48 adafruit_blinka > drwxrwxrwx 2 wulfraed wulfraed 4096 Sep 13 15:56 Desktop > drwxrwxrwx 3 wulfraed wulfraed 4096 Sep 13 14:20 Documents > drwxrwxrwx 2 wulfraed wulfraed 4096 Sep 13 13:53 Downloads > drwxr-xr-x 3 wulfraed wulfraed 4096 Sep 13 15:18 eclipse > drwxr-xr-x 4 wulfraed wulfraed 4096 Sep 13 15:25 eclipse-workspace > drwxrwxrwx 5 wulfraed wulfraed 4096 Sep 11 11:33 ga > drwxrwxrwx 7 wulfraed wulfraed 4096 Sep 11 13:58 Mail > drwxrwxrwx 2 wulfraed wulfraed 4096 Sep 11 10:43 Music > drwxrwxrwx 2 wulfraed wulfraed 4096 Sep 11 10:43 Pictures > drwxrwxrwx 2 wulfraed wulfraed 4096 Sep 11 10:43 Public > drwxrwxrwx 3 wulfraed wulfraed 4096 Sep 17 14:33 Scratch > drwxrwxrwx 3 wulfraed wulfraed 4096 Sep 13 15:15 temp > drwxrwxrwx 2 wulfraed wulfraed 4096 Sep 11 10:43 Templates > -rwxrwxrwx 1 wulfraed wulfraed 8984 Sep 13 13:49 testcross > -rwxrwxrwx 1 wulfraed wulfraed 124 Sep 13 13:32 testcross.cpp > drwxrwxrwx 2 wulfraed wulfraed 4096 Sep 11 10:43 Videos > > It created a large file that, on examination, is an EPS image file of my > Debian desktop! "import" appears to be a command supplied as part of > "ImageMagick" conversion utility. > > After removing the file, the Python3 interpreter reports > > wulfraed at debian:~$ python3 > Python 3.7.3 (default, Apr 3 2019, 05:39:12) > [GCC 8.3.0] on linux > Type "help", "copyright", "credits" or "license" for more information. >>>> import adafruit_blinka > Traceback (most recent call last): > File "", line 1, in > ModuleNotFoundError: No module named 'adafruit_blinka' >>>> exit() > wulfraed at debian:~$ > > as expected, since the AdaFruit libraries are not installed on a desktop > machine. > > > > > > > > No I wasn't in the Python3 interpreter, and typing Python3 gets me into, I assume. Then typing: import adafruit_blinka seemed to work as there were no errors. Thanks From ekopalypse at gmail.com Sat Sep 28 15:46:55 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Sat, 28 Sep 2019 12:46:55 -0700 (PDT) Subject: Synchronous and Asynchronous callbacks Message-ID: Hello, I'm looking for a solution to a problem which is described best as an observer pattern issue I guess. The scenario is the following, client code can register a callback for one or a list of notifications. This is currently solved by having a dictionary with the possible notifications as the keys and lists of methods being the values. Notification sent, triggers the list of method to be called. Nothing fancy. Now, where it gets trickier is that the client should be able to register synchronous and asynchronous callbacks because some of the actions can't be done in a synchronous callback like updating the statusbar of the UI as the main app would overwrite it. I've read that this isn't a good idea of having synchronous and asynchronous callbacks but to be honest, I don't see how this could be solved otherwise, hence the question what to do in such a case? And as I currently think that there is no other solution then to provide sync and async callback registration possibility how would a solution look like? Two dicts, one for sync and the other for async registrations? That would mean that each notification handler needs to execute one dict after the other. In an ideal world, the notification handler wouldn't know about the difference at all and would call its list of methods(objects?) Any tips for me? Just to be sure as I don't know whether this is relevant, each notification handler provides a different set of arguments to the registered callback functions, hence the multiple notification handlers. Thank you Eren From anthony.flury at btinternet.com Sun Sep 29 05:51:37 2019 From: anthony.flury at btinternet.com (Anthony Flury) Date: Sun, 29 Sep 2019 10:51:37 +0100 Subject: Interesting performance question Message-ID: <68cd7d62-f1da-4c49-3b5c-d8a1395182cd@btinternet.com> I have just noticed an oddity : Using python 3.6 building a tuple like this : my_tuple = tuple([x*x for x in range(1,1000)]) is about 1/3 quicker than ??? my_tuple = tuple(x*x for x in range(1,1000)) Measurements : $? python3 -m timeit 'my_tuple = tuple([x*x for x in range(1,1000)])' 10000 loops, best of 3: 33.5 usec per loop $? python3 -m timeit 'my_tuple = tuple(x*x for x in range(1,1000))' 10000 loops, best of 3: 44.1 usec per loop My first assumption was that on the first example (with the list comprehension) the tuple constructor is able allocate the right amount of memory immediately, and therefore doesn't have the cost overhead of having to resize compared to the version having to resize every so often as new items are fetched from the generator. One question though - wouldn't the list generator have a similar resizing issue, since the list comprehension wont predict the final size of the list ahead of time. Is there a good explanation of the performance difference - I don't think that the memory resizing overhead is the whole answer. -- Tony Flury From ekopalypse at gmail.com Sun Sep 29 09:14:51 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Sun, 29 Sep 2019 06:14:51 -0700 (PDT) Subject: Synchronous and Asynchronous callbacks In-Reply-To: <874l0w10tv.fsf@nightsong.com> References: <874l0w10tv.fsf@nightsong.com> Message-ID: Am Sonntag, 29. September 2019 01:02:48 UTC+2 schrieb Paul Rubin: > Eko palypse writes: > > Two dicts, one for sync and the other for async registrations? > > Why not make all the callbacks asynchronous? Clients that don't want to > handle a callback immediately can keep it pending until they are ready, > perhaps in a queue. > > Whether this can work of course depends on how the application is put > together, but the pattern of a bunch of asynchronous tasks communicating > through fifos tends to work pretty well. I'm used to doing it with > threads, though those can be expensive if you use a lot of them. I > don't know whether async/await unavoidably complicates things. Thank you very much for your answer. Unfortunately, I can't make all callbacks synchronous or asynchronous because this has negative effects on the application in one way or another. I also came across the asyncio methods yesterday, Whoa, what can I say, I haven't really understood yet. My first impression is that either the whole library works asynchronously or not. Still too many Bohemian villages (not sure if this is also common in English - it should say there are too many things I don't understand). But as I said, thank you very much for the time and your thoughts you have given about my problem, very much appreciated. Eren From eryksun at gmail.com Sun Sep 29 09:45:55 2019 From: eryksun at gmail.com (Eryk Sun) Date: Sun, 29 Sep 2019 08:45:55 -0500 Subject: Interesting performance question In-Reply-To: <68cd7d62-f1da-4c49-3b5c-d8a1395182cd@btinternet.com> References: <68cd7d62-f1da-4c49-3b5c-d8a1395182cd@btinternet.com> Message-ID: On 9/29/19, Anthony Flury via Python-list wrote: > > Using python 3.6 building a tuple like this : > > my_tuple = tuple([x*x for x in range(1,1000)]) The list comprehension is implemented internally as a function that builds and returns the list. This function creates an empty list and loops over the range iterator to evaluate the expression and append the result. Appending to a list uses an over-allocation strategy to efficiently grow the list. Finally, the list is passed to the tuple constructor, which can efficiently and quickly create a tuple from the list because it's simply copying a PyObject * array in C. > my_tuple = tuple(x*x for x in range(1,1000)) In this case a generator is created instead of a list. This is passed to the tuple constructor, which iterates the generator. There's no __length_hint__() for a generator, so it starts with a length 10 tuple. The tuple grows with an over-allocation rule, and at the end it's resized to the actual length. I expect the generator-based expression to be a bit more expensive. Iterating a generator requires resuming evaluation of the code object up to a yield, which suspends evaluation. For the list comprehension, the loop that builds the list executes continuously, without an interruption to yield a value in each pass. From specially at processed.almost.meat Sun Sep 29 12:39:26 2019 From: specially at processed.almost.meat (Roy Hann) Date: Sun, 29 Sep 2019 16:39:26 +0000 (UTC) Subject: Generate simple image on a standalone Raspberrry Pi References: Message-ID: Eli the Bearded wrote: > In comp.lang.python, Roy Hann wrote: >> I am designing a mobile application to run on a Raspberry Pi 3 model B. >> It will not have any Internet access. I need to generate a static image >> consisting of a simple arc representing (say) a speedometer or a >> pressure gauge. The image will need to be regenerated every 5 seconds. >> The image must be displayed in a web browser (served by gunicorn >> running on the Pi). I prefer it to be a PNG but that is only a >> preference. > > The browser can probably display SVG. So generating that may be easier. > Is the browser running on the same system? You say no "Internet access", > but that's not the same as "no network access". You are correct to this point; the Pi is serving HTML to the experimental browser on a Kindle 4 over wifi. > A browser running on a > different local system might have more CPU oomph to handle the "SVG to > bitmap for display" step. Sadly the Kindle, impressive as it is, ain't no workhorse. That said, I've only assumed it would be more efficient to hand it PNGs to render. I should actually test it rendering SVGs. I set out down the PNG road on the assumption PNGs would be easy to generate. Thanks for steering me back to SVG. Roy From barry at barrys-emacs.org Sun Sep 29 12:49:17 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Sun, 29 Sep 2019 17:49:17 +0100 Subject: Synchronous and Asynchronous callbacks In-Reply-To: References: <874l0w10tv.fsf@nightsong.com> Message-ID: <48A7EC4D-D15E-45AB-87FF-0574EE8FB632@barrys-emacs.org> > On 29 Sep 2019, at 14:14, Eko palypse wrote: > > Unfortunately, I can't make all callbacks synchronous or asynchronous because this has negative effects on the application in one way or another. Surely you can make all callbacks async? You do not have to have them wait, they can complete their work in one call. sync == async-that-does-not-await Barry From tjreedy at udel.edu Sun Sep 29 14:42:44 2019 From: tjreedy at udel.edu (Terry Reedy) Date: Sun, 29 Sep 2019 14:42:44 -0400 Subject: Interesting performance question In-Reply-To: References: <68cd7d62-f1da-4c49-3b5c-d8a1395182cd@btinternet.com> Message-ID: On 9/29/2019 9:45 AM, Eryk Sun wrote: > On 9/29/19, Anthony Flury via Python-list wrote: >> >> Using python 3.6 building a tuple like this : >> >> my_tuple = tuple([x*x for x in range(1,1000)]) > > The list comprehension is implemented internally as a function that > builds and returns the list. This function creates an empty list and > loops over the range iterator to evaluate the expression and append > the result. Appending to a list uses an over-allocation strategy to > efficiently grow the list. The internal function can use, and I believe, does use >>> iter(range(1,1000)).__length_hint__() 999 to directly allocate space for at least 999 items and skip growing from an empty list. > Finally, the list is passed to the tuple > constructor, which can efficiently and quickly create a tuple from the > list because it's simply copying a PyObject * array in C. > >> my_tuple = tuple(x*x for x in range(1,1000)) > > In this case a generator is created instead of a list. This is passed > to the tuple constructor, which iterates the generator. There's no > __length_hint__() for a generator, so it starts with a length 10 > tuple. The tuple grows with an over-allocation rule, and at the end > it's resized to the actual length. > > I expect the generator-based expression to be a bit more expensive. > Iterating a generator requires resuming evaluation of the code object > up to a yield, which suspends evaluation. For the list comprehension, > the loop that builds the list executes continuously, without an > interruption to yield a value in each pass. > -- Terry Jan Reedy From ekopalypse at gmail.com Sun Sep 29 16:41:58 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Sun, 29 Sep 2019 13:41:58 -0700 (PDT) Subject: Synchronous and Asynchronous callbacks In-Reply-To: References: <874l0w10tv.fsf@nightsong.com> <48A7EC4D-D15E-45AB-87FF-0574EE8FB632@barrys-emacs.org> Message-ID: <226fc893-a461-4f47-9f2b-a6d723be58a0@googlegroups.com> Am Sonntag, 29. September 2019 19:18:32 UTC+2 schrieb Barry Scott: > > On 29 Sep 2019, at 14:14, Eko palypse wrote: > > > > Unfortunately, I can't make all callbacks synchronous or asynchronous because this has negative effects on the application in one way or another. > > Surely you can make all callbacks async? > You do not have to have them wait, they can complete their work in one call. > > sync == async-that-does-not-await > > Barry Thank you for your answer but I'm sorry I don't get it. What do you mean by I don't have to have them wait? Let me explain my understanding, with some pseudo code, how async callbacks works and please keep in mind that I'm a novice when it comes to OO designs and basic software design strategies/techniques, so I might be totally wrong about what I'm doing. An SYNCHRONOUS callback is like this. Event/notification gets fired and notification handler calls all registered methods one after the other. Something like if notification.list_of_methods: for method in list_of_methods: method() whereas an ASYNCHRONOUS callback is more like this. Event/notification gets fired and notification handler informs another thread that the event has been fired and all registered async callback methods should get executed. So something like this class EventThread(threading.Thread): def __init__(...): super()... self.event = threading.Event() self.kill = False ... def run(self): while True: self.event.wait() self.event.clear() if not self.kill: for method in self.list_of_methods: method() et = EventThread() if notification.list_of_methods: et.event.set() # run all async methods for method in list_of_methods: # run sync methods method() So if there is no synchronous callback for that notification then the notification handler would just set the event and return. The EventThread would then call one registered callback after the other. Right? Using this approach does sound fine from UI point of view as there is minimal impact and UI keeps responsive but from code execution point of view it sound much more complicated. What if one async callback is slow or buggy? This could lead to unpredictable behavior where as in a synchronous execution you immediately know what is causing it. That was the reason I decided not to offer async callbacks until I discovered that other parts, like UI modifications aren't working properly. And there are notification which must be called in a synchronous way, like the modification event. If you want to intercept a modification safely then it can only be done within a synchronous callback. Does this makes sense to you? Or do I have wrong understanding or, again, an wrong assumption how this works? Thank you Eren From PythonList at DancesWithMice.info Mon Sep 30 00:40:38 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Mon, 30 Sep 2019 17:40:38 +1300 Subject: pathlib Message-ID: <699641c7-b16a-a239-e760-068a6a5b41d2@etelligence.info> Should pathlib reflect changes it has made to the file-system? Sample code, below, shows pathlib identifying a data-file and then renaming it. Yet, after the rename operation, pathlib doesn't recognise its own change; whereas the file system does/proves the change was actioned. $ touch before.file $ python3 Python 3.7.4 (default, Jul 9 2019, 16:48:28) [GCC 8.3.1 20190223 (Red Hat 8.3.1-2)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import pathlib >>> p = pathlib.Path( "before.file" ) >>> p PosixPath('before.file') >>> p.name 'before.file' >>> p.rename( "after.file" ) >>> p.name 'before.file' >>> exit() $ ls -lah after* -rw-rw-r--. 1 dn dn 0 Sep 30 16:05 after.file $ ls -lah before* ls: cannot access 'before*': No such file or directory I would expect pathlib to keep track of changes it has made. Any ideas/good reasons why/why not? NB "name" is a r/o attribute. -- Regards, =dn From none at gmail.com Mon Sep 30 03:53:18 2019 From: none at gmail.com (ast) Date: Mon, 30 Sep 2019 09:53:18 +0200 Subject: Recursive method in class In-Reply-To: References: <5d8df86e$0$3370$426a74cc@news.free.fr> Message-ID: <5d91b46f$0$15197$426a74cc@news.free.fr> Le 27/09/2019 ? 14:26, Jan van den Broek a ?crit?: > On 2019-09-27, ast wrote: >> Is it feasible to define a recursive method in a class ? >> (I don't need it, it's just a trial) >> >> Here are failing codes: >> >> >> class Test: >> def fib(self, n): >> if n < 2: return n >> return fib(self, n-2) + fib(self, n-1) > self.fib(...) > > [Schnipp] > Yes you are right. I forgot that class methods don't see class attributes An other workaround: def fib(self, n): if n < 2: return n return Test.fib(self, n-2) + Test.fib(self, n-1) It comes from https://www.pythonsheets.com/notes/python-object.html >>> def fib(self, n): ... if n <= 2: ... return 1 ... return fib(self, n-1) + fib(self, n-2) ... >>> Fib = type('Fib', (object,), {'val': 10, ... 'fib': fib}) >>> f = Fib() >>> f.val 10 >>> f.fib(f.val) 55 So it means that when classes are constructed explicitely with type(name, bases, dict_), some methods are transformed From barry at barrys-emacs.org Mon Sep 30 04:28:54 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Mon, 30 Sep 2019 09:28:54 +0100 Subject: pathlib In-Reply-To: <699641c7-b16a-a239-e760-068a6a5b41d2@etelligence.info> References: <699641c7-b16a-a239-e760-068a6a5b41d2@etelligence.info> Message-ID: <25BFF716-8D88-4533-AE47-80725D2A0294@barrys-emacs.org> > On 30 Sep 2019, at 05:40, DL Neil via Python-list wrote: > > Should pathlib reflect changes it has made to the file-system? I think it should not. A Path() is the name of a file it is not the file itself. Why should it track changes in the file system for the name? Here is code to show why following the rename will break logic: save_file = pathlib.Path('my-data.txt') def save( data ): # backup file if save_file.exists(): save_file.rename('my-data.backup') # save data with save_file.open() as f: f.write( data ) while True: save( generate_data() ) time.sleep( interval ) If the rename of the file changed the path the above code will fail. Barry > > > Sample code, below, shows pathlib identifying a data-file and then renaming it. Yet, after the rename operation, pathlib doesn't recognise its own change; whereas the file system does/proves the change was actioned. > > > $ touch before.file > $ python3 > Python 3.7.4 (default, Jul 9 2019, 16:48:28) > [GCC 8.3.1 20190223 (Red Hat 8.3.1-2)] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> import pathlib > >>> p = pathlib.Path( "before.file" ) > >>> p > PosixPath('before.file') > >>> p.name > 'before.file' > >>> p.rename( "after.file" ) > >>> p.name > 'before.file' > >>> exit() > $ ls -lah after* > -rw-rw-r--. 1 dn dn 0 Sep 30 16:05 after.file > $ ls -lah before* > ls: cannot access 'before*': No such file or directory > > > I would expect pathlib to keep track of changes it has made. Any ideas/good reasons why/why not? > NB "name" is a r/o attribute. > -- > Regards, > =dn > -- > https://mail.python.org/mailman/listinfo/python-list > From __peter__ at web.de Mon Sep 30 04:55:24 2019 From: __peter__ at web.de (Peter Otten) Date: Mon, 30 Sep 2019 10:55:24 +0200 Subject: pathlib References: <699641c7-b16a-a239-e760-068a6a5b41d2@etelligence.info> Message-ID: DL Neil via Python-list wrote: > Should pathlib reflect changes it has made to the file-system? > > > Sample code, below, shows pathlib identifying a data-file and then > renaming it. Yet, after the rename operation, pathlib doesn't recognise > its own change; whereas the file system does/proves the change was > actioned. > > > $ touch before.file > $ python3 > Python 3.7.4 (default, Jul 9 2019, 16:48:28) > [GCC 8.3.1 20190223 (Red Hat 8.3.1-2)] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> import pathlib > >>> p = pathlib.Path( "before.file" ) > >>> p > PosixPath('before.file') > >>> p.name > 'before.file' > >>> p.rename( "after.file" ) > >>> p.name > 'before.file' > >>> exit() > $ ls -lah after* > -rw-rw-r--. 1 dn dn 0 Sep 30 16:05 after.file > $ ls -lah before* > ls: cannot access 'before*': No such file or directory > > > I would expect pathlib to keep track of changes it has made. Any > ideas/good reasons why/why not? > NB "name" is a r/o attribute. This is certainly not a backwards-compatible change. Consider: p = pathlib.Path("data.txt") p.rename("data.txt.bak") with p.open("w") as f: f.write("new stuff") # oops, we are overwriting the backup Also, it will not necessarily be as obvious as in the above example. Everywhere you write p = q you produce an occasion to unexpectedly change a file reference. Generally, it's easier to reason about immutable objects. What might be helpful would be to return a new Path object renamed = original.rename(...) but as I'm not a pathlib user I have no idea what the use-it to throw-it- away ratio would be. From barry at barrys-emacs.org Mon Sep 30 05:09:06 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Mon, 30 Sep 2019 10:09:06 +0100 Subject: Synchronous and Asynchronous callbacks In-Reply-To: <226fc893-a461-4f47-9f2b-a6d723be58a0@googlegroups.com> References: <874l0w10tv.fsf@nightsong.com> <48A7EC4D-D15E-45AB-87FF-0574EE8FB632@barrys-emacs.org> <226fc893-a461-4f47-9f2b-a6d723be58a0@googlegroups.com> Message-ID: <5030FB6B-D089-4606-A979-45D313BCBF15@barrys-emacs.org> > On 29 Sep 2019, at 21:41, Eko palypse wrote: > > Am Sonntag, 29. September 2019 19:18:32 UTC+2 schrieb Barry Scott: >>> On 29 Sep 2019, at 14:14, Eko palypse wrote: >>> >>> Unfortunately, I can't make all callbacks synchronous or asynchronous because this has negative effects on the application in one way or another. >> >> Surely you can make all callbacks async? >> You do not have to have them wait, they can complete their work in one call. >> >> sync == async-that-does-not-await >> >> Barry > > Thank you for your answer but I'm sorry I don't get it. > What do you mean by I don't have to have them wait? > > Let me explain my understanding, with some pseudo code, how async callbacks > works and please keep in mind that I'm a novice when it comes to OO designs > and basic software design strategies/techniques, so I might be totally > wrong about what I'm doing. When you said async I thought you meant the specific programming model in Python that uses "async def", "await" etc. You are using threads. The problem you are facing is that the UI must not become unresponsive. Further a background thread cannot call any UI framework functions as the UI framework can only be called on the main thread. You can run fast event handers on the UI thread but slow running handlers must be run on another thread to allow the UI to be responsive. What I do is have the event handler for a long running task take the responsibility to queue the task onto a background thread. While the task is running it sends status to the UI thread so that UI elements are updated (progress bars, etc). If you are using PyQt5 you might find the code I used for SCM Workbench interesting. I can write event handlers that look like this: @thread_switcher def eventHandler( self, ... ): # starts in the foreground (UI thread) self.status.update('Working...') yield self.switchToBackground # now on the background thread self.doSlowTask() yield self.switchToForeground # now in the foreground self.status.update('Done') The code is in: https://github.com/barry-scott/scm-workbench/blob/master/Source/Common/wb_background_thread.py @thread_switcher adds an attribute to the function so the rest of the code knows this is an event handler that needs to use a background thread. When I add an event handler I call through a function (wrapWithThreadSwitcher) that wraps only functions that have been marked with @thread_switcher. The rest of the code is the machinery to handle moving between threads via the generator. Barry > > An SYNCHRONOUS callback is like this. Event/notification gets fired and > notification handler calls all registered methods one after the other. > Something like > > if notification.list_of_methods: > for method in list_of_methods: > method() > > whereas an ASYNCHRONOUS callback is more like this. Event/notification gets > fired and notification handler informs another thread that the event has > been fired and all registered async callback methods should get executed. > So something like this > > class EventThread(threading.Thread): > def __init__(...): > super()... > self.event = threading.Event() > self.kill = False > ... > def run(self): > while True: > self.event.wait() > self.event.clear() > if not self.kill: > for method in self.list_of_methods: > method() > > et = EventThread() > if notification.list_of_methods: > et.event.set() # run all async methods > for method in list_of_methods: # run sync methods > method()) > > > So if there is no synchronous callback for that notification then the > notification handler would just set the event and return. > The EventThread would then call one registered callback after the other. > Right? > > Using this approach does sound fine from UI point of view as there is > minimal impact and UI keeps responsive but from code execution point of view > it sound much more complicated. What if one async callback is slow or buggy? > This could lead to unpredictable behavior where as in a synchronous execution you immediately know what is causing it. > That was the reason I decided not to offer async callbacks until I > discovered that other parts, like UI modifications aren't working properly. > And there are notification which must be called in a synchronous way, like > the modification event. If you want to intercept a modification safely then it can only be done within a synchronous callback. > > Does this makes sense to you? Or do I have wrong understanding or, again, an > wrong assumption how this works? > > Thank you > Eren > -- > https://mail.python.org/mailman/listinfo/python-list > From barry at barrys-emacs.org Mon Sep 30 05:19:19 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Mon, 30 Sep 2019 10:19:19 +0100 Subject: pathlib In-Reply-To: References: <699641c7-b16a-a239-e760-068a6a5b41d2@etelligence.info> Message-ID: > On 30 Sep 2019, at 09:55, Peter Otten <__peter__ at web.de> wrote: > > DL Neil via Python-list wrote: > >> Should pathlib reflect changes it has made to the file-system? >> >> >> Sample code, below, shows pathlib identifying a data-file and then >> renaming it. Yet, after the rename operation, pathlib doesn't recognise >> its own change; whereas the file system does/proves the change was >> actioned. >> >> >> $ touch before.file >> $ python3 >> Python 3.7.4 (default, Jul 9 2019, 16:48:28) >> [GCC 8.3.1 20190223 (Red Hat 8.3.1-2)] on linux >> Type "help", "copyright", "credits" or "license" for more information. >>>>> import pathlib >>>>> p = pathlib.Path( "before.file" ) >>>>> p >> PosixPath('before.file') >>>>> p.name >> 'before.file' >>>>> p.rename( "after.file" ) >>>>> p.name >> 'before.file' >>>>> exit() >> $ ls -lah after* >> -rw-rw-r--. 1 dn dn 0 Sep 30 16:05 after.file >> $ ls -lah before* >> ls: cannot access 'before*': No such file or directory >> >> >> I would expect pathlib to keep track of changes it has made. Any >> ideas/good reasons why/why not? >> NB "name" is a r/o attribute. > > This is certainly not a backwards-compatible change. Consider: > > p = pathlib.Path("data.txt") > p.rename("data.txt.bak") > with p.open("w") as f: > f.write("new stuff") # oops, we are overwriting the backup > > Also, it will not necessarily be as obvious as in the above example. > Everywhere you write > > p = q > > you produce an occasion to unexpectedly change a file reference. Generally, > it's easier to reason about immutable objects. > > What might be helpful would be to return a new Path object > > renamed = original.rename(...) I do not think this is useful as in my code I will already have a path object to use as the rename target. src = pathlib.Path('/source/data.txt') dst = pathlib.Path('/backup/data.bak') src.rename( dst ) print( 'Renamed %s to %s' % (src, dst) ) > > but as I'm not a pathlib user I have no idea what the use-it to throw-it- > away ratio would be. Barry > > -- > https://mail.python.org/mailman/listinfo/python-list From ekopalypse at gmail.com Mon Sep 30 07:00:27 2019 From: ekopalypse at gmail.com (Eko palypse) Date: Mon, 30 Sep 2019 04:00:27 -0700 (PDT) Subject: Synchronous and Asynchronous callbacks In-Reply-To: References: <874l0w10tv.fsf@nightsong.com> <48A7EC4D-D15E-45AB-87FF-0574EE8FB632@barrys-emacs.org> <226fc893-a461-4f47-9f2b-a6d723be58a0@googlegroups.com> <5030FB6B-D089-4606-A979-45D313BCBF15@barrys-emacs.org> Message-ID: Am Montag, 30. September 2019 11:46:43 UTC+2 schrieb Barry Scott: > > On 29 Sep 2019, at 21:41, Eko palypse wrote: > > > > Am Sonntag, 29. September 2019 19:18:32 UTC+2 schrieb Barry Scott: > >>> On 29 Sep 2019, at 14:14, Eko palypse wrote: > >>> > >>> Unfortunately, I can't make all callbacks synchronous or asynchronous because this has negative effects on the application in one way or another. > >> > >> Surely you can make all callbacks async? > >> You do not have to have them wait, they can complete their work in one call. > >> > >> sync == async-that-does-not-await > >> > >> Barry > > > > Thank you for your answer but I'm sorry I don't get it. > > What do you mean by I don't have to have them wait? > > > > Let me explain my understanding, with some pseudo code, how async callbacks > > works and please keep in mind that I'm a novice when it comes to OO designs > > and basic software design strategies/techniques, so I might be totally > > wrong about what I'm doing. > > When you said async I thought you meant the specific programming model in Python > that uses "async def", "await" etc. You are using threads. > > The problem you are facing is that the UI must not become unresponsive. > Further a background thread cannot call any UI framework functions as the UI > framework can only be called on the main thread. > > You can run fast event handers on the UI thread but slow running handlers > must be run on another thread to allow the UI to be responsive. > > What I do is have the event handler for a long running task take the responsibility > to queue the task onto a background thread. While the task is running it sends status > to the UI thread so that UI elements are updated (progress bars, etc). > > If you are using PyQt5 you might find the code I used for SCM Workbench interesting. > > I can write event handlers that look like this: > > @thread_switcher > def eventHandler( self, ... ): > # starts in the foreground (UI thread) > self.status.update('Working...') > > yield self.switchToBackground > # now on the background thread > self.doSlowTask() > > yield self.switchToForeground > # now in the foreground > self.status.update('Done') > > The code is in: > > https://github.com/barry-scott/scm-workbench/blob/master/Source/Common/wb_background_thread.py > > @thread_switcher adds an attribute to the function so the rest of the > code knows this is an event handler that needs to use a background thread. > > When I add an event handler I call through a function (wrapWithThreadSwitcher) > that wraps only functions that have been marked with @thread_switcher. > > The rest of the code is the machinery to handle moving between threads via the generator. > > Barry > > > > > An SYNCHRONOUS callback is like this. Event/notification gets fired and > > notification handler calls all registered methods one after the other. > > Something like > > > > if notification.list_of_methods: > > for method in list_of_methods: > > method() > > > > whereas an ASYNCHRONOUS callback is more like this. Event/notification gets > > fired and notification handler informs another thread that the event has > > been fired and all registered async callback methods should get executed. > > So something like this > > > > class EventThread(threading.Thread): > > def __init__(...): > > super()... > > self.event = threading.Event() > > self.kill = False > > ... > > def run(self): > > while True: > > self.event.wait() > > self.event.clear() > > if not self.kill: > > for method in self.list_of_methods: > > method() > > > > et = EventThread() > > if notification.list_of_methods: > > et.event.set() # run all async methods > > for method in list_of_methods: # run sync methods > > method()) > > > > > > So if there is no synchronous callback for that notification then the > > notification handler would just set the event and return. > > The EventThread would then call one registered callback after the other. > > Right? > > > > Using this approach does sound fine from UI point of view as there is > > minimal impact and UI keeps responsive but from code execution point of view > > it sound much more complicated. What if one async callback is slow or buggy? > > This could lead to unpredictable behavior where as in a synchronous execution you immediately know what is causing it. > > That was the reason I decided not to offer async callbacks until I > > discovered that other parts, like UI modifications aren't working properly. > > And there are notification which must be called in a synchronous way, like > > the modification event. If you want to intercept a modification safely then it can only be done within a synchronous callback. > > > > Does this makes sense to you? Or do I have wrong understanding or, again, an > > wrong assumption how this works? > > > > Thank you > > Eren > > -- > > https://mail.python.org/mailman/listinfo/python-list > > Thank you very much, this sounds like what I'm looking for and I'm sure I find tons of other useful information/techniques which I'm not aware of yet. Thank you Eren From 2QdxY4RzWzUUiLuE at potatochowder.com Mon Sep 30 07:51:29 2019 From: 2QdxY4RzWzUUiLuE at potatochowder.com (Dan Sommers) Date: Mon, 30 Sep 2019 07:51:29 -0400 Subject: pathlib In-Reply-To: <25BFF716-8D88-4533-AE47-80725D2A0294@barrys-emacs.org> References: <699641c7-b16a-a239-e760-068a6a5b41d2@etelligence.info> <25BFF716-8D88-4533-AE47-80725D2A0294@barrys-emacs.org> Message-ID: <4233b002-ec5a-b00f-41d4-76cde37a77b9@potatochowder.com> On 9/30/19 4:28 AM, Barry Scott wrote: > > >> On 30 Sep 2019, at 05:40, DL Neil via Python-list wrote: >> >> Should pathlib reflect changes it has made to the file-system? > > I think it should not. > > A Path() is the name of a file it is not the file itself. Why should it > track changes in the file system for the name? I would have said the same thing, but the docs? disagree: a PurePath represents the name of (or the path to) a file, but a Path represents the actual file. That said, why doesn't your argument apply to read and write? I would certainly expect that writing to a path and then reading from that same path would return the newly written data. If I squint funny, the Path object is tracking the operations on the file system. I think I'm actually arguing against some long since made (and forgotten?) design decisions that can't be changed (dare I say fixed?) because changing them would break backwards compatibility. Yuck. :-) And I can absolutely see all sorts of different expecations not being met and having to be explained by saying "well, that's the way it works." ? https://docs.python.org/3/library/pathlib.html From rosuav at gmail.com Mon Sep 30 08:09:36 2019 From: rosuav at gmail.com (Chris Angelico) Date: Mon, 30 Sep 2019 22:09:36 +1000 Subject: pathlib In-Reply-To: <4233b002-ec5a-b00f-41d4-76cde37a77b9@potatochowder.com> References: <699641c7-b16a-a239-e760-068a6a5b41d2@etelligence.info> <25BFF716-8D88-4533-AE47-80725D2A0294@barrys-emacs.org> <4233b002-ec5a-b00f-41d4-76cde37a77b9@potatochowder.com> Message-ID: On Mon, Sep 30, 2019 at 9:54 PM Dan Sommers <2QdxY4RzWzUUiLuE at potatochowder.com> wrote: > I would have said the same thing, but the docs? disagree: a > PurePath represents the name of (or the path to) a file, but a > Path represents the actual file. > > > ? https://docs.python.org/3/library/pathlib.html I don't think it represents the actual file. If it did, equality would be defined by samefile, NOT by the file name. >>> from pathlib import Path >>> import os >>> open("file1", "w").close() >>> os.link("file1", "file2") >>> Path("file1") == Path("file2") False >>> Path("file1").samefile(Path("file2")) True >>> Path("file1") == Path("file1") True It still represents the path to the file, not the file itself, and if you move something over it, it will see the new file. ChrisA From barry at barrys-emacs.org Mon Sep 30 08:12:44 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Mon, 30 Sep 2019 13:12:44 +0100 Subject: Announcing config-path an OS independent configuration file paths Message-ID: <8733DEC4-9479-4496-A1B3-22D1C95944BF@barrys-emacs.org> See https://pypi.org/project/config-path/ for documentation. Install using pip: python -m pip install config-path I write tools that run on macOS, Unix and Windows systems. Each operating system has its own conventions on where config files should be stored. Following the conventions is not always straight forward. After a question raised here on Python users about config file locations I was inspired to write this module to help with that problem. config_path library works out which path to use for configuration folders and files in an operating system independent way. Each operating system has particular conventions for where an application is expected to stores it configuration. The information provided to ConfigPath is used to figure out an appropriate file path or folder path for the application's configuration data. Supports Windows, macOS and most unix systems using the 'XDG Base Directory Specification'. Example for the "widget" app from "example.com" that uses JSON for its config: from config_path import ConfigPath conf_path = ConfigPath( 'example.com', 'widget', '.json' ) path = conf_path.saveFilePath( mkdir=True ) with path.open() as f: f.write( config_data ) And to read the config: path = conf_path.readFilePath() if path is not None: # path exists and config can be read with path.open() as f: config = json.loads( f ) else: config = default_config Barry From barry at barrys-emacs.org Mon Sep 30 07:59:19 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Mon, 30 Sep 2019 12:59:19 +0100 Subject: Announcing colour-text and colour-print Message-ID: <7460D86A-CEAE-4C3B-AAE1-955F81299FF5@barrys-emacs.org> See https://pypi.org/project/colour-text/ for documentation with colourful examples. Install using pip: python -m pip install colour-text Many command line tools now colour there output to make it easier to pick out important details. I want to do the same for my commands that I tend to write in Python, bash and Windows CMD. The solution needs to work for Unix, macOS and Windows. Embedding escape sequences I found to be too low level. And the existing python libraries a bit too verbose. My solution is to use a simple markup for adding colour to text. The markup delimited defaults to "<>". Names of colours include semantic names like, info, error and em. Here is an example: from colour_text import ColourText ct = ColourText() ct.initTerminal() print( ct( "The next section is in green: <>green example<>." ) ) I also want to be able to add colour output to shell scripts and Windows CMD scripts. When pip installs colour-text it also installs the colour-print command for use from scripts. For example: $ colour-print "<>info Info:<> this is an <>em informational<> message" $ colour-print "<>error Error: This is an error message<>" And you can also pass in arguments in a printf or python % style, only %s is supported. $ colour-print "<>info Info:<> Home folder is %s" "$HOME" Barry From __peter__ at web.de Mon Sep 30 08:36:43 2019 From: __peter__ at web.de (Peter Otten) Date: Mon, 30 Sep 2019 14:36:43 +0200 Subject: Announcing colour-text and colour-print References: <7460D86A-CEAE-4C3B-AAE1-955F81299FF5@barrys-emacs.org> Message-ID: Barry Scott wrote: > See https://pypi.org/project/colour-text/ for documentation > with colourful examples. > > Install using pip: > > python -m pip install colour-text Have you considered to spell that color-text? In programming the guys who can't spell won ;) From barry at barrys-emacs.org Mon Sep 30 08:40:15 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Mon, 30 Sep 2019 13:40:15 +0100 Subject: pathlib In-Reply-To: <4233b002-ec5a-b00f-41d4-76cde37a77b9@potatochowder.com> References: <699641c7-b16a-a239-e760-068a6a5b41d2@etelligence.info> <25BFF716-8D88-4533-AE47-80725D2A0294@barrys-emacs.org> <4233b002-ec5a-b00f-41d4-76cde37a77b9@potatochowder.com> Message-ID: <2AE351E4-E23D-44C5-AC82-8741DA63ABA9@barrys-emacs.org> > On 30 Sep 2019, at 12:51, Dan Sommers <2QdxY4RzWzUUiLuE at potatochowder.com> wrote: > > On 9/30/19 4:28 AM, Barry Scott wrote: >>> On 30 Sep 2019, at 05:40, DL Neil via Python-list wrote: >>> Should pathlib reflect changes it has made to the file-system? >> I think it should not. >> A Path() is the name of a file it is not the file itself. Why should it >> track changes in the file system for the name? > > I would have said the same thing, but the docs? disagree: a > PurePath represents the name of (or the path to) a file, but a > Path represents the actual file. I'm not seeing that wording in the python 3.7 pathlib documentation. Can you quote the exact wording please? I do see this: "Pure path objects provide path-handling operations which don?t actually access a filesystem." And: "Concrete paths are subclasses of the pure path classes. In addition to operations provided by the latter, they also provide methods to do system calls on path objects." There is no requirement that a Path() names a file that exists even. > That said, why doesn't your argument apply to read and write? I > would certainly expect that writing to a path and then reading > from that same path would return the newly written data. If I > squint funny, the Path object is tracking the operations on the > file system. I do not expect that. Consider the time line: 1. with p.open('w') write data 2. external process changes file on disk 3. with p.open('r') read data How would (3) get the data written at (1) guaranteed? It will lead to bugs to assume that. The path object is allowing system calls that need a file's path to be called, that is all. Beyond that there is no relationship between the pathlib.Path() objects and files. > > I think I'm actually arguing against some long since made (and > forgotten?) design decisions that can't be changed (dare I say > fixed?) because changing them would break backwards > compatibility. > > Yuck. :-) And I can absolutely see all sorts of different > expecations not being met and having to be explained by saying > "well, that's the way it works." I'd suggest that the design is reasonable and If there is misunderstanding that its something that docs could address. > > ? https://docs.python.org/3/library/pathlib.html > -- > https://mail.python.org/mailman/listinfo/python-list Barry From p.f.moore at gmail.com Mon Sep 30 09:17:57 2019 From: p.f.moore at gmail.com (Paul Moore) Date: Mon, 30 Sep 2019 14:17:57 +0100 Subject: Announcing config-path an OS independent configuration file paths In-Reply-To: <8733DEC4-9479-4496-A1B3-22D1C95944BF@barrys-emacs.org> References: <8733DEC4-9479-4496-A1B3-22D1C95944BF@barrys-emacs.org> Message-ID: How does this compare to the existing appdirs module on PyPI? On Mon, 30 Sep 2019 at 13:15, Barry Scott wrote: > > See https://pypi.org/project/config-path/ for documentation. > > Install using pip: > > python -m pip install config-path > > I write tools that run on macOS, Unix and Windows systems. > Each operating system has its own conventions on where config > files should be stored. Following the conventions is not always > straight forward. > > After a question raised here on Python users about config file locations > I was inspired to write this module to help with that problem. > > config_path library works out which path to use for configuration folders > and files in an operating system independent way. > > Each operating system has particular conventions for where an application > is expected to stores it configuration. The information provided to ConfigPath > is used to figure out an appropriate file path or folder path for the application's > configuration data. > > Supports Windows, macOS and most unix systems using the 'XDG Base Directory Specification'. > > Example for the "widget" app from "example.com" that uses JSON for its config: > > from config_path import ConfigPath > conf_path = ConfigPath( 'example.com', 'widget', '.json' ) > > path = conf_path.saveFilePath( mkdir=True ) > with path.open() as f: > f.write( config_data ) > > And to read the config: > > path = conf_path.readFilePath() > if path is not None: > # path exists and config can be read > with path.open() as f: > config = json.loads( f ) > else: > config = default_config > > Barry > > > -- > https://mail.python.org/mailman/listinfo/python-list From 2QdxY4RzWzUUiLuE at potatochowder.com Mon Sep 30 10:21:42 2019 From: 2QdxY4RzWzUUiLuE at potatochowder.com (Dan Sommers) Date: Mon, 30 Sep 2019 10:21:42 -0400 Subject: pathlib In-Reply-To: <2AE351E4-E23D-44C5-AC82-8741DA63ABA9@barrys-emacs.org> References: <699641c7-b16a-a239-e760-068a6a5b41d2@etelligence.info> <25BFF716-8D88-4533-AE47-80725D2A0294@barrys-emacs.org> <4233b002-ec5a-b00f-41d4-76cde37a77b9@potatochowder.com> <2AE351E4-E23D-44C5-AC82-8741DA63ABA9@barrys-emacs.org> Message-ID: <7d6579d3-f28e-c73b-8b9a-7338f67355af@potatochowder.com> On 9/30/19 8:40 AM, Barry Scott wrote: > > >> On 30 Sep 2019, at 12:51, Dan Sommers <2QdxY4RzWzUUiLuE at potatochowder.com> wrote: >> >> On 9/30/19 4:28 AM, Barry Scott wrote: >>>> On 30 Sep 2019, at 05:40, DL Neil via Python-list wrote: >>>> Should pathlib reflect changes it has made to the file-system? >>> I think it should not. >>> A Path() is the name of a file it is not the file itself. Why should it >>> track changes in the file system for the name? >> >> I would have said the same thing, but the docs? disagree: a >> PurePath represents the name of (or the path to) a file, but a >> Path represents the actual file. > > I'm not seeing that wording in the python 3.7 pathlib documentation. > Can you quote the exact wording please? > > I do see this: > > "Pure path objects provide path-handling operations which don?t actually access a filesystem." > > And: > > "Concrete paths are subclasses of the pure path classes. In addition to operations provided > by the latter, they also provide methods to do system calls on path objects." That's the wording I read. I inferred that "path-handling operations which don't actually access a filesystem" meant an object that didn't necessarily represent an actual file, and that "provide methods to do system calls on path objects" did indicate an actual file. From the existence of Path.read_bytes, I inferred that at least some Path objects represent (and operate on) actual existing files. I've been doing this for a long time, and I may have read my expecations into those words. > There is no requirement that a Path() names a file that exists even. Agreed. >> That said, why doesn't your argument apply to read and write? I >> would certainly expect that writing to a path and then reading >> from that same path would return the newly written data. If I >> squint funny, the Path object is tracking the operations on the >> file system. > > I do not expect that. Consider the time line: > > 1. with p.open('w') write data > 2. external process changes file on disk > 3. with p.open('r') read data > > How would (3) get the data written at (1) guaranteed? > It will lead to bugs to assume that. I didn't say anything about a guarantee, or about an external processes. If I have a single process that writes data to a file and then reads from that file, I would expect to read what I just wrote. See the documentation of Path.read_bytes and Path.write_bytes. If I throw an external process, or a networked file system, or multiple layers of buffering and/or caching into the mix, then all such bets are off. I think you're making my point about expectations. :-) > The path object is allowing system calls that need a file's path to be called, > that is all. Beyond that there is no relationship between the pathlib.Path() > objects and files. The documentation of Path.read_bytes and Path.write_bytes say otherwise. >> I think I'm actually arguing against some long since made (and >> forgotten?) design decisions that can't be changed (dare I say >> fixed?) because changing them would break backwards >> compatibility. >> >> Yuck. :-) And I can absolutely see all sorts of different >> expecations not being met and having to be explained by saying >> "well, that's the way it works." > > I'd suggest that the design is reasonable and If there is misunderstanding that its > something that docs could address. I'm not disagreeing. I suspect that we've both worked on enough different systems to know that not all OSes, file systems, libraries, and versions and combinations thereof work the same way under all circumstances (multiple threads, multiple processes, caching, buffering, etc.). It's the epitome of YMMV. Rename is a particularly thorny case because renaming a file, at least on a POSIX system, is an operation on the directory containing the file rather than the file itself. From barry at barrys-emacs.org Mon Sep 30 10:33:11 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Mon, 30 Sep 2019 15:33:11 +0100 Subject: pathlib In-Reply-To: <826a715b-6191-d3c8-555d-76f6e71865c7@potatochowder.com> References: <699641c7-b16a-a239-e760-068a6a5b41d2@etelligence.info> <25BFF716-8D88-4533-AE47-80725D2A0294@barrys-emacs.org> <4233b002-ec5a-b00f-41d4-76cde37a77b9@potatochowder.com> <2AE351E4-E23D-44C5-AC82-8741DA63ABA9@barrys-emacs.org> <826a715b-6191-d3c8-555d-76f6e71865c7@potatochowder.com> Message-ID: > On 30 Sep 2019, at 14:20, Dan Sommers <2QdxY4RzWzUUiLuE at potatochowder.com> wrote: > > That's the wording I read. I inferred that "path-handling operations > which don't actually access a filesystem" meant an object that didn't > necessarily represent an actual file, and that "provide methods to do > system calls on path objects" did indicate an actual file. From the > existence of Path.read_bytes, I inferred that at least some Path objects > represent (and operate on) actual existing files. I've been doing this > for a long time, and I may have read my expecations into those words. pathlib.Path() adds the semantics of operating system file rules to strings. The addition of open(), read_bytes() etc is a convenience to the programmer. Some people consider this a mistake other a great feature. You can write this: pathlib.Path("name").open() Which is the same as: open(pathlib.Path("name")) Which is the same as: open("name") You would not expect str to track the file, why expect Path() to? Barry From barry at barrys-emacs.org Mon Sep 30 10:51:03 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Mon, 30 Sep 2019 15:51:03 +0100 Subject: Announcing config-path an OS independent configuration file paths In-Reply-To: References: <8733DEC4-9479-4496-A1B3-22D1C95944BF@barrys-emacs.org> Message-ID: <8B1398A7-F3BD-404B-8742-BD0BE9FB01ED@barrys-emacs.org> > On 30 Sep 2019, at 14:17, Paul Moore wrote: > > How does this compare to the existing appdirs module on PyPI? > I did not know about appdirs. It does not seem to have separate read vs. save paths. Required for XDG specification where a path of config folder is defined. On 1st run the config may be in a system directory which the user cannot write to. Saving a config file will go into the users config file. On the 2nd run that users config should be read not the system config. appdirs covers the location of more type of files, like cache and data. Barry From p.f.moore at gmail.com Mon Sep 30 11:06:48 2019 From: p.f.moore at gmail.com (Paul Moore) Date: Mon, 30 Sep 2019 16:06:48 +0100 Subject: Announcing config-path an OS independent configuration file paths In-Reply-To: <8B1398A7-F3BD-404B-8742-BD0BE9FB01ED@barrys-emacs.org> References: <8733DEC4-9479-4496-A1B3-22D1C95944BF@barrys-emacs.org> <8B1398A7-F3BD-404B-8742-BD0BE9FB01ED@barrys-emacs.org> Message-ID: On Mon, 30 Sep 2019 at 15:51, Barry Scott wrote: > > > On 30 Sep 2019, at 14:17, Paul Moore wrote: > > > > How does this compare to the existing appdirs module on PyPI? > > > > I did not know about appdirs. Fair enough ;-) > It does not seem to have separate read vs. save paths. > Required for XDG specification where a path of config folder is defined. > > On 1st run the config may be in a system directory which the user cannot write to. > Saving a config file will go into the users config file. > On the 2nd run that users config should be read not the system config. > > appdirs covers the location of more type of files, like cache and data. Interesting - thanks for the comparison, I'll take a detailed look next time I need this type of functionality. Paul From 2QdxY4RzWzUUiLuE at potatochowder.com Mon Sep 30 11:49:58 2019 From: 2QdxY4RzWzUUiLuE at potatochowder.com (Dan Sommers) Date: Mon, 30 Sep 2019 11:49:58 -0400 Subject: pathlib In-Reply-To: References: <699641c7-b16a-a239-e760-068a6a5b41d2@etelligence.info> <25BFF716-8D88-4533-AE47-80725D2A0294@barrys-emacs.org> <4233b002-ec5a-b00f-41d4-76cde37a77b9@potatochowder.com> <2AE351E4-E23D-44C5-AC82-8741DA63ABA9@barrys-emacs.org> <826a715b-6191-d3c8-555d-76f6e71865c7@potatochowder.com> Message-ID: <77fc603e-8cdb-1d9a-cf92-32bbc13397a4@potatochowder.com> On 9/30/19 10:33 AM, Barry Scott wrote: > > >> On 30 Sep 2019, at 14:20, Dan Sommers >> <2QdxY4RzWzUUiLuE at potatochowder.com >> > wrote: >> >> That's the wording I read. ?I inferred that "path-handling operations >> which don't actually access a filesystem" meant an object that didn't >> necessarily represent an actual file, and that "provide methods to do >> system calls on path objects" did indicate an actual file. ?From the >> existence of Path.read_bytes, I inferred that at least some Path objects >> represent (and operate on) actual existing files. ?I've been doing this >> for a long time, and I may have read my expecations into those words. > > pathlib.Path() adds the semantics of operating system file rules to strings. pathlib.Path() adds the semantics of OS pathnames to strings > The addition of open(), read_bytes() etc is a convenience to the programmer. > Some people consider this a mistake other a great feature. Regardless of your opinion of those methods, they exist, and evidently, they lead to arguably unfounded expectations. Do they rise to the level of a wart that should be deprecated? I don't know. Are they practical in a number of use cases? Probably. That was my original point: a Path object backed by OS- and filesystem- objects and operations is a great idea, but it's imperfect and it leads some programmers astray. > You can write this: > > pathlib.Path("name").open() > > Which is the same as: > > open(pathlib.Path("name")) > > Which is the same as: > > open("name") > > You would not expect str to track the file, why expect Path() to? That's an interesting question. If you phrase the question like that, then you're right: expecting a string to track the content of a file is a mistake. In the totality of a Path object that claims to represent paths to files, including the arguably troublesome read_bytes and write_bytes methods, and a rename method, however, it's not unreasonable expect the object to track *itself* when I use *its* own rename method (backwards compatibility restraints notwithstanding). Dan From toby at tobiah.org Mon Sep 30 11:51:11 2019 From: toby at tobiah.org (Tobiah) Date: Mon, 30 Sep 2019 08:51:11 -0700 Subject: Delay in python startup. Message-ID: I don't have a lot of information, so here goes a shot in the dark. One day I started experiencing a delay when starting python. I'm on Ubuntu 16.04. It takes three seconds to get a prompt when I type 'python' on the command line (Python 2.7.12). When I run a script that imports packages, it takes longer, up to 17 seconds just to do the imports. Python3 is not affected, and is snappy as expected. That's all I know. I'm hoping someone else has seen this. I'm about ready to wipe the drive and upgrade to 18.04. From toby at tobiah.org Mon Sep 30 11:58:07 2019 From: toby at tobiah.org (Tobiah) Date: Mon, 30 Sep 2019 08:58:07 -0700 Subject: Hi how do I import files inside a txt file? References: <23056c2a-bd63-4ee8-af2a-16a3c1aceb73@googlegroups.com> Message-ID: On 9/2/19 3:32 AM, Spencer Du wrote: > Hi > > How do i import files inside a txt file if they exist in the current > directory? > Once you've read the module names you can use: new_module = __import__(modulename) So you'd read the name from your file into modulename and import the name contained in that variable in this way. From anders.marak.leffler at gmail.com Mon Sep 30 07:11:24 2019 From: anders.marak.leffler at gmail.com (=?UTF-8?Q?Anders_M=C3=A4rak_Leffler?=) Date: Mon, 30 Sep 2019 13:11:24 +0200 Subject: Recursive method in class In-Reply-To: <5d91b46f$0$15197$426a74cc@news.free.fr> References: <5d8df86e$0$3370$426a74cc@news.free.fr> <5d91b46f$0$15197$426a74cc@news.free.fr> Message-ID: What do you mean by transformed? This is probably your understanding already, but a further consequence of when arguments are evaluated plus what you said about data attributes is that the fib(self, n - 1) call will follow the standard LEGB-lookup of whatever "fib" is, from the point of view of the function object. As far as I know, there is no transformation of these scopes - either when it comes to creating the class, or creating the instances. (self is just the instance, passed as an argument.) Cf when you change a binding: >>> def factorial(self, n): ... if not n: ... return 1 ... else: ... return n * factorial(self, n - 1) ... >>> Dummy = type("DummyObject", (object, ), {"factorial" : factorial}) >>> instance = Dummy() >>> def factorial(self, n): ... print("Hello!") ... return 999 ... >>> instance.factorial(5) # Where will the call go ("old" or "new" factorial?)? Where will possible recursive calls go (and why)? Hello! 4995 Oh, and as others have pointed out on this list - you/whoever runs the system sending the mail might want to change the return address. none at gmail.com is somewhat consistently classed as spam. //Anders PS. We could further complicate this by adding a call to self.factorial in the new function, but let's not go there. :) On Mon, Sep 30, 2019 at 9:58 AM ast wrote: > > Le 27/09/2019 ? 14:26, Jan van den Broek a ?crit : > > On 2019-09-27, ast wrote: > >> Is it feasible to define a recursive method in a class ? > >> (I don't need it, it's just a trial) > >> > >> Here are failing codes: > >> > >> > >> class Test: > >> def fib(self, n): > >> if n < 2: return n > >> return fib(self, n-2) + fib(self, n-1) > > self.fib(...) > > > > [Schnipp] > > > > Yes you are right. I forgot that class methods > don't see class attributes > > An other workaround: > > def fib(self, n): > if n < 2: return n > return Test.fib(self, n-2) + Test.fib(self, n-1) > > It comes from https://www.pythonsheets.com/notes/python-object.html > > >>> def fib(self, n): > ... if n <= 2: > ... return 1 > ... return fib(self, n-1) + fib(self, n-2) > ... > >>> Fib = type('Fib', (object,), {'val': 10, > ... 'fib': fib}) > >>> f = Fib() > >>> f.val > 10 > >>> f.fib(f.val) > 55 > > So it means that when classes are constructed explicitely > with type(name, bases, dict_), some methods are transformed > -- > https://mail.python.org/mailman/listinfo/python-list From rosuav at gmail.com Mon Sep 30 12:51:04 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 1 Oct 2019 02:51:04 +1000 Subject: pathlib In-Reply-To: <77fc603e-8cdb-1d9a-cf92-32bbc13397a4@potatochowder.com> References: <699641c7-b16a-a239-e760-068a6a5b41d2@etelligence.info> <25BFF716-8D88-4533-AE47-80725D2A0294@barrys-emacs.org> <4233b002-ec5a-b00f-41d4-76cde37a77b9@potatochowder.com> <2AE351E4-E23D-44C5-AC82-8741DA63ABA9@barrys-emacs.org> <826a715b-6191-d3c8-555d-76f6e71865c7@potatochowder.com> <77fc603e-8cdb-1d9a-cf92-32bbc13397a4@potatochowder.com> Message-ID: On Tue, Oct 1, 2019 at 1:51 AM Dan Sommers <2QdxY4RzWzUUiLuE at potatochowder.com> wrote: > In the totality of a Path object that claims to represent paths > to files, including the arguably troublesome read_bytes and > write_bytes methods, and a rename method, however, it's not > unreasonable expect the object to track *itself* when I use *its* > own rename method (backwards compatibility restraints > notwithstanding). > What if the path is formed from another path, and the other one renames? Create a Path("/path/to/some/dir") and then file = dir / "some_file", then rename the directory - should the file insta-reflect that? Even if you DO have them link magically so that it updates, you would then have to contend with the fact that, when you rename directories, you often *want* to slide onto the new dir - that's one way to replace an entire set of files atomically (which also involves open_at). ChrisA From rosuav at gmail.com Mon Sep 30 12:54:18 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 1 Oct 2019 02:54:18 +1000 Subject: Delay in python startup. In-Reply-To: References: Message-ID: On Tue, Oct 1, 2019 at 1:56 AM Tobiah wrote: > > I don't have a lot of information, so here goes a shot in > the dark. One day I started experiencing a delay when > starting python. I'm on Ubuntu 16.04. It takes three > seconds to get a prompt when I type 'python' on the command > line (Python 2.7.12). When I run a script that imports > packages, it takes longer, up to 17 seconds just to do > the imports. Python3 is not affected, and is snappy as > expected. > > That's all I know. I'm hoping someone else has seen this. > I'm about ready to wipe the drive and upgrade to 18.04. > Python 2 and Python 3 have completely independent installations, so it could be a lot of things. First question: Does it take three seconds *every* time you type 'python', or only the first time? If it's slow the first time but then fast, it's probably just a matter of disk caching; running Python 3 doesn't pre-cache the files for Python 2, so you have to pay the load-time cost anew. If it's slow every time, though, you may have something messing with your startup. Try "python -S" and "python -E" see if they're just as slow. That would be a way to delve into things a bit. ChrisA From 2QdxY4RzWzUUiLuE at potatochowder.com Mon Sep 30 13:13:31 2019 From: 2QdxY4RzWzUUiLuE at potatochowder.com (Dan Sommers) Date: Mon, 30 Sep 2019 13:13:31 -0400 Subject: pathlib In-Reply-To: References: <699641c7-b16a-a239-e760-068a6a5b41d2@etelligence.info> <25BFF716-8D88-4533-AE47-80725D2A0294@barrys-emacs.org> <4233b002-ec5a-b00f-41d4-76cde37a77b9@potatochowder.com> <2AE351E4-E23D-44C5-AC82-8741DA63ABA9@barrys-emacs.org> <826a715b-6191-d3c8-555d-76f6e71865c7@potatochowder.com> <77fc603e-8cdb-1d9a-cf92-32bbc13397a4@potatochowder.com> Message-ID: <290b9421-6792-ff6d-ad7e-4f4b3a58f8dc@potatochowder.com> On 9/30/19 12:51 PM, Chris Angelico wrote: > On Tue, Oct 1, 2019 at 1:51 AM Dan Sommers > <2QdxY4RzWzUUiLuE at potatochowder.com> wrote: >> In the totality of a Path object that claims to represent paths >> to files, including the arguably troublesome read_bytes and >> write_bytes methods, and a rename method, however, it's not >> unreasonable expect the object to track *itself* when I use *its* >> own rename method (backwards compatibility restraints >> notwithstanding). >> > > What if the path is formed from another path, and the other one > renames? Create a Path("/path/to/some/dir") and then file = dir / > "some_file", then rename the directory - should the file insta-reflect > that? Even if you DO have them link magically so that it updates, you > would then have to contend with the fact that, when you rename > directories, you often *want* to slide onto the new dir - that's one > way to replace an entire set of files atomically (which also involves > open_at). Mu. The filesystem is its own ultimate arbiter. It handles (or not) race conditions, failures, multiple processes, etc. in some manner, which might not even be reasonable or consistent from one moment to the next (who among us hasn't debugged a problem that came down the patch level of NFS running on some remote system?). The level to which some random object in a Python heap reflects any given filesystem is, well, arbitrary. All I'm doing is defending the OP, who was surprised that renaming a file *using a Path instance* didn't reflect that operation *in that Path instance*. I believe that such a surprise is reasonable; others apparently don't. Yes, there are a lot of reasons that it is the way it is, and a lot of reasons not to change it now. I get that. If the OP is still here, then I'd like to think that the OP also gets that. From toby at tobiah.org Mon Sep 30 13:28:49 2019 From: toby at tobiah.org (Tobiah) Date: Mon, 30 Sep 2019 10:28:49 -0700 Subject: Delay in python startup. References: Message-ID: On 9/30/19 9:54 AM, Chris Angelico wrote: > On Tue, Oct 1, 2019 at 1:56 AM Tobiah wrote: >> >> I don't have a lot of information, so here goes a shot in >> the dark. One day I started experiencing a delay when >> starting python. I'm on Ubuntu 16.04. It takes three >> seconds to get a prompt when I type 'python' on the command >> line (Python 2.7.12). When I run a script that imports >> packages, it takes longer, up to 17 seconds just to do >> the imports. Python3 is not affected, and is snappy as >> expected. >> >> That's all I know. I'm hoping someone else has seen this. >> I'm about ready to wipe the drive and upgrade to 18.04. >> > > Python 2 and Python 3 have completely independent installations, so it > could be a lot of things. First question: Does it take three seconds > *every* time you type 'python', or only the first time? If it's slow > the first time but then fast, it's probably just a matter of disk > caching; running Python 3 doesn't pre-cache the files for Python 2, so > you have to pay the load-time cost anew. > > If it's slow every time, though, you may have something messing with > your startup. Try "python -S" and "python -E" see if they're just as > slow. That would be a way to delve into things a bit. > > ChrisA > It was much faster with -S and instantaneous with -E. I had a directory in my PYTHONPATH that I mount with sshfs from a server. For some reason that mount is very slow. Thanks for helping me figure this out. Tobiah From PythonList at DancesWithMice.info Mon Sep 30 13:39:07 2019 From: PythonList at DancesWithMice.info (DL Neil) Date: Tue, 1 Oct 2019 06:39:07 +1300 Subject: pathlib In-Reply-To: <290b9421-6792-ff6d-ad7e-4f4b3a58f8dc@potatochowder.com> References: <699641c7-b16a-a239-e760-068a6a5b41d2@etelligence.info> <25BFF716-8D88-4533-AE47-80725D2A0294@barrys-emacs.org> <4233b002-ec5a-b00f-41d4-76cde37a77b9@potatochowder.com> <2AE351E4-E23D-44C5-AC82-8741DA63ABA9@barrys-emacs.org> <826a715b-6191-d3c8-555d-76f6e71865c7@potatochowder.com> <77fc603e-8cdb-1d9a-cf92-32bbc13397a4@potatochowder.com> <290b9421-6792-ff6d-ad7e-4f4b3a58f8dc@potatochowder.com> Message-ID: On 1/10/19 6:13 AM, Dan Sommers wrote: > On 9/30/19 12:51 PM, Chris Angelico wrote: >> On Tue, Oct 1, 2019 at 1:51 AM Dan Sommers ... > All I'm doing is defending the OP, who was surprised that > renaming a file *using a Path instance* didn't reflect that > operation *in that Path instance*.? I believe that such a > surprise is reasonable; others apparently don't. > > Yes, there are a lot of reasons that it is the way it is, and a > lot of reasons not to change it now.? I get that.? If the OP is > still here, then I'd like to think that the OP also gets that. The OP is still 'here' - has been asleep = lazy toad! Thank you. Yes, that is a reasonable summary. Yes it did catch me by surprise - that I couldn't rely upon the instance to reflect 'reality' (IMHO). I see there is a different point-of-view. To adjust my mind-set and properly appreciate the discussion, I'd like to re-read the docs with that in-mind, first... -- Regards =dn From rosuav at gmail.com Mon Sep 30 13:58:37 2019 From: rosuav at gmail.com (Chris Angelico) Date: Tue, 1 Oct 2019 03:58:37 +1000 Subject: Delay in python startup. In-Reply-To: References: Message-ID: On Tue, Oct 1, 2019 at 3:31 AM Tobiah wrote: > > On 9/30/19 9:54 AM, Chris Angelico wrote: > > On Tue, Oct 1, 2019 at 1:56 AM Tobiah wrote: > >> > >> I don't have a lot of information, so here goes a shot in > >> the dark. One day I started experiencing a delay when > >> starting python. I'm on Ubuntu 16.04. It takes three > >> seconds to get a prompt when I type 'python' on the command > >> line (Python 2.7.12). When I run a script that imports > >> packages, it takes longer, up to 17 seconds just to do > >> the imports. Python3 is not affected, and is snappy as > >> expected. > >> > >> That's all I know. I'm hoping someone else has seen this. > >> I'm about ready to wipe the drive and upgrade to 18.04. > >> > > > > Python 2 and Python 3 have completely independent installations, so it > > could be a lot of things. First question: Does it take three seconds > > *every* time you type 'python', or only the first time? If it's slow > > the first time but then fast, it's probably just a matter of disk > > caching; running Python 3 doesn't pre-cache the files for Python 2, so > > you have to pay the load-time cost anew. > > > > If it's slow every time, though, you may have something messing with > > your startup. Try "python -S" and "python -E" see if they're just as > > slow. That would be a way to delve into things a bit. > > > > ChrisA > > > It was much faster with -S and instantaneous with -E. I had a directory > in my PYTHONPATH that I mount with sshfs from a server. For some reason > that mount is very slow. Thanks for helping me figure this out. > Cool! That would be it, then - every import had to go through that SSH tunnel. Happy to help out. ChrisA From barry at barrys-emacs.org Mon Sep 30 15:56:06 2019 From: barry at barrys-emacs.org (Barry Scott) Date: Mon, 30 Sep 2019 20:56:06 +0100 Subject: pathlib In-Reply-To: <77fc603e-8cdb-1d9a-cf92-32bbc13397a4@potatochowder.com> References: <699641c7-b16a-a239-e760-068a6a5b41d2@etelligence.info> <25BFF716-8D88-4533-AE47-80725D2A0294@barrys-emacs.org> <4233b002-ec5a-b00f-41d4-76cde37a77b9@potatochowder.com> <2AE351E4-E23D-44C5-AC82-8741DA63ABA9@barrys-emacs.org> <826a715b-6191-d3c8-555d-76f6e71865c7@potatochowder.com> <77fc603e-8cdb-1d9a-cf92-32bbc13397a4@potatochowder.com> Message-ID: <59D0D1C8-F0E8-4152-BD59-72377100C2A7@barrys-emacs.org> > On 30 Sep 2019, at 16:49, Dan Sommers <2QdxY4RzWzUUiLuE at potatochowder.com> wrote: > > That's an interesting question. If you phrase the question like > that, then you're right: expecting a string to track the content > of a file is a mistake. > > In the totality of a Path object that claims to represent paths > to files, It represents string that *should* in most cases work in the APIs that work on files. Not the same idea. > including the arguably troublesome read_bytes and > write_bytes methods, and a rename method, however, it's not > unreasonable expect the object to track *itself* when I use *its* > own rename method (backwards compatibility restraints > notwithstanding). "the object" did track the changes its just that "the object" is not the Path object, it's in the operating system and/or the file system. For the rename it's the directory that the name is recorded in. There was an interest talk at this years PYCON UK about the the errors that people new to python make. Misunderstand syntax is about 1/3 of the problems, but 2/3 was having the wrong model. This discussion seems to fall into the "wrong model" world that leads to a expectation failure. Have we moved on to how we can improve the situation? Barry From cs at cskk.id.au Mon Sep 30 20:39:59 2019 From: cs at cskk.id.au (Cameron Simpson) Date: Tue, 1 Oct 2019 10:39:59 +1000 Subject: Delay in python startup. In-Reply-To: References: Message-ID: <20191001003959.GA87471@cskk.homeip.net> On 30Sep2019 10:28, Tobiah wrote: >On 9/30/19 9:54 AM, Chris Angelico wrote: >>On Tue, Oct 1, 2019 at 1:56 AM Tobiah wrote: >It was much faster with -S and instantaneous with -E. I had a >directory >in my PYTHONPATH that I mount with sshfs from a server. For some reason >that mount is very slow. Thanks for helping me figure this out. For the future: you can often see something this slow with an strace: strace python Watch what it is doing when it stalls. Cheers, Cameron Simpson From 2QdxY4RzWzUUiLuE at potatochowder.com Mon Sep 30 21:58:10 2019 From: 2QdxY4RzWzUUiLuE at potatochowder.com (Dan Sommers) Date: Mon, 30 Sep 2019 21:58:10 -0400 Subject: pathlib In-Reply-To: <59D0D1C8-F0E8-4152-BD59-72377100C2A7@barrys-emacs.org> References: <699641c7-b16a-a239-e760-068a6a5b41d2@etelligence.info> <25BFF716-8D88-4533-AE47-80725D2A0294@barrys-emacs.org> <4233b002-ec5a-b00f-41d4-76cde37a77b9@potatochowder.com> <2AE351E4-E23D-44C5-AC82-8741DA63ABA9@barrys-emacs.org> <826a715b-6191-d3c8-555d-76f6e71865c7@potatochowder.com> <77fc603e-8cdb-1d9a-cf92-32bbc13397a4@potatochowder.com> <59D0D1C8-F0E8-4152-BD59-72377100C2A7@barrys-emacs.org> Message-ID: On 9/30/19 3:56 PM, Barry Scott wrote: > > >> On 30 Sep 2019, at 16:49, Dan Sommers >> <2QdxY4RzWzUUiLuE at potatochowder.com >> > wrote: >> In the totality of a Path object that claims to represent paths >> to files, > > It represents string that *should* in most cases work in the APIs > that work on files. Not the same idea. I'm not sure the documentation supports your view. Components of paths are strings, but IMO the Path object represents a file. >> including the arguably troublesome read_bytes and >> write_bytes methods, and a rename method, however, it's not >> unreasonable expect the object to track *itself* when I use *its* >> own rename method (backwards compatibility restraints >> notwithstanding). > > "the object" did track the changes its just that "the object" is not > the Path object, it's in the operating system and/or the file system. > For the rename it's the directory that the name is recorded in. So which is it? Does the Path object represent a string, the name of a file (whether that file exists or not), or the file itself? A moment ago, you claimed that a Path object represents a string that should work in the APIs that work on files. Now, you're claiming that the Path object is a proxy for something in the filesystem. I don't mean to be combative or confrontational, but I think that this fuzziness/inconsistency is at or near the root of the differing expectations. > There was an interest talk at this years PYCON UK about the > the errors that people new to python make. Misunderstand syntax > is about 1/3 of the problems, but 2/3 was having the wrong model. > > This discussion seems to fall into the "wrong model" world that > leads to a expectation failure. On this (that there's something about the model of Path objects that leads to expectation failures) we agree. :-) > Have we moved on to how we can improve the situation? Absolutely. Documenting the fact that calling rename on a Path object does not update that object, and at least an acknowledgement of the backwards compatibility issues, would be a good start. Does the same sort of thing happen with Path.chmod as well? Clarifying what a Path object represents is also in order. Again, I note that the top of https://docs.python.org/3/library/pathlib.html talks about filesystem paths, but a lot of the method descriptions (e.g., rename) use the phrase "this file," as if Path objects represent (or are actually proxies for) actual files or an actual filesystem.