Re: [Tutor] Is Python useful for emulating?
It probably won't be fast enough. I wrote an NES emu in python and I ran into a lot of performance issues. If you're new to emu scene, start with David winters' doc and write a chip-8 emu. I will help you learn the structure and process of writing an emulator. Then reimplememt it in c afterward. I'm actually in th midst of writing an introductory emulation ebook that follows a similar course. Another consideration that I will explore myself is languages like rpython or Pyrex that are similar to python but can be compiled down for speed. Might be worth a look for you too. Good luck, and if you need any mentorship or have any questions about starting out writing emulators just drop me a line! - Sent from a mobile device with a bad e-mail client. - On Nov 27, 2010, at 8:59 PM, Mauricio Alejandro alexandert...@hotmail.com wrote: I never created any emulator before, and i'm learning C++. Let's say i try to write an emulator for... SNES. Would Python be fast enough? Also, any useful advice you can give me? Things i should know about before i start coding? ___ 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] Is Python useful for emulating?
Mauricio Alejandro, 28.11.2010 03:59: I never created any emulator before, and i'm learning C++. Let's say i try to write an emulator for... SNES. Would Python be fast enough? Also, any useful advice you can give me? Things i should know about before i start coding? If you want to write something like this from scratch, I would suggest you look at two tools: pygame and Cython. pygame is a very useful tool for writing graphics games. Cython is a way to speed up performance critical Python code by translating it to fast C code. You shouldn't run into that many performance problems if you use both, but you'd likely be able to write your code a lot quicker than in C or C++. One important advice: write your code in Python first, then benchmark it and move only the performance critical parts of it into Cython. The higher level you stay, the simpler your code will remain. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] string case manipulation in python 3.x
Rance Hall wrote: I need to do some case manipulation that I don't see in the documented string functions. I want to make sure that user input meets a certain capitalization scheme, for example, if user input is a name, then the first letter of each word in the name is upper case, and the rest are lower. I know how to force the upper and lower cases with string.lower() and friends. Then you should know about str.capitalize and str.title: s = joHN clEeSE s.capitalize() 'John cleese' s.title() 'John Cleese' It looks like you want str.title, with the exception that numeric suffixes will need to be handled specially: fred smith III.title() 'Fred Smith Iii' But see below. and I could even do a string.split() on the spaces in the names to break the name into pieces. I don't see an obvious way to do this. what I've come up with so far is to do something like this. break the name into pieces force each piece to be lower case replace the first letter in each word with a uppercase version of whats there already. Problems with this approach as I see them: The built in split function will create undesirable results for names that contain suffixes like Jr. etc. Why? I don't see how it would, unless people leave out the space between their name and suffix. But then, if they leave out the space between their first name and surname, you have the same problem. I'm not entirely sure how to replace the string with an uppercase first letter on a per word basis. The right way is with the capitalize or title methods, but the generic way is illustrated by this similar function: def reverse_capitalize(s): # abcd - aBCD if s: return s[0].lower() + s[1:].upper() else: # empty string return s Which brings back to the issue of Roman number suffixes. You can deal with them like this: def capitalize(s): s = s.title() words = s.split() suffixes = 'ii iii iv v vi vii viii ix x xi xii'.split() # Twelve generations should be enough. If not, add more. if words[-1].lower() in suffixes: words[-1] = words[-1].upper() return .join(words) return s Whats the right way to do something like this? Another approach would be the old-fashioned way: change the string character by character. # change in place letters = list(name) for i, c in letters: letters[i] = do_something_with(c) name = ''.join(letters) -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Pyserial and invalid handle
Can anybody tell me why the handle below is invalid? I'm running Win7. TIA, John Python 2.7 (r27:82525, Jul 4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on win32 Type copyright, credits or license() for more information. import serial ser = serial.Serial('com1', timeout = 5) x = ser.read() Traceback (most recent call last): File pyshell#2, line 1, in module x = ser.read() File E:\Python27\lib\site-packages\serial\serialwin32.py, line 236, in read raise SerialException(ReadFile failed (%s) % ctypes.WinError()) SerialException: ReadFile failed ([Error 6] The handle is invalid.) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] A regular expression problem
I'm trying to use regular expressions to extract strings that match certain patterns in a collection of texts. Basically these texts are edited versions of medieval manuscripts that use certain symbols to mark information that is useful for filologists. I'm interested in isolating words that have some non alpha-numeric symbol attached to the beginning or the end of the word or inserted in them. Here are some examples: '¿de' ,'«orden', '§Don', '·II·', 'que·l', 'Rey»' I'm using some modules from a package called NLTK but I think my problem is related to some misunderstanding of how regular expressions work. Here's what I do. This was just a first attempt to get strings starting with a non alpha-numeric symbol. If this had worked, I would have continued to build the regular expression to get words with non alpha-numeric symbols in the middle and in the end. Alas, even this first attempt didn't work. - with open('output_tokens.txt', 'a') as out_tokens: with open('text.txt', 'r') as in_tokens: t = RegexpTokenizer('[^a-zA-Z\s0-9]+\w+\S') output = t.tokenize(in_tokens.read()) for item in output: out_tokens.write( %s % (item)) What puzzles me is that I get some results that don't make much sense given the regular expression. Here's some excerpt from the text I'm processing: --- filename=B-05-Libro_Oersino__14-214-2.txt %Pág. 87 L-[LIBRO VII. DE OÉRSINO]L+ // §Comeza el ·VII· libro, que es de Oérsino las bístias. // §Canto Félix ha tomado prenda del phisoloffo, el […] ·II· hómnes, e ellos Here's the relevant part of the output file ('output_tokens.txt'): -- filename= -05- _Oersino__14- -2. %Pág. L- [LLIBRO ÉRSINO] L+ §Comenza ·VII· ístias. §Canto élix ·II· ómnes --- If you notice, there are some words that have an accented character that get treated in a strange way: all the characters that don't have a tilde get deleted and the accented character behaves as if it were a non alpha-numeric symbol. What is going on? What am I doing wrong? Josep M. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] temporarily modifying sys.path
I need a function that will import a module (using __import__) from only one specific location on my filesystem. Name collisions are possible. To avoid this I could *temporarily* modify sys.path for the operation so that it contains only the path that I want to work from. console example: sys_path = sys.path sys.path = [/cgi-bin/libraries/python] sys.path ['/cgi-bin/libraries/python'] sys.path = sys_path sys.path ## original sys.path I don't want to create something that could bite me later. Documentation on sys.path that I have found does not address *removing* items from sys.path or completely changing it. Any caveats or comments on this procedure? TIA -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] temporarily modifying sys.path
I need a function that will import a module (using __import__) from only one specific location on my filesystem. Name collisions are possible. To avoid this I could *temporarily* modify sys.path for the operation so that it contains only the path that I want to work from. Just curious, but could the imp module help you? imp.find_module appears to look on a specific path if you specify it, and it seems you have the path available here. From there imp.load_module perhaps. I have very little experience with imp, but I'm wondering if it could help you without the necessity of modifying sys.path. Though I do think you can temporarily make a copy of sys.path, then modify sys.path, then after the import reassign sys.path to your temp variable. But try the above route first. Evert console example: sys_path = sys.path sys.path = [/cgi-bin/libraries/python] sys.path ['/cgi-bin/libraries/python'] sys.path = sys_path sys.path ## original sys.path I don't want to create something that could bite me later. Documentation on sys.path that I have found does not address *removing* items from sys.path or completely changing it. Any caveats or comments on this procedure? TIA -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.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] Pyserial and invalid handle
On 11/28/2010 7:55 AM John Smith said... Can anybody tell me why the handle below is invalid? I'm running Win7. TIA, John Python 2.7 (r27:82525, Jul 4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on win32 Type copyright, credits or license() for more information. import serial ser = serial.Serial('com1', timeout = 5) What do you get when you add 'print ser' here? The docs at http://pyserial.sourceforge.net/shortintro.html show you should get something like Serialid=0xa81c10, open=False(port='COM1', baudrate=19200, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=0, rtscts=0) So if you're getting an invalid handle error I'd expect you'd get something different. Emile x = ser.read() Traceback (most recent call last): File pyshell#2, line 1, in module x = ser.read() File E:\Python27\lib\site-packages\serial\serialwin32.py, line 236, in read raise SerialException(ReadFile failed (%s) % ctypes.WinError()) SerialException: ReadFile failed ([Error 6] The handle is invalid.) ___ 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] A regular expression problem
snip intro Here's what I do. This was just a first attempt to get strings starting with a non alpha-numeric symbol. If this had worked, I would have continued to build the regular expression to get words with non alpha-numeric symbols in the middle and in the end. Alas, even this first attempt didn't work. - with open('output_tokens.txt', 'a') as out_tokens: with open('text.txt', 'r') as in_tokens: t = RegexpTokenizer('[^a-zA-Z\s0-9]+\w+\S') output = t.tokenize(in_tokens.read()) for item in output: out_tokens.write( %s % (item)) What puzzles me is that I get some results that don't make much sense given the regular expression. Here's some excerpt from the text I'm processing: --- filename=B-05-Libro_Oersino__14-214-2.txt %Pág. 87 L-[LIBRO VII. DE OÉRSINO]L+ // §Comeza el ·VII· libro, que es de Oérsino las bístias. // §Canto Félix ha tomado prenda del phisoloffo, el […] ·II· hómnes, e ellos Here's the relevant part of the output file ('output_tokens.txt'): -- filename= -05- _Oersino__14- -2. %Pág. L- [LLIBRO ÉRSINO] L+ §Comenza ·VII· ístias. §Canto élix ·II· ómnes --- If you notice, there are some words that have an accented character that get treated in a strange way: all the characters that don't have a tilde get deleted and the accented character behaves as if it were a non alpha-numeric symbol. What is going on? What am I doing wrong? I don't know for sure, but I would hazard a guess that you didn't specify unicode for the regular expression: character classes like \w and \s are dependent on your LOCALE settings. A flag like re.UNICODE could help, but I don't know if Regexptokenizer accepts that. It would also appear that you could get a long way with the builtin re.split function, and supply the flag inside that function; no need then or Regexptokenizer. Your tokenizer just appears to split on the tokens you specify. Lastly, an output convenience: output.write(' '.join(list(output))) instead of the for-loop. (I'm casting output to a list here, since I don't know whether output is a list or an iterator.) Let us know how if UNICODE (or other LOCALE settings) can solve your problem. Cheers, Evert ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] temporarily modifying sys.path
* Evert Rol evert@gmail.com [101128 07:56]: I need a function that will import a module (using __import__) from only one specific location on my filesystem. Name collisions are possible. To avoid this I could *temporarily* modify sys.path for the operation so that it contains only the path that I want to work from. Just curious, but could the imp module help you? imp.find_module appears to look on a specific path if you specify it, and it seems you have the path available here. From there imp.load_module perhaps. I have very little experience with imp, but I'm wondering if it could help you without the necessity of modifying sys.path. Though I do think you can temporarily make a copy of sys.path, then modify sys.path, then after the import reassign sys.path to your temp variable. But try the above route first. I'll be darned. I never even heard of that module, but I just did an import and looked at the docs. I *will* give that a try. Thanks for the tip! cheers -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] temporarily modifying sys.path
Tim Johnson t...@johnsons-web.com wrote Just curious, but could the imp module help you? imp.find_module I'll be darned. I never even heard of that module, but I just did an import and looked at the docs. I *will* give that a try. I think its new in Python v3... Compare my recent post about reload() - now a part of imp... Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] REport Card Question
Write a code that will take an input from a user (numerical grade) and convert their numerical grade into a letter grade that is accompanied by a “smart” statement. def grade_score(grade): if grade =95 and grade = 100: print 'A+, Excellent' elif grade =85 and grade 95: print 'A, Good Work' elif grade =80 and grade 85: print 'A-, Good Work, but you could do better' elif grade =75 and grade 80: print 'B, Try Harder' elif grade =70 and grade 75: print 'B-, Try Harder' elif grade =65 and grade 70: print 'C, Work Harder' elif grade =60 and grade 65: print 'C-, Work Harder' elif grade =55 and grade 60: print 'D, Study Harder' elif grade =50 and grade 55: print 'D-, Study Harder' elif grade =0 and grade 50: print 'F, You Failed' else: print You did not enter an appropriate value, please run the program again! grade = raw_input('Put your grade here:’) grade_score() Put your grade here:77 Traceback (most recent call last): File /Users/andre.jeyarajan/Documents/workspace/Chapter 5 Problems/src/ReportCardQuestion.py, line 28, in module grade_score() TypeError: grade_score() takes exactly 1 argument (0 given) Can you help and tell me why it doesn’t work? Thanks___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] REport Card Question
On Sun, Nov 28, 2010 at 6:50 PM, Andre Jeyarajan andrejeyara...@rogers.comwrote: Write a code that will take an input from a user (numerical grade) and convert their numerical grade into a letter grade that is accompanied by a “smart” statement. def grade_score(grade): if grade =95 and grade = 100: print 'A+, Excellent' elif grade =85 and grade 95: print 'A, Good Work' elif grade =80 and grade 85: print 'A-, Good Work, but you could do better' elif grade =75 and grade 80: print 'B, Try Harder' elif grade =70 and grade 75: print 'B-, Try Harder' elif grade =65 and grade 70: print 'C, Work Harder' elif grade =60 and grade 65: print 'C-, Work Harder' elif grade =55 and grade 60: print 'D, Study Harder' elif grade =50 and grade 55: print 'D-, Study Harder' elif grade =0 and grade 50: print 'F, You Failed' else: print You did not enter an appropriate value, please run the program again! grade = raw_input('Put your grade here:’) grade_score() Put your grade here:77 Traceback (most recent call last): File /Users/andre.jeyarajan/Documents/workspace/Chapter 5 Problems/src/ReportCardQuestion.py, line 28, in module grade_score() This line tells you why it doesn't work: TypeError: grade_score() takes exactly 1 argument (0 given) Are you familiar with functions? When you define the function you define it as taking one parameter: def grade_score(grade): # Code here but then when you call it: grade_score() you don't give it any parameters (or arguments). The traceback tells you that you need to provide it 1 argument and that you gave it 0 instead. If you called grade_score(3, 150) you would get a similar error, only it would say (2 given) instead. HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Temperature Scales
Write two functions that will convert temperatures back and forth from the Celsius and Fahrenheit temperature scales (using raw_input) def C_F(x): y = (x-32)*(5.0/9) print y def F_C(x): y = (x*9.0/5)+32 print y I have created the two functions but I don’t know what to do from here. Can you help? Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pyserial and invalid handle
John, On 28 November 2010 15:55, John Smith jocj...@verizon.net wrote: Python 2.7 (r27:82525, Jul 4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on win32 Type copyright, credits or license() for more information. import serial ser = serial.Serial('com1', timeout = 5) x = ser.read() Traceback (most recent call last): File pyshell#2, line 1, in module x = ser.read() File E:\Python27\lib\site-packages\serial\serialwin32.py, line 236, in read raise SerialException(ReadFile failed (%s) % ctypes.WinError()) SerialException: ReadFile failed ([Error 6] The handle is invalid.) Ugh, you're probably not going to like this. I've done some googling and it appears this may be a 64-bit issue with the ctypes module... apparently 64-bit ctypes can only import 64-bit libraries. See here: http://ur.ly/vSMQ Then again, it also seems to be an open issue on the PySerial bug tracker: http://ur.ly/vZNL Note, the above ticket suggests that PySerial 2.4 works ok (impliedly even on 64-bit XP, so I imagine also on Windows 7.) You should be able to try this out by downloading the 2.4 version instead and installing it in the same way you did the 2.5 version. In any case, it might be an idea to post a report/add a comment to that bug report as well to maybe help get this issue resolved. Cheers, Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Pyserial and invalid handle
Also note this link: http://ur.ly/vVU9 It confirms that PySerial 2.4 works fine on Windows 7 64 bit. (I've also just downloaded and checked that installing pyserial 2.4 replaces the existing pyserial and it does on my Python installation.) Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Temperature Scales
On 11/28/2010 8:33 PM, Andre Jeyarajan wrote: Write two functions that will convert temperatures back and forth from the Celsius and Fahrenheit temperature scales (using raw_input) def C_F(x): y = (x-32)*(5.0/9) print y def F_C(x): y = (x*9.0/5)+32 print y I have created the two functions but I don’t know what to do from here. Can you help? Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor I get the curious feeling this is homework. You would need to do one of two things: Pass the output of raw_input as an argument or rewrite your functions so that instead of taking the temperature as a parameter, it asks in the body of the function for a temperature. In either case, you will run into a little TypeError, easily fixed however. HTH, ~Corey Richardson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] temporarily modifying sys.path
* Alan Gauld alan.ga...@btinternet.com [101128 15:17]: Tim Johnson t...@johnsons-web.com wrote Just curious, but could the imp module help you? imp.find_module I'll be darned. I never even heard of that module, but I just did an import and looked at the docs. I *will* give that a try. I think its new in Python v3... Hello Alan: Actually, I am using 2.6.5 on linux ubuntu 10.04 and imp is provided. Compare my recent post about reload() - now a part of imp... will do. thanks -- Tim tim at johnsons-web.com or akwebsoft.com http://www.akwebsoft.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and Tkinter Programming by Grayson--New Version?
On Thu, 25 Nov 2010, Alan Gauld wrote: Yves Dextraze yd...@videotron.ca wrote Sent from my iPod There is no mention on Amazon of any new editions and they usually announce several months in advance... A pity a new Tkinter book using Tix and ttk instead of PMW would be a really useful resource! Odd -- Yves's note shows up on my system as a reply in a long-dormant thread from March 2009. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python and Tkinter Programming by Grayson--New Version?
On Sun, Nov 28, 2010 at 7:18 PM, Terry Carroll carr...@tjc.com wrote: On Thu, 25 Nov 2010, Alan Gauld wrote: Yves Dextraze yd...@videotron.ca wrote Sent from my iPod There is no mention on Amazon of any new editions and they usually announce several months in advance... A pity a new Tkinter book using Tix and ttk instead of PMW would be a really useful resource! Odd -- Yves's note shows up on my system as a reply in a long-dormant thread from March 2009. i agree with alan's sentiment though. perhaps someone just brought up a server recently (where this msg was in the outbox)? -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Python Web Development with Django, Addison Wesley, (c) 2009 http://withdjango.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor