[Tutor] telnetlib - character hex00 missing
Even though my non-python telnet-server on the other side is sending #00-bytes, they are not recognized by python's telnetlib (characters #01-#FF seem to work fine though). My C++ implementation has no problems with this. I have to use Python 3.1 on Windows. I'm guessing this a known bug. What is the workaround? :( # Receiver tn = telnetlib.Telnet() tn.open(ip, port) while 1: response = (tn.read_until(b\r,20))[1:-1] if response.find(bytes.fromhex(00)) -1: print (There are hex00 characters) else: print (No hex00 characters found) tn.close -- Neu: GMX De-Mail - Einfach wie E-Mail, sicher wie ein Brief! Jetzt De-Mail-Adresse reservieren: http://portal.gmx.net/de/go/demail ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lambda function, was: Simple counter to determine frequencies of words in adocument
Thanks Alan and Emile, By the way, I know what a lambda function is and I read about the key parameter in sorted() but I don't understand very well what key=lambda item: item[1] does. ... What I don't understand is the syntax of item : item[1]. ... Alan says: So reverse engineering that we get lambda item: item[1] item is the parameter of the anonymous function. and item[1] is the return value. (The colon separates parameter from return value) Emile says: Key defines a lambda function that accepts as a single passed parameter named item and returns the element in position [1] thereof. OK, the explanation is very clear. I guess what confuses me here is the use of the term 'item' and here I'm going to reveal my greenness in Python and programming in general. So, I know the term 'item' is used in general to refer to the different elements that are part of a collection. Thus an item in a dictionary would be a dictionary entry of the type {x:y}, an item in a list would be any of the elements that can be a member of a list (a digit, a string, another list, etc.). In this case, an item would be a tuple of the form (word, x) and so item[1] refers to x. Since the key parameter indicates which term is supposed to be used for comparison in the sorting, in this case the result will be that the sorting will be done according to the digits in that position. So, that much is clear. What I don't understand is the nature of the term 'item'. Is it a variable? Is it some kind of built-in term (it cannot be a built-in data type since it refers to elements of different data types)? I checked the Python books I have and did some googling and the only uses I find of 'item' are generic uses where it is clear that 'item' refers generically to members of some collection but it does not look like it is some built-in name in Python. I mean, since the first time 'item' is mentioned is within the lambda function, how does Python know that item[1] refers to the second position of the tuple? If it is a variable, where is it defined? I see how you get to that conclusion through human reasoning but I'm trying to understand how Python does it. Does python know because the only thing that word_table.items() contains is a list of tuples and therefore X[1] can only refer to the second position in the tuple in this particular context? Would this work if instead of item[1] the code said foobar[1]? Understanding the inner workings of Python (or programming languages in general) can help me (and anybody) become better at this. Do you see what I mean? Josep M. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] telnetlib - character hex00 missing
Joachim Roop no...@gmx.de wrote Even though my non-python telnet-server on the other side is sending #00-bytes, they are not recognized by python's telnetlib (characters #01-#FF seem to work fine though). My C++ implementation has no problems with this. I have to use Python 3.1 on Windows. I'm guessing this a known bug. What is the workaround? :( Telnetlib has been around for at least 10 years so I'd expect a bug that serious to have been fixed long ago! I suspect something else is at fault. # Receiver tn = telnetlib.Telnet() tn.open(ip, port) while 1: Aside: Python idiom nowadays prefers while True: to while 1: response = (tn.read_until(b\r,20))[1:-1] It might be worth breaking this line down and printing the results data = tn.read_until(b\r,20)# did telnet even read the 00? response = data[1:-1] # did we strip it correctly? if response.find(bytes.fromhex(00)) -1: print (There are hex00 characters) else: print (No hex00 characters found) tn.close Others may have more info, but I'll be surprised if telnetlib can't read a '00'. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] IDEs
Alan gave me this piece of advice in his response to another message I sent to the list. Since the topic is a bit different from the one in the original message, I think it is better to start a different thread. Don;t run your code inside the IDE except for testing. IDEs are Development Environments, they are not ideal for executing production code. Run your file from the Terminal command prompt directly. I thought the code was not run inside the IDE but it was run by Python independently, that is, the IDE just provides an interface. When I run code from within Eclipse (the IDE I'm starting to use) I can see a Python process in my process monitor. The process for Eclipse is consuming very little CPU. So, what is the advantage of running the file from Terminal? I can see only disadvantages: a) it is slower since I have to type $python filename.py (ok, this takes very little time but clicking a button or typing a key shortcut to execute the code takes less time) b) with the terminal I don't get the debugging benefits that an IDE provides (I must say though that I still haven't really taken advantage of the benefits that Eclipse provides since the code I'm writing so far is pretty simple). JM ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Lambda function, was: Simple counter to determine frequencies of words in adocument
What I don't understand is the nature of the term 'item'. Is it a variable? Yes, its just a descriptive name. You could have used x just as easily, Python doesn't know, nor care. It is just like in defining any normal function def f(x): return x is exactly the same as: def f(data): return data data is just a different name for the input parameter. refers generically to members of some collection but it does not look like it is some built-in name in Python. Correct, the name 'item' has been used to be descriptive of what kind of thing the lambda is processing. In this case an item from the collection to be sorted. I mean, since the first time 'item' is mentioned is within the lambda function, how does Python know that item[1] refers to the second position of the tuple? If it is a variable, where is it defined? When you specify a parameter you create a variable. def f(data): return data creates a local variable called data within my function f. to understand how Python does it. Does python know because the only thing that word_table.items() contains is a list of tuples No, it knows because you defined the variable 'item' before the colon lambda item : item[1] So Python looks at the name(s) before the colon and uses them within the expression following the colon. You can have multiple names (or none) before the colon, try: f = lambda : 6# no parameters f() 6 g = lambda a,b : a+b# two parameters g(2,3) 5 cube = lambda sillyName: sillyName**3 cube(3) 27 this particular context? Would this work if instead of item[1] the code said foobar[1]? only if you specified foobar before the colon! Understanding the inner workings of Python (or programming languages in general) can help me (and anybody) become better at this. Do you see what I mean? Absolutely true. You may like to track down a (library?) copy of Wesley Chun's book Core Python. It does a very good job of explaining to beginners what Python is up to internally. In my opinion its the best book for those beginners who like to peer under the hood. HTH, Alan G. Author of the Learn to Program site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] telnetlib - character hex00 missing
Joachim Roop wrote: Even though my non-python telnet-server on the other side is sending #00-bytes, they are not recognized by python's telnetlib (characters #01-#FF seem to work fine though). My C++ implementation has no problems with this. I have to use Python 3.1 on Windows. I'm guessing this a known bug. What is the workaround? :( Don't guess, search. http://bugs.python.org/ # Receiver tn = telnetlib.Telnet() tn.open(ip, port) while 1: response = (tn.read_until(b\r,20))[1:-1] Is there a reason that you throw away the first and last byte being read? My wild guess is that the NULL is either the first byte, or most likely the last, and then you throw it away. if response.find(bytes.fromhex(00)) -1: It might be simpler to use a byte literal instead of calling a function to generate a NULL. if response.find(b'\x00') -1: print (There are hex00 characters) else: print (No hex00 characters found) tn.close Hope this helps. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDEs
On Mon, Nov 22, 2010 at 1:13 AM, Josep M. Fontana josep.m.font...@gmail.com wrote: Alan gave me this piece of advice in his response to another message I sent to the list. Since the topic is a bit different from the one in the original message, I think it is better to start a different thread. Don;t run your code inside the IDE except for testing. IDEs are Development Environments, they are not ideal for executing production code. Run your file from the Terminal command prompt directly. The issue is that phrase production code. If you're just noodling around, production can mean printing Hello World. If, on the other hand, you're writing Python code to solve real-world problems and the end goal is for users to install and run your applications on their own machines... then testing/running inside the IDE is an issue, because the environment is so different from anything you'll find in the wild. The most egregious case is trying to run GUI code from inside of IDLE (the basic IDE that ships with Python.) Nothing works properly, and IDLE introduces all sorts of weirdness, but people keep doing it. Testing outside the IDE doesn't have to be a big deal. You don't have to type $python filename.py every time; you could keep a terminal window open so you can just up-arrow through the command history, or (what I usually do) create a shortcut to my file on the desktop. Just make sure you've saved your changes - you generally don't have to close the IDE. I thought the code was not run inside the IDE but it was run by Python independently, that is, the IDE just provides an interface. The IDE spawns an instance of Python as a child process; it inherits the IDE's environment. That can lead to surprising behavior if you don't know what's been set in the IDE'S environment. b) with the terminal I don't get the debugging benefits that an IDE provides (I must say though that I still haven't really taken advantage of the benefits that Eclipse provides since the code I'm writing so far is pretty simple). Nobody is saying that you shouldn't debug in the IDE! Just that the IDE is not the best place to run finished code, nor is IDE-based debugging the last step in testing code that's intended to run on other people's machines. -- www.fsrtechnologies.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDEs
Josep M. Fontana wrote: Don;t run your code inside the IDE except for testing. IDEs are Development Environments, they are not ideal for executing production code. Run your file from the Terminal command prompt directly. I thought the code was not run inside the IDE but it was run by Python independently, that is, the IDE just provides an interface. When I run code from within Eclipse (the IDE I'm starting to use) I can see a Python process in my process monitor. The process for Eclipse is consuming very little CPU. I don't know about Eclipse specifically, but I understand that IDEs can often mess with the Python environment. E.g. they might change settings, add wrappers around functions to change the behaviour, install import hooks, change stdin and stdout, etc. Personally, I don't think IDEs are necessary under Linux (although Windows users might find them more useful, due to the otherwise poor development tools available). I haven't used an IDE since THINK Pascal and Hypercard in the early 1990s -- I've looked at them, but the learning curve always seems far greater than the benefit. So, what is the advantage of running the file from Terminal? I can see only disadvantages: a) it is slower since I have to type $python filename.py (ok, this takes very little time but clicking a button or typing a key shortcut to execute the code takes less time) If you're repeatedly running the file, it takes even less time to just hit UP-ARROW to get the last-used line, then ENTER to execute it. My usual working technique is: I have an editor and a terminal open. The terminal has at least two tabs open. One tab is for the shell, where I run `python filename.py`. The second tab is for running an interactive Python shell. I run the script from one tab, and see if it works: $ python filename.py After the first time, I just hit UP ARROW and ENTER. If it doesn't work, nine times out of ten I can see why just from the failure. I fix that bug in the editor, and run it again. Repeat until no more bugs. The other nine times out of ten *wink* I need to do debugging, and I swap tabs and work in my interactive Python interpreter. import filename # first time only reload(filename) # all subsequent times # test something: filename.function_that_isnt_working(some_useful_data) (I keep meaning to learn how to use the Python debugger, but honestly, I haven't needed it yet. You can go a long, long way with a few print statements.) Notice that this is a very good reason for breaking your programs up into small, single-purpose functions. It makes it simple to isolate problems into a small part of your program. Occasionally, when the environment gets too full of testing gunk, I exit the interpreter, and then immediately start it up again. Also, I'm a big believer in test-driven development. I must admit though I'm not so pedantic to write the tests first, but for anything except quick-and-dirty scripts, I make sure that *every* function in my program, without exception, has a test to ensure that it works correctly. So I'll often have a third tab open for running my test suite. I use a mix of doctests and unit-tests. Here's an example from a project I'm working on at the moment: [st...@sylar tests]$ python3 main_test.py Doctests: failed 0, attempted 80 WARNING: No example text file found. Running unit tests: ... -- Ran 255 tests in 0.491s OK No garbage found. [st...@sylar tests]$ If I see that, I know everything is working correctly (to the extent that everything is covered by tests). Each dot in the output represents one test. A dot means the test passes, E means it has an error, and F means the test failed. If there are any errors or failures, a full diagnostic is printed. Of course, it's a bit more work up front to write the tests, but: (1) it pays off a dozen times over in the long run; (2) the discipline of writing functions that are easy to test forces you to use good programming practice, avoid bad habits, and helps you design the program to be easy to maintain and debug; and (3) if you want your work to be included in the standard library, you MUST have a full test suite. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDEs
Josep M. Fontana josep.m.font...@gmail.com wrote Don;t run your code inside the IDE except for testing. IDEs are Development Environments, they are not ideal for executing production code. Run your file from the Terminal command prompt directly. I thought the code was not run inside the IDE but it was run by Python independently, that is, the IDE just provides an interface. Only partly true, the IDE wraps the interpreter so that it can catch errors and warnings and in some cases treat them differently to the way the ternminal would do it. Plus the IDE introduces a (small) overhead so you may not get accurate timings. But the biggest issues I find are that a lot of IDEs catch things like Ctrl-C interrupts and either ignore them or don;t report them accurately. Also some try to turn Pythons stack trace error reports into dialog boxes or worse, highlighted bars in the editor - losing a lot of detailed info about the bug in the process. Finally some IDEs intersperse their printed output with the interactive prompt window which makes it harder toi go bacvk and figure out what came from the program and what was test input by you! Also IDEs sometimes inherit spurious variable settings and imports that you have set during an interactive sssion. Then you get bizarre results which don't reflect the real behaviour of your code when run from a command prompt or fuile explorer (Finder in your case). a) it is slower since I have to type $python filename.py (ok, this takes very little time but clicking a button or typing a key shortcut to execute the code takes less time) You can use command history to recall the last command. Its very quick. b) with the terminal I don't get the debugging benefits that an IDE provides (I must say though that I still haven't really taken advantage of the benefits that Eclipse provides since the code I'm writing so far is pretty simple). Debug your code in the IDE to the point that it looks right. Then run it outside the IDE to make sure that it really is right! And don't try to do timing or volume load testing in the IDE - it will alter the results by a greater or lesser degree depending on how the IDE is written. And unless you know how the IDE is coded how do you know how much is the IDE and how much your code? IDEs are useful tools but thats all, development tools. After all you wouldn't really test your car's acceleration in a workshop test roller would you? You'd take it out on the road... HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Fw: Installing Pyserial for Python27 on Win 7
Forwarding to the list Please send list mail to tutor@python.org not tutor-owner... - Forwarded Message From: John Smith jocj...@verizon.net To: tutor-ow...@python.org Sent: Monday, 22 November, 2010 19:29:37 Subject: Installing Pyserial for Python27 on Win 7 My OS is Win 7. I have the 64-bit Python27 installed and it works well. I want to be able to do serial communications. I downloaded Pyserial, unfortunately in 32 bit release only, and I cannot install it. When I attempt the installation, it says No Python installation found in the registry and I cannot continue. 1) Does a 64-bit release of Pyserial exist? 2) Will the 32-bit release of Pyserial work with my OS somehow? 3) If the answer is Yes to #2, how do I get past my problem? Thanks in advance for help. Cheers, John ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fw: Installing Pyserial for Python27 on Win 7
See http://sourceforge.net/tracker/index.php?func=detailaid=2921957group_id=46487atid=446302 where it's explained that this bug won't get fixed, but that you can install from sources. Emile On 11/22/2010 1:08 PM ALAN GAULD said... Forwarding to the list Please send list mail to tutor@python.org not tutor-owner... - Forwarded Message From: John Smithjocj...@verizon.net To: tutor-ow...@python.org Sent: Monday, 22 November, 2010 19:29:37 Subject: Installing Pyserial for Python27 on Win 7 My OS is Win 7. I have the 64-bit Python27 installed and it works well. I want to be able to do serial communications. I downloaded Pyserial, unfortunately in 32 bit release only, and I cannot install it. When I attempt the installation, it says No Python installation found in the registry and I cannot continue. 1) Does a 64-bit release of Pyserial exist? 2) Will the 32-bit release of Pyserial work with my OS somehow? 3) If the answer is Yes to #2, how do I get past my problem? Thanks in advance for help. Cheers, John ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fw: Installing Pyserial for Python27 on Win 7
On 11/22/2010 3:05 PM John Smith said... Hi, Emile - Install from sources? What is that? see http://pyserial.sourceforge.net/pyserial.html#installation the From Source section. I'm not sure what else may be required but it should help get you started. Emile I searched for that phrase but did not find anything I could understand. My shortcoming, perhaps. Thanks for your reply. John On 11/22/2010 4:02 PM, Emile van Sebille wrote: See http://sourceforge.net/tracker/index.php?func=detailaid=2921957group_id=46487atid=446302 where it's explained that this bug won't get fixed, but that you can install from sources. Emile On 11/22/2010 1:08 PM ALAN GAULD said... Forwarding to the list Please send list mail to tutor@python.org not tutor-owner... - Forwarded Message From: John Smithjocj...@verizon.net To: tutor-ow...@python.org Sent: Monday, 22 November, 2010 19:29:37 Subject: Installing Pyserial for Python27 on Win 7 My OS is Win 7. I have the 64-bit Python27 installed and it works well. I want to be able to do serial communications. I downloaded Pyserial, unfortunately in 32 bit release only, and I cannot install it. When I attempt the installation, it says No Python installation found in the registry and I cannot continue. 1) Does a 64-bit release of Pyserial exist? 2) Will the 32-bit release of Pyserial work with my OS somehow? 3) If the answer is Yes to #2, how do I get past my problem? Thanks in advance for help. Cheers, John ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Fw: Installing Pyserial for Python27 on Win 7
So, let's see Serial communications does not come with Python so a separate package has to be installed. Okay, but the pyserial Windows installer will not work in Windows 7 and will not be fixed. So, a source install is required. I _assume_ the source is the one that is a tar.gz thingy. Since Windows will not handle the unpacking of that, I have to install a decompressor/unpacker to do it. Then I can finally get around to installing the serial package. Maybe. Unless I run into a similar problem because of Win 7 or because pyserial is 32-bit. I think it is better that I stop now before I install a bunch of extra applications that I need only to install one or two Python modules. I like Python itself. Very powerful. But I guess I'll look for some other language which provides the features I need without the hassle. Thanks again for your help. Cheers, John On 11/22/2010 6:18 PM, Emile van Sebille wrote: On 11/22/2010 3:05 PM John Smith said... Hi, Emile - Install from sources? What is that? see http://pyserial.sourceforge.net/pyserial.html#installation the From Source section. I'm not sure what else may be required but it should help get you started. Emile I searched for that phrase but did not find anything I could understand. My shortcoming, perhaps. Thanks for your reply. John On 11/22/2010 4:02 PM, Emile van Sebille wrote: See http://sourceforge.net/tracker/index.php?func=detailaid=2921957group_id=46487atid=446302 where it's explained that this bug won't get fixed, but that you can install from sources. Emile On 11/22/2010 1:08 PM ALAN GAULD said... Forwarding to the list Please send list mail to tutor@python.org not tutor-owner... - Forwarded Message From: John Smithjocj...@verizon.net To: tutor-ow...@python.org Sent: Monday, 22 November, 2010 19:29:37 Subject: Installing Pyserial for Python27 on Win 7 My OS is Win 7. I have the 64-bit Python27 installed and it works well. I want to be able to do serial communications. I downloaded Pyserial, unfortunately in 32 bit release only, and I cannot install it. When I attempt the installation, it says No Python installation found in the registry and I cannot continue. 1) Does a 64-bit release of Pyserial exist? 2) Will the 32-bit release of Pyserial work with my OS somehow? 3) If the answer is Yes to #2, how do I get past my problem? Thanks in advance for help. Cheers, John ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor