Re: How complex is complex?
On Mar 19, 11:29 am, Daniel Fetchinson fetchin...@googlemail.com wrote: When we say readability counts over complexity, how do we define what level of complexity is ok? For example: Say I have dict a = {'a': 2, 'c': 4, 'b': 3} I want to increment the values by 1 for all keys in the dictionary. So, should we do: for key in a: ... a[key] = a[key] + 1 or is it Ok to have code like: dict(map(lambda key: (key, a[key] + 1), a)) Before doing anything else I'd suggest leaving your code as is, closing your editor immediately and not touching it at all before the One True Answer arrives from the PSF. Please mail your question (along with a self-addressed envelope) to: Python Software Foundation P.O. Box 848 Hampton, NH 03843 USA where python language lawyers will consider it in great detail, consulting GvR if necessary. Please do not try to figure this one out by yourself! The PSF lawyers are trained to do this, such things are better left to professionals, you don't want to shoot yourself in the foot. Once every nuanced detail has been carefully weighed in and a consensus has been reached among the Supreme Python Language Commission chamber of the PSF the appropriate answer will be mailed back to you. Now you should be able to open up your favorite editor and hack away knowing full well that nobody and nothing can stop you, ever! Cheers, Daniel -- Psss, psss, put it down! -http://www.cafepress.com/putitdown I understand that my question was foolish, even for a newbie. I will not ask any more such questions in the future. -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
On Mar 19, 8:42 pm, Paul McGuire pt...@austin.rr.com wrote: On Mar 19, 4:39 am, Kottiyath n.kottiy...@gmail.com wrote: I understand that my question was foolish, even for a newbie. I will not ask any more such questions in the future. Gaaah! Your question was just fine, a good question on coding style. I wish more people would ask such questions so that bad habits could be avoided. The newbie posts that are annoying are the ones that: - are answered on page 1 of any tutorial (how do I get the second character of a string?) - are obvious homework assignments with no actual effort on the poster's part (how do I write a Python program to find the first 10 prime numbers?) - pontificate on what is wrong with Python, based on 2 hours' experience with the language (often titled What's wrong with Python, with content like Python sucks because it doesn't have a switch statement/has significant whitespace/doesn't check types of arguments/ isn't totally object-oriented like Java/doesn't have interfaces/...) - are so vague as to be just Usenet noise (titled Help me, with no content, or i need to write a program and don't know where to start can someone write it for me?) I think Daniel's joke was on the rest of us, who each had to chime in with our favorite dict processing algorithm. It *would* be good for you as a newbie to get an appreciation of the topics that were covered in these responses, though, especially the distinction between updating the dict in-place vs. creating a new dict. -- Paul Daniel, Sorry for misunderstanding your post. I hope I was not being passive-aggresive - (also because I found that the second mechanism I provided was quite horrible :-), so I was indeed being foolish there. ) Paul/Aahz, I did understand 2 things (1) When using map always consider that the function will be called everytime, so the hit on the performance is more. (2) The second mechanism and the first mechanism provides different solutions (new dict/same dict) both of which I did not think about at all. Also, thank you everyone for all the help. I have been following this thread for the last 4 months (when I started with python) and I have learned a lot. The amount of help provided here is amazing. p.s. - English is indeed not my first language :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: How complex is complex?
On Mar 19, 9:33 pm, Kottiyath n.kottiy...@gmail.com wrote: On Mar 19, 8:42 pm, Paul McGuire pt...@austin.rr.com wrote: On Mar 19, 4:39 am, Kottiyath n.kottiy...@gmail.com wrote: I understand that my question was foolish, even for a newbie. I will not ask any more such questions in the future. Gaaah! Your question was just fine, a good question on coding style. I wish more people would ask such questions so that bad habits could be avoided. The newbie posts that are annoying are the ones that: - are answered on page 1 of any tutorial (how do I get the second character of a string?) - are obvious homework assignments with no actual effort on the poster's part (how do I write a Python program to find the first 10 prime numbers?) - pontificate on what is wrong with Python, based on 2 hours' experience with the language (often titled What's wrong with Python, with content like Python sucks because it doesn't have a switch statement/has significant whitespace/doesn't check types of arguments/ isn't totally object-oriented like Java/doesn't have interfaces/...) - are so vague as to be just Usenet noise (titled Help me, with no content, or i need to write a program and don't know where to start can someone write it for me?) I think Daniel's joke was on the rest of us, who each had to chime in with our favorite dict processing algorithm. It *would* be good for you as a newbie to get an appreciation of the topics that were covered in these responses, though, especially the distinction between updating the dict in-place vs. creating a new dict. -- Paul Daniel, Sorry for misunderstanding your post. I hope I was not being passive-aggresive - (also because I found that the second mechanism I provided was quite horrible :-), so I was indeed being foolish there. ) Paul/Aahz, I did understand 2 things (1) When using map always consider that the function will be called everytime, so the hit on the performance is more. (2) The second mechanism and the first mechanism provides different solutions (new dict/same dict) both of which I did not think about at all. Also, thank you everyone for all the help. I have been following this thread for the last 4 months (when I started with python) and I have learned a lot. The amount of help provided here is amazing. p.s. - English is indeed not my first language :-) Oops, Forgot to mention the biggest learning. Readability is better than brevity - Thanks to Rhodri. This was a question which was bugging me all the time. When I look at code, I am always envious when I see the same code written in much smaller number of lines. Now, I will force myself to ask the questions Rhodri proposed (esp: does it look uglier part) before deciding whether or not to go ahead with brevity. -- http://mail.python.org/mailman/listinfo/python-list
How complex is complex?
When we say readability counts over complexity, how do we define what level of complexity is ok? For example: Say I have dict a = {'a': 2, 'c': 4, 'b': 3} I want to increment the values by 1 for all keys in the dictionary. So, should we do: for key in a: ... a[key] = a[key] + 1 or is it Ok to have code like: dict(map(lambda key: (key, a[key] + 1), a)) How do we decide whether a level of complexity is Ok or not? -- http://mail.python.org/mailman/listinfo/python-list
An ordering question
Hi, I have 2 lists a = [(4, 1), (7, 3), (3, 2), (2, 4)] b = [2, 4, 1, 3] Now, I want to order _a_ (a[1]) based on _b_. i.e. the second element in tuple should be the same as b. i.e. Output would be [(3, 2), (2, 4), (4, 1), (7, 3)] I did the same as follows: l = len(a) * [None] for (k, v) in a: ... for i, e in enumerate(b): ... if e == v: ...l[i] = (k, v) This works, but the code -for python- looks very kludgy. I thought for ~2 hours to see whether I can do it in a line or 2, but I cannot seem to find a mechanism. Can someone help me out? -- http://mail.python.org/mailman/listinfo/python-list
Re: Parameter sublists [was: An ordering question]
On Mar 14, 5:39 am, Chris Rebert c...@rebertia.com wrote: On Fri, Mar 13, 2009 at 5:30 PM, Peter Pearson ppear...@nowhere.invalid wrote: On Fri, 13 Mar 2009 18:56:30 +0100, Hrvoje Niksic hnik...@xemacs.org wrote: [snip] a.sort(key=lambda (x, y): b[y - 1], reverse=True) Huh? I had no idea one could do this: def g( ( ( x, y ), z ) ): ... return y ... g( ((1,2),3) ) 2 What should I have read to learn that trick? Don't bother. It's been excised in Python 3.0. Cheers, Chris -- I have a blog:http://blog.rebertia.com Thank you very much. These many solutions ? I think programming is not my forte :-) :-) -- http://mail.python.org/mailman/listinfo/python-list
Re: Porting to new Python version
On Feb 19, 5:50 pm, Gabor Urban urbang...@gmail.com wrote: Hi, I have a tough issue: we are using a Python application written quite a time ago for version 2.4. The code is mature, and there are no bugs. My bosses came up with the idea to port it to the latest release... I am not really convinced that it's a good step. I wellcome any information pro and contra. I would like to get the background as precisely as possible. Thanks in advance and good day to You! Gabor It might be helpful if you can provide more information regarding your application. For example, does it use lot of 3rd party code, What is the reason behind porting to latest version etc. All said, 2.4 to 2.5 or even 2.6 should not be much of an issue - it even has the advantage that performance is improved and some memory leaks are no longer there. Python 3* should be a problem since many 3rd party applications are not yet ported to it. -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there something easier than ORM?
一首诗 wrote: Hi all, Recently I am studying some python ORM libraries, such as sqlalchemy. These are very powerful technologies to handle database. But I think my project are not complicated to enough to benefit from a complete ORM system. What I really want, is some easy ways to load data from database, and change rows of data to list of named tuple, then I could send these data to my client application. I don't think I want these subtle behavior such as lazy load, auto update, ect. in ORM. So is there some libraries like that? Or is there some tools that could generate code from database scheme as I want? If ORM is not used, you might have to contend with different tools for different databases - and will have to write SQL commands. Luckily everything follows DB-API2 properly. http://www.python.org/dev/peps/pep-0249/. So you mostly dont have to change the commands to access DB. For the different tools, this http://wiki.python.org/moin/DatabaseInterfaces might be a good starting point. For example - psycopg2 for postgresql is almost the default etc. -- http://mail.python.org/mailman/listinfo/python-list
Spam
Hi, There seems to be lot of spam coming in this newsgroup. Is it possible to have some mechanism similar to say - slashdot - wherein mails can be moderated by any of the usual users? This is required only for people who is posting for the first time (or people who has been rated spam before). Second post onwards, no moderation required. Just wondering. -- http://mail.python.org/mailman/listinfo/python-list
Re: Import without executing module
On Feb 2, 12:19 pm, Stephen Hansen apt.shan...@gmail.com wrote: On Sun, Feb 1, 2009 at 11:05 PM, Ray rayky...@gmail.com wrote: Basically, someone has created a python script and I would like to make use of his functions. I would prefer to not modify his file so what I would like to do is just write my script and import parts that are needed. i.e., I would like to separate my changes from his as best as I can. However, that other module has both functions (def's, which I would like to use) and top-level commands which I don't need and in fact, prints errors when I import it since it was meant to be run as a top-level module and not imported in. i.e., its expecting arguments to be supplied. Unfortunately, that's not possible, I believe. All the top level commands in a particular Python script are executed: that's how the functions get created. --S Maybe he can wrap the things he dont need inside if __name__ == '__main__': check. -- http://mail.python.org/mailman/listinfo/python-list
Re: Swapping values of two variables
On Jan 30, 8:31 am, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On Thu, 29 Jan 2009 17:50:04 -0800, tony.clarke5 wrote: On Jan 30, 12:29 am, Eric Kang y...@sfu.ca wrote: In python, I set: x=1 y=3 z = x x = y y = z This gave me 3 1, which are the values of x and y swapped. The following would have given me the same result: x, y = y, x But could the swapping be done using less extra memory than this? What is the minimum amount of extra memory required to exchange two 32-bit quantities? What would be the pseudocode that achieves this minimum? How about: def transpose(x, y): print x, y, 'becomes: ', x = x + y y = x - y x = x - y print x, ' ', y transpose(1,3) transpose (9,8) I'm not sure what the point of that function is. It doesn't actually swap its arguments: x = 23 y = 42 transpose(x, y) 23 42 becomes: 42 23 x 23 y 42 And it certainly doesn't save memory, as the original poster asked: import dis swap = compile('x, y = y, x', '', 'single') dis.dis(swap) 1 0 LOAD_NAME 0 (y) 3 LOAD_NAME 1 (x) 6 ROT_TWO 7 STORE_NAME 1 (x) 10 STORE_NAME 0 (y) 13 LOAD_CONST 0 (None) 16 RETURN_VALUE dis.dis(transpose) 2 0 LOAD_FAST 0 (x) 3 PRINT_ITEM 4 LOAD_FAST 1 (y) 7 PRINT_ITEM 8 LOAD_CONST 1 ('becomes: ') 11 PRINT_ITEM 3 12 LOAD_FAST 0 (x) 15 LOAD_FAST 1 (y) 18 BINARY_ADD 19 STORE_FAST 0 (x) 4 22 LOAD_FAST 0 (x) 25 LOAD_FAST 1 (y) 28 BINARY_SUBTRACT 29 STORE_FAST 1 (y) 5 32 LOAD_FAST 0 (x) 35 LOAD_FAST 1 (y) 38 BINARY_SUBTRACT 39 STORE_FAST 0 (x) 6 42 LOAD_FAST 0 (x) 45 PRINT_ITEM 46 LOAD_CONST 2 (' ') 49 PRINT_ITEM 50 LOAD_FAST 1 (y) 53 PRINT_ITEM 54 PRINT_NEWLINE 55 LOAD_CONST 0 (None) 58 RETURN_VALUE The compiled code of the transpose function *alone* (not including all the other associated parts) takes 59 bytes, or 472 bits. len(transpose.func_code.co_code) 59 Even if it worked, that's hardly using less memory than a direct swap. -- Steven Is it possible to swap two floats without a variable? -- http://mail.python.org/mailman/listinfo/python-list
Re: pyAA for Python2.5
On Jan 29, 1:51 am, Rob Williscroft r...@freenet.co.uk wrote: Kottiyath wrote in news:6a594643-f6a2-4d8d-aab3-27eb16cb2fb8 @b38g2000prf.googlegroups.com in comp.lang.python: I have mingw32-gcc in my path. If I try that too -it fails. C:\Documents and Settings\Guest\pyAApython setup.py install -c mingw32-gcc invalid command name 'mingw32-gcc' All the examples I found via google have the tool name as mingw32 so try: python setup.py install -c mingw32 or python setup.py build --compiler=mingw32 install The compiler that the MinGW package installs is gcc. You shoulf be able to verify it is on your path by typing: gcc --help or gcc --version and see some output. Rob. --http://www.victim-prime.dsl.pipex.com/ Thank you Rob. The installation went ahead for some more time - but failed showing a lot of errors: compile running build running build_py file pyAAc.py (for module pyAAc) not found file pyAAc.py (for module pyAAc) not found ... pyAAc.cpp:5887: error: `EVENT_OBJECT_HELPCHANGE' was not declared in this scope pyAAc.cpp:5888: error: `EVENT_OBJECT_DEFACTIONCHANGE' was not declared in this scope pyAAc.cpp:5889: error: `EVENT_OBJECT_ACCELERATORCHANGE' was not declared in this scope ... error: command 'gcc' failed with exit status 1 I cannot understand why it fails. I have not worked in C till now, so I am pretty confused. I googled also, but to no avail. -- http://mail.python.org/mailman/listinfo/python-list
Re: Counting number of objects
Thank you everyone for your very helpful comments and suggestions. I have interacted in other newsgroups, but this is the most helpful of them all. As per the comments, I have now decided to go with the weakref mechanism - as per Andreas suggestion, functionally it looks correct that the person should not know the total number of people. So, in a higher level class, have a weakref list which contains a reference to each person. Total count will be len(list) at any time. Now, I couldnt find a weakref list - so I am using WeakKeyDictionary with the value as None - since len(dict) also should give me the data any time. I have another question here. In the documentation, it is mentioned that - Note: Caution: Because a WeakKeyDictionary is built on top of a Python dictionary, it must not change size when iterating over it. This can be difficult to ensure for a WeakKeyDictionary because actions performed by the program during iteration may cause items in the dictionary to vanish by magic (as a side effect of garbage collection). Now, the only two operations that I am doing are - __init__: d = weakref.WeakKeyDictionary() method y: x = aa() d[x] = None method z: total = len(d) I believe that all the commands which I perform on WeakKeyDictionary here - (adding a new element) (len(d)) - are atomic - or atleast nothing that can cause any worry as per the Note given above. Can anyone let me know whether my assumption is correct or not? Reason: My code has many many number of threads which interact with each other in myraid ways - so I do want to decrease the number of locks as much as possible. Especially I do not want to block other threads just for getting the count. -- http://mail.python.org/mailman/listinfo/python-list
pyAA for Python2.5
Hi, I would like to obtain pyAA for Python 2.5. I went through their web site, but they provide the windows exe only for Python2.4. I tried compiling from source, but that also was to no avail - it errs out as follows: C:\Documents and Settings\Guest\pyAApython setup.py install running install running build running build_py file pyAAc.py (for module pyAAc) not found file pyAAc.py (for module pyAAc) not found running build_ext error: Python was built with Visual Studio 2003; extensions must be built with a compiler than can generate compatible binaries. Visual Studio 2003 was not found on this system. If you have Cygwin installed, you can try compiling with MingW32, by passing -c mingw32 to setup.py. C:\Documents and Settings\Guest\pyAApython setup.py install -c mingw32 usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] or: setup.py --help [cmd1 cmd2 ...] or: setup.py --help-commands or: setup.py cmd --help error: invalid command 'mingw32' Has anyone tried the same? Can anyone give any suggestions? -- http://mail.python.org/mailman/listinfo/python-list
Re: pyAA for Python2.5
On Jan 27, 11:56 pm, Mike Driscoll kyoso...@gmail.com wrote: On Jan 27, 12:12 pm, Kottiyath n.kottiy...@gmail.com wrote: Hi, I would like to obtain pyAA for Python 2.5. I went through their web site, but they provide the windows exe only for Python2.4. I tried compiling from source, but that also was to no avail - it errs out as follows: C:\Documents and Settings\Guest\pyAApython setup.py install running install running build running build_py file pyAAc.py (for module pyAAc) not found file pyAAc.py (for module pyAAc) not found running build_ext error: Python was built with Visual Studio 2003; extensions must be built with a compiler than can generate compatible binaries. Visual Studio 2003 was not found on this system. If you have Cygwin installed, you can try compiling with MingW32, by passing -c mingw32 to setup.py. C:\Documents and Settings\Guest\pyAApython setup.py install -c mingw32 usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...] or: setup.py --help [cmd1 cmd2 ...] or: setup.py --help-commands or: setup.py cmd --help error: invalid command 'mingw32' Has anyone tried the same? Can anyone give any suggestions? You probably have to put mingw32 on your path or provide an absolute path to it. By the way, where did you get the source? I couldn't find it... Mike I have mingw32-gcc in my path. If I try that too -it fails. C:\Documents and Settings\Guest\pyAApython setup.py install -c mingw32-gcc invalid command name 'mingw32-gcc' I got the source from http://sourceforge.net/cvs/?group_id=65529 Can somebody help me out? Our project needs pyAA, but I am unable to build on Python2.5. -- http://mail.python.org/mailman/listinfo/python-list
Counting number of objects
Hi, I am creating a class called people - subclasses men, women, children etc. I want to count the number of people at any time. So, I created code like the following: class a(object): counter = 0 def __new__(cls, *args, **kwargs): a.counter += 1 return object.__new__(cls, *args, **kwargs) def __del__(self): a.counter -= 1 class aa(a): pass Now, the code works Ok. I have the following questions: 1. Is this code Ok? Is there any straightforward mechanism other than this to get the number of objects? 2. I read in Python Documentation that inside __del__ we should the minimum of interaction with external parameters. So, I am a little worried in subclassing __del__ to check the counter. Is whatever I have done Ok? Another question - unrelated to the major topic: How much time does it take to be proficient in Python? I have been working exclusively in Python for close to 3 months now, and even now I get inferiority complex when I read the answers sent by many of you. I have been programming for close to 7 years now (earlier in a language similar to COBOL). Does it take quite a bit of time to be proficient - as many of you guys - or am I just dumb? -- http://mail.python.org/mailman/listinfo/python-list
Python logging rollover
Hi, I want to do a log rollover - preferably based on day; size is also Ok. I checked logging.TimedRotatingFileHandler, but I am not sure whether it will suit my purpose. Mine is a desktop application. So, everytime the machine starts, the whole logging system is reinitialized. So, in such a case, can I use TimedRotatingFileHandler? I tested it with 'midnight' option, but it did not work as I expected. Regards K -- http://mail.python.org/mailman/listinfo/python-list
Re: Traceback in Logging
The issue is that I am on Python 2.4 which doesnt support func name. I am using filename and lineno now. That does serve the purpose. Thank you, I had not checked all the parameters. Regards K Vinay Sajip wrote: On Jan 6, 4:17 pm, Kottiyath n.kottiy...@gmail.com wrote: I dont want the whole traceback. I just wanted to know where the log was generated from i.e. which procedure and which line. I have 3/4 points in many procedures where I encounter a small error (not an exception) and want to log it. So having individual names for each looks to be somewhat verbose - esp since the application is 10K LOC. Don't the funcName and lineno arguments in the format string work for you? (See http://docs.python.org/library/logging.html#id1) Regards, Vinay Sajip -- http://mail.python.org/mailman/listinfo/python-list
Traceback in Logging
Hi all, Is it possible to print traceback for all logging? I have created something like this: def trace(): import traceback return ''.join(traceback.format_stack(limit=4)[1:]) And during every logging, append it - self.logger.info('Data: %s, kwargs: %s Traceback: %s' %(data, kwargs, trace())) --Result-- 2009-01-06 18:52:21,483 - test- INFO --- Data:, kwargs: {} Traceback: File C:\test.py, line 48, in success super(testObj, self).success(reply, **kwargs) File C:\test.py, line 87, in success self.logger.info('Data: %s, kwargs: %s Traceback: %s' %(data, kwargs, trace())) File C:\test.py, line 151, in trace return ''.join(traceback.format_stack(limit=4)[1:]) This seems somewhat kludgy. Is it possible in logging mechanism itself to provide traceback as default? I use Python2.4, so LoggingAdapter is not there too. -- http://mail.python.org/mailman/listinfo/python-list
Re: Traceback in Logging
I dont want the whole traceback. I just wanted to know where the log was generated from i.e. which procedure and which line. I have 3/4 points in many procedures where I encounter a small error (not an exception) and want to log it. So having individual names for each looks to be somewhat verbose - esp since the application is 10K LOC. This might be a good item to have in the logging system - along with time and level, just proc name and the line number. Thank you for the help. Regards K Vinay Sajip wrote: On Jan 6, 1:33 pm, Kottiyath n.kottiy...@gmail.com wrote: This seems somewhat kludgy. Is it possible in logging mechanism itself to provide traceback as default? No, because it's not a common use case to print tracebacks for every logging call. There's support for adding traceback information in exception handling, via use of the Logger.exception method in the exception handling code. I use Python2.4, so LoggingAdapter is not there too. If you need LoggingAdapter, you can always copy and paste the relevant code from Python's SVN repository into your own application. Regards, Vinay Sajip -- http://mail.python.org/mailman/listinfo/python-list
Is there a better algorithm?
I have the following list of tuples: L = [(1, 2), (3, 4, 5), (6, 7)] I want to loop through the list and extract the values. The only algorithm I could think of is: for i in l: ... u = None ... try: ... (k, v) = i ... except ValueError: ... (k, u, v) = i ... print k, u, v - 1 None 2 3 4 5 6 None 7 - But, this algorithm doesnt look very beautiful - like say - for k, u, v in L: Can anyone suggest a better algorithm to get the values? -- http://mail.python.org/mailman/listinfo/python-list
Re: Is there a better algorithm?
On Jan 3, 2:38 am, mr mario.rugg...@gmail.com wrote: As has been noted, the best is to fix the input to be regular-3- tuples. For the fun of it, here's another variation of a solution: tuples = [(1, 2), (3, 4, 5), (6, 7)] def triple_or_pair(seq): u = None try: k, u, v = seq except ValueError: k, v = seq return k, u, v for k, u, v in [ triple_or_pair(seq) for seq in tuples ]: print k, u, v It is a code to post some data to HTML server. Even though usually the POST values are of type(name, value), if file transfer is involved, then POST values change to (name, filename, value). My view was that since filename is a rare occurance and doesnt make sense in a usual POST, I had not kept it as a full 3 tuple. Since so many programmers (that too much more capable than me) are suggesting that it is code smell, I am reviewing my decision. -- http://mail.python.org/mailman/listinfo/python-list
Python module import loop issue
This might not be pure python question. Sorry about that. I couldnt think of any other place to post the same. I am creating a _medium_complex_ application, and I am facing issues with creating the proper module structure. This is my first application and since this is a run-of-the-mill application, I hope someone would be able to help me. Base Module: Contains definitions for Class A1, Class A2 Module 1.1: Class B1 (refines A1) Module 1.2: Class C1 (refines A1) Module 1.3: Class D1 (refines A1) Module 2.1: Class B2 (refines A2): Uses objects of B1, C1, D1 Module 2.2: Class C2 (refines A2) Module 2.3: Class D2 (refines A2) --Python Entry Module : Module EN-- Calls objects of B1, C1 and D1 Module EN and also Module 2 creates and calls the objects during run time - and so calls cannot be hardcoded. So, I want to use Factory methods to create everything. Module Factory: import 1.1,1.2,1.3, 2.1,2.2,2.3 A1Factory: {'B1Tag':1.1.B1, 'C1Tag':1.2.C1, 'D1Tag':1.3.D1'} A2Factory: {'B2Tag':2.1.B2, 'C2Tag':2.2.C2, 'D2Tag':2.3.D2'} But, since Module requires objects of B1, C1 etc, it has to import Factory. Module 2.1: import Factory. Now, there is a import loop. How can we avoid this loop? The following ways I could think of 1. Automatic updation of factory inside superclass whenever a subclass is created. But, since there is no object created, I cannot think of a way of doing this. 2. Update A1Factory in each module which implements refinements. _Very_important_, how do I make sure each module is hit - so that the factory is updated? The module EN will be looking only at base module, so the other modules is not hit. I will have to import every module in EN - just to make sure that the A1Factory updation code is hit. This looks in-elegent. If somebody could help me out, I would be very thankful. -- http://mail.python.org/mailman/listinfo/python-list
Re: Python module import loop issue
On Dec 30, 8:24 am, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: En Mon, 29 Dec 2008 19:47:51 -0200, Carl Banks pavlovevide...@gmail.com escribió: On Dec 29, 10:51 am, Kottiyath n.kottiy...@gmail.com wrote: Module Factory: A1Factory: {'B1Tag':1.1.B1, 'C1Tag':1.2.C1, 'D1Tag':1.3.D1'} A2Factory: {'B2Tag':2.1.B2, 'C2Tag':2.2.C2, 'D2Tag':2.3.D2'} But, since Module requires objects of B1, C1 etc, it has to import Factory. Now, there is a import loop. How can we avoid this loop? I'm going to suggest three ways: a straightforward, good-enough way; a powerful, intelligent, badass way; and a sneaky way. In Python 2.6 (and 3.0) there is a fourth way: class decorators. 1. The straightforward, good-enough way Define functions in Factory.py called register_A1_subclass and register_A2_subclass, then call them whenever you create a new subclass. Class decorators are a clean variant of this approach (in my opinion). package1/module1.py: - import Factory class B1(A1): # define class B1 here Factory.register_A1_subclass(B1Tag,B1) - That would become: @Factory.register_A1_subclass(B1Tag) class B1(A1): ... (for an adequate variant of register_A1_subclass). The advantage is that the register stuff appears prominently near the name of the class, and there is no need to repeat the name. Also, B1Tag can be left out, if it is stored as a class attribute of B1 (in some cases using __name__ is enough) 2. The powerful, intelligent, badass way Metaclasses. I would guess you do not want to do this, and I wouldn't recommend it if you haven't studied up on how metaclasses work, but it's a textbook example of their usefulness. If you expect to use factory functions like this a lot, it might be worth your while to learn them. A problem with metaclasses is when you have intermediate subclasses that are not meant to be registered, but the metaclass applies equally to all of them. -- Gabriel Genellina Hi Gabriel, Carl, Thank you very much for your help. I never knew about metaclassess and class decorators. Thank you again. I am actually inclined towards the straightforward way (1). But still one of the issues that I have mentioned in the first mail remains. How do I actually hit the code because my entry point is the EN module. Importing every module in EN module so that it hits the code atleast once is fraught with danger because later, someone might delete it to clean it up and will start facing issues. Could you give me some pointers in such a case? Regards K -- http://mail.python.org/mailman/listinfo/python-list
Iterating over objects of a class
Hi, How can I iterate over all the objects of a class? I wrote the code like following: class baseClass(object): __registry = [] def __init__(self, name): self.__registry.append(self) self.name = name def __iter__(self): baseClass.item = 0 return self.__registry[0] def next(self): if baseClass.item = len(self.__registry): raise StopIteration baseClass.item += 1 return self.__registry[baseClass.item - 1] For testing, create the following objects- a = baseClass(Test1) b = baseClass(Test2) class subClass (baseClass): pass c = subClass(Test3) Actual Iteration for i in a: print i.name Test1 Test2 Test3 --- I see the following problems in the code: 1. I have to iterate over any of the objects. For correctness, I wanted to iterate over the class, like for i in baseClass(): do x but that will will create one more object - which I do not want. 2. If the subclass wants to do somethings in its constructor, I am not sure how to update the registry. class subClass (baseClass): def __init__(self, name): **do something** super.init(self, name) This errors out, saying it needs super, not subClass Another method I thought of implementing it was using generators - where-in baseClass.objects() is a generator which will yield the objects one by one - but even then the second issue remains. If somebody can help me out, I would be very thankful. Regards K -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterating over objects of a class
On Dec 24, 10:52 pm, Diez B. Roggisch de...@nospam.web.de wrote: Kottiyath schrieb: Hi, How can I iterate over all the objects of a class? I wrote the code like following: class baseClass(object): Consider adopting PEP 8 coding conventions. __registry = [] def __init__(self, name): self.__registry.append(self) self.name = name def __iter__(self): baseClass.item = 0 return self.__registry[0] def next(self): if baseClass.item = len(self.__registry): raise StopIteration baseClass.item += 1 return self.__registry[baseClass.item - 1] For testing, create the following objects- a = baseClass(Test1) b = baseClass(Test2) class subClass (baseClass): pass c = subClass(Test3) Actual Iteration for i in a: print i.name Test1 Test2 Test3 --- I see the following problems in the code: 1. I have to iterate over any of the objects. For correctness, I wanted to iterate over the class, like for i in baseClass(): do x but that will will create one more object - which I do not want. 2. If the subclass wants to do somethings in its constructor, I am not sure how to update the registry. class subClass (baseClass): def __init__(self, name): **do something** super.init(self, name) This errors out, saying it needs super, not subClass You don't show the actual traceback, however the idiom for invoking super for new-style-classes is super(subClass, self).__init__(name) for your case. Another method I thought of implementing it was using generators - where-in baseClass.objects() is a generator which will yield the objects one by one - but even then the second issue remains. If somebody can help me out, I would be very thankful. Using a generator or not isn't the issue here. What you need is a *class*-based access, not instance-based. There are various methods to accomplish this. The simplest is to ditch the obnoxious __registry as name, and just do class BaseClass(object): REGISTRY = [] Then iterating is a simple matter of for instance in BaseClass.REGISTRY: ... Case solved. Alternatively, if you insist on the concept of privacy for that registry, you can use a classmethod: class BaseClass(object): @classmethod def registry(cls): for i in cls.__registry: yield i Last but not least you *could* go for a __metaclass__ with an __getitem__-method, that makes thinks look fancy because you then can do: for instance in BaseClass: ... I leave it as an exercise to you - gotta go christmas dining now :) Diez Thank you Very much, Diez. I was able to do the Generator and the super part of it, but I never even thought of the metaclass option. I will try it out. Thank you very much. Merry Christmas. P.S - Also, I will use the PEP 8 coding conventions -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterating over objects of a class
On Dec 24, 11:04 pm, MRAB goo...@mrabarnett.plus.com wrote: Diez B. Roggisch wrote: Kottiyath schrieb: Hi, How can I iterate over all the objects of a class? I wrote the code like following: class baseClass(object): Consider adopting PEP 8 coding conventions. __registry = [] def __init__(self, name): self.__registry.append(self) self.name = name def __iter__(self): baseClass.item = 0 return self.__registry[0] def next(self): if baseClass.item = len(self.__registry): raise StopIteration baseClass.item += 1 return self.__registry[baseClass.item - 1] For testing, create the following objects- a = baseClass(Test1) b = baseClass(Test2) class subClass (baseClass): pass c = subClass(Test3) Actual Iteration for i in a: print i.name Test1 Test2 Test3 --- I see the following problems in the code: 1. I have to iterate over any of the objects. For correctness, I wanted to iterate over the class, like for i in baseClass(): do x but that will will create one more object - which I do not want. 2. If the subclass wants to do somethings in its constructor, I am not sure how to update the registry. class subClass (baseClass): def __init__(self, name): **do something** super.init(self, name) This errors out, saying it needs super, not subClass You don't show the actual traceback, however the idiom for invoking super for new-style-classes is super(subClass, self).__init__(name) for your case. Another method I thought of implementing it was using generators - where-in baseClass.objects() is a generator which will yield the objects one by one - but even then the second issue remains. If somebody can help me out, I would be very thankful. Using a generator or not isn't the issue here. What you need is a *class*-based access, not instance-based. There are various methods to accomplish this. The simplest is to ditch the obnoxious __registry as name, and just do class BaseClass(object): REGISTRY = [] Then iterating is a simple matter of for instance in BaseClass.REGISTRY: ... Case solved. Alternatively, if you insist on the concept of privacy for that registry, you can use a classmethod: class BaseClass(object): �...@classmethod def registry(cls): for i in cls.__registry: yield i Last but not least you *could* go for a __metaclass__ with an __getitem__-method, that makes thinks look fancy because you then can do: for instance in BaseClass: ... I leave it as an exercise to you - gotta go christmas dining now :) The other thing to remember is that because the 'registry' contains references to the instances, they won't be garbage collected. Is there any other way out in this case? I have factory methods - and I have to loop over them - sort of Chain of Responsibility pattern. Having a registry inside the class instance and looping through them was the only clean thing I could think of. I understand that garbage collection would be an issue - but is there any way out? -- http://mail.python.org/mailman/listinfo/python-list
Re: Iterating over objects of a class
On Dec 24, 11:48 pm, Gabriel Genellina gagsl-...@yahoo.com.ar wrote: En Wed, 24 Dec 2008 16:18:55 -0200, Kottiyath n.kottiy...@gmail.com escribió: The other thing to remember is that because the 'registry' contains references to the instances, they won't be garbage collected. Is there any other way out in this case? I have factory methods - and I have to loop over them - sort of Chain of Responsibility pattern. Having a registry inside the class instance and looping through them was the only clean thing I could think of. I understand that garbage collection would be an issue - but is there any way out? You may keep all that structures - just use weak references (see the weakref module). There isn't a WeakList nor WeakSet out-of-the-box but you may use a WeakKeyDictionary (set the value to anything, None by example). -- Gabriel Genellina Thank you very much, Gabriel. I am very thankful to everyone. -- http://mail.python.org/mailman/listinfo/python-list
Event Driven programming - Doubts
Hi, I have been looking at Twisted and lately Circuits as examples for event driven programming in Python. Even though I understood how to implement the code in these and what is deferred etc, I have not yet understood the implementation of deferred. I went through a lot of tutorials, but I guess most places they expect that the user already understands how events are generated. The tutorials mention that there is no more threads once twisted is used. My question is as follows: I have not understood how the callbacks are hit without (a) blocking the code or (b) having new threads. The usual example given is that of a program waiting for data coming through a socket. In the tutorials, it is mentioned that -in an event driven program, we schedule the code to hit when the remote server gets back to us - . Now, my question is - somebody has to still wait on that socket and check whether the data is received, and once all the data is received, call the appropriate callbacks. Is twisted creating a micro-thread which just waits on the socket and once the data is received, calls callFromThread for it to run on the main loop? If so, Even though data locking etc is not a problem, are we not still having threads? Will it not still cause scalability problems in high traffic? If not, could somebody let me know how it is done? -- http://mail.python.org/mailman/listinfo/python-list
Twisted for non-networking applications
Hi all, Is it a good idea to use Twisted inside my application, even though it has no networking part in it? Basically, my application needs lots of parallel processing - but I am rather averse to using threads - due to myraid issues it can cause. So, I was hoping to use a reactor pattern to avoid the threads. I am using twisted in another part of the application for networking, so I was hoping to use the same for the non-networking part for reusing the reactor pattern. If somebody can help me on this, it would be very helpful. Regards K -- http://mail.python.org/mailman/listinfo/python-list
Can anyone suggest a good HTTP/1.1 web client?
Hi all, I have to connect to a secure website every second to get the data and then post to it. I have been investigating on many web clients in python, but nothing fits the bill properly. The ones I tried implementing are: 1. httplib based - I created myself. (I cannot use urllib2 since I have to transfer files, and urllib2 doesnt have multipart content-type support) 2. Twisted web client. I also looked at mechanize etc too. The problems I face are - 1. I liked twisted a lot, but when I implemented it, I found that client support is there only for twisted.web and not twisted.web2. Since I connect to the same website every time, I would like to have persistent connections and since twisted.web is HTTP/1.0, persistent connection support is not yet there. Without persistent connections, I would have to have TCP connection handshake everytime and it is taking too much time. 2. Since I connect to the website every second, I have to have many connections running at the same time. I am worried that creating threads for each connection is going to be a big problem (esp if the server response is slow), since the processor will get swamped - especially since there are many other activities going on in the machine. 3. I would also like to pipe line the requests - esp if the response is slow. Other requirements: 1. HTTPS Support 2. Connection through proxy. Is there any good web client which I can use straight up? Or would I have to implement the whole thing myself? It looks like a big beast and I was wondering whether python provides it straight up. Regards K -- http://mail.python.org/mailman/listinfo/python-list
Re: Can anyone suggest a good HTTP/1.1 web client?
If you'd like to help out with the new Twisted HTTP client, that would be wonderful. Even if you can just try it out and report any problems you run into, that would be immensely helpful. Jean-Paul I would love to help out with the client. But, I am extremely tied up at the current moment. I would be able to provide any sort of meaningful contribution only after ~a month. I can surely try this out and upload any bugs in the ticket you mentioned. Also, is there any planned date for persistent connections? If not, that will be the first thing I will be working on after my assignment is over - since it increasingly looks like it is essential to my system (actually, any REST applications). Thank you very much, Jean-Paul. Regards, K -- http://mail.python.org/mailman/listinfo/python-list
__import__ problem
Hi all, When I try to import a module via __import__, I am facing ImportError. But, when I tried to import it via usual 'import', it worked fine. Please see below: try: import exact except: logging.exception('Error during importing') try: code = __import__('exact') except: logging.exception('Is it still happening?') The error is as: 2008-12-06 20:06:59,328 ERROR Is it still happening? Traceback (most recent call last): File C:\django\test\..\test\basic\views.py, line 166, in getValue code = __import__('exact') ImportError: No module named exact Could you please let me know why this is happening? I tried to __import__ 'sys etc, and it worked fine. Is it due to some issue in the path? Regards K -- http://mail.python.org/mailman/listinfo/python-list
Re: Good introductory book?
On Dec 3, 7:44 pm, Ken D'Ambrosio [EMAIL PROTECTED] wrote: Hi, all. I'm getting ready to do some projects in Python, and I've cut my teeth a little bit, but I've found the Learning|Programming Python books from O'Reilly to be more-or-less useless (to my surprise -- I'm usually an O'Reilly fan). I really, really like Python Essential Reference, but it's -- well, more of a reference than an intro. So, an introductory text that actually assumes some previous programming experience (as opposed to Learning Python which must be the most slowly-paced programming book ever) would be terrific. Thanks for your suggestions! -Ken Dive into python is a very good one. It is free too. http://diveintopython.org/ Try it out. If you want more of examples of how everything is done, then Python Cookbook is another one. You can get many recipes at http://code.activestate.com/recipes/langs/python/ too - the book is just selected recipes from this site. Regards K -- http://mail.python.org/mailman/listinfo/python-list
Reg: PIL2.4 Error: AttributeError: pixel_access
Hi all, I am facing the following problem in PIL 2.4: Code: img = ImageGrab.grab() img.save(image2.jpg) Error: img.save(image2.jpg) File C:\Python24\Lib\site-packages\PIL\Image.py, line 1372, in save self.load() File C:\Python24\Lib\site-packages\PIL\Image.py, line 599, in load return self.im.pixel_access(self.readonly) AttributeError: pixel_access When I googled this error, I saw that such an error could be because of some botched installation. So, I re-installed PIL. But still this error persists. The version is print Image.VERSION 1.1.6 I had installed PIL in Python2.5 earlier and had done image capture also. But I need to go back to Python2.4 (because pymedia exe is available for only python2.4) and now I am facing this issue. I am pretty new to application programming, so if someone can help me out, it would be very helpful. Regards, Kottiyath -- http://mail.python.org/mailman/listinfo/python-list
Re: Reg: PIL2.4 Error: AttributeError: pixel_access
On Dec 1, 11:20 pm, Kottiyath [EMAIL PROTECTED] wrote: Hi all, I am facing the following problem in PIL 2.4: Code: img = ImageGrab.grab() img.save(image2.jpg) Error: img.save(image2.jpg) File C:\Python24\Lib\site-packages\PIL\Image.py, line 1372, in save self.load() File C:\Python24\Lib\site-packages\PIL\Image.py, line 599, in load return self.im.pixel_access(self.readonly) AttributeError: pixel_access When I googled this error, I saw that such an error could be because of some botched installation. So, I re-installed PIL. But still this error persists. The version is print Image.VERSION 1.1.6 I had installed PIL in Python2.5 earlier and had done image capture also. But I need to go back to Python2.4 (because pymedia exe is available for only python2.4) and now I am facing this issue. I am pretty new to application programming, so if someone can help me out, it would be very helpful. Regards, Kottiyath Hi all, I could solve it myself. Just FI, if somebody else faces the same issue. The issue was that I had installed many other 3rd party tools. One of those (I havent found out which - later it is going to bite me ) has overridden the _imaging.pyd (in linux it would be _imaging.so) file in PythonXX/DLLs directory with its version. I deleted the _imaging.pyd and _imaginft.pyd from PythonXX/DLLs and now PIL takes the data from site-packages/PIL/_imaging.pyd itself. Regards K -- http://mail.python.org/mailman/listinfo/python-list