From monikajg at netzero.net Thu Sep 1 01:35:33 2016 From: monikajg at netzero.net (monikajg at netzero.net) Date: Thu, 1 Sep 2016 05:35:33 GMT Subject: [Tutor] python projects Message-ID: <20160831.223533.19014.0@webmail04.dca.untd.com> Hi: I have been taking python classes for overa year now and studying and studying it all days long. However, I still cannot get a job as a python automation qa (despite of many years of experience in qa) because everybody is looking for a senior python developers for automation qa jobs. Entry level positions are rare. Is there a website where I could maybe do some python projects for somebody, for free or for low pay? Pretty soon I will have to take a survival job since my savings are ending and when that happens I will not have much time to practice python and all my hard work will go to waste. Thank you very much Monika ---------- Original Message ---------- From: Alan Gauld via Tutor To: tutor at python.org Subject: Re: [Tutor] generator object Date: Thu, 1 Sep 2016 00:23:53 +0100 On 31/08/16 23:24, Alan Gauld via Tutor wrote: > Then I tried > ... > x = list(2) Should be list(gen(2)) I hope that was obvious... -- 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 ____________________________________________________________ Health News 24 (Sponsored by Content.Ad) Don't Use Botox, Use This Instead: Granny Reveals $39 Method http://thirdpartyoffers.netzero.net/TGL3241/57c7be4cdccca3e4c38a6st04duc From alan.gauld at yahoo.co.uk Thu Sep 1 04:52:41 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 1 Sep 2016 09:52:41 +0100 Subject: [Tutor] Help with NameError In-Reply-To: References: Message-ID: On 01/09/16 02:29, Bryan Callow wrote: > Could someone help me with a NameError that I can't seem to figure out. It is hard for us to figure out without seeing the error message. It should tell you which name is in error and where. Please repost with the full error message included. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Thu Sep 1 05:39:59 2016 From: __peter__ at web.de (Peter Otten) Date: Thu, 01 Sep 2016 11:39:59 +0200 Subject: [Tutor] Help with NameError References: Message-ID: Bryan Callow wrote: > Could someone help me with a NameError that I can't seem to figure out. > The program asks for an input and then runs two DC motors. It worked for > a while and then when I reopened the program today I keep getting this > error. Thank you. -Bryan [...] > print('Which direction would you like to move?') > direction = input("Type 'f' for forward, 'b' for backward, or 'e' to exit: > ") > while direction not in exit_: [...] My crystal ball says: Your script is written in Python 3, but you are trying to run it with Python 2. Background: Python 2's input() function evaluates user input as a Python expression -- if for example you enter 1+1 input() will return 2; if you enter b input() will look up the variable b and fail with a NameError because no such variable exists. Run your script with $ python3 yourscript.py and everything should work as expected. To avoid such confusion in the future you can insert #!/usr/bin/env python3 as the first line, make the script executable with $ chmod u+x yourscript.py and run it with $ ./yourscript.py From alan.gauld at yahoo.co.uk Thu Sep 1 06:24:57 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 1 Sep 2016 11:24:57 +0100 Subject: [Tutor] Inheritance vs Assignment In-Reply-To: <7BD64E0E-1FD4-4010-8E1F-5414D1BCA634@gmail.com> References: <7BD64E0E-1FD4-4010-8E1F-5414D1BCA634@gmail.com> Message-ID: On 01/09/16 04:18, kay Cee wrote: > Class a(): > def__init__(self, var): > pass > > Class b(a): > def__init__(self): > super().__init__(self, var) > pass > Is it better to do > > b = a() > > Instead of making b its own class? > Also, what would be the benefit of making a separate class for b if any at all? I'm afraid your question is too abstract to answer. The use of classes depends on the context and in this case we have no context on which to base a judgement. Looking strictly at your code it makes no sense to have either a or b as classes since there is no state (variables) involved and no operations(methods) are provided. Both are zero impact classes. But I'm guessing you intended that to be indicative of a more complex scenario. In general, inheritance is used to specialise a class. It should be true to say that b *is an* a. So if b were to add some specialized content to 'a' then inheritance would make sense. But b = a() makes b an instance of 'a' not a class. So the two things are entirely different, it's like asking which is better: a mountain or Mount Everest? -- 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 zemmoura.khalil at gmail.com Thu Sep 1 06:15:35 2016 From: zemmoura.khalil at gmail.com (khalil zakaria Zemmoura) Date: Thu, 1 Sep 2016 11:15:35 +0100 Subject: [Tutor] Inheritance vs Assignment In-Reply-To: <7BD64E0E-1FD4-4010-8E1F-5414D1BCA634@gmail.com> References: <7BD64E0E-1FD4-4010-8E1F-5414D1BCA634@gmail.com> Message-ID: Assignment and inheritance are not comparable at all In the inheritance you are extending the base class (a) and in "a=b()" you are instantiating it - you create an object according to the blueprint that you difining. To understand the difference. Inheritance: Say you defined a costume type (a class ) and classes are really that. After that you needed an other kind of object that share the same code with "class a" with additional attribute and actions you want to apply to them. Instead of duplicating the code in "b" just inherit from "a" and write your extra code in "b" and you are done. You still need to instantiate "b" to get an object and work with. Assignment is a different thing that doesn't have any thing to do with inheritance, since what you are doing here is creating an object in the memory and binding it to a name to access it later. Is that make sense? Now I can see some interest in using the "b inherit from a" that way. It's a common technique to just inherit from an exception class to just catch an exception with a meaningful name. The second case that I see is that when using multiple inheritance that's allow you to linearize the parents classes to get the code works. Since I am responding from a smartphone, It's tedious to expand the explanation, I wanted to give you an overview of some concepts and ideas on how they work. Google working with exceptions and you'll get a tons of tutorials that shows you how it works. And search a talk about "super" named "super consider super". It'll give you more information about the inheritance model of Python, especially the multiple inheritance model and the "method resolution order called 'mro' ". I hope that helps you. If it doesn't, just ask again. Regards. Le 1 sept. 2016 09:51, "kay Cee" a ?crit : > > Suppose there is -----> > > Class a(): > def__init__(self, var): > pass > > Class b(a): > def__init__(self): > super().__init__(self, var) > pass > > Note: syntax may be incorrect ... > > Is it better to do > > b = a() > > Instead of making b its own class? > Also, what would be the benefit of making a separate class for b if any at > all? > > Thanks in advance .... > > Unee0x > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From zemmoura.khalil at gmail.com Thu Sep 1 10:07:53 2016 From: zemmoura.khalil at gmail.com (khalil zakaria Zemmoura) Date: Thu, 1 Sep 2016 15:07:53 +0100 Subject: [Tutor] Inheritance vs Assignment In-Reply-To: References: <7BD64E0E-1FD4-4010-8E1F-5414D1BCA634@gmail.com> Message-ID: Let me clarify some ideas that I spoke about in my last email. I talked about extending exception and linearization from the perspective of inheriting from a class without adding behavior or some extra attributes, because the code you provided was doing that. Reading the Alan response made me realize that my last email wasn't very clear and can lead to validating bad implementation. So to clarify. Extending classes without adding behavior can be useful in custom exception or resolving problems with super() by linearization of the parents classes. The implementation is a different thing. We do that by defining an abstract class that inherit from its parent. We do that that way class B(A): pass And no need to a second init method and calling super on it because instantiating the B class will call implicitly the __init__ of it's parent But as Alan mentioned before, maybe your purpose was to give just an example to explain your question. Regards. Le 1 sept. 2016 11:28, "Alan Gauld via Tutor" a ?crit : > On 01/09/16 04:18, kay Cee wrote: > > > Class a(): > > def__init__(self, var): > > pass > > > > Class b(a): > > def__init__(self): > > super().__init__(self, var) > > pass > > > Is it better to do > > > > b = a() > > > > Instead of making b its own class? > > Also, what would be the benefit of making a separate class for b if any > at all? > > I'm afraid your question is too abstract to answer. > The use of classes depends on the context and in this > case we have no context on which to base a judgement. > > Looking strictly at your code it makes no sense to > have either a or b as classes since there is no > state (variables) involved and no operations(methods) > are provided. Both are zero impact classes. But I'm > guessing you intended that to be indicative of a > more complex scenario. > > In general, inheritance is used to specialise a class. > It should be true to say that b *is an* a. > So if b were to add some specialized content > to 'a' then inheritance would make sense. > > But b = a() makes b an instance of 'a' not a class. > So the two things are entirely different, it's like > asking which is better: a mountain or Mount Everest? > > > -- > 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 monikajg at netzero.net Thu Sep 1 10:12:11 2016 From: monikajg at netzero.net (monikajg at netzero.net) Date: Thu, 1 Sep 2016 14:12:11 GMT Subject: [Tutor] python memory management Message-ID: <20160901.071211.32022.0@webmail13.dca.untd.com> Hi: Can somebody please explain how memory is managed by python? What kind of memory it uses? What structures use what kind of memory? If many people work on the same project and have many instances of the same object how do they ensure that all instances are killed before the programs exit? Apparently if one of the programmer leaves a reference to object it might not be automatically deleted by python on exit. What is the command to do this? Could somebody please explain how this works, especially on projects involving multiple programmers? Thank you very much Monika ____________________________________________________________ DrAnkurWalia (Sponsored by Content.Ad) Breaking: Delhi Girls Are Getting 5 Shades Fairer (Do This) http://thirdpartyoffers.netzero.net/TGL3241/57c8379d62edd379d57b6st04duc From alan.gauld at yahoo.co.uk Thu Sep 1 12:57:01 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 1 Sep 2016 17:57:01 +0100 Subject: [Tutor] python memory management In-Reply-To: <20160901.071211.32022.0@webmail13.dca.untd.com> References: <20160901.071211.32022.0@webmail13.dca.untd.com> Message-ID: On 01/09/16 15:12, monikajg at netzero.net wrote: > Can somebody please explain how memory is managed by python? > What kind of memory it uses? What structures use what kind of memory? I'm not sure what you have in mind? Do you want to know the internal structure of the various data types? Do you want to know the size of the memory footprint? > If many people work on the same project and have many > instances of the same object how do they ensure that > all instances are killed before the programs exit? Generally each developer runs their own instance of the program and the OS/Python manage the memory for each process entirely separate from all other processes. > Apparently if one of the programmer leaves a reference > to object it might not be automatically deleted > by python on exit. What is the command to do this? When python exits, all objects are deleted regardless of how many references still exist. The issue is more relevant to a server process which never exits. In that case it is possible for a programmer to leave a reference to an obsolete object and prevent it from being disposed of. This is known as a memory leak. But the issue applies just as much to a single programmer as to multiple programmers. It is a design issue that requires that the redundant objects are cleared away correctly with no dangling references. It is one of the many design considerations when building long running 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 steve at pearwood.info Thu Sep 1 14:10:02 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 2 Sep 2016 04:10:02 +1000 Subject: [Tutor] python memory management In-Reply-To: <20160901.071211.32022.0@webmail13.dca.untd.com> References: <20160901.071211.32022.0@webmail13.dca.untd.com> Message-ID: <20160901181002.GA26300@ando.pearwood.info> On Thu, Sep 01, 2016 at 02:12:11PM +0000, monikajg at netzero.net wrote: > Hi: > Can somebody please explain how memory is managed by python? What kind > of memory it uses? What structures use what kind of memory? > If many people work on the same project and have many instances of the > same object how do they ensure that all instances are killed before > the programs exit? Apparently if one of the programmer leaves a > reference to object it might not be automatically deleted by python on > exit. What is the command to do this? > > Could somebody please explain how this works, especially on projects > involving multiple programmers? In general, you (almost) never need to care about memory management, Python will do it for you. The number of programmers writing the code doesn't matter. What matters is how many times the program is running *at the same time*. Each time it runs, your computer's operating system (Windows, Linux, Mac OS X) will start what is called "a process", running the Python interpreter. When the process exits at the end, the OS will reclaim all the memory used and make it available for the next process. While the program is running, the OS has to allocate memory between many different processes. On my computer, right now, I have over 200 processes running. Most of them are handled by the OS, but the others include my email program, my web browser, a few text editors, my desktop manager, and many others. The OS manages the memory allocation. As far as Python is concerned, it manages its own memory from what the OS gives it. When you assign a value: name = "Inigo Montoya" the Python interpreter allocates a chunk of memory in the memory heap to hold the string. It then tracks whether or not the string is being used. So long as the string is being used by your program, or *could possibly* be used, Python will hold onto that string, forever. But as soon as it sees that it can no longer be used, it will free the memory and reuse it. This process is called "garbage collection". You can google for more information, or ask here. Different Python interpreters use different garbage collectors: IronPython uses the .Net garbage collector; Jython uses the Java garbage collector; PyPy has a few different ones that you can choose from; and the CPython (that's the standard Python you are probably running) interpreter has two, a simple "reference counter" GC that works very fast but not very thoroughly, and a more thorough GC that picks up anything the reference counter can't handle. (Mostly reference cycles: if one object has a reference to another, and that second object also has a reference to the first, that's a cycle. The reference counter can't deal with that, but the second GC can.) Let's track the life-span of a chunk of memory. Suppose you write the following code in a module: name = "Inigo Montoya" print(name) name = "The Dread Pirate Roberts" The second assignment frees up the string "Inigo Montoya", as no part of your program can possibly access the old value any more, since it has been replaced by the new value. So the garbage collector frees that chunk of memory and makes it available for something else. This happens automatically, and virtually instantly. You never need to care about allocating or deallocating memory. The interpreter has its own memory manager to do that, with a garbage collector to deallocate memory. So, when do you need to care about memory? - If you are writing a C extension, you have to manage your own memory. - If you're using the ctypes module, you have access to the C code of the interpreter, so you have to care about managing your own memory. - If you're creating massively huge strings or lists, you might have to worry about running out of memory. For example, I once locked up my computer by foolishly creating a HUGE list: # Don't try this at home! L = list(range(100**100)) That would need to find enough memory for a list with one hundred trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion entries. Each entry will take at least four bytes, so the total is ... well, it's a lot. Much more than my poor computer has. On Windows, this will fail quite quickly with a MemoryError, no real harm done, but on Linux (which I use) the OS will gamely, or perhaps stupidly, try very hard to allocate a trillion trillion trillion ... trillion terabytes of memory, locking up my computer. (I let it run overnight, and eventually needed to just pull the plug to reset it.) Fortunately, even on Linux there are ways to tell the OS not to be so stupid, which means that Python will raise a MemoryError and no real harm is done, but at the time I didn't know about them. So that's about it really... if you're writing long-lasting server class programs, you might need to care about memory; if you're trying to process huge files bigger than the amount of RAM you have, you need to think about memory... but most of the time, just write your code and let the Python garbage collector manage it for you. Feel free to ask questions if anything is unclear! -- Steve From monikajg at netzero.net Thu Sep 1 16:21:36 2016 From: monikajg at netzero.net (monikajg at netzero.net) Date: Thu, 1 Sep 2016 20:21:36 GMT Subject: [Tutor] python memory management Message-ID: <20160901.132136.4753.0@webmail01.dca.untd.com> Thank you for your explanation. It is very clear and confirms what I thought I knew. However, I had a job interview and the interview said it was a mistake that I did not say that in cases when there are multiple programmers, there might be some objects/references left and not deallocated from the memory by python. He asked me how this should be handles. So I told him that python has its automatic garbage collection which keeps track of all objects and references and deletes them as appropriate (similar what you state). I added that programmers can keep track of occurrences of object and make sure that it goes down to 0. And if it does not then use del to delete. However, he did not like my answer. So Im trying to learn from my mistakes and learn if an object or reference are still at the end of the program how they should be deleted. I had problem understanding him too well since he was calling from overseas (there was interference) and I could not understand his accent well. But I need to learn this for future interviews. Your posting states that this is not a problem but according to the interviewer it is. (I do not mean to be un-nice to you, sorry) So how this situation should be handled? Thank you Monika ---------- Original Message ---------- From: Steven D'Aprano To: tutor at python.org Subject: Re: [Tutor] python memory management Date: Fri, 2 Sep 2016 04:10:02 +1000 On Thu, Sep 01, 2016 at 02:12:11PM +0000, monikajg at netzero.net wrote: > Hi: > Can somebody please explain how memory is managed by python? What kind > of memory it uses? What structures use what kind of memory? > If many people work on the same project and have many instances of the > same object how do they ensure that all instances are killed before > the programs exit? Apparently if one of the programmer leaves a > reference to object it might not be automatically deleted by python on > exit. What is the command to do this? > > Could somebody please explain how this works, especially on projects > involving multiple programmers? In general, you (almost) never need to care about memory management, Python will do it for you. The number of programmers writing the code doesn't matter. What matters is how many times the program is running *at the same time*. Each time it runs, your computer's operating system (Windows, Linux, Mac OS X) will start what is called "a process", running the Python interpreter. When the process exits at the end, the OS will reclaim all the memory used and make it available for the next process. While the program is running, the OS has to allocate memory between many different processes. On my computer, right now, I have over 200 processes running. Most of them are handled by the OS, but the others include my email program, my web browser, a few text editors, my desktop manager, and many others. The OS manages the memory allocation. As far as Python is concerned, it manages its own memory from what the OS gives it. When you assign a value: name = "Inigo Montoya" the Python interpreter allocates a chunk of memory in the memory heap to hold the string. It then tracks whether or not the string is being used. So long as the string is being used by your program, or *could possibly* be used, Python will hold onto that string, forever. But as soon as it sees that it can no longer be used, it will free the memory and reuse it. This process is called "garbage collection". You can google for more information, or ask here. Different Python interpreters use different garbage collectors: IronPython uses the .Net garbage collector; Jython uses the Java garbage collector; PyPy has a few different ones that you can choose from; and the CPython (that's the standard Python you are probably running) interpreter has two, a simple "reference counter" GC that works very fast but not very thoroughly, and a more thorough GC that picks up anything the reference counter can't handle. (Mostly reference cycles: if one object has a reference to another, and that second object also has a reference to the first, that's a cycle. The reference counter can't deal with that, but the second GC can.) Let's track the life-span of a chunk of memory. Suppose you write the following code in a module: name = "Inigo Montoya" print(name) name = "The Dread Pirate Roberts" The second assignment frees up the string "Inigo Montoya", as no part of your program can possibly access the old value any more, since it has been replaced by the new value. So the garbage collector frees that chunk of memory and makes it available for something else. This happens automatically, and virtually instantly. You never need to care about allocating or deallocating memory. The interpreter has its own memory manager to do that, with a garbage collector to deallocate memory. So, when do you need to care about memory? - If you are writing a C extension, you have to manage your own memory. - If you're using the ctypes module, you have access to the C code of the interpreter, so you have to care about managing your own memory. - If you're creating massively huge strings or lists, you might have to worry about running out of memory. For example, I once locked up my computer by foolishly creating a HUGE list: # Don't try this at home! L = list(range(100**100)) That would need to find enough memory for a list with one hundred trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion entries. Each entry will take at least four bytes, so the total is ... well, it's a lot. Much more than my poor computer has. On Windows, this will fail quite quickly with a MemoryError, no real harm done, but on Linux (which I use) the OS will gamely, or perhaps stupidly, try very hard to allocate a trillion trillion trillion ... trillion terabytes of memory, locking up my computer. (I let it run overnight, and eventually needed to just pull the plug to reset it.) Fortunately, even on Linux there are ways to tell the OS not to be so stupid, which means that Python will raise a MemoryError and no real harm is done, but at the time I didn't know about them. So that's about it really... if you're writing long-lasting server class programs, you might need to care about memory; if you're trying to process huge files bigger than the amount of RAM you have, you need to think about memory... but most of the time, just write your code and let the Python garbage collector manage it for you. Feel free to ask questions if anything is unclear! -- Steve _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ____________________________________________________________ Health News 24 (Sponsored by Content.Ad) Granny Reveals Her Method: Don't Use Botox, Do This Instead http://thirdpartyoffers.netzero.net/TGL3241/57c88e246b882e24436dst02duc From joel.goldstick at gmail.com Thu Sep 1 17:31:05 2016 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Thu, 1 Sep 2016 17:31:05 -0400 Subject: [Tutor] python memory management In-Reply-To: <20160901.132136.4753.0@webmail01.dca.untd.com> References: <20160901.132136.4753.0@webmail01.dca.untd.com> Message-ID: On Thu, Sep 1, 2016 at 4:21 PM, monikajg at netzero.net wrote: > Thank you for your explanation. It is very clear and confirms what I thought I knew. However, I had a job interview and the interview said it was a mistake that I did not say that in cases when there are multiple programmers, there might be some objects/references left and not deallocated from the memory by python. He asked me how this should be handles. > So I told him that python has its automatic garbage collection which keeps track of all objects and references and deletes them as appropriate (similar what you state). I added that programmers can keep track of occurrences of object and make sure that it goes down to 0. And if it does not then use del to delete. However, he did not like my answer. So Im trying to learn from my mistakes and learn if an object or reference are still at the end of the program how they should be deleted. I had problem understanding him too well since he was calling from overseas (there was interference) and I could not understand his accent well. But I need to learn this for future interviews. > Your posting states that this is not a problem but according to the interviewer it is. (I do not mean to be un-nice to you, sorry) So how this situation should be handled? > Thank you > Monika I would chalk this up to the interviewer wanting to take the upper hand. If he (she?) thought your understanding was wrong, the appropriate thing to do would be to explain how you were wrong, and take it as a teaching moment. If it happens again, ask how your answer was incorrect. I think this shows interest in learning more, and that can't be a bad thing > > ---------- Original Message ---------- > From: Steven D'Aprano > To: tutor at python.org > Subject: Re: [Tutor] python memory management > Date: Fri, 2 Sep 2016 04:10:02 +1000 > > On Thu, Sep 01, 2016 at 02:12:11PM +0000, monikajg at netzero.net wrote: >> Hi: >> Can somebody please explain how memory is managed by python? What kind >> of memory it uses? What structures use what kind of memory? >> If many people work on the same project and have many instances of the >> same object how do they ensure that all instances are killed before >> the programs exit? Apparently if one of the programmer leaves a >> reference to object it might not be automatically deleted by python on >> exit. What is the command to do this? >> >> Could somebody please explain how this works, especially on projects >> involving multiple programmers? > > > In general, you (almost) never need to care about memory management, > Python will do it for you. > > The number of programmers writing the code doesn't matter. What matters > is how many times the program is running *at the same time*. Each time > it runs, your computer's operating system (Windows, Linux, Mac OS X) > will start what is called "a process", running the Python interpreter. > When the process exits at the end, the OS will reclaim all the memory > used and make it available for the next process. > > While the program is running, the OS has to allocate memory between many > different processes. On my computer, right now, I have over 200 > processes running. Most of them are handled by the OS, but the others > include my email program, my web browser, a few text editors, my desktop > manager, and many others. The OS manages the memory allocation. > > As far as Python is concerned, it manages its own memory from what the > OS gives it. When you assign a value: > > name = "Inigo Montoya" > > the Python interpreter allocates a chunk of memory in the memory heap to > hold the string. It then tracks whether or not the string is being used. > So long as the string is being used by your program, or *could possibly* > be used, Python will hold onto that string, forever. > > But as soon as it sees that it can no longer be used, it will free the > memory and reuse it. > > This process is called "garbage collection". You can google for more > information, or ask here. Different Python interpreters use different > garbage collectors: > > IronPython uses the .Net garbage collector; > > Jython uses the Java garbage collector; > > PyPy has a few different ones that you can choose from; > > and the CPython (that's the standard Python you are probably running) > interpreter has two, a simple "reference counter" GC that works very > fast but not very thoroughly, and a more thorough GC that picks up > anything the reference counter can't handle. > > (Mostly reference cycles: if one object has a reference to another, and > that second object also has a reference to the first, that's a cycle. > The reference counter can't deal with that, but the second GC can.) > > > Let's track the life-span of a chunk of memory. Suppose you write the > following code in a module: > > > name = "Inigo Montoya" > print(name) > name = "The Dread Pirate Roberts" > > > The second assignment frees up the string "Inigo Montoya", as no part of > your program can possibly access the old value any more, since it has > been replaced by the new value. So the garbage collector frees that > chunk of memory and makes it available for something else. This happens > automatically, and virtually instantly. > > You never need to care about allocating or deallocating memory. The > interpreter has its own memory manager to do that, with a garbage > collector to deallocate memory. > > So, when do you need to care about memory? > > - If you are writing a C extension, you have to manage your own memory. > > - If you're using the ctypes module, you have access to the C code of > the interpreter, so you have to care about managing your own memory. > > - If you're creating massively huge strings or lists, you might have to > worry about running out of memory. > > For example, I once locked up my computer by foolishly creating a HUGE > list: > > # Don't try this at home! > L = list(range(100**100)) > > > That would need to find enough memory for a list with one hundred > trillion trillion trillion trillion trillion trillion trillion trillion > trillion trillion trillion trillion trillion trillion trillion trillion > trillion trillion trillion trillion trillion trillion entries. Each > entry will take at least four bytes, so the total is ... well, it's a > lot. Much more than my poor computer has. > > On Windows, this will fail quite quickly with a MemoryError, no real > harm done, but on Linux (which I use) the OS will gamely, or perhaps > stupidly, try very hard to allocate a trillion trillion trillion ... > trillion terabytes of memory, locking up my computer. (I let it run > overnight, and eventually needed to just pull the plug to reset it.) > > Fortunately, even on Linux there are ways to tell the OS not to be so > stupid, which means that Python will raise a MemoryError and no real > harm is done, but at the time I didn't know about them. > > So that's about it really... if you're writing long-lasting server class > programs, you might need to care about memory; if you're trying to > process huge files bigger than the amount of RAM you have, you need to > think about memory... but most of the time, just write your code and let > the Python garbage collector manage it for you. > > > > Feel free to ask questions if anything is unclear! > > > > -- > Steve > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > > ____________________________________________________________ > Health News 24 (Sponsored by Content.Ad) > Granny Reveals Her Method: Don't Use Botox, Do This Instead > http://thirdpartyoffers.netzero.net/TGL3241/57c88e246b882e24436dst02duc > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From yohei.uemura at gmail.com Fri Sep 2 04:36:28 2016 From: yohei.uemura at gmail.com (Yohei Uemura) Date: Fri, 2 Sep 2016 17:36:28 +0900 Subject: [Tutor] About C++ library with multiprocessing Message-ID: Hi all, I'm Yohei. I have a problem with making a script which use both multiprocessing and C++ library. My C++ library is made by using boost python. The library worked well if it was imported normally. That is, "import simple as F A = range(0,100) for x in A: print F.addition(x) " However, it couldn't work if it was called from multiprocessing. That is, "import simple as F import multiprocessing as mp pool = mp.Pool() A = range(0,100) results = pool.map(F.simple,A) " The error messages are shown in the attached file. It seems a 'pickle' problem... Best regards, -------------- next part -------------- Traceback (most recent call last): File "testCXX.py", line 15, in results = pool.map(F.addition,A) File "/usr/lib64/python2.7/multiprocessing/pool.py", line 251, in map return self.map_async(func, iterable, chunksize).get() File "/usr/lib64/python2.7/multiprocessing/pool.py", line 558, in get raise self._value cPickle.PicklingError: Can't pickle : attribute lookup __builtin__.builtin_function_or_method failed From __peter__ at web.de Fri Sep 2 08:13:30 2016 From: __peter__ at web.de (Peter Otten) Date: Fri, 02 Sep 2016 14:13:30 +0200 Subject: [Tutor] About C++ library with multiprocessing References: Message-ID: Yohei Uemura wrote: > Hi all, > > I'm Yohei. > I have a problem with making a script which use both multiprocessing and > C++ library. > My C++ library is made by using boost python. > The library worked well if it was imported normally. > That is, > "import simple as F > A = range(0,100) > for x in A: > print F.addition(x) > " > However, it couldn't work if it was called from multiprocessing. > That is, > "import simple as F > import multiprocessing as mp > pool = mp.Pool() > A = range(0,100) > results = pool.map(F.simple,A) > " > The error messages are shown in the attached file. > It seems a 'pickle' problem... Attachments may be stripped off, so it's better to include the traceback into the message body. > Traceback (most recent call last): > File "testCXX.py", line 15, in > results = pool.map(F.addition,A) > File "/usr/lib64/python2.7/multiprocessing/pool.py", line 251, in map > return self.map_async(func, iterable, chunksize).get() > File "/usr/lib64/python2.7/multiprocessing/pool.py", line 558, in get > raise self._value > cPickle.PicklingError: Can't pickle : > attribute lookup __builtin__.builtin_function_or_method failed The processes communicate by passing pickled objects to each other. For some reason F.simple() doesn't have a __module__ attribute that correctly tells Python its origin, and thus pickling fails. You can use https://docs.python.org/dev/library/copyreg.html to work around that limitation or -- even easier -- wrap it into a Python function that can be pickled: ... def mysimple(arg): return F.simple(arg) ... results = pool.map(mysimple, A) From girish.grover at gmail.com Fri Sep 2 10:07:58 2016 From: girish.grover at gmail.com (Girish Grover) Date: Fri, 2 Sep 2016 09:07:58 -0500 Subject: [Tutor] Issue while using the python library Message-ID: Hi, I am trying to use a python package from github and struggling to make it work. Looking for experts to help me out. The issue is described via the following link. http://community.cloudera.com/t5/Hadoop-101-Training-Quickstart/Not-able-to-run-Python-Streaming-Tutorial/td-p/44648 Thanks Girish -- Sent from Gmail Mobile From gaurvipul at gmail.com Fri Sep 2 13:46:44 2016 From: gaurvipul at gmail.com (Vipul Gaur) Date: Fri, 2 Sep 2016 23:16:44 +0530 Subject: [Tutor] python projects In-Reply-To: <20160831.223533.19014.0@webmail04.dca.untd.com> References: <20160831.223533.19014.0@webmail04.dca.untd.com> Message-ID: Monika If you have the technical skills they want then give them the experience they want Technically it's just selling why so conscientious? Cheers On Thursday, September 1, 2016, monikajg at netzero.net wrote: > Hi: > I have been taking python classes for overa year now and studying and > studying it all days long. However, I still cannot get a job as a python > automation qa (despite of many years of experience in qa) because everybody > is looking for a senior python developers for automation qa jobs. Entry > level positions are rare. Is there a website where I could maybe do some > python projects for somebody, for free or for low pay? Pretty soon I will > have to take a survival job since my savings are ending and when that > happens I will not have much time to practice python and all my hard work > will go to waste. > Thank you very much > Monika > > ---------- Original Message ---------- > From: Alan Gauld via Tutor > > To: tutor at python.org > Subject: Re: [Tutor] generator object > Date: Thu, 1 Sep 2016 00:23:53 +0100 > > On 31/08/16 23:24, Alan Gauld via Tutor wrote: > > > Then I tried > > ... > > x = list(2) > > Should be > > > list(gen(2)) > > I hope that was obvious... > -- > 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 > > ____________________________________________________________ > Health News 24 (Sponsored by Content.Ad) > Don't Use Botox, Use This Instead: Granny Reveals $39 Method > http://thirdpartyoffers.netzero.net/TGL3241/57c7be4cdccca3e4c38a6st04duc > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Rgds Vipul Gaur From alan.gauld at yahoo.co.uk Fri Sep 2 19:14:24 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 3 Sep 2016 00:14:24 +0100 Subject: [Tutor] Issue while using the python library In-Reply-To: References: Message-ID: On 02/09/16 15:07, Girish Grover wrote: > I am trying to use a python package from github and struggling to make it > work. Looking for experts to help me out. Technically its off topic for this list which is for core language and standard library issues. But if you were to tell us what package and what the error was you might get some help. > The issue is described via the following link. > > http://community.cloudera.com/t5/Hadoop-101-Training-Quickstart/Not-able-to-run-Python-Streaming-Tutorial/td-p/44648 What motivation do we have for visiting a random web page? If you want help provide the information needed. I did look at your post which doesn't provide much more detail although it does include a traceback. It seems you need to install a module called tagger? Or maybe just put it into your PYTHONPATH? Without a lot more information about what you have done we can't really say. Assuming it's not as simple as installing the module or fixing the path you probably need to find a support forum for the package, they are probably best placed to help. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From monikajg at netzero.net Fri Sep 2 23:25:20 2016 From: monikajg at netzero.net (monikajg at netzero.net) Date: Sat, 3 Sep 2016 03:25:20 GMT Subject: [Tutor] python memory management Message-ID: <20160902.202520.22050.0@webmail13.dca.untd.com> By: "reference cycles: if one object has a reference to another, and that second object also has a reference to the first, that's a cycle." Is this what you mean? a = 5 b = a a = b I just want to make sure I understand. Thank you very much for your explanation. Monika ---------- Original Message ---------- From: Steven D'Aprano To: tutor at python.org Subject: Re: [Tutor] python memory management Date: Fri, 2 Sep 2016 04:10:02 +1000 On Thu, Sep 01, 2016 at 02:12:11PM +0000, monikajg at netzero.net wrote: > Hi: > Can somebody please explain how memory is managed by python? What kind > of memory it uses? What structures use what kind of memory? > If many people work on the same project and have many instances of the > same object how do they ensure that all instances are killed before > the programs exit? Apparently if one of the programmer leaves a > reference to object it might not be automatically deleted by python on > exit. What is the command to do this? > > Could somebody please explain how this works, especially on projects > involving multiple programmers? In general, you (almost) never need to care about memory management, Python will do it for you. The number of programmers writing the code doesn't matter. What matters is how many times the program is running *at the same time*. Each time it runs, your computer's operating system (Windows, Linux, Mac OS X) will start what is called "a process", running the Python interpreter. When the process exits at the end, the OS will reclaim all the memory used and make it available for the next process. While the program is running, the OS has to allocate memory between many different processes. On my computer, right now, I have over 200 processes running. Most of them are handled by the OS, but the others include my email program, my web browser, a few text editors, my desktop manager, and many others. The OS manages the memory allocation. As far as Python is concerned, it manages its own memory from what the OS gives it. When you assign a value: name = "Inigo Montoya" the Python interpreter allocates a chunk of memory in the memory heap to hold the string. It then tracks whether or not the string is being used. So long as the string is being used by your program, or *could possibly* be used, Python will hold onto that string, forever. But as soon as it sees that it can no longer be used, it will free the memory and reuse it. This process is called "garbage collection". You can google for more information, or ask here. Different Python interpreters use different garbage collectors: IronPython uses the .Net garbage collector; Jython uses the Java garbage collector; PyPy has a few different ones that you can choose from; and the CPython (that's the standard Python you are probably running) interpreter has two, a simple "reference counter" GC that works very fast but not very thoroughly, and a more thorough GC that picks up anything the reference counter can't handle. (Mostly reference cycles: if one object has a reference to another, and that second object also has a reference to the first, that's a cycle. The reference counter can't deal with that, but the second GC can.) Let's track the life-span of a chunk of memory. Suppose you write the following code in a module: name = "Inigo Montoya" print(name) name = "The Dread Pirate Roberts" The second assignment frees up the string "Inigo Montoya", as no part of your program can possibly access the old value any more, since it has been replaced by the new value. So the garbage collector frees that chunk of memory and makes it available for something else. This happens automatically, and virtually instantly. You never need to care about allocating or deallocating memory. The interpreter has its own memory manager to do that, with a garbage collector to deallocate memory. So, when do you need to care about memory? - If you are writing a C extension, you have to manage your own memory. - If you're using the ctypes module, you have access to the C code of the interpreter, so you have to care about managing your own memory. - If you're creating massively huge strings or lists, you might have to worry about running out of memory. For example, I once locked up my computer by foolishly creating a HUGE list: # Don't try this at home! L = list(range(100**100)) That would need to find enough memory for a list with one hundred trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion trillion entries. Each entry will take at least four bytes, so the total is ... well, it's a lot. Much more than my poor computer has. On Windows, this will fail quite quickly with a MemoryError, no real harm done, but on Linux (which I use) the OS will gamely, or perhaps stupidly, try very hard to allocate a trillion trillion trillion ... trillion terabytes of memory, locking up my computer. (I let it run overnight, and eventually needed to just pull the plug to reset it.) Fortunately, even on Linux there are ways to tell the OS not to be so stupid, which means that Python will raise a MemoryError and no real harm is done, but at the time I didn't know about them. So that's about it really... if you're writing long-lasting server class programs, you might need to care about memory; if you're trying to process huge files bigger than the amount of RAM you have, you need to think about memory... but most of the time, just write your code and let the Python garbage collector manage it for you. Feel free to ask questions if anything is unclear! -- Steve _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ____________________________________________________________ Health News 24 (Sponsored by Content.Ad) Granny Reveals Her Method: Don't Use Botox, Do This Instead http://thirdpartyoffers.netzero.net/TGL3241/57ca42ebce29042eb0747st02duc From sharad1087 at gmail.com Sat Sep 3 01:55:07 2016 From: sharad1087 at gmail.com (Sharad Singla) Date: Sat, 3 Sep 2016 11:25:07 +0530 Subject: [Tutor] What's the correct way to define/access methods of a member variable in a class pointing to an object? Message-ID: Hi Pythonistas What's the correct way to define/access methods of a member variable in a class pointing to an object? For example, I have a class Foo that has a method foo_method: class Foo: def foo_method(self): return 'bar' Now, in another class Bar, I'd like to store an object to this class (I do not want Bar to inherit Foo). What is the correct/recommended way to define class Bar? class Bar: def __init__(self): self.foo = Foo() # OR class Bar: def __init__(self): self.foo = Foo() def bar_method(self): return self.foo.bar() The former will allow me to use: x = Bar() x.foo.foo_method() But, with this I'm directly accessing methods of a member variable (strong coupling). The benefits I see with this approach are: - I don't have to wrap every new method that gets added to class Foo. - Bar may contain more member variables pointing to other classes. Not wrapping them keeps Bar smaller and manageable in size. - The auto-completion facility from IDE (PyCharm, etc.) or IPython helps inspect bar like a menu (x.foo) followed by a sub-menu ( x.foo.foo_method(), x.bar.foobar(), etc.) making it easier to develop code. - Functional programming look-n-feel (not sure if this a pro or con) The cons are strong coupling, not encapsulating internal details of foo, etc. I wanted to check if this a recommended practice? And/or if there are any guidelines related to this (kind of implementing a composite pattern)? There may be more classes (Bat(), etc.) in future and I'd like to extend Foo in future to store an object of these classes. Foo() is a part of a library and my intent is to make Foo() as the entry point for the target users (to allow them auto-completion via dotted notation) rather than having them remember all the classes Bar(), Bat(), etc. Any inputs/pointers will be highly appreciated! Regards Sharad PS> I've posted this in SO at http://stackoverflow.com/questions/39231932/whats-the-correct-way-to-access-methods-of-a-member-variable-in-a-class-pointin/39237874#39237874 but I'm looking for more information/ideas/thoughts/opinions on this. From steve at pearwood.info Sat Sep 3 07:16:18 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 3 Sep 2016 21:16:18 +1000 Subject: [Tutor] What's the correct way to define/access methods of a member variable in a class pointing to an object? In-Reply-To: References: Message-ID: <20160903111618.GE26300@ando.pearwood.info> On Sat, Sep 03, 2016 at 11:25:07AM +0530, Sharad Singla wrote: > Hi Pythonistas > > What's the correct way to define/access methods of a member variable in a > class pointing to an object? Python recommends that you start with the simplest thing that will work first, which is direct attribute access, and only do something more complex if and when you need to. Python makes it easy to change the implementation without changing the interface! See more comments below. > For example, I have a class Foo that has a method foo_method: > > class Foo: [snip implementation of class Foo] > Now, in another class Bar, I'd like to store an object to this class (I do > not want Bar to inherit Foo). > > What is the correct/recommended way to define class Bar? > > class Bar: > def __init__(self): > self.foo = Foo() Usually this one, with a few exceptions. > The former will allow me to use: > > x = Bar() > x.foo.foo_method() Correct. > But, with this I'm directly accessing methods of a member variable (strong > coupling). This is true, but the warning against coupling is a little more subtle than just "don't do it". The problem is that often you have to duplicate the interface to avoid coupling the *interface* of Bar class to Foo class. Imagine if Foo has, not one method, but fifty: class Bar: def __init__(self): self._foo = Foo() # Keep it private. def bar_method(self): return self._foo.bar_method() def baz_method(self): return self._foo.baz_method() def bing_method(self): return self._foo.bing_method() def spam_method(self): return self._foo.spam_method() def eggs_method(self): return self._foo.eggs_method() def cheese_method(self): return self._foo.cheese_method() ... and so on, for 44 more duplicate methods. At this point, you should ask yourself: what is Bar class adding? It's just a middle-man, which adds complexity to your code and run-time inefficiency. (In Java, middle-man classes still add complexity, but they don't add run-time ineffeciency. The Java compiler can resolve a long chain of dot accesses like: instance.foo.bar.baz.foobar.fe.fi.fo.fum.spam.eggs.cheese.ardvaark_method() into a fast and efficient call direct to ardvaark_method(), but in Python every one of those dots has to be resolved at runtime. You really don't want enormously long chains of dot method calls in Python! A few dots is fine, but don't write Java style.) So middle-man classes have a cost, and they have to pay their way. If they don't pay their way, it is better to dump them, and just work with Foo directly. The same goes for attribute access: you have to weigh up the added complexity of hiding the foo attribute against the benefit gained, and only hide it behind getter and setter methods if you really need to. Python also takes the philosophy that public attributes are usually a good thing. You will often see classes where an attribute is a dict, or a list. For example, in Python 3, there is a class collections.ChainMap which stores a list of dicts. Rather than doing something like this: class ChainMap: def list_sort(self): self._maps.sort() def list_reverse(self): self._maps.reverse() def list_get(self, i): return self._maps[i] # etc. the class just exposes the list directly to the caller. Its a list. You know how to sort lists, and extract items. There's no need to hide it. The interface will never change, it will always be a list. Just access the "maps" attribute, and work with it directly. But generally this applies to relatively simple objects with well-known interfaces, like lists, dicts, and so forth. You might find you have good reason to hide Foo behind a middle-man. Perhaps Foo has a complex, ugly, un-Pythonic interface and you want to provide a more pleasant interface. Or perhaps you want the freedom to change the Foo interface at will, without changing the Bar interface. So while it is *usual* to just do direct attribute access in Python, it is not forbidden to do it the other way. Just make sure you have a good reason, better than "because that's what my Comp Sci 101 professor told me to always do". > The benefits I see with this approach are: > > - I don't have to wrap every new method that gets added to class Foo. > - Bar may contain more member variables pointing to other classes. Not > wrapping them keeps Bar smaller and manageable in size. > - The auto-completion facility from IDE (PyCharm, etc.) or IPython helps > inspect bar like a menu (x.foo) followed by a sub-menu ( > x.foo.foo_method(), x.bar.foobar(), etc.) making it easier to develop > code. > - Functional programming look-n-feel (not sure if this a pro or con) Exactly. That's why Python programmers tend to expose attributes as part of the public interface by default, and only hide them if justified by other concerns. > The cons are strong coupling, not encapsulating internal details of foo, > etc. Right. And sometimes you really do want to encapsulate the internal details of Foo. In Java, programmers learn to hide attributes by default, because if you expose them and then decide you need to hide the internal details of Foo, you are screwed, you can't do it. But Python lets you do it. Suppose we stick to the initial plan, and just say: class Bar(object): def __init__(self): self.foo = Foo() # Make it public. so that the user can say bar.foo.bar_method(). But then some day you realise that you need to change the Foo interface. Perhaps bar_method has to take a mandatory argument where before it took none. How can we change Foo without changing Bar, now that we exposed it? In Java, you can't, or at least not easily, hence Java programmers learn to always hide everything by default. But in Python, properties to the rescue! Properties only work in "new-style" classes. In Python 3, you don't have to change anything, but in Python 2 we need to inherit from object. So let's start by inheriting from object (which you should be doing anyway), and changing our *public* foo to a *private* foo: class Bar(object): def __init__(self): self._foo = Foo() # Make it private. Now let's add a "foo" property. @property def foo(self): return FooProxy(self._foo) Now we need a simple FooProxy class to wrap the real Foo, hiding the bits we want hidden and exposing the bits we can: class FooProxy(object): def __init__(self, foo): self.foo = foo def bar_method(self): # bar_method used to take no arguments. return self.foo.bar_method("some", "arguments") # Any other methods that we have to change the implementation? def baz_method(self): # Fix a bug in the class. try: return self.foo.baz_method() except SomeError: return "baz baz baz" # For everything else, delegate to the real foo. def __getattr__(self, name): return getattr(self.foo, name) def __setattr__(self, name, value): setattr(self.foo, name, value) def __delattr__(self, name): delattr(self.foo, name) And so with a small amount of extra work, and a little bit of runtime cost, we have full information hiding of Foo, but as far as the caller is concerned, it is as if we have exposed the Foo instance directly. The caller simply writes: instance = Bar() instance.foo.bar_method() exactly the same as before. (P.S. I haven't tested the above code, it is possible there may be minor errors or typos.) But you should only do this if you are sure you need to. Otherwise, just use self.foo = Foo(). You can always add the extra layer of indirect code later, when you need it. -- Steve From __peter__ at web.de Sat Sep 3 08:26:12 2016 From: __peter__ at web.de (Peter Otten) Date: Sat, 03 Sep 2016 14:26:12 +0200 Subject: [Tutor] python memory management References: <20160902.202520.22050.0@webmail13.dca.untd.com> Message-ID: monikajg at netzero.net wrote: > > By: > "reference cycles: if one object has a reference to another, and > that second object also has a reference to the first, that's a cycle." > > Is this what you mean? > a = 5 > b = a > a = b No. int instances are immutable. The assignments above bind both /names/ a and b to the same instance of int -- 5. The last statement a = b is redundant as a is already bound to 5. For a reference cycle you need something in an object x to refer to another y and vice versa. x and y here are not Python names, but the actual objects. An example using lists: >>> a = ["a"] >>> b = ["b"] Let's append b to a: >>> a.append(b) >>> a[1] ['b'] >>> a[1][1] Traceback (most recent call last): File "", line 1, in IndexError: list index out of range We don't have a cycle yet, so Python eventually complains. Now let's complete the cycle by appending b to a. >>> b.append(a) >>> a[1][1] ['a', ['b', [...]]] >>> a[1][1][1][1][1] ['b', ['a', [...]]] We can keep accessing x[1] forever, and if Python weren't smart enough to detect the cycle instead of producing the [...] it would continue building the string representation for the nested lists until all available memory was consumed or the stack overflowed. Another example with a custom class: >>> class C: ... def __init__(self, name): self.name = name ... def __repr__(self): return self.name ... >>> a = C("a") >>> b = C("b") >>> a a >>> b b >>> a.b = b >>> b.a = a >>> a.b.a.b.a.b.a.b.a.b.a a A cycle may of course consist of more than two objects: >>> a = C("a") >>> b = C("b") >>> c = C("c") >>> a.b = b >>> b.c = c >>> c.a = a >>> c.a.b.c c >>> c.a.b.c.a.b.c.a.b.c c The minimal cycle consists of an object referencing itself: >>> a = C("a") >>> a.a = a >>> a.a.a.a.a.a.a a From random832 at fastmail.com Sat Sep 3 05:51:05 2016 From: random832 at fastmail.com (Random832) Date: Sat, 03 Sep 2016 05:51:05 -0400 Subject: [Tutor] python memory management In-Reply-To: <20160902.202520.22050.0@webmail13.dca.untd.com> References: <20160902.202520.22050.0@webmail13.dca.untd.com> Message-ID: <1472896265.3360957.714620537.3A55E043@webmail.messagingengine.com> On Fri, Sep 2, 2016, at 23:25, monikajg at netzero.net wrote: > > By: > "reference cycles: if one object has a reference to another, and > that second object also has a reference to the first, that's a cycle." > > Is this what you mean? > a = 5 > b = a > a = b > > I just want to make sure I understand. > Thank you very much for your explanation. > Monika No, an object that holds reference to other objects is something like a list, tuple, dict. e.g. if you had: a = [] b = [a] a.append(b) you've got two lists, and each one has a reference to the other one. These exist independent of the variables - you could del b and it would still exist in a[0]. From zemmoura.khalil at gmail.com Sat Sep 3 07:42:31 2016 From: zemmoura.khalil at gmail.com (khalil zakaria Zemmoura) Date: Sat, 3 Sep 2016 12:42:31 +0100 Subject: [Tutor] What's the correct way to define/access methods of a member variable in a class pointing to an object? In-Reply-To: References: Message-ID: Hi, Composition is a technique that allows you to express the 'has a' relationship between your object. I'm not a specialist of the OOP but I can see some issues in the model that you expose in your mail. Encapsulation and abstraction are all about designing an API. It allows you to expose simple usable API'S and hiding all the complexity from the user of your classes. If a user have to use your classes in inheritance or composition, he/she doesn't have to worry about the internal details. Wrapping methods of class Foo is a good thing I think. In the case you don't use encapsulation: If Foo is used in Bar, Bat, Baf and after that, the three classes are used anywhere in your code, if you change the API of Foo, you'll end up making changes in all the places where Foo is mentioned! and that's tedious Now, if you use encapsulation The same scenario is not a big deal anymore because you'll end up making less changes than before. That's makes the life easier Abstracting irrelevant details also makes the process of developing easier because you don't have to deal with low level of details to make a code working and helps you to reason about your code in a more efficient way. That techniques lower the level of complexity you have to deal with. I hope that helps a little bit. Regards. Le 3 sept. 2016 10:24, "Sharad Singla" a ?crit : > Hi Pythonistas > > What's the correct way to define/access methods of a member variable in a > class pointing to an object? > > For example, I have a class Foo that has a method foo_method: > > class Foo: > def foo_method(self): > return 'bar' > > Now, in another class Bar, I'd like to store an object to this class (I do > not want Bar to inherit Foo). > > What is the correct/recommended way to define class Bar? > > class Bar: > def __init__(self): > self.foo = Foo() > # OR > class Bar: > def __init__(self): > self.foo = Foo() > > def bar_method(self): > return self.foo.bar() > > The former will allow me to use: > > x = Bar() > x.foo.foo_method() > > But, with this I'm directly accessing methods of a member variable (strong > coupling). > > The benefits I see with this approach are: > > - I don't have to wrap every new method that gets added to class Foo. > - Bar may contain more member variables pointing to other classes. Not > wrapping them keeps Bar smaller and manageable in size. > - The auto-completion facility from IDE (PyCharm, etc.) or IPython helps > inspect bar like a menu (x.foo) followed by a sub-menu ( > x.foo.foo_method(), x.bar.foobar(), etc.) making it easier to develop > code. > - Functional programming look-n-feel (not sure if this a pro or con) > > The cons are strong coupling, not encapsulating internal details of foo, > etc. > > I wanted to check if this a recommended practice? And/or if there are any > guidelines related to this (kind of implementing a composite pattern)? > > There may be more classes (Bat(), etc.) in future and I'd like to extend > Foo in future to store an object of these classes. Foo() is a part of a > library and my intent is to make Foo() as the entry point for the target > users (to allow them auto-completion via dotted notation) rather than > having them remember all the classes Bar(), Bat(), etc. > > Any inputs/pointers will be highly appreciated! > > > Regards > > Sharad > > > PS> I've posted this in SO at > http://stackoverflow.com/questions/39231932/whats-the- > correct-way-to-access-methods-of-a-member-variable-in-a- > class-pointin/39237874#39237874 > but I'm looking for more information/ideas/thoughts/opinions on this. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Sat Sep 3 09:51:22 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 3 Sep 2016 14:51:22 +0100 Subject: [Tutor] What's the correct way to define/access methods of a member variable in a class pointing to an object? In-Reply-To: References: Message-ID: On 03/09/16 06:55, Sharad Singla wrote: > What's the correct way to define/access methods of a member variable in a > class pointing to an object? Steven has already given you a long and comprehensive answer based on pragmatic python programming. But since you use the term "correct" I'll give you an alternative view based on generic/theoretical OOP practice. The caveat is that theoretical best practice does not always transfer to the real world and you must compromise. Just ensure you compromise in the full knowledge that it is a compromise... The first thing to do is point out that what you are asking about is the Law of Demeter on OOP. See Wikipedia for a full description. In essence it says that the user of an object should not directly access that object's internal attributes/state. So ideally you only access foo via a call to a method of Bar. BUT... that does not mean you should expose all of foo's interface as methods of Bar. After all Bar should only have an instance of Foo to support its own behaviour. In other words whatever methods you call on Bar may manipulate Foo *behind the scenes*. (If Foo is not being used by Foo's methods then what is it doing in Bar at all?!) So if you really want to change some aspect of Bar's Foo instance then it should only be happening as a result of you doing something to your Bar instance. If you really, really need to modify the Foo instance directly (and this should be fairly rare) the "correct" OOP way to do that is for Bar to provide a getFoo() accessor method and for you to fetch the Foo instance and then manipulate it directly b = Bar() f = b.getFoo() f.someMethod() f.someAttribute = 42 # etc... b.setFoo(f) # put it back when you are done. This then keeps Bar's use of foo hidden (you don't know if Bar is returning a reference to its internal Foo or creating a copy for you to play with.) In python we tend to use direct access rather than getFoo/setFoo. We'd just say b.foo, but that means if Bar wants to return a copy of Foo then it needs to implement foo as a property. So to summarise, the Law of Demeter states that you should provide foo access via Bar's interface but the expectation is that you only create operations(methods) on Bar that modify Foo as a part of that operation, not that you simply expose all of Foo via Bar. One huge exception to all of this is where you create Bar explicitly as a manager of Foo objects and its role is only to provide access to Foos (an example might be an object pool in a server application). But in that case there probably should be get/set methods which will do some sanity checking before handing over a Foo to you to play with, or storing a foo that you have been messing about with. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Sat Sep 3 09:56:25 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 3 Sep 2016 14:56:25 +0100 Subject: [Tutor] python memory management In-Reply-To: <20160902.202520.22050.0@webmail13.dca.untd.com> References: <20160902.202520.22050.0@webmail13.dca.untd.com> Message-ID: On 03/09/16 04:25, monikajg at netzero.net wrote: > Is this what you mean? > a = 5 > b = a > a = b No, you are confusing variable names with objects. Here you only have one object - the number 5. For a cycle you need at least 2 objects and those objects must be able to reference another object. In practice that means a collection or an instance of a class. a = [] b = [a] a.append(b) The two lists are now cyclically referring to each other. We can now delete the names a and b and the two list objects will continue to exist in memory even though no variables refer to them. This is where the second garbage collector comes into play, it can recognise the link between the lists and the fact that no variable refers to them. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Sat Sep 3 12:35:24 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 4 Sep 2016 02:35:24 +1000 Subject: [Tutor] python memory management In-Reply-To: <20160901.132136.4753.0@webmail01.dca.untd.com> References: <20160901.132136.4753.0@webmail01.dca.untd.com> Message-ID: <20160903163524.GF26300@ando.pearwood.info> On Thu, Sep 01, 2016 at 08:21:36PM +0000, monikajg at netzero.net wrote: > Thank you for your explanation. It is very clear and confirms what I > thought I knew. However, I had a job interview and the interview said > it was a mistake that I did not say that in cases when there are > multiple programmers, there might be some objects/references left and > not deallocated from the memory by python. He asked me how this should > be handles. Without being there, I cannot be sure precisely what he meant. But frankly it sounds like the interviewer doesn't know what he is talking about. The number of programmers who work on a project has nothing to do with how it manages memory. Whether you have one person who writes the code, or a hundred people writing the code, memory is managed exactly the same way. Perhaps you misunderstood the question, or perhaps the interviewer simply wasn't as knowledgable or smart as he thought he was. > So I told him that python has its automatic garbage collection which > keeps track of all objects and references and deletes them as > appropriate (similar what you state). I added that programmers can > keep track of occurrences of object and make sure that it goes down to > 0. And if it does not then use del to delete. However, he did not like > my answer. What you say is technically correct, but if you find yourself using del more than very occasionally, I suggest that you need to think hard about what you are doing. It is not normally necessary to use del. > So Im trying to learn from my mistakes and learn if an > object or reference are still at the end of the program how they > should be deleted. A thought comes to mind... When Python shuts down, it has to deallocate all your remaining objects, giving them a chance to run any finaliser methods. But sometimes the finaliser methods don't get to run until Python has already deallocated the global variables, including modules you might want to use. So for example: import math class Widget: def __del__(self): # Finaliser print(math.sin(10)) but if your widget doesn't get deallocated until the end of your application, it may be that math has been set to None and math.sin is no longer available. Perhaps he was asking about that? What sort of job were you asking for? I expect that would be counted as an extremely advanced corner of Python, not something that most people know or need to know. > I had problem understanding him too well since he > was calling from overseas (there was interference) and I could not > understand his accent well. But I need to learn this for future > interviews. > > Your posting states that this is not a problem but according to the > interviewer it is. (I do not mean to be un-nice to you, sorry) So how > this situation should be handled? There's no way to know the right way. Some interviewers think they know more than they actually do. When you tell them the right answer, they think it is wrong because they are ignorant. Some interviewers are deliberately testing you with really obscure, complex corners of the language. Maybe he was right, and there is some tiny corner where what I told you was incorrect. Maybe you misunderstood the question. Maybe the interviewer was deliberately testing you with false information, to see what you would do. Would you argue with him? Get frustrated? Agree with him even if you knew the answer was actually wrong? There's no way of knowing what they are looking for. Do they want people who will push back and stand their ground when they know they are right, or people who will be good little employees who will agree when the boss says that milk is purple and England is the capital of China? I guess the only thing you can do is answer as best you can, ask for clarifications, and hope. -- Steve From steve at pearwood.info Sat Sep 3 12:52:45 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 4 Sep 2016 02:52:45 +1000 Subject: [Tutor] python projects In-Reply-To: <20160831.223533.19014.0@webmail04.dca.untd.com> References: <20160831.223533.19014.0@webmail04.dca.untd.com> Message-ID: <20160903165245.GG26300@ando.pearwood.info> On Thu, Sep 01, 2016 at 05:35:33AM +0000, monikajg at netzero.net wrote: > Hi: > I have been taking python classes for overa year now and studying and > studying it all days long. However, I still cannot get a job as a > python automation qa (despite of many years of experience in qa) > because everybody is looking for a senior python developers for > automation qa jobs. Entry level positions are rare. Is there a website > where I could maybe do some python projects for somebody, for free or > for low pay? Pretty soon I will have to take a survival job since my > savings are ending and when that happens I will not have much time to > practice python and all my hard work will go to waste. I really, really feel sympathy for you. I think that a lot of people are in situations like yours. I am lucky enough to have a job that gives me time to practice Python, but that is the silver lining in a dark cloud: I have time to practice Python because I only work part time (not by choice). Very occasionally I get to use my Python skills as part of my work, but not often. So I share your pain. I'll be honest, I'm not sure what software QA is supposed to do, or what skills you have, so I'll have to guess. I don't know of any *paid* Python projects looking with entry-level QA positions, I am sorry, but you might consider helping with some open-source projects or even the Python language itself. The work is unpaid, but you can list it on your CV and gain experience. Pick some open-source Python projects run by volunteers, and see if they need help with testing and QA. Most small projects need help with testing and documentation -- there are lots of programmers who can write code but are no good (or, let's be honest, too lazy) to write tests and documentation. Do you have lots of experience with a testing framework like Nose? Maybe you can volunteer to work on the framework, and get some skills and useful contacts and something to put on your CV. Again, as these projects are all run by volunteers, there is unlikely to be any pay for it. Or look at the Python bug tracker: http://bugs.python.org/ Can you find some issues that you can work on? Bugs that you can fix? Find issues that are waiting for tests and write some tests, and put them on the bug tracker. As a beginner, expect that senior developers will want to check your work. Sometimes they are extremely busy and patches languish in the bug tracker, waiting for somebody to review them. More about contributing to Python here: https://docs.python.org/devguide/#contributing Don't forget the Job Board: https://jobs.python.org/ And good luck! -- Steve From steve at pearwood.info Sat Sep 3 13:17:36 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 4 Sep 2016 03:17:36 +1000 Subject: [Tutor] What's the correct way to define/access methods of a member variable in a class pointing to an object? In-Reply-To: References: Message-ID: <20160903171735.GH26300@ando.pearwood.info> On Sat, Sep 03, 2016 at 02:51:22PM +0100, Alan Gauld via Tutor wrote: [...] > The first thing to do is point out that what you > are asking about is the Law of Demeter on OOP. > See Wikipedia for a full description. In essence > it says that the user of an object should not > directly access that object's internal attributes/state. > So ideally you only access foo via a call to a method of > Bar. That's a very important answer too, thanks Alan for reminding me of it, but remember that the Law of Demeter is more of a guideline, and we should understand it, not just blindly quote it. One classic example of the Law Of Demeter is: "If you want your dog to come to you, don't talk to your dog's legs, talk to the dog." class Dog: ... fido = Dog() # I want the dog to come here. fido.left_front_leg.lift() fido.left_front_leg.forward() fido.left_front_leg.down() fido.right_back_leg.lift() ... Arggh, no, that's terrible! That violates the Law Of Demeter, and you shouldn't expect your users to do that. The caller shouldn't have to care about *how the dog walks*. fido.command("come here boy!") Or perhaps: fido.heel() whatever is most appropriate. The details of how the dog walks is encapsulated inside the Dog class. Another classic example is of a transaction where the paper boy gives you a newspaper and you pay him a dollar: newspaper = paperboy.get_paper() paperboy.balance += 1.0 customer.trousers.wallet.balance -= 1.0 No! Would you really let the paperboy reach into your trousers, grab your wallet, open it up, and take money out? Again, another Demeter violation. Better to re-write your Customer class: class Customer: def pay(self, amount): if amount > self.wallet.balance: raise ValueError self.wallet.balance -= amount return amount paperboy.receive(customer.pay(1.0)) But sometimes the Law Of Demeter should not apply. Sometimes you are expected to tinker with the internals of an object. car = Car() # replace the engine with a bigger one car.engine = Engine("800 horsepower of throbbing nitro-fueled POWER") car.engine.inject(nitro=True) but as I suggested in an earlier email, that mostly applies when the attribute is a fairly simple object like a list, a dict, or similar. -- Steve From alan.gauld at yahoo.co.uk Sat Sep 3 14:23:48 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 3 Sep 2016 19:23:48 +0100 Subject: [Tutor] What's the correct way to define/access methods of a member variable in a class pointing to an object? In-Reply-To: <20160903171735.GH26300@ando.pearwood.info> References: <20160903171735.GH26300@ando.pearwood.info> Message-ID: On 03/09/16 18:17, Steven D'Aprano wrote: > One classic example of the Law Of Demeter is: > > "If you want your dog to come to you, don't talk to your dog's legs, > talk to the dog." I love that, I've never seen it before but a great example. > But sometimes the Law Of Demeter should not apply. Sometimes you are > expected to tinker with the internals of an object. > > car = Car() > # replace the engine with a bigger one > car.engine = Engine("800 horsepower of throbbing nitro-fueled POWER") > car.engine.inject(nitro=True) Yep, although in that case I'd take the engine "out of the car" engine = car.engine engine.inject() car.engine = engine Of course in python(or Java) the last line is usually not needed because we work with references to the real object rather than copies... Its not necessary to get the extra reference but for me I like the code to reflect the intent as well as the need. So by creating an external reference to the engine it reminds me that I am tinkering with a different object to the car. -- 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 chidinma.ikebudu at yahoo.com Sat Sep 3 17:45:17 2016 From: chidinma.ikebudu at yahoo.com (chidinma.ikebudu at yahoo.com) Date: Sat, 3 Sep 2016 21:45:17 +0000 (UTC) Subject: [Tutor] 'int' object has no attribute 'items' References: <1841421105.1147906.1472939117076.ref@mail.yahoo.com> Message-ID: <1841421105.1147906.1472939117076@mail.yahoo.com> Hello,Am trying to solve a problem, but keep getting different errors. Country X calculates tax for its citizens using a graduated scale rate as shown below: - Yearly Income:?0 - 1000Tax Rate:?0% - Yearly Income:?1,001 - 10,000Tax Rate:?10% - Yearly Income:?10,001 - 20,200Tax Rate:?15% - Yearly Income:?20,201 - 30,750Tax Rate:?20% - Yearly Income:?30,751 - 50,000Tax Rate:?25% - Yearly Income:?Over 50,000Tax Rate:?30% Am trying to write a Python function that will calculate tax rate. def calculate_tax(dict_inp):? result = {}? if dict_inp == {}:? ? result = "Please enter valid inputs"? else:? ? for k, v in dict_inp.items():? ? ? try:? ? ? ? x = int(dict_inp[k])? ? ? except ValueError:? ? ? ? print("That's not an int!")? ? ? ? break? ? ? if(x):? ? ? ? if x > 50000:? ? ? ? ? tax = ((x - 50000) * 0.3) + 4812.5 + 2110 + 1530 + 900? ? ? ? ? result[k] = tax? ? ? ? elif x > 30750:? ? ? ? ? tax = ((x - 30750) * 0.25) + 2110 + 1530 + 900? ? ? ? ? result[k] = tax? ? ? ? elif x > 20200:? ? ? ? ? tax = ((x - 20200) * 0.2) + 1530 + 900? ? ? ? ? result[k] = tax? ? ? ? elif x > 10000:? ? ? ? ? tax = ((x - 10000) * 0.15) + 900? ? ? ? ? result[k] = tax? ? ? ? elif x > 1000:? ? ? ? ? tax = ((x - 1000) * 0.1)? ? ? ? ? result[k] = tax? ? ? ? else:? ? ? ? ? tax = 0? ? ? ? ? result[k] = tax? ? ? else:? ? ? ? print("Yearly income is not an integer")? ? return result? ??dict_inp = {'Alex': 500,'James': 20500,'Kinuthia': 70000}#dict_inp = {200: 1500,300: 20500,400: 70000}print(calculate_tax(dict_inp)) But I get the result: THERE IS AN ERROR/BUG IN YOUR CODE Results:?Internal Error: runTests aborted: TestOutcomeEvent(handled=False, test=, result=, outcome='error', exc_info=(, AttributeError("'int' object has no attribute 'items'",), ), reason=None, expected=False, shortLabel=None, longLabel=None) is not JSON serializable{'James': 2490.0, 'Alex': 0, 'Kinuthia': 15352.5} But when i take away the .items(), i get: for k, v in dict_inp: ValueError: too many values to unpack Please I need help.I am using windows 10, python 3.5.2, Thanks From monikajg at netzero.net Sat Sep 3 17:16:52 2016 From: monikajg at netzero.net (monikajg at netzero.net) Date: Sat, 3 Sep 2016 21:16:52 GMT Subject: [Tutor] python memory management Message-ID: <20160903.141652.10011.0@webmail09.dca.untd.com> So what does [...] mean? ---------- Original Message ---------- From: Peter Otten <__peter__ at web.de> To: tutor at python.org Subject: Re: [Tutor] python memory management Date: Sat, 03 Sep 2016 14:26:12 +0200 monikajg at netzero.net wrote: > > By: > "reference cycles: if one object has a reference to another, and > that second object also has a reference to the first, that's a cycle." > > Is this what you mean? > a = 5 > b = a > a = b No. int instances are immutable. The assignments above bind both /names/ a and b to the same instance of int -- 5. The last statement a = b is redundant as a is already bound to 5. For a reference cycle you need something in an object x to refer to another y and vice versa. x and y here are not Python names, but the actual objects. An example using lists: >>> a = ["a"] >>> b = ["b"] Let's append b to a: >>> a.append(b) >>> a[1] ['b'] >>> a[1][1] Traceback (most recent call last): File "", line 1, in IndexError: list index out of range We don't have a cycle yet, so Python eventually complains. Now let's complete the cycle by appending b to a. >>> b.append(a) >>> a[1][1] ['a', ['b', [...]]] >>> a[1][1][1][1][1] ['b', ['a', [...]]] We can keep accessing x[1] forever, and if Python weren't smart enough to detect the cycle instead of producing the [...] it would continue building the string representation for the nested lists until all available memory was consumed or the stack overflowed. Another example with a custom class: >>> class C: ... def __init__(self, name): self.name = name ... def __repr__(self): return self.name ... >>> a = C("a") >>> b = C("b") >>> a a >>> b b >>> a.b = b >>> b.a = a >>> a.b.a.b.a.b.a.b.a.b.a a A cycle may of course consist of more than two objects: >>> a = C("a") >>> b = C("b") >>> c = C("c") >>> a.b = b >>> b.c = c >>> c.a = a >>> c.a.b.c c >>> c.a.b.c.a.b.c.a.b.c c The minimal cycle consists of an object referencing itself: >>> a = C("a") >>> a.a = a >>> a.a.a.a.a.a.a a _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ____________________________________________________________ howlifeworks.com (Sponsored by Content.Ad) Why Women Are Flocking to This Incredible New Shopping Site http://thirdpartyoffers.netzero.net/TGL3241/57cb3e00e29ee3e000c94st01duc From sharad1087 at gmail.com Sat Sep 3 14:01:52 2016 From: sharad1087 at gmail.com (Sharad Singla) Date: Sat, 3 Sep 2016 23:31:52 +0530 Subject: [Tutor] What's the correct way to define/access methods of a member variable in a class pointing to an object? In-Reply-To: References: Message-ID: @Steven, @Khalil, @Alan Thanks for the inputs/explanations. Appreciate it! Regards Sharad From zemmoura.khalil at gmail.com Sat Sep 3 18:20:07 2016 From: zemmoura.khalil at gmail.com (zakaria) Date: Sat, 03 Sep 2016 23:20:07 +0100 Subject: [Tutor] python memory management In-Reply-To: References: <20160902.202520.22050.0@webmail13.dca.untd.com> Message-ID: <1472941207.1241.1.camel@gmail.com> Is there any practical usage of using reference cycling? On Sat, 2016-09-03 at 14:56 +0100, Alan Gauld via Tutor wrote: > On 03/09/16 04:25, monikajg at netzero.net wrote: > > > > > Is this what you mean?? > > a = 5 > > b = a > > a = b > > No, you are confusing variable names with objects. > Here you only have one object - the number 5. > For a cycle you need at least 2 objects and those > objects must be able to reference another object. > In practice that means a collection or an instance > of a class. > > a = [] > b = [a] > a.append(b) > > The two lists are now cyclically referring to each > other. We can now delete the names a and b and > the two list objects will continue to exist > in memory even though no variables refer to them. > This is where the second garbage collector comes > into play, it can recognise the link between the > lists and the fact that no variable refers to them. > From alan.gauld at yahoo.co.uk Sat Sep 3 20:25:43 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 4 Sep 2016 01:25:43 +0100 Subject: [Tutor] python memory management In-Reply-To: <20160903.141652.10011.0@webmail09.dca.untd.com> References: <20160903.141652.10011.0@webmail09.dca.untd.com> Message-ID: On 03/09/16 22:16, monikajg at netzero.net wrote: > So what does [...] mean? Its Python's way of telling you that you have a self referential data structure. Its just a representational thing but without it Python would end up printing an infinite sequence of values. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Sat Sep 3 20:22:27 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 4 Sep 2016 01:22:27 +0100 Subject: [Tutor] python memory management In-Reply-To: <1472941207.1241.1.camel@gmail.com> References: <20160902.202520.22050.0@webmail13.dca.untd.com> <1472941207.1241.1.camel@gmail.com> Message-ID: <57CB6943.2070404@yahoo.co.uk> On 03/09/16 23:20, zakaria wrote: > Is there any practical usage of using reference cycling? There are a (very) few cases where data structures require the creation of cyclic references. One example I've used is in managing comms networks where nodes are multiply and/or cyclically linked and you need to build those linkages into your data model. In those cases you need to manage the cleanup yourself. But most cyclic references occur by accident, the programmer probably didn't intend them to exist but it just so happened that one object points to another which happens to point at another which in turn points at the first. The cycle can span many objects. Think of a directory tree where a deeply nested node has a link pointing back at a root level node. But the root also indirectly points at that low level node by means of the tree structure... As a specific example, It often happens in GUIs where widgets hold references to other widgets higher in the widget parent/child tree. -- 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 Sat Sep 3 21:35:27 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 4 Sep 2016 11:35:27 +1000 Subject: [Tutor] 'int' object has no attribute 'items' In-Reply-To: <1841421105.1147906.1472939117076@mail.yahoo.com> References: <1841421105.1147906.1472939117076.ref@mail.yahoo.com> <1841421105.1147906.1472939117076@mail.yahoo.com> Message-ID: <20160904013527.GK26300@ando.pearwood.info> Hi Chidinma, I'm afraid it is very difficult for me to understand your code, because your email program (Yahoo mail perhaps?) has mangled the code and put it all on one single line: On Sat, Sep 03, 2016 at 09:45:17PM +0000, Chidinma via Tutor wrote: > def calculate_tax(dict_inp):? result = {}? if dict_inp == {}:? ? result = "Please enter valid inputs"? else:? ? for k, v in dict_inp.items():? ? ? try:? ? ? ? x = int(dict_inp[k])? ? ? except ValueError:? ? ? ? print("That's not an int!")? ? ? ? break? ? ? if(x):? ? ? ? if x > 50000:? ? ? ? ? tax = ((x - 50000) * 0.3) + 4812.5 + 2110 + 1530 + 900? ? ? ? ? result[k] = tax? ? ? ? elif x > 30750:? ? ? ? ? tax = ((x - 30750) * 0.25) + 2110 + 1530 + 900? ? ? ? ? result[k] = tax? ? ? ? elif x > 20200:? ? ? ? ? tax = ((x - 20200) * 0.2) + 1530 + 900? ? ? ? ? result[k] = tax? ? ? ? elif x > 10000:? ? ? ? ? tax = ((x - 10000) * 0.15) + 900? ? ? ? ? result[k] = tax? ? ? ? elif x > 1000:? ? ? ? ? tax = ((x - 1000) * 0.1)? ? ? ? ? result[k] = tax? ? ? ? else:? ? ? ? ? tax = 0? ? ? ? ? result[k] = tax? ? ? else:? ? ? ? print("Yearly income is not an integer")? ? return result? ??dict_inp = {'Alex': 500,'James': 20500,'Kinuthia': 70000}#dict_inp = {200: 1500,300: 20500,400: 70000}print(calculate_tax(dict_inp)) You may be able to prevent that by turning of "formatted text", or "rich text", or "HTML email", or whatever your email program calls this feature. > But I get the result: > > THERE IS AN ERROR/BUG IN YOUR CODE How are you running this? Python doesn't normally print "THERE IS AN ERROR/BUG IN YOUR CODE". My guess is that you are using one of the on-line Python courses where you type your code into the web page. Am I right? Which one? > Results:?Internal Error: runTests aborted: TestOutcomeEvent(handled=False, test=, result=, outcome='error', exc_info=(, AttributeError("'int' object has no attribute 'items'",), ), reason=None, expected=False, shortLabel=None, longLabel=None) is not JSON serializable{'James': 2490.0, 'Alex': 0, 'Kinuthia': 15352.5} That error doesn't seem to have anything to do with your code. Are you sure it is connected to the code you give above? If you are using a website, it might be a bug in the website. > But when i take away the .items(), i get: > for k, v in dict_inp: > ValueError: too many values to unpack The first thing to confirm that dict_inp is a dict. Run: print( isinstance(dict_inp, dict) ) just before that "for ..." line. If it prints True, then change the for line to: for k, v in dict_inp.items(): What happens then? From monikajg at netzero.net Sat Sep 3 23:21:25 2016 From: monikajg at netzero.net (monikajg at netzero.net) Date: Sun, 4 Sep 2016 03:21:25 GMT Subject: [Tutor] python memory management Message-ID: <20160903.202125.21622.0@webmail01.dca.untd.com> Thank you all for your explanations. I really enjoy learning about things like that. Monika ---------- Original Message ---------- From: Alan Gauld via Tutor To: zakaria , tutor at python.org Subject: Re: [Tutor] python memory management Date: Sun, 4 Sep 2016 01:22:27 +0100 On 03/09/16 23:20, zakaria wrote: > Is there any practical usage of using reference cycling? There are a (very) few cases where data structures require the creation of cyclic references. One example I've used is in managing comms networks where nodes are multiply and/or cyclically linked and you need to build those linkages into your data model. In those cases you need to manage the cleanup yourself. But most cyclic references occur by accident, the programmer probably didn't intend them to exist but it just so happened that one object points to another which happens to point at another which in turn points at the first. The cycle can span many objects. Think of a directory tree where a deeply nested node has a link pointing back at a root level node. But the root also indirectly points at that low level node by means of the tree structure... As a specific example, It often happens in GUIs where widgets hold references to other widgets higher in the widget parent/child tree. -- 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 ____________________________________________________________ Unlock Visions (Sponsored by Content.Ad) 1 Odd Method 'Restores' Your 20/20 Vision. Try This http://thirdpartyoffers.netzero.net/TGL3241/57cb93756ab9c13757ae2st02duc From __peter__ at web.de Sun Sep 4 05:11:02 2016 From: __peter__ at web.de (Peter Otten) Date: Sun, 04 Sep 2016 11:11:02 +0200 Subject: [Tutor] 'int' object has no attribute 'items' References: <1841421105.1147906.1472939117076.ref@mail.yahoo.com> <1841421105.1147906.1472939117076@mail.yahoo.com> Message-ID: Chidinma via Tutor wrote: > Hello,Am trying to solve a problem, but keep getting different errors. > Country X calculates tax for its citizens using a graduated scale rate as > shown below: > - Yearly Income: 0 - 1000Tax Rate: 0% > - Yearly Income: 1,001 - 10,000Tax Rate: 10% > - Yearly Income: 10,001 - 20,200Tax Rate: 15% > - Yearly Income: 20,201 - 30,750Tax Rate: 20% > - Yearly Income: 30,751 - 50,000Tax Rate: 25% > - Yearly Income: Over 50,000Tax Rate: 30% > Am trying to write a Python function that will calculate tax rate. Perhaps you misunderstood the task and you were asked to write a simpler function that takes a value and calculates the tax. For example, in a country with a rate of 50% for everyone the solution would look like this... # expected? def caculate_tax(income): return 0.5 * income ...but you wrote something similar to def calculate_tax(payer_income_pairs): return { payer: income * 0.5 for payer, income in payer_income_pairs.items()} > def calculate_tax(dict_inp): result = {} if dict_inp == {}: result = > "Please enter valid inputs" else: for k, v in dict_inp.items(): > try: x = int(dict_inp[k]) except ValueError: > print("That's not an int!") break if(x): if x > 50000: Awful. Please change your configuration to preserve newlines before posting more code. From sjeik_appie at hotmail.com Mon Sep 5 08:24:50 2016 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Mon, 5 Sep 2016 12:24:50 +0000 Subject: [Tutor] __init__ In-Reply-To: <20160901012437.GU26300@ando.pearwood.info> References: <20160831152358.GT26300@ando.pearwood.info> , <20160901012437.GU26300@ando.pearwood.info> Message-ID: From: Tutor on behalf of Steven D'Aprano Sent: Thursday, September 1, 2016 1:24 AM To: tutor at python.org Subject: Re: [Tutor] __init__ ? On Wed, Aug 31, 2016 at 06:35:44PM +0000, Albert-Jan Roskam wrote: > > >In Python 3, old-style classes are gone. Even in Python 2, they're > >discouraged. > > This suggests that old-style classes are still used, even in Python 3, doesn't it? > > albertjan at debian:~$ python3.5 > Python 3.5.0 (default, Apr 13 2016, 20:39:27) > [GCC 4.9.2] on linux > Type "help", "copyright", "credits" or "license" for more information. > >>> import re, inspect > >>> re.findall("class.+:", inspect.getsource(tkinter)) > ['class Event:', 'class Variable:', 'class StringVar(Variable):', 'class IntVar(Variable):', 'class DoubleVar(Variable):', 'class BooleanVar(Variable):', 'class Misc:', 'className):', 'class(self):', 'class(self, className, sequence=None, func=None, add=None):', 'class(self, className, sequence):', 'class CallWrapper:', 'class XView:', 'class YView:', 'class Wm:', 'class Tk(Misc, Wm):', 'className):', 'class_tcl):', 'class_py):', "className='Tk', useTk=0):", 'class Pack:', 'class Place:', 'class Grid:', 'class BaseWidget(Misc):', 'classes:', 'classes:', 'class Widget(BaseWidget, Pack, Place, Grid):', 'class Toplevel(BaseWidget, Wm):', 'class Button(Widget):', 'class Canvas(Widget, XView, YView):', 'class Checkbutton(Widget):', 'class Entry(Widget, XView):', 'class Frame(Widget):', "class_' in cnf:", "class' in cnf:", 'class Label(Widget):', 'class Listbox(Widget, XView, YView):', 'class Menu(Widget):', 'class Menubutton(Widget):', 'class Message(Widget):', 'class Radiobutton(Widget ?):', 'class Scale(Widget):', 'class Scrollbar(Widget):', 'class Text(Widget, XView, YView):', 'class _setit:', 'class OptionMenu(Menubutton):', 'class Image:', 'class PhotoImage(Image):', 'class BitmapImage(Image):', 'class Spinbox(Widget, XView):', 'class LabelFrame(Widget):', 'class PanedWindow(Widget):'] No. A bare class with no declared parent has object automatically added, in Python 3. Compare the output of this: class K: ??? pass print(K.__bases__) in Python 2 and Python 3, and you will see () in Python 2 (an empty tuple, i.e. no bases classes) but (,) in Python 3. =====> Aha, thank you, I did not know that. So for Python-3-only code, the idiom is "class SomeClass:", and for Python2/3 code "class SomeClass(object)". What are the most common code changes a developer needs to make when switching from old- to new-style classes? > I sometimes want to use the @property decorator in classes that > inherit from e.g. tkinter.Frame. The getter then works, but the setter > fails without any sign. So I then also inherit from object, as in > > class WickedFrame(tkinter.frame, object): [...] > I do this in Python 2.7. Is this the recommended approach? I also like the fact that I can use super() that way. I believe that is fine. ===> thanks again :-) From monikajg at netzero.net Mon Sep 5 16:59:24 2016 From: monikajg at netzero.net (monikajg at netzero.net) Date: Mon, 5 Sep 2016 20:59:24 GMT Subject: [Tutor] cannot find html file, in the same dir as python file Message-ID: <20160905.135924.6706.0@webmail04.dca.untd.com> Hi: Im trying to learn Selenium with python. I have a html file button.html in the same directory as python file. I have the htnl file on my PC since my html file is much simpler for me to learn from than regular html files on the web. The blow code works with web html files but cannot find my button.html file on my PC. Why? chromedriver = "resources/chromedriver" os.environ["webdriver.chrome.driver"] = chromedriver self.driver = webdriver.Chrome(chromedriver) base_url = 'button.html' self.driver.get(base_url) FileNotFoundError: [WinError 2] The system cannot find the file specified Even though this is in Selenium this is a python question. I think. Thank you very much Monika ____________________________________________________________ Daily News (Sponsored by Content.Ad) How Does This Delhi Student Makes Up to Rs 15000 Per Day? http://thirdpartyoffers.netzero.net/TGL3241/57cddcf17f4b25cf11e5fst02duc From alan.gauld at yahoo.co.uk Mon Sep 5 20:25:27 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 6 Sep 2016 01:25:27 +0100 Subject: [Tutor] cannot find html file, in the same dir as python file In-Reply-To: <20160905.135924.6706.0@webmail04.dca.untd.com> References: <20160905.135924.6706.0@webmail04.dca.untd.com> Message-ID: On 05/09/16 21:59, monikajg at netzero.net wrote: > chromedriver = "resources/chromedriver" > os.environ["webdriver.chrome.driver"] = chromedriver > self.driver = webdriver.Chrome(chromedriver) > base_url = 'button.html' > self.driver.get(base_url) > > FileNotFoundError: [WinError 2] The system cannot find the file specified > Even though this is in Selenium this is a python question. I think. Have you tried giving it the full path? Have you tried asking Python where it is looking, eg print( os.getcwd() ) Just in case Python is not running where you think it is - I can't remember how windows decides... and its better not to guess anyway. -- 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 Sep 6 03:04:44 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 6 Sep 2016 17:04:44 +1000 Subject: [Tutor] __init__ In-Reply-To: References: <20160831152358.GT26300@ando.pearwood.info> Message-ID: <20160906070444.GB10468@ando.pearwood.info> On Mon, Sep 05, 2016 at 12:24:50PM +0000, Albert-Jan Roskam wrote: > =====> Aha, thank you, I did not know that. So for Python-3-only code, > the idiom is "class SomeClass:", and for Python2/3 code "class > SomeClass(object)". I prefer to explicitly inherit from object regardless of which version I am using. > What are the most common code changes a developer > needs to make when switching from old- to new-style classes? Apart from making sure you explicitly inherit from object in Python 2 (and optionally the same in 3), there aren't that many. - use super() instead of manually calling the superclass method. # Old way class Parent: ... class Child(Parent): def method(self, arg): result = Parent.method(self, arg) process(result) return result # New way class Parent(object): ... class Child(Parent): def method(self, arg): result = super(Child, self).method(self, arg) # in Python 3, write super().method(arg) process(result) return result Other than that, I don't think there are any *common* changes. There are new features which don't work in old-style classes, like property, but everything else is mostly the same. -- Steve From monikajg at netzero.net Mon Sep 5 22:18:30 2016 From: monikajg at netzero.net (monikajg at netzero.net) Date: Tue, 6 Sep 2016 02:18:30 GMT Subject: [Tutor] cannot find html file, in the same dir as python file Message-ID: <20160905.191830.6317.0@webmail04.dca.untd.com> I put print( os.getcwd() ) into my script, and got C:\Users\Monika\Documents\PythonWeb\class3code\class3code So I put into my script: base_url = 'C:\Users\Monika\Documents\PythonWeb\class3code\class3code\button.html' instead of base_url = 'button.html' and ran it andI got a popup message in python: (unicode error) 'unicodesscape' codec can't decode bytes in position 2-3; truncated \UXXXXXXXXescape --------- Original Message ---------- From: Alan Gauld via Tutor To: tutor at python.org Subject: Re: [Tutor] cannot find html file, in the same dir as python file Date: Tue, 6 Sep 2016 01:25:27 +0100 On 05/09/16 21:59, monikajg at netzero.net wrote: > chromedriver = "resources/chromedriver" > os.environ["webdriver.chrome.driver"] = chromedriver > self.driver = webdriver.Chrome(chromedriver) > base_url = 'button.html' > self.driver.get(base_url) > > FileNotFoundError: [WinError 2] The system cannot find the file specified > Even though this is in Selenium this is a python question. I think. Have you tried giving it the full path? Have you tried asking Python where it is looking, eg print( os.getcwd() ) Just in case Python is not running where you think it is - I can't remember how windows decides... and its better not to guess anyway. -- 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 ____________________________________________________________ howlifeworks.com (Sponsored by Content.Ad) How to Read Your Child's Deleted Texts - Get Your Free Trial http://thirdpartyoffers.netzero.net/TGL3241/57ce27cfe928e27cf1ef7st04duc From alan.gauld at yahoo.co.uk Tue Sep 6 03:43:03 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 6 Sep 2016 08:43:03 +0100 Subject: [Tutor] cannot find html file, in the same dir as python file In-Reply-To: <20160905.191830.6317.0@webmail04.dca.untd.com> References: <20160905.191830.6317.0@webmail04.dca.untd.com> Message-ID: <57CE7387.4080906@yahoo.co.uk> On 06/09/16 03:18, monikajg at netzero.net wrote: > I put > > print( os.getcwd() ) > > into my script, and got > > C:\Users\Monika\Documents\PythonWeb\class3code\class3code Is that the directory you expected? Is it where your html file lives? > So I put into my script: > > base_url = 'C:\Users\Monika\Documents\PythonWeb\class3code\class3code\button.html' instead of base_url = 'button.html' > and ran it andI got a popup message in python: > > (unicode error) 'unicodesscape' codec can't decode bytes in position 2-3; truncated \UXXXXXXXXescape Because you are using a single backslash Python sees the \U as a Unicode indicator. Either change the backslashes to forward slashes or put an 'r' in front of the string to make it raw: base_url = r'C:\Users\Monika\Documents\.....' -- 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 monikajg at netzero.net Tue Sep 6 11:44:57 2016 From: monikajg at netzero.net (monikajg at netzero.net) Date: Tue, 6 Sep 2016 15:44:57 GMT Subject: [Tutor] cannot find html file, in the same dir as python file Message-ID: <20160906.084457.4932.0@webmail12.dca.untd.com> The r trick did it. Yay! Thank you Monika ---------- Original Message ---------- From: Alan Gauld To: "monikajg at netzero.net" Cc: tutor at python.org Subject: Re: [Tutor] cannot find html file, in the same dir as python file Date: Tue, 6 Sep 2016 08:43:03 +0100 On 06/09/16 03:18, monikajg at netzero.net wrote: > I put > > print( os.getcwd() ) > > into my script, and got > > C:\Users\Monika\Documents\PythonWeb\class3code\class3code Is that the directory you expected? Is it where your html file lives? > So I put into my script: > > base_url = 'C:\Users\Monika\Documents\PythonWeb\class3code\class3code\button.html' instead of base_url = 'button.html' > and ran it andI got a popup message in python: > > (unicode error) 'unicodesscape' codec can't decode bytes in position 2-3; truncated \UXXXXXXXXescape Because you are using a single backslash Python sees the \U as a Unicode indicator. Either change the backslashes to forward slashes or put an 'r' in front of the string to make it raw: base_url = r'C:\Users\Monika\Documents\.....' -- 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 ____________________________________________________________ moneynews.com (Sponsored by Content.Ad) 5 Stocks to Buy Now | Massive Market Correction http://thirdpartyoffers.netzero.net/TGL3241/57cee48f50a49648f16dfst01duc From rosalie at bambara.nl Wed Sep 7 08:21:39 2016 From: rosalie at bambara.nl (Rosalie de Klerk Bambara) Date: Wed, 7 Sep 2016 14:21:39 +0200 Subject: [Tutor] crashes Message-ID: Hello, Im trying to install python on my macbook air, I?ve installed these 3 versions and deinstalled too. By typing a ? the IDLE crash every time? after the first ( the idle stops?by all 3 versions. Do you have any idea? print(? (and the it crash?) Hope to hear from you soon. Cheers, Rosalie Met groet, Rosalie de Klerk - Bambara www.bambara.nl www.rekenautomaat.nl www.informaticaonderwijs.nl Mobiel: 06 38 38 05 38 Dit document en de eventuele bijlagen is/zijn uitsluitend bestemd voor de hierboven genoemde geadresseerde(n) en kan/kunnen vertrouwelijke informatie bevatten. Indien u niet de geadresseerde bent, gelieve u zich te onthouden van verspreiding of vermenigvuldiging van dit bericht. Indien dit bericht kennelijk per vergissing naar u is verzonden, verzoek ik u mij dit onverwijld te berichten en dit bericht te vernietigen. (!) Denk aan het milieu voordat je deze e-mail print. From alan.gauld at yahoo.co.uk Wed Sep 7 19:31:36 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 8 Sep 2016 00:31:36 +0100 Subject: [Tutor] crashes In-Reply-To: References: Message-ID: On 07/09/16 13:21, Rosalie de Klerk Bambara wrote: > Im trying to install python on my macbook air, I?ve installed these 3 versions and deinstalled too. Python 2.7 should be installed by default since MacOS uses it. > By typing a ? the IDLE crash every time? > after the first ( the idle stops?by all 3 versions. When you say "crash" what exactly happens? Does the IDLE GUI literally just die and disappear? Or is there an error box first? Or do you just mean you are getting an error message? > Do you have any idea? Not yet, we need more information I suspect. > print(? (and the it crash?) Where are you typing this? Is it the IDLE "Shell" window - the one with the >>> prompt? Can you run Python from the MacOs Console window (Application->Utilities-Console I think) -- 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 eg2597 at gmail.com Fri Sep 9 12:21:45 2016 From: eg2597 at gmail.com (Eric Gardner) Date: Fri, 9 Sep 2016 12:21:45 -0400 Subject: [Tutor] Hello! Message-ID: I don't know if i'm sending the email to the right address but here it goes!. Would Python be a suitable language for first time learners like me? -- Eric G From alan.gauld at yahoo.co.uk Fri Sep 9 13:56:40 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 9 Sep 2016 18:56:40 +0100 Subject: [Tutor] Hello! In-Reply-To: References: Message-ID: On 09/09/16 17:21, Eric Gardner wrote: > I don't know if i'm sending the email to the right address but here it > goes!. Would Python be a suitable language for first time learners like me? Yes, this is the right address, welcome. And yes, Python is an excellent language with which to learn programming. If you are comfortable with basic computer use you can try my tutorial(see below) or there is a whole web page on the Python site dedicated to new programmers: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers -- 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 Joaquin.Alzola at lebara.com Fri Sep 9 13:57:16 2016 From: Joaquin.Alzola at lebara.com (Joaquin Alzola) Date: Fri, 9 Sep 2016 17:57:16 +0000 Subject: [Tutor] Hello! In-Reply-To: References: Message-ID: > I don't know if i'm sending the email to the right address but here it goes!. Would Python be a suitable language for first time learners like me? I suppose it depends on each one but from my experience I try with C and then Java without finding the feeling for it ... Then I learn python over a year ago and I can tell you that it is one of the best for first timers. I am now learning Java and it is much easier after somehow controlling python. (I suppose ruby has to be the same that python). -- Joaquin This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt. From poojabhalode11 at gmail.com Fri Sep 9 14:50:57 2016 From: poojabhalode11 at gmail.com (Pooja Bhalode) Date: Fri, 9 Sep 2016 14:50:57 -0400 Subject: [Tutor] Error: 'IndexedVar' object is not callable Message-ID: Hi everyone, I was getting this error which read ' 'IndexedVar' object is not callable ' for a variable type. The variable is defined as a class variable and has dimensions m.C(i,j) in z and t axis. The program isnt able to take in any of the equations that I am giving it to because of this error. I looked up online, and it said that maybe the variable is being used up somewhere else, also tried changing the name but it didn't help. Could someone please tell me the possible reasons for this happening. ERROR: Rule failed when generating expression for constraint concentration with index (10, 10): TypeError: 'IndexedVar' object is not callable ERROR: Constructing component 'concentration' from data=None failed: TypeError: 'IndexedVar' object is not callable I would appreciate it. Thank you From alan.gauld at yahoo.co.uk Fri Sep 9 20:39:32 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 10 Sep 2016 01:39:32 +0100 Subject: [Tutor] Error: 'IndexedVar' object is not callable In-Reply-To: References: Message-ID: On 09/09/16 19:50, Pooja Bhalode wrote: > I was getting this error which read ' 'IndexedVar' object is not callable ' > for a variable type. Python error messages are very informative, but only if we can see them. Please post the entire error message not just a summary. > The variable is defined as a class variable and has dimensions m.C(i,j) in > z and t axis. Python variables are just names that reference objects, so variables do not have any type per se, rather they take the type of whatever they are currently referencing. > Could someone please tell me the possible reasons for this happening. "not callable" means that you are trying to call the variable (using parens (...) ) on a variable that is not callable, in this case an IndexedVar - whatever that is; it's not part of the standard Python language so you are presumably using some kind of third party add-on, (possibly SciPy?). It will help if you tell us which libraries you are using. Anyway, its most likely that you have assigned a variable to something you expected to be a callable but is in fact an IndexedVar. This could, for example, be the parameter of a function and you have passed in the wrong kind of data, or it could be the return value from a function that you assign to your variable. > ERROR: Rule failed when generating expression for constraint concentration > with index (10, 10): > TypeError: 'IndexedVar' object is not callable > ERROR: Constructing component 'concentration' from data=None failed: > TypeError: 'IndexedVar' object is not callable Those don't look like Python error messages. How are you running this code? If it is an IDE then it may be contributing to the confusion. Also we can only guess what your code looks like. Please post the code causing the error. As a minimum the function/method involved. If possible all of it (if less than ~100 lines?). And please post the entire error message, plus a note of your Python version and whatever library you are using. -- 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 akleider at sonic.net Fri Sep 9 20:48:03 2016 From: akleider at sonic.net (Alex Kleider) Date: Fri, 09 Sep 2016 17:48:03 -0700 Subject: [Tutor] Error: 'IndexedVar' object is not callable In-Reply-To: References: Message-ID: <5ad1838304be43c6de0b345b83c74a5a@sonic.net> On 2016-09-09 11:50, Pooja Bhalode wrote: > Hi everyone, > > I was getting this error which read ' 'IndexedVar' object is not > callable ' > for a variable type. You haven't provided much information but it seems to me you are calling IndexedVar as though it were a function but it probably isn't a function. some_name(zero_or_more_parameters) is a function call, calling some_name. If some_name hasn't been defined as a function: def some_name(....): do something then you can expect the error you got. From steve at pearwood.info Fri Sep 9 21:13:39 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Sat, 10 Sep 2016 11:13:39 +1000 Subject: [Tutor] Error: 'IndexedVar' object is not callable In-Reply-To: References: Message-ID: <20160910011338.GI22471@ando.pearwood.info> Hi Pooja, and welcome! On Fri, Sep 09, 2016 at 02:50:57PM -0400, Pooja Bhalode wrote: > Hi everyone, > > I was getting this error which read ' 'IndexedVar' object is not callable ' > for a variable type. > > The variable is defined as a class variable and has dimensions m.C(i,j) in > z and t axis. I'm afraid I have no idea what you are talking about here. What is an IndexedVar object? What does "dimensions m.C(i,j) in z and t axis" mean? Is this from some third-party library? Which one? I would help if you show us the actual code you are trying to run. Not your entire application, especially if it is big, but enough of the code that we can understand the context. Please read this article first for how you can improve the chances of getting good answers to your questions: http://sscce.org/ > ERROR: Rule failed when generating expression for constraint concentration > with index (10, 10): > TypeError: 'IndexedVar' object is not callable > ERROR: Constructing component 'concentration' from data=None failed: > TypeError: 'IndexedVar' object is not callable These do not look like standard Python errors. I would expect to see a traceback, which may look something like this: Traceback (most recent call last): File "", line 1, in File "/usr/local/lib/python3.3/random.py", line 298, in sample raise TypeError("Population must be a sequence or set. For dicts, use list(d).") TypeError: Population must be a sequence or set. For dicts, use list(d). How are you running this code? My *wild-guess* is that if you change m.C(i, j) to m.C[i, j] it might fix this specific error, but it really is a guess. -- Steve From akleider at sonic.net Fri Sep 9 21:49:13 2016 From: akleider at sonic.net (Alex Kleider) Date: Fri, 09 Sep 2016 18:49:13 -0700 Subject: [Tutor] Error: 'IndexedVar' object is not callable In-Reply-To: <20160910011338.GI22471@ando.pearwood.info> References: <20160910011338.GI22471@ando.pearwood.info> Message-ID: On 2016-09-09 18:13, Steven D'Aprano wrote: > Please read this article first for > how you can improve the chances of getting good answers to your > questions: > > http://sscce.org/ In addition to the link Seven provides above, I've also found the following to be worth perusing: http://www.catb.org/esr/faqs/smart-questions.html From poojabhalode11 at gmail.com Sat Sep 10 12:23:44 2016 From: poojabhalode11 at gmail.com (Pooja Bhalode) Date: Sat, 10 Sep 2016 12:23:44 -0400 Subject: [Tutor] Error: 'IndexedVar' object is not callable In-Reply-To: References: <20160910011338.GI22471@ando.pearwood.info> Message-ID: Hi everyone, I am sorry about the confusion earlier, I am trying to run Pyomo environment in Python on sublime text editor. Python version of 2.7.11. I am running the code in sublime itself. The code is somewhat long, around 200 lines. I can add snippets of it though. Code: m.z = ContinuousSet(bounds = (0,10)) m.t = ContinuousSet(bounds = (0,10)) where all the variables that I am defining, are varying along the z and t direction. Variables: m.C = Var(m.z,m.t) m.Cair = Var(m.z,m.t) m.masswater = Var(m.z,m.t) m.massair = Param(initialize = 1833.50) that is C and Cair and all other variables are varying along z and t direction. After this, when I define the equations, def _calculations(m,i,j): if i == 0 or i == 1 or j == 0: return Constraint.Skip return m.wateraccumulated(j) == 916.50 - (m.C(i,j)*46*28/(28*18*1.205 + m.C(i,j) * 46*28))*2750 if i == 0: return m.vel(i,j) == m.inmassflowrate/ ((m.outerdiameter * m.outerdiameter * m.pi/4)*m.densityfeed) if i == 1: return m.vel(i,j) == m.outmassflowrate/((m.outerdiameter * m.outerdiameter * m.pi/4)*m.densityfeed) return m.vel(i,j) == m.flowrate/((m.outerdiameter * m.outerdiameter * m.pi/4)*m.densityfeed) m.calculations = Constraint(m.z,m.t, rule = _calculations) It tells me that TypeError: 'IndexedVar' object is not callable for m.C itself. I think the syntax is correct but I am not able to figure out why the variable is not callable. I am aware that the language is slightly different than Python here, I am just stuck and would appreciate any help here. On Fri, Sep 9, 2016 at 9:49 PM, Alex Kleider wrote: > On 2016-09-09 18:13, Steven D'Aprano wrote: > >> Please read this article first for >> how you can improve the chances of getting good answers to your >> questions: >> >> http://sscce.org/ >> > > In addition to the link Seven provides above, I've also found the > following to be worth perusing: > http://www.catb.org/esr/faqs/smart-questions.html > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Sat Sep 10 16:11:43 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 10 Sep 2016 21:11:43 +0100 Subject: [Tutor] Error: 'IndexedVar' object is not callable In-Reply-To: References: <20160910011338.GI22471@ando.pearwood.info> Message-ID: On 10/09/16 17:23, Pooja Bhalode wrote: > I am trying to run Pyomo environment in Python on sublime text editor. OK, For the pyomo stuff you should ask on the pyomo support site (or I notice they do staxck overflow too) > Python version of 2.7.11. I am running the code in sublime itself. You maybe should try running it outside Sublime, it may give you more useful error messages. > Code: > > m.z = ContinuousSet(bounds = (0,10)) > m.t = ContinuousSet(bounds = (0,10)) > where all the variables that I am defining, are varying along the z and t > direction. Most of that is mreaningless to us because we don;t know pyomo. I'd never even heard of it until you said. > Variables: > m.C = Var(m.z,m.t) > m.Cair = Var(m.z,m.t) > m.masswater = Var(m.z,m.t) > m.massair = Param(initialize = 1833.50) Again that is all very pyomo specific. Its hard to comment without knowing pyomo. > After this, when I define the equations, > > def _calculations(m,i,j): > if i == 0 or i == 1 or j == 0: > return Constraint.Skip > > return m.wateraccumulated(j) == 916.50 - (m.C(i,j)*46*28/(28*18*1.205 + > m.C(i,j) * 46*28))*2750 What happens if you do: m.C = Var(m.z,m.t) print m.C print m.C(i,j) > It tells me that TypeError: 'IndexedVar' object is not callable for m.C > itself. I think the syntax is correct but I am not able to figure out why > the variable is not callable. Its not clear where the error is occurring, are you sure that's all the error message says?? Usually there is a whole heap of stuff about where the call happened and its context. -- 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 jf_byrnes at comcast.net Sat Sep 10 20:19:02 2016 From: jf_byrnes at comcast.net (Jim Byrnes) Date: Sat, 10 Sep 2016 19:19:02 -0500 Subject: [Tutor] pip says no downloads for PyMedia Message-ID: I am working on an exercise that needs PyMedia. pypi seems to have the package but will not download it. jfb at Jims-1404:~$ pip3 search PyMedia pymediafire - A python package for MediaFire API ffmpymedia - Wrapper around the FFMPEG utility PyMedia - PyMedia is a module/library for manipulating mp3, avi, ogg, divx, mpeg, dvd media files pymedia2-pyrana - Package for simple manipulation of multimedia files pymediainfo - A Python wrapper for the mediainfo library. PyMediaRSS2Gen - A Python library for generating Media RSS 2.0 feeds. jfb at Jims-1404:~$ sudo pip3 install PyMedia [sudo] password for jfb: Downloading/unpacking PyMedia Could not find any downloads that satisfy the requirement PyMedia Cleaning up... No distributions at all found for PyMedia Storing debug log for failure in /home/jfb/.pip/pip.log jfb at Jims-1404:~$ From pip.log: ------------------------------------------------------------ /usr/bin/pip3 run on Sat Sep 10 19:04:34 2016 Downloading/unpacking PyMedia Getting page https://pypi.python.org/simple/PyMedia/ URLs to search for versions for PyMedia: * https://pypi.python.org/simple/PyMedia/ Analyzing links from page https://pypi.python.org/simple/pymedia/ Could not find any downloads that satisfy the requirement PyMedia Cleaning up... Removing temporary dir /tmp/pip_build_root... No distributions at all found for PyMedia Exception information: Traceback (most recent call last): File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 122, in main status = self.run(options, args) File "/usr/lib/python3/dist-packages/pip/commands/install.py", line 278, in run requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle) File "/usr/lib/python3/dist-packages/pip/req.py", line 1178, in prepare_files url = finder.find_requirement(req_to_install, upgrade=self.upgrade) File "/usr/lib/python3/dist-packages/pip/index.py", line 277, in find_requirement raise DistributionNotFound('No distributions at all found for %s' % req) pip.exceptions.DistributionNotFound: No distributions at all found for PyMedia I am using python3 could that be the problem? I looked but couldn't find any info on what version of python is needed. Thanks, Jim From poojabhalode11 at gmail.com Sat Sep 10 16:01:32 2016 From: poojabhalode11 at gmail.com (Pooja Bhalode) Date: Sat, 10 Sep 2016 16:01:32 -0400 Subject: [Tutor] Error: 'IndexedVar' object is not callable In-Reply-To: References: <20160910011338.GI22471@ando.pearwood.info> Message-ID: Hi Steven, Thank you so much for your help. It worked. That was the mistake I had. I had (i,j) instead of [i,j] Thanks again Pooja On Sat, Sep 10, 2016 at 12:23 PM, Pooja Bhalode wrote: > Hi everyone, > > I am sorry about the confusion earlier, > I am trying to run Pyomo environment in Python on sublime text editor. > Python version of 2.7.11. I am running the code in sublime itself. > > The code is somewhat long, around 200 lines. > I can add snippets of it though. > > Code: > > m.z = ContinuousSet(bounds = (0,10)) > m.t = ContinuousSet(bounds = (0,10)) > where all the variables that I am defining, are varying along the z and t > direction. > > Variables: > m.C = Var(m.z,m.t) > m.Cair = Var(m.z,m.t) > m.masswater = Var(m.z,m.t) > m.massair = Param(initialize = 1833.50) > > that is C and Cair and all other variables are varying along z and t > direction. > > After this, when I define the equations, > > def _calculations(m,i,j): > if i == 0 or i == 1 or j == 0: > return Constraint.Skip > > return m.wateraccumulated(j) == 916.50 - (m.C(i,j)*46*28/(28*18*1.205 + > m.C(i,j) * 46*28))*2750 > > if i == 0: > return m.vel(i,j) == m.inmassflowrate/ ((m.outerdiameter * m.outerdiameter > * m.pi/4)*m.densityfeed) > if i == 1: > return m.vel(i,j) == m.outmassflowrate/((m.outerdiameter * > m.outerdiameter * m.pi/4)*m.densityfeed) > > return m.vel(i,j) == m.flowrate/((m.outerdiameter * m.outerdiameter * > m.pi/4)*m.densityfeed) > m.calculations = Constraint(m.z,m.t, rule = _calculations) > > It tells me that TypeError: 'IndexedVar' object is not callable for m.C > itself. I think the syntax is correct but I am not able to figure out why > the variable is not callable. > > I am aware that the language is slightly different than Python here, I am > just stuck and would appreciate any help here. > > On Fri, Sep 9, 2016 at 9:49 PM, Alex Kleider wrote: > >> On 2016-09-09 18:13, Steven D'Aprano wrote: >> >>> Please read this article first for >>> how you can improve the chances of getting good answers to your >>> questions: >>> >>> http://sscce.org/ >>> >> >> In addition to the link Seven provides above, I've also found the >> following to be worth perusing: >> http://www.catb.org/esr/faqs/smart-questions.html >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > From robertvstepp at gmail.com Mon Sep 12 19:06:04 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Mon, 12 Sep 2016 18:06:04 -0500 Subject: [Tutor] pip says no downloads for PyMedia In-Reply-To: References: Message-ID: On Sep 10, 2016 7:20 PM, "Jim Byrnes" wrote: > > > I am using python3 could that be the problem? I looked but couldn't find any info on what version of python is needed. > I went to pymedia.org. The copyright at the bottom of the page is 2004. The "latest" news entry is February 1, 2006. So this looks to be from the Python 2 only days. boB From jf_byrnes at comcast.net Mon Sep 12 20:59:11 2016 From: jf_byrnes at comcast.net (Jim Byrnes) Date: Mon, 12 Sep 2016 19:59:11 -0500 Subject: [Tutor] pip says no downloads for PyMedia In-Reply-To: References: Message-ID: On 09/12/2016 06:06 PM, boB Stepp wrote: > On Sep 10, 2016 7:20 PM, "Jim Byrnes" wrote: >> >> >> I am using python3 could that be the problem? I looked but couldn't find > any info on what version of python is needed. >> > I went to pymedia.org. The copyright at the bottom of the page is 2004. > The "latest" news entry is February 1, 2006. So this looks to be from the > Python 2 only days. > > boB > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > Thanks. I looked but did not dig as deep as you did. Regards, Jim From jf_byrnes at comcast.net Tue Sep 13 15:17:37 2016 From: jf_byrnes at comcast.net (Jim Byrnes) Date: Tue, 13 Sep 2016 14:17:37 -0500 Subject: [Tutor] pip says no downloads for PyMedia In-Reply-To: References: Message-ID: On 09/12/2016 07:59 PM, Jim Byrnes wrote: > On 09/12/2016 06:06 PM, boB Stepp wrote: >> On Sep 10, 2016 7:20 PM, "Jim Byrnes" wrote: >>> >>> >>> I am using python3 could that be the problem? I looked but couldn't >>> find >> any info on what version of python is needed. >>> >> I went to pymedia.org. The copyright at the bottom of the page is 2004. >> The "latest" news entry is February 1, 2006. So this looks to be from >> the >> Python 2 only days. >> >> boB >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > Thanks. I looked but did not dig as deep as you did. > > Regards, Jim > > So it looks like I need to use Python 2. On my Ubuntu 14.04 system I have vers 2.7.6. I tried that with the below result. jfb at Jims-1404:~$ pip install PyMedia Collecting PyMedia /usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#snimissingwarning. SNIMissingWarning /usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:122: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning. InsecurePlatformWarning Could not find a version that satisfies the requirement PyMedia (from versions: ) No matching distribution found for PyMedia jfb at Jims-1404:~$ So I went to the urllib3 address listed above. It said to do pip install urllib3[secure} to solve the problem. So I still get the errors shown above. What else do I need to do? Thanks, Jim From avedione at gmail.com Tue Sep 13 16:11:07 2016 From: avedione at gmail.com (Glen Reytas) Date: Wed, 14 Sep 2016 04:11:07 +0800 Subject: [Tutor] Need reference materials to learn flask + python for payroll project that would run both in windows (PC) and android/iOS. Message-ID: Hi, I have a basic knowledge in Python and I would like to enhance it by engaging in a payroll project. For you to gauge my coding level, I finished the Python course at codecademy.com but it was 2-3 years ago. Since then I haven't coded aside from HTML. So, I would appreciate if you could provide me a series of topics that would help me code my first python project and be online at the end of it. Accessible through PC and mobile. Hoping, Glen From robertvstepp at gmail.com Tue Sep 13 17:01:28 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 13 Sep 2016 16:01:28 -0500 Subject: [Tutor] pip says no downloads for PyMedia In-Reply-To: References: Message-ID: On Tue, Sep 13, 2016 at 2:17 PM, Jim Byrnes wrote: > On 09/12/2016 07:59 PM, Jim Byrnes wrote: >> >> On 09/12/2016 06:06 PM, boB Stepp wrote: >>> >>> On Sep 10, 2016 7:20 PM, "Jim Byrnes" wrote: >>>> >>>> >>>> >>>> I am using python3 could that be the problem? I looked but couldn't >>>> find >>> >>> any info on what version of python is needed. >>>> >>>> >>> I went to pymedia.org. The copyright at the bottom of the page is 2004. >>> The "latest" news entry is February 1, 2006. So this looks to be from >>> the >>> Python 2 only days. I personally know nothing about PyMedia, but if 2004 should be the release date (The copyright at the bottom of their home page.), then we are looking at a version of Python prior to Py 2.4.4 which was released in October of 2006! If this is correct, then you are probably going to be unable to use PyMedia with any modern version of Python. Is there not something else more modern and compatible that can meet your needs? -- boB From jf_byrnes at comcast.net Tue Sep 13 17:41:56 2016 From: jf_byrnes at comcast.net (Jim Byrnes) Date: Tue, 13 Sep 2016 16:41:56 -0500 Subject: [Tutor] pip says no downloads for PyMedia In-Reply-To: References: Message-ID: On 09/13/2016 04:01 PM, boB Stepp wrote: > On Tue, Sep 13, 2016 at 2:17 PM, Jim Byrnes wrote: >> On 09/12/2016 07:59 PM, Jim Byrnes wrote: >>> >>> On 09/12/2016 06:06 PM, boB Stepp wrote: >>>> >>>> On Sep 10, 2016 7:20 PM, "Jim Byrnes" wrote: >>>>> >>>>> >>>>> >>>>> I am using python3 could that be the problem? I looked but couldn't >>>>> find >>>> >>>> any info on what version of python is needed. >>>>> >>>>> >>>> I went to pymedia.org. The copyright at the bottom of the page is 2004. >>>> The "latest" news entry is February 1, 2006. So this looks to be from >>>> the >>>> Python 2 only days. > > I personally know nothing about PyMedia, but if 2004 should be the > release date (The copyright at the bottom of their home page.), then > we are looking at a version of Python prior to Py 2.4.4 which was > released in October of 2006! If this is correct, then you are > probably going to be unable to use PyMedia with any modern version of > Python. Is there not something else more modern and compatible that > can meet your needs? > > I am following some code in a Tkinter book I am reading. The author says he is using Windows 7 and Python 2.7.3, so I should be good with python 2.7.6. Right now I am more concerned about and trying to fix the urllib3 warnings. As a test just now I used python 2.7.3 version of pip to install pyperclip. It gave me the same urllib3 warnings as when I tried to install PyMedia, BUT it did go ahead and finish the installation. So maybe the two problems are not related but I would like to figure out and correct the urllib3 problem. Regards, Jim From alan.gauld at yahoo.co.uk Tue Sep 13 20:26:16 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 14 Sep 2016 01:26:16 +0100 Subject: [Tutor] Need reference materials to learn flask + python for payroll project that would run both in windows (PC) and android/iOS. In-Reply-To: References: Message-ID: On 13/09/16 21:11, Glen Reytas wrote: > I have a basic knowledge in Python and I would like to enhance it by > engaging in a payroll project. For you to gauge my coding level, I finished > the Python course at codecademy.com but it was 2-3 years ago. Since then I > haven't coded aside from HTML. I don;t have a lot on Flask, I only read the tutorial stuff on the Flask web site and watched a couple of YouTube videos. > So, I would appreciate if you could provide me a series of topics that > would help me code my first python project and be online at the end of it. To build a payroll application requires quite a lot of skills, usually including a database of some kind and a lot of security type stuff. Do you have experience in either of those areas? Also you need to test it really thoroughly, especially around areas involving legal rules (like taxes, overtime rates, max/min hours etc etc.) It's not something I'd choose as a first project unless you are only doing a very simplistic payslip viewer or similar - and even then data protection legislation will likely still be applicable. -- 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 itetteh34 at hotmail.com Tue Sep 13 20:59:58 2016 From: itetteh34 at hotmail.com (isaac tetteh) Date: Wed, 14 Sep 2016 00:59:58 +0000 Subject: [Tutor] Need reference materials to learn flask + python for payroll project that would run both in windows (PC) and android/iOS. In-Reply-To: References: , Message-ID: "Project that will run on both windows and android/iso" if mean its responsiveness then flask cannot do that but css3, javascript(Angular), twitter-bootstrap can help you do that. But if you mean like a desktop/ phone app then maybe check another framework. Check out Flask Web Development: Developing Web Applications with Python By Miguel Grinberg Sent from my iPhone On Sep 13, 2016, at 7:28 PM, Alan Gauld via Tutor > wrote: On 13/09/16 21:11, Glen Reytas wrote: I have a basic knowledge in Python and I would like to enhance it by engaging in a payroll project. For you to gauge my coding level, I finished the Python course at codecademy.com but it was 2-3 years ago. Since then I haven't coded aside from HTML. I don;t have a lot on Flask, I only read the tutorial stuff on the Flask web site and watched a couple of YouTube videos. So, I would appreciate if you could provide me a series of topics that would help me code my first python project and be online at the end of it. To build a payroll application requires quite a lot of skills, usually including a database of some kind and a lot of security type stuff. Do you have experience in either of those areas? Also you need to test it really thoroughly, especially around areas involving legal rules (like taxes, overtime rates, max/min hours etc etc.) It's not something I'd choose as a first project unless you are only doing a very simplistic payslip viewer or similar - and even then data protection legislation will likely still be applicable. -- 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 ron20018 at gmail.com Tue Sep 13 13:27:43 2016 From: ron20018 at gmail.com (Ron Williams) Date: Tue, 13 Sep 2016 13:27:43 -0400 Subject: [Tutor] awk like functionality in python Message-ID: Hello, this is my first post. I'm glad a place like this exists. I'm coming from primarily a shell scripting background and it was suggested I should learn python because the two are closely related. I've been studying python now for about a week and as of right now, I'm not really seeing many similarities. Anyway, to learn python as fast as I can, I thought I'd just dive in and try to write a program but I'm stuck on the first step. The program I'm writing comes from this website: http://www.meetup.com/NOVA-Python/events/232629865/ I am not a member of the group and have not been to any meetings yet but here's the homework assignment I'm working on... *1. Find the total number of births in each year. If you can, find the total for both male and female births. Plot your results. * I've downloaded the files and put them in a directory and here's the script I wrote to tackle the first part of the question, the total number of births in a year: ---------------------------------------------------------------------- #!/bin/bash FILE_DIR=/home/ronw/my_python_journey/names for i in `ls $FILE_DIR` do OUTPUT=`awk -F, 'BEGIN{OFS=" ";} {sum+=$3} END {print FILENAME, sum}' $FILE_DIR/$i | awk -F'[^0-9]*' '{print $2, $3}'` echo $OUTPUT done -------------------------------------------------------------------- Question: How would I write a similar program and achieve awk like functionality in python? Thanks From sam.oduor at gmail.com Tue Sep 13 17:34:50 2016 From: sam.oduor at gmail.com (Sam Oduor) Date: Wed, 14 Sep 2016 00:34:50 +0300 Subject: [Tutor] Need reference materials to learn flask + python for payroll project that would run both in windows (PC) and android/iOS. In-Reply-To: References: Message-ID: Hi Html is a markup language so arguably you are not to classify it as coding & python modules are also a bit enhanced 2-3 years difference. I would suggest you delve deeper again on python - lists, strings, tuples, dict, utilities, regex .... e.t.c .... A good place to start revision -> https://developers.google.com/edu/python/ ; many more options to learning are available; some are commerical .. Also register on https://github.com/ to view a few open projects and please do contribute -> a quick search on payroll + python will land you to things of interest that will help you build .. For payroll you may also need to be fluent in database back-ends; oracles, mysql or whichever you prefer python has a modules that you can import to connect to them .. All the best in your pursuit, kindly share with us your code once you are successful ! On Tue, Sep 13, 2016 at 11:11 PM, Glen Reytas wrote: > Hi, > > I have a basic knowledge in Python and I would like to enhance it by > engaging in a payroll project. For you to gauge my coding level, I finished > the Python course at codecademy.com but it was 2-3 years ago. Since then I > haven't coded aside from HTML. > > So, I would appreciate if you could provide me a series of topics that > would help me code my first python project and be online at the end of it. > Accessible through PC and mobile. > > Hoping, > > Glen > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Samson Oduor From alan.gauld at yahoo.co.uk Wed Sep 14 06:02:28 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 14 Sep 2016 11:02:28 +0100 Subject: [Tutor] awk like functionality in python In-Reply-To: References: Message-ID: On 13/09/16 18:27, Ron Williams wrote: > Hello, this is my first post. I'm glad a place like this exists. I'm coming > from primarily a shell scripting background and it was suggested I should > learn python because the two are closely related. Its often been said but its not strictly true. Python is a general purpose programming language with some scripting facilities, but they are a small part of the whole Python arsenal. If you are doing a lot of scripting type projects there are a few modules that you are going to need to study closely. - The os module gives you access to lots of tools like the basic os commands (ls, chmod, pwd etc) - The path module lets you manipulate file paths - glob lets you access lists of files using wildcards - shutil gives you tools to manipulate files(cop, rm etc) - subprocess lets you run other programs and manipulate the input/output - fileinput lets you process all files in a sub-directory tree. - string methoods - not a module but the builtin string class - re gives you access to regex (as in grep, sed) > FILE_DIR=/home/ronw/my_python_journey/names > > for i in `ls $FILE_DIR` > do look at the for loop in Python check out os.listdir() and glob.glob() > OUTPUT=`awk -F, 'BEGIN{OFS=" ";} {sum+=$3} END {print FILENAME, sum}' > $FILE_DIR/$i | awk -F'[^0-9]*' '{print $2, $3}'` There is nothing that directly matches awk in Python you have to do a bit more work here. You usually need an if/elif/else chain, although in this case you match every line so its easier: sep = " " with open(filename) as inp: for line in inp: sum += int(line.split(sep)[2]) print(filename, sum) But using fileinput makes it slightly simpler, the whole thing then reduces to: import fileinput as fin FILE_DIR = "/home/ronw/my_python_journey/names" data = {} for line in fin.input(os.listdir(FILE_DIR)) births = int(line.split()[2]) # year = line.split[???] # is there a year field data.setdefault(fin.filename(),0) += births # or use year as key? print(data) Which basically processes every file in the directory line by line. It extracts the birth figure and converts it to an integer. It then inserts/adds it into a dictionary(data) based on the filename. At the end it prints the dictionary - you may need some extra processing to map filenames to years? Or maybe extract the year from the line? I didn't look at your files to check the data format, but it should be close... hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Wed Sep 14 13:15:24 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 14 Sep 2016 18:15:24 +0100 Subject: [Tutor] awk like functionality in python In-Reply-To: References: Message-ID: On 14/09/16 11:02, Alan Gauld via Tutor wrote: > - fileinput lets you process all files in a sub-directory tree. Duh, no it doesn't. It processes all the files in a list of filenames. Apologies. Correct usage is demonstrated below... > But using fileinput makes it slightly simpler, > the whole thing then reduces to: > > import fileinput as fin > FILE_DIR = "/home/ronw/my_python_journey/names" > data = {} > for line in fin.input(os.listdir(FILE_DIR)) > births = int(line.split()[2]) > # year = line.split[???] # is there a year field > data.setdefault(fin.filename(),0) += births # or use year as key? > > print(data) > > Which basically processes every file in the directory > line by line. It extracts the birth figure and converts > it to an integer. It then inserts/adds it into a > dictionary(data) based on the filename. At the end > it prints the dictionary -- 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 anish198519851985 at gmail.com Thu Sep 15 03:05:15 2016 From: anish198519851985 at gmail.com (anish singh) Date: Thu, 15 Sep 2016 00:05:15 -0700 Subject: [Tutor] global interpreter lock Message-ID: Can someone explain global interpreter lock with some source code examples? I didn't understand explanation offered here: https://docs.python.org/3/glossary.html#term-global-interpreter-lock From monikajg at netzero.net Thu Sep 15 00:40:22 2016 From: monikajg at netzero.net (monikajg at netzero.net) Date: Thu, 15 Sep 2016 04:40:22 GMT Subject: [Tutor] @property for old style classes vs new style classes Message-ID: <20160914.214022.31408.0@webmail01.dca.untd.com> Hi: Im studying @property, @var.setter and @var.deleter. I understand how they work in new style classes. Even though I do not use old style classes it would be interesting to understand what is going on behind the scenes. I can try to make assumptions but I do not want to because they might be incorrect. Could somebody please explain what is going on for old style classes for the below code: class GetSet(): def __init__(self, value): self.attrval = value @property def var(self): print "getting the var attribute" return self.attrval @var.setter def var(self,value): print "setting the var attribute" self.attrval = value @var.deleter def var(self): print "deleting the var attribute" self.attrval = None me = GetSet(5) me.var = 1000 print me.var del me.var print me.var Output: 1000 getting the var attribute 5 >>> Same code but with new style classes (this one I understand): class GetSet(object): def __init__(self, value): self.attrval = value @property def var(self): print ("getting the var attribute") return self.attrval @var.setter def var(self,value): print ("setting the var attribute") self.attrval = value @var.deleter def var(self): print ("deleting the var attribute") self.attrval = None me = GetSet(5) me.var = 1000 print (me.var) del me.var print (me.var) Output: setting the var attribute getting the var attribute 1000 deleting the var attribute getting the var attribute None Thank you very much Monika ____________________________________________________________ Unlock Visions (Sponsored by Content.Ad) 1 Odd Method 'Restores' Your 20/20 Vision. Try This http://thirdpartyoffers.netzero.net/TGL3241/57da2673b610726731820st03duc From ingoogni at gmail.com Thu Sep 15 06:30:34 2016 From: ingoogni at gmail.com (ingo) Date: Thu, 15 Sep 2016 12:30:34 +0200 Subject: [Tutor] automatic setting of class property when another one changes Message-ID: Rather stuck with this one, I'd like to automatically (re)set the propery "relaystate" when one of the others (Tm, Tset, Th) is set, regardless of wether their value has changed. Ingo My code so far: from collections import namedtuple import logging logger = logging.getLogger() logger.setLevel(logging.DEBUG) stream_handler = logging.StreamHandler() stream_handler.setLevel(logging.DEBUG) formatter = logging.Formatter( '[%(levelname)-8s] %(asctime)s (%(name)-8s) - %(message)s', '%Y-%m-%d %H:%M:%S', ) stream_handler.setFormatter(formatter) logger.addHandler(stream_handler) class Thermostat(): def __init__(self, Tm=20, Tset=20, Th=0.3): logger.debug("__INIT__") self.Tm = Tm self.Tset = Tset self.Th = Th self.relaystate = None @property def Tm(self): logger.debug("GETTER Tm") return self._Tm @Tm.setter def Tm(self, value): logger.debug("SETTER Tm") self._Tm = value @property def Tset(self): logger.debug("GETTER Tset") return self._Tset @Tset.setter def Tset(self, value): logger.debug("SETTER Tset") self._Tset = value @property def Th(self): logger.debug("GETTER Th") return self._Th @Th.setter def Th(self, value): logger.debug("SETTER Th") self._Th = value @property def relaystate(self): logger.debug("GETTER relaystate") return self._relaystate @relaystate.setter def relaystate(self, value): logger.debug("SETTER relaystate") #on init while setting Tm, Tset and Th are not known #so relaystate can not be calculated try: lower = self.Tset-self.Th upper = self.Tset+self.Th except AttributeError as e: logger.debug("SETTER relaystate : %s", e) self._relaystate = None rlstate = namedtuple('relaystate', ['heat', 'cool']) if self.Tm < lower: value = rlstate(1,0) elif self.Tm > upper: value = rlstate(0,1) elif self.Tm > lower and self.Tm < upper: value = rlstate(0,0) self._relaystate = value if __name__ == "__main__": TS1 = Thermostat() TS1.Tset = 44 print("heat : ",TS1.relaystate.heat,"cool : ",TS1.relaystate.cool) From __peter__ at web.de Thu Sep 15 08:14:33 2016 From: __peter__ at web.de (Peter Otten) Date: Thu, 15 Sep 2016 14:14:33 +0200 Subject: [Tutor] automatic setting of class property when another one changes References: Message-ID: ingo wrote: > Rather stuck with this one, I'd like to automatically (re)set the > propery "relaystate" when one of the others (Tm, Tset, Th) is set, > regardless of wether their value has changed. The easiest way to achieve your goal seems to be a read-only property: # this creates a class that should be shared by all RelayState instances; # therefore it belongs on the module level RelayState = namedtuple('RelayState', ['heat', 'cool']) # the states are immutable and can safely be defined outside the method HEAT = rlstate(1, 0) COOL = rlstate(0, 1) OFF = rlstate(0, 0) class Thermostat(): ... @property def relaystate(self): logger.debug("GETTER relaystate") lower = self.Tset - self.Th upper = self.Tset + self.Th if self.Tm < lower: return HEAT elif self.Tm > upper: return COOL else: return OFF If that doesn't work (because you want to operate an actual relay, say) you probably have to trigger a change in the TXXX setters: class Thermostat(): def __init__(self, Tm=20, Tset=20, Th=0.3): self._relaystate = None self.Tm = Tm self.Tset = Tset self.Th = Th def update_relaystate(self): try: state = self.calculate_relaystate() except AttributeError: pass else: self.relaystate = state @property def Tm(self): logger.debug("GETTER Tm") return self._Tm @Tm.setter def Tm(self, value): logger.debug("SETTER Tm") self._Tm = value self.update_relaystate() ... @property def relaystate(self): logger.debug("GETTER relaystate") return self._relaystate @relaystate.setter def relaystate(self, value): if value != self.relaystate: logger.debug( "SWITCHING relaystate from %s to %s", self.relaystate, value) self._relaystate = value def calculate_relaystate(self): lower = self.Tset-self.Th upper = self.Tset+self.Th if self.Tm < lower: return HEAT elif self.Tm > upper: return COOL else: return OFF > > Ingo > > My code so far: > > from collections import namedtuple > import logging > > logger = logging.getLogger() > logger.setLevel(logging.DEBUG) > stream_handler = logging.StreamHandler() > stream_handler.setLevel(logging.DEBUG) > formatter = logging.Formatter( > '[%(levelname)-8s] %(asctime)s (%(name)-8s) - %(message)s', > '%Y-%m-%d %H:%M:%S', > ) > stream_handler.setFormatter(formatter) > logger.addHandler(stream_handler) > > > class Thermostat(): > def __init__(self, Tm=20, Tset=20, Th=0.3): > logger.debug("__INIT__") > self.Tm = Tm > self.Tset = Tset > self.Th = Th > self.relaystate = None > > @property > def Tm(self): > logger.debug("GETTER Tm") > return self._Tm > @Tm.setter > def Tm(self, value): > logger.debug("SETTER Tm") > self._Tm = value > > @property > def Tset(self): > logger.debug("GETTER Tset") > return self._Tset > @Tset.setter > def Tset(self, value): > logger.debug("SETTER Tset") > self._Tset = value > > @property > def Th(self): > logger.debug("GETTER Th") > return self._Th > @Th.setter > def Th(self, value): > logger.debug("SETTER Th") > self._Th = value > > @property > def relaystate(self): > logger.debug("GETTER relaystate") > return self._relaystate > @relaystate.setter > def relaystate(self, value): > logger.debug("SETTER relaystate") > #on init while setting Tm, Tset and Th are not known > #so relaystate can not be calculated > try: > lower = self.Tset-self.Th > upper = self.Tset+self.Th > except AttributeError as e: > logger.debug("SETTER relaystate : %s", e) > self._relaystate = None > rlstate = namedtuple('relaystate', ['heat', 'cool']) > if self.Tm < lower: > value = rlstate(1,0) > elif self.Tm > upper: > value = rlstate(0,1) > elif self.Tm > lower and self.Tm < upper: > value = rlstate(0,0) > self._relaystate = value > > > if __name__ == "__main__": > > TS1 = Thermostat() > TS1.Tset = 44 > print("heat : ",TS1.relaystate.heat,"cool : ",TS1.relaystate.cool) > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From steve at pearwood.info Thu Sep 15 09:35:11 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 15 Sep 2016 23:35:11 +1000 Subject: [Tutor] automatic setting of class property when another one changes In-Reply-To: References: Message-ID: <20160915133511.GQ22471@ando.pearwood.info> On Thu, Sep 15, 2016 at 12:30:34PM +0200, ingo wrote: > Rather stuck with this one, I'd like to automatically (re)set the > propery "relaystate" when one of the others (Tm, Tset, Th) is set, > regardless of wether their value has changed. The obvious way is to make Tm, etc properties, and have their setter method reset the relaystate property. Since they relay state cannot be calculated during __init__ until all of the properties have been set, simply bypass the properties in the __init__. See below. > My code so far: Thanks for showing your code, but you shouldn't drown us in masses of irrelevant code that has nothing to do with your problem. We don't care about the logging code you show. Take it out. In fact, the best way to demo code is to cut it down to the barest minimum demonstrating the problem: class Thermostat(object): def __init__(self, Tm): self._Tm = Tm self.reset_relay() @property def Tm(self): return self._Tm @Tm.setter def Tm(self, value): self.reset_relay() self._Tm = value def reset_relay(): self.relaystate = None About a dozen lines, versus seventy for your code. See also this for more information: http://sscce.org/ -- Steve From eryksun at gmail.com Thu Sep 15 10:08:12 2016 From: eryksun at gmail.com (eryk sun) Date: Thu, 15 Sep 2016 14:08:12 +0000 Subject: [Tutor] @property for old style classes vs new style classes In-Reply-To: <20160914.214022.31408.0@webmail01.dca.untd.com> References: <20160914.214022.31408.0@webmail01.dca.untd.com> Message-ID: On Thu, Sep 15, 2016 at 4:40 AM, monikajg at netzero.net wrote: > class GetSet(): > > def __init__(self, value): > self.attrval = value > > @property > def var(self): > print "getting the var attribute" > return self.attrval > @var.setter > def var(self,value): > print "setting the var attribute" > self.attrval = value > > @var.deleter > def var(self): > print "deleting the var attribute" > self.attrval = None > > me = GetSet(5) > me.var = 1000 A classic instance has both a type (`instance`) and a __class__ (e.g. GetSet). The type defines how getting, setting, and deleting attributes works, and the hard-coded classic behavior for calling the class __getattr__, __setattr__, and __delattr__ methods. If the class doesn't define __setattr__ and __delattr__, the instance type sets and deletes attributes directly in the instance dict. Unlike new-style classes, a data descriptor defined by the class gets ignored here. Getting attributes also prefers the instance dict. However, to support bound methods (e.g. __init__), it falls back on a class lookup and calls the descriptor __get__ method if defined. Unintentionally, it happens that this partially supports the property descriptor. But since there's no data descriptor support, it's only useful for read-only properties. From steve at pearwood.info Thu Sep 15 12:50:46 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 16 Sep 2016 02:50:46 +1000 Subject: [Tutor] @property for old style classes vs new style classes In-Reply-To: References: <20160914.214022.31408.0@webmail01.dca.untd.com> Message-ID: <20160915165046.GR22471@ando.pearwood.info> On Thu, Sep 15, 2016 at 02:08:12PM +0000, eryk sun wrote: > Getting attributes also prefers the instance dict. However, to support > bound methods (e.g. __init__), it falls back on a class lookup and > calls the descriptor __get__ method if defined. Is that documented anywhere? When was it introduced? Because I can see that in Python 2.4 and higher, function objects have a __get__, and old-style classes appear to call __get__ methods. But going back to Python 1.5 function objects DON'T have a __get__ and attribute lookup ignores __get__ even if designed. >>> class MyDescriptor: ... def __get__(self): ... print "calling descriptor __get__" ... return lambda self: "the getter" ... >>> class X: ... desc = MyDescriptor() ... >>> X.desc <__main__.MyDescriptor instance at 82d1940> Some time between Python 1.5 and 2.4, the behaviour of old-style classes was changed to *half* support the descriptor protocol. As far as I can see, that's not mentioned in the Descriptor HowTo guide: https://docs.python.org/2/howto/descriptor.html and it contradicts the comment here: https://docs.python.org/2/reference/datamodel.html#invoking-descriptors Quote: Note that descriptors are only invoked for new style objects or classes (ones that subclass object() or type()). -- Steve From steve at pearwood.info Thu Sep 15 12:57:12 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 16 Sep 2016 02:57:12 +1000 Subject: [Tutor] @property for old style classes vs new style classes In-Reply-To: <20160914.214022.31408.0@webmail01.dca.untd.com> References: <20160914.214022.31408.0@webmail01.dca.untd.com> Message-ID: <20160915165712.GS22471@ando.pearwood.info> On Thu, Sep 15, 2016 at 04:40:22AM +0000, monikajg at netzero.net wrote: > Could somebody please explain what is going on for old style classes for the below code: The important part is that the descriptor protocol doesn't get used for old style classes. If that statement means something to you, that's great, otherwise please ask. So in an old-style class: > class GetSet(): > > def __init__(self, value): > self.attrval = value > > @property > def var(self): > print "getting the var attribute" > return self.attrval > @var.setter > def var(self,value): > print "setting the var attribute" > self.attrval = value At this point, you have a class GetSet, with a class attribute: GetSet.var which is a property object. Now let's make an instance: > me = GetSet(5) > me.var = 1000 At this point, the instance now has an instance attribute: me.var which is the int 1000. So there are now TWO attributes called "me", living in different scopes: me.__dict__['var'] = 1000 # in the instance GetSet.__dict__['var'] = property object Remember that attribute look-ups normally look in the instance __dict__ before the class __dict__. > print me.var > del me.var > print me.var The first print looks for 'var' in the instance, finds it, and prints 1000. Then the del command looks for 'var' in the instance, finds it, and deletes it. Then finally the second print looks for 'var' in the instance, *doesn't* find it there (because it has been deleted), so it looks in the class, and finds GetSet.__dict__['var'] which is a property object. At this point, things get a bit mysterious. According to the documentation, Python ought to print something like: since the descriptor protocol doesn't run for old-style classes. But apparently it does *partly* run, because the property object's __get__ method is invoked, which calls the getter method that you defined. > Output: > 1000 > getting the var attribute > 5 So there is a part mystery here. As far as I can tell, the documentation suggests that the output should be: 1000 rather than what you got. -- Steve From Joaquin.Alzola at lebara.com Thu Sep 15 04:35:52 2016 From: Joaquin.Alzola at lebara.com (Joaquin Alzola) Date: Thu, 15 Sep 2016 08:35:52 +0000 Subject: [Tutor] global interpreter lock In-Reply-To: References: Message-ID: > Can someone explain global interpreter lock with some source code examples? Watch this youtube video. Gilectomy https://www.youtube.com/watch?v=fgWUwQVoLHo -- Joaquin This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt. From zemmoura.khalil at gmail.com Thu Sep 15 09:44:44 2016 From: zemmoura.khalil at gmail.com (khalil zakaria Zemmoura) Date: Thu, 15 Sep 2016 14:44:44 +0100 Subject: [Tutor] global interpreter lock In-Reply-To: References: Message-ID: Basically, what that said is the global interpreter lock is something that allows only one thread at a time to be executed when you launch a python program in opposition of executing multiple threads at the same time (parallelism). when you launch a python program it create a process in memory. because of the GIL you cannot do parallelism in an efficient way, but if you are not using threads, you shouldn't care about. The GIL was relevant when we had one processor in our machine and honestly it facilitate the development of the core maintainers of the CPython. Now we have multicore processor, it start to be restrictive for some people, not all but some of them who need to compute data and want to use all the power they have. Instead of giving you some examples I'll give you a link of some great presentations made by David Beazly Understanding the GIL: https://m.youtube.com/watch?v=Obt-vMVdM8s Embracing the GIL: https://m.youtube.com/watch?v=fwzPF2JLoeU Concurrency from the ground: https://m.youtube.com/watch?v=MCs5OvhV9S4 This guy will explain to you better than I I'll do. Hope that helps. Regards. Le 15 sept. 2016 09:18, "anish singh" a ?crit : > Can someone explain global interpreter lock with > some source code examples? > > I didn't understand explanation offered here: > https://docs.python.org/3/glossary.html#term-global-interpreter-lock > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From monikajg at netzero.net Thu Sep 15 17:07:10 2016 From: monikajg at netzero.net (monikajg at netzero.net) Date: Thu, 15 Sep 2016 21:07:10 GMT Subject: [Tutor] @property for old style classes vs new style classes Message-ID: <20160915.140710.14003.1@webmail10.dca.untd.com> For both old and new classes I have var only in GetSet class, but not in instance me. Why? print me.__dict__ print GetSet.__dict__ {'attrval': 5} {'__weakref__': , '__doc__': None, '__module__': '__main__', '__init__': , '__dict__': , 'var': } >>> Thank you very much Monika ---------- Original Message ---------- From: Steven D'Aprano To: tutor at python.org Subject: Re: [Tutor] @property for old style classes vs new style classes Date: Fri, 16 Sep 2016 02:57:12 +1000 On Thu, Sep 15, 2016 at 04:40:22AM +0000, monikajg at netzero.net wrote: > Could somebody please explain what is going on for old style classes for the below code: The important part is that the descriptor protocol doesn't get used for old style classes. If that statement means something to you, that's great, otherwise please ask. So in an old-style class: > class GetSet(): > > def __init__(self, value): > self.attrval = value > > @property > def var(self): > print "getting the var attribute" > return self.attrval > @var.setter > def var(self,value): > print "setting the var attribute" > self.attrval = value At this point, you have a class GetSet, with a class attribute: GetSet.var which is a property object. Now let's make an instance: > me = GetSet(5) > me.var = 1000 At this point, the instance now has an instance attribute: me.var which is the int 1000. So there are now TWO attributes called "me", living in different scopes: me.__dict__['var'] = 1000 # in the instance GetSet.__dict__['var'] = property object Remember that attribute look-ups normally look in the instance __dict__ before the class __dict__. > print me.var > del me.var > print me.var The first print looks for 'var' in the instance, finds it, and prints 1000. Then the del command looks for 'var' in the instance, finds it, and deletes it. Then finally the second print looks for 'var' in the instance, *doesn't* find it there (because it has been deleted), so it looks in the class, and finds GetSet.__dict__['var'] which is a property object. At this point, things get a bit mysterious. According to the documentation, Python ought to print something like: since the descriptor protocol doesn't run for old-style classes. But apparently it does *partly* run, because the property object's __get__ method is invoked, which calls the getter method that you defined. > Output: > 1000 > getting the var attribute > 5 So there is a part mystery here. As far as I can tell, the documentation suggests that the output should be: 1000 rather than what you got. -- Steve _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ____________________________________________________________ Do This Before Bed Tonight to Burn Belly Flab All Night Long Flat Belly Overnight http://thirdpartyoffers.netzero.net/TGL3241/57db0db4a9dedb31f3bst04duc From monikajg at netzero.net Thu Sep 15 17:20:53 2016 From: monikajg at netzero.net (monikajg at netzero.net) Date: Thu, 15 Sep 2016 21:20:53 GMT Subject: [Tutor] @property for old style classes vs new style classes Message-ID: <20160915.142053.14003.2@webmail10.dca.untd.com> Thank you both for your explanations but they were too difficult for me to understand. What is descriptor protocol? Can you please explain? I checked for var in both instance and class __dict__ but it was only in class, not in the instance. I checked for it after I instantiated me, before deletion. So now the post explaining it to me does not make a full sense. So it seem to me that the class on udemy did not explain very well how this @property works (they talked only about new classes). Now after reading the posts several times I feel I understand less how it works. Could somebody please explain how @property works in new classes? What is going on behind the scenes? And then how in comparison with old classes? I will reread the posts again and maybe I will understand it better. Please explain in simple English. :) Thank you very much Monika ---------- Original Message ---------- From: Steven D'Aprano To: tutor at python.org Subject: Re: [Tutor] @property for old style classes vs new style classes Date: Fri, 16 Sep 2016 02:57:12 +1000 On Thu, Sep 15, 2016 at 04:40:22AM +0000, monikajg at netzero.net wrote: > Could somebody please explain what is going on for old style classes for the below code: The important part is that the descriptor protocol doesn't get used for old style classes. If that statement means something to you, that's great, otherwise please ask. So in an old-style class: > class GetSet(): > > def __init__(self, value): > self.attrval = value > > @property > def var(self): > print "getting the var attribute" > return self.attrval > @var.setter > def var(self,value): > print "setting the var attribute" > self.attrval = value At this point, you have a class GetSet, with a class attribute: GetSet.var which is a property object. Now let's make an instance: > me = GetSet(5) > me.var = 1000 At this point, the instance now has an instance attribute: me.var which is the int 1000. So there are now TWO attributes called "me", living in different scopes: me.__dict__['var'] = 1000 # in the instance GetSet.__dict__['var'] = property object Remember that attribute look-ups normally look in the instance __dict__ before the class __dict__. > print me.var > del me.var > print me.var The first print looks for 'var' in the instance, finds it, and prints 1000. Then the del command looks for 'var' in the instance, finds it, and deletes it. Then finally the second print looks for 'var' in the instance, *doesn't* find it there (because it has been deleted), so it looks in the class, and finds GetSet.__dict__['var'] which is a property object. At this point, things get a bit mysterious. According to the documentation, Python ought to print something like: since the descriptor protocol doesn't run for old-style classes. But apparently it does *partly* run, because the property object's __get__ method is invoked, which calls the getter method that you defined. > Output: > 1000 > getting the var attribute > 5 So there is a part mystery here. As far as I can tell, the documentation suggests that the output should be: 1000 rather than what you got. -- Steve _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ____________________________________________________________ Do This Before Bed Tonight to Burn Belly Flab All Night Long Flat Belly Overnight http://thirdpartyoffers.netzero.net/TGL3241/57db10cd7d30310cd7bb9st01duc From monikajg at netzero.net Thu Sep 15 17:48:45 2016 From: monikajg at netzero.net (monikajg at netzero.net) Date: Thu, 15 Sep 2016 21:48:45 GMT Subject: [Tutor] @property for old style classes vs new style classes Message-ID: <20160915.144845.14003.3@webmail10.dca.untd.com> I figured out why you have var in instance __dict__. It was after you added it. But why var is in class __dict__? Does @property make it a class attribute? Thank you Monika ---------- Original Message ---------- From: Steven D'Aprano To: tutor at python.org Subject: Re: [Tutor] @property for old style classes vs new style classes Date: Fri, 16 Sep 2016 02:57:12 +1000 On Thu, Sep 15, 2016 at 04:40:22AM +0000, monikajg at netzero.net wrote: > Could somebody please explain what is going on for old style classes for the below code: The important part is that the descriptor protocol doesn't get used for old style classes. If that statement means something to you, that's great, otherwise please ask. So in an old-style class: > class GetSet(): > > def __init__(self, value): > self.attrval = value > > @property > def var(self): > print "getting the var attribute" > return self.attrval > @var.setter > def var(self,value): > print "setting the var attribute" > self.attrval = value At this point, you have a class GetSet, with a class attribute: GetSet.var which is a property object. Now let's make an instance: > me = GetSet(5) > me.var = 1000 At this point, the instance now has an instance attribute: me.var which is the int 1000. So there are now TWO attributes called "me", living in different scopes: me.__dict__['var'] = 1000 # in the instance GetSet.__dict__['var'] = property object Remember that attribute look-ups normally look in the instance __dict__ before the class __dict__. > print me.var > del me.var > print me.var The first print looks for 'var' in the instance, finds it, and prints 1000. Then the del command looks for 'var' in the instance, finds it, and deletes it. Then finally the second print looks for 'var' in the instance, *doesn't* find it there (because it has been deleted), so it looks in the class, and finds GetSet.__dict__['var'] which is a property object. At this point, things get a bit mysterious. According to the documentation, Python ought to print something like: since the descriptor protocol doesn't run for old-style classes. But apparently it does *partly* run, because the property object's __get__ method is invoked, which calls the getter method that you defined. > Output: > 1000 > getting the var attribute > 5 So there is a part mystery here. As far as I can tell, the documentation suggests that the output should be: 1000 rather than what you got. -- Steve _______________________________________________ Tutor maillist - Tutor at python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ____________________________________________________________ 15 Actors Who Are Gay - No. 12 Will Shock Women trendytribune.com http://thirdpartyoffers.netzero.net/TGL3241/57db177b8815a177b1d3dst03duc From eryksun at gmail.com Fri Sep 16 02:31:35 2016 From: eryksun at gmail.com (eryk sun) Date: Fri, 16 Sep 2016 06:31:35 +0000 Subject: [Tutor] @property for old style classes vs new style classes In-Reply-To: <20160915.144845.14003.3@webmail10.dca.untd.com> References: <20160915.144845.14003.3@webmail10.dca.untd.com> Message-ID: On Thu, Sep 15, 2016 at 9:48 PM, monikajg at netzero.net wrote: > But why var is in class __dict__? > Does @property make it a class attribute? An instance of `property` is a data descriptor, i.e. it implements the __get__, __set__, and __delete__ methods of the descriptor protocol. A non-data descriptor only implements __get__. Descriptors are fundamental to the way attribute lookup works in Python. See the descriptor howto guide: https://docs.python.org/3/howto/descriptor.html The above document paints a picture of attribute access that's not really accurate at times. Also, the example code that attempts to demonstrate type.__getattribute__ in terms of object.__getattribute__ is wrong. (It fails to search the __mro__ of the class, only the metaclass. Also, what it's trying to do is fundamentally impossible. You can't know whether object.__getattribute___ already evaluated a descriptor that it found in the metaclass.) Here's an overview of how object.__getattribute__ works: * If a data descriptor is found in the class, call its __get__ method, passing it the instance and the class, and return the result. * Else if the instance has a dict and the name is found in it, return the value from the instance dict. * Else if a non-data descriptor is found in the class, call its __get__ method, passing it the instance and the class, and return the result. * Else if a non-descriptor is found in the class, return it. * Else raise AttributeError. A class lookup searches the dicts of each class in the __mro__, stopping on the first hit. Also, a type can define a __getattr__ fallback method, in which case the AttributeError raised by __getattribute__ is ignored. For type.__getattribute__, the class object in this case is handled as an instance of the metaclass. The main difference compared to object.__getattribute__ is in the second step, since the instance in this case is a class. Instead of searching just the class dict, it searches the dicts of each class in the __mro__. Also, if it finds a descriptor in the class __mro__, it calls its __get__ method, passing None for the instance. From monikajg at netzero.net Thu Sep 15 22:36:26 2016 From: monikajg at netzero.net (monikajg at netzero.net) Date: Fri, 16 Sep 2016 02:36:26 GMT Subject: [Tutor] maximum recursion error Message-ID: <20160915.193626.20643.0@webmail07.dca.untd.com> Hi: Below is code where mistakenly self.var is named the same as function var instead of a different name. I know that this is not correct but I was experimenting to see what happens. class GetSet(): def __init__(self, value): #self.attrval = value self.var = value @property def var(self): print "getting the var attribute. " #return self.attrval return self.var @var.setter def var(self,value): print "setting the var attribute" #self.attrval = value self.var = value @var.deleter def var(self): print "deleting the var attribute. " #self.attrval = None self.var = None When I run the code with below I get " maximum recursion depth exceeded while calling a Python object" which is what I expected. me = GetSet(5) me.var = 1000 print me.var - I do not get "max recursion error" here. Why? del me.var print me.var - Here I get "max recursion error" What I do not understand is why I do not get the same error when runing below: me = GetSet(5) me.var = 1000 print me.var Why do I get the error after I do del but not without del? Why do I get the error after doing print me.var for the second time but not for the first time? In my understanding I should get the recursion error after the first print me.var. Why do I have to del and print again me.var to get the "max recursion error"? Thank you very much Monika ____________________________________________________________ Do This Before Bed Tonight to Burn Belly Flab All Night Long Flat Belly Overnight http://thirdpartyoffers.netzero.net/TGL3241/57db5afb836b65afb35f7st04duc From __peter__ at web.de Fri Sep 16 09:10:37 2016 From: __peter__ at web.de (Peter Otten) Date: Fri, 16 Sep 2016 15:10:37 +0200 Subject: [Tutor] maximum recursion error References: <20160915.193626.20643.0@webmail07.dca.untd.com> Message-ID: monikajg at netzero.net wrote: > > Hi: > Below is code where mistakenly self.var is named the same as function var > instead of a different name. I know that this is not correct but I was > experimenting to see what happens. > class GetSet(): GetSet is a classic class -- when you use properties with these all bets are off. > > def __init__(self, value): > #self.attrval = value > self.var = value > > @property > def var(self): > print "getting the var attribute. " > #return self.attrval > return self.var > @var.setter > def var(self,value): > print "setting the var attribute" > #self.attrval = value > self.var = value > > @var.deleter > def var(self): > print "deleting the var attribute. " > #self.attrval = None > self.var = None > > When I run the code with below I get " maximum recursion depth exceeded > while calling a Python object" which is what I expected. me = GetSet(5) > me.var = 1000 > print me.var - I do not get "max recursion error" here. Why? > del me.var > print me.var - Here I get "max recursion error" > > What I do not understand is why I do not get the same error when runing > below: > > me = GetSet(5) > me.var = 1000 This updates the instance dict (called __dict__). > print me.var This accesses the instance dict and finds the key "var". When you remove that key with del me.var the next access me.var will fall back from the instance to the class and thus invoke the getter of the var property which itself tries to read me.var and thus causes the recursion error. A sample session in the interactive interpreter: >>> class GetSet: ... @property ... def var(self): ... print "reading var" ... return self.var ... @var.setter ... def var(self, value): ... print "writing var" ... >>> x = GetSet() >>> x.var = 42 >>> x.__dict__ {'var': 42} >>> del x.var >>> x.__dict__ {} >>> import sys; sys.setrecursionlimit(7) >>> x.var reading var reading var reading var Traceback (most recent call last): File "", line 1, in File "", line 5, in var File "", line 5, in var File "", line 5, in var RuntimeError: maximum recursion depth exceeded while calling a Python object To make it crystal clear, classic classes are not intended to work with properties -- the support for getters is likely an implementation accident. Once you switch to newstyle classes sanity is restored, and the setter is no longer bypassed: >>> class GS(GetSet, object): pass ... >>> GS().var = 123 writing var > > Why do I get the error after I do del but not without del? Why do I get > the error after doing print me.var for the second time but not for the > first time? In my understanding I should get the recursion error after the > first print me.var. Why do I have to del and print again me.var to get the > "max recursion error"? Thank you very much Monika From dvnsarma at gmail.com Fri Sep 16 10:26:16 2016 From: dvnsarma at gmail.com (=?UTF-8?B?RC5WLk4uU2FybWEg4LCh4LC/LuCwteCwvy7gsI7gsKjgsY0u4LC24LCw4LGN4LCu?=) Date: Fri, 16 Sep 2016 19:56:16 +0530 Subject: [Tutor] maximum recursion error In-Reply-To: References: <20160915.193626.20643.0@webmail07.dca.untd.com> Message-ID: Can somebody tell me why my IDLE hangs when asked setrecursionlimit. >>> x=GetSet(40) >>> x.var 40 >>> x.__dict__ {'var': 40} >>> del x.var >>> x.__dict__ {} >>> import sys >>> sys.setrecursionlimit(7) regards, Sarma. From __peter__ at web.de Fri Sep 16 11:16:40 2016 From: __peter__ at web.de (Peter Otten) Date: Fri, 16 Sep 2016 17:16:40 +0200 Subject: [Tutor] IDLE hangs after sys.setrecursionlimit(), was Re: maximum recursion error References: <20160915.193626.20643.0@webmail07.dca.untd.com> Message-ID: D.V.N.Sarma ??.??.???.???? wrote: > Can somebody tell me why my IDLE hangs when asked setrecursionlimit. > >>>> x=GetSet(40) >>>> x.var > 40 >>>> x.__dict__ > {'var': 40} >>>> del x.var >>>> x.__dict__ > {} >>>> import sys >>>> sys.setrecursionlimit(7) IDLE's commandline emulation needs the stack you just drastically reduced in size to communicate with the GUI. It looks like the code is not written to expect and cope with such a communication problem. When I start IDLE from the commandline and try a slightly higher value Python 2.7.6 (default, Jun 22 2015, 17:58:13) [GCC 4.8.2] on linux2 Type "copyright", "credits" or "license()" for more information. >>> import sys; sys.setrecursionlimit(25) >>> ================================ RESTART ================================ I see the following $ idle ---------------------------------------- Unhandled server exception! Thread: SockThread Client Address: ('127.0.0.1', 51058) Request: Traceback (most recent call last): File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock self.process_request(request, client_address) File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request self.finish_request(request, client_address) File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request self.RequestHandlerClass(request, client_address, self) File "/usr/lib/python2.7/idlelib/rpc.py", line 503, in __init__ SocketServer.BaseRequestHandler.__init__(self, sock, addr, svr) File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__ self.handle() File "/usr/lib/python2.7/idlelib/run.py", line 291, in handle rpc.RPCHandler.getresponse(self, myseq=None, wait=0.05) File "/usr/lib/python2.7/idlelib/rpc.py", line 280, in getresponse response = self._getresponse(myseq, wait) File "/usr/lib/python2.7/idlelib/rpc.py", line 300, in _getresponse response = self.pollresponse(myseq, wait) File "/usr/lib/python2.7/idlelib/rpc.py", line 415, in pollresponse qmsg = response_queue.get(0) File "/usr/lib/python2.7/Queue.py", line 164, in get if not self._qsize(): RuntimeError: maximum recursion depth exceeded *** Unrecoverable, server exiting! ---------------------------------------- When I continue to use smaller stack sizes at >>> ================================ RESTART ================================ >>> import sys; sys.setrecursionlimit(17) I get Unhandled server exception! Thread: SockThread Client Address: ('127.0.0.1', 51058) Request: Traceback (most recent call last): File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock Unhandled exception in thread started by and IDLE indeed hangs. From swallace61 at embarqmail.com Fri Sep 16 19:08:44 2016 From: swallace61 at embarqmail.com (Sharon Wallace) Date: Fri, 16 Sep 2016 19:08:44 -0400 Subject: [Tutor] Help with Max Number and Min number script Message-ID: <000001d2106f$46e9d6f0$d4bd84d0$@embarqmail.com> inp = raw_input largest = None smallest = None while True: num = raw_input('Enter a number: ') if num = 'done' : break print num try : num = float(inp) except : print 'Invalid input' continue if largest is None or num > largest : largest = num if smallest is None or num < smallest : smallest = num print 'Maximum is:', largest print 'Minimum is:', smallest From joel.goldstick at gmail.com Fri Sep 16 20:30:41 2016 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Fri, 16 Sep 2016 20:30:41 -0400 Subject: [Tutor] Help with Max Number and Min number script In-Reply-To: <000001d2106f$46e9d6f0$d4bd84d0$@embarqmail.com> References: <000001d2106f$46e9d6f0$d4bd84d0$@embarqmail.com> Message-ID: On Fri, Sep 16, 2016 at 7:08 PM, Sharon Wallace wrote: > inp = raw_input The above doesn't do what you think. It creates a name called inp which is bound to the function raw_input > > largest = None > > smallest = None > > > > while True: > > num = raw_input('Enter a number: ') > > if num = 'done' : break > > print num > > > > try : > > num = float(inp) > > except : > > print 'Invalid input' > > continue > > > > if largest is None or num > largest : > > largest = num > > > > if smallest is None or num < smallest : > > smallest = num > > > > print 'Maximum is:', largest > > print 'Minimum is:', smallest > > > What is your question? What works, or doesn't work? > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From bgailer at gmail.com Fri Sep 16 20:36:08 2016 From: bgailer at gmail.com (Bob Gailer) Date: Fri, 16 Sep 2016 20:36:08 -0400 Subject: [Tutor] Help with Max Number and Min number script In-Reply-To: <000001d2106f$46e9d6f0$d4bd84d0$@embarqmail.com> References: <000001d2106f$46e9d6f0$d4bd84d0$@embarqmail.com> Message-ID: On Sep 16, 2016 8:12 PM, "Sharon Wallace" wrote: > In addition to showing us your code , Please show us whatever Trace back you got when you ran the code. I would guess you are either getting an indentation error or syntax error in the vicinity of line that begins if num =. > inp = raw_input > > largest = None > > smallest = None > > > > while True: > > num = raw_input('Enter a number: ') > > if num = 'done' : break > > print num > > > > try : > > num = float(inp) > > except : > > print 'Invalid input' > > continue > > > > if largest is None or num > largest : > > largest = num > > > > if smallest is None or num < smallest : > > smallest = num > > > > print 'Maximum is:', largest > > print 'Minimum is:', smallest > > > > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From bgailer at gmail.com Sat Sep 17 17:15:18 2016 From: bgailer at gmail.com (Bob Gailer) Date: Sat, 17 Sep 2016 17:15:18 -0400 Subject: [Tutor] Help with Max Number and Min number script In-Reply-To: <001f01d21125$c74dc2f0$55e948d0$@embarqmail.com> References: <000001d2106f$46e9d6f0$d4bd84d0$@embarqmail.com> <001901d2107c$888655e0$999301a0$@embarqmail.com> <001f01d21125$c74dc2f0$55e948d0$@embarqmail.com> Message-ID: On Sep 17, 2016 4:55 PM, "Sharon Wallace" wrote: > > Would you please help me with the last piece of this assignment? > > > > This is my code: > > > > largest = None > > smallest = None > > > > while True: > > num = raw_input("Enter a number: ") > > if num == "done" : break > > print num > > > > try : > > num = int(num) > > except : > > print "Invalid input" > > continue > > > > if num > largest : > comparing integers To None is not a good idea. Your earlier program had it right when you tested for None and greater than / less then. Another way to tackle this is to initialize largest to some extremely large negative value and I smallest to some extremely large positive value. When replying to these emails please always include the tutor group. Congratulations on persisting and making it this far. > largest = num > > > > if num < smallest : > > smallest = num > > > > print 'Maximum is', largest > > print 'Minimum is', smallest . From alan.gauld at yahoo.co.uk Sat Sep 17 20:20:10 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 18 Sep 2016 01:20:10 +0100 Subject: [Tutor] Help with Max Number and Min number script In-Reply-To: <000001d2106f$46e9d6f0$d4bd84d0$@embarqmail.com> References: <000001d2106f$46e9d6f0$d4bd84d0$@embarqmail.com> Message-ID: On 17/09/16 00:08, Sharon Wallace wrote: > inp = raw_input > largest = None > smallest = None > > while True: > num = raw_input('Enter a number: ') > if num = 'done' : break > print num > try : > num = float(inp) > except : > print 'Invalid input' > continue While some whitespace is good, too much is as bad as none. I've removed most of the blank lines but your indentation levels are way too big. Also they are inconsistent and in some places wrong. Indentation is crucially important in Python. Your code above should look like: largest = None smallest = None while True: num = raw_input('Enter a number: ') if num = 'done' : break print num try : num = float(inp) except : print 'Invalid input' continue That uses the recommended 4 spaces indentation and makes it much easier to see what is going on. hth -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Sun Sep 18 19:37:36 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 19 Sep 2016 00:37:36 +0100 Subject: [Tutor] Help with Max Number and Min number script In-Reply-To: <000001d2106f$46e9d6f0$d4bd84d0$@embarqmail.com> References: <000001d2106f$46e9d6f0$d4bd84d0$@embarqmail.com> Message-ID: On 17/09/16 00:08, Sharon Wallace wrote: > largest = None > smallest = None > > while True: > num = raw_input('Enter a number: ') > if num = 'done' : break should use == to test equality a single = is an assignment and would give an error here print num try : num = float(inp) except : Don't use a bare except here, specify the error, otherwise you potentially miss a lot of useful debug information. Specifically you want to catch ValueError here. print 'Invalid input' continue if largest is None or num > largest : largest = num if smallest is None or num < smallest : smallest = num > print 'Maximum is:', largest > print 'Minimum is:', smallest Fix those errors and it should work -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From TGampper at mccneb.edu Mon Sep 19 19:01:30 2016 From: TGampper at mccneb.edu (Gampper, Terry) Date: Mon, 19 Sep 2016 23:01:30 +0000 Subject: [Tutor] New to Python, Already Confused Message-ID: Hello I started my journey with Python last week and find it to be an easy-to-learn language. I am currently teaching introduction to computer programming. One of the assignments we are working on involves an instructor giving a series of 5 tests, and the lowest score is dropped, then calculating the percent. Here is my attempt at coding: name = input("Enter Student's Name: " ) tscore1 = input ("Enter test score 1: ") tscore2 = input ("Enter test score 2: ") tscore3 = input ("Enter test score 3: ") tscore4 = input ("Enter test score 4: ") tscore5 = input ("Enter test score 5: ") total_tscore = int(tscore1) + int(tscore2) + int(tscore3) + int(tscore4) + int(tscore5) low_tscore = min(tscore1,tscore2,tscore3,tscore4,tscore5) adjusted_tscore = int(total_tscore) - int(low_tscore) percent = int(adjusted_tscore/400*100) print("The sum of the 5 tests is",total_tscore) print("The lowest test score is thrown out",low_tscore) print("The adjusted score is",adjusted_tscore) print("Student",name, "has a score of",percent, "percent") I ran the program several times using random numbers and I found that the results were not consistent. Here are a couple of runs: ============ RESTART: /Users/tgampper/Documents/Python/assign2.py ============ Enter Student's Name: Joe Enter test score 1: 78 Enter test score 2: 89 Enter test score 3: 90 Enter test score 4: 78 Enter test score 5: 99 The sum of the 5 tests is 434 The lowest test score is thrown out 78 The adjusted score is 356 Student Joe has a score of 89 percent >>> ============ RESTART: /Users/tgampper/Documents/Python/assign2.py ============ Enter Student's Name: Sally Enter test score 1: 78 Enter test score 2: 89 Enter test score 3: 90 Enter test score 4: 78 Enter test score 5: 100 The sum of the 5 tests is 435 The lowest test score is thrown out 100 The adjusted score is 335 Student Sally has a score of 83 percent >>> Notice the scores for test 5 for each student. My conclusion is that if the score is <100, the lowest score is properly displayed, but if the score is >=100, then the lowest score is equal to the highest score. Doesn't make sense to me. Do you have any ideas? Thanks! -- Terry Gampper Adjunct INFO Instructor Metropolitan Community College, Omaha NE From gb.gabrielebrambilla at gmail.com Tue Sep 20 11:48:47 2016 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Tue, 20 Sep 2016 11:48:47 -0400 Subject: [Tutor] job killed: too high numbers? Message-ID: Hi, I have this script import numpy as np import random as rnd from math import * import matplotlib.pyplot as plt folder = '../NOBACKUP/heavi3/' pre = '10' dat = '.dat' snshot = '4189' d = 0.02 d2 = d/2.0 tot = 156 half = 78 points = 33750000000 BxN = np.zeros((tot,tot,tot)) ByN = np.zeros((tot,tot,tot)) BzN = np.zeros((tot,tot,tot)) ExN = np.zeros((tot,tot,tot)) EyN = np.zeros((tot,tot,tot)) EzN = np.zeros((tot,tot,tot)) Egnotte = [] ExS = [1,0,0] EyS = [0,1,0] EzS = [0,0,1] BxS = [0,1,1] ByS = [1,0,1] BzS = [1,1,0] numpyArrays =[ExN]#, EyN, EzN, BxN, ByN, BzN] FC = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] fields = ['EX_']#, 'EY_', 'EZ_', 'BX_', 'BY_', 'BZ_'] stagger = [ExS]#, EyS, EzS, BxS, ByS, BzS] print 'im here' for field, arry, stag in zip(fields, numpyArrays, stagger): g=0 fname = folder+ field + pre + snshot + dat print fname f = open(fname, 'rb') print "pastopens" for iw in range(points): print iw But when I run it it is killed in the for cycle, it doesn't print any number: [gabriele:~/Desktop/GITcode] gbrambil% python EknotFromKostas.py im here ../NOBACKUP/heavi3/EX_104189.dat pastopens Killed does it mean that my number of points is too high? Thanks GB From joel.goldstick at gmail.com Tue Sep 20 12:07:28 2016 From: joel.goldstick at gmail.com (Joel Goldstick) Date: Tue, 20 Sep 2016 12:07:28 -0400 Subject: [Tutor] New to Python, Already Confused In-Reply-To: References: Message-ID: You should probably convert scores to ints on input. At any rate, here is your problem: low_tscore = min(tscore1,tscore2,tscore3,tscore4,tscore5) You need to convert tscore1, etc. to ints, otherwise min will give lexical comparison (alphabetic order), not numeric order On Mon, Sep 19, 2016 at 7:01 PM, Gampper, Terry wrote: > Hello > I started my journey with Python last week and find it to be an easy-to-learn language. I am currently teaching introduction to computer programming. One of the assignments we are working on involves an instructor giving a series of 5 tests, and the lowest score is dropped, then calculating the percent. Here is my attempt at coding: > > name = input("Enter Student's Name: " ) > tscore1 = input ("Enter test score 1: ") > tscore2 = input ("Enter test score 2: ") > tscore3 = input ("Enter test score 3: ") > tscore4 = input ("Enter test score 4: ") > tscore5 = input ("Enter test score 5: ") > total_tscore = int(tscore1) + int(tscore2) + int(tscore3) + int(tscore4) + int(tscore5) > low_tscore = min(tscore1,tscore2,tscore3,tscore4,tscore5) > adjusted_tscore = int(total_tscore) - int(low_tscore) > percent = int(adjusted_tscore/400*100) > print("The sum of the 5 tests is",total_tscore) > print("The lowest test score is thrown out",low_tscore) > print("The adjusted score is",adjusted_tscore) > print("Student",name, "has a score of",percent, "percent") > > I ran the program several times using random numbers and I found that the results were not consistent. Here are a couple of runs: > > ============ RESTART: /Users/tgampper/Documents/Python/assign2.py ============ > Enter Student's Name: Joe > Enter test score 1: 78 > Enter test score 2: 89 > Enter test score 3: 90 > Enter test score 4: 78 > Enter test score 5: 99 > The sum of the 5 tests is 434 > The lowest test score is thrown out 78 > The adjusted score is 356 > Student Joe has a score of 89 percent >>>> > ============ RESTART: /Users/tgampper/Documents/Python/assign2.py ============ > Enter Student's Name: Sally > Enter test score 1: 78 > Enter test score 2: 89 > Enter test score 3: 90 > Enter test score 4: 78 > Enter test score 5: 100 > The sum of the 5 tests is 435 > The lowest test score is thrown out 100 > The adjusted score is 335 > Student Sally has a score of 83 percent >>>> > > Notice the scores for test 5 for each student. My conclusion is that if the score is <100, the lowest score is properly displayed, but if the score is >=100, then the lowest score is equal to the highest score. Doesn't make sense to me. Do you have any ideas? Thanks! > -- > Terry Gampper > Adjunct INFO Instructor > Metropolitan Community College, Omaha NE > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor -- Joel Goldstick http://joelgoldstick.com/blog http://cc-baseballstats.info/stats/birthdays From gb.gabrielebrambilla at gmail.com Tue Sep 20 12:52:09 2016 From: gb.gabrielebrambilla at gmail.com (Gabriele Brambilla) Date: Tue, 20 Sep 2016 12:52:09 -0400 Subject: [Tutor] job killed: too high numbers? In-Reply-To: <14963d35-ca44-82c6-84eb-5d6cc244a137@gmail.com> References: <14963d35-ca44-82c6-84eb-5d6cc244a137@gmail.com> Message-ID: Hi, The number was high but it was only a sort of test.now I learned how it works, thanks Gb On Sep 20, 2016 12:48 PM, "Matt Ruffalo" wrote: > Hello- > > On 2016-09-20 11:48, Gabriele Brambilla wrote: > > does it mean that my number of points is too high? > > In short, yes. From your usage of the 'print' statement, you are running > the code under Python 2.x. In this version of Python, the 'range' > function creates a full list of numbers, and so you are asking 'range' > to create a list of 33.8 billion integers. Python lists are essentially > implemented in C as dense arrays of pointers to PyObject structs, so in > addition to the actual numeric values, you will need eight bytes per > value in the list (assuming a 64-bit OS and Python build). This is > already 270GB of memory just for these pointers, in addition to the > actual numeric values, which might take up to an additional 135GB (if > each numeric value is stored as a 32-bit integer). Are you running this > on a machine with ?405GB memory? > > To solve your immediate problem, you could replace 'range' with 'xrange' > in your code, but this will probably only allow you to encounter another > problem: this loop will take a *very* long time to run, even without > doing any numerical work inside it. Unfortunately, there's no way to > suggest any algorithm/numerical analysis improvements without more > information about what you're trying to accomplish. > > MMR... > From __peter__ at web.de Tue Sep 20 12:55:41 2016 From: __peter__ at web.de (Peter Otten) Date: Tue, 20 Sep 2016 18:55:41 +0200 Subject: [Tutor] job killed: too high numbers? References: Message-ID: Gabriele Brambilla wrote: > Hi, > > I have this script > points = 33750000000 > for iw in range(points): > print iw > > > But when I run it it is killed in the for cycle, it doesn't print any > number: > > [gabriele:~/Desktop/GITcode] gbrambil% python EknotFromKostas.py > im here > ../NOBACKUP/heavi3/EX_104189.dat > pastopens > Killed > > does it mean that my number of points is too high? In Python 2 range(points) creates a list. To store only the object references in the list (the actual int object takes much more memory) you need >>> 33750000000 * 8 270000000000 Bytes or >>> _ / 2**30 251.4570951461792 i. e. over 250 GiB of RAM on a 64 bit system. While you can replace range() with xrange() in Python 2 or switch to Python 3 where range() is an object >>> range(33750000000) range(0, 33750000000) you should start with much smaller numbers and then extrapolate the expected runtime and memory consumption of your script once it does anything useful. From wolfgang.maier at biologie.uni-freiburg.de Tue Sep 20 12:44:14 2016 From: wolfgang.maier at biologie.uni-freiburg.de (Wolfgang Maier) Date: Tue, 20 Sep 2016 18:44:14 +0200 Subject: [Tutor] job killed: too high numbers? In-Reply-To: References: Message-ID: <2768d475-5183-0c69-f81a-a8c0636c8263@biologie.uni-freiburg.de> On 20.09.2016 17:48, Gabriele Brambilla wrote: > Hi, > > I have this script > > import numpy as np > import random as rnd > from math import * > import matplotlib.pyplot as plt > > folder = '../NOBACKUP/heavi3/' > pre = '10' > dat = '.dat' > snshot = '4189' > d = 0.02 > d2 = d/2.0 > tot = 156 > half = 78 > points = 33750000000 > > BxN = np.zeros((tot,tot,tot)) > ByN = np.zeros((tot,tot,tot)) > BzN = np.zeros((tot,tot,tot)) > ExN = np.zeros((tot,tot,tot)) > EyN = np.zeros((tot,tot,tot)) > EzN = np.zeros((tot,tot,tot)) > Egnotte = [] > ExS = [1,0,0] > EyS = [0,1,0] > EzS = [0,0,1] > BxS = [0,1,1] > ByS = [1,0,1] > BzS = [1,1,0] > > numpyArrays =[ExN]#, EyN, EzN, BxN, ByN, BzN] > FC = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] > fields = ['EX_']#, 'EY_', 'EZ_', 'BX_', 'BY_', 'BZ_'] > stagger = [ExS]#, EyS, EzS, BxS, ByS, BzS] > print 'im here' > for field, arry, stag in zip(fields, numpyArrays, stagger): > g=0 > fname = folder+ field + pre + snshot + dat > print fname > f = open(fname, 'rb') > print "pastopens" > for iw in range(points): range() in Python 2 returns a list of all the numbers in the range. With points set to 33750000000, as in your code, the list will have that many integers and a block of memory that may hold all of them is probably very hard to obtain on most machines. The solution is to use xrange() which yields values on demand, i.e. 1 single integer everytime through the for loop. As one of many small improvements in Python 3, range behaves like Python 2 xrange, so this kind of problem cannot happen there. Best, Wolfgang > print iw > > > But when I run it it is killed in the for cycle, it doesn't print any > number: > > [gabriele:~/Desktop/GITcode] gbrambil% python EknotFromKostas.py > im here > ../NOBACKUP/heavi3/EX_104189.dat > pastopens > Killed > > does it mean that my number of points is too high? > > Thanks > > GB > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From steve at pearwood.info Tue Sep 20 12:06:42 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 21 Sep 2016 02:06:42 +1000 Subject: [Tutor] New to Python, Already Confused In-Reply-To: References: Message-ID: <20160920160642.GO22471@ando.pearwood.info> On Mon, Sep 19, 2016 at 11:01:30PM +0000, Gampper, Terry wrote: > Hello > I started my journey with Python last week and find it to be an > easy-to-learn language. I am currently teaching introduction to > computer programming. One of the assignments we are working on > involves an instructor giving a series of 5 tests, and the lowest > score is dropped, then calculating the percent. Here is my attempt at > coding: Thank you for showing your code! Do I understand that you are the *teacher* in this course, rather than a student? We try not to do student's work for them, although we may sometimes guide them towards the right direction. But if you are the teacher, then of course we're happy to help. > name = input("Enter Student's Name: " ) > tscore1 = input ("Enter test score 1: ") > tscore2 = input ("Enter test score 2: ") > tscore3 = input ("Enter test score 3: ") > tscore4 = input ("Enter test score 4: ") > tscore5 = input ("Enter test score 5: ") Here the various scores are all strings. I recommend that you convert them to int immediately: tscore1 = int(input("Enter test score 1: ")) etc. That will avoid the problems you have below. > total_tscore = int(tscore1) + int(tscore2) + int(tscore3) + int(tscore4) + int(tscore5) You add the scores as numbers. But the next line: > low_tscore = min(tscore1,tscore2,tscore3,tscore4,tscore5) you pick the score that comes first *as a string*, not as a number. String comparisons are in lexicographic order: "1000" < "9" because "1" comes before "9" but 1000 > 9 because 1000 is a larger number. So that's why you're getting unexpected results for the lowest score. > adjusted_tscore = int(total_tscore) - int(low_tscore) No need to convert total_tscore into an int, as it already is one. > percent = int(adjusted_tscore/400*100) This will truncate the percentages to the whole number part. That is, if the percentage calculates as (say) 49.9999%, it will round down to 49, rather than round to the nearest value 50. Rather than int(), perhaps you should use round()? Hope that helps! -- Steven From steve at pearwood.info Tue Sep 20 12:14:16 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 21 Sep 2016 02:14:16 +1000 Subject: [Tutor] job killed: too high numbers? In-Reply-To: References: Message-ID: <20160920161415.GP22471@ando.pearwood.info> On Tue, Sep 20, 2016 at 11:48:47AM -0400, Gabriele Brambilla wrote: > Hi, > > I have this script [...] > print "pastopens" > for iw in range(points): > print iw > > > But when I run it it is killed in the for cycle, it doesn't print any > number: > > [gabriele:~/Desktop/GITcode] gbrambil% python EknotFromKostas.py > im here > ../NOBACKUP/heavi3/EX_104189.dat > pastopens > Killed You're running Linux, right? I haven't actually seen that "Killed" before. I wonder if it has something to do with the OOM Killer? Can you check your logs and see if the Python process was killed? http://stackoverflow.com/questions/624857/finding-which-process-was-killed-by-linux-oom-killer Otherwise, I have no idea what's going on, but I'd be fascinated to find out. What happens if you watch the system in top while the script is running? > does it mean that my number of points is too high? Possibly. -- Steve From matt.ruffalo at gmail.com Tue Sep 20 12:48:34 2016 From: matt.ruffalo at gmail.com (Matt Ruffalo) Date: Tue, 20 Sep 2016 12:48:34 -0400 Subject: [Tutor] job killed: too high numbers? In-Reply-To: References: Message-ID: <14963d35-ca44-82c6-84eb-5d6cc244a137@gmail.com> Hello- On 2016-09-20 11:48, Gabriele Brambilla wrote: > does it mean that my number of points is too high? In short, yes. From your usage of the 'print' statement, you are running the code under Python 2.x. In this version of Python, the 'range' function creates a full list of numbers, and so you are asking 'range' to create a list of 33.8 billion integers. Python lists are essentially implemented in C as dense arrays of pointers to PyObject structs, so in addition to the actual numeric values, you will need eight bytes per value in the list (assuming a 64-bit OS and Python build). This is already 270GB of memory just for these pointers, in addition to the actual numeric values, which might take up to an additional 135GB (if each numeric value is stored as a 32-bit integer). Are you running this on a machine with ?405GB memory? To solve your immediate problem, you could replace 'range' with 'xrange' in your code, but this will probably only allow you to encounter another problem: this loop will take a *very* long time to run, even without doing any numerical work inside it. Unfortunately, there's no way to suggest any algorithm/numerical analysis improvements without more information about what you're trying to accomplish. MMR... From random832 at fastmail.com Tue Sep 20 12:00:30 2016 From: random832 at fastmail.com (Random832) Date: Tue, 20 Sep 2016 12:00:30 -0400 Subject: [Tutor] job killed: too high numbers? In-Reply-To: References: Message-ID: <1474387230.2742301.731571345.4C7719EF@webmail.messagingengine.com> On Tue, Sep 20, 2016, at 11:48, Gabriele Brambilla wrote: > I have this script ... > points = 33750000000 ... > for iw in range(points): > print iw > does it mean that my number of points is too high? Probably. The most likely thing to cause something to exit with a status of "Killed" on Linux is that your system ran out of memory. In particular, on Python 2, the "range" function returns a real list that allocates space for all those numbers from 0 to 33749999999. Try using xrange instead, or if you can, switch to Python 3. From alan.gauld at yahoo.co.uk Tue Sep 20 18:34:35 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 20 Sep 2016 23:34:35 +0100 Subject: [Tutor] Testing tutor list server Message-ID: <57E1B97B.2080907@yahoo.co.uk> Just checkin' -- 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 aaron.rose.tx at gmail.com Wed Sep 21 11:14:49 2016 From: aaron.rose.tx at gmail.com (Aaron Rose) Date: Wed, 21 Sep 2016 10:14:49 -0500 Subject: [Tutor] Python IDLE for Windows 10, 64-bit Message-ID: Hi, I'm looking for a link for Python IDLE for Windows 10, 64-bit. Can't seem to find the right link on python.org. Could someone kindly point me in the right direction? Thank you. From dentingerpaul at gmail.com Wed Sep 21 10:53:36 2016 From: dentingerpaul at gmail.com (Paul Dentinger) Date: Wed, 21 Sep 2016 07:53:36 -0700 Subject: [Tutor] help with Numpy Message-ID: Hello, Sorry for such a basic question, but I am trying to get Numpy to work. I have python 3.5 and working in IDLE. I need Numpy and Scipy and may need matplotlib. So I tried to call Numpy and it can't be found. So I go online and find Numpy and download it. But because it's not an executable installer, it's sitting in my downloads. What I can't find is where to put NumPy such that Python can find it when I call it? I'm a little amazed that I cannot find this. Alternatively, if there is an executable installer for these, I would appreciate it. Thanks, Paul From eryksun at gmail.com Wed Sep 21 13:01:52 2016 From: eryksun at gmail.com (eryk sun) Date: Wed, 21 Sep 2016 17:01:52 +0000 Subject: [Tutor] Python IDLE for Windows 10, 64-bit In-Reply-To: References: Message-ID: On Wed, Sep 21, 2016 at 3:14 PM, Aaron Rose wrote: > I'm looking for a link for Python IDLE for Windows 10, 64-bit. Can't seem to find > the right link on python.org. Could someone kindly point me in the right direction? Python's installer has an option to install IDLE. Here's the link for the latest stable version of the web-based installer for x64: https://www.python.org/ftp/python/3.5.2/python-3.5.2-amd64-webinstall.exe From eryksun at gmail.com Wed Sep 21 13:58:53 2016 From: eryksun at gmail.com (eryk sun) Date: Wed, 21 Sep 2016 17:58:53 +0000 Subject: [Tutor] help with Numpy In-Reply-To: References: Message-ID: On Wed, Sep 21, 2016 at 2:53 PM, Paul Dentinger wrote: > > I need Numpy and Scipy and may need matplotlib. So I tried to call Numpy and it > can't be found. So I go online and find Numpy and download it. Read the following docs on Python packaging and pip: https://pip.pypa.io https://packaging.python.org I recommend Christoph Gohlke's MKL builds of NumPy and SciPy: http://www.lfd.uci.edu/~gohlke/pythonlibs From alan.gauld at yahoo.co.uk Wed Sep 21 17:21:35 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 21 Sep 2016 22:21:35 +0100 Subject: [Tutor] Python IDLE for Windows 10, 64-bit In-Reply-To: References: Message-ID: On 21/09/16 16:14, Aaron Rose wrote: > Hi, I'm looking for a link for Python IDLE for Windows 10, 64-bit. On Windows it is usually installed by default at: %PYTHONDIR%/Lib/idelib/idle.bat Where %PYTHONDIR% is the folder where you installed Python. Create a shortcut to that and you should be good to go. However there is an enhanced IDLE that I strongly recommend called IdleX. Google it and install it somewhere on your system then create a shortcut to pythonw idlex.py To gainfeatrures such as tabbed windows, folding code etc http://idlex.sourceforge.net/features.html -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Wed Sep 21 17:27:02 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 21 Sep 2016 22:27:02 +0100 Subject: [Tutor] help with Numpy In-Reply-To: References: Message-ID: On 21/09/16 15:53, Paul Dentinger wrote: > Hello, > Sorry for such a basic question, but I am trying to get Numpy to work. I > have python 3.5 and working in IDLE. I need Numpy and Scipy and may need > matplotlib. To be honest while you can install all the bits separately I normally recommend just getting a distro that already includes everything SciPy related. Anaconda or Canopy are two that I can recommend. https://www.continuum.io/downloads https://store.enthought.com/downloads/#default It just means that everything is there and although there may be more than you need it saves any hassles with dependencies 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 eryksun at gmail.com Wed Sep 21 17:44:06 2016 From: eryksun at gmail.com (eryk sun) Date: Wed, 21 Sep 2016 21:44:06 +0000 Subject: [Tutor] Python IDLE for Windows 10, 64-bit In-Reply-To: References: Message-ID: On Wed, Sep 21, 2016 at 9:21 PM, Alan Gauld via Tutor wrote: > On Windows it is usually installed by default at: > > %PYTHONDIR%/Lib/idelib/idle.bat With Python 3, you can run the package as a script: pythonw -m idlelib Python 2: pythonw -m idlelib.idle From oscar.j.benjamin at gmail.com Wed Sep 21 18:35:09 2016 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Wed, 21 Sep 2016 23:35:09 +0100 Subject: [Tutor] help with Numpy In-Reply-To: References: Message-ID: On 21 Sep 2016 22:29, "Alan Gauld via Tutor" wrote: > > On 21/09/16 15:53, Paul Dentinger wrote: > > Hello, > > Sorry for such a basic question, but I am trying to get Numpy to work. I > > have python 3.5 and working in IDLE. I need Numpy and Scipy and may need > > matplotlib. > > To be honest while you can install all the bits separately > I normally recommend just getting a distro that already includes > everything SciPy related. I would have given the same advice a year or so ago. It's worth noting though that the situation with pip, wheel etc has improved significantly recently. It's now straightforward to pip install Numpy, scipy, matplotlib and many others on Windows, OSX and Linux. Try it! -- Oscar From eryksun at gmail.com Wed Sep 21 23:47:13 2016 From: eryksun at gmail.com (eryk sun) Date: Thu, 22 Sep 2016 03:47:13 +0000 Subject: [Tutor] help with Numpy In-Reply-To: References: Message-ID: On Wed, Sep 21, 2016 at 10:35 PM, Oscar Benjamin wrote: > I would have given the same advice a year or so ago. It's worth noting > though that the situation with pip, wheel etc has improved significantly > recently. It's now straightforward to pip install Numpy, scipy, matplotlib > and many others on Windows, OSX and Linux. Try it! There are NumPy and Matplotlib wheels for Windows on PyPI, but not for SciPy. Building SciPy from source on Windows is not straightforward. If you try to build with just VS 2015 installed, it fails because the required LAPACK/BLAS libs aren't available, and there's no Fortran compiler (Intel MKL is not free). For Windows users, I recommend downloading the wheels for NumPy and SciPy from Gohlke's site -- or just use a batteries-included distro as Alan recommended. From oscar.j.benjamin at gmail.com Thu Sep 22 05:43:12 2016 From: oscar.j.benjamin at gmail.com (Oscar Benjamin) Date: Thu, 22 Sep 2016 10:43:12 +0100 Subject: [Tutor] help with Numpy In-Reply-To: References: Message-ID: On 22 September 2016 at 04:47, eryk sun wrote: > On Wed, Sep 21, 2016 at 10:35 PM, Oscar Benjamin > wrote: >> I would have given the same advice a year or so ago. It's worth noting >> though that the situation with pip, wheel etc has improved significantly >> recently. It's now straightforward to pip install Numpy, scipy, matplotlib >> and many others on Windows, OSX and Linux. Try it! > > There are NumPy and Matplotlib wheels for Windows on PyPI, but not for > SciPy. Building SciPy from source on Windows is not straightforward. > If you try to build with just VS 2015 installed, it fails because the > required LAPACK/BLAS libs aren't available, and there's no Fortran > compiler (Intel MKL is not free). For Windows users, I recommend > downloading the wheels for NumPy and SciPy from Gohlke's site -- or > just use a batteries-included distro as Alan recommended. Oh yes I forgot about that. So all combinations except Windows+scipy are now working. I thought the mingwpy project would have solved this by now but it looks to be stalled. -- Oscar From sjeik_appie at hotmail.com Thu Sep 22 10:58:04 2016 From: sjeik_appie at hotmail.com (Albert-Jan Roskam) Date: Thu, 22 Sep 2016 14:58:04 +0000 Subject: [Tutor] Array of Tkinter objects? Message-ID: Hi, I have created a grid of Tkinter Entry object that looks a bit like a sheet in MS Excel. The data come from a pandas.DataFrame. I use a nested loop, one for rows and one for cols. Inside the loop, I create the Entry instances which I also grid() right away. This works nicely, though a little slow. What data structure would be suitable so I can store the instances in it? I want to be able to easily call grid() on all of them. Also, I want to easily call .configure() on subsets, for instance to change the background color of a row when a certain value in that row is too high/low. A numpy array of np.objects might be okay, but it is called NUMpy for a reason, no? Still, the way of indexing seems very useful/intuitive/neat here. Likewise for pd.DataFrame. Other options might be a list of lists, or a dictionary of the form {(row, col): Entry instance}. Thanks in advance for your replies! ALBERT-JAN From robertvstepp at gmail.com Sat Sep 24 01:55:28 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 24 Sep 2016 00:55:28 -0500 Subject: [Tutor] Questions as to how to run the same unit test multiple times on varying input data. Message-ID: After another long layoff from studying Python due to "that's life", I am back at it again. I am trying to combine learning unit testing and TDD with working through the text, "Think Python 2" by Allen Downey. I am currently in chapter 3 which is an introduction to functions. In trying to apply TDD to exercise 3.1, which is: "Write a function named right_justify that takes a string named s as a parameter and prints the string with enough leading spaces so that the last letter of the string is in column 70 of the display. Hint: Use concatenation and repetition..." The problem is pretty easy, but the point is for me is to practice the steps of TDD and learn how to use unit testing, and I figure using easy problems as I go through the book will make learning TDD and testing easier. After a couple of iterations I have this program, right_justify.py: #!/usr/bin/env python3 '''Exerise 3.1 from "Think Python 2" by Allen Downey. This module will take a string and right justify it so that the last character of the line will fall in column 70 of the display. This is accomplished by padding the line with spaces.''' def right_justify(a_string): '''This fucntion will take the string, "a_string", and left justify it by padding it with spaces until its last character falls in column 70 of the display. The padded string will be returned.''' return ' ' * 58 + a_string Of course, this code passed its test when the string was "Monty Python", so I next wanted to run the same test with different strings each time, so that my code would fail in these more general cases. However, I struggled to find a way to repeat the same test with varying data, eventually crying "Uncle!" and consulted the documentation. This led me to this version of test_right_justify.py: #!/usr/bin/env python3 '''This module contains unit tests for the function, "right_justify(a_string)", located in the module, "right_justify".''' import unittest import right_justify class TestRightJustify(unittest.TestCase): '''Tests for the function, "right_justify(a_string)", in the "right_justify module".''' def setUp(self): '''Define variables needed to run this class' tests.''' self.test_strings = [ "Monty Python", "She's a witch!", "Beware of the rabbit!!! She is a vicious beast who will rip" " your throat out!"] def test_returned_len_is_70(self): '''Check that the string returned by "right_justify(a_string)" is the length of the entire line, i.e., 70 columns.''' for test_string in self.test_strings: with self.subTest(test_string = test_string): length_of_returned_string = ( len(right_justify.right_justify(test_string))) print('The current test string is: ', test_string) self.assertEqual(length_of_returned_string, 70) if __name__ == '__main__': unittest.main() I know that I need more tests (or a better single one) to fully test this function. But I am taking baby steps here, checking the most obvious stuff first. Refinement will come as I learn and move along. Anyway, running the tests gave me this result: c:\thinkpython2\ch3\ex_3-1>py -m unittest The current test string is: Monty Python The current test string is: She's a witch! The current test string is: Beware of the rabbit!!! She is a vicious beast who will rip your throat out! ====================================================================== FAIL: test_returned_len_is_70 (test_right_justify.TestRightJustify) (test_string="She's a witch!") Check that the string returned by "right_justify(a_string)" is the ---------------------------------------------------------------------- Traceback (most recent call last): File "c:\thinkpython2\ch3\ex_3-1\test_right_justify.py", line 32, in test_returned_len_is_70 self.assertEqual(length_of_returned_string, 70) AssertionError: 72 != 70 ====================================================================== FAIL: test_returned_len_is_70 (test_right_justify.TestRightJustify) (test_string='Beware of the rabbit!!! She is a vicious beast who will rip your throat out!') Check that the string returned by "right_justify(a_string)" is the ---------------------------------------------------------------------- Traceback (most recent call last): File "c:\thinkpython2\ch3\ex_3-1\test_right_justify.py", line 32, in test_returned_len_is_70 self.assertEqual(length_of_returned_string, 70) AssertionError: 135 != 70 ---------------------------------------------------------------------- Ran 1 test in 0.009s FAILED (failures=2) Question #1: Why is the test output NOT showing the first, passed test? The one where the string is "Monty Python"? Question #2: Before I hit the docs to learn about subTest, I tried a bunch of different ways to make that for loop to work inside the class, but I guess my current poor knowledge of OOP did me in. But I KNOW there has to be a way to accomplish this WITHOUT using subTest. After all, subTest was not introduced until Python 3.4. So how should I go about this without using subTest? Question #3: In "with self.subTest(test_string = test_string):" why is "test_string = test_string" necessary? Is it that using "with" sets up a new local namespace within this context manager? I think with these long Python layoffs I am losing knowledge faster than I am gaining it! Cheers! -- boB From robertvstepp at gmail.com Sat Sep 24 02:30:39 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 24 Sep 2016 01:30:39 -0500 Subject: [Tutor] Questions as to how to run the same unit test multiple times on varying input data. In-Reply-To: References: Message-ID: I wanted to add a clarification as to what I was hoping to achieve. On Sat, Sep 24, 2016 at 12:55 AM, boB Stepp wrote: > > Of course, this code passed its test when the string was "Monty > Python", so I next wanted to run the same test with different strings > each time, so that my code would fail in these more general cases. > However, I struggled to find a way to repeat the same test with > varying data, eventually crying "Uncle!" and consulted the > documentation. What I struggled with was getting tests to run for ALL of the data. Until I found subTest in the docs, the tests would stop with the FIRST failure, whereas I wanted to see ALL failures and passes. That is, I wanted to exercise all of the data inputs and see the collected results. boB From ben+python at benfinney.id.au Sat Sep 24 03:04:37 2016 From: ben+python at benfinney.id.au (Ben Finney) Date: Sat, 24 Sep 2016 17:04:37 +1000 Subject: [Tutor] Questions as to how to run the same unit test multiple times on varying input data. References: Message-ID: <85r389n5h6.fsf@benfinney.id.au> Bob, you may want to also subscribe to the specific forum for testing in Python . You're beyond the beginner material that's usually discussed on this Tutor forum. boB Stepp writes: > What I struggled with was getting tests to run for ALL of the data. > Until I found subTest in the docs, the tests would stop with the FIRST > failure, whereas I wanted to see ALL failures and passes. That is, I > wanted to exercise all of the data inputs and see the collected > results. The ?testscenarios? third-party package is designed for this . You define ?scenarios? of data to apply to each of the test case functions in a class; the ?testscenarios? magic is then to multiply those functions, at run time, by each of the scenarios. A separate test case will be generated for each combination, with its own distinct result reported in the test run. To discuss more, join us over at the testing-in-python forum . -- \ ?Courage is resistance to fear, mastery of fear ? not absence | `\ of fear.? ?Mark Twain, _Pudd'n'head Wilson_ | _o__) | Ben Finney From phil_lor at bigpond.com Sat Sep 24 05:21:54 2016 From: phil_lor at bigpond.com (Phil) Date: Sat, 24 Sep 2016 19:21:54 +1000 Subject: [Tutor] Basic telnet question Message-ID: <21fa7ac2-4d54-f829-2ad8-d9719440f0da@bigpond.com> Thank you for reading this. If I enter the following at the prompt: $ telnet localhost 7356 The client then responds with: Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. I'd like to do the same from Python. My code is as follows: import telnetlib tn = telnetlib.Telnet("127.0.0.1", 7356) print(tn.read_all()) Then, once a connection is established send the character "f" tn.write("f" + "\n") print(tn.read_all()) The problem is that the client is not responding, certainly not as expected. There aren't any Python errors either, however, the console is blocked until the client is disabled. If I then attempt a connection with the disabled client a Python connection refused error is displayed, as I would expect. I have read the telnetlib document and searched for examples but I seem to be stuck on a very basic problem. By the way, I'm using Python 3.5 under Linux. -- Regards, Phil From wprins at gmail.com Sat Sep 24 09:34:12 2016 From: wprins at gmail.com (Walter Prins) Date: Sat, 24 Sep 2016 14:34:12 +0100 Subject: [Tutor] Questions as to how to run the same unit test multiple times on varying input data. In-Reply-To: References: Message-ID: Hi, On 24 September 2016 at 06:55, boB Stepp wrote: > > def test_returned_len_is_70(self): > '''Check that the string returned by "right_justify(a_string)" is the > length of the entire line, i.e., 70 columns.''' > > for test_string in self.test_strings: > with self.subTest(test_string = test_string): > length_of_returned_string = ( > len(right_justify.right_justify(test_string))) > print('The current test string is: ', test_string) > self.assertEqual(length_of_returned_string, 70) > To add to what Benn Finney's said: The key thing to understand is that to the test framework, this is *one* test due to there being one def test_....() method. The first iteration of the loop (which is a "pass" in your mind that should be displayed) is just a part of this one test that hasn't caused a failure. To accomplish what you want in a generalized way you have to therefore specify and make the framework aware of a) the existence of a generalized test method that takes some kind of test data to run, and b) what the test data is. The framework should then repeatedly run your test, varying the input as specified and showing the failure cases as independently run tests. And the way to do this when using the unittest framework is as Benn's described. I'd however suggest at this stage to not get stuck in the minutiae of how to write fully generalized tests, and to simply write separate test cases (seperate def's) with different data, to keep your focus on learning TDD rather than learning the ins and outs of unittest, if you find yourself falling down some kind of test framework learning rabbit hole. That said, I would suggest perhaps consider using Py.Test instead of unittest, which is arguably much "lower friction" with less overhead/boilerplate/ceremony required. With Pytest, tests can be defined via simple functions if you wish, and checks may be performed with simple asserts if you like. PyTest will also collect all test functions automatically wherever they may exist, based on its conventions. It is therefore possible to start really simply with any code you write, and have test functions defined alongside the code you're writing without any explicit reference/dependency on the test framework, which allows a very easy way of getting started. Obviously you can keep the tests in a separate module if you prefer, and/or make use of the infrastructure provided by the test framework (base classes, decorators etc) if you like or need them, but this is not obligatory and hence makes a very direct style of getting started possible (hence "low friction") The kind of parameterization you want can be more compactly and simply achieved with PyTest attributes, as follows below. I've rewritten your test module slightly and include a non-implemented right_justify() method, and thus you have 3 failing tests if you run the below module: ----------------------------begin code----------------------------- #!/usr/bin/env python3 '''This module defines the function right_justify(a_string) and defines its unit tests. When the module is run by itself it will self-test. The module should otherwise be imported as normal to use.''' def right_justify(s): #To be completed pass import pytest # Here we use an attribute to define the various inputs and outputs with which # to run our generalized test function test_returned_len_is_correct(). # As you can see, you define the paremeter names, followed by a list of # tuples defining the input and expected output. @pytest.mark.parametrize("test_string, expected_len", [ ("Monty Python", 12), ("She's a witch!", 14), ("Beware of the rabbit!!! She is a vicious beast who will rip"\ " your throat out!", 77), ] ) # Here's the generalized test function, that takes an input and expected length. def test_returned_len_is_correct(test_string, expected_len): '''Check that the string returned by "right_justify(a_string)" is the length of the input''' actual_len = len(right_justify(test_string)) assert actual_len == expected_len if __name__ == '__main__': # This is how to invoke pytest directly from this module as the main function. # Note this is an uncommon pytest usage pattern. More common is to run it # from command line in your source folder, and have pytest collect all the # tests from all modules automatically. See http://doc.pytest.org/en/latest/usage.html pytest.main([__file__]) ----------------------------end code----------------------------- Hope that gives you some ideas. An aside, you might also consider some of the coding challenge sites which are also a fun way to practice your TDD skills. For example: https://www.hackerrank.com/domains/python/py-introduction https://www.codeeval.com https://www.codewars.com Best wishes, Walter From david at graniteweb.com Sat Sep 24 11:01:18 2016 From: david at graniteweb.com (David Rock) Date: Sat, 24 Sep 2016 10:01:18 -0500 Subject: [Tutor] Basic telnet question In-Reply-To: <21fa7ac2-4d54-f829-2ad8-d9719440f0da@bigpond.com> References: <21fa7ac2-4d54-f829-2ad8-d9719440f0da@bigpond.com> Message-ID: <29B71E5D-143F-4C8E-A4BA-EB63D918DFA7@graniteweb.com> > On Sep 24, 2016, at 04:21, Phil wrote: > > The problem is that the client is not responding, certainly not as expected. There aren't any Python errors either, however, the console is blocked until the client is disabled. If I then attempt a connection with the disabled client a Python connection refused error is displayed, as I would expect. > > I have read the telnetlib document and searched for examples but I seem to be stuck on a very basic problem. > > By the way, I'm using Python 3.5 under Linux. when you say "the client is not responding, certainly not as expected?, what, exactly, is the output you get? read_all is a bit touchy, and it?s a blocking operation. the blocking part is likely why the console is blocked. read_all doesn?t know it should give things back to you just because you don?t see any new data; it?s still trying to read everything until it times out. either add a short timeout value to your telnetlib.Telnet(), or try a different read method; for example, read_very_eager you could also try using telnetlib.set_debuglevel() to try and get more details about what?s actually happening. ? David Rock david at graniteweb.com From robertvstepp at gmail.com Sat Sep 24 11:17:04 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 24 Sep 2016 10:17:04 -0500 Subject: [Tutor] Questions as to how to run the same unit test multiple times on varying input data. In-Reply-To: <85r389n5h6.fsf@benfinney.id.au> References: <85r389n5h6.fsf@benfinney.id.au> Message-ID: On Sat, Sep 24, 2016 at 2:04 AM, Ben Finney wrote: > Bob, you may want to also subscribe to the specific forum for testing in > Python . You're > beyond the beginner material that's usually discussed on this Tutor forum. Honestly, Ben, I thought that this would be a "basic" set of issues and I was just missing something obvious in unittest. As I just mentioned to Walter off-list, it just seemed "common sense" to exercise a given test with varying data and to not repeat myself unnecessarily (DRY) in writing the tests. Anyway I have been a subscriber for TIP for some time (Per an earlier suggestion a while back, perhaps even yours? ~(:>)) and will continue this there. Thanks, Ben! boB From phil_lor at bigpond.com Sat Sep 24 16:45:14 2016 From: phil_lor at bigpond.com (Phil) Date: Sun, 25 Sep 2016 06:45:14 +1000 Subject: [Tutor] Basic telnet question In-Reply-To: References: <21fa7ac2-4d54-f829-2ad8-d9719440f0da@bigpond.com> Message-ID: <6f408c55-58ee-bd6d-292b-2005525cf6f6@bigpond.com> On 24/09/16 21:03, Joaquin Alzola wrote: > >> $ telnet localhost 7356 > >> The client then responds with: > >> Trying 127.0.0.1... >> Connected to localhost. >> Escape character is '^]'. > > Why not use the socket module? I did try the socket module, Joaquin but it didn't seem to be leading anywhere plus the telnetlib module seemed to be a more logical choice. Is there some advantage to using the socket module? -- Regards, Phil From phil_lor at bigpond.com Sat Sep 24 16:49:51 2016 From: phil_lor at bigpond.com (Phil) Date: Sun, 25 Sep 2016 06:49:51 +1000 Subject: [Tutor] Basic telnet question In-Reply-To: <29B71E5D-143F-4C8E-A4BA-EB63D918DFA7@graniteweb.com> References: <21fa7ac2-4d54-f829-2ad8-d9719440f0da@bigpond.com> <29B71E5D-143F-4C8E-A4BA-EB63D918DFA7@graniteweb.com> Message-ID: <93d38194-d1fa-de79-aee7-c4589c3c7b6a@bigpond.com> On 25/09/16 01:01, David Rock wrote: > >> On Sep 24, 2016, at 04:21, Phil wrote: >> >> The problem is that the client is not responding, certainly not as expected. There aren't any Python errors either, however, the console is blocked until the client is disabled. If I then attempt a connection with the disabled client a Python connection refused error is displayed, as I would expect. >> >> I have read the telnetlib document and searched for examples but I seem to be stuck on a very basic problem. >> >> By the way, I'm using Python 3.5 under Linux. > > when you say "the client is not responding, certainly not as expected?, what, exactly, is the output you get? > In my dazed state I think I responded to David personally instead of the list, my apologies. Thank you for your reply David. This is what I expect: Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. But, as I say, the console is blocked and nothing is returned. I'll try the debug method that you suggested. > read_all is a bit touchy, and it?s a blocking operation. the blocking part is likely why the console is blocked. read_all doesn?t know it should give things back to you just because you don?t see any new data; it?s still trying to read everything until it times out. > > either add a short timeout value to your telnetlib.Telnet(), or try a different read method; for example, read_very_eager > > you could also try using telnetlib.set_debuglevel() to try and get more details about what?s actually happening. > -- Regards, Phil From david at graniteweb.com Sat Sep 24 17:17:07 2016 From: david at graniteweb.com (David Rock) Date: Sat, 24 Sep 2016 16:17:07 -0500 Subject: [Tutor] Basic telnet question In-Reply-To: <93d38194-d1fa-de79-aee7-c4589c3c7b6a@bigpond.com> References: <21fa7ac2-4d54-f829-2ad8-d9719440f0da@bigpond.com> <29B71E5D-143F-4C8E-A4BA-EB63D918DFA7@graniteweb.com> <93d38194-d1fa-de79-aee7-c4589c3c7b6a@bigpond.com> Message-ID: <6F1FDA9C-2086-41DB-B93B-1681E0A22EC5@graniteweb.com> > On Sep 24, 2016, at 15:49, Phil wrote: > > On 25/09/16 01:01, David Rock wrote: >> >> when you say "the client is not responding, certainly not as expected?, what, exactly, is the output you get? >> > > In my dazed state I think I responded to David personally instead of the list, my apologies. :-) > Thank you for your reply David. > > This is what I expect: > > Trying 127.0.0.1... > Connected to localhost. > Escape character is '^]'. > > But, as I say, the console is blocked and nothing is returned. > > I'll try the debug method that you suggested. So you are expecting to read until the header stanza is complete, then send a command and presumably get the results of the command. The problem is read_all() doesn?t work the way you think it does. See what happens with your current code if you send ctrl+], it will probably print as expected, but also close the connection. I don?t think read_all is the right function for the task you are trying to perform. You are putting it inside a print statement print(tn.read_all()) but that?s never going to print anything or return control because read_all will never finish (and never return to the print call). read_all() is a blocking operation that?s going to sit there and keep trying to read data until it?s told to stop (EOF), or until the connection times out. What you probably mean to do is use read_until(). You know what you are expecting, so try to catch it with something like: header = tn.read_until("character is '^]?.?, timeout=5) print(header) tn.write("f" + "\n?) etc read_until looks for a specific string and returns when it finds it, giving control back to you. ? David Rock david at graniteweb.com From phil_lor at bigpond.com Sat Sep 24 19:59:07 2016 From: phil_lor at bigpond.com (Phil) Date: Sun, 25 Sep 2016 09:59:07 +1000 Subject: [Tutor] Basic telnet question solved In-Reply-To: <6F1FDA9C-2086-41DB-B93B-1681E0A22EC5@graniteweb.com> References: <21fa7ac2-4d54-f829-2ad8-d9719440f0da@bigpond.com> <29B71E5D-143F-4C8E-A4BA-EB63D918DFA7@graniteweb.com> <93d38194-d1fa-de79-aee7-c4589c3c7b6a@bigpond.com> <6F1FDA9C-2086-41DB-B93B-1681E0A22EC5@graniteweb.com> Message-ID: On 25/09/16 07:17, David Rock wrote: > header = tn.read_until("character is '^]?.?, timeout=5) > print(header) Thank you David, read_until() led me to a result. It seems that the telnetlib doesn't emulate the console telnet command exactly, so I didn't get the connection response that I had expected. However, if I send a command to the client and use read_until() then I do get the result from the command that I expected. Thanks again, I was on the verge of giving this idea away. -- Regards, Phil From steve at pearwood.info Sun Sep 25 00:05:25 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 25 Sep 2016 14:05:25 +1000 Subject: [Tutor] Questions as to how to run the same unit test multiple times on varying input data. In-Reply-To: References: Message-ID: <20160925040523.GG22471@ando.pearwood.info> On Sat, Sep 24, 2016 at 12:55:28AM -0500, boB Stepp wrote: > "Write a function named right_justify that takes a string named s as a > parameter and prints the string with enough leading spaces so that the > last letter of the string is in column 70 of the display. Hint: Use > concatenation and repetition..." Or the built-in str.rjust() method... :-) > The problem is pretty easy, but the point is for me is to practice the > steps of TDD and learn how to use unit testing, and I figure using > easy problems as I go through the book will make learning TDD and > testing easier. After a couple of iterations I have this program, > right_justify.py: > > #!/usr/bin/env python3 > > '''Exerise 3.1 from "Think Python 2" by Allen Downey. > > This module will take a string and right justify it so that the last character > of the line will fall in column 70 of the display. This is accomplished by > padding the line with spaces.''' > > def right_justify(a_string): > '''This fucntion will take the string, "a_string", and left justify it by Left justify? > padding it with spaces until its last character falls in column 70 of the > display. The padded string will be returned.''' > > return ' ' * 58 + a_string > > Of course, this code passed its test when the string was "Monty > Python", so I next wanted to run the same test with different strings > each time, so that my code would fail in these more general cases. > However, I struggled to find a way to repeat the same test with > varying data, There are two simple ways to handle testing with different test cases: - write multiple test cases, one of each test; - write a multiple tests within a single test case. Of course these are not mutually exclusive. You can do both. For example: # Untested. from my_module import right_justify import random class RightJustifyTest(unittest.TestCase): def test_right_justify(self): self.assertEqual( right_justify('Monty Python'), ' Monty Python' ) self.assertEqual( right_justify('NOBODY EXPECTS THE SPANISH INQUISITION!!!'), ' NOBODY EXPECTS THE SPANISH INQUISITION!!!' ) def test_empty_string(self): self.assertEqual(right_justify(''), ' '*70) def test_single_char(self): for c in 'abcdefg': with self.subTest(c=c): self.assertEqual(right_justify(c), ' '*69+c) def test_issue12345(self): # Regression test for issue #12345: right_justify() of a # single space returns the empty string. input = ' ' assert len(input) == 1 and ord(input) == 32 expected = ' '*70 self.assertEqual(right_justify(input), expected) def test_random_string(self): input = [] for i in range(random.randint(2, 69): input.append(random.choice('abcdefghijklmnopqrstuvwxyz')) input = ''.join(input) if random.random() > 0.5: input = input.upper() expected = ' '*(70-len(input)) + input assert len(expected) == 70 self.assertEqual(right_justify(input), expected) > class TestRightJustify(unittest.TestCase): [...] > self.test_strings = [ > "Monty Python", > "She's a witch!", > "Beware of the rabbit!!! She is a vicious beast who will rip" > " your throat out!"] I'm not sure that you should name your data "test something". unittest takes methods starting with "test" as test cases. I'm reasonably sure that it can cope with non-methods starting with "test", and won't get confused, but I'm not sure that it won't confuse *me*. So I would prefer to use a different name. > def test_returned_len_is_70(self): > '''Check that the string returned by "right_justify(a_string)" is the > length of the entire line, i.e., 70 columns.''' > > for test_string in self.test_strings: > with self.subTest(test_string = test_string): > length_of_returned_string = ( > len(right_justify.right_justify(test_string))) > print('The current test string is: ', test_string) > self.assertEqual(length_of_returned_string, 70) It is probably not a good idea to use print in the middle of unit testing. The unittest module prints a neat progress display, and it would be rude to muck that up with a print. If you really need to monitor the internal state of your test cases, write to a log file and watch that. Of course, you can *temporarily* insert a print into the test suite just to see what's going on. Test code is code, which means it too can be buggy, and sometimes you need to *debug your tests*. And we know that a simple but effective way to debug code is using temporary calls to print. In this specific case, you probably don't really case about the test cases that pass. You only want to see *failed* tests. That's what the subTest() call does: if the test fails, it will print out the value of "test_string" so you can see which input strings failed. No need for print. > Anyway, running the tests gave me this result: > > c:\thinkpython2\ch3\ex_3-1>py -m unittest > The current test string is: Monty Python > The current test string is: She's a witch! > The current test string is: Beware of the rabbit!!! She is a vicious > beast who will rip your throat out! /me looks carefully at that output... I'm not seeing the unittest progress display, which I would expect to contain at least one F (for Failed test). Normally unittest prints out a dot . for each passed test, F for failured tests, E for errors, and S for tests which are skipped. I do not see them. > ====================================================================== > FAIL: test_returned_len_is_70 (test_right_justify.TestRightJustify) > (test_string="She's a witch!") > Check that the string returned by "right_justify(a_string)" is the > ---------------------------------------------------------------------- > Traceback (most recent call last): > File "c:\thinkpython2\ch3\ex_3-1\test_right_justify.py", line 32, in > test_returned_len_is_70 > self.assertEqual(length_of_returned_string, 70) > AssertionError: 72 != 70 Notice that unittest prints the name of the test, the value or values you pass to subTest, a description of the test (taken from the doc string of the method -- you should try to include a 1-line summary) and the traceback from the failed test. And the traceback includes the value which fails. > Ran 1 test in 0.009s > > FAILED (failures=2) > > Question #1: Why is the test output NOT showing the first, passed > test? The one where the string is "Monty Python"? By default, unittest shows a progress bar of dots like this: .....F....E....FF.....S........F.......F.... That shows 37 passing tests, 1 error (the test itself is buggy), 1 skipped test, and five failed tests. If everything passes, you just see the series of dots. To see passing tests listed as well as failures, you need to pass the verbose flag to unittest: python -m unittest --verbose > Question #2: Before I hit the docs to learn about subTest, I tried a > bunch of different ways to make that for loop to work inside the > class, but I guess my current poor knowledge of OOP did me in. But I > KNOW there has to be a way to accomplish this WITHOUT using subTest. > After all, subTest was not introduced until Python 3.4. So how should > I go about this without using subTest? *shrug* Just leave the subTest out. The difference is: - without subtest, the first failed test will exit the test case (that is, the test_* method); - the failed test may not print out sufficient information to identify which data failed. Only if all the data passes will the entire loop run. Before subTest, I used to write a lot of tests like: def test_Spam(self): for x in bunch_of_stuff: self.assertEqual(func(x), expected) which was great so long as they all passed. But as soon as one failed, it was twenty kinds of trouble to work out *which* one failed. So then, and only then, did I break it up into separate test cases: def test_Spam_foo(self): self.assertEqual(func(foo), expected) def test_Spam_bar(self): self.assertEqual(func(bar), expected) def test_Spam_baz(self): self.assertEqual(func(baz), expected) subTest is a MUCH BETTER WAY to solve this problem!!!! /me dances the Dance Of Joy > Question #3: In "with self.subTest(test_string = test_string):" why > is "test_string = test_string" necessary? Is it that using "with" > sets up a new local namespace within this context manager? You need to tell subTest which variables you care about, and what their value is. Your test case might have any number of local or global variables. It might care about attributes of the test suite. Who knows what information you want to see if a test fails? So rather than using some sort of dirty hack to try to guess which variables are important, subTest requires you to tell it what you want to know. Which could be anything you like: with subTest(now=time.time(), spam=spam, seed=random.seed()): ... you're not limited to just your own local variables. subTest doesn't care what they are or what you call them -- it just collects them as keyword arguments, then prints them at the end if the test fails. -- Steve From robertvstepp at gmail.com Sun Sep 25 01:38:07 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 25 Sep 2016 00:38:07 -0500 Subject: [Tutor] Questions as to how to run the same unit test multiple times on varying input data. In-Reply-To: <20160925040523.GG22471@ando.pearwood.info> References: <20160925040523.GG22471@ando.pearwood.info> Message-ID: On Sat, Sep 24, 2016 at 11:05 PM, Steven D'Aprano wrote: > On Sat, Sep 24, 2016 at 12:55:28AM -0500, boB Stepp wrote: >> def right_justify(a_string): >> '''This fucntion will take the string, "a_string", and left justify it by > > Left justify? Oops! Typo. [snip] > def test_random_string(self): > input = [] > for i in range(random.randint(2, 69): > input.append(random.choice('abcdefghijklmnopqrstuvwxyz')) > input = ''.join(input) > if random.random() > 0.5: > input = input.upper() > expected = ' '*(70-len(input)) + input > assert len(expected) == 70 > self.assertEqual(right_justify(input), expected) Hmm. I like this one. >> class TestRightJustify(unittest.TestCase): > [...] >> self.test_strings = [ >> "Monty Python", >> "She's a witch!", >> "Beware of the rabbit!!! She is a vicious beast who will rip" >> " your throat out!"] > > I'm not sure that you should name your data "test something". unittest > takes methods starting with "test" as test cases. I'm reasonably sure > that it can cope with non-methods starting with "test", and won't get > confused, but I'm not sure that it won't confuse *me*. So I would prefer > to use a different name. It didn't get confused, but I take your point. > >> def test_returned_len_is_70(self): >> '''Check that the string returned by "right_justify(a_string)" is the >> length of the entire line, i.e., 70 columns.''' >> >> for test_string in self.test_strings: >> with self.subTest(test_string = test_string): >> length_of_returned_string = ( >> len(right_justify.right_justify(test_string))) >> print('The current test string is: ', test_string) >> self.assertEqual(length_of_returned_string, 70) > > > It is probably not a good idea to use print in the middle of > unit testing. The unittest module prints a neat progress display, and it > would be rude to muck that up with a print. If you really need to > monitor the internal state of your test cases, write to a log file and > watch that. > > Of course, you can *temporarily* insert a print into the test suite just > to see what's going on. Test code is code, which means it too can be > buggy, and sometimes you need to *debug your tests*. And we know that a > simple but effective way to debug code is using temporary calls to > print. The print function is an artifact of multiple efforts to write something that would do what I wanted, that is, an individual test result for each string input into the test method. It was strictly for debugging those (failed) efforts. > In this specific case, you probably don't really case about the test > cases that pass. You only want to see *failed* tests. That's what the > subTest() call does: if the test fails, it will print out the value of > "test_string" so you can see which input strings failed. No need for > print. Actually, the persnickety part of me wanted to see the little dot for the first test which did pass and I was puzzled why subTest() does not result in that happening. >> Anyway, running the tests gave me this result: >> >> c:\thinkpython2\ch3\ex_3-1>py -m unittest >> The current test string is: Monty Python >> The current test string is: She's a witch! >> The current test string is: Beware of the rabbit!!! She is a vicious >> beast who will rip your throat out! > > > /me looks carefully at that output... > > I'm not seeing the unittest progress display, which I would expect to > contain at least one F (for Failed test). Normally unittest prints out a > dot . for each passed test, F for failured tests, E for errors, and S > for tests which are skipped. I do not see them. Nor did I and I was and still am puzzled by this. But the docs for subTest() at https://docs.python.org/3/library/unittest.html#distinguishing-test-iterations-using-subtests give an example which also does not show the dots or F's. So I suppose this is the intended behavior. >> Question #1: Why is the test output NOT showing the first, passed >> test? The one where the string is "Monty Python"? > > By default, unittest shows a progress bar of dots like this: > > .....F....E....FF.....S........F.......F.... > > That shows 37 passing tests, 1 error (the test itself is buggy), 1 > skipped test, and five failed tests. If everything passes, you just see > the series of dots. > > To see passing tests listed as well as failures, you need to pass the > verbose flag to unittest: > > python -m unittest --verbose No, that does not seem to work when using subTest(). I had already tried this. I get: c:\thinkpython2\ch3\ex_3-1>py -m unittest --verbose test_returned_len_is_70 (test_right_justify.TestRightJustify) Check that the string returned by "right_justify(a_string)" is the ... The current test string is: Monty Python The current test string is: She's a witch! The current test string is: Beware of the rabbit!!! She is a vicious beast who will rip your throat out! ====================================================================== FAIL: test_returned_len_is_70 (test_right_justify.TestRightJustify) (test_string="She's a witch!") Check that the string returned by "right_justify(a_string)" is the ---------------------------------------------------------------------- Traceback (most recent call last): File "c:\thinkpython2\ch3\ex_3-1\test_right_justify.py", line 32, in test_returned_len_is_70 self.assertEqual(length_of_returned_string, 70) AssertionError: 72 != 70 ====================================================================== FAIL: test_returned_len_is_70 (test_right_justify.TestRightJustify) (test_string='Beware of the rabbit!!! She is a vicious beast who will rip your throat out!') Check that the string returned by "right_justify(a_string)" is the ---------------------------------------------------------------------- Traceback (most recent call last): File "c:\thinkpython2\ch3\ex_3-1\test_right_justify.py", line 32, in test_returned_len_is_70 self.assertEqual(length_of_returned_string, 70) AssertionError: 135 != 70 ---------------------------------------------------------------------- Ran 1 test in 0.013s FAILED (failures=2) >> Question #3: In "with self.subTest(test_string = test_string):" why >> is "test_string = test_string" necessary? Is it that using "with" >> sets up a new local namespace within this context manager? > > You need to tell subTest which variables you care about, and what their > value is. > > Your test case might have any number of local or global variables. It > might care about attributes of the test suite. Who knows what > information you want to see if a test fails? So rather than using some > sort of dirty hack to try to guess which variables are important, > subTest requires you to tell it what you want to know. > > Which could be anything you like: > > with subTest(now=time.time(), spam=spam, seed=random.seed()): > ... > > > you're not limited to just your own local variables. subTest doesn't > care what they are or what you call them -- it just collects them as > keyword arguments, then prints them at the end if the test fails. Thanks for this info. It was not clear to me from the documentation. subTest() does allow ALL failures to show up. It is a pity it does not allow the passed tests to be listed as well. But, as you point out, for evaluating test results, it is the failures that are critical. I will have to decide if it is worth my time to investigate testscenarios as Ben suggests, or perhaps go with PyTest. I spent a lot of time looking for examples using testscenarios this evening and did not find much, but I think I finally understand its basic usage.. I may just put off my persnickety nature till later and plow on with learning TDD and testing more generally while working on extending my Python and computer science knowledge. -- boB From jonathancurtis at aggiemail.usu.edu Fri Sep 23 14:19:04 2016 From: jonathancurtis at aggiemail.usu.edu (Jonathan Curtis) Date: Fri, 23 Sep 2016 12:19:04 -0600 Subject: [Tutor] Problems using .lower() Message-ID: Hi, I am trying to complete an assignment for school and having problems getting it to work. I have lists named l_one and and l_two they both have a combination of integers and strings. I am supposed to combined the lists into a l_three and have no duplicate numbers and strings. Both lists have the number 2 and l_one has cat while l_two has Cat. I am having a problem being able to get Cat to cat so that it recognizes the duplicate. Pleas advise me in this problem. Let me know how you would do it so that I can compare it to what I have been trying to do. Thanks for your time and help. Sincerely, Jonathan Curtis From mr.eightnoteight at gmail.com Sat Sep 24 03:39:46 2016 From: mr.eightnoteight at gmail.com (srinivas devaki) Date: Sat, 24 Sep 2016 13:09:46 +0530 Subject: [Tutor] event loop vs threads In-Reply-To: References: Message-ID: how does Python switch execution and maintain context i.e function stack etc,.. for co-routines and why is it less costly than switching threads which almost do the same, and both are handled by Python Interpreter itself(event loop for co-routines and GIL scheduling for threading), so where does the extra overhead for threads come from ? Regards Srinivas Devaki Senior (final yr) student at Indian Institute of Technology (ISM), Dhanbad Computer Science and Engineering Department ph: +91 9491 383 249 telegram_id: @eightnoteight From Joaquin.Alzola at lebara.com Sat Sep 24 07:03:19 2016 From: Joaquin.Alzola at lebara.com (Joaquin Alzola) Date: Sat, 24 Sep 2016 11:03:19 +0000 Subject: [Tutor] Basic telnet question In-Reply-To: <21fa7ac2-4d54-f829-2ad8-d9719440f0da@bigpond.com> References: <21fa7ac2-4d54-f829-2ad8-d9719440f0da@bigpond.com> Message-ID: >$ telnet localhost 7356 >The client then responds with: >Trying 127.0.0.1... >Connected to localhost. >Escape character is '^]'. Why not use the socket module? -- Joaquin This email is confidential and may be subject to privilege. If you are not the intended recipient, please do not copy or disclose its content but contact the sender immediately upon receipt. From skotaru at cisco.com Fri Sep 23 18:12:22 2016 From: skotaru at cisco.com (Srinivas Naga Kotaru (skotaru)) Date: Fri, 23 Sep 2016 22:12:22 +0000 Subject: [Tutor] CSR generation script Message-ID: <30EF54B8-EA07-4E49-8E14-7A109AC4360D@cisco.com> I am using pyOpenSSL module to automate CSR generation. Not much self-explanatory documentation available except few blog posts which cover this topic. Can anyone write a script on this topic? -- Srinivas Kotaru From david at graniteweb.com Sun Sep 25 15:01:47 2016 From: david at graniteweb.com (David Rock) Date: Sun, 25 Sep 2016 14:01:47 -0500 Subject: [Tutor] Basic telnet question solved In-Reply-To: References: <21fa7ac2-4d54-f829-2ad8-d9719440f0da@bigpond.com> <29B71E5D-143F-4C8E-A4BA-EB63D918DFA7@graniteweb.com> <93d38194-d1fa-de79-aee7-c4589c3c7b6a@bigpond.com> <6F1FDA9C-2086-41DB-B93B-1681E0A22EC5@graniteweb.com> Message-ID: > On Sep 24, 2016, at 18:59, Phil wrote: > > On 25/09/16 07:17, David Rock wrote: >> header = tn.read_until("character is '^]?.?, timeout=5) >> print(header) > > Thank you David, read_until() led me to a result. It seems that the telnetlib doesn't emulate the console telnet command exactly, so I didn't get the connection response that I had expected. However, if I send a command to the client and use read_until() then I do get the result from the command that I expected. > > Thanks again, I was on the verge of giving this idea away. Glad it worked for you :-) ? David Rock david at graniteweb.com From steve at pearwood.info Sun Sep 25 19:43:21 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 26 Sep 2016 09:43:21 +1000 Subject: [Tutor] Problems using .lower() In-Reply-To: References: Message-ID: <20160925234321.GH22471@ando.pearwood.info> Hi Jonathan, and welcome! It is difficult to advise you when we don't know what you are doing wrong. You should always show us your code: the *minimum* amount of code that demonstrates the nature of your problem. Otherwise we're just guessing. More below: On Fri, Sep 23, 2016 at 12:19:04PM -0600, Jonathan Curtis wrote: > Hi, > > I am trying to complete an assignment for school and having problems > getting it to work. I have lists named l_one and and l_two they both have > a combination of integers and strings. I am supposed to combined the lists > into a l_three and have no duplicate numbers and strings. Both lists have > the number 2 and l_one has cat while l_two has Cat. I am having a problem > being able to get Cat to cat so that it recognizes the duplicate. Pleas > advise me in this problem. Let me know how you would do it so that I can > compare it to what I have been trying to do. Thanks for your time and help. I'll try to guess that you're doing something like this: # Wrong way, doesn't work. a = 'cat' b = 'Cat' b.lower() # lowercase of b a == b # returns False, they are not equal # Instead, try this: a = 'cat' b = 'Cat' b = b.lower() # set b to the lowercase of b a == b # returns True # Or even better: a = 'cat' b = 'Cat' a.lower() == b.lower() # returns True -- Steve From rus.cahimb at gmail.com Sun Sep 25 15:45:21 2016 From: rus.cahimb at gmail.com (Ramanathan Muthaiah) Date: Sun, 25 Sep 2016 19:45:21 +0000 Subject: [Tutor] CSR generation script In-Reply-To: <30EF54B8-EA07-4E49-8E14-7A109AC4360D@cisco.com> References: <30EF54B8-EA07-4E49-8E14-7A109AC4360D@cisco.com> Message-ID: On Sun, Sep 25, 2016 at 11:47 PM Srinivas Naga Kotaru (skotaru) < skotaru at cisco.com> wrote: > I am using pyOpenSSL module to automate CSR generation. Not much > self-explanatory documentation available except few blog posts which cover > this topic. Can anyone write a script on this topic? > To enhance your chances of technical response to your query, please list: i) what's CSR and how you plan to automate CSR generation ? ii) If enough documentation isn't available, why don't you come up with one ? iii) What attempts have you undertaken (and failed or succeeded) in writing a script on this topic ? /Ram From alan.gauld at yahoo.co.uk Sun Sep 25 20:05:32 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 26 Sep 2016 01:05:32 +0100 Subject: [Tutor] Array of Tkinter objects? In-Reply-To: References: Message-ID: On 22/09/16 15:58, Albert-Jan Roskam wrote: > I have created a grid of Tkinter Entry object The Tix module has a grid object - but its terribly documented and I couldn't get it to work properly. But then I didn't have much motive to try, you might fare better... Alternatively I think the PMW widgets include a grid and it is rather better documented. But I've never used PMW... -- 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 badouglas at gmail.com Mon Sep 26 12:59:04 2016 From: badouglas at gmail.com (bruce) Date: Mon, 26 Sep 2016 12:59:04 -0400 Subject: [Tutor] unicode decode/encode issue Message-ID: Hi. Ive got a "basic" situation that should be simpl. So it must be a user (me) issue! I've got a page from a web fetch. I'm simply trying to go from utf-8 to ascii. I'm not worried about any cruft that might get stripped out as the data is generated from a us site. (It's a college/class dataset). I know this is a unicode issue. I know I need to have a much more robust/ythnic/correct approach. I will later, but for now, just want to resolve this issue, and get it off my plate so to speak. I've looked at stackoverflow, as well as numerous other sites, so I turn to the group for a pointer or two... The unicode that I'm dealing with is 'u\2013' The basic things I've done up to now are: s=content s=ascii_strip(s) s=s.replace('\u2013', '-') s=s.replace(u'\u2013', '-') s=s.replace(u"\u2013", "-") s=re.sub(u"\u2013", "-", s) print repr(s) When I look at the input content, I have : u'English 120 Course Syllabus \u2013 Fall \u2013 2006' So, any pointers on replacing the \u2013 with a simple '-' (dash) (or I could even handle just a ' ' (space) thanks From steve at pearwood.info Mon Sep 26 13:31:42 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 27 Sep 2016 03:31:42 +1000 Subject: [Tutor] unicode decode/encode issue In-Reply-To: References: Message-ID: <20160926173141.GL22471@ando.pearwood.info> On Mon, Sep 26, 2016 at 12:59:04PM -0400, bruce wrote: > When I look at the input content, I have : > > u'English 120 Course Syllabus \u2013 Fall \u2013 2006' > > So, any pointers on replacing the \u2013 with a simple '-' (dash) (or I > could even handle just a ' ' (space) You misinterpret what you see. \u2013 *is* a dash (its an en-dash): py> import unicodedata py> unicodedata.name(u'\u2013') 'EN DASH' Try printing the string, and you will see what it looks like: py> content = u'English 120 Course Syllabus \u2013 Fall \u2013 2006' py> print content English 120 Course Syllabus ? Fall ? 2006 Python strings include a lot of escape codes. Simple byte strings include: \t tab \n newline \r carriage return \0 ASCII null byte etc. plus escape codes for hex codes: \xDD (two digit hex code, between hex 00 and hex FF) That lets you enter any byte between (decimal) 0 and 255. For example: \x20 is the hex code 20 (decimal 32), which is a space: py> '\x20' == ' ' True Unicode strings allow the same escape codes as byte strings, plus special Unicode escape codes: \uDDDD (four digit hex codes, for codes between 0 and 65535) \UDDDDDDDD (eight digit hex codes, for codes between 0 and 1114111) \N{name} (Unicode names) Remember to print the string to see what it looks like with the escape codes shown as actual characters, instead of escape codes. -- Steve From rkoeman at smcdsb.on.ca Mon Sep 26 11:35:32 2016 From: rkoeman at smcdsb.on.ca (Richard Koeman) Date: Mon, 26 Sep 2016 11:35:32 -0400 Subject: [Tutor] python coding problem Message-ID: This is my first time using this so I hope it works. I am trying to find out why this code doesnt work. Its simple. I want to find out which number is bigger. I hope you can help and that I am using this python feature properly. Thanks. The function prints the first two print statements then nothing else happens. def maximum(n1, n2): print "the first number is" ,n1 print "the second number is", n2 if n1 > n2: return print n1 , "is the bigger number" elif n1 < n2: return n2 print n2 elif n1 == n2: print "they are equal" else: print "Sorry, No response found" maximum(1,2) -- This is a staff email account managed by Simcoe Muskoka Catholic District School Board. This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the sender. From dyoo at hashcollision.org Mon Sep 26 13:54:17 2016 From: dyoo at hashcollision.org (Danny Yoo) Date: Mon, 26 Sep 2016 10:54:17 -0700 Subject: [Tutor] python coding problem In-Reply-To: References: Message-ID: Hi Richard, The "return" statement does an early escape out of the currently running function. You have a "return" statement in your program that looks unintentional. In an ideal world, the Python compiler would give a warning about this because it's a common mistake. Unfortunately it looks like Python doesn't do this. Please feel free to ask followup questions to the mailing list. Good luck! From __peter__ at web.de Mon Sep 26 13:54:44 2016 From: __peter__ at web.de (Peter Otten) Date: Mon, 26 Sep 2016 19:54:44 +0200 Subject: [Tutor] unicode decode/encode issue References: Message-ID: bruce wrote: > Hi. > > Ive got a "basic" situation that should be simpl. So it must be a user > (me) issue! > > > I've got a page from a web fetch. I'm simply trying to go from utf-8 to > ascii. I'm not worried about any cruft that might get stripped out as the > data is generated from a us site. (It's a college/class dataset). > > I know this is a unicode issue. I know I need to have a much more > robust/ythnic/correct approach. I will later, but for now, just want to > resolve this issue, and get it off my plate so to speak. > > I've looked at stackoverflow, as well as numerous other sites, so I turn > to the group for a pointer or two... > > The unicode that I'm dealing with is 'u\2013' > > The basic things I've done up to now are: > > s=content > s=ascii_strip(s) > s=s.replace('\u2013', '-') > s=s.replace(u'\u2013', '-') > s=s.replace(u"\u2013", "-") > s=re.sub(u"\u2013", "-", s) > print repr(s) > > When I look at the input content, I have : > > u'English 120 Course Syllabus \u2013 Fall \u2013 2006' > > So, any pointers on replacing the \u2013 with a simple '-' (dash) (or I > could even handle just a ' ' (space) I suppose you want to replace the DASH with HYPHEN-MINUS. For that both > s=s.replace(u'\u2013', '-') > s=s.replace(u"\u2013", "-") should work (the Python interpreter sees no difference between the two). Let's try: >>> s = u'English 120 Course Syllabus \u2013 Fall \u2013 2006' >>> t = s.replace(u"\u2013", "-") >>> s == t False >>> s u'English 120 Course Syllabus \u2013 Fall \u2013 2006' >>> t u'English 120 Course Syllabus - Fall - 2006' So it look like you did not actually try the code you posted. To remove all non-ascii codepoints you can use encode(): >>> s.encode("ascii", "ignore") 'English 120 Course Syllabus Fall 2006' (Note that the result is a byte string) From alan.gauld at yahoo.co.uk Mon Sep 26 14:26:58 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 26 Sep 2016 19:26:58 +0100 Subject: [Tutor] python coding problem In-Reply-To: References: Message-ID: On 26/09/16 16:35, Richard Koeman wrote: > The function prints the first two print statements then nothing else > happens. > > def maximum(n1, n2): > print "the first number is" ,n1 > print "the second number is", n2 We know this works so far, so that's fine. > if n1 > n2: > return But this returns no value (so Python will provide a value of None) and immediately exits the function, no further processing will happen. But in your test you use 1,2 so this bit of code never gets executed anyway, so its not your problem(yet). > print n1 , "is the bigger number" Becauyse you used return this print will never, ever be executed. > elif n1 < n2: Given 1,2 this should be where your code goes. > return n2 And straight away you return 2, so nothing beyond here gets executed. > print n2 ie. This is never executed > elif n1 == n2: > print "they are equal" You print a message but don;t return anything so Python by default returns None. > else: > print "Sorry, No response found" Same here, None gets returned. So your function returns None for any set of values except where the second is largest, in which case it returns the second. > maximum(1,2) So here we should see the first 2 print outputs the returned value 2 but only if you execute this in a Python interactive shell. If you put it in a file and try running it you will not see the 2. Try adding a print before the call to maximum: print(maximum(1,2) That will then print the 2 as well. BTW As a matter of good style it's better not to mix print and returns inside a function. Either it returns a value and the calling function can print the result or it just prints stuff and never returns anything. This is because if you try using a function like yours in, say, a sorting program you will get a lot of printed information that's irrelevant to the sorting program. HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From alan.gauld at yahoo.co.uk Mon Sep 26 14:33:46 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 26 Sep 2016 19:33:46 +0100 Subject: [Tutor] CSR generation script In-Reply-To: <30EF54B8-EA07-4E49-8E14-7A109AC4360D@cisco.com> References: <30EF54B8-EA07-4E49-8E14-7A109AC4360D@cisco.com> Message-ID: On 23/09/16 23:12, Srinivas Naga Kotaru (skotaru) wrote: > I am using pyOpenSSL module to automate CSR generation. Don't assume we know what that means. I know what pyOpenSSL is, but have no idea what CSR means to you. (To me it means Customer Service Representative!) > Not much self-explanatory documentation available > except few blog posts which cover this topic. Which topic in particular? SSL? OpenSSL? pyOpenSSL? CSR? > Can anyone write a script on this topic? Again, which topic? Once we know that the answer is "probably"... But probably more usefully, what can we do to enable you to write such a script? -- 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 ahall at autodist.com Mon Sep 26 14:06:25 2016 From: ahall at autodist.com (Alex Hall) Date: Mon, 26 Sep 2016 14:06:25 -0400 Subject: [Tutor] python coding problem In-Reply-To: References: Message-ID: On Mon, Sep 26, 2016 at 11:35 AM, Richard Koeman wrote: > This is my first time using this so I hope it works. > I am trying to find out why this code doesnt work. > Its simple. I want to find out which number is bigger. > > I hope you can help and that I am using this python feature properly. > Thanks. > The function prints the first two print statements then nothing else > happens. > > def maximum(n1, n2): > print "the first number is" ,n1 > print "the second number is", n2 > if n1 > n2: > return > Using the 'return' keyword will return whatever follows it (nothing, in this case). However, it also stops your function from doing anything else. Think of it as telling the function "stop here, and do nothing else. Just quit now." > print n1 , "is the bigger number" > This line will never execute, because you've already returned. > elif n1 < n2: > return n2 > You now return a number, not nothing as before, but the result for your function is the same. Your print statements must be *before* the return statements. > print n2 > elif n1 == n2: > print "they are equal" > else: > print "Sorry, No response found" > maximum(1,2) > > -- > This is a staff email account managed by Simcoe Muskoka Catholic District > School Board. This email and any files transmitted with it are > confidential and intended solely for the use of the individual or entity to > whom they are addressed. If you have received this email in error please > notify the sender. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Alex Hall Automatic Distributors, IT department ahall at autodist.com From rus.cahimb at gmail.com Mon Sep 26 13:52:50 2016 From: rus.cahimb at gmail.com (Ramanathan Muthaiah) Date: Mon, 26 Sep 2016 17:52:50 +0000 Subject: [Tutor] python coding problem In-Reply-To: References: Message-ID: > > This is my first time using this so I hope it works. > I am trying to find out why this code doesnt work. > Its simple. I want to find out which number is bigger. > Welcome! > > I hope you can help and that I am using this python feature properly. > Thanks. > The function prints the first two print statements then nothing else > happens. > Have you tried swapping the print and return statement in the if-elif-elif-else loop ? > > def maximum(n1, n2): > print "the first number is" ,n1 > print "the second number is", n2 > if n1 > n2: > return > print n1 , "is the bigger number" > elif n1 < n2: > return n2 > print n2 > elif n1 == n2: > print "they are equal" > else: > print "Sorry, No response found" > maximum(1,2) > From ben+python at benfinney.id.au Mon Sep 26 16:19:54 2016 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 27 Sep 2016 06:19:54 +1000 Subject: [Tutor] python coding problem References: Message-ID: <858tuemn11.fsf@benfinney.id.au> Alex Hall writes: > On Mon, Sep 26, 2016 at 11:35 AM, Richard Koeman > wrote: > > > def maximum(n1, n2): > > print "the first number is" ,n1 > > print "the second number is", n2 > > if n1 > n2: > > return > > Using the 'return' keyword will return whatever follows it (nothing, in > this case). Not nothing; the ?return? statement will *always* return an object. ?return? without an argument is a special case. It returns the ?None? object. > However, it also stops your function from doing anything else. > Think of it as telling the function "stop here, and do nothing else. Just > quit now." Rather, the ?return? statement tells the interpreter ?return from where you called this function, bringing with you this specified object?. In this case, it brings the ?None? object. It does indeed cease executing the function, but that's because it continues elsewhere (back at the site where the function was called). -- \ ?No smoothen the lion.? ?lion cage, zoo, Czech Republic | `\ | _o__) | Ben Finney From badouglas at gmail.com Mon Sep 26 17:27:54 2016 From: badouglas at gmail.com (bruce) Date: Mon, 26 Sep 2016 17:27:54 -0400 Subject: [Tutor] unicode decode/encode issue In-Reply-To: References: Message-ID: Hey folks. (peter!) Thanks for the reply. I wound up doing: #s=s.replace('\u2013', '-') #s=s.replace(u'\u2013', '-') #s=s.replace(u"\u2013", "-") #s=re.sub(u"\u2013", "-", s) s=s.encode("ascii", "ignore") s=s.replace(u"\u2013", "-") s=s.replace("–", "-") ##<<< this was actually in the raw content apparently print repr(s) The test no longer has the unicode 'dash' I'll revisit and simplify later. One or two of the above ines should be able to be removed, and still have the unicode issue resolved. Thanks On Mon, Sep 26, 2016 at 1:54 PM, Peter Otten <__peter__ at web.de> wrote: > bruce wrote: > > > Hi. > > > > Ive got a "basic" situation that should be simpl. So it must be a user > > (me) issue! > > > > > > I've got a page from a web fetch. I'm simply trying to go from utf-8 to > > ascii. I'm not worried about any cruft that might get stripped out as the > > data is generated from a us site. (It's a college/class dataset). > > > > I know this is a unicode issue. I know I need to have a much more > > robust/ythnic/correct approach. I will later, but for now, just want to > > resolve this issue, and get it off my plate so to speak. > > > > I've looked at stackoverflow, as well as numerous other sites, so I turn > > to the group for a pointer or two... > > > > The unicode that I'm dealing with is 'u\2013' > > > > The basic things I've done up to now are: > > > > s=content > > s=ascii_strip(s) > > s=s.replace('\u2013', '-') > > s=s.replace(u'\u2013', '-') > > s=s.replace(u"\u2013", "-") > > s=re.sub(u"\u2013", "-", s) > > print repr(s) > > > > When I look at the input content, I have : > > > > u'English 120 Course Syllabus \u2013 Fall \u2013 2006' > > > > So, any pointers on replacing the \u2013 with a simple '-' (dash) (or I > > could even handle just a ' ' (space) > > I suppose you want to replace the DASH with HYPHEN-MINUS. For that both > > > s=s.replace(u'\u2013', '-') > > s=s.replace(u"\u2013", "-") > > should work (the Python interpreter sees no difference between the two). > Let's try: > > >>> s = u'English 120 Course Syllabus \u2013 Fall \u2013 2006' > >>> t = s.replace(u"\u2013", "-") > >>> s == t > False > >>> s > u'English 120 Course Syllabus \u2013 Fall \u2013 2006' > >>> t > u'English 120 Course Syllabus - Fall - 2006' > > So it look like you did not actually try the code you posted. > > To remove all non-ascii codepoints you can use encode(): > > >>> s.encode("ascii", "ignore") > 'English 120 Course Syllabus Fall 2006' > > (Note that the result is a byte string) > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From cebirim at gmail.com Mon Sep 26 16:54:18 2016 From: cebirim at gmail.com (angela ebirim) Date: Mon, 26 Sep 2016 21:54:18 +0100 Subject: [Tutor] TDD in Python Message-ID: Hello everyone, I'm a new member on this maling list and am in the process of learning python. I'm keen to learn about testing my Python code and hoping someone could help me with this query... #test.py class Test: def __init__(self, members): self.members = members def printMembers(self): print('Printing members of Test class...') for member in self.members: print('\t%s ' % member) test = Test(['Gazelle', 'Elephant', 'Lion', 'Fish']) test.printMembers() #test_test.py import unittest from test import Test class TestTestCase(unittest.TestCase): """Tests for `test.py`.""" #preparing to test def setUp(self): """Sample test case""" print "TestTest::setUp_:begin" # self.members = [1, 2, 3, 4, 5] self.members = Test([1, 2, 3, 4]) def test_is_printMembers(self): """Will print a list of contents?""" self.assertTrue(self.members.printMembers) if __name__ == '__main__': unittest.main() Does my test code in test_test.py look correct? Many thanks From mr.eightnoteight at gmail.com Mon Sep 26 18:16:25 2016 From: mr.eightnoteight at gmail.com (srinivas devaki) Date: Tue, 27 Sep 2016 03:46:25 +0530 Subject: [Tutor] Unicode to Ascii In-Reply-To: References: Message-ID: How can I convert Unicode to Ascii by stripping of any non ascii characters. one way is to filter on s like ascii = ''.join(filter(lambda x: 0 <= ord(x) < 256, unicode_string)) but are there any other simple ways ? Regards Srinivas Devaki Senior (final yr) student at Indian Institute of Technology (ISM), Dhanbad Computer Science and Engineering Department ph: +91 9491 383 249 telegram_id: @eightnoteight From steve at pearwood.info Mon Sep 26 19:56:31 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 27 Sep 2016 09:56:31 +1000 Subject: [Tutor] event loop vs threads In-Reply-To: References: Message-ID: <20160926235631.GM22471@ando.pearwood.info> Hi Srinivas, On Sat, Sep 24, 2016 at 01:09:46PM +0530, srinivas devaki wrote: > how does Python switch execution and maintain context i.e function stack > etc,.. for co-routines and why is it less costly than switching threads > which almost do the same, and both are handled by Python Interpreter > itself(event loop for co-routines and GIL scheduling for threading), so > where does the extra overhead for threads come from ? I'm afraid that your question is *far* too advanced for this mailing list, which is for beginners. I suggest that you ask your question on the main Python list: https://mail.python.org/mailman/listinfo/python-list also available on Usenet at comp.lang.python. If you don't get a response there, then you may ask the core Python developers on the Python-Dev mailing list: https://mail.python.org/mailman/listinfo/python-dev Regards, Steve From steve at pearwood.info Mon Sep 26 20:42:14 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 27 Sep 2016 10:42:14 +1000 Subject: [Tutor] unicode decode/encode issue In-Reply-To: References: Message-ID: <20160927004214.GN22471@ando.pearwood.info> I'm sorry, I have misinterpreted your question. On Mon, Sep 26, 2016 at 12:59:04PM -0400, bruce wrote: > I've got a page from a web fetch. I'm simply trying to go from utf-8 to > ascii. Why would you do that? It's 2016, not 1953, and ASCII is well and truly obsolete. (ASCII was even obsolete in 1953, even then there were characters in common use in American English that couldn't be written in ASCII, like ?.) Any modern program should be dealing with UTF-8. Nevertheless, assuming you have a good reason, you are dealing with data scraped from a webpage, so it is likely to include HTML escape codes, as you have already learned. So you need to go from something like this: – *first* to the actual EN DASH character, and then to the - hyphen. And remember that HTML supports all(?) of Unicode via character escapes, so you shouldn't assume that this is the only Unicode character. Assuming you scrape the data from the webpage as a byte string, you'll have something like this: data = "hello world – goodbye" # byte-string read from HTML page from HTMLParser import HTMLParser parser = HTMLParser() text = parser.unescape(data) print text which should display: hello world ? goodbye including the en-dash. So now you have a Unicode string, which you can manipulate any way you like: text = text.replace(u'?', u'--') # remember to use Unicode strings here See also the text.translate() method if you have to do lots of changes in one go. Lastly you can convert to an ASCII byte-string using the encode method. By default, this will raise an exception if there are any non-ASCII characters in your text string: data = text.encode('ascii') You can also skip non-ASCII characters, replace them with question marks, or replace them with an escape code: data = text.encode('ascii', 'ignore') data = text.encode('ascii', 'replace') data = text.encode('ascii', 'xmlcharrefreplace') which will finally give you something suitable for use in programs written in the 1970s :-) -- Steve From steve at pearwood.info Mon Sep 26 20:45:35 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 27 Sep 2016 10:45:35 +1000 Subject: [Tutor] Unicode to Ascii In-Reply-To: References: Message-ID: <20160927004535.GO22471@ando.pearwood.info> On Tue, Sep 27, 2016 at 03:46:25AM +0530, srinivas devaki wrote: > How can I convert Unicode to Ascii by stripping of any non ascii characters. > > one way is to filter on s like > > ascii = ''.join(filter(lambda x: 0 <= ord(x) < 256, unicode_string)) > > but are there any other simple ways ? See also my reply to Bruce just now, but the easiest way is to encode to bytes, then back to text: ascii = unicode_string.encode('ascii', 'ignore').decode('ascii') -- Steve From steve at pearwood.info Mon Sep 26 22:50:14 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Tue, 27 Sep 2016 12:50:14 +1000 Subject: [Tutor] TDD in Python In-Reply-To: References: Message-ID: <20160927025012.GP22471@ando.pearwood.info> Hi Angela, and welcome! My comments below, interleaved with yours. On Mon, Sep 26, 2016 at 09:54:18PM +0100, angela ebirim wrote: [...] > I'm keen to learn about testing my Python code and hoping someone could > help me with this query... > > #test.py > > class Test: > def __init__(self, members): > self.members = members > > def printMembers(self): > print('Printing members of Test class...') > for member in self.members: print('\t%s ' % member) > > > test = Test(['Gazelle', 'Elephant', 'Lion', 'Fish']) > test.printMembers() It is a little confusing to have your code *being tested* called "test". As you can see below, that leads to your test code being called "test_test". You might like to consider renaming the code being tested "demo", "example" or something similar. > #test_test.py > > import unittest > from test import Test > > class TestTestCase(unittest.TestCase): > """Tests for `test.py`.""" > > #preparing to test > def setUp(self): > """Sample test case""" > print "TestTest::setUp_:begin" > # self.members = [1, 2, 3, 4, 5] > self.members = Test([1, 2, 3, 4]) > > def test_is_printMembers(self): > """Will print a list of contents?""" > self.assertTrue(self.members.printMembers) [...] > Does my test code in test_test.py look correct? Unfortunately not. You are on the right track, but not quite there yet. Firstly, unfortunately you have picked a complicated example to test! Your printMembers method doesn't return a result, it operates by side-effect: it prints the existing members. That makes it hard to test. Your test_is_printMembers test method will always succeed (unless it crashes and raises an exception). Let's run through it by hand: self.assertTrue(self.members.printMembers) First we grab self.members, which is created by the setUp method. So far so good, this should always work. Then we fetch the printMembers attribute from self.members, but *don't* call it. That should always succeed, since the class does have a printMembers method. If it doesn't succeed, we get an exception, and this test case (test_is_printMembers) will be flagged as "E" (error). Finally we do our test assertion: assertTrue tests whether the method object is truthy, which it always is. Again, this cannot fail (unless you change printMembers to a falsey object, like None or 0). So long as printMembers is a method, this test will always succeed, no matter what the method does. So let's fix this. First, let's write a test that simply checks whether or not printMembers exists and is a method, we don't care what it does, only whether it is there: import inspect class TestTestCase(unittest.TestCase): """Tests for `test.py`.""" def setUp(self): self.members = Test([1, 2, 3, 4]) def test_printMembers_is_method(self): printMembers = self.members.printMembers # don't call it self.assertTrue(inspect.ismethod(printMembers)) That's not a very exiting test, but it is simple: it will raise an exception if printMembers doesn't exist, and the test will fail if it does exist but isn't a method. Now let's add a test that checks the result of calling the printMembers method. This is tricky because we have to capture the output of print. Let's start with a test that only checks that the method returns None. Add a couple more imports: import sys from io import StringIO and a new test method. We need to save the existing place where print output goes to ("standard out", or stdout), replace it with a fake, run call the method, replace the fake with the real one, and then check the result: def test_printMembers_returns_none(self): stdout = sys.stdout # Save the current stdout try: sys.stdout = StringIO() # replace it with a fake result = self.members.printMembers() # call the method finally: sys.stdout = stdout # restore the original # now we can test that the return result is what we expect self.assertTrue(result is None) Finally, let's check that the output of calling printMembers is correct. That's just a slight variation on the above: def test_printMembers_output(self): stdout = sys.stdout # Save the current stdout out = StringIO() try: sys.stdout = out self.members.printMembers() finally: sys.stdout = stdout # restore the original text = out.getvalue() expected = 'Printing members of Test class...\n' expected += '\t1 \n\t2 \n\t3 \n\t4 \n' self.assertEqual(text, expected) When you run the unit test suite, each test will be run, and it will print a nice progress display. Each test will print one of: . (dot) for passed tests E for tests which raise an exception F for tests which fail S for tests which are skipped P.S. I have not run the above code, so while I expect it to work, there may be a few bugs in it. I am also assuming you are using Python 3. -- Steve From shooby.herrmann at icloud.com Mon Sep 26 18:50:57 2016 From: shooby.herrmann at icloud.com (Shooby Herrmann) Date: Mon, 26 Sep 2016 18:50:57 -0400 Subject: [Tutor] Help!! Message-ID: <029501d21848$724f5780$56ee0680$@icloud.com> Hi, I am using JES. I need to write a program and find all pixels in a circle and in a square. Here is what I have written with the more detailed purpose of the program. I know this is not correct, but I'm not sure how to fix it. Please help. #The purpose of this assignment is to give you practice in function calls and if statements. Your program will have the user #to draw a random circle and a random rectangle and to guess which shape has more pixels. It will time how long the user #takes to do this and will connect the circle, the rectangle, and the outline of the picture with colored lines. from random import* def main(): #Make an empty white 500x500 picture. pic= makeEmptyPicture(500, 500, white) #Assign variable radius to random value from 10 to 120 (Why this range?). radius= randrange(10, 121) xCircle= randrange(0, 501) yCircle= randrange(0, 501) #Draw a circle with this radius anywhere in the picture - show the whole circle inside the picture. circle= addOvalFilled(pic, xCircle, yCircle, radius, radius) #Assign the variable numPxCircle to the number of pixels in this circle. numPxCircle= getPixels(pic) #Assign variables x and y to random values from 5 to 200 (Why this range?). x= randrange(5, 201) y= randrange(5, 201) #Assign variables width and height to random values. # - find the ranges of the values that will show the whole rectangle inside the picture. w= randrange(0, 501) h= randrange(0, 501) #Draw a green rectangle at the position (x,y) with this width and height on the picture: # - (x, y) is the upper left-hand corner of the rectangle. #Assign the variable numPxRectangle to the number of pixels in this rectangle. rectangle= addRectFilled(pic, x, y, w, h, green) numPxRectangle= getPixels(pic) #Show the picture. show(pic) #Ask the user, "Which one has more pixels (type 'r' or 'c')?" info= requestString("Which one has more pixels? (type r or c).") #If the answer is correct, fill the circle with red and pop up an information box saying "Congratulations!". if info== (numPxRectangle >= numPxCircle) : showInformation("Congradulations!") #If the answer is incorrect, pop up an information box saying "Sorry. Bye!" #(Make sure that the full circle and the full square are shown inside the picture.) From alan.gauld at yahoo.co.uk Tue Sep 27 04:36:12 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 27 Sep 2016 09:36:12 +0100 Subject: [Tutor] Help!! In-Reply-To: <029501d21848$724f5780$56ee0680$@icloud.com> References: <029501d21848$724f5780$56ee0680$@icloud.com> Message-ID: On 26/09/16 23:50, Shooby Herrmann wrote: > I am using JES. I've never heard of JES and know nothing about it. Fortunately it doesn't look like this problem has much to do with JES so that's not a problem this time... However in future when you submit a question (whether on this list or anywhere else) it will help immensely if you tell us what the problem is. What output did you expect? What did you get? If you get an error show us the full error message. Also tell us the Python version and OS you are using. It all helps us to help you. In this case it looks like a basic error so we can probably help without the extra details... > #The purpose of this assignment is to give you practice in function calls > and if statements. Your program will have the user > #to draw a random circle and a random rectangle and to guess which shape has > more pixels. It will time how long the user > #takes to do this and will connect the circle, the rectangle, and the > outline of the picture with colored lines. all the drawing stuff > #Ask the user, "Which one has more pixels (type 'r' or 'c')?" > > info= requestString("Which one has more pixels? (type r or c).") > So info will contain a character - either 'r' or 'c' > if info== (numPxRectangle >= numPxCircle) : But you are comparing that character to the result of a boolean comparison. Basically you are asking one of: if 'c' == True: if 'c' == False: if 'r' == True: if 'r' == False: None of which makes much sense, so Python returns False in every case. > showInformation("Congradulations!") So that line never gets executed. You need to change the code so you can determine whether the correct answer is 'c' or 'r' and then compare that to the users input. Also notice you were asked to time the user, you still need to add code for that. But for now, focus on getting the logic bit right. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From __peter__ at web.de Tue Sep 27 05:27:29 2016 From: __peter__ at web.de (Peter Otten) Date: Tue, 27 Sep 2016 11:27:29 +0200 Subject: [Tutor] TDD in Python References: Message-ID: angela ebirim wrote: > Hello everyone, > > I'm a new member on this maling list and am in the process of learning > python. > > I'm keen to learn about testing my Python code and hoping someone could > help me with this query... > > #test.py > > class Test: > def __init__(self, members): > self.members = members > > def printMembers(self): > print('Printing members of Test class...') > for member in self.members: print('\t%s ' % member) > > > test = Test(['Gazelle', 'Elephant', 'Lion', 'Fish']) > test.printMembers() > > #test_test.py > > import unittest > from test import Test > > class TestTestCase(unittest.TestCase): > """Tests for `test.py`.""" > > #preparing to test > > def setUp(self): > """Sample test case""" > print "TestTest::setUp_:begin" > # self.members = [1, 2, 3, 4, 5] > self.members = Test([1, 2, 3, 4]) > > > def test_is_printMembers(self): > """Will print a list of contents?""" > self.assertTrue(self.members.printMembers) > > > if __name__ == '__main__': > unittest.main() > > Does my test code in test_test.py look correct? Steven has enough experience to plough through and show you how to test what you have, but if you have the choice another approach is often advisable: write your code in such a way that it is easy to test. The easiest code to test is a function with a return value that is completely determined by its input, e. g. def add(a, b): return a + b This is called a "pure" function. A test would be just self.assertEqual(add(1, 2), 3) Slightly harder but still doable is a class class Calculator: def __init__(self, a, b): self.a = a self.b = b def sum(self): return self.a + self.b The test is self.assertEqual(Caculator(1, 2).sum(), 3) or in more complex cases class Test(TestCase): def setUp(self): self.calculator = Calculator(1, 2) def test_sum(self): self.assertEqual(self.calculator.sum(), 3) When you have to interact with the environment (the console, a GUI, a database) good tests will be harder to create, so the lazy bastard's approach is to avoid them if you can. That's why I would not add a print_sum() method to the above Caculator class. print_sum() would implicitly communicate with the console, so if you are serious about testing you might consider that it's closed while you write, redirected to a file, even a file on a full device, ... the possibilities and failure modes are endless. Instead I might add a format_sum() method def format_sum(self): return "{0.a} + {0.b} = {1}".format(self, self.sum()) If I want to print I can invoke it with c = Calculator(1, 2) print(c.format_sum()) which is not much harder than c.print_sum() but more flexible and easy to test def test_format_sum(self): self.assertEqual( self.calculator.format_sum(), "1 + 2 = 3") The outside world remains messy, but my Calculator class is unaffected, let the makers of print() take care of it. From sourceonly at gmail.com Tue Sep 27 05:23:52 2016 From: sourceonly at gmail.com (source liu) Date: Tue, 27 Sep 2016 17:23:52 +0800 Subject: [Tutor] differences between map and partial? Message-ID: Hi, List the test code as attached first i tried map, and failed, then i turned to partial from functools the latter one works, i wonder if there is unseen tricky from me between them :) this one works >>>>print p.map(partial(file_op,lineop=unity),input) this one doesn't work >>>>print p.map(lambda x:file_op(x,unity), input) Thanks. -- Liu An Institution of modern physics, Shanghai, China From thomas.floeck at siemens.com Tue Sep 27 11:23:23 2016 From: thomas.floeck at siemens.com (Floeck, Thomas) Date: Tue, 27 Sep 2016 15:23:23 +0000 Subject: [Tutor] NumPy and SciPy Message-ID: <391DC91FA32BFC449B07F6B717E2C74B0D5FA6E0@DEFTHW99EJ0MSX.ww902.siemens.net> Hi there, you have an idea where I can find NumPy and SciPy windows *.exe-files for Python 3.5? Thanks for any help! From dyoo at hashcollision.org Tue Sep 27 15:26:35 2016 From: dyoo at hashcollision.org (Danny Yoo) Date: Tue, 27 Sep 2016 12:26:35 -0700 Subject: [Tutor] differences between map and partial? In-Reply-To: References: Message-ID: On Tue, Sep 27, 2016 at 2:23 AM, source liu wrote: > Hi, List > > the test code as attached Unfortunately, it didn't attach. If you can inline the content of the test code, that would be helpful. > this one works >>>>print p.map(partial(file_op,lineop=unity),input) > this one doesn't work >>>>print p.map(lambda x:file_op(x,unity), input) I don't know what the function signature of file_op is. This is an important detail! I would expect: p.map(partial(file_op,lineop=unity),input) and: p.map(lambda x:file_op(x,lineop=unity), input) to be functionality equivalent. However, I do not know, without seeing file_op, whether: p.map(lambda x:file_op(x, lineop=unity), input) versus: p.map(lambda x:file_op(x, unity), input) Depending on method signature, this might not be equivalent, especially if file_op takes in multiple keyword arguments. Also, I don't know what you mean by "works" vs. "doesn't work": those are human expressions that are ambiguous enough that I only know something is wrong, but I don't know what. :) Can you be more specific? If you see error messages or stack traces, please post them, as they are additional "signal" that may be helpful in figuring out what's happening. From alan.gauld at yahoo.co.uk Tue Sep 27 19:54:36 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 28 Sep 2016 00:54:36 +0100 Subject: [Tutor] NumPy and SciPy In-Reply-To: <391DC91FA32BFC449B07F6B717E2C74B0D5FA6E0@DEFTHW99EJ0MSX.ww902.siemens.net> References: <391DC91FA32BFC449B07F6B717E2C74B0D5FA6E0@DEFTHW99EJ0MSX.ww902.siemens.net> Message-ID: On 27/09/16 16:23, Floeck, Thomas wrote: > you have an idea where I can find NumPy and SciPy windows *.exe-files for Python 3.5? I'm not sure if they are up to 3.5 yet or not but the easiest way is just to grab a full distro such as Anaconda or Canopy. Google is your friend. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From ben+python at benfinney.id.au Tue Sep 27 20:25:27 2016 From: ben+python at benfinney.id.au (Ben Finney) Date: Wed, 28 Sep 2016 10:25:27 +1000 Subject: [Tutor] Google is not your friend (was: NumPy and SciPy) References: <391DC91FA32BFC449B07F6B717E2C74B0D5FA6E0@DEFTHW99EJ0MSX.ww902.siemens.net> Message-ID: <85d1jolvk8.fsf_-_@benfinney.id.au> Alan Gauld via Tutor writes: > I'm not sure if they are up to 3.5 yet or not but the easiest way is > just to grab a full distro such as Anaconda or Canopy. Google is your > friend. I do wish that meme would die. Google is many things, but it is *not* your friend. Corporations are not friends of anyone. In particular, Google is not your friend even when you use their search . So, Google may be useful, but let's stop telling people it's a friend. -- \ ?The great thing about science is we can test our ideas.? But | `\ until we do, until we have data, it is just one more proposal.? | _o__) ?Darren Saunders, 2015-12-02 | Ben Finney From steve at pearwood.info Tue Sep 27 20:32:49 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 28 Sep 2016 10:32:49 +1000 Subject: [Tutor] differences between map and partial? In-Reply-To: References: Message-ID: <20160928003248.GR22471@ando.pearwood.info> Hello Liu An and welcome! On Tue, Sep 27, 2016 at 05:23:52PM +0800, source liu wrote: > this one works >>>>print p.map(partial(file_op,lineop=unity),input) > this one doesn't work >>>>print p.map(lambda x:file_op(x,unity), input) What does "doesn't work" mean? What is "p.map"? What are "file_op" and "input"? It will help if you can provide a minimal example of code that we can actually run: http://sscce.org/ and also state what you expect to happen and what actually happens. But my *guess* is that if the following two expressions are different: partial(file_op, lineop=unity) lambda x:file_op(x, unity) that implies that file_op() takes more than two arguments, and in the second case (the lambda) unity is being used for something other than lineop. Try to compare these instead: partial(file_op, lineop=unity) lambda x:file_op(x, lineop=unity) -- Steve From nirajkumarpandey at gmail.com Wed Sep 28 01:22:44 2016 From: nirajkumarpandey at gmail.com (niraj pandey) Date: Wed, 28 Sep 2016 10:52:44 +0530 Subject: [Tutor] Need help Message-ID: Hi, I am new in python. Could you guys please help me to short this code ? Want to write this iteration (Yellow one) using loop. r = 0 L1 = Label(bg = 'orange', text="Flat_No", relief=RIDGE,width=30) L1.grid(row=0,column=0) E1 = Entry(relief=SUNKEN,width=30) E1.grid(row=0,column=1) L2 = Label(bg = 'orange', text="Mains Unit", relief=RIDGE,width=30) L2.grid(row=1,column=0) E2 = Entry(relief=SUNKEN,width=30) E2.grid(row=1,column=1) L3 = Label(bg = 'orange', text="DG Unit", relief=RIDGE,width=30) L3.grid(row=2,column=0) E3 = Entry(relief=SUNKEN,width=30) E3.grid(row=2,column=1) L4 = Label(bg = 'orange', text="Month", relief=RIDGE,width=30) L4.grid(row=3,column=0) E4 = Entry(relief=SUNKEN,width=30) E4.grid(row=3,column=1) MyButton1 = Button(top, text="Submit", width=10, bg='red', command=lambda: database.data(E1.get(), E2.get(), E3.get(), E4.get())) MyButton1.grid(row=8, column=1) The output should be like this : [image: Inline image 1] Thanks Niraj -- Success occurs when opportunity and preparation meet From nirajkumarpandey at gmail.com Wed Sep 28 03:00:34 2016 From: nirajkumarpandey at gmail.com (niraj pandey) Date: Wed, 28 Sep 2016 12:30:34 +0530 Subject: [Tutor] Need help In-Reply-To: References: Message-ID: Found the solution for this . entry_option = ['Flat_No','Mains Unit','DG Unit','Month'] r = 0 entry = {} label = {} for item in entry_option: lb = Label(bg = 'orange', text=item, relief=RIDGE,width=30) lb.grid(row=r,column=0) label[item] = lb e = Entry(relief=SUNKEN,width=30) e.grid(row=r,column=1) entry[item] = e r=r+1 But now how to pass these values as an argument for this function ? command=lambda: database.data(E1.get(), E2.get(), E3.get(), E4.get()) Thanks Niraj On Wed, Sep 28, 2016 at 10:52 AM, niraj pandey wrote: > Hi, > > I am new in python. Could you guys please help me to short this code ? > > Want to write this iteration (Yellow one) using loop. > > r = 0 > L1 = Label(bg = 'orange', text="Flat_No", relief=RIDGE,width=30) > L1.grid(row=0,column=0) > E1 = Entry(relief=SUNKEN,width=30) > E1.grid(row=0,column=1) > L2 = Label(bg = 'orange', text="Mains Unit", relief=RIDGE,width=30) > L2.grid(row=1,column=0) > E2 = Entry(relief=SUNKEN,width=30) > E2.grid(row=1,column=1) > L3 = Label(bg = 'orange', text="DG Unit", relief=RIDGE,width=30) > L3.grid(row=2,column=0) > E3 = Entry(relief=SUNKEN,width=30) > E3.grid(row=2,column=1) > L4 = Label(bg = 'orange', text="Month", relief=RIDGE,width=30) > L4.grid(row=3,column=0) > E4 = Entry(relief=SUNKEN,width=30) > E4.grid(row=3,column=1) > > > MyButton1 = Button(top, text="Submit", width=10, bg='red', command=lambda: > database.data(E1.get(), E2.get(), E3.get(), E4.get())) > MyButton1.grid(row=8, column=1) > > The output should be like this : > > [image: Inline image 1] > > Thanks > Niraj > -- > Success occurs when opportunity and preparation meet > -- Success occurs when opportunity and preparation meet From sourceonly at gmail.com Tue Sep 27 22:56:59 2016 From: sourceonly at gmail.com (source liu) Date: Wed, 28 Sep 2016 10:56:59 +0800 Subject: [Tutor] differences between map and partial? In-Reply-To: References: Message-ID: Hi, Danny, Thank you for your reply I checked the mail, the attachment is attached, i don't know why you can't see it ( I change the extension of .tar.gz to .tar.gz_src, as gmail can't pass the security check of the .tar.gz) I would like to inline the code as well as attached again( the are all simple files ) ------ accouting - log1 - log2 logtool - __init__.py - tools.py run.py log1,log2 ( write anything you like, it pretend to be log file ) __init__.py :blank ------tools.py------------ #!/bin/env python def identy(string): print string; def unity(string): return 1 def line_op(string,op=identy): return apply(op,(string,)) def file_op(filename,lineop): f=open(filename) res=None; try: res=map(lambda x:line_op(x,lineop),f) finally: f.close() return res ------------run.py-------------------------------------- import os #log_location="/home/source/python/pg/accounting" log_location='accounting' from multiprocessing import Pool from functools import partial if __name__=="__main__": from logtool.tools import * p=Pool(processes=4); for root,dir,file in os.walk(log_location): input=map(lambda x: os.path.join(root,x),file) print p.map(partial(file_op,lineop=unity),input) #print p.map(lambda x:file_op(x,unity), input) ========================== THAT'S ALL OF THE CODE Thank you On Wed, Sep 28, 2016 at 3:26 AM, Danny Yoo wrote: > On Tue, Sep 27, 2016 at 2:23 AM, source liu wrote: >> Hi, List >> >> the test code as attached > > > Unfortunately, it didn't attach. If you can inline the content of the > test code, that would be helpful. > > >> this one works >>>>print p.map(partial(file_op,lineop=unity),input) >> this one doesn't work >>>>print p.map(lambda x:file_op(x,unity), input) > > > I don't know what the function signature of file_op is. This is an > important detail! > > > > I would expect: > > p.map(partial(file_op,lineop=unity),input) > > and: > > p.map(lambda x:file_op(x,lineop=unity), input) > > to be functionality equivalent. > > > > However, I do not know, without seeing file_op, whether: > > p.map(lambda x:file_op(x, lineop=unity), input) > > versus: > > p.map(lambda x:file_op(x, unity), input) > > Depending on method signature, this might not be equivalent, > especially if file_op takes in multiple keyword arguments. > > > > Also, I don't know what you mean by "works" vs. "doesn't work": those > are human expressions that are ambiguous enough that I only know > something is wrong, but I don't know what. :) Can you be more > specific? If you see error messages or stack traces, please post > them, as they are additional "signal" that may be helpful in figuring > out what's happening. -- Liu An Institution of modern physics, Shanghai, China From __peter__ at web.de Wed Sep 28 04:36:34 2016 From: __peter__ at web.de (Peter Otten) Date: Wed, 28 Sep 2016 10:36:34 +0200 Subject: [Tutor] differences between map and partial? References: Message-ID: source liu wrote: > Hi, Danny, > > > Thank you for your reply > > I checked the mail, the attachment is attached, i don't know why you > can't see it ( I change the extension of .tar.gz to .tar.gz_src, as > gmail can't pass the security check of the .tar.gz) They are stripped off by the mailing list software, so don't bother trying. > from multiprocessing import Pool > p=Pool(processes=4); > print p.map(partial(file_op,lineop=unity),input) > #print p.map(lambda x:file_op(x,unity), input) The missing part of information was that you are using multiprocessing (The traceback would also have shown the problem). Multiprocessing uses pickle to pass data around between processes, and anonymous functions (aka lambdas) cannot be pickled. Instead you have to use an ordinary function defined on the module level def whatever(x): return file_op(x, unity) p.map(whatever, input) For these pickle need not pass the code, it just remembers the module and function name which are then used to look up the function during unpickling. As you found out functools.partial() can be pickled, too, and thus works when all of its arguments can be pickled (in particular its first argument has to be a global function rather than a local one or a lambda). From __peter__ at web.de Wed Sep 28 05:17:02 2016 From: __peter__ at web.de (Peter Otten) Date: Wed, 28 Sep 2016 11:17:02 +0200 Subject: [Tutor] Need help References: Message-ID: niraj pandey wrote: > Found the solution for this. You can further simplifiy it with enumerate() > entry_option = ['Flat_No','Mains Unit','DG Unit','Month'] > entry = {} > label = {} for r, item in enumerate(entry_option): > lb = Label(bg = 'orange', text=item, relief=RIDGE,width=30) > lb.grid(row=r,column=0) > label[item] = lb > e = Entry(relief=SUNKEN,width=30) > e.grid(row=r,column=1) > entry[item] = e > > But now how to pass these values as an argument for this function ? > > command=lambda: database.data(E1.get(), E2.get(), E3.get(), E4.get()) Well, you saved the Entry instances in a dict, so you can retrieve them: command=lambda: database.data(*[entry[k].get() for k in entry_option]) If you use a list instead of or in addition to the dict entries = [] for ...: # your loop from above ... entries.append(e) the lambda becomes command=lambda: database.data(*[e.get() for e in entries]) If you have not come across it before: the * operator unpacks the list, so args = ["a", "b"] f(*args) is equivalent to calling f with all items in the list, f(args[0], args[1]) in the above example. From alan.gauld at yahoo.co.uk Wed Sep 28 07:28:21 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 28 Sep 2016 12:28:21 +0100 Subject: [Tutor] Google is not your friend In-Reply-To: <85d1jolvk8.fsf_-_@benfinney.id.au> References: <391DC91FA32BFC449B07F6B717E2C74B0D5FA6E0@DEFTHW99EJ0MSX.ww902.siemens.net> <85d1jolvk8.fsf_-_@benfinney.id.au> Message-ID: On 28/09/16 01:25, Ben Finney wrote: > Alan Gauld via Tutor writes: > >> I'm not sure if they are up to 3.5 yet or not but the easiest way is >> just to grab a full distro such as Anaconda or Canopy. Google is your >> friend. > > I do wish that meme would die. Google is many things, but it is *not* > your friend. Corporations are not friends of anyone. Valid point, I actually use duck-duck-go personally, but I was intending to imply google in the generic sense of a search engine. So I should just have said search... -- 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 nirajkumarpandey at gmail.com Wed Sep 28 04:02:20 2016 From: nirajkumarpandey at gmail.com (niraj pandey) Date: Wed, 28 Sep 2016 13:32:20 +0530 Subject: [Tutor] Need help In-Reply-To: References: Message-ID: Ignore this I have done it. Thanks Niraj On Wed, Sep 28, 2016 at 12:30 PM, niraj pandey wrote: > Found the solution for this . > > entry_option = ['Flat_No','Mains Unit','DG Unit','Month'] > > r = 0 > entry = {} > label = {} > for item in entry_option: > lb = Label(bg = 'orange', text=item, relief=RIDGE,width=30) > lb.grid(row=r,column=0) > label[item] = lb > e = Entry(relief=SUNKEN,width=30) > e.grid(row=r,column=1) > entry[item] = e > r=r+1 > > But now how to pass these values as an argument for this function ? > > command=lambda: database.data(E1.get(), E2.get(), E3.get(), E4.get()) > > Thanks > Niraj > > On Wed, Sep 28, 2016 at 10:52 AM, niraj pandey > wrote: > >> Hi, >> >> I am new in python. Could you guys please help me to short this code ? >> >> Want to write this iteration (Yellow one) using loop. >> >> r = 0 >> L1 = Label(bg = 'orange', text="Flat_No", relief=RIDGE,width=30) >> L1.grid(row=0,column=0) >> E1 = Entry(relief=SUNKEN,width=30) >> E1.grid(row=0,column=1) >> L2 = Label(bg = 'orange', text="Mains Unit", relief=RIDGE,width=30) >> L2.grid(row=1,column=0) >> E2 = Entry(relief=SUNKEN,width=30) >> E2.grid(row=1,column=1) >> L3 = Label(bg = 'orange', text="DG Unit", relief=RIDGE,width=30) >> L3.grid(row=2,column=0) >> E3 = Entry(relief=SUNKEN,width=30) >> E3.grid(row=2,column=1) >> L4 = Label(bg = 'orange', text="Month", relief=RIDGE,width=30) >> L4.grid(row=3,column=0) >> E4 = Entry(relief=SUNKEN,width=30) >> E4.grid(row=3,column=1) >> >> >> MyButton1 = Button(top, text="Submit", width=10, bg='red', >> command=lambda: database.data(E1.get(), E2.get(), E3.get(), E4.get())) >> MyButton1.grid(row=8, column=1) >> >> The output should be like this : >> >> [image: Inline image 1] >> >> Thanks >> Niraj >> -- >> Success occurs when opportunity and preparation meet >> > > > > -- > Success occurs when opportunity and preparation meet > -- Success occurs when opportunity and preparation meet From nirajkumarpandey at gmail.com Wed Sep 28 05:22:07 2016 From: nirajkumarpandey at gmail.com (niraj pandey) Date: Wed, 28 Sep 2016 14:52:07 +0530 Subject: [Tutor] Need help In-Reply-To: References: Message-ID: Thanks Peter for the help. Best Regards Niraj On Wed, Sep 28, 2016 at 2:47 PM, Peter Otten <__peter__ at web.de> wrote: > niraj pandey wrote: > > > Found the solution for this. > > You can further simplifiy it with enumerate() > > entry_option = ['Flat_No','Mains Unit','DG Unit','Month'] > > > entry = {} > > label = {} > for r, item in enumerate(entry_option): > > lb = Label(bg = 'orange', text=item, relief=RIDGE,width=30) > > lb.grid(row=r,column=0) > > label[item] = lb > > e = Entry(relief=SUNKEN,width=30) > > e.grid(row=r,column=1) > > entry[item] = e > > > > But now how to pass these values as an argument for this function ? > > > > command=lambda: database.data(E1.get(), E2.get(), E3.get(), E4.get()) > > Well, you saved the Entry instances in a dict, so you can retrieve them: > > command=lambda: database.data(*[entry[k].get() for k in entry_option]) > > If you use a list instead of or in addition to the dict > > entries = [] > for ...: # your loop from above > ... > entries.append(e) > > the lambda becomes > > command=lambda: database.data(*[e.get() for e in entries]) > > If you have not come across it before: the * operator unpacks the list, so > > args = ["a", "b"] > f(*args) > > is equivalent to calling f with all items in the list, > > f(args[0], args[1]) > > in the above example. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- Success occurs when opportunity and preparation meet From steve at pearwood.info Wed Sep 28 22:08:43 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 29 Sep 2016 12:08:43 +1000 Subject: [Tutor] NumPy and SciPy In-Reply-To: <391DC91FA32BFC449B07F6B717E2C74B0D5FA6E0@DEFTHW99EJ0MSX.ww902.siemens.net> References: <391DC91FA32BFC449B07F6B717E2C74B0D5FA6E0@DEFTHW99EJ0MSX.ww902.siemens.net> Message-ID: <20160929020843.GV22471@ando.pearwood.info> On Tue, Sep 27, 2016 at 03:23:23PM +0000, Floeck, Thomas wrote: > Hi there, > > you have an idea where I can find NumPy and SciPy windows *.exe-files > for Python 3.5? Why does it have to be an .exe file? http://www.scipy.org/install.html -- Steve From robertvstepp at gmail.com Thu Sep 29 22:24:51 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Thu, 29 Sep 2016 21:24:51 -0500 Subject: [Tutor] Testing print Message-ID: Testing output of print functions (Py 3). First off, is it worth it to do so? Second, it seems that prints are often intermingled with the main logic of a function and only serve to pass on a message to the user. For example, in an earlier thread ( Questions as to how to run the same unit test multiple times on varying input data.: https://mail.python.org/pipermail/tutor/2016-September/109686.html) I finally wound up with this function: def right_justify(a_string): '''This fucntion will take the string, "a_string", and right justify it by padding it with spaces until its last character falls in column 70 of the display. The padded string will be returned.''' if len(a_string) <= 70: num_pad_spcs = 70 - len(a_string) return (' ' * num_pad_spcs) + a_string else: print("The string has too many characters (> 70)!") print("Only a partial, 70 character line will be returned.") return a_string[:70] I had to consider the possibility that the string supplied to the function was more than 70 characters long. The exercise in "Think Python2" did not give any input into how to handle such a case, so I decided I would return 70 characters worth of the overly long string and print a message to the user. So (Even though this is a simple exercise.) should I test the prints? And if so, how to I separate this out? In my mind, if I am going to be testing calls to print, then perhaps I should write a separate function, say "print_msg(msg)" and put that in place of the prints in the else clause and then test the print_msg function, where "print(msg)" would be isolated in its own little function to be tested.. Anyway, it would seem that the only way to capture the output of a print is to redirect the stdout to something I can capture and compare against. Googling brings up some people doing something with mock objects, some redirecting to a string buffer, some to a file, some to other things. What, in your opinion(s), is the cleanest way to handle this? -- boB From robertvstepp at gmail.com Thu Sep 29 22:43:57 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Thu, 29 Sep 2016 21:43:57 -0500 Subject: [Tutor] Passing functions as arguments to other functions Message-ID: I believe I understand the barebone mechanics on how to do this. But I do not understand the rationale of why Python does it the way it does. Say def f(g, *args): g(*args) def g(*args): # Do something. do_things = f(g, *args) is the outline of how I understand the mechanics of doing this. But noob boB initially want to do instead: def f(g(*args)): g(*args) def g(*args): # Do somenthing. do_things = f(g(*args)) which, of course, will give me a syntax error. Also, I note that if I just type a function name without the parentheses in the interpreter, I will get something like this: >>> def f(): pass >>> f So the impression I am getting is that a function name by itself (with no parentheses) is the function *object*. But why does Python require separating the function object from its parameters when it is being passed as an argument to another function? -- boB From ben+python at benfinney.id.au Fri Sep 30 01:28:00 2016 From: ben+python at benfinney.id.au (Ben Finney) Date: Fri, 30 Sep 2016 15:28:00 +1000 Subject: [Tutor] Passing functions as arguments to other functions References: Message-ID: <85twcyj6sf.fsf@benfinney.id.au> boB Stepp writes: > So the impression I am getting is that a function name by itself (with > no parentheses) is the function *object*. Yes. The expression ?func? resolves to whatever object is referenced by that name; if it's a function object, you get that object. The expression ?func(foo, bar)? resolves to whatever object is returned from *calling* the object ?func?, with the arguments ?foo, bar?. > But why does Python require separating the function object from its > parameters when it is being passed as an argument to another function? You've already answered your question. An expression resolves to exactly one object. Either you want the function object, or you want something else. In the case of ?func(foo, bar)? you will get exactly one object from that expression. You won't get the ?func? object as well; you need a different expression (the expression ?func?) to get that. If you want to talk about different objects at the same time, you need multiple parameters in which to place those objects. -- \ ?By instructing students how to learn, unlearn, and relearn, a | `\ powerful new dimension can be added to education.? ?Alvin | _o__) Toffler, _Future Shock_, 1970 | Ben Finney From alan.gauld at yahoo.co.uk Fri Sep 30 04:43:47 2016 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 30 Sep 2016 09:43:47 +0100 Subject: [Tutor] Passing functions as arguments to other functions In-Reply-To: References: Message-ID: On 30/09/16 03:43, boB Stepp wrote: > Also, I note that if I just type a function name without the > parentheses in the interpreter, I will get something like this: > >>>> def f(): > pass > >>>> f > > > So the impression I am getting is that a function name by itself (with > no parentheses) is the function *object*. No. The function name is just a name like any other name in Python foo = 42 bar = lambda x: x**2 foo and bar are both names. foo refers to the integer object 42 and bar refers to the function object x**2. But they are just names and we can reassign them at any time to any other kind of object. So in your example f is not the function object it is a reference to a function object. > But why does Python require separating the function object > from its parameters when it is being passed as an argument > to another function? g = f causes g to become a second reference to the function object g = f(x) causes g to become a reference to the result of calling the function object referred to by f, passing in the object referred to by x. The parentheses in this case are not there as a container of the object x they are there as an indication that f is being called. Now let us assume that f doesn't care what kind of object x is it would be perfectly possible to pass f itself as an argument to f(): g = f(f) The mechanism is exactly the same as before: It causes g to become a reference to the result of calling the function object referred to by f, passing in the object referred to by f - a function object in this case. and again if we do g = f(f()) we apply the same rules. The argument to the outer f is the result of calling the inner f() and g gets the result of calling f with that inner result. The parens after a function name cause that function to be executed and its result to be returned. So python is not doing anything different to the "normal" case when passing functions. They are treated just like any other function call parameter, as an object. BTW You don't have to pass the arguments as separate values. You could use globals(but please don't!) or you could create local values inside the containing function, or even read them from a user: def do_op(aFunc): x = int(input("type an integer: ")) return aFunc(x) do_op(lambda x: x**2) or def process_3_and_4(aFunc): return aFunc(3,4) process_3_and_4(lambda x,y: x**y) HTH -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos From steve at pearwood.info Fri Sep 30 06:07:08 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 30 Sep 2016 20:07:08 +1000 Subject: [Tutor] Testing print In-Reply-To: References: Message-ID: <20160930100707.GY22471@ando.pearwood.info> On Thu, Sep 29, 2016 at 09:24:51PM -0500, boB Stepp wrote: > Testing output of print functions (Py 3). First off, is it worth it to do so? Unless you are writing tests for the Python language itself, you can assume that print() itself is working. You should test functions that call print: call the function (or method) and see that it does print what you expect. But, in general, you should not intermingle *calculation* and *output*. There may be a few examples, but functions should do one, or the other, not both. This makes it easy to reason about, re-use, and test, both functions. For example, suppose you have a function that takes a single string, formats it in some way, and then displays it in a dialog box. You should split that into two: - take a string and format it; - take a formatted string and display it. Same applies to print, writing to log files, or writing output to any other file. > Second, it seems that prints are often intermingled with the main > logic of a function and only serve to pass on a message to the user. Yeah, you normally shouldn't do that. Imagine if every time you called len(something), the len function was chatty and decided to print something. It would make it really hard to print anything where len() is involved as part of the calculation. Say I want to print a formatted table: -------- -------- -------- COLUMN 1 COLUMN 2 COLUMN 3 -------- -------- -------- alpha second gamma letter -------- -------- -------- but got something like: hi this is len I just wanted to let you know I'm working hard -------- hi this is len I just wanted to let you know I'm working hard -------- hi this is len I just wanted to let you know I'm working hard -------- hi this is len I just wanted to let you know I'm working hard COLUMN 1 hi this is len oops something went wrong but that's okay I fixed it COLUMN 2 hi this is len I just wanted to let you know I'm working really hard you ought to be grateful okay COLUMN 3 hi this is len I just wanted to let you know I'm working hard -------- hi this is len ... etc. Chatty functions are *the worst*, unless they are specifically intended for reporting and output. Don't mix calculation and output. Or rather, its okay to mix them, provided you have *at least* two separate functions: (1) function that does the calculation; (2) function that calls (1) and then does the output and preferably three: (1) function that does the calculation; (2) function that does the output; (3) function that calls (1) and then (2) If (1) and (2) are well-designed, then (3) is so trivial it needs no tests: def main(): x = calculate(stuff) report(x) but of course it's not always that simple. Nevertheless, that's the ideal you should aim for. [...] > def right_justify(a_string): > '''This fucntion will take the string, "a_string", and right justify it by > padding it with spaces until its last character falls in column 70 of the > display. The padded string will be returned.''' > > if len(a_string) <= 70: > num_pad_spcs = 70 - len(a_string) > return (' ' * num_pad_spcs) + a_string > else: > print("The string has too many characters (> 70)!") > print("Only a partial, 70 character line will be returned.") > return a_string[:70] Too chatty! It mixed output and calculation. Instead, you should just document what the function does ("pads or truncates the string to exactly 70 characters") and leave it at that, or if you must report on what it is doing, consider: - print warning messages to stderr instead of stdout; - write warnings to a log file; - use the warnings module: import warnings warnings.warn("unable to pad string, as it is too long; truncating instead") - raise an exception and stop processing. [...] > So (Even though this is a simple > exercise.) should I test the prints? If you don't test it, how do you know it works? > Anyway, it would seem that the only way to capture the output of a > print is to redirect the stdout to something I can capture and compare > against. Googling brings up some people doing something with mock > objects, some redirecting to a string buffer, some to a file, some to > other things. What, in your opinion(s), is the cleanest way to handle > this? I find that using stringIO is the simplest and most obvious way to do it, although I don't have a lot of experience with the mock objects. -- Steve From steve at pearwood.info Fri Sep 30 07:55:57 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 30 Sep 2016 21:55:57 +1000 Subject: [Tutor] Passing functions as arguments to other functions In-Reply-To: References: Message-ID: <20160930115555.GZ22471@ando.pearwood.info> On Thu, Sep 29, 2016 at 09:43:57PM -0500, boB Stepp wrote: > I believe I understand the barebone mechanics on how to do this. But > I do not understand the rationale of why Python does it the way it > does. Say > > def f(g, *args): > g(*args) Right. This says: - Define a function called "f", which takes one named parameter, "g", and collect any other arguments under the name "args". - For the body of "f", simply call that argument "g" with all the other arguments. You probably want `return g(*args)` rather than just `g(*args)`. > def g(*args): > # Do something. > > do_things = f(g, *args) As it stands now, f() is a middle-man that adds nothing to the picture. In practice, to be worth calling f() rather than just calling g() directly, you would want it to do some sort of additional processing: either manipulate the arguments, or the result returned by g(). > is the outline of how I understand the mechanics of doing this. But > noob boB initially want to do instead: > > def f(g(*args)): > g(*args) [...] > which, of course, will give me a syntax error. Of course. I'm not sure what you think a parameter called "g(*args)" even means. When you write this: def f(x): return x+1 do you feel the need to write: def f(x+1): return x+1 instead? I expect not. Can you see how the two situations are equivalent? My example uses +1, your example uses "call the function with these arguments" instead, but the two are otherwise equivalent. In common English, there is a simple name for the +1 part of x+1, namely "plus one" or "add one" or similar. But I don't know of any simple name for the `(*args)` part of `g(*args)`. "Call g with given arguments" perhaps, but that's not really simple. The situations are conceptually the same: + is a binary (two argument) operator that takes the object on the left, x, and the object on the right, 1, and adds them together; () is an n-ary (n arguments) operator (or perhaps "pseudo-operator" if you prefer) that takes the object on the left, g, and the multiple arguments inside the brackets, *args, and applies the arguments to function g. > Also, I note that if I just type a function name without the > parentheses in the interpreter, I will get something like this: > > >>> def f(): > pass > > >>> f > > > So the impression I am getting is that a function name by itself (with > no parentheses) is the function *object*. Correct. > But why does Python require > separating the function object from its parameters when it is being > passed as an argument to another function? If you pass the function arguments to the function *first*, then it gets evaluated before the second function gets to see it. Let's investigate: def g(*args): print("Calling function g with arguments {}".format(args)) return 999 def f(func, *args): print("f received first argument: {}".format(func)) print("f received additional arguments: {}".format(args)) return func(*args) Let's try it the right way: py> f(g, 1, 2, 3) f received first argument: f received additional arguments: (1, 2, 3) Calling function g with arguments (1, 2, 3) 999 And the wrong way: py> f(g(1, 2, 3)) Calling function g with arguments (1, 2, 3) f received first argument: 999 f received additional arguments: () Traceback (most recent call last): File "", line 1, in File "", line 4, in f TypeError: 'int' object is not callable Does this help? -- Steve From steve at pearwood.info Fri Sep 30 08:26:16 2016 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 30 Sep 2016 22:26:16 +1000 Subject: [Tutor] Passing functions as arguments to other functions In-Reply-To: References: Message-ID: <20160930122616.GA22471@ando.pearwood.info> On Fri, Sep 30, 2016 at 09:43:47AM +0100, Alan Gauld via Tutor wrote: > On 30/09/16 03:43, boB Stepp wrote: > > > Also, I note that if I just type a function name without the > > parentheses in the interpreter, I will get something like this: > > > >>>> def f(): > > pass > > > >>>> f > > > > > > So the impression I am getting is that a function name by itself (with > > no parentheses) is the function *object*. > > No. > The function name is just a name like any other name in Python Alan's correct here -- functions aren't treated specially. But in the same way that is you say: x = 999 x the bare `x` on its own returns the int object 999, so: def func(): ... func the bare `func` on its own returns the function object called "func". So in *that sense alone*, func without the parentheses is the function object. [...] > So in your example f is not the function object it is a > reference to a function object. You say tomahto, I say edible wolf peach: http://recipes.howstuffworks.com/fresh-ideas/dinner-food-facts/tomato-called-a-love-apple.htm -- Steve From robertvstepp at gmail.com Fri Sep 30 20:22:59 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 30 Sep 2016 19:22:59 -0500 Subject: [Tutor] Testing print In-Reply-To: <20160930100707.GY22471@ando.pearwood.info> References: <20160930100707.GY22471@ando.pearwood.info> Message-ID: On Fri, Sep 30, 2016 at 5:07 AM, Steven D'Aprano wrote: > On Thu, Sep 29, 2016 at 09:24:51PM -0500, boB Stepp wrote: >> Second, it seems that prints are often intermingled with the main >> logic of a function and only serve to pass on a message to the user. > > Yeah, you normally shouldn't do that. Imagine if every time you called > len(something), the len function was chatty and decided to print > something. It would make it really hard to print anything where len() is > involved as part of the calculation. Say I want to print a formatted > table: > > -------- -------- -------- > COLUMN 1 COLUMN 2 COLUMN 3 > -------- -------- -------- > alpha second gamma > letter > -------- -------- -------- > > but got something like: > > > hi this is len I just wanted to let you know I'm working hard > -------- hi this is len I just wanted to let you know I'm working hard > -------- hi this is len I just wanted to let you know I'm working hard > -------- > hi this is len I just wanted to let you know I'm working hard COLUMN > 1 hi this is len oops something went wrong but that's okay I fixed it > COLUMN 2 hi this is len I just wanted to let you know I'm working really > hard you ought to be grateful okay COLUMN 3 > hi this is len I just wanted to let you know I'm working hard > -------- hi this is len ... You're a funny guy, Steve! I must confess, I have been very cavalier in using prints in my functions. It was not until I was trying to imagine last night how to test the example function I gave that it truly dawned on me that this has been a baaaaad practice on my part. This TDD practice has been making me think about my coding efforts in ways that I never considered previously. Testing is a very, very good thing! Thanks, Steve! -- boB From robertvstepp at gmail.com Fri Sep 30 20:36:57 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 30 Sep 2016 19:36:57 -0500 Subject: [Tutor] Passing functions as arguments to other functions In-Reply-To: References: Message-ID: On Fri, Sep 30, 2016 at 3:43 AM, Alan Gauld via Tutor wrote: > On 30/09/16 03:43, boB Stepp wrote: > >> Also, I note that if I just type a function name without the >> parentheses in the interpreter, I will get something like this: >> >>>>> def f(): >> pass >> >>>>> f >> >> >> So the impression I am getting is that a function name by itself (with >> no parentheses) is the function *object*. > > No. > The function name is just a name like any other name in Python I understand this. It gets tiring to be precise and say something like, "So the impression I am getting is that a function name by itself (with no parentheses) references the function object." But I probably should strive to be more precise in my writing as you otherwise have to guess what I know and don't know. >> But why does Python require separating the function object >> from its parameters when it is being passed as an argument >> to another function? > > g = f > > causes g to become a second reference to the function object > > g = f(x) > > causes g to become a reference to the result of calling > the function object referred to by f, passing in the object > referred to by x. > > The parentheses in this case are not there as a container > of the object x they are there as an indication that f > is being called. I think this was my key point of confusion. I was mistakenly thinking of f(x) as referring to the function object. Instead, it is calling that object with argument x, which is two separate things. > HTH It does. Thanks, Alan! -- boB From robertvstepp at gmail.com Fri Sep 30 20:46:00 2016 From: robertvstepp at gmail.com (boB Stepp) Date: Fri, 30 Sep 2016 19:46:00 -0500 Subject: [Tutor] Passing functions as arguments to other functions In-Reply-To: <20160930115555.GZ22471@ando.pearwood.info> References: <20160930115555.GZ22471@ando.pearwood.info> Message-ID: On Fri, Sep 30, 2016 at 6:55 AM, Steven D'Aprano wrote: > On Thu, Sep 29, 2016 at 09:43:57PM -0500, boB Stepp wrote: >> But why does Python require >> separating the function object from its parameters when it is being >> passed as an argument to another function? > > If you pass the function arguments to the function *first*, then it gets > evaluated before the second function gets to see it. Let's investigate: > > def g(*args): > print("Calling function g with arguments {}".format(args)) > return 999 > > def f(func, *args): > print("f received first argument: {}".format(func)) > print("f received additional arguments: {}".format(args)) > return func(*args) > > > Let's try it the right way: > > py> f(g, 1, 2, 3) > f received first argument: > f received additional arguments: (1, 2, 3) > Calling function g with arguments (1, 2, 3) > 999 > > > > And the wrong way: > > py> f(g(1, 2, 3)) > Calling function g with arguments (1, 2, 3) > f received first argument: 999 > f received additional arguments: () > Traceback (most recent call last): > File "", line 1, in > File "", line 4, in f > TypeError: 'int' object is not callable > > > > Does this help? Yes. Thanks, Steve! Very helpful examples. -- boB