Re: [Tutor] Coursera Python Course starts today
On 20/08/13 03:02, Leam Hall wrote: https://class.coursera.org/programming1-002/class/index Leam Leam, your link for some reason redirects on a default coursera page, probably cause I'm not enlisted. I think a better link for the course would be https://www.coursera.org/course/programming1 so that others interested may find it. Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] short url processor
On 05/14/2011 03:49 AM, ian douglas wrote: for i in giant_list: if i[0]: if i[1]: mc.set(i[0], i[1]) Until Alan comes with a more round answer, I'd suggest something along the lines of [mc.set(x, y) for (x, y) in giant_list if x and y] I'm writing this by memory, but check list comprehension in the documentation. Anyway, there are map, reduce and such functions in python, but I think that in python 3.x you have to import them. Now, the real question would be, can you use the cursor as an iterator (but without hitting the database for each new record)? Then you can skip the worst part of loading all the values in giant_list. Just an idea for Alan and the others to answer. Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to print the installed web browser
On 09/01/2010 11:17 AM, Alan Gauld wrote: Ranjith Kumar ranjitht...@gmail.com wrote I`m using ubuntu how to find and print the installed web browsers using python scripting. How would you do it without Python scripting? Is it even possible? And on a multiuser system like Linux would you print out all the browsers installed for the current user or for all users? Alan, let me make a wild guess here. Ubuntu does have little Preferred applications config tool. I don't know how or where it stores this data, but my guess is it's the same place xdg (as in xdg-open) gets it's configuration from. This article might help http://www.crystalorb.net/mikem/xdg-settings.html Quote from above article (near the end): ...especially as there is (currently) no single, portable, authoritative way for applications to query the values of these settings... Ranjith, get ready for some configuration file parsing. But if you just want to open a url with the default browser, you can just execute xdg-open your-url as a subprocess. Hope I shifted you to the right direction. Nick PS-trivia: I got to guess these just because I've read the source from import antigravity ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to print the installed web browser
On 09/01/2010 11:46 AM, Nick Raptis wrote: Alan, let me make a wild guess here. Ubuntu does have little Preferred applications config tool. I don't know how or where it stores this data, but my guess is it's the same place xdg (as in xdg-open) gets it's configuration from. This article might help http://www.crystalorb.net/mikem/xdg-settings.html Quote from above article (near the end): ...especially as there is (currently) no single, portable, authoritative way for applications to query the values of these settings... Ranjith, get ready for some configuration file parsing. But if you just want to open a url with the default browser, you can just execute xdg-open your-url as a subprocess. Hope I shifted you to the right direction. Nick PS-trivia: I got to guess these just because I've read the source from import antigravity Ooops! Sorry if I caused any confusion, I thought the goal was to print the default browser, not all of the installed ones. Silly me. Still, the Preferred applications tool seems to know that info (so to give you a choice) so it might be something to dig into. Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] input problem
On 08/22/2010 09:35 PM, Roelof Wobben wrote: Hello, I made this programm : def count_letters(n,a): count = 0 for char in n: if char == a: count += 1 return count fruit= letter= fruit= input(Enter a sort of fruit: ) teller = input(Enter the character which must be counted: ) x=count_letters (fruit,letter) print De letter, letter , komt, x , maal voor in het woord, fruit The problem is that I can't have the opportuntity for input anything. I use python 2.7 on a Win7 machine with as editor SPE. Roelof You are using Python 2.x, so you should use raw_input() instead of input(). Note that in Python 3.x, raw_input() has been renamed to just input(), with the old 2.x input() gone. Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] input problem
On 08/22/2010 09:35 PM, Roelof Wobben wrote: Hello, I made this programm : def count_letters(n,a): count = 0 for char in n: if char == a: count += 1 return count fruit= letter= fruit= input(Enter a sort of fruit: ) teller = input(Enter the character which must be counted: ) x=count_letters (fruit,letter) print De letter, letter , komt, x , maal voor in het woord, fruit The problem is that I can't have the opportuntity for input anything. I use python 2.7 on a Win7 machine with as editor SPE. Roelof Also, you have a typo in your code (teller instead of letter) so fix that too :) A couple more comments: - You don't have to initialize fruit and letter. Doesn't make sense in this context. - Although I can see you made the count_letters() function as an exercise, there is a build in method of string that accomplishes the same effect. Try x=fruit.count(letter) - Give a bit of thought about what would happen if someone enters a whole string instead of a character for letter. How should your program accommodate that? Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] input problem
Please try and reply to the list instead of just me. raw_input did not the trick. fruit.count is the next exercise. Oke, I deleted the initialazion and change teller into letter. Roelof Should be alright now.. Hmmm Can you paste your exact code AND the error you're getting? As I understand you never get the prompts, right? Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problem with simple script
On 07/28/2010 02:51 PM, Richard D. Moores wrote: I have a practical need for a script that will give me a random int in the closed interval [n, m]. Please see http://tutoree7.pastebin.com/xeCjE7bV. This works fine when I enter both n and m as, for example, 23, 56, or even 56, 23. But often the closed interval is [1, m], so I'd like to not have to enter the 1 in those cases, and just enter, say, 37 to mean the interval [1, 37]. Highlighted lines 9-11 are my attempt to do this, but it fails. This seems like it should be so simple to do, but it isn't not for me. Advice, please. Thanks, Dick Moores Split the input before the if. Fork based on the length of the resulting list. :) Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] String replace question
On 07/28/2010 03:41 PM, Rod wrote: Hello, I need to replace a period (.) in a domain name with a slash period (\.). I'm attempting to use the string replace method to do this. Example: uri = domain.com uri.replace('.', '\.') This returns 'domain\\.com' Of course it does! Try to print the value. Now the extra backslash is gone. Confused yet? Well, the backslash is an escape character in Python and other languages too. It's used for special characters like newlines ( \n ). But then how can you enter just a backslash by itself? Easy, escape it with a backslash. So \ becomes '\\' The representation under your uri.replace() line reflects that (escaped) syntax. When you're printing it though, you see the string as you meant it to. So, nothing is wrong with your lines of code , there's only one backslash there, it just get's represented as two. In fact, you should have escaped \. your self writing the line as such: uri.replace('.', '\\.') but since \. is not a special character, python is smart enough to not mind Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] nested list help
On 07/27/2010 04:48 PM, Evert Rol wrote: On the other hand, if you want to combine lists based on their first element, consider using dictionaries and extend lists which have the same key. Depending on how you create the lists (eg, when reading columns from a file), you can actually do this during creationi. I'm very much with Everet on this one. Your data would be better contained in a dictionary as such: x = {'NM100': [3, 4, 5, 6, 7, 10, 11, 12, 13], 'NM200': [15, 16, 17]} Choosing your data structure is, most of the time, as important as the rest of your code. The rest of the time, the format of your input is not something you can control so: If your data is already on a nested list, as it's on your example, you can convert them quite easily to a dictionary. You then can manipulate them with ease, and if you do want them back on nested list format, convert them back. Here's a very quick script I wrote on how to do it. Please read it through and hit me with questions on what you don't understand. - input = [['NM100', 3, 4, 5, 6, 7], ['NM100', 10, 11, 12, 13], ['NM200', 15, 16, 17]] # Convert and combine the input into a dictionary output_dict = {} for entry in input: key = entry[0] values = entry[1:] if key in output_dict: output_dict[key].extend(values) else: output_dict[key] = values print output_dict # Convert the dictionary back into a nested list output = [] for key in output_dict: entry = output_dict[key] entry.insert(0, key) output.append(entry) print output Of course, my script is very basic and it doesn't tend to a lot of things you'd might want, like eliminating double values or sorting, but it should start you on your path. Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] need help with msvcrt.getch()
On 07/27/2010 05:22 PM, Richard D. Moores wrote: Python 3.1 on Vista. Please see http://tutoree7.pastebin.com/f3TaxDBc. I'm trying to recall what I used to know, thus this simple script. But 'y' or 'q' do nothing. What's wrong? Thanks, Dick Moores Hi Dick! I'm not on Windows here so this might be a wild guess. From the documentation I gather that msvcrt can work either on binary or text mode. Perhaps you operate in the wrong one and need to switch with msvcrt.setmode(/) Anyway, insert some print lines to see what exactly is it that /getch() gets and debug from there Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Calculating and returning possible combinations of elements from a given set
On 07/28/2010 01:20 AM, ZUXOXUS wrote: Hi all pythoners I've got a probably easy to answer question. Say I've got a collections of strings, e.g.: 'man', 'bat', 'super', 'ultra'. They are in a list, or in a sequence or whatever, say a bag of words And now I want to know how many couples I can do with them, and I want the program to show me the actual couples: 'manman', 'manbat', 'mansuper', 'manultra', 'batbat', 'batman', 'batsuper', etc. But hey, why building up new words from just two strings? I also want to know the possible combinations of three words, four words, and perhaps, why not, five words. So, is it easy to do? Sorry, I'm new in programing, and am probably far from being a math-master I'm clueless, I think probably the code have some FOR I IN SEQUENCE... but then what? I don't know how to say: take every element and paste it to another one from the bag, and with another one, and with another one,... If it's too complex, I dont need the whole code recipe, just need some clues, or perhaps a useful link Thank you very much in advance! Take a look in the itertools module http://docs.python.org/library/itertools.html Check the section *Combinatoric generators: (website doesn't have an anchor link for that, search around a bit) Nick * ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Searching a text file's contents and comparing them toalist
On 07/14/2010 11:57 PM, Eric Hamiter wrote: Last question (for today, at least): Right now, the output is less than aesthetically pleasing: (['Located on aisle 1: ', 'bread', 'magazines'], ['Located on aisle 2: ', 'juice', 'ice cream'], ['Located on aisle 3: ', 'asparagus'], ['Not found in the database: ', 'butter', 'soap']) How can I format it so it looks more like this: Located on aisle 1: bread magazines Located on aisle 2 [etc...] I tried putting \n into it but it just prints the literal string. I'm sure it has to do with the list format of holding multiple items, but so far haven't found a way to break them apart. Readup on str.join() http://docs.python.org/library/stdtypes.html#str.join (link is not that helpful I admit, google some uses too) For example, print \n.join(first_run) will get you started. I think the end code you're looking for is something like output = [\n.join(run) for run in sorted_list] print \n.join(output) but I'm not sure if you've got the hang of list comprehensions by now. Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Documentation Clarification
compile(source, filename, mode[, flags[, dont_inherit]]) I see within this built in function, the first argument can be what they define as source, the second argument as the filename and the third as the mode. But what confuses me is sometimes I see a bracket, above as [, flags[, dont_inherit]]. Is this an optional argument like flags=dont_inherit? Brackets do indeed mean optional arguments. So you can do compile(source, filename, mode, flags=whatever, dont_inherit=True) or something. The nested brackets most likely show that (in your example), dont_inherit is optional, but can be used only if you (optionally) also provide the flags argument. Of course, don't take my word for it and read the rest of the description in the documentation. Also read here: http://docs.python.org/reference/introduction.html?highlight=bracketshttp://docs.python.org/reference/introduction.html?highlight=brackets#notation Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function returns 'None'
On 07/11/2010 04:59 PM, Dominik Danter wrote: Hello As en exercise I wrote the following function: def recursfac(x,carryover=1): print 'x:',x,'carryover:', carryover if x 1: carryover *= x recursfac(x-1, carryover) else: return carryover print recursfac(3) Very much to my surprise I get the following output: x: 3 carryover: 1 x: 2 carryover: 3 x: 1 carryover: 6 None Where did I go wrong? Your problem is that you expect the return to exit the recursion altogether. Instead, carryover is passed to the previous level of the recursion, which has nothing more to execute and returns None. So the first step to fix this would be to make sure that your function returns carryover no matter what: def recursfac(x,carryover=1): print 'x:',x,'carryover:', carryover if x 1: carryover *= x recursfac(x-1, carryover) return carryover else: return carryover Or simply (to remove the code duplication): def recursfac(x,carryover=1): print 'x:',x,'carryover:', carryover if x 1: carryover *= x recursfac(x-1, carryover) return carryover Now there's still one more problem. The output is this: x: 3 carryover: 1 x: 2 carryover: 3 x: 1 carryover: 6 3 Why is it returning 3 istead of 6? Well, the function didn't catch the returned carryover value on the way up, so it's just returns what it knows: the value of carryover that it self has computed. Instead, you have to do this: def recursfac(x,carryover=1): print 'x:',x,'carryover:', carryover if x 1: carryover *= x carryover = recursfac(x-1, carryover) return carryover And this returns x: 3 carryover: 1 x: 2 carryover: 3 x: 1 carryover: 6 6 Done! What you should learn from this is that, when doing recursion, figuring out what your function should do on the way up is as crucial as what you want it to do on the way down. Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function returns 'None'
On 07/11/2010 06:28 PM, Nick Raptis wrote: def recursfac(x,carryover=1): print 'x:',x,'carryover:', carryover if x 1: carryover *= x carryover = recursfac(x-1, carryover) return carryover And this returns x: 3 carryover: 1 x: 2 carryover: 3 x: 1 carryover: 6 6 Done! Also, I realized that my final code may be tough to decipher now.. A nicer way to write it would be (the functionality is still exactly the same): def recursfac(x,carryover=1): print 'x:',x,'carryover:', carryover if x 1: carryover *= x result = recursfac(x-1, carryover) else: # done with recursion, start our way up result = carryover return result ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Function returns 'None'
On 07/11/2010 06:50 PM, Luke Paireepinart wrote: I think the new version is harder to understand. Sent from my iPhone On Jul 11, 2010, at 10:43 AM, Nick Raptisairsc...@otenet.gr wrote: Aww! A critic! You humble me (really, I'm not being sarcastic here, I welcome it gladly) I won't argue about it, though. If you prefer it, the last version is still there :) My reasoning is that, with the new variable name ('result') in place, now it is evident (to me at least) that 'passover' is passed on the way down, while 'result' is passed on the way up. Harder it not, it seems more clean Also, another preference of mine, would you be kind enough to answer to the list and cc the original poster if you can? Doing it the other way around breaks my (quite stupid I admit) filters, and perhaps others' too. Thanks for the feedback. Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] newbie to gui programming
Thanks a lot for the mails all of you. Someone commented that wxpython occassionally shows it C/C++ roots. Will that haunt me cos' I have zero knowledge of C/C++. That would be me, sorry about that, didn't mean to confuse you further. Well, think it this way, if you have zero knowledge of C, you won't even notice. What about py-gtk? Is it more pythonic to learn and hence easy? Not really, every library shows it's roots every now and then. Or can I start with tkinter (and maybe remain with it), if it is easy to learn? (I liked fetchmailconf and I think it was done in tkinter). It comes bundled with python, is easy for easy tasks but 'might' be a burden as you scale up your projects. Why not? I get discouraged a bit fast, so I want the first toolset to be as easy as possible. With warm regards, -Payal Well I can relate to that. Was not so long ago when all choice seemed overwhelming and I thought that I better learn the best language/library from the start or I'll be wasting time. Truth is, it doesn't really matter this much. Start with one, anyone and for whatever reason (eg, you found a book for it) and go for it for a week. If it doesn't work for you, you'll know by then, and not a lot of energy got wasted. GUI programming is hard cause there a lot of new concepts to learn. Learning those concepts so you can apply them to any library should be your goal. Anyway, my personal story is that after trying wxPython with a book a bit I decided that GUI programming was not my thing at all, and started writing games with pygame. Many similar concepts, twice the fun. Now I'm doing web work. Give it time, trust your gut and don't panic, you'll end up right where you want :) Nick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Django Read
There actually aren't that many books on django around yet which is a pity. You should definitely read The django book: http://www.djangobook.com/en/2.0/ either on the online version on that link, or it's printed counterpart (yes, it's really the same book): http://www.amazon.com/Definitive-Guide-Django-Development-Second/dp/143021936X/ The printed one is a bit more updated (1.1) and pays off it's money because of it's great reference section :) Nick On 07/08/2010 03:48 PM, Dipo Elegbede wrote: Hi all, I have done a little basic on python and have to start working on a major django platform. I'm starting new and would like recommendations on books I can read. Kindly help me out. I want to get my hands dirty as fast as I can so that I can be part of the project. Thanks and Best regards, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Having a return when subprocess.Popen finishes
subprocess.Popen is a class, and as such it returns an object which can do a lot of stuff besides just reading the output. What you want to do here is using it's communicate() method as such: output, errors = ping.communicate() Also, there is a quicker way, I think from version 2.7 forward: use the shortcut output = subprocess.check_output(your command here) Always check latest documentation for your python version too http://docs.python.org/library/subprocess.html Nick On 07/08/2010 04:04 PM, Paul VanGundy wrote: Hi All, I'm trying to get data from subprocess.Popen. To be specific, I am trying to read a ping echo and take the output and assign it to a variable like below: ping = subprocess.Popen(ping -c 5 %s % (server), stdout=subprocess.PIPE, shell=True) However, when I run the command the output that gets assigned to my ping variable is something along the lines of 'subprocess.Popen object at 0x9524bec' and am not returned to prompt. I know that is the proper output but I need to be able to capture the ping replies and assign those to a variable. I tried adding a \r and \n at the end of my cmd. Any help would be greatly appreciated. I'm open to improvements, different ways of doing it and corrections. :) If more info is needed let me know. Thanks. /paul ___ 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] newbie to gui programming
Well, choice is a great thing! Except when you're new and all that choice seems overwhelming :) When I started out python a year ago, I knew just enough C to know that I didn't want C/C++ to be my first language that I learned. That's why I found the wxPython style a nuisance, because I was at the same time trying to learn the python way of doing things. I do see the merits you say in how wxPython does things, it just didn't work out then. In the end, I never did any GUI work because of the overwhelming choice and instead did a little pygame stuff and then got into django, mostlly because my local community did. So, db-centric seems good to me right now and will definately check dabo out, although to be honest, a webkit application on top of django sounds as good. Anyways, I'm drifting all this while away from the purpose of this list. Thank you so much for the answers, you're such a helpful bunch. Nick On 07/07/2010 10:16 AM, Alan Gauld wrote: Nick Raptis airsc...@otenet.gr wrote Really good news is that on this very list on another thread, someone suggested Dabo http://dabodev.com/ It's a python library on top of wxPython and it's database-logic-GUI But its not a complete wrapper for wxPython so you still need to revert to wxPython at some stages. Also its being database centric is great if thats what you are doing, not so great for games programming etc. But the tight coupling of wxPython to its C++ roots can be seen as a bonus because it means you can quickly use its Ruby and Perl incarnations too - and if you need to the base C++. This is one of the reasons I mainly use Tkinter - because I already knew the underlying Tk and I can use those same skills in Lisp and Ruby and Perl UI code. You pays your money and makes your choice! :-) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] differences between mmap and StringIO
Actually, for simple file operations I'd neither. Standard file usage is described here, if you haven't checked it out, which I'm sure you have http://docs.python.org/library/stdtypes.html#file-objects StringIO is useful as a buffer. That is, you make a file-like object in memory with StringIO, manipulate it as a file, and can then copy it to a real file with standard file operations. Really really useful for very intensive file operations. Pdf creation comes in mind. mmap... Well I didn't even know it existed until you mentioned it! Seems to be an advanced method of reading a file in memory and manipulating it from there with choice of whether it actually affects the physical file or not. It's help page says it is also used for communicating with subprocesses.. wow! You definately won't need that :) Anyway, files, StringIOs and mmaps are file-like objects, which means they have the same methods and functionality, so you know how to use one, you know them all from this aspect. My recommendation would be, either manipulate a file directly which is fine for most cases. Or if you really want to: open your file for reading, make a StringIO instance, copy your file to it, close the file, do whatever you want in memory, open your file again for writing, copy the StringIO to it, close both. I'd consider that overkill for most projects Is there something in particular you want to do? Nick On 07/08/2010 01:52 AM, Eduardo Vieira wrote: Hello, I'm getting confused about the usage of those 2 modules. Which should I use one to get/manipulate data from a text file? Regards, Eduardo www.express-sign-supply.com ___ 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] newbie to gui programming
Please excuse if I'm jumping on the topic. Haven't done any GUI work so this interests me too. wxPython always seemed a great choice as it works on all platforms, and uses GTK+ for linux. Well, what mainly bugs me about wxPython is that most of it's API names come from the wx C library, you almost can feel the C code underneath. I would really love a more pythonic wrapper around it. Really good news is that on this very list on another thread, someone suggested Dabo http://dabodev.com/ It's a python library on top of wxPython and it's database-logic-GUI separation looks a lot like the MVP of django, which I'm familiar with. Of course, a bit not that easy to install if you're just starting out and there are no books for it. It's also database oriented, but I consider this a plus. I'd like to hear your views on whether you think it might be a good choice for a new python programmer, exactly for the above reasons. I think it might be worth the hurdles and pay off in the end. Nick On 07/06/2010 09:48 PM, Alan Gauld wrote: There are many toolkits but these have as many similarities as differences. But none of them will be easy to learn if you have not done GUI work before because GUI programming is a whole new style and that's what takes the time. Once you learn one framework picking up another is not that hard - just a lot of new API names to learn! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] To write data in two different fonts?
Dave Angel wrote: However, if someone had tried to do that in a single call to the current function, their code would already be broken because the dictionary doesn't preserve order, so the substitution might not happen first. Wow, I never thought about the dictionary not being sorted messing things up. Thanks for the insight. Nick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] To write data in two different fonts?
Your main concern when displaying plain text in HTML is actually only 5 characters: , , , , ' which you should escape(replace) with their coresponding HTML entities. Here's a few lines of code to give you an idea how to do it: #put this somewhere in the beginning of your script, #tailor it as you like entities = {'' : 'amp;', '' : 'lt;', '' : 'gt;', '' : 'quot;', ' : 'apos;'} .. #put this in the file crunching loop #I'll use *line* as the text variable, like it would happen #in a *for line in file* scenario for character, entity in entities.items(): line = line.replace(character, entity) . . #do the rest of your stuff with the line --- That's a simple implementation and it actually performs 5 replacements on every line of your file (which is not as slow as you might think though) but should get you started if you need it. You also might want to consider escaping other characters as well (maybe spaces to preserve whitespace without a pre) Get your hands on an online HTML entity reference online. Extra points: Include a CSS file with your HTML output to handle representation. With some more lines in your script you can then code different fonts or sizes according to any logic you want. Won't go into explaining HTML though (hint: use the class HTML attribute) If it sounds more complex than you'd like, it's ok. It's more powerful and more rewarding too. Once you get the basic structure in, you can extend your script to do pretty much anything with your text. Nick Alan Gauld wrote: prasad rao prasadarao...@gmail.com wrote I put in tag pre and it worked. But at one particular line in a module the font changed colour.After that all the lines are in blue colour and under lined. How are you viewing the file? As Kent poinred out pure text files have no fonts or colours. So the blue colour must be coming from the tyool you are using to display the contents. It is interpreting sometjhing in the data as a command to switch format. Since you are using HTML it is probably a a tag. Have you checked the data for any a sequences (or and other characters for that matter)? figure out why the change of colour of font took place. Open it in a simple text editor like notepad or nano and see what it looks like. HTH, ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] To write data in two different fonts?
Dave Angel wrote: As I said, you'd probably get in trouble if any of the lines had '' or '' characters in them. The following function from the standard library can be used to escape the line directly, or of course you could use the function Nick supplied. xml.sax.saxutils.escape(/data/[, /entities/]) Escape '', '', and '' in a string of data. You can escape other strings of data by passing a dictionary as the optional /entities/ parameter. The keys and values must all be strings; each key will be replaced with its corresponding value. The characters '', '' and '' are always escaped, even if /entities/ is provided. Let us know if that doesn't do the trick. DaveA Thanks Dave for the info on xml.sax.saxutils.escape Didn't know about this one. For the rest: It is sometimes This is the source code of the xml.sax.saxutils.escape function: --- def __dict_replace(s, d): Replace substrings of a string using a dictionary. for key, value in d.items(): s = s.replace(key, value) return s def escape(data, entities={}): Escape , , and in a string of data. You can escape other strings of data by passing a dictionary as the optional entities parameter. The keys and values must all be strings; each key will be replaced with its corresponding value. # must do ampersand first data = data.replace(, amp;) data = data.replace(, gt;) data = data.replace(, lt;) if entities: data = __dict_replace(data, entities) return data - As you can see, it too uses string.replace to do the job. However, using a built-in function that works for what you want to do is preferable. It's tested and might also be optimized to be faster. It's easy and fun to look into the source though and know exactly what something does. It's also one of the ways for a begginer (me too) to progress. From the source code I can see this for example: *Don' t pass the entity dictionary I proposed earlier to this function:* entities = {'' : 'amp;', '' : 'lt;', '' : 'gt;', '' : 'quot;', ' : 'apos;'} If you pass an entity for '' into escape(), it will escape it in the already partially escaped string, resulting in chaos. Think of it, this function not checking for a '' entity passed to it might worth qualifying as a bug :) Nick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] droplet like behaviour in Python
pedro wrote: #!/usr/bin/env python # encoding: utf-8 import sys theFilePath = sys.argv[1] print theFilePath But when I try to drop something on it nothing happens. Sorry I guess there is something fundamental that I am missing. Pete Pedro, I'll reply to this message instead of the last one because, I actually tried the little script you have there, and it does work I'm on Windows though. Well, my version of your script is this: - import sys for arg in sys.argv: print arg raw_input() - The raw_input() at the end is what keeps the window open so I can see that something really happened before the window closed. When I drop a file into my script, it prints two lines: The script filename and the filename of the file I dropped. So I call it a win. I don't know how it behaves in mac, but please try and tell. (Also put your usual #!/usr/bin/env python in there to work for you) Nick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] droplet like behaviour in Python
For the Windows users out there: A google search returned me this: Make Python Scripts Droppable in Windows http://mindlesstechnology.wordpress.com/2008/03/29/make-python-scripts-droppable-in-windows/ I checked my registry and the extension was there. That must be why my drop script works. No idea how it got there though. FWIW, I have installed Python through the .msi installer. PS: What I like is that the page I linked actually has my little test script in it :D I must be doing something right. PS2: I guess I'm starting to deduce that the answer is platform/installation specific rather than python/script specific. That turns the question it to a whole new direction for me. Nick Nick Raptis wrote: pedro wrote: #!/usr/bin/env python # encoding: utf-8 import sys theFilePath = sys.argv[1] print theFilePath But when I try to drop something on it nothing happens. Sorry I guess there is something fundamental that I am missing. Pete Pedro, I'll reply to this message instead of the last one because, I actually tried the little script you have there, and it does work I'm on Windows though. Well, my version of your script is this: - import sys for arg in sys.argv: print arg raw_input() - The raw_input() at the end is what keeps the window open so I can see that something really happened before the window closed. When I drop a file into my script, it prints two lines: The script filename and the filename of the file I dropped. So I call it a win. I don't know how it behaves in mac, but please try and tell. (Also put your usual #!/usr/bin/env python in there to work for you) Nick ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] clear screen inside python interpreter
I don't think there's a Python command to do this. You can always print enough newlines to clear your view of the window: print \n * 80 or use os.system to issue a command the shell/terminal/cmd will understand import os os.system(cls) or import os os.system(clear) I resently made a 'game of life' clone and used the first method to clear the screen. It's good enough for many things! Nick Monte Milanuk wrote: Okay, simple question: is there anything similar to to 'clear' or 'cls' to clean up a console window full of commands in the python shell? Short of exiting and restarting the interpreter I'm not having a lot of luck here. Thanks, Monte ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor