From dyoo at hashcollision.org Wed Jul 1 01:27:27 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 30 Jun 2015 16:27:27 -0700 Subject: [Tutor] memory error In-Reply-To: References: Message-ID: On Tue, Jun 30, 2015 at 8:10 AM, Joshua Valdez wrote: > So I wrote this script to go over a large wiki XML dump and pull out the > pages I want. However, every time I run it the kernel displays 'Killed' I'm > assuming this is a memory issue after reading around but I'm not sure where > the memory problem is in my script and if there were any tricks to reduce > the virtual memory usage. Yes. Unfortunately, this is a common problem with representing a potentially large stream of data with a single XML document. The straightforward approach to load an XML, to read it all into memory at once, doesn't work when files get large. We can work around this by using a parser that knows how to progressively read chunks of the document in a streaming or "pulling" approach. Although I don't think Beautiful Soup knows how to do this, however, if you're working with XML, there are other libraries that work similarly to Beautiful Soup that can work in a streaming way. There was a thread about this about a year ago that has good references, the "XML Parsing from XML" thread: https://mail.python.org/pipermail/tutor/2014-May/101227.html Stefan Behnel's contribution to that thread is probably the most helpful in seeing example code: https://mail.python.org/pipermail/tutor/2014-May/101270.html I think you'll probably want to use xml.etree.cElementTree; I expect the code for your situation will look something like (untested though!): ############################### from xml.etree.cElementTree import iterparse, tostring ## ... later in your code, something like this... doc = iterparse(wiki) for _, node in doc: if node.tag == "page": title = node.find("title").text if title in page_titles: print tostring(node) node.clear() ############################### Also, don't use "del" unless you know what you're doing. It's not particularly helpful in your particular scenario, and it is cluttering up the code. Let us know if this works out or if you're having difficulty, and I'm sure folks would be happy to help out. Good luck! From alan.gauld at btinternet.com Wed Jul 1 01:49:19 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 01 Jul 2015 00:49:19 +0100 Subject: [Tutor] __repr__ and __str__ In-Reply-To: <85fv59rkxw.fsf@benfinney.id.au> References: <58064.50.168.22.182.1435646865.squirrel@mail.tigertech.net> <85fv59rkxw.fsf@benfinney.id.au> Message-ID: On 30/06/15 21:38, Ben Finney wrote: >> def __str__(self): >> return """Here are your data: >> %s >> """ % list.__str__(self) > > Python allows any class to participate in multiple inheritance, and > that's not determined at the point of writing your class. So you > can never assume you know what class is next in the resolution order; > that's what ?super? is for:: Whilst I agree with the general use of super I'm not sure what the MRO resolution has to do with this case? It's explicitly single inheritance and they are explicitly calling the superclass. There should be no MRO lookup at this class level (there may be at the list level of course). super makes sense (sometimes) in a multiple inheritance setup. It makes less sense in a single inheritance situation and no sense at all in any setup where you want to call a specific superclass rather than rely on a generic walk up the inheritance tree. eg. class A(B,C,D): def m(self): C.m(self) # call C's m but not B and D if they have them. Or am I missing something in the way Python evaluates the C.m() call above? > """ % super().__str__() > > (I am assuming this is Python 3 code, Nope. The OP explicitly said 2.7. > since Python 2 is legacy-only and should not be used for teaching today. That's a fine aspiration but pretty much impractical in the real world. There are many reasons why folks need to use 2.7 (or even older!) [ Just look at the stuff Bob Stepp is doing where he is stuck on 2.4. ] At my last job they wouldn't install Python 3 (although that was 2 years ago, it may have moved on now!) and we didn't move off 1.5 to v2 until ~5 years after v2 was released. > In other words, since you're defining ?__repr__?, it is ?__repr__? you > should be calling on the superclass:: That depends on what representation they want in the repr output, it may be more similar to the superclass str() than its repr(). There should be no *necessity* to call the "same" method in the superclasss, even though most of the time that is what you do. > are named with a double leading and trailing underscore), it is best > only to extend the same method on the superclass, and not complicate the > connextions back and forth. I do agree as a principle but sometimes the principle doesn't yield the right result. But when you bend the 'rules' you do have to expect to get bitten occasionally. -- 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 Jul 1 01:54:08 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 01 Jul 2015 00:54:08 +0100 Subject: [Tutor] memory error In-Reply-To: References: Message-ID: On 30/06/15 16:10, Joshua Valdez wrote: > So I wrote this script to go over a large wiki XML dump and pull out the > pages I want. However, every time I run it the kernel displays 'Killed' I'm > assuming this is a memory issue after reading around but I'm not sure where > the memory problem is in my script That's quite a big assumption. How big is the wiki file? How much RAM do you have? What do your system resource monitoring tools (eg top) say? > and if there were any tricks to reduce > the virtual memory usage. Of course, but as always be sure what you are tweaking before you start. Otherwise you can waste a lot of time doing nothing useful. > from bs4 import BeautifulSoup > import sys > > pages_file = open('pages_file.txt', 'r') > .... > > ##################################### > > with open(sys.argv[1], 'r') as wiki: > soup = BeautifulSoup(wiki) > wiki.closed Is that really what you mean? Or should it be wiki.close()? > wiki_page = soup.find_all("page") > del soup > for item in wiki_page: > title = item.title.get_text() > if title in page_titles: > print item > del title -- 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 Jul 1 03:20:38 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 30 Jun 2015 18:20:38 -0700 Subject: [Tutor] memory error In-Reply-To: References: Message-ID: Please use reply to all: I'm currently not in front of a keyboard at the moment. Others on the mailing list should be able to help. On Jun 30, 2015 6:13 PM, "Joshua Valdez" wrote: > Hi Danny, > > So I tried that code snippet you pointed me too and I'm not getting any > output. > > I tried playing around with the code and when I tried > > doc = etree.iterparse(wiki) > for _, node in doc: > print node > > I get output like: > > 0x100602410> > 0x1006024d0> > > 0x100602710> > > 0x1006027d0> > 0x100602810> > 0x100602850> > 0x100602890> > 0x1006028d0> > > so the .tag function is capturing everything in the string. Do you know > why this and how I can get around it? > > > > > > > > *Joshua Valdez* > *Computational Linguist : Cognitive Scientist > * > > (440)-231-0479 > jdv12 at case.edu | jdv2 at uw.edu | joshv at armsandanchors.com > > > On Tue, Jun 30, 2015 at 7:27 PM, Danny Yoo wrote: > >> On Tue, Jun 30, 2015 at 8:10 AM, Joshua Valdez wrote: >> > So I wrote this script to go over a large wiki XML dump and pull out the >> > pages I want. However, every time I run it the kernel displays 'Killed' >> I'm >> > assuming this is a memory issue after reading around but I'm not sure >> where >> > the memory problem is in my script and if there were any tricks to >> reduce >> > the virtual memory usage. >> >> Yes. Unfortunately, this is a common problem with representing a >> potentially large stream of data with a single XML document. The >> straightforward approach to load an XML, to read it all into memory at >> once, doesn't work when files get large. >> >> We can work around this by using a parser that knows how to >> progressively read chunks of the document in a streaming or "pulling" >> approach. Although I don't think Beautiful Soup knows how to do this, >> however, if you're working with XML, there are other libraries that >> work similarly to Beautiful Soup that can work in a streaming way. >> >> There was a thread about this about a year ago that has good >> references, the "XML Parsing from XML" thread: >> >> https://mail.python.org/pipermail/tutor/2014-May/101227.html >> >> Stefan Behnel's contribution to that thread is probably the most >> helpful in seeing example code: >> >> https://mail.python.org/pipermail/tutor/2014-May/101270.html >> >> I think you'll probably want to use xml.etree.cElementTree; I expect >> the code for your situation will look something like (untested >> though!): >> >> ############################### >> from xml.etree.cElementTree import iterparse, tostring >> >> ## ... later in your code, something like this... >> >> doc = iterparse(wiki) >> for _, node in doc: >> if node.tag == "page": >> title = node.find("title").text >> if title in page_titles: >> print tostring(node) >> node.clear() >> ############################### >> >> >> Also, don't use "del" unless you know what you're doing. It's not >> particularly helpful in your particular scenario, and it is cluttering >> up the code. >> >> >> Let us know if this works out or if you're having difficulty, and I'm >> sure folks would be happy to help out. >> >> >> Good luck! >> > > From ben+python at benfinney.id.au Wed Jul 1 04:48:47 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 01 Jul 2015 12:48:47 +1000 Subject: [Tutor] =?utf-8?q?Inheritance=2C_superclass=2C_=E2=80=98super?= =?utf-8?b?4oCZICh3YXM6IF9fcmVwcl9fIGFuZCBfX3N0cl9fKQ==?= References: <58064.50.168.22.182.1435646865.squirrel@mail.tigertech.net> <85fv59rkxw.fsf@benfinney.id.au> Message-ID: <85bnfwsicw.fsf_-_@benfinney.id.au> Alan Gauld writes: > Whilst I agree with the general use of super I'm not sure what > the MRO resolution has to do with this case? When accessing the superclass, the MRO is always relevant. > It's explicitly single inheritance and they are explicitly calling the > superclass. They don't know that the superclass will be what is written there; that's an implication of what I said with ?in Python, any class an participate in multiple inheritance?. The author of a class (e.g. the OP's example ?MyList?) cannot know at time of writing whether the superclass is ?list?. This is because some other class may be using ?MyList? in a different inheritance arrangement. Even without either party (the author of ?MyList?, or the author of the other class) intending it to be so, the MRO can run through ?MyList? in a direction not specified by either author. Multiple inheitance is a fact in Python, and good practice is to not arbitrarily write classes that break it. Hence my advice to avoid hard-coding the superclass, and only use ?super? to discover the superclass. > There should be no MRO lookup at this class level (there may be at the > list level of course). Much as the class author might like it to be so simple, in Python it just isn't so: any other class using multiple inheitance may pull in this one, either as a parent or in some other indirect relationship. Thus the above assertion is violated. In Python code it can't be said ?there should be not MRO lookup at this class level?. It's not for the class author to say whether some other class ?should not? inherit, directly or indirectly, from this and some other class(es). > super makes sense (sometimes) in a multiple inheritance setup. Yes, and the class author can't know at time of writing whether that's the case. So it's prudent to never hard-code the superclass, and to only use ?super? to dynamically determine at run time what is the superclass. > eg. > > class A(B,C,D): > def m(self): > C.m(self) # call C's m but not B and D if they have them. > > Or am I missing something in the way Python evaluates > the C.m() call above? My understanding comes from (among other places) the Python docs:: [?] dynamic ordering is necessary because all cases of multiple inheritance exhibit one or more diamond relationships (where at least one of the parent classes can be accessed through multiple paths from the bottommost class). For example, all new-style classes inherit from `object`, so any case of multiple inheritance provides more than one path to reach `object`. [?] The paths are determined at run time, and can link through the class one is writing whether on intends that or not. Any class one writes can become part of some other class's multiple-inheritance chain, and it is bad practice to assume one knows at time of writing what the superclass will be at run time. We have discused this multiple times in the past. Here is a thread from 2011, explaining why, even one isn't using multiple base classes explicitly, one should always only ever use ?super? to determine at run-time what the superclass is: All the smarts managing the entire inheritance hierarchy is built into `super`, so each method gets called once and exactly once, provided that every class *only* uses `super`. If you try to mix `super` calls and direct method calls like B.method(self, arg), it probably won't work correctly, and if it does, it will probably be by accident. So please use `super`, even in single inheritance. Otherwise you are restricting the usefulness of your class: it can never be used with multiple inheritance. Multiple inheritance is a counter-intuitive topic, so it's not really surprising there is still confusion about it. I try to nip the problem before it takes hold, by widely advocating use of ?super? to aid everyone else's Python code. Hope that helps! -- \ ?I took it easy today. I just pretty much layed around in my | `\ underwear all day. ? Got kicked out of quite a few places, | _o__) though.? ?Bug-Eyed Earl, _Red Meat_ | Ben Finney From dyoo at hashcollision.org Wed Jul 1 07:17:27 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 30 Jun 2015 22:17:27 -0700 Subject: [Tutor] memory error In-Reply-To: References: Message-ID: Hi Joshua, The issue you're encountering sounds like XML namespace issues. >> So I tried that code snippet you pointed me too and I'm not getting any output. This is probably because the tag names of the XML are being prefixed with namespaces. This would make the original test for node.tag to be too stingy: it wouldn't exactly match the string we want, because there's a namespace prefix in front that's making the string mismatch. Try relaxing the condition from: if node.tag == "page": ... to something like: if node.tag.endswith("page"): ... This isn't quite technically correct, but we want to confirm whether namespaces are the issue that's preventing you from seeing those pages. If namespaces are the issue, then read: http://effbot.org/zone/element-namespaces.htm From meghagarg1565 at gmail.com Wed Jul 1 08:11:22 2015 From: meghagarg1565 at gmail.com (megha garg) Date: Wed, 1 Jul 2015 11:41:22 +0530 Subject: [Tutor] Problem in packages Message-ID: Python version 2.7.10. I have problem in installing .whl format packages. what version of setuptools is required for .whl format packages. I have installed Setuptools version 0.6.0 and upgraded pip to 7.0.3. wndows 7 powershell 1. I tried installing setuptools 18.0 but it is also in .whl format. and installing wheel requires greater than 0.8 versions. so how can i install setuptools and wheel to install .whl format packages. please reply as soon as possible. thanks From oscar.j.benjamin at gmail.com Wed Jul 1 14:08:08 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 1 Jul 2015 13:08:08 +0100 Subject: [Tutor] Problem in packages In-Reply-To: References: Message-ID: On 1 July 2015 at 07:11, megha garg wrote: > Python version 2.7.10. > I have problem in installing .whl format packages. > what version of setuptools is required for .whl format packages. > I have installed Setuptools version 0.6.0 and upgraded pip to 7.0.3. > wndows 7 powershell 1. > I tried installing setuptools 18.0 but it is also in .whl format. > and installing wheel requires greater than 0.8 versions. > so how can i install setuptools and wheel to install .whl format packages. > please reply as soon as possible. > thanks If you have pip can you run pip from the command line? If so you should be able to update setuptools with: pip install -U setuptools It's hard to provide better help without more information: What operating system are you using (Windows?). How did you install Python or which Python distribution did you install? (Can you a post a link to where you got it from?) Did you install for all users or just you? -- Oscar From Steve.Flynn at capita.co.uk Wed Jul 1 13:54:23 2015 From: Steve.Flynn at capita.co.uk (Flynn, Stephen (L & P - IT)) Date: Wed, 1 Jul 2015 12:54:23 +0100 Subject: [Tutor] Problem in packages In-Reply-To: References: Message-ID: <5C71288A643C6249B50BC6B27CB3A14C01CCA68C@CAPPRWMMBX23.central.ad.capita.co.uk> > Python version 2.7.10. > I have problem in installing .whl format packages. > what version of setuptools is required for .whl format packages. > I have installed Setuptools version 0.6.0 and upgraded pip to 7.0.3. > wndows 7 powershell 1. > I tried installing setuptools 18.0 but it is also in .whl format. > and installing wheel requires greater than 0.8 versions. > so how can i install setuptools and wheel to install .whl format packages. > please reply as soon as possible. http://stackoverflow.com/questions/27885397/how-do-i-install-a-python-pa ckage-with-a-whl-file This email is security checked and subject to the disclaimer on web-page: http://www.capita.co.uk/email-disclaimer.aspx From jdv12 at case.edu Wed Jul 1 16:13:08 2015 From: jdv12 at case.edu (Joshua Valdez) Date: Wed, 1 Jul 2015 10:13:08 -0400 Subject: [Tutor] memory error In-Reply-To: References: Message-ID: Hi Danny, So I got my code workin now and it looks like this TAG = '{http://www.mediawiki.org/xml/export-0.10/}page' doc = etree.iterparse(wiki) for _, node in doc: if node.tag == TAG: title = node.find("{http://www.mediawiki.org/xml/export-0.10/}title ").text if title in page_titles: print (etree.tostring(node)) node.clear() Its mostly giving me what I want. However it is adding extra formatting (I believe name_spaces and attributes). I was wondering if there was a way to strip these out when I'm printing the node tostring? Here is an example of the last few lines of my output: [[Category:Asteroids| ]] [[Category:Spaceflight]] h4rxxfq37qg30eqegyf4vfvkqn3r142 *Joshua Valdez* *Computational Linguist : Cognitive Scientist * (440)-231-0479 jdv12 at case.edu | jdv2 at uw.edu | joshv at armsandanchors.com On Wed, Jul 1, 2015 at 1:17 AM, Danny Yoo wrote: > Hi Joshua, > > > > The issue you're encountering sounds like XML namespace issues. > > > >> So I tried that code snippet you pointed me too and I'm not getting any > output. > > > This is probably because the tag names of the XML are being prefixed > with namespaces. This would make the original test for node.tag to be > too stingy: it wouldn't exactly match the string we want, because > there's a namespace prefix in front that's making the string mismatch. > > > Try relaxing the condition from: > > if node.tag == "page": ... > > to something like: > > if node.tag.endswith("page"): ... > > > This isn't quite technically correct, but we want to confirm whether > namespaces are the issue that's preventing you from seeing those > pages. > > > If namespaces are the issue, then read: > > http://effbot.org/zone/element-namespaces.htm > From alan.gauld at btinternet.com Wed Jul 1 23:40:20 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 01 Jul 2015 22:40:20 +0100 Subject: [Tutor] =?utf-8?q?Inheritance=2C_superclass=2C_=E2=80=98super?= =?utf-8?b?4oCZ?= In-Reply-To: <85bnfwsicw.fsf_-_@benfinney.id.au> References: <58064.50.168.22.182.1435646865.squirrel@mail.tigertech.net> <85fv59rkxw.fsf@benfinney.id.au> <85bnfwsicw.fsf_-_@benfinney.id.au> Message-ID: On 01/07/15 03:48, Ben Finney wrote: > Alan Gauld writes: > >> Whilst I agree with the general use of super I'm not sure what >> the MRO resolution has to do with this case? > > When accessing the superclass, the MRO is always relevant Can you explain that? If I access a class explicitly and call a method directly how is the MRO involved in that invocation? (It is involved if the method is actually implemented in a super-superclass of course, I understand that bit.) But how does MRO affect a direct call to a method of a class? >> It's explicitly single inheritance and they are explicitly calling the >> superclass. > > They don't know that the superclass will be what is written there; > that's an implication of what I said with ?in Python, any class an > participate in multiple inheritance?. If MyList is included in a MI lattice that doesn't change the fact that MyList inherits directly from list. Or at least I hope not, that could be catastrophic to the behaviour of the class. The class definition demands that MyList has a single direct superclass (and potentially many super-super-classes). that is the very basis of the Is-A relationship that inheritance indicates. > The author of a class (e.g. the OP's example ?MyList?) cannot know at > time of writing whether the superclass is ?list?. This is because some > other class may be using ?MyList? in a different inheritance > arrangement. That should not change the superclass of MyList. It just means that the new subclass doesn't know where list appears in the execution order of its superclass calls. But it should not change the behaviour of MyList methods. > Even without either party (the author of ?MyList?, or the author of the > other class) intending it to be so, the MRO can run through ?MyList? in > a direction not specified by either author. I'm not sure what you mean by 'direction' there? But I agree neither author can tell where in the sequence of method calls the MyLIst version will be called when the method is invoked on the subclass. If that's what you mean? > Multiple inheitance is a fact in Python, and good practice is to not > arbitrarily write classes that break it. That depends on what you mean by break it., MI should allow the inheriting class to specify which, if any, of its direct superclasses methods are invoked. That's critical to the use of MI in any language The issues around that are addressed differently by different languages, for example Eiffel allows you to rename inherited methods or attributes to avoid them being used involuntarily. Most just allow the programmer to explicitly access the superclasses where default behaviour is not required. That's what I thought Python was doing with super. Making default easy to get but still allowing direct overriding when required. > Much as the class author might like it to be so simple, in Python it > just isn't so: any other class using multiple inheitance may pull in > this one, either as a parent or in some other indirect relationship. > Thus the above assertion is violated. Not so. A single inheritance class, even in an MI scenario must still be able to rely on its own superclass being the one stated. To do otherwise would result in very weird and unpredictable behaviour. > In Python code it can't be said ?there should be not MRO lookup at this > class level?. It's not for the class author to say whether some other > class ?should not? inherit, directly or indirectly, from this and some > other class(es). Yes, but then the MRO is invoked at that subclass level. Its not in the power of the class author to know how the class will be used in future class definitions. But the class *must* always follow the semantics of the original class or the contract of the definition is broken. >> class A(B,C,D): >> def m(self): >> C.m(self) # call C's m but not B and D if they have them. >> >> Or am I missing something in the way Python evaluates >> the C.m() call above? I still want to know how you would code the above scenario using super? If B,C and D all have an m() method but I only want to call the version in C how do I control that in Python other than by a direct call to C.m()? > My understanding comes from (among other places) the Python docs:: > > [?] dynamic ordering is necessary because all cases of multiple > inheritance exhibit one or more diamond relationships (where at > least one of the parent classes can be accessed through multiple > paths from the bottommost class). For example, all new-style classes > inherit from `object`, so any case of multiple inheritance provides > more than one path to reach `object`. [?] Yes I understand that. If you want to call all of the superclass versions of a method you are overriding then super is the only way to go. I'm talking about the case where you have a single superclass method that you want to invoke. As I understand it, MyList can still explicitly call list and, if in an MI scenario, the list method can call super and it will ripple through the MRO. Similarly the MyList method invocation will appear in the normal MRO processing of the subclass. The risk is that list gets called multiple times but that's sometimes desirable and sometimes not. (Mainly an issue where state changes are involved) > The paths are determined at run time, and can link through the class one > is writing whether on intends that or not. The issue is not whether the sub class method invocation goes through MyList or not but whether MyList can control which of its superclasses gets called and, more importantly, which do not. Of course another peer superclass may invoke the methods that MyList doesn't want to use but thats of no concern to MyList. > Any class one writes can > become part of some other class's multiple-inheritance chain, and it is > bad practice to assume one knows at time of writing what the superclass > will be at run time. This is the bit I don't understand. The superclass of a given class should never change. The sequence of default processing may change but any other behavior would be a serious breach of the class definition. > We have discused this multiple times in the past. Here is a thread from > 2011, ... > > All the smarts managing the entire inheritance hierarchy is built > into `super`, so each method gets called once and exactly once, And that is what super does. It does not determine what the superclass is. It arranges the superclass such that a default invocation will excercise all of the superclasses exactly once. But as Steven pointed out there are exceptions (He used the example of differing signatures but there are several others) One of the most common is where multiple superclasses define the same method but with very different semantics. The normal way to disambiguate that is to create multiple methods in the subclass which only invoke the appropriate superclass. A rather contrived example is where I want to inherit a Pen and a Curtain to produce a GraphicalCurtain class. Both Pen and Curtain define a draw() method but they do very different things. So I define two methods: draw() which invokes Pen.draw() and close() which invokes Curtain.draw(). If I use super in either of those methods I'll get the wrong behavior because both Pen.draw and Curtain.draw will be invoked, Unless I'm missing something clever in super? I never quite understood the parameters in super() so it is possible that there is a way to exclude some super classes, but I'm not aware of any such. > Multiple inheritance is a counter-intuitive topic, so it's not really > surprising there is still confusion about it. It depends on your background. I learned OOP using Flavors in the mid 80s and it was all about MI. It was standard practice to define classes with 5, 6 or more superclasses. But it had much more sophisticated tools than python to control the MRO definition on a class by class basis, C++ also allows MI and takes a different approach again but that only works because the compiler can sort it out for you. And Eiffel uses renaming of attributes. There are lots of options to addressing the same problem. > before it takes hold, by widely advocating use of ?super? to aid > everyone else's Python code. And lest there is any confusion, I agree that super should be the default option. I'm only pointing out that a) super does not change the superclass, it simply moves it to a different place in the chain of execution and b) several cases exist where the default MRO sequence is not what you need. In those cases you have to work it out for yourself (or find a way to avoid them if possible!) -- 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 Thu Jul 2 17:33:25 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 3 Jul 2015 01:33:25 +1000 Subject: [Tutor] =?utf-8?q?Inheritance=2C_superclass=2C_=E2=80=98super?= =?utf-8?b?4oCZICh3YXM6IF9fcmVwcl9fIGFuZCBfX3N0cl9fKQ==?= In-Reply-To: <85bnfwsicw.fsf_-_@benfinney.id.au> References: <58064.50.168.22.182.1435646865.squirrel@mail.tigertech.net> <85fv59rkxw.fsf@benfinney.id.au> <85bnfwsicw.fsf_-_@benfinney.id.au> Message-ID: <20150702153323.GH10773@ando.pearwood.info> I mostly agree with what Ben says, comments below. On Wed, Jul 01, 2015 at 12:48:47PM +1000, Ben Finney wrote: > Alan Gauld writes: > > > Whilst I agree with the general use of super I'm not sure what > > the MRO resolution has to do with this case? > > When accessing the superclass, the MRO is always relevant. For those who aren't sure, MRO is "Method Resolution Order". To be precise, all (new style) classes have a MRO: py> object.__mro__ (,) In the multiple-inheritance world, all inheritance is supposed to follow the MRO, at least by default. It should be quite unusual to manually bypass the MRO or invent your own, that should stand out as a code smell. One complication in Python 2 is that "classic classes" (old style classes) don't have an explicit MRO. But they do have an implicit one. Unfortunately classic classes' MRO is buggy when used with multiple inheritance with a diamond-shaped class hierachy. I won't explain that unless somebody asks, the important thing to take from this is that you should try to avoid inheriting from "classic classes" in Python 2. > > It's explicitly single inheritance and they are explicitly calling the > > superclass. > > They don't know that the superclass will be what is written there; > that's an implication of what I said with ?in Python, any class an > participate in multiple inheritance?. > > The author of a class (e.g. the OP's example ?MyList?) cannot know at > time of writing whether the superclass is ?list?. This is because some > other class may be using ?MyList? in a different inheritance > arrangement. In principle, Ben is correct here, but in practice, multiple-inheritance in Python is explicitly described as *cooperative* multiple-inheritance. You cannot expect to take any two (or more) arbitrary classes and inherit from them both. Sometimes you cannot inherit from them both at all: >>> class X(list, int): ... pass ... Traceback (most recent call last): File "", line 1, in TypeError: multiple bases have instance lay-out conflict Sometimes you cannot inherit from them both because there is no consistent MRO, and sometimes you cannot inherit from them both because they simply don't work well together. In this last case, Python cannot warn you about it, but your class will just not work right. ("Your save method over-writes the data my save method just wrote!" sort of thing.) The simplest way to break M-I is to directly call your parent class by name, instead of using super(). That effectively guarantees that your class cannot be used in M-I, except perhaps by accident. In a nutshell, you can physically inherit from "the" parent class by explicitly giving the name of the class, but by doing so you are effectively saying "Uh uh, oh no you don't, no way!" to any one wishing to use your class in a multiple inheritance situation. As the writer of the class, that is your right to do so (nobody can force you to write cooperative M-I classes), but in my opinion it's a bit rude or ignorant, like writing functions that never return their result but always print it instead. ("Nobody's going to re-use *my* functions, uh uh, no way!") (In the M-I world, there is no such thing as "the" parent class. There is, however, the parent *classes*.) [...] > Multiple inheitance is a fact in Python, and good practice is to not > arbitrarily write classes that break it. Hence my advice to avoid > hard-coding the superclass, and only use ?super? to discover the > superclass. I agree that it is good practice to use super, even if you personally expect to only use single-inheritance. You never know when somebody else, or even your future self, will decide to use M-I. Don't unnecessarily cut off that possibility, especially in Python 3 where using super() is actually easier than explicitly calling the parent class by hand: list.append(self, arg) super().append(arg) On the other hand, M-I in Python is cooperative, and it's not very often that you can expect to pick two arbitrary classes and inherit from them both. And on the third hand, inheritance in general is often over-rated, and M-I in particular is hard to get right. There's a strong argument to be made that one should only use full cooperative M-I when easier, less powerful versions like mixins and traits aren't enough. Or use composition and/or delegation instead. > So please use `super`, even in single inheritance. Otherwise you are > restricting the usefulness of your class: it can never be used with > multiple inheritance. I cannot disagree with Ben here. Even if you only intend to use single inheritance, don't unnecessarily rule out the alternatives. Using super is still usually the right thing to do. -- Steve From steve at pearwood.info Thu Jul 2 18:29:31 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 3 Jul 2015 02:29:31 +1000 Subject: [Tutor] =?utf-8?q?Inheritance=2C_superclass=2C_=E2=80=98super?= =?utf-8?b?4oCZ?= In-Reply-To: References: <58064.50.168.22.182.1435646865.squirrel@mail.tigertech.net> <85fv59rkxw.fsf@benfinney.id.au> <85bnfwsicw.fsf_-_@benfinney.id.au> Message-ID: <20150702162930.GJ10773@ando.pearwood.info> On Wed, Jul 01, 2015 at 10:40:20PM +0100, Alan Gauld wrote: > >Multiple inheitance is a fact in Python, and good practice is to not > >arbitrarily write classes that break it. > > That depends on what you mean by break it., MI should allow the > inheriting class to specify which, if any, of its direct superclasses > methods are invoked. That's critical to the use of MI in any language > The issues around that are addressed differently by different languages, > for example Eiffel allows you to rename inherited methods > or attributes to avoid them being used involuntarily. That sounds like Eiffel multiple-inheritance is closer to traits than full M-I: http://www.artima.com/weblogs/viewpost.jsp?thread=246488 > Most just allow > the programmer to explicitly access the superclasses where default > behaviour is not required. That's what I thought Python was doing with > super. Making default easy to get but still allowing direct overriding > when required. In Python, super() is designed to ensure that when you call the parent(s)'s method, *all* the parents that define that method get run, not just the immediate parent. (Assuming that they all cooperate by using super.) To quote Michele Simionato from the above link: [quote] The point to notice is that the complication of the MRO is by design: languages with a non-trivial MRO where [sic] designed this way to make possible method cooperation via super calls. That means that if both parents P1 and P2 define a method m, a child class C can override it and still have access to the m methods of the parents via super: C.m will call first P1.m and then P2.m, if P1.m features a super call itself. [end quote] If your inheritance needs don't match that behaviour, then Python's M-I model is not a good fit for you, and you might be better off with Michele's strait module, some other library, or just managing the inheritance calls yourself. But if you do that, you're implicitly ruling your class out from participating in "regular" M-I hierarchies. > >>class A(B,C,D): > >> def m(self): > >> C.m(self) # call C's m but not B and D if they have them. > >> > >>Or am I missing something in the way Python evaluates > >>the C.m() call above? > > I still want to know how you would code the above scenario > using super? If B,C and D all have an m() method but I only > want to call the version in C how do I control that in > Python other than by a direct call to C.m()? That can't be done using super(). Super isn't designed for that use-case, it's designed for the case where you want to call B.m, C.m and D.m in that order. Hence my comments in the previous post about not *unnecessarily* ruling out the use of multiple inheritance. If it's necessary, you can do it. > >Any class one writes can > >become part of some other class's multiple-inheritance chain, and it is > >bad practice to assume one knows at time of writing what the superclass > >will be at run time. > > This is the bit I don't understand. The superclass of a given class > should never change. The sequence of default processing may change but > any other behavior would be a serious breach of the class definition. Even in the single inheritance world, there is no such concept as "the superclass". There is *one or more* superclasses (note plural). (Zero only in the case of the root of the object hierarchy.) In the most general case, your child class doesn't know whether it is inheriting the method from the parent, grandparent, great-grandparent, ... great-to-the-Nth-grandparent. Now, in S-I world, it doesn't matter whether you write: super().method(arg) or MyParent.method(self, arg) it will just work. But in a M-I world, only the first case will operate correctly when using multiple inheritance of the particular model for M-I supported by Python. If your needs are different, you're on your own, and good luck to you. > A rather contrived example is where I want to inherit a Pen and a > Curtain to produce a GraphicalCurtain class. Both Pen and Curtain define > a draw() method but they do very different things. So I define two > methods: draw() which invokes Pen.draw() and close() which invokes > Curtain.draw(). If I use super in either of those methods I'll get the > wrong behavior because both Pen.draw and Curtain.draw will be invoked, I think Ben's super()-friendly answer to that would be to use an adaptor class: # Untested class MyCurtain(object): def __init__(self, *args): self.__curtain = Curtain(*args) def __getattr__(self, name): if name == 'draw': raise AttributeError return getattr(self.__curtain, name) def close(self): return self.__curtain.draw() class GraphicalCurtain(Pen, MyCurtain): def draw(self): print("Drawing drawing drawing...") super().draw() > Unless I'm missing something clever in super? I never quite > understood the parameters in super() so it is possible that > there is a way to exclude some super classes, but I'm not > aware of any such. There may be tricks you can play with metaclasses, to re-define the __mro__. You cannot do that with a regular class, as the __mro__ attribute is read-only, but with metaclasses there is no limit to the deep cacky you can find yourself. Er, I mean, no limit to the wonderful things you can do. > And lest there is any confusion, I agree that super should be the > default option. I'm only pointing out that a) super does not change > the superclass, it simply moves it to a different place in the > chain of execution and b) several cases exist where the default MRO > sequence is not what you need. In those cases you have to work > it out for yourself (or find a way to avoid them if possible!) I think I can agree with that. -- Steve From dyoo at hashcollision.org Thu Jul 2 19:15:44 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 2 Jul 2015 10:15:44 -0700 Subject: [Tutor] memory error In-Reply-To: References: Message-ID: > > So I got my code working now and it looks like this > > TAG = '{http://www.mediawiki.org/xml/export-0.10/}page' > doc = etree.iterparse(wiki) > > for _, node in doc: > if node.tag == TAG: > title = node.find("{http://www.mediawiki.org/xml/export-0.10/}title").text > if title in page_titles: > print (etree.tostring(node)) > node.clear() > Its mostly giving me what I want. However it is adding extra formatting (I believe name_spaces and attributes). I was wondering if there was a way to strip these out when I'm printing the node tostring? I suspect that you'll want to do an explicit walk over the node. Rather than use etree.tostring(), which indiscriminately walks the entire tree, you'll probably want to write a function to walk over selected portions of the tree structure. You can see: https://docs.python.org/2/library/xml.etree.elementtree.html#tutorial for an introduction to navigating portions of the tree, given a node. As a more general response: you have significantly more information about the problem than we do. At the moment, we don't have enough context to effectively help; we need more information. Do you have a sample *input* file that folks here can use to execute on your program? Providing sample input is important if you want reproducibility. Reproducibility is important because then we'll be on the same footing in terms of knowing what the problem's inputs are. See: http://sscce.org/ As for the form of the desired output: can you say more precisely what parts of the document you want? Rather than just say: "this doesn't look the way I want it to", it may be more helpful to say: "here's *exactly* what I'd like it to look like..." and show us the desired text output. That is: by expressing what you want as a collection of concrete input/output examples, you gain the added benefit that once you have revised your program, you can re-run it and see if what it's producing is what you anticipate. That is, you can use these concrete examples as a "regression test suite". This technique is something that software engineers use regularly in their day-to-day work, to make believe that their incomplete programs are working, to write out explicitly what those programs should do, and then to work on their programs until they do what they want them to. Good luck to you! From dyoo at hashcollision.org Thu Jul 2 19:38:53 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 2 Jul 2015 10:38:53 -0700 Subject: [Tutor] memory error In-Reply-To: References: Message-ID: On Thu, Jul 2, 2015 at 9:57 AM, Joshua Valdez wrote: > > Hi so I figured out my problem, with this code and its working great but its still taking a very long time to process...I was wondering if there was a way to do this with just regular expressions instead of parsing the text with lxml... Be careful: there are assumptions here that may not be true. To be clear: regular expressions are not magic. Just because something uses regular expressions does not make it fast. Nor are regular expressions appropriate for parsing tree-structured content. For a humorous discussion of this, see: http://blog.codinghorror.com/parsing-html-the-cthulhu-way/ > the idea would be to identify a tag and then move to the next line of a file to see if there is a match between the title text and the pages in my pages file. This makes another assumption about the input that isn't necessarily true. Just because you see tags and content on separate lines now doesn't mean that this won't change in the future. XML tree structure does not depend on newlines. Don't try parsing XML files line-by-line. > So again, my pages are just an array like: [Anarchism, Abrahamic Mythology, ...] I'm a little confused as to how to even start this my initial idea was something like this but I'm not sure how to execute it: > wiki --> XML file > page_titles -> array of strings corresponding to titles > > tag = r '()' > wiki = wiki.readlines() > > for line in wiki: > page = re.search(tag,line) > if page: > ......(I'm not sure what to do here) > > is it possible to look ahead in a loop to discover other lines and then backtrack? > I think this may be the solution but again I'm not sure how I would execute such a command structure... You should probably abandon this line of thinking. From your initial problem description, the approach you have now should be computationally *linear* in the size of the input. So maybe your program is fine. The fact that your program was exhausting your computer's entire memory in your initial attempt suggests that your input file is large. But how large? It is much more likely that your program is slow simply because your input is honking huge. To support this hypothesis, we need more knowledge about the input size. How large is your input file? How large is your collection of page_titles? From jdv12 at case.edu Thu Jul 2 18:57:13 2015 From: jdv12 at case.edu (Joshua Valdez) Date: Thu, 2 Jul 2015 12:57:13 -0400 Subject: [Tutor] memory error In-Reply-To: References: Message-ID: Hi so I figured out my problem, with this code and its working great but its still taking a very long time to process...I was wondering if there was a way to do this with just regular expressions instead of parsing the text with lxml... the idea would be to identify a tag and then move to the next line of a file to see if there is a match between the title text and the pages in my pages file. I would then want to write the entire page tag fdsalkfdjadslf text to ouput... So again, my pages are just an array like: [Anarchism, Abrahamic Mythology, ...] I'm a little confused as to how to even start this my initial idea was something like this but I'm not sure how to execute it: wiki --> XML file page_titles -> array of strings corresponding to titles tag = r '()' wiki = wiki.readlines() for line in wiki: page = re.search(tag,line) if page: ......(I'm not sure what to do here) is it possible to look ahead in a loop to discover other lines and then backtrack? I think this may be the solution but again I'm not sure how I would execute such a command structure... *Joshua Valdez* *Computational Linguist : Cognitive Scientist * (440)-231-0479 jdv12 at case.edu | jdv2 at uw.edu | joshv at armsandanchors.com On Wed, Jul 1, 2015 at 10:13 AM, Joshua Valdez wrote: > Hi Danny, > > So I got my code workin now and it looks like this > > TAG = '{http://www.mediawiki.org/xml/export-0.10/}page' > doc = etree.iterparse(wiki) > > for _, node in doc: > if node.tag == TAG: > title = node.find("{ > http://www.mediawiki.org/xml/export-0.10/}title").text > if title in page_titles: > print (etree.tostring(node)) > node.clear() > Its mostly giving me what I want. However it is adding extra formatting > (I believe name_spaces and attributes). I was wondering if there was a way > to strip these out when I'm printing the node tostring? > > Here is an example of the last few lines of my output: > > [[Category:Asteroids| ]] > [[Category:Spaceflight]] > h4rxxfq37qg30eqegyf4vfvkqn3r142 > > > > > > > > > *Joshua Valdez* > *Computational Linguist : Cognitive Scientist > * > > (440)-231-0479 > jdv12 at case.edu | jdv2 at uw.edu | joshv at armsandanchors.com > > > On Wed, Jul 1, 2015 at 1:17 AM, Danny Yoo wrote: > >> Hi Joshua, >> >> >> >> The issue you're encountering sounds like XML namespace issues. >> >> >> >> So I tried that code snippet you pointed me too and I'm not getting >> any output. >> >> >> This is probably because the tag names of the XML are being prefixed >> with namespaces. This would make the original test for node.tag to be >> too stingy: it wouldn't exactly match the string we want, because >> there's a namespace prefix in front that's making the string mismatch. >> >> >> Try relaxing the condition from: >> >> if node.tag == "page": ... >> >> to something like: >> >> if node.tag.endswith("page"): ... >> >> >> This isn't quite technically correct, but we want to confirm whether >> namespaces are the issue that's preventing you from seeing those >> pages. >> >> >> If namespaces are the issue, then read: >> >> http://effbot.org/zone/element-namespaces.htm >> > > From cybervigilante at gmail.com Thu Jul 2 21:30:23 2015 From: cybervigilante at gmail.com (Jim Mooney Py3.4.3winXP) Date: Thu, 2 Jul 2015 12:30:23 -0700 Subject: [Tutor] Are the methods in a class copied or just linked to? Message-ID: When an instance uses a class method, does it actually use the method that is in the class object's memory space, or is the method copied to the instance? -- Jim From dyoo at hashcollision.org Thu Jul 2 22:46:37 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 2 Jul 2015 13:46:37 -0700 Subject: [Tutor] Are the methods in a class copied or just linked to? In-Reply-To: References: Message-ID: On Thu, Jul 2, 2015 at 12:30 PM, Jim Mooney Py3.4.3winXP wrote: > When an instance uses a class method, does it actually use the method that > is in the class object's memory space, or is the method copied to the > instance? Unsure. How would it be observable? [The following is not beginner material, and mostly implementation-specific experimentation. Skip if you are a beginner.] We know that bound functions are more than code: they need to include references to the otherwise free variables. But I think the underlying function code is shared. It's not logically required, but it makes engineering sense. Otherwise, making bound functions would be significantly more expensive than the obvious approach of sharing the part that doesn't change. As an example: ######################################### >>> def f(name): ... def g(): ... return name ... return g ... >>> d = f("danny") >>> j = f("jim") >>> d() 'danny' >>> j() 'jim' >>> id(d) 4407845544 >>> id(j) 4407847344 >>> d.__code__ ", line 2> >>> j.__code__ ", line 2> ######################################### Here, 'd' and 'j' may be different values, but they share the same underlying __code__, the compiled code object that defines 'g'. __code__ and __func__ are special variables used to access the underlying function values. They're described in: https://docs.python.org/3/reference/datamodel.html The following section in the reference are immediately relevant to your question: """" When an instance method object is created by retrieving a user-defined function object from a class via one of its instances, its __self__ attribute is the instance, and the method object is said to be bound. The new method?s __func__ attribute is the original function object. When a user-defined method object is created by retrieving another method object from a class or instance, the behaviour is the same as for a function object, except that the __func__ attribute of the new instance is not the original method object but its __func__ attribute. When an instance method object is created by retrieving a class method object from a class or instance, its __self__ attribute is the class itself, and its __func__ attribute is the function object underlying the class method. """" >From my reading of this, the language reference is guaranteeing that __func__ is shared. From __peter__ at web.de Thu Jul 2 23:09:13 2015 From: __peter__ at web.de (Peter Otten) Date: Thu, 02 Jul 2015 23:09:13 +0200 Subject: [Tutor] Are the methods in a class copied or just linked to? References: Message-ID: Danny Yoo wrote: > On Thu, Jul 2, 2015 at 12:30 PM, Jim Mooney Py3.4.3winXP > wrote: >> When an instance uses a class method, does it actually use the method >> that is in the class object's memory space, or is the method copied to >> the instance? > Unsure. How would it be observable? [snip implementation details] My understanding of the question is less involved. I'd suggest: Create a class Foo with a method bar(), say, instantiate it, replace foo in the class and then invoke foo in the previously created instance. If the old method is executed the instance must keep a copy, if the new method is executed there is probably no copy. >>> class Foo: ... def bar(self): print("original bar") ... >>> foo = Foo() >>> def modified_bar(self): ... print("modified bar") ... >>> Foo.bar = modified_bar >>> foo.bar() modified bar Conclusion: no copy. From alan.gauld at btinternet.com Thu Jul 2 23:18:15 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 02 Jul 2015 22:18:15 +0100 Subject: [Tutor] Are the methods in a class copied or just linked to? In-Reply-To: References: Message-ID: On 02/07/15 20:30, Jim Mooney Py3.4.3winXP wrote: > When an instance uses a class method, does it actually use the method that > is in the class object's memory space, or is the method copied to the > instance? As soon as you mention memory space in relation to how something works in Python you know you are barking up the wrong tree. Memory space is a concept that only applies at the implementation level in Python, never in the concepts. Python objects (including classes, which are just another object) are not necessarily constrained to a single memory space, Classes are conglomerations of other objects (data and functions) and linked to other classes (via inheritance). This is not like C++ (or even Java). The method definitions inside a class define method objects that live in their own conceptual space as values in the dictionary that defines the class structure. Two methods of the same class could live in wildly different memory locations or they could all be part of the same memory area. That's an implementation detail and likely to be very different in CPython, Jython and IronPython etc.. So the question should be whether Python objects access the references to those methods by linking through the class object or whether they get copies of the references internally. I think Danny's answer covers that aspect. But remember that questions of where Python objects live in memory are nearly always the wrong question to ask. Finally, I'm guessing that when you said a "class method" you actually meant a "method in a class", ie an instance method? You did not mean an actual class method as defined using the @classmethod decorator? -- 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 ben+python at benfinney.id.au Fri Jul 3 01:29:33 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 03 Jul 2015 09:29:33 +1000 Subject: [Tutor] =?utf-8?q?Inheritance=2C_superclass=2C_=E2=80=98super?= =?utf-8?b?4oCZ?= References: <58064.50.168.22.182.1435646865.squirrel@mail.tigertech.net> <85fv59rkxw.fsf@benfinney.id.au> <85bnfwsicw.fsf_-_@benfinney.id.au> <20150702153323.GH10773@ando.pearwood.info> Message-ID: <85wpyiqgte.fsf@benfinney.id.au> Steven D'Aprano writes: > I mostly agree with what Ben says, comments below. > > On Wed, Jul 01, 2015 at 12:48:47PM +1000, Ben Finney wrote: > > So please use `super`, even in single inheritance. Otherwise you are > > restricting the usefulness of your class: it can never be used with > > multiple inheritance. > > I cannot disagree with Ben here. Even if you only intend to use single > inheritance, don't unnecessarily rule out the alternatives. Using > super is still usually the right thing to do. I'm glad you agree with the above, since I was quoting your words from 2011 :-) -- \ ?? no testimony can be admitted which is contrary to reason; | `\ reason is founded on the evidence of our senses.? ?Percy Bysshe | _o__) Shelley, _The Necessity of Atheism_, 1811 | Ben Finney From ben+python at benfinney.id.au Fri Jul 3 01:47:03 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 03 Jul 2015 09:47:03 +1000 Subject: [Tutor] =?utf-8?q?Inheritance=2C_superclass=2C_=E2=80=98super?= =?utf-8?b?4oCZ?= References: <58064.50.168.22.182.1435646865.squirrel@mail.tigertech.net> <85fv59rkxw.fsf@benfinney.id.au> <85bnfwsicw.fsf_-_@benfinney.id.au> Message-ID: <85h9pmqg08.fsf@benfinney.id.au> Alan Gauld writes: > On 01/07/15 03:48, Ben Finney wrote: > > Alan Gauld writes: > > > >> Whilst I agree with the general use of super I'm not sure what > >> the MRO resolution has to do with this case? > > > > When accessing the superclass, the MRO is always relevant > > Can you explain that? > If I access a class explicitly As explained on other posts: You're not getting ?the superclass? if you do that. Knowing what the superclass is requires run-time determination of the MRO. > If MyList is included in a MI lattice that doesn't change the fact > that MyList inherits directly from list. Correct. The distinction being drawn here is between ?the class from which MyList directly inherits? versus ?the superclass of this class?. They are not the same, in multiple inheritance. To refer to the direct parent class, yes it's simple enough to hard-code that. But that's not the same thing as accessing the superclass, which must be determined at run-time. > That depends on what you mean by break it., MI should allow the > inheriting class to specify which, if any, of its direct superclasses > methods are invoked. That ?should? is contrary to Python's collaborative multiple inheritance model. Instead, multiple inheritance in Python entails that any class should allow the methods of the same name, in all superclasses of this one, to execute in the sequence determined by ?super?. So, if you want behaviour as you describe there, you want something that Python's mutliple inheritance explicitly won't give you. -- \ ?Intellectual property is to the 21st century what the slave | `\ trade was to the 16th.? ?David Mertz | _o__) | Ben Finney From cybervigilante at gmail.com Fri Jul 3 02:39:12 2015 From: cybervigilante at gmail.com (Jim Mooney Py3.4.3winXP) Date: Thu, 2 Jul 2015 17:39:12 -0700 Subject: [Tutor] method conflict? Message-ID: Okay, it appears the method in a class has its own ID, but all instantiations of that method have identical IDs. But what happens if we have a huge number of instantiations trying to access the identical method at the same time? class MyClass: def setdata(self, data): self.data = data def getdata(self): print(self.data) >>> MyClass.setdata >>> id(MyClass.setdata) 40676568 >>> f = MyClass() >>> g = MyClass() >>> id(f.setdata) 43576616 >>> id(g.setdata) 43576616 >>> d = MyClass() >>> id(d.setdata) 43576616 -- Jim From steve at pearwood.info Fri Jul 3 03:17:12 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 3 Jul 2015 11:17:12 +1000 Subject: [Tutor] Are the methods in a class copied or just linked to? In-Reply-To: References: Message-ID: <20150703011712.GM10773@ando.pearwood.info> On Thu, Jul 02, 2015 at 12:30:23PM -0700, Jim Mooney Py3.4.3winXP wrote: > When an instance uses a class method, does it actually use the method that > is in the class object's memory space, or is the method copied to the > instance? The first. And it is not just an implementation detail, it is part of Python's programming model. When you define a class in you define methods and other attributes in the class namespace: class C: attr = 23 def method(self, arg): pass 'attr' and 'method' exist inside C.__dict__. That is, I believe, more than just implementation, I believe that is considered part of the language. For instance, this works in IronPython too: >>> class C: ... def method(self): pass ... >>> C.__dict__['method'] even though classes in IronPython are based on a completely different technology (the .Net CLR) from the standard CPython implementation. When you create an instance, under normal circumstances it gets its own __dict__, but that does *not* get a copy of the method, nor even a reference to the original method. In IronPython again: >>> c = X() >>> c.__dict__ != C.__dict__ # Separate dictionaries? True >>> 'method' in c.__dict__ # in the instance? False >>> 'method' in C.__dict__ # in the class? True When you call a method, c.method(), Python does a name look-up. Simplified, it looks in c.__dict__, then C.__dict__, then the __dict__s of each of C's parents (if any). The upshot of this is that you can actually override the method for a specific instance. In Ruby, this technique is called "singleton method", in Python it doesn't have a standard name, but it can be useful. It only works on classes with an instance __dict__, so it doesn't work on ints, floats, strings and other built-in types, and for technical reasons it doesn't work on magic __dunder__ methods, and it is a little fiddly to get working, but it does work: >>> class C: ... def method(self): ... return "shared" ... >>> c = C() >>> d = C() >>> from types import MethodType >>> c.method = MethodType(lambda self: 'overridden', c) >>> c.method() 'overridden' >>> d.method() 'shared' -- Steve From cs at zip.com.au Fri Jul 3 03:46:26 2015 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 3 Jul 2015 11:46:26 +1000 Subject: [Tutor] method conflict? In-Reply-To: References: Message-ID: <20150703014626.GA14768@cskk.homeip.net> On 02Jul2015 17:39, Jim Mooney Py3.4.3winXP wrote: >Okay, it appears the method in a class has its own ID, but all >instantiations of that method have identical IDs. But what happens if we >have a huge number of instantiations trying to access the identical method >at the same time? Um, they all get given access to the method? I'm not sure what you think should be happening here: >class MyClass: > def setdata(self, data): > self.data = data > def getdata(self): > print(self.data) > >>>> MyClass.setdata > >>>> id(MyClass.setdata) >40676568 >>>> f = MyClass() >>>> g = MyClass() Two instances of the class. >>>> id(f.setdata) >43576616 >>>> id(g.setdata) >43576616 Two references to the same method obtained from the class. >>>> d = MyClass() >>>> id(d.setdata) >43576616 And a third. Steven D'Aprano posted a decent explaination in your other thread ("Are the methods in a class copied or just linked to?"); it would have been helpful if your post had stayed in that thread - it is essentially the same discussion. So all your "d.setdata" expressions consult various places in order to find the method definition, and they all find it in the class. And so you are given the same method every time, and thus the same id. Cheers, Cameron Simpson From steve at pearwood.info Fri Jul 3 04:00:54 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 3 Jul 2015 12:00:54 +1000 Subject: [Tutor] method conflict? In-Reply-To: References: Message-ID: <20150703020054.GN10773@ando.pearwood.info> On Thu, Jul 02, 2015 at 05:39:12PM -0700, Jim Mooney Py3.4.3winXP wrote: > Okay, it appears the method in a class has its own ID, but all > instantiations of that method have identical IDs. I'm not sure what you mean by this. *All* objects have their own ID, and instantiations of methods *don't* have identical IDs, although it may appear so at first glance (see further below). py> class C: ... def method(self): ... return 23 ... py> c = C() py> x = c.method # grab a reference to the method py> y = c.method # and another one py> print(id(x), id(y)) 3082036364 3082100428 *But* that's actually an implementation detail. It just so happens that the way CPython currently works, each time you look up a method you get a new instance. Technical details are technical, and involve the descriptor protocol, which you don't need to understand. I'm happy to go into detail if you like, but for now let's just skip over it and just call it "magic". There's nothing in the language specification (that I know of) which *promises* that x and y must be separate objects with different IDs. The underlying function object, retrieved directly from the class __dict__, *is* guaranteed to always give the same object: py> a = C.__dict__['method'] py> b = C.__dict__['method'] py> print(id(a), id(b)) 3083756356 3083756356 That's the way dictionaries work. > But what happens if we > have a huge number of instantiations trying to access the identical method > at the same time? What of it? I don't understand what you think the potential problem is. Regardless of whether you access the method wrapper, or the underlying function, calling it from multiple places is allowed. Each function call is independent of the others. Naturally, the *action* of the function may not be safe to call twice. (It might, say, try to delete the same file twice, and the second time you get an error because the file no longer exists.) But the function calling infrastructure is safe to do multiple times: len(a) # Safe to call it once. len(b) # and still safe to call it twice LEN = len; LEN(c) # even if you use a different name > class MyClass: > def setdata(self, data): > self.data = data > def getdata(self): > print(self.data) > > > >>> MyClass.setdata > In Python 3, looking up MyClass.setdata returns the underlying function object. But in Python 2, it actually returns an "unbound method" object, which is a wrapper around the underlying function. I mention this only for completion, in practice it makes little or no difference. > >>> id(MyClass.setdata) > 40676568 > >>> f = MyClass() > >>> g = MyClass() > >>> id(f.setdata) > 43576616 > >>> id(g.setdata) > 43576616 > >>> d = MyClass() > >>> id(d.setdata) > 43576616 You are misinterpreting what you are seeing. Python is free to reuse IDs. CPython does re-use them, IronPython and Jython do not. When you run this line of code: id(f.setdata) at least five things happen: - Python does an attribute search for "setdata" starting with f; - it finds the *function* f inside the class __dict__; - because functions are descriptors ("magic"), Python creates a *method* wrapper around the function, and returns that; - the method gets passed to the id() function, which determines the ID (in this case 43576616) and returns that; - the method object is then garbage collected, which frees up the ID to be re-used the next time you create a method. If you did the same thing in (say) Jython, you would get very different results, something like this: >>> id(f.setdata) 43 >>> id(g.setdata) 44 >>> id(d.setdata) 45 (the actual IDs may vary). But *most of the time* none of this has the least bit of importance to your code. Whether you get the same method object or different method objects is more or less irrelevant to how your code works. The exception is when you start looking at introspection functions like id(), or testing for object identity ("is this the same object?") using `is`. -- Steve From breamoreboy at yahoo.co.uk Fri Jul 3 06:35:30 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 03 Jul 2015 05:35:30 +0100 Subject: [Tutor] method conflict? In-Reply-To: References: Message-ID: On 03/07/2015 01:39, Jim Mooney Py3.4.3winXP wrote: > Okay, it appears the method in a class has its own ID, but all > instantiations of that method have identical IDs. But what happens if we > have a huge number of instantiations trying to access the identical method > at the same time? I understand that Python "just works". What do you think happens? > > class MyClass: > def setdata(self, data): > self.data = data > def getdata(self): > print(self.data) > I'd say the only conflict here is writing unneeded boilerplate code in Python :) -- 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 Fri Jul 3 11:21:16 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 03 Jul 2015 10:21:16 +0100 Subject: [Tutor] method conflict? In-Reply-To: References: Message-ID: On 03/07/15 01:39, Jim Mooney Py3.4.3winXP wrote: > Okay, it appears the method in a class has its own ID, but all > instantiations of that method have identical IDs. But what happens if we > have a huge number of instantiations trying to access the identical method > at the same time? They all execute the method. Its no different to a global function. What happens if lots of objects, or even threads, access a global function? They all execute the same code, each within their own environment. It's the same with methods. I'm not sure why you think there would be an issue? One issue that may be confusing things is when you say "at the same time". Now in principle they could all be running concurrently since they never modify the function they only "read" it but in practice CPython serializes those calls anyway so only one call is active at any one time. But there is no real reason why that should be the case. -- 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 Fri Jul 3 11:32:52 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 03 Jul 2015 10:32:52 +0100 Subject: [Tutor] Are the methods in a class copied or just linked to? In-Reply-To: <20150703011712.GM10773@ando.pearwood.info> References: <20150703011712.GM10773@ando.pearwood.info> Message-ID: On 03/07/15 02:17, Steven D'Aprano wrote: > On Thu, Jul 02, 2015 at 12:30:23PM -0700, Jim Mooney Py3.4.3winXP wrote: > >> When an instance uses a class method, does it actually use the method that >> is in the class object's memory space, or is the method copied to the >> instance? > > The first. And it is not just an implementation detail, it is part of > Python's programming model. Pythons implementation model does not dictate the memory space of the method. The method is in the class name space and referenced via the internal dict. But the actual memory space of the method object can be outside the memory space of the class. That's what I mean by implementation detail. You cannot for example do a memcopy of a class and expect to get a fully functioning independant clone of the class in the new memory space. Nor an you reliably calculate a method's memory location by adding an offset to the class's location as you can in say C++ (a common way of gaining access to private methods in early C++ code). > When you define a class in you define methods and other attributes in > the class namespace: But this is true and the mechanism for looking up the method is defined in the Python model and it goes via the class. But where the class stores its methods is up to the implementation. -- 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 Fri Jul 3 12:35:59 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 3 Jul 2015 20:35:59 +1000 Subject: [Tutor] Are the methods in a class copied or just linked to? In-Reply-To: References: <20150703011712.GM10773@ando.pearwood.info> Message-ID: <20150703103559.GO10773@ando.pearwood.info> On Fri, Jul 03, 2015 at 10:32:52AM +0100, Alan Gauld wrote: > On 03/07/15 02:17, Steven D'Aprano wrote: > >On Thu, Jul 02, 2015 at 12:30:23PM -0700, Jim Mooney Py3.4.3winXP wrote: > > > >>When an instance uses a class method, does it actually use the method that > >>is in the class object's memory space, or is the method copied to the > >>instance? > > > >The first. And it is not just an implementation detail, it is part of > >Python's programming model. > > Pythons implementation model does not dictate the memory space > of the method. The method is in the class name space and referenced > via the internal dict. But the actual memory space of the method > object can be outside the memory space of the class. That's what > I mean by implementation detail. You cannot for example do a > memcopy of a class and expect to get a fully functioning > independant clone of the class in the new memory space. Nor an you > reliably calculate a method's memory location by adding an offset > to the class's location as you can in say C++ (a common way > of gaining access to private methods in early C++ code). Oh. Right, I agree with this. Classes, and instances, in Python can be spread over multiple chunks of heap memory. The specific details of where the various parts live in memory are not defined by the language, and will differ from implementation to implementation. All you know is that methods (by default) will be found in the class __dict__, but not where the methods actually are located, of even where the class __dict__ will be located. > >When you define a class in you define methods and other attributes in > >the class namespace: > > But this is true and the mechanism for looking up the method is defined > in the Python model and it goes via the class. But where the class > stores its methods is up to the implementation. Methods are stored in the __dict__, but the location of the __dict__ is a mystery :-) -- Steve From oscar.j.benjamin at gmail.com Fri Jul 3 16:47:04 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Fri, 3 Jul 2015 15:47:04 +0100 Subject: [Tutor] Are the methods in a class copied or just linked to? In-Reply-To: <20150703011712.GM10773@ando.pearwood.info> References: <20150703011712.GM10773@ando.pearwood.info> Message-ID: On 3 July 2015 at 02:17, Steven D'Aprano wrote: > On Thu, Jul 02, 2015 at 12:30:23PM -0700, Jim Mooney Py3.4.3winXP wrote: > >> When an instance uses a class method, does it actually use the method that >> is in the class object's memory space, or is the method copied to the >> instance? > > The first. And it is not just an implementation detail, it is part of > Python's programming model. > > When you define a class in you define methods and other attributes in > the class namespace: > > class C: > attr = 23 > def method(self, arg): > pass > [snip] > > When you call a method, c.method(), Python does a name look-up. > Simplified, it looks in c.__dict__, then C.__dict__, then the __dict__s > of each of C's parents (if any). To expand on this slightly. The expression c.method() is calculated as (c.method)(). So the expression c.method must be evaulated first. To evaluate this the name lookup occurs and a bound method object is created. The bound method object wraps together the instance with the method that was found during lookup. Having evaluated the expression c.method the resulting bound method is then called to complete the evaluation of the expression c.method(). Subsequently replacing the method in the class does not affect the bound method since the lookup has already occurred: >>> class A: ... def foo(self): ... print('Original foo') ... >>> a = A() >>> afoo = a.foo >>> A.foo = lambda self: print('New foo') >>> afoo > >>> afoo() Original foo >>> a.foo() New foo -- Oscar From oscar.j.benjamin at gmail.com Fri Jul 3 18:12:54 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Fri, 3 Jul 2015 17:12:54 +0100 Subject: [Tutor] =?utf-8?q?Inheritance=2C_superclass=2C_=E2=80=98super?= =?utf-8?b?4oCZ?= In-Reply-To: <85h9pmqg08.fsf@benfinney.id.au> References: <58064.50.168.22.182.1435646865.squirrel@mail.tigertech.net> <85fv59rkxw.fsf@benfinney.id.au> <85bnfwsicw.fsf_-_@benfinney.id.au> <85h9pmqg08.fsf@benfinney.id.au> Message-ID: On 3 July 2015 at 00:47, Ben Finney wrote: >> That depends on what you mean by break it., MI should allow the >> inheriting class to specify which, if any, of its direct superclasses >> methods are invoked. > > That ?should? is contrary to Python's collaborative multiple inheritance > model. Instead, multiple inheritance in Python entails that any class > should allow the methods of the same name, in all superclasses of this > one, to execute in the sequence determined by ?super?. > > So, if you want behaviour as you describe there, you want something that > Python's mutliple inheritance explicitly won't give you. I guess you mean something that it won't *implicitly* give you. Python makes it possible to construct whatever method dispatch system you like in MI but super is designed with the implicit intention that you would use this particular convention that the same method is called in all superclasses. The particular model of multiple inheritance for which super is designed requires the classes involved to have been designed for that use case. Simply using super instead of explicitly invoking the expected superclass does not make a class suitable for this or any other MI method-calling convention. It is a necessary but not sufficient condition to make it suitable for the convention you describe. You seem to be advocating that "everyone should always use super() because everyone should always assume that someone else will want to use their class in a particular style of multiple inheritance setup." I would rather say that "no one should ever use classes in multiple inheritance unless the classes involved were designed for that usage together." (The everyone/always etc. are meant loosely.) I guess a different point is that it's not really harmful to just use super() so you may as well "just do it" without giving it much thought. This argument makes more sense in Python 3 than Python 2 though since 2's super is a little tricky to get right in some cases (e.g. __new__). -- Oscar From nymcity at yahoo.com Fri Jul 3 23:04:10 2015 From: nymcity at yahoo.com (Nym City) Date: Fri, 3 Jul 2015 21:04:10 +0000 (UTC) Subject: [Tutor] Loop not iterating In-Reply-To: <20150630021900.GB10773@ando.pearwood.info> References: <20150630021900.GB10773@ando.pearwood.info> Message-ID: <2073306837.2195403.1435957450868.JavaMail.yahoo@mail.yahoo.com> Thank to very much for replying. ?The second solution that you proposed worked perfectly: import csvdomains = open('top500domains.csv')domainsReader = csv.reader(domains)domains = ["https://www." + row[1] for row in domainsReader]for domain in domains:? ? print(domain)?The above solution is perfect and simple. It allows me to easily insert text such as?"https://www."??in the beginning of my strings or at the end. However, something else that came to mind was how would you break the string and insert new text in the middle. For example:"www.lovepython.com" I want to insert "lesson1." after the second period above. So it would come back as: "www.lovepython.lesson1.com"First I thought?row[1]?in the code above referred to the first place in the beginning of the string. So I tried to change that number around but it did not work. I have a feeling I might be mixing few concepts together...?Thank you. On Monday, June 29, 2015 10:19 PM, Steven D'Aprano wrote: On Tue, Jun 30, 2015 at 01:05:13AM +0000, Nym City wrote: > Hello all, > Thank you for your time and tips. The reason why I decided to create a > loop is because the output is cleaner and did not require any > formatting. However, based on what I have learned from all of your > responses, that is not going to work with what I am trying to do. There's no reason why a loop wouldn't work, although there may be better solutions, and a loop is a much better solution to what you have below. > Here is the updated code: > import csvdomains = open('top500domains.csv')domainsReader = > csv.reader(domains)domainLists = > list(domainsReader)print('\n'.join(["https://www ." + str(lst) for lst > in domainLists]), sep='\n') Please ensure you post as *plain text*, not HTML (so called "rich text" or "formatted text"). When you post as HTML, many mail programs mangle the code and destroy the formatting, as you can see above. Reconstructing what the code should look like on five separate lines: import csv domains = open('top500domains.csv') domainsReader = csv.reader(domains) domainLists = list(domainsReader) print('\n'.join(["https://www ." + str(lst) for lst in domainLists]), sep='\n') The first three lines seem to be correct, but the next: ? ? domainLists = list(domainsReader) is unnecessary and should be deleted. However, the last line does too much, and the wrong thing too. A simple loop is *much* better here, since you just print the domains and don't need to keep them for further processing: for row in domainLists: ? ? # row is a list of *one or more* fields; in this case, there ? ? # is only one field per row, the domain name ? ? domain = "https://www." + row[0] ? ? print(domain) Suppose that you do need to keep the domains for later, as well as print them. Then you can do this instead: import csv domains = open('top500domains.csv') domainsReader = csv.reader(domains) # Make a list of the domains. domains = ["https://www." + row[0] for row in domainsReader] # Print each domain. for domain in domains: ? ? print(domain) # Now continue to do more work on the list of domains... -- Steve _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From paul at whoosh.cn Sat Jul 4 06:05:47 2015 From: paul at whoosh.cn (Paul) Date: Sat, 04 Jul 2015 12:05:47 +0800 Subject: [Tutor] =?utf-8?q?about_real-time_audio_synthesis_and_algorithmic?= =?utf-8?q?_composition?= In-Reply-To: ----1X------r5KXX$7a16e2c2-6696-444d-b940-d3559e0df81b@whoosh.cn References: ----1X------r5KXX$7a16e2c2-6696-444d-b940-d3559e0df81b@whoosh.cn Message-ID: <----1X------r5KXX$12dd714d-d504-4f13-922a-fb2a53e68cd7@whoosh.cn> Hi ! I'm a rookie to programming, and just graduated from a conservatory of music last year. I'm interested in real-time audio synthesis and algorithmic composition, I can write a little bit SuperCollider and Django. I just buy a book called "Python Programming for the Absolute Beginner" (Chinese version), and try to learn Python via it. I have a query, Is there have a way (e.g. import some library) to synthesis sound in Python as SuperCollider? (synthesis sound via write some code in a file, and run it will hear the result immediately.) I'm realy like Python's style, and want to learn Programming from Python. If I can learn it through synthesis sound, I will be very happy. :-) In addition, I think if I can synthesis sound via Python Code, I will have more chance to cooperate with other programer. :-) Thanks! Paul Yeh From alan.gauld at btinternet.com Sat Jul 4 10:04:00 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 04 Jul 2015 09:04:00 +0100 Subject: [Tutor] about real-time audio synthesis and algorithmic composition In-Reply-To: <----1X------r5KXX$12dd714d-d504-4f13-922a-fb2a53e68cd7@whoosh.cn> References: <----1X------r5KXX$12dd714d-d504-4f13-922a-fb2a53e68cd7@whoosh.cn> Message-ID: On 04/07/15 05:05, Paul wrote: > I'm interested in real-time audio synthesis and > algorithmic composition, There are a few options for working with audio in the standard library - winsound, aifc,wave etc. The PyGame package also has some audio capabilities. And finally the Scikit project has some audio packages too. There is a wiki page on the subject here: https://wiki.python.org/moin/PythonInMusic 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 lac at openend.se Sun Jul 5 01:25:11 2015 From: lac at openend.se (Laura Creighton) Date: Sun, 05 Jul 2015 01:25:11 +0200 Subject: [Tutor] method conflict? In-Reply-To: Message from "Jim Mooney Py3.4.3winXP" of "Thu, 02 Jul 2015 17:39:12 -0700." References: Message-ID: <201507042325.t64NPBn4005930@theraft.openend.se> In a message of Thu, 02 Jul 2015 17:39:12 -0700, "Jim Mooney Py3.4.3winXP" writ es: >Okay, it appears the method in a class has its own ID, but all >instantiations of that method have identical IDs. But what happens if we >have a huge number of instantiations trying to access the identical method >at the same time? > >class MyClass: > def setdata(self, data): > self.data = data > def getdata(self): > print(self.data) > > >>>> MyClass.setdata > >>>> id(MyClass.setdata) >40676568 >>>> f = MyClass() >>>> g = MyClass() >>>> id(f.setdata) >43576616 >>>> id(g.setdata) >43576616 >>>> d = MyClass() >>>> id(d.setdata) >43576616 > > >-- >Jim Implementation dependent. What Jython does and what CPython does are not the same thing here. You need, if at all possible, to flush your brain of the C and C++ ish idea that through id I can find the exact chunk of memory where this value is stored, if you ever want your code to run other places than in CPython. Python does not have any idea of 'the exact chunk of memory where something is stored', though that is, indeed how CPython implements it. The language only promises that you will get an integer which is unique and constant for the lifetime of an object. If your Python uses a GC that relocates objects, then one of the things that the GC will have to do is guarantee that the moved object has the same id as it did when it was somewhere else. But it still will be somewhere else. Laura From lac at openend.se Sun Jul 5 02:32:40 2015 From: lac at openend.se (Laura Creighton) Date: Sun, 05 Jul 2015 02:32:40 +0200 Subject: [Tutor] =?utf-8?q?about_real-time_audio_synthesis_and_algorithmic?= =?utf-8?q?_composition?= In-Reply-To: Message from "Paul" of "Sat, 04 Jul 2015 12:05:47 +0800." <----1X------r5KXX$12dd714d-d504-4f13-922a-fb2a53e68cd7@whoosh.cn> References: ----1X------r5KXX$7a16e2c2-6696-444d-b940-d3559e0df81b@whoosh.cn<----1X------r5KXX$12dd714d-d504-4f13-922a-fb2a53e68cd7@whoosh.cn> Message-ID: <201507050032.t650Werq022167@theraft.openend.se> In a message of Sat, 04 Jul 2015 12:05:47 +0800, "Paul" writes: >Hi ! >I'm a rookie to programming, and just graduated from a conservatory of music last year. I'm interested in real-time audio synthesis and algorithmic composition, I can write a little bit SuperCollider and Django. I just buy a book called "Python Programming for the Absolute Beginner" (Chinese version), and try to learn Python via it. > >I have a query, Is there have a way (e.g. import some library) to synthesis sound in Python as SuperCollider? (synthesis sound via write some code in a file, and run it will hear the result immediately.) > >I'm realy like Python's style, and want to learn Programming from Python. If I can learn it through synthesis sound, I will be very happy. :-) In addition, I think if I can synthesis sound via Python Code, I will have more chance to cooperate with other programer. :-) > >Thanks! >Paul Yeh There are python bindings for supercollider. I've never used them. http://supercollider.sourceforge.net/wiki/index.php/Systems_interfacing_with_SC lists 3 and I think this is a 4th. https://pypi.python.org/pypi/SC/0.2 You can also get it through Blender. http://blenderscripting.blogspot.se/2014/11/blender-pynodes-and-supercollider-and.html I've done this. It works. Best of luck, write back if you have any problems, Laura From lac at openend.se Sun Jul 5 07:18:46 2015 From: lac at openend.se (Laura Creighton) Date: Sun, 05 Jul 2015 07:18:46 +0200 Subject: [Tutor] =?utf-8?q?about_real-time_audio_synthesis_and_algorithmic?= =?utf-8?q?_composition?= In-Reply-To: Message from "Zhongye" of "Sun, 05 Jul 2015 12:18:34 +0800." <----1X------r5KXX$8c87c5de-ecff-4ea5-a203-dbe56e7c85db@whoosh.cn> References: ----1X------r5KXX$7a16e2c2-6696-444d-b940-d3559e0df81b@whoosh.cn<----1X------r5KXX$12dd714d-d504-4f13-922a-fb2a53e68cd7@whoosh.cn>, 201507050032.t650Werq022167@theraft.openend.se<----1X------r5KXX$8c87c5de-ecff-4ea5-a203-dbe56e7c85db@whoosh.cn> Message-ID: <201507050518.t655Ik2p017606@theraft.openend.se> In a message of Sun, 05 Jul 2015 12:18:34 +0800, "Zhongye" writes: >Thanks for your helpful reply. >I have check them, that it control SuperCollider via OSC (Open Sound Control), and it provides with a simpler API to use those libraries. >Is that mean if I write some python scripts importing it, and the others want to use my scripts, they have to also install the SuperCollider? That is one way to do things. The other, more complicated way is to package your result into a binary file that contains all the things you need --- which means that they won't have to install SuperCollider because you already have done this for them. Packaging solutions are different depending on what operating system you are running, so if you want your thing to run under windows and Mac OS and Linux you will need to make at least 3 different versions. Laura From steve at pearwood.info Sun Jul 5 08:08:44 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 5 Jul 2015 16:08:44 +1000 Subject: [Tutor] Loop not iterating In-Reply-To: <2073306837.2195403.1435957450868.JavaMail.yahoo@mail.yahoo.com> References: <20150630021900.GB10773@ando.pearwood.info> <2073306837.2195403.1435957450868.JavaMail.yahoo@mail.yahoo.com> Message-ID: <20150705060844.GT10773@ando.pearwood.info> Hi Nym, sorry your code's formatting is broken again. I've tried my best to fix it below: On Fri, Jul 03, 2015 at 09:04:10PM +0000, Nym City via Tutor wrote: > Thank to very much for replying. ?The second solution that you proposed worked perfectly: > > import csv > domains = open('top500domains.csv') > domainsReader = csv.reader(domains) > domains = ["https://www." + row[1] for row in domainsReader] > for domain in domains: >? ? print(domain)? > > The above solution is perfect and simple. It allows me to easily > insert text such as?"https://www."??in the beginning of my strings or > at the end. > > However, something else that came to mind was how would you break the > string and insert new text in the middle. For > example:"www.lovepython.com" I want to insert "lesson1." after the > second period above. So it would come back as: > "www.lovepython.lesson1.com" The most general way to do this is with string slicing. You have to build a new string: s = "www.lovepython.com" # find the 1st period i = s.find('.') # and the second: i = s.find('.', i+1) # slice just before the second dot new_s = s[:i] + ".lesson1" + s[i:] print(new_s) > First I thought?row[1]?in the code above > referred to the first place in the beginning of the string. So I tried > to change that number around but it did not work. I have a feeling I > might be mixing few concepts together...?Thank you. row[1] refers to item 1 in the list row, it has nothing to do with dots in the string. The best tool for learning Python is to play with the interactive interpeter. Start up Python to get a command prompt. By default, the prompt is ">>> " but I prefer to use "py> ". Now just type commands as needed. Python will automatically display them, or you can use print. py> row = ["This", "that", "www.lovepython.com.au/", "blah blah"] py> row[0] 'This' py> row[2] 'www.lovepython.com.au/' py> s = row[2] py> s.find(".") # first dot 3 py> s.find(".", 4) # second dot 14 py> s[:14] # slice up to the second dot 'www.lovepython' py> s[14:] # and from the second dot onwards '.com.au/' Here's another way to do it: py> s = row[2] py> L = s.split(".") py> print(L) ['www', 'lovepython', 'com', 'au/'] py> L.insert(2, "lesson999") py> print(L) ['www', 'lovepython', 'lesson999', 'com', 'au/'] py> s = '.'.join(L) py> print(s) www.lovepython.lesson999.com.au/ -- Steve From paul at whoosh.cn Sun Jul 5 06:04:42 2015 From: paul at whoosh.cn (Zhongye) Date: Sun, 05 Jul 2015 12:04:42 +0800 Subject: [Tutor] =?utf-8?q?about_real-time_audio_synthesis_and_algorithmic?= =?utf-8?q?=09composition?= In-Reply-To: mn8425$3pb$1@ger.gmane.org References: <----1X------r5KXX$12dd714d-d504-4f13-922a-fb2a53e68cd7@whoosh.cn>, mn8425$3pb$1@ger.gmane.org Message-ID: <----1X------r5KXX$d81bdde2-13e3-44fc-89a6-1ec2dd8a8747@whoosh.cn> Thank for your helpful replay, I had a rough learning route. I think I need learn some basics via the book firstly, and try to write some code for sound using the standard library. I have check the link you send to me, I'm interested in the PyGame package, and I found that the Audio Programming Frameworks seem not very active? Now, I think I already start to learn Python in right route. :-) ------------------------------------------------------------------ From:Alan Gauld Send Time:2015?7?4?(???) 16:05 To:tutor Subject:Re: [Tutor] about real-time audio synthesis and algorithmic composition On 04/07/15 05:05, Paul wrote: > I'm interested in real-time audio synthesis and > algorithmic composition, There are a few options for working with audio in the standard library - winsound, aifc,wave etc. The PyGame package also has some audio capabilities. And finally the Scikit project has some audio packages too. There is a wiki page on the subject here: https://wiki.python.org/moin/PythonInMusic 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 _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From paul at whoosh.cn Sun Jul 5 06:18:34 2015 From: paul at whoosh.cn (Zhongye) Date: Sun, 05 Jul 2015 12:18:34 +0800 Subject: [Tutor] =?utf-8?q?about_real-time_audio_synthesis_and_algorithmic?= =?utf-8?q?_composition?= In-Reply-To: 201507050032.t650Werq022167@theraft.openend.se References: ----1X------r5KXX$7a16e2c2-6696-444d-b940-d3559e0df81b@whoosh.cn<----1X------r5KXX$12dd714d-d504-4f13-922a-fb2a53e68cd7@whoosh.cn>, 201507050032.t650Werq022167@theraft.openend.se Message-ID: <----1X------r5KXX$8c87c5de-ecff-4ea5-a203-dbe56e7c85db@whoosh.cn> Thanks for your helpful reply. I have check them, that it control SuperCollider via OSC (Open Sound Control), and it provides with a simpler API to use those libraries. Is that mean if I write some python scripts importing it, and the others want to use my scripts, they have to also install the SuperCollider? ------------------------------------------------------------------ From:Laura Creighton Send Time:2015?7?5?(???) 08:32 To:Paul Cc:tutor ?lac Subject:Re: [Tutor] about real-time audio synthesis and algorithmic composition In a message of Sat, 04 Jul 2015 12:05:47 +0800, "Paul" writes: >Hi ! >I'm a rookie to programming, and just graduated from a conservatory of music last year. I'm interested in real-time audio synthesis and algorithmic composition, I can write a little bit SuperCollider and Django. I just buy a book called "Python Programming for the Absolute Beginner" (Chinese version), and try to learn Python via it. > >I have a query, Is there have a way (e.g. import some library) to synthesis sound in Python as SuperCollider? (synthesis sound via write some code in a file, and run it will hear the result immediately.) > >I'm realy like Python's style, and want to learn Programming from Python. If I can learn it through synthesis sound, I will be very happy. :-) In addition, I think if I can synthesis sound via Python Code, I will have more chance to cooperate with other programer. :-) > >Thanks! >Paul Yeh There are python bindings for supercollider. I've never used them. http://supercollider.sourceforge.net/wiki/index.php/Systems_interfacing_with_SC lists 3 and I think this is a 4th. https://pypi.python.org/pypi/SC/0.2 You can also get it through Blender. http://blenderscripting.blogspot.se/2014/11/blender-pynodes-and-supercollider-and.html I've done this. It works. Best of luck, write back if you have any problems, Laura From lac at openend.se Sun Jul 5 12:46:22 2015 From: lac at openend.se (Laura Creighton) Date: Sun, 05 Jul 2015 12:46:22 +0200 Subject: [Tutor] =?utf-8?q?about_real-time_audio_synthesis_and_algorithmic?= =?utf-8?q?=09composition?= In-Reply-To: Message from "Zhongye" of "Sun, 05 Jul 2015 12:04:42 +0800." <----1X------r5KXX$d81bdde2-13e3-44fc-89a6-1ec2dd8a8747@whoosh.cn> References: <----1X------r5KXX$12dd714d-d504-4f13-922a-fb2a53e68cd7@whoosh.cn>, mn8425$3pb$1@ger.gmane.org<----1X------r5KXX$d81bdde2-13e3-44fc-89a6-1ec2dd8a8747@whoosh.cn> Message-ID: <201507051046.t65AkM5j013110@fido.openend.se> In a message of Sun, 05 Jul 2015 12:04:42 +0800, "Zhongye" writes: >Thank for your helpful replay, I had a rough learning route. >I think I need learn some basics via the book firstly, and try to write some code for sound using the standard library. > >I have check the link you send to me, I'm interested in the PyGame package, and I found that the Audio Programming Frameworks seem not very active? >Now, I think I already start to learn Python in right route. :-) > Definitely learning Python is the right first step. Blender development is very active. Laura From nymcity at yahoo.com Sun Jul 5 18:02:13 2015 From: nymcity at yahoo.com (Nym City) Date: Sun, 5 Jul 2015 16:02:13 +0000 (UTC) Subject: [Tutor] Loop not iterating In-Reply-To: <20150705060844.GT10773@ando.pearwood.info> References: <20150705060844.GT10773@ando.pearwood.info> Message-ID: <250400993.2776574.1436112133456.JavaMail.yahoo@mail.yahoo.com> This is pretty cool, Thank you for explaining! I liked the second solution.?Thank you. On Sunday, July 5, 2015 2:10 AM, Steven D'Aprano wrote: Hi Nym, sorry your code's formatting is broken again. I've tried my best to fix it below: On Fri, Jul 03, 2015 at 09:04:10PM +0000, Nym City via Tutor wrote: > Thank to very much for replying. ?The second solution that you proposed worked perfectly: > > import csv > domains = open('top500domains.csv') > domainsReader = csv.reader(domains) > domains = ["https://www." + row[1] for row in domainsReader] > for domain in domains: >? ? print(domain)? > > The above solution is perfect and simple. It allows me to easily > insert text such as?"https://www."??in the beginning of my strings or > at the end. > > However, something else that came to mind was how would you break the > string and insert new text in the middle. For > example:"www.lovepython.com" I want to insert "lesson1." after the > second period above. So it would come back as: > "www.lovepython.lesson1.com" The most general way to do this is with string slicing. You have to build a new string: s = "www.lovepython.com" # find the 1st period i = s.find('.') # and the second: i = s.find('.', i+1) # slice just before the second dot new_s = s[:i] + ".lesson1" + s[i:] print(new_s) > First I thought?row[1]?in the code above > referred to the first place in the beginning of the string. So I tried > to change that number around but it did not work. I have a feeling I > might be mixing few concepts together...?Thank you. row[1] refers to item 1 in the list row, it has nothing to do with dots in the string. The best tool for learning Python is to play with the interactive interpeter. Start up Python to get a command prompt. By default, the prompt is ">>> " but I prefer to use "py> ". Now just type commands as needed. Python will automatically display them, or you can use print. py> row = ["This", "that", "www.lovepython.com.au/", "blah blah"] py> row[0] 'This' py> row[2] 'www.lovepython.com.au/' py> s = row[2] py> s.find(".")? # first dot 3 py> s.find(".", 4)? # second dot 14 py> s[:14]? # slice up to the second dot 'www.lovepython' py> s[14:]? # and from the second dot onwards '.com.au/' Here's another way to do it: py> s = row[2] py> L = s.split(".") py> print(L) ['www', 'lovepython', 'com', 'au/'] py> L.insert(2, "lesson999") py> print(L) ['www', 'lovepython', 'lesson999', 'com', 'au/'] py> s = '.'.join(L) py> print(s) www.lovepython.lesson999.com.au/ -- Steve _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From crusier at gmail.com Mon Jul 6 09:44:32 2015 From: crusier at gmail.com (Crusier) Date: Mon, 6 Jul 2015 15:44:32 +0800 Subject: [Tutor] String Problem Message-ID: Dear All, I have used the urllib.request and download some of the information from a site. I am currently using Python 3.4. My program is as follows: import urllib.request response = urllib.request.urlopen(' http://www.hkex.com.hk/eng/ddp/Contract_Details.asp?PId=175') saveFile = open('HKEX.txt','w') saveFile.write(str(response.read())) saveFile.close() And the result is as follows: d align="right"> - 08.56N/A1\r\n\t\t\t\t\t\t\t\tC Jul-15 - 23.00 - - 0.01 - - - 00.01N/A467\r\n\t\t\t\t\t\t\t\tP Jul-15 - 23.00 - - - - - - 09.56N/A0\r\n\t\t\t\t\t\t\t\tC Jul-15 - 24.00 - - 0.01 - - - 00.01N/A156\r\n\t\t\t\t\t\t\t\tP Jul-15 - 24.00 - - - - - - 010.56N/A0\r\n\t\t\t\t\t\t\t\tC Jul-15 - 25.00 - - 0.01 - - - 00.01N/A6\r\n\t\t\t\t\t\t\t\tP Jul-15 - 25.00 - - - - - - 011.56N/A0\r\n\t\t\t\t\t\t\t\tC Aug-15 - 8.75 - - - - - - 04.71N/A0\r\n\t\t\t\t\t\t\t\tP Aug-15 - 8.75 - 0.030.05 - - - 00.01N/A35\r\n\t\t\t\t\t\t\t\tC Aug-15 - 9.00 - - - - References: Message-ID: On 06/07/2015 08:44, Crusier wrote: > Dear All, > > I have used the urllib.request and download some of the information from a > site. > > I am currently using Python 3.4. My program is as follows: > > import urllib.request > > response = urllib.request.urlopen(' > http://www.hkex.com.hk/eng/ddp/Contract_Details.asp?PId=175') > > saveFile = open('HKEX.txt','w') > saveFile.write(str(response.read())) > saveFile.close() > > > > And the result is as follows: [snipped] > > Please let me know how to deal with this string. I hope I could put onto a > table first. Eventually, I am hoping that I can able to put all this > database. I need some guidance of which area of coding I should look into. > > Thank you > Hank > Start here https://docs.python.org/3/library/html.parser.html#example-html-parser-application -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From cs at zip.com.au Mon Jul 6 10:19:45 2015 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 6 Jul 2015 18:19:45 +1000 Subject: [Tutor] String Problem In-Reply-To: References: Message-ID: <20150706081945.GA10182@cskk.homeip.net> On 06Jul2015 15:44, Crusier wrote: >Dear All, > >I have used the urllib.request and download some of the information from a >site. > >I am currently using Python 3.4. My program is as follows: > >import urllib.request > >response = urllib.request.urlopen(' >http://www.hkex.com.hk/eng/ddp/Contract_Details.asp?PId=175') > >saveFile = open('HKEX.txt','w') >saveFile.write(str(response.read())) >saveFile.close() > >And the result is as follows: > >d align="right"> - 0Please let me know how to deal with this string. I hope I could put onto a >table first. Eventually, I am hoping that I can able to put all this >database. I need some guidance of which area of coding I should look into. Look into the BeautifulSoup library, which will parse HTML. That will let you locate the TABLE element and extract the content by walking the rows (TR) and cells (TD). Start here: http://www.crummy.com/software/BeautifulSoup/bs4/doc/ You can install bs4 using pip, or in other ways: http://www.crummy.com/software/BeautifulSoup/bs4/doc/#installing-beautiful-soup Cheers, Cameron Simpson 30 years ago, I made some outrageous promises about AI. I didn't deliver. Neither did you. This is all your fault. - Marvin Minsky, IJCAI'91 (summary) From carydeveloper at gmail.com Mon Jul 6 16:24:43 2015 From: carydeveloper at gmail.com (Cary Developer) Date: Mon, 6 Jul 2015 10:24:43 -0400 Subject: [Tutor] python help Message-ID: <008201d0b7f7$808b1cb0$81a15610$@com> I am looking for help on getting started with Python. This link says it all: http://raleigh.craigslist.org/cpg/5108772711.html Any help (and response to the CL post) would be truly appreciated. Thanks. -Roger From suresh852456 at gmail.com Mon Jul 6 15:55:10 2015 From: suresh852456 at gmail.com (Suresh Nagulavancha) Date: Mon, 6 Jul 2015 19:25:10 +0530 Subject: [Tutor] Variable reference Message-ID: <559a88cb.21cd440a.07ae.1c0c@mx.google.com> Hello everyone I want to know about the variables dereference Code is in python 27 Let my variable be foo="hello python" Print foo del foo What del command here actually doing , is it dereferencing or deleting the variable along with value it stored? Thank you Suresh Nagulavancha From alan.gauld at btinternet.com Mon Jul 6 22:42:23 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 06 Jul 2015 21:42:23 +0100 Subject: [Tutor] python help In-Reply-To: <008201d0b7f7$808b1cb0$81a15610$@com> References: <008201d0b7f7$808b1cb0$81a15610$@com> Message-ID: On 06/07/15 15:24, Cary Developer wrote: > I am looking for help on getting started with Python. This link says it > all: > > http://raleigh.craigslist.org/cpg/5108772711.html It would be more helpful to post the content of the query, not all mail subscribers can access web pages at the time of reading the mail. > Any help (and response to the CL post) would be truly appreciated. Thanks. Since you are experienced in web development and PHP/SQL you should go straight to the official Python tutorial. It will take you through the basics in a few hours - less than a full day for sure. You could then look at the Django tutorial if you want to go down the web route. (Other frameworks exist - lots of them - but Django is powerful, and very popular and has good support from its own community) Python 3.4 is the preferred version for newbies these days unless you know that your target framework/toolset only runs on v2 If you have more specific questions ask here... -- 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 Jul 6 22:44:38 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 06 Jul 2015 21:44:38 +0100 Subject: [Tutor] Variable reference In-Reply-To: <559a88cb.21cd440a.07ae.1c0c@mx.google.com> References: <559a88cb.21cd440a.07ae.1c0c@mx.google.com> Message-ID: On 06/07/15 14:55, Suresh Nagulavancha wrote: > Hello everyone > I want to know about the variables dereference > Code is in python 27 > Let my variable be > foo="hello python" > Print foo > del foo > What del command here actually doing Python variables are stored internally in a dictionary. del removes the name/value pair from the dictionary. If there are no more references to the object then the garbage collector will eventually remove it from memory. But you shouldn't need to worry too much about 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 breamoreboy at yahoo.co.uk Mon Jul 6 22:52:16 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 06 Jul 2015 21:52:16 +0100 Subject: [Tutor] python help In-Reply-To: <008201d0b7f7$808b1cb0$81a15610$@com> References: <008201d0b7f7$808b1cb0$81a15610$@com> Message-ID: On 06/07/2015 15:24, Cary Developer wrote: Welcome. > I am looking for help on getting started with Python. You've come to the right place, that's always a good start. > > This link says it all: > > http://raleigh.craigslist.org/cpg/5108772711.html > > Any help (and response to the CL post) would be truly appreciated. Thanks. > > -Roger For someone with programming experience try this http://www.diveintopython3.net/ Anybody suggesting to you starting with Django before they've learnt basic Python needs a really good psychiatrist, although I understand that they're rather more expensive in the USA than the UK :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From matt.williams45.mw at gmail.com Mon Jul 6 23:09:10 2015 From: matt.williams45.mw at gmail.com (Matt Williams) Date: Mon, 6 Jul 2015 22:09:10 +0100 Subject: [Tutor] python help In-Reply-To: <008201d0b7f7$808b1cb0$81a15610$@com> References: <008201d0b7f7$808b1cb0$81a15610$@com> Message-ID: Personally I would start with Python 2.7, and start with simple scripts. The standard library in Python is very "wide", and having a good understanding of what is already there is very useful. As to GUI/ Web/ etc. - I think it depends on what you want to do. However, you will need the basics before then. You don't say what your background is, but if you've done some programming before then the basics should be pretty quick. Once you've done the basics, some more "intermediate" level stuff is useful. Personally, I find reading source code useful (there is a tonne on the PPI). There are some other resources listed here: https://news.ycombinator.com/item?id=5998750 HTH, M On 6 July 2015 at 15:24, Cary Developer wrote: > I am looking for help on getting started with Python. This link says it > all: > > > > http://raleigh.craigslist.org/cpg/5108772711.html > > > > Any help (and response to the CL post) would be truly appreciated. Thanks. > > > > -Roger > > > > _______________________________________________ > 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 Tue Jul 7 00:30:30 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 06 Jul 2015 23:30:30 +0100 Subject: [Tutor] python help In-Reply-To: References: <008201d0b7f7$808b1cb0$81a15610$@com> Message-ID: On 06/07/2015 22:09, Matt Williams wrote: > Personally I would start with Python 2.7, and start with simple scripts. > I think it makes much more sense to learn Python 3 and if you need code to run on both 2 and 3 take the advice here https://docs.python.org/3/howto/pyporting.html By the way, please don't top post here, in can get irritating trying to follow long threads, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From dyoo at hashcollision.org Tue Jul 7 02:18:16 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 6 Jul 2015 17:18:16 -0700 Subject: [Tutor] Variable reference In-Reply-To: References: <559a88cb.21cd440a.07ae.1c0c@mx.google.com> Message-ID: I'd also add that the 'del' statement has near-zero utility. 'del' is a language blemish. It should not be used by beginners, because it asks them to try to manually manage the lifetime of their variable names. That's an unreasonable and ridiculous burden. Functions have local variables for a reason. From alan.gauld at btinternet.com Tue Jul 7 02:38:01 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 07 Jul 2015 01:38:01 +0100 Subject: [Tutor] Variable reference In-Reply-To: References: <559a88cb.21cd440a.07ae.1c0c@mx.google.com> Message-ID: On 07/07/15 01:18, Danny Yoo wrote: > I'd also add that the 'del' statement has near-zero utility. > > 'del' is a language blemish. It should not be used by beginners, > because it asks them to try to manually manage the lifetime of their > variable names. That's an unreasonable and ridiculous burden. > Functions have local variables for a reason. I don't know that I'd go that far. There are valid uses for it in deleting things from dictionaries and the like. But I agree its not needed very often and can lead to users over-managing their data. -- 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 Tue Jul 7 03:58:46 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 7 Jul 2015 11:58:46 +1000 Subject: [Tutor] Variable reference In-Reply-To: <559a88cb.21cd440a.07ae.1c0c@mx.google.com> References: <559a88cb.21cd440a.07ae.1c0c@mx.google.com> Message-ID: <20150707015845.GA10773@ando.pearwood.info> On Mon, Jul 06, 2015 at 07:25:10PM +0530, Suresh Nagulavancha wrote: > Hello everyone > I want to know about the variables dereference First you need to know how variables reference. When you assign a value to a variable, we say that we "bind the value to the variable's name": spam = 42 tells Python to bind 42 to the name "spam", which associates the value 42 with the name "spam". Every program has a global scope and local scopes for each function. We call those "namespaces", and that is where Python tracks the association between names and values. In practice, Python often uses a dict for such namespaces, but not always. 99.9% of the time, you don't need to care about that, just let Python manage the variable names. > Code is in python 27 There is no Python 27. I think you mean "Python 2.7" (two point seven). > Let my variable be > foo="hello python" > Print foo That is a syntax error. As a programmer, you must be precise and accurate about what you say. "foo" and "Foo" and "FOO" are not the same thing, neither is "print" and "Print" and "PRINT". > del foo > What del command here actually doing , is it dereferencing or deleting the variable along with value it stored? del unbinds the value from the name and removes the name from the current namespace. To put it another way, it deletes *the variable* but not the value. Here is another example: spam = "Hello world!" eggs = spam # these 2 lines can be written as 1: spam = eggs = "Hello world!" At this point, there are two references to the string "Hello world!": the two names (variables), "spam" and "eggs". We can print them, pass them to functions, etc. del spam This removes the binding from variable "spam" to the string. The string itself is not deleted, only the name binding ("the variable spam"). At this point, we can still write: print eggs which is okay, because the variable eggs still exists and is still bound to the string. But this line: print spam raises an exception, since the variable spam no longer exists. The string itself, "Hello world!", is not deleted until the last reference to it is gone. Hope this is clear. -- Steve From steve at pearwood.info Tue Jul 7 04:07:44 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 7 Jul 2015 12:07:44 +1000 Subject: [Tutor] Variable reference In-Reply-To: References: <559a88cb.21cd440a.07ae.1c0c@mx.google.com> Message-ID: <20150707020744.GB10773@ando.pearwood.info> On Mon, Jul 06, 2015 at 05:18:16PM -0700, Danny Yoo wrote: > I'd also add that the 'del' statement has near-zero utility. > > 'del' is a language blemish. It should not be used by beginners, > because it asks them to try to manually manage the lifetime of their > variable names. That's an unreasonable and ridiculous burden. > Functions have local variables for a reason. Not all variables are local variables, and del exists to manage more than just name bindings. Deleting attributes and items from sequences are good uses for it, as is deleting global names which are not needed. You are right that del should not, generally, be used by beginners, and especially not for manually managing names. Fortunately, beginners are not usually inclined to write code like this: def spam(s): a = s.upper() b = s + "s" process(a, b) return do_something_else(a, b) del a, b, s as that would be both pointless and silly. Not only is the del line never reached by Python, but the local variables are automatically deleted when the function returns, so it is a waste of programmer time and effort to manually delete them. -- Steve From sahil4u2u at gmail.com Tue Jul 7 04:16:04 2015 From: sahil4u2u at gmail.com (Sahil Chauhan) Date: Mon, 6 Jul 2015 22:16:04 -0400 Subject: [Tutor] Pytest help Message-ID: Hi, I am trying to use py.test for writing some selenium webdriver tests. I wrote my first test and pytest is deselecting that test even though I didn't mark any test. How can I resolve this issue? Is there some default setting used by py.test, (qa)MacBook-Air:$ py.test tests/test_login.py =========================================================================================== test session starts =========================================================================================== platform darwin -- Python 2.7.6 -- pytest-2.2.4 collected 1 items =============================================================================== *1 tests deselected by "-m 'nondestructive'" * =============================================================================== ====================================================================================== 1 deselected in 0.01 seconds ============== Thanks in advance! Sahil From oscar.j.benjamin at gmail.com Tue Jul 7 11:59:56 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 07 Jul 2015 09:59:56 +0000 Subject: [Tutor] Pytest help In-Reply-To: References: Message-ID: On Tue, 7 Jul 2015 at 09:51 Sahil Chauhan wrote: > Hi, > > I am trying to use py.test for writing some selenium webdriver tests. I > wrote my first test and > pytest is deselecting that test even though I didn't mark any test. > > How can I resolve this issue? Is there some default setting used by > py.test, > > I don't know as that's never happened to me and you haven't shown any code for me to check. Try breaking your problem down to a small example module+test that still exhibits the same behaviour. You may find that you solve the problem in the process. If not then post the small example code here so that someone else can see what the problem is. -- Oscar From breamoreboy at yahoo.co.uk Tue Jul 7 12:39:51 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 07 Jul 2015 11:39:51 +0100 Subject: [Tutor] Pytest help In-Reply-To: References: Message-ID: On 07/07/2015 03:16, Sahil Chauhan wrote: > Hi, > > I am trying to use py.test for writing some selenium webdriver tests. I > wrote my first test and > pytest is deselecting that test even though I didn't mark any test. > > How can I resolve this issue? Is there some default setting used by > py.test, > > (qa)MacBook-Air:$ py.test tests/test_login.py > =========================================================================================== > test session starts > =========================================================================================== > platform darwin -- Python 2.7.6 -- pytest-2.2.4 > collected 1 items > > =============================================================================== > *1 tests deselected by "-m 'nondestructive'" * > =============================================================================== > ====================================================================================== > 1 deselected in 0.01 seconds ============== > > Thanks in advance! > Sahil > Hopefully this helps https://pytest.org/latest/example/markers.html based on your reference to "selenium webdriver tests" and its reference to "@pytest.mark.webtest". -- 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 Tue Jul 7 12:58:16 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 07 Jul 2015 11:58:16 +0100 Subject: [Tutor] Pytest help In-Reply-To: References: Message-ID: On 07/07/15 03:16, Sahil Chauhan wrote: > Hi, > > I am trying to use py.test for writing some selenium webdriver tests. I > wrote my first test and > pytest is deselecting that test even though I didn't mark any test. > > How can I resolve this issue? Is there some default setting used by > py.test, > The best place to ask for help on any 3rd party package is on the package's own support forum/list. Py.test has several options: https://pytest.org/latest/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 paradox at pobox.com Tue Jul 7 15:52:56 2015 From: paradox at pobox.com (Thomas C. Hicks) Date: Tue, 7 Jul 2015 21:52:56 +0800 Subject: [Tutor] Pytest help :p: In-Reply-To: References: Message-ID: <559BD9B8.3090005@pobox.com> On 07/07/2015 06:58 PM, Alan Gauld wrote: > > The best place to ask for help on any 3rd party package is on the > package's own support forum/list. Py.test has several options: > > https://pytest.org/latest/contact.html The Testing In Python mailing list is outstanding, civil, knowledgable people really wanting to help. ============================== Thomas C. Hicks, MD, MPH Training Manager, Gansu Gateway Lanzhou, Gansu, PR China From __peter__ at web.de Tue Jul 7 16:08:30 2015 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Jul 2015 16:08:30 +0200 Subject: [Tutor] Variable reference References: <559a88cb.21cd440a.07ae.1c0c@mx.google.com> Message-ID: Alan Gauld wrote: > On 07/07/15 01:18, Danny Yoo wrote: >> I'd also add that the 'del' statement has near-zero utility. >> >> 'del' is a language blemish. It should not be used by beginners, >> because it asks them to try to manually manage the lifetime of their >> variable names. That's an unreasonable and ridiculous burden. >> Functions have local variables for a reason. > > I don't know that I'd go that far. There are valid uses for > it in deleting things from dictionaries and the like. For dicts and lists a method would work as well. Even now you can write items.pop(index) # instead of del items[index] lookup.pop(key) # del lookup[key] If you find the name pop() random or hard to discover a delete() method could be added. globals().pop("name") # instead of del name in the global namespace delattr(obj, "name") # del obj.name For the above the replacement is less elegant, but I don't think it matters for a rarely used feature. So for everything but local and nonlocal names del is syntactic sugar at best. > But I agree its not needed very often and can lead to > users over-managing their data. From steve at pearwood.info Tue Jul 7 17:45:34 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 8 Jul 2015 01:45:34 +1000 Subject: [Tutor] Variable reference In-Reply-To: References: <559a88cb.21cd440a.07ae.1c0c@mx.google.com> Message-ID: <20150707154534.GE10773@ando.pearwood.info> On Tue, Jul 07, 2015 at 04:08:30PM +0200, Peter Otten wrote: > For dicts and lists a method would work as well. Even now you can write > > items.pop(index) # instead of del items[index] > lookup.pop(key) # del lookup[key] > > If you find the name pop() random or hard to discover a delete() method > could be added. > > globals().pop("name") # instead of del name in the global namespace > delattr(obj, "name") # del obj.name > > For the above the replacement is less elegant, but I don't think it matters > for a rarely used feature. So for everything but local and nonlocal names > del is syntactic sugar at best. Not so. The point of del being a statement is that it should be considered an operation on the *reference*, not the *value* of the reference. So: x = 23 delete(x) # if it existed, it would see the value 23 del x # operates on the reference "x", not 23 We can work around this by quoting the variable name: delete("x") # this could work but that is not an elegant design. It's a work-around for the fact that Python doesn't have dedicated syntax to say "operate on the reference foo" rather than the value of foo. In Python, I think there are only two operations on references themselves: binding, and unbinding. For some purposes, we can consider unbinding just a special case of binding. (For example, adding a `del spam` line to a function makes spam a local, just as assigning to it would.) All binding operations are firstly statements, not function calls: x = 23 # not assign("x", 23) import spam for eggs in sequence with expr as cheese except SomeError as foo and del is no exception. For some of these, there are functional versions: setattr, delattr come to mind, but I don't think there are many others. dict.pop and similiar are not conceptually the same, as they don't operate on references, they operate on keys, indexes, names as strings, etc. I acknowledge that there is some overlap between the two, and one can replace the other (at least sometimes), but conceptually they operate in different spheres. -- Steve From __peter__ at web.de Tue Jul 7 18:50:25 2015 From: __peter__ at web.de (Peter Otten) Date: Tue, 07 Jul 2015 18:50:25 +0200 Subject: [Tutor] Variable reference References: <559a88cb.21cd440a.07ae.1c0c@mx.google.com> <20150707154534.GE10773@ando.pearwood.info> Message-ID: Steven D'Aprano wrote: > On Tue, Jul 07, 2015 at 04:08:30PM +0200, Peter Otten wrote: > >> For dicts and lists a method would work as well. Even now you can write >> >> items.pop(index) # instead of del items[index] >> lookup.pop(key) # del lookup[key] >> >> If you find the name pop() random or hard to discover a delete() method >> could be added. >> >> globals().pop("name") # instead of del name in the global namespace >> delattr(obj, "name") # del obj.name >> >> For the above the replacement is less elegant, but I don't think it >> matters for a rarely used feature. So for everything but local and >> nonlocal names del is syntactic sugar at best. > > Not so. The point of del being a statement is that it should be > considered an operation on the *reference*, not the *value* of the > reference. So: > > x = 23 > delete(x) # if it existed, it would see the value 23 > del x # operates on the reference "x", not 23 Read again. I said that del x in the global namespace can be emulated with globals().pop("x") and that there is no equivalent to del x if x is a local/nonlocal name. > > We can work around this by quoting the variable name: > > delete("x") # this could work > > but that is not an elegant design. I agree that if you think that explicitly unbinding a name is a useful feature of the language a statement is the way to go. For me it's a feature I hardly ever use and that I hardly ever find compelling in other people's code. I'd happily resort to x = None should the need arise to dereference a specific variable. > It's a work-around for the fact that > Python doesn't have dedicated syntax to say "operate on the reference > foo" rather than the value of foo. I think Danny's point was that you should not micromanage name bindings at all. Then Alan added that del is useful for dicts etc. on which I replied that a method would be sufficient for that. > In Python, I think there are only two operations on references > themselves: binding, and unbinding. For some purposes, we can consider > unbinding just a special case of binding. (For example, adding a `del > spam` line to a function makes spam a local, just as assigning to it > would.) All binding operations are firstly statements, not function > calls: > > x = 23 # not assign("x", 23) > import spam > for eggs in sequence > with expr as cheese > except SomeError as foo > > > and del is no exception. For some of these, there are functional > versions: setattr, delattr come to mind, but I don't think there are > many others. dict.pop and similiar are not conceptually the same, as > they don't operate on references, they operate on keys, indexes, names > as strings, etc. > > I acknowledge that there is some overlap between the two, and one can > replace the other (at least sometimes), but conceptually they operate in > different spheres. I'd put that the other way round: allowing both del a["x"] # invokes a method on `a` and del x # manipulates a namespace glosses over the "conceptual difference"; if a clean approach were the goal only the latter should be allowed. From dyoo at hashcollision.org Tue Jul 7 23:01:48 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 7 Jul 2015 14:01:48 -0700 Subject: [Tutor] Variable reference In-Reply-To: References: <559a88cb.21cd440a.07ae.1c0c@mx.google.com> <20150707154534.GE10773@ando.pearwood.info> Message-ID: >> It's a work-around for the fact that >> Python doesn't have dedicated syntax to say "operate on the reference >> foo" rather than the value of foo. > > I think Danny's point was that you should not micromanage name bindings at > all. Then Alan added that del is useful for dicts etc. on which I replied > that a method would be sufficient for that. Yes. Apologies for not being clear. I was thinking of the two recent uses of 'del' showing up on the mailing list in the past week or so. Both of the uses, from different authors, were trying to manually manage name bindings. It made the code harder to understand. In both cases, both uses of 'del' were ineffective. When I'm reading code, I want to know statically what my variables are. By statically, I mean that I should be able to tell, just be reading the code, what the code means, without running it. "What variables are accessible?" is one of the most basic questions I ask myself when I'm reading code. But if we use 'del' on name bindings, that makes the set of accessible variables a dynamic property that, in the most general case, requires us to run the code to figure it out. When I'm trading a static property for a dynamic property, I want to get a useful amount of power for that tradeoff, because it's costly. In my opinion, 'del' on a name binding is just not useful enough to be worth that conceptual cost. 'del' to remove attributes or dictionary key/value pairs is a different matter. I want to constrain my objections to 'del' specifically to its use on name bindings. That all being said: wow. I'm getting a heck of a lot more curmudgeon-y these days. :P From steve at pearwood.info Wed Jul 8 02:54:24 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 8 Jul 2015 10:54:24 +1000 Subject: [Tutor] Variable reference In-Reply-To: References: <559a88cb.21cd440a.07ae.1c0c@mx.google.com> <20150707154534.GE10773@ando.pearwood.info> Message-ID: <20150708005424.GF10773@ando.pearwood.info> On Tue, Jul 07, 2015 at 06:50:25PM +0200, Peter Otten wrote: [...] > > Not so. The point of del being a statement is that it should be > > considered an operation on the *reference*, not the *value* of the > > reference. So: > > > > x = 23 > > delete(x) # if it existed, it would see the value 23 > > del x # operates on the reference "x", not 23 > > Read again. I said that > > del x > > in the global namespace can be emulated with > > globals().pop("x") > > and that there is no equivalent to > > del x > > if x is a local/nonlocal name. Yes, I read all that. There *could* (at least in theory) be an equivalent to `del x` using a function, which I called `delete("x")`, that applied to locals/nonlocals as well as globals. It would still not be *close* equivalent to del, because they operate in different spheres, semantically. Using pop() is not quite right, because it doesn't just remove the key:value pair, it *returns the value*. So that's a another difference between popping a key from the globals, and del: del x: - operates on the variable x, not the value of x - unbinds that variable - has no return result (is a statement, not an expression globals().pop("x"): - operates on a value, the string "x", not a variable - conceptually, not an unbinding operation at all - returns the value bound to the variable x This makes a practical difference at the interactive interpreter: popping as two side effects, only one of which is intended: py> a, b = 1, 2 py> del a py> globals().pop("b") 2 It's unlikely to change, since the current semantics of globals() is documented as a language feature, but in principle at least a Python implementation might optimize the language by making globals() more like locals(), i.e. calling globals() returns a *copy* of the global namespace, not the namespace itself. As I said, this is unlikely to change without a period of deprecation, but still, del is *intended* to unbind variables, pop is not. The fact that globals().pop also unbinds them is an accident of the way manages globals. For all these reasons, if I saw globals().pop("x") in code I was reviewing, I would change it to `del x` without hesitation. Not withstanding the fact that we *can* replace del with pop as above, we shouldn't. [...] > I agree that if you think that explicitly unbinding a name is a useful > feature of the language a statement is the way to go. For me it's a feature > I hardly ever use and that I hardly ever find compelling in other people's > code. I completely agree that del is rarely useful! But *rare* is not *never*. There are two compelling cases for using del on names: explicitly managing the names in a namespace (i.e. to avoid "namespace pollution") and avoiding long-lived global references to objects which you no longer need. I agree that using del on a local variable is probably unnecessary micro-management. I can't think of any scenario where you would want to manually del a local variable. If you did, that's possibly a sign that your function is too big. When writing a script or application, name management is not a big deal. But in a library, temporary variables are pollution. They make it harder for the users of your library to tell what's part of the API and what isn't, and they make "from module import *" less useful. So if I have a temporary global variable, I may want to get rid of it once I'm finished with it. Here's a snippet from a library module I have, designed to be used with * imports: tmp = set(C0.keys()) & set(C1.keys()) assert not tmp, 'duplicate control code acronyms: %s' % tmp # Special check for SCG abbreviated acronym. assert 'SGC' not in C0 assert 'SGC' not in C1 # Validate that the ^ and ESC codes are correct. for C in (C0, C1): for cc in C.values(): assert cc.code == _code(cc.ordinal), 'failed check: %s' % cc del C, cc, tmp Those three temporary variables, C, cc and tmp, would otherwise hang around forever, polluting the namespace and confusing my module's users. (My module's users, so far, is mostly me, but I'm easily confused.) > I'd happily resort to > > x = None > > should the need arise to dereference a specific variable. Ah, reading that makes me sad, because it looks like you are not getting the difference between a *variable* (a name in a namespace) and the *value* of that variable. `x = None` does not remove the variable from the namespace, it just binds it to None. So it is no substitute for the del statement. > > It's a work-around for the fact that > > Python doesn't have dedicated syntax to say "operate on the reference > > foo" rather than the value of foo. > > I think Danny's point was that you should not micromanage name bindings at > all. Then Alan added that del is useful for dicts etc. on which I replied > that a method would be sufficient for that. Sure, Python *could* have used methods for deleting a key from a dict, or a slice from a list: mydict.delete(key) # like del mydict[key] mylist.delete(1, 20, 3) # like del mylist[1:20:3] But note that introduces a source of API confusion: people may use mylist.remove(1) when they mean delete, or the other way around. Either way, that error is harder to make with the del statement: there is no reasonable way for a person to be confused about whether del mylist[3] removes the 3rd item, or an item with the value 3. With: mylist = [3, 6, 9, 12] mylist.delete(3) it isn't clear whether you end up with [3, 6, 9] or [6, 9, 12]. And when it comes to attributes, we shouldn't use getattr, setattr or delattr with constant arguments: # Yes y = x.spam x.spam = 23 del x.spam # No y = getattr(x, "spam") setattr(x, "spam", 23) delattr(x, "spam") I wouldn't hestitate to replace any of the second set with the version from the first set. -- Steve From __peter__ at web.de Wed Jul 8 14:54:55 2015 From: __peter__ at web.de (Peter Otten) Date: Wed, 08 Jul 2015 14:54:55 +0200 Subject: [Tutor] Variable reference References: <559a88cb.21cd440a.07ae.1c0c@mx.google.com> <20150707154534.GE10773@ando.pearwood.info> <20150708005424.GF10773@ando.pearwood.info> Message-ID: Steven D'Aprano wrote: > When writing a script or application, name management is not a big deal. > But in a library, temporary variables are pollution. They make it harder > for the users of your library to tell what's part of the API and what > isn't, and they make "from module import *" less useful. So if I have a > temporary global variable, I may want to get rid of it once I'm finished > with it. Here's a snippet from a library module I have, designed to be > used with * imports: > > tmp = set(C0.keys()) & set(C1.keys()) > assert not tmp, 'duplicate control code acronyms: %s' % tmp > # Special check for SCG abbreviated acronym. > assert 'SGC' not in C0 > assert 'SGC' not in C1 > # Validate that the ^ and ESC codes are correct. > for C in (C0, C1): > for cc in C.values(): > assert cc.code == _code(cc.ordinal), 'failed check: %s' % cc > del C, cc, tmp > > Those three temporary variables, C, cc and tmp, would otherwise hang > around forever, polluting the namespace and confusing my module's users. > (My module's users, so far, is mostly me, but I'm easily confused.) You are not alone with that design. The first thing that came up in the stdlib was the webbrowser module: $ cd /usr/lib/python3.4 $ find . -name \*.py -print0 | xargs -0 egrep '\bdel\s+(_|[[:alnum:]])+$' | head -n1 ./webbrowser.py: del cmdline $ Here's the relevant part of the code where the author manages to del two out of three helper variables: """ # OK, now that we know what the default preference orders for each # platform are, allow user to override them with the BROWSER variable. if "BROWSER" in os.environ: _userchoices = os.environ["BROWSER"].split(os.pathsep) _userchoices.reverse() # Treat choices in same way as if passed into get() but do register # and prepend to _tryorder for cmdline in _userchoices: if cmdline != '': cmd = _synthesize(cmdline, -1) if cmd[1] is None: register(cmdline, None, GenericBrowser(cmdline), -1) cmdline = None # to make del work if _userchoices was empty del cmdline del _userchoices """ I really don't like that approach; I prefer writing a helper function, properly marked as an implementation detail, e. g. def _setup_userchoices(userchoices=None): """ >>> _setup_userchoices(["one"]) >>> _tryorder[0] 'one' >>> _setup_userchoices(["two", "three"]) >>> _tryorder[:3] ['two', 'three', 'one'] """ if userchoices is None: userchoices = os.environ.get("BROWSER", "").split(os.pathsep) for cmdline in reversed(userchoices): if cmdline != "": cmd = _synthesize(cmdline, -1) if cmd[1] is None: register(cmdline, None, GenericBrowser(cmdline), -1) _setup_userchoices() That is easy to test (if this were for real you'd use unit test, not doctest), and you don't have to decide whether the leftover `cmd` is kept intentionally or by accident, or be careful to avoid that you tread on names that *are* part of the API. That are the advantages for the author/reader of the module. As a user the extra _setup_userchoices() function doesn't bother me at all. From cmgcomsol at gmail.com Thu Jul 9 19:40:54 2015 From: cmgcomsol at gmail.com (George) Date: Thu, 09 Jul 2015 23:10:54 +0530 Subject: [Tutor] How to run same lines of code in different order at runtime Message-ID: <559EB226.5030401@gmail.com> Hello, I have done little piece of work for finding nearest shortest route from one point on a graph to other with connected points(cities to be precise). The way i achieved it is by getting the nearest neighbours in (NE, NE, SE, SW) and then traversing them one by one until i reach my destination in the first instance of recursion. this thing works for me and i am not looking for route solving problems but rather a peculiar problem of running different lines of code arbitarily. The problem with my coding is that it follows a strict route search algorithm that i hardcoded i.e. it first searches NE, then NW, then SE, and finally SW. this code will always find a route but will not be the shortest route which could either start from any direction. So i have to devise a way by which i can run all the posibilities to find the best route. so i have to run 4*3*3*3 runs to find out the shortest route. My code is as below, any way i can achieve it in python without typing all the posibilites. def GetShortestRoute(source:list,destination:City,recursion=0): print("recursion",recursion) #print(source) assert type(source)==list,"Source must be a list of list" assert len(source)>0,"Source must contain atleast 1 item" assert type(source[0])==list,"Sub items of source must be a list" #debug for route in source: print (route) #end Debug #found something foundlist=[] for miniroute in source: if destination in miniroute: print("found",destination," in ",miniroute,":", destination in miniroute) foundlist.append(miniroute) else: print("Not found",destination," in ",miniroute,":", destination in miniroute) #print ("length of found list",len(foundlist),foundlist) #retun the shortest amongst the find shortestRoute=[None,9999999.9] if len(foundlist)>0: for miniroute in foundlist: #print("shortest distance",shortestRoute[1]) distance=calculateRouteDistantce(miniroute) #print("distance calculated",distance) if distance References: <559EB226.5030401@gmail.com> Message-ID: On 09/07/15 18:40, George wrote: > and i am not looking for route solving problems but rather a peculiar > problem of running different lines of code arbitarily. I'm not sure what you mean by that. What is arbitrary? What determines which lines are run? > either start from any direction. So i have to devise a way by which i > can run all the posibilities to find the best route. so i have to run > 4*3*3*3 runs to find out the shortest route. My code is as below, any > way i can achieve it in python without typing all the posibilites. The normal way to deal with this kind of thing is to a) create a function that does the work and b) make it data driven so you can code the choices as data. But i don;t pretend to know what your algorithm would look like so I'll somply make some suggestions to tidy the code below in the hope that they might fire some ideas. > def GetShortestRoute(source:list,destination:City,recursion=0): > print("recursion",recursion) recursion may not be the best solution for this since Pythons recursion limit is not huge (1000 last time I looked) and you could easily hit that with this type of problem. > #print(source) > assert type(source)==list,"Source must be a list of list" > assert len(source)>0,"Source must contain atleast 1 item" > assert type(source[0])==list,"Sub items of source must be a list" Just an observation but you seem very concerned with typechecking. Thats not usually necessary in Python since it will check for incompatible types as you go and raise exceptions. > #found something > foundlist=[] > for miniroute in source: > if destination in miniroute: > print("found",destination," in ",miniroute,":", destination > in miniroute) > foundlist.append(miniroute) > else: > print("Not found",destination," in ",miniroute,":", > destination in miniroute) > > shortestRoute=[None,9999999.9] > if len(foundlist)>0: This is more usually written as if foundlist: > for miniroute in foundlist: > distance=calculateRouteDistantce(miniroute) > if distance shortestRoute[1]=distance > shortestRoute[0]=miniroute > return shortestRoute > duplicatesource=source[:] > for route in source: > lastCity=route[len(route)-1] It would be easierv to access the last element directly rather than looping: lastCity = route[-1] > #the part i do not want to recode everytime to find the shortest route > if lastCity.NE!=None and > isCityInRouteList(lastCity.NE,source)==False: > tmproute=route[:] > tmproute.append(lastCity.NE) > if tmproute not in duplicatesource: > duplicatesource.append(tmproute) You repeat the if clause each time so put it in a function. I'm not sure what you would call it but it will look like: def add_route(direction,source): node = getattr(lastCity,direction) if node and not isCityInRouteList(node,source): tmproute=route[:] tmproute.append(lastCity.NE) if tmproute not in duplicatesource: duplicatesource.append(tmproute) And you can apply it by iterating over a list of 'directions'. for dir in ['NE','NW','SE','SW']: add_route(dir,source) > return GetShortestRoute(duplicatesource,destination,recursion+1) 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 Fri Jul 10 04:52:48 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 10 Jul 2015 12:52:48 +1000 Subject: [Tutor] How to run same lines of code in different order at runtime In-Reply-To: <559EB226.5030401@gmail.com> References: <559EB226.5030401@gmail.com> Message-ID: <20150710025241.GK10773@ando.pearwood.info> On Thu, Jul 09, 2015 at 11:10:54PM +0530, George wrote: > Hello, > > I have done little piece of work for finding nearest shortest route from > one point on a graph to other with connected points(cities to be > precise). The way i achieved it is by getting the nearest neighbours in > (NE, NE, SE, SW) and then traversing them one by one until i reach my > destination in the first instance of recursion. this thing works for me > and i am not looking for route solving problems but rather a peculiar > problem of running different lines of code arbitarily. Generally speaking, pathfinding algorithms that exhaustively check every possible route should not depend on the order that you search. If you don't perform an exhaustive search, then they may. > The problem with my coding is that it follows a strict route search > algorithm that i hardcoded i.e. > it first searches NE, then NW, then SE, and finally SW. this code will > always find a route but will not be the shortest route which could > either start from any direction. You don't have to stop at the first route found, you can keep going and return the shortest route. If you do that, you don't need separate runs: NE NW SE SW NE NW SW SE NE SW NW SE NE SW SE NW ... etc. Just run through them all once, in any direction, and when you find a route, you compare it to any previous route you found, remembering only the shortest. Or return all the routes. > So i have to devise a way by which i > can run all the posibilities to find the best route. so i have to run > 4*3*3*3 runs to find out the shortest route. I think you mean 4*3*2*1. I don't think you actually need to do that, but if you do, one approach is to have a "master search" function that takes a set of directions, then searches in those. Start off with four functions to do a partial search in a particular direction: def search_NE(): # Search in the NE direction ... def search_NW(): ... def search_SE(): ... def search_SW(): ... and a main search that calculates the overall route, if any. There are two approaches, one uses strings (or integers, but I prefer strings) to give the directions: def search(a, b, c, d): # Search in direction a, b, c then d, in that order: directions = { 'SW': search_SW, # note carefully there are no parentheses 'SE': search_SE, 'NW': search_NW, 'NE': search_NE, } # Find the route w = directions[a]() # Look up the function, then call it x = directions[b]() y = directions[c]() z = directions[d]() return route (Obviously, I haven't looked at how you actually combine the functions to give a route. That's up to you.) Then, you can search in all 4! = 24 permutations: import sys from itertools import permutations directions = 'NE SE NW SW'.split() best = (None, sys.maxsize) # In Python 2, use sys.maxint for dirs in permutations(directions, 4): route = search(*dirs) if distance(route) < best[1]: best = (route, distance(route)) print(best) The permutations function will iterate over all of the permutations: ('NE', 'SE', 'NW', 'SW') ('NE', 'SE', 'SW', 'NW') ('NE', 'NW', 'SE', 'SW') ('NE', 'NW', 'SW', 'SE') etc. The other approach is to eshew the intervening strings, and just use the functions themselves: def search(a, b, c, d): # Search using functions a, b, c then d, in that order: w = a() x = b() y = c() z = d() return route # Make sure you *don't* call the functions here. directions = [search_SW, search_NW, search_NE, search_SE] best = (None, sys.maxsize) for dirs in permutations(directions, 4): route = search(*dirs) if distance(route) < best[1]: best = (route, distance(route)) Obviously the above is not a complete solution, but hopefully it will point you in the right direction. Don't hesitate to ask if you have any questions. -- Steve From alan.gauld at btinternet.com Fri Jul 10 11:10:34 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 10 Jul 2015 10:10:34 +0100 Subject: [Tutor] How to run same lines of code in different order at runtime In-Reply-To: References: <559EB226.5030401@gmail.com> Message-ID: On 10/07/15 01:29, Alan Gauld wrote: > It would be easierv to access the last element directly rather than > looping: > > lastCity = route[-1] Oops. looping -> calculating. -- 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 wumeid at hotmail.com Sat Jul 11 15:01:15 2015 From: wumeid at hotmail.com (Michelle Meiduo Wu) Date: Sat, 11 Jul 2015 09:01:15 -0400 Subject: [Tutor] Access HBase Message-ID: Hi there, I'm just starting to use Python. I'd like to ask which Library is good for Python to access HBASE? Besides Happybase and Python HBase, is there any other one? Which is more robotic and better functional? Any advice would be greatly appreciated. Thank you, Michelle From lac at openend.se Sat Jul 11 19:53:19 2015 From: lac at openend.se (Laura Creighton) Date: Sat, 11 Jul 2015 19:53:19 +0200 Subject: [Tutor] Access HBase In-Reply-To: Message from Michelle Meiduo Wu of "Sat, 11 Jul 2015 09:01:15 -0400." References: Message-ID: <201507111753.t6BHrJcj029406@fido.openend.se> In a message of Sat, 11 Jul 2015 09:01:15 -0400, Michelle Meiduo Wu writes: >Hi there, >I'm just starting to use Python. I'd like to ask which Library is good for Python to access HBASE? > >Besides Happybase and Python HBase, is there any other one? Which is more robotic and better functional? > >Any advice would be greatly appreciated. > >Thank you, >Michelle One thing that people I know of are doing is connecting using Jython, instead of CPython. see: http://wiki.apache.org/hadoop/Hbase/Jython They say it is a _lot_ faster. Laura From carolynncsf at yahoo.com Sun Jul 12 03:35:38 2015 From: carolynncsf at yahoo.com (c.fryer) Date: Sun, 12 Jul 2015 01:35:38 +0000 (UTC) Subject: [Tutor] Not very nice Marilyn Davis person References: <1380765706.55390.YahooMailNeo@web161002.mail.bf1.yahoo.com> Message-ID: why is this listed under my name? C.ya! > From dyoo at hashcollision.org Sun Jul 12 03:55:47 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sat, 11 Jul 2015 18:55:47 -0700 Subject: [Tutor] Not very nice Marilyn Davis person In-Reply-To: References: <1380765706.55390.YahooMailNeo@web161002.mail.bf1.yahoo.com> Message-ID: On Sat, Jul 11, 2015 at 6:35 PM, c.fryer via Tutor wrote: > > why is this listed under my name? C.ya! Hi C Fryer, A good rule of thumb, when working with communities of people with incomplete knowledge, is to give folks the benefit of the doubt. Email is an information-poor medium for communication between emotional humans, but it's fast and convenient. As to what you're talking about above, I have no idea what you're referring to here. I look at the archives: https://mail.python.org/pipermail/tutor/2015-July/date.html and I see no recent events that might be applicable to your complaint. I'll assume for the moment that there's been a simple mistake or mixup. If you can explain further what is bothering you, that will help. From wumeid at hotmail.com Sun Jul 12 05:46:56 2015 From: wumeid at hotmail.com (Michelle Meiduo Wu) Date: Sat, 11 Jul 2015 23:46:56 -0400 Subject: [Tutor] Access HBase In-Reply-To: <201507111753.t6BHrJcj029406@fido.openend.se> References: , <201507111753.t6BHrJcj029406@fido.openend.se> Message-ID: Thanks a lot! Do you know anything about HappyBase compared with Jython? Best, Michelle > To: wumeid at hotmail.com > CC: tutor at python.org; lac at openend.se > From: lac at openend.se > Subject: Re: [Tutor] Access HBase > Date: Sat, 11 Jul 2015 19:53:19 +0200 > > In a message of Sat, 11 Jul 2015 09:01:15 -0400, Michelle Meiduo Wu writes: > >Hi there, > >I'm just starting to use Python. I'd like to ask which Library is good for Python to access HBASE? > > > >Besides Happybase and Python HBase, is there any other one? Which is more robotic and better functional? > > > >Any advice would be greatly appreciated. > > > >Thank you, > >Michelle > > One thing that people I know of are doing is connecting using Jython, > instead of CPython. see: http://wiki.apache.org/hadoop/Hbase/Jython > > They say it is a _lot_ faster. > > Laura > From lac at openend.se Sun Jul 12 12:46:15 2015 From: lac at openend.se (Laura Creighton) Date: Sun, 12 Jul 2015 12:46:15 +0200 Subject: [Tutor] Access HBase In-Reply-To: Message from Michelle Meiduo Wu of "Sat, 11 Jul 2015 23:46:56 -0400." References: , <201507111753.t6BHrJcj029406@fido.openend.se> Message-ID: <201507121046.t6CAkF7E019126@fido.openend.se> In a message of Sat, 11 Jul 2015 23:46:56 -0400, Michelle Meiduo Wu writes: >Thanks a lot! > >Do you know anything about HappyBase compared with Jython? > >Best, >Michelle I don't know anything at all about HappyBase, and next to nothing about Hadoop. But I know quite a bit about Jython. The Python you get from python.org (and from ActiveState) is written in the C programming language. That is why some of us often call it CPython. However, there are other implementations of Python that are not written in C, and one of them is called Jython. Jython is the Python Programming lanaguage written in Java. Now, in the Greater Python Programming World, most libraries you would like to import and include in your own programs are written in Python itself. They will be perfectly happy running under CPython or under Jython. There are also a whole lot of libraries that are written in C. Getting other things written in C to talk to CPython is work, but it is a pretty straight forward business to wrap your C libraries and getting them to work with CPython. Getting something written in some other language than C to work with CPython is much harder. C++ libraries get wrapped often enough that we have standard ways of doing that, too, but it is a lot more work. And C++ is, by design, a whole lot like C. It is very rare that CPython developers bother to wrap something written in any other language at all. So the world is full of perfectly great libraries written in Java and the CPython world basically never uses them because going from the Java world to the C world is even harder. On the Java side things are similar. If you like your Java world, but want Python syntax, not Java syntax -- you would like to program in Python -- you use Jython. If you want to use a library and it is written in Python, you import that, and it just works. If you want to use a library and it is written in Java, usually -- not all of the time, but most of the time -- you can wrap it very easily, import it, and it works. And if you want to use a C or a C++ library, things get harder. But your goal here is to talk to apache HBase, and that is a java library. Somebody wanted that enough that they made CPython bindings for that, and that is called Thrift, and all I know about that is that it was so hard to use and error prone that the HappyBase developers wrote HappyBase to wrap Thrift in something easier to use. If you want to develop in CPython this is probably where you will end up. (But somebody tomorrow could come by with information on a library I have never heard of, of course.) The other way to go after this is to develop your code with Jython not CPython. Then you follow the instructions here: http://wiki.apache.org/hadoop/Hbase/Jython and you are good to go. You just write to Hbase directly using Python syntax. The next thing you need to do is see what other libraries you need to use for your work, and see if they exist for Jython. For instance, if you need NumPy, you cannot use Jython at this time. People are working on this problem over here: http://jyni.org/ where they are trying to build a better way to interface Jython with C and C++ programs, but they aren't close to being done yet. Laura From lac at openend.se Sun Jul 12 13:02:04 2015 From: lac at openend.se (Laura Creighton) Date: Sun, 12 Jul 2015 13:02:04 +0200 Subject: [Tutor] Access HBase In-Reply-To: Message from Laura Creighton of "Sun, 12 Jul 2015 12:46:15 +0200." <201507121046.t6CAkF7E019126@fido.openend.se> References: , <201507111753.t6BHrJcj029406@fido.openend.se><201507121046.t6CAkF7E019126@fido.openend.se> Message-ID: <201507121102.t6CB24bp019723@fido.openend.se> In a message of Sun, 12 Jul 2015 12:46:15 +0200, Laura Creighton writes: >If you want >to use a library and it is written in Java, usually -- not all of the >time, but most of the time -- you can wrap it very easily, import it, >and it works. I mispoke here. Most of the time you don't even have to wrap your java libraries. They just work. So this is the main reason for using Jython at all -- I want to use these very nice java libraries, but I don't want to program in Java, but in Python. Laura From alan.gauld at btinternet.com Sun Jul 12 17:55:07 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 12 Jul 2015 16:55:07 +0100 Subject: [Tutor] Access HBase In-Reply-To: <201507121102.t6CB24bp019723@fido.openend.se> References: , <201507111753.t6BHrJcj029406@fido.openend.se><201507121046.t6CAkF7E019126@fido.openend.se> <201507121102.t6CB24bp019723@fido.openend.se> Message-ID: On 12/07/15 12:02, Laura Creighton wrote: > In a message of Sun, 12 Jul 2015 12:46:15 +0200, Laura Creighton writes: >> If you want >> to use a library and it is written in Java, usually -- not all of the >> time, but most of the time -- you can wrap it very easily, import it, >> and it works. > > I mispoke here. Most of the time you don't even have to wrap your > java libraries. They just work. > > So this is the main reason for using Jython at all -- I want to use > these very nice java libraries, but I don't want to program in Java, > but in Python. I'll just add slightly to that. I used to use Pyton in my days as an architect because our development teams wrote their code in Java. I could fire up Jython and just import a new library and use the interactive prompt to test the new code. That was cool. But there's another trick which is that Jython also has a compiler so you can write a class in Python test it interactively, then compile it so that it looks to Java like a native Java class and Java code an use it. As an architect I could prototype ideas in Python, and give the code for the Java guys to use. Once they were happy they recoded it in native Java. Even more 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 alan.gauld at btinternet.com Sun Jul 12 17:57:36 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 12 Jul 2015 16:57:36 +0100 Subject: [Tutor] Access HBase In-Reply-To: References: Message-ID: On 11/07/15 14:01, Michelle Meiduo Wu wrote: > Hi there, > I'm just starting to use Python. I'd like to ask which Library is good for Python to access HBASE? I know next to nothing about HBase (I read the IBM blurb) but it looks like it has a RESTful interface so you could access that directly via the standard "urllib" library or the "requests" third party package. I guess it depends on what you want to do and which API supports 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 alan.gauld at btinternet.com Sun Jul 12 18:33:44 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 12 Jul 2015 17:33:44 +0100 Subject: [Tutor] Access HBase In-Reply-To: References: , <201507111753.t6BHrJcj029406@fido.openend.se><201507121046.t6CAkF7E019126@fido.openend.se> <201507121102.t6CB24bp019723@fido.openend.se> Message-ID: On 12/07/15 16:55, Alan Gauld wrote: > ... I used to use Pyton Doh! Pyton -> Jython... -- 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 Sun Jul 12 19:38:37 2015 From: lac at openend.se (Laura Creighton) Date: Sun, 12 Jul 2015 19:38:37 +0200 Subject: [Tutor] Access HBase In-Reply-To: Message from Alan Gauld of "Sun, 12 Jul 2015 16:57:36 +0100." References: Message-ID: <201507121738.t6CHcbG2028358@fido.openend.se> In a message of Sun, 12 Jul 2015 16:57:36 +0100, Alan Gauld writes: >On 11/07/15 14:01, Michelle Meiduo Wu wrote: >> Hi there, >> I'm just starting to use Python. I'd like to ask which Library is good for Python to access HBASE? > >I know next to nothing about HBase (I read the IBM blurb) but it looks >like it has a RESTful interface so you could access that directly via >the standard "urllib" library or the "requests" third party package. >I guess it depends on what you want to do and which API supports that. > >-- >Alan G Maybe, and somebody tried over here using urlib: https://github.com/tousif/Hwrapper/tree/master/Hwrapper (which I have never used, so I have no idea if the code even works) but since HBase is all about really, really, really big tables I suspect there are some optimisations that you need to have to get decent performance at all. But again, I haven't used the thing so my opinion isn't worth much. Laura From nymcity at yahoo.com Mon Jul 13 00:36:11 2015 From: nymcity at yahoo.com (Nym City) Date: Sun, 12 Jul 2015 22:36:11 +0000 (UTC) Subject: [Tutor] Socket Module Message-ID: <191814046.902860.1436740572059.JavaMail.yahoo@mail.yahoo.com> Hello, I am working on a 2 individual programs. In the first program, I am taking in a list of domains in .csv format and want to get the associated IPs. In the second program, I want to import in a list of IP .csv and than get the associated domains out. Eventually I want to get both of the above programs to write out to the same .csv file that they are reading from- in the next column. But I am not there yet. Doing research I came across the following example:http://python.about.com/od/pythonstandardlibrary/qt/dnscheck.htm >From the link above, doing domain -> IP would be easier to begin with. Here are the two variations of my code: Variation 1: import csv import socket domains = open('top500domains.csv', 'r') for domain in domains: ??? domain = socket.gethostbyname(str(domains)) ??? print(domains + "\n") For the code above, I receive the following error on run: socket.gaierror: [Errno -2] Name or service not known. Variation 2: import csv import socketdomains = [] with open('top500domains.csv', 'r') as f: ??? for line in f: ??????? line = line.strip() ??????? domains.append(line) hostbyname_ex = socket.gethostbyname_ex(str(domains)) print(hostbyname_ex) I receive the same error as the first variation. Please share your advice and let me know what I am doing wrong. The top500domains.csv list is attached. Thanks in advance! ?Thank you. From cs at zip.com.au Mon Jul 13 02:25:32 2015 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 13 Jul 2015 10:25:32 +1000 Subject: [Tutor] Socket Module In-Reply-To: <191814046.902860.1436740572059.JavaMail.yahoo@mail.yahoo.com> References: <191814046.902860.1436740572059.JavaMail.yahoo@mail.yahoo.com> Message-ID: <20150713002532.GA85586@cskk.homeip.net> On 12Jul2015 22:36, Nym City wrote: >Hello, >I am working on a 2 individual programs. In the first program, I am taking in a list of domains in .csv format and want to get the associated IPs. >In the second program, I want to import in a list of IP .csv and than get the associated domains out. >Eventually I want to get both of the above programs to write out to the same .csv file that they are reading from- in the next column. But I am not there yet. >Doing research I came across the following example:http://python.about.com/od/pythonstandardlibrary/qt/dnscheck.htm >From the link above, doing domain -> IP would be easier to begin with. Here are the two variations of my code: > >Variation 1: >import csv >import socket > >domains = open('top500domains.csv', 'r') >for domain in domains: You're getting a whole line here, including the traling newline. Add this line: print("domain=%r" % (domain,)) at the start of the loop to see this. >??? domain = socket.gethostbyname(str(domains)) There's no point converting a line to str; a line is a str. If you're wokring from example code I suppose you might have mistyped "strip". Anyway, you're passing a string ending in a newline to gethostbyname. It will not resolve. >??? print(domains + "\n") >For the code above, I receive the following error on run: socket.gaierror: [Errno -2] Name or service not known. As the exception suggests. When debugging issues like these, _always_ put in print() calls ahead of the failing function to ensure that you are passing the values you think you're passing. >Variation 2: >import csv >import socketdomains = [] > >with open('top500domains.csv', 'r') as f: >??? for line in f: >??????? line = line.strip() >??????? domains.append(line) > >hostbyname_ex = socket.gethostbyname_ex(str(domains)) gethostbyname_ex takes a single hostname string. You are passing a list of strings. >print(hostbyname_ex) >I receive the same error as the first variation. >Please share your advice and let me know what I am doing wrong. The top500domains.csv list is attached. Plenty of mailing list software strips attachments. In future, unless the file is large, just append the contents below your message (with some explaination). Cheers, Cameron Simpson Life is uncertain. Eat dessert first. - Jim Blandy From dyoo at hashcollision.org Mon Jul 13 04:27:04 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 12 Jul 2015 19:27:04 -0700 Subject: [Tutor] Socket Module In-Reply-To: <20150713002532.GA85586@cskk.homeip.net> References: <191814046.902860.1436740572059.JavaMail.yahoo@mail.yahoo.com> <20150713002532.GA85586@cskk.homeip.net> Message-ID: One other thing to note: if you're working with a comma-separated value file (CSV), then you may want to use the 'csv' module to parse it. https://docs.python.org/3.5/library/csv.html This should allow you to walk through the file as if it were a sequence of records. In contrast, if you're dealing with your input as a sequence of lines, then you have more to deal with: you need to think about issues like line endings, which Cameron has pointed out. From wlq6037 at 163.com Mon Jul 13 04:57:59 2015 From: wlq6037 at 163.com (=?GBK?B?zfXA+8e/?=) Date: Mon, 13 Jul 2015 10:57:59 +0800 (CST) Subject: [Tutor] [issue570] [windows server 2003]Datagram Socket on windows server 2003 (python winsock not work well)) Message-ID: <56260036.6def.14e85590113.Coremail.wlq6037@163.com> -------- Forwarding messages -------- From: "johnny_wang" Date: 2015-07-10 17:33:58 To: wlq6037 at 163.com Subject: [issue570] [windows server 2003]Datagram Socket on windows server 2003 (python winsock not work well)) New submission from johnny_wang: when use: s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.setsockopt(socket.IPPROTO_IP, socket.IP_HDRINCL, 1) s.bind(('', 1029)) while True try : databuffer, (raddress, rport) = s.recvfrom(1024) except socket.timeout: logging.error("Write timeout on socket") continue databuffer is nothing in it! http://stackoverflow.com/questions/31316216/how-to-detect-or-capture-udp-packet-on-a-local-port69-with-python-on-windows ---------- messages: 2978 nosy: johnny_wang priority: bug status: unread title: [windows server 2003]Datagram Socket on windows server 2003 (python winsock not work well)) _______________________________________________________ PSF Meta Tracker _______________________________________________________ From alan.gauld at btinternet.com Mon Jul 13 08:58:45 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 13 Jul 2015 07:58:45 +0100 Subject: [Tutor] Socket Module In-Reply-To: <191814046.902860.1436740572059.JavaMail.yahoo@mail.yahoo.com> References: <191814046.902860.1436740572059.JavaMail.yahoo@mail.yahoo.com> Message-ID: On 12/07/15 23:36, Nym City via Tutor wrote: > import csv > import socket > > domains = open('top500domains.csv', 'r') > for domain in domains: > domain = socket.gethostbyname(str(domains)) You are passing your file object to gethostbyname() > print(domains + "\n") > For the code above, I receive the following error on run: socket.gaierror: [Errno -2] Name or service not known. > Variation 2: > import csv > import socketdomains = [] I assume the domains bit is on a separate line? > > with open('top500domains.csv', 'r') as f: > for line in f: > line = line.strip() > domains.append(line) > > hostbyname_ex = socket.gethostbyname_ex(str(domains)) Again you are passing the entire list to gethostbyname_ex() -- 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 sunil.techspk at gmail.com Mon Jul 13 14:03:03 2015 From: sunil.techspk at gmail.com (Sunil Tech) Date: Mon, 13 Jul 2015 17:33:03 +0530 Subject: [Tutor] Unpack from list Message-ID: Hi Tutor, [a,b,c] = [1,2] this will give Traceback (most recent call last): File "", line 1, in ValueError: need more than 2 values to unpack But is there any chance, if there are 3 values on left hand side and 2 values on right side, to add an empty value to the left side dynamically? there can be multiple elements to unpack depending on what is expected on left side. Like in methods there can be optional parameters, if the values is not passed, optional values will take what is been declared. def optal(a,b,c=''): """ c - is option """ ?Thanks, Sunil. G ? From breamoreboy at yahoo.co.uk Mon Jul 13 14:27:13 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 13 Jul 2015 13:27:13 +0100 Subject: [Tutor] Unpack from list In-Reply-To: References: Message-ID: On 13/07/2015 13:03, Sunil Tech wrote: > Hi Tutor, > > > [a,b,c] = [1,2] > this will give > Traceback (most recent call last): > File "", line 1, in > ValueError: need more than 2 values to unpack > > But is there any chance, if there are 3 values on left hand side and 2 > values on right side, to add an empty value to the left side dynamically? > You need "Extended Iterable Unpacking" as described in https://www.python.org/dev/peps/pep-3132/ An example says more than a thousand words: >>> a, *b, c = range(5) >>> a 0 >>> c 4 >>> b [1, 2, 3] Hence:- >>> a,b,*c=[1,2] >>> a,b,c (1, 2, []) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From gb.gabrielebrambilla at gmail.com Mon Jul 13 17:14:36 2015 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Mon, 13 Jul 2015 11:14:36 -0400 Subject: [Tutor] reading Fortran unformatted files with Python Message-ID: Hi, I have problems reading unformatted fortran output (binary) with python. I have a code in fortran where I write data on a file inside a cycle: write(11) x,y,z,BA01(i,j,k,1),BA01(i,j,k,2),1 BB01(i,j,k,1),BB01(i,j,k,2),2 BE01(i,j,k,1),3 EC01(i,j,k,1),EC01(i,j,k,2),4 ED01(i,j,k,1),ED01(i,j,k,2),5 EF01(i,j,k,1),6 rGH01(i,j,k,1),rGH01(i,j,k,2),7 rGI01(i,j,k,1),rGI01(i,j,k,2),8 rGJ01(i,j,k,1),1 rGL(i,j,k,2),rGM(i,j,k,2),rGN(i,j,k,2) How can I read this 21 real line by line in Python? How can I decode this unit specifier 11? All the numbers are defined as REAL. thanks Gabriele From lac at openend.se Mon Jul 13 17:29:08 2015 From: lac at openend.se (Laura Creighton) Date: Mon, 13 Jul 2015 17:29:08 +0200 Subject: [Tutor] reading Fortran unformatted files with Python In-Reply-To: Message from Gabriele Brambilla of "Mon, 13 Jul 2015 11:14:36 -0400." References: Message-ID: <201507131529.t6DFT855025676@fido.openend.se> In a message of Mon, 13 Jul 2015 11:14:36 -0400, Gabriele Brambilla writes: >Hi, > >I have problems reading unformatted fortran output (binary) with python. > >I have a code in fortran where I write data on a file inside a cycle: > >write(11) x,y,z,BA01(i,j,k,1),BA01(i,j,k,2),1 >BB01(i,j,k,1),BB01(i,j,k,2),2 BE01(i,j,k,1),3 >EC01(i,j,k,1),EC01(i,j,k,2),4 ED01(i,j,k,1),ED01(i,j,k,2),5 > EF01(i,j,k,1),6 rGH01(i,j,k,1),rGH01(i,j,k,2),7 > rGI01(i,j,k,1),rGI01(i,j,k,2),8 rGJ01(i,j,k,1),1 > rGL(i,j,k,2),rGM(i,j,k,2),rGN(i,j,k,2) > >How can I read this 21 real line by line in Python? How can I decode this >unit specifier 11? All the numbers are defined as REAL. > >thanks > >Gabriele >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >https://mail.python.org/mailman/listinfo/tutor You want to use this package: https://pypi.python.org/pypi/fortranfile You need to also load numpy for this to work. Some people cannot get this to work, and like this way of doing things better: https://physics.ucf.edu/~dle/blog.php?id=1 Again, needs numpy. Write back if you need a numpy-free solution (i.e. you are on Jython). Laura From gb.gabrielebrambilla at gmail.com Mon Jul 13 20:08:41 2015 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Mon, 13 Jul 2015 14:08:41 -0400 Subject: [Tutor] reading Fortran unformatted files with Python In-Reply-To: <201507131529.t6DFT855025676@fido.openend.se> References: <201507131529.t6DFT855025676@fido.openend.se> Message-ID: The second method seem to work. But how can I know which dtype in Python corresponds to REAL in fortran? Thanks On Mon, Jul 13, 2015 at 11:29 AM, Laura Creighton wrote: > In a message of Mon, 13 Jul 2015 11:14:36 -0400, Gabriele Brambilla writes: > >Hi, > > > >I have problems reading unformatted fortran output (binary) with python. > > > >I have a code in fortran where I write data on a file inside a cycle: > > > >write(11) x,y,z,BA01(i,j,k,1),BA01(i,j,k,2),1 > >BB01(i,j,k,1),BB01(i,j,k,2),2 BE01(i,j,k,1),3 > >EC01(i,j,k,1),EC01(i,j,k,2),4 ED01(i,j,k,1),ED01(i,j,k,2),5 > > EF01(i,j,k,1),6 rGH01(i,j,k,1),rGH01(i,j,k,2),7 > > rGI01(i,j,k,1),rGI01(i,j,k,2),8 rGJ01(i,j,k,1),1 > > rGL(i,j,k,2),rGM(i,j,k,2),rGN(i,j,k,2) > > > >How can I read this 21 real line by line in Python? How can I decode this > >unit specifier 11? All the numbers are defined as REAL. > > > >thanks > > > >Gabriele > >_______________________________________________ > >Tutor maillist - Tutor at python.org > >To unsubscribe or change subscription options: > >https://mail.python.org/mailman/listinfo/tutor > > You want to use this package: > https://pypi.python.org/pypi/fortranfile > > You need to also load numpy for this to work. > > Some people cannot get this to work, and like this way of doing > things better: > > https://physics.ucf.edu/~dle/blog.php?id=1 > > Again, needs numpy. Write back if you need a numpy-free solution (i.e. > you are on Jython). > > Laura > From gb.gabrielebrambilla at gmail.com Mon Jul 13 21:32:06 2015 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Mon, 13 Jul 2015 15:32:06 -0400 Subject: [Tutor] reading Fortran unformatted files with Python In-Reply-To: References: <201507131529.t6DFT855025676@fido.openend.se> Message-ID: Hi, sorry for the confusion I understood that the Real(8) I'm using correspond to dtype float64 in Python. With the second method it seems to work but I get a wrong number of elements. They should be grouped by 21 float64 but I don't find a number of data = 21*m where m is an integer number. I think the problem is that Fortran write 4bytes at the beginning and at the end of each file. I found a way to remove the first 4 bytes but not the last. Do you have any suggestion? Thanks On Mon, Jul 13, 2015 at 2:08 PM, Gabriele Brambilla < gb.gabrielebrambilla at gmail.com> wrote: > The second method seem to work. > But how can I know which dtype in Python corresponds to REAL in fortran? > > Thanks > > > > On Mon, Jul 13, 2015 at 11:29 AM, Laura Creighton wrote: > >> In a message of Mon, 13 Jul 2015 11:14:36 -0400, Gabriele Brambilla >> writes: >> >Hi, >> > >> >I have problems reading unformatted fortran output (binary) with python. >> > >> >I have a code in fortran where I write data on a file inside a cycle: >> > >> >write(11) x,y,z,BA01(i,j,k,1),BA01(i,j,k,2),1 >> >BB01(i,j,k,1),BB01(i,j,k,2),2 BE01(i,j,k,1),3 >> >EC01(i,j,k,1),EC01(i,j,k,2),4 ED01(i,j,k,1),ED01(i,j,k,2),5 >> > EF01(i,j,k,1),6 rGH01(i,j,k,1),rGH01(i,j,k,2),7 >> > rGI01(i,j,k,1),rGI01(i,j,k,2),8 rGJ01(i,j,k,1),1 >> > rGL(i,j,k,2),rGM(i,j,k,2),rGN(i,j,k,2) >> > >> >How can I read this 21 real line by line in Python? How can I decode this >> >unit specifier 11? All the numbers are defined as REAL. >> > >> >thanks >> > >> >Gabriele >> >_______________________________________________ >> >Tutor maillist - Tutor at python.org >> >To unsubscribe or change subscription options: >> >https://mail.python.org/mailman/listinfo/tutor >> >> You want to use this package: >> https://pypi.python.org/pypi/fortranfile >> >> You need to also load numpy for this to work. >> >> Some people cannot get this to work, and like this way of doing >> things better: >> >> https://physics.ucf.edu/~dle/blog.php?id=1 >> >> Again, needs numpy. Write back if you need a numpy-free solution (i.e. >> you are on Jython). >> >> Laura >> > > From gb.gabrielebrambilla at gmail.com Mon Jul 13 22:11:02 2015 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Mon, 13 Jul 2015 16:11:02 -0400 Subject: [Tutor] reading Fortran unformatted files with Python In-Reply-To: References: <201507131529.t6DFT855025676@fido.openend.se> Message-ID: I solved the issue. If I need more help I'll send another email. thanks GB On Mon, Jul 13, 2015 at 3:32 PM, Gabriele Brambilla < gb.gabrielebrambilla at gmail.com> wrote: > Hi, > > sorry for the confusion I understood that the Real(8) I'm using correspond > to dtype float64 in Python. > With the second method it seems to work but I get a wrong number of > elements. > > They should be grouped by 21 float64 but I don't find a number of data = > 21*m where m is an integer number. > > I think the problem is that Fortran write 4bytes at the beginning and at > the end of each file. > I found a way to remove the first 4 bytes but not the last. > > Do you have any suggestion? > > Thanks > > > On Mon, Jul 13, 2015 at 2:08 PM, Gabriele Brambilla < > gb.gabrielebrambilla at gmail.com> wrote: > >> The second method seem to work. >> But how can I know which dtype in Python corresponds to REAL in fortran? >> >> Thanks >> >> >> >> On Mon, Jul 13, 2015 at 11:29 AM, Laura Creighton wrote: >> >>> In a message of Mon, 13 Jul 2015 11:14:36 -0400, Gabriele Brambilla >>> writes: >>> >Hi, >>> > >>> >I have problems reading unformatted fortran output (binary) with python. >>> > >>> >I have a code in fortran where I write data on a file inside a cycle: >>> > >>> >write(11) x,y,z,BA01(i,j,k,1),BA01(i,j,k,2),1 >>> >BB01(i,j,k,1),BB01(i,j,k,2),2 BE01(i,j,k,1),3 >>> >EC01(i,j,k,1),EC01(i,j,k,2),4 ED01(i,j,k,1),ED01(i,j,k,2),5 >>> > EF01(i,j,k,1),6 rGH01(i,j,k,1),rGH01(i,j,k,2),7 >>> > rGI01(i,j,k,1),rGI01(i,j,k,2),8 rGJ01(i,j,k,1),1 >>> > rGL(i,j,k,2),rGM(i,j,k,2),rGN(i,j,k,2) >>> > >>> >How can I read this 21 real line by line in Python? How can I decode >>> this >>> >unit specifier 11? All the numbers are defined as REAL. >>> > >>> >thanks >>> > >>> >Gabriele >>> >_______________________________________________ >>> >Tutor maillist - Tutor at python.org >>> >To unsubscribe or change subscription options: >>> >https://mail.python.org/mailman/listinfo/tutor >>> >>> You want to use this package: >>> https://pypi.python.org/pypi/fortranfile >>> >>> You need to also load numpy for this to work. >>> >>> Some people cannot get this to work, and like this way of doing >>> things better: >>> >>> https://physics.ucf.edu/~dle/blog.php?id=1 >>> >>> Again, needs numpy. Write back if you need a numpy-free solution (i.e. >>> you are on Jython). >>> >>> Laura >>> >> >> > From lac at openend.se Mon Jul 13 22:30:11 2015 From: lac at openend.se (Laura Creighton) Date: Mon, 13 Jul 2015 22:30:11 +0200 Subject: [Tutor] reading Fortran unformatted files with Python In-Reply-To: Message from Gabriele Brambilla of "Mon, 13 Jul 2015 15:32:06 -0400." References: <201507131529.t6DFT855025676@fido.openend.se> Message-ID: <201507132030.t6DKUBIT032388@fido.openend.se> In a message of Mon, 13 Jul 2015 15:32:06 -0400, Gabriele Brambilla writes: >Hi, > >sorry for the confusion I understood that the Real(8) I'm using correspond >to dtype float64 in Python. >With the second method it seems to work but I get a wrong number of >elements. > >They should be grouped by 21 float64 but I don't find a number of data = >21*m where m is an integer number. > >I think the problem is that Fortran write 4bytes at the beginning and at >the end of each file. >I found a way to remove the first 4 bytes but not the last. > >Do you have any suggestion? > >Thanks I always used FortranFile, which uses (or used at any rate) the Python struct module to get those parts right. I suppose you could fiddle around with struct yourself, but it is probably better if you can just get FortranFile to work for you. This entry in the Scientific Python cookbook may help. http://wiki.scipy.org/Cookbook/FortranIO If you still cannot get it to work, the place to ask for help with the FortranFile library is the scientific Python mailing list. http://mail.scipy.org/mailman/listinfo/scipy-user whatever you are misunderstanding, they will know in a second because they do this all the time, and I do this once every 3 or 5 years, and then pretty much forget everything I learned until the next time I have to relearn it. Sorry I cannot remember more of the internal details, Laura From wumeid at hotmail.com Mon Jul 13 22:51:01 2015 From: wumeid at hotmail.com (Michelle Meiduo Wu) Date: Mon, 13 Jul 2015 16:51:01 -0400 Subject: [Tutor] Access HBase In-Reply-To: <201507121046.t6CAkF7E019126@fido.openend.se> References: , <201507111753.t6BHrJcj029406@fido.openend.se>, <201507121046.t6CAkF7E019126@fido.openend.se> Message-ID: Thanks a lot!Your information is very helpful. I believe it's easier to try HappyBase at beginning. Thanks,Michelle > To: wumeid at hotmail.com > CC: lac at openend.se; tutor at python.org; lac at openend.se > From: lac at openend.se > Subject: Re: [Tutor] Access HBase > Date: Sun, 12 Jul 2015 12:46:15 +0200 > > In a message of Sat, 11 Jul 2015 23:46:56 -0400, Michelle Meiduo Wu writes: > >Thanks a lot! > > > >Do you know anything about HappyBase compared with Jython? > > > >Best, > >Michelle > > I don't know anything at all about HappyBase, and next to nothing about > Hadoop. But I know quite a bit about Jython. > > The Python you get from python.org (and from ActiveState) is written > in the C programming language. That is why some of us often call it > CPython. However, there are other implementations of Python that > are not written in C, and one of them is called Jython. Jython is > the Python Programming lanaguage written in Java. > > Now, in the Greater Python Programming World, most libraries you > would like to import and include in your own programs are written > in Python itself. They will be perfectly happy running under CPython > or under Jython. There are also a whole lot of libraries that are > written in C. Getting other things written in C to talk to CPython > is work, but it is a pretty straight forward business to wrap your > C libraries and getting them to work with CPython. > > Getting something written in some other language than C to work with > CPython is much harder. C++ libraries get wrapped often enough that > we have standard ways of doing that, too, but it is a lot more work. > And C++ is, by design, a whole lot like C. > > It is very rare that CPython developers bother to wrap something > written in any other language at all. So the world is full of > perfectly great libraries written in Java and the CPython world > basically never uses them because going from the Java world to the C > world is even harder. > > On the Java side things are similar. If you like your Java world, > but want Python syntax, not Java syntax -- you would like to program > in Python -- you use Jython. If you want to use a library and it is > written in Python, you import that, and it just works. If you want > to use a library and it is written in Java, usually -- not all of the > time, but most of the time -- you can wrap it very easily, import it, > and it works. And if you want to use a C or a C++ library, things > get harder. > > But your goal here is to talk to apache HBase, and that is a java > library. Somebody wanted that enough that they made CPython bindings > for that, and that is called Thrift, and all I know about that is that > it was so hard to use and error prone that the HappyBase developers > wrote HappyBase to wrap Thrift in something easier to use. If you > want to develop in CPython this is probably where you will end up. > (But somebody tomorrow could come by with information on a library > I have never heard of, of course.) > > The other way to go after this is to develop your code with Jython > not CPython. Then you follow the instructions here: > http://wiki.apache.org/hadoop/Hbase/Jython > > and you are good to go. > > You just write to Hbase directly using Python syntax. > > The next thing you need to do is see what other libraries you need to > use for your work, and see if they exist for Jython. For instance, > if you need NumPy, you cannot use Jython at this time. People are > working on this problem over here: http://jyni.org/ where they are > trying to build a better way to interface Jython with C and C++ > programs, but they aren't close to being done yet. > > Laura > From alan.gauld at btinternet.com Mon Jul 13 23:16:28 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 13 Jul 2015 22:16:28 +0100 Subject: [Tutor] Unpack from list In-Reply-To: References: Message-ID: On 13/07/15 13:27, Mark Lawrence wrote: > Hence:- > > >>> a,b,*c=[1,2] > >>> a,b,c > (1, 2, []) Neat! a new one on me. -- 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 francois.dion at gmail.com Tue Jul 14 18:05:24 2015 From: francois.dion at gmail.com (Francois Dion) Date: Tue, 14 Jul 2015 12:05:24 -0400 Subject: [Tutor] Access HBase In-Reply-To: References: Message-ID: On Sat, Jul 11, 2015 at 9:01 AM, Michelle Meiduo Wu wrote: > Hi there, > I'm just starting to use Python. I'd like to ask which Library is good for > Python to access HBASE? > I think the Python for Data Science group would be a good place to ask: https://groups.google.com/forum/?fromgroups#!forum/pydata If you are just starting out in Python, though, I would suggest you learn core python first. Francois From robertvstepp at gmail.com Sat Jul 18 00:04:51 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 17 Jul 2015 17:04:51 -0500 Subject: [Tutor] How to use tkinter with a touch screen device? Message-ID: Apparently I will be finding myself having to write some programs to run on a Samsung Slate tablet, manufactured circa 2011. It has Windows 7 home edition 64-bit as its OS. Hopefully Python 3 will be easily installed, and I will be able to run Python 3 programs on it. Operating under this assumption, what adjustments will I need to make in order to use tkinter for GUIs on this device? So far my searches have not found targeted information (yet). One thing I did find is that complex gestures are not supported by tkinter. But will tkinter see: 1) Short presses as a left mouse click? 2) Long presses as a right mouse click? 3) Virtual keyboard events the same as physical keyboard events? 4) Etc. ? Any useful info along these lines will be greatly appreciated! Otherwise, I will do the usual once I get this tablet updated and Python installed: Barge ahead, write some code, and see what happens! -- boB From breamoreboy at yahoo.co.uk Sat Jul 18 00:32:50 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 17 Jul 2015 23:32:50 +0100 Subject: [Tutor] How to use tkinter with a touch screen device? In-Reply-To: References: Message-ID: On 17/07/2015 23:04, boB Stepp wrote: > Apparently I will be finding myself having to write some programs to > run on a Samsung Slate tablet, manufactured circa 2011. It has > Windows 7 home edition 64-bit as its OS. Hopefully Python 3 will be > easily installed, and I will be able to run Python 3 programs on it. > Operating under this assumption, what adjustments will I need to make > in order to use tkinter for GUIs on this device? So far my searches > have not found targeted information (yet). One thing I did find is > that complex gestures are not supported by tkinter. But will tkinter > see: > > 1) Short presses as a left mouse click? > 2) Long presses as a right mouse click? > 3) Virtual keyboard events the same as physical keyboard events? > 4) Etc. ? > > Any useful info along these lines will be greatly appreciated! > Otherwise, I will do the usual once I get this tablet updated and > Python installed: Barge ahead, write some code, and see what happens! > A possible starter http://sumerulabs.in/blog/2015/02/21/touch-screen-for-the-new-raspberry-pi-2-available-in-india-sumerulabs/ ??? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From robertvstepp at gmail.com Sat Jul 18 01:01:59 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 17 Jul 2015 18:01:59 -0500 Subject: [Tutor] How to use tkinter with a touch screen device? In-Reply-To: References: Message-ID: On Fri, Jul 17, 2015 at 5:32 PM, Mark Lawrence wrote: > A possible starter > http://sumerulabs.in/blog/2015/02/21/touch-screen-for-the-new-raspberry-pi-2-available-in-india-sumerulabs/ > ??? I had briefly glanced at that, but other than a short snippet of code, it seemed to be more an advertisement for a new touch screen monitor for Raspberry Pi. In fact most of the search results I got earlier involve the Raspberry Pi. Nothing leaped out at me. I may have to sift through those results more carefully if nothing else turns up. But thanks! -- boB From alan.gauld at btinternet.com Sat Jul 18 01:43:30 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 18 Jul 2015 00:43:30 +0100 Subject: [Tutor] How to use tkinter with a touch screen device? In-Reply-To: References: Message-ID: On 17/07/15 23:04, boB Stepp wrote: > that complex gestures are not supported by tkinter. But will tkinter > see: > > 1) Short presses as a left mouse click? > 2) Long presses as a right mouse click? > 3) Virtual keyboard events the same as physical keyboard events? > 4) Etc. ? > I'd go to the Tk and Tkinter mailing lists for that one Bob. Well beyond tutor expectations. (which means I have no idea! :-) But seriously it will depend what your version of Tkinter supports and that's really one for the Tcl/Tk community first and tkinter second. -- 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 Jul 18 05:13:25 2015 From: lac at openend.se (Laura Creighton) Date: Sat, 18 Jul 2015 05:13:25 +0200 Subject: [Tutor] How to use tkinter with a touch screen device? In-Reply-To: Message from boB Stepp of "Fri, 17 Jul 2015 17:04:51 -0500." References: Message-ID: <201507180313.t6I3DPAa011107@theraft.openend.se> The place to ask this question is https://mail.python.org/mailman/listinfo/tkinter-discuss but I think you are out of luck. On the other hand, kivy ought to work for you. http://kivy.org/#home I've yet to try it on a windows tablet, though. Laura From robertvstepp at gmail.com Sat Jul 18 05:20:56 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 17 Jul 2015 22:20:56 -0500 Subject: [Tutor] Design question: Web-based vs. desktop-based vs. desktop-based with data backed up to web server with a tablet thrown in for all cases? Message-ID: My wife (a Montessori teacher of 7th through 9th grades) is trying to coerce me to create some helpful software for her classroom. She has lots of things she *wants*: Some involve only her and her record keeping. Some will involve both her and students. Some would additionally involve parents. With the parents it would have to be web-based. For students, some software would be used only in their classroom. Others would be used both in and outside the classroom. In my wife's case, she would want to be able to access the software and its data either at school or at home. Currently she does most of her work on a laptop that she carries back and forth between home and school. Anything involving student records would have to be kept secure. For her most urgent project to be done before school starts in 5 weeks (!), she wants me to enable her to assess her students' work as she wanders about the classroom using this Samsung Slate tablet the school has given her to use. This device would be okay for stuff like checking check boxes, navigating menus, radio buttons, drop-down list boxes, etc., but would be horrible for any type of manual data entry. So she would need to do the latter at her laptop, though she might do limited data entry on the tablet. I do own server space and a domain name that I have never used, but should be adequate for my wife's needs. The school has a wireless network that allows Internet access, which is usually (but not always!) reliable. The main question: How do I best handle the storage and access of my wife's classroom data in these three different scenarios (wife's access only; wife and student's access; wife's, student's and parent's), and have each party to have access to relevant software I create? Using this *most urgent* project as a starting example, this student assessment software would involve only my wife, the tablet and her laptop. Both the laptop and the tablet would have to both be able to run the software and have synchronized data between the two devices. If the student data resided only on these two devices, then protecting the student data isn't as much of an issue as long as my wife controls her physical devices. However, if the data were to reside on a server then that would become a more important issue. It would obviously be very important that data cannot be lost. I can easily imagine my wife dropping and destroying the tablet, the tablet's hard drive crashing, the laptop's crashing, etc. If the data were stored online, then possible school wireless network outages would be a potential bad problem. My best thoughts at the moment are to: 1) Find a way to always keep the laptop and tablet synchronized. 2) Have both devices write a backup copy of the data to the server whenever a change in the data occurs. 3) In case of wireless network failure, the software would realize this and know that whenever connectivity is restored to write any data that has changed in the interim. 4) The backup data on the server would have to be securely encrypted against whatever hackers might try to to access or intercept the data. (Hmm. What about the communications between the laptop and the tablet?) Is this a sensible way of approaching at least this one project? Of course, I have not a clue as to how to do any of this (yet!). As always, many thanks in advance! P.S.: My wife has researched existing software and has found everything lacking. She wants custom solutions to her custom needs. Oh, joy. But very interesting! -- boB From robertvstepp at gmail.com Sat Jul 18 05:49:16 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 17 Jul 2015 22:49:16 -0500 Subject: [Tutor] How to use tkinter with a touch screen device? In-Reply-To: <201507180313.t6I3DPAa011107@theraft.openend.se> References: <201507180313.t6I3DPAa011107@theraft.openend.se> Message-ID: On Fri, Jul 17, 2015 at 10:13 PM, Laura Creighton wrote: > The place to ask this question is https://mail.python.org/mailman/listinfo/tkinter-discuss but I think you are out of luck... This would explain why I am having trouble finding much information. I *think* I could get things to work on the tablet if I restrict myself to coding for short press and long press gestures, and the virtual keyboard. But even this might be more involved than I would like. I read something a bit ago that one fellow could not get a short or long press gesture to work unless he coded for BOTH the press and release the press events. Not exactly analogous to a mouse-click event. > ... On the other hand, kivy > ought to work for you. http://kivy.org/#home I've yet to try it on > a windows tablet, though. I think it was you who mentioned this GUI framework on the main python list fairly recently. In fact, on my phone I have left its web page up to look at when I get a chance. Does kivy have all of the functionality of tkinter in addition to supporting multi-touch gestures? Any downsides I should be aware of? Judging by their "Hello, world" example, the code looks even simpler than tkinter. Does everything have to be written as classes? Thanks, Laura! -- boB From lac at openend.se Sat Jul 18 06:18:38 2015 From: lac at openend.se (Laura Creighton) Date: Sat, 18 Jul 2015 06:18:38 +0200 Subject: [Tutor] Design question: Web-based vs. desktop-based vs. desktop-based with data backed up to web server with a tablet thrown in for all cases? In-Reply-To: Message from boB Stepp of "Fri, 17 Jul 2015 22:20:56 -0500." References: Message-ID: <201507180418.t6I4Icn8011379@fido.openend.se> I have a plug in usb keyboard that I can just plug into my tablet. It works great for data entry. I don't know anything about windows tablets though, this is working for android. You need a usb port that is capable of being both a slave and a master. Most of them can do this these days -- if you send me the model number of the tablet I can look that up for you. >1) Find a way to always keep the laptop and tablet synchronized. All the cloud storage services want to sell you something that does this. So does Dropbox. >2) Have both devices write a backup copy of the data to the server >whenever a change in the data occurs. This may be overkill. Plus it may not be what you want. You may be happier with 'the server gets the new data whenever I push it to there'. Or the cloud gets it then. >3) In case of wireless network failure, the software would realize >this and know that whenever connectivity is restored to write any data >that has changed in the interim. >4) The backup data on the server would have to be securely encrypted >against whatever hackers might try to to access or intercept the data. >(Hmm. What about the communications between the laptop and the >tablet?) If wireless, yes, if you are plugging your tablet into your laptop > >Is this a sensible way of approaching at least this one project? Of >course, I have not a clue as to how to do any of this (yet!). > >As always, many thanks in advance! > >P.S.: My wife has researched existing software and has found >everything lacking. She wants custom solutions to her custom needs. >Oh, joy. But very interesting! >-- >boB The idea is to use as much off the shelf technology to do what you want, and not reinvent the wheel. Especially when it comes to security issues that, worse case scenario, have all the parents suing you for not keeping things that are required to be private from being hacked. Before there was cloud computing, and smartphones, we wrote a client server app that handled the billing situation for the local energy company -- plus all their power outages, service apps etc, etc, etc. It took 4 of us nearly a year to get the permissions down right for all possible cases. Doing the same sort of thing for our mobile app using existing cloud libraries took about 3 weeks. Only some of that speedup was due to the fact that we knew what we were doing a lot better the second time around, and we had really extensive unit tests from the old way of doingf that we could slightly refactor and then make sure they passed under trhe new regime. You haven't picked yourself an easy one here ... One warning -- the trick to high speed performance for such things is to copy as little data as is needed from one device to another (and then let the devices regenerate a lot of what they need from the basic necessary set). But the trick for _developing_ and debugging such things is to send more information, including a bunch of handshaking things like 'I am trying to open the MongoDB Database now. Ok, that worked, now I am adding new job tickets ... and so on and so forth. If you do not do things like that, the time will come when you sit pointing at your server saying 'You aren't doing anything. Why aren't you doing ?" Utterly frustrating. YOu need to build a verbose way into what you do so you can have your server tell you what it thinks it ought to be doing, and then a way to shut the server up when you go into production. But I would go google for cloud services that have data synchronisation. (Which is pretty much all of them, that being the point, after all). You want to build your app out of some of these ready made parts, or risk that your wife's students will all be in university before you are done with your app. Laura From lac at openend.se Sat Jul 18 06:22:19 2015 From: lac at openend.se (Laura Creighton) Date: Sat, 18 Jul 2015 06:22:19 +0200 Subject: [Tutor] How to use tkinter with a touch screen device? In-Reply-To: Message from boB Stepp of "Fri, 17 Jul 2015 22:49:16 -0500." References: <201507180313.t6I3DPAa011107@theraft.openend.se> Message-ID: <201507180422.t6I4MJKB011518@fido.openend.se> In a message of Fri, 17 Jul 2015 22:49:16 -0500, boB Stepp writes: >> ... On the other hand, kivy >> ought to work for you. http://kivy.org/#home I've yet to try it on >> a windows tablet, though. > >I think it was you who mentioned this GUI framework on the main python >list fairly recently. In fact, on my phone I have left its web page >up to look at when I get a chance. Does kivy have all of the >functionality of tkinter in addition to supporting multi-touch >gestures? Any downsides I should be aware of? Judging by their >"Hello, world" example, the code looks even simpler than tkinter. >Does everything have to be written as classes? > >Thanks, Laura! > >-- >boB You are most welcome. Everything has to be written as classes. So far, I haven't found anything I could do with tkinter that I cannot do with kivy, but I haven't coded anything really complicated with it yet, either. Laura From robertvstepp at gmail.com Sat Jul 18 06:57:01 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 17 Jul 2015 23:57:01 -0500 Subject: [Tutor] Design question: Web-based vs. desktop-based vs. desktop-based with data backed up to web server with a tablet thrown in for all cases? In-Reply-To: <201507180418.t6I4Icn8011379@fido.openend.se> References: <201507180418.t6I4Icn8011379@fido.openend.se> Message-ID: On Fri, Jul 17, 2015 at 11:18 PM, Laura Creighton wrote: > I have a plug in usb keyboard that I can just plug into my tablet. > It works great for data entry... I suggested this to my wife and believe she is looking into it. However, whenever she is walking about her classroom, she does not want to be messing with her keyboard, just quickly tapping and selecting predetermined skill assessment options (Student just beginning to learn; practicing now; has achieved mastery of this skill; etc.). > >>1) Find a way to always keep the laptop and tablet synchronized. > > All the cloud storage services want to sell you something that > does this. So does Dropbox. My wife has a Dropbox account and mentioned this as a possibility. >>Is this a sensible way of approaching at least this one project? Of >>course, I have not a clue as to how to do any of this (yet!). [snip] > The idea is to use as much off the shelf technology to do what you > want, and not reinvent the wheel. Especially when it comes to > security issues that, worse case scenario, have all the parents > suing you for not keeping things that are required to be private > from being hacked. > > Before there was cloud computing, and smartphones, we wrote a client > server app that handled the billing situation for the local energy > company -- plus all their power outages, service apps etc, etc, etc. > It took 4 of us nearly a year to get the permissions down right for > all possible cases. > > Doing the same sort of thing for our mobile app using existing > cloud libraries took about 3 weeks. Only some of that speedup was > due to the fact that we knew what we were doing a lot better the > second time around, and we had really extensive unit tests from > the old way of doingf that we could slightly refactor and then > make sure they passed under trhe new regime. > > You haven't picked yourself an easy one here ... Tell me about it! Vonda (my honored spouse) has been hinting about all of this for quite some time. But she had not figured out her exact needs. She actually still hasn't, but has a broad idea of what she initially would like to have now. And she has a greatly exaggerated idea of what I can accomplish in five weeks! I suspect version 0.0.1 will be something along the lines of: Everything basically exists on the tablet. Get a keyboard that works with the tablet. Manually backup/sync tablet with laptop as needed. Does the program have the basic functionality you *really* want as you wander about the room? Probably not. Tell me what you now need. Etc. Only when I feel I have what she really wants nailed down will I get into the rest of our discussion. But I *do* want to design my code with where the final version needs to go, so hopefully I won't have any major rewrites in my future! > One warning -- the trick to high speed performance for such things is > to copy as little data as is needed from one device to another (and > then let the devices regenerate a lot of what they need from the > basic necessary set). But the trick for _developing_ and > debugging such things is to send more information, including a > bunch of handshaking things like 'I am trying to open the MongoDB > Database now. Ok, that worked, now I am adding new job tickets ... > and so on and so forth. > > If you do not do things like that, the time will come when you sit > pointing at your server saying 'You aren't doing anything. Why aren't > you doing ?" Utterly frustrating. > YOu need to build a verbose way into what you do so you can have your > server tell you what it thinks it ought to be doing, and then a > way to shut the server up when you go into production. This makes a lot of sense! Must remember... > But I would go google for cloud services that have data synchronisation. > (Which is pretty much all of them, that being the point, after all). > You want to build your app out of some of these ready made parts, or > risk that your wife's students will all be in university before you are > done with your app. For sure! Especially when I get into some student drills programs that she will undoubtedly want the students to work on both in class and at home with an option for the parents to view their child's progress online. But for this student assessment project, it is going to have to be without all the desired bells and whistles to have something that will be practically useful for her when school starts. Especially when I am certain Vonda is still figuring out what she *really* needs! -- boB From lac at openend.se Sat Jul 18 07:18:00 2015 From: lac at openend.se (Laura Creighton) Date: Sat, 18 Jul 2015 07:18:00 +0200 Subject: [Tutor] Design question: Web-based vs. desktop-based vs. desktop-based with data backed up to web server with a tablet thrown in for all cases? In-Reply-To: Message from boB Stepp of "Fri, 17 Jul 2015 23:57:01 -0500." References: <201507180418.t6I4Icn8011379@fido.openend.se> Message-ID: <201507180518.t6I5I0R6012740@fido.openend.se> In a message of Fri, 17 Jul 2015 23:57:01 -0500, boB Stepp writes: >But for this student assessment project, it is going to have to be >without all the desired bells and whistles to have something that will >be practically useful for her when school starts. Especially when I >am certain Vonda is still figuring out what she *really* needs! Every user is like this. You hand them a thing that does 100% exactly what they said they wanted, they play with it for 10 minutes, (or sometimes 10 seconds) and say 'aha! Now what I would really like is ...' This means that you need to have frequent chats with the people who will use the app as you are developing it. And here, you got lucky in that you live with the person you need to talk to. So you won't run into -- we are too busy to talk to you right now, we cancelled the last meeting we had to discuss things, and no, we don't know when we can reschedule -- which can be a problem when doing commercial development. One warning -- how a person uses an app when they are new to it, and learning how to use it is different than how they use it when they are very familiar with it. So, for instance, it is common to hear 'I will never remember the 4 commands. Can I have a pull down menu?' And you give them that. Then, a week later they tell you 'This pull down menu is too slow. Can we have a keyboard shortcut instead?' At this point the 4 commands won't be hard to remember, and may be exactly the keyboard shortcut they are now looking for. It is a thing to remember when you are discussing what users want with the users. Laura From alan.gauld at btinternet.com Sat Jul 18 11:23:02 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 18 Jul 2015 10:23:02 +0100 Subject: [Tutor] Design question: Web-based vs. desktop-based vs. desktop-based with data backed up to web server with a tablet thrown in for all cases? In-Reply-To: References: Message-ID: On 18/07/15 04:20, boB Stepp wrote: > My wife (a Montessori teacher No idea what that is but I'll assume its pretty much a regular type teacher... > For her most urgent project to be done before school starts in 5 weeks For the *urgent* project I'd consider using Sqlite for the data and store it on a cloud server. That will enable sync between devices, provided they don't both make changes at once. Its not super secure but it does provide some level of access control on the cloud and the data isn't in a simple text file so can't be trivially read. You can migrate to a bigger client/server solution later if needed provided you build a data access API from the start. And the more extensive security features need to be added later. Treat the 5 week project as a semi-throwaway prototype learning experience. Other than that Laura's advice is pretty much spot on. > I do own server space and a domain name that I have never used, but > should be adequate for my wife's needs. What software will they allow you to run? The server owners restrictions are likely to be a major factor in your choice of solution. For example do they allow you to install a web framework like Django? Or do you have to use Wordpress or similar? Not all web providers are equal. You may find you are better off using something like Google or Amazon web services. Finally, for a really modern feeling web app you are going to have to use Javascript, possibly quite a lot of it. If you don't already know it, it will be a steep learning curve, so you might want to start reading now! -- 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 cmgcomsol at gmail.com Sat Jul 18 18:59:46 2015 From: cmgcomsol at gmail.com (George) Date: Sat, 18 Jul 2015 22:29:46 +0530 Subject: [Tutor] How to run same lines of code in different order at runtime In-Reply-To: References: <559EB226.5030401@gmail.com> Message-ID: <55AA8602.3040500@gmail.com> On Friday 10 July 2015 02:40 PM, Alan Gauld wrote: > On 10/07/15 01:29, Alan Gauld wrote: > >> It would be easierv to access the last element directly rather than >> looping: >> >> lastCity = route[-1] > > Oops. looping -> calculating. > Thank u Steven and Alan, I am sorry to reply late. Your replies were insightful and particularly Steven's which was more specific to my problem, although i had a slight difficulty in understanding them. I will get back to you with the problem on graph with pylab. Although i am also looking into another solutions to the same problem (me a Python newbie and programming hobbyist). i will do some programming and will get back to you with my results. Thanks again. George. From nymcity at yahoo.com Sat Jul 18 23:07:59 2015 From: nymcity at yahoo.com (Nym City) Date: Sat, 18 Jul 2015 21:07:59 +0000 (UTC) Subject: [Tutor] Socket Module In-Reply-To: References: Message-ID: <741313766.285268.1437253679581.JavaMail.yahoo@mail.yahoo.com> Thank you all for your responses. I have a follow up question: So if gethostbyname_ex() takes only a single hostname string, how can I use it to go through a list of hostnames and get their IP resolution as an output? This would mean,socket.gethostbyaddr() would also not work for my second project. ?Thank you. On Monday, July 13, 2015 2:59 AM, Alan Gauld wrote: On 12/07/15 23:36, Nym City via Tutor wrote: > import csv > import socket > > domains = open('top500domains.csv', 'r') > for domain in domains: >? ? ? domain = socket.gethostbyname(str(domains)) You are passing your file object to gethostbyname() >? ? ? print(domains + "\n") > For the code above, I receive the following error on run: socket.gaierror: [Errno -2] Name or service not known. > Variation 2: > import csv > import socketdomains = [] I assume the domains bit is on a separate line? > > with open('top500domains.csv', 'r') as f: >? ? ? for line in f: >? ? ? ? ? line = line.strip() >? ? ? ? ? domains.append(line) > > hostbyname_ex = socket.gethostbyname_ex(str(domains)) Again you are passing the entire list to gethostbyname_ex() -- 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 danny.yoo at gmail.com Sun Jul 19 01:09:37 2015 From: danny.yoo at gmail.com (Danny Yoo) Date: Sat, 18 Jul 2015 16:09:37 -0700 Subject: [Tutor] Socket Module In-Reply-To: <741313766.285268.1437253679581.JavaMail.yahoo@mail.yahoo.com> References: <741313766.285268.1437253679581.JavaMail.yahoo@mail.yahoo.com> Message-ID: On Jul 18, 2015 3:50 PM, "Nym City via Tutor" wrote: > > Thank you all for your responses. I have a follow up question: > > So if gethostbyname_ex() takes only a single hostname string, how can I use it to go through a list of hostnames and get their IP resolution as an output? > Look into loops. If you have a function that works on a single thing, you can use a loop to apply that function for each element in a list. Any good tutorial should show how to do this. Let us know if you run into difficulties. From robertvstepp at gmail.com Sun Jul 19 07:49:50 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 19 Jul 2015 00:49:50 -0500 Subject: [Tutor] Design question: Web-based vs. desktop-based vs. desktop-based with data backed up to web server with a tablet thrown in for all cases? In-Reply-To: <20150718215816.GI6451@test-chamber-1.castopulence.org> References: <20150718215816.GI6451@test-chamber-1.castopulence.org> Message-ID: On Sat, Jul 18, 2015 at 4:58 PM, Brandon McCaig wrote: > Rather than worrying about technology, frameworks, peripherals, > and protocols you need to focus on the real problem. Have her > describe exactly what she'll do with it. Try to get her to put it > into context of what she does NOW without the software so that > you can compare. I'm 29 years old so I've been away from school > for a long time. I don't know what teachers are using computers > for these days, but I cannot imagine it being useful to walk > around the classroom with a tablet (if anything I don't think I'd > want my children's teachers having such a distraction). At least, > not so useful as to commission the development of custom > software. I'm also not familiar with what a Montessori teacher > does, but it sounds even less likely that custom software will be > useful since it sounds like the students' learning is much less > organized than in a traditional school. The Montessori philosophy of teaching is quite non-traditional. Two or more different grades of children are taught in the same classroom, usually with just one teacher, who may or may not have any helpers. My wife often does not have help, or, if she does, it has not been as helpful as it might. She has 7th, 8th and 9th graders in her class. This coming school year she will have approximately 20 students. At any moment during the school day, all of the students are NOT doing the same thing. Some may be working in small groups. Others will be doing individual work, where it is quite likely that none of these students are doing the same assignment. Each student often has an individualized learning plan. The initial assessment project is quite needful. In the past my wife has wandered about the classroom assessing her students with either a clipboard in hand or trusting her memory until she got back to her desk and able to write down her assessment notes for each student. Note that students do not get traditional grades. Instead, they have a detailed list of skills that they are working towards mastery in. Her assessments will notate each student's progress towards mastery in light of whatever assignments they might be working on. When it comes time to interact with each student's parents, my wife needs to combine all of this assessment data into a report of the student's progress in each skill area to share with the parents. This is quite burdensome to do entirely by hand. The Montessori method of teaching is most frequently associated with children of age three up through sixth grade. There are relatively few examples of junior high/high school Montessori programs and there does not seem to be a good consensus of how to apply the Montessori methods to secondary education. So the main reasons why my wife does not have detailed requirements for what she would like me to program is that she has been working out what is and is not appropriate on her own. After several years of developing her program, she feels she is at the point where software can be a real help for her. BTW, she is very computer literate. She has done programming before when she was a university student and sporadically since then. So she is well aware of what is possible and what can be useful. I have not given you a good idea of what a Montessori education is all about. If you are curious I encourage you to do the usual online searching. In this thread, I kept it very general on purpose. I was probing this community for their thoughts on two things: 1) Should I make the software web-based or desktop-based? 2) What would be the best way of handling the data involved, particularly the data that impinges on student privacy concerns. As I said in the initial thread, some software would be strictly for the teacher's use. Some would be used by the students and the teacher with the potential that the students might have to access that software in their homes (homework). And finally in addition to the students and teachers, some things might be desirable for the parents to be able to monitor their student's progress from home. And then to make things at least a little more concrete I gave a very broad outline of the first project that she (perhaps foolishly ~(: >) ) hopes to have at the start of her school year. And I have been given some great thoughts by this community to help me on my way! I understand and appreciate your concerns. If the client was anyone but my wife, I would not be attempting this for all of the excellent reasons you gave. But Vonda is kinda the lone ranger here in what she is trying to build, and I think I can give her some real help. And from my perspective it is another opportunity to learn many cool new things and push my limits. I never seem to ever find time to just read and study a Python (or other programming-related) book from beginning to end, but when I have a concrete project to do, then at least I start learning *something* out of sheer necessity. As things develop more concretely and I get puzzled and stumped, I will post more detailed questions. Thanks for your thoughts! -- boB From steve at pearwood.info Sun Jul 19 08:27:13 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 19 Jul 2015 16:27:13 +1000 Subject: [Tutor] Design question: Web-based vs. desktop-based vs. desktop-based with data backed up to web server with a tablet thrown in for all cases? In-Reply-To: References: <20150718215816.GI6451@test-chamber-1.castopulence.org> Message-ID: <20150719062713.GK21874@ando.pearwood.info> On Sun, Jul 19, 2015 at 12:49:50AM -0500, boB Stepp wrote: > In this thread, I kept it very general on purpose. I was probing this > community for their thoughts on two things: 1) Should I make the > software web-based or desktop-based? 2) What would be the best way of > handling the data involved, particularly the data that impinges on > student privacy concerns. Not to be a wet blanket, but this is way off-topic for this mailing list. However, you're a regular in good standing, and we don't have a strict rule about staying on-topic (such rules are tiresome and annoying if they allow for no exceptions), so let's go with it. I would start with the simplest thing that can work. With an initial user-base of one person, especially one who is quite computer literate, you don't have to worry about backwards compatibility and can feel free to experiment and expand the software as needed. So I would start with something that just runs on the local machine. Since your missus intends to use a tablet, you may want to investigate Kivy for your GUI needs. http://kivy.org/ I'm not sure if it runs on Macs, but it should work on Android, Windows, and Linux, and of course it is entire Python-based. Once you have a proof-of-concept working for your one user, then you can consider how to expand on it by moving the data to a server and possibly giving access to others. In the meantime, keep everything on the local machine. Take backups of the data by exporting to some sort of regular file which can be zipped up and copied onto a USB stick and locked in a safe. You might consider an encrypted file system if the data is particularly sensitive. But that's a separate issue from the application itself. If you use SQLite for the data storage, it should have a simple "dump" function that would let you export the database to a standard SQL dump format, and reconstruct the database from said dump if needed. Otherwise you would have to write your own export/import routines. If you make the decision to provide multi-user access, then you have a large number of problems to deal with: - security of the database server; - access to the database itself; - what to do if the link between the app and the database goes down? - user authentication and permissions (you don't want little Freddy changing his grades, or accessing other people's records); etc. Why deal with those things from Day One if you only have one user? You don't even know if this application is going to be useful. Start with a proof of concept to prove that it is worth the extra effort. Perhaps your wife will decide that the application isn't useful at all, or that it is useful, but it can remain a single person app and no shared database is needed. But you won't know until you have something to actually experiment with. -- Steve From alan.gauld at btinternet.com Sun Jul 19 09:56:39 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 19 Jul 2015 08:56:39 +0100 Subject: [Tutor] Design question: Web-based vs. desktop-based vs. desktop-based with data backed up to web server with a tablet thrown in for all cases? In-Reply-To: <20150719062713.GK21874@ando.pearwood.info> References: <20150718215816.GI6451@test-chamber-1.castopulence.org> <20150719062713.GK21874@ando.pearwood.info> Message-ID: On 19/07/15 07:27, Steven D'Aprano wrote: > If you use SQLite for the data storage, it should have a simple "dump" > function that would let you export the database to a standard SQL dump > format, and reconstruct the database from said dump if needed. Otherwise > you would have to write your own export/import routines. SQLite is a single file database so you shouldn't need to dump/load it. Just save the file. That's why I suggested Dropbox or similar since you get the benefit of a local copy with automated backup to the cloud. SQLIte provides no login/user access control but the Cloud will provide visibility control so only approved users can access the file. (Of course they can copy it anywhere they want so its not as good as full server based access but for a few users/devices it should be sufficient) -- 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 Sun Jul 19 11:43:13 2015 From: lac at openend.se (Laura Creighton) Date: Sun, 19 Jul 2015 11:43:13 +0200 Subject: [Tutor] Design question: Web-based vs. desktop-based vs. desktop-based with data backed up to web server with a tablet thrown in for all cases? In-Reply-To: Message from "Steven D'Aprano" of "Sun, 19 Jul 2015 16:27:13 +1000." <20150719062713.GK21874@ando.pearwood.info> References: <20150718215816.GI6451@test-chamber-1.castopulence.org> <20150719062713.GK21874@ando.pearwood.info> Message-ID: <201507190943.t6J9hDHJ018370@fido.openend.se> In a message of Sun, 19 Jul 2015 16:27:13 +1000, "Steven D'Aprano" writes: >I'm not sure if it runs on Macs, but it should work on Android, Windows, >and Linux, and of course it is entire Python-based. Python 2.7 only on for MacOSX. Python 3 is coming very soon. Laura From lac at openend.se Mon Jul 20 00:28:55 2015 From: lac at openend.se (Laura Creighton) Date: Mon, 20 Jul 2015 00:28:55 +0200 Subject: [Tutor] Design question: Web-based vs. desktop-based vs. desktop-based with data backed up to web server with a tablet thrown in for all cases? In-Reply-To: Message from Dave P of "Sun, 19 Jul 2015 18:11:36 -0400." References: <20150718215816.GI6451@test-chamber-1.castopulence.org> <20150719062713.GK21874@ando.pearwood.info> <201507190943.t6J9hDHJ018370@fido.openend.se> Message-ID: <201507192228.t6JMStEw003754@fido.openend.se> In a message of Sun, 19 Jul 2015 18:11:36 -0400, Dave P writes: >On Jul 19, 2015 5:43 AM, "Laura Creighton" wrote: >> >> In a message of Sun, 19 Jul 2015 16:27:13 +1000, "Steven D'Aprano" writes: >> >I'm not sure if it runs on Macs, but it should work on Android, Windows, >> >and Linux, and of course it is entire Python-based. >> >> Python 2.7 only on for MacOSX. Python 3 is coming very soon. >> >> Laura > >Hi all, > >I can't help thinking we're trying to reinvent the wheel here. I would be >quite surprised if Trello couldn't help in this situation. (I do not work >for Trello.com) > >Long story short, it's kanban in the cloud and can be adapted for just >about anything. It's free, too! My son's multi-age, non-traditional >school -kids, teachers, and parents alike- use it to track student projects >& goals, plan school events, fundraising, wish lists, supplies lists, and >more. It's easy to use, share, and collaborate. It works in the browser >or you can use their mobile apps. You can set up checklists, attach images >and files, work with due dates, comment and activity streams, etc. Plus, >using their RESTful API, you could whip up some slick automation and >reporting (using Python, of course!) > >I'll be glad to help you and your wife off-list if you'd like some >pointers. > >Good luck! > >Dave boB: this was precisely the sort of solution 'already existing in the cloud' I was hoping you could find. I just didn't know about this one. Very cool. Thank you Dave. Laura From robertvstepp at gmail.com Mon Jul 20 01:15:27 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 19 Jul 2015 18:15:27 -0500 Subject: [Tutor] Design question: Web-based vs. desktop-based vs. desktop-based with data backed up to web server with a tablet thrown in for all cases? In-Reply-To: <201507192228.t6JMStEw003754@fido.openend.se> References: <20150718215816.GI6451@test-chamber-1.castopulence.org> <20150719062713.GK21874@ando.pearwood.info> <201507190943.t6J9hDHJ018370@fido.openend.se> <201507192228.t6JMStEw003754@fido.openend.se> Message-ID: On Sun, Jul 19, 2015 at 5:28 PM, Laura Creighton wrote: > In a message of Sun, 19 Jul 2015 18:11:36 -0400, Dave P writes: >>On Jul 19, 2015 5:43 AM, "Laura Creighton" wrote: >>> >>> In a message of Sun, 19 Jul 2015 16:27:13 +1000, "Steven D'Aprano" writes: >>> >I'm not sure if it runs on Macs, but it should work on Android, Windows, >>> >and Linux, and of course it is entire Python-based. >>> >>> Python 2.7 only on for MacOSX. Python 3 is coming very soon. >>> >>> Laura >> >>Hi all, >> >>I can't help thinking we're trying to reinvent the wheel here. I would be >>quite surprised if Trello couldn't help in this situation. (I do not work >>for Trello.com) >> >>Long story short, it's kanban in the cloud and can be adapted for just >>about anything. It's free, too! My son's multi-age, non-traditional >>school -kids, teachers, and parents alike- use it to track student projects >>& goals, plan school events, fundraising, wish lists, supplies lists, and >>more. It's easy to use, share, and collaborate. It works in the browser >>or you can use their mobile apps. You can set up checklists, attach images >>and files, work with due dates, comment and activity streams, etc. Plus, >>using their RESTful API, you could whip up some slick automation and >>reporting (using Python, of course!) >> >>I'll be glad to help you and your wife off-list if you'd like some >>pointers. >> >>Good luck! >> >>Dave > > boB: this was precisely the sort of solution 'already existing in the > cloud' I was hoping you could find. I just didn't know about this one. > Very cool. Thank you Dave. I will forward this to Vonda and see what she thinks. But from what she has described thus far, I don't think it will give her the type of interface she is hoping to get, or make it easy to organize her information in the way she seems to want. However, it looks like it might do some stuff that I have wanted done on my phone that I used to do on a Palm once upon a time. I will have to glance at the API docs and see what else we might do with it. Thanks! -- boB From lists.davep at gmail.com Mon Jul 20 00:11:36 2015 From: lists.davep at gmail.com (Dave P) Date: Sun, 19 Jul 2015 18:11:36 -0400 Subject: [Tutor] Design question: Web-based vs. desktop-based vs. desktop-based with data backed up to web server with a tablet thrown in for all cases? In-Reply-To: <201507190943.t6J9hDHJ018370@fido.openend.se> References: <20150718215816.GI6451@test-chamber-1.castopulence.org> <20150719062713.GK21874@ando.pearwood.info> <201507190943.t6J9hDHJ018370@fido.openend.se> Message-ID: On Jul 19, 2015 5:43 AM, "Laura Creighton" wrote: > > In a message of Sun, 19 Jul 2015 16:27:13 +1000, "Steven D'Aprano" writes: > >I'm not sure if it runs on Macs, but it should work on Android, Windows, > >and Linux, and of course it is entire Python-based. > > Python 2.7 only on for MacOSX. Python 3 is coming very soon. > > Laura Hi all, I can't help thinking we're trying to reinvent the wheel here. I would be quite surprised if Trello couldn't help in this situation. (I do not work for Trello.com) Long story short, it's kanban in the cloud and can be adapted for just about anything. It's free, too! My son's multi-age, non-traditional school -kids, teachers, and parents alike- use it to track student projects & goals, plan school events, fundraising, wish lists, supplies lists, and more. It's easy to use, share, and collaborate. It works in the browser or you can use their mobile apps. You can set up checklists, attach images and files, work with due dates, comment and activity streams, etc. Plus, using their RESTful API, you could whip up some slick automation and reporting (using Python, of course!) I'll be glad to help you and your wife off-list if you'd like some pointers. Good luck! Dave From nymcity at yahoo.com Mon Jul 20 01:55:33 2015 From: nymcity at yahoo.com (Nym City) Date: Sun, 19 Jul 2015 23:55:33 +0000 (UTC) Subject: [Tutor] Socket Module In-Reply-To: References: Message-ID: <538678716.689471.1437350133540.JavaMail.yahoo@mail.yahoo.com> Thank you for your response. I gave it another try: As suggested, first I ran the concept just in the terminal, and it worked fine: >>> names =['173.252.120.6', '98.139.183.24'] >>> import socket >>> for name in names: ??? socket.gethostbyaddr(name) ??? print(name) ? output:? ('edge-star-shv-12-frc3.facebook.com', [], ['173.252.120.6']) ('ir2.fp.vip.bf1.yahoo.com', [], ['98.139.183.24']) However, when I run it in a program, from a CSV file, it just outputs the content of the CSV file without running it thought socket.gethostbyaddr(): import csv import socket domains = [] with open('top500ips.csv', 'r') as f: ??? for line in f: ??????? line = line.strip() ??????? domains.append(line) for name in domains: ??? socket.gethostbyaddr(name) ??? print(name) output: 173.252.120.6 98.139.183.24 What am I missing? Thank in advance. ?Thank you. On Saturday, July 18, 2015 7:09 PM, Danny Yoo wrote: On Jul 18, 2015 3:50 PM, "Nym City via Tutor" wrote: > > Thank you all for your responses. I have a follow up question: > > So if gethostbyname_ex() takes only a single hostname string, how can I use it to go through a list of hostnames and get their IP resolution as an output? > Look into loops.? If you have a function that works on a single thing, you can use a loop to apply that function for each element in a list.? Any good tutorial should show how to do this.Let us know if you run into difficulties. From danny.yoo at gmail.com Mon Jul 20 03:15:35 2015 From: danny.yoo at gmail.com (Danny Yoo) Date: Sun, 19 Jul 2015 18:15:35 -0700 Subject: [Tutor] Socket Module In-Reply-To: <538678716.689471.1437350133540.JavaMail.yahoo@mail.yahoo.com> References: <538678716.689471.1437350133540.JavaMail.yahoo@mail.yahoo.com> Message-ID: > for name in domains: > socket.gethostbyaddr(name) > print(name) > > output: > 173.252.120.6 > 98.139.183.24 > > What am I missing? Thank in advance. > You have confused yourself a little because the variable names you've chosen are slightly misleading. Specifically, "name" is really an IP address. Hence, your loop here really should be: for addr in domains: ... I would also strongly suggest renaming "domains" to something like "domainAddresses", for similar reasons. Still, you do want a "name" at some point, since that's what you care about. Can you say in human language what the following will return? socket.gethostbyaddr(addr) Can you give it a good name? From robertvstepp at gmail.com Mon Jul 20 03:23:07 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 19 Jul 2015 20:23:07 -0500 Subject: [Tutor] Design question: Web-based vs. desktop-based vs. desktop-based with data backed up to web server with a tablet thrown in for all cases? In-Reply-To: <20150719062713.GK21874@ando.pearwood.info> References: <20150718215816.GI6451@test-chamber-1.castopulence.org> <20150719062713.GK21874@ando.pearwood.info> Message-ID: On Sun, Jul 19, 2015 at 1:27 AM, Steven D'Aprano wrote: > On Sun, Jul 19, 2015 at 12:49:50AM -0500, boB Stepp wrote: > >> In this thread, I kept it very general on purpose. I was probing this >> community for their thoughts on two things: 1) Should I make the >> software web-based or desktop-based? 2) What would be the best way of >> handling the data involved, particularly the data that impinges on >> student privacy concerns. > > Not to be a wet blanket, but this is way off-topic for this mailing > list. However, you're a regular in good standing, and we don't have a > strict rule about staying on-topic (such rules are tiresome and annoying > if they allow for no exceptions), so let's go with it. Thanks for the pass! But I actually thought this was on-topic. I am preparing to write a Python application. I have some questions about how to design it. Why would this be off-topic? I get it that program design applies to all programming languages, but since this list is targeted to people learning programming, I would think we would all need help in these areas, too. Otherwise our Python programs would not be very well written, however correct the Python syntax might be. For the rest of your answer, I think that this is exactly how I should approach this first program. And after having a discussion with Vonda today, it is actually her intent--play with this project until it is in a form that works in her workflow, and then expand upon it. Laura suggested kivy, too, and I think it will best do what is needed for the Slate tablet. Alan's and your suggestion to use SQLite seems a good one as well for the prototype. Thanks to everyone who responded to this thread. It has really clarified my thinking on the best way to get productive as quickly as possible. Now to find the energy after my regular work and the time at home which is always being taken up by my two kids and Vonda! Would it be off-putting later to ask specific questions about implementing features of kivy on this list? I imagine that they would tend to be basic questions that would probably apply to any GUI development. But I'm sure there is a kivy list somewhere once I look for it if I need to go there. I took Laura's suggestion and asked my original questions that started this thread on the tkinter list she suggested, but I have yet to receive an response. One thing I cherish about this list is it is both active and enthusiastic in providing help and direction, even if that help is to suggest other avenues for help. Much better than silence! Thanks! boB From robertvstepp at gmail.com Mon Jul 20 05:59:40 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 19 Jul 2015 22:59:40 -0500 Subject: [Tutor] Design question: Web-based vs. desktop-based vs. desktop-based with data backed up to web server with a tablet thrown in for all cases? In-Reply-To: References: <20150718215816.GI6451@test-chamber-1.castopulence.org> <20150719062713.GK21874@ando.pearwood.info> Message-ID: On Sun, Jul 19, 2015 at 8:23 PM, boB Stepp wrote: > Would it be off-putting later to ask specific questions about > implementing features of kivy on this list? I imagine that they would > tend to be basic questions that would probably apply to any GUI > development. But I'm sure there is a kivy list somewhere once I look > for it if I need to go there. I guess I will answer this question myself: I have just been skimming through the kivy docs tonight and they look to be quite good. Also, I found their Google group and it looks to be quite active. So unless I have a GUI question that seems to fit better here, I will direct my kivy inquiries to their Google group. -- boB From PNiewosz at slb.com Mon Jul 20 09:26:47 2015 From: PNiewosz at slb.com (Patrycja Niewosz) Date: Mon, 20 Jul 2015 07:26:47 +0000 Subject: [Tutor] Connecting with running module and capturing the data Message-ID: <2B14D1AD04128B4AA0B29D4096FC649AFE2C4979@NL0230MBX03N2.DIR.slb.com> Hi All, I am a beginner in python and have just started using it a couple weeks ago. I am using python 3.4.3 on windows 7 machine together with IDLE 3.4.3 for compiling a code. The following steps are: I have a program, called PyAquire, such as: import PyAquireUi import sys from PySide import QtGui import PyAquireMain def main(): master = PyAquireMain.PyAquireMain() app = QtGui.QApplication(sys.argv) ex = PyAquireUi.PyAquireMainWindow(master) sys.exit(app.exec_()) if __name__ == '__main__': main() which opens a new window, where by clicking RUN/STOP button, program acquires data. The data is running in the window. Program runs in the background, acquiring data all the time and my task is to write a code, where I will be able a) to connect with this program without interrupting the process b) capture the current available data c) and save data to excel file. The code I have written is: import PyAquireMain def main(): obj = PyAquireMain.PyAquireMain()# Create instance obj obj.saveSweepData()# call the method from PyAquireMain to save the file if __name__ == '__main__': main() saveSweepData function from PyAquireMain is: def saveSweepData(self): fileSuffix = QtCore.QDateTime.currentDateTime().toString('yyyy-MM-dd_hh.mm.ss') fileType = '.csv' # was .txt if self.ch1.yData is not None: saveFileName = 'data' + fileSuffix + fileType with open(os.path.join(self.settings.saveDir,saveFileName),'wb') as f: # f.writelines(header) np.savetxt(f,self.ch1.yData,fmt='%d',delimiter='\n') Therefore the question is how can I access the PyAquireMain, capture data and save file simultaneously when other program is running in the background. Thanks, Patrycja From alan.gauld at btinternet.com Mon Jul 20 10:48:12 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 20 Jul 2015 09:48:12 +0100 Subject: [Tutor] Design question: Web-based vs. desktop-based vs. desktop-based with data backed up to web server with a tablet thrown in for all cases? In-Reply-To: References: <20150718215816.GI6451@test-chamber-1.castopulence.org> <20150719062713.GK21874@ando.pearwood.info> Message-ID: On 20/07/15 04:59, boB Stepp wrote: >> implementing features of kivy on this list? I imagine that they would >>...But I'm sure there is a kivy list somewhere > I guess I will answer this question myself: I have just been skimming > through the kivy docs tonight and they look to be quite good. Also, I > found their Google group and it looks to be quite active. So unless I > have a GUI question that seems to fit better here, I will direct my > kivy inquiries to their Google group. right answer ;-) -- 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 Jul 20 10:59:59 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 20 Jul 2015 09:59:59 +0100 Subject: [Tutor] Socket Module In-Reply-To: <538678716.689471.1437350133540.JavaMail.yahoo@mail.yahoo.com> References: <538678716.689471.1437350133540.JavaMail.yahoo@mail.yahoo.com> Message-ID: On 20/07/15 00:55, Nym City via Tutor wrote: > Thank you for your response. I gave it another try: > As suggested, first I ran the concept just in the terminal, and it worked fine: >>>> names =['173.252.120.6', '98.139.183.24'] >>>> import socket >>>> for name in names: > socket.gethostbyaddr(name) > print(name) > > output: > ('edge-star-shv-12-frc3.facebook.com', [], ['173.252.120.6']) > ('ir2.fp.vip.bf1.yahoo.com', [], ['98.139.183.24']) Remember that the >>> prompt evaluates your results and automatically prints them for you. Thus >>> 5 + 3 8 But if you put code in a script and execute it the interpreter does NOT print out arbitrary expressions so a file add.py containing just 5 + 3 will not output anything, you need the print function: print(5+3) to see the result. So in the interpreter your gethostbyaddr() function is evaluated AND printed. But in a script it will only be evaluated... > import csv > import socket > > domains = [] > > with open('top500ips.csv', 'r') as f: > for line in f: > line = line.strip() > domains.append(line) You are importing csv but not using it to read your file. But I'll ignore that for now! (in fact it looks like the csv file really only contains the IP addresses, one per line so csv is probably redundant here.) Also you could do all of the above in a single line with: domains = [line.strip() for line in open('top500ips.csv')] But none of that matters for your specific issue... > for name in domains: > socket.gethostbyaddr(name) > print(name) Notice here that you run the gethostbyaddr() function but do nothing with the result. You don't store it and you don't print it. Instead you print the initial name that you passed in, which does not change. You need to create a variable to store the host result and then print that host. And since you want to store a set of hosts you probably want to append the results to a list of some kind (or a dictionary based on your names?) -- 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 Jul 20 11:23:13 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 20 Jul 2015 10:23:13 +0100 Subject: [Tutor] Connecting with running module and capturing the data In-Reply-To: <2B14D1AD04128B4AA0B29D4096FC649AFE2C4979@NL0230MBX03N2.DIR.slb.com> References: <2B14D1AD04128B4AA0B29D4096FC649AFE2C4979@NL0230MBX03N2.DIR.slb.com> Message-ID: On 20/07/15 08:26, Patrycja Niewosz wrote: > I am a beginner in python and have just started using it a couple weeks ago. > I am using python 3.4.3 on windows 7 machine together with IDLE 3.4.3 for compiling a code. > The following steps are: > > I have a program, called PyAquire, such as: Can you tell us where you got it? I did a search and the only one I could find was called pyacquire (with a c) and seems to be an astronomy tool that is allegedly hosted on google code but the site has no code! And no wiki entries or issues or logged changes - in short its dead! Is this the same package or something else? (Or did you write it yourself?) The reason I did the search is that this list is for questions about the Python language and its standard library so normally I'd suggest you ask on the pyaquire list or forum. But since I can't find one that's not much help! The only other place you might get help is the main Python mailing list. > import PyAquireUi > import sys > from PySide import QtGui For PySide questions you can try any of several Qt mailing lists/fora > import PyAquireMain > > def main(): > master = PyAquireMain.PyAquireMain() > app = QtGui.QApplication(sys.argv) > ex = PyAquireUi.PyAquireMainWindow(master) > sys.exit(app.exec_()) > > > > if __name__ == '__main__': > main() > > which opens a new window, where by clicking RUN/STOP button, > program acquires data. The data is running in the window. > Program runs in the background, acquiring data all the time That last line is probably not true, but without knowing more about the module I can't be sure. However I strongly suspect that it is probably running within an event loop which gives your code the opportunity to register an event whereby you can intercept the results. But without any sign of code or documentation we can't really help. > a) to connect with this program without interrupting the process > b) capture the current available data See comment above but we need to see the API. > c) and save data to excel file. There are a couple of modules for saving to Excel native format, but I'd suggest you use a csv file instead since that is compatible with most spreadsheets etc - not everyone uses Excel and you might have to share the results someday! The csv module will do that for you. > The code I have written is: > > import PyAquireMain > > def main(): > obj = PyAquireMain.PyAquireMain()# Create instance obj > obj.saveSweepData()# call the method from PyAquireMain to save the file Its not clear how this relates to the other program above? Do you run the previous code in one interpreter and this code in another? That's almost certainly the wrong way to go about things. > saveSweepData function from PyAquireMain is: > def saveSweepData(self): > fileSuffix = QtCore.QDateTime.currentDateTime().toString('yyyy-MM-dd_hh.mm.ss') > fileType = '.csv' # was .txt > if self.ch1.yData is not None: > saveFileName = 'data' + fileSuffix + fileType > with open(os.path.join(self.settings.saveDir,saveFileName),'wb') as f: > # f.writelines(header) > np.savetxt(f,self.ch1.yData,fmt='%d',delimiter='\n') This doesn't help us very much except that the reference to np suggests this might be part of numpy so the numpy or scipy fora might be able to help. > Therefore the question is how can I access the PyAquireMain, capture data and save file > simultaneously when other program is running in the background. It all depends on how pyaquire does its job. One possibility might be to use the new asyncio module in Python 3.4 but until we know more about this module we can't be sure. -- 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 jon.f.paris at gmail.com Mon Jul 20 16:53:32 2015 From: jon.f.paris at gmail.com (Jon Paris) Date: Mon, 20 Jul 2015 10:53:32 -0400 Subject: [Tutor] Identifying V3 examples Message-ID: I?m having problems identifying sites that feature V3 code. My learning is being hampered by having to worry about conversion for the vast majority of the examples I encounter. Any suggestions on how to deal with this? Jon Paris jon.f.paris at gmail.com From alan.gauld at btinternet.com Tue Jul 21 01:46:34 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Jul 2015 00:46:34 +0100 Subject: [Tutor] Identifying V3 examples In-Reply-To: References: Message-ID: On 20/07/15 15:53, Jon Paris wrote: > I?m having problems identifying sites that feature V3 code. The simplest clues are print foo -> v2 print(foo) -> v3 import Tkinter -> v2 import tkinter -> v3 However, mostly it doesn't make much difference. Are there particular sites or code issues you are having problems with? -- 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 Tue Jul 21 05:05:27 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 21 Jul 2015 13:05:27 +1000 Subject: [Tutor] Identifying V3 examples In-Reply-To: References: Message-ID: <20150721030526.GF25179@ando.pearwood.info> On Mon, Jul 20, 2015 at 10:53:32AM -0400, Jon Paris wrote: > I?m having problems identifying sites that feature V3 code. My > learning is being hampered by having to worry about conversion for the > vast majority of the examples I encounter. > > Any suggestions on how to deal with this? This can be an issue for beginners, so I'm sure you are not alone. But remember, Python 2 and Python 3 share about 98% of the language, the differences are quite small. The most general way to deal with this, in my opinion, is to have both Python 2 and Python 3 installed, and try the example in both and see which one it works in. The most obvious hint that you're using Python 3 is the use of print() with round brackets (parentheses), that is, print as a function: print x # Works in v2, syntax error in v3 print(x) # Likely to be v3 The second most obvious hint is the use of Unicode ("funny non-ASCII characters") as ordinary strings, without the u prefix: s = u"??????????????" # Probably v2 s = "??????????????" # Probably v3 I say "probably" because, starting with version 3.3, Python 3 also supports the u"..." format, to make it easier to port code from v2 to v3. There are a few other changes, like the use of x.next() changing to next(x), some changes in behaviour, some libraries were renamed for consistency with the rest of the standard library, some functions were moved around, etc. If you google for "Python 2 3 changes", you will find plenty of places talking about this: https://duckduckgo.com/html/?q=python+2+3+changes https://startpage.com/do/search?q=python+2+3+changes If in doubt, feel free to ask here! -- Steve From chris_roysmith at internode.on.net Tue Jul 21 07:05:07 2015 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Tue, 21 Jul 2015 15:05:07 +1000 Subject: [Tutor] tkinter window not showing max, min and close buttons Message-ID: I am working my way through Alan Gauld's tutorial and have just started the section on GUI. The windows that are created look odd with no title bar or maximise, minimise or close window button. system Python 2.7 (32 bit) Ubuntu Linux (unity) How might I get things to look like in the tutorial? Thank you Chris Roy-Smith From alan.gauld at btinternet.com Tue Jul 21 09:02:11 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Jul 2015 08:02:11 +0100 Subject: [Tutor] tkinter window not showing max, min and close buttons In-Reply-To: References: Message-ID: On 21/07/15 06:05, Chris Roy-Smith wrote: > I am working my way through Alan Gauld's tutorial and have just started > the section on GUI. The windows that are created look odd with no title > bar or maximise, minimise or close window button. The simplest program that should show the icons is: >>> import Tkinter >>> top = Tkinter.Tk() >>> top.mainloop() What do you see when you type just those lines at the >>> 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 chris_roysmith at internode.on.net Tue Jul 21 09:15:20 2015 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Tue, 21 Jul 2015 17:15:20 +1000 Subject: [Tutor] tkinter window not showing max, min and close buttons In-Reply-To: References: Message-ID: <55ADF188.6060009@internode.on.net> On 21/07/15 17:02, Alan Gauld wrote: > On 21/07/15 06:05, Chris Roy-Smith wrote: >> I am working my way through Alan Gauld's tutorial and have just started >> the section on GUI. The windows that are created look odd with no title >> bar or maximise, minimise or close window button. > > > The simplest program that should show the icons is: > > >>> import Tkinter > >>> top = Tkinter.Tk() > >>> top.mainloop() > > What do you see when you type just those lines at > the >>> prompt? > All I get is a white square, with no way to close without using kill I have done a bit of tinkering and have found that it is only my user account that behaves this way. From chris_roysmith at internode.on.net Tue Jul 21 09:15:20 2015 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Tue, 21 Jul 2015 17:15:20 +1000 Subject: [Tutor] tkinter window not showing max, min and close buttons In-Reply-To: References: Message-ID: <55ADF188.6060009@internode.on.net> On 21/07/15 17:02, Alan Gauld wrote: > On 21/07/15 06:05, Chris Roy-Smith wrote: >> I am working my way through Alan Gauld's tutorial and have just started >> the section on GUI. The windows that are created look odd with no title >> bar or maximise, minimise or close window button. > > > The simplest program that should show the icons is: > > >>> import Tkinter > >>> top = Tkinter.Tk() > >>> top.mainloop() > > What do you see when you type just those lines at > the >>> prompt? > All I get is a white square, with no way to close without using kill I have done a bit of tinkering and have found that it is only my user account that behaves this way. From alan.gauld at btinternet.com Tue Jul 21 13:52:44 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Jul 2015 12:52:44 +0100 Subject: [Tutor] tkinter window not showing max, min and close buttons In-Reply-To: <55ADF188.6060009@internode.on.net> References: <55ADF188.6060009@internode.on.net> Message-ID: <55AE328C.6030003@btinternet.com> On 21/07/15 08:15, Chris Roy-Smith wrote: > On 21/07/15 17:02, Alan Gauld wrote: >> On 21/07/15 06:05, Chris Roy-Smith wrote: >>> I am working my way through Alan Gauld's tutorial and have just started >>> the section on GUI. The windows that are created look odd with no title >>> bar or maximise, minimise or close window button. >> >> >> The simplest program that should show the icons is: >> >> >>> import Tkinter >> >>> top = Tkinter.Tk() >> >>> top.mainloop() >> >> What do you see when you type just those lines at >> the >>> prompt? >> > All I get is a white square, with no way to close without using kill > > I have done a bit of tinkering and have found that it is only my user > account that behaves this way. OK, No idea why that would be happening but it may be a Unity setting - I use Mint/Cinnamon because I hated Unity... Can you try logging out and back in using a different desktop - say XFCE and see if that changes anything. That will hopefully narrow it down to a Unity v User settings issue. In either case it will probably be an issue for another forum since its unlikely to be a Python issue, but please keep me in the loop since it might affect other tutorial users. -- 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 Tue Jul 21 19:35:54 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 21 Jul 2015 18:35:54 +0100 Subject: [Tutor] Identifying V3 examples In-Reply-To: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> Message-ID: <55AE82FA.4060700@btinternet.com> Forwarding to group, please use Reply All when replying to the group. On 21/07/15 14:27, Jon Paris wrote: > Just about everywhere I had looked Alan! > > I had figured out the print() bit pretty early on but some other things were more problematic - particularly when 2to3 basically just added commented names that effectively said to fix it manually. I subsequently found out that the original example (a praised published example) was using poor V2 coding practice and that that was the main reason that 2to3 couldn?t convert it. Name some names. It's hard to guess without seeing examples. > I guess I had just hoped that there were one or two sites that had taken the step of converting V2 examples or at least specialized in V3 examples. Some tutorial sites (including mine) have v3 versions. But libraries take longer to update, especially since writing documentation tends to be a non-favourite job... Some libraries, such as Pillow, should be v3 since it was largely motivated by v3. -- 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 chris_roysmith at internode.on.net Wed Jul 22 00:07:17 2015 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Wed, 22 Jul 2015 08:07:17 +1000 Subject: [Tutor] tkinter window not showing max, min and close buttons In-Reply-To: <55AE328C.6030003@btinternet.com> References: <55ADF188.6060009@internode.on.net> <55AE328C.6030003@btinternet.com> Message-ID: <55AEC295.3080400@internode.on.net> On 21/07/15 21:52, Alan Gauld wrote: > On 21/07/15 08:15, Chris Roy-Smith wrote: >> On 21/07/15 17:02, Alan Gauld wrote: >>> On 21/07/15 06:05, Chris Roy-Smith wrote: >>>> I am working my way through Alan Gauld's tutorial and have just started >>>> the section on GUI. The windows that are created look odd with no title >>>> bar or maximise, minimise or close window button. >>> >>> >>> The simplest program that should show the icons is: >>> >>> >>> import Tkinter >>> >>> top = Tkinter.Tk() >>> >>> top.mainloop() >>> >>> What do you see when you type just those lines at >>> the >>> prompt? >>> >> All I get is a white square, with no way to close without using kill >> >> I have done a bit of tinkering and have found that it is only my user >> account that behaves this way. > > OK, No idea why that would be happening but it may be a Unity > setting - I use Mint/Cinnamon because I hated Unity... > > Can you try logging out and back in using a different desktop - say XFCE > and see if that > changes anything. That will hopefully narrow it down to a Unity v User > settings issue. > > In either case it will probably be an issue for another forum since its > unlikely to be > a Python issue, but please keep me in the loop since it might affect > other tutorial > users. > Thanks Alan, other desktops allow correct operation. Only Unity 3D has the fault, Unity-2D works fine. I had not considered that the desktop could be the problem. I hope to find the offending file or setting in the Unity configuration, as I would prefer to know how to fix this if I need to in the future. A clean install and restore isn't an option because my backup also has the offending error :( I have narrowed it down to 1 machine, so a little detective work should identify the offending file. From jon.f.paris at gmail.com Tue Jul 21 22:19:28 2015 From: jon.f.paris at gmail.com (Jon Paris) Date: Tue, 21 Jul 2015 16:19:28 -0400 Subject: [Tutor] Identifying V3 examples In-Reply-To: <55AE82FA.4060700@btinternet.com> References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> Message-ID: <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> The one example I specifically remember was this one http://code.activestate.com/recipes/532908-text-to-pdf-converter-rewrite/ - I happened to be looking for a simple pdf utility and this one was well reviewed. I subsequently have been told that the parts that 2to3 had trouble with were bad practice to begin with - but what do I know. Most of the other examples 2to3 converted (once I discovered it existed and how to use it in my setup) or I was able to decipher myself. I?ll take a look at your tutorial - thanks. Jon Paris jon.f.paris at gmail.com On Jul 21, 2015, at 1:35 PM, Alan Gauld wrote: > Forwarding to group, please use Reply All when replying to the group. > > On 21/07/15 14:27, Jon Paris wrote: >> Just about everywhere I had looked Alan! >> >> I had figured out the print() bit pretty early on but some other things were more problematic - particularly when 2to3 basically just added commented names that effectively said to fix it manually. I subsequently found out that the original example (a praised published example) was using poor V2 coding practice and that that was the main reason that 2to3 couldn?t convert it. > > Name some names. > It's hard to guess without seeing examples. > > >> I guess I had just hoped that there were one or two sites that had taken the step of converting V2 examples or at least specialized in V3 examples. > > Some tutorial sites (including mine) have v3 versions. But libraries take longer to update, > especially since writing documentation tends to be a non-favourite job... > > Some libraries, such as Pillow, should be v3 since it was largely motivated by v3. > > -- > 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 chris_roysmith at internode.on.net Wed Jul 22 00:07:17 2015 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Wed, 22 Jul 2015 08:07:17 +1000 Subject: [Tutor] tkinter window not showing max, min and close buttons In-Reply-To: <55AE328C.6030003@btinternet.com> References: <55ADF188.6060009@internode.on.net> <55AE328C.6030003@btinternet.com> Message-ID: <55AEC295.3080400@internode.on.net> On 21/07/15 21:52, Alan Gauld wrote: > On 21/07/15 08:15, Chris Roy-Smith wrote: >> On 21/07/15 17:02, Alan Gauld wrote: >>> On 21/07/15 06:05, Chris Roy-Smith wrote: >>>> I am working my way through Alan Gauld's tutorial and have just started >>>> the section on GUI. The windows that are created look odd with no title >>>> bar or maximise, minimise or close window button. >>> >>> >>> The simplest program that should show the icons is: >>> >>> >>> import Tkinter >>> >>> top = Tkinter.Tk() >>> >>> top.mainloop() >>> >>> What do you see when you type just those lines at >>> the >>> prompt? >>> >> All I get is a white square, with no way to close without using kill >> >> I have done a bit of tinkering and have found that it is only my user >> account that behaves this way. > > OK, No idea why that would be happening but it may be a Unity > setting - I use Mint/Cinnamon because I hated Unity... > > Can you try logging out and back in using a different desktop - say XFCE > and see if that > changes anything. That will hopefully narrow it down to a Unity v User > settings issue. > > In either case it will probably be an issue for another forum since its > unlikely to be > a Python issue, but please keep me in the loop since it might affect > other tutorial > users. > Thanks Alan, other desktops allow correct operation. Only Unity 3D has the fault, Unity-2D works fine. I had not considered that the desktop could be the problem. I hope to find the offending file or setting in the Unity configuration, as I would prefer to know how to fix this if I need to in the future. A clean install and restore isn't an option because my backup also has the offending error :( I have narrowed it down to 1 machine, so a little detective work should identify the offending file. From chris_roysmith at internode.on.net Wed Jul 22 00:52:17 2015 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Wed, 22 Jul 2015 08:52:17 +1000 Subject: [Tutor] tkinter window not showing max, min and close buttons In-Reply-To: <55AEC295.3080400@internode.on.net> References: <55ADF188.6060009@internode.on.net> <55AE328C.6030003@btinternet.com> <55AEC295.3080400@internode.on.net> Message-ID: <55AECD21.3060206@internode.on.net> On 22/07/15 08:07, Chris Roy-Smith wrote: > On 21/07/15 21:52, Alan Gauld wrote: >> On 21/07/15 08:15, Chris Roy-Smith wrote: >>> On 21/07/15 17:02, Alan Gauld wrote: >>>> On 21/07/15 06:05, Chris Roy-Smith wrote: >>>>> I am working my way through Alan Gauld's tutorial and have just >>>>> started >>>>> the section on GUI. The windows that are created look odd with no >>>>> title >>>>> bar or maximise, minimise or close window button. >>>> >>>> >>>> The simplest program that should show the icons is: >>>> >>>> >>> import Tkinter >>>> >>> top = Tkinter.Tk() >>>> >>> top.mainloop() >>>> >>>> What do you see when you type just those lines at >>>> the >>> prompt? >>>> >>> All I get is a white square, with no way to close without using kill >>> >>> I have done a bit of tinkering and have found that it is only my user >>> account that behaves this way. >> >> OK, No idea why that would be happening but it may be a Unity >> setting - I use Mint/Cinnamon because I hated Unity... >> >> Can you try logging out and back in using a different desktop - say XFCE >> and see if that >> changes anything. That will hopefully narrow it down to a Unity v User >> settings issue. >> >> In either case it will probably be an issue for another forum since its >> unlikely to be >> a Python issue, but please keep me in the loop since it might affect >> other tutorial >> users. >> > Thanks Alan, other desktops allow correct operation. Only Unity 3D has > the fault, Unity-2D works fine. I had not considered that the desktop > could be the problem. I hope to find the offending file or setting in > the Unity configuration, as I would prefer to know how to fix this if I > need to in the future. A clean install and restore isn't an option > because my backup also has the offending error :( I have narrowed it > down to 1 machine, so a little detective work should identify the > offending file. > Problem solved. Unity settings are stored using GConf. Resetting Unity with the following: dconf reset -f /org/compiz/ then log out and back in. This fixed a few other mysteries at the same time (some windows could not be moved) > _______________________________________________ > 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 Wed Jul 22 02:16:16 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 22 Jul 2015 01:16:16 +0100 Subject: [Tutor] Identifying V3 examples In-Reply-To: <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> Message-ID: On 21/07/15 21:19, Jon Paris wrote: > The one example I specifically remember was this one http://code.activestate.com/recipes/ For Activestate check the languages tab. You can choose to see only Python 2 or Python 3 recipes. 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 breamoreboy at yahoo.co.uk Wed Jul 22 15:52:10 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 22 Jul 2015 14:52:10 +0100 Subject: [Tutor] Identifying V3 examples In-Reply-To: <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> Message-ID: On 21/07/2015 21:19, Jon Paris wrote: > The one example I specifically remember was this one http://code.activestate.com/recipes/532908-text-to-pdf-converter-rewrite/ - I happened to be looking for a simple pdf utility and this one was well reviewed. I subsequently have been told that the parts that 2to3 had trouble with were bad practice to begin with - but what do I know. Most of the other examples 2to3 converted (once I discovered it existed and how to use it in my setup) or I was able to decipher myself. I've just run that recipe through 2to3 with no problem, so exactly what do you mean by "parts that 2to3 had trouble with"? Possibly this? - except IOError, (strerror, errno): - print 'Error: Could not open file to read --->', self._ifile + except IOError as xxx_todo_changeme: + (strerror, errno) = xxx_todo_changeme.args + print('Error: Could not open file to read --->', self._ifile) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From jon.f.paris at gmail.com Wed Jul 22 15:49:57 2015 From: jon.f.paris at gmail.com (Jon Paris) Date: Wed, 22 Jul 2015 09:49:57 -0400 Subject: [Tutor] Identifying V3 examples In-Reply-To: References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> Message-ID: <6E68C610-A6B0-4E04-87CD-5EDEB1B99728@gmail.com> Thanks - I?m pretty sure that was how I got to the updated version of that recipe in the first place. I assumed that if you were on a page of V3 examples that the search option would only give you V3 results. It doesn?t. It does seem to restrict the results to Python but ignores the fact that you are on the V3 list. Jon Paris jon.f.paris at gmail.com On Jul 21, 2015, at 8:16 PM, Alan Gauld wrote: > On 21/07/15 21:19, Jon Paris wrote: >> The one example I specifically remember was this one http://code.activestate.com/recipes/ > > For Activestate check the languages tab. > You can choose to see only Python 2 or Python 3 recipes. > > 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 > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From jon.f.paris at gmail.com Wed Jul 22 16:16:22 2015 From: jon.f.paris at gmail.com (Jon Paris) Date: Wed, 22 Jul 2015 10:16:22 -0400 Subject: [Tutor] Identifying V3 examples In-Reply-To: References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> Message-ID: <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> Yup - the ?xxx_todo_changeme? was the part that I meant. That might not constitute a ?problem? for you but, for someone just starting out, exactly what is needed to correct it was not obvious. I have subsequently resolved the issue. Jon Paris jon.f.paris at gmail.com On Jul 22, 2015, at 9:52 AM, Mark Lawrence wrote: > On 21/07/2015 21:19, Jon Paris wrote: >> The one example I specifically remember was this one http://code.activestate.com/recipes/532908-text-to-pdf-converter-rewrite/ - I happened to be looking for a simple pdf utility and this one was well reviewed. I subsequently have been told that the parts that 2to3 had trouble with were bad practice to begin with - but what do I know. Most of the other examples 2to3 converted (once I discovered it existed and how to use it in my setup) or I was able to decipher myself. > > I've just run that recipe through 2to3 with no problem, so exactly what do you mean by "parts that 2to3 had trouble with"? > > Possibly this? > > - except IOError, (strerror, errno): > - print 'Error: Could not open file to read --->', self._ifile > + except IOError as xxx_todo_changeme: > + (strerror, errno) = xxx_todo_changeme.args > + print('Error: Could not open file to read --->', self._ifile) > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence > > _______________________________________________ > 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 Jul 22 23:31:34 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 22 Jul 2015 22:31:34 +0100 Subject: [Tutor] Identifying V3 examples In-Reply-To: <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> Message-ID: On 22/07/2015 15:16, Jon Paris wrote: > Yup - the ?xxx_todo_changeme? was the part that I meant. > > That might not constitute a ?problem? for you but, for someone just starting out, exactly what is needed to correct it was not obvious. I have subsequently resolved the issue. > > > Jon Paris > jon.f.paris at gmail.com > Good to hear, but would you please not top post here, it drives me insane trying to read things that are arse about face, thank you. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From jon.f.paris at gmail.com Thu Jul 23 15:59:22 2015 From: jon.f.paris at gmail.com (Jon Paris) Date: Thu, 23 Jul 2015 09:59:22 -0400 Subject: [Tutor] Identifying V3 examples In-Reply-To: References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> Message-ID: On Jul 22, 2015, at 5:31 PM, Mark Lawrence wrote: > On 22/07/2015 15:16, Jon Paris wrote: >> Yup - the ?xxx_todo_changeme? was the part that I meant. >> >> That might not constitute a ?problem? for you but, for someone just starting out, exactly what is needed to correct it was not obvious. I have subsequently resolved the issue. >> >> >> Jon Paris >> jon.f.paris at gmail.com >> > > Good to hear, but would you please not top post here, it drives me insane trying to read things that are arse about face, thank you. > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor I am not familiar with the term ?top post? - I?m guessing you mean that my reply came before your original message. My email does it that way because that is my preference - and for that matter most people I do business with. I will however try to remember that at least some people on this list don?t like it. Of course the minute I change it somebody else will probably complain about that! Jon Paris jon.f.paris at gmail.com From alan.gauld at btinternet.com Thu Jul 23 16:18:20 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 23 Jul 2015 15:18:20 +0100 Subject: [Tutor] Identifying V3 examples In-Reply-To: References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> Message-ID: On 23/07/15 14:59, Jon Paris wrote: > I am not familiar with the term ?top post? See this wikipedia article which describes in detail all the alternatives along with their relative merits. https://en.wikipedia.org/wiki/Posting_style including this commonly seen example: Because it messes up the order in which people normally read text. > Why is top-posting such a bad thing? >> Top-posting. >>> What is the most annoying thing in e-mail? Which with bottom posting becomes the more readable: >>> What is the most annoying thing in e-mail? >> Top-posting. > Why is top-posting such a bad thing? Because it messes up the order in which people normally read text. Most technical mailing lists and newsgroups prefer interleaved posting where replies to individual points in a message are placed just under the relevant part of the message. Just as importantly all irrelevant parts of the message should be deleted. The common business practice of top posting was encouraged by Microsoft Outlook and results in many megabytes of wasted disk-space due to long threads of mail being posted multiple times in every reply to the thread. It used to drive me mad when I was a corporate wage slave... :-) -- 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 Thu Jul 23 16:41:33 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 24 Jul 2015 00:41:33 +1000 Subject: [Tutor] Identifying V3 examples In-Reply-To: References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> Message-ID: <20150723144132.GL25179@ando.pearwood.info> On Thu, Jul 23, 2015 at 09:59:22AM -0400, Jon Paris wrote: > I am not familiar with the term ?top post? - I?m guessing you mean > that my reply came before your original message. Yes, it means "post at the top". Hence, "top post". A: Because it messes up the order in which you read. Q: Why is that? A: Top posting. Q: What is the most annoying email practice? In these sorts of technical forums, email is a discussion between multiple parties, not just two, often in slow motion (sometimes replies may not come in for a week, or a month). Often, a single email will reply to anything up to a dozen or twenty individual points. Top posting works reasonably well for short replies answering one, maybe two brief points where the context is obvious. In technical discussions like we have here, that is rarely the case. People may be reading these emails on the archives years from now, in any order. Establishing context before answering the question makes sense. Without context, our answers may not make sense. Hence we quote the part we are replying to before we answer it: Q: What is the most annoying email practice? A: Top posting. Q: Why is that? A: Because it messes up the order in which you read. > My email does it that way because that is my preference - and for that > matter most people I do business with. I will however try to remember > that at least some people on this list don?t like it. Of course the > minute I change it somebody else will probably complain about that! What you do in your business emails is up to you, but in my experience (and YMMV) is that business emails are a wasteland of lazy and incompetent replies from people who barely bother to read your email before banging out the shortest top-posted response they can. Not that I'm bitter :-) If I had a dollar for every time I've asked a customer or supplier three questions, and they've answered the middle question and not the two others, I'd be a wealthy man. But maybe I've just been unlucky :-) But I digress. You may find this helpful: https://en.wikipedia.org/wiki/Posting_style -- Steve From lac at openend.se Thu Jul 23 17:58:08 2015 From: lac at openend.se (Laura Creighton) Date: Thu, 23 Jul 2015 17:58:08 +0200 Subject: [Tutor] Identifying V3 examples In-Reply-To: Message from Jon Paris of "Thu, 23 Jul 2015 09:59:22 -0400." References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> Message-ID: <201507231558.t6NFw8fs027032@fido.openend.se> In a message of Thu, 23 Jul 2015 09:59:22 -0400, Jon Paris writes: >I am not familiar with the term ?top post? - I?m guessing you mean that my reply came before your original message. > >My email does it that way because that is my preference - and for that matter most people I do business with. I will however try to remember that at least some people on this list don?t like it. Of course the minute I change it somebody else will probably complain about that! Not on this list, or on nearly any of the python.org lists. We had this conversation nearly 2 decades ago, and the people who like to read posts interleaved, with new content after what it refers to won. There is a current certain problem with email readers for smartphones that don't let you do this, but that's not your problem, we see. :) >Jon Paris >jon.f.paris at gmail.com Welcome! Laura Creighton From jon.f.paris at gmail.com Thu Jul 23 16:55:13 2015 From: jon.f.paris at gmail.com (Jon Paris) Date: Thu, 23 Jul 2015 10:55:13 -0400 Subject: [Tutor] Identifying V3 examples In-Reply-To: <20150723144132.GL25179@ando.pearwood.info> References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> <20150723144132.GL25179@ando.pearwood.info> Message-ID: <258B7EC4-78FC-48EA-9FF5-A359FFCCE63C@gmail.com> On Jul 23, 2015, at 10:41 AM, Steven D'Aprano wrote: > On Thu, Jul 23, 2015 at 09:59:22AM -0400, Jon Paris wrote: > >> I am not familiar with the term ?top post? - I?m guessing you mean >> that my reply came before your original message. > > Yes, it means "post at the top". Hence, "top post". > > A: Because it messes up the order in which you read. > Q: Why is that? > A: Top posting. > Q: What is the most annoying email practice? > > > In these sorts of technical forums, email is a discussion between > multiple parties, not just two, often in slow motion (sometimes replies > may not come in for a week, or a month). Often, a single email will > reply to anything up to a dozen or twenty individual points. Top posting > works reasonably well for short replies answering one, maybe two brief > points where the context is obvious. In technical discussions like we > have here, that is rarely the case. > > People may be reading these emails on the archives years from now, in > any order. Establishing context before answering the question makes > sense. Without context, our answers may not make sense. Hence we > quote the part we are replying to before we answer it: > > Q: What is the most annoying email practice? > A: Top posting. > Q: Why is that? > A: Because it messes up the order in which you read. > > >> My email does it that way because that is my preference - and for that >> matter most people I do business with. I will however try to remember >> that at least some people on this list don?t like it. Of course the >> minute I change it somebody else will probably complain about that! > > What you do in your business emails is up to you, but in my experience > (and YMMV) is that business emails are a wasteland of lazy and > incompetent replies from people who barely bother to read your email > before banging out the shortest top-posted response they can. Not that > I'm bitter :-) If I had a dollar for every time I've asked a customer or > supplier three questions, and they've answered the middle question and > not the two others, I'd be a wealthy man. But maybe I've just been > unlucky :-) > > But I digress. You may find this helpful: > > https://en.wikipedia.org/wiki/Posting_style > > > > -- > Steve > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor See my response to Alan. Jon Paris jon.f.paris at gmail.com From jon.f.paris at gmail.com Thu Jul 23 18:17:11 2015 From: jon.f.paris at gmail.com (Jon Paris) Date: Thu, 23 Jul 2015 12:17:11 -0400 Subject: [Tutor] Identifying V3 examples In-Reply-To: <201507231558.t6NFw8fs027032@fido.openend.se> References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> <201507231558.t6NFw8fs027032@fido.openend.se> Message-ID: <1D6182FE-5C84-4E80-B17D-00E764AD06B7@gmail.com> On Jul 23, 2015, at 11:58 AM, Laura Creighton wrote: > In a message of Thu, 23 Jul 2015 09:59:22 -0400, Jon Paris writes: >> I am not familiar with the term ?top post? - I?m guessing you mean that my reply came before your original message. >> >> My email does it that way because that is my preference - and for that matter most people I do business with. I will however try to remember that at least some people on this list don?t like it. Of course the minute I change it somebody else will probably complain about that! > > Not on this list, or on nearly any of the python.org lists. We had this > conversation nearly 2 decades ago, and the people who like to read posts > interleaved, with new content after what it refers to won. > > There is a current certain problem with email readers for smartphones > that don't let you do this, but that's not your problem, we see. :) Thank you Laura. That?s the nicest response I have had to-date. I think I may have finally done it right so hopefully nobody will jump on my head this time. I guess the Python world is primarily a subset of the Unix/Linux world and that (and all of its conventions) is still somewhat alien to me. > >> Jon Paris >> jon.f.paris at gmail.com > > Welcome! > > Laura Creighton From jon.paris at partner400.com Thu Jul 23 16:54:09 2015 From: jon.paris at partner400.com (Jon Paris) Date: Thu, 23 Jul 2015 10:54:09 -0400 Subject: [Tutor] Identifying V3 examples In-Reply-To: References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> Message-ID: On Jul 23, 2015, at 10:18 AM, Alan Gauld wrote: > On 23/07/15 14:59, Jon Paris wrote: > >> I am not familiar with the term ?top post? > > See this wikipedia article which describes in detail all the > alternatives along with their relative merits. > > https://en.wikipedia.org/wiki/Posting_style > > including this commonly seen example: > > Because it messes up the order in which people normally read text. > > Why is top-posting such a bad thing? > >> Top-posting. > >>> What is the most annoying thing in e-mail? > > Which with bottom posting becomes the more readable: > > >>> What is the most annoying thing in e-mail? > >> Top-posting. > > Why is top-posting such a bad thing? > Because it messes up the order in which people normally read text. > > Most technical mailing lists and newsgroups prefer interleaved > posting where replies to individual points in a message are > placed just under the relevant part of the message. Just as > importantly all irrelevant parts of the message should be deleted. > > The common business practice of top posting was encouraged by > Microsoft Outlook and results in many megabytes of wasted > disk-space due to long threads of mail being posted multiple > times in every reply to the thread. It used to drive me mad > when I was a corporate wage slave... :-) > > -- > 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 I?ve been posting to many different sites for twenty plus years and never had this kind of complaint. I can?t even find a way of telling my email client (Mac) to do it the way you want. Right now I?m manually changing every response to comply with the required etiquette. Personally I find it more useful to see the response and then look below for the content. But that?s just me. I will try not to bother you again. Jon Paris www.partner400.com www.SystemiDeveloper.com From lac at openend.se Thu Jul 23 20:50:10 2015 From: lac at openend.se (Laura Creighton) Date: Thu, 23 Jul 2015 20:50:10 +0200 Subject: [Tutor] Identifying V3 examples In-Reply-To: Message from Jon Paris of "Thu, 23 Jul 2015 10:55:13 -0400." <258B7EC4-78FC-48EA-9FF5-A359FFCCE63C@gmail.com> References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> <20150723144132.GL25179@ando.pearwood.info><258B7EC4-78FC-48EA-9FF5-A359FFCCE63C@gmail.com> Message-ID: <201507231850.t6NIoA0V031136@fido.openend.se> In a message of Thu, 23 Jul 2015 10:55:13 -0400, Jon Paris writes: >On Jul 23, 2015, at 10:41 AM, Steven D'Aprano wrote: >> In these sorts of technical forums, email is a discussion between >> multiple parties, not just two, often in slow motion (sometimes replies >> may not come in for a week, or a month). Often, a single email will >> reply to anything up to a dozen or twenty individual points. Top posting >> works reasonably well for short replies answering one, maybe two brief >> points where the context is obvious. In technical discussions like we >> have here, that is rarely the case. >> Steve > > >See my response to Alan. > > >Jon Paris >jon.f.paris at gmail.com Bottom posting is an improvement on top posting, but I see that you have rapidly moved to the point where it is time for your next trick. :) Including all the text and then adding a short comment on the bottom is only a slight improvement on having a short comment on the top and then including all the text. Because right now I had to read all of what Steve said, again, and once was more than enough for me. :) So in order to be compassionate to your readers, you trim your reply, deleting all the lines that aren't relevant. I have marked these places where I did a lot of deleting with the text but that isn't necessary. Also some people like to use the combination %< %< %< or >% >% >% because it looks like 3 scissors (at least with the font they are using). If you see text like that, that is what is going on. Laura From lac at openend.se Thu Jul 23 20:55:18 2015 From: lac at openend.se (Laura Creighton) Date: Thu, 23 Jul 2015 20:55:18 +0200 Subject: [Tutor] Identifying V3 examples In-Reply-To: Message from Jon Paris of "Thu, 23 Jul 2015 10:54:09 -0400." References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> Message-ID: <201507231855.t6NItIDW031267@fido.openend.se> In a message of Thu, 23 Jul 2015 10:54:09 -0400, Jon Paris writes: >I?ve been posting to many different sites for twenty plus years and never had this kind of complaint. I can?t even find a way of telling my email client (Mac) to do it the way you want. Right now I?m manually changing every response to comply with the required etiquette. >Jon Paris You may find this program useful. http://www.macupdate.com/app/mac/40735/href=%27 But you still have to go back and trim out unnecessary verbiage. Laura From lac at openend.se Thu Jul 23 22:14:51 2015 From: lac at openend.se (Laura Creighton) Date: Thu, 23 Jul 2015 22:14:51 +0200 Subject: [Tutor] Identifying V3 examples In-Reply-To: Message from Jon Paris of "Thu, 23 Jul 2015 15:10:14 -0400." References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> <201507231855.t6NItIDW031267@fido.openend.se> Message-ID: <201507232014.t6NKEpea000694@fido.openend.se> In a message of Thu, 23 Jul 2015 15:10:14 -0400, Jon Paris writes: > >Thanks for the info Laura - I don?t think I can use it though unless it provides for activation against only one email account. For the vast majority of my mail (this is my only usenet type group) I need it the ?normal? way. > >Anyway - thanks again. You?re the first ?friendly face? I?ve encountered here. This is really sad. The tutor list is _supposed_ to be friendly. If we aren't being friendly, or worse, not being compassionate, then we are _failing_ at our job of providing an environment where people can learn how to program in Python. >Jon Paris Laura From lac at openend.se Thu Jul 23 22:42:00 2015 From: lac at openend.se (Laura Creighton) Date: Thu, 23 Jul 2015 22:42:00 +0200 Subject: [Tutor] Identifying V3 examples In-Reply-To: Message from Jon Paris of "Thu, 23 Jul 2015 16:23:29 -0400." References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> <201507231855.t6NItIDW031267@fido.openend.se> <201507232014.t6NKEpea000694@fido.openend.se> Message-ID: <201507232042.t6NKg0Bt001440@fido.openend.se> In a message of Thu, 23 Jul 2015 16:23:29 -0400, Jon Paris writes: >Well I confess that is what I was expecting, and certainly you have been very friendly for which I thank you. It did feel a little odd to come to a beginners group and immediately get dumped on. > >C?est la via. I have now found a number of people familiar with Python in my own ?universe? (IBM i systems) so hopefully I won?t have to run the gauntlet here too often! > >Thanks again. >>> Jon Paris >> >> Laura Local people are always the best resource. But, alas, most people on this list are actually very freindly. It is just that top posting bothers some of them worse than being stung by killer bees. I have no idea why this is so. I hope you will come back with Python questions, and give us a chance to redeem ourselves, though I perfectly understand if we have utterly worn out our welcome with you. Laura From lac at openend.se Thu Jul 23 23:02:25 2015 From: lac at openend.se (Laura Creighton) Date: Thu, 23 Jul 2015 23:02:25 +0200 Subject: [Tutor] Identifying V3 examples In-Reply-To: Message from Jon Paris of "Thu, 23 Jul 2015 15:10:14 -0400." References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> <201507231855.t6NItIDW031267@fido.openend.se> Message-ID: <201507232102.t6NL2P9m001973@fido.openend.se> In a message of Thu, 23 Jul 2015 15:10:14 -0400, Jon Paris writes: >> You may find this program useful. >> http://www.macupdate.com/app/mac/40735/href=%27 >> >> But you still have to go back and trim out unnecessary verbiage. >> >> Laura > >Thanks for the info Laura - I don?t think I can use it though unless it provides for activation against only one email account. For the vast majority of my mail (this is my only usenet type group) I need it the ?normal? way. Good news. I don't have a mac, so I decided to abuse the quotefix issue tracker to find out if what you want to do is available. See: https://github.com/robertklep/quotefixformac/issues/48 Now, Robert Klep, who is also a helpful soul answered the query only about 10 seconds after I had made it, said that you can install the plugin, set it up to do top posting by default, and then on a per message basis just type Alt/Opt to get it turned into a bottom posted thing. Sounds perfect to me. But if you have more questions, I suggest you get yourself a github account if you do not already have one and go talk to Robert about it in the issue tracker as part of this issue. I guarantee he is very friendly. Laura From jon.paris at partner400.com Thu Jul 23 21:10:14 2015 From: jon.paris at partner400.com (Jon Paris) Date: Thu, 23 Jul 2015 15:10:14 -0400 Subject: [Tutor] Identifying V3 examples In-Reply-To: <201507231855.t6NItIDW031267@fido.openend.se> References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> <201507231855.t6NItIDW031267@fido.openend.se> Message-ID: On Jul 23, 2015, at 2:55 PM, Laura Creighton wrote: > In a message of Thu, 23 Jul 2015 10:54:09 -0400, Jon Paris writes: >> I?ve been posting to many different sites for twenty plus years and never had this kind of complaint. I can?t even find a way of telling my email client (Mac) to do it the way you want. Right now I?m manually changing every response to comply with the required etiquette. > >> Jon Paris > > You may find this program useful. > http://www.macupdate.com/app/mac/40735/href=%27 > > But you still have to go back and trim out unnecessary verbiage. > > Laura Thanks for the info Laura - I don?t think I can use it though unless it provides for activation against only one email account. For the vast majority of my mail (this is my only usenet type group) I need it the ?normal? way. Anyway - thanks again. You?re the first ?friendly face? I?ve encountered here. Jon Paris www.partner400.com www.SystemiDeveloper.com From jon.paris at partner400.com Thu Jul 23 22:23:29 2015 From: jon.paris at partner400.com (Jon Paris) Date: Thu, 23 Jul 2015 16:23:29 -0400 Subject: [Tutor] Identifying V3 examples In-Reply-To: <201507232014.t6NKEpea000694@fido.openend.se> References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> <201507231855.t6NItIDW031267@fido.openend.se> <201507232014.t6NKEpea000694@fido.openend.se> Message-ID: On Jul 23, 2015, at 4:14 PM, Laura Creighton wrote: > In a message of Thu, 23 Jul 2015 15:10:14 -0400, Jon Paris writes: >> >> Thanks for the info Laura - I don?t think I can use it though unless it provides for activation against only one email account. For the vast majority of my mail (this is my only usenet type group) I need it the ?normal? way. >> >> Anyway - thanks again. You?re the first ?friendly face? I?ve encountered here. > > This is really sad. The tutor list is _supposed_ to be friendly. If > we aren't being friendly, or worse, not being compassionate, then we > are _failing_ at our job of providing an environment where people > can learn how to program in Python. Well I confess that is what I was expecting, and certainly you have been very friendly for which I thank you. It did feel a little odd to come to a beginners group and immediately get dumped on. C?est la via. I have now found a number of people familiar with Python in my own ?universe? (IBM i systems) so hopefully I won?t have to run the gauntlet here too often! Thanks again. > >> Jon Paris > > Laura From jon.paris at partner400.com Thu Jul 23 22:45:44 2015 From: jon.paris at partner400.com (Jon Paris) Date: Thu, 23 Jul 2015 16:45:44 -0400 Subject: [Tutor] Identifying V3 examples In-Reply-To: <201507232042.t6NKg0Bt001440@fido.openend.se> References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> <201507231855.t6NItIDW031267@fido.openend.se> <201507232014.t6NKEpea000694@fido.openend.se> <201507232042.t6NKg0Bt001440@fido.openend.se> Message-ID: <05EE73DB-1F51-4222-B78B-612B36A1BB06@partner400.com> On Jul 23, 2015, at 4:42 PM, Laura Creighton wrote: > In a message of Thu, 23 Jul 2015 16:23:29 -0400, Jon Paris writes: > >> Well I confess that is what I was expecting, and certainly you have been very friendly for which I thank you. It did feel a little odd to come to a beginners group and immediately get dumped on. >> >> C?est la via. I have now found a number of people familiar with Python in my own ?universe? (IBM i systems) so hopefully I won?t have to run the gauntlet here too often! >> >> Thanks again. > >>>> Jon Paris >>> >>> Laura > > Local people are always the best resource. But, alas, most people on this > list are actually very freindly. It is just that top posting bothers > some of them worse than being stung by killer bees. I have no idea > why this is so. > > I hope you will come back with Python questions, and give us a chance to > redeem ourselves, though I perfectly understand if we have utterly > worn out our welcome with you. > > Laura > Thanks Laura - we?ll see how it goes. My alternate resources are by no means local - they are on other internet lists but let?s just say they are less obsessed with etiquette than some here. From jon.paris at partner400.com Thu Jul 23 23:09:45 2015 From: jon.paris at partner400.com (Jon Paris) Date: Thu, 23 Jul 2015 17:09:45 -0400 Subject: [Tutor] Identifying V3 examples In-Reply-To: <201507232102.t6NL2P9m001973@fido.openend.se> References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> <201507231855.t6NItIDW031267@fido.openend.se> <201507232102.t6NL2P9m001973@fido.openend.se> Message-ID: On Jul 23, 2015, at 5:02 PM, Laura Creighton wrote: > In a message of Thu, 23 Jul 2015 15:10:14 -0400, Jon Paris writes: >>> You may find this program useful. >>> http://www.macupdate.com/app/mac/40735/href=%27 >>> >>> But you still have to go back and trim out unnecessary verbiage. >>> >>> Laura >> >> Thanks for the info Laura - I don?t think I can use it though unless it provides for activation against only one email account. For the vast majority of my mail (this is my only usenet type group) I need it the ?normal? way. > > Good news. > > I don't have a mac, so I decided to abuse the quotefix issue tracker to > find out if what you want to do is available. > > See: > https://github.com/robertklep/quotefixformac/issues/48 > > Now, Robert Klep, who is also a helpful soul answered the query only about > 10 seconds after I had made it, said that you can install the plugin, > set it up to do top posting by default, and then on a per message basis > just type Alt/Opt to get it turned into a bottom posted thing. > > Sounds perfect to me. But if you have more questions, I suggest you > get yourself a github account if you do not already have one and go talk > to Robert about it in the issue tracker as part of this issue. I > guarantee he is very friendly. > > Laura Brilliant Laura - thank you so very much - I?ll download it now. From alan.gauld at btinternet.com Fri Jul 24 02:08:23 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 24 Jul 2015 01:08:23 +0100 Subject: [Tutor] Identifying V3 examples In-Reply-To: <201507231850.t6NIoA0V031136@fido.openend.se> References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> <20150723144132.GL25179@ando.pearwood.info><258B7EC4-78FC-48EA-9FF5-A359FFCCE63C@gmail.com> <201507231850.t6NIoA0V031136@fido.openend.se> Message-ID: On 23/07/15 19:50, Laura Creighton wrote: > Also some people like to use the combination %< %< %< or >% >% >% A new one on me, but I kind of like it. ;-) I usually just use -- 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 Fri Jul 24 02:16:03 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 24 Jul 2015 01:16:03 +0100 Subject: [Tutor] Identifying V3 examples In-Reply-To: References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> Message-ID: On 23/07/15 15:54, Jon Paris wrote: > I?ve been posting to many different sites for twenty plus years > and never had this kind of complaint. It's not really a complaint from me, I was just explaining Mark's comment. When I was working (now semi-retired) it did drive me nuts because with 200-300 emails a day it added significant extra effort. Nowadays I rarely get more than 100 messages in a day so I've time to browse if needed. > I can?t even find a way of telling my email client (Mac) to do > it the way you want. Sadly that's the way many newer mail tools are going. Customisation in terms of colouring, fonts etc but not in actual functionality. > Personally I find it more useful to see the response and then > look below for the content. But that?s just me. Obviously not, or top posting wouldn't be so popular. But it tends to only work well when the thread is active and you can recall the gist of the content quickly. It's not so easy on messages that are a year or two old, as in list archives. -- 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 emile at fenx.com Fri Jul 24 02:21:51 2015 From: emile at fenx.com (Emile van Sebille) Date: Thu, 23 Jul 2015 17:21:51 -0700 Subject: [Tutor] Identifying V3 examples In-Reply-To: References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> <20150723144132.GL25179@ando.pearwood.info><258B7EC4-78FC-48EA-9FF5-A359FFCCE63C@gmail.com> <201507231850.t6NIoA0V031136@fido.openend.se> Message-ID: On 7/23/2015 5:08 PM, Alan Gauld wrote: > On 23/07/15 19:50, Laura Creighton wrote: > >> Also some people like to use the combination %< %< %< or >% >% >% > > A new one on me, but I kind of like it. ;-) > I usually just use > > I include them generally to bracket code intended to be cut and paste into the interpreter. eg ---8<---8<---8<---8<---8<---8<--- import sys for ii in sys.path: print ii ---8<---8<---8<---8<---8<---8<--- YMMV, Emile From alan.gauld at btinternet.com Fri Jul 24 02:23:06 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 24 Jul 2015 01:23:06 +0100 Subject: [Tutor] Identifying V3 examples In-Reply-To: References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> <201507231855.t6NItIDW031267@fido.openend.se> Message-ID: On 23/07/15 20:10, Jon Paris wrote: > Anyway - thanks again. You?re the first ?friendly face? I?ve encountered here. Hi Jon, that slightly worries me as list moderator. Can you explain what you mean (off list if you prefer). You received many answers to your original query and none of them seemed unfriendly to me? Mark commented on the top posting but even that was a polite request advising you of the list etiquette. It didn't seem particularly unfriendly? We do try to make the tutor list a safe place to learn and ask questions, for beginners of every level and background. (Although your comment about Python being slightly Unixy-geek oriented is true enough, as it is for most non MS specific programming languages.) -- 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 jon.f.paris at gmail.com Fri Jul 24 16:18:24 2015 From: jon.f.paris at gmail.com (Jon Paris) Date: Fri, 24 Jul 2015 10:18:24 -0400 Subject: [Tutor] Identifying V3 examples In-Reply-To: References: <6D550B75-456B-4B60-8785-C60BB4B74883@gmail.com> <55AE82FA.4060700@btinternet.com> <6A28D0EC-2EC3-4CB6-819C-B207D476412B@gmail.com> <7989FEFE-F7C6-4BE8-82D8-031474E619CD@gmail.com> <201507231855.t6NItIDW031267@fido.openend.se> Message-ID: <3DCF7349-3775-434C-A4CD-03A1EB9D695A@gmail.com> On Jul 23, 2015, at 8:23 PM, Alan Gauld wrote: > On 23/07/15 20:10, Jon Paris wrote: > >> Anyway - thanks again. You?re the first ?friendly face? I?ve encountered here. > > Hi Jon, that slightly worries me as list moderator. > > Can you explain what you mean (off list if you prefer). > You received many answers to your original query and > none of them seemed unfriendly to me? And I think the fact it didn?t seem unfriendly to you sums up the problem Alan. I came asking what I thought was a simple question. What I got for the most part was (I felt) somewhat patronizing and subsequently treated me like an idiot for not knowing the local etiquette. That?s certainly the way it seemed to me. This to me just seems to be something that happens more on the Unix/Linux oriented lists of this world which are not, as I said before, my natural habitat. > Mark commented on the top posting but even that was > a polite request advising you of the list etiquette. > It didn't seem particularly unfriendly? Again it may not have seemed unfriendly to you - but it did to me. Polite? Yes - but total overkill. Wouldn?t a simple ?The preference for this list is to either embed your responses in the original message or to put your response at the end of the original.? and a link to the guidelines have been enough? > We do try to make the tutor list a safe place to learn > and ask questions, for beginners of every level and > background. (Although your comment about Python being > slightly Unixy-geek oriented is true enough, as it > is for most non MS specific programming languages.) My background is not MS. I?m from the IBM i midrange world. All I can say is that when I first got into PHP some years ago I found the community a little friendlier - and that was on lists that did not purport to be for newbies. I?m sure this list does a great job and hopefully if I have to come back again I?ll feel better about it. Right now my entire experience (excluding Laura who went above and beyond to be helpful) left me feeling like an ignorant child who has been chastised for not following rules he did not know about. Anyway I?ve wasted more than enough of my and your time on this - I will try to be better behaved in the future. -- > 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 Jon Paris jon.f.paris at gmail.com From abhijeet560 at yahoo.in Fri Jul 24 18:37:00 2015 From: abhijeet560 at yahoo.in (abhijeet560 at yahoo.in) Date: Fri, 24 Jul 2015 16:37:00 +0000 (UTC) Subject: [Tutor] Help Message-ID: <888929326.1302819.1437755820463.JavaMail.yahoo@mail.yahoo.com> Dear Pythonist?I wanted to develop any application for my college project using python .I have the basic of the language and wanted to develop any real time software or application .I am not getting any help or idea you know i am just getting a little unsure java is popular and my college classmates are using java .I don't know please help me out from this situation? From breamoreboy at yahoo.co.uk Fri Jul 24 22:52:40 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 24 Jul 2015 21:52:40 +0100 Subject: [Tutor] Help In-Reply-To: <888929326.1302819.1437755820463.JavaMail.yahoo@mail.yahoo.com> References: <888929326.1302819.1437755820463.JavaMail.yahoo@mail.yahoo.com> Message-ID: On 24/07/2015 17:37, abhijeet560 at yahoo.in wrote: > Dear Pythonist I wanted to develop any application for my college project using python .I have the basic of the language and wanted to develop any real time software or application .I am not getting any help or idea you know i am just getting a little unsure java is popular and my college classmates are using java .I don't know please help me out from this situation? You will not want to be writing real time software in Python, unless it's the control system for the steering system on a super tanker. This is very much the domain of languages like C, C++ or Java. There may be other more modern languages but I don't follow too closely so you'll have to do further research. Here's a starter anyway http://stackoverflow.com/questions/697916/what-languages-are-used-for-real-time-systems-programming. And I've just remembered http://www.erlang.org/ -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From cangle at bpsk12.com Fri Jul 24 20:49:49 2015 From: cangle at bpsk12.com (Claudia Angle) Date: Fri, 24 Jul 2015 14:49:49 -0400 Subject: [Tutor] Help Message-ID: I installed python but I am unable to decompress the pyserial library. I need a free and safe program as it is my school comuter. I am using the irobot Create (Roomba ). From alan.gauld at btinternet.com Fri Jul 24 23:33:47 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 24 Jul 2015 22:33:47 +0100 Subject: [Tutor] Help In-Reply-To: <888929326.1302819.1437755820463.JavaMail.yahoo@mail.yahoo.com> References: <888929326.1302819.1437755820463.JavaMail.yahoo@mail.yahoo.com> Message-ID: On 24/07/15 17:37, abhijeet560 at yahoo.in wrote: > I wanted to develop any application for my college project using python. OK, That is easy enough, what kind of application do you have in mind? An office tool? A game? A web site? There are lots of possibilities. > I have the basic of the language and wanted to develop any > real time software or application . Real Time software is extremely hard to write well in any language. And genuine real-time is almost impossible in Python (and most other interpreted languages). Pseudo Real-Time is perhaps do-able. What do you mean by real-time? Do you have an example? Controlling some hardware for example? (How fast does the hardware respond - seconds, milliseconds, microseconds, less?) Then we can decide whether Python is even worth considering. (Even if it isn't suitable for the real-time bits it may be fine for other parts of the application such as the user interface) Or maybe you are using real-time to mean something different to that? In which case the opportunities may be greater. For example if you just mean something that responds to user commands while the user waits (like a word processor say) then its a very different story. > I am not getting any help or idea you know i am just getting > a little unsure java is popular and my college classmates > are using java. I normally wouldn't consider Java good for real real-time either, but it is way better than Python for that. But there are many other options too. Can you fill us in a bit more on your background - other programming skills? project ideas? OS and hardware you are using? timescale available? Budget, if applicable. Anything of that sort will help. -- 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 jobrh2993 at gmail.com Sat Jul 25 03:49:26 2015 From: jobrh2993 at gmail.com (Job Hernandez) Date: Fri, 24 Jul 2015 18:49:26 -0700 Subject: [Tutor] (no subject) Message-ID: I have been reading a book on Python. I am currently stuck with one of the exercises and so wanted to ask you if you can help me. Of course, if you have the time. Exercise : Ask the user to input 3 integers and prints out the largest odd number. if no odd number was entered it should print a message o that effect. These lines of code don't work : a = raw_input('enter number: ') b = raw_input('enter number: ') c = raw_input('enter number: ') list = [ a, b, c] list2 =[ ] for x in list: if x%2 == 1: # responsible for the type error: not all arguments converted during string # #formatting list2.append(x) print list2 w = max(list2) print ' %d is the largest odd number.' % w # i don't know but maybe I have to return the list for this to work? Because if I assign a #variable to to 3 integers, like the code below it works. But these do: a = 3 b = 7 c = 9 list = [ a, b, c] list2 =[] for x in list: if x%2 == 1: list2.append(x) print list2 w = max(list2) print ' %d is the largest odd number.' % w #Thank you for your time. Sincerely , Job From alan.gauld at btinternet.com Sat Jul 25 10:43:45 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 25 Jul 2015 09:43:45 +0100 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 25/07/15 02:49, Job Hernandez wrote: > These lines of code don't work : > > a = raw_input('enter number: ') > b = raw_input('enter number: ') > c = raw_input('enter number: ') raw_input() returns a string. So if you enter 6, say, it is stored as the character '6' not the number 6. You need to use the int() function to convert it to an integer or float() to convert it to a floating point number. eg. c = int(raw_input('enter number: ')) > list = [ a, b, c] A small point: its not a good idea to use the type name as your variable because that then hides the function used to convert things to lists. ie you can no longer do >>> list('abc') ['a','b','c'] Its better to use names that describe the content of the data, so in your case something like inputs or numbers. Just a small point which makes no difference in this example but might trip you up in future. > list2 =[ ] > > for x in list: > if x%2 == 1: # responsible for the type error: not all arguments You get the error because you are trying to divide a character by a number. If you convert to int() up above then it will go away. > list2.append(x) > print list2 > > w = max(list2) > -- 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 paul at whoosh.cn Sat Jul 25 13:15:31 2015 From: paul at whoosh.cn (Paul Z) Date: Sat, 25 Jul 2015 19:15:31 +0800 Subject: [Tutor] How to generate a pure tones and random noise using Python? Message-ID: Hi All, I try to train my listening by using python. (estimating the frequency of sound) So... Are there some way to generate a fixed frequency sound in different waves (eg. Sine Wave, Saw Wave, Triangle Wave etc.) and different random noise. (eg. white noise & pink noise) ? I have search in web, some people say that I can use winsound which can generate a fixed frequency beep. However, It is Windows only. (I'm under Linux) and I think It is not a professional library to generate audio signal. How about pygame? and some examples? any ideas? Thanks Paul Z From lac at openend.se Sat Jul 25 22:35:24 2015 From: lac at openend.se (Laura Creighton) Date: Sat, 25 Jul 2015 22:35:24 +0200 Subject: [Tutor] How to generate a pure tones and random noise using Python? In-Reply-To: Message from Paul Z of "Sat, 25 Jul 2015 19:15:31 +0800." References: Message-ID: <201507252035.t6PKZOk8002265@fido.openend.se> In a message of Sat, 25 Jul 2015 19:15:31 +0800, Paul Z writes: >Hi All, > >I try to train my listening by using python. (estimating the frequency of sound) >So... Are there some way to generate a fixed frequency sound in different waves (eg. Sine Wave, Saw Wave, Triangle Wave etc.) and different random noise. (eg. white noise & pink noise) ? > >I have search in web, some people say that I can use winsound which can generate a fixed frequency beep. However, It is Windows only. (I'm under Linux) and I think It is not a professional library to generate audio signal. > >How about pygame? and some examples? >any ideas? > >Thanks > >Paul Z >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >https://mail.python.org/mailman/listinfo/tutor I think you want this: https://zach.se/generate-audio-with-python/ https://github.com/zacharydenton/wavebender pygame will not give you want you want. blender might. I am not sure but worth googling for. Laura From robertvstepp at gmail.com Sat Jul 25 23:08:03 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 25 Jul 2015 16:08:03 -0500 Subject: [Tutor] _ vs. _name vs. __name vs. name_ vs. __name__ usages Message-ID: After having a long discussion with my wife on her user requirements, I am convinced that an OO approach is required. Which is just as well as that has been one of my next areas of learning to do. I am currently reading "Python 3 Object Oriented Programming" by Dusty Phillips, which so far seems to be a reasonably good text. This has led me to the subject line topics. >From my understandings to date: 1) A single underscore is used conventionally for a "throw-away" variable, such as a loop index for which the index value is not actually used in a subsequent calculation. 2) _name is used inside class methods to indicate that the programmer's intention is that this name should only be accessed internally by that particular class. Other supposedly "adult" Python programmers will attempt to respect this original intent if they use my code. 3) __name invokes Python's name mangling mechanism. The intent of this usage is to not allow subclasses of the class containing __name to access this name, and to add additional emphasis to future users of my code that this name is meant to be strictly hands-off. 4) name_ is used when one is "forced" to use one of Python's reserved words as a name. 5) __name__ is meant to be used only by the creators of Python for their special built-in methods, such as __init__, __new__, etc. Are my understandings above correct or flawed? For (3), it seems to me that one would normally be able to use the simpler _name construction from (2). What would be a best-practice example of when name mangling *should* be used? Likewise, it seems that normally (4) should never be needed, though I have a feeling that I have seen something in tkinter recently that suggests some exceptions, but I cannot (yet) bring it to mind. And for (5), surely I should never violate this one? It seems that in some future edition of Python they might add any particular __name__ that I might try to use presently in their future version of Python (however miniscule that possibility might actually be). Thanks! boB -- boB From lac at openend.se Sat Jul 25 23:42:07 2015 From: lac at openend.se (Laura Creighton) Date: Sat, 25 Jul 2015 23:42:07 +0200 Subject: [Tutor] _ vs. _name vs. __name vs. name_ vs. __name__ usages In-Reply-To: Message from boB Stepp of "Sat, 25 Jul 2015 16:08:03 -0500." References: Message-ID: <201507252142.t6PLg7hh003765@fido.openend.se> In a message of Sat, 25 Jul 2015 16:08:03 -0500, boB Stepp writes: >After having a long discussion with my wife on her user requirements, >I am convinced that an OO approach is required. Which is just as well >as that has been one of my next areas of learning to do. I am >currently reading "Python 3 Object Oriented Programming" by Dusty >Phillips, which so far seems to be a reasonably good text. This has >led me to the subject line topics. > >>From my understandings to date: > >1) A single underscore is used conventionally for a "throw-away" >variable, such as a loop index for which the index value is not >actually used in a subsequent calculation. Some people do this. I find it irritatingly twee, and hard to read as well. I like 'junk' for such things. >2) _name is used inside class methods to indicate that the >programmer's intention is that this name should only be accessed >internally by that particular class. Other supposedly "adult" Python >programmers will attempt to respect this original intent if they use >my code. The world is packed very full of Python programmers who have never heard of this rule, and thus this is sort of a 'pious hope' more than anything else. The world is also full of python modules that have no single underscore variable names at all. >3) __name invokes Python's name mangling mechanism. The intent of >this usage is to not allow subclasses of the class containing __name >to access this name, and to add additional emphasis to future users of >my code that this name is meant to be strictly hands-off. worry about this when you want to share your code with the world and don't want new users to be able to rely on things to always be the way you have it right now. i.e. adding double underscores is often the last thing you do before release. >4) name_ is used when one is "forced" to use one of Python's reserved >words as a name. You are never forced to. Sometimes you might want to. Sometimes name_ is a good choice in this case. But in my life 'stop wanting this, you arrogant wretch' has mostly been the correct thing. :) (At least I think never. Maybe I am being an arrogrant wretch here, too.) >5) __name__ is meant to be used only by the creators of Python for >their special built-in methods, such as __init__, __new__, etc. > >Are my understandings above correct or flawed? Pretty much dead on. > >For (3), it seems to me that one would normally be able to use the >simpler _name construction from (2). What would be a best-practice >example of when name mangling *should* be used? If you do not mangle the names, you are making a promise, sometimes a half-hearted promise, that through the life of the code other people can come by and use these methods, with exactly this signature, and it will just work. This is called 'being Part of the public API'. If you publish such things and then, next release, remove those methods or change the number or order of the arguments .... their code will break. If its just you and your tapeworm, you don't care and you make and remove methods and signatures all the time. If people do not like it, tough, and so it goes. It's what they get for running your experimental software. If you publish your code as a module on PyPI people will find ways to use the public API, so if you want to reserve the right to delete this method later, or change it's signature then by all means mangle the thing. In the Python world people are generally less worried about what will happen if some wretch gets a hold of a method that I didn't really want as part of the public API than what will happen if they badly need to get a hold of a method and I didn't make it possible. This makes for a lot of the single underscore variables. 'I don't want to make it impossible for you to access things this way, but I wish you wouldn't' more or less. > >Likewise, it seems that normally (4) should never be needed, though I >have a feeling that I have seen something in tkinter recently that >suggests some exceptions, but I cannot (yet) bring it to mind. Not sure what you are thinking about. >And for (5), surely I should never violate this one? It seems that in >some future edition of Python they might add any particular __name__ >that I might try to use presently in their future version of Python >(however miniscule that possibility might actually be). Right. Don't go there. > >Thanks! >boB > >-- >boB MY .02 euros. Others will have other ideas. but that is the way I see it. Laura From zachary.ware+pytut at gmail.com Sat Jul 25 23:58:32 2015 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Sat, 25 Jul 2015 16:58:32 -0500 Subject: [Tutor] _ vs. _name vs. __name vs. name_ vs. __name__ usages In-Reply-To: References: Message-ID: On Saturday, July 25, 2015, boB Stepp > wrote: > > 5) __name__ is meant to be used only by the creators of Python for > their special built-in methods, such as __init__, __new__, etc. Everything up to this point was pretty accurate. You're only half right with this one, though; __dunder__ names are ones that you should only use when hooking into the "magic" of the interpreter. That is, you should not define your own __dunder__ names, but feel free to use ones that have been defined by the implementation and documented as the way to do something. For example: class Spam: def __init__(self, value): self.value = value def __getitem__(self, key): return self.value assert Spam(42)['eggs'] == 42 __getitem__ is the documented method to implement to allow instances of your class to be indexed like a list or dict. Are my understandings above correct or flawed? > > For (3), it seems to me that one would normally be able to use the > simpler _name construction from (2). What would be a best-practice > example of when name mangling *should* be used? I have yet to ever see a place where name mangling was warranted. I have been severely annoyed by it before, though. To make a particular class work the way I wanted it to, I had to subclass it and explicitly override a couple of mangled names. In my opinion, name mangling should never be used unless overriding the value will set your CPU aflame. Likewise, it seems that normally (4) should never be needed, though I > have a feeling that I have seen something in tkinter recently that > suggests some exceptions, but I cannot (yet) bring it to mind. There are a couple of methods in tkinter that accept an 'in_' keyword argument, where in Tcl it is documented as 'in', which is a Python keyword. In code that's not interfacing with something else that uses a Python keyword, it's usually best to just find a different name. And for (5), surely I should never violate this one? It seems that in > some future edition of Python they might add any particular __name__ > that I might try to use presently in their future version of Python > (however miniscule that possibility might actually be). > Right, don't make up your own __dunder__ names. Hope this helps, -- Zach (On an iPad) -- Sent from Gmail Mobile From breamoreboy at yahoo.co.uk Sun Jul 26 02:51:19 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 26 Jul 2015 01:51:19 +0100 Subject: [Tutor] _ vs. _name vs. __name vs. name_ vs. __name__ usages In-Reply-To: References: Message-ID: On 25/07/2015 22:08, boB Stepp wrote: > After having a long discussion with my wife on her user requirements, > I am convinced that an OO approach is required. Which is just as well > as that has been one of my next areas of learning to do. I am > currently reading "Python 3 Object Oriented Programming" by Dusty > Phillips, which so far seems to be a reasonably good text. This has > led me to the subject line topics. > > From my understandings to date: > > 1) A single underscore is used conventionally for a "throw-away" > variable, such as a loop index for which the index value is not > actually used in a subsequent calculation. It is not a convention, it is inbuilt. It is very useful as linter type tools don't complain about you defining something but not using it. > 2) _name is used inside class methods to indicate that the > programmer's intention is that this name should only be accessed > internally by that particular class. Other supposedly "adult" Python > programmers will attempt to respect this original intent if they use > my code. Correct. > 3) __name invokes Python's name mangling mechanism. The intent of > this usage is to not allow subclasses of the class containing __name > to access this name, and to add additional emphasis to future users of > my code that this name is meant to be strictly hands-off. > > 4) name_ is used when one is "forced" to use one of Python's reserved > words as a name. I don't know about reserved words but it is certainly used rather than override a built-in name. > 5) __name__ is meant to be used only by the creators of Python for > their special built-in methods, such as __init__, __new__, etc. Correct. > > Are my understandings above correct or flawed? > > For (3), it seems to me that one would normally be able to use the > simpler _name construction from (2). What would be a best-practice > example of when name mangling *should* be used? I'd be inclined not to bother yourself with this. I've never used it in the centuries that I've been writing Python, and somebody who is determined enough can work around it anyway owing to Python's dynamic nature. > Likewise, it seems that normally (4) should never be needed, though I > have a feeling that I have seen something in tkinter recently that > suggests some exceptions, but I cannot (yet) bring it to mind. If you like a name enough and cannot think of a better alternative why not? > > And for (5), surely I should never violate this one? It seems that in > some future edition of Python they might add any particular __name__ > that I might try to use presently in their future version of Python > (however miniscule that possibility might actually be). Definitely. > > Thanks! > boB > No problem. Once again my invoice is in the post, your cheque by return please :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From nymcity at yahoo.com Sun Jul 26 00:13:06 2015 From: nymcity at yahoo.com (Nym City) Date: Sat, 25 Jul 2015 22:13:06 +0000 (UTC) Subject: [Tutor] Socket Module In-Reply-To: References: Message-ID: <1702356350.2680742.1437862386083.JavaMail.yahoo@mail.yahoo.com> Thank you all for your responses. I have taken your feedback and made changes to my code. -Danny, per your suggestion, I have renamed some of my variables to make their purpose little more clearer. - Alan,? I have created a new host list (ResolvedAddresses) which is storing the output from socket.gethostbyaddr(address). Here is the updated code: https://bpaste.net/show/358583e1a0bd The issue that I am running into now is that for some reason, the script is not resolving known-public IP addresses that I am passing through. For example, some of the IPs, that I have used are for sites like facebook (173.252.120.6) github (207.97.227.239), however the script is not able to resolve them. But its interesting that I am able to resolve them using nslookup on windows command prompt. Which means my laptop's DNS setting is fine. Looking forward to your feedback. Thanks! ?Thank you. On Monday, July 20, 2015 5:00 AM, Alan Gauld wrote: On 20/07/15 00:55, Nym City via Tutor wrote: > Thank you for your response. I gave it another try: > As suggested, first I ran the concept just in the terminal, and it worked fine: >>>> names =['173.252.120.6', '98.139.183.24'] >>>> import socket >>>> for name in names: >? ? ? socket.gethostbyaddr(name) >? ? ? print(name) > > output: > ('edge-star-shv-12-frc3.facebook.com', [], ['173.252.120.6']) > ('ir2.fp.vip.bf1.yahoo.com', [], ['98.139.183.24']) Remember that the >>> prompt evaluates your results and automatically prints them for you. Thus >>> 5 + 3 8 But if you put code in a script and execute it the interpreter does NOT print out arbitrary expressions so a file add.py containing just 5 + 3 will not output anything, you need the print function: print(5+3) to see the result. So in the interpreter your gethostbyaddr() function is evaluated AND printed. But in a script it will only be evaluated... > import csv > import socket > > domains = [] > > with open('top500ips.csv', 'r') as f: >? ? ? for line in f: >? ? ? ? ? line = line.strip() >? ? ? ? ? domains.append(line) You are importing csv but not using it to read your file. But I'll ignore that for now! (in fact it looks like the csv file really only contains the IP addresses, one per line so csv is probably redundant here.) Also you could do all of the above in a single line with: domains = [line.strip() for line in open('top500ips.csv')] But none of that matters for your specific issue... > for name in domains: >? ? ? socket.gethostbyaddr(name) >? ? ? print(name) Notice here that you run the gethostbyaddr() function but do nothing with the result. You don't store it and you don't print it. Instead you print the initial name that you passed in, which does not change. You need to create a variable to store the host result and then print that host. And since you want to store a set of hosts you probably want to append the results to a list of some kind (or a dictionary based on your names?) -- 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 ben+python at benfinney.id.au Sun Jul 26 03:14:30 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 26 Jul 2015 11:14:30 +1000 Subject: [Tutor] _ vs. _name vs. __name vs. name_ vs. __name__ usages References: Message-ID: <85io97n2ix.fsf@benfinney.id.au> boB Stepp writes: > From my understandings to date: > > 1) A single underscore is used conventionally for a "throw-away" > variable, such as a loop index for which the index value is not > actually used in a subsequent calculation. That accurately describes common usage. But it's important to also know that ?_? has numerous conflicting common usages (including a widely-used text translation function). Less common, but IMO much more preferable for ?don't actually want this value but need to bind something to it?, is the convention ?__? as a name:: for (__, important_value) in sequence_of_tuples: ... The name ?__? doesn't AFAIK have conflicting usages (therefore more strongly connoting this meaning), is pretty easy to type and much easier to spot than a single underscore. > 2) _name is used inside class methods to indicate that the > programmer's intention is that this name should only be accessed > internally by that particular class. Not so much ?inside class methods?; rather, the names are on attributes of a class (or module or, sometimes, function). This is the convention for ?even if you can see this attribute, it is an implementation detail, not part of the public API for this class, anything might change about this in future revisions?. With no leading underscore, the implicit promise is that the name is a published API for the code, and can be relied on to keep the same name and behaviour in future revisions of the code. > 3) __name invokes Python's name mangling mechanism. The intent of this > usage is to not allow subclasses of the class containing __name to > access this name, and to add additional emphasis to future users of my > code that this name is meant to be strictly hands-off. > > [?] it seems to me that one would normally be able to use the > simpler _name construction Right. I've felt any need to use this in my Python programming career; the distinction between ?public API? (?foo?) versus ?implementation detail? (?_foo?) is plenty. > 4) name_ is used when one is "forced" to use one of Python's reserved > words as a name. Yes. It's a last resort, IMO, but a valuable one; and the only damage is to readability (difficult to distinguish when reading quickly). > it seems that normally (4) should never be needed, though I have a > feeling that I have seen something in tkinter recently that suggests > some exceptions, but I cannot (yet) bring it to mind. The Python keywords and built-in object names include some obvious, common words. This is a clear benefit, but it can also cause a clash when choosing your own names: some of the best ones are already taken. I've seen this used to name ?assert_?, ?file_?, and the like. > 5) __name__ is meant to be used only by the creators of Python for > their special built-in methods, such as __init__, __new__, etc. > > [?] surely I should never violate this one? It seems that in > some future edition of Python they might add any particular __name__ > that I might try to use presently in their future version of Python > (however miniscule that possibility might actually be). Yes. Never name anything with this ?__foo__? style unless Python already will treat it specially, and your intention is to invoke that special treatment by Python. This convention is violated too often in third-party code for names that don't have any special significance to Python, leading to needless confusion about how special a newly-encoutered name really is. All the ones I've seen in third-party code would always be improved for clarity by choosing a normal ?foo? or ?_foo? name. > Are my understandings above correct or flawed? I hope that helps. -- \ ?It's dangerous to be right when the government is wrong.? | `\ ?Francois Marie Arouet Voltaire | _o__) | Ben Finney From anuragvyas14 at gmail.com Sun Jul 26 06:34:19 2015 From: anuragvyas14 at gmail.com (Anurag Vyas) Date: Sun, 26 Jul 2015 10:04:19 +0530 Subject: [Tutor] Help with List Message-ID: Hello Team, I am noob to programming. I am trying to remove a item from a list. The item that I am trying to remove has multiple entries in the list. I am not able to remove all the entries. Please Help. The code is in the attachment. -- Thanks & Regards Anurag Vyas From steve at pearwood.info Sun Jul 26 10:24:19 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 26 Jul 2015 18:24:19 +1000 Subject: [Tutor] Help with List In-Reply-To: References: Message-ID: <20150726082418.GU25179@ando.pearwood.info> On Sun, Jul 26, 2015 at 10:04:19AM +0530, Anurag Vyas wrote: > Hello Team, > I am noob to programming. I am trying to remove a item from a list. The > item that I am trying to remove has multiple entries in the list. I am not > able to remove all the entries. > Please Help. The code is in the attachment. There is no attachment. Perhaps you forgot to attach it? In general, if your code is less than 20 or 30 lines, you should just include it in the body of your email. If your code is more than 30 lines, you should simplify it so that it is smaller. To delete a single item from a list: alist = ['a', 'b', 'c', 'd', 'e'] alist.remove('b') To delete multiple entries with the same value: alist = ['a', 'b', 'c', 'b', 'd', 'b', 'e', 'b', 'b'] try: while True: alist.remove('b') except ValueError: pass but this will probably be more efficient: alist = ['a', 'b', 'c', 'b', 'd', 'b', 'e', 'b', 'b'] alist = [item for item in alist if item != 'b'] That last one is called a "list comprehension", and is a short cut for a for-loop like this: tmp = [] for item in alist: if item != 'b': tmp.append(item) alist = tmp -- Steve From paul at whoosh.cn Sun Jul 26 13:29:19 2015 From: paul at whoosh.cn (Paul Z) Date: Sun, 26 Jul 2015 19:29:19 +0800 Subject: [Tutor] How to generate a pure tones and random noise using Python? In-Reply-To: <201507252035.t6PKZOk8002265@fido.openend.se> References: , <201507252035.t6PKZOk8002265@fido.openend.se> Message-ID: Hi Laura, Thanks for you helpful reply, and it is exactly what I want. I'm going to learn Python from write a ears training program. ;-) And Is there have a library to scope(print) this wave form? (real-time and non real-time) I have already started to learn "generate audio with python", Thank you. :-) Paul Z ---------------------------------------- > To: paul at whoosh.cn > CC: tutor at python.org; lac at openend.se > From: lac at openend.se > Subject: Re: [Tutor] How to generate a pure tones and random noise using Python? > Date: Sat, 25 Jul 2015 22:35:24 +0200 > > In a message of Sat, 25 Jul 2015 19:15:31 +0800, Paul Z writes: >>Hi All, >> >>I try to train my listening by using python. (estimating the frequency of sound) >>So... Are there some way to generate a fixed frequency sound in different waves (eg. Sine Wave, Saw Wave, Triangle Wave etc.) and different random noise. (eg. white noise & pink noise) ? >> >>I have search in web, some people say that I can use winsound which can generate a fixed frequency beep. However, It is Windows only. (I'm under Linux) and I think It is not a professional library to generate audio signal. >> >>How about pygame? and some examples? >>any ideas? >> >>Thanks >> >>Paul Z >>_______________________________________________ >>Tutor maillist - Tutor at python.org >>To unsubscribe or change subscription options: >>https://mail.python.org/mailman/listinfo/tutor > > I think you want this: > https://zach.se/generate-audio-with-python/ > https://github.com/zacharydenton/wavebender > > pygame will not give you want you want. > > blender might. I am not sure but worth googling for. > > Laura > From paul at whoosh.cn Sun Jul 26 18:10:29 2015 From: paul at whoosh.cn (Paul Z) Date: Mon, 27 Jul 2015 00:10:29 +0800 Subject: [Tutor] How to generate a pure tones and random noise using Python? In-Reply-To: References: , , <201507252035.t6PKZOk8002265@fido.openend.se>, Message-ID: Hi Laura, I just have read this webpage about Generate Audio with Python. It seem writing by Python 2? Esp. itertools (izip, imap). I try to change izip and imap to zip and map, but I get error: beatfreq, amplitude = remainder.split('/') ValueError: need more than 1 value to unpack Are there some way to conver it to the code of Python 3? I'm too anxious to hear the sound from Python though I'm a absolute beginner. Many thanks Paul Z ---------------------------------------- > From: paul at whoosh.cn > To: lac at openend.se > Date: Sun, 26 Jul 2015 19:29:19 +0800 > Subject: Re: [Tutor] How to generate a pure tones and random noise using Python? > CC: tutor at python.org > > Hi Laura, > > Thanks for you helpful reply, and it is exactly what I want. > > I'm going to learn Python from write a ears training program. ;-) > And Is there have a library to scope(print) this wave form? (real-time and non real-time) > > I have already started to learn "generate audio with python", Thank you. :-) > > Paul Z > > ---------------------------------------- >> To: paul at whoosh.cn >> CC: tutor at python.org; lac at openend.se >> From: lac at openend.se >> Subject: Re: [Tutor] How to generate a pure tones and random noise using Python? >> Date: Sat, 25 Jul 2015 22:35:24 +0200 >> >> In a message of Sat, 25 Jul 2015 19:15:31 +0800, Paul Z writes: >>>Hi All, >>> >>>I try to train my listening by using python. (estimating the frequency of sound) >>>So... Are there some way to generate a fixed frequency sound in different waves (eg. Sine Wave, Saw Wave, Triangle Wave etc.) and different random noise. (eg. white noise & pink noise) ? >>> >>>I have search in web, some people say that I can use winsound which can generate a fixed frequency beep. However, It is Windows only. (I'm under Linux) and I think It is not a professional library to generate audio signal. >>> >>>How about pygame? and some examples? >>>any ideas? >>> >>>Thanks >>> >>>Paul Z >>>_______________________________________________ >>>Tutor maillist - Tutor at python.org >>>To unsubscribe or change subscription options: >>>https://mail.python.org/mailman/listinfo/tutor >> >> I think you want this: >> https://zach.se/generate-audio-with-python/ >> https://github.com/zacharydenton/wavebender >> >> pygame will not give you want you want. >> >> blender might. I am not sure but worth googling for. >> >> Laura >> > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From martin at linux-ip.net Mon Jul 27 00:59:39 2015 From: martin at linux-ip.net (Martin A. Brown) Date: Sun, 26 Jul 2015 15:59:39 -0700 Subject: [Tutor] Socket Module In-Reply-To: <1702356350.2680742.1437862386083.JavaMail.yahoo@mail.yahoo.com> References: <1702356350.2680742.1437862386083.JavaMail.yahoo@mail.yahoo.com> Message-ID: Hello Nym, > Here is the updated code: https://bpaste.net/show/358583e1a0bd It's short. I have included inline here: import socket ListOfIPAddresses = [] with open('top500ips.csv', 'r') as f: for line in f: line = line.strip() ListOfIPAddresses.append(line) for address in ListOfIPAddresses: try: ResolvedAddresses = socket.gethostbyaddr(address) except: print('No Resolution Available') print(ResolvedAddresses) > The issue that I am running into now is that for some reason, the > script is not resolving known-public IP addresses that I am > passing through. For example, some of the IPs, that I have used > are for sites like facebook (173.252.120.6) github > (207.97.227.239), however the script is not able to resolve them. > > But its interesting that I am able to resolve them using nslookup > on windows command prompt. Which means my laptop's DNS setting is > fine. The apparent (?) DNS lookup failure ----------------------------------- At time X, you run your Python program and something (perhaps in the DNS resolution process) fails and you see "No Resolution Available", but you do not know what has failed, nor for which address lookup. At time Y, you run 'nslookup' at the shell prompt, receive an answer and conclude that your script is operating properly. While this is may appear logical, it is an incorrect conclusion. One coding error (a dangerous habit to perpetuate) -------------------------------------------------- When performing the DNS lookup, you are using something called a 'bare except'. This will catch any and all errors, even if it's something unrelated like, for example, a signal. This is a bad and dangerous habit. In general, you should catch only the exceptions that you can do something about. In addition, this will offer you more information about the problem. Here's a simple example, where I'm only changing two lines: for address in ListOfIPAddresses: try: ResolvedAddresses = socket.gethostbyaddr(address) except socket.herror as e: print("No resolution available for %s: %s" % (address, e)) This will give you a little more information about what, specifically, the failure is in your call to socket.gethostbyaddr() Comment on NXdomain responses ----------------------------- I picked at random an address that had no PTR record and tried to call socket.gethostbyaddr('8.97.227.2'). What do you think I got for my trouble? When running through the code block above, I saw the following output to my terminal: No resolution available for 8.97.227.2: [Errno 0] Resolver Error 0 (no error) In short, there is no guarantee that anybody has properly set up reverse DNS entries (DNS PTR records) for the addresses you are looking up. Although the vast majority of lookups occur successfully and smoothly, there are many things that can go wrong in the network and on an end host which can cause transient errors during DNS lookups, and it is possible that you have already encountered some of these problems (even though I would not expect to hit very many errors looking up PTR records for only 500 IPs). May I wish you good luck resolving not just your addresses, but also your problem! -Martin -- Martin A. Brown http://linux-ip.net/ From plucena24 at gmail.com Sun Jul 26 23:32:57 2015 From: plucena24 at gmail.com (Pablo Lucena) Date: Sun, 26 Jul 2015 17:32:57 -0400 Subject: [Tutor] How to generate a pure tones and random noise using Python? In-Reply-To: References: <201507252035.t6PKZOk8002265@fido.openend.se> Message-ID: > > > I just have read this webpage about Generate Audio with Python. > It seem writing by Python 2? Esp. itertools (izip, imap). > I try to change izip and imap to zip and map, but I get error: > > ?? > beatfreq, amplitude = remainder.split('/') > ?? > ValueError: need more than 1 value to unpack > > Are there some way to conver it to the code of Python 3? > > I'm too anxious to hear the sound from Python though I'm a absolute > beginner. > > Many thanks > > Paul Z > > ?If you are using Python 3, replace calls to itertools.izip and itertools.imap with simply "zip" and "map" respectively. Why not just use Pyton2 instead, as there may be other things that will break. ?beatfreq, amplitude = > ?? > remainder.split('/') ?This throwing an exception means there is an issue with the data you are trying to split, not necessarily a Py2 vs Py3 thing. What string is assigned `remainder`? -- *Pablo Lucena* From alan.gauld at btinternet.com Mon Jul 27 01:48:02 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 27 Jul 2015 00:48:02 +0100 Subject: [Tutor] How to generate a pure tones and random noise using Python? In-Reply-To: References: Message-ID: On 25/07/15 12:15, Paul Z wrote: > Are there some way to generate a fixed frequency sound in different > waves (eg. Sine Wave, Saw Wave, Triangle Wave etc.) and > different random noise. (eg. white noise & pink noise) ? While there are several options in Python (check out the Activestate recipes) the simplest way on Linux may be to create a file with the relevant signal and then play that through aplay using the subprocess module. One such module(by B Walker) that I found on Activestate is this (slightly tweaked): def sinebeep(): header=[ 82, 73, 70, 70, 100, 31, 0, 0, 87, 65, 86, 69, 102, 109, 116, 32, 16, 0, 0, 0, 1, 0, 1, 0, 64, 31, 0, 0, 64, 31, 0, 0, 1, 0, 8, 0, 100, 97, 116, 97, 64, 31, 0, 0 ] waveform=[ 79, 45, 32, 45, 79, 113, 126, 113 ] wavefile=open("beep.wav", "w+") for hdr in header: wavefile.write(chr(hdr)) for sample in range(0, 1000, 1): for wf in waveform: wavefile.write(chr(wf)) wavefile.close() if __name__ == '__main__': sinebeep() You can then play the resultant file with $ aplay beep.wav #you might need sudo Its not very clever but you could create a series of beep files each with a different frequency. PS. There is also a command line program called beep that does what you want but it didn't work for me... YMMV 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 jobrh2993 at gmail.com Mon Jul 27 03:06:51 2015 From: jobrh2993 at gmail.com (Job) Date: Sun, 26 Jul 2015 18:06:51 -0700 Subject: [Tutor] Abs Message-ID: <5277389D-F615-4AFB-877F-69122569F887@gmail.com> I want to be able to ask a user to input an integer and print out the root and power of the given integer. Why do you use abs(x) for this program? I don't understand or see the link between abs() and root and powers. This reminds me of this: By knowing that when x%2==1 x is an odd number and when x%2 ==0 x is even, I was able to create a program that asked the user to enter 10 integers and printed out the largest odd number . So If I understand how and why abs() is used to find the cube root of a perfect cube or how to use abs() to make the computer print out the root and power of a given integer I may make this program. Thank you and forgive for my fuzzy thoughts. Job From alan.gauld at btinternet.com Mon Jul 27 10:43:30 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 27 Jul 2015 09:43:30 +0100 Subject: [Tutor] Abs In-Reply-To: <5277389D-F615-4AFB-877F-69122569F887@gmail.com> References: <5277389D-F615-4AFB-877F-69122569F887@gmail.com> Message-ID: On 27/07/15 02:06, Job wrote: > I want to be able to ask a user to input an integer and print out the root and power of the given integer. > > Why do you use abs(x) for this program? You don't need to, and I'm not sure why you think you do? I assume it says something about it in your assignment description? abs() converts a number to a positive so you could use it as a guard to prevent you from taking the square root of a negative number. > So If I understand how and why abs() is used to find > the cube root of a perfect cube In Python 2 the pow() function (and ** operator) refuse to take fractional powers of negative numbers. So even though you should be able to do: >>> pow (-8,0.33333) you can't. You need to convert the number to positive which is where abs comes in. Note: In Python v3 pow() function (and the ** operator) happily take negative numbers. But it will tell you the root of a negative number as a complex number result. And abs() will convert a complex number into its magnitude so that's a second option. if using Python 3. In both cases you will need to manage the sign of the final answer yourself since the use of abs() will always convert things to a positive. 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 chris_roysmith at internode.on.net Mon Jul 27 10:24:37 2015 From: chris_roysmith at internode.on.net (Chris Roy-Smith) Date: Mon, 27 Jul 2015 18:24:37 +1000 Subject: [Tutor] Abs In-Reply-To: <5277389D-F615-4AFB-877F-69122569F887@gmail.com> References: <5277389D-F615-4AFB-877F-69122569F887@gmail.com> Message-ID: <55B5EAC5.3020808@internode.on.net> On 27/07/15 11:06, Job wrote: > I want to be able to ask a user to input an integer and print out the root and power of the given integer. > > Why do you use abs(x) for this program? > > I don't understand or see the link between abs() and root and powers. > > This reminds me of this: > By knowing that when x%2==1 x is an odd number and when x%2 ==0 x is even, I was able to create a program that asked the user to enter 10 integers and printed out the largest odd number . > > So If I understand how and why abs() is used to find the cube root of a perfect cube or how to use abs() to make the computer print out the root and power of a given integer I may make this program. > > Thank you and forgive for my fuzzy thoughts. > > Job > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > You will fine that any attempt to find the root of a negative value will result in an error. From colin.ross.dal at gmail.com Mon Jul 27 20:47:42 2015 From: colin.ross.dal at gmail.com (Colin Ross) Date: Mon, 27 Jul 2015 15:47:42 -0300 Subject: [Tutor] Shading Between Curves with Different Colour Over Specified X value Range Message-ID: *Goal:* Shade between I_2 (curve 1) and I_3 (curve 2) with following conditions: - Green for 0 < x < 4 - Red for 4 < x < 12 *Code: * *Note: Code currently only attempting to shade green for 0 < x < 4 * import numpy as np import pylab from pylab import * import matplotlib.pyplot as plt import csv # Load data from .txt file with open('current_mirror_output_swing.csv', 'rb') as f: reader = csv.reader(f) your_list = list(reader) data = np.asarray(your_list) I_ref = np.asarray(data[1:,0]) I_1 = data[1:,1] I_2 = data[1:,2] I_3 = data[1:,3] # Create an array of x values to fill b/w curves with a certain color. X1 = np.linspace(0.,4.,len(I_3)) I_ref = I_ref.astype(float)*1000. I_1 = I_1.astype(float)*1000. I_2 = I_2.astype(float)*1000. I_3 = I_3.astype(float)*1000. # Plotting commands. plot(I_ref, I_2, 'r-') plot(I_ref, I_3, 'b-') title('Current Mirror Output Swing') xlabel('$I_{ref}$ (mA)') ylabel('I (mA)') plt.fill_between(X1, I_2, I_3, color = 'g', alpha = '0.5') plt.legend(['$I_{ref}$', '$I_{out}$'], loc='upper left') plt.grid() show() *Issue: * See attached figure. Thank you. From breamoreboy at yahoo.co.uk Mon Jul 27 22:01:46 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 27 Jul 2015 21:01:46 +0100 Subject: [Tutor] Shading Between Curves with Different Colour Over Specified X value Range In-Reply-To: References: Message-ID: On 27/07/2015 19:47, Colin Ross wrote: > *Goal:* Shade between I_2 (curve 1) and I_3 (curve 2) with following > conditions: > - Green for 0 < x < 4 > - Red for 4 < x < 12 > > *Code: * > > *Note: Code currently only attempting to shade green for 0 < x < 4 * > > import numpy as np > import pylab > from pylab import * > import matplotlib.pyplot as plt > import csv > > > # Load data from .txt file > > with open('current_mirror_output_swing.csv', 'rb') as f: > reader = csv.reader(f) > your_list = list(reader) > > data = np.asarray(your_list) > > I_ref = np.asarray(data[1:,0]) > I_1 = data[1:,1] > I_2 = data[1:,2] > I_3 = data[1:,3] > > # Create an array of x values to fill b/w curves with a certain color. > > X1 = np.linspace(0.,4.,len(I_3)) > > I_ref = I_ref.astype(float)*1000. > I_1 = I_1.astype(float)*1000. > I_2 = I_2.astype(float)*1000. > I_3 = I_3.astype(float)*1000. > > > # Plotting commands. > > plot(I_ref, I_2, 'r-') > plot(I_ref, I_3, 'b-') > title('Current Mirror Output Swing') > xlabel('$I_{ref}$ (mA)') > ylabel('I (mA)') > > plt.fill_between(X1, I_2, I_3, color = 'g', alpha = '0.5') > plt.legend(['$I_{ref}$', '$I_{out}$'], loc='upper left') > plt.grid() > > show() > > *Issue: * > > See attached figure. > > Thank you. There is no attachment to see, sorry :( One thing to note about the following lines. from pylab import * import matplotlib.pyplot as plt The first was designed to make matplotlib easy to use interactively, especially in iPython, the second in a script. IIRC the former is deprecated so I suggest you stick with the latter. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From cs at zip.com.au Tue Jul 28 01:42:13 2015 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 28 Jul 2015 09:42:13 +1000 Subject: [Tutor] _ vs. _name vs. __name vs. name_ vs. __name__ usages In-Reply-To: References: Message-ID: <20150727234213.GA77287@cskk.homeip.net> On 25Jul2015 16:08, boB Stepp wrote: >After having a long discussion with my wife on her user requirements, >I am convinced that an OO approach is required. Which is just as well >as that has been one of my next areas of learning to do. I am >currently reading "Python 3 Object Oriented Programming" by Dusty >Phillips, which so far seems to be a reasonably good text. This has >led me to the subject line topics. > >From my understandings to date: Broadly everything you have said is correct. >1) A single underscore is used conventionally for a "throw-away" >variable, such as a loop index for which the index value is not >actually used in a subsequent calculation. Yes, but there are some problems. Some don't like it, and it can conflict with other uses. I use it very rarely, almost only ever in very simple list comprehensions, such as this real example: self.hashlist = [None for _ in range(maxchunks)] which primes a list of None values. Using it in a wider context like a loop has the same issues as any other "trite" variable name: that you will reuse it later and forget that it is either already in play (eg nested loops) or already initialised (eg code later break because of some presumption it is not set up). It is almost always better, if only for readability, to pick a sensible name, like "i", even if it is not used. A short name like "i" pretty much says "this is used only here, because otherwise I would have used a longer name that has more context". Of course, the flip side of that is that "_" _is_ the name that says "not used elsewhere", and thus if you use it in in an expression t should scream "mistake" at you. So this: [ (i,) for i in range(10) ] to make a list of tuples (made up example) should not be used with "_" instead, as "i" does get used. So yes, correct, but many people use it very sparingly and some avoid it altogether. >2) _name is used inside class methods to indicate that the >programmer's intention is that this name should only be accessed >internally by that particular class. Other supposedly "adult" Python >programmers will attempt to respect this original intent if they use >my code. Or by cooperating classes. But only _closely_ cooperating classes that have knowledge of your first class' internals, usually because they muck with them directly. Basicly, a _name is held to be part of the private API, and subject to change. So code which is prepared to be broken by changes might use it, such as if you have a pair of classes which both muck _directly_ with some shared data structure. Note than in pure OO one class never mucks with another class' internals; they only ever communicate via methods. >3) __name invokes Python's name mangling mechanism. The intent of >this usage is to not allow subclasses of the class containing __name >to access this name, and to add additional emphasis to future users of >my code that this name is meant to be strictly hands-off. Yeah. I have pretty much ceased using this. It causes pain and doesn't fulling solve the problems. The usual use case is subclassing: class Shape(object): def __init__(name): self.name = name self.__count = 0 def inc(self): self.__count += 1 class Cube(Shape): def __init__(name): Shape.__init__(name) self.__count = 6 def add_face(self): self.__count += 1 Internally there two classes work on "__Shape_count" and "__Cube_count", and thus avoid stepping on each others' toes, because although each wants a counter with a nice simple name, they mean very different things with it. So you Cube class has two separate counters. But the names are arguably uglier and harder to type. Also, consider this: from my_geometry_module import Shape as UrShape class Shape(UrShape): def __init__(self, name): UrShape.__init__(name) self.__count = 5 def bfmi(self): self.__count -= 1 class Pyramid(Shape): ... Here, your local "Shape" class will _also_ use "__Shape_count", and _will_ have trouble. So this system is not totally robust. While the example above is contrived, a deeper nesting of classes might easily have the same issue because the final class may not be aware that a class of the same name is already in use higher up the chain. >4) name_ is used when one is "forced" to use one of Python's reserved >words as a name. Yes, quite command. Also for predefined names as well as reserved names. My commonest use is the name "type", which is no a reserved word (i.e. special in the language grammar, like "if") but predefined as the "what is the type of this object" function. I've got a few objects floating around where a ".type" attribute is a natural name to indicate their flavour. That works just fine. But when I want to access the object, "type" is also a natural name for the parameter: def check_type(self, type): return self.type == type While that works, it means you have lost access to the predefined Python type() function inside that bit of code. Which can be bad. Even if you do not use the function, it makes your code confusing for other programmers (== you several months later) because they have an expectation of its meaning. Way of addressing this all involve just using another name. Common ways include "name_", "n" (trivial short term variable) or a misspelling. For example, saying "klass" instead of "class". >5) __name__ is meant to be used only by the creators of Python for >their special built-in methods, such as __init__, __new__, etc. Yes, but in the sense that they have predefined meanings. If you are implementing a mapping (a class that has methods and access like a dictionary), you will need to define methods using these names to implement the API. So don't make up your own! >Are my understandings above correct or flawed? Yes. >For (3), it seems to me that one would normally be able to use the >simpler _name construction from (2). What would be a best-practice >example of when name mangling *should* be used? Well, people rarely use _name as a function parameter, unless it is a private/secret parameter for special internal use. So I would write this: def check_type(self, type_): return self.type == type_ instead of using "_type". because "type_" here is a _public_ parameter for people to use. Using "_type" would mislead. >Likewise, it seems that normally (4) should never be needed, though I >have a feeling that I have seen something in tkinter recently that >suggests some exceptions, but I cannot (yet) bring it to mind. (4) is never required, but is often convenient. >And for (5), surely I should never violate this one? It seems that in >some future edition of Python they might add any particular __name__ >that I might try to use presently in their future version of Python >(however miniscule that possibility might actually be). Yes: don't make up new __name__s. But you will _implement_ some __name__s at some point. Cheers, Cameron Simpson On the one hand I knew that programs could have a compelling and deep logical beauty, on the other hand I was forced to admit that most programs are presented in a way fit for mechanical execution, but even if of any beauty at all, totally unfit for human appreciation. - Edsger W. Dijkstra From colin.ross.dal at gmail.com Tue Jul 28 02:00:16 2015 From: colin.ross.dal at gmail.com (Colin Ross) Date: Mon, 27 Jul 2015 21:00:16 -0300 Subject: [Tutor] Shading Between Curves with Different Colour Over Specified X value Range In-Reply-To: References: Message-ID: On Mon, Jul 27, 2015 at 5:01 PM, Mark Lawrence wrote: > On 27/07/2015 19:47, Colin Ross wrote: > >> *Goal:* Shade between I_2 (curve 1) and I_3 (curve 2) with following >> conditions: >> - Green for 0 < x < 4 >> - Red for 4 < x < 12 >> >> *Code: * >> >> *Note: Code currently only attempting to shade green for 0 < x < 4 * >> >> >> import numpy as np >> import pylab >> from pylab import * >> import matplotlib.pyplot as plt >> import csv >> >> >> # Load data from .txt file >> >> with open('current_mirror_output_swing.csv', 'rb') as f: >> reader = csv.reader(f) >> your_list = list(reader) >> >> data = np.asarray(your_list) >> >> I_ref = np.asarray(data[1:,0]) >> I_1 = data[1:,1] >> I_2 = data[1:,2] >> I_3 = data[1:,3] >> >> # Create an array of x values to fill b/w curves with a certain color. >> >> X1 = np.linspace(0.,4.,len(I_3)) >> >> I_ref = I_ref.astype(float)*1000. >> I_1 = I_1.astype(float)*1000. >> I_2 = I_2.astype(float)*1000. >> I_3 = I_3.astype(float)*1000. >> >> >> # Plotting commands. >> >> plot(I_ref, I_2, 'r-') >> plot(I_ref, I_3, 'b-') >> title('Current Mirror Output Swing') >> xlabel('$I_{ref}$ (mA)') >> ylabel('I (mA)') >> >> plt.fill_between(X1, I_2, I_3, color = 'g', alpha = '0.5') >> plt.legend(['$I_{ref}$', '$I_{out}$'], loc='upper left') >> plt.grid() >> >> show() >> >> *Issue: * >> >> See attached figure. >> >> Thank you. >> > > There is no attachment to see, sorry :( > My apologies. SHould be there now! > > One thing to note about the following lines. > > from pylab import * > import matplotlib.pyplot as plt > > The first was designed to make matplotlib easy to use interactively, > especially in iPython, the second in a script. IIRC the former is > deprecated so I suggest you stick with the latter. Great, thank you! > > > -- > My fellow Pythonistas, ask not what our language can do for you, ask > what you can do for our language. > > Mark Lawrence > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From nymcity at yahoo.com Tue Jul 28 03:52:34 2015 From: nymcity at yahoo.com (Nym City) Date: Tue, 28 Jul 2015 01:52:34 +0000 (UTC) Subject: [Tutor] Socket Module In-Reply-To: References: Message-ID: <2099282370.4058697.1438048354309.JavaMail.yahoo@mail.yahoo.com> Hi Martin, Thank you for taking the time to write such a detailed response. Very much appreciate it. I tried the code again with modifications that you suggested and even though none of the public addresses resolved; I did get little more details. I am still working on finding a solution for this and I will share it here when I have it. ?Thank you. On Sunday, July 26, 2015 6:59 PM, Martin A. Brown wrote: Hello Nym, > Here is the updated code: https://bpaste.net/show/358583e1a0bd It's short.? I have included inline here: ? import socket ? ListOfIPAddresses = [] ? with open('top500ips.csv', 'r') as f: ? ? ? for line in f: ? ? ? ? ? line = line.strip() ? ? ? ? ? ListOfIPAddresses.append(line) ? for address in ListOfIPAddresses: ? ? ? try: ? ? ? ? ? ResolvedAddresses = socket.gethostbyaddr(address) ? ? ? except: ? ? ? ? ? print('No Resolution Available') ? print(ResolvedAddresses) > The issue that I am running into now is that for some reason, the > script is not resolving known-public IP addresses that I am > passing through. For example, some of the IPs, that I have used > are for sites like facebook (173.252.120.6) github > (207.97.227.239), however the script is not able to resolve them. > > But its interesting that I am able to resolve them using nslookup > on windows command prompt. Which means my laptop's DNS setting is > fine. The apparent (?) DNS lookup failure ----------------------------------- At time X, you run your Python program and something (perhaps in the DNS resolution process) fails and you see "No Resolution Available", but you do not know what has failed, nor for which address lookup. At time Y, you run 'nslookup' at the shell prompt, receive an answer and conclude that your script is operating properly.? While this is may appear logical, it is an incorrect conclusion. One coding error (a dangerous habit to perpetuate) -------------------------------------------------- When performing the DNS lookup, you are using something called a 'bare except'.? This will catch any and all errors, even if it's something unrelated like, for example, a signal.? This is a bad and dangerous habit.? In general, you should catch only the exceptions that you can do something about. In addition, this will offer you more information about the problem. Here's a simple example, where I'm only changing two lines: ? for address in ListOfIPAddresses: ? ? ? try: ? ? ? ? ? ResolvedAddresses = socket.gethostbyaddr(address) ? ? ? except socket.herror as e: ? ? ? ? ? print("No resolution available for %s: %s" % (address, e)) This will give you a little more information about what, specifically, the failure is in your call to socket.gethostbyaddr() Comment on NXdomain responses ----------------------------- I picked at random an address that had no PTR record and tried to call socket.gethostbyaddr('8.97.227.2').? What do you think I got for my trouble?? When running through the code block above, I saw the following output to my terminal: ? No resolution available for 8.97.227.2: [Errno 0] Resolver Error 0 (no error) In short, there is no guarantee that anybody has properly set up reverse DNS entries (DNS PTR records) for the addresses you are looking up.? Although the vast majority of lookups occur successfully and smoothly, there are many things that can go wrong in the network and on an end host which can cause transient errors during DNS lookups, and it is possible that you have already encountered some of these problems (even though I would not expect to hit very many errors looking up PTR records for only 500 IPs). May I wish you good luck resolving not just your addresses, but also your problem! -Martin -- Martin A. Brown http://linux-ip.net/ From alan.gauld at btinternet.com Tue Jul 28 11:08:52 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 28 Jul 2015 10:08:52 +0100 Subject: [Tutor] _ vs. _name vs. __name vs. name_ vs. __name__ usages In-Reply-To: References: Message-ID: On 25/07/15 22:08, boB Stepp wrote: > 4) name_ is used when one is "forced" to use one of Python's reserved > words as a name. Various others have commented on the use cases for this. I'd just add that my solution to using a name that's already used by the language (or even already used by my project) is to attach an article prefix such as 'my': Thus instead of using 'type_' I'd use 'my_type' or 'the_type'. I also do this a lot in parameter lists of functions/methods so: def someMethod(self, aWidget, aDimension): .... which avoids the common confusion where the parameters are the same as the names of variables in the calling scope and it can get confusing as to whether references inside the method are to parameters or global names. In particular in class definitions I often use theXXX for a class attribute name, myXXX for an instance attribute name, and aXXX for the parameter of a method. This is not nearly so important in Python because you have to use self.xxx to get the attribute (and use Class.xxx for class attributes) but in other OOP languages where self is not required I find it helps a lot. I work quite hard to avoid names with dangling underscores because: 1) They look like an error - somebody forgot to type half the name! 2) Depending on font its hard to differentiate single and double underscores. 3) They make reading the code aloud (eg over the phone during a late night call-out!) much more difficult. -- 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 Tue Jul 28 11:13:49 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 28 Jul 2015 10:13:49 +0100 Subject: [Tutor] Shading Between Curves with Different Colour Over Specified X value Range In-Reply-To: References: Message-ID: On 28/07/15 01:00, Colin Ross wrote: >>> *Issue: * >>> >>> See attached figure. >>> >>> Thank you. >>> >> >> There is no attachment to see, sorry :( >> > > My apologies. SHould be there now! The problem is a lot of mailing list servers (and even some corporate mail servers) strip off attachments. In this case I still can't see one so something between you and me is stripping it. It may even be that some people will see it and others don't. For that reason it's usually better if you have access to some web space to post the image there and provide a URL. Or at least provide a textual description for those who can't see the image. 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 colin.ross.dal at gmail.com Tue Jul 28 13:18:36 2015 From: colin.ross.dal at gmail.com (Colin Ross) Date: Tue, 28 Jul 2015 08:18:36 -0300 Subject: [Tutor] Shading Between Curves with Different Colour Over Specified X value Range In-Reply-To: References: Message-ID: On Tue, Jul 28, 2015 at 6:13 AM, Alan Gauld wrote: > On 28/07/15 01:00, Colin Ross wrote: > > *Issue: * >>>> >>>> See attached figure. >>>> >>>> Thank you. >>>> >>>> >>> There is no attachment to see, sorry :( >>> >>> >> My apologies. SHould be there now! >> > > The problem is a lot of mailing list servers (and even some > corporate mail servers) strip off attachments. In this case > I still can't see one so something between you and me is > stripping it. It may even be that some people will see it > and others don't. > > For that reason it's usually better if you have access to > some web space to post the image there and provide a URL. > Or at least provide a textual description for those who > can't see the image. > > 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 > > > It appears to be taking the entire area between the two curves and compressing it between the specified x values. When I specify the entire x range, the shaded area fits between the two curves perfectly. However, when I specify a smaller range of x values, the shape of the area between the two curves appears to be elongated vertically as though it is trying to squeeze the total area into the smaller x range (i.e. it does not follow the upper and lower limits anymore). I hope this makes sense, I will try and post a URL... > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From skarthickbabu at gmail.com Tue Jul 28 11:19:44 2015 From: skarthickbabu at gmail.com (s karthick babu) Date: Tue, 28 Jul 2015 11:19:44 +0200 Subject: [Tutor] script to lookup and extract the values next to it Message-ID: Dear All, I am new to python programming. is there a place where people exchange the python scripts? I would like to extract the values present next to certain text, Can any one help me ? Thanks in advance, Kadi From stephanie.quiles001 at albright.edu Tue Jul 28 03:30:20 2015 From: stephanie.quiles001 at albright.edu (Quiles, Stephanie) Date: Tue, 28 Jul 2015 01:30:20 +0000 Subject: [Tutor] the big o Message-ID: <57584E42-A044-4244-9ACB-2A04C6712F29@albright.edu> Hello, I am trying to figure this out but i do not understand any of it. the question asks give the big-o performance of the following code fragment: for i in range(n): for j in range(n): k = 2 + 2 i am not sure how i am supposed to figure this out. i have been reading the book, looking at blog posts and watching online tutorials and i still cannot grasp the big-o. please keep in mind that i have never taken a calc course and that i am a complete novice to programming, even more so to python. Any help would be greatly appreciated. I am pretty much on my own with these since my fellow students are unwilling to help me with anything since they are far more advanced than i am. Thank you, Stephanie From alan.gauld at btinternet.com Tue Jul 28 14:50:41 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 28 Jul 2015 13:50:41 +0100 Subject: [Tutor] script to lookup and extract the values next to it In-Reply-To: References: Message-ID: On 28/07/15 10:19, s karthick babu wrote: > Dear All, > > I am new to python programming. Hi, welcome. > is there a place where people exchange the python scripts? Not that I know of, although I suppose the Activestate catalog of "recipes" is sort of like that. > I would like to extract the values present next to certain text, Can any > one help me ? Probably but we need a lot more detail. Please repost giving us: 1) The Python version you use 2) The OS you use 3) Any previous programming experience in other languages 4) What the input data looks like - what do you mean by value "next to" certain text? - What kind of text? What kind of value? - Is the data in a standard format such as CSV or XML or HTML? Armed with that information we can start to make suggestions. 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 Tue Jul 28 15:04:03 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 28 Jul 2015 14:04:03 +0100 Subject: [Tutor] the big o In-Reply-To: <57584E42-A044-4244-9ACB-2A04C6712F29@albright.edu> References: <57584E42-A044-4244-9ACB-2A04C6712F29@albright.edu> Message-ID: On 28/07/15 02:30, Quiles, Stephanie wrote: > question asks give the big-o performance of the following code fragment: > > for i in range(n): > for j in range(n): > k = 2 + 2 > > > ...i still cannot grasp the big-o. The big O is a way of describing the relative performance of an algorithm. For example if you have a random list of N values and want to find a particular one you need to search the list looking at each item until you find the one you want. That could mean doing anything from 1-N lookups. But on average it would be N/2 (Do you understand how I got that value?) So the big-O performance of our search is O(N/2) The result will, on average take N/2 operations. Now if the list were sorted into ascending order we can speed that up significantly by using an algorithm called a binary-chop. That means we start by looking at the middle item and seeing if it is bigger or smaller than our search item. If its smaller we can limit the search to the lower half of the list. We now apply the binary chop again and once more limit the list to half of the new list. Eventually we get a list of one item which contains our item(assuming it existed of course!) It turns out the number of chops we need is log2(N) (ie for 16 items we need at most 4 chops to find the item) So the algorithm is O(log2(N)) which is faster than O(N/2) Does that make sense so far? Looking at your example you have a variable 'n' that controls how many operations you will perform. Can you see how many times the addition (k = 2+2) will be performed? is it n? is it 2*n is it n*n? something else? The answer is your big-O value. 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 oscar.j.benjamin at gmail.com Tue Jul 28 16:03:48 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 28 Jul 2015 14:03:48 +0000 Subject: [Tutor] Shading Between Curves with Different Colour Over Specified X value Range In-Reply-To: References: Message-ID: On Mon, 27 Jul 2015 at 20:53 Colin Ross wrote: > *Goal:* Shade between I_2 (curve 1) and I_3 (curve 2) with following > conditions: > - Green for 0 < x < 4 > - Red for 4 < x < 12 > > *Code: * > > *Note: Code currently only attempting to shade green for 0 < x < 4 * > > import numpy as np > import pylab > from pylab import * > import matplotlib.pyplot as plt > import csv > > > # Load data from .txt file > > with open('current_mirror_output_swing.csv', 'rb') as f: > reader = csv.reader(f) > your_list = list(reader) > > data = np.asarray(your_list) > > I_ref = np.asarray(data[1:,0]) > I_1 = data[1:,1] > I_2 = data[1:,2] > I_3 = data[1:,3] > > # Create an array of x values to fill b/w curves with a certain color. > > Here you specify the X values for the fill shape as going from 0 to 4: > X1 = np.linspace(0.,4.,len(I_3)) > > I_ref = I_ref.astype(float)*1000. > I_1 = I_1.astype(float)*1000. > I_2 = I_2.astype(float)*1000. > I_3 = I_3.astype(float)*1000. > > > # Plotting commands. > > Here you specify the X values for the line plots as being whatever I_ref is (I don't know what it is since I can't see your csv file): > plot(I_ref, I_2, 'r-') > plot(I_ref, I_3, 'b-') > title('Current Mirror Output Swing') > xlabel('$I_{ref}$ (mA)') > ylabel('I (mA)') > > Try changing this line: > plt.fill_between(X1, I_2, I_3, color = 'g', alpha = '0.5') > to: plt.fill_between(I_ref, I_2, I_3, color = 'g', alpha = '0.5') and then the filled area should have the same X range as the lines. -- Oscar From joseph.lee22590 at gmail.com Tue Jul 28 16:07:29 2015 From: joseph.lee22590 at gmail.com (Joseph Lee) Date: Tue, 28 Jul 2015 07:07:29 -0700 Subject: [Tutor] the big o In-Reply-To: <57584E42-A044-4244-9ACB-2A04C6712F29@albright.edu> References: <57584E42-A044-4244-9ACB-2A04C6712F29@albright.edu> Message-ID: <009001d0c93e$be0b47b0$3a21d710$@gmail.com> Hi Stephanie, I'm wondering which courses you were introduced to Python and which book you are using. I do understand how it might be difficult to understand this concept, especially for someone who is a complete novice to algorithm analysis where Big O shows up. I'll answer you inline. -----Original Message----- From: Tutor [mailto:tutor-bounces+joseph.lee22590=gmail.com at python.org] On Behalf Of Quiles, Stephanie Sent: Monday, July 27, 2015 6:30 PM To: python tutor Subject: [Tutor] the big o > Hello, > I am trying to figure this out but i do not understand any of it. the question asks give the big-o performance of the following code fragment: > for i in range(n): > for j in range(n): > k = 2 + 2 > I am not sure how i am supposed to figure this out. i have been reading the book, looking at blog posts and watching online tutorials and i still cannot grasp the > big-o. please keep in mind that i have never taken a calc course and that i am a complete novice to programming, even more so to python. Any help would be greatly appreciated. I am pretty much on my own with these since my fellow students are unwilling to help me with anything since they are far more advanced than i am. > Thank you, > Stephanie JL: Okay, let's start from the very beginning. First, before talking about what Big O means, it is important to go over some basics: Big O comes from algorithm analysis, a branch of computer science dealing with coming up with solutions to problems and analyzing them. In our context, an algorithm is a list of precise steps to solve a problem. For example, using Alan's searching example, it could be paraphrased as follows: Suppose I have a list of items and I want to search for a specific item. How would I do this? If your friend was asked to do this, what would he or she do? Obviously, your friend will search for items one at a time until what you are looking for is found. You or your friend could say: First, have a list of items. Then examine one item at a time until what I want is found. This is a description of an algorithm: precise steps to be followed. The above paragraph describes searching algorithms - given a list of items, a computer (or a machine whether it's physical or virtual) will go through the list and compare each item to the target it is looking for. There are more elegant ways of doing it that mimics how humans perform specific searches, including the one that Alan described (called logarithmic search, commonly introduced as binary search in earlier courses). Once you have an algorithm, it is time to think about how to implement, or write it in Python. This is where you need to become skilled at translating paper instructions to something that Python can understand. In case of Alan's linear search example, one way to put it is: For item in items: if item == target: position = items.index(item) return position Essentially, this code fragment says to perform a linear search on a list of items. As part of learning about Big O and algorithms, it is important to practice how to translate between English and Python: translate a description of an algorithm in English to Python by implementing it, and understand what the code fragment does. Now the topic at hand: For decades, computer scientists and software developers were asking themselves, "how can we make our programs run faster and use fewer resources?" This is where Big O comes into play: Many computer science textbooks define Big O as worst--case running time of algorithms. That is, Big O describes how an algorithm will behave when it encounters input that'll cause it to run slowest. In a nutshell, an algorithm (or a program) will take in a set of values (called input), do something with it (searching, sorting, send and receive data between computers, etc.) and return one or more results (output). Many people would want to assume that algorithms will work on typical data only. In reality, algorithms can encounter input that it may have hard time to process. For example, if a program is asked to sort a list, it will expect to get a list of items that can be sorted using fewest resources and can be done quickly. There is one kind of input that this program will have hard time with (I'll let you figure out what this input might be; may I ask that we let Stephanie figure this out? That way she can understand how algorithm design works). Now you know what the worst possible input to an algorithm is like, you are ready to tackle the actual definition of Big O. Simply put, Big O is running time of an algorithm given a worst-case input. This is way different from what the book asks: the question and the accompanying code fragment simply asks for running time, not exactly Big O, hence the reason I asked which course you are taking and book that's used (Big O is reserved for computer science courses dealing with algorithms). Based on our discussion so far, there must be one more thing involved when talking about Big O: input data, and I'm leaning towards shaking my head at this point, seeing that the question could confuse novices (it is way too early to be introduced to Big O unless if you are taking a course in algorithms). In case of the code fragment above, as I said in a previous message, the easiest way to calculate Big O is to look at what the inner loop does. Once you understand that, look at the outer loop. I think the easiest way to look at this is through a real-life example: suppose you have a bunch of boxes full of books, and you want to find a specific book. You would open each box and look through various books, looking for the book you need. If you didn't find the book you are looking for, you'd move onto the next box, right? That's exactly what's going on here: * The inner loop: you look for books in one box. * Outer loop: you repeat this search for all boxes. And the result you get is the running time of this algorithm. It turns out there are more elegant ways to do this given the right input, and this algorithm or a variation is invoked in many real-life situations such as searching for a character in strings, searching for files in folders, sorting and so on (hint: the running time of this algorithm is not logarithmic; there are logarithmic (chopping) algorithms that has polynomial Big O value, with a well-known example being quicksort (you tell the algorithm to define a pivot that'll be used to align items for ease of sorting) which has Big O or worst-case running time of N squared). Hope this helps. Good luck in your courses. Cheers, Joseph _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From ltc.hotspot at gmail.com Tue Jul 28 19:00:37 2015 From: ltc.hotspot at gmail.com (ltc.hotspot at gmail.com) Date: Tue, 28 Jul 2015 17:00:37 +0000 Subject: [Tutor] =?utf-8?q?Help_Command_Question?= Message-ID: <55b7b57f.ea7c420a.7e143.16dc@mx.google.com> Hi Everyone, I'm trying to print a command of list options by using the help command in the iPython interpreter. Read captured copy of the printout as follows: 'Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v. 1500 64 bit (AMD64)] Type "copyright", "credits" or "license" for more information. IPython 3.2.0 -- An enhanced Interactive Python. Anaconda is brought to you by Continuum Analytics. Please check out: http://continuum.io/thanks and https://anaconda.org In [1]: help list File "", line 1 help list ^ SyntaxError: invalid syntax.' Question: What is the correct help command? Regards, Hal Sent from Surface From martin at linux-ip.net Tue Jul 28 19:44:20 2015 From: martin at linux-ip.net (Martin A. Brown) Date: Tue, 28 Jul 2015 10:44:20 -0700 Subject: [Tutor] Help Command Question In-Reply-To: <55b7b57f.ea7c420a.7e143.16dc@mx.google.com> References: <55b7b57f.ea7c420a.7e143.16dc@mx.google.com> Message-ID: Hi there, > In [1]: help list > File "", line 1 > help list > ^ > SyntaxError: invalid syntax.' > > > Question: What is the correct help command? Try: help(list) Snipped from my ipython session: In [1]: help(list) Good luck, -Martin -- Martin A. Brown http://linux-ip.net/ From breamoreboy at yahoo.co.uk Tue Jul 28 19:59:50 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 28 Jul 2015 18:59:50 +0100 Subject: [Tutor] Help Command Question In-Reply-To: <55b7b57f.ea7c420a.7e143.16dc@mx.google.com> References: <55b7b57f.ea7c420a.7e143.16dc@mx.google.com> Message-ID: On 28/07/2015 18:00, ltc.hotspot at gmail.com wrote: > > Hi Everyone, > > I'm trying to print a command of list options by using the help command in the iPython interpreter. Read captured copy of the printout as follows: > > 'Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v. > 1500 64 bit (AMD64)] > Type "copyright", "credits" or "license" for more information. > > IPython 3.2.0 -- An enhanced Interactive Python. > Anaconda is brought to you by Continuum Analytics. > Please check out: http://continuum.io/thanks and https://anaconda.org > > In [1]: help list > File "", line 1 > help list > ^ > SyntaxError: invalid syntax.' > > > Question: What is the correct help command? > Standard iPython says this. Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] Type "copyright", "credits" or "license" for more information. IPython 3.2.0 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: list? Docstring: list() -> new empty list list(iterable) -> new list initialized from iterable's items Type: type In [2]: list?? Docstring: list() -> new empty list list(iterable) -> new list initialized from iterable's items Type: type In [3]: help(list) Help on class list in module builtins: class list(object) | list() -> new empty list | list(iterable) -> new list initialized from iterable's items | | Methods defined here: | | __add__(self, value, /) | Return self+value. ... Got it? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From mikablom at yahoo.com Tue Jul 28 21:35:14 2015 From: mikablom at yahoo.com (mikablom) Date: Tue, 28 Jul 2015 21:35:14 +0200 Subject: [Tutor] mailing list Message-ID: I forgot how to stop getting mails from you all. please, will someone tell me how. thank you very much. Von Samsung-Tablet gesendet From danny.yoo at gmail.com Tue Jul 28 22:10:22 2015 From: danny.yoo at gmail.com (Danny Yoo) Date: Tue, 28 Jul 2015 13:10:22 -0700 Subject: [Tutor] mailing list In-Reply-To: References: Message-ID: On Jul 28, 2015 12:38 PM, "mikablom via Tutor" wrote: > > > > I forgot how to stop getting mails from you all. please, will someone tell me how. thank you very much. Visit https://mail.python.org/mailman/listinfo/tutor; you should be able to unsubscribe from there. From stephanie.quiles001 at albright.edu Tue Jul 28 21:08:32 2015 From: stephanie.quiles001 at albright.edu (Quiles, Stephanie) Date: Tue, 28 Jul 2015 19:08:32 +0000 Subject: [Tutor] the big o In-Reply-To: <009001d0c93e$be0b47b0$3a21d710$@gmail.com> References: <57584E42-A044-4244-9ACB-2A04C6712F29@albright.edu>, <009001d0c93e$be0b47b0$3a21d710$@gmail.com> Message-ID: Hi Jason, I took Intro to Programming at Albright College and we used Starting Out with Python 3rd ed. Right now I am taking Data Structure and Analysis and we are using This book : http://interactivepython.org/runestone/static/pythonds/BasicDS/InfixPrefixandPostfixExpressions.html Thanks Alan for your explanation. The problem with me getting these terms and things is in part due to the accelerated rate in which my classes run. I cant possible master anything in a 6-8 week course, especially when I have zero programming background and about 2/3 of the class does... So I'll fake it till I make it. Thanks a lot. I think I have a very basic understanding of Big O right now. But if someone could please make it a little easier to figure out postfix and infix? My homework assignment asks me to convert from infix to postfix. I got a very very basic understanding of this but when the problems get a little more involved I get totally lost. Here is an example A+B/C*D-E+F I thought it was something like ABC/+ cd*ef+-?? That's probably wrong but I am trying Stephanie Quiles Sent from my iPhone > On Jul 28, 2015, at 10:07 AM, Joseph Lee wrote: > > Hi Stephanie, > I'm wondering which courses you were introduced to Python and which book you > are using. I do understand how it might be difficult to understand this > concept, especially for someone who is a complete novice to algorithm > analysis where Big O shows up. > I'll answer you inline. > > -----Original Message----- > From: Tutor [mailto:tutor-bounces+joseph.lee22590=gmail.com at python.org] On > Behalf Of Quiles, Stephanie > Sent: Monday, July 27, 2015 6:30 PM > To: python tutor > Subject: [Tutor] the big o > >> Hello, > >> I am trying to figure this out but i do not understand any of it. the > question asks give the big-o performance of the following code fragment: > >> for i in range(n): >> for j in range(n): >> k = 2 + 2 > >> I am not sure how i am supposed to figure this out. i have been reading > the book, looking at blog posts and watching online tutorials and i still > cannot grasp the > big-o. please keep in mind that i have never taken a calc > course and that i am a complete novice to programming, even more so to > python. Any help would be greatly appreciated. I am pretty much on my own > with these since my fellow students are unwilling to help me with anything > since they are far more advanced than i am. > >> Thank you, > >> Stephanie > > JL: Okay, let's start from the very beginning. First, before talking about > what Big O means, it is important to go over some basics: > > Big O comes from algorithm analysis, a branch of computer science dealing > with coming up with solutions to problems and analyzing them. In our > context, an algorithm is a list of precise steps to solve a problem. For > example, using Alan's searching example, it could be paraphrased as follows: > > Suppose I have a list of items and I want to search for a specific item. How > would I do this? > > If your friend was asked to do this, what would he or she do? Obviously, > your friend will search for items one at a time until what you are looking > for is found. You or your friend could say: > > First, have a list of items. Then examine one item at a time until what I > want is found. > > This is a description of an algorithm: precise steps to be followed. The > above paragraph describes searching algorithms - given a list of items, a > computer (or a machine whether it's physical or virtual) will go through the > list and compare each item to the target it is looking for. There are more > elegant ways of doing it that mimics how humans perform specific searches, > including the one that Alan described (called logarithmic search, commonly > introduced as binary search in earlier courses). > > Once you have an algorithm, it is time to think about how to implement, or > write it in Python. This is where you need to become skilled at translating > paper instructions to something that Python can understand. In case of > Alan's linear search example, one way to put it is: > > For item in items: > if item == target: > position = items.index(item) > return position > > Essentially, this code fragment says to perform a linear search on a list of > items. As part of learning about Big O and algorithms, it is important to > practice how to translate between English and Python: translate a > description of an algorithm in English to Python by implementing it, and > understand what the code fragment does. > > Now the topic at hand: For decades, computer scientists and software > developers were asking themselves, "how can we make our programs run faster > and use fewer resources?" This is where Big O comes into play: Many computer > science textbooks define Big O as worst--case running time of algorithms. > That is, Big O describes how an algorithm will behave when it encounters > input that'll cause it to run slowest. In a nutshell, an algorithm (or a > program) will take in a set of values (called input), do something with it > (searching, sorting, send and receive data between computers, etc.) and > return one or more results (output). > > Many people would want to assume that algorithms will work on typical data > only. In reality, algorithms can encounter input that it may have hard time > to process. For example, if a program is asked to sort a list, it will > expect to get a list of items that can be sorted using fewest resources and > can be done quickly. There is one kind of input that this program will have > hard time with (I'll let you figure out what this input might be; may I ask > that we let Stephanie figure this out? That way she can understand how > algorithm design works). > > Now you know what the worst possible input to an algorithm is like, you are > ready to tackle the actual definition of Big O. Simply put, Big O is running > time of an algorithm given a worst-case input. This is way different from > what the book asks: the question and the accompanying code fragment simply > asks for running time, not exactly Big O, hence the reason I asked which > course you are taking and book that's used (Big O is reserved for computer > science courses dealing with algorithms). Based on our discussion so far, > there must be one more thing involved when talking about Big O: input data, > and I'm leaning towards shaking my head at this point, seeing that the > question could confuse novices (it is way too early to be introduced to Big > O unless if you are taking a course in algorithms). > > In case of the code fragment above, as I said in a previous message, the > easiest way to calculate Big O is to look at what the inner loop does. Once > you understand that, look at the outer loop. I think the easiest way to look > at this is through a real-life example: suppose you have a bunch of boxes > full of books, and you want to find a specific book. You would open each box > and look through various books, looking for the book you need. If you didn't > find the book you are looking for, you'd move onto the next box, right? > That's exactly what's going on here: > > * The inner loop: you look for books in one box. > * Outer loop: you repeat this search for all boxes. > > And the result you get is the running time of this algorithm. It turns out > there are more elegant ways to do this given the right input, and this > algorithm or a variation is invoked in many real-life situations such as > searching for a character in strings, searching for files in folders, > sorting and so on (hint: the running time of this algorithm is not > logarithmic; there are logarithmic (chopping) algorithms that has polynomial > Big O value, with a well-known example being quicksort (you tell the > algorithm to define a pivot that'll be used to align items for ease of > sorting) which has Big O or worst-case running time of N squared). > > Hope this helps. Good luck in your courses. > Cheers, > Joseph > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From HMcDonald at defenders.org Tue Jul 28 20:52:59 2015 From: HMcDonald at defenders.org (Hannah G. McDonald) Date: Tue, 28 Jul 2015 18:52:59 +0000 Subject: [Tutor] Concatenating columns via python Message-ID: I extracted a table from a PDF so the data is quite messy and the data that should be in 1 row is in 3 colums, like so: year color location 1 1997 blue, MD 2 green, 3 and yellow SO far my code is below, but I know I am missing data I am just not sure what to put in it: # Simply read and split an example Table 4 import sys # Assigning count number and getting rid of right space def main(): count = 0 pieces = [] for line in open(infile, 'U'): if count < 130: data = line.replace('"', '').rstrip().split("\t") data = clean_data(data) if data[1] == "year" and data[1] != "": write_pieces(pieces) pieces = data str.join(pieces) else: for i in range(len(data)): pieces[i] = pieces[i] + data[i] str.join(pieces) # Executing command to remove right space def clean_data(s): return [x.rstrip() for x in s] def write_pieces(pieces): print if __name__ == '__main__': infile = "file.txt" main() From ltc.hotspot at gmail.com Tue Jul 28 22:21:19 2015 From: ltc.hotspot at gmail.com (ltc.hotspot at gmail.com) Date: Tue, 28 Jul 2015 20:21:19 +0000 Subject: [Tutor] =?utf-8?q?line_error_on_no=2E_7?= Message-ID: <55b7e48e.e226460a.3f7e6.3db5@mx.google.com> Hi Everyone, I'm writing python code to read a data text file, split the file into a list of words using the split(function) and to print the results in alphabetical order. The raw python code is located at http://tinyurl.com/oua9uqx The sample data is located at http://tinyurl.com/odt9nhe Desired Output: ['Arise', 'But', 'It', 'Juliet', 'Who', 'already', 'and', 'breaks', 'east', 'envious', 'fair', 'grief', 'is', 'kill', 'light', 'moon', 'pale', 'sick', 'soft', 'sun', 'the', 'through', 'what', 'window', 'with', 'yonder'] There is a line error on no. 7 What is the cause of this error? Regards, Hal Sent from Surface From martin at linux-ip.net Wed Jul 29 01:41:20 2015 From: martin at linux-ip.net (Martin A. Brown) Date: Tue, 28 Jul 2015 16:41:20 -0700 Subject: [Tutor] line error on no. 7 In-Reply-To: <55b7e48e.e226460a.3f7e6.3db5@mx.google.com> References: <55b7e48e.e226460a.3f7e6.3db5@mx.google.com> Message-ID: Hello again, > The raw python code is located at http://tinyurl.com/oua9uqx It is not very long, so you can post inline (I pasted it below). If you have a longer piece of code, then, yes, a pastebin is a good choice. (Also, if in the future, you have a more complex piece of code, try to simplify your posting to just the part that is giving you trouble.) But, your question is clear here. fname = raw_input("Enter file name: ") fh = open(fname) lst = list() for line in fh: if fh == list: continue list.split() list.append sorted("fh") print line.rstrip() You have confused your names in line 7. list.split() There are a few lessons to learn here. * Are you operating on the variable you intend to be operating on? No, in this case. You wanted to do something like 'lst.split()' * Did you want to throw away the result of the call to .split()? I'm guessing the answer is 'No.' So, you'll need another variable to hold the value from lst.split(). Line 7 should become: lst.extend(line.split()) Additional comments: * Your line 6 performs a test to see if fh (file object referring to the stanza) equals the builtin called 'list'. That doesn't make sense. Try typing list and list() at an interactive prompt, and you may see that it doesn't make sense to compare those things. >>> list >>> list() [] The first tells you what 'list' is. The second calls 'list()', which returns you, well... a new Python list object. * Your line 8 is a NOOP (no-operation). In that line, you are simply referring to a method on the builtin list type. Use the interactive interpreter to see what I mean: >>> * Your line 9 doesn't make much sense to me. It may no longer be a syntax error, but it isn't doing what you think it's doing. Try it out in the interactive interpreter to see what it's doing: >>> sorted("fh") ['f', 'h'] OK, so now, let me make a few suggestions. Others may have additional comments, but I'd be interested in seeing how you adjust your program after working through the above. Thus, I suggest removing most of the loop internals and rewriting. Everything else is a good start. I'll suggest a possible ending, too. fname = raw_input("Enter file name: ") fh = open(fname) lst = list() for line in fh: # Code is good until this point. # Here, you want to find the words and add them to the list # called 'lst'. # -- now, outside the loop, I would suggest printing the list print lst Once you can run your program and get something other than an empty list, you know you have made progress. Good luck with your iambic pentameter, -Martin > The sample data is located at > http://tinyurl.com/odt9nhe Also, I'm including your short data sample: But soft what light through yonder window breaks It is the east and Juliet is the sun Arise fair sun and kill the envious moon Who is already sick and pale with grief -- Martin A. Brown http://linux-ip.net/ From alan.gauld at btinternet.com Wed Jul 29 01:43:07 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 29 Jul 2015 00:43:07 +0100 Subject: [Tutor] Postfix notation [was: Re: the big o] In-Reply-To: References: <57584E42-A044-4244-9ACB-2A04C6712F29@albright.edu>, <009001d0c93e$be0b47b0$3a21d710$@gmail.com> Message-ID: On 28/07/15 20:08, Quiles, Stephanie wrote: Hoi Stephanie, Please start a new topic on a new thread/subject. It makes funding stuff in the archives much easier. > ...if someone could please make it a little easier to figure out postfix and infix? > My homework assignment asks me to convert from infix to postfix. ... > Here is an example A+B/C*D-E+F > > I thought it was something like ABC/+ cd*ef+-?? > If I work this back correctly it translates as: (A+B/C) (C*D)-(E+F) which seems to miss an operation? It also uses C twice. The first thing you need to do it work out how to put parens on the original to express the precedence precisely. Then the postfix format should pretty much drop out. Do you have access to an RPN calculator(emulator)? That will help you test it. If on *nix try xcalc -rpn 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 Jul 29 02:01:07 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 29 Jul 2015 01:01:07 +0100 Subject: [Tutor] Concatenating columns via python In-Reply-To: References: Message-ID: On 28/07/15 19:52, Hannah G. McDonald wrote: > I extracted a table from a PDF so the data is quite messy > and the data that should be in 1 row is in 3 colums, like so: > year color location > 1 1997 blue, MD > 2 green, > 3 and yellow > > SO far my code is below, but I know I am missing data I am just not sure what to put in it: > Please post in plain text. Your code has got mangled and lost the indentation soi I'll need to guess... Also tell us the Python(and OS) version, it all helps. So far as the sample data you provided it doesn't seem to bear much relation to the code below apoart from (maybe) the hreader line. DFor example what are you planning on doing with the 'and' in the 3rd line? There seems to be no attempt to process that? And how can you add the strings meaningfully? In other words can you show both the input *and the output* you are aiming for? > # Simply read and split an example Table 4 > import sys > > # Assigning count number and getting rid of right space > def main(): > count = 0 > pieces = [] > for line in open(infile, 'U'): > if count < 130: > data = line.replace('"', '').rstrip().split("\t") > data = clean_data(data) For which I guess: def main(): count = 0 pieces = [] for line in open(infile, 'U'): if count < 130: data = line.replace('"', '').rstrip().split("\t") data = clean_data(data) > if data[1] == "year" and data[1] != "": This doesn't make sense since if data[1] is 'year' it can never be "" so the second test is redundant. And it should only ever be true on the header line. > write_pieces(pieces) > pieces = data > str.join(pieces) When you do the write_pieces() call pieces is an empty list? Then you try to join it using str.join but that is the class method so expects a string instance as its first argument. I suspect you should have used: " ".join(pieces) or "\t".join(pieces) But I'm not certain what you plan on doing here. Especially since you don;t assign the result to any variable so it gets deleted. > else: > for i in range(len(data)): > pieces[i] = pieces[i] + data[i] > str.join(pieces) Since pieces is potentially the empty list here you cannot safely assign anything to pieces[i]. And again I don;t know what the last line is supposed to be doing. > > # Executing command to remove right space > def clean_data(s): > return [x.rstrip() for x in s] > > def write_pieces(pieces): > print This makes no sense since it only prints a blank 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 dyoo at hashcollision.org Wed Jul 29 03:52:47 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 28 Jul 2015 18:52:47 -0700 Subject: [Tutor] the big o In-Reply-To: References: <57584E42-A044-4244-9ACB-2A04C6712F29@albright.edu> <009001d0c93e$be0b47b0$3a21d710$@gmail.com> Message-ID: > I took Intro to Programming at Albright College and we used Starting Out with Python 3rd ed. Right now I am taking Data Structure and Analysis and we are using This book : http://interactivepython.org/runestone/static/pythonds/BasicDS/InfixPrefixandPostfixExpressions.html > > Thanks Alan for your explanation. The problem with me getting these terms and things is in part due to the accelerated rate in which my classes run. I cant possible master anything in a 6-8 week course, especially when I have zero programming background and about 2/3 of the class does... So I'll fake it till I make it. This makes sense. If you can, it might help to look for personal support at your college too. Of course, we'll be happy to help on this mailing list! But face-to-face support can also be very helpful, because email can be parsimonious. Some colleges have organizations and societies that offer help with classwork. If such "peer tutoring" services are available on your campus, I'd strongly recommend connecting with them. For example, when I went to Berkeley, I used the services of Eta Kappa Nu. (https://hkn.eecs.berkeley.edu/tutor/) Good folks. And I would assume that there are similar groups at Albright College. From steve at pearwood.info Wed Jul 29 04:42:16 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 29 Jul 2015 12:42:16 +1000 Subject: [Tutor] mailing list In-Reply-To: References: Message-ID: <20150729024216.GK25179@ando.pearwood.info> On Tue, Jul 28, 2015 at 09:35:14PM +0200, mikablom via Tutor wrote: > > > I forgot how to stop getting mails from you all. please, will someone tell me how. thank you very much. At the bottom of every email on the mailing list, it says: > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Steve From jobrh2993 at gmail.com Wed Jul 29 05:29:00 2015 From: jobrh2993 at gmail.com (Job Hernandez) Date: Tue, 28 Jul 2015 20:29:00 -0700 Subject: [Tutor] Root and power Message-ID: How is it going tutors? The following problem seems impossible to me: "*Write a program that asks the user to enter an integer and prints two integers, root and pwr, such that 0 < pwr < 6 and root^pwr (root**pwr) is equal to the integer entered by the user. If no such pair of integers exists, it should print a message to that effect*." I would like to solve this problem myself so please don't give me the solution. I need to learn how in the world do find the root and power of an integer that x user entered? I haven been looking on the python website for an appropriate function but I have not. If you have the time can you please tell me about the functions and other facts I need to know in order to solve this problem? Is there a book you guys recommend for total beginners who have no ideal of what computer science and programming is? Thank you, Job From ltc.hotspot at gmail.com Wed Jul 29 01:33:53 2015 From: ltc.hotspot at gmail.com (ltc.hotspot at gmail.com) Date: Tue, 28 Jul 2015 23:33:53 +0000 Subject: [Tutor] =?utf-8?q?String_Attribute?= Message-ID: <55b811cb.0566420a.6c354.4cc9@mx.google.com> Hi Everyone: What is the source of the syntax error to the String Attribute? Go to the following URL links and view a copy of the raw data file code and sample data: 1.) http://tinyurl.com/p2xxxhl 2.) http://tinyurl.com/nclg6pq Here is the desired output: stephen.marquard at uct.ac.za louis at media.berkeley.edu .... Hal Sent from Surface From ltc.hotspot at gmail.com Wed Jul 29 04:01:50 2015 From: ltc.hotspot at gmail.com (ltc.hotspot at gmail.com) Date: Wed, 29 Jul 2015 02:01:50 +0000 Subject: [Tutor] =?utf-8?q?line_error_on_no=2E_7?= In-Reply-To: References: <55b7e48e.e226460a.3f7e6.3db5@mx.google.com>, Message-ID: <55b8342b.092a460a.3125f.ffff8ebe@mx.google.com> Thanks, I?ll need some time to review your notes Sent from Surface From: Martin A. Brown Sent: ?Tuesday?, ?July? ?28?, ?2015 ?4?:?41? ?PM To: ltc.hotspot at gmail.com Cc: Tutor at python.org Hello again, > The raw python code is located at http://tinyurl.com/oua9uqx It is not very long, so you can post inline (I pasted it below). If you have a longer piece of code, then, yes, a pastebin is a good choice. (Also, if in the future, you have a more complex piece of code, try to simplify your posting to just the part that is giving you trouble.) But, your question is clear here. fname = raw_input("Enter file name: ") fh = open(fname) lst = list() for line in fh: if fh == list: continue list.split() list.append sorted("fh") print line.rstrip() You have confused your names in line 7. list.split() There are a few lessons to learn here. * Are you operating on the variable you intend to be operating on? No, in this case. You wanted to do something like 'lst.split()' * Did you want to throw away the result of the call to .split()? I'm guessing the answer is 'No.' So, you'll need another variable to hold the value from lst.split(). Line 7 should become: lst.extend(line.split()) Additional comments: * Your line 6 performs a test to see if fh (file object referring to the stanza) equals the builtin called 'list'. That doesn't make sense. Try typing list and list() at an interactive prompt, and you may see that it doesn't make sense to compare those things. >>> list >>> list() [] The first tells you what 'list' is. The second calls 'list()', which returns you, well... a new Python list object. * Your line 8 is a NOOP (no-operation). In that line, you are simply referring to a method on the builtin list type. Use the interactive interpreter to see what I mean: >>> * Your line 9 doesn't make much sense to me. It may no longer be a syntax error, but it isn't doing what you think it's doing. Try it out in the interactive interpreter to see what it's doing: >>> sorted("fh") ['f', 'h'] OK, so now, let me make a few suggestions. Others may have additional comments, but I'd be interested in seeing how you adjust your program after working through the above. Thus, I suggest removing most of the loop internals and rewriting. Everything else is a good start. I'll suggest a possible ending, too. fname = raw_input("Enter file name: ") fh = open(fname) lst = list() for line in fh: # Code is good until this point. # Here, you want to find the words and add them to the list # called 'lst'. # -- now, outside the loop, I would suggest printing the list print lst Once you can run your program and get something other than an empty list, you know you have made progress. Good luck with your iambic pentameter, -Martin > The sample data is located at > http://tinyurl.com/odt9nhe Also, I'm including your short data sample: But soft what light through yonder window breaks It is the east and Juliet is the sun Arise fair sun and kill the envious moon Who is already sick and pale with grief -- Martin A. Brown http://linux-ip.net/ From alan.gauld at btinternet.com Wed Jul 29 10:51:26 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 29 Jul 2015 09:51:26 +0100 Subject: [Tutor] Abs In-Reply-To: References: <5277389D-F615-4AFB-877F-69122569F887@gmail.com> Message-ID: <55B8940E.3060209@btinternet.com> On 29/07/15 04:28, Job Hernandez wrote: > How is it going tutors? You only sent it to me. Please use Reply All to include the list. > > The following problem seems impossible to me: > > "*Write a program that asks the user to enter an integer and prints > two integers, /root /and /pwr/, such that 0 < pwr < 6 and root^pwr > (root**pwr) is equal to the integer entered by the user. If no such > pair of integers exists, it should print a message to that effect*." > > I would like to solve this problem myself so please don't give me the > solution. > > I need to learn how in the world do find the root and power of an > integer that x user entered? I haven been looking on the python > website for an appropriate function but I have not. The only function you need is pow() Or you could do it without a function by using the ** operator. You want to try various integer values and see if the result is the users input. That means you need a loop. The pwr value is set between 1 and 5 in the assignment. The maximum root value will be the user's input (since X**1 = X, and that will always be a valid solution!) > Is there a book you guys recommend for total beginners who have no > idea of what computer science and programming is? Being biased, I'd recommend my web site(see below). You can get an older version in a paper book if you must, and its mostly still applicable. There is also a good option by Allen Downey which focuses on the CS side of things if that's what you want. 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 Jul 29 11:17:31 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 29 Jul 2015 10:17:31 +0100 Subject: [Tutor] String Attribute In-Reply-To: <55b811cb.0566420a.6c354.4cc9@mx.google.com> References: <55b811cb.0566420a.6c354.4cc9@mx.google.com> Message-ID: On 29/07/15 00:33, ltc.hotspot at gmail.com wrote: > Hi Everyone: > > > What is the source of the syntax error to the String Attribute? Normally I'd ask you to post the full text of any errors. They usually contain a lot of useful information. They also help us identify which syntax error you are asking about in the case where there are several! :-) But in your case it seems you are running the code in an online debugger so you may not have seen the full error text. Although, even there, it gives more information that you posted, namely: AttributeError: 'str' object has no attribute 'startwith' So it's not a syntax error but an attribute error... Your error is with the attribute startwith, which doesn't exist. To check the attributes of a string, type dir(str) at a >>> prompt. (I assume you have access to one of those somewhere?) You will see that you mis-spelled startswith. However, your code has several other problems... > Go to the following URL links and view a copy of the raw data file code and sample data: > > 1.) http://tinyurl.com/p2xxxhl > 2.) http://tinyurl.com/nclg6pq If its short (<100 lines?) just include the code in the message. Here it is: count = 0 fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" for line in fname: line = line.strip() if not line.startwith('From '): continue line = line.split() count = count + 1 print len(line) fh = open(fname) print "There were", count, "lines in the file with From as the first word" You set the filename and then iterate over the name. I suspect you intended to iterate over the file contents? To do that you need to open the file (which you do near the end!) So something like: with open(fname as in_file: for line in in_file: # do your stuff here The next problem is that the last line of the loop holds the individual elements of the split, but you throw that away when the loop goes back to the top. You need to save the result somewhere so you can process it after the loop completes. For this specific example you could just indent the count = count + 1 print len(line) lines inside the loop. But that won't be enough to get you to your final output of the email addresses. > Here is the desired output: > stephen.marquard at uct.ac.za > louis at media.berkeley.edu > .... 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 Jul 29 11:19:05 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 29 Jul 2015 10:19:05 +0100 Subject: [Tutor] Abs In-Reply-To: <55B8940E.3060209@btinternet.com> References: <5277389D-F615-4AFB-877F-69122569F887@gmail.com> <55B8940E.3060209@btinternet.com> Message-ID: On 29/07/15 09:51, Alan Gauld wrote: > On 29/07/15 04:28, Job Hernandez wrote: >> How is it going tutors? > > You only sent it to me. Please use Reply All to include the list. My mistake, you sent it to the list too. For some reason my mailer didn't show the tutor header... Its in a new thread now. -- 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 Jul 29 11:23:16 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 29 Jul 2015 10:23:16 +0100 Subject: [Tutor] Root and power In-Reply-To: References: Message-ID: On 29/07/15 04:29, Job Hernandez wrote: > How is it going tutors? > > The following problem seems impossible to me: I made a reply in the thread 'Abs' started on July 27th. Basically you can use the pow() function. The power lies between 1-5. The largest root will be the same as the user input since X**1 = X. So you always have at least 1 answer! You need to iterate up to the power of 1 solution to find if there is a smaller pair of integers. Some may have several, for example 16 yields: 16**1 4**2 2**4 Your assignment doesn't make it clear how that should be handled... -- 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 dpalao.python at gmail.com Wed Jul 29 11:27:32 2015 From: dpalao.python at gmail.com (David Palao) Date: Wed, 29 Jul 2015 11:27:32 +0200 Subject: [Tutor] Root and power In-Reply-To: References: Message-ID: 2015-07-29 5:29 GMT+02:00 Job Hernandez : > How is it going tutors? > > The following problem seems impossible to me: > > "*Write a program that asks the user to enter an integer and prints two > integers, root and pwr, such that 0 < pwr < 6 and root^pwr (root**pwr) is > equal to the integer entered by the user. If no such pair of integers > exists, it should print a message to that effect*." > > I would like to solve this problem myself so please don't give me the > solution. > > I need to learn how in the world do find the root and power of an integer > that x user entered? I haven been looking on the python website for an > appropriate function but I have not. > > If you have the time can you please tell me about the functions and other > facts I need to know in order to solve this problem? > > Is there a book you guys recommend for total beginners who have no ideal of > what computer science and programming is? > > Thank you, > > Job > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor Hello, First, you need an algorithm that solves your problem. Once you have it, you need to implement it in Python. For the algorithm. Although there are theorems and all sort of smart mathematical tricks you could use, given the conditions you have, have you considered to use a brute force approach? I mean: if all involved numbers are positive you could start testing different values for root from 0 on, and for each value test pwr from 1 to 5 until you find either a solution, something bigger than x. Once you chose the algorithm, for the actual implementation you have to say what part you are blocked at. Best. From lissa.hopson at gmail.com Wed Jul 29 11:16:58 2015 From: lissa.hopson at gmail.com (Lissa Hopson) Date: Wed, 29 Jul 2015 04:16:58 -0500 Subject: [Tutor] I need help with my homework. No, really.... Message-ID: I'm taking a beginning Python course at Austin Community College. I'm also taking two other project-based web programming courses. It's summer semester, meaning we have eight weeks instead of the usual 16 to finish all the requirements. The semester ends Friday, July 131st. Yes, I am aware that I'm a teensy bit screwed. I have to complete eight programs ("complete" meaning "functioning"). I'm having a really tough time with this one. It's matrix arithmetic using 2d arrays. If ANYONE can help me, I'd really appreciate it. Someday, maybe I can be the guy helping someone else...except I'm a girl. Whatever. I digress. I'm already planning to retake the course because I want to get more out of it- I like Python a lot, it's just really difficult to absorb it all that fast...especially since I'm used to HTML and JavaScript. Okay- so these are the directions for the program, and I'll cut and paste my program so far from the interpreter to the email. Don't want to freak anyone out with attachments. It's gonna be a long email. Given x as an array of [5,3] and y as an array of [3,7] perform the following: 1. Load array x column-wise and array y row-wise 2. Multiply x by y to compute array z 3. Compute the sum of all elements in column 2 of array x and add it to the sum of all elements in row 2 of y (the first row/column is 0, the second is 1, etc. That got me at first) 4. Compute the smallest element in row 1 of y ---using appropriate headings: 5. Print out matrices x, y, and z (display on screen, but y'all probably get that) 6. Print out sum and smallest element The data with which array x is loaded: 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1, 2, 3, 4 The data with which array y is loaded: 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1 Must use functions named as follows: LOADX, LOADY, COMPUTEZ, SMALLEST, SUMMATION, OUTDATA lab5.dat is simply a dat file with the data with which the arrays are loaded in one long line, each separated by commas. Thanks- in advance- no more comments after the program. This is what I have thus far: #Lab #5 #COSC 1336-31493 #SUM 2015 NRG #Tu/Th 1:15-4:25pm def main(): #matrix initialization x=[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]] y=[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]] z=[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]] #file declaration infile = open('lab5x.dat','r') infile = open('lab5y.dat','r') outfile = open('lab5.out', 'w') #variables sumx = 0 sumy = 0 small = 0 A = 0 B = 0 C = 0 #call functions LOADX(infile, A) LOADY(infile, B) COMPUTEZ(A, B, C) SUMMATION(A, B) SMALLEST(A) OUTDATA(file, A, B, C) #close files infile.close() infile.close() outfile.close() dummy = input('Press any key to continue.') #develop functions #load matrix x def LOADX(infile, A): #local variables n=0 k=0 s=0 templist = infile.readline().strip('\n').split(',') while (k<3): j=0 while(j<5): A[j][k] = int(templist[n]) s=s+A[j][k] j=j+1 k=k+1 n=n+1 #load matrix y def LOADY(infile, B): #local variables n=0 j=0 templist = infile.readline().strip('\n').split(',') while (j<3): k=0 while (k<7): B[j][k] = int(templist[n]) s=s+B[j][k] j=j+1 n=n+1 k=k+1 #define computation of Z matrix def COMPUTEZ (A, B, C): i=0 while (i<5): j=0 while (j<=7): k=0 while (k<=3): C[i][j]= C[i][j]+ A[i][k] * B[k][j] k=k+1 j=j+1 i=i+1 #def summation def SUMMATION(x,y): s=0 k=0 j=0 while (k<5): sumx=sumx + x[k][2] k=k+1 while (j<7): sumy=sumy + y[2][j] j=j+1 s=sumx + sumy #def smallest def SMALLEST (B): k=0 s=B[1][k] k=k+1 while (k<7): if(s> B[1][k]): s=B[1][k] k=k+1 def OUTDATA(outfile, x, y, z,SMALLEST,SUMMATION): i=0 j=0 k=0 while (k<3): print(A[k][0],A[k][1],A[k][2],A[k][3],A[k][4]) k=k+1 file.write[str(A[k][0])+str(A[k][1])+str(A[k][2])+str(A[k][3])+str(A[k][3])+str(A[k][4])] while (j<7): print(B[j][0],B[j][1],B[j][2]) j=j+1 file.write[str(B[j][0])+str(B[j][1])+str(B[j][2])] while (i<7): print(C[i][0],C[i][1],C[i][2],C[i][3],C[i][4]) file.write[str(C[i][0]+C[i][1]+C[i][2]+C[i][3]+C[i][4])] print ('Summation= ',SUMMATION) file.write('Summation= ', SUMMATION) print ('Smallest= ',SMALLEST) file.write('Smallest= ',SMALLEST) main() From steve at pearwood.info Wed Jul 29 14:54:35 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 29 Jul 2015 22:54:35 +1000 Subject: [Tutor] I need help with my homework. No, really.... In-Reply-To: References: Message-ID: <20150729125435.GL25179@ando.pearwood.info> On Wed, Jul 29, 2015 at 04:16:58AM -0500, Lissa Hopson wrote: > I'm taking a beginning Python course at Austin Community College. I'm also > taking two other project-based web programming courses. It's summer > semester, meaning we have eight weeks instead of the usual 16 to finish all > the requirements. > The semester ends Friday, July 131st. July 131st? Whew, you've got over 100 days to complete this! *wink* But seriously... more comments (hopefully useful comments this time) follow below, interleaved with your code. Grab a coffee, this may be a bit long. Oh, and I'm going to split my reply over a couple of emails. > Yes, I am aware that I'm a teensy bit screwed. > > I have to complete eight programs ("complete" meaning "functioning"). I'm > having a really tough time with this one. It's matrix arithmetic using 2d > arrays. [...] > Given x as an array of [5,3] and y as an array of [3,7] perform the > following: > > 1. Load array x column-wise and array y row-wise I'm not sure that I understand what this means. I think what they mean is that if the data looks like this: 10, 20, 30, 40, 50, 60 and x and y are both 3x2 arrays, we end up with these: # read data down the columns first x = [ [10, 40], [20, 50], [30, 60] ] # read data across the rows first y = [ [10, 20], [30, 40], [50, 60] ] > 2. Multiply x by y to compute array z > 3. Compute the sum of all elements in column 2 of array x and add it to the > sum of all elements in row 2 of y (the first row/column is 0, the second is > 1, etc. That got me at first) > 4. Compute the smallest element in row 1 of y > ---using appropriate headings: > 5. Print out matrices x, y, and z (display on screen, but y'all probably > get that) > 6. Print out sum and smallest element > > The data with which array x is loaded: > 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1, 2, 3, 4 > > The data with which array y is loaded: > 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1 > > Must use functions named as follows: > LOADX, LOADY, COMPUTEZ, SMALLEST, SUMMATION, OUTDATA > > lab5.dat is simply a dat file with the data with which the arrays are > loaded in one long line, each separated by commas. Below, you have lab5x.dat and lab5y.dat. Are there two files, or just one? That's going to make a big difference to the way you read the input. > Thanks- in advance- no more comments after the program. > > This is what I have thus far: > > #Lab #5 > #COSC 1336-31493 > #SUM 2015 NRG > #Tu/Th 1:15-4:25pm > > def main(): > #matrix initialization > x=[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]] > y=[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]] You can simplify the matrix initialization a little bit by using list multiplication: x = [ [0]*3, [0]*3, [0]*3 ] and similarly for y, and z. What they do should be quite obvious: [0]*2 --> [0, 0] ['hello']*3 --> ['hello', 'hello', 'hello'] Now, if you're paying attention, you might think "Wait, why don't I multiply each row as well?" [ [0]*3 ]*5 # Don't do this! I don't want to spend to much time on this, but in a nutshell, the above looks like it should work, but it doesn't work as you would expect because it doesn't copy the inner list. Instead of getting five different rows of [0, 0, 0], you get the same row repeated five times. If my explanation doesn't make sense to you, feel free to ask, or feel free to just accept it on faith that [ [0]*3 ]*5 will not work the way you want. You can always come back to discuss this later. > z=[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]] Your indentation here got messed up. Unfortunately sometimes email doesn't work well with indentation, which is sad. To fix this, you need to indent the line z = ... so that it in aligned with the other lines inside the main function. z = [ [0]*7, [0]*7, etc. ] > #file declaration > infile = open('lab5x.dat','r') > infile = open('lab5y.dat','r') > outfile = open('lab5.out', 'w') You have two variables both called "infile", that isn't going to work. You need to give them separate names, say, infileX and infileY. > #variables > sumx = 0 > sumy = 0 > small = 0 > A = 0 > B = 0 > C = 0 I'm not sure that you need these A B C variables. I think you actually want to use x, y, z, the three matrices you already initialized. > #call functions > LOADX(infile, A) > LOADY(infile, B) > COMPUTEZ(A, B, C) > SUMMATION(A, B) > SMALLEST(A) > OUTDATA(file, A, B, C) That will become: LOADX(infileX, x) LOADY(infileY, y) COMPUTEZ(x, y, z) SUMMATION(x, y) SMALLEST(x) OUTDATA(outfile, x, y, z) > #close files > infile.close() > infile.close() > outfile.close() > dummy = input('Press any key to continue.') Don't forget to change the names of those infiles. More to follow in my next email. -- Steve From steve at pearwood.info Wed Jul 29 15:59:45 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 29 Jul 2015 23:59:45 +1000 Subject: [Tutor] I need help with my homework. No, really.... In-Reply-To: References: Message-ID: <20150729135945.GM25179@ando.pearwood.info> Part 2... On Wed, Jul 29, 2015 at 04:16:58AM -0500, Lissa Hopson wrote: > Given x as an array of [5,3] and y as an array of [3,7] perform the > following: > > 1. Load array x column-wise and array y row-wise > 2. Multiply x by y to compute array z > 3. Compute the sum of all elements in column 2 of array x and add it to the > sum of all elements in row 2 of y (the first row/column is 0, the second is > 1, etc. That got me at first) > 4. Compute the smallest element in row 1 of y > ---using appropriate headings: > 5. Print out matrices x, y, and z (display on screen, but y'all probably > get that) > 6. Print out sum and smallest element > > The data with which array x is loaded: > 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1, 2, 3, 4 > > The data with which array y is loaded: > 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1 > > Must use functions named as follows: > LOADX, LOADY, COMPUTEZ, SMALLEST, SUMMATION, OUTDATA > > lab5.dat is simply a dat file with the data with which the arrays are > loaded in one long line, each separated by commas. > Thanks- in advance- no more comments after the program. > > This is what I have thus far: > > #load matrix x > def LOADX(infile, A): > #local variables > n=0 > k=0 > s=0 It's hard to tell what those variables mean from the names. It may be more useful to give them descriptive names. I think that k is the column number, j (below) is the row number, n is an index into the templist you generate next, and s is, well, I have no idea what s is. You do some calculations on s, but then it never gets used, so I'm not sure what it is for. item = 0 column = 0 s = 0 # huh? > templist = infile.readline().strip('\n').split(',') To be clear, this reads the first line from the file, and one line only. It removes the newline \n from the end, then splits on commas, and returns a list of strings, say: ['1', '2', '3', '4', ...] Is that what you expect? > while (k<3): > j=0 > while(j<5): > A[j][k] = int(templist[n]) > s=s+A[j][k] > j=j+1 > k=k+1 > n=n+1 Assuming s in not needed, this becomes: while (column < 3): row = 0 while(row < 5): A[row][column] = int(templist[item]) row = row + 1 column = column + 1 item = item + 1 But that can't be right, because you end up processing: column=0, row=0 column=1, row=1 column=2, row=2 and then stopping. That only gives you three numbers. What you need is to process fifteen numbers: column=0, row=0 column=0, row=1 column=0, row=2 column=0, row=3 column=0, row=4 column=1, row=0 ... column=2, row=4 The way to do that is to only increase the column when you've processed all the rows. Here's a sketch, you can fill in the details: while (column < 3): while(row < 5): process one element A[row][column] add one to row # when we get here (outdented), we've finished the inner # while loop, but are still inside the outer while loop add one to column > #load matrix y > def LOADY(infile, B): LOADY should be almost exactly the same as LOADX, except that instead of looping down the columns, you should loop across the rows. So: while row < 3: while column < 7: but otherwise more or less the same as LOADX. > #define computation of Z matrix > def COMPUTEZ (A, B, C): Try re-writing COMPUTEZ with row and columns, as above, and see if that makes sense. I can see one obvious problem below: > i=0 > while (i<5): > j=0 > while (j<=7): > k=0 > while (k<=3): > C[i][j]= C[i][j]+ A[i][k] * B[k][j] > k=k+1 This bit can't work, because you have a while loop where k never advances! while k <= 3: process C[i][j] ... but k doesn't change. So Python will loop forever, or until you get sick of waiting and type Ctrl-C to halt it. You need to advance k inside the while loop: while k <= 3: process C[i][j] ... k = k + 1 Remember that the body of the while loop is defined by the *indented* block beneath it. You do have a k = k+1 beneath the while loop, but it isn't indented enough, so it counts as *outside* the while block. I haven't studied it in detail, but you can try fixing that and see if it works. > #def summation > def SUMMATION(x,y): > s=0 > k=0 > j=0 > while (k<5): > sumx=sumx + x[k][2] > k=k+1 > while (j<7): > sumy=sumy + y[2][j] > j=j+1 > s=sumx + sumy This can't work, because sumx and sumy don't have a value to start with. You need to initialize them (perhaps zero?) first. Actually, I don't think you need them at all. I think you can just calculate the total directly: total = 0 while row < 5: total = total + x[row][2] while column < 7: total = total + y[2][column] You'll need to initialize the variables, advance them, etc. More to follow... -- Steve From steve at pearwood.info Wed Jul 29 16:33:26 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 30 Jul 2015 00:33:26 +1000 Subject: [Tutor] I need help with my homework. No, really.... In-Reply-To: References: Message-ID: <20150729143325.GN25179@ando.pearwood.info> Part 3... On Wed, Jul 29, 2015 at 04:16:58AM -0500, Lissa Hopson wrote: > following: > > 1. Load array x column-wise and array y row-wise > 2. Multiply x by y to compute array z > 3. Compute the sum of all elements in column 2 of array x and add it to the > sum of all elements in row 2 of y (the first row/column is 0, the second is > 1, etc. That got me at first) > 4. Compute the smallest element in row 1 of y > ---using appropriate headings: > 5. Print out matrices x, y, and z (display on screen, but y'all probably > get that) > 6. Print out sum and smallest element > > The data with which array x is loaded: > 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1, 2, 3, 4 > > The data with which array y is loaded: > 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 3, 5, 7, 9, 0, 2, 4, 6, 8, 0, 1 > > Must use functions named as follows: > LOADX, LOADY, COMPUTEZ, SMALLEST, SUMMATION, OUTDATA > > lab5.dat is simply a dat file with the data with which the arrays are > loaded in one long line, each separated by commas. > Thanks- in advance- no more comments after the program. > > This is what I have thus far: > > #Lab #5 > #COSC 1336-31493 > #SUM 2015 NRG > #Tu/Th 1:15-4:25pm > > def main(): > #matrix initialization > x=[[0,0,0],[0,0,0],[0,0,0],[0,0,0],[0,0,0]] > y=[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]] > > z=[[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0],[0,0,0,0,0,0,0]] > #file declaration > infile = open('lab5x.dat','r') > infile = open('lab5y.dat','r') > outfile = open('lab5.out', 'w') > #variables > sumx = 0 > sumy = 0 > small = 0 > A = 0 > B = 0 > C = 0 > > #call functions > LOADX(infile, A) > LOADY(infile, B) > COMPUTEZ(A, B, C) > SUMMATION(A, B) > SMALLEST(A) > OUTDATA(file, A, B, C) > #close files > infile.close() > infile.close() > outfile.close() > dummy = input('Press any key to continue.') > #def smallest > def SMALLEST (B): > k=0 > s=B[1][k] > k=k+1 > while (k<7): > if(s> B[1][k]): > s=B[1][k] > k=k+1 I don't think that works at all. There doesn't seem to be any attempt to check for the smallest value. Python has a function, min(), which can take a list of values and returns the smallest of them. So we can do the following: def SMALLEST(B): # Return the smallest value in row 1 of matrix B. get row one pass it to function min() return the result Obviously that's not actual Python code! Now, remember, your matrix looks like this: [ [a, b, c, d], # row 0 [e, f, g, h], # row 1 etc. So getting a row is easy. (Getting a column is trickier.) the_row = B[1] result = min(the_row) return result will put row 1 into variable the_row, then pass it to min(), and finally return it. > def OUTDATA(outfile, x, y, z,SMALLEST,SUMMATION): > i=0 > j=0 > k=0 > while (k<3): > print(A[k][0],A[k][1],A[k][2],A[k][3],A[k][4]) > k=k+1 This should be printing the x matrix, but you're using variable A instead, which as far as I understand it, won't exist. I think the easiest fix for this problem is to change the name in the function declaration: def OUTDATA(outfile, x, y, z, SMALLEST, SUMMATION): becomes: def OUTDATA(outfile, A, B, C, SMALLEST, SUMMATION): Note carefully that you have a clash between the names of the *function* SMALLEST and the argument SMALLEST. Python won't be confused, but you may be! I recommend that you change the name in the function declaration. > file.write[str(A[k][0])+str(A[k][1])+str(A[k][2])+str(A[k][3])+str(A[k][3])+str(A[k][4])] Three problems with this one line: (1) The indentation is lost. Maybe that's just an email thing. (2) The variable should be called outfile, not file. (3) You're writing the numbers mashed up together: "12345678" instead of "12,34,56". Here's a little trick: you can join a list of strings with commas like this: list_of_strings = ['12', '34', '56'] print( ','.join(list_of_strings) ) (except you won't use print, you will write it to a file). So first you make a list of numbers making up the row: row = A[k][:] Convert each item from an int to a str: row = [str(n) for n in row] Join with commas: thestring = ','.join(row) and finally write it to the file: # don't forget the newline at the end of each line outfile.write(thestring + '\n') > while (j<7): > print(B[j][0],B[j][1],B[j][2]) > j=j+1 > file.write[str(B[j][0])+str(B[j][1])+str(B[j][2])] > while (i<7): > print(C[i][0],C[i][1],C[i][2],C[i][3],C[i][4]) > file.write[str(C[i][0]+C[i][1]+C[i][2]+C[i][3]+C[i][4])] Again, I believe these will run all the numbers together. > print ('Summation= ',SUMMATION) > file.write('Summation= ', SUMMATION) The print() line is okay, because Python will happily print ints as well as strings. But you can't write ints to a file, you need to convert to a string first. > print ('Smallest= ',SMALLEST) > file.write('Smallest= ',SMALLEST) Likewise. Whew! I'm not sure if I caught everything. There's probably some more bugs that escaped me, but that should give you a good start to go on with. Good luck! Let us know how you go! -- Steve From steve at pearwood.info Wed Jul 29 16:42:11 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 30 Jul 2015 00:42:11 +1000 Subject: [Tutor] String Attribute In-Reply-To: <55b811cb.0566420a.6c354.4cc9@mx.google.com> References: <55b811cb.0566420a.6c354.4cc9@mx.google.com> Message-ID: <20150729144211.GO25179@ando.pearwood.info> On Tue, Jul 28, 2015 at 11:33:53PM +0000, ltc.hotspot at gmail.com wrote: > > Hi Everyone: > > What is the source of the syntax error to the String Attribute? > > Go to the following URL links and view a copy of the raw data file code and sample data: Please don't send people to URLs to view your code. Copy and paste it into the body of your email. > 1.) http://tinyurl.com/p2xxxhl Running the code in the simulator, I get the following error on line 6: AttributeError: 'str' object has no attribute 'startwith' You misspelled "startswith" as "startwith" (missing the second "s"). -- Steve From steve at pearwood.info Wed Jul 29 17:08:53 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 30 Jul 2015 01:08:53 +1000 Subject: [Tutor] Root and power In-Reply-To: References: Message-ID: <20150729150853.GP25179@ando.pearwood.info> On Tue, Jul 28, 2015 at 08:29:00PM -0700, Job Hernandez wrote: [...] > I need to learn how in the world do find the root and power of an integer > that x user entered? I haven been looking on the python website for an > appropriate function but I have not. Let's suppose the user entered 36. Then the possible answers are: 36**1 = 36 6**2 = 36 and I think that's about it. We know that pwr=0 won't give any solutions unless the number itself is 1: 1**0 = 1 2**0 = 1 3**0 = 1 4**0 = 1 etc. So if the user enters 1, you can just print root=1 and pwr=0 and you are done. (For that matter, you could print any value for root!) Otherwise, for any pwr other than 1, we want to find some root such that: root**pwr = the number the user entered How might we do this for, say, pwr=2, and the number 25? There's no built in function for this, instead you need to do a loop, testing each number in turn: 1**2 = 1, too small 2**2 = 4, too small 3**2 = 9, too small 4**2 = 16, too small 5**2 = 25, equals the user's number so this tells us that 25 is a perfect square, and we can now print root=5, pwr=2. How about pwr=2, number = 27? 1**2 = 1, too small 2**2 = 4, too small 3**2 = 9, too small 4**2 = 16, too small 5**2 = 25, too small 6**2 = 36, too big So this tells us that 27 is NOT a perfect square. Let's check to see if it's a perfect cube: 1**3 = 1, too small 2**3 = 8, too small 3**3 = 27, equals the user's number so 27 is a perfect cube, and we can print root=3, pwr=3. Obviously we don't actually need to check root=1, since 1 to the power of anything is always 1. Let's try (say) 59: 2**2 = 4, too small 3**2 = 9, too small ... 7**2 = 49, too small 8**2 = 64, too big -- pwr cannot be 2 2**3 = 8, too small 3**3 = 27, too small 4**3 = 64, too big -- pwr cannot be 3 2**4 = 16, too small 3**4 = 81, too big -- pwr cannot be 4 2**5 = 32, too small 3**5 = 243, too big -- pwr cannot be 5 2**6 = 64, too big -- pwr cannot be 6 At this point you have a choice: print "No such root and pwr" print "root=59, pwr=1" but I guess the second one is probably going against the spirit of the question. Or maybe not? Hard to say. Obviously you shouldn't write out all the tests by hand: # No, don't do this! if 2**2 == number: print("root=2, pwr=2") elif 3**2 == number: print("root=3, pwr=2") elif 4**2 == number: print("you've got to be kidding, I quit!") Instead you will use for-loops and range, and break to exit out of the loop early. for pwr in range(2, 7): for root in range(2, num): ... Is that enough of a hint, or do you need more assistence? -- Steve From colin.ross.dal at gmail.com Wed Jul 29 17:33:21 2015 From: colin.ross.dal at gmail.com (Colin Ross) Date: Wed, 29 Jul 2015 12:33:21 -0300 Subject: [Tutor] Shading Between Curves with Different Colour Over Specified X value Range In-Reply-To: References: Message-ID: On Tue, Jul 28, 2015 at 11:03 AM, Oscar Benjamin wrote: > On Mon, 27 Jul 2015 at 20:53 Colin Ross wrote: > >> *Goal:* Shade between I_2 (curve 1) and I_3 (curve 2) with following >> conditions: >> - Green for 0 < x < 4 >> - Red for 4 < x < 12 >> >> *Code: * >> >> *Note: Code currently only attempting to shade green for 0 < x < 4 * >> >> import numpy as np >> import pylab >> from pylab import * >> import matplotlib.pyplot as plt >> import csv >> >> >> # Load data from .txt file >> >> with open('current_mirror_output_swing.csv', 'rb') as f: >> reader = csv.reader(f) >> your_list = list(reader) >> >> data = np.asarray(your_list) >> >> I_ref = np.asarray(data[1:,0]) >> I_1 = data[1:,1] >> I_2 = data[1:,2] >> I_3 = data[1:,3] >> >> # Create an array of x values to fill b/w curves with a certain color. >> >> > Here you specify the X values for the fill shape as going from 0 to 4: > > >> X1 = np.linspace(0.,4.,len(I_3)) >> >> I_ref = I_ref.astype(float)*1000. >> I_1 = I_1.astype(float)*1000. >> I_2 = I_2.astype(float)*1000. >> I_3 = I_3.astype(float)*1000. >> >> >> # Plotting commands. >> >> > Here you specify the X values for the line plots as being whatever I_ref > is (I don't know what it is since I can't see your csv file): > > >> plot(I_ref, I_2, 'r-') >> plot(I_ref, I_3, 'b-') >> title('Current Mirror Output Swing') >> xlabel('$I_{ref}$ (mA)') >> ylabel('I (mA)') >> >> > Try changing this line: > > >> plt.fill_between(X1, I_2, I_3, color = 'g', alpha = '0.5') >> > > to: > > plt.fill_between(I_ref, I_2, I_3, color = 'g', alpha = '0.5') > > and then the filled area should have the same X range as the lines. > > -- > Oscar > Thanks! This works now. From colin.ross.dal at gmail.com Wed Jul 29 17:59:33 2015 From: colin.ross.dal at gmail.com (Colin Ross) Date: Wed, 29 Jul 2015 12:59:33 -0300 Subject: [Tutor] Arrow of constant size on log plot Message-ID: I am attempting to draw an arrow that begins at a specified point on a logarithmic plot and then extends a certain distance in the -Y direction. This part is fine, however I would like to draw multiple arrows with the same size corresponding to different points on a plot. If I specify a certain value (as shown in the example below), the arrow size will change depending on where the point is on the plot, since the X and Y axis are logarithmic. ------------------------------------------------------------- import numpy as np import pylab as pl x_1 = np.arange(10) y_1 = np.arange(10) x_1_avg = np.sum(x_1)/len(x_1) y_1_avg = np.sum(y_1)/len(y_1) x_2 = 3. y_2 = 3. pl.plot(x_1,y_1,'k') pl.arrow(x_1_avg,y_1_avg , 0.0, -0.5, fc='b', ec='b', head_width=0.1, head_length=0.1) pl.arrow(x_2,y_2, 0.0, -0.5, fc='r', ec='r', head_width=0.1, head_length=0.1 ) pl.yscale('log') pl.xscale('log') pl.show() ------------------------------------------------------------- Any advice is appreciated. From David.Aldrich at EMEA.NEC.COM Wed Jul 29 17:45:55 2015 From: David.Aldrich at EMEA.NEC.COM (David Aldrich) Date: Wed, 29 Jul 2015 15:45:55 +0000 Subject: [Tutor] Basic question about docstrings Message-ID: <41302A7145AC054FA7A96CFD03835A0A0B9C040C@EX10MBX02.EU.NEC.COM> Hi If I have a script called main.py and document a function in it: def get_value(x): """ Some text ... :param x: Some value :returns: Something useful """ What is the most basic way of showing those docstrings at the Python prompt? For getting started with documentation, is Sphinx a good way to go, or would you recommend something simpler? Best regards David From ltc.hotspot at gmail.com Wed Jul 29 20:38:14 2015 From: ltc.hotspot at gmail.com (ltc.hotspot at gmail.com) Date: Wed, 29 Jul 2015 18:38:14 +0000 Subject: [Tutor] =?utf-8?q?mbox-short?= Message-ID: <55b91e26.2f83460a.bbf2.2029@mx.google.com> Hi Everyone, How do I file in the empty list at 0 on line # 3 to produce the desired output: The first goal of the program is to produce an output from the date list file as following: stephen.marquard at uct.ac.za louis at media.berkeley.edu zqian at umich.edu rjlowe at iupui.edu zqian at umich.edu rjlowe at iupui.edu cwen at iupui.edu cwen at iupui.edu And secondly, print out a count at the end Raw Python code is available at http://tinyurl.com/p4k8qa4 The data input file is available at http://www.pythonlearn.com/code/mbox-short.txt Regards, Hal Sent from Surface Sent from Surface From danny.yoo at gmail.com Wed Jul 29 21:56:29 2015 From: danny.yoo at gmail.com (Danny Yoo) Date: Wed, 29 Jul 2015 12:56:29 -0700 Subject: [Tutor] mbox-short In-Reply-To: <55b91e26.2f83460a.bbf2.2029@mx.google.com> References: <55b91e26.2f83460a.bbf2.2029@mx.google.com> Message-ID: > How do I file in the empty list at 0 on line # 3 to produce the desired output: What trouble are you having? Please try to describe where you are getting stuck. What have you tried? Is there anything confusing? Also note that a few of your peers have asked the exact same homework assignment on this list. We are not a homework answering service: rather, we are a group of volunteers to help beginners learn to program. The distinction is that we actually care about *why* you're having difficulty. The homework assignment itself is not our focus: what matters to us is how to help you learn how to solve these kinds of problems. So, can you say more why you're getting stuck? From emile at fenx.com Wed Jul 29 21:57:46 2015 From: emile at fenx.com (Emile van Sebille) Date: Wed, 29 Jul 2015 12:57:46 -0700 Subject: [Tutor] Basic question about docstrings In-Reply-To: <41302A7145AC054FA7A96CFD03835A0A0B9C040C@EX10MBX02.EU.NEC.COM> References: <41302A7145AC054FA7A96CFD03835A0A0B9C040C@EX10MBX02.EU.NEC.COM> Message-ID: On 7/29/2015 8:45 AM, David Aldrich wrote: > Hi > > If I have a script called main.py and document a function in it: > > def get_value(x): > """ > Some text ... > :param x: Some value > :returns: Something useful > """ > > What is the most basic way of showing those docstrings at the Python prompt? Are you asking about help? as in: >>> help(get_value) Emile From dyoo at hashcollision.org Wed Jul 29 22:00:13 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 29 Jul 2015 13:00:13 -0700 Subject: [Tutor] Basic question about docstrings In-Reply-To: <41302A7145AC054FA7A96CFD03835A0A0B9C040C@EX10MBX02.EU.NEC.COM> References: <41302A7145AC054FA7A96CFD03835A0A0B9C040C@EX10MBX02.EU.NEC.COM> Message-ID: On Jul 29, 2015 12:45 PM, "David Aldrich" wrote: > > Hi > > If I have a script called main.py and document a function in it: > > def get_value(x): > """ > Some text ... > :param x: Some value > :returns: Something useful > """ > > What is the most basic way of showing those docstrings at the Python prompt? Try: help(get_value) At the Python prompt. This uses the built in help facility: https://docs.python.org/2/library/functions.html#help From breamoreboy at yahoo.co.uk Wed Jul 29 22:31:08 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 29 Jul 2015 21:31:08 +0100 Subject: [Tutor] mbox-short In-Reply-To: <55b91e26.2f83460a.bbf2.2029@mx.google.com> References: <55b91e26.2f83460a.bbf2.2029@mx.google.com> Message-ID: On 29/07/2015 19:38, ltc.hotspot at gmail.com wrote: > I have no intention of answering a question that contains massives of whitespace and no code. Please post your code inline. -- 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 Jul 30 02:16:15 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 30 Jul 2015 01:16:15 +0100 Subject: [Tutor] mbox-short In-Reply-To: <55b91e26.2f83460a.bbf2.2029@mx.google.com> References: <55b91e26.2f83460a.bbf2.2029@mx.google.com> Message-ID: On 29/07/15 19:38, ltc.hotspot at gmail.com wrote: > How do I file in the empty list at 0 on line # 3 to produce the desired output: I have no idea what you mean? What does "file in the empty list" mean? What are you trying to do? > The first goal of the program is to produce an output from the date list file as following: Should that be "data list" rather than date list? > stephen.marquard at uct.ac.za > louis at media.berkeley.edu > zqian at umich.edu > > And secondly, print out a count at the end The count bit is easily done once you have the other bit working. But its not clear what you re asking us to do. Also please post the code in the mail rather than pastebin. Pastebin is fine if its a big program (>100 lines say) but for small programs just paste the code into the email. Its much easier to read and comment on that way. > Raw Python code is available at http://tinyurl.com/p4k8qa4 > The data input file is available at http://www.pythonlearn.com/code/mbox-short.txt -- 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 ltc.hotspot at gmail.com Wed Jul 29 23:55:34 2015 From: ltc.hotspot at gmail.com (ltc.hotspot at gmail.com) Date: Wed, 29 Jul 2015 21:55:34 +0000 Subject: [Tutor] =?utf-8?q?Mailbox?= In-Reply-To: <55b9386d.6a65420a.1866.275d@mx.google.com> References: <1438127743513.656239125@boxbe> <55b9270a.2558460a.9efb4.2a02@mx.google.com> <55b92da0.aaf5420a.ff2b1.2128@mx.google.com>, , <55b9386d.6a65420a.1866.275d@mx.google.com> Message-ID: <55b94c40.e201460a.c7eec.3d3a@mx.google.com> Hi Everyone: I have a second and unrelated question: I tried to work backward to see if there is a logic error associated with a variable is being skipped over: #top of code, initialize variable output_list = ["default"] #rest of code If you get at the end print output_list ['default'] Raw Data File: count = 0 fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" for line in fname: line = line.strip() if not line.startswith('From '): continue line = line.split() count = count + 1 print len(line) fh = open(fname) print "There were", count, "lines in the file with From as the first word" Sample data file at http://www.pythonlearn.com/code/mbox-short.txt Desired Output: stephen.marquard at uct.ac.za louis at media.berkeley.edu zqian at umich.edu rjlowe at iupui.edu zqian at umich.edu rjlowe at iupui.edu Thanks, Hal Sent from Surface From: Phu Sam Sent: ?Wednesday?, ?July? ?29?, ?2015 ?1?:?06? ?PM To: Ltc Hotspot _______________________________________________ Baypiggies mailing list Baypiggies at python.org To change your subscription options or unsubscribe: https://mail.python.org/mailman/listinfo/baypiggies From cs at zip.com.au Thu Jul 30 04:01:05 2015 From: cs at zip.com.au (Cameron Simpson) Date: Thu, 30 Jul 2015 12:01:05 +1000 Subject: [Tutor] Mailbox In-Reply-To: <55b94c40.e201460a.c7eec.3d3a@mx.google.com> References: <55b94c40.e201460a.c7eec.3d3a@mx.google.com> Message-ID: <20150730020105.GA96916@cskk.homeip.net> On 29Jul2015 21:55, ltc.hotspot at gmail.com wrote: >I have a second and unrelated question: > >I tried to work backward to see if there is a logic error associated with a variable is being skipped over: > >#top of code, initialize variable >output_list = ["default"] Curious: why not just make this an empty list: [] [...snip...] >count = 0 >fname = raw_input("Enter file name: ") >if len(fname) < 1 : fname = "mbox-short.txt" >for line in fname: > line = line.strip() > if not line.startswith('From '): continue > line = line.split() >count = count + 1 >print len(line) >fh = open(fname) >print "There were", count, "lines in the file with From as the first word" [...snip...] My first observation would be that "count = count + 1" is not inside the loop. That means that it fires just once, _after_ the loop. So it will always be 1. Also, what is the purpose of this line: line = line.split() Cheers, Cameron Simpson From alan.gauld at btinternet.com Thu Jul 30 09:26:21 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 30 Jul 2015 08:26:21 +0100 Subject: [Tutor] Mailbox In-Reply-To: <55b94c40.e201460a.c7eec.3d3a@mx.google.com> References: <1438127743513.656239125@boxbe> <55b9270a.2558460a.9efb4.2a02@mx.google.com> <55b92da0.aaf5420a.ff2b1.2128@mx.google.com>, , <55b9386d.6a65420a.1866.275d@mx.google.com> <55b94c40.e201460a.c7eec.3d3a@mx.google.com> Message-ID: On 29/07/15 22:55, ltc.hotspot at gmail.com wrote: > #top of code, initialize variable > output_list = ["default"] > > #rest of code > print output_list > ['default'] > > Raw Data File: Note, this is the code not the data... > count = 0 > fname = raw_input("Enter file name: ") > if len(fname) < 1 : fname = "mbox-short.txt" > for line in fname: You are still looping over the filename not the file contents. thus line will take the values: m, b, ,o, x -, s, h,.... > line = line.strip() > if not line.startswith('From '): continue And this will always be true so the loop will stop here. > line = line.split() > count = count + 1 this is outside the loop so will always be 1 > print len(line) > fh = open(fname) this opens the file but you do nothing with it. > print "There were", count, "lines in the file with From as the first word" You do not seem to have changed anything from your original post. All the same errors are still there. -- 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 jobrh2993 at gmail.com Thu Jul 30 04:03:36 2015 From: jobrh2993 at gmail.com (Job Hernandez) Date: Wed, 29 Jul 2015 19:03:36 -0700 Subject: [Tutor] Root and power In-Reply-To: References: Message-ID: Thank you all,\. Programming is the hardest thing i have ever tried to learn. So thank you for your help. Job On Tue, Jul 28, 2015 at 8:29 PM, Job Hernandez wrote: > How is it going tutors? > > The following problem seems impossible to me: > > "*Write a program that asks the user to enter an integer and prints two > integers, root and pwr, such that 0 < pwr < 6 and root^pwr (root**pwr) is > equal to the integer entered by the user. If no such pair of integers > exists, it should print a message to that effect*." > > I would like to solve this problem myself so please don't give me the > solution. > > I need to learn how in the world do find the root and power of an integer > that x user entered? I haven been looking on the python website for an > appropriate function but I have not. > > If you have the time can you please tell me about the functions and other > facts I need to know in order to solve this problem? > > Is there a book you guys recommend for total beginners who have no ideal > of what computer science and programming is? > > Thank you, > > Job > > > From nymcity at yahoo.com Thu Jul 30 04:41:20 2015 From: nymcity at yahoo.com (Nym City) Date: Thu, 30 Jul 2015 02:41:20 +0000 (UTC) Subject: [Tutor] Socket Module In-Reply-To: <2099282370.4058697.1438048354309.JavaMail.yahoo@mail.yahoo.com> References: <2099282370.4058697.1438048354309.JavaMail.yahoo@mail.yahoo.com> Message-ID: <1641578950.5593522.1438224080105.JavaMail.yahoo@mail.yahoo.com> Writing to share an update on my previous request. So, after reviewing my code over, it seems like my last print statement "print(ResolvedAddresses)" was not properly indented inside the for loop - and for that reason the output was coming our as empty. After I aligned that with the rest of the loop, it worked fine. I still have couple parts to add to this project but at least this hurdle is behind. Thank you. On Tuesday, July 28, 2015 4:49 AM, Nym City via Tutor wrote: Hi Martin, Thank you for taking the time to write such a detailed response. Very much appreciate it. I tried the code again with modifications that you suggested and even though none of the public addresses resolved; I did get little more details. I am still working on finding a solution for this and I will share it here when I have it. ?Thank you. ? ? On Sunday, July 26, 2015 6:59 PM, Martin A. Brown wrote: ? Hello Nym, > Here is the updated code: https://bpaste.net/show/358583e1a0bd It's short.? I have included inline here: ? import socket ? ListOfIPAddresses = [] ? with open('top500ips.csv', 'r') as f: ? ? ? for line in f: ? ? ? ? ? line = line.strip() ? ? ? ? ? ListOfIPAddresses.append(line) ? for address in ListOfIPAddresses: ? ? ? try: ? ? ? ? ? ResolvedAddresses = socket.gethostbyaddr(address) ? ? ? except: ? ? ? ? ? print('No Resolution Available') ? print(ResolvedAddresses) > The issue that I am running into now is that for some reason, the > script is not resolving known-public IP addresses that I am > passing through. For example, some of the IPs, that I have used > are for sites like facebook (173.252.120.6) github > (207.97.227.239), however the script is not able to resolve them. > > But its interesting that I am able to resolve them using nslookup > on windows command prompt. Which means my laptop's DNS setting is > fine. The apparent (?) DNS lookup failure ----------------------------------- At time X, you run your Python program and something (perhaps in the DNS resolution process) fails and you see "No Resolution Available", but you do not know what has failed, nor for which address lookup. At time Y, you run 'nslookup' at the shell prompt, receive an answer and conclude that your script is operating properly.? While this is may appear logical, it is an incorrect conclusion. One coding error (a dangerous habit to perpetuate) -------------------------------------------------- When performing the DNS lookup, you are using something called a 'bare except'.? This will catch any and all errors, even if it's something unrelated like, for example, a signal.? This is a bad and dangerous habit.? In general, you should catch only the exceptions that you can do something about. In addition, this will offer you more information about the problem. Here's a simple example, where I'm only changing two lines: ? for address in ListOfIPAddresses: ? ? ? try: ? ? ? ? ? ResolvedAddresses = socket.gethostbyaddr(address) ? ? ? except socket.herror as e: ? ? ? ? ? print("No resolution available for %s: %s" % (address, e)) This will give you a little more information about what, specifically, the failure is in your call to socket.gethostbyaddr() Comment on NXdomain responses ----------------------------- I picked at random an address that had no PTR record and tried to call socket.gethostbyaddr('8.97.227.2').? What do you think I got for my trouble?? When running through the code block above, I saw the following output to my terminal: ? No resolution available for 8.97.227.2: [Errno 0] Resolver Error 0 (no error) In short, there is no guarantee that anybody has properly set up reverse DNS entries (DNS PTR records) for the addresses you are looking up.? Although the vast majority of lookups occur successfully and smoothly, there are many things that can go wrong in the network and on an end host which can cause transient errors during DNS lookups, and it is possible that you have already encountered some of these problems (even though I would not expect to hit very many errors looking up PTR records for only 500 IPs). May I wish you good luck resolving not just your addresses, but also your problem! -Martin -- Martin A. Brown http://linux-ip.net/ ? _______________________________________________ Tutor maillist? -? Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From David.Aldrich at EMEA.NEC.COM Thu Jul 30 10:24:27 2015 From: David.Aldrich at EMEA.NEC.COM (David Aldrich) Date: Thu, 30 Jul 2015 08:24:27 +0000 Subject: [Tutor] Basic question about docstrings In-Reply-To: References: <41302A7145AC054FA7A96CFD03835A0A0B9C040C@EX10MBX02.EU.NEC.COM> Message-ID: <41302A7145AC054FA7A96CFD03835A0A0B9C070C@EX10MBX02.EU.NEC.COM> >> If I have a script called main.py and document a function in it: >> >> def get_value(x): >> """ >> Some text ... >> :param x: Some value >> :returns: Something useful >> """ >> >> What is the most basic way of showing those docstrings at the Python >> prompt? > > Try: > > help(get_value) > > At the Python prompt. This uses the built in help facility: > > https://docs.python.org/2/library/functions.html#help > I understand that 'help' works with modules that I have imported. But if I've just written a script called main.py (which contains get_value()) I don't think I can 'import' that. So how would I see the docstrings in main.py? From joel.goldstick at gmail.com Thu Jul 30 18:15:31 2015 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 30 Jul 2015 12:15:31 -0400 Subject: [Tutor] Basic question about docstrings In-Reply-To: <41302A7145AC054FA7A96CFD03835A0A0B9C070C@EX10MBX02.EU.NEC.COM> References: <41302A7145AC054FA7A96CFD03835A0A0B9C040C@EX10MBX02.EU.NEC.COM> <41302A7145AC054FA7A96CFD03835A0A0B9C070C@EX10MBX02.EU.NEC.COM> Message-ID: On Thu, Jul 30, 2015 at 4:24 AM, David Aldrich wrote: >>> If I have a script called main.py and document a function in it: >>> >>> def get_value(x): >>> """ >>> Some text ... >>> :param x: Some value >>> :returns: Something useful >>> """ >>> >>> What is the most basic way of showing those docstrings at the Python >>> prompt? >> >> Try: >> >> help(get_value) >> >> At the Python prompt. This uses the built in help facility: >> >> https://docs.python.org/2/library/functions.html#help >> > > I understand that 'help' works with modules that I have imported. But if I've just written a script called main.py (which contains get_value()) I don't think I can 'import' that. So how would I see the docstrings in main.py? > Try it! you'll find it works fine -- Joel Goldstick http://joelgoldstick.com From steve at pearwood.info Thu Jul 30 18:26:11 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 31 Jul 2015 02:26:11 +1000 Subject: [Tutor] Basic question about docstrings In-Reply-To: <41302A7145AC054FA7A96CFD03835A0A0B9C070C@EX10MBX02.EU.NEC.COM> References: <41302A7145AC054FA7A96CFD03835A0A0B9C040C@EX10MBX02.EU.NEC.COM> <41302A7145AC054FA7A96CFD03835A0A0B9C070C@EX10MBX02.EU.NEC.COM> Message-ID: <20150730162610.GR25179@ando.pearwood.info> On Thu, Jul 30, 2015 at 08:24:27AM +0000, David Aldrich wrote: > I understand that 'help' works with modules that I have imported. But > if I've just written a script called main.py (which contains > get_value()) I don't think I can 'import' that. So how would I see the > docstrings in main.py? import main help(main) Of course, you have to write main.py so it is intended to be imported. If you write main.py as a regular script, then importing it the first time will run the script, which you don't want. The trick is to put the script functionality in a function, say: # File script.py def do_this(): """This is a helper function""" ... def main(): """Main function.""" print("Processing...") do_this() do_that() do_something_else() print("Done!") if __name__ == "__main__": # Run the main script. main() By using that pattern, the file "script.py" can be imported at the interactive interpreter: import script help(script.main) help(script.do_this) # etc. but of you run it directly, outside the interactive intepreter, the main() function will run and it will behave as a script as expected. The reason this works is that when you run a script, Python sets the magic variable __name__ to "__main__". But when you import it, the variable is set to the actual name of the file (minus the .py extension). -- Steve From steve at pearwood.info Thu Jul 30 19:09:37 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 31 Jul 2015 03:09:37 +1000 Subject: [Tutor] Basic question about docstrings In-Reply-To: <41302A7145AC054FA7A96CFD03835A0A0B9C0A6A@EX10MBX02.EU.NEC.COM> References: <41302A7145AC054FA7A96CFD03835A0A0B9C040C@EX10MBX02.EU.NEC.COM> <41302A7145AC054FA7A96CFD03835A0A0B9C070C@EX10MBX02.EU.NEC.COM> <20150730162610.GR25179@ando.pearwood.info> <41302A7145AC054FA7A96CFD03835A0A0B9C0A6A@EX10MBX02.EU.NEC.COM> Message-ID: <20150730170937.GS25179@ando.pearwood.info> On Thu, Jul 30, 2015 at 04:28:33PM +0000, David Aldrich wrote: > So main.py contains: > > def get_field(value, start_bit, end_bit): > > > and I see: > > >>> import main > >>> help(get_field) > Traceback (most recent call last): > File "", line 1, in > NameError: name 'get_field' is not defined Naturally. Name resolution works the same for help() as for anything else. If you say: import math then you need to refer to math.sin and math.cos, not sin or cos on their own. The same applies to your main.get_field, whether you are using help() or not. -- Steve From David.Aldrich at EMEA.NEC.COM Thu Jul 30 18:28:33 2015 From: David.Aldrich at EMEA.NEC.COM (David Aldrich) Date: Thu, 30 Jul 2015 16:28:33 +0000 Subject: [Tutor] Basic question about docstrings In-Reply-To: <20150730162610.GR25179@ando.pearwood.info> References: <41302A7145AC054FA7A96CFD03835A0A0B9C040C@EX10MBX02.EU.NEC.COM> <41302A7145AC054FA7A96CFD03835A0A0B9C070C@EX10MBX02.EU.NEC.COM> <20150730162610.GR25179@ando.pearwood.info> Message-ID: <41302A7145AC054FA7A96CFD03835A0A0B9C0A6A@EX10MBX02.EU.NEC.COM> >>>> If I have a script called main.py and document a function in it: >>>> >>>> def get_value(x): >>>> """ >>>> Some text ... >>>> :param x: Some value >>>> :returns: Something useful >>>> """ >>>> >>>> What is the most basic way of showing those docstrings at the >>>> Python prompt? >>> >>> Try: >>> >>> help(get_value) >>> >>> At the Python prompt. This uses the built in help facility: >>> >>> https://docs.python.org/2/library/functions.html#help >>> >> >> I understand that 'help' works with modules that I have imported. But if I've just written a script called main.py (which contains get_value()) I don't think I can 'import' that. So how would I see the docstrings in main.py? >> > Try it! you'll find it works fine So main.py contains: def get_field(value, start_bit, end_bit): and I see: >>> import main >>> help(get_field) Traceback (most recent call last): File "", line 1, in NameError: name 'get_field' is not defined help(main) works ok but is rather verbose. From ltc.hotspot at gmail.com Thu Jul 30 21:07:33 2015 From: ltc.hotspot at gmail.com (ltc.hotspot at gmail.com) Date: Thu, 30 Jul 2015 19:07:33 +0000 Subject: [Tutor] =?utf-8?q?String_Attribute?= In-Reply-To: <55ba7145.aa88420a.88128.7f18@mx.google.com> References: <55b811cb.0566420a.6c354.4cc9@mx.google.com>, <20150729144211.GO25179@ando.pearwood.info>, <55ba7145.aa88420a.88128.7f18@mx.google.com> Message-ID: <55ba761a.af80460a.8b634.ffff8861@mx.google.com> Sent from Surface From: ltc.hotspot at gmail.com Sent: ?Thursday?, ?July? ?30?, ?2015 ?11?:?47? ?AM To: Steven D'Aprano Hi Steve: New revision code: count = 0 fn = raw_input("Enter file name: ") if len(fn) < 1 : fname = "mbox-short.txt" for line in fn: if 'From' in line.split()[0]: count += 1 print "There are %d lines starting with From" % count print len(line) fn = open(fname) print "There were", count, "lines in the file with From as the first word" Syntax message produced by iPython interperter: NameError Traceback (most recent call last) C:\Users\vm\Desktop\apps\docs\Python\assinment_8_5_v_2.py in () 6 print "There are %d lines starting with From" % count 7 print len(line) ----> 8 fn = open(fname) 9 print "There were", count, "lines in the file with From as the first wor d" NameError: name 'fname' is not defined In [16]: Question: Why is fname = "mbox-short.txt" not loading the sample data? Sample data file is located at http://www.pythonlearn.com/code/mbox-short.txt Regards, Hal Sent from Surface From: Steven D'Aprano Sent: ?Wednesday?, ?July? ?29?, ?2015 ?7?:?42? ?AM To: ltc.hotspot at gmail.com Cc: Tutor at python.org On Tue, Jul 28, 2015 at 11:33:53PM +0000, ltc.hotspot at gmail.com wrote: > > Hi Everyone: > > What is the source of the syntax error to the String Attribute? > > Go to the following URL links and view a copy of the raw data file code and sample data: Please don't send people to URLs to view your code. Copy and paste it into the body of your email. > 1.) http://tinyurl.com/p2xxxhl Running the code in the simulator, I get the following error on line 6: AttributeError: 'str' object has no attribute 'startwith' You misspelled "startswith" as "startwith" (missing the second "s"). -- Steve From breamoreboy at yahoo.co.uk Fri Jul 31 00:25:06 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 30 Jul 2015 23:25:06 +0100 Subject: [Tutor] String Attribute In-Reply-To: <55ba761a.af80460a.8b634.ffff8861@mx.google.com> References: <55b811cb.0566420a.6c354.4cc9@mx.google.com> <20150729144211.GO25179@ando.pearwood.info> <55ba7145.aa88420a.88128.7f18@mx.google.com> <55ba761a.af80460a.8b634.ffff8861@mx.google.com> Message-ID: On 30/07/2015 20:07, ltc.hotspot at gmail.com wrote: > When you post here can you please find a mechanism that gives us more text than whitespace, thank you. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From ltc.hotspot at gmail.com Thu Jul 30 23:17:49 2015 From: ltc.hotspot at gmail.com (ltc.hotspot at gmail.com) Date: Thu, 30 Jul 2015 21:17:49 +0000 Subject: [Tutor] =?utf-8?q?String_Attribute?= In-Reply-To: <55ba761a.af80460a.8b634.ffff8861@mx.google.com> References: <55b811cb.0566420a.6c354.4cc9@mx.google.com>, <20150729144211.GO25179@ando.pearwood.info>, <55ba7145.aa88420a.88128.7f18@mx.google.com>, <55ba761a.af80460a.8b634.ffff8861@mx.google.com> Message-ID: <55ba94ab.af80460a.8b634.ffff9a3f@mx.google.com> Hi everyone, Revised code: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" # assign fname fh=open(fname,'r') # Open a new file handle for line in fh: print line if 'From' in line.split()[0] and '@' in line: sender = line.split()[1] fn.seek(0) print sender Questions: Why is the loop not repeating, and where should I insert a split to remove 'Sat Jan 5:09:14:16 2008' From stephen.marquard at uct.ac.za Sat Jan 5 09:14:16 2008 ? Mismatch Sent from Surface From: ltc.hotspot at gmail.com Sent: ?Thursday?, ?July? ?30?, ?2015 ?12?:?07? ?PM To: Tutor at python.org Sent from Surface From: ltc.hotspot at gmail.com Sent: ?Thursday?, ?July? ?30?, ?2015 ?11?:?47? ?AM To: Steven D'Aprano Hi Steve: New revision code: count = 0 fn = raw_input("Enter file name: ") if len(fn) < 1 : fname = "mbox-short.txt" for line in fn: if 'From' in line.split()[0]: count += 1 print "There are %d lines starting with From" % count print len(line) fn = open(fname) print "There were", count, "lines in the file with From as the first word" Syntax message produced by iPython interperter: NameError Traceback (most recent call last) C:\Users\vm\Desktop\apps\docs\Python\assinment_8_5_v_2.py in () 6 print "There are %d lines starting with From" % count 7 print len(line) ----> 8 fn = open(fname) 9 print "There were", count, "lines in the file with From as the first wor d" NameError: name 'fname' is not defined In [16]: Question: Why is fname = "mbox-short.txt" not loading the sample data? Sample data file is located at http://www.pythonlearn.com/code/mbox-short.txt Regards, Hal Sent from Surface From: Steven D'Aprano Sent: ?Wednesday?, ?July? ?29?, ?2015 ?7?:?42? ?AM To: ltc.hotspot at gmail.com Cc: Tutor at python.org On Tue, Jul 28, 2015 at 11:33:53PM +0000, ltc.hotspot at gmail.com wrote: > > Hi Everyone: > > What is the source of the syntax error to the String Attribute? > > Go to the following URL links and view a copy of the raw data file code and sample data: Please don't send people to URLs to view your code. Copy and paste it into the body of your email. > 1.) http://tinyurl.com/p2xxxhl Running the code in the simulator, I get the following error on line 6: AttributeError: 'str' object has no attribute 'startwith' You misspelled "startswith" as "startwith" (missing the second "s"). -- Steve From ltc.hotspot at gmail.com Fri Jul 31 00:34:05 2015 From: ltc.hotspot at gmail.com (ltc.hotspot at gmail.com) Date: Thu, 30 Jul 2015 22:34:05 +0000 Subject: [Tutor] =?utf-8?q?String_Attribute?= In-Reply-To: References: <55b811cb.0566420a.6c354.4cc9@mx.google.com> <20150729144211.GO25179@ando.pearwood.info> <55ba7145.aa88420a.88128.7f18@mx.google.com> <55ba761a.af80460a.8b634.ffff8861@mx.google.com>, Message-ID: <55baa676.6164460a.481f3.ffff9960@mx.google.com> sure Sent from Surface From: Mark Lawrence Sent: ?Thursday?, ?July? ?30?, ?2015 ?3?:?25? ?PM To: Tutor at python.org _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor From ltc.hotspot at gmail.com Fri Jul 31 00:51:54 2015 From: ltc.hotspot at gmail.com (ltc.hotspot at gmail.com) Date: Thu, 30 Jul 2015 22:51:54 +0000 Subject: [Tutor] =?utf-8?q?String_Attribute?= In-Reply-To: References: <55b811cb.0566420a.6c354.4cc9@mx.google.com> <20150729144211.GO25179@ando.pearwood.info> <55ba7145.aa88420a.88128.7f18@mx.google.com> <55ba761a.af80460a.8b634.ffff8861@mx.google.com>, Message-ID: <55baab2f.c5bd420a.18e3.ffffa685@mx.google.com> Hi Mark, I?m still confused because line 4 reads: fh=open(fname,'r') # Open a new file handle, not fn = open(fname) Therefore, can you write down line by line from error to correction? Here is the revised code: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" # assign fname fh=open(fname,'r') # Open a new file handle for line in fh: print line if 'From' in line.split()[0] and '@' in line: sender = line.split()[2] print sender Regards, Hal Sent from Surface _______________________________________________ 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 Fri Jul 31 02:04:56 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 31 Jul 2015 01:04:56 +0100 Subject: [Tutor] String Attribute In-Reply-To: <55ba94ab.af80460a.8b634.ffff9a3f@mx.google.com> References: <55b811cb.0566420a.6c354.4cc9@mx.google.com>, <20150729144211.GO25179@ando.pearwood.info>, <55ba7145.aa88420a.88128.7f18@mx.google.com>, <55ba761a.af80460a.8b634.ffff8861@mx.google.com> <55ba94ab.af80460a.8b634.ffff9a3f@mx.google.com> Message-ID: On 30/07/15 22:17, ltc.hotspot at gmail.com wrote: > fname = raw_input("Enter file name: ") > if len(fname) < 1 : fname = "mbox-short.txt" # assign fname > fh=open(fname,'r') # Open a new file handle > for line in fh: > print line > if 'From' in line.split()[0] and '@' in line: sender = line.split()[1] > fn.seek(0) > print sender > > Questions: Why is the loop not repeating, What makes you think so? If you get an error(as I suspect) please post the entire error message. I would expect a name error on the last line of the loop since there is no variable fn defined. I don't know what you think the seek() is doing, but (assuming you meant fh) it will reset the file to the first line each time so you never finish the loop. > and where should I insert a split to remove 'Sat Jan 5:09:14:16 2008' > > From stephen.marquard at uct.ac.za Sat Jan 5 09:14:16 2008 ? Mismatch Splitting on whitespace will ensure the bit you want is in the second element -- 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 Fri Jul 31 02:13:35 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 31 Jul 2015 01:13:35 +0100 Subject: [Tutor] String Attribute In-Reply-To: <55baab2f.c5bd420a.18e3.ffffa685@mx.google.com> References: <55b811cb.0566420a.6c354.4cc9@mx.google.com> <20150729144211.GO25179@ando.pearwood.info> <55ba7145.aa88420a.88128.7f18@mx.google.com> <55ba761a.af80460a.8b634.ffff8861@mx.google.com>, <55baab2f.c5bd420a.18e3.ffffa685@mx.google.com> Message-ID: On 30/07/15 23:51, ltc.hotspot at gmail.com wrote: > fname = raw_input("Enter file name: ") > if len(fname) < 1 : fname = "mbox-short.txt" # assign fname > fh=open(fname,'r') # Open a new file handle > for line in fh: > print line > if 'From' in line.split()[0] and '@' in line: sender = line.split()[2] Note that you are overwriting sender each time through the loop. Also [2] isa the third element, I think you want the second [1] BTW Its probably clearer to write that last line as: if line.startswith('From') and '@' in line: sender = line.split()[1] Better still may be to split the line first: sections = line.split() if 'FROM' in sections[0].upper() and '@' in sections: sender = sections[1] > print sender And this is outside the loop so will only print the last item. To print all of them you need to move the print inside the loop. 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 ltc.hotspot at gmail.com Fri Jul 31 01:41:56 2015 From: ltc.hotspot at gmail.com (ltc.hotspot at gmail.com) Date: Thu, 30 Jul 2015 23:41:56 +0000 Subject: [Tutor] =?utf-8?q?=27open=27_is_not_defined?= Message-ID: <55baba60.46eb420a.2ce03.ffffadc9@mx.google.com> Hi Everyone: Why is open not defined in the following code:NameError: name 'open' is not defined Code reads as follows: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 for line in fh: if not line.startswith('From'): continue line2 = line.strip() line3 = line2.split() line4 = line3[1] print line4 count = count + 1 print "There were", count, "lines in the file with From as the first word" Regards, Hal Sent from Surface From breamoreboy at yahoo.co.uk Fri Jul 31 02:30:57 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 31 Jul 2015 01:30:57 +0100 Subject: [Tutor] String Attribute In-Reply-To: <55baa676.6164460a.481f3.ffff9960@mx.google.com> References: <55b811cb.0566420a.6c354.4cc9@mx.google.com> <20150729144211.GO25179@ando.pearwood.info> <55ba7145.aa88420a.88128.7f18@mx.google.com> <55ba761a.af80460a.8b634.ffff8861@mx.google.com> <55baa676.6164460a.481f3.ffff9960@mx.google.com> Message-ID: On 30/07/2015 23:34, ltc.hotspot at gmail.com wrote: > sure > > > > > > > Sent from Surface > > > > > > From: Mark Lawrence > Sent: ?Thursday?, ?July? ?30?, ?2015 ?3?:?25? ?PM > To: Tutor at python.org > > > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > Could have fooled me :( -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From emile at fenx.com Fri Jul 31 03:01:27 2015 From: emile at fenx.com (Emile van Sebille) Date: Thu, 30 Jul 2015 18:01:27 -0700 Subject: [Tutor] 'open' is not defined In-Reply-To: <55baba60.46eb420a.2ce03.ffffadc9@mx.google.com> References: <55baba60.46eb420a.2ce03.ffffadc9@mx.google.com> Message-ID: On 7/30/2015 4:41 PM, ltc.hotspot at gmail.com wrote: > Hi Everyone: > > > Why is open not defined in the following code:NameError: name 'open' is not defined Because of something you did previously. We don't have enough information to answer. open exists as a built-in function in python: Python 2.5 (r25:51908, Dec 18 2009, 14:22:21) [GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> open We'd need to see a full cut-n-paste example to help diagnose what's actually happening. I can get a NameError with: >>> del open Traceback (most recent call last): File "", line 1, in NameError: name 'open' is not defined >>> So, don't try to delete built-in identifiers. For example, when I paste your code from below into a python shell, I get: Python 2.5 (r25:51908, Dec 18 2009, 14:22:21) [GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> fname = raw_input("Enter file name: ") Enter file name: if len(fname) < 1 : fname = "mbox-short.txt" >>> fh = open(fname) Traceback (most recent call last): File "", line 1, in IOError: [Errno 2] No such file or directory: 'if len(fname) < 1 : fname = "mbox-short.txt"' >>> count = 0 >>> for line in fh: Please paste in the contents from your screen where the error appears. Note that in my example the open line complains about fname not existing so I'm getting and IOError, and if the issue were with open, as per the example with xxopen below, you then get the NameError. >>> fh = xxopen('yyy') Traceback (most recent call last): File "", line 1, in NameError: name 'xxopen' is not defined Please post the equivalent from your system and we can help better. Emile > > > Code reads as follows: > > > > fname = raw_input("Enter file name: ") > if len(fname) < 1 : fname = "mbox-short.txt" > fh = open(fname) > count = 0 > for line in fh: > if not line.startswith('From'): continue > line2 = line.strip() > line3 = line2.split() > line4 = line3[1] > print line4 > count = count + 1 > print "There were", count, "lines in the file with From as the first word" > > > > Regards, > > Hal > > > > > > > Sent from Surface > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From danny.yoo at gmail.com Fri Jul 31 03:49:44 2015 From: danny.yoo at gmail.com (Danny Yoo) Date: Thu, 30 Jul 2015 18:49:44 -0700 Subject: [Tutor] Basic question about docstrings In-Reply-To: <41302A7145AC054FA7A96CFD03835A0A0B9C0A6A@EX10MBX02.EU.NEC.COM> References: <41302A7145AC054FA7A96CFD03835A0A0B9C040C@EX10MBX02.EU.NEC.COM> <41302A7145AC054FA7A96CFD03835A0A0B9C070C@EX10MBX02.EU.NEC.COM> <20150730162610.GR25179@ando.pearwood.info> <41302A7145AC054FA7A96CFD03835A0A0B9C0A6A@EX10MBX02.EU.NEC.COM> Message-ID: > > So main.py contains: > > def get_field(value, start_bit, end_bit): > > > and I see: > > >>> import main > >>> help(get_field) > Traceback (most recent call last): > File "", line 1, in > NameError: name 'get_field' is not defined > > > help(main) works ok but is rather verbose. Try: import main help(main.get_field) From steve at pearwood.info Fri Jul 31 04:23:14 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 31 Jul 2015 12:23:14 +1000 Subject: [Tutor] 'open' is not defined In-Reply-To: <55baba60.46eb420a.2ce03.ffffadc9@mx.google.com> References: <55baba60.46eb420a.2ce03.ffffadc9@mx.google.com> Message-ID: <20150731022314.GU25179@ando.pearwood.info> On Thu, Jul 30, 2015 at 11:41:56PM +0000, ltc.hotspot at gmail.com wrote: > Hi Everyone: > > > Why is open not defined in the following code:NameError: name 'open' is not defined Are you still running your code on the PythonTutor website? http://pythontutor.com/visualize.html says in the fine-print at the bottom of the page: "Online Python Tutor currently supports five languages (despite its name!): 1. Python 2.7 and 3.3 with limited module imports and no file I/O." So open() is not defined because the Online Python Tutor website has removed it. By the way, I tried working with that website for a little while. It is *amazingly* slow and clunky and painful to use on my computer. I don't know how you can bear to use it. Have you considered installing Python on your computer and working directly on that? -- Steve From fiberfolly at gmail.com Fri Jul 31 02:58:27 2015 From: fiberfolly at gmail.com (D Wyatt) Date: Thu, 30 Jul 2015 17:58:27 -0700 Subject: [Tutor] a puzzle about -3**2 vs (-3)**2 Message-ID: I just read in a book a little while ago that ** trumps a negative sign? I am struggling with the audacity of that as -1 is negative 1, NOT minus 1. How can an arithmetic operation trump an attribute of a negative integer? It truly makes no sense to me. Thank you for any enlightenment you can provide. Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (In tel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> 3**2 9 >>> (-3)**2 9 >>> -3**2 -9 >>> -- Deb Wyatt in WA From ltc.hotspot at gmail.com Fri Jul 31 02:25:26 2015 From: ltc.hotspot at gmail.com (ltc.hotspot at gmail.com) Date: Fri, 31 Jul 2015 00:25:26 +0000 Subject: [Tutor] =?utf-8?q?String_Attribute?= In-Reply-To: <55babf6b.a582460a.d501f.ffffaf77@mx.google.com> References: <55b811cb.0566420a.6c354.4cc9@mx.google.com>, <20150729144211.GO25179@ando.pearwood.info>, <55ba7145.aa88420a.88128.7f18@mx.google.com>, <55ba761a.af80460a.8b634.ffff8861@mx.google.com> <55ba94ab.af80460a.8b634.ffff9a3f@mx.google.com>, , <55babf6b.a582460a.d501f.ffffaf77@mx.google.com> Message-ID: <55bac08d.882e460a.50a2.ffffaf6a@mx.google.com> Hi Alan, I rewrote the code as follows: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 for line in fh: if not line.startswith('From'): continue line2 = line.strip() line3 = line2.split() line4 = line3[1] print line4 count = count + 1 print "There were", count, "lines in the file with From as the first word" Question: How do I remove the duplicates: stephen.marquard at uct.ac.za stephen.marquard at uct.ac.za? Mismatch louis at media.berkeley.edu louis at media.berkeley.edu zqian at umich.edu zqian at umich.edu rjlowe at iupui.edu rjlowe at iupui.edu Regards, Hal Sent from Surface From: Alan Gauld Sent: ?Thursday?, ?July? ?30?, ?2015 ?5?:?04? ?PM To: Tutor at python.org On 30/07/15 22:17, ltc.hotspot at gmail.com wrote: > fname = raw_input("Enter file name: ") > if len(fname) < 1 : fname = "mbox-short.txt" # assign fname > fh=open(fname,'r') # Open a new file handle > for line in fh: > print line > if 'From' in line.split()[0] and '@' in line: sender = line.split()[1] > fn.seek(0) > print sender > > Questions: Why is the loop not repeating, What makes you think so? >>No count = count +1 If you get an error(as I suspect) please post the entire error message. >>OK I would expect a name error on the last line of the loop since there is no variable fn defined. I don't know what you think the seek() is doing, but (assuming you meant fh) it will reset the file to the first line each time so you never finish the loop. >>OK > and where should I insert a split to remove 'Sat Jan 5:09:14:16 2008' > > From stephen.marquard at uct.ac.za Sat Jan 5 09:14:16 2008 ? Mismatch Splitting on whitespace will ensure the bit you want is in the second element >>Check the revised code, above -- 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 ltc.hotspot at gmail.com Fri Jul 31 03:13:34 2015 From: ltc.hotspot at gmail.com (ltc.hotspot at gmail.com) Date: Fri, 31 Jul 2015 01:13:34 +0000 Subject: [Tutor] =?utf-8?q?=27open=27_is_not_defined?= In-Reply-To: References: <55baba60.46eb420a.2ce03.ffffadc9@mx.google.com>, Message-ID: <55bacc2e.ee59460a.4f815.ffffbe76@mx.google.com> Hi Emile, I hope this answers your question? Question: How do I remove each duplicate line output? Here is the raw data code: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 for line in fh: if not line.startswith('From'): continue line2 = line.strip() line3 = line2.split() line4 = line3[1] print line4 count = count + 1 print "There were", count, "lines in the file with From as the first word" The problem is in the output results: Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 16:44:52) [MSC v.1500 64 bit (AMD64)] Type "copyright", "credits" or "license" for more information. IPython 3.2.0 -- An enhanced Interactive Python. Anaconda is brought to you by Continuum Analytics. Please check out: http://continuum.io/thanks and https://anaconda.org ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: cd C:\Users\vm\Desktop\apps\docs\Python C:\Users\vm\Desktop\apps\docs\Python In [2]: %run _8_5_v_7.py Enter file name: mbox-short.txt stephen.marquard at uct.ac.za stephen.marquard at uct.ac.za louis at media.berkeley.edu louis at media.berkeley.edu zqian at umich.edu zqian at umich.edu rjlowe at iupui.edu rjlowe at iupui.edu zqian at umich.edu zqian at umich.edu rjlowe at iupui.edu rjlowe at iupui.edu cwen at iupui.edu cwen at iupui.edu cwen at iupui.edu cwen at iupui.edu gsilver at umich.edu gsilver at umich.edu gsilver at umich.edu gsilver at umich.edu zqian at umich.edu zqian at umich.edu gsilver at umich.edu gsilver at umich.edu wagnermr at iupui.edu wagnermr at iupui.edu zqian at umich.edu zqian at umich.edu antranig at caret.cam.ac.uk antranig at caret.cam.ac.uk gopal.ramasammycook at gmail.com gopal.ramasammycook at gmail.com david.horwitz at uct.ac.za david.horwitz at uct.ac.za david.horwitz at uct.ac.za david.horwitz at uct.ac.za david.horwitz at uct.ac.za david.horwitz at uct.ac.za david.horwitz at uct.ac.za david.horwitz at uct.ac.za stephen.marquard at uct.ac.za stephen.marquard at uct.ac.za louis at media.berkeley.edu louis at media.berkeley.edu louis at media.berkeley.edu louis at media.berkeley.edu ray at media.berkeley.edu ray at media.berkeley.edu cwen at iupui.edu cwen at iupui.edu cwen at iupui.edu cwen at iupui.edu cwen at iupui.edu cwen at iupui.edu There were 54 lines in the file with From as the first word In [3]: Regards, Hal From alan.gauld at btinternet.com Fri Jul 31 11:00:50 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 31 Jul 2015 10:00:50 +0100 Subject: [Tutor] String Attribute In-Reply-To: <55bac08d.882e460a.50a2.ffffaf6a@mx.google.com> References: <55b811cb.0566420a.6c354.4cc9@mx.google.com>, <20150729144211.GO25179@ando.pearwood.info>, <55ba7145.aa88420a.88128.7f18@mx.google.com>, <55ba761a.af80460a.8b634.ffff8861@mx.google.com> <55ba94ab.af80460a.8b634.ffff9a3f@mx.google.com>, , <55babf6b.a582460a.d501f.ffffaf77@mx.google.com> <55bac08d.882e460a.50a2.ffffaf6a@mx.google.com> Message-ID: On 31/07/15 01:25, ltc.hotspot at gmail.com wrote: > fname = raw_input("Enter file name: ") > if len(fname) < 1 : fname = "mbox-short.txt" > fh = open(fname) > count = 0 > for line in fh: > if not line.startswith('From'): continue > line2 = line.strip() > line3 = line2.split() > line4 = line3[1] > print line4 > count = count + 1 > print "There were", count, "lines in the file with From as the first word" > > Question: How do I remove the duplicates: OK, You now have the original code working, well done. To remove the duplicates you need to collect the addresses rather than printing them. Since you want the addresses to be unique you can use a set. You do that by first creating an empty set above the loop, let's call it addresses: addresses = set() Then replace your print statement with the set add() method: addresses.add(line4) This means that at the end of your loop you will have a set containing all of the unique addresses you found. You now print the set. You can do that directly or for more control over layout you can write another for loop that prints each address individually. print addresses or for address in addresses: print address # plus any formatting you want You can also sort the addresses by calling the sorted() function before printing: print sorted(addresses) 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 Fri Jul 31 11:18:14 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 31 Jul 2015 10:18:14 +0100 Subject: [Tutor] a puzzle about -3**2 vs (-3)**2 In-Reply-To: References: Message-ID: On 31/07/15 01:58, D Wyatt wrote: > I just read in a book a little while ago that ** trumps a negative > sign? I am struggling with the audacity of that as -1 is negative 1, > NOT minus 1. How can an arithmetic operation trump an attribute of a > negative integer? Because Python is a programming language with its own rules created by its designer. It doesn't need to follow the rules of math as you understand them. The decision may have been made because it made parsing constructs like this easier: 5-2 Is that 2 adjacent numbers 5 and -2? or is it an infix subtraction? Its easy for us to decide but harder for a computer reading the text. As the expressions get more complex there are more and more potentially ambiguous combinations so the language designer may decide to make the language more consistent by defining a set of precedence rules. In this case that ** is higher than -. He is quite within his rights to do that. It's his language after all. Some languages solve these problems by not permitting infix notation, so in Lisp for example (3 - 5) is illegal, you need to do (- 3 5) It looks odd to us but that's not the point, its how the language works. You learn to get used to it. Most languages have some idiosyncrasies like this. 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 toddrjen at gmail.com Fri Jul 31 11:07:31 2015 From: toddrjen at gmail.com (Todd) Date: Fri, 31 Jul 2015 11:07:31 +0200 Subject: [Tutor] a puzzle about -3**2 vs (-3)**2 In-Reply-To: References: Message-ID: On Fri, Jul 31, 2015 at 2:58 AM, D Wyatt wrote: > I just read in a book a little while ago that ** trumps a negative > sign? I am struggling with the audacity of that as -1 is negative 1, > NOT minus 1. How can an arithmetic operation trump an attribute of a > negative integer? It truly makes no sense to me. Thank you for any > enlightenment you can provide. > > Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 > bit (In > tel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. > >>> 3**2 > 9 > >>> (-3)**2 > 9 > >>> -3**2 > -9 > >>> > > It is a matter of operator precedence. Certain operators are carried out before others. See here: https://docs.python.org/3/reference/expressions.html#operator-precedence Negation has lower precedence than exponentiation. That means that the exponentiation is carried out first, and negation is carried out second. So "-3**2" is equivalent to "-(3**2)". This matches the precedence rules for written mathematics, where negation has a lower precedence than exponentiation as well. So python is doing the correct thing here mathematically. See, for example, http://mathforum.org/library/drmath/view/53194.html From ljmamoreira at gmail.com Fri Jul 31 11:55:19 2015 From: ljmamoreira at gmail.com (Jose Amoreira) Date: Fri, 31 Jul 2015 10:55:19 +0100 Subject: [Tutor] a puzzle about -3**2 vs (-3)**2 In-Reply-To: References: Message-ID: <55BB4607.80407@gmail.com> Hello! On 07/31/2015 01:58 AM, D Wyatt wrote: > I just read in a book a little while ago that ** trumps a negative > sign? I am struggling with the audacity of that as -1 is negative 1, > NOT minus 1. I'm not sure about what you mean by "trumps", but the square of negative one is positive one (negative times negative gives positive). > How can an arithmetic operation trump an attribute of a > negative integer? It truly makes no sense to me. Thank you for any > enlightenment you can provide. > > Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (In > tel)] on win32 > Type "help", "copyright", "credits" or "license" for more information. >>>> 3**2 > 9 >>>> (-3)**2 > 9 >>>> -3**2 > -9 >>>> > Given the precedence rules already mentioned by Alan and Todd, the results of the operations you showed us are exactly as expected. You'll get the same results if you try with a pocket calculator or using any other programming language or scientific package. Or MS-excel. HTH, Ze From turtle at 64.hu Fri Jul 31 12:04:09 2015 From: turtle at 64.hu (=?UTF-8?B?VsOhbGFzIFDDqXRlcg==?=) Date: Fri, 31 Jul 2015 12:04:09 +0200 Subject: [Tutor] a puzzle about -3**2 vs (-3)**2 In-Reply-To: References: Message-ID: 2015-07-31 2:58 GMT+02:00 D Wyatt : > > >>> 3**2 > 9 > >>> (-3)**2 > 9 > >>> -3**2 > -9 > >>> > > Try to get any other result for these operations in a primary school paper, and then have a look at your marks... From alan.gauld at btinternet.com Fri Jul 31 12:36:22 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 31 Jul 2015 11:36:22 +0100 Subject: [Tutor] a puzzle about -3**2 vs (-3)**2 In-Reply-To: <55BB4607.80407@gmail.com> References: <55BB4607.80407@gmail.com> Message-ID: On 31/07/15 10:55, Jose Amoreira wrote: > Given the precedence rules already mentioned by Alan and Todd, the > results of the operations you showed us are exactly as expected. You'll > get the same results if you try with a pocket calculator or using any > other programming language or scientific package. The point I was making is that you may NOT get the same results in any other language. Each language designer is free to define his/her own precedence rules. Most try to follow the rules of math, but some do not (like Lisp using prefix notation rather than infix). Several languages evaluate purely left to right, at least 1 goes from right to left. In programming you have to learn the language and not expect it to conform to any preconceived ideas of how it *should* work. -- 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 ljmamoreira at gmail.com Fri Jul 31 12:45:14 2015 From: ljmamoreira at gmail.com (Jose Amoreira) Date: Fri, 31 Jul 2015 11:45:14 +0100 Subject: [Tutor] a puzzle about -3**2 vs (-3)**2 In-Reply-To: References: <55BB4607.80407@gmail.com> Message-ID: <55BB51BA.8040405@gmail.com> On 07/31/2015 11:36 AM, Alan Gauld wrote: > On 31/07/15 10:55, Jose Amoreira wrote: > >> Given the precedence rules already mentioned by Alan and Todd, the >> results of the operations you showed us are exactly as expected. You'll >> get the same results if you try with a pocket calculator or using any >> other programming language or scientific package. > > The point I was making is that you may NOT get the same results in any > other language. Each language designer is free to define his/her own > precedence rules. Most try to follow the rules of math, but some do not > (like Lisp using prefix notation rather than infix). Several languages > evaluate purely left to right, at least 1 goes from right to left. > > In programming you have to learn the language and not expect it to > conform to any preconceived ideas of how it *should* work. > Yes, you are right, Alan. That expectation is often the source of much frustration when learning a new language. But, for this particular application, this particular language even conforms to those general preconceived ideas... And I'm glad it does! :) From steve at pearwood.info Fri Jul 31 14:12:51 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 31 Jul 2015 22:12:51 +1000 Subject: [Tutor] a puzzle about -3**2 vs (-3)**2 In-Reply-To: References: Message-ID: <20150731121250.GV25179@ando.pearwood.info> On Thu, Jul 30, 2015 at 05:58:27PM -0700, D Wyatt wrote: > I just read in a book a little while ago that ** trumps a negative > sign? Correct. Exponentiation has higher priority than subtraction, addition, multiplication and division: 2+3**2 => 11 not 25 10-3**2 => 1 not 49 2*3**2 => 18 not 36 100/5**2 => 4 not 400 As you have discovered, it also has higher priority than unary minus so that: -3**2 => -(3**2) => -9 NOT (-3)**2 => 9 Mathematically, this is perfectly acceptable, and what we would normally expect. In algebra, if we write: -x? we normally mean the negative of (x squared), not (negative x) squared, which would be just x?. So Python here agrees with standard mathematical notation. > I am struggling with the audacity of that as -1 is negative 1, > NOT minus 1. How can an arithmetic operation trump an attribute of a > negative integer? It truly makes no sense to me. Thank you for any > enlightenment you can provide. Speaking as a maths tutor with about 20 years experience, and a B.Sc. with a major in mathematics, I'm not sure I understand what you are getting at. There is no mathematical difference between the inherent negativeness of -1 and the arithmetic operation - 1 (unary minus operator followed by 1). Whichever way you treat it, we have to agree what it means. For example, 2x means 2 multiplied by x; but 23 doesn't mean 2 multiplied by 3. It could if we wanted it to, but that would be inconvenient. Mathematicians could define -3? as (-3)? = 9 if they wanted to, but generally they don't, although there are exceptions. Consequently such expressions are ambiguous and are best avoided. Although -x? never means -x squared, it always means minus (x squared). Python removes the ambiguity and has exponentiation always take priority over other arithmetic operators, regardless of whether you consider - a unary or binary operator, or an inherent part of the integer. Although, for the record, the standard Python interpreter does NOT parse an expression like -123 as "negative 123", but as "unary minus 123". In practice this makes zero difference to the code. -- Steve From acdupont at mtu.edu Fri Jul 31 17:13:48 2015 From: acdupont at mtu.edu (Anthony DuPont) Date: Fri, 31 Jul 2015 11:13:48 -0400 Subject: [Tutor] Python application with same name as its corresponding project Message-ID: I am trying to setup my python application in a more standard way. In my research, the general recommendation seems to be that if my application is called projectgendocs, here is an acceptable structure: ProjectParent |-- bin/ | |-- projectgendocs.py | |-- projectgendocs | |-- unittest | | |-- __init__.py | | |-- test_main.py | | | |-- __init__.py | |-- main.py | |-- setup.py |-- README It is my understanding that ProjectParent would be added to the PYTHONPATH so the projectgendocs project could be discovered for importing. The problem I am encountering is that when the bin/projectgendocs.py is run, it breaks the imports in the ProjectParent/projectgendocs files that have import projectgendocs.somefile.py I discovered this is due to the fact that the bin/projectgendocs is discovered in the search for a package called projectgendocs because the bin directory is added to the search path. I verified this by renaming the bin/projectgendocs.py file to something different. So, my question is this: How can a python application (the file installed in the bin folder) have the same name as its corresponding python project and not break the absolute imports? Or is the only way to do this is by using relative imports? From ltc.hotspot at gmail.com Fri Jul 31 16:39:46 2015 From: ltc.hotspot at gmail.com (ltc.hotspot at gmail.com) Date: Fri, 31 Jul 2015 14:39:46 +0000 Subject: [Tutor] =?utf-8?q?String_Attribute?= In-Reply-To: <55bb87da.440d460a.7735.4f29@mx.google.com> References: <55b811cb.0566420a.6c354.4cc9@mx.google.com>, <20150729144211.GO25179@ando.pearwood.info>, <55ba7145.aa88420a.88128.7f18@mx.google.com>, <55ba761a.af80460a.8b634.ffff8861@mx.google.com> <55ba94ab.af80460a.8b634.ffff9a3f@mx.google.com>, , <55babf6b.a582460a.d501f.ffffaf77@mx.google.com> <55bac08d.882e460a.50a2.ffffaf6a@mx.google.com>, , <55bb87da.440d460a.7735.4f29@mx.google.com> Message-ID: <55bb88e2.6285460a.a1971.5a55@mx.google.com> Hi Alan, Here is the revised code below: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 for line in fh: if not line.startswith('From'): continue line2 = line.strip() line3 = line2.split() line4 = line3[1] addresses = set() addresses.add(line4) count = count + 1 print addresses print "There were", count, "lines in the file with From as the first word" The code produces the following out put: In [15]: %run _8_5_v_13.py Enter file name: mbox-short.txt set(['stephen.marquard at uct.ac.za']) set(['stephen.marquard at uct.ac.za']) set(['louis at media.berkeley.edu']) set(['louis at media.berkeley.edu']) set(['zqian at umich.edu']) set(['zqian at umich.edu']) set(['rjlowe at iupui.edu']) set(['rjlowe at iupui.edu']) set(['zqian at umich.edu']) set(['zqian at umich.edu']) set(['rjlowe at iupui.edu']) set(['rjlowe at iupui.edu']) set(['cwen at iupui.edu']) set(['cwen at iupui.edu']) set(['cwen at iupui.edu']) set(['cwen at iupui.edu']) set(['gsilver at umich.edu']) set(['gsilver at umich.edu']) set(['gsilver at umich.edu']) set(['gsilver at umich.edu']) set(['zqian at umich.edu']) set(['zqian at umich.edu']) set(['gsilver at umich.edu']) set(['gsilver at umich.edu']) set(['wagnermr at iupui.edu']) set(['wagnermr at iupui.edu']) set(['zqian at umich.edu']) set(['zqian at umich.edu']) set(['antranig at caret.cam.ac.uk']) set(['antranig at caret.cam.ac.uk']) set(['gopal.ramasammycook at gmail.com']) set(['gopal.ramasammycook at gmail.com']) set(['david.horwitz at uct.ac.za']) set(['david.horwitz at uct.ac.za']) set(['david.horwitz at uct.ac.za']) set(['david.horwitz at uct.ac.za']) set(['david.horwitz at uct.ac.za']) set(['david.horwitz at uct.ac.za']) set(['david.horwitz at uct.ac.za']) set(['david.horwitz at uct.ac.za']) set(['stephen.marquard at uct.ac.za']) set(['stephen.marquard at uct.ac.za']) set(['louis at media.berkeley.edu']) set(['louis at media.berkeley.edu']) set(['louis at media.berkeley.edu']) set(['louis at media.berkeley.edu']) set(['ray at media.berkeley.edu']) set(['ray at media.berkeley.edu']) set(['cwen at iupui.edu']) set(['cwen at iupui.edu']) set(['cwen at iupui.edu']) set(['cwen at iupui.edu']) set(['cwen at iupui.edu']) set(['cwen at iupui.edu']) There were 54 lines in the file with From as the first word Question no. 1: is there a build in function for set that parses the data for duplicates. In [18]: dir (set) Out[18]: ['__and__', '__class__', '__cmp__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update'] Question no. 2: Why is there not a building function for append? Question no. 3: If all else fails, i.e., append & set, my only option is the slice the data set? Regards, Hal Sent from Surface From: Alan Gauld Sent: ?Friday?, ?July? ?31?, ?2015 ?2?:?00? ?AM To: Tutor at python.org On 31/07/15 01:25, ltc.hotspot at gmail.com wrote: > fname = raw_input("Enter file name: ") > if len(fname) < 1 : fname = "mbox-short.txt" > fh = open(fname) > count = 0 > for line in fh: > if not line.startswith('From'): continue > line2 = line.strip() > line3 = line2.split() > line4 = line3[1] > print line4 > count = count + 1 > print "There were", count, "lines in the file with From as the first word" > > Question: How do I remove the duplicates: OK, You now have the original code working, well done. To remove the duplicates you need to collect the addresses rather than printing them. Since you want the addresses to be unique you can use a set. You do that by first creating an empty set above the loop, let's call it addresses: addresses = set() Then replace your print statement with the set add() method: addresses.add(line4) This means that at the end of your loop you will have a set containing all of the unique addresses you found. You now print the set. You can do that directly or for more control over layout you can write another for loop that prints each address individually. print addresses or for address in addresses: print address # plus any formatting you want You can also sort the addresses by calling the sorted() function before printing: print sorted(addresses) 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 _______________________________________________ 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 Fri Jul 31 18:08:49 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 31 Jul 2015 17:08:49 +0100 Subject: [Tutor] String Attribute In-Reply-To: <55bb88e2.6285460a.a1971.5a55@mx.google.com> References: <55b811cb.0566420a.6c354.4cc9@mx.google.com>, <20150729144211.GO25179@ando.pearwood.info>, <55ba7145.aa88420a.88128.7f18@mx.google.com>, <55ba761a.af80460a.8b634.ffff8861@mx.google.com> <55ba94ab.af80460a.8b634.ffff9a3f@mx.google.com>, , <55babf6b.a582460a.d501f.ffffaf77@mx.google.com> <55bac08d.882e460a.50a2.ffffaf6a@mx.google.com>, , <55bb87da.440d460a.7735.4f29@mx.google.com> <55bb88e2.6285460a.a1971.5a55@mx.google.com> Message-ID: On 31/07/15 15:39, ltc.hotspot at gmail.com wrote: > fname = raw_input("Enter file name: ") > if len(fname) < 1 : fname = "mbox-short.txt" > fh = open(fname) > count = 0 > for line in fh: > if not line.startswith('From'): continue > line2 = line.strip() > line3 = line2.split() > line4 = line3[1] > addresses = set() Notice I said you had to create and initialize the set *above* the loop. Here you are creating a new set every time round the loop and throwing away the old one. > addresses.add(line4) > count = count + 1 > print addresses And notice I said to move the print statement to *after* the loop so as to print the complete set, not just the current status. > print "There were", count, "lines in the file with From as the first word" > > The code produces the following out put: > > In [15]: %run _8_5_v_13.py > Enter file name: mbox-short.txt > set(['stephen.marquard at uct.ac.za']) > set(['stephen.marquard at uct.ac.za']) > set(['louis at media.berkeley.edu']) Thats correct because you create a new set each time and add precisely one element to it before throwing it away and starting over next time round. > Question no. 1: is there a build in function for set that parses the data for duplicates. No because thats what a set does. it is a collection of unique items. It will not allow duplicates. Your problem is you create a new set of one item for every line. So you have multiple sets with the same data in them. > Question no. 2: Why is there not a building function for append? add() is the equivalent of append for a set. If you try to add() a value that already exists it will be ignored. > Question no. 3: If all else fails, i.e., append & set, my only option is the slice the data set? No there are lots of other options but none of them are necessary because a set is a collection of unique values. You just need to use it properly. Read my instructions again, carefully: > You do that by first creating an empty set above > the loop, let's call it addresses: > > addresses = set() > > Then replace your print statement with the set add() > method: > > addresses.add(line4) > > This means that at the end of your loop you will have > a set containing all of the unique addresses you found. > You now print the set. -- 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 martin at linux-ip.net Fri Jul 31 18:18:26 2015 From: martin at linux-ip.net (Martin A. Brown) Date: Fri, 31 Jul 2015 09:18:26 -0700 Subject: [Tutor] String Attribute In-Reply-To: <55bb88e2.6285460a.a1971.5a55@mx.google.com> References: <55b811cb.0566420a.6c354.4cc9@mx.google.com>, <20150729144211.GO25179@ando.pearwood.info>, <55ba7145.aa88420a.88128.7f18@mx.google.com>, <55ba761a.af80460a.8b634.ffff8861@mx.google.com> <55ba94ab.af80460a.8b634.ffff9a3f@mx.google.com>, , <55babf6b.a582460a.d501f.ffffaf77@mx.google.com> <55bac08d.882e460a.50a2.ffffaf6a@mx.google.com>, , <55bb87da.440d460a.7735.4f29@mx.google.com> <55bb88e2.6285460a.a1971.5a55@mx.google.com> Message-ID: Greetings again Hal, Thank you for posting your small amounts of code and results inline. Thanks for also including clear questions. Your "surface" still seems to add extra space, so, if you could trim that, you may get even more responses from others who are on the Tutor mailing list. Now, on to your question. > fname = raw_input("Enter file name: ") > if len(fname) < 1 : fname = "mbox-short.txt" > fh = open(fname) > count = 0 > for line in fh: > if not line.startswith('From'): continue > line2 = line.strip() > line3 = line2.split() > line4 = line3[1] > addresses = set() > addresses.add(line4) > count = count + 1 > print addresses > print "There were", count, "lines in the file with From as the first word" > The code produces the following out put: > > In [15]: %run _8_5_v_13.py > Enter file name: mbox-short.txt > set(['stephen.marquard at uct.ac.za']) [ ... snip ... ] > set(['cwen at iupui.edu']) > > Question no. 1: is there a build in function for set that parses > the data for duplicates. The problem is not with the data structure called set(). Your program is not bad at all. I would suggest making two small changes to it. I think I have seen a pattern in the samples of code you have been sending--this pattern is that you reuse the same variable inside a loop, and do not understand why you are not collecting (or accumulating) all of the results. Here's your program. I have moved two lines. The idea here is to initialize the 'addresses' variable before the loop begins (exactly like you do with the 'count' variable). Then, after the loop completes (and, you have processed all of your input and accumulated all of the desired data), you can also print out the contents of the set variable called 'addresses'. Try this out: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 addresses = set() for line in fh: if not line.startswith('From'): continue line2 = line.strip() line3 = line2.split() line4 = line3[1] addresses.add(line4) count = count + 1 print "There were", count, "lines in the file with From as the first word" print addresses > Question no. 2: Why is there not a building function for append? > Question no. 3: If all else fails, i.e., append & set, my only > option is the slice the data set? I do not understand these two questions. Good luck. -Martin P.S. By the way, Alan Gauld has also responded to your message, with a differently-phrased answer, but, fundamentally, he and I are saying the same thing. Think about where you are initializing your variables, and know that 'addresses = set()' in the middle of the code is re-initializing the variable and throwing away anything that was there before.. -- Martin A. Brown http://linux-ip.net/ From turtle at 64.hu Fri Jul 31 20:10:06 2015 From: turtle at 64.hu (=?UTF-8?B?VsOhbGFzIFDDqXRlcg==?=) Date: Fri, 31 Jul 2015 20:10:06 +0200 Subject: [Tutor] a puzzle about -3**2 vs (-3)**2 In-Reply-To: References: Message-ID: 2015-07-31 19:23 GMT+02:00 D Wyatt : > > Try to get any other result for these operations in a primary school > paper, > > and then have a look at your marks... > > > > Condescending and unhelpful response. Why did you bother? > > I tell you another way. These ARE the arithmetically correct results. Why do you expect Python give wrong ones? From kwpolska at gmail.com Fri Jul 31 20:16:58 2015 From: kwpolska at gmail.com (Chris Warrick) Date: Fri, 31 Jul 2015 20:16:58 +0200 Subject: [Tutor] Python application with same name as its corresponding project In-Reply-To: References: Message-ID: On 31 July 2015 at 17:13, Anthony DuPont wrote: > I am trying to setup my python application in a more standard way. In my > research, the general recommendation seems to be that if my application is > called projectgendocs, here is an acceptable structure: > > ProjectParent > |-- bin/ > | |-- projectgendocs.py > | > |-- projectgendocs > | |-- unittest > | | |-- __init__.py > | | |-- test_main.py > | | > | |-- __init__.py > | |-- main.py > | > |-- setup.py > |-- README > > > It is my understanding that ProjectParent would be added to the PYTHONPATH > so the projectgendocs project could be discovered for importing. The > problem I am encountering is that when the bin/projectgendocs.py is run, it > breaks the imports in the ProjectParent/projectgendocs files that have > > import projectgendocs.somefile.py > > I discovered this is due to the fact that the bin/projectgendocs is > discovered in the search for a package called projectgendocs because the > bin directory is added to the search path. I verified this by renaming the > bin/projectgendocs.py file to something different. So, my question is this: > How can a python application (the file installed in the bin folder) have > the same name as its corresponding python project and not break the > absolute imports? Or is the only way to do this is by using relative > imports? > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor Don?t create custom bin/ scripts, use setuptools entry points. I described it on my blog: https://chriswarrick.com/blog/2014/09/15/python-apps-the-right-way-entry_points-and-scripts/ -- Chris Warrick PGP: 5EAAEA16 From alan.gauld at btinternet.com Fri Jul 31 20:51:37 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 31 Jul 2015 19:51:37 +0100 Subject: [Tutor] a puzzle about -3**2 vs (-3)**2 In-Reply-To: References: Message-ID: <55BBC3B9.5090008@btinternet.com> On 31/07/15 18:08, D Wyatt wrote: > > It looks odd to us but that's not the point, its how the language works. > > You learn to get used to it. Most languages have some idiosyncrasies like > > this. > > Yes, I understand that the creator of the language can make it work > however he wants, but I was really hoping for a logical answer. Just > because 'that's the way it is' kind of sucks and will make it more > difficult to remember. Many languages do their own thing because it makes the compiler go faster, or makes the code produced more reliable/consistent. That's often more important to a language designer than making it intuitive to the reader. In fact, some languages deliberately use a different set of rules/notation because the language designer believes that his/her way is superior to the traditional notation and uses the language to test those ideas. Now, as others have pointed out, Python does in fact follow traditional math in most things, so is usually intuitively correct, but you should never assume that of any programming language. The designers are often reaching for different targets than the eventual user. And very often we the users don't know what the designer's aims or priorities were. (For example Forth was designed to fit into the very small amount of memory left over on an astronomical telescope control system, so is very, very terse, and uses many "illogical" code layouts. Everything was sacrificed to save space.) Of course as users we get to decide whether the designers choices are acceptable to us or whether we will adopt another language. Many experimental languages full of good, powerful programming ideas never made it into common use precisely because they chose some weird syntax or layout convention that was just too far out for programmers to accept. -- 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 Fri Jul 31 20:54:52 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 1 Aug 2015 04:54:52 +1000 Subject: [Tutor] a puzzle about -3**2 vs (-3)**2 In-Reply-To: References: <20150731121250.GV25179@ando.pearwood.info> Message-ID: <20150731185452.GY25179@ando.pearwood.info> Hi Deb, On Fri, Jul 31, 2015 at 10:48:57AM -0700, D Wyatt wrote: > I have never really thought about any of this before, but many of you > have responded like this is so obvious. That is not helpful. I'm > looking at a negative number as being an object that is one less than > zero, and the unary sign being a part of that object, glued to it. > Why is that so hard to understand? Like many things, the behaviour here is obvious in hindsight. And you are right, sometimes people forget that hindsight is 20:20 but foresight is not. If I said anything that gave you the impression that your question was a dumb question, sorry, that wasn't my intention. In this case, your expectation that -3**2 should be treated as -3 as a single value, raised to the power of 2, is a perfectly reasonable expectation. I daresay that there are programming languages where that actually is the case, and if you search the Internet, you'll find lots of people making the same assumption as you. Regards, Steve From fiberfolly at gmail.com Fri Jul 31 19:08:52 2015 From: fiberfolly at gmail.com (D Wyatt) Date: Fri, 31 Jul 2015 10:08:52 -0700 Subject: [Tutor] a puzzle about -3**2 vs (-3)**2 In-Reply-To: References: Message-ID: > > He is quite within his rights to do that. It's his language after all. > Some languages solve these problems by not permitting infix notation, > so in Lisp for example > > (3 - 5) > > is illegal, you need to do > > (- 3 5) > > It looks odd to us but that's not the point, its how the language works. > You learn to get used to it. Most languages have some idiosyncrasies like > this. > > 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 > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor Yes, I understand that the creator of the language can make it work however he wants, but I was really hoping for a logical answer. Just because 'that's the way it is' kind of sucks and will make it more difficult to remember. -- Deb Wyatt in WA From fiberfolly at gmail.com Fri Jul 31 19:20:50 2015 From: fiberfolly at gmail.com (D Wyatt) Date: Fri, 31 Jul 2015 10:20:50 -0700 Subject: [Tutor] a puzzle about -3**2 vs (-3)**2 In-Reply-To: References: Message-ID: > > This matches the precedence rules for written mathematics, where negation > has a lower precedence than exponentiation as well. So python is doing the > correct thing here mathematically. See, for example, > http://mathforum.org/library/drmath/view/53194.html > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor That is just so counterintuitive, and I've never run into this in any mathematics I have taken. Now I'm going to have to research this further, from a mathematics standpoint. -- Deb Wyatt in WA From fiberfolly at gmail.com Fri Jul 31 19:48:57 2015 From: fiberfolly at gmail.com (D Wyatt) Date: Fri, 31 Jul 2015 10:48:57 -0700 Subject: [Tutor] a puzzle about -3**2 vs (-3)**2 In-Reply-To: <20150731121250.GV25179@ando.pearwood.info> References: <20150731121250.GV25179@ando.pearwood.info> Message-ID: > > Mathematically, this is perfectly acceptable, and what we would > normally expect. In algebra, if we write: > > -x? > > we normally mean the negative of (x squared), not (negative x) squared, > which would be just x?. So Python here agrees with standard mathematical > notation. > > > Speaking as a maths tutor with about 20 years experience, and a B.Sc. > with a major in mathematics, I'm not sure I understand what you are > getting at. There is no mathematical difference between the inherent > negativeness of -1 and the arithmetic operation - 1 (unary minus > operator followed by 1). > > Whichever way you treat it, we have to agree what it means. For example, > 2x means 2 multiplied by x; but 23 doesn't mean 2 multiplied by 3. It > could if we wanted it to, but that would be inconvenient. Mathematicians > could define -3? as (-3)? = 9 if they wanted to, but generally they > don't, although there are exceptions. Consequently such expressions are > ambiguous and are best avoided. Although -x? never means -x squared, > it always means minus (x squared). > > > -- > Steve I have never really thought about any of this before, but many of you have responded like this is so obvious. That is not helpful. I'm looking at a negative number as being an object that is one less than zero, and the unary sign being a part of that object, glued to it. Why is that so hard to understand? So, mathematically, it works the way python works. I never realized that, though I've always been good at math. It's just not something that has ever explicitly come up anywhere until I read it in the Python book. The whole purpose of this email group is to share knowledge and help each other, but when you forget that what seems obvious to you is not obvious to everybody, you lose the ability to be as helpful as you could be. I'm sorry my question turned out to be more about math than Python. I had no idea. This semi-rant is NOT directed at you, personally, Steve. I would like to thank almost everybody for their responses. I've learned a few things. But I'll keep asking questions anyway, even if some of your responses imply they're stupid questions. The only stupid question is the question you don't ask. -- Deb Wyatt in WA From japhy at pearachute.com Fri Jul 31 20:28:30 2015 From: japhy at pearachute.com (Japhy Bartlett) Date: Fri, 31 Jul 2015 13:28:30 -0500 Subject: [Tutor] Python application with same name as its corresponding project In-Reply-To: References: Message-ID: Like Chris mentions, usually you don't write your own stuff in /bin/. To make what you've written work anyhow, you can run them from inside /ProjectParent/, not from inside /ProjectParent/bin/. eg, `python bin/projectgendocs.py` On Fri, Jul 31, 2015 at 1:16 PM, Chris Warrick wrote: > On 31 July 2015 at 17:13, Anthony DuPont wrote: > > I am trying to setup my python application in a more standard way. In my > > research, the general recommendation seems to be that if my application > is > > called projectgendocs, here is an acceptable structure: > > > > ProjectParent > > |-- bin/ > > | |-- projectgendocs.py > > | > > |-- projectgendocs > > | |-- unittest > > | | |-- __init__.py > > | | |-- test_main.py > > | | > > | |-- __init__.py > > | |-- main.py > > | > > |-- setup.py > > |-- README > > > > > > It is my understanding that ProjectParent would be added to the > PYTHONPATH > > so the projectgendocs project could be discovered for importing. The > > problem I am encountering is that when the bin/projectgendocs.py is run, > it > > breaks the imports in the ProjectParent/projectgendocs files that have > > > > import projectgendocs.somefile.py > > > > I discovered this is due to the fact that the bin/projectgendocs is > > discovered in the search for a package called projectgendocs because the > > bin directory is added to the search path. I verified this by renaming > the > > bin/projectgendocs.py file to something different. So, my question is > this: > > How can a python application (the file installed in the bin folder) have > > the same name as its corresponding python project and not break the > > absolute imports? Or is the only way to do this is by using relative > > imports? > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > > Don?t create custom bin/ scripts, use setuptools entry points. I > described it on my blog: > > > https://chriswarrick.com/blog/2014/09/15/python-apps-the-right-way-entry_points-and-scripts/ > > -- > Chris Warrick > PGP: 5EAAEA16 > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From turtle at 64.hu Fri Jul 31 21:22:33 2015 From: turtle at 64.hu (=?UTF-8?B?VsOhbGFzIFDDqXRlcg==?=) Date: Fri, 31 Jul 2015 21:22:33 +0200 Subject: [Tutor] a puzzle about -3**2 vs (-3)**2 In-Reply-To: <55BBC3B9.5090008@btinternet.com> References: <55BBC3B9.5090008@btinternet.com> Message-ID: 2015-07-31 20:51 GMT+02:00 Alan Gauld : > > (For example Forth was designed to fit > into the very small amount of memory left over on an astronomical telescope > control system, so is very, very terse, and uses many "illogical" code > layouts. > Everything was sacrificed to save space.) > Thank you for this interesting story, I never knew this. You mentioned such interesting languages as Lisp and Forth. While you are right that operator precedence may vary from language to language (just a few weeks ago PHP schocked me with the different precedence of and/or and "C-style" &&/|| and it took me quite a time to find the error), I think we may say that all the mainstream languages of present times leave the precedence of ARITHMETICAL operators intact among each other. And this follows the rules of elementary math. While thinking that minus is glued to the following number has its own logic and is not hard to understand this logic, it's not the rule of mathematics. That's why math has its worldwide accepted rules, and that's why these are taught in schools whereever we go on the globe. These rules are also based on logics, of course. And because prorgramming is a branch of applied mathematics, it is really-really worth to keep the rules of math -- I think where they are broken, it is an exception and not the normal workflow. While all we should read the famous manual concerning the precedence when we learn a new language, we may trust that * will always preceed +, ** will preceed any other arithmetical operator, and AND will preceed OR -- unless we deal with some special/obscure/historical language. Python is not an exception. From ltc.hotspot at gmail.com Fri Jul 31 20:57:56 2015 From: ltc.hotspot at gmail.com (ltc.hotspot at gmail.com) Date: Fri, 31 Jul 2015 18:57:56 +0000 Subject: [Tutor] =?utf-8?q?String_Attribute?= In-Reply-To: References: <55b811cb.0566420a.6c354.4cc9@mx.google.com>, <20150729144211.GO25179@ando.pearwood.info>, <55ba7145.aa88420a.88128.7f18@mx.google.com>, <55ba761a.af80460a.8b634.ffff8861@mx.google.com> <55ba94ab.af80460a.8b634.ffff9a3f@mx.google.com>, , <55babf6b.a582460a.d501f.ffffaf77@mx.google.com> <55bac08d.882e460a.50a2.ffffaf6a@mx.google.com>, , <55bb87da.440d460a.7735.4f29@mx.google.com> <55bb88e2.6285460a.a1971.5a55@mx.google.com>, Message-ID: <55bbcd29.e236460a.2902.7959@mx.google.com> Hi Martin, Hal is not have a great day, indeed to day: Here is the raw data entered: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 addresses = set() for line in fh: line2 = line.strip() line3 = line2.split() line4 = line3[0] addresses.add(line4) count = count + 1 print "There were", count, "lines in the file with From as the first word" print addresses ?Question: Why is the list index out of range on line # 9: IndexError Traceback (most recent call last) C:\Users\vm\Desktop\apps\docs\Python\assinment_8_5_v_20.py in () 7 line2 = line.strip() 8 line3 = line2.split() ----> 9 line4 = line3[1] 10 addresses.add(line4) 11 count = count + 1 IndexError: list index out of range ? ?I entered different index ranges from [] to [5] that, later, produced the same Index Error message: IndexError: list index out of range In [34]: print line3[] File "", line 1 print line3[] ^ SyntaxError: invalid syntax In [35]: print line[1] --------------------------------------------------------------------------- IndexError Traceback (most recent call last) in () ----> 1 print line[1] IndexError: string index out of range In [36]: print line[2] --------------------------------------------------------------------------- IndexError Traceback (most recent call last) in () ----> 1 print line[2] IndexError: string index out of range In [37]: print line[3] --------------------------------------------------------------------------- IndexError Traceback (most recent call last) in () ----> 1 print line[3] IndexError: string index out of range In [38]: print line[4] --------------------------------------------------------------------------- IndexError Traceback (most recent call last) in () ----> 1 print line[4] IndexError: string index out of range In [39]: print line[5] --------------------------------------------------------------------------- IndexError Traceback (most recent call last) in () ----> 1 print line[5] IndexError: string index out of range ?Question: I think the problem is in the placement of the address set: The addresses = set()? Regards, Hal Sent from Surface From: Martin A. Brown Sent: ?Friday?, ?July? ?31?, ?2015 ?9?:?18? ?AM To: ltc.hotspot at gmail.com Cc: Tutor at python.org Greetings again Hal, Thank you for posting your small amounts of code and results inline. Thanks for also including clear questions. Your "surface" still seems to add extra space, so, if you could trim that, you may get even more responses from others who are on the Tutor mailing list. Now, on to your question. > fname = raw_input("Enter file name: ") > if len(fname) < 1 : fname = "mbox-short.txt" > fh = open(fname) > count = 0 > for line in fh: > if not line.startswith('From'): continue > line2 = line.strip() > line3 = line2.split() > line4 = line3[1] > addresses = set() > addresses.add(line4) > count = count + 1 > print addresses > print "There were", count, "lines in the file with From as the first word" > The code produces the following out put: > > In [15]: %run _8_5_v_13.py > Enter file name: mbox-short.txt > set(['stephen.marquard at uct.ac.za']) [ ... snip ... ] > set(['cwen at iupui.edu']) > > Question no. 1: is there a build in function for set that parses > the data for duplicates. The problem is not with the data structure called set(). Your program is not bad at all. I would suggest making two small changes to it. I think I have seen a pattern in the samples of code you have been sending--this pattern is that you reuse the same variable inside a loop, and do not understand why you are not collecting (or accumulating) all of the results. Here's your program. I have moved two lines. The idea here is to initialize the 'addresses' variable before the loop begins (exactly like you do with the 'count' variable). Then, after the loop completes (and, you have processed all of your input and accumulated all of the desired data), you can also print out the contents of the set variable called 'addresses'. Try this out: fname = raw_input("Enter file name: ") if len(fname) < 1 : fname = "mbox-short.txt" fh = open(fname) count = 0 addresses = set() for line in fh: if not line.startswith('From'): continue line2 = line.strip() line3 = line2.split() line4 = line3[1] addresses.add(line4) count = count + 1 print "There were", count, "lines in the file with From as the first word" print addresses > Question no. 2: Why is there not a building function for append? >> Alan answered the question, thanks > Question no. 3: If all else fails, i.e., append & set, my only > option is the slice the data set? I do not understand these two questions. >> Alan answered the question thanks Good luck. -Martin P.S. By the way, Alan Gauld has also responded to your message, with a differently-phrased answer, but, fundamentally, he and I are saying the same thing. Think about where you are initializing your variables, and know that 'addresses = set()' in the middle of the code is re-initializing the variable and throwing away anything that was there before.. -- Martin A. Brown http://linux-ip.net/