Re: Multiple independently started python processes and sharing of a module
On 01/14/11 03:04, Kushal Kumaran wrote: - Original message - Hi all, I have the following problem (which I already have a hacked around solution that works but I'd would like some more input on it): I have a situation where multiple python processes are started independently from each other but by the same user with the same environment (as happens with mod_wsgi, when not using daemon mode). All of these processes access a single module which needs synchronization for some of the commands, for example a db (MySQLdb) module where when a select is done, the fetchall must be done of that same process before another process can do anything else. If the processes are independent, they are not sharing the database connection, unless you've taken steps to make it so. MySQLdb imported in one process should not interfere with MySQLdb importerd in another process. It might be a misconfiguration but, under mod_wsgi with apache it does. Cheers, Martin -- http://mail.python.org/mailman/listinfo/python-list
how to use priority queue with multiprocessing
Hey, -- question -- How can I use a priority queue to schedule jobs within the "multiprocessing pool" module? -- my scenario -- I want to run several jobs on a server. The jobs are being sent by users. However, all jobs have a different priority, and high-priority jobs should be processed before any low-priority job gets touched. Currently I just append all incoming jobs to the multiprocessing worker pool as follows: ### initialize worker pool pool= PriorityPool(processes=worker_count) process_handles = [] ### distribute function execution over several processes for job_parameter in job_parameter_list: handle = pool.apply_async(process_function, [job_parameter,]) process_handles.append(handle) This will only put the jobs in some kind of a list - and execute the jobs in the order they come in. Is it possible to use a priority queue for the process-pool? Kind Regards, Marco -- http://mail.python.org/mailman/listinfo/python-list
ANNOUNCE: NHI1-0.11, PLMK-2.0 und libmsgque-5.0
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 Dear User, ANNOUNCE:Major Feature Release libmsgque: Application-Server-Toolkit for C, C++, JAVA, C#, Go, TCL, PERL, PHP, PYTHON, RUBY, VB.NET PLMK: Programming-Language-Microkernel NHI1: Non-Human-Intelligence #1 SUMMARY === Finish release 4 of wp2 with adding Factory support. The Factory add the ability to create NEW server-types on-the-fly and introduce the self-programming capability to NHI1. The "Factory" is an important part of the object management and has the following basic features: * create a new instance identified by an "Identifier" or using an already available instance as template * cleanup and delete an instance * provide an "Identifier" for factory lookup and as an unique application name * identify the server in the network The link between the "Factory-Identifier" and the "Factory-Interface" is important for the future development of "libmsgque". Message-Routing, Service-Location and Persistent-Transactions depend on this feature. The relationship between the "MqFactoryS" and the "MqS" is the same as the relationship between a "type" and an "instance" of the "type" in a regular programming language. The "MqFactoryS" define the "type" of the server and the "MqS" define a single instance of the server. Every kind of server has !!only one!! specific "MqFactoryS" object but every instance of a server has one "MqS" object used for object management. Decreasing the size and the complexity of a "MqS" object will improve the server performance. In future more fields, defined in the "MqSetupS" attribute of the the "MqS" object, will move into "MqFactoryS" object. LINKS = libmsgque including PHP documentation: > http://nhi1.berlios.de/theLink/index.htm NHI1: > http://nhi1.berlios.de/ DOWNLOAD: > http://developer.berlios.de/projects/nhi1/ mfg, Andreas Otto (aotto1968) -BEGIN PGP SIGNATURE- Version: GnuPG v2.0.15 (GNU/Linux) Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJNMBYJAAoJEGTcPijNG3/AxGkH/1Nf7GBL7DWAUktwaFs7Bs69 7voAXXWgIug+X42MqmsjFY8TrVGSHJfB8au+gecP1z6RQnPlubT2Od9T3GbXJL5h ZeyK8r2cf7reqp0W63iw0Gh+mDV/bmcjqjA8RTvw95du8l8t0W+zSjcDmeMct/a6 o8eTvQTfCyr7+LcOqjzVEA19XVVgJBF55DA24+HACVFgXfRchpylZiXegmAC0iFy gWKDAyiC95wJzZuqK+a5hPAYOZ+nhAaEMDVY0olN81qnWnb7j6uubSWAbgdXPWaP zu1gXoGo2fugqQt8XB1Ux8gHZhXOXVQGxcX2LyMUwiI1iXxnLVXXL1K3p7+Wnng= =/7W5 -END PGP SIGNATURE- -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple independently started python processes and sharing of a module
On Fri, Jan 14, 2011 at 1:51 PM, Martin P. Hellwig wrote: > On 01/14/11 03:04, Kushal Kumaran wrote: >> >> - Original message - >>> >>> Hi all, >>> >>> I have the following problem (which I already have a hacked around >>> solution that works but I'd would like some more input on it): >>> >>> I have a situation where multiple python processes are started >>> independently from each other but by the same user with the same >>> environment (as happens with mod_wsgi, when not using daemon mode). >>> >>> All of these processes access a single module which needs >>> synchronization for some of the commands, for example a db (MySQLdb) >>> module where when a select is done, the fetchall must be done of that >>> same process before another process can do anything else. >>> >> >> If the processes are independent, they are not sharing the database >> connection, unless you've taken steps to make it so. MySQLdb imported in >> one process should not interfere with MySQLdb importerd in another process. >> >>> >> > It might be a misconfiguration but, under mod_wsgi with apache it does. > Ah, I didn't notice the mod_wsgi reference. I'm out of my depth here. Hopefully someone with mod_wsgi experience will chime in. This might help though: https://code.google.com/p/modwsgi/wiki/ProcessesAndThreading It seems if you're not using 'daemon' mode, global data might be shared. You could create new connections for each request (and close them when done). There won't be interference between select/fetch across multiple database connections. Additionally, the documentation of MySQLdb says it is a bad idea to share database connections between threads. -- regards, kushal -- http://mail.python.org/mailman/listinfo/python-list
Re: Multiple independently started python processes and sharing of a module
On 01/14/11 10:05, Kushal Kumaran wrote: This might help though: https://code.google.com/p/modwsgi/wiki/ProcessesAndThreading It seems if you're not using 'daemon' mode, global data might be shared. Yes I read that thoroughly before I started out implementing a solution. But in my case I wanted something that worked as expected and not be depending on specific configuration of underlying technology as I can not assure that these condition will be met. You could create new connections for each request (and close them when done). There won't be interference between select/fetch across multiple database connections. Additionally, the documentation of MySQLdb says it is a bad idea to share database connections between threads. That is a possible solution too, however the performance impact is in the range of 40% while doing forced synchronization and overhead of the singleton wrapper is around 20%. So the latter is what I have gone with. Thanks for bouncing off ideas though, much appreciated. -- mph -- http://mail.python.org/mailman/listinfo/python-list
Re: Resolve circular reference
Magnus Lyckå writes: a = X() del a > Deleted <__main__.X instance at 0x00CCCF80> a=X() b=X() a.b=b b.a=a del a gc.collect() > 0 del b gc.collect() > 4 If your method has a __del__ at all, the automatic cyclic collector is disabled. It detects the cycle, but it only stores the objects in gc.garbage, to give you a chance to do something about them, such as break the cycle(s) yourself. For example: >>> class X(object): ... def __del__(self): ... print 'deleted', self ... >>> a, b = X(), X() >>> a.cycle = b >>> b.cycle = a >>> del a, b >>> import gc >>> gc.collect() 4 >>> gc.garbage [<__main__.X object at 0xb76d84cc>, <__main__.X object at 0xb76d980c>] >>> del gc.garbage[0].cycle >>> del gc.garbage[:] deleted <__main__.X object at 0xb76d980c> deleted <__main__.X object at 0xb76d84cc> -- http://mail.python.org/mailman/listinfo/python-list
FTP problem
I'm using ftplib for the first time, and am having trouble getting it to work. I type >>> from ftplib import FTP >>> ftp = FTP('ftp.indexftp.barcap.com', 'A Valid Username', ' A Valid >>> Password') where I have suppressed the user name and password, and I get Traceback (most recent call last): File "", line 1, in ftp = FTP('ftp.indexftp.barcap.com') File "C:\Python26\lib\ftplib.py", line 116, in __init__ self.connect(host) File "C:\Python26\lib\ftplib.py", line 131, in connect self.sock = socket.create_connection((self.host, self.port), self.timeout) File "C:\Python26\lib\socket.py", line 498, in create_connection for res in getaddrinfo(host, port, 0, SOCK_STREAM): gaierror: [Errno 11001] getaddrinfo failed I have tried this on two different computers and on two different versions of Python (2.6 and 2.7). I get the same error both times, and have no understanding of what the problem might be. Any assistance would be greatly appreciated. Sincerely Thomas Philips -- http://mail.python.org/mailman/listinfo/python-list
Re: FTP problem
Thomas, > ftp = FTP('ftp.indexftp.barcap.com', 'A Valid Username', ' A Valid Password') Your FTP URI is bad. When I try to connect to your site from the Windows FTP client, I get the following response: Unknown host ftp.indexftp.barcap.com. Malcolm -- http://mail.python.org/mailman/listinfo/python-list
Re: FTP problem
gaierror: [Errno 11001] getaddrinfo failed That part of the error indicates, your computer is unable to resolve the IP address for the hostname ftp.indexftp.barcap.com Make sure the hostname is valid. -- With warm regards, Sudheer. S Personal home page - http://sudheer.net | Tech Chorus - http://techchorus.net Web and IT services - http://binaryvibes.co.in -- http://mail.python.org/mailman/listinfo/python-list
unbalanced tree iteration issue
Dear All! I have deal with large unbalanced trees and I have to implement post-order tree traversal. My first attempt is shown below ("Node" and "Tree" classes) and based on recursive generators approach. class Node(): def __init__(self,value): self.childs = [] self.value = value class Tree(): def __init__(self, root): self.root = root self.numberCells = 1 def add(self, node, child): node.childs.append(child) self.numberCells+=1 def __iter__(self): return self.postorder(self.root) def postorder(self, node): if node: for child in node.childs: for n in self.postorder(child): yield n yield node It works fine for small test trees. But, my tree has approximately 30 nodes, and shown post order traversal with generators takes 80 sec against 1 sec with simple recursive routine: def recursiveFromTop(node): for child in node.childs: recursiveFromTop(child) ## here I can do some computations with current node's data So, I'd like to know how should I implement (if it's possible of course) __iter__ for my tree class based on recursion without generators? Please, can You show me the ways? because I'm very passionate in idea iterate through my tree with simple: for node in tree: do something with node Thanks in Advance! Best Regards! Alex -- http://mail.python.org/mailman/listinfo/python-list
wx Just Print!
I'm using Python 2.6.5. I would like to be able to print an RTF file, with no prompts for printers or anything like that. Here's the code so far: import wx.richtext rtp = wx.richtext.RichTextPrinting() rtp.PrintFile('C:\\path\\to\\file.rtf') When I run it, it says: ... assert "(wxThePrintPaperDatabase*) NULL) failed ... What is the fix? -- http://mail.python.org/mailman/listinfo/python-list
TestFixtures 1.8.0 Released!
Hi All, I'm very happy to announce the first fully-documented release of TestFixtures, my collection of testing fixtures and helpers that I've been collecting for the last couple of years. Along with my own take on a lot of the common fixtures and helpers, it has some bits that I haven't seen anywhere else and so would like to point out: - Comparison objects for comparing objects that don't natively support comparison: http://packages.python.org/testfixtures/comparing.html#comparison-objects - Helpful mock objects for when you want to test code that makes use of datetime.datetime.now, datetime.date.today or time.time: http://packages.python.org/testfixtures/datetime.html - Helpers for capturing and checking messages logged from your code using the python logging framework: http://packages.python.org/testfixtures/logging.html - Helpers for working with temporary directories in tests. In particular, quickly and easily creating temporary directories and files within them and making assertions about things written to them by the code under test: http://packages.python.org/testfixtures/files.html There's plenty more in there too! Please do let me know what you find useful, if you find any bugs or if there are any features you'd like to see added. The package is on PyPI and a full list of all the links to docs, issue trackers and the like can be found here: http://www.simplistix.co.uk/software/python/testfixtures cheers, Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk -- http://mail.python.org/mailman/listinfo/python-list
Re: python 3 and Unicode line breaking
Of course I searched for one and couldn’t find; that goes without saying. Otherwise I wouldn’t even bother writing a message, isn’t it? I disagree people should cruft their messages with details about how they failed to find information, as that is unrelated to the question at hand and has no point other than polluting people’s mailboxes. I also see no reason to reply to a simple question with such discourtesy, and cannot understand why someone would be so aggressive to a stranger. -- http://mail.python.org/mailman/listinfo/python-list
Re: unbalanced tree iteration issue
14.01.2011, 14:15, "Alex Boyko" : > Dear All! > > I have deal with large unbalanced trees and I have to implement post-order > tree traversal. My first attempt is shown below ("Node" and "Tree" classes) > and based on recursive generators approach. > > class Node(): > def __init__(self,value): > self.childs = [] > self.value = value > > class Tree(): > > def __init__(self, root): > self.root = root > self.numberCells = 1 > > def add(self, node, child): > node.childs.append(child) > self.numberCells+=1 > > def __iter__(self): > return self.postorder(self.root) > > def postorder(self, node): > if node: > for child in node.childs: > for n in self.postorder(child): > yield n > yield node > > It works fine for small test trees. But, my tree has approximately 30 > nodes, and shown post order traversal with generators takes 80 sec against 1 > sec with simple recursive routine: > > def recursiveFromTop(node): > for child in node.childs: > recursiveFromTop(child) > ## here I can do some computations with current node's data > > So, I'd like to know how should I implement (if it's possible of course) > __iter__ for my tree class based on recursion without generators? Please, can > You show me the ways? > because I'm very passionate in idea iterate through my tree with simple: > > for node in tree: > do something with node > > Thanks in Advance! > Best Regards! > Alex > > -- > http://mail.python.org/mailman/listinfo/python-list Well, I think it's actually because the difference is that you would not do yielding, you would just put everything in memory and then return it. ret_val = [x for x in self.postorder(child)] return ret_val + [self] or something like that (but beware of memory). But that's strange. This code works fast: #!/usr/bin/env python # -*- coding: utf-8 -*- import sys def w(s): sys.stdout.write("%s" % s) sys.stdout.flush() class Node(): __slots__ = ('childs', 'value',) def __init__(self, value): self.childs = [] self.value = value def post_order(self): for child in self.childs: yield child yield self def build_tree(): def append_1000_childs(node): for i in xrange(20): node.childs.append(Node(10)) def append_n_levels(node, levels=1): if levels >= 1: append_1000_childs(node) if levels > 1: for child in node.childs: append_n_levels(child, levels - 1) root = Node(10) append_n_levels(root, 5) return root if __name__ == '__main__': from datetime import datetime w("building tree...") _t = datetime.now() root = build_tree() w("done\n") w(datetime.now() - _t) w("\n") w("doing generator post_order...") _t = datetime.now() for item in root.post_order(): fake = item.value w("done\n") w(datetime.now() - _t) w("\n") def post_order(root): for child in root.childs: post_order(child) fake = item.value w("doing non-generator post_order...") _t = datetime.now() post_order(root) w("done\n") w(datetime.now() - _t) w("\n") $ python postorder.py building tree...done 0:01:34.422288 doing generator post_order...done 0:00:00.18 doing non-generator post_order...done 0:00:01.232272 -- jabber: k...@ya.ru -- http://mail.python.org/mailman/listinfo/python-list
Re: unbalanced tree iteration issue
14.01.2011, 14:15, "Alex Boyko" : > Dear All! > > I have deal with large unbalanced trees and I have to implement post-order > tree traversal. My first attempt is shown below ("Node" and "Tree" classes) > and based on recursive generators approach. > > class Node(): > def __init__(self,value): > self.childs = [] > self.value = value > > class Tree(): > > def __init__(self, root): > self.root = root > self.numberCells = 1 > > def add(self, node, child): > node.childs.append(child) > self.numberCells+=1 > > def __iter__(self): > return self.postorder(self.root) > > def postorder(self, node): > if node: > for child in node.childs: > for n in self.postorder(child): > yield n > yield node > > It works fine for small test trees. But, my tree has approximately 30 > nodes, and shown post order traversal with generators takes 80 sec against 1 > sec with simple recursive routine: > > def recursiveFromTop(node): > for child in node.childs: > recursiveFromTop(child) > ## here I can do some computations with current node's data > > So, I'd like to know how should I implement (if it's possible of course) > __iter__ for my tree class based on recursion without generators? Please, can > You show me the ways? > because I'm very passionate in idea iterate through my tree with simple: > > for node in tree: > do something with node > > Thanks in Advance! > Best Regards! > Alex > > -- > http://mail.python.org/mailman/listinfo/python-list Forgot to make new-style object) class Node(object): The results for new-style objects are: $ python postorder.py building tree...done 0:00:26.180799 doing generator post_order...done 0:00:00.17 doing non-generator post_order...done 0:00:01.117986 -- jabber: k...@ya.ru -- http://mail.python.org/mailman/listinfo/python-list
Re: unbalanced tree iteration issue
14.01.2011, 14:17, "Alex Boyko" : > Dear All! > > I have deal with large unbalanced trees and I have to implement post-order > tree traversal. My first attempt is shown below ("Node" and "Tree" classes) > and based on recursive generators approach. > > class Node(): > def __init__(self,value): > self.childs = [] > self.value = value > > class Tree(): > > def __init__(self, root): > self.root = root > self.numberCells = 1 > > def add(self, node, child): > node.childs.append(child) > self.numberCells+=1 > > def __iter__(self): > return self.postorder(self.root) > > def postorder(self, node): > if node: > for child in node.childs: > for n in self.postorder(child): > yield n > yield node > > It works fine for small test trees. But, my tree has approximately 30 > nodes, and shown post order traversal with generators takes 80 sec against 1 > sec with simple recursive routine: > > def recursiveFromTop(node): > for child in node.childs: > recursiveFromTop(child) > ## here I can do some computations with current node's data > > So, I'd like to know how should I implement (if it's possible of course) > __iter__ for my tree class based on recursion without generators? Please, can > You show me the ways? > because I'm very passionate in idea iterate through my tree with simple: > > for node in tree: > do something with node > > Thanks in Advance! > Best Regards! > Alex > > -- > http://mail.python.org/mailman/listinfo/python-list God damn pypy is fast)) $ ~/bin/pypy-1.4.1-linux64/bin/pypy ./postorder.py building tree...done 0:00:03.000854 doing generator post_order...done 0:00:00.69 doing non-generator post_order...done 0:00:00.240168 -- jabber: k...@ya.ru -- http://mail.python.org/mailman/listinfo/python-list
Re: python 3 and Unicode line breaking
leoboiko, 14.01.2011 14:06: Of course I searched for one and couldn’t find; that goes without saying. Otherwise I wouldn’t even bother writing a message, isn’t it? I disagree people should cruft their messages with details about how they failed to find information, as that is unrelated to the question at hand and has no point other than polluting people’s mailboxes. http://www.catb.org/~esr/faqs/smart-questions.html#beprecise http://www.catb.org/~esr/faqs/smart-questions.html#volume Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: python 3 and Unicode line breaking
Steven D'Aprano, 14.01.2011 01:15: On Thu, 13 Jan 2011 12:45:31 -0800, leoboiko wrote: Is there an equivalent to the textwrap module that knows about the Unicode line breaking algorithm (UAX #14, http://unicode.org/reports/tr14/ )? Is access to Google blocked where you are, or would you just like us to do your searches for you? If you have tried searching, please say so, otherwise most people will conclude you haven't bothered, and most likely will not bother to reply. I think the OP was asking for something like the "textwrap" module (which the OP apparently knows about), but based on a special line break algorithm which, as suggested by the way the OP asks, is not supported by textwrap. Sadly, the OP did not clearly state that the required feature is really not supported by "textwrap" and in what way textwrap behaves differently. That would have helped in answering. Stefan -- http://mail.python.org/mailman/listinfo/python-list
Re: [TIP] TestFixtures 1.8.0 Released!
[ please set the reply-to to testing-in-python@ !] > The package is on PyPI and a full list of all the links to docs, issue > trackers and the like can be found here: > > http://www.simplistix.co.uk/software/python/testfixtures The number one problem with all the test fixture systems I ever auditioned for Django models was unbelievable slowness. Thats' a major bummer for TDD, because you should integrate after every few edits, and a slow integration derails your flow. On a project with 500 tests the complete run could be 5 minutes. Some fixture systems use JSON to represent model values, and they pump the JSON directly into SQL insert statements. I was using farmdev's fixture system, and it was incredibly slow. It also did not use raw SQL. It fully resolved all records into model objects, and called save() on them. (The extra round trip thru a model's validations would be nice - if we were testing validations. That's what explicit tests are for!) I rewrote the fixture loader, going direct to SQL, and got a major speed boost. Then I rewrote it again, going directly from XML to our (very simple & stereotypical) model objects, and called save(). The speed boost remained. I have no idea what farmdev fixture was doing to slow things down. Anyway thanks for the library, but you can see I can't use its fixture loader; I'm just putting this out here. But does it do Django models, and are they performant? -- Phlip http://c2.com/cgi/wiki?ZeekLand -- http://mail.python.org/mailman/listinfo/python-list
Re: python 3 and Unicode line breaking
On Jan 14, 11:48 am, Stefan Behnel wrote: > Sadly, the OP did not clearly state that the required feature > is really not supported by "textwrap" and in what way textwrap > behaves differently. That would have helped in answering. Oh, textwrap doesn’t work for arbitrary Unicode text at all. For example, it separates combining sequences: >>> s = "tiếng Việt" # precomposed >>> len(s) 10 >>> s = "tiếng Việt" # combining >>> len(s) # number of unicode characters; ≠ line length 14 >>> print(textwrap.fill(s, width=4)) # breaks sequences tiê ng Viê t It also doesn’t know about double-width characters: >>> s1 = "日本語のテキト" >>> s2 = "12345678901234" # both s1 and s2 use 14 columns >>> print(textwrap.fill(s1, width=7)) 日本語のテキト >>> print(textwrap.fill(s2, width=7)) 1234567 8901234 It doesn’t know about non-ascii punctuation: >>> print(textwrap.fill("abc-def", width=5)) # ASCII minus-hyphen abc- def >>> print(textwrap.fill("abc‐def", width=5)) # true hyphen U+2010 abc‐d ef It doesn’t know East Asian filling rules (though this is perhaps pushing it a bit beyond textwrap’s goals): >>> print(textwrap.fill("日本語、中国語", width=3)) 日本語 、中国 # should avoid linebreak before CJK punctuation 語 And it generally doesn’t try to pick good places to break lines at all, just making the assumption that 1 character = 1 column and that breaking on ASCII whitespaces/hyphens is enough. We can’t really blame textwrap for that, it is a very simple module and Unicode line breaking gets complex fast (that’s why the consortium provides a ready-made algorithm). It’s just that, with python3’s emphasis on Unicode support, I was surprised not to be able to find an UAX #14 implementation. I thought someone would surely have written one and I simply couldn’t find, so I asked precisely that. -- http://mail.python.org/mailman/listinfo/python-list
Re: TestFixtures 1.8.0 Released!
I'm new to python and have just been looking into a solution for Mocking objects. In particular, at the moment, testing some code that needs to access hardware IOs using DLLs. I do this using Ctypes. However, on my dev machine, I don't have the same hardware so calling the DLL functions will not work as expected. Therefore I'd like to mock out the ctypes dll calls. Can you give me an indication of how you would go about doing this with Simplistix? Just so you know, I use py.test for unit testing. Thanks, Jack On Fri, Jan 14, 2011 at 12:38 PM, Chris Withers wrote: > Hi All, > > I'm very happy to announce the first fully-documented release of > TestFixtures, my collection of testing fixtures and helpers that I've been > collecting for the last couple of years. > > Along with my own take on a lot of the common fixtures and helpers, it has > some bits that I haven't seen anywhere else and so would like to point out: > > - Comparison objects for comparing objects that don't natively support > comparison: > > http://packages.python.org/testfixtures/comparing.html#comparison-objects > > - Helpful mock objects for when you want to test code that makes use of > datetime.datetime.now, datetime.date.today or time.time: > > http://packages.python.org/testfixtures/datetime.html > > - Helpers for capturing and checking messages logged from your code > using the python logging framework: > > http://packages.python.org/testfixtures/logging.html > > - Helpers for working with temporary directories in tests. In > particular, quickly and easily creating temporary directories and > files within them and making assertions about things written to them > by the code under test: > > http://packages.python.org/testfixtures/files.html > > There's plenty more in there too! > > Please do let me know what you find useful, if you find any bugs or if > there are any features you'd like to see added. > > The package is on PyPI and a full list of all the links to docs, issue > trackers and the like can be found here: > > http://www.simplistix.co.uk/software/python/testfixtures > > cheers, > > Chris > > -- > Simplistix - Content Management, Batch Processing & Python Consulting > - http://www.simplistix.co.uk > -- > http://mail.python.org/mailman/listinfo/python-list > -- The earth is a very small stage in a vast cosmic arena. Think of the rivers of blood spilled by all those generals and emperors so that in glory and in triumph they could become the momentary masters of a fraction of a dot. - Carl Sagan [Pale Blue Dot] -- http://mail.python.org/mailman/listinfo/python-list
Re: TestFixtures 1.8.0 Released!
Appologies, please read the 2nd last line as: Can you give me an indication of how you would go about doing this with TestFixtures? :) Thanks Jack On Fri, Jan 14, 2011 at 2:39 PM, Jack Keegan wrote: > I'm new to python and have just been looking into a solution for Mocking > objects. In particular, at the moment, testing some code that needs to > access hardware IOs using DLLs. I do this using Ctypes. > However, on my dev machine, I don't have the same hardware so calling the > DLL functions will not work as expected. Therefore I'd like to mock out the > ctypes dll calls. Can you give me an indication of how you would go about > doing this with Simplistix? Just so you know, I use py.test for unit > testing. > > Thanks, > > Jack > > On Fri, Jan 14, 2011 at 12:38 PM, Chris Withers wrote: > >> Hi All, >> >> I'm very happy to announce the first fully-documented release of >> TestFixtures, my collection of testing fixtures and helpers that I've been >> collecting for the last couple of years. >> >> Along with my own take on a lot of the common fixtures and helpers, it has >> some bits that I haven't seen anywhere else and so would like to point out: >> >> - Comparison objects for comparing objects that don't natively support >> comparison: >> >> >> http://packages.python.org/testfixtures/comparing.html#comparison-objects >> >> - Helpful mock objects for when you want to test code that makes use of >> datetime.datetime.now, datetime.date.today or time.time: >> >> http://packages.python.org/testfixtures/datetime.html >> >> - Helpers for capturing and checking messages logged from your code >> using the python logging framework: >> >> http://packages.python.org/testfixtures/logging.html >> >> - Helpers for working with temporary directories in tests. In >> particular, quickly and easily creating temporary directories and >> files within them and making assertions about things written to them >> by the code under test: >> >> http://packages.python.org/testfixtures/files.html >> >> There's plenty more in there too! >> >> Please do let me know what you find useful, if you find any bugs or if >> there are any features you'd like to see added. >> >> The package is on PyPI and a full list of all the links to docs, issue >> trackers and the like can be found here: >> >> http://www.simplistix.co.uk/software/python/testfixtures >> >> cheers, >> >> Chris >> >> -- >> Simplistix - Content Management, Batch Processing & Python Consulting >> - http://www.simplistix.co.uk >> -- >> http://mail.python.org/mailman/listinfo/python-list >> > > > > -- > The earth is a very small stage in a vast cosmic arena. Think of the rivers > of blood spilled by all those generals and emperors so that in glory and in > triumph they could become the momentary masters of a fraction of a dot. > - Carl Sagan [Pale Blue Dot] > -- The earth is a very small stage in a vast cosmic arena. Think of the rivers of blood spilled by all those generals and emperors so that in glory and in triumph they could become the momentary masters of a fraction of a dot. - Carl Sagan [Pale Blue Dot] -- http://mail.python.org/mailman/listinfo/python-list
Re: TestFixtures 1.8.0 Released!
Hi Jack, On 14/01/2011 14:39, Jack Keegan wrote: objects. In particular, at the moment, testing some code that needs to access hardware IOs using DLLs. I do this using Ctypes. However, on my dev machine, I don't have the same hardware so calling the DLL functions will not work as expected. Therefore I'd like to mock out the ctypes dll calls. Can you give me an indication of how you would go about doing this with TestFixtures? Just so you know, I use py.test for unit testing. I've not used ctypes myself so let me know if things don't work ;-) I'd suggest developing mock objects to work in place of your ctypes objects. You may well find that if you're just wanting to test that the right calls are made you can use an existing mock objects such as: http://pypi.python.org/pypi/mock/ ...and then compare the method_calls attribute of the mock with what you expect it to be. TestFixtures provides some handy tools for managing the insertion and removal of mock objects, read here: http://packages.python.org/testfixtures/mocking.html cheers, Chris -- Simplistix - Content Management, Batch Processing & Python Consulting - http://www.simplistix.co.uk -- http://mail.python.org/mailman/listinfo/python-list
Re: TestFixtures 1.8.0 Released!
Hi Chris, Thanks for that. I'll give it a go. Cheers, Jack On Fri, Jan 14, 2011 at 2:49 PM, Chris Withers wrote: > Hi Jack, > > > On 14/01/2011 14:39, Jack Keegan wrote: > >> objects. In particular, at the moment, testing some code that needs to >> access hardware IOs using DLLs. I do this using Ctypes. >> However, on my dev machine, I don't have the same hardware so calling >> the DLL functions will not work as expected. Therefore I'd like to mock >> out the ctypes dll calls. Can you give me an indication of how you would >> go about doing this with TestFixtures? Just so you know, I use py.test for >> unit testing. >> > > I've not used ctypes myself so let me know if things don't work ;-) > > I'd suggest developing mock objects to work in place of your ctypes > objects. You may well find that if you're just wanting to test that the > right calls are made you can use an existing mock objects such as: > > http://pypi.python.org/pypi/mock/ > > ...and then compare the method_calls attribute of the mock with what you > expect it to be. > > TestFixtures provides some handy tools for managing the insertion and > removal of mock objects, read here: > > http://packages.python.org/testfixtures/mocking.html > > > cheers, > > Chris > > -- > Simplistix - Content Management, Batch Processing & Python Consulting > - http://www.simplistix.co.uk > -- The earth is a very small stage in a vast cosmic arena. Think of the rivers of blood spilled by all those generals and emperors so that in glory and in triumph they could become the momentary masters of a fraction of a dot. - Carl Sagan [Pale Blue Dot] -- http://mail.python.org/mailman/listinfo/python-list
Re: FTP problem
Please make the below change to get past this problem Change *ftp.*indexftp.barcap.com to indexftp.barcap.com Regards, Anurag On Fri, Jan 14, 2011 at 5:25 PM, Thomas Philips wrote: > I'm using ftplib for the first time, and am having trouble getting it > to work. I type > > >>> from ftplib import FTP > >>> ftp = FTP('ftp.indexftp.barcap.com', 'A Valid Username', ' A Valid > Password') > > where I have suppressed the user name and password, and I get > > Traceback (most recent call last): > File "", line 1, in >ftp = FTP('ftp.indexftp.barcap.com') > File "C:\Python26\lib\ftplib.py", line 116, in __init__ >self.connect(host) > File "C:\Python26\lib\ftplib.py", line 131, in connect >self.sock = socket.create_connection((self.host, self.port), > self.timeout) > File "C:\Python26\lib\socket.py", line 498, in create_connection >for res in getaddrinfo(host, port, 0, SOCK_STREAM): > gaierror: [Errno 11001] getaddrinfo failed > > I have tried this on two different computers and on two different > versions of Python (2.6 and 2.7). I get the same error both times, and > have no understanding of what the problem might be. Any assistance > would be greatly appreciated. > > Sincerely > > Thomas Philips > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Is it possible to let a virtual file created by cStringIO have a filename so that functions can read it by its filename?
Hi,all I hope use cStringIO to create virtual file, but my customed function which is from a shared library imported by ctypes just accepts a filename(string type) as parameter. So I'm wondering whether there is any method that make the virtual file created by cStringIO like a normal file which have a filename, so it can be called by my functions. Thank you! Yours, Cun Zhang -- http://mail.python.org/mailman/listinfo/python-list
Re: python 3 and Unicode line breaking
On Fri, 14 Jan 2011 05:06:15 -0800, leoboiko wrote: > Of course I searched for one and couldn’t find; that goes without > saying. Otherwise I wouldn’t even bother writing a message, isn’t it? You wouldn't say that if you had the slightest idea about how many people write to newsgroups and web forums asking for help without making the tiniest effort to solve the problem themselves. So, no, it *doesn't* go without saying -- unless, of course, you want the answer to also go without saying. > I disagree people should cruft their messages with details about how > they failed to find information, as that is unrelated to the question at > hand and has no point other than polluting people’s mailboxes. This is total nonsense -- how on earth can you say that it is unrelated to the question you are asking? It tells others what they should not waste their time trying, because you've already tried it. You don't need to write detailed step-by-step instructions of everything you've tried, but you can point us in the directions you've already traveled. Think of it this way... if you were paying money for professional advice, would you be happy to receive a bill for time spent doing the exact same things you have already tried? I'm sure you wouldn't be. So why do you think it is okay to waste the time of unpaid volunteers? That's just thoughtless and selfish. If you think so little of other people's time that you won't even write a few words to save them from going down the same dead-ends that you've already tried, then don't be surprised if they think so little of your time that they don't bother replying even when they know the answer. > I also see no reason to reply to a simple question with such > discourtesy, and cannot understand why someone would be so aggressive to > a stranger. If you think my reply was aggressive and discourteous, you've got a lot to learn about public forums. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: unbalanced tree iteration issue
Thank you for your reply, but I have question about your code. Your defined: def post_order(self): for child in self.childs: yield child yield self just for "Node" , not for "Tree", and do w("doing generator post_order...") _t = datetime.now() for item in root.post_order(): fake = item.value w("done\n") w(datetime.now() - _t) w("\n") what give us only iterating along root's children, not iterating along tree...Or I didn't catch your though? Best Regards Alex 2011/1/14 kost BebiX > 14.01.2011, 14:15, "Alex Boyko" : > > Dear All! > > > > I have deal with large unbalanced trees and I have to implement > post-order tree traversal. My first attempt is shown below ("Node" and > "Tree" classes) and based on recursive generators approach. > > > > class Node(): > > def __init__(self,value): > > self.childs = [] > > self.value = value > > > > class Tree(): > > > > def __init__(self, root): > > self.root = root > > self.numberCells = 1 > > > > def add(self, node, child): > > node.childs.append(child) > > self.numberCells+=1 > > > > def __iter__(self): > > return self.postorder(self.root) > > > > def postorder(self, node): > > if node: > > for child in node.childs: > > for n in self.postorder(child): > > yield n > > yield node > > > > It works fine for small test trees. But, my tree has approximately 30 > nodes, and shown post order traversal with generators takes 80 sec against 1 > sec with simple recursive routine: > > > > def recursiveFromTop(node): > > for child in node.childs: > > recursiveFromTop(child) > > ## here I can do some computations with current node's data > > > > So, I'd like to know how should I implement (if it's possible of course) > __iter__ for my tree class based on recursion without generators? Please, > can You show me the ways? > > because I'm very passionate in idea iterate through my tree with simple: > > > > for node in tree: > >do something with node > > > > Thanks in Advance! > > Best Regards! > > Alex > > > > -- > > http://mail.python.org/mailman/listinfo/python-list > > Well, I think it's actually because the difference is that you would not do > yielding, you would just put everything in memory and then return it. > > ret_val = [x for x in self.postorder(child)] > return ret_val + [self] > > or something like that (but beware of memory). But that's strange. This > code works fast: > > #!/usr/bin/env python > # -*- coding: utf-8 -*- > > import sys > > def w(s): >sys.stdout.write("%s" % s) >sys.stdout.flush() > > class Node(): >__slots__ = ('childs', 'value',) > >def __init__(self, value): >self.childs = [] >self.value = value > > def post_order(self): >for child in self.childs: >yield child >yield self > > def build_tree(): >def append_1000_childs(node): >for i in xrange(20): >node.childs.append(Node(10)) > >def append_n_levels(node, levels=1): >if levels >= 1: >append_1000_childs(node) >if levels > 1: > for child in node.childs: > append_n_levels(child, levels - 1) > >root = Node(10) >append_n_levels(root, 5) >return root > > if __name__ == '__main__': >from datetime import datetime > >w("building tree...") >_t = datetime.now() >root = build_tree() >w("done\n") >w(datetime.now() - _t) >w("\n") > >w("doing generator post_order...") >_t = datetime.now() >for item in root.post_order(): >fake = item.value >w("done\n") >w(datetime.now() - _t) >w("\n") > >def post_order(root): >for child in root.childs: >post_order(child) >fake = item.value > >w("doing non-generator post_order...") >_t = datetime.now() >post_order(root) >w("done\n") >w(datetime.now() - _t) >w("\n") > > $ python postorder.py > building tree...done > 0:01:34.422288 > doing generator post_order...done > 0:00:00.18 > doing non-generator post_order...done > 0:00:01.232272 > > -- > jabber: k...@ya.ru > -- http://mail.python.org/mailman/listinfo/python-list
Re: unbalanced tree iteration issue
So I mean, your approach with generators showed good results in terms of time efficiency 'cos iteration was done for root and root's children. On 14 January 2011 18:57, Alex Boyko wrote: > Thank you for your reply, but I have question about your code. Your > defined: > >def post_order(self): >for child in self.childs: >yield child >yield self > > just for "Node" , not for "Tree", and do > >w("doing generator post_order...") >_t = datetime.now() >for item in root.post_order(): >fake = item.value >w("done\n") >w(datetime.now() - _t) >w("\n") > > what give us only iterating along root's children, not iterating along > tree...Or I didn't catch your though? > > Best Regards > Alex > > 2011/1/14 kost BebiX > > 14.01.2011, 14:15, "Alex Boyko" : >> > Dear All! >> > >> > I have deal with large unbalanced trees and I have to implement >> post-order tree traversal. My first attempt is shown below ("Node" and >> "Tree" classes) and based on recursive generators approach. >> > >> > class Node(): >> > def __init__(self,value): >> > self.childs = [] >> > self.value = value >> > >> > class Tree(): >> > >> > def __init__(self, root): >> > self.root = root >> > self.numberCells = 1 >> > >> > def add(self, node, child): >> > node.childs.append(child) >> > self.numberCells+=1 >> > >> > def __iter__(self): >> > return self.postorder(self.root) >> > >> > def postorder(self, node): >> > if node: >> > for child in node.childs: >> > for n in self.postorder(child): >> > yield n >> > yield node >> > >> > It works fine for small test trees. But, my tree has approximately >> 30 nodes, and shown post order traversal with generators takes 80 sec >> against 1 sec with simple recursive routine: >> > >> > def recursiveFromTop(node): >> > for child in node.childs: >> > recursiveFromTop(child) >> > ## here I can do some computations with current node's data >> > >> > So, I'd like to know how should I implement (if it's possible of course) >> __iter__ for my tree class based on recursion without generators? Please, >> can You show me the ways? >> > because I'm very passionate in idea iterate through my tree with simple: >> > >> > for node in tree: >> >do something with node >> > >> > Thanks in Advance! >> > Best Regards! >> > Alex >> > >> > -- >> > http://mail.python.org/mailman/listinfo/python-list >> >> Well, I think it's actually because the difference is that you would not >> do yielding, you would just put everything in memory and then return it. >> >> ret_val = [x for x in self.postorder(child)] >> return ret_val + [self] >> >> or something like that (but beware of memory). But that's strange. This >> code works fast: >> >> #!/usr/bin/env python >> # -*- coding: utf-8 -*- >> >> import sys >> >> def w(s): >>sys.stdout.write("%s" % s) >>sys.stdout.flush() >> >> class Node(): >>__slots__ = ('childs', 'value',) >> >>def __init__(self, value): >>self.childs = [] >>self.value = value >> >> def post_order(self): >>for child in self.childs: >>yield child >>yield self >> >> def build_tree(): >>def append_1000_childs(node): >>for i in xrange(20): >>node.childs.append(Node(10)) >> >>def append_n_levels(node, levels=1): >>if levels >= 1: >>append_1000_childs(node) >>if levels > 1: >> for child in node.childs: >> append_n_levels(child, levels - 1) >> >>root = Node(10) >>append_n_levels(root, 5) >>return root >> >> if __name__ == '__main__': >>from datetime import datetime >> >>w("building tree...") >>_t = datetime.now() >>root = build_tree() >>w("done\n") >>w(datetime.now() - _t) >>w("\n") >> >>w("doing generator post_order...") >>_t = datetime.now() >>for item in root.post_order(): >>fake = item.value >>w("done\n") >>w(datetime.now() - _t) >>w("\n") >> >>def post_order(root): >>for child in root.childs: >>post_order(child) >>fake = item.value >> >>w("doing non-generator post_order...") >>_t = datetime.now() >>post_order(root) >>w("done\n") >>w(datetime.now() - _t) >>w("\n") >> >> $ python postorder.py >> building tree...done >> 0:01:34.422288 >> doing generator post_order...done >> 0:00:00.18 >> doing non-generator post_order...done >> 0:00:01.232272 >> >> -- >> jabber: k...@ya.ru >> > > -- http://mail.python.org/mailman/listinfo/python-list
Re: unbalanced tree iteration issue
On Fri, Jan 14, 2011 at 5:15 AM, Alex Boyko wrote: > So, I'd like to know how should I implement (if it's possible of course) > __iter__ for my tree class based on recursion without generators? You could try something like this (untested): from itertools import chain, imap ... def postorder(self, node): return chain(chain.from_iterable(imap(self.postorder, node.childs)), [node]) Or you could write the iterator the old-fashioned way, as an object with state (also untested): def __iter__(self): return PostOrderIter(self.root) class PostOrderIter(object): def __iter__(self, node): self.stack = [(node, 0)] def next(self): if not self.stack: raise StopIteration node, index = self.stack.pop() if index < len(node.childs): child = node.childs[index] self.stack.append((node, index+1)) self.stack.append((child, 0)) return self.next() else: return node -- http://mail.python.org/mailman/listinfo/python-list
Re: unbalanced tree iteration issue
14.01.2011, 18:57, "Alex Boyko" : > 2011/1/14 kost BebiX >> 14.01.2011, 14:15, "Alex Boyko" : >> >>> Dear All! >>> >>> I have deal with large unbalanced trees and I have to implement post-order >>> tree traversal. My first attempt is shown below ("Node" and "Tree" classes) >>> and based on recursive generators approach. >>> >>> class Node(): >>> def __init__(self,value): >>> self.childs = [] >>> self.value = value >>> >>> class Tree(): >>> >>> def __init__(self, root): >>> self.root = root >>> self.numberCells = 1 >>> >>> def add(self, node, child): >>> node.childs.append(child) >>> self.numberCells+=1 >>> >>> def __iter__(self): >>> return self.postorder(self.root) >>> >>> def postorder(self, node): >>> if node: >>> for child in node.childs: >>> for n in self.postorder(child): >>> yield n >>> yield node >>> >>> It works fine for small test trees. But, my tree has approximately 30 >>> nodes, and shown post order traversal with generators takes 80 sec against >>> 1 sec with simple recursive routine: >>> >>> def recursiveFromTop(node): >>> for child in node.childs: >>> recursiveFromTop(child) >>> ## here I can do some computations with current node's data >>> >>> So, I'd like to know how should I implement (if it's possible of course) >>> __iter__ for my tree class based on recursion without generators? Please, >>> can You show me the ways? >>> because I'm very passionate in idea iterate through my tree with simple: >>> >>> for node in tree: >>> do something with node >>> >>> Thanks in Advance! >>> Best Regards! >>> Alex >>> >> >>> -- >>> http://mail.python.org/mailman/listinfo/python-list >> >> Well, I think it's actually because the difference is that you would not do >> yielding, you would just put everything in memory and then return it. >> >> ret_val = [x for x in self.postorder(child)] >> return ret_val + [self] >> >> or something like that (but beware of memory). But that's strange. This code >> works fast: >> >> #!/usr/bin/env python >> # -*- coding: utf-8 -*- >> >> import sys >> >> def w(s): >> sys.stdout.write("%s" % s) >> sys.stdout.flush() >> >> class Node(): >> __slots__ = ('childs', 'value',) >> >> def __init__(self, value): >> self.childs = [] >> self.value = value >> >> def post_order(self): >> for child in self.childs: >> yield child >> yield self >> >> def build_tree(): >> def append_1000_childs(node): >> for i in xrange(20): >> node.childs.append(Node(10)) >> >> def append_n_levels(node, levels=1): >> if levels >= 1: >> append_1000_childs(node) >> if levels > 1: >> for child in node.childs: >> >> append_n_levels(child, levels - 1) >> >> root = Node(10) >> append_n_levels(root, 5) >> return root >> >> if __name__ == '__main__': >> from datetime import datetime >> >> w("building tree...") >> _t = datetime.now() >> root = build_tree() >> w("done\n") >> w(datetime.now() - _t) >> w("\n") >> >> w("doing generator post_order...") >> _t = datetime.now() >> for item in root.post_order(): >> fake = item.value >> w("done\n") >> w(datetime.now() - _t) >> w("\n") >> >> def post_order(root): >> for child in root.childs: >> post_order(child) >> fake = item.value >> >> w("doing non-generator post_order...") >> _t = datetime.now() >> post_order(root) >> w("done\n") >> w(datetime.now() - _t) >> w("\n") >> >> $ python postorder.py >> building tree...done >> 0:01:34.422288 >> doing generator post_order...done >> 0:00:00.18 >> doing non-generator post_order...done >> 0:00:01.232272 >> >> -- >> jabber: k...@ya.ru > Thank you for your reply, but I have question about your code. Your defined: > >def post_order(self): >for child in self.childs: >yield child >yield self > > just for "Node" , not for "Tree", and do > >w("doing generator post_order...") >_t = datetime.now() >for item in root.post_order(): >fake = item.value >w("done\n") >w(datetime.now() - _t) >w("\n") > > what give us only iterating along root's children, not iterating along > tree...Or I didn't catch your though? > > Best Regards > Alex > Well, isn't tree is a root node and it's children? Why do you need Tree class anyway? p.s.: please, do not top-post the messages, write your reply at bottom because it will then be easier to read for those who will google this page. -- jabber: k...@ya.ru -- http://mail.python.org/mailman/listinfo/python-list
Re: unbalanced tree iteration issue
On Fri, Jan 14, 2011 at 11:07 AM, Ian Kelly wrote: > class PostOrderIter(object): > > def __iter__(self, node): > self.stack = [(node, 0)] That __iter__ should actually be __init__, of course. -- http://mail.python.org/mailman/listinfo/python-list
Re: unbalanced tree iteration issue
2011/1/14 kost BebiX : > Well, isn't tree is a root node and it's children? And its grandchildren, great-grandchildren, etc. What Alex is saying is that the implementation you posted traverses the root and its immediate children, but does not recur any further than that. That is why it was so fast. -- http://mail.python.org/mailman/listinfo/python-list
Re: unbalanced tree iteration issue
14.01.2011, 20:19, "Ian Kelly" : > 2011/1/14 kost BebiX ;: > >> Well, isn't tree is a root node and it's children? > > And its grandchildren, great-grandchildren, etc. What Alex is saying > is that the implementation you posted traverses the root and its > immediate children, but does not recur any further than that. That is > why it was so fast. Oh, yeah, sorry, forgot the recursion. It should be (if I'm not wrong again): def post_order(self): for child in self.childs: for po in child.post_order(): yield po yield self if you give it more deepness: $ python postorder.py building tree...done 0:00:25.839462 doing generator post_order...done 0:00:02.776876 doing non-generator post_order...done 0:00:01.092648 still not bad, but if you'll give it more deepness $ python postorder.py building tree...done 0:00:16.078972 doing generator post_order...done 0:00:03.119023 doing non-generator post_order...done 0:00:00.841976 it will be worse -- jabber: k...@ya.ru -- http://mail.python.org/mailman/listinfo/python-list
Re: how to use priority queue with multiprocessing
On 1/13/2011 9:07 AM, Marco Hornung wrote: Hey, -- question -- How can I use a priority queue to schedule jobs within the "multiprocessing pool" module? -- my scenario -- I want to run several jobs on a server. The jobs are being sent by users. However, all jobs have a different priority, and high-priority jobs should be processed before any low-priority job gets touched. Currently I just append all incoming jobs to the multiprocessing worker pool as follows: ### initialize worker pool pool = PriorityPool(processes=worker_count) process_handles = [] ### distribute function execution over several processes for job_parameter in job_parameter_list: handle = pool.apply_async(process_function, [job_parameter,]) process_handles.append(handle) This will only put the jobs in some kind of a list - and execute the jobs in the order they come in. Is it possible to use a priority queue for the process-pool? You''ll probably have to track the available processes yourself, starting a new job when there's a process available. One way to do this is to have a management thread for each process. Each management thread starts a subprocess, gets a work item from the priority queue (blocking if necessary), gives it to the subprocess, waits for the subprocess to return a result, and goes back to get another work item. This is straightforward, except for working out a way to cleanly shut the thing down. One way to do that is to have a "shutdown" flag visible to all the threads. That's checked before getting a new task. If it's set, the thread terminates its subprocess and returns. Set the terminate flag in a signal handler for control-C. (I have something that manages multiple processes using a priority queue, where the queue is implemented using MySQL. This allows me to put a whole cluster to work.) John Nagle -- http://mail.python.org/mailman/listinfo/python-list
Developing a program to make a family tree.
Hi there. I'm trying to develop a program like family tree maker. I have all information, so there is no need to search on the net. This must be something like trees. Can someone help me? I'm at the beginning. Thanks. -- Ata J. Tabrizi atae.tabr...@metu.edu.tr -- http://mail.python.org/mailman/listinfo/python-list
Re: python 3 and Unicode line breaking
Hey, On 14 Jan 2011 16:07:12 GMT Steven D'Aprano wrote: > > > I also see no reason to reply to a simple question with such > > discourtesy, and cannot understand why someone would be so aggressive to > > a stranger. > > If you think my reply was aggressive and discourteous, you've got a lot > to learn about public forums. Perhaps you've got to learn about politeness yourself! Just because some people are jerks on internet forums (or in real life) doesn't mean everyone should; this is quite a stupid and antisocial excuse actually. You would never have reacted this way if the same question had been phrased by a regular poster here (let alone on python-dev). Taking cheap shots at newcomers is certainly not the best way to welcome them. Thank you Antoine. -- http://mail.python.org/mailman/listinfo/python-list
Elliptic Curve Prime factorisation
Hello all , I have implemented Elliptic curve prime factorisation using wikipedia [ http://en.wikipedia.org/wiki/Lenstra_elliptic_curve_factorization]. I think that this code is not optimised and posting for further improvement. Feel free to comment and if you have any link regarding Elliptic curve prime factorisation , kindly post it. Thank you import math import random #y^2=x^3+ax+b mod n def extended_gcd(a,b): # taken from wikipedia x,y,lastx,lasty=0,1,1,0 while b!=0: q=a/b a,b=b,a%b x,lastx=(lastx-q*x,x) y,lasty=(lasty-q*y,y) if a<0: return (-a,-lastx,-lasty) else: return (a,lastx,lasty) def gcd(a,b): if a < 0: a = -a if b < 0: b = -b if a == 0: return b if b == 0: return a while b != 0: (a, b) = (b, a%b) return a def randomCurve(N): A,u,v=random.randrange(N),random.randrange(N),random.randrange(N) B=(v*v-u*u*u-A*u)%N return [(A,B,N),(u,v)] def addPoint(E,p_1,p_2): if p_1=="Identity": return [p_2,1] if p_2=="Identity": return [p_1,1] a,b,n=E (x_1,y_1)=p_1 (x_2,y_2)=p_2 x_1%=n y_1%=n x_2%=n y_2%=n if x_1 != x_2 : d,u,v=extended_gcd(x_1-x_2,n) s=((y_1-y_2)*u)%n x_3=(s*s-x_1-x_2)%n y_3=(-y_1-s*(x_3-x_1))%n else: if (y_1+y_2)%n==0:return ["Identity",1] else: d,u,v=extended_gcd(2*y_1,n) s=((3*x_1*x_1+a)*u)%n x_3=(s*s-2*x_1)%n y_3=(-y_1-s*(x_3-x_1))%n return [(x_3,y_3),d] def mulPoint(E,P,m): Ret="Identity" d=1 while m!=0: if m%2!=0: Ret,d=addPoint(E,Ret,P) if d!=1 : return [Ret,d] # as soon as i got anything otherthan 1 return P,d=addPoint(E,P,P) if d!=1 : return [Ret,d] m>>=1 return [Ret,d] def ellipticFactor(N,m,times=5): for i in xrange(times): E,P=randomCurve(N); Q,d=mulPoint(E,P,m) if d!=1 : return d return N if __name__=="__main__": n=input() m=int(math.factorial(1000)) while n!=1: k=ellipticFactor(n,m) n/=k print k -- http://mail.python.org/mailman/listinfo/python-list
Re: Developing a program to make a family tree.
On Jan 14, 7:39 pm, Ata Jafari wrote: > Hi there. > I'm trying to develop a program like family tree maker. I have all > information, so there is no need to search on the net. This must be > something like trees. Can someone help me? I'm at the beginning. > Thanks. > > -- > Ata J. Tabrizi > atae.tabr...@metu.edu.tr If you're after mature and actively developed Genealogy software developed in Python, then check out http://gramps-project.org/ The developer list is very friendly. Otherwise, you're in for a struggle, as you need to choose a storage back-end, a GUI (wxWindows/GTK/Qt4 etc...), how to handle GEDCOM format (unless it's not going to be compatible with other software), does it need to produce web pages/reports (and in what formats). I strongly suggest looking at GRAMPS and see what you're setting yourself up for :) hth Jon -- http://mail.python.org/mailman/listinfo/python-list
Re: how to use priority queue with multiprocessing
On Fri, 2011-01-14 at 10:57 -0800, John Nagle wrote: > On 1/13/2011 9:07 AM, Marco Hornung wrote: > I want to run several jobs on a server. The jobs are being sent by > users. However, all jobs have a different priority, and high-priority > jobs should be processed before any low-priority job gets touched. > > Currently I just append all incoming jobs to the multiprocessing > > worker pool as follows: ### initialize worker pool pool > > = > > PriorityPool(processes=worker_count) process_handles = [] > > ### distribute function execution over several processes for > > job_parameter in job_parameter_list: handle = > > pool.apply_async(process_function, [job_parameter,]) > > process_handles.append(handle) > > This will only put the jobs in some kind of a list - and execute the > > jobs in the order they come in. Is it possible to use a priority > > queue for the process-pool? > You''ll probably have to track the available processes yourself, > starting a new job when there's a process available. Which is exactly what we do in OpenGroupwre Coils' OIE. There is a process [job] list which is sorted by priority and the next available process is started when a worker is available. We use multiprocessing to create a *process*, rather than a thread, for each job. > One way to do this is to have a management thread for each > process. Each management thread starts a subprocess, gets > a work item from the priority queue (blocking if necessary), > gives it to the subprocess, waits for the subprocess to > return a result, and goes back to get another work item. We have a manager process and an executor process. These communicate via AMQ, but you could use any mechanism. The manager process controls the process [job] list. When a process needs to be started a message is send to the executor which creates a worker process if an opening is available. Otherwise it messages the manager process to place the process in a queued state. When a worker process completes it messages the executor which in turn messages the manager that a process slot may be available; then the manager looks up the next available process and messages the executor to start it - provided a worker slot is still available the executor will start the worker [otherwise the process will go back into a queued state]. > This is straightforward, except for working out a way > to cleanly shut the thing down. One way to do that is > to have a "shutdown" flag visible to all the threads. Using a message bus helps a lot, and with multiprocessing you just do a join/isalive to make sure a worker is still working. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is it possible to let a virtual file created by cStringIO have a filename so that functions can read it by its filename?
On Fri, Jan 14, 2011 at 7:52 AM, Cun Zhang wrote: > Hi,all > I hope use cStringIO to create virtual file, but my customed function which > is from a shared library imported by ctypes > just accepts a filename(string type) as parameter. > > So I'm wondering whether there is any method that make the virtual file > created by cStringIO like a normal file which have > a filename, so it can be called by my functions. That's not possible. (c)StringIO presents a file-like interface at the Python level, but under the covers, it's not implemented using anything like a normal file; thus, it doesn't have a presence on any filesystem. I would suggest using a temporary file (http://docs.python.org/library/tempfile.html ) for communicating with the C module, writing the contents of the StringIO object to the temporary file if necessary. (It's probably also possible to hack something together with FUSE, but it'd be a slow, platform-specific kludge.) Cheers, Chris -- http://blog.rebertia.com -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter: The good, the bad, and the ugly!
In article , Adam Skutt wrote: > >Replacing TkInter with some sort of minimized wxwidgets is a dumb idea >for some very obvious reasons, reasons that are obvious if you simply >look at a widget gallery and then the applications you run on your own >computer. Quite honestly, if you're not capable of that, there's >little reason to believe you'll ever be able to bring forth a >coherent, cogent proposal. I really don't follow that. You need a tremendous set to write gimp. Obviously you won't write gimp in Python. Now you want to jot together three cooperating screens to specify some properties for say bluetooth. The proposed set is ample for that, no? Such things make up a substantial part of the applications as far as numbers is concerned. They are probably written by people who don't want to dive very deeply into GUI. (Maybe they are more bluetooth experts than GUI-experts, what would you say?) > >Adam Groetjes Albert -- -- Albert van der Horst, UTRECHT,THE NETHERLANDS Economic growth -- being exponential -- ultimately falters. albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst -- http://mail.python.org/mailman/listinfo/python-list
Re: python 3 and Unicode line breaking
On 14-Jan-11 14:47 PM, Antoine Pitrou wrote: Hey, On 14 Jan 2011 16:07:12 GMT Steven D'Aprano wrote: I also see no reason to reply to a simple question with such discourtesy, and cannot understand why someone would be so aggressive to a stranger. If you think my reply was aggressive and discourteous, you've got a lot to learn about public forums. Perhaps you've got to learn about politeness yourself! Just because some people are jerks on internet forums (or in real life) doesn't mean everyone should; this is quite a stupid and antisocial excuse actually. You would never have reacted this way if the same question had been phrased by a regular poster here (let alone on python-dev). Taking cheap shots at newcomers is certainly not the best way to welcome them. Thank you Antoine. +1 -- http://mail.python.org/mailman/listinfo/python-list
do you know what's CGI? (web history personal story)
some extempore thought. Do you know what is CGI? Worked with Mathematica for 5 hours yesterday. Fantastic! This old hand can still do something! lol. My plane curve packages soon to be out n am gonna be rich. ...gosh what godly hours i've spend on Mathematica in 1990s. Surprised to find that i even Unproctected builtin symbols to fix things. (get rid of asymptotes in ParametricPlot) (Draft notes as i go: Mathematica Version 3 to Version 7 Conversion Notes) ... i recall, i stopped doing Mathematica in 1998 because it's a career dead-end as a programing lang, and dived into the utterly idiotic Perl & unix & mysql world. (See: The Unix Pestilence ◇ Xah Lee's Computing Experience (Impression Of Lisp from Mathematica).) Well, dead-end just as Emacs Lisp i'm spending my nights with in the past 4 years. LOL. And on that note, same thing can be said with haskell, OCaml. Though, fringe langs are picking up these days. Remember Python, ruby, in year 2000? Who'd imagined they'd become mainstream. But it took 10+ years. (See: Language, Purity, Cult, and Deception.) Also got reminded my age recently. Someone on stackoverflow is asking about what are those “A:” and “B:” drives on Windows. (anyone heard of floppy drives?) In another incident, i was chatting to a friend, and the topic went to internet tech in 1990s, and i was telling him about how PHP (aka Pretty Home Page) came about, then naturally i discussed CGI. After a while, i realized, those who are around 20 years old today were under 10 in the 1990s. They wouldn't know what was CGI, and no amount of explanation can tell them exactly it was like, because it has become HISTORY — if you didn't live it, you can't feel it. http://xahlee.blogspot.com/2011/01/do-you-know-what-is-cgi.html Xah ∑ http://xahlee.org/ -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter: The good, the bad, and the ugly!
On Wed, Dec 29, 2010 at 5:03 PM, rantingrick wrote: > On Dec 29, 6:41 pm, Gerry Reno wrote: >> Also, what do you think about frameworks such as pyjamas? It lets you >> write in python and compiles everything down to Javascript so it can be >> used across the Web as well as on the desktop. > > Hmm, this is like two double edged swords smashing one another in > battle. Seriously, get off of WoW and go write some code. If you'd spent the last year programming instead of doing your best Xah Lee impression you might have actually made some progress on this. Geremy Condra -- http://mail.python.org/mailman/listinfo/python-list
Re: Python use growing fast
On Jan 10, 1:43 pm, Alice Bevan–McGregor wrote: > On 2011-01-10 13:02:09 -0800, MRAB said: > > Wikipedia is a Wiki; everyone is free to contribute and correct mistakes. > > - Alice. Except for some of us. I tried to make a correction to a chemistry Wikipedia entry several months back. I received a message saying that a series of IP addresses which happen to include the one that my ISP assigned me had been blocked, due to hacking problems. Wikipedia provided a link to contact a real human being to request that an address be unblocked. I submitted a request, and -- nothing happened. -- http://mail.python.org/mailman/listinfo/python-list
Re: wx Just Print!
I think you need to create a wxApp first. Try adding app = wx.PySimpleApp() at the beginning. -- http://mail.python.org/mailman/listinfo/python-list
Re: python 3 and Unicode line breaking
On Fri, 14 Jan 2011 20:47:35 +0100, Antoine Pitrou wrote: > You would never have reacted this way if the same question had been > phrased by a regular poster here (let alone on python-dev). Taking cheap > shots at newcomers is certainly not the best way to welcome them. You're absolutely correct. Regular posters have demonstrated their ability to perform the basics -- if you had asked the question, I could assume that you would have done a google search, because I know you're not a lazy n00b who expects others to do their work for them. But the Original Poster has not, as far as I can see, ever posted here before. He has no prior reputation and gives no detail in his post. You have focused on my first blunt remark, and ignored the second: "If you have tried searching, please say so, otherwise most people will conclude you haven't bothered, and most likely will not bother to reply." This is good, helpful advice, and far more useful to the OP than just ignoring his post. You have jumped to his defense (or rather, you have jumped to criticise me) but I see that you haven't replied to his question or given him any advice in how to solve his problem. Instead of encouraging him to ask smarter questions, you encourage the behaviour that hinders his ability to get help from others. The only other person I can see who has attempted to actually help the OP is Stefan Behnel, who tried to get more information about the problem being solved in order to better answer the question. The OP has, so far as I can see, not responded, although he has taken the time to write to me in private to argue further. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Tkinter: The good, the bad, and the ugly!
> Seriously, get off of WoW and go write some code. If you'd spent the > last year programming instead of doing your best Xah Lee impression you > might have actually made some progress on this. I'm curious, is Xah Lee some sort of a Usenet meme? Cause this is not the first time I see his name in the context of a lightweight invective. -- http://mail.python.org/mailman/listinfo/python-list
Re: python 3 and Unicode line breaking
On Jan 14, 8:10 pm, Steven D'Aprano wrote: > The only other person I can see who has attempted to actually help the OP > is Stefan Behnel, who tried to get more information about the problem > being solved in order to better answer the question. The OP has, so far > as I can see, not responded, although he has taken the time to write to > me in private to argue further. I have written in private because I really feel this discussion is out- of-place here. This thread is already in the first page of google results for “python unicode line breaking”, “python uax #14” etc. I feel it would be good to use this place to discuss Unicode line breaking, not best practices on asking questions, or in how disappointly impolite the Internet has become. (Briefly: As a tech support professional myself, I prefer direct, concise questions than crufty ones; and I try to ask questions in the most direct manner precisely _because_ I don’t want to waste the time of kind volunteers with my problems.) As for taking the time to provide information, I wonder if there was any technical problem that prevented you from seeing my reply to Stefan, sent Jan 14, 12:29PM? He asked how exacly the stdlib module “textwrap” differs from the Unicode algorithm, so I provided some commented examples. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is it possible to let a virtual file created by cStringIO have a filename so that functions can read it by its filename?
On 01/14/2011 12:51 PM, Chris Rebert wrote: On Fri, Jan 14, 2011 at 7:52 AM, Cun Zhang wrote: Hi,all I hope use cStringIO to create virtual file, but my customed function which is from a shared library imported by ctypes just accepts a filename(string type) as parameter. So I'm wondering whether there is any method that make the virtual file created by cStringIO like a normal file which have a filename, so it can be called by my functions. That's not possible. (c)StringIO presents a file-like interface at the Python level, but under the covers, it's not implemented using anything like a normal file; thus, it doesn't have a presence on any filesystem. I would suggest using a temporary file (http://docs.python.org/library/tempfile.html ) for communicating with the C module, writing the contents of the StringIO object to the temporary file if necessary. (It's probably also possible to hack something together with FUSE, but it'd be a slow, platform-specific kludge.) Cheers, Chris -- http://blog.rebertia.com However, as the only reason to have the cstringIO object have a name is for you to open or close it. The rest of the functionality is the same, reading and writing too. -- in a terminal ... import cStringIO ab = cStringIO.StringIO() # an output type, as it was call with nothing. cd = cStringIO.StringIO( 'a filled buffer') # an input type. type( ab ) == cStringIO.OutputType True type( cd ) == cStringIO.InputType True Working with these properties we get Let's assume you have class with read and write ability, which assumes opening a file or cStringIO object (could extend to StringIO object with out much changing ). class foo: def __init__( self ): """ Define some variables. House keeping. """ self.readState = False self.writeState = False self.readObj = None self.writeObj = None def fooOpenWrite( self, fileobj ): if type( fileobj ) === StringType: self.writeObj = open( fileobj, 'wb' ) elif type( fileobj ) == cStringIO.OutputType: self.writeObj = fileobj else: self.writeState = False return self.readState = True return True def fooOpenRead( self, fileobj ): if type( fileobj ) === StringType: self.readObj = open( fileobj, 'wb' ) elif type( fileobj ) == cStringIO.OutputType: self.readObj = fileobj else: self.readState = False return self.readState = True return def fooRead( self ): for x in self.readObj: print x def fooWrite( self, str ): self.readObj.write( str ) Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: python 3 and Unicode line breaking
On 14 Jan 2011 22:10:02 GMT Steven D'Aprano wrote: > > This is good, helpful advice, and far more useful to the OP than just > ignoring his post. You have jumped to his defense (or rather, you have > jumped to criticise me) but I see that you haven't replied to his > question or given him any advice in how to solve his problem. Simply because I have no elaborate answer to give, even in the light of his/her recent precisions on the topic (and, actually, neither do you). Asking for precisions is certainly fine; doing it in an agressive way is not, especially when the original message doesn't look like the usual blunt, impolite and typo-ridden "can you do my homework" message. Also, I would expect someone familiar with the textwrap module's (lack of) unicode capabilities would have been able to answer the first message without even asking for precisions. Regards Antoine. -- http://mail.python.org/mailman/listinfo/python-list
Re: python 3 and Unicode line breaking
On Fri, 14 Jan 2011 06:29:27 -0800 (PST) leoboiko wrote: > > And it generally doesn’t try to pick good places to break lines > at all, just making the assumption that 1 character = 1 column > and that breaking on ASCII whitespaces/hyphens is enough. We > can’t really blame textwrap for that, it is a very simple module > and Unicode line breaking gets complex fast (that’s why the > consortium provides a ready-made algorithm). It’s just that, > with python3’s emphasis on Unicode support, I was surprised not > to be able to find an UAX #14 implementation. I thought someone > would surely have written one and I simply couldn’t find, so I > asked precisely that. If you're willing to help on that matter (or some aspects of them, textwrap-specific or not), you can open an issue on http://bugs.python.org and propose a patch. See also http://docs.python.org/devguide/#contributing if you need more info on how to contribute. Regards Antoine. -- http://mail.python.org/mailman/listinfo/python-list
Re: Developing a program to make a family tree.
try http://gramps-project.org/, which is created in python.. :) On Fri, Jan 14, 2011 at 1:39 PM, Ata Jafari wrote: > Hi there. > I'm trying to develop a program like family tree maker. I have all > information, so there is no need to search on the net. This must be > something like trees. Can someone help me? I'm at the beginning. > Thanks. > > -- > Ata J. Tabrizi > atae.tabr...@metu.edu.tr > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: Developing a program to make a family tree.
- Original message - > Hi there. > I'm trying to develop a program like family tree maker. I have all > information, so there is no need to search on the net. This must be > something like trees. Can someone help me? I'm at the beginning. > Thanks. > Family trees are nothing like trees, actually. If you start with that assumption, your software will sadly not be usable by many people. -- regards, kushal -- http://mail.python.org/mailman/listinfo/python-list
Re: python 3 and Unicode line breaking
On Fri, 14 Jan 2011 14:26:09 -0800, leoboiko wrote: ... > As for taking the time to provide information, I wonder if there was any > technical problem that prevented you from seeing my reply to Stefan, > sent Jan 14, 12:29PM? Presumably, since I haven't got it in my news client. This is not the first time. > He asked how exacly the stdlib module “textwrap” > differs from the Unicode algorithm, so I provided some commented > examples. Does this help? http://packages.python.org/kitchen/api-text-display.html kitchen.text.display.wrap(text, width=70, initial_indent=u'', subsequent_indent=u'', encoding='utf-8', errors='replace') Works like we want textwrap.wrap() to work [...] textwrap.wrap() from the python standard libray has two drawbacks that this attempts to fix: 1. It does not handle textual width. It only operates on bytes or characters which are both inadequate (due to multi-byte and double width characters). 2. It malforms lists and blocks. -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Elliptic Curve Prime factorisation
On Fri, 14 Jan 2011 11:52:21 -0800, mukesh tiwari wrote: > Hello all , I have implemented Elliptic curve prime factorisation using > wikipedia [ > http://en.wikipedia.org/wiki/Lenstra_elliptic_curve_factorization]. I > think that this code is not optimised and posting for further > improvement. Feel free to comment and if you have any link regarding > Elliptic curve prime factorisation , kindly post it. Thank you I don't think you can optimize it further in pure Python, although it is probably a good candidate for something like Cython, Pyrex or Shedskin. I think the code can be optimized for easier reading by putting single spaces around operators, following commas, etc. I find your style difficult to read. It could do with a docstring explaining what it does and how to use it, and some doctests. But other than that, it looks good. Have you considered putting it up on the ActiveState Python cookbook? -- Steven -- http://mail.python.org/mailman/listinfo/python-list
Re: Developing a program to make a family tree.
On 2011-01-14, Ata Jafari wrote: > I'm trying to develop a program like family tree maker. I have all > information, so there is no need to search on the net. This must be > something like trees. Can someone help me? I'm at the beginning. I don't know anything specific about family tree software and you don't really specify what you want your software to do. I can only assume that you are interested in taking the data in some format which contains the links between family members and creating a tree representation of that data? If I was going to attempt something like this, I would probably generate the representation as a set of postscript instructions. I would start with a basic template for a union which could be essentially pasted into different places in the output page. Then generating your tree is a simple matter of laying out the templates to match your data, filling in the template fields for each persion within their union, and drawing the connecting lines of the tree. Since I was already generating postscript anyway, I would probably implement much of the actual logic in postscript (the built in stacks provide an exellent way to process tree like structures in all of their nested levels). I would Python provide any user interface for manipulating the data and to dump the data into the postscript program. -- http://mail.python.org/mailman/listinfo/python-list
Re: Elliptic Curve Prime factorisation
On Fri, Jan 14, 2011 at 11:52 AM, mukesh tiwari wrote: > Hello all , I have implemented Elliptic curve prime factorisation > using wikipedia [ > http://en.wikipedia.org/wiki/Lenstra_elliptic_curve_factorization]. > I think that this code is not optimised and posting for further > improvement. Feel free to comment and if you have any link regarding > Elliptic curve prime factorisation , kindly post it. > Thank you You can get a lot of good suggestions for your code quickly and easily by going over it with pylint. For performance, you could try the gmpy module - it's good at dealing with large numbers. For an example, you might examine http://stromberg.dnsalias.org/svn/huge-prime/trunk . BTW, huge-prime dates from just shortly before I started routinely going over my code with pylint. -- http://mail.python.org/mailman/listinfo/python-list
Re: Elliptic Curve Prime factorisation
On Jan 15, 7:02 am, Steven D'Aprano wrote: > On Fri, 14 Jan 2011 11:52:21 -0800, mukesh tiwari wrote: > > Hello all , I have implemented Elliptic curve prime factorisation using > > wikipedia [ > >http://en.wikipedia.org/wiki/Lenstra_elliptic_curve_factorization]. I > > think that this code is not optimised and posting for further > > improvement. Feel free to comment and if you have any link regarding > > Elliptic curve prime factorisation , kindly post it. Thank you > > I don't think you can optimize it further in pure Python, although it is > probably a good candidate for something like Cython, Pyrex or Shedskin. > > I think the code can be optimized for easier reading by putting single > spaces around operators, following commas, etc. I find your style > difficult to read. > > It could do with a docstring explaining what it does and how to use it, > and some doctests. But other than that, it looks good. Have you > considered putting it up on the ActiveState Python cookbook? > > -- > Steven Thank you for your suggestion. I posted it ActiveState with comments. #!/usr/local/bin/python # -*- coding: utf-8 -*- import math import random #y^2=x^3+ax+b mod n # ax+by=gcd(a,b). This function returns [gcd(a,b),x,y]. Source Wikipedia def extended_gcd(a,b): x,y,lastx,lasty=0,1,1,0 while b!=0: q=a/b a,b=b,a%b x,lastx=(lastx-q*x,x) y,lasty=(lasty-q*y,y) if a<0: return (-a,-lastx,-lasty) else: return (a,lastx,lasty) def gcd(a,b): if a < 0: a = -a if b < 0: b = -b if a == 0: return b if b == 0: return a while b != 0: (a, b) = (b, a%b) return a # pick first a point P=(u,v) with random non-zero coordinates u,v (mod N), then pick a random non-zero A (mod N), # then take B = u^2 - v^3 - Ax (mod N). # http://en.wikipedia.org/wiki/Lenstra_elliptic_curve_factorization def randomCurve(N): A,u,v=random.randrange(N),random.randrange(N),random.randrange(N) B=(v*v-u*u*u-A*u)%N return [(A,B,N),(u,v)] # Given the curve y^2 = x^3 + ax + b over the field K (whose characteristic we assume to be neither 2 nor 3), and points # P = (xP, yP) and Q = (xQ, yQ) on the curve, assume first that xP != xQ. Let the slope of the line s = (yP - yQ)/(xP - xQ); since K # is a field, s is well-defined. Then we can define R = P + Q = (xR, - yR) by # s=(xP-xQ)/(yP-yQ) Mod N # xR=s^2-xP-xQMod N # yR=yP+s(xR-xP) Mod N # If xP = xQ, then there are two options: if yP = -yQ, including the case where yP = yQ = 0, then the sum is defined as 0[Identity]. # thus, the inverse of each point on the curve is found by reflecting it across the x-axis. If yP = yQ != 0, then R = P + P = 2P = # (xR, -yR) is given by # s=3xP^2+a/(2yP) Mod N # xR=s^2-2xP Mod N # yR=yP+s(xR-xP) Mod N # http://en.wikipedia.org/wiki/Elliptic_curve#The_group_law''') def addPoint(E,p_1,p_2): if p_1=="Identity": return [p_2,1] if p_2=="Identity": return [p_1,1] a,b,n=E (x_1,y_1)=p_1 (x_2,y_2)=p_2 x_1%=n y_1%=n x_2%=n y_2%=n if x_1 != x_2 : d,u,v=extended_gcd(x_1-x_2,n) s=((y_1-y_2)*u)%n x_3=(s*s-x_1-x_2)%n y_3=(-y_1-s*(x_3-x_1))%n else: if (y_1+y_2)%n==0:return ["Identity",1] else: d,u,v=extended_gcd(2*y_1,n) s=((3*x_1*x_1+a)*u)%n x_3=(s*s-2*x_1)%n y_3=(-y_1-s*(x_3-x_1))%n return [(x_3,y_3),d] # http://en.wikipedia.org/wiki/Elliptic_curve_point_multiplication # Q=0 [Identity element] # while m: # if (m is odd) Q+=P # P+=P # m/=2 # return Q') def mulPoint(E,P,m): Ret="Identity" d=1 while m!=0: if m%2!=0: Ret,d=addPoint(E,Ret,P) if d!=1 : return [Ret,d] # as soon as i got anything otherthan 1 return P,d=addPoint(E,P,P) if d!=1 : return [Ret,d] m>>=1 return [Ret,d] def ellipticFactor(N,m,times=5): for i in xrange(times): E,P=randomCurve(N); Q,d=mulPoint(E,P,m) if d!=1 : return d return N if __name__=="__main__": n=input() m=int(math.factorial(1000)) while n!=1: k=ellipticFactor(n,m) n/=k print k -- http://mail.python.org/mailman/listinfo/python-list
Re: Python use growing fast
On Jan 10, 9:24 pm, Dan Stromberg wrote: > About JavaScript's popularity: > 1) I've been getting the impression that JavaScript is popular in a > manner similar to how x86 machine language is popular: That is, it's > used all over, but few people hand code it (though admittedly, there > are probably more people hand coding JavaScript than people hand > coding x86 assembler today) Exactly, another half baked language that has been shoved down our throats like artery clogging Big Macs and French Fries! Oh how many times i have lamented for Python's eloquent syntax whilst brain farting Javascript idiosyncrasies! >:( -- http://mail.python.org/mailman/listinfo/python-list