From jarod_v6 at libero.it Sun Nov 1 13:31:03 2015 From: jarod_v6 at libero.it (jarod_v6 at libero.it) Date: Sun, 1 Nov 2015 19:31:03 +0100 (CET) Subject: [Tutor] How to parse large files Message-ID: <1299087175.5904061446402663325.JavaMail.httpd@webmail-61.iol.local> My file have 1960607 rows but I don't understand why I'm not able to create a dictionary in fast way I try to use also gc.disable but Not work. I need to have dictionary but I have this erro: with shelve.open("diz5") as db: with open("tmp1.txt") as instream: for line in instream: assert line.count("\t") == 1 key, _tab, value = line.rstrip("\n").partition("\t") values = db.get(key) or set() values.add(value) db[key] = values AttributeError Traceback (most recent call last) in () ----> 1 with shelve.open("diz5") as db: 2 with open("tmp1.txt") as instream: 3 for line in instream: 4 assert line.count("\t") == 1 5 key, _tab, value = line.rstrip("\n").partition("\t") AttributeError: DbfilenameShelf instance has no attribute '__exit__' In [4]: I need to do intersection of dictionary key. thanks for the help M. From dyoo at hashcollision.org Sun Nov 1 13:58:23 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 1 Nov 2015 10:58:23 -0800 Subject: [Tutor] How to parse large files In-Reply-To: <1299087175.5904061446402663325.JavaMail.httpd@webmail-61.iol.local> References: <1299087175.5904061446402663325.JavaMail.httpd@webmail-61.iol.local> Message-ID: > AttributeError Traceback (most recent call last) > in () > ----> 1 with shelve.open("diz5") as db: > 2 with open("tmp1.txt") as instream: > 3 for line in instream: > 4 assert line.count("\t") == 1 > 5 key, _tab, value = line.rstrip("\n").partition("\t") > > AttributeError: DbfilenameShelf instance has no attribute '__exit__' The error that you're seeing is on this line: with shelve.open("diz5") as db: so we should focus our efforts to know why this line is failing. The with statement in Python has a requirement, that the resource supports context management. Context managers have to have a few methods, according to: https://docs.python.org/3/reference/compound_stmts.html#the-with-statement https://docs.python.org/3/reference/datamodel.html#context-managers However, the error message reports that it can't find a method that it's looking for, "__exit__". it looks like shelves don't have the methods "__enter__" or "__exit__", which context managers must have. It looks like a deficiency in 'shelve', but one that we can work around without too much difficulty. We can use the contextlib.closing() function to adapt a thing that knows how to close(), so that it works as a context manager. https://docs.python.org/3/library/contextlib.html#contextlib.closing It should be a matter of saying: import contextlib ... with contextlib.closing(shelve.open("diz5")) as db: ... If you have questions, please feel free to ask. From __peter__ at web.de Sun Nov 1 14:19:25 2015 From: __peter__ at web.de (Peter Otten) Date: Sun, 01 Nov 2015 20:19:25 +0100 Subject: [Tutor] How to parse large files References: <1299087175.5904061446402663325.JavaMail.httpd@webmail-61.iol.local> Message-ID: Danny Yoo wrote: >> AttributeError Traceback (most recent call >> last) in () >> ----> 1 with shelve.open("diz5") as db: >> 2 with open("tmp1.txt") as instream: >> 3 for line in instream: >> 4 assert line.count("\t") == 1 >> 5 key, _tab, value = line.rstrip("\n").partition("\t") >> >> AttributeError: DbfilenameShelf instance has no attribute '__exit__' > > > The error that you're seeing is on this line: > > with shelve.open("diz5") as db: > > so we should focus our efforts to know why this line is failing. > > > The with statement in Python has a requirement, that the resource > supports context management. Context managers have to have a few > methods, according to: > > https://docs.python.org/3/reference/compound_stmts.html#the-with-statement > > https://docs.python.org/3/reference/datamodel.html#context-managers > > > However, the error message reports that it can't find a method that > it's looking for, "__exit__". it looks like shelves don't have the > methods "__enter__" or "__exit__", which context managers must have. > > It looks like a deficiency in 'shelve', but one that we can work > around without too much difficulty. You need 3.4 or higher to use with... Quoting : """ class shelve.Shelf(dict, protocol=None, writeback=False, keyencoding='utf-8') ... Changed in version 3.4: Added context manager support. """ > We can use the > contextlib.closing() function to adapt a thing that knows how to > close(), so that it works as a context manager. > https://docs.python.org/3/library/contextlib.html#contextlib.closing > > It should be a matter of saying: > > import contextlib > ... > with contextlib.closing(shelve.open("diz5")) as db: ... Yes, either use contextlib.closing() or close the db manually: db = shelve.open("diz5") try: ... # use db finally: db.close() From jarod_v6 at libero.it Sun Nov 1 15:33:32 2015 From: jarod_v6 at libero.it (jarod_v6 at libero.it) Date: Sun, 1 Nov 2015 21:33:32 +0100 (CET) Subject: [Tutor] How to parse large files Message-ID: <442325405.5680531446410012492.JavaMail.httpd@webmail-05.iol.local> Thanks!! I use python2.7 Can Also use in that version? I don't understand why use partition and not split(). what is the reason for that? From __peter__ at web.de Sun Nov 1 18:17:19 2015 From: __peter__ at web.de (Peter Otten) Date: Mon, 02 Nov 2015 00:17:19 +0100 Subject: [Tutor] How to parse large files References: <442325405.5680531446410012492.JavaMail.httpd@webmail-05.iol.local> Message-ID: jarod_v6--- via Tutor wrote: > Thanks!! > I use python2.7 Can Also use in that version? Yes. > I don't understand why use partition and not split(). what is the reason > for that? If there is exactly one "\t" in the line key, value = line.split("\t") and key, _tab, value = line.partition("\t") are equivalent, so it's fine to use split(). I used partition() because the method name indicates that I want two parts, but with the extra assert parts = line.split("\t") if len(parts) != 2: raise ValueError key, value = parts would probably have been the better choice because it works the same with enabled and disabled asserts. From alan.gauld at btinternet.com Sun Nov 1 18:56:25 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 1 Nov 2015 23:56:25 +0000 Subject: [Tutor] How to parse large files In-Reply-To: <442325405.5680531446410012492.JavaMail.httpd@webmail-05.iol.local> References: <442325405.5680531446410012492.JavaMail.httpd@webmail-05.iol.local> Message-ID: On 01/11/15 20:33, jarod_v6--- via Tutor wrote: > Thanks!! > I use python2.7 Can Also use in that version? > I don't understand why use partition and not split(). what is the reason for > that? I'm confused? Where did partition v split enter into things? Am I missing a message some place? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Sun Nov 1 19:42:09 2015 From: __peter__ at web.de (Peter Otten) Date: Mon, 02 Nov 2015 01:42:09 +0100 Subject: [Tutor] How to parse large files References: <442325405.5680531446410012492.JavaMail.httpd@webmail-05.iol.local> Message-ID: Alan Gauld wrote: > On 01/11/15 20:33, jarod_v6--- via Tutor wrote: >> Thanks!! >> I use python2.7 Can Also use in that version? >> I don't understand why use partition and not split(). what is the reason >> for that? > > I'm confused? Where did partition v split enter into things? > Am I missing a message some place? I used partition() instead of split() in my shelve example: https://mail.python.org/pipermail/tutor/2015-October/107103.html Sorry for that distraction ;) From dyoo at hashcollision.org Sun Nov 1 19:46:44 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 1 Nov 2015 18:46:44 -0600 Subject: [Tutor] How to parse large files In-Reply-To: References: <442325405.5680531446410012492.JavaMail.httpd@webmail-05.iol.local> Message-ID: On Sun, Nov 1, 2015 at 5:17 PM, Peter Otten <__peter__ at web.de> wrote: > jarod_v6--- via Tutor wrote: > >> Thanks!! >> I use python2.7 Can Also use in that version? > > Yes. Hi Jarod, Also for reference, here are the equivalent 2.7 doc links: https://docs.python.org/2/reference/compound_stmts.html#the-with-statement https://docs.python.org/2/reference/datamodel.html#context-managers https://docs.python.org/2/library/contextlib.html#contextlib.closing Good luck! From oscar.j.benjamin at gmail.com Tue Nov 3 10:37:34 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 3 Nov 2015 15:37:34 +0000 Subject: [Tutor] Plotting with python In-Reply-To: References: Message-ID: On 31 October 2015 at 00:00, Terry Carroll wrote: > If you were going to get started doing some simple plotting with Python 2.7 > (in my case, I'm simply plotting temperature against time-of-day) what would > you use? > > - matplotlib [1] > - gnuplot [2] > - something else entirely? I'd use matplotlib. > Assume no substantial familiarity with the underlying plotting software, let > alone the Python bindings. > > The only thing I can think of that might be special is to specify the > upper/lower bounds of the plot; for example, in my case, I know the > temperatures vary between somewhere around 70-78 degrees F., so I'd want the > Y-axis to go, say 60-90, not arbitrarily start at zero; but I suspect this > is a pretty standard thing in almost any plotting package. This is straightforward in most plotting packages. Here's a simple example of doing it in matplotlib: #!/usr/bin/env python3 import matplotlib.pyplot as plt times = [0, 1, 2, 3, 4, 5] # hours temperatures = [68, 70, 75, 73, 72, 71] # Fahrenheit fig = plt.figure(figsize=(5, 4)) ax = fig.add_axes([0.15, 0.15, 0.70, 0.70]) ax.plot(times, temperatures) ax.set_xlabel('Time (hours)') ax.set_ylabel(r'Temp ($^{\circ}\mathrm{F}$)') ax.set_title('Temperature vs time') plt.show() -- Oscar From andrew.c.machen at gmail.com Tue Nov 3 12:20:07 2015 From: andrew.c.machen at gmail.com (Andrew Machen) Date: Tue, 3 Nov 2015 17:20:07 +0000 Subject: [Tutor] Missing Standard Libraries on Python 3.5 for Mac Message-ID: Hi, I am new to Python, and I am trying to use a Python Standard Library, namely ?unicodedata?, however it appears to be missing (others are also missing) from the built-in libraries that come with Python 3.5 for Mac. I have searched the following directory, but have not found the library: /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5 I have tried reinstalling Python 3.5 and also tried Python 3.4.3, and neither install the ?unicodedata? library. Is there anyway to install it manually? The library is present in the Python 2.7 version provided by Apple, however I was hoping to learn and use Python 3. The library is present in the Linux version of Python 3.5 that I have tried. Your help is greatly appreciated. -Andrew From stpetersn at hotmail.com Tue Nov 3 12:59:26 2015 From: stpetersn at hotmail.com (Tommy Peterson) Date: Tue, 3 Nov 2015 12:59:26 -0500 Subject: [Tutor] ImportError: No module named connect Message-ID: I have been trying to install the MySQL Connect module for a day and a half. First I got an error saying that "No module named mysql" was installed. After a lot of trial and error I got passed that. I realized that because I have python installed in /usr/local that I needed to build and then install the library into site-packages where Python was looking. But now it appears that the submodule "connect" cannot be located. Some people have suggested online that I needed to make sure the module was added to the system path by doing this: import sys sys.path.insert(1,'//usr/local/lib/python2.4/site-packages/mysql') import mysql Some have suggested that I needed to edit the __init__.py file in /mysql/ with this: from pkgutil import extend_path path = extend_path(path, name) But after I built and then installed pointing to my site-packages directory the __init__.py file in /usr/local/lib/python2.4/site-packages/mysql/ has that code already. The __init__.py in mysql/connector/ has a lot of stuff. It uses dot notation as well which is beyond my rudimentary Python knowledge: for example: from . import version from .connection import MySQLConnection It appears this is related to the install and that I am using /usr/local/lib/python2.4/site-packages. But I am running out of ideas to try. I am not Python expert as you can tell. Any suggestions? Again the error in my script where I try to import mysql.connection to connect to mysql is: import mysql.connect ImportError: No module named connect The installation instructions are : http://dev.mysql.com/doc/connector-python/en/connector-python-introduction.html Also I have tried installing it via yum. No go there either. From stpetersn at hotmail.com Tue Nov 3 13:22:21 2015 From: stpetersn at hotmail.com (Tommy Peterson) Date: Tue, 3 Nov 2015 13:22:21 -0500 Subject: [Tutor] ImportError: No module named connect In-Reply-To: References: Message-ID: As I typed this out I double checked something. I resolved my own problem. Should have stepped back before posting. It is import mysql.connector not import mysql.connection From: stpetersn at hotmail.com To: tutor at python.org Subject: ImportError: No module named connect Date: Tue, 3 Nov 2015 12:59:26 -0500 I have been trying to install the MySQL Connect module for a day and a half. First I got an error saying that "No module named mysql" was installed. After a lot of trial and error I got passed that. I realized that because I have python installed in /usr/local that I needed to build and then install the library into site-packages where Python was looking. But now it appears that the submodule "connect" cannot be located. Some people have suggested online that I needed to make sure the module was added to the system path by doing this: import sys sys.path.insert(1,'//usr/local/lib/python2.4/site-packages/mysql') import mysql Some have suggested that I needed to edit the __init__.py file in /mysql/ with this: from pkgutil import extend_path path = extend_path(path, name) But after I built and then installed pointing to my site-packages directory the __init__.py file in /usr/local/lib/python2.4/site-packages/mysql/ has that code already. The __init__.py in mysql/connector/ has a lot of stuff. It uses dot notation as well which is beyond my rudimentary Python knowledge: for example: from . import version from .connection import MySQLConnection It appears this is related to the install and that I am using /usr/local/lib/python2.4/site-packages. But I am running out of ideas to try. I am not Python expert as you can tell. Any suggestions? Again the error in my script where I try to import mysql.connection to connect to mysql is: import mysql.connect ImportError: No module named connect The installation instructions are : http://dev.mysql.com/doc/connector-python/en/connector-python-introduction.html Also I have tried installing it via yum. No go there either. From zachary.ware+pytut at gmail.com Tue Nov 3 13:51:36 2015 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Tue, 3 Nov 2015 12:51:36 -0600 Subject: [Tutor] Missing Standard Libraries on Python 3.5 for Mac In-Reply-To: References: Message-ID: Hi Andrew, On Tue, Nov 3, 2015 at 11:20 AM, Andrew Machen wrote: > Hi, > > I am new to Python, and I am trying to use a Python Standard Library, namely ?unicodedata?, however it appears to be missing (others are also missing) from the built-in libraries that come with Python 3.5 for Mac. > > I have searched the following directory, but have not found the library: > > /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5 Have you tried importing it? If importing it works, you can then find where it lives by checking the __file__ attribute: import unicodedata print(unicodedata.__file__) > I have tried reinstalling Python 3.5 and also tried Python 3.4.3, and neither install the ?unicodedata? library. Is there anyway to install it manually? It should be included with your installation, assuming you're using the package from python.org. If you are unable to import it, please raise an issue on the bug tracker at bugs.python.org. For the record, here's where unicodedata lives on my Mac: /Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload/unicodedata.cpython-35m-darwin.so Note that unicodedata is implemented in C, so it does not have a .py extension. -- Zach From steve at pearwood.info Tue Nov 3 19:13:54 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 4 Nov 2015 11:13:54 +1100 Subject: [Tutor] ImportError: No module named connect In-Reply-To: References: Message-ID: <20151104001353.GL10946@ando.pearwood.info> On Tue, Nov 03, 2015 at 12:59:26PM -0500, Tommy Peterson wrote: > I have been trying to install the MySQL Connect module for a day and a half. I see that you believe that you have solved your problem ("from mysql import connector" rather than "connect" or "connection") but for the record you have other problems. I recognise that, right now, you probably have a working system and the last thing you want to do is mess with it, but trust me, whoever has to maintain your system in the future is going to be *utterly confused* by your setup. If you choose to ignore this ("hey, my system works, I'm not going to mess with it") at least copy this post into your site wiki (if you have one) or put it in a README file somewhere where the next guy (or even you, in the future) can see it. Because your Python setup is *seriously* weird. You talk about using the site-packages in python2.4. Why are you using Python 2.4? That's ancient. I see that you are using a Redhat based system, as you refer to yum, but it doesn't make sense that you have Python 2.4 in /usr/local/lib. In old Centos/Fedora/Redhat systems, where Python 2.4 is still used, we have the path set here: /usr/lib/python2.4/ *not* /usr/local/lib. Perhaps you are using a more recent RH-based system, which defaults to Python 2.6 or 2.7. So why have you installed 2.4? It's so old that the mysql package won't work. I know it won't work because mysql __init__.py includes the dot notation from . import version which is a SyntaxError in Python 2.4. So it appears that: - you have installed Python 2.4 in a non-standard place; - installed a package for 2.6 or higher in the 2.4 site-packages; - and are running Python 2.6 or higher using the 2.4 site-packages. Anyone who has to maintain or administer your system is going to be horribly confused. Despite the references to 2.4 site-packages, I expect you are using Python 2.6 or higher. You ought to be able to install the Python mysql library using yum. On my Centos system, I can do this: [root at ando ~]# yum search mysql-python [...] MySQL-python.i386 : An interface to MySQL python26-mysqldb.i386 : An interface to MySQL so all I need to do is: sudo yum install mysql-python to install into the system Python 2.4, or: sudo yum install python26-mysqldb to install into the yum-provided Python 2.6 installation. I don't know which precise RH-based system you are using, but I would be *astonished* if it didn't support the mysql package appropriate for your version of Python. Installing things by hand, as you appear to have done, is not needed. Some further comments about the quality of advice you appear to have been given: > Some people have suggested online that I needed to make sure the > module was added to the system path by doing this: > import sys > sys.path.insert(1,'//usr/local/lib/python2.4/site-packages/mysql') I don't know who "some people" are, but that's clearly wrong. site-packages will be automatically installed in the path (unless something has gone horribly, horribly wrong!). And, you don't need to add the package itself (mysql in this case) to the path. Well, you can, but it goes against the concept of a self-contained package. It's certainly not the right way to fix the problem. > import mysql > Some have suggested that I needed to edit the __init__.py file in /mysql/ with this: > from pkgutil import extend_path > path = extend_path(path, name) Absolutely not. Again, there are thousands of users of mysql. If submodules couldn't be imported, surely they would have noticed, and the mysql package fixed. Having to hack a buggy package in that way might be a reasonable suggestion for some sort of minor, half-debugged and slightly dodgy library with one maintainer and three users, but certainly not for something as widely-used as mysql. > But after I built and then installed pointing to my site-packages > directory the __init__.py file in > /usr/local/lib/python2.4/site-packages/mysql/ has that code already. > The __init__.py in mysql/connector/ has a lot of stuff. It uses dot > notation as well which is beyond my rudimentary Python knowledge: for > example: > > from . import version > from .connection import MySQLConnection As I mentioned above, this dot notation is not supported by Python 2.4. But you've installed the package into the 2.4 site-packages. If you ever run Python 2.4, this could cause problems for you. Good luck! -- Steve From richkappler at gmail.com Wed Nov 4 13:36:07 2015 From: richkappler at gmail.com (richard kappler) Date: Wed, 4 Nov 2015 13:36:07 -0500 Subject: [Tutor] parsing xml as lines Message-ID: I have an xml file that get's written to as events occur. Each event writes a new 'line' of xml to the file, in a specific format, eg: sometthing like this: somestuff and each 'line' has that same structure or format. I've written a script that parses out the needed data and forwards it on using regex's, but think it might be better to use an xml parser. I can parse out what I need to if I have just one line in the file, but when there are number of lines as there actually are, I can't figure out how to get it to work. In other words, with a one line file, this works fine and I understand it: import xml.etree.cElementTree as ET tree = ET.ElementTree(file='1lineTest.log' grandchild = tree.find('grandchild') print grandchild.tag, grandchild.text and I get the output I desire: grandchild Sally But if I have several lines in the file try to run a loop: import xml.etree.cElementTree as ET f1 = open('5lineTest.log', 'r') lineList = f1.readlines() Imax = len(lineList) i = 0 while i <= Imax: tree = ET.ElementTree(lineList[i]) grandchild = tree.find('grandchild') print grandchild.tag, grandchild.txt i += 1 Traceback (most recent call last): File "", line 4, in AttributeError: 'int' object has no attribute 'tag' and yet I can do: print lineList[0] and it will print out the first line. I get why (I think), I just can't figure out a way around it. Guidance please? From __peter__ at web.de Wed Nov 4 14:41:27 2015 From: __peter__ at web.de (Peter Otten) Date: Wed, 04 Nov 2015 20:41:27 +0100 Subject: [Tutor] parsing xml as lines References: Message-ID: richard kappler wrote: > I have an xml file that get's written to as events occur. Each event > writes a new 'line' of xml to the file, in a specific format, eg: > sometthing like this: > > xsi:noNamespaceSchemaLocation="Logging.xsd" version="1.0"> heresmydata/>somestuff > > and each 'line' has that same structure or format. > > I've written a script that parses out the needed data and forwards it on > using regex's, but think it might be better to use an xml parser. I can > parse out what I need to if I have just one line in the file, but when > there are number of lines as there actually are, I can't figure out how to > get it to work. > > In other words, with a one line file, this works fine and I understand it: > > import xml.etree.cElementTree as ET > tree = ET.ElementTree(file='1lineTest.log' > grandchild = tree.find('grandchild') > print grandchild.tag, grandchild.text > > and I get the output I desire: > > grandchild Sally > > But if I have several lines in the file try to run a loop: > > import xml.etree.cElementTree as ET > f1 = open('5lineTest.log', 'r') > lineList = f1.readlines() > Imax = len(lineList) > > i = 0 > while i <= Imax: > tree = ET.ElementTree(lineList[i]) > grandchild = tree.find('grandchild') > print grandchild.tag, grandchild.txt > i += 1 > > Traceback (most recent call last): > File "", line 4, in > AttributeError: 'int' object has no attribute 'tag' > > and yet I can do: > > print lineList[0] and it will print out the first line. > > I get why (I think), I just can't figure out a way around it. > > Guidance please? Ceterum censo ;) Abandon the notion of lines! To process nodes as they arrive from the parser have a look at iterparse: http://effbot.org/zone/element-iterparse.htm https://docs.python.org/dev/library/xml.etree.elementtree.html#xml.etree.ElementTree.iterparse From esawiek at gmail.com Wed Nov 4 12:43:25 2015 From: esawiek at gmail.com (Ek Esawi) Date: Wed, 4 Nov 2015 12:43:25 -0500 Subject: [Tutor] cvxopt install problem Message-ID: Hi All-- I posted this question to another group but have not gotten any input. I needed to install cvxopt on a 64 bit W7. I found out that cvxopt is incompatible with a 64 bit. It only compatible with a 32 bit which can be installed on a 64 bit computer and it works. I stumbled on this on another discussion group posting. The suggestion was to install python 32 bit on a 64 bit computer, then download cvxopt and install it via conda. Here is the suggestion from the posting [If you are using Windows, an easy solution could be the Anaconda distribution (http://continuum.io/downloads). Select and install the 32 bit version. It will be easier to install the cvxopt package. Search it in the binstar repository ( https://binstar.org/search?q=cvxopt). Open a console terminal and install it (e. g. "conda install -chttps://conda.binstar.org/omnia cvxopt").?] I did everything except the last command to install cvxopt, that?s ?conda install -chttps://conda.binstar.org/omnia cvxopt". Here is the command I used: conda install c:/Users/eesawi0001/Downloads/cvxopt-1.1.7-py34.tar I kept getting invalid syntax Any help is greatly appreciated. EKE From alan.gauld at btinternet.com Thu Nov 5 04:05:14 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 5 Nov 2015 09:05:14 +0000 Subject: [Tutor] cvxopt install problem In-Reply-To: References: Message-ID: On 04/11/15 17:43, Ek Esawi wrote: Caveat: This list is for help with the Python language and standard library so any third party modules like cvxopt will depend on whether anybody here has experience or not. Usually you will get better results asking on the library's support forum. Having said that here are a few observations... > Here is the suggestion from the posting >[If you are using Windows, an easy solution > could be the Anaconda distribution (http://continuum.io/downloads). > > Select and install the 32 bit version. It will be easier to install the cvxopt > package. Search it in the binstar repository (https://binstar.org/search?q=cvxopt). I'm assuming you did this bit and it worked? > Open a console terminal and install > > it (e. g. "conda install -chttps://conda.binstar.org/omnia cvxopt").?] Note this is a console window, in other words a Windows CMD prompt, not a Python prompt > I did everything except the last command to install cvxopt, that?s > ?conda install > -chttps://conda.binstar.org/omnia cvxopt". > > Here is the command I used: > > conda install c:/Users/eesawi0001/Downloads/cvxopt-1.1.7-py34.tar Why did you change it? The given command uses a URL. Your version uses a local file path. > I kept getting invalid syntax That suggests you are maybe using a Python prompt(>>>) rather than the CMD prompt? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From dalupus at gmail.com Thu Nov 5 08:19:01 2015 From: dalupus at gmail.com (Michael Crawford) Date: Thu, 5 Nov 2015 08:19:01 -0500 Subject: [Tutor] cvxopt install problem In-Reply-To: References: Message-ID: I tried installing this about 6 months ago on windows and gave up after about a day and just made a ubuntu vm to install it. (you can install via apt-get in ubuntu) if you are ok with reverting to 32bit anyway, I assume you aren't doing any huge number crunching so you might have a look at that as a quick option. On Thu, Nov 5, 2015 at 4:05 AM, Alan Gauld wrote: > On 04/11/15 17:43, Ek Esawi wrote: > > Caveat: This list is for help with the Python language and > standard library so any third party modules like cvxopt will > depend on whether anybody here has experience or not. Usually > you will get better results asking on the library's support forum. > > Having said that here are a few observations... > > Here is the suggestion from the posting >> > >[If you are using Windows, an easy solution > >> could be the Anaconda distribution (http://continuum.io/downloads). >> >> Select and install the 32 bit version. It will be easier to install the >> cvxopt >> package. Search it in the binstar repository ( >> https://binstar.org/search?q=cvxopt). >> > > I'm assuming you did this bit and it worked? > > Open a console terminal and install >> >> it (e. g. "conda install -chttps://conda.binstar.org/omnia cvxopt").?] >> > > Note this is a console window, in other words a Windows CMD prompt, > not a Python prompt > > I did everything except the last command to install cvxopt, that?s >> ?conda install >> -chttps://conda.binstar.org/omnia cvxopt". >> >> Here is the command I used: >> >> conda install c:/Users/eesawi0001/Downloads/cvxopt-1.1.7-py34.tar >> > > Why did you change it? > The given command uses a URL. > Your version uses a local file path. > > I kept getting invalid syntax >> > > That suggests you are maybe using a Python prompt(>>>) rather than > the CMD prompt? > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Thanks, Michael Crawford General Manager Colombian Solutions From esawiek at gmail.com Thu Nov 5 12:45:21 2015 From: esawiek at gmail.com (Ek Esawi) Date: Thu, 5 Nov 2015 12:45:21 -0500 Subject: [Tutor] cvxopt install problem Message-ID: Thank you Alan and Michael for your input. Alan was right on what happened; that?s my mistake was that I used IDEL instead of CMD to install cvx. Once I used CMD all went well. I am hoping that cvx will work now w/o any issue. I used it on matlab. I am an experienced numerical programmer but new to python. At the moment I am using cvx just for very small linear system, so speed is not important. Thanks again EK From chris_roysmith at internode.on.net Fri Nov 6 23:34:02 2015 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Sat, 7 Nov 2015 15:34:02 +1100 Subject: [Tutor] Why does this function execute before being called? Message-ID: <563D7F3A.4000809@internode.on.net> Hi, Environment: Python 2.7 Linux (Ubuntu 15.10) I am experiencing a problem with the code below running the "genF" function on opening the second window. I expected that function to be executed on clicking the 'fill text' button. The text widget gets filled on opening the window. This is my first attempt at opening a second window, so I expect I have done something stupid. #! /usr/bin/python from Tkinter import * root=Tk() def genF(ofield): for x in range(10): ofield.insert(END, x) ofield.insert(END, "\n") def second(): main=Toplevel(root) ofield=Text(main, height=15, width=15) ofield.pack() B3=Button(main, text='exit', command=main.destroy) B3.pack() B4=Button(main, text='fill text', command=genF(ofield)) B4.pack() main.mainloop() b1=Button(root, text='open second window', command=second) b1.pack() b2=Button(root, text='exit', command=root.destroy) b2.pack() root.mainloop() Thanks, Chris From shubhamsharma1318 at gmail.com Fri Nov 6 22:39:28 2015 From: shubhamsharma1318 at gmail.com (shubham sharma) Date: Sat, 7 Nov 2015 09:09:28 +0530 Subject: [Tutor] Regarding to kivy installation Message-ID: Respected sir/madam, hello to all members of python.org . I had successfully downloaded kivy on window7 64-bit version.but when i try to install it then it show me error like: Traceback (most recent call last): File "C:/Python27/kivyhello.py", line 4, in from kivy.app import App File "C:/Python27\kivy\app.py", line 316, in from kivy.base import runTouchApp, stopTouchApp File "C:/Python27\kivy\base.py", line 30, in from kivy.event import EventDispatcher File "C:/Python27\kivy\event.py", line 8, in import kivy._event ImportError: DLL load failed: %1 is not a valid Win32 application.and the application close . i kindly request to all please suggest me some ways to solve it . Thank you From alan.gauld at btinternet.com Sat Nov 7 04:18:30 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 7 Nov 2015 09:18:30 +0000 Subject: [Tutor] Why does this function execute before being called? In-Reply-To: <563D7F3A.4000809@internode.on.net> References: <563D7F3A.4000809@internode.on.net> Message-ID: On 07/11/15 04:34, Chris Roy-Smith wrote: > def genF(ofield): ... > for x in range(10): > def second(): > main=Toplevel(root) > ofield=Text(main, height=15, width=15) > ofield.pack() > B3=Button(main, text='exit', command=main.destroy) > B3.pack() > B4=Button(main, text='fill text', command=genF(ofield)) You call a function by sup[plying the parens after its name. So the function gets called here. The normal way to circumvent that in Tkinter is to use a lambda expression to defer execution, like so: B4=Button(main, text='fill text', command=lambda wgt=ofield : genF(wgt)) > B4.pack() > main.mainloop() I'm not sure you need the second mainloop. I think the root level mainloop will work for your window too. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Sat Nov 7 04:20:40 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 7 Nov 2015 09:20:40 +0000 Subject: [Tutor] Regarding to kivy installation In-Reply-To: References: Message-ID: On 07/11/15 03:39, shubham sharma wrote: > Respected sir/madam, > hello to all members of python.org . I had successfully downloaded kivy on > window7 64-bit version.but when i try to install it then it show me error > like: > Traceback (most recent call last): > File "C:/Python27/kivyhello.py", line 4, in > from kivy.app import App > File "C:/Python27\kivy\app.py", line 316, in > from kivy.base import runTouchApp, stopTouchApp > File "C:/Python27\kivy\base.py", line 30, in > from kivy.event import EventDispatcher > File "C:/Python27\kivy\event.py", line 8, in > import kivy._event > ImportError: DLL load failed: %1 is not a valid Win32 application.and the > application close . > i kindly request to all please suggest me some ways to solve it . Can you tell us exactly what you did to install it after you downloaded it? Also, I think there is a kivy support forum who may be able give more detailed help than we can on the tutor list. Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From garry.willgoose at newcastle.edu.au Fri Nov 6 20:56:16 2015 From: garry.willgoose at newcastle.edu.au (Garry Willgoose) Date: Sat, 7 Nov 2015 01:56:16 +0000 Subject: [Tutor] command line list arguments Message-ID: <26496994-764A-4373-AC9A-96FBA6540F19@newcastle.edu.au> I want to input a python list as a command line argument as for example python weathering-sens.py -daughter ['p0-50-50','p0-0-0-100?] but what I get from sys.argv is [p0-50-50,p0-0-0-100] without the string delimiters on the list elements. I?m probably missing something really simple because sys.argv returns strings and probably strips the string delimiters in that conversion ? but is there any way that I can keep the string delimiters so that inside the code I can just go (if arg is ['p0-50-50','p0-0-0-100?]) a=eval(arg) or is there no alternative to doing this python weathering-sens.py -daughter 'p0-50-50? 'p0-0-0-100? and doing the legwork of interpreting all the arguments individually (I?ve seen an example of this on the web). From kwpolska at gmail.com Sat Nov 7 06:07:50 2015 From: kwpolska at gmail.com (Chris Warrick) Date: Sat, 7 Nov 2015 12:07:50 +0100 Subject: [Tutor] command line list arguments In-Reply-To: <26496994-764A-4373-AC9A-96FBA6540F19@newcastle.edu.au> References: <26496994-764A-4373-AC9A-96FBA6540F19@newcastle.edu.au> Message-ID: On 7 November 2015 at 02:56, Garry Willgoose wrote: > I want to input a python list as a command line argument as for example > > python weathering-sens.py -daughter ['p0-50-50','p0-0-0-100?] > > but what I get from sys.argv is [p0-50-50,p0-0-0-100] without the string delimiters on the list elements. I?m probably missing something really simple because sys.argv returns strings and probably strips the string delimiters in that conversion ? but is there any way that I can keep the string delimiters so that inside the code I can just go (if arg is ['p0-50-50','p0-0-0-100?]) > > a=eval(arg) > > or is there no alternative to doing this > > python weathering-sens.py -daughter 'p0-50-50? 'p0-0-0-100? > > and doing the legwork of interpreting all the arguments individually (I?ve seen an example of this on the web). 1. NEVER use eval(). 2. Trying to pass Python code as arguments looks bad. Don?t do that. 3. Your issues with '' are caused by your shell. You would need to wrap your entire thing in quotes first, or use escaping. But instead, 4. Use argparse or another argument parsing solution, and implement it with two arguments. -- Chris Warrick PGP: 5EAAEA16 From __peter__ at web.de Sat Nov 7 06:48:52 2015 From: __peter__ at web.de (Peter Otten) Date: Sat, 07 Nov 2015 12:48:52 +0100 Subject: [Tutor] command line list arguments References: <26496994-764A-4373-AC9A-96FBA6540F19@newcastle.edu.au> Message-ID: Garry Willgoose wrote: > I want to input a python list as a command line argument as for example > > python weathering-sens.py -daughter ['p0-50-50','p0-0-0-100?] > > but what I get from sys.argv is [p0-50-50,p0-0-0-100] without the string > delimiters on the list elements. I?m probably missing something really > simple because sys.argv returns strings and probably strips the string > delimiters in that conversion ? but is there any way that I can keep the > string delimiters so that inside the code I can just go (if arg is > ['p0-50-50','p0-0-0-100?]) > > a=eval(arg) > > or is there no alternative to doing this > > python weathering-sens.py -daughter 'p0-50-50? 'p0-0-0-100? > > and doing the legwork of interpreting all the arguments individually (I?ve > seen an example of this on the web). With argparse it's really not that much legwork: $ cat weathering-sens.py import argparse parser = argparse.ArgumentParser() parser.add_argument("-d", "--daughter", nargs="+") args = parser.parse_args() print(args.daughter) $ python weathering-sens.py -d foo bar ['foo', 'bar'] $ python weathering-sens.py --daughter p0-50-50 p0-0-0-100 ['p0-50-50', 'p0-0-0-100'] Note that args.daughter is a list of strings -- no need for eval(). From chris_roysmith at internode.on.net Sat Nov 7 04:28:47 2015 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Sat, 7 Nov 2015 20:28:47 +1100 Subject: [Tutor] Why does this function execute before being called? In-Reply-To: References: <563D7F3A.4000809@internode.on.net> Message-ID: <563DC44F.2050204@internode.on.net> On 07/11/15 20:18, Alan Gauld wrote: > On 07/11/15 04:34, Chris Roy-Smith wrote: > >> def genF(ofield): ... >> for x in range(10): > >> def second(): >> main=Toplevel(root) >> ofield=Text(main, height=15, width=15) >> ofield.pack() >> B3=Button(main, text='exit', command=main.destroy) >> B3.pack() >> B4=Button(main, text='fill text', command=genF(ofield)) > > You call a function by sup[plying the parens after its name. > So the function gets called here. The normal way to circumvent > that in Tkinter is to use a lambda expression to defer execution, > like so: > > B4=Button(main, text='fill text', command=lambda wgt=ofield : genF(wgt)) This certainly wasn't obvious from what I could find on the internet. Now I see an application for Lambda > >> B4.pack() >> main.mainloop() > > I'm not sure you need the second mainloop. I think the > root level mainloop will work for your window too. > > Just tried out leaving this second mainloop, and every works the same. I had assumed I needed to create a loop the same as the top window. Thanks for clearing up this mystery From alan.gauld at btinternet.com Sat Nov 7 07:36:39 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 7 Nov 2015 12:36:39 +0000 Subject: [Tutor] Why does this function execute before being called? In-Reply-To: <563DC44F.2050204@internode.on.net> References: <563D7F3A.4000809@internode.on.net> <563DC44F.2050204@internode.on.net> Message-ID: On 07/11/15 09:28, Chris Roy-Smith wrote: >> B4=Button(main, text='fill text', command=lambda wgt=ofield : genF(wgt)) > > This certainly wasn't obvious from what I could find on the internet. > Now I see an application for Lambda I should point out you can use a def if you prefer: def second(): main=Toplevel(root) ofield=Text(main, height=15, width=15) ofield.pack() B3=Button(main, text='exit', command=main.destroy) B3.pack() def genFwrapper(widget = ofield): return genF(widget) B4=Button(main, text='fill text', command=genFwrapper) B4.pack() The lambda just saves a line. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From sjeik_appie at hotmail.com Sat Nov 7 07:53:11 2015 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Sat, 7 Nov 2015 12:53:11 +0000 Subject: [Tutor] question about descriptors Message-ID: p, li { white-space: pre-wrap; } Hi, First, before I forget, emails from hotmail/yahoo etc appear to end up in the spam folder these days, so apologies in advance if I do not appear to follow up to your replies. Ok, now to my question. I want to create a class with read-only attribute access to the columns of a .csv file. E.g. when a file has a column named 'a', that column should be returned as list by using instance.a. At first I thought I could do this with the builtin 'property' class, but I am not sure how. I now tried to use descriptors (__get__ and __set__), which are also used by ' property' (See also: https://docs.python.org/2/howto/descriptor.html). In the " if __name__ == '__main__'" section, [a] is supposed to be a shorthand for == equivalent to [b]. But it's not.I suspect it has to do with the way attributes are looked up. So once an attribute has been found in self.__dict__ aka "the usual place", the search stops, and __get__ is never called. But I may be wrong. I find the __getatttribute__, __getattr__ and __get__ distinction quite confusing. What is the best approach to do this? Ideally, the column values should only be retrieved when they are actually requested (the .csv could be big). Thanks in advance! import csv from cStringIO import StringIO class AttrAccess(object): def __init__(self, fileObj): self.__reader = csv.reader(fileObj, delimiter=";") self.__header = self.__reader.next() #[setattr(self, name, self.__get_column(name)) for name in self.header] self.a = range(10) @property def header(self): return self.__header def __get_column(self, name): return [record[self.header.index(name)] for record in self.__reader] # generator expression might be better here. def __get__(self, obj, objtype=type): print "__get__ called" return self.__get_column(obj) #return getattr(self, obj) def __set__(self, obj, val): raise AttributeError("Can't set attribute") if __name__ == " __main__": f = StringIO("a;b;c\n1;2;3\n4;5;6\n7;8;9\n") instance = AttrAccess(f) print instance.a # [a] does not call __get__. Looks, and finds, in self.__dict__? print instance.__get__("a") # [b] this is supposed to be equivalent to [a] instance.a = 42 # should throw AttributeError! From alan.gauld at btinternet.com Sat Nov 7 08:31:34 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 7 Nov 2015 13:31:34 +0000 Subject: [Tutor] question about descriptors In-Reply-To: References: Message-ID: On 07/11/15 12:53, Albert-Jan Roskam wrote: > Ok, now to my question. > I want to create a class with read-only attribute access > to the columns of a .csv file. Can you clarify what you mean by that? The csvreader is by definition read only. So is it the in-memory model that you want read-only? Except you don't really have an in-memory model that I can see? > E.g. when a file has a column named 'a', that column should > be returned as list by using instance.a. That appears to be a separate issue to whether the returned list is read-only or not? As ever the issue of dynamically naming variables at run time and then figuring out how to access them later raises its head. Its hardly ever a good plan. > At first I thought I could do this with the builtin 'property' > class, but I am not sure how. To use property I think you'd need to know the names of your columns in advance. (Or dynamically build your classes) > I now tried to use descriptors (__get__ and __set__), > which are also used by ' property' > In the " if __name__ == '__main__'" section, [a] is supposed > to be a shorthand for == equivalent to [b]. I have no idea what you mean by that sentence? > class AttrAccess(object): > > def __init__(self, fileObj): > self.__reader = csv.reader(fileObj, delimiter=";") > self.__header = self.__reader.next() > @property > def header(self): > return self.__header > > def __get_column(self, name): > return [record[self.header.index(name)] for record in self.__reader] # generator expression might be better here. You should only get the index once otherwise it could add a lot of time for a long file(especially if there were a lot of columns) def __get_column(self, name): idx = self.header.index(name) return [record[idx] for record in self.__reader] > def __get__(self, obj, objtype=type): > print "__get__ called" > return self.__get_column(obj) > #return getattr(self, obj) > > def __set__(self, obj, val): > raise AttributeError("Can't set attribute") If you want everything read-only should this not be __setattr__()? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From lac at openend.se Sat Nov 7 08:51:21 2015 From: lac at openend.se (Laura Creighton) Date: Sat, 07 Nov 2015 14:51:21 +0100 Subject: [Tutor] Regarding to kivy installation In-Reply-To: References: Message-ID: <201511071351.tA7DpL0f003466@fido.openend.se> In a message of Sat, 07 Nov 2015 09:09:28 +0530, shubham sharma writes: >Respected sir/madam, >hello to all members of python.org . I had successfully downloaded kivy on >window7 64-bit version.but when i try to install it then it show me error >like: > Traceback (most recent call last): > File "C:/Python27/kivyhello.py", line 4, in > from kivy.app import App > File "C:/Python27\kivy\app.py", line 316, in > from kivy.base import runTouchApp, stopTouchApp > File "C:/Python27\kivy\base.py", line 30, in > from kivy.event import EventDispatcher > File "C:/Python27\kivy\event.py", line 8, in > import kivy._event > ImportError: DLL load failed: %1 is not a valid Win32 application.and the >application close . >i kindly request to all please suggest me some ways to solve it . >Thank you Your problem is likely that you need to set your path properly so the libraries can be found. http://stackoverflow.com/questions/14629818/importerror-dll-load-failed-1-is-not-a-valid-win32-application I am not a windows user, but https://docs.python.org/3/using/windows.html says how you are to configure your PATH. And this mailing list is full of windows users, all of whom can probably explain this a whole lot better than I, or the docs can, given that they know this is the (likely) problem. Laura From __peter__ at web.de Sat Nov 7 09:03:44 2015 From: __peter__ at web.de (Peter Otten) Date: Sat, 07 Nov 2015 15:03:44 +0100 Subject: [Tutor] question about descriptors References: Message-ID: Albert-Jan Roskam wrote: > > > p, li { white-space: pre-wrap; } > > Hi, > First, before I forget, emails from hotmail/yahoo etc appear to end up in > the spam folder these days, so apologies in advance if I do not appear to > follow up to your replies. Ok, now to my question. I want to create a > class with read-only attribute access to the columns of a .csv file. E.g. > when a file has a column named 'a', that column should be returned as list > by using instance.a. At first I thought I could do this with the builtin > 'property' class, but I am not sure how. I now tried to use descriptors > (__get__ and __set__), which are also used by ' property' (See also: > https://docs.python.org/2/howto/descriptor.html). > > In the " if __name__ == '__main__'" section, [a] is supposed to be a > shorthand for == equivalent to [b]. But it's not.I suspect it has to do > with the way attributes are looked up. So once an attribute has been found > in self.__dict__ aka "the usual place", the search stops, and __get__ is > never called. But I may be wrong. I find the __getatttribute__, > __getattr__ and __get__ distinction quite confusing. What is the best > approach to do this? Ideally, the column values should only be retrieved > when they are actually requested (the .csv could be big). Thanks in > advance! > > > > import csv > from cStringIO import StringIO > > > class AttrAccess(object): > > > def __init__(self, fileObj): > self.__reader = csv.reader(fileObj, delimiter=";") > self.__header = self.__reader.next() > #[setattr(self, name, self.__get_column(name)) for name in > #[self.header] > self.a = range(10) > > > @property > def header(self): > return self.__header > > def __get_column(self, name): > return [record[self.header.index(name)] for record in > self.__reader] # generator expression might be better here. > > def __get__(self, obj, objtype=type): > print "__get__ called" > return self.__get_column(obj) > #return getattr(self, obj) > > def __set__(self, obj, val): > raise AttributeError("Can't set attribute") > > if __name__ == " __main__": > f = StringIO("a;b;c\n1;2;3\n4;5;6\n7;8;9\n") > instance = AttrAccess(f) > print instance.a # [a] does not call __get__. Looks, and finds, in > self.__dict__? > print instance.__get__("a") # [b] this is supposed to be equivalent > to [a] > instance.a = 42 # should throw AttributeError! I think the basic misunderstandings are that (1) the __get__() method has to be implemented by the descriptor class (2) the descriptor instances should be attributes of the class that is supposed to invoke __get__(). E. g.: class C(object): x = decriptor() c = C() c.x # invoke c.x.__get__(c, C) under the hood. As a consequence you need one class per set of attributes, instantiating the same AttrAccess for csv files with differing layouts won't work. Here's how to do it all by yourself: class ReadColumn(object): def __init__(self, index): self._index = index def __get__(self, obj, type=None): return obj._row[self._index] def __set__(self, obj, value): raise AttributeError("oops") def first_row(instream): reader = csv.reader(instream, delimiter=";") class Row(object): def __init__(self, row): self._row = row for i, header in enumerate(next(reader)): setattr(Row, header, ReadColumn(i)) return Row(next(reader)) f = StringIO("a;b;c\n1;2;3\n4;5;6\n7;8;9\n") row = first_row(f) print row.a row.a = 42 Instead of a custom descriptor you can of course use the built-in property: for i, header in enumerate(next(reader)): setattr(Row, header, property(lambda self, i=i: self._row[i])) In many cases you don't care about the specifics of the row class and use collections.namedtuple: def rows(instream): reader = csv.reader(instream, delimiter=";") Row = collections.namedtuple("Row", next(reader)) return itertools.imap(Row._make, reader) f = StringIO("a;b;c\n1;2;3\n4;5;6\n7;8;9\n") row = next(rows(f)) print row.a row.a = 42 From steve at pearwood.info Sat Nov 7 09:24:58 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 8 Nov 2015 01:24:58 +1100 Subject: [Tutor] question about descriptors In-Reply-To: References: Message-ID: <20151107142458.GS10946@ando.pearwood.info> On Sat, Nov 07, 2015 at 12:53:11PM +0000, Albert-Jan Roskam wrote: [...] > Ok, now to my question. I want to create a class with read-only > attribute access to the columns of a .csv file. E.g. when a file has a > column named 'a', that column should be returned as list by using > instance.a. At first I thought I could do this with the builtin > 'property' class, but I am not sure how. 90% of problems involving computed attributes (including "read-only" attributes) are most conveniently solved with `property`, but I think this may be an exception. Nevertheless, I'll give you a solution in terms of `property` first. I'm too busy/lazy to handle reading from a CSV file, so I'll fake it with a dict of columns. class ColumnView(object): _data = {'a': [1, 2, 3, 4, 5, 6], 'b': [1, 2, 4, 8, 16, 32], 'c': [1, 10, 100, 1000, 10000, 100000], } @property def a(self): return self._data['a'][:] @property def b(self): return self._data['b'][:] @property def c(self): return self._data['c'][:] And in use: py> cols = ColumnView() py> cols.a [1, 2, 3, 4, 5, 6] py> cols.a = [] Traceback (most recent call last): File "", line 1, in ? AttributeError: can't set attribute Now, some comments: (1) You must inherit from `object` for this to work. (Or use Python 3.) It won't work if you just say "class ColumnView:", which would make it a so-called "classic" or "old-style" class. You don't want that. (2) Inside the property getter functions, I make a copy of the lists before returning them. That is, I do: return self._data['c'][:] rather than: return self._data['c'] The empty slice [:] makes a copy. If I did not do this, you could mutate the list (say, by appending a value to it, or deleting items from it) and that mutation would show up the next time you looked at the column. (3) It's very tedious having to create a property for each column ahead of time. But we can do this instead: def make_getter(key): def inner(self): return self._data[key][:] inner.__name__ = key return property(inner) class ColumnView(object): _data = {'a': [1, 2, 3, 4, 5, 6], 'b': [1, 2, 4, 8, 16, 32], 'c': [1, 10, 100, 1000, 10000, 100000], } for key in _data: locals()[key] = make_getter(key) del key and it works as above, but without all the tedious manual creation of property getters. Do you understand how this operates? If not, ask, and someone will explain. (And yes, this is one of the few times that writing to locals() actually works!) (4) But what if you don't know what the columns are called ahead of time? You can't use property, or descriptors, because you don't know what to call the damn things until you know what the column headers are, and by the time you know that, the class is already well and truly created. You might think you can do this: class ColumnView(object): def __init__(self): # read the columns from the CSV file self._data = ... # now create properties to suit for key in self._data: setattr(self, key, property( ... )) but that doesn't work. Properties only perform their "magic" when they are attached to the class itself. By setting them as attributes on the instance (self), they lose their power and just get treated as ordinary attributes. To be technical, we say that the descriptor protocol is only enacted when the attribute is found in the class, not in the instance. You might be tempted to write this instead: setattr(self.__class__, key, property( ... )) but that's even worse. Now, every time you create a new ColumnView instance, *all the other instances will change*. They will grown new properties, or overwrite existing properties. You don't want that. Fortunately, Python has an mechanism for solving this problem: the `__getattr__` method and friends. class ColumnView(object): _data = {'a': [1, 2, 3, 4, 5, 6], 'b': [1, 2, 4, 8, 16, 32], 'c': [1, 10, 100, 1000, 10000, 100000], } def __getattr__(self, name): if name in self._data: return self._data[name][:] else: raise AttributeError def __setattr__(self, name, value): if name in self._data: raise AttributeError('read-only attribute') super(ColumnView, self).__setattr__(name, value) def __delattr__(self, name): if name in self._data: raise AttributeError('read-only attribute') super(ColumnView, self).__delattr__(name) -- Steve From jjhartley at gmail.com Sat Nov 7 17:09:19 2015 From: jjhartley at gmail.com (James Hartley) Date: Sat, 7 Nov 2015 16:09:19 -0600 Subject: [Tutor] relative imports within a package? Message-ID: The Python 3 tutorial discusses relative imports at: https://docs.python.org/3/tutorial/modules.html#intra-package-references I have the following directory structure for a package in development: + outer_package/ + __init__.py + inner_package | + __init__.py | + myclass.py + collateral_directory + arbitrary_tool.py arbitrary_tool.py needs to instantiate the class MyClass found in myclass.py. I have made MyClass accessible by adding the following to inner_package/__init__.py: from myclass import MyClass Within arbitrary_tool.py, I have attempted the following: from ..inner_package import MyClass ...but executing with Python 3.4 yields the following stack trace: Traceback (most recent call last): File "./arbitrary_tool.py", line 5, in from ..inner_package import MyClass SystemError: Parent module '' not loaded, cannot perform relative import Suggestions on how to solve this would be most certainly welcomed! From steve at pearwood.info Sat Nov 7 20:17:12 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 8 Nov 2015 12:17:12 +1100 Subject: [Tutor] relative imports within a package? In-Reply-To: References: Message-ID: <20151108011712.GT10946@ando.pearwood.info> On Sat, Nov 07, 2015 at 04:09:19PM -0600, James Hartley wrote: > The Python 3 tutorial discusses relative imports at: > > https://docs.python.org/3/tutorial/modules.html#intra-package-references > > I have the following directory structure for a package in development: > > + outer_package/ > + __init__.py > + inner_package > | + __init__.py > | + myclass.py > + collateral_directory > + arbitrary_tool.py As shown above, "collateral_directory" is not part of the package. It is just a directory with stuff in it. The fact that the "stuff" happens to include a Python script is irrelevant. If "arbitrary_tool.py" is actually independent of outer_package, you should have this layout: + arbitrary_tool.py + outer_package/ + __init__.py + inner_package | + __init__.py | + myclass.py where the enclosing directory is part of your PYTHONPATH. Then, in arbitrary_tool, you say: from outer_package import MyClass # assuming outer_package has already made this available or from outer_package.inner_packages import MyClass # assuming inner_package has made this available # which you have done or from outer_package.inner_package.myclass import MyClass whichever you prefer. If arbitrary_tool is not independent of outer_package, then you should convert it to a package with this layout: + outer_package/ + __init__.py + inner_package | + __init__.py | + myclass.py + collateral_directory + __init__.py + arbitrary_tool.py __init__.py may be an empty file, it just has to exist. Now collateral_directory is a sub-package, and you can do things like this: # from outer_package.__init__ import collateral_directory.arbitrary_tool # from inner_package.myclass (but watch out for circular imports) from ..collateral_directory.arbitrary_tool import Spam But if arbitrary_tool is part of the package, and it is the only file in collateral_directory, why mess about with a subpackage? Lay your package out like this: + outer_package/ + __init__.py + arbitrary_tool.py + inner_package | + __init__.py | + myclass.py and inside arbitrary_tool say: from inner_package.myclass import MyClass (P.S. this is Python, not Java. There is no requirement to put every class in its own file, in fact doing so is mildly discouraged. Does MyClass truly deserve its own file? If not, you may be able to simplify your package structure even more.) -- Steve From kfh777 at earthlink.net Tue Nov 10 12:38:27 2015 From: kfh777 at earthlink.net (Ken Hammer) Date: Tue, 10 Nov 2015 12:38:27 -0500 Subject: [Tutor] Run Python 2.7 on Android Tablet Message-ID: My MIT OCW 6.00SC course is installed on a Windows 8.1 desktop machine. I'd like to study and explore away from my office and desk on my tablet running Android 4.2.2. Possible? thanks, Ken From alan.gauld at btinternet.com Tue Nov 10 16:07:07 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 10 Nov 2015 21:07:07 +0000 Subject: [Tutor] Run Python 2.7 on Android Tablet In-Reply-To: References: Message-ID: On 10/11/15 17:38, Ken Hammer wrote: > > My MIT OCW 6.00SC course is installed on a Windows 8.1 desktop machine. > > I'd like to study and explore away from my office and desk on my tablet running Android 4.2.2. Possible? I can't comment on your course but you can install Python on Android and for basic programming tasks it works just like any other Python. I use QPython FWIW but I think there are others too. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From lac at openend.se Tue Nov 10 16:32:27 2015 From: lac at openend.se (Laura Creighton) Date: Tue, 10 Nov 2015 22:32:27 +0100 Subject: [Tutor] Run Python 2.7 on Android Tablet In-Reply-To: References: Message-ID: <201511102132.tAALWRtP022562@fido.openend.se> In a message of Tue, 10 Nov 2015 12:38:27 -0500, "Ken Hammer" writes: > > >My MIT OCW 6.00SC course is installed on a Windows 8.1 desktop machine. > >I'd like to study and explore away from my office and desk on my tablet running Android 4.2.2. Possible? > >thanks, Ken Yes. use Python Anywhere in a browser on your tablet. https://www.pythonanywhere.com/ Laura From memilanuk at gmail.com Tue Nov 10 17:10:04 2015 From: memilanuk at gmail.com (memilanuk) Date: Tue, 10 Nov 2015 14:10:04 -0800 Subject: [Tutor] Run Python 2.7 on Android Tablet In-Reply-To: <201511102132.tAALWRtP022562@fido.openend.se> References: <201511102132.tAALWRtP022562@fido.openend.se> Message-ID: On 11/10/2015 01:32 PM, Laura Creighton wrote: > In a message of Tue, 10 Nov 2015 12:38:27 -0500, "Ken Hammer" writes: >> >> >> My MIT OCW 6.00SC course is installed on a Windows 8.1 desktop machine. >> >> I'd like to study and explore away from my office and desk on my tablet running Android 4.2.2. Possible? >> >> thanks, Ken > > Yes. > use Python Anywhere in a browser on your tablet. > > https://www.pythonanywhere.com/ > > https://mail.python.org/mailman/listinfo/tutor > Or koding.io... but PythonAnywhere is nicer for straight-up python. -- Shiny! Let's be bad guys. Reach me @ memilanuk (at) gmail dot com From sjeik_appie at hotmail.com Wed Nov 11 03:08:46 2015 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Wed, 11 Nov 2015 08:08:46 +0000 Subject: [Tutor] question about descriptors In-Reply-To: References: , Message-ID: > I think the basic misunderstandings are that > > (1) the __get__() method has to be implemented by the descriptor class > (2) the descriptor instances should be attributes of the class that is > supposed to invoke __get__(). E. g.: > > class C(object): > x = decriptor() > > c = C() > > c.x # invoke c.x.__get__(c, C) under the hood. Exactly right, that was indeed my misunderstanding! I was thinking about __get__ and __set__ in the same terms as e.g. __getitem__ and __setitem__ > As a consequence you need one class per set of attributes, instantiating the > same AttrAccess for csv files with differing layouts won't work. That is no problem at all for me. One instance per file will be fine. > Here's how to do it all by yourself: > > class ReadColumn(object): > def __init__(self, index): > self._index = index > def __get__(self, obj, type=None): > return obj._row[self._index] > def __set__(self, obj, value): > raise AttributeError("oops") This appears to return one value, whereas I wanted I wanted to return all values of a column, ie as many values as there are rows. But the logic probably won't change. Same applies to the use of namedtuple, I suppose (?). I have never used namedtuple like namedtuple("Column", self.header)(*self.columns). > def first_row(instream): > reader = csv.reader(instream, delimiter=";") > > class Row(object): > def __init__(self, row): > self._row = row > > for i, header in enumerate(next(reader)): > setattr(Row, header, ReadColumn(i)) > > return Row(next(reader)) > > > f = StringIO("a;b;c\n1;2;3\n4;5;6\n7;8;9\n") > row = first_row(f) > print row.a > row.a = 42 > > Instead of a custom descriptor you can of course use the built-in property: > > for i, header in enumerate(next(reader)): > setattr(Row, header, property(lambda self, i=i: self._row[i])) This seems most attractive/straightforward to me. > In many cases you don't care about the specifics of the row class and use > collections.namedtuple: > > > def rows(instream): > reader = csv.reader(instream, delimiter=";") > Row = collections.namedtuple("Row", next(reader)) > return itertools.imap(Row._make, reader) > > > f = StringIO("a;b;c\n1;2;3\n4;5;6\n7;8;9\n") > row = next(rows(f)) > print row.a > row.a = 42 Thanks a lot for helping me! From sjeik_appie at hotmail.com Wed Nov 11 03:33:17 2015 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Wed, 11 Nov 2015 08:33:17 +0000 Subject: [Tutor] question about descriptors In-Reply-To: <20151107142458.GS10946@ando.pearwood.info> References: , <20151107142458.GS10946@ando.pearwood.info> Message-ID: > Date: Sun, 8 Nov 2015 01:24:58 +1100 > From: steve at pearwood.info > To: tutor at python.org > Subject: Re: [Tutor] question about descriptors > > On Sat, Nov 07, 2015 at 12:53:11PM +0000, Albert-Jan Roskam wrote: > > [...] > > Ok, now to my question. I want to create a class with read-only > > attribute access to the columns of a .csv file. E.g. when a file has a > > column named 'a', that column should be returned as list by using > > instance.a. At first I thought I could do this with the builtin > > 'property' class, but I am not sure how. > > 90% of problems involving computed attributes (including "read-only" > attributes) are most conveniently solved with `property`, but I think > this may be an exception. Nevertheless, I'll give you a solution in > terms of `property` first. > > I'm too busy/lazy to handle reading from a CSV file, so I'll fake it > with a dict of columns. Actually, I want to make this work for any iterable, as long as I can get the header names and as long as it returns one record per iteration. > class ColumnView(object): > _data = {'a': [1, 2, 3, 4, 5, 6], > 'b': [1, 2, 4, 8, 16, 32], > 'c': [1, 10, 100, 1000, 10000, 100000], > } > @property > def a(self): > return self._data['a'][:] > @property > def b(self): > return self._data['b'][:] > @property > def c(self): > return self._data['c'][:] Interesting. I never would have thought to define a separate class for this. > And in use: > > py> cols = ColumnView() > py> cols.a > [1, 2, 3, 4, 5, 6] > py> cols.a = [] > Traceback (most recent call last): > File "", line 1, in ? > AttributeError: can't set attribute > > > > Now, some comments: > > (1) You must inherit from `object` for this to work. (Or use Python 3.) > It won't work if you just say "class ColumnView:", which would make it a > so-called "classic" or "old-style" class. You don't want that. Are there any use cases left where one still must use old-style classes? Or should new code always inherit from object (unless one want to inherit from another "true" class, of course). > (2) Inside the property getter functions, I make a copy of the lists > before returning them. That is, I do: > > return self._data['c'][:] > > rather than: > > return self._data['c'] > > > The empty slice [:] makes a copy. If I did not do this, you could mutate > the list (say, by appending a value to it, or deleting items from it) > and that mutation would show up the next time you looked at the column. These mutability problems always make me pull my hair out! :-) I like the [:] notation, but: In [1]: giant = range(10 ** 7) In [2]: %timeit copy1 = giant[:] 10 loops, best of 3: 97 ms per loop In [3]: from copy import copy In [4]: %timeit copy2 = copy(giant) 10 loops, best of 3: 90 ms per loop In [5]: import copy In [6]: %timeit copy2 = copy.copy(giant) 10 loops, best of 3: 88.6 ms per loop Hmmm, wicked, when I looked earlier this week the difference appear to be bigger. > (3) It's very tedious having to create a property for each column ahead > of time. But we can do this instead: > > > def make_getter(key): > def inner(self): > return self._data[key][:] > inner.__name__ = key > return property(inner) > > > class ColumnView(object): > _data = {'a': [1, 2, 3, 4, 5, 6], > 'b': [1, 2, 4, 8, 16, 32], > 'c': [1, 10, 100, 1000, 10000, 100000], > } > for key in _data: > locals()[key] = make_getter(key) > del key > > > and it works as above, but without all the tedious manual creation of > property getters. > > Do you understand how this operates? If not, ask, and someone will > explain. (And yes, this is one of the few times that writing to locals() > actually works!) I think so. I still plan to write several working implementations to get a better idea about which strategy to choose. > (4) But what if you don't know what the columns are called ahead of > time? You can't use property, or descriptors, because you don't know > what to call the damn things until you know what the column headers are, > and by the time you know that, the class is already well and truly > created. You might think you can do this: > > class ColumnView(object): > def __init__(self): > # read the columns from the CSV file > self._data = ... > # now create properties to suit > for key in self._data: > setattr(self, key, property( ... )) > > > but that doesn't work. Properties only perform their "magic" when they > are attached to the class itself. By setting them as attributes on the > instance (self), they lose their power and just get treated as ordinary > attributes. To be technical, we say that the descriptor protocol is only > enacted when the attribute is found in the class, not in the instance. Ha! That is indeed exactly what I tried! :-)) > You might be tempted to write this instead: > > setattr(self.__class__, key, property( ... )) I thought about defining a classmethod, then inside it do setattr(cls, key, property( ... )) But that is probably the same? > but that's even worse. Now, every time you create a new ColumnView > instance, *all the other instances will change*. They will grown new > properties, or overwrite existing properties. You don't want that. > > Fortunately, Python has an mechanism for solving this problem: > the `__getattr__` method and friends. > > > class ColumnView(object): > _data = {'a': [1, 2, 3, 4, 5, 6], > 'b': [1, 2, 4, 8, 16, 32], > 'c': [1, 10, 100, 1000, 10000, 100000], > } > def __getattr__(self, name): > if name in self._data: > return self._data[name][:] > else: > raise AttributeError > def __setattr__(self, name, value): > if name in self._data: > raise AttributeError('read-only attribute') > super(ColumnView, self).__setattr__(name, value) > def __delattr__(self, name): > if name in self._data: > raise AttributeError('read-only attribute') > super(ColumnView, self).__delattr__(name) That also seems very straightforward. Why does "if name in self._data:" not cause a recursion? self._data calls __getattr__, which has self._data in it, which...etc. From ulhaqz at gmail.com Wed Nov 11 07:18:06 2015 From: ulhaqz at gmail.com (Burhan ul haq) Date: Wed, 11 Nov 2015 17:18:06 +0500 Subject: [Tutor] Tutor Digest, Vol 141, Issue 11 In-Reply-To: References: Message-ID: Continuing "Run Python 2.7 on Android Tablet" Hi, I am constrained to install anything on my official laptop, therefore I need to have an "online life saver" for Python Learning. I have already tried "pythonanywhere" but could not get it going, even for a basic "hello world" script. I could not locate any basic documentation to help me with. The basic help tab "I want to start learning Python " bring you back to the main dashboard. Can anybody share a basic how to get started ... Many Thanks / On Wed, Nov 11, 2015 at 1:34 PM, wrote: > Send Tutor mailing list submissions to > tutor at python.org > > To subscribe or unsubscribe via the World Wide Web, visit > https://mail.python.org/mailman/listinfo/tutor > or, via email, send a message with subject or body 'help' to > tutor-request at python.org > > You can reach the person managing the list at > tutor-owner at python.org > > When replying, please edit your Subject line so it is more specific > than "Re: Contents of Tutor digest..." > > > Today's Topics: > > 1. Run Python 2.7 on Android Tablet (Ken Hammer) > 2. Re: Run Python 2.7 on Android Tablet (Alan Gauld) > 3. Re: Run Python 2.7 on Android Tablet (Laura Creighton) > 4. Re: Run Python 2.7 on Android Tablet (memilanuk) > 5. Re: question about descriptors (Albert-Jan Roskam) > 6. Re: question about descriptors (Albert-Jan Roskam) > > > ---------------------------------------------------------------------- > > Message: 1 > Date: Tue, 10 Nov 2015 12:38:27 -0500 > From: "Ken Hammer" > To: tutor at python.org > Subject: [Tutor] Run Python 2.7 on Android Tablet > Message-ID: > > > My MIT OCW 6.00SC course is installed on a Windows 8.1 desktop machine. > > I'd like to study and explore away from my office and desk on my tablet > running Android 4.2.2. Possible? > > thanks, Ken > > > > ------------------------------ > > Message: 2 > Date: Tue, 10 Nov 2015 21:07:07 +0000 > From: Alan Gauld > To: tutor at python.org > Subject: Re: [Tutor] Run Python 2.7 on Android Tablet > Message-ID: > Content-Type: text/plain; charset=utf-8; format=flowed > > On 10/11/15 17:38, Ken Hammer wrote: > > > > My MIT OCW 6.00SC course is installed on a Windows 8.1 desktop machine. > > > > I'd like to study and explore away from my office and desk on my tablet > running Android 4.2.2. Possible? > > I can't comment on your course but you can install Python on Android and > for basic programming tasks it works just like any other Python. > I use QPython FWIW but I think there are others too. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > > > ------------------------------ > > Message: 3 > Date: Tue, 10 Nov 2015 22:32:27 +0100 > From: Laura Creighton > To: Ken Hammer > Cc: tutor at python.org, lac at openend.se > Subject: Re: [Tutor] Run Python 2.7 on Android Tablet > Message-ID: <201511102132.tAALWRtP022562 at fido.openend.se> > Content-Type: text/plain; charset="us-ascii" > > In a message of Tue, 10 Nov 2015 12:38:27 -0500, "Ken Hammer" writes: > > > > > >My MIT OCW 6.00SC course is installed on a Windows 8.1 desktop machine. > > > >I'd like to study and explore away from my office and desk on my tablet > running Android 4.2.2. Possible? > > > >thanks, Ken > > Yes. > use Python Anywhere in a browser on your tablet. > > https://www.pythonanywhere.com/ > > Laura > > > ------------------------------ > > Message: 4 > Date: Tue, 10 Nov 2015 14:10:04 -0800 > From: memilanuk > To: tutor at python.org > Subject: Re: [Tutor] Run Python 2.7 on Android Tablet > Message-ID: > Content-Type: text/plain; charset=windows-1252; format=flowed > > On 11/10/2015 01:32 PM, Laura Creighton wrote: > > In a message of Tue, 10 Nov 2015 12:38:27 -0500, "Ken Hammer" writes: > >> > >> > >> My MIT OCW 6.00SC course is installed on a Windows 8.1 desktop machine. > >> > >> I'd like to study and explore away from my office and desk on my tablet > running Android 4.2.2. Possible? > >> > >> thanks, Ken > > > > Yes. > > use Python Anywhere in a browser on your tablet. > > > > https://www.pythonanywhere.com/ > > > > > https://mail.python.org/mailman/listinfo/tutor > > > > Or koding.io... but PythonAnywhere is nicer for straight-up python. > > -- > Shiny! Let's be bad guys. > > Reach me @ memilanuk (at) gmail dot com > > > > ------------------------------ > > Message: 5 > Date: Wed, 11 Nov 2015 08:08:46 +0000 > From: Albert-Jan Roskam > To: Peter Otten <__peter__ at web.de>, "tutor at python.org" > > Subject: Re: [Tutor] question about descriptors > Message-ID: > Content-Type: text/plain; charset="windows-1256" > > > > > > I think the basic misunderstandings are that > > > > (1) the __get__() method has to be implemented by the descriptor class > > (2) the descriptor instances should be attributes of the class that is > > supposed to invoke __get__(). E. g.: > > > > class C(object): > > x = decriptor() > > > > c = C() > > > > c.x # invoke c.x.__get__(c, C) under the hood. > > Exactly right, that was indeed my misunderstanding! I was thinking about > __get__ and __set__ in the same terms as e.g. __getitem__ and __setitem__ > > > > As a consequence you need one class per set of attributes, instantiating > the > > same AttrAccess for csv files with differing layouts won't work. > > That is no problem at all for me. One instance per file will be fine. > > > > Here's how to do it all by yourself: > > > > class ReadColumn(object): > > def __init__(self, index): > > self._index = index > > def __get__(self, obj, type=None): > > return obj._row[self._index] > > def __set__(self, obj, value): > > raise AttributeError("oops") > > This appears to return one value, whereas I wanted I wanted to return all > values of a column, ie as many values as there are rows. > But the logic probably won't change. Same applies to the use of > namedtuple, I suppose (?). I have never used namedtuple like > namedtuple("Column", self.header)(*self.columns). > > > > def first_row(instream): > > reader = csv.reader(instream, delimiter=";") > > > > class Row(object): > > def __init__(self, row): > > self._row = row > > > > for i, header in enumerate(next(reader)): > > setattr(Row, header, ReadColumn(i)) > > > > return Row(next(reader)) > > > > > > f = StringIO("a;b;c\n1;2;3\n4;5;6\n7;8;9\n") > > row = first_row(f) > > print row.a > > row.a = 42 > > > > Instead of a custom descriptor you can of course use the built-in > property: > > > > for i, header in enumerate(next(reader)): > > setattr(Row, header, property(lambda self, i=i: self._row[i])) > > This seems most attractive/straightforward to me. > > > In many cases you don't care about the specifics of the row class and use > > collections.namedtuple: > > > > > > def rows(instream): > > reader = csv.reader(instream, delimiter=";") > > Row = collections.namedtuple("Row", next(reader)) > > return itertools.imap(Row._make, reader) > > > > > > f = StringIO("a;b;c\n1;2;3\n4;5;6\n7;8;9\n") > > row = next(rows(f)) > > print row.a > > row.a = 42 > > Thanks a lot for helping me! > > > > > ------------------------------ > > Message: 6 > Date: Wed, 11 Nov 2015 08:33:17 +0000 > From: Albert-Jan Roskam > To: Steven D'Aprano , "tutor at python.org" > > Subject: Re: [Tutor] question about descriptors > Message-ID: > Content-Type: text/plain; charset="windows-1256" > > > Date: Sun, 8 Nov 2015 01:24:58 +1100 > > From: steve at pearwood.info > > To: tutor at python.org > > Subject: Re: [Tutor] question about descriptors > > > > On Sat, Nov 07, 2015 at 12:53:11PM +0000, Albert-Jan Roskam wrote: > > > > [...] > > > Ok, now to my question. I want to create a class with read-only > > > attribute access to the columns of a .csv file. E.g. when a file has a > > > column named 'a', that column should be returned as list by using > > > instance.a. At first I thought I could do this with the builtin > > > 'property' class, but I am not sure how. > > > > 90% of problems involving computed attributes (including "read-only" > > attributes) are most conveniently solved with `property`, but I think > > this may be an exception. Nevertheless, I'll give you a solution in > > terms of `property` first. > > > > I'm too busy/lazy to handle reading from a CSV file, so I'll fake it > > with a dict of columns. > > Actually, I want to make this work for any iterable, as long as I can get > the header names and as long as it returns one record per iteration. > > > > class ColumnView(object): > > _data = {'a': [1, 2, 3, 4, 5, 6], > > 'b': [1, 2, 4, 8, 16, 32], > > 'c': [1, 10, 100, 1000, 10000, 100000], > > } > > @property > > def a(self): > > return self._data['a'][:] > > @property > > def b(self): > > return self._data['b'][:] > > @property > > def c(self): > > return self._data['c'][:] > > Interesting. I never would have thought to define a separate class for > this. > > > > And in use: > > > > py> cols = ColumnView() > > py> cols.a > > [1, 2, 3, 4, 5, 6] > > py> cols.a = [] > > Traceback (most recent call last): > > File "", line 1, in ? > > AttributeError: can't set attribute > > > > > > > > Now, some comments: > > > > (1) You must inherit from `object` for this to work. (Or use Python 3.) > > It won't work if you just say "class ColumnView:", which would make it a > > so-called "classic" or "old-style" class. You don't want that. > > Are there any use cases left where one still must use old-style classes? > Or should new code always inherit from object (unless one want to inherit > from another "true" class, of course). > > > > (2) Inside the property getter functions, I make a copy of the lists > > before returning them. That is, I do: > > > > return self._data['c'][:] > > > > rather than: > > > > return self._data['c'] > > > > > > The empty slice [:] makes a copy. If I did not do this, you could mutate > > the list (say, by appending a value to it, or deleting items from it) > > and that mutation would show up the next time you looked at the column. > > These mutability problems always make me pull my hair out! :-) I like the > [:] notation, but: > > In [1]: giant = range(10 ** 7) > > In [2]: %timeit copy1 = giant[:] > 10 loops, best of 3: 97 ms per loop > > In [3]: from copy import copy > > In [4]: %timeit copy2 = copy(giant) > 10 loops, best of 3: 90 ms per loop > > In [5]: import copy > > In [6]: %timeit copy2 = copy.copy(giant) > 10 loops, best of 3: 88.6 ms per loop > > Hmmm, wicked, when I looked earlier this week the difference appear to be > bigger. > > > > (3) It's very tedious having to create a property for each column ahead > > of time. But we can do this instead: > > > > > > def make_getter(key): > > def inner(self): > > return self._data[key][:] > > inner.__name__ = key > > return property(inner) > > > > > > class ColumnView(object): > > _data = {'a': [1, 2, 3, 4, 5, 6], > > 'b': [1, 2, 4, 8, 16, 32], > > 'c': [1, 10, 100, 1000, 10000, 100000], > > } > > for key in _data: > > locals()[key] = make_getter(key) > > del key > > > > > > and it works as above, but without all the tedious manual creation of > > property getters. > > > > Do you understand how this operates? If not, ask, and someone will > > explain. (And yes, this is one of the few times that writing to locals() > > actually works!) > > I think so. I still plan to write several working implementations to get a > better idea about which strategy to choose. > > > (4) But what if you don't know what the columns are called ahead of > > time? You can't use property, or descriptors, because you don't know > > what to call the damn things until you know what the column headers are, > > and by the time you know that, the class is already well and truly > > created. You might think you can do this: > > > > class ColumnView(object): > > def __init__(self): > > # read the columns from the CSV file > > self._data = ... > > # now create properties to suit > > for key in self._data: > > setattr(self, key, property( ... )) > > > > > > but that doesn't work. Properties only perform their "magic" when they > > are attached to the class itself. By setting them as attributes on the > > instance (self), they lose their power and just get treated as ordinary > > attributes. To be technical, we say that the descriptor protocol is only > > enacted when the attribute is found in the class, not in the instance. > > Ha! That is indeed exactly what I tried! :-)) > > > You might be tempted to write this instead: > > > > setattr(self.__class__, key, property( ... )) > > I thought about defining a classmethod, then inside it do setattr(cls, > key, property( ... )) > But that is probably the same? > > > but that's even worse. Now, every time you create a new ColumnView > > instance, *all the other instances will change*. They will grown new > > properties, or overwrite existing properties. You don't want that. > > > > Fortunately, Python has an mechanism for solving this problem: > > the `__getattr__` method and friends. > > > > > > class ColumnView(object): > > _data = {'a': [1, 2, 3, 4, 5, 6], > > 'b': [1, 2, 4, 8, 16, 32], > > 'c': [1, 10, 100, 1000, 10000, 100000], > > } > > def __getattr__(self, name): > > if name in self._data: > > return self._data[name][:] > > else: > > raise AttributeError > > def __setattr__(self, name, value): > > if name in self._data: > > raise AttributeError('read-only attribute') > > super(ColumnView, self).__setattr__(name, value) > > def __delattr__(self, name): > > if name in self._data: > > raise AttributeError('read-only attribute') > > super(ColumnView, self).__delattr__(name) > > That also seems very straightforward. Why does "if name in self._data:" > not cause a recursion? self._data calls __getattr__, which has self._data > in it, which...etc. > > > > ------------------------------ > > Subject: Digest Footer > > _______________________________________________ > Tutor maillist - Tutor at python.org > https://mail.python.org/mailman/listinfo/tutor > > > ------------------------------ > > End of Tutor Digest, Vol 141, Issue 11 > ************************************** > From alan.gauld at btinternet.com Wed Nov 11 12:37:26 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 11 Nov 2015 17:37:26 +0000 Subject: [Tutor] Python on Android (was Re: Tutor Digest, Vol 141, Issue 11) In-Reply-To: References: Message-ID: On 11/11/15 12:18, Burhan ul haq wrote: > I have already tried "pythonanywhere" but could not get it going, even for > a basic "hello world" script. Create a beginner account and login. Select the type of user you want - beginner Python in your case Select which Python version - 2.7 in your case A console will appear in the browser. Obviously being web based not all modules will work or be available but the bssic Python commands and modules will be fine. If your course gets machine specific (like the os module) then things may be less than ideal. HTH PS Please don't reply to a digest without first changing the subject line and deleting all the irrelevant stuff. Some people pay by the byte and all of us have already seen the digest material at least once already. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Wed Nov 11 13:28:52 2015 From: __peter__ at web.de (Peter Otten) Date: Wed, 11 Nov 2015 19:28:52 +0100 Subject: [Tutor] question about descriptors References: Message-ID: Albert-Jan Roskam wrote: >> class ReadColumn(object): >> def __init__(self, index): >> self._index = index >> def __get__(self, obj, type=None): >> return obj._row[self._index] >> def __set__(self, obj, value): >> raise AttributeError("oops") > > This appears to return one value, whereas I wanted I wanted to return all > values of a column, ie as many values as there are rows. But the logic > probably won't change. Sorry, I overlooked that aspect. If you want a whole column you either have to iterate over the complete file and keep the data in memory or you need a separate file descriptor for every access of a column. Here's an implementation of the first: def csv_columns(instream): reader = csv.reader(instream, delimiter=";") header = next(reader) return namedtuple("Columns", header)._make(tuple(zip(*reader))) From __peter__ at web.de Wed Nov 11 14:06:20 2015 From: __peter__ at web.de (Peter Otten) Date: Wed, 11 Nov 2015 20:06:20 +0100 Subject: [Tutor] question about descriptors References: <20151107142458.GS10946@ando.pearwood.info> Message-ID: Albert-Jan Roskam wrote: >> From: steve at pearwood.info >> Fortunately, Python has an mechanism for solving this problem: >> the `__getattr__` method and friends. >> >> >> class ColumnView(object): >> _data = {'a': [1, 2, 3, 4, 5, 6], >> 'b': [1, 2, 4, 8, 16, 32], >> 'c': [1, 10, 100, 1000, 10000, 100000], >> } >> def __getattr__(self, name): >> if name in self._data: >> return self._data[name][:] >> else: >> raise AttributeError >> def __setattr__(self, name, value): >> if name in self._data: >> raise AttributeError('read-only attribute') >> super(ColumnView, self).__setattr__(name, value) >> def __delattr__(self, name): >> if name in self._data: >> raise AttributeError('read-only attribute') >> super(ColumnView, self).__delattr__(name) > > That also seems very straightforward. Why does "if name in self._data:" > not cause a recursion? self._data calls __getattr__, which has self._data > in it, which...etc. __getattr__() is only invoked as a fallback when the normal attribute lookup fails: >>> class A(object): ... def __getattr__(self, name): ... return self.data[name] ... >>> a = A() >>> a.data = dict(foo="bar") >>> a.foo 'bar' >>> del a.data >>> import sys >>> sys.setrecursionlimit(10) >>> a.foo Traceback (most recent call last): File "", line 1, in File "", line 3, in __getattr__ File "", line 3, in __getattr__ File "", line 3, in __getattr__ RuntimeError: maximum recursion depth exceeded while calling a Python object If you need to intercept every attribute lookup use __getattribute__(): >>> class B(A): ... def __getattribute__(self, name): ... print "looking for", name ... return super(B, self).__getattribute__(name) ... >>> b = B() >>> b.data = dict(foo="bar") >>> b.foo looking for foo looking for data 'bar' From gfhenry1947 at gmail.com Wed Nov 11 14:38:47 2015 From: gfhenry1947 at gmail.com (George Henry) Date: Wed, 11 Nov 2015 11:38:47 -0800 Subject: [Tutor] Newbie Question Message-ID: <001b01d11cb8$95d41740$c17c45c0$@com> How do I find a site to download Python for Windows that includes a Toolbar? I'm using Windows 8.1 Have tried installing Python 3.4.2 but notice that the Python shell does not include a tool bar (i.e. File, Edit, Shell, Debug, etc.). Help please. Thanks! From sjeik_appie at hotmail.com Wed Nov 11 15:06:12 2015 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Wed, 11 Nov 2015 20:06:12 +0000 Subject: [Tutor] Newbie Question In-Reply-To: <001b01d11cb8$95d41740$c17c45c0$@com> References: <001b01d11cb8$95d41740$c17c45c0$@com> Message-ID: Hi, (Sorry for top-postin - I am using my phone). You can try Spyder IDE, it is part of Anaconda and Python(x, y): https://pythonhosted.org/spyder/installation.html#installing-on-windows-vista-7-8-10 Regards, Albert-Jan > From: gfhenry1947 at gmail.com > To: tutor at python.org > Date: Wed, 11 Nov 2015 11:38:47 -0800 > Subject: [Tutor] Newbie Question > > How do I find a site to download Python for Windows that includes a Toolbar? > > I'm using Windows 8.1 Have tried installing Python 3.4.2 but notice that > the Python shell does not include a tool bar (i.e. File, Edit, Shell, Debug, > etc.). > > > > Help please. > > > > Thanks! > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From breamoreboy at yahoo.co.uk Wed Nov 11 15:12:56 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 11 Nov 2015 20:12:56 +0000 Subject: [Tutor] Newbie Question In-Reply-To: <001b01d11cb8$95d41740$c17c45c0$@com> References: <001b01d11cb8$95d41740$c17c45c0$@com> Message-ID: On 11/11/2015 19:38, George Henry wrote: > How do I find a site to download Python for Windows that includes a Toolbar? > > I'm using Windows 8.1 Have tried installing Python 3.4.2 but notice that > the Python shell does not include a tool bar (i.e. File, Edit, Shell, Debug, > etc.). > > Help please. > > Thanks! > So IDLE is not good enough for you? I'll let you find it as it's part of the standard library, i.e. you've all ready downloaded it. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From alan.gauld at btinternet.com Wed Nov 11 18:27:33 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 11 Nov 2015 23:27:33 +0000 Subject: [Tutor] Newbie Question In-Reply-To: References: <001b01d11cb8$95d41740$c17c45c0$@com> Message-ID: On 11/11/15 20:12, Mark Lawrence wrote: > On 11/11/2015 19:38, George Henry wrote: >> How do I find a site to download Python for Windows that includes a >> Toolbar? >> > > So IDLE is not good enough for you? I'll let you find it as it's part > of the standard library, i.e. you've all ready downloaded it. IDLE only has menus. I suspect that's what the OP is already using when he refers to Python Shell. Since he is on Windows he can use the Pythonwin IDE. This used to be the default for ActiveState distros of Python but it seems they have dropped it recently. But you can still use it by going to the PyWin32 extensions (Install them from Sourceforge first(*) if you have a regular Python.org distro) Go to Lib/site-packages/pythonwin and find the pythonwin.exe file. Pin it to your start menu and you now have an IDLE like(but better) IDE with a toolbar. (*)Installing from Sourceforge is non-trivial, read the Readme file - linked to the download button - carefully then revist the top level page and follow them. Once the installer completes go to sitepackages as described above. Alternatively research some of the other IDEs available such as Eric (or even the Pydev extension to Eclipse if you already have Eclipse installed.) You will find YouTYube videos on using all of these - that might be the easiet way to evaluate them before downloading. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Wed Nov 11 18:59:16 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 11 Nov 2015 15:59:16 -0800 Subject: [Tutor] Tutor Digest, Vol 141, Issue 11 In-Reply-To: References: Message-ID: On Wed, Nov 11, 2015 at 4:18 AM, Burhan ul haq wrote: > Continuing "Run Python 2.7 on Android Tablet" > > Hi, > > I am constrained to install anything on my official laptop, therefore I > need to have an "online life saver" for Python Learning. You might look into repl.it: https://repl.it/languages/python For example: https://repl.it/BZUA You'll get a basic Python REPL that you can use from the web. Helpful in situations like the one you're describing. I wouldn't use it for important work, but for learning or exploratory programming, it's probably good enough. From dyoo at hashcollision.org Wed Nov 11 19:01:41 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 11 Nov 2015 16:01:41 -0800 Subject: [Tutor] Tutor Digest, Vol 141, Issue 11 In-Reply-To: References: Message-ID: >> I am constrained to install anything on my official laptop, therefore I >> need to have an "online life saver" for Python Learning. > > > You might look into repl.it: > > https://repl.it/languages/python As for tutorial material, you might look into: https://wiki.python.org/moin/BeginnersGuide/NonProgrammers Learn to Program, by Alan Gauld, is an example of a tutorial that you might enjoy: http://www.alan-g.me.uk/tutor/index.htm > > For example: > > https://repl.it/BZUA > > > You'll get a basic Python REPL that you can use from the web. Helpful > in situations like the one you're describing. I wouldn't use it for > important work, but for learning or exploratory programming, it's > probably good enough. From lac at openend.se Thu Nov 12 04:46:45 2015 From: lac at openend.se (Laura Creighton) Date: Thu, 12 Nov 2015 10:46:45 +0100 Subject: [Tutor] Tutor Digest, Vol 141, Issue 11 In-Reply-To: References: Message-ID: <201511120946.tAC9kjd6025940@fido.openend.se> In a message of Wed, 11 Nov 2015 17:18:06 +0500, Burhan ul haq writes: >Continuing "Run Python 2.7 on Android Tablet" > >Hi, > >I am constrained to install anything on my official laptop, therefore I >need to have an "online life saver" for Python Learning. > >I have already tried "pythonanywhere" but could not get it going, even for >a basic "hello world" script. I could not locate any basic documentation to >help me with. The basic help tab "I want to start learning Python >" bring >you back to the main dashboard. > >Can anybody share a basic how to get started ... > >Many Thanks / That link works for me. You have to create an account and login before you will be taken to the page about how to create your account. Laura From sjeik_appie at hotmail.com Thu Nov 12 07:11:19 2015 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Thu, 12 Nov 2015 12:11:19 +0000 Subject: [Tutor] question about descriptors In-Reply-To: References: , <20151107142458.GS10946@ando.pearwood.info>, , Message-ID: > To: tutor at python.org > From: __peter__ at web.de > Date: Wed, 11 Nov 2015 20:06:20 +0100 > Subject: Re: [Tutor] question about descriptors > > Albert-Jan Roskam wrote: > > >> From: steve at pearwood.info > > >> Fortunately, Python has an mechanism for solving this problem: > >> the `__getattr__` method and friends. > >> > >> > >> class ColumnView(object): > >> _data = {'a': [1, 2, 3, 4, 5, 6], > >> 'b': [1, 2, 4, 8, 16, 32], > >> 'c': [1, 10, 100, 1000, 10000, 100000], > >> } > >> def __getattr__(self, name): > >> if name in self._data: > >> return self._data[name][:] > >> else: > >> raise AttributeError > >> def __setattr__(self, name, value): > >> if name in self._data: > >> raise AttributeError('read-only attribute') > >> super(ColumnView, self).__setattr__(name, value) > >> def __delattr__(self, name): > >> if name in self._data: > >> raise AttributeError('read-only attribute') > >> super(ColumnView, self).__delattr__(name) > > > > That also seems very straightforward. Why does "if name in self._data:" > > not cause a recursion? self._data calls __getattr__, which has self._data > > in it, which...etc. > > __getattr__() is only invoked as a fallback when the normal attribute lookup > fails: Aha.. and "normal attributes" live in self.__dict__? > >>> class A(object): > ... def __getattr__(self, name): > ... return self.data[name] > ... > >>> a = A() > >>> a.data = dict(foo="bar") > >>> a.foo > 'bar' > >>> del a.data > >>> import sys > >>> sys.setrecursionlimit(10) > >>> a.foo > Traceback (most recent call last): > File "", line 1, in > File "", line 3, in __getattr__ > File "", line 3, in __getattr__ > File "", line 3, in __getattr__ > RuntimeError: maximum recursion depth exceeded while calling a Python object > > If you need to intercept every attribute lookup use __getattribute__(): Fantastic, thank you for the clear explanation. Do you happen to know whether the __getattr__ vs. __getattribute__ distinction was (a) a deliberate design decision or (b) a historic anomaly? If one considers the distinction between "normal attributes" vs. "attributes of which the read/write/delete properties*) may be changed" , I'd say (a). *) with files these are called "attributes", so one could call them attributes with attributes. :-) > >>> class B(A): > ... def __getattribute__(self, name): > ... print "looking for", name > ... return super(B, self).__getattribute__(name) > ... > >>> b = B() > >>> b.data = dict(foo="bar") > >>> b.foo > looking for foo > looking for data > 'bar' > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From mokshavivek at gmail.com Thu Nov 12 06:23:20 2015 From: mokshavivek at gmail.com (Br. Sayan) Date: Thu, 12 Nov 2015 16:53:20 +0530 Subject: [Tutor] Python ASTM Implementation Message-ID: Dear All, We have a Lab Analyzer(Cobas C311), which we want to interface with Pyhton. As you might be knowing that clinical lab analyzers use ASTM protocol. Python has a an ASTM module , but I need help in its implementation. In the first place, to my embarrassment I could not install it on Linux Mint 15 with Python 2.7.4. The following error is coming : > /usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown > distribution option: 'zip_safe' > warnings.warn(msg) > /usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown > distribution option: 'test_suite' > warnings.warn(msg) > /usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown > distribution option: 'install_requires' > warnings.warn(msg) > usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] > or: setup.py --help [cmd1 cmd2 ...] > or: setup.py --help-commands > or: setup.py cmd --help > > error: no commands supplied > And next, cannot figure out how to start. Desperately need your help in starting off the coding. I have little experience in Python, but am confident that I can pick it up soon if I get help. If you feel like, you may direct me to some other links where this type of codings have been shown with examples of some sort. Regards, Sayan From sajjadul.islam.bd at gmail.com Thu Nov 12 12:16:31 2015 From: sajjadul.islam.bd at gmail.com (Sajjadul Islam) Date: Thu, 12 Nov 2015 18:16:31 +0100 Subject: [Tutor] The very first program in python Message-ID: Hello I have coded the first snippet in python - defined a function , then I am getting error if I try call the function from the IDLE window. The error is : /////////////////////////////////// Traceback (most recent call last): File "", line 1, in NameError: name 'print_lol' is not defined ////////////////////////////////// print_lol is the name of the function that is declared and defined inside the file called nester.py and IDLE is called in the same path. What is that I am missing here ? Thanks From breamoreboy at yahoo.co.uk Thu Nov 12 12:42:36 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 12 Nov 2015 17:42:36 +0000 Subject: [Tutor] The very first program in python In-Reply-To: References: Message-ID: On 12/11/2015 17:16, Sajjadul Islam wrote: > Hello > > I have coded the first snippet in python - defined a function , then I am > getting error if I try call the function from the IDLE window. The error is > : > > Traceback (most recent call last): > File "", line 1, in > NameError: name 'print_lol' is not defined > > print_lol is the name of the function that is declared and defined inside > the file called nester.py and IDLE is called in the same path. What is that > I am missing here ? > > Thanks > You need to either:- import nester nester.print_lol() or:- from nester import print_lol print_lol() -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From alan.gauld at btinternet.com Thu Nov 12 12:45:02 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 12 Nov 2015 17:45:02 +0000 Subject: [Tutor] Python ASTM Implementation In-Reply-To: References: Message-ID: On 12/11/15 11:23, Br. Sayan wrote: > As you might be knowing that clinical lab analyzers use ASTM protocol. Nope, never heard of ASTM. > Python has a an ASTM module , but > I need help in its implementation. In the first place, to my embarrassment > I could not install it on Linux Mint 15 with Python 2.7.4. I note that the most recent release of ASTM (Mar 16 2013) pre-dates 2.7.4 (Apr 6th 2013) by a week or two. There is a slight chance that it is incompatible. I can't see any notes about which Python versions it works with. > The following error is coming : > > ... >> distribution option: 'install_requires' >> warnings.warn(msg) >> usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] >> or: setup.py --help [cmd1 cmd2 ...] >> or: setup.py --help-commands >> or: setup.py cmd --help >> >> error: no commands supplied What did you do to install it? What commands did you run and from whre? > And next, cannot figure out how to start. Desperately need your help in > starting off the coding. I have little experience in Python, but am > confident that I can pick it up soon if I get help. I assume you read the home page documentation for ASTM? http://python-astm.readthedocs.org/en/latest/ You need to provide a bit more detailed info on what you are doing and what specific issues you want help with. In particular we need to see the actual code you are running. Thanks for providing the full error though, that helps a lot. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Thu Nov 12 12:49:10 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 12 Nov 2015 17:49:10 +0000 Subject: [Tutor] The very first program in python In-Reply-To: References: Message-ID: On 12/11/15 17:16, Sajjadul Islam wrote: > Hello > > I have coded the first snippet in python - defined a function , Give us a clue - show us the code. > getting error if I try call the function from the IDLE window. If you defined your function in a separate module using IDLE you can use the run menu to run the module directly. That will in turn load it into the IDLE shell for you to use. Alternatively import the module directly in the shell. > /////////////////////////////////// > > Traceback (most recent call last): > File "", line 1, in > NameError: name 'print_lol' is not defined > > print_lol is the name of the function that is declared and defined inside > the file called nester.py and IDLE is called in the same path. What is that > I am missing here ? Try running nester.py from IDLE. Alternatively try typing import nester nester.print_lol() or from nester import print_lol print_lol() Its probably worth including a note of OS and Python version in future posts. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From sjeik_appie at hotmail.com Thu Nov 12 13:19:55 2015 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Thu, 12 Nov 2015 18:19:55 +0000 Subject: [Tutor] Python ASTM Implementation In-Reply-To: References: Message-ID: > Date: Thu, 12 Nov 2015 16:53:20 +0530 > From: mokshavivek at gmail.com > To: tutor at python.org > Subject: [Tutor] Python ASTM Implementation > > Dear All, > > We have a Lab Analyzer(Cobas C311), which we want to interface with Pyhton. > As you might be knowing that clinical lab analyzers use ASTM protocol. > Python has a an ASTM module , but > I need help in its implementation. In the first place, to my embarrassment > I could not install it on Linux Mint 15 with Python 2.7.4. > > The following error is coming : > > > > /usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown > > distribution option: 'zip_safe' > > warnings.warn(msg) > > /usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown > > distribution option: 'test_suite' > > warnings.warn(msg) > > /usr/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown > > distribution option: 'install_requires' > > warnings.warn(msg) > > usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] > > or: setup.py --help [cmd1 cmd2 ...] > > or: setup.py --help-commands > > or: setup.py cmd --help > > > > error: no commands supplied > > > > And next, cannot figure out how to start. Desperately need your help in > starting off the coding. I have little experience in Python, but am > confident that I can pick it up soon if I get help. > > If you feel like, you may direct me to some other links where this type of > codings have been shown with examples of some sort. Hi, Maybe it's setuptools that is too old? Or perhaps pip? sudo apt-get update sudo apt-get install --only-upgrade python-setuptools python-pip I believe it's safer to install Python packages via apt-get if you are using the Linux internal Python, rather than using pip. Albert-Jan From __peter__ at web.de Fri Nov 13 03:26:55 2015 From: __peter__ at web.de (Peter Otten) Date: Fri, 13 Nov 2015 09:26:55 +0100 Subject: [Tutor] question about descriptors References: <20151107142458.GS10946@ando.pearwood.info> Message-ID: Albert-Jan Roskam wrote: >> __getattr__() is only invoked as a fallback when the normal attribute >> lookup fails: > > > Aha.. and "normal attributes" live in self.__dict__? I meant "normal (attribute lookup)" rather than "(normal attribute) lookup". __getattr__() works the same (I think) when there is no __dict__: >>> class A(object): ... __slots__ = ["foo"] ... def __getattr__(self, name): ... print "looking for", name ... return 42 ... >>> a = A() >>> a.foo looking for foo 42 >>> a.__dict__ looking for __dict__ 42 >>> a.foo = "bar" >>> a.foo 'bar' From mokshavivek at gmail.com Thu Nov 12 23:37:49 2015 From: mokshavivek at gmail.com (Br. Sayan) Date: Fri, 13 Nov 2015 10:07:49 +0530 Subject: [Tutor] Python ASTM Implementation In-Reply-To: References: Message-ID: *Alan* ?I note that the most recent release of ASTM (Mar 16 2013) > pre-dates 2.7.4 (Apr 6th 2013) by a week or two. > There is a slight chance that it is incompatible. I can't > ?see any notes about which Python versions it works with. > In the Chnagelog for Ver. 0.4 it says "Set minimal Python version to 2.6, but 3.2-3.3 also works well." But in the setup.py they have mentioned against 'Programming Language::' Python 2.6, 2.7, 3.2 and 3.3. I'll try in different machine and let you know. I just did the following from the directory 'astm-0.5.0' ( as untarred from the tarball from the website) $ chmod 777 setup.py $ ./setup.py ?You need to provide a bit more detailed info on what you are > doing and what specific issues you want help with. > Let me explain. The biochemistry analyzer Cobas C311 can be made to configure in a full-duplex mode in which it accepts the 'Patient_ID' and reads the tests to be done and performs them and again it sends the results to the Host machine. For the time being, we need the second part. Only data acquisition from the analyzer. The program will just acquire the data from the analyser and store it in a CSV file.(to be incorporated to an existing Oracle database) The Python ASTM looks promising with its objectives: 1. Provide decoder ASTM data to Python objects and encode them back to valid ASTM messages. 2. Provide ability to *create your own ASTM client and server applications *followed by common specification routines. 3. *Provide ready-to-use ?drivers? for various ASTM-driven analysers*, middlewares and other software or hardware solutions. But I need some solid example to start off with. And if it is *'ready to use'* then what is the syntax. I don't seem to find any usage examples. For example, what is to be done for starting data acquisition. Then comes manipulation. Even if I manage to get some bits of meaningful information transferred from the machine, it makes sense. Though there are some examples in the 'OmniLab' section, I can't figure out what to do with them. *Albert* Maybe it's setuptools that is too old? Or perhaps pip? > sudo apt-get update > sudo apt-get install --only-upgrade python-setuptools python-pip > > I believe it's safer to install Python packages via apt-get if you are > using the Linux internal Python, rather than using pip. > I have not installed through pip. I just gave the setup script 777 permission and run it with ./setup.py . How do I do it with apt-get? I don't think it's in repository. Pls let me know. On 13 November 2015 at 09:55, Br. Sayan wrote: > Dear Alan, > > ?I note that the most recent release of ASTM (Mar 16 2013) >> pre-dates 2.7.4 (Apr 6th 2013) by a week or two. >> There is a slight chance that it is incompatible. I can't >> ?see any notes about which Python versions it works with. >> > > In the Chnagelog for Ver. 0.4 it says "Set minimal Python version to 2.6, > but 3.2-3.3 also works well." But in the setup.py they have mentioned > against 'Programming Language::' Python 2.6, 2.7, 3.2 and 3.3. I'll try in > different machine and let you know. > > I just did the following from the directory 'astm-0.5.0' ( as untarred > from the tarball from the website) > > $ chmod 777 setup.py > $ ./setup.py > > ?You need to provide a bit more detailed info on what you are >> doing and what specific issues you want help with. >> > > Let me explain. The biochemistry analyzer Cobas C311 can be made to > configure in a full-duplex mode in which it accepts the 'Patient_ID' and > reads the tests to be done and performs them and again it sends the results > to the Host machine. For the time being, we need the second part. Only data > acquisition from the analyzer. The program will just acquire the data from > the analyser and store it in a CSV file.(to be incorporated to an existing > Oracle database) > > The Python ASTM looks promising with its objectives: > > > 1. Provide decoder ASTM data to Python objects and encode them back to > valid ASTM messages. > 2. Provide ability to *create your own ASTM client and server > applications *followed by common specification routines. > 3. *Provide ready-to-use ?drivers? for various ASTM-driven analysers*, > middlewares and other software or hardware solutions. > > But I need some solid example to start off with. And if it is *'ready to > use'* then what is the syntax. I don't seem to find any usage examples. > For example, what is to be done for starting data acquisition. Then comes > manipulation. Even if I manage to get some bits of meaningful information > transferred from the machine, it makes sense. Though there are some > examples in the 'OmniLab' section, I can't figure out what to do with them. > > > Reagrds, > > Sayan > > > > On 12 November 2015 at 23:15, Alan Gauld > wrote: > >> On 12/11/15 11:23, Br. Sayan wrote: >> >> As you might be knowing that clinical lab analyzers use ASTM protocol. >>> >> >> Nope, never heard of ASTM. >> >> Python has a an ASTM module , >>> but >>> I need help in its implementation. In the first place, to my >>> embarrassment >>> I could not install it on Linux Mint 15 with Python 2.7.4. >>> >> >> ?? >> I note that the most recent release of ASTM (Mar 16 2013) >> pre-dates 2.7.4 (Apr 6th 2013) by a week or two. >> There is a slight chance that it is incompatible. I can't >> ?? >> ?? >> see any notes about which Python versions it works with. >> >> The following error is coming : >>> >>> ... >>> >>>> distribution option: 'install_requires' >>>> warnings.warn(msg) >>>> usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] >>>> or: setup.py --help [cmd1 cmd2 ...] >>>> or: setup.py --help-commands >>>> or: setup.py cmd --help >>>> >>>> error: no commands supplied >>>> >>> >> What did you do to install it? >> What commands did you run and from whre? >> >> And next, cannot figure out how to start. Desperately need your help in >>> starting off the coding. I have little experience in Python, but am >>> confident that I can pick it up soon if I get help. >>> >> >> I assume you read the home page documentation for ASTM? >> >> http://python-astm.readthedocs.org/en/latest/ >> >> ?? >> You need to provide a bit more detailed info on what you are >> doing and what specific issues you want help with. In particular >> we need to see the actual code you are running. Thanks for >> providing the full error though, that helps a lot. >> >> >> -- >> Alan G >> Author of the Learn to Program web site >> http://www.alan-g.me.uk/ >> http://www.amazon.com/author/alan_gauld >> Follow my photo-blog on Flickr at: >> http://www.flickr.com/photos/alangauldphotos >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > From mokshavivek at gmail.com Fri Nov 13 01:33:41 2015 From: mokshavivek at gmail.com (Br. Sayan) Date: Fri, 13 Nov 2015 12:03:41 +0530 Subject: [Tutor] Python ASTM Implementation In-Reply-To: References: Message-ID: Well, doing $ python setup.py install did the installation. It can now import astm. Should I try with PySerial first, as the communication is through RS232 serial cable? Sayan On 13 November 2015 at 10:07, Br. Sayan wrote: > *Alan* > > ?I note that the most recent release of ASTM (Mar 16 2013) >> pre-dates 2.7.4 (Apr 6th 2013) by a week or two. >> There is a slight chance that it is incompatible. I can't >> ?see any notes about which Python versions it works with. >> > > In the Chnagelog for Ver. 0.4 it says "Set minimal Python version to 2.6, > but 3.2-3.3 also works well." But in the setup.py they have mentioned > against 'Programming Language::' Python 2.6, 2.7, 3.2 and 3.3. I'll try in > different machine and let you know. > > I just did the following from the directory 'astm-0.5.0' ( as untarred > from the tarball from the website) > > $ chmod 777 setup.py > $ ./setup.py > > ?You need to provide a bit more detailed info on what you are >> doing and what specific issues you want help with. >> > > Let me explain. The biochemistry analyzer Cobas C311 can be made to > configure in a full-duplex mode in which it accepts the 'Patient_ID' and > reads the tests to be done and performs them and again it sends the results > to the Host machine. For the time being, we need the second part. Only data > acquisition from the analyzer. The program will just acquire the data from > the analyser and store it in a CSV file.(to be incorporated to an existing > Oracle database) > > The Python ASTM looks promising with its objectives: > > 1. Provide decoder ASTM data to Python objects and encode them back to > valid ASTM messages. > 2. Provide ability to *create your own ASTM client and server > applications *followed by common specification routines. > 3. *Provide ready-to-use ?drivers? for various ASTM-driven analysers*, > middlewares and other software or hardware solutions. > > But I need some solid example to start off with. And if it is *'ready to > use'* then what is the syntax. I don't seem to find any usage examples. > For example, what is to be done for starting data acquisition. Then comes > manipulation. Even if I manage to get some bits of meaningful information > transferred from the machine, it makes sense. Though there are some > examples in the 'OmniLab' section, I can't figure out what to do with them. > *Albert* > > Maybe it's setuptools that is too old? Or perhaps pip? >> sudo apt-get update >> sudo apt-get install --only-upgrade python-setuptools python-pip >> >> I believe it's safer to install Python packages via apt-get if you are >> using the Linux internal Python, rather than using pip. >> > > I have not installed through pip. I just gave the setup script 777 > permission and run it with ./setup.py . How do I do it with apt-get? I > don't think it's in repository. Pls let me know. > > > On 13 November 2015 at 09:55, Br. Sayan wrote: > >> Dear Alan, >> >> ?I note that the most recent release of ASTM (Mar 16 2013) >>> pre-dates 2.7.4 (Apr 6th 2013) by a week or two. >>> There is a slight chance that it is incompatible. I can't >>> ?see any notes about which Python versions it works with. >>> >> >> In the Chnagelog for Ver. 0.4 it says "Set minimal Python version to 2.6, >> but 3.2-3.3 also works well." But in the setup.py they have mentioned >> against 'Programming Language::' Python 2.6, 2.7, 3.2 and 3.3. I'll try in >> different machine and let you know. >> >> I just did the following from the directory 'astm-0.5.0' ( as untarred >> from the tarball from the website) >> >> $ chmod 777 setup.py >> $ ./setup.py >> >> ?You need to provide a bit more detailed info on what you are >>> doing and what specific issues you want help with. >>> >> >> Let me explain. The biochemistry analyzer Cobas C311 can be made to >> configure in a full-duplex mode in which it accepts the 'Patient_ID' and >> reads the tests to be done and performs them and again it sends the results >> to the Host machine. For the time being, we need the second part. Only data >> acquisition from the analyzer. The program will just acquire the data from >> the analyser and store it in a CSV file.(to be incorporated to an existing >> Oracle database) >> >> The Python ASTM looks promising with its objectives: >> >> >> 1. Provide decoder ASTM data to Python objects and encode them back >> to valid ASTM messages. >> 2. Provide ability to *create your own ASTM client and server >> applications *followed by common specification routines. >> 3. *Provide ready-to-use ?drivers? for various ASTM-driven analysers*, >> middlewares and other software or hardware solutions. >> >> But I need some solid example to start off with. And if it is *'ready to >> use'* then what is the syntax. I don't seem to find any usage examples. >> For example, what is to be done for starting data acquisition. Then comes >> manipulation. Even if I manage to get some bits of meaningful information >> transferred from the machine, it makes sense. Though there are some >> examples in the 'OmniLab' section, I can't figure out what to do with them. >> >> >> Reagrds, >> >> Sayan >> >> >> >> On 12 November 2015 at 23:15, Alan Gauld >> wrote: >> >>> On 12/11/15 11:23, Br. Sayan wrote: >>> >>> As you might be knowing that clinical lab analyzers use ASTM protocol. >>>> >>> >>> Nope, never heard of ASTM. >>> >>> Python has a an ASTM module , >>>> but >>>> I need help in its implementation. In the first place, to my >>>> embarrassment >>>> I could not install it on Linux Mint 15 with Python 2.7.4. >>>> >>> >>> ?? >>> I note that the most recent release of ASTM (Mar 16 2013) >>> pre-dates 2.7.4 (Apr 6th 2013) by a week or two. >>> There is a slight chance that it is incompatible. I can't >>> ?? >>> ?? >>> see any notes about which Python versions it works with. >>> >>> The following error is coming : >>>> >>>> ... >>>> >>>>> distribution option: 'install_requires' >>>>> warnings.warn(msg) >>>>> usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] >>>>> or: setup.py --help [cmd1 cmd2 ...] >>>>> or: setup.py --help-commands >>>>> or: setup.py cmd --help >>>>> >>>>> error: no commands supplied >>>>> >>>> >>> What did you do to install it? >>> What commands did you run and from whre? >>> >>> And next, cannot figure out how to start. Desperately need your help in >>>> starting off the coding. I have little experience in Python, but am >>>> confident that I can pick it up soon if I get help. >>>> >>> >>> I assume you read the home page documentation for ASTM? >>> >>> http://python-astm.readthedocs.org/en/latest/ >>> >>> ?? >>> You need to provide a bit more detailed info on what you are >>> doing and what specific issues you want help with. In particular >>> we need to see the actual code you are running. Thanks for >>> providing the full error though, that helps a lot. >>> >>> >>> -- >>> Alan G >>> Author of the Learn to Program web site >>> http://www.alan-g.me.uk/ >>> http://www.amazon.com/author/alan_gauld >>> Follow my photo-blog on Flickr at: >>> http://www.flickr.com/photos/alangauldphotos >>> >>> >>> _______________________________________________ >>> Tutor maillist - Tutor at python.org >>> To unsubscribe or change subscription options: >>> https://mail.python.org/mailman/listinfo/tutor >>> >> >> > From lac at openend.se Fri Nov 13 06:04:27 2015 From: lac at openend.se (Laura Creighton) Date: Fri, 13 Nov 2015 12:04:27 +0100 Subject: [Tutor] Python ASTM Implementation In-Reply-To: References: Message-ID: <201511131104.tADB4RtW017278@fido.openend.se> In a message of Fri, 13 Nov 2015 10:07:49 +0530, "Br. Sayan" writes: >Maybe it's setuptools that is too old? Or perhaps pip? >> sudo apt-get update >> sudo apt-get install --only-upgrade python-setuptools python-pip >> >> I believe it's safer to install Python packages via apt-get if you are >> using the Linux internal Python, rather than using pip. >> > >I have not installed through pip. I just gave the setup script 777 >permission and run it with ./setup.py . How do I do it with apt-get? I >don't think it's in repository. Pls let me know. It is not in the repository (at least in mine in debian unstable.) Here is how you find such things out: $ apt-cache search astm python-django-model-utils - Django model mixins and utilities python3-django-model-utils - Django model mixins and utilities libcache-fastmmap-perl - Perl module providing a mmap'ed cache libcatalyst-plugin-cache-store-fastmmap-perl - (deprecated) FastMmap cache store plugin for Catalyst::Plugin::Cache libcatalyst-plugin-session-store-fastmmap-perl - Catalyst session storage plugin backed by Cache::FastMMap libcgi-application-plugin-tt-perl - plugin that adds Template Toolkit support to CGI::Application r-cran-fastmatch - GNU R package for fast match replacement for repeated look-ups So, lots of packages have the string 'astm' in their names, and none of them are the one we are looking for. Did you find http://python-astm.readthedocs.org/en/latest/ It's not much in the way of documentation, but a bit better than nothing. There are people recommending python-astm here. http://www.limsforum.com/ (just search for ASTM and you will get many hits). I didn't find any tutorials, though. I did find this, https://github.com/mpasternak/cobas-scraper which in no way is what you were looking for, but I thought, hmm, he might want one of these as well. Laura From lac at openend.se Fri Nov 13 06:13:48 2015 From: lac at openend.se (Laura Creighton) Date: Fri, 13 Nov 2015 12:13:48 +0100 Subject: [Tutor] Python ASTM Implementation In-Reply-To: References: Message-ID: <201511131113.tADBDmM8018093@fido.openend.se> In a message of Fri, 13 Nov 2015 12:03:41 +0530, "Br. Sayan" writes: >Well, doing > >$ python setup.py install > >did the installation. It can now import astm. > >Should I try with PySerial first, as the communication is through RS232 >serial cable? > >Sayan Are you on ubuntu? Be warned about this bug: https://bugs.launchpad.net/ubuntu/+source/linux-lts-trusty/+bug/1501345 which makes PySerial not work with that particular kernel. It is supposedly fixed now, but check that you have all the newest versions before you run into trouble. I don't know whether starting with PySerial is a) necessary or b) desirable. Laura From sjeik_appie at hotmail.com Fri Nov 13 06:42:18 2015 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Fri, 13 Nov 2015 11:42:18 +0000 Subject: [Tutor] Python ASTM Implementation In-Reply-To: <201511131104.tADB4RtW017278@fido.openend.se> References: , <201511131104.tADB4RtW017278@fido.openend.se> Message-ID: > To: mokshavivek at gmail.com > CC: alan.gauld at btinternet.com; tutor at python.org; sjeik_appie at hotmail.com; lac at openend.se > From: lac at openend.se > Subject: Re: [Tutor] Python ASTM Implementation > Date: Fri, 13 Nov 2015 12:04:27 +0100 > > In a message of Fri, 13 Nov 2015 10:07:49 +0530, "Br. Sayan" writes: > > >Maybe it's setuptools that is too old? Or perhaps pip? > >> sudo apt-get update > >> sudo apt-get install --only-upgrade python-setuptools python-pip > >> > >> I believe it's safer to install Python packages via apt-get if you are > >> using the Linux internal Python, rather than using pip. > >> > > > >I have not installed through pip. I just gave the setup script 777 > >permission and run it with ./setup.py . How do I do it with apt-get? I > >don't think it's in repository. Pls let me know. > > It is not in the repository (at least in mine in debian unstable.) > Here is how you find such things out: I was not suggesting to install astm via apt-get, just setuptools (and while we' re at it, pip as well). After you've done that, you could try to install astm the way you intended to (apparently using setup.py install, which tries to import distutils or setuptools). > $ apt-cache search astm > python-django-model-utils - Django model mixins and utilities > python3-django-model-utils - Django model mixins and utilities > libcache-fastmmap-perl - Perl module providing a mmap'ed cache > libcatalyst-plugin-cache-store-fastmmap-perl - (deprecated) FastMmap cache store plugin for Catalyst::Plugin::Cache > libcatalyst-plugin-session-store-fastmmap-perl - Catalyst session storage plugin backed by Cache::FastMMap > libcgi-application-plugin-tt-perl - plugin that adds Template Toolkit support to CGI::Application > r-cran-fastmatch - GNU R package for fast match replacement for repeated look-ups > > So, lots of packages have the string 'astm' in their names, and none > of them are the one we are looking for. > > Did you find > http://python-astm.readthedocs.org/en/latest/ > > It's not much in the way of documentation, but a bit better than > nothing. > > There are people recommending python-astm here. > http://www.limsforum.com/ (just search for ASTM and you will get many > hits). I didn't find any tutorials, though. > > I did find this, https://github.com/mpasternak/cobas-scraper > which in no way is what you were looking for, but I thought, hmm, he > might want one of these as well. > > Laura From sjeik_appie at hotmail.com Fri Nov 13 06:44:47 2015 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Fri, 13 Nov 2015 11:44:47 +0000 Subject: [Tutor] question about descriptors In-Reply-To: References: , <20151107142458.GS10946@ando.pearwood.info>, , , Message-ID: > To: tutor at python.org > From: __peter__ at web.de > Date: Fri, 13 Nov 2015 09:26:55 +0100 > Subject: Re: [Tutor] question about descriptors > > Albert-Jan Roskam wrote: > > >> __getattr__() is only invoked as a fallback when the normal attribute > >> lookup fails: > > > > > > Aha.. and "normal attributes" live in self.__dict__? > > I meant "normal (attribute lookup)" rather than "(normal attribute) lookup". > __getattr__() works the same (I think) when there is no __dict__: > > >>> class A(object): > ... __slots__ = ["foo"] > ... def __getattr__(self, name): > ... print "looking for", name > ... return 42 > ... > >>> a = A() > >>> a.foo > looking for foo > 42 > >>> a.__dict__ > looking for __dict__ > 42 > >>> a.foo = "bar" > >>> a.foo > 'bar' Thank you again for the explanation. Much appreciated. I had not even thought about __slots__ yet. From steve at pearwood.info Fri Nov 13 07:18:48 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 13 Nov 2015 23:18:48 +1100 Subject: [Tutor] question about descriptors In-Reply-To: References: Message-ID: <20151113121847.GB2038@ando.pearwood.info> On Thu, Nov 12, 2015 at 12:11:19PM +0000, Albert-Jan Roskam wrote: > > __getattr__() is only invoked as a fallback when the normal attribute lookup > > fails: > > > Aha.. and "normal attributes" live in self.__dict__? Not necessarily. Attributes can live either in "slots" or the instance dict, or the class dict, or one of the superclass dicts. Some examples may help. Let's start with defining a hierarchy of classes, and make an instance: class Grandparent(object): spam = "from the grandparent class" def __getattr__(self, name): return "%s calculated by __getattr__" % name class Parent(Grandparent): eggs = "from the parent class" class MyClass(Parent): cheese = "from the instance's own class" instance = MyClass() instance.tomato = "from the instance itself" The attributes defined above return their value without calling __getattr__: py> instance.tomato, instance.cheese, instance.eggs, instance.spam ('from the instance itself', "from the instance's own class", 'from the parent class', 'from the grandparent class') but only "tomato" lives in the instance __dict__: py> instance.__dict__ {'tomato': 'from the instance itself'} You can check MyClass.__dict__, etc. to see the other class attributes. And, of course, __getattr__ is called for anything not found in those dicts: py> instance.foo 'foo calculated by __getattr__' Slots are an alternative to dict-based attributes. If you have millions of instances, all with a fixed number of attributes, using a dict for each one can waste a lot of memory. Using slots is a way of optimizing for memory: class Slotted(object): __slots__ = ["spam", "eggs"] def __init__(self): self.spam = 1 self.eggs = 2 def __getattr__(self, name): return "%s calculated by __getattr__" % name x = Slotted() This works similarly to the above, except there is no instance dict at all: py> x.spam 1 py> x.eggs 2 py> x.foo 'foo calculated by __getattr__' py> x.__dict__ '__dict__ calculated by __getattr__' To be honest, I didn't expect that last result. I expected it to return Slotted.__dict__. I'm not entirely sure why it didn't. [...] > > If you need to intercept every attribute lookup use __getattribute__(): > > Fantastic, thank you for the clear explanation. Do you happen to know > whether the __getattr__ vs. __getattribute__ distinction was (a) a > deliberate design decision or (b) a historic anomaly? A bit of both. Originally, classes didn't support __getattribute__. Only __getattr__ existed (together with __setattr__ and __delattr__), and as you have seen, that is only called where the normal attribute lookup mechanism fails. That was deliberate. But in Python 2.2, "new style" classes were added. For technical reasons, new-style classes need to support intercepting every attribute lookup (that provides the hook for descriptors to work). So __getattribute__ was added, but only for new-style classes. But be warned: writing your own __getattribute__ method is tricky to get right, and tends to slow down your class. So it's best avoided, unless you really need it. -- Steve From alan.gauld at btinternet.com Sat Nov 14 19:08:37 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 15 Nov 2015 00:08:37 +0000 Subject: [Tutor] Missing posts Message-ID: Hi Folks, I've approved a couple of messages in the last 24 hours that have not made it onto the list. I'm not sure what's happening but, if you posted recently and it has not shown up, please feel free to post again. Meantime, I'm going to try to find out what has happened. -- Alan G Moderator From sjeik_appie at hotmail.com Sun Nov 15 03:41:24 2015 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Sun, 15 Nov 2015 08:41:24 +0000 Subject: [Tutor] Missing posts In-Reply-To: References: Message-ID: (Sorry for top-posting) Could it be related to this? https://www.emailonacid.com/blog/article/industry-news/could_yahoo_and_aols_dmarc_policies_destroy_your_deliverability Albert-Jan > To: tutor at python.org > From: alan.gauld at btinternet.com > Date: Sun, 15 Nov 2015 00:08:37 +0000 > Subject: [Tutor] Missing posts > > Hi Folks, > > I've approved a couple of messages in the last 24 hours that have > not made it onto the list. I'm not sure what's happening but, if > you posted recently and it has not shown up, please feel free > to post again. > > Meantime, I'm going to try to find out what has happened. > > > -- > Alan G > Moderator > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Sun Nov 15 04:04:47 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 15 Nov 2015 09:04:47 +0000 Subject: [Tutor] Missing posts In-Reply-To: References: Message-ID: On 15/11/15 08:41, Albert-Jan Roskam wrote: > (Sorry for top-posting) > > Could it be related to this? ... I don't think so, since the mails in question are getting as far as the mailman server moderation queue. If the users weren't on moderation I assume they would have been forwarded on as usual (unless somebody changed mailman to do the same kind of checks). -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From sajjadul.islam.bd at gmail.com Sat Nov 14 22:23:48 2015 From: sajjadul.islam.bd at gmail.com (Sajjadul Islam) Date: Sun, 15 Nov 2015 04:23:48 +0100 Subject: [Tutor] The very first program in python In-Reply-To: References: Message-ID: Hi I am on Ubuntu 15.10 and using Python 3.4. The following suggestion is not clear to me : " If you defined your function in a separate module using IDLE you can use the run menu to run the module directly. That will in turn load it into the IDLE shell for you to use. " Let me explain again what I did. I created a .py file and defined a function that takes a last as input argument. Then i saved the file. 1.Start python3 interpretor from the command line 2. Defined a list in the interpreter. 3. Send the list as input argument to the function in the .py file. Eventually, I got the error message as mentioned in my previous post. How to call the run menu once you are in the command line at linux env ? Thanks On Thu, Nov 12, 2015 at 6:16 PM, Sajjadul Islam wrote: > Hello > > I have coded the first snippet in python - defined a function , then I am > getting error if I try call the function from the IDLE window. The error is > : > > /////////////////////////////////// > > Traceback (most recent call last): > File "", line 1, in > NameError: name 'print_lol' is not defined > > > > ////////////////////////////////// > > print_lol is the name of the function that is declared and defined inside > the file called nester.py and IDLE is called in the same path. What is that > I am missing here ? > > > > Thanks > > > From alan.gauld at btinternet.com Sun Nov 15 04:14:30 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 15 Nov 2015 09:14:30 +0000 Subject: [Tutor] The very first program in python In-Reply-To: References: Message-ID: On 15/11/15 03:23, Sajjadul Islam wrote: > The following suggestion is not clear to me : > > " > If you defined your function in a separate module using IDLE > you can use the run menu to run the module directly. That will > in turn load it into the IDLE shell for you to use. > " I assumed (incorrectly as it turns out) that you were using the IDLE IDE to write and run your code. You can safely ignore it. > 3. Send the list as input argument to the function in the .py file. > > Eventually, I got the error message as mentioned in my previous post. The advice about importing the file is what you need to follow. > How to call the run menu once you are in the command line at linux env ? This only applies to the IDLE IDE, You don't seem to be using that. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From crusier at gmail.com Mon Nov 16 02:41:54 2015 From: crusier at gmail.com (Crusier) Date: Mon, 16 Nov 2015 15:41:54 +0800 Subject: [Tutor] Unable to retreive the stock code Message-ID: Dear All, I am currently trying to download the stock code. I am using Python 3.4 and the code is as follows: from bs4 import BeautifulSoup import requests import re url = 'https://www.hkex.com.hk/eng/market/sec_tradinfo/stockcode/eisdeqty.htm' def web_scraper(url): response = requests.get(url) html = response.content soup = BeautifulSoup(html,"html.parser") for link in soup.find_all("a"): stock_code = re.search('/d/d/d/d/d', "00001" ) print(stock_code, '', link.text) print(link.text) web_scraper(url) I am trying to retrieve the stock code from here: 00001 or from a href. Please kindly inform which library I should use. Thanks Henry From cs at zip.com.au Mon Nov 16 02:53:56 2015 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 16 Nov 2015 18:53:56 +1100 Subject: [Tutor] Unable to retreive the stock code In-Reply-To: References: Message-ID: <20151116075356.GA91672@cskk.homeip.net> On 16Nov2015 15:41, Crusier wrote: >I am currently trying to download the stock code. I am using Python >3.4 and the code is as follows: [...] > for link in soup.find_all("a"): > stock_code = re.search('/d/d/d/d/d', "00001" ) > print(stock_code, '', link.text) [...] >I am trying to retrieve the stock code from here: >00001 >or from a href. Well it looks like you have all the needed libraries. You're doing a few things wrong above. Firstly, to match a digit you need "\d", not "/d". Secondly, your use of "re.search" searches the literal string "00001" instead of, presumably, the value of link.text. Thirdly, the return from "re.search" is not a stock code but a "match object"; I would not call it "stock_code" but "stock_code_match". That will contain a reference to the stock code; since your regexp matches the stock code then stock_code_match.group(0) will return the actual matched text, the stock code. Fix that stuff and see where you are. Cheers, Cameron Simpson From __peter__ at web.de Mon Nov 16 04:17:24 2015 From: __peter__ at web.de (Peter Otten) Date: Mon, 16 Nov 2015 10:17:24 +0100 Subject: [Tutor] Unable to retreive the stock code References: Message-ID: Crusier wrote: > Dear All, > > I am currently trying to download the stock code. I am using Python > 3.4 and the code is as follows: > > from bs4 import BeautifulSoup > import requests > import re > > url = > 'https://www.hkex.com.hk/eng/market/sec_tradinfo/stockcode/eisdeqty.htm' > > def web_scraper(url): > response = requests.get(url) > html = response.content > soup = BeautifulSoup(html,"html.parser") > for link in soup.find_all("a"): > stock_code = re.search('/d/d/d/d/d', "00001" ) > print(stock_code, '', link.text) > print(link.text) > > web_scraper(url) > > I am trying to retrieve the stock code from here: > 00001 > > or from a href. > > Please kindly inform which library I should use. The good news is that you don't need regular expressions here, just beautiful soup is sufficient. Have a look at the html source of eisdeqty.html in a text editor, and then use the interactive interpreter to get closer to the desired result: Python 3.4.3 (default, Oct 14 2015, 20:28:29) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import requests >>> from bs4 import BeautifulSoup >>> url = 'https://www.hkex.com.hk/eng/market/sec_tradinfo/stockcode/eisdeqty.htm' >>> soup = BeautifulSoup(requests.get(url).content) [snip some intermediate attempts] >>> soup.html.body.table.table.table.table.tr.td STOCK CODE >>> stock_codes = [tr.td.text for tr in soup.html.body.table.table.table.table.find_all("tr")] >>> stock_codes[:10] ['STOCK CODE', '00001', '00002', '00003', '00004', '00005', '00006', '00007', '00008', '00009'] >>> stock_codes[-10:] ['06882', '06886', '06888', '06889', '06893', '06896', '06898', '06899', '80737', '84602'] From soweto at gmail.com Mon Nov 16 07:17:00 2015 From: soweto at gmail.com (Vusa Moyo) Date: Mon, 16 Nov 2015 14:17:00 +0200 Subject: [Tutor] os.popen - using commands and input % Message-ID: Hi Guys, OS = SuSE Enterprise Linux Python V2.7 My code is as follows # this list contains system process ID's pidst=[1232, 4543, 12009] pmap_str=[] command="pmap -d %s | grep private |awk '{print $1}' | awk -FK '{print $1}'" for i in range(len(pids)): pmap_str.append(os.popen("(command) % pidlist_int[i])")) # <-- this is where I need help, please As I'm sure you can see, I'm trying to output the os.popen output to a new list. On the shell console I can run the pmap command as follows pmap -d | grep private |awk '{print $1}' | awk -FK '{print $1}' Output will be a single number such as 485921. My error is on the line of code shown above. Please assist. Kind Regards From soweto at gmail.com Mon Nov 16 08:07:45 2015 From: soweto at gmail.com (Vusa Moyo) Date: Mon, 16 Nov 2015 15:07:45 +0200 Subject: [Tutor] os.popen - using commands and input % In-Reply-To: References: Message-ID: The following code seems to be pointing me to the right direction, BUT, my list has 0's instead of the output generated. >>> for i in range(len(pids)): ... final.append(subprocess.call(["sudo pmap -d %s | grep private |awk '{print $1}' | awk -FK '{print $1}'" % pids[i]], shell=True)) ... 60772 106112 3168 13108 14876 8028 3328 8016 139424 6037524 5570492 4128 144364 154980 154980 >>> pmap_str [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] I;m assuming the zero's are exit codes, which then populate the list, which is not what I'm after. . On Mon, Nov 16, 2015 at 2:17 PM, Vusa Moyo wrote: > Hi Guys, > > OS = SuSE Enterprise Linux > Python V2.7 > > My code is as follows > > # this list contains system process ID's > pidst=[1232, 4543, 12009] > > pmap_str=[] > command="pmap -d %s | grep private |awk '{print $1}' | awk -FK '{print > $1}'" > > for i in range(len(pids)): > pmap_str.append(os.popen("(command) % pidlist_int[i])")) # <-- > this is where I need help, please > > As I'm sure you can see, I'm trying to output the os.popen output to a new > list. > > On the shell console I can run the pmap command as follows > > pmap -d | grep private |awk '{print $1}' | awk -FK '{print $1}' > > Output will be a single number such as 485921. > > My error is on the line of code shown above. Please assist. > > Kind Regards > From soweto at gmail.com Mon Nov 16 09:05:01 2015 From: soweto at gmail.com (Vusa Moyo) Date: Mon, 16 Nov 2015 16:05:01 +0200 Subject: [Tutor] os.popen - using commands and input % In-Reply-To: References: Message-ID: SOLVED> the code I used was. for i in range(len(pids)): final.append(subprocess.Popen(["sudo pmap -d %s | grep private |awk '{print $1}' | awk -FK '{print $1}'" % pids[i]], shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE).communicate()[0]) Allowed me to append the subprocess output to my list. Thanks for the help everyone :-) On Mon, Nov 16, 2015 at 3:07 PM, Vusa Moyo wrote: > The following code seems to be pointing me to the right direction, BUT, my > list has 0's instead of the output generated. > > >>> for i in range(len(pids)): > ... final.append(subprocess.call(["sudo pmap -d %s | grep private |awk > '{print $1}' | awk -FK '{print $1}'" % pids[i]], shell=True)) > ... > 60772 > 106112 > 3168 > 13108 > 14876 > 8028 > 3328 > 8016 > 139424 > 6037524 > 5570492 > 4128 > 144364 > 154980 > 154980 > >>> pmap_str > [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] > > I;m assuming the zero's are exit codes, which then populate the list, > which is not what I'm after. . > > On Mon, Nov 16, 2015 at 2:17 PM, Vusa Moyo wrote: > >> Hi Guys, >> >> OS = SuSE Enterprise Linux >> Python V2.7 >> >> My code is as follows >> >> # this list contains system process ID's >> pidst=[1232, 4543, 12009] >> >> pmap_str=[] >> command="pmap -d %s | grep private |awk '{print $1}' | awk -FK '{print >> $1}'" >> >> for i in range(len(pids)): >> pmap_str.append(os.popen("(command) % pidlist_int[i])")) # <-- >> this is where I need help, please >> >> As I'm sure you can see, I'm trying to output the os.popen output to a >> new list. >> >> On the shell console I can run the pmap command as follows >> >> pmap -d | grep private |awk '{print $1}' | awk -FK '{print $1}' >> >> Output will be a single number such as 485921. >> >> My error is on the line of code shown above. Please assist. >> >> Kind Regards >> > > From sajjadul.islam.bd at gmail.com Mon Nov 16 04:55:44 2015 From: sajjadul.islam.bd at gmail.com (Sajjadul Islam) Date: Mon, 16 Nov 2015 10:55:44 +0100 Subject: [Tutor] Debugging in Python Message-ID: Hello forum, I am trying Python 3.4 on Ubuntu and I am a bit confused with the debugging scope of python in general. I wrote a small function and then I tried to run with the following call: /////////////////////////// import hilbert hilbert.hilbert(3) /////////////////////////// Please note that , I am not using the IDLE IDE. Instead I am using the typical IDE that shows up with the command "python3" in the linux command line. I encountered some error in the source , then I fixed it and tried to run the module with the above snippet again , but the the error prevails even though the error part is commented and the updated version saved. If I get out of the python3 console entirely, get into it again, import the module and execute, then the last update in the source is effected. Why is it so clumsy ? I believe that there is better way to program in python which I am missing ? Thanks From alan.gauld at btinternet.com Mon Nov 16 09:38:29 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 16 Nov 2015 14:38:29 +0000 Subject: [Tutor] os.popen - using commands and input % In-Reply-To: References: Message-ID: On 16/11/15 14:05, Vusa Moyo wrote: > SOLVED> the code I used was. > > for i in range(len(pids)): > final.append(subprocess.Popen(["sudo pmap -d %s | grep private |awk > '{print $1}' | awk -FK '{print $1}'" % pids[i]], shell=True, Glad you solved it and using subprocess is fine for the pmap stuff. But the grep and awk stuff are usually a lot faster and almost as easy using Python. eg. for line in pmap_output if private in line: print line.split()[0] # {print $1} Or, to create a list instead of printing col1 = [line.split()[0] for line in pmap_output if private in line] I can't remember what -FK does (F is field sep but K?) But I'm fairly sure it will be easy to replicate in Python. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Mon Nov 16 09:43:55 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 16 Nov 2015 14:43:55 +0000 Subject: [Tutor] Debugging in Python In-Reply-To: References: Message-ID: On 16/11/15 09:55, Sajjadul Islam wrote: > Hello forum, > > I am trying Python 3.4 on Ubuntu and I am a bit confused with the debugging > scope of python in general. > > I wrote a small function and then I tried to run with the following call: > > /////////////////////////// > > import hilbert > hilbert.hilbert(3) > > /////////////////////////// > > Please note that , I am not using the IDLE IDE. Instead I am using the > typical IDE that shows up with the command "python3" in the linux command > line. Thats not an IDE its just a raw interpreter. IDLE is a full IDE that includes a debugger. > I encountered some error in the source , then I fixed it and tried to run > the module with the above snippet again , but the the error prevails even > though the error part is commented and the updated version saved. You need to reload the module but sadly there is no simple command to do that in the interpreter. There is in IDLE ("Restart Shell" menu item) So if you use IDLE your issues will be resolved. You can install IDLE3 from the Ubuntu package manager. > Why is it so clumsy ? I believe that there is better way to program in > python which I am missing ? Use IDLE :-) Or any of the other IDEs. Alternatively use multiple windows. I personally prefer to have a vim window for editing the code and a separate terminal for running it. But that assumes I have a main file and can just type $ python myfile.py. In the terminal. If I'm only creating a module I use the IDLE shell instead of the terminal window. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From kwpolska at gmail.com Mon Nov 16 10:42:32 2015 From: kwpolska at gmail.com (Chris Warrick) Date: Mon, 16 Nov 2015 16:42:32 +0100 Subject: [Tutor] Debugging in Python In-Reply-To: References: Message-ID: On 16 November 2015 at 15:43, Alan Gauld wrote: > Thats not an IDE its just a raw interpreter. > IDLE is a full IDE that includes a debugger. It?s an awful piece of garbage that pretends to be an IDE. >> I encountered some error in the source , then I fixed it and tried to run >> the module with the above snippet again , but the the error prevails even >> though the error part is commented and the updated version saved. > > > You need to reload the module but sadly there is no simple command to do > that in the interpreter. There is in IDLE ("Restart Shell" menu item) > > So if you use IDLE your issues will be resolved. You can install > IDLE3 from the Ubuntu package manager. No, they won?t. They will be replaced by a much worse and less friendly IDE. Please don?t bother installing IDLE and use the normal `python3` shell, ipython or bpython. The correct fix is to exit() from the python3 shell and start it again. Alternatively, add some main code at the end of your file and use `python3 hlibert.py`: if __name__ == '__main__': hilbert(3) -- Chris Warrick PGP: 5EAAEA16 From alan.gauld at btinternet.com Mon Nov 16 12:48:00 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 16 Nov 2015 17:48:00 +0000 Subject: [Tutor] Debugging in Python In-Reply-To: References: Message-ID: On 16/11/15 15:42, Chris Warrick wrote: > On 16 November 2015 at 15:43, Alan Gauld wrote: >> Thats not an IDE its just a raw interpreter. >> IDLE is a full IDE that includes a debugger. > > It?s an awful piece of garbage that pretends to be an IDE. Would you care to expand. Its been doing a fair impression for the past 20 odd years. And in its IDLEX incarnation most of the niggles have been removed. It's not Eclipse but it doesn't pretend to be. But for beginners like the OP its adequate IMHO. Certainly easier than the raw interpreter prompt. >> You need to reload the module but sadly there is no simple command to do >> that in the interpreter. There is in IDLE ("Restart Shell" menu item) >> >> So if you use IDLE your issues will be resolved. You can install >> IDLE3 from the Ubuntu package manager. > > No, they won?t. They will be replaced by a much worse and less > friendly IDE. Please don?t bother installing IDLE and use the normal > `python3` shell, ipython or bpython. He's tried the normal shell and that didn't work for him. iPython and bPython are certainly other more advanced options but he needs to install them too and both have a steeper learning curve for the average user than IDLE. > The correct fix is to exit() from the python3 shell and start it again. He's tried that and didn't find it satisfactory. That's why he wants a "better" workflow. > Alternatively, add some main code at the end of your file and use > `python3 hlibert.py`: > > if __name__ == '__main__': > hilbert(3) That depends on the nature of his module and the type of testing he wants to do. He could, I agree, create a new test file that imports hilbert and then add his test code there but that loses the interactivity of a shell prompt which may be important. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From clyxx001 at odu.edu Mon Nov 16 10:30:27 2015 From: clyxx001 at odu.edu (CUONG LY) Date: Mon, 16 Nov 2015 10:30:27 -0500 Subject: [Tutor] question about "__main__" Message-ID: <8815A1FC-A9C7-4873-947A-767C52406CD5@odu.edu> Hello, I?m learning Python. I want to know what does the following line represent and why I see some python codes have two of them ? if __name__ == ?__main__?: Thanks in advance Cuong From alan.gauld at btinternet.com Mon Nov 16 13:06:51 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 16 Nov 2015 18:06:51 +0000 Subject: [Tutor] question about "__main__" In-Reply-To: <8815A1FC-A9C7-4873-947A-767C52406CD5@odu.edu> References: <8815A1FC-A9C7-4873-947A-767C52406CD5@odu.edu> Message-ID: On 16/11/15 15:30, CUONG LY wrote: > Hello, > > I?m learning Python. Hello, welcome. > I want to know what does the following line represent > if __name__ == ?__main__?: The short answer is that it tests whether the file is being imported as a module or executed as a program. If its being imported as a module its __name__ attribute will be set to the name of the module. If its being executed as the main (top level) program file its __name__ will be set to "__main__". So when you see: if __name__ == ?__main__?: # some code here the 'some code here' block will only be executed if the file is being executed as a program. It will not be executed if the file is being imported by another file. That will only make sense if you have come across modules yet. If not don't worry about it, you will get there eventually. Then it will all become clear. > and why I see some python codes have two of them ? I don't know, I've never seen two. But it is perfectly acceptable to do it, just a little pointless - unless perhaps they are inside a function or something. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From gokoproject at gmail.com Mon Nov 16 17:33:00 2015 From: gokoproject at gmail.com (John Wong) Date: Mon, 16 Nov 2015 17:33:00 -0500 Subject: [Tutor] Debugging in Python In-Reply-To: References: Message-ID: On Mon, Nov 16, 2015 at 12:48 PM, Alan Gauld wrote: > > > The correct fix is to exit() from the python3 shell and start it again. >> > > He's tried that and didn't find it satisfactory. That's why > he wants a "better" workflow. > > Alternatively, add some main code at the end of your file and use >> `python3 hlibert.py`: >> >> if __name__ == '__main__': >> hilbert(3) >> > > That depends on the nature of his module and the type of testing he wants > to do. He could, I agree, create a new test file that imports hilbert and > then add his test code there but that loses the interactivity of a shell > prompt which may be important. Yeah, the only way you can actually keep trying is to write some kind of test script. It doesn't have to be using any testing library. i.e. whatever code you throw at the console for testing can be written down in another python file. I call that a scratch pad. Obviously a proper test is desirable. I too recommend investing time in that. Doesn't matter whether you use a scratch pad or not, you can use PDB as your debugger. I often use it to step into my code and hold on the execution. I often do this when I need to inspect what an object instance can do... for example: main.py def main(): import pdb; pdb.set_trace() file_object = some_complex_code_written_by_someone_else() do_stuff_with_file_object(file_object) main() Now when I run main.py, I get an interpreter session inside the PDB debugger (it's similar to GDB if you have written C/C++ code). I can now type dir(file_object), play with file_object, or step inside code to inspect errors. Hope this helps. Thanks. John From caroline.metcalf at aloha-college.com Tue Nov 17 07:27:03 2015 From: caroline.metcalf at aloha-college.com (caroline metcalf) Date: Tue, 17 Nov 2015 13:27:03 +0100 Subject: [Tutor] Help - International School in Spain Message-ID: Hi there I am having an issue. This code normally works fine Code: veg="carrots" print("Mashed ",veg," on the ceiling.") print("Green beans on the floor.") print("Stewed tomatoes in the corner.") print("Squash upon the door.") But, I am getting this. Result: ('Mashed ', 'carrots', ' on the ceiling.') Green beans on the floor. Stewed tomatoes in the corner. Squash upon the door. >>> I have tried semi-colons instead of commas as Spain uses these often but... I don't know what else to try. Any ideas please? -- Caroline Metcalf *Head of Business, Economics and Technology (BET)* *Responsable Dept. Empresa y Gesti?n, Economia y Technologia* *ALOHA COLLEGE* *Tel: (+34) 952 814 133* www.aloha-college.com Antes de imprimir este mensaje, aseg?rese de que es necesario - Before you print this message, make sure that it is necessary CONFIDENTIALITY WARNING This message is intended exclusively for its addressee and may contain confidential information. If you have received this message in error, please notify us immediately via e-mail and delete it and any attachment. AVISO DE CONFIDENCIALIDAD Mensaje dirigido de manera exclusiva a su destinatario y puede contener informaci?n confidencial. Si ha recibido este mensaje por error, le rogamos nos lo comunique de forma inmediata por esta misma v?a y proceda a su eliminaci?n, as? como a la de cualquier documento adjunto al mismo. From payo2000 at gmail.com Tue Nov 17 10:58:39 2015 From: payo2000 at gmail.com (Pa Yo) Date: Tue, 17 Nov 2015 15:58:39 +0000 Subject: [Tutor] Help - International School in Spain In-Reply-To: References: Message-ID: Might you be using two single inverted commas '' rather than one inverted comma " Just a thought. Also check the use of acute accents and inverted commas, they are easy to mix up on a Spanish keyboard - depending on the font you use. PY On Tuesday, 17 November 2015, caroline metcalf < caroline.metcalf at aloha-college.com> wrote: > Hi there > > I am having an issue. This code normally works fine > > Code: > veg="carrots" > print("Mashed ",veg," on the ceiling.") > print("Green beans on the floor.") > print("Stewed tomatoes in the corner.") > print("Squash upon the door.") > > But, I am getting this. Result: > ('Mashed ', 'carrots', ' on the ceiling.') > Green beans on the floor. > Stewed tomatoes in the corner. > Squash upon the door. > >>> > > I have tried semi-colons instead of commas as Spain uses these often but... > I don't know what else to try. Any ideas please? > > > -- > > Caroline Metcalf > > *Head of Business, Economics and Technology (BET)* > > *Responsable Dept. Empresa y Gesti?n, Economia y Technologia* > > *ALOHA COLLEGE* > > *Tel: (+34) 952 814 133* > > www.aloha-college.com > > Antes de imprimir este mensaje, aseg?rese de que es necesario - Before > you print this message, make sure that it is necessary > > CONFIDENTIALITY WARNING > This message is intended exclusively for its addressee and may contain > confidential information. If you have received this message in error, > please notify us immediately via e-mail and delete it and any attachment. > > AVISO DE CONFIDENCIALIDAD > Mensaje dirigido de manera exclusiva a su destinatario y puede contener > informaci?n confidencial. Si ha recibido este mensaje por error, le rogamos > nos lo comunique de forma inmediata por esta misma v?a y proceda a su > eliminaci?n, as? como a la de cualquier documento adjunto al mismo. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at btinternet.com Tue Nov 17 11:03:21 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 17 Nov 2015 16:03:21 +0000 Subject: [Tutor] Help - International School in Spain In-Reply-To: References: Message-ID: On 17/11/15 12:27, caroline metcalf wrote: > veg="carrots" > print("Mashed ",veg," on the ceiling.") > print("Green beans on the floor.") > > But, I am getting this. Result: > ('Mashed ', 'carrots', ' on the ceiling.') > Green beans on the floor. > > I have tried semi-colons instead of commas as Spain uses these often but... > I don't know what else to try. Any ideas please? Your code is for Python v3 but you seem to be running it using Python v2. This could be a problem with: 1) How you execute the script (ie what you type) 2) where you execute it from (which folder) 3) Your computer's configuration (did v3 get un-installed, or v2 get re-installed?) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Tue Nov 17 11:07:49 2015 From: __peter__ at web.de (Peter Otten) Date: Tue, 17 Nov 2015 17:07:49 +0100 Subject: [Tutor] Help - International School in Spain References: Message-ID: caroline metcalf wrote: > Hi there > > I am having an issue. This code normally works fine > > Code: > veg="carrots" > print("Mashed ",veg," on the ceiling.") > print("Green beans on the floor.") > print("Stewed tomatoes in the corner.") > print("Squash upon the door.") > > But, I am getting this. Result: > ('Mashed ', 'carrots', ' on the ceiling.') > Green beans on the floor. > Stewed tomatoes in the corner. > Squash upon the door. >>>> > > I have tried semi-colons instead of commas as Spain uses these often > but... > I don't know what else to try. Any ideas please? The code you show is written for Python 3 where print() is a function, but you run it under Python 2 where print was a statement and the line > print("Mashed ",veg," on the ceiling.") tells Python to print the tuple ("Mashed ",veg," on the ceiling.") instead of invoking the print() function. As a fix I recommend that you ensure that you use a Python 3 interpreter. Your other options are: 1. Remove the parens: print "Mashed ",veg," on the ceiling." # python 2 only 2. Add from __future__ import print_function # make print() available in py2 3 Use string formatting: print("Mashed {} on the ceiling.".format(veg)) Parens around a single value are ignored, so here print works both as a statement and as a function. From __peter__ at web.de Tue Nov 17 11:21:00 2015 From: __peter__ at web.de (Peter Otten) Date: Tue, 17 Nov 2015 17:21 +0100 Subject: [Tutor] Help - International School in Spain References: Message-ID: Peter Otten wrote: >> print("Mashed ",veg," on the ceiling.") An unrelated note: Python automatically adds spaces between the printed values, so the output of print("Mashed", veg, "on the ceiling.") will look better. From mike at thestraws.net Tue Nov 17 11:00:49 2015 From: mike at thestraws.net (Mike Straw) Date: Tue, 17 Nov 2015 16:00:49 +0000 Subject: [Tutor] Help - International School in Spain In-Reply-To: References: Message-ID: Caroline, The parentheses change the interpretation for the print statement. If you put it without the parentheses, it will work fine. print "Mashed ",veg," on the ceiling." Also, another note: the spaces at end of the first string and the start of the third string aren't necessary. When you use multiple parameters, print will automatically put a space between them. HTH On Tue, Nov 17, 2015 at 10:53 AM caroline metcalf < caroline.metcalf at aloha-college.com> wrote: > Hi there > > I am having an issue. This code normally works fine > > Code: > veg="carrots" > print("Mashed ",veg," on the ceiling.") > print("Green beans on the floor.") > print("Stewed tomatoes in the corner.") > print("Squash upon the door.") > > But, I am getting this. Result: > ('Mashed ', 'carrots', ' on the ceiling.') > Green beans on the floor. > Stewed tomatoes in the corner. > Squash upon the door. > >>> > > I have tried semi-colons instead of commas as Spain uses these often but... > I don't know what else to try. Any ideas please? > > > -- > > Caroline Metcalf > > *Head of Business, Economics and Technology (BET)* > > *Responsable Dept. Empresa y Gesti?n, Economia y Technologia* > > *ALOHA COLLEGE* > > *Tel: (+34) 952 814 133* > > www.aloha-college.com > > Antes de imprimir este mensaje, aseg?rese de que es necesario - Before > you print this message, make sure that it is necessary > > CONFIDENTIALITY WARNING > This message is intended exclusively for its addressee and may contain > confidential information. If you have received this message in error, > please notify us immediately via e-mail and delete it and any attachment. > > AVISO DE CONFIDENCIALIDAD > Mensaje dirigido de manera exclusiva a su destinatario y puede contener > informaci?n confidencial. Si ha recibido este mensaje por error, le rogamos > nos lo comunique de forma inmediata por esta misma v?a y proceda a su > eliminaci?n, as? como a la de cualquier documento adjunto al mismo. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From dyoo at hashcollision.org Tue Nov 17 21:06:45 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 17 Nov 2015 18:06:45 -0800 Subject: [Tutor] os.popen - using commands and input % In-Reply-To: References: Message-ID: On Mon, Nov 16, 2015 at 4:17 AM, Vusa Moyo wrote: > Hi Guys, > > My code is as follows > > # this list contains system process ID's > pidst=[1232, 4543, 12009] > > pmap_str=[] > command="pmap -d %s | grep private |awk '{print $1}' | awk -FK '{print $1}'" > > for i in range(len(pids)): > pmap_str.append(os.popen("(command) % pidlist_int[i])")) Hi Vusa, If you can, please try to avoid using os.popen() or subprocess.call() with formatted strings: that's a common security risk that's known as a "code injection" vulnerability. https://en.wikipedia.org/wiki/Code_injection More generally, your program is over-using strings to do things that they aren't meant to do. One of the habits of the beginner is to try to do everything in terms of strings, since strings are flexible. But this approach often ignores features of the language that might be more appropriate. I brought up that constructing the command pipeline string is one thing to try to avoid with string formatting. But it tries to use strings in another way that is somewhat unnatural: it is trying to encode the idea of re-using the command pipeline so you can feed it different pids "(command) % pidlist_int[i]) This idea of reuse is admirable, but the execution with string formatting can be improved. An alternative approach can use the language's natural construct for reusability: the function. We can write a function to construct a command that is parameterized on the given pid. The rest of this message will sketch out what this looks like. --- What you're doing here: pmap -d | grep private |awk '{print $1}' | awk -FK '{print $1}' can be translated to a sequence of operations that does not use any string formatting to construct command lines. See: https://docs.python.org/2/library/subprocess.html#replacing-shell-pipeline. If we apply this to your command, we should expect to see something like this (untested) ################################# def get_pmap(pid): p1 = Popen(['pmap', '-d', str(pid)], stdout=PIPE) p2 = Popen(['grep', 'private'], stdin=p1.stdout, stdout=PIPE) p3 = Popen(['awk', '{print $1}'], stdin=p2.stdout, stdout=PIPE) p4 = Popen(['awk', 'FK', '{print $1}'], stdin=p3.stdout, stdout=PIPE) p1.stdout.close() return p4.communicate()[0] ################################# Here, we package up this collection of statements into a function that takes in a pid and returns the result. Note that we don't have to do any string concatenation or string formatting. After which, we can apply this function across your list directly. pmap_str = [get_pmap(pid) for pid in pidst] From mimikveh2 at gmail.com Tue Nov 17 19:48:25 2015 From: mimikveh2 at gmail.com (John Spitz) Date: Tue, 17 Nov 2015 16:48:25 -0800 Subject: [Tutor] how to debug python script which is a parameter of first Message-ID: Hi, Python 3.4 CentOS 6.5 Using IDE eclipse with pydev, how do I debug a python script which is the parameter of the first script? For example I'm writing selenium web driver tests in the cloud with BrowserStack and they have a parallel testing script which is the first python script following my script and a json file like this: python3.4 run_parallel_tests.py clickurl.py browsers.json So in Eclipse, on the run_parallel_tests.py, I have the "Debug Configurations" > Arguments tab with: clickurl.py browsers.json but when adding a breakpoint to the clickurl.py it doesn't work??? Please help, thanks, John From goleary1 at hwemail.com Tue Nov 17 13:58:37 2015 From: goleary1 at hwemail.com (Gavin O'Leary) Date: Tue, 17 Nov 2015 18:58:37 +0000 Subject: [Tutor] Guessing program with a twist isn't working Message-ID: Hello all, I have been trying to write a program that has the user think of a number between 1 and 1000, and the computer will guess the number in 10 or less tries. It isn?t working there. Here is my code. import sys high = 1000 low = 0 print "Think of a number between 1 and 1000 but don't tell me!" def ask(x): while True: print "I guess %d" % x answer = raw_input("Is that right, too high, or too low?: ") if answer == 'right': print "Lucky me!" sys.exit(0) elif answer == 'too low': return 1 elif answer == 'too high': return -1 else: print "I don't understand" while True: guess = (high + low) / 2 ask(guess) if ask(guess == 1): guess = (high + guess) / 2 elif ask(guess == -1): guess = (high - guess) / 2 else: sys.exit(0) Can somebody help me please? ?Gavin O'Leary From alan.gauld at btinternet.com Wed Nov 18 07:10:05 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 18 Nov 2015 12:10:05 +0000 Subject: [Tutor] Guessing program with a twist isn't working In-Reply-To: References: Message-ID: On 17/11/15 18:58, Gavin O'Leary wrote: > def ask(x): > while True: > print "I guess %d" % x > answer = raw_input("Is that right, too high, or too low?: ") > if answer == 'right': > print "Lucky me!" > sys.exit(0) > elif answer == 'too low': > return 1 > elif answer == 'too high': > return -1 > else: > print "I don't understand" This is OK, but notice that the function returns the result for high and low but exits the entire program if you are successful. It would be more consistent to return another result (0 maybe?) for success. > while True: > guess = (high + low) / 2 > ask(guess) Here you call the function but do not store the result anywhere. You need something like: result = ask(guess) > if ask(guess == 1): And this is just plain wrong. What you are doing is passing a boolean (truth) value into your function which will interpret it as 1 or 0. Thats not what you want. Instead I suspect you want something like: if result == 1: ... elif result == -1: ... > guess = (high + guess) / 2 > elif ask(guess == -1): > guess = (high - guess) / 2 > else: > sys.exit(0) Notice that the last bit can never happen because your ask function performs the sys.exit. This is why I suggested returning zero for success. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Wed Nov 18 07:15:32 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 18 Nov 2015 12:15:32 +0000 Subject: [Tutor] how to debug python script which is a parameter of first In-Reply-To: References: Message-ID: On 18/11/15 00:48, John Spitz wrote: > Using IDE eclipse with pydev, how do I debug a python script which is > the parameter of the first script? > ... > python3.4 run_parallel_tests.py clickurl.py browsers.json Its almost impossible to say since we don;t know what you re doing in your tests script. Assuming you just call it using subprocess or similar then its next to impossible since you are launching another process. The best bet in that case is to insert print statements but that then modifies the code which is not ideal in a test environment. > So in Eclipse, on the run_parallel_tests.py, I have the "Debug > Configurations" > Arguments tab with: > clickurl.py browsers.json > > but when adding a breakpoint to the clickurl.py it doesn't work??? Breakpoints will only work in the process you are running in the IDE. We would need to see how the test script works to advise further. There are several options, none of them particularly easy. If you want to debug clickurl.py it would be better to work on it directly and just use the tests script to identify the need to debug it. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From jarod_v6 at libero.it Thu Nov 19 05:06:22 2015 From: jarod_v6 at libero.it (jarod_v6 at libero.it) Date: Thu, 19 Nov 2015 11:06:22 +0100 (CET) Subject: [Tutor] Strange error Message-ID: <1919898783.77731447927582288.JavaMail.defaultUser@defaultHost> HI!! This is my list: In [378]: type(Span) Out[378]: list In [379]: Span Out[379]: [['M02898:39:000000000-AH4BK:1:2107:17412:10850', 'M02898:39:000000000-AH4BK:1:2117:15242:18766', 'M02898:39:000000000-AH4BK:1:1112:21747:21214', 'M02898:39:000000000-AH4BK:1:2112:5119:9813', 'M02898:39:000000000-AH4BK:1:1102:26568:5630', 'M02898:39:000000000-AH4BK:1:2118:19680:11792', 'M02898:39:000000000-AH4BK:1:1103:5469:6578', 'M02898:39:000000000-AH4BK:1:2101:13087:20965', 'M02898:39:000000000-AH4BK:1:1103:28031:13653', 'M02898:39:000000000-AH4BK:1:1103:8013:21346', 'M02898:39:000000000-AH4BK:1:1107:9189:22557', 'M02898:39:000000000-AH4BK:1:2118:21263:23091', 'M02898:39:000000000-AH4BK:1:1115:12279:20054', 'M02898:39:000000000-AH4BK:1:1102:19433:17489', 'M02898:39:000000000-AH4BK:1:1110:14533:11792', 'M02898:39:000000000-AH4BK:1:2106:18027:12878', 'M02898:39:000000000-AH4BK:1:1104:4408:6824', 'M02898:39:000000000-AH4BK:1:2101:5678:7400']] I have this error In [381]: len(Span) --------------------------------------------------------------------------- TypeError Traceback (most recent call last) in () ----> 1 len(Span) TypeError: 'str' object is not callable Why??? IS a list!!! From __peter__ at web.de Thu Nov 19 05:31:56 2015 From: __peter__ at web.de (Peter Otten) Date: Thu, 19 Nov 2015 11:31:56 +0100 Subject: [Tutor] Strange error References: <1919898783.77731447927582288.JavaMail.defaultUser@defaultHost> Message-ID: jarod_v6--- via Tutor wrote: > HI!! > This is my list: > > In [378]: type(Span) > Out[378]: list > > In [379]: Span > Out[379]: > [['M02898:39:000000000-AH4BK:1:2107:17412:10850', > 'M02898:39:000000000-AH4BK:1:2117:15242:18766', > 'M02898:39:000000000-AH4BK:1:1112:21747:21214', > 'M02898:39:000000000-AH4BK:1:2112:5119:9813', > 'M02898:39:000000000-AH4BK:1:1102:26568:5630', > 'M02898:39:000000000-AH4BK:1:2118:19680:11792', > 'M02898:39:000000000-AH4BK:1:1103:5469:6578', > 'M02898:39:000000000-AH4BK:1:2101:13087:20965', > 'M02898:39:000000000-AH4BK:1:1103:28031:13653', > 'M02898:39:000000000-AH4BK:1:1103:8013:21346', > 'M02898:39:000000000-AH4BK:1:1107:9189:22557', > 'M02898:39:000000000-AH4BK:1:2118:21263:23091', > 'M02898:39:000000000-AH4BK:1:1115:12279:20054', > 'M02898:39:000000000-AH4BK:1:1102:19433:17489', > 'M02898:39:000000000-AH4BK:1:1110:14533:11792', > 'M02898:39:000000000-AH4BK:1:2106:18027:12878', > 'M02898:39:000000000-AH4BK:1:1104:4408:6824', > 'M02898:39:000000000-AH4BK:1:2101:5678:7400']] > > I have this error > > > In [381]: len(Span) > --------------------------------------------------------------------------- > TypeError Traceback (most recent call > last) in () > ----> 1 len(Span) > > TypeError: 'str' object is not callable > > > > Why??? IS a list!!! My crystal ball says that this is self-inflicted pain. You shaded the built- by typing In [81]: len = "whatever" and then forgot about it until after 300 more lines. To make the built-in len() function visible again delete the string in the global namespace of the __main__ module with In [382]: del len From glchristian at comcast.net Thu Nov 19 10:31:47 2015 From: glchristian at comcast.net (Greg Christian) Date: Thu, 19 Nov 2015 08:31:47 -0700 Subject: [Tutor] Using sorted in Python 3.3.5 Message-ID: <101B16CEC1154914BF0CA97F5D5406AF@GregsLaptopPC> I?m trying to sort a list of tuples based on the second item in the tuple. When I run this in IDLE I get the correct output; however, when running inside of a program, and calling the counter() function, sorted does not seem to work? Any ideas on why this works in IDLE and not in program would be appreciated. Thank You. def getKey(item): return item[1] def counter(): L = [("baby", 9999), ("aeuron", 100), ("pablo", 1234)] sorted(L, key=getKey) print ("L = ", L) OUTPUTS THIS (when calling counter inside of program): L = [('baby', 9999), ('aeuron', 100), ('pablo', 1234)] OUTPUTS THIS (when running with IDLE ? desired output): [('aeuron', 100), ('pablo', 1234), ('baby', 9999)] From python at thestraws.net Thu Nov 19 08:25:11 2015 From: python at thestraws.net (Mike) Date: Thu, 19 Nov 2015 13:25:11 +0000 Subject: [Tutor] unittest not working Message-ID: I'm trying to unit test a self-built regular expression processor for an assignment. I'm trying to set up unit tests for the package, but it's not executing them. This is my first time trying to use the unittest module, so I'm sure I'm missing something, I'm just not sure what. I even put a test case in there I knew would fail just to try it. Unit Test code: import unittest from regex import regexp class RegexTest(unittest.TestCase): def fail_test(self): self.assertEqual(1, 2) def basic_test(self): self.assertEqual(regexp('Hello', 'Goodbye'), '') self.assertEqual(regexp('hello', 'ello'), 'ello') with self.assertRaises(SyntaxError): regexp('hello', 'he)') if __name__ == '__main__': unittest.main() Output: >>> ---------------------------------------------------------------------- Ran 0 tests in 0.000s OK Exit code: False >>> From alan.gauld at btinternet.com Thu Nov 19 13:29:02 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 19 Nov 2015 18:29:02 +0000 Subject: [Tutor] Strange error In-Reply-To: <1919898783.77731447927582288.JavaMail.defaultUser@defaultHost> References: <1919898783.77731447927582288.JavaMail.defaultUser@defaultHost> Message-ID: On 19/11/15 10:06, jarod_v6--- via Tutor wrote: > I have this error > > > In [381]: len(Span) > --------------------------------------------------------------------------- > TypeError Traceback (most recent call last) > in () > ----> 1 len(Span) > > TypeError: 'str' object is not callable To call something in Python you use parens - eg. to call f you use f() So the error is talking about whatever has parens after it. In this case that's len() And it's saying len is a string. That means somewhere in your code you have created a variable called len and assigned a string to it. You thus hide the built in function len() You need to delete the len variable. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From breamoreboy at yahoo.co.uk Thu Nov 19 13:33:46 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 19 Nov 2015 18:33:46 +0000 Subject: [Tutor] Using sorted in Python 3.3.5 In-Reply-To: <101B16CEC1154914BF0CA97F5D5406AF@GregsLaptopPC> References: <101B16CEC1154914BF0CA97F5D5406AF@GregsLaptopPC> Message-ID: On 19/11/2015 15:31, Greg Christian wrote: > I?m trying to sort a list of tuples based on the second item in the tuple. When I run this in IDLE I get the correct output; however, when running inside of a program, and calling the counter() function, sorted does not seem to work? Any ideas on why this works in IDLE and not in program would be appreciated. Thank You. > > def getKey(item): > return item[1] > > def counter(): > L = [("baby", 9999), ("aeuron", 100), ("pablo", 1234)] > sorted(L, key=getKey) > print ("L = ", L) > > OUTPUTS THIS (when calling counter inside of program): > > L = [('baby', 9999), ('aeuron', 100), ('pablo', 1234)] > > OUTPUTS THIS (when running with IDLE ? desired output): > > [('aeuron', 100), ('pablo', 1234), ('baby', 9999)] Your use of sorted achieves precisely nothing as you discard the return value. Either save the value or use the built-in sort which works in place. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From alan.gauld at btinternet.com Thu Nov 19 13:31:03 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 19 Nov 2015 18:31:03 +0000 Subject: [Tutor] Using sorted in Python 3.3.5 In-Reply-To: <101B16CEC1154914BF0CA97F5D5406AF@GregsLaptopPC> References: <101B16CEC1154914BF0CA97F5D5406AF@GregsLaptopPC> Message-ID: On 19/11/15 15:31, Greg Christian wrote: > I?m trying to sort a list of tuples based on the second item in the tuple. When I run this in IDLE I get the correct output; however, when running inside of a program, and calling the counter() function, sorted does not seem to work? Any ideas on why this works in IDLE and not in program would be appreciated. Thank You. > > def getKey(item): > return item[1] > > def counter(): > L = [("baby", 9999), ("aeuron", 100), ("pablo", 1234)] > sorted(L, key=getKey) > print ("L = ", L) > > OUTPUTS THIS (when calling counter inside of program): > > L = [('baby', 9999), ('aeuron', 100), ('pablo', 1234)] > sorted() returns the soirted collection, it does not sort it in place. If you want in-place use the sort method. > OUTPUTS THIS (when running with IDLE ? desired output): > > [('aeuron', 100), ('pablo', 1234), ('baby', 9999)] I'm not sure why IDLE does that. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Thu Nov 19 13:51:47 2015 From: __peter__ at web.de (Peter Otten) Date: Thu, 19 Nov 2015 19:51:47 +0100 Subject: [Tutor] unittest not working References: Message-ID: Mike wrote: > I'm trying to unit test a self-built regular expression processor for an > assignment. I'm trying to set up unit tests for the package, but it's not > executing them. This is my first time trying to use the unittest module, > so I'm sure I'm missing something, I'm just not sure what. I even put a > test case in there I knew would fail just to try it. > > Unit Test code: > import unittest > from regex import regexp > > class RegexTest(unittest.TestCase): > def fail_test(self): > self.assertEqual(1, 2) > > def basic_test(self): > self.assertEqual(regexp('Hello', 'Goodbye'), '') > self.assertEqual(regexp('hello', 'ello'), 'ello') > with self.assertRaises(SyntaxError): > regexp('hello', 'he)') > > if __name__ == '__main__': > unittest.main() > > Output: >>>> > > ---------------------------------------------------------------------- > Ran 0 tests in 0.000s > > OK > Exit code: False >>>> Rename the mathods fail_test to test_fail, and basic_test to test_basic. Test methods are recognized by their prefix. From oscar.j.benjamin at gmail.com Thu Nov 19 13:52:07 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 19 Nov 2015 18:52:07 +0000 Subject: [Tutor] unittest not working In-Reply-To: References: Message-ID: On 19 November 2015 at 13:25, Mike wrote: > I'm trying to unit test a self-built regular expression processor for an > assignment. I'm trying to set up unit tests for the package, but it's not > executing them. This is my first time trying to use the unittest module, so > I'm sure I'm missing something, I'm just not sure what. I even put a test > case in there I knew would fail just to try it. > > Unit Test code: > import unittest > from regex import regexp > > class RegexTest(unittest.TestCase): > def fail_test(self): > self.assertEqual(1, 2) > > def basic_test(self): > self.assertEqual(regexp('Hello', 'Goodbye'), '') > self.assertEqual(regexp('hello', 'ello'), 'ello') > with self.assertRaises(SyntaxError): > regexp('hello', 'he)') > > if __name__ == '__main__': > unittest.main() > > Output: >>>> > > ---------------------------------------------------------------------- > Ran 0 tests in 0.000s > > OK > Exit code: False The unittest.main() call will attempt to find all of the TestCase subclasses and for each class, create an instance and call all of the methods that are named test_xxx. So if you rename your methods as test_fail and test_basic then I think it should work. The reason for this is that you might want to add other methods to your TestCase subclass that will not be directly called by the test runner but that you can use in your tests. -- Oscar From kfh777 at earthlink.net Thu Nov 19 16:11:25 2015 From: kfh777 at earthlink.net (Ken Hammer) Date: Thu, 19 Nov 2015 16:11:25 -0500 Subject: [Tutor] Modulus Operator ? Message-ID: A text for Python 2.7 includes these two paragraphs. The first I understand. The 2nd below mystifies me. How do I use "x % 10" and "x % 100" in the context of the examples I've created following the paragraphs? "The modulus operator turns out to be surprisingly useful. For example, you can check whether one number is divisible by another if x % y is zero, then x is divisible by y. Also, you can extract the right-most digit or digits from a number. For example, x % 10 yields the right-most digit of x (in base 10). Similarly x % 100 yields the last two digits." I understand these: x = 49/13 print x 3 y = 49%13 print y 10 z = 0.0 + x + y/13.0 print z 3.76923076923 print 49.0/13 3.76923076923 Thanks, Ken From lists.davep at gmail.com Thu Nov 19 13:55:59 2015 From: lists.davep at gmail.com (Dave P) Date: Thu, 19 Nov 2015 13:55:59 -0500 Subject: [Tutor] unittest not working In-Reply-To: References: Message-ID: On Thu, Nov 19, 2015 at 8:25 AM, Mike wrote: > I'm trying to unit test a self-built regular expression processor for an > assignment. I'm trying to set up unit tests for the package, but it's not > executing them. This is my first time trying to use the unittest module, so > I'm sure I'm missing something, I'm just not sure what. I even put a test > case in there I knew would fail just to try it. > > Unit Test code: > import unittest > from regex import regexp > class RegexTest(unittest.TestCase): > def fail_test(self): > self.assertEqual(1, 2) > > def basic_test(self): > self.assertEqual(regexp('Hello', 'Goodbye'), '') > self.assertEqual(regexp('hello', 'ello'), 'ello') > with self.assertRaises(SyntaxError): > regexp('hello', 'he)') > Your functions should start with the word 'test'. For example: def test_fail_test(self): According to the docs, "This naming convention informs the test runner about which methods represent tests." From alan.gauld at btinternet.com Thu Nov 19 19:25:42 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 20 Nov 2015 00:25:42 +0000 Subject: [Tutor] Modulus Operator ? In-Reply-To: References: Message-ID: On 19/11/15 21:11, Ken Hammer wrote: > ...The 2nd below mystifies me. How do I use "x % 10" and "x % 100" ... > Also, you can extract the right-most digit or digits from a number. > For example, x % 10 yields the right-most digit of x (in base 10). > I understand these: > > x = 49/13 > print x > 3 > > y = 49%13 > print y > 10 So now try it with 10 instead of 13 print 49%10 9 > z = 0.0 + x + y/13.0 > print z > 3.76923076923 > > print 49.0/13 > 3.76923076923 Since mod doesn't come into these you can't really apply it. But here is another example, lets create a list of numbers: >>> nums = [123,234,345] Now we want the last two digits from each number, so we use mod 100: >>> for n in nums: ... print n % 100 23 34 45 As always in python the best way to see how something works is just to try it out at the >>> prompt. If something gives a different result to what you expect then come back here and ask. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Fri Nov 20 00:45:08 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 19 Nov 2015 21:45:08 -0800 Subject: [Tutor] Modulus Operator ? In-Reply-To: References: Message-ID: On Thu, Nov 19, 2015 at 1:11 PM, Ken Hammer wrote: > y = 49%13 > print y > 10 Actually, let me pretend for a moment that I don't know what the modulus operator is. Why do we get 10 here? Can you verbalize the reason? Can you modify this example above to use the modulus operator with "10" on the right hand side? What do you expect it computes? What do you see? From jarod_v6 at libero.it Fri Nov 20 05:39:33 2015 From: jarod_v6 at libero.it (jarod_v6 at libero.it) Date: Fri, 20 Nov 2015 11:39:33 +0100 (CET) Subject: [Tutor] Strange error (Peter Otten) Message-ID: <935199762.437611448015973686.JavaMail.defaultUser@defaultHost> Thanks so much!! was a very silly error From jarod_v6 at libero.it Fri Nov 20 05:48:44 2015 From: jarod_v6 at libero.it (jarod_v6 at libero.it) Date: Fri, 20 Nov 2015 11:48:44 +0100 (CET) Subject: [Tutor] Dictionary on data Message-ID: <854688251.442091448016524536.JavaMail.defaultUser@defaultHost> Dear All! I have this elements In [445]: pt = line.split("\t")[9] In [446]: pt Out[446]: 'gene_id "ENSG00000223972"; gene_version "5"; transcript_id "ENST00000456328"; transcript_version "2"; exon_number "1"; gene_name "DDX11L1"; gene_source "havana"; gene_biotype "transcribed_unprocessed_pseudogene"; transcript_name "DDX11L1-002"; transcript_source "havana"; transcript_biotype "processed_transcript"; exon_id "ENSE00002234944"; exon_version "1"; tag "basic"; transcript_support_level "1";\n' and I want to create a dictionary like this gene_id = "ENSG00000223972"; ... I found on stack over flow this way to create a dictionary of dictionary (http://stackoverflow.com/questions/8550912/python-dictionary-of-dictionaries) # This is our sample data data = [("Milter", "Miller", 4), ("Milter", "Miler", 4), ("Milter", "Malter", 2)] # dictionary we want for the result dictionary = {} # loop that makes it work for realName, falseName, position in data: dictionary.setdefault(realName, {})[falseName] = position I want to create a dictionary using setdefault but I have difficult to trasform pt as list of tuple. data = pt.split(";") in () 1 for i in data: 2 l = i.split() ----> 3 print l[0] 4 IndexError: list index out of range In [457]: for i in data: l = i.split() print l .....: ['gene_id', '"ENSG00000223972"'] ['gene_version', '"5"'] ['transcript_id', '"ENST00000456328"'] ['transcript_version', '"2"'] ['exon_number', '"1"'] ['gene_name', '"DDX11L1"'] ['gene_source', '"havana"'] ['gene_biotype', '"transcribed_unprocessed_pseudogene"'] ['transcript_name', '"DDX11L1-002"'] ['transcript_source', '"havana"'] ['transcript_biotype', '"processed_transcript"'] ['exon_id', '"ENSE00002234944"'] ['exon_version', '"1"'] ['tag', '"basic"'] ['transcript_support_level', '"1"'] [] So how can do that more elegant way? thanks so much!! From rhce.san at gmail.com Fri Nov 20 07:29:47 2015 From: rhce.san at gmail.com (Santosh Kumar) Date: Fri, 20 Nov 2015 17:59:47 +0530 Subject: [Tutor] A small query on fabric Message-ID: All, i am working on fabric with python . I have a structure as below. topdir + fabfile.py other_scripts + script1 + script2 + script3 I am trying to import these scripts into fabfile.py and use them for executing across multiple hosts. But i am able to run them only on the localhost not able to get them running on muliple hosts as i set in my env.hosts list parameters. If anyone has any prior experience with fabric, please asssit. Thanks, -- D. Santosh Kumar From __peter__ at web.de Fri Nov 20 08:58:39 2015 From: __peter__ at web.de (Peter Otten) Date: Fri, 20 Nov 2015 14:58:39 +0100 Subject: [Tutor] Dictionary on data References: <854688251.442091448016524536.JavaMail.defaultUser@defaultHost> Message-ID: jarod_v6--- via Tutor wrote: > Dear All! > I have this elements > > In [445]: pt = line.split("\t")[9] > > In [446]: pt > Out[446]: 'gene_id "ENSG00000223972"; gene_version "5"; transcript_id > "ENST00000456328"; transcript_version "2"; exon_number "1"; gene_name > "DDX11L1"; gene_source "havana"; gene_biotype > "transcribed_unprocessed_pseudogene"; transcript_name "DDX11L1-002"; > transcript_source "havana"; transcript_biotype "processed_transcript"; > exon_id "ENSE00002234944"; exon_version "1"; tag "basic"; > transcript_support_level "1";\n' > > > and I want to create a dictionary like this > > gene_id = "ENSG00000223972"; ... > > > I found on stack over flow this way to create a dictionary of dictionary > (http://stackoverflow.com/questions/8550912/python-dictionary-of-dictionaries) > # This is our sample data > data = [("Milter", "Miller", 4), ("Milter", "Miler", 4), ("Milter", > "Malter", 2)] > > # dictionary we want for the result > dictionary = {} > > # loop that makes it work > for realName, falseName, position in data: > dictionary.setdefault(realName, {})[falseName] = position > > I want to create a dictionary using setdefault but I have difficult to > trasform pt as list of tuple. > > data = pt.split(";") > in () > 1 for i in data: > 2 l = i.split() > ----> 3 print l[0] > 4 > > IndexError: list index out of range > > In [457]: for i in data: > l = i.split() > print l > .....: > ['gene_id', '"ENSG00000223972"'] > ['gene_version', '"5"'] > ['transcript_id', '"ENST00000456328"'] > ['transcript_version', '"2"'] > ['exon_number', '"1"'] > ['gene_name', '"DDX11L1"'] > ['gene_source', '"havana"'] > ['gene_biotype', '"transcribed_unprocessed_pseudogene"'] > ['transcript_name', '"DDX11L1-002"'] > ['transcript_source', '"havana"'] > ['transcript_biotype', '"processed_transcript"'] > ['exon_id', '"ENSE00002234944"'] > ['exon_version', '"1"'] > ['tag', '"basic"'] > ['transcript_support_level', '"1"'] > [] > > > So how can do that more elegant way? > thanks so much!! I don't see why you would need dict.setdefault(), you have the necessary pieces together: data = pt.split(";") pairs = (item.split() for item in data) mydict = {item[0]: item[1].strip('"') for item in pairs if len(item) == 2} You can protect against whitespace in the quoted strings with item.split(None, 1) instead of item.split(). If ";" is allowed in the quoted strings you have to work a little harder. From alan.gauld at btinternet.com Fri Nov 20 11:55:58 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 20 Nov 2015 16:55:58 +0000 Subject: [Tutor] A small query on fabric In-Reply-To: References: Message-ID: On 20/11/15 12:29, Santosh Kumar wrote: > All, > > i am working on fabric with python . I have a structure as below. I know nothing of Fabric and this list is really for Python language and standard library questions. But there is a fabric mailing list and a twitter feed, you should probably try them for detailed help http://www.fabfile.org/contact.html -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Fri Nov 20 16:39:17 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Fri, 20 Nov 2015 13:39:17 -0800 Subject: [Tutor] Modulus Operator ? In-Reply-To: References: Message-ID: >>On Thu, Nov 19, 2015 at 1:11 PM, Ken Hammer wrote: > >>> y = 49%13 >>> print y >>> 10 > >>Actually, let me pretend for a moment that I don't know what the modulus >>operator is. Why do we get 10 here? Can you verbalize the reason? > > 49 contains 13 3 times and leaves 10 to be divided. > > >>Can you modify this example above to use the modulus operator with "10" on >>the right hand side? What do you expect it computes? What do you see? > > I see these. Is this what you mean? In view of the third float entry I don't understand why the first two are showing me the digits of the dividend. Why doesn't y = 49%10 deliver 4 as an answer and with 100 as the divisor why don't I get 0? > >>>> y = 49%10 >>>> print y > 9 >>>> y= 49%100 >>>> print y > 49 >>>> 49/13.0 > 3.7692307692307692 Ok, I think I might see where're you getting stuck. You're misinterpreting what you read about the modulus operator. Contrary to what you've read, you don't use it a single time to get *all* the digits of the dividend all at once. Rather, what it does do is let you probe at the *very last* digit in the number. In that sense, when we're doing: 49 % 10 What you're asking is what's left to be when we divide 49 by 10. 49 contains 10 4 times and leaves 9 to be divided. and that's why 49 % 10 == 9: it gives us the last, rightmost digit of the number. So you might be feeling a little let down. What was it trying to say? I think the text was trying to say, when it talks about getting all the digits of a number, is that you can do *repeated* uses of division and modulus can get the individual digits. In a sense, division and modulus are like measuring tools that allow us to shift and probe at an object to figure out what it's made of. To make this concrete, imagine holding something underneath a microscope. For our purposes, let's do this: ############ thing = 123456 ############ But close your eyes for a moment: pretend for a minute that you don't know what this "thing" looks like. What can we do to inspect it, if we don't want to look at it all at once? We can probe and shift it, to inspect portions of it. ############################# def probe(x): """Return the last digit of x.""" return x % 10 def shift(x): """Move x a little to the right, dropping the last digit.""" return x / 10 ############################# Here, we'll have two functions that we'll use on our thing. But why do we call these things "probe" and "shift"? Because we can do things like this. We can get the last digit... ######################## >>> probe(thing) 6 ######################## And now we know that this thing ends with a 6. And we can move the thing around and probe again, to get the second to last digit... ######################## >>> probe(shift(thing)) 5 ######################## So now we know this thing looks something like "...56" And we can repeat. Shift the thing twice, and probe it to get the third to the last digit... ######################## >>> probe(shift(shift(thing))) 4 ######################## ... and so on. Why is this useful? In general: there are times when we're dealing with things that are REALLY large, things that we truly can't look at all at once, and knowing how to deal with just things by progressively poking and moving through it is a skill we can use to navigate through our data. You'll hear the term "iteration", which is essentially what this is. Exploring a number, using division and modulo operators, just happens to be a really simple toy example of this in action. From kfh777 at earthlink.net Fri Nov 20 21:54:58 2015 From: kfh777 at earthlink.net (Ken Hammer) Date: Fri, 20 Nov 2015 21:54:58 -0500 Subject: [Tutor] Modulus Operator ? SOLVED In-Reply-To: Message-ID: Thanks, Danny Got it. Ken In , on 11/20/15 at 01:39 PM, Danny Yoo said: >>>On Thu, Nov 19, 2015 at 1:11 PM, Ken Hammer wrote: >> >>>> y = 49%13 >>>> print y >>>> 10 >> >>>Actually, let me pretend for a moment that I don't know what the modulus >>>operator is. Why do we get 10 here? Can you verbalize the reason? >> >> 49 contains 13 3 times and leaves 10 to be divided. >> >> >>>Can you modify this example above to use the modulus operator with "10" on >>>the right hand side? What do you expect it computes? What do you see? >> >> I see these. Is this what you mean? In view of the third float entry I don't understand why the first two are showing me the digits of the dividend. Why doesn't y = 49%10 deliver 4 as an answer and with 100 as the divisor why don't I get 0? >> >>>>> y = 49%10 >>>>> print y >> 9 >>>>> y= 49%100 >>>>> print y >> 49 >>>>> 49/13.0 >> 3.7692307692307692 >Ok, I think I might see where're you getting stuck. >You're misinterpreting what you read about the modulus operator. Contrary >to what you've read, you don't use it a single time to get *all* the digits >of the dividend all at once. Rather, what it does do is let you probe at >the *very last* digit in the number. In that sense, when we're doing: > 49 % 10 >What you're asking is what's left to be when we divide 49 by 10. > 49 contains 10 4 times and leaves 9 to be divided. >and that's why 49 % 10 == 9: it gives us the last, rightmost digit of the >number. >So you might be feeling a little let down. What was it trying to say? > I think the text was trying to say, when it talks about getting all the >digits of a number, is that you can do *repeated* uses of division and >modulus can get the individual digits. >In a sense, division and modulus are like measuring tools that allow us to >shift and probe at an object to figure out what it's made of. To make this >concrete, imagine holding something underneath a microscope. For our >purposes, let's do this: >############ >thing = 123456 >############ >But close your eyes for a moment: pretend for a minute that you don't know >what this "thing" looks like. What can we do to inspect it, if we don't >want to look at it all at once? >We can probe and shift it, to inspect portions of it. >############################# >def probe(x): > """Return the last digit of x.""" > return x % 10 >def shift(x): > """Move x a little to the right, dropping the last digit.""" > return x / 10 >############################# >Here, we'll have two functions that we'll use on our thing. But why do we >call these things "probe" and "shift"? >Because we can do things like this. We can get the last digit... >######################## >>>> probe(thing) >6 >######################## >And now we know that this thing ends with a 6. And we can move the thing >around and probe again, to get the second to last digit... >######################## >>>> probe(shift(thing)) >5 >######################## >So now we know this thing looks something like "...56" >And we can repeat. Shift the thing twice, and probe it to get the third to >the last digit... >######################## >>>> probe(shift(shift(thing))) >4 >######################## >... and so on. >Why is this useful? In general: there are times when we're dealing with >things that are REALLY large, things that we truly can't look at all at >once, and knowing how to deal with just things by progressively poking and >moving through it is a skill we can use to navigate through our data. >You'll hear the term "iteration", which is essentially what this is. >Exploring a number, using division and modulo operators, just happens to be >a really simple toy example of this in action. -- ----------------------------------------------------------- K.F.Hammer Associates Ken Hammer management consultations Saint Johnsbury, VT 05819 ----------------------------------------------------------- From aadeshere1 at gmail.com Sat Nov 21 07:05:49 2015 From: aadeshere1 at gmail.com (Aadesh Shrestha) Date: Sat, 21 Nov 2015 17:50:49 +0545 Subject: [Tutor] Squaring every digit in number Message-ID: I am currently taking challenges on codewars. Problem: Square Every Digit is there a better solution than this which requires less code? def square_digits(num): arr = [] list = [] while(num !=0): temp = num%10 num = num /10 arr.insert(0, temp) for i in arr: list.append( i*i) str1 = ''.join(str(e) for e in list) return int(str1) ************************ Aadesh Shrestha From lac at openend.se Sat Nov 21 10:06:48 2015 From: lac at openend.se (Laura Creighton) Date: Sat, 21 Nov 2015 16:06:48 +0100 Subject: [Tutor] Squaring every digit in number In-Reply-To: References: Message-ID: <201511211506.tALF6ml2003156@fido.openend.se> In a message of Sat, 21 Nov 2015 17:50:49 +0545, Aadesh Shrestha writes: >I am currently taking challenges on codewars. >Problem: Square Every Digit > >is there a better solution than this which requires less code? > >def square_digits(num): > arr = [] > list = [] > while(num !=0): > temp = num%10 > num = num /10 > arr.insert(0, temp) > for i in arr: > > list.append( i*i) > str1 = ''.join(str(e) for e in list) > return int(str1) > > > >************************ >Aadesh Shrestha Read up on list comprehensions: https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions Have fun! Laura From joel.goldstick at gmail.com Sat Nov 21 11:38:43 2015 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sat, 21 Nov 2015 11:38:43 -0500 Subject: [Tutor] Squaring every digit in number In-Reply-To: <201511211506.tALF6ml2003156@fido.openend.se> References: <201511211506.tALF6ml2003156@fido.openend.se> Message-ID: On Sat, Nov 21, 2015 at 10:06 AM, Laura Creighton wrote: > In a message of Sat, 21 Nov 2015 17:50:49 +0545, Aadesh Shrestha writes: > >I am currently taking challenges on codewars. > >Problem: Square Every Digit > > > >is there a better solution than this which requires less code? > > > >def square_digits(num): > > arr = [] > > list = [] > > while(num !=0): > > temp = num%10 > > num = num /10 > > arr.insert(0, temp) > > for i in arr: > > > > list.append( i*i) > > str1 = ''.join(str(e) for e in list) > > return int(str1) > > > > > > > >************************ > >Aadesh Shrestha > > Read up on list comprehensions: > https://docs.python.org/2/tutorial/datastructures.html#list-comprehensions > > Have fun! > > Laura > This method converts the number to a string, walks thru the string and converts to the squares. >>> num = 123 >>> d_squared = "" >>> for d in str(num): ... d_squared += str(int(d)**2) ... >>> d_squared '149' >>> int(d_squared) >>> 149 > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick http://joelgoldstick.com/stats/birthdays From alan.gauld at btinternet.com Sat Nov 21 14:30:22 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 21 Nov 2015 19:30:22 +0000 Subject: [Tutor] Squaring every digit in number In-Reply-To: References: Message-ID: On 21/11/15 12:05, Aadesh Shrestha wrote: > is there a better solution than this which requires less code? Yes, several. But less lines of code is not necessarily better. it might take more memory, or run slower. So better is a relative term and you always need to think about what you mean by better. That having been said... > def square_digits(num): > arr = [] > list = [] Never use the name of a built in type as a variable name. You will lose the ability to access the original type name. > while(num !=0): > temp = num%10 > num = num /10 You can use the divmod() function to combine these two lines: num,temp = divmod(num,10) > arr.insert(0, temp) > for i in arr: Rather than inserting each digit into the array then looping over the array squaring everything, why not insert the squared number directly into the array and avoid the second loop? > list.append( i*i) > str1 = ''.join(str(e) for e in list) > return int(str1) I assume this does what you want? For example if num is 47 The return value is 1649. If so you could again save a step by converting the square into a string before inserting into the array: arr.insert(0, str(temp**2)) return int(''.join(arr) ) So the final code would be: def square_digits(num): arr = [] while(num !=0): num,temp = divmod(num,10) arr.insert(0, str(temp**2)) return int(''.join(arr)) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From nhilterbrand at gmail.com Sat Nov 21 09:10:48 2015 From: nhilterbrand at gmail.com (Nathan Hilterbrand) Date: Sat, 21 Nov 2015 09:10:48 -0500 Subject: [Tutor] Squaring every digit in number In-Reply-To: References: Message-ID: <56507B68.7080301@gmail.com> On 11/21/2015 7:05 AM, Aadesh Shrestha wrote: > I am currently taking challenges on codewars. > Problem: Square Every Digit > > is there a better solution than this which requires less code? > > def square_digits(num): > arr = [] > list = [] > while(num !=0): > temp = num%10 > num = num /10 > arr.insert(0, temp) > for i in arr: > > list.append( i*i) > str1 = ''.join(str(e) for e in list) > return int(str1) > > > > ************************ > Aadesh Shrestha > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor "Better" is somewhat subjective, but "less code" is not so much so: def square_digits(num): try: return int(''.join((str(int(n)**2) for n in str(int(num))))) except ValueError: return 0 The "str(int(num))" could be shortened to "str(num)", also, but I liked being able to accept any argument that could be converted to an int, and also raising the ValueError before looping . Have fun at codewars Nathan Hilterbrand From __peter__ at web.de Sat Nov 21 15:49:46 2015 From: __peter__ at web.de (Peter Otten) Date: Sat, 21 Nov 2015 21:49:46 +0100 Subject: [Tutor] Squaring every digit in number References: Message-ID: Aadesh Shrestha wrote: > I am currently taking challenges on codewars. > Problem: Square Every Digit > > is there a better solution than this which requires less code? > > def square_digits(num): > arr = [] > list = [] > while(num !=0): > temp = num%10 > num = num /10 > arr.insert(0, temp) > for i in arr: > > list.append( i*i) > str1 = ''.join(str(e) for e in list) > return int(str1) Here's one which moves most of the code out of the function: # Python 2 TRANS = {ord("0") + digit: unicode(digit*digit) for digit in range(10)} def square_digits(num): return unicode(num).translate(TRANS) Negative numbers keep their leading "-" sign. Replace unicode with str for the Python 3 version. From cmgcomsol at gmail.com Sun Nov 22 00:53:58 2015 From: cmgcomsol at gmail.com (CMG Thrissur) Date: Sun, 22 Nov 2015 11:23:58 +0530 Subject: [Tutor] How to kill a thread Message-ID: <56515876.2090203@gmail.com> Hello, I have seen some examples of terminating a thread the safe way. But is there a way to kill a thread without worrying about what happens next. Like thread.stop() or something George From steve at pearwood.info Sun Nov 22 04:36:17 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 22 Nov 2015 20:36:17 +1100 Subject: [Tutor] How to kill a thread In-Reply-To: <56515876.2090203@gmail.com> References: <56515876.2090203@gmail.com> Message-ID: <20151122093617.GF3821@ando.pearwood.info> On Sun, Nov 22, 2015 at 11:23:58AM +0530, CMG Thrissur wrote: > Hello, > > I have seen some examples of terminating a thread the safe way. But is > there a way to kill a thread without worrying about what happens next. > Like thread.stop() or something No. Threads in Python cannot be killed. They can only be asked to stop themselves. If you exit the entire process, that will also shut down the threads, but that's it. -- Steve From lac at openend.se Sun Nov 22 06:54:39 2015 From: lac at openend.se (Laura Creighton) Date: Sun, 22 Nov 2015 12:54:39 +0100 Subject: [Tutor] How to kill a thread In-Reply-To: <56515876.2090203@gmail.com> References: <56515876.2090203@gmail.com> Message-ID: <201511221154.tAMBsdKm023768@fido.openend.se> In a message of Sun, 22 Nov 2015 11:23:58 +0530, CMG Thrissur writes: >Hello, > >I have seen some examples of terminating a thread the safe way. But is >there a way to kill a thread without worrying about what happens next. >Like thread.stop() or something > >George import sys sys.exit(1) This will brutally kill the whole process. Sometimes that is what you want. Most of the time, it isn't. Python doesn't have a way to grab a thread and kill it, it can only ask it to please stop themselves. Laura From cmgcomsol at gmail.com Sun Nov 22 11:10:13 2015 From: cmgcomsol at gmail.com (CMG Thrissur) Date: Sun, 22 Nov 2015 21:40:13 +0530 Subject: [Tutor] How to kill a thread In-Reply-To: <201511221154.tAMBsdKm023768@fido.openend.se> References: <56515876.2090203@gmail.com> <201511221154.tAMBsdKm023768@fido.openend.se> Message-ID: <5651E8E5.3030001@gmail.com> On Sunday 22 November 2015 05:24 PM, Laura Creighton wrote: > In a message of Sun, 22 Nov 2015 11:23:58 +0530, CMG Thrissur writes: >> Hello, I have seen some examples of terminating a thread the safe >> way. But is there a way to kill a thread without worrying about what >> happens next. Like thread.stop() or something George > import sys sys.exit(1) This will brutally kill the whole process. > Sometimes that is what you want. Most of the time, it isn't. Python > doesn't have a way to grab a thread and kill it, it can only ask it to > please stop themselves. Laura Thank you Steven and Laura for your reply. I was testing some urls with pythonwhois, and i got stuck with some threads taking longer than expected time, i found a roundabout way for it. Below is my code, do you suggest any improvements: import datetime import time import mysql.connector import threading import pythonwhois cnx = mysql.connector.connect(user='root',password=pas,host='localhost',database=db) cursor = cnx.cursor() def ProcessList(domain:str): ''' :paramdomain:din, dcom etc :return: ''' query='select url from urls where (%s="" or %s0: print ('loop',runcounter,'total count',len(threading.enumerate()),'total of value', len(tdic.keys()),datetime.datetime.now().time()) if len(tdic.keys())<10: #changed to consider on keys while len(tdic.keys())<10: # same as above wlist=urllist[counter:counter+10] print ('for new thread pass list',wlist) tdic[counter]=DomainUrlValidityChecker('us',wlist) #specifying actual domain tdic[counter].start() counter+=10 else: tmpitems=list(tdic.keys()) for item in tmpitems: #print(tdic[item].name) if tdic[item].isAlive()==False: print ('deleting dead',item,tdic[item].name) del tdic[item] continue if (datetime.datetime.now()-tdic[item].time).total_seconds()>30: print ('deleting long overdue ',item,tdic[item].name) del tdic[item] for item in tdic.keys(): print (tdic[item].name) print ("sleeping") slth=Sleeper(10) slth.start() slth.join() print ('awake') runcounter-=1 Thanks George From lac at openend.se Sun Nov 22 13:42:59 2015 From: lac at openend.se (Laura Creighton) Date: Sun, 22 Nov 2015 19:42:59 +0100 Subject: [Tutor] How to kill a thread In-Reply-To: <5651E8E5.3030001@gmail.com> References: <56515876.2090203@gmail.com> <201511221154.tAMBsdKm023768@fido.openend.se> <5651E8E5.3030001@gmail.com> Message-ID: <201511221842.tAMIgxpR018716@fido.openend.se> In a message of Sun, 22 Nov 2015 21:40:13 +0530, CMG Thrissur writes: > > > >On Sunday 22 November 2015 05:24 PM, Laura Creighton wrote: >> In a message of Sun, 22 Nov 2015 11:23:58 +0530, CMG Thrissur writes: >>> Hello, I have seen some examples of terminating a thread the safe >>> way. But is there a way to kill a thread without worrying about what >>> happens next. Like thread.stop() or something George >> import sys sys.exit(1) This will brutally kill the whole process. >> Sometimes that is what you want. Most of the time, it isn't. Python >> doesn't have a way to grab a thread and kill it, it can only ask it to >> please stop themselves. Laura > >Thank you Steven and Laura for your reply. I was testing some urls with >pythonwhois, and i got stuck with some threads taking longer than >expected time, i found a roundabout way for it. Below is my code, do you >suggest any improvements: > >import datetime import time import mysql.connector import threading >import pythonwhois cnx = >mysql.connector.connect(user='root',password=pas,host='localhost',database=db) >cursor = cnx.cursor() def ProcessList(domain:str): ''' :paramdomain:din, >dcom etc :return: ''' query='select url from urls where (%s="" or >%sprint (query) cursor.execute(query) tmplist=[] for url in cursor: >tmplist.append(url[0]) return tmplist #print (ProcessList('din')) class >DomainUrlValidityChecker(threading.Thread): def >__init__(self,domain:str,urllist:list): threading.Thread.__init__(self) >print ("Starting",self.name) self.domain=domain self.urllist=urllist >self.time=datetime.datetime.now() def run(self): for url in >self.urllist: workurl = url + '.' + self.domain result = >pythonwhois.get_whois(workurl) if result.get('expiration_date', None) != >None: #print(workurl, result['expiration_date'][0].date().isoformat()) >#print(result['expiration_date'][0].year) query="update urls set >d%s='%s',d%s=curdate() where >url='%s';"%(self.domain,result['expiration_date'][0].date().isoformat(),self.domain+'lastcheck',url) >print(query) cnx = >mysql.connector.connect(user='root',password=pas,host='localhost',database=db) >cursor = cnx.cursor() cursor.execute(query) cnx.commit() else: >#print('Available', workurl) query="update urls set d%s='',d%s=curdate() >where url='%s';"%(self.domain,self.domain+'lastcheck',url) print(query) >cnx = >mysql.connector.connect(user='root',password=pas,host='localhost',database=db) >cursor = cnx.cursor() cursor.execute(query) cnx.commit() print >("Ending",self.name) class Sleeper(threading.Thread): def >__init__(self,sleepcounter): threading.Thread.__init__(self) >self.sleepinterval=sleepcounter def run(self): >time.sleep(self.sleepinterval) if __name__=="__main__": >urllist=ProcessList('dus') #specifying domain table column print >(urllist) counter=0 tdic={} runcounter=10000 while runcounter>0: print >('loop',runcounter,'total count',len(threading.enumerate()),'total of >value', len(tdic.keys()),datetime.datetime.now().time()) if >len(tdic.keys())<10: #changed to consider on keys while >len(tdic.keys())<10: # same as above wlist=urllist[counter:counter+10] >print ('for new thread pass list',wlist) >tdic[counter]=DomainUrlValidityChecker('us',wlist) #specifying actual >domain tdic[counter].start() counter+=10 else: >tmpitems=list(tdic.keys()) for item in tmpitems: #print(tdic[item].name) >if tdic[item].isAlive()==False: print ('deleting >dead',item,tdic[item].name) del tdic[item] continue if >(datetime.datetime.now()-tdic[item].time).total_seconds()>30: print >('deleting long overdue ',item,tdic[item].name) del tdic[item] for item >in tdic.keys(): print (tdic[item].name) print ("sleeping") >slth=Sleeper(10) slth.start() slth.join() print ('awake') runcounter-=1 > > >Thanks > >George Your editor or mail program is trying to do the favour of flowing yours and other people's text. Usually you can get your mailer to stop doing this by specifying your mail as 'plain text' but not always -- I hear that Google mail will insist on flowing if you are on a mobile device no matter what. Since spacing is significant in python, there is no way we can tell what it is your code is doing until you get gmail to behave. Laura From marcus.luetolf at bluewin.ch Sun Nov 22 15:58:35 2015 From: marcus.luetolf at bluewin.ch (=?utf-8?Q?marcus_l=C3=BCtolf?=) Date: Sun, 22 Nov 2015 21:58:35 +0100 Subject: [Tutor] how to invert tuples Message-ID: <08dc01d12568$911453b0$b33cfb10$@bluewin.ch> dear pythonistas i have written the code below for identifying tuples occurring twice or more times in the list of tuples called "flights". 1. Question: How can i modify this code so it does not matter which string is first and which is second or (h,i) == (i,h) respectively ? 2. Question: On my python file I have written the rather long list of tuples in 3 lines. If I break them down in multiple shorter lines the for loop will not work through the last tuple. Thanks in advance for help, Marcus. >>> lst = dict() >>> a = 'M1' >>> b = 'M2' >>> c = 'M3' >>> d = 'M4' >>> e = 'M5' >>> f = 'M6' >>> g = 'M7' >>> h = 'M8' >>> i = 'M9' >>> j = 'M10' >>> k = 'M11' >>> l = 'M12' >>> flights = (h,i), (h,j),(k,f), (k,a), (f,a), (e,f), (e,g), (d,g), (l,c),(l,b), (c,b),(j,c), (j,k), (c,k), (h,f), (h,d), (f,d), (a,l), (a,g),(e,i), (e,b,), (i,b), (i,l), (i,k), (l,k), (f,j),(f,b), (j,b),(h,a),(h,g),(a,g), (d,e), (d,c), (e,c), (i,c), (i,d), (c,d), (f,e,),(f,i),(e,i), (a,j,), (a,b), (j,b), (h,l), (h,k), (l,k), (l,f), (l,g),(f,g),(b,k), (b,d), (k,d), (i,a), (i,j), (a,j), (h,c), (h,e), (c,e), (h,j) >>> for pair in flights: if pair not in lst: lst[pair] = 1 else: lst [pair] += 1 >>> for key,val in lst.items(): if val > 1: >>> print key, val --- Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. https://www.avast.com/antivirus From alan.gauld at btinternet.com Sun Nov 22 18:55:05 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 22 Nov 2015 23:55:05 +0000 Subject: [Tutor] how to invert tuples In-Reply-To: <08dc01d12568$911453b0$b33cfb10$@bluewin.ch> References: <08dc01d12568$911453b0$b33cfb10$@bluewin.ch> Message-ID: On 22/11/15 20:58, marcus l?tolf wrote: > dear pythonistas > > i have written the code below for identifying tuples occurring > twice or more times in the list of tuples called "flights". First thing to point out is that flights is not a list of tuples, its a tuple of tuples. Lists must be enclosed in []. If you do that it may simplify things for you. Also you create a dictionary called lst which implies its a list. You should try to make your variable names relate to their logical purpose rather than their data structure. So maybe counts or similar would have been a good name? These may seem like details but names are an important aspect of communicating a programs intent and accuracy of data type determines what can and can't be done with the data so it is important to get them right. > 1. Question: How can i modify this code so it does not matter which > string is first and which is second or (h,i) == (i,h) respectively ? There are several ways to do this. But in your code you are never comparing the tuples so I'm not sure where you plan on using it? Can you explain exactly how you plan on using that function? > 2. Question: On my python file I have written the rather long list > of tuples in 3 lines. If I break them down in multiple shorter lines > the for loop will not work through the last tuple. We'd need to see that code to know why, but more usefully you can just put the list inside parens/brackets to ensure they all get handled, like so: flights = [(),(),(),().....,()] OR flights = { (), (), ... ()] Or anything in between > Thanks in advance for help, Marcus. > >>>> lst = dict() >>>> a = 'M1' >>>> b = 'M2' >>>> c = 'M3' >>>> d = 'M4' >>>> e = 'M5' >>>> f = 'M6' >>>> g = 'M7' >>>> h = 'M8' >>>> i = 'M9' >>>> j = 'M10' >>>> k = 'M11' >>>> l = 'M12' >>>> flights = (h,i), (h,j),(k,f), (k,a), (f,a), (e,f), (e,g), (d,g), (l,c),(l,b), (c,b),(j,c), (j,k), (c,k), (h,f), (h,d), (f,d), (a,l), (a,g),(e,i), (e,b,), (i,b), (i,l), (i,k), (l,k), (f,j),(f,b), > (j,b),(h,a),(h,g),(a,g), > (d,e), (d,c), (e,c), (i,c), (i,d), (c,d), (f,e,),(f,i),(e,i), (a,j,), (a,b), (j,b), (h,l), (h,k), (l,k), (l,f), > (l,g),(f,g),(b,k), (b,d), (k,d), (i,a), (i,j), (a,j), (h,c), (h,e), (c,e), (h,j) >>>> for pair in flights: > if pair not in lst: > lst[pair] = 1 > else: > lst [pair] += 1 You can replace the if/else lines by using the get() method of a dictionary. For example, using get(): lst[pair] = lst.get(pair,0) + 1 >>>> for key,val in lst.items(): > if val > 1: >>>> print key, val The indentation on that last line looks off but I suspect that may just be email issues HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Sun Nov 22 19:39:07 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 23 Nov 2015 11:39:07 +1100 Subject: [Tutor] how to invert tuples In-Reply-To: <08dc01d12568$911453b0$b33cfb10$@bluewin.ch> References: <08dc01d12568$911453b0$b33cfb10$@bluewin.ch> Message-ID: <20151123003907.GH3821@ando.pearwood.info> On Sun, Nov 22, 2015 at 09:58:35PM +0100, marcus l?tolf wrote: > dear pythonistas > > i have written the code below for identifying tuples occurring twice > or more times in the list of tuples called "flights". > 1. Question: How can i modify this code so it does not matter which > string is first and which is second or (h,i) == (i,h) respectively ? Modifying your code is perhaps not the best way. It might be better to start with something different. a, b, c, d, e, f, g, h, i, j, k, l = ['M' + str(i) for i in range(1, 13)] flights = [ (h,i), (h,j),(k,f), (k,a), (f,a), (e,f), (e,g), (d,g), (l,c),(l,b), (c,b),(j,c), (j,k), (c,k), (h,f), (h,d), (f,d), (a,l), (a,g),(e,i), (e,b,), (i,b), (i,l), (i,k), (l,k), (f,j),(f,b), (j,b),(h,a),(h,g), (a,g), (d,e), (d,c), (e,c), (i,c), (i,d), (c,d), (f,e,),(f,i),(e,i), (a,j,), (a,b), (j,b), (h,l), (h,k), (l,k), (l,f), (l,g),(f,g),(b,k), (b,d), (k,d), (i,a), (i,j), (a,j), (h,c), (h,e), (c,e), (h,j) ] # Convert from tuples to sets, the hard way: tmp = [] for x in flights: tmp.append(frozenset(x)) flights = tmp # Count how many times each frozen set appears. from collections import Counter c = Counter(flights) # Print duplicate keys. for key, count in c.items(): if count > 1: k = tuple(sorted(key)) # Make it prettier to look at. print "%s appears %d times" % (k, count) There are a couple of tricks here: - Use the Counter object to do the counting. - Use sets instead of tuples. The problem is that tuples (a, b) care about their order. By definition, (a, b) is not the same as (b, a). But sets don't care about their order. We can write sets like {a, b}, which by definition is equal to {b, a}. Unfortunately, for technical reasons sets can't be used as the key in dicts or Counters. Fortunately, Python has a frozenset type which can. So we convert the tuples into frozensets. Above, we do it the hard way. Of course, there's an easy way too: # Convert from tuples to sets the easy way: flights = [frozenset(x) for x in flights] *Two* easy ways: flights = map(frozenset, flights) You are not expected to understand how they work, but you are welcome to ask if you would like it explained. Unfortunately, frozensets don't have a particularly pleasant looking display when printed, for example: frozenset(['M7', 'M1']) Not ugly by any means, but rather bulky. So when printing, we convert back to a tuple with: tuple(sorted(key)) The call to sorted() ensures that the two items ('M7' and 'M1', say) always appear in consistent order. But perhaps it would be better to avoid generating the duplicate flights in the first place. I'm not sure where your list of flights came from in the first place, but we can systematically generate all the unique pairs like this: locations = ['M' + str(i) for i in range(1, 13)] unique_pairs = [] for i, a in enumerate(locations): for b in locations[i+1:]: unique_pairs.append((a, b)) > 2. Question: On my python file I have written the rather long list of > tuples in 3 lines. If I break them down in multiple shorter lines the > for loop will not work through the last tuple. I'm afraid I don't understand what you mean by this. -- Steve From badouglas at gmail.com Sun Nov 22 23:19:17 2015 From: badouglas at gmail.com (bruce) Date: Sun, 22 Nov 2015 23:19:17 -0500 Subject: [Tutor] ascii to/from AL32UTF8 conversion Message-ID: Hi. Doing a 'simple' test with linux command line curl, as well as pycurl to fetch a page from a server. The page has a charset of >>AL32UTF8. Anyway to conert this to straight ascii. Python is throwing a notice/error on the charset in another part of the test.. The target site is US based, so there's no weird chars in it.. I suspect that the page/system is based on legacy oracle The metadata of the page is I tried the usual foo = foo.decode('utf-8') foo = foo.decode('ansii') etc.. but no luck. Thanks for any pointers/help From lac at openend.se Mon Nov 23 02:57:35 2015 From: lac at openend.se (Laura Creighton) Date: Mon, 23 Nov 2015 08:57:35 +0100 Subject: [Tutor] how to invert tuples In-Reply-To: <08dc01d12568$911453b0$b33cfb10$@bluewin.ch> References: <08dc01d12568$911453b0$b33cfb10$@bluewin.ch> Message-ID: <201511230757.tAN7vZ1C008716@fido.openend.se> In a message of Sun, 22 Nov 2015 21:58:35 +0100, marcus l?tolf writes: >dear pythonistas > >i have written the code below for identifying tuples occurring twice or more times in the list of tuples called "flights". >1. Question: How can i modify this code so it does not matter which string is first and which is second or (h,i) == (i,h) respectively ? This may be an indication that you have got the wrong datatype. It may be that you would be happier with a set. >>> s1={'Stockholm', 'Berlin'} >>> s2={'Berlin', 'Stockholm'} >>> s1 == s2 True If you need a tuple later -- for instance you want to use the tuple as a key in a dictionary later, because you cannot use the set directly >>> tuple(s1) ('Stockholm', 'Berlin') >>> tuple(s2) ('Stockholm', 'Berlin') >>> Laura From cmgcomsol at gmail.com Sun Nov 22 20:42:29 2015 From: cmgcomsol at gmail.com (CMG Thrissur) Date: Mon, 23 Nov 2015 07:12:29 +0530 Subject: [Tutor] How to kill a thread In-Reply-To: <201511221842.tAMIgxpR018716@fido.openend.se> References: <56515876.2090203@gmail.com> <201511221154.tAMBsdKm023768@fido.openend.se> <5651E8E5.3030001@gmail.com> <201511221842.tAMIgxpR018716@fido.openend.se> Message-ID: <56526F05.9090409@gmail.com> On Monday 23 November 2015 12:12 AM, Laura Creighton wrote: > In a message of Sun, 22 Nov 2015 21:40:13 +0530, CMG Thrissur writes: >> >> >> On Sunday 22 November 2015 05:24 PM, Laura Creighton wrote: >>> In a message of Sun, 22 Nov 2015 11:23:58 +0530, CMG Thrissur writes: >>>> Hello, I have seen some examples of terminating a thread the safe >>>> way. But is there a way to kill a thread without worrying about what >>>> happens next. Like thread.stop() or something George >>> import sys sys.exit(1) This will brutally kill the whole process. >>> Sometimes that is what you want. Most of the time, it isn't. Python >>> doesn't have a way to grab a thread and kill it, it can only ask it to >>> please stop themselves. Laura >> Thank you Steven and Laura for your reply. I was testing some urls with >> pythonwhois, and i got stuck with some threads taking longer than >> expected time, i found a roundabout way for it. Below is my code, do you >> suggest any improvements: >> >> import datetime import time import mysql.connector import threading >> import pythonwhois cnx = >> mysql.connector.connect(user='root',password=pas,host='localhost',database=db) >> cursor = cnx.cursor() def ProcessList(domain:str): ''' :paramdomain:din, >> dcom etc :return: ''' query='select url from urls where (%s="" or >> %s> print (query) cursor.execute(query) tmplist=[] for url in cursor: >> tmplist.append(url[0]) return tmplist #print (ProcessList('din')) class >> DomainUrlValidityChecker(threading.Thread): def >> __init__(self,domain:str,urllist:list): threading.Thread.__init__(self) >> print ("Starting",self.name) self.domain=domain self.urllist=urllist >> self.time=datetime.datetime.now() def run(self): for url in >> self.urllist: workurl = url + '.' + self.domain result = >> pythonwhois.get_whois(workurl) if result.get('expiration_date', None) != >> None: #print(workurl, result['expiration_date'][0].date().isoformat()) >> #print(result['expiration_date'][0].year) query="update urls set >> d%s='%s',d%s=curdate() where >> url='%s';"%(self.domain,result['expiration_date'][0].date().isoformat(),self.domain+'lastcheck',url) >> print(query) cnx = >> mysql.connector.connect(user='root',password=pas,host='localhost',database=db) >> cursor = cnx.cursor() cursor.execute(query) cnx.commit() else: >> #print('Available', workurl) query="update urls set d%s='',d%s=curdate() >> where url='%s';"%(self.domain,self.domain+'lastcheck',url) print(query) >> cnx = >> mysql.connector.connect(user='root',password=pas,host='localhost',database=db) >> cursor = cnx.cursor() cursor.execute(query) cnx.commit() print >> ("Ending",self.name) class Sleeper(threading.Thread): def >> __init__(self,sleepcounter): threading.Thread.__init__(self) >> self.sleepinterval=sleepcounter def run(self): >> time.sleep(self.sleepinterval) if __name__=="__main__": >> urllist=ProcessList('dus') #specifying domain table column print >> (urllist) counter=0 tdic={} runcounter=10000 while runcounter>0: print >> ('loop',runcounter,'total count',len(threading.enumerate()),'total of >> value', len(tdic.keys()),datetime.datetime.now().time()) if >> len(tdic.keys())<10: #changed to consider on keys while >> len(tdic.keys())<10: # same as above wlist=urllist[counter:counter+10] >> print ('for new thread pass list',wlist) >> tdic[counter]=DomainUrlValidityChecker('us',wlist) #specifying actual >> domain tdic[counter].start() counter+=10 else: >> tmpitems=list(tdic.keys()) for item in tmpitems: #print(tdic[item].name) >> if tdic[item].isAlive()==False: print ('deleting >> dead',item,tdic[item].name) del tdic[item] continue if >> (datetime.datetime.now()-tdic[item].time).total_seconds()>30: print >> ('deleting long overdue ',item,tdic[item].name) del tdic[item] for item >> in tdic.keys(): print (tdic[item].name) print ("sleeping") >> slth=Sleeper(10) slth.start() slth.join() print ('awake') runcounter-=1 >> >> >> Thanks >> >> George > Your editor or mail program is trying to do the favour of flowing > yours and other people's text. Usually you can get your mailer to > stop doing this by specifying your mail as 'plain text' but not > always -- I hear that Google mail will insist on flowing if you > are on a mobile device no matter what. > > Since spacing is significant in python, there is no way we > can tell what it is your code is doing until you get gmail to > behave. > > Laura Is the below text in plain text? I am using thunderbird. import datetime import time import mysql.connector import threading import pythonwhois cnx = mysql.connector.connect(user='root',password=pas,host='localhost',database=db) cursor = cnx.cursor() def ProcessList(domain:str): ''' :paramdomain:din, dcom etc :return: ''' query='select url from urls where (%s="" or %s0: print ('loop',runcounter,'total count',len(threading.enumerate()),'total of value', len(tdic.keys()),datetime.datetime.now().time()) if len(tdic.keys())<10: #changed to consider on keys while len(tdic.keys())<10: # same as above wlist=urllist[counter:counter+10] print ('for new thread pass list',wlist) tdic[counter]=DomainUrlValidityChecker('us',wlist) #specifying actual domain tdic[counter].start() counter+=10 else: tmpitems=list(tdic.keys()) for item in tmpitems: #print(tdic[item].name) if tdic[item].isAlive()==False: print ('deleting dead',item,tdic[item].name) del tdic[item] continue if (datetime.datetime.now()-tdic[item].time).total_seconds()>30: print ('deleting long overdue ',item,tdic[item].name) del tdic[item] for item in tdic.keys(): print (tdic[item].name) print ("sleeping") slth=Sleeper(10) slth.start() slth.join() print ('awake') runcounter-=1 Thanks George Thanks George From marcus.luetolf at bluewin.ch Mon Nov 23 03:47:02 2015 From: marcus.luetolf at bluewin.ch (=?utf-8?Q?marcus_l=C3=BCtolf?=) Date: Mon, 23 Nov 2015 09:47:02 +0100 Subject: [Tutor] how to invert tuples, 2nd question solved Message-ID: <005601d125cb$89121ef0$9b365cd0$@bluewin.ch> dear pythonistas, i solved my 2. question in my mail of Sunday 22.11.2015 21:59, sorry for bothering you, Marcus. --- Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. https://www.avast.com/antivirus From alan.gauld at btinternet.com Mon Nov 23 04:58:50 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 23 Nov 2015 09:58:50 +0000 Subject: [Tutor] How to kill a thread In-Reply-To: <56526F05.9090409@gmail.com> References: <56515876.2090203@gmail.com> <201511221154.tAMBsdKm023768@fido.openend.se> <5651E8E5.3030001@gmail.com> <201511221842.tAMIgxpR018716@fido.openend.se> <56526F05.9090409@gmail.com> Message-ID: On 23/11/15 01:42, CMG Thrissur wrote: >> Since spacing is significant in python, there is no way we >> can tell what it is your code is doing until you get gmail to >> behave. >> >> Laura > Is the below text in plain text? I am using thunderbird. > Nope > > import datetime import time import mysql.connector import threading > import pythonwhois cnx = > mysql.connector.connect(user='root',password=pas,host='localhost',database=db) > cursor = cnx.cursor() def ProcessList(domain:str): ''' :paramdomain:din, > dcom etc :return: ''' query='select url from urls where (%s="" or > %sPreferences dialog and click on the Composition tab Click the 'Send options' button at bottom right Click the 'Plain Text Domains' tab Add python.org to your list of plain text domains 'OK/Close' back out and hopefully any new messages to python.org will be in plain text! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Mon Nov 23 05:17:01 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 23 Nov 2015 21:17:01 +1100 Subject: [Tutor] How to kill a thread In-Reply-To: References: <56515876.2090203@gmail.com> <201511221154.tAMBsdKm023768@fido.openend.se> <5651E8E5.3030001@gmail.com> <201511221842.tAMIgxpR018716@fido.openend.se> <56526F05.9090409@gmail.com> Message-ID: <20151123101701.GK3821@ando.pearwood.info> On Mon, Nov 23, 2015 at 09:58:50AM +0000, Alan Gauld wrote: > On 23/11/15 01:42, CMG Thrissur wrote: > > >>Since spacing is significant in python, there is no way we > >>can tell what it is your code is doing until you get gmail to > >>behave. > >> > >>Laura > >Is the below text in plain text? I am using thunderbird. > > > > Nope I think you are mistaken about that. I can only see a single attachment to CMG's email, and it is plain/text. -- Steve From steve at pearwood.info Mon Nov 23 05:12:07 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 23 Nov 2015 21:12:07 +1100 Subject: [Tutor] How to kill a thread In-Reply-To: <56526F05.9090409@gmail.com> References: <56515876.2090203@gmail.com> <201511221154.tAMBsdKm023768@fido.openend.se> <5651E8E5.3030001@gmail.com> <201511221842.tAMIgxpR018716@fido.openend.se> <56526F05.9090409@gmail.com> Message-ID: <20151123101207.GJ3821@ando.pearwood.info> On Mon, Nov 23, 2015 at 07:12:29AM +0530, CMG Thrissur wrote: > Is the below text in plain text? I am using thunderbird. Yes, it is plain text, but the word-wrapping of the text is completely broken, as you should be able to see below: > import datetime import time import mysql.connector import threading > import pythonwhois cnx = > mysql.connector.connect(user='root',password=pas,host='localhost',database=db) > cursor = cnx.cursor() def ProcessList(domain:str): ''' :paramdomain:din, > dcom etc :return: ''' query='select url from urls where (%s="" or > %s (query) cursor.execute(query) tmplist=[] for url in cursor: [...] Apart from the leading ">" used for quoting, what you see above is what I see in the original email. Unfortunately I don't know how to solve this. I don't use Gmail, and don't now whether it has something to do with Gmail or Thunderbird. Wait... I have a thought... Your email has a header: Content-Type: text/plain; charset="us-ascii"; Format="flowed" The format="flowed" is probably the bit that is screwing up the formatting. Flowed formatting is exactly what you want for ordinary text, paragraphs and sentences, but it is exactly what you DON'T want for poetry and programming code, where line breaks are important. If you look through the preferences and settings for Thunderbird, you will probably find something to control how lines are formatted. P.S. Please don't send vast mountains of text to the list while you're trying settings out. Just two or three lines is enough to see whether they get word-wrapped or not. Thanks, -- Steve From oscar.j.benjamin at gmail.com Mon Nov 23 05:07:36 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 23 Nov 2015 10:07:36 +0000 Subject: [Tutor] How to kill a thread In-Reply-To: <56526F05.9090409@gmail.com> References: <56515876.2090203@gmail.com> <201511221154.tAMBsdKm023768@fido.openend.se> <5651E8E5.3030001@gmail.com> <201511221842.tAMIgxpR018716@fido.openend.se> <56526F05.9090409@gmail.com> Message-ID: On 23 November 2015 at 01:42, CMG Thrissur wrote: > Is the below text in plain text? I am using thunderbird. No it isn't. I've kept the first part of it so you can see how it looks: > import datetime import time import mysql.connector import threading import > pythonwhois cnx = > mysql.connector.connect(user='root',password=pas,host='localhost',database=db) > cursor = cnx.cursor() def ProcessList(domain:str): ''' :paramdomain:din, > dcom etc :return: ''' query='select url from urls where (%s="" or > %s (query) cursor.execute(query) tmplist=[] for url in cursor: > tmplist.append(url[0]) return tmplist #print -- Oscar From steve at pearwood.info Mon Nov 23 05:37:00 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 23 Nov 2015 21:37:00 +1100 Subject: [Tutor] ascii to/from AL32UTF8 conversion In-Reply-To: References: Message-ID: <20151123103659.GL3821@ando.pearwood.info> On Sun, Nov 22, 2015 at 11:19:17PM -0500, bruce wrote: > Hi. > > Doing a 'simple' test with linux command line curl, as well as pycurl > to fetch a page from a server. > > The page has a charset of >>AL32UTF8. I had never heard of that before, so I googled for it. No surprise, it comes from Oracle, and they have made a complete dog's breakfast out of it. According to the answers here: https://community.oracle.com/thread/3514820 (1) Oracle thinks that UTF-8 is a two-byte encoding (it isn't); (2) AL32UTF8 has "extra characters" that UTF-8 doesn't, but UTF-8 is a superset of AL32UTF8 (that's a contradiction!); (3) Oracle's UTF-8 is actually the abomination more properly known as CESU-8: http://www.unicode.org/reports/tr26/ (4) Oracle's AL32UTF8 might actually be the real UTF-8, not "Oracle UTF-8", which is rubbish. > Anyway to conert this to straight ascii. Python is throwing a > notice/error on the charset in another part of the test.. > > The target site is US based, so there's no weird chars in it.. I wouldn't be so sure about that. > I suspect that the page/system is based on legacy oracle > > The metadata of the page is > > > > I tried the usual > > foo = foo.decode('utf-8') And what happened? Did you get an error? Please copy and paste the complete traceback. The easy way to hit this problem with a hammer and "fix it" is to do this: foo = foo.decode('utf-8', errors='replace') but that will replace any non-ASCII chars or malformed UFT-8 bytes with question marks: py> s = u"abc ? def".encode('utf-8') # non-ASCII string py> print s.decode('ascii', errors='replace') abc ?? def which loses data. That should normally be considered a last resort. It might also help to open the downloaded file in a hex editor and see if it looks like binary or text. If you see lots of zeroes, e.g.: ...006100340042005600... then the encoding is probably not UTF-8. -- Steve From steve at pearwood.info Mon Nov 23 05:40:33 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 23 Nov 2015 21:40:33 +1100 Subject: [Tutor] How to kill a thread In-Reply-To: References: <56515876.2090203@gmail.com> <201511221154.tAMBsdKm023768@fido.openend.se> <5651E8E5.3030001@gmail.com> <201511221842.tAMIgxpR018716@fido.openend.se> <56526F05.9090409@gmail.com> Message-ID: <20151123104033.GM3821@ando.pearwood.info> On Mon, Nov 23, 2015 at 10:07:36AM +0000, Oscar Benjamin wrote: > On 23 November 2015 at 01:42, CMG Thrissur wrote: > > Is the below text in plain text? I am using thunderbird. > > No it isn't. I've kept the first part of it so you can see how it looks: Looks like plain text to me. CMG's email has only a single part. In mutt, it looks like this: I 1 [text/plain, 7bit, us-ascii, 7.4K] Text/plain, 7-bit ASCII. How much plainer text do you want? I'm pretty sure the problem is the format='flowed', not that it is being sent as HTML. -- Steve From lac at openend.se Mon Nov 23 06:06:15 2015 From: lac at openend.se (Laura Creighton) Date: Mon, 23 Nov 2015 12:06:15 +0100 Subject: [Tutor] How to kill a thread In-Reply-To: <20151123101207.GJ3821@ando.pearwood.info> References: <56515876.2090203@gmail.com> <201511221154.tAMBsdKm023768@fido.openend.se> <5651E8E5.3030001@gmail.com> <201511221842.tAMIgxpR018716@fido.openend.se> <56526F05.9090409@gmail.com> <20151123101207.GJ3821@ando.pearwood.info> Message-ID: <201511231106.tANB6Fbc006066@fido.openend.se> Yes, in Thunderbird disabling the flowing is separate from setting plain text. http://kb.mozillazine.org/Plain_text_e-mail_-_Thunderbird#Flowed_format see: 'Completely plain email' right down on the bottom of the page. That is what you want. Laura From cmgcomsol at gmail.com Mon Nov 23 06:45:09 2015 From: cmgcomsol at gmail.com (CMG Thrissur) Date: Mon, 23 Nov 2015 17:15:09 +0530 Subject: [Tutor] How to kill a thread In-Reply-To: <56526F05.9090409@gmail.com> References: <56515876.2090203@gmail.com> <201511221154.tAMBsdKm023768@fido.openend.se> <5651E8E5.3030001@gmail.com> <201511221842.tAMIgxpR018716@fido.openend.se> <56526F05.9090409@gmail.com> Message-ID: <5652FC45.7080605@gmail.com> Thank you all. i have pasted the code on pastebin. http://pastebin.com/embed_iframe.php?i=kHAUJQC2 My one main question in the code is that there are times when my thread using pythonwhois hangs up the process by statying active very long time. can i hope for a better solution to this problem. thanks George From __peter__ at web.de Tue Nov 24 04:09:01 2015 From: __peter__ at web.de (Peter Otten) Date: Tue, 24 Nov 2015 10:09:01 +0100 Subject: [Tutor] How to kill a thread References: <56515876.2090203@gmail.com> <201511221154.tAMBsdKm023768@fido.openend.se> <5651E8E5.3030001@gmail.com> <201511221842.tAMIgxpR018716@fido.openend.se> <56526F05.9090409@gmail.com> <5652FC45.7080605@gmail.com> Message-ID: CMG Thrissur wrote: > Thank you all. i have pasted the code on pastebin. > > http://pastebin.com/embed_iframe.php?i=kHAUJQC2 > > My one main question in the code is that there are times when my thread > using pythonwhois hangs up the process by statying active very long > time. can i hope for a better solution to this problem. > result = pythonwhois.get_whois(workurl) I don't know pythonwhois. Does it invoke the whois commandline tool or implement the protocol itself? If it implements the protocol you can try and set a timeout, e. g. import socket socket.setdefaulttimeout(1) If it uses subprocess to invoke whois and uses the communicate() method to fetch the data you can tweak the code and provide a timeout, see https://docs.python.org/dev/library/subprocess.html#subprocess.Popen.communicate If none of the above suggestions work tell us the exact Python version you are using and provide a link to the pythonwhois module so that we can try to come up with something else. From sjeik_appie at hotmail.com Tue Nov 24 10:36:21 2015 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Tue, 24 Nov 2015 15:36:21 +0000 Subject: [Tutor] a class that may not be instantiated Message-ID: Hi, I have two classes with a number of methods that are the same, so I want to define a super class that holds these methods. But the super class (below called _Generic) should not be instantiated, because it serves no purpose other than the DRY principle. I raise a NotImplementedError in case if somebody dares to instantiate _Generic. Below, only one common method is defined in_Generic, namely __repr__ (the other ones are __enter__ and __exit__, maybe more, if you must know). At first I thought I'd need the abc module for this, but this seems to do the trick. I do not want to enforce concrete implementations of abstract methods, which is the goal of abc. Is this the way to do this, or this this quirky code? import inspect class _Generic(object): ??? def __init__(self, *args, **kwargs): ??????? raise NotImplementedError ??? def __repr__(self): ??????? fmt = [] ??????? for arg in inspect.getargspec(self.__init__).args[1:]: ??????????? value = getattr(self, arg) ??????????? sr = "%r" if isinstance(value, basestring) else "%s" ??????????? fmt.append(("%s=" + sr) % (arg, value)) ??????? return self.__class__.__name__ + "(" + ", ".join(fmt) + ")" ??????? class Concrete(Generic): ??? def __init__(self, x=42, y='a'): ??????? self.x = x ??????? self.y = y ??? class OneMore(Generic): ??? def __init__(self, w=555, z='foo'): ??????? self.w = w ??????? self.z = z ??? c = Concrete() print repr(c) a = _Generic(666) ? # NotImplementedError Thank you! Albert-Jan From __peter__ at web.de Tue Nov 24 11:31:44 2015 From: __peter__ at web.de (Peter Otten) Date: Tue, 24 Nov 2015 17:31:44 +0100 Subject: [Tutor] a class that may not be instantiated References: Message-ID: Albert-Jan Roskam wrote: > I have two classes with a number of methods that are the same, so I want to define a super class that holds these methods. But the super class (below called _Generic) should not be instantiated, because it serves no purpose other than the DRY principle. I raise a NotImplementedError in case if somebody dares to instantiate _Generic. Below, only one common method is defined in_Generic, namely __repr__ (the other ones are __enter__ and __exit__, maybe more, if you must know). At first I thought I'd need the abc module for this, but this seems to do the trick. I do not want to enforce concrete implementations of abstract methods, which is the goal of abc. Is this the way to do this, or this this quirky code? I think this is OK, but I don't understand your argument against # python 2 class _Generic: __metaclass__ = abc.ABCMeta @abc.abstractmethod def __init__(self, *args, **kwargs): pass either. > import inspect > > class _Generic(object): > > def __init__(self, *args, **kwargs): > raise NotImplementedError > > def __repr__(self): > fmt = [] > for arg in inspect.getargspec(self.__init__).args[1:]: > value = getattr(self, arg) > sr = "%r" if isinstance(value, basestring) else "%s" > fmt.append(("%s=" + sr) % (arg, value)) > return self.__class__.__name__ + "(" + ", ".join(fmt) + ")" > > class Concrete(Generic): > > def __init__(self, x=42, y='a'): > self.x = x > self.y = y > > class OneMore(Generic): > > def __init__(self, w=555, z='foo'): > self.w = w > self.z = z > > > c = Concrete() > print repr(c) > > a = _Generic(666) # NotImplementedError From alan.gauld at btinternet.com Tue Nov 24 12:37:43 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 24 Nov 2015 17:37:43 +0000 Subject: [Tutor] a class that may not be instantiated In-Reply-To: References: Message-ID: On 24/11/15 15:36, Albert-Jan Roskam wrote: > I have two classes with a number of methods that are the same, > so I want to define a super class that holds these methods. So far so good. > But the super class (below called _Generic) should not be instantiated, But here it starts to feel a tad non Pythonic to me. It's not C++. You can certainly do it (as you show below). But should you? Is a docstring comment not sufficient? Why would anyone want to instantiate Generic? And if they do think of a valid reason, why stop them? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From oscar.j.benjamin at gmail.com Tue Nov 24 12:39:51 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 24 Nov 2015 17:39:51 +0000 Subject: [Tutor] a class that may not be instantiated In-Reply-To: References: Message-ID: On 24 November 2015 at 15:36, Albert-Jan Roskam wrote: > Hi, > > I have two classes with a number of methods that are the same, so I want to define a super class that holds these methods. But the super class (below called _Generic) should not be instantiated, because it serves no purpose other than the DRY principle. I raise a NotImplementedError in case if somebody dares to instantiate _Generic. Below, only one common method is defined in_Generic, namely __repr__ (the other ones are __enter__ and __exit__, maybe more, if you must know). At first I thought I'd need the abc module for this, but this seems to do the trick. I do not want to enforce concrete implementations of abstract methods, which is the goal of abc. Is this the way to do this, or this this quirky code? > > import inspect > > class _Generic(object): > > def __init__(self, *args, **kwargs): > raise NotImplementedError > > def __repr__(self): > fmt = [] > for arg in inspect.getargspec(self.__init__).args[1:]: > value = getattr(self, arg) > sr = "%r" if isinstance(value, basestring) else "%s" > fmt.append(("%s=" + sr) % (arg, value)) > return self.__class__.__name__ + "(" + ", ".join(fmt) + ")" I just wouldn't bother with this (personally). Firstly why are you worried that someone will try to instantiate _Generic? I don't imagine that I would if I was trying to use your library since it has an underscore prefix and presumably your user-facing docs don't mention it. You could make that more explicit by putting the word Abstract into the class name or something. Really though that class is just a function: def my_repr_function(self): fmt = [] for arg in inspect.getargspec(self.__init__).args[1:]: value = getattr(self, arg) sr = "%r" if isinstance(value, basestring) else "%s" fmt.append(("%s=" + sr) % (arg, value)) return self.__class__.__name__ + "(" + ", ".join(fmt) + ")" Then you can do: class Concrete(Generic): ... __repr__ = my_repr_function I realise that in your actual case there are more methods but then this can just be a mixin class. Then you can call it something with Mixin in the name and again people will know not to use it (except in inheritance). -- Oscar From evanlespaul at gmail.com Tue Nov 24 09:01:58 2015 From: evanlespaul at gmail.com (Evan Sommer) Date: Tue, 24 Nov 2015 09:01:58 -0500 Subject: [Tutor] Countdown Clock Programming Question Message-ID: Hello there! I am working on a project for an engineering class that I am in at my high school, and our task has been to create a clock that counts down the time between periods, so that the students walking the hallways know how much time they have to get to class. The timer will be displayed on multiple monitors throughout the halls. However, the idea behind the countdown clock is for the background to change colours when it hits a certain time. The goal is for the clock to change from green to yellow at 2 minutes, and yellow to red at 1 minute. However, I have been having a hard time trying to get the color change to display in one window. If you could give me some advice, I'd really appreciate it! Here's the code: try: # Python2 import Tkinter as tk except ImportError: # Python3 import tkinter as tk import time def count_down(): # start with 4 minutes --> 240 seconds for t in range(240, 120, -1): # format as 2 digit integers, fills with zero to the left # divmod() gives minutes, seconds sf = "{:01d}:{:02d}".format(*divmod(t, 60)) #print(sf) # test time_str.set(sf) root.update() # delay one second time.sleep(1)# create root/main window root = tk.Tk() time_str = tk.StringVar() # create the time display label, give it a large font # label auto-adjusts to the font label_font = ('helvetica', 100) tk.Label(root, textvariable=time_str, font=label_font, bg='green', fg='white', relief='raised', bd=3).pack(fill='x', padx=5, pady=5) # start with 2 minutes --> 119 seconds for t in range(240, 120, -1): # format as 2 digit integers, fills with zero to the left # divmod() gives minutes, seconds sf = "{:01d}:{:02d}".format(*divmod(t, 60)) #print(sf) # test time_str.set(sf) root.update() # delay one second time.sleep(1) # create the time display label, give it a large font # label auto-adjusts to the font label_font = ('helvetica', 100) tk.Label(root, textvariable=time_str, font=label_font, bg='yellow', fg='white', relief='raised', bd=3).pack(fill='x', padx=5, pady=5) # start with 1 minutes --> 59 seconds for t in range(120,60, -1): # format as 2 digit integers, fills with zero to the left # divmod() gives minutes, seconds sf = "{:01d}:{:02d}".format(*divmod(t, 60)) #print(sf) # test time_str.set(sf) root.update() # delay one second time.sleep(1) # create the time display label, give it a large font # label auto-adjusts to the font label_font = ('helvetica', 100) tk.Label(root, textvariable=time_str, font=label_font, bg='red', fg='white', relief='raised', bd=3).pack(fill='x', padx=5, pady=5) # start with 4 minutes --> 240 seconds for t in range(60,-1, -1): # format as 2 digit integers, fills with zero to the left # divmod() gives minutes, seconds sf = "{:01d}:{:02d}".format(*divmod(t, 60)) #print(sf) # test time_str.set(sf) root.update() # delay one second time.sleep(1) # start the GUI event loop root.mainloop() Thanks for the help! Sincerely, Evan Sommer From alan.gauld at btinternet.com Tue Nov 24 13:19:22 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 24 Nov 2015 18:19:22 +0000 Subject: [Tutor] Countdown Clock Programming Question In-Reply-To: References: Message-ID: On 24/11/15 14:01, Evan Sommer wrote: > goal is for the clock to change from green to yellow at 2 minutes, and > yellow to red at 1 minute. > def count_down(): > # start with 4 minutes --> 240 seconds > for t in range(240, 120, -1): > # format as 2 digit integers, fills with zero to the left > # divmod() gives minutes, seconds > sf = "{:01d}:{:02d}".format(*divmod(t, 60)) > #print(sf) # test > time_str.set(sf) > root.update() > # delay one second > time.sleep(1)# create root/main window Its not recommended to use sleep in a GUI program, the Tkinter library provides the after() method to do this instead. In your case you'd call it with a timeout of 1000ms Also because sleep() and after() are rather imprecise in their timings it will be more accurate to use the time module to get the actual start time and then on each update fetch that again and compute the difference. (Although I doubt it will make much difference over 4 minutes!) eg target_time = time.time() + 240 # now plus 4 min and then display_period = target_time - time.time() # accurate seconds left > root = tk.Tk() > time_str = tk.StringVar() > # create the time display label, give it a large font > # label auto-adjusts to the font > label_font = ('helvetica', 100) > tk.Label(root, textvariable=time_str, font=label_font, bg='green', > fg='white', relief='raised', bd=3).pack(fill='x', padx=5, pady=5) It would be better to create the label once and then just change its colours. But for that you need to store a reference to it: timeLabel = tk.Label(root, textvariable=time_str, font=label_font, bg='green', fg='white', relief='raised', bd=3).pack(fill='x', padx=5, pady=5) I'd also miss out the Stringvar and just update the text attribute directly. Stringvar is great when you want two way interaction with a field but for a label it doesn't really help, IMHO. > # start with 2 minutes --> 119 seconds > for t in range(240, 120, -1): > # format as 2 digit integers, fills with zero to the left > # divmod() gives minutes, seconds > sf = "{:01d}:{:02d}".format(*divmod(t, 60)) > #print(sf) # test > time_str.set(sf) > root.update() > # delay one second > time.sleep(1) > # create the time display label, give it a large font > # label auto-adjusts to the font > label_font = ('helvetica', 100) You already defined label_font no need to do it again. > tk.Label(root, textvariable=time_str, font=label_font, bg='yellow', > fg='white', relief='raised', bd=3).pack(fill='x', padx=5, pady=5) > # start with 1 minutes --> 59 seconds Doesn't this create a second label on your display? Or do you delete the old one somewhere that I haven't seen? > for t in range(120,60, -1): > # format as 2 digit integers, fills with zero to the left > # divmod() gives minutes, seconds > sf = "{:01d}:{:02d}".format(*divmod(t, 60)) > #print(sf) # test > time_str.set(sf) > root.update() > # delay one second > time.sleep(1) > # create the time display label, give it a large font > # label auto-adjusts to the font > label_font = ('helvetica', 100) > tk.Label(root, textvariable=time_str, font=label_font, bg='red', > fg='white', relief='raised', bd=3).pack(fill='x', padx=5, pady=5) > # start with 4 minutes --> 240 seconds See previous comments re duplicating code. > for t in range(60,-1, -1): > # format as 2 digit integers, fills with zero to the left > # divmod() gives minutes, seconds > sf = "{:01d}:{:02d}".format(*divmod(t, 60)) > #print(sf) # test > time_str.set(sf) > root.update() > # delay one second > time.sleep(1) > # start the GUI event loop > root.mainloop() > Personally I'd do it in a pair of functions that look loosely like def initialise(): set up GUI create label with startuing font/size/color target_time = time.time() + 240 label[text] = "4:00" after(1000, update_display) def update_display() display_period = target_time - time.time() min,sec = divmod(display_period,60) if min < 2: set color to yellow elif: min < 1: set color to red else: set color to green set label text to min/sec after(1000, update_display) No loops required and no repeated code. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From richkappler at gmail.com Tue Nov 24 13:22:06 2015 From: richkappler at gmail.com (richard kappler) Date: Tue, 24 Nov 2015 13:22:06 -0500 Subject: [Tutor] responding to command line Message-ID: I need to write a script that runs ssh-copy-id to over 500 clients from one master (setting up keys to use ansible). I understand how to get my script to read each IP address in a list, use it as input and then move on to the next, but not sure how to handle the password feed, particularly as there will be a considerable delay in the passwords request. Specifically: $ssh-copy-id user at IPofClient .... delay of as much as 90 or more seconds..... user at IPofClient's password: and I want the script to detect that password request and provide the password (Same for all 500 + machines, so no issue there). -- All internal models of the world are approximate. ~ Sebastian Thrun From steve at pearwood.info Tue Nov 24 13:19:57 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 25 Nov 2015 05:19:57 +1100 Subject: [Tutor] a class that may not be instantiated In-Reply-To: References: Message-ID: <20151124181957.GQ3821@ando.pearwood.info> On Tue, Nov 24, 2015 at 03:36:21PM +0000, Albert-Jan Roskam wrote: > Hi, > > I have two classes with a number of methods that are the same, so I > want to define a super class that holds these methods. Sounds like a misuse of classes to me. DRY is not a good reason to make two otherwise unrelated classes subclasses of an otherwise pointless parent. Better solutions include: - Factor out the common code to a module-level function, and have the classes call that function from their methods: def spam(n): return " ".join(["spam"]*n) class Parrot: def speak(self): return "Polly want some %s." % spam(5) class Viking: def sing(self): return "%s WONDERFUL %s!!!" % (spam(4), spam(1)) - Use a Mixin class to hold the shared methods, and "mix them in" to the two other classes as needed. In Python, mixins aren't treated any differently from "ordinary" classes. But the difference is conceptual. Contrast: class Generic: ... class Parrot(Generic): ... class Viking(Generic): ... This implies that both parrots and vikings are a kind of "generic", whatever that is. class SpamMixin: ... class Parrot(SpamMixin): ... class Viking(SpamMixin): ... This tells the reader that the intent is for the SpamMixin to "mix in" common methods to parrots and vikings, without implying anything about them both being the same kind of thing. But, let's say you still want to proceed with your first plan: > But the super > class (below called _Generic) should not be instantiated, because it > serves no purpose other than the DRY principle. I raise a > NotImplementedError in case if somebody dares to instantiate _Generic. Sounds reasonable, although you should be kind to your subclasses: class _Generic(object): ???def __init__(self, *args, **kwargs): if type(self) is _Generic: ??????? raise NotImplementedError('abstract base class cannot be instantiated') Now your concrete subclasses aren't forced to override __init__ if they don't need to. -- Steve From lac at openend.se Tue Nov 24 14:48:28 2015 From: lac at openend.se (Laura Creighton) Date: Tue, 24 Nov 2015 20:48:28 +0100 Subject: [Tutor] Countdown Clock Programming Question In-Reply-To: References: Message-ID: <201511241948.tAOJmSHa001120@fido.openend.se> Welcome Evan. In a message of Tue, 24 Nov 2015 18:19:22 +0000, Alan Gauld writes: >def update_display() > display_period = target_time - time.time() > min,sec = divmod(display_period,60) > if min < 2: set color to yellow > elif: min < 1: set color to red > else: set color to green > set label text to min/sec > after(1000, update_display) You'd better check them in red, yellow, green order, or the reverse. if min < 1: set colour to red elif min < 2: set colour to yellow else: set colour to green Your pseudo-code will only show yellow and green colours. Laura From alan.gauld at btinternet.com Tue Nov 24 16:37:43 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 24 Nov 2015 21:37:43 +0000 Subject: [Tutor] Countdown Clock Programming Question In-Reply-To: <201511241948.tAOJmSHa001120@fido.openend.se> References: <201511241948.tAOJmSHa001120@fido.openend.se> Message-ID: <5654D8A7.7010207@btinternet.com> On 24/11/15 19:48, Laura Creighton wrote: > Welcome Evan. > > In a message of Tue, 24 Nov 2015 18:19:22 +0000, Alan Gauld writes: >> def update_display() >> display_period = target_time - time.time() >> min,sec = divmod(display_period,60) >> if min < 2: set color to yellow >> elif: min < 1: set color to red >> else: set color to green >> set label text to min/sec >> after(1000, update_display) > You'd better check them in red, yellow, green order, or the reverse. > > if min < 1: > set colour to red > elif min < 2: > set colour to yellow > else: > set colour to green > > Your pseudo-code will only show yellow and green colours. Oops, yes. Good catch. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From pythonbeginner004 at gmail.com Tue Nov 24 13:36:54 2015 From: pythonbeginner004 at gmail.com (Python Beginner) Date: Tue, 24 Nov 2015 13:36:54 -0500 Subject: [Tutor] PDF Scrapping Message-ID: Hi, I am looking for the best way to scrape the following PDF's: (1) http://minerals.usgs.gov/minerals/pubs/commodity/gold/mcs-2015-gold.pdf (table on page 1) (2) http://minerals.usgs.gov/minerals/pubs/commodity/gold/myb1-2013-gold.pdf (table 1) I have done a lot of research and have read that pdftables 0.0.4 is an excellent way to scrape tabular data from PDF'S (see https://blog.scraperwiki.com/2013/07/pdftables-a-python-library-for-getting-tables-out-of-pdf-files/ ). I downloaded pdftables 0.0.4 (see https://pypi.python.org/pypi/pdftables). I am new to Python and having trouble finding good documentation for how to use this library. Has anybody used pdftables before that could help me get started or point me to the ideal library for scrapping the PDF links above? I have read that different PDF libraries are used depending on the format of the PDF. What library would be best for the PDF formats above? Knowing this will help me get started, then I can write up some code and ask further questions if needed. Thanks in advance for your help! ~Chris From crusier at gmail.com Wed Nov 25 04:28:24 2015 From: crusier at gmail.com (Crusier) Date: Wed, 25 Nov 2015 17:28:24 +0800 Subject: [Tutor] Problem on handling if statement Message-ID: Dear All, I am trying to do some web scraping. Attached below is my code: from bs4 import BeautifulSoup import requests #string = str(0175, 0005, 1177) url = "https://www.etnet.com.hk/www/eng/stocks/realtime/quote.php?code=0175" def web_scraper(url): response = requests.get(url) html = response.content soup = BeautifulSoup(html,"html.parser") real_time_down = soup.find("span", attrs = {"class": "Price down2"}) real_time_up = soup.find("span", attrs = {"class": "Price up2"}) real_time_unchange = soup.find("span",attrs = {"class" :"Price unchange2"}) change_percent = soup.find("span", attrs = {"class" :"Change"}) if real_time_down == soup.find("span", attrs = {"class" : "Price down2"}) or real_time_up == soup.find("span", attrs \ = {"class": "Price up2"}) or real_time_unchange == soup.find("span",{"class" : "Price unchange2"}) : print(real_time_down) print(real_time_up) print(real_time_unchange) print(change_percent.string) else: return None web_scraper(url) I have problem trying to get rid of None object. For example, if I put in 1177 to the url, the real_price_down and the real_time_unchange will become a None Object. I hope that the program will be able to sort it out automatically and able to print out the string. Please help. Thank you very much Regards, Henry From chantie1611 at yahoo.com Tue Nov 24 22:29:38 2015 From: chantie1611 at yahoo.com (chantie sunday) Date: Wed, 25 Nov 2015 03:29:38 +0000 (UTC) Subject: [Tutor] Help References: <119783506.10422370.1448422178959.JavaMail.yahoo.ref@mail.yahoo.com> Message-ID: <119783506.10422370.1448422178959.JavaMail.yahoo@mail.yahoo.com> Hello!,Can I kindly get someone to help me with my assignment? Thank you!Best Regards,Chantelle From patrickhess at gmx.net Wed Nov 25 03:27:47 2015 From: patrickhess at gmx.net (Patrick Hess) Date: Wed, 25 Nov 2015 09:27:47 +0100 Subject: [Tutor] responding to command line In-Reply-To: References: Message-ID: <1636122.vvXsxp1epV@desk8.phess.net> richard kappler wrote: > and I want the script to detect that password request and provide the > password (Same for all 500 + machines, so no issue there). My recommendation would be to take a look at Expect[1]. Yes, I know, it's not Python, but using a tool specifically tailored to handle this kind of task will make life so much easier that it even justifies the use of Tcl. ;-) Patrick [1] http://expect.sourceforge.net From alan.gauld at btinternet.com Wed Nov 25 05:08:41 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 25 Nov 2015 10:08:41 +0000 Subject: [Tutor] Help In-Reply-To: <119783506.10422370.1448422178959.JavaMail.yahoo@mail.yahoo.com> References: <119783506.10422370.1448422178959.JavaMail.yahoo.ref@mail.yahoo.com> <119783506.10422370.1448422178959.JavaMail.yahoo@mail.yahoo.com> Message-ID: On 25/11/15 03:29, chantie sunday via Tutor wrote: > Hello!,Can I kindly get someone to help me with my assignment? Thank you!Best Regards,Chantelle Sure, we will help you, but we won't do the assignment for you, just give you pointers in the right direction. But first you need to tell us: What Python version? What OS? What assignment? What have you done so far? (show us your code) What are you stuck on? If you get any errors include the full error message. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Wed Nov 25 05:11:37 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 25 Nov 2015 10:11:37 +0000 Subject: [Tutor] responding to command line In-Reply-To: <1636122.vvXsxp1epV@desk8.phess.net> References: <1636122.vvXsxp1epV@desk8.phess.net> Message-ID: On 25/11/15 08:27, Patrick Hess wrote: > richard kappler wrote: >> and I want the script to detect that password request and provide the >> password (Same for all 500 + machines, so no issue there). > > My recommendation would be to take a look at Expect[1]. > > Yes, I know, it's not Python, but using a tool specifically > tailored to handle this kind of task will make life so much > easier that it even justifies the use of Tcl. ;-) But there is a python wrapoper round expect - pexpect - that means you don't need to learn Tcl :-) https://github.com/pexpect/pexpect -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From lac at openend.se Wed Nov 25 05:55:35 2015 From: lac at openend.se (Laura Creighton) Date: Wed, 25 Nov 2015 11:55:35 +0100 Subject: [Tutor] Problem on handling if statement In-Reply-To: References: Message-ID: <201511251055.tAPAtZHg015654@fido.openend.se> In a message of Wed, 25 Nov 2015 17:28:24 +0800, Crusier writes: >Dear All, > >I am trying to do some web scraping. Attached below is my code: > > >from bs4 import BeautifulSoup >import requests > >#string = str(0175, 0005, 1177) >url = "https://www.etnet.com.hk/www/eng/stocks/realtime/quote.php?code=0175" > > >def web_scraper(url): > response = requests.get(url) > html = response.content > soup = BeautifulSoup(html,"html.parser") > > real_time_down = soup.find("span", attrs = {"class": "Price down2"}) > real_time_up = soup.find("span", attrs = {"class": "Price up2"}) > real_time_unchange = soup.find("span",attrs = {"class" :"Price unchange2"}) > change_percent = soup.find("span", attrs = {"class" :"Change"}) > > if real_time_down == soup.find("span", attrs = {"class" : "Price >down2"}) or real_time_up == soup.find("span", attrs \ > = {"class": "Price up2"}) or real_time_unchange == >soup.find("span",{"class" : "Price unchange2"}) : > print(real_time_down) > print(real_time_up) > print(real_time_unchange) > print(change_percent.string) > > else: > return None > >web_scraper(url) > >I have problem trying to get rid of None object. For example, if I put >in 1177 to the url, the real_price_down and the real_time_unchange >will become a None Object. I hope that the program will be able to >sort it out automatically and able to print out the string. > > >Please help. Thank you very much > >Regards, >Henry Why are you returning None in the first place? Nothing in the code seems to indicate you need to. To figure out what is going on, split the code into 3 pieces: real_time_down == soup.find("span", attrs = {"class" : "Price down2"}) real_time_up == soup.find("span", attrs = {"class": "Price up2"}) real_time_unchange ==soup.find("span",{"class" : "Price unchange2"}) Printing them is fine: print(real_time_down) print(real_time_up) print(real_time_unchange) These 3 lines will print either the value you scraped, or None if it wasn't found. You can say: if real_time_down is not None: # you can also say if real_time_down: print(real_time_down) if you only want things printed if they exist. But you cannot join them all up the way you did and expect Python to figure out that you want all the parts that can be printed. Just write 3 if statements in a row: if real_time_down: print(real_time_down) if real_time_up: print(real_time_up) if real_time_unchanged: print(real_time_unchange) For 3 values, that's not a bad thing. Reading a screen or more of them would get hard to read and understand, so ifyou are going to have a whole lot of these things, then it might be easier to write: for data in [real_time_down, real_time_up, real_time_unchange, ]: if data: print(data) HTH, Laura From lac at openend.se Wed Nov 25 06:34:57 2015 From: lac at openend.se (Laura Creighton) Date: Wed, 25 Nov 2015 12:34:57 +0100 Subject: [Tutor] Help In-Reply-To: <119783506.10422370.1448422178959.JavaMail.yahoo@mail.yahoo.com> References: <119783506.10422370.1448422178959.JavaMail.yahoo.ref@mail.yahoo.com> <119783506.10422370.1448422178959.JavaMail.yahoo@mail.yahoo.com> Message-ID: <201511251134.tAPBYvK1016293@fido.openend.se> In a message of Wed, 25 Nov 2015 03:29:38 +0000, chantie sunday via Tutor write s: >Hello!,Can I kindly get someone to help me with my assignment? Thank you!Best Regards,Chantelle Hi, and welcome. We don't do people's assignments, but we do help them write them. Post the assignment and the code that you have, and tell us what is not working. You need to teach your mailer to not send your replies flowed as one line of text. Spacing is significant in Python. if condition: print("condition was true") print("more stuff") and if condition: print("condition was true") print("more stuff") do not mean the same thing -- so if your mailer mangles your program we won't be able to figure out what you really wrote. Laura From pythonbeginner004 at gmail.com Wed Nov 25 08:41:31 2015 From: pythonbeginner004 at gmail.com (Python Beginner) Date: Wed, 25 Nov 2015 08:41:31 -0500 Subject: [Tutor] PDF Scrapping In-Reply-To: References: Message-ID: Oh, I forgot to mention that I am using Python 3.4. Thanks again for your help pointing me in the right direction. ~Chris On Tue, Nov 24, 2015 at 1:36 PM, Python Beginner < pythonbeginner004 at gmail.com> wrote: > Hi, > > I am looking for the best way to scrape the following PDF's: > > (1) > http://minerals.usgs.gov/minerals/pubs/commodity/gold/mcs-2015-gold.pdf > (table on page 1) > > (2) > http://minerals.usgs.gov/minerals/pubs/commodity/gold/myb1-2013-gold.pdf > (table 1) > > I have done a lot of research and have read that pdftables 0.0.4 is an > excellent way to scrape tabular data from PDF'S (see > https://blog.scraperwiki.com/2013/07/pdftables-a-python-library-for-getting-tables-out-of-pdf-files/ > ). > > I downloaded pdftables 0.0.4 (see https://pypi.python.org/pypi/pdftables). > > I am new to Python and having trouble finding good documentation for how > to use this library. > > Has anybody used pdftables before that could help me get started or point > me to the ideal library for scrapping the PDF links above? I have read that > different PDF libraries are used depending on the format of the PDF. What > library would be best for the PDF formats above? Knowing this will help me > get started, then I can write up some code and ask further questions if > needed. > > Thanks in advance for your help! > > ~Chris > From sjeik_appie at hotmail.com Wed Nov 25 10:51:59 2015 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Wed, 25 Nov 2015 15:51:59 +0000 Subject: [Tutor] a class that may not be instantiated In-Reply-To: <20151124181957.GQ3821@ando.pearwood.info> References: , <20151124181957.GQ3821@ando.pearwood.info> Message-ID: Hi all, Thanks a lot for your replies! > Date: Wed, 25 Nov 2015 05:19:57 +1100 > From: steve at pearwood.info > To: tutor at python.org > Subject: Re: [Tutor] a class that may not be instantiated > > On Tue, Nov 24, 2015 at 03:36:21PM +0000, Albert-Jan Roskam wrote: > > Hi, > > > > I have two classes with a number of methods that are the same, so I > > want to define a super class that holds these methods. > > Sounds like a misuse of classes to me. DRY is not a good reason to make > two otherwise unrelated classes subclasses of an otherwise pointless > parent. > > Better solutions include: > > - Factor out the common code to a module-level function, and have the > classes call that function from their methods: > - Use a Mixin class to hold the shared methods, and "mix them in" to the > two other classes as needed. I think I like this option best. There is as little "visual clutter" in the child classes as possible, but the user does not get confusing info about parent-child relationships.When the methods are converted to functions, one still needs to implement a (very short) method in the child class.But the term "Mixin" is just a convention, right? (Similar to a single leading underscore) It's not that the class name gets mangled whenever it contains the string "Mixin" (similar to name mangling with two leading underscores). > But, let's say you still want to proceed with your first plan: > > > > But the super > > class (below called _Generic) should not be instantiated, because it > > serves no purpose other than the DRY principle. I raise a > > NotImplementedError in case if somebody dares to instantiate _Generic. > > Sounds reasonable, although you should be kind to your subclasses: > > class _Generic(object): > def __init__(self, *args, **kwargs): > if type(self) is _Generic: > raise NotImplementedError('abstract base class cannot be instantiated') > > > Now your concrete subclasses aren't forced to override __init__ if they > don't need to. That "if type"check is a very nice addition indeed. That was also my objection against the use of an abstract method with abc, as Peter mentioned:with abc the __init__ *must* be implemented in the concrete class. Not usually a big problem, but still... regards,Albert-Jan From francois.dion at gmail.com Wed Nov 25 12:43:51 2015 From: francois.dion at gmail.com (Francois Dion) Date: Wed, 25 Nov 2015 12:43:51 -0500 Subject: [Tutor] PDF Scrapping In-Reply-To: References: Message-ID: This is well beyond the scope of Tutor, but let me mention the following: The code to pdftables disappeared from github some time back. What is on sourceforge is old, same with pypi. I wouldn't create a project using pdftables based on that... As far as what you are trying to do, it looks like they might have the data in excel spreadsheets. That is totally trivial to load in pandas. if you have any choice at all, avoid PDF at all cost to get data. See some detail of the complexity here: http://ieg.ifs.tuwien.ac.at/pub/yildiz_iicai_2005.pdf For your two documents, if you cannot find the data in the excel sheets, I think the tabula (ruby based application) approach is the best bet. Francois On Wed, Nov 25, 2015 at 8:41 AM, Python Beginner < pythonbeginner004 at gmail.com> wrote: > Oh, I forgot to mention that I am using Python 3.4. Thanks again for your > help pointing me in the right direction. > > ~Chris > > On Tue, Nov 24, 2015 at 1:36 PM, Python Beginner < > pythonbeginner004 at gmail.com> wrote: > > > Hi, > > > > I am looking for the best way to scrape the following PDF's: > > > > (1) > > http://minerals.usgs.gov/minerals/pubs/commodity/gold/mcs-2015-gold.pdf > > (table on page 1) > > > > (2) > > http://minerals.usgs.gov/minerals/pubs/commodity/gold/myb1-2013-gold.pdf > > (table 1) > > > > I have done a lot of research and have read that pdftables 0.0.4 is an > > excellent way to scrape tabular data from PDF'S (see > > > https://blog.scraperwiki.com/2013/07/pdftables-a-python-library-for-getting-tables-out-of-pdf-files/ > > ). > > > > I downloaded pdftables 0.0.4 (see https://pypi.python.org/pypi/pdftables > ). > > > > I am new to Python and having trouble finding good documentation for how > > to use this library. > > > > Has anybody used pdftables before that could help me get started or point > > me to the ideal library for scrapping the PDF links above? I have read > that > > different PDF libraries are used depending on the format of the PDF. What > > library would be best for the PDF formats above? Knowing this will help > me > > get started, then I can write up some code and ask further questions if > > needed. > > > > Thanks in advance for your help! > > > > ~Chris > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- raspberry-python.blogspot.com - www.pyptug.org - www.3DFutureTech.info - @f_dion From lac at openend.se Wed Nov 25 13:29:26 2015 From: lac at openend.se (Laura Creighton) Date: Wed, 25 Nov 2015 19:29:26 +0100 Subject: [Tutor] PDF Scrapping In-Reply-To: References: Message-ID: <201511251829.tAPITQdX022650@fido.openend.se> In a message of Wed, 25 Nov 2015 12:43:51 -0500, Francois Dion writes: >This is well beyond the scope of Tutor, but let me mention the following: > >The code to pdftables disappeared from github some time back. What is on >sourceforge is old, same with pypi. I wouldn't create a project using >pdftables based on that... > >As far as what you are trying to do, it looks like they might have the data >in excel spreadsheets. That is totally trivial to load in pandas. if you >have any choice at all, avoid PDF at all cost to get data. See some detail >of the complexity here: >http://ieg.ifs.tuwien.ac.at/pub/yildiz_iicai_2005.pdf > >For your two documents, if you cannot find the data in the excel sheets, I >think the tabula (ruby based application) approach is the best bet. > >Francois What he said. Double. However ... you can also use see about using popplar. It has a nice pdftohtml utility. Once you get your data in as html, if you are lucky, and the table information didn't get destroyed in the process, you can then send your data to pandas, which will happily read html tables. Once you have pandas reading it, you are pretty much home free and can do whatever you like with the data. If you happen to be on ubuntu, then getting popplar and pdftohtml is easy. http://www.ubuntugeek.com/howto-convert-pdf-files-to-html-files.html It seems to be harder on windows, but there are stackoverflow questions outlining how to do it ... Laura From patrickhess at gmx.net Wed Nov 25 10:35:16 2015 From: patrickhess at gmx.net (Patrick Hess) Date: Wed, 25 Nov 2015 16:35:16 +0100 Subject: [Tutor] responding to command line In-Reply-To: References: <1636122.vvXsxp1epV@desk8.phess.net> Message-ID: <1617708.bsv6H40eDL@desk8.phess.net> Alan Gauld wrote: > But there is a python wrapoper round expect - pexpect - that > means you don't need to learn Tcl ;) > > https://github.com/pexpect/pexpect Interesting, thanks. And better yet, it's not just a wrapper around Expect: "Pexpect is in the spirit of Don Libes' Expect, but Pexpect is pure Python." So you don't even have to install anything Tcl. :-) On the downside, Windows support seems to be quite limited at this point. However, if that's not of any concern to Richard, Pexpect is probably his best option. This example right here looks like a good starting point: https://github.com/pexpect/pexpect/blob/master/examples/passmass.py I, unfortunately, have to deal with Windows from time to time, so it's still Tcl for me... Patrick From ag4ve.us at gmail.com Wed Nov 25 14:38:57 2015 From: ag4ve.us at gmail.com (shawn wilson) Date: Wed, 25 Nov 2015 14:38:57 -0500 Subject: [Tutor] PDF Scrapping In-Reply-To: References: Message-ID: On Nov 25, 2015 12:44 PM, "Francois Dion" wrote: > > if you > have any choice at all, avoid PDF at all cost to get data. > Agreed and IIRC all of that data should be in xml somewhere (look for their rpc pages). Probably start by searching for similar table names (and Google dorking their site for appropriate APIs and/or look through the code of w/e tables you find). That's simpler than dealing with pdf. Might also try emailing them and asking where the data came from (keeping in mind thanksgiving is a federal holiday in the States so you won't get a reply until Monday earliest). OTOH, they can just tell you to go away since pdf is "open" - YMMV. From nanney.56 at gmail.com Wed Nov 25 17:41:59 2015 From: nanney.56 at gmail.com (Robert Nanney) Date: Wed, 25 Nov 2015 16:41:59 -0600 Subject: [Tutor] responding to command line In-Reply-To: <1617708.bsv6H40eDL@desk8.phess.net> References: <1636122.vvXsxp1epV@desk8.phess.net> <1617708.bsv6H40eDL@desk8.phess.net> Message-ID: I like to use paramiko for these types of things. Of course I don't have all the details but from the info you have provided it seems like you should be able to do this with ansible itself. Regards, Robert From sunil.techspk at gmail.com Fri Nov 27 02:14:28 2015 From: sunil.techspk at gmail.com (Sunil Tech) Date: Fri, 27 Nov 2015 12:44:28 +0530 Subject: [Tutor] super and __init__ methods Message-ID: class Adam(object): """docstring for Adam""" def __init__(self, name): self.name = name class Cain(Adam): """docstring for Cain""" def __init__(self, age, *args): super(Cain, self).__init__(age, *args) self.age = age a = Adam('Eve') c = Cain(12) print a.name, c.age, c.name >>> Eve 12 12 May i know why c.name is 12? I am expecting Eve. Help me to understand. Thanks, Sunil. G From sunil.techspk at gmail.com Fri Nov 27 02:39:03 2015 From: sunil.techspk at gmail.com (Sunil Tech) Date: Fri, 27 Nov 2015 13:09:03 +0530 Subject: [Tutor] super and __init__ methods In-Reply-To: References: Message-ID: Thanks I got it. class Cain(Adam): """docstring for Cain""" def __init__(self, age, *args): super(Cain, self).__init__(*args) self.age = age a = Adam('Eve') c = Cain(12, 'Eve') print a.name, c.age, c.name >>> Eve 12 Eve On Fri, Nov 27, 2015 at 12:44 PM, Sunil Tech wrote: > class Adam(object): > """docstring for Adam""" > def __init__(self, name): > self.name = name > > > class Cain(Adam): > """docstring for Cain""" > def __init__(self, age, *args): > super(Cain, self).__init__(age, *args) > self.age = age > > > a = Adam('Eve') > c = Cain(12) > print a.name, c.age, c.name > >>> Eve 12 12 > > May i know why c.name is 12? > I am expecting Eve. > > Help me to understand. > > > Thanks, > Sunil. G > From kayla.phelps at wartburg.edu Thu Nov 26 20:43:36 2015 From: kayla.phelps at wartburg.edu (Kayla Phelps) Date: Fri, 27 Nov 2015 01:43:36 +0000 Subject: [Tutor] underscores Message-ID: <61C30924FF42604D9428740F9CA040ED46BF6A@MSEXCHMB02.wartburg.edu> Hi, When I go to type underscores in my program it doesn't work, they just turn into spaces. I can use references from other programs I downloaded just fine but I even tried copying and pasting underscores and they disappeared. Any ideas of what to do? Even my teacher doesn't know what's wrong. Thanks, Kayla From ben+python at benfinney.id.au Fri Nov 27 04:03:55 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 27 Nov 2015 20:03:55 +1100 Subject: [Tutor] super and __init__ methods References: Message-ID: <85two795l0.fsf@benfinney.id.au> Sunil Tech writes: > Thanks I got it. Thanks for telling us! You should know that ?super? is a topic that confuses even quite experienced Python programmers. It is good to encounter this early, when you can learn about it. See the article ?Python?s super() considered super!? by Raymond Hettinger, for a good guide to this interesting feature. -- \ ?A free press is one where it's okay to state the conclusion | `\ you're led to by the evidence.? ?Bill Moyers | _o__) | Ben Finney From alan.gauld at btinternet.com Fri Nov 27 04:04:22 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 27 Nov 2015 09:04:22 +0000 Subject: [Tutor] underscores In-Reply-To: <61C30924FF42604D9428740F9CA040ED46BF6A@MSEXCHMB02.wartburg.edu> References: <61C30924FF42604D9428740F9CA040ED46BF6A@MSEXCHMB02.wartburg.edu> Message-ID: On 27/11/15 01:43, Kayla Phelps wrote: > Hi, > When I go to type underscores in my program it doesn't work, they just turn into spaces. Ok, this doesn't really have anything to do with Python itself, it depends on what you are using to type. Tell us : What OS you are using? What program are you using to do the typing? What version of Python? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From lac at openend.se Fri Nov 27 04:11:58 2015 From: lac at openend.se (Laura Creighton) Date: Fri, 27 Nov 2015 10:11:58 +0100 Subject: [Tutor] underscores In-Reply-To: <61C30924FF42604D9428740F9CA040ED46BF6A@MSEXCHMB02.wartburg.edu> References: <61C30924FF42604D9428740F9CA040ED46BF6A@MSEXCHMB02.wartburg.edu> Message-ID: <201511270911.tAR9BxE8024363@fido.openend.se> In a message of Fri, 27 Nov 2015 01:43:36 +0000, Kayla Phelps writes: >Hi, >When I go to type underscores in my program it doesn't work, they just turn into spaces. I can use references from other programs I downloaded just fine but I even tried copying and pasting underscores and they disappeared. Any ideas of what to do? Even my teacher doesn't know what's wrong. >Thanks, >Kayla You are on a Mac, correct? https://bugs.python.org/issue24310 On some operating systems, for instance a Macbook Pro with Retina, the bottoms of hanging letters such as 'g' or 'y', as well as underscorces, cannot be seen in IDLE. The fix is to go to Options -> Configure IDLE, and change the size of the default font to 9 or 11. See if that works. Laura From mokshavivek at gmail.com Fri Nov 27 06:35:21 2015 From: mokshavivek at gmail.com (Br. Sayan) Date: Fri, 27 Nov 2015 17:05:21 +0530 Subject: [Tutor] Read lines opening with a particular set of characters from a file Message-ID: How can we read specific lines from a text files using python. File attached. Suppose we want to read lines starting with 'R|1|^^^' and 'O|1|' Should we use: line.startswith(("..", "..", "----")) -------------- next part -------------- H|\^&|||C311^1|||||host|RSUPL^BATCH|P|1 P|1 O|1|9060^MANWAL ^1^^010|R1|^^^989/\^^^990/\^^^734/\^^^693/\^^^435/\^^^413/\^^^418/\^^^685/\^^^59/\^^^698/\^^^683/\^^^798/\^^^717/\^^^678/\^^^712/\^^^690/\^^^781/|R||||||N||^^||SC|||bmserv| ^ ^ ^ ^ |||20151126133212|||F R|1|^^^989/|141|mmol/l||N||F|||||ISE11 C|1|I|0|I R|2|^^^990/|4.46|mmol/l||N||F|||||ISE11 C|1|I|0|I R|3|^^^734/|0.2|mg/dl||N||F|||||P1 C|1|I|0|I R|4|^^^693/|1.77|mg/dl||N||F|||||P1 C|1|I|0|I R|5|^^^435/|55.2|mg/dl||N||F|||||P1 C|1|I|0|I R|6|^^^413/|4.65|g/dl||N||F|||||P1 C|1|I|0|I R|7|^^^418/|26.2|mg/dl||N||F|||||P1 C|1|I|0|I R|8|^^^685/|77.4|U/l||N||F|||||P1 C|1|I|0|I R|9|^^^59/|115.0|mg/dl||N||F|||||P1 C|1|I|0|I R|10|^^^698/|9.5|mg/dl||N||F|||||P1 C|1|I|0|I R|11|^^^683/|72|U/l||N||F|||||P1 C|1|I|0|I R|12|^^^798/|190.4|mg/dl||N||F|||||P1 C|1|I|0|I R|13|^^^717/|110.8|mg/dl||N||F|||||P1 C|1|I|0|I R|14|^^^678/|7.37|g/dl||N||F|||||P1 C|1|I|0|I R|15|^^^712/|0.73|mg/dl||N||F|||||P1 C|1|I|0|I R|16|^^^690/|0.73|mg/dl||N||F|||||P1 C|1|I|0|I R|17|^^^781/|169.7|mg/dl||N||F|||||P1 C|1|I|0|I L|1|N From lac at openend.se Fri Nov 27 12:07:49 2015 From: lac at openend.se (Laura Creighton) Date: Fri, 27 Nov 2015 18:07:49 +0100 Subject: [Tutor] Read lines opening with a particular set of characters from a file In-Reply-To: References: Message-ID: <201511271707.tARH7nEO032741@fido.openend.se> In a message of Fri, 27 Nov 2015 17:05:21 +0530, "Br. Sayan" writes: >How can we read specific lines from a text files using python. File >attached. > >Suppose we want to read lines starting with 'R|1|^^^' and 'O|1|' > >Should we use: > >line.startswith(("..", "..", "----")) that matches lines that start with '..' or '..' or '----' so whatever else is going on, there is no need to repeat. If that is what is literally found in the file then line.startswith(('R|1|^^^','O|1|')) ought to match them. But if that syntax means something else, i.e an R, followed by a 1, followed by any 4 characters, then you will have to explain what the syntax means. Laura From marcus.luetolf at bluewin.ch Fri Nov 27 15:50:50 2015 From: marcus.luetolf at bluewin.ch (=?utf-8?Q?marcus_l=C3=BCtolf?=) Date: Fri, 27 Nov 2015 21:50:50 +0100 Subject: [Tutor] how to invert tuples, one problem more Message-ID: <492001d12955$4fc3dc50$ef4b94f0$@bluewin.ch> dear pythonistas, thanks for your very valuable help. I am struggling with a simple piece of code: >>> x = Marcus >>> print '{0} {1} x'.format('a', '=') which gives me >>> a = x instead of >>> a = Marcus What's wrong ? Marcus. --- Diese E-Mail wurde von Avast Antivirus-Software auf Viren gepr?ft. https://www.avast.com/antivirus From alan.gauld at btinternet.com Fri Nov 27 20:42:20 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 28 Nov 2015 01:42:20 +0000 Subject: [Tutor] how to invert tuples, one problem more In-Reply-To: <492001d12955$4fc3dc50$ef4b94f0$@bluewin.ch> References: <492001d12955$4fc3dc50$ef4b94f0$@bluewin.ch> Message-ID: On 27/11/15 20:50, marcus l?tolf wrote: >>>> x = Marcus Here you assign the variable x to the same thing as the variable Marcus. But that should be an error since Marcus is not defined. So I'm guessing you actually wrote: >>> x = 'Marcus' [ It's always better to copy real code into messages rather than retype it. ] >>>> print '{0} {1} x'.format('a', '=') Here you define a string '{0} {1} x' The {} sections are place holders, all other characters are just that - characters to be printed. The x is no different to the spaces. Then you apply the format method with the arguments 'a' and '=' which are just literal characters that get substituted for their corresponding {} markers in the original string. Again nothing here refers to variable x or to the string 'Marcus'. If you want the value of variable x to be printed you need another format marker. However, since the first two format markers are being replaced by literal characters they are pointless so what you really wanted was: >>> print 'a = {0}'.format(x) Notice that the x has no quotes around it so is a variable name that refers to the value 'Marcus' (Assuming my guess about line 1 was correct!) You need to clarify in your head the difference between strings (surrounded by quotes) and variable names (with no quotes and assigned values). -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From lac at openend.se Sat Nov 28 06:43:25 2015 From: lac at openend.se (Laura Creighton) Date: Sat, 28 Nov 2015 12:43:25 +0100 Subject: [Tutor] Read lines opening with a particular set of characters from a file In-Reply-To: References: <201511271707.tARH7nEO032741@fido.openend.se> Message-ID: <201511281143.tASBhPGs017368@fido.openend.se> In a message of Sat, 28 Nov 2015 16:52:07 +0530, "Br. Sayan" writes: >I am doing the following : > >with open('Manwal.txt') as infile, open('Manwal_req.txt','w') as outfile: > for line in infile: > if line.startswith(('R')): > outfile.write(line) > >It is executing without error but returns a blank file. Where is the >problem? Just try to print the lines, instead of writing them to the file. If nothing gets printed then your infile name is wrong, or you aren't running in the directory where that file is, or, the lines don't really start with R after all. If things print, then you aren't creating a new outfile properly, possibly a permissions issue. Laura From contactjamesoren at gmail.com Sat Nov 28 01:04:23 2015 From: contactjamesoren at gmail.com (James Oren) Date: Sat, 28 Nov 2015 17:04:23 +1100 Subject: [Tutor] Help with returning a list object from a C extension. Message-ID: Hi all, this is my first time using the mailing list. I'm trying to learn how to use C to extend my code, and have already worked out how to use ctypes. I'm now trying to learn the full C extension module approach with Python.h and have worked through the Python doc and a couple other examples. They all use examples where the C function expects a primitive, so now I'd like to learn how to use a Python list object. I guess that a C-based numpy array may be easier to write for, but just for now I'd like to stick to lists. So far I've worked out to use the parse tuple command with the "O" argument and I think thanks to an archived post on this mailing list I've worked out how to get the Python list data into a C array and pass it to the function to modify, now I just need to return a list object of the updated C array data. I'd like it to be that calling this function from Python has the same effect as modifying the list in-place, so I guess I need to either empty the original and repopulate it with the c array data, or build a new list object with the data and reassign it to the original list name .... Which seems like it'd be troublesome? Unfortunately I've hit a brick wall and can't find any examples :-( Any help would be greatly appreciated! Jim From mokshavivek at gmail.com Sat Nov 28 06:22:07 2015 From: mokshavivek at gmail.com (Br. Sayan) Date: Sat, 28 Nov 2015 16:52:07 +0530 Subject: [Tutor] Read lines opening with a particular set of characters from a file In-Reply-To: <201511271707.tARH7nEO032741@fido.openend.se> References: <201511271707.tARH7nEO032741@fido.openend.se> Message-ID: I am doing the following : with open('Manwal.txt') as infile, open('Manwal_req.txt','w') as outfile: for line in infile: if line.startswith(('R')): outfile.write(line) It is executing without error but returns a blank file. Where is the problem? On 27 November 2015 at 22:37, Laura Creighton wrote: > In a message of Fri, 27 Nov 2015 17:05:21 +0530, "Br. Sayan" writes: > >How can we read specific lines from a text files using python. File > >attached. > > > >Suppose we want to read lines starting with 'R|1|^^^' and 'O|1|' > > > >Should we use: > > > >line.startswith(("..", "..", "----")) > > that matches lines that start with '..' or '..' or '----' > so whatever else is going on, there is no need to repeat. > > If that is what is literally found in the file then > line.startswith(('R|1|^^^','O|1|')) > ought to match them. But if that syntax means something else, i.e > an R, followed by a 1, followed by any 4 characters, then you will > have to explain what the syntax means. > > Laura > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at btinternet.com Sat Nov 28 07:14:31 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 28 Nov 2015 12:14:31 +0000 Subject: [Tutor] Read lines opening with a particular set of characters from a file In-Reply-To: References: <201511271707.tARH7nEO032741@fido.openend.se> Message-ID: On 28/11/15 11:22, Br. Sayan wrote: > I am doing the following : > > with open('Manwal.txt') as infile, open('Manwal_req.txt','w') as outfile: > for line in infile: > if line.startswith(('R')): > outfile.write(line) > > It is executing without error but returns a blank file. Where is the > problem? Are you sure your line actually has multiple lines? The sample you posted last time showed up on my system as one very long line. I think it had ^M line breaks in it but my OS didn't recognise them. So if the file was generated on a different computer/OS to the one you are using you may need to split the file before processing. Just a thought. Try printing the first 5 characters of each line line (line[:5]) and see if you get what you expect. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Sat Nov 28 07:22:12 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 28 Nov 2015 12:22:12 +0000 Subject: [Tutor] Help with returning a list object from a C extension. In-Reply-To: References: Message-ID: On 28/11/15 06:04, James Oren wrote: > I'm trying to learn how to use C to extend my code, and have already worked > out how to use ctypes. I'm now trying to learn the full C extension module > approach with Python.h and have worked through the Python doc and a couple > other examples. OK, This is probably a bit more advanced than the tutor list scope. If you are doing that level of coding you should probably promote yourself to the main Python list. That having been said I'm sure we have folks here who can answer the question, it's just that there are a lot more of them on the main list. Also I think there is a specific list for writing C extensions, they may be an even better choice. It's the capi-sig (as in C API SIG) https://mail.python.org/mailman/listinfo/capi-sig -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From lac at openend.se Sat Nov 28 07:29:11 2015 From: lac at openend.se (Laura Creighton) Date: Sat, 28 Nov 2015 13:29:11 +0100 Subject: [Tutor] Help with returning a list object from a C extension. In-Reply-To: References: Message-ID: <201511281229.tASCTBGj018006@fido.openend.se> In a message of Sat, 28 Nov 2015 12:22:12 +0000, Alan Gauld writes: >On 28/11/15 06:04, James Oren wrote: > >> I'm trying to learn how to use C to extend my code, and have already worked >> out how to use ctypes. I'm now trying to learn the full C extension module >> approach with Python.h and have worked through the Python doc and a couple >> other examples. > >OK, This is probably a bit more advanced than the tutor list scope. If >you are doing that level of coding you should probably promote yourself >to the main Python list. > >That having been said I'm sure we have folks here who can answer the >question, it's just that there are a lot more of them on the main list. > >Also I think there is a specific list for writing C extensions, they may >be an even better choice. > >It's the capi-sig (as in C API SIG) > >https://mail.python.org/mailman/listinfo/capi-sig > >-- >Alan G Most people I know have abandoned ctypes. They are using cffi instead. http://cffi.readthedocs.org/en/latest/ and they discuss it here https://groups.google.com/forum/#!forum/python-cffi Laura From lac at openend.se Sat Nov 28 07:30:41 2015 From: lac at openend.se (Laura Creighton) Date: Sat, 28 Nov 2015 13:30:41 +0100 Subject: [Tutor] Help with returning a list object from a C extension. In-Reply-To: References: Message-ID: <201511281230.tASCUfIG018037@fido.openend.se> In a message of Sat, 28 Nov 2015 12:22:12 +0000, Alan Gauld writes: >On 28/11/15 06:04, James Oren wrote: > >> I'm trying to learn how to use C to extend my code, and have already worked >> out how to use ctypes. I'm now trying to learn the full C extension module >> approach with Python.h and have worked through the Python doc and a couple >> other examples. > >OK, This is probably a bit more advanced than the tutor list scope. If >you are doing that level of coding you should probably promote yourself >to the main Python list. > >That having been said I'm sure we have folks here who can answer the >question, it's just that there are a lot more of them on the main list. > >Also I think there is a specific list for writing C extensions, they may >be an even better choice. > >It's the capi-sig (as in C API SIG) > >https://mail.python.org/mailman/listinfo/capi-sig Also, have you checked out Cython? Its been a very long time since I needed to write a C extension that I couldn't write in Cython. Laura From alan.gauld at btinternet.com Sat Nov 28 07:46:11 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 28 Nov 2015 12:46:11 +0000 Subject: [Tutor] Help with returning a list object from a C extension. In-Reply-To: <201511281229.tASCTBGj018006@fido.openend.se> References: <201511281229.tASCTBGj018006@fido.openend.se> Message-ID: <5659A213.6090200@btinternet.com> On 28/11/15 12:29, Laura Creighton wrote: > Most people I know have abandoned ctypes. They are using cffi instead. Really? Why? I'd never heard of cffi before, but looking at the docs it seems like a lot more effort than ctypes to call a library function? I can't see any advantage, so what am I missing? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Sat Nov 28 08:40:07 2015 From: __peter__ at web.de (Peter Otten) Date: Sat, 28 Nov 2015 14:40:07 +0100 Subject: [Tutor] Read lines opening with a particular set of characters from a file References: <201511271707.tARH7nEO032741@fido.openend.se> Message-ID: Br. Sayan wrote: > I am doing the following : > > with open('Manwal.txt') as infile, open('Manwal_req.txt','w') as outfile: > for line in infile: > if line.startswith(('R')): > outfile.write(line) > > It is executing without error but returns a blank file. Where is the > problem? Your sample data uses the "\r" character to separate lines. This has gone out of fashion, but Python 3 handles it gracefully. In Python 2 you have to enable "universal newlines" explicitly with with open("Manwal.txt", "U") as infile, ... ... This will recognize "\r", "\r\n", and "\n" as line separators, and translate all of them to "\n". From lac at openend.se Sat Nov 28 10:59:35 2015 From: lac at openend.se (Laura Creighton) Date: Sat, 28 Nov 2015 16:59:35 +0100 Subject: [Tutor] Help with returning a list object from a C extension. In-Reply-To: <5659A213.6090200@btinternet.com> References: <201511281229.tASCTBGj018006@fido.openend.se> <5659A213.6090200@btinternet.com> Message-ID: <201511281559.tASFxZ1S020459@fido.openend.se> In a message of Sat, 28 Nov 2015 12:46:11 +0000, Alan Gauld writes: >On 28/11/15 12:29, Laura Creighton wrote: >> Most people I know have abandoned ctypes. They are using cffi instead. > >Really? Why? >I'd never heard of cffi before, but looking at the docs it seems like a >lot more >effort than ctypes to call a library function? I can't see any advantage, >so what am I missing? > >-- >Alan G Ctypes is slow. It's fine to get simple things running, but if the object of the exercise was to get speed in the first place you are better off with Cython, if you are writing the thing yourself, or cffi if you are wrapping an existing C library. Laura From alan.gauld at btinternet.com Sat Nov 28 13:17:41 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 28 Nov 2015 18:17:41 +0000 Subject: [Tutor] Help with returning a list object from a C extension. In-Reply-To: <201511281559.tASFxZ1S020459@fido.openend.se> References: <201511281229.tASCTBGj018006@fido.openend.se> <5659A213.6090200@btinternet.com> <201511281559.tASFxZ1S020459@fido.openend.se> Message-ID: On 28/11/15 15:59, Laura Creighton wrote: >> effort than ctypes to call a library function? I can't see any advantage, >> so what am I missing? > > Ctypes is slow. Aha. Performance is always the invisible feature. I only ever use ctypes to access OS features that don't have a Python equivalent function (and that's rare). Performance has never been an issue so ctypes has been just grand. Cython is a whole different beastie though, it's way cool! :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From oscar.j.benjamin at gmail.com Sat Nov 28 13:43:56 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sat, 28 Nov 2015 18:43:56 +0000 Subject: [Tutor] Help with returning a list object from a C extension. In-Reply-To: References: Message-ID: On 28 Nov 2015 12:06, "James Oren" wrote: > > Hi all, this is my first time using the mailing list. > > I'm trying to learn how to use C to extend my code, and have already worked > out how to use ctypes. I'm now trying to learn the full C extension module > approach with Python.h and have worked through the Python doc and a couple > other examples. They all use examples where the C function expects a > primitive, so now I'd like to learn how to use a Python list object. I > guess that a C-based numpy array may be easier to write for, but just for > now I'd like to stick to lists. > > So far I've worked out to use the parse tuple command with the "O" argument > and I think thanks to an archived post on this mailing list I've worked out > how to get the Python list data into a C array and pass it to the function > to modify, now I just need to return a list object of the updated C array > data. > > I'd like it to be that calling this function from Python has the same > effect as modifying the list in-place, so I guess I need to either empty > the original and repopulate it with the c array data, or build a new list > object with the data and reassign it to the original list name .... Which > seems like it'd be troublesome? Unfortunately I've hit a brick wall and > can't find any examples :-( Can you post an example of a function written in Python that does what you want? Does PyObject_SetItem do what you want: https://docs.python.org/3.5/c-api/object.html -- Oscar From mokshavivek at gmail.com Sat Nov 28 23:53:29 2015 From: mokshavivek at gmail.com (Br. Sayan) Date: Sun, 29 Nov 2015 10:23:29 +0530 Subject: [Tutor] Read lines opening with a particular set of characters from a file In-Reply-To: References: <201511271707.tARH7nEO032741@fido.openend.se> Message-ID: It is exactly what Peter has said. Opened the file with 'U' and it worked like a charm. Thank you very much! On 28 November 2015 at 19:10, Peter Otten <__peter__ at web.de> wrote: > Br. Sayan wrote: > > > I am doing the following : > > > > with open('Manwal.txt') as infile, open('Manwal_req.txt','w') as outfile: > > for line in infile: > > if line.startswith(('R')): > > outfile.write(line) > > > > It is executing without error but returns a blank file. Where is the > > problem? > > Your sample data uses the "\r" character to separate lines. This has gone > out of fashion, but Python 3 handles it gracefully. > > In Python 2 you have to enable "universal newlines" explicitly with > > with open("Manwal.txt", "U") as infile, ... > ... > > This will recognize "\r", "\r\n", and "\n" as line separators, and > translate > all of them to "\n". > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From emile at fenx.com Mon Nov 30 17:35:29 2015 From: emile at fenx.com (Emile van Sebille) Date: Mon, 30 Nov 2015 14:35:29 -0800 Subject: [Tutor] how to invert tuples, one problem more In-Reply-To: <492001d12955$4fc3dc50$ef4b94f0$@bluewin.ch> References: <492001d12955$4fc3dc50$ef4b94f0$@bluewin.ch> Message-ID: On 11/27/2015 12:50 PM, marcus l?tolf wrote: > dear pythonistas, > thanks for your very valuable help. > I am struggling with a simple piece of code: > >>>> x = Marcus >>>> print '{0} {1} x'.format('a', '=') You're almost there - print '{0} {1} {2}'.format('a', '=', x) Emile > which gives me >>>> a = x instead of >>>> a = Marcus From evanlespaul at gmail.com Mon Nov 30 14:23:43 2015 From: evanlespaul at gmail.com (Evan Sommer) Date: Mon, 30 Nov 2015 14:23:43 -0500 Subject: [Tutor] Countdown Clock Programming Question In-Reply-To: References: Message-ID: Hello again Alan! Do you think you could write a revised code with the modifications that you suggested? I tried changing the code with your recommendations and I keep getting syntax errors. If you could do that, I would greatly appreciate it!! Thank you for all your help! Evan Sommer On Tue, Nov 24, 2015 at 9:01 AM, Evan Sommer wrote: > Hello there! > > I am working on a project for an engineering class that I am in at my high > school, and our task has been to create a clock that counts down the time > between periods, so that the students walking the hallways know how much > time they have to get to class. The timer will be displayed on multiple > monitors throughout the halls. However, the idea behind the countdown clock > is for the background to change colours when it hits a certain time. The > goal is for the clock to change from green to yellow at 2 minutes, and > yellow to red at 1 minute. However, I have been having a hard time trying > to get the color change to display in one window. If you could give me some > advice, I'd really appreciate it! > > Here's the code: > > try: > # Python2 > import Tkinter as tk > except ImportError: > # Python3 > import tkinter as tk > import time > def count_down(): > # start with 4 minutes --> 240 seconds > for t in range(240, 120, -1): > # format as 2 digit integers, fills with zero to the left > # divmod() gives minutes, seconds > sf = "{:01d}:{:02d}".format(*divmod(t, 60)) > #print(sf) # test > time_str.set(sf) > root.update() > # delay one second > time.sleep(1)# create root/main window > root = tk.Tk() > time_str = tk.StringVar() > # create the time display label, give it a large font > # label auto-adjusts to the font > label_font = ('helvetica', 100) > tk.Label(root, textvariable=time_str, font=label_font, bg='green', > fg='white', relief='raised', bd=3).pack(fill='x', padx=5, pady=5) > # start with 2 minutes --> 119 seconds > for t in range(240, 120, -1): > # format as 2 digit integers, fills with zero to the left > # divmod() gives minutes, seconds > sf = "{:01d}:{:02d}".format(*divmod(t, 60)) > #print(sf) # test > time_str.set(sf) > root.update() > # delay one second > time.sleep(1) > # create the time display label, give it a large font > # label auto-adjusts to the font > label_font = ('helvetica', 100) > tk.Label(root, textvariable=time_str, font=label_font, bg='yellow', > fg='white', relief='raised', bd=3).pack(fill='x', padx=5, pady=5) > # start with 1 minutes --> 59 seconds > for t in range(120,60, -1): > # format as 2 digit integers, fills with zero to the left > # divmod() gives minutes, seconds > sf = "{:01d}:{:02d}".format(*divmod(t, 60)) > #print(sf) # test > time_str.set(sf) > root.update() > # delay one second > time.sleep(1) > # create the time display label, give it a large font > # label auto-adjusts to the font > label_font = ('helvetica', 100) > tk.Label(root, textvariable=time_str, font=label_font, bg='red', > fg='white', relief='raised', bd=3).pack(fill='x', padx=5, pady=5) > # start with 4 minutes --> 240 seconds > for t in range(60,-1, -1): > # format as 2 digit integers, fills with zero to the left > # divmod() gives minutes, seconds > sf = "{:01d}:{:02d}".format(*divmod(t, 60)) > #print(sf) # test > time_str.set(sf) > root.update() > # delay one second > time.sleep(1) > # start the GUI event loop > root.mainloop() > > Thanks for the help! > > Sincerely, > > Evan Sommer > From tsmithers731 at gmail.com Mon Nov 30 16:50:35 2015 From: tsmithers731 at gmail.com (Tyler Smithers) Date: Mon, 30 Nov 2015 16:50:35 -0500 Subject: [Tutor] Python 3.5 console logging Message-ID: I am doing a project for my school and i am trying to find out how to make a event log. But every where i look all it has is just making a text document and it having in their what they put in their script. But i cant find out how to code python into recording everything that happens when i run the program. And can you please help me soon because i don't have much time. Thank you!