Re: [Tutor] binary translator
"2face xzibit" <[EMAIL PROTECTED]> wrote > ...f you have any comments on the source code please let me know I'll add a few. > def ChooSe(): Its kind of conventional to have function names start lowercase and use uppercase for classes. Not a rule but it helps when code includes both to know wwhat the names refer to... > kN = ('n','N','no','nah') > if xit in kN: You could cover more options by just checking for the first letter being in 'nN': if xit[0] in 'nN' > chartobinary = { > 'A' : '0101', > 'B' : '0110', > ... > '9' : '00111001', wow! There are usually easier ways to create these kinds of lists programmatically. Unless you have invented your own encoding of course! - I didn't check... > def TransLate(): > TransLate() > ChooSe() > ChooSe() Calling ChooSe repeatedly like this is not a good idea, it could run into the recursion limit. You would be better to restructure the code to use a loop. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] binary translator
> > Hey i have created a program that turns a string into a binary one. But > when i began to test the program it turned out that it could not handle > some special characters (e.g ÆØÅ). Could someone please help me? These special characters have different values than those you have put into your big list. I have suggestions down below. > > p.s i am a newbie so if you have any comments on the source code please > let me know I have a couple. > here is the source code: > > #!/usr/bin/python > # -*- coding: Latin-1 -*- > import os, sys > import sys > > def ChooSe(): > print > print " encrypt " > print " quit" > print You know that you can make this one print statement if you wish? ( \n is the escape character for a new-line) print "\nencrypt\nquit" or more easily readable~ print """\ encrypt quit """ > choice = raw_input("What do you need: ") > > if choice == "encrypt": > encrypt() > elif choice == "quit": > kExit() > else: > print "Invalid choice" > ChooSe() > def kExit(): > xit = raw_input("Do you really want to quit? ") > kY = ('y','Y','yes','yah','yeah') > kN = ('n','N','no','nah') You could put those tuples directly into the if statement, it is not necessary to assign them to a variable first. I think it makes it more readable. You do not have to. if xit in ('n','N','no','nah'): > if xit in kN: > ChooSe() > elif xit in kY: > print "Goodbye!" > print > sys.exit() > else: > print "Invalid choice" > ChooSe() > def encrypt(): > > print > print " encryption " > print > > secret = raw_input('Text to encrypt: ') > code = list(secret) ^ This is unnecessary. secret is already iterable. secret[0] == code[0] > > chartobinary = { > 'A' : '0101', > 'B' : '0110', > 'C' : '0111', > 'D' : '01000100', > 'E' : '01000101', > 'F' : '01000110', > 'G' : '01000111', > 'H' : '01001000', > 'I' : '01001001', > 'J' : '01001010', > 'K' : '01001011', > 'L' : '01001100', > 'M' : '01001101', > 'N' : '01001110', > 'O' : '0100', > 'P' : '0101', > 'Q' : '01010001', > 'R' : '01010010', > 'S' : '01010011', > 'T' : '01010100', > 'U' : '01010101', > 'V' : '01010110', > 'W' : '01010111', > 'X' : '01011000', > 'Y' : '01011001', > 'Z' : '01011010', > 'a' : '0111', > 'b' : '01100010', > 'c' : '01100011', > 'd' : '01100100', > 'e' : '01100101', > 'f' : '01100110', > 'g' : '01100111', > 'h' : '01101000', > 'i' : '01101001', > 'j' : '01101010', > 'k' : '01101011', > 'l' : '01101100', > 'm' : '01101101', > 'n' : '01101110', > 'o' : '0110', > 'p' : '0111', > 'q' : '01110001', > 'r' : '01110010', > 's' : '01110011', > 't' : '01110100', > 'u' : '01110101', > 'v' : '01110110', > 'w' : '01110111', > 'x' : '0000', > 'y' : '0001', > 'z' : '0010', > ' ' : '0010', > '!' : '0011', > "\\" : '00100010', > '#' : '00100011', > '$' : '00100100', > '%' : '00100101', > '&' : '00100110', > "'" : '00100111', > '(' : '00101000', > ')' : '00101001', > '*' : '00101010', > '+' : '00101011', > ',' : '00101100', > '-' : '00101101', > '.' : '00101110', > '/' : '0010', > ':' : '00111010', > ';' : '00111011', > '<' : '0000', > '=' : '0001', > '>' : '0010', > '?' : '0011', > '@' : '0100', > '[' : '01011011', > ']' : '01011101', > '^' : '0100', > '_' : '0101', > '`' : '0110', > '{' : '0011', > '|' : '0100', > '}' : '0101', > '~' : '0110', > '€' : '1000', > '¡' : '1011', > '¢' : '10100010', > '£' : '10100011', > '¤' : '10100100', > '¥' : '10100101', > '¦' : '10100110', > '§' : '10100111', > '¨' : '10101000', > '©' : '10101001', > 'ª' : '10101010', > '«' : '10101011', > '¬' : '10101100', > '®' : '10101110', > '¯' : '1010', > '°' : '1011', > '±' : '10110001', > '²' : '10110010', > '³' : '10110011', > '´' : '10110100', > 'µ' : '10110101', > '¶' : '10110110', > '·' : '10110111', > '¸' : '10111000', > '¹' : '10111001', > 'º' : '10111010', > '»' : '10111011', > '¼' : '1000', > '½' : '1001', > '¾' : '0110', > '¿' : '1011', > 'À' : '1100', > 'Á' : '1101', > 'Â' : '1110', > 'Ã' : '1111', > 'Ä' : '11000100', > 'Å' : '11000101', > 'Æ' : '11000110', > 'Ç' : '11000111', > 'È' : '11001000', > 'É' : '11001001', > 'Ê' : '11001010', > 'Ë' : '11001011', > 'Ì' : '11001100', > 'Í' : '11001101', > 'Î' : '11001110', > 'Ï' : '1100', > 'Ð' : '1101', > 'Ñ' : '11010001', > 'Ò' : '11010010', > 'Ó' : '11010011', > 'Ô' : '11010100', > 'Õ' : '11010101', > 'Ö' : '11010110', > '×' : '11010111', > 'Ø' : '11011000', > 'Ù' : '11011001', > 'Ú' : '11011010', > 'Û' : '11011011', > 'Ü' : '11011100', > 'Ý' : '11011101', > 'Þ' : '1100', > 'ß' : '1101', > 'à' : '1110', > 'á' : '1111', > 'â' : '11100010', > 'ã' : '11100011', > 'ä' : '11100100', > 'å' : '11100101', > 'æ' : '11100110', > 'ç' : '11100111', > 'è' : '11101000', > 'é' : '11101001', > 'ê' : '11101010', > 'ë' : '11101011', > 'ì' : '11101100', > 'í' : '11101101', > 'î' : '11101110', > 'ï' : '1110', > 'ð' : '', > 'ñ' : '0001', > 'ò
[Tutor] binary translator
Hey i have created a program that turns a string into a binary one. But when i began to test the program it turned out that it could not handle some special characters (e.g ÆØÅ). Could someone please help me? p.s i am a newbie so if you have any comments on the source code please let me know here is the source code: #!/usr/bin/python # -*- coding: Latin-1 -*- import os, sys import sys def ChooSe(): print print " encrypt " print " quit" print choice = raw_input("What do you need: ") if choice == "encrypt": encrypt() elif choice == "quit": kExit() else: print "Invalid choice" ChooSe() def kExit(): xit = raw_input("Do you really want to quit? ") kY = ('y','Y','yes','yah','yeah') kN = ('n','N','no','nah') if xit in kN: ChooSe() elif xit in kY: print "Goodbye!" print sys.exit() else: print "Invalid choice" ChooSe() def encrypt(): print print " encryption " print secret = raw_input('Text to encrypt: ') code = list(secret) chartobinary = { 'A' : '0101', 'B' : '0110', 'C' : '0111', 'D' : '01000100', 'E' : '01000101', 'F' : '01000110', 'G' : '01000111', 'H' : '01001000', 'I' : '01001001', 'J' : '01001010', 'K' : '01001011', 'L' : '01001100', 'M' : '01001101', 'N' : '01001110', 'O' : '0100', 'P' : '0101', 'Q' : '01010001', 'R' : '01010010', 'S' : '01010011', 'T' : '01010100', 'U' : '01010101', 'V' : '01010110', 'W' : '01010111', 'X' : '01011000', 'Y' : '01011001', 'Z' : '01011010', 'a' : '0111', 'b' : '01100010', 'c' : '01100011', 'd' : '01100100', 'e' : '01100101', 'f' : '01100110', 'g' : '01100111', 'h' : '01101000', 'i' : '01101001', 'j' : '01101010', 'k' : '01101011', 'l' : '01101100', 'm' : '01101101', 'n' : '01101110', 'o' : '0110', 'p' : '0111', 'q' : '01110001', 'r' : '01110010', 's' : '01110011', 't' : '01110100', 'u' : '01110101', 'v' : '01110110', 'w' : '01110111', 'x' : '0000', 'y' : '0001', 'z' : '0010', ' ' : '0010', '!' : '0011', "\\" : '00100010', '#' : '00100011', '$' : '00100100', '%' : '00100101', '&' : '00100110', "'" : '00100111', '(' : '00101000', ')' : '00101001', '*' : '00101010', '+' : '00101011', ',' : '00101100', '-' : '00101101', '.' : '00101110', '/' : '0010', ':' : '00111010', ';' : '00111011', '<' : '0000', '=' : '0001', '>' : '0010', '?' : '0011', '@' : '0100', '[' : '01011011', ']' : '01011101', '^' : '0100', '_' : '0101', '`' : '0110', '{' : '0011', '|' : '0100', '}' : '0101', '~' : '0110', '€' : '1000', '¡' : '1011', '¢' : '10100010', '£' : '10100011', '¤' : '10100100', '¥' : '10100101', '¦' : '10100110', '§' : '10100111', '¨' : '10101000', '©' : '10101001', 'ª' : '10101010', '«' : '10101011', '¬' : '10101100', '®' : '10101110', '¯' : '1010', '°' : '1011', '±' : '10110001', '²' : '10110010', '³' : '10110011', '´' : '10110100', 'µ' : '10110101', '¶' : '10110110',
Re: [Tutor] Python Versions
* Tiger12506 (Fri, 14 Dec 2007 16:23:00 -0500) > > Despite what your english teacher might have tried to make you > > believe, they were wrong about the lack of a neutral in english. > > Just like ending sentences with prepositions has always been done > > and always will be done, the use of "they" to refer to someone of > > indeterminate gender has been well and alive for hundreds of > > years. > > > > The fact you think it isn't okay is because some english teacher > > sold you a line of crap about prescriptive grammar rules that > > don't actually hold true in actual writing. Many grammar books try > > to make the language into what it is not, rather than describing > > it as it is. > > No. I form my own opinions and do not believe individuals such as my > english "teachers" unless I truly believe that each one is correct. > Each of my english teachers will not tell you to use "they" or even > the masculine, instead prefering the "proper" way to be "his/her", > and equivalent forms. I personally believe this to be a waste of > time, and efficiency is one of my primary motivators. Therefore, for > space and time I use "he" for an unknown. That's common English usage: 'In languages with a masculine and feminine gender (and possibly a neuter), the masculine is usually employed by default to refer to persons of unknown gender. This is still done sometimes in English, although an alternative is to use the singular "they".'[1] > Proper english (as it is from my viewpoint) would be to restructure > the sentence so that it uses "one" in that instance. (My english > teachers would gasp at this) This makes the most logical sense. For > more than one person of unknown gender, English uses "everyone". So > for one person of unknown gender, English uses "one". No (see above). > "They" does not match plurality. Using "they" as you describe it > would mean that we should be forming sentences such as "They works" > "They bakes" "They codes in python". Clearly this does not sound > correct because the word "they" is meant to be used as a plural > pronoun only. No (see above). Thorsten [1] http://en.wikipedia.org/wiki/Grammatical_gender#Indeterminate_gender ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] upper and lower case input for file name
> "earlylight publishing" <[EMAIL PROTECTED]> wrote > >>I don't know if this'll help or not but I just learned about this: >> >> file = raw_input(info).lower > > file = raw_input(prompt).lower() # note the parens! > >> The .lower is supposed to convert any input to lower case. > > It will indeed. There is also an upper() if you want to make it > uppercase instead. And not just input either. All strings have this method. (just for those who are not aware) >>> a = "JaCoB" >>> a.lower() "jacob" >>> a.upper() "JACOB" >>> dir(a) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__ ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__g t__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__ ', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', ' __rmul__', '__setattr__', '__str__', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha', 'isdi git', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lst rip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit' , 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', ' translate', 'upper', 'zfill'] >>> Just look at all the possibilities! ;-) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python logging module: two handlers writing to the samefile - okay?
- Original Message - From: "Hans Fangohr" <[EMAIL PROTECTED]> To: Sent: Saturday, December 15, 2007 1:43 AM Subject: [Tutor] python logging module: two handlers writing to the samefile - okay? > I have an example program (test.py) and the logging configuration file > (log.conf) attached below (they are also available at > http://www.soton.ac.uk/~fangohr/geheim/two_handlers_one_file) > (ii) in particular, it appears we have two filehandlers that write to > the same file (in mode 'a+'). While this seems to work fine in the > examples I have tested, I'd like some independent advice on whether > this is 'legal' (or whether it works by chance). > > (I have seen this not working when both files are opened with mode > 'w'.) I think this makes sense. 'w' mode automatically blanks out the file before writing. So each time that the file is opened, whatever was in it before is gone. Also, there may be some sort of difference due to the '+' which means to open the file in update mode. What that means still makes little sense to me. I *think* it has something to do with whether the file can be read and written without different file handles. But that doesn't seem to matter in python. Does someone know about this? Maybe a new thread... Thanks, JS ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Versions
* earlylight publishing (Sat, 15 Dec 2007 09:21:45 -0800 (PST)) > No prob about the gender confusion. :-) That's why people put Firstname Lastname in the From field of their newsreader or mail reader. And please do a line break after about 70 characters. Your reply was one(!) big line. Thorsten ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] windowsfiles permissions
Found it. tested it. worked it. sgharinig it for otheers. # os.chmod(filename,stat.S_IREAD ) # print 'This page is now set to read only. Use WindowsExplorer to turn this off.' Let's me edit the terms and conditions page then set it to read only- thought others would also value a convenient way to set pages to read only once polished up to a finished state. just be sure to import stat module. And addressing the windows thread fro adayor so ago; There's gold in them dar hills, even if it is a tad smelly, unlike linux gold; gold washes off nicely. There's one heck of a lot of windows customers out there. Kirk Bailey wrote: > So, in windows, when my wiki's editor saves a file, i want to have the > option to set it to read only. In windows, how do i get python to set a > file's permissions? > -- Salute! -Kirk Bailey Think +-+ | BOX | +-+ knihT Fnord. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] upper and lower case input for file name
"earlylight publishing" <[EMAIL PROTECTED]> wrote >I don't know if this'll help or not but I just learned about this: > > file = raw_input(info).lower file = raw_input(prompt).lower() # note the parens! > The .lower is supposed to convert any input to lower case. It will indeed. There is also an upper() if you want to make it uppercase instead. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python Versions
No prob about the gender confusion. :-) I'd be willing to bet most folks around here are male so it's not unreasonable to assume. I wasn't offended, just thought I'd share in the interest of accuracy. Thanks for the kind appology anyway. Hope I haven't set off a firestorm! Message: 1 Date: Fri, 14 Dec 2007 07:43:42 -0500 From: "Tiger12506" <[EMAIL PROTECTED]> Subject: To: Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; format=flowed; charset="iso-8859-1"; reply-type=original My apologies for mistaking your gender. Because English does not have adequate neutral gender indication, I tend to use the male as such, as they do in Spanish, and perhaps many other languages. At any rate, that's how it's written in the Bible. I presumed that it was an issue with raw input because not many other things are truly different within the prompt. An extra line is necessary within the prompt after blocks of code such as classes and function declarations. (I guess this didn't bother me when I first started learning python because I got irritated when I hit enter and it didn't do anything, so I hit enter again, much more violently. ;-) My apologies also to you for assuming what was the issue. I have a knack for knowing just what goes wrong, but there are many occasions where I am wrong. :-) - Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it now.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] upper and lower case input for file name
I don't know if this'll help or not but I just learned about this: file = raw_input(info).lower The .lower is supposed to convert any input to lower case. -- Message: 4 Date: Fri, 14 Dec 2007 11:14:13 -0500 From: "Bryan Fodness" <[EMAIL PROTECTED]> Subject: [Tutor] upper and lower case input for file name To: tutor-python Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset="iso-8859-1" Is there an easy way that an input can be upper or lower case? The file name is TEST.TXT, and I get. - Enter File (if not local, enter path):test.txt Traceback (most recent call last): File "test.py", line 52, in for line in open(file) : IOError: [Errno 2] No such file or directory: 'test.txt' - This is a non-issue on Windows, but now I have migrated to Ubuntu. Bryan - Looking for last minute shopping deals? Find them fast with Yahoo! Search.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] user-given variable names for objects
Tiger12506 wrote: >> Mmm, to nit-pick a little, dictionaries are iterables, not iterators. They >> don't have a next() method. > > I'm a little fuzzy on the details of that, I will have to look over some > reference material again. An iterable is something that produces an iterator when you call iter() on it. An iterator has a next() method. More here: http://personalpages.tds.net/~kent37/kk/4.html > Oh! This is new to me! When did they put in itervalues? See what happens > when you leave the python community so you can learn C and assembly~ Python 2.2, when explicit iterators were introduced. >> and of course if you care about efficiency you should hoist the call to >> time.time() out of the loop! > > Oh, I thought it was necessary for the time to change within the loop which > might or might not be necessary Yes, the functionality is slightly different if the time() call is hoisted out of the loop. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] user-given variable names for objects
> Mmm, to nit-pick a little, dictionaries are iterables, not iterators. They > don't have a next() method. I'm a little fuzzy on the details of that, I will have to look over some reference material again. >> [a for a in eventData if eventData[a] < time.time()] >> >> This is more efficient. The keys method creates a list in memory first >> and then it iterates over it. > > I've never tested it but I suspect that when you need keys and values, it > is more efficient to use itervalues(): > > [ k for k, v in eventData.itervalues() if v < time.time() ] Oh! This is new to me! When did they put in itervalues? See what happens when you leave the python community so you can learn C and assembly~ > and of course if you care about efficiency you should hoist the call to > time.time() out of the loop! Oh, I thought it was necessary for the time to change within the loop which might or might not be necessary, depending on just what exactly is supposed to happen. I would guess that in an unordered data structure like that, it wouldn't be of any use to update the time within the loop. Ask me if you are curious and can't follow my reasoning. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python logging module: two handlers writing to the same file - okay?
Ricardo Aráoz wrote: > Kent Johnson wrote: >> I don't know the answer, but it has nothing to do with the logging >> module. The question is, can the same file reliably be opened twice for >> writing in the same module. > > Well, the question would actually be if the logging module is smart > enough to find out that both your filehandlers are referring to the same > file and open it only once. A quick glance at the logging module shows that it is not that smart. It just opens the file twice; hence my original question. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python logging module: two handlers writing to the same file - okay?
Kent Johnson wrote: > Hans Fangohr wrote: > >> (i) is this (as in the log.conf file) the right use of the logging >> module to achieve what I need? > > I think you understand the module correctly. > >> (ii) in particular, it appears we have two filehandlers that write to >> the same file (in mode 'a+'). While this seems to work fine in the >> examples I have tested, I'd like some independent advice on whether >> this is 'legal' (or whether it works by chance). > > I don't know the answer, but it has nothing to do with the logging > module. The question is, can the same file reliably be opened twice for > writing in the same module. > > Another option: If you configure logging in code, you could create two > StreamHandlers that log to the same file - open the file yourself and > pass it to both handlers. If you do this you will have to close the file > yourself somehow. > > Kent > Well, the question would actually be if the logging module is smart enough to find out that both your filehandlers are referring to the same file and open it only once. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] windowsfiles permissions
So, in windows, when my wiki's editor saves a file, i want to have the option to set it to read only. In windows, how do i get python to set a file's permissions? -- Salute! -Kirk Bailey Think +-+ | BOX | +-+ knihT Fnord. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] python logging module: two handlers writing to the same file - okay?
Hans Fangohr wrote: > (i) is this (as in the log.conf file) the right use of the logging > module to achieve what I need? I think you understand the module correctly. > (ii) in particular, it appears we have two filehandlers that write to > the same file (in mode 'a+'). While this seems to work fine in the > examples I have tested, I'd like some independent advice on whether > this is 'legal' (or whether it works by chance). I don't know the answer, but it has nothing to do with the logging module. The question is, can the same file reliably be opened twice for writing in the same module. Another option: If you configure logging in code, you could create two StreamHandlers that log to the same file - open the file yourself and pass it to both handlers. If you do this you will have to close the file yourself somehow. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor