Re: Little novice program written in Python
Rogério Brito: Hi, All. I'm just getting my feet wet on Python and, just for starters, I'm coding some elementary number theory algorithms (yes, I know that most of them are already implemented as modules, but this is an exercise in learning the language idioms). As you can see from the code below, my background is in C, without too much sophistication. What I would like is to receive some criticism to my code to make it more Python'esque and, possibly, use the resources of the computer in a more efficient way (the algorithm implemented below is the Sieve of Eratosthenes): my variant of the sieve def GetPrimes(N): arr = [] for i in range(1,N+1): arr.append(i) #Set first item to 0, because 1 is not a prime arr[0]=0 #sieve processing s=2 while s math.sqrt(N): if arr[s-1] != 0: j = s*s while j = N: arr[j-1] = 0 j += s s += 1 return [x for x in arr if x != 0] -- http://mail.python.org/mailman/listinfo/python-list
Re: Little novice program written in Python
also, i would recommend you to visit projecteuler.net you can solve math tasks and then see how others have done the same. you can fetch very good and pythonic solution there. -- http://mail.python.org/mailman/listinfo/python-list
Re: Little novice program written in Python
On 25 апр, 13:29, Arnaud Delobelle [EMAIL PROTECTED] wrote: hellt [EMAIL PROTECTED] writes: my variant of the sieve Since you posted it, you are also looking for advice to improve your code ;) def GetPrimes(N): arr = [] for i in range(1,N+1): arr.append(i) This is the same as: arr = range(1, N+1) !-) #Set first item to 0, because 1 is not a prime arr[0]=0 #sieve processing s=2 remove this line while s math.sqrt(N): for s in xrange(2, int(math.sqrt(N))+1): if arr[s-1] != 0: if arr[s-1]: j = s*s remove this line while j = N: for j in xrange(s*s, N+1, s): arr[j-1] = 0 j += s remove this line s += 1 remove this line return [x for x in arr if x != 0] return filter(None, arr) Altogether now: def getprimes(N): arr = range(1, N+1) arr[0] = 0 for s in xrange(2, int(math.sqrt(N))+1): if arr[s-1]: for j in xrange(s*s, N+1, s): arr[j-1] = 0 return filter(None, arr) It's the same, but it looks a bit less like the litteral translation of some C code. Lastly, the lines: for j in xrange(s*s, N+1, s): arr[j-1] = 0 from above can be condensed using extended slices: arr[s*s-1 : N+1 : s] = [0] * (N/s - s + 1) (If I can count correctly) Giving the following, slightly shorter and probably faster: def getprimes(N): arr = range(1, N+1) arr[0] = 0 for s in xrange(2, int(math.sqrt(N))+1): if arr[s-1]: arr[s*s-1 : N+1 : s] = [0] * (N/s - s + 1) return filter(None, arr) If it was me, I would include 0 in the array, giving the slightly simpler: def getprimes(N): arr = range(N+1) arr[1] = 0 for s in xrange(2, int(math.sqrt(N))+1): if arr[s]: arr[s*s : N+1 : s] = [0] * (N/s - s + 1) return filter(None, arr) (I think) This all needs to be tested. -- Arnaud nice, but i'm a newbie to python too, so some things for me seems a liitle complicated))) -- http://mail.python.org/mailman/listinfo/python-list
Re: Little novice program written in Python
On 25 апр, 15:02, Max M [EMAIL PROTECTED] wrote: Rogério Brito skrev: Hi, All. What I would like is to receive some criticism to my code to make it more Python'esque and, possibly, use the resources of the computer in a more efficient way (the algorithm implemented below is the Sieve of Eratosthenes): I agree with the rest here. Your code generally looks fine. But on another note, this type of code is not something you often see in Python. It is very dense with regard to algorithm. Most code is not like that so perhaps you should try something more usual like sending email, fetching webpages etc. to get a feel for the language. -- hilsen/regards Max M, Denmark http://www.mxm.dk/ IT's Mad Science em, i would say, that python (esp. with NumPy+Psyco) is very popular in numerical processing also. -- http://mail.python.org/mailman/listinfo/python-list
Re: Can you recommend a book?
On 25 апр, 15:09, Malcolm Greene [EMAIL PROTECTED] wrote: My two favorites: - Core Python Programming by Chun - Learning Python by Lutz Malcolm Learning Python (Lutz) is very good, reading right now. -- http://mail.python.org/mailman/listinfo/python-list
winreg module, need help to understand why i'm getting exception
HI all, i found winreg module from http://www.rutherfurd.net/python/winreg/index.html very useful and simple, instead _winreg. But i have a problem with this module, in its iterable part. look at the following code key = Key(HKLM,rSYSTEM\CurrentControlSet\Services\Tcpip\Enum) for i in key.values: print i.value and that is the exception Traceback (most recent call last): File D:\.Projects\python\temp.py, line 21, in module for i in key.values: File C:\Python25\Lib\site-packages\winreg.py, line 451, in next return self.values[self.index - 1] File C:\Python25\Lib\site-packages\winreg.py, line 289, in __getitem__ name = _winreg.EnumValue(self.hkey, key)[0] WindowsError: [Error 259] No more data is available so there is some problem with iterate, i think i am still a beginner, so i cant understand why this appears, and what should i fix. if anyone have some time to look closer to this module, i will appreciate this very much. thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: Filtering a Python list to uniques
On 26 мар, 02:30, kellygreer1 [EMAIL PROTECTED] wrote: What is the best way to filter a Python list to its unique members? I tried some method using Set but got some unhashable error. lstone = [ 1, 2, 3, 3, 4, 5, 5, 6 ] # how do i reduce this to lsttwo = [ 1, 2, 3, 4, 5, 6 ] Is there a page on this in the Python in a Nutshell or the Python Cookbook? Did I miss something? Kelly Greer [EMAIL PROTECTED] change nospam to yahoo or just look this thread for a fastest solution http://groups.google.com/group/comp.lang.python/browse_frm/thread/709189841310/af8961f1ed91ccea?lnk=gstq=duplicates#af8961f1ed91ccea -- http://mail.python.org/mailman/listinfo/python-list
Need help with OptionParser
today i've decided to use optionparser instead of GetOpt and unfortunately i've got an error which i cant handle my pice of code: from optparse import OptionParser def main(): usage = usage: %prog [options] parser = OptionParser(usage) parser.add_option(-f, --file, dest=filename, help=executable filename, metavar=FILE) parser.add_option(-b, --bighosts, action=store_true, dest=bighosts, default=False, help=with big hosts [default: %default]) (options, args) = parser.parse_args() if not options.bighosts: print parser.print_usage() if __name__==__main__: main() if i run test3.py without any arguments, i wait for generated help, but i see the following Usage: test3.py [options] None why None? Where are all of my options -- http://mail.python.org/mailman/listinfo/python-list
Re: Need help with OptionParser
On 25 мар, 15:42, hellt [EMAIL PROTECTED] wrote: today i've decided to use optionparser instead of GetOpt and unfortunately i've got an error which i cant handle my pice of code: from optparse import OptionParser def main(): usage = usage: %prog [options] parser = OptionParser(usage) parser.add_option(-f, --file, dest=filename, help=executable filename, metavar=FILE) parser.add_option(-b, --bighosts, action=store_true, dest=bighosts, default=False, help=with big hosts [default: %default]) (options, args) = parser.parse_args() if not options.bighosts: print parser.print_usage() if __name__==__main__: main() if i run test3.py without any arguments, i wait for generated help, but i see the following Usage: test3.py [options] None why None? Where are all of my options =) my bad unnecessary print in print parser.print_usage() and print_help() instead print_usage() -- http://mail.python.org/mailman/listinfo/python-list
Strange behavior of the Eclipse embedded console
i've faced with some strangeness while executing this sample: choice = raw_input(your choice: ) print len(choice) when i run this sample in my eclipse console with CPython and print Yes, i have this output 4 #trailing \t is the fourth element but when i use command line method python sample.py i have the correct length = 3 i'm curious now, can anyone explain that? -- http://mail.python.org/mailman/listinfo/python-list
Re: Strange behavior of the Eclipse embedded console
On 20 мар, 14:31, hellt [EMAIL PROTECTED] wrote: i've faced with some strangeness while executing this sample: choice = raw_input(your choice: ) print len(choice) when i run this sample in my eclipse console with CPython and print Yes, i have this output 4 #trailing \t is the fourth element but when i use command line method python sample.py i have the correct length = 3 i'm curious now, can anyone explain that? just took a look into PyDEV-FAQ. The eclipse console is not an exact copy of a shell... one of the changes is that when you press ENTER in a shell, it may give you a \r, \n or \r\n as an end-line char, depending on your platform. Python does not expect this -- from the docs it says that it will remove the last \n (checked in version 2.4), but, in some platforms that will leave a \r there. This means that the raw_input() should usually be used as raw_input().replace('\r', ''), and input() should be changed for: eval(raw_input().replace('\r', '')). -- http://mail.python.org/mailman/listinfo/python-list
Re: About reading Python code
On 18 мар, 03:57, sturlamolden [EMAIL PROTECTED] wrote: On 17 Mar, 04:54, WaterWalk [EMAIL PROTECTED] wrote: So I'm curious how to read code effectively. I agree that python code is clear, but when it becomes long, reading it can still be a hard work. First, I recommend that you write readable code! Don't use Python as if you're entering the obfuscated C contest. Two particularly important points: * If you find yourself thinking this module is too long, that's probably what it is. Half a page of code per module is fine. Two pages of code per module can be too much. * Comments are always helpful to the reader. Second, I recommend getting a good IDE. E.g. pick one of: * Microsoft Visual Studio (commercial) * Eclipse with PyDev and CDT (free) * SPE (free) * ActiveState Komodo IDE (commercial) under Microsoft Visual Studio do you mean IronPython instance? -- http://mail.python.org/mailman/listinfo/python-list
py2exe + pywinauto + sendkeys issue
Hi all i have a problem with this modules py2exe + pywinauto + sendkeys used together. In my script i'm using this expression app.window_(title=SJphone).Edit.TypeKeys(Test is running,with_spaces=True) TypeKeys is using SendKeys module i suppose. my setup.py looks like that: from distutils.core import setup import py2exe setup( options = {py2exe: {compressed: 1, optimize: 0, bundle_files: 1, packages: [encodings, pywinauto, pywinauto.controls, pywinauto.tests] } }, zipfile = None, console=[hosts.py] ) and when i'm trying to run my hosts.exe i'm getting this traceback Traceback (most recent call last): File hosts.py, line 524, in module main() File hosts.py, line 517, in main TestCase3_1() File hosts.py, line 421, in TestCase3_1 SJstart() File hosts.py, line 36, in SJstart app.window_(title=SJphone).Edit.TypeKeys(Test is running,with_spaces=Tru e) File pywinauto\controls\HwndWrapper.pyc, line 928, in TypeKeys NameError: global name 'SendKeys' is not defined are there any workarounds on that? -- http://mail.python.org/mailman/listinfo/python-list
Re: py2exe + pywinauto + sendkeys issue
On 17 мар, 15:48, Gabriel Genellina [EMAIL PROTECTED] wrote: En Mon, 17 Mar 2008 08:56:26 -0200, hellt [EMAIL PROTECTED] escribi�: i have a problem with this modules py2exe + pywinauto + sendkeys used together. In my script i'm using this expression app.window_(title=SJphone).Edit.TypeKeys(Test is running,with_spaces=True) TypeKeys is using SendKeys module i suppose. Does it work as a normal script, without using py2exe? my setup.py looks like that: from distutils.core import setup import py2exe setup( options = {py2exe: {compressed: 1, optimize: 0, bundle_files: 1, packages: [encodings, pywinauto, pywinauto.controls, pywinauto.tests] } }, zipfile = None, console=[hosts.py] ) Perhaps you have to include SendKeys explicitely. I think pywinauto doesn't require SendKeys, but uses it if already installed. -- Gabriel Genellina i tried this: packages: [encodings, pywinauto, SendKeys, pywinauto.controls, pywinauto.tests] But there was an error too( -- http://mail.python.org/mailman/listinfo/python-list
Re: py2exe + pywinauto + sendkeys issue
On 17 мар, 15:48, Gabriel Genellina [EMAIL PROTECTED] wrote: En Mon, 17 Mar 2008 08:56:26 -0200, hellt [EMAIL PROTECTED] escribi�: i have a problem with this modules py2exe + pywinauto + sendkeys used together. In my script i'm using this expression app.window_(title=SJphone).Edit.TypeKeys(Test is running,with_spaces=True) TypeKeys is using SendKeys module i suppose. Does it work as a normal script, without using py2exe? my setup.py looks like that: from distutils.core import setup import py2exe setup( options = {py2exe: {compressed: 1, optimize: 0, bundle_files: 1, packages: [encodings, pywinauto, pywinauto.controls, pywinauto.tests] } }, zipfile = None, console=[hosts.py] ) Perhaps you have to include SendKeys explicitely. I think pywinauto doesn't require SendKeys, but uses it if already installed. -- Gabriel Genellina pywinauto uses sendkeys when performs TypeKeys function. when i include sendkeys to my package's list i have an error posted below: No module named SendKeys -- http://mail.python.org/mailman/listinfo/python-list
Re: py2exe + pywinauto + sendkeys issue
On 17 мар, 20:22, hellt [EMAIL PROTECTED] wrote: On 17 мар, 15:48, Gabriel Genellina [EMAIL PROTECTED] wrote: En Mon, 17 Mar 2008 08:56:26 -0200, hellt [EMAIL PROTECTED] escribi�: i have a problem with this modules py2exe + pywinauto + sendkeys used together. In my script i'm using this expression app.window_(title=SJphone).Edit.TypeKeys(Test is running,with_spaces=True) TypeKeys is using SendKeys module i suppose. Does it work as a normal script, without using py2exe? my setup.py looks like that: from distutils.core import setup import py2exe setup( options = {py2exe: {compressed: 1, optimize: 0, bundle_files: 1, packages: [encodings, pywinauto, pywinauto.controls, pywinauto.tests] } }, zipfile = None, console=[hosts.py] ) Perhaps you have to include SendKeys explicitely. I think pywinauto doesn't require SendKeys, but uses it if already installed. -- Gabriel Genellina pywinauto uses sendkeys when performs TypeKeys function. when i include sendkeys to my package's list i have an error posted below: No module named SendKeys ok.. all is ok. i have just reinstall sendkeys. -- http://mail.python.org/mailman/listinfo/python-list