在消息 "Hi"中发现病毒
The message contains Unicode characters and has been sent as a binary attachment. -- http://mail.python.org/mailman/listinfo/python-list
Should I use Python for these programs?
Hi: I have considerable C and assembly language experience. However, these are mostly on embedded microcontrollers since I moved away from PC programming all the way back in 1988 :-O I wish to accomplish a few PC programming tasks, and am considering to learn Python: 1. Develop a simple GUI program to run on Linux and Windows which can send parameters and small blocks of data to an embedded microcontroller device via RS-232 or USB. Also display simple data (probably single numbers) sent from the device. Note, if it is USB, then the client will be implemented by me using FTDI chips that appear to the PC as a serial port. 2. Develop a simple vector drawing program that will allow one to freehand draw a sketch composed of a few lines, or perhaps render text in a vector form. Then sample the lines with a certain (user configurable) spacing, and use the mouse to move the sample points along the lines to tweak the sample locations if desired. Then output a file of X,Y coordinates for the samples. What is this crazy thing for? It's to develop simple lasershow vector frames. I am also designing a DSP-based lasershow output device, so the same capabilities of delivering a data payload over serial/USB to a target device will be needed here as well. I would prefer to be able to write a program that is cross-platform between Linux and Windows. I realize this might be especially problematic with the serial comms. I am also confused by the plethora of Python GUI extensions, though Tkinter seems like a likely candidate. I am uncertain if I will have difficulty learning how to use this if I don't know Tcl/Tk. Do you think Python is the right language for these projects? Any input will be appreciated. -- _ Christopher R. Carlen [EMAIL PROTECTED] SuSE 9.1 Linux 2.6.5 -- http://mail.python.org/mailman/listinfo/python-list
Re: Should I use Python for these programs?
Hendrik van Rooyen wrote: > "CC" <[EMAIL PROTECTED]> wrote:[edit] >>1. Develop a simple GUI program to run on Linux and Windows which can >>send parameters and small blocks of data to an embedded microcontroller >>device via RS-232 or USB. Also display simple data (probably single >>numbers) sent from the device. > > If you spend a bit of money on a Lantronix Xport, you can use ethernet, > and then the linux/windows problems goes away - as sockets seem to > "just work" Yeah, that's neato. Trouble is, I work at a national lab, which is where some of these apps will be developed. They have a big problem with ethernet devices. Basically, it's almost impossible to use ethernet on other than PCs, on the official LAN. I will have to look into this further though, as higher data rate stuff is a problem with serial. Although I could do 10Mbps RS-422 I suppose. >>[edit] >>I am also confused by the plethora of Python GUI extensions, though >>Tkinter seems like a likely candidate. I am uncertain if I will have >>difficulty learning how to use this if I don't know Tcl/Tk. > > No - there are excellent examples and docs available on the web. > Unless you are doing something very fancy, you dont "need" to know Tcl. > > The development cycle is fast, and if you get stuck you can get help > here - this is a super group of people (said he modestly... :- ) ) Yeah, that's really great. Our in-house programmer uses wxPython, I've discovered, so that's a strong push in that direction. Though he isn't totally unfamiliar with TkInter too. >>Do you think Python is the right language for these projects? > > Yes - I am in the process of finishing an Injection Moulding Machine > Controller, with the GUI in tkinter (cos its in the standard library), > with some crude animation of the machine functions - and it all "just works" Thanks for the input. I am not seeing anything to indicate that Python isn't the direction in which I should head. Good day! -- _ Christopher R. Carlen [EMAIL PROTECTED] SuSE 9.1 Linux 2.6.5 -- http://mail.python.org/mailman/listinfo/python-list
Re: Should I use Python for these programs?
Michele Simionato wrote: > On Jul 10, 5:09 am, CC <[EMAIL PROTECTED]> wrote: >>2. Develop a simple vector drawing program that will allow one to >>freehand draw a sketch composed of a few lines, or perhaps render text >>in a vector form. Then sample the lines with a certain (user >>configurable) spacing, and use the mouse to move the sample points along >>the lines to tweak the sample locations if desired. Then output a file >>of X,Y coordinates for the samples. > > You may look at dia for that. > >Michele Simionato Thanks for the input. Yes, I will have a look. Makes sense to check out if any drawing progs can do what I want off the shelf. But it might be fun to write one anyway! -- _ Christopher R. Carlen [EMAIL PROTECTED] SuSE 9.1 Linux 2.6.5 -- http://mail.python.org/mailman/listinfo/python-list
Re: Should I use Python for these programs?
Bjoern Schliessmann wrote: > Grant Edwards wrote: > >>Most of the graphics I do with Python is with Gnuplot (not >>really appropriate for what you want to do. >>wxWidgets/Floatcanvas might be worth looking into. > > Agreed (I'm quite sure you mean wxPython though). Also, in "wxPython > in Action" (the official book) a simple drawing app is constructed. > It could help to start from there. Ooh, that's interesting. The programming contractor at work who does all our DAQ stuff also uses wxPython so it's looking like I should use that since I can get lots of help. Thanks for the book tip. -- _ Christopher R. Carlen [EMAIL PROTECTED] SuSE 9.1 Linux 2.6.5 -- http://mail.python.org/mailman/listinfo/python-list
How to stop print printing spaces?
Hi: I've conjured up the idea of building a hex line editor as a first real Python programming exercise. To begin figuring out how to display a line of data as two-digit hex bytes, I created a hunk of data then printed it: ln = '\x00\x01\xFF 456789abcdef' for i in range(0,15): print '%.2X ' % ord(ln[i]), This prints: 00 01 FF 20 34 35 36 37 38 39 61 62 63 64 65 because print adds a space after each invocation. I only want one space, which I get if I omit the space in the format string above. But this is annoying, since print is doing more than what I tell it to do. What if I wanted no spaces at all? Then I'd have to do something obnoxious like: for i in range(0,15): print '\x08%.2X' % ord(ln[i]), This works: import sys for i in range(0,15): sys.stdout.write( '%.2X' % ord(ln[i]) ) print Is that the best way, to work directly on the stdout stream? Thanks for input. -- _ Christopher R. Carlen [EMAIL PROTECTED] SuSE 9.1 Linux 2.6.5 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to stop print printing spaces?
Roel Schroeven wrote: > CC schreef: >> ln = '\x00\x01\xFF 456789abcdef' >> # This works: >> import sys >> for i in range(0,15): >> sys.stdout.write( '%.2X' % ord(ln[i]) ) >> print >> Is that the best way, to work directly on the stdout stream? > > It's not a bad idea: print is mostly designed to be used in interactive > mode and for quick and dirty logging; not really for normal program > output (though I often use it for that purpose). > > There is another way: construct the full output string before printing > it. You can do that efficiently using a list comprehension and the > string method join(): > > >>> print ''.join(['%.2X' % ord(c) for c in ln]) > 0001FF20343536373839616263646566 Oh yeah, that's right! > In Python 2.4 and higher, you can use a generator expression instead of > a list comprehension: > > >>> print ''.join('%.2X' % ord(c) for c in ln) > 0001FF20343536373839616263646566 Hmm. I'm at 2.3 :-( > BTW, in your examples it's more pythonic not to use range with an index > variable in the for-loop; you can loop directly over the contents of ln > like this: > > import sys > for c in ln: > sys.stdout.write('%.2X' % ord(c)) > print Ah yes, duh! I played with this means of iterating over items in a sequence the other day while studying the tutorial, but it sinks in slowly for a C programmer. That's why I posted my silly code. I'll probably do that a lot until I get used to this pythonic business. Thanks for the input! -- _ Christopher R. Carlen [EMAIL PROTECTED] SuSE 9.1 Linux 2.6.5 -- http://mail.python.org/mailman/listinfo/python-list
Hex editor display - can this be more pythonic?
Hi: I'm building a hex line editor as a first real Python programming exercise. Yesterday I posted about how to print the hex bytes of a string. There are two decent options: ln = '\x00\x01\xFF 456\x0889abcde~' import sys for c in ln: sys.stdout.write( '%.2X ' % ord(c) ) or this: sys.stdout.write( ' '.join( ['%.2X' % ord(c) for c in ln] ) + ' ' ) Either of these produces the desired output: 00 01 FF 20 34 35 36 08 38 39 61 62 63 64 65 7E I find the former more readable and simpler. The latter however has a slight advantage in not putting a space at the end unless I really want it. But which is more pythonic? The next step consists of printing out the ASCII printable characters. I have devised the following silliness: printable = ' [EMAIL PROTECTED]&8*9(0)aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ\ `~-_=+\\|[{]};:\'",<.>/?' for c in ln: if c in printable: sys.stdout.write(c) else: sys.stdout.write('.') print Which when following the list comprehension based code above, produces the desired output: 00 01 FF 20 34 35 36 08 38 39 61 62 63 64 65 7E ... 456.89abcde~ I had considered using the .translate() method of strings, however this would require a larger translation table than my printable string. I was also using the .find() method of the printable string before realizing I could use 'in' here as well. I'd like to display the non-printable characters differently, since they can't be distinguished from genuine period '.' characters. Thus, I may use ANSI escape sequences like: for c in ln: if c in printable: sys.stdout.write(c) else: sys.stdout.write('\x1B[31m.') sys.stdout.write('\x1B[0m') print I'm also toying with the idea of showing hex bytes together with their ASCII representations, since I've often found it a chore to figure out which hex byte to change if I wanted to edit a certain ASCII char. Thus, I might display data something like this: 00(\0) 01() FF() 20( ) 34(4) 35(5) 36(6) 08(\b) 38(8) 39(9) 61(a) 62(b) 63(c) 64(d) 65(e) 7E(~) Where printing chars are shown in parenthesis, characters with Python escape sequences will be shown as their escapes in parens., while non-printing chars with no escapes will be shown with nothing in parens. Or perhaps a two-line output with offset addresses under the data. So many possibilities! Thanks for input! -- _ Christopher R. Carlen [EMAIL PROTECTED] SuSE 9.1 Linux 2.6.5 -- http://mail.python.org/mailman/listinfo/python-list
Re: Hex editor display - can this be more pythonic?
Marc 'BlackJack' Rintsch wrote: > On Sun, 29 Jul 2007 12:24:56 -0700, CC wrote: >>The next step consists of printing out the ASCII printable characters. >>I have devised the following silliness: >> >>printable = ' >>[EMAIL PROTECTED]&8*9(0)aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ\ >>`~-_=+\\|[{]};:\'",<.>/?' > > I'd use `string.printable` and remove the "invisible" characters like '\n' > or '\t'. What is `string.printable` ? There is no printable method to strings, though I had hoped there would be. I don't yet know how to make one. >>for c in ln: >> if c in printable: sys.stdout.write(c) >> else: sys.stdout.write('.') > The translation table can be created once and should be faster. I suppose the way I'm doing it requires a search through `printable` for each c, right? Whereas the translation would just be a lookup operation? If so then perhaps the translation would be better. >>I'd like to display the non-printable characters differently, since they >>can't be distinguished from genuine period '.' characters. Thus, I may >>use ANSI escape sequences like: >> >>for c in ln: >> if c in printable: sys.stdout.write(c) >> else: >> sys.stdout.write('\x1B[31m.') >> sys.stdout.write('\x1B[0m') >> >>print > > `re.sub()` might be an option here. Yeah, that is an interesting option. Since I don't wish to modify the block of data unless the user specifically edits it, so I might prefer the simple display operation. > For escaping: > > In [90]: '\n'.encode('string-escape') > Out[90]: '\\n' Hmm, I see there's an encoder that can do my hex display too. Thanks for the input! -- _ Christopher R. Carlen [EMAIL PROTECTED] SuSE 9.1 Linux 2.6.5 -- http://mail.python.org/mailman/listinfo/python-list
Re: Hex editor display - can this be more pythonic?
Dennis Lee Bieber wrote: > On Sun, 29 Jul 2007 12:24:56 -0700, CC <[EMAIL PROTECTED]> > declaimed the following in comp.lang.python: >>for c in ln: >> if c in printable: sys.stdout.write(c) >> else: >> sys.stdout.write('\x1B[31m.') >> sys.stdout.write('\x1B[0m') > Be aware that this does require having a terminal that understands > the escape sequences (which, to my understanding, means unusable on a > WinXP console window) Yeah, with this I'm not that concerned about Windows. Though, can WinXP still load the ansi.sys driver? >>Thus, I might display data something like this: >> >>00(\0) 01() FF() 20( ) 34(4) 35(5) 36(6) 08(\b) 38(8) 39(9) 61(a) 62(b) >>63(c) 64(d) 65(e) 7E(~) >> > UGH! :-D Lovely isn't it? > If the original "hex bytesdotted ASCII" side by side isn't > workable, I'd suggest going double line... > > 00 01 FF 20 34 35 36 08 38 39 61 62 63 64 65 7E > nul soh xFF sp 4 5 6 bs 8 9 a b c d e ~ Yeah, something like that is probably nicer. > Use the standard "name" for the control codes (though I shortened > "space" to "sp", and maybe just duplicate the hex for non-named, > non-printable, codes (mostly those in the x80-xFF range, unless you are > NOT using ASCII but something like ISO-Latin-1 I've got a lot to learn about this encoding business. > To allow for the names, means using a field width of four. Using a > line width of 16-data bytes makes for an edit window width of 64, and > you could fit a hex offset at the left of each line to indicate what > part of the file is being worked. Right. Thanks for the reply! -- _ Christopher R. Carlen [EMAIL PROTECTED] SuSE 9.1 Linux 2.6.5 -- http://mail.python.org/mailman/listinfo/python-list
Hex editor - Python beginner's code open to review
Hi: http://web.newsguy.com/crcarl/python/hexl.py This is my first Python program other than tutorial code snippet experimentation. I chose a hex line editor. I may do a hex screen editor once this is done, if I feel like playing with the curses module. Or move straight to wxPython. This is unfinished, and is really just a hex viewer at this point. It seems like a good point to stop and see what others think. I would be interested to hear any comments. My command parsing if of course crude, brute-force, and not very scalable. I am doing some reading on this subject, and will be trying to do a more generalized approach in the next revision. This is going to be a subject where I need to learn a lot, because I have 2-3 embedded applications in C that need varying levels of command parsers, so I will be seeking to improve this skill considerably. Thanks for input. -- _ Christopher R. Carlen [EMAIL PROTECTED] SuSE 9.1 Linux 2.6.5 -- http://mail.python.org/mailman/listinfo/python-list
Can I add methods to built in types with classes?
Hi: I've gotten through most of the "9. Classes" section of the tutorial. I can deal with the syntax. I understand the gist of what it does enough that I can play with it. But am still a long way from seeing how I can use this OOP stuff. But I have one idea. Not that the functional approach isn't workable, but I have a situation where I need to test if all the characters in a string are in the set of hexadecimal digits. So I wrote: -- from string import hexdigits def ishex(word): for d in word: if d not in hexdigits: return(False) else return(True) -- Then I can do this to check if a string is safe to pass to the int() function without raising an exception: if ishex(string): value = int(string, 16) But can I create a class which inherits the attributes of the string class, then add a method to it called ishex()? Then I can do: if string.ishex(): value = int(string, 16) The thing is, it doesn't appear that I can get my hands on the base class definition/name for the string type to be able to do: --- class EnhancedString(BaseStringType): def ishex(self): for d in word: if d not in hexdigits: return(False) else return(True) --- Thanks. -- _ Christopher R. Carlen [EMAIL PROTECTED] SuSE 9.1 Linux 2.6.5 -- http://mail.python.org/mailman/listinfo/python-list
How to add columns to python arrays
Hi there, I wanna compile a 6000x1000 array with python. The array starts from 'empty', each time I get a 6000 length list, I wanna add it to the exist array as a column vector. Is there any function to do so? Or, I can add the list as a rows, if this is easier, and transpose the whole array after all the rows are setup. Thanks so much. -- http://mail.python.org/mailman/listinfo/python-list
Consistent error
Good day, please I'm writing the algorithm below in python but unittest keeps giving error no matter how i rewrite it. This is the algorithm: Create a function get_algorithm_result to implement the algorithm below Get a list of numbers L1, L2, L3LN as argument Assume L1 is the largest, Largest = L1 Take next number Li from the list and do the following If Largest is less than Li Largest = Li If Li is last number from the list then return Largest and come out Else repeat same process starting from step 3 Create a function prime_number that does the following Takes as parameter an integer and Returns boolean value true if the value is prime or Returns boolean value false if the value is not prime Here's my code in python : def get_algorithm_result( numlist ): largest = numlist[0] i = 1 while ( i < len(numlist) ): if ( largest < numlist[i]): largest = numlist[i] i = i + 1 numlist[i] = numlist[-1] return largest numlist = [1,2,3,4,5] largest = get_algorithm_result(numlist) print largest def prime_number(x): return len([n for n in range(1, x + 1) if x % n == 0]) <= 2 With this code it gives error message saying failure in test_maximum_number_two Then when I remove the numlist[i] = numlist[-1] The error message becomes failure in test_maximum_number_one (Using unit test) Please help, what am I writing wrong? Thanks! -- https://mail.python.org/mailman/listinfo/python-list
Re: Consistent error
Thanks Chris! Don't worry about the indent, will fix it I've rewritten it to this- def get_algorithm_result( numlist ): > largest = numlist[0] > i = 1 > while ( i < len(numlist) ): i = i + 1 >if ( largest < numlist[i]): > largest = numlist[i] > numlist[i] = numlist[-1] > numlist = [1,2,3,4,5] return largest > def prime_number(x): > return len([n for n in range(1, x + 1) if x % n == 0]) <= 2 But it still gives the test_maximum_number_one error. Please if you have any ideas what else I should change or add, let me know. Thanks! -- https://mail.python.org/mailman/listinfo/python-list
Re: Consistent error
On Sunday, January 3, 2016 at 5:14:33 PM UTC+1, Chris Angelico wrote: > On Mon, Jan 4, 2016 at 2:59 AM, wrote: > > Thanks Chris! > > Don't worry about the indent, will fix it > > I've rewritten it to this- > > > > def get_algorithm_result( numlist ): > >> largest = numlist[0] > >> i = 1 > >> while ( i < len(numlist) ): > > i = i + 1 > >>if ( largest < numlist[i]): > >> largest = numlist[i] > >> numlist[i] = numlist[-1] > >> numlist = [1,2,3,4,5] > >return largest > >> def prime_number(x): > >> return len([n for n in range(1, x + 1) if x % n == 0]) <= 2 > > > > But it still gives the test_maximum_number_one error. > > Please if you have any ideas what else I should change or add, let me know. > > Thanks! > > Well, the algorithmic comments I mentioned would still help you to > figure out what's going on :) > > ChrisA Thanks Chris! You possess great knowledge I'd like to have... ... well I'm just a newbie... -- https://mail.python.org/mailman/listinfo/python-list
Re: Consistent error
On Sunday, January 3, 2016 at 5:28:49 PM UTC+1, Ian wrote: > On Sun, Jan 3, 2016 at 8:59 AM, wrote: > > Thanks Chris! > > Don't worry about the indent, will fix it > > I've rewritten it to this- > > > > def get_algorithm_result( numlist ): > >> largest = numlist[0] > >> i = 1 > >> while ( i < len(numlist) ): > > i = i + 1 > >>if ( largest < numlist[i]): > >> largest = numlist[i] > >> numlist[i] = numlist[-1] > >> numlist = [1,2,3,4,5] > >return largest > > This is even harder to read than before since some of the lines are > now quoted and some are not. > > >> def prime_number(x): > >> return len([n for n in range(1, x + 1) if x % n == 0]) <= 2 > > > > But it still gives the test_maximum_number_one error. > > Please if you have any ideas what else I should change or add, let me know. > > Thanks! > > It's hard to give any specific advice about fixing the unittest > failure without knowing what the test is testing. These two lines > don't seem to have anything to do with the algorithm that you quoted > in the first post, however: > > > numlist[i] = numlist[-1] > > numlist = [1,2,3,4,5] > > It looks like you should kill everything in this function after the > assignment to largest and then start reimplementing the algorithm > again from the " If Li is last number from the list" step. Thanks Ian! The algorithm is actually two part question, that's why the prime number part in the answer. And good enough that part isn't raising any errors. Still going over it hoping to get it right. Appreciate your input, God bless! -- https://mail.python.org/mailman/listinfo/python-list
Re: Help with regex search-and-replace (Perl to Python)
On 07 Feb 2010, at 10:03, Shashwat Anand wrote: Here is one simple solution : >>> intext = """Lorem [ipsum] dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut [labore] et [dolore] magna aliqua.""" >>> intext.replace('[', '{').replace(']', '}') 'Lorem {ipsum} dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut {labore} et {dolore} magna aliqua.' Some people, when confronted with a problem, think "I know, I’ll use regular expressions." Now they have two problems. — Jamie Zawinski in comp.lang.emacs. That is because regular expressions are what we learned in programming the shell from sed to awk and ksh and zsh and of course Perl and we've read the two books by Jeffrey and much much more!!! How do we rethink and relearn how we do things and should we ? What is the solution ? Jerry-- http://mail.python.org/mailman/listinfo/python-list
Python dos2unix one liner
Hi, This morning I am working though Building Skills in Python and was having problems with string.strip. Then I found the input file I was using was in DOS format and I thought it be best to convert it to UNIX and so I started to type perl -i -pe 's/ and then I though, wait, I'm learning Python, I have to think in Python, as I'm a Python newbie I fired up Google and typed: +python convert dos to unix +one +liner Found perl, sed, awk but no python on the first page So I tried +python dos2unix +one +liner -perl Same thing.. But then I found http://wiki.python.org/moin/Powerful%20Python%20One-Liners and tried this: cat file.dos | python -c "import sys,re; [sys.stdout.write(re.compile('\r\n').sub('\n', line)) for line in sys.stdin]" >file.unix And it works.. [10:31:11 incc-imac-intel ~/python] cat -vet file.dos one^M$ two^M$ three^M$ [10:32:10 incc-imac-intel ~/python] cat -vet file.unix one$ two$ three$ But it is long and just like sed does not do it in place. Is there a better way in Python or is this kind of thing best done in Perl ? Thanks, Jerry -- http://mail.python.org/mailman/listinfo/python-list
Re: Python dos2unix one liner
On 27 Feb 2010, at 12:44, Steven D'Aprano wrote: On Sat, 27 Feb 2010 10:36:41 +0100, @ Rocteur CC wrote: cat file.dos | python -c "import sys,re; [sys.stdout.write(re.compile('\r\n').sub('\n', line)) for line in sys.stdin]" >file.unix Holy cow!!! Calling a regex just for a straight literal-to-literal string replacement! You've been infected by too much Perl coding! Thanks for the replies I'm looking at them now, however, for those who misunderstood, the above cat file.dos pipe pythong does not come from Perl but comes from: http://wiki.python.org/moin/Powerful%20Python%20One-Liners Apply regular expression to lines from stdin [another command] | python -c "import sys,re; [sys.stdout.write(re.compile('PATTERN').sub('SUBSTITUTION', line)) for line in sys.stdin]" Nothing to do with Perl, Perl only takes a handful of characters to do this and certainly does not require the creation an intermediate file, I simply found the above example on wiki.python.org whilst searching Google for a quick conversion solution. Thanks again for the replies I've learned a few things and I appreciate your help. Jerry -- http://mail.python.org/mailman/listinfo/python-list
ACE RSA Authentication Manager
Hi, Anyone out there writing Jython for RSA Authentication Manager ? I'm fairly new to Python but I see it is possible to write Jython scripts for user administration. I'm also very interested in writing scripts for monitoring and basically everything else related to RSA but in Jython, i.e. instead of Java ? If you have resources, hints, links for doing this I'd very much appreciate it. Thanks in advance, Jerry -- http://mail.python.org/mailman/listinfo/python-list