__autoinit__ (Was: Proposal: reducing self.x=x; self.y=y; self.z=z boilerplate code)
Hello python-list, As I Understood, semantic may be next: def qwerty(a,a.i,b,b.i,f.j): pass Would work like: def qwerty(anonymous1,anonymous2,anonymous3,anonymous4,anonymous5): (a,a.i,b,b.i,f.j)=(anonymous1,anonymous2,anonymous3,anonymous4,anonymous5) del anonymous1 del anonymous2 del anonymous3 del anonymous4 del anonymous5 If so then I like it, because it more explicit than anything other and it is working right now. I just tryed: >>> class Stub: ... pass >>> def qwer(a1,a2,a3,a4,a5): ... (a,a.i,b,b.i,f['i'])=(a1,a2,a3,a4,a5) ... del a1 ... del a2 ... del a3 ... del a4 ... del a5 ... print (a,a.i,b,b.i,f['i']) >>> f={} >>> qwer(Stub(),1,Stub(),2,3) (<__main__.Stub instance at 0x00C49530>, 1, <__main__.Stub instance at 0x00C498C8>, 2, 3) So there are not too much for implement. Another question - named arguments. How I can assign to them? May be so: f={} def qwerty(a,a.i,f['i']): print (a,a.i,f['i']) qwerty(f['i']=3,a=Stub(),a.i=4) - ? and for __init__: class MyClass: def __init__(self,self.i,self.j,self.k): pass MyObject = MyClass(self.i=1,self.j=2,self.k=3) ? or may be there can be an aliase syntax? class MyClass: def __init__(self, self.i by i,self.j by j, self.k by k): pass MyObject = MyClass(i=1,j=2,k=3) -- Sokolov Yura [EMAIL PROTECTED] -- http://mail.python.org/mailman/listinfo/python-list
PEP-343 - Context Managment variant
I know I came after the battle. And I have just another sight on context managment. Simple Context Managment may look in Python 2.4.1 like this: Synhronized example: def Synhronised(lock,func): lock.acquire() try: func() finally: lock.release() lock=Lock() def Some(): local_var1=x local_var2=y local_var3=Z def Work(): global local_var3 local_var3=Here_I_work(local_var1,local_var2,local_var3) Synhronised(lock,Work) return asd(local_var3) FileOpenClose Example: def FileOpenClose(name,mode,func): f=file(name,mode) try: func(f) finally: f.close() def Another(name): local_var1=x local_var2=y local_var3=None def Work(f): global local_var3 local_var3=[[x,y(i)] for i in f] FileOpenClose(name,'r',Work) return local_var3 It was complicated because : 1. We must declare closure (local function) before using it 2. We must declare local vars, to which we wish assign in "global" statement 3. We cannot create variable local to outter function in closure, so we must create it before and declare in global So one can say: "that is because there're no block lambda". (I can be wrong) I think it is possible to introduce block-object in analogy to lambda-object (and function-object) It's difference from function: it has no true self local variables, all global(free) and local variables it steels from outter scope. And all local vars, introduced in block are really introduced in outter scope (i think, during parse state). So its global dicts and local dicts are realy corresponding dicts of outter scope. (Excuse my english) So, may be it would be faster than function call. I don't know. Syntax can be following: lock=Lock() def Some(): local_var1=x local_var2=y local_var3=Z Synhronised(lock,block) local_var3=Here_I_work(local_var1,local_var2,local_var3) return asd(local_var3) def Another(name): local_var1=x local_var2=y FileOpenClose(name,'r',block{f}) local_var3=[[x,y(i)] for i in f] return local_var3 And here is sample of returning value: def Foo(context,proc): context.enter() try: res=proc(context.x,context.y)*2 except Exception,Err: context.throw(Err) finally: context.exit() return res ... context=MyContext() ... def Bar(): result=Foo(context,block{x,y}) continue x+y return result It's idea was token from Ruby. But I think, good idea is good whatever it came from. It can be not Pythonic. -- http://mail.python.org/mailman/listinfo/python-list
PEP-343 - Context Managment variant
Sorry. I was wrong about "global" statement. Then examples seems to be even more intricate: def Synhronised(lock,func): lock.acquire() try: func() finally: lock.release() lock=Lock() def Here_I_work(*a): return 1 # Stub def asd(a): return a # Stub def Some(): local_var1=1 local_var2=2 local_var3=[None] def Work(): local_var3[0]=Here_I_work(local_var1,local_var2,local_var3) Synhronised(lock,Work) return asd(local_var3[0]) FileOpenClose Example: def FileOpenClose(name,mode,func): f=file(name,mode) try: func(f) finally: f.close() def Another(name): local_var3=[None] def Work(f): local_var3[0]=[[x,y(i)] for i in f] FileOpenClose(name,'r',Work) return local_var3[0] Here we must to declare local_var3 as array, though we using it as scalar. Following is from previous letter (with correction). Please do not be severe, I'm not great programmer and my English isn't A and even B graduated. I think it is possible to introduce(internally) block-object in analogy to lambda-object (and function-object). It's difference from function: it has no true self local variables, all global(free) and local variables it steels from outter scope. And all local vars, introduced in block are really introduced in outter scope (i think, during parse state). So its global dicts and local dicts are realy corresponding dicts of outter scope (so, may be block call would be faster than function call. I don't know) Syntax can be following: lock=Lock() def Some(): local_var1=1 local_var2=2 local_var3=3 Synhronised(lock,block): local_var3=Here_I_work(local_var1,local_var2,local_var3) return asd(local_var3) def Another(name): FileOpenClose(name,'r',block{f}): local_var3=[[x,y(i)] for i in f] return local_var3 And here is sample of returning value: def Foo(context,proc): context.enter() try: res=proc(context.x,context.y)*2 except Exception,Err: context.throw(Err) finally: context.exit() return res ... context=MyContext() ... def Bar(): result=Foo(context,block{x,y}): continue x+y return result The idea was stolen from Ruby. My be it is not Pythonic. -- http://mail.python.org/mailman/listinfo/python-list
python web framework for straight sql (no ORM)?
Can someone please recommend a python web app framework which doesn't abstract away sql in favor of python objects? As far as I can tell, frameworks such as django, turbo gears, etc. allow one to drop down the the sql layer, however that is not the most effecient use of the framework. I would like to use sql (similar to how JSP's JSTL provides tag libraries for sql), yet not have to build my own components such as: site administration, user registration, authentication, rss feeds, etc. (I guess I'm thinking of a Django with straight db-api database interface). Is there such a framework? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
newbie: working iwth list of tuples
Hi All, I am fairly new to Python (less than a week). My goal is to write a small prototype of a database. Rather than build it using the typical method where one provides selection, projection, aggregation, union, intersection, etc. functions, I would like to do it in a more 'functional' style (executing a database query by using map, reduce, filter, etc. I am curious what the best way is to work with a list of tuples. For example, if I have the following list: [('a',1),('a',1),('a',3),('b',1),('b',2),('c',2),('c',3),('c',4)] how do I apply a map to each item in each tuple in the list? What if I want to filter based on both elements (the number AND the letter)? The most obvious solution is just to provide a function or a lambda expression whcih does what I want. However, since this is, eventually, going to be a database, the user will decide at run time what functions and operations should be run on a 'table' (really a list of tuples) ... and the structure of the 'table' will obviously not be known until run-time. I didn't find any examples of using idioms from functional programming beyond basic explanation of reduce and map (I have a couple of Haskell and ML books, but I know those languages even less than Python). I hope you understand the problem. Thanks in advance. -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: working iwth list of tuples
I forgot to add that I passing a tuple of functions to the reduce function but apparently that is not allowed. My guess was that a tuple made up of individual (simple) functions might be easier to manipulate programatically than a function which has to know the structure of a list. (My test code) x=[('a',1),('a',1),('a',3),('b',1),('b',2),('c',2),('c',3),('c',4)] reduce((lambda x,y: x+y,lambda x,y: x+y),x) -- http://mail.python.org/mailman/listinfo/python-list
Re: newbie: working iwth list of tuples
Wow, great answers here. Thank you all for the links and examples, I'm going through them all now. -- http://mail.python.org/mailman/listinfo/python-list
time series calculation in list comprehension?
Is there a way I can do time series calculation, such as a moving average in list comprehension syntax? I'm new to python but it looks like list comprehension's 'head' can only work at a value at a time. I also tried using the reduce function and passed in my list and another function which calculates a moving average outside the list comp. ... but I'm not clear how to do it. Any ideas? Thanks. -- http://mail.python.org/mailman/listinfo/python-list
Re: time series calculation in list comprehension?
Wow, thanks for all the reponses. Very helpful! -- http://mail.python.org/mailman/listinfo/python-list