From livingmike at gmail.com Thu Aug 1 00:01:27 2013 From: livingmike at gmail.com (Mike McTernan) Date: Wed, 31 Jul 2013 15:01:27 -0700 Subject: [Tutor] readline In-Reply-To: References: Message-ID: Thanks everyone. For some reason I thought that readline counted the \n at the end of each line and stopped at each one. Clearly, I was mistaken! mike On Wed, Jul 31, 2013 at 2:05 PM, Alan Gauld wrote: > On 31/07/13 21:12, Mike McTernan wrote: >> >> I am having problems with the readline command in Python 2.7. > > >> script, zodiac = argv >> prediction = open(zodiac, "r") > > This code is very fragile, you should check that zodiac is actually being > set by the user and ideally that it is a real file before using it. > Alternatively use a try/except block to catch any error. The latter is > probably the most Pythonic approach. > > > if "Leo" in star_sign: > print prediction.readline(5) > > >> instead of printing the line 5 it prints the first 5 letters on line 1. > > > What made you think it would read line 5? The help clearly says: > > readline(...) > readline([size]) -> next line from the file, as a string. > > Retain newline. A non-negative size argument limits the maximum > number of bytes to return (an incomplete line may be returned then). > Return an empty string at EOF. > (END) > > So the '5' limits the number of *bytes* read not the lines. > > The program is working exactly as you should expect. > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- http://mikejmct.blogspot.com/ From alan.gauld at btinternet.com Thu Aug 1 01:20:50 2013 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Thu, 1 Aug 2013 00:20:50 +0100 (BST) Subject: [Tutor] readline In-Reply-To: References: Message-ID: <1375312850.9068.YahooMailNeo@web186005.mail.ir2.yahoo.com> > For some reason I thought that readline counted the \n at the end of > each line and stopped at each one. Clearly, I was mistaken! That is kind of what the default behaviour does. It doesn't? actually?count the \n characters but it reads up to and? including it. Alan G. From chigga101 at gmail.com Thu Aug 1 14:11:05 2013 From: chigga101 at gmail.com (Matthew Ngaha) Date: Thu, 1 Aug 2013 13:11:05 +0100 Subject: [Tutor] unittest skipping tests Message-ID: im trying to do some unittesting and i've written 1 class with 4 test methods and it skips 2 of these methods for no reason. If i comment out the 2 it doesn't skip, it will now test the 2 it previously skipped. Is this the default behaviour to only test 2 methods in a class? Anyone have any ideas how i can solve this problem? Thanks. I'm using Python 3 From alan.gauld at btinternet.com Thu Aug 1 14:19:18 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 01 Aug 2013 13:19:18 +0100 Subject: [Tutor] unittest skipping tests In-Reply-To: References: Message-ID: On 01/08/13 13:11, Matthew Ngaha wrote: > im trying to do some unittesting and i've written 1 class with 4 test > methods and it skips 2 of these methods for no reason. If i comment > out the 2 it doesn't skip, it will now test the 2 it previously > skipped. Is this the default behaviour I'm pretty sure its not the default but it might help if you posted the code and a cut n' paste of your test session. That way we can see what framework you use, how you use it and what actually happens. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From wprins at gmail.com Thu Aug 1 14:44:42 2013 From: wprins at gmail.com (Walter Prins) Date: Thu, 1 Aug 2013 13:44:42 +0100 Subject: [Tutor] unittest skipping tests In-Reply-To: References: Message-ID: Hi, On 1 August 2013 13:11, Matthew Ngaha wrote: > im trying to do some unittesting and i've written 1 class with 4 test > methods and it skips 2 of these methods for no reason. If i comment > out the 2 it doesn't skip, it will now test the 2 it previously > skipped. Is this the default behaviour to only test 2 methods in a > class? No. (That would be silly, nay, crazy even.) > Anyone have any ideas how i can solve this problem? Alan's answer is the first port of call. You really need to give us more to work with, otherwise we're just guessing, and if we have to guess it makes our lives and your life more difficult than it needs to be. That said, what you describe makes me suspect indentation problems of some sort. It sounds almost as if the latter test methods are somehow considered to be inside the former test code, such that they're not "seen" by the testing framework's test discovery process. And, when you then comment out the prior 2 methods, the latter 2 become "visible" due to not being in the former's enclosing scope. Or something like that. But, that's just a guess, and may be way off base... Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From chigga101 at gmail.com Thu Aug 1 15:22:19 2013 From: chigga101 at gmail.com (Matthew Ngaha) Date: Thu, 1 Aug 2013 14:22:19 +0100 Subject: [Tutor] unittest skipping tests In-Reply-To: References: Message-ID: Thanks guys. i was following some examples and noticed it. I tried to use it in my program and the same thing happened. In this example, there are 6 tests, and only 4 run. http://bpaste.net/show/abLNTHU49w1j2M8Fey8X/ From wprins at gmail.com Thu Aug 1 15:42:24 2013 From: wprins at gmail.com (Walter Prins) Date: Thu, 1 Aug 2013 14:42:24 +0100 Subject: [Tutor] unittest skipping tests In-Reply-To: References: Message-ID: Hi, On 1 August 2013 14:22, Matthew Ngaha wrote: > Thanks guys. i was following some examples and noticed it. I tried to > use it in my program and the same thing happened. In this example, > there are 6 tests, and only 4 run. > > http://bpaste.net/show/abLNTHU49w1j2M8Fey8X/ > The reason that 2 of the tests are apparently ignored, is because they are overwritten/redefined by the last 2 test methods in the class, which have the same name as the prior 2. You cannot have have multiple methods with the same name in the same class. Change the name of the last 2 methods in your class and your problem will go away. Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Thu Aug 1 15:47:59 2013 From: __peter__ at web.de (Peter Otten) Date: Thu, 01 Aug 2013 15:47:59 +0200 Subject: [Tutor] unittest skipping tests References: Message-ID: Matthew Ngaha wrote: > Thanks guys. i was following some examples and noticed it. I tried to > use it in my program and the same thing happened. In this example, > there are 6 tests, and only 4 run. class TestAverage(unittest.TestCase): [...] def test_python30_mine(self): self.assertRaises(ZeroDivisionError, average, [5]) [...] #although sum is Zero, the length is not Zero def test_python30_mine(self): self.assertRaises(ZeroDivisionError, average, [0]) You need a different name for every method. That's the same situation as x = "foo" x = "bar" print(x) # there's no way to find the previous value of x Python's 'def' is executable code similar to an assignment, basically def f(): ... makes a function object and binds it to the name 'f'. From alan.gauld at btinternet.com Thu Aug 1 15:51:17 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 01 Aug 2013 14:51:17 +0100 Subject: [Tutor] unittest skipping tests In-Reply-To: References: Message-ID: On 01/08/13 14:22, Matthew Ngaha wrote: > Thanks guys. i was following some examples and noticed it. I tried to > use it in my program and the same thing happened. In this example, > there are 6 tests, and only 4 run. > > http://bpaste.net/show/abLNTHU49w1j2M8Fey8X/ You're still just teasing us. You show us the code and say 4 methods run. But which 4? And how are you running the test? And what output do you get that tells you 4 ran? We need to know what is happening in more detail. While you are at it, which OS and Python version too. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From chigga101 at gmail.com Thu Aug 1 16:08:19 2013 From: chigga101 at gmail.com (Matthew Ngaha) Date: Thu, 1 Aug 2013 15:08:19 +0100 Subject: [Tutor] unittest skipping tests In-Reply-To: References: Message-ID: Thanks for the help and explanations guys. I feel really bad for making this type of error. On Thu, Aug 1, 2013 at 2:51 PM, Alan Gauld wrote: > You're still just teasing us. > You show us the code and say 4 methods run. > But which 4? > > And how are you running the test? > And what output do you get that tells you 4 ran? Sorry about this, I wasn't sure at the time which 4 were running. I have run it through both IDLE and my terminal. Here's the output http://bpaste.net/show/UmSaLOBhJxPF8v5lV1Aj/ > We need to know what is happening in more detail. > While you are at it, which OS and Python version too. > I'm on windows vista and I have both Python 3.1 and 3.3. This script is a Python 3.1 script, and in the terminal it runs via Python 3.1. But for some reason when i run any Python through IDLE, they all run from Python 3.3. The test output i showed is from Python 3.3, but it's the same as the 3.1 output From sunithanc at gmail.com Thu Aug 1 16:52:16 2013 From: sunithanc at gmail.com (SM) Date: Thu, 1 Aug 2013 10:52:16 -0400 Subject: [Tutor] Implementing Copy/Cut/Paste menu actions using MainWindow/PyQT4 Message-ID: Hello, I am using the PyQT4 designer to create the MainWindow with very simple drop-down menu items. File->Exit, Edit->Cut,Copy,Paste After converting .ui to .py I am trying to make some manual modifications to the Python script to set the SLOTs for cut/copy/paste actions. Within the main window, I have a tabbed-Gui, where clicking on each tab will display more options for the users to choose various files and directories. I am using lineEdit and TextEdit widgets for this and would like to have the cut/copy/paste menus from the MainWindow let the user use them while typing text into the lineEdit and textEdit spaces. I am setting the slots as below: self.actionExit.triggered.connect(self.exitMenu) self.actionCopy.triggered.connect(self.copyMenu) self.actionCut.triggered.connect(self.cutMenu) self.actionPaste.triggered.connect(self.pasteMenu) I am implementing routines for the "slot" functions, where I need help. Exit action works. But I couldn't find a way to implement the rest for this situation where there are multiple locations on the Gui where these actions are to be done. def exitMenu(self): QtCore.QCoreApplication.instance().quit() def cutMenu(self): print("cutting text ") ??? def copyMenu(self): print("Copying text ") ??? def pasteMenu(self): print("Pasting text ") ??? If I was using just one editor for all these actions, I could do something like: edit = QtGui .QTextEdit (self) pasteAction.triggered.connect(edit.paste) cutAction.triggered.connect(edit.cut) etc. But here I have multiple places on multiple tabs of the Gui to do cut/copy/paste actions. Is there a way in PyQt4 that I can do it? Thanks! SM -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Aug 1 18:52:34 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 01 Aug 2013 17:52:34 +0100 Subject: [Tutor] Implementing Copy/Cut/Paste menu actions using MainWindow/PyQT4 In-Reply-To: References: Message-ID: On 01/08/13 15:52, SM wrote: > I am using the PyQT4 designer... This list is for those learning Python and its standard library. You may find somebody here that knows QT well enough to answer but you will have more chance of success on a PyQT specific forum or the main Python list. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From mike at froward.org Thu Aug 1 19:51:48 2013 From: mike at froward.org (mike at froward.org) Date: Thu, 1 Aug 2013 12:51:48 -0500 (CDT) Subject: [Tutor] Using dictionary key values as subprocess arguments Message-ID: <50811.50.73.105.57.1375379508.squirrel@epoch.froward.org> Hi all, I'm trying to use dictionary key values as arguments being passed via subprocess. The following is supposed to use argv arguments passed for the target host, and direction to turn the ipmi interface up on or off. When I run it I get an error about TypeError: format requires a mapping Any insight would be appreciated, thanks! #!/usr/bin/env python from sys import argv import subprocess script, targ, switch = argv ren = {'hostn':'ren.ipmi', 'usern':'Admin', 'passw':'p0w1r'} stimpy = {'hostn':'stimpy.ipmi', 'usern':'Admin', 'passw':'p0w1r'} lrrr = {'hostn':'lrrr.ipmi', 'usern':'ADMIN', 'passw':'p0w1r'} kif = {'hostn':'kif.ipmi', 'usern':'ADMIN', 'passw':'p0w1r'} ndnd = {'hostn':'ndnd.ipmi', 'usern':'ADMIN', 'passw':'p0w1r'} zapp = {'hostn':'zapp.ipmi', 'usern':'ADMIN', 'passw':'p0w1r'} def run(): if switch == "on": on() elif switch == "off": off() def on(): turnOn = ['ipmitool', '-I', 'lan', '-U', '%(usern)s' % targ, '-P', '%(passw)s' % targ, '-H', '%(hostn)s' % targ, 'chassis', 'power', 'on'] subprocess.call(turnOn) print "on!" def off(): turnOff = ['ipmitool', '-I', 'lan', '-U', '%(usern)s' % targ, '-P', '%(passw)s' % targ, '-H', '%(hostn)s' % targ, 'chassis', 'power', 'off'] print "off" subprocess.call(turnOff) run() From steve at pearwood.info Thu Aug 1 20:27:17 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 02 Aug 2013 04:27:17 +1000 Subject: [Tutor] Using dictionary key values as subprocess arguments In-Reply-To: <50811.50.73.105.57.1375379508.squirrel@epoch.froward.org> References: <50811.50.73.105.57.1375379508.squirrel@epoch.froward.org> Message-ID: <51FAA885.6020501@pearwood.info> Hi Mike, and welcome! On 02/08/13 03:51, mike at froward.org wrote: > Hi all, > > I'm trying to use dictionary key values as arguments being passed via > subprocess. The following is supposed to use argv arguments passed for the > target host, and direction to turn the ipmi interface up on or off. When I > run it I get an error about TypeError: format requires a mapping Would you like us to guess which line gives that error? I love guessing games! Ah, who am I fooling? Actually I hate them. Please don't expect us to guess where the error is, and don't assume we're going to run your code and see the same error. - we might not get the same error, for many reasons - or we might not be willing, or able, to run the code In general, the most valuable piece of information Python will ever give you is the full traceback it prints when an error occurs, starting with the line "Traceback" and ending at the actual error message. Please copy and paste the entire traceback -- don't retype, simplify, summarize or abbreviate it. In the meantime, I'm going to guess where your error might be: > script, targ, switch = argv Here you assign targ to one of the items of argv. Unless you've done something really weird elsewhere, targ will be a string. > def on(): > turnOn = ['ipmitool', '-I', 'lan', '-U', '%(usern)s' % targ, Here you call the % string interpolation operator and try to look up the name 'usern' from what ought to be a dict or other mapping, but is actually just a string. > '%(passw)s' % targ, And again. >'%(hostn)s' % targ, And one more time. You can replicate this error trivially: py> '%(spam)s' % 'hello' Traceback (most recent call last): File "", line 1, in TypeError: format requires a mapping Solution: targ is a string, not a mapping. You need to convert it into a mapping. If anyone suggests you use eval(targ), hit them with a halibut. eval() is not safe with untrusted data, and can execute arbitrary code. Instead, you can either parse targ yourself, or try using ast.literal_eval. -- Steven From fomcl at yahoo.com Thu Aug 1 21:28:35 2013 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 1 Aug 2013 12:28:35 -0700 (PDT) Subject: [Tutor] True and 1 [was Re: use of the newer dict types] In-Reply-To: References: <51F1DBE9.4010405@pearwood.info> <1374828645.89019.YahooMailNeo@web163806.mail.gq1.yahoo.com> <51F34F7D.3010301@pearwood.info> <51F4133A.8050204@pearwood.info> <51F4984E.5090905@pearwood.info> <51F4D760.1030100@pearwood.info> <1375097596.79660.YahooMailNeo@web163803.mail.gq1.yahoo.com> Message-ID: <1375385315.81653.YahooMailNeo@web163806.mail.gq1.yahoo.com> ----- Original Message ----- > From: eryksun > To: Albert-Jan Roskam > Cc: "tutor at python.org" > Sent: Wednesday, July 31, 2013 4:16 PM > Subject: Re: [Tutor] True and 1 [was Re: use of the newer dict types] > > On Mon, Jul 29, 2013 at 7:33 AM, Albert-Jan Roskam > wrote: >> Wow, too bad __cmp__ is no longer present. Not so much for cmp(), but >> because it's such a compact way to define all the comparison methods. >> Btw, my book says "Python will supply the __ne__() (not equal) >> inequality operator automatically if wel implement __eq__ but don't >> implement __ne__()". [Programming in Python 3 (2009), M. Summerfield, >> p.213] > > That's true in 3.x. The base object type has a default rich comparison > that uses __eq__ to implement __ne__ (but not vice versa). > > The CPython tp_richcompare slot function is object_richcompare in > Objects/typeobject.c. Here's a link to 3.3.2: > > http://hg.python.org/cpython/file/d047928ae3f6/Objects/typeobject.c#l3175 > > In 2.x the base object type doesn't implement rich comparisons. You'll > get the default comparison in CPython (by id or type name), which > won't necessarily be consistent with __eq__. > > Here's something to riddle out. Why does CPython 2.x make 22 method > calls for the following cmp()? > > ? ? class A(object): > ? ? ? ? def _print(self, op): > ? ? ? ? ? ? print '%s.__%s__' % (type(self).__name__, op) > ? ? ? ? def __eq__(self, other): > ? ? ? ? ? ? self._print('eq') > ? ? ? ? ? ? return NotImplemented > ? ? ? ? def __lt__(self, other): > ? ? ? ? ? ? self._print('lt') > ? ? ? ? ? ? return NotImplemented > ? ? ? ? def __gt__(self, other): > ? ? ? ? ? ? self._print('gt') > ? ? ? ? ? ? return NotImplemented > ? ? ? ? def __coerce__(self, other): > ? ? ? ? ? ? self._print('coerce') > ? ? ? ? ? ? return NotImplemented > > ? ? class B(A): > ? ? ? ? pass > > ? ? >>> cmp(A(), B()) > ? ? B.__eq__ > ? ? A.__eq__ > ? ? A.__eq__ > ? ? B.__eq__ > ? ? B.__eq__ > ? ? A.__eq__ > ? ? B.__gt__ > ? ? A.__lt__ > ? ? A.__lt__ > ? ? B.__gt__ > ? ? B.__gt__ > ? ? A.__lt__ > ? ? B.__lt__ > ? ? A.__gt__ > ? ? A.__gt__ > ? ? B.__lt__ > ? ? B.__lt__ > ? ? A.__gt__ > ? ? A.__coerce__ > ? ? B.__coerce__ > ? ? B.__coerce__ > ? ? A.__coerce__ > ? ? -1 Six comparisons for each operator (6 x 3) and the 4 calls to __coerce__ seems much. So you arrvie at 8 calls by 3 operator methods called "bidirectionally" (A-B and B-A) + two calls to __coerce__? > Why is the order BAABBA repeated 3 times? As a hint, I'll group it > like this: BA--AB--BA. > > Jython does this 'right' (IMHO) with 8 calls. PyPy appears to be > playing tight to what's technically allowed and steps over the line. > It only uses 4 calls, but it doesn't attempt a __coerce__, which I > think is technically wrong. I did "help(coerce)" and it seems straightforward, however, I can't connect it to your remark about coercion. I thought pypy would behave differently with e.g cmp(1, "x"), compared to cpython antonia at antonia-HP-2133 ~ $ pypy Python 2.7.2 (1.9+dfsg-1, Jun 19 2012, 23:23:45) [PyPy 1.9.0 with GCC 4.7.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. And now for something completely different: ``'that's definitely a case of "uh????"''' >>>> cmp(1, "x") -1 >>>> cmp(1, 1.0) 0 >>>> cmp(-1, 1.0) -1 >>>> quit() antonia at antonia-HP-2133 ~ $ python Python 2.7.3 (default, Apr 10 2013, 05:09:49) [GCC 4.7.2] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> cmp(1, "x") -1 >>> cmp(1, 1.0) 0 >>> cmp(-1, 1.0) -1 # I was hoping this, I mean dis, would shed some light on this >>> import dis >>> dis.dis("cmp(0, 0)") ????????? 0 DUP_TOPX??????? 28781 ????????? 3 STORE_SLICE+0? ????????? 4 <48>?????????? ????????? 5 <44>?????????? ????????? 6 SLICE+2??????? ????????? 7 <48>?????????? ????????? 8 STORE_SLICE+1? >>> dis.dis("cmp(0, 0.0)") ????????? 0 DUP_TOPX??????? 28781 ????????? 3 STORE_SLICE+0? ????????? 4 <48>?????????? ????????? 5 <44>?????????? ????????? 6 SLICE+2??????? ????????? 7 <48>?????????? ????????? 8 <46>?????????? ????????? 9 <48>?????????? ???????? 10 STORE_SLICE+1? From alan.gauld at btinternet.com Thu Aug 1 23:30:56 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 01 Aug 2013 22:30:56 +0100 Subject: [Tutor] Using dictionary key values as subprocess arguments In-Reply-To: <50811.50.73.105.57.1375379508.squirrel@epoch.froward.org> References: <50811.50.73.105.57.1375379508.squirrel@epoch.froward.org> Message-ID: On 01/08/13 18:51, mike at froward.org wrote: > I'm trying to use dictionary key values as arguments being passed via > subprocess. The following is supposed to use argv arguments passed for the > target host, and direction to turn the ipmi interface up on or off. When I > run it I get an error about TypeError: format requires a mapping You need to provide a dict to the form,at operator you are passing a string which (you hope!) represents one of your dicts. > > script, targ, switch = argv > > ren = {'hostn':'ren.ipmi', 'usern':'Admin', 'passw':'p0w1r'} > stimpy = {'hostn':'stimpy.ipmi', 'usern':'Admin', 'passw':'p0w1r'} > lrrr = {'hostn':'lrrr.ipmi', 'usern':'ADMIN', 'passw':'p0w1r'} > kif = {'hostn':'kif.ipmi', 'usern':'ADMIN', 'passw':'p0w1r'} > ndnd = {'hostn':'ndnd.ipmi', 'usern':'ADMIN', 'passw':'p0w1r'} > zapp = {'hostn':'zapp.ipmi', 'usern':'ADMIN', 'passw':'p0w1r'} Rather than using named variables it would be better to make the hosts a dicty too keyed by host name: hosts = { 'ren':{'hostn':'ren.ipmi', 'usern':'Admin', 'passw':'p0w1r'}, 'stimpy':{'hostn':'stimpy.ipmi', 'usern':'Admin', 'passw':'p0w1r'} # etc... 'zapp':{'hostn':'zapp.ipmi', 'usern':'ADMIN', 'passw':'p0w1r'} } Now you can pass the dict into the format with: turnOn = ['ipmitool', '-I', 'lan', '-U', '%(usern)s' % hosts[targ], '-P', '%(passw)s' % hosts[targ], '-H', '%(hostn)s' % hosts[targ], 'chassis', 'power', 'on'] And while you are at it catch invalid host names by using a try/except block to catch KeyErrors -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eryksun at gmail.com Fri Aug 2 01:58:52 2013 From: eryksun at gmail.com (eryksun) Date: Thu, 1 Aug 2013 19:58:52 -0400 Subject: [Tutor] True and 1 [was Re: use of the newer dict types] In-Reply-To: <1375385315.81653.YahooMailNeo@web163806.mail.gq1.yahoo.com> References: <51F1DBE9.4010405@pearwood.info> <1374828645.89019.YahooMailNeo@web163806.mail.gq1.yahoo.com> <51F34F7D.3010301@pearwood.info> <51F4133A.8050204@pearwood.info> <51F4984E.5090905@pearwood.info> <51F4D760.1030100@pearwood.info> <1375097596.79660.YahooMailNeo@web163803.mail.gq1.yahoo.com> <1375385315.81653.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: On Thu, Aug 1, 2013 at 3:28 PM, Albert-Jan Roskam wrote: > > Six comparisons for each operator (6 x 3) and the 4 calls to __coerce__ > seems much. So you arrvie at 8 calls by 3 operator methods called > "bidirectionally" (A-B and B-A) + two calls to __coerce__? If the types are the same, cmp uses __cmp__ if it's defined and implemented. Otherwise CPython uses the rich comparisons EQ, LT, and GT, with the operands both unswapped and swapped (EQ swaps with itself, and LT swaps with GT). It returns the first implemented result (but not the default comparison at this step). Also, if the 2nd operand's type is a subclass of the first, the swapped operation is done first, so the subclass can override the parent. My example returns NotImplemented for all of the rich comparisons, so the interpreter should try a classic comparison using __coerce__ (unswapped and swapped) to get comparable objects. PyPy skips coercing the objects. It also allows the default result from the LT rich comparison (comparing type names in this case) to trump attempting a GT comparison. CPython only uses the default comparison after exhausting all other options. That seems right to me, but this is language lawyer territory... As to the excessive calls in CPython, (1) it doesn't remember that it's already tried the swapped operation if the 2nd operand's type is a subclass, so it does it again. (2) The API calls such as PyObject_RichCompare try each comparison swapped and unswapped, but the slot function (e.g. slot_tp_richcompare) *also* tries it swapped and unswapped. The same problem exists with PyNumber_CoerceEx vs. slot_nb_coerce. CPython 3.x doesn't have this problem with its rich comparison implementation. > I did "help(coerce)" and it seems straightforward, however, I can't connect > it to your remark about coercion. I thought pypy would behave differently > with e.g cmp(1, "x"), compared to cpython In CPython 2.x numbers are less than other types, except None is less than anything. I think PyPy implements the same default behavior. int 1 and str "x" won't be coerced to a comparable type. str doesn't implement __coerce__. The number types have rich comparisons defined with each other and won't coerce with non-number types. For example, long can coerce an int; float can coerce an int or long (maybe with loss of precision); and complex can coerce an int, long, or float. Here's a simple example that returns the default result in PyPy 2.02: class A(object): def __coerce__(self, other): return 10, other >>>> cmp(A(), 9), cmp(A(), 10), cmp(A(), 11) (1, 1, 1) >>>> cmp(9, A()), cmp(10, A()), cmp(11, A()) (-1, -1, -1) Here's the result in CPython 2.7.5: >>> cmp(A(), 9), cmp(A(), 10), cmp(A(), 11) (1, 0, -1) >>> cmp(9, A()), cmp(10, A()), cmp(11, A()) (-1, 0, 1) > # I was hoping this, I mean dis, would shed some light on this >>>> import dis >>>> dis.dis("cmp(0, 0)") > 0 DUP_TOPX 28781 > 3 STORE_SLICE+0 > 4 <48> > 5 <44> > 6 SLICE+2 > 7 <48> > 8 STORE_SLICE+1 First, disassembling a function *call* won't tell you anything about its implementation. You'd have to disassemble the function, which you can't do for a built-in function or method (it isn't bytecode). You can't even disassemble cmp in PyPy. Second, you just disassembled a string as bytecode. Let's do that manually. First get the op names, but remove the 'mp' from 'cmp', which is treated as an arg to DUP_TOPX: >>> ops = [dis.opname[ord(c)] for c in 'c(0, 0)'] Now unpack 'mp' as a little-endian, unsigned short and insert it at index 1: >>> ops.insert(1, struct.unpack('>> ops ['DUP_TOPX', 28781, 'STORE_SLICE+0', '<48>', '<44>', 'SLICE+2', '<48>', 'STORE_SLICE+1'] From cybervigilante at gmail.com Fri Aug 2 09:32:20 2013 From: cybervigilante at gmail.com (Jim Mooney) Date: Fri, 2 Aug 2013 00:32:20 -0700 Subject: [Tutor] implied tuple in a list comprehension Message-ID: # python 3 on win 7 S = enumerate('all good dogs eat shoes'.split()) # I'm curious why this works: x = [(idx, word) for idx, word in S] # but this doesn't: x = [idx, word for idx, word in S] #syntax error # Why can I imply a tuple after the for, but not before? -- Jim Just remember, food faddists, the first three letters of Diet spells DIE! From alan.gauld at btinternet.com Fri Aug 2 09:46:21 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 02 Aug 2013 08:46:21 +0100 Subject: [Tutor] implied tuple in a list comprehension In-Reply-To: References: Message-ID: On 02/08/13 08:32, Jim Mooney wrote: > x = [idx, word for idx, word in S] #syntax error > # Why can I imply a tuple after the for, but not before? How should Python interpret this? As x = [idx, (word for idx, word in S)] Or x = [(idx, word) for idx, word in S] It's ambiguous. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From cybervigilante at gmail.com Fri Aug 2 10:02:01 2013 From: cybervigilante at gmail.com (Jim Mooney) Date: Fri, 2 Aug 2013 01:02:01 -0700 Subject: [Tutor] implied tuple in a list comprehension In-Reply-To: References: Message-ID: On 2 August 2013 00:46, Alan Gauld wrote: > On 02/08/13 08:32, Jim Mooney wrote: > How should Python interpret this? > > As > > x = [idx, (word for idx, word in S)] > > Or > > > x = [(idx, word) for idx, word in S] > > It's ambiguous. > I see what you mean, but I figured it can't be ambiguous if one interpretation makes no sense, and I can't see what x = [idx, (word for idx, word in S)] could possibly mean. Am I assuming too much foresight on the part of the parser or does that actually mean something? -- Jim Just remember, food faddists, the first three letters of Diet are DIE! From steve at pearwood.info Fri Aug 2 10:27:38 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 02 Aug 2013 18:27:38 +1000 Subject: [Tutor] implied tuple in a list comprehension In-Reply-To: References: Message-ID: <51FB6D7A.2080602@pearwood.info> On 02/08/13 18:02, Jim Mooney wrote: > I see what you mean, but I figured it can't be ambiguous if one > interpretation makes no sense, and I can't see what x = [idx, (word > for idx, word in S)] could possibly mean. Am I assuming too much > foresight on the part of the parser or does that actually mean > something? It makes perfect sense. You can try it yourself, if you pre-define some names that get used: idx = 42 S = [(0, "fe"), (1, "fi"), (2, "fo"), (3, "fum")] x = [idx, (word for idx, word in S)] print(x) => prints [42, at 0xb7b7098c>] Hmmm, that's interesting. What's a generator object? py> next(x[1]) 'fe' py> next(x[1]) 'fi' py> next(x[1]) 'fo' py> next(x[1]) 'fum' py> next(x[1]) Traceback (most recent call last): File "", line 1, in StopIteration -- Steven From hugo.yoshi at gmail.com Fri Aug 2 10:27:19 2013 From: hugo.yoshi at gmail.com (Hugo Arts) Date: Fri, 2 Aug 2013 10:27:19 +0200 Subject: [Tutor] implied tuple in a list comprehension In-Reply-To: References: Message-ID: On Fri, Aug 2, 2013 at 10:02 AM, Jim Mooney wrote: > On 2 August 2013 00:46, Alan Gauld wrote: > > On 02/08/13 08:32, Jim Mooney wrote: > > > How should Python interpret this? > > > > As > > > > x = [idx, (word for idx, word in S)] > > > > Or > > > > > > x = [(idx, word) for idx, word in S] > > > > It's ambiguous. > > > I see what you mean, but I figured it can't be ambiguous if one > interpretation makes no sense, and I can't see what x = [idx, (word > for idx, word in S)] could possibly mean. Am I assuming too much > foresight on the part of the parser or does that actually mean > something? > > Yes, it means something very clear, try running it: >>> s = zip(range(10), range(10, 0, -1)) >>> s [(0, 10), (1, 9), (2, 8), (3, 7), (4, 6), (5, 5), (6, 4), (7, 3), (8, 2), (9, 1)] >>> idx = "hello" >>> x = [idx, (word for idx, word in s)] >>> x ['hello', at 0x7f54a21e0780>] >>> it means "make the name 'x' point to a list, with as its first element the variable 'idx' and as its second variable a generator expression. The generator expression takes s, assumes it is a sequence of 2-tuples (by unpacking each item into two variables, 'idx' and 'word', and grabs the second item ('word') from each tuple. HTH, Hugo -------------- next part -------------- An HTML attachment was scrubbed... URL: From cybervigilante at gmail.com Fri Aug 2 20:13:25 2013 From: cybervigilante at gmail.com (Jim Mooney) Date: Fri, 2 Aug 2013 11:13:25 -0700 Subject: [Tutor] implied tuple in a list comprehension In-Reply-To: <51FB6D7A.2080602@pearwood.info> References: <51FB6D7A.2080602@pearwood.info> Message-ID: On 2 August 2013 01:27, Steven D'Aprano wrote: > It makes perfect sense. You can try it yourself, if you pre-define some > names that get used: > > idx = 42 > S = [(0, "fe"), (1, "fi"), (2, "fo"), (3, "fum")] > > x = [idx, (word for idx, word in S)] Ack, I even made the original mistake, thinking word for idx, then comma, then word in S, which made no sense, instead of unpacking - word for (idx, word) in S. Done in again by the implied tuple ;') Jim --- If you sign a petition that usually says "People for Freedoms of blah, blah, blah" and look into it, you may find the petitioner is a paid shill for a giant company or organization, it has nothing to do with people or freedom, and is positively damaging. l. From alan.gauld at btinternet.com Fri Aug 2 21:07:25 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 02 Aug 2013 20:07:25 +0100 Subject: [Tutor] implied tuple in a list comprehension In-Reply-To: References: <51FB6D7A.2080602@pearwood.info> Message-ID: On 02/08/13 19:13, Jim Mooney wrote: > comma, then word in S, which made no sense, instead of unpacking - > word for (idx, word) in S. Done in again by the implied tuple ;') Just to pick up a point that might be confusing you. A tuple does not need parentheses. >>> tup = 5,4 >>> tup (5, 4) >>> type(tup) tup is a tuple. Its not implied it is complete. The parentheses are only added to avoid ambiguity and aid readability. For example a single item tuple is more obvious with the parens: t = (6,) # t=6, works but is much harder to see. HTH, -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From cybervigilante at gmail.com Fri Aug 2 21:18:10 2013 From: cybervigilante at gmail.com (Jim Mooney) Date: Fri, 2 Aug 2013 12:18:10 -0700 Subject: [Tutor] implied tuple in a list comprehension In-Reply-To: References: <51FB6D7A.2080602@pearwood.info> Message-ID: On 2 August 2013 12:07, Alan Gauld wrote: > t = (6,) # t=6, works but is much harder to see. That could lead to awful bugs, since it really is hard to see, and hitting the comma is a mistake I often make. I think I may stick with always using parentheses for tuples. Some conveniences aren't that convenient. Jim From alan.gauld at btinternet.com Fri Aug 2 21:26:31 2013 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Fri, 2 Aug 2013 20:26:31 +0100 (BST) Subject: [Tutor] implied tuple in a list comprehension In-Reply-To: References: <51FB6D7A.2080602@pearwood.info> Message-ID: <1375471591.79308.YahooMailNeo@web186006.mail.ir2.yahoo.com> >> t = (6,)? # t=6,? works but is much harder to see. > > That could lead to awful bugs, since it really is hard to see, and > hitting the comma is a mistake I often make. I think I may stick with > always using parentheses for tuples. Some conveniences aren't that > convenient. And this from the man who hates extra typing! ;-) Alan g. From cybervigilante at gmail.com Fri Aug 2 23:09:40 2013 From: cybervigilante at gmail.com (Jim Mooney) Date: Fri, 2 Aug 2013 14:09:40 -0700 Subject: [Tutor] implied tuple in a list comprehension In-Reply-To: <1375471591.79308.YahooMailNeo@web186006.mail.ir2.yahoo.com> References: <51FB6D7A.2080602@pearwood.info> <1375471591.79308.YahooMailNeo@web186006.mail.ir2.yahoo.com> Message-ID: On 2 August 2013 12:26, ALAN GAULD wrote: > And this from the man who hates extra typing! ;-) Well, I'm not going whole hog. I'll keep unstated tuples in simple stuff like a,b,c,d,e = 1,2,3,4,5 , but I'll state them in complex, dynamic expressions that make my eyes swim. It's a matter of a sensible compromise between laziness and confusion ;') Jim From tim at akwebsoft.com Sat Aug 3 01:34:45 2013 From: tim at akwebsoft.com (Tim Johnson) Date: Fri, 2 Aug 2013 15:34:45 -0800 Subject: [Tutor] apply() vs. the extended call syntax Message-ID: <20130802233445.GE428@mail.akwebsoft.com> Frequently I use a function dispatch approach like this: funcs = {"key1":func1,"key2":func2} ## and call one of those functions with a key value ## as follows funcs[key](some,fixed,number,of,arguments) Less frequently, I might want to dispatch functions with variable numbers of arguments. Follows is an illustrative (hopefully) REPL session ## define a couple of simple function def test_apply(one,two): print(str(one) + " " + str(two)) def func_two(): print("I don't have an argument!") ## define references to one or more functions and argument lists ## of variable lengths func_D = {"key1":(test_apply,("one","two")), "key2":(func_two,())} ## give a key some value k = "key1" ## Use the deprecated apply() function first apply(*func_D[k]) one two ## yup, that's what I was looking for! ## Now use the extended call syntax func_D[k][0](*func_D[k][1]) one two ## also what I was looking for, but yuck! Is there a cleaner way to do this? using apply() looks so much simpler, but I understand it is not even available in py 3 .... thanks -- Tim tim at tee jay forty nine dot com or akwebsoft dot com http://www.akwebsoft.com From tim at akwebsoft.com Sat Aug 3 04:06:30 2013 From: tim at akwebsoft.com (Tim Johnson) Date: Fri, 2 Aug 2013 18:06:30 -0800 Subject: [Tutor] apply() vs. the extended call syntax In-Reply-To: <20130802233445.GE428@mail.akwebsoft.com> References: <20130802233445.GE428@mail.akwebsoft.com> Message-ID: <20130803020630.GH428@mail.akwebsoft.com> * Tim Johnson [130802 15:41]: <...> Is there a cleaner way to do this? using apply() > looks so much simpler, but I understand it is not even available in > py 3 .... def apl(funcall): funcall[0](*funcall[1]) k = "key2" >>> apl(func_D[k]) I don't have an argument! ## ??? :) -- Tim tim at tee jay forty nine dot com or akwebsoft dot com http://www.akwebsoft.com From davea at davea.name Sat Aug 3 04:25:19 2013 From: davea at davea.name (Dave Angel) Date: Sat, 3 Aug 2013 02:25:19 +0000 (UTC) Subject: [Tutor] apply() vs. the extended call syntax References: <20130802233445.GE428@mail.akwebsoft.com> <20130803020630.GH428@mail.akwebsoft.com> Message-ID: Tim Johnson wrote: > * Tim Johnson [130802 15:41]: > <...> Is there a cleaner way to do this? using apply() >> looks so much simpler, but I understand it is not even available in >> py 3 .... > def apl(funcall): funcall[0](*funcall[1]) > k = "key2" > >>> apl(func_D[k]) > I don't have an argument! > ## ??? :) > See http://docs.python.org/2/library/functions.html#apply The apply approach has been replaced by * and ** arguments, the former for positional args, and the latter for keyword arguments. See http://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists -- DaveA From tim at akwebsoft.com Sat Aug 3 04:38:57 2013 From: tim at akwebsoft.com (Tim Johnson) Date: Fri, 2 Aug 2013 18:38:57 -0800 Subject: [Tutor] apply() vs. the extended call syntax In-Reply-To: References: <20130802233445.GE428@mail.akwebsoft.com> <20130803020630.GH428@mail.akwebsoft.com> Message-ID: <20130803023857.GJ428@mail.akwebsoft.com> * Dave Angel [130802 18:31]: > Tim Johnson wrote: > > > * Tim Johnson [130802 15:41]: > > <...> Is there a cleaner way to do this? using apply() > >> looks so much simpler, but I understand it is not even available in > >> py 3 .... > > def apl(funcall): funcall[0](*funcall[1]) > > k = "key2" > > >>> apl(func_D[k]) > > I don't have an argument! > > ## ??? :) > > > > See http://docs.python.org/2/library/functions.html#apply > > The apply approach has been replaced by * and ** arguments, the former > for positional args, and the latter for keyword arguments. > > See > http://docs.python.org/2/tutorial/controlflow.html#unpacking-argument-lists Hi Dave : Thank you for the reply, but I don't see anything that answers my original query at _that_ URL. Perhaps I will be edified further after a good night's sleep OR apply() needs to be revisited. I look forward to the morning's revelations. thanks again -- Tim tim at tee jay forty nine dot com or akwebsoft dot com http://www.akwebsoft.com From __peter__ at web.de Sat Aug 3 08:32:46 2013 From: __peter__ at web.de (Peter Otten) Date: Sat, 03 Aug 2013 08:32:46 +0200 Subject: [Tutor] apply() vs. the extended call syntax References: <20130802233445.GE428@mail.akwebsoft.com> Message-ID: Tim Johnson wrote: > Frequently I use a function dispatch approach like this: > funcs = {"key1":func1,"key2":func2} > > ## and call one of those functions with a key value > ## as follows > funcs[key](some,fixed,number,of,arguments) > > Less frequently, I might want to dispatch functions with variable > numbers of arguments. > > Follows is an illustrative (hopefully) REPL session > ## define a couple of simple function > def test_apply(one,two): print(str(one) + " " + str(two)) > def func_two(): print("I don't have an argument!") > > ## define references to one or more functions and argument lists > ## of variable lengths > > func_D = {"key1":(test_apply,("one","two")), "key2":(func_two,())} > > ## give a key some value > k = "key1" > > ## Use the deprecated apply() function first > apply(*func_D[k]) > one two ## yup, that's what I was looking for! > > ## Now use the extended call syntax > func_D[k][0](*func_D[k][1]) > one two > ## also what I was looking for, but yuck! > > Is there a cleaner way to do this? using apply() > looks so much simpler, but I understand it is not even available in > py 3 .... I'd say func, args = func_D[k] func(*args) looks pretty clean. Alternatively, if the arguments are fixed anyway, change the dict to store only no-arg functions. You can make them on the fly with lambda expressions or functools.partial(): from functools import partial func_dict = { "key1": partial(test_apply, "one", "two"), "key2": func_two, "key3": lambda: foo + bar/baz, } ... func_dict[k]() From fomcl at yahoo.com Sat Aug 3 11:02:36 2013 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sat, 3 Aug 2013 02:02:36 -0700 (PDT) Subject: [Tutor] apply() vs. the extended call syntax In-Reply-To: References: <20130802233445.GE428@mail.akwebsoft.com> Message-ID: <1375520556.29480.YahooMailNeo@web163803.mail.gq1.yahoo.com> ----- Original Message ----- > From: Peter Otten <__peter__ at web.de> > To: tutor at python.org > Cc: > Sent: Saturday, August 3, 2013 8:32 AM > Subject: Re: [Tutor] apply() vs. the extended call syntax > >T im Johnson wrote: > >> Frequently I use a function dispatch approach like this: >> funcs = {"key1":func1,"key2":func2} >> >> ## and call one of those functions with a key value >> ## as follows >> funcs[key](some,fixed,number,of,arguments) >> >> Less frequently, I might want to dispatch functions with variable >> numbers of arguments. >> >> Follows is an illustrative (hopefully) REPL session >> ## define a couple of simple function >> def test_apply(one,two): print(str(one) + " " + str(two)) >> def func_two(): print("I don't have an argument!") >> >> ## define references to one or more functions and argument lists >> ## of variable lengths >> >> func_D = > {"key1":(test_apply,("one","two")), > "key2":(func_two,())} >> >> ## give a key some value >> k = "key1" >> >> ## Use the deprecated apply() function first Is there a reason why the apply function is deprecated/gone, but map() isn't? Aren't they both based on the lisp-style functional programming approach? From __peter__ at web.de Sat Aug 3 11:24:51 2013 From: __peter__ at web.de (Peter Otten) Date: Sat, 03 Aug 2013 11:24:51 +0200 Subject: [Tutor] apply() vs. the extended call syntax References: <20130802233445.GE428@mail.akwebsoft.com> <1375520556.29480.YahooMailNeo@web163803.mail.gq1.yahoo.com> Message-ID: Albert-Jan Roskam wrote: > Is there a reason why the apply function is deprecated/gone, but map() > isn't? Aren't they both based on the lisp-style functional programming > approach? I don't remember the discussion, but personally I never (well, maybe once or twice) used apply while there are many instances of map/imap() in my code, even though these could be rewritten as list comprehensions or generator expressions. Perhaps there are core-devs in the same situation... Had there been a great demand for apply() it could have been moved into functools (like reduce()). From fomcl at yahoo.com Sat Aug 3 16:50:49 2013 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sat, 3 Aug 2013 07:50:49 -0700 (PDT) Subject: [Tutor] .__mul__ Message-ID: <1375541449.64114.YahooMailNeo@web163801.mail.gq1.yahoo.com> Hi, Suppose I initialize? a list (let? say it's a record) to e.g all zeroes, or all sixes. Suppose, further, that I use "*" for this (which is a nice an clean way). Then how do I get rid of the scary thing that the list items are "interdependent"? See below what I mean with that. # repeating items of a list using list.__mul__ >>> y = 4 * [[6]] >>> y [[6], [6], [6], [6]] >>> # is there something I can do right *here* to make the list items independent? (copy.deepcopy does not work) >>> y[0][0] = 666 >>> y [[666], [666], [666], [666]]??? # look mom, I changed all four items in one go! :-( # repeating items of a list using itertools.repeat >>> from itertools import repeat >>> yyy = repeat([6], 4) >>> yyy repeat([6], 4) >>> yyy = list(yyy) >>> yyy [[6], [6], [6], [6]] >>> yyy[0][0] = 666 >>> yyy [[666], [666], [666], [666]]? # same thing: assignment of one item changes all items. # the not-so-scary alternative >>> yy = [[6] for i in range(4)] >>> yy [[6], [6], [6], [6]] >>> yy[0][0] = 666 >>> yy [[666], [6], [6], [6]] Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ?~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? From eryksun at gmail.com Sat Aug 3 17:19:39 2013 From: eryksun at gmail.com (eryksun) Date: Sat, 3 Aug 2013 11:19:39 -0400 Subject: [Tutor] .__mul__ In-Reply-To: <1375541449.64114.YahooMailNeo@web163801.mail.gq1.yahoo.com> References: <1375541449.64114.YahooMailNeo@web163801.mail.gq1.yahoo.com> Message-ID: On Sat, Aug 3, 2013 at 10:50 AM, Albert-Jan Roskam wrote: > # repeating items of a list using itertools.repeat > >>>> from itertools import repeat >>>> yyy = repeat([6], 4) >>>> yyy > repeat([6], 4) >>>> yyy = list(yyy) >>>> yyy > [[6], [6], [6], [6]] >>>> yyy[0][0] = 666 >>>> yyy > [[666], [666], [666], [666]] > # same thing: assignment of one item changes all items. You repeated the same list object 4 times in a new list. All that does is increment the reference count on the original list: >>> base = [6] >>> sys.getrefcount(base) 2 >>> seq = list(repeat(base, 4)) >>> sys.getrefcount(base) # +4 6 You'd need to make a shallow copy: >>> base = [6] >>> seq = map(list, repeat(base, 4)) >>> sys.getrefcount(base) 2 >>> seq[0][0] = 666 >>> seq [[666], [6], [6], [6]] From tim at akwebsoft.com Sat Aug 3 17:26:13 2013 From: tim at akwebsoft.com (Tim Johnson) Date: Sat, 3 Aug 2013 07:26:13 -0800 Subject: [Tutor] apply() vs. the extended call syntax In-Reply-To: References: <20130802233445.GE428@mail.akwebsoft.com> Message-ID: <20130803152613.GK428@mail.akwebsoft.com> * Peter Otten <__peter__ at web.de> [130803 07:11]: > > func, args = func_D[k] > func(*args) Of course! Much cleaner and clearer. > looks pretty clean. Alternatively, if the arguments are fixed anyway, change > the dict to store only no-arg functions. You can make them on the fly with > lambda expressions or functools.partial(): > > from functools import partial > > func_dict = { > "key1": partial(test_apply, "one", "two"), > "key2": func_two, > "key3": lambda: foo + bar/baz, > } > ... > func_dict[k]() ... and it remains quite easy for one to 'rool their own' apply() Thanks for the tip, Peter regards -- Tim tim at tee jay forty nine dot com or akwebsoft dot com http://www.akwebsoft.com From alan.gauld at btinternet.com Sat Aug 3 18:41:01 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 03 Aug 2013 17:41:01 +0100 Subject: [Tutor] .__mul__ In-Reply-To: <1375541449.64114.YahooMailNeo@web163801.mail.gq1.yahoo.com> References: <1375541449.64114.YahooMailNeo@web163801.mail.gq1.yahoo.com> Message-ID: On 03/08/13 15:50, Albert-Jan Roskam wrote: > Suppose I initialize a list (let? say it's a record) to e.g all zeroes, > or all sixes. Suppose, further, that I use "*" for this > (which is a nice an clean way). Its only nice if you use it at the top level with an immutable value, otherwise , as you can see, it quickly becomes not nice and not clean. Use a list comprehension instead y = [[6] for i in range(4)] As the name suggests list comprehensions are designed for building lists. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From fomcl at yahoo.com Sat Aug 3 19:50:21 2013 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sat, 3 Aug 2013 10:50:21 -0700 (PDT) Subject: [Tutor] .__mul__ In-Reply-To: References: <1375541449.64114.YahooMailNeo@web163801.mail.gq1.yahoo.com> Message-ID: <1375552221.74337.YahooMailNeo@web163803.mail.gq1.yahoo.com> ----- Original Message ----- > From: eryksun > To: Albert-Jan Roskam > Cc: Python Mailing List > Sent: Saturday, August 3, 2013 5:19 PM > Subject: Re: [Tutor] .__mul__ > > On Sat, Aug 3, 2013 at 10:50 AM, Albert-Jan Roskam > wrote: >> # repeating items of a list using itertools.repeat >> >>>>> from itertools import repeat >>>>> yyy = repeat([6], 4) >>>>> yyy >> repeat([6], 4) >>>>> yyy = list(yyy) >>>>> yyy >> [[6], [6], [6], [6]] >>>>> yyy[0][0] = 666 >>>>> yyy >> [[666], [666], [666], [666]] >> # same thing: assignment of one item changes all items. > > You repeated the same list object 4 times in a new list. All that does > is increment the reference count on the original list: > > ? ? >>> base = [6] > ? ? >>> sys.getrefcount(base) > ? ? 2 > ? ? >>> seq = list(repeat(base, 4)) > ? ? >>> sys.getrefcount(base)? # +4 > ? ? 6 > > You'd need to make a shallow copy: > > ? ? >>> base = [6] > ? ? >>> seq = map(list, repeat(base, 4)) > ? ? >>> sys.getrefcount(base) > ? ? 2 > ? ? >>> seq[0][0] = 666 > ? ? >>> seq > ? ? [[666], [6], [6], [6]] Hi Alan, Eryksun, Thank you. If list.__mul__ is so tricky, why did they implement it the way they did? Are there situations where this behavior could be useful? Btw, this is one of the rare (very, very rare) cases where I find CRAN R better than Python: R version 2.15.1 (2012-06-22) -- "Roasted Marshmallows" Copyright (C) 2012 The R Foundation for Statistical Computing ISBN 3-900051-07-0 Platform: i686-pc-linux-gnu (32-bit) > rrr <- rep(list(list(6)), 4) > class(rrr) [1] "list" > rrr[[1]][[1]] <- 666 > rrr [[1]] [[1]][[1]] [1] 666 [[2]] [[2]][[1]] [1] 6 [[3]] [[3]][[1]] [1] 6 [[4]] [[4]][[1]] [1] 6 > rrrr <- list() > for (i in 1:4) { rrrr[[i]] <- list(6) } > rrrr[[1]][[1]] <- 666 > identical(rrr, rrrr) [1] TRUE From akleider at sonic.net Sat Aug 3 20:15:28 2013 From: akleider at sonic.net (Alex Kleider) Date: Sat, 03 Aug 2013 11:15:28 -0700 Subject: [Tutor] re module- puzzling results when matching money Message-ID: <62d4c7f7cbc741fc3a60a31f6db63d85@sonic.net> #!/usr/bin/env python """ I've been puzzling over the re module and have a couple of questions regarding the behaviour of this script. I've provided two possible patterns (re_US_money): the one surrounded by the 'word boundary' meta sequence seems not to work while the other one does. I can't understand why the addition of the word boundary defeats the match. I also don't understand why the split method includes the matched text. Splitting only works as I would have expected if no goupings are used. If I've set this up as intended, the full body of this e-mail should be executable as a script. Comments appreciated. alex kleider """ # file : tutor.py (Python 2.7, NOT Python 3) print 'Running "tutor.py" on an Ubuntu Linux machine. *********' import re target = \ """Cost is $4.50. With a $.30 discount: Price is $4.15. The price could be less, say $4 or $4. Let's see how this plays out: $4.50.60 """ # Choose one of the following two alternatives: re_US_money =\ r"((?P\$)(?P\d{0,})(?:\.(?P\d{2})){0,1})" # The above provides matches. # The following does NOT. # re_US_money =\ # r"\b((?P\$)(?P\d{0,})(?:\.(?P\d{2})){0,1})\b" pat_object = re.compile(re_US_money) match_object = pat_object.search(target) if match_object: print "'match_object.group()' and 'match_object.span()' yield:" print match_object.group(), match_object.span() print else: print "NO MATCH FOUND!!!" print print "Now will use 'finditer()':" print iterator = pat_object.finditer(target) i = 1 for iter in iterator: print print "iter #%d: "%(i, ), print iter.group() print "'groups()' yields: '%s'."%(iter.groups(), ) print iter.span() i += 1 sign = iter.group("sign") dollars = iter.group("dollars") cents = iter.group("cents") print sign, print " ", if dollars: print dollars, else: print "00", print " ", if cents: print cents, else: print "00", print t = target sub_target = pat_object.sub("", t) print print "Printing substitution: " print sub_target split_target = pat_object.split(target) print "Result of splitting on the target: " print split_target # End of script. From davea at davea.name Sat Aug 3 21:58:59 2013 From: davea at davea.name (Dave Angel) Date: Sat, 3 Aug 2013 19:58:59 +0000 (UTC) Subject: [Tutor] .__mul__ References: <1375541449.64114.YahooMailNeo@web163801.mail.gq1.yahoo.com> <1375552221.74337.YahooMailNeo@web163803.mail.gq1.yahoo.com> Message-ID: Albert-Jan Roskam wrote: > > Thank you. If list.__mul__ is so tricky, why did they implement it the way they did? Are there situations where this behavior could be useful? > > Btw, this is one of the rare (very, very rare) cases where I find CRAN R better than Python: Using "multiply" to pre-fill a list is very useful, and it's harmless if the value used is immutable, which it usually is. And the problem described in this thread is not specific to a list being filled with multiply.. Any time you bind the same mutable value to multiple places, you need to be aware that changing one of them changes them all. But this behavior is much safer than the alternative, not to mention faster and more memory efficient. Further, if the alternative (copy) were the default, it would require a new syntax to specify that you don't want a copy. Most languages I know of have the same duality, but they expose it differently. And in many cases, you can hide the behavior under the declarations. So you have tricky declarations and if you get them wrong, there are nasty surprises in store. For example in C++, you can declare a variable as a refernece to another one. Now when you use the first variable, you get value of the second one, changed or not. But if you forget the & in the declaration, the behavior changes in a subtle way, perhaps far from the place you made the declaration. One thing that Python does differently is that it *allows* you to change what something references. By being less restrictive than C++, you get more power. And more responsibility. -- Signature file not found From fomcl at yahoo.com Sat Aug 3 22:30:14 2013 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sat, 3 Aug 2013 13:30:14 -0700 (PDT) Subject: [Tutor] re module- puzzling results when matching money In-Reply-To: <62d4c7f7cbc741fc3a60a31f6db63d85@sonic.net> References: <62d4c7f7cbc741fc3a60a31f6db63d85@sonic.net> Message-ID: <1375561814.54714.YahooMailNeo@web163801.mail.gq1.yahoo.com> ----- Original Message ----- > From: Alex Kleider > To: Python Tutor > Cc: > Sent: Saturday, August 3, 2013 8:15 PM > Subject: [Tutor] re module- puzzling results when matching money > > #!/usr/bin/env python > > """ > I've been puzzling over the re module and have a couple of questions > regarding the behaviour of this script. > > I've provided two possible patterns (re_US_money): > the one surrounded by the 'word boundary' meta sequence seems not to > work > while the other one does. I can't understand why the addition of the > word > boundary defeats the match. \b Word boundary. This is a zero-width assertion that matches only at the beginning or end of a word. A word is defined as a sequence of alphanumeric characters, so the end of a word is indicated by whitespace or a non-alphanumeric character.[http://docs.python.org/2/howto/regex.html] So I think it's because a dollar sign is not an alphanumeric character. >>> re.findall(r"\b\e\b", "d e f") ['e'] >>> re.findall(r"\b\$\b", "d $ f") [] >>> re.findall(r"\b\&\b", "d & f") [] How about this version (I like the re.VERBOSE/re.X flag!) import re import collections regex = r"""(?P\$) ??????????? (?P\d*) ??????????? (?:\.) ??????????? (?P\d{2})""" target = \ """Cost is $4.50. With a $.30 discount: Price is $4.15. The price could be less, say $4 or $4. Let's see how this plays out:? $4.50.60 """ Match = collections.namedtuple("Match", "sign dollars cents") matches = [Match(*match) for match in re.findall(regex, target, re.X)] for match in matches: ??? print repr(match.sign), repr(match.dollars), repr(match.cents) From nik at naturalnet.de Sat Aug 3 22:38:56 2013 From: nik at naturalnet.de (Dominik George) Date: Sat, 03 Aug 2013 22:38:56 +0200 Subject: [Tutor] re module- puzzling results when matching money In-Reply-To: <62d4c7f7cbc741fc3a60a31f6db63d85@sonic.net> References: <62d4c7f7cbc741fc3a60a31f6db63d85@sonic.net> Message-ID: Hi, \b is defined as all non-word characters, so it is the complement oft \w. \w is [A-Za-z0-9_-], so \b includes \$ and thus cuts off your group. -nik Alex Kleider schrieb: >#!/usr/bin/env python > >""" >I've been puzzling over the re module and have a couple of questions >regarding the behaviour of this script. > >I've provided two possible patterns (re_US_money): >the one surrounded by the 'word boundary' meta sequence seems not to >work >while the other one does. I can't understand why the addition of the >word >boundary defeats the match. > >I also don't understand why the split method includes the matched text. >Splitting only works as I would have expected if no goupings are used. > >If I've set this up as intended, the full body of this e-mail should be >executable as a script. > >Comments appreciated. >alex kleider >""" > ># file : tutor.py (Python 2.7, NOT Python 3) >print 'Running "tutor.py" on an Ubuntu Linux machine. *********' > >import re > >target = \ >"""Cost is $4.50. With a $.30 discount: >Price is $4.15. >The price could be less, say $4 or $4. >Let's see how this plays out: $4.50.60 >""" > ># Choose one of the following two alternatives: >re_US_money =\ >r"((?P\$)(?P\d{0,})(?:\.(?P\d{2})){0,1})" ># The above provides matches. ># The following does NOT. ># re_US_money =\ ># r"\b((?P\$)(?P\d{0,})(?:\.(?P\d{2})){0,1})\b" > >pat_object = re.compile(re_US_money) >match_object = pat_object.search(target) >if match_object: > print "'match_object.group()' and 'match_object.span()' yield:" > print match_object.group(), match_object.span() > print >else: > print "NO MATCH FOUND!!!" >print >print "Now will use 'finditer()':" > >print >iterator = pat_object.finditer(target) >i = 1 >for iter in iterator: > print > print "iter #%d: "%(i, ), > print iter.group() > print "'groups()' yields: '%s'."%(iter.groups(), ) > print iter.span() > i += 1 > sign = iter.group("sign") > dollars = iter.group("dollars") > cents = iter.group("cents") > print sign, > print " ", > if dollars: > print dollars, > else: > print "00", > print " ", > if cents: > print cents, > else: > print "00", > >print > >t = target >sub_target = pat_object.sub("", t) >print >print "Printing substitution: " >print sub_target >split_target = pat_object.split(target) >print "Result of splitting on the target: " >print split_target > ># End of script. >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >http://mail.python.org/mailman/listinfo/tutor -- Diese Nachricht wurde von meinem Android-Mobiltelefon mit K-9 Mail gesendet. -------------- next part -------------- An HTML attachment was scrubbed... URL: From akleider at sonic.net Sun Aug 4 01:34:34 2013 From: akleider at sonic.net (Alex Kleider) Date: Sat, 03 Aug 2013 16:34:34 -0700 Subject: [Tutor] re module- puzzling results when matching money In-Reply-To: <1375561814.54714.YahooMailNeo@web163801.mail.gq1.yahoo.com> References: <62d4c7f7cbc741fc3a60a31f6db63d85@sonic.net> <1375561814.54714.YahooMailNeo@web163801.mail.gq1.yahoo.com> Message-ID: On 2013-08-03 13:30, Albert-Jan Roskam wrote: > Word boundary. This is a zero-width assertion that matches only at the > beginning or end of a word. A word is defined as a sequence of > alphanumeric > characters, so the end of a word is indicated by whitespace or a > non-alphanumeric character.[http://docs.python.org/2/howto/regex.html] > So I think it's because a dollar sign is not an alphanumeric character. I get it now, thanks. > >>>> re.findall(r"\b\e\b", "d e f") ^ I'm puzzled by the presence of the '\' character before the 'e' above. Testing suggests that its presence or absence seems to make no difference. > ['e'] >>>> re.findall(r"\b\$\b", "d $ f") ^ Here it escapes the '$' which would otherwise be a metachar. > [] >>>> re.findall(r"\b\&\b", "d & f") ^ Here also I don't understand but again it seems not to matter. > [] > > > How about this version (I like the re.VERBOSE/re.X flag!) I am also now getting to like re.VERBOSE > > import re > import collections > > regex = r"""(?P\$) > ??????????? (?P\d*) > ??????????? (?:\.) > ??????????? (?P\d{2})""" > target = \ > """Cost is $4.50. With a $.30 discount: > Price is $4.15. > The price could be less, say $4 or $4. > Let's see how this plays out:? $4.50.60 > """ > Match = collections.namedtuple("Match", "sign dollars cents") > matches = [Match(*match) for match in re.findall(regex, target, re.X)] > for match in matches: > ??? print repr(match.sign), repr(match.dollars), repr(match.cents) 'collections' is new to me. A new topic to study. Thanks for the help, much appreciated! alex k From cybervigilante at gmail.com Sun Aug 4 03:14:31 2013 From: cybervigilante at gmail.com (Jim Mooney) Date: Sat, 3 Aug 2013 18:14:31 -0700 Subject: [Tutor] range as a not-quite-iterator Message-ID: using py3.3 on win7 I'm reading the Lutz book, and he clearly calls range an iterator in the context of Python 3.0, yet when I try x = range(1,10) next(x) I get: builtins.TypeError: 'range' object is not an iterator And x itself is returned as: range(1, 10) What am I missing here? Is it an iterator or not? -- Jim The Curiosity Rover has been on Mars a year. It feels like it landed, at most, a few months ago. Where is the time going? From amitsaha.in at gmail.com Sun Aug 4 03:27:45 2013 From: amitsaha.in at gmail.com (Amit Saha) Date: Sun, 4 Aug 2013 11:27:45 +1000 Subject: [Tutor] range as a not-quite-iterator In-Reply-To: References: Message-ID: On Sun, Aug 4, 2013 at 11:14 AM, Jim Mooney wrote: > using py3.3 on win7 > > I'm reading the Lutz book, and he clearly calls range an iterator in > the context of Python 3.0, yet when I try > > x = range(1,10) > next(x) > I get: builtins.TypeError: 'range' object is not an iterator > And x itself is returned as: range(1, 10) > > What am I missing here? Is it an iterator or not? I think what you are looking for is x.__iter__() >>> next(x.__iter__()) 1 Sorry for the insufficient explanation or background, but I don't quite recall it myself well enough. -- http://echorand.me From eryksun at gmail.com Sun Aug 4 03:32:19 2013 From: eryksun at gmail.com (eryksun) Date: Sat, 3 Aug 2013 21:32:19 -0400 Subject: [Tutor] range as a not-quite-iterator In-Reply-To: References: Message-ID: On Sat, Aug 3, 2013 at 9:14 PM, Jim Mooney wrote: > using py3.3 on win7 > > I'm reading the Lutz book, and he clearly calls range an iterator in > the context of Python 3.0, yet when I try > > x = range(1,10) > next(x) > I get: builtins.TypeError: 'range' object is not an iterator > And x itself is returned as: range(1, 10) > > What am I missing here? Is it an iterator or not? No, it's not an iterator. It's an iterable. This way it's simple to reuse a range object concurrently. For example, this creates 3 iterators and grabs values from each: from itertools import islice r = range(1, 10) it = [iter(r) for i in range(3)] >>> type(it[0]) >>> list(islice(it[0], 5)) [1, 2, 3, 4, 5] >>> list(islice(it[1], 7)) [1, 2, 3, 4, 5, 6, 7] >>> list(islice(it[2], 3)) [1, 2, 3] >>> list(it[0]), list(it[1]), list(it[2]) ([6, 7, 8, 9], [8, 9], [4, 5, 6, 7, 8, 9]) The range object itself doesn't have to keep track of the state of each iteration. It just spins off an iterator that handles it. Like most iterators these get used once and thrown away: >>> next(it[0], "It's dead, Jim") "It's dead, Jim" From steve at pearwood.info Sun Aug 4 04:03:06 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 04 Aug 2013 12:03:06 +1000 Subject: [Tutor] range as a not-quite-iterator In-Reply-To: References: Message-ID: <51FDB65A.8070908@pearwood.info> On 04/08/13 11:14, Jim Mooney wrote: > using py3.3 on win7 > > I'm reading the Lutz book, and he clearly calls range an iterator in > the context of Python 3.0, yet when I try > > x = range(1,10) > next(x) > I get: builtins.TypeError: 'range' object is not an iterator > And x itself is returned as: range(1, 10) > > What am I missing here? Is it an iterator or not? Technically, no. Informally, yes. Technically, there are three rules for something to be an iterator: * it must have an __iter__ method which returns itself; * it must have a __next__ method which returns each subsequent value; * if there are no more values, it must raise StopIteration (this is called the "iterator protocol"). In addition, well-behaved iterators must continue to always return StopIteration once they have become empty. An iterator that becomes empty, then becomes non-empty, is officially deemed to be "broken", although you are permitted to write broken iterators if you insist. So by this definition, range (or xrange in Python 2.x) is not an iterator, since it doesn't follow the iterator protocol. However, it does behave like an iterator, in the sense that it is a lazily generated sequence, so informally, it sometimes gets called one. If it quacks like an iterator, you can treat it as an iterator. Note that range objects have an __iter__ which doesn't return the range object itself, but an iterator wrapper around itself: py> r = range(2, 45, 3) py> it = iter(r) # calls __iter__ internally py> it is r False py> it py> r range(2, 45, 3) That wrapper object actually is a genuine iterator: py> iter(it) is it True The reason for this is partly historical (xrange goes all the way back to Python 1.4 or even older, while iterators only go back to about 2.2, if I remember correctly) and partly practical -- iterators can normally be used only once, while range objects can be re-used, and also can be sliced almost like a list. py> r[1:3] range(5, 11, 3) -- Steven From cybervigilante at gmail.com Sun Aug 4 04:01:11 2013 From: cybervigilante at gmail.com (Jim Mooney) Date: Sat, 3 Aug 2013 19:01:11 -0700 Subject: [Tutor] range as a not-quite-iterator In-Reply-To: References: Message-ID: On 3 August 2013 18:27, Amit Saha wrote: Oh, he clearly contradicts himself: >>> R = range(10) # range returns an iterator, not a list >>> R range(0, 10) >>> I = iter(R) # Make an iterator from the range He's saying R is an iterator, then he's saying iter makes it into an iterator. It makes no sense to make an iterator into an iterator, so the wording is wrong, somehow. One of the definitions is wrong or the other one needs qualification. But I'll go by the interpreter, which is the final judge. Besides, I can list(range(10)) all day long and not raise StopIteration, so it must not be an iterator, at least according to my understanding. I could have sworn range(10) was returning a range object def <...> before, when it's now just >>> range(10) range(0, 10) in the interpreter, but I may be misremembering something. Or this is a very recent change to Python. I'm using sys.version_info(major=3, minor=3, micro=2, releaselevel='final', serial=0) -- Jim The Curiosity Rover has been on Mars a year Huh? It feels like it landed, at most, a few months ago. Where in hell is the time going? From cybervigilante at gmail.com Sun Aug 4 04:17:14 2013 From: cybervigilante at gmail.com (Jim Mooney) Date: Sat, 3 Aug 2013 19:17:14 -0700 Subject: [Tutor] range as a not-quite-iterator In-Reply-To: <51FDB65A.8070908@pearwood.info> References: <51FDB65A.8070908@pearwood.info> Message-ID: On 3 August 2013 19:03, Steven D'Aprano wrote: while range objects can be re-used, and also can be sliced almost > like a list. > > py> r[1:3] > range(5, 11, 3) Ah, that makes them much more useful. I was wondering a while back if I had to go through the entire range to get part of the sequence, but didn't think of slicing the range since it seemed too dynamic. -- Jim The Curiosity Rover has been on Mars a year Huh? It feels like it landed, at most, a few months ago. Where in hell is the time going? From eryksun at gmail.com Sun Aug 4 04:25:29 2013 From: eryksun at gmail.com (eryksun) Date: Sat, 3 Aug 2013 22:25:29 -0400 Subject: [Tutor] range as a not-quite-iterator In-Reply-To: References: Message-ID: On Sat, Aug 3, 2013 at 9:32 PM, eryksun wrote: > The range object itself doesn't have to keep track of the state of > each iteration. It just spins off an iterator that handles it. Like > most iterators these get used once and thrown away: A common exception to this is the file type. A file is an iterator that can be rewound and reused (e.g. seek(0)). If you want concurrent access to a file, each with its own file pointer, then you have to open the file multiple times -- for the obvious reason that the operating system manages the underlying data structure. From fomcl at yahoo.com Sun Aug 4 09:21:07 2013 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Sun, 4 Aug 2013 00:21:07 -0700 (PDT) Subject: [Tutor] re module- puzzling results when matching money In-Reply-To: References: <62d4c7f7cbc741fc3a60a31f6db63d85@sonic.net> <1375561814.54714.YahooMailNeo@web163801.mail.gq1.yahoo.com> Message-ID: <1375600867.77340.YahooMailNeo@web163802.mail.gq1.yahoo.com> ----- Original Message ----- > From: Alex Kleider > To: Albert-Jan Roskam > Cc: Python Tutor > Sent: Sunday, August 4, 2013 1:34 AM > Subject: Re: [Tutor] re module- puzzling results when matching money > > On 2013-08-03 13:30, Albert-Jan Roskam wrote: > > >> Word boundary.? This is a zero-width assertion that matches only at the >> beginning or end of a word.? A word is defined as a sequence of >> alphanumeric >> characters, so the end of a word is indicated by whitespace or a >> non-alphanumeric character.[http://docs.python.org/2/howto/regex.html] >> So I think it's because a dollar sign is not an alphanumeric character. > > I get it now, thanks. > > >> >>>>> re.findall(r"\b\e\b", "d e f") > ? ? ? ? ? ? ? ? ? ? ^ > I'm puzzled by the presence of the '\' character before the > 'e' above. sorry, my bad. I forgot to delete that backslash, I meant re.findall(r"\be\b", "d e f"). Same with the other example. From nik at naturalnet.de Sun Aug 4 09:43:09 2013 From: nik at naturalnet.de (Dominik George) Date: Sun, 04 Aug 2013 09:43:09 +0200 Subject: [Tutor] re module- puzzling results when matching money In-Reply-To: <396ddd0ac9d7659c5b19e244203624ef@sonic.net> References: <62d4c7f7cbc741fc3a60a31f6db63d85@sonic.net> <396ddd0ac9d7659c5b19e244203624ef@sonic.net> Message-ID: <9ca17624-609e-4b4e-bc6f-a952f7ef25c9@email.android.com> Hi, not quite. The moral is to learn about greedy and non-greedy matching ;)! -nik Alex Kleider schrieb: >On 2013-08-03 13:38, Dominik George wrote: >> Hi, >> >> b is defined as all non-word characters, so it is the complement oft >> w. w is [A-Za-z0-9_-], so b includes $ and thus cuts off your >> group. >> >> -nik > >I get it now. I was using it before the '$' to define the beginning of > >a word but I think things are failing because it detects an end of >word. >Anyway, the moral is not to use it with anything but \w! > >Thanks! -- Diese Nachricht wurde von meinem Android-Mobiltelefon mit K-9 Mail gesendet. -------------- next part -------------- An HTML attachment was scrubbed... URL: From akleider at sonic.net Sun Aug 4 09:45:37 2013 From: akleider at sonic.net (Alex Kleider) Date: Sun, 04 Aug 2013 00:45:37 -0700 Subject: [Tutor] re module- puzzling results when matching money In-Reply-To: <1375600867.77340.YahooMailNeo@web163802.mail.gq1.yahoo.com> References: <62d4c7f7cbc741fc3a60a31f6db63d85@sonic.net> <1375561814.54714.YahooMailNeo@web163801.mail.gq1.yahoo.com> <1375600867.77340.YahooMailNeo@web163802.mail.gq1.yahoo.com> Message-ID: <8cdce2cf2f9176c0a0a0dcfcf38621eb@sonic.net> On 2013-08-04 00:21, Albert-Jan Roskam wrote: > ----- Original Message ----- > >> From: Alex Kleider >> To: Albert-Jan Roskam >> Cc: Python Tutor >> Sent: Sunday, August 4, 2013 1:34 AM >> Subject: Re: [Tutor] re module- puzzling results when matching money >> >> On 2013-08-03 13:30, Albert-Jan Roskam wrote: >> >> >>> Word boundary.? This is a zero-width assertion that matches only at >>> the >>> beginning or end of a word.? A word is defined as a sequence of >>> alphanumeric >>> characters, so the end of a word is indicated by whitespace or a >>> non-alphanumeric >>> character.[http://docs.python.org/2/howto/regex.html] >>> So I think it's because a dollar sign is not an alphanumeric >>> character. >> >> I get it now, thanks. >> >> >>> >>>>>> re.findall(r"\b\e\b", "d e f") >> ? ? ? ? ? ? ? ? ? ? ^ >> I'm puzzled by the presence of the '\' character before the >> 'e' above. > > sorry, my bad. I forgot to delete that backslash, I meant > re.findall(r"\be\b", "d e f"). Same with the other example. ..but the interesting thing is that the presence or absence of the spurious back slashes seems not to change the results. From alan.gauld at btinternet.com Sun Aug 4 10:30:57 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 04 Aug 2013 09:30:57 +0100 Subject: [Tutor] re module- puzzling results when matching money In-Reply-To: <8cdce2cf2f9176c0a0a0dcfcf38621eb@sonic.net> References: <62d4c7f7cbc741fc3a60a31f6db63d85@sonic.net> <1375561814.54714.YahooMailNeo@web163801.mail.gq1.yahoo.com> <1375600867.77340.YahooMailNeo@web163802.mail.gq1.yahoo.com> <8cdce2cf2f9176c0a0a0dcfcf38621eb@sonic.net> Message-ID: On 04/08/13 08:45, Alex Kleider wrote: >> sorry, my bad. I forgot to delete that backslash, I meant >> re.findall(r"\be\b", "d e f"). Same with the other example. > > ..but the interesting thing is that the presence or absence of the > spurious back slashes seems not to change the results. It wouldn't because the backslash says treat the next character as a literal and if its not a metacharacter its already treated as a literal. So the \ is effectively a non-operation in that context. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From tonifuente at yahoo.co.uk Mon Aug 5 11:56:27 2013 From: tonifuente at yahoo.co.uk (Antonio de la Fuente) Date: Mon, 5 Aug 2013 10:56:27 +0100 Subject: [Tutor] LDAP search with ldap library module failing Message-ID: <20130805095627.GA4138@cateto> Hello everybody, I am trying to do a ldap search against a LDAP server, like this: ld = ldap.initialize(ldapURI) ld.simple_bind_s(ldapBindDn,ldapBindPass) zones = ld.search_st(ldapBase,ldap.SCOPE_SUBTREE,'(&(objectclass=dnszone)(cn=*))',['dnszonename','modifytimestamp'],timeout=5) It times out after 5 seconds, and if I don't use timeout, it will hang up for ever. Now, if I search fog cn=ab*, a much limited search it will return the search results. Thinking that it could be a limitation from the server side, I've done the same search with ldapsearch in bash, and it gets what is expected. I am using ipython, and python 2.6 in a linux OS. My question is: How can I try to debug this issue? Thank you in advance fo your support, Antonio. Error output: In [122]: zones = ld.search_st(ldapBase,ldap.SCOPE_SUBTREE,'(&(objectclass=dnszone)(cn=*))',['dnszonename','modifytimestamp'],timeout=5) ERROR: An unexpected error occurred while tokenizing input The following traceback may be corrupted or invalid The error message is: ('EOF in multi-line statement', (379, 0)) --------------------------------------------------------------------------- TIMEOUT Traceback (most recent call last) /home/admin/afm/tinydns_databases/ in () /usr/lib64/python2.6/site-packages/ldap/ldapobject.pyc in search_st(self, base, scope, filterstr, attrlest, attrsonly, timeout) 517 518 def search_st(self,base,scope,filterstr='(objectClass=*)',attrlist=None,attrsonly=0,timeout=-1): --> 519 return self.search_ext_s(base,scope,filterstr,attrlist,attrsonly,None,None,timeout) 520 521 def set_cache_options(self,*args,**kwargs): [...] /usr/lib64/python2.6/site-packages/ldap/ldapobject.pyc in _ldap_call(self, func, *args, **kwargs) 94 try: 95 try: ---> 96 result = func(*args,**kwargs) 97 if __debug__ and self._trace_level>=2: 98 if func.__name__!="unbind_ext": From nik at naturalnet.de Mon Aug 5 12:45:42 2013 From: nik at naturalnet.de (Dominik George) Date: Mon, 5 Aug 2013 12:45:42 +0200 Subject: [Tutor] LDAP search with ldap library module failing In-Reply-To: <20130805095627.GA4138@cateto> References: <20130805095627.GA4138@cateto> Message-ID: <20130805104541.GE11510@keks.naturalnet.de> Hi, > Now, if I search fog cn=ab*, a much limited search it will return the search > results. Thinking that it could be a limitation from the server side, I've done > the same search with ldapsearch in bash, and it gets what is expected. Are you sure it gets what is expected? ldapsearch limits the results to 500 - maybe the LDAP library does not. How many entries do you expect? > I am using ipython, and python 2.6 in a linux OS. Why are you using such an outdated version? Any chance you upgrade to Python 2.7? > My question is: How can I try to debug this issue? There are several ways, Two are reading the LDAP server logs, and tcpdumping the connection to see if anything happens. I am pretty certain that this is not an issue with your code. > The error message is: ('EOF in multi-line statement', (379, 0)) > --> 519 return self.search_ext_s(base,scope,filterstr,attrlist,attrsonly,None,None,timeout) This is why I say "never use ipython". The error message is complete non-sense. -nik -- * concerning Mozilla code leaking assertion failures to tty without D-BUS * That means, D-BUS is a tool that makes software look better than it actually is. PGP-Fingerprint: 3C9D 54A4 7575 C026 FB17 FD26 B79A 3C16 A0C4 F296 -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 905 bytes Desc: Digital signature URL: From tonifuente at yahoo.co.uk Mon Aug 5 13:57:20 2013 From: tonifuente at yahoo.co.uk (Antonio de la Fuente) Date: Mon, 5 Aug 2013 12:57:20 +0100 Subject: [Tutor] LDAP search with ldap library module failing In-Reply-To: <20130805104541.GE11510@keks.naturalnet.de> References: <20130805095627.GA4138@cateto> <20130805104541.GE11510@keks.naturalnet.de> Message-ID: <20130805115720.GA4863@cateto> * Dominik George [2013-08-05 12:45:42 +0200]: > Date: Mon, 5 Aug 2013 12:45:42 +0200 > From: Dominik George > To: Antonio de la Fuente , tutor at python.org > Subject: Re: [Tutor] LDAP search with ldap library module failing > User-Agent: Mutt/1.5.21 (2010-09-15) > Message-ID: <20130805104541.GE11510 at keks.naturalnet.de> > > Hi, > > > Now, if I search fog cn=ab*, a much limited search it will return the search > > results. Thinking that it could be a limitation from the server side, I've done > > the same search with ldapsearch in bash, and it gets what is expected. > > Are you sure it gets what is expected? ldapsearch limits the results to > 500 - maybe the LDAP library does not. How many entries do you expect? > It doesn't look that ldapsearch is limiting the results to 500 hundred. Around the 40000ish entries... And that is what ldapsearch returns, 48522 entries. > > I am using ipython, and python 2.6 in a linux OS. > > Why are you using such an outdated version? Any chance you upgrade to > Python 2.7? > I am going to run the script in a CentOS 6.4 box, and that is its python version. > > My question is: How can I try to debug this issue? > > There are several ways, Two are reading the LDAP server logs, and > tcpdumping the connection to see if anything happens. I am pretty > certain that this is not an issue with your code. > I'll try that. > > The error message is: ('EOF in multi-line statement', (379, 0)) > > > --> 519 return self.search_ext_s(base,scope,filterstr,attrlist,attrsonly,None,None,timeout) > > This is why I say "never use ipython". The error message is complete > non-sense. > I will also try with IDLE. > -nik > Thank you nik. > -- > * concerning Mozilla code leaking assertion failures to tty without D-BUS * > That means, D-BUS is a tool that makes software look better > than it actually is. > > PGP-Fingerprint: 3C9D 54A4 7575 C026 FB17 FD26 B79A 3C16 A0C4 F296 From davea at davea.name Mon Aug 5 14:16:58 2013 From: davea at davea.name (Dave Angel) Date: Mon, 5 Aug 2013 12:16:58 +0000 (UTC) Subject: [Tutor] LDAP search with ldap library module failing References: <20130805095627.GA4138@cateto> <20130805104541.GE11510@keks.naturalnet.de> <20130805115720.GA4863@cateto> Message-ID: Antonio de la Fuente wrote: > * Dominik George [2013-08-05 12:45:42 +0200]: > >> > The error message is: ('EOF in multi-line statement', (379, 0)) >> >> > --> 519 return self.search_ext_s(base,scope,filterstr,attrlist,attrsonly,None,None,timeout) >> >> This is why I say "never use ipython". The error message is complete >> non-sense. >> > > I will also try with IDLE. Consider using the shell window and just running python. it seems like every IDE or pseudo-IDE tries to be helpful, and frequently misses the mark. The terminal will show you the unmodified traceback, I'm not saying never use an IDE. A good one is very useful for certain things. But when it interferes with the job, run without it. -- DaveA From rdmoores at gmail.com Mon Aug 5 14:51:18 2013 From: rdmoores at gmail.com (Richard D. Moores) Date: Mon, 5 Aug 2013 05:51:18 -0700 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? Message-ID: >>> import fractions >>> fractions.Fraction(6, 21) Fraction(2, 7) How do I turn that Fraction(2, 7) into "1/7"? (and not 0.2857142857142857...) Or do I have to employ fractions.gcd? I can't seem to find the answer in the doc at Thanks, DIck Moores From nik at naturalnet.de Mon Aug 5 14:56:33 2013 From: nik at naturalnet.de (Dominik George) Date: Mon, 05 Aug 2013 14:56:33 +0200 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: Message-ID: <67bc14d5-8dd9-45f4-a5db-cd837dfe515d@email.android.com> Hi, how about casting to str()? -nik "Richard D. Moores" schrieb: >>>> import fractions >>>> fractions.Fraction(6, 21) >Fraction(2, 7) > >How do I turn that Fraction(2, 7) into "1/7"? (and not >0.2857142857142857...) > >Or do I have to employ fractions.gcd? > >I can't seem to find the answer in the doc at > > >Thanks, > >DIck Moores >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >http://mail.python.org/mailman/listinfo/tutor -- Diese Nachricht wurde von meinem Android-Mobiltelefon mit K-9 Mail gesendet. -------------- next part -------------- An HTML attachment was scrubbed... URL: From rdmoores at gmail.com Mon Aug 5 15:09:32 2013 From: rdmoores at gmail.com (Richard D. Moores) Date: Mon, 5 Aug 2013 06:09:32 -0700 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: <67bc14d5-8dd9-45f4-a5db-cd837dfe515d@email.android.com> References: <67bc14d5-8dd9-45f4-a5db-cd837dfe515d@email.android.com> Message-ID: On Mon, Aug 5, 2013 at 5:56 AM, Dominik George wrote: > Hi, > > how about casting to str()? > > -nik Thanks very much Nik. I should have tried that. >>> from fractions import Fraction >>> str(Fraction(6,21)) '2/7' Dick From amitsaha.in at gmail.com Mon Aug 5 15:15:34 2013 From: amitsaha.in at gmail.com (Amit Saha) Date: Mon, 5 Aug 2013 23:15:34 +1000 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: Message-ID: On Mon, Aug 5, 2013 at 10:51 PM, Richard D. Moores wrote: >>>> import fractions >>>> fractions.Fraction(6, 21) > Fraction(2, 7) > > How do I turn that Fraction(2, 7) into "1/7"? (and not 0.2857142857142857...) I think you meant, 2/7? Here is how you can extract the numerator and the denominator (as your subject states): >>> fractions.Fraction(6,21) Fraction(2, 7) >>> f=fractions.Fraction(6,21) >>> f Fraction(2, 7) >>> f.numerator 2 >>> f.denominator 7 Also, of course, str(f) would give you 2/7. -- http://echorand.me From marc at marcd.org Mon Aug 5 13:56:22 2013 From: marc at marcd.org (Marc) Date: Mon, 5 Aug 2013 07:56:22 -0400 Subject: [Tutor] Tutor Digest, Vol 114, Issue 11 In-Reply-To: References: Message-ID: > > I am trying to do a ldap search against a LDAP server, like this: > > ld = ldap.initialize(ldapURI) > ld.simple_bind_s(ldapBindDn,ldapBindPass) > zones = > ld.search_st(ldapBase,ldap.SCOPE_SUBTREE,'(&(objectclass=dnszone)(cn=*))',['dnszonename','modifytimestamp'],timeout=5) > > It times out after 5 seconds, and if I don't use timeout, it will hang > up for ever. > > Now, if I search fog cn=ab*, a much limited search it will return the > search > results. Thinking that it could be a limitation from the server side, I've > done > the same search with ldapsearch in bash, and it gets what is expected. > > I am using ipython, and python 2.6 in a linux OS. > > My question is: How can I try to debug this issue? > > Thank you in advance fo your support, > Antonio. > > > Error output: > > In [122]: zones = > ld.search_st(ldapBase,ldap.SCOPE_SUBTREE,'(&(objectclass=dnszone)(cn=*))',['dnszonename','modifytimestamp'],timeout=5) > ERROR: An unexpected error occurred while tokenizing input > The following traceback may be corrupted or invalid > The error message is: ('EOF in multi-line statement', (379, 0)) > > --------------------------------------------------------------------------- > TIMEOUT Traceback (most recent call > last) > > /home/admin/afm/tinydns_databases/ in () > > /usr/lib64/python2.6/site-packages/ldap/ldapobject.pyc in search_st(self, > base, scope, filterstr, attrlest, attrsonly, timeout) > 517 > 518 def > search_st(self,base,scope,filterstr='(objectClass=*)',attrlist=None,attrsonly=0,timeout=-1): > --> 519 return > self.search_ext_s(base,scope,filterstr,attrlist,attrsonly,None,None,timeout) > 520 > 521 def set_cache_options(self,*args,**kwargs): > > > [...] > > > /usr/lib64/python2.6/site-packages/ldap/ldapobject.pyc in _ldap_call(self, > func, *args, **kwargs) > 94 try: > 95 try: > ---> 96 result = func(*args,**kwargs) > 97 if __debug__ and self._trace_level>=2: > 98 if func.__name__!="unbind_ext": Hi - An example of the code (relevant snippet) I use is below - a little different technique, but maybe it will help you figure it out (disclaimer: I am by NO means an expert with Python - but I can get by): import ldap import sys import string if __name__ == "__main__": try: outfile = open("LDAPOut.txt",'w') outfile.write("CN|Created|LastLogin|Login\n") con = ldap.initialize("") con.simple_bind(who="cn=,o=", cred="") print "success" except: print "failure" res = con.search_s("ou=,o=", ldap.SCOPE_SUBTREE, 'objectClass=user' ,['createTimestamp','lastLoginTime','LoginTime']) Manipulate the 'res =' statement to get the attributes you want. From rdmoores at gmail.com Mon Aug 5 16:34:21 2013 From: rdmoores at gmail.com (Richard D. Moores) Date: Mon, 5 Aug 2013 07:34:21 -0700 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: Message-ID: On Mon, Aug 5, 2013 at 6:15 AM, Amit Saha wrote: > On Mon, Aug 5, 2013 at 10:51 PM, Richard D. Moores wrote: >>>>> import fractions >>>>> fractions.Fraction(6, 21) >> Fraction(2, 7) >> >> How do I turn that Fraction(2, 7) into "1/7"? (and not 0.2857142857142857...) > > I think you meant, 2/7? Yes, sorry for screwing that up. Here is how you can extract the numerator and > the denominator (as your subject states): > >>>> fractions.Fraction(6,21) > Fraction(2, 7) >>>> f=fractions.Fraction(6,21) >>>> f > Fraction(2, 7) > >>>> f.numerator > 2 >>>> f.denominator > 7 Ah, better yet! Thanks! But where could I have found that in ? Dick From kwpolska at gmail.com Mon Aug 5 16:40:04 2013 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Mon, 5 Aug 2013 16:40:04 +0200 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: Message-ID: On Mon, Aug 5, 2013 at 4:34 PM, Richard D. Moores wrote: > On Mon, Aug 5, 2013 at 6:15 AM, Amit Saha wrote: >> On Mon, Aug 5, 2013 at 10:51 PM, Richard D. Moores wrote: >>>>>> import fractions >>>>>> fractions.Fraction(6, 21) >>> Fraction(2, 7) >>> >>> How do I turn that Fraction(2, 7) into "1/7"? (and not 0.2857142857142857...) >> >> I think you meant, 2/7? > > Yes, sorry for screwing that up. > > Here is how you can extract the numerator and >> the denominator (as your subject states): >> >>>>> fractions.Fraction(6,21) >> Fraction(2, 7) >>>>> f=fractions.Fraction(6,21) >>>>> f >> Fraction(2, 7) >> >>>>> f.numerator >> 2 >>>>> f.denominator >> 7 > > Ah, better yet! Thanks! But where could I have found that in > ? > > Dick > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > The Fraction class inherits from the abstract base class numbers.Rational, and implements all of the methods and operations from that class. [?] In addition, Fraction has the following methods: This basically translates to ?go look numbers.Rational up if you want more methods/properties?. numbers.Rational is there: http://docs.python.org/3/library/numbers.html#numbers.Rational -- Chris ?Kwpolska? Warrick PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense From amitsaha.in at gmail.com Mon Aug 5 17:49:25 2013 From: amitsaha.in at gmail.com (Amit Saha) Date: Tue, 6 Aug 2013 01:49:25 +1000 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: Message-ID: On Tue, Aug 6, 2013 at 12:34 AM, Richard D. Moores wrote: > On Mon, Aug 5, 2013 at 6:15 AM, Amit Saha wrote: >> On Mon, Aug 5, 2013 at 10:51 PM, Richard D. Moores wrote: >>>>>> import fractions >>>>>> fractions.Fraction(6, 21) >>> Fraction(2, 7) >>> >>> How do I turn that Fraction(2, 7) into "1/7"? (and not 0.2857142857142857...) >> >> I think you meant, 2/7? > > Yes, sorry for screwing that up. > > Here is how you can extract the numerator and >> the denominator (as your subject states): >> >>>>> fractions.Fraction(6,21) >> Fraction(2, 7) >>>>> f=fractions.Fraction(6,21) >>>>> f >> Fraction(2, 7) >> >>>>> f.numerator >> 2 >>>>> f.denominator >> 7 > > Ah, better yet! Thanks! But where could I have found that in > ? Sometimes, you may also want to do dir() on an object to see what attributes/methods it has/supports. -- http://echorand.me From alan.gauld at btinternet.com Mon Aug 5 19:18:13 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 05 Aug 2013 18:18:13 +0100 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: Message-ID: On 05/08/13 16:49, Amit Saha wrote: >> >> Ah, better yet! Thanks! But where could I have found that in >> ? > > Sometimes, you may also want to do dir() on an object to see what > attributes/methods it has/supports. Also >>> help(fractions.Fraction) yields lots of details including the following: ... | ---------------------------------------------------------------------- | Data descriptors defined here: | | denominator | | numerator | HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From ryan.waples at gmail.com Mon Aug 5 21:17:39 2013 From: ryan.waples at gmail.com (Ryan Waples) Date: Mon, 5 Aug 2013 12:17:39 -0700 Subject: [Tutor] Start multiple threads from Python Message-ID: Python 2.7.x on Windows 7. I'm looking for a bit of advice, not sure how to proceed. With Python I am generating a file with a bunch of data in it. I want to analyse the data in this file with three separate programs. Each of these programs is single threaded needs only read access to the data, and they do not depend on each other. Currently I am calling each analysis program one at a time with subprocess.call(). This is working without a hitch, but as each analysis can take a while to run, I want to try to speed things up. I realize I can start three different python sessions to do this, but that just begs the question how to do that from python? How can I start multiple independent programs (as in subprocess.call()) without waiting for them to finish? -Thanks Ryan -------------- next part -------------- An HTML attachment was scrubbed... URL: From ryan.waples at gmail.com Mon Aug 5 21:28:48 2013 From: ryan.waples at gmail.com (Ryan Waples) Date: Mon, 5 Aug 2013 12:28:48 -0700 Subject: [Tutor] Start multiple threads from Python In-Reply-To: <20130805192630.GA1345@gopher> References: <20130805192630.GA1345@gopher> Message-ID: Thanks, that may be just what I'm looking for. -ryan On Mon, Aug 5, 2013 at 12:26 PM, Chris Down wrote: > On 2013-08-05 12:17, Ryan Waples wrote: > > Currently I am calling each analysis program one at a time with > > subprocess.call(). This is working without a hitch, but as each analysis > > can take a while to run, I want to try to speed things up. I realize I > can > > start three different python sessions to do this, but that just begs the > > question how to do that from python? > > subprocess.Popen does not block unless you explicitly tell it to (by using > communicate()). Perhaps that's what you want. > > >>> import subprocess > >>> x = subprocess.Popen([ "sleep", "60" ]) > >>> y = subprocess.Popen([ "sleep", "60" ]) > >>> x.pid > 3035 > >>> y.pid > 3036 > -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Mon Aug 5 22:50:19 2013 From: eryksun at gmail.com (eryksun) Date: Mon, 5 Aug 2013 16:50:19 -0400 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: Message-ID: On Mon, Aug 5, 2013 at 10:34 AM, Richard D. Moores wrote: >>>>> f.numerator >> 2 >>>>> f.denominator >> 7 > > Ah, better yet! Thanks! But where could I have found that in > ? Start with help(fractions), dir(fractions), and dir(fractions.Fraction). If that doesn't answer your question, then scan over the source: >>> import fractions, inspect, pydoc >>> pydoc.pager(inspect.getsource(fractions)) A quick scan comes up with the following: __slots__ = ('_numerator', '_denominator') @property def numerator(a): return a._numerator @property def denominator(a): return a._denominator def __str__(self): """str(self)""" if self._denominator == 1: return str(self._numerator) else: return '%s/%s' % (self._numerator, self._denominator) More interesting is the reason it uses a property without a setter, which makes the fraction 'immutable' -- as long as you don't tamper with the private attributes. The numerator and denominator values are used to compute the fraction's hash, such that the hash is the same for an equal number (int, float, Fraction, Decimal). So a Fraction can be used as a dict key: >>> frac = Fraction(1) >>> d = {frac: 1} >>> frac in d True The key to fetch the value can be any equal number: >>> d[1], d[1.0], d[Decimal(1)], d[Fraction(1)] (1, 1, 1, 1) The value has to be immutable because mutating it (and thus the hash) would leave the dict in an inconsistent state: >>> frac._numerator = 2 >>> list(d)[0] is frac True >>> frac in d False >>> Fraction(2) in d False >>> Fraction(1) in d False After mutating the value, frac has the hash of Fraction(2), but it's still at the position for Fraction(1) in the dict's hash table. Looking for frac or Fraction(2) just finds an empty slot. Looking for Fraction(1) finds frac in the table, but since it could just be a hash collision it has to check for equality -- but frac no longer equals Fraction(1). From rdmoores at gmail.com Tue Aug 6 00:37:58 2013 From: rdmoores at gmail.com (Richard D. Moores) Date: Mon, 5 Aug 2013 15:37:58 -0700 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: Message-ID: My thanks to all for the great help. Yay Tutors! Dick Moores From sbjaved at gmail.com Tue Aug 6 00:48:16 2013 From: sbjaved at gmail.com (Saad Javed) Date: Tue, 6 Aug 2013 03:48:16 +0500 Subject: [Tutor] adding users to tweets on a list Message-ID: I want to add users to the tweet from the list, the no. of users added based on the length of the tweet. #!/usr/bin/env python lst = ['@saad', '@asad', '@sherry', '@danny', '@ali', '@hasan', '@adil', '@yousaf', '@maria', '@bilal', '@owais'] #string = raw_input('enter string: ') #example string string = 'These are my friends living in the same city as i am. I have known them for years. They are good people in general. They are:' limit = 140 remaining = limit - len(string) print remaining, '\n' running = True while running: if len(lst) >=3 and len(lst[0]) + len(lst[1]) + len(lst[2]) <= remaining: print string, lst[0], lst[1], lst[2] del lst[:3] if len(lst) < 3: pass elif len(lst) >=2 and len(lst[0]) + len(lst[1]) <= remaining: print string, lst[0], lst[1] del lst[:2] if len(lst) < 2: pass elif len(lst) >=1 and len(lst[0]) <= remaining: print string, lst[0] del lst[0] if len(lst) < 1: pass else: print 'no room for friends' running = False This code works but it can add a maximum of 3 users to the tweet at a time. Is there a way to calculate how many users would fit into the remaining space (i.e. 140 - len(tweet)) and add that no. of users from the list to the tweet? From cybervigilante at gmail.com Tue Aug 6 05:50:16 2013 From: cybervigilante at gmail.com (Jim Mooney) Date: Mon, 5 Aug 2013 20:50:16 -0700 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: <67bc14d5-8dd9-45f4-a5db-cd837dfe515d@email.android.com> Message-ID: On 5 August 2013 06:09, Richard D. Moores wrote: > On Mon, Aug 5, 2013 at 5:56 AM, Dominik George wrote: >> Hi, >> >> how about casting to str()? Or, to make it even more usable and get numbers back: f = [int(x) for x in str(fractions.Fraction(6,21)).split('/')] [2, 7] Jim From eryksun at gmail.com Tue Aug 6 06:21:43 2013 From: eryksun at gmail.com (eryksun) Date: Tue, 6 Aug 2013 00:21:43 -0400 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: <67bc14d5-8dd9-45f4-a5db-cd837dfe515d@email.android.com> Message-ID: On Mon, Aug 5, 2013 at 11:50 PM, Jim Mooney wrote: > Or, to make it even more usable and get numbers back: > > f = [int(x) for x in str(fractions.Fraction(6,21)).split('/')] > [2, 7] This looks like a job for IterableFraction: class IterableFraction(Fraction): def __iter__(self): yield self.numerator yield self.denominator >>> list(IterableFraction(6, 21)) [2, 7] j/k :) From cybervigilante at gmail.com Tue Aug 6 09:16:24 2013 From: cybervigilante at gmail.com (Jim Mooney) Date: Tue, 6 Aug 2013 00:16:24 -0700 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: <67bc14d5-8dd9-45f4-a5db-cd837dfe515d@email.android.com> Message-ID: On 5 August 2013 21:21, eryksun wrote: > This looks like a job for IterableFraction: No, it's a job for SpammyFraction ;') from fractions import Fraction class SpammyFraction(Fraction): def spam(self): return [self.numerator, self.denominator] >>>SpammyFraction(6, 21).spam() [2, 7] Seriously, though, I thought yield was like return - one and you're done - how are you getting two yields in there? -- Jim The Curiosity Rover has been on Mars a year Huh? It feels like it landed, at most, a few months ago. Where in hell is the time going? From fomcl at yahoo.com Tue Aug 6 09:30:43 2013 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Tue, 6 Aug 2013 00:30:43 -0700 (PDT) Subject: [Tutor] managing version numbers (with versioneer?) Message-ID: <1375774243.82906.YahooMailNeo@web163802.mail.gq1.yahoo.com> Hi, ? I was wondering what is the best way to manage version strings during the release process. I was browsing on the web and I found out about the 'versioneer' package, which seems very promising: "[During the creation of a bdist/sdist] they invoke ?git describe? (with ?tags ?always ?dirty) to come up with a fine-grained version string. If you?re sitting on a tag, you get just ?1.4?. If you?re after a tag, you?ll get something like ?1.4-8-gf7283c2?, which means there are 8 commits after the 1.4 tag, and the abbreviated SHA1 revision ID is f7283c2. And if your tree has uncommitted changes, you?ll get ?1.4-8-gf7283c2-dirty?. "?[http://blog.mozilla.org/warner/2012/01/31/version-string-management-in-python-introducing-python-versioneer/] What are the best practices here? Is there some, possibly better, alternative to versioneer? I am using git on linux ubuntu 12, and setuptools with Python 2.7.3. Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? From alan.gauld at btinternet.com Tue Aug 6 09:58:31 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 06 Aug 2013 08:58:31 +0100 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: <67bc14d5-8dd9-45f4-a5db-cd837dfe515d@email.android.com> Message-ID: On 06/08/13 08:16, Jim Mooney wrote: >> class IterableFraction(Fraction): >> def __iter__(self): >> yield self.numerator >> yield self.denominator > Seriously, though, I thought yield was like return - one and you're > done - how are you getting two yields in there? yield acts like a return but also a freeze frame. So the first time you call the function it runs till it hits yield. The next time you call the function it picks up at the yield and continues from that point. In this case to the next yield. The usual paradigm is to have the yield inside a loop such that every call returns the next result of a loop. You could do that here too but its shorter to use 2 yields: def __iter__(self): for value in [self.numerator, self.denominator] yield value hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Tue Aug 6 10:26:35 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 6 Aug 2013 18:26:35 +1000 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: <67bc14d5-8dd9-45f4-a5db-cd837dfe515d@email.android.com> Message-ID: <20130806082634.GD17751@ando> On Tue, Aug 06, 2013 at 08:58:31AM +0100, Alan Gauld wrote: > On 06/08/13 08:16, Jim Mooney wrote: > > >> class IterableFraction(Fraction): > >> def __iter__(self): > >> yield self.numerator > >> yield self.denominator > > >Seriously, though, I thought yield was like return - one and you're > >done - how are you getting two yields in there? > > yield acts like a return but also a freeze frame. > So the first time you call the function it runs till it hits yield. The > next time you call the function it picks up at the yield and continues > from that point. In this case to the next yield. All this is correct, but yield is more powerful than that. Not only does yield get used to return values from a function, it also gets used to send values *into* a running function. py> def cr(): # Co-Routine. ... x = yield() ... while True: ... x = yield(x + 1) ... py> magic = cr() py> magic.send(None) () py> magic.send(1) 2 py> magic.send(2) 3 py> magic.send(99) 100 py> magic.send(3) 4 Prepare to have your mind expanded, possibly until it leaks out your ears :-) -- Steven From alan.gauld at btinternet.com Tue Aug 6 10:44:51 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 06 Aug 2013 09:44:51 +0100 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: <20130806082634.GD17751@ando> References: <67bc14d5-8dd9-45f4-a5db-cd837dfe515d@email.android.com> <20130806082634.GD17751@ando> Message-ID: On 06/08/13 09:26, Steven D'Aprano wrote: > All this is correct, but yield is more powerful than that. Not only does > yield get used to return values from a function, it also gets used to > send values *into* a running function. Yikes. This is new to me too. When did that happen? Has yield always had these two way powers? > py> def cr(): # Co-Routine. > ... x = yield() > ... while True: > ... x = yield(x + 1) > ... > py> magic = cr() > py> magic.send(None) > () > py> magic.send(1) > 2 > > Prepare to have your mind expanded I have some reading to do... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From wprins at gmail.com Tue Aug 6 11:01:55 2013 From: wprins at gmail.com (Walter Prins) Date: Tue, 6 Aug 2013 10:01:55 +0100 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: <67bc14d5-8dd9-45f4-a5db-cd837dfe515d@email.android.com> <20130806082634.GD17751@ando> Message-ID: Hi Steven, Alan, On 6 August 2013 09:44, Alan Gauld wrote: > On 06/08/13 09:26, Steven D'Aprano wrote: > > All this is correct, but yield is more powerful than that. Not only does >> yield get used to return values from a function, it also gets used to >> send values *into* a running function. >> > > Yikes. This is new to me too. > When did that happen? Has yield always had these two way powers? > No, but having done a search myself about this just now, apparently since about 2005: http://www.python.org/dev/peps/pep-0342/ > > py> def cr(): # Co-Routine. >> ... x = yield() >> ... while True: >> ... x = yield(x + 1) >> ... >> py> magic = cr() >> py> magic.send(None) >> () >> py> magic.send(1) >> 2 >> >> Prepare to have your mind expanded >> > Wow, I like you Alan was aware of the basics of yield but not send etc..., this is rather neat. So you can also simplistically think of "send" and yield as akin to threaded execution, where cr() is like a thread that blocks at the yield call after creation, and then when you call on magic.send() the "thread" wakes up and continues and eventually returns another value to the "calling thread" and itself blocks again at the yield? All without using real threads anywhere? Very very cool.... Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From sbjaved at gmail.com Tue Aug 6 11:40:14 2013 From: sbjaved at gmail.com (Saad Javed) Date: Tue, 6 Aug 2013 14:40:14 +0500 Subject: [Tutor] adding users to tweets on a list In-Reply-To: <20130806091523.GD960@gopher> References: <20130806091523.GD960@gopher> Message-ID: > It looks like you're using Python 2, but you didn't specify. > > I'd probably do something like this: > > #!/usr/bin/env python > > MAX_LENGTH = 140 > > users = [ > "saad", "asad", "sherry", "danny", "ali", "hasan", "adil", > "yousaf", > "maria", "bilal", "owais", > ] > > def populate(): > message = raw_input("Enter string: ") > while users: > new_message = " ".join([message, "@" + users.pop(0)]) > if len(new_message) > MAX_LENGTH: > break > message = new_message > return message > > if __name__ == "__main__": > print(populate()) > It will add max no. of users to one tweet until limit is reached. I want all users added to the tweet. E.g. if 4 users can be added to the tweet before reaching the limit, return three tweets...first two with 4 users attached and the last one with three. Think of it as sending the same tweet with all the users in the list mentioned. The code I posted does that but can only add 3 users at a time. I want it to be able to calculate maximum no of users it can attach to the tweet and then keep doing it until the list runs out. Hope i've explained my problem. And yes, i'm using python 2. Saad -------------- next part -------------- An HTML attachment was scrubbed... URL: From tonifuente at yahoo.co.uk Tue Aug 6 12:01:59 2013 From: tonifuente at yahoo.co.uk (Antonio de la Fuente) Date: Tue, 6 Aug 2013 11:01:59 +0100 Subject: [Tutor] LDAP search with ldap library module failing In-Reply-To: References: <20130805095627.GA4138@cateto> <20130805104541.GE11510@keks.naturalnet.de> <20130805115720.GA4863@cateto> Message-ID: <20130806100158.GA3584@cateto> * Dave Angel [2013-08-05 12:16:58 +0000]: > > * Dominik George [2013-08-05 12:45:42 +0200]: > > > > >> > The error message is: ('EOF in multi-line statement', (379, 0)) > >> > >> > --> 519 return self.search_ext_s(base,scope,filterstr,attrlist,attrsonly,None,None,timeout) > >> > >> This is why I say "never use ipython". The error message is complete > >> non-sense. > >> > > > > I will also try with IDLE. > > Consider using the shell window and just running python. it seems like > every IDE or pseudo-IDE tries to be helpful, and frequently misses the > mark. The terminal will show you the unmodified traceback, > > I'm not saying never use an IDE. A good one is very useful for certain > things. > > But when it interferes with the job, run without it. > It seems all it was to blame is my impatience :-/ On the LDAP server I've run tcpdump catching anything that came from the server were I am running the script, source IP: # tcpdump -vvv src [IP address] I wrote the python statements on a file and I've run it on the bash shell. I've also changed the LDAP object search so I didn't include a timeout argument, as I could see that when running the search, there was periods of up to 30 seconds where the connection was idle, tcpdump didn't spit anything: ld.search_s(ldapBase,ldap.SCOPE_SUBTREE,'(&(objectclass=dnszone)(cn=*))',['dnszonename','modifytimestamp']) After around 10 minutes there were the expected results. Then I tried with iPython and IDLE, both of then also gave me the expected result. Thank you. -- Toni M?s vale una palabra a tiempo que cien a destiempo. -- Miguel de Cervantes Saavedra. (1547-1616) Escritor espa?ol. From oscar.j.benjamin at gmail.com Tue Aug 6 16:40:22 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 6 Aug 2013 15:40:22 +0100 Subject: [Tutor] hi In-Reply-To: <000601ce8eaa$c6baabb0$54300310$@orange.mu> References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> Message-ID: On 1 August 2013 12:32, Vick wrote: > Hi, Hi Vick, sorry I've been away and I've only had a chance to look at this now. > As per your request below, I have attached a stand-alone example (test3d.py) > of my problem. I am trying to plot in 3D using ion() from a loop. Basically don't use ion() for animation: it is intended for interactive use. Use matplotlib's animation API for animation. See here: http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ I've put example scripts below. Both run fine on this computer. You'll want a recent matplotlib version. Here's a 2d animation script: #!/usr/bin/env python # 2d animation import numpy as np from matplotlib import pyplot as plt from matplotlib import animation fig = plt.figure(figsize=(5, 5)) ax = fig.add_axes([0.15, 0.15, 0.70, 0.70]) line, = ax.plot([], [], linewidth=2) def init(): ax.set_xlim([-1, 1]) ax.set_ylim([-1, 1]) line.set_data([], []) return line, def animate(i): t = dt * np.arange(i) x = np.cos(omega * t) y = np.sin(omega * t) line.set_data(x, y) return line, dt = .02 # in seconds T = 10 # Period (seconds) omega = 2 * np.pi / T # 0.1 Hz anim = animation.FuncAnimation(fig, animate, init_func=init, frames=int(T/dt), interval=int(1000*dt), blit=True) plt.show() And here's a 3d script: #!/usr/bin/env python # 3d animation import numpy as np from matplotlib import pyplot as plt from matplotlib import animation from mpl_toolkits.mplot3d import Axes3D fig = plt.figure(figsize=(5, 5)) ax = fig.add_axes([0.15, 0.15, 0.70, 0.70], projection='3d') line, = ax.plot([], [], [], linewidth=2) def init(): ax.set_xlim([-1, 1]) ax.set_ylim([-1, 1]) ax.set_zlim([0, 10]) line.set_data([], []) return line, def animate(i): t = dt * np.arange(i) x = np.cos(omega * t) y = np.sin(omega * t) z = t line.set_data(x, y) line.set_3d_properties(z) # WTF! return line, dt = .02 # in seconds T = 10 # Duration (seconds) omega = 2 * np.pi # 1 Hz anim = animation.FuncAnimation(fig, animate, init_func=init, frames=int(T/dt), interval=int(1000*dt), blit=True) plt.show() Oscar From alan.gauld at btinternet.com Tue Aug 6 18:33:22 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 06 Aug 2013 17:33:22 +0100 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: <67bc14d5-8dd9-45f4-a5db-cd837dfe515d@email.android.com> <20130806082634.GD17751@ando> Message-ID: On 06/08/13 10:01, Walter Prins wrote: > py> def cr(): # Co-Routine. > ... x = yield() > ... while True: > ... x = yield(x + 1) > ... > "send" and yield as akin to threaded execution, where cr() is like a > thread that blocks at the yield call after creation, and then when you > call on magic.send() the "thread" wakes up and continues and eventually > returns another value It's clever but I'm not keen on it overloading yield to do it. yield as a word conveys (to me at least) the idea of returning a value but not quite completely ending. This usage sounds like a different concept and I'd have preferred a more explicit name - although I can't think what!! Also I'm not keen on the argument/parameter mechanism here either. Arguments are sent but not explicitly declared in the receiver, that all feels rather un-pythonic to me. But I've only skimmed it so far, I need to do some hands-on playing I think. I am getting concerned that python is developing a lot of these non-intuitive type features, almost because it can(*). A lot of it no doubt scratches somebody's itch but its at the expense of making what was a very easy to use language ever more complex. I'm not sure if i would choose Python for my Learn to Program tutorial if I was starting it these days - although I'm not sure what else is any better... (*)The same thing happened with C++ between v2 and ISO standardization - a lot of bells n' whistles were added which made C++ much more complex than was good for it (or for me as a programmer!). Eventually you had to be a guru level coder to use it successfully. Python hasn't got that far yet but it seems to be in danger of it. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From sbjaved at gmail.com Tue Aug 6 19:31:45 2013 From: sbjaved at gmail.com (Saad Javed) Date: Tue, 6 Aug 2013 22:31:45 +0500 Subject: [Tutor] adding users to tweets on a list In-Reply-To: <20130806135109.GB4143@gopher> References: <20130806091523.GD960@gopher> <20130806135109.GB4143@gopher> Message-ID: > Ah, I see. Sorry, I misread your requirements. Something like this should > work. > > #!/usr/bin/env python > > MAX_LENGTH = 140 > > > class TweetTooLongError(Exception): > """ > Raised when a user would be too long to add to the tweet, even > alone. > """ > pass > > > def generate_tweets(message, users): > """ > Generate tweets based around a message, with users > appended to each tweet. > > :param message: the base message > :param users: a group of users to append > :returns: tweets based around the message to the users > """ > > add = "" > longest_in_list = " @" + max(users, key=len) > > if len(longest_in_list) + len(message) > MAX_LENGTH: > raise TweetTooLongError( > "At least one user would make the tweet too long." > ) > > while users: > new_message = message > while len(new_message) + len(add) <= MAX_LENGTH: > new_message += add > if not users: > break > add = " @" + users.pop(0) > yield new_message > > > if __name__ == "__main__": > users = [ > "saad", "asad", "sherry", "danny", "ali", "hasan", "adil", > "yousaf", "maria", "bilal", "owais", > ] > message = raw_input("Enter string: ") > print("\n".join(generate_tweets(message, users))) > Thank you for your response. This code has a bug. If there is one user left in the user list, it doesn't print a tweet with just that one user added. For example use this string: "These are my friends living in the same city as i am. I have known them for years. They are good people in general. They are:"...you will see that "owais" is still in the list and is not added to a new tweet and printed. Saad -------------- next part -------------- An HTML attachment was scrubbed... URL: From sbjaved at gmail.com Tue Aug 6 19:49:19 2013 From: sbjaved at gmail.com (Saad Javed) Date: Tue, 6 Aug 2013 22:49:19 +0500 Subject: [Tutor] adding users to tweets on a list In-Reply-To: <20130806174430.GD9493@gopher> References: <20130806091523.GD960@gopher> <20130806135109.GB4143@gopher> <20130806174205.GC9493@gopher> <20130806174430.GD9493@gopher> Message-ID: > ...or, better, remove the if...break and just do: > > while users and len(new_message) + len(add) <= MAX_LENGTH: > That causes: Enter string: These are my friends living in the same city as i am. I have known them for years. They are good people in general. They are: Traceback (most recent call last): File "chris_tweet_len.py", line 44, in print("\n".join(generate_tweets(message, users))) File "chris_tweet_len.py", line 31, in generate_tweets while users and len(new_message) + len(add) <= MAX_LENGTH: UnboundLocalError: local variable 'new_message' referenced before assignment And the earlier fix now adds two users to a tweet, then one user, then two user, then one... :( -------------- next part -------------- An HTML attachment was scrubbed... URL: From sbjaved at gmail.com Tue Aug 6 19:51:37 2013 From: sbjaved at gmail.com (Saad Javed) Date: Tue, 6 Aug 2013 22:51:37 +0500 Subject: [Tutor] adding users to tweets on a list In-Reply-To: References: <20130806091523.GD960@gopher> <20130806135109.GB4143@gopher> <20130806174205.GC9493@gopher> <20130806174430.GD9493@gopher> Message-ID: > > And the earlier fix now adds two users to a tweet, then one user, then two > user, then one... :( > Enter string: These are my friends living in the same city as i am. I have known them for years. They are good people in general. They are: These are my friends living in the same city as i am. I have known them for years. They are good people in general. They are: @saad @asad These are my friends living in the same city as i am. I have known them for years. They are good people in general. They are: @sherry These are my friends living in the same city as i am. I have known them for years. They are good people in general. They are: @danny @ali These are my friends living in the same city as i am. I have known them for years. They are good people in general. They are: @hasan @adil These are my friends living in the same city as i am. I have known them for years. They are good people in general. They are: @yousaf These are my friends living in the same city as i am. I have known them for years. They are good people in general. They are: @maria @bilal These are my friends living in the same city as i am. I have known them for years. They are good people in general. They are: @owais -------------- next part -------------- An HTML attachment was scrubbed... URL: From oscar.j.benjamin at gmail.com Tue Aug 6 19:56:14 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 6 Aug 2013 18:56:14 +0100 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: <67bc14d5-8dd9-45f4-a5db-cd837dfe515d@email.android.com> <20130806082634.GD17751@ando> Message-ID: On 6 August 2013 17:33, Alan Gauld wrote: > On 06/08/13 10:01, Walter Prins wrote: > >> py> def cr(): # Co-Routine. >> ... x = yield() >> ... while True: >> ... x = yield(x + 1) >> ... > > >> "send" and yield as akin to threaded execution, where cr() is like a >> thread that blocks at the yield call after creation, and then when you >> call on magic.send() the "thread" wakes up and continues and eventually >> returns another value > > > It's clever but I'm not keen on it overloading yield to do it. > yield as a word conveys (to me at least) the idea of returning a value but > not quite completely ending. This usage sounds like a different concept and > I'd have preferred a more explicit name - although I can't think what!! I thought of Python's yield in the sense that 'to yield' means 'to concede'. So funcA yields execution to to the parent frame. The new 'yield from' sort of messes that interpretation up though since it should really be 'yield to' to make sense in this way. > Also > I'm not keen on the argument/parameter mechanism > here either. Arguments are sent but not explicitly declared in the receiver, > that all feels rather un-pythonic to me. I'm not really sure what you mean here. > But I've only > skimmed it so far, I need to do some hands-on playing I think. > > I am getting concerned that python is developing a lot of these > non-intuitive type features, almost because it can(*). A lot of it no doubt > scratches somebody's itch but its at the expense of making what was a very > easy to use language ever more complex. I'm not sure if i would choose > Python for my Learn to Program tutorial if I was starting it these days - > although I'm not sure what else is any better... No one needs to use .send() if they don't want to. It's also so uncommonly used that you don't need to mention it in any introductory tutorial. There's also .throw(): def cr(): try: yield except Exception as e: print('received: %s' % e) yield g = cr() next(g) # Advance to yield g.throw(ValueError('42')) Some time ago I wanted to be able to compute statistics over some data in a single-pass and I wanted to think of an easy way to invert a function that consumes an iterator so that I could push values in. The basic idea is to do something like: mean = Mean() var = Var() max = Max() for x in data: mean.update(data) var.update(data) max.update(data) print('mean:', mean.compute()) print('var:', var.compute()) print('max:', max.compute()) Then I realised that any function that computes a fold over an iterator can be inverted using yield so that this def isum(iterable, start=0): total = start for x in iterable: total += x return total becomes this def gsum(start=0): total = start yield lambda: total while True: total += yield gen = gsum() evaluate = next(gen) next(gen) # Move to second yield push = gen.send push(1) push(2) push(3) print(evaluate()) # 6 It looks a little shaky like this but you can use a decorator to clean it up: import functools def gencalc(gfunc): @functools.wraps(gfunc) def wrapper(*args, **kwargs): gen = gfunc(*args, **kwargs) evaluate = next(gen) next(gen) # Move to second yield push = gen.send return push, evaluate return wrapper @gencalc def gsum(start=0): total = start yield lambda: total while True: total += yield push, evaluate = gsum() Then it's easy to make other generator calculation functions: @gencalc def gmax(): yield lambda : currentmax currentmax = yield while True: newval = yield if newval > currentmax: currentmax = newval @gencalc def gvar(): # I haven't tested that this is correct yield lambda: totalvar / (count - 1) if count > 1 else 0 mean = 0 totalvar = 0 for count in itertools.count(): newval = yield oldmean = mean mean += (newval - mean) / count totalvar += (newval - oldmean) * (newval - mean) The obvious way to do this without generators is something like: class Sum: def __init__(self, start=0): self.total = start def push(self, value): self.total += value def evaluate(self): return self.total But I prefer the version that keeps it all in one function and still looks a lot like the normal function that consumes an iterable. Oscar From sbjaved at gmail.com Tue Aug 6 20:11:27 2013 From: sbjaved at gmail.com (Saad Javed) Date: Tue, 6 Aug 2013 23:11:27 +0500 Subject: [Tutor] adding users to tweets on a list In-Reply-To: <20130806175702.GE9493@gopher> References: <20130806091523.GD960@gopher> <20130806135109.GB4143@gopher> <20130806174205.GC9493@gopher> <20130806174430.GD9493@gopher> <20130806175702.GE9493@gopher> Message-ID: > I don't see how that differs from your expected output...? > > > I want all users added to the tweet. E.g. if 4 users can be added to the > > tweet before reaching the limit, return three tweets...first two with 4 > users > > attached and the last one with three. > > You hit the 140 character limit if another user is added, so it resets to > the > base message and adds the next user(s) as possible. > > What is your expected output for that sample input? > Oops! My bad. The fix worked as expected. The output fooled me into thinking it was skipping a user every second line. Actually MAX_LENGTH was being reached in each case. I'll check the tweet.py you posted. Thanks again! -------------- next part -------------- An HTML attachment was scrubbed... URL: From sbjaved at gmail.com Tue Aug 6 20:16:21 2013 From: sbjaved at gmail.com (Saad Javed) Date: Tue, 6 Aug 2013 23:16:21 +0500 Subject: [Tutor] adding users to tweets on a list In-Reply-To: References: <20130806091523.GD960@gopher> <20130806135109.GB4143@gopher> <20130806174205.GC9493@gopher> <20130806174430.GD9493@gopher> <20130806175702.GE9493@gopher> Message-ID: I added *len(new_message) + len(add) <= MAX_LENGTH *to the outer while loop instead of inner which threw an error. Your code works as expected. Thanks a lot! Saad -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Tue Aug 6 20:36:09 2013 From: davea at davea.name (Dave Angel) Date: Tue, 6 Aug 2013 18:36:09 +0000 (UTC) Subject: [Tutor] adding users to tweets on a list References: Message-ID: Saad Javed wrote: > I want to add users to the tweet from the list, the no. of users added > based on the length of the tweet. > This version should be a bit cleaner than what I've seen on this thread. #!/usr/bin/env python LIMIT = 140 #(I use uppercase there to show it's a constant) def send(message, users): output = message while users: while users and 1+len(output+users[0]) < LIMIT: output += " " + users.pop(0) if output == message: print "message too long for user", user[0] raise userError print output output = message lst = ['@saad', '@asad', '@sherry', '@danny', '@ali', '@hasan', '@adil', '@yousaf', '@maria', '@bilal', '@owais'] #string = raw_input('enter string: ') #example string string = ("These are my friends living in the same city as i am." " I have known them for years. They are " "good people in general. They are:") send(string, lst) BTW, you have a bunch of other messages on the thread which are replying to the invisible man, posts that aren't (yet?) visible. Since you quote him without attribution, we have no clue who you're commenting about. -- DaveA From alan.gauld at btinternet.com Tue Aug 6 20:56:04 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 06 Aug 2013 19:56:04 +0100 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: <67bc14d5-8dd9-45f4-a5db-cd837dfe515d@email.android.com> <20130806082634.GD17751@ando> Message-ID: On 06/08/13 18:56, Oscar Benjamin wrote: >>> py> def cr(): # Co-Routine. >>> ... x = yield() >>> ... while True: >>> ... x = yield(x + 1) >>> ... >> >> I'm not keen on the argument/parameter mechanism >> here either. Arguments are sent but not explicitly declared in the receiver, >> that all feels rather un-pythonic to me. > > I'm not really sure what you mean here. I simply meant that the syntax for send is like a normal function call: foo.send(someValue) But its not obvious from the code for cr() that someValue will get assigned to x. (It's not even obvious that the cr() code would be invoked at all!) And in the last line I'm still not sure (I've not had time to play yet) but I think it's saying: "return someValue+1 and assign that result to x ready for the next iteration of the cr() function." At least, that's what I read it to mean. So x which looks like a regular local variable assigned the result of yield() suddenly acquires the value of the argument to send(). That's not intuitive and it's not explicit (It breaks with the Python credo - explicit is better than implicit). It all seems a bit like too much magic going on for most beginners to understand. And while it's true they don't have to use it, it's easy for beginners to hit that kind of thing by accident, have their code "work" (no errors) but actually do something very different to what they expect. Now, in this case they have to call send(), which in itself is not an intuitive thing to do accidentally but I shudder to think how I would explain all that to a newbie who did stumble upon it! (Maybe by cut n' pasting code from a forum/thread somewhere?) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From wolfrage8765 at gmail.com Tue Aug 6 21:04:39 2013 From: wolfrage8765 at gmail.com (wolfrage8765 at gmail.com) Date: Tue, 6 Aug 2013 15:04:39 -0400 Subject: [Tutor] How to extract numerator and denominator from fractions.Fraction(4, 32)? In-Reply-To: References: <67bc14d5-8dd9-45f4-a5db-cd837dfe515d@email.android.com> <20130806082634.GD17751@ando> Message-ID: > It all seems a bit like too much magic going on for most beginners to > understand. And while it's true they don't have to use it, it's easy for > beginners to hit that kind of thing by accident, have their code "work" (no > errors) but actually do something very different to what they expect. Now, > in this case they have to call send(), which in itself is not an intuitive > thing to do accidentally but I shudder to think how I would explain all that > to a newbie who did stumble upon it! (Maybe by cut n' pasting code from a > forum/thread somewhere?) I have to agree, at some point I thought I was getting to know Python pretty well, then I read about iterators, and realized I understood zero of what I had read about iterators. Since I have unfortunately avoided them, until I have time to really learn how they work. It sounds very cool and all, async type operations like threading with out threads, but the magical syntax completely alludes me. And it is definitely above and beyond an average users level. But I think that is because it has it's root in functional programming which as I had stated before is a complex subject on it's own. From tonifuente at yahoo.co.uk Wed Aug 7 15:10:18 2013 From: tonifuente at yahoo.co.uk (Toni Fuente) Date: Wed, 7 Aug 2013 14:10:18 +0100 Subject: [Tutor] Writing bind9 zone files with python Message-ID: <20130807131018.GA2810@cateto> Hi again, I've got a Berkeley database with a key = 'domain name' and a value = 'timestamp'. And then I've got a list with items that I've got from a LDAP search, it is a list of tuples. Each tuple is formed of a string, which is a LDAP dn, and a dictionary with two key-value pairs, dnszonename: ['bladibla.com'] a domain name, and modifytimestamp: ['20130723201557Z'], with a number representing the last time that the domain was modified. This is a data example stored in a list variable from the LDAP search (ldapEntries): [('cn=example1.com,ou=dns,o=mycompany.com', {'dnszonename': ['example1.com'], 'modifytimestamp': ['20120601100135Z']}), [...] ('cn=exampleN.com,ou=dns,o=mycompany.com', {'dnszonename': ['exampleN.com'], 'modifytimestamp': ['20130801100135Z']})] I need to write something in python that would go through all the domains in the ldap search and if the domain exist on the local database, it then compares the timestamps, if they are the same, nothing will be done, but if they are different then, it will need to do another LDAP search for the zone, and then write a bind zone file in the appropriate place, and update the timestamp value in the local database with the one from LDAP. Finally if the domain zone name doesn't exist on the local database, it will need to write a bind zone file in the appropriate place and add a new key-value from the domain-timestamp on the local database. This is a sketch of what I am doing: ldapDict = {} ldapEntries = ld.search_s(ldapBase,ldap.SCOPE_SUBTREE,'(&(objectclass=dnszone)(cn=*))',['dnszonename','modifytimestamp']) for entry in ldapEntries: tmpDict = entry[1] ldapDict[''.join(tmpDict['dnszonename'])] = ''.join(tmpDict['modifytimestamp']) for k, v in ldapDict.iteritems(): value = zonesdb.get(k) if value == None: print "New domain" elif v != zonesdb[k]: print "New record" The first for loop converts the LDAP search list, into a dictionary of key-values that I need to work with. And the second loop depending of the values or existence of the keys, It will do something or the other. My two questions are: - Is this the right approach to solve the problem? Is there better ones? - I don't know how to approach the problem of writing bind zone files with the data that I will get from the LDAP server, so I was wondering if anyone knows a module that will help me. I am looking at things on the Internet like bind2csv.py, but it looks a daunting task for someone with my python skills level, to reverse that script. Thank you, -- Toni El que se casa fuera, o la trae o la lleva. From sbjaved at gmail.com Wed Aug 7 16:41:47 2013 From: sbjaved at gmail.com (Saad Javed) Date: Wed, 7 Aug 2013 19:41:47 +0500 Subject: [Tutor] adding users to tweets on a list In-Reply-To: References: Message-ID: There was only Chris responding to the question so I removed the line above the quoted part in my responses that says "On Aug 6, 2013 11:36 PM, "Dave Angel" wrote:"... is that what's confusing? On Aug 6, 2013 11:36 PM, "Dave Angel" wrote: > Saad Javed wrote: > > > I want to add users to the tweet from the list, the no. of users added > > based on the length of the tweet. > > > This version should be a bit cleaner than what I've seen on this thread. > > #!/usr/bin/env python > > LIMIT = 140 > #(I use uppercase there to show it's a constant) > > def send(message, users): > output = message > while users: > while users and 1+len(output+users[0]) < LIMIT: > output += " " + users.pop(0) > if output == message: > print "message too long for user", user[0] > raise userError > print output > output = message > > lst = ['@saad', '@asad', '@sherry', '@danny', '@ali', '@hasan', > '@adil', '@yousaf', '@maria', '@bilal', '@owais'] > > > #string = raw_input('enter string: ') > #example string > string = ("These are my friends living in the same city as i am." > " I have known them for years. They are " > "good people in general. They are:") > > send(string, lst) > > BTW, you have a bunch of other messages on the thread which are replying > to the invisible man, posts that aren't (yet?) visible. Since you quote > him without attribution, we have no clue who you're commenting about. > > -- > DaveA > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Aug 7 19:47:33 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 07 Aug 2013 18:47:33 +0100 Subject: [Tutor] adding users to tweets on a list In-Reply-To: References: Message-ID: On 07/08/13 15:41, Saad Javed wrote: > There was only Chris responding Apparently he didn't CC the list so the rest of us didn't see it(*). > is that what's confusing? It's not just the lack of attribution but the fact we just didn't see the post to which you are replying. (*)It is possible that Chris' mail is in the moderation queue, which I was hoping to visit tonight or tomorrow... In which case, watch this space... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From chris at chrisdown.name Mon Aug 5 21:26:30 2013 From: chris at chrisdown.name (Chris Down) Date: Mon, 5 Aug 2013 21:26:30 +0200 Subject: [Tutor] Start multiple threads from Python In-Reply-To: References: Message-ID: <20130805192630.GA1345@gopher> On 2013-08-05 12:17, Ryan Waples wrote: > Currently I am calling each analysis program one at a time with > subprocess.call(). This is working without a hitch, but as each analysis > can take a while to run, I want to try to speed things up. I realize I can > start three different python sessions to do this, but that just begs the > question how to do that from python? subprocess.Popen does not block unless you explicitly tell it to (by using communicate()). Perhaps that's what you want. >>> import subprocess >>> x = subprocess.Popen([ "sleep", "60" ]) >>> y = subprocess.Popen([ "sleep", "60" ]) >>> x.pid 3035 >>> y.pid 3036 -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From amandeep.qa at gmail.com Sat Aug 3 01:33:00 2013 From: amandeep.qa at gmail.com (Amandeep Behl) Date: Fri, 2 Aug 2013 16:33:00 -0700 Subject: [Tutor] Can anyone explain this Message-ID: Can anyone explain this code below: import sys import os if __name__ == '__main__': sys.path.insert(0, "..") else: sys.path.insert(0, os.path.join( os.path.split(__file__)[0], '..')) -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Tue Aug 6 22:34:47 2013 From: davea at davea.name (Dave Angel) Date: Tue, 06 Aug 2013 16:34:47 -0400 Subject: [Tutor] adding users to tweets on a list In-Reply-To: <20130806191827.GA17412@gopher> References: <20130806191827.GA17412@gopher> Message-ID: <52015DE7.8070004@davea.name> On 08/06/2013 03:18 PM, Chris Down wrote: > On 2013-08-06 18:36, Dave Angel wrote: >> This version should be a bit cleaner than what I've seen on this thread. > > Our methods are almost the same, other than the fact that you don't use a > generator, and you do the length validity check during the loop instead of > preemptively, I'm not sure which I think is cleaner. > > I'm more inclined toward using more lines of code to clearly express the reason > for the failure's conclusion, but either is perfectly valid and readable. > Thanks for the alternative :-) I didn't actually check whether that exception is valid; it's probably spelt wrong. > > On 2013-08-06 18:36, Dave Angel wrote: >> BTW, you have a bunch of other messages on the thread which are replying >> to the invisible man, posts that aren't (yet?) visible. Since you quote >> him without attribution, we have no clue who you're commenting about. > > He is replying to me, but his client seems to not prepend any header to his > quotes. I am new to this list and am still held in the moderation queue. Thanks for the status, I didn't think about the moderation queue. Presumably the only reason he saw your messages already is you did a reply-all instead of a reply-list. DaveA From chris at chrisdown.name Tue Aug 6 21:18:27 2013 From: chris at chrisdown.name (Chris Down) Date: Tue, 6 Aug 2013 21:18:27 +0200 Subject: [Tutor] adding users to tweets on a list In-Reply-To: References: Message-ID: <20130806191827.GA17412@gopher> On 2013-08-06 18:36, Dave Angel wrote: > This version should be a bit cleaner than what I've seen on this thread. Our methods are almost the same, other than the fact that you don't use a generator, and you do the length validity check during the loop instead of preemptively, I'm not sure which I think is cleaner. I'm more inclined toward using more lines of code to clearly express the reason for the failure's conclusion, but either is perfectly valid and readable. Thanks for the alternative :-) On 2013-08-06 18:36, Dave Angel wrote: > BTW, you have a bunch of other messages on the thread which are replying > to the invisible man, posts that aren't (yet?) visible. Since you quote > him without attribution, we have no clue who you're commenting about. He is replying to me, but his client seems to not prepend any header to his quotes. I am new to this list and am still held in the moderation queue. Chris -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From joshkim.319 at gmail.com Wed Aug 7 18:38:27 2013 From: joshkim.319 at gmail.com (Joshua Kim) Date: Wed, 7 Aug 2013 12:38:27 -0400 Subject: [Tutor] .txt matrix import Message-ID: <1DF57B27-0A10-4349-9E4E-3B997BB78EBE@gmail.com> Hello, This is my first time using Tutor, so I apologize for any fallacies I may be making; my problem involves opening a .txt file which contains several lines of descriptive information followed by a tab delimited matrix of numerical values? I am trying to find an effective way in which to skip the first 'X' number of lines in the .txt file (54 in my case) and input the columns of data into individual arrays (note the 55th line denotes the title of the column, so preferably I would like to have python use those values as the names to the individual array, but I am unsure of how involved that may become) Any hints/suggestions/pointers would be appreciated. Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From protonlenny at gmail.com Thu Aug 1 07:21:36 2013 From: protonlenny at gmail.com (Zachary Rizer) Date: Thu, 1 Aug 2013 00:21:36 -0500 Subject: [Tutor] Question in regards to loops and matplotlib Message-ID: So I just started coding, and I'm so glad I chose Python to start me off! I really enjoy the clean layout, easy syntax, and power of the language. I'm doing astronomy research and I just started teaching myself matplotlib along with my general python work. I feel like I'm catching on quick, but I also feel that this particular plot script is a little rough. The plot looks correct, but the code seems really long for what I'm trying to do. So any tricks to make this more efficient would be greatly appreciated! A little bit about what's going on before I post the script. I'm using np.genfromtext to upload a CSV file of a bunch of astronomy data. I need to break this data down into multiple subsets. 4 mass separated subsets (row['logM']) and each of those is divided into bulge and disk dominated. And for each of those I need to separate quiescent and star forming. So I have a total of 8 star forming subsets and 8 quiescent subsets. And all sixteen different styles of object need a distinct marker on the plot. I hope that with the comments you can see where I divided those up in the code. I did it all with a ton of for loops and if statements filling in a bunch of empty arrays. Hopefully this is enough info to make sense of what I'm going for. My main question is can I shorten this code, especially the for loops, to make it more efficient and clean? Here is the script: # -*- coding: utf-8 -*- #----- Zach Rizer #----- UVJ plot #----- data = n>2_qui_flag.csv import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as patch from matplotlib.path import Path fig = plt.figure('UVJ') ax = fig.add_subplot(111) #subplot for shaded region ########################################################################## # ----- Data uploaded from preliminary TopCat CSV Files data = np.genfromtxt('/Users/ProtonLenny/Documents/Research/Catalog_Data/Catalog_3/n>2_qui_flag.csv',\ dtype=None,names=True,delimiter =",") # ----- Define Quiesent Subset qui_m1d_xval = [] qui_m1d_yval = [] qui_m2d_xval = [] qui_m2d_yval = [] qui_m3d_xval = [] qui_m3d_yval = [] qui_m4d_xval = [] qui_m4d_yval = [] qui_m1b_xval = [] qui_m1b_yval = [] qui_m2b_xval = [] qui_m2b_yval = [] qui_m3b_xval = [] qui_m3b_yval = [] qui_m4b_xval = [] qui_m4b_yval = [] for row in data: if row['Dv'] > 0.65 and row['DSw'] > 0.65: * # filling disk-dom arrays* if row['UminV'] > 1.3 and row['VminJ'] < 1.6 and row['UminV'] > 0.88*(row['VminJ']+0.59):#quiescent criteria at particular z if row['Z'] >= 0.6 and row['Z'] < 0.9:#redshift bin criteria if row['logM'] >= 9.7 and row['logM'] < 10:#mass subsets for shape in plots qui_m1d_xval.append(row['VminJ']) * # (x) fill the empty list with the data from the appropriate column.* qui_m1d_yval.append(row['UminV']) elif row['logM'] >= 10 and row['logM'] < 10.5: qui_m2d_xval.append(row['VminJ']) qui_m2d_yval.append(row['UminV']) elif row['logM'] >= 10.5 and row['logM'] < 10.8: qui_m3d_xval.append(row['VminJ']) qui_m3d_yval.append(row['UminV']) elif row['logM'] >= 10.8: qui_m4d_xval.append(row['VminJ']) qui_m4d_yval.append(row['UminV']) if row['Dv'] < 0.35 and row['DSw'] > 0.65: * # filling bulge-dom arrays* if row['UminV'] > 1.3 and row['VminJ'] < 1.6 and row['UminV'] > 0.88*(row['VminJ']+0.59):#quiescent criteria at particular z if row['Z'] >= 0.6 and row['Z'] < 0.9:#redshift bin criteria if row['logM'] >= 9.7 and row['logM'] < 10:#mass subsets for shape in plots qui_m1b_xval.append(row['VminJ']) qui_m1b_yval.append(row['UminV']) elif row['logM'] >= 10 and row['logM'] < 10.5: qui_m2b_xval.append(row['VminJ']) qui_m2b_yval.append(row['UminV']) elif row['logM'] >= 10.5 and row['logM'] < 10.8: qui_m3b_xval.append(row['VminJ']) qui_m3b_yval.append(row['UminV']) elif row['logM'] >= 10.8: qui_m4b_xval.append(row['VminJ']) qui_m4b_yval.append(row['UminV']) # ----- Define Star-Forming Subset sf_m1d_xval = [] sf_m1d_yval = [] sf_m2d_xval = [] sf_m2d_yval = [] sf_m3d_xval = [] sf_m3d_yval = [] sf_m4d_xval = [] sf_m4d_yval = [] sf_m1b_xval = [] sf_m1b_yval = [] sf_m2b_xval = [] sf_m2b_yval = [] sf_m3b_xval = [] sf_m3b_yval = [] sf_m4b_xval = [] sf_m4b_yval = [] for row in data: if row['Dv'] > 0.65 and row['DSw'] > 0.65: # filling disk-dom arrays if row['UminV'] < 1.3 or row['VminJ'] > 1.6 or row['UminV'] < 0.88*(row['VminJ']+0.59):#star forming creteria at particular z if row['Z'] >= 0.6 and row['Z'] < 0.9:#reshift bin criteria if row['logM'] >= 9.7 and row['logM'] < 10: sf_m1d_xval.append(row['VminJ']) sf_m1d_yval.append(row['UminV']) elif row['logM'] >= 10 and row['logM'] < 10.5: sf_m2d_xval.append(row['VminJ']) sf_m2d_yval.append(row['UminV']) elif row['logM'] >= 10.5 and row['logM'] < 10.8: sf_m3d_xval.append(row['VminJ']) sf_m3d_yval.append(row['UminV']) elif row['logM'] >= 10.8: sf_m4d_xval.append(row['VminJ']) sf_m4d_yval.append(row['UminV']) if row['Dv'] < 0.35 and row['DSw'] > 0.65: # filling disk-dom arrays if row['UminV'] < 1.3 or row['VminJ'] > 1.6 or row['UminV'] < 0.88*(row['VminJ']+0.59):#star forming creteria at particular z if row['Z'] >= 0.6 and row['Z'] < 0.9:#reshift bin criteria if row['logM'] >= 9.7 and row['logM'] < 10: sf_m1b_xval.append(row['VminJ']) sf_m1b_yval.append(row['UminV']) elif row['logM'] >= 10 and row['logM'] < 10.5: sf_m2b_xval.append(row['VminJ']) sf_m2b_yval.append(row['UminV']) elif row['logM'] >= 10.5 and row['logM'] < 10.8: sf_m3b_xval.append(row['VminJ']) sf_m3b_yval.append(row['UminV']) elif row['logM'] >= 10.8: sf_m4b_xval.append(row['VminJ']) sf_m4b_yval.append(row['UminV']) # ----- plot the first column as x, and second column as y plot_qui_m4d, = plt.plot(qui_m4d_xval, qui_m4d_yval, 'r^', ms=12) plot_qui_m4b, = plt.plot(qui_m4b_xval, qui_m4b_yval, 'ro', ms=12) plot_qui_m3d, = plt.plot(qui_m3d_xval, qui_m3d_yval, 'r^', ms=9) plot_qui_m3b, = plt.plot(qui_m3b_xval, qui_m3b_yval, 'ro', ms=9) plot_qui_m2d, = plt.plot(qui_m2d_xval, qui_m2d_yval, 'r^', ms=6) plot_qui_m2b, = plt.plot(qui_m2b_xval, qui_m2b_yval, 'ro', ms=6) plot_qui_m1d, = plt.plot(qui_m1d_xval, qui_m1d_yval, 'r^', ms=3) plot_qui_m1b, = plt.plot(qui_m1b_xval, qui_m1b_yval, 'ro', ms=3) plot_sf_m4d, = plt.plot(sf_m4d_xval, sf_m4d_yval, 'b^', ms=12) plot_sf_m4b, = plt.plot(sf_m4b_xval, sf_m4b_yval, 'bo', ms=12) plot_sf_m3d, = plt.plot(sf_m3d_xval, sf_m3d_yval, 'b^', ms=9) plot_sf_m3b, = plt.plot(sf_m3b_xval, sf_m3b_yval, 'bo', ms=9) plot_sf_m2d, = plt.plot(sf_m2d_xval, sf_m2d_yval, 'b^', ms=6) plot_sf_m2b, = plt.plot(sf_m2b_xval, sf_m2b_yval, 'bo', ms=6) plot_sf_m1d, = plt.plot(sf_m1d_xval, sf_m1d_yval, 'b^', ms=3) plot_sf_m1b, = plt.plot(sf_m1b_xval, sf_m1b_yval, 'bo', ms=3) ########################################################################## ####### Shading, coloring, patches, etc... #------ quiescent divided region verts = [(-1.,1.3), (0.8,1.3), (1.6,2.0), (1.6,2.5), (-1.,2.5), (-1.,1.3)] codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY] path = Path(verts,codes) patch = patch.PathPatch(path, facecolor='none', lw=1.5, alpha=0.5, hatch='//') ax.add_patch(patch) ########################################################################## #----- titles and axis labels and axis limits plt.title('UVJ Plot of n>2 population at 0.6 From vick1975 at orange.mu Thu Aug 1 13:32:16 2013 From: vick1975 at orange.mu (Vick) Date: Thu, 1 Aug 2013 15:32:16 +0400 Subject: [Tutor] hi In-Reply-To: References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> Message-ID: <000601ce8eaa$c6baabb0$54300310$@orange.mu> Hi, As per your request below, I have attached a stand-alone example (test3d.py) of my problem. I am trying to plot in 3D using ion() from a loop. The code should plot x,y,z coordinates from a loop in ion(). We should see the plot line moving and the marker moving as well. I have also attached a working 2d-plot (test2d.py) to show what it's supposed to do. The 3d plot (test3d.py) should plot the same but with an additional z coordinates and a z axis! Thanks Vick -----Original Message----- From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] Sent: Thursday, 01 August, 2013 01:42 To: Vick Subject: Re: [Tutor] hi On 31 July 2013 22:20, Vick wrote: > Hello, Hi Vick, I would prefer it if you would send questions like this to the tutor mailing list rather than directly to me. This is because: 1) I'm often unable to respond and there are many other people there who could help (I may be the only one there who understands ODEs but I'm certainly not the only one who understands matplotlib). 2) It's helpful for others on the tutor mailing list to see the kind of problems (and solutions) that people new to Python are working with. 3) Any problem/solution gets publicly archived for future reference. > Can you help me please with plotting in 3D using ion()? > > My code is the following: > > from pylab import * > #import pylab > from mpl_toolkits.mplot3d.axes3d import Axes3D import > matplotlib.pyplot as plt from mpl_toolkits.mplot3d import axes3d > > plt.ion() > > fig = plt.figure(dpi=100) > ax = axes3d.Axes3D(fig)#fig.add_subplot(111, projection = '3d') > > > tn= mpf('300')#mpf('800')*dt > > for i in drange (t+dt, tn,dt): > mi = testran(i,mi,dt) > #print i+ dt," ", mi > a.append(mi[0]) > b.append(mi[1]) > z1.append(mi[2]) > c.append(mi[6]) > d.append(mi[7]) > z2.append(mi[8]) > #clf() > ax.plot(a,b,z1)#linestyle='None', marker='o', markersize = 5) > ax.plot(a,b,z1, marker='o', linestyle='None') > ax.plot(c,d,z2) > ax.plot(c,d,z2, marker='o', linestyle='None') > #draw() > > #grid() > #show() > > I'm trying to plot in 3D, coordinates that are appended from a loop > using ion(), but it doesn't plot. I get something different: $ python plot3d.py Traceback (most recent call last): File "plot3d.py", line 13, in tn= mpf('300')#mpf('800')*dt NameError: name 'mpf' is not defined Where does the mpf function come from? There are many other symbols that are not defined (t, dt, testran, ...) so presumably this code is incomplete somehow. Could you please make a complete example illustrating the problem you have and then post it to the tutor list? Oscar -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: test2d.py URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: test3d.py URL: From chris at chrisdown.name Tue Aug 6 19:57:02 2013 From: chris at chrisdown.name (Chris Down) Date: Tue, 6 Aug 2013 19:57:02 +0200 Subject: [Tutor] adding users to tweets on a list In-Reply-To: References: <20130806091523.GD960@gopher> <20130806135109.GB4143@gopher> <20130806174205.GC9493@gopher> <20130806174430.GD9493@gopher> Message-ID: <20130806175702.GE9493@gopher> On 2013-08-06 22:49, Saad Javed wrote: > That causes: > > Enter string: These are my friends living in the same city as i am. I have > known them for years. They are good people in general. They are: > Traceback (most recent call last): > File "chris_tweet_len.py", line 44, in > print("\n".join(generate_tweets(message, users))) > File "chris_tweet_len.py", line 31, in generate_tweets > while users and len(new_message) + len(add) <= MAX_LENGTH: > UnboundLocalError: local variable 'new_message' referenced before assignment Cannot reproduce. I'm not sure how you edited the script, but somehow you moved new_message to where it is unbound. See attached. > And the earlier fix now adds two users to a tweet, then one user, then two > user, then one... :( I don't see how that differs from your expected output...? > I want all users added to the tweet. E.g. if 4 users can be added to the > tweet before reaching the limit, return three tweets...first two with 4 users > attached and the last one with three. You hit the 140 character limit if another user is added, so it resets to the base message and adds the next user(s) as possible. What is your expected output for that sample input? -------------- next part -------------- A non-text attachment was scrubbed... Name: tweet.py Type: text/x-python Size: 1151 bytes Desc: not available URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From chris at chrisdown.name Tue Aug 6 19:42:05 2013 From: chris at chrisdown.name (Chris Down) Date: Tue, 6 Aug 2013 19:42:05 +0200 Subject: [Tutor] adding users to tweets on a list In-Reply-To: References: <20130806091523.GD960@gopher> <20130806135109.GB4143@gopher> Message-ID: <20130806174205.GC9493@gopher> On 2013-08-06 22:31, Saad Javed wrote: > Thank you for your response. This code has a bug. > > If there is one user left in the user list, it doesn't print a tweet with > just that one user added. For example use this string: "These are my > friends living in the same city as i am. I have known them for years. They > are good people in general. They are:"...you will see that "owais" is still > in the list and is not added to a new tweet and printed. Good catch, that's my bad, sorry. Because pop() is called on the last element just before the next iteration, when `users' is coerced to a bool, it becomes False in the outer while loop. This affects only 50% of cases because it's possible we will be in the inner loop when we hit the last element in `users', which is why I didn't see it. All that needs to happen is to move the pop to the top of the MAX_LENGTH check: while len(new_message) + len(add) <= MAX_LENGTH: add = " @" + users.pop(0) new_message += add if not users: break -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From chris at chrisdown.name Tue Aug 6 19:44:30 2013 From: chris at chrisdown.name (Chris Down) Date: Tue, 6 Aug 2013 19:44:30 +0200 Subject: [Tutor] adding users to tweets on a list In-Reply-To: <20130806174205.GC9493@gopher> References: <20130806091523.GD960@gopher> <20130806135109.GB4143@gopher> <20130806174205.GC9493@gopher> Message-ID: <20130806174430.GD9493@gopher> On 2013-08-06 19:42, Chris Down wrote: > All that needs to happen is to move the pop to the top of the MAX_LENGTH check: > > while len(new_message) + len(add) <= MAX_LENGTH: ...or, better, remove the if...break and just do: while users and len(new_message) + len(add) <= MAX_LENGTH: -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From chris at chrisdown.name Tue Aug 6 15:51:09 2013 From: chris at chrisdown.name (Chris Down) Date: Tue, 6 Aug 2013 15:51:09 +0200 Subject: [Tutor] adding users to tweets on a list In-Reply-To: References: <20130806091523.GD960@gopher> Message-ID: <20130806135109.GB4143@gopher> On 2013-08-06 14:40, Saad Javed wrote: > It will add max no. of users to one tweet until limit is reached. I want > all users added to the tweet. E.g. if 4 users can be added to the tweet > before reaching the limit, return three tweets...first two with 4 users > attached and the last one with three. Ah, I see. Sorry, I misread your requirements. Something like this should work. #!/usr/bin/env python MAX_LENGTH = 140 class TweetTooLongError(Exception): """ Raised when a user would be too long to add to the tweet, even alone. """ pass def generate_tweets(message, users): """ Generate tweets based around a message, with users appended to each tweet. :param message: the base message :param users: a group of users to append :returns: tweets based around the message to the users """ add = "" longest_in_list = " @" + max(users, key=len) if len(longest_in_list) + len(message) > MAX_LENGTH: raise TweetTooLongError( "At least one user would make the tweet too long." ) while users: new_message = message while len(new_message) + len(add) <= MAX_LENGTH: new_message += add if not users: break add = " @" + users.pop(0) yield new_message if __name__ == "__main__": users = [ "saad", "asad", "sherry", "danny", "ali", "hasan", "adil", "yousaf", "maria", "bilal", "owais", ] message = raw_input("Enter string: ") print("\n".join(generate_tweets(message, users))) -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From chris at chrisdown.name Tue Aug 6 11:15:23 2013 From: chris at chrisdown.name (Chris Down) Date: Tue, 6 Aug 2013 11:15:23 +0200 Subject: [Tutor] adding users to tweets on a list In-Reply-To: References: Message-ID: <20130806091523.GD960@gopher> On 2013-08-06 03:48, Saad Javed wrote: > I want to add users to the tweet from the list, the no. of users added > based on the length of the tweet. It looks like you're using Python 2, but you didn't specify. I'd probably do something like this: #!/usr/bin/env python MAX_LENGTH = 140 users = [ "saad", "asad", "sherry", "danny", "ali", "hasan", "adil", "yousaf", "maria", "bilal", "owais", ] def populate(): message = raw_input("Enter string: ") while users: new_message = " ".join([message, "@" + users.pop(0)]) if len(new_message) > MAX_LENGTH: break message = new_message return message if __name__ == "__main__": print(populate()) -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From sbjaved at gmail.com Wed Aug 7 23:40:31 2013 From: sbjaved at gmail.com (Saad Javed) Date: Thu, 8 Aug 2013 02:40:31 +0500 Subject: [Tutor] adding users to tweets on a list In-Reply-To: References: Message-ID: On Wednesday, August 7, 2013, Alan Gauld wrote: > On 07/08/13 15:41, Saad Javed wrote: > >> There was only Chris responding >> > > Apparently he didn't CC the list so the rest of us didn't see it(*). > > is that what's confusing? >> > > It's not just the lack of attribution but the fact we just didn't see the > post to which you are replying. > > (*)It is possible that Chris' mail is in the moderation queue, > which I was hoping to visit tonight or tomorrow... > In which case, watch this space... > > Chris has been emailing me directly. Thats why his responses are not showing up in the conversation. Dear Chris, please remember to send your replies to tutor at python.org with the subject of the question ("adding users to tweets on a list"), so that others can see them. Thanks! -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Wed Aug 7 23:46:33 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 7 Aug 2013 17:46:33 -0400 Subject: [Tutor] .txt matrix import In-Reply-To: <1DF57B27-0A10-4349-9E4E-3B997BB78EBE@gmail.com> References: <1DF57B27-0A10-4349-9E4E-3B997BB78EBE@gmail.com> Message-ID: On Wed, Aug 7, 2013 at 12:38 PM, Joshua Kim wrote: > Hello, > > This is my first time using Tutor, so I apologize for any fallacies I may be > making; my problem involves opening a .txt file which contains several lines > of descriptive information followed by a tab delimited matrix of numerical > values? I am trying to find an effective way in which to skip the first 'X' > number of lines in the .txt file (54 in my case) and input the columns of > data into individual arrays (note the 55th line denotes the title of the > column, so preferably I would like to have python use those values as the > names to the individual array, but I am unsure of how involved that may > become) > > > Any hints/suggestions/pointers would be appreciated. > If you use the file method readlines() it will read the complete file into a list of each line in the file. From there you can start processing from the 54th item in the list. You should also look up the csv module which implies that is it for processing comma separated value data, but it can also work with tab separated values. > Thanks > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick http://joelgoldstick.com From joel.goldstick at gmail.com Wed Aug 7 23:50:37 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 7 Aug 2013 17:50:37 -0400 Subject: [Tutor] Online references for Python In-Reply-To: References: Message-ID: start with the links at python.org. They will keep you busy until you know quite a lot! On Mon, Jul 29, 2013 at 12:29 PM, Rene Datta wrote: > Hi: > > I was wondering if anyone could suggest a good quality online resource tool > or compendium to provide a beginner's level of information to start learning > the Python language. > > Thank you. > > Rene Datta > renedatta at gmail.com > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick http://joelgoldstick.com From sbjaved at gmail.com Wed Aug 7 23:53:40 2013 From: sbjaved at gmail.com (Saad Javed) Date: Thu, 8 Aug 2013 02:53:40 +0500 Subject: [Tutor] adding users to tweets on a list In-Reply-To: <20130807215020.GB14236@gopher> References: <20130807215020.GB14236@gopher> Message-ID: On Thursday, August 8, 2013, Chris Down wrote: > On 2013-08-08 02:40, Saad Javed wrote: > > Chris has been emailing me directly. Thats why his responses are not > > showing up in the conversation. > > Dear Chris, please remember to send your replies to tutor at python.orgwith > > the subject of the question ("adding users to tweets on a list"), so that > > others can see them. > > You are mistaken, python-tutor was Cc'd on every message. I was merely > held in > the moderation queue -- there is nothing I can do about that. > My mistake, I apologize. I did the same thing a while back by accident and assumed you made the same mistake. I guess it must be the moderation queue then. It has reinforced my long standing belief that most programmers are smarter than I am ;) Saad -------------- next part -------------- An HTML attachment was scrubbed... URL: From tonifuente at yahoo.co.uk Wed Aug 7 23:54:07 2013 From: tonifuente at yahoo.co.uk (Antonio de la Fuente) Date: Wed, 7 Aug 2013 22:54:07 +0100 Subject: [Tutor] Python Trouble, Please Help! In-Reply-To: References: Message-ID: <20130807215407.GC2810@cateto> * Jonathan Hong [2013-07-24 16:23:52 -0700]: > Hello, > > I am currently working with the Python programming language and had a very > basic question regarding the start up of Python. For some reason, when I > open up the Command Prompt in my Windows 7 computer, I am unable to access > python files. So when I type in the python command, I am able to start > writing in python, but I cannot call other python files such as saying python > helloworld.py. I think it might have something to do with where I am > placing my files and the path of python? Not too sure though because of my > lack of computer knowledge. When I do put in python helloworld.py into the > command prompt line, the computer gives me a response of: "python: can't > open file 'hello.py': [Errno 2] No such file or directory." I know for a > fact that there does exist a file called hello.py though. > Please help me if you can! > > Sincerely, > Python_Beginner I think I can help with this one. Yes you are right, it is to where you are placing your files and where you are in the file system when you are trying to run your helloworld.py. I leave you a link with some more info in how to navigate windows file systems with the command prompt, I hope it helps: http://www.watchingthenet.com/how-to-navigate-through-folders-when-using-windows-command-prompt.html Regards, -- Toni Las tres cosas que mas me gustan en el mundo son el silencio, la soledad y los espacios vacios. -- Chumy Chomez. From tonifuente at yahoo.co.uk Thu Aug 8 00:01:17 2013 From: tonifuente at yahoo.co.uk (Antonio de la Fuente) Date: Wed, 7 Aug 2013 23:01:17 +0100 Subject: [Tutor] Online references for Python In-Reply-To: References: Message-ID: <20130807220117.GD2810@cateto> * Rene Datta [2013-07-29 10:29:40 -0600]: > Hi: > > I was wondering if anyone could suggest a good quality online resource tool > or compendium to provide a beginner's level of information to start > learning the Python language. Hi Rene, As Joel has said python.org is a very good place to start with, specifically its getting started page: http://www.python.org/about/gettingstarted/ Regards, -- Toni The onset and the waning of love make themselves felt in the uneasiness experienced at being alone together. -- Jean de la Bruyere From ramit.prasad at jpmorgan.com.dmarc.invalid Wed Aug 7 23:50:48 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Wed, 7 Aug 2013 21:50:48 +0000 Subject: [Tutor] Online references for Python In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF47418621291@SCACMX008.exchad.jpmchase.net> Rene Datta wrote: > Hi: > > I was wondering if anyone could suggest a good quality online resource tool or compendium to provide a > beginner's level of information to start learning the Python language. > > Thank you. > > Rene Datta > renedatta at gmail.com You can try the links on the python.org documentation page. There are several tutorials/HOWTOs listed there. http://www.python.org/doc/ ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From ramit.prasad at jpmorgan.com.dmarc.invalid Wed Aug 7 23:46:59 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Wed, 7 Aug 2013 21:46:59 +0000 Subject: [Tutor] .txt matrix import In-Reply-To: <1DF57B27-0A10-4349-9E4E-3B997BB78EBE@gmail.com> References: <1DF57B27-0A10-4349-9E4E-3B997BB78EBE@gmail.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741862125B@SCACMX008.exchad.jpmchase.net> Joshua Kim wrote: > Hello, > > > This is my first time using Tutor, so I apologize for any fallacies I may be making; my problem > involves opening a .txt file which contains several lines of descriptive information followed by a tab > delimited matrix of numerical values... I am trying to find an effective way in which to skip the first > 'X' number of lines in the .txt file (54 in my case) and input the columns of data into individual > arrays (note the 55th line denotes the title of the column, so preferably I would like to have python > use those values as the names to the individual array, but I am unsure of how involved that may > become) > > Any hints/suggestions/pointers would be appreciated. > > > Thanks The csv module should be able to handle most common delimiters and also has a DictReader which lets you load each line on dictionary. You can use next() on the filehandle to skip lines. ====== File data ====== blah blah blah col1,col2,col3 d1,d2,d3 d4,d5,d6 ======================= >>> with open( filename, 'r' ) as f: # Might need 'rb' instead of 'r' ... for _ in xrange(3): # skip arbitrary amounts. ... _ = next(f) # use _ to avoid it showing on interpreter output ... column_names = next(f).split(',') # split on your delimiting character ... # might need to strip() on each column name ... reader = csv.DictReader( f, fieldnames=column_names, ) ... for d in reader: ... pprint.pprint(d) ... {'col1': 'd1', 'col2': 'd2', 'col3\n': 'd3'} {'col1': 'd4', 'col2': 'd5', 'col3\n': 'd6'} ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From davea at davea.name Thu Aug 8 01:28:11 2013 From: davea at davea.name (Dave Angel) Date: Wed, 7 Aug 2013 23:28:11 +0000 (UTC) Subject: [Tutor] .txt matrix import References: <1DF57B27-0A10-4349-9E4E-3B997BB78EBE@gmail.com> Message-ID: Joshua Kim wrote: > Hello, > > This is my first time using Tutor, so I apologize for any fallacies I may be making; my problem involves opening a .txt file which contains several lines of descriptive information followed by a tab delimited matrix of numerical values? I am trying to find an effective way in which to skip the first 'X' number of lines in the .txt file (54 in my case) and input the columns of data into individual arrays (note the 55th line denotes the title of the column, so preferably I would like to have python use those values as the names to the individual array, but I am unsure of how involved that may become) > > > Any hints/suggestions/pointers would be appreciated. > First, let me suggest you not send html mail. It basically tripled the size of your message, and many times it will also distort parts of your message, especially when you're including code. Your query basically says you have a file that's in two parts. The first part is to be ignored, and the second part is a simple csv file, complete with column headings. All you need do to skip the first part is to call readline() on the file object X times, something like: for temp in range(X): infile.readline() At this point, the infile object is positioned at the header line of the csv data. You can then use the csv module to successively turn each row of the infile into a dict, with the names being the ones specified in the file. This will give you the data organized by row. I'd recommend you get this much debugged before trying to convert it to columns. BTW you may not need the csv module if your data itself never has a tab in it. The place where the module really helps is (for example in a comma-delimited file) when there's sometimes a comma in the actual data that needs escaping, or quoting. -- DaveA From wprins at gmail.com Thu Aug 8 01:51:59 2013 From: wprins at gmail.com (Walter Prins) Date: Thu, 8 Aug 2013 00:51:59 +0100 Subject: [Tutor] Converting a string to an integer In-Reply-To: <201307310814100.SM6474454@VADER2> References: <201307310814100.SM6474454@VADER2> Message-ID: On 31 July 2013 16:14, Art Bullentini wrote: > I'm trying to convert from a string of digits such as x = [y:y+1] into an > integer using int(x). the book says I can do this, but I get an error > message saying no can do. Anybody tell me what's going on? > > Please supply the actual code you've tried and the actual error message please. Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Thu Aug 8 02:00:29 2013 From: wprins at gmail.com (Walter Prins) Date: Thu, 8 Aug 2013 01:00:29 +0100 Subject: [Tutor] Python Trouble, Please Help! In-Reply-To: References: Message-ID: Hi, On 25 July 2013 00:23, Jonathan Hong wrote: > Hello, > > I am currently working with the Python programming language and had a very > basic question regarding the start up of Python. For some reason, when I > open up the Command Prompt in my Windows 7 computer, I am unable to access > python files. > I'd like to add the following suggestion to the link Antonio's already sent you: http://www.voidspace.org.uk/python/articles/command_line.shtml Regards, Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Thu Aug 8 02:11:58 2013 From: davea at davea.name (Dave Angel) Date: Thu, 8 Aug 2013 00:11:58 +0000 (UTC) Subject: [Tutor] Can anyone explain this References: Message-ID: Amandeep Behl wrote: > Can anyone explain this code below: > > > >
Can anyone explain this code below:


import sys
import os
? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
if __name__ == '__main__': ?
? ? sys.path.insert(0, "..")?
>
else: ? ? ? ? ? ? ? ? ? ? ? ?
? ? sys.path.insert(0, os.path.join(
? ? ? ? os.path.split(__file__)[0], '..'))
> Yes, the gobbledygook is caused by selecting html email, rather than the needed text form. As for the code that's buried inside there, it seems to have no usefulness if the file is run directly as a script, but if it's imported from another script, it adds a particular directory to the search path. Subsequent imports may find imports in that directory. -- DaveA From eryksun at gmail.com Thu Aug 8 03:58:02 2013 From: eryksun at gmail.com (eryksun) Date: Wed, 7 Aug 2013 21:58:02 -0400 Subject: [Tutor] Start multiple threads from Python In-Reply-To: <20130805192630.GA1345@gopher> References: <20130805192630.GA1345@gopher> Message-ID: On Mon, Aug 5, 2013 at 3:26 PM, Chris Down wrote: > subprocess.Popen does not block unless you explicitly tell it to (by using > communicate()). Perhaps that's what you want. Popen.wait is the method that blocks until the process exits. On POSIX systems this uses waitpid. On Windows it uses WaitForSingleObject with the process handle. From eryksun at gmail.com Thu Aug 8 04:06:38 2013 From: eryksun at gmail.com (eryksun) Date: Wed, 7 Aug 2013 22:06:38 -0400 Subject: [Tutor] Can anyone explain this In-Reply-To: References: Message-ID: On Wed, Aug 7, 2013 at 8:11 PM, Dave Angel wrote: > As for the code that's buried inside there, it seems to have no > usefulness if the file is run directly as a script Amandeep, adding '..' to sys.path is an unusual thing to do. That's relative to the current working directory. What's the source for this? plain text version: import sys import os if __name__ == '__main__': sys.path.insert(0, "..") else: sys.path.insert(0, os.path.join( os.path.split(__file__)[0], '..')) From marc.tompkins at gmail.com Thu Aug 8 04:19:39 2013 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Wed, 7 Aug 2013 19:19:39 -0700 Subject: [Tutor] Can anyone explain this In-Reply-To: References: Message-ID: On Fri, Aug 2, 2013 at 4:33 PM, Amandeep Behl wrote: > Can anyone explain this code below: > > > import sys > import os > > if __name__ == '__main__': > sys.path.insert(0, "..") > else: > sys.path.insert(0, os.path.join( > os.path.split(__file__)[0], '..')) > When you run this module as a standalone, the value of __name__ will be "__main__"; this is a standard test to see whether you're running it from the command line or importing it from another script. If you're running this code standalone, it will insert the directory one level up from the current working directory (..) as the first item in the Python path, so that subsequent imports will look there first. If you're calling this code from another script, it can't rely on knowing the current working directory - so it calls os.path.split to find the directory where this file is located; then it inserts the parent of that directory as the first item in the Python path. -------------- next part -------------- An HTML attachment was scrubbed... URL: From cybervigilante at gmail.com Thu Aug 8 05:54:26 2013 From: cybervigilante at gmail.com (Jim Mooney) Date: Wed, 7 Aug 2013 20:54:26 -0700 Subject: [Tutor] inconsistent destruction Message-ID: This bugs me for some reason. The final variable is saved in a for loop but not in a list comprehension. It just seems to me they should both be destroyed to avoid confusion. lst = [] for cnt in range(5): lst.append(cnt) cnt 4 lst = [cnt2 for cnt2 in range(5)] cnt2 builtins.NameError: name 'cnt2' is not defined Is there a form of for loop that would destroy the loop variable? I could always do del cnt right after the for, but that seems artificial. -- Jim From cybervigilante at gmail.com Thu Aug 8 06:26:23 2013 From: cybervigilante at gmail.com (Jim Mooney) Date: Wed, 7 Aug 2013 21:26:23 -0700 Subject: [Tutor] inconsistent destruction In-Reply-To: References: Message-ID: On 7 August 2013 21:23, David Hutto wrote: Oh, I don't want the variable from the list comp. It's the cnt in the for loop I don't want hanging around after iteration, just waiting to make trouble. It seems like a loose end. -- Jim From steve at pearwood.info Thu Aug 8 08:34:19 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 8 Aug 2013 16:34:19 +1000 Subject: [Tutor] inconsistent destruction In-Reply-To: References: Message-ID: <20130808063419.GI17751@ando> On Wed, Aug 07, 2013 at 08:54:26PM -0700, Jim Mooney wrote: > This bugs me for some reason. The final variable is saved in a for > loop but not in a list comprehension. It just seems to me they should > both be destroyed to avoid confusion. The variable in a for-loop is deemed to be in the same scope as the rest of the function, the same as any other local variable. This is often very useful: for item in objects: if condition(item): break print(item) If for-loops introduced their own scope, as they do in a few other languages, you would need to do something like this: found = None for item in objects: if condition(item): found = item break print(found) On the other hand, list comprehensions (since Python 3) and generator expressions (always) are deemed to exist in their own scope. That makes conceptual sense, since a generator expression is its own chunk of executable code, like a function, and in fact are implemented much the same way that functions are implemented internally. List comps look the same, and since Python 3 are now treated the same. That way you can use a list comp or generator expression without worrying that its loop variable will clobber an existing local variable of the same name. [...] > Is there a form of for loop that would destroy the loop variable? I > could always do del cnt right after the for, but that seems > artificial. Why do you care? The typical function is chock full of local variables that are used a few times, then hang around doing nothing until the function exits. This doesn't cause any problems in practice. -- Steven From eryksun at gmail.com Thu Aug 8 08:15:32 2013 From: eryksun at gmail.com (eryksun) Date: Thu, 8 Aug 2013 02:15:32 -0400 Subject: [Tutor] inconsistent destruction In-Reply-To: References: Message-ID: On Wed, Aug 7, 2013 at 11:54 PM, Jim Mooney wrote: > This bugs me for some reason. The final variable is saved in a for > loop but not in a list comprehension. It just seems to me they should > both be destroyed to avoid confusion. FYI, in 2.x a comprehension executes in the current scope, i.e. it leaks. A 3.x comprehension uses a function: >>> code = compile('[cnt for cnt in range(5)]', '', 'eval') The main code instantiates the function and iterator, and then calls the function: >>> dis.dis(code) 0 LOAD_CONST 0 () 3 LOAD_CONST 1 ('') 6 MAKE_FUNCTION 0 9 LOAD_NAME 0 (range) 12 LOAD_CONST 2 (5) 15 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 18 GET_ITER 19 CALL_FUNCTION 1 (1 positional, 0 keyword pair) 22 RETURN_VALUE The 0th constant is the compiled code for the comprehension: >>> dis.dis(code.co_consts[0]) 0 BUILD_LIST 0 3 LOAD_FAST 0 (.0) 6 FOR_ITER 12 (to 21) 9 STORE_FAST 1 (cnt) 12 LOAD_FAST 1 (cnt) 15 LIST_APPEND 2 18 JUMP_ABSOLUTE 6 21 RETURN_VALUE > lst = [] > for cnt in range(5): > lst.append(cnt) > cnt > 4 > > Is there a form of for loop that would destroy the loop variable? I > could always do del cnt right after the for, but that seems > artificial. There are tricks you can do manually with exec() and decorators, but nothing simple enough to be worth considering. Just use a function; the loop will even perform a bit better with "cnt" as a fast local instead of a dict key. Nick Coghlan has given the problem a lot of thought in two competing PEPs (both deferred): Statement local namespaces (aka "given" clause) http://www.python.org/dev/peps/pep-3150 General purpose decorator clause (aka "@in" clause) http://www.python.org/dev/peps/pep-0403 From alan.gauld at btinternet.com Thu Aug 8 09:29:29 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 08 Aug 2013 08:29:29 +0100 Subject: [Tutor] Python Trouble, Please Help! In-Reply-To: References: Message-ID: On 25/07/13 00:23, Jonathan Hong wrote: > because of my lack of computer knowledge. When I do put in python > helloworld.py into the command prompt line, the computer gives me a > response of: "python: can't open file 'hello.py': [Errno 2] No such file You need to supply the full path to the file (or be in the same directory as the file) when you try to run it. Mostly on Windows you can run scripts from Windows explorer but for simple scripts (like hello world) the window appears and then dissappears too quickly to see it. You can most easily get around that by putting one of: raw_input("Hit Enter to quit") # for Python v2 input("Hit Enter to quit") # for Python v3 as the last line in your script. You might find that technique easier than using the CMD window. But the CMD window is better when things go wrong - it lets you see any error messages,... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From oscar.j.benjamin at gmail.com Thu Aug 8 15:38:33 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 8 Aug 2013 14:38:33 +0100 Subject: [Tutor] Question in regards to loops and matplotlib In-Reply-To: References: Message-ID: On 1 August 2013 06:21, Zachary Rizer wrote: > So I just started coding, and I'm so glad I chose Python to start me off! I > really enjoy the clean layout, easy syntax, and power of the language. I'm > doing astronomy research and I just started teaching myself matplotlib along > with my general python work. I feel like I'm catching on quick, but I also > feel that this particular plot script is a little rough. The plot looks > correct, but the code seems really long for what I'm trying to do. So any > tricks to make this more efficient would be greatly appreciated! Hi Zachary, You can definitely make this shorter. I can't run your code since you didn't provide any data. It's good to use dummy data in examples posted to mailing lists so that others can run the code. Instead I'll show an example with my own dummy data: #!/usr/bin/env python # # Plot heights and weights of subjects from different groups. from matplotlib import pyplot as plt # Dummy data subjects = [ # Group, height (m), weight (kg), diagnosis {'type': 'patient', 'height': 1.8, 'weight': 90 , 'diag': 'acute' }, {'type': 'patient', 'height': 1.6, 'weight': 85 , 'diag': 'acute' }, {'type': 'patient', 'height': 1.9, 'weight': 120, 'diag': 'chronic'}, {'type': 'patient', 'height': 2.0, 'weight': 110, 'diag': 'chronic'}, {'type': 'control', 'height': 1.7, 'weight': 60 , 'diag': 'N/A' }, {'type': 'control', 'height': 2.1, 'weight': 100, 'diag': 'N/A' }, ] # Have just one place where we define the properties of each group groups = { 'control': { 'indicator': lambda s: s['type'] == 'control', 'color': 'blue', 'marker': '*', 'label':'Controls' }, 'chronic': { 'indicator': lambda s: s['diag'] == 'chronic', 'color': 'magenta', 'marker': 's', 'label':'Chronic patients' }, 'acute' : { 'indicator': lambda s: s['diag'] == 'acute', 'color': 'red', 'marker': 'o', 'label':'Acute patients' }, } # Now we can reuse the same code for every subject for groupdata in groups.values(): groupdata['subjects'] = [] # Distribute the subjects to each group for subject in subjects: for groupdata in groups.values(): isingroup = groupdata['indicator'] if isingroup(subject): groupdata['subjects'].append(subject) break # Now create/format the figure fig = plt.figure(figsize=(6, 5)) ax = fig.add_axes([0.15, 0.15, 0.70, 0.70]) ax.set_xlabel(r'Weight ($kg$)') ax.set_ylabel(r'Height ($m$)') ax.set_title('Height vs. weight by subject group') ax.set_xlim([50, 150]) ax.set_ylim([1.5, 2.2]) # Plot each group separately with its own format settings for group, groupdata in groups.items(): xdata = [s['weight'] for s in groupdata['subjects']] ydata = [s['height'] for s in groupdata['subjects']] ax.plot(xdata, ydata, linestyle='none', color=groupdata['color'], marker=groupdata['marker'], label=groupdata['label']) ax.legend(loc='upper left', numpoints=1) # Show the figure in GUI by default. # Save to e.g. 'plot.pdf' if filename is given. import sys if len(sys.argv) > 1: imgname = sys.argv[1] fig.savefig(imgname) else: plt.show() Oscar From oscar.j.benjamin at gmail.com Thu Aug 8 15:45:35 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 8 Aug 2013 14:45:35 +0100 Subject: [Tutor] (no subject) In-Reply-To: References: Message-ID: On 25 July 2013 03:36, Rocko Brown wrote: > Hello, > > I am trying to generate a 2D histogram for two arrays of data in CSV format. > > The code is: > > x, y = '5k_vec.csv','tcd_5k.csv' > H, xedges, yedges = np.histogram2d(x, y) > H.shape, xedges.shape, yedges.shape Your using x and y as if they are your data. Presumably your data is contained in the files '5k_vec.csv' and 'tcd_5k.csv'. However just writing x = '5k_vec.csv' will not load the data from the file. It just creates a string with those characters. Presumably you wanted something like: with open('5k_vec.csv') as inputfile: x = [float(line) for line in inputfile] (I'm assuming your file just has a number on each line here). Have a read about files here: http://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files And the csv module may be helpful too: http://docs.python.org/3.3/library/csv.html Oscar From oscar.j.benjamin at gmail.com Thu Aug 8 16:13:59 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 8 Aug 2013 15:13:59 +0100 Subject: [Tutor] hi In-Reply-To: <000001ce93b7$24a7d480$6df77d80$@orange.mu> References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> Message-ID: On 7 August 2013 22:43, Vick wrote: > Hi, > > It just plot one line. How to print several lines? Just do: line1, = plot([], []) line2, = plot([], []) And then line1.set_data(x1, y1) line2.set_data(x2, y2) and so on. > I've attached your example and I've corrected my own 3d code which works now > but it is not an animation. So how can I plot it using the code you gave me? Just change the data that you use for the line. > By the way, I think you know that I'm trying to plot for n-body problem? > Have you made a python code for the n-body problem? No I haven't but I have written code for computing numerical solutions of ODEs. > Mine works, but I'm just > having trouble with 3d plotting of the orbits. Once you have the lists of x, y, and z coordinates for the orbit just call: plot(x, y, z) (after creating an axes with projection='3d' and importing mplot3d). What's not working for you? > And also I have searched the > web on how to integrate conservation of energy as you suggested to me, but I > couldn't find any article that talks about 1st order ODEs for energy, and > the annotation is different from mine. I think you misunderstood. What I meant was if you implement your own routine for solving ODEs you need to check its accuracy. In a 2-body problem you would have an analytic solution in closed form. You can then check the error between your numerical solution and the analytic one. Investigating how this error scales with the step size dt enables you to confirm the order of your integrator. The reason I suggested this was that the original code you sent implemented the Runge-Kutta algorithm incorrectly and I wanted you to check your code properly before assuming it works. In the case of the N-body problem you don't have an analytic solution but, assuming Hamiltonian dynamics, energy is conserved. You can compute the energy of your system at each state and compare how constant it is over time in the output from your integrator. This gives you a proxy for not having an analytic solution that you can use to check the accuracy of your integrator: if the energy of your system is not (approximately) maintained during integration then your integration must be inaccurate. > PS: Also how do you make the plot run only once without having to do it over > and over again? I'm not sure TBH. I normally use the animation API to save video files for presentations. You may be more interested in the approach here rather than using the animation API: http://stackoverflow.com/questions/11874767/real-time-plotting-in-while-loop-with-matplotlib Oscar From sunithanc at gmail.com Thu Aug 8 18:23:05 2013 From: sunithanc at gmail.com (SM) Date: Thu, 8 Aug 2013 12:23:05 -0400 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? Message-ID: I am defining multiple classes (some of them are threads) I have been writing Python programs for a few months, but never faced this issue so far unless I am doing something different inadvertently. The only difference I see is that I am calling the methods belonging to other classes, from a class which is also a thread. I see the following error: AttributeError: 'Ui_MainWindow' object has no attribute 'textEdit_fwcmdlineoutput' Code Snippets: class Ui_MainWindow(object): [snip] def setStdoutToTextEditWindowFw(self): self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() ) sys.stdout = self.oldstdout Calling the above method from within the class works fine. But I am calling it from another class as below: class bcThread(threading.Thread): def __init__(self, cmd): threading.Thread.__init__(self) self.cmd = cmd def run(self): [snip] Ui_MainWindow.setStdoutToTextEditWindowFw(Ui_MainWindow) The highlighted line gives the following error : Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python3.2/threading.py", line 740, in _bootstrap_inner self.run() File "bc_reports_tab.py", line 1299, in run Ui_MainWindow.setStdoutToTextEditWindowFw(Ui_MainWindow) File "bc_reports_tab.py", line 465, in setStdoutToTextEditWindowFw self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() ) AttributeError: type object 'Ui_MainWindow' has no attribute 'textEdit_fwcmdlineoutput' I also tried many different ways of calling the method. The highlighted line is one of them. Another one I tried is here where I create an instance, which also gives the same error: x = Ui_MainWindow() x.setStdoutToTextEditWindowFw() I see the same error. Can someone guide me as to what is the correct way to do something like this? Thanks in advance. -SM -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Aug 8 19:22:12 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 08 Aug 2013 18:22:12 +0100 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: References: Message-ID: On 08/08/13 17:23, SM wrote: > I am defining multiple classes (some of them are threads) > I have been writing Python programs for a few months, but never faced > this issue so far unless I am doing something different inadvertently. > The only difference I see is that I am calling the methods belonging to > other classes, from a class which is also a thread. Your main issue is that you are trying to program using classes not objects. The normal usage of classes is to create an object from the class and then call the methods via the objects. You can pass objects around as arguments to the methods etc. So if you have two classes A and B you can create objects from those called, say, a and b: class A: def f(self): print 'in A.f' class B: def g(self): print 'in B.g' def h(self,anObject) anObject.f() #cxall method of the object passed in #create the object instances a = A() b = B() a2 = A() # a second object of class A. #Now we can call the methods using the objects a.f() #-> print "in A.f" b.g() #-> print "in B.g" a2.f() #-> print "in A.f" again # pass object a into object B's h() method b.h(a) # use a from b to print "in A.f" b.h(a2) # and again using a2 this time So by creating objects within your threads you can access the methods of your classes. You can even access global level objects from multiple threads and share state - but be careful about synchronising and locking issues. Finally you need to make sure your classes are visibnle in the threads that use them. So if you define a master class per thread and keep each in its own module then each module will need to import the other class modules before it can use them. I'm going out soon and typed that in a hurry. I hope it makes sense and doesn't have too many mistakes(all untested!). HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From ramit.prasad at jpmorgan.com.dmarc.invalid Thu Aug 8 19:17:18 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Thu, 8 Aug 2013 17:17:18 +0000 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF474186264CF@SCACMX008.exchad.jpmchase.net> SM wrote: > > I am defining multiple classes (some of them are threads) > I have been writing Python programs for a few months, but never faced this issue so far unless I am > doing something different inadvertently. The only difference I see is that I am calling the methods > belonging to other classes, from a class which is also a thread. > ?I see the following error: > > AttributeError: 'Ui_MainWindow' object has no attribute 'textEdit_fwcmdlineoutput' > Code Snippets: > > class Ui_MainWindow(object): > ??? [snip] > > ??? def setStdoutToTextEditWindowFw(self): > ?????? ?? self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() ) > ?????? ?? sys.stdout = self.oldstdout > Calling the above method from within the class works fine. But I am calling it from another class as > below: > > class bcThread(threading.Thread): > ??? def __init__(self, cmd): > ??????? threading.Thread.__init__(self) > ??????? self.cmd = cmd > ??? def run(self): > ??????? [snip] > ??????? Ui_MainWindow.setStdoutToTextEditWindowFw(Ui_MainWindow) > The highlighted line gives the following error : > > Exception in thread Thread-1: > Traceback (most recent call last): > ? File "/usr/lib/python3.2/threading.py", line 740, in _bootstrap_inner > ??? self.run() > ? File "bc_reports_tab.py", line 1299, in run > ??? Ui_MainWindow.setStdoutToTextEditWindowFw(Ui_MainWindow) > ? File "bc_reports_tab.py", line 465, in setStdoutToTextEditWindowFw > ??? self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() ) > AttributeError: type object 'Ui_MainWindow' has no attribute 'textEdit_fwcmdlineoutput' > I also tried many different ways of calling the method. The highlighted line is one of them. Another > one I tried is here where I create an instance, which also gives the same error: > > x = Ui_MainWindow() > x.setStdoutToTextEditWindowFw() > I see the same error. I do not think that actually gave you the same error. Most likely it gave you a *similar* error. See the below. >>> o = object() >>> o.blah # on instance object, not class object AttributeError: 'object' object has no attribute 'blah' >>> object.blah # on class object, not instance object AttributeError: type object 'object' has no attribute 'blah' If you got the top error from the snippet where you instantiate an instance of Ui_MainWindow then I believe some function must create the textEdit_fwcmdlineoutput attribute and that needs to be called first. I would have imagined this creation should occur on __init__ but obviously it must be somewhere else. You will need to call that section of code. It should be in the Ui_MainWindow class and look something like: self.textEdit_fwcmdlineoutput = > Can someone guide me as to what is the correct way to do something like this? > > Thanks in advance. > -SM > This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From sunithanc at gmail.com Thu Aug 8 22:03:21 2013 From: sunithanc at gmail.com (SM) Date: Thu, 8 Aug 2013 16:03:21 -0400 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: <5B80DD153D7D744689F57F4FB69AF474186264CF@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF474186264CF@SCACMX008.exchad.jpmchase.net> Message-ID: Ramit: Thanks for the quick response. You are right about the error. When I did the following: x = Ui_MainWindow() x.setStdoutToTextEditWindowFw() I got the following error: AttributeError: 'Ui_MainWindow' object has no attribute 'textEdit_fwcmdlineoutput' But I do have code that creates an attribute in Ui_MainWindow() class: self.textEdit_fwcmdlineoutput = QtGui.QTextEdit(self.tab_fw) This is what is making me get confused as to why it complains that there is no attribute. Thanks, -SM On Thu, Aug 8, 2013 at 1:17 PM, Prasad, Ramit wrote: > SM wrote: > > > > I am defining multiple classes (some of them are threads) > > I have been writing Python programs for a few months, but never faced > this issue so far unless I am > > doing something different inadvertently. The only difference I see is > that I am calling the methods > > belonging to other classes, from a class which is also a thread. > > I see the following error: > > > > AttributeError: 'Ui_MainWindow' object has no attribute > 'textEdit_fwcmdlineoutput' > > Code Snippets: > > > > class Ui_MainWindow(object): > > [snip] > > > > def setStdoutToTextEditWindowFw(self): > > self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() ) > > sys.stdout = self.oldstdout > > Calling the above method from within the class works fine. But I am > calling it from another class as > > below: > > > > class bcThread(threading.Thread): > > def __init__(self, cmd): > > threading.Thread.__init__(self) > > self.cmd = cmd > > def run(self): > > [snip] > > Ui_MainWindow.setStdoutToTextEditWindowFw(Ui_MainWindow) > > The highlighted line gives the following error : > > > > Exception in thread Thread-1: > > Traceback (most recent call last): > > File "/usr/lib/python3.2/threading.py", line 740, in _bootstrap_inner > > self.run() > > File "bc_reports_tab.py", line 1299, in run > > Ui_MainWindow.setStdoutToTextEditWindowFw(Ui_MainWindow) > > File "bc_reports_tab.py", line 465, in setStdoutToTextEditWindowFw > > self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() ) > > AttributeError: type object 'Ui_MainWindow' has no attribute > 'textEdit_fwcmdlineoutput' > > I also tried many different ways of calling the method. The highlighted > line is one of them. Another > > one I tried is here where I create an instance, which also gives the > same error: > > > > x = Ui_MainWindow() > > x.setStdoutToTextEditWindowFw() > > I see the same error. > > I do not think that actually gave you the same error. > Most likely it gave you a *similar* error. See the below. > > >>> o = object() > >>> o.blah # on instance object, not class object > AttributeError: 'object' object has no attribute 'blah' > > >>> object.blah # on class object, not instance object > AttributeError: type object 'object' has no attribute 'blah' > > > If you got the top error from the snippet where you instantiate > an instance of Ui_MainWindow then I believe some function must > create the textEdit_fwcmdlineoutput attribute and that needs > to be called first. I would have imagined this creation should > occur on __init__ but obviously it must be somewhere else. > You will need to call that section of code. It should be in > the Ui_MainWindow class and look something like: > > self.textEdit_fwcmdlineoutput = > > > Can someone guide me as to what is the correct way to do something like > this? > > > > Thanks in advance. > > -SM > > > > > > This email is confidential and subject to important disclaimers and > conditions including on offers for the purchase or sale of securities, > accuracy and completeness of information, viruses, confidentiality, legal > privilege, and legal entity disclaimers, available at > http://www.jpmorgan.com/pages/disclosures/email. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sunithanc at gmail.com Thu Aug 8 22:24:41 2013 From: sunithanc at gmail.com (SM) Date: Thu, 8 Aug 2013 16:24:41 -0400 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: References: Message-ID: Hi Alan, Thanks for your quick and detailed reply. I guess what you are suggesting is very similar to what I am doing (of course not when I use the class. I tried using the class when I couldn't figure out why it gave error when I used the object). Looking at your example, I ended up doing exactly what you suggested. I am sure I am missing something, as it is giving the same error: Here is what I am doing based on your suggestion: class bcThread(threading.Thread): def __init__(self, cmd): threading.Thread.__init__(self) self.cmd = cmd def h(self, y): y.setStdoutToTextEditWindowFw() #(line 1277) def run(self): # Run the command first. (data, err) = Popen(self.cmd, stdout=PIPE, stderr=PIPE).communicate() [snip] x = Ui_MainWindow() self.h(x) # (line 1319) class Ui_MainWindow(object) def setupUi(self, MainWindow): [snip] self.textEdit_fwcmdlineoutput = QtGui.QTextEdit(self.tab_fw) self.textEdit_fwcmdlineoutput.setObjectName(_fromUtf8("textEdit_fwcmdlineoutput")) [snip] (line: 475) def setStdoutToTextEditWindowFw(self): self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() ) sys.stdout = self.oldstdout The error looks like this: Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python3.2/threading.py", line 740, in _bootstrap_inner self.run() File "bc_reports_tab.py", line 1319, in run self.h(x) File "bc_reports_tab.py", line 1277, in h y.setStdoutToTextEditWindowFw() File "bc_reports_tab.py", line 475, in setStdoutToTextEditWindowFw self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() ) AttributeError: 'Ui_MainWindow' object has no attribute 'textEdit_fwcmdlineoutput' Wonder why it says the class/object has no attribute. Thanks, -SM On Thu, Aug 8, 2013 at 1:22 PM, Alan Gauld wrote: > On 08/08/13 17:23, SM wrote: > >> I am defining multiple classes (some of them are threads) >> I have been writing Python programs for a few months, but never faced >> this issue so far unless I am doing something different inadvertently. >> The only difference I see is that I am calling the methods belonging to >> other classes, from a class which is also a thread. >> > > Your main issue is that you are trying to program using classes not > objects. > The normal usage of classes is to create an object from the class and then > call the methods via the objects. You can pass objects around as arguments > to the methods etc. > > So if you have two classes A and B you can create objects > from those called, say, a and b: > > class A: > def f(self): > print 'in A.f' > > class B: > def g(self): > print 'in B.g' > def h(self,anObject) > anObject.f() #cxall method of the object passed in > > #create the object instances > a = A() > b = B() > a2 = A() # a second object of class A. > > > #Now we can call the methods using the objects > a.f() #-> print "in A.f" > b.g() #-> print "in B.g" > a2.f() #-> print "in A.f" again > > # pass object a into object B's h() method > b.h(a) # use a from b to print "in A.f" > b.h(a2) # and again using a2 this time > > So by creating objects within your threads you > can access the methods of your classes. You can even access > global level objects from multiple threads and share state > - but be careful about synchronising and locking issues. > > Finally you need to make sure your classes are visibnle in the threads > that use them. So if you define a master class per thread and keep each in > its own module then each module will need to import the other class modules > before it can use them. > > I'm going out soon and typed that in a hurry. I hope it makes sense and > doesn't have too many mistakes(all untested!). > > HTH > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From ramit.prasad at jpmorgan.com.dmarc.invalid Thu Aug 8 22:41:41 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Thu, 8 Aug 2013 20:41:41 +0000 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF474186264CF@SCACMX008.exchad.jpmchase.net> Message-ID: <5B80DD153D7D744689F57F4FB69AF47418626FEB@SCACMX008.exchad.jpmchase.net> Please post responses in-line or at the bottom for this mailing list. SM wrote: > > Ramit: > Thanks for the quick response. You are right about the error. When I did the following: > x = Ui_MainWindow() > x.setStdoutToTextEditWindowFw() > I got the following error: > AttributeError: 'Ui_MainWindow' object has no attribute 'textEdit_fwcmdlineoutput' > But I do have code that creates an attribute in Ui_MainWindow() class: > > self.textEdit_fwcmdlineoutput = QtGui.QTextEdit(self.tab_fw) > This is what is making me get confused as to why it complains that there is no attribute. > Thanks, > -SM You need to call that line of code to create the widget. Obviously it is not being created from the class's __init__. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From alan.gauld at btinternet.com Thu Aug 8 22:57:07 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 08 Aug 2013 21:57:07 +0100 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: References: Message-ID: On 08/08/13 21:24, SM wrote: > example, I ended up doing exactly what you suggested. I am sure I am > missing something, as it is giving the same error: > Here is what I am doing based on your suggestion: > > class bcThread(threading.Thread): > def h(self, y): > y.setStdoutToTextEditWindowFw() #(line 1277) ... > [snip] > x = Ui_MainWindow() > self.h(x) # (line 1319) So far so good. > class Ui_MainWindow(object) > def setupUi(self, MainWindow): > [snip] > > self.textEdit_fwcmdlineoutput = QtGui.QTextEdit(self.tab_fw) This only comes into existence after setupUi is called. Normally that would be in the UI init() method but you don;t show any init or any other call to setupUi > self.textEdit_fwcmdlineoutput.setObjectName(_fromUtf8("textEdit_fwcmdlineoutput")) > def setStdoutToTextEditWindowFw(self): > self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() ) So this fails if setupUi has not been called earlier because the attribute has not been initialised yet. > self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() ) > AttributeError: 'Ui_MainWindow' object has no attribute > 'textEdit_fwcmdlineoutput' You need to ensure that the UI __init__() method calls self.setupUi() I suspect. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From sunithanc at gmail.com Thu Aug 8 22:57:54 2013 From: sunithanc at gmail.com (SM) Date: Thu, 8 Aug 2013 16:57:54 -0400 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47418626FEB@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF474186264CF@SCACMX008.exchad.jpmchase.net> <5B80DD153D7D744689F57F4FB69AF47418626FEB@SCACMX008.exchad.jpmchase.net> Message-ID: Is it necessary that the call should be in __init__? The widget is very much getting created and I have the line (copied again here) under a method setupUi() under Ui_MainWindow: class Ui_MainWindow(object): [snip] def setupUi(self, MainWindow): [snip] self.textEdit_fwcmdlineoutput = QtGui.QTextEdit(self.tab_fw) self.textEdit_fwcmdlineoutput.setObjectName(_fromUtf8("textEdit_fwcmdlineoutput")) self.gridLayout.addWidget(self.textEdit_fwcmdlineoutput, 6, 0, 1, 3) Thanks, -SM On Thu, Aug 8, 2013 at 4:41 PM, Prasad, Ramit wrote: > Please post responses in-line or at the bottom for this > mailing list. > > SM wrote: > > > > Ramit: > > Thanks for the quick response. You are right about the error. When I did > the following: > > x = Ui_MainWindow() > > x.setStdoutToTextEditWindowFw() > > I got the following error: > > AttributeError: 'Ui_MainWindow' object has no attribute > 'textEdit_fwcmdlineoutput' > > But I do have code that creates an attribute in Ui_MainWindow() class: > > > > self.textEdit_fwcmdlineoutput = QtGui.QTextEdit(self.tab_fw) > > This is what is making me get confused as to why it complains that there > is no attribute. > > Thanks, > > -SM > > You need to call that line of code to create the widget. Obviously > it is not being created from the class's __init__. > > > ~Ramit > > > > This email is confidential and subject to important disclaimers and > conditions including on offers for the purchase or sale of securities, > accuracy and completeness of information, viruses, confidentiality, legal > privilege, and legal entity disclaimers, available at > http://www.jpmorgan.com/pages/disclosures/email. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From sunithanc at gmail.com Thu Aug 8 23:03:43 2013 From: sunithanc at gmail.com (SM) Date: Thu, 8 Aug 2013 17:03:43 -0400 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: References: Message-ID: On 08/08/13 21:24, SM wrote: example, I ended up doing exactly what you suggested. I am sure I am > missing something, as it is giving the same error: > Here is what I am doing based on your suggestion: > > class bcThread(threading.Thread): > def h(self, y): > y.setStdoutToTextEditWindowFw() #(line 1277) > ... [snip] > x = Ui_MainWindow() > self.h(x) # (line 1319) > So far so good. class Ui_MainWindow(object) > def setupUi(self, MainWindow): > [snip] > > self.textEdit_fwcmdlineoutput = QtGui.QTextEdit(self.tab_fw) > This only comes into existence after setupUi is called. Normally that would be in the UI init() method but you don;t show any init or any other call to setupUi [SM] Sorry that I didn't add that part. setupUI is called from the main as below: if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) MainWindow = QtGui.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_()) Wonder if it is necessary to call it through __init__. I used Py Qt4 Designer to generate the skeleton of the Gui code and have been using it like this for sometime now. Thanks, -SM self.textEdit_fwcmdlineoutput.setObjectName(_fromUtf8("textEdit_fwcmdlineoutput")) > def setStdoutToTextEditWindowFw(self): > self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() ) > So this fails if setupUi has not been called earlier because the attribute has not been initialised yet. self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() ) > AttributeError: 'Ui_MainWindow' object has no attribute > 'textEdit_fwcmdlineoutput' > You need to ensure that the UI __init__() method calls self.setupUi() I suspect. On Thu, Aug 8, 2013 at 4:57 PM, Alan Gauld wrote: > On 08/08/13 21:24, SM wrote: > > example, I ended up doing exactly what you suggested. I am sure I am >> missing something, as it is giving the same error: >> Here is what I am doing based on your suggestion: >> >> class bcThread(threading.Thread): >> def h(self, y): >> y.setStdoutToTextEditWindowFw(**) #(line 1277) >> > > ... > > [snip] >> x = Ui_MainWindow() >> self.h(x) # (line 1319) >> > > So far so good. > > > class Ui_MainWindow(object) >> def setupUi(self, MainWindow): >> [snip] >> >> self.textEdit_fwcmdlineoutput = QtGui.QTextEdit(self.tab_fw) >> > > This only comes into existence after setupUi is called. > Normally that would be in the UI init() method but you don;t > show any init or any other call to setupUi > > self.textEdit_fwcmdlineoutput.**setObjectName(_fromUtf8("** >> textEdit_fwcmdlineoutput")) >> > > def setStdoutToTextEditWindowFw(**self): >> self.textEdit_fwcmdlineoutput.**setText( sys.stdout.getvalue() ) >> > > So this fails if setupUi has not been called earlier because the attribute > has not been initialised yet. > > > self.textEdit_fwcmdlineoutput.**setText( sys.stdout.getvalue() ) >> AttributeError: 'Ui_MainWindow' object has no attribute >> 'textEdit_fwcmdlineoutput' >> > > You need to ensure that the UI __init__() method calls self.setupUi() > I suspect. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Aug 8 23:47:47 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 08 Aug 2013 22:47:47 +0100 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: References: Message-ID: On 08/08/13 22:03, SM wrote: > [SM] Sorry that I didn't add that part. setupUI is called from the main > as below: > > if __name__ == "__main__": > import sys > app = QtGui.QApplication(sys.argv) > MainWindow = QtGui.QMainWindow() > ui = Ui_MainWindow() > ui.setupUi(MainWindow) > MainWindow.show() > sys.exit(app.exec_()) > > Wonder if it is necessary to call it through __init__. Thats the problem. You are creating a new instance inside your run() method but the call in main() only sets it for the instance created in main. It won't set it up for any other instance. You need to call it every time you create a new object. Unless.... If you put it in init() it will be called every time you create a new instance. Much safer. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eryksun at gmail.com Fri Aug 9 00:02:27 2013 From: eryksun at gmail.com (eryksun) Date: Thu, 8 Aug 2013 18:02:27 -0400 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: References: Message-ID: On Thu, Aug 8, 2013 at 5:47 PM, Alan Gauld wrote: > On 08/08/13 22:03, SM wrote: > >> [SM] Sorry that I didn't add that part. setupUI is called from the main >> as below: >> >> if __name__ == "__main__": >> import sys >> app = QtGui.QApplication(sys.argv) >> MainWindow = QtGui.QMainWindow() >> ui = Ui_MainWindow() >> ui.setupUi(MainWindow) >> MainWindow.show() >> sys.exit(app.exec_()) >> >> Wonder if it is necessary to call it through __init__. > > > Thats the problem. > You are creating a new instance inside your run() method but > the call in main() only sets it for the instance created in > main. It won't set it up for any other instance. You need > to call it every time you create a new object. Unless.... > > If you put it in init() it will be called every time you > create a new instance. Much safer. I'm confused. Why does the OP want multiple instances of the user interface widgets? From alan.gauld at btinternet.com Fri Aug 9 00:30:15 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 08 Aug 2013 23:30:15 +0100 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: References: Message-ID: On 08/08/13 23:02, eryksun wrote: >> You are creating a new instance inside your run() method but >> the call in main() only sets it for the instance created in >> main. It won't set it up for any other instance. You need >> to call it every time you create a new object. Unless.... >> >> If you put it in init() it will be called every time you >> create a new instance. Much safer. > > I'm confused. Why does the OP want multiple instances of the user > interface widgets? He may not, but he is creating them! But it is possible he does want multiple duplicate windows, one monitoring each thread. (eg progress dialogs on an FTP program). Since I don't know the context I'm assuming he knows how many windows he wants! If he does want just one then he needs to reference the global object in his run() method... But that brings us back into the land of synchronisation issues and threadsafe locks etc. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From dwightdhutto at gmail.com Thu Aug 8 06:15:15 2013 From: dwightdhutto at gmail.com (David Hutto) Date: Thu, 8 Aug 2013 00:15:15 -0400 Subject: [Tutor] inconsistent destruction In-Reply-To: References: Message-ID: if the variable is the range in the first one, then just don't append it, and replace it with something else. The second, you use cnt2, but it is a part of the the list comp, but not a variable: #this is in [ython 3, but you can import from future, or remove quotes from print parameters lst = [cnt2 for cnt2 in range(5)] print(lst) you want cnt2, but cnt2 is in the list comprehension, so it is a variable within the list comp, but after it has been used there, then you need to iterate through the list(lst), in order to find particular cnt's within the lst list of cnt2 variables.. On Wed, Aug 7, 2013 at 11:54 PM, Jim Mooney wrote: > This bugs me for some reason. The final variable is saved in a for > loop but not in a list comprehension. It just seems to me they should > both be destroyed to avoid confusion. > > lst = [] > for cnt in range(5): > lst.append(cnt) > cnt > 4 > > lst = [cnt2 for cnt2 in range(5)] > cnt2 > builtins.NameError: name 'cnt2' is not defined > > Is there a form of for loop that would destroy the loop variable? I > could always do del cnt right after the for, but that seems > artificial. > > -- > Jim > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Best Regards, David Hutto *CEO:* *http://www.hitwebdevelopment.com* -------------- next part -------------- An HTML attachment was scrubbed... URL: From dwightdhutto at gmail.com Thu Aug 8 06:23:03 2013 From: dwightdhutto at gmail.com (David Hutto) Date: Thu, 8 Aug 2013 00:23:03 -0400 Subject: [Tutor] inconsistent destruction In-Reply-To: References: Message-ID: In other words lst is a global variable, where as cnt2 is a local variable to just the list comps completion of the data being diplayed in the list comp, with each cnt2 in the global var lst being contained in the list comp, but only accessible through iteration/placing/splicing of the lst variable that corresponds within the lst global variable which uses list methods. On Thu, Aug 8, 2013 at 12:15 AM, David Hutto wrote: > if the variable is the range in the first one, then just don't append it, > and replace it with something else. > > The second, you use cnt2, but it is a part of the the list comp, but not a > variable: > > #this is in [ython 3, but you can import from future, or remove quotes > from print parameters > lst = [cnt2 for cnt2 in range(5)] > print(lst) > > you want cnt2, but cnt2 is in the list comprehension, so it is a variable > within the list comp, but after it has been used there, then you need to > iterate through the list(lst), in order to find particular cnt's within the > lst list of cnt2 variables.. > > > > On Wed, Aug 7, 2013 at 11:54 PM, Jim Mooney wrote: > >> This bugs me for some reason. The final variable is saved in a for >> loop but not in a list comprehension. It just seems to me they should >> both be destroyed to avoid confusion. >> >> lst = [] >> for cnt in range(5): >> lst.append(cnt) >> cnt >> 4 >> >> lst = [cnt2 for cnt2 in range(5)] >> cnt2 >> builtins.NameError: name 'cnt2' is not defined >> >> Is there a form of for loop that would destroy the loop variable? I >> could always do del cnt right after the for, but that seems >> artificial. >> >> -- >> Jim >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > > > > -- > Best Regards, > David Hutto > *CEO:* *http://www.hitwebdevelopment.com* > -- Best Regards, David Hutto *CEO:* *http://www.hitwebdevelopment.com* -------------- next part -------------- An HTML attachment was scrubbed... URL: From japhy at pearachute.com Thu Aug 8 06:26:54 2013 From: japhy at pearachute.com (Japhy Bartlett) Date: Wed, 7 Aug 2013 23:26:54 -0500 Subject: [Tutor] inconsistent destruction In-Reply-To: References: Message-ID: you can either manually manage the memory with `del cnt` or let the built in memory management .. manage the memory. On Wed, Aug 7, 2013 at 10:54 PM, Jim Mooney wrote: > This bugs me for some reason. The final variable is saved in a for > loop but not in a list comprehension. It just seems to me they should > both be destroyed to avoid confusion. > > lst = [] > for cnt in range(5): > lst.append(cnt) > cnt > 4 > > lst = [cnt2 for cnt2 in range(5)] > cnt2 > builtins.NameError: name 'cnt2' is not defined > > Is there a form of for loop that would destroy the loop variable? I > could always do del cnt right after the for, but that seems > artificial. > > -- > Jim > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From john.feleppa at gmail.com Thu Aug 8 19:05:13 2013 From: john.feleppa at gmail.com (John Feleppa) Date: Thu, 8 Aug 2013 18:05:13 +0100 Subject: [Tutor] Python Programming for the absolute beginner 3e Ch3 Challenge 4 Message-ID: Hello, I am working through Michael Dawson's book, "Python Programming for the absolute beginner 3rd edition". Have just completed Chapter 3, but cannot solve Challenge 4. Has anyone solved this yet - and be willing to share this code? I would much appreciate.. John. -------------- next part -------------- An HTML attachment was scrubbed... URL: From protonlenny at gmail.com Thu Aug 8 17:58:18 2013 From: protonlenny at gmail.com (Zachary Rizer) Date: Thu, 8 Aug 2013 10:58:18 -0500 Subject: [Tutor] Question in regards to loops and matplotlib In-Reply-To: References: Message-ID: This looks quite clean! Since posting this question I have cleaned up this script by using multiple functions, but it still isn't this clean! My data is a normal CSV table with column headers. It isn't in a dictionary format like your sample data here. I'm sure I could switch it to that format though. I'll post a sample of my data, first 10 rows with column headers here, and my current rendition of the code. Also, not to familiar with the lambda function you did here. Little bit confused on how you split the groups up. Anyway, new version of code and sample data: I attached the ten row sample because it has 60 columns of info on each row and would look huge pasted. """ Zach Rizer UVJ plot of n>2 pop compare qui and sf disk and bulge pop data = n>2_qui_flag.csv """ import numpy as np import matplotlib.pyplot as plt import matplotlib.patches as patch from matplotlib.path import Path z1 = (0.6, 0.9, 0.59, 0.81, 2, "0.6= z_range[0] and row['pz_z'] < z_range[1]: z_data.append(row) z_data = np.array(z_data) return z_data def quiescence(table,z_range): """ takes given csv table and specific redshfit range and spits out a two-tuple where first index of the tuple is the quiescent pop and the second index is the star forming pop """ qui_data = [] sf_data = [] for row in table: if row['rf_UminV'] > 1.3 and row['rf_VminJ'] < 1.6 and row['rf_UminV'] > 0.88*(row['rf_VminJ'])+z_range[2]: qui_data.append(row) else: sf_data.append(row) qui_data = np.array(qui_data) sf_data = np.array(sf_data) return qui_data, sf_data def disk_vs_bulge(table): """ turns given csv table into a two-tuple array where the first index is the disk-dom pop and the second index is the bulge-dom pop: these cuts are determined by visclass data """ disk_data = [] bulge_data = [] for row in table: if row['vc_Dv'] > 0.65 and row['vc_DSw'] > 0.65: disk_data.append(row) elif row['vc_Dv'] < 0.35 and row['vc_DSw'] > 0.65: bulge_data.append(row) disk_data = np.array(disk_data) bulge_data = np.array(bulge_data) return disk_data, bulge_data def make_mass_cuts(table): """ make mass cuts using logMass row and creates four-tuple each index is divided into its own dual tuple, x and y UVJ colors array returned = [(m1_x, m1_y), (m2_x, m2_y), (m3_x, m3_y), (m4_x, m4_y)] """ m1_x = [] m1_y = [] m2_x = [] m2_y = [] m3_x = [] m3_y = [] m4_x = [] m4_y = [] for row in table: if row['ms_lmass'] >= 9.7 and row['ms_lmass'] < 10: m1_x.append(row['rf_VminJ']) m1_y.append(row['rf_UminV']) elif row['ms_lmass'] >= 10 and row['ms_lmass'] < 10.5: m2_x.append(row['rf_VminJ']) m2_y.append(row['rf_UminV']) elif row['ms_lmass'] >= 10.5 and row['ms_lmass'] < 10.8: m3_x.append(row['rf_VminJ']) m3_y.append(row['rf_UminV']) elif row['ms_lmass'] >= 10.8: m4_x.append(row['rf_VminJ']) m4_y.append(row['rf_UminV']) return [(m1_x, m1_y), (m2_x, m2_y), (m3_x, m3_y), (m4_x, m4_y)] def plots(m_q_disk, m_q_bulge, m_sf_disk, m_sf_bulge): """ plot the first column as x, and second column as y takes all four mass arrays and plots all 16 subplots returns a 3-tuple of plot of qui_bulge, sf_bulge, sf_disk for legend """ plt.plot(m_q_disk[3][0], m_q_disk[3][1], 'wo', ms=12) qui_bulge_label, = plt.plot(m_q_bulge[3][0], m_q_bulge[3][1], 'ro', ms=12) plt.plot(m_q_disk[2][0], m_q_disk[2][1], 'wo', ms=9) plt.plot(m_q_bulge[2][0], m_q_bulge[2][1], 'ro', ms=9) plt.plot(m_q_disk[1][0], m_q_disk[1][1], 'wo', ms=6) plt.plot(m_q_bulge[1][0], m_q_bulge[1][1], 'ro', ms=6) plt.plot(m_q_disk[0][0], m_q_disk[0][1], 'wo', ms=3) plt.plot(m_q_bulge[0][0], m_q_bulge[0][1], 'ro', ms=3) sf_disk_label, = plt.plot(m_sf_disk[3][0], m_sf_disk[3][1], 'wo', ms=12) sf_bulge_label, = plt.plot(m_sf_bulge[3][0], m_sf_bulge[3][1], 'bo', ms=12) plt.plot(m_sf_disk[2][0], m_sf_disk[2][1], 'wo', ms=9) plt.plot(m_sf_bulge[2][0], m_sf_bulge[2][1], 'bo', ms=9) plt.plot(m_sf_disk[1][0], m_sf_disk[1][1], 'wo', ms=6) plt.plot(m_sf_bulge[1][0], m_sf_bulge[1][1], 'bo', ms=6) plt.plot(m_sf_disk[0][0], m_sf_disk[0][1], 'wo', ms=3) plt.plot(m_sf_bulge[0][0], m_sf_bulge[0][1], 'bo', ms=3) return qui_bulge_label, sf_bulge_label, sf_disk_label def make_hash_region(z_range): """make quiescent region""" verts = [(-1.,1.3), (z_range[3],1.3), (1.6,z_range[4]), (1.6,2.5), (-1.,2.5), (-1.,1.3)] codes = [Path.MOVETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.LINETO, Path.CLOSEPOLY] return Path(verts,codes) def titles_labels(labels, z_range): """ creates axis labels, title, and legend associating labels to these three specific plots """ plt.title('UVJ Plot of n>2 population at ' + z_range[5]) plt.xlabel('V-J') plt.ylabel('U-V') plt.xlim(0.5, 2.0) plt.ylim(0.5, 2.5) plt.legend([labels[0], labels[1], labels[2]], ["Qui-Bulge-Dom", "SF-Bulge-Dom", "Disk-Dom"], 'best', numpoints=1, fontsize='small') def main(): """ calls total script to create plot to run, simply load csv in the first spot in the np.genfromtxt args and exchange the zbin value to either z1, z2, z3, or z4 """ fig = plt.figure('UVJ') n2_subset = np.genfromtxt('/Users/ProtonLenny/Documents/Research/Catalog_Data/Catalog_4/n>2.csv', dtype=None, names=True, delimiter =",") zbin = z4 z_data = redshift(n2_subset, zbin) qui_data = (quiescence(z_data, zbin))[0] sf_data = (quiescence(z_data, zbin))[1] qui_disk = (disk_vs_bulge(qui_data))[0] qui_bulge = (disk_vs_bulge(qui_data))[1] sf_disk = (disk_vs_bulge(sf_data))[0] sf_bulge = (disk_vs_bulge(sf_data))[1] m_q_disk = make_mass_cuts(qui_disk) m_q_bulge = make_mass_cuts(qui_bulge) m_sf_disk = make_mass_cuts(sf_disk) m_sf_bulge = make_mass_cuts(sf_bulge) legend_labels = plots(m_q_disk, m_q_bulge, m_sf_disk, m_sf_bulge) path = make_hash_region(zbin) titles_labels(legend_labels, zbin) hash_region = patch.PathPatch(path, facecolor='none', lw=1.5, alpha=0.5, hatch='//') ax = fig.add_subplot(111) #subplot for shaded region ax.add_patch(hash_region) plt.show() if __name__ == '__main__': main() On Thu, Aug 8, 2013 at 8:38 AM, Oscar Benjamin wrote: > On 1 August 2013 06:21, Zachary Rizer wrote: > > So I just started coding, and I'm so glad I chose Python to start me > off! I > > really enjoy the clean layout, easy syntax, and power of the language. > I'm > > doing astronomy research and I just started teaching myself matplotlib > along > > with my general python work. I feel like I'm catching on quick, but I > also > > feel that this particular plot script is a little rough. The plot looks > > correct, but the code seems really long for what I'm trying to do. So any > > tricks to make this more efficient would be greatly appreciated! > > Hi Zachary, > > You can definitely make this shorter. I can't run your code since you > didn't provide any data. It's good to use dummy data in examples > posted to mailing lists so that others can run the code. Instead I'll > show an example with my own dummy data: > > > #!/usr/bin/env python > # > # Plot heights and weights of subjects from different groups. > > from matplotlib import pyplot as plt > > # Dummy data > subjects = [ > # Group, height (m), weight (kg), diagnosis > {'type': 'patient', 'height': 1.8, 'weight': 90 , 'diag': 'acute' }, > {'type': 'patient', 'height': 1.6, 'weight': 85 , 'diag': 'acute' }, > {'type': 'patient', 'height': 1.9, 'weight': 120, 'diag': 'chronic'}, > {'type': 'patient', 'height': 2.0, 'weight': 110, 'diag': 'chronic'}, > {'type': 'control', 'height': 1.7, 'weight': 60 , 'diag': 'N/A' }, > {'type': 'control', 'height': 2.1, 'weight': 100, 'diag': 'N/A' }, > ] > > # Have just one place where we define the properties of each group > groups = { > 'control': { > 'indicator': lambda s: s['type'] == 'control', > 'color': 'blue', > 'marker': '*', > 'label':'Controls' > }, > 'chronic': { > 'indicator': lambda s: s['diag'] == 'chronic', > 'color': 'magenta', > 'marker': 's', > 'label':'Chronic patients' > }, > 'acute' : { > 'indicator': lambda s: s['diag'] == 'acute', > 'color': 'red', > 'marker': 'o', > 'label':'Acute patients' > }, > } > > # Now we can reuse the same code for every subject > for groupdata in groups.values(): > groupdata['subjects'] = [] > > # Distribute the subjects to each group > for subject in subjects: > for groupdata in groups.values(): > isingroup = groupdata['indicator'] > if isingroup(subject): > groupdata['subjects'].append(subject) > break > > # Now create/format the figure > fig = plt.figure(figsize=(6, 5)) > ax = fig.add_axes([0.15, 0.15, 0.70, 0.70]) > ax.set_xlabel(r'Weight ($kg$)') > ax.set_ylabel(r'Height ($m$)') > ax.set_title('Height vs. weight by subject group') > ax.set_xlim([50, 150]) > ax.set_ylim([1.5, 2.2]) > > # Plot each group separately with its own format settings > for group, groupdata in groups.items(): > xdata = [s['weight'] for s in groupdata['subjects']] > ydata = [s['height'] for s in groupdata['subjects']] > ax.plot(xdata, ydata, linestyle='none', color=groupdata['color'], > marker=groupdata['marker'], label=groupdata['label']) > > ax.legend(loc='upper left', numpoints=1) > > # Show the figure in GUI by default. > # Save to e.g. 'plot.pdf' if filename is given. > import sys > if len(sys.argv) > 1: > imgname = sys.argv[1] > fig.savefig(imgname) > else: > plt.show() > > > Oscar > -------------- next part -------------- An HTML attachment was scrubbed... URL: -------------- next part -------------- A non-text attachment was scrubbed... Name: sample.csv Type: text/csv Size: 5099 bytes Desc: not available URL: From tfarley at gmail.com Thu Aug 8 00:11:58 2013 From: tfarley at gmail.com (Travis Farley) Date: Wed, 7 Aug 2013 16:11:58 -0600 Subject: [Tutor] Online references for Python In-Reply-To: References: Message-ID: I've also found this to be helpful: http://www.diveintopython.net/toc/index.html It's a little dated but most of the information still applies. On Mon, Jul 29, 2013 at 10:29 AM, Rene Datta wrote: > Hi: > > I was wondering if anyone could suggest a good quality online resource > tool or compendium to provide a beginner's level of information to start > learning the Python language. > > Thank you. > > Rene Datta > renedatta at gmail.com > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From vick1975 at orange.mu Wed Aug 7 23:43:24 2013 From: vick1975 at orange.mu (Vick) Date: Thu, 8 Aug 2013 01:43:24 +0400 Subject: [Tutor] hi In-Reply-To: References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> Message-ID: <000001ce93b7$24a7d480$6df77d80$@orange.mu> Hi, It just plot one line. How to print several lines? I've attached your example and I've corrected my own 3d code which works now but it is not an animation. So how can I plot it using the code you gave me? By the way, I think you know that I'm trying to plot for n-body problem? Have you made a python code for the n-body problem? Mine works, but I'm just having trouble with 3d plotting of the orbits. And also I have searched the web on how to integrate conservation of energy as you suggested to me, but I couldn't find any article that talks about 1st order ODEs for energy, and the annotation is different from mine. PS: Also how do you make the plot run only once without having to do it over and over again? Thanks Vick -----Original Message----- From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] Sent: Tuesday, 06 August, 2013 18:40 To: Vick Cc: tutor at python.org Subject: Re: [Tutor] hi On 1 August 2013 12:32, Vick wrote: > Hi, Hi Vick, sorry I've been away and I've only had a chance to look at this now. > As per your request below, I have attached a stand-alone example > (test3d.py) of my problem. I am trying to plot in 3D using ion() from a loop. Basically don't use ion() for animation: it is intended for interactive use. Use matplotlib's animation API for animation. See here: http://jakevdp.github.io/blog/2012/08/18/matplotlib-animation-tutorial/ I've put example scripts below. Both run fine on this computer. You'll want a recent matplotlib version. Here's a 2d animation script: #!/usr/bin/env python # 2d animation import numpy as np from matplotlib import pyplot as plt from matplotlib import animation fig = plt.figure(figsize=(5, 5)) ax = fig.add_axes([0.15, 0.15, 0.70, 0.70]) line, = ax.plot([], [], linewidth=2) def init(): ax.set_xlim([-1, 1]) ax.set_ylim([-1, 1]) line.set_data([], []) return line, def animate(i): t = dt * np.arange(i) x = np.cos(omega * t) y = np.sin(omega * t) line.set_data(x, y) return line, dt = .02 # in seconds T = 10 # Period (seconds) omega = 2 * np.pi / T # 0.1 Hz anim = animation.FuncAnimation(fig, animate, init_func=init, frames=int(T/dt), interval=int(1000*dt), blit=True) plt.show() And here's a 3d script: #!/usr/bin/env python # 3d animation import numpy as np from matplotlib import pyplot as plt from matplotlib import animation from mpl_toolkits.mplot3d import Axes3D fig = plt.figure(figsize=(5, 5)) ax = fig.add_axes([0.15, 0.15, 0.70, 0.70], projection='3d') line, = ax.plot([], [], [], linewidth=2) def init(): ax.set_xlim([-1, 1]) ax.set_ylim([-1, 1]) ax.set_zlim([0, 10]) line.set_data([], []) return line, def animate(i): t = dt * np.arange(i) x = np.cos(omega * t) y = np.sin(omega * t) z = t line.set_data(x, y) line.set_3d_properties(z) # WTF! return line, dt = .02 # in seconds T = 10 # Duration (seconds) omega = 2 * np.pi # 1 Hz anim = animation.FuncAnimation(fig, animate, init_func=init, frames=int(T/dt), interval=int(1000*dt), blit=True) plt.show() Oscar -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: test3d.py URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: testanimpy.py URL: From vick1975 at orange.mu Thu Aug 8 19:22:57 2013 From: vick1975 at orange.mu (Vick) Date: Thu, 8 Aug 2013 21:22:57 +0400 Subject: [Tutor] hi In-Reply-To: References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> Message-ID: <000501ce945b$ed947f70$c8bd7e50$@orange.mu> Hi, Thanks ! What is the best numerical ODE that you have coded then? Can you solve for the following equation and tell me the error it gives: N(ODE) = (4 * (exp(1) ** (0.8 * t))) - (0.5 * x) for f(t , x) The true value should be : TF = (4/1.3)*EXP(0.8*t)-(1.4/1.3)*EXP(-0.5*t) The error after your numerical ODE should be Er = ABS((TF-N(ODE))/TF)*100 For my n-body problem, I will need to see how to implement the animation API in my code and see if it works! Regards Vick -----Original Message----- From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] Sent: Thursday, 08 August, 2013 18:14 To: Vick Cc: tutor at python.org Subject: Re: [Tutor] hi On 7 August 2013 22:43, Vick wrote: > Hi, > > It just plot one line. How to print several lines? Just do: line1, = plot([], []) line2, = plot([], []) And then line1.set_data(x1, y1) line2.set_data(x2, y2) and so on. > I've attached your example and I've corrected my own 3d code which > works now but it is not an animation. So how can I plot it using the code you gave me? Just change the data that you use for the line. > By the way, I think you know that I'm trying to plot for n-body problem? > Have you made a python code for the n-body problem? No I haven't but I have written code for computing numerical solutions of ODEs. > Mine works, but I'm just > having trouble with 3d plotting of the orbits. Once you have the lists of x, y, and z coordinates for the orbit just call: plot(x, y, z) (after creating an axes with projection='3d' and importing mplot3d). What's not working for you? > And also I have searched the > web on how to integrate conservation of energy as you suggested to me, > but I couldn't find any article that talks about 1st order ODEs for > energy, and the annotation is different from mine. I think you misunderstood. What I meant was if you implement your own routine for solving ODEs you need to check its accuracy. In a 2-body problem you would have an analytic solution in closed form. You can then check the error between your numerical solution and the analytic one. Investigating how this error scales with the step size dt enables you to confirm the order of your integrator. The reason I suggested this was that the original code you sent implemented the Runge-Kutta algorithm incorrectly and I wanted you to check your code properly before assuming it works. In the case of the N-body problem you don't have an analytic solution but, assuming Hamiltonian dynamics, energy is conserved. You can compute the energy of your system at each state and compare how constant it is over time in the output from your integrator. This gives you a proxy for not having an analytic solution that you can use to check the accuracy of your integrator: if the energy of your system is not (approximately) maintained during integration then your integration must be inaccurate. > PS: Also how do you make the plot run only once without having to do > it over and over again? I'm not sure TBH. I normally use the animation API to save video files for presentations. You may be more interested in the approach here rather than using the animation API: http://stackoverflow.com/questions/11874767/real-time-plotting-in-while-loop -with-matplotlib Oscar From sudipta.mml at gmail.com Wed Aug 7 23:37:06 2013 From: sudipta.mml at gmail.com (sudipta) Date: Wed, 7 Aug 2013 17:37:06 -0400 Subject: [Tutor] constrained least square fitting using python Message-ID: Hi All, I am facing a problem for constrained linear least square fitting. In my case the matrix equation looks like [Y]nX1=[X]nXm[P]mX1, where Y and P are vectors and X is a matrix and n, m are dimension of the matrix. Further, there is a equality constraint on P which is Sum(P(i))=0.0. How do I proceed to solve that? Which function of python is suitable for this? I saw few of discussion on scipy.optimize.fmin_slsqp() function but the implementation of this function is not very straightforward. Therefore, I need your help. I am new in SCIPY. Please help me out in this regard. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Aug 9 01:14:31 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 09 Aug 2013 00:14:31 +0100 Subject: [Tutor] Python Programming for the absolute beginner 3e Ch3 Challenge 4 In-Reply-To: References: Message-ID: On 08/08/13 18:05, John Feleppa wrote: > I am working through Michael Dawson's book, "Python Programming for the > absolute beginner 3rd edition". > > Have just completed Chapter 3, but cannot solve Challenge 4. > > Has anyone solved this yet - and be willing to share this code? If you give us a clue what it is then more of us might be able to give you a hint... Especially if you tell us what you've tried so far. Also what python version you are using... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Fri Aug 9 01:17:16 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 09 Aug 2013 00:17:16 +0100 Subject: [Tutor] constrained least square fitting using python In-Reply-To: References: Message-ID: On 07/08/13 22:37, sudipta wrote: > this? I saw few of discussion on scipy.optimize.fmin_slsqp() function > but the implementation of this function is not very straightforward. > Therefore, I need your help. I am new in SCIPY. Please help me out in This list is really aimed at newcomers to Python and is standard library. You probably will get a better response on a dedicated scipy forum/mailing list. However, we do have a few scipy users here too so you might be lucky. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From sunithanc at gmail.com Fri Aug 9 03:52:58 2013 From: sunithanc at gmail.com (SM) Date: Thu, 8 Aug 2013 21:52:58 -0400 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: References: Message-ID: >>I'm confused. Why does the OP want multiple instances of the user interface widgets? > He may not, but he is creating them! But it is possible he does want multiple duplicate windows, one monitoring each thread. (eg progress dialogs on an FTP program). Since I don't know the context I'm assuming he knows how many windows he wants! If he does want just one then he needs to reference the global object in his run() method... But that brings us back into the land of synchronisation issues and threadsafe locks etc. OP is me? Not sure what it stands for, but I am a 'she' :) Since you asked here is some context on what I am trying to do: I don't want multiple instances of the widget. I have a Gui with 3 tabs, each for a different purpose, with a unique functionality within the main window. Each tab has only one instance of this widget I am asking question about. Everything is working great with full functionality. As an added feature, I am now asked to have a "progress bar" widget which appears when an operation is going on, to tell the user that he/she has to wait. So I am running two threads - first one is the progressbar widget which has a rectangular slab spinning sideways to indicate that the operation is going on. The second thread is the one which is actually doing the operation . I have a flag that the second thread sets after the job is done, which the widget thread keeps checking in a loop and stops spinning and gets out of the loop when the flag is set. I have that working as well. I also have a textEdit window on the Gui where I redirect all the standard output using StringIO. For that I use the following code: Before the operation starts, I call the following: self.oldstdout = sys.stdout sys.stdout = StringIO() After the operation, I call the method: def setStdoutToTextEditWindowFw(self): self.textEdit_fwcmdlineoutput.setText( sys.stdout.getvalue() ) sys.stdout = self.oldstdout I need to call it from the thread after the job is done, so the text I want to output on the command line will show up on the textEdit window on the Gui. The above method is defined on Ui_MainWindow class. The widget gets created in the method setupUI which is defined under Ui_MainWindow class: self.textEdit_fwcmdlineoutput = QtGui.QTextEdit(self.tab_fw) Alan indicated that it should be called by __init__. I tried doing it and the gui crashed! (I am yet to see why and where it crashed) I am not sure I know what the problem or the solution is. Hence the email Thanks for all your time, suggestions and help. Thanks, -SM On Thu, Aug 8, 2013 at 6:30 PM, Alan Gauld wrote: > On 08/08/13 23:02, eryksun wrote: > > You are creating a new instance inside your run() method but >>> the call in main() only sets it for the instance created in >>> main. It won't set it up for any other instance. You need >>> to call it every time you create a new object. Unless.... >>> >>> If you put it in init() it will be called every time you >>> create a new instance. Much safer. >>> >> >> I'm confused. Why does the OP want multiple instances of the user >> interface widgets? >> > > > He may not, but he is creating them! But it is possible he does want > multiple duplicate windows, one monitoring each thread. (eg progress > dialogs on an FTP program). Since I don't know the context I'm > assuming he knows how many windows he wants! > > If he does want just one then he needs to reference the global object in > his run() method... But that brings us back into the land of > synchronisation issues and threadsafe locks etc. > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Fri Aug 9 04:15:41 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 8 Aug 2013 22:15:41 -0400 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: References: Message-ID: On Thu, Aug 8, 2013 at 9:52 PM, SM wrote: > > OP is me? Not sure what it stands for, but I am a 'she' :) FYI (For you information) OP means Original Poster. Although people do refer to names in replies here, the term OP is used quite often. As for the implication of he not she, I guess we are stuck with guessing in English usage. (They doesn't cut it with Language mavens!) Good to have "shes' to get more diverse user base. Best thing about user groups is that different backgrounds make for better discussions and interesting ways of solving problems -- Joel Goldstick http://joelgoldstick.com From marc.tompkins at gmail.com Fri Aug 9 05:01:11 2013 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Thu, 8 Aug 2013 20:01:11 -0700 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: References: Message-ID: On Thu, Aug 8, 2013 at 7:15 PM, Joel Goldstick wrote: > On Thu, Aug 8, 2013 at 9:52 PM, SM wrote: > > > > > OP is me? Not sure what it stands for, but I am a 'she' :) > > FYI (For you information) OP means Original Poster. Although people > do refer to names in replies here, the term OP is used quite often. > As for the implication of he not she, I guess we are stuck with > guessing in English usage. (They doesn't cut it with Language mavens!) > Good to have "shes' to get more diverse user base. Best thing about > user groups is that different backgrounds make for better discussions > and interesting ways of solving problems > > You know the old joke: on the Internet, nobody knows you're a dog... -------------- next part -------------- An HTML attachment was scrubbed... URL: From eryksun at gmail.com Fri Aug 9 08:14:06 2013 From: eryksun at gmail.com (eryksun) Date: Fri, 9 Aug 2013 02:14:06 -0400 Subject: [Tutor] constrained least square fitting using python In-Reply-To: References: Message-ID: On Wed, Aug 7, 2013 at 5:37 PM, sudipta wrote: > > I am facing a problem for constrained linear least square fitting. In my > case the matrix equation looks like [Y]nX1=[X]nXm[P]mX1, where Y and P are > vectors and X is a matrix and n, m are dimension of the matrix. Further, > there is a equality constraint on P which is Sum(P(i))=0.0. How do I proceed > to solve that? Which function of python is suitable for this? I saw few of > discussion on scipy.optimize.fmin_slsqp() function but the implementation of > this function is not very straightforward. Therefore, I need your help. I am > new in SCIPY. Please help me out in this regard. You can use scipy.optimize.minimize() with method='SLSQP' (it uses fmin_slsqp). You'll need an objective function that returns the sum of the squared residuals; the algorithm minimizes this. You'll also need an 'eq' constraint function; the algorithm tries to make this zero. Here's a contrived example. Hopefully if there's a problem with this example, someone else on the list (Oscar?) can set it straight. I don't use scipy.optimize very much. I know a bit more about scipy.signal. This example uses a 100x3 matrix X that's set up to compute a polynomial fit. Normally I'd use the pseudoinverse of the matrix, but in this case there's an added constraint. Also, strictly speaking the matrix isn't required when using minimize(); you could just evaluate the polynomial to compute the residuals at each step, but this way matches your problem description. import numpy as np from scipy import optimize import matplotlib.pyplot as plt np.random.seed(42) def peval(p, X): return np.dot(X, p) def objective(p, y, X): return np.sum((y - peval(p, X)) ** 2) # y = -2*x**2 + 2.5*x - 0.5 t = np.reshape(np.arange(100) * 0.1, (100, 1)) X = np.hstack([t**2, t**1, t**0]) ptrue = np.array([-2.0, 2.5, -0.5]) ytrue = peval(ptrue, X) # add measurement noise ymeas = ytrue + 5 * np.random.randn(len(ytrue)) p0 = np.zeros(3) cons = { 'type': 'eq', 'fun': np.sum, } res = optimize.minimize( objective, p0, args=(ymeas, X), method='SLSQP', constraints=cons) pcons = res.x yfit = peval(pcons, X) ss_res = np.sum((ymeas - yfit) ** 2) ss_tot = np.var(ymeas) * len(ymeas) rsq = 1 - ss_res / ss_tot plt.plot(t, ymeas, 'o', t, ytrue, t, yfit) plt.title('Least Squares Fit') plt.legend(['Noisy Data', 'True', 'Fit']) plt.show() Graph: http://i.imgur.com/BHLPbCa.png Estimated parameters and R**2 value: >>> pcons array([-1.94471575, 1.95757699, -0.01286124]) >>> np.sum(pcons) -2.0816681711721685e-17 >>> rsq 0.99247909721611327 Pseudoinverse result for comparison: >>> plsq = np.dot(np.linalg.pinv(X), ymeas) >>> plsq array([-1.96260467, 2.19944918, -0.75938176]) >>> np.sum(plsq) -0.52253724312209116 >>> ss_res = np.sum((ymeas - np.dot(X, plsq)) ** 2) >>> 1 - ss_res / ss_tot 0.99250546279261975 From alan.gauld at btinternet.com Fri Aug 9 10:01:04 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 09 Aug 2013 09:01:04 +0100 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: References: Message-ID: On 09/08/13 02:52, SM wrote: > OP is me? Not sure what it stands for, but I am a 'she' :) Oops. my bad. Stereotyping is a bad habit. > I don't want multiple instances of the widget. I have a Gui with 3 tabs, > each for a different purpose, with a unique functionality within the > main window. Each tab has only one instance of this widget So you do want multiple instances of the widget. You want a separate instance in each of the 3 tabs. But you only want one main UI. So you should probably create a separate class for the widgets that are duplicated and then insert the instances of that class into your main UI object. The new widget class should have the methods you want to access from your threads. > an added feature, I am now asked to have a "progress bar" widget which > appears when an operation is going on, to tell the user that he/she has > to wait. Is this progress bar duplicated in each tab or is that another thing entirely? If it is yet another widget set you may want to make that a separate class too. > So I am running two threads - first one is the progressbar > widget which has a rectangular slab spinning sideways to indicate that > the operation is going on. The second thread is the one which is > actually doing the operation . I have a flag that the second thread sets > after the job is done, which the widget thread keeps checking in a loop > and stops spinning and gets out of the loop when the flag is set. I > have that working as well. I wouldn't normal use a thread for the progress bar I'd just use a timer event in my main GUI to check the flag periodically. Threads will work but they are more effort to setup and manage. > I also have a textEdit window on the Gui where I redirect all the > standard output using StringIO. Just one text window in the main GUI? not one per tab or per thread? > The above method is defined on Ui_MainWindow class. The widget gets > created in the method setupUI which is defined under Ui_MainWindow class: > self.textEdit_fwcmdlineoutput = QtGui.QTextEdit(self.tab_fw) > > Alan indicated that it should be called by __init__. Only really needed if you want to create a new widget per thread, it sounds like you only want this in the main UI so main() is OK (although putting it in init shouldn't be a problem!) > I tried doing it and the gui crashed! I can only think that maybe you are overwriting some global values that were set in the call in the first widget. The first objective should be to clearly work out how many windows you are creating and where. It sounds like you have a main window class, 3 tab classes and maybe progress indicator and text window classes too. Then build up the GUI structure by adding instances of the tabs to the main window instance and adding the progress indicator instance(s) when you start the processing threads. Try to map the visible objects in your GUI to objects(classes) in your code. Having said that I've never used Qt so there may be ready made classes in the toolkit (tabs etc) that you can use out of the box. In fact progress indicator dialogs are quite a common toolkit feature so that may well exist already too., -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Fri Aug 9 10:11:07 2013 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Fri, 9 Aug 2013 09:11:07 +0100 (BST) Subject: [Tutor] Python Programming for the absolute beginner 3e, Ch3 Challenge 4 In-Reply-To: References: Message-ID: <1376035867.52727.YahooMailNeo@web186005.mail.ir2.yahoo.com> # Write the psudocode for a program where the player and the computer ># trade places in the number guessing game. ?That is, the player picks a? ># random number between 1 and 100 that the computer has to guess. ># Before you start, think about how you guess. ?If all goes well, try ># coding the game. > > >It builds on the previous challenge, where the user guesses the number - I coded that OK, as below.OK, So which bits of this challenge puzzle you? Can you write down the process you used when guessing the numbers? Can you turn that description into pseudo-Python code? Hint: You need to get the computer ?to generate numbers in an ever narrowing range,? Maybe the midpoint number? (You could also google "binary chop" to get? more ideas) Then you need to tell the computer which direction it should go next. Have a stab at it, get back to us with what you've come up with. HTH Alan G. -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Fri Aug 9 11:26:51 2013 From: __peter__ at web.de (Peter Otten) Date: Fri, 09 Aug 2013 11:26:51 +0200 Subject: [Tutor] Question in regards to loops and matplotlib References: Message-ID: Zachary Rizer wrote: > def make_mass_cuts(table): > """ > make mass cuts using logMass row and creates four-tuple > each index is divided into its own dual tuple, x and y UVJ colors > array returned = [(m1_x, m1_y), (m2_x, m2_y), (m3_x, m3_y), (m4_x, > m4_y)] > """ > m1_x = [] > m1_y = [] > m2_x = [] > m2_y = [] > m3_x = [] > m3_y = [] > m4_x = [] > m4_y = [] > for row in table: > if row['ms_lmass'] >= 9.7 and row['ms_lmass'] < 10: > m1_x.append(row['rf_VminJ']) > m1_y.append(row['rf_UminV']) > elif row['ms_lmass'] >= 10 and row['ms_lmass'] < 10.5: > m2_x.append(row['rf_VminJ']) > m2_y.append(row['rf_UminV']) > elif row['ms_lmass'] >= 10.5 and row['ms_lmass'] < 10.8: > m3_x.append(row['rf_VminJ']) > m3_y.append(row['rf_UminV']) > elif row['ms_lmass'] >= 10.8: > m4_x.append(row['rf_VminJ']) > m4_y.append(row['rf_UminV']) > return [(m1_x, m1_y), (m2_x, m2_y), (m3_x, m3_y), (m4_x, m4_y)] You have redundant tests there: > if row['ms_lmass'] >= 9.7 and row['ms_lmass'] < 10: ... > elif row['ms_lmass'] >= 10 and row['ms_lmass'] < 10.5: At this point you already know that the mass is >= 10, no need to repeat the check. Which gives: def _make_mass_cuts(table): bins = [([], []) for i in range(4)] for row in table: mass = row["ms_lmass"] if mass < 9.7: continue elif mass < 10: bin_index = 0 elif mass < 10.5: bin_index = 1 elif mass < 10.8: bin_index = 2 else: bin_index = 3 binx, biny = bins[bin_index] binx.append(row['rf_VminJ']) biny.append(row['rf_UminV']) return bins For so few cuts it's probably not worthwhile, but the bisect module allows to generalise this: import bisect def make_mass_cuts(table): cuts = [9.7, 10.0, 10.5, 10.8] bins = [([], []) for cut in cuts] for row in table: index = bisect.bisect_right(cuts, row['ms_lmass']) if index: x, y = bins[index-1] x.append(row['rf_VminJ']) y.append(row['rf_UminV']) return bins If you want to play with the values or the number of bins now all you have to change is the cuts list. From sunithanc at gmail.com Fri Aug 9 17:50:19 2013 From: sunithanc at gmail.com (SM) Date: Fri, 9 Aug 2013 11:50:19 -0400 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: References: Message-ID: On Fri, Aug 9, 2013 at 4:01 AM, Alan Gauld wrote: > On 09/08/13 02:52, SM wrote: > > OP is me? Not sure what it stands for, but I am a 'she' :) >> > > Oops. my bad. Stereotyping is a bad habit. [SM]: Not your fault. My bad too. Dog's face tells little about the owner... > > > I don't want multiple instances of the widget. I have a Gui with 3 tabs, >> each for a different purpose, with a unique functionality within the >> main window. Each tab has only one instance of this widget >> > > So you do want multiple instances of the widget. You want a separate > instance in each of the 3 tabs. But you only want one main UI. > No, no! Each tab is totally distinct from the other. I shouldn't have mentioned the tab. Each tab has its own widget (ex: self.tab_fw = QtGui.QWidget(), self.tab_ann = QtGui.QWidget(), etc), its own textEdit window and its own progress bar widget. All the tabs are defined within this single class - they are not instances of the class, as the tabs are distinct from each other. All of that is working really well. > So you should probably create a separate class for the widgets that are > duplicated and then insert the instances of that class into your main UI > object. The new widget class should have the methods you want to access > from your threads. > > > an added feature, I am now asked to have a "progress bar" widget which >> appears when an operation is going on, to tell the user that he/she has >> to wait. >> > > Is this progress bar duplicated in each tab or is that another thing > entirely? If it is yet another widget set you may want to make that a > separate class too. [SM]: Yes. I shouldn't say duplicated, but each tab has a different progress bar of its own. In the example, I was quoting the code for just on tab. The others will be implemented similarly. > > > So I am running two threads - first one is the progressbar >> widget which has a rectangular slab spinning sideways to indicate that >> the operation is going on. The second thread is the one which is >> actually doing the operation . I have a flag that the second thread sets >> after the job is done, which the widget thread keeps checking in a loop >> and stops spinning and gets out of the loop when the flag is set. I >> have that working as well. >> > > I wouldn't normal use a thread for the progress bar I'd just use a timer > event in my main GUI to check the flag periodically. Threads will work but > they are more effort to setup and manage. [SM]: I did start out with a timer. But due to my lack of experience with usage of timer in Python, the code I wrote was running sequentially - that is, the two jobs I am trying to synchronize - the progress bar and the job to be done - were running one after the other, beating the whole purpose. So I had to go for the thread-based implementation. If you or anyone else reading this have a sample code for a timer-based implementation, I could try that. I did use an example I found online, but that didn't help. > > > I also have a textEdit window on the Gui where I redirect all the >> standard output using StringIO. >> > > Just one text window in the main GUI? not one per tab or per thread? [SM]. One text window oer tab - not per thread. I am using threads only for running the progress bar. > > The above method is defined on Ui_MainWindow class. The widget gets >> created in the method setupUI which is defined under Ui_MainWindow class: >> self.textEdit_fwcmdlineoutput = QtGui.QTextEdit(self.tab_fw) >> >> Alan indicated that it should be called by __init__. >> > > Only really needed if you want to create a new widget per thread, > it sounds like you only want this in the main UI so main() is OK (although > putting it in init shouldn't be a problem!) > > > I tried doing it and the gui crashed! >> > > I can only think that maybe you are overwriting some global values that > were set in the call in the first widget. The first objective should be to > clearly work out how many windows you are creating and where. It sounds > like you have a main window class, 3 tab classes and maybe progress > indicator and text window classes too. > > Then build up the GUI structure by adding instances of the tabs > to the main window instance and adding the progress indicator instance(s) > when you start the processing threads. Try to map the visible objects in > your GUI to objects(classes) in your code. > > Having said that I've never used Qt so there may be ready made > classes in the toolkit (tabs etc) that you can use out of the box. > In fact progress indicator dialogs are quite a common toolkit > feature so that may well exist already too., [SM] I think my answers on the top will clarify these. Thanks for all your time! Really appreciate it. -SM > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From oscar.j.benjamin at gmail.com Fri Aug 9 20:05:01 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Fri, 9 Aug 2013 19:05:01 +0100 Subject: [Tutor] hi In-Reply-To: <000501ce945b$ed947f70$c8bd7e50$@orange.mu> References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> Message-ID: Can you respond inline and trim the part you're not quoting instead of top-posting please? On 8 August 2013 18:22, Vick wrote: > Hi, > > Thanks ! > > What is the best numerical ODE that you have coded then? What do you mean by best? There are different ODE solvers for different problems. > Can you solve for > the following equation and tell me the error it gives: > > N(ODE) = (4 * (exp(1) ** (0.8 * t))) - (0.5 * x) for f(t , x) > > The true value should be : TF = (4/1.3)*EXP(0.8*t)-(1.4/1.3)*EXP(-0.5*t) You haven't given the initial condition but I guess it should be that x=2 at t=0 from the solution. > The error after your numerical ODE should be Er = ABS((TF-N(ODE))/TF)*100 Why are you multiplying by 100? That doesn't make any sense. I don't see what's so special about this ODE that you wouldn't just use scipy's odeint: #!/usr/bin/env python # solve.py from math import exp from scipy.integrate import odeint # System derivative def f(x, t): return 4 * exp(0.8 * t) - 0.5 * x # Analytic solution def analytic(t): return (40/13.)*exp(0.8*t) - (14/13.)*exp(-0.5*t) # Initial condition x0 = [2] # Time through which to integrate (from t=0 to t=T) T = 4 xtrue = analytic(T) for tol in 1e-8, 1e-9, 1e-10, 1e-11, 1e-12, 1e-13, 1e-14, 1e-15: xest = odeint(f, x0, [0, T], rtol=tol, atol=tol)[-1] relerror = abs((xest - xtrue)/xtrue) print('Tolerance: %s Error: %.3e' % (tol, relerror)) Output: $ ./solve.py Tolerance: 1e-08 Error: 2.066e-09 Tolerance: 1e-09 Error: 1.623e-10 Tolerance: 1e-10 Error: 2.707e-11 Tolerance: 1e-11 Error: 1.818e-12 Tolerance: 1e-12 Error: 7.281e-13 Tolerance: 1e-13 Error: 7.620e-14 lsoda-- at start of problem, too much accuracy requested for precision of machine.. see tolsf (=r1) In above message, R1 = 0.2960594732334E+01 Illegal input detected (internal error). Run with full_output = 1 to get quantitative information. Tolerance: 1e-14 Error: 9.735e-01 lsoda-- at start of problem, too much accuracy requested for precision of machine.. see tolsf (=r1) In above message, R1 = 0.2960594732334E+02 Illegal input detected (internal error). Run with full_output = 1 to get quantitative information. Tolerance: 1e-15 Error: 9.735e-01 So you can get a relative error of 1e-14 with this. Is that good enough? Oscar From oscar.j.benjamin at gmail.com Fri Aug 9 20:40:52 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Fri, 9 Aug 2013 19:40:52 +0100 Subject: [Tutor] constrained least square fitting using python In-Reply-To: References: Message-ID: On 7 August 2013 22:37, sudipta wrote: > Hi All, > > I am facing a problem for constrained linear least square fitting. In my > case the matrix equation looks like [Y]nX1=[X]nXm[P]mX1, where Y and P are > vectors and X is a matrix and n, m are dimension of the matrix. It took me a moment to understand that equation. I would have written it as y = X p where I use capitals to distinguish matrices and vectors. Okay so to change the terminology slightly if E[y] = X p is the estimate of y given X and p. We want to choose p_lsq to minimise res = sum((E[y] - y)^2) = sum((X p - y)^2) The optimal solution is given by the exact solution of the linear system X' X p_lsq = X' y where X' means the transpose of X. > Further, > there is a equality constraint on P which is Sum(P(i))=0.0. How do I proceed > to solve that? The least-squares choice p_lsqcon that satisfies the linear constraint A p = a is given by p_lsqcon = p_lsq - Z A' [A Z A']^(-1) (A p - a) where Z is the inverse of X' X. In general A is a matrix but in your case it is a 1xm vector of all 1s and a is just 0. The expressions above may look complicated but when you actually work it out they're not so bad. > Which function of python is suitable for this? I saw few of > discussion on scipy.optimize.fmin_slsqp() function but the implementation of > this function is not very straightforward. Therefore, I need your help. I am > new in SCIPY. Please help me out in this regard. You should look at how to do matrix multiplication and how to solve a linear system. Here's a quick ipython session that illustrates how to do this: In [1]: import numpy as np In [2]: M = np.array([[1, 2], [3, 4]]) In [3]: M Out[3]: array([[1, 2], [3, 4]]) In [4]: x = np.array([[1], [2]]) In [5]: x Out[5]: array([[1], [2]]) In [6]: np.dot(M, x) # matrix multiplication Out[6]: array([[ 5], [11]]) In [7]: b = np.dot(M, x) In [8]: b Out[8]: array([[ 5], [11]]) In [9]: np.linalg.solve(M, b) Out[9]: array([[ 1.], [ 2.]]) I don't know whether using this analytic result will be more/less accurate/efficient than the methods that Eryksun suggested but this would be my first attempt. Oscar From eryksun at gmail.com Sat Aug 10 01:00:23 2013 From: eryksun at gmail.com (eryksun) Date: Fri, 9 Aug 2013 19:00:23 -0400 Subject: [Tutor] constrained least square fitting using python In-Reply-To: References: Message-ID: On Fri, Aug 9, 2013 at 2:40 PM, Oscar Benjamin wrote: > > E[y] = X p > > is the estimate of y given X and p. We want to choose p_lsq to minimise > > res = sum((E[y] - y)^2) = sum((X p - y)^2) > > The optimal solution is given by the exact solution of the linear system > > X' X p_lsq = X' y > > where X' means the transpose of X. The pseudoinverse I mentioned is (X' X)^-1 X'. Oscar's way using linalg.solve on the above equation will behave better numerically. > The least-squares choice p_lsqcon that satisfies the linear constraint > > A p = a > > is given by > > p_lsqcon = p_lsq - Z A' [A Z A']^(-1) (A p - a) > > where Z is the inverse of X' X. In general A is a matrix but in your > case it is a 1xm vector of all 1s and a is just 0. The expressions > above may look complicated but when you actually work it out they're > not so bad. The result from the analytic solution is very close to what minimize() got in my example: >>> Q = np.dot(X.T, X) >>> plsq = np.linalg.solve(Q, np.dot(X.T, ymeas)) >>> plsq array([-1.96260467, 2.19944918, -0.75938176]) >>> Z = np.linalg.inv(Q) >>> A = np.ones((1, 3)) I'm tempted to switch to np.matrix here, which supports infix matrix products and exponents. But I'll just break this mess up into temp variables: >>> t1 = np.dot(Z, A.T) >>> t2 = np.linalg.inv(np.dot(np.dot(A, Z), A.T)) >>> t3 = np.dot(A, plsq) >>> plsqcon = plsq - np.dot(np.dot(t1, t2), t3) >>> plsqcon array([-1.9447156 , 1.95757573, -0.01286013]) My solution from minimize(): >>> pcons array([-1.94471575, 1.95757699, -0.01286124]) From oscar.j.benjamin at gmail.com Sat Aug 10 03:03:25 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sat, 10 Aug 2013 02:03:25 +0100 Subject: [Tutor] hi In-Reply-To: <000301ce9542$2c955f50$85c01df0$@orange.mu> References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> Message-ID: On 9 August 2013 21:51, Vick wrote: > Oscar wrote: >> Can you respond inline and trim the part you're not quoting instead of >> top-posting please? > > Ok. But may I ask why please? You may. It makes for better communication. Isn't it easier to read this as a conversation with the context I'm replying to above my reply? Also you please don't trim the who wrote what header line (so that I don't have to add in the "Oscar wrote" part above. >> What do you mean by best? There are different ODE solvers for different >> problems. > > I meant best accuracy in digits. But for what problem? Different ODE solvers are better at solving different types of ODEs, or even at finding different solutions of the same ODEs. Do you mean the best solver for non-smooth problems, for stiff problems, for getting hyper-accurate solutions to super-smooth problems, etc.? [snip] > I just didn't realize that scipy had an ODE solver! Always check this kind of thing before reinventing the wheel. Scipy's odeint is a very good general purpose solver. > Ok, but what is the best > one you have? Of course in terms of digits accuracy? Even the simplest solvers can usually be made as accurate as desired by reducing the step-size and increasing the precision. When people talk about one solver as being more accurate than another they usually mean that it is better accuracy for a given precision (and a particular ODE, initial condition etc.) or that it achieves a better accuracy given the same amount of computation time. The question you've asked is ill-posed as we can easily construct a solver to any arbitrary degree of accuracy (for well-behaved ODEs). Oscar From oscar.j.benjamin at gmail.com Sat Aug 10 03:12:33 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sat, 10 Aug 2013 02:12:33 +0100 Subject: [Tutor] constrained least square fitting using python In-Reply-To: References: Message-ID: On 10 August 2013 00:00, eryksun wrote: > On Fri, Aug 9, 2013 at 2:40 PM, Oscar Benjamin > > The result from the analytic solution is very close to what minimize() > got in my example: That's reassuring. Which of the two solutions is more accurate in terms of satisfying the constraint and minimising the objective function? Do they take similar times to run? [snip] > I'm tempted to switch to np.matrix here, which supports infix matrix > products and exponents. But I'll just break this mess up into temp > variables: This probably is a good use for np.matrix (I guess you generally dislike it for the same reasons I do: explicit is better than implicit etc.). Oscar From eryksun at gmail.com Sat Aug 10 09:18:54 2013 From: eryksun at gmail.com (eryksun) Date: Sat, 10 Aug 2013 03:18:54 -0400 Subject: [Tutor] constrained least square fitting using python In-Reply-To: References: Message-ID: On Fri, Aug 9, 2013 at 9:12 PM, Oscar Benjamin wrote: > > Which of the two solutions is more accurate in terms of satisfying the > constraint and minimising the objective function? Do they take similar > times to run? In my example, minimize() required 28 function evaluations, so your solution is more efficient. As far as minimizing the objective and satisfying the constraint, the results are very close: >>> objective(pcons, ymeas, X) # minimize 2036.6364327061785 >>> np.sum(pcons) -2.0816681711721685e-17 >>> objective(plsqcon, ymeas, X) # analytic 2036.6364327060171 >>> np.sum(plsqcon) 0.0 I think SLSQP is using something like gradient descent on successive quadratic approximations of the objective. But it's not something I know a lot about. Here's the paper and links to the FORTRAN source code: Dieter Kraft, "Algorithm 733: TOMP?Fortran modules for optimal control calculations," ACM Transactions on Mathematical Software, vol. 20, no. 3, pp. 262-281 (1994) http://doi.acm.org/10.1145/192115.192124 http://www.netlib.org/toms/733 https://github.com/scipy/scipy/blob/master/scipy/optimize/slsqp/slsqp_optmz.f > This probably is a good use for np.matrix (I guess you generally > dislike it for the same reasons I do: explicit is better than implicit > etc.). I'm not against np.matrix in principle. I just didn't want to muddy the water. From phil_lor at bigpond.com Sat Aug 10 08:45:09 2013 From: phil_lor at bigpond.com (Phil) Date: Sat, 10 Aug 2013 16:45:09 +1000 Subject: [Tutor] Map function Message-ID: <5205E175.8000808@bigpond.com> The Arduino has a map function named "map" which looks like this: map(value, 0, 1023, 0, 100) The function, in this case, takes an integer value between 0 and 1023 and returns a number between 0 and 100. Is there a Python equivalent? -- Regards, Phil From chris at chrisdown.name Sat Aug 10 11:39:18 2013 From: chris at chrisdown.name (Chris Down) Date: Sat, 10 Aug 2013 11:39:18 +0200 Subject: [Tutor] Map function In-Reply-To: <5205E175.8000808@bigpond.com> References: <5205E175.8000808@bigpond.com> Message-ID: <20130810093917.GA2310@gopher> Hi Phil, On 2013-08-10 16:45, Phil wrote: > The Arduino has a map function named "map" which looks like this: > > map(value, 0, 1023, 0, 100) > > The function, in this case, takes an integer value between 0 and > 1023 and returns a number between 0 and 100. Is there a Python > equivalent? The Arduino documentation[0] says that `map' is equivalent to: long map(long x, long in_min, long in_max, long out_min, long out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } With that in mind, you can almost copy and paste the exact same code in Python (albeit with floor division, as in Python 3, integer division can yield floats). Note that `//' is not strictly equivalent to C's integer division, though.[1] >>> def arduino_map(x, in_min, in_max, out_min, out_max): ... return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min ... >>> arduino_map(50, 0, 1023, 0, 100) 4 0: http://arduino.cc/en/Reference/map 1: http://stackoverflow.com/a/5365702/945780 -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From phil_lor at bigpond.com Sat Aug 10 12:18:30 2013 From: phil_lor at bigpond.com (Phil) Date: Sat, 10 Aug 2013 20:18:30 +1000 Subject: [Tutor] Map function In-Reply-To: <20130810093917.GA2310@gopher> References: <5205E175.8000808@bigpond.com> <20130810093917.GA2310@gopher> Message-ID: <52061376.7010506@bigpond.com> On 10/08/13 19:39, Chris Down wrote: > Hi Phil, > > On 2013-08-10 16:45, Phil wrote: >> The Arduino has a map function named "map" which looks like this: >> >> map(value, 0, 1023, 0, 100) >> >> The function, in this case, takes an integer value between 0 and >> 1023 and returns a number between 0 and 100. Is there a Python >> equivalent? > > The Arduino documentation[0] says that `map' is equivalent to: > > long map(long x, long in_min, long in_max, long out_min, long out_max) { > return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; > } > > With that in mind, you can almost copy and paste the exact same code in Python > (albeit with floor division, as in Python 3, integer division can yield > floats). Note that `//' is not strictly equivalent to C's integer division, > though.[1] > > >>> def arduino_map(x, in_min, in_max, out_min, out_max): > ... return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min > ... > >>> arduino_map(50, 0, 1023, 0, 100) > 4 > > 0: http://arduino.cc/en/Reference/map > 1: http://stackoverflow.com/a/5365702/945780 > Thanks Chris, I had come up with something similar, but it was more complex without the // operator, which led me to thinking that perhaps Python had a similar map function built in. Anyway, thanks again my curiosity has been satisfied. -- Regards, Phil From alan.gauld at btinternet.com Sat Aug 10 13:04:57 2013 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Sat, 10 Aug 2013 12:04:57 +0100 (BST) Subject: [Tutor] Python Programming for the absolute beginner 3e, Ch3 Challenge 4 In-Reply-To: References: <1376035867.52727.YahooMailNeo@web186005.mail.ir2.yahoo.com> Message-ID: <1376132697.65005.YahooMailNeo@web186002.mail.ir2.yahoo.com> Also don't know how to do a binary chop, but the book hasn't covered anything? >like that - it has just taught if, else and while,? > >A binary chop is an algorithm. All you need is if/else and while. The algorithm looks a bit like this: while not found ? ? ? ?determine the middle value between min ?and max ? ? ? ?if value > target set max to value ? ? ? ?if value < target set min to value ? ? ? ?else found = True the_number = int(input("Type in a number - I swear down I no look!^^ "))You don't need to store the number since the human user can just remember it print("But I asked the computer, and it reckons your number is...") > >guess = (random.randint(1, 100))You don't want to use random guesses - that could take a very long time! while guess != the_number: > >? ? hilo = input("\nWas I too high or too low? ?Write 'h' or 'l'.\n") >? ? if hilo == "h": >? ? ? ? print("\nToo high, well, in that case what about...\n") >? ? ? ? lower_guess = (random.randint(lowest, temp))This is almost right except you will be faster using the mid value rather than a random one. And what happened in the shell: > > > >Hello & Welcome to 'Modified Guess My Number'! > > >Think of a number between 1 and 100 & the computer has to guess it. >Type in a number - I swear down I no look!^^ 30 >OK you wrote 30 but I no show the computer. > > >But I asked the computer, and it reckons your number is... >57 > > >Was I too high or too low? ?Write 'h' or 'l'. >h > > >Too high, well, in that case what about... > > >10 > > >Was I too high or too low? ?Write 'h' or 'l'. >l > > >Too low, well, in that case what about... > > >53 > > >Was I too high or too low? ?Write 'h' or 'l'. >h > > >Too high, well, in that case what about... > > >51 > > >Was I too high or too low? ?Write 'h' or 'l'. >hAs you can see its working, but just taking a lot of guesses.? You will speed it up dramatically if you calculate the mid? point rather than use random. HTH Alan G. -------------- next part -------------- An HTML attachment was scrubbed... URL: From oscar.j.benjamin at gmail.com Sat Aug 10 17:10:09 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sat, 10 Aug 2013 16:10:09 +0100 Subject: [Tutor] hi In-Reply-To: <000001ce9590$e1b667a0$a52336e0$@orange.mu> References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> <000001ce9590$e1b667a0$a52336e0$@orange.mu> Message-ID: On 10 August 2013 07:14, Vick wrote: >> From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] > >> But for what problem? Different ODE solvers are better at solving > different >> types of ODEs, or even at finding different solutions of the same ODEs. Do >> you mean the best solver for non-smooth problems, for stiff problems, for >> getting hyper-accurate solutions to super-smooth problems, etc.? >> Even the simplest solvers can usually be made as accurate as desired by >> reducing the step-size and increasing the precision. When people talk > about >> one solver as being more accurate than another they usually mean that it > is >> better accuracy for a given precision (and a particular ODE, initial > condition >> etc.) or that it achieves a better accuracy given the same amount of >> computation time. The question you've asked is ill-posed as we can easily >> construct a solver to any arbitrary degree of accuracy (for well-behaved >> ODEs). > > Yes I understand, but as you hinted above, any solver can reach any > desired accuracy just by increasing their step-size. However by doing so, a > low-order solver will surely tax your computer memory and it will only reach > a solution after a long time. However a high-order solver will find a > solution with more accuracy in less time and by utilizing less memory. Why would it use more memory? You don't need to store every intermediate step in your computation (see the sample code below). How long a time it takes depends on many different things: the language, the precision, how good a programmer you are. Much of the "this integrator is better than that one" arguments are implicitly tied to certain presumptions about how your code gets implemented. > I am > particularly referring to solver for 1st order ODEs for general ODEs, This is the type that I normally work with. > quadratic problems What do you mean by that? > and linear constant coefficient differential > equations. Can you not just solve these analytically? > (obviously non-stiff equations). Higher-order doesn't always mean higher accuracy. The order really refers to the order of the local truncation error. Whether the integrator achieves the stated global truncation error depends on certain assumptions about how the local error propagates through to the global. This is why even very high-order methods often fail for stiff equations if the error propagation is unstable so that small errors increase exponentially. Similarly in chaotic systems where you try to follow a trajectory with positive Lyapunov exponent small errors will increase exponentially no matter what integrator you use so that before long your solution is no better than a guess. If you're not bothered about stiff problems and are only interested in ultra-high accuracy for well-behaved systems then consider using a very high-order Adams-Moulton or Adams-Bashforth integrator with a high-precision numeric type. You can compute the coefficients for AM or AB integrators of arbitrary order using the fractions module (see Wikipedia for the formulas). These are more complicated to implement as you need a secondary algorithm to bootstrap the process. > I'm guessing you have solvers for stiff problems as well? I've toyed with these but a proper application hasn't yet come up for me so I haven't needed more than what scipy has. (scipy has integrators specifically for stiff problems). > Do you have a symplectic integrator? No. Here is an example that shows we can achieve the precision of scipy's integrator (for the system you posted) easily using a standard 4th order Runge-Kutta: #!/usr/bin/env python from math import exp from pylab import loglog, plot, show, legend, xlabel, ylabel # System specification def f(x, t): return 4 * exp(0.8 * t) - 0.5 * x def analytic(t): return (40/13.)*exp(0.8*t) - (14/13.)*exp(-0.5*t) # This is for the analytic solution to be true x0 = 2 def euler(f, x, t, dt, carry=0): return x + dt * f(x, t), carry def euler_comp(f, x, t, dt, carry=0): dx = dt * f(x, t) newx = x + (dx + carry) carry = newx - (x + dx) return newx, carry def rk4(f, x, t, dt, carry=0): k1 = dt * f(x, t) k2 = dt * f(x + k1/2, t + dt/2) k3 = dt * f(x + k2/2, t + dt/2) k4 = dt * f(x + k3, t + dt) dx = (k1 + 2*k2 + 2*k3 + k4) / 6 return x + dx, carry def solveto(stepper, f, x1, t1, t2, dtmax, carry=0): t = t1 x = x1 while t < t2: newt = min(t + dtmax, t2) dt = newt - t x, carry = stepper(f, x, t, dt, carry) t = newt return x, carry def solve(stepper, f, x0, ts, dtmax, carry=0): xs = [x0] xi = x0 for t1, t2 in zip(ts[:-1], ts[1:]): xi, carry = solveto(stepper, f, xi, t1, t2, dtmax, carry) xs.append(xi) return xs def solve_and_plot(): # Use powers of 2 for accurate binary computation T = 4 dtout = 2 ** -5 dtmax = 2 ** -8 ts = [n * dtout for n in range(int(T // dtout) + 1)] xeuler = solve(euler, f, x0, ts, dtmax) xtrue = [analytic(t) for t in ts] plot(ts, xeuler, label=r'$x_{est}$') plot(ts, xtrue, label=r'$x_{true}$') show() def check_errors(): methods = [ (euler , 'euler' , 'r-+'), (rk4 , 'rk4' , 'b-+'), ] T = 4 xtrue = analytic(T) dts = [2**-n for n in range(2, 18)] for stepper, name, fmt in methods: errors = [] for dtmax in dts: xest, carry = solveto(stepper, f, x0, 0, T, dtmax) error = abs((xest - xtrue) / xtrue) errors.append(error) loglog(dts, errors, fmt, label=name) legend() xlabel(r'$\Delta t$') ylabel(r'$E_{rel}$', rotation=0) show() check_errors() You'll see that the error gets smaller as we reduce the step-size until about dt=1e-3 at which point the error is about 1e-14 (as was the case for scipy's odeint). We can't go smaller with the error because the truncation error is already smaller than the rounding error. Using compensated summation in rk4 wouldn't help because the real problem is the rounding error in f which comes from the fact that 0.8 cannot be exactly represented in binary floating point and the exp() function needs to round its output. If we can't compute the derivative exactly then we can't do anything about the rounding error. We can do better by using the higher precision Decimal type from Python's decimal module. The output from the script below is: $ py -3.3 ./oded.py dt: 1.000000e-02 error: 1.338e-11 dt: 1.000000e-03 error: 1.337e-15 dt: 1.000000e-04 error: 1.337e-19 dt: 1.000000e-05 error: 1.339e-23 We can probably get the error down to say 1e-27 by using a step-size of 1e-6. However it would take an hour or two to run that on my computer (there's probably not much scope for optimisation since profiling identifies the exp() function as the culprit). #!/usr/bin/env python from decimal import Decimal as D # We don't have transcendental functions for Decimals. _E = +D('2.71828182845904523536028747135266249775724709369995') def exp(y): return _E ** y # System specification def f(x, t): return 4 * exp(D('0.8') * t) - D('0.5') * x def analytic(t): return (D('40')/D('13'))*exp(D('0.8')*t) - (D('14')/D('13'))*exp(-D('0.5')*t) # This is for the analytic solution to be true x0 = 2 def rk4(f, x, t, dt, carry=0): k1 = dt * f(x, t) k2 = dt * f(x + k1/2, t + dt/2) k3 = dt * f(x + k2/2, t + dt/2) k4 = dt * f(x + k3, t + dt) dx = (k1 + 2*k2 + 2*k3 + k4) / 6 return x + dx, carry def solveto(stepper, f, x1, t1, t2, dtmax, carry=0): t = t1 x = x1 while t < t2: newt = min(t + dtmax, t2) dt = newt - t x, carry = stepper(f, x, t, dt, carry) t = newt return x, carry T = 4 xtrue = analytic(T) # It's good to use powers of 10 when working in decimal dts = [D('10')**-n for n in range(2, 6)] for dt in dts: xest, carry = solveto(rk4, f, x0, 0, T, dt) error = abs((xest - xtrue) / xtrue) print('dt: %e error: %.3e' % (dt, error)) Oscar From oscar.j.benjamin at gmail.com Sat Aug 10 17:19:07 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sat, 10 Aug 2013 16:19:07 +0100 Subject: [Tutor] constrained least square fitting using python In-Reply-To: References: Message-ID: On 10 August 2013 08:18, eryksun wrote: > On Fri, Aug 9, 2013 at 9:12 PM, Oscar Benjamin > wrote: >> >> Which of the two solutions is more accurate in terms of satisfying the >> constraint and minimising the objective function? Do they take similar >> times to run? > > In my example, minimize() required 28 function evaluations, so your > solution is more efficient. Really what I meant is: do they both run in the blink of an eye. I'm guessing they do since you haven't given a time in seconds. > As far as minimizing the objective and satisfying the constraint, the > results are very close: > > >>> objective(pcons, ymeas, X) # minimize > 2036.6364327061785 > >>> np.sum(pcons) > -2.0816681711721685e-17 > > >>> objective(plsqcon, ymeas, X) # analytic > 2036.6364327060171 > >>> np.sum(plsqcon) > 0.0 That seems plenty good enough. It's impressive that the more general SLSQP method can be so accurate. > I think SLSQP is using something like gradient descent on successive > quadratic approximations of the objective. But it's not something I > know a lot about. Here's the paper and links to the FORTRAN source > code: > > Dieter Kraft, "Algorithm 733: TOMP?Fortran modules for optimal control > calculations," ACM Transactions on Mathematical Software, vol. 20, no. > 3, pp. 262-281 (1994) > http://doi.acm.org/10.1145/192115.192124 > > http://www.netlib.org/toms/733 > https://github.com/scipy/scipy/blob/master/scipy/optimize/slsqp/slsqp_optmz.f I just had a quick look at that code; I've decided I won't even try to understand it! Oscar From oscar.j.benjamin at gmail.com Sat Aug 10 18:33:36 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sat, 10 Aug 2013 17:33:36 +0100 Subject: [Tutor] hi In-Reply-To: References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> <000001ce9590$e1b667a0$a52336e0$@orange.mu> Message-ID: On 10 August 2013 16:10, Oscar Benjamin wrote: > > We can probably get the error down to say 1e-27 by using a step-size > of 1e-6. However it would take an hour or two to run that on my > computer (there's probably not much scope for optimisation since > profiling identifies the exp() function as the culprit). Actually I was wrong about that. There is a Decimal.exp method that is about 10x faster: >>> from decimal import Decimal as D >>> D(1).exp() Decimal('2.718281828459045235360287471') Oscar From alan.gauld at btinternet.com Sun Aug 11 18:05:30 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 11 Aug 2013 17:05:30 +0100 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: References: Message-ID: On 09/08/13 16:50, SM wrote: Sorry I only just picked this up. > (ex: self.tab_fw = QtGui.QWidget(), self.tab_ann = QtGui.QWidget(), > etc), its own textEdit window and its own progress bar widget. > All the tabs are defined within this single class - they are not > instances of the class, as the tabs are distinct from each other. All of > that is working really well. OK, I'll assume the structure looks like MainGUIWindow - Tab1 - TextEdit1 - ProgressBar1 - any other widgets on tab1 - Tab2 - TextEdit2 - ProgressBar2 - any other widgets on tab2 - Tab3 - TextEdit3 - ProgressBar3 - any other widgets on tab3 And that both the TextEdit and progress bar widgets are part of the Qt framework/library? So there are 3 distinct progress bar instances, one per tab? > an added feature, I am now asked to have a "progress bar" widget > which appears when an operation is going on, to tell the user that > he/she has to wait. Is this an extra (4th?) progress bar or is this the ones already described as being in the tabs? Also, when you say "appears" do you mean its invisible until the operation starts? Or is it visible but inactive (maybe greyed out?) > [SM]: Yes. I shouldn't say duplicated, but each tab has a different > progress bar of its own. In the example, I was quoting the code for just > on tab. The others will be implemented similarly. OK So I'm again going to assume: A total of 1 progress bar per tab. The "added feature" is what these bars are addressing? > So I am running two threads - first one is the progressbar > widget which has a rectangular slab spinning sideways to > indicate that the operation is going on. I usually expect a progress bar to indicate the percentage complete but this sounds like its just a spinning eggshell activity indicator. Is that correct? You have no progress value indication? > The second thread is the one which is actually doing the operation. > I have a flag that the second thread sets after the job is done OK, again I'll assume we are dealing with just an activity indicator not a value display. > and stops spinning and gets out of the loop when the flag is set. I > have that working as well. OK, Good the picture is building. > [SM]: I did start out with a timer. But due to my lack of experience > with usage of timer in Python, the code I wrote was running sequentially That sounds like you tried to use a Python native timer. Normally in a GUI the toolkit (Qt in this case) provides a timer event that triggers via the normal event mechanism. You set the timer, the event arrives and you do something then reset the timer for the next time round. I found this page that describes the Qt approach (which includes multi-shot timers) and links to more detailed examples etc. http://qt-project.org/doc/qt-4.7/timers.html#id-fd46b213-376e-4636-a7d2-8ae899de1e11 The code is in C++ but should translate easily to python and PyQt. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Sun Aug 11 23:13:19 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 11 Aug 2013 22:13:19 +0100 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: References: Message-ID: On 11/08/13 17:05, Alan Gauld wrote: > this sounds like its just a spinning eggshell erm, make that egg-timer... auto spell correction strikes again! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From engg at cleantechsolution.in Fri Aug 9 10:33:02 2013 From: engg at cleantechsolution.in (Engineering) Date: Fri, 9 Aug 2013 14:03:02 +0530 Subject: [Tutor] Python Arduino Web Page Message-ID: <000601ce94db$147b32d0$3d719870$@in> I have written the following code in Python for my Arduino #!c:\Python27\Python.exe import cgi, cgitb; import sys, serial cgitb.enable() ser = serial.Serial('COM27', 9600) myvar = ser.readline() print "Content-type:text/html\n\n" print """ Real Time Temperature

Real Time Temperature:

""" I have Apache Web Server But on running this my localhost , I am getting the following output "TemperatureNaN" . Why is NaN being showed. Why cant I get the number as read by se.readline()? Is there a way to separate the Python code and the Javascript on different files. If so can you please give a example.? I am not really interested to do with bottle or other web framework as I will be controlling my arduino with python from the web and also do video image processing with openCV. Open CV have a support for Python and not for Javascript. Thanks Subhendu Sinha Chaudhuri CLEANTECH SOLUTION www.cleantechsolution.in SAVE PAPER , SAVE EARTH -------------- next part -------------- An HTML attachment was scrubbed... URL: From engg at cleantechsolution.in Fri Aug 9 14:16:27 2013 From: engg at cleantechsolution.in (Engineering) Date: Fri, 9 Aug 2013 17:46:27 +0530 Subject: [Tutor] Python and Web Message-ID: <001601ce94fa$4a175120$de45f360$@in> Dear All After a lot of struggling for weeks , I can read the serial port readings in my web page. Below is the code #!c:\Python27\Python.exe import cgi, cgitb; import sys, serial ,time cgitb.enable() ser = serial.Serial('COM27', 9600) numOfLines = 0 print "Content-type:text/html\n\n" print """ Real Time Temperature

Real Time Temperature:

""" while True: response = ser.readline() print """
""" ser.close() sys.exit(0) BUT is this good programming ? I personally do not feel so. SECONDLY , during the whole execution process , the cursor is showing busy and I am prevented to click anything else. Why is this so? How to avoid it. CLEANTECH SOLUTION www.cleantechsolution.in SAVE PAPER , SAVE EARTH -------------- next part -------------- An HTML attachment was scrubbed... URL: From eschneider92 at comcast.net Sat Aug 10 05:30:37 2013 From: eschneider92 at comcast.net (eschneider92 at comcast.net) Date: Sat, 10 Aug 2013 03:30:37 +0000 (UTC) Subject: [Tutor] Beginner question In-Reply-To: <982153176.733237.1376105121438.JavaMail.root@comcast.net> Message-ID: <2050405155.733934.1376105437847.JavaMail.root@comcast.net> I've been learning python from the website 'inventwithpython.com', and I'm on a chapter that covers the following code: import random import time def displayIntro(): print('You are in a land full of dragons. In front of you,') print('you see two caves. In one cave, the dragon is friendly') print('and will share his treasure with you. The other dragon') print('is greedy and hungry, and will eat you on sight.') print() def chooseCave(): cave = '' while cave != '1' and cave != '2': print('Which cave will you go into? (1 or 2)') cave = input() return cave def checkCave(chosenCave): print('You approach the cave...') time.sleep(2) print('It is dark and spooky...') time.sleep(2) print('A large dragon jumps out in front of you! He opens his jaws and...') print() time.sleep(2) friendlyCave = random.randint(1, 2) if chosenCave == str(friendlyCave): print('Gives you his treasure!') else: print('Gobbles you down in one bite!') playAgain = 'yes' while playAgain == 'yes' or playAgain == 'y': displayIntro() caveNumber = chooseCave() checkCave(caveNumber) print('Do you want to play again? (yes or no)') playAgain = input() I'm confused about what the line 'checkCave(caveNumber)' does and how it works. I would appreciate any help with this Thank you, Eric -------------- next part -------------- An HTML attachment was scrubbed... URL: From john.feleppa at gmail.com Fri Aug 9 09:51:20 2013 From: john.feleppa at gmail.com (John Feleppa) Date: Fri, 9 Aug 2013 08:51:20 +0100 Subject: [Tutor] Python Programming for the absolute beginner 3e, Ch3 Challenge 4 Message-ID: Thanks for getting back to me... I am using Python 3.3.2. The challenge is as follows: # Chapter 3 Challenge 4 # # Write the psudocode for a program where the player and the computer # trade places in the number guessing game. That is, the player picks a # random number between 1 and 100 that the computer has to guess. # Before you start, think about how you guess. If all goes well, try # coding the game. It builds on the previous challenge, where the user guesses the number - I coded that OK, as below. import random print("\tWelcome to 'Guess My Number'!") print("\nI'm thinking of a number between 1 and 100.") print("Try to guess it in as few attempts as possible.\n") # set the initial values the_number = random.randint(1, 100) guess = int(input("Take a guess: ")) tries = 1 # guessing loop while guess != the_number and tries < 10: if guess > the_number: print("Lower...") else: print("Higher...") guess = int(input("Take a guess: ")) tries += 1 if guess == the_number: print("You guessed it! The number was", the_number) print("And it only took you", tries, "tries!\n") else: print("Huh - you took ", tries, "tries and still never got it.") print("It was ", the_number, " - but why tell you, you'll forget it, won't you.") input("\n\nPress the enter key to exit.") -------------- next part -------------- An HTML attachment was scrubbed... URL: From piet at vanoostrum.org Fri Aug 9 22:21:21 2013 From: piet at vanoostrum.org (Piet van Oostrum) Date: Fri, 9 Aug 2013 16:21:21 -0400 Subject: [Tutor] [Soap-Python] Convert SOAP response (ArrayOfInt) to Python list In-Reply-To: <51D5D134.6020506@bioprocess.org> References: <51D5D134.6020506@bioprocess.org> Message-ID: <20997.20289.58335.215440@cochabamba.vanoostrum.org> Robert Winkler wrote: > Thanks to the OSA library, which works for SOAP requests with Python 3.x, I can now use SOAP > services at http://www.chemspider.com. > > The result is a list of accession numbers (which correspond to chemical compounds) and I get them > in the following format: > > (ArrayOfInt){ > int[] = [ > 5744, > 69182, > 292, > 68027, > 3404131, > 82616, > 18280, > 11200, > 704646, > 543430 > ... > ] > } > > How could I transform this to a simple python list? > > [5744, 69182, 292,68027, 3404131, 82616, 18280, 11200, 704646, 543430 ...] > > Conversion to a numpy array (and subsequent list(), or similar) does not solve the problem, since > the structure is maintained; the numpy.shape returns (). > > Suggestions? If result is the return value then result.int will give you the list. -- Piet van Oostrum WWW: http://pietvanoostrum.com/ PGP key: [8DAE142BE17999C4] From vick1975 at orange.mu Sun Aug 11 07:09:43 2013 From: vick1975 at orange.mu (Vick) Date: Sun, 11 Aug 2013 09:09:43 +0400 Subject: [Tutor] hi In-Reply-To: References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> <000001ce9590$e1b667a0$a52336e0$@orange.mu> Message-ID: <000001ce9650$fe15e180$fa41a480$@orange.mu> > -----Original Message----- > From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] > Sent: Saturday, 10 August, 2013 19:10 > To: Vick > Cc: tutor at python.org > Subject: Re: [Tutor] hi > > > How long a time it takes depends on many different things: the language, the > precision, how good a programmer you are. Much of the "this integrator is > better than that one" arguments are implicitly tied to certain presumptions > about how your code gets implemented. [Vick] Well, if you coded the methods in the same format, structure and language, surely the higher order method will produce better accuracy for the same ODE problem and for the same amount of time than the lower order. > Here is an example that shows we can achieve the precision of scipy's > integrator (for the system you posted) easily using a standard 4th order > Runge-Kutta: [Vick] Your code shows just that. I ran it and it went on for like about 40 minutes. In that amount of time, the memory was taxed a lot. For the same ODE problem, RK4 should produce an error of 1e-4 in a reasonable amount of time (about 2 secs) and utilizing normal computer memory for its calculations. As I mentioned before I used this problem as a test for accuracy to compare different methods. I use percentage change (x 100) to derive the error for each method. RK4 in this instance scores about 1e-4 accuracy. With this problem I've tested RKF5, DPRK45, some 6 and 7 order RK methods and the last one is the DOPRI8(7)13. The Leapfrog method is a symplectic integrator and I am currently trying to code it. I have it in Excel format already. The following is my code for the ODE problem test for RK4 and DOPRI8(7)13: (Note that they are using the same language, structure and format) (you will also need mpmath to run: I use mpmath for large number calculations) The code should run using less than 2 secs and the output is pre-defined for time, y , true value and error. The error for the two methods will show the difference between the different orders. You should change the call to each method for comparison as I coded it to run one method at any one time. from mpmath import * mp.dps=30 def drange(start, stop, step): r = start while r < stop: yield r r += step def f(t, y): return (mpf('4') * (exp(1) ** (mpf('0.8') * t))) - (mpf('0.5') * y) def rungk4(t, y, dt): k1 = dt * f(t, y) k2 = dt * f(t + dt / mpf('2'), y + k1 / mpf('2')) k3 = dt * f(t + dt / mpf('2'), y + k2 / mpf('2')) k4 = dt * f(t + dt, y + k3) return y + (k1 + mpf('2') * (k2 + k3) + k4) / mpf('6') def rungkdp8(t, y, dt): k1 = dt * f(t, y) k2 = dt * f(t + dt / mpf('18'), y + k1 / mpf('18')) k3 = dt * f(t + dt / mpf('12'), y + k1 / mpf('48') + k2 / mpf('16')) k4 = dt * f(t + dt / mpf('8'), y + k1 / mpf('32') + k3 * mpf('3') / mpf('32')) k5 = dt * f(t + dt * mpf('5') / mpf('16'), y + k1 * mpf('5') / mpf('16') - k3 * mpf('75') / mpf('64') + k4 * mpf('75') / mpf('64')) k6 = dt * f(t + dt * mpf('3') / mpf('8'), y + k1 * mpf('3') / mpf('80') + k4 * mpf('3') / mpf('16') + k5 * mpf('3') / mpf('20')) k7 = dt * f(t + dt * mpf('59') / mpf('400'), y + k1 * mpf('29443841') / mpf('614563906') + k4 * mpf('77736538') / mpf('692538347') - k5 * mpf('28693883') / mpf('1125000000') + k6 * mpf('23124283') / mpf('1800000000')) k8 = dt * f(t + dt * mpf('93') / mpf('200'), y + k1 * mpf('16016141') / mpf('946692911') + k4 * mpf('61564180') / mpf('158732637') + k5 * mpf('22789713') / mpf('633445777') + k6 * mpf('545815736') / mpf('2771057229') - k7 * mpf('180193667') / mpf('1043307555')) k9 = dt * f(t + dt * mpf('5490023248') / mpf('9719169821'), y + k1 * mpf('39632708') / mpf('573591083') - k4 * mpf('433636366') / mpf('683701615') - k5 * mpf('421739975') / mpf('2616292301') + k6 * mpf('100302831') / mpf('723423059') + k7 * mpf('790204164') / mpf('839813087') + k8 * mpf('800635310') / mpf('3783071287')) k10 = dt * f(t + dt * mpf('13') / mpf('20'), y + k1 * mpf('246121993') / mpf('1340847787') - k4 * mpf('37695042795') / mpf('15268766246') - k5 * mpf('309121744') / mpf('1061227803') - k6 * mpf('12992083') / mpf('490766935') + k7 * mpf('6005943493') / mpf('2108947869') + k8 * mpf('393006217') / mpf('1396673457') + k9 * mpf('123872331') / mpf('1001029789')) k11 = dt * f(t + dt * mpf('1201146811') / mpf('1299019798'), y - k1 * mpf('1028468189') / mpf('846180014') + k4 * mpf('8478235783') / mpf('508512852') + k5 * mpf('1311729495') / mpf('1432422823') - k6 * mpf('10304129995') / mpf('1701304382') - k7 * mpf('48777925059') / mpf('3047939560') + k8 * mpf('15336726248') / mpf('1032824649') - k9 * mpf('45442868181') / mpf('3398467696') + k10 * mpf('3065993473') / mpf('597172653')) k12 = dt * f(t + dt, y + k1 * mpf('185892177') / mpf('718116043') - k4 * mpf('3185094517') / mpf('667107341') - k5 * mpf('477755414') / mpf('1098053517') - k6 * mpf('703635378') / mpf('230739211') + k7 * mpf('5731566787') / mpf('1027545527') + k8 * mpf('5232866602') / mpf('850066563') - k9 * mpf('4093664535') / mpf('808688257') + k10 * mpf('3962137247') / mpf('1805957418') + k11 * mpf('65686358') / mpf('487910083')) k13 = dt * f(t + dt, y + k1 * mpf('403863854') / mpf('491063109') - k4 * mpf('5068492393') / mpf('434740067') - k5 * mpf('411421997') / mpf('543043805') + k6 * mpf('652783627') / mpf('914296604') + k7 * mpf('11173962825') / mpf('925320556') - k8 * mpf('13158990841') / mpf('6184727034') + k9 * mpf('3936647629') / mpf('1978049680') - k10 * mpf('160528059') / mpf('685178525') + k11 * mpf('248638103') / mpf('1413531060')) return y + (k1 * mpf('14005451') / mpf('335480064') - k6 * mpf('59238493') / mpf('1068277825') + k7 * mpf('181606767') / mpf('758867731') + k8 * mpf('561292985') / mpf('797845732') - k9 * mpf('1041891430') / mpf('1371343529') + k10 * mpf('760417239') / mpf('1151165299') + k11 * mpf('118820643') / mpf('751138087') - k12 * mpf('528747749') / mpf('2220607170') + k13 / mpf('4')) #return y + (k1 * mpf('13451932') / mpf('455176623') - k6 * mpf('808719846') / mpf('976000145') + k7 * mpf('1757004468') / mpf('5645159321') + k8 * mpf('656045339') / mpf('265891186') - k9 * mpf('3867574721') / mpf('1518517206') + k10 * mpf('465885868') / mpf('322736535') + k11 * mpf('53011238') / mpf('667516719') + k12 * mpf('2') / mpf('45')) h = mpf('0.25') t = mpf('0') y = mpf('2') tn = mpf('4.25') tv =(mpf('4')/mpf('1.3'))*exp(mpf('0.8')*t)-(mpf('1.4')/mpf('1.3'))*exp(mpf('-0 .5')*t) ti = t+h tv1 =(mpf('4')/mpf('1.3'))*exp(mpf('0.8')*ti)-(mpf('1.4')/mpf('1.3'))*exp(mpf('- 0.5')*ti) mi = rungkdp8(t,y,h) #change method here (either rungkdp8 or rungk4) er1 =abs((tv1-mi)/tv1)*mpf('100') print "x Y True Value Error" print "--------------------------------------------------------------------------- ----------------------" print t," ",y," ",tv print ti," ", mi," ",tv1," ",er1 for i in drange (ti, tn,h): mi = rungkdp8(i,mi,h) #change method here (either rungkdp8 or rungk4) tvn =(mpf('4')/mpf('1.3'))*exp(mpf('0.8')*(i+h))-(mpf('1.4')/mpf('1.3'))*exp(mpf ('-0.5')*(i+h)) er2 = abs((tvn-mi)/tvn)*mpf('100') print i+h," ", mi," ",tvn," ",er2 Vick From amitsaha.in at gmail.com Mon Aug 12 00:53:19 2013 From: amitsaha.in at gmail.com (Amit Saha) Date: Mon, 12 Aug 2013 08:53:19 +1000 Subject: [Tutor] dbus.Array to string Message-ID: Hello all, The other day, I had to convert a dbus.Array [1] object to a string. I found a way to do it, but I am not sure if that is the best way. Here is a dbus.Array object: dbus.Array([dbus.Byte(66), dbus.Byte(105), dbus.Byte(103), dbus.Byte(80), dbus.Byte(111), dbus.Byte(110), dbus.Byte(100), dbus.Byte(54), dbus.Byte(55), dbus.Byte(57), dbus.Byte(68), dbus.Byte(56), dbus.Byte(53)], signature=dbus.Signature('y'), variant_level=1) And this is how I converted it to it's string representation: >>> import dbus >>> ssid = "dbus.Array([dbus.Byte(66), dbus.Byte(105), dbus.Byte(103), dbus.Byte(80), dbus.Byte(111), dbus.Byte(110), dbus.Byte(100), dbus.Byte(54), dbus.Byte(55), dbus.Byte(57), dbus.Byte(68), dbus.Byte(56), dbus.Byte(53)], signature=dbus.Signature('y'), variant_level=1) " >>> ssid = ''.join([chr(character) for character in ssid]) >>> ssid 'BigPond679D85' Basically I use the idea that iterating over the array will give me only the dbus.Byte 's and hence use chr() to convert it back to the character representation. Is this is the best way to do it? [1] http://dbus.freedesktop.org/doc/dbus-python/api/dbus.Array-class.html Thanks, Amit. -- http://echorand.me From alan.gauld at btinternet.com Mon Aug 12 02:28:13 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 12 Aug 2013 01:28:13 +0100 Subject: [Tutor] Beginner question In-Reply-To: <2050405155.733934.1376105437847.JavaMail.root@comcast.net> References: <982153176.733237.1376105121438.JavaMail.root@comcast.net> <2050405155.733934.1376105437847.JavaMail.root@comcast.net> Message-ID: On 10/08/13 04:30, eschneider92 at comcast.net wrote: > I've been learning python from the website 'inventwithpython.com', and > I'm on a chapter that covers the following code: > > import random > import time > def displayIntro(): > print('You are in a land full of dragons. In front of you,') > print('you see two caves. In one cave, the dragon is friendly') > print('and will share his treasure with you. The other dragon') > print('is greedy and hungry, and will eat you on sight.') > print() > def chooseCave(): > cave = '' > while cave != '1' and cave != '2': > print('Which cave will you go into? (1 or 2)') > cave = input() > return cave > def checkCave(chosenCave): > print('You approach the cave...') > time.sleep(2) > print('It is dark and spooky...') > time.sleep(2) > print('A large dragon jumps out in front of you! He opens his jaws > and...') > print() > time.sleep(2) > friendlyCave = random.randint(1, 2) > if chosenCave == str(friendlyCave): > print('Gives you his treasure!') > else: > print('Gobbles you down in one bite!') > playAgain = 'yes' > while playAgain == 'yes' or playAgain == 'y': > displayIntro() > caveNumber = chooseCave() > checkCave(caveNumber) > print('Do you want to play again? (yes or no)') > playAgain = input() > I'm confused about what the line 'checkCave(caveNumber)' does and how it > works. I would appreciate any help with this It calls the checkCave function defined above. It passes in the cave 'number' as an argument. The function basically decides whether the monster inside is friendly or not and prints an appropriate message. It then returns control to the main while loop. Does that help? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From steve at pearwood.info Mon Aug 12 03:04:48 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 12 Aug 2013 11:04:48 +1000 Subject: [Tutor] dbus.Array to string In-Reply-To: References: Message-ID: <520834B0.80704@pearwood.info> On 12/08/13 08:53, Amit Saha wrote: > Hello all, > > The other day, I had to convert a dbus.Array [1] object to a string. I > found a way to do it, but I am not sure if that is the best way. Does the Array object not have a "toString" method? Or similar? What do you get when you call str() or repr() on the Array object? > Here is a dbus.Array object: > > dbus.Array([dbus.Byte(66), dbus.Byte(105), dbus.Byte(103), > dbus.Byte(80), dbus.Byte(111), dbus.Byte(110), dbus.Byte(100), > dbus.Byte(54), dbus.Byte(55), dbus.Byte(57), dbus.Byte(68), > dbus.Byte(56), dbus.Byte(53)], signature=dbus.Signature('y'), > variant_level=1) Basically it is an array of bytes, with a couple of extra arguments. Do you care about the signature and variant_level arguments? > And this is how I converted it to it's string representation: Careful. In usual Python terminology, the string representation of an object, or just repr, is the string that represents how the object is created, more or less. So the string representation would be something like: "Array([Byte(66), Byte(105), ..., Byte(53)], signature=Signature('y'), variant_level=1)" which is nothing like what you want. >>>> import dbus > >>>> ssid = "dbus.Array([dbus.Byte(66), dbus.Byte(105), dbus.Byte(103), dbus.Byte(80), dbus.Byte(111), dbus.Byte(110), dbus.Byte(100), dbus.Byte(54), dbus.Byte(55), dbus.Byte(57), dbus.Byte(68), dbus.Byte(56), dbus.Byte(53)], signature=dbus.Signature('y'), variant_level=1) > " Here you create a string, which looks like "dbus.Array(...)". >>>> ssid = ''.join([chr(character) for character in ssid]) Since ssid is already a string, iterating over it gives you characters; calling chr() on a character raises an exception. >>>> ssid > 'BigPond679D85' Not with the code you provide. If you drop the quotation marks around the dbus.Array, so you get an Array object instead of a string, then your code may work. But look at your list comprehension: [chr(character) for character in ssid] That's confusing. If iterating over ssid gives characters, as the code claims, then there is no need to call chr(), and in fact you wouldn't need the list comp at all, you could just say: ''.join(ssid) But it isn't an array of characters, it is an array of bytes. So: ''.join([chr(byte) for byte in ssid]) is less misleading and confusing. Another alternative: ''.join(map(chr, ssid)) is more terse, but may not be as readable to those who aren't familiar with map. Both forms should be more or less equally as efficient. -- Steven From amitsaha.in at gmail.com Mon Aug 12 03:14:00 2013 From: amitsaha.in at gmail.com (Amit Saha) Date: Mon, 12 Aug 2013 11:14:00 +1000 Subject: [Tutor] dbus.Array to string In-Reply-To: <520834B0.80704@pearwood.info> References: <520834B0.80704@pearwood.info> Message-ID: On Mon, Aug 12, 2013 at 11:04 AM, Steven D'Aprano wrote: > On 12/08/13 08:53, Amit Saha wrote: >> >> Hello all, >> >> The other day, I had to convert a dbus.Array [1] object to a string. I >> found a way to do it, but I am not sure if that is the best way. > > > > Does the Array object not have a "toString" method? Or similar? No, not from what I see. > > What do you get when you call str() or repr() on the Array object? >> str(ssid) "dbus.Array([dbus.Byte(66), dbus.Byte(105), dbus.Byte(103), dbus.Byte(80), dbus.Byte(111), dbus.Byte(110), dbus.Byte(100), dbus.Byte(54), dbus.Byte(55), dbus.Byte(57), dbus.Byte(68), dbus.Byte(56), dbus.Byte(53)], signature=dbus.Signature('y'), variant_level=1)" > > > >> Here is a dbus.Array object: >> >> dbus.Array([dbus.Byte(66), dbus.Byte(105), dbus.Byte(103), >> dbus.Byte(80), dbus.Byte(111), dbus.Byte(110), dbus.Byte(100), >> dbus.Byte(54), dbus.Byte(55), dbus.Byte(57), dbus.Byte(68), >> dbus.Byte(56), dbus.Byte(53)], signature=dbus.Signature('y'), >> variant_level=1) > > > > Basically it is an array of bytes, with a couple of extra arguments. Do you > care about the signature and variant_level arguments? No, I don't care about the the signature and variant_level arguments. > > > > >> And this is how I converted it to it's string representation: > > > Careful. In usual Python terminology, the string representation of an > object, or just repr, is the string that represents how the object is > created, more or less. So the string representation would be something like: > > "Array([Byte(66), Byte(105), ..., Byte(53)], signature=Signature('y'), > variant_level=1)" > > which is nothing like what you want. > > > >>>>> import dbus >> >> >>>>> ssid = "dbus.Array([dbus.Byte(66), dbus.Byte(105), dbus.Byte(103), >>>>> dbus.Byte(80), dbus.Byte(111), dbus.Byte(110), dbus.Byte(100), >>>>> dbus.Byte(54), dbus.Byte(55), dbus.Byte(57), dbus.Byte(68), dbus.Byte(56), >>>>> dbus.Byte(53)], signature=dbus.Signature('y'), variant_level=1) >> >> " > > > Here you create a string, which looks like "dbus.Array(...)". > > > >>>>> ssid = ''.join([chr(character) for character in ssid]) > > > Since ssid is already a string, iterating over it gives you characters; > calling chr() on a character raises an exception. Sorry, ssid wasn't supposed to be a string, of course. > > >>>>> ssid >> >> 'BigPond679D85' > > > Not with the code you provide. > > > If you drop the quotation marks around the dbus.Array, so you get an Array > object instead of a string, then your code may work. But look at your list > comprehension: > > > [chr(character) for character in ssid] > > That's confusing. If iterating over ssid gives characters, as the code > claims, then there is no need to call chr(), and in fact you wouldn't need > the list comp at all, you could just say: > > ''.join(ssid) > > > But it isn't an array of characters, it is an array of bytes. So: > > ''.join([chr(byte) for byte in ssid]) > > is less misleading and confusing. Agree. > > Another alternative: > > ''.join(map(chr, ssid)) > > > is more terse, but may not be as readable to those who aren't familiar with > map. Both forms should be more or less equally as efficient. Right. Just for the sake of correctness: >>> import dbus >>> ssid=dbus.Array([dbus.Byte(66), dbus.Byte(105), dbus.Byte(103), dbus.Byte(80), dbus.Byte(111), dbus.Byte(110), dbus.Byte(100), dbus.Byte(54), dbus.Byte(55), dbus.Byte(57), dbus.Byte(68), dbus.Byte(56), dbus.Byte(53)], signature=dbus.Signature('y'), variant_level=1) >>> temp_ssid = ''.join([chr(byte) for byte in ssid]) >>> temp_ssid 'BigPond679D85' Thanks, Amit. -- http://echorand.me From eryksun at gmail.com Mon Aug 12 03:40:02 2013 From: eryksun at gmail.com (eryksun) Date: Sun, 11 Aug 2013 21:40:02 -0400 Subject: [Tutor] dbus.Array to string In-Reply-To: References: <520834B0.80704@pearwood.info> Message-ID: On Sun, Aug 11, 2013 at 9:14 PM, Amit Saha wrote: > >>> import dbus > >>> ssid=dbus.Array([dbus.Byte(66), dbus.Byte(105), dbus.Byte(103), > dbus.Byte(80), dbus.Byte(111), dbus.Byte(110), dbus.Byte(100), > dbus.Byte(54), dbus.Byte(55), dbus.Byte(57), dbus.Byte(68), > dbus.Byte(56), dbus.Byte(53)], signature=dbus.Signature('y'), > variant_level=1) > > >>> temp_ssid = ''.join([chr(byte) for byte in ssid]) > >>> temp_ssid > 'BigPond679D85' Typically strings should be unicode. If the byte sequence is Latin-1 (including ASCII), you can map unichr() and join the characters with u''.join(); that's equivalent to chr() and ''.join() in 3.x. More generally, decode() the byte string. A simply way that works in 2.6+ is to create a bytearray: >>> bytearray(ssid).decode('latin-1') u'BigPond679D85' From steve at pearwood.info Mon Aug 12 04:34:37 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 12 Aug 2013 12:34:37 +1000 Subject: [Tutor] dbus.Array to string In-Reply-To: References: <520834B0.80704@pearwood.info> Message-ID: <520849BD.4030004@pearwood.info> On 12/08/13 11:40, eryksun wrote: > On Sun, Aug 11, 2013 at 9:14 PM, Amit Saha wrote: >>>>> import dbus >>>>> ssid=dbus.Array([dbus.Byte(66), dbus.Byte(105), dbus.Byte(103), >> dbus.Byte(80), dbus.Byte(111), dbus.Byte(110), dbus.Byte(100), >> dbus.Byte(54), dbus.Byte(55), dbus.Byte(57), dbus.Byte(68), >> dbus.Byte(56), dbus.Byte(53)], signature=dbus.Signature('y'), >> variant_level=1) >> >>>>> temp_ssid = ''.join([chr(byte) for byte in ssid]) >>>>> temp_ssid >> 'BigPond679D85' > > Typically strings should be unicode. If the byte sequence is Latin-1 > (including ASCII), you can map unichr() and join the characters with > u''.join(); that's equivalent to chr() and ''.join() in 3.x. More > generally, decode() the byte string. A simply way that works in 2.6+ > is to create a bytearray: > > >>> bytearray(ssid).decode('latin-1') > u'BigPond679D85' Nice! But, what makes you think that the encoding will be latin-1? If I had to guess an encoding, I'd guess UTF-8. -- Steven From eryksun at gmail.com Mon Aug 12 05:13:07 2013 From: eryksun at gmail.com (eryksun) Date: Sun, 11 Aug 2013 23:13:07 -0400 Subject: [Tutor] dbus.Array to string In-Reply-To: <520849BD.4030004@pearwood.info> References: <520834B0.80704@pearwood.info> <520849BD.4030004@pearwood.info> Message-ID: On Sun, Aug 11, 2013 at 10:34 PM, Steven D'Aprano wrote: > On 12/08/13 11:40, eryksun wrote: >> Typically strings should be unicode. If the byte sequence is Latin-1 >> (including ASCII), you can map unichr() and join the characters with >> u''.join(); that's equivalent to chr() and ''.join() in 3.x. More >> generally, decode() the byte string. A simply way that works in 2.6+ >> is to create a bytearray: >> >> >>> bytearray(ssid).decode('latin-1') >> u'BigPond679D85' > > But, what makes you think that the encoding will be latin-1? If I had to > guess an encoding, I'd guess UTF-8. Unicode ordinals in the 8-bit range are Latin-1 (aka ISO-8859-1, i.e. Basic Latin and Latin-1 Supplement, including C0 and C1 control codes). You can always decode as Latin-1 without getting a decoding error. UTF-8 is multibyte and defaults to raising a decoding error if the sequence isn't valid. It's up to Amit to figure out what the encoding is. It's D-Bus, so probably Linux. In that case, UTF-8 is a good guess. From kliateni at gmail.com Mon Aug 12 11:14:49 2013 From: kliateni at gmail.com (Karim Liateni) Date: Mon, 12 Aug 2013 11:14:49 +0200 Subject: [Tutor] Beginner question Message-ID: 5?t5?6hhhyyyfrrtr eschneider92 at comcast.net a ?crit?: >I've been learning python from the website 'inventwithpython.com', and I'm on a chapter that covers the following code: > > > >import random >import time >def displayIntro(): >print('You are in a land full of dragons. In front of you,') >print('you see two caves. In one cave, the dragon is friendly') >print('and will share his treasure with you. The other dragon') >print('is greedy and hungry, and will eat you on sight.') >print() >def chooseCave(): >cave = '' >while cave != '1' and cave != '2': >print('Which cave will you go into? (1 or 2)') >cave = input() >return cave >def checkCave(chosenCave): >print('You approach the cave...') >time.sleep(2) >print('It is dark and spooky...') >time.sleep(2) >print('A large dragon jumps out in front of you! He opens his jaws and...') >print() >time.sleep(2) >friendlyCave = random.randint(1, 2) >if chosenCave == str(friendlyCave): >print('Gives you his treasure!') >else: >print('Gobbles you down in one bite!') >playAgain = 'yes' >while playAgain == 'yes' or playAgain == 'y': >displayIntro() >caveNumber = chooseCave() >checkCave(caveNumber) >print('Do you want to play again? (yes or no)') >playAgain = input() > >I'm confused about what the line 'checkCave(caveNumber)' does and how it works. I would appreciate any help with this > >Thank you, > >Eric > >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >http://mail.python.org/mailman/listinfo/tutor From dancingbush at gmail.com Mon Aug 12 11:49:35 2013 From: dancingbush at gmail.com (Ciaran Mooney) Date: Mon, 12 Aug 2013 10:49:35 +0100 Subject: [Tutor] Beginner question In-Reply-To: <2050405155.733934.1376105437847.JavaMail.root@comcast.net> References: <2050405155.733934.1376105437847.JavaMail.root@comcast.net> Message-ID: On 10 Aug 2013, at 04:30, eschneider92 at comcast.net wrote: > I've been learning python from the website 'inventwithpython.com', and I'm on a chapter that covers the following code: > > import random > import time > def displayIntro(): > print('You are in a land full of dragons. In front of you,') > print('you see two caves. In one cave, the dragon is friendly') > print('and will share his treasure with you. The other dragon') > print('is greedy and hungry, and will eat you on sight.') > print() > def chooseCave(): > cave = '' > while cave != '1' and cave != '2': > print('Which cave will you go into? (1 or 2)') > cave = input() > return cave > def checkCave(chosenCave): > print('You approach the cave...') > time.sleep(2) > print('It is dark and spooky...') > time.sleep(2) > print('A large dragon jumps out in front of you! He opens his jaws and...') > print() > time.sleep(2) > friendlyCave = random.randint(1, 2) > if chosenCave == str(friendlyCave): > print('Gives you his treasure!') > else: > print('Gobbles you down in one bite!') > playAgain = 'yes' > while playAgain == 'yes' or playAgain == 'y': > displayIntro() > caveNumber = chooseCave() > checkCave(caveNumber) > print('Do you want to play again? (yes or no)') > playAgain = input() > > I'm confused about what the line 'checkCave(caveNumber)' does and how it works. I would appreciate any help with this > > Thank you, > > Eric > Hi Eric, This line calls the method 'checkCave()'. This method takes the argument caveNumber (checkCave(caveNumber) , which is a string returned from user input generated in the method chooseCave(). In checkCave method, the value (caveNumber) is then used in a if/else statement to compare with a random integer (parsed to a string str(frindlyCave)) to determine whether you will be eaten or given treasure ;) Hope that was of some help. Ciaran > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From oscar.j.benjamin at gmail.com Mon Aug 12 14:08:06 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 12 Aug 2013 13:08:06 +0100 Subject: [Tutor] hi In-Reply-To: <000001ce9650$fe15e180$fa41a480$@orange.mu> References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> <000001ce9590$e1b667a0$a52336e0$@orange.mu> <000001ce9650$fe15e180$fa41a480$@orange.mu> Message-ID: On 11 August 2013 06:09, Vick wrote: >> From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] >> >> How long a time it takes depends on many different things: the language, > the >> precision, how good a programmer you are. Much of the "this integrator is >> better than that one" arguments are implicitly tied to certain > presumptions >> about how your code gets implemented. > > [Vick] Well, if you coded the methods in the same format, structure and > language, surely the higher order method will produce better accuracy for > the same ODE problem and for the same amount of time than the lower order. Maybe. It still depends. If you use a higher-order method where it's just not needed you can end up wasting a lot of CPU-cycles. The same goes for using multi-precision arithmetic when you could happily work in double precision. >> Here is an example that shows we can achieve the precision of scipy's >> integrator (for the system you posted) easily using a standard 4th order >> Runge-Kutta: > > [Vick] Your code shows just that. I ran it and it went on for like about 40 > minutes. Which script are you referring to? The first deliberately uses unnecessarily small step-sizes to illustrate the limits of the rk4 algorithm in double precision. The second is slow but achieves absurdly high levels of accuracy. It gives better accuracy than I got with your rungkdp8 (see below) and note that it runs a lot faster in Python 3.3 since the decimal module was made a lot faster 3.3. > In that amount of time, the memory was taxed a lot. I don't think you really know what you're talking about on this subject. The algorithm works with a constant, very small, amount of memory regardless of stepsize. In fact the rungkdp8 method uses more memory, however, the difference is entirely insignificant for such a low-dimensional system. I think you're confusing the fact that the way *you* have implemented the algorithm you store every intermediate step in the computation. My code only stores the desired output and the current value of the intermediate results. > For the same > ODE problem, RK4 should produce an error of 1e-4 in a reasonable amount of > time (about 2 secs) and utilizing normal computer memory for its > calculations. If you modify the check_errors() function in the first script I posted so that it looks like this: def check_errors(): T = 4 xtrue = analytic(T) dt = 2 ** -3 xest, carry = solveto(rk4, f, x0, 0, T, dt) error = abs((xest - xtrue) / xtrue) print('dt : %.5e error : %.2e' % (xest, error)) Then you can run it and get the following: $ time ./ode.py dt : 7.53390e+01 error : 3.28e-07 real 0m0.094s user 0m0.030s sys 0m0.046s In other words it takes 90 milliseconds to run the script and get an error less than 1e-6 (or 1e-4% if you prefer). Actually it really just takes 90 milliseconds to *initialise* the script. We can use timeit to check how long the actual computation takes (with the print line commented out): $ python -m timeit -s 'from ode import check_errors' 'check_errors()' 10000 loops, best of 3: 168 usec per loop It takes 168 microseconds to achieve the accuracy you requested, for your test problem, using my rk4 integrator. Scipy's odeint is probably faster (I'll let you do the timing if you want to know but bear in mind that it takes a significant amount of time to *import* scipy so you should use timeit for benchmarking). > As I mentioned before I used this problem as a test for > accuracy to compare different methods. I use percentage change (x 100) to > derive the error for each method. I don't understand why you insist on defying convention by using percentages. Really your errors should be so small that percent is a meaningless unit of measure. Fractional errors are useful because you can easily relate them to absolute errors without the pointless cognitive burden of a factor of 100 getting in the way. > RK4 in this instance scores about 1e-4 > accuracy. With this problem I've tested RKF5, DPRK45, some 6 and 7 order RK > methods and the last one is the DOPRI8(7)13. Are you testing all the methods with the same step-sizes? If so that's not a fair comparison since they do different amounts of work within each step. > > The Leapfrog method is a symplectic integrator and I am currently trying to > code it. I have it in Excel format already. > > The following is my code for the ODE problem test for RK4 and DOPRI8(7)13: > (Note that they are using the same language, structure and format) (you will > also need mpmath to run: I use mpmath for large number calculations) You don't need mpmath or DOPRI8 for the error-level you said you wanted. Scipy's odeint will do just fine. When you're old, grey and wise like me you'll value the time that it takes to *write* the code much more than the time it takes to run (okay so I'm not that old and my wisdom is questionable but I do have some grey hairs). It took me 2-3 minutes to write the script that solves your problem with scipy's odeint. > > The code should run using less than 2 secs and the output is pre-defined for > time, y , true value and error. The error for the two methods will show the > difference between the different orders. You should change the call to each > method for comparison as I coded it to run one method at any one time. > [snip lots of code] I would report a comparison of timings between this code and my decimal rk4 but I think it would be unfair as I think there may be something wrong with the way your's is implemented. Unless I've misunderstood something it's not achieving the proper benefits of the mpmath library since for some reason the error bottoms out at 1e-18. I replaced the bottom of your script with this: x0 = mpf('2') T = mpf('4') def analytic(t): return (mpf('4')/mpf('1.3'))*exp(mpf('0.8')*(t))-(mpf('1.4')/mpf('1.3'))*exp(mpf('-0.5')*(t)) def solveto(stepper, x1, t1, t2, dt): t = t1 x = x1 while t < t2: newt = min(t + dt, t2) dt = newt - t x = stepper(t, x, dt) t = newt return x xtrue = analytic(T) dts = [mpf('10') ** -n for n in range(0, 3)] errors = [] for dt in dts: xest = solveto(rungkdp8, x0, 0, T, dt) error = abs((xest - xtrue) / xtrue) print('dt : %.1e error : %.2g' % (dt, error)) errors.append(error) from pylab import * loglog(dts, errors) ylabel(r'$E_{rel}$', rotation=0) xlabel(r'$\Delta t$') show() When I run that I get: $ ./vick.py dt : 1.0e+00 error : 1.1e-09 dt : 1.0e-01 error : 9.4e-18 dt : 1.0e-02 error : 2.3e-18 dt : 1.0e-03 error : 2.3e-18 Do you get the same (it could be a difference in the underlying library etc.)? Oscar From oscar.j.benjamin at gmail.com Mon Aug 12 16:18:16 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 12 Aug 2013 15:18:16 +0100 Subject: [Tutor] hi In-Reply-To: References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> <000001ce9590$e1b667a0$a52336e0$@orange.mu> <000001ce9650$fe15e180$fa41a480$@orange.mu> Message-ID: On 12 August 2013 13:08, Oscar Benjamin wrote: > > I would report a comparison of timings between this code and my > decimal rk4 but I think it would be unfair as I think there may be > something wrong with the way your's is implemented. Unless I've > misunderstood something it's not achieving the proper benefits of the > mpmath library since for some reason the error bottoms out at 1e-18. I > replaced the bottom of your script with this: I'm interested to have this DOPRIS8 integrator in my own collection of integrators so I checked the source of the error. It turns out that the coefficients you are using are only rational approximations to the true coefficients as published in [1]. The rational approximations are intended to be good enough for use with double precision but are insufficient for the 30 dps setting that you are using. The reason given by the authors for publishing approximate coefficients is that (in 1981) it was deemed computationally intractable to compute the exact rational coefficients. However that may not still be the case with modern computing power. I doubt that I'll attempt this any time soon but if you're able (and bothered) to do that I'd love to know what the coefficients would be. Oscar References: [1] P.J. Prince, J.R. Dormand, High order embedded Runge-Kutta formulae, Journal of Computational and Applied Mathematics, Volume 7, Issue 1, March 1981, Pages 67-75, ISSN 0377-0427, http://dx.doi.org/10.1016/0771-050X(81)90010-3. (http://www.sciencedirect.com/science/article/pii/0771050X81900103) From kwpolska at gmail.com Mon Aug 12 16:26:50 2013 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Mon, 12 Aug 2013 16:26:50 +0200 Subject: [Tutor] Python and Web In-Reply-To: <001601ce94fa$4a175120$de45f360$@in> References: <001601ce94fa$4a175120$de45f360$@in> Message-ID: On Fri, Aug 9, 2013 at 2:16 PM, Engineering wrote: > Dear All > > After a lot of struggling for weeks , I can read the serial port readings in > my web page. Below is the code > > [snip double-spaced code, courtesy of Outlook 2007] > > BUT is this good programming ? I personally do not feel so. > > SECONDLY , during the whole execution process , the cursor is showing busy > and I am prevented to click anything else. Why is this so? How to avoid it. > [snip 14 lines of signature and emptiness] > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > You already posted this 4 hours ago. Additionally, gmail thinks both messages are spam. You also posted in ugly HTML. Please do not do all three of those. About your code, you do something really ugly in your JS. You are sending JS code forever. This is evil. You should serve the value somewhere and use AJAX to pull it. -- Chris ?Kwpolska? Warrick PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense From ramit.prasad at jpmorgan.com.dmarc.invalid Mon Aug 12 17:20:34 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Mon, 12 Aug 2013 15:20:34 +0000 Subject: [Tutor] Python Programming for the absolute beginner 3e, Ch3 Challenge 4 In-Reply-To: <1376132697.65005.YahooMailNeo@web186002.mail.ir2.yahoo.com> References: <1376035867.52727.YahooMailNeo@web186005.mail.ir2.yahoo.com> <1376132697.65005.YahooMailNeo@web186002.mail.ir2.yahoo.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF47418650D5D@SCACMX008.exchad.jpmchase.net> (some Attributions lost due to HTML->TXT conversion) ALAN GAULD wrote: > > Also don't know how to do a binary chop, but the book hasn't covered anything > > like that - it has just taught if, else and while, > > A binary chop is an algorithm. All you need is if/else and while. > The algorithm looks a bit like this: > > while not found > ? ? ? ?determine the middle value between min ?and max > ? ? ? ?if value > target set max to value > ? ? ? ?if value < target set min to value > ? ? ? ?else found = True Also known as Binary search; binary chop was not a name I had heard before either. http://en.wikipedia.org/wiki/Binary_Search ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From oscar.j.benjamin at gmail.com Mon Aug 12 18:42:05 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 12 Aug 2013 17:42:05 +0100 Subject: [Tutor] hi In-Reply-To: <000701ce9771$187e6bc0$497b4340$@orange.mu> References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> <000001ce9590$e1b667a0$a52336e0$@orange.mu> <000001ce9650$fe15e180$fa41a480$@orange.mu> <000701ce9771$187e6bc0$497b4340$@orange.mu> Message-ID: On 12 August 2013 16:32, Vick wrote: >> From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] > >> The rational approximations are intended to >> be good enough for use with double precision but are insufficient for the > 30 >> dps setting that you are using. The reason given by the authors for > publishing >> approximate coefficients is that (in 1981) it was deemed computationally >> intractable to compute the exact rational coefficients. However that may > not >> still be the case with modern computing power. I doubt that I'll attempt > this >> any time soon but if you're able (and bothered) to do that I'd love to > know >> what the coefficients would be. >> > [Vick] Yeah they are rational coefficients and I got them from the google > book satellites orbits which I believe I posted the links for it previously. > > The problem is how are the coefficients derived? I've never seen an > algorithm to compute the coefficients of an integrator method. Anyway it > would surely involve complicated coding to start an algorithm to compute > them. If I can get it somewhere, I wouldn't mind doing the DOPRI8 > computations. Are you able to access this paper (link at the bottom)? P.J. Prince, J.R. Dormand, High order embedded Runge-Kutta formulae, Journal of Computational and Applied Mathematics, Volume 7, Issue 1, March 1981, Pages 67-75, ISSN 0377-0427, http://dx.doi.org/10.1016/0771-050X(81)90010-3. (http://www.sciencedirect.com/science/article/pii/0771050X81900103) I believe this is the original source for the coefficients you are using. They describe constructing a system of linear rational equations for the coefficients of the method. I think that the equations must be satisfied for any 13-stage explicit RK method that has an 8th-order with embedded 7th-order method. Solving the equations leads to a solution space with 10 degrees of freedom. You could use e.g. sympy for this step of the calculation. They then use these 10 degrees of freedom to try and satisfy some additional conditions that they describe in the paper such as minimising the coefficient of the leading error term. It is this last step that they claim was too computationally expensive, so they did it in 24 d.p numerical precision and then simplified the results to fractions that are accurate to 18 d.p. suitable for use in double precision computation. I haven't studied the equations in detail to see why they might be expensive to solve. If it is possible you would probably want to use the fractions module for the arithmetic at this step. As a simpler example, imagine constructing the 4th order RK method which has Butcher table: 0 | 1/2 | 1/2 1/2 | 0 1/2 1 | 0 0 1 ----|---------------- | 1/6 1/3 1/3 1/6 Essentially there are a number of constraints that must be satisfied for the method to be sensible and to have the appropriate order: 1) The top-left entry is always zero. 2) The sum of any row in the upper-right part is equal to number at the left of the row (3 equations) 3) The coefficients in the Taylor expansion for the first four terms in the error must be zero (4 equations) 4) Possibly more ... This gives us 8 equations for the 15 coefficients: a1 | a2 | b21 a3 | b31 b32 a4 | b41 b42 b43 ---|----------------- | c1 c2 c3 c4 If the equations have full rank then their solution has 7 degrees of freedom. More equations can easily be constructed by imposing additional conditions on how you want the resulting table to look. Standard rk4 is one way to satisfy these conditions. Another way is the 3/8 rule: 0 | 1/3 | 1/3 2/3 | -1/3 1 1 | 1 -1 1 ----|-------------------- | 1/8 3/8 3/8 1/8 Which you prefer is partly arbitrary. The second will usually have smaller error terms but the first is very efficient to implement and code because it has a few zeros in it. Essentially the authors in the Dormand and Prince paper have chosen some complex additional conditions for their 7/8 table and somehow tried to find the coefficients that satisfy them. Much as I would love to see those coefficients, for your own sake it would be a lot easier to just use an Adams-Bashforth integrator. Here's a stripped down script that can compute the exact coefficients for any arbitrary order: #!/usr/bin/env python # # Compute coefficients for Adams-Moulton integrator. # from math import factorial from operator import neg from itertools import dropwhile from fractions import Fraction class Polynomial(tuple): def __new__(typ, coefficients): coeffs = map(Fraction, coefficients) coeffs = dropwhile(lambda c: c==0, coeffs) return tuple.__new__(typ, coeffs) @property def order(self): return len(self) - 1 def evaluate(self, x, denomlimit=None): total, = self[:1] or [0] for c in self[1:]: total = total * x + c if denomlimit is not None: total = total.limit_denominator(denomlimit) return total def integrate(self, a, b): coeffs = [c / (self.order - n + 1) for n, c in enumerate(self)] anti = Polynomial(coeffs + [0]) return anti.evaluate(b) - anti.evaluate(a) def __mul__(p1, p2): ''' >>> from eventphys.poly import Polynomial >>> Polynomial(['2', '-1/2']) * Polynomial([3, 2, '1/2']) Polynomial(['6', '5/2', '0', '-1/4']) ''' p3 = [0] * (len(p1) + len(p2) - 1) for n1, c1 in enumerate(p1): for n2, c2 in enumerate(p2): p3[n1 + n2] += c1 * c2 return Polynomial(p3) def ab_coeffs(nsteps): b = [] N = nsteps for j in range(N): p = Polynomial([1]) for i in range(N): if i != j: p *= Polynomial([1, i]) a = Fraction((-1)**j, factorial(j) * factorial(N-j-1)) b.insert(0, a * p.integrate(0, 1)) return b def main(nsteps): coeffs = ab_coeffs(int(nsteps)) print('Coefficients for Adams-Moulton with %s steps' % nsteps) for c in coeffs: print(c) if __name__ == "__main__": import sys main(*sys.argv[1:]) Example output: $ ./am.py 5 Coefficients for Adams-Moulton with 5 steps 251/720 -637/360 109/30 -1387/360 1901/720 Compare with http://en.wikipedia.org/wiki/Linear_multistep_method#Adams.E2.80.93Bashforth_methods Oscar From oscar.j.benjamin at gmail.com Mon Aug 12 18:52:48 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 12 Aug 2013 17:52:48 +0100 Subject: [Tutor] hi In-Reply-To: <000601ce976e$33f0f830$9bd2e890$@orange.mu> References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> <000001ce9590$e1b667a0$a52336e0$@orange.mu> <000001ce9650$fe15e180$fa41a480$@orange.mu> <000601ce976e$33f0f830$9bd2e890$@orange.mu> Message-ID: On 12 August 2013 16:11, Vick wrote: > > [Vick] That's the whole point! I had to wait for about 40 minutes to get > very high accuracy which the method was not designed to obtain. > Whereas the DOPRI8 got the result with 1e-13 error in less than a second. > (Before when I said 2 or less seconds, it is obvious I didn't time them > really; but it just printed the result as soon as I pressed F5) With a smaller stepsize my rk4 integrator can get an error of 1e-15 (1e-13%) in 20 milliseconds: $ ./ode.py dt : 9.76562e-04 error : 2.83e-15 $ vim ode.py # This is to comment out the print line $ python -m timeit -s 'from ode import check_errors' 'check_errors()' 10 loops, best of 3: 20.5 msec per loop Oscar From cybervigilante at gmail.com Mon Aug 12 18:55:32 2013 From: cybervigilante at gmail.com (Jim Mooney) Date: Mon, 12 Aug 2013 09:55:32 -0700 Subject: [Tutor] Beginner question In-Reply-To: References: Message-ID: On 12 August 2013 02:14, Karim Liateni wrote: > 5?t5?6hhhyyyfrrtr > > eschneider92 at comcast.net a ?crit : > > >I've been learning python from the website 'inventwithpython.com', and > I'm on a chapter that covers the following code: > Just a quick note - not on the algorithm itself. If you run that in some IDEs, such as Wing101, all the time.sleep()s will concatenate, and all the prints will then print at once with no delay ;') If that happens, run it from the command line or try a different IDE. Jim -- "If you don't know it's impossible, it's easier to do." --Neil Gaiman "The Process is not the Picture...Reality can only be proved to be weakly-objective. Strong objectivity is a myth." --Bernardo Kastrup "You cannot use logic to justify logic, so logic itself has no basis other than faith." --Agrippa -------------- next part -------------- An HTML attachment was scrubbed... URL: From oscar.j.benjamin at gmail.com Mon Aug 12 22:11:21 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 12 Aug 2013 21:11:21 +0100 Subject: [Tutor] hi In-Reply-To: <000001ce9794$cc7a6390$656f2ab0$@orange.mu> References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> <000001ce9590$e1b667a0$a52336e0$@orange.mu> <000001ce9650$fe15e180$fa41a480$@orange.mu> <000601ce976e$33f0f830$9bd2e890$@orange.mu> <000001ce9794$cc7a6390$656f2ab0$@orange.mu> Message-ID: On 12 August 2013 20:47, Vick wrote: >> -----Original Message----- >> From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] >> Sent: Monday, 12 August, 2013 20:53 >> >> With a smaller stepsize my rk4 integrator can get an error of 1e-15 >> (1e-13%) in 20 milliseconds: >> >> $ ./ode.py >> dt : 9.76562e-04 error : 2.83e-15 > > [Vick] I have compared your rk4 code with mine and they are virtually the > same. It is the standard rk4 method. Using only the rk4 method the error is > 1e-4. However you seem to be getting different result due to the fact that > you have good programming skills as evidenced by the other defs in your code > and I really don't understand what the other defs are really doing. The > solveto and solve defs are certainly doing much to get your result. > > Just strip your code of all the superpowers it has got and leave just the > rk4 method to compute. How much error do you get then? ;) It's because I'm using double precision and you're using mpmath. The mpmath library slows your code down by several orders of magnitude for little gain in accuracy (since the dopri coefficients are not sufficiently accurate). There are several reasons that rk4 is so popular and one is that it pretty much has as high an order as is needed for working in double precision (in most cases). As I said before mpmath is massive overkill for the requested level of accuracy. On the other hand if you used a 10th (or higher) order Adams-Bashforth with mpmath you might start to see the gains from high order outweigh the costs from mpmath at *really* small error levels. Of course you have to ask yourself how much time you're prepared to spend coding up integrators to make the error smaller when you could really just use scipy's odeint and get on with plotting your results! Oscar From oscar.j.benjamin at gmail.com Mon Aug 12 23:00:42 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 12 Aug 2013 22:00:42 +0100 Subject: [Tutor] hi In-Reply-To: <000101ce979b$1d17ff00$5747fd00$@orange.mu> References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> <000001ce9590$e1b667a0$a52336e0$@orange.mu> <000001ce9650$fe15e180$fa41a480$@orange.mu> <000701ce9771$187e6bc0$497b4340$@orange.mu> <000101ce979b$1d17ff00$5747fd00$@orange.mu> Message-ID: On 12 August 2013 21:32, Vick wrote: >> From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] >> Sent: Monday, 12 August, 2013 20:42 > >> Are you able to access this paper (link at the bottom)? > > [Vick] yes. But the formula seems unfamiliar to me. And I really can't > understand what with the compact writing and small letters. Yeah, I'm not old enough to have seen the days when people wrote equations with typewriters so it looks strange to me as well. >> >> I believe this is the original source for the coefficients you are using. > They >> describe constructing a system of linear rational equations for the >> coefficients of the method. I think that the equations must be satisfied > for >> any 13-stage explicit RK method that has an 8th-order with embedded 7th- >> order method. Solving the equations leads to a solution space with 10 >> degrees of freedom. > > [Vick] You are telling only half the story...it looks like we have to write > up over 230 equations or so. Well you don't actually have to write them out by hand. You would use loops in a script to compute the coefficients for the equations. But yes it seems like too much work for me right now and I don't think it would really help you that much so I wouldn't bother. >> Much as I would love to see those coefficients, for your own sake it would > be >> a lot easier to just use an Adams-Bashforth integrator. >> Here's a stripped down script that can compute the exact coefficients for > any >> arbitrary order: >> > [Vick] It looks as though your code is using some form of polynomial and if > I'm not mistaken is it the legendre polynomial? Yes, that's my own code for doing algebra with polynomials. You can see the explanation for using polynomials to get the coefficients here: http://en.wikipedia.org/wiki/Linear_multistep_method#Adams.E2.80.93Bashforth_methods > I'm not sure what your code is really doing as I'm not an expert programmer. > What I need is to get an explanation with example of what some things are > supposed to be doing and then if I have understood correctly I will try to > code it on my own using my own coding capabilities. I think it's better if I > start work with the standard rk4 method as it has the same properties as the > dopri8. You make heavy use of scipy and numpy and the other modules in > python, so I guess you are a seasoned programmer in python so we are really > not on the same page with regard to understanding coding. My code shows that > I have done the rk4 and dopri8 from scratch without using the modules in > python except of course for mpmath. It would do me no good if I have to use > a function in coding which I really don't understand. It depends what you mean by "don't understand". Truthfully I don't know how scipy's odeint works. I have a general understanding of how integration methods work and I know that it does a good job. The important thing is not so much that you understand all of the code in question but that you know how to check the accuracy of the results yourself. > It seems that I got you interested in DOPRI8 after all. You said you have a > collection of integrators. What are they, just to compare notes! Actually > the best or most efficient and most accurate I know about for ODEs is the > Gauss Jackson 8th order method (GJ8), but I don't have it. The second most > powerful is the RKN12 this is the runge kutta Nystrom 12th order but it is > useful only for 2nd order ODEs and as I haven't any use for it I haven't > completed its coding, but I do have its rational coefficients. Then the > third most efficient is the DOPRI8(7)13, the one you just got interested in. > With regard to integration (area under a curve) the best I know is the > tanh-sinh quadrature and I have it coded. Anyway it is also built in mpmath. > The second best is Gauss-Konrod but I don't have it and the third one is the > Gauss-legendre which I have also. And then there are the lesser orders like > Boole rule, Simpson's rule etc, even in their adaptive forms. As I've said before, comparisons between different integrators are actually quite subjective. The most significant difference between the methods you have just described is between the stable implicit methods for stiff problems and unstable explicit methods for non-stiff problems. That difference is fundamental. Otherwise the arguments about which is better are, as I said before, implicitly tied to certain assumptions about how you write your code. The fact that (as shown by decimal rk4 script) the simplest methods can achieve arbitrarily small errors (if you're prepared to wait for the output) means that there is no clear definition of the "best integrator". In fact one strong assumption that is often made implicitly is that you're using something like C or Fortran and that you're definitely not using Python! Since you've already broken that assumption you can't rely on the common wisdom about which integrator better than which other. > Well just send me some tutorial on how to build and obtain the coefficients > for the butcher tableau for the RK4 as an example, and after I've mastered > it, I'd give the dopri8 a shot. I am up for it so I'll see if I can find time to write a script that shows how to do it. Oscar From oscar.j.benjamin at gmail.com Mon Aug 12 23:07:31 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 12 Aug 2013 22:07:31 +0100 Subject: [Tutor] hi In-Reply-To: <000201ce979c$59ecda30$0dc68e90$@orange.mu> References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> <000001ce9590$e1b667a0$a52336e0$@orange.mu> <000001ce9650$fe15e180$fa41a480$@orange.mu> <000601ce976e$33f0f830$9bd2e890$@orange.mu> <000001ce9794$cc7a6390$656f2ab0$@orange.mu> <000201ce979c$59ecda30$0dc68e90$@orange.mu> Message-ID: On 12 August 2013 21:41, Vick wrote: >> From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] >> >> It's because I'm using double precision and you're using mpmath. > > [Vick] Where is the call for double precision in your code? > Is it built in with scipy or maybe odeint? When you write from math import exp a = 1.0 b = exp(a) you're working with Python float objects which are implemented in 64 bit double precision. These are very efficient since the computations are accelerated in the FPU. When you write from mpmath import mpf, exp a = mpf('1') b = exp(a) your working with Multi-Precision Floats from the mpmath library. This is implemented in pure Python and runs a lot slower (it'll be faster if you have gmpy installed but still much slower than the FPU). Try writing a simple script that computes something using floats or using mpmath and you will notice the speed difference very quickly. The same thing happens when using the decimal module (although it is *much* faster in Python 3.3). Oscar From alan.gauld at btinternet.com Mon Aug 12 23:11:49 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 12 Aug 2013 22:11:49 +0100 Subject: [Tutor] Python Programming for the absolute beginner 3e, Ch3 Challenge 4 In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47418650D5D@SCACMX008.exchad.jpmchase.net> References: <1376035867.52727.YahooMailNeo@web186005.mail.ir2.yahoo.com> <1376132697.65005.YahooMailNeo@web186002.mail.ir2.yahoo.com> <5B80DD153D7D744689F57F4FB69AF47418650D5D@SCACMX008.exchad.jpmchase.net> Message-ID: On 12/08/13 16:20, Prasad, Ramit wrote: >> A binary chop is an algorithm. > Also known as Binary search; binary chop was not a name I had > heard before either. Maybe a UK thing. It means chop as in Karate not as in Lamb... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From engg at cleantechsolution.in Mon Aug 12 12:04:58 2013 From: engg at cleantechsolution.in (Engineering) Date: Mon, 12 Aug 2013 15:34:58 +0530 Subject: [Tutor] Python & Arduino Message-ID: <000501ce9743$6ddac560$49905020$@in> Dear All I have designed a small program to control an arduino through python socket server. The front end is the Javaquery. My HTML code is given below $(function() { $( "#slider" ).slider({ min: 0, max: 180, step: 10, slide: function( event, ui ) { $( "#amount" ).val( "$" + ui.value ); } }); $( "#amount" ).val( "$" + $( "#slider" ).slider( "value" ) ); }); My python websocket code is given below from twisted.internet import reactor print "Using Twisted reactor", reactor.__class__ print from twisted.python import usage, log from twisted.protocols.basic import LineReceiver from twisted.internet.serialport import SerialPort from twisted.web.server import Site from twisted.web.static import File from autobahn.websocket import listenWS from autobahn.wamp import WampServerFactory, WampServerProtocol, exportRpc class Serial2WsOptions(usage.Options): optParameters = [ ['baudrate', 'b', 9600, 'Serial baudrate'], ['port', 'p', 26, 'Serial port to use'], ['webport', 'w', 8080, 'Web port to use for embedded Web server'], ['wsurl', 's', "ws://localhost:9000", 'WebSocket port to use for embedded WebSocket server'] ] Can you please help me with the python code . I want the server to write string which will be sent to the arduino digital pin to control the servo. The arduino can have the following in its code if(!Serial.available()) { // convert String to int. int recievedVal = stringToInt(); //analogWrite(servoPin,recievedVal); recievedVal = map(recievedVal,0,179,0,179); webcam.write(recievedVal); delay(15); pwmComplete = false; } Python side coding help is required CLEANTECH SOLUTION www.cleantechsolution.in SAVE PAPER , SAVE EARTH -------------- next part -------------- An HTML attachment was scrubbed... URL: From i.am.songoku at gmail.com Mon Aug 12 19:31:38 2013 From: i.am.songoku at gmail.com (Krishnan Shankar) Date: Mon, 12 Aug 2013 23:01:38 +0530 Subject: [Tutor] Beginner question In-Reply-To: References: Message-ID: >def checkCave(chosenCave): > print('You approach the cave...') > time.sleep(2) > print('It is dark and spooky...') > time.sleep(2) > print('A large dragon jumps out in front of you! He opens his jaws and...') > print() > time.sleep(2) > friendlyCave = random.randint(1, 2) > if chosenCave == str(friendlyCave): > print('Gives you his treasure!') > else: > print('Gobbles you down in one bite!') >playAgain = 'yes' >while playAgain == 'yes' or playAgain == 'y': > displayIntro() > caveNumber = chooseCave() > checkCave(caveNumber) > print('Do you want to play again? (yes or no)') > playAgain = input() Hi, - Here we are passing the chosen integer (1 or 2) got from chooseCave() method to checkCave as arguement - When called in while loop inside checkCave the following happens; - The statements of approaching the cave and seeing the dragon are printed with a time interval of 2 secs between each - Randomly either 1 or 2 is generated by the randint() method of random module in python. - That randomly generated integer (1 0r 2) is compared with our integer input (1 or 2) - If they match dragon gives us gold. Or else - We will be eaten by dragon :) But in the code there is a flaw. input() will evaluate your user input. i.e. If you give an integer expression it will tell the answer. And when you provide a number it will take it as int type. See below. >>> var = input() 1+2+3 >>> var 6 >>> var = input() 2 >>> type(var) >>> var = input() s Traceback (most recent call last): File "", line 1, in File "", line 1, in NameError: name 's' is not defined >>> var = input() So since the integer number is checked with string in Line 13, it will run into infinite loop. If you use raw_input() instead of input() you will be able to run the example. Regards, Krishnan On Mon, Aug 12, 2013 at 10:25 PM, Jim Mooney wrote: > On 12 August 2013 02:14, Karim Liateni wrote: > >> 5?t5?6hhhyyyfrrtr >> >> eschneider92 at comcast.net a ?crit : >> >> >I've been learning python from the website 'inventwithpython.com', and >> I'm on a chapter that covers the following code: >> > > Just a quick note - not on the algorithm itself. If you run that in some > IDEs, such as Wing101, all the time.sleep()s will concatenate, and all the > prints will then print at once with no delay ;') If that happens, run it > from the command line or try a different IDE. > > Jim > -- > > "If you don't know it's impossible, it's easier to do." --Neil Gaiman > "The Process is not the Picture...Reality can only be proved to be > weakly-objective. Strong objectivity is a myth." --Bernardo Kastrup > "You cannot use logic to justify logic, so logic itself has no basis other > than faith." --Agrippa > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From i.am.songoku at gmail.com Mon Aug 12 19:53:44 2013 From: i.am.songoku at gmail.com (Krishnan Shankar) Date: Mon, 12 Aug 2013 23:23:44 +0530 Subject: [Tutor] Beginner question In-Reply-To: References: Message-ID: >But in the code there is a flaw. input() will evaluate your user input. i.e. If you give an integer >expression it will tell the answer. And when you provide a number it will take it as int type. See >below. Hi, Ignore my above statements if using Python 3. Sorry my bad. Had a doubt and went to the site to see the version of python used. My statement above is correct only if run in Python 2 and not in 3. Regards, Krishnan On Mon, Aug 12, 2013 at 11:01 PM, Krishnan Shankar wrote: > >def checkCave(chosenCave): > > print('You approach the cave...') > > time.sleep(2) > > print('It is dark and spooky...') > > time.sleep(2) > > print('A large dragon jumps out in front of you! He opens his jaws > and...') > > print() > > time.sleep(2) > > friendlyCave = random.randint(1, 2) > > if chosenCave == str(friendlyCave): > > print('Gives you his treasure!') > > else: > > print('Gobbles you down in one bite!') > >playAgain = 'yes' > >while playAgain == 'yes' or playAgain == 'y': > > displayIntro() > > caveNumber = chooseCave() > > checkCave(caveNumber) > > print('Do you want to play again? (yes or no)') > > playAgain = input() > > Hi, > > - Here we are passing the chosen integer (1 or 2) got from chooseCave() > method to checkCave as arguement > - When called in while loop inside checkCave the following happens; > - The statements of approaching the cave and seeing the dragon are > printed with a time interval of 2 secs between each > - Randomly either 1 or 2 is generated by the randint() method of > random module in python. > - That randomly generated integer (1 0r 2) is compared with our > integer input (1 or 2) > - If they match dragon gives us gold. Or else > - We will be eaten by dragon :) > > But in the code there is a flaw. input() will evaluate your user input. > i.e. If you give an integer expression it will tell the answer. And when > you provide a number it will take it as int type. See below. > > >>> var = input() > 1+2+3 > >>> var > 6 > >>> var = input() > 2 > >>> type(var) > > >>> var = input() > s > Traceback (most recent call last): > File "", line 1, in > File "", line 1, in > NameError: name 's' is not defined > >>> var = input() > > So since the integer number is checked with string in Line 13, it will run > into infinite loop. If you use raw_input() instead of input() you will be > able to run the example. > > Regards, > Krishnan > > > On Mon, Aug 12, 2013 at 10:25 PM, Jim Mooney wrote: > >> On 12 August 2013 02:14, Karim Liateni wrote: >> >>> 5?t5?6hhhyyyfrrtr >>> >>> eschneider92 at comcast.net a ?crit : >>> >>> >I've been learning python from the website 'inventwithpython.com', and >>> I'm on a chapter that covers the following code: >>> >> >> Just a quick note - not on the algorithm itself. If you run that in some >> IDEs, such as Wing101, all the time.sleep()s will concatenate, and all the >> prints will then print at once with no delay ;') If that happens, run it >> from the command line or try a different IDE. >> >> Jim >> -- >> >> "If you don't know it's impossible, it's easier to do." --Neil Gaiman >> "The Process is not the Picture...Reality can only be proved to be >> weakly-objective. Strong objectivity is a myth." --Bernardo Kastrup >> "You cannot use logic to justify logic, so logic itself has no basis >> other than faith." --Agrippa >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > -------------- next part -------------- An HTML attachment was scrubbed... URL: From chris at chrisdown.name Mon Aug 12 23:19:09 2013 From: chris at chrisdown.name (Chris Down) Date: Mon, 12 Aug 2013 23:19:09 +0200 Subject: [Tutor] [Python-Help] Instancing class issue In-Reply-To: References: Message-ID: <20130812211908.GC2927@gopher> Hi Dino, On 2013-08-12 20:32, Dino Bekte?evi? wrote: > def __init__(self, **keys): > from . import util > > self.keys=keys > self.load() > > where I don't understand what **keys mean, I've only seen that as **kwargs > meaning other key words and arguments in examples. The name of the variable doesn't matter, that's still what it does; you can think of it as encapsulating any other keyword arguments. >>> def foo(bar, **kwargs): ... print("bar: %s" % (bar,)) ... print("kwargs: %r" % (kwargs,)) ... >>> foo("bar", baz="qux", wibble="wobble") bar: bar kwargs: {'baz': 'qux', 'wibble': 'wobble'} > When I try to instance > Astrom class by: > new=Astrom() > I receive a following error: Sorry, no idea about this bit, I've never used sdsspy. est of luck sorting this out. Chris -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From ljetibo at gmail.com Mon Aug 12 20:32:37 2013 From: ljetibo at gmail.com (=?ISO-8859-2?Q?Dino_Bekte=B9evi=E6?=) Date: Mon, 12 Aug 2013 20:32:37 +0200 Subject: [Tutor] Instancing class issue Message-ID: Hello, I have an unusual problem when I try to instance a class Astrom from a set of tools for astronomy SDSSPY. Here's the link to the project page: http://code.google.com/p/sdsspy/ and this is the class I'm having problems instancing: http://code.google.com/p/sdsspy/source/browse/sdsspy/astrom.py I'm running LucidLynx ("You are using Ubuntu 10.04 LTS released in April 2010 and supported until April 2013.") on a Oracle VirtualBox v4.2.12 and I'm using Python 2.6.5. (reason being that I'm also importing various other modules like esutil, scipy, numpy of which some don't work properly on never version, or so I have been informed). "Home" OS is win7 64bit. I'm not new to programming but don't consider me an expert, or even amateur in python, most about python I learned from diveintopython and other random sources I used as I got further into it solving problems. Ok now onto the problem itself: There's a method pix2eq in the class Astrom I wish to use. However because it's inside a class obviously I need to instance it because you can only use methods on class instances. When I read the Astrom class definition there clearly states: def __init__(self, **keys): from . import util self.keys=keys self.load() where I don't understand what **keys mean, I've only seen that as **kwargs meaning other key words and arguments in examples. When I try to instance Astrom class by: new=Astrom() I receive a following error: Traceback (most recent call last): File "", line 1, in novi=Astrom() File "/usr/local/lib/python2.6/dist-packages/sdsspy/astrom.py", line 44, in __init__ self.load() File "/usr/local/lib/python2.6/dist-packages/sdsspy/astrom.py", line 266, in load raise ValueError("send run= and camcol=") ValueError: send run= and camcol= which seems pretty straight forward except that I don't see it requiring me to do so anywhere and that when I try to follow instructions: new = Astrom(run=2888, camcol=1) (both run 002888 and camcol 1 exist I have the images I need downloaded on my drive already) I recieve error about my enviroment: Traceback (most recent call last): File "", line 1, in novi = Astrom (run=2888, camcol=1) File "/usr/local/lib/python2.6/dist-packages/sdsspy/astrom.py", line 44, in __init__ self.load() File "/usr/local/lib/python2.6/dist-packages/sdsspy/astrom.py", line 270, in load fname=files.filename('photoField', **keys) File "/usr/local/lib/python2.6/dist-packages/sdsspy/files.py", line 274, in filename return fs.filename(ftype, run, camcol, field, **keys) File "/usr/local/lib/python2.6/dist-packages/sdsspy/files.py", line 479, in filename f = expand_sdssvars(f, run=run, camcol=camcol, field=field, **keys) File "/usr/local/lib/python2.6/dist-packages/sdsspy/files.py", line 870, in expand_sdssvars raise ValueError(err) ValueError: There were unexpanded variables: '$PHOTO_REDUX/runList.par', there may be no such run but you might try sending the rerun except search doesn't pop-out any runList.par files anywhere. Is it something I'm doing wrong or is it something I should contact the author Mr. Sheldon personally? Any comment about the class is appreciated, including the Astrom(object) type inheritance. Help is greatly appreciated, Dino Bekte?evi? -------------- next part -------------- An HTML attachment was scrubbed... URL: From vick1975 at orange.mu Mon Aug 12 17:11:21 2013 From: vick1975 at orange.mu (Vick) Date: Mon, 12 Aug 2013 19:11:21 +0400 Subject: [Tutor] hi In-Reply-To: References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> <000001ce9590$e1b667a0$a52336e0$@orange.mu> <000001ce9650$fe15e180$fa41a480$@orange.mu> Message-ID: <000601ce976e$33f0f830$9bd2e890$@orange.mu> > -----Original Message----- > From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] > Sent: Monday, 12 August, 2013 16:08 > To: Vick > > > Maybe. It still depends. If you use a higher-order method where it's just not > needed you can end up wasting a lot of CPU-cycles. The same goes for using > multi-precision arithmetic when you could happily work in double precision. [Vick] Well I'm using the higher order method for n-body problem. The problem has four 1st order ODEs to solve for one body; two trivial and two very significant ones. So for n-body problem you would need (n x 4) 1st order ODEs to solve. The equation is set for 2-D coordinates x and y as follows for a 2-body problem example: dxdt(3) = vx1 dxdt(4) = vy1 dxdt(1) = ((G*m2)/(((x1-x2)^2 + (y1-y2)^2)^(3/2)))*(x2-x1) dxdt(2) = ((G*m2)/(((x1-x2)^2 + (y1-y2)^2)^(3/2)))*(y2-y1) dxdt(5) = vx2 dxdt(6) = vy2 dxdt(7) = ((G * m1) / (((x1 - x2) ^ 2 + (y1 -y2) ^ 2) ^ (3 / 2))) * (x1 - x2) dxdt(8) = ((G * m1) / (((x1 - x2) ^ 2 + (y1 - y2) ^ 2) ^ (3 / 2))) * (y1 - y2) >The second is slow but achieves absurdly high levels of accuracy. It > gives better accuracy than I got with your rungkdp8 (see below) [Vick] That's the whole point! I had to wait for about 40 minutes to get very high accuracy which the method was not designed to obtain. Whereas the DOPRI8 got the result with 1e-13 error in less than a second. (Before when I said 2 or less seconds, it is obvious I didn't time them really; but it just printed the result as soon as I pressed F5) > > In that amount of time, the memory was taxed a lot. > > I don't think you really know what you're talking about on this subject. The > algorithm works with a constant, very small, amount of memory regardless of > stepsize. In fact the rungkdp8 method uses more memory, however, the > difference is entirely insignificant for such a low-dimensional system. I think > you're confusing the fact that the way > *you* have implemented the algorithm you store every intermediate step in > the computation. My code only stores the desired output and the current > value of the intermediate results. [Vick] Well yeah, maybe there is a confusion on my use of computer memory. It is obvious that the Runge Kutta 4th order method has only 4 steps and that it is using very small numbers to compute and whereas the DOPRI8 has 13 steps in which it has to use large numbers to compute. Yes the DOPRI8 uses more memory than the RK4 in that regard. However the computing speed and the computer memory the code needs to operate on the algorithm is excessive with your code as it has to run for 10^(2-6). > > For the same > > ODE problem, RK4 should produce an error of 1e-4 in a reasonable > > amount of time (about 2 secs) and utilizing normal computer memory for > > its calculations. > > If you modify the check_errors() function in the first script I posted so that it > looks like this: > Then you can run it and get the following: > > $ time ./ode.py > dt : 7.53390e+01 error : 3.28e-07 > > real 0m0.094s > user 0m0.030s > sys 0m0.046s > > In other words it takes 90 milliseconds to run the script and get an error less > than 1e-6 (or 1e-4% if you prefer). Actually it really just takes 90 milliseconds > to *initialise* the script. We can use timeit to check how long the actual > computation takes (with the print line commented out): [Vick] That's just it. The error margin of RK4 in that instance is just 1e-4 in as you say 90 msec but for about the same amount of time DOPRI8 gets 1e-13. > It takes 168 microseconds to achieve the accuracy you requested, for your > test problem, using my rk4 integrator. Scipy's odeint is probably faster (I'll let > you do the timing if you want to know but bear in mind that it takes a > significant amount of time to *import* scipy so you should use timeit for > benchmarking). [Vick] What I meant was that I don't need to wait for 40,30...10,5 or even 1 minute to get the result. I am not actually measuring the real speed of each computation by each method. With human perception in view, all I'm saying is that for less than a second which is a reasonable "waiting" time for a human computing a running animation of the n-body problem, the error gotten by each method just demonstrates the superiority of the one over the other. It has been tested anyway. The DOPRI8 is an 8th order method whereas the RK4 is a 4th order. For the same amount of time, the latter can offer only 1e-4 of error for that test problem whereas DOPRI8 offers 1e-13. > > As I mentioned before I used this problem as a test for accuracy to > > compare different methods. I use percentage change (x 100) to derive > > the error for each method. > > I don't understand why you insist on defying convention by using > percentages. Really your errors should be so small that percent is a > meaningless unit of measure. Fractional errors are useful because you can > easily relate them to absolute errors without the pointless cognitive burden > of a factor of 100 getting in the way. [Vick] I don't understand! Normally if a got 12 and b got 12.5, I want to know by how much a is different from b. So the normal arithmetic if I wanted the percentage would be: 12*100/12.5 = 96% so the error is 100%-96% = 4%.......doing so with my error calculation we take abs(12.5-12)/12.5 * 100 = 4 whereas with your error calculation it gives (12.5-12)/12.5 = 0.04 . The percentage is 4% and the fraction of that error is 4/100 = 0.04 Both are valid, and anyway, it is really no big deal as both calculations are distant by 2 digits only. > > RK4 in this instance scores about 1e-4 accuracy. With this problem > > I've tested RKF5, DPRK45, some 6 and 7 order RK methods and the last > > one is the DOPRI8(7)13. > > Are you testing all the methods with the same step-sizes? If so that's not a > fair comparison since they do different amounts of work within each step. > [Vick] That's the whole point for one method to be better than the other in that for the same amount of time and for the same test problem, in one single step of their algorithms they are achieving different accuracies. > > I would report a comparison of timings between this code and my decimal > rk4 but I think it would be unfair as I think there may be something wrong > with the way your's is implemented. Unless I've misunderstood something > it's not achieving the proper benefits of the mpmath library since for some > reason the error bottoms out at 1e-18. I replaced the bottom of your script > with this: > > > When I run that I get: > > $ ./vick.py > dt : 1.0e+00 error : 1.1e-09 > dt : 1.0e-01 error : 9.4e-18 > dt : 1.0e-02 error : 2.3e-18 > dt : 1.0e-03 error : 2.3e-18 > > Do you get the same (it could be a difference in the underlying library etc.)? > [Vick] yeah I get the same results. The mpf is here to give me more digits to compute. I think Python would normally give up to 6 digits. When I first started out with Python I had trouble getting very long decimals. But when I started to use mpmath, all my problems were solved. So I think it's a habit to get mpmath involve in any code I make in python which involves scientific or advanced math calculations. If you increase the dps level the number of decimals the code will print will become greater. Vick From vick1975 at orange.mu Mon Aug 12 17:32:03 2013 From: vick1975 at orange.mu (Vick) Date: Mon, 12 Aug 2013 19:32:03 +0400 Subject: [Tutor] hi In-Reply-To: References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> <000001ce9590$e1b667a0$a52336e0$@orange.mu> <000001ce9650$fe15e180$fa41a480$@orange.mu> Message-ID: <000701ce9771$187e6bc0$497b4340$@orange.mu> > -----Original Message----- > From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] > Sent: Monday, 12 August, 2013 18:18 > > On 12 August 2013 13:08, Oscar Benjamin > wrote: > The rational approximations are intended to > be good enough for use with double precision but are insufficient for the 30 > dps setting that you are using. The reason given by the authors for publishing > approximate coefficients is that (in 1981) it was deemed computationally > intractable to compute the exact rational coefficients. However that may not > still be the case with modern computing power. I doubt that I'll attempt this > any time soon but if you're able (and bothered) to do that I'd love to know > what the coefficients would be. > [Vick] Yeah they are rational coefficients and I got them from the google book satellites orbits which I believe I posted the links for it previously. The problem is how are the coefficients derived? I've never seen an algorithm to compute the coefficients of an integrator method. Anyway it would surely involve complicated coding to start an algorithm to compute them. If I can get it somewhere, I wouldn't mind doing the DOPRI8 computations. Vick From vick1975 at orange.mu Mon Aug 12 21:47:37 2013 From: vick1975 at orange.mu (Vick) Date: Mon, 12 Aug 2013 23:47:37 +0400 Subject: [Tutor] hi In-Reply-To: References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> <000001ce9590$e1b667a0$a52336e0$@orange.mu> <000001ce9650$fe15e180$fa41a480$@orange.mu> <000601ce976e$33f0f830$9bd2e890$@orange.mu> Message-ID: <000001ce9794$cc7a6390$656f2ab0$@orange.mu> > -----Original Message----- > From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] > Sent: Monday, 12 August, 2013 20:53 > > With a smaller stepsize my rk4 integrator can get an error of 1e-15 > (1e-13%) in 20 milliseconds: > > $ ./ode.py > dt : 9.76562e-04 error : 2.83e-15 [Vick] I have compared your rk4 code with mine and they are virtually the same. It is the standard rk4 method. Using only the rk4 method the error is 1e-4. However you seem to be getting different result due to the fact that you have good programming skills as evidenced by the other defs in your code and I really don't understand what the other defs are really doing. The solveto and solve defs are certainly doing much to get your result. Just strip your code of all the superpowers it has got and leave just the rk4 method to compute. How much error do you get then? ;) From vick1975 at orange.mu Mon Aug 12 22:32:50 2013 From: vick1975 at orange.mu (Vick) Date: Tue, 13 Aug 2013 00:32:50 +0400 Subject: [Tutor] hi In-Reply-To: References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> <000001ce9590$e1b667a0$a52336e0$@orange.mu> <000001ce9650$fe15e180$fa41a480$@orange.mu> <000701ce9771$187e6bc0$497b4340$@orange.mu> Message-ID: <000101ce979b$1d17ff00$5747fd00$@orange.mu> > -----Original Message----- > From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] > Sent: Monday, 12 August, 2013 20:42 > Are you able to access this paper (link at the bottom)? > [Vick] yes. But the formula seems unfamiliar to me. And I really can't understand what with the compact writing and small letters. > > I believe this is the original source for the coefficients you are using. They > describe constructing a system of linear rational equations for the > coefficients of the method. I think that the equations must be satisfied for > any 13-stage explicit RK method that has an 8th-order with embedded 7th- > order method. Solving the equations leads to a solution space with 10 > degrees of freedom. [Vick] You are telling only half the story...it looks like we have to write up over 230 equations or so. > > As a simpler example, imagine constructing the 4th order RK method which > has Butcher table: > > 0 | > 1/2 | 1/2 > 1/2 | 0 1/2 > 1 | 0 0 1 > ----|---------------- > | 1/6 1/3 1/3 1/6 > [Vick] Yeah I know about the butcher table, I've actually coded my copy of DOPRI8 from its butcher tableau. However I still don't understand how the coefficients are derived. > > This gives us 8 equations for the 15 coefficients: > > a1 | > a2 | b21 > a3 | b31 b32 > a4 | b41 b42 b43 > ---|----------------- > | c1 c2 c3 c4 > > > Much as I would love to see those coefficients, for your own sake it would be > a lot easier to just use an Adams-Bashforth integrator. > Here's a stripped down script that can compute the exact coefficients for any > arbitrary order: > [Vick] It looks as though your code is using some form of polynomial and if I'm not mistaken is it the legendre polynomial? I'm not sure what your code is really doing as I'm not an expert programmer. What I need is to get an explanation with example of what some things are supposed to be doing and then if I have understood correctly I will try to code it on my own using my own coding capabilities. I think it's better if I start work with the standard rk4 method as it has the same properties as the dopri8. You make heavy use of scipy and numpy and the other modules in python, so I guess you are a seasoned programmer in python so we are really not on the same page with regard to understanding coding. My code shows that I have done the rk4 and dopri8 from scratch without using the modules in python except of course for mpmath. It would do me no good if I have to use a function in coding which I really don't understand. It seems that I got you interested in DOPRI8 after all. You said you have a collection of integrators. What are they, just to compare notes! Actually the best or most efficient and most accurate I know about for ODEs is the Gauss Jackson 8th order method (GJ8), but I don't have it. The second most powerful is the RKN12 this is the runge kutta Nystrom 12th order but it is useful only for 2nd order ODEs and as I haven't any use for it I haven't completed its coding, but I do have its rational coefficients. Then the third most efficient is the DOPRI8(7)13, the one you just got interested in. With regard to integration (area under a curve) the best I know is the tanh-sinh quadrature and I have it coded. Anyway it is also built in mpmath. The second best is Gauss-Konrod but I don't have it and the third one is the Gauss-legendre which I have also. And then there are the lesser orders like Boole rule, Simpson's rule etc, even in their adaptive forms. Well just send me some tutorial on how to build and obtain the coefficients for the butcher tableau for the RK4 as an example, and after I've mastered it, I'd give the dopri8 a shot. Vick From vick1975 at orange.mu Mon Aug 12 22:41:41 2013 From: vick1975 at orange.mu (Vick) Date: Tue, 13 Aug 2013 00:41:41 +0400 Subject: [Tutor] hi In-Reply-To: References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> <000001ce9590$e1b667a0$a52336e0$@orange.mu> <000001ce9650$fe15e180$fa41a480$@orange.mu> <000601ce976e$33f0f830$9bd2e890$@orange.mu> <000001ce9794$cc7a6390$656f2ab0$@orange.mu> Message-ID: <000201ce979c$59ecda30$0dc68e90$@orange.mu> > -----Original Message----- > From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] > Sent: Tuesday, 13 August, 2013 00:11 > To: Vick > > It's because I'm using double precision and you're using mpmath. [Vick] Where is the call for double precision in your code? Is it built in with scipy or maybe odeint? > Of course you have to ask yourself how much time you're prepared to spend > coding up integrators to make the error smaller when you could really just > use scipy's odeint and get on with plotting your results! [Vick] I've always wanted to code things up on my own without having recourse to some modules except when I really cannot do without it. Anyway I also use excel VBA to code in excel, so having the codes in a way I understand them means that I can translate it either to python or to excel VBA. What type of calculations do you do with your integrators? From zack.hasanov at gmail.com Mon Aug 12 02:52:34 2013 From: zack.hasanov at gmail.com (Zack Hasanov) Date: Sun, 11 Aug 2013 20:52:34 -0400 Subject: [Tutor] Python Programming for the Absolute Beginner - Chap 7 Q: 2 Message-ID: <00ff01ce96f6$3ccd93f0$b668bbd0$@gmail.com> Hello, I am a python newbie. I am reading this book (Python Programming for the Absolute Beginner). I am on Chapter 7, Question 2. "Improve the Trivia Challenge game so that it maintains a high-scores list in a file. The program should record the player's name and score. Store the high scores using a pickled object." I have the following code so far: def high_score(): """Records a player's score""" high_scores = [] #add a score // Do current stuff for adding a new score... name = input("What is your name? ") player_score = int(input("What is your score? ")) entry = (name, player_score) high_scores.append(entry) high_scores.sort(reverse=True) high_scores = high_scores[:5] # keep only top five # dump scores f = open("pickles1.dat", "wb") pickle.dump(high_scores, f) f.close() f = open("pickles1.dat", "rb") high_scores = pickle.load(f) print(high_scores) f.close() When I execute this program in the main() program I get only the existing single name, player_score list combination stored in the pickles1.dat file. Can someone walk me through how it can store all the values each time the program is ran? Thanks, Zack H. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Aug 13 01:20:58 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 13 Aug 2013 00:20:58 +0100 Subject: [Tutor] Python Programming for the Absolute Beginner - Chap 7 Q: 2 In-Reply-To: <00ff01ce96f6$3ccd93f0$b668bbd0$@gmail.com> References: <00ff01ce96f6$3ccd93f0$b668bbd0$@gmail.com> Message-ID: On 12/08/13 01:52, Zack Hasanov wrote: > I have the following code so far: > > def high_score(): > high_scores = [] > > name = input("What is your name? ") > player_score = int(input("What is your score? ")) > entry = (name, player_score) > high_scores.append(entry) > high_scores.sort(reverse=True) > high_scores = high_scores[:5] # keep only top five Note that you never read the stored values from your pickle file you just add a single value. > > # dump scores > f = open("pickles1.dat", "wb") > pickle.dump(high_scores, f) > f.close() This then dumps that single value to the file overwriting anything that might have already been there. > f = open("pickles1.dat", "rb") > high_scores = pickle.load(f) > print(high_scores) You then load the new value and print it. > When I execute this program in the main() program I get only the > existing single name, player_score list combination stored in the > pickles1.dat file. I have no idea how you call this. Presumably from within a loop? But because you never read in the stored values before writing the new you only ever get the last value stored. You need to insert code like your last block above at the top of the function to pre-populate high_scores before adding the new entry. Once you get that working we can look at tidying up some of the unnecessary code that will be left over, but lets get it working first! HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From sunithanc at gmail.com Tue Aug 13 05:33:48 2013 From: sunithanc at gmail.com (SM) Date: Mon, 12 Aug 2013 23:33:48 -0400 Subject: [Tutor] How to access a method defined in one class from another class (which is a thread) in Python3? In-Reply-To: References: Message-ID: Hi Alan, Thanks, very much, for your time answering my question. I ended up using global variables for all the attributes I had to share between the MainWindow class and the thread, and it worked. I am sure there are better alternatives, but since it was just a couple of attributes, the code looks ok. Some responses in line at [SM] On Sun, Aug 11, 2013 at 12:05 PM, Alan Gauld wrote: > On 09/08/13 16:50, SM wrote: > > Sorry I only just picked this up. > > > (ex: self.tab_fw = QtGui.QWidget(), self.tab_ann = QtGui.QWidget(), >> etc), its own textEdit window and its own progress bar widget. >> All the tabs are defined within this single class - they are not >> instances of the class, as the tabs are distinct from each other. All of >> that is working really well. >> > > OK, I'll assume the structure looks like > > MainGUIWindow > - Tab1 > - TextEdit1 > - ProgressBar1 > - any other widgets on tab1 > - Tab2 > - TextEdit2 > - ProgressBar2 > - any other widgets on tab2 > - Tab3 > - TextEdit3 > - ProgressBar3 > - any other widgets on tab3 > [SM] Yes > > And that both the TextEdit and progress bar widgets are part of > the Qt framework/library? So there are 3 distinct progress bar instances, > one per tab? [SM]: Yes, that is right. > > > an added feature, I am now asked to have a "progress bar" widget >> which appears when an operation is going on, to tell the user that >> he/she has to wait. >> > > Is this an extra (4th?) progress bar or is this the ones already described > as being in the tabs? > [SM] The latter. There are just 3 tabs and 3 progress bars > > Also, when you say "appears" do you mean its invisible until the operation > starts? Or is it visible but inactive (maybe greyed out?) [SM] Wrong choice of word on my part. I should have said, " widget which swings horizontally" > > > [SM]: Yes. I shouldn't say duplicated, but each tab has a different >> progress bar of its own. In the example, I was quoting the code for just >> on tab. The others will be implemented similarly. >> > > OK So I'm again going to assume: > > A total of 1 progress bar per tab. The "added feature" is what these bars > are addressing? [SM]: yes > > > So I am running two threads - first one is the progressbar >> widget which has a rectangular slab spinning sideways to >> indicate that the operation is going on. >> > > I usually expect a progress bar to indicate the percentage complete but > this sounds like its just a spinning eggshell activity indicator. Is that > correct? You have no progress value indication? [SM] Yes, it is more of a egg-whatever.... It just spins horizontally as opposed to showing the percentage of work done. That is part of the requirement. > > > The second thread is the one which is actually doing the operation. >> > > I have a flag that the second thread sets after the job is done > > OK, again I'll assume we are dealing with just an activity indicator not a > value display. [SM] yes > > > and stops spinning and gets out of the loop when the flag is set. >> I >> have that working as well. >> > > OK, Good the picture is building. > > > [SM]: I did start out with a timer. But due to my lack of experience >> with usage of timer in Python, the code I wrote was running sequentially >> > > That sounds like you tried to use a Python native timer. Normally in a GUI > the toolkit (Qt in this case) provides a timer event that triggers > via the normal event mechanism. You set the timer, the event arrives and > you do something then reset the timer for the next time round. > > I found this page that describes the Qt approach (which includes > multi-shot timers) and links to more detailed examples etc. > > http://qt-project.org/doc/qt-**4.7/timers.html#id-fd46b213-** > 376e-4636-a7d2-8ae899de1e11 > [SM] Thanks, very much, for the link. I will go over it. In my opinion, if the timer works, using it is less complicated than using threads. Thanks! -SM > > The code is in C++ but should translate easily to python and PyQt. > > HTH > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From amitsaha.in at gmail.com Tue Aug 13 17:59:29 2013 From: amitsaha.in at gmail.com (Amit Saha) Date: Wed, 14 Aug 2013 01:59:29 +1000 Subject: [Tutor] dbus.Array to string In-Reply-To: References: <520834B0.80704@pearwood.info> <520849BD.4030004@pearwood.info> Message-ID: On Mon, Aug 12, 2013 at 1:13 PM, eryksun wrote: > On Sun, Aug 11, 2013 at 10:34 PM, Steven D'Aprano wrote: >> On 12/08/13 11:40, eryksun wrote: >>> Typically strings should be unicode. If the byte sequence is Latin-1 >>> (including ASCII), you can map unichr() and join the characters with >>> u''.join(); that's equivalent to chr() and ''.join() in 3.x. More >>> generally, decode() the byte string. A simply way that works in 2.6+ >>> is to create a bytearray: >>> >>> >>> bytearray(ssid).decode('latin-1') >>> u'BigPond679D85' >> >> But, what makes you think that the encoding will be latin-1? If I had to >> guess an encoding, I'd guess UTF-8. > > Unicode ordinals in the 8-bit range are Latin-1 (aka ISO-8859-1, i.e. > Basic Latin and Latin-1 Supplement, including C0 and C1 control > codes). You can always decode as Latin-1 without getting a decoding > error. UTF-8 is multibyte and defaults to raising a decoding error if > the sequence isn't valid. > > It's up to Amit to figure out what the encoding is. It's D-Bus, so > probably Linux. In that case, UTF-8 is a good guess. Nice, thanks. What does it mean (and will it always work?) when I don't specify any encoding: >>> bytearray(ssid).decode() u'BigPond679D85' The default encoding (on Python 2) is ascii: >>> import sys >>> sys.getdefaultencoding() 'ascii' Thanks, Amit. -- http://echorand.me From JATINSHR001 at e.ntu.edu.sg Tue Aug 13 18:45:55 2013 From: JATINSHR001 at e.ntu.edu.sg (#PATHANGI JANARDHANAN JATINSHRAVAN#) Date: Tue, 13 Aug 2013 16:45:55 +0000 Subject: [Tutor] To find the least number divisible by all numbers from 1-20 Message-ID: <52B2907AE37EB94B8690A0B45EE20918C9BD8E@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> Hello All, Sorry for the earlier mail without subject. I was in a hurry so I missed that I am solving problem number 5 in project euler. I think my solution seems logically correct but it is not giving an answer as it is taking too long to execute. So can someone suggest an alternative solution? Here is my source code: import sys def check(num): lis=[20,19,18,17,16,14,13,11] #because a no. divisible by 20 is divisible by 2,4,5 and so on for the values omitted for i in lis: if num%i==0: continue else: return False break return True def main(): num=2520 # Because we can start from the no. divisible by 1-10 while not check(num): print(num) num+=2 # Because num has to be even print(num) if __name__ == '__main__': main() -------------- next part -------------- An HTML attachment was scrubbed... URL: From marc.tompkins at gmail.com Tue Aug 13 18:49:49 2013 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 13 Aug 2013 09:49:49 -0700 Subject: [Tutor] dbus.Array to string In-Reply-To: References: <520834B0.80704@pearwood.info> <520849BD.4030004@pearwood.info> Message-ID: On Tue, Aug 13, 2013 at 8:59 AM, Amit Saha wrote: > What does it mean (and will it always work?) when I don't specify any > encoding: > > >>> bytearray(ssid).decode() > u'BigPond679D85' > If you don't specify an encoding, then the default encoding is used; as you point out a bit later, your local default is ascii. Will it always work? NO. If there are any characters in the input stream (the SSID in this case), .decode will fail (probably with UnicodeDecodeError, but I can't test it at the moment.) I don't know the WiFi spec well enough to know whether you're ever going to run into non-ASCII characters in an SSID; I'm guessing that people in e.g. Russia name their networks in Cyrillic, but (despite living in a Russian neighborhood of Los Angeles) I've never seen any SSIDs that weren't pure ASCII. Does anybody out there know the rules for this? Just now I tried to change the SSID of my cell phone's mobile hotspot to Cyrillic, but the configuration utility wouldn't even let me change keyboards; I don't know, though, whether this is a limitation of the spec or just the local implementation. -------------- next part -------------- An HTML attachment was scrubbed... URL: From JATINSHR001 at e.ntu.edu.sg Tue Aug 13 18:43:12 2013 From: JATINSHR001 at e.ntu.edu.sg (#PATHANGI JANARDHANAN JATINSHRAVAN#) Date: Tue, 13 Aug 2013 16:43:12 +0000 Subject: [Tutor] (no subject) Message-ID: <52B2907AE37EB94B8690A0B45EE20918C9BD88@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> Hello All, I am solving problem number 5 in project euler. I think my solution seems logically correct but it is not giving an answer as it is taking too long to execute. So can someone suggest an alternative solution? Here is my source code: import sys def check(num): lis=[20,19,18,17,16,14,13,11] #because a no. divisible by 20 is divisible by 2,4,5 and so on for the values omitted for i in lis: if num%i==0: continue else: return False break return True def main(): num=2520 # Because we can start from the no. divisible by 1-10 while not check(num): print(num) num+=2 # Because num has to be even print(num) if __name__ == '__main__': main() -------------- next part -------------- An HTML attachment was scrubbed... URL: From marc.tompkins at gmail.com Tue Aug 13 19:14:51 2013 From: marc.tompkins at gmail.com (Marc Tompkins) Date: Tue, 13 Aug 2013 10:14:51 -0700 Subject: [Tutor] To find the least number divisible by all numbers from 1-20 In-Reply-To: <52B2907AE37EB94B8690A0B45EE20918C9BD8E@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> References: <52B2907AE37EB94B8690A0B45EE20918C9BD8E@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> Message-ID: On Tue, Aug 13, 2013 at 9:45 AM, #PATHANGI JANARDHANAN JATINSHRAVAN# < JATINSHR001 at e.ntu.edu.sg> wrote: > Hello All, > > Sorry for the earlier mail without subject. I was in a hurry so I missed > that > > I am solving problem number 5 in project euler. I think my solution > seems logically correct but it is not giving an answer as it is taking too > long to execute. So can someone suggest an alternative solution? > My approach: factor each number from 1 to 20 (for example, 20 factors to 1, 2, 2, 5) and build a list of factors; for each number, check to see that the list contains enough copies of all of the current number's factors (e.g. four 2s and two 3s) and add them to the list if not; at the end, multiply the list of factors. It took me about a minute in Excel; about 45 minutes to work out my algorithm in Python - but it executed in less than a second. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Aug 13 19:19:50 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 13 Aug 2013 18:19:50 +0100 Subject: [Tutor] To find the least number divisible by all numbers from 1-20 In-Reply-To: <52B2907AE37EB94B8690A0B45EE20918C9BD8E@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> References: <52B2907AE37EB94B8690A0B45EE20918C9BD8E@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> Message-ID: On 13/08/13 17:45, #PATHANGI JANARDHANAN JATINSHRAVAN# wrote: > def check(num): > lis=[20,19,18,17,16,14,13,11] #because a no. divisible by 20 is > for i in lis: > if num%i==0: > continue > else: > return False > break > > return True This is a bit convoluted. All you need is for i in lis: if num%i != 0: return False return True > def main(): > num=2520 # Because we can start from the no. divisible by 1-10 > while not check(num): > print(num) > num+=2 # Because num has to be even Surely you can increment by 20 since that's the next number that is divisible by 20. eg if we start at 20 the next number divisible by 20 will be 40, not 22... That should speed up the search considerably! But my brain is nagging me that there should be a cleverer mathematical trick too, but I can't remember what it is... I'm sure one of our math gurus will tell us! :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From __peter__ at web.de Tue Aug 13 20:09:30 2013 From: __peter__ at web.de (Peter Otten) Date: Tue, 13 Aug 2013 20:09:30 +0200 Subject: [Tutor] To find the least number divisible by all numbers from 1-20 References: <52B2907AE37EB94B8690A0B45EE20918C9BD8E@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> Message-ID: #PATHANGI JANARDHANAN JATINSHRAVAN# wrote: > Hello All, > > Sorry for the earlier mail without subject. I was in a hurry so I missed > that > > I am solving problem number 5 in project euler. I think my solution seems > logically correct but it is not giving an answer as it is taking too long > to execute. So can someone suggest an alternative solution? Here is my > source code: > > import sys > def check(num): > lis=[20,19,18,17,16,14,13,11] #because a no. divisible by 20 is > divisible by 2,4,5 and so on for the values omitted for i in lis: > if num%i==0: > continue > else: > return False > break > > return True > > def main(): > num=2520 # Because we can start from the no. divisible by 1-10 > while not check(num): > print(num) > num+=2 # Because num has to be even > print(num) > > > if __name__ == '__main__': > main() A simple algorithm that I think should work: Start with the list of 19 numbers (no need to include the 1). Pick two numbers a and b, calculate the (greatest common divisor) gcd, and replace them with the single number c calculated as d = gcd(a, b) c = a*b//d until there is only one number left in the list. This should be your result. I'll post a naive implementation of that idea in a follow-up. From alan.gauld at btinternet.com Tue Aug 13 20:10:28 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 13 Aug 2013 19:10:28 +0100 Subject: [Tutor] To find the least number divisible by all numbers from 1-20 In-Reply-To: References: <52B2907AE37EB94B8690A0B45EE20918C9BD8E@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> Message-ID: On 13/08/13 18:19, Alan Gauld wrote: > On 13/08/13 17:45, #PATHANGI JANARDHANAN JATINSHRAVAN# wrote: > >> def check(num): >> lis=[20,19,18,17,16,14,13,11] #because a no. divisible by 20 is >> for i in lis: >> if num%i==0: >> continue >> else: >> return False >> break >> >> return True > > This is a bit convoluted. All you need is > > for i in lis: > if num%i != 0: > return False > return True Oops, that last return should be outside the loop, sorry... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From __peter__ at web.de Tue Aug 13 20:20:04 2013 From: __peter__ at web.de (Peter Otten) Date: Tue, 13 Aug 2013 20:20:04 +0200 Subject: [Tutor] [Spoiler alert] Re: To find the least number divisible by all numbers from 1-20 References: <52B2907AE37EB94B8690A0B45EE20918C9BD8E@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> Message-ID: Peter Otten wrote: > I'll post a naive implementation of that idea in a follow-up. So there: def gcd(a, b): if a == b: return a elif a > b: return gcd(a-b, b) else: return gcd(a, b-a) N = 20 numbers = range(2, N+1) while len(numbers) > 1: numbers.sort() a, b = numbers[:2] d = gcd(a, b) numbers[:2] = [a*b//d] result = numbers[0] assert all(result % i == 0 for i in range(1, N+1)) print result It turns out I can only avoid the recursion limit by sorting the numbers and picking the smallest. For greater N you need to come up with a smarter approach (the actual limit was N<=48) or at least a non-recursive implementation of gcd(). From tim.peters at gmail.com Tue Aug 13 20:35:28 2013 From: tim.peters at gmail.com (Tim Peters) Date: Tue, 13 Aug 2013 13:35:28 -0500 Subject: [Tutor] To find the least number divisible by all numbers from 1-20 In-Reply-To: <52B2907AE37EB94B8690A0B45EE20918C9BD8E@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> References: <52B2907AE37EB94B8690A0B45EE20918C9BD8E@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> Message-ID: [#PATHANGI JANARDHANAN JATINSHRAVAN#] > /// > I am solving problem number 5 in project euler. I think my solution seems > logically correct but it is not giving an answer as it is taking too long to > execute. It does take a long time, but it does return the correct answer - eventually ;-) > So can someone suggest an alternative solution? Here is my source > code: > > import sys > def check(num): > lis=[20,19,18,17,16,14,13,11] #because a no. divisible by 20 is divisible > by 2,4,5 and so on for the values omitted > for i in lis: > if num%i==0: > continue > else: > return False > break > > return True Simpler: def check(num): return all(num % i == 0 for i in [20,19,18,17,16,14,13,11]) > def main(): > num=2520 # Because we can start from the no. divisible by 1-10 > while not check(num): > print(num) > num+=2 # Because num has to be even You can speed this up by a factor of about 1000 via incrementing by 2520 instead of by 2. Because the correct answer has to be divisible by all of 1 thru 10, it has to be divisible by 2520 too, right? Therefore the correct answer must be a multiple of 2520. > print(num) Peter is on "the best" track via exploiting this fact: lcm(a, b) = a * b // gcd(a, b) The problems he hit with recursion can be avoided by using an iterative gcd function instead. Like so: def gcd(a, b): while b: a, b = b, a % b return a def lcm_list(ns): sofar = 1 for n in ns: sofar = sofar * n // gcd(sofar, n) return sofar Then: >>> print lcm_list(range(1, 21)) # your case 232792560 That takes a small fraction of a second. So does this: >>> print lcm_list(range(1, 101)) 69720375229712477164533808935312303556800 Even this takes a fraction of a second!: >>> print lcm_list(range(1, 1001)) 7128865274665093053166384155714272920668358861885893040452001991154324087581111499476444151913871586911717817019575256512980264067621009251465871004305131072686268143200196609974862745937188343705015434452523739745298963145674982128236956232823794011068809262317708861979540791247754558049326475737829923352751796735248042463638051137034331214781746850878453485678021888075373249921995672056932029099390891687487672697950931603520000 Amazing, eh? :-) From oscar.j.benjamin at gmail.com Tue Aug 13 20:42:46 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 13 Aug 2013 19:42:46 +0100 Subject: [Tutor] [Spoiler alert] Re: To find the least number divisible by all numbers from 1-20 In-Reply-To: References: <52B2907AE37EB94B8690A0B45EE20918C9BD8E@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> Message-ID: On Aug 13, 2013 7:21 PM, "Peter Otten" <__peter__ at web.de> wrote: > > Peter Otten wrote: > > > I'll post a naive implementation of that idea in a follow-up. > > So there: > > def gcd(a, b): > if a == b: > return a > elif a > b: > return gcd(a-b, b) > else: > return gcd(a, b-a) > > N = 20 > numbers = range(2, N+1) > while len(numbers) > 1: > numbers.sort() > a, b = numbers[:2] > d = gcd(a, b) > numbers[:2] = [a*b//d] > result = numbers[0] > assert all(result % i == 0 for i in range(1, N+1)) > print result > > It turns out I can only avoid the recursion limit by sorting the numbers and > picking the smallest. For greater N you need to come up with a smarter > approach (the actual limit was N<=48) or at least a non-recursive > implementation of gcd(). I think it's better to compute the lcm by finding its prime factorisation. E.g. There are 4 twos in it since that's the maximum number of twos in the prime factorisation of any of the integers up to 20. I would post a snippet but isn't it generally a bit of a faux-pas to give solutions to these problems (I mean this as a real question)? Oscar > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Tue Aug 13 21:08:52 2013 From: __peter__ at web.de (Peter Otten) Date: Tue, 13 Aug 2013 21:08:52 +0200 Subject: [Tutor] Posting solutions to "homeworky" problems References: <52B2907AE37EB94B8690A0B45EE20918C9BD8E@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> Message-ID: Oscar Benjamin wrote: > I would post a snippet but isn't it generally a bit of a faux-pas to give > solutions to these problems (I mean this as a real question)? In principle I agree, but sometimes I lack the discipline to hold back. I think the code I gave is peculiar enough to make it easy to find for a suspicious teacher hunting for cut-and-paste artists ;) From ramit.prasad at jpmorgan.com.dmarc.invalid Tue Aug 13 21:27:18 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Tue, 13 Aug 2013 19:27:18 +0000 Subject: [Tutor] Posting solutions to "homeworky" problems In-Reply-To: References: <52B2907AE37EB94B8690A0B45EE20918C9BD8E@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> Message-ID: <5B80DD153D7D744689F57F4FB69AF4741865963F@SCACMX008.exchad.jpmchase.net> Peter Otten wrote: > Oscar Benjamin wrote: > > > I would post a snippet but isn't it generally a bit of a faux-pas to give > > solutions to these problems (I mean this as a real question)? > > In principle I agree, but sometimes I lack the discipline to hold back. > > I think the code I gave is peculiar enough to make it easy to find for a > suspicious teacher hunting for cut-and-paste artists ;) Not to mention that since this is a Euler question (stated in the original post), optimized answers are easily found on the Internet. The tricky bit is in figuring the appropriate algorithm and not actually in the coding. I like the Euler project, but I found it more a test of my math/analysis skills and much less of my programming skills. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From steve at pearwood.info Wed Aug 14 04:14:19 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 14 Aug 2013 12:14:19 +1000 Subject: [Tutor] dbus.Array to string In-Reply-To: References: <520834B0.80704@pearwood.info> <520849BD.4030004@pearwood.info> Message-ID: <520AE7FB.7080303@pearwood.info> On 14/08/13 02:49, Marc Tompkins wrote: > On Tue, Aug 13, 2013 at 8:59 AM, Amit Saha wrote: > > >> What does it mean (and will it always work?) when I don't specify any >> encoding: >> >>>>> bytearray(ssid).decode() >> u'BigPond679D85' >> > > If you don't specify an encoding, then the default encoding is used; as you > point out a bit later, your local default is ascii. > > Will it always work? NO. If there are any characters in the input stream > (the SSID in this case), .decode will fail (probably with > UnicodeDecodeError, but I can't test it at the moment.) Careful -- you are confusing two distinct concepts here. ssid does not contain characters. It contains bytes. There are exactly 256 possible bytes, which are numbers 0, 1, ... 255. They may *represent* characters, or sounds, or images, or motion video, or any other form of data you like, in which case you have to ask (e.g.) "how is the sound encoded into bytes? is it a WAV file, or MP3, or OGG, or something else?" In this case, the ssid represents characters, but it contains bytes, and the same question applies -- how are the characters A, B, C, ... encoded into bytes? Unless you know which encoding is used, you have to guess. If you guess wrong, you'll get errors. If you're lucky you will get an exception, and know that you guessed wrong, but if you're unlucky you'll just get garbage characters. Fortunately, there are a couple of decent guesses you can make which will often be correct, at least in Western European countries, Australia, the USA, and similar: UTF-8 ASCII Latin-1 Latin-1 should be considered the "last resort" encoding, since it will never fail. But it can return garbage. UTF-8 should be considered your first guess, since it is the standard encoding that everyone should use. (Any application that doesn't use UTF-8 by default in the 21st century is, in my opinion, buggy.) > I don't know the WiFi spec well enough to know whether you're ever going to > run into non-ASCII characters in an SSID; A little bit of googling shows that it definitely happens, and that UTF-8 is the standard encoding to use. -- Steven From eryksun at gmail.com Wed Aug 14 04:11:36 2013 From: eryksun at gmail.com (eryksun) Date: Tue, 13 Aug 2013 22:11:36 -0400 Subject: [Tutor] dbus.Array to string In-Reply-To: References: <520834B0.80704@pearwood.info> <520849BD.4030004@pearwood.info> Message-ID: On Tue, Aug 13, 2013 at 12:49 PM, Marc Tompkins wrote: > > Will it always work? NO. If there are any characters in the input stream > (the SSID in this case), .decode will fail (probably with > UnicodeDecodeError, but I can't test it at the moment.) > > I don't know the WiFi spec well enough to know whether you're ever going to > run into non-ASCII characters in an SSID It's defined as 32 octets (bytes), but the allowed values aren't specified. Stick with alphanumeric ASCII for interoperability. Using UTF-8 is spec'd in the 802.11-2012 revision. There's an SSIDEncoding field in the MLME-START.request that initiates a BSS. If it's UTF-8, then subsequent beacon/probe response frames will have the UTF-8 SSID field (bit 48) set in the Extended Capabilities element, indicating that the SSID is UTF-8 encoded. http://standards.ieee.org/findstds/standard/802.11-2012.html From JATINSHR001 at e.ntu.edu.sg Wed Aug 14 04:49:47 2013 From: JATINSHR001 at e.ntu.edu.sg (#PATHANGI JANARDHANAN JATINSHRAVAN#) Date: Wed, 14 Aug 2013 02:49:47 +0000 Subject: [Tutor] To find the least number divisible by all numbers from 1-20 In-Reply-To: References: <52B2907AE37EB94B8690A0B45EE20918C9BD8E@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com>, Message-ID: <52B2907AE37EB94B8690A0B45EE20918C9BE49@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> ________________________________________ From: Tim Peters [tim.peters at gmail.com] Sent: Wednesday, August 14, 2013 02:35 AM To: #PATHANGI JANARDHANAN JATINSHRAVAN# Cc: tutor at python.org; N_Gopalakrishnan at Dell.com; pathangi_janardhanan at dell.com Subject: Re: [Tutor] To find the least number divisible by all numbers from 1-20 [#PATHANGI JANARDHANAN JATINSHRAVAN#] > /// > I am solving problem number 5 in project euler. I think my solution seems > logically correct but it is not giving an answer as it is taking too long to > execute. It does take a long time, but it does return the correct answer - eventually ;-) > So can someone suggest an alternative solution? Here is my source > code: > > import sys > def check(num): > lis=[20,19,18,17,16,14,13,11] #because a no. divisible by 20 is divisible > by 2,4,5 and so on for the values omitted > for i in lis: > if num%i==0: > continue > else: > return False > break > > return True Simpler: def check(num): return all(num % i == 0 for i in [20,19,18,17,16,14,13,11]) > def main(): > num=2520 # Because we can start from the no. divisible by 1-10 > while not check(num): > print(num) > num+=2 # Because num has to be even You can speed this up by a factor of about 1000 via incrementing by 2520 instead of by 2. Because the correct answer has to be divisible by all of 1 thru 10, it has to be divisible by 2520 too, right? Therefore the correct answer must be a multiple of 2520. Incrementing by 2520 drastically speeds up the execution time. Thanks, I didn't think of that. And I'm still trying to comprehend the LCM and GCD methods. I'm pretty new to programming and python. > print(num) Peter is on "the best" track via exploiting this fact: lcm(a, b) = a * b // gcd(a, b) The problems he hit with recursion can be avoided by using an iterative gcd function instead. Like so: def gcd(a, b): while b: a, b = b, a % b return a def lcm_list(ns): sofar = 1 for n in ns: sofar = sofar * n // gcd(sofar, n) return sofar Then: >>> print lcm_list(range(1, 21)) # your case 232792560 That takes a small fraction of a second. So does this: >>> print lcm_list(range(1, 101)) 69720375229712477164533808935312303556800 Even this takes a fraction of a second!: >>> print lcm_list(range(1, 1001)) 7128865274665093053166384155714272920668358861885893040452001991154324087581111499476444151913871586911717817019575256512980264067621009251465871004305131072686268143200196609974862745937188343705015434452523739745298963145674982128236956232823794011068809262317708861979540791247754558049326475737829923352751796735248042463638051137034331214781746850878453485678021888075373249921995672056932029099390891687487672697950931603520000 Amazing, eh? :-) From tim.peters at gmail.com Wed Aug 14 05:20:40 2013 From: tim.peters at gmail.com (Tim Peters) Date: Tue, 13 Aug 2013 22:20:40 -0500 Subject: [Tutor] To find the least number divisible by all numbers from 1-20 In-Reply-To: <52B2907AE37EB94B8690A0B45EE20918C9BE49@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> References: <52B2907AE37EB94B8690A0B45EE20918C9BD8E@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> <52B2907AE37EB94B8690A0B45EE20918C9BE49@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> Message-ID: [#PATHANGI JANARDHANAN JATINSHRAVAN# ] > ... > And I'm still trying to comprehend the LCM and GCD methods. I'm pretty new to > programming and python. The first rule is to have fun ;-) The key to a blazing fast solution to this problem really isn't in the programming, it's in understanding the mathematics of it. Then the code pretty much writes itself - LOL. Here's a good discussion of "the math": http://en.wikipedia.org/wiki/Least_common_multiple From oscar.j.benjamin at gmail.com Wed Aug 14 14:40:11 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 14 Aug 2013 13:40:11 +0100 Subject: [Tutor] Posting solutions to "homeworky" problems In-Reply-To: <5B80DD153D7D744689F57F4FB69AF4741865963F@SCACMX008.exchad.jpmchase.net> References: <52B2907AE37EB94B8690A0B45EE20918C9BD8E@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> <5B80DD153D7D744689F57F4FB69AF4741865963F@SCACMX008.exchad.jpmchase.net> Message-ID: On 13 August 2013 20:27, Prasad, Ramit wrote: > Peter Otten wrote: >> Oscar Benjamin wrote: >> >> > I would post a snippet but isn't it generally a bit of a faux-pas to give >> > solutions to these problems (I mean this as a real question)? >> >> In principle I agree, but sometimes I lack the discipline to hold back. I agree that it's difficult (see below). >> I think the code I gave is peculiar enough to make it easy to find for a >> suspicious teacher hunting for cut-and-paste artists ;) A teacher should know not to set this as a weighted assignment except perhaps under exam conditions. I think I'd get into a lot of trouble if I made my students do this in an exam: the current consensus is that I'm not allowed to actually *require* original lateral thinking from them unless it's coursework (so that they can cheat). > Not to mention that since this is a Euler question (stated in the original > post), optimized answers are easily found on the Internet. The tricky bit > is in figuring the appropriate algorithm and not actually in the coding. Well if the solutions are already on the internet I guess it doesn't matter. More importantly if Peter gets to do it and Tim gets to do it then it's just *so* unfair if I can't do it too! So here's my own deliberately cryptic solution :) def lcm_1_to_N(N): sup = 1 logpn = 0 while sup < N: sup *= 2 logpn += 1 lcm = 1 p1 = 1 nums = [] while p1 < N: p1 += 1 flag = True for p2 in nums: if not p1 % p2: flag = False break elif p2 ** 2 > p1: break if flag: nums.append(p1) inf = p1 ** logpn while inf > N: inf /= p1 logpn -= 1 lcm *= inf return lcm Oscar From emile at fenx.com Thu Aug 15 19:52:48 2013 From: emile at fenx.com (Emile van Sebille) Date: Thu, 15 Aug 2013 10:52:48 -0700 Subject: [Tutor] Python Arduino Web Page In-Reply-To: <000601ce94db$147b32d0$3d719870$@in> References: <000601ce94db$147b32d0$3d719870$@in> Message-ID: <520D1570.5010207@fenx.com> The value of myvar is not passed in your print"""... +parseInt(myvar);...""" which results in the string characters myvar being interpreted by parseInt and resulting in NaN Try print"""... +parseInt(%s);...""" % myvar this causes python to swap in the value of myvar which then will be parseable with parseInt. HTH, Emile On 8/9/2013 1:33 AM, Engineering wrote: > I have written the following code in Python for my Arduino > > #!c:\Python27\Python.exe > > import cgi, cgitb; > > import sys, serial > > cgitb.enable() > > ser = serial.Serial('COM27', 9600) > > myvar = ser.readline() > > print "Content-type:text/html\n\n" > > print """ > > > > > > > > Real Time Temperature > > > > > > > > > >

Real Time Temperature:

> >
> > > > """ > > I have Apache Web Server > > But on running this my localhost , I am getting the following output > ?TemperatureNaN? . Why is NaN being showed. Why cant I get the number as > read by se.readline()? > > Is there a way to separate the Python code and the Javascript on > different files. If so can you please give a example.? > > I am not really interested to do with bottle or other web framework as I > will be controlling my arduino with python from the web and also do > video image processing with openCV. Open CV have a support for Python > and not for Javascript. > > Thanks > > Subhendu Sinha Chaudhuri > > *CLEANTECH SOLUTION* > > *www.cleantechsolution.in* > > ** > > SAVE PAPER , SAVE EARTH > > ** > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From davea at davea.name Thu Aug 15 23:45:09 2013 From: davea at davea.name (Dave Angel) Date: Thu, 15 Aug 2013 21:45:09 +0000 (UTC) Subject: [Tutor] Python Arduino Web Page References: <000601ce94db$147b32d0$3d719870$@in> Message-ID: Engineering wrote: > >

I have written the following code in Python for my Arduino

 

#!c:\Python27\Python.exe

import cgi, cgitb;

import sys, serial

cgitb.enable()

ser = serial.Serial('COM27', 9600)

myvar = ser.readline()

print "Content-type:text/html\n\n"

print """

<html>

<head>

<title>

Real Time Temperature

</title>

  <script type="text/j It's pretty hard to interpret that html message. Please post using text messages, as it's both more compact and less likely to be misinterpreted by the (text) mailing list mechanisms. You fetch a value from the serial port in Python called myvar. However, the javascript code cannot see that value unless you stuff it into the generated script. The easiest place to stuff it would be on the line:

by doing something like
Temperature=%d
and at the end of the code, changing the final triple quote to: """ % myvar That way the generated html has the string already substituted. If this is just a (somewhat) simplified version of something that *needs* javascript, then consider stuffing an assignment statement in the startinterval() function. However, I can't see as you actually pass it to startTime() anyway, so the javascript ought to be blowing up with a missing argument. Clearly I don't know enough javascript to get out of the mud, so if your real code is more complex, somebody else will have to help you figure where to change it. As for seeing the javascript code separately, one approach is to use your browser to view the source html. Another is to use wget to just fetch the page. -- DaveA From chao_888 at yahoo.com Tue Aug 13 18:18:48 2013 From: chao_888 at yahoo.com (cindy chao) Date: Tue, 13 Aug 2013 09:18:48 -0700 (PDT) Subject: [Tutor] please help with read_until, write script Message-ID: <1376410728.90213.YahooMailNeo@web120301.mail.ne1.yahoo.com> Hi, all Python Experts, I am a Python beginner. I am trying to script the following steps. I have problem to match the step after 'signature check?' '[999.daisyse.07260019]:' the value of 0726009 is dynamic, every time it will be different, so I don't know how to do it. For sure I have problem to match 'directory and config file? (Yes/No) [Yes]:' and 'current configuration? (Yes/No) [Yes]:' I use IDLE in pc to write and run the script. Please help, thank you very much. All the steps I want to script. vyatta at Daisy-BGP1:~$ add sys image http://packages.vyatta.com/vyatta-dev/daisy-se/unstable/iso/livecd_2013-07-26-0018-7e824ac_i386.iso Trying to fetch ISO file from http://packages.vyatta.com/vyatta-dev/daisy-se/unstable/iso/livecd_2013-07-26-0018-7e824ac_i386.iso ? % Total??? % Received % Xferd? Average Speed?? Time??? Time???? Time? Current ???????????????????????????????? Dload? Upload?? Total?? Spent??? Left? Speed 100? 223M? 100? 223M??? 0???? 0? 45.6M????? 0? 0:00:04? 0:00:04 --:--:-- 45.2M ISO download succeeded. Checking for digital signature file... ? % Total??? % Received % Xferd? Average Speed?? Time??? Time???? Time? Current ???????????????????????????????? Dload? Upload?? Total?? Spent??? Left? Speed ? 0???? 0??? 0???? 0??? 0???? 0????? 0????? 0 --:--:-- --:--:-- --:--:--???? 0 curl: (22) The requested URL returned error: 404 Unable to fetch digital signature file. Do you want to continue without signature check? (yes/no) [yes] yes Checking MD5 checksums of files on the ISO image...OK. Done! What would you like to name this image? [999.daisyse.07260019]: OK.? This image will be named: 999.daisyse.07260019 Installing "999.daisyse.07260019" image. Copying new release files... Would you like to save the current configuration directory and config file? (Yes/No) [Yes]: yes Copying current configuration... Would you like to save the SSH host keys from your current configuration? (Yes/No) [Yes]: yes Copying SSH keys... Setting up grub configuration... Done. ===================================================== Here is my code. session = telnetlib.Telnet("10.1.34.21", timeout=5) ...skip some write and read_until... session.write('yes'+'\n') h1=session.read_until('[999.daisyse.07260019]:') print h1 session.write('\n') ??????? #h20=session.read_until('(Yes/No) [Yes]:')- never match #h20=session.read_until('[Yes]:') - never match #h20=session.expect('(Yes/No)', '[Yes]:')- script just terminate #h20=session.read_lazy('[Yes]:')-script just terminate #h20=session.read_expect('.*\(Yes\/No\)')- script just terminate h20=session.read_expect('.*\s\(Yes\/No\)')- script just terminate print h20 ======================================================= #h20=session.read_until('(Yes/No) [Yes]:')- never match, just wait there, never finish. OUtput from IDLE: [Kvyatta at Daisy-BGP1:~$ ?add sys image http://packages.vyatta.com/vyatta-dev/daisy-s e/unstable/iso/livecd_2013-07-26-0018-7e824ac_i386.iso Trying to fetch ISO file from http://packages.vyatta.com/vyatta-dev/daisy-se/unstable/iso/livecd_2013-07-26-0018-7e824ac_i386.iso ? % Total??? % Received % Xferd? Average Speed?? Time??? Time???? Time? Current ???????????????????????????????? Dload? Upload?? Total?? Spent??? Left? Speed ? 0???? 0??? 0???? 0??? 0???? 0????? 0????? 0 --:--:-- --:--:-- --:--:--???? 0 ?34? 223M?? 34 77.1M??? 0???? 0? 87.1M????? 0? 0:00:02 --:--:--? 0:00:02 87.4M ?60? 223M?? 60? 135M??? 0???? 0? 71.6M????? 0? 0:00:03? 0:00:01? 0:00:02 71.8M ?69? 223M?? 69? 154M??? 0???? 0? 53.4M????? 0? 0:00:04? 0:00:02? 0:00:02 53.5M ?70? 223M?? 70? 156M??? 0???? 0? 19.2M????? 0? 0:00:11? 0:00:08? 0:00:03 19.2M 100? 223M? 100? 223M??? 0???? 0? 25.2M????? 0? 0:00:08? 0:00:08 --:--:-- 25.2M ISO download succeeded. Checking for digital signature file... ? % Total??? % Received % Xferd? Average Speed?? Time??? Time???? Time? Current ???????????????????????????????? Dload? Upload?? Total?? Spent??? Left? Speed ? 0???? 0??? 0???? 0??? 0???? 0????? 0????? 0 --:--:-- --:--:-- --:--:--???? 0 ? 0???? 0??? 0???? 0??? 0???? 0????? 0????? 0 --:--:-- --:--:-- --:--:--???? 0 curl: (22) The requested URL returned error: 404 Unable to fetch digital signature file. Do you want to continue without signature check? (yes/no) [yes] ?yes Checking MD5 checksums of files on the ISO image...OK. Done! What would you like to name this image? [999.daisyse.07260019]: ========================================== #h20=session.expect('(Yes/No)', '[Yes]')- script just terminate Do you want to continue without signature check? (yes/no) [yes] ?yes Checking MD5 checksums of files on the ISO image...OK. Done! What would you like to name this image? [999.daisyse.07260019]: >>> =========================================== -------------- next part -------------- An HTML attachment was scrubbed... URL: From jake.wohldmann at gmail.com Wed Aug 14 00:38:34 2013 From: jake.wohldmann at gmail.com (Jake Wohldmann) Date: Tue, 13 Aug 2013 17:38:34 -0500 Subject: [Tutor] Python Message-ID: Hello I am a beginner to using any type of script. How long does it take to become fluent at a script? I will only be able to practice python on weekends since school is starting. I was also wondering if I could use python on my android phone. -------------- next part -------------- An HTML attachment was scrubbed... URL: From leamhall at gmail.com Tue Aug 13 17:46:30 2013 From: leamhall at gmail.com (leam hall) Date: Tue, 13 Aug 2013 11:46:30 -0400 Subject: [Tutor] Input into lists? Message-ID: I'm trying to take input from a file (CSV spreadsheet) and use the first line inputs (header) as the names of lists. So far I'm not successful. :) How do I take line_list[0], the result of "line.split(',')" and use it as the name of a list? Does that make sense? Thanks! Leam -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From leamhall at gmail.com Tue Aug 13 21:29:37 2013 From: leamhall at gmail.com (leam hall) Date: Tue, 13 Aug 2013 15:29:37 -0400 Subject: [Tutor] Posting solutions to "homeworky" problems In-Reply-To: <5B80DD153D7D744689F57F4FB69AF4741865963F@SCACMX008.exchad.jpmchase.net> References: <52B2907AE37EB94B8690A0B45EE20918C9BD8E@HKNPRD0111MB386.apcprd01.prod.exchangelabs.com> <5B80DD153D7D744689F57F4FB69AF4741865963F@SCACMX008.exchad.jpmchase.net> Message-ID: I've just started some of the Euler stuff. Most of the time I ask for pointers to the logic needed. I enjoy that much more as hopefully I learn something. Leam -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From leamhall at gmail.com Wed Aug 14 21:21:19 2013 From: leamhall at gmail.com (leam hall) Date: Wed, 14 Aug 2013 15:21:19 -0400 Subject: [Tutor] Simple Python SNMP ping? Message-ID: Is there a way to do a simple check in Python to see if a remote host is listening on SNMP port 161/UDP? Thanks! Leam -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From ljetibo at gmail.com Tue Aug 13 00:57:09 2013 From: ljetibo at gmail.com (=?ISO-8859-2?Q?Dino_Bekte=B9evi=E6?=) Date: Tue, 13 Aug 2013 00:57:09 +0200 Subject: [Tutor] [Python-Help] Instancing class issue In-Reply-To: <20130812211908.GC2927@gopher> References: <20130812211908.GC2927@gopher> Message-ID: Hello, The name of the variable doesn't matter, that's still what it does; you can > think of it as encapsulating any other keyword arguments. > > >>> def foo(bar, **kwargs): > ... print("bar: %s" % (bar,)) > ... print("kwargs: %r" % (kwargs,)) > ... > >>> foo("bar", baz="qux", wibble="wobble") > bar: bar > kwargs: {'baz': 'qux', 'wibble': 'wobble'} > Yeah so much I figured, same way self could be me, but what kind of __init__ function is that that doesn't even know how many arguments it takes, at least it should have the mandatory arguments to satisfy the Parent __init__ function which is object but I don't even see object.__init__(self, **kwargs) in sdsspy's __init__ (or **keys whatever you prefer) what are even the keyword arguments needed for instancing object. Isn't object like THE most abstract class that becomes anything? How do I even go about trying to instance this? (and since it calls on some printed error obviously written somewhere in the SDSSPY how do I reach it so I'd know what to do?) many thanks to responders, Dino Bekte?evi? 2013/8/12 Chris Down > Hi Dino, > > On 2013-08-12 20:32, Dino Bekte?evi? wrote: > > def __init__(self, **keys): > > from . import util > > > > self.keys=keys > > self.load() > > > > where I don't understand what **keys mean, I've only seen that as > **kwargs > > meaning other key words and arguments in examples. > > The name of the variable doesn't matter, that's still what it does; you can > think of it as encapsulating any other keyword arguments. > > >>> def foo(bar, **kwargs): > ... print("bar: %s" % (bar,)) > ... print("kwargs: %r" % (kwargs,)) > ... > >>> foo("bar", baz="qux", wibble="wobble") > bar: bar > kwargs: {'baz': 'qux', 'wibble': 'wobble'} > > > > When I try to instance > > Astrom class by: > > new=Astrom() > > I receive a following error: > > Sorry, no idea about this bit, I've never used sdsspy. est of luck sorting > this > out. > > Chris > -------------- next part -------------- An HTML attachment was scrubbed... URL: From zoyatvkl at gmail.com Wed Aug 14 20:10:57 2013 From: zoyatvkl at gmail.com (Zoya Tavakkoli) Date: Wed, 14 Aug 2013 11:10:57 -0700 Subject: [Tutor] Color segmentation -Open cv Message-ID: Hi every one , could you please give me suggestion and code for color segmentation via opencv on python? Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From zoyatvkl at gmail.com Thu Aug 15 15:54:32 2013 From: zoyatvkl at gmail.com (Zoya Tavakkoli) Date: Thu, 15 Aug 2013 06:54:32 -0700 Subject: [Tutor] Reading a video Message-ID: Hi How can read a video frame by frame in python? Thanks Zoya -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Aug 16 02:27:31 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 16 Aug 2013 01:27:31 +0100 Subject: [Tutor] Input into lists? In-Reply-To: References: Message-ID: On 13/08/13 16:46, leam hall wrote: > I'm trying to take input from a file (CSV spreadsheet) and use the first > line inputs (header) as the names of lists. So far I'm not successful. :) Have you tried the csv module? Usually you use a dictionary keyed by the first line values though. But i#'m sure you could tweak it to generate separate lists if thats really what you need. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From davea at davea.name Fri Aug 16 02:28:47 2013 From: davea at davea.name (Dave Angel) Date: Fri, 16 Aug 2013 00:28:47 +0000 (UTC) Subject: [Tutor] Input into lists? References: Message-ID: leam hall wrote: > I'm trying to take input from a file (CSV spreadsheet) and use the first > line inputs (header) as the names of lists. So far I'm not successful. :) > > How do I take line_list[0], the result of "line.split(',')" and use it as > the name of a list? Does that make sense? > No, it doesn't make sense. You're trying to define a variable that has a name determined not by your source, but by a name in line 0 of the csv file. How then are you going to use that variable? There are two possibilities: Either you actually know those names and are using them in your code, or the names won't be known till runtime. If the latter, then make a dictionary of lists, mydict = {} for item in line_list: mydict[item] = [] and then populate those lists from each line of the file. Now, if you're sure you know what the names are to be, then: names = mydict["name"] addresses = mydict["address"] or whatever. -- DaveA From alan.gauld at btinternet.com Fri Aug 16 02:30:34 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 16 Aug 2013 01:30:34 +0100 Subject: [Tutor] Python In-Reply-To: References: Message-ID: On 13/08/13 23:38, Jake Wohldmann wrote: > Hello I am a beginner to using any type of script. How long does it > take to become fluent at a script? It depends on what you mean by Fluent. I've spent 40+ years programming and am still learning. OTOH I can usually learn enough about a new language to start using it in a day. Its like learning a musical instrument. You can bang out a few chords and play a recognizable melody pretty quickly. To be a maestro takes a lifetime. Learn the basics then pick a project and go for it. The more hands on you can get the better. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From leamhall at gmail.com Fri Aug 16 02:37:12 2013 From: leamhall at gmail.com (Leam Hall) Date: Thu, 15 Aug 2013 20:37:12 -0400 Subject: [Tutor] Python In-Reply-To: References: Message-ID: <520D7438.2050806@gmail.com> Python is one of the easier languages to read. Look at Alan's signature for a website that will help you learn it. Leam On 08/15/2013 08:30 PM, Alan Gauld wrote: > On 13/08/13 23:38, Jake Wohldmann wrote: >> Hello I am a beginner to using any type of script. How long does it >> take to become fluent at a script? > > It depends on what you mean by Fluent. > > I've spent 40+ years programming and am still learning. > OTOH I can usually learn enough about a new language to > start using it in a day. > > Its like learning a musical instrument. You can bang > out a few chords and play a recognizable melody pretty > quickly. To be a maestro takes a lifetime. > > Learn the basics then pick a project and go for it. > The more hands on you can get the better. > > -- http://31challenge.net http://31challenge.net/insight From alan.gauld at btinternet.com Fri Aug 16 02:35:01 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 16 Aug 2013 01:35:01 +0100 Subject: [Tutor] Reading a video In-Reply-To: References: Message-ID: On 15/08/13 14:54, Zoya Tavakkoli wrote: > How can read a video frame by frame in python? I'm not aware of anything that does it out of the box in the standard library(*) so you will likely need to use Google (or another equivalent) to find a third party module. But you will need to be much more specific. There are a lot of video formats out there. Which ones do you want to process? (*) But I'm always being surprised by what is in there! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From davea at davea.name Fri Aug 16 02:53:07 2013 From: davea at davea.name (Dave Angel) Date: Fri, 16 Aug 2013 00:53:07 +0000 (UTC) Subject: [Tutor] Python References: Message-ID: Jake Wohldmann wrote: > Hello I am a beginner to using any type of script. How long does it take > to become fluent at a script? I will only be able to practice python on > weekends since school is starting. I was also wondering if I could use > python on my android phone. > > >

Hello I am a beginner to using any type of script.? How long does it take to become fluent at a script?? I will only be able to practice python on weekends since school is starting.? I was also wondering if I could use python on my android phone.

> Please leave off the html part of your messages. Tell your email program to post text messages, not html. We cannot tell how long a particular person might take to really learn Python, when he has no previous experience in programming. It could be weeks of full time work, or it could be years. When I first studied a programming language it was at least 6 months, and probably 8 before I first got access to a computer. And that language wasn't available, so I had to learn a second. Neither of those was as easy as Python, which didn't come till decades later. Python was approximately the 35th language I've used at various jobs. There are apparently ways to get Python on Android, but I have no experience with it. https://code.google.com/p/python-for-android/ http://www.amazon.com/MI1982-QPython-Python-on-Android/dp/B00AP36S4Y http://qpython.com/ http://python-for-android.readthedocs.org/en/latest/android/ http://www.linuxjournal.com/article/10940 https://code.google.com/p/android-scripting/ -- DaveA From steve at pearwood.info Fri Aug 16 04:23:52 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 16 Aug 2013 12:23:52 +1000 Subject: [Tutor] Python In-Reply-To: References: Message-ID: <520D8D38.2000001@pearwood.info> On 14/08/13 08:38, Jake Wohldmann wrote: > Hello I am a beginner to using any type of script. How long does it take > to become fluent at a script? How long is a piece of string? The answer could be anything from "a couple of hours" to "forever". It depends on: - what scripting language are you using? (presumably Python) - how smart and focused are you? - how much effort and work are you willing and able to put it? - can you practice regularly, before you forget your last practice session? - do you have a natural inclination to programming? - have you ever programmed before? - what other programming languages do you know? - what level of knowledge do you consider "fluent"? If you're an intelligent, expert programmer with lots of experience in other languages, you could learn the basics of Python in a few minutes, and become fluent in hours. (Fluent, however, is not "expert".) If you can barely spell "PC" and spend all your time messing about on Youtube and Facebook, you'll never become fluent. Given that we don't know the answer to any of those questions except the first, the only honest answer is, it will take as long as it takes. But as an extremely rough rule of thumb, for the average person who has never programmed before but has an inclination towards programming, it probably takes about 1-200 hours of focused practice in a programming language to: - learn the syntax and functionality of the base language and built-in types; - become comfortable reading average code; - learn the more common standard library functions; - become comfortable searching the standard library for existing solutions instead of re-inventing the wheel; - become able to write your own code following the standards and idioms common in the language; which is what I consider moderately fluent. >I will only be able to practice python on > weekends since school is starting. I was also wondering if I could use > python on my android phone. Yes, but not trivially. First you have to be able to install software on your phone. Have you tried googling for "python android phone"? https://duckduckgo.com/html/?q=python+android+phone -- Steven From francois.dion at gmail.com Fri Aug 16 05:19:13 2013 From: francois.dion at gmail.com (Francois Dion) Date: Thu, 15 Aug 2013 23:19:13 -0400 Subject: [Tutor] Python In-Reply-To: References: Message-ID: On Aug 15, 2013 8:04 PM, "Jake Wohldmann" wrote: > > I was also wondering if I could use python on my android phone. > Http://qpython.com And then, there is the browser based approach. For example, see http://raspberry-python.blogspot.com That's using Brython and the interactive mode. Works on iPhone, iPad, android (tested it on my nexus 7 HD) Francois -- www.pyptug.com - raspberry-python.blogspot.com - @f_dion -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Fri Aug 16 05:53:07 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 16 Aug 2013 13:53:07 +1000 Subject: [Tutor] Reading a video In-Reply-To: References: Message-ID: <520DA223.1040003@pearwood.info> On 15/08/13 23:54, Zoya Tavakkoli wrote: > Hi > How can read a video frame by frame in python? Of course! Videos are just binary files, and Python can read binary files. The hard part is writing code that understands the internal structure of a video. There are many video formats (ogv, mp4, mov, avi, flv, mpg, asf, wmv, ram, to name just a few), and the internal structure of them will be different and not always documented anywhere, which may mean you have to reverse-engineer the format. Doing this in pure Python code will likely be slow, unless you use an optimizing compiler like PyPy. Your best best is to look for a library that already understands video formats. If there is no pre-existing Python library, there may be a C library with a Python interface. Of you can create your own Python interface, maybe using ctypes to talk to the library. But honestly, it's probably a huge amount of work for something that will likely be rather slow, unless you use PyPy, in which case it will just be a huge amount of work. It will be less work if you only care about one video format. What are you hoping to do with the frames once you get them? You may be better off using an external application like mplayer or ffmpeg to extract the frames to (say) png files, and then manipulate the images from Python, perhaps using PIL or similar. -- Steven From chris at chrisdown.name Fri Aug 16 06:06:33 2013 From: chris at chrisdown.name (Chris Down) Date: Fri, 16 Aug 2013 06:06:33 +0200 Subject: [Tutor] Simple Python SNMP ping? In-Reply-To: References: Message-ID: <20130816040633.GA871@gopher> Hi Leam, On 2013-08-14 15:21, leam hall wrote: > Is there a way to do a simple check in Python to see if a remote host is > listening on SNMP port 161/UDP? "Simple" in this case could either mean technically simple (in which case, use a socket with SOCK_DGRAM and wait for data) or implementationally simple (in which case, use an SNMP library). I'd only recommend doing the latter. Since UDP is stateless, you'll need to make sure that your destination replies with something, which means you probably need to send a real SNMP request. Since that's the case, you should really just use a real SNMP library (although I fear that your meaning of "simple" was "not using an SNMP library", which is really not simple at all. net-snmp[0] can do this fairly trivially, anyway. The following works for me: >>> import netsnmp >>> var = netsnmp.Varbind('sysDescr.0') >>> netsnmp.snmpget( ... var, ... Version=2, ... DestHost="localhost", ... Community="pub", ... ) ('OpenBSD',) Best, Chris 0: http://www.net-snmp.org/wiki/index.php/Python_Bindings -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From cybervigilante at gmail.com Fri Aug 16 10:01:38 2013 From: cybervigilante at gmail.com (Jim Mooney) Date: Fri, 16 Aug 2013 01:01:38 -0700 Subject: [Tutor] Reading a video In-Reply-To: <520DA223.1040003@pearwood.info> References: <520DA223.1040003@pearwood.info> Message-ID: http://code.google.com/p/pyffmpeg/ PyFFmpeg has stayed in minimal status where it could be used to extract individual frames from video files and create PIL image objects from them. While there were several Python libraries that offer to play video files most of them did not provide any API to extract individual frames, and this is the reason PyFFmpeg was created. Jim On 15 August 2013 20:53, Steven D'Aprano wrote: > On 15/08/13 23:54, Zoya Tavakkoli wrote: >> >> Hi >> How can read a video frame by frame in python? > > > Of course! Videos are just binary files, and Python can read binary files. > The hard part is writing code that understands the internal structure of a > video. There are many video formats (ogv, mp4, mov, avi, flv, mpg, asf, wmv, > ram, to name just a few), and the internal structure of them will be > different and not always documented anywhere, which may mean you have to > reverse-engineer the format. > > Doing this in pure Python code will likely be slow, unless you use an > optimizing compiler like PyPy. > > Your best best is to look for a library that already understands video > formats. If there is no pre-existing Python library, there may be a C > library with a Python interface. Of you can create your own Python > interface, maybe using ctypes to talk to the library. > > But honestly, it's probably a huge amount of work for something that will > likely be rather slow, unless you use PyPy, in which case it will just be a > huge amount of work. It will be less work if you only care about one video > format. > > What are you hoping to do with the frames once you get them? You may be > better off using an external application like mplayer or ffmpeg to extract > the frames to (say) png files, and then manipulate the images from Python, > perhaps using PIL or similar. > > > -- > Steven > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Jim "If you don't know it's impossible, it's easier to do." --Neil Gaiman "The Process is not the Picture...Reality can only be proved to be weakly-objective. Strong objectivity is a myth." --Bernardo Kastrup "You cannot use logic to justify logic, so logic itself has no basis other than faith." --Agrippa From leamhall at gmail.com Fri Aug 16 16:38:09 2013 From: leamhall at gmail.com (leam hall) Date: Fri, 16 Aug 2013 10:38:09 -0400 Subject: [Tutor] Simple Python SNMP ping? In-Reply-To: <20130816040633.GA871@gopher> References: <20130816040633.GA871@gopher> Message-ID: One of the things I forgot to mention is that my host is using Python 2.4 and net-snmp 5.3; both packaged by a major North American Linux vendor. :) So there doesn't seem to be a native net-snmp tool set for these versions. I'll poke around and see what else can be done. Thanks! On Fri, Aug 16, 2013 at 12:06 AM, Chris Down wrote: > Hi Leam, > > On 2013-08-14 15:21, leam hall wrote: > > Is there a way to do a simple check in Python to see if a remote host is > > listening on SNMP port 161/UDP? > > "Simple" in this case could either mean technically simple (in which case, > use > a socket with SOCK_DGRAM and wait for data) or implementationally simple > (in > which case, use an SNMP library). I'd only recommend doing the latter. > > Since UDP is stateless, you'll need to make sure that your destination > replies > with something, which means you probably need to send a real SNMP request. > Since that's the case, you should really just use a real SNMP library > (although > I fear that your meaning of "simple" was "not using an SNMP library", > which is > really not simple at all. > > net-snmp[0] can do this fairly trivially, anyway. The following works for > me: > > >>> import netsnmp > >>> var = netsnmp.Varbind('sysDescr.0') > >>> netsnmp.snmpget( > ... var, > ... Version=2, > ... DestHost="localhost", > ... Community="pub", > ... ) > ('OpenBSD',) > > Best, > > Chris > > 0: http://www.net-snmp.org/wiki/index.php/Python_Bindings > -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From leamhall at gmail.com Fri Aug 16 16:42:10 2013 From: leamhall at gmail.com (leam hall) Date: Fri, 16 Aug 2013 10:42:10 -0400 Subject: [Tutor] Input into lists? In-Reply-To: References: Message-ID: Hey Dave, you're right. I worked through this with a for loop and set the contents of a dict's key as the input. Thanks! Leam On Thu, Aug 15, 2013 at 8:28 PM, Dave Angel wrote: > leam hall wrote: > > > I'm trying to take input from a file (CSV spreadsheet) and use the first > > line inputs (header) as the names of lists. So far I'm not successful. > :) > > > > How do I take line_list[0], the result of "line.split(',')" and use it as > > the name of a list? Does that make sense? > > > > No, it doesn't make sense. You're trying to define a variable that has > a name determined not by your source, but by a name in line 0 of the csv > file. How then are you going to use that variable? > > There are two possibilities: Either you actually know those names and > are using them in your code, or the names won't be known till runtime. > > If the latter, then make a dictionary of lists, > > mydict = {} > for item in line_list: > mydict[item] = [] > > and then populate those lists from each line of the file. > > Now, if you're sure you know what the names are to be, then: > > names = mydict["name"] > addresses = mydict["address"] > > or whatever. > > > > > -- > DaveA > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From arunkumar413 at gmail.com Sat Aug 17 17:59:53 2013 From: arunkumar413 at gmail.com (Arun Kumar) Date: Sat, 17 Aug 2013 21:29:53 +0530 Subject: [Tutor] I need a good resource for python Django Message-ID: Hi, Can anyone suggest me a good resource for python Django. I've gone through the official website of Django but it is of limited use to me. Any help on this would be highly appreciated. Regards, Arun Kumar http://clicknscroll.blogspot.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Sat Aug 17 18:17:06 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sat, 17 Aug 2013 12:17:06 -0400 Subject: [Tutor] I need a good resource for python Django In-Reply-To: References: Message-ID: On Sat, Aug 17, 2013 at 11:59 AM, Arun Kumar wrote: > Hi, > > Can anyone suggest me a good resource for python Django. I've gone through > the official website of Django but it is of limited use to me. Any help on > this would be highly appreciated. > My view is that the djangoproject.com is a fantastic site. So, what is it that you need to learn about django that you haven't found? > Regards, > Arun Kumar > http://clicknscroll.blogspot.com > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick http://joelgoldstick.com From hrs6 at njit.edu Sun Aug 18 07:18:53 2013 From: hrs6 at njit.edu (Savla, Haem) Date: Sun, 18 Aug 2013 00:18:53 -0500 Subject: [Tutor] I need a good resource for python Django In-Reply-To: References: Message-ID: I am a fucking douchebag! I like beating up my girlfriend. On Saturday, August 17, 2013, Joel Goldstick wrote: > On Sat, Aug 17, 2013 at 11:59 AM, Arun Kumar > > wrote: > > Hi, > > > > Can anyone suggest me a good resource for python Django. I've gone > through > > the official website of Django but it is of limited use to me. Any help > on > > this would be highly appreciated. > > > My view is that the djangoproject.com is a fantastic site. So, what > is it that you need to learn about django that you haven't found? > > > > Regards, > > Arun Kumar > > http://clicknscroll.blogspot.com > > > > > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > > > > > -- > Joel Goldstick > http://joelgoldstick.com > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From meher.tanmay3 at gmail.com Sat Aug 17 19:02:32 2013 From: meher.tanmay3 at gmail.com (Tanmaya Meher) Date: Sat, 17 Aug 2013 22:32:32 +0530 Subject: [Tutor] I need a good resource for python Django In-Reply-To: References: Message-ID: hey Arun ! U can check the below sites: 1. https://code.djangoproject.com/wiki/Tutorials (u will get lots of tuts and examples to do) 2. http://lightbird.net/dbe/ (Teaches by examples, A very good tut) 3. https://www.udemy.com/full-django-tutorial/ (complete video tuts from django developer) 4. http://gettingstartedwithdjango.com/ (good site, teaches u a bit slowly but through different kind of examples) 5. http://effectivedjango.com/index.html#contents (This one is also a good tut) On Sat, Aug 17, 2013 at 9:47 PM, Joel Goldstick wrote: > On Sat, Aug 17, 2013 at 11:59 AM, Arun Kumar > wrote: > > Hi, > > > > Can anyone suggest me a good resource for python Django. I've gone > through > > the official website of Django but it is of limited use to me. Any help > on > > this would be highly appreciated. > > > My view is that the djangoproject.com is a fantastic site. So, what > is it that you need to learn about django that you haven't found? > > > > Regards, > > Arun Kumar > > http://clicknscroll.blogspot.com > > > > > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > > > > > -- > Joel Goldstick > http://joelgoldstick.com > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Tanmaya Meher M.Tech., School of Computaional and Integrative Sciences, JNU New Delhi -110067* -------------- next part -------------- An HTML attachment was scrubbed... URL: From jake.wohldmann at gmail.com Fri Aug 16 03:34:27 2013 From: jake.wohldmann at gmail.com (Jake Wohldmann) Date: Thu, 15 Aug 2013 20:34:27 -0500 Subject: [Tutor] Python Message-ID: Hello I am a beginner to using any type of programming lanhuage How long does it take to become fluent at a script? Do you know and good sites to learn python? I will only be able to practice python on weekends since school is starting. I was also wondering if I could use python on my android phone. -------------- next part -------------- An HTML attachment was scrubbed... URL: From wayne at waynewerner.com Sun Aug 18 19:10:34 2013 From: wayne at waynewerner.com (Wayne Werner) Date: Sun, 18 Aug 2013 12:10:34 -0500 (CDT) Subject: [Tutor] Python In-Reply-To: References: Message-ID: On Thu, 15 Aug 2013, Jake Wohldmann wrote: > > Hello I am a beginner to using any type of programming lanhuage? How long > does it take to become fluent at a script? Do you know and good sites to > learn python?? I will only be able to practice python on weekends since > school is starting.? I was also wondering if I could use python on my > android phone. The official python tutorial is pretty handy - www.python.org Also if you can install 3rd party APKs, search for the SL4a (Scripting Layer for Android). You can install Python3 (recommended - the future of Python), or Python2.7 (current version). Not all 3rd party modules are available for Python3, but most 3rd party modules aren't available on Android anyway so you wouldn't be missing out too much. HTH, Wayne From alan.gauld at btinternet.com Mon Aug 19 00:36:33 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 18 Aug 2013 23:36:33 +0100 Subject: [Tutor] Python In-Reply-To: References: Message-ID: On 16/08/13 02:34, Jake Wohldmann wrote: > Hello I am a beginner to using any type of programming lanhuage How > long does it take to become fluent at a script? Do you know and good > sites to learn python? I will only be able to practice python on > weekends since school is starting. I was also wondering if I could use > python on my android phone. You asked an almost identical question 3 days ago. Did you read the answers you received then? They should cover all that you have asked. If you have more specific questions then feel free to ask them. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From shanmukhateja at gmail.com Sun Aug 18 12:40:14 2013 From: shanmukhateja at gmail.com (shanmukhateja at gmail.com) Date: Sun, 18 Aug 2013 10:40:14 +0000 Subject: [Tutor] =?utf-8?q?http=2Eserver_--_stuck_at_binding_=5Bwindows8?= =?utf-8?q?=5D?= Message-ID: <5210a597.e6d5440a.62f2.ffffa8ad@mx.google.com> Hello, I am developing a simple http media streamer with the help of simple http server of http.server and I have a problem in binding the server. I get the ?the address is not valid in it?s content error? and I KNOW that it can be solved by socket.INADDR_ANY but the problem is my router has assigned me 192.168.1.*** and my ISP has given me a ip like 119.*.*.* which is causing the problem.. I find this 119.*.*.* IP nowhere in ipconfig [neither with the /all flag] How can I solve this puzzle??? Kindly help me out -------------- next part -------------- An HTML attachment was scrubbed... URL: From zoyatvkl at gmail.com Sun Aug 18 19:18:06 2013 From: zoyatvkl at gmail.com (Zoya Tavakkoli) Date: Sun, 18 Aug 2013 10:18:06 -0700 Subject: [Tutor] Error :SyntaxError: 'break' outside loop Message-ID: Hi everyone I write this code for color segmentation ,but after Run I recived this error: SyntaxError: 'break' outside loop I could not resolve that ,could you please help me? python.2.7.5 import cv2 import numpy as np cap = cv2.VideoCapture("C:\Users\Zohreh\Desktop\Abdomen.MST") while (1): _, frame = cap.read() hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) lower_blue = np.array([110,50,50]) upper_blue = np.array([130,255,255]) mask = cv2.inRange(hsv, lower_blue, upper_blue) res = cv2.bitwise_and(frame,frame, mask= mask) cv2.imshow("frame",frame) cv2.imshow("mask",mask) cv2.imshow("res",res) k = cv2.waitkey(5) & 0xFF if k == 27: break cv2.destroyWindow() -------------- next part -------------- An HTML attachment was scrubbed... URL: From chris at chrisdown.name Mon Aug 19 10:55:31 2013 From: chris at chrisdown.name (Chris Down) Date: Mon, 19 Aug 2013 10:55:31 +0200 Subject: [Tutor] Error :SyntaxError: 'break' outside loop In-Reply-To: References: Message-ID: <20130819085530.GA717@gopher> On 2013-08-18 10:18, Zoya Tavakkoli wrote: > if k == 27: > > break Well, you're not in a function here, so break doesn't make any sense. What is it that you want to do? -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From chris at chrisdown.name Mon Aug 19 10:55:55 2013 From: chris at chrisdown.name (Chris Down) Date: Mon, 19 Aug 2013 10:55:55 +0200 Subject: [Tutor] Error :SyntaxError: 'break' outside loop In-Reply-To: <20130819085530.GA717@gopher> References: <20130819085530.GA717@gopher> Message-ID: <20130819085555.GB717@gopher> On 2013-08-19 10:55, Chris Down wrote: > On 2013-08-18 10:18, Zoya Tavakkoli wrote: > > if k == 27: > > > > break > > Well, you're not in a function here, so break doesn't make any sense. What is > it that you want to do? s/function/loop/ -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From chris at chrisdown.name Mon Aug 19 12:05:57 2013 From: chris at chrisdown.name (Chris Down) Date: Mon, 19 Aug 2013 12:05:57 +0200 Subject: [Tutor] http.server -- stuck at binding [windows8] In-Reply-To: <5210a597.e6d5440a.62f2.ffffa8ad@mx.google.com> References: <5210a597.e6d5440a.62f2.ffffa8ad@mx.google.com> Message-ID: <20130819100556.GC717@gopher> Hello, On 2013-08-18 10:40, shanmukhateja at gmail.com wrote: > I am developing a simple http media streamer with the help of simple http > server of http.server and I have a problem in binding the server. I get the > ?the address is not valid in it?s content error? and I KNOW that it can be > solved by socket.INADDR_ANY but the problem is my router has assigned me > 192.168.1.*** and my ISP has given me a ip like 119.*.*.* which is causing > the problem.. I find this 119.*.*.* IP nowhere in ipconfig [neither with the > /all flag] Your problem is that your computer is behind a NAT provided by your router. Essentially, to provide access to multiple devices on your network while still only using a single external IP address, your router uses network(s) that are private to your LAN and then attempts to map calls from the outside to IPs on the inside. The usual way to handle it would be to bind to your local WAN-facing interface and then port forward on your router, however this is off-topic for this list. This is fairly facile though, if you google for "port forward" and your router model, you should find instructions on how to do so. Best, Chris -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From steve at pearwood.info Mon Aug 19 12:48:54 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 19 Aug 2013 20:48:54 +1000 Subject: [Tutor] Error :SyntaxError: 'break' outside loop In-Reply-To: References: Message-ID: <20130819104854.GA9487@ando> Hello Zoya, and welcome! On Sun, Aug 18, 2013 at 10:18:06AM -0700, Zoya Tavakkoli wrote: > Hi everyone > > I write this code for color segmentation ,but after Run I recived this > error: > > SyntaxError: 'break' outside loop > > I could not resolve that ,could you please help me? Is the error message not clear enough? You have a 'break' command outside of a loop. If the error message is not clear enough, please suggest something that would be more clear. Also, you should read the entire traceback, which will show you not just the error, but the exact line causing the problem. For example, these two are legal: # Legal while x > 100: if y == 0: break ... # Also legal for i in range(1000): if x > 20: break ... But not this: # Not legal if x > 20: break You can't break out of a loop, if you aren't inside a loop to begin with. > while (1): > _, frame = cap.read() You have a while loop with only one line. It loops forever, and never exits. Are you sure that's what you want? By the way, it is much, much, much easier to see indentation when you make it four spaces or eight. Good programmer's editors let you set indentation to four or eight spaces. Even Notepad, which is *not* a programmer's editor, lets you hit the Tab key and indent. > hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) Because this line is not indented, it is not inside the loop. > if k == 27: > > break Again, because the "if" line is not indented, it is outside the loop, and so the break is illegal. -- Steven From kwpolska at gmail.com Mon Aug 19 12:58:06 2013 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Mon, 19 Aug 2013 12:58:06 +0200 Subject: [Tutor] Fwd: http.server -- stuck at binding [windows8] In-Reply-To: References: <5210a597.e6d5440a.62f2.ffffa8ad@mx.google.com> Message-ID: gaah, forgot to reply-all. ---------- Forwarded message ---------- From: Chris ?Kwpolska? Warrick Date: Mon, Aug 19, 2013 at 11:13 AM Subject: Re: [Tutor] http.server -- stuck at binding [windows8] To: shanmukhateja at gmail.com On Sun, Aug 18, 2013 at 12:40 PM, wrote: > Hello, > I am developing a simple http media streamer with the help of > simple http server of http.server and I have a problem in binding the > server. I get the ?the address is not valid in it?s content error? and I > KNOW that it can be solved by socket.INADDR_ANY but the problem is my router > has assigned me 192.168.1.*** and my ISP has given me a ip like 119.*.*.* > which is causing the problem.. I find this 119.*.*.* IP nowhere in ipconfig > [neither with the /all flag] > > How can I solve this puzzle??? > > Kindly help me out > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > 192.168.1.* belongs to one of the three private IP ranges, it is not accessible outside of your network. If you want to stream outside of your home network, you must set up port forwarding. 1. Configure a static IP address for your computer (in the properties of your NIC, Google ?Windows 8 static IP address? for more info). You can use any of the 17.8 million available private addresses, but you are better off by using whatever your router gave you + 10 (a safe margin to ensure DHCP won?t give the address to someone else; e.g. 192.168.1.25 turns into 192.168.1.35) 2. Log into your router and configure port forwarding. Where you can conifigure this depends on your router. Once again, Google should be of help in this case. -- Chris ?Kwpolska? Warrick PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense From russel at winder.org.uk Mon Aug 19 20:41:33 2013 From: russel at winder.org.uk (Russel Winder) Date: Mon, 19 Aug 2013 19:41:33 +0100 Subject: [Tutor] I need a good resource for python Django In-Reply-To: References: Message-ID: <1376937693.6253.39.camel@anglides.winder.org.uk> On Sat, 2013-08-17 at 21:29 +0530, Arun Kumar wrote: > Hi, > > Can anyone suggest me a good resource for python Django. I've gone through > the official website of Django but it is of limited use to me. Any help on > this would be highly appreciated. You have given no indication as to why you consider the website not to have given you what you need so we have no information to try and direct you to useful material. Others have provided so URL of other material, but, for me, the best "Let's Learn Django" website has to be Harry Percival's Test-Driven Django Tutorial, it emphasizes TDD and tests where the Django site rarely mentions them in the introductory material (a gross oversight). http://www.tdd-django-tutorial.com/ -- Russel. ============================================================================= Dr Russel Winder t: +44 20 7585 2200 voip: sip:russel.winder at ekiga.net 41 Buckmaster Road m: +44 7770 465 077 xmpp: russel at winder.org.uk London SW11 1EN, UK w: www.russel.org.uk skype: russel_winder -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 198 bytes Desc: This is a digitally signed message part URL: From engg at cleantechsolution.in Mon Aug 19 15:16:07 2013 From: engg at cleantechsolution.in (Engineering) Date: Mon, 19 Aug 2013 18:46:07 +0530 Subject: [Tutor] . http.server -- stuck at binding [windows8] Message-ID: <000201ce9cde$492d2050$db8760f0$@in> Since you are behind a router , ipconfig will only show the address of your machine which has been given by the router. Ipconfig cannot see beyond the router. If you work within your own LAN , the IP address of your machine is sufficient for the socket. If you want to access from outside , there should be port forwarding enabled on your router. One of my personal interest . I have done an HTTP server on Python using twisted and autobahn to control my arduino micro controller. But , I want to stream media using it for my OpenCV to access it. How are you streaming videos? CLEANTECH SOLUTION www.cleantechsolution.in SAVE PAPER , SAVE EARTH -------------- next part -------------- An HTML attachment was scrubbed... URL: From leamhall at gmail.com Tue Aug 20 02:02:25 2013 From: leamhall at gmail.com (Leam Hall) Date: Mon, 19 Aug 2013 20:02:25 -0400 Subject: [Tutor] Coursera Python Course starts today Message-ID: <5212B211.5050900@gmail.com> Hey all, In case I'm not the absolute last person to know, the newest edition of the Coursera "Learn to Program" course started today. It is Python based, free, lasts 7 weeks, and pretty fun. I didn't get a certificate last time as life got in the way. Hope to succeed this time. https://class.coursera.org/programming1-002/class/index Leam -- http://31challenge.net http://31challenge.net/insight From leamhall at gmail.com Tue Aug 20 02:18:06 2013 From: leamhall at gmail.com (Leam Hall) Date: Mon, 19 Aug 2013 20:18:06 -0400 Subject: [Tutor] [OT] Replies go to individuals, not the list? Message-ID: <5212B5BE.1060604@gmail.com> All, Am I more confused than normal or if I click "Reply" should it go just to the sender instead of the list? Leam -- http://31challenge.net http://31challenge.net/insight From steve at pearwood.info Tue Aug 20 03:43:26 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 20 Aug 2013 11:43:26 +1000 Subject: [Tutor] [OT] Replies go to individuals, not the list? In-Reply-To: <5212B5BE.1060604@gmail.com> References: <5212B5BE.1060604@gmail.com> Message-ID: <5212C9BE.4040602@pearwood.info> On 20/08/13 10:18, Leam Hall wrote: > All, > > Am I more confused than normal or if I click "Reply" should it go just to the sender instead of the list? Ah, the perennial argument about From address munging! Google for "From address munging considered helpful" and "From address munging considered harmful" for more information. https://duckduckgo.com/html/?q=from+address+munging+considered+helpful https://duckduckgo.com/html/?q=from+address+munging+considered+harmful In a nutshell, when you send an email to a mailing list, some mailing lists leave the From address to your email address, while others edit the From address to be the mailing list itself. This mailing list performs no munging, so on this list: Reply -> sender only Reply All -> sender, and the list CCed Reply To List (if your email program supports this command) -> list only -- Steven From barliant at gmail.com Tue Aug 20 08:13:07 2013 From: barliant at gmail.com (Anung Ariwibowo) Date: Tue, 20 Aug 2013 13:13:07 +0700 Subject: [Tutor] Coursera Python Course starts today In-Reply-To: <5212B211.5050900@gmail.com> References: <5212B211.5050900@gmail.com> Message-ID: This should go to the list Let's discuss our progress then, if it is allowed by list rules. I was just retake the course as well. Regards, Anung On 8/20/13, Leam Hall wrote: > Hey all, > > In case I'm not the absolute last person to know, the newest edition of > the Coursera "Learn to Program" course started today. It is Python > based, free, lasts 7 weeks, and pretty fun. I didn't get a certificate > last time as life got in the way. Hope to succeed this time. > > https://class.coursera.org/programming1-002/class/index > > Leam > > > -- > http://31challenge.net > http://31challenge.net/insight > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From barliant at gmail.com Tue Aug 20 08:14:24 2013 From: barliant at gmail.com (Anung Ariwibowo) Date: Tue, 20 Aug 2013 13:14:24 +0700 Subject: [Tutor] Coursera Python Course starts today In-Reply-To: <5212B582.6070005@gmail.com> References: <5212B211.5050900@gmail.com> <1420783636-1376957527-cardhu_decombobulator_blackberry.rim.net-669752004-@b1.c2.bise3.blackberry> <5212B582.6070005@gmail.com> Message-ID: Just started today before catching bus to work. I will start this evening. Regards, Anung On 8/20/13, Leam Hall wrote: > A little cooperation is motivating. I have finished up Week 1 but I was > a few minutes late for work this morning! :) > > You? > > Leam > > On 08/19/2013 08:12 PM, barliant at gmail.com wrote: >> Let's discuss our progress then, if it is allowed by list rules. I was >> just retake the course as well. >> >> Regards, >> Anung >> >> Sent from my BlackBerry? smartphone from Sinyal Bagus XL, Nyambung >> Teruuusss...! >> >> -----Original Message----- >> From: Leam Hall >> Sender: "Tutor" Date: Mon, 19 >> Aug 2013 20:02:25 >> To: >> Subject: [Tutor] Coursera Python Course starts today >> >> Hey all, >> >> In case I'm not the absolute last person to know, the newest edition of >> the Coursera "Learn to Program" course started today. It is Python >> based, free, lasts 7 weeks, and pretty fun. I didn't get a certificate >> last time as life got in the way. Hope to succeed this time. >> >> https://class.coursera.org/programming1-002/class/index >> >> Leam >> > -- > http://31challenge.net > http://31challenge.net/insight From alan.gauld at btinternet.com Tue Aug 20 10:13:12 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 20 Aug 2013 09:13:12 +0100 Subject: [Tutor] [OT] Replies go to individuals, not the list? In-Reply-To: <5212B5BE.1060604@gmail.com> References: <5212B5BE.1060604@gmail.com> Message-ID: On 20/08/13 01:18, Leam Hall wrote: > Am I more confused than normal or if I click "Reply" should it go just > to the sender instead of the list? Reply replies to the sender. Reply All replies to the whole list Some mail tools also have a Reply List feature which also replies to the list. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From petluke at gmail.com Tue Aug 20 10:31:37 2013 From: petluke at gmail.com (Luke Pettit) Date: Tue, 20 Aug 2013 18:31:37 +1000 Subject: [Tutor] Coursera Python Course starts today In-Reply-To: References: <5212B211.5050900@gmail.com> <1420783636-1376957527-cardhu_decombobulator_blackberry.rim.net-669752004-@b1.c2.bise3.blackberry> <5212B582.6070005@gmail.com> Message-ID: https://plus.google.com/u/0/communities/100767777754413628630 Is a G+ community for that very course feel free to join On 20 August 2013 16:14, Anung Ariwibowo wrote: > Just started today before catching bus to work. I will start this evening. > > Regards, > Anung > > > On 8/20/13, Leam Hall wrote: > > A little cooperation is motivating. I have finished up Week 1 but I was > > a few minutes late for work this morning! :) > > > > You? > > > > Leam > > > > On 08/19/2013 08:12 PM, barliant at gmail.com wrote: > >> Let's discuss our progress then, if it is allowed by list rules. I was > >> just retake the course as well. > >> > >> Regards, > >> Anung > >> > >> Sent from my BlackBerry? smartphone from Sinyal Bagus XL, Nyambung > >> Teruuusss...! > >> > >> -----Original Message----- > >> From: Leam Hall > >> Sender: "Tutor" Date: > Mon, 19 > >> Aug 2013 20:02:25 > >> To: > >> Subject: [Tutor] Coursera Python Course starts today > >> > >> Hey all, > >> > >> In case I'm not the absolute last person to know, the newest edition of > >> the Coursera "Learn to Program" course started today. It is Python > >> based, free, lasts 7 weeks, and pretty fun. I didn't get a certificate > >> last time as life got in the way. Hope to succeed this time. > >> > >> https://class.coursera.org/programming1-002/class/index > >> > >> Leam > >> > > -- > > http://31challenge.net > > http://31challenge.net/insight > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Luke Pettit ,,, ^..^,,, http://lukepettit-3d.blogspot.com/ -------------- next part -------------- An HTML attachment was scrubbed... URL: From shanmukhateja at gmail.com Tue Aug 20 07:00:56 2013 From: shanmukhateja at gmail.com (Shanmukha Surya Teja) Date: Tue, 20 Aug 2013 10:30:56 +0530 Subject: [Tutor] . http.server -- stuck at binding [windows8] In-Reply-To: <000201ce9cde$492d2050$db8760f0$@in> References: <000201ce9cde$492d2050$db8760f0$@in> Message-ID: Actually I am using the Simple HTTP Server class to implement it but instead of redirecting to a html file, I am redirecting it to a media file :P use google my friend...if ur still unable to do it, I'll post the code as a reply to that message On Mon, Aug 19, 2013 at 6:46 PM, Engineering wrote: > Since you are behind a router , ipconfig will only show the address of > your machine which has been given by the router. Ipconfig cannot see beyond > the router. **** > > If you work within your own LAN , the IP address of your machine is > sufficient for the socket. **** > > If you want to access from outside , there should be port forwarding > enabled on your router.**** > > ** ** > > One of my personal interest . I have done an HTTP server on Python using > twisted and autobahn to control my arduino micro controller. But , I want > to stream media using it for my OpenCV to access it. **** > > ** ** > > How are you streaming videos?**** > > ** ** > > ** ** > > ** ** > > *CLEANTECH SOLUTION* > > *www.cleantechsolution.in* > > * * > > SAVE PAPER , SAVE EARTH**** > > * * > > ** ** > -- Surya Teja -------------- next part -------------- An HTML attachment was scrubbed... URL: From zubair.alam.jmi at gmail.com Tue Aug 20 08:15:50 2013 From: zubair.alam.jmi at gmail.com (zubair alam) Date: Tue, 20 Aug 2013 11:45:50 +0530 Subject: [Tutor] Coursera Python Course starts today In-Reply-To: References: <5212B211.5050900@gmail.com> Message-ID: i signed up for the course...lets see how much i gain from this. On Tue, Aug 20, 2013 at 11:43 AM, Anung Ariwibowo wrote: > This should go to the list > > Let's discuss our progress then, if it is allowed by list rules. I was > just retake the course as well. > > Regards, > Anung > > > On 8/20/13, Leam Hall wrote: > > Hey all, > > > > In case I'm not the absolute last person to know, the newest edition of > > the Coursera "Learn to Program" course started today. It is Python > > based, free, lasts 7 weeks, and pretty fun. I didn't get a certificate > > last time as life got in the way. Hope to succeed this time. > > > > https://class.coursera.org/programming1-002/class/index > > > > Leam > > > > > > -- > > http://31challenge.net > > http://31challenge.net/insight > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From zubair.alam.jmi at gmail.com Tue Aug 20 08:17:53 2013 From: zubair.alam.jmi at gmail.com (zubair alam) Date: Tue, 20 Aug 2013 11:47:53 +0530 Subject: [Tutor] Coursera Python Course starts today In-Reply-To: References: <5212B211.5050900@gmail.com> <1420783636-1376957527-cardhu_decombobulator_blackberry.rim.net-669752004-@b1.c2.bise3.blackberry> <5212B582.6070005@gmail.com> Message-ID: @anung..have you started this course? On Tue, Aug 20, 2013 at 11:44 AM, Anung Ariwibowo wrote: > Just started today before catching bus to work. I will start this evening. > > Regards, > Anung > > > On 8/20/13, Leam Hall wrote: > > A little cooperation is motivating. I have finished up Week 1 but I was > > a few minutes late for work this morning! :) > > > > You? > > > > Leam > > > > On 08/19/2013 08:12 PM, barliant at gmail.com wrote: > >> Let's discuss our progress then, if it is allowed by list rules. I was > >> just retake the course as well. > >> > >> Regards, > >> Anung > >> > >> Sent from my BlackBerry? smartphone from Sinyal Bagus XL, Nyambung > >> Teruuusss...! > >> > >> -----Original Message----- > >> From: Leam Hall > >> Sender: "Tutor" Date: > Mon, 19 > >> Aug 2013 20:02:25 > >> To: > >> Subject: [Tutor] Coursera Python Course starts today > >> > >> Hey all, > >> > >> In case I'm not the absolute last person to know, the newest edition of > >> the Coursera "Learn to Program" course started today. It is Python > >> based, free, lasts 7 weeks, and pretty fun. I didn't get a certificate > >> last time as life got in the way. Hope to succeed this time. > >> > >> https://class.coursera.org/programming1-002/class/index > >> > >> Leam > >> > > -- > > http://31challenge.net > > http://31challenge.net/insight > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From amckenzie4 at gmail.com Tue Aug 20 14:15:39 2013 From: amckenzie4 at gmail.com (Andy McKenzie) Date: Tue, 20 Aug 2013 08:15:39 -0400 Subject: [Tutor] [OT] Replies go to individuals, not the list? In-Reply-To: <5212B5BE.1060604@gmail.com> References: <5212B5BE.1060604@gmail.com> Message-ID: On Mon, Aug 19, 2013 at 8:18 PM, Leam Hall wrote: > All, > > Am I more confused than normal or if I click "Reply" should it go just to > the sender instead of the list? > Yep. Someone decided it didn't make sense for "reply" to go to the list that sent the message and should be receiving the reply. I've never understood it, even after reading the arguments in favor. Andy -------------- next part -------------- An HTML attachment was scrubbed... URL: From FowlerTM at hendrix.edu Tue Aug 20 15:22:57 2013 From: FowlerTM at hendrix.edu (Fowler, Trent) Date: Tue, 20 Aug 2013 08:22:57 -0500 Subject: [Tutor] python tutoring Message-ID: Hello, Not long ago I came across the website of a professional programmer offering python tutoring services: http://www.jeffknupp.com/python-tutoring/ I have attempted to contact him because I am interested but I've been unable to get in touch. I was wondering if anyone knew of people offering similar services. I am a self-starter and highly motivated, but I live in a small town in South Korea and I don't have any friends who program. Since I also don't have a computer science background and python is my first language, I really need someone who can help me with the beginning stages. Often times when I run into a problem not only do I not know how to solve it, I don't even know how to ask the questions that will help someone else solve it. I don't want to be spoon-fed, just gently nudged and guided. I'm on a budget but I'd be willing to pay for a good teacher. Preliminary googling has turned up precious little, so I thought someone here might be able to point me in the right direction. Thanks, -Trent. From nielsen.jared at gmail.com Tue Aug 20 16:34:37 2013 From: nielsen.jared at gmail.com (Jared Nielsen) Date: Tue, 20 Aug 2013 07:34:37 -0700 Subject: [Tutor] I need a good resource for python Django Message-ID: > Hi, > > Can anyone suggest me a good resource for python Django. I've gone through > the official website of Django but it is of limited use to me. Any help on > this would be highly appreciated. I recommend Mike Hibbert's YouTube series on Django. http://www.youtube.com/watch?v=oT1A1KKf0SI&feature=c4-overview-vl&list=PLxxA5z-8B2xk4szCgFmgonNcCboyNneMD It's the most comprehensive and easy to follow tutorial I've found. Otherwise go to the Google django-users group and search. Someone asks this question everyday. -- http://jarednielsen.com http://thehelloworldprogram.com http://dototot.com -------------- next part -------------- An HTML attachment was scrubbed... URL: From ramit.prasad at jpmorgan.com.dmarc.invalid Tue Aug 20 16:52:21 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Tue, 20 Aug 2013 14:52:21 +0000 Subject: [Tutor] python tutoring In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF47418678CF3@SCACMX008.exchad.jpmchase.net> Fowler, Trent wrote: > > Hello, > > Not long ago I came across the website of a professional programmer offering python tutoring services: > > http://www.jeffknupp.com/python-tutoring/ > > I have attempted to contact him because I am interested but I've been unable to get in touch. I was > wondering if anyone knew of people offering similar services. I am a self-starter and highly > motivated, but I live in a small town in South Korea and I don't have any friends who program. Since > I also don't have a computer science background and python is my first language, I really need someone > who can help me with the beginning stages. Often times when I run into a problem not only do I not > know how to solve it, I don't even know how to ask the questions that will help someone else solve it. > > I don't want to be spoon-fed, just gently nudged and guided. I'm on a budget but I'd be willing to > pay for a good teacher. Preliminary googling has turned up precious little, so I thought someone here > might be able to point me in the right direction. > > Thanks, > > -Trent. Why not just post your questions on here? I mean, that *is* the purpose of this list. There are some very excellent tutors on here which has the advantage of not being limited to only one tutor's level of experience. Not to mention it is free. :) I would recommend reading http://www.catb.org/esr/faqs/smart-questions.html, posting in plain text and bottom or in-line posting. In general, try and reduce the amount of code to the smallest example you can. Provide Python version, operating system, code, input, expected output, actual output, and any exceptions with full trace (copy and paste the full message, not paraphrasing or retyping). Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From oscar.j.benjamin at gmail.com Tue Aug 20 17:56:51 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 20 Aug 2013 16:56:51 +0100 Subject: [Tutor] [OT] Replies go to individuals, not the list? In-Reply-To: References: <5212B5BE.1060604@gmail.com> Message-ID: On 20 August 2013 13:15, Andy McKenzie wrote: > On Mon, Aug 19, 2013 at 8:18 PM, Leam Hall wrote: >> >> Am I more confused than normal or if I click "Reply" should it go just to >> the sender instead of the list? > > Yep. Someone decided it didn't make sense for "reply" to go to the list > that sent the message and should be receiving the reply. I've never > understood it, even after reading the arguments in favor. I've never understood it either. The reasoning seems to be that a special List-Post email field was added for this purpose and so all the email clients should get fixed to have a reply-list button that sends your reply to that address. It's been almost 10 years since RFC 4021 described this idea (or is it even older?) during which time the number of email clients in use has exploded and AFAIK very few of them have the reply-list feature. Some time ago I suggested reply-to munging for this very list but there wasn't a great deal of enthusiasm for the idea. Oscar From wprins at gmail.com Tue Aug 20 18:25:03 2013 From: wprins at gmail.com (Walter Prins) Date: Tue, 20 Aug 2013 17:25:03 +0100 Subject: [Tutor] python tutoring In-Reply-To: References: Message-ID: Hi, On 20 August 2013 14:22, Fowler, Trent wrote: > I am a self-starter and highly motivated, but I live in a small town in > South Korea and I don't have any friends who program. Since I also don't > have a computer science background and python is my first language, I > really need someone who can help me with the beginning stages. Often times > when I run into a problem not only do I not know how to solve it, I don't > even know how to ask the questions that will help someone else solve it. > Want to echo Ramit's sentiments. While the people here are volunteers and do not take kindly to having their time wasted by obvious laziness or lack of effort from the side of questioners, we do like helping and teaching others Python, and also like seeing and learning from others' questions. Even basic questions can sometimes spark pretty interesting discussions that is beneficial to the wider readership and not just the original poster. As far as picking up Python: There's a lot of video tutorials on the web, including a lot of decent stuff on showmedo.com Some of it's not free and requires membership (with the free set growing and being pretty substantial in any case.) Either way the $29 annual price is hopefully cheap enough to not be an obstacle should you choose to want to see the non-free stuff: http://showmedo.com/club/joinus?smdc=pyban Good luck, Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From leamhall at gmail.com Tue Aug 20 18:23:18 2013 From: leamhall at gmail.com (leam hall) Date: Tue, 20 Aug 2013 12:23:18 -0400 Subject: [Tutor] python tutoring In-Reply-To: <5B80DD153D7D744689F57F4FB69AF47418678CF3@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF47418678CF3@SCACMX008.exchad.jpmchase.net> Message-ID: Trent, You can do well with Alan's on-line pages ( http://www.freenetpages.co.uk/hp/alan.gauld/) and just ask questions here. The biggest thing is to pick a big enough but not too big project. Using Python as your first programming language will spoil you, though, it's a great language and covers a lot of options. Leam On Tue, Aug 20, 2013 at 10:52 AM, Prasad, Ramit < ramit.prasad at jpmorgan.com.dmarc.invalid> wrote: > Fowler, Trent wrote: > > > > Hello, > > > > Not long ago I came across the website of a professional programmer > offering python tutoring services: > > > > http://www.jeffknupp.com/python-tutoring/ > > > > I have attempted to contact him because I am interested but I've been > unable to get in touch. I was > > wondering if anyone knew of people offering similar services. I am a > self-starter and highly > > motivated, but I live in a small town in South Korea and I don't have > any friends who program. Since > > I also don't have a computer science background and python is my first > language, I really need someone > > who can help me with the beginning stages. Often times when I run into > a problem not only do I not > > know how to solve it, I don't even know how to ask the questions that > will help someone else solve it. > > > > I don't want to be spoon-fed, just gently nudged and guided. I'm on a > budget but I'd be willing to > > pay for a good teacher. Preliminary googling has turned up precious > little, so I thought someone here > > might be able to point me in the right direction. > > > > Thanks, > > > > -Trent. > > Why not just post your questions on here? I mean, that *is* the purpose > of this list. There are some very excellent tutors on here which has > the advantage of not being limited to only one tutor's level of experience. > Not to mention it is free. :) > > I would recommend reading > http://www.catb.org/esr/faqs/smart-questions.html, > posting in plain text and bottom or in-line posting. > > In general, try and reduce the amount of code to the smallest example you > can. > Provide Python version, operating system, code, input, expected output, > actual output, and any exceptions with full trace (copy and paste the full > message, not paraphrasing or retyping). > > > > Ramit > > > > This email is confidential and subject to important disclaimers and > conditions including on offers for the purchase or sale of securities, > accuracy and completeness of information, viruses, confidentiality, legal > privilege, and legal entity disclaimers, available at > http://www.jpmorgan.com/pages/disclosures/email. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From bessenkphilip at gmail.com Tue Aug 20 19:20:40 2013 From: bessenkphilip at gmail.com (bessenkphilip) Date: Tue, 20 Aug 2013 22:50:40 +0530 Subject: [Tutor] Coursera Python Course starts today In-Reply-To: <5212B211.5050900@gmail.com> References: <5212B211.5050900@gmail.com> Message-ID: <5213A568.7050408@gmail.com> Thank you. need to check out. Me too completed first 2 weeks but haven't completed the full course. -Bessen On 08/20/2013 05:32 AM, Leam Hall wrote: > Hey all, > > In case I'm not the absolute last person to know, the newest edition > of the Coursera "Learn to Program" course started today. It is Python > based, free, lasts 7 weeks, and pretty fun. I didn't get a certificate > last time as life got in the way. Hope to succeed this time. > > https://class.coursera.org/programming1-002/class/index > > Leam > > From alan.gauld at btinternet.com Tue Aug 20 20:53:43 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 20 Aug 2013 19:53:43 +0100 Subject: [Tutor] [OT] Replies go to individuals, not the list? In-Reply-To: References: <5212B5BE.1060604@gmail.com> Message-ID: On 20/08/13 13:15, Andy McKenzie wrote: > Yep. Someone decided it didn't make sense for "reply" to go to the list > that sent the message Lists never send messages. People do. So reply goes to the *person* who sent the message. Which is what mail always does, to modify it for mail forwarded by a list server makes no sense whatsoever. And it breaks the ability to send to the originator. IMHO of course :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From vick1975 at orange.mu Tue Aug 20 14:49:06 2013 From: vick1975 at orange.mu (Vick) Date: Tue, 20 Aug 2013 16:49:06 +0400 Subject: [Tutor] hi In-Reply-To: References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> <000001ce9590$e1b667a0$a52336e0$@orange.mu> <000001ce9650$fe15e180$fa41a480$@orange.mu> <000701ce9771$187e6bc0$497b4340$@orange.mu> <000101ce979b$1d17ff00$5747fd00$@orange.mu> Message-ID: <000601ce9da3$a7e1b460$f7a51d20$@orange.mu> -----Original Message----- From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] Sent: Tuesday, 13 August, 2013 01:01 > Well just send me some tutorial on how to build and obtain the > coefficients for the butcher tableau for the RK4 as an example, and > after I've mastered it, I'd give the dopri8 a shot. I am up for it so I'll see if I can find time to write a script that shows how to do it. [Vick] Hope you've had the time to code it. I'm waiting for it. By the way your code for the Adams-Moulton coefficients are actually the Adams-Bashforth ones and so I copied it and modified the copy to have the Adams-Moulton coefficients as well. This means that I have now an nth-order predictor-corrector method to solve for ODEs. Vick From tanjyunda at gmail.com Tue Aug 20 11:20:23 2013 From: tanjyunda at gmail.com (sikonai sikonai) Date: Tue, 20 Aug 2013 17:20:23 +0800 Subject: [Tutor] is the the two style of writting the same? Message-ID: >>> list=[1,2,3,4,5,6,7,8,9] >>> list[9:0:-2] [9, 7, 5, 3] >>> list[10:0:-2] [9, 7, 5, 3] I want to know whether they have some difference. -------------- next part -------------- An HTML attachment was scrubbed... URL: From tanjyunda at gmail.com Tue Aug 20 13:50:47 2013 From: tanjyunda at gmail.com (tan Sikonai) Date: Tue, 20 Aug 2013 19:50:47 +0800 Subject: [Tutor] I have download python 3.3, then I click the link of Module Docs, but nothing appears Message-ID: where is the docs -------------- next part -------------- An HTML attachment was scrubbed... URL: From tanjyunda at gmail.com Tue Aug 20 13:51:34 2013 From: tanjyunda at gmail.com (tan Sikonai) Date: Tue, 20 Aug 2013 19:51:34 +0800 Subject: [Tutor] is the the two style of writting the same? In-Reply-To: References: Message-ID: ? 2013/8/20 sikonai sikonai > >>> list=[1,2,3,4,5,6,7,8,9] > >>> list[9:0:-2] > [9, 7, 5, 3] > >>> list[10:0:-2] > [9, 7, 5, 3] > > I want to know whether they have some difference. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From kwpolska at gmail.com Tue Aug 20 21:00:01 2013 From: kwpolska at gmail.com (=?UTF-8?B?Q2hyaXMg4oCcS3dwb2xza2HigJ0gV2Fycmljaw==?=) Date: Tue, 20 Aug 2013 21:00:01 +0200 Subject: [Tutor] [OT] Replies go to individuals, not the list? In-Reply-To: References: <5212B5BE.1060604@gmail.com> Message-ID: On Tue, Aug 20, 2013 at 8:53 PM, Alan Gauld wrote: > On 20/08/13 13:15, Andy McKenzie wrote: > >> Yep. Someone decided it didn't make sense for "reply" to go to the list >> that sent the message > > > Lists never send messages. People do. > > So reply goes to the *person* who sent the message. > Which is what mail always does, to modify it for mail > forwarded by a list server makes no sense whatsoever. > And it breaks the ability to send to the originator. The From: field still contains an e-mail address of a human. Just copy-paste it into the To: field of your reply. And replying to the human instead of the list is almost never what you want. -- Chris ?Kwpolska? Warrick PGP: 5EAAEA16 stop html mail | always bottom-post | only UTF-8 makes sense From alan.gauld at btinternet.com Tue Aug 20 20:57:20 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 20 Aug 2013 19:57:20 +0100 Subject: [Tutor] python tutoring In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF47418678CF3@SCACMX008.exchad.jpmchase.net> Message-ID: On 20/08/13 17:23, leam hall wrote: > You can do well with Alan's on-line pages > (http://www.freenetpages.co.uk/hp/alan.gauld/) but don't use that site, its prehistoric but locked so that I can't redirect it. See my sig instead.... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From amckenzie4 at gmail.com Tue Aug 20 21:00:04 2013 From: amckenzie4 at gmail.com (Andy McKenzie) Date: Tue, 20 Aug 2013 15:00:04 -0400 Subject: [Tutor] [OT] Replies go to individuals, not the list? In-Reply-To: References: <5212B5BE.1060604@gmail.com> Message-ID: On Tue, Aug 20, 2013 at 2:53 PM, Alan Gauld wrote: > On 20/08/13 13:15, Andy McKenzie wrote: > > Yep. Someone decided it didn't make sense for "reply" to go to the list >> that sent the message >> > > Lists never send messages. People do. > > So reply goes to the *person* who sent the message. > Which is what mail always does, to modify it for mail > forwarded by a list server makes no sense whatsoever. > And it breaks the ability to send to the originator. > > IMHO of course :-) > http://mail.python.org/mailman/listinfo/tutor > The problem is, as far as I'm concerned the message came from the list. Needing to go to the dropdown and select "Reply to all" is just one extra movement, and it's one I have to make every single time I reply. In all honesty, I can't think of a single time that I've wanted to reply to just the original sender: that's the point of a mailing list, to have conversations on it. I've occasionally been prompted to remember that I wanted to ask an individual something specific off-list, but it's never been a direct response to what was posted ON the list. As to the ability to send to the originator: I've been on a lot of lists where the address was munged. They all included the original sender's email address in the body, so if I really wanted to send to them, I could. And the rest of the time (basically always) I didn't have to think about it. It's basically a practicality thing for me. On a list where the vast majority of replies went to the original sender, I'd agree with you. For something like this, it's just making me do extra work without providing me with an extra benefit. Andy -------------- next part -------------- An HTML attachment was scrubbed... URL: From leamhall at gmail.com Tue Aug 20 21:09:31 2013 From: leamhall at gmail.com (leam hall) Date: Tue, 20 Aug 2013 15:09:31 -0400 Subject: [Tutor] [OT] Replies go to individuals, not the list? In-Reply-To: References: <5212B5BE.1060604@gmail.com> Message-ID: Replying to the human, vice the list, is about 99% never what I want. With a 1% margin of error. :) Leam On Tue, Aug 20, 2013 at 3:00 PM, Chris ?Kwpolska? Warrick < kwpolska at gmail.com> wrote: > On Tue, Aug 20, 2013 at 8:53 PM, Alan Gauld > wrote: > > On 20/08/13 13:15, Andy McKenzie wrote: > > > >> Yep. Someone decided it didn't make sense for "reply" to go to the list > >> that sent the message > > > > > > Lists never send messages. People do. > > > > So reply goes to the *person* who sent the message. > > Which is what mail always does, to modify it for mail > > forwarded by a list server makes no sense whatsoever. > > And it breaks the ability to send to the originator. > > The From: field still contains an e-mail address of a human. Just > copy-paste it into the To: field of your reply. > > And replying to the human instead of the list is almost never what you > want. > > -- > Chris ?Kwpolska? Warrick > PGP: 5EAAEA16 > stop html mail | always bottom-post | only UTF-8 makes sense > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From malaclypse2 at gmail.com Tue Aug 20 21:09:36 2013 From: malaclypse2 at gmail.com (Jerry Hill) Date: Tue, 20 Aug 2013 15:09:36 -0400 Subject: [Tutor] I have download python 3.3, then I click the link of Module Docs, but nothing appears In-Reply-To: References: Message-ID: You don't give much context for your question, so I'm going to guess. You installed Python 3.3 on windows, and it installed some links in your start menu. One of those links is titled "Module Docs", and clicking on it doesn't do anything. Is that right? That seems to be the case for me too, though I'd never clicked on that link until just now. I'm not sure what that's supposed to be a shortcut for. If you're looking for the Python documentation though, you want the shortcut titled "Python Manuals". That will give you a windows help file with all of the python documentation bundled up in there. If you don't care for that format, all of the same information is available from http://docs.python.org/3/ as well. -- Jerry From charlesleguen at gmail.com Tue Aug 20 21:16:19 2013 From: charlesleguen at gmail.com (charles le guen) Date: Tue, 20 Aug 2013 15:16:19 -0400 Subject: [Tutor] using python with photoshop Message-ID: Hi I'm running Windows 7 64 bit and I want to manipulate psd photoshop files using python. Google searches led me to a few cool sitessuggesting I need to use comtypes but that looks like dated technology. for someone working in 64bit windows 7... Can anyone tell me the best way to manipulate photoshop with python? Is the comtypes approach truly the best? Thanks Charles -------------- next part -------------- An HTML attachment was scrubbed... URL: From wprins at gmail.com Tue Aug 20 21:31:11 2013 From: wprins at gmail.com (Walter Prins) Date: Tue, 20 Aug 2013 20:31:11 +0100 Subject: [Tutor] is the the two style of writting the same? In-Reply-To: References: Message-ID: Hi, On 20 August 2013 10:20, sikonai sikonai wrote: > >>> list=[1,2,3,4,5,6,7,8,9] > >>> list[9:0:-2] > [9, 7, 5, 3] > >>> list[10:0:-2] > [9, 7, 5, 3] > > I want to know whether they have some difference. > For the specific list you show, the 2 expressions yield the same result. However this does not mean that this is true of any list. The reason the 2 expressions return the same result is because the starting index for the slice operation is effectively capped at 9, due to the list containing only 9 elements, so hence any value above 9 in the first slice parameter is essentially reset/capped to 9. For longer lists of course this is not true so the results are different. Play around with some longer/other examples to see this: Python 2.7.5 (default, May 15 2013, 22:43:36) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> l = [1,2,3,4,5,6,7,8,9,10,11] #note the extra elements as compared to your example >>> l[9:0] [] >>> l[9:0:-2] [10, 8, 6, 4, 2] >>> l[10:0:-2] [11, 9, 7, 5, 3] >>> l[12:0:-2] [11, 9, 7, 5, 3] >>> l[20:0:-2] [11, 9, 7, 5, 3] >>> Walter -------------- next part -------------- An HTML attachment was scrubbed... URL: From brian.van.den.broek at gmail.com Tue Aug 20 21:47:14 2013 From: brian.van.den.broek at gmail.com (Brian van den Broek) Date: Tue, 20 Aug 2013 15:47:14 -0400 Subject: [Tutor] [OT] Replies go to individuals, not the list? In-Reply-To: References: <5212B5BE.1060604@gmail.com> Message-ID: On Aug 20, 2013 3:04 PM, "Andy McKenzie" wrote: > > On Tue, Aug 20, 2013 at 2:53 PM, Alan Gauld wrote: >> >> On 20/08/13 13:15, Andy McKenzie wrote: >> >>> Yep. Someone decided it didn't make sense for "reply" to go to the list >>> that sent the message >> >> >> Lists never send messages. People do. >> >> So reply goes to the *person* who sent the message. > The problem is, as far as I'm concerned the message came from the list. Needing to go to the dropdown and select "Reply to all" is just one extra movement, and it's one I have to make every single time I reply. In all honesty, I can't think of a single time that I've wanted to reply to just the original sender: that's the point of a mailing list, to have conversations on it. I've occasionally been prompted to remember that I wanted to ask an individual something specific off-list, but it's never been a direct response to what was posted ON the list. > It's basically a practicality thing for me. On a list where the vast majority of replies went to the original sender, I'd agree with you. For something like this, it's just making me do extra work without providing me with an extra benefit. Hi all, Alan's argument seems compelling, but is principled, thus perhaps vulnerable to a `practicality beats purity' response. What tips me against reply to munging is the principle of least damage, itself eminently practical. Imagine the non-actual possible world where this list reply munges and in which I wished to write Andy directly to cast aspersions on Alan's character and ancestry out of a misguided belief that reply to munging is right. I hit reply and shortly afterwards realize that I am missing toes. In the actual world, I might have accidentally sent this solely to Andy out of inattention. Irksome, but I still have all 7 of my toes. Powerful software often can and ought allow one to shoot oneself in the foot. It ought not however be designed so that in one context, doing what is safe and normal in another context surprisingly points a firearm at your feet without the accompaniment of klaxons and lights. (And even then ?) Best, Brian vdB -------------- next part -------------- An HTML attachment was scrubbed... URL: From leamhall at gmail.com Tue Aug 20 22:00:36 2013 From: leamhall at gmail.com (leam hall) Date: Tue, 20 Aug 2013 16:00:36 -0400 Subject: [Tutor] [OT] Replies go to individuals, not the list? In-Reply-To: References: <5212B5BE.1060604@gmail.com> Message-ID: The only question I have is what is compelling about being different than other lists? Far as I can tell, most reply to the list if you click reply. It's not something to get religious over; if I reply and don't have time to make sure it goes to those who might be interested, at least it will go to the person I'm responding to. They can forward it on if it's important enough. :) Leam On Tue, Aug 20, 2013 at 3:47 PM, Brian van den Broek < brian.van.den.broek at gmail.com> wrote: > > On Aug 20, 2013 3:04 PM, "Andy McKenzie" wrote: > > > > On Tue, Aug 20, 2013 at 2:53 PM, Alan Gauld > wrote: > >> > >> On 20/08/13 13:15, Andy McKenzie wrote: > >> > >>> Yep. Someone decided it didn't make sense for "reply" to go to the > list > >>> that sent the message > >> > >> > >> Lists never send messages. People do. > >> > >> So reply goes to the *person* who sent the message. > > > > > The problem is, as far as I'm concerned the message came from the list. > Needing to go to the dropdown and select "Reply to all" is just one extra > movement, and it's one I have to make every single time I reply. In all > honesty, I can't think of a single time that I've wanted to reply to just > the original sender: that's the point of a mailing list, to have > conversations on it. I've occasionally been prompted to remember that I > wanted to ask an individual something specific off-list, but it's never > been a direct response to what was posted ON the list. > > > > > It's basically a practicality thing for me. On a list where the vast > majority of replies went to the original sender, I'd agree with you. For > something like this, it's just making me do extra work without providing me > with an extra benefit. > > Hi all, > > Alan's argument seems compelling, but is principled, thus perhaps > vulnerable to a `practicality beats purity' response. > > What tips me against reply to munging is the principle of least damage, > itself eminently practical. > > Imagine the non-actual possible world where this list reply munges and in > which I wished to write Andy directly to cast aspersions on Alan's > character and ancestry out of a misguided belief that reply to munging is > right. I hit reply and shortly afterwards realize that I am missing toes. > > In the actual world, I might have accidentally sent this solely to Andy > out of inattention. Irksome, but I still have all 7 of my toes. > > Powerful software often can and ought allow one to shoot oneself in the > foot. It ought not however be designed so that in one context, doing what > is safe and normal in another context surprisingly points a firearm at your > feet without the accompaniment of klaxons and lights. (And even then ?) > > Best, > > Brian vdB > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From fomcl at yahoo.com Tue Aug 20 22:33:37 2013 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Tue, 20 Aug 2013 13:33:37 -0700 (PDT) Subject: [Tutor] using python with photoshop In-Reply-To: References: Message-ID: <1377030817.32494.YahooMailNeo@web163805.mail.gq1.yahoo.com> ________________________________ > From: charles le guen >To: tutor at python.org >Sent: Tuesday, August 20, 2013 9:16 PM >Subject: [Tutor] using python with photoshop > >Hi > >I'm running Windows 7 64 bit and I want to manipulate psd photoshop files using python. > >Google searches led me to a few cool sites suggesting I need to use comtypes but that looks like dated technology. for someone working in 64bit windows 7... > >Can anyone tell me the best way to manipulate photoshop with python? ?Is the comtypes approach truly the best? Photoshop: no idea. But GIMP might be an alternative: http://www.gimp.org/docs/python/. Or PIL + psd tools: https://pypi.python.org/pypi/psd-tools From ramit.prasad at jpmorgan.com.dmarc.invalid Tue Aug 20 21:37:30 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Tue, 20 Aug 2013 19:37:30 +0000 Subject: [Tutor] is the the two style of writting the same? In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF4741867C699@SCACMX008.exchad.jpmchase.net> tan Sikonai wrote: > > ? You should wait at least 24 hours as many people in this mailing list are around the world and may not see this immediately. > > 2013/8/20 sikonai sikonai > >>> list=[1,2,3,4,5,6,7,8,9] > >>> list[9:0:-2] > [9, 7, 5, 3] > >>> list[10:0:-2] > [9, 7, 5, 3] > > I want to know whether they have some difference. You should try and avoid using built-in names like "list" as this shadows list function. Yes, there is a difference. Python slices ignore out of range elements so this becomes the last element and elements with a -2 step (meaning traverse the list in reverse and select every other element) until you get to the 0th slot. >>> list[100:0:-2] [9, 7, 5, 3] ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From davea at davea.name Wed Aug 21 00:19:55 2013 From: davea at davea.name (Dave Angel) Date: Tue, 20 Aug 2013 22:19:55 +0000 (UTC) Subject: [Tutor] [OT] Replies go to individuals, not the list? References: <5212B5BE.1060604@gmail.com> Message-ID: leam hall wrote: > The only question I have is what is compelling about being different than > other lists? Far as I can tell, most reply to the list if you click reply. > > It's not something to get religious over; if I reply and don't have time to > make sure it goes to those who might be interested, at least it will go to > the person I'm responding to. They can forward it on if it's important > enough. :) > Yeah, and top-posting with html mail is similarly taking the easy way out. After all, who cares if everyone else has to put up with your bad habits. -- DaveA From amckenzie4 at gmail.com Wed Aug 21 00:40:53 2013 From: amckenzie4 at gmail.com (Andy McKenzie) Date: Tue, 20 Aug 2013 18:40:53 -0400 Subject: [Tutor] [OT] Replies go to individuals, not the list? In-Reply-To: References: <5212B5BE.1060604@gmail.com> Message-ID: leam hall wrote: > > > The only question I have is what is compelling about being different than > > other lists? Far as I can tell, most reply to the list if you click > reply. > > > > It's not something to get religious over; if I reply and don't have time > to > > make sure it goes to those who might be interested, at least it will go > to > > the person I'm responding to. They can forward it on if it's important > > enough. :) > > > > Yeah, and top-posting with html mail is similarly taking the easy way > out. After all, who cares if everyone else has to put up with your bad > habits. > Well, since someone else brought it up... I really prefer top posting. In general, I don't WANT to reread every message: I want to quickly get to whatever is new. Top posting, much like the return-address munging question, is a personal preference. For me, it runs opposite to what this list requires. Clearly whoever set up this list agreed with you. What REALLY gets to me is the people who try to insist that their way is objectively RIGHT, and everyone else is practicing bad habits, or polluting the net, or some other nonsense like that. The fact is, we just have different work flow preferences. You like one thing, I like another. If you want to present your view rationally and objectively, or talk about your preferred layouts, that's fine. But let's not start saying someone has "bad habits" because they disagree with you. Andy -------------- next part -------------- An HTML attachment was scrubbed... URL: From chris at chrisdown.name Wed Aug 21 01:01:56 2013 From: chris at chrisdown.name (Chris Down) Date: Wed, 21 Aug 2013 01:01:56 +0200 Subject: [Tutor] [OT] Replies go to individuals, not the list? In-Reply-To: References: <5212B5BE.1060604@gmail.com> Message-ID: <20130820230156.GD9251@gopher> On 2013-08-20 18:40, Andy McKenzie wrote: > Well, since someone else brought it up... I really prefer top posting. In > general, I don't WANT to reread every message: I want to quickly get to > whatever is new. Right, which is why when top posting you should cut to the relevant context. > What REALLY gets to me is the people who try to insist that their way is > objectively RIGHT, and everyone else is practicing bad habits, or polluting > the net, or some other nonsense like that. The fact is, we just have > different work flow preferences. You like one thing, I like another. If > you want to present your view rationally and objectively, or talk about > your preferred layouts, that's fine. But let's not start saying someone > has "bad habits" because they disagree with you. In Gmail (which it appears that you are using) I don't think it really matters, since it selectively collapses the context anyway. It certainly matters when reading in a mail client that doesn't collapse quotes (which, in my opinion, is not something a mail reader should be doing anyway). I agree this is a personal opinion, but mixing the two in a single thread often makes message flow completely incomprehensible. I am also in the bottomposting camp, I'm not very dogmatic about it as long as people don't mix the two in a single thread. Then it just becomes functionally irritating. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From chris at chrisdown.name Wed Aug 21 01:09:07 2013 From: chris at chrisdown.name (Chris Down) Date: Wed, 21 Aug 2013 01:09:07 +0200 Subject: [Tutor] [OT] Replies go to individuals, not the list? In-Reply-To: <20130820230156.GD9251@gopher> References: <5212B5BE.1060604@gmail.com> <20130820230156.GD9251@gopher> Message-ID: <20130820230907.GE9251@gopher> On 2013-08-21 01:01, Chris Down wrote: > Right, which is why when top posting you should cut to the relevant context. s/top posting/bottom posting/ I'm interested to know how you can reply and reference multiple parts of a message clearly when top posting, though. I think that's impossible without destroying clarity. Bottom posting is just objectively much more intuitive when replying per-context and not per-message, which is what you want almost all of the time. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From alan.gauld at btinternet.com Wed Aug 21 01:51:30 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 21 Aug 2013 00:51:30 +0100 Subject: [Tutor] using python with photoshop In-Reply-To: References: Message-ID: On 20/08/13 20:16, charles le guen wrote: > I'm running Windows 7 64 bit and I want to manipulate psd photoshop > files using python. Because PSD is a proprietary (but published) format designed for Photoshop (although in practice many other tools can open them too) it is always going to be a slightly clunky process. However, if you can convert the files into more standard and open formats you will have more luck. That being said I did find this with a simple Google search: https://pypi.python.org/pypi/psd-tools Since you don't say what you want to do I don't know if it will help... > > suggesting I need to use comtypes Never heard of comtypes but I have heard of ctypes and COM. Both are available from Python but imply you are managing the files through Photoshop itself. That's usually a bad technique and a last resort. The psd-tools approach appears to be based on direct manipulation of the files. > but that looks like > dated technology. for someone working in 64bit windows 7... COM is still supported and ctypes simply calls whatever APIs Microsoft (or Adobe) make available so it is as up to date as the exposed APIs... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From eryksun at gmail.com Wed Aug 21 01:53:26 2013 From: eryksun at gmail.com (eryksun) Date: Tue, 20 Aug 2013 19:53:26 -0400 Subject: [Tutor] is the the two style of writting the same? In-Reply-To: References: Message-ID: On Tue, Aug 20, 2013 at 3:31 PM, Walter Prins wrote: > On 20 August 2013 10:20, sikonai sikonai wrote: >> >> >>> list=[1,2,3,4,5,6,7,8,9] >> >>> list[9:0:-2] >> [9, 7, 5, 3] >> >>> list[10:0:-2] >> [9, 7, 5, 3] > > For the specific list you show, the 2 expressions yield the same result. > However this does not mean that this is true of any list. The reason the 2 > expressions return the same result is because the starting index for the > slice operation is effectively capped at 9, due to the list containing only > 9 elements, so hence any value above 9 in the first slice parameter is > essentially reset/capped to 9. The start index is capped at 8: >>> slice(9, 0, -2).indices(9) (8, 0, -2) From steve at pearwood.info Wed Aug 21 02:30:52 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 21 Aug 2013 10:30:52 +1000 Subject: [Tutor] [OT] Replies go to individuals, not the list? In-Reply-To: <20130820230907.GE9251@gopher> References: <5212B5BE.1060604@gmail.com> <20130820230156.GD9251@gopher> <20130820230907.GE9251@gopher> Message-ID: <52140A3C.5060204@pearwood.info> On 21/08/13 09:09, Chris Down wrote: > On 2013-08-21 01:01, Chris Down wrote: >> Right, which is why when top posting you should cut to the relevant context. > > s/top posting/bottom posting/ > > I'm interested to know how you can reply and reference multiple parts of a > message clearly when top posting, though. I think that's impossible without > destroying clarity. Bottom posting is just objectively much more intuitive when > replying per-context and not per-message, which is what you want almost all of > the time. Please don't call it "bottom posting". Bottom posting is when you scroll all the way past the original message, and append your reply at the bottom. Unless the quoted message is very short, as it is here, bottom-posting is worse than top-posting, since it has all the disadvantages of top-posting, AND loses the one advantage (namely that you can quickly see new content without scrolling). What you're referring to is inline or interleaved posting, where the reply is interleaved between paragraphs of quoted text, thus establishing context, rather like a conversation. Since what we're doing here *is* a conversation, albeit written rather than spoken, interleaving responses is most natural. But sometimes, if the communication isn't really a conversation as such, a short top-posted reply is best. The rule of thumb I use is this: - If the poster raises various points that need to be answered individually, then I interleave my responses with their comments: Question, Answer, Question, Answer sort of thing. - If there is nowhere I can reasonably trim their comments to establish context, and my response is just a general reply rather than specifically responding to specific comments (e.g. if my reply is "thanks for your email, I'll consider it for the future" sort of thing) then I might top post, leaving their comments below for context. Whatever posting style is used *clarity of communication* should be the intent. Too many people make *ease of firing off an email with the least effort possible* their intent, and screw their readers. -- Steven From steve at pearwood.info Wed Aug 21 03:34:18 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 21 Aug 2013 11:34:18 +1000 Subject: [Tutor] [OT] Replies go to individuals, not the list? In-Reply-To: References: <5212B5BE.1060604@gmail.com> Message-ID: <5214191A.3050002@pearwood.info> On 21/08/13 08:40, Andy McKenzie wrote: > Well, since someone else brought it up... I really prefer top posting. In > general, I don't WANT to reread every message: I want to quickly get to > whatever is new. You shouldn't have to reread every message. At most, you should have to skim a few paragraphs of quoted text to establish context, and then get to the new stuff. Like here. Notice I've trimmed all the extraneous conversation, and got right down to the bit that matters. Of course, this is a simple case. Sometimes it's harder to trim, and you end up with multiple paragraphs of older text. But even then you don't have to read the whole thing, you should be able to skim it, looking for key words or key sentences that establish context. Those with reading difficulties (e.g. the blind or partially sighted, those reading in a language they are not fluent in, or simply lousy readers) may have trouble skimming text. I'm sympathetic, but they're not actually worse off than with top-posting. They can just ignore the quoted text, and hope that the response makes sense without context. Just like reading a top-posted message. [...] > What REALLY gets to me is the people who try to insist that their way is > objectively RIGHT, and everyone else is practicing bad habits, or polluting > the net, or some other nonsense like that. The fact is, we just have > different work flow preferences. You like one thing, I like another. If > you want to present your view rationally and objectively, or talk about > your preferred layouts, that's fine. But let's not start saying someone > has "bad habits" because they disagree with you. I've been getting and sending email long enough, in enough different contexts, that I think I can objectively say: most email users can't write for shit, and posting style doesn't enter into it, they're just poor writers, lazy writers, incompetent writers. On a technical forum like this, you're seeing a better-than-average set of writers. I think I can also say that for a wide range of situations, top-posting is objectively worse for a number of reasons, but it's not too bad if you have a very small number of emails between just two parties, and it certainly does have an advantage that it clearly puts the response right up top where it is easy to see. The worst part of top-posting is that the typical email will raise more than one question or point that needs answering, but without context, it's hard to clearly respond when top-posting. You need a chunk of added verbiage: You asked a question about map(), the answer is blah blah blah. You also asked about the exception that you got. The line of code that failed was blah blah blah, and the reason for the exception was blah... Also, you mentioned blah blah blah, to which I say, blah... You simply don't need that extra verbiage when posting interleaved after the question, the question can stand for itself! But since most people are lazy writers, they don't do either. They arbitrarily pick one question (usually the first, or the simplest) and answer it alone. (I've sent business emails to people where I clearly said "I need the answer to these three questions or we cannot proceed with your project", and enumerate the questions, and they responded to the *last* question and ignored the other two. Lazy *and* stupid, the story of mankind.) What gets me is the ever-growing cancerous lump of quoted-quoted-quoted-quoted-quoted text that grows at the bottom of top-posted emails. Email volume grow exponentially in size, e.g.: First email is 5 lines long. Reply is 5 lines long + 5 quoted lines, = 10 lines. Reply to that is 5 lines long, + 10 quoted lines = 15 lines. Reply to that is 5 lines long, + 15 quoted lines = 20 lines. Reply to that is 5 lines long, + 20 quoted lines = 25 lines. After five emails, we have a total of 75 lines of text, of which only 25 lines is actual fresh content, a ratio of 33%. The signal-to-noise ratio rapidly diminishes. After ten emails, the ratio is 18%, and after 20, just 9%. That's worse than interleaved posting with trimming, where the ideal is a 1:1 ratio. Real email conversations don't get anywhere near that ideal, but my estimate is that a ratio of 50% or better is easily attainable so long as people trim. In practice, business email is even worse than this: messages tend to be short, and those stupid and legally meaningless disclaimers at the bottom of emails long. I've seen a TWENTY line disclaimer, quoted FOURTEEN times, in a single email: >>>>>>>>>>>>>> This email may contain blah blah blah ... >>>>>>>>>>>>> This email may contain blah blah blah ... >>>>>>>>>>>> This email may contain blah blah blah ... >>>>>>>>>>> This email may contain blah blah blah ... >>>>>>>>>> This email may contain blah blah blah ... ... Seriously, I kid you not. A few years ago, the company I work for took a customer to court for non-payment. During discovery, we had to provide the customer with copies of all emails between us. I estimated the volume of email to be multiple thousands of pages, if printed out in full, and only a couple of dozen if we extracted out the fresh (unquoted) content, trimming legal disclaimers and signatures and quoting. To a first approximation, the signal to noise ratio of business email is approximately zero :-) -- Steven From davea at davea.name Wed Aug 21 03:43:45 2013 From: davea at davea.name (Dave Angel) Date: Wed, 21 Aug 2013 01:43:45 +0000 (UTC) Subject: [Tutor] [OT] Replies go to individuals, not the list? References: <5212B5BE.1060604@gmail.com> Message-ID: Andy McKenzie wrote: > (DaveA wrote, but wasn't attributed) >> Yeah, and top-posting with html mail is similarly taking the easy way >> out. After all, who cares if everyone else has to put up with your bad >> habits. >> > > Well, since someone else brought it up... I really prefer top posting. In > general, I don't WANT to reread every message: You shouldn't have any messages to reread. If one quotes only what's relevant, it gives context without extra noise. The whole notion of top--posting only makes sense when it's a one-to-one email session, where you need to keep the entire history of the exchange. It breaks down as soon as there are multiple public replies in the same thread, or when there are multiple points you want to respond to. > What REALLY gets to me is the people who try to insist that their way is > objectively RIGHT, and everyone else is practicing bad habits, or polluting > the net, or some other nonsense like that. The fact is, we just have > different work flow preferences. You like one thing, I like another. If > you want to present your view rationally and objectively, or talk about > your preferred layouts, that's fine. But let's not start saying someone > has "bad habits" because they disagree with you. >
leam hall wrote:
> >
> > The only question I have is what is compelling about being different than
> > other lists? Far as I can tell, most reply to the list if you click reply.
> >
> > It's not something to get religious over; if I reply and don't have time to
> > make sure it goes to those who might be interested, at least it will go to
> > the person I'm responding to. They can forward it on if it's important
> > enough. ?:)
> >
>
I notice that you didn't pay any attention to the other bad habit. Why should we all pay to download an html message as well as the text message. -- DaveA From charlesleguen at gmail.com Wed Aug 21 04:01:10 2013 From: charlesleguen at gmail.com (charles le guen) Date: Tue, 20 Aug 2013 22:01:10 -0400 Subject: [Tutor] using python with photoshop In-Reply-To: <1377030817.32494.YahooMailNeo@web163805.mail.gq1.yahoo.com> References: <1377030817.32494.YahooMailNeo@web163805.mail.gq1.yahoo.com> Message-ID: Gimp is indeed an option. I might use nuke as well (it opens psd files). But I think I figured out how to manipulate photoshop with python... Seems installing pywin32 does the trick. It's important to install the proper version (I run 6 bit windows 7 but I had 32 bit version of python installed so the version of pywin32 needed to be the 32 bit version). Once pywin32 is installed, this code (below) opens photoshop: from win32com.client import Dispatch psApp = Dispatch("Photoshop.Application") >From here, I'll figure out how to manipulate layers etc as well. thanks C On Tue, Aug 20, 2013 at 4:33 PM, Albert-Jan Roskam wrote: > > > ________________________________ > > From: charles le guen > >To: tutor at python.org > >Sent: Tuesday, August 20, 2013 9:16 PM > >Subject: [Tutor] using python with photoshop > > > >Hi > > > >I'm running Windows 7 64 bit and I want to manipulate psd photoshop files > using python. > > > >Google searches led me to a few cool sites suggesting I need to use > comtypes but that looks like dated technology. for someone working in 64bit > windows 7... > > > >Can anyone tell me the best way to manipulate photoshop with python? Is > the comtypes approach truly the best? > > > Photoshop: no idea. But GIMP might be an alternative: > http://www.gimp.org/docs/python/. Or PIL + psd tools: > https://pypi.python.org/pypi/psd-tools > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From cybervigilante at gmail.com Wed Aug 21 06:52:10 2013 From: cybervigilante at gmail.com (Jim Mooney) Date: Tue, 20 Aug 2013 21:52:10 -0700 Subject: [Tutor] Runestone Python Course Message-ID: http://interactivepython.org Since there has been some mention of courses lately, I think this has become the best online Python course: "How to Think Like a Computer Scientist." It has an interactive Online Interpreter, where you can run the examples, or change things and experiment with new code (and even save it). This is rather like W3 Schools but much more convenient, since there is a new interpreter for every small segment, and you aren't flipping around like you are at W3 Schools. It also has a cleaner and more vivid look. There are simple quizzes, videos, and CodeLens, which is something like a debugger stepthrough, where you can activate your code line by line, and watch data and results. Minor drawback: Codelens just steps through code the instructors have written, for illustrative purposes. You can't write your own for the debugger simulation. However, you Can write anything in the Online Interpreter. The only drawback there is not All python standard modules have been implemented. (You can import math, for instance, but not some of the more obscure modules. But as they say, by the time you get good enough to use obscure modules, you'll have your own environment ;') However, the Online Interpreter can handle anything presented in the course, and it's a good course. The Interpreter is more like an editor than the standard shell, since you run a program or snippet rather than immediate interpretation, and you must print() to see a value. You also don't have those anachronistic >>> symbols. If anyone looked at this a few months ago, it was in development and there were a few bugs I wrote them about, but everything works great now. It starts with the basics, but there's no harm in going over them again, or at least skimming the definitions, even if you know them. I've found that when you re-read, you discover stuff you could swear wasn't there the first time, as if the author snuck in your house and added to the material ;') There is also a more advanced course on Algorithms and Data Structures on the page linked above, if you're already surefooted about the basics. -- Jim More and more, science is showing that animals, even "simple" ones, have awareness and feelings. There is no hard divide, as the rape-the-earth crowd would have us believe.. From steve at pearwood.info Wed Aug 21 07:54:11 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 21 Aug 2013 15:54:11 +1000 Subject: [Tutor] is the the two style of writting the same? In-Reply-To: References: Message-ID: <20130821055411.GA24672@ando> On Tue, Aug 20, 2013 at 05:20:23PM +0800, sikonai sikonai wrote: > >>> list=[1,2,3,4,5,6,7,8,9] > >>> list[9:0:-2] > [9, 7, 5, 3] > >>> list[10:0:-2] > [9, 7, 5, 3] > > I want to know whether they have some difference. Consider: 2+2 2*2 2**2 All three give the same result, 4, and yet they are different. You can see that they are different, because if you change the numbers from 2 to something else, the results will no longer be the same: 3+3 3*3 3**3 No longer equal! This proves that + * and ** are not the same thing. Go back to your example: L = [1, 2, 3, 4, 5, 6, 7, 8, 9] It turns out that L[9:0:-2] happens to be the same as L[10:0:-2], but that is only because the list only has 9 items. But if you do this: L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] for example, and then compare L[9:0:-2] and L[10:0:-2], you will see they are quite different: py> L = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] py> L[9:0:-2] [10, 8, 6, 4, 2] py> L[10:0:-2] [11, 9, 7, 5, 3] -- Steven From tanjyunda at gmail.com Wed Aug 21 05:58:09 2013 From: tanjyunda at gmail.com (tan Sikonai) Date: Wed, 21 Aug 2013 11:58:09 +0800 Subject: [Tutor] is the the two style of writting the same? In-Reply-To: References: Message-ID: Oh,I have just thinked that the max index equals the length of the list, but python supports more index. 2013/8/21 eryksun > On Tue, Aug 20, 2013 at 3:31 PM, Walter Prins wrote: > > On 20 August 2013 10:20, sikonai sikonai wrote: > >> > >> >>> list=[1,2,3,4,5,6,7,8,9] > >> >>> list[9:0:-2] > >> [9, 7, 5, 3] > >> >>> list[10:0:-2] > >> [9, 7, 5, 3] > > > > For the specific list you show, the 2 expressions yield the same result. > > However this does not mean that this is true of any list. The reason > the 2 > > expressions return the same result is because the starting index for the > > slice operation is effectively capped at 9, due to the list containing > only > > 9 elements, so hence any value above 9 in the first slice parameter is > > essentially reset/capped to 9. > > The start index is capped at 8: > > >>> slice(9, 0, -2).indices(9) > (8, 0, -2) > -------------- next part -------------- An HTML attachment was scrubbed... URL: From wcrowder at yandex.ru Wed Aug 21 05:00:24 2013 From: wcrowder at yandex.ru (William Crowder) Date: Tue, 20 Aug 2013 22:00:24 -0500 Subject: [Tutor] library: Message-ID: <2FF8332F-D73A-43A4-A1CB-BC2670993D46@yandex.ru> I am reading posts and watching videos. I am following along with the shell, i am retaining the info. But WHAT is a library? Thanks, everyone. -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Wed Aug 21 10:29:59 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 21 Aug 2013 18:29:59 +1000 Subject: [Tutor] library: In-Reply-To: <2FF8332F-D73A-43A4-A1CB-BC2670993D46@yandex.ru> References: <2FF8332F-D73A-43A4-A1CB-BC2670993D46@yandex.ru> Message-ID: <20130821082959.GC24672@ando> On Tue, Aug 20, 2013 at 10:00:24PM -0500, William Crowder wrote: > I am reading posts and watching videos. I am following along > with the shell, i am retaining the info. But WHAT is a library? In English, a library is a collection of books, or magazines. A "software library" is a collection of programs or functions, instead of books. Sometimes it will be one file, with many functions. Sometimes it will be many files. In Python: import math will load the math library, so you can use functions like math.sin, math.sqrt, and others. Does this answer your question? If you need more help, please ask. -- Steven From omar.aboumrad at gmail.com Wed Aug 21 11:05:52 2013 From: omar.aboumrad at gmail.com (Omar Abou Mrad) Date: Wed, 21 Aug 2013 12:05:52 +0300 Subject: [Tutor] Runestone Python Course In-Reply-To: References: Message-ID: On Wed, Aug 21, 2013 at 7:52 AM, Jim Mooney wrote: > http://interactivepython.org > > Would be nice if it worked though, logged in through my google account, now i get this error which I can do nothing about: Sorry, Something went wrong The error is: invalid request -------------- next part -------------- An HTML attachment was scrubbed... URL: From chris at chrisdown.name Wed Aug 21 12:17:15 2013 From: chris at chrisdown.name (Chris Down) Date: Wed, 21 Aug 2013 12:17:15 +0200 Subject: [Tutor] [OT] Replies go to individuals, not the list? In-Reply-To: <52140A3C.5060204@pearwood.info> References: <20130820230156.GD9251@gopher> <20130820230907.GE9251@gopher> <52140A3C.5060204@pearwood.info> Message-ID: <20130821101713.GB735@gopher> On 2013-08-21 10:30, Steven D'Aprano wrote: > - If there is nowhere I can reasonably trim their comments to establish > context, and my response is just a general reply rather than specifically > responding to specific comments (e.g. if my reply is "thanks for your email, > I'll consider it for the future" sort of thing) then I might top post, > leaving their comments below for context. In such a case, I cut to the conclusion point in their e-mail and reply below it. I don't see why top-posting would be beneficial here, this method is wasteful. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From chris at chrisdown.name Wed Aug 21 12:20:58 2013 From: chris at chrisdown.name (Chris Down) Date: Wed, 21 Aug 2013 12:20:58 +0200 Subject: [Tutor] Runestone Python Course In-Reply-To: References: Message-ID: <20130821102057.GC735@gopher> On 2013-08-20 21:52, Jim Mooney wrote: > This is rather like W3 Schools That doesn't exactly fill me with confidence about the quality of it considering W3Schools is literally the cesspit of web standards misinformation... -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From airscorp at otenet.gr Wed Aug 21 14:40:32 2013 From: airscorp at otenet.gr (Nick Raptis) Date: Wed, 21 Aug 2013 15:40:32 +0300 Subject: [Tutor] Coursera Python Course starts today In-Reply-To: <5212B211.5050900@gmail.com> References: <5212B211.5050900@gmail.com> Message-ID: <5214B540.1010502@otenet.gr> On 20/08/13 03:02, Leam Hall wrote: > https://class.coursera.org/programming1-002/class/index > > Leam > > Leam, your link for some reason redirects on a default coursera page, probably cause I'm not enlisted. I think a better link for the course would be https://www.coursera.org/course/programming1 so that others interested may find it. Nick From joel.goldstick at gmail.com Wed Aug 21 19:31:55 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 21 Aug 2013 13:31:55 -0400 Subject: [Tutor] library: In-Reply-To: <20130821082959.GC24672@ando> References: <2FF8332F-D73A-43A4-A1CB-BC2670993D46@yandex.ru> <20130821082959.GC24672@ando> Message-ID: On Wed, Aug 21, 2013 at 4:29 AM, Steven D'Aprano wrote: > On Tue, Aug 20, 2013 at 10:00:24PM -0500, William Crowder wrote: > >> I am reading posts and watching videos. I am following along >> with the shell, i am retaining the info. But WHAT is a library? > In python libraries are called modules I believe. So you may see either term, and unless someone here corrects me, they are the same. > > In English, a library is a collection of books, or magazines. > > A "software library" is a collection of programs or functions, instead > of books. Sometimes it will be one file, with many functions. Sometimes > it will be many files. > > In Python: > > import math > > > will load the math library, so you can use functions like math.sin, > math.sqrt, and others. > > Does this answer your question? If you need more help, please ask. > > > -- > Steven > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com From chris at chrisdown.name Wed Aug 21 22:25:48 2013 From: chris at chrisdown.name (Chris Down) Date: Wed, 21 Aug 2013 22:25:48 +0200 Subject: [Tutor] library: In-Reply-To: References: <2FF8332F-D73A-43A4-A1CB-BC2670993D46@yandex.ru> <20130821082959.GC24672@ando> Message-ID: <20130821202548.GA1445@gopher> On 2013-08-21 13:31, Joel Goldstick wrote: > In python libraries are called modules I believe. So you may see > either term, and unless someone here corrects me, they are the same. They are often interchangeable, but they do not have to be the same (for example, it is perfectly imaginable that a library contains multiple modules), since "library" is an aesthetic constraint, but "module" isn't. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From cybervigilante at gmail.com Wed Aug 21 23:38:53 2013 From: cybervigilante at gmail.com (Jim Mooney) Date: Wed, 21 Aug 2013 14:38:53 -0700 Subject: [Tutor] Runestone Python Course In-Reply-To: <20130821102057.GC735@gopher> References: <20130821102057.GC735@gopher> Message-ID: That doesn't exactly fill me with confidence about the quality of it considering W3Schools is literally the cesspit of web standards misinformation... ================ I meant that it was like it in that it had an interactive interpreter, but it was much better. So far the info is detailed and valid. I've found no errors - I just dropped one book I was reading when it had an egregious error about python, showing the author was confused, so I'm not tolerant of errors in training material. One other good thing I forgot to mention is that after enlarging a video, right clicking on it allows you to download it in webm format (playable by VLC, MPC, and WMP (kind of - Windows Media Player doesn't see the extension but will play it. Msoft is always a bit behind the times ;') Darn, gmail made a change that makes it harder to quote the sender name. Annoying As a general observation about software, if you make an "improvement," let those who want the old feature keep it. With OOP, that shouldn't be hard. I have no idea what happened with Omar. It could be you have to enroll directly instead of through google. I've found indirect enrollment doesn't always work for a number of things. Jim On 21 August 2013 03:20, Chris Down wrote: > On 2013-08-20 21:52, Jim Mooney wrote: > > This is rather like W3 Schools > > That doesn't exactly fill me with confidence about the quality of it > considering W3Schools is literally the cesspit of web standards > misinformation... > -- Jim More and more, science is showing that animals, even "simple" ones, have awareness and feelings. There is no hard divide, as the rape-the-earth crowd would have us believe.. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Thu Aug 22 00:30:21 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 21 Aug 2013 23:30:21 +0100 Subject: [Tutor] library: In-Reply-To: <2FF8332F-D73A-43A4-A1CB-BC2670993D46@yandex.ru> References: <2FF8332F-D73A-43A4-A1CB-BC2670993D46@yandex.ru> Message-ID: On 21/08/13 04:00, William Crowder wrote: > I am reading posts and watching videos. I am following along with the > shell, i am retaining the info. But WHAT is a library? It varies but in general programming terms is a collection of functions or classes that can be reused by programmers. Most languages have a "standard library" and a collection of additional proprietary libraries. Python's standard library consists of a set of modules, each of which exposes a set of functions or classes for performing related tasks. The documentation for Python's standard library (V2) can be found here: http://docs.python.org/2/py-modindex.html The documentation for GNU C++'s standard library is here: http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-api-4.6/modules.html And for Java it is here: http://docs.oracle.com/javase/7/docs/api/ It's interesting to compare their breadth and nature. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From chris at chrisdown.name Thu Aug 22 00:39:14 2013 From: chris at chrisdown.name (Chris Down) Date: Thu, 22 Aug 2013 00:39:14 +0200 Subject: [Tutor] library: In-Reply-To: References: <2FF8332F-D73A-43A4-A1CB-BC2670993D46@yandex.ru> Message-ID: <20130821223914.GC1445@gopher> On 2013-08-21 23:30, Alan Gauld wrote: > It varies but in general programming terms is a collection of functions or > classes that can be reused by programmers. Most languages have a "standard > library" and a collection of additional proprietary libraries. Unless I'm misunderstanding, don't you mean "third-party", not "proprietary" (or is this a use of "proprietary" that I am not familiar with)? -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From alan.gauld at btinternet.com Thu Aug 22 02:11:59 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 22 Aug 2013 01:11:59 +0100 Subject: [Tutor] library: In-Reply-To: <20130821223914.GC1445@gopher> References: <2FF8332F-D73A-43A4-A1CB-BC2670993D46@yandex.ru> <20130821223914.GC1445@gopher> Message-ID: On 21/08/13 23:39, Chris Down wrote: > On 2013-08-21 23:30, Alan Gauld wrote: >> It varies but in general programming terms is a collection of functions or >> classes that can be reused by programmers. Most languages have a "standard >> library" and a collection of additional proprietary libraries. > > Unless I'm misunderstanding, don't you mean "third-party", not "proprietary" > (or is this a use of "proprietary" that I am not familiar with)? My dictionary says: "proprietary: protected by trademark or patent or copyright; made or produced or distributed by one having exclusive rights" So I mean a third party library, one whose copyright is separate from the standard library. It could, of course, be copyright free or public domain too, so third party may be a more accurate term here. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From oscar.j.benjamin at gmail.com Thu Aug 22 14:03:10 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 22 Aug 2013 13:03:10 +0100 Subject: [Tutor] hi In-Reply-To: <000601ce9da3$a7e1b460$f7a51d20$@orange.mu> References: <000001ce7ee7$d2233170$76699450$@orange.mu> <000901ce8703$4e599b30$eb0cd190$@orange.mu> <000001ce8e33$cefe4650$6cfad2f0$@orange.mu> <000601ce8eaa$c6baabb0$54300310$@orange.mu> <000001ce93b7$24a7d480$6df77d80$@orange.mu> <000501ce945b$ed947f70$c8bd7e50$@orange.mu> <000301ce9542$2c955f50$85c01df0$@orange.mu> <000001ce9590$e1b667a0$a52336e0$@orange.mu> <000001ce9650$fe15e180$fa41a480$@orange.mu> <000701ce9771$187e6bc0$497b4340$@orange.mu> <000101ce979b$1d17ff00$5747fd00$@orange.mu> <000601ce9da3$a7e1b460$f7a51d20$@orange.mu> Message-ID: On 20 August 2013 13:49, Vick wrote: > > From: Oscar Benjamin [mailto:oscar.j.benjamin at gmail.com] > >> Well just send me some tutorial on how to build and obtain the >> coefficients for the butcher tableau for the RK4 as an example, and >> after I've mastered it, I'd give the dopri8 a shot. > > I am up for it so I'll see if I can find time to write a script that shows > how to do it. > > [Vick] Hope you've had the time to code it. I'm waiting for it. Sorry, I haven't found the time yet. It is still on my todo list though! > By the way your code for the Adams-Moulton coefficients are actually the > Adams-Bashforth ones and so I copied it and modified the copy to have the > Adams-Moulton coefficients as well. This means that I have now an nth-order > predictor-corrector method to solve for ODEs. Oh sorry. That'll be a cut and paste error. My code lives in a private software library that I keep meaning to release on PyPI but it's not ready for public consumption in quite a number of ways. I'm glad that you worked it out though. You''ll probably understand what I mean now when I say that the AM or AB integrators need a secondary algorithm to bootstrap. The accuracy of the subsequent AM/AB method depends on the accuracy of that step. In the worst case you can just use rk4 with a very small time-step for this bit though. Oscar From chigga101 at gmail.com Thu Aug 22 14:36:24 2013 From: chigga101 at gmail.com (Matthew Ngaha) Date: Thu, 22 Aug 2013 13:36:24 +0100 Subject: [Tutor] global variables Message-ID: I'm always told to avoid using them. I read discussions on the python irc channel about them but honestly i feel there are some times where i can't avoid using them. Like where i want to keep track of a state variable in many different functions that may or may not alter its value and also not wanting any of the functions to return it to the caller. My question is how many global variables did your last decent sized program have? Also please share any insight you have about them. I do try to avoid them, but is this always possible? From chris at chrisdown.name Thu Aug 22 14:40:34 2013 From: chris at chrisdown.name (Chris Down) Date: Thu, 22 Aug 2013 14:40:34 +0200 Subject: [Tutor] global variables In-Reply-To: References: Message-ID: <20130822124033.GC4577@chrisdown.name> On 2013-08-22 13:36, Matthew Ngaha wrote: > I'm always told to avoid using them. I read discussions on the python > irc channel about them but honestly i feel there are some times where > i can't avoid using them. Like where i want to keep track of a state > variable in many different functions that may or may not alter its > value and also not wanting any of the functions to return it to the > caller. It sounds like you want to use a class. > My question is how many global variables did your last decent sized > program have? Also please share any insight you have about them. I do > try to avoid them, but is this always possible? I don't have any global variables in any of my projects, and I've been programming Python in some capacity for almost 8 years now. Why would you not just use a class if you want to store state? -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From chigga101 at gmail.com Thu Aug 22 15:43:03 2013 From: chigga101 at gmail.com (Matthew Ngaha) Date: Thu, 22 Aug 2013 14:43:03 +0100 Subject: [Tutor] global variables In-Reply-To: <20130822124033.GC4577@chrisdown.name> References: <20130822124033.GC4577@chrisdown.name> Message-ID: On Thu, Aug 22, 2013 at 1:40 PM, Chris Down wrote: > It sounds like you want to use a class. > Why would you not just use a class if you want to store state? I don't feel my program needs a class. Also i have been told to stop using classes by some very experienced Python programmers on irc even though i don't see why. It's confusing being told different things. From chris at chrisdown.name Thu Aug 22 15:52:13 2013 From: chris at chrisdown.name (Chris Down) Date: Thu, 22 Aug 2013 15:52:13 +0200 Subject: [Tutor] global variables In-Reply-To: References: <20130822124033.GC4577@chrisdown.name> Message-ID: <20130822135212.GA6965@chrisdown.name> On 2013-08-22 14:43, Matthew Ngaha wrote: > I don't feel my program needs a class. Also i have been told to stop > using classes by some very experienced Python programmers on irc even > though i don't see why. It's confusing being told different things. Well, if you want to store state, you should really be using a class. What has made you think that your program doesn't "need a class"? There's no "need", there's just what's best suited to your problem case (which you have not made clear, so nobody can comment on it). No experienced Python programmers are going to universally tell you not to use classes, likewise, no experienced Python programmers are going to universally tell you to use them all the time. It's a matter of context and suitability, which is entirely dependent on what it is that you are coding in the first place. I would doubt that anyone has told you "don't ever use classes", because that's nonsense; you've probably misread a dissuasion from that path in a single instance as applying more broadly than was intended. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From alan.gauld at btinternet.com Thu Aug 22 16:04:42 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 22 Aug 2013 15:04:42 +0100 Subject: [Tutor] global variables In-Reply-To: References: Message-ID: On 22/08/13 13:36, Matthew Ngaha wrote: > I'm always told to avoid using them. Global variables in themselves are not the problem. It's how they tend to get used that causes problems. Read-only global values - aka constants (so not really variables!) - are not an issue. Globals that are only changed via a set of dedicated functions are not topo much of a problem - although they should probably be bundled as a class (or module) if you have such features available. > i can't avoid using them. Like where i want to > keep track of a state variable in many > different functions Usually that should be in a class. A class represents a set of operations using common data. Therefore shared state would naturally fit in a class along with the operations which depend on/modify that state. You may then have a single global variable which is the instance of that class. > also not wanting any of the functions to return it to the > caller. Thats more problematic and usually a sign of a bad design. Even if using global variables you should modify them explicitly via return values of functions rather than as hidden side-effects inside other functions. mystate = changestate(mystate, some, other, args) > My question is how many global variables did your last decent sized > program have? I usually have not more than a half dozen plus one per thousand lines of code. So in a 10,000 line program I'd expect to have less than 16 in total (actually I'd hope less than 10!). And many of those would be instances of classes. I would not expect to have more than one or two fundamental typed globals (ints, strings, bools etc), if any. > Also please share any insight you have about them. I do > try to avoid them, but is this always possible? It is possible but only by playing silly games with semantics such as: class MyProgram global1 = 0 global2 = True def __init__(self, lots, of, args, self.inst1 = .... # initialise program runtime vars def run(self) # my program code all goes here # and accesses the class level globals and instance # level runtime and state values if __name__ = __main__": MyProgram(args....).run() [Or alternatively you can hide them inside a database.] Now technically there are no globals but in fact we are just moving them inside the meaningless class and have all the same potential issues with global side effects etc. In my experience there are usually a few globals required for any meaningful program. It's not avoiding globals completely that's important, it's being careful to use them sensibly and with good adherence to the principles of coupling and cohesion in the design. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Thu Aug 22 16:10:55 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 22 Aug 2013 15:10:55 +0100 Subject: [Tutor] global variables In-Reply-To: References: <20130822124033.GC4577@chrisdown.name> Message-ID: On 22/08/13 14:43, Matthew Ngaha wrote: > On Thu, Aug 22, 2013 at 1:40 PM, Chris Down wrote: >> It sounds like you want to use a class. >> Why would you not just use a class if you want to store state? Local coding conventions or programmer skill levels may preclude it. > I don't feel my program needs a class. But in this case it sounds like a class is the best solution. Why would you "feel" that you don't need a class when you have a situation where several functions share common state? That's almost the definition of a class. > Also i have been told to stop using classes by some very > experienced Python programmers on irc Really? What reasons did they give. Unless they are talking about very specific circumstances that doesn't sound like good advice! > It's confusing being told different things. Software engineering, like any branch of engineering, is about learning to use many different tools and selecting the best set for a problem. There are cases where classes are not in the best set, there are cases where many global variables are a good fit. But both of these are the exceptions to the rule and the engineer's job is to identify when a genuine exception exists and make the right choice. There is never a single right answer. Sorry, but that's life. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From chigga101 at gmail.com Thu Aug 22 16:12:47 2013 From: chigga101 at gmail.com (Matthew Ngaha) Date: Thu, 22 Aug 2013 15:12:47 +0100 Subject: [Tutor] global variables In-Reply-To: <20130822135212.GA6965@chrisdown.name> References: <20130822124033.GC4577@chrisdown.name> <20130822135212.GA6965@chrisdown.name> Message-ID: On Thu, Aug 22, 2013 at 2:52 PM, Chris Down wrote: >I would doubt that anyone has told you "don't ever use classes", because > that's nonsense; you've probably misread a dissuasion from that path in a > single instance as applying more broadly than was intended. I am being totally honest here. I was very confused at the time and i said i didn't agree because it's what i had put so much effort into learning. They went on to say at some well known Python talks speakers have stated why using OOP (especially inheritance, but not excluding any others) is very bad design and the same thing can always be achieved without it. To be clear they said every use case OOP is the worst option. I asked what about GUIs which their design is strongly based around OOP? and they sad GUIs are badly designed to begin with so it proves the point about OOP. From chigga101 at gmail.com Thu Aug 22 16:27:21 2013 From: chigga101 at gmail.com (Matthew Ngaha) Date: Thu, 22 Aug 2013 15:27:21 +0100 Subject: [Tutor] global variables In-Reply-To: References: Message-ID: On Thu, Aug 22, 2013 at 3:04 PM, Alan Gauld wrote: > On 22/08/13 13:36, Matthew Ngaha wrote: > Global variables in themselves are not the problem. > It's how they tend to get used that causes problems. > Globals that are only changed via a set of > dedicated functions are not topo much of a > problem - although they should probably be > bundled as a class (or module) > > Usually that should be in a class. A class represents > a set of operations using common data. Therefore shared > state would naturally fit in a class along with the > operations which depend on/modify that state. > Oh this makes a lot of sense. Sorry about disagreeing with Chris i now see that perhaps a class is definately the best choice for keeping a state variable. > Thats more problematic and usually a sign of a bad design. > Even if using global variables you should modify them > explicitly via return values of functions rather than > as hidden side-effects inside other functions. > > mystate = changestate(mystate, some, other, args) Yeah i have to agree about the bad design now i think about it. I didn't see the issues before reading this > It is possible but only by playing silly games with > semantics such as: > > class MyProgram > global1 = 0 > global2 = True > > [Or alternatively you can hide them inside a database.] > > Now technically there are no globals but in fact we are > just moving them inside the meaningless class and have > all the same potential issues with global side effects > etc. About the class variables, i have used them a lot without realising they had all the same side effects as global variables. Something i now have to try a different approach with. > In my experience there are usually a few globals required > for any meaningful program. It's not avoiding globals > completely that's important, it's being careful to use > them sensibly and with good adherence to the principles > of coupling and cohesion in the design. Thanks alot! This is definately a lot of food for choice and will help in my future decisions. I'm happy i asked this question as i can honestly say i had been developing some bad habbits. The responses have definately been helpful about program design and not just global variables. From chris at chrisdown.name Thu Aug 22 16:32:30 2013 From: chris at chrisdown.name (Chris Down) Date: Thu, 22 Aug 2013 16:32:30 +0200 Subject: [Tutor] global variables In-Reply-To: References: <20130822124033.GC4577@chrisdown.name> <20130822135212.GA6965@chrisdown.name> Message-ID: <20130822143229.GA8494@chrisdown.name> On 2013-08-22 15:12, Matthew Ngaha wrote: > I am being totally honest here. I was very confused at the time and i said i > didn't agree because it's what i had put so much effort into learning. They > went on to say at some well known Python talks speakers have stated why using > OOP (especially inheritance, but not excluding any others) is very bad design > and the same thing can always be achieved without it. To be clear they said > every use case OOP is the worst option. I asked what about GUIs which their > design is strongly based around OOP? and they sad GUIs are badly designed to > begin with so it proves the point about OOP. Were these "expert Python programmers" smoking crack cocaine at the time? -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From joel.goldstick at gmail.com Thu Aug 22 16:35:56 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 22 Aug 2013 10:35:56 -0400 Subject: [Tutor] global variables In-Reply-To: References: <20130822124033.GC4577@chrisdown.name> <20130822135212.GA6965@chrisdown.name> Message-ID: On Thu, Aug 22, 2013 at 10:12 AM, Matthew Ngaha wrote: > On Thu, Aug 22, 2013 at 2:52 PM, Chris Down wrote: >>I would doubt that anyone has told you "don't ever use classes", because >> that's nonsense; you've probably misread a dissuasion from that path in a >> single instance as applying more broadly than was intended. > > I am being totally honest here. I was very confused at the time and i > said i didn't agree because it's what i had put so much effort into > learning. They went on to say at some well known Python talks speakers > have stated why using OOP (especially inheritance, but not excluding > any others) is very bad design and the same thing can always be > achieved without it. To be clear they said every use case OOP is the > worst option. I asked what about GUIs which their design is strongly > based around OOP? and they sad GUIs are badly designed to begin with > so it proves the point about OOP. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor Discussions among developers concerning the suitability of global variables have been going on at least since the 1970s. In my view they are the data equivalent of the 'goto' statement which has pretty much disappeared from acceptable coding practice. Think of them this way: Take a group of housemates who all use the same refrigerator. They all work different hours, spend time with different friends, and never talk to each other. When Bob buys beer, and later comes home to find there is no beer in the refrigerator it frustrates him. He doesn't even know who to complain to because anyone in the house could have taken it -- even a visitor. So, globals seem like a great idea to start with. But over time they cause trouble. I think you should write your code with globals if you think its best. Probably your code isn't that important, and it won't matter what you did in this program in a year or two. Anyway you will be learning. If your code is important for work, or some other purpose, over time it will be changed, and expanded, and problems will arise. Then you, or worse some one else, will have to figure out what is going wrong. That's where you will rue the day you sprinkled globals here and there. -- Joel Goldstick http://joelgoldstick.com From amckenzie4 at gmail.com Thu Aug 22 16:59:00 2013 From: amckenzie4 at gmail.com (Andy McKenzie) Date: Thu, 22 Aug 2013 10:59:00 -0400 Subject: [Tutor] global variables In-Reply-To: References: <20130822124033.GC4577@chrisdown.name> <20130822135212.GA6965@chrisdown.name> Message-ID: On Thu, Aug 22, 2013 at 10:12 AM, Matthew Ngaha wrote: > On Thu, Aug 22, 2013 at 2:52 PM, Chris Down wrote: > >I would doubt that anyone has told you "don't ever use classes", because > > that's nonsense; you've probably misread a dissuasion from that path in a > > single instance as applying more broadly than was intended. > > I am being totally honest here. I was very confused at the time and i > said i didn't agree because it's what i had put so much effort into > learning. They went on to say at some well known Python talks speakers > have stated why using OOP (especially inheritance, but not excluding > any others) is very bad design and the same thing can always be > achieved without it. To be clear they said every use case OOP is the > worst option. I asked what about GUIs which their design is strongly > based around OOP? and they sad GUIs are badly designed to begin with > so it proves the point about OOP. > OK, I'm not a fantastic programmer in any language, but this strikes me as someone with an axe to grind giving bad advice to new programmers. Here are a few of the things that I see wrong with their statements: 1) For almost every option in programming, there's at least one case where it's a good idea, design-wise. Saying "in every use case OOP is the worst option" is absurd. Of course there are cases where it's not the worst option. There are also cases where it is. That goes for just about everything. 2) If they think OOP is always a bad idea, WHY are they using Python? Isn't object orientation kind of the whole POINT of Python? From python.org: "Python is an interpreted, object-oriented, high-level programming language with dynamic semantics." If they honestly believe that object oriented programming is always a bad idea, they really need to pick a different language. Perl, maybe, although even in Perl people are doing object oriented work. I have trouble believing that someone who believes OOP is inherently bad is a current high-level programmer in Python, unless they're trapped in a job they don't want to be doing. 3) Bad design in a product does not mean bad design in the tool used to build it. I've built some really terrible things out of wood with really nice tools. I've watched people use really nice drafting tools to design houses that would have been unusable for living in. Saying badly designed GUIs prove that OOP is bad is, frankly, illogical at best and stupid at worst. I strongly suspect that either the speaker they were listening to wasn't clear, or they weren't clear. Either that, or the speaker or whoever you were talking to mis-represented their ability level. Andy -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Thu Aug 22 17:37:44 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 23 Aug 2013 01:37:44 +1000 Subject: [Tutor] global variables In-Reply-To: References: <20130822124033.GC4577@chrisdown.name> Message-ID: <52163048.40403@pearwood.info> On 22/08/13 23:43, Matthew Ngaha wrote: > On Thu, Aug 22, 2013 at 1:40 PM, Chris Down wrote: > >> It sounds like you want to use a class. >> Why would you not just use a class if you want to store state? > > I don't feel my program needs a class. Also i have been told to stop > using classes by some very experienced Python programmers on irc even > though i don't see why. It's confusing being told different things. If you're talking about #python on Freenode, in my experience, they are rude and unpleasant people with an inflated sense of their own intelligence. But they possibly think the same about me ;-) -- Steven From steve at pearwood.info Thu Aug 22 17:35:53 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 23 Aug 2013 01:35:53 +1000 Subject: [Tutor] global variables In-Reply-To: References: Message-ID: <52162FD9.8070709@pearwood.info> On 22/08/13 22:36, Matthew Ngaha wrote: > I'm always told to avoid using them. I read discussions on the python > irc channel about them but honestly i feel there are some times where > i can't avoid using them. Like where i want to keep track of a state > variable in many different functions that may or may not alter its > value and also not wanting any of the functions to return it to the > caller. > > My question is how many global variables did your last decent sized > program have? Also please share any insight you have about them. I do > try to avoid them, but is this always possible? Good question! And one that needs a long answer. Yes, it is possible to avoid global variables. The proof of this, if you need any, is programming languages like Haskell which are purely functional languages, which means they don't have variables at all! Everything is a fixed value, usually created on the fly as needed, and thrown away when no longer required, but never modified[1]. Need to change something? You create a new value rather than modify an existing one. Haskell programmers are capable of writing large programs, and they do it without global variables. Functional programming is a very different paradigm, and one which takes a while to get used to. Haskell, which is pure functional, can be hard to wrap your head around, but Python is strongly influenced by Haskell and Lisp and could be considered a semi-functional language. If you've ever used list comprehensions or generator expressions, you've got a small taste of functional programming. Truth be told, you *could* write a large, non-trivial Python program in entirely functional style, but without a clever Lisp or Haskell compiler behind it, it would probably be just as slow to run as it would be hard to write. In Python, you should consider global variables to be something to be minimized rather than entirely avoided. In my experience, global variables are well-suited to dealing with user preferences and command-line options, and otherwise best avoided. But notice that user prefs and cmd line options aren't exactly *variables*, since they are typically set once, when the program starts up, and then never -- or at least hardly ever -- changed. Your functions may read their value, but they will not usually change their value. That brings us to why global variables are unsafe. They're not unsafe because they are global. Global constants, or almost-constants, are perfectly fine. They're unsafe because they vary unpredictably. They introduce coupling between functions which otherwise should be independent. For example, suppose we have a global variable and three functions, f, g, h, all of which read and write to the global under various situations. Now suppose you call f() -- the result you get depends on whether or not g() or h() have been called first, since they may change the global. And likewise, the result g() gives depends on whether or not f() or h() have been called, and similarly for h(). So instead of the result of f() depending in a nice clean way only on the arguments passed to it, instead it depends in a messy, convoluted, confusing way on the history of calls to separate functions that may have nothing to do with f(). You now have a tangle of couplings between functions: f depends on g and h g depends on f and h h depends on f and g Now imagine ten globals and twenty functions. Globals introduce "action at a distance". A function in one part of your program can reach out over a great distance and effect another function's result, just by changing a global. That's a bad thing and should be minimized or eliminated. So the real problem here is *state*. If your functions didn't depend on mutable state, but only on arguments passed to them, the problem of action at a distance would go away. This is where functional programming comes in: they eliminate mutable state altogether. Once a value is created, you can't change it, only throw it away and create a new one. In Python we don't necessarily go quite to that extreme, but it's still a good ideal to work towards. One mistake people have is to create a class just to hold global variables, and then think that they are virtuous because "it's not a global". So then they end up with something like this: class Everything_I_Need: def __init__(self): self.a = 1 self.b = 2 self.c = 3 # etc. everything = Everything_I_Need() f(everything) g(everything) h(everything) but of course this is just global variables in disguise, and still suffers from the same problem with strong coupling and action at a distance. [1] Never say never. Haskell actually does have a way to create modifiable values, but to get an idea of how people consider it, it is often called the "unsafePerformIO hack". -- Steven From steve at pearwood.info Thu Aug 22 17:58:29 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 23 Aug 2013 01:58:29 +1000 Subject: [Tutor] global variables In-Reply-To: References: Message-ID: <52163525.2040807@pearwood.info> On 22/08/13 22:36, Matthew Ngaha wrote: > My question is how many global variables did your last decent sized > program have? Also please share any insight you have about them. I do > try to avoid them, but is this always possible? Eiffel is another language that aims to eliminate global variables entirely. Here is an excellent article by the creator of Eiffel explaining why and how: http://archive.eiffel.com/doc/manuals/technology/bmarticles/joop/globals.html -- Steven From steve at pearwood.info Thu Aug 22 18:00:20 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 23 Aug 2013 02:00:20 +1000 Subject: [Tutor] global variables In-Reply-To: <20130822135212.GA6965@chrisdown.name> References: <20130822124033.GC4577@chrisdown.name> <20130822135212.GA6965@chrisdown.name> Message-ID: <52163594.3010303@pearwood.info> On 22/08/13 23:52, Chris Down wrote: > On 2013-08-22 14:43, Matthew Ngaha wrote: >> I don't feel my program needs a class. Also i have been told to stop >> using classes by some very experienced Python programmers on irc even >> though i don't see why. It's confusing being told different things. > > Well, if you want to store state, you should really be using a class. Depends on how much state and what you do with it. But fundamentally, if you want to store state, you should try very, very hard to *avoid* storing state unless you really need to. Or at least, make sure that there is a very strict demarcation between functions (or methods) which change that state, and those which do not. The ultimate aim is to avoid the bad parts of storing state, without necessarily going all the way to strict functional code: - reduce or eliminate side-effects wherever possible; - functions that have side-effects should do so only in well-understood and innocuous ways; - eliminate coupling between unrelated parts of code; - reduce coupling within related parts of code; - avoid action at a distance whenever possible. If you keep these aims in mind, the problem of global variables will solve itself. -- Steven From steve at pearwood.info Thu Aug 22 18:10:50 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 23 Aug 2013 02:10:50 +1000 Subject: [Tutor] global variables In-Reply-To: References: <20130822124033.GC4577@chrisdown.name> <20130822135212.GA6965@chrisdown.name> Message-ID: <5216380A.1070901@pearwood.info> On 23/08/13 00:12, Matthew Ngaha wrote: > On Thu, Aug 22, 2013 at 2:52 PM, Chris Down wrote: >> I would doubt that anyone has told you "don't ever use classes", because >> that's nonsense; you've probably misread a dissuasion from that path in a >> single instance as applying more broadly than was intended. > > I am being totally honest here. I was very confused at the time and i > said i didn't agree because it's what i had put so much effort into > learning. They went on to say at some well known Python talks speakers > have stated why using OOP (especially inheritance, but not excluding > any others) is very bad design and the same thing can always be > achieved without it. To be clear they said every use case OOP is the > worst option. I asked what about GUIs which their design is strongly > based around OOP? and they sad GUIs are badly designed to begin with > so it proves the point about OOP. Well I agree with that last point. As far as "avoid OOP", I suspect they were thinking about this (in)famous video: http://pyvideo.org/video/880/stop-writing-classes Here is a counter-viewpoint: http://lucumr.pocoo.org/2013/2/13/moar-classes/ I think both have excellent advice. Many people do write unnecessary, complicated classes, and many people do unnecessarily lock up the inner workings of their code. -- Steven From steve at pearwood.info Thu Aug 22 18:18:52 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 23 Aug 2013 02:18:52 +1000 Subject: [Tutor] global variables In-Reply-To: References: <20130822124033.GC4577@chrisdown.name> <20130822135212.GA6965@chrisdown.name> Message-ID: <521639EC.8090903@pearwood.info> On 23/08/13 00:59, Andy McKenzie wrote: > Isn't object orientation kind of the whole POINT of Python? From python.org: > "Python is an interpreted, object-oriented, high-level programming language > with dynamic semantics." Well, yes and no. Python is an object-oriented language in the sense that all of Python is built using objects. Everything, including modules, functions, ints, strings, yes, even classes themselves, are objects. But also no, in the sense that the code you write doesn't have to be written using OOP techniques. Python is a multi-paradigm language in the sense that you can write code using any of these styles: - object-oriented - functional - procedural - imperative - or a mix of all of the above. -- Steven From chigga101 at gmail.com Thu Aug 22 18:49:00 2013 From: chigga101 at gmail.com (Matthew Ngaha) Date: Thu, 22 Aug 2013 17:49:00 +0100 Subject: [Tutor] global variables In-Reply-To: <52162FD9.8070709@pearwood.info> References: <52162FD9.8070709@pearwood.info> Message-ID: On Thu, Aug 22, 2013 at 4:35 PM, Steven D'Aprano wrote: > Good question! And one that needs a long answer. Hey i just checked mail again... A big thank you for your responses, i will read all of them now.. im pretty my programs/designs will be much cleaner by the time i done. And yes i was talking about freenode:) From leamhall at gmail.com Thu Aug 22 22:14:41 2013 From: leamhall at gmail.com (leam hall) Date: Thu, 22 Aug 2013 16:14:41 -0400 Subject: [Tutor] How much in a "try" block? Message-ID: If I have a series of tasks that depend on X happening, should I put them all in the same "try" block or just put X in there and exit out if it fails? Thanks! Leam -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From amitsaha.in at gmail.com Thu Aug 22 22:20:45 2013 From: amitsaha.in at gmail.com (Amit Saha) Date: Fri, 23 Aug 2013 06:20:45 +1000 Subject: [Tutor] How much in a "try" block? In-Reply-To: References: Message-ID: On Fri, Aug 23, 2013 at 6:14 AM, leam hall wrote: > If I have a series of tasks that depend on X happening, should I put them > all in the same "try" block or just put X in there and exit out if it fails? You are right about the latter. You should put only the statement which you expect to raise the exception. -- http://echorand.me From chris at chrisdown.name Thu Aug 22 22:27:28 2013 From: chris at chrisdown.name (Chris Down) Date: Thu, 22 Aug 2013 22:27:28 +0200 Subject: [Tutor] How much in a "try" block? In-Reply-To: References: Message-ID: <20130822202727.GB922@chrisdown.name> On 2013-08-23 06:20, Amit Saha wrote: > On Fri, Aug 23, 2013 at 6:14 AM, leam hall wrote: > > If I have a series of tasks that depend on X happening, should I put them > > all in the same "try" block or just put X in there and exit out if it fails? > > You are right about the latter. You should put only the statement > which you expect to raise the exception. You can also use the "else" clause if there is stuff you want to run if the try block doesn't raise the caught exception, which avoids putting it in "try" if you don't intend to exit from the exception. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From wolfrage8765 at gmail.com Fri Aug 23 00:06:53 2013 From: wolfrage8765 at gmail.com (wolfrage8765 at gmail.com) Date: Thu, 22 Aug 2013 18:06:53 -0400 Subject: [Tutor] global variables In-Reply-To: <521639EC.8090903@pearwood.info> References: <20130822124033.GC4577@chrisdown.name> <20130822135212.GA6965@chrisdown.name> <521639EC.8090903@pearwood.info> Message-ID: Steve, Thanks for all of this information. It seems the OP has sparked the kind of discussion that I had hopped for when I posted http://thread.gmane.org/gmane.comp.python.tutor/83251 Luckily though I did get an excellent response from Alan. http://thread.gmane.org/gmane.comp.python.tutor/83251/focus=83252 Still what you have posted here has been a good read, so, thank you. -- Jordan On Thu, Aug 22, 2013 at 12:18 PM, Steven D'Aprano wrote: > On 23/08/13 00:59, Andy McKenzie wrote: >> >> Isn't object orientation kind of the whole POINT of Python? From >> python.org: >> "Python is an interpreted, object-oriented, high-level programming >> language >> with dynamic semantics." > > > > Well, yes and no. > > Python is an object-oriented language in the sense that all of Python is > built using objects. Everything, including modules, functions, ints, > strings, yes, even classes themselves, are objects. > > But also no, in the sense that the code you write doesn't have to be written > using OOP techniques. Python is a multi-paradigm language in the sense that > you can write code using any of these styles: > > - object-oriented > > - functional > > - procedural > > - imperative > > - or a mix of all of the above. > > > -- > Steven > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Fri Aug 23 02:30:21 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 23 Aug 2013 01:30:21 +0100 Subject: [Tutor] How much in a "try" block? In-Reply-To: <20130822202727.GB922@chrisdown.name> References: <20130822202727.GB922@chrisdown.name> Message-ID: On 22/08/13 21:27, Chris Down wrote: > You can also use the "else" clause if there is stuff you want to run if the try > block doesn't raise the caught exception, which avoids putting it in "try" if > you don't intend to exit from the exception. I admit that I've never really found a use for else in a try block. I don;t see much advantage in try: f(x) except MyError: pass else: g(x) h(x) over try: f(x) except MyError: pass g(x) h(x) Unless you really only want g(x) executed if there is no MyError exception but want h(x) executed regardless. I guess where h() is not using x it might be helpful but in most(all?) of my code I've usually bailed when x has gone wrong or I've fixed things such that hg() and h() are required. I'm curious, how often do others use the try/else combination? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From amitsaha.in at gmail.com Fri Aug 23 02:40:52 2013 From: amitsaha.in at gmail.com (Amit Saha) Date: Fri, 23 Aug 2013 10:40:52 +1000 Subject: [Tutor] How much in a "try" block? In-Reply-To: References: <20130822202727.GB922@chrisdown.name> Message-ID: On Fri, Aug 23, 2013 at 10:30 AM, Alan Gauld wrote: > On 22/08/13 21:27, Chris Down wrote: > >> You can also use the "else" clause if there is stuff you want to run if >> the try >> block doesn't raise the caught exception, which avoids putting it in "try" >> if >> you don't intend to exit from the exception. > > > I admit that I've never really found a use for else in a try block. > I don;t see much advantage in > > try: f(x) > except MyError: > pass > else: > g(x) > h(x) > > over > > try: f(x) > except MyError: > pass > g(x) > h(x) > > Unless you really only want g(x) executed if there > is no MyError exception but want h(x) executed regardless. > > I guess where h() is not using x it might be helpful but in most(all?) of > my code I've usually bailed when x has gone > wrong or I've fixed things such that hg() and h() are required. > > I'm curious, how often do others use the try/else combination? I think one use of writing something in an "else" would be to write those statements which are part of a "block" (loosely used) of statements which would depend on the statement in the try block. That doesn't change the way it works if they were not written in an "else", but perhaps makes it clear that those statements are to be executed only when there is no exception. Explicit is better than implicit, may be? -- http://echorand.me From dipo.elegbede at dipoelegbede.com Fri Aug 23 03:02:16 2013 From: dipo.elegbede at dipoelegbede.com (Oladipupo Elegbede) Date: Fri, 23 Aug 2013 02:02:16 +0100 Subject: [Tutor] How much in a "try" block? In-Reply-To: References: Message-ID: Depending on what x is and also whether or not you've decided to go the 'try'route, if, else may be useful from the way you sound. My thought. On 22 Aug 2013 21:16, "leam hall" wrote: > If I have a series of tasks that depend on X happening, should I put them > all in the same "try" block or just put X in there and exit out if it fails? > > Thanks! > > Leam > > > -- > Mind on a Mission > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From steve at pearwood.info Fri Aug 23 03:26:24 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 23 Aug 2013 11:26:24 +1000 Subject: [Tutor] How much in a "try" block? In-Reply-To: References: <20130822202727.GB922@chrisdown.name> Message-ID: <5216BA40.3080500@pearwood.info> On 23/08/13 10:30, Alan Gauld wrote: > I admit that I've never really found a use for else in a try block. > I don;t see much advantage in > > try: f(x) > except MyError: > pass > else: > g(x) > h(x) > > over > > try: f(x) > except MyError: > pass > g(x) > h(x) > > Unless you really only want g(x) executed if there > is no MyError exception but want h(x) executed regardless. That's exactly the use-case for try...else. Admittedly, it's a relatively uncommon use-case, but when you need it, you need it. Instead of writing something like this: failed = False try: do_stuff() except SomeException: failed = True handle_error() if not failed: do_something_else() always_do_this() a cleaner design is to use try...else: try: do_stuff() except SomeException: handle_error() else: do_something_else() always_do_this() The advantage of try...else is even more obvious when you combine it with a finally clause: try: do_stuff() except SomeException: handle_error() else: do_something_else() finally: cleanup() always_do_this() The cleanup function runs no matter how you exit the try block, whether it is via the except clause, the else clause, or even an unhandled exception. In this case, do_something_else is supposed to run before cleanup. I might be tempted to write this instead: failed = False try: do_stuff() except SomeException: failed = True handle_error() finally: cleanup() if not failed: do_something_else() always_do_this() but that's wrong, because now do_something_else runs after cleanup. So I would have to use two try blocks to match the behaviour of try...else...finally. try: failed = False try: do_stuff() except SomeException: failed = True handle_error() if not failed: do_something_else() finally: cleanup() always_do_this() > I'm curious, how often do others use the try/else combination? Rarely :-) In my statistics module[1], out of nearly 2300 lines of code including doc strings, I use "else" just ten times in total and none of them are from try...else. On the other hand, in my startup file that runs when I launch the Python interactive interpreter, a mere 157 lines, I have *four* try...else blocks. So it really depends on the nature of the code. Here is a typical example: # === Command line completion and history === try: import history except ImportError: print('*** warning: command line history not available ***') else: history = history.History() [1] http://www.python.org/dev/peps/pep-0450/ -- Steven From jai633 at g.rwu.edu Fri Aug 23 04:45:38 2013 From: jai633 at g.rwu.edu (Jing Ai) Date: Thu, 22 Aug 2013 22:45:38 -0400 Subject: [Tutor] How to present python experience (self-taught) to potential employer Message-ID: Hi everyone, This is Jing and I am a recent college graduate with Biology and Public Health background. I'm currently learning python on my own when i have time off from my PH internship. There's a job posting that looks really idea for me in the near future (a PH Research position) that requires "Python experience" and I wonder if any of you have any suggestions how I can demonstrate my python skills if I'm learning it on my own as opposed to taking courses? Some people had previously suggested GitHub, but it seems to only show my abilities to read python code and detect bugs, but not abilities to write python code. Some others suggested doing a project of my own, but I don't currently have any data or problem to solve in my field. Thanks so much! Jing -------------- next part -------------- An HTML attachment was scrubbed... URL: From jai633 at g.rwu.edu Fri Aug 23 04:50:33 2013 From: jai633 at g.rwu.edu (Jing Ai) Date: Thu, 22 Aug 2013 22:50:33 -0400 Subject: [Tutor] python tutoring In-Reply-To: References: Message-ID: Hi Trent, I was once wondering about the similar question, but I discovered that Python has a huge user group (stationed across the world) and here's a site that includes the local links to all the user groups around the world, South Korea is included! You can probably find help locally from there. http://wiki.python.org/moin/LocalUserGroups#Korea On Tue, Aug 20, 2013 at 9:22 AM, Fowler, Trent wrote: > Hello, > > Not long ago I came across the website of a professional programmer > offering python tutoring services: > > http://www.jeffknupp.com/python-tutoring/ > > I have attempted to contact him because I am interested but I've been > unable to get in touch. I was wondering if anyone knew of people offering > similar services. I am a self-starter and highly motivated, but I live in > a small town in South Korea and I don't have any friends who program. > Since I also don't have a computer science background and python is my > first language, I really need someone who can help me with the beginning > stages. Often times when I run into a problem not only do I not know how > to solve it, I don't even know how to ask the questions that will help > someone else solve it. > > I don't want to be spoon-fed, just gently nudged and guided. I'm on a > budget but I'd be willing to pay for a good teacher. Preliminary googling > has turned up precious little, so I thought someone here might be able to > point me in the right direction. > > Thanks, > > -Trent. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -------------- next part -------------- An HTML attachment was scrubbed... URL: From amitsaha.in at gmail.com Fri Aug 23 05:12:03 2013 From: amitsaha.in at gmail.com (Amit Saha) Date: Fri, 23 Aug 2013 13:12:03 +1000 Subject: [Tutor] How to present python experience (self-taught) to potential employer In-Reply-To: References: Message-ID: Hi Jing Ai, On Fri, Aug 23, 2013 at 12:45 PM, Jing Ai wrote: > Hi everyone, > This is Jing and I am a recent college graduate with Biology and Public > Health background. I'm currently learning python on my own when i have time > off from my PH internship. There's a job posting that looks really idea for > me in the near future (a PH Research position) that requires "Python > experience" and I wonder if any of you have any suggestions how I can > demonstrate my python skills if I'm learning it on my own as opposed to > taking courses? > > Some people had previously suggested GitHub, but it seems to only show my > abilities to read python code and detect bugs, but not abilities to write > python code. Some others suggested doing a project of my own, but I don't > currently have any data or problem to solve in my field. I am not from your background. In your field of work do you need to do lot of data analysis (read statistical analysis)?. Do you think you could find something like that and something of your interest? You could then use Python (the language and related tools) to perform data analysis, presenting data graphically, etc to work the data and infer some relevant conclusions. For example, http://www.quandl.com/ has data sets related to various fields of study. Does that sound like something you may find interesting and is relevant? Best, Amit. -- http://echorand.me From amitsaha.in at gmail.com Fri Aug 23 05:56:49 2013 From: amitsaha.in at gmail.com (Amit Saha) Date: Fri, 23 Aug 2013 13:56:49 +1000 Subject: [Tutor] How to present python experience (self-taught) to potential employer In-Reply-To: References: Message-ID: On Fri, Aug 23, 2013 at 1:52 PM, Jing Ai wrote: > @Amit > Thank you for your suggestions! I'll look into the data there and see if > there's something relevant that I can use to do a project. Yes I believe it > would involve some data analysis (and I may need to learn R as well or use > RPy). Do you think one project is sufficient to demonstrate my skills if > it's in-depth? Or does it take several projects? Hmm I am not sure. But, depends on how much time you have. If you can do one "big" project that demonstrates a number of your skills - use of Python and one or more of the scientific libraries, that perhaps speaks fair bit about what you know. Also, consider using version control for your projects and of course, unit testing. I also suggest looking into Sphinx for documentation of your project. They also demonstrate that you know some of the things that you need to beyond just writing programs. Best of luck. -Amit. From jai633 at g.rwu.edu Fri Aug 23 05:52:45 2013 From: jai633 at g.rwu.edu (Jing Ai) Date: Thu, 22 Aug 2013 23:52:45 -0400 Subject: [Tutor] How to present python experience (self-taught) to potential employer In-Reply-To: References: Message-ID: @Amit Thank you for your suggestions! I'll look into the data there and see if there's something relevant that I can use to do a project. Yes I believe it would involve some data analysis (and I may need to learn R as well or use RPy). Do you think one project is sufficient to demonstrate my skills if it's in-depth? Or does it take several projects? @Japhy Thanks for your response. I don't really mean that I can't think of any problems in my fields of study, but rather that I'm inexperienced in selecting a specific problem that's appropriate (not too simple or complex) to work with based on my current Python experience. I agree that python skills are demonstrated through writing though. On Thu, Aug 22, 2013 at 11:12 PM, Amit Saha wrote: > Hi Jing Ai, > > On Fri, Aug 23, 2013 at 12:45 PM, Jing Ai wrote: > > Hi everyone, > > This is Jing and I am a recent college graduate with Biology and Public > > Health background. I'm currently learning python on my own when i have > time > > off from my PH internship. There's a job posting that looks really idea > for > > me in the near future (a PH Research position) that requires "Python > > experience" and I wonder if any of you have any suggestions how I can > > demonstrate my python skills if I'm learning it on my own as opposed to > > taking courses? > > > > Some people had previously suggested GitHub, but it seems to only show my > > abilities to read python code and detect bugs, but not abilities to write > > python code. Some others suggested doing a project of my own, but I > don't > > currently have any data or problem to solve in my field. > > I am not from your background. In your field of work do you need to do > lot of data analysis (read statistical analysis)?. Do you think you > could find something like that and something of your interest? You > could then use Python (the language and related tools) to perform data > analysis, presenting data graphically, etc to work the data and infer > some relevant conclusions. For example, http://www.quandl.com/ has > data sets related to various fields of study. > > Does that sound like something you may find interesting and is relevant? > > Best, > Amit. > -- > http://echorand.me > -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Fri Aug 23 09:48:47 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 23 Aug 2013 08:48:47 +0100 Subject: [Tutor] How to present python experience (self-taught) to potential employer In-Reply-To: References: Message-ID: On 23/08/13 03:45, Jing Ai wrote: > time off from my PH internship. There's a job posting that looks really > idea for me in the near future (a PH Research position) that requires > "Python experience" and I wonder if any of you have any suggestions how > I can demonstrate my python skills if I'm learning it on my own as > opposed to taking courses? I only took a course for 3 of the 20-30 programming languages I know/use. A course should not be necessary. Working code should be. > Some people had previously suggested GitHub, but it seems to only show > my abilities to read python code and detect bugs, but not abilities to > write python code. Any Python based open source project should offer opportunities to document/test/debug/fix and *enhance* the software. Get involved with your favourite project and make a difference. You can also get involved with the Python community here on tutor and the main Python list. Your comments and responses can demonstrate your level of understanding. Since its a research position you are interested in they are probably more interested in your coding ability than in your general software engineering skills, but its worth developing good engineering skills too. (TDD, Version control, Documentation, etc) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From japhy at pearachute.com Fri Aug 23 05:34:13 2013 From: japhy at pearachute.com (Japhy Bartlett) Date: Thu, 22 Aug 2013 23:34:13 -0400 Subject: [Tutor] How to present python experience (self-taught) to potential employer In-Reply-To: References: Message-ID: Jing - You demonstrate skill at writing python by writing python. If you don't have data, write something to scrape data. If you seriously can't think of any problems or interesting side projects to solve in either of your fields, bluntly, you're almost certainly worthless as a researcher. Go build anything! On Thu, Aug 22, 2013 at 10:45 PM, Jing Ai wrote: > Hi everyone, > This is Jing and I am a recent college graduate with Biology and Public > Health background. I'm currently learning python on my own when i have > time off from my PH internship. There's a job posting that looks really > idea for me in the near future (a PH Research position) that requires > "Python experience" and I wonder if any of you have any suggestions how I > can demonstrate my python skills if I'm learning it on my own as opposed to > taking courses? > > Some people had previously suggested GitHub, but it seems to only show my > abilities to read python code and detect bugs, but not abilities to write > python code. Some others suggested doing a project of my own, but I don't > currently have any data or problem to solve in my field. > > Thanks so much! > > Jing > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From japhy at pearachute.com Fri Aug 23 07:01:26 2013 From: japhy at pearachute.com (Japhy Bartlett) Date: Fri, 23 Aug 2013 01:01:26 -0400 Subject: [Tutor] How to present python experience (self-taught) to potential employer In-Reply-To: References: Message-ID: One project is fine, unless your competition has finished two. Start at least with one script in each language that you want on your resume that does some sort of analysis on a set of data. With all due respect to Amit, if you are going for academic work don't bother with tests or documentation, proceed directly towards a demo that makes charts. On Thu, Aug 22, 2013 at 11:56 PM, Amit Saha wrote: > On Fri, Aug 23, 2013 at 1:52 PM, Jing Ai wrote: > > @Amit > > Thank you for your suggestions! I'll look into the data there and see if > > there's something relevant that I can use to do a project. Yes I > believe it > > would involve some data analysis (and I may need to learn R as well or > use > > RPy). Do you think one project is sufficient to demonstrate my skills if > > it's in-depth? Or does it take several projects? > > Hmm I am not sure. But, depends on how much time you have. If you can > do one "big" project that demonstrates a number of your skills - use > of Python and one or more of the scientific libraries, that perhaps > speaks fair bit about what you know. Also, consider using version > control for your projects and of course, unit testing. I also suggest > looking into Sphinx for documentation of your project. They also > demonstrate that you know some of the things that you need to beyond > just writing programs. > > Best of luck. > -Amit. > -------------- next part -------------- An HTML attachment was scrubbed... URL: From fal at libero.it Fri Aug 23 10:18:27 2013 From: fal at libero.it (Francesco Loffredo) Date: Fri, 23 Aug 2013 10:18:27 +0200 Subject: [Tutor] Runestone Python Course In-Reply-To: References: Message-ID: <52171AD3.4000509@libero.it> Omar Abou Mrad wrote: > > > > On Wed, Aug 21, 2013 at 7:52 AM, Jim Mooney > wrote: > > http://interactivepython.org > > > > > Would be nice if it worked though, logged in through my google account, now i get this error which I can do nothing about: > > > Sorry, Something went wrong > > The error is: |invalid request| > |It was the same for me|. It gives this error message as soon as I try to run one of their "ActiveCode" examples. But I discovered that if you delete the single leading blank in front of each line, then it works. Maybe their parser doesn't like unindented lines starting with a space... It's a nuisance, though. Francesco From chris at chrisdown.name Fri Aug 23 12:55:45 2013 From: chris at chrisdown.name (Chris Down) Date: Fri, 23 Aug 2013 12:55:45 +0200 Subject: [Tutor] How much in a "try" block? In-Reply-To: References: <20130822202727.GB922@chrisdown.name> Message-ID: <20130823105544.GA912@chrisdown.name> On 2013-08-23 01:30, Alan Gauld wrote: > Unless you really only want g(x) executed if there is no MyError exception > but want h(x) executed regardless. I've had that situation a few times before when using the logic "try this, or fall back to this if it doesn't work". > I'm curious, how often do others use the try/else combination? Rarely. I think I've only used it twice in recent memory. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From leamhall at gmail.com Fri Aug 23 13:37:12 2013 From: leamhall at gmail.com (leam hall) Date: Fri, 23 Aug 2013 07:37:12 -0400 Subject: [Tutor] How much in a "try" block? In-Reply-To: <20130823105544.GA912@chrisdown.name> References: <20130822202727.GB922@chrisdown.name> <20130823105544.GA912@chrisdown.name> Message-ID: Well, maybe I'm looking at the wrong construct. The script needs to open up one file that will be on the machine. It will open up other files given as command line arguments and open files to write to. It should fail gracefully if it cannot open the files to be read or written. The community that will use the script has varying levels of scripting but not hordes of Python. I'd prefer to fail with an explanation so they could fix the issue and not just blame the script. Leam On Fri, Aug 23, 2013 at 6:55 AM, Chris Down wrote: > On 2013-08-23 01:30, Alan Gauld wrote: > > Unless you really only want g(x) executed if there is no MyError > exception > > but want h(x) executed regardless. > > I've had that situation a few times before when using the logic "try this, > or > fall back to this if it doesn't work". > > > I'm curious, how often do others use the try/else combination? > > Rarely. I think I've only used it twice in recent memory. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From __peter__ at web.de Fri Aug 23 14:14:35 2013 From: __peter__ at web.de (Peter Otten) Date: Fri, 23 Aug 2013 14:14:35 +0200 Subject: [Tutor] How much in a "try" block? References: <20130822202727.GB922@chrisdown.name> Message-ID: Alan Gauld wrote: > On 22/08/13 21:27, Chris Down wrote: > >> You can also use the "else" clause if there is stuff you want to run if >> the try block doesn't raise the caught exception, which avoids putting it >> in "try" if you don't intend to exit from the exception. > > I admit that I've never really found a use for else in a try block. > I don;t see much advantage in > > try: f(x) > except MyError: > pass > else: > g(x) > h(x) > > over > > try: f(x) > except MyError: > pass > g(x) > h(x) > > Unless you really only want g(x) executed if there > is no MyError exception but want h(x) executed regardless. > > I guess where h() is not using x it might be helpful but in most(all?) > of my code I've usually bailed when x has gone > wrong or I've fixed things such that hg() and h() are required. > > I'm curious, how often do others use the try/else combination? I use it for clarity even when it is not necessary. I think try: text = file.read() except AttributeError: with open(file) as f: text = f.read() looks odd (an AttributeError when reading a file?) compared to try: read = file.read except AttributeError: with open(file) as f: text = f.read() else: text = read() Ah -- we're not sure whether it's a file or a filename. Looking through my bunch of casual scripts I find that 22% of try...except have an else clause compared to only 11.4% in /usr/lib/python2.7. From cybervigilante at gmail.com Fri Aug 23 21:02:33 2013 From: cybervigilante at gmail.com (Jim Mooney) Date: Fri, 23 Aug 2013 12:02:33 -0700 Subject: [Tutor] Runestone Python Course In-Reply-To: <52171AD3.4000509@libero.it> References: <52171AD3.4000509@libero.it> Message-ID: But I discovered that if you delete the single leading blank in front of each line, then it works. Maybe their parser doesn't like unindented lines starting with a space... ========= That's odd. I haven't had any of those problems. I wonder if it's your browser. I'm using FF. Are you doing the first course or the more advanced one? I'm only looking at the first one. If you write them I 've found them to be very cooperative, which is the norm for open source stuff if you're polite. imagine trying to get through the Microsoft phalanx to the programmer, if you could even find them, and then they'd say they had no authority to make a change until it was reviewed by the Committee of 400, a year from now ;') Jim On 23 August 2013 01:18, Francesco Loffredo wrote: > Omar Abou Mrad wrote: > >> >> >> >> On Wed, Aug 21, 2013 at 7:52 AM, Jim Mooney > cybervigilante at gmail.**com >> wrote: >> >> http://interactivepython.org >> >> >> >> >> Would be nice if it worked though, logged in through my google account, >> now i get this error which I can do nothing about: >> >> >> Sorry, Something went wrong >> >> The error is: |invalid request| >> >> |It was the same for me|. It gives this error message as soon as I try > to run one of their "ActiveCode" examples. > But I discovered that if you delete the single leading blank in front of > each line, then it works. > Maybe their parser doesn't like unindented lines starting with a space... > > It's a nuisance, though. > > Francesco > ______________________________**_________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor > -- Jim More and more, science is showing that animals, even "simple" ones, have awareness and feelings. There is no hard divide, as the rape-the-earth crowd would have us believe.. -------------- next part -------------- An HTML attachment was scrubbed... URL: From chris at chrisdown.name Sat Aug 24 10:03:28 2013 From: chris at chrisdown.name (Chris Down) Date: Sat, 24 Aug 2013 10:03:28 +0200 Subject: [Tutor] Python 3.x VIM In-Reply-To: References: Message-ID: <20130824080327.GA794@chrisdown.name> Hi Neelesh, On 2013-08-24 12:55, Neelesh Chandola wrote: > On your recommendation , I downloaded VIM but when checking for python 3.x > support , using :py3 print ("Hello") , it gives error E370 and E263. I also > checked :version and it shows that both py2.x and py3.x are in use . > So my question is how do I use it just for py3.x .I use win8 . > Please help - I am completely new so you would have to explain things while > answering . I do not provide any support by 1-to-1 email. If you want support, please e-mail the list (Cc'd to this mail). It sounds like your version of Vim was compiled without Python support, but I haven't used Windows in years, and have no experience in that. As for your question in your followup e-mail, you can find my .vimrc (with all of my other configs) here: https://github.com/cdown/dotfiles Thanks. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From dedur at rumantsch.ch Fri Aug 23 11:34:08 2013 From: dedur at rumantsch.ch (Duri Denoth) Date: Fri, 23 Aug 2013 11:34:08 +0200 Subject: [Tutor] verb conjugations Message-ID: Hello Tutor I have written a program that generates verb conjugations. There is a loop that generates the regular forms: for i in range(6): present[i] = stem + worded_present_ar[i] For irregular verbs the correct form is set individually: present[2] = stem + worded_present_rule_7[2] This dosen't seem to me the best readable Python code. Is there any python language feature more suitable for this problem? Thanks! Duri From __peter__ at web.de Sun Aug 25 10:48:51 2013 From: __peter__ at web.de (Peter Otten) Date: Sun, 25 Aug 2013 10:48:51 +0200 Subject: [Tutor] verb conjugations References: Message-ID: Duri Denoth wrote: > Hello Tutor > > I have written a program that generates verb conjugations. > > There is a loop that generates the regular forms: > > for i in range(6): > present[i] = stem + worded_present_ar[i] An alternative way to write this is present = [stem + suffix for suffix in worded_present_ar] > For irregular verbs the correct form is set individually: > > present[2] = stem + worded_present_rule_7[2] Does this mean that all forms but the 3rd person singular are regular? You could set up a dictionary irregular_forms = { "go": {2: "{}es"}, "be": {0: "am", 1: "are", 2: "is", ..., 5: "are"}, ... } present = ... # preload with regular forms if stem in irregular_forms: for numerus_person, template in irregular_forms[stem].items(): present[numerus_person] = template.format(stem) Use of format() instead of concatenation with '+' allows you to build forms that don't start with the stem. > This dosen't seem to me the best readable Python code. Is there any python > language feature more suitable for this problem? I don't think readability is the issue with your code; rather the problem is to cover all the irregularities of a natural language. From chris at chrisdown.name Mon Aug 26 00:34:40 2013 From: chris at chrisdown.name (Chris Down) Date: Mon, 26 Aug 2013 00:34:40 +0200 Subject: [Tutor] Resetting state of http.client/httplib HTTPSConnection objects Message-ID: <20130825223439.GA12945@chrisdown.name> I am experiencing intermittent issues where an exception will be raised when calling getresponse(), which makes the entire connection stuck in Request-sent state. Is it possible to reset to idle state somehow without reinstantiating the HTTPSConnection? I ideally want to keep the connection to the server, and just reset the state to idle so that I can make another request (like you would usually do by calling getresponse()). >>> a.conn >>> a.conn.getresponse() Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python3.3/http/client.py", line 1143, in getresponse response.begin() File "/usr/lib64/python3.3/http/client.py", line 354, in begin version, status, reason = self._read_status() File "/usr/lib64/python3.3/http/client.py", line 324, in _read_status raise BadStatusLine(line) http.client.BadStatusLine: '' >>> a.conn.request("GET", "foo") Traceback (most recent call last): File "", line 1, in File "/usr/lib64/python3.3/http/client.py", line 1061, in request self._send_request(method, url, body, headers) File "/usr/lib64/python3.3/http/client.py", line 1089, in _send_request self.putrequest(method, url, **skips) File "/usr/lib64/python3.3/http/client.py", line 944, in putrequest raise CannotSendRequest(self.__state) http.client.CannotSendRequest: Request-sent -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From alan.gauld at btinternet.com Mon Aug 26 02:23:52 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 26 Aug 2013 01:23:52 +0100 Subject: [Tutor] Resetting state of http.client/httplib HTTPSConnection objects In-Reply-To: <20130825223439.GA12945@chrisdown.name> References: <20130825223439.GA12945@chrisdown.name> Message-ID: On 25/08/13 23:34, Chris Down wrote: > I am experiencing intermittent issues where an exception will be raised when > calling getresponse(), which makes the entire connection stuck in Request-sent > state. While this is technically within the remit of this list, since its about a standard library module, I suspect you might be better off asking on the main tutor list. It's at a deeper level of skill/experience than most of the tutor queries. But you may well get an answer here, it's just that the main list would certainly consider this to be a valid query too. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From chris at chrisdown.name Mon Aug 26 11:28:47 2013 From: chris at chrisdown.name (Chris Down) Date: Mon, 26 Aug 2013 11:28:47 +0200 Subject: [Tutor] Resetting state of http.client/httplib HTTPSConnection objects In-Reply-To: References: <20130825223439.GA12945@chrisdown.name> Message-ID: <20130826092845.GE800@chrisdown.name> On 2013-08-26 01:23, Alan Gauld wrote: > While this is technically within the remit of this list, since its > about a standard library module, I suspect you might be better > off asking on the main tutor list. It's at a deeper level of > skill/experience than most of the tutor queries. Hm, I guess I don't understand the remit of the two lists then. Tutor has always seemed very... non-tutory. I'll post it there, anyway. Thanks! -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From steve at pearwood.info Mon Aug 26 12:45:43 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 26 Aug 2013 20:45:43 +1000 Subject: [Tutor] Resetting state of http.client/httplib HTTPSConnection objects In-Reply-To: <20130826092845.GE800@chrisdown.name> References: <20130825223439.GA12945@chrisdown.name> <20130826092845.GE800@chrisdown.name> Message-ID: <521B31D7.9060509@pearwood.info> On 26/08/13 19:28, Chris Down wrote: > On 2013-08-26 01:23, Alan Gauld wrote: >> While this is technically within the remit of this list, since its >> about a standard library module, I suspect you might be better >> off asking on the main tutor list. It's at a deeper level of >> skill/experience than most of the tutor queries. > > Hm, I guess I don't understand the remit of the two lists then. Tutor has > always seemed very... non-tutory. I'll post it there, anyway. Thanks! It's not so much that your question is off-topic for this list -- it sort of is, but who cares, we often discuss things which are only tangentially related to "learning Python the language" -- but that the number of people on this list is smaller than the main python-list at python.org list. So for more advanced questions like this, you're more likely to get an answer from someone there, just by weight of numbers. -- Steven From chris at chrisdown.name Mon Aug 26 15:53:00 2013 From: chris at chrisdown.name (Chris Down) Date: Mon, 26 Aug 2013 15:53:00 +0200 Subject: [Tutor] Resetting state of http.client/httplib HTTPSConnection objects In-Reply-To: References: <20130825223439.GA12945@chrisdown.name> <20130826092845.GE800@chrisdown.name> Message-ID: <20130826135259.GC14785@chrisdown.name> On 2013-08-26 09:50, William Ray Wing wrote: > I'd be willing to bet that Alan simply foobar'd his answer - he _meant_ to > say the main python list, not the main tutor list. I guessed that was what he meant. I posted on python-list, anyway. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From alan.gauld at btinternet.com Mon Aug 26 16:30:24 2013 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Mon, 26 Aug 2013 15:30:24 +0100 (BST) Subject: [Tutor] Resetting state of http.client/httplib HTTPSConnection objects In-Reply-To: References: <20130825223439.GA12945@chrisdown.name> <20130826092845.GE800@chrisdown.name> Message-ID: <1377527424.7290.YahooMailNeo@web186004.mail.ir2.yahoo.com> Oops, yes. That's absolutely correct. Sorry about any confusion caused! ? Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ >________________________________ > From: William Ray Wing >To: Chris Down >Cc: William Ray Wing ; Alan Gauld ; tutor at python.org >Sent: Monday, 26 August 2013, 14:50 >Subject: Re: [Tutor] Resetting state of http.client/httplib HTTPSConnection objects > > >On Aug 26, 2013, at 5:28 AM, Chris Down wrote: > >> On 2013-08-26 01:23, Alan Gauld wrote: >>> While this is technically within the remit of this list, since its >>> about a standard library module, I suspect you might be better >>> off asking on the main tutor list. It's at a deeper level of >>> skill/experience than most of the tutor queries. >> >> Hm, I guess I don't understand the remit of the two lists then. Tutor has >> always seemed very... non-tutory. I'll post it there, anyway. Thanks! >> _______________________________________________ >> Tutor maillist? -? Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > >I'd be willing to bet that Alan simply foobar'd his answer - he _meant_ to say the main python list, not the main tutor list. > >-Bill > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From isaaceric at ymail.com Mon Aug 26 10:29:40 2013 From: isaaceric at ymail.com (isaac Eric) Date: Mon, 26 Aug 2013 01:29:40 -0700 (PDT) Subject: [Tutor] i need help with the following question Message-ID: <1377505780.4955.YahooMailNeo@web164005.mail.gq1.yahoo.com> describe different ways of displaying output using python! -------------- next part -------------- An HTML attachment was scrubbed... URL: From dfjennings at gmail.com Mon Aug 26 16:37:06 2013 From: dfjennings at gmail.com (Don Jennings) Date: Mon, 26 Aug 2013 10:37:06 -0400 Subject: [Tutor] i need help with the following question In-Reply-To: <1377505780.4955.YahooMailNeo@web164005.mail.gq1.yahoo.com> References: <1377505780.4955.YahooMailNeo@web164005.mail.gq1.yahoo.com> Message-ID: On Aug 26, 2013, at 4:29 AM, isaac Eric wrote: > describe different ways of displaying output using python! Well, that's not really a question now, is it? I would be happy to help, but which part of the task is confusing for you? Take care, Don From wrw at mac.com Mon Aug 26 15:50:26 2013 From: wrw at mac.com (William Ray Wing) Date: Mon, 26 Aug 2013 09:50:26 -0400 Subject: [Tutor] Resetting state of http.client/httplib HTTPSConnection objects In-Reply-To: <20130826092845.GE800@chrisdown.name> References: <20130825223439.GA12945@chrisdown.name> <20130826092845.GE800@chrisdown.name> Message-ID: On Aug 26, 2013, at 5:28 AM, Chris Down wrote: > On 2013-08-26 01:23, Alan Gauld wrote: >> While this is technically within the remit of this list, since its >> about a standard library module, I suspect you might be better >> off asking on the main tutor list. It's at a deeper level of >> skill/experience than most of the tutor queries. > > Hm, I guess I don't understand the remit of the two lists then. Tutor has > always seemed very... non-tutory. I'll post it there, anyway. Thanks! > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor I'd be willing to bet that Alan simply foobar'd his answer - he _meant_ to say the main python list, not the main tutor list. -Bill From chris at chrisdown.name Mon Aug 26 16:59:11 2013 From: chris at chrisdown.name (Chris Down) Date: Mon, 26 Aug 2013 16:59:11 +0200 Subject: [Tutor] i need help with the following question In-Reply-To: <1377505780.4955.YahooMailNeo@web164005.mail.gq1.yahoo.com> References: <1377505780.4955.YahooMailNeo@web164005.mail.gq1.yahoo.com> Message-ID: <20130826145911.GD14785@chrisdown.name> On 2013-08-26 01:29, isaac Eric wrote: > describe different ways of displaying output using python! Please, read this: http://www.catb.org/esr/faqs/smart-questions.html -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From bgailer at gmail.com Mon Aug 26 17:32:20 2013 From: bgailer at gmail.com (bob gailer) Date: Mon, 26 Aug 2013 11:32:20 -0400 Subject: [Tutor] i need help with the following question In-Reply-To: <1377505780.4955.YahooMailNeo@web164005.mail.gq1.yahoo.com> References: <1377505780.4955.YahooMailNeo@web164005.mail.gq1.yahoo.com> Message-ID: <521B7504.3050709@gmail.com> On 8/26/2013 4:29 AM, isaac Eric wrote: > describe different ways of displaying output using python! I am glad you are asking for help. We encourage that. I agree with the other responses. Your question is clear to you, but not to us. We could play the guessing game and eventually discover what you want - but that is not efficient. Better is for you to read the article on how to ask questions, then re-send your question. -- Bob Gailer 919-636-4239 Chapel Hill NC -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Mon Aug 26 18:32:14 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 26 Aug 2013 17:32:14 +0100 Subject: [Tutor] i need help with the following question In-Reply-To: <1377505780.4955.YahooMailNeo@web164005.mail.gq1.yahoo.com> References: <1377505780.4955.YahooMailNeo@web164005.mail.gq1.yahoo.com> Message-ID: On 26/08/13 09:29, isaac Eric wrote: > describe different ways of displaying output using python! This smells like homework. We don;t do your homework for you although we will try to point you in the right direction. But you need to show that you are at least trying to figure it out for yourself. Also, in general, it helps if you tell us what version of Python you use on which OS and what tutorial, if any, you are following. Then be as specific as possible in your question so that we can give you a specific answer. As it stands I'd say the answer to your question is: Python can output on stdout, to named files, to a GUI or to a network. The output can be in the form of text strings or bytes. Which of those, if any, counts as 'display' is up to you. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From alan.gauld at btinternet.com Mon Aug 26 19:55:02 2013 From: alan.gauld at btinternet.com (ALAN GAULD) Date: Mon, 26 Aug 2013 18:55:02 +0100 (BST) Subject: [Tutor] i need help with the following question In-Reply-To: References: <1377505780.4955.YahooMailNeo@web164005.mail.gq1.yahoo.com>, Message-ID: <1377539702.44258.YahooMailNeo@web186001.mail.ir2.yahoo.com> You need to include the tutor list (use ReplyAll)? if you want to send to the list. I'm not sure I understand what you are after, can? you give a bit more background. What do you mean by? "knowledge base", "object code", "a.i. style of program",? OFC, GUI linking, "xyz matrix code" etc? I could guess,? but I might get it wrong. I assume Houdini is some kind of package or app but? I've never heard of it and don't know what it does. Python is a general purpose programming language and? so can likely do whatever you want it to do. But you? will need to figure out how. It's unlikely to be a? trivial exercise. ? Alan Gauld Author of the Learn To Program website http://www.alan-g.me.uk/ >________________________________ > From: JAMIE shelley >To: Alan Gauld >Sent: Monday, 26 August 2013, 18:47 >Subject: RE: [Tutor] i need help with the following question > > > > >hello, not sure id this is correct method to publish a question to th emailing list but hear goes :) >(for a start let me just add that i'm moving onto electronic electrical extended lvl 3 diploma mainly due to very poor computing teaching quality -among other things- budget cuts :c )? >- So my interest in python is no longer academic, until uni? >but still... :) > >wondering if you could tell me if it's possible to make a program that is effectively just a knowledge base but one that can change the area of it's specialization. > > >To clear it up a bit , coupled with the possibility of a GUI or object code as to allow your avg joe to input said questions in any format and the program can interpret it, not so much using a.i style of program (don't think efficiency of the code will be a problem here) but rather reference imported libraries/lists. > >I've been ?thinking about it for a while and the best I can come up with is just constant iteration , while and import, then OFC for GUI linking some xyz matrix code. - or even run that kinda stuff on programs like Houdini ? > > >If I haven't waffled enough; what I would like to achieve > >simple general purpose code for matching keyword in said locations to serial lists, be able to outomaticly append the list to allow retrieval of most common searches faster > > >linking a program like Houdini to xyz (courtesan) code ? > > > > >any info on this is great greatly appreciated, or even just ideas to get me going? > > >regards >-j.shelley > > >> To: tutor at python.org >> From: alan.gauld at btinternet.com >> Date: Mon, 26 Aug 2013 17:32:14 +0100 >> Subject: Re: [Tutor] i need help with the following question >> >> On 26/08/13 09:29, isaac Eric wrote: >> > describe different ways of displaying output using python! >> >> This smells like homework. >> We don;t do your homework for you although we will try to point >> you in the right direction. But you need to show that you are >> at least trying to figure it out for yourself. >> >> Also, in general, it helps if you tell us what version of Python >> you use on which OS and what tutorial, if any, you are following. >> >> Then be as specific as possible in your question so that we can >> give you a specific answer. >> >> As it stands I'd say the answer to your question is: >> >> Python can output on stdout, to named files, to a GUI or to a network. >> The output can be in the form of text strings or bytes. >> Which of those, if any, counts as 'display' is up to you. >> >> -- >> Alan G >> Author of the Learn to Program web site >> http://www.alan-g.me.uk/ >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From joel.goldstick at gmail.com Mon Aug 26 20:31:04 2013 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 26 Aug 2013 14:31:04 -0400 Subject: [Tutor] i need help with the following question In-Reply-To: <1377539702.44258.YahooMailNeo@web186001.mail.ir2.yahoo.com> References: <1377505780.4955.YahooMailNeo@web164005.mail.gq1.yahoo.com> <1377539702.44258.YahooMailNeo@web186001.mail.ir2.yahoo.com> Message-ID: First of all, ONLY use text mode for sending messages to the mailing list. On Mon, Aug 26, 2013 at 1:55 PM, ALAN GAULD wrote: > You need to include the tutor list (use ReplyAll) > if you want to send to the list. > > I'm not sure I understand what you are after, can > you give a bit more background. What do you mean by > "knowledge base", "object code", "a.i. style of program", > OFC, GUI linking, "xyz matrix code" etc? I could guess, > but I might get it wrong. > > I assume Houdini is some kind of package or app but > I've never heard of it and don't know what it does. > > Python is a general purpose programming language and > so can likely do whatever you want it to do. But you > will need to figure out how. It's unlikely to be a > trivial exercise. > > Alan Gauld > Author of the Learn To Program website > http://www.alan-g.me.uk/ > > ________________________________ > From: JAMIE shelley > To: Alan Gauld > Sent: Monday, 26 August 2013, 18:47 > Subject: RE: [Tutor] i need help with the following question > > hello, not sure id this is correct method to publish a question to th > emailing list but hear goes :) Not sure what that last sentence means. > (for a start let me just add that i'm moving onto electronic electrical > extended lvl 3 diploma mainly due to very poor computing teaching quality > -among other things- budget cuts :c ) For starters, this is a list with many Americans and people from all over the world. I have no idea what extended lvl 3 diploma is, and you lose points by complaining about your teachers. Maybe you don't ask questions very well (a thought!) > - So my interest in python is no longer academic, until uni > but still... :) > > wondering if you could tell me if it's possible to make a program that is > effectively just a knowledge base but one that can change the area of it's > specialization. So, the original question about ways to output from python has nothing to do with your questions now? Maybe you are talking about a general purpose data base. At this point, let me back up and ask if you could describe your general level of programming experience. What language(s)? Do you understand what an algorithm is? A database? > > To clear it up a bit , coupled with the possibility of a GUI or object code > as to allow your avg joe to input said questions in any format and the > program can interpret it, not so much using a.i style of program (don't > think efficiency of the code will be a problem here) but rather reference > imported libraries/lists. Sorry, this last paragraph looks like something made up by a bot to sound intelligent. It reminds me of the Dilbert Corporate philosophy generator. On what basis are we to compare GUI to object code . How does this relate to a.i I want to be positive and helpful, but the more I look at this thread, the less of a question I see. Earlier a responder posted a link describing how to ask a good question on a mailing list. You should read that. > > I've been thinking about it for a while and the best I can come up with is > just constant iteration , while and import, then OFC for GUI linking some > xyz matrix code. - or even run that kinda stuff on programs like Houdini Where will you put the flux capacitor? > > If I haven't waffled enough; what I would like to achieve > > simple general purpose code for matching keyword in said locations to serial > lists, be able to outomaticly append the list to allow retrieval of most > common searches faster > > linking a program like Houdini to xyz (courtesan) code > > > any info on this is great greatly appreciated, or even just ideas to get me > going > > regards > -j.shelley > >> To: tutor at python.org >> From: alan.gauld at btinternet.com >> Date: Mon, 26 Aug 2013 17:32:14 +0100 >> Subject: Re: [Tutor] i need help with the following question > >> >> On 26/08/13 09:29, isaac Eric wrote: >> > describe different ways of displaying output using python! >> >> This smells like homework. >> We don;t do your homework for you although we will try to point >> you in the right direction. But you need to show that you are >> at least trying to figure it out for yourself. >> >> Also, in general, it helps if you tell us what version of Python >> you use on which OS and what tutorial, if any, you are following. >> >> Then be as specific as possible in your question so that we can >> give you a specific answer. >> >> As it stands I'd say the answer to your question is: >> >> Python can output on stdout, to named files, to a GUI or to a network. >> The output can be in the form of text strings or bytes. >> Which of those, if any, counts as 'display' is up to you. >> >> -- >> Alan G >> Author of the Learn to Program web site >> http://www.alan-g.me.uk/ >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > -- Joel Goldstick http://joelgoldstick.com From bgailer at gmail.com Mon Aug 26 21:41:25 2013 From: bgailer at gmail.com (bob gailer) Date: Mon, 26 Aug 2013 15:41:25 -0400 Subject: [Tutor] Comment on http://www.catb.org/esr/faqs/smart-questions.html Message-ID: <521BAF65.3050507@gmail.com> Hi fellow tutors and helpers. I have a reaction to http://www.catb.org/esr/faqs/smart-questions.html and I'd like your feedback. Perhaps I will then send revised comments to the authors of the site. Before reading my comments pretend you are a newbie asking your first question and getting directed to this site. Go to the site and try it get the answer to "how do I ask a good question?" ---- proposed comments ---- I participate in several Python Help lists. We frequently refer newbies to the above site. I decided to take another look at it today. I found myself a bit disappointed. Here' s why: All I see at first is email links and Revision History. If I did not know to scroll down I might have just figured I was at the wrong page. Reading translations and disclaimer is not why I'd come here. I also would not have come here to be potentially labeled an "idiot". There's a lot of words to wade thru to get to the heart of the matter - how to ask good questions. What I'd prefer is a page that starts out with a brief summary of a what a good question. I think many newbies will read that and get the idea. Followed by more detail about "how to ask good questions" Then all the other stuff. With contact names and email at the bottom preceded by a neutrally worded disclaimer. Only those who seek the contact info would need to read that. -- Bob Gailer 919-636-4239 Chapel Hill NC From alan.gauld at btinternet.com Mon Aug 26 23:09:21 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 26 Aug 2013 22:09:21 +0100 Subject: [Tutor] i need help with the following question In-Reply-To: References: <1377505780.4955.YahooMailNeo@web164005.mail.gq1.yahoo.com> <1377539702.44258.YahooMailNeo@web186001.mail.ir2.yahoo.com> Message-ID: On 26/08/13 19:31, Joel Goldstick wrote: ___________ >> From: JAMIE shelley >> To: Alan Gauld >> effectively just a knowledge base but one that can change the area of it's >> specialization. > > So, the original question about ways to output from python has nothing > to do with your questions now? The problem is that Jamie Shelley has used a post from Eric Isaac to post his question so we have two questions on the same thread. >> just constant iteration , while and import, then OFC for GUI linking some >> xyz matrix code. - or even run that kinda stuff on programs like Houdini > > Where will you put the flux capacitor? LOL :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From taserian at gmail.com Mon Aug 26 23:26:37 2013 From: taserian at gmail.com (taserian) Date: Mon, 26 Aug 2013 17:26:37 -0400 Subject: [Tutor] Comparison Textboxes Message-ID: I'm attempting to gather the pieces I need for a simple project I'd like to do for my job, but I'm having a difficult time finding something, and I'm appealing to the hive mind at Tutor for wisdom. My project needs to compare two or more large textboxes (each one containing a file or a large amount of text) and point out their differences, establishing one textbox as the canonical one to which the other textboxes are compared. This functionality is done in many text editors (Notepad++ can install a plug-in that allows comparison between two files), so I would think that the code behind this is pretty common, yet I can't seem to find any reference to it. My searches for "python text comparison code" points to Python IDEs that include that functionality, but no indication as to how to include it into my own code. I think I'm missing something else to search by, but I can't determine what it is. Does anyone have an idea of what I'm talking about, and can you point me in the right direction? Antonio Rodriguez -------------- next part -------------- An HTML attachment was scrubbed... URL: From emile at fenx.com Mon Aug 26 23:39:22 2013 From: emile at fenx.com (Emile van Sebille) Date: Mon, 26 Aug 2013 14:39:22 -0700 Subject: [Tutor] Comment on http://www.catb.org/esr/faqs/smart-questions.html In-Reply-To: <521BAF65.3050507@gmail.com> References: <521BAF65.3050507@gmail.com> Message-ID: I suspect that of the referrals made to this site only one-in-ten actually bother to _read_ the damn thing. And those are the ones that quickly move on to the main python list going forward. (I hope!) And while you are right that it doesn't serve the needs of the ten-second-toms out there that only want an answer to their question, ill stated as it may be, and without effort, which is mostly when I'd refer someone there, perhaps an introductory page hosted on the python site (beginners page perhaps) that we could refer to instead may be called for. In any case, I wouldn't write esr and Rick Moen to request changes -- I expect you'd be pointed to http://www.catb.org/esr/faqs/smart-questions.html#disclaimer if you got a reply at all. Emile On 8/26/2013 12:41 PM, bob gailer wrote: > Hi fellow tutors and helpers. I have a reaction to > http://www.catb.org/esr/faqs/smart-questions.html and I'd like your > feedback. Perhaps I will then send revised comments to the authors of > the site. > > Before reading my comments pretend you are a newbie asking your first > question and getting directed to this site. Go to the site and try it > get the answer to "how do I ask a good question?" > > ---- proposed comments ---- > > I participate in several Python Help lists. We frequently refer newbies > to the above site. I decided to take another look at it today. > > I found myself a bit disappointed. Here' s why: > > All I see at first is email links and Revision History. If I did not > know to scroll down I might have just figured I was at the wrong page. > > Reading translations and disclaimer is not why I'd come here. I also > would not have come here to be potentially labeled an "idiot". > > There's a lot of words to wade thru to get to the heart of the matter - > how to ask good questions. > > What I'd prefer is a page that starts out with a brief summary of a what > a good question. I think many newbies will read that and get the idea. > > Followed by more detail about "how to ask good questions" > > Then all the other stuff. > > With contact names and email at the bottom preceded by a neutrally > worded disclaimer. Only those who seek the contact info would need to > read that. > From emile at fenx.com Mon Aug 26 23:42:43 2013 From: emile at fenx.com (Emile van Sebille) Date: Mon, 26 Aug 2013 14:42:43 -0700 Subject: [Tutor] Comment on http://www.catb.org/esr/faqs/smart-questions.html In-Reply-To: <521BAF65.3050507@gmail.com> References: <521BAF65.3050507@gmail.com> Message-ID: perhaps pointing to http://www.catb.org/esr/faqs/smart-questions.html#intro is a better answer? Emile From bgailer at gmail.com Mon Aug 26 23:40:42 2013 From: bgailer at gmail.com (bob gailer) Date: Mon, 26 Aug 2013 17:40:42 -0400 Subject: [Tutor] Comparison Textboxes In-Reply-To: References: Message-ID: <521BCB5A.4090802@gmail.com> On 8/26/2013 5:26 PM, taserian wrote: > I'm attempting to gather the pieces I need for a simple project I'd > like to do for my job, but I'm having a difficult time finding > something, and I'm appealing to the hive mind at Tutor for wisdom. > > My project needs to compare two or more large textboxes (each one > containing a file or a large amount of text) and point out their > differences, establishing one textbox as the canonical one to which > the other textboxes are compared. This functionality is done in many > text editors (Notepad++ can install a plug-in that allows comparison > between two files), so I would think that the code behind this is > pretty common, yet I can't seem to find any reference to it. My > searches for "python text comparison code" points to Python IDEs that > include that functionality, but no indication as to how to include it > into my own code. I think I'm missing something else to search by, but > I can't determine what it is. > > Does anyone have an idea of what I'm talking about, and can you point > me in the right direction? If you have diff available (e.g. on *nix) write a subprocess to pass the texts to diff and retrieve the results. There is NO need to re-invent the wheel. -- Bob Gailer 919-636-4239 Chapel Hill NC From ljetibo at gmail.com Mon Aug 26 20:20:30 2013 From: ljetibo at gmail.com (=?ISO-8859-2?Q?Dino_Bekte=B9evi=E6?=) Date: Mon, 26 Aug 2013 20:20:30 +0200 Subject: [Tutor] Python execution timer/proficiency testing Message-ID: Hello, I'm interested in learning more about testing a program proficiency and how to measure execution times in seconds. I have a very repetitive functions and methods that work on images with large amount of measuring points each one working with numpy.ndarrays which is really taxing and has to be really repetitive because I either use .fill() or either have to go pix by pix. I don't dare to run my program on a batch of ~9.5million images because I can't assert how long could it last and because of obvious space issues my program edits the information on the images and then overwrites the original data. Should something go awry I'd have to spend a long time cleaning it up. My plan is to test average profficiency on ~100 000 images to see how it fairs and what to do next. So far it takes about 1.5-2sec per image using the "guess_by_eye" method (which isn't long, the first version took 25sec xD, but I will still add couple of functions) but I still get the following warning: Warning (from warnings module): File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 152 warnings.warn(msg, RuntimeWarning) RuntimeWarning: The iteration is not making good progress, as measured by the improvement from the last ten iterations. It doesn't seem to produce any error in my data, but how dangerous is this? thanks! Dino -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Tue Aug 27 01:37:35 2013 From: davea at davea.name (Dave Angel) Date: Mon, 26 Aug 2013 23:37:35 +0000 (UTC) Subject: [Tutor] Comparison Textboxes References: Message-ID: On 26/8/2013 17:26, taserian wrote: >
I'm attempting to gather the pieces I need for a simple project I'd like to do for my job, but I'm having a difficult time finding something, and I'm appealing to the hive mind at Tutor for wisdom.
>
My project needs to compare two or more large textboxes Please post using text email, not html. it's messy, more than doubles most messages, and frequently has bugs in how it gets displayed. First question is what environment this is. What version of Python, and what operating system? Next question is what's the reason for the requirement. Is it an assignment, and must be done by hand, is it something for your own use? Is it an app that will be used by programmers or by people unfamiliar with programming tools. Next question is what will your user be needing the differences for? Will they be cutting and pasting from the difference file into the same or other dialog boxes? Are they interested in the "minimum" set of differences, or is there a bias towards showing largish blocks, even if not every line in the block is different? Next is how it will be presented. Some diff programs colorize the "new" and "deleted" lines, and more or less ignore changes in order. Others produce something that could be applied to the one file to produce the other (eg. a version control system). Finally, is this a requirement for the intellectual stimulation of coding it from scratch? Are you curious about the possible algorithms or do you want something that can be done with minimum effort? For one set of answers, I'd say to shell out to one of the tools that any programmer already has available. For another such set of answers, i'd write code that analyzed the two files and found all lines that appeared exactly once in each file. Each of those pairs of lines are called "islands". Now, for each island, examine the lines immediately preceding and following the present pair, and if they're identical, add them to the island. If in so doing, you encounter another island, join the two. When none of the islands can grow any more, you have one possible mapping of identical lines between files, and the lines not included are differences. You're in a good position to process those blocks of differences using the islands for context. -- DaveA From davea at davea.name Tue Aug 27 02:04:19 2013 From: davea at davea.name (Dave Angel) Date: Tue, 27 Aug 2013 00:04:19 +0000 (UTC) Subject: [Tutor] Python execution timer/proficiency testing References: Message-ID: On 26/8/2013 14:20, Dino Bekte?evi? wrote: Please post using text email, not html. All the extra junk is a waste of space, and really slows down reading. There are other problems caused sometimes, but you haven't hit those yet. > >
Hello,

I'm interested in learning more about testing a program proficiency and how to measure execution times in seconds. I think you mean efficiency, not proficiency. > I have a very repetitive functions and methods that work on images > with large amount of measuring points each one working with numpy.ndarrays which is really taxing and has to be really repetitive because I either use .fill() or either have to go pix by pix.
I think you're saying it's slow, but you're not sure how slow. >
I don't dare to run my program on a batch of ~9.5million images because I can't assert how long could it last and because of obvious space issues my program edits the information on > the images and then overwrites the original data. Should something go > awry I'd have to spend a long time cleaning it up. Doesn't matter how slow or fast a program is. If it's not reliable, you'd better not run it with in-place updating of valuable data. And if it is reliable, so you trust it, but too slow to run in one pass, then you'd better arrange that each file is recognizably done or not done. For example, you might drop a marker into a directory, make a copy of that directory, work on the copy, then only when the whole directory is finished do you move the files back where they belong and remove the marker. Pick your method such that it would only take a few seconds for the program to figure out where it had left off. > My plan is to test average profficiency on ~100 000 images to see how > it fairs and what to do next.
>
So far it takes about 1.5-2sec per image using the "guess_by_eye" method (which isn't long, If we assume 1 second per file, you're talking 3 years of execution time for 10 million files. Assuming you can wait that long, you'll also have to figure that the program/os/computer will crash a few times in that span. So restartability is mandatory. is anything else going to be using these files, or this computer, in the meantime? How big are these files, in total? It may be much more practical to have a separate drive(s) to hold the results. > the first version took 25sec xD, but I will still add couple of > functions) but I still get the following warning:
>
Warning (from warnings module):
? File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line 152
??? warnings.warn(msg, RuntimeWarning)
> RuntimeWarning: The iteration is not making good progress, as measured by the
? improvement from the last ten iterations.


It doesn't seem to produce any error in my data, but how dangerous is this?
>

thanks!
Dino
That sounds like a question specific to scipy. If I had to guess what it means, I'd say that the processing time per file varies too much to show a meaningful progress bar. Perhaps that last question should be made on a scipy forum, or at least on the main python-list. This tutor list is intended for questions on the language itself. No harm in asking here, but you reduce the odds that somebody has actually used scipy enough to know that message. You can do your own timings, if you like, using time.time() or other mechanisms. Which one is more accurate depends on which OS you're running. But if you make measurements of say 100 files, then any methods is accurate enough. -- DaveA From malaclypse2 at gmail.com Tue Aug 27 03:37:26 2013 From: malaclypse2 at gmail.com (Jerry Hill) Date: Mon, 26 Aug 2013 21:37:26 -0400 Subject: [Tutor] Comparison Textboxes In-Reply-To: References: Message-ID: On Mon, Aug 26, 2013 at 5:26 PM, taserian wrote: > Does anyone have an idea of what I'm talking about, and can you point me in > the right direction? Historically, the unix tool for comparing the differences between two files is called 'diff'. Python has similar functionality in a library called 'difflib'. See http://docs.python.org/3/library/difflib.html The Wikipedia article may provide some useful search terms too: http://en.wikipedia.org/wiki/Diff Does that help any? -- Jerry From steve at pearwood.info Tue Aug 27 04:41:45 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 27 Aug 2013 12:41:45 +1000 Subject: [Tutor] Comment on http://www.catb.org/esr/faqs/smart-questions.html In-Reply-To: <521BAF65.3050507@gmail.com> References: <521BAF65.3050507@gmail.com> Message-ID: <521C11E9.5070009@pearwood.info> On 27/08/13 05:41, bob gailer wrote: > Hi fellow tutors and helpers. I have a reaction to http://www.catb.org/esr/faqs/smart-questions.html and I'd like your feedback. Perhaps I will then send revised comments to the authors of the site. I think you are right. Eric S Raymond is a very smart, very opinionated computer geek, and even when he is trying to be "non-geek friendly" he still writes like a geek for a geek audience. That's one of the reasons I now very rarely link to Smart-Questions, instead I prefer "Short, self contained, correct example". http://sscce.org/ But really, I want to re-write them both for a Python audience. One day, when I get a round tuit. Feel free to pass your feedback on to ESR, but I expect he has probably heard it all before and doesn't care. But I might be wrong. In my feedback below, I'm going to attempt to channel ESR, based on what I know of the man from his reputation and writings. I emphasis that although I am using quotation marks, I'm not actually quoting ESR, I'm putting words into his mouth. > ---- proposed comments ---- > > I participate in several Python Help lists. We frequently refer newbies to the above site. I decided to take another look at it today. > > I found myself a bit disappointed. Here' s why: > > All I see at first is email links and Revision History. If I did not know to scroll down I might have just figured I was at the wrong page. "If you don't know how to scroll down, I can't help you. There are all sorts of people and places that will teach you the basics of using a web browser, I am not one of them." "If you are too lazy to read the entire page, you're too lazy to follow whatever instructions we give you, so stop wasting our time." > Reading translations and disclaimer is not why I'd come here. I also would not have come here to be potentially labeled an "idiot". "Then you are exactly the sort of person we don't want wasting our time asking dumb questions. Sorry, but harsh truths are still truths. If you cannot follow my simple recipe for asking smart questions, then we don't want you wasting our time and filing our communication channels with your dumb questions. Our mailing lists and forums are for the use of people wanting to get things done, not for the purpose of hand-holding the ignorant, the stupid and the lazy." > There's a lot of words to wade thru Writing SMS-speak is not a good way to get ESR to respect your views. (Or mine, for that matter.) >to get to the heart of the matter - how to ask good questions. > > What I'd prefer is a page that starts out with a brief summary of a what a good question. I think many newbies will read that and get the idea. "It's a free internet, and here in the Glorious USA our forefathers spilled their blood, and that of tyrants, so that you have the inalienable right to Freedom of Speech. And guns. Especially guns. Go ahead and write your own How To Ask Smart Questions the Bob Gailer Way page, and so long as we protect our rights with guns the free market of ideas will choose between your version and mine." -- Steven From chris at chrisdown.name Tue Aug 27 04:48:53 2013 From: chris at chrisdown.name (Chris Down) Date: Tue, 27 Aug 2013 04:48:53 +0200 Subject: [Tutor] Comment on http://www.catb.org/esr/faqs/smart-questions.html In-Reply-To: <521C11E9.5070009@pearwood.info> References: <521BAF65.3050507@gmail.com> <521C11E9.5070009@pearwood.info> Message-ID: <20130827024852.GD2583@chrisdown.name> On 2013-08-27 12:41, Steven D'Aprano wrote: > http://sscce.org/ "Keep the width of the lines in your example to under 62 characters wide." I don't really see any reason to use less than 79 in 2013. In my opinion(!), this document is too opinionated to be useful as a generic guide (but it mostly lines up with my own prejudices). > "If you don't know how to scroll down, I can't help you. There are all sorts > of people and places that will teach you the basics of using a web browser, I > am not one of them." I have mixed feelings about this. I think this is a poor excuse for poor UX, really. > "If you are too lazy to read the entire page, you're too lazy to follow > whatever instructions we give you, so stop wasting our time." I agree with this one. > >There's a lot of words to wade thru > > Writing SMS-speak is not a good way to get ESR to respect your views. (Or > mine, for that matter.) Why on earth do people give a crap about "thru"? It's dictionary approved and peaked in 1930(?) or something, an era when I'm sure if you said they were using "SMS speak", they would have laughed in your face. I only give a shit about how someone represents English when it obscures meaning (which I have a hard time believing "thru" would do). -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From steve at pearwood.info Tue Aug 27 07:33:12 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 27 Aug 2013 15:33:12 +1000 Subject: [Tutor] Comparison Textboxes In-Reply-To: References: Message-ID: <20130827053312.GB4387@ando> Hi Taserian, On Mon, Aug 26, 2013 at 05:26:37PM -0400, taserian wrote: > I'm attempting to gather the pieces I need for a simple project I'd like to > do for my job, but I'm having a difficult time finding something, and I'm > appealing to the hive mind at Tutor for wisdom. Just for the record, "hive mind" is not a compliment, it is an insult. Rather than suggest "the wisdom of the crowd", it suggests a group of people indoctrinated into all believing exactly the same thing. > My project needs to compare two or more large textboxes (each one > containing a file or a large amount of text) and point out their > differences, establishing one textbox as the canonical one to which the > other textboxes are compared. You want the difflib module: http://docs.python.org/2/library/difflib.html -- Steven From steve at pearwood.info Tue Aug 27 08:18:11 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 27 Aug 2013 16:18:11 +1000 Subject: [Tutor] Python execution timer/proficiency testing In-Reply-To: References: Message-ID: <20130827061811.GC4387@ando> On Mon, Aug 26, 2013 at 08:20:30PM +0200, Dino Bekte?evi? wrote: > Hello, > > I'm interested in learning more about testing a program proficiency and how > to measure execution times in seconds. I have a very repetitive functions For the record, "proficiency" means skill, and is used when talking about people. You and I can have proficiency with Python. A Python program doesn't have proficiency. It has efficiency, how fast it operates, how much memory it requires. For timing very small, *fast* code snippets, the best tool is the timeit module: http://docs.python.org/2/library/timeit.html For timing long-running pieces of code, I use this simple recipe: http://code.activestate.com/recipes/577896-benchmark-code-with-the-with-statement/ To find out where a function is spending most of its time, you want a profiler, such as profile and hotshot: http://docs.python.org/2/library/debug.html > Warning (from warnings module): > File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line > 152 > warnings.warn(msg, RuntimeWarning) > RuntimeWarning: The iteration is not making good progress, as measured by > the improvement from the last ten iterations. > > It doesn't seem to produce any error in my data, but how dangerous is this? That's just telling you that your code is slow. I say "your code", but it might be scipy, numpy, or code you wrote yourself. Or it might simply be that the task you are trying to do is hard, and no matter what you do it will always be slow. I'm afraid that it will probably take a numpy/scipy expert to tell you which is the case. -- Steven From alan.gauld at btinternet.com Tue Aug 27 10:49:46 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 27 Aug 2013 09:49:46 +0100 Subject: [Tutor] Comment on http://www.catb.org/esr/faqs/smart-questions.html In-Reply-To: <20130827024852.GD2583@chrisdown.name> References: <521BAF65.3050507@gmail.com> <521C11E9.5070009@pearwood.info> <20130827024852.GD2583@chrisdown.name> Message-ID: On 27/08/13 03:48, Chris Down wrote: > "Keep the width of the lines in your example to under 62 characters wide." > > I don't really see any reason to use less than 79 in 2013. The reason for preferring shorter lines is to leave room for the chevrons when the message gets quoted multiple times. eg >>>>>> I don't really see any reason to use less than 79 in 2013. >>>>> The reason for preferring shorter lines is to leave room for >>>>> the chevrons when the message gets quoted multiple times. >>>> That's just crazy >>> Maybe but its the reason given >> I prefer using different colours the way Outlook does it > But that needs rich text and I hate rich text And that's a whole new discussion ;-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From chris at chrisdown.name Tue Aug 27 11:51:04 2013 From: chris at chrisdown.name (Chris Down) Date: Tue, 27 Aug 2013 11:51:04 +0200 Subject: [Tutor] Comment on http://www.catb.org/esr/faqs/smart-questions.html In-Reply-To: References: <521BAF65.3050507@gmail.com> <521C11E9.5070009@pearwood.info> <20130827024852.GD2583@chrisdown.name> Message-ID: <20130827095103.GB872@chrisdown.name> On 2013-08-27 09:49, Alan Gauld wrote: > The reason for preferring shorter lines is to leave room for > the chevrons when the message gets quoted multiple times. I always reformat quotes with `gq' in vim when I am quoting, I suggest others do the same. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From steve at pearwood.info Tue Aug 27 12:11:03 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 27 Aug 2013 20:11:03 +1000 Subject: [Tutor] Comment on http://www.catb.org/esr/faqs/smart-questions.html In-Reply-To: References: <521BAF65.3050507@gmail.com> <521C11E9.5070009@pearwood.info> <20130827024852.GD2583@chrisdown.name> Message-ID: <20130827101103.GD4387@ando> On Tue, Aug 27, 2013 at 09:49:46AM +0100, Alan Gauld wrote: > >>>>>> I don't really see any reason to use less than 79 in 2013. > >>>>> The reason for preferring shorter lines is to leave room for > >>>>> the chevrons when the message gets quoted multiple times. > >>>> That's just crazy > >>> Maybe but its the reason given > >> I prefer using different colours the way Outlook does it > > But that needs rich text and I hate rich text And it also makes it hard to impossible for the colour blind to tell what is quoted how many times. I wish mail clients would support rich text rather than HTML. There actually is a standard for rich text which does not have the disadvantages of HTML mail (bloat, security implications, vulnerable to malware): http://tools.ietf.org/html/rfc1896 but of course any form of rich text is vulerable to people with rubbish taste screaming in ALL CAPS BOLD ITALIC RED TEXT WITH GREEN UNDERLINED SPACES BETWEEN WORDS... :-) Since this is a Python list, I should also mention ReST (ReStructured Text), which is the Python standard for generating rich text from plain text markup (not to be confused with Markdown, which is another competing, but not quite as powerful, standard). ReST has the advantage that it is human readable. For example: This is a header ---------------- This paragraph contains *italic* and **bold** text. - These are bullet points. - No kidding. - Told you it was readable. I wish mail clients would support rich text using ReST or Markdown. The mail client could still include a GUI so you choose formatting commands rather than have to type markup. -- Steven From chris at chrisdown.name Tue Aug 27 12:16:27 2013 From: chris at chrisdown.name (Chris Down) Date: Tue, 27 Aug 2013 12:16:27 +0200 Subject: [Tutor] Comment on http://www.catb.org/esr/faqs/smart-questions.html In-Reply-To: <20130827101103.GD4387@ando> References: <521BAF65.3050507@gmail.com> <521C11E9.5070009@pearwood.info> <20130827024852.GD2583@chrisdown.name> <20130827101103.GD4387@ando> Message-ID: <20130827101627.GH872@chrisdown.name> On 2013-08-27 20:11, Steven D'Aprano wrote: > I wish mail clients would support rich text using ReST or Markdown. The > mail client could still include a GUI so you choose formatting commands > rather than have to type markup. Why can't you do this through your mailcap instead of relying on the client to implement that functionality? Works well enough for me. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From eryksun at gmail.com Tue Aug 27 12:03:43 2013 From: eryksun at gmail.com (eryksun) Date: Tue, 27 Aug 2013 06:03:43 -0400 Subject: [Tutor] Python execution timer/proficiency testing In-Reply-To: <20130827061811.GC4387@ando> References: <20130827061811.GC4387@ando> Message-ID: On Tue, Aug 27, 2013 at 2:18 AM, Steven D'Aprano wrote: > On Mon, Aug 26, 2013 at 08:20:30PM +0200, Dino Bekte?evi? wrote: > >> Warning (from warnings module): >> File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line >> 152 >> warnings.warn(msg, RuntimeWarning) >> RuntimeWarning: The iteration is not making good progress, as measured by >> the improvement from the last ten iterations. >> >> It doesn't seem to produce any error in my data, but how dangerous is this? > > That's just telling you that your code is slow. > > I say "your code", but it might be scipy, numpy, or code you wrote > yourself. Or it might simply be that the task you are trying to do is > hard, and no matter what you do it will always be slow. I'm afraid that > it will probably take a numpy/scipy expert to tell you which is the > case. The MINPACK routine called by fsolve() failed to converge; it quit after making little or no progress over 10 consecutive iterations. Maybe you need a better initial estimate; maybe there's no solution. From oscar.j.benjamin at gmail.com Tue Aug 27 12:37:37 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 27 Aug 2013 11:37:37 +0100 Subject: [Tutor] Python execution timer/proficiency testing In-Reply-To: References: <20130827061811.GC4387@ando> Message-ID: On 27 August 2013 11:03, eryksun wrote: > On Tue, Aug 27, 2013 at 2:18 AM, Steven D'Aprano wrote: >> On Mon, Aug 26, 2013 at 08:20:30PM +0200, Dino Bekte?evi? wrote: >> >>> Warning (from warnings module): >>> File "/usr/lib/python2.7/dist-packages/scipy/optimize/minpack.py", line >>> 152 >>> warnings.warn(msg, RuntimeWarning) >>> RuntimeWarning: The iteration is not making good progress, as measured by >>> the improvement from the last ten iterations. >>> >>> It doesn't seem to produce any error in my data, but how dangerous is this? >> >> That's just telling you that your code is slow. >> >> I say "your code", but it might be scipy, numpy, or code you wrote >> yourself. Or it might simply be that the task you are trying to do is >> hard, and no matter what you do it will always be slow. I'm afraid that >> it will probably take a numpy/scipy expert to tell you which is the >> case. > > The MINPACK routine called by fsolve() failed to converge; it quit > after making little or no progress over 10 consecutive iterations. > Maybe you need a better initial estimate; maybe there's no solution. Exactly. Dino, whatever scipy routine you're using is warning you that it has failed. You should heed this warning since it likely means that your code is not doing what you want it to do. Without knowing what you're trying to do and what function you're calling I can't say more than that. Oscar From dfjennings at gmail.com Tue Aug 27 13:20:24 2013 From: dfjennings at gmail.com (Don Jennings) Date: Tue, 27 Aug 2013 07:20:24 -0400 Subject: [Tutor] i need help with the following question In-Reply-To: <1377589200.13011.YahooMailNeo@web164002.mail.gq1.yahoo.com> References: <1377505780.4955.YahooMailNeo@web164005.mail.gq1.yahoo.com> <1377589200.13011.YahooMailNeo@web164002.mail.gq1.yahoo.com> Message-ID: <978143AA-5E67-4CB0-A340-16D643A1BAEF@gmail.com> On Aug 27, 2013, at 3:40 AM, isaac Eric wrote: > print "For a circle of radius %s the area is %s" % (radius,area) > Question: What is the purpose of %s ? Okay, so you're just getting started with python. We're happy to do some hand-holding, but we encourage you to think first. You've followed the rest of the instructions and run the program? Don't worry about being precise with your answer to this question. Just try to express what you think is happening (we'll build on what you give us to make it clearer). Take care, Don From fomcl at yahoo.com Tue Aug 27 15:14:33 2013 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Tue, 27 Aug 2013 06:14:33 -0700 (PDT) Subject: [Tutor] spss.BasePivotTable Message-ID: <1377609273.61990.YahooMailNeo@web163805.mail.gq1.yahoo.com> Hello, ? I am trying to create a BasePivot table with three columns: one for the labels, one for a a category "Before" and one for "After". The following attempt fails, but what am I doing wrong? ? begin program. import spss try: ??? spss.StartSPSS() ??? #spss.Submit("get file='demo.sav'.") ??? spss.StartProcedure("proc") ??? table = spss.BasePivotTable("table","mytable") ??? table.Append(spss.Dimension.Place.row,"rowdim") ??? table.Append(spss.Dimension.Place.column,"coldim1") ??? table.Append(spss.Dimension.Place.column,"coldim2") ??? value1 = spss.CellText.Number(23,spss.FormatSpec.Count) ??? value2 = spss.CellText.Number(24,spss.FormatSpec.Count) ??? table[(spss.CellText.String("M"),)] = (value1, value2) ??? spss.EndProcedure() ??? spss.StopSPSS() except spss.SpssError: ??? print "Error." print spss.GetLastErrorMessage() end program. ? Thank you in advance! Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? From fomcl at yahoo.com Tue Aug 27 15:31:58 2013 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Tue, 27 Aug 2013 06:31:58 -0700 (PDT) Subject: [Tutor] spss.BasePivotTable In-Reply-To: <1377609273.61990.YahooMailNeo@web163805.mail.gq1.yahoo.com> References: <1377609273.61990.YahooMailNeo@web163805.mail.gq1.yahoo.com> Message-ID: <1377610318.97133.YahooMailNeo@web163806.mail.gq1.yahoo.com> Ooops, sorry.... wrong mailing list! ;-) Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? ----- Original Message ----- > From: Albert-Jan Roskam > To: Python Mailing List > Cc: > Sent: Tuesday, August 27, 2013 3:14 PM > Subject: [Tutor] spss.BasePivotTable > > Hello, > ? > I am trying to create a BasePivot table with three columns: one for the labels, > one for a a category "Before" and one for "After". The > following attempt fails, but what am I doing wrong? > ? > begin program. > import spss > try: > ??? spss.StartSPSS() > ??? #spss.Submit("get file='demo.sav'.") > ??? spss.StartProcedure("proc") > ??? table = spss.BasePivotTable("table","mytable") > ??? table.Append(spss.Dimension.Place.row,"rowdim") > ??? table.Append(spss.Dimension.Place.column,"coldim1") > ??? table.Append(spss.Dimension.Place.column,"coldim2") > ??? value1 = spss.CellText.Number(23,spss.FormatSpec.Count) > ??? value2 = spss.CellText.Number(24,spss.FormatSpec.Count) > ??? table[(spss.CellText.String("M"),)] = (value1, value2) > ??? spss.EndProcedure() > ??? spss.StopSPSS() > except spss.SpssError: > ??? print "Error." > print spss.GetLastErrorMessage() > end program. > ? > Thank you in advance! > > Regards, > Albert-Jan > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > All right, but apart from the sanitation, the medicine, education, wine, public > order, irrigation, roads, a > fresh water system, and public health, what have the Romans ever done for us? > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~? > _______________________________________________ > Tutor maillist? -? Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > From leamhall at gmail.com Tue Aug 27 19:58:14 2013 From: leamhall at gmail.com (leam hall) Date: Tue, 27 Aug 2013 13:58:14 -0400 Subject: [Tutor] Global var not defined? Message-ID: Could use some help with this. Python 2.4.3 on RHEL 5.x. In the functions file that gets imported: def append_customer(line_list): global customers cust = line_list[0] // list with Customer info in [0] cust = clean_word(cust) // Trims white space if len(cust) and cust not in customers: host_list[cust] = {} customers.append(cust) In the calling file: import functions import sys customers = [] . . . for line in input_file: line = line.strip() if not len(line): continue line_list = line.split(',') functions.append_customer(line_list) Error message: Traceback (most recent call last): File "./host_tools.py", line 55, in ? functions.append_customer(line_list) File "/home/lhall/lang/functions.py", line 27, in append_customer if len(cust) and cust not in customers: NameError: global name 'customers' is not defined -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Tue Aug 27 20:34:23 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 27 Aug 2013 19:34:23 +0100 Subject: [Tutor] Global var not defined? In-Reply-To: References: Message-ID: On 27/08/13 18:58, leam hall wrote: > def append_customer(line_list): > global customers > cust = line_list[0] // list with Customer info in [0] > cust = clean_word(cust) // Trims white space > > if len(cust) and cust not in customers: > host_list[cust] = {} > customers.append(cust) In Python global only applies to the local file. Thus customers needs to be at the global level in functions.py. But since its a bad use of globals it would be better to just get the function to return the value and do the append in the top level file... BTW the line > host_list[cust] = {} Makes no sense since you are adding an empty dictionary to a variable that doesn't exist so you should get an error. I assume you have simplified the code somewhat? > In the calling file: > > import functions > import sys > > customers = [] > . > . > for line in input_file: > line = line.strip() > if not len(line): > continue > line_list = line.split(',') > functions.append_customer(line_list) customers.append(functions.get_customer(line_list, customers)) Avoids any need for globals. Personally I'd move the line split into the function so your loop looks like: for line in input_file: line = line.strip() if line: customers.append(functions.get_customer(line_list, customers)) Alternatively I'd put the customers list into a module called customer and move the customer functions into that module. And then it looks like I might have a Customer class emerging... And maybe the code above would become a class method: Customer.readFromFile(aFile) -> [cust1, cust2, ...] Just a thought... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From ramit.prasad at jpmorgan.com.dmarc.invalid Tue Aug 27 20:34:54 2013 From: ramit.prasad at jpmorgan.com.dmarc.invalid (Prasad, Ramit) Date: Tue, 27 Aug 2013 18:34:54 +0000 Subject: [Tutor] Global var not defined? In-Reply-To: References: Message-ID: <5B80DD153D7D744689F57F4FB69AF4741868E132@SCACMX008.exchad.jpmchase.net> leam hall wrote: > Could use some help with this. Python 2.4.3 on RHEL 5.x. > > In the functions file that gets imported: > > def append_customer(line_list): > ??????? global customers > ??????? cust = line_list[0]???? // list with Customer info in [0] > ??????? cust = clean_word(cust)? // Trims white space > > ??????? if len(cust) and cust not in customers: > ??????????????? host_list[cust] = {} > ??????????????? customers.append(cust) > > In the calling file: > > > import functions > import sys > > customers = [] > > . > . > . > for line in input_file: > ??? line = line.strip() > ??? if not len(line): > ??????? continue > ??? line_list = line.split(',') > ??? functions.append_customer(line_list) > > Error message: > Traceback (most recent call last): > ? File "./host_tools.py", line 55, in ? > ??? functions.append_customer(line_list) > ? File "/home/lhall/lang/functions.py", line 27, in append_customer > ??? if len(cust) and cust not in customers: > NameError: global name 'customers' is not defined > > The problem is because "customers" needs to be defined in the module with the append_customers. Global as written refers to module level variables. Some (possible and untested) methods to get around this are: 1. pass in customers as an argument 2. use globals()? 3. add it to functions module `functions.customers = customers`. ~Ramit This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. From leamhall at gmail.com Tue Aug 27 20:50:52 2013 From: leamhall at gmail.com (leam hall) Date: Tue, 27 Aug 2013 14:50:52 -0400 Subject: [Tutor] Global var not defined? In-Reply-To: <5B80DD153D7D744689F57F4FB69AF4741868E132@SCACMX008.exchad.jpmchase.net> References: <5B80DD153D7D744689F57F4FB69AF4741868E132@SCACMX008.exchad.jpmchase.net> Message-ID: Well, I'm happy to change things but my python is only so good. And much of that is based off of shell programming. What the data looks like is fairly simple. I have a spreadsheet of host information. Customer 'Alan' may have a dozen or so servers and customer Ramit has another dozen or two. When I print these out they will be sorted by customer but rolled into a single file. The line "host_list[cust] = {}" creates the customer dictionary if that customer doesn't exist. Then there's a host key with multiple layers: host_list['alan']['webserver']['ip'] = '12.23.34.45' host_list['alan']['webserver']['environ'] = 'Dev' Make sense? As I do not know a lot about classes I'm not sure they are better in this case than a multi-level dictionary. The data does not get altered, just organized. Leam On Tue, Aug 27, 2013 at 2:34 PM, Prasad, Ramit wrote: > leam hall wrote: > > Could use some help with this. Python 2.4.3 on RHEL 5.x. > > > > In the functions file that gets imported: > > > > def append_customer(line_list): > > global customers > > cust = line_list[0] // list with Customer info in [0] > > cust = clean_word(cust) // Trims white space > > > > if len(cust) and cust not in customers: > > host_list[cust] = {} > > customers.append(cust) > > > > In the calling file: > > > > > > import functions > > import sys > > > > customers = [] > > > > . > > . > > . > > for line in input_file: > > line = line.strip() > > if not len(line): > > continue > > line_list = line.split(',') > > functions.append_customer(line_list) > > > > Error message: > > Traceback (most recent call last): > > File "./host_tools.py", line 55, in ? > > functions.append_customer(line_list) > > File "/home/lhall/lang/functions.py", line 27, in append_customer > > if len(cust) and cust not in customers: > > NameError: global name 'customers' is not defined > > > > > > The problem is because "customers" needs to be defined > in the module with the append_customers. Global as > written refers to module level variables. > > Some (possible and untested) methods to get around this are: > 1. pass in customers as an argument > 2. use globals()? > 3. add it to functions module `functions.customers = customers`. > > > ~Ramit > > > > This email is confidential and subject to important disclaimers and > conditions including on offers for the purchase or sale of securities, > accuracy and completeness of information, viruses, confidentiality, legal > privilege, and legal entity disclaimers, available at > http://www.jpmorgan.com/pages/disclosures/email. > -- Mind on a Mission -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Wed Aug 28 00:03:37 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 27 Aug 2013 23:03:37 +0100 Subject: [Tutor] Global var not defined? In-Reply-To: References: <5B80DD153D7D744689F57F4FB69AF4741868E132@SCACMX008.exchad.jpmchase.net> Message-ID: On 27/08/13 19:50, leam hall wrote: > Well, I'm happy to change things but my python is only so good. And much > of that is based off of shell programming. > You will need to change something because what you have won;t work. The question is what to change? > What the data looks like is fairly simple. I have a spreadsheet of host > information. Customer 'Alan' may have a dozen or so servers and customer > Ramit has another dozen or two. So abstracting that you have several customer objects each containing a list(or dict?) of servers. Servers in turn have numerous attributes: name, role, ip, environ etc... > When I print these out they will be > sorted by customer but rolled into a single file. So the customer objects have a method that prints to a file. That's the OOP approach, but you can do it without classes if you want, its just a bit more effort on the readability and coding front. > The line "host_list[cust] = {}" creates the customer dictionary if that > customer doesn't exist. It may be better to look at the get() method of dictionaries for that. But the problem I highlighted was that the top level host_list dictionary didn't exist in your code. You need to initialize it before you can access the cust key. host_list = {} . . . host_list.get(cust, {}) > Then there's a host key with multiple layers: > > host_list['alan']['webserver']['ip'] = '12.23.34.45' > host_list['alan']['webserver']['environ'] = 'Dev' > > Make sense? As I do not know a lot about classes I'm not sure they are > better in this case than a multi-level dictionary. A class is effectively a dictionary inside so its very similar. Using my OOP suggestion above this would translate to something like: alan.servers['webserver'].ip = '12.23.34.45 Which you find more readable is a matter of taste! There are some other advantages to the OOP approach but they are not critical here. > The data does not get altered, just organized. That doesn't make much difference in this case. The simplest solution to get it working is probably just to move the customers list into the functions module. In the longer term the other options might prove more beneficial. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From ljetibo at gmail.com Tue Aug 27 16:05:19 2013 From: ljetibo at gmail.com (=?ISO-8859-2?Q?Dino_Bekte=B9evi=E6?=) Date: Tue, 27 Aug 2013 16:05:19 +0200 Subject: [Tutor] =?iso-8859-2?q?Python_execution_timer/proficiency_testing?= =?iso-8859-2?q?_=28Dino_Bekte=B9evi=E6=29?= Message-ID: Hello, First off thank you for the good responses. That's most likely why I couldn't find it with google, English is not my native language so the little difference between proficiency and efficiency escaped me. I apologize for HTML text first time I sent something from gmail there seemed to be no issues. 2013/8/27 : > Message: 5 > Date: Tue, 27 Aug 2013 00:04:19 +0000 (UTC) > From: Dave Angel > To: tutor at python.org > Subject: Re: [Tutor] Python execution timer/proficiency testing > Message-ID: > Content-Type: text/plain; charset=ISO-8859-2 > >>
I don't dare to run my program on a batch of ~9.5million images because I can't assert how long could it last and because of obvious space issues my program edits the information on >> the images and then overwrites the original data. Should something go >> awry I'd have to spend a long time cleaning it up. > > Doesn't matter how slow or fast a program is. If it's not reliable, > you'd better not run it with in-place updating of valuable data. And if > it is reliable, so you trust it, but too slow to run in one pass, then > you'd better arrange that each file is recognizably done or not done. > For example, you might drop a marker into a directory, make a copy of > that directory, work on the copy, then only when the whole directory is > finished do you move the files back where they belong and remove the > marker. Pick your method such that it would only take a few seconds for > the program to figure out where it had left off. > >> My plan is to test average profficiency on ~100 000 images to see how >> it fairs and what to do next.
>>
So far it takes about 1.5-2sec per image using the "guess_by_eye" method (which isn't long, > > If we assume 1 second per file, you're talking 3 years of execution > time for 10 million files. Assuming you can wait that long, you'll also > have to figure that the program/os/computer will crash a few times in > that span. So restartability is mandatory. is anything else going to > be using these files, or this computer, in the meantime? > > How big are these files, in total? It may be much more practical to > have a separate drive(s) to hold the results. > The entire database is ~60TB large so you don't have to worry that I will try to run the entire thing at once on one computer. They will be split up in smaller sections called 'runs' couple of runs will be processed on a single comp and multiple comps will be used. Number of files per run varies and I will most likely not try to change that. The computers will not be used for anything else in the meantime. I'm still waiting to hear from my mentor if the server will be available. They are 'FITS' files and also vary from 12-16MB taken from Sloan Digital Sky Survey (SDSS) db. Because of the organisation of FITS files I need the information in 'headers' to further process the image itself, by overwriting the original data I save space and time it takes me to copy paste the files. However you are right I will mostly likely add a 'FLAG' entry into the header so I can restart should something happen. > Date: Tue, 27 Aug 2013 00:04:19 +0000 (UTC) > From: eryksun > To: tutor at python.org, Dino Bekte?evi? > Subject: Re: [Tutor] Python execution timer/proficiency testing >The MINPACK routine called by fsolve() failed to converge; it quit >after making little or no progress over 10 consecutive iterations. >Maybe you need a better initial estimate; maybe there's no solution. > From: Oscar Benjamin > To: tutor at python.org, Dino Bekte?evi? , eryksun > Subject: Re: [Tutor] Python execution timer/proficiency testing >Exactly. Dino, whatever scipy routine you're using is warning you that >it has failed. You should heed this warning since it likely means that >your code is not doing what you want it to do. Without knowing what >you're trying to do and what function you're calling I can't say more >than that. Thank you both I did not know it quits(!) but since my further code never reported an error I assume it returned something similar to initial guess? I will add a test of the returned variable ier and try to find another initial guess or handle it somehow else. Under 'Narrow-field astrometry' are the equations I'm solving and here's the code snippet: row_guess = ( mudiff*fd['f'] - fd['c']*nudiff )/det col_guess = ( fd['b']*nudiff - mudiff*fd['e'] )/det row=zeros(mu.size,dtype='f8') col=zeros(mu.size,dtype='f8') for i in xrange(mu.size): self._tmp_color=color[i] self._tmp_munu=array([mu[i],nu[i]]) rowcol_guess=array([row_guess[i], col_guess[i]]) rowcol = scipy.optimize.fsolve(self._pix2munu_for_fit, rowcol_guess) row[i] = rowcol[0] col[i] = rowcol[1] Thanks, Dino From ndk1991 at gmail.com Tue Aug 27 12:33:24 2013 From: ndk1991 at gmail.com (Nitish Kunder) Date: Tue, 27 Aug 2013 16:03:24 +0530 Subject: [Tutor] os.system() not working Message-ID: Hii I have a python program which i am calling from a php script. The arguments to the program is a path to the file The program when directly run from console executes normally. But when I try to execute the program from browser ie call the python script from php, os.system command is not working what might be the problem. Thanks -------------- next part -------------- An HTML attachment was scrubbed... URL: From rick at linuxmafia.com Tue Aug 27 20:19:14 2013 From: rick at linuxmafia.com (Rick Moen) Date: Tue, 27 Aug 2013 11:19:14 -0700 Subject: [Tutor] Comment on http://www.catb.org/esr/faqs/smart-questions.html Message-ID: <20130827181914.GI2848@linuxmafia.com> /me waves to esteemed tutors and helpers. Quoting Bob Gailer (bgailer at gmail.com): > I have a reaction to > http://www.catb.org/esr/faqs/smart-questions.html and I'd like your > feedback. Perhaps I will then send revised comments to the authors of > the [essay]. As one of the essays' co-authors, I'm always interested in thoughts, especially ones about how to improve the fool thing. (As you'll see below , I have more than a few, myself.) > I found myself a bit disappointed. Me too. (No irony intended.) Eric and I never quite achieved what we hoped with it. > All I see at first is email links and Revision History. If I did not > know to scroll down I might have just figured I was at the wrong page. > > Reading translations and disclaimer is not why I'd come here. Personally, I'd have tried to put the links to translations and the revision history at the end, if possible. Eric maintains the master version, and IIRC uses a Docbook XML toolkit with a precooked stylesheet. I've never personally tried to significantly hack such tools to customise presentation. As maintainer of one Linuxdoc SGML-format FAQ and one Docbook SGML-format HOWTO for the Linux Documentation Project, I've long been a bit dissatisfied with the somewhat inflexible document structure the related toolkits push authors towards. Perhaps Eric found likewise; I'd have to ask him. Anyway, my point is that as an author, I'd generally rather spend time writing than hacking madly on document-production tools to rearrange the elements and alter the presentation. Time permitting, I do hope to get around to working with Eric to see if the order of elements can be rearranged with reasonable ease. However, there are much larger problems, which I'll get to in a minute. > With contact names and email at the bottom Objecting to e-mail links' living at the top doesn't seem reasonable, sorry. Those are merely the names of us co-authors. If it really bothers you to see authors' names at the top of what they write, Bob, you might want to quit the habit of reading before you become truly vexed, as you'll see it more often than not. Books in particular are going to be a huge disappointment to you. As to the 'disclaimer', can you guess _why_ it is there? Let me tell you, sir: For over a decade, every single day of every single year, I get at least a half dozen misdirected helpdesk requests in my personal e-mail. Most are for technical projects (mostly software, but not always) I've never even heard of. Often, they are demanding and downright rude in their insistance that I help the querent with $FOO for some bizarre value of FOO _right now_. I'm always nice to them and try to send them in the right direction, but _man_.... The 'disclaimer', the bit that says that Rick and Eric are not a helpdesk for several thousand projects they (mostly) haven't even heard of, and please for gosh sakes avoid pegging the irony metre and send your help requests to the right place, has cut the daily inflow. I used to get several dozen per day. I'm sorry you're annoyed by seeing it, but not moved to think it's a bad idea. Rather the contrary. If anything, I'm pondering the virtues of tags and red text, man.[1] > There's a lot of words to wade thru Exactly. That is the biggest problem, and it's fundamental such that it can be addressed properly only through a do-over. Let me recount what happened. Around 2000, Eric and I became aware that we were more-or-less writing the same sort of essay (except Eric's wording is pitched as advice to those seeking technical help from 'hackers', while I just was generically talking about online technical help). We joined forces, with Eric keeping the master copy and using his document-production toolchain. We put out a nice -little-, tightly focussed essay. To our astonishment, it proved very popular. Thousands (at minimum) of technical projects linked to it from their help pages. Correspondents kept asking us to add things: 'You should also cover $FOO.' We're obliging people and tried to accomodate most such requests. And they kept coming. Lather, rinse, repeat. Over ten years later, we now realise to our dismay: no more nice -little-, tightly focussed essay. What can I say? We tried, we wrote, people asked us to add more, we obliged, and the results have gradually become bloated. (Tolkien said 'It grew in the telling.' Ours did, too, albeit there the comparison ends.) Moreover, it's an unsatisfyingly linear piece, which is not really what is needed. _That_ along with sheer bloat makes it, IMO, fail at its original goal of helping frustrated and impatient people deal better with techincal projects' online help forums. So, I've been wanting to sit down and start over from scratch, and this time write a nice -little-, tightly focussed essay using a radically different format of some drill-down-for-more variety. Some toolkit hacking may be required -- or maybe just roll my own, in Python, starting from something like ReST rather than overengineered XML stuff. But I've not yet had time. Meanwhile, we're absolutely delighted when other people think they can do better, and enthusiastically encourage them to do so. I'm personally a big fan of Java Ranch's nice -little-, tightly focussed essay for people asking questions. > I also would not have come here to be potentially labeled an "idiot". Being a naive and cheerful optimist, I hope and expect you eventually noticed that Eric's and my essay neither stated nore implied that anyone is an idiot, Bob. We didn't even imply that idiots are idiots. Anyway, thank you for your thoughts. Quoting Emile van Sebille (emile at fenx.com): > In any case, I wouldn't write esr and Rick Moen to request changes -- > I expect you'd be pointed to > http://www.catb.org/esr/faqs/smart-questions.html#disclaimer if you got > a reply at all. We try to give thoughtful responses to anyone who writes us about the essay. After all, we share their desire for it to be useful to people (which is why we goodnaturedly kept adding more and more coverage to it upon request, although in retrospect we probably should have politely said no). Quoting Steven D'Aprano (steve at pearwood.info): > But really, I want to re-write them both [How to Ask Questiosn the > Smart Way and Short, Self-Contained, Correct Example] for a Python > audience. One day, when I get a round tuit. Please do. (Those circular tuits are scarce, I notice.) > In my feedback below, I'm going to attempt to channel ESR, As the saying goes, Eric's co-author is not chopped liver, by the way (but he is not taking offence). [1] If not ALL CAPS BOLD ITALIC RED TEXT WITH GREEN UNDERLINED SPACES BETWEEN WORDS. (Yes, I'm kidding.) From steve at pearwood.info Wed Aug 28 01:32:01 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 28 Aug 2013 09:32:01 +1000 Subject: [Tutor] Global var not defined? In-Reply-To: References: Message-ID: <521D36F1.1010102@pearwood.info> On 28/08/13 03:58, leam hall wrote: > Could use some help with this. Python 2.4.3 on RHEL 5.x. > > In the functions file that gets imported: > > def append_customer(line_list): > global customers Globals are not "globally global", they are global to the module. Otherwise variables defined in module X would stomp all over variables defined in module Y, unpredictably depending on the order than modules were imported. > In the calling file: > > import functions > import sys > > customers = [] This cannot work, because it belongs to a different namespace (module). Just pass customers as an explicit parameter to append_customer, and then google for "Global variables considered harmful". If you absolutely must emulate COBOL programmers of the 1970s and insist on using globals, write: functions.customers = [] instead. -- Steven From alan.gauld at btinternet.com Wed Aug 28 01:34:51 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 28 Aug 2013 00:34:51 +0100 Subject: [Tutor] os.system() not working In-Reply-To: References: Message-ID: On 27/08/13 11:33, Nitish Kunder wrote: > I have a python program which i am calling from a php script. How are you executing the PHP script? Which OS? Which web server? Which browser? Have you tried others? > The arguments to the program is a path to the file > The program when directly run from console executes normally. So the script itself is fine. > But when I try to execute the program from browser ie call the python > script from php, > os.system command is not working what might be the problem. It may be that the browser is not allowed to execute system commands (I hope not since that would be a huge security issue!) But I suspect you are actually running this from a webserver not a browser - that's the usual PHP environment. The webserver runs PHP which renders up HTML out to the browser. In that case the os.system call will try to run on the web server. But it will be running under the webserver account which, again for good reason, may not be permitted to execute system commands. Other than that its hard to say anything since you don't tell us what os.system() is trying to execute nor what "not working" means. Finally remember that os.system() does not give you any access to data output by the command, you only get the exit code returned. You will need to use the subprocess module if you want to use the output of the system command (eg to send it to the browser to display). -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From oscar.j.benjamin at gmail.com Wed Aug 28 02:38:15 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 28 Aug 2013 01:38:15 +0100 Subject: [Tutor] =?utf-8?q?Python_execution_timer/proficiency_testing_=28D?= =?utf-8?b?aW5vIEJla3RlxaFldmnEhyk=?= In-Reply-To: References: Message-ID: On 27 August 2013 15:05, Dino Bekte?evi? wrote: > Thank you both I did not know it quits(!) but since my further code > never reported an error I assume it returned something similar to > initial guess? > I will add a test of the returned variable ier and try to find another > initial guess or handle it somehow else. > Under 'Narrow-field astrometry' are the equations I'm solving and > here's the code snippet: > > > row_guess = ( mudiff*fd['f'] - fd['c']*nudiff )/det > col_guess = ( fd['b']*nudiff - mudiff*fd['e'] )/det > > row=zeros(mu.size,dtype='f8') > col=zeros(mu.size,dtype='f8') > for i in xrange(mu.size): > self._tmp_color=color[i] > > self._tmp_munu=array([mu[i],nu[i]]) > > rowcol_guess=array([row_guess[i], col_guess[i]]) > > rowcol = scipy.optimize.fsolve(self._pix2munu_for_fit, rowcol_guess) Eryksun guessed that fsolve was the source of the message. If the line above emits the warning you mentioned then the output most likely is not a solution to the equations. As Eryksun said it could be that your initial guess isn't good or it could be that there is no solution. Another possibility is that you're just not working with a very well-behaved function: there are solutions but the solver wouldn't find them no matter how good your initial guess. In any case while it continues to emit that warning message you cannot trust the results it returns. Try passing full_output=True to get more information e.g.: x, infodict, ier, msg = scipy.optimize.fsolve(..., full_output=True) Then have a look at the infodict, ier and msg variables to see if they tell you more about what happened: http://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.fsolve.html Oscar From steve at pearwood.info Wed Aug 28 04:19:11 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 28 Aug 2013 12:19:11 +1000 Subject: [Tutor] spss.BasePivotTable In-Reply-To: <1377609273.61990.YahooMailNeo@web163805.mail.gq1.yahoo.com> References: <1377609273.61990.YahooMailNeo@web163805.mail.gq1.yahoo.com> Message-ID: <521D5E1F.1070607@pearwood.info> On 27/08/13 23:14, Albert-Jan Roskam wrote: > Hello, > > I am trying to create a BasePivot table with three columns: one for the labels, one for a a category "Before" and one for "After". The following attempt fails, but what am I doing wrong? At least two things. Here is the first: > try: [much code] > except spss.SpssError: > print "Error." You should replace that line with: print """Python gives you a nice traceback showing you exactly what went wrong and where it went wrong, but I'm not going to show it to you. Instead, you have to guess, because Screw You. """ It's a little more wordy, but more accurate. And the second: > print spss.GetLastErrorMessage() You're apparently not reading what the last error message is, or if you have read it, you're keeping it a secret from us. What you actually ought to do is preferably get rid of the try...except altogether, at least while debugging. Your aim as a programmer is not to hide errors, but to eliminate them. Hiding them only makes it harder to identify the errors, which is the first step in eliminating them. If you truly cannot get rid of the try...except, then do this instead: try: [much code] except spss.SpssError: print spss.GetLastErrorMessage() raise which will give you both the spss last error message, and the Python traceback. -- Steven From rammy.sagar at gmail.com Wed Aug 28 07:05:31 2013 From: rammy.sagar at gmail.com (ram sagar) Date: Wed, 28 Aug 2013 10:35:31 +0530 Subject: [Tutor] Runestone Python Course In-Reply-To: References: <52171AD3.4000509@libero.it> Message-ID: I am a beginner in python i found the Runestone python learning environment the best one i came across. CodeLens is the best feature , as it shows the actual working of the program in graphical way that one will never forget those details. Thanks to all the people who involved in this project. Ram Sagar Mourya On Sat, Aug 24, 2013 at 12:32 AM, Jim Mooney wrote: > But I discovered that if you delete the single leading blank in front of > each line, then it works. > Maybe their parser doesn't like unindented lines starting with a space... > ========= > That's odd. I haven't had any of those problems. I wonder if it's your > browser. I'm using FF. Are you doing the first course or the more advanced > one? I'm only looking at the first one. If you write them I 've found them > to be very cooperative, which is the norm for open source stuff if you're > polite. imagine trying to get through the Microsoft phalanx to the > programmer, if you could even find them, and then they'd say they had no > authority to make a change until it was reviewed by the Committee of 400, a > year from now ;') > > Jim > > > On 23 August 2013 01:18, Francesco Loffredo wrote: > >> Omar Abou Mrad wrote: >> >>> >>> >>> >>> On Wed, Aug 21, 2013 at 7:52 AM, Jim Mooney >> cybervigilante at gmail.**com >> wrote: >>> >>> http://interactivepython.org >>> >>> >>> >>> >>> Would be nice if it worked though, logged in through my google account, >>> now i get this error which I can do nothing about: >>> >>> >>> Sorry, Something went wrong >>> >>> The error is: |invalid request| >>> >>> |It was the same for me|. It gives this error message as soon as I try >> to run one of their "ActiveCode" examples. >> But I discovered that if you delete the single leading blank in front of >> each line, then it works. >> Maybe their parser doesn't like unindented lines starting with a space... >> >> It's a nuisance, though. >> >> Francesco >> ______________________________**_________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/**mailman/listinfo/tutor >> > > > > -- > Jim > > More and more, science is showing that animals, even "simple" ones, have > awareness and feelings. There is no hard divide, as the rape-the-earth > crowd would have us believe.. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > -------------- next part -------------- An HTML attachment was scrubbed... URL: From chris at chrisdown.name Wed Aug 28 08:03:43 2013 From: chris at chrisdown.name (Chris Down) Date: Wed, 28 Aug 2013 08:03:43 +0200 Subject: [Tutor] os.system() not working In-Reply-To: References: Message-ID: <20130828060342.GA530@chrisdown.name> Hello, On 2013-08-27 16:03, Nitish Kunder wrote: > I have a python program which i am calling from a php script. > The arguments to the program is a path to the file > The program when directly run from console executes normally. > But when I try to execute the program from browser ie call the python > script from php, > os.system command is not working what might be the problem. Your question is lacking some context, like Alan already mentioned, but my first suspicions would be: - Relying on a PATH that doesn't exist/is not as you expect in that environment - Relying on a working directory that turns out to not be what you expected -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From nik at naturalnet.de Wed Aug 28 08:49:43 2013 From: nik at naturalnet.de (Dominik George) Date: Wed, 28 Aug 2013 08:49:43 +0200 Subject: [Tutor] os.system() not working In-Reply-To: References: Message-ID: Hi, in any case and apart from what was already said, your setup sounds very awkward, if not insane. Are you sure your implementation is a good idea? -nik Nitish Kunder schrieb: >Hii >I have a python program which i am calling from a php script. >The arguments to the program is a path to the file >The program when directly run from console executes normally. >But when I try to execute the program from browser ie call the python >script from php, >os.system command is not working what might be the problem. >Thanks > > >------------------------------------------------------------------------ > >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >http://mail.python.org/mailman/listinfo/tutor -- Diese Nachricht wurde von meinem Android-Mobiltelefon mit K-9 Mail gesendet. -------------- next part -------------- An HTML attachment was scrubbed... URL: From wolfrage8765 at gmail.com Wed Aug 28 14:16:56 2013 From: wolfrage8765 at gmail.com (wolfrage8765 at gmail.com) Date: Wed, 28 Aug 2013 08:16:56 -0400 Subject: [Tutor] os.system() not working In-Reply-To: References: Message-ID: On Tue, Aug 27, 2013 at 6:33 AM, Nitish Kunder wrote: > Hii > I have a python program which i am calling from a php script. > The arguments to the program is a path to the file > The program when directly run from console executes normally. > But when I try to execute the program from browser ie call the python script > from php, > os.system command is not working what might be the problem. > Thanks Due to the lack of context I am forced to think you must be slightly confused. PHP is not Python. Thus your PHP script can not execute the os.system() function, because it does not exist in PHP, only in Python. PHP equivalent would be exec() at least as far as I know, but use it carefully and with caution. From chris at chrisdown.name Wed Aug 28 15:23:34 2013 From: chris at chrisdown.name (Chris Down) Date: Wed, 28 Aug 2013 15:23:34 +0200 Subject: [Tutor] os.system() not working In-Reply-To: References: Message-ID: <20130828132334.GB11425@chrisdown.name> On 2013-08-28 08:16, wolfrage8765 at gmail.com wrote: > PHP is not Python. You misread. > > I have a python program which i am calling from a php script. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From fomcl at yahoo.com Wed Aug 28 18:10:58 2013 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 28 Aug 2013 09:10:58 -0700 (PDT) Subject: [Tutor] spss.BasePivotTable In-Reply-To: <521D5E1F.1070607@pearwood.info> References: <1377609273.61990.YahooMailNeo@web163805.mail.gq1.yahoo.com> <521D5E1F.1070607@pearwood.info> Message-ID: <1377706258.71116.YahooMailNeo@web163805.mail.gq1.yahoo.com> From: Steven D'Aprano >To: tutor at python.org >Sent: Wednesday, August 28, 2013 4:19 AM >Subject: Re: [Tutor] spss.BasePivotTable > > >On 27/08/13 23:14, Albert-Jan Roskam wrote: >> Hello, >> >> I am trying to create a BasePivot table with three columns: one for the labels, one for a a category "Before" and one for "After". The following attempt fails, but what am I doing wrong? > >At least two things. Here is the first: > >> try: >? ? [much code] >> except spss.SpssError: >>? ? ? print "Error." > >You should replace that line with: > >print """Python gives you a nice traceback showing you exactly >what went wrong and where it went wrong, but I'm not going to >show it to you. Instead, you have to guess, because Screw You. >""" > >It's a little more wordy, but more accurate. ;-) Thank you, I entirely agree. I hate nondescriptive errors myself, too. >And the second: > >> print spss.GetLastErrorMessage() > >You're apparently not reading what the last error message is, or if you have read it, you're keeping it a secret from us. > > >What you actually ought to do is preferably get rid of the try...except altogether, at least while debugging. Your aim as a programmer is not to hide errors, but to eliminate them. Hiding them only makes it harder to identify the errors, which is the first step in eliminating them. ? I was also thinking that there was a bit MUCH code in the try-except. But sometimes I keep it all in a try-clause because these lines of code are one coherent block. From nybor.robyn at gmail.com Wed Aug 28 04:41:49 2013 From: nybor.robyn at gmail.com (Robyn Perry) Date: Tue, 27 Aug 2013 19:41:49 -0700 Subject: [Tutor] Fwd: Need help returning a null/blank value when using string indexes Message-ID: Hi, This is my first time asking for help on this list, so I welcome constructive criticism about how to make my question clearer. AND I'm thankful that I found this list! Here's my issue: Below the line, I've copied and pasted an assignment. This isn't homework -- it's practice on a Udacity (free online course) as I'm learning Python. I've written it out and gotten it almost correct except for Test Case 3. All the other problem sets give you the answers, but this one was suggested from a student in the class and there was no given solution that I could use to see where I'm going wrong. Which brings me to you all. The task asks me to assign something to variables part1, part2, and part3 using the given variables fragA, fragB, and fragC. Everything works except that I can't seem to give part2 the right assignment to produce "Ucity". I've gotten this far as an assignment for part2: part2 = (fragA[-7:-5] + '') But it asks me to use a string, not a blank space. part2 needs begin with 'da' so I can produce 'Udacity' in Test Case 2, but needs to somehow end with a null kind of value so I can produce 'Ucity' in Test Case 3. See below for the complete assignment, in comments, for more clarity in what's going on: (By the way, the commented out bit at the end is just my notes to help steer me in the right direction). _________________________________________________________ # Write one line of Python code that uses # only the variables fragA, fragB, and fragC # to satisfy the given test cases. # If you are not sure how multiple assignments and # string slicing works, check out the links to # additional tutorials in Instructor Comments # under this exercise! fragA, fragB, fragC = 'supercalifragilisticexpialudacious', \ 'SUPERMAN', 'ytiroirepus' ### WRITE MULTIPLE ASSIGNMENT HERE ### ### THIS MUST BE ONE LINE ### part1, part2, part3 = fragB[1], (fragA[-7:-5] + ''), (fragA[5] + fragC[2] + fragC[1] + fragC[0]) ### TEST CASES. PRESS RUN TO TEST ### deadline = part1 + part2[0:2] + part3[-1] print "Test case 1 (Uday): ", deadline == 'Uday' fixed = part1 + part2 + part3 print "Test case 2 (Udacity): ", fixed == 'Udacity' destination = part1 + part2[-1] + part3 print "Test case 3 (Ucity): ", destination == 'Ucity' #part1, part2, part3 = fragB[1], fragA[-7:-5], fragC[0] #deadline #part3 = ....y #part2 = da #part1 = U #part1, part2, part3 = fragB[1], fragA[-7:-5], ('c' + fragC[2] + fragC[1] + fragC[0]) #fixed #part3 = city #part2 = da #part1 = U #part1, part2, part3 = fragB[1], fragA[-7:-5], fragC[0] #destination #part3 = city #part2 = da' ' #part1 = U ________________________ Thank you kindly! Robyn -------------- next part -------------- An HTML attachment was scrubbed... URL: From davea at davea.name Thu Aug 29 02:18:50 2013 From: davea at davea.name (Dave Angel) Date: Thu, 29 Aug 2013 00:18:50 +0000 (UTC) Subject: [Tutor] Fwd: Need help returning a null/blank value when using string indexes References: Message-ID: On 27/8/2013 22:41, Robyn Perry wrote: > > part2 = (fragA[-7:-5] + '') > > But it asks me to use a string, not a blank space. part2 needs begin with > 'da' so I can produce 'Udacity' in Test Case 2, but needs to somehow end > with a null kind of value so I can produce 'Ucity' in Test Case 3. See > below for the complete assignment, in comments, for more clarity in what's > going on: > It's been so long since I've seen such a nonsensical assignment. However, I can point out a few things that might clarify it for you, as you've presumably seen the entire assignment. If you're trying to produce destination from part1, part2, and part3, I don't know why you don't just use part1 + part3 If you're required somehow to use all three variables, then you can just use part1 + part2[0:0] + part3 There's no need to have a "null value" within part2. -- DaveA From i.sheeha at gmail.com Thu Aug 29 09:37:10 2013 From: i.sheeha at gmail.com (Ismar Sehic) Date: Thu, 29 Aug 2013 09:37:10 +0200 Subject: [Tutor] XMLRPC question Message-ID: Hello, I should write a python script(s) that listens to an existing XMLRPC service on my company's dev server.My utility should take some data from that XMLRPC, send it to an online xml service provider(it's something about hotel accomodation, where they have xml patterns for different requests), then they return their response to me, also in xml data interchange format.then i should parse that and return to the existing XML-RPC, or write the parsed data to the Posgresql database.The thing is, i know something about XML parsing, i know also something about XMLRPC, but i'm not permitted to edit the existing XMLRPC service.Can anyone just tell me what is the shortest and the best solution for this problem, and give me some guidelines for my project? -------------- next part -------------- An HTML attachment was scrubbed... URL: From dfjennings at gmail.com Thu Aug 29 12:52:23 2013 From: dfjennings at gmail.com (Don Jennings) Date: Thu, 29 Aug 2013 06:52:23 -0400 Subject: [Tutor] XMLRPC question In-Reply-To: References: Message-ID: <23B118E2-9F09-47FD-A93C-B8D440675FE0@gmail.com> On Aug 29, 2013, at 3:37 AM, Ismar Sehic wrote: > Hello, > I should write a python script(s) that listens to an existing XMLRPC service on my company's dev server. Yes, you should do that. Then, if you have problems, you should show some of your code to the fine tutors here, and they'll be happy to help you out :>) > My utility should take some data from that XMLRPC, send it to an online xml service provider(it's something about hotel accomodation, where they have xml patterns for different requests), then they return their response to me, also in xml data interchange format.then i should parse that and return to the existing XML-RPC, or write the parsed data to the Posgresql database.The thing is, i know something about XML parsing, i know also something about XMLRPC, That's good, but we have no idea what you know, unless you tell us. > but i'm not permitted to edit the existing XMLRPC service.Can anyone just tell me what is the shortest and the best solution for this problem, and give me some guidelines for my project? In answer to the question you asked, yes (but they'll probably want your paycheck ;>) A quick search [1] yields a couple of promising articles [2] [3]. Including Doug Hellman's work in our search [4] provides another resource [5]. Take care, Don [1] https://duckduckgo.com/?q=python+xmlrpc [2] http://www.ibm.com/developerworks/webservices/library/ws-pyth10/index.html [3] http://www.xml.com/pub/r/1159 [4] https://duckduckgo.com/?q=python+xmlrpc+pymotw [5] http://pymotw.com/2/xmlrpclib/ From steve at pearwood.info Fri Aug 30 10:33:07 2013 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 30 Aug 2013 18:33:07 +1000 Subject: [Tutor] Comment on http://www.catb.org/esr/faqs/smart-questions.html In-Reply-To: <20130827181914.GI2848@linuxmafia.com> References: <20130827181914.GI2848@linuxmafia.com> Message-ID: <522058C3.2090804@pearwood.info> On 28/08/13 04:19, Rick Moen wrote: > /me waves to esteemed tutors and helpers. /me waves back Did you subscribe to the list just to discuss this? I'm impressed by your dedication! Thank you! [snip explanation for why Smart Questions is the way it is] Thank you for taking the time to explain. It certainly puts things in a different perspective. > Quoting Steven D'Aprano (steve at pearwood.info): [...] >> In my feedback below, I'm going to attempt to channel ESR, > > As the saying goes, Eric's co-author is not chopped liver, by the way > (but he is not taking offence). I'm glad, because no offence was intended. Somehow I always associated Smart Questions with ESR. -- Steven From fomcl at yahoo.com Fri Aug 30 17:04:14 2013 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 30 Aug 2013 08:04:14 -0700 (PDT) Subject: [Tutor] myown.getfilesystemencoding() Message-ID: <1377875054.96190.YahooMailNeo@web163806.mail.gq1.yahoo.com> In Windows, sys.getfilesystemencoding() returns 'mbcs' (multibyte code system), which doesn't say very much imho. So I wrote the function below, which returns the codepage as reported by the windows chcp command. I noticed that the function returns 850 (codepage 850) when I run it via the command prompt, but 1252 (cp1252) when I run it in my IDE (Spyder). Any idea why? Is?it a good idea anyway to make this function (no, probably, because Python devs are smart people ;-) ? #!python.exe #Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32 ? import subprocess, re, sys ? def getfilesystemencoding(): ??? if sys.platform.startswith("win"): ??????? proc = subprocess.Popen("chcp", shell=True, stdout=subprocess.PIPE) ??????? m = re.search(": (?P\d+)", proc.communicate()[0]) ??????? if m: ??????????? return m.group("codepage") ??????? return sys.getfilesystemencoding() ??? return sys.getfilesystemencoding() print getfilesystemencoding() Regards, Albert-Jan ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~???? From chris at chrisdown.name Fri Aug 30 17:21:25 2013 From: chris at chrisdown.name (Chris Down) Date: Fri, 30 Aug 2013 17:21:25 +0200 Subject: [Tutor] myown.getfilesystemencoding() In-Reply-To: <1377875054.96190.YahooMailNeo@web163806.mail.gq1.yahoo.com> References: <1377875054.96190.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: <20130830152124.GC2477@chrisdown.name> On 2013-08-30 08:04, Albert-Jan Roskam wrote: > In Windows, sys.getfilesystemencoding() returns 'mbcs' (multibyte code > system), which doesn't say very much imho. Well, what's the problem you have with mbcs being the output here? On NT, mbcs is the encoding that should be used to convert Unicode to a bytestring that is equivalent when used as a path name, after all. -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From eryksun at gmail.com Fri Aug 30 18:39:25 2013 From: eryksun at gmail.com (eryksun) Date: Fri, 30 Aug 2013 12:39:25 -0400 Subject: [Tutor] myown.getfilesystemencoding() In-Reply-To: <1377875054.96190.YahooMailNeo@web163806.mail.gq1.yahoo.com> References: <1377875054.96190.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: On Fri, Aug 30, 2013 at 11:04 AM, Albert-Jan Roskam wrote: > In Windows, sys.getfilesystemencoding() returns 'mbcs' (multibyte code > system), which doesn't say very much imho. Why aren't you using Unicode for the filename? The native encoding for NTFS is UTF-16, and CPython 2.x uses _wfopen() if you pass it a Unicode filename: http://hg.python.org/cpython/file/70274d53c1dd/Objects/fileobject.c#l357 http://msdn.microsoft.com/en-us/library/yeby3zcb(v=vs.90) Anyway, the "mbcs" codec uses mbcs_encode() and mbcs_decode() from the codecs module. In CPython 2.x, these call PyUnicode_EncodeMBCS() and PyUnicode_DecodeMBCS(), which in turn call the Windows API functions WideCharToMultiByte() and MultiByteToWideChar() for the CP_ACP (ANSI) codepage. This is a system defined encoding, such as Windows 1252. > So I wrote the function below, which returns the codepage as reported by > the windows chcp command. chcp.com is a console application. It's calling GetConsoleCP(), which simply returns the current code page of the attached console (running the command creates a new console if there isn't one to inherit from the parent). This isn't the function you want. There's already a Python function that returns the default ANSI codepage: >>> import locale >>> locale.getpreferredencoding() 'cp1252' You can also use ctypes to call the Windows API directly, and then convert the integer to a string: >>> from ctypes import windll >>> str(windll.kernel32.GetACP()) '1252' > the function returns 850 (codepage 850) when I run it via the command prompt, > but 1252 (cp1252) when I run it in my IDE (Spyder). Maybe Spyder communicates with python.exe as a subprocess in a hidden console, with the console's codepage set to 1252. You can use ctypes to check windll.kernel32.GetConsoleCP(). If a console is attached, this will return a nonzero value. From niubao56 at gmail.com Fri Aug 30 20:10:26 2013 From: niubao56 at gmail.com (Bao Niu) Date: Fri, 30 Aug 2013 11:10:26 -0700 Subject: [Tutor] Best practice for generalizing and documenting each method's behaviour Message-ID: I'm starting a small project coding in Python as I learn the ropes. As the project grows bigger, there are more and more overlapping and even redundant methods. For example, several classes have a checkAndClean_obj_state() method. If just one or two such classes, it is easy to analyze the behaviour of them and design the optimal interaction for all objects. However, when there are many of such classes, exactly at what point to invoke check and clean behaviour becomes a little blurred. There is a desperate need for generalizing and documenting the behaviour of each such class, preferably in a flowchart. I'm currently doing the flowchart manually but the job becomes a bit overwhelming. I wonder what Python pros are using for analyzing and documenting classes/functions behaviours and interactions? Is UML the only way? Personally I found UML is a bit overkill for a one person project, but I'm not sure if it is the right direction. I'd appreciate any insight. Many thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: From alan.gauld at btinternet.com Sat Aug 31 00:59:55 2013 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 30 Aug 2013 23:59:55 +0100 Subject: [Tutor] Best practice for generalizing and documenting each method's behaviour In-Reply-To: References: Message-ID: On 30/08/13 19:10, Bao Niu wrote: > ... However, when there are many of such classes, exactly > at what point to invoke check and clean behaviour becomes a little > blurred. I'm not too sure what you mean by that. Can you give an example? > There is a desperate need for generalizing and documenting the > behaviour of each such class, preferably in a flowchart. I'm currently > doing the flowchart manually but the job becomes a bit overwhelming. Apart from documenting high level processes or very low level code I'd not recommend a flowchart for anything much. UML Activity diagrams are a similar but much more powerful option. But even then I'm not sure I'd use them in the way you seem to be suggesting? > I wonder what Python pros are using for analyzing and documenting > classes/functions behaviours and interactions? Is UML the only way? No there are many options. The simplest is probably CRC cards. And they can be turned into UML later if need be. Other OO methodologies exist too, but they are generally variations on the UML notation more than anything. > Personally I found UML is a bit overkill for a one person project You are not supposed to use all of UML on every project. For example in my work as an "Enterprise Architect" I use UML extensively but very rarely draw a class diagram and never draw object diagrams. But I use Use Case diagrams, Activity Diagrams, Sequence Charts and Deployment Diagrams in almost every project... But as a programmer I mainly use Class Diagrams, Object Diagrams, State Charts and Activity Diagrams. On a small project just a Class diagram and a few Object interaction diagrams (or sequence charts if you prefer) would do. The main thing to remember is that design documentation is primarily for the people who come after. You are trying to tell these future workers (who may be yourself) what you did to make it work. Too many people fall into the trap of trying to write design documents that tell the current developers how to do their job - that's not usually necessary, they are probably the folks writing the documents! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ From oscar.j.benjamin at gmail.com Sat Aug 31 15:16:39 2013 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sat, 31 Aug 2013 14:16:39 +0100 Subject: [Tutor] myown.getfilesystemencoding() In-Reply-To: References: <1377875054.96190.YahooMailNeo@web163806.mail.gq1.yahoo.com> Message-ID: On 30 August 2013 17:39, eryksun wrote: > On Fri, Aug 30, 2013 at 11:04 AM, Albert-Jan Roskam wrote: > >> the function returns 850 (codepage 850) when I run it via the command prompt, >> but 1252 (cp1252) when I run it in my IDE (Spyder). > > Maybe Spyder communicates with python.exe as a subprocess in a hidden > console, with the console's codepage set to 1252. You can use ctypes > to check windll.kernel32.GetConsoleCP(). If a console is attached, > this will return a nonzero value. Spyder has both an internal interpreter and an external interpreter. One is the same interpreter process that runs the Spyder GUI. The other is run in a subprocess which keeps the GUI safe but reduces your ability to inspect the workspace data via the GUI. So presumable Albert means the "external" interpreter here. Also Spyder has the option to use ipython as the shell for (I think) either interpreter and ipython does a lot of weirdness to stdin/stdout etc. (according to the complaints of the Spyder author when users asked for ipython support). Oscar From dvnsarma at gmail.com Sat Aug 31 18:30:23 2013 From: dvnsarma at gmail.com (=?UTF-8?B?RC5WLk4uU2FybWEg4LCh4LC/LuCwteCwvy7gsI7gsKjgsY0u4LC24LCw4LGN4LCu?=) Date: Sat, 31 Aug 2013 22:00:23 +0530 Subject: [Tutor] A mergesort Message-ID: I have been searching for mergesort implimentations in python and came across this. def merge(a, b): if len(a)*len(b) == 0: return a+b v = (a[0] < b[0] and a or b).pop(0) return [v] + merge(a, b) def mergesort(lst): if len(lst) < 2: return lst m = len(lst)/2 return merge(mergesort(lst[:m]), mergesort(lst[m:])) mlst = [10, 9, 8, 4, 5, 6, 7, 3, 2, 1] sorted = mergesort(mlst) print sorted Besides using recursion in merge function also, it has somethings intresting. Especially the statement v = (a[0] < b[0] and a or b).pop(0) gives a.pop(0), if a[0] < b[0] otherwise b.pop(0). We have to look at the statement as v = ((a[0] < b[0] and a) or b).pop(0) regards, Sarma. -------------- next part -------------- An HTML attachment was scrubbed... URL: From chris at chrisdown.name Sat Aug 31 18:43:57 2013 From: chris at chrisdown.name (Chris Down) Date: Sat, 31 Aug 2013 18:43:57 +0200 Subject: [Tutor] A mergesort In-Reply-To: References: Message-ID: <20130831164356.GA18650@chrisdown.name> On 2013-08-31 22:00, D.V.N.Sarma ??.??.???.???? wrote: > def merge(a, b): > if len(a)*len(b) == 0: > return a+b Indentation in Python matters; if you're going to post code, you should probably keep it. > We have to look at the statement as > > v = ((a[0] < b[0] and a) or b).pop(0) This is short circuit evaluation, which is fairly common in programming languages.[0] 0: https://en.wikipedia.org/wiki/Short-circuit_evaluation -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 490 bytes Desc: not available URL: From stefan_ml at behnel.de Sat Aug 31 22:46:14 2013 From: stefan_ml at behnel.de (Stefan Behnel) Date: Sat, 31 Aug 2013 22:46:14 +0200 Subject: [Tutor] A mergesort In-Reply-To: References: Message-ID: D.V.N.Sarma ??.??.???.????, 31.08.2013 18:30: > I have been searching for mergesort implimentations in python and came > across this. In case this isn't just for education and you actually want to use it, the built-in sorting algorithm in Python (used by list.sort() and sorted()) is a very fast mergesort variant. Anything you could write in Python code is bound to be slower. Stefan From jacklittlemc at yahoo.com Sat Aug 31 23:30:36 2013 From: jacklittlemc at yahoo.com (Jack Little) Date: Sat, 31 Aug 2013 14:30:36 -0700 (PDT) Subject: [Tutor] how to save variables after a user quits in python Message-ID: <1377984636.28529.YahooMailNeo@web124502.mail.ne1.yahoo.com> I am coding a game and I want the player to be able to quit the game and immediately take off right from where they started from. --Jack -------------- next part -------------- An HTML attachment was scrubbed... URL: