From ben+python at benfinney.id.au Sun Mar 1 01:27:25 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 01 Mar 2015 11:27:25 +1100 Subject: [Tutor] BinaryTrees References: Message-ID: <8561al36ky.fsf@benfinney.id.au> Fatimah Taghdi writes: > So i am doing this assignment and we have make a function that changes > a binary tree represenation list of lists to nodes and I was wondering > if somone can help on where to start Your course material (if it's any good) will have given you good places to start. You will already have gained knowledge of the fundamental pieces to use, and the ability to put them together. Start by experimenting! Use the parts you've already learned about, read the assignment and relate the concepts there to the concepts you've already learned. Then show us the code you've come up with. Honestly, it's much more valuable to fit this into the course you're doing, than to have someone tell you ?where to start?. Start with the preceding lessons :-) -- \ ?I put instant coffee in a microwave oven and almost went back | `\ in time.? ?Steven Wright | _o__) | Ben Finney From ftaghdia at gmail.com Sun Mar 1 17:19:25 2015 From: ftaghdia at gmail.com (Fatimah Taghdi) Date: Sun, 1 Mar 2015 11:19:25 -0500 Subject: [Tutor] How to test a class in python? Message-ID: Hello I was wondering how to test a class in python is it the same way as testing a function ? -- *F.T.* From alan.gauld at btinternet.com Sun Mar 1 20:41:48 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 01 Mar 2015 19:41:48 +0000 Subject: [Tutor] How to test a class in python? In-Reply-To: References: Message-ID: On 01/03/15 16:19, Fatimah Taghdi wrote: > Hello I was wondering how to test a class in python is it the same way as > testing a function ? Possibly but it depends on how you test a function, there are many options. So if you tell us how you would test a function like def square(n): return n ** 2 We can consider how to extend that to a class like class Square: def __init__(self, side = 0): self.sode = 0 def area(self): return self.side ** 2 -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Mon Mar 2 05:41:48 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 1 Mar 2015 20:41:48 -0800 Subject: [Tutor] How to test a class in python? In-Reply-To: References: Message-ID: On Sun, Mar 1, 2015 at 11:41 AM, Alan Gauld wrote: > On 01/03/15 16:19, Fatimah Taghdi wrote: >> >> Hello I was wondering how to test a class in python is it the same way as >> testing a function ? Depending on the design of the class, there might be a little bit more set-up involved. But the ideas are similar. In a test case for a function, we have a few things in hand: 1. The function being tested. 2. The parameters we're going to pass as inputs to that function. 3. The expected output we want to see if the function is behaving properly. In a test case for a class, we have a few more things in hand. 1. The class being tested. 2. The parameters we use to create the class instance. 3. The particular method of the class that we're testing. 4. The parameters we're going to pass as inputs to that method. 5. The expected output we want to see if the method is behaving properly. So you can see that the core concept for testing is the same: we express the things that are going to change, and we also say what the expected result is supposed to be. It just so happens that classes can have a lot more state, and this can contribute to a few extra steps to do the test set up. Do you have a particular class in mind? From alan.gauld at btinternet.com Mon Mar 2 09:12:53 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 02 Mar 2015 08:12:53 +0000 Subject: [Tutor] How to test a class in python? In-Reply-To: References: Message-ID: On 02/03/15 04:41, Danny Yoo wrote: > In a test case for a class, we have a few more things in hand. > > 1. The class being tested. > 2. The parameters we use to create the class instance. > 3. The particular method of the class that we're testing. > 4. The parameters we're going to pass as inputs to that method. > 5. The expected output we want to see if the method is behaving properly. And if the class has static or class methods you need to test those too. Although they are much more like ordinary functions. > expected result is supposed to be. It just so happens that classes > can have a lot more state, and this can contribute to a few extra > steps to do the test set up. I was going to pick this up once the discussion got going, but since you've raised it now... Classes/objects maintain state, so the same method called with the same inputs can return different outputs at different times. That rarely happens with functions. So it is important when testing classes that you exercise the entire state machine. That may require test cases that call specific combinations of methods in a particular sequence with particular sets of inputs. This can lead to a very large set of tests (sometimes known as a state explosion) and the best way to handle such tests is to data drive them, but that usually requires much more complex testing code than you would normally have for functions. So although the principles of testing functions and classes are the same the practice can be much more complex. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From marcos.al.azevedo at gmail.com Mon Mar 2 05:35:26 2015 From: marcos.al.azevedo at gmail.com (Marcos Almeida Azevedo) Date: Mon, 2 Mar 2015 12:35:26 +0800 Subject: [Tutor] BinaryTrees In-Reply-To: References: Message-ID: On Sun, Mar 1, 2015 at 4:20 AM, Fatimah Taghdi wrote: > So i am doing this assignment and we have make a function that changes a > binary tree represenation list of lists to nodes and I was wondering if > Maybe share the starting code given to you? Or code you already have? > somone can help on where to start and If there is a possiblity of keeping > track of the indexdes. > > -- > > *F.T.* > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Marcos | I love PHP, Linux, and Java From s.shall at virginmedia.com Mon Mar 2 17:25:52 2015 From: s.shall at virginmedia.com (Sydney Shall) Date: Mon, 02 Mar 2015 16:25:52 +0000 Subject: [Tutor] What exactly is "state"? Message-ID: <54F48F10.907@virginmedia.com> I am a beginner and I am now at the strage of learning to write unittests. I have followed the current discussion entitled "How to test a class in pyhton", and I am not clear precisely what is meant by state. In its common meaning I can see some relevance. But is there a technical aspect to the notion. I see it mentioned often and feel rather uncomfortable that I know so little about it. I have deliberately started a new thread. Thanks. -- Sydney From joel.goldstick at gmail.com Mon Mar 2 18:23:50 2015 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Mon, 2 Mar 2015 12:23:50 -0500 Subject: [Tutor] What exactly is "state"? In-Reply-To: <54F48F10.907@virginmedia.com> References: <54F48F10.907@virginmedia.com> Message-ID: On Mon, Mar 2, 2015 at 11:25 AM, Sydney Shall wrote: > I am a beginner and I am now at the strage of learning to write unittests. > I have followed the current discussion entitled "How to test a class in > pyhton", and I am not clear precisely what is meant by state. In its common > meaning I can see some relevance. But is there a technical aspect to the > notion. I see it mentioned often and feel rather uncomfortable that I know > so little about it. > I have deliberately started a new thread. > Thanks. > -- > Sydney > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor 'State' is the present value of all variables related to the code that is running. Often code execution is dependent on the present state to determine the path of execution. -- Joel Goldstick http://joelgoldstick.com From dyoo at hashcollision.org Mon Mar 2 18:34:14 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 2 Mar 2015 09:34:14 -0800 Subject: [Tutor] What exactly is "state"? In-Reply-To: <54F48F10.907@virginmedia.com> References: <54F48F10.907@virginmedia.com> Message-ID: On Mon, Mar 2, 2015 at 8:25 AM, Sydney Shall wrote: > I am a beginner and I am now at the strage of learning to write unittests. > I have followed the current discussion entitled "How to test a class in > pyhton", and I am not clear precisely what is meant by state. In its common > meaning I can see some relevance. But is there a technical aspect to the > notion. I see it mentioned often and feel rather uncomfortable that I know > so little about it. Hi Sydney, Let's take a concrete example that might help things get started. Let's say that we're writing a playlist generator for a music broadcast system. ########################## class Playlist(object): # ...? ########################## What do we want the Playlist to know? When we create such a Playlist, what does the playlist have in hand? Let's say that it has a complete list of songs when it's being built. ############################## class Playlist(object): def __init__(self, songs): self.songs = songs ############################## That is, 'songs' is a piece of a playlist's state, something that it owns. So, we can imagine creating a playlist with songs like: ############################## class Playlist(object): def __init__(self, songs): self.songs = songs ## Here's an example playlist. playlist = Playlist(["Happy", "Beethoven's 9th Symphony", "Power"]) ############################## Ok, a bit eclectic. But let's go with it. Let's say that we want to add a simple behavior to this class, so that it can do something useful. Let's be able to ask the playlist to give us the *next* song. What would we like next to do here? Let's say that if we call it once on the playlist example above, we'd like to first see "Happy". If we call next() again, we'd like to see "Beethoven", and if we call next() again, we'd like to see "Power". And for the sake of it, once we're out of songs, let's just see "*silence*". We can express the previous paragraph as a unit test! ############################## import unittest class PlaylistTest(unittest.TestCase): def testSimple(self): playlist = Playlist(["Happy", "Beethoven", "Power"]) self.assertEqual(playlist.next(), "Happy") self.assertEqual(playlist.next(), "Beethoven") self.assertEqual(playlist.next(), "Power") self.assertEqual(playlist.next(), "*silence*") ############################## In a unit test with a function, all we need to do is call the function on arguments. But in a unit test with a class, we need to set up the background scene. Here, we need to first create a playlist, and *then* test it out. Also, notice the weirdness of calling playlist.next() multple times, and expecting *different* results. If you have mathematical training, this is particularly weird! It's important to notice this because this means that playlist's next() method can't behave as a mathematical function that only pays attention to its immediate parameters. It will also need to take into account something else, something part of the state of the playlist. We can actually run the tests at this point. Of course, they'll fail, but that's ok. Here is a complete program that we can run: ################################################### import unittest class PlaylistTest(unittest.TestCase): def testSimple(self): playlist = Playlist(["Happy", "Beethoven", "Power"]) self.assertEqual(playlist.next(), "Happy") self.assertEqual(playlist.next(), "Beethoven") self.assertEqual(playlist.next(), "Power") self.assertEqual(playlist.next(), "*silence*") class Playlist(object): def __init__(self, songs): self.songs = songs if __name__ == '__main__': unittest.main() ################################################### When we run this, we'll see an error. ################################################### $ python playlist.py E ====================================================================== ERROR: testSimple (__main__.PlaylistGeneratorTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "playlist.py", line 6, in testSimple self.assertEqual(playlist.next(), "Happy") AttributeError: 'Playlist' object has no attribute 'next' ---------------------------------------------------------------------- Ran 1 test in 0.000s FAILED (errors=1) ################################################### And in this case, we are overjoyed to see errors. The tests are telling us that we haven't implemented a next() method. For the sake of keeping this message short, let's write a quick and dirty implementation of one: ################################################### ## Revised implementation of a Playlist: class Playlist(object): def __init__(self, songs): self.songs = songs def next(self): if not self.songs: return '*silence*' nextSong, self.songs = self.songs[0], self.songs[1:] return nextSong ################################################### The next() method now manages the state of the playlist: it bumps off the top of the list to be returned to the caller. If we rerun the tests at this case, the test case will be happy. To summarize: when we're writing tests on functions, all we need to do is express the expected value of the function with respect to the parameters we're passing in. Input-output pairs. But when we're dealing with objects, the concept of "parameter" with regards to the test is a bit larger in scope: we now need to do some set-up. We need to construct the object as well in our test case. That object will likely manage its own values. If you have questions, please feel free to ask. From alan.gauld at btinternet.com Mon Mar 2 19:00:30 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 02 Mar 2015 18:00:30 +0000 Subject: [Tutor] What exactly is "state"? In-Reply-To: <54F48F10.907@virginmedia.com> References: <54F48F10.907@virginmedia.com> Message-ID: On 02/03/15 16:25, Sydney Shall wrote: > I am not clear precisely what is meant by state. In its > common meaning I can see some relevance. But is there a technical aspect > to the notion. Yes, although it is related to the normal vernacular meaning. There is a whole area of math/engineering dedicated to the study of state and in particular finite state machines and automata. Finite State Machine on Wikipedia will tell you more about that than you want to know :-) Trying to simplify it to the level of ordinary mortals, state simply means a specific set of data attribute values, which affect the outcome of the operations of a system. A very simple state might be a button which can be ON or OFF. That's not very interesting. But suppose we have a more complex circuit model with an upstairs and downstairs switch controlling a stairway light. Now there are two boolean variables and whether the light is on or off depends on the state of both switches Top Bottom Light UP UP OFF UP DOWN ON DOWN UP ON DOWN DOWN OFF So the state of the light depends on the state of the two switches. You can't tell what effect moving the Top switch to the UP position will have on the light unless you already know either the current state of the light or the state of the Bottom switch. We can very easily model that as a class: class StairLight: def __init__(self, sw1=True,sw2=True): self.sw1 = sw1 self.sw2 = sw2 self._setLight() def _setLight(self): self.light = self.sw1 != self.sw2 def __str__(self): if self.light: return 'light is ON' else: return 'light is OFF' def toggle1(self): self.sw1 = not self.sw1 self._setLight() def toggle2(self): self.sw2 = not self.sw2 self._setLight() SL = StairLight() print(SL) # -> OFF SL.toggle1() print(SL) # -> ON SL.toggle1() print(SL) # -> OFF SL.toggle2() print(SL) # -> ON SL.toggle2() print(SL) # -> OFF Of course the state values don't need to be boolean, they can be any type. And they can have many valid values. Consider the state of a order with several order lines. It can be initiated, costed, approved, packaged, shipped, delivered, paid, completed, archived. That's the happy path, but then consider the extra states needed for the 'unhappy paths' - awaiting stock, cancelled, rejected, partial delivery, returned, faulty, and so on. To simplify things we usually create an explicit state attribute which is updated after each operation on the order to reflect the current status. There are many ways to address these kinds of problems, and mathematical models that can be applied to simplify them (in many ways similar to the techniques for simplifying boolean algebra). In practice they boil down to either a state model where each state is modelled as a class or a state table where a large table contains a row for each possible state and a column for each possible event. The cells contain an action and the action returns the next state. The huge advantage of using state machines like this is that they are entirely predictable in behaviour and can be modelled and tested exhaustively. This means they are ideal for safety critical systems and the like. Most air/rail/traffic control systems, telecomms network switches, and equipment control software (cameras, iPods, phones etc) are built using state machines. Specialist CASE tools are available (for many $$$$!) that will generate the code from a state model (often graphical). They also allow the user to run simulations etc to see the effects, perform performance tests etc all without writing a line of actual code. It's one of the main reasons that aircraft don't often crash due to software faults even though your web browser probably falls over almost every day! One formal language for developing complex designs based on this approach is SDL (not the graphics toolset). There is an opensource CASE tool for SDL written in Python and Qt, called openGeode: http://taste.tuxfamily.org/wiki/index.php?title=Technical_topic:_OpenGEODE,_an_SDL_editor_for_TASTE I spent many years of my life building systems using SDL, C and Motorola 68000 assembler.... Oh, and finally. Another big win for state machines is that they can oftewn be translated from software into hardware for the ultimate in performance tweaking! :-) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From davea at davea.name Mon Mar 2 19:03:46 2015 From: davea at davea.name (Dave Angel) Date: Mon, 02 Mar 2015 13:03:46 -0500 Subject: [Tutor] What exactly is "state"? In-Reply-To: <54F48F10.907@virginmedia.com> References: <54F48F10.907@virginmedia.com> Message-ID: <54F4A602.3080706@davea.name> On 03/02/2015 11:25 AM, Sydney Shall wrote: > I am a beginner and I am now at the strage of learning to write unittests. > I have followed the current discussion entitled "How to test a class in > pyhton", and I am not clear precisely what is meant by state. In its > common meaning I can see some relevance. But is there a technical aspect > to the notion. I see it mentioned often and feel rather uncomfortable > that I know so little about it. > I have deliberately started a new thread. > Thanks. When I started composing this, there were no other replies. Sorry for any duplication caused by that. Starting with a dictionary definition: http://www.merriam-webster.com/dictionary/state "the overall physical condition of something : the ability of something to be used, enjoyed, etc." Others: "The particular condition that someone or something is in at a specific time" "In computer science and automata theory, the state of a digital logic circuit or computer program is a technical term for all the stored information, at a given instant in time, to which the circuit or program has access." That last comes the closest to what I'd like to explain. For a given fragment of executing code, the state includes all local variables, all parameters, all closures, all visible globals (ie the ones that *could* be visible to the code. It also includes indirectly the values of all environment variables, lots of system information like the current directory, the time, the network IP address. It also includes the current phase of the moon, the astrological sign of the current president of France, and the number of specs of sand on the eastern shore of a certain Martian lake. -- DaveA From s.shall at virginmedia.com Mon Mar 2 19:42:56 2015 From: s.shall at virginmedia.com (Sydney Shall) Date: Mon, 02 Mar 2015 18:42:56 +0000 Subject: [Tutor] What exactly is "state"? In-Reply-To: <54F4A602.3080706@davea.name> References: <54F48F10.907@virginmedia.com> <54F4A602.3080706@davea.name> Message-ID: <54F4AF30.8070206@virginmedia.com> On 02/03/2015 18:03, Dave Angel wrote: > On 03/02/2015 11:25 AM, Sydney Shall wrote: >> I am a beginner and I am now at the strage of learning to write >> unittests. >> I have followed the current discussion entitled "How to test a class in >> pyhton", and I am not clear precisely what is meant by state. In its >> common meaning I can see some relevance. But is there a technical aspect >> to the notion. I see it mentioned often and feel rather uncomfortable >> that I know so little about it. >> I have deliberately started a new thread. >> Thanks. > > When I started composing this, there were no other replies. Sorry for > any duplication caused by that. > > Starting with a dictionary definition: > > http://www.merriam-webster.com/dictionary/state > "the overall physical condition of something : the ability of something > to be used, enjoyed, etc." > > Others: > > > "The particular condition that someone or something is in at a specific > time" > > "In computer science and automata theory, the state of a digital logic > circuit or computer program is a technical term for all the stored > information, at a given instant in time, to which the circuit or program > has access." > > That last comes the closest to what I'd like to explain. > > For a given fragment of executing code, the state includes all local > variables, all parameters, all closures, all visible globals (ie the > ones that *could* be visible to the code. It also includes indirectly > the values of all environment variables, lots of system information like > the current directory, the time, the network IP address. It also > includes the current phase of the moon, the astrological sign of the > current president of France, and the number of specs of sand on the > eastern shore of a certain Martian lake. Thank you very much, Joel, Danny, Alan and Dave. Your explanations are all very clear and very enlightening. I shall have to change several of my unittests now. In good time. I am particularly pleased with the examples; they clarify matters considerably for me. Out of subject, I wonder from this exchange whether teaching should not always involve at least several teachers. Your replies are very complimentary! -- Sydney From davea at davea.name Mon Mar 2 20:07:16 2015 From: davea at davea.name (Dave Angel) Date: Mon, 02 Mar 2015 14:07:16 -0500 Subject: [Tutor] What exactly is "state"? In-Reply-To: <54F4AF30.8070206@virginmedia.com> References: <54F48F10.907@virginmedia.com> <54F4A602.3080706@davea.name> <54F4AF30.8070206@virginmedia.com> Message-ID: <54F4B4E4.6040609@davea.name> On 03/02/2015 01:42 PM, Sydney Shall wrote: > Thank you very much, Joel, Danny, Alan and Dave. > Your explanations are all very clear and very enlightening. > I shall have to change several of my unittests now. In good time. > I am particularly pleased with the examples; they clarify matters > considerably for me. > > Out of subject, I wonder from this exchange whether teaching should not > always involve at least several teachers. Your replies are very > complimentary! > Your reply was very complimentary, our replies were complementary. Notice the e instead of the i. Yes, multiple teachers is frequently useful. When I was in college, many classes had both a professor (lecturer) and assistant professors (or teaching assistants, or ...). One lecture in a big hall, one relatively small class where you could more easily interact. I found the two different viewpoints useful, though sometimes the leaders of the small class were almost afraid to challenge the full prof. I also advocated a dual-manager role in one company. Idea didn't formally go anywhere, but I did manage to escape from management -- went from 50 people to 1, and a promotion at the same time. -- DaveA From dyoo at hashcollision.org Mon Mar 2 20:07:15 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 2 Mar 2015 11:07:15 -0800 Subject: [Tutor] Use python to parse the subject line of emails, listen for and react to commands In-Reply-To: References: Message-ID: > The code is not pretty and I have no doubt that there are thousands of ways for it to be hacked or messed up since there is little to no error checking in it. I don?t know enough to do so yet sorry I am doing the best I can and have only started learning python a week ago. Yikes! My apologies for my alarmed tone. Panic is not a good way to get people to think clearly. I may have to rethink the way that I personally warn people away from eval(). The way that I've been doing it now has bad side effects in terms of sounding like personal criticism. :( That is, the technical content story might be getting overshadowed by the personal criticism story if I'm not careful. I will think about this more. As a brief comment about your program: execfile might be ok. I haven't taken a close look at your program yet. If I have more time, I'll take a closer look, and hopefully others on the mailing list can also contribute. Do make sure you get eyes to look at your program; getting security right is hard, and I think it takes a village to learn how to program well. Good luck to you! From dyoo at hashcollision.org Tue Mar 3 00:19:18 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 2 Mar 2015 15:19:18 -0800 Subject: [Tutor] Use python to parse the subject line of emails, listen for and react to commands In-Reply-To: References: Message-ID: Hi Willie, Ok, spent a few minutes looking over the code. I'll spend a brief moment reviewing the following block: > msg = email.message_from_string(data[0][1]) > decode = email.header.decode_header(msg['Subject'])[0] > subject = unicode(decode[0]) > print '%s' % (subject) > COMMAND_FILE.write('%s' % (subject)+'.py') > COMMAND_FILE.close() > EX = '%s' %(subject)+'.py' #save the subject as an name.py > execfile (EX) # exe name as a py 1. Do you have a complete list of all the command files you're planning to use? 2. The use of string formatting here might be redundant. Instead of: print '%s' % (subject) since we know subject is a unicode string, can we just say: print subject 3. Similar comment on the line: COMMAND_FILE.write('%s' % (subject)+'.py') but somewhat different. If you're doing string formatting, be consistent: do it so we can see the shape of the final string by looking at the string literal alone. Here, the concatenation of '.py' is apart from the string literal. It should be together. COMMAND_FILE.write('%s.py' % (subject)) My comment here, though, might be overridden by the recommendation to avoid use of unrestricted execfile in favor of a closed approach. 4. If you have a closed set of command names, then you can do something significantly safer here. Concretely, let's say that you have the following three commands: "open", "reply", "ping" I'm just making these up so that we have something concrete to talk about. Let us assume that we have, corresponding to these command names, the three modules: "open_command.py", "reply_command.py" and "ping_command.py". But unlike what you probably have now, let's say that each of these modules defines a run() function. ##################### ## open.py def run(): print "This is open." ## reply.py def run(): print "This is reply." ## ping.py def run(): print "This is ping." ##################### If we have this organization, then in your main command driver, you can safely construct a mapping from command name to the appropriate run() function: ######################################################## import open_command import reply_command import ping_command COMMANDS = { u'open' : open_command.run, u'reply' : reply_command.run, u'ping' : ping_command.run, } # ... later in your program ... def process_mailbox(M): # ... msg = email.message_from_string(data[0][1]) decode = email.header.decode_header(msg['Subject'])[0] subject = unicode(decode[0]) ## here's where things are slightly different: command = COMMANDS[subject] command() ######################################################### There are a few things to note there: 1. This approach is much safer because we know that the only commands that can be run are listed in COMMANDS. It's a lot more constrained in what it can do. 2. It's also robust in the face of changes to the current directory. If your current directory is somewhere where you can't write, that has no effect whatsover with this revision. The original approach can suffer from mysterious failure possibilities because it implicitly depends on the "global" current working directory. 3. Other folks will better understand what's going on: this command-dispatching pattern is used quite often. For example, see: https://docs.python.org/2/faq/design.html#why-isn-t-there-a-switch-or-case-statement-in-python There are also some guaranteed low-level efficiency bonuses if we take this approach. But those are ancillary when we compare vs the safety guarantees we'll get if we avoid exec or execfile approaches. If you have questions about this, please ask, and hopefully someone here can help explain further and point to resources. Good luck! From alan.gauld at btinternet.com Tue Mar 3 02:33:57 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 03 Mar 2015 01:33:57 +0000 Subject: [Tutor] text file help In-Reply-To: References: <54E5D53F.6050308@btinternet.com> Message-ID: <54F50F85.6070509@btinternet.com> Forwarding to list too. On 02/03/15 19:04, Tihomir Zjajic wrote: > kl_number = [] > myfile = open("formular_doznake.txt") > for line in "formular_doznake": Note that you want to iterate over the file not the name of the file. The code above sets line to each character in the file name. You want to set it to each line in the file so it should look like: myfile = open("formular_doznake.txt") for line in myfile: or just for line in open("formular_doznake.txt") : > if line.startswith('1'): > num = makeNumber(next[vrs_drv], next[prs_prec], next[teh_kl]) > kl_number.append(num) > > def makeNumber(l1,l2,l3): > nums = [] > for line in(vrs_drv,prs_prec,teh_kl): > nums.append(line.rstrip().split()[-1]) > return ".join(nums)" > print(next(myfile)+next(myfile)+next(myfile)) > print(kl_number) > sorry, but this doesn't work ! Always try to be specific. Say what doesn't work about it and include any error messages. The hint above may help however. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From phil_lor at bigpond.com Tue Mar 3 07:50:41 2015 From: phil_lor at bigpond.com (Phil) Date: Tue, 03 Mar 2015 16:50:41 +1000 Subject: [Tutor] List of ints Message-ID: <54F559C1.40102@bigpond.com> Thank you for reading this. Python 3 under Linux. I'd like to set up a two dimensional list of counters as follows; count = [ [0], [0], [0] ] And then increment the first counter as follows; count [0] += 1 This fails with the following error; TypeError: 'int' object is not iterable The array module looks like the answer because it seems to function in the same way as an array under C. However, it seems to me that I should be able to do the same thing with a list. Is there a way to add a value to a list of ints? -- Regards, Phil From breamoreboy at yahoo.co.uk Tue Mar 3 08:46:26 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 03 Mar 2015 07:46:26 +0000 Subject: [Tutor] List of ints In-Reply-To: <54F559C1.40102@bigpond.com> References: <54F559C1.40102@bigpond.com> Message-ID: On 03/03/2015 06:50, Phil wrote: > Thank you for reading this. > Python 3 under Linux. > > I'd like to set up a two dimensional list of counters as follows; > > count = [ > [0], > [0], > [0] > ] > > And then increment the first counter as follows; > > count [0] += 1 > > This fails with the following error; > > TypeError: 'int' object is not iterable > > The array module looks like the answer because it seems to function in > the same way as an array under C. However, it seems to me that I should > be able to do the same thing with a list. > > Is there a way to add a value to a list of ints? > You are trying to increment the first element of count which is itself a list containing one element. You actually need:- count[0][0] +=1 -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From alan.gauld at btinternet.com Tue Mar 3 10:34:45 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 03 Mar 2015 09:34:45 +0000 Subject: [Tutor] List of ints In-Reply-To: <54F559C1.40102@bigpond.com> References: <54F559C1.40102@bigpond.com> Message-ID: On 03/03/15 06:50, Phil wrote: > I'd like to set up a two dimensional list of counters as follows; > > count = [ [0], [0], [0] ] > > And then increment the first counter as follows; > > count [0] += 1 Are you trying to increment the zero to make it 1? Or are you trying to add a new value, 1, to the first sublist? ie Do you want the output to be: count = [ [1], [0], [0] ] OR count = [ [0,1], [0], [0] ] To do the first you need to increment the *value* of the first sublist not the sublist: count[0][0] += 1 To do the second you must use append(): count[0].append(1) > The array module looks like the answer because it seems to function in > the same way as an array under C. The array module is pretty specialized, in most cases a simple list is a better solution. > Is there a way to add a value to a list of ints? Yes, see above. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Tue Mar 3 10:38:05 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 3 Mar 2015 01:38:05 -0800 Subject: [Tutor] List of ints In-Reply-To: <54F559C1.40102@bigpond.com> References: <54F559C1.40102@bigpond.com> Message-ID: On Mon, Mar 2, 2015 at 10:50 PM, Phil wrote: > Thank you for reading this. > Python 3 under Linux. > > I'd like to set up a two dimensional list of counters as follows; > > count = [ > [0], > [0], > [0] > ] > Can you explain why the list is two-dimensional? It's not quite clear why. Do you have a particular use case in mind? > Is there a way to add a value to a list of ints? Can you give an example of what you'd like to see? Unfortunately, the word "add" is too ambiguous to know what the expectations are. If you can disambiguate with concrete examples, that may help us. Good luck! From chelseysp at yahoo.com Tue Mar 3 07:22:49 2015 From: chelseysp at yahoo.com (chelseysp at yahoo.com) Date: Tue, 3 Mar 2015 06:22:49 +0000 Subject: [Tutor] (no subject) Message-ID: <29241.38109.bm@smtp203.mail.bf1.yahoo.com> don't understand why these execute different things? total=total+10 >>> total=total+25 >>> total=total+25 >>> average=total/3 >>> total 110 >>> average 36.666666666666664 and total=0 >>> total=total+10 >>> total=total+25 >>> total=total+25 >>> average=total/3 >>> average 20.0 Sent from Windows Mail From marcos.al.azevedo at gmail.com Tue Mar 3 07:25:44 2015 From: marcos.al.azevedo at gmail.com (Marcos Almeida Azevedo) Date: Tue, 3 Mar 2015 14:25:44 +0800 Subject: [Tutor] How to test a class in python? In-Reply-To: References: Message-ID: On Mon, Mar 2, 2015 at 12:41 PM, Danny Yoo wrote: > On Sun, Mar 1, 2015 at 11:41 AM, Alan Gauld > wrote: > > On 01/03/15 16:19, Fatimah Taghdi wrote: > >> > >> Hello I was wondering how to test a class in python is it the same way > as > >> testing a function ? > > > Depending on the design of the class, there might be a little bit more > set-up involved. But the ideas are similar. > > > In a test case for a function, we have a few things in hand: > > 1. The function being tested. > 2. The parameters we're going to pass as inputs to that function. > 3. The expected output we want to see if the function is behaving > properly. > > > In a test case for a class, we have a few more things in hand. > > 1. The class being tested. > 2. The parameters we use to create the class instance. > 3. The particular method of the class that we're testing. > 4. The parameters we're going to pass as inputs to that method. > 5. The expected output we want to see if the method is behaving > properly. > > So you can see that the core concept for testing is the same: we > express the things that are going to change, and we also say what the > expected result is supposed to be. It just so happens that classes > can have a lot more state, and this can contribute to a few extra > steps to do the test set up.. > I suggest PyUnit for easier testing. Here is a very good guide: http://pyunit.sourceforge.net/pyunit.html > > > Do you have a particular class in mind? > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Marcos | I love PHP, Linux, and Java From dyoo at hashcollision.org Tue Mar 3 10:45:13 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 3 Mar 2015 01:45:13 -0800 Subject: [Tutor] (no subject) In-Reply-To: <29241.38109.bm@smtp203.mail.bf1.yahoo.com> References: <29241.38109.bm@smtp203.mail.bf1.yahoo.com> Message-ID: On Mon, Mar 2, 2015 at 10:22 PM, wrote: > > don't understand why these execute different things? > > > > > total=total+10 >>>> total=total+25 >>>> total=total+25 >>>> average=total/3 >>>> total > 110 >>>> average > 36.666666666666664 In your first case, what's the value of 'total' *before* all the things you've typed in? Can you say more why you expected to see the same result here as in the next program? They do look textually different, so I'd expect them to likely have a different meaning, so I'm confused as to what your expectations are. Say more about why you expected them to be the same, and maybe we can understand better. > total=0 >>>> total=total+10 >>>> total=total+25 >>>> total=total+25 >>>> average=total/3 >>>> average > 20.0 Your second case makes sense. 10 + 25 + 25 is 60, and if we divide 60 by 3, the we'd expect the average to be twenty. From fomcl at yahoo.com Tue Mar 3 11:31:01 2015 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Tue, 3 Mar 2015 02:31:01 -0800 Subject: [Tutor] codecs.open vs io.open Message-ID: <1425378661.13766.YahooMailBasic@web163806.mail.gq1.yahoo.com> Hi, Is there in a (use case) difference between codecs.open and io.open? What is the difference? A small difference that I just discovered is that codecs.open(somefile).read() returns a bytestring if no encoding is specified, but a unicode string if an encoding is specified. io.open always returns a unicode string. Thank you! 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 breamoreboy at yahoo.co.uk Tue Mar 3 17:05:57 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 03 Mar 2015 16:05:57 +0000 Subject: [Tutor] How to test a class in python? In-Reply-To: References: Message-ID: On 03/03/2015 06:25, Marcos Almeida Azevedo wrote: > On Mon, Mar 2, 2015 at 12:41 PM, Danny Yoo wrote: > >> On Sun, Mar 1, 2015 at 11:41 AM, Alan Gauld >> wrote: >>> On 01/03/15 16:19, Fatimah Taghdi wrote: >>>> >>>> Hello I was wondering how to test a class in python is it the same way >> as >>>> testing a function ? >> >> >> Depending on the design of the class, there might be a little bit more >> set-up involved. But the ideas are similar. >> >> >> In a test case for a function, we have a few things in hand: >> >> 1. The function being tested. >> 2. The parameters we're going to pass as inputs to that function. >> 3. The expected output we want to see if the function is behaving >> properly. >> >> >> In a test case for a class, we have a few more things in hand. >> >> 1. The class being tested. >> 2. The parameters we use to create the class instance. >> 3. The particular method of the class that we're testing. >> 4. The parameters we're going to pass as inputs to that method. >> 5. The expected output we want to see if the method is behaving >> properly. >> >> So you can see that the core concept for testing is the same: we >> express the things that are going to change, and we also say what the >> expected result is supposed to be. It just so happens that classes >> can have a lot more state, and this can contribute to a few extra >> steps to do the test set up.. >> > > I suggest PyUnit for easier testing. > Here is a very good guide: http://pyunit.sourceforge.net/pyunit.html > Quoting from the link "PyUnit forms a part of the Python Standard Library as of Python version 2.1." so for the latest version see https://docs.python.org/3/library/unittest.html -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From breamoreboy at yahoo.co.uk Tue Mar 3 17:08:19 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 03 Mar 2015 16:08:19 +0000 Subject: [Tutor] codecs.open vs io.open In-Reply-To: <1425378661.13766.YahooMailBasic@web163806.mail.gq1.yahoo.com> References: <1425378661.13766.YahooMailBasic@web163806.mail.gq1.yahoo.com> Message-ID: On 03/03/2015 10:31, Albert-Jan Roskam wrote: > Hi, > > Is there in a (use case) difference between codecs.open and io.open? What is the difference? > A small difference that I just discovered is that codecs.open(somefile).read() returns a bytestring if no encoding is specified, but a unicode string if an encoding is specified. io.open always returns a unicode string. > > Thank you! > > Regards, > > Albert-Jan > You might get an answer here if you're lucky, but I believe this is more python-list territory. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From alan.gauld at btinternet.com Tue Mar 3 21:48:36 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 03 Mar 2015 20:48:36 +0000 Subject: [Tutor] text file help In-Reply-To: References: <54E5D53F.6050308@btinternet.com> <54F50F85.6070509@btinternet.com> Message-ID: <54F61E24.5070107@btinternet.com> On 03/03/15 12:56, Tihomir Zjajic wrote: > kl_number = [] > myfile = open("formular_doznake.txt") > for line in myfile: > if line.startswith('1'): > num = makeNumber(next[vrs_drv], next[prs_prec], next[teh_kl]) > kl_number.append(num) > > def makeNumber(l1,l2,l3): > nums = [] > for line in(vrs_drv,prs_prec,teh_kl): > nums.append(line.rstrip().split()[-1]) > return ".join(nums)" > print(next(myfile) + next(myfile) + next(myfile)) > print(kl_number) Do you have any idea what these two lines do? If not you need to go and read about next() and iterators. It is obviously never going to work to try to read the next line from a file that has already been processed. I don't know what you think the lines will do but they obviously throw an error. Its like trying to climb a ladder when you ae already at the top. I don't know what your logic is here, only you can answer that. But you will get better responses asking the whole iist not just me. I'm only one pair of eyes. The list has dozens. > Traceback (most recent call last): > File "C:\Python31\pro.py", line 13, in > print(next(myfile) + next(myfile) + next(myfile)) > StopIteration Alan G. From dyoo at hashcollision.org Tue Mar 3 23:10:51 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 3 Mar 2015 14:10:51 -0800 Subject: [Tutor] (no subject) In-Reply-To: <765268.30657.bm@smtp235.mail.bf1.yahoo.com> References: <29241.38109.bm@smtp203.mail.bf1.yahoo.com> <765268.30657.bm@smtp235.mail.bf1.yahoo.com> Message-ID: On Mar 3, 2015 1:49 PM, wrote: > > i expected them to be the same because 10 plus 25 plus 25 divided by 3 is 60 and they both say that but the one that that says total=0. im just not sure how that changes the equation > Please use "reply to all" in your email client. You might be getting confused because your mental model is of a formula and mathematics. In Python, unfortunately that's not what's happening. Instead, in a python program, a computation is a sequence of actions that changes the world. For example, total=total+10 In a Python program, this has the physical form of a mathematical equation , but it's not! It means the following sequence of steps: "1. Load the current value of the 'total' variable, 2. add ten to that value, and 3. store the result into the 'total' variable". For any of that to work, 'total' must already have some prior numeric value that you haven't shown us yet. Math variables don't directly have a notion of "prior" or change over time. But Python variables do. Note how different the meaning of the statement is from an equation of algebra. You have to be very careful because the notation used in certain programming languages looks superficially like math. Here, the notation is borrowed, but the meanings are different: programmers have gotten used to the different meaning of symbols like '='. There *are* good programming curricula where the programming language behaves like algebra (see http://bootstrapworld.org for example). But unfortunately it's not what you'll be doing in Python. > Sent from Windows Mail > > From: Danny Yoo > Sent: ?Tuesday?, ?March? ?3?, ?2015 ?3?:?45? ?AM > To: chelseysp at yahoo.com.dmarc.invalid > Cc: Tutor at python.org > > On Mon, Mar 2, 2015 at 10:22 PM, wrote: > > > > don't understand why these execute different things? > > > > > > > > > > total=total+10 > >>>> total=total+25 > >>>> total=total+25 > >>>> average=total/3 > >>>> total > > 110 > >>>> average > > 36.666666666666664 > > > In your first case, what's the value of 'total' *before* all the > things you've typed in? > > Can you say more why you expected to see the same result here as in > the next program? They do look textually different, so I'd expect > them to likely have a different meaning, so I'm confused as to what > your expectations are. Say more about why you expected them to be the > same, and maybe we can understand better. > > > > > > > total=0 > >>>> total=total+10 > >>>> total=total+25 > >>>> total=total+25 > >>>> average=total/3 > >>>> average > > 20.0 > > Your second case makes sense. 10 + 25 + 25 is 60, and if we divide 60 > by 3, the we'd expect the average to be twenty. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From phil_lor at bigpond.com Wed Mar 4 00:09:03 2015 From: phil_lor at bigpond.com (Phil) Date: Wed, 04 Mar 2015 09:09:03 +1000 Subject: [Tutor] List of ints In-Reply-To: References: <54F559C1.40102@bigpond.com> Message-ID: <54F63F0F.6080706@bigpond.com> On 03/03/15 17:46, Mark Lawrence wrote: > > You are trying to increment the first element of count which is itself a > list containing one element. You actually need:- > > count[0][0] +=1 > Thank you Lawrence, Alan, and Danny, The solution is embarrassingly obvious. It's been a long time since I've attempted any programming and I'd even forgotten that I needed a nested loop to access the cells in a two-dimensional array, or list. In this case I didn't need a two-dimensional array anyway. I'd been away from home for five weeks and during a quiet period I installed QPython on my tablet with the aim of porting a programme that I'd written in C++ 15 years ago to Python. Cutting and pasting and even moving around the IDE turned out to be a truly frustrating exercise. I wonder if it was just my clumsiness or if others have had the same experience? -- Regards, Phil From breamoreboy at yahoo.co.uk Wed Mar 4 00:44:16 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Tue, 03 Mar 2015 23:44:16 +0000 Subject: [Tutor] List of ints In-Reply-To: <54F63F0F.6080706@bigpond.com> References: <54F559C1.40102@bigpond.com> <54F63F0F.6080706@bigpond.com> Message-ID: On 03/03/2015 23:09, Phil wrote: > On 03/03/15 17:46, Mark Lawrence wrote: >> >> You are trying to increment the first element of count which is itself a >> list containing one element. You actually need:- >> >> count[0][0] +=1 >> > > Thank you Lawrence, Alan, and Danny, > > The solution is embarrassingly obvious. It's been a long time since I've > attempted any programming and I'd even forgotten that I needed a nested > loop to access the cells in a two-dimensional array, or list. In this > case I didn't need a two-dimensional array anyway. > > I'd been away from home for five weeks and during a quiet period I > installed QPython on my tablet with the aim of porting a programme that > I'd written in C++ 15 years ago to Python. Cutting and pasting and even > moving around the IDE turned out to be a truly frustrating exercise. > > I wonder if it was just my clumsiness or if others have had the same > experience? > Having never heard of QPython I've just looked it up, so for those who don't know from http://qpython.com/ it's "a script engine which runs Python programs on android devices". I doubt if there is much experience on this list with it although you might get lucky. I am aware though of http://bugs.python.org/issue23496 "Steps for Android Native Build of Python 3.4.2" which may be of interest. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From steve at pearwood.info Wed Mar 4 01:21:46 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 4 Mar 2015 11:21:46 +1100 Subject: [Tutor] List of ints In-Reply-To: <54F63F0F.6080706@bigpond.com> References: <54F559C1.40102@bigpond.com> <54F63F0F.6080706@bigpond.com> Message-ID: <20150304002145.GH7655@ando.pearwood.info> On Wed, Mar 04, 2015 at 09:09:03AM +1000, Phil wrote: > I'd been away from home for five weeks and during a quiet period I > installed QPython on my tablet with the aim of porting a programme that > I'd written in C++ 15 years ago to Python. Cutting and pasting and even > moving around the IDE turned out to be a truly frustrating exercise. I don't actually know QPython, but in general, using a tablet or a smart phone is only acceptable for the most trivial actions. Fine if you're taping out a 15 character tweet with one finger, not so useful if you want to get real work done. -- Steve From steve at pearwood.info Wed Mar 4 01:25:38 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 4 Mar 2015 11:25:38 +1100 Subject: [Tutor] List of ints In-Reply-To: <54F559C1.40102@bigpond.com> References: <54F559C1.40102@bigpond.com> Message-ID: <20150304002538.GI7655@ando.pearwood.info> On Tue, Mar 03, 2015 at 04:50:41PM +1000, Phil wrote: > count [0] += 1 > > This fails with the following error; > > TypeError: 'int' object is not iterable I know that others have already solved the problem, but here is something which might help you solve similar problems in the future. The way to debug simple things like this is quite simple: print count[0] which will show you that count[0] is a list [0], not an int 0, and you are trying to add [0]+1 which doesn't work. Never under-estimate the usefulness of a few print calls when debugging. -- Steve From alan.gauld at btinternet.com Wed Mar 4 09:03:12 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 04 Mar 2015 08:03:12 +0000 Subject: [Tutor] List of ints In-Reply-To: References: <54F559C1.40102@bigpond.com> <54F63F0F.6080706@bigpond.com> Message-ID: On 03/03/15 23:44, Mark Lawrence wrote: > Having never heard of QPython I've just looked it up, so for those who > don't know from http://qpython.com/ it's "a script engine which runs > Python programs on android devices". I doubt if there is much > experience on this list with it although you might get lucky. I have QPython on my Android phone. It's not much good for development but its ok for running a few basic Python scripts. Obviously GUIs etc won't work but file processing and the like are fine. But I certainly wouldn't try programming in it! But then I don't do any programming on my phone anyway. On my tablet I use vim as editor and Qpython to run the code but its still not an experience I recommend. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From breamoreboy at yahoo.co.uk Wed Mar 4 10:20:02 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 04 Mar 2015 09:20:02 +0000 Subject: [Tutor] List of ints In-Reply-To: <20150304002538.GI7655@ando.pearwood.info> References: <54F559C1.40102@bigpond.com> <20150304002538.GI7655@ando.pearwood.info> Message-ID: On 04/03/2015 00:25, Steven D'Aprano wrote: > On Tue, Mar 03, 2015 at 04:50:41PM +1000, Phil wrote: > >> count [0] += 1 >> >> This fails with the following error; >> >> TypeError: 'int' object is not iterable > > I know that others have already solved the problem, but here is > something which might help you solve similar problems in the future. > The way to debug simple things like this is quite simple: > > print count[0] > > which will show you that count[0] is a list [0], not an int 0, and you > are trying to add [0]+1 which doesn't work. > > Never under-estimate the usefulness of a few print calls when debugging. > > About time we threw in the use of the interactive interpreter for trying code snippets as well, just for good measure :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From niyanaxx95 at gmail.com Wed Mar 4 16:40:21 2015 From: niyanaxx95 at gmail.com (niyanaxx95 at gmail.com) Date: Wed, 4 Mar 2015 15:40:21 +0000 Subject: [Tutor] =?utf-8?q?Fixed_Vector_Array?= Message-ID: <54f728b3.949ee00a.1038.fffff298@mx.google.com> Need help trying to implement insert, remove, indexof, and reverse functions. I tried to do them but am not sure if it is correct. I am struggling with arrays. This is python and using ezarrays. Assignment: A fixed vector is very similar to the vector in that it is a sequence container that can grow and shrink as needed and include many useful operations. But the fixed vector has a maximum capacity beyond which it can not expand. ? FixedVector(maxSize): Creates an empty fixed vector with the given maximum capacity. ? length(): Returns the number of elements contained in the vector. ? isFull(): Returns a Boolean indicating if the vector is full. ? getitem(index): Returns the element stored in the vector at position index. The value of index must be within the valid range. ? setitem(index, item): Sets the element at position index to contain the given item. The value of index must be within the valid range, which includes one position beyond the end of the vector. In the latter case, the item is simply appended onto the end. ? contains(item): Determines if the given item is contained in the vector. ? toString(): Returns a string representation of the vector. ? append(item): Adds the given item to the end of the vector. An item can not be appended to a full vector. ? clear(): Removes all elements from the vector. ? insert(index, item): Inserts the given item into the vector at position index. The elements at and following the given position are shifted down to make room for the new item. The index must be within the valid range and the vector can not be full. If index is one position beyond the end of the vector, the item is appended onto the vector. ? remove(index): Removes the element at position index from the vector. The removed element is returned. The index must be within the valid range. ? indexOf(item): Returns the index of the element containing the given item. The item must be in the list. ? reverse(): Performs a list reversal by reversing the order of the elements within the vector. My Code: from ezarrays import Array class FixedVector : # Creates a new empty fixed vector with the given maximum capacity. def __init__(self, maxSize) : self._theItems = Array(maxSize) self._numItems = 0 # Returns the number of elements contained in the vector. def __len__(self) : return self._numItems # Returns a Boolean indicating if the vector is full. def isFull(self) : return self._numItems == len(self._theItems) # Returns the element stored in the vector at position index. # The value of index must be within the valid range. def __getitem__(self, index) : assert index >= 0 and index < self._numItems, "Index out of Range." return self._theItems[index] # Sets the element at position index to contain the given item. The # value of index must be within the valid range, which includes one # position beyond the end of the vector. In the latter case, the item # is simply appended onto the end. def __setitem__(self, index, item) : assert index >= 0 and index <= self._numItems, "Index out of range." if index == self._numItems : self.append(item) else : self._theItems[index] = item # Determines if the given item is contained in the vector. def __contains__(self, item) : i = 0 while i < self._numItems : if self._theItems[i] == item : return True i = i + 1 return False # Returns a string representation of the vector. def __repr__(self) : if self._numItems == 0 : return "[]" vectStr = "[" + str(self._theItems[0]) for i in range(1, self._numItems) : vectStr = vectStr + ", " + str(self._theItems[i]) vectStr = vectStr + "]" return vectStr # Adds the given item to the end of the vector. An item can not be # appended to a full vector. def append(self, item) : assert self._numItems < len(self._theItems), \ "Can not add to a full vector." self._theItems[self._numItems] = item self._numItems = self._numItems + 1 # Removes all elements from the vector. def clear(self) : self._theItems.clear(None) # Inserts the given item into the vector at position index. The elements # at and following the given position are shifted down to make room # for the new item. The index must be within the valid range and the # vector can not be full. If index is one position beyond the end of # the vector, the item is appended. def insert(self, index, item) : i = numItems while i > index : self._theItem[i] = self._theItem[i-1] i = i-1 self._theItems[i] = item self._numItems = self._numItems + 1 # Removes the element at position index from the vector. The removed # element is returned. The index must be within the valid range. def remove(self, index) : removed = self._theItems[index] self._theItems[index] = None return removed # Returns the index of the element containing the given item. The item # must be in the list. def indexOf(self, item) : for i in range(0, self._numItems ): if self._theItems[i] == item : return i # Reverses the order of the elements within the vector. def reverse(self) : end = self._numItems -1 for x in range(0, self._numItems // 2) : tmp = self._theItems[x] self._theItems[x] = self._theItems[end] self._theItems = tmp end = end -1 From dyoo at hashcollision.org Wed Mar 4 18:44:51 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 4 Mar 2015 09:44:51 -0800 Subject: [Tutor] Fixed Vector Array In-Reply-To: <54f728b3.949ee00a.1038.fffff298@mx.google.com> References: <54f728b3.949ee00a.1038.fffff298@mx.google.com> Message-ID: On Wed, Mar 4, 2015 at 7:40 AM, wrote: > Need help trying to implement insert, remove, indexof, and reverse functions. > > I tried to do them but am not sure if it is correct. I am struggling with arrays. Hi Niyana, Programmers generally use unit tests to double-check their programs. Do you have any *unit tests* to help you feel more confident which methods are probably ok? From dyoo at hashcollision.org Wed Mar 4 18:51:19 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 4 Mar 2015 09:51:19 -0800 Subject: [Tutor] Fwd: Re: Fixed Vector Array In-Reply-To: <54f74511.5c17370a.5f61.ffff972c@mx.google.com> References: <54f728b3.949ee00a.1038.fffff298@mx.google.com> <54f74511.5c17370a.5f61.ffff972c@mx.google.com> Message-ID: Can someone point out how to write and run tests to Ni'Yana? I'm busy at the moment. Thanks! ---------- Forwarded message ---------- From: Date: Mar 4, 2015 9:46 AM Subject: Re: [Tutor] Fixed Vector Array To: "Danny Yoo" Cc: I am not sure how to test this. I do not understand arrays fully but this a lab assignment that I must turn in. Sent from Windows Mail *From:* Danny Yoo *Sent:* ?Wednesday?, ?March? ?4?, ?2015 ?12?:?44? ?PM *To:* Ni'Yana Morgan *Cc:* tutor at python.org On Wed, Mar 4, 2015 at 7:40 AM, wrote: > Need help trying to implement insert, remove, indexof, and reverse functions. > > I tried to do them but am not sure if it is correct. I am struggling with arrays. Hi Niyana, Programmers generally use unit tests to double-check their programs. Do you have any *unit tests* to help you feel more confident which methods are probably ok? From alan.gauld at btinternet.com Wed Mar 4 19:04:45 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 04 Mar 2015 18:04:45 +0000 Subject: [Tutor] Fixed Vector Array In-Reply-To: <54f728b3.949ee00a.1038.fffff298@mx.google.com> References: <54f728b3.949ee00a.1038.fffff298@mx.google.com> Message-ID: On 04/03/15 15:40, niyanaxx95 at gmail.com wrote: > Need help trying to implement insert, remove, indexof, and reverse functions. > > I tried to do them but am not sure if it is correct. I am struggling with arrays. I;m not sure why so many courses seem to insist on teaching old school arrays using Python. It involves rewriting functions that already exist, in superior form in the standard library. Reinventing the wheel is a bad practice and it seems a shame that some teachers appear to want to teach bad habits! > This is python and using ezarrays. And using third party libraries to do so just compounds the wrong, why not at least base it on a standard list? Caveat I have no idea what this module does so some of my answers may be off track. Anyway, rant over, it's not your fault and I doubt you can influence your course design. > Assignment: > > A fixed vector is very similar to the vector in that > it is a sequence containerthat can grow and shrink > as needed and include many useful operations. Just like a Python list in fact... > But the fixed vector has a maximum capacity beyond which it can not expand. Which is rather limiting and very rarely what you want... > ? FixedVector(maxSize): Creates an empty fixed vector with the given maximum capacity. > ? length(): Returns the number of elements contained in the vector. > ? isFull(): Returns a Boolean indicating if the vector is full. > ? getitem(index): Returns the element stored in the vector at position index. The value of index must be within the valid range. > ? setitem(index, item): Sets the element at position index to contain the given item. The value of index must be within the valid range, which includes one position beyond the end of the vector. In the latter case, the item is simply appended onto the end. ? contains(item): Determines if the given item is contained in the vector. > ? toString(): Returns a string representation of the vector. > ? append(item): Adds the given item to the end of the vector. An item can not be appended to a full vector. > ? clear(): Removes all elements from the vector. > ? insert(index, item): Inserts the given item into the vector at position index. The elements at and following the given position are shifted down to make room for the new item. The index must be within the valid range and the vector can not be full. If index is one position beyond the end of the vector, the item is appended onto the vector. > ? remove(index): Removes the element at position index from the vector. The removed element is returned. The index must be within the valid range. > ? indexOf(item): Returns the index of the element containing the given item. The item must be in the list. > ? reverse(): Performs a list reversal by reversing the order of the elements within the vector. > from ezarrays import Array > > class FixedVector : > # Creates a new empty fixed vector with the given maximum capacity. > def __init__(self, maxSize) : > self._theItems = Array(maxSize) > self._numItems = 0 Presumably the Array created has maxSize items already created? If so of what type? Or is it type agnostic? > # Returns the number of elements contained in the vector. > def __len__(self) : > return self._numItems I'd probably prefer to return the actual number of elements rather than rely on your counter being accurate. is I assume len(self._theItems) will return an accurate result? Of if the Array really is fixed size maybe the correct answer is return maxSize? Or should it only return the number of non-empty items? > # Returns a Boolean indicating if the vector is full. > def isFull(self) : > return self._numItems == len(self._theItems) This suggests that your len() returns the number of non empty cells and len(Array) returns the maxsize value? > # Returns the element stored in the vector at position index. > # The value of index must be within the valid range. > def __getitem__(self, index) : > assert index >= 0 and index < self._numItems, "Index out of Range." > return self._theItems[index] I don;t think an assert is the correct solution here, it wouyld be better to raise an IndexError exception. This would be consistent with what Python does with the built in list type. > # Sets the element at position index to contain the given item. The > # value of index must be within the valid range, which includes one > # position beyond the end of the vector. In the latter case, the item > # is simply appended onto the end. > def __setitem__(self, index, item) : > assert index >= 0 and index <= self._numItems, "Index out of range." Same issue here. > if index == self._numItems : > self.append(item) > else : > self._theItems[index] = item If Array returns a fixed size array can't you just always assign to the index position. In other words does the array need to be filled in a sequential manner or could you have a 'hole' in the middle (one of the few things an array allows that a list doesn't - although you can just use a dictionary if that's really important! > # Determines if the given item is contained in the vector. > def __contains__(self, item) : > i = 0 > while i < self._numItems : > if self._theItems[i] == item : > return True > i = i + 1 > > return False Couldn't this be more clearly written as for thing in self.theItems: if thing == item: return True return False > # Returns a string representation of the vector. > def __repr__(self) : > if self._numItems == 0 : > return "[]" > > vectStr = "[" + str(self._theItems[0]) > for i in range(1, self._numItems) : > vectStr = vectStr + ", " + str(self._theItems[i]) > > vectStr = vectStr + "]" > return vectStr As a matter of interest what does str(self._theItems) return? > # Adds the given item to the end of the vector. An item can not be > # appended to a full vector. > def append(self, item) : > assert self._numItems < len(self._theItems), \ > "Can not add to a full vector." > self._theItems[self._numItems] = item > self._numItems = self._numItems + 1 I'm not sure this makes sense for a *fixed* array, but its in the assignment so... I'm not sure what the vectStr thing does. A feature of ezarray presumably? > # Removes all elements from the vector. > def clear(self) : > self._theItems.clear(None) Shouldn't you also reset self_numItems? > # Inserts the given item into the vector at position index. The elements > # at and following the given position are shifted down to make room > # for the new item. The index must be within the valid range and the > # vector can not be full. If index is one position beyond the end of > # the vector, the item is appended. > def insert(self, index, item) : > i = numItems I guess that should be i = self._numItems > while i > index : > self._theItem[i] = self._theItem[i-1] And that should be self._the items - ie plural > i = i-1 > self._theItems[i] = item > self._numItems = self._numItems + 1 What happens if index >= numItems? Won't you wind up with an index error from your setitems code earlier?? > # Removes the element at position index from the vector. The removed > # element is returned. The index must be within the valid range. > def remove(self, index) : > removed = self._theItems[index] > self._theItems[index] = None > return removed This is relying on the earlier getitem/setitem error checking of index I assume? > # Returns the index of the element containing the given item. The item > # must be in the list. > def indexOf(self, item) : > for i in range(0, self._numItems ): > if self._theItems[i] == item : > return i > > > # Reverses the order of the elements within the vector. > def reverse(self) : > end = self._numItems -1 > for x in range(0, self._numItems // 2) : > tmp = self._theItems[x] > self._theItems[x] = self._theItems[end] > self._theItems = tmp > end = end -1 Shouldn't that last line be self._theItems[end] = tmp? But in Python we can swap without using a tmp: items = self._theItems for x in range(len(items)//2): items[x],items[end] = items[end],items[x] end -= 1 HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From robertvstepp at gmail.com Wed Mar 4 20:10:11 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 4 Mar 2015 13:10:11 -0600 Subject: [Tutor] Strengths & weaknesses of Python lists compared to "old school" arrays [Was "Fixed Vector Array"] Message-ID: On Wed, Mar 4, 2015 at 12:04 PM, Alan Gauld wrote: > On 04/03/15 15:40, niyanaxx95 at gmail.com wrote: >> >> Need help trying to implement insert, remove, indexof, and reverse >> functions. >> >> I tried to do them but am not sure if it is correct. I am struggling with >> arrays. > > > I;m not sure why so many courses seem to insist on teaching old school > arrays using Python. It involves rewriting functions that already exist, in > superior form in the standard library. Reinventing the wheel is a bad > practice and it seems a shame that some teachers appear to want to teach bad > habits! Alan's comments here have got me to wondering. I originally first learned of programming via FORTRAN and BASIC (various incarnations). These used multidimensional array-based thinking. Of course, these were (then) primarily used to process numerical data. And that is what I used them for. I don't recollect anything similar to Python lists in those languages way back then. In doing physics-related programming it was not unusual to have a need for 3- or 4-dimensional arrays, and sometimes more. OTH, Python lists are *very* flexible in how they can be implemented and especially in what can be stored (objects they point to) in them. So I thought I would ask the question: What are the strengths and weaknesses of Python lists compared to "old school" arrays? 1) If the data involved is strictly numerical, does this alter your answer? Especially if multidimensional matrix calculations are involved? 2) If either numerical or string data is allowed to be stored, does this change your answer? 3) Have "old school" arrays evolved into something more flexible in most programming languages? 4) Are there other clarifying questions I should be asking on this topic that are not occurring to me? [...] > If Array returns a fixed size array can't you just always assign to the > index position. In other words does the array need to be filled > in a sequential manner or could you have a 'hole' in the middle (one of the > few things an array allows that a list doesn't - although you can just use a > dictionary if that's really important! What is a complete list of the "things" array-style thinking allow, but that Python lists don't? And for those "things", what would be a Python way of handling them? One thing that I have noticed in my recent Python efforts is that when I am implementing an array-like object as a list, that when the number of dimensions gets beyond two, the Python method of addressing individual data items seems a bit clunky to me. For instance, if I wanted to address an item in a 3-dimensional array, I would use something like (x, y, z) whereas the Python list form amounts to [x][y][z] . Of course, all attempts to print out an entire array or list if n >= 3 will not display well. But this slight "clunkiness" in addressing pales in comparison to the power and flexibility of the "stuff" I can store in a list compared to an array (unless they have substantially evolved). And I have greatly enjoyed making use of this flexibility! -- boB From fomcl at yahoo.com Wed Mar 4 20:16:34 2015 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 4 Mar 2015 19:16:34 +0000 (UTC) Subject: [Tutor] List of ints In-Reply-To: References: Message-ID: <1386616416.1112988.1425496594152.JavaMail.yahoo@mail.yahoo.com> ----- Original Message ----- > From: Mark Lawrence > To: tutor at python.org > Cc: > Sent: Wednesday, March 4, 2015 10:20 AM > Subject: Re: [Tutor] List of ints > > On 04/03/2015 00:25, Steven D'Aprano wrote: >> On Tue, Mar 03, 2015 at 04:50:41PM +1000, Phil wrote: >> >>> count [0] += 1 >>> >>> This fails with the following error; >>> >>> TypeError: 'int' object is not iterable >> >> I know that others have already solved the problem, but here is >> something which might help you solve similar problems in the future. >> The way to debug simple things like this is quite simple: >> >> print count[0] >> >> which will show you that count[0] is a list [0], not an int 0, and you >> are trying to add [0]+1 which doesn't work. >> >> Never under-estimate the usefulness of a few print calls when debugging. >> >> > > About time we threw in the use of the interactive interpreter for trying > code snippets as well, just for good measure :) Perhaps both at the same time using the PDB debugger from within IPython? %run -d -b myscript.py d = debug, b = breakpoint inside pdb: c = continue, q = quit, p = print, r = return value of current function, s = step, and more: https://docs.python.org/2/library/pdb.html From dyoo at hashcollision.org Wed Mar 4 22:08:06 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Wed, 4 Mar 2015 13:08:06 -0800 Subject: [Tutor] Fixed Vector Array In-Reply-To: References: <54f728b3.949ee00a.1038.fffff298@mx.google.com> <54f74511.5c17370a.5f61.ffff972c@mx.google.com> Message-ID: On Wed, Mar 4, 2015 at 9:51 AM, Danny Yoo wrote: > Can someone point out how to write and run tests to Ni'Yana? I'm busy at > the moment. Thanks! I'm still too busy to answer your question directly, but I can point to you a recent post on unit testing a class: https://mail.python.org/pipermail/tutor/2015-March/104387.html Read that mailing list thread. You should be testing your code. It appears that you haven't written tests from your earlier response, and that's a bit concerning! Your class should be teaching your basic skills, and testing is one of those basic skills. Otherwise, you won't have any independent confirmation that the code is actually doing something useful. From davea at davea.name Thu Mar 5 00:40:26 2015 From: davea at davea.name (Dave Angel) Date: Wed, 04 Mar 2015 18:40:26 -0500 Subject: [Tutor] Fixed Vector Array In-Reply-To: <54f728b3.949ee00a.1038.fffff298@mx.google.com> References: <54f728b3.949ee00a.1038.fffff298@mx.google.com> Message-ID: <54F797EA.10607@davea.name> On 03/04/2015 10:40 AM, niyanaxx95 at gmail.com wrote: > Need help trying to implement insert, remove, indexof, and reverse functions. > > I tried to do them but am not sure if it is correct. I am struggling with arrays. > > > This is python and using ezarrays. > I don't know any Python that includes something called ezarrays in its library. What version of Python are you using, and where do you get ezarrays? In general, the tutor list is for learning about Python and its standard library. You might get lucky, and find someone who knows your special library, but that's more likely on either python-list, or in a forum that is specifically for that library. We'll try to help here, but it'll be based on a bunch of guesses, rather than experience. Not the best way to get help. I'm going to speculate and guess you're talking about http://courses.necaiseweb.org/EZArrays/EZArrays > > Assignment: Seems like it's trying to teach you pascal, or java, not Python. > > A fixed vector is very similar to the vector in that it is a sequence container that can grow and shrink as needed and include many useful operations. But the fixed vector has a maximum capacity beyond which it can not expand. > > ? FixedVector(maxSize): Creates an empty fixed vector with the given maximum capacity. > > ? length(): Returns the number of elements contained in the vector. You called the method __len__(), rather than the requested length(). It's not clear what they want here, but it's a reasonable guess that they want 1+ the index of the highest item that isn't None. > > ? isFull(): Returns a Boolean indicating if the vector is full. > > ? getitem(index): Returns the element stored in the vector at position index. The value of index must be within the valid range. > Again, you implemented __getitem__() rather than the requested getitem() > ? setitem(index, item): Sets the element at position index to contain the given item. The value of index must be within the valid range, which includes one position beyond the end of the vector. In the latter case, the item is simply appended onto the end. And again here. > ? contains(item): Determines if the given item is contained in the vector. And again here. > > ? toString(): Returns a string representation of the vector. But you called it __str__ > > ? append(item): Adds the given item to the end of the vector. An item can not be appended to a full vector. > > ? clear(): Removes all elements from the vector. > > ? insert(index, item): Inserts the given item into the vector at position index. The elements at and following the given position are shifted down to make room for the new item. The index must be within the valid range and the vector can not be full. If index is one position beyond the end of the vector, the item is appended onto the vector. > > ? remove(index): Removes the element at position index from the vector. The removed element is returned. The index must be within the valid range. > Not clear if this is supposed to be symmetric with insert. If so, then you have to slide things left, like you slid them right in the previous method. > ? indexOf(item): Returns the index of the element containing the given item. The item must be in the list. > > ? reverse(): Performs a list reversal by reversing the order of the elements within the vector. > > > > > > My Code: > > > from ezarrays import Array > > > class FixedVector : > # Creates a new empty fixed vector with the given maximum capacity. > def __init__(self, maxSize) : > self._theItems = Array(maxSize) > self._numItems = 0 > > # Returns the number of elements contained in the vector. > def __len__(self) : > return self._numItems > > # Returns a Boolean indicating if the vector is full. > def isFull(self) : > return self._numItems == len(self._theItems) > > # Returns the element stored in the vector at position index. > # The value of index must be within the valid range. > def __getitem__(self, index) : > assert index >= 0 and index < self._numItems, "Index out of Range." assert should never be used to check data values, but only for program invariants. It should be an if statement with a raise statement in the body. Same thing for other asserts in this code. > return self._theItems[index] > > # Sets the element at position index to contain the given item. The > # value of index must be within the valid range, which includes one > # position beyond the end of the vector. In the latter case, the item > # is simply appended onto the end. > def __setitem__(self, index, item) : > assert index >= 0 and index <= self._numItems, "Index out of range." > > if index == self._numItems : > self.append(item) At this point, the _numItems value is incorrect > else : > self._theItems[index] = item > > # Determines if the given item is contained in the vector. > def __contains__(self, item) : > i = 0 > while i < self._numItems : > if self._theItems[i] == item : > return True > i = i + 1 Don't use a while loop when you know the range it'll be using. And don't use a range when you can iterate directly over the collection. A for loop would be half the size > > return False > > # Returns a string representation of the vector. > def __repr__(self) : > if self._numItems == 0 : > return "[]" > > vectStr = "[" + str(self._theItems[0]) > for i in range(1, self._numItems) : > vectStr = vectStr + ", " + str(self._theItems[i]) > > vectStr = vectStr + "]" > return vectStr > > # Adds the given item to the end of the vector. An item can not be > # appended to a full vector. > def append(self, item) : Seems like the easiest way would be to simply call setitem with the appropriate index value. > assert self._numItems < len(self._theItems), \ > "Can not add to a full vector." > self._theItems[self._numItems] = item > self._numItems = self._numItems + 1 > > # Removes all elements from the vector. > def clear(self) : > self._theItems.clear(None) No need to do that. Just set the _numItems to zero. Nobody's allowed to look beyond that anyway. > > # Inserts the given item into the vector at position index. The elements > # at and following the given position are shifted down to make room > # for the new item. The index must be within the valid range and the > # vector can not be full. If index is one position beyond the end of > # the vector, the item is appended. > def insert(self, index, item) : You need to check both that the index is within range, and that _numItems isn't already at max. Do a raise if either situation occurs. > i = numItems > while i > index : > self._theItem[i] = self._theItem[i-1] > i = i-1 > self._theItems[i] = item > self._numItems = self._numItems + 1 > > # Removes the element at position index from the vector. The removed > # element is returned. The index must be within the valid range. > def remove(self, index) : You need to test the value of index, to see if it's within range. > removed = self._theItems[index] > self._theItems[index] = None > return removed > > > # Returns the index of the element containing the given item. The item > # must be in the list. > def indexOf(self, item) : > for i in range(0, self._numItems ): > if self._theItems[i] == item : > return i You need to raise an exception to tell the caller that the item was not in the list. > > > # Reverses the order of the elements within the vector. > def reverse(self) : > end = self._numItems -1 > for x in range(0, self._numItems // 2) : > tmp = self._theItems[x] > self._theItems[x] = self._theItems[end] > self._theItems = tmp > end = end -1 Generally, it's better practice to use an expression instead of another variable. The end value is always _numItems - x -1 And you can swap two items just by doing a tuple assignment: a, b = b, a -- DaveA From phil_lor at bigpond.com Thu Mar 5 02:53:26 2015 From: phil_lor at bigpond.com (Phil) Date: Thu, 05 Mar 2015 11:53:26 +1000 Subject: [Tutor] Python 3 - bugs or installation problem Message-ID: <54F7B716.9050003@bigpond.com> I hope this is not another embarrassingly obvious answer to a simple question. Python 3, under Kubuntu. xrange() fails whereas range() is accepted. Could this be an installation problem? phil at Asus:~/Python$ python3 Python 3.4.2 (default, Oct 8 2014, 13:18:07) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> for row in xrange(0,12): ... print(row) ... Traceback (most recent call last): File "", line 1, in NameError: name 'xrange' is not defined >>> Under IDLE 3: for row in xrange(0,12): print('test ',row) xrange() is accepted but prints the following: ('test ', 0) ('test ', 1) ('test ', 2) ('test ', 3) ('test ', 4) Whereas it should be printed as: test 0 test 1 etc Could this be another Python 3 installation problem, this time with print()? Under the Python 3 interpreter using range() and not xrange() the printout is correct. -- Regards, Phil From robertvstepp at gmail.com Thu Mar 5 03:11:45 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 4 Mar 2015 20:11:45 -0600 Subject: [Tutor] Python 3 - bugs or installation problem In-Reply-To: <54F7B716.9050003@bigpond.com> References: <54F7B716.9050003@bigpond.com> Message-ID: On Wed, Mar 4, 2015 at 7:53 PM, Phil wrote: > I hope this is not another embarrassingly obvious answer to a simple > question. > > Python 3, under Kubuntu. > > xrange() fails whereas range() is accepted. Could this be an installation > problem? > etc This may fall into the obvious answer. ~(:>)) My Python reference says that the xrange function was discontinued in Python 3, which you are using. "In Python 3.x, the original range() function is changed to return an iterable instead of producing a result in memory, and thus subsumes and [sic?] Python 2.x's xrange(), which is removed." -- Python Pocket Reference, 5th ed., by Mark Lutz. -- boB From davea at davea.name Thu Mar 5 03:18:11 2015 From: davea at davea.name (Dave Angel) Date: Wed, 04 Mar 2015 21:18:11 -0500 Subject: [Tutor] Python 3 - bugs or installation problem In-Reply-To: References: <54F7B716.9050003@bigpond.com> Message-ID: <54F7BCE3.3070507@davea.name> On 03/04/2015 09:11 PM, boB Stepp wrote: > On Wed, Mar 4, 2015 at 7:53 PM, Phil wrote: >> I hope this is not another embarrassingly obvious answer to a simple >> question. >> >> Python 3, under Kubuntu. >> >> xrange() fails whereas range() is accepted. Could this be an installation >> problem? >> etc > > This may fall into the obvious answer. ~(:>)) > > My Python reference says that the xrange function was discontinued in > Python 3, which you are using. > > "In Python 3.x, the original range() function is changed to return an > iterable instead of producing a result in memory, and thus subsumes > and [sic?] Python 2.x's xrange(), which is removed." -- Python Pocket > Reference, 5th ed., by Mark Lutz. > And the other half of the answer is that your IDLE is apparently using the Python 2 interpreter, in which print is defined differently. -- DaveA From alan.gauld at btinternet.com Thu Mar 5 09:10:30 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 05 Mar 2015 08:10:30 +0000 Subject: [Tutor] Python 3 - bugs or installation problem In-Reply-To: <54F7B716.9050003@bigpond.com> References: <54F7B716.9050003@bigpond.com> Message-ID: On 05/03/15 01:53, Phil wrote: > I hope this is not another embarrassingly obvious answer to a simple > question. > > Python 3, under Kubuntu. > > xrange() fails whereas range() is accepted. Could this be an > installation problem? There are many incompatible changes in Python v3 compared with v2. Some are obvious like the removal of xrange and raw_input and a few others. Some are slightly more subtle like the change of print from a statement to a function. Still others are simply name changes to improve consistency of naming. The standard library has had a major makeover too and many modules have been reorganised and renamed. If you are moving to v3 you need to keep the documentation handy for the first few weeks to quickly check errors to see if its a bug or a feature! The good news is that once you get used to ut most of the changes are for the better. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Thu Mar 5 11:07:03 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 05 Mar 2015 10:07:03 +0000 Subject: [Tutor] Strengths & weaknesses of Python lists compared to "old school" arrays [Was "Fixed Vector Array"] In-Reply-To: References: Message-ID: On 04/03/15 19:10, boB Stepp wrote: > learned of programming via FORTRAN and BASIC (various incarnations). > These used multidimensional array-based thinking. > ... I don't recollect anything similar to Python lists There weren't, and it was a common programming exercise to create them using arrays and an API. This made sense since it demonstrated how to enrich the language with a superior data structure. Unfortunately some courses seem to have decided that its equally good to go from the rich lists to simple arrays! Thus removing or replicating built in functionality. > those languages way back then. In doing physics-related programming it > was not unusual to have a need for 3- or 4-dimensional arrays, and > sometimes more. That hasn't changed > What are the strengths and weaknesses of Python lists compared to "old > school" arrays? This will be a somewhat subjective assessment because each programmers use cases will vary. My summary would be that in 17 years of using Python I've never felt the need of a traditional array. It doesn't mean there aren't cases where they might have an edge, but usually there is a more Pythonic solution. Such as using a dictionary to create a sparse array simulation, or creating a class. > 1) If the data involved is strictly numerical, does this alter > your answer? Especially if multidimensional matrix calculations are > involved? No. Numbers are just objects, there is no inherejnt advantage to using arrays v lists. > 2) If either numerical or string data is allowed to be stored, > does this change your answer? Strictly speaking a traditional array is single typed so you can't have strings and numbers mixed (although you could have an array of variant records which achieves the same.) > 3) Have "old school" arrays evolved into something more flexible > in most programming languages? Yes, exactly that's why most modern languages (from about 1990 onwards) have native collection objects that work more or less like Python lists. Either as a native data type or via a standard library. > 4) Are there other clarifying questions I should be asking on this > topic that are not occurring to me? Issues such as performance (arrays are usually direct memory access) and resource consumption (simply memory block arrays with no extra pointers to maintain) enter into the picture. Also compatibility with other languages. An array is typically implemented as a simple block of memory and so can be accessed from most languages. Lists tend to be much more sophisticated structures requiring an API that may not be (fully) supported in another language. > What is a complete list of the "things" array-style thinking allow, > but that Python lists don't? And for those "things", what would be a > Python way of handling them? Hmm, a "complete list" is hard to pinpoint. Especially since there is nearly always a way round it. Also it depends on which language you are comparing to. For example C is almost optimised for native arrays with direct pointer access and pointer arithmetic allowing some very slick array handling tricks. But in BASIC those things are just not possible. So are we comparing Python lists to BASIC arrays (in which case Python wins nearly every time) or Python lists to C arrays, in which case C has several tricks up its sleeve. (Note Visual Basic arrays are much more sophisticated that vanilla BASIC arrays, they are more like Python lists) Here are some typical concepts: 1) arrays are typically created at compile time so there is no initialisation cost. Similarly by being fixed size there is no performance cost in adding new members(up to the fixed size limit) 2) arrays can be empty and sparcely populated. You cannot do this in Python: array = [0,1,2,,,,,,8,9,,,12] you need to specify None in the empty slots But you can use a dictionary: array = {0:0, 1:1, 2:2, 8:8, 9:9, 12,12} But that's more cumbersome to create/maintain. But its just as easy to use... 3) In some language you can create the array as single entities but then use them in pairs or triples etc. In Python you need a combination of slicing and helper functions to do that. but its a fairly esoteric case mainly, in my experience, used for systems programming in segmented memory architectures. 4) Arrays as memory blocks can be quickly copied or moved or dumped to disk/networks using byte level operations. This can be important in high performance applications. I'm running out of ideas here, I'm sure others will chip in with more. > wanted to address an item in a 3-dimensional array, I would use > something like (x, y, z) whereas the Python list form amounts to > [x][y][z] . That's just a syntax thing, Python could have allowed single bracketing of index, Guido chose to follow his mantra of explicit is better than implicit. Many array based languages (including C) also require multiple explicit indices. > addressing pales in comparison to the power and flexibility of the > "stuff" I can store in a list Absolutely and that's why most modern languages, even when they have "arrays", are more like Python lists. And don't forget the other data collections in Python that used to have to be built in older languages: dictionaries, sets, tuples, classes. When combined these are far superior to managing multiple arrays: trying to keep indices in step, resizing when you need more space, inserting into the middle. sorting across multiple arrays etc. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Thu Mar 5 13:20:50 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 5 Mar 2015 23:20:50 +1100 Subject: [Tutor] Strengths & weaknesses of Python lists compared to "old school" arrays [Was "Fixed Vector Array"] In-Reply-To: References: Message-ID: <20150305122044.GK7655@ando.pearwood.info> On Wed, Mar 04, 2015 at 01:10:11PM -0600, boB Stepp wrote: > What are the strengths and weaknesses of Python lists compared to "old > school" arrays? Python lists are objects, containing a sequence of objects. Objects are "fatter" than low-level data like machine ints. (They take up more space.) Python lists are over-allocated. E.g. when you create an empty list, Python allocates enough space for (I'm guessing) eight items. When it fills up, and you add another item, the list is *quadrupled* to allow enough space for 32 items. When those items are all full, and you add another item, it quadruples in size again. At some point it stops quadrupling and merely doubles in size, but the over-all result is that *on average* Python lists are about 50% bigger than actually needed. Why do they do that? Because that saves time. Python lists trade off a bit of extra space for extra speed. Resizing the list is expensive, and it can be mathematically proven that by doubling in size like this, appending to a list works out to be approximately constant time on average, regardless of whether the list is nearly empty or full. The technical term used is "constant time amortized". Python lists can hold any arbitrary object. Arrays (from the `array` module) can only hold low-level types like C integers or floats. That saves memory, but is actually slower. > 1) If the data involved is strictly numerical, does this alter > your answer? Especially if multidimensional matrix calculations are > involved? Absolutely. In that case, use numpy, which has powerful numeric functions operating at high speed on low-level C-like arrays. py> import numpy py> L = [1] * 5000000 py> A = numpy.array(L) py> with Stopwatch(): ... sum(L) ... 5000000 time taken: 0.098561 seconds py> with Stopwatch(): ... numpy.sum(A) ... 5000000 time taken: 0.008987 seconds > 2) If either numerical or string data is allowed to be stored, > does this change your answer? If you have mixed data, use lists. > 3) Have "old school" arrays evolved into something more flexible > in most programming languages? I don't know about "most" languages, but generally high-level languages will tend to have something similar to Python lists, while low-level languages tend to have things closer to C arrays. > 4) Are there other clarifying questions I should be asking on this > topic that are not occurring to me? Probably :-) > > If Array returns a fixed size array can't you just always assign to the > > index position. In other words does the array need to be filled > > in a sequential manner or could you have a 'hole' in the middle (one of the > > few things an array allows that a list doesn't - although you can just use a > > dictionary if that's really important! I'm not really sure that arrays can have holes in them. Basic low-level arrays, like found in Pascal, C or Fortran, cannot. There are ways and means of creating so-called "sparse arrays", but they typically don't allow you to just have arbitrary gaps in the array, rather they have some sort of symmetry or pattern, e.g. a two dimensional array like: [ a b c d e f 0 g h i j k 0 0 l m n o 0 0 0 p q r 0 0 0 0 s t 0 0 0 0 0 u ] with all zeroes below the diagonal may be able to be programmed in a more efficient way. > What is a complete list of the "things" array-style thinking allow, > but that Python lists don't? And for those "things", what would be a > Python way of handling them? The biggest -- probably only -- advantage is that with low-level arrays, you can use high-speed C functions that effectively operate on the whole array at once (for some definition of "at once") instead of relatively slow item-at-a-time processing like in Python. For ten items, or ten thousand, the difference is minimal, but for serious number-crunching will millions or hundreds of millions of values, numpy will out-perform anything you can write in pure Python. -- Steve From alan.gauld at btinternet.com Thu Mar 5 14:26:53 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 05 Mar 2015 13:26:53 +0000 Subject: [Tutor] Strengths & weaknesses of Python lists compared to "old school" arrays [Was "Fixed Vector Array"] In-Reply-To: <20150305122044.GK7655@ando.pearwood.info> References: <20150305122044.GK7655@ando.pearwood.info> Message-ID: On 05/03/15 12:20, Steven D'Aprano wrote: >>> index position. In other words does the array need to be filled >>> in a sequential manner or could you have a 'hole' in the middle... > > I'm not really sure that arrays can have holes in them. Basic low-level > arrays, like found in Pascal, C or Fortran, cannot. They can in the sense that I originally meant: You declare the array and at compile time a block of memory is reserved but it contains random trash, they are not usually pre-filled with, say zero. (Actually, Pascal may well do that, it sounds like the kind of thing Pascal would do...) You can then allocate values to any index within that array, you don't need to do it sequentially as with a python list You can fake it in Python by filling the array with a null value such as zero or None but it takes explicit action and run time resources to do that, unlike the C/Pascal equivalent. Alternatively just use a dict. > means of creating so-called "sparse arrays", I wasn't really thinking of sparse arrays in the pure CS sense, just a regular C array which does not necessarily have a specified value at certain positions. // file: openarray.c #include void main(){ static int ia[10]; ia[0] = 3; ia[9] = 42; printf("array = %d,%d\n",ia[0],ia[9]); printf("splat! %d\n",ia[4]); } $ make openarray $ ./openarray array = 3,42 splat! 4195696 You can of course also initialize to zero by making it a static array, but that has other consequences that you may not want. Hopefully that clarifies what I was thinking about. Also I should add that this behaviour is rarely a "Good Thing". Uninitialized values are usually to be avoided! PS. Totally off topic but... I just checked Pascal - boy am I rusty! program openarray; var ia : array[0..9] of integer; begin ia[0] := 3; ia[9] := 42; writeln('array holds ', ia[0],' and ', ia[9]); writeln('splat - ',ia[4]); end. $ fpc openarray.pas $ ./openarray array holds 3 and 42 splat - 0 So yes it does fill with zeros. Thank you Nicholas... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From malaclypse2 at gmail.com Thu Mar 5 17:16:58 2015 From: malaclypse2 at gmail.com (Jerry Hill) Date: Thu, 5 Mar 2015 11:16:58 -0500 Subject: [Tutor] Python 3 - bugs or installation problem In-Reply-To: <54F7B716.9050003@bigpond.com> References: <54F7B716.9050003@bigpond.com> Message-ID: On Wed, Mar 4, 2015 at 8:53 PM, Phil wrote: > phil at Asus:~/Python$ python3 > Python 3.4.2 (default, Oct 8 2014, 13:18:07) > [GCC 4.9.1] on linux > Type "help", "copyright", "credits" or "license" for more information. >>>> for row in xrange(0,12): > ... print(row) > ... > Traceback (most recent call last): > File "", line 1, in > NameError: name 'xrange' is not defined That looks like proper python 3 behavior, since xrange was replaced by range in python 3 (the old behavior of creating an actual list from range is now spelled list(range(0,12))). > Under IDLE 3: > > for row in xrange(0,12): > print('test ',row) > > xrange() is accepted but prints the following: > > ('test ', 0) > ('test ', 1) > ('test ', 2) > ('test ', 3) > ('test ', 4) Are you 100% positive that's the python 3 IDLE? Based on the output you show, that's python 2. -- Jerry From robertvstepp at gmail.com Thu Mar 5 19:25:59 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Thu, 5 Mar 2015 12:25:59 -0600 Subject: [Tutor] Strengths & weaknesses of Python lists compared to "old school" arrays [Was "Fixed Vector Array"] In-Reply-To: <20150305122044.GK7655@ando.pearwood.info> References: <20150305122044.GK7655@ando.pearwood.info> Message-ID: Thank you very much Alan and Steve for your detailed answers. You have clarified many things and sent me off searching for more information on variant records, tagged and untagged unions, sparse arrays, linked lists, how memory allocation is affected by these topics and some other items. And I would like to throw out a general THANK YOU to all of the "tutors", who so generously donate your time. I know I have gotten many detailed, patient answers to my queries. I just hope I truly absorb and properly utilize the wisdom passed on to me! boB From breamoreboy at yahoo.co.uk Thu Mar 5 21:32:37 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 05 Mar 2015 20:32:37 +0000 Subject: [Tutor] Python 3 - bugs or installation problem In-Reply-To: References: <54F7B716.9050003@bigpond.com> Message-ID: On 05/03/2015 08:10, Alan Gauld wrote: > On 05/03/15 01:53, Phil wrote: >> I hope this is not another embarrassingly obvious answer to a simple >> question. >> >> Python 3, under Kubuntu. >> >> xrange() fails whereas range() is accepted. Could this be an >> installation problem? > > There are many incompatible changes in Python v3 compared with v2. > Some are obvious like the removal of xrange and raw_input and a few > others. Some are slightly more subtle like the change of print from a > statement to a function. Still others are simply name changes to improve > consistency of naming. > > The standard library has had a major makeover too and many > modules have been reorganised and renamed. > > If you are moving to v3 you need to keep the documentation > handy for the first few weeks to quickly check errors to > see if its a bug or a feature! > Starting here https://docs.python.org/3/howto/pyporting.html -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From breamoreboy at yahoo.co.uk Thu Mar 5 21:54:38 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 05 Mar 2015 20:54:38 +0000 Subject: [Tutor] Strengths & weaknesses of Python lists compared to "old school" arrays [Was "Fixed Vector Array"] In-Reply-To: References: Message-ID: On 05/03/2015 10:07, Alan Gauld wrote: > On 04/03/15 19:10, boB Stepp wrote: >> wanted to address an item in a 3-dimensional array, I would use >> something like (x, y, z) whereas the Python list form amounts to >> [x][y][z] . > > That's just a syntax thing, Python could have allowed single > bracketing of index, Guido chose to follow his mantra of explicit > is better than implicit. Many array based languages (including C) > also require multiple explicit indices. > You could regard this a code smell in Python. Perhaps the most repeated thing written here by beginners is something like:- for i in range(len(this)): for j in range(len(that)): for k in range(len(other)): if mystruct[i][j][k] then: ... An experienced Pythonista's code would maybe be:- for x in mystruct: for y in x: for z in y: if z: ... -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From fomcl at yahoo.com Thu Mar 5 22:48:53 2015 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Thu, 5 Mar 2015 21:48:53 +0000 (UTC) Subject: [Tutor] Strengths & weaknesses of Python lists compared to "old school" arrays [Was "Fixed Vector Array"] In-Reply-To: References: Message-ID: <654772950.1421296.1425592133556.JavaMail.yahoo@mail.yahoo.com> ---- Original Message ----- > From: Mark Lawrence > To: tutor at python.org > Cc: > Sent: Thursday, March 5, 2015 9:54 PM > Subject: Re: [Tutor] Strengths & weaknesses of Python lists compared to "old school" arrays [Was "Fixed Vector Array"] > > On 05/03/2015 10:07, Alan Gauld wrote: >> On 04/03/15 19:10, boB Stepp wrote: >>> wanted to address an item in a 3-dimensional array, I would use >>> something like (x, y, z) whereas the Python list form amounts to >>> [x][y][z] . >> >> That's just a syntax thing, Python could have allowed single >> bracketing of index, Guido chose to follow his mantra of explicit >> is better than implicit. Many array based languages (including C) >> also require multiple explicit indices. >> > > You could regard this a code smell in Python. Perhaps the most repeated > thing written here by beginners is something like:- > > for i in range(len(this)): > for j in range(len(that)): > for k in range(len(other)): > if mystruct[i][j][k] then: > ... > > An experienced Pythonista's code would maybe be:- > > for x in mystruct: > for y in x: > for z in y: > if z: > ... > aside from the range(len)) horror: isn't this also a matter of "putting the busiest loop on the inside of nested loops"? See also the book Code Complete: https://books.google.nl/books?id=I-83BAAAQBAJ&pg=PA643&lpg=PA643&dq=%22busiest+loop%22+inside&source=bl&ots=4ER2sPjGcq&sig=UyyxYY5LSDN4Xd5B-u-Ft7zNjpo&hl=nl&sa=X&ei=TM74VLOSBIOuPdfcgFg&ved=0CDgQ6AEwAg#v=onepage&q=%22busiest%20loop%22%20inside&f=false From chrisstinemetz at gmail.com Fri Mar 6 15:28:24 2015 From: chrisstinemetz at gmail.com (Chris Stinemetz) Date: Fri, 6 Mar 2015 08:28:24 -0600 Subject: [Tutor] append value to dictionary key Message-ID: I would like to append a value to a dictionary key where there is already a value. Something like the below: d = {'name': {'2': 0.0, '7': 10.0, '8': 0.0, '9': 0.0}} append 10 to d['name']['2'] d = {'name': {'2': 0.0,10, '7': 10.0, '8': 0.0, '9': 0.0}} When I try to this I get the following error: >>> d['name']['2'].append(10) Traceback (most recent call last): File "", line 1, in AttributeError: 'float' object has no attribute 'append' I am obviously doing this wrong. How would I accomplish this? Thank you in advance. From alan.gauld at btinternet.com Fri Mar 6 19:53:17 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 06 Mar 2015 18:53:17 +0000 Subject: [Tutor] append value to dictionary key In-Reply-To: References: Message-ID: On 06/03/15 14:28, Chris Stinemetz wrote: > I would like to append a value to a dictionary key where there is already a > value. Something like the below: > > d = {'name': {'2': 0.0, '7': 10.0, '8': 0.0, '9': 0.0}} > append 10 to d['name']['2'] > d = {'name': {'2': 0.0,10, '7': 10.0, '8': 0.0, '9': 0.0}} > > When I try to this I get the following error: >>>> d['name']['2'].append(10) > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'float' object has no attribute 'append' To append a value you need a list. So put the values of the dictionary in lists. d = {'name': {'2': [0.0], '7': [10.0], '8': [0.0], '9': [0.0]}} Now your d['name']['2'].append(10) will work and produce:. d = {'name': {'2': [0.0, 10], '7': [10.0], '8': [0.0], '9': [0.0]}} -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From timomlists at gmail.com Fri Mar 6 20:06:01 2015 From: timomlists at gmail.com (Timo) Date: Fri, 06 Mar 2015 20:06:01 +0100 Subject: [Tutor] append value to dictionary key In-Reply-To: References: Message-ID: <54F9FA99.6030100@gmail.com> Op 06-03-15 om 15:28 schreef Chris Stinemetz: > I would like to append a value to a dictionary key where there is already a > value. Something like the below: > > d = {'name': {'2': 0.0, '7': 10.0, '8': 0.0, '9': 0.0}} > append 10 to d['name']['2'] > d = {'name': {'2': 0.0,10, '7': 10.0, '8': 0.0, '9': 0.0}} > > When I try to this I get the following error: >>>> d['name']['2'].append(10) > Traceback (most recent call last): > File "", line 1, in > AttributeError: 'float' object has no attribute 'append' Maybe it's easier to break down your problem and start with a smaller example. Remove the dictionary and do the following: >>> x = 0.0 >>> x.append(10) What do you expect to happen? Here's the output: Traceback (most recent call last): File "", line 1, in AttributeError: 'float' object has no attribute 'append' Exactly the same as yours! So, a float has no 'append' attribute, but what objects do have that? Sounds like you need a list to hold an arbitrary number of items. >>> l = [0.0] >>> l [0.0] >>> l.append(10) >>> l [0.0, 10] Timo > > I am obviously doing this wrong. How would I accomplish this? > > Thank you in advance. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From markos at c2o.pro.br Fri Mar 6 19:27:25 2015 From: markos at c2o.pro.br (Markos) Date: Fri, 06 Mar 2015 15:27:25 -0300 Subject: [Tutor] Idle - ImportError: No module named numpy In-Reply-To: References: <20150305122044.GK7655@ando.pearwood.info> Message-ID: <54F9F18D.80001@c2o.pro.br> Hi, I'm beginning to study the numpy. When I open a terminal (Debian Squeeze) and run the python interpreter the command "import numpy as np" run without errors. But when I run the same command on idle3 the following error appears. >>> import numpy as np Traceback (most recent call last): File "", line 1, in import numpy as np ImportError: No module named numpy How configure idle to load the numpy module? Thanks, Markos From wolfrage8765 at gmail.com Sat Mar 7 01:42:40 2015 From: wolfrage8765 at gmail.com (WolfRage) Date: Fri, 06 Mar 2015 19:42:40 -0500 Subject: [Tutor] Idle - ImportError: No module named numpy In-Reply-To: <54F9F18D.80001@c2o.pro.br> References: <20150305122044.GK7655@ando.pearwood.info> <54F9F18D.80001@c2o.pro.br> Message-ID: <54FA4980.2000605@gmail.com> Well on the python interpretor did you use python3 or just python? On 03/06/2015 01:27 PM, Markos wrote: > Hi, > > I'm beginning to study the numpy. > > When I open a terminal (Debian Squeeze) and run the python interpreter > the command "import numpy as np" run without errors. > > But when I run the same command on idle3 the following error appears. > > >>> import numpy as np > Traceback (most recent call last): > File "", line 1, in > import numpy as np > ImportError: No module named numpy > > How configure idle to load the numpy module? > > Thanks, > Markos > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From robertvstepp at gmail.com Sat Mar 7 01:43:34 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 6 Mar 2015 18:43:34 -0600 Subject: [Tutor] Idle - ImportError: No module named numpy In-Reply-To: <54F9F18D.80001@c2o.pro.br> References: <20150305122044.GK7655@ando.pearwood.info> <54F9F18D.80001@c2o.pro.br> Message-ID: On Fri, Mar 6, 2015 at 12:27 PM, Markos wrote: > Hi, > > I'm beginning to study the numpy. > > When I open a terminal (Debian Squeeze) and run the python interpreter the > command "import numpy as np" run without errors. > > But when I run the same command on idle3 the following error appears. > >>>> import numpy as np > Traceback (most recent call last): > File "", line 1, in > import numpy as np > ImportError: No module named numpy > > How configure idle to load the numpy module? You don't by chance have Python 2 installed as well as Python 3, where Python 2 is associated with your terminal session? And your numpy module is for Python 2, not 3? That is my first thought, but I'm sure the experts will chime in shortly with a more definitive diagnosis. -- boB From breamoreboy at yahoo.co.uk Sat Mar 7 03:13:30 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 07 Mar 2015 02:13:30 +0000 Subject: [Tutor] Idle - ImportError: No module named numpy In-Reply-To: <54FA4980.2000605@gmail.com> References: <20150305122044.GK7655@ando.pearwood.info> <54F9F18D.80001@c2o.pro.br> <54FA4980.2000605@gmail.com> Message-ID: On 07/03/2015 00:42, WolfRage wrote: > Well on the python interpretor did you use python3 or just python? > Please don't top post on this list. It makes following long threads difficult if not impossible, and is basically downright irritating. Thanks for listening. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From davea at davea.name Sat Mar 7 04:03:21 2015 From: davea at davea.name (Dave Angel) Date: Fri, 06 Mar 2015 22:03:21 -0500 Subject: [Tutor] Idle - ImportError: No module named numpy In-Reply-To: <54F9F18D.80001@c2o.pro.br> References: <20150305122044.GK7655@ando.pearwood.info> <54F9F18D.80001@c2o.pro.br> Message-ID: <54FA6A79.7070802@davea.name> On 03/06/2015 01:27 PM, Markos wrote: > Hi, > > I'm beginning to study the numpy. > And what does this have to do with the """Strengths & weaknesses of Python lists compared to "old school" arrays [Was "Fixed Vector Array"]""" thread? Please don't hijack a thread by replying with an unrelated message. Just start a new one with "Write email" or equivalent. Address it to tutor at python.org, and it'll be fine. > When I open a terminal (Debian Squeeze) and run the python interpreter > the command "import numpy as np" run without errors. > > But when I run the same command on idle3 the following error appears. > > >>> import numpy as np > Traceback (most recent call last): > File "", line 1, in > import numpy as np > ImportError: No module named numpy > > How configure idle to load the numpy module? > As others have said, you probably have a version mismatch. When you ran Python from the terminal, what did you call it? Which version did you get? When you ran idle3, you presumably ran some version 3 installation of Python. From each interactive session, you can check the Python version with: >>>> import sys >>>> sys.version Unless they're identical in the two environments you describe, you can't assume they'll both have numpy loaded. -- DaveA From akash.shekhar121 at gmail.com Sat Mar 7 14:15:32 2015 From: akash.shekhar121 at gmail.com (Akash Shekhar) Date: Sat, 7 Mar 2015 18:45:32 +0530 Subject: [Tutor] String method "strip()" not working Message-ID: I am trying to learn how to use strip() method. It is supposed to cut out all the whitespace as I read in the tutorial. But the code is not working. Here's my code: sentence = "Hello, how are you?" > > >> print(sentence) > > >> print(sentence.strip()) > > >> input("\n\nPress enter key to exit.") > > > Here's it's output: Hello, how are you? > Hello, how are you? > > Press enter key to exit. > Both results are same. P.S.: I am using Python 3.1 IDLE on Windows 7. From alan.gauld at btinternet.com Sat Mar 7 15:54:17 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 07 Mar 2015 14:54:17 +0000 Subject: [Tutor] String method "strip()" not working In-Reply-To: References: Message-ID: On 07/03/15 13:15, Akash Shekhar wrote: > I am trying to learn how to use strip() method. It is supposed to cut out > all the whitespace as I read in the tutorial. Read it again more closely. ------------------- Help on built-in function strip: strip(...) S.strip([chars]) -> string or unicode Return a copy of the string S with leading and trailing whitespace removed. If chars is given and not None, remove characters in chars instead. If chars is unicode, S will be converted to unicode before stripping ---------------------- So it only strips leading and training whitespace, not the whitespace inside the string. (Use str.translate() for that) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From davea at davea.name Sat Mar 7 16:07:24 2015 From: davea at davea.name (Dave Angel) Date: Sat, 07 Mar 2015 10:07:24 -0500 Subject: [Tutor] String method "strip()" not working In-Reply-To: References: Message-ID: <54FB142C.2010705@davea.name> On 03/07/2015 08:15 AM, Akash Shekhar wrote: > I am trying to learn how to use strip() method. It is supposed to cut out > all the whitespace as I read in the tutorial. But the code is not working. > > Here's my code: > > sentence = "Hello, how are you?" >> >> >>> print(sentence) >> >> >>> print(sentence.strip()) >> >> >>> input("\n\nPress enter key to exit.") >> >> >> > Here's it's output: > > Hello, how are you? >> Hello, how are you? >> >> Press enter key to exit. >> > > > Both results are same. > > P.S.: I am using Python 3.1 IDLE on Windows 7. Thanks for mentioning the python version and OS. You don't have any whitespace at the beginning nor end of the string bound to sentence. So there's nothing to strip. By the way, if you're checking such a function, it's sometimes more informative to write print(repr(sentence)) which will add quotes at begin and end, and show newlines and tabs as escape sequences. If you only want to strip from one end of the string, you'd use lstrip() or rstrip(). If you're also trying to remove characters from the middle of the string, you might use translate() or the string method replace(). For example, to remove all spaces from a string, use sentence.replace(" ", "") -- DaveA From breamoreboy at yahoo.co.uk Sat Mar 7 16:17:33 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 07 Mar 2015 15:17:33 +0000 Subject: [Tutor] String method "strip()" not working In-Reply-To: References: Message-ID: On 07/03/2015 14:54, Alan Gauld wrote: > On 07/03/15 13:15, Akash Shekhar wrote: >> I am trying to learn how to use strip() method. It is supposed to cut out >> all the whitespace as I read in the tutorial. > > Read it again more closely. > > ------------------- > Help on built-in function strip: > > strip(...) > S.strip([chars]) -> string or unicode > > Return a copy of the string S with leading and trailing > whitespace removed. > If chars is given and not None, remove characters in chars instead. > If chars is unicode, S will be converted to unicode before stripping Presumably Python 2 (or earlier Python 3) help output. In Python 3 strings are always unicode, so that last line has gone. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From alan.gauld at btinternet.com Sat Mar 7 17:38:24 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 07 Mar 2015 16:38:24 +0000 Subject: [Tutor] String method "strip()" not working In-Reply-To: References: Message-ID: On 07/03/15 15:17, Mark Lawrence wrote: >> S.strip([chars]) -> string or unicode >> >> Return a copy of the string S with leading and trailing >> whitespace removed. >> If chars is given and not None, remove characters in chars instead. >> If chars is unicode, S will be converted to unicode before stripping > > Presumably Python 2 (or earlier Python 3) help output. In Python 3 > strings are always unicode, so that last line has gone. Good catch, especially since the OP specifically said v3. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From eliekhairallah1996 at gmail.com Sat Mar 7 16:39:36 2015 From: eliekhairallah1996 at gmail.com (elie khairallah) Date: Sat, 7 Mar 2015 17:39:36 +0200 Subject: [Tutor] Help Message-ID: hello , I would like to know if theres a way to change a variable's name after every iteration for example I want to make a function that stores a number in x_1 if i=1 and in x_2 if i=2. To be more precise: i=1 def f(n): while i References: Message-ID: On Sat, Mar 7, 2015 at 10:39 AM, elie khairallah wrote: > hello , I would like to know if theres a way to change a variable's name > after every iteration for example I want to make a function that stores a > number in x_1 if i=1 and in x_2 if i=2. > To be more precise: > i=1 > def f(n): > while i x_i = i**2 ##this writing is wrong I would like here to have > x_1 or x_2 ... > (depending on i) > return (x_1,x_2,x_3 .....) > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor You could certainly use I as an index into a list called x. This doesn't look like a good idea though -- Joel Goldstick http://joelgoldstick.com From breamoreboy at yahoo.co.uk Sat Mar 7 19:29:17 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 07 Mar 2015 18:29:17 +0000 Subject: [Tutor] Daft subjects (Was Help) In-Reply-To: References: Message-ID: On 07/03/2015 15:39, elie khairallah wrote: > hello , I would like to know if theres a way to change a variable's name > after every iteration for example I want to make a function that stores a > number in x_1 if i=1 and in x_2 if i=2. > To be more precise: > i=1 > def f(n): > while i x_i = i**2 ##this writing is wrong I would like here to have > x_1 or x_2 ... > (depending on i) > return (x_1,x_2,x_3 .....) Why bother? What's wrong with this? def f(i, n): values = [] for x in range(i, n+1, 1): values.append(x**2) return values -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From joel.goldstick at gmail.com Sat Mar 7 19:45:01 2015 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Sat, 7 Mar 2015 13:45:01 -0500 Subject: [Tutor] Help In-Reply-To: References: Message-ID: On Sat, Mar 7, 2015 at 1:28 PM, Danny Yoo wrote: >> You could certainly use I as an index into a list called x. This >> doesn't look like a good idea though > > > Can you explain more what's problematic with a list? > > My best understanding so far of the problem is that the original > questioner is trying to compute a tabulation of results. For example, > I think they're trying to compute the squares of the numbers 1 up to > some large number like 20. In that scenario, lists seem like a > reasonable representation for such a table. Sorry for clumsy answer. I was trying to say that using a list with i as an index was a solution rather than trying to build names on the fly with some text concatenated to the value of i -- Joel Goldstick http://joelgoldstick.com From dyoo at hashcollision.org Sat Mar 7 19:25:19 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sat, 7 Mar 2015 10:25:19 -0800 Subject: [Tutor] Help In-Reply-To: References: Message-ID: On Sat, Mar 7, 2015 at 7:39 AM, elie khairallah wrote: > hello , I would like to know if theres a way to change a variable's name > after every iteration for example I want to make a function that stores a > number in x_1 if i=1 and in x_2 if i=2. Conceptually, I think you're looking for a list. The "subscript" you're using can be implemented as a list indexing operation. e.g.: ############ x = [0, 0] x[0] = 5 x[1] = 4 print x[0] print x[1] ############## The index we're using can be an arbitrary expression, not just a fixed number: ################ messages = [0] * 5 for i in range(5): messages[i] = ("This is %d" % i) print messages ################ From dyoo at hashcollision.org Sat Mar 7 19:28:15 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sat, 7 Mar 2015 10:28:15 -0800 Subject: [Tutor] Help In-Reply-To: References: Message-ID: > You could certainly use I as an index into a list called x. This > doesn't look like a good idea though Can you explain more what's problematic with a list? My best understanding so far of the problem is that the original questioner is trying to compute a tabulation of results. For example, I think they're trying to compute the squares of the numbers 1 up to some large number like 20. In that scenario, lists seem like a reasonable representation for such a table. From alan.gauld at btinternet.com Sat Mar 7 20:04:55 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 07 Mar 2015 19:04:55 +0000 Subject: [Tutor] Help In-Reply-To: References: Message-ID: On 07/03/15 15:39, elie khairallah wrote: > hello , I would like to know if theres a way to change a variable's name > after every iteration for example I want to make a function that stores a > number in x_1 if i=1 and in x_2 if i=2. > To be more precise: > i=1 > def f(n): > while i x_i = i**2 ##this writing is wrong I would like here to have > x_1 or x_2 ... > (depending on i) > return (x_1,x_2,x_3 .....) Others have already suggested a list, which IMHO is the best option. However if for some reason the zero based index of a list is an issue for you then you could use your naming scheme in a dictionary: def f(n): values = {} root = 'x_' for n in range(1,n+1): values[root+str(n)] = n**2 return values You can then access the values with, for example: print (values['x_2']) But unless you are reading the variable names from a file/network or a user then the numerical index of a list will usually be easier to work with. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From breamoreboy at yahoo.co.uk Sat Mar 7 21:28:50 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sat, 07 Mar 2015 20:28:50 +0000 Subject: [Tutor] Help In-Reply-To: References: Message-ID: On 07/03/2015 19:04, Alan Gauld wrote: > On 07/03/15 15:39, elie khairallah wrote: >> hello , I would like to know if theres a way to change a variable's name >> after every iteration for example I want to make a function that stores a >> number in x_1 if i=1 and in x_2 if i=2. >> To be more precise: >> i=1 >> def f(n): >> while i> x_i = i**2 ##this writing is wrong I would like here >> to have >> x_1 or x_2 ... >> (depending on i) >> return (x_1,x_2,x_3 .....) > > Others have already suggested a list, which IMHO is the best option. > However if for some reason the zero based index of a list is an > issue for you then you could use your naming scheme in a > dictionary: > > def f(n): > values = {} > root = 'x_' > for n in range(1,n+1): > values[root+str(n)] = n**2 > return values > > You can then access the values with, for example: > > print (values['x_2']) > > But unless you are reading the variable names from > a file/network or a user then the numerical index of a > list will usually be easier to work with. > > If zero based indexing is an issue I'd just write:- values = [None] to start with. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From alan.gauld at btinternet.com Mon Mar 9 17:50:11 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 09 Mar 2015 16:50:11 +0000 Subject: [Tutor] file exists question Message-ID: Somebody posted a question asking how to fond out if a file exists. The message was in the queue and I thought I'd approved it but it hasn't shown up yet. Sorry to the OP if I've messed up. The answer is that you use the os.path.exists() function. It takes a path as an argument which can be relative to the cwd or absolute. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From davidheiserca at gmail.com Mon Mar 9 18:50:06 2015 From: davidheiserca at gmail.com (David Heiser) Date: Mon, 09 Mar 2015 10:50:06 -0700 Subject: [Tutor] file exists question In-Reply-To: References: Message-ID: <54FDDD4E.4040208@gmail.com> On 3/9/2015 9:50 AM, Alan Gauld wrote: > Somebody posted a question asking how to fond out if a file > exists. The message was in the queue and I thought I'd approved > it but it hasn't shown up yet. Sorry to the OP if I've messed up. > > The answer is that you use the os.path.exists() function. > It takes a path as an argument which can be relative to > the cwd or absolute. > > HTH If you are testing for the existence of a file before opening it to read it, you can use exception testing instead. try: data = open(filename, 'r').read() except NameError: do something else From wolfgang.maier at biologie.uni-freiburg.de Mon Mar 9 22:32:49 2015 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Mon, 09 Mar 2015 22:32:49 +0100 Subject: [Tutor] file exists question In-Reply-To: <54FDDD4E.4040208@gmail.com> References: <54FDDD4E.4040208@gmail.com> Message-ID: On 09.03.2015 18:50, David Heiser wrote: > > > On 3/9/2015 9:50 AM, Alan Gauld wrote: >> Somebody posted a question asking how to fond out if a file >> exists. The message was in the queue and I thought I'd approved >> it but it hasn't shown up yet. Sorry to the OP if I've messed up. >> >> The answer is that you use the os.path.exists() function. >> It takes a path as an argument which can be relative to >> the cwd or absolute. >> >> HTH > > If you are testing for the existence of a file before opening it to read > it, you can use exception testing instead. > > try: > data = open(filename, 'r').read() > except NameError: > do something else > But why are you watching out for a NameError ? FileNotFoundError would be appropriate in Python3.3 and later. In earlier versions you could rewrite this as (I think): try: data_in = open(filename, 'r') except IOError: do somthing data = data_in.read() to make sure you are detecting a problem with opening the file independently of one reading from it. Wolfgang From markos at c2o.pro.br Mon Mar 9 19:09:09 2015 From: markos at c2o.pro.br (Markos) Date: Mon, 09 Mar 2015 15:09:09 -0300 Subject: [Tutor] Idle - ImportError: No module named numpy In-Reply-To: References: <20150305122044.GK7655@ando.pearwood.info> <54F9F18D.80001@c2o.pro.br> Message-ID: <54FDE1C5.6000301@c2o.pro.br> On 06-03-2015 21:43, boB Stepp wrote: > On Fri, Mar 6, 2015 at 12:27 PM, Markos wrote: > >> Hi, >> >> I'm beginning to study the numpy. >> >> When I open a terminal (Debian Squeeze) and run the python interpreter the >> command "import numpy as np" run without errors. >> >> But when I run the same command on idle3 the following error appears. >> >> >>>>> import numpy as np >>>>> >> Traceback (most recent call last): >> File "", line 1, in >> import numpy as np >> ImportError: No module named numpy >> >> How configure idle to load the numpy module? >> > You don't by chance have Python 2 installed as well as Python 3, where > Python 2 is associated with your terminal session? And your numpy > module is for Python 2, not 3? That is my first thought, but I'm sure > the experts will chime in shortly with a more definitive diagnosis. > > > Hi boB, I installed numpy in Debian (6.0) with the command: apt-get install python-numpy I just find that this package is exclusively for python 2.5 and 2.6 The idle3 use Python 3.1.3. How to install numpy for Python 3 in order to use it with the idle? Is there any simple way? Thanks, Markos From alan.gauld at btinternet.com Tue Mar 10 01:20:43 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 10 Mar 2015 00:20:43 +0000 Subject: [Tutor] Idle - ImportError: No module named numpy In-Reply-To: <54FDE1C5.6000301@c2o.pro.br> References: <20150305122044.GK7655@ando.pearwood.info> <54F9F18D.80001@c2o.pro.br> <54FDE1C5.6000301@c2o.pro.br> Message-ID: On 09/03/15 18:09, Markos wrote: > How to install numpy for Python 3 in order to use it with the idle? > > Is there any simple way? Searching for numpy in Synaptic shows a python3-numpy package I'm using Mint 17 which is based on Debian/Ubuntu so there should be something in the debian repositories of the same name. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Tue Mar 10 01:22:57 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 10 Mar 2015 11:22:57 +1100 Subject: [Tutor] file exists question In-Reply-To: References: Message-ID: <20150310002257.GW7655@ando.pearwood.info> On Mon, Mar 09, 2015 at 04:50:11PM +0000, Alan Gauld wrote: > Somebody posted a question asking how to fond out if a file > exists. The message was in the queue and I thought I'd approved > it but it hasn't shown up yet. Sorry to the OP if I've messed up. > > The answer is that you use the os.path.exists() function. > It takes a path as an argument which can be relative to > the cwd or absolute. os.path.exists is a little bit of an anti-pattern though. It has two problems: there is a race condition here, waiting to bite you. Just because the file exists now, doesn't mean it will exist a millisecond later when you try to open it. Also, even if the file exists, there is no guarantee you can open it. Code like this is buggy: filename = raw_input("What file do you want to open? ") if os.path.exists(filename): with open(filename) as f: text = f.read() process(text) Nearly all computers these days, and all of the common Windows/Mac/Linux/Unix systems, are both multi-processing and multi-user. That means the above code can fail two different ways: * In a multi-processing system, another process can delete the file just after os.path.exists() returns True. It *did* exist, but a millisecond later when you try to open it, it is gone. * In a multi-user system, files can be owned by different users, and you might not have permission to open files belonging to another user. (Technically, you might not even have permission to open files owned by yourself, but that's pretty rare.) So the above is usually written like this: filename = raw_input("What file do you want to open? ") try: with open(filename) as f: text = f.read() except (IOError, OSError): pass else: process(text) We just try to open the file and read from it. If it succeeds, the "else" block runs. (Yes, try...except...else works!). If it fails, because the file doesn't exist or permission is denied, then we just skip the processing step. The only use I have found for os.path.exists is to try to *avoid* an existing file. (Even here, it is still subject to one of the above problems: just because a file *doesn't* exist now, a millisecond it might.) For example, automatically numbering files rather than overwriting them: filename = raw_input("Save file as...? ") name, ext = os.path.splitext(filename) n = 1 while os.path.exists(filename): # automatically pick a new name filename = "%s~%d" % (name, n) n += 1 try: with open(filename, 'w') as f: f.write(text) except (IOError, OSError): pass I'm not 100% happy with that solution, because there is a risk that some other process will create a file with the same name in the gap between calling os.path.exists and calling open, but I don't know how to solve that race condition. What I really want is an option to open() that only opens a new file, and fails if the file already exists. In other contexts, this can actually be a security risk, there is a whole class of "time of check to time of use" security bugs: http://en.wikipedia.org/wiki/Time_of_check_to_time_of_use http://cwe.mitre.org/data/definitions/367.html -- Steve From oscar.j.benjamin at gmail.com Tue Mar 10 01:25:51 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 10 Mar 2015 00:25:51 +0000 Subject: [Tutor] Idle - ImportError: No module named numpy In-Reply-To: <54FDE1C5.6000301@c2o.pro.br> References: <20150305122044.GK7655@ando.pearwood.info> <54F9F18D.80001@c2o.pro.br> <54FDE1C5.6000301@c2o.pro.br> Message-ID: On 9 March 2015 at 18:09, Markos wrote: > I installed numpy in Debian (6.0) with the command: > apt-get install python-numpy > > I just find that this package is exclusively for python 2.5 and 2.6 > > The idle3 use Python 3.1.3. > > How to install numpy for Python 3 in order to use it with the idle? > > Is there any simple way? Hi Markos, I'm not sure about Squeeze but on this Ubuntu 14.04 system you can install numpy for Python 3 by typing: $ sudo apt-get install python3-numpy The Python 3 version of the package is a relatively new addition to the repositories though so it may not be available in Squeeze. If there is no package then you can build numpy yourself. It's not too hard to do. First make sure that you have pip installed for python3. You can check if it's installed by typing: $ python3 -m pip If it says /usr/bin/python3: No module named pip then you don't have pip. You may be able to install pip with $ sudo apt-get install python3-pip If not then you can install it by downloading the get-pip.py script from here: https://pip.pypa.io/en/latest/installing.html Once you have that you can run it as $ sudo python3 get-pip.py Once pip is installed you can use it to install lots of things including numpy. For numpy in particular though you'll need to first install the compilers and Python 3 header files: $ sudo apt-get install build-essential python3-dev Now you can use pip to install numpy for python3: $ sudo python3 -m pip install numpy This will download numpy from PyPI and then try to compile and install it. Compiling numpy takes a while and you will see a lot of strange compiler messages. If it fails then just post the very last part of the output here. It will probably mean that you need to install more compilers or something. If it succeeds then you should be able to import numpy e.g.: $ python3 Python 3.4.0 (default, Apr 11 2014, 13:05:11) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import numpy >>> numpy.cos(1) 0.54030230586813977 Oscar From oscar.j.benjamin at gmail.com Tue Mar 10 01:44:55 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 10 Mar 2015 00:44:55 +0000 Subject: [Tutor] file exists question In-Reply-To: <20150310002257.GW7655@ando.pearwood.info> References: <20150310002257.GW7655@ando.pearwood.info> Message-ID: On 10 March 2015 at 00:22, Steven D'Aprano wrote: > On Mon, Mar 09, 2015 at 04:50:11PM +0000, Alan Gauld wrote: >> >> Somebody posted a question asking how to fond out if a file >> exists. The message was in the queue and I thought I'd approved >> it but it hasn't shown up yet. Sorry to the OP if I've messed up. >> >> The answer is that you use the os.path.exists() function. >> It takes a path as an argument which can be relative to >> the cwd or absolute. > > os.path.exists is a little bit of an anti-pattern though. It has two > problems: there is a race condition here, waiting to bite you. Just > because the file exists now, doesn't mean it will exist a millisecond > later when you try to open it. Also, even if the file exists, there is > no guarantee you can open it. > > > Code like this is buggy: > > > filename = raw_input("What file do you want to open? ") > if os.path.exists(filename): > with open(filename) as f: > text = f.read() > process(text) The potential bug is often a non-issue in simple use cases. Also it does read a little nicer to use an if statement than catching an exception. You may disagree but I find that in the earlier stages of teaching programming it's inappropriate to try and ensure that every suggestion is suitable for production code. > > The only use I have found for os.path.exists is to try to *avoid* an > existing file. (Even here, it is still subject to one of the above > problems: just because a file *doesn't* exist now, a millisecond it > might.) For example, automatically numbering files rather than > overwriting them: > > > filename = raw_input("Save file as...? ") > name, ext = os.path.splitext(filename) > n = 1 > while os.path.exists(filename): > # automatically pick a new name > filename = "%s~%d" % (name, n) > n += 1 > try: > with open(filename, 'w') as f: > f.write(text) > except (IOError, OSError): > pass > > > I'm not 100% happy with that solution, because there is a risk that some > other process will create a file with the same name in the gap between > calling os.path.exists and calling open, but I don't know how to solve > that race condition. What I really want is an option to open() that only > opens a new file, and fails if the file already exists. If you go to the file descriptor level then you can do def open_new(filename): fd = os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_EXCL) return os.fdopen(fd) which I think is what you want. Oscar From zachary.ware+pytut at gmail.com Tue Mar 10 02:51:52 2015 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Mon, 9 Mar 2015 20:51:52 -0500 Subject: [Tutor] file exists question In-Reply-To: <20150310002257.GW7655@ando.pearwood.info> References: <20150310002257.GW7655@ando.pearwood.info> Message-ID: On Mar 9, 2015 7:25 PM, "Steven D'Aprano" wrote: > What I really want is an option to open() that only > opens a new file, and fails if the file already exists. If I'm not mistaken, this is the 'x' open mode, added in Python 3.4 (or maybe 3.3, I forget). -- Zach From markos at c2o.pro.br Tue Mar 10 14:59:19 2015 From: markos at c2o.pro.br (Markos) Date: Tue, 10 Mar 2015 10:59:19 -0300 Subject: [Tutor] Idle - ImportError: No module named numpy In-Reply-To: References: <20150305122044.GK7655@ando.pearwood.info> <54F9F18D.80001@c2o.pro.br> <54FDE1C5.6000301@c2o.pro.br> Message-ID: <54FEF8B7.4060304@c2o.pro.br> On 09-03-2015 21:25, Oscar Benjamin wrote: > On 9 March 2015 at 18:09, Markos wrote: > >> I installed numpy in Debian (6.0) with the command: >> apt-get install python-numpy >> >> I just find that this package is exclusively for python 2.5 and 2.6 >> >> The idle3 use Python 3.1.3. >> >> How to install numpy for Python 3 in order to use it with the idle? >> >> Is there any simple way? >> > Hi Markos, > > I'm not sure about Squeeze but on this Ubuntu 14.04 system you can > install numpy for Python 3 by typing: > > $ sudo apt-get install python3-numpy > > The Python 3 version of the package is a relatively new addition to > the repositories though so it may not be available in Squeeze. > > If there is no package then you can build numpy yourself. It's not too > hard to do. First make sure that you have pip installed for python3. > You can check if it's installed by typing: > > $ python3 -m pip > > If it says > > /usr/bin/python3: No module named pip > > then you don't have pip. You may be able to install pip with > > $ sudo apt-get install python3-pip > > If not then you can install it by downloading the get-pip.py script from here: > > https://pip.pypa.io/en/latest/installing.html > > Once you have that you can run it as > > $ sudo python3 get-pip.py > > Once pip is installed you can use it to install lots of things > including numpy. For numpy in particular though you'll need to first > install the compilers and Python 3 header files: > > $ sudo apt-get install build-essential python3-dev > > Now you can use pip to install numpy for python3: > > $ sudo python3 -m pip install numpy > > This will download numpy from PyPI and then try to compile and install > it. Compiling numpy takes a while and you will see a lot of strange > compiler messages. If it fails then just post the very last part of > the output here. It will probably mean that you need to install more > compilers or something. > > If it succeeds then you should be able to import numpy e.g.: > > $ python3 > Python 3.4.0 (default, Apr 11 2014, 13:05:11) > [GCC 4.8.2] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> import numpy > >>> numpy.cos(1) > 0.54030230586813977 > > > Oscar > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > Hi. I don't have the python3-pip in the repository, then I downloaded the get-pip.py from: https://bootstrap.pypa.io/get-pip.py But when running the command "python3 get-pip.py" appears the error message (end of message). I found a message about a bug with pip at https://github.com/pypa/pip/issues/1093 Is it the same kind of error? Any tip? And another doubt. Is there any risk of numpy module to be used by python3 to conflict with the numpy module that is already installed for python2.5 and python2.6? I still don't know the structure of Python packages. Thank you very much for your detailed explanation. Markos # python3 get-pip.py Collecting pip Exception: Traceback (most recent call last): File "/tmp/tmpn0At3p/pip.zip/pip/basecommand.py", line 232, in main status = self.run(options, args) File "/tmp/tmpn0At3p/pip.zip/pip/commands/install.py", line 339, in run requirement_set.prepare_files(finder) File "/tmp/tmpn0At3p/pip.zip/pip/req/req_set.py", line 333, in prepare_files upgrade=self.upgrade, File "/tmp/tmpn0At3p/pip.zip/pip/index.py", line 305, in find_requirement page = self._get_page(main_index_url, req) File "/tmp/tmpn0At3p/pip.zip/pip/index.py", line 783, in _get_page return HTMLPage.get_page(link, req, session=self.session) File "/tmp/tmpn0At3p/pip.zip/pip/index.py", line 872, in get_page "Cache-Control": "max-age=600", File "/tmp/tmpn0At3p/pip.zip/pip/_vendor/requests/sessions.py", line 473, in get return self.request('GET', url, **kwargs) File "/tmp/tmpn0At3p/pip.zip/pip/download.py", line 365, in request return super(PipSession, self).request(method, url, *args, **kwargs) File "/tmp/tmpn0At3p/pip.zip/pip/_vendor/requests/sessions.py", line 461, in request resp = self.send(prep, **send_kwargs) File "/tmp/tmpn0At3p/pip.zip/pip/_vendor/requests/sessions.py", line 573, in send r = adapter.send(request, **kwargs) File "/tmp/tmpn0At3p/pip.zip/pip/_vendor/cachecontrol/adapter.py", line 43, in send resp = super(CacheControlAdapter, self).send(request, **kw) File "/tmp/tmpn0At3p/pip.zip/pip/_vendor/requests/adapters.py", line 370, in send timeout=timeout File "/tmp/tmpn0At3p/pip.zip/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 518, in urlopen body=body, headers=headers) File "/tmp/tmpn0At3p/pip.zip/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 322, in _make_request self._validate_conn(conn) File "/tmp/tmpn0At3p/pip.zip/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 727, in _validate_conn conn.connect() File "/tmp/tmpn0At3p/pip.zip/pip/_vendor/requests/packages/urllib3/connection.py", line 238, in connect ssl_version=resolved_ssl_version) File "/tmp/tmpn0At3p/pip.zip/pip/_vendor/requests/packages/urllib3/util/ssl_.py", line 254, in ssl_wrap_socket return context.wrap_socket(sock) File "/tmp/tmpn0At3p/pip.zip/pip/_vendor/requests/packages/urllib3/util/ssl_.py", line 80, in wrap_socket return wrap_socket(socket, ciphers=self.ciphers, **kwargs) TypeError: wrap_socket() got an unexpected keyword argument 'ciphers' From oscar.j.benjamin at gmail.com Tue Mar 10 20:48:07 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 10 Mar 2015 19:48:07 +0000 Subject: [Tutor] Idle - ImportError: No module named numpy In-Reply-To: <54FEF8B7.4060304@c2o.pro.br> References: <20150305122044.GK7655@ando.pearwood.info> <54F9F18D.80001@c2o.pro.br> <54FDE1C5.6000301@c2o.pro.br> <54FEF8B7.4060304@c2o.pro.br> Message-ID: On 10 March 2015 at 13:59, Markos wrote: > I don't have the python3-pip in the repository, then I downloaded the > get-pip.py from: > > https://bootstrap.pypa.io/get-pip.py > > But when running the command "python3 get-pip.py" appears the error message > (end of message). Looks like a bug in pip or in the get-pip script. What version of python3 are you using? You'll just need to install setuptools and pip the old-fashioned way. First check if you already have setuptools: $ python3 Python 3.4.0 (default, Apr 11 2014, 13:05:11) [GCC 4.8.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import setuptools >>> setuptools.__version__ '3.3' If not you can install it from here (download the .tar.gz file near the bottom of the page): https://pypi.python.org/pypi/setuptools Extract the .tar.gz file, cd into it and run $ sudo python3 setup.py install Then do the same for pip from here: https://pypi.python.org/pypi/pip Then you should be able to install things with $ python3 -m pip install where package_name would be e.g. numpy and you'll need to be root or use sudo. > I found a message about a bug with pip at > https://github.com/pypa/pip/issues/1093 > > Is it the same kind of error? I don't think so. > Any tip? See above. > And another doubt. > > Is there any risk of numpy module to be used by python3 to conflict with the > numpy module that is already installed for python2.5 and python2.6? No. As long as you install setuptools and pip with "python3 setup.py install" and install numpy with "python3 -m pip install numpy" then your Python 2.x installations will be unaffected. Oscar From alan.gauld at btinternet.com Tue Mar 10 21:07:31 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 10 Mar 2015 20:07:31 +0000 Subject: [Tutor] Idle - ImportError: No module named numpy In-Reply-To: <54FEF8B7.4060304@c2o.pro.br> References: <20150305122044.GK7655@ando.pearwood.info> <54F9F18D.80001@c2o.pro.br> <54FDE1C5.6000301@c2o.pro.br> <54FEF8B7.4060304@c2o.pro.br> Message-ID: On 10/03/15 13:59, Markos wrote: >> I'm not sure about Squeeze but on this Ubuntu 14.04 system you can >> install numpy for Python 3 by typing: >> >> $ sudo apt-get install python3-numpy >> >> The Python 3 version of the package is a relatively new addition to >> the repositories though so it may not be available in Squeeze. Did you try this route first? Its by far the easiest way to do it. Or you could download and install Anaconda (or Canopy) which has SciPy (including Numpy) installed already! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From roizland at yahoo.com Wed Mar 11 04:25:52 2015 From: roizland at yahoo.com (Elroi Henry) Date: Wed, 11 Mar 2015 03:25:52 +0000 (UTC) Subject: [Tutor] Calculating and displaying students record in Python v12 (Odoo) Message-ID: <923929134.2299296.1426044352175.JavaMail.yahoo@mail.yahoo.com> ?Hello everyone, am a newbie in python programming language. I am working on a student management system for a primary school using Odoo (Python v12). please am having problems with the report as certain fields are not being calculated. Here is the link to the image of what I'm trying to achieve as well as the source code (http://jmp.sh/Shn8gHe?) I need the result to calculate and display the child's Position in that particular subject and particular school year in the field "Pos"; the highest average in the class for that particular subject and particular school year in the field "Highest in class"; The average of the whole class for a particular subject for that particular year in the field "Class Average";. Like always, am forever indebted to my teachers, thank you all.There is a perfect distinction between Standard of living and Satandard of Life, Material things may boost-up your standard of living, but it may never make-up for a good standard of life. From duxbuz at hotmail.com Wed Mar 11 15:08:18 2015 From: duxbuz at hotmail.com (Ian D) Date: Wed, 11 Mar 2015 14:08:18 +0000 Subject: [Tutor] Tuple indexing Message-ID: Hi I have seen some examples that seem to use a tuple with a method called index() The original code I was looking at had this sort of thing: SENSORS = ('sensor1', 'sensor2') pin_index = SENSORS.index("sensor1") so the result is that pin_index the is equal to 0 I then read that a Tuple has no attribute index on this site http://www.diveintopython.net/native_data_types/tuples.html >>> t.index("example") Traceback (innermost last): File "", line 1, in ? AttributeError: 'tuple' object has no attribute 'index' The example seems to work with 2.7 and 3.3 for me. But I don't find much documentation on indexing Tuples using this method. I am just wondering if there is more specific documentation on using Tuples this way Thanks. I am using Python 2.7 at moment. From joel.goldstick at gmail.com Wed Mar 11 15:39:05 2015 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Wed, 11 Mar 2015 10:39:05 -0400 Subject: [Tutor] Tuple indexing In-Reply-To: References: Message-ID: On Wed, Mar 11, 2015 at 10:08 AM, Ian D wrote: > Hi > > > > I have seen some examples that seem to use a tuple with a method called index() > > > The original code I was looking at had this sort of thing: > > > > SENSORS = ('sensor1', 'sensor2') > > > SENSORS[0] > pin_index = SENSORS.index("sensor1") > > > so the result is that pin_index the is equal to 0 > > > I then read that a Tuple has no attribute index on this site http://www.diveintopython.net/native_data_types/tuples.html > > >>>> t.index("example") > Traceback (innermost last): File "", line 1, in ? AttributeError: 'tuple' object has no attribute 'index' > > > > The example seems to work with 2.7 and 3.3 for me. > > > > > > But I don't find much documentation on indexing Tuples using this method. > > > > I am just wondering if there is more specific documentation on using Tuples this way > https://docs.python.org/2/library/stdtypes.html#sequence-types-str-unicode-list-tuple-bytearray-buffer-xrange It wasn't around until 2.4 according to what I read somewhere > > > Thanks. > > > > I am using Python 2.7 at moment. > > > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com From __peter__ at web.de Wed Mar 11 16:35:09 2015 From: __peter__ at web.de (Peter Otten) Date: Wed, 11 Mar 2015 16:35:09 +0100 Subject: [Tutor] Tuple indexing References: Message-ID: Ian D wrote: > Hi > > > > I have seen some examples that seem to use a tuple with a method called > index() > > > The original code I was looking at had this sort of thing: > > > Hi Ian! Please don't use that much whitespace. It makes your post hard and unpleasant to read. Thank you. > SENSORS = ('sensor1', 'sensor2') > > > > pin_index = SENSORS.index("sensor1") > > > so the result is that pin_index the is equal to 0 > > > I then read that a Tuple has no attribute index on this site > http://www.diveintopython.net/native_data_types/tuples.html Dive into Python is quite old. >>>> t.index("example") > Traceback (innermost last): File "", line 1, in ? > AttributeError: 'tuple' object has no attribute 'index' > > > > The example seems to work with 2.7 and 3.3 for me. The tuple.index() method has been added in Python 2.6: https://docs.python.org/2.6/whatsnew/2.6.html """ Tuples now have index() and count() methods matching the list type?s index() and count() methods: >>> t = (0,1,2,3,4,0,1,2) >>> t.index(3) 3 >>> t.count(0) 2 (Contributed by Raymond Hettinger) """ > But I don't find much documentation on indexing Tuples using this method. > I am just wondering if there is more specific documentation on using > Tuples this way Many Python coders don't use tuples to look up an item index; the traditional application is to pass multiple values around, e. g. def extent(thing): x = calculate_width(thing) y = calculate_height(thing) return x, y width, height = extent(picture) portrait = width < height In the example you have to know the index beforehand, by reading the code or its documentation rather than going through all items for a matching item. When you want to treat all items in a tuple uniformly in most cases using a tuple is a premature optimisation; use a list or set unless you can name a compelling reason not to. Your sensors example could be solved with a dict: sensors = {"sensor1": 0, "sensor2": 1} pin_index = sensors["sensor1"] This approach will still work well for huge numbers of sensors (constant time or O(1)), unlike tuple/list.index() where the average lookup time grows with the number of items in the tuple or list (O(n)). From alan.gauld at btinternet.com Wed Mar 11 18:35:08 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 11 Mar 2015 17:35:08 +0000 Subject: [Tutor] Idle - ImportError: No module named numpy In-Reply-To: References: <20150305122044.GK7655@ando.pearwood.info> <54F9F18D.80001@c2o.pro.br> <54FDE1C5.6000301@c2o.pro.br> <54FEF8B7.4060304@c2o.pro.br> <54FF8472.7040800@btinternet.com> Message-ID: <55007CCC.4000803@btinternet.com> Please use ReplyAll to include the tutor list On 11/03/15 02:00, Jack Potrashitel wrote: > Ok , thanks a lot. But I still in doubts :(. > if self.root is None: > self.root = BinaryNode( value ) > else: > self.root.add( value ) > the object is created as BinaryNode(value ) only if self.root is None . That's correct. But root will only be None on the very first call to add(). After the first call the root attribute will already be set to a BinaryNode (from the first call) so you just keep calling add() on that same root instance. Remember that self.root is part of the object so it will remain active for as long as the BinaryTree object is active. It exists even between method invocations. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Wed Mar 11 19:49:20 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 11 Mar 2015 18:49:20 +0000 Subject: [Tutor] Calculating and displaying students record in Python v12 (Odoo) In-Reply-To: <923929134.2299296.1426044352175.JavaMail.yahoo@mail.yahoo.com> References: <923929134.2299296.1426044352175.JavaMail.yahoo@mail.yahoo.com> Message-ID: On 11/03/15 03:25, Elroi Henry wrote: > I am working on a student management system for a primary school using Odoo (Python v12). I assume that's Odoo v12? Python is only at version 3.5... > please am having problems with the report as certain fields are not being calculated. This list is for people learning Python and the standard Library. It is not reasonable to expect an answer on something so obviously Odoo specific. I had a look at your code but it is not clear how the framework functions, which methods do what etc. If you really think this is a Python issue, rather than an Odoo one, you will need to extract the relevant piece of code and ask your question again related to that code. Otherwise you are probably better off asking on the Odoo support forum. Or failing that you could try the main Python list. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From markos at c2o.pro.br Wed Mar 11 20:43:00 2015 From: markos at c2o.pro.br (Markos) Date: Wed, 11 Mar 2015 16:43:00 -0300 Subject: [Tutor] Idle - ImportError: No module named numpy In-Reply-To: References: <20150305122044.GK7655@ando.pearwood.info> <54F9F18D.80001@c2o.pro.br> <54FDE1C5.6000301@c2o.pro.br> <54FEF8B7.4060304@c2o.pro.br> Message-ID: <55009AC4.5070601@c2o.pro.br> On 10-03-2015 16:48, Oscar Benjamin wrote: > On 10 March 2015 at 13:59, Markos wrote: > >> I don't have the python3-pip in the repository, then I downloaded the >> get-pip.py from: >> >> https://bootstrap.pypa.io/get-pip.py >> >> But when running the command "python3 get-pip.py" appears the error message >> (end of message). >> > Looks like a bug in pip or in the get-pip script. What version of > python3 are you using? > > You'll just need to install setuptools and pip the old-fashioned way. > > First check if you already have setuptools: > > $ python3 > Python 3.4.0 (default, Apr 11 2014, 13:05:11) > [GCC 4.8.2] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> import setuptools > >>> setuptools.__version__ > '3.3' > > If not you can install it from here (download the .tar.gz file near > the bottom of the page): > > https://pypi.python.org/pypi/setuptools > > Extract the .tar.gz file, cd into it and run > > $ sudo python3 setup.py install > > Then do the same for pip from here: > > https://pypi.python.org/pypi/pip > > Then you should be able to install things with > > $ python3 -m pip install > > where package_name would be e.g. numpy and you'll need to be root or use sudo. > > >> I found a message about a bug with pip at >> https://github.com/pypa/pip/issues/1093 >> >> Is it the same kind of error? >> > I don't think so. > > >> Any tip? >> > See above. > > >> And another doubt. >> >> Is there any risk of numpy module to be used by python3 to conflict with the >> numpy module that is already installed for python2.5 and python2.6? >> > No. As long as you install setuptools and pip with "python3 setup.py > install" and install numpy with "python3 -m pip install numpy" then > your Python 2.x installations will be unaffected. > > > Oscar > > Hi Oscar, I downloaded the file https://pypi.python.org/packages/source/s/setuptools/setuptools-14.0.tar.gz tar -xzvf setuptools-14.0.tar.gz cd setuptools-14.0 python3 setup.py install Then I downloaded the file https://pypi.python.org/packages/source/p/pip/pip-6.0.8.tar.gz tar -xzvf pip-6.0.8.tar.gz cd pip-6.0.8 python3 setup.py install And the command python3 -m pip list pip (6.0.8) setuptools (14.0) But the command to install numpy gave an error message: # python3 -m pip install numpy Collecting numpy Exception: Traceback (most recent call last): File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/basecommand.py", line 232, in main status = self.run(options, args) File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/commands/install.py", line 339, in run requirement_set.prepare_files(finder) File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/req/req_set.py", line 333, in prepare_files upgrade=self.upgrade, File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/index.py", line 305, in find_requirement page = self._get_page(main_index_url, req) File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/index.py", line 783, in _get_page return HTMLPage.get_page(link, req, session=self.session) File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/index.py", line 872, in get_page "Cache-Control": "max-age=600", File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/_vendor/requests/sessions.py", line 473, in get return self.request('GET', url, **kwargs) File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/download.py", line 365, in request return super(PipSession, self).request(method, url, *args, **kwargs) File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/_vendor/requests/sessions.py", line 461, in request resp = self.send(prep, **send_kwargs) File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/_vendor/requests/sessions.py", line 573, in send r = adapter.send(request, **kwargs) File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/_vendor/cachecontrol/adapter.py", line 43, in send resp = super(CacheControlAdapter, self).send(request, **kw) File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/_vendor/requests/adapters.py", line 370, in send timeout=timeout File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 518, in urlopen body=body, headers=headers) File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 322, in _make_request self._validate_conn(conn) File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/_vendor/requests/packages/urllib3/connectionpool.py", line 727, in _validate_conn conn.connect() File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/_vendor/requests/packages/urllib3/connection.py", line 238, in connect ssl_version=resolved_ssl_version) File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py", line 254, in ssl_wrap_socket return context.wrap_socket(sock) File "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/_vendor/requests/packages/urllib3/util/ssl_.py", line 80, in wrap_socket return wrap_socket(socket, ciphers=self.ciphers, **kwargs) TypeError: wrap_socket() got an unexpected keyword argument 'ciphers' Should I use older versions of pip and setuptools compatible with Debian 6? Thanks for your attention, Markos From steve at pearwood.info Thu Mar 12 01:30:09 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 12 Mar 2015 11:30:09 +1100 Subject: [Tutor] Tuple indexing In-Reply-To: References: Message-ID: <20150312003008.GD7655@ando.pearwood.info> On Wed, Mar 11, 2015 at 02:08:18PM +0000, Ian D wrote: [...] > I then read that a Tuple has no attribute index on this site > http://www.diveintopython.net/native_data_types/tuples.html That's pretty old, and written by an independent author. The official documentation for Python is here: https://docs.python.org/2/index.html https://docs.python.org/3/index.html You should start there for any questions, not books written by some random person on the Internet. (Actually, in the case of Dive Into Python, no longer on the Internet. The original author, Mark Pilgrim, famously "disappeared" from the Internet a few years ago, deleting almost every trace of his life, removing email addresses, closing down accounts, etc.) You can also use the help() function in the Python interactive interpreter: at the Python prompt, run: help(tuple) to see what methods are available. -- Steve From saphirmartin at gmail.com Wed Mar 11 22:26:38 2015 From: saphirmartin at gmail.com (Wibble) Date: Wed, 11 Mar 2015 21:26:38 +0000 Subject: [Tutor] How to handle a non integer within a question function that needs to return a integer? Message-ID: <5500B30E.3090700@gmail.com> Dear All Very basic question from a newbie. I have defined a function for asking a question within a range def user_choice(question, low, high, step = 1): """Define user choice""" choice = None while choice not in range(low, high, step): choice = int(input(question)) return choice If user enters a string rather than a number it creates this error Traceback (most recent call last): File "/home/wibble/bhdevices.py", line 100, in choice = user_choice("Choose an option: ", 0, CHOICES) File "/home/wibble/bhdevices.py", line 53, in user_choice choice = int(input(question)) ValueError: invalid literal for int() with base 10: 'd' How do I make it show a message to users saying only numbers excepted and then the loop continues? I have tried this but same error is still generated. def user_choice(question, low, high, step = 1): """Define user choice""" choice = None while choice not in range(low, high, step): choice = int(input(question)) if choice == str(input(question)): print('Numbers only please!') return choice Do I need to create another while loop within this loop to handle a string input? Any assistance would be greatly appreciated. Kind regards Andrew. From wisdomofmetis at gmail.com Thu Mar 12 00:21:40 2015 From: wisdomofmetis at gmail.com (metis wisdom) Date: Thu, 12 Mar 2015 01:21:40 +0200 Subject: [Tutor] =?utf-8?q?How_linux_software_centers_make_=E2=80=9Csearch?= =?utf-8?b?4oCdIG9wZXJhdGlvbj8=?= Message-ID: Hello, I want to develop a software center in Ubuntu similar to Ubuntu software center. In ubuntu software center, when we type a keyword and hits enter button, it displays us the related results. For example, when i searched for "eclipse" keyword, 5 result are listed in ubuntu software center. I want to do the similar functionality in my software center. I tried to solve this problem by making search in apt package manager using bash command(apt search package_name), but it gives all packages as result, approximately more than 100 packages. How ubuntu software center and other software centers search a keyword? Where do they search the keyword and retrieve results? Source code of ubuntu software center is so complex and i cannot find what i need. Any guide will be appreciated. Thanks in advance. I analysed all source code of ubuntu software center. These codes includes so many technologies that it is very hard to understand it. IN order to understand these codes, i have to learn many technologies , it may take at least one month,maybe this time may not be enough. After i spent so many times learning these technologies, what if these technologies does not solve my problem? I know only python, i am not familiar with os library etc, and i have a limited time, please guide me. I need to build a simple software center, not a sophisticated one. From alan.gauld at btinternet.com Thu Mar 12 01:50:07 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 12 Mar 2015 00:50:07 +0000 Subject: [Tutor] How to handle a non integer within a question function that needs to return a integer? In-Reply-To: <5500B30E.3090700@gmail.com> References: <5500B30E.3090700@gmail.com> Message-ID: On 11/03/15 21:26, Wibble wrote: > def user_choice(question, low, high, step = 1): > """Define user choice""" > choice = None > while choice not in range(low, high, step): > choice = int(input(question)) > return choice > > If user enters a string rather than a number it creates this error > > Traceback (most recent call last):... > choice = int(input(question)) > ValueError: invalid literal for int() with base 10: 'd' > > How do I make it show a message to users saying only numbers excepted > and then the loop continues? Use a try/except construct inside your while loop. Something like (untested): def user_choice(question, low, high, step = 1): """Define user choice""" choice = None while choice not in range(low, high, step): try: choice = int(input(question)) except ValueError: print("You must enter a number between ",low," and ", high) return choice HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From davea at davea.name Thu Mar 12 02:11:18 2015 From: davea at davea.name (Dave Angel) Date: Wed, 11 Mar 2015 21:11:18 -0400 Subject: [Tutor] =?windows-1252?q?How_linux_software_centers_make_=93searc?= =?windows-1252?q?h=94_operation=3F?= In-Reply-To: References: Message-ID: <5500E7B6.5000705@davea.name> On 03/11/2015 07:21 PM, metis wisdom wrote: > Hello, I want to develop a software center in Ubuntu similar to Ubuntu > software center. You forgot the rest of the caps. It's "Uuntu Software Center". Why? Is there something wrong with what it does, that you need something different? Is this actually a school assignment? What was the actual assignment you were given, and when is it due? What course material have you already covered? Are you supposed to make this software work with live Linux data, or with some madeup samples? > In ubuntu software center, when we type a keyword and hits > enter button, it displays us the related results. For example, when i > searched for "eclipse" keyword, 5 result are listed in ubuntu software > center. Is that good, or do you wish it showed 100 results? > > I want to do the similar functionality in my software center. Why? > > I tried to solve this problem by making search in apt package manager using > bash command(apt search package_name), but it gives all packages as result, > approximately more than 100 packages. > > How ubuntu software center and other software centers search a keyword? What's a software center? Now you're using the term as though it's a generic term, rather than the name of a particular software package. > Where do they search the keyword and retrieve results? They probably issue an SQL command. to some database. Or maybe they issue some Perl code that looks something up in a hash table. Or maybe a SOAP call. Does it matter? > Source code of > ubuntu software center is so complex and i cannot find what i need. Any > guide will be appreciated. Thanks in advance. You've looked at the source? What languages is it implemented in? You mention technologies, but not what they are. > > I analysed all source code of ubuntu software center. These codes includes > so many technologies that it is very hard to understand it. IN order to > understand these codes, i have to learn many technologies , it may take at > least one month,maybe this time may not be enough. After i spent so many > times learning these technologies, what if these technologies does not > solve my problem? I know only python, i am not familiar with os library > etc, and i have a limited time, please guide me. I need to build a simple > software center, not a sophisticated one. -- DaveA From ben+python at benfinney.id.au Thu Mar 12 02:21:13 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 12 Mar 2015 12:21:13 +1100 Subject: [Tutor] =?utf-8?q?How_linux_software_centers_make_=E2=80=9Csearch?= =?utf-8?b?4oCdICBvcGVyYXRpb24/?= References: Message-ID: <85r3svui2e.fsf@benfinney.id.au> metis wisdom writes: > Hello, I want to develop a software center in Ubuntu similar to Ubuntu > software center. Similar, but different in what way? > In ubuntu software center, when we type a keyword and hits enter > button, it displays us the related results. For example, when i > searched for "eclipse" keyword, 5 result are listed in ubuntu software > center. > > I want to do the similar functionality in my software center. How is your code currently getting its results? Can you show a *very small* and *simple* example of your Python code that is doing this? -- \ ?I never forget a face, but in your case I'll be glad to make | `\ an exception.? ?Groucho Marx | _o__) | Ben Finney From steve at pearwood.info Thu Mar 12 02:21:27 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 12 Mar 2015 12:21:27 +1100 Subject: [Tutor] =?utf-8?q?How_linux_software_centers_make_=E2=80=9Csearch?= =?utf-8?b?4oCdIG9wZXJhdGlvbj8=?= In-Reply-To: References: Message-ID: <20150312012127.GE7655@ando.pearwood.info> On Thu, Mar 12, 2015 at 01:21:40AM +0200, metis wisdom wrote: > Hello, I want to develop a software center in Ubuntu similar to Ubuntu > software center. In ubuntu software center, when we type a keyword and hits > enter button, it displays us the related results. For example, when i > searched for "eclipse" keyword, 5 result are listed in ubuntu software > center. > > I want to do the similar functionality in my software center. What is your software center? I don't even know what a software center is, and I don't use Ubuntu. This is not an Ubuntu help forum, why do you think we can answer these questions? > I tried to solve this problem by making search in apt package manager using > bash command(apt search package_name), but it gives all packages as result, > approximately more than 100 packages. That's not "all packages", there are thousands of packages in Ubuntu. If you expect only five results, but get 100, then you are probably not using apt search correctly. You should ask some experts on apt, or on an Ubuntu mailing list, or just read the man pages: man apt Have you tried using aptitude instead of apt? It might be better for you. Or maybe the Ununtu software center applies additional filtering to the list. I have no idea, you say you have analysed the source code so you should have a better idea than we do. But my guess is that it calls apt with your search terms, then filters the results somehow. Maybe by only returning the packages with Eclipse in the name, not the description? But I'm only guessing, because I haven't used apt in any detail. > How ubuntu software center and other software centers search a keyword? > Where do they search the keyword and retrieve results? Source code of > ubuntu software center is so complex and i cannot find what i need. Any > guide will be appreciated. Thanks in advance. Just because we program in Python doesn't mean we understand everything about all technologies. You probably know more about Ubuntu software center than we do. Some of us are Windows users, or Red Hat/Centos, or Mac users -- we're not experts on Ubuntu or Ubuntu software. > I analysed all source code of ubuntu software center. These codes includes > so many technologies that it is very hard to understand it. IN order to > understand these codes, i have to learn many technologies , it may take at > least one month,maybe this time may not be enough. Life is hard, and technology is complex. There are no magic short-cuts to understanding. > After i spent so many > times learning these technologies, what if these technologies does not > solve my problem? What is your problem? What exactly are you trying to do, and how is it related to Python? > I know only python, i am not familiar with os library > etc, and i have a limited time, please guide me. I need to build a simple > software center, not a sophisticated one. -- Steve From steve at pearwood.info Thu Mar 12 02:49:06 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 12 Mar 2015 12:49:06 +1100 Subject: [Tutor] file exists question In-Reply-To: References: <20150310002257.GW7655@ando.pearwood.info> Message-ID: <20150312014906.GF7655@ando.pearwood.info> On Mon, Mar 09, 2015 at 08:51:52PM -0500, Zachary Ware wrote: > On Mar 9, 2015 7:25 PM, "Steven D'Aprano" wrote: > > What I really want is an option to open() that only > > opens a new file, and fails if the file already exists. > > If I'm not mistaken, this is the 'x' open mode, added in Python 3.4 (or > maybe 3.3, I forget). That's exactly what I want! Thanks for pointing that out. -- Steve From steve at pearwood.info Thu Mar 12 03:16:07 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 12 Mar 2015 13:16:07 +1100 Subject: [Tutor] How to handle a non integer within a question function that needs to return a integer? In-Reply-To: <5500B30E.3090700@gmail.com> References: <5500B30E.3090700@gmail.com> Message-ID: <20150312021607.GG7655@ando.pearwood.info> On Wed, Mar 11, 2015 at 09:26:38PM +0000, Wibble wrote: [...] > How do I make it show a message to users saying only numbers excepted > and then the loop continues? > > I have tried this but same error is still generated. > > def user_choice(question, low, high, step = 1): > """Define user choice""" > choice = None > while choice not in range(low, high, step): > choice = int(input(question)) Your error occurs at the line above. > if choice == str(input(question)): > print('Numbers only please!') And here you try to detect the error, but it's too late, since it has already happened! > return choice > > Do I need to create another while loop within this loop to handle a > string input? Yes you do, because the user might be stupid, clumsy, obnoxious, or a cat walking on the keyboard, and might repeatedly type non-numbers. Or they might innocently try typing number words like "one", "two", etc. So let's break the question down. You want to give the user the option to enter a number, then you want to check that it is within a range. Let's start by accepting any old number: # Python 3 version! # for Python 2, make sure you use raw_input() not input() def enter_number(prompt): if not prompt.endswith(" "): # Make sure there is at least one space between the prompt # and where the user types. prompt += " " answer = input(prompt) while not answer.isdigit(): print("You must enter a number!") answer = input(prompt) return int(answer) If you try that, you will find a couple of limitations: - it doesn't accept negative numbers; - it doesn't accept floating point numbers like 1.2 or 3e5. Here's another way to write the same function. It's a bit more verbose, but probably a better design: def enter_number(prompt): """Ask the user to enter an integer number. Takes a single argument, the string to use to prompt the user. This repeats until they enter an actual number. """ if not prompt.endswith(" "): # Make sure there is at least one space between the prompt # and where the user types. prompt += " " while True: # Loop forever. answer = input(prompt) # use raw_input in Python 2 try: number = int(answer) except ValueError: print("You must enter a number!") else: return number This loops forever, asking the user for a number, then trying to convert their answer into a number inside a `try` block. If the conversion fails, the `except` block runs, and the loop continues. But if the conversion succeeds, the `else` block runs, which returns the number, thus breaking the infinite loop. Now you can use that as a building block to write the function you want: def user_choice(question, low, high, step = 1): """A better docstring would be appropriate.""" choice = None while choice not in range(low, high, step): choice = enter_number(question) return choice There's a problem with this function too -- if the user enters a number out of range, they don't get any feedback as to why their answer is rejected, instead they just get asked for a number again. Very disheartening! How about this? def user_choice(question, num, high=None): """A better docstring would be appropriate.""" if high is None: start = 1 end = num else: start = num end = high allowed = range(start, end+1) while True: choice = enter_number(question) if choice in allowed: return choice else: print("Out of range! Try again.") -- Steve From duxbuz at hotmail.com Thu Mar 12 11:22:57 2015 From: duxbuz at hotmail.com (Ian D) Date: Thu, 12 Mar 2015 10:22:57 +0000 Subject: [Tutor] Tuple indexing In-Reply-To: References: , Message-ID: Thanks. My original approach was to use a list as it seemed to lend itself to that. But as I referred back to the code I was adapting from I ended up using that example with a Tuple. I may look at changing it again, but for now I need the thing to work asap. ---------------------------------------- > To: tutor at python.org > From: __peter__ at web.de > Date: Wed, 11 Mar 2015 16:35:09 +0100 > Subject: Re: [Tutor] Tuple indexing > > Ian D wrote: > >> Hi >> >> >> >> I have seen some examples that seem to use a tuple with a method called >> index() >> >> >> The original code I was looking at had this sort of thing: >> >> >> > > Hi Ian! Please don't use that much whitespace. It makes your post hard and > unpleasant to read. Thank you. > > >> SENSORS = ('sensor1', 'sensor2') >> >> >> >> pin_index = SENSORS.index("sensor1") >> >> >> so the result is that pin_index the is equal to 0 >> >> >> I then read that a Tuple has no attribute index on this site >> http://www.diveintopython.net/native_data_types/tuples.html > > Dive into Python is quite old. > >>>>> t.index("example") >> Traceback (innermost last): File "", line 1, in ? >> AttributeError: 'tuple' object has no attribute 'index' >> >> >> >> The example seems to work with 2.7 and 3.3 for me. > > The tuple.index() method has been added in Python 2.6: > > https://docs.python.org/2.6/whatsnew/2.6.html > > """ > Tuples now have index() and count() methods matching the list type?s index() > and count() methods: > >>>> t = (0,1,2,3,4,0,1,2) >>>> t.index(3) > 3 >>>> t.count(0) > 2 > (Contributed by Raymond Hettinger) > """ > >> But I don't find much documentation on indexing Tuples using this method. >> I am just wondering if there is more specific documentation on using >> Tuples this way > > Many Python coders don't use tuples to look up an item index; the > traditional application is to pass multiple values around, e. g. > > def extent(thing): > x = calculate_width(thing) > y = calculate_height(thing) > return x, y > > width, height = extent(picture) > > portrait = width < height > > In the example you have to know the index beforehand, by reading the code or > its documentation rather than going through all items for a matching item. > > When you want to treat all items in a tuple uniformly in most cases using a > tuple is a premature optimisation; use a list or set unless you can name a > compelling reason not to. > > Your sensors example could be solved with a dict: > > sensors = {"sensor1": 0, "sensor2": 1} > > pin_index = sensors["sensor1"] > > This approach will still work well for huge numbers of sensors (constant > time or O(1)), unlike tuple/list.index() where the average lookup time grows > with the number of items in the tuple or list (O(n)). > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From beachkidken at gmail.com Fri Mar 13 14:57:25 2015 From: beachkidken at gmail.com (Ken G.) Date: Fri, 13 Mar 2015 09:57:25 -0400 Subject: [Tutor] Rearranging a list of numbers with corresponding index Message-ID: <5502ECC5.8090500@gmail.com> I have been keeping track of numbers drawn in our local lotto drawings into a list format as shown in a short example below. Using such list, I am able to determine how often a number appears within the last 100 plus drawings. The length of my lists range from 5, 15, 35, 59and 75 long. I will give an example of one of my short list. PowerPlay = [0, 0, 61, 32, 11, 14] Disregarding index 0 and 1, I can see and print the following: Index Number 2 61 3 32 4 11 5 14 showing that number 2 appears 61 times, number 3 appears 32 times, number 4 appears 11 times and number 5 appears 14 times within last 100 plus drawings. How I best rearrange the numbers from high to low with its corresponding index number such as below: Number Index 61 2 32 3 14 5 11 4 showing the number 2 appears 61 times, number 3 appears 32 times, number 5 appears 14 times and number 4 appears 11 times. I know that using MegaBall.reverse() sort the number from high to low but the index numbers still remain in the same positions. Thanks for pointing out the way to do this. Ken From __peter__ at web.de Fri Mar 13 15:21:23 2015 From: __peter__ at web.de (Peter Otten) Date: Fri, 13 Mar 2015 15:21:23 +0100 Subject: [Tutor] Rearranging a list of numbers with corresponding index References: <5502ECC5.8090500@gmail.com> Message-ID: Ken G. wrote: > I have been keeping track of numbers drawn in our local lotto drawings > into a list format as shown in a short example below. Using such list, I > am able to determine how often a number appears within the last 100 plus > drawings. > > The length of my lists range from 5, 15, 35, 59and 75 long. I will give > an example of one of my short list. > > PowerPlay = [0, 0, 61, 32, 11, 14] > > Disregarding index 0 and 1, I can see and print the following: > > Index Number > 2 61 > 3 32 > 4 11 > 5 14 > > showing that number 2 appears 61 times, number 3 appears 32 times, > number 4 appears 11 times and number 5 appears 14 times within last 100 > plus drawings. > > How I best rearrange the numbers from high to low with its corresponding > index number such as below: > > Number Index > 61 2 > 32 3 > 14 5 > 11 4 > > showing the number 2 appears 61 times, number 3 appears 32 times, number > 5 appears 14 times and number 4 appears 11 times. I know that using > > MegaBall.reverse() > > sort the number from high to low but the index numbers still remain in > the same positions. > > Thanks for pointing out the way to do this. The initial list: >>> power_play = [0, 0, 61, 32, 11, 14] Use enumerate() to get index, frequency pairs: >>> list(enumerate(power_play)) [(0, 0), (1, 0), (2, 61), (3, 32), (4, 11), (5, 14)] Sort the pairs: >>> sorted(enumerate(power_play)) [(0, 0), (1, 0), (2, 61), (3, 32), (4, 11), (5, 14)] Provide a key to sort after the second value, i. e. the frequency in the (index, frequency) pairs: >>> def second_value(item): ... return item[1] ... >>> sorted(enumerate(power_play), key=second_value) [(0, 0), (1, 0), (4, 11), (5, 14), (3, 32), (2, 61)] Note that instead of writing your own custom function or lambda you could also use operator.itemgetter(1) from the operator module in the stdlib. Sort in reverse order: >>> sorted(enumerate(power_play), key=second_value, reverse=True) [(2, 61), (3, 32), (5, 14), (4, 11), (0, 0), (1, 0)] Print the output for frequencies > 0: >>> pairs = sorted(enumerate(power_play), key=second_value, reverse=True) >>> for index, freq in pairs: ... if freq == 0: ... break ... print(freq, index, sep="\t") ... 61 2 32 3 14 5 11 4 From davea at davea.name Fri Mar 13 15:38:56 2015 From: davea at davea.name (Dave Angel) Date: Fri, 13 Mar 2015 10:38:56 -0400 Subject: [Tutor] Rearranging a list of numbers with corresponding index In-Reply-To: <5502ECC5.8090500@gmail.com> References: <5502ECC5.8090500@gmail.com> Message-ID: <5502F680.8090406@davea.name> On 03/13/2015 09:57 AM, Ken G. wrote: > I have been keeping track of numbers drawn in our local lotto drawings > into a list format as shown in a short example below. Using such list, I > am able to determine how often a number appears within the last 100 plus > drawings. > > The length of my lists range from 5, 15, 35, 59and 75 long. I will give > an example of one of my short list. > > PowerPlay = [0, 0, 61, 32, 11, 14] > > Disregarding index 0 and 1, I can see and print the following: > > Index Number > 2 61 > 3 32 > 4 11 > 5 14 > > showing that number 2 appears 61 times, number 3 appears 32 times, > number 4 appears 11 times and number 5 appears 14 times within last 100 > plus drawings. > > How I best rearrange the numbers from high to low with its corresponding > index number such as below: > > Number Index > 61 2 > 32 3 > 14 5 > 11 4 > > showing the number 2 appears 61 times, number 3 appears 32 times, number > 5 appears 14 times and number 4 appears 11 times. I know that using > > MegaBall.reverse() > > sort the number from high to low but the index numbers still remain in > the same positions. > > Thanks for pointing out the way to do this. > Make a list of tuples by doing something like: new_list = [ (cnt, index) for index, cnt in enumerate(power_play) ] to produce: [ (0, 0), (0, 1), (61, 2), (32, 3), (11, 4), (14, 5) ] then sort it with reverse=True. When you print it, just omit the items that have zero as their count. (they'll be at the end, anyway) Lots of other variants, some more concise. And other approaches might be cleaner if we change how we generate the list. (I tried it briefly using Python 3.4) -- DaveA From beachkidken at gmail.com Fri Mar 13 15:43:01 2015 From: beachkidken at gmail.com (Ken G.) Date: Fri, 13 Mar 2015 10:43:01 -0400 Subject: [Tutor] Rearranging a list of numbers with corresponding index In-Reply-To: References: <5502ECC5.8090500@gmail.com> Message-ID: <5502F775.5040204@gmail.com> On 03/13/2015 10:21 AM, Peter Otten wrote: > Ken G. wrote: > >> I have been keeping track of numbers drawn in our local lotto drawings >> into a list format as shown in a short example below. Using such list, I >> am able to determine how often a number appears within the last 100 plus >> drawings. >> >> The length of my lists range from 5, 15, 35, 59and 75 long. I will give >> an example of one of my short list. >> >> PowerPlay = [0, 0, 61, 32, 11, 14] >> >> Disregarding index 0 and 1, I can see and print the following: >> >> Index Number >> 2 61 >> 3 32 >> 4 11 >> 5 14 >> >> showing that number 2 appears 61 times, number 3 appears 32 times, >> number 4 appears 11 times and number 5 appears 14 times within last 100 >> plus drawings. >> >> How I best rearrange the numbers from high to low with its corresponding >> index number such as below: >> >> Number Index >> 61 2 >> 32 3 >> 14 5 >> 11 4 >> >> showing the number 2 appears 61 times, number 3 appears 32 times, number >> 5 appears 14 times and number 4 appears 11 times. I know that using >> >> MegaBall.reverse() >> >> sort the number from high to low but the index numbers still remain in >> the same positions. >> >> Thanks for pointing out the way to do this. > The initial list: > >>>> power_play = [0, 0, 61, 32, 11, 14] > Use enumerate() to get index, frequency pairs: > >>>> list(enumerate(power_play)) > [(0, 0), (1, 0), (2, 61), (3, 32), (4, 11), (5, 14)] > > Sort the pairs: > >>>> sorted(enumerate(power_play)) > [(0, 0), (1, 0), (2, 61), (3, 32), (4, 11), (5, 14)] > > Provide a key to sort after the second value, i. e. the frequency in the > (index, frequency) pairs: > >>>> def second_value(item): > ... return item[1] > ... >>>> sorted(enumerate(power_play), key=second_value) > [(0, 0), (1, 0), (4, 11), (5, 14), (3, 32), (2, 61)] > > Note that instead of writing your own custom function or lambda you could > also use operator.itemgetter(1) from the operator module in the stdlib. > > Sort in reverse order: > >>>> sorted(enumerate(power_play), key=second_value, reverse=True) > [(2, 61), (3, 32), (5, 14), (4, 11), (0, 0), (1, 0)] > > > Print the output for frequencies > 0: > >>>> pairs = sorted(enumerate(power_play), key=second_value, reverse=True) >>>> for index, freq in pairs: > ... if freq == 0: > ... break > ... print(freq, index, sep="\t") > ... > 61 2 > 32 3 > 14 5 > 11 4 > > Wow! Thanks! Printing this out and will be studying it completely. Again, thanks. Ken From oscar.j.benjamin at gmail.com Fri Mar 13 16:34:11 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Fri, 13 Mar 2015 15:34:11 +0000 Subject: [Tutor] Idle - ImportError: No module named numpy In-Reply-To: <55009AC4.5070601@c2o.pro.br> References: <20150305122044.GK7655@ando.pearwood.info> <54F9F18D.80001@c2o.pro.br> <54FDE1C5.6000301@c2o.pro.br> <54FEF8B7.4060304@c2o.pro.br> <55009AC4.5070601@c2o.pro.br> Message-ID: On 11 March 2015 at 19:43, Markos wrote: > > On 10-03-2015 16:48, Oscar Benjamin wrote: >> >> Looks like a bug in pip or in the get-pip script. What version of >> python3 are you using? > > But the command to install numpy gave an error message: > > # python3 -m pip install numpy > > Collecting numpy > Exception: > Traceback (most recent call last): > File > "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/basecommand.py", Ah okay. You're using Python 3.1. Python 3.1 is not supported by pip, setuptools or numpy. That may explain the error messages that you get with pip. Also it explains why there is no python3-numpy package in your repositories. Essentially most major Python packages are aimed at Python 2.6/2.7 and Python 3.2 upwards but not Python 3.0 or 3.1. Does squeeze have a newer version of Python 3 (e.g. 3.2)? It may be possible to get numpy to work with python 3.1 but I would recommend either: 1) Stick to Python 2. 2) Upgrade to wheezy - I haven't checked but I assume that it has a newer version of Python 3. 3) Install Python 3.4 manually (by compiling it yourself). If you do want to try and install numpy for python 3.1 then you would download the .tar.gz from here: https://pypi.python.org/pypi/numpy Then extract it, cd in, and run "python3 setup.py install". The people who make numpy don't support Python 3.1 though which means that they don't try to guarantee that it will work so you may encounter more problems. Oscar From beachkidken at gmail.com Fri Mar 13 16:55:22 2015 From: beachkidken at gmail.com (Ken G.) Date: Fri, 13 Mar 2015 11:55:22 -0400 Subject: [Tutor] Rearranging a list of numbers with corresponding index (RESOLVED) In-Reply-To: <5502F680.8090406@davea.name> References: <5502ECC5.8090500@gmail.com> <5502F680.8090406@davea.name> Message-ID: <5503086A.6080504@gmail.com> On 03/13/2015 10:38 AM, Dave Angel wrote: > On 03/13/2015 09:57 AM, Ken G. wrote: >> I have been keeping track of numbers drawn in our local lotto drawings >> into a list format as shown in a short example below. Using such list, I >> am able to determine how often a number appears within the last 100 plus >> drawings. >> >> The length of my lists range from 5, 15, 35, 59and 75 long. I will give >> an example of one of my short list. >> >> PowerPlay = [0, 0, 61, 32, 11, 14] >> >> Disregarding index 0 and 1, I can see and print the following: >> >> Index Number >> 2 61 >> 3 32 >> 4 11 >> 5 14 >> >> showing that number 2 appears 61 times, number 3 appears 32 times, >> number 4 appears 11 times and number 5 appears 14 times within last 100 >> plus drawings. >> >> How I best rearrange the numbers from high to low with its corresponding >> index number such as below: >> >> Number Index >> 61 2 >> 32 3 >> 14 5 >> 11 4 >> >> showing the number 2 appears 61 times, number 3 appears 32 times, number >> 5 appears 14 times and number 4 appears 11 times. I know that using >> >> MegaBall.reverse() >> >> sort the number from high to low but the index numbers still remain in >> the same positions. >> >> Thanks for pointing out the way to do this. >> > > Make a list of tuples by doing something like: > new_list = [ (cnt, index) for index, cnt in enumerate(power_play) ] > > to produce: > [ (0, 0), (0, 1), (61, 2), (32, 3), (11, 4), (14, 5) ] > > then sort it with reverse=True. When you print it, just omit the > items that have zero as their count. (they'll be at the end, anyway) > > Lots of other variants, some more concise. And other approaches might > be cleaner if we change how we generate the list. > > (I tried it briefly using Python 3.4) > > I will be studying this also. I am still using Python 2.7.6 as that is the latest as provided by Ubuntu 14.04.1. With thanks to all, I was able to resolve my request for assistance here in this forum. Thanks Peter. Ken From colin.ross.dal at gmail.com Fri Mar 13 18:13:14 2015 From: colin.ross.dal at gmail.com (Colin Ross) Date: Fri, 13 Mar 2015 14:13:14 -0300 Subject: [Tutor] Fitting data to error function Message-ID: Hi all, I am attempting to optimize the parameters I_0 and w_0 in the function (func(x,I_0,w_0) shown below) to fit a set of data. However when I run this code I recieve the error shown below. Any suggestions would be greatly appreciated. Code: import numpy as np import math from math import * # data xdata = np.array([0.1,1.0,2.0,3.0,4.0,5.0]) ydata = np.array([0.1,0.9,2.2,2.8,3.9,5.1]) # Initial guess. x0 = np.array([0.0, 0.0]) # Data errors: sigma = np.array([1.0,1.0,1.0,1.0,1.0,1.0]) # Error function def func(x,a,b): return ((a)/2.)*(math.erfc((np.sqrt(2.)*x*1.E-3)/b)) # Optimization import scipy.optimize as optimization print optimization.curve_fit(func, xdata, ydata, x0, sigma) The error I am recieving is as follows: TypeError: only length-1 arrays can be converted to Python scalars From rhce.san at gmail.com Fri Mar 13 18:09:50 2015 From: rhce.san at gmail.com (Santosh Kumar) Date: Fri, 13 Mar 2015 22:39:50 +0530 Subject: [Tutor] Ipython Queries Message-ID: Hi All, I have installed both python2 and python3 in my system . When i used ipython it by default goes to python2 base. How/what is the easy way to swith between python2 and python3 ? Thanks, santosh From fomcl at yahoo.com Fri Mar 13 18:40:27 2015 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 13 Mar 2015 17:40:27 +0000 (UTC) Subject: [Tutor] Ipython Queries In-Reply-To: References: Message-ID: <1213194878.446432.1426268427670.JavaMail.yahoo@mail.yahoo.com> ----- Original Message ----- > From: Santosh Kumar > To: python mail list > Cc: > Sent: Friday, March 13, 2015 6:09 PM > Subject: [Tutor] Ipython Queries > > Hi All, > > I have installed both python2 and python3 in my system . When i used > ipython it by default goes to python2 base. How/what is the easy way to > swith between python2 and python3 ? The easy way? Dunno. I would love to know about it. Meanwhile, I use: Python 3.4.2 (default, Nov 21 2014, 20:13:40) [GCC 4.7.2] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import IPython; IPython.start_ipython(argv=[]) Python 3.4.2 (default, Nov 21 2014, 20:13:40) Type "copyright", "credits" or "license" for more information. IPython 2.4.1 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: From dyoo at hashcollision.org Fri Mar 13 19:00:01 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Fri, 13 Mar 2015 11:00:01 -0700 Subject: [Tutor] Fitting data to error function In-Reply-To: References: Message-ID: > The error I am recieving is as follows: > > TypeError: only length-1 arrays can be converted to Python scalars Hi Colin, Do you have a more informative "stack trace" of the entire error? Providing this will help localize the problem. As is, it's clear there's a type error... somewhere... :P Seeing the stack trace will make things easier for us. From mmr15 at case.edu Fri Mar 13 17:49:11 2015 From: mmr15 at case.edu (Matthew Ruffalo) Date: Fri, 13 Mar 2015 12:49:11 -0400 Subject: [Tutor] Rearranging a list of numbers with corresponding index (RESOLVED) In-Reply-To: <5503086A.6080504@gmail.com> References: <5502ECC5.8090500@gmail.com> <5502F680.8090406@davea.name> <5503086A.6080504@gmail.com> Message-ID: <55031507.20607@case.edu> On 03/13/2015 11:55 AM, Ken G. wrote: > I will be studying this also. I am still using Python 2.7.6 as that is > the latest as provided by Ubuntu 14.04.1. FYI, Python 3.4 is installed by default in Ubuntu 14.04(.1). You have to invoke it as 'python3' though. MMR... From dyoo at hashcollision.org Fri Mar 13 19:27:00 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Fri, 13 Mar 2015 11:27:00 -0700 Subject: [Tutor] Fitting data to error function In-Reply-To: References: Message-ID: On Fri, Mar 13, 2015 at 11:00 AM, Danny Yoo wrote: >> The error I am recieving is as follows: >> >> TypeError: only length-1 arrays can be converted to Python scalars > > > Hi Colin, > > Do you have a more informative "stack trace" of the entire error? > Providing this will help localize the problem. As is, it's clear > there's a type error... somewhere... :P Seeing the stack trace will > make things easier for us. Quick and dirty analysis. If I had to guess, I'd look at the mix of math.erfc with numeric array values in the subexpression: math.erfc((np.sqrt(2.)*x*1.E-3)/b) np.sqrt(...) returns a numpy array, if I'm not mistaken. But math.erfc() probably can't deal with numpy values. So there's definitely a problem there. See messages such as: https://groups.google.com/forum/#!topic/comp.lang.python/9E4HX4AES-M which suggest that trying to use the standard library math functions on numpy arrays isn't going to work. Unfortunately, without explicit stack trace, I don't know if that's the *only* problem you're seeing. That's why providing a good stack trace is so important in bug reports: there can be multiple causes for something to go wrong. I'd rather make sure we've hit the one that's causing you grief. Anyway, remediation time. Reading docs... ok, you could probably make a vectorized version of the erfc function: vectorized_erfc = np.vectorize(math.erfc) and use this in favor of the non-vectorized version. vectorize allows you to take regular functions and turn them into ones that work on numpy arrays: http://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html A better approach is probably to reuse the scipy.special.erfc function within scipy itself: http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.special.erfc.html If you have more questions, please feel free to ask. From steve at pearwood.info Sat Mar 14 10:53:19 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 14 Mar 2015 20:53:19 +1100 Subject: [Tutor] Ipython Queries In-Reply-To: References: Message-ID: <20150314095319.GL7655@ando.pearwood.info> On Fri, Mar 13, 2015 at 10:39:50PM +0530, Santosh Kumar wrote: > Hi All, > > I have installed both python2 and python3 in my system . When i used > ipython it by default goes to python2 base. How/what is the easy way to > swith between python2 and python3 ? I don't think there is an easy way on Windows, but I could be wrong. You could try asking on a dedicated iPython mailing list. If you get an answer, please come back and tell us here so we can learn too. On Linux, install the ipython3 package. On Debian or Ubuntu systems, try: sudo aptitude install ipython3 and then run ipython3 instead of ipython. On Centos or Fedora systems: sudo yum install ipython3 will probably work if your system isn't too old. Or, you could try the instructions shown here: http://stackoverflow.com/questions/9386048/ipython-reads-wrong-python-version but I would be surprised if it works. (I just tried it, and it failed. You need to install all the ipython libraries into Python 3.) -- Steve From markos at c2o.pro.br Sat Mar 14 16:40:21 2015 From: markos at c2o.pro.br (Markos) Date: Sat, 14 Mar 2015 12:40:21 -0300 Subject: [Tutor] Idle - ImportError: No module named numpy In-Reply-To: References: <20150305122044.GK7655@ando.pearwood.info> <54F9F18D.80001@c2o.pro.br> <54FDE1C5.6000301@c2o.pro.br> <54FEF8B7.4060304@c2o.pro.br> <55009AC4.5070601@c2o.pro.br> Message-ID: <55045665.3020105@c2o.pro.br> On 13-03-2015 12:34, Oscar Benjamin wrote: > On 11 March 2015 at 19:43, Markos wrote: > >> On 10-03-2015 16:48, Oscar Benjamin wrote: >> >>> Looks like a bug in pip or in the get-pip script. What version of >>> python3 are you using? >>> >> But the command to install numpy gave an error message: >> >> # python3 -m pip install numpy >> >> Collecting numpy >> Exception: >> Traceback (most recent call last): >> File >> "/usr/local/lib/python3.1/dist-packages/pip-6.0.8-py3.1.egg/pip/basecommand.py", >> > Ah okay. You're using Python 3.1. Python 3.1 is not supported by pip, > setuptools or numpy. That may explain the error messages that you get > with pip. Also it explains why there is no python3-numpy package in > your repositories. Essentially most major Python packages are aimed at > Python 2.6/2.7 and Python 3.2 upwards but not Python 3.0 or 3.1. Does > squeeze have a newer version of Python 3 (e.g. 3.2)? > > It may be possible to get numpy to work with python 3.1 but I would > recommend either: > 1) Stick to Python 2. > 2) Upgrade to wheezy - I haven't checked but I assume that it has a > newer version of Python 3. > 3) Install Python 3.4 manually (by compiling it yourself). > > If you do want to try and install numpy for python 3.1 then you would > download the .tar.gz from here: > https://pypi.python.org/pypi/numpy > Then extract it, cd in, and run "python3 setup.py install". > > The people who make numpy don't support Python 3.1 though which means > that they don't try to guarantee that it will work so you may > encounter more problems. > > > Oscar > > Hi Oscar, Despite failing to install numpy for Python 3.1, this exchange of messages helped me to understand the "Python world": ^) Thank you very much for your attention. Markos From oscar.j.benjamin at gmail.com Sat Mar 14 23:17:44 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Sat, 14 Mar 2015 22:17:44 +0000 Subject: [Tutor] Ipython Queries In-Reply-To: <20150314095319.GL7655@ando.pearwood.info> References: <20150314095319.GL7655@ando.pearwood.info> Message-ID: On 14 March 2015 at 09:53, Steven D'Aprano wrote: > On Fri, Mar 13, 2015 at 10:39:50PM +0530, Santosh Kumar wrote: >> Hi All, >> >> I have installed both python2 and python3 in my system . When i used >> ipython it by default goes to python2 base. How/what is the easy way to >> swith between python2 and python3 ? > > I don't think there is an easy way on Windows, but I could be wrong. You > could try asking on a dedicated iPython mailing list. If you get an > answer, please come back and tell us here so we can learn too. When you install ipython (or any Python package) you install it for a particular Python version. You haven't said how you installed either of the Python versions or ipython or which version of Python you installed ipython for. Assuming that you have installed ipython for Python 3 then you may be able to run ipython for Python 3 by typing one of the following in a terminal: $ ipython3 $ python3 -m IPython $ py -3 -m IPython (Note carefully that the I and P at the start of IPython in the last two examples are capitalised - the first example is all lower case). You may find that ipython is in the "programs" menu on Windows. I'm not really sure how that stuff works on newer versions of Windows though. Without knowing more about your setup it's hard to be specific about what you should do. What OS are you using (e.g. Windows 8?)? How did you install Python 2 and Python 3 and which versions (e.g. 2.7 and 3.4?). How did you install ipython? Which version of Python did you install it for? Do you know what folder the Python installations are in (e.g. C:\Python34)? Oscar From colin.ross.dal at gmail.com Sun Mar 15 16:27:50 2015 From: colin.ross.dal at gmail.com (Colin Ross) Date: Sun, 15 Mar 2015 12:27:50 -0300 Subject: [Tutor] Fitting data to error function In-Reply-To: References: Message-ID: Hi Danny, Thanks for the help! As you mentioned, using scipy.special.erfc was a much better idea. Below is a copy of my program and the stack trace, showing a new error. It seems that the first auto correlation works, however the second fails. ############################### # Autocorrelation program ############################### import numpy as np from numpy import ma, logical_or import pylab from pylab import * import matplotlib.pyplot as plt from matplotlib.ticker import AutoMinorLocator import math from math import exp import scipy from scipy.integrate import quad,fixed_quad import sys from scipy.optimize import curve_fit from scipy.fftpack import fft, ifft, fftfreq ############################## #fitting function (gaussian) ############################## def fit(x,a,b,c): return a*np.exp(-(x-b)**2/c) #(1.0/(np.sqrt(2*np.pi)*sigma))*np.exp(-(x-mu)**2 / (2*sigma**2)) ############################## # Load data from .txt file ############################## data1 = np.loadtxt('TL_pulseC.txt',skiprows = 2 ,usecols = (0,1)) data2 = np.loadtxt('cos_pulseC.txt',skiprows = 2 ,usecols = (0,1)) #print "\n Stage Position (um) Amplitude (V)" #print data ################################ # Create data arrays ################################ pos = np.array(data1[:,0]) # micrometers pos_m = pos*1.0*10**(-6) # meters print pos_m amp = np.array(data1[:,1]) # V amp_auto = np.array(data2[:,1]) ################################################################################################# # Finding the index of the maximum amplitude where the pulse delay is zero. Then calculating the # mean to shift the curve accordingly. ################################################################################################# peak_id = np.where(amp == np.max(amp))[0][0] mean = pos_m[peak_id] print mean dif = pos_m - mean t_d =(2.*dif)/(2.99*10**8)*10**(12.) # ps print t_d t = np.arange(0.,0.5,1/float(2*len(t_d))) # picoseconds (ps) ################################ # Define constants ################################ c = 2.99*10**8 # m/s alpha = np.pi # rad gamma = 200.0*10**(-15.) lamb_o = 1160.00*10**(-9.) # m omega_o = ((2.*np.pi*c)/lamb_o)*1.*10**(-12.) # ps^-1 delta = np.pi/2. # rad FWHM = 32.53*10**(-6) # m t_p = ((FWHM)/c)*10**(12.) # ps print t_p E_norm = 1. # normalized ################################ # Define functions ################################ ################################ # Electric field ################################ def E_o(x): return E_norm*(cosh(1.76*x/t_p))**(-1.) # Real part of electric field def E_rin(x): return (1./2.)*E_o(x)*cos(omega_o*x) # Imaginary part of electric field def E_iin(x): return (1./2.)*E_o(x)*sin(omega_o*x) # Total electric field def E_(x): return (1./2.)*E_o(x)*np.exp(-1j*omega_o*x) ################################ # Autocorrelation ################################ ''' def integrand(t,t_d): return abs(E_rin(t))**2.*abs(E_rin(t - t_d))**2. ''' def integrand(x,y): return abs(E_(x))**2.*abs(E_(x - y))**2. #integrand = abs(E_(t))**2.*abs(E_(t - t_d))**2. def G(y): return quad(integrand, -np.infty, np.infty, args=(y)) G_plot = [] for tau in t_d: integral,error = G(tau) G_plot.append(integral) #fit data params = curve_fit(fit,pos[174-100:174+100],amp[174-100:174+100],p0=[0.003, 8550,350]) #function, xdata, ydata, initial guess (from plot) #parameters [a,b,d] = params[0] #error [da,db,dd] = np.sqrt(np.diag(params[1])) ################################# # Shaped electric field ################################# alpha = np.pi delta = np.pi/2. gamma = 200.*10**(-15) # sec dt = (t[1]-t[0])*(1*10**(-12)) def phi(x): return alpha*np.cos(gamma*(x - omega_o) - delta) def M(x): return np.exp(1j*phi(x)) def E_out(x): E_in_w = fft(E_(x)) omega = fftfreq(len(x),dt)*2*np.pi E_out_w = E_in_w*M(omega) return ifft(E_out_w) ################################# # Second autocorrelation ################################# def integrand1(x,y): return abs(E_out(x))**2.*abs(E_out(x - y))**2. def G1(y): return quad(integrand1, -np.infty, np.infty, args=(y)) G_plot_1 = [] for tau in t_d: integral,error = G1(tau) G_plot_1.append(integral) ################################# # Plotting data ################################# # Defining x and y minorlocator xminorLocator = AutoMinorLocator() yminorLocator = AutoMinorLocator() # Setting minor ticks ax = plt.subplot(111) ax.xaxis.set_minor_locator(xminorLocator) ax.yaxis.set_minor_locator(yminorLocator) sample_pos = np.linspace(min(pos),max(pos),1000) equation_label = str(round(a*1E3,1)) + r'$\times 10^{-3} \exp((x-$' + str(round(b,1)) + r'$)/$' + str(round(d,1)) + r'$)$' ''' subplot(2,1,1) plot(pos,amp, 'r.', label='Data') plot(sample_pos,fit(sample_pos,a,b,d),'k-', label='Guassian Fit') title('Autocorrelation Intensity') #xlabel('Stage Position (um)') ylabel('Amplitude (V)') text(8600,0.003,equation_label) text(8800 , 0.0025 ,'Transform Limited Pulse', horizontalalignment='center', verticalalignment='center') legend(loc='upper left') subplot(2,1,2) plot(pos, amp_auto, 'b-') #title('Autocorrelation Intensity') xlabel('Stage Position (um)') ylabel('Amplitude (V)') text(8800 , 0.0005 , r'$\Phi_M(\omega) = \alpha\cos(\gamma(\omega - \omega_0) - \delta)$', horizontalalignment='center', verticalalignment='center') show() ''' plot(t,E_(t)) title('Electric Field') xlabel('Time (ps)') ylabel('E (V/m)') text(0.35, 0.35, r'$E_{out}(t)=E_{in}(t)=\frac{1}{2}E_0(t)e^{-i\omega_0t}$', horizontalalignment='center', verticalalignment='center') ''' subplot(1,2,2) plot(t,E_iin(t)) title('Electric Field') xlabel('Time (sec)') ''' show() plot(t_d,G_plot) title('Non-collinear Autocorrelation') xlabel('Time (ps)') ylabel('G(t_d)') text(12.5, 0.8e-16, r'$G(t_d) = \int_{-\infty}^{\infty} |E(t)|^2 |E(t - t_d)|^2 dt$', horizontalalignment='center', verticalalignment='center') show() plot(t_d,G_plot_1) title('Masked') xlabel('Time (ps)') ylabel('G(t_d)') text(12.5, 0.8e-16, r'$G(t_d) = \int_{-\infty}^{\infty} |E(t)|^2 |E(t - t_d)|^2 dt$', horizontalalignment='center', verticalalignment='center') show() plot(t,E_out(t)) title('Shaped Electric Field') xlabel('Time (ps)') ylabel('E_out') show() The stack trace is as follows: IndexError Traceback (most recent call last) in () ----> 1 G1(t_d[0]) in G1(y) 7 def G1(y): 8 ----> 9 return quad(integrand1, -np.infty, np.infty, args=(y)) /Users/colinross/anaconda/lib/python2.7/site-packages/scipy/integrate/quadpack.pyc in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst) 279 args = (args,) 280 if (weight is None): --> 281 retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points) 282 else: 283 retval = _quad_weight(func,a,b,args,full_output,epsabs,epsrel,limlst,limit,maxp1,weight,wvar,wopts) /Users/colinross/anaconda/lib/python2.7/site-packages/scipy/integrate/quadpack.pyc in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points) 345 return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit) 346 else: --> 347 return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit) 348 else: 349 if infbounds != 0: in integrand1(x, y) 1 def integrand1(x,y): 2 ----> 3 return abs(E_out(x))**2.*abs(E_out(x - y))**2. 4 5 in E_out(x) 6 7 def E_out(x): ----> 8 E_in_w = fft(E_(x)) 9 omega = fftfreq(len(x),dt)*2*np.pi 10 E_out_w = E_in_w*M(omega) /Users/colinross/anaconda/lib/python2.7/site-packages/scipy/fftpack/basic.pyc in fft(x, n, axis, overwrite_x) 253 254 if n is None: --> 255 n = tmp.shape[axis] 256 elif n != tmp.shape[axis]: 257 tmp, copy_made = _fix_shape(tmp,n,axis) IndexError: tuple index out of range On Fri, Mar 13, 2015 at 3:27 PM, Danny Yoo wrote: > On Fri, Mar 13, 2015 at 11:00 AM, Danny Yoo > wrote: > >> The error I am recieving is as follows: > >> > >> TypeError: only length-1 arrays can be converted to Python scalars > > > > > > Hi Colin, > > > > Do you have a more informative "stack trace" of the entire error? > > Providing this will help localize the problem. As is, it's clear > > there's a type error... somewhere... :P Seeing the stack trace will > > make things easier for us. > > > Quick and dirty analysis. If I had to guess, I'd look at the mix of > math.erfc with numeric array values in the subexpression: > > math.erfc((np.sqrt(2.)*x*1.E-3)/b) > > > np.sqrt(...) returns a numpy array, if I'm not mistaken. But > math.erfc() probably can't deal with numpy values. So there's > definitely a problem there. > > > See messages such as: > > https://groups.google.com/forum/#!topic/comp.lang.python/9E4HX4AES-M > > which suggest that trying to use the standard library math functions > on numpy arrays isn't going to work. > > > Unfortunately, without explicit stack trace, I don't know if that's > the *only* problem you're seeing. That's why providing a good stack > trace is so important in bug reports: there can be multiple causes for > something to go wrong. I'd rather make sure we've hit the one that's > causing you grief. > > > > Anyway, remediation time. Reading docs... ok, you could probably make > a vectorized version of the erfc function: > > vectorized_erfc = np.vectorize(math.erfc) > > and use this in favor of the non-vectorized version. vectorize allows > you to take regular functions and turn them into ones that work on > numpy arrays: > > > http://docs.scipy.org/doc/numpy/reference/generated/numpy.vectorize.html > > > A better approach is probably to reuse the scipy.special.erfc function > within scipy itself: > > > http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.special.erfc.html > > > > If you have more questions, please feel free to ask. > From dbasberg at comcast.net Sun Mar 15 17:46:04 2015 From: dbasberg at comcast.net (Doug Basberg) Date: Sun, 15 Mar 2015 12:46:04 -0400 Subject: [Tutor] print method in Python2.7 problem Message-ID: <036e01d05f3f$87b2ba40$97182ec0$@net> An embedded and charset-unspecified text was scrubbed... Name: dsbtest02.py URL: From rhce.san at gmail.com Sun Mar 15 18:25:49 2015 From: rhce.san at gmail.com (Santosh Kumar) Date: Sun, 15 Mar 2015 22:55:49 +0530 Subject: [Tutor] Ipython Queries In-Reply-To: References: <20150314095319.GL7655@ando.pearwood.info> Message-ID: Thank you All. I forgot to update you guys , i use ubuntu 14.10 . I believe/was under assumption that we cannot used ipython both for python 2.x and python 3.x. So ipython is for python 2.x and ipython3 is for python 3.x Thanks, santosh On Sun, Mar 15, 2015 at 3:47 AM, Oscar Benjamin wrote: > On 14 March 2015 at 09:53, Steven D'Aprano wrote: > > On Fri, Mar 13, 2015 at 10:39:50PM +0530, Santosh Kumar wrote: > >> Hi All, > >> > >> I have installed both python2 and python3 in my system . When i used > >> ipython it by default goes to python2 base. How/what is the easy way to > >> swith between python2 and python3 ? > > > > I don't think there is an easy way on Windows, but I could be wrong. You > > could try asking on a dedicated iPython mailing list. If you get an > > answer, please come back and tell us here so we can learn too. > > When you install ipython (or any Python package) you install it for a > particular Python version. You haven't said how you installed either > of the Python versions or ipython or which version of Python you > installed ipython for. > > Assuming that you have installed ipython for Python 3 then you may be > able to run ipython for Python 3 by typing one of the following in a > terminal: > > $ ipython3 > > $ python3 -m IPython > > $ py -3 -m IPython > > (Note carefully that the I and P at the start of IPython in the last > two examples are capitalised - the first example is all lower case). > > You may find that ipython is in the "programs" menu on Windows. I'm > not really sure how that stuff works on newer versions of Windows > though. > > Without knowing more about your setup it's hard to be specific about > what you should do. What OS are you using (e.g. Windows 8?)? How did > you install Python 2 and Python 3 and which versions (e.g. 2.7 and > 3.4?). How did you install ipython? Which version of Python did you > install it for? Do you know what folder the Python installations are > in (e.g. C:\Python34)? > > > Oscar > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- D. Santosh Kumar RHCE | SCSA +91-9703206361 Every task has a unpleasant side .. But you must focus on the end result you are producing. From breamoreboy at yahoo.co.uk Sun Mar 15 18:40:08 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 15 Mar 2015 17:40:08 +0000 Subject: [Tutor] print method in Python2.7 problem In-Reply-To: <036e01d05f3f$87b2ba40$97182ec0$@net> References: <036e01d05f3f$87b2ba40$97182ec0$@net> Message-ID: On 15/03/2015 16:46, Doug Basberg wrote: Nothing because it was in a completely unnecessary attachment (some people won't even receive it), so here's the code. Stat = {'Vbatt': 51.24, 'Ichrg': 6.75} print ' SOLAR PANEL VOLTAGE' print(' DSB "HI;" %s') % (str(Stat['Vbatt'])) print(' %s') % (str(Stat['Vbatt'])) He also didn't bother specifying the actual problem,but like others here we just love guessing games. So I'll go for:- TypeError: unsupported operand type(s) for %: 'NoneType' and 'str' Print in 2.7 is a statement so the first one is fine. The second (and third) you're trying to apply the % operator to the print statememt which by definition is None, hence the error. You also don't need the call to str so put it all together it simply becomes. print ' DSB "HI;" %s' % Stat['Vbatt'] I'll leave you to come back with the next problem as I haven't worked out what the solution is yet :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From steve at pearwood.info Sun Mar 15 18:46:12 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 16 Mar 2015 04:46:12 +1100 Subject: [Tutor] print method in Python2.7 problem In-Reply-To: <036e01d05f3f$87b2ba40$97182ec0$@net> References: <036e01d05f3f$87b2ba40$97182ec0$@net> Message-ID: <20150315174612.GO7655@ando.pearwood.info> On Sun, Mar 15, 2015 at 12:46:04PM -0400, Doug Basberg wrote: > Stat = {'Vbatt': 51.24, 'Ichrg': 6.75} > print ' SOLAR PANEL VOLTAGE' > print(' DSB "HI;" %s') % (str(Stat['Vbatt'])) > print(' %s') % (str(Stat['Vbatt'])) Yes? What about it? Do you have a question or do you expect us to read your mind? If the second, this is your lucky day, because in fact I am a powerful psychic, and by reading your mind I can see that you are getting a ValueError exception from the last line: ValueError: unsupported format character ';' (0x3b) at index 24 By using another of my superpowers, called "reading the error message", I can work out the nature of the problem: you have a format command "%;" which isn't supported. When using string interpolation with the % operator, it looks for % formatting characters in the string. %s you already know. %d needs an integer value, %f a float. And %; isn't supported at all -- it is an error. What you need to do is escape the percent sign, so it will be turned into a percent sign in the output, not treated as a format command in the input: print(' %s') % (str(Stat['Vbatt'])) -- Steve From breamoreboy at yahoo.co.uk Sun Mar 15 18:57:46 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 15 Mar 2015 17:57:46 +0000 Subject: [Tutor] print method in Python2.7 problem In-Reply-To: References: <036e01d05f3f$87b2ba40$97182ec0$@net> Message-ID: On 15/03/2015 17:40, Mark Lawrence wrote: > On 15/03/2015 16:46, Doug Basberg wrote: > > Nothing because it was in a completely unnecessary attachment (some > people won't even receive it), so here's the code. > > Stat = {'Vbatt': 51.24, 'Ichrg': 6.75} > > print ' SOLAR PANEL VOLTAGE' > print(' DSB "HI;" %s') % (str(Stat['Vbatt'])) > print(' %s') % > (str(Stat['Vbatt'])) > > He also didn't bother specifying the actual problem,but like others here > we just love guessing games. So I'll go for:- > > TypeError: unsupported operand type(s) for %: 'NoneType' and 'str' > > Print in 2.7 is a statement so the first one is fine. The second (and > third) you're trying to apply the % operator to the print statememt > which by definition is None, hence the error. You also don't need the > call to str so put it all together it simply becomes. > > print ' DSB "HI;" %s' % Stat['Vbatt'] > > I'll leave you to come back with the next problem as I haven't worked > out what the solution is yet :) > Having read Steven D'Aprano's reply I have now, I was mixing Python 2 and 3, whoops, sorry about that!!! -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From alan.gauld at btinternet.com Sun Mar 15 19:29:06 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 15 Mar 2015 18:29:06 +0000 Subject: [Tutor] print method in Python2.7 problem In-Reply-To: <036e01d05f3f$87b2ba40$97182ec0$@net> References: <036e01d05f3f$87b2ba40$97182ec0$@net> Message-ID: To be fair to Doug his mails aren't cp,ing through to the list fully. I susp3ect because he is using an Outlook message format with lots of fancy formatting and a background all of which are being sent as attachments... Here is his text (with a lot of extraneous whitespace removed): --------------------- For some reason my e-mails to this forum seem to have a problem, I may sign up again to see if something is wrong with my account. Mean while can the moderator cut and paste so members can see my post? Thank you. Do you know what my account problem is? Thank you, Doug Basberg dbasberg at comcast.net I am attempting to write web pages in Python and inserting numbers from a dictionary read in this program and populated in another using a classes methods for read and write. In this process, I found that in Python 2.7 on my Raspberry Pi, I have a problem with a print statement. I have attached a very small test program exhibiting the same problem. The error message tells me that the semicolon is a problem. It is in a single quoted string, so it should not be interpreted ?? I know that %xxx is interpreted inside strings of print statements. Are there other special characters (like %) that I am not aware of? I can sure use some help with this. Thanks, Doug File attached and shown as text below: Stat = {'Vbatt': 51.24, 'Ichrg': 6.75} print ' SOLAR PANEL VOLTAGE' print(' DSB "HI;" %s') % (str(Stat['Vbatt'])) print(' %s') % (str(Stat['Vbatt'])) Running the program: $ python dsbtest02.py SOLAR PANEL VOLTAGE DSB "HI;" 51.24 Traceback (most recent call last): File "dsbtest02.py", line 5, in print(' %s') % (str(Stat['Vbatt'])) ValueError: unsupported format character ';' (0x3b) at index 24 --------------------- The solution (to the messaging issue) is if Doug uses plain text for posts to the list in future. The solution for the code issue is, as Steven says, to use %% to escape the percent sign. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Mon Mar 16 03:48:21 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 15 Mar 2015 19:48:21 -0700 Subject: [Tutor] Fitting data to error function In-Reply-To: References: Message-ID: > Thanks for the help! As you mentioned, using scipy.special.erfc was a much > better idea. Below is a copy of my program and the stack trace, showing a > new error. It seems that the first auto correlation works, however the > second fails. At this point, the program is large enough that we need to apply alternative techniques besides reading the entire program and trying to keep it in our head at once. One approach that is often successful is to check the _user functions_ in isolation, because they should be separable and independently testable. You've written a few functions in your program. In particular: ################################################## def fit(x,a,b,c): ... def E_o(x): ... def E_rin(x): ... def E_iin(x): ... def E_(x): ... def integrand(t,t_d): ... def integrand(x,y): ... ## why is this being defined twice?! def G(y): ... def phi(x): ... def M(x): ... def E_out(x): ... def integrand1(x,y): ... def G1(y): ... ################################################## A function-based perspective will ask the following questions: 1. What is the documented purpose of each of these functions? 2. What are the expected inputs and output types for each of these functions? 3. Which of these functions have unit tests to spot-check their quality? I can say for certain, without looking at any significant code, that something is strange with regards to integrand. Your program has defined this twice, and that's almost certainly not right. The strategy here is to pinpoint where the problem is: we can't possibly say that the whole program is broken, so we try to scope the problem down to a manageable size. You mention that the first auto-correlation is producing good results. What functions are involved in the computation of the first auto-correlation? Another technique to try to scope is to look at the stack trace and the involved functions. The stack trace says that the following functions are involved at the point of the program breakage: G1 quad integrand1 E_out E_ In particular, note the tail end of the stack trace: > 7 def E_out(x): > ----> 8 E_in_w = fft(E_(x)) It is on the call to: fft(E_(x)) that things break with the IndexError. Note that fft() is provided by the scipy library. For the purposes of debugging, let us assume that all external third-party libraries are bug-free. What does fft expect to receive as an argument? We can read the following: http://docs.scipy.org/doc/scipy/reference/generated/scipy.fftpack.fft.html#scipy.fftpack.fft Since fft is erroring out: there's only one possibility: E_(x) is not providing a value that's appropriate to fft(). Why would it do that? Two possibilities: 1. E_ is buggy and needs investigation. 2. E_ is fine, but the inputted value of x is one that E_x is not defined to handle. And so we start the investigation by considering those two possibilities. Start with #1. Do you know if E_ is fine? What is it supposed to do? What is the shape of its input? What is the shape of its output? Is its output something that fft() is designed to handle? From dyoo at hashcollision.org Mon Mar 16 03:57:45 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 15 Mar 2015 19:57:45 -0700 Subject: [Tutor] Fitting data to error function In-Reply-To: References: Message-ID: > What does fft expect to receive as an argument? We can read the following: > > http://docs.scipy.org/doc/scipy/reference/generated/scipy.fftpack.fft.html#scipy.fftpack.fft > > Since fft is erroring out: there's only one possibility: E_(x) is not > providing a value that's appropriate to fft(). > > Why would it do that? Two possibilities: > > 1. E_ is buggy and needs investigation. > > 2. E_ is fine, but the inputted value of x is one that E_x is not > defined to handle. > > > And so we start the investigation by considering those two > possibilities. Start with #1. > > Do you know if E_ is fine? What is it supposed to do? What is the > shape of its input? What is the shape of its output? Is its output > something that fft() is designed to handle? Just to add: this is not to say that E_() is buggy. We just have to start investigation somewhere, and it seemed as good a place as any, since I don't know if _any_ of the functions are behaving. :P This is very much the reason why programmers like to do unit testing: we want to know what functions at least do something that we know is useful. We know all too well that whole programs break, and we want to have some confidence on what components of our program are likely ok. If #2 is the actual issue, then the question becomes: why is the program producing an invalid input 'x'? And that's where we need to start reasoning backwards, to discover how that value was constructed. From colin.ross.dal at gmail.com Mon Mar 16 22:50:50 2015 From: colin.ross.dal at gmail.com (Colin Ross) Date: Mon, 16 Mar 2015 18:50:50 -0300 Subject: [Tutor] Fitting data to error function In-Reply-To: References: Message-ID: HI Danny, Here is a simplified version: -------------------------------------------------------------------- import numpy as np import pylab from pylab import * import matplotlib.pyplot as plt import scipy from scipy.integrate import quad from scipy.fftpack import fft, ifft, fftfreq ############################## # Load data from .txt file ############################## data1 = np.loadtxt('TL_pulseC.txt',skiprows = 2 ,usecols = (0,1)) data2 = np.loadtxt('cos_pulseC.txt',skiprows = 2 ,usecols = (0,1)) ################################ # Create data arrays ################################ pos = np.array(data1[:,0]) # micrometers pos_m = pos*1.0*10**(-6) # meters amp = np.array(data1[:,1]) # V amp_auto = np.array(data2[:,1]) # V ############################################################# # Finding the index of the maximum amplitude where the pulse delay is zero. # Then calculating the mean to shift the curve accordingly. ############################################################# peak_id = np.where(amp == np.max(amp))[0][0] mean = pos_m[peak_id] dif = pos_m - mean t_d =(2.*dif)/(2.99*10**8)*10**(12.) # picoseconds (ps) t = np.arange(-0.5,0.5,0.0001) # picoseconds (ps) ################################ # Define constants ################################ c = 2.99*10**8 # m/s alpha = np.pi # rad gamma = 200.0*10**(-15.) lamb_o = 1160.00*10**(-9.) # m omega_o = ((2.*np.pi*c)/lamb_o)*1.*10**(-12.) # ps^-1 delta = np.pi/2. # rad FWHM = 32.53*10**(-6) # m t_p = ((FWHM)/c)*10**(12.) # ps E_norm = 1. # normalized ######################################################### # Define functions ######################################################### ################################ # Input electric field ################################ def E_o(x): return E_norm*(cosh(1.76*x/t_p))**(-1.) def E_(x): return (1./2.)*E_o(x)*np.exp(-1j*omega_o*x) ################################# # Shaped electric field ################################# alpha = np.pi delta = np.pi/2. gamma = 200.*10**(-15) # sec dt = (t[1]-t[0])*(1*10**(-12)) # sec def phi(x): return alpha*np.cos(gamma*(x - omega_o) - delta) def M(x): return np.exp(1j*phi(x)) ################################## # Write E(w) as E(t) using FFT ################################## def E_out(x): E_in_w = fft(E_(x)) omega = fftfreq(len(x),dt)*2*np.pi E_out_w = E_in_w*M(omega) return ifft(E_out_w) def integrand(x,y): return abs(E_out(x))**2.*abs(E_(x - y))**2. def G(y): return quad(integrand, -np.infty, np.infty, args=(y)) auto_corr = [] for tau in t_d: integral,error = G(tau) auto_corr.append(integral) # Plotting plt.plot(t_d,auto_corr) plt.show() ------------------------------------------------------------------------- --------------------------------------------------------------------------- IndexError Traceback (most recent call last) /Users/colinross/Documents/Optics/Programming/five.py in () 107 108 for tau in t_d: --> 109 integral,error = G(tau) 110 auto_corr.append(integral) 111 /Users/colinross/Documents/Optics/Programming/five.py in G(y) 102 103 def G(y): --> 104 return quad(integrand, -np.infty, np.infty, args=(y)) 105 106 auto_corr = [] /Users/colinross/anaconda/lib/python2.7/site-packages/scipy/integrate/quadpack.pyc in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst) 279 args = (args,) 280 if (weight is None): --> 281 retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points) 282 else: 283 retval = _quad_weight(func,a,b,args,full_output,epsabs,epsrel,limlst,limit,maxp1,weight,wvar,wopts) /Users/colinross/anaconda/lib/python2.7/site-packages/scipy/integrate/quadpack.pyc in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points) 345 return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit) 346 else: --> 347 return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit) 348 else: 349 if infbounds != 0: /Users/colinross/Documents/Optics/Programming/five.py in integrand(x, y) 99 100 def integrand(x,y): --> 101 return abs(E_out(x))**2.*abs(E_(x - y))**2. 102 103 def G(y): /Users/colinross/Documents/Optics/Programming/five.py in E_out(x) 93 94 def E_out(x): ---> 95 E_in_w = fft(E_(x)) 96 omega = fftfreq(len(x),dt)*2*np.pi 97 E_out_w = E_in_w*M(omega) /Users/colinross/anaconda/lib/python2.7/site-packages/scipy/fftpack/basic.pyc in fft(x, n, axis, overwrite_x) 253 254 if n is None: --> 255 n = tmp.shape[axis] 256 elif n != tmp.shape[axis]: 257 tmp, copy_made = _fix_shape(tmp,n,axis) IndexError: tuple index out of range On Sun, Mar 15, 2015 at 11:57 PM, Danny Yoo wrote: > > What does fft expect to receive as an argument? We can read the > following: > > > > > http://docs.scipy.org/doc/scipy/reference/generated/scipy.fftpack.fft.html#scipy.fftpack.fft > > > > Since fft is erroring out: there's only one possibility: E_(x) is not > > providing a value that's appropriate to fft(). > > > > Why would it do that? Two possibilities: > > > > 1. E_ is buggy and needs investigation. > > > > 2. E_ is fine, but the inputted value of x is one that E_x is not > > defined to handle. > > > > > > And so we start the investigation by considering those two > > possibilities. Start with #1. > > > > Do you know if E_ is fine? What is it supposed to do? What is the > > shape of its input? What is the shape of its output? Is its output > > something that fft() is designed to handle? > > Just to add: this is not to say that E_() is buggy. We just have to > start investigation somewhere, and it seemed as good a place as any, > since I don't know if _any_ of the functions are behaving. :P > > This is very much the reason why programmers like to do unit testing: > we want to know what functions at least do something that we know is > useful. We know all too well that whole programs break, and we want > to have some confidence on what components of our program are likely > ok. > > If #2 is the actual issue, then the question becomes: why is the > program producing an invalid input 'x'? And that's where we need to > start reasoning backwards, to discover how that value was constructed. > From colin.ross.dal at gmail.com Mon Mar 16 22:55:46 2015 From: colin.ross.dal at gmail.com (Colin Ross) Date: Mon, 16 Mar 2015 18:55:46 -0300 Subject: [Tutor] Fitting data to error function In-Reply-To: References: Message-ID: What I am trying to do is calculate the non-colinear autocorrelation: G(t_d) = \int_{-\infty}^{+\infty} |E(t)|^2 * |E(t - t_d)|^2 dt So I need to loop through an array of t_d values (len = 376) and calculate G(t_d) for as many t values as possible to eliminate sampling issues. Colin On Mon, Mar 16, 2015 at 6:50 PM, Colin Ross wrote: > HI Danny, > > Here is a simplified version: > > -------------------------------------------------------------------- > > import numpy as np > > import pylab > > from pylab import * > > import matplotlib.pyplot as plt > > import scipy > > from scipy.integrate import quad > > from scipy.fftpack import fft, ifft, fftfreq > > ############################## > > # Load data from .txt file > > ############################## > > data1 = np.loadtxt('TL_pulseC.txt',skiprows = 2 ,usecols = (0,1)) > > data2 = np.loadtxt('cos_pulseC.txt',skiprows = 2 ,usecols = (0,1)) > > ################################ > > # Create data arrays > > ################################ > > pos = np.array(data1[:,0]) # micrometers > > pos_m = pos*1.0*10**(-6) # meters > > amp = np.array(data1[:,1]) # V > > amp_auto = np.array(data2[:,1]) # V > > ############################################################# > > # Finding the index of the maximum amplitude where the pulse delay is zero. > > # Then calculating the mean to shift the curve accordingly. > > ############################################################# > > peak_id = np.where(amp == np.max(amp))[0][0] > > mean = pos_m[peak_id] > > dif = pos_m - mean > > t_d =(2.*dif)/(2.99*10**8)*10**(12.) # picoseconds (ps) > > > t = np.arange(-0.5,0.5,0.0001) # picoseconds (ps) > > ################################ > > # Define constants > > ################################ > > c = 2.99*10**8 # m/s > > alpha = np.pi # > rad > > gamma = 200.0*10**(-15.) > > lamb_o = 1160.00*10**(-9.) # m > > omega_o = ((2.*np.pi*c)/lamb_o)*1.*10**(-12.) # ps^-1 > > delta = np.pi/2. # rad > > FWHM = 32.53*10**(-6) # m > > t_p = ((FWHM)/c)*10**(12.) # ps > > > E_norm = 1. # > normalized > > ######################################################### > > # Define functions > > ######################################################### > > > ################################ > > # Input electric field > > ################################ > > def E_o(x): > > return E_norm*(cosh(1.76*x/t_p))**(-1.) > > def E_(x): > > return (1./2.)*E_o(x)*np.exp(-1j*omega_o*x) > > ################################# > > # Shaped electric field > > ################################# > > alpha = np.pi > > delta = np.pi/2. > > gamma = 200.*10**(-15) # sec > > dt = (t[1]-t[0])*(1*10**(-12)) # sec > > def phi(x): > > return alpha*np.cos(gamma*(x - omega_o) - delta) > > def M(x): > > return np.exp(1j*phi(x)) > > ################################## > > # Write E(w) as E(t) using FFT > > ################################## > > def E_out(x): > > E_in_w = fft(E_(x)) > > omega = fftfreq(len(x),dt)*2*np.pi > > E_out_w = E_in_w*M(omega) > > return ifft(E_out_w) > > def integrand(x,y): > > return abs(E_out(x))**2.*abs(E_(x - y))**2. > > def G(y): > > return quad(integrand, -np.infty, np.infty, args=(y)) > > auto_corr = [] > > for tau in t_d: > > integral,error = G(tau) > > auto_corr.append(integral) > > # Plotting > > plt.plot(t_d,auto_corr) > > plt.show() > > > ------------------------------------------------------------------------- > > --------------------------------------------------------------------------- > > IndexError Traceback (most recent call last) > > /Users/colinross/Documents/Optics/Programming/five.py in () > > 107 > > 108 for tau in t_d: > > --> 109 integral,error = G(tau) > > 110 auto_corr.append(integral) > > 111 > > > /Users/colinross/Documents/Optics/Programming/five.py in G(y) > > 102 > > 103 def G(y): > > --> 104 return quad(integrand, -np.infty, np.infty, args=(y)) > > 105 > > 106 auto_corr = [] > > > /Users/colinross/anaconda/lib/python2.7/site-packages/scipy/integrate/quadpack.pyc > in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, > weight, wvar, wopts, maxp1, limlst) > > 279 args = (args,) > > 280 if (weight is None): > > --> 281 retval = > _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points) > > 282 else: > > 283 retval = > _quad_weight(func,a,b,args,full_output,epsabs,epsrel,limlst,limit,maxp1,weight,wvar,wopts) > > > /Users/colinross/anaconda/lib/python2.7/site-packages/scipy/integrate/quadpack.pyc > in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points) > > 345 return > _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit) > > 346 else: > > --> 347 return > _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit) > > 348 else: > > 349 if infbounds != 0: > > > /Users/colinross/Documents/Optics/Programming/five.py in integrand(x, y) > > 99 > > 100 def integrand(x,y): > > --> 101 return abs(E_out(x))**2.*abs(E_(x - y))**2. > > 102 > > 103 def G(y): > > > /Users/colinross/Documents/Optics/Programming/five.py in E_out(x) > > 93 > > 94 def E_out(x): > > ---> 95 E_in_w = fft(E_(x)) > > 96 omega = fftfreq(len(x),dt)*2*np.pi > > 97 E_out_w = E_in_w*M(omega) > > > /Users/colinross/anaconda/lib/python2.7/site-packages/scipy/fftpack/basic.pyc > in fft(x, n, axis, overwrite_x) > > 253 > > 254 if n is None: > > --> 255 n = tmp.shape[axis] > > 256 elif n != tmp.shape[axis]: > > 257 tmp, copy_made = _fix_shape(tmp,n,axis) > > > IndexError: tuple index out of range > > On Sun, Mar 15, 2015 at 11:57 PM, Danny Yoo > wrote: > >> > What does fft expect to receive as an argument? We can read the >> following: >> > >> > >> http://docs.scipy.org/doc/scipy/reference/generated/scipy.fftpack.fft.html#scipy.fftpack.fft >> > >> > Since fft is erroring out: there's only one possibility: E_(x) is not >> > providing a value that's appropriate to fft(). >> > >> > Why would it do that? Two possibilities: >> > >> > 1. E_ is buggy and needs investigation. >> > >> > 2. E_ is fine, but the inputted value of x is one that E_x is not >> > defined to handle. >> > >> > >> > And so we start the investigation by considering those two >> > possibilities. Start with #1. >> > >> > Do you know if E_ is fine? What is it supposed to do? What is the >> > shape of its input? What is the shape of its output? Is its output >> > something that fft() is designed to handle? >> >> Just to add: this is not to say that E_() is buggy. We just have to >> start investigation somewhere, and it seemed as good a place as any, >> since I don't know if _any_ of the functions are behaving. :P >> >> This is very much the reason why programmers like to do unit testing: >> we want to know what functions at least do something that we know is >> useful. We know all too well that whole programs break, and we want >> to have some confidence on what components of our program are likely >> ok. >> >> If #2 is the actual issue, then the question becomes: why is the >> program producing an invalid input 'x'? And that's where we need to >> start reasoning backwards, to discover how that value was constructed. >> > > From dyoo at hashcollision.org Mon Mar 16 23:19:27 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 16 Mar 2015 15:19:27 -0700 Subject: [Tutor] Fitting data to error function In-Reply-To: References: Message-ID: On Mon, Mar 16, 2015 at 2:55 PM, Colin Ross wrote: > What I am trying to do is calculate the non-colinear autocorrelation: > > G(t_d) = \int_{-\infty}^{+\infty} |E(t)|^2 * |E(t - t_d)|^2 dt > > So I need to loop through an array of t_d values (len = 376) and calculate > G(t_d) for as many t values as possible to eliminate sampling issues. Ok. But you're using the term "E(t)" and E(t-t_d)" in your LaTeX-ified equation in such a way that it sounds like 'E' is context sensitive. Look at the Python definition of integrand() again: 100 def integrand(x,y): --> 101 return abs(E_out(x))**2.*abs(E_(x - y))**2. and note that there are *two* distinct functions here being used: E_out E_ In contrast, in your mathematics, it looks like these should be the *same* E function, so the fact that this is *different* is worth consideration. I have to assume that the mathematics has some context sensitivity based on the argument type that you haven't quite explained here. Also, I need to ask: do you know what is meant by the term "unit test"? Because this doesn't seem to have been addressed yet, so I need to double check. From colin.ross.dal at gmail.com Mon Mar 16 23:22:46 2015 From: colin.ross.dal at gmail.com (Colin Ross) Date: Mon, 16 Mar 2015 19:22:46 -0300 Subject: [Tutor] Fitting data to error function In-Reply-To: References: Message-ID: Yes, thank you, they were suppose to both be E_out. And to answer your last question, I do not. Can you please explain? On Mon, Mar 16, 2015 at 7:19 PM, Danny Yoo wrote: > On Mon, Mar 16, 2015 at 2:55 PM, Colin Ross > wrote: > > What I am trying to do is calculate the non-colinear autocorrelation: > > > > G(t_d) = \int_{-\infty}^{+\infty} |E(t)|^2 * |E(t - t_d)|^2 dt > > > > So I need to loop through an array of t_d values (len = 376) and > calculate > > G(t_d) for as many t values as possible to eliminate sampling issues. > > > Ok. But you're using the term "E(t)" and E(t-t_d)" in your > LaTeX-ified equation in such a way that it sounds like 'E' is context > sensitive. > > Look at the Python definition of integrand() again: > > 100 def integrand(x,y): > --> 101 return abs(E_out(x))**2.*abs(E_(x - y))**2. > > > and note that there are *two* distinct functions here being used: > > E_out > E_ > > In contrast, in your mathematics, it looks like these should be the > *same* E function, so the fact that this is *different* is worth > consideration. I have to assume that the mathematics has some context > sensitivity based on the argument type that you haven't quite > explained here. > > > Also, I need to ask: do you know what is meant by the term "unit > test"? Because this doesn't seem to have been addressed yet, so I > need to double check. > From dyoo at hashcollision.org Mon Mar 16 23:40:19 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 16 Mar 2015 15:40:19 -0700 Subject: [Tutor] Fitting data to error function In-Reply-To: References: Message-ID: On Mon, Mar 16, 2015 at 3:22 PM, Colin Ross wrote: > Yes, thank you, they were suppose to both be E_out. > > And to answer your last question, I do not. Can you please explain? The article: http://en.wikipedia.org/wiki/Unit_testing may help. As a brief intro: we treat each interesting function as a independently-testable "unit", and write code that exercises each function against a series of input->expected output pairs. E.g. toy example: if we were writing a function to compute hypotenuse: ############################## def h(x, y): return sqrt(sq(x) + sq(y)) def sqrt(x): return x**0.5 def sq(x): return x ** 2 ############################## then we can "unit test" the individual functions. Here is a complete toy example of the idea: ############################### import unittest def hypo(x, y): return sqrt(sq(x) + sq(y)) def sqrt(x): return x**0.5 def sq(x): return x ** 2 class HypoTestCases(unittest.TestCase): def testSqrt(self): self.assertEqual(2, sqrt(4)) self.assertEqual(0, sqrt(0)) def testSq(self): self.assertEqual(1, sq(1)) self.assertEqual(4, sq(2)) self.assertEqual(9, sq(3)) def testHypo(self): self.assertEqual(5, hypo(3, 4)) if __name__ == '__main__': unittest.main() ################################ When we run this program, the unit tests will execute and tell us if they see any deviation between expectations and reality. ################################################ $ python hypo.py ... ---------------------------------------------------------------------- Ran 3 tests in 0.000s OK ################################################ Now let us intentionally damage one of the functions. Change the definition of sq(x): to: ############# def sq(x): return x * 2 ############# and execute the unit tests again. We'll now see something very interesting: ############################################################################## $ python hypo.py FF. ====================================================================== FAIL: testHypo (__main__.HypoTestCases) ---------------------------------------------------------------------- Traceback (most recent call last): File "hypo.py", line 23, in testHypo self.assertEqual(5, hypo(3, 4)) AssertionError: 5 != 3.7416573867739413 ====================================================================== FAIL: testSq (__main__.HypoTestCases) ---------------------------------------------------------------------- Traceback (most recent call last): File "hypo.py", line 18, in testSq self.assertEqual(1, sq(1)) AssertionError: 1 != 2 ---------------------------------------------------------------------- Ran 3 tests in 0.000s FAILED (failures=2) ############################################################################## Our "unit test code" will automatically report if the actual output of a function mismatches against the expected output. Flaws in auxiliary functions can be reported, which means that if we do see problems, we can more easily scope what the issue is. Note: from the unit test error output, we know that sqrt() is probably ok, but hypo() and sq() are probably broken. And since hypo()'s definition depends on sq(), it naturally leads us to investigate sq() first. It is the use of tests on individual units of our code that lets us determine a fault without having to look at the entire aggregate program. Contrast this with the situation you're in at the moment: you're currently stuck trying to debug the *entire program* when something goes wrong. Unfortunately, this strategy doesn't scale when programs get too large. An effective mechanism that programmers use is to unit test the individual functions. From oscar.j.benjamin at gmail.com Tue Mar 17 00:10:59 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Mon, 16 Mar 2015 23:10:59 +0000 Subject: [Tutor] Fitting data to error function In-Reply-To: References: Message-ID: On 16 March 2015 at 22:22, Colin Ross wrote: > > Yes, thank you, they were suppose to both be E_out. Hi Colin, I'm not sure if that means that your problem is fixed or not but I thought I would point something out that helps in fixing this kind of problem. You're using ipython which has an excellent debugger. So let's say I have the following buggy (and not very useful) program called tmp.py: from numpy.fft import fft def g(z): return str(z) def f(x): return fft(g(x)) f(1) I can run the program under ipython by typing "run tmp.py": $ ipython Python 2.7.6 (default, Mar 22 2014, 22:59:56) Type "copyright", "credits" or "license" for more information. IPython 1.2.1 -- An enhanced Interactive Python. ? -> Introduction and overview of IPython's features. %quickref -> Quick reference. help -> Python's own help system. object? -> Details about 'object', use 'object??' for extra details. In [1]: run tmp.py --------------------------------------------------------------------------- IndexError Traceback (most recent call last) /usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where) 202 else: 203 filename = fname --> 204 __builtin__.execfile(filename, *where) /stuff/oscar/work/current/tmp/ipdb/tmp.py in () 7 return fft(g(x)) 8 ----> 9 f(1) /stuff/oscar/work/current/tmp/ipdb/tmp.py in f(x) 5 6 def f(x): ----> 7 return fft(g(x)) 8 9 f(1) /usr/lib/python2.7/dist-packages/numpy/fft/fftpack.pyc in fft(a, n, axis) 172 """ 173 --> 174 return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftf, _fft_cache) 175 176 /usr/lib/python2.7/dist-packages/numpy/fft/fftpack.pyc in _raw_fft(a, n, axis, init_function, work_function, fft_cache) 48 49 if n is None: ---> 50 n = a.shape[axis] 51 52 if n < 1: IndexError: tuple index out of range That's great because the information that I see shows me where the error occurs - if I know how to read it. In this case I look up until I find a line that is part of my own code i.e. line 7 of tmp.py. That's where I passed something into the numpy fft function which lead to an error. At this point I can try to understand what is the object that is being passed into fft by looking at my code. Or I can use the ipython debugger to manually inspect the object. Turn the debugger on by typing "pdb". Then run your script again: In [2]: pdb Automatic pdb calling has been turned ON In [3]: run tmp.py --------------------------------------------------------------------------- IndexError Traceback (most recent call last) /usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where) 202 else: 203 filename = fname --> 204 __builtin__.execfile(filename, *where) /stuff/oscar/work/current/tmp/ipdb/tmp.py in () 7 return fft(g(x)) 8 ----> 9 f(1) /stuff/oscar/work/current/tmp/ipdb/tmp.py in f(x) 5 6 def f(x): ----> 7 return fft(g(x)) 8 9 f(1) /usr/lib/python2.7/dist-packages/numpy/fft/fftpack.pyc in fft(a, n, axis) 172 """ 173 --> 174 return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftf, _fft_cache) 175 176 /usr/lib/python2.7/dist-packages/numpy/fft/fftpack.pyc in _raw_fft(a, n, axis, init_function, work_function, fft_cache) 48 49 if n is None: ---> 50 n = a.shape[axis] 51 52 if n < 1: IndexError: tuple index out of range > /usr/lib/python2.7/dist-packages/numpy/fft/fftpack.py(50)_raw_fft() 49 if n is None: ---> 50 n = a.shape[axis] 51 ipdb> u > /usr/lib/python2.7/dist-packages/numpy/fft/fftpack.py(174)fft() 173 --> 174 return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftf, _fft_cache) 175 ipdb> u > /stuff/oscar/work/current/tmp/ipdb/tmp.py(7)f() 6 def f(x): ----> 7 return fft(g(x)) 8 ipdb> p x 1 ipdb> p g(x) '1' ipdb> p type(g(x)) The debugger with the ipdb prompt freezes the script at the point where the error occurs and enables me to manually inspect what is going on. It started in the numpy code so I typed "u" twice to go "up" two frames to where my function called the function that called the function that generated the exception. Here I can query the object x (typing "p" for "print") and test what g(x) returns. Now the problem is clear to me: I'm passing a string into fft when it is expecting an array of numbers. The python interpreter comes with a slightly less convenient debugger called pdb that you can use in the same way if you run your script from the terminal like so: $ python -m pdb tmp.py > /stuff/oscar/work/current/tmp/ipdb/tmp.py(1)() -> from numpy.fft import fft (Pdb) c Traceback (most recent call last): File "/usr/lib/python2.7/pdb.py", line 1314, in main pdb._runscript(mainpyfile) File "/usr/lib/python2.7/pdb.py", line 1233, in _runscript self.run(statement) File "/usr/lib/python2.7/bdb.py", line 400, in run exec cmd in globals, locals File "", line 1, in File "tmp.py", line 1, in from numpy.fft import fft File "tmp.py", line 7, in f return fft(g(x)) File "/usr/lib/python2.7/dist-packages/numpy/fft/fftpack.py", line 174, in fft return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftf, _fft_cache) File "/usr/lib/python2.7/dist-packages/numpy/fft/fftpack.py", line 50, in _raw_fft n = a.shape[axis] IndexError: tuple index out of range Uncaught exception. Entering post mortem debugging Running 'cont' or 'step' will restart the program > /usr/lib/python2.7/dist-packages/numpy/fft/fftpack.py(50)_raw_fft() -> n = a.shape[axis] (Pdb) u > /usr/lib/python2.7/dist-packages/numpy/fft/fftpack.py(174)fft() -> return _raw_fft(a, n, axis, fftpack.cffti, fftpack.cfftf, _fft_cache) (Pdb) u > /stuff/oscar/work/current/tmp/ipdb/tmp.py(7)f() -> return fft(g(x)) (Pdb) p x 1 (Pdb) p g(x) '1' Oscar From dbasberg at comcast.net Mon Mar 16 21:39:06 2015 From: dbasberg at comcast.net (Doug Basberg) Date: Mon, 16 Mar 2015 16:39:06 -0400 Subject: [Tutor] passing dictionaries through a file Message-ID: <040301d06029$3fa862f0$bef928d0$@net> I would like to pass the contents of a dictionary from one program to another through a file. So, what is the elegant way to pass a dictionary by file? My early learning about Python seems to indicate that only ascii is easily passed in files. I am using Python 2.7.4 now. Should I be learning one of the serialization methods? Can I use ascii in the file and easily put that back into a dictionary in the file read program? I will be very happy to hear alternatives that I can read about and learn more about Python. In C++, I was very happy using structs and classes with serialization. Is the Python solution somewhat similar to C++? Thanks, Doug From rajbirs799 at gmail.com Mon Mar 16 19:29:36 2015 From: rajbirs799 at gmail.com (Rajbir Singh) Date: Mon, 16 Mar 2015 23:59:36 +0530 Subject: [Tutor] set current working dir Message-ID: i need to know how i can set current working dir in an executing phython using os module From alan.gauld at btinternet.com Tue Mar 17 00:57:57 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 16 Mar 2015 23:57:57 +0000 Subject: [Tutor] set current working dir In-Reply-To: References: Message-ID: On 16/03/15 18:29, Rajbir Singh wrote: > i need to know how i can set current working dir in an executing phython > using os module try os.chdir('/some/path/here') -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From oscar.j.benjamin at gmail.com Tue Mar 17 01:03:21 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 17 Mar 2015 00:03:21 +0000 Subject: [Tutor] passing dictionaries through a file In-Reply-To: <040301d06029$3fa862f0$bef928d0$@net> References: <040301d06029$3fa862f0$bef928d0$@net> Message-ID: On 16 March 2015 at 20:39, Doug Basberg wrote: > > I would like to pass the contents of a dictionary from one program to > another through a file. So, what is the elegant way to pass a dictionary by > file? My early learning about Python seems to indicate that only ascii is > easily passed in files. I am using Python 2.7.4 now. Should I be learning > one of the serialization methods? Can I use ascii in the file and easily > put that back into a dictionary in the file read program? > > I will be very happy to hear alternatives that I can read about and learn > more about Python. In C++, I was very happy using structs and classes with > serialization. Is the Python solution somewhat similar to C++? I assume that when you say "dictionary" you mean a dict object. The best way does depend on what kind of objects are used as the keys and values of the dict. Assuming that the keys are strings and that the values are relatively simple objects like strings and integers or lists of strings etc. then you should be able to use the json module to export the dict in json format. This format looks similar to the Python code for creating a dict and can be read into programs written in many different languages (not just Python). Here's a simple example: >>> d = {'asd': 123, 'qwe': 456} >>> import json # Import the json module json.dumps shows us what the json text for the dict would look like: >>> json.dumps(d) '{"qwe": 456, "asd": 123}' We can save it directly to a file like so: >>> with open('dict.txt', 'w') as fout: ... json.dump(d, fout) ... We can then read it in again in another program with: >>> with open('dict.txt', 'r') as fin: ... d2 = json.load(fin) ... >>> d2 {'qwe': 456, 'asd': 123} You can read more about Python's json module here: https://docs.python.org/2/library/json.html Oscar From alan.gauld at btinternet.com Tue Mar 17 01:04:21 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 17 Mar 2015 00:04:21 +0000 Subject: [Tutor] passing dictionaries through a file In-Reply-To: <040301d06029$3fa862f0$bef928d0$@net> References: <040301d06029$3fa862f0$bef928d0$@net> Message-ID: On 16/03/15 20:39, Doug Basberg wrote: > I would like to pass the contents of a dictionary from one program to > another through a file. So, what is the elegant way to pass a dictionary by > file? The elegant way may be not to use a file. A Python dictionary usually converts easily to JSON format which can be sent directly between processes over http. If you control the receiving program. Alternatively, you could save the JSON into a file and pass that between processes. If the receiving program knows about JSON. Or you could just use the Python pickle module to save the dictionary. If the receiving program is in Python. Or you could write the dictionary out to a CSV file using the csv module. If the other program is not under your control and needs to use standard format files. (eg Excel) > more about Python. In C++, I was very happy using structs and classes with > serialization. Is the Python solution somewhat similar to C++? You can do it manually but in Python there is usually a better way. One of the options above should work for you, it depends on how much control you have over the receiving program code. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From oscar.j.benjamin at gmail.com Tue Mar 17 01:04:51 2015 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Tue, 17 Mar 2015 00:04:51 +0000 Subject: [Tutor] set current working dir In-Reply-To: References: Message-ID: On 16 March 2015 at 18:29, Rajbir Singh wrote: > > i need to know how i can set current working dir in an executing phython > using os module If you explain why you want to do that then it may be that there is a better way to achieve what you want. It's usually not necessary to change the current working directory. Oscar From ben+python at benfinney.id.au Tue Mar 17 01:33:48 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 17 Mar 2015 11:33:48 +1100 Subject: [Tutor] passing dictionaries through a file References: <040301d06029$3fa862f0$bef928d0$@net> Message-ID: <853854tqc3.fsf@benfinney.id.au> "Doug Basberg" writes: > I would like to pass the contents of a dictionary from one program to > another through a file. The term for this in programming is ?serialisation?, in the sense that a specific series of bits will represent the Python value, and you want to convert to and from that series of bits. More generally, having data values persist beyond the life of a program is termed ?persistence? of the data. > So, what is the elegant way to pass a dictionary by file? The Python standard library has several serialisation / persistence tools. -- \ ?Software patents provide one more means of controlling access | `\ to information. They are the tool of choice for the internet | _o__) highwayman.? ?Anthony Taylor | Ben Finney From ben+python at benfinney.id.au Tue Mar 17 01:35:45 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 17 Mar 2015 11:35:45 +1100 Subject: [Tutor] set current working dir References: Message-ID: <85y4mwsboe.fsf@benfinney.id.au> Rajbir Singh writes: > i need to know how i can set current working dir in an executing > phython using os module Why must it be using the ?os? module? It's generally a bad idea to assume you need a specific module to provide the solution; better to describe what you need to do, and not assume which module will provide it. As it turns out, the documentation for the ?os? module does show it has what you need . But presumably since you know the name of that module you already looked there? -- \ ?I am as agnostic about God as I am about fairies and the | `\ Flying Spaghetti Monster.? ?Richard Dawkins, 2006-10-13 | _o__) | Ben Finney From pythonlearner at yahoo.com Tue Mar 17 05:13:15 2015 From: pythonlearner at yahoo.com (Nick Nguyen) Date: Tue, 17 Mar 2015 04:13:15 +0000 (UTC) Subject: [Tutor] print string using triple quote Message-ID: <567558233.123048.1426565595398.JavaMail.yahoo@mail.yahoo.com> Hi, I use python 3.4.3. I'm using print function with triple quote, as I understand all the character will be printed as exactly within the triple quote, even with the backslash character. However as you see in the RESULT printout, the first backslash come out OK, but it misses the last backslash, unless I put "\\". print(? ? ? ? '''? ? ? ____ ? ?_ ? ? ? __ ?______ ? _____? ? ?/ ?_ ? ?\ ? | | ? ? / / ? ?| ? ___| ? ?| ?_ ? ? \? ? ?? ? ? ? ? ?'''? ? ? ? ) RESULT: ? ? ? ____ ? ?_ ? ? ? __ ?______ ? _____? ? ?/ ?_ ? ?\ ?| ?| ? ? / / ? ?| ?___| ? ? | ?_ ? ? ?? Thank for your help.--nick From alan.gauld at btinternet.com Tue Mar 17 10:54:55 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Tue, 17 Mar 2015 09:54:55 +0000 Subject: [Tutor] print string using triple quote In-Reply-To: <567558233.123048.1426565595398.JavaMail.yahoo@mail.yahoo.com> References: <567558233.123048.1426565595398.JavaMail.yahoo@mail.yahoo.com> Message-ID: On 17/03/15 04:13, Nick Nguyen wrote: > > Hi, > I use python 3.4.3. > I'm using print function with triple quote, > as I understand all the character will be printed > as exactly within the triple quote, even with > the backslash character. You understand wrongly. Triple quotes are no different to any other kind of quote except that they can span lines. If you want to print all the characters (with any kind of quote) you must precede the quotes with r, for raw. >>> print ("""A string with \t in it""") A string with in it >>> print (r"""A string with \t in it""") A string with \t in it >>> HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From davea at davea.name Tue Mar 17 11:33:49 2015 From: davea at davea.name (Dave Angel) Date: Tue, 17 Mar 2015 06:33:49 -0400 Subject: [Tutor] set current working dir In-Reply-To: References: Message-ID: <5508030D.1070803@davea.name> On 03/16/2015 02:29 PM, Rajbir Singh wrote: > i need to know how i can set current working dir in an executing phython > using os module os.chdir() will change the current directory, but it changes it for the whole program (all threads), and the change lasts till the program terminates. Very often, one of these "buts" is a problem, so most people prefer to find a different way of solving the problem. I prefer to have a policy of never changing the current directory once started, and for all file names make them either relative to that original current directory, or just make them absolute. There are a number of functions that let you manipulate the file name strings, mostly in os.path -- DaveA From davea at davea.name Tue Mar 17 11:40:34 2015 From: davea at davea.name (Dave Angel) Date: Tue, 17 Mar 2015 06:40:34 -0400 Subject: [Tutor] print string using triple quote In-Reply-To: References: <567558233.123048.1426565595398.JavaMail.yahoo@mail.yahoo.com> Message-ID: <550804A2.3060601@davea.name> On 03/17/2015 05:54 AM, Alan Gauld wrote: > On 17/03/15 04:13, Nick Nguyen wrote: >> >> Hi, >> I use python 3.4.3. >> I'm using print function with triple quote, > > > as I understand all the character will be printed >> as exactly within the triple quote, even with > > the backslash character. > > You understand wrongly. > Triple quotes are no different to any other kind > of quote except that they can span lines. If you want > to print all the characters (with any kind of quote) > you must precede the quotes with r, for raw. > > >>> print ("""A string with \t in it""") > A string with in it > >>> print (r"""A string with \t in it""") > A string with \t in it > >>> And a raw string literal cannot easily have a backslash as the final character. Sometimes it's good enough to just add a space at the end of a raw string. -- DaveA From robertvstepp at gmail.com Tue Mar 17 13:26:16 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 17 Mar 2015 07:26:16 -0500 Subject: [Tutor] print string using triple quote In-Reply-To: <567558233.123048.1426565595398.JavaMail.yahoo@mail.yahoo.com> References: <567558233.123048.1426565595398.JavaMail.yahoo@mail.yahoo.com> Message-ID: On Mon, Mar 16, 2015 at 11:13 PM, Nick Nguyen wrote: > > Hi, > I use python 3.4.3. > I'm using print function with triple quote, as I understand all the character will be printed as exactly within the triple quote, even with the backslash character. However as you see in the RESULT printout, the first backslash come out OK, but it misses the last backslash, unless I put "\\". > print( ''' ____ _ __ ______ _____ / _ \ | | / / | ___| | _ \ > ''' ) > > RESULT: > > ____ _ __ ______ _____ / _ \ | | / / | ___| | _ > I started a thread recently: http://code.activestate.com/lists/python-tutor/103394/ essentially about this same topic. The problem is the backslash character is used to initiate "escape" sequences. And since every line ends with with "invisible" line termination character(s) (newline, \n, carriage return, \r), the backslash at the end of a line interacts with what is there, whether you can see it or not. -- boB From davidheiserca at gmail.com Tue Mar 17 15:56:40 2015 From: davidheiserca at gmail.com (David Heiser) Date: Tue, 17 Mar 2015 07:56:40 -0700 Subject: [Tutor] passing dictionaries through a file In-Reply-To: References: <040301d06029$3fa862f0$bef928d0$@net> Message-ID: <550840A8.1020204@gmail.com> On 3/16/2015 5:04 PM, Alan Gauld wrote: > On 16/03/15 20:39, Doug Basberg wrote: >> I would like to pass the contents of a dictionary from one program to >> another through a file. So, what is the elegant way to pass a >> dictionary by >> file? > > The elegant way may be not to use a file. > A Python dictionary usually converts easily > to JSON format which can be sent directly > between processes over http. If you control > the receiving program. > > Alternatively, you could save the JSON into a > file and pass that between processes. If the > receiving program knows about JSON. > > Or you could just use the Python pickle module > to save the dictionary. If the receiving program > is in Python. > > Or you could write the dictionary out to a CSV > file using the csv module. If the other program > is not under your control and needs to use > standard format files. (eg Excel) > > >> more about Python. In C++, I was very happy using structs and >> classes with >> serialization. Is the Python solution somewhat similar to C++? > > You can do it manually but in Python there is usually > a better way. One of the options above should work > for you, it depends on how much control you have > over the receiving program code. > > The "shelve" module is ideally suited to storing dictionary type data to a file. It's a simplification of "pickle". From dbasberg at comcast.net Tue Mar 17 23:30:03 2015 From: dbasberg at comcast.net (Doug Basberg) Date: Tue, 17 Mar 2015 18:30:03 -0400 Subject: [Tutor] using json to pass a dict thru a file Message-ID: <00b101d06101$e9ffc760$bdff5620$@net> I appreciate the advise to use json to pass a dict thru a file. Below is the code To 'dump' the dict to a file and the code to 'load' the dict and the error message I get testing this. What am I doing wrong? Thanks. //////////////////////////////////////////////////////////////////////////// // ///// code to create the file with json ///////////////////////////////////// //////////////////////////////////////////////////////////////////////////// // #! /usr/bin/python ############################################################################ ## # file = myTest00.py # try out concepts for use in 5500 Power project # ############################################################################ ## ##### Module history ####################################################### ############################################################################ ## # import time as t import json tymNow = t.time() tymLocal = t.localtime() tymAsc = t.asctime(tymLocal) dataDict = {'Time': None, 'Vbatt': None, 'Ichrg': None, 'Vpanel': None} vb = 51.25 ic = 5.89 vp = 55.78 i = 0 dataDict['Time'] = tymAsc dataDict['Vbatt'] = vb dataDict['Ichrg'] = ic dataDict['Vpanel'] = vp fn = 'testData01.dat' f = open(fn, 'ab') for i in xrange(5): json.dump(dataDict, f) # f.write(str(dataDict)) # f.write('\n\r') tymAsc = t.asctime(t.localtime()) dataDict['Time'] = tymAsc dataDict['Vbatt'] = vb + i dataDict['Ichrg'] = ic + i dataDict['Vpanel'] = vp + i f.close() print('*********** done ********** \n\r') //////////////////////////////////////////////////////////////////////////// // ///// code to use json to load the dict from the file ////////////////////// //////////////////////////////////////////////////////////////////////////// // #! /usr/bin/python ############################################################################ ## # file = myTestRcv00.py # try out concepts for use in 5500 Power project # ############################################################################ ## ##### Module history ####################################################### ############################################################################ ## # import json #dataDict = {} fn = 'testData01.dat' f = open(fn, 'r') dataDict = json.load(f) f.close() print(type(dataDict),dataDict['Time']) print('Vbatt =', dataDict['Vbatt']) print('*********** done ********** \n\r') //////////////////////////////////////////////////////////////////////////// /////// ////// ERROR FROM RUNNING THE JSON LOAD PROGRAM ///////////////////////////////// //////////////////////////////////////////////////////////////////////////// /////// Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> Traceback (most recent call last): File "C:/Python27/myTestRcv00.py", line 17, in dataDict = json.load(f) File "C:\Python27\lib\json\__init__.py", line 290, in load **kw) File "C:\Python27\lib\json\__init__.py", line 338, in loads return _default_decoder.decode(s) File "C:\Python27\lib\json\decoder.py", line 368, in decode raise ValueError(errmsg("Extra data", s, end, len(s))) ValueError: Extra data: line 1 column 85 - line 1 column 421 (char 84 - 420) >>> From davea at davea.name Wed Mar 18 03:02:13 2015 From: davea at davea.name (Dave Angel) Date: Tue, 17 Mar 2015 22:02:13 -0400 Subject: [Tutor] using json to pass a dict thru a file In-Reply-To: <00b101d06101$e9ffc760$bdff5620$@net> References: <00b101d06101$e9ffc760$bdff5620$@net> Message-ID: <5508DCA5.5030904@davea.name> On 03/17/2015 06:30 PM, Doug Basberg wrote: > I appreciate the advise to use json to pass a dict thru a file. Below is > the code > To 'dump' the dict to a file and the code to 'load' the dict and the error > message > I get testing this. > > What am I doing wrong? Thanks. First two things I see are that you used the "ab" mode to write the file, and "r" to read it. if you're appending, then the second time you run it, there'll be extra junk at the end. If you're reading in text mode, then linefeeds might get abused. When dealing with binary data, be sure and use "rb". ..... > > Traceback (most recent call last): > File "C:/Python27/myTestRcv00.py", line 17, in > dataDict = json.load(f) > File "C:\Python27\lib\json\__init__.py", line 290, in load > **kw) > File "C:\Python27\lib\json\__init__.py", line 338, in loads > return _default_decoder.decode(s) > File "C:\Python27\lib\json\decoder.py", line 368, in decode > raise ValueError(errmsg("Extra data", s, end, len(s))) > ValueError: Extra data: line 1 column 85 - line 1 column 421 (char 84 - 420) -- DaveA From nethiprudhvi at gmail.com Wed Mar 18 03:15:24 2015 From: nethiprudhvi at gmail.com (prudhvi nethi) Date: Tue, 17 Mar 2015 19:15:24 -0700 Subject: [Tutor] PyBrain: Question on creating customized DataSet Message-ID: Hi, I'm trying to create a data set for using it with BackpropTrainer *(PyBrain)* The model is that i have a bunch of inputs (which can have multiple values). Resulting in some Output Kx( different values). Sample data ( A subset of the data ) Input (x) Input (y) Input (z) Input(a) Output(k) X1 Y1 Z1 A1 K1 X1 Y2 Z2 A3 K2 X2 Y3 Z2 A2 K1 Where Input x can have any of the following value in a specific entry: (X1,X2,x2) *An example from PyBrain Documentation for a simple XOR data set modeling:* >>> ds.addSample((0, 0), (0,))>>> ds.addSample((0, 1), (1,))>>> ds.addSample((1, 0), (1,))>>> ds.addSample((1, 1), (0,)) As the function is simple here (XOR) and the inputs and o/p's can be represented in binary format. How can i model a data set for a slightly complex like mine ? *Please let me know if i mis-speak or interpret something here.* -Prudhvi From alan.gauld at btinternet.com Wed Mar 18 10:22:22 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 18 Mar 2015 09:22:22 +0000 Subject: [Tutor] using json to pass a dict thru a file In-Reply-To: <00b101d06101$e9ffc760$bdff5620$@net> References: <00b101d06101$e9ffc760$bdff5620$@net> Message-ID: On 17/03/15 22:30, Doug Basberg wrote: > dataDict = {'Time': None, 'Vbatt': None, 'Ichrg': None, 'Vpanel': None} > > vb = 51.25 > ic = 5.89 > vp = 55.78 > i = 0 > > dataDict['Time'] = tymAsc > dataDict['Vbatt'] = vb > dataDict['Ichrg'] = ic > dataDict['Vpanel'] = vp > > fn = 'testData01.dat' > f = open(fn, 'ab') I don;t believe json needs a binary file. Also do you really want to append data? > for i in xrange(5): > json.dump(dataDict, f) > # f.write(str(dataDict)) > # f.write('\n\r') > tymAsc = t.asctime(t.localtime()) > dataDict['Time'] = tymAsc > dataDict['Vbatt'] = vb + i > dataDict['Ichrg'] = ic + i > dataDict['Vpanel'] = vp + i > f.close() > print('*********** done ********** \n\r') You are effectively creating 5 separate dictionaries in your json file. > fn = 'testData01.dat' > f = open(fn, 'r') > dataDict = json.load(f) > f.close() But you only read one back > Traceback (most recent call last): > File "C:/Python27/myTestRcv00.py", line 17, in > dataDict = json.load(f) > File "C:\Python27\lib\json\__init__.py", line 290, in load > **kw) > File "C:\Python27\lib\json\__init__.py", line 338, in loads > return _default_decoder.decode(s) > File "C:\Python27\lib\json\decoder.py", line 368, in decode > raise ValueError(errmsg("Extra data", s, end, len(s))) > ValueError: Extra data: line 1 column 85 - line 1 column 421 (char 84 - 420) >>>> Python is therefore confused. Try your experiment using just one dictionary in and one dictionary out. Once you have that working write multiple dictionaries and then read them back. Incidentally, you can check the content of your json file by opening it in a text editor, it is a human readable format. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Wed Mar 18 10:30:41 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 18 Mar 2015 09:30:41 +0000 Subject: [Tutor] PyBrain: Question on creating customized DataSet In-Reply-To: References: Message-ID: On 18/03/15 02:15, prudhvi nethi wrote: > Hi, > > I'm trying to create a data set for using it with BackpropTrainer > *(PyBrain)* Your question seems to be about pybrain rather than about Python. There is a pybrain google group: http://groups.google.com/group/pybrain and a general email link contact at pybrain.org You will probably get more specific help there. > The model is that i have a bunch of inputs (which can have multiple > values). Resulting in some Output Kx( different values). > > Sample data ( A subset of the data ) > > Input (x) > Input (y) > Input (z) > Input(a) > Output(k) > > X1 > Y1 > Z1 > A1 > K1 > X1 ... > Where Input x can have any of the following value in a specific entry: > (X1,X2,x2) > > *An example from PyBrain Documentation for a simple XOR data set modeling:* > >>>> ds.addSample((0, 0), (0,))>>> ds.addSample((0, 1), (1,))>>> ds.addSample((1, 0), (1,))>>> ds.addSample((1, 1), (0,)) I'm afraid that's making no sense to me whatsoever. Maybe someone else can decipher it. But I suggest you try the pybrain community. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From solomon.vimal at gmail.com Wed Mar 18 17:33:34 2015 From: solomon.vimal at gmail.com (Solomon Vimal) Date: Wed, 18 Mar 2015 12:33:34 -0400 Subject: [Tutor] COM(ActiveX) class - how to list methods? Message-ID: Hi, I am a newbie in python. I am more used to MATLAB. In MATLAB there is a function methodsview(h) that will pop up a window listing out all the methods of a handle (h) referring to a Windows COM object - in my case, a software controller. Is there is a similar function in Python? Maybe there is a generic function that lists methods applicable to a class. I tried dir, __dict__, and inspect.getmembers() - no luck yet! I would really appreciate any pointer. Thanks a lot, Solomon Vimal (+1)9198690115 From alan.gauld at btinternet.com Wed Mar 18 18:24:05 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 18 Mar 2015 17:24:05 +0000 Subject: [Tutor] COM(ActiveX) class - how to list methods? In-Reply-To: References: Message-ID: On 18/03/15 16:33, Solomon Vimal wrote: > I am a newbie in python. I am more used to MATLAB. > > In MATLAB there is a function methodsview(h) that will pop up a window > listing out all the methods of a handle (h) referring to a Windows COM > object - in my case, a software controller. If its a COM question you are probably better off asking on the Python Windows mailing list. It's available on gmane.org as gmane.comp.python.windows. > Is there is a similar function in Python? Maybe there is a generic > function that lists methods applicable to a class. There is no generic solution that works with COM. However, if you are accessing COM via pythonwin (aka PyWin32) you will have access to the Pythonwin IDE which includes a COM object browser which will, I think, give you what you want. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From dbasberg at comcast.net Wed Mar 18 19:28:49 2015 From: dbasberg at comcast.net (Doug Basberg) Date: Wed, 18 Mar 2015 14:28:49 -0400 Subject: [Tutor] Is there a 'hook' to capture all exits from a python program? Message-ID: <018401d061a9$6131b800$23952800$@net> I do real-time controls programming and I control a relay in a python program that must be turned off before the program ends. First, the program should never exit unless I do it explicitly. Is there a 'hook' to process before any exit (even if not expected). I must turn off that relay or harm may occur. Thanks, Doug From breamoreboy at yahoo.co.uk Wed Mar 18 20:43:51 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Wed, 18 Mar 2015 19:43:51 +0000 Subject: [Tutor] Is there a 'hook' to capture all exits from a python program? In-Reply-To: <018401d061a9$6131b800$23952800$@net> References: <018401d061a9$6131b800$23952800$@net> Message-ID: On 18/03/2015 18:28, Doug Basberg wrote: > I do real-time controls programming and I control a relay in a python > program that must be turned off before the program ends. First, the program > should never exit unless I do it explicitly. Is there a 'hook' to process > before any exit (even if not expected). I must turn off that relay or harm > may occur. Thanks, Doug > https://docs.python.org/3/library/atexit.html -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From fomcl at yahoo.com Wed Mar 18 21:13:01 2015 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Wed, 18 Mar 2015 13:13:01 -0700 Subject: [Tutor] Is there a 'hook' to capture all exits from a python program? Message-ID: <1426709581.73993.BPMail_high_carrier@web163806.mail.gq1.yahoo.com> ----------------------------- On Wed, Mar 18, 2015 8:43 PM CET Mark Lawrence wrote: >On 18/03/2015 18:28, Doug Basberg wrote: >> I do real-time controls programming and I control a relay in a python >> program that must be turned off before the program ends. First, the program >> should never exit unless I do it explicitly. Is there a 'hook' to process >> before any exit (even if not expected). I must turn off that relay or harm >> may occur. Thanks, Doug >> > >https://docs.python.org/3/library/atexit.html ... But that's only for normal program termination; sys.excepthook is for unexpected exits. I would be interested to hear about a module that does both. From alan.gauld at btinternet.com Wed Mar 18 23:59:35 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Wed, 18 Mar 2015 22:59:35 +0000 Subject: [Tutor] Is there a 'hook' to capture all exits from a python program? In-Reply-To: <018401d061a9$6131b800$23952800$@net> References: <018401d061a9$6131b800$23952800$@net> Message-ID: On 18/03/15 18:28, Doug Basberg wrote: > I do real-time controls programming and I control a relay in a python > program that must be turned off before the program ends. You don't say what your environment is but you should be aware that Python is not really a suitable tool for real time programming, especially if it safety critical. The same applies to most(all?) scripting languages and especially if running on a time sharing OS like Unix or windows. If your code is not truly real-time or the device is not safety critical you may get away with it. But if you are likely to get sued for loss of life or major damage consider your options. That having been said there are several things you can do to safeguard yourself. Others have mentioned the exit hooks in the sys module, You can also try wrapping your entire main program in a try/finally. You could also monitor the interpreter in an external program that turns off your device directly if the interpreter fails. But none of these can be guaranteed to happen in real time indeed, the last option can be guaranteed not to be... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Thu Mar 19 00:55:34 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 19 Mar 2015 10:55:34 +1100 Subject: [Tutor] Is there a 'hook' to capture all exits from a python program? In-Reply-To: References: <018401d061a9$6131b800$23952800$@net> Message-ID: <20150318235534.GT7655@ando.pearwood.info> On Wed, Mar 18, 2015 at 10:59:35PM +0000, Alan Gauld wrote: > On 18/03/15 18:28, Doug Basberg wrote: > >I do real-time controls programming and I control a relay in a python > >program that must be turned off before the program ends. > > You don't say what your environment is but you should be aware > that Python is not really a suitable tool for real time > programming, especially if it safety critical. The same applies > to most(all?) scripting languages and especially if running > on a time sharing OS like Unix or windows. I wouldn't want to use Python to control the critical parts of a nuclear reactor or a plane's autopilot, but people use it to control mechanical devices in near-real-time. There's an old-timer, Gene Heskett, on the comp.lang.python newsgroup who (I think) uses Python to drive some sort of circuit board mechanical etching machine. Using Python to control (say) an Arduino is pretty popular too. But I guess you'll rightly say these things don't count as "real-time". -- Steve From alan.gauld at btinternet.com Thu Mar 19 02:07:13 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 19 Mar 2015 01:07:13 +0000 Subject: [Tutor] Is there a 'hook' to capture all exits from a python program? In-Reply-To: <20150318235534.GT7655@ando.pearwood.info> References: <018401d061a9$6131b800$23952800$@net> <20150318235534.GT7655@ando.pearwood.info> Message-ID: On 18/03/15 23:55, Steven D'Aprano wrote: >> You don't say what your environment is but you should be aware >> that Python is not really a suitable tool for real time >> programming, especially if it safety critical. > I wouldn't want to use Python to control the critical parts of a nuclear > reactor or a plane's autopilot, but people use it to control mechanical > devices in near-real-time. Yes, and that's the crux. Near real-time is fine, especially if nobody will die. > comp.lang.python newsgroup who (I think) uses Python to drive some sort > of circuit board mechanical etching machine. > > Using Python to control (say) an Arduino is pretty popular too. Yes, I use Python on my Raspberry Pi and on an Arduino, it's fine for that kind of thing. (Actually the Arduino can do real real-time stuff, but the Pi can't.) My concern was the OPs comment that: " I must turn off that relay or harm may occur." Now if the 'harm' is just a mucked up circuit board it's not such a big deal. But, if its a flooded field, a damaged machine costing $100Ks or even a person's life then it's important to point out that python may not be the tool of choice! Similarly, if the time to turn it off is more than a second, say, its not an issue. But if its less than a couple of milliseconds then I'd be worried. Maybe I spent too many years designing hard-real-time safety critical systems. But a little bell always goes off in my head when I read stuff like "...or harm may occur" -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From breamoreboy at yahoo.co.uk Thu Mar 19 02:31:06 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 19 Mar 2015 01:31:06 +0000 Subject: [Tutor] Is there a 'hook' to capture all exits from a python program? In-Reply-To: References: <018401d061a9$6131b800$23952800$@net> <20150318235534.GT7655@ando.pearwood.info> Message-ID: On 19/03/2015 01:07, Alan Gauld wrote: > On 18/03/15 23:55, Steven D'Aprano wrote: > >>> You don't say what your environment is but you should be aware >>> that Python is not really a suitable tool for real time >>> programming, especially if it safety critical. > >> I wouldn't want to use Python to control the critical parts of a nuclear >> reactor or a plane's autopilot, but people use it to control mechanical >> devices in near-real-time. > > Yes, and that's the crux. Near real-time is fine, especially if > nobody will die. > >> comp.lang.python newsgroup who (I think) uses Python to drive some sort >> of circuit board mechanical etching machine. >> >> Using Python to control (say) an Arduino is pretty popular too. > > Yes, I use Python on my Raspberry Pi and on an Arduino, it's fine for > that kind of thing. (Actually the Arduino can do real real-time stuff, > but the Pi can't.) > > My concern was the OPs comment that: > " I must turn off that relay or harm may occur." > > Now if the 'harm' is just a mucked up circuit board it's not such > a big deal. > > But, if its a flooded field, a damaged machine costing $100Ks or even a > person's life then it's important to point out that python may not > be the tool of choice! > > Similarly, if the time to turn it off is more than a second, say, > its not an issue. But if its less than a couple of milliseconds > then I'd be worried. > > Maybe I spent too many years designing hard-real-time safety > critical systems. But a little bell always goes off in my head > when I read stuff like "...or harm may occur" > > All depends on your definition of real time. Most systems we usually mean pretty darn fast, but the control system for the rudder on a super tanker is real time but takes minutes for the command to change course to take effect. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From robertvstepp at gmail.com Thu Mar 19 05:20:17 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 18 Mar 2015 23:20:17 -0500 Subject: [Tutor] List comprehensions to search a list--amazing! Message-ID: I hope extolling the beauty and power of Python on this list is allowed, because I have had a large "WOW!!!" moment tonight. I had a problem I was working on at work this afternoon. I have a list of ~ 10,000 floating point numbers, which run from largest to smallest. There are duplicates scattered throughout, so I might have something like: [5942.6789, 5942.6789, 5941.000003, 5941.01, 5941.01, ... ], etc. I wanted to search the list for a test value, which, depending on the particular list (I can have many such lists, each different from the other.), could conceivably be anywhere within the given list. I needed to return the index where the list values change from being just greater than the test value to just less than the test value at the very next index position. I spent a good chunk of my afternoon writing a binary search function and wondering what theoretically the optimum search algorithm would be, got interrupted (as usual on this project), and decided to look at my books at home to see if a better solution would be staring at me from some book (Like there usually is!). I haven't studied list comprehensions formally yet, but a snippet of code in a book caught my eye where the author was discussing filtering data in a list. This led me to try: The generalized problem: L = [V0, V1, ..., Vn], where V0 >= V1 >= V2 >= ... >= Vn . Find index i, such that V[i] >= Vt >= V[i + 1], where Vt is the test value being searched for. I need to know the indices i and i + 1, which I need to interpolate based on where Vt falls. The solution (As a sublist, S) I worked out tonight after experimenting with comprehension syntax is: S = [i for i, V in enumerate(L) if L[i] >= Vt >= L[i + 1]] And, of course, the index i I need is: i = S[0] I tested this out with concrete examples in the interpreter, such as with a list, L: L = [item for item in range(10000, 0, -1)] and trying different test values. It was blazingly fast, too! All I can say is: WOW!!! -- boB From davea at davea.name Thu Mar 19 06:10:30 2015 From: davea at davea.name (Dave Angel) Date: Thu, 19 Mar 2015 01:10:30 -0400 Subject: [Tutor] List comprehensions to search a list--amazing! In-Reply-To: References: Message-ID: <550A5A46.9020409@davea.name> On 03/19/2015 12:20 AM, boB Stepp wrote: > I hope extolling the beauty and power of Python on this list is > allowed, because I have had a large "WOW!!!" moment tonight. I had a > problem I was working on at work this afternoon. I have a list of ~ > 10,000 floating point numbers, which run from largest to smallest. > There are duplicates scattered throughout, so I might have something > like: [5942.6789, 5942.6789, 5941.000003, 5941.01, 5941.01, ... ], > etc. I wanted to search the list for a test value, which, depending on > the particular list (I can have many such lists, each different from > the other.), could conceivably be anywhere within the given list. I > needed to return the index where the list values change from being > just greater than the test value to just less than the test value at > the very next index position. I spent a good chunk of my afternoon > writing a binary search function and wondering what theoretically the > optimum search algorithm would be, got interrupted (as usual on this > project), and decided to look at my books at home to see if a better > solution would be staring at me from some book (Like there usually > is!). > > I haven't studied list comprehensions formally yet, but a snippet of > code in a book caught my eye where the author was discussing filtering > data in a list. This led me to try: > > The generalized problem: > > L = [V0, V1, ..., Vn], where V0 >= V1 >= V2 >= ... >= Vn . > Find index i, such that V[i] >= Vt >= V[i + 1], where Vt is the test > value being searched for. I need to know the indices i and i + 1, > which I need to interpolate based on where Vt falls. > > The solution (As a sublist, S) I worked out tonight after > experimenting with comprehension syntax is: > S = [i for i, V in enumerate(L) if L[i] >= Vt >= L[i + 1]] > > And, of course, the index i I need is: > i = S[0] > > I tested this out with concrete examples in the interpreter, such as > with a list, L: > > L = [item for item in range(10000, 0, -1)] > > and trying different test values. It was blazingly fast, too! > > All I can say is: WOW!!! > > That's very innovative. The catch to a list comprehension is it has to visit all the elements, while a binary search would visit log-base-2 of them. So instead of 10000 elements, you'd be searching about 14 items. For large lists, it'd probably be much quicker to use the bisect module. https://docs.python.org/3.4/library/bisect.html Check out bisect.bisect_left() and bisect.bisect_right() I don't see how to directly use those functions on a list which is reverse-sorted, but the source is available. On my install, it's located at: /usr/lib/python3.4/bisect.py -- DaveA From breamoreboy at yahoo.co.uk Thu Mar 19 06:14:15 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 19 Mar 2015 05:14:15 +0000 Subject: [Tutor] List comprehensions to search a list--amazing! In-Reply-To: References: Message-ID: On 19/03/2015 04:20, boB Stepp wrote: > I hope extolling the beauty and power of Python on this list is > allowed, because I have had a large "WOW!!!" moment tonight. I had a > problem I was working on at work this afternoon. I have a list of ~ > 10,000 floating point numbers, which run from largest to smallest. > There are duplicates scattered throughout, so I might have something > like: [5942.6789, 5942.6789, 5941.000003, 5941.01, 5941.01, ... ], > etc. I wanted to search the list for a test value, which, depending on > the particular list (I can have many such lists, each different from > the other.), could conceivably be anywhere within the given list. I > needed to return the index where the list values change from being > just greater than the test value to just less than the test value at > the very next index position. I spent a good chunk of my afternoon > writing a binary search function and wondering what theoretically the > optimum search algorithm would be, got interrupted (as usual on this > project), and decided to look at my books at home to see if a better > solution would be staring at me from some book (Like there usually > is!). > > I haven't studied list comprehensions formally yet, but a snippet of > code in a book caught my eye where the author was discussing filtering > data in a list. This led me to try: > > The generalized problem: > > L = [V0, V1, ..., Vn], where V0 >= V1 >= V2 >= ... >= Vn . > Find index i, such that V[i] >= Vt >= V[i + 1], where Vt is the test > value being searched for. I need to know the indices i and i + 1, > which I need to interpolate based on where Vt falls. > > The solution (As a sublist, S) I worked out tonight after > experimenting with comprehension syntax is: > S = [i for i, V in enumerate(L) if L[i] >= Vt >= L[i + 1]] > > And, of course, the index i I need is: > i = S[0] > > I tested this out with concrete examples in the interpreter, such as > with a list, L: > > L = [item for item in range(10000, 0, -1)] > > and trying different test values. It was blazingly fast, too! > > All I can say is: WOW!!! > > How does the performance of your code compare to the bisect module https://docs.python.org/3/library/bisect.html#module-bisect ? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From __peter__ at web.de Thu Mar 19 10:52:51 2015 From: __peter__ at web.de (Peter Otten) Date: Thu, 19 Mar 2015 10:52:51 +0100 Subject: [Tutor] List comprehensions to search a list--amazing! References: <550A5A46.9020409@davea.name> Message-ID: Dave Angel wrote: > On 03/19/2015 12:20 AM, boB Stepp wrote: >> I hope extolling the beauty and power of Python on this list is >> allowed, because I have had a large "WOW!!!" moment tonight. I had a >> problem I was working on at work this afternoon. I have a list of ~ >> 10,000 floating point numbers, which run from largest to smallest. >> There are duplicates scattered throughout, so I might have something >> like: [5942.6789, 5942.6789, 5941.000003, 5941.01, 5941.01, ... ], >> etc. I wanted to search the list for a test value, which, depending on >> the particular list (I can have many such lists, each different from >> the other.), could conceivably be anywhere within the given list. I >> needed to return the index where the list values change from being >> just greater than the test value to just less than the test value at >> the very next index position. I spent a good chunk of my afternoon >> writing a binary search function and wondering what theoretically the >> optimum search algorithm would be, got interrupted (as usual on this >> project), and decided to look at my books at home to see if a better >> solution would be staring at me from some book (Like there usually >> is!). >> >> I haven't studied list comprehensions formally yet, but a snippet of >> code in a book caught my eye where the author was discussing filtering >> data in a list. This led me to try: >> >> The generalized problem: >> >> L = [V0, V1, ..., Vn], where V0 >= V1 >= V2 >= ... >= Vn . >> Find index i, such that V[i] >= Vt >= V[i + 1], where Vt is the test >> value being searched for. I need to know the indices i and i + 1, >> which I need to interpolate based on where Vt falls. >> >> The solution (As a sublist, S) I worked out tonight after >> experimenting with comprehension syntax is: >> S = [i for i, V in enumerate(L) if L[i] >= Vt >= L[i + 1]] >> >> And, of course, the index i I need is: >> i = S[0] >> >> I tested this out with concrete examples in the interpreter, such as >> with a list, L: >> >> L = [item for item in range(10000, 0, -1)] >> >> and trying different test values. It was blazingly fast, too! >> >> All I can say is: WOW!!! By the way, if you were to use a plain old loop the expected speedup over the listcomp would be 2. You can break out of the loop when you have found the gap, after iterating over one half of the list on average. So for-loops are twice as amazing ;) > That's very innovative. > > The catch to a list comprehension is it has to visit all the elements, > while a binary search would visit log-base-2 of them. So instead of > 10000 elements, you'd be searching about 14 items. > > For large lists, it'd probably be much quicker to use the bisect module. > https://docs.python.org/3.4/library/bisect.html > > > Check out bisect.bisect_left() and bisect.bisect_right() > > I don't see how to directly use those functions on a list which is > reverse-sorted, but the source is available. On my install, it's > located at: > > /usr/lib/python3.4/bisect.py > To back Dave's suggestion with some empirical data here are two ways to make bisect() work with a descending list (though if possible I would recommend that you change your script to use ascending lists). $ cat reverse_bisect2.py import bisect import random def break_listcomp(L, Vt): S = [i for i, V in enumerate(L) if L[i] >= Vt >= L[i + 1]] return S[0] def break_bisect_reverse(L, Vt): L.reverse() result = bisect.bisect(L, Vt) L.reverse() return len(L) - result -1 class Items: def __init__(self, items): self.items = items def __len__(self): return len(self.items) def __getitem__(self, index): return self.items[len(self.items) - 1 - index] def break_bisect_virt(L, Vt): return len(L) - 1 - bisect.bisect(Items(L), Vt) random.seed(42) N = 10**6 data = [random.randrange(N) for i in range(10**5)] data = data + data data.sort(reverse=True) sample = random.sample(data, 10) $ break_bisect_reverse() reverses the list before and after applying bisect. This is still O(N), but the actual work is done in C. break_bisect_virt() wraps the actual list in a class that translates items[0] to items[len(items)-1] items[1] to items[len(items)-2] and so on, thus providing a reversed view of the list without moving any values. This severely slows down access to a single value, but as bisect needs much fewer lookups than the listcomp the overall result is still a massive speedup. The actual timings: $ python3 -m timeit -s 'from reverse_bisect2 import data, sample, break_listcomp as f' '[f(data, v) for v in sample]' 10 loops, best of 3: 781 msec per loop $ python3 -m timeit -s 'from reverse_bisect2 import data, sample, break_bisect_reverse as f' '[f(data, v) for v in sample]' 100 loops, best of 3: 15 msec per loop $ python3 -m timeit -s 'from reverse_bisect2 import data, sample, break_bisect_virt as f' '[f(data, v) for v in sample]' 1000 loops, best of 3: 221 usec per loop So reverse/bisect is 50 times faster than the listcomp, and bisect/virt is 3500 times faster than the listcomp. I expect that a prepackaged linear interpolation function from numpy/scipy can still do better, and also handle the corner cases correctly. To use such a function you may have to reverse order of the values. From pathunstrom at gmail.com Thu Mar 19 18:15:33 2015 From: pathunstrom at gmail.com (Patrick Thunstrom) Date: Thu, 19 Mar 2015 13:15:33 -0400 Subject: [Tutor] List comprehensions to search a list--amazing! In-Reply-To: References: <550A5A46.9020409@davea.name> Message-ID: >>> The generalized problem: >>> >>> L = [V0, V1, ..., Vn], where V0 >= V1 >= V2 >= ... >= Vn . >>> Find index i, such that V[i] >= Vt >= V[i + 1], where Vt is the test >>> value being searched for. I need to know the indices i and i + 1, >>> which I need to interpolate based on where Vt falls. >>> >>> The solution (As a sublist, S) I worked out tonight after >>> experimenting with comprehension syntax is: >>> S = [i for i, V in enumerate(L) if L[i] >= Vt >= L[i + 1]] >>> >>> And, of course, the index i I need is: >>> i = S[0] >>> >>> I tested this out with concrete examples in the interpreter, such as >>> with a list, L: >>> >>> L = [item for item in range(10000, 0, -1)] >>> >>> and trying different test values. It was blazingly fast, too! >>> >>> All I can say is: WOW!!! > > By the way, if you were to use a plain old loop the expected speedup over > the listcomp would be 2. You can break out of the loop when you have found > the gap, after iterating over one half of the list on average. > > So for-loops are twice as amazing ;) For the same basic speed up, a generator expression with a call to next() will produce similar results. Without the additional code to get the indexed item. index = (i for i, _ in enumerate(original_list) if original_list[i] >= target >= original_list[i + 1]).next() The only catch is it raises a StopIteration exception if no element fits the test. P From robertvstepp at gmail.com Thu Mar 19 22:52:03 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Thu, 19 Mar 2015 16:52:03 -0500 Subject: [Tutor] What is the best approach to organizing the order of functions in a module? Message-ID: I'm still working in the procedural paradigm of programming. Hopefully I will find time for OOP soon. But in some of my modules-in-progress,my collection of functions has gotten large enough that I feel I need to bring some sort of order to their positioning. Currently my best thought is to mimic the natural order, in so far as it is possible, in which the functions get called. Are there better ways to organize them? -- boB From breamoreboy at yahoo.co.uk Thu Mar 19 22:58:37 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Thu, 19 Mar 2015 21:58:37 +0000 Subject: [Tutor] What is the best approach to organizing the order of functions in a module? In-Reply-To: References: Message-ID: On 19/03/2015 21:52, boB Stepp wrote: > I'm still working in the procedural paradigm of programming. Hopefully > I will find time for OOP soon. But in some of my > modules-in-progress,my collection of functions has gotten large enough > that I feel I need to bring some sort of order to their positioning. > Currently my best thought is to mimic the natural order, in so far as > it is possible, in which the functions get called. Are there better > ways to organize them? > This is so much down to personal taste that it's impossible to call. The natural order in which functions get called is as good as any, so if it fits your mind set, use it :) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From alan.gauld at btinternet.com Thu Mar 19 23:04:07 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Thu, 19 Mar 2015 22:04:07 +0000 Subject: [Tutor] What is the best approach to organizing the order of functions in a module? In-Reply-To: References: Message-ID: On 19/03/15 21:52, boB Stepp wrote: > that I feel I need to bring some sort of order to their positioning. > Currently my best thought is to mimic the natural order, in so far as > it is possible, in which the functions get called. Are there better > ways to organize them? That sometimes works. But if they are data centric another common approach is to have 1) initialization/shutdown 2) Create 3) Read 4) Update 5) Delete The so-called CRUD API. You can then group functions under those headings So if you have a bunch of functions for creating things they go in section 2. This tends to work very well if using an IDE/Editor that supports code folding. Its also a good migration towards OOP since the data entities tend to become classes andthe CRUD functions become the methods of those classes - at least as a first iteration. Yet another option, especially for Web apps is to adopt the RESTful approach and group functions under those prionciples. Very similar to CRUD just more http oriented than data. http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From ben+python at benfinney.id.au Thu Mar 19 23:09:16 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 20 Mar 2015 09:09:16 +1100 Subject: [Tutor] What is the best approach to organizing the order of functions in a module? References: Message-ID: <85lhisr65v.fsf@benfinney.id.au> boB Stepp writes: > I'm still working in the procedural paradigm of programming. Hopefully > I will find time for OOP soon. Fortunately, with Python, you're almost always doing OOP because all Python values are accessed as objects :-) But I think you mean you're not yet writing classes. > But in some of my modules-in-progress,my collection of functions has > gotten large enough that I feel I need to bring some sort of order to > their positioning. Currently my best thought is to mimic the natural > order, in so far as it is possible, in which the functions get called. > Are there better ways to organize them? You can migrate a step further to writing classes: organise your code by *conceptual proximity*. Functions that are very closely related conceptually get placed close together, and the data structures and constants they use are also placed nearby. Use a page break (the U+000C FORM FEED) white space character on a line by itself, to break up the source code into conceptually-separate areas. Your text editor should have a way of navigating to the previous/next form-feed character, as a way of navigating your file at this level. Deliberately start organising your code so conceptually-related code is closer together, and you'll find it more natural when you begin writing classes. -- \ ?Repetition leads to boredom, boredom to horrifying mistakes, | `\ horrifying mistakes to God-I-wish-I-was-still-bored, and it | _o__) goes downhill from there.? ?Will Larson, 2008-11-04 | Ben Finney From steve at pearwood.info Fri Mar 20 00:08:41 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 20 Mar 2015 10:08:41 +1100 Subject: [Tutor] What is the best approach to organizing the order of functions in a module? In-Reply-To: <85lhisr65v.fsf@benfinney.id.au> References: <85lhisr65v.fsf@benfinney.id.au> Message-ID: <20150319230841.GE31862@ando.pearwood.info> On Fri, Mar 20, 2015 at 09:09:16AM +1100, Ben Finney wrote: > Use a page break (the U+000C FORM FEED) white space character on a line > by itself, to break up the source code into conceptually-separate areas. > Your text editor should have a way of navigating to the previous/next > form-feed character, as a way of navigating your file at this level. I thought I liked that idea, but I've since *almost* decided that giving semantic meaning (section break) to \f characters is an anti-pattern. (1) Not all editors allow you to insert, jump to, and see such page breaks. Not even all programmer's editors. I think it's an Emacs and/or Vim feature which is basically unheard of anywhere else. (2) Page breaks are invisible whitespace. If you have a commit hook that deletes trailing whitespace at the end of lines and normalises line breaks, your page breaks may disappear. (3) Because they are invisible, you need editor support to see them. You may not be able to see them when using other tools (e.g. `cat somefile.py`) or when printed out. (4) Since a bare formfeed is invisible and meaningless, you're going to need some sort of section heading, say: # === Public functions === so the formfeed is in a sense redundent. (5) Pedantic: Formfeeds aren't for section breaks, they're for page breaks. Using them in this way is abuse. -- Steve From alan.gauld at btinternet.com Fri Mar 20 01:36:34 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 20 Mar 2015 00:36:34 +0000 Subject: [Tutor] What is the best approach to organizing the order of functions in a module? In-Reply-To: References: Message-ID: On 19/03/15 21:52, boB Stepp wrote: > modules-in-progress,my collection of functions has gotten large enough > that I feel I need to bring some sort of order to their positioning. One thing that I meant to mention is that the best form of organisation is splitting them into separate files. Trying to maintain one enormous file of "useful functions" is almost always the wrong thing to do. You could create a package to contain the separate files/modules though. So the first decision is how to group the functions such that they make useful (and reusable) modules. That usually is down to dependency management. Things that must be used together go together. (If everything depends on everything else then you have a major design disaster to unpick and that should be even higher priority that reorganising the functions!). -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From niyanaxx95 at gmail.com Fri Mar 20 01:50:57 2015 From: niyanaxx95 at gmail.com (niyanaxx95 at gmail.com) Date: Fri, 20 Mar 2015 00:50:57 +0000 Subject: [Tutor] =?utf-8?q?Reversi_Game_Logic?= Message-ID: <550b6f2f.202a370a.337a.ffffbb99@mx.google.com> I am having trouble with a function in my reversi logic code. The function is the isLegalMove I am asked to "Return a Boolean indicating if the current player can place their chip in the square at position (row, col). Both row and col must be valid indices?." So I came up with my code below, however a move I make in the game says Error: not a legal move. Please help! Code: from ezarrays import Array2D # Values representing the color of the chips on the board. EMPTY = 0 BLACK = 1 WHITE = 2 class ReversiGameLogic : # Creates an instance of Reversi game logic with the board correctly # initialized and the current player set to black. def __init__(self) : # Use a 2-D array to represent the board. self._gameBoard = Array2D(8, 8) self._gameBoard.clear(EMPTY) # Set the initial configuration of the board. self._gameBoard[4,3] = BLACK self._gameBoard[3,4] = BLACK self._gameBoard[3,3] = WHITE self._gameBoard[4,4] = WHITE # Maintain the number of the current player. self._currentPlayer = BLACK # Keep track of the number of each players chips. self._numBlackChips = 2 self._numWhiteChips = 2 # A flag that is set when the game is over. That is, when there are # no empty squares on the board or neither player can make a move. self._gameOver = False # Returns a boolean indicating whether the game is over. def isOver(self) : isOver = 0 for i in range(8) : for j in range(8) : if self._gameBoard[i, j] != 0 : isOver + 1 if isOver == 64 : self._gameOver = True return True else: return False # Returns the player number of the current player. def whoseTurn(self) : if self._currentPlayer == 1: return 1 else: self._curentPlayer == 2 return 2 # Returns the number of chips on the board for the given player. def numChips(self, player) : chipCounter = 0 if player == 1 : for i in range(8) : for j in range(8) : if self._gameBoard[i, j] == BLACK : chipCounter = chipCounter + 1 else : for i in range(8) : for j in range(8) : if self._gameBoard[i, j] == WHITE : chipCounter = chipCounter + 1 return chipCounter # Returns the number of open squares on the board. def numOpenSquares(self) : numOpenSquares = 0 for i in range(8) : for j in range(8) : if self._gameBoard[i, j] == EMPTY : numOpenSquares = numOpenSquares + 1 return numOpenSquares # Returns the player number of the winner or 0 if it's a draw. def getWinner( self ): player1 = 0 player2 = 0 if self._gameOver is True : for i in range(8) : for j in range(8) : if self._gameBoard[i, j] == BLACK : player1 = player1 + 1 else : player2 = player2 + 1 if player1 > player2 : return 1 if player2 > player1 : return 2 else: return 0 # Returns the def isLegalMove( self, row, col): if row < 8 and col < 8: if self._gameBoard[row,col] != EMPTY: return True else: return False # Returns the player number whose chip occupies the given square. def occupiedBy(self, row, col): if self._gameBoard[row, col] == BLACK : return 1 if self._gameBoard[row, col] == WHITE : return 2 else: return 0 # Performs an actual move in the game. That is the current player places # one of his chips in the square at position (row, col). def makeMove( row, col ): if isALineOfAttack(row, col, 1, 1) is True : if self._currentPlayer == 1 : self._gameBoard[row, col] = BLACK else : self._gameBoard[row, col] = WHITE # Helper method that returns a Boolean indicating if there is a line of # attack from cell (row, col) in the direction offset given by rowInc # and colInc. The direction offsets should be, 0, 1, or -1. def _isALineOfAttack(self, row, col, rowInc, colInc) : row += rowInc col += colInc # The next cell in the line must contain the opponents chip. if self.occupiedBy(row, col) == self._currentPlayer : return False # Traverse along the line and determine if it's a line of attack. while row >= 0 and col >= 0 and row < 8 and col < 8 : if self.occupiedBy(row, col) == self._currentPlayer : return True elif self.occupiedBy(row, col) == EMPTY : return False else : row += rowInc col += colInc if row < 0 or row > 7 or col < 0 or col > 7 : return False return False From dyoo at hashcollision.org Fri Mar 20 02:28:54 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 19 Mar 2015 18:28:54 -0700 Subject: [Tutor] Reversi Game Logic In-Reply-To: <550b6f2f.202a370a.337a.ffffbb99@mx.google.com> References: <550b6f2f.202a370a.337a.ffffbb99@mx.google.com> Message-ID: On Thu, Mar 19, 2015 at 5:50 PM, wrote: > I am having trouble with a function in my reversi logic code. The function is the isLegalMove I am asked to "Return a Boolean indicating if the current player can place their chip in the square at position (row, col). Both row and col must be valid indices." So I came up with my code below, however a move I make in the game says Error: not a legal move. Please help! Do you have any tests? From ben+python at benfinney.id.au Fri Mar 20 03:57:05 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 20 Mar 2015 13:57:05 +1100 Subject: [Tutor] Semantic division of source code with U+000C FORM FEED (was: What is the best approach to organizing the order of functions in a module?) References: <85lhisr65v.fsf@benfinney.id.au> <20150319230841.GE31862@ando.pearwood.info> Message-ID: <85bnjoqsu6.fsf_-_@benfinney.id.au> (Since this has moved beyond ?tutor? material, I'm redirecting to the ?python-list? forum; anyone interested, please follow up there.) Steven D'Aprano writes: > On Fri, Mar 20, 2015 at 09:09:16AM +1100, Ben Finney wrote: > > > Use a page break (the U+000C FORM FEED) white space character on a > > line by itself, to break up the source code into > > conceptually-separate areas. Your text editor should have a way of > > navigating to the previous/next form-feed character, as a way of > > navigating your file at this level. > > I thought I liked that idea, but I've since *almost* decided that > giving semantic meaning (section break) to \f characters is an > anti-pattern. To be clear, I'm fine with others choosing not to use U+000C for semantic divisions in text. As far as I'm concerned, you avoiding the practice doesn't harm anyone, and (AFAICT) my using it doesn't harm anyone either. There's room for coexistence. So I am not convinced by your argument that it's an anti-pattern. > (1) Not all editors allow you to insert, jump to, and see such page > breaks. Not even all programmer's editors. I think it's an Emacs > and/or Vim feature which is basically unheard of anywhere else. All editors allow you to insert U+000C. They may not go to any special effort to do so, but I don't rely on an editor for producing characters; that's what the overall OS user interface is for. So this one is just false. Perhaps there are editors which don't display U+000C distinctly; I would say that excludes them from being ?programmer's editor? then. I have yet to see a programmer's editor which won't render a U+000C as visually distinct. So this one is mostly false, and the cases where it isn't aren't relevant IMO for programming. Not all programmer's editors allow navigation by pages at form-feed boundaries, true. My ?should? can be read as normative: if there's a programmer's editor which doesn't have that feature, it should :-) So this one doesn't harm anyone and is irrelevant to an argument for ?anti-pattern?. > (2) Page breaks are invisible whitespace. If you have a commit hook > that deletes trailing whitespace at the end of lines and normalises > line breaks, your page breaks may disappear. Again, I haven't seen a default ?delete trailing whitespace? operation which deletes U+000C, nor U+000A nor U+000D. All three of those are defined as vertical whitespace operations, not ?trailing whitespace?. Conventional ?delete trailing whitespace? includes U+0020 and U+0008 which are horizontal whitespace. If you've made one specially to strip U+000C as well, then that's a perverse case and I don't think that argues against the practice of inserting them for vertical separation. > (5) Pedantic: Formfeeds aren't for section breaks, they're for page > breaks. Using them in this way is abuse. They're for page breaks, sure. I did say as much. I don't see how this argues against the practice. So, I'm fine with you disliking the practice, but I am not convinced anything is harmed beyond anti-patterns themselves (e.g. choosing deliberately to remove U+000C when that's not conventional; choosing to use a non-programmer's editor). I reject the claim of ?anti-pattern?. -- \ ?Software patents provide one more means of controlling access | `\ to information. They are the tool of choice for the internet | _o__) highwayman.? ?Anthony Taylor | Ben Finney From breamoreboy at yahoo.co.uk Fri Mar 20 04:12:30 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 20 Mar 2015 03:12:30 +0000 Subject: [Tutor] Reversi Game Logic In-Reply-To: <550b6f2f.202a370a.337a.ffffbb99@mx.google.com> References: <550b6f2f.202a370a.337a.ffffbb99@mx.google.com> Message-ID: On 20/03/2015 00:50, niyanaxx95 at gmail.com wrote: From just a quick glance seeing it's 3 in the morning here. > I am having trouble with a function in my reversi logic code. The function is the isLegalMove I am asked to "Return a Boolean indicating if the current player can place their chip in the square at position (row, col). Both row and col must be valid indices?." So I came up with my code below, however a move I make in the game says Error: not a legal move. Please help! > > Code: > > from ezarrays import Array2D > > # Values representing the color of the chips on the board. > EMPTY = 0 > BLACK = 1 > WHITE = 2 > > > class ReversiGameLogic : > > # Creates an instance of Reversi game logic with the board correctly > # initialized and the current player set to black. > def __init__(self) : > # Use a 2-D array to represent the board. > self._gameBoard = Array2D(8, 8) > self._gameBoard.clear(EMPTY) > > # Set the initial configuration of the board. > self._gameBoard[4,3] = BLACK > self._gameBoard[3,4] = BLACK > self._gameBoard[3,3] = WHITE > self._gameBoard[4,4] = WHITE > > # Maintain the number of the current player. > self._currentPlayer = BLACK > > # Keep track of the number of each players chips. > self._numBlackChips = 2 > self._numWhiteChips = 2 > > # A flag that is set when the game is over. That is, when there are > # no empty squares on the board or neither player can make a move. > self._gameOver = False > > # Returns a boolean indicating whether the game is over. > def isOver(self) : > isOver = 0 > for i in range(8) : > for j in range(8) : > if self._gameBoard[i, j] != 0 : > isOver + 1 The above line does nothing. > if isOver == 64 : > self._gameOver = True > return True > else: > return False > > # Returns the player number of the current player. > def whoseTurn(self) : > if self._currentPlayer == 1: > return 1 > else: > self._curentPlayer == 2 The above line has two errors. > return 2 > > # Returns the number of chips on the board for the given player. > def numChips(self, player) : > chipCounter = 0 > if player == 1 : > for i in range(8) : > for j in range(8) : > if self._gameBoard[i, j] == BLACK : > chipCounter = chipCounter + 1 > else : > for i in range(8) : > for j in range(8) : > if self._gameBoard[i, j] == WHITE : > chipCounter = chipCounter + 1 > return chipCounter > You can greatly simplify the above function - I'll let you think about it :) > # Returns the number of open squares on the board. > def numOpenSquares(self) : > numOpenSquares = 0 > for i in range(8) : > for j in range(8) : > if self._gameBoard[i, j] == EMPTY : > numOpenSquares = numOpenSquares + 1 > return numOpenSquares > > # Returns the player number of the winner or 0 if it's a draw. > def getWinner( self ): > player1 = 0 > player2 = 0 > if self._gameOver is True : > for i in range(8) : > for j in range(8) : > if self._gameBoard[i, j] == BLACK : > player1 = player1 + 1 > else : > player2 = player2 + 1 > if player1 > player2 : > return 1 > if player2 > player1 : > return 2 > else: > return 0 > > # Returns the > def isLegalMove( self, row, col): > if row < 8 and col < 8: In Python the above line can be written. if row < 8 > col: > if self._gameBoard[row,col] != EMPTY: > return True > else: > return False > > # Returns the player number whose chip occupies the given square. > def occupiedBy(self, row, col): > if self._gameBoard[row, col] == BLACK : > return 1 > if self._gameBoard[row, col] == WHITE : > return 2 > else: > return 0 > > # Performs an actual move in the game. That is the current player places > # one of his chips in the square at position (row, col). > def makeMove( row, col ): > if isALineOfAttack(row, col, 1, 1) is True : > if self._currentPlayer == 1 : > self._gameBoard[row, col] = BLACK > else : > self._gameBoard[row, col] = WHITE > > # Helper method that returns a Boolean indicating if there is a line of > # attack from cell (row, col) in the direction offset given by rowInc > # and colInc. The direction offsets should be, 0, 1, or -1. > def _isALineOfAttack(self, row, col, rowInc, colInc) : > row += rowInc > col += colInc > # The next cell in the line must contain the opponents chip. > if self.occupiedBy(row, col) == self._currentPlayer : > return False > > # Traverse along the line and determine if it's a line of attack. > while row >= 0 and col >= 0 and row < 8 and col < 8 : Again Python comparisons don't need to be written like this. > if self.occupiedBy(row, col) == self._currentPlayer : > return True > elif self.occupiedBy(row, col) == EMPTY : > return False > else : > row += rowInc > col += colInc > if row < 0 or row > 7 or col < 0 or col > 7 : > return False > return False -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From dyoo at hashcollision.org Fri Mar 20 05:50:59 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Thu, 19 Mar 2015 21:50:59 -0700 Subject: [Tutor] Reversi Game Logic In-Reply-To: <550b78bd.5719370a.64f2.ffffc321@mx.google.com> References: <550b6f2f.202a370a.337a.ffffbb99@mx.google.com> <550b784c.82228c0a.798c.ffffc01d@mx.google.com> <550b78bd.5719370a.64f2.ffffc321@mx.google.com> Message-ID: >>>> From: Danny Yoo >>>> Do you have any tests? >>> On Thu, Mar 19, 2015 at 6:29 PM, wrote: >>> Yes the code is to be tested with the main code and the gui. I have >>> attached >>> it in this email. >> From: Danny Yoo >> Sent: ?Thursday?, ?March? ?19?, ?2015 ?9?:?31? ?PM >> To: Ni'Yana Morgan >> >> I can't help you directly. Please send messages to the mailing list. On Thu, Mar 19, 2015 at 6:32 PM, wrote: > What? What are you saying? > I sent my question to the mailing list. Hi Ni'Yana, You sent your *reply* only just to me. This means no one else on the mailing list saw your response. Please try again: make sure you are using Reply To All. You can see that your reply is missing on the mailing list archive, for example. As of this writing, the only messages in the thread that the list has seen are: https://mail.python.org/pipermail/tutor/2015-March/104554.html https://mail.python.org/pipermail/tutor/2015-March/104555.html https://mail.python.org/pipermail/tutor/2015-March/104557.html I tried to tell you this earlier, but I had no time to elaborate. I was literally running to catch an appointment at the exact time that you replied just to me. I did not have time to answer your question. The other issue is that, because this is homework, we do not have free action to tell you everything that you should do to fix your program. I personally restrict myself to either point to areas that you should look at. In this case, the more direct think I can think of that will help is to point you to learning how to test your own programs. My apologies for not being clearer. ------ When I say "tests", I mean something very specific. I do not mean the GUI code that you sent me in your reply. I really mean "unit tests" in the sense that I mentioned earlier, the last time we talked: https://mail.python.org/pipermail/tutor/2015-March/104421.html I am guessing that you do not understand what I'm asking? If so, then you're missing a crucial beginner programming skill. Please ask your instructor: "How do I write unit tests?" At the very least, your instructor might be impressed and will help teach you this skill. Some instructors out there do not realize that unit testing is considered a standard technique for introductory programming. Ask, and maybe that will change. The reason it's so important here is because you're sending us a bunch of code and saying that it's broken.... but as far as I can tell, it will run without any significant runtime errors. It's also very long, too long for the problem to be obvious. What you're really saying is that your program doesn't *behave* the way that you want. And that's precisely what unit tests are meant to capture, to let you express formally what you *want* your code to do. Without unit tests, we can't tell that your program is broken. It looks perfectly fine to me, because I do not know the rules of Othello. Recently, we've had a few people ask about unit testing. See: https://mail.python.org/pipermail/tutor/2015-March/104389.html https://mail.python.org/pipermail/tutor/2015-March/104513.html for examples of basic unit testing. Please feel free to ask questions! From __peter__ at web.de Fri Mar 20 09:21:13 2015 From: __peter__ at web.de (Peter Otten) Date: Fri, 20 Mar 2015 09:21:13 +0100 Subject: [Tutor] List comprehensions to search a list--amazing! References: <550A5A46.9020409@davea.name> Message-ID: Patrick Thunstrom wrote: >>>> The generalized problem: >>>> >>>> L = [V0, V1, ..., Vn], where V0 >= V1 >= V2 >= ... >= Vn . >>>> Find index i, such that V[i] >= Vt >= V[i + 1], where Vt is the test >>>> value being searched for. I need to know the indices i and i + 1, >>>> which I need to interpolate based on where Vt falls. >>>> >>>> The solution (As a sublist, S) I worked out tonight after >>>> experimenting with comprehension syntax is: >>>> S = [i for i, V in enumerate(L) if L[i] >= Vt >= L[i + 1]] >>>> >>>> And, of course, the index i I need is: >>>> i = S[0] >>>> >>>> I tested this out with concrete examples in the interpreter, such as >>>> with a list, L: >>>> >>>> L = [item for item in range(10000, 0, -1)] >>>> >>>> and trying different test values. It was blazingly fast, too! >>>> >>>> All I can say is: WOW!!! >> >> By the way, if you were to use a plain old loop the expected speedup over >> the listcomp would be 2. You can break out of the loop when you have >> found the gap, after iterating over one half of the list on average. >> >> So for-loops are twice as amazing ;) > > For the same basic speed up, a generator expression with a call to > next() will produce similar results. Without the additional code to > get the indexed item. > > index = (i for i, _ in enumerate(original_list) if original_list[i] >= > target >= original_list[i + 1]).next() > > The only catch is it raises a StopIteration exception if no element > fits the test. In new code you shouldn't call the next() method (renamed __next__() in Python 3) explicitly. Use the next() builtin instead which also allows you to provide a default: >>> exhausted = iter(()) >>> next(exhausted) Traceback (most recent call last): File "", line 1, in StopIteration >>> next(exhausted, "default_value") 'default_value' That said, rather than polishing the implementation of the inferior algorithm pick the better one. Ceterum censeo bisect is the way to go here ;) From dbasberg at comcast.net Fri Mar 20 03:57:14 2015 From: dbasberg at comcast.net (Doug Basberg) Date: Thu, 19 Mar 2015 22:57:14 -0400 Subject: [Tutor] UPDATE: Is there a 'hook' to capture all exits from a python program? Message-ID: <00fd01d062b9$924c4c50$b6e4e4f0$@net> I do real-time controls programming and I control a relay in a python program that must be turned off before the program ends. First, the program should never exit unless I do it explicitly. Is there a 'hook' to process before any exit (even if not expected). I must turn off that relay or harm may occur. Thanks, Doug Well, the harm is running down a solar battery bank and shortening the life of the batteries. The relay that runs the inverter fed by the batteries needs to turn off at a particular battery voltage level and within several minutes. So, not time critical and not devastating consequences. Given these circumstances, is there a point I can run some code on any exit? I have decided to use a missing pulse circuit with a 2n3906 and 555 timer, so that missing a pulse train will shut off the inverter. Then if Python stops sending the 'watchdog' pulses, the inverter shuts down. Still, I would like to know if a 'hook' exists on exit from Python. I am running Linux on a Raspberry Pi with Python 2.7.4 I also run an Apache server on the Pi for monitor and control of power, HVAC, and security. Thanks, Doug From alan.gauld at btinternet.com Fri Mar 20 10:02:54 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 20 Mar 2015 09:02:54 +0000 Subject: [Tutor] UPDATE: Is there a 'hook' to capture all exits from a python program? In-Reply-To: <00fd01d062b9$924c4c50$b6e4e4f0$@net> References: <00fd01d062b9$924c4c50$b6e4e4f0$@net> Message-ID: On 20/03/15 02:57, Doug Basberg wrote: > Still, I would like to know if a 'hook' exists on exit from Python. I am > running Linux on a Raspberry Pi with Python 2.7.4 I also run an Apache > server on the Pi for monitor and control of power, HVAC, and security. Your previous mail got you three options. I'd use all of them! > > https://docs.python.org/3/library/atexit.html > > ... But that's only for normal program termination; sys.excepthook is > for unexpected exits def close_relay(e=None,v=None,t=None): try: if not relay_closed() really_close_relay() except: really_close_relay() import sys, atexit atexit.register(close_relay) sys.excepthook = close_relay try: main program here finally: close_relay() -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at btinternet.com Fri Mar 20 10:17:55 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 20 Mar 2015 09:17:55 +0000 Subject: [Tutor] Reversi Game Logic In-Reply-To: References: <550b6f2f.202a370a.337a.ffffbb99@mx.google.com> <550b784c.82228c0a.798c.ffffc01d@mx.google.com> <550b78bd.5719370a.64f2.ffffc321@mx.google.com> Message-ID: On 20/03/15 04:50, Danny Yoo wrote: > Some instructors out there do not realize that unit testing is > considered a standard technique for introductory programming. Ask, > and maybe that will change. Sadly, unit tests are not considered a standard technique for introductory programming. At least not in my part of the world. They should be, but they ain't. Or not at high school level(*). I've yet to meet a teacher who even discusses them in the sense of using unitest type frameworks - they do of course tell the kids to "test their code" but don't show them how! Many schools teach programming because they have to, but have no qualified staff in the subject, so a teacher, often a math or science teacher, gets sent off to "do a course". He (usually he!) returns and teaches what was learned, guided by what is in the published syllabus. If the kids are really lucky they get somebody who taught themselves to code (probably in BASIC/VB) when they were a teenager... That's not how it should be of course, and the whole RaspberyPi movement is largely addressed at trying to remedy that situation. But I have a lot of sympathy for anyone being taught programming in the high schools around here just now (and for the poor, ill-equipped teachers too for that matter!) Disclaimer: Of course other places may have squadrons of properly trained and knowledgeable teachers. In which case, be very happy! (*) I'm not sure about the current state of play in universities. I know I was never taught anything about testing at university but things may well have moved on there. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Fri Mar 20 10:37:54 2015 From: __peter__ at web.de (Peter Otten) Date: Fri, 20 Mar 2015 10:37:54 +0100 Subject: [Tutor] UPDATE: Is there a 'hook' to capture all exits from a python program? References: <00fd01d062b9$924c4c50$b6e4e4f0$@net> Message-ID: Alan Gauld wrote: > On 20/03/15 02:57, Doug Basberg wrote: > >> Still, I would like to know if a 'hook' exists on exit from Python. I am >> running Linux on a Raspberry Pi with Python 2.7.4 I also run an Apache >> server on the Pi for monitor and control of power, HVAC, and security. > > Your previous mail got you three options. I'd use all of them! > > > > https://docs.python.org/3/library/atexit.html > > > > ... But that's only for normal program termination; sys.excepthook is > > for unexpected exits > > > def close_relay(e=None,v=None,t=None): > try: > if not relay_closed() > really_close_relay() > except: > really_close_relay() > > import sys, atexit > atexit.register(close_relay) > sys.excepthook = close_relay > > > try: > main program here > finally: > close_relay() That reeks of cargo cult. Are there actual scenarios for each of the three mechanisms where it is the only one that works? I would expect that try: main program here finally: close_relay() provides the same level of confidence, i. e. the relay will be closed when the program closes normally or the main code raises an exception, but not if the process is killed. From davea at davea.name Fri Mar 20 14:38:42 2015 From: davea at davea.name (Dave Angel) Date: Fri, 20 Mar 2015 09:38:42 -0400 Subject: [Tutor] Reversi Game Logic In-Reply-To: <550b6f2f.202a370a.337a.ffffbb99@mx.google.com> References: <550b6f2f.202a370a.337a.ffffbb99@mx.google.com> Message-ID: <550C22E2.3060704@davea.name> On 03/19/2015 08:50 PM, niyanaxx95 at gmail.com wrote: > I am having trouble with a function in my reversi logic code. The function is the isLegalMove I am asked to "Return a Boolean indicating if the current player can place their chip in the square at position (row, col). Both row and col must be valid indices?." So I came up with my code below, however a move I make in the game says Error: not a legal move. Please help! > > I see lots of things wrong with the code, just by visual inspection. That should tell us that unit tests are necessary. If nothing else, unit tests help you refine just what each method is supposed to do, and under what conditions. I don't recall the rules for Othello, as it's been about 25 years since I've played it, and even then I didn't play much. But there are lots of things about the code that the comments don't describe. For example, it would seem from some of your code that the first player is always BLACK, and the second player is always WHITE. But in other places, you keep them distinct. I see Mark gave you a number of markers into your code for problems that already exist. So I'm going to concentrate only on the method you mention. > > # Returns the Finish the comment > def isLegalMove( self, row, col): > if row < 8 and col < 8: > if self._gameBoard[row,col] != EMPTY: > return True > else: > return False There are 3 exit points from the above function, and only two of them have return statements. Your unit test could detect that by assuring that the return value is always either True or False. The above function sometimes returns None. Have you been taught yet what happens when a function falls off the end without a return statement? -- DaveA From fomcl at yahoo.com Fri Mar 20 16:01:06 2015 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 20 Mar 2015 08:01:06 -0700 Subject: [Tutor] UPDATE: Is there a 'hook' to capture all exits from a python program? Message-ID: <1426863666.76603.BPMail_high_carrier@web163806.mail.gq1.yahoo.com> ---------------------------- On Fri, Mar 20, 2015 10:37 AM CET Peter Otten wrote: >Alan Gauld wrote: > >> On 20/03/15 02:57, Doug Basberg wrote: >> >> Still, I would like to know if a 'hook' exists on exit from Python. I am >> running Linux on a Raspberry Pi with Python 2.7.4 I also run an Apache >> server on the Pi for monitor and control of power, HVAC, and security. >> >> Your previous mail got you three options. I'd use all of them! >> >> > > https://docs.python.org/3/library/atexit.html >> > >> > ... But that's only for normal program termination; sys.excepthook is >> > for unexpected exits >> >> >> def close_relay(e=None,v=None,t=None): >> try: >> if not relay_closed() >> really_close_relay() >> except: >> really_close_relay() >> >> import sys, atexit >> atexit.register(close_relay) >> sys.excepthook = close_relay >> >> >> try: >> main program here >> finally: >> close_relay() > >That reeks of cargo cult. Are there actual scenarios for each of the three >mechanisms where it is the only one that works? > >I would expect that > >try: > main program here >finally: > close_relay() Is this (also) called a diaper pattern? Or is that name reserved for the antipattern with try-bare except, where the 'except' catches all the sh*t (pardon my language)? >provides the same level of confidence, i. e. the relay will be closed when >the program closes normally or the main code raises an exception, but not if >the process is killed. > >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >https://mail.python.org/mailman/listinfo/tutor From __peter__ at web.de Fri Mar 20 16:37:51 2015 From: __peter__ at web.de (Peter Otten) Date: Fri, 20 Mar 2015 16:37:51 +0100 Subject: [Tutor] UPDATE: Is there a 'hook' to capture all exits from a python program? References: <1426863666.76603.BPMail_high_carrier@web163806.mail.gq1.yahoo.com> Message-ID: Albert-Jan Roskam wrote: >>I would expect that >> >>try: >>main program here >>finally: >>close_relay() > > Is this (also) called a diaper pattern? Cool name, but most parents want the baby's faeces to go into the diapers whereas try ... bare except is notorious for swallowing useful information that should be propagated to the user or developer. > Or is that name reserved for the > antipattern with try-bare except, where the 'except' catches all the sh*t > (pardon my language)? For try ... finally it's its raison d'?tre to execute some code no matter what. E. g. before the advent of with ... with open(...) as f: ... # use file # file is closed now even if something went wrong while using it and no # matter how garbage collection works for the Python implementation running # the code. # The exception (if any) is not swallowed it was good style to write f = open(...) try: ... # use file finally: f.close() # file is closed... From dyoo at hashcollision.org Fri Mar 20 19:01:02 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Fri, 20 Mar 2015 11:01:02 -0700 Subject: [Tutor] Reversi Game Logic In-Reply-To: <550b6f2f.202a370a.337a.ffffbb99@mx.google.com> References: <550b6f2f.202a370a.337a.ffffbb99@mx.google.com> Message-ID: Hi Ni'Yana, Here's a little transcript of what I'd do if I were to take a testing approach to the problem. --- Let's say that we start with a fresh board. Imagine that we've just created a fresh ReversiGameLogic. What does the board look like? Let's look at the state in the initializer. ################################# def __init__(self) : # Use a 2-D array to represent the board. self._gameBoard = Array2D(8, 8) self._gameBoard.clear(EMPTY) self._gameBoard[4,3] = BLACK self._gameBoard[3,4] = BLACK self._gameBoard[3,3] = WHITE self._gameBoard[4,4] = WHITE self._currentPlayer = BLACK ... ################################# Let's visualize this. The board looks like this: . . . . . . . . . . . . . . . . . . . . . . . . . . . W B . . . . . . B W . . . . . . . . . . . . . . . . . . . . . . . . . . . and we'll keep in mind that the current player is black. You mention the following: ################################################ > I am having trouble with a function in my reversi logic code. The function is the isLegalMove I am asked to "Return a Boolean indicating if the current player can place their chip in the square at position (row, col). Both row and col must be valid indices." ################################################ What moves are illegal when the board is just starting off? Well, if I try placing on the same points as what's already on the board, that's definitely wrong. Let me try writing this as a unit test. ############################## import unittest class ReversiTests(unittest.TestCase): def testIslegalMoveOnExistingSpots(self): logic = ReversiGameLogic() self.assertFalse(logic.isLegalMove(4, 3)) self.assertFalse(logic.isLegalMove(3, 4)) self.assertFalse(logic.isLegalMove(3, 3)) self.assertFalse(logic.isLegalMove(4, 4)) if __name__ == '__main__': unittest.main() ############################## I'd then run these tests, and they'd all pass, so I'd like that your code is doing *something* good. But then, I'd notice that the tests don't really say too much yet: they've only said that it's illegal to play where there's already a piece. What at the places where black can play when the board looks like this? . . . . . . . . . . . . . . . . . . . . . . . . . . . W B . . . . . . B W . . . . . . . . . . . . . . . . . . . . . . . . . . . According to: http://en.wikipedia.org/wiki/Reversi the legal places where black can play next are here, marked with *'s. . . . . . . . . . . . . . . . . . . . * . . . . . . * W B . . . . . . B W * . . . . . . * . . . . . . . . . . . . . . . . . . . So let's say that in the unit tests. Add assertions that we want those four starred points to be legal moves. ############################################# import unittest class ReversiTests(unittest.TestCase): def testIslegalMoveOnExistingSpots(self): logic = ReversiGameLogic() # Placing on existing spots should not be legal. self.assertFalse(logic.isLegalMove(4, 3)) self.assertFalse(logic.isLegalMove(3, 4)) self.assertFalse(logic.isLegalMove(3, 3)) self.assertFalse(logic.isLegalMove(4, 4)) def testIsLegalMoveGood(self): logic = ReversiGameLogic() # But here are spots that should be legal. self.assertTrue(logic.isLegalMove(2, 3)) ... ## fill me in with the other three legal moves! if __name__ == '__main__': unittest.main() ############################################## If this is the first time you've seen a testing approach, notice that writing tests is a matter of asking yourself: "if this is what the world looks like, what *should* happen if my code were working?" That's an easier thing to do than to actually write the working code. But it's not just something that we ponder: it's something we can run! Once we have the other three legal moves in play, now we have something we can execute. This is something that will say "yes" or "no" to our implementation of isLegalMove(). This is the sort of thing we *need* in order to tell if the logic is working or not. It will also point out very clearly that your isLegalMove() is not working, since it's going to fail. But these are going to tell more than than things are broken: they're going to tell you that things are partially working, which is good! That is, if you have these tests in place, one of the tests will succeed, and the other will fail. And that failure is going to point out where you're missing something in your definition of isLegalMove(). From dyoo at hashcollision.org Fri Mar 20 19:22:22 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Fri, 20 Mar 2015 11:22:22 -0700 Subject: [Tutor] Reversi Game Logic In-Reply-To: References: <550b6f2f.202a370a.337a.ffffbb99@mx.google.com> Message-ID: > > So let's say that in the unit tests. Add assertions that we want > those four starred points to be legal moves. > > ############################################# > import unittest > > class ReversiTests(unittest.TestCase): > def testIslegalMoveOnExistingSpots(self): > logic = ReversiGameLogic() > # Placing on existing spots should not be legal. > self.assertFalse(logic.isLegalMove(4, 3)) > self.assertFalse(logic.isLegalMove(3, 4)) > self.assertFalse(logic.isLegalMove(3, 3)) > self.assertFalse(logic.isLegalMove(4, 4)) > > > def testIsLegalMoveGood(self): > logic = ReversiGameLogic() > # But here are spots that should be legal. > self.assertTrue(logic.isLegalMove(2, 3)) > ... ## fill me in with the other three legal moves! > > > if __name__ == '__main__': > unittest.main() > ############################################## Sorry: I sent this draft a bit too early! There really should be at least one more test that we need to have; we need to check for moves that are not legal, but for fundamental Reversi-based reasons. For example, we *know* that playing on (1, 1) can't be legal when the game is starting: it's an empty spot on the board, but it's not adjacent to a line of white pieces. This is the sort of thing that would be an appropriate to write as a test: ################################################### import unittest class ReversiTests(unittest.TestCase): def testIslegalMoveOnExistingSpots(self): logic = ReversiGameLogic() # Placing on existing spots should not be legal. self.assertFalse(logic.isLegalMove(4, 3)) self.assertFalse(logic.isLegalMove(3, 4)) self.assertFalse(logic.isLegalMove(3, 3)) self.assertFalse(logic.isLegalMove(4, 4)) def testIsLegalMoveGood(self): logic = ReversiGameLogic() # But here are spots that should be legal. self.assertTrue(logic.isLegalMove(2, 3)) ## ... fill me in with the other three legal moves! def testIsLegalMoveNotAdjacentAttackLine(self): logic = ReversiGameLogic() # But here is a spot that should be illegal. self.assertTrue(logic.isLegalMove(1, 1)) if __name__ == '__main__': unittest.main() #################################################### You'll should see that one of the tests is succeeding, which is great! That's what you want to see: partial success. It means that you're going in the right direction, and that you just need to amend what you've got so far. It should also point out two concrete ways in which your program isn't quite working yet. From alan.gauld at btinternet.com Fri Mar 20 20:05:57 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 20 Mar 2015 19:05:57 +0000 Subject: [Tutor] UPDATE: Is there a 'hook' to capture all exits from a python program? In-Reply-To: References: <00fd01d062b9$924c4c50$b6e4e4f0$@net> Message-ID: On 20/03/15 09:37, Peter Otten wrote: >> def close_relay(e=None,v=None,t=None): >> try: >> if not relay_closed() >> really_close_relay() >> except: >> really_close_relay() The purpose of the if clause is to ensure that if the function is called many times you only close the relay once (I surmised that more than once could be harmful?) >> import sys, atexit >> atexit.register(close_relay) >> sys.excepthook = close_relay atexit should be overkill, but it might be needed if for some reason the interpreter dies while performing the usual cleanup. excepthook replaces the usual exception mechanism with a clean up. This is needed for cases where an exception occurs before getting to the finally. We want to close the relay ASAP, not waiting till the interpreter decides to call the finally. >> try: >> main program here >> finally: >> close_relay() This is the happy path where everything shuts down as expected. > That reeks of cargo cult. Are there actual scenarios for each of the three > mechanisms where it is the only one that works? In real-time you never trust anything. Always cover your back. > I would expect that > > try: > main program here > finally: > close_relay() > > provides the same level of confidence, Only if the interpreter is behaving as normal. The hooks are to try (we hope) to catch cases where the interpreter has broken its normal flow. So the scenarios are: 1) an unexpected exception occurs - close the relay ASAP. - Use excepthook 2) The interpreter gets sent a kill or similar unexpected termination - use atexit because finally may not get called. (I'm not sure, so belt n' braces here) (BTW Does anyone know what the interpreter does when suspending - Ctrl-Z in Unix land?) 3) Normal program exit. Use the finally clause. But its only ever going to be a best endeavour, that's why Python is not suitable for true real-time/critical apps... But I'd never trust any environment to its usual behaviour if there is a possibility of something being broken. In this case the relay and its battery pack. > the program closes normally or the main code raises an exception, but not if > the process is killed. What's not clear in the Python documentation is how Python responds to a kill(or suspend). I'd hope the atexit got called even in a kill. I would not expect the finally to be executed. Of course, if its a seg fault you are probably stuffed either way... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From fomcl at yahoo.com Fri Mar 20 21:04:13 2015 From: fomcl at yahoo.com (Albert-Jan Roskam) Date: Fri, 20 Mar 2015 13:04:13 -0700 Subject: [Tutor] UPDATE: Is there a 'hook' to capture all exits from a python program? Message-ID: <1426881853.77899.BPMail_high_carrier@web163801.mail.gq1.yahoo.com> ----------------------------- On Fri, Mar 20, 2015 8:05 PM CET Alan Gauld wrote: >On 20/03/15 09:37, Peter Otten wrote: > >> def close_relay(e=None,v=None,t=None): >> try: >> if not relay_closed() >> really_close_relay() >> except: >> really_close_relay() > >The purpose of the if clause is to ensure that >if the function is called many times you only >close the relay once (I surmised that more than >once could be harmful?) > >> import sys, atexit >> atexit.register(close_relay) >> sys.excepthook = close_relay > >atexit should be overkill, but it might be needed >if for some reason the interpreter dies while >performing the usual cleanup. > >excepthook replaces the usual exception mechanism >with a clean up. This is needed for cases where an >exception occurs before getting to the finally. We >want to close the relay ASAP, not waiting till >the interpreter decides to call the finally. > >> try: >> main program here >> finally: >> close_relay() > >This is the happy path where everything shuts down as expected. > >> That reeks of cargo cult. Are there actual scenarios for each of the three >> mechanisms where it is the only one that works? > >In real-time you never trust anything. >Always cover your back. > >> I would expect that >> >> try: >> main program here >> finally: >> close_relay() >> >> provides the same level of confidence, > >Only if the interpreter is behaving as normal. >The hooks are to try (we hope) to catch cases where >the interpreter has broken its normal flow. > >So the scenarios are: > >1) an unexpected exception occurs - close the relay ASAP. > - Use excepthook > >2) The interpreter gets sent a kill or similar unexpected >termination - use atexit because finally may not get >called. (I'm not sure, so belt n' braces here) >(BTW Does anyone know what the interpreter does when >suspending - Ctrl-Z in Unix land?) No experience with it, but I would first check the 'signal' module import signal import sys def signal_term_handler(signal, frame): print 'got SIGTERM' sys.exit(0) signal.signal(signal.SIGTERM, signal_term_handler) https://nattster.wordpress.com/2013/06/05/catch-kill-signal-in-python/ > >3) Normal program exit. Use the finally clause. > >But its only ever going to be a best endeavour, that's >why Python is not suitable for true real-time/critical apps... >But I'd never trust any environment to its usual behaviour >if there is a possibility of something being broken. >In this case the relay and its battery pack. > >> the program closes normally or the main code raises an exception, but not if >> the process is killed. > >What's not clear in the Python documentation is how Python responds >to a kill(or suspend). I'd hope the atexit got called even in a kill. >I would not expect the finally to be executed. > >Of course, if its a seg fault you are probably stuffed either way... > > >-- >Alan G >Author of the Learn to Program web site >http://www.alan-g.me.uk/ >http://www.amazon.com/author/alan_gauld >Follow my photo-blog on Flickr at: >http://www.flickr.com/photos/alangauldphotos > > >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >https://mail.python.org/mailman/listinfo/tutor From kale at kalegood.com Fri Mar 20 13:22:03 2015 From: kale at kalegood.com (Kale Good) Date: Fri, 20 Mar 2015 08:22:03 -0400 Subject: [Tutor] Fastest find in 2 2D lists with else statement Message-ID: <550C10EB.4090804@kalegood.com> Hello all, I'm new to python and a bit of a weekend-warrior programmer (guitar teacher by trade), so there are plenty of computer sciencey concepts that I haven't grasped yet, and I think I'm bumping up against those now. Read from separate csv files, I have something like a=[['bass',9],['eagle',36],['human',68]] b=[['bass','fish'],['eagle','bird'],['dog',land']] c=[[1,'fish','water'],[2, 'mammal','land'],[3,'bird','air']] What I want is to return where each animal lives. What I've been able to glisten from the manuals and stack overflow is: for i in range(len(a)): for x in range(len(b)): if a in b[x][1]: clss = (b[x][0]) occurrence w/ else statement for y in range(len(c)): if clss in c[y][1]: place = (c[y][2]) a[i].append(place) a)I'd like to have an else statement; if a match isn't found in b, prompt the user for one of the values in c[:][0]. (no need to print the list c; the user will have that available elsewhere) b)Is there a faster way to do this? List a has ~300 elements, while list b has ~45000, so I'm curious if I can make it faster. -I only need to find the first match, so it seems a next statement would do the trick. However, I can't figure out how to use next for finding in two 2d arrays. -From my understanding, for this use, b needs to be a list. However, each line is unique, so it could be a set if necessary. Thanks in advance. Best, Kale -- ------------------------------------------------------------------------ Kale Good: Guitar Instructor ? phillyguitarlessons.com phone: (215)260-5383 * 4705 Baltimore Ave, Phila, PA 19143 :Mailing & Lessons * 1867 Frankford Ave. Phila, PA 19125 :Lessons Google+ Facebook Read my article "The Seven Secrets to Six String Success " at GuitarNoise.com Leading the Journey from No-Skills-Guitarist to Talented Musician! From niyanaxx95 at gmail.com Fri Mar 20 18:28:41 2015 From: niyanaxx95 at gmail.com (niyanaxx95 at gmail.com) Date: Fri, 20 Mar 2015 17:28:41 +0000 Subject: [Tutor] =?utf-8?q?Reversi_Game_Logic?= In-Reply-To: <550c39fd.8413370a.0fc0.1f96@mx.google.com> References: <550c36fe.4c1a370a.313d.1ea1@mx.google.com>, <229966562.1969393.1426864312079.JavaMail.yahoo@mail.yahoo.com>, <550c39fd.8413370a.0fc0.1f96@mx.google.com> Message-ID: <550c5900.a91f370a.2279.3690@mx.google.com> I am very new to Comp Science and am still learning. I have attached the programs needed for testing to show that I am testing my code as well as the instructions provided for my project. Please help me out! Sent from Windows Mail From: Ni'Yana Morgan Sent: ?Friday?, ?March? ?20?, ?2015 ?11?:?16? ?AM To: Mark Lawrence I am very new to Comp Science and am still learning. I have attached the programs needed for testing to show that I am testing my code as well as the instructions provided for my project. Please help me out! Sent from Windows Mail From: Mark Lawrence Sent: ?Friday?, ?March? ?20?, ?2015 ?11?:?11? ?AM To: Ni'Yana Morgan please use reply all so everybody can benefit Kindest regards. Mark Lawrence. On Friday, 20 March 2015, 15:04, "niyanaxx95 at gmail.com" wrote: Thank you Mark for replying. I fixed the note you provided on the isLegalMove. However the lineOfAttack function is a function my Professor did so students are not allowed to touch it. For the isOver function, are you able to guide me on that? I am very new to Comp Science and am still learning. I have attached the programs needed for testing to show that I am testing my code as well as the instructions provided for my project. Please help me out! from ezarrays import Array2D # Values representing the color of the chips on the board. EMPTY = 0 BLACK = 1 WHITE = 2 class ReversiGameLogic : # Creates an instance of Reversi game logic with the board correctly # initialized and the current player set to black. def __init__(self) : # Use a 2-D array to represent the board. self._gameBoard = Array2D(8, 8) self._gameBoard.clear(EMPTY) # Set the initial configuration of the board. self._gameBoard[4,3] = BLACK self._gameBoard[3,4] = BLACK self._gameBoard[3,3] = WHITE self._gameBoard[4,4] = WHITE # Maintain the number of the current player. self._currentPlayer = BLACK # Keep track of the number of each players chips. self._numBlackChips = 2 self._numWhiteChips = 2 # A flag that is set when the game is over. That is, when there are # no empty squares on the board or neither player can make a move. self._gameOver = False # Returns a boolean indicating whether the game is over. def isOver(self) : isOver = 0 for i in range(8) : for j in range(8) : if self._gameBoard[i, j] != 0 : isOver + 1 if isOver == 64 : self._gameOver = True return True else: return False # Returns the player number of the current player. def whoseTurn(self) : if self._currentPlayer == 1: return 1 else: self._curentPlayer == 2 return 2 # Returns the number of chips on the board for the given player. def numChips(self, player) : chipCounter = 0 if player == 1 : for i in range(8) : for j in range(8) : if self._gameBoard[i, j] == BLACK : chipCounter = chipCounter + 1 else : for i in range(8) : for j in range(8) : if self._gameBoard[i, j] == WHITE : chipCounter = chipCounter + 1 return chipCounter # Returns the number of open squares on the board. def numOpenSquares(self) : numOpenSquares = 0 for i in range(8) : for j in range(8) : if self._gameBoard[i, j] == EMPTY : numOpenSquares = numOpenSquares + 1 return numOpenSquares # Returns the player number of the winner or 0 if it's a draw. def getWinner( self ): player1 = 0 player2 = 0 if self._gameOver is True : for i in range(8) : for j in range(8) : if self._gameBoard[i, j] == BLACK : player1 = player1 + 1 else : player2 = player2 + 1 if player1 > player2 : return 1 if player2 > player1 : return 2 else: return 0 # Returns the def isLegalMove( self, row, col): if row < 8 > col: if self._gameBoard[row,col] != EMPTY: return True else: return False # Returns the player number whose chip occupies the given square. def occupiedBy(self, row, col): if self._gameBoard[row, col] == BLACK : return 1 if self._gameBoard[row, col] == WHITE : return 2 else: return 0 # Performs an actual move in the game. That is the current player places # one of his chips in the square at position (row, col). def makeMove( row, col ): if isALineOfAttack(row, col, 1, 1) is True : if self._currentPlayer == 1 : self._gameBoard[row, col] = BLACK else : self._gameBoard[row, col] = WHITE # Helper method that returns a Boolean indicating if there is a line of # attack from cell (row, col) in the direction offset given by rowInc # and colInc. The direction offsets should be, 0, 1, or -1. def _isALineOfAttack(self, row, col, rowInc, colInc) : row += rowInc col += colInc # The next cell in the line must contain the opponents chip. if self.occupiedBy(row, col) == self._currentPlayer : return False # Traverse along the line and determine if it's a line of attack. while row >= 0 and col >= 0 and row < 8 and col < 8 : if self.occupiedBy(row, col) == self._currentPlayer : return True elif self.occupiedBy(row, col) == EMPTY : return False else : row += rowInc col += colInc if row < 0 or row > 7 or col < 0 or col > 7 : return False return False Sent from Windows Mail From: Mark Lawrence Sent: ?Thursday?, ?March? ?19?, ?2015 ?11?:?12? ?PM To: tutor at python.org On 20/03/2015 00:50, niyanaxx95 at gmail.com wrote: From just a quick glance seeing it's 3 in the morning here. > I am having trouble with a function in my reversi logic code. The function is the isLegalMove I am asked to "Return a Boolean indicating if the current player can place their chip in the square at position (row, col). Both row and col must be valid indices?." So I came up with my code below, however a move I make in the game says Error: not a legal move. Please help! > > Code: > > from ezarrays import Array2D > > # Values representing the color of the chips on the board. > EMPTY = 0 > BLACK = 1 > WHITE = 2 > > > class ReversiGameLogic : > > # Creates an instance of Reversi game logic with the board correctly > # initialized and the current player set to black. > def __init__(self) : > # Use a 2-D array to represent the board. > self._gameBoard = Array2D(8, 8) > self._gameBoard.clear(EMPTY) > > # Set the initial configuration of the board. > self._gameBoard[4,3] = BLACK > self._gameBoard[3,4] = BLACK > self._gameBoard[3,3] = WHITE > self._gameBoard[4,4] = WHITE > > # Maintain the number of the current player. > self._currentPlayer = BLACK > > # Keep track of the number of each players chips. > self._numBlackChips = 2 > self._numWhiteChips = 2 > > # A flag that is set when the game is over. That is, when there are > # no empty squares on the board or neither player can make a move. > self._gameOver = False > > # Returns a boolean indicating whether the game is over. > def isOver(self) : > isOver = 0 > for i in range(8) : > for j in range(8) : > if self._gameBoard[i, j] != 0 : > isOver + 1 The above line does nothing. > if isOver == 64 : > self._gameOver = True > return True > else: > return False > > # Returns the player number of the current player. > def whoseTurn(self) : > if self._currentPlayer == 1: > return 1 > else: > self._curentPlayer == 2 The above line has two errors. > return 2 > > # Returns the number of chips on the board for the given player. > def numChips(self, player) : > chipCounter = 0 > if player == 1 : > for i in range(8) : > for j in range(8) : > if self._gameBoard[i, j] == BLACK : > chipCounter = chipCounter + 1 > else : > for i in range(8) : > for j in range(8) : > if self._gameBoard[i, j] == WHITE : > chipCounter = chipCounter + 1 > return chipCounter > You can greatly simplify the above function - I'll let you think about it :) > # Returns the number of open squares on the board. > def numOpenSquares(self) : > numOpenSquares = 0 > for i in range(8) : > for j in range(8) : > if self._gameBoard[i, j] == EMPTY : > numOpenSquares = numOpenSquares + 1 > return numOpenSquares > > # Returns the player number of the winner or 0 if it's a draw. > def getWinner( self ): > player1 = 0 > player2 = 0 > if self._gameOver is True : > for i in range(8) : > for j in range(8) : > if self._gameBoard[i, j] == BLACK : > player1 = player1 + 1 > else : > player2 = player2 + 1 > if player1 > player2 : > return 1 > if player2 > player1 : > return 2 > else: > return 0 > > # Returns the > def isLegalMove( self, row, col): > if row < 8 and col < 8: In Python the above line can be written. if row < 8 > col: > if self._gameBoard[row,col] != EMPTY: > return True > else: > return False > > # Returns the player number whose chip occupies the given square. > def occupiedBy(self, row, col): > if self._gameBoard[row, col] == BLACK : > return 1 > if self._gameBoard[row, col] == WHITE : > return 2 > else: > return 0 > > # Performs an actual move in the game. That is the current player places > # one of his chips in the square at position (row, col). > def makeMove( row, col ): > if isALineOfAttack(row, col, 1, 1) is True : > if self._currentPlayer == 1 : > self._gameBoard[row, col] = BLACK > else : > self._gameBoard[row, col] = WHITE > > # Helper method that returns a Boolean indicating if there is a line of > # attack from cell (row, col) in the direction offset given by rowInc > # and colInc. The direction offsets should be, 0, 1, or -1. > def _isALineOfAttack(self, row, col, rowInc, colInc) : > row += rowInc > col += colInc > # The next cell in the line must contain the opponents chip. > if self.occupiedBy(row, col) == self._currentPlayer : > return False > > # Traverse along the line and determine if it's a line of attack. > while row >= 0 and col >= 0 and row < 8 and col < 8 : Again Python comparisons don't need to be written like this. > if self.occupiedBy(row, col) == self._currentPlayer : > return True > elif self.occupiedBy(row, col) == EMPTY : > return False > else : > row += rowInc > col += colInc > if row < 0 or row > 7 or col < 0 or col > 7 : > return False > return False -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: reversilogic.py URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: reversigui.py URL: -------------- next part -------------- An embedded and charset-unspecified text was scrubbed... Name: reversi.py URL: From alan.gauld at btinternet.com Fri Mar 20 21:35:34 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Fri, 20 Mar 2015 20:35:34 +0000 Subject: [Tutor] UPDATE: Is there a 'hook' to capture all exits from a python program? In-Reply-To: <1426881853.77899.BPMail_high_carrier@web163801.mail.gq1.yahoo.com> References: <1426881853.77899.BPMail_high_carrier@web163801.mail.gq1.yahoo.com> Message-ID: On 20/03/15 20:04, Albert-Jan Roskam wrote: >> (BTW Does anyone know what the interpreter does when >> suspending - Ctrl-Z in Unix land?) > > No experience with it, but I would first check the 'signal' module Yeah, I know you can catch a signal and add your own handler, but I meant what is the default Python suspend behaviour? Does it execute any outstanding exception blocks? What about finally blocks? Or, if about to exit a context manager, the __exit__ method? Or does it just stop and wait till its resumed? Kind of like an implicit yield statement? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From breamoreboy at yahoo.co.uk Fri Mar 20 22:05:30 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Fri, 20 Mar 2015 21:05:30 +0000 Subject: [Tutor] Fastest find in 2 2D lists with else statement In-Reply-To: <550C10EB.4090804@kalegood.com> References: <550C10EB.4090804@kalegood.com> Message-ID: On 20/03/2015 12:22, Kale Good wrote: > Hello all, > I'm new to python and a bit of a weekend-warrior programmer (guitar > teacher by trade), so there are plenty of computer sciencey concepts > that I haven't grasped yet, and I think I'm bumping up against those now. Welcome to the club :) > > Read from separate csv files, I have something like > > a=[['bass',9],['eagle',36],['human',68]] > b=[['bass','fish'],['eagle','bird'],['dog',land']] > c=[[1,'fish','water'],[2, 'mammal','land'],[3,'bird','air']] > > What I want is to return where each animal lives. What I've been able to > glisten from the manuals and stack overflow is: > > for i in range(len(a)): > > for x in range(len(b)): > if a in b[x][1]: > clss = (b[x][0]) > occurrence w/ else statement > for y in range(len(c)): > if clss in c[y][1]: > place = (c[y][2]) > > a[i].append(place) > Classic newbie stuff that immediately flags up a code smell. You do not need to loop around Python containers using indexes. This is typically written something like. for animal in animals: doSomething(animal) However see my comment below, you probably don't need so many loops if you hold your data in dicts. > a)I'd like to have an else statement; if a match isn't found in b, > prompt the user for one of the values in c[:][0]. (no need to print the > list c; the user will have that available elsewhere) > > b)Is there a faster way to do this? List a has ~300 elements, while list > b has ~45000, so I'm curious if I can make it faster. Take a look at the DictReader here https://docs.python.org/3/library/csv.html as when it comes to looking things up dicts are far faster than lists. Try coding it up and come back to us if you get any problems. We don't bite although I have been known to bark :) > -I only need to find the first match, so it seems a next statement > would do the trick. However, I can't figure out how to use next for > finding in two 2d arrays. > -From my understanding, for this use, b needs to be a list. However, > each line is unique, so it could be a set if necessary. > > Thanks in advance. > > Best, > Kale -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From davea at davea.name Sat Mar 21 00:45:48 2015 From: davea at davea.name (Dave Angel) Date: Fri, 20 Mar 2015 19:45:48 -0400 Subject: [Tutor] Reversi Game Logic In-Reply-To: <550c5900.a91f370a.2279.3690@mx.google.com> References: <550c36fe.4c1a370a.313d.1ea1@mx.google.com>, <229966562.1969393.1426864312079.JavaMail.yahoo@mail.yahoo.com>, <550c39fd.8413370a.0fc0.1f96@mx.google.com> <550c5900.a91f370a.2279.3690@mx.google.com> Message-ID: <550CB12C.4060502@davea.name> On 03/20/2015 01:28 PM, niyanaxx95 at gmail.com wrote: > You have more than one copy of some lines of previous messages, and more than one version of code in the message. So I have to guess which one you intend to be current. > > Thank you Mark for replying. I fixed the note you provided on the isLegalMove. However the lineOfAttack function is a function my Professor did so students are not allowed to touch it. > > > > > For the isOver function, are you able to guide me on that? > > I am very new to Comp Science and am still learning. > > I have attached the programs needed for testing to show that I am testing my code as well as the instructions provided for my project. > > Please help me out! > > > > > # Returns a boolean indicating whether the game is over. > def isOver(self) : > isOver = 0 > for i in range(8) : > for j in range(8) : > if self._gameBoard[i, j] != 0 : > isOver + 1 The above line does NOT change isOver variable. Try again. By the way, it's not usually a good idea to use the function name as a local within the function, even though it'll work. > if isOver == 64 : > self._gameOver = True > return True > else: > return False > > > # Returns the > def isLegalMove( self, row, col): > if row < 8 > col: > if self._gameBoard[row,col] != EMPTY: > return True > else: > return False This function is still buggy. It does not return either True or False if the selected row is non-empty. > > > # Returns the player number whose chip occupies the given square. > def occupiedBy(self, row, col): How is the following function body any different from: return self._gameBoard[row, col] > if self._gameBoard[row, col] == BLACK : > return 1 > if self._gameBoard[row, col] == WHITE : > return 2 > else: > return 0 > > > > > # Performs an actual move in the game. That is the current player places > # one of his chips in the square at position (row, col). > def makeMove( row, col ): > if isALineOfAttack(row, col, 1, 1) is True : How are the four following lines any different from: self._gameBoard[row, col] = self._currentPlayer > if self._currentPlayer == 1 : > self._gameBoard[row, col] = BLACK > else : > self._gameBoard[row, col] = WHITE > > > -- DaveA From niyanaxx95 at gmail.com Fri Mar 20 23:20:53 2015 From: niyanaxx95 at gmail.com (niyanaxx95 at gmail.com) Date: Fri, 20 Mar 2015 22:20:53 +0000 Subject: [Tutor] =?utf-8?q?Reversi_Game_Logic?= In-Reply-To: References: <550b6f2f.202a370a.337a.ffffbb99@mx.google.com> , Message-ID: <550c9d4f.0d18370a.4cbb.6988@mx.google.com> Thank you Danny Yoo for replying. I figured out what to do for the isLegalMove but I ran into another problem. I now get a traceback error every chip is black. This is the traceback: Traceback (most recent call last): File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__ return self.func(*args) File "u:\code\reversiguiapp.py", line 83, in _cbMouseClick TypeError: makeMove() takes 2 positional arguments but 3 were given Exception in Tkinter callback Here is my code again just in case: from ezarrays import Array2D # Values representing the color of the chips on the board. EMPTY = 0 BLACK = 1 WHITE = 2 class ReversiGameLogic : # Creates an instance of Reversi game logic with the board correctly # initialized and the current player set to black. def __init__(self) : # Use a 2-D array to represent the board. self._gameBoard = Array2D(8, 8) self._gameBoard.clear(EMPTY) # Set the initial configuration of the board. self._gameBoard[4,3] = BLACK self._gameBoard[3,4] = BLACK self._gameBoard[3,3] = WHITE self._gameBoard[4,4] = WHITE # Maintain the number of the current player. self._currentPlayer = BLACK # Keep track of the number of each players chips. self._numBlackChips = 2 self._numWhiteChips = 2 # A flag that is set when the game is over. That is, when there are # no empty squares on the board or neither player can make a move. self._gameOver = False # Returns a boolean indicating whether the game is over. def isOver(self) : isOver = 0 for i in range(8) : for j in range(8) : if self._gameBoard[i, j] != 0 : isOver = isOver + 1 if isOver == 64 : self._gameOver = True return True else: return False # Returns the player number of the current player. def whoseTurn(self) : if self._currentPlayer == 1: return 1 else: self._curentPlayer == 2 return 2 # Returns the number of chips on the board for the given player. def numChips(self, player) : chipCounter = 0 if player == 1 : for i in range(8) : for j in range(8) : if self._gameBoard[i, j] == BLACK : chipCounter = chipCounter + 1 else : for i in range(8) : for j in range(8) : if self._gameBoard[i, j] == WHITE : chipCounter = chipCounter + 1 return chipCounter # Returns the number of open squares on the board. def numOpenSquares(self) : numOpenSquares = 0 for i in range(8) : for j in range(8) : if self._gameBoard[i, j] == EMPTY : numOpenSquares = numOpenSquares + 1 return numOpenSquares # Returns the player number of the winner or 0 if it's a draw. def getWinner( self ): player1 = 0 player2 = 0 if self._gameOver is True : for i in range(8) : for j in range(8) : if self._gameBoard[i, j] == BLACK : player1 = player1 + 1 else : player2 = player2 + 1 if player1 > player2 : return 1 if player2 > player1 : return 2 else: return 0 # Returns the Return a Boolean indicating if the current player can place # their chip in the square at position (row, col). Both row and col must be # valid indices def isLegalMove( self, row, col ): if row < 0 or row >= 8 or col < 0 or col >= 8: return False if self._gameBoard[row,col] == EMPTY: return True else: return False # Returns the player number whose chip occupies the given square. def occupiedBy(self, row, col): if self._gameBoard[row, col] == BLACK : return 1 if self._gameBoard[row, col] == WHITE : return 2 else: return 0 # Performs an actual move in the game. That is the current player places # one of his chips in the square at position (row, col). def makeMove( row, col ): if isALineOfAttack(row, col, 1, 1) is True : if self._currentPlayer == 1 : self._gameBoard[row, col] = BLACK else : self._gameBoard[row, col] = WHITE # Helper method that returns a Boolean indicating if there is a line of # attack from cell (row, col) in the direction offset given by rowInc # and colInc. The direction offsets should be, 0, 1, or -1. def _isALineOfAttack(self, row, col, rowInc, colInc) : row += rowInc col += colInc # The next cell in the line must contain the opponents chip. if self.occupiedBy(row, col) == self._currentPlayer : return False # Traverse along the line and determine if it's a line of attack. while row >= 0 and col >= 0 and row < 8 and col < 8 : if self.occupiedBy(row, col) == self._currentPlayer : return True elif self.occupiedBy(row, col) == EMPTY : return False else : row += rowInc col += colInc if row < 0 or row > 7 or col < 0 or col > 7 : return False return False Sent from Windows Mail From: Danny Yoo Sent: ?Friday?, ?March? ?20?, ?2015 ?2?:?22? ?PM To: Ni'Yana Morgan Cc: tutor at python.org > > So let's say that in the unit tests. Add assertions that we want > those four starred points to be legal moves. > > ############################################# > import unittest > > class ReversiTests(unittest.TestCase): > def testIslegalMoveOnExistingSpots(self): > logic = ReversiGameLogic() > # Placing on existing spots should not be legal. > self.assertFalse(logic.isLegalMove(4, 3)) > self.assertFalse(logic.isLegalMove(3, 4)) > self.assertFalse(logic.isLegalMove(3, 3)) > self.assertFalse(logic.isLegalMove(4, 4)) > > > def testIsLegalMoveGood(self): > logic = ReversiGameLogic() > # But here are spots that should be legal. > self.assertTrue(logic.isLegalMove(2, 3)) > ... ## fill me in with the other three legal moves! > > > if __name__ == '__main__': > unittest.main() > ############################################## Sorry: I sent this draft a bit too early! There really should be at least one more test that we need to have; we need to check for moves that are not legal, but for fundamental Reversi-based reasons. For example, we *know* that playing on (1, 1) can't be legal when the game is starting: it's an empty spot on the board, but it's not adjacent to a line of white pieces. This is the sort of thing that would be an appropriate to write as a test: ################################################### import unittest class ReversiTests(unittest.TestCase): def testIslegalMoveOnExistingSpots(self): logic = ReversiGameLogic() # Placing on existing spots should not be legal. self.assertFalse(logic.isLegalMove(4, 3)) self.assertFalse(logic.isLegalMove(3, 4)) self.assertFalse(logic.isLegalMove(3, 3)) self.assertFalse(logic.isLegalMove(4, 4)) def testIsLegalMoveGood(self): logic = ReversiGameLogic() # But here are spots that should be legal. self.assertTrue(logic.isLegalMove(2, 3)) ## ... fill me in with the other three legal moves! def testIsLegalMoveNotAdjacentAttackLine(self): logic = ReversiGameLogic() # But here is a spot that should be illegal. self.assertTrue(logic.isLegalMove(1, 1)) if __name__ == '__main__': unittest.main() #################################################### You'll should see that one of the tests is succeeding, which is great! That's what you want to see: partial success. It means that you're going in the right direction, and that you just need to amend what you've got so far. It should also point out two concrete ways in which your program isn't quite working yet. From steve at pearwood.info Sat Mar 21 02:03:17 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 21 Mar 2015 12:03:17 +1100 Subject: [Tutor] UPDATE: Is there a 'hook' to capture all exits from a python program? In-Reply-To: References: <1426881853.77899.BPMail_high_carrier@web163801.mail.gq1.yahoo.com> Message-ID: <20150321010317.GG31862@ando.pearwood.info> On Fri, Mar 20, 2015 at 08:35:34PM +0000, Alan Gauld wrote: > Yeah, I know you can catch a signal and add your own handler, but I > meant what is the default Python suspend behaviour? Does it execute any > outstanding exception blocks? What about finally blocks? Or, if about to > exit a context manager, the __exit__ method? Depends on the signal. I'm not an expert on how signals work in Linux, or other Unixes, but I expect that, in the absense of a specific signal handler to catch it, the "sleep" (pause?) signal will cause the interpreter to just stop and wait. I think that's signal 19 on Linux, and 18 to wake. Signal 9 doesn't give the interpreter to do anything. The OS just yanks the carpet out from under its feet and terminates the process with extreme prejudice. Signal 9 cannot be caught, no signal handlers will detect it, no try...finally blocks will run. The process just stops. Don't use kill -9 unless you need to. I always try three steps to kill a rogue process: First use "kill " with no other arguments. Give it 30 seconds or so to let the process tidy up after itself, and if it still hasn't quiet, try "kill -HUP ". Again, give it 30 seconds or so. Then, if and only if necessary, "kill -9 ". > Or does it just stop and wait till its resumed? Kind of like > an implicit yield statement? > > > -- > Alan G > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > http://www.amazon.com/author/alan_gauld > Follow my photo-blog on Flickr at: > http://www.flickr.com/photos/alangauldphotos > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From davea at davea.name Sat Mar 21 02:23:56 2015 From: davea at davea.name (Dave Angel) Date: Fri, 20 Mar 2015 21:23:56 -0400 Subject: [Tutor] Reversi Game Logic In-Reply-To: <550c9d4f.0d18370a.4cbb.6988@mx.google.com> References: <550b6f2f.202a370a.337a.ffffbb99@mx.google.com> , <550c9d4f.0d18370a.4cbb.6988@mx.google.com> Message-ID: <550CC82C.6050507@davea.name> On 03/20/2015 06:20 PM, niyanaxx95 at gmail.com wrote: > > Thank you Danny Yoo for replying. > > I figured out what to do for the isLegalMove but I ran into another problem. I now get a traceback error every chip is black. > > This is the traceback: > > Traceback (most recent call last): > File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__ > return self.func(*args) > File "u:\code\reversiguiapp.py", line 83, in _cbMouseClick > TypeError: makeMove() takes 2 positional arguments but 3 were given > Exception in Tkinter callback > > > > > > > > # Performs an actual move in the game. That is the current player places > # one of his chips in the square at position (row, col). > def makeMove( row, col ): Don't you need a 'self' parameter to this method, like all the others? > if isALineOfAttack(row, col, 1, 1) is True : > if self._currentPlayer == 1 : > self._gameBoard[row, col] = BLACK > else : > self._gameBoard[row, col] = WHITE > > -- DaveA From steve at pearwood.info Sat Mar 21 03:06:27 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 21 Mar 2015 13:06:27 +1100 Subject: [Tutor] Fastest find in 2 2D lists with else statement In-Reply-To: <550C10EB.4090804@kalegood.com> References: <550C10EB.4090804@kalegood.com> Message-ID: <20150321020626.GH31862@ando.pearwood.info> On Fri, Mar 20, 2015 at 08:22:03AM -0400, Kale Good wrote: > Hello all, > I'm new to python and a bit of a weekend-warrior programmer (guitar > teacher by trade), so there are plenty of computer sciencey concepts > that I haven't grasped yet, and I think I'm bumping up against those now. > > Read from separate csv files, I have something like > > a=[['bass',9],['eagle',36],['human',68]] > b=[['bass','fish'],['eagle','bird'],['dog',land']] > c=[[1,'fish','water'],[2, 'mammal','land'],[3,'bird','air']] What do the numbers 9, 36, 68 in "a" list mean? If I'm reading "b" correctly, you say a bass is a fish, an eagle is a bird, and a dog is a land. What do the numbers 1, 2, 3 in "c" list mean? > What I want is to return where each animal lives. What I've been able to > glisten from the manuals and stack overflow is: Let's start by writing in English how you would do this. My guess is: For each animal: find out what kind of animal it is; then look up where that kind of animal lives. This tells you that the most natural sort of information you want is a list of animals: # [...] is used for lists animals = ["bass", "eagle", "human", "spider", "salmon"] plus a dict that associates each animal with its kind: # {...} is used for dicts kinds = {"bass": "fish", "eagle": "bird", "human": "mammal", "spider": "arachnid", "salmon": "fish", } plus a second dict that maps each kind to a location: locations = {"fish": "water", "bird": "air", "mammal": "land", "insect": "land", "arachnid": "land", "mollusk": "water", } Then your code becomes trivially easy! for animal in animals: kind = kinds[animal] print(animal, "lives in or on", locations[kind]) The dict lookup kinds[animal] takes care of all the searching for you, no need to write a for loop at all. To handle the case where the animal or kind is unknown: for animal in animals: kind = kinds.get(animal, None) if kind is None: # Ask the user what sort of kind of animal it is. # In Python 2, you MUST use raw_input instead of input! kind = input("What kind of animal is '%s'?" % animal) kind = kind.strip().lower() kinds[animal] = kind location = locations.get(kind, None) if location is None: # Ask the user where this kind of animal lives. # In Python 2, you MUST use raw_input instead of input! location = input("Where do '%s' animals live?" % kind) location = location.strip().lower() locations[kind] = location print(animal, "lives in or on", location) Note that direct lookups into a dict are written with square brackets: kinds[animal] but optional lookups with a default value use round brackets (parentheses): kinds.get(animal, None) # None is the default. Also, there's nothing you can write in Python which will be faster than looking it up in a dict. If you have concerns about looping over lists repeatedly, you are right to worry about speed. But if you can get the same data into a dict or two, dict lookups are *blazingly* fast. Are you able to move your data into the list and dictionary formats shown above? The secret to programming successfully is that getting the data into the right format is half the battle. Once you have the data in the right format, everything else becomes so much simpler. For example, I might take your "a" list and convert it like this: a=[['bass',9],['eagle',36],['human',68]] animals = [sublist[0] for sublist in a] That extracts out the strings "bass", "eagle" etc. and puts them into a list called "animals". Converting your "b" list to a dict should be even easier: b=[['bass', 'fish'],['eagle', 'bird'],['dog', 'mammal']] kinds = dict(b) Can you work out how to get the locations you need, or do you need more help? Feel free to ask any questions you have! -- Steve From martin at linux-ip.net Sat Mar 21 03:20:33 2015 From: martin at linux-ip.net (Martin A. Brown) Date: Fri, 20 Mar 2015 19:20:33 -0700 Subject: [Tutor] UPDATE: Is there a 'hook' to capture all exits from a python program? In-Reply-To: <20150321010317.GG31862@ando.pearwood.info> References: <1426881853.77899.BPMail_high_carrier@web163801.mail.gq1.yahoo.com> <20150321010317.GG31862@ando.pearwood.info> Message-ID: Hi, This is mostly a distant footnote to Doug Basberg's original question, which I believe is largely answered at this point. Albert-Jan Roskum, Alan Gauld and Steven D'Aprano were asking about signals and how they are handled (in Un*xen). I am trying to address that. >> Yeah, I know you can catch a signal and add your own handler, but I >> meant what is the default Python suspend behaviour? Does it execute any >> outstanding exception blocks? What about finally blocks? Or, if about to >> exit a context manager, the __exit__ method? Suspension of the process has nothing to do with Python. This happens in the Un*x scheduler--long before Python is involved. > Depends on the signal. Correct. It all depends on the signal. Short version: (Apologies, Jack Nicholson, in demonic form or otherwise): * You can't 'handle': STOP, CONT, KILL, SEGV, BUS. * You can handle: HUP, INT, QUIT, USR1, USR2, PIPE, ALRM, TERM and others. Short advice (though I do not have experience dealing with signals in a realtime environment). I suggest the following guidelines: 1. Do not catch a signal you cannot handle (or do not intend to handle). 2. Do everything you can at startup to make sure that the environment in which you are operating is as you expect. 3. Catch all the signals you want to catch and, in response to receiving such a signal, do what you need in order to shut down cleanly. This coexists peacefully with the handy atexit handlers suggested earlier. (I am normally not at all a fan of an unspecific try--finally, but I get what Peter Otten is suggesting and might make the same choice, were I faced with Doug Basberg's situation.) > I'm not an expert on how signals work in Linux, or other Unixes, > but I expect that, in the absense of a specific signal handler to > catch it, the "sleep" (pause?) signal will cause the interpreter > to just stop and wait. I think that's signal 19 on Linux, and 18 > to wake. Longer version: I have experience with handling Linux signals and Python. There may be subtle differences on other Un*xen. If you wish to know more, I would suggest reading the chapter on Signals in _Advanced Programming in the Unix Environment_ (Chapter 10, in my second edition by Stevens & Rago). You cannot catch nor handle: * SIGSTOP (19), because that tells Un*x, "Please remove this process from the scheduler, i.e. freeze it!" * SIGCONT (18), because that tells Unix, "Please restore this process to normal scheduling, i.e. unfreeze it." * SIGKILL (9), because that tells Unix, "Terminate this thing, with prejudice! Do not tell it what happened." This means, your Un*X will never actually deliver SIGSTOP, SIGCONT or SIGKILL to Python and your program. I believe that you cannot do anything with the following signals: * SIGSEGV (11), because this means that there has been a memory fault. Python is a sufficiently high-level language, that, if this happens, this should not be your code doing it. (Unless you are writing C extensions for Python, and then, of course, you know what you are doing....) * SIGBUS (7), because this is extraordinarily rare (today), but would be a case of trying to access memory that does not exist. In practice, I have seen SIGSEGV often over the last 20 years (perhaps I have worked with flaky software, or perhaps that is just something that happens in this line of work). I have seen SIGBUS very rarely (usually a precursor to a machine eating itself for lunch). The signals STOP and CONT are so rarely exhibited that they are perceived as exotic specimens when demonstrated. The KILL signal is the big hammer that everybody learns in their first month using any Un*x (which is unfortunate because of the power it commands). > Signal 9 doesn't give the interpreter to do anything. The OS just > yanks the carpet out from under its feet and terminates the > process with extreme prejudice. Signal 9 cannot be caught, no > signal handlers will detect it, no try...finally blocks will run. > The process just stops. Correct. When the (Linux | Un*x) kernel has a signal 9 for a process, that process does not get any chance to clean up. It simply disappears. It is never given an opportunity to run again--i.e. it will never be scheduled again. > Don't use kill -9 unless you need to. I always try three steps to > kill a rogue process: > > First use "kill " with no other arguments. Give it 30 > seconds or so to let the process tidy up after itself, and if it > still hasn't quiet, try "kill -HUP ". Again, give it 30 > seconds or so. Then, if and only if necessary, "kill -9 > ". Agreed. In my experience, most mature sysadmins do this, too. >> Or does it just stop and wait till its resumed? Kind of like >> an implicit yield statement? Sending a SIGSTOP to a process is equivalent to freezing it in memory/process space. I sometimes think of this as suspend (because of ctrl-Z in bash, Alan). The process does not know that time is passing. It does not get scheduled. (Its memory may be swapped out, but this doesn't matter.) Sending SIGSTOP to a STOPped process is a noop. Sending a SIGCONT to a process is equivalent to unfreezing it in memory/process space. The process will now be scheduled (along with all other processes). It will resume, not knowing how much time has passed since it was last running. (If it checks gettimeofday() or checks the clock in some other fashion, of course, it can determine that it was suspended.) Sending a SIGCONT to a running process is a noop. If you are going to list out explicitly the signals you wish to catch, the following are the most common signals to catch which represent various ways people attempt to tell a process to terminate. They are: INT, QUIT, HUP, TERM and maybe HUP. HUP is special, as it is commonly understood (in the sysadmin world) to mean things like 're-read your config file' or 'restart a certain routine activity'. So, it may surprise some folk if a process died after receiving a HUP. This may be desirable--it depends entirely on what your software does. Stopping and starting are hard! -Martin -- Martin A. Brown http://linux-ip.net/ From cs at zip.com.au Sat Mar 21 06:26:28 2015 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 21 Mar 2015 16:26:28 +1100 Subject: [Tutor] UPDATE: Is there a 'hook' to capture all exits from a python program? In-Reply-To: References: Message-ID: <20150321052628.GA33856@cskk.homeip.net> On 20Mar2015 19:20, Martin A. Brown wrote: [...] >Short version: >(Apologies, Jack Nicholson, in demonic form or otherwise): > * You can't 'handle': STOP, CONT, KILL, SEGV, BUS. You can handle SEGV and BUS. Though probably not meaningfully in Python, haven't tried; if they fire in Python something internal is already badly wrong. IIRC, in the distant past the Bourne shell used SIGSEGV as a trigger to allocate more memory:-) Really, the only 3 a UNIX process can't intercept are the first three: STOP, CONT, KILL. Since everything else Martin says seems to be in the context of "in Python", no other quibbles. Cheers, Cameron Simpson From __peter__ at web.de Sat Mar 21 09:19:42 2015 From: __peter__ at web.de (Peter Otten) Date: Sat, 21 Mar 2015 09:19:42 +0100 Subject: [Tutor] UPDATE: Is there a 'hook' to capture all exits from a python program? References: <00fd01d062b9$924c4c50$b6e4e4f0$@net> Message-ID: Alan Gauld wrote: > On 20/03/15 09:37, Peter Otten wrote: > >>> def close_relay(e=None,v=None,t=None): >>> try: >>> if not relay_closed() >>> really_close_relay() >>> except: >>> really_close_relay() > > The purpose of the if clause is to ensure that > if the function is called many times you only > close the relay once (I surmised that more than > once could be harmful?) > >>> import sys, atexit >>> atexit.register(close_relay) >>> sys.excepthook = close_relay > > atexit should be overkill, but it might be needed > if for some reason the interpreter dies while > performing the usual cleanup. > > excepthook replaces the usual exception mechanism > with a clean up. This is needed for cases where an > exception occurs before getting to the finally. We > want to close the relay ASAP, not waiting till > the interpreter decides to call the finally. > >>> try: >>> main program here >>> finally: >>> close_relay() > > This is the happy path where everything shuts down as expected. > >> That reeks of cargo cult. Are there actual scenarios for each of the >> three mechanisms where it is the only one that works? > > In real-time you never trust anything. > Always cover your back. > >> I would expect that >> >> try: >> main program here >> finally: >> close_relay() >> >> provides the same level of confidence, > > Only if the interpreter is behaving as normal. > The hooks are to try (we hope) to catch cases where > the interpreter has broken its normal flow. > > So the scenarios are: > > 1) an unexpected exception occurs - close the relay ASAP. > - Use excepthook > > 2) The interpreter gets sent a kill or similar unexpected > termination - use atexit because finally may not get > called. (I'm not sure, so belt n' braces here) > (BTW Does anyone know what the interpreter does when > suspending - Ctrl-Z in Unix land?) > > 3) Normal program exit. Use the finally clause. > > But its only ever going to be a best endeavour, that's > why Python is not suitable for true real-time/critical apps... > But I'd never trust any environment to its usual behaviour > if there is a possibility of something being broken. > In this case the relay and its battery pack. > >> the program closes normally or the main code raises an exception, but not >> if the process is killed. > > What's not clear in the Python documentation is how Python responds > to a kill(or suspend). I'd hope the atexit got called even in a kill. > I would not expect the finally to be executed. > > Of course, if its a seg fault you are probably stuffed either way... I ran a few experiments: $ cat bnb.py import atexit import os import signal import sys import time def handle_except(*args): print("except", args, flush=True) def handle_exit(): print("exit", flush=True) def register_signalhandler(sig): def handler(*args): print("receiving signal", sig, args, flush=True) signal.signal(sig, handler) def main(): print("Hello from", os.getpid()) while True: print(".", flush=True, end="") time.sleep(1) sys.excepthook = handle_except atexit.register(handle_exit) for sig in sys.argv[1:]: register_signalhandler(getattr(signal, sig)) try: main() finally: print("finally", flush=True) $ python3 bnb.py Hello from 32578 ....^Cfinally except (, KeyboardInterrupt(), ) exit When there is no signal handler all three mechanisms work, in the order - finally - except hook - exit handler Now let's kill: $ python3 bnb.py Hello from 32584 .............Terminated None of the three are invoked. Let's install a signal handler for SIGTERM: $ python3 bnb.py SIGTERM Hello from 32593 .................receiving signal 15 (15, ) ...............^Cfinally except (, KeyboardInterrupt(), ) exit The signal is intercepted (and ignored by the no-op handler thus the additional Ctrl-C). If we raise a SystemExit in the handler - finally - exit handler will be invoked, but not the except hook. $ kill -9 of course cannot be intercepted. My conclusions: - If finally does not work nothing does. - Signal handlers increase safety Bonus: $ python3 bnb.py SIGTSTP Hello from 32614 ........^Zreceiving signal 20 (20, ) ........^Cfinally except (, KeyboardInterrupt(), ) exit So Ctrl-Z can be intercepted. The program could put the relay into a safe state before it suspends. From momohund1972 at yahoo.com Sat Mar 21 08:05:51 2015 From: momohund1972 at yahoo.com (Michael Omohundro) Date: Sat, 21 Mar 2015 00:05:51 -0700 Subject: [Tutor] ArcGIS Create a python script to generate north-facing aspect raster from digital elevation model Message-ID: <1426921551.78493.YahooMailBasic@web161005.mail.bf1.yahoo.com> Does anyone know how to create a python script to generate an aspect raster from the input elevation in a digital elevation model? I need to specify TWO variables as user parameters: input elevation and output north-facing aspect. I need to create an aspect raster from the input elevation from a digital elevation model. I need to find the north facing aspect is the trick, between 0 and 22.5 and between 337.5 ? 360. These are the north facing elevation ranges. I need to reclass the north facing aspect into a 1/Nodata raster. Any cell is north facing should have value 0, other cells should be NoData. I need to save the results as a permanent raster named as my second input parameter. I can't use ModelBuilder then convert it into Python script. Here is what I have so far: # Created on: March 20 2015 # Usage: lab03-5 Aspect from raster surface # Requirements: ArcGIS Desktop and Spatial Analyst Extension # --------------------------------------------------------------------------- # Import system modules import arcpy from arcpy import env from arcpy.sa import * arcpy.env.overwriteOutput = True # Set environment settings.? dem30 is the digital elevation model that holds the elevation data. elevation = r"F:\NW_Missouri_State_University\_Python_Class\Week_10\lab10\dem30" # North facing elevation 1, range 0 to 22.5 inRange_North1 = range (0, 22.5, 0.5) #North facing elevation 2,range 337.5 - 360 inRange_North2 = range (337.5, 360, 0.5) # Set local variables inRaster = "elevation" # Check out the ArcGIS Spatial Analyst extension license arcpy.CheckOutExtension("Spatial") # Execute Aspect outAspect = Aspect(inRaster) # Save the output outAspect.save("F:\NW_Missouri_State_University\_Python_Class\Week_10\lab10\TestLab10_5") # Specify the current workspace as the workspace where the input elevation raster is. # Extract the base name of the elevation raster. arcpy.env.workspace = os.path.dirname(elevation) inputElev = os.path.basename(elevation) # Specify the output raster name as the input elevation name appending with ?NorthFacing?. saveReclass = arcpy.env.workspace + os.sep + inputElev + "NorthFacing" # Check out the Spatial Analyst extension license for the script. arcpy.CheckOutExtension("Spatial") # Construct a map algebra expression to find the cell with elevation equal to the range values. minRaster = arcpy.sa.Raster(inputElev) = inRange_North1 maxRaster = arcpy.sa.Raster(inputElev) = inRange_North2 # Construct a map algebra expression to perform a Boolean And # operation on the cell values of minRaster and maxRaster. # If the cell value is 1 in both raster, then set the output cell value as 1. # Otherwise, set the output cell value as 0. Save the output raster as variable outRaster. outRaster = minRaster & maxRaster # Create a remap object through RemapValue() function. # If the old value is 0, then set the new value as NODATA. # If the old value is 1, then set the new value as 1. remap = arcpy.sa.RemapValue([[0, "NODATA"], [1, 1]]) outReclassify = arcpy.sa.Reclassify( ? ? outRaster, "Value", remap, "NODATA") #Call the save method on the raster object to save the raster as permanent dataset. outReclassify.save(saveReclass) # Check in the Spatial Analyst extension license. arcpy.CheckInExtension("Spatial") From cs at zip.com.au Sun Mar 22 03:29:24 2015 From: cs at zip.com.au (Cameron Simpson) Date: Sun, 22 Mar 2015 13:29:24 +1100 Subject: [Tutor] UPDATE: Is there a 'hook' to capture all exits from a python program? In-Reply-To: References: Message-ID: <20150322022924.GA99718@cskk.homeip.net> On 21Mar2015 09:19, Peter Otten <__peter__ at web.de> wrote: >I ran a few experiments: [...] >Bonus: >$ python3 bnb.py SIGTSTP >Hello from 32614 >........^Zreceiving signal 20 (20, ) >........^Cfinally >except (, KeyboardInterrupt(), at 0x7f2f895a2bc8>) >exit > >So Ctrl-Z can be intercepted. The program could put the relay into a safe >state before it suspends. Yes, Ctrl-Z (SIGTSTP) can be caught ("stop from terminal", ^Z). Note, however, that SIGSTOP cannot be caught ("stop"); it is not SIGTSTP. Basicly you can kill (SIGKILL - abort process and never schedule it again), stop (SIGSTOP - cease scheduling this process) and continue (SIGCONT - resume scheduling this process) a process from outside a process and the process cannot intercept these. Which is just great! However, it means there are some things you cannot manage from within the process. This is where watchdogs of various kinds come into play: an external process of some kind which monitors the primary process (or something it manages), and take action if the primary process goes away or some activity does not occur for a period. Cheers, Cameron Simpson Cordless hoses have been around for quite some time. They're called buckets. - Dan Prener From robertvstepp at gmail.com Tue Mar 24 02:42:23 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Mon, 23 Mar 2015 20:42:23 -0500 Subject: [Tutor] List comprehensions to search a list--amazing! In-Reply-To: <550A5A46.9020409@davea.name> References: <550A5A46.9020409@davea.name> Message-ID: On Thu, Mar 19, 2015 at 12:10 AM, Dave Angel wrote: > The catch to a list comprehension is it has to visit all the elements, while > a binary search would visit log-base-2 of them. So instead of 10000 > elements, you'd be searching about 14 items. I suspected as much, but had not verified this. Nonetheless, this may prove sufficiently fast. I will have to test this with my final data files. Right now I am using test cases, while I continue to design, check, rewrite, etc. > For large lists, it'd probably be much quicker to use the bisect module. > https://docs.python.org/3.4/library/bisect.html Can you give me a ballpark number for "large", where this would start making a meaningful difference? > Check out bisect.bisect_left() and bisect.bisect_right() It looks like this should work. Thanks! I will investigate. > I don't see how to directly use those functions on a list which is > reverse-sorted, but the source is available. On my install, it's located > at: > > /usr/lib/python3.4/bisect.py And I see this is available on my oldest Python installlation, 2.4.4, too. -- boB From robertvstepp at gmail.com Tue Mar 24 02:55:46 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Mon, 23 Mar 2015 20:55:46 -0500 Subject: [Tutor] List comprehensions to search a list--amazing! In-Reply-To: References: <550A5A46.9020409@davea.name> Message-ID: On Thu, Mar 19, 2015 at 4:52 AM, Peter Otten <__peter__ at web.de> wrote: > Dave Angel wrote: [...] > By the way, if you were to use a plain old loop the expected speedup over > the listcomp would be 2. You can break out of the loop when you have found > the gap, after iterating over one half of the list on average. > > So for-loops are twice as amazing ;) Actually, this was my first approach to solving the problem. >> The catch to a list comprehension is it has to visit all the elements, >> while a binary search would visit log-base-2 of them. So instead of >> 10000 elements, you'd be searching about 14 items. >> >> For large lists, it'd probably be much quicker to use the bisect module. >> https://docs.python.org/3.4/library/bisect.html >> >> >> Check out bisect.bisect_left() and bisect.bisect_right() >> >> I don't see how to directly use those functions on a list which is >> reverse-sorted, but the source is available. On my install, it's >> located at: >> >> /usr/lib/python3.4/bisect.py >> > > To back Dave's suggestion with some empirical data here are two ways to make > bisect() work with a descending list (though if possible I would recommend > that you change your script to use ascending lists). I could easily do this, though the data naturally presents itself as I stated originally. > $ cat reverse_bisect2.py > import bisect > import random > > def break_listcomp(L, Vt): > S = [i for i, V in enumerate(L) if L[i] >= Vt >= L[i + 1]] > return S[0] > > def break_bisect_reverse(L, Vt): > L.reverse() > result = bisect.bisect(L, Vt) > L.reverse() > return len(L) - result -1 > > class Items: > def __init__(self, items): > self.items = items > def __len__(self): > return len(self.items) > def __getitem__(self, index): > return self.items[len(self.items) - 1 - index] > > def break_bisect_virt(L, Vt): > return len(L) - 1 - bisect.bisect(Items(L), Vt) > > random.seed(42) > N = 10**6 > data = [random.randrange(N) for i in range(10**5)] > data = data + data > data.sort(reverse=True) > sample = random.sample(data, 10) > $ > > break_bisect_reverse() reverses the list before and after applying bisect. > This is still O(N), but the actual work is done in C. > > break_bisect_virt() wraps the actual list in a class that translates > > items[0] to items[len(items)-1] > items[1] to items[len(items)-2] Thank you for taking time to write this. I may have questions later. > and so on, thus providing a reversed view of the list without moving any > values. This severely slows down access to a single value, but as bisect > needs much fewer lookups than the listcomp the overall result is still a > massive speedup. The actual timings: > > $ python3 -m timeit -s 'from reverse_bisect2 import data, sample, > break_listcomp as f' '[f(data, v) for v in sample]' > 10 loops, best of 3: 781 msec per loop > > $ python3 -m timeit -s 'from reverse_bisect2 import data, sample, > break_bisect_reverse as f' '[f(data, v) for v in sample]' > 100 loops, best of 3: 15 msec per loop > > $ python3 -m timeit -s 'from reverse_bisect2 import data, sample, > break_bisect_virt as f' '[f(data, v) for v in sample]' > 1000 loops, best of 3: 221 usec per loop > > So reverse/bisect is 50 times faster than the listcomp, and > bisect/virt is 3500 times faster than the listcomp. You present a compelling case! > I expect that a prepackaged linear interpolation function from numpy/scipy > can still do better, and also handle the corner cases correctly. To use such > a function you may have to reverse order of the values. This is not an option for me as I would not be allowed to install numpy/scipy. -- boB From davea at davea.name Tue Mar 24 03:17:11 2015 From: davea at davea.name (Dave Angel) Date: Mon, 23 Mar 2015 22:17:11 -0400 Subject: [Tutor] List comprehensions to search a list--amazing! In-Reply-To: References: <550A5A46.9020409@davea.name> Message-ID: <5510C927.5040001@davea.name> On 03/23/2015 09:42 PM, boB Stepp wrote: > On Thu, Mar 19, 2015 at 12:10 AM, Dave Angel wrote: >> The catch to a list comprehension is it has to visit all the elements, while >> a binary search would visit log-base-2 of them. So instead of 10000 >> elements, you'd be searching about 14 items. > > I suspected as much, but had not verified this. Nonetheless, this may > prove sufficiently fast. I will have to test this with my final data > files. Right now I am using test cases, while I continue to design, > check, rewrite, etc. > >> For large lists, it'd probably be much quicker to use the bisect module. >> https://docs.python.org/3.4/library/bisect.html > > Can you give me a ballpark number for "large", where this would start > making a meaningful difference? > Not really. See Steve's response for some numbers. If I had to guess, I'd say that for lists over 100 items, you should use bisect or equivalent. But I'd also say you should have one algorithm in your final code, even if it's sub-optimal for tiny lists. If even a fraction of the searches are going to be on a list of 10k items, you should switch to a bisect approach. I'd have to measure it, same as anyone. And because of the reverse-ordering problem, you have to weigh the advantages of using a standard library (which is unlikely to be buggy), versus making an edited version which works directly on reversed lists. It also can matter how many times you're searching the same list. If you're going to be many lookups, it's worth keeping a reversed copy. You can reverse as simply as rlist = mylist[::-1] -- DaveA From davea at davea.name Tue Mar 24 03:20:54 2015 From: davea at davea.name (Dave Angel) Date: Mon, 23 Mar 2015 22:20:54 -0400 Subject: [Tutor] List comprehensions to search a list--amazing! In-Reply-To: <5510C927.5040001@davea.name> References: <550A5A46.9020409@davea.name> <5510C927.5040001@davea.name> Message-ID: <5510CA06.9050105@davea.name> On 03/23/2015 10:17 PM, Dave Angel wrote: > On 03/23/2015 09:42 PM, boB Stepp wrote: > Not really. See Steve's OOPS. Peter's > response for some numbers. If I had to guess, > I'd say that for lists over 100 items, you should use bisect or > equivalent. But I'd also say you should have one algorithm in your > final code, even if it's sub-optimal for tiny lists. If even a fraction > of the searches are going to be on a list of 10k items, you should > switch to a bisect approach. > -- DaveA From steve at pearwood.info Tue Mar 24 03:21:26 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 24 Mar 2015 13:21:26 +1100 Subject: [Tutor] List comprehensions to search a list--amazing! In-Reply-To: References: <550A5A46.9020409@davea.name> Message-ID: <20150324022125.GV31862@ando.pearwood.info> On Mon, Mar 23, 2015 at 08:42:23PM -0500, boB Stepp wrote: > On Thu, Mar 19, 2015 at 12:10 AM, Dave Angel wrote: > > The catch to a list comprehension is it has to visit all the elements, while > > a binary search would visit log-base-2 of them. So instead of 10000 > > elements, you'd be searching about 14 items. > > I suspected as much, but had not verified this. Nonetheless, this may > prove sufficiently fast. I will have to test this with my final data > files. Right now I am using test cases, while I continue to design, > check, rewrite, etc. > > > For large lists, it'd probably be much quicker to use the bisect module. > > https://docs.python.org/3.4/library/bisect.html > > Can you give me a ballpark number for "large", where this would start > making a meaningful difference? Tell us what you consider a meaningful difference :-) What counts as "too slow" will depend on: - what you are doing - how often you are doing it - what else you're doing at the same time - what hardware you have to run it on - whether you are running the program only once, or repeatedly etc. E.g. taking 30 seconds to iterate over a billion items in a list is insignificant if this is part of a bigger program which takes twenty minutes to run, but critical if you are hoping to run the program thirty times a second, hundreds of times a day. But I've never heard anyone complain that a program was too fast :-) (Actually, that's not quite true. Sometimes if the user is expecting a program function to take a while, say "Rebuild database", and it actually runs near instantaneously, it is indeed *too fast* because it can give the user the idea that the rebuild function isn't working. But that's a UI issue, not a speed issue.) But if you twist my arm, and force me to pluck some round numbers from thin air, I would guess: - for under ten items, a linear search will be insignificantly faster; - for under a hundred items, there's no meaningful difference; - for under a million items, binary search will be typically better but a linear search still acceptable ("everything is fast for small N"); - for over a million items, linear search will typically be unacceptably slow. For certain definitions of what's acceptable or not :-) -- Steve From steve at pearwood.info Tue Mar 24 03:23:25 2015 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 24 Mar 2015 13:23:25 +1100 Subject: [Tutor] List comprehensions to search a list--amazing! In-Reply-To: <5510C927.5040001@davea.name> References: <550A5A46.9020409@davea.name> <5510C927.5040001@davea.name> Message-ID: <20150324022324.GW31862@ando.pearwood.info> On Mon, Mar 23, 2015 at 10:17:11PM -0400, Dave Angel wrote: > On 03/23/2015 09:42 PM, boB Stepp wrote: > >Can you give me a ballpark number for "large", where this would start > >making a meaningful difference? > > > > Not really. See Steve's response for some numbers. o_O Have you borrowed Guido's Time Machine? I hadn't even finished writing my post??? -- Steve From juan0christian at gmail.com Sat Mar 28 20:06:30 2015 From: juan0christian at gmail.com (Juan C.) Date: Sat, 28 Mar 2015 19:06:30 +0000 Subject: [Tutor] Python OO Message-ID: So, I'm trying to create a little script here using OO to learn it, and I got some questions: The script uses the Movie DB API, and I have a Series module. In this module I have 3 classes, 'Serie', 'Season' and 'Episode'. In __main__.py I will instantiate a serie like so 'some_random_name = Serie(123)', where 123 is the ID of the serie in the API. Serie class: title, year, length (X seasons), serieid and a function get_season(self, number) Season class: number, year, length (X episodes) and a function get_episode(self, number) Episode class: title, number, date and two functions, get_character(self, index) and crew_size() What would be the best approach using OO? Should I use class inheritance and how? What would be the best way to create the whole Serie instance with the least API calls possible? Remember that when I create a new Serie(serie_id) I create the whole thing, every season, every episode. Many thanks. PS: If you guys know a good book/course/tutorial about Python OO feel free to post it here. From duxbuz at hotmail.com Sat Mar 28 20:37:58 2015 From: duxbuz at hotmail.com (Ian D) Date: Sat, 28 Mar 2015 19:37:58 +0000 Subject: [Tutor] escape character regex Message-ID: Hi I run a regex like this: >pchars = re.compile('\x00\x00\x00') #with or without 'r' for raw on a string like this: >data = "['broadcast', 'd8on\x00\x00\x00\x11broadcast', 'd11on']" >print "found pchars :",pchars.findall(data) which returns: >found pchars : ['\x00\x00\x00'] But if I try to match the extra digits at the end like this: >pchars = re.compile('\x00\x00\x00\x\d+') I get an error: >ValueError: invalid \x escape Or if I use another ide than idle it actually flags it as an "illegal hexadecimal escape sequence" How could I match the \x00\x00\x00\x11 portion of the string? I have tried escape sequence \\x Thanks From alan.gauld at btinternet.com Sat Mar 28 21:23:07 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 28 Mar 2015 20:23:07 +0000 Subject: [Tutor] Python OO In-Reply-To: References: Message-ID: On 28/03/15 19:06, Juan C. wrote: > The script uses the Movie DB API, and I have a Series module. So far so good, but you probably want to hide the MDB API inside your module so that if you replace it with another one (or add another one later) you don't change your modules API. > In this module I have 3 classes, 'Serie', 'Season' and 'Episode'. In > __main__.py I will instantiate a serie like so 'some_random_name = > Serie(123)', where 123 is the ID of the serie in the API. > > Serie class: title, year, length (X seasons), serieid and a function > get_season(self, number) That looks like a data oriented class, which may not be a bad thing but you should also be thinking about the methods. What will a series object do in the context of your application? How will those data attributes help the methods do their job? Also is date really an attribute of the series? Or is it a derived feature based on the seasons? And maybe should be a range, or a tuple of values? The "function" - do you mean method? - get_season() should probably be a private method and maybe called by __init__()? (I'm assuming this is the thing that talks to the MDB API? > Season class: number, year, length (X episodes) and a function > get_episode(self, number) Much as before. Is the year an attribute or derived from the episodes? Can a series be shown more than once? Does it have multiple dates in that case? > Episode class: title, number, date and two functions, get_character(self, > index) and crew_size() This is the core data, most of the higher level stuff can be derived from this. And it has two methods (not functions). Or are they also just calls to the MDB DB? Or are they methods that your users will want to use? Itsnot clear. A good way to design classes is the CRC technique. For each class define C - Class name R - responsibility - what does the class do/own? Collaborators - Which other classes (or APIs - which could be classes in their own right)will the class interact with? Many projects use a literal 5x3inch paper card index for their classes using this technique. For 3 classes thats probably overkill! > What would be the best approach using OO? Should I use class inheritance > and how? Inheritance implies an "is-a" relationship Is it true that a series is-a season? Or is a season a series - now that sounds more likely... Is an episode a season or series? Is a season or series an episide? - Probably not. Are they all 3 types of BroadcastEvent - maybe. For now I'd go with separation, if it turns out that all the operations in season are the same as on series then think about converting to inheritance. Remember its similarity of operation that defines inheritance not similarity of data. Thats what buys you polymorphism. > What would be the best way to create the whole Serie instance with > the least API calls possible? I don't know the API so I can't answer that. > Remember that when I create a new Serie(serie_id) I create the whole thing, > every season, every episode. Do you have to? Why not fetch the seasons and episodes on demand? > PS: If you guys know a good book/course/tutorial about Python OO feel free > to post it here. Lots of them, and not all python specific. I still like Grady Booch's book OOA&D even though its C++ based. Even better is if you get the first edition from a library since it is multi lingual, and much better for it. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From juan0christian at gmail.com Sat Mar 28 22:39:02 2015 From: juan0christian at gmail.com (Juan C.) Date: Sat, 28 Mar 2015 21:39:02 +0000 Subject: [Tutor] Python OO In-Reply-To: References: Message-ID: On Sat, Mar 28, 2015 at 5:23 PM Alan Gauld wrote: > That looks like a data oriented class, which may not be a bad thing but you should also be thinking about the methods. What will a series object do in the context of your application? How will those data attributes help the methods do their job? My app doesn't do anything specific. It's just for the sake of doing something. Maybe, I just create a serie instance and print all the info about it like title, year of launch, numbers of seasons, go through each season and post each episode info like title, air date, summary, things like that. > Also is date really an attribute of the series? Or is it a derived feature based on the seasons? And maybe should be a range, or a tuple of values? The date thing is a little different in each case, Serie will have only the year, let's say "Breaking Bad" would be 2008. Season would have year + month. Episode would have full date, year + month + day. > The "function" - do you mean method? - get_season() should probably be a private method and maybe called by __init__()? (I'm assuming this is the thing that talks to the MDB API? Yes, I mean method. Yeah, forget about get_season(), I was thinking a little "Java" there. The best thing to do would be having a self.seasons just like every else (self.title, self.year and so on) list of 'Season' instances and then I call it directly. > Much as before. Is the year an attribute or derived from the episodes? Can a series be shown more than once? Does it have multiple dates in that case? Year is an attribute of season. Not multiple dates, it will have the year + month of the first episode of the season. > This is the core data, most of the higher level stuff can be derived from this. And it has two methods (not functions). Or are they also just calls to the MDB DB? Or are they methods that your users will want to use? Itsnot clear. I don't want to create a bunch of "get" methods, I read in many places that Python isn't Java, you don't need "getters/setters" you can pretty much call the 'self.var' directly. Again, I was thinking 'Java' there, I don't need a 'get_character' method, I can have a list on my __init__ and call it directly, same goes for crew_size, I just use len() on characters. > I don't know the API so I can't answer that. The API: http://docs.themoviedb.apiary.io/#reference/tv/tvid/get It returns the data in JSON and I use requests 'json' method to read the data. > Do you have to? Why not fetch the seasons and episodes on demand? Well, let's say I create a new instance, 'bb = Serie(1396) # Breaking Bad', won't I need to have my 'self.seasons' list ready, because I can call it anytime? And the same logic applies to the 'Season' class, I need to have the 'self.episodes' ready. To call on demand I will need to change to code and indead create methods like 'get_season' and 'get_episode'. Maybe I'm a little confuse and can't really express my logic here. From alan.gauld at btinternet.com Sun Mar 29 00:15:19 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sat, 28 Mar 2015 23:15:19 +0000 Subject: [Tutor] Python OO In-Reply-To: References: Message-ID: On 28/03/15 21:39, Juan C. wrote: > you should also be thinking about the methods. What will a series object > do in the context of your application? > My app doesn't do anything specific. It's just for the sake of doing > something. Maybe, I just create a serie instance and print all the info > about it like title, year of launch, numbers of seasons, go through each > season and post each episode info like title, air date, summary, things > like that. OK, So lets say you don't print all the info about it. Say the series prints its own information - or at least presents it as a string that you can print? Then you just say: s = Series(ID) print(s) So you want a method for getting a printable sting from series. But how does series print itself? By printing each of its seasons? class Series: ... def __str__(self): output = ['...'] # whatever header you need for season in self.seasons: output.append(str(season)) return '\n'.join(output) And how does a series print? Maybe it prints some header details about each episode? class Season: ... def __str__(self): output = ['....'] # same pattern as above for episode in self.episodes: output.append(episode.get_header()) return '.'.join(output) So we have an operation and we see how it ripples down through the classes and how the data attributes support it. What else might you want to do with these objects beside print them? find specific episodes in a series(search)? List plot synopses? Search by featured actor? others? >> Also is date really an attribute of the series? Or is it a derived > feature based on the seasons? And maybe should be a range, or a > tuple of values? > > The date thing is a little different in each case, Serie will have only the > year, let's say "Breaking Bad" would be 2008. Season would have year + > month. Episode would have full date, year + month + day. But isn't that the date of the first episode in each case? So you don't need to store it. You can find it by asking the first episode (of the first series) for the date. > little "Java" there. The best thing to do would be having a self.seasons > just like every else (self.title, self.year and so on) list of 'Season' > instances and then I call it directly. Yes the attributes would be self.xxx where necessary. >> Much as before. Is the year an attribute or derived from the > episodes? Can a series be shown more than once? Does it have > multiple dates in that case? > > Year is an attribute of season. Not multiple dates, it will have the year + > month of the first episode of the season. Exactly, its the date of the first episode, so you can get it on demand, rather than store it. (Of course you might store it for performance reasons but its not necessary.) > I don't want to create a bunch of "get" methods, I read in many places that > Python isn't Java, you don't need "getters/setters" you can pretty much > call the 'self.var' directly. Thats true, but better still is to put the intelligence of how/when the attributes are accessed into the object itself. It owns the data so it should manipulate it. > Again, I was thinking 'Java' there, I don't need a 'get_character' method, > I can have a list on my __init__ and call it directly, same goes for > crew_size, I just use len() on characters. Yes, but you could also use the API as you need it too. But again performance may dictate otherwise. It all depends on what you need/want from the objects. >> Do you have to? Why not fetch the seasons and episodes on demand? > > Well, let's say I create a new instance, 'bb = Serie(1396) # Breaking > Bad', won't I need to have my 'self.seasons' list ready, because I can call > it anytime? If you are net connected you can call the API anytime and save storage space. That way it will always be up to date, no risk of new data sneaking in. But it all depends on your application needs. Either way is valid. I'm just trying to challenge you to step back from a data centric view. Objects are more than data, they are also behaviour and the behaviour is usually more important to the design than the data. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From dyoo at hashcollision.org Sun Mar 29 00:48:33 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sat, 28 Mar 2015 16:48:33 -0700 Subject: [Tutor] escape character regex In-Reply-To: References: Message-ID: > But if I try to match the extra digits at the end like this: > >>pchars = re.compile('\x00\x00\x00\x\d+') > > I get an error: > >>ValueError: invalid \x escape You should just be able to say: pchars = re.compile('\x00\x00\x00..') because it looks like you're trying to grab at the last two characters. Hexadecimal escape is just another notation for expressing a character. So, for example, take a look at this string. '\x68\x69\x20\x69\x61\x6e' Try printing it. Hope this helps. From davea at davea.name Sun Mar 29 01:21:09 2015 From: davea at davea.name (Dave Angel) Date: Sat, 28 Mar 2015 20:21:09 -0400 Subject: [Tutor] escape character regex In-Reply-To: References: Message-ID: <55174575.4010104@davea.name> On 03/28/2015 03:37 PM, Ian D wrote: > Hi > > > I run a regex like this: > >> pchars = re.compile('\x00\x00\x00') #with or without 'r' for raw Which one did you actually want? The 3 byte sequence consisting of nulls, or the 12 byte one containing zeroes and backslashes? I'm going to assume the former, in which case you cannot use 'r' for raw. Unless you've got a null key on your keyboard. > > on a string like this: > >> data = "['broadcast', 'd8on\x00\x00\x00\x11broadcast', 'd11on']" > >> print "found pchars :",pchars.findall(data) > > which returns: > >> found pchars : ['\x00\x00\x00'] > > > But if I try to match the extra digits at the end like this: > >> pchars = re.compile('\x00\x00\x00\x\d+') > > I get an error: > >> ValueError: invalid \x escape The \x escape sequence must be followed by exactly two hex digits, and forms a single byte from them. What did you want that byte to be, and why didn't you specify it? > > Or if I use another ide than idle it actually flags it as an "illegal hexadecimal escape sequence" > The question is not what the various IDE's produce, but what the Python compiler produces. So once you started getting errors, you really should have just run it in the interactive interpreter, without IDE's second-guessing you. Anyway, in 2.7.6's interactive interpreter, I get: >>> a = '\x00\x00\x00\x\d+' ValueError: invalid \x escape >>> So it has nothing to do with re, and is simply the result of trying an invalid string literal. What string were you hoping to get? You mention you wanted to match digits at the end (end of what?). Perhaps you wanted a real backslash followed by the letter d. In that case, since you cannot use a raw string (see my first response paragraph), you need to double the backslash. >>> a = '\x00\x00\x00\\d+' >>> print a \d+ Your data is funny, too, since it almost looks like it might be a string representation of a Python list. But assuming you meant it exactly like it is, there is a funny control character following the nulls. > > How could I match the \x00\x00\x00\x11 portion of the string? > There are no digits in that portion of the string, so I'm not sure why you were earlier trying to match digits. Perhaps you meant you were trying to match the single control character x'11'. In that case, you'd want a = '\x00\x00\x00\x11' pchars = re.compile(a) But if you wanted to match an arbitrary character following the nulls, you'd want something different. I think you'd better supply several strings to match against, and show which ones you'd expect a match for. -- DaveA From juan0christian at gmail.com Sun Mar 29 03:16:05 2015 From: juan0christian at gmail.com (Juan C.) Date: Sun, 29 Mar 2015 01:16:05 +0000 Subject: [Tutor] Python OO In-Reply-To: References: Message-ID: Ok, so, let me try to express what I think is 'right' here according to what you said. My code structure needs to be something like that: pycinema - package: pycinema - - __init__.py - - api.py - - actor.py - - movie.py - - serie.py - __main__.py And why I use it this way? 1. You said that I need to separate the API methods and calls from my code, so that if I need to move to another API or something like that, all the API code and logic will be in one place 2. I've read in many places that a basic structure, package with all the logic and a single __main__.py on the folder root, is good and simple. Let's start with something more simple, the actor.py module. Currently I already have this working code (http://pastebin.com/wcCnCwMc) for this module. How would be a good way to "convert" this API call to my api.py? Should I create an API class and have a bunch of methods like "get_actor", "get_movie", "get_serie", "get_season" and "get_episode"? In this code I'm only getting the name of the actor, but I can get other things from the API like birthday, deathday (when applicable), place_of_birth, and so on. Should my api.py get only the RAW data, in this case the JSON, and give it to my classes (Actor, Movie, Serie) and they (the classes) read this JSON and do what they need? I'll be finishing this message here so as to not make it so long. But I still have some "code design" doubts. But let's resolve it in parts. From breamoreboy at yahoo.co.uk Sun Mar 29 03:25:29 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 29 Mar 2015 02:25:29 +0100 Subject: [Tutor] Python OO In-Reply-To: References: Message-ID: On 29/03/2015 02:16, Juan C. wrote: > Ok, so, let me try to express what I think is 'right' here according to > what you said. > > My code structure needs to be something like that: > > pycinema > - package: pycinema > - - __init__.py > - - api.py > - - actor.py > - - movie.py > - - serie.py > - __main__.py > > And why I use it this way? > > 1. You said that I need to separate the API methods and calls from my code, > so that if I need to move to another API or something like that, all the > API code and logic will be in one place > > 2. I've read in many places that a basic structure, package with all the > logic and a single __main__.py on the folder root, is good and simple. > If your classes are small put them in one source file, which is clearly simpler than your proposed structure. Why over-engineer something if there is no need to? -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From juan0christian at gmail.com Sun Mar 29 03:32:54 2015 From: juan0christian at gmail.com (Juan C.) Date: Sun, 29 Mar 2015 01:32:54 +0000 Subject: [Tutor] Python OO In-Reply-To: References: Message-ID: On Sat, Mar 28, 2015 at 10:26 PM Mark Lawrence wrote: If your classes are small put them in one source file, which is clearly simpler than your proposed structure. Why over-engineer something if there is no need to? Well, my classes won't be that small, and separating them by modules would be easier to maintain, and after all, it's only 4 modules, not 10-15. My actor related methods should be set apart from my movie, serie related ones, don't you agree? Having all the classes (Actor, Movie, Serie) in one module would make it 200, maybe 300 lines long. From robertvstepp at gmail.com Sun Mar 29 07:34:09 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 29 Mar 2015 00:34:09 -0500 Subject: [Tutor] Why does print(a_list.sort()) return None? Message-ID: I am puzzled by the following: Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 64 bit (AMD64)] on win32 Type "copyright", "credits" or "license()" for more information. >>> a_list = [5, 0, 2, 4, 1] >>> print(a_list.sort()) None >>> print(a_list) [0, 1, 2, 4, 5] >>> I expected the first print statement to return what the second one did. Apparently the first print printed a_list, then did the sort. Why is this so? -- boB From cs at zip.com.au Sun Mar 29 08:00:46 2015 From: cs at zip.com.au (Cameron Simpson) Date: Sun, 29 Mar 2015 17:00:46 +1100 Subject: [Tutor] Why does print(a_list.sort()) return None? In-Reply-To: References: Message-ID: <20150329060046.GA59262@cskk.homeip.net> On 29Mar2015 00:34, boB Stepp wrote: >I am puzzled by the following: > >Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v.1600 >64 bit (AMD64)] on win32 >Type "copyright", "credits" or "license()" for more information. >>>> a_list = [5, 0, 2, 4, 1] >>>> print(a_list.sort()) >None >>>> print(a_list) >[0, 1, 2, 4, 5] >>>> > >I expected the first print statement to return what the second one >did. Apparently the first print printed a_list, then did the sort. Why >is this so? Both prints are as expected: they are printing the value of the expressions inside the parentheses. So: taking the latter print first: print(a_list) it is printing a_list, as you might expect after the sort. The former print: print(a_list.sort()) is printing the result of "a_list.sort()". Like most Python functions that operate on something (i.e. .sort, which sorts the list in place), the .sort method returns None. And that is printed. Try running the expressions themselves, discarding the "print". In the interactive interpreter, each expression you type in has its results printed unless the result is None. So, in Python 3.4.3 here: Python 3.4.3 (default, Mar 10 2015, 14:53:35) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> a_list = [5, 0, 2, 4, 1] >>> a_list.sort() >>> a_list [0, 1, 2, 4, 5] >>> The assignment has no return value. The .sort() call returns None, so the interpreter prints nothing. The expression "a_list" is of course the list, which is printed. Just to be glaingly obvious about this aspect of the interactive interpreter: >>> x=1 >>> x 1 >>> x=None >>> x >>> Cheers, Cameron Simpson You are just paranoid, and all your friends think so too. - James Joseph Dominguez From robertvstepp at gmail.com Sun Mar 29 08:24:43 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 29 Mar 2015 01:24:43 -0500 Subject: [Tutor] Why does print(a_list.sort()) return None? In-Reply-To: <20150329060046.GA59262@cskk.homeip.net> References: <20150329060046.GA59262@cskk.homeip.net> Message-ID: On Sun, Mar 29, 2015 at 1:00 AM, Cameron Simpson wrote: > The former print: > > print(a_list.sort()) > > is printing the result of "a_list.sort()". > > Like most Python functions that operate on something (i.e. .sort, which > sorts the list in place), the .sort method returns None. And that is > printed. Ah! I did not realize this. That makes sense now. Many thanks, Cameron! boB From ben+python at benfinney.id.au Sun Mar 29 08:55:53 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Sun, 29 Mar 2015 17:55:53 +1100 Subject: [Tutor] Python OO References: Message-ID: <85y4mg47ie.fsf@benfinney.id.au> "Juan C." writes: > My actor related methods should be set apart from my movie, serie > related ones, don't you agree? As a side issue: You apparently intend to choose names that are English language. If that's true, you should know that ?actor?, ?movie?, ?series? are all singular. There is not English word ?serie?, the word ?series? is singular and plural. Just as the word ?species? is singular and plural. > Having all the classes (Actor, Movie, Serie) in one module would make > it 200, maybe 300 lines long. So, those classes should be named ?Actor?, ?Movie?, ?Series?. -- \ ?I thought I'd begin by reading a poem by Shakespeare, but then | `\ I thought ?Why should I? He never reads any of mine.?? ?Spike | _o__) Milligan | Ben Finney From alan.gauld at btinternet.com Sun Mar 29 09:53:30 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 29 Mar 2015 08:53:30 +0100 Subject: [Tutor] Why does print(a_list.sort()) return None? In-Reply-To: <20150329060046.GA59262@cskk.homeip.net> References: <20150329060046.GA59262@cskk.homeip.net> Message-ID: On 29/03/15 07:00, Cameron Simpson wrote: > print(a_list.sort()) > > is printing the result of "a_list.sort()". > > Like most Python functions that operate on something (i.e. .sort, which > sorts the list in place), the .sort method returns None. And that is > printed. But you can use the sorted() function which returns a sorted copy of the list. So replace your print statement with print(sorted(a_list)) gets you the display you want. But does not sort the original. So it depends on whether you just want to display it, or actually want to sort it. Use either: a_list.sort() print(a_list) OR print(sorted(a_list)) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From duxbuz at hotmail.com Sun Mar 29 09:55:01 2015 From: duxbuz at hotmail.com (Ian D) Date: Sun, 29 Mar 2015 07:55:01 +0000 Subject: [Tutor] escape character regex In-Reply-To: <55174575.4010104@davea.name> References: , <55174575.4010104@davea.name> Message-ID: Ha ha thanks Danny for the hex message! I am looking to basically match 2 unknown hex values or a byte at the end of the 4 byte sequence. I realise now I am trying to use a numeric \d expression when it needs to be matching 2 nibbles or a byte. Is there a way to match using some sort of wildcard for the last byte as it changes? Thanks ---------------------------------------- > Date: Sat, 28 Mar 2015 20:21:09 -0400 > From: davea at davea.name > To: tutor at python.org > Subject: Re: [Tutor] escape character regex > > On 03/28/2015 03:37 PM, Ian D wrote: >> Hi >> >> >> I run a regex like this: >> >>> pchars = re.compile('\x00\x00\x00') #with or without 'r' for raw > > Which one did you actually want? The 3 byte sequence consisting of > nulls, or the 12 byte one containing zeroes and backslashes? I'm going > to assume the former, in which case you cannot use 'r' for raw. Unless > you've got a null key on your keyboard. > >> >> on a string like this: >> >>> data = "['broadcast', 'd8on\x00\x00\x00\x11broadcast', 'd11on']" >> >>> print "found pchars :",pchars.findall(data) >> >> which returns: >> >>> found pchars : ['\x00\x00\x00'] >> >> >> But if I try to match the extra digits at the end like this: >> >>> pchars = re.compile('\x00\x00\x00\x\d+') >> >> I get an error: >> >>> ValueError: invalid \x escape > > The \x escape sequence must be followed by exactly two hex digits, and > forms a single byte from them. What did you want that byte to be, and > why didn't you specify it? > >> >> Or if I use another ide than idle it actually flags it as an "illegal hexadecimal escape sequence" >> > > The question is not what the various IDE's produce, but what the Python > compiler produces. So once you started getting errors, you really > should have just run it in the interactive interpreter, without IDE's > second-guessing you. Anyway, in 2.7.6's interactive interpreter, I get: > >>>> a = '\x00\x00\x00\x\d+' > ValueError: invalid \x escape >>>> > > So it has nothing to do with re, and is simply the result of trying an > invalid string literal. > > What string were you hoping to get? You mention you wanted to match > digits at the end (end of what?). Perhaps you wanted a real backslash > followed by the letter d. In that case, since you cannot use a raw > string (see my first response paragraph), you need to double the backslash. > >>>> a = '\x00\x00\x00\\d+' >>>> print a > \d+ > > > Your data is funny, too, since it almost looks like it might be a string > representation of a Python list. But assuming you meant it exactly like > it is, there is a funny control character following the nulls. >> >> How could I match the \x00\x00\x00\x11 portion of the string? >> > > There are no digits in that portion of the string, so I'm not sure why > you were earlier trying to match digits. > > Perhaps you meant you were trying to match the single control character > x'11'. In that case, you'd want > > a = '\x00\x00\x00\x11' > pchars = re.compile(a) > > > But if you wanted to match an arbitrary character following the nulls, > you'd want something different. > > I think you'd better supply several strings to match against, and show > which ones you'd expect a match for. > > -- > DaveA > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From alan.gauld at btinternet.com Sun Mar 29 10:04:04 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Sun, 29 Mar 2015 09:04:04 +0100 Subject: [Tutor] Python OO In-Reply-To: References: Message-ID: On 29/03/15 02:16, Juan C. wrote: > Ok, so, let me try to express what I think is 'right' here according to > what you said. > > My code structure needs to be something like that: > > pycinema > - package: pycinema > - - __init__.py > - - api.py > - - actor.py > - - movie.py > - - serie.py > - __main__.py > What I said only relates to serie.py, You have just introduced a whole bunch of new concepts that we haven't discussed so far. > 1. You said that I need to separate the API methods and calls from my code, > so that if I need to move to another API or something like that, all the > API code and logic will be in one place That's one way, but what I really said was that you should separate the MDB API from the one you expose to your users (eg. your app). One way to do that is wrap the MDB API in a class of its own class MDB, say? You can then subclass or replace the instance of that class if you change or use another MDB service later. > 2. I've read in many places that a basic structure, package with all the > logic and a single __main__.py on the folder root, is good and simple. This is good for building a reusable module. This is different from building an actual app. The app code could have a different structure, and just import the modules or package you define above. > Let's start with something more simple, the actor.py module. Currently I > already have this working code (http://pastebin.com/wcCnCwMc) for this > module. > > How would be a good way to "convert" this API call to my api.py? > > Should I create an API class and have a bunch of methods like "get_actor", > "get_movie", "get_serie", "get_season" and "get_episode"? Yes, probably. Express the API in generic terms so that if you were using another one the same generic requests would apply. Do NOT call the API class API! Call it something that reflects what the API represents. In this case a Movie DB... > In this code I'm only getting the name of the actor, but I can get other > things from the API like birthday, deathday (when applicable), > place_of_birth, and so on. > > Should my api.py get only the RAW data, in this case the JSON, and give it > to my classes (Actor, Movie, Serie) and they (the classes) read this JSON > and do what they need? No, the data representation is part of the API. Your API class should return native Python code that your classes can use. Then if you use a new API that returns XML, or CSV, or even uses a local (SQL?) database, you can replicate the API methods in a new class but the returned data looks the same. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From duxbuz at hotmail.com Sun Mar 29 10:32:55 2015 From: duxbuz at hotmail.com (Ian D) Date: Sun, 29 Mar 2015 08:32:55 +0000 Subject: [Tutor] escape character regex In-Reply-To: References: , , <55174575.4010104@davea.name>, Message-ID: Ok I got it. pchars = re.compile(b'\x00\x00\x00[\0-\xff]') preceeding b and [0-\xff] ---------------------------------------- > From: duxbuz at hotmail.com > To: tutor at python.org > Date: Sun, 29 Mar 2015 07:55:01 +0000 > Subject: Re: [Tutor] escape character regex > > Ha ha thanks Danny for the hex message! > > > I am looking to basically match 2 unknown hex values or a byte at the end of the 4 byte sequence. > > I realise now I am trying to use a numeric \d expression when it needs to be matching 2 nibbles or a byte. > > > Is there a way to match using some sort of wildcard for the last byte as it changes? > > > Thanks > > ---------------------------------------- >> Date: Sat, 28 Mar 2015 20:21:09 -0400 >> From: davea at davea.name >> To: tutor at python.org >> Subject: Re: [Tutor] escape character regex >> >> On 03/28/2015 03:37 PM, Ian D wrote: >>> Hi >>> >>> >>> I run a regex like this: >>> >>>> pchars = re.compile('\x00\x00\x00') &with or without 'r' for raw >> >> Which one did you actually want? The 3 byte sequence consisting of >> nulls, or the 12 byte one containing zeroes and backslashes? I'm going >> to assume the former, in which case you cannot use 'r' for raw. Unless >> you've got a null key on your keyboard. >> >>> >>> on a string like this: >>> >>>> data = "['broadcast', 'd8on\x00\x00\x00\x11broadcast', 'd11on']" >>> >>>> print "found pchars :",pchars.findall(data) >>> >>> which returns: >>> >>>> found pchars : ['\x00\x00\x00'] >>> >>> >>> But if I try to match the extra digits at the end like this: >>> >>>> pchars = re.compile('\x00\x00\x00\x\d+') >>> >>> I get an error: >>> >>>> ValueError: invalid \x escape >> >> The \x escape sequence must be followed by exactly two hex digits, and >> forms a single byte from them. What did you want that byte to be, and >> why didn't you specify it? >> >>> >>> Or if I use another ide than idle it actually flags it as an "illegal hexadecimal escape sequence" >>> >> >> The question is not what the various IDE's produce, but what the Python >> compiler produces. So once you started getting errors, you really >> should have just run it in the interactive interpreter, without IDE's >> second-guessing you. Anyway, in 2.7.6's interactive interpreter, I get: >> >>>>> a = '\x00\x00\x00\x\d+' >> ValueError: invalid \x escape >>>>> >> >> So it has nothing to do with re, and is simply the result of trying an >> invalid string literal. >> >> What string were you hoping to get? You mention you wanted to match >> digits at the end (end of what?). Perhaps you wanted a real backslash >> followed by the letter d. In that case, since you cannot use a raw >> string (see my first response paragraph), you need to double the backslash. >> >>>>> a = '\x00\x00\x00\\d+' >>>>> print a >> \d+ >> >> >> Your data is funny, too, since it almost looks like it might be a string >> representation of a Python list. But assuming you meant it exactly like >> it is, there is a funny control character following the nulls. >>> >>> How could I match the \x00\x00\x00\x11 portion of the string? >>> >> >> There are no digits in that portion of the string, so I'm not sure why >> you were earlier trying to match digits. >> >> Perhaps you meant you were trying to match the single control character >> x'11'. In that case, you'd want >> >> a = '\x00\x00\x00\x11' >> pchars = re.compile(a) >> >> >> But if you wanted to match an arbitrary character following the nulls, >> you'd want something different. >> >> I think you'd better supply several strings to match against, and show >> which ones you'd expect a match for. >> >> -- >> DaveA >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From __peter__ at web.de Sun Mar 29 11:02:50 2015 From: __peter__ at web.de (Peter Otten) Date: Sun, 29 Mar 2015 11:02:50 +0200 Subject: [Tutor] escape character regex References: <55174575.4010104@davea.name> Message-ID: Ian D wrote: > Ok I got it. > > pchars = re.compile(b'\x00\x00\x00[\0-\xff]') > > preceeding b and [0-\xff] Also possible: re.compile(b"\0\0\0.", re.DOTALL) . by default means "any byte except \n", the re.DOTALL flag changes that to "any byte". or re.compile(b"\0{3}.", re.DOTALL) {3} means "repeat 3 times". From davea at davea.name Sun Mar 29 15:32:31 2015 From: davea at davea.name (Dave Angel) Date: Sun, 29 Mar 2015 09:32:31 -0400 Subject: [Tutor] Python OO In-Reply-To: References: Message-ID: <5517FEEF.409@davea.name> On 03/28/2015 09:16 PM, Juan C. wrote: > Ok, so, let me try to express what I think is 'right' here according to > what you said. > > My code structure needs to be something like that: > > pycinema > - package: pycinema > - - __init__.py > - - api.py > - - actor.py > - - movie.py > - - serie.py > - __main__.py > I'd suggest that you NEVER call a module __main__.py The name "__main__" is reserved for identifying the script file, and is faked during the program initialization. By using that name for an imported file, you could get some very confusing errors later. -- DaveA From breamoreboy at yahoo.co.uk Sun Mar 29 16:30:37 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 29 Mar 2015 15:30:37 +0100 Subject: [Tutor] Python OO In-Reply-To: References: Message-ID: On 29/03/2015 02:32, Juan C. wrote: > On Sat, Mar 28, 2015 at 10:26 PM Mark Lawrence > wrote: > If your classes are small put them in one source file, which is clearly > simpler than your proposed structure. Why over-engineer something if > there is no need to? > > Well, my classes won't be that small, and separating them by modules would > be easier to maintain, and after all, it's only 4 modules, not 10-15. > > My actor related methods should be set apart from my movie, serie related > ones, don't you agree? No. > > Having all the classes (Actor, Movie, Serie) in one module would make it > 200, maybe 300 lines long. We're talking Python, not Java. Some modules run to 1000s of lines, a couple of hundred is just peanuts. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From __peter__ at web.de Sun Mar 29 16:42:33 2015 From: __peter__ at web.de (Peter Otten) Date: Sun, 29 Mar 2015 16:42:33 +0200 Subject: [Tutor] Python OO References: <5517FEEF.409@davea.name> Message-ID: Dave Angel wrote: > On 03/28/2015 09:16 PM, Juan C. wrote: >> Ok, so, let me try to express what I think is 'right' here according to >> what you said. >> >> My code structure needs to be something like that: >> >> pycinema >> - package: pycinema >> - - __init__.py >> - - api.py >> - - actor.py >> - - movie.py >> - - serie.py >> - __main__.py >> > > I'd suggest that you NEVER call a module __main__.py The name > "__main__" is reserved for identifying the script file, and is faked > during the program initialization. > > By using that name for an imported file, you could get some very > confusing errors later. No, the __main__.py module is used to invoke a package: $ mkdir mypackage $ echo 'print("Hi")' > mypackage/__main__.py $ python3 -m mypackage Hi In Python 2 you need an __init__.py module to run the above example: $ python -m mypackage /usr/bin/python: No module named mypackage $ touch mypackage/__init__.py $ python -m mypackage Hi See also (there should be better documentation, but I didn't manage to find it...) From robertvstepp at gmail.com Sun Mar 29 17:25:21 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 29 Mar 2015 10:25:21 -0500 Subject: [Tutor] Why does print(a_list.sort()) return None? In-Reply-To: References: <20150329060046.GA59262@cskk.homeip.net> Message-ID: On Sun, Mar 29, 2015 at 2:53 AM, Alan Gauld wrote: > On 29/03/15 07:00, Cameron Simpson wrote: > >> print(a_list.sort()) >> >> is printing the result of "a_list.sort()". >> >> Like most Python functions that operate on something (i.e. .sort, which >> sorts the list in place), the .sort method returns None. And that is >> printed. > > > But you can use the sorted() function which returns a > sorted copy of the list. So replace your print statement > with > > print(sorted(a_list)) > > gets you the display you want. But does not sort the original. > So it depends on whether you just want to display it, or > actually want to sort it. > Use either: > > a_list.sort() > print(a_list) > > OR > > print(sorted(a_list)) Thanks for chiming in on this, Alan. I had not noticed that sorted() was an available option. I had focused on available list methods. While it does not matter if my actual lists do or do not get sorted, my intent was to just have a sorted view of the list, so your suggestion works better here and uses one less line of code. Thanks! -- boB From breamoreboy at yahoo.co.uk Sun Mar 29 17:40:35 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 29 Mar 2015 16:40:35 +0100 Subject: [Tutor] Why does print(a_list.sort()) return None? In-Reply-To: References: <20150329060046.GA59262@cskk.homeip.net> Message-ID: On 29/03/2015 16:25, boB Stepp wrote: > On Sun, Mar 29, 2015 at 2:53 AM, Alan Gauld wrote: >> On 29/03/15 07:00, Cameron Simpson wrote: >> >>> print(a_list.sort()) >>> >>> is printing the result of "a_list.sort()". >>> >>> Like most Python functions that operate on something (i.e. .sort, which >>> sorts the list in place), the .sort method returns None. And that is >>> printed. >> >> >> But you can use the sorted() function which returns a >> sorted copy of the list. So replace your print statement >> with >> >> print(sorted(a_list)) >> >> gets you the display you want. But does not sort the original. >> So it depends on whether you just want to display it, or >> actually want to sort it. >> Use either: >> >> a_list.sort() >> print(a_list) >> >> OR >> >> print(sorted(a_list)) > > Thanks for chiming in on this, Alan. I had not noticed that sorted() > was an available option. I had focused on available list methods. > While it does not matter if my actual lists do or do not get sorted, > my intent was to just have a sorted view of the list, so your > suggestion works better here and uses one less line of code. Thanks! > > The practice of returning None is standard in Python for anything that works in place, so as a parallel see also the difference between reverse() and reversed() As for saving a line of code, I'd much rather write three or four lines that I can understand at a glance in six months time [or even tomorrow :) ] rather than a potentially obtuse one liner. Glad you've got this sorted -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From juan0christian at gmail.com Sun Mar 29 19:30:23 2015 From: juan0christian at gmail.com (Juan C.) Date: Sun, 29 Mar 2015 17:30:23 +0000 Subject: [Tutor] Python OO In-Reply-To: <85y4mg47ie.fsf@benfinney.id.au> References: <85y4mg47ie.fsf@benfinney.id.au> Message-ID: On Sun, Mar 29, 2015 at 3:56 AM Ben Finney wrote: As a side issue: You apparently intend to choose names that are English language. If that's true, you should know that ?actor?, ?movie?, ?series? are all singular. My bad, it's series indeed. On Sun, Mar 29, 2015 at 10:33 AM Dave Angel wrote: I'd suggest that you NEVER call a module __main__.py The name "__main__" is reserved for identifying the script file, and is faked during the program initialization. By using that name for an imported file, you could get some very confusing errors later. As Peter said, I'm using __main__.py so that I can invoke the the app like 'python pycinema' in the console. A bunch of people recommend this approach. On Sun, Mar 29, 2015 at 11:31 AM Mark Lawrence wrote: No. We're talking Python, not Java. Some modules run to 1000s of lines, a couple of hundred is just peanuts. Ok, so you recommend me to have only 2 modules? One for the MDB API and one for the app inside the pycinema package and then my __main__.py on the folder root? From breamoreboy at yahoo.co.uk Sun Mar 29 19:38:18 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Sun, 29 Mar 2015 18:38:18 +0100 Subject: [Tutor] Python OO In-Reply-To: References: <85y4mg47ie.fsf@benfinney.id.au> Message-ID: On 29/03/2015 18:30, Juan C. wrote: > On Sun, Mar 29, 2015 at 3:56 AM Ben Finney > wrote: > As a side issue: You apparently intend to choose names that are English > language. > > If that's true, you should know that ?actor?, ?movie?, ?series? are all > singular. > > My bad, it's series indeed. > > > On Sun, Mar 29, 2015 at 10:33 AM Dave Angel wrote: > I'd suggest that you NEVER call a module __main__.py The name > "__main__" is reserved for identifying the script file, and is faked > during the program initialization. > > By using that name for an imported file, you could get some very > confusing errors later. > > As Peter said, I'm using __main__.py so that I can invoke the the app like > 'python pycinema' in the console. A bunch of people recommend this approach. > > > On Sun, Mar 29, 2015 at 11:31 AM Mark Lawrence > wrote: > No. > > We're talking Python, not Java. Some modules run to 1000s of lines, a > couple of hundred is just peanuts. > > Ok, so you recommend me to have only 2 modules? One for the MDB API and one > for the app inside the pycinema package and then my __main__.py on the > folder root? If that is the best solution for your problem then yes. I believe it was Albert Einstein who said something like "Make it as simple as possible, but no simpler". -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From juan0christian at gmail.com Sun Mar 29 21:16:40 2015 From: juan0christian at gmail.com (Juan C.) Date: Sun, 29 Mar 2015 19:16:40 +0000 Subject: [Tutor] Python OO In-Reply-To: References: <85y4mg47ie.fsf@benfinney.id.au> Message-ID: Ok, applying what you guys said I have: - folder: pycinema - package: pycinema - - core.py - - mdb.py - __main__.py Current code on core.py: # -*- coding: utf-8 -*- from pycinema.mdb import MovieDB API = MovieDB('API KEY GOES HERE') class Actor: def __init__(self, actor_id): api = API.get_actor(actor_id) self.name = api['name'] self.biography = api['biography'] self.actorid = api['id'] class Movie: def __init__(self): pass class Series: def __init__(self): pass Current code on mdb.py: # -*- coding: utf-8 -*- import requests class MovieDB: def __init__(self, api_key): self.api_key = api_key def get_actor(self, actor_id): response = requests.get('http://api.themoviedb.org/3/person/' + str(actor_id) + '?api_key=' + str(self.api_key)).json() return {'name': response['name'], 'id': actor_id, 'biography': response['biography']} I think the get_actor return is a bit bad. What would be the best way to return the data? Should I use classes inside the MovieDB class (Actor, Movie, Series) and inside those classes have different methods to get each data, like name, birthday, bio, etc separately? Only 1 call to the API gives all this data, example ( http://private-anon-37abaab74-themoviedb.apiary-mock.com/3/person/678). If I have different methods to get each data separately, I would be calling this same URL many times, when the best approach would be only 1. I won't need to get the ID from the API, as the user must provide the ID when instantiating the Actor. If you can't read the source code, try here: http://pastebin.com/1DWjxQwH From alan.gauld at btinternet.com Mon Mar 30 01:54:33 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 30 Mar 2015 00:54:33 +0100 Subject: [Tutor] Python OO In-Reply-To: References: <85y4mg47ie.fsf@benfinney.id.au> Message-ID: On 29/03/15 20:16, Juan C. wrote: > Current code on core.py: > > from pycinema.mdb import MovieDB > API = MovieDB('API KEY GOES HERE') > > class Actor: ... > Current code on mdb.py: > import requests > > class MovieDB: > ... > def get_actor(self, actor_id): > response = requests.get('http://api.themoviedb.org/3/person/'... > return {'name': response['name'], 'id': actor_id, 'biography': > response['biography']} > > I think the get_actor return is a bit bad. What would be the best way to > return the data? As an instance of your actor class would seem reasonable. If I ask for an actor I expect to get an actor back, not a dictionary. > Should I use classes inside the MovieDB class (Actor, Movie, Series) and > inside those classes have different methods to get each data, like name, > birthday, bio, etc separately? No, The API should just be a wrapper around the API you are using. It should return the Actor, Movie, etc objects that your application uses. The whole point of the API is just to allow you to change to (or add) a different source if needed. (It also shields you from future changes to the API) It should act as a translator from data source to application object. Any new source can be wrapped in the same way so it returns the same core objects. > Only 1 call to the API gives all this data, example ( > http://private-anon-37abaab74-themoviedb.apiary-mock.com/3/person/678). If > I have different methods to get each data separately, I would be calling > this same URL many times, when the best approach would be only 1. So inside your API separate out the single data response into its different objects. Call the API in the API init method. Then have separate methods to get the actors etc which operate on that internal stored data. Your objective should be to hide the details of how the interface works from the application objects. [ Aside: If there is a lot of data returned you might even store it temporarily in a local data base (SQLite file maybe?) The other methods then translate into SQL queries into that database. Refreshing the API then involves fetching the latest data and updating the database. But that's probably only worthwhile if you are storing many hundreds or thousands of records.] > I won't need to get the ID from the API, as the user must provide the ID > when instantiating the Actor. Can you explain how that works? Does the user create their own random unique values? Do you use a source of unique keys? Or could the Actor init() maybe generate an ID for the user? But without the API knowing the ID, how does the correct data get selected? Surely there must be some kind of unique key for that to happen? -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From ahlusar.ahluwalia at gmail.com Sun Mar 29 13:43:05 2015 From: ahlusar.ahluwalia at gmail.com (Saran Ahluwalia) Date: Sun, 29 Mar 2015 07:43:05 -0400 Subject: [Tutor] Advice on Strategy for Attacking IO Program Message-ID: Hello: Here is what I am trying have my program do: ? Monitor a folder for files that are dropped throughout the day ? When a file is dropped in the folder the program should scan the file o IF all the contents in the file have the same length o THEN the file should be moved to a "success" folder and a text file written indicating the total number of records processed o IF the file is empty OR the contents are not all of the same length o THEN the file should be moved to a "failure" folder and a text file written indicating the cause for failure (for example: Empty file or line 100 was not the same length as the rest). Below are the functions that I have been experimenting with. I am not sure how to most efficiently create a functional program from each of these constituent parts. I could use decorators (sacrificing speed) or simply pass a function within another function. [code] import time import fnmatch import os import shutil #If you want to write to a file, and if it doesn't exist, do this: if not os.path.exists(filepath): f = open(filepath, 'w') #If you want to read a file, and if it exists, do the following: try: f = open(filepath) except IOError: print 'I will be moving this to the ' #Changing a directory to "/home/newdir" os.chdir("/home/newdir") def move(src, dest): shutil.move(src, dest) def fileinfo(file): filename = os.path.basename(file) rootdir = os.path.dirname(file) lastmod = time.ctime(os.path.getmtime(file)) creation = time.ctime(os.path.getctime(file)) filesize = os.path.getsize(file) print "%s**\t%s\t%s\t%s\t%s" % (rootdir, filename, lastmod, creation, filesize) searchdir = r'D:\Your\Directory\Root' matches = [] def search for root, dirnames, filenames in os.walk(searchdir): ## for filename in fnmatch.filter(filenames, '*.c'): for filename in filenames: ## matches.append(os.path.join(root, filename)) ##print matches fileinfo(os.path.join(root, filename)) def get_files(src_dir): # traverse root directory, and list directories as dirs and files as files for root, dirs, files in os.walk(src_dir): path = root.split('/') for file in files: process(os.path.join(root, file)) os.remove(os.path.join(root, file)) def del_dirs(src_dir): for dirpath, _, _ in os.walk(src_dir, topdown=False): # Listing the files if dirpath == src_dir: break try: os.rmdir(dirpath) except OSError as ex: print(ex) def main(): get_files(src_dir) del_dirs(src_dir) if __name__ == "__main__": main() [/code] From ahlusar.ahluwalia at gmail.com Sun Mar 29 13:45:33 2015 From: ahlusar.ahluwalia at gmail.com (Saran Ahluwalia) Date: Sun, 29 Mar 2015 07:45:33 -0400 Subject: [Tutor] Feedback on Script for Pandas DataFrame Written into XML Message-ID: Hello: I would appreciate your feedback on whether I correctly wrote my XML. I am exporting a DataFrame and writing into a XML file. I used the ElementTree library. The DataFrame has 11 rows and 8 columns (excluding the index column). #My schema assumption: # #[ #Some number row #Sample text #] # CODE: SELECT ALL document = ET.Element("list") def make_message(document, row): msg = ET.SubElement(document, "message") for field in row.index: field_element = ET.SubElement(msg, field) field_element.text = row[field] return msg def add_to_document(row): return make_message(document, row) #df.apply(add_to_document, axis=0) ---> if I were to import a DataFrame stored in the variable #"df", I would simply APPLY the add_to_document function and COMBINE this into a document ET.dump(document) Thank you, in advance for your help. From antonia.van.der.leeuw at hotmail.nl Sun Mar 22 21:18:22 2015 From: antonia.van.der.leeuw at hotmail.nl (Antonia van der Leeuw) Date: Sun, 22 Mar 2015 20:18:22 -0000 Subject: [Tutor] What is wrong with my code? Message-ID: Hehey! I'm learning python on a website called codecademy.com, where I made a program to decode binary numbers. I guess the site uses a different compiler, because on the site my code worked fine, but when I copied and pasted it into the Python IDLE (3.4.2) it didn't work! I'm really don't know what is wrong with my code, can anyone of you fine sirs help me? Meh code: number_input = input("What binary number do you want me to decode? ") def decoder(number): number_backwards = str(number)[::-1] # Because binary numbers go from right to left. result = 0 value = 1 br = False for n in number_backwards: if n != "1" and n != "0": print number, "is not a binary number" br = True break elif n == "1": result += value value += value if br == False: print "The binary number decoded is", result decoder(number_input) Thank you! ^^ From calvinthai44 at gmail.com Sun Mar 29 05:56:17 2015 From: calvinthai44 at gmail.com (Calvin Thai) Date: Sun, 29 Mar 2015 14:56:17 +1100 Subject: [Tutor] Functions not changing Message-ID: I'm currently making a pygame using IDLE and i'm having a minor issue with my functions not changing color. My text is all plain black and its bothering me. Is there a solution or can i make my program using another text editor? (I tried using notepad++ but i don't know how to run the program, it only lets me open html only?) From calvinthai44 at gmail.com Sun Mar 29 09:11:50 2015 From: calvinthai44 at gmail.com (Calvin Thai) Date: Sun, 29 Mar 2015 18:11:50 +1100 Subject: [Tutor] Functions not changing In-Reply-To: References: Message-ID: Found the solution On Sun, Mar 29, 2015 at 2:56 PM, Calvin Thai wrote: > I'm currently making a pygame using IDLE and i'm having a minor issue with > my functions not changing color. My text is all plain black and its > bothering me. Is there a solution or can i make my program using another > text editor? > (I tried using notepad++ but i don't know how to run the program, it only > lets me open html only?) > From cybervigilante at gmail.com Sun Mar 29 03:25:28 2015 From: cybervigilante at gmail.com (Jim Mooney) Date: Sat, 28 Mar 2015 18:25:28 -0700 Subject: [Tutor] lamda in list comp Message-ID: Shouldn't this give me a list of squares? [lambda x: x**2 for x in range(10)] Instead I'm getting ten of these (with different addresses) . at 0x01262A50>] -- Jim I can't think of a clever tagline today, so just imagine I said something clever. From george.bollen at gmail.com Sun Mar 29 00:04:44 2015 From: george.bollen at gmail.com (George Bollen) Date: Sun, 29 Mar 2015 11:04:44 +1200 Subject: [Tutor] script Message-ID: hi, i am new to python and i have not done programming before but i am learning along the way, just want you to help me on how i can write a script that accepts an input a measurement of a time interval in seconds and prints a sentence giving the equivalent of the given number of seconds in hours, minutes, and seconds. thanks. From pp8407 at gmail.com Fri Mar 27 01:42:05 2015 From: pp8407 at gmail.com (Priya Persaud) Date: Thu, 26 Mar 2015 19:42:05 -0500 Subject: [Tutor] if statement help Message-ID: Hello, I recently learned about if statements and math functions. For my class we are unit testing absolute values and have to make if statements so the tests pass, but we cannot use built in functions like (abs). How do I start it, I am so lost. Thank you From t.roll at sbcglobal.net Thu Mar 26 20:55:10 2015 From: t.roll at sbcglobal.net (Troll Worfolk) Date: Thu, 26 Mar 2015 19:55:10 +0000 (UTC) Subject: [Tutor] Starting the IDLE Message-ID: <1889705246.2369455.1427399710714.JavaMail.yahoo@mail.yahoo.com> ?I downloaded Python 3.4.3 today (twice) and I can't get the IDLE to start.? The Python in the command window works great but I can't figure how to start the IDLE.? When I installed python there are two icons,? 'PYTHON'? and "PYTHONW" . The first will opens Python in a command window and all is working.? The second does nothing.? I've downloaded python twice today and still no IDLE.? What am I doing wrong??? I'm working on Windows 7 pro (32 bit). Also this is true on a second Windows 7 pro (64bit)? computer I have. ?Troll Worfolk ph: 928.634.8016 e-mail: T.Roll at sbcglobal.net From breamoreboy at yahoo.co.uk Mon Mar 30 02:13:24 2015 From: breamoreboy at yahoo.co.uk (Mark Lawrence) Date: Mon, 30 Mar 2015 01:13:24 +0100 Subject: [Tutor] lamda in list comp In-Reply-To: References: Message-ID: On 29/03/2015 02:25, Jim Mooney wrote: > Shouldn't this give me a list of squares? > [lambda x: x**2 for x in range(10)] > > Instead I'm getting ten of these (with different addresses) > . at 0x01262A50>] > That's exactly what you've asked the code to do. What makes you think you need a lambda function? [x**2 for x in range(10)] should do the trick. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence From juan0christian at gmail.com Mon Mar 30 02:39:30 2015 From: juan0christian at gmail.com (Juan C.) Date: Mon, 30 Mar 2015 00:39:30 +0000 Subject: [Tutor] Python OO In-Reply-To: References: <85y4mg47ie.fsf@benfinney.id.au> Message-ID: On Sun, Mar 29, 2015 at 8:55 PM Alan Gauld wrote: Can you explain how that works? Does the user create their own random unique values? Do you use a source of unique keys? Or could the Actor init() maybe generate an ID for the user? But without the API knowing the ID, how does the correct data get selected? Surely there must be some kind of unique key for that to happen? The ID is set by the API, I don't have control over it, I can't modify it, and I don't really need to modify it. There is an option to search the API using the name, like so ( http://private-anon-37abaab74-themoviedb.apiary-mock.com/3/search/person?query=Will%20Smith ). Of course in the "user side" of the things I will ask for names, let's say. The person wants to know all the movies that Will Smith played in. Of course I won't ask the user for his ID (that is 287) because they wouldn't know, the user will simply type his name. And my code will call this part of the API to get the ID of the person, and then create a new Actor using 'actor = Actor(287)'. The same goes for Movie and Series related stuff, I just modify the "/person?" to "/movie?" or "/tv?". From dyoo at hashcollision.org Mon Mar 30 03:16:32 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 29 Mar 2015 18:16:32 -0700 Subject: [Tutor] lamda in list comp In-Reply-To: References: Message-ID: On Sat, Mar 28, 2015 at 6:25 PM, Jim Mooney wrote: > Shouldn't this give me a list of squares? > [lambda x: x**2 for x in range(10)] > > Instead I'm getting ten of these (with different addresses) > . at 0x01262A50>] Mark Lawrence's answer shows how to correct this. Let's look at the problem a little more closely. To do so, one thing we can do is rewrite to try removing the use of lambdas. Here's a fairly similar situation to what you had: ###################### def square(x): return x * x [square for x in range(10)] ###################### If you saw that last expression, you might notice a mistake: we haven't called the function on an argument! We wanted to say: [square(x) for x in range(10)] Once we understand this, let's go back to your original expression: [lambda x: x**2 for x in range(10)] If we wanted to revise this so that it applied the function, we can do that: [(lambda x: x**2)(x) for x in range(10)] If you're a functional programmer, Python's list comprehension syntax will take a slight re-adjustment. List comprehensions do sort-of the same sort of thing as a functional map, but since it's built-in syntax, it does a bit more, in that the "mapper" is an expression that directly involves the mapped values. From dyoo at hashcollision.org Mon Mar 30 03:27:22 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 29 Mar 2015 18:27:22 -0700 Subject: [Tutor] What is wrong with my code? In-Reply-To: References: Message-ID: On Fri, Jan 23, 2015 at 1:40 PM, Antonia van der Leeuw wrote: > Hehey! > > I'm learning python on a website called codecademy.com, where I made a > program to decode binary numbers. I guess the site uses a different > compiler, because on the site my code worked fine, but when I copied and > pasted it into the Python IDLE (3.4.2) it didn't work! I'm really don't know > what is wrong with my code, can anyone of you fine sirs help me? Hi Antonia, Python 3 is a different language than Python 2. It looks like the codeacademy materials use Python 2, so you should probably do the same on your local system. By the way, next time you ask a question where something goes wrong, also show us what went wrong. Be descriptive! When you learn to program, you'll find that programs break for all sorts of crazy pedantic reasons. Normally, a Python program will explain *why* it broke. In your specific situation, it should have said something that points to one of the lines of your program, with a particular error message. When you see this and ask for help, copy and paste the error message. It will help. Good luck to you! From dyoo at hashcollision.org Mon Mar 30 03:32:17 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 29 Mar 2015 18:32:17 -0700 Subject: [Tutor] script In-Reply-To: References: Message-ID: On Sat, Mar 28, 2015 at 4:04 PM, George Bollen wrote: > hi, i am new to python and i have not done programming before but i am > learning along the way, > > just want you to help me on how i can write a script that accepts an input > a measurement of a time interval in > seconds and prints a sentence giving the equivalent of the given number of > seconds > in hours, minutes, and seconds. Given this is homework, we can't answer your question by just spitting out a program. Rather, we'll try to point you toward resources and give suggestions. Can you show us what you've tried? Can you point at any programs you've seen so far? Maybe some program that you're seen might be similar in concept to the problem you're trying to solve. Also, at a high level of problem solving, it might be useful to look at George Polya's "How to Solve It" in your local library. There are general techniques that people use to solve problems, and they need to be better known. Not sure if you've encountered material that shows these strategies, but you might want to look at them at some point. Please make sure to keep replies directed at the mailing list. Good luck to you. From dyoo at hashcollision.org Mon Mar 30 03:39:25 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 29 Mar 2015 18:39:25 -0700 Subject: [Tutor] if statement help In-Reply-To: References: Message-ID: On Thu, Mar 26, 2015 at 5:42 PM, Priya Persaud wrote: > Hello, > > I recently learned about if statements and math functions. For my class we > are unit testing absolute values and have to make if statements so the > tests pass, but we cannot use built in functions like (abs). How do I start > it, I am so lost. > > Thank you There are multiple situations where we need to teach the computer to compute a function, where it doesn't know about the function already. Your teacher appears to be trying to teach you how to build basic functions, which is admirable. Pretend that you're talking to someone who doesn't know what the absolute value function does. * Can you say what the absolute value function does? What's its purpose? * Can you give a few examples of inputs and expected outputs of the function? By the way, if you have some free time, you might find the following book helpful: http://www.ccs.neu.edu/home/matthias/HtDP2e/ It's not Python, but it tries to teach how to tackle the kind of programming problems you'll see in a beginner's class. What's good about it is that it concentrates on methodology and problem-solving strategies, all to keep yourself from getting lost. From ben+python at benfinney.id.au Mon Mar 30 03:45:31 2015 From: ben+python at benfinney.id.au (Ben Finney) Date: Mon, 30 Mar 2015 12:45:31 +1100 Subject: [Tutor] What is wrong with my code? References: Message-ID: <85h9t345s4.fsf@benfinney.id.au> Danny Yoo writes: > Python 3 is a different language than Python 2. It looks like the > codeacademy materials use Python 2, so you should probably do the same > on your local system. Alternatively, learn Python 3 (which at this time means learning somewhere other than Codecademy). Python 2 is only ever going to be the past, don't learn it until you need to. Learning Python 3 is essential to keep your knowledge relevant today, and I strongly recommending learning Python 3 *first*. -- \ ?The Vatican is not a state.? a state must have people. There | `\ are no Vaticanians.? No-one gets born in the Vatican except by | _o__) an unfortunate accident.? ?Geoffrey Robertson, 2010-09-18 | Ben Finney From dyoo at hashcollision.org Mon Mar 30 03:44:59 2015 From: dyoo at hashcollision.org (Danny Yoo) Date: Sun, 29 Mar 2015 18:44:59 -0700 Subject: [Tutor] Starting the IDLE In-Reply-To: <1889705246.2369455.1427399710714.JavaMail.yahoo@mail.yahoo.com> References: <1889705246.2369455.1427399710714.JavaMail.yahoo@mail.yahoo.com> Message-ID: On Thu, Mar 26, 2015 at 12:55 PM, Troll Worfolk wrote: > I downloaded Python 3.4.3 today (twice) and I can't get the IDLE to start. Did you run the installer afterwards? What menu items do you see in your Windows "Start Menu" that are associated to "Python 3.4"? From badouglas at gmail.com Mon Mar 30 03:49:23 2015 From: badouglas at gmail.com (bruce) Date: Sun, 29 Mar 2015 21:49:23 -0400 Subject: [Tutor] trying to convert pycurl/html to ascii Message-ID: Hi. Doing a quick/basic pycurl test on a site and trying to convert the returned page to pure ascii. The page has the encoding line The test uses pycurl, and the StringIO to fetch the page into a str. pycurl stuff . . . foo=gg.getBuffer() -at this point, foo has the page in a str buffer. What's happening, is that the test is getting the following kind of error/ UnicodeDecodeError: 'utf8' codec can't decode byte 0xa0 in position 20: invalid start byte The test is using python 2.6 on redhat. I've tried different decode functions based on different sites/articles/stackoverflow but can't quite seem to resolve the issue. Any thoughts/pointers would be useful! Thanks From juan0christian at gmail.com Mon Mar 30 03:57:15 2015 From: juan0christian at gmail.com (Juan C.) Date: Mon, 30 Mar 2015 01:57:15 +0000 Subject: [Tutor] Python OO In-Reply-To: References: <85y4mg47ie.fsf@benfinney.id.au> Message-ID: On Sun, Mar 29, 2015 at 9:39 PM Juan C. wrote: The ID is set by the API, I don't have control over it, I can't modify it, and I don't really need to modify it. There is an option to search the API using the name, like so ( http://private-anon-37abaab74-themoviedb.apiary-mock.com/3/search/person?query=Will%20Smith ). Of course in the "user side" of the things I will ask for names, let's say. The person wants to know all the movies that Will Smith played in. Of course I won't ask the user for his ID (that is 287) because they wouldn't know, the user will simply type his name. And my code will call this part of the API to get the ID of the person, and then create a new Actor using 'actor = Actor(287)'. The same goes for Movie and Series related stuff, I just modify the "/person?" to "/movie?" or "/tv?". Oh, and yes, my bad, indeed, the user will provide the name only, my script will get the ID. From davea at davea.name Mon Mar 30 03:59:08 2015 From: davea at davea.name (Dave Angel) Date: Sun, 29 Mar 2015 21:59:08 -0400 Subject: [Tutor] What is wrong with my code? In-Reply-To: References: Message-ID: <5518ADEC.6010006@davea.name> On 01/23/2015 04:40 PM, Antonia van der Leeuw wrote: > Hehey! > > I'm learning python on a website called codecademy.com, where I made a > program to decode binary numbers. I guess the site uses a different > compiler, because on the site my code worked fine, but when I copied and > pasted it into the Python IDLE (3.4.2) it didn't work! When asking a question here, it's really more useful to say in what way it didn't work. Like if you crashed with an exception, show the stack trace including the error. Still, it's a pretty safe guess that you got an exception on the print statement(s), which is a function in Python 3.x. I'm really don't know > what is wrong with my code, can anyone of you fine sirs help me? > > > > Meh code: > > > > number_input = input("What binary number do you want me to decode? ") > > > > def decoder(number): > > number_backwards = str(number)[::-1] # Because binary numbers go from > right to left. > > result = 0 > > value = 1 > > br = False > > for n in number_backwards: > > if n != "1" and n != "0": > > print number, "is not a binary number" > print(number, "is not a binary number") > br = True > > break > > elif n == "1": > > result += value > > value += value > > if br == False: > > print "The binary number decoded is", result > print("The binary number decoded is", result) > > > decoder(number_input) > >-- DaveA From davea at davea.name Mon Mar 30 04:08:18 2015 From: davea at davea.name (Dave Angel) Date: Sun, 29 Mar 2015 22:08:18 -0400 Subject: [Tutor] trying to convert pycurl/html to ascii In-Reply-To: References: Message-ID: <5518B012.2090404@davea.name> On 03/29/2015 09:49 PM, bruce wrote: > Hi. > > Doing a quick/basic pycurl test on a site and trying to convert the > returned page to pure ascii. You cannot convert it to pure ASCII. You could replace all the invalid characters with some special one, like question marks. But I doubt if that's what you really want. > > The page has the encoding line > > That would mean you should use 8859 in your decode. > > The test uses pycurl, and the StringIO to fetch the page into a str. > > pycurl stuff > . > . > . > foo=gg.getBuffer() > > -at this point, foo has the page in a str buffer. > > > What's happening, is that the test is getting the following kind of error/ > > UnicodeDecodeError: 'utf8' codec can't decode byte 0xa0 in position 20: > invalid start byte That's not the whole error. You need to show the whole stack trace, not just a single line. It would also be really useful if you showed the lines between the foo= line and the one that gets the error. > > The test is using python 2.6 on redhat. > Very good to tell us that. It makes a huge difference. > I've tried different decode functions based on different > sites/articles/stackoverflow but can't quite seem to resolve the issue. > Pick one, show us the code, and show us the full error traceback, and somebody can help. As it stands all I can tell us is a decode takes a byte string and an encoding name, and produces a unicode object. And it's not going to give you a utf-8 error if you're trying to decode 8859. -- DaveA From cs at zip.com.au Mon Mar 30 04:06:44 2015 From: cs at zip.com.au (Cameron Simpson) Date: Mon, 30 Mar 2015 13:06:44 +1100 Subject: [Tutor] trying to convert pycurl/html to ascii In-Reply-To: References: Message-ID: <20150330020644.GA22709@cskk.homeip.net> On 29Mar2015 21:49, bruce wrote: >Doing a quick/basic pycurl test on a site and trying to convert the >returned page to pure ascii. And if the page cannot be representing in ASCII? >The page has the encoding line > >The test uses pycurl, and the StringIO to fetch the page into a str. Which StringIO? StringIO.StringIO or io.StringIO? In Python 2 the format is effectively bytes (python 2 str) and the latter is unicode (as it is in python 3). >pycurl stuff >foo=gg.getBuffer() >-at this point, foo has the page in a str buffer. >What's happening, is that the test is getting the following kind of error/ >UnicodeDecodeError: 'utf8' codec can't decode byte 0xa0 in position 20: >invalid start byte Please show us more of the code, preferrably a complete example as small as possible to reproduce the exception. We have no idea what "gg" is or how it was obtained. >The test is using python 2.6 on redhat. >I've tried different decode functions based on different >sites/articles/stackoverflow but can't quite seem to resolve the issue. Flailing about on stackoverflow sounds a bit random. Have you consulted the PycURL documentation, especially this page: http://pycurl.sourceforge.net/doc/unicode.html which looks like it ought to discuss your problem. Cheers, Cameron Simpson From martin at linux-ip.net Mon Mar 30 04:25:30 2015 From: martin at linux-ip.net (Martin A. Brown) Date: Sun, 29 Mar 2015 19:25:30 -0700 Subject: [Tutor] Advice on Strategy for Attacking IO Program In-Reply-To: References: Message-ID: Greetings, > Here is what I am trying have my program do: > > ? Monitor a folder for files that are dropped throughout the day > > ? When a file is dropped in the folder the program should scan the file > > o IF all the contents in the file have the same length > > o THEN the file should be moved to a "success" folder and a text file > written indicating the total number of records processed > > o IF the file is empty OR the contents are not all of the same length > > o THEN the file should be moved to a "failure" folder and a text file > written indicating the cause for failure (for example: Empty file or line > 100 was not the same length as the rest). This is a very good description of the intent of the programmer. Thanks for including this. > Below are the functions that I have been experimenting with. I am > not sure how to most efficiently create a functional program Do you mean 'functional programming' [0] style as in Scheme, Haskell and/or ML? Or do you simply mean, 'functioning program'? > from each of these constituent parts. I could use decorators > (sacrificing speed) or simply pass a function within another > function. Given what you describe, I think your concern about sacrificing speed is worth forgetting (for now). Unless we are talking thousands of files per minute, I think you can sleep comfortably that decorators (which are, operationally, nothing more than another function call) are not going to slow down your program terribly. If you are (ever) worried about speed, then profile and understand where to place your effort. (I belong to the camp that searches for correctness first and then performance later.) Are you aware that you can define functions yourself? And, that you could have your function call one of the below functions? I will demonstrate by changing your 'fileinfo' function slightly. I snipped the rest of your code. > def fileinfo(file): > filename = os.path.basename(file) > rootdir = os.path.dirname(file) > lastmod = time.ctime(os.path.getmtime(file)) > creation = time.ctime(os.path.getctime(file)) > filesize = os.path.getsize(file) > > print "%s**\t%s\t%s\t%s\t%s" % (rootdir, filename, lastmod, creation, > filesize) Do you realize you can actually return the values to the caller rather than print them? This is an excelent way to pass information around a program. Without data to examine here, I can only guess based on your language that fixed records are in the input. If so, here's a slight revision to your program, which takes the function fileinfo as a starting point and demonstrates calling a function from within a function. I tested this little sample on a small set of files created with MD5 checksums. I wrote the Python in such a way as it would work with Python 2.x or 3.x (note the __future__ at the top). There are so many wonderful ways of failure, so you will probably spend a bit more time trying to determine which failure(s) you want to report to your user, and how. The only other comments I would make are about safe-file handling. #1: After a user has created a file that has failed (in processing),can the user create a file with the same name? If so, then you will probably want to look at some sort of file-naming strategy to avoid overwriting evidence of earlier failures. File naming is a tricky thing. See the tempfile module [1] and the Maildir naming scheme to see two different types of solutions to the problem of choosing a unique filename. Good luck with your foray, -Martin Code snippet: #! /usr/bin/python from __future__ import print_function import os import time RECORD_LENGTH = 32 def process_data(f, filesize): f.seek(0, os.SEEK_SET) counter = 0 # -- are the records text? I assume yes, though your data may differ. # if your definition of a record includes the newline, then # you will want to use len(record) ... # for record in f: print("record: %s" % ( record.strip()), file=sys.stderr) if RECORD_LENGTH == len(record.strip()): # -- len(record) counter += 1 else: return False, counter return True, counter def validate_and_process_data(f, filesize): f.seek(0, os.SEEK_END) lastbyte = f.tell() if lastbyte != filesize: return False, 0 else: return process_data(f, filesize) def validate_files(files): for file in files: filename, rootdir, lastmod, creation, filesize = fileinfo(file) f = open(filename) result, record_count = validate_and_process_data(f, filesize) if result: # -- Success! Write text file with count of records # recognized in record_count print('success with %s on %d records' % (filename, record_count,), file=sys.stderr) pass else: # -- Failure; need additional logic here to determine # the cause for failure. print('failure with %s after %d records' % (filename, record_count,), file=sys.stderr) pass def fileinfo(file): filename = os.path.basename(file) rootdir = os.path.dirname(file) lastmod = time.ctime(os.path.getmtime(file)) creation = time.ctime(os.path.getctime(file)) filesize = os.path.getsize(file) return filename, rootdir, lastmod, creation, filesize if __name__ == '__main__': import sys validate_files(sys.argv[1:]) # -- end of file [0] http://en.wikipedia.org/wiki/Functional_programming [1] https://docs.python.org/2/library/tempfile.html#module-tempfile -- Martin A. Brown http://linux-ip.net/ From martin at linux-ip.net Mon Mar 30 04:32:53 2015 From: martin at linux-ip.net (Martin A. Brown) Date: Sun, 29 Mar 2015 19:32:53 -0700 Subject: [Tutor] Feedback on Script for Pandas DataFrame Written into XML In-Reply-To: References: Message-ID: Good evening again, I'm replying to your second post, because I replied to the first. This may be a more specific request than is typically handled on Python tutor. This involves specific knowledge of the xml.etree.ElementTree and pandas.DataFrame objects. > I would appreciate your feedback on whether I correctly wrote my > XML. I am exporting a DataFrame and writing into a XML file. I > used the ElementTree library. The DataFrame has 11 rows and 8 > columns (excluding the index column). Side note: Hard to know or give any advice without considerably more detail on the data involved. But.... > #My schema assumption: > # > #[ > #Some number row > #Sample text > #] > # That shows 6 (XML) elements. This is neither 8 nor 11. > CODE: SELECT ALL > > document = ET.Element("list") > > def make_message(document, row): > msg = ET.SubElement(document, "message") > for field in row.index: > field_element = ET.SubElement(msg, field) > field_element.text = row[field] > return msg > > def add_to_document(row): > return make_message(document, row) > > #df.apply(add_to_document, axis=0) ---> if I were to import a DataFrame > stored in the variable > #"df", I would simply APPLY the add_to_document function and COMBINE this > into a document > > ET.dump(document) > > Thank you, in advance for your help. This is a more general inquiry and is probably better suited for the lxml (ElementTree) mailing list ... https://mailman-mail5.webfaction.com/listinfo/lxml ... or maybe the Pandas mailing list: https://groups.google.com/forum/#!forum/pydata Best of luck, -Martin -- Martin A. Brown http://linux-ip.net/ From alan.gauld at btinternet.com Mon Mar 30 10:55:42 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 30 Mar 2015 09:55:42 +0100 Subject: [Tutor] Starting the IDLE In-Reply-To: <1889705246.2369455.1427399710714.JavaMail.yahoo@mail.yahoo.com> References: <1889705246.2369455.1427399710714.JavaMail.yahoo@mail.yahoo.com> Message-ID: On 26/03/15 19:55, Troll Worfolk wrote: > I downloaded Python 3.4.3 today (twice) and I can't get the IDLE to start. ... > When I installed python there are two icons, 'PYTHON' and "PYTHONW" . Where did you get your Python version? If its the ActiveState version then it installs Pythonwin which is similar to IDLE but more Windows oriented. Do you have a Pythonwin icon? > The first will opens Python in a command window and all is working. > The second does nothing. Pythonw is a special program used to run GUI programs without showing the commandline window. You should very rarely use it, if ever. If all else fails you can find the IDLE file buried in the IDLE files and create a shortcut to your desktop or start menu. Its: PythonInstallDir\Lib\idlelib\idle.bat HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From martin at linux-ip.net Mon Mar 30 17:57:36 2015 From: martin at linux-ip.net (Martin A. Brown) Date: Mon, 30 Mar 2015 08:57:36 -0700 Subject: [Tutor] Feedback on Script for Pandas DataFrame Written into XML In-Reply-To: References: Message-ID: Good morning again, Saran, > I have attached a .html file (I would recommend downloading this > first and then opening the file), and my .py script. Here is the > data . > > My function (included in the prior message) and my schema is based > off of my interpretation that each row of the DataFrame is a > message and each field is within the message. > > Each column's index and its corresponding field is nested within > each message (for example "date"). I gave this hypothetical > example as one can see one of the columns includes a > data/timestamp of a correspondence. My question is as follows: > > 1. I this the correct translation/interpretation of the data set? I do not know. Who is the author of IntroToPython? Can you ask this person? > Or am I over thinking the schema and interpretation of the > DataFrame? For understanding and using a DataFrame, you'd probably be better off asking on the pandas mailing list. >> ... or maybe the Pandas mailing list: >> >> https://groups.google.com/forum/#!forum/pydata May the Python never release you from its grip! -Martin -- Martin A. Brown http://linux-ip.net/ From alan.gauld at btinternet.com Mon Mar 30 23:13:19 2015 From: alan.gauld at btinternet.com (Alan Gauld) Date: Mon, 30 Mar 2015 22:13:19 +0100 Subject: [Tutor] Starting the IDLE In-Reply-To: References: <1889705246.2369455.1427399710714.JavaMail.yahoo@mail.yahoo.com> Message-ID: On 30/03/15 09:55, Alan Gauld wrote: > If its the ActiveState version then it installs Pythonwin which is > similar to IDLE but more Windows oriented. Do you have a Pythonwin icon? By strange coincidence I had to reinstall Python on my Windows box today and it turns out that Activestate's V3.4 does install IDLE, not Pythonwin, in the menus. So I don't know what might be wrong. The path info is correct however... -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From ahlusar.ahluwalia at gmail.com Mon Mar 30 11:32:37 2015 From: ahlusar.ahluwalia at gmail.com (Saran Ahluwalia) Date: Mon, 30 Mar 2015 05:32:37 -0400 Subject: [Tutor] Feedback on Script for Pandas DataFrame Written into XML In-Reply-To: References: Message-ID: Good Morning Martin: Thank you for your feedback. I have attached a .html file (I would recommend downloading this first and then opening the file), and my .py script. Here is the data . My function (included in the prior message) and my schema is based off of my interpretation that each row of the DataFrame is a message and each field is within the message. Each column's index and its corresponding field is nested within each message (for example "date"). I gave this hypothetical example as one can see one of the columns includes a data/timestamp of a correspondence. My question is as follows: 1. I this the correct translation/interpretation of the data set? Or am I over thinking the schema and interpretation of the DataFrame? I welcome your thoughts and feedback. Sincerely, Saran On Sun, Mar 29, 2015 at 10:32 PM, Martin A. Brown wrote: > > Good evening again, > > I'm replying to your second post, because I replied to the first. This may > be a more specific request than is typically handled on Python tutor. This > involves specific knowledge of the xml.etree.ElementTree and > pandas.DataFrame objects. > > I would appreciate your feedback on whether I correctly wrote my XML. I >> am exporting a DataFrame and writing into a XML file. I used the >> ElementTree library. The DataFrame has 11 rows and 8 columns (excluding the >> index column). >> > > Side note: Hard to know or give any advice without considerably more > detail on the data involved. But.... > > #My schema assumption: >> # >> #[ >> #Some number row >> #Sample text >> #] >> # >> > > That shows 6 (XML) elements. This is neither 8 nor 11. > > CODE: SELECT ALL >> >> document = ET.Element("list") >> >> def make_message(document, row): >> msg = ET.SubElement(document, "message") >> for field in row.index: >> field_element = ET.SubElement(msg, field) >> field_element.text = row[field] >> return msg >> >> def add_to_document(row): >> return make_message(document, row) >> >> #df.apply(add_to_document, axis=0) ---> if I were to import a DataFrame >> stored in the variable >> #"df", I would simply APPLY the add_to_document function and COMBINE this >> into a document >> >> ET.dump(document) >> >> Thank you, in advance for your help. >> > > This is a more general inquiry and is probably better suited for the lxml > (ElementTree) mailing list ... > > https://mailman-mail5.webfaction.com/listinfo/lxml > > ... or maybe the Pandas mailing list: > > https://groups.google.com/forum/#!forum/pydata > > Best of luck, > > -Martin > > -- > Martin A. Brown > http://linux-ip.net/ > From duxbuz at hotmail.com Tue Mar 31 16:00:53 2015 From: duxbuz at hotmail.com (Ian D) Date: Tue, 31 Mar 2015 14:00:53 +0000 Subject: [Tutor] Dynamic naming of lists Message-ID: Hi I have a list that I am splitting into pairs of values. But the list is dynamic in size. It could have 4 values or 6 or more. I originally split the list into pairs, by using a new list and keep a pair in the old list by just popping 2 values. But if the list is longer than 4 values. I cannot do this. I can only envision I would need to dynamically create lists. How would I do this? while returned_list_of_items: for i in range(1): new_list.append(returned_list_of_items.pop(0)) #pop first value and append new_list.append(returned_list_of_items.pop(0)) #pop second value and append Thanks From davea at davea.name Tue Mar 31 16:30:04 2015 From: davea at davea.name (Dave Angel) Date: Tue, 31 Mar 2015 10:30:04 -0400 Subject: [Tutor] Dynamic naming of lists In-Reply-To: References: Message-ID: <551AAF6C.20003@davea.name> On 03/31/2015 10:00 AM, Ian D wrote: > Hi > > I have a list that I am splitting into pairs of values. But the list is dynamic in size. It could have 4 values or 6 or more. > > I originally split the list into pairs, by using a new list and keep a pair in the old list by just popping 2 values. But if the list is longer than 4 values. I cannot do this. I can only envision I would need to dynamically create lists. How would I do this? > > while returned_list_of_items: > for i in range(1): > new_list.append(returned_list_of_items.pop(0)) #pop first value and append > new_list.append(returned_list_of_items.pop(0)) #pop second value and append > It'd really be a lot clearer if you gave one or more examples of input and output data. Like you want list [1,2,3,4] to become [ (1,2), (3,4) ] I'll guess you want a list of two-tuples. It so happens there's a nice built-in for the purpose. https://docs.python.org/3/library/functions.html#zip >>> s = [1,2,3,4,5,6,7,8] >>> list(zip(*[iter(s)]*2)) [(1, 2), (3, 4), (5, 6), (7, 8)] -- DaveA From duxbuz at hotmail.com Tue Mar 31 17:35:37 2015 From: duxbuz at hotmail.com (Ian D) Date: Tue, 31 Mar 2015 15:35:37 +0000 Subject: [Tutor] Dynamic naming of lists In-Reply-To: <551AAF6C.20003@davea.name> References: , <551AAF6C.20003@davea.name> Message-ID: Thanks I will look into these. The data going in is a list like this:['broadcast', '"d8on"', 'broadcast', '"d11on"'] With the output beng something like this. lst_0 = ['broadcast', '"d8on"'] lst_0 = ['broadcast', '"d11on"'] I have managed to use a dictionary as advised in a post on StackOverflow; not quite completed it as I am overwriting my lists each time. From your experience is it better to pursue the dictionary route or the zip tuple option. I am in python2.7 ---------------------------------------- > Date: Tue, 31 Mar 2015 10:30:04 -0400 > From: davea at davea.name > To: tutor at python.org > Subject: Re: [Tutor] Dynamic naming of lists > > On 03/31/2015 10:00 AM, Ian D wrote: >> Hi >> >> I have a list that I am splitting into pairs of values. But the list is dynamic in size. It could have 4 values or 6 or more. >> >> I originally split the list into pairs, by using a new list and keep a pair in the old list by just popping 2 values. But if the list is longer than 4 values. I cannot do this. I can only envision I would need to dynamically create lists. How would I do this? >> >> while returned_list_of_items: >> for i in range(1): >> new_list.append(returned_list_of_items.pop(0)) #pop first value and append >> new_list.append(returned_list_of_items.pop(0)) #pop second value and append >> > > It'd really be a lot clearer if you gave one or more examples of input > and output data. Like you want list [1,2,3,4] to become [ (1,2), (3,4) ] > > I'll guess you want a list of two-tuples. It so happens there's a nice > built-in for the purpose. > > https://docs.python.org/3/library/functions.html#zip > >>>> s = [1,2,3,4,5,6,7,8] >>>> list(zip(*[iter(s)]*2)) > [(1, 2), (3, 4), (5, 6), (7, 8)] > > > > -- > DaveA > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From __peter__ at web.de Tue Mar 31 17:50:01 2015 From: __peter__ at web.de (Peter Otten) Date: Tue, 31 Mar 2015 17:50:01 +0200 Subject: [Tutor] Dynamic naming of lists References: <551AAF6C.20003@davea.name> Message-ID: Ian D wrote: > Thanks I will look into these. The data going in is a list like > this:['broadcast', '"d8on"', 'broadcast', '"d11on"'] > > With the output beng something like this. > > lst_0 = ['broadcast', '"d8on"'] > > lst_0 = ['broadcast', '"d11on"'] Is that a typo, did you mean lst_1 = ['broadcast', '"d11on"'] ? If so us a list. Complete example: >>> flat_pairs = ['broadcast', '"d8on"', 'broadcast', '"d11on"'] >>> it = iter(flat_pairs) >>> pairs = list(zip(it, it)) >>> pairs [('broadcast', '"d8on"'), ('broadcast', '"d11on"')] You can then access individual pairs by providing an index into the pairs list: >>> pairs[0] ('broadcast', '"d8on"') >>> pairs[1] ('broadcast', '"d11on"') > I have managed to use a dictionary as advised in a post on StackOverflow; > not quite completed it as I am overwriting my lists each time. From your > experience is it better to pursue the dictionary route or the zip tuple > option. I am in python2.7 A dictionary is typically used when the keys are not as regular as a sequence of integers, e. g. to map a user name to an email address or a word to a list of the positions where it occurs in a text. From duxbuz at hotmail.com Tue Mar 31 17:56:16 2015 From: duxbuz at hotmail.com (Ian D) Date: Tue, 31 Mar 2015 15:56:16 +0000 Subject: [Tutor] Dynamic naming of lists In-Reply-To: References: , <551AAF6C.20003@davea.name> , Message-ID: Ok Thanks a lot. And sadly not a typo, my bad logic overwriting values! ---------------------------------------- > To: tutor at python.org > From: __peter__ at web.de > Date: Tue, 31 Mar 2015 17:50:01 +0200 > Subject: Re: [Tutor] Dynamic naming of lists > > Ian D wrote: > >> Thanks I will look into these. The data going in is a list like >> this:['broadcast', '"d8on"', 'broadcast', '"d11on"'] >> >> With the output beng something like this. >> >> lst_0 = ['broadcast', '"d8on"'] >> >> lst_0 = ['broadcast', '"d11on"'] > > Is that a typo, did you mean > > lst_1 = ['broadcast', '"d11on"'] > > ? If so us a list. Complete example: > >>>> flat_pairs = ['broadcast', '"d8on"', 'broadcast', '"d11on"'] >>>> it = iter(flat_pairs) >>>> pairs = list(zip(it, it)) >>>> pairs > [('broadcast', '"d8on"'), ('broadcast', '"d11on"')] > > You can then access individual pairs by providing an index into the pairs > list: > >>>> pairs[0] > ('broadcast', '"d8on"') >>>> pairs[1] > ('broadcast', '"d11on"') > >> I have managed to use a dictionary as advised in a post on StackOverflow; >> not quite completed it as I am overwriting my lists each time. From your >> experience is it better to pursue the dictionary route or the zip tuple >> option. I am in python2.7 > > A dictionary is typically used when the keys are not as regular as a > sequence of integers, e. g. to map a user name to an email address or a word > to a list of the positions where it occurs in a text. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From robertvstepp at gmail.com Tue Mar 31 22:23:55 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 31 Mar 2015 15:23:55 -0500 Subject: [Tutor] Unexpected results using enumerate() and .split() Message-ID: The following behavior has me stumped: Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> L = ['#ROI:roi_0', '#TXT:text_0', '#1:one^two^three'] >>> for i, item in enumerate(L): subitems = item.split(':') if subitems[0] == '#ROI': print subitems[1] if subitems[0] == '#TXT': print subitems[1] if subitems[0] == '#1' or '#2': print subitems[1] roi_0 roi_0 text_0 text_0 one^two^three >>> My desired output was: roi_0 text_0 one^two^three Oh, wonderful founts of wisdom, where is my understanding lacking? BTW, I copied and pasted the above into my Gmail window, but it removed the indentation that was present in the interpreter. I added spaces manually to get it appear as it did in the interpreter. Anyone know why Gmail does that to my copy and paste? -- boB From zachary.ware+pytut at gmail.com Tue Mar 31 22:28:39 2015 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Tue, 31 Mar 2015 15:28:39 -0500 Subject: [Tutor] Unexpected results using enumerate() and .split() In-Reply-To: References: Message-ID: On Tue, Mar 31, 2015 at 3:23 PM, boB Stepp wrote: > The following behavior has me stumped: > > Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit > (Intel)] on win32 > Type "copyright", "credits" or "license()" for more information. >>>> L = ['#ROI:roi_0', '#TXT:text_0', '#1:one^two^three'] >>>> for i, item in enumerate(L): > subitems = item.split(':') > if subitems[0] == '#ROI': > print subitems[1] > if subitems[0] == '#TXT': > print subitems[1] > if subitems[0] == '#1' or '#2': Here's your problem: "#2" is always true. Try "if subitems[0] in ['#1', '#2']:" -- Zach From davea at davea.name Tue Mar 31 22:32:17 2015 From: davea at davea.name (Dave Angel) Date: Tue, 31 Mar 2015 16:32:17 -0400 Subject: [Tutor] Unexpected results using enumerate() and .split() In-Reply-To: References: Message-ID: <551B0451.8010302@davea.name> On 03/31/2015 04:23 PM, boB Stepp wrote: > The following behavior has me stumped: > > Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit > (Intel)] on win32 > Type "copyright", "credits" or "license()" for more information. >>>> L = ['#ROI:roi_0', '#TXT:text_0', '#1:one^two^three'] >>>> for i, item in enumerate(L): > subitems = item.split(':') > if subitems[0] == '#ROI': > print subitems[1] > if subitems[0] == '#TXT': > print subitems[1] > if subitems[0] == '#1' or '#2': I think what you meant here was: if subitems[0] == "#1" or subitems[0] == "#2": > print subitems[1] Study the first expression and see if you can figure out what the difference is. If it's not clear, then make a simpler program just to test a compound if, and we'll all talk about it. > > roi_0ail > roi_0 > text_0 > text_0 > one^two^three >>>> > > My desired output was: > > roi_0 > text_0 > one^two^three > > Oh, wonderful founts of wisdom, where is my understanding lacking? > > BTW, I copied and pasted the above into my Gmail window, but it > removed the indentation that was present in the interpreter. I added > spaces manually to get it appear as it did in the interpreter. Anyone > know why Gmail does that to my copy and paste? > Buggy, I guess. Why not use a program like Thunderbird, which is free and available on most PC operating systems? (Unfortunately, it's not on Android) -- DaveA From robertvstepp at gmail.com Tue Mar 31 22:37:34 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 31 Mar 2015 15:37:34 -0500 Subject: [Tutor] Unexpected results using enumerate() and .split() In-Reply-To: References: Message-ID: On Tue, Mar 31, 2015 at 3:28 PM, Zachary Ware wrote: > On Tue, Mar 31, 2015 at 3:23 PM, boB Stepp wrote: >> The following behavior has me stumped: >> >> Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit >> (Intel)] on win32 >> Type "copyright", "credits" or "license()" for more information. >>>>> L = ['#ROI:roi_0', '#TXT:text_0', '#1:one^two^three'] >>>>> for i, item in enumerate(L): >> subitems = item.split(':') >> if subitems[0] == '#ROI': >> print subitems[1] >> if subitems[0] == '#TXT': >> print subitems[1] >> if subitems[0] == '#1' or '#2': > > Here's your problem: "#2" is always true. Try "if subitems[0] in > ['#1', '#2']:" Thanks, Zach! About the time your reply arrived I was starting to realize that my '#1' or '#2' might not be doing what I thought. In the "Python Pocket Reference" I was just looking at: X or Y If X is false then Y; else X. I forgot that different languages have different interpretations of "or". -- boB From zachary.ware+pytut at gmail.com Tue Mar 31 22:42:47 2015 From: zachary.ware+pytut at gmail.com (Zachary Ware) Date: Tue, 31 Mar 2015 15:42:47 -0500 Subject: [Tutor] Unexpected results using enumerate() and .split() In-Reply-To: References: Message-ID: On Tue, Mar 31, 2015 at 3:37 PM, boB Stepp wrote: > On Tue, Mar 31, 2015 at 3:28 PM, Zachary Ware > wrote: >> On Tue, Mar 31, 2015 at 3:23 PM, boB Stepp wrote: >>> The following behavior has me stumped: >>> >>> Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit >>> (Intel)] on win32 >>> Type "copyright", "credits" or "license()" for more information. >>>>>> L = ['#ROI:roi_0', '#TXT:text_0', '#1:one^two^three'] >>>>>> for i, item in enumerate(L): >>> subitems = item.split(':') >>> if subitems[0] == '#ROI': >>> print subitems[1] >>> if subitems[0] == '#TXT': >>> print subitems[1] >>> if subitems[0] == '#1' or '#2': >> >> Here's your problem: "#2" is always true. Try "if subitems[0] in >> ['#1', '#2']:" > > Thanks, Zach! About the time your reply arrived I was starting to > realize that my '#1' or '#2' might not be doing what I thought. In the > "Python Pocket Reference" I was just looking at: > > X or Y If X is false then Y; else X. > > I forgot that different languages have different interpretations of "or". In this case, the differing languages being Python and English :). Also, not that since you aren't using the index for anything, you don't need to use enumerate() to iterate over the list. Just do "for item in L:". Of course, if you actually use the index in the real code that I assume this was cut out of, keep enumerate; it's the right tool for the job. -- Zach From robertvstepp at gmail.com Tue Mar 31 22:47:09 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 31 Mar 2015 15:47:09 -0500 Subject: [Tutor] Unexpected results using enumerate() and .split() In-Reply-To: <551B0451.8010302@davea.name> References: <551B0451.8010302@davea.name> Message-ID: On Tue, Mar 31, 2015 at 3:32 PM, Dave Angel wrote: > On 03/31/2015 04:23 PM, boB Stepp wrote: >> >> The following behavior has me stumped: >> >> Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit >> (Intel)] on win32 >> Type "copyright", "credits" or "license()" for more information. >>>>> >>>>> L = ['#ROI:roi_0', '#TXT:text_0', '#1:one^two^three'] >>>>> for i, item in enumerate(L): >> >> subitems = item.split(':') >> if subitems[0] == '#ROI': >> print subitems[1] >> if subitems[0] == '#TXT': >> print subitems[1] >> if subitems[0] == '#1' or '#2': > > > I think what you meant here was: > if subitems[0] == "#1" or subitems[0] == "#2": > >> print subitems[1] > > > Study the first expression and see if you can figure out what the difference > is. If it's not clear, then make a simpler program just to test a compound > if, and we'll all talk about it. Yeah, your version evaluates to "False or False", which is what I wanted, but my version didn't. >> BTW, I copied and pasted the above into my Gmail window, but it >> removed the indentation that was present in the interpreter. I added >> spaces manually to get it appear as it did in the interpreter. Anyone >> know why Gmail does that to my copy and paste? >> > > Buggy, I guess. Why not use a program like Thunderbird, which is free and > available on most PC operating systems? > > (Unfortunately, it's not on Android) This is on my TODO list! I had started an earlier thread looking for a Gmail replacement if you recall. I just haven't gotten riled enough by Gmail to make the switch, but I am oh, so close! -- boB From robertvstepp at gmail.com Tue Mar 31 22:49:13 2015 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 31 Mar 2015 15:49:13 -0500 Subject: [Tutor] Unexpected results using enumerate() and .split() In-Reply-To: References: Message-ID: On Tue, Mar 31, 2015 at 3:42 PM, Zachary Ware wrote: > Also, not that since you aren't using the index for anything, you > don't need to use enumerate() to iterate over the list. Just do "for > item in L:". Of course, if you actually use the index in the real > code that I assume this was cut out of, keep enumerate; it's the right > tool for the job. Yeah, I simplified my actual code into the smallest snippet that I could reproduce my problem in. My actual code need the index. -- boB From japhy at pearachute.com Tue Mar 31 18:18:44 2015 From: japhy at pearachute.com (Japhy Bartlett) Date: Tue, 31 Mar 2015 11:18:44 -0500 Subject: [Tutor] Dynamic naming of lists In-Reply-To: References: <551AAF6C.20003@davea.name> Message-ID: Ian - Note that if all your keys are named 'broadcast', the dictionary approach is probably not going to work. You'll end up with something like: { 'broadcast': 'last_value_in_the_list' } On Tue, Mar 31, 2015 at 10:56 AM, Ian D wrote: > Ok Thanks a lot. And sadly not a typo, my bad logic overwriting values! > > ---------------------------------------- > > To: tutor at python.org > > From: __peter__ at web.de > > Date: Tue, 31 Mar 2015 17:50:01 +0200 > > Subject: Re: [Tutor] Dynamic naming of lists > > > > Ian D wrote: > > > >> Thanks I will look into these. The data going in is a list like > >> this:['broadcast', '"d8on"', 'broadcast', '"d11on"'] > >> > >> With the output beng something like this. > >> > >> lst_0 = ['broadcast', '"d8on"'] > >> > >> lst_0 = ['broadcast', '"d11on"'] > > > > Is that a typo, did you mean > > > > lst_1 = ['broadcast', '"d11on"'] > > > > ? If so us a list. Complete example: > > > >>>> flat_pairs = ['broadcast', '"d8on"', 'broadcast', '"d11on"'] > >>>> it = iter(flat_pairs) > >>>> pairs = list(zip(it, it)) > >>>> pairs > > [('broadcast', '"d8on"'), ('broadcast', '"d11on"')] > > > > You can then access individual pairs by providing an index into the pairs > > list: > > > >>>> pairs[0] > > ('broadcast', '"d8on"') > >>>> pairs[1] > > ('broadcast', '"d11on"') > > > >> I have managed to use a dictionary as advised in a post on > StackOverflow; > >> not quite completed it as I am overwriting my lists each time. From your > >> experience is it better to pursue the dictionary route or the zip tuple > >> option. I am in python2.7 > > > > A dictionary is typically used when the keys are not as regular as a > > sequence of integers, e. g. to map a user name to an email address or a > word > > to a list of the positions where it occurs in a text. > > > > _______________________________________________ > > Tutor maillist - Tutor at python.org > > To unsubscribe or change subscription options: > > https://mail.python.org/mailman/listinfo/tutor > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor >