Re: changing a var by reference of a list
Hi Bruno, Unfortunately SQLAlchemy will be too involved at this point I will have to rewrite a lot of code to remove my current DB solution and use that. Howerver I've learned from my mistake and the next project will use it, as it seems to be a nice way of mapping objects to databases.. I'v solved it by just sending back the record set from sqlite3 because I noticed sometimes a 1:1 mapping cannot be done from column value to variable anyway.. Thanks everyone, - Jorgen -- http://mail.python.org/mailman/listinfo/python-list
Re: changing a var by reference of a list
Jorgen Bodde a écrit : > Ok thanks, > > I will try this approach. The idea was that I could give a list to the > SQL execute command, so that the results coming back would > automatically be assigned to variables. > You may want to have a look at SQLAlchemy. -- http://mail.python.org/mailman/listinfo/python-list
Re: changing a var by reference of a list
"Jorgen Bodde" <[EMAIL PROTECTED]> wrote: > I will try this approach. The idea was that I could give a list to the > SQL execute command, so that the results coming back would > automatically be assigned to variables. > Don't forget that in Python a function can return multiple results (or rather can return a sequence). That means you can do something like: varA, varB = doSomething() where doSomething would execute your SQL and return two results. -- http://mail.python.org/mailman/listinfo/python-list
Re: changing a var by reference of a list
On Tue, 2007-05-08 at 15:40 +0200, Jorgen Bodde wrote: > Ok thanks, > > I will try this approach. The idea was that I could give a list to the > SQL execute command, so that the results coming back would > automatically be assigned to variables. It's not possible to do that exactly as stated. A function can not modify its caller's namespace. (There is probably some dirty trick that can do this anyway, but you don't want to use dirty tricks.) Also, I'm not sure I'd want a function to pollute my local namespace with any variables of its choice. How would I know that it won't overwrite any variable whose contents I need? (Don't try to answer, this is a rhetorical question!) Most DB-API implementations have a facility to return query results as dictionaries or "a bag full of attributes"-type objects. The syntax for achieving this varies between implementations, because this is a non-standard addition outside the DB-API spec. With InformixDB for example, it would look something like this: import informixdb conn = informixdb.connect("stores_demo") cur = conn.cursor(rowformat=informixdb.ROW_AS_OBJECT) cur.execute("select * from customer") for row in cur: print row.customer_num, row.company This allows accessing the result columns as attributes of a row object, which is 99% as convenient as having local variables assigned to the results, and its 15000% cleaner. I strongly suggest you find an equivalent mechanism to use with whatever database module you're using, since it's a fairly safe bet that you're not using Informix. We could help you find that mechanism if you told us what database and what DB-API module you're using. Best regards, -- Carsten Haese http://informixdb.sourceforge.net -- http://mail.python.org/mailman/listinfo/python-list
Re: changing a var by reference of a list
Ok thanks, I will try this approach. The idea was that I could give a list to the SQL execute command, so that the results coming back would automatically be assigned to variables. With regards, - Jorgen On 5/8/07, Diez B. Roggisch <[EMAIL PROTECTED]> wrote: > Jorgen Bodde wrote: > > > Hi, > > > > I am trying to simplify my code, and want to automate the assigning of > > variables I get back from a set. I was thinking of putting the > > variables I want changed in a list: > > > > L = [self._varA, self._varB ] > > > > self._varA is a variable I want to change when I pass L to a function. > > I know doing this; > > > > L[0] = 12 > > > > Will replace the entry self._varA with 12, but is there a way to > > indirectly change the value of self._varA, through the list, something > > like a by-reference in C++ or a pointer-pointer? > > No, there isn't. > > But you could do > > L = ['_varA'] > > for v in L: > setattr(self, v, value) > > Diez > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
Re: changing a var by reference of a list
Jorgen Bodde wrote: > Hi, > > I am trying to simplify my code, and want to automate the assigning of > variables I get back from a set. I was thinking of putting the > variables I want changed in a list: > > L = [self._varA, self._varB ] > > self._varA is a variable I want to change when I pass L to a function. > I know doing this; > > L[0] = 12 > > Will replace the entry self._varA with 12, but is there a way to > indirectly change the value of self._varA, through the list, something > like a by-reference in C++ or a pointer-pointer? No, there isn't. But you could do L = ['_varA'] for v in L: setattr(self, v, value) Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: changing a var by reference of a list
Jorgen Bodde wrote: > Hi, > > I am trying to simplify my code, and want to automate the assigning of > variables I get back from a set. I was thinking of putting the > variables I want changed in a list: > > L = [self._varA, self._varB ] > > self._varA is a variable I want to change when I pass L to a function. > I know doing this; > > L[0] = 12 > > Will replace the entry self._varA with 12, but is there a way to > indirectly change the value of self._varA, through the list, something > like a by-reference in C++ or a pointer-pointer? > > With regards, > - Jorgen You can make self._varA and self._varB instances of a class and assign a value. Not tested. class _var: pass self._varA=_var() self._varB=_var() L=[self._varA, self._varB] then in function (or method of a class instance): L[0].value=12 Another way is to use a class to pass everything: class _vars: def __init__(self, vars=None): if vars is not None: for varname, value in vars.items(): setattr(self, varname, value) return vars=_vars({'_varA': 0, '_varB': 0}) Then you can access: vars._varA vars._varB -Larry l -- http://mail.python.org/mailman/listinfo/python-list
changing a var by reference of a list
Hi, I am trying to simplify my code, and want to automate the assigning of variables I get back from a set. I was thinking of putting the variables I want changed in a list: L = [self._varA, self._varB ] self._varA is a variable I want to change when I pass L to a function. I know doing this; L[0] = 12 Will replace the entry self._varA with 12, but is there a way to indirectly change the value of self._varA, through the list, something like a by-reference in C++ or a pointer-pointer? With regards, - Jorgen -- http://mail.python.org/mailman/listinfo/python-list