Re: Dynamic variable creation from string

2011-12-13 Thread 88888 Dihedral
On Tuesday, December 13, 2011 9:35:52 AM UTC+8, Steven D'Aprano wrote: > On Mon, 12 Dec 2011 15:45:06 -0800, alex23 wrote: > > > On Dec 12, 10:49 pm, 8 Dihedral > > wrote: > >> This is the way to write an assembler or to roll out a script language > >> to be included in an app by users. > >

Re: Dynamic variable creation from string

2011-12-12 Thread Steven D'Aprano
On Mon, 12 Dec 2011 15:45:06 -0800, alex23 wrote: > On Dec 12, 10:49 pm, 8 Dihedral > wrote: >> This is the way to write an assembler or to roll out a script language >> to be included in an app by users. > > This is a garbage comment that has absolutely nothing to do with the > topic at han

Re: Dynamic variable creation from string

2011-12-12 Thread alex23
On Dec 12, 10:49 pm, 8 Dihedral wrote: > This is the way to write an assembler or > to roll out a script language to be included in an app > by users. This is a garbage comment that has absolutely nothing to do with the topic at hand _at all_. -- http://mail.python.org/mailman/listinfo/pytho

Re: Dynamic variable creation from string

2011-12-12 Thread 88888 Dihedral
On Monday, December 12, 2011 3:11:18 PM UTC+8, alex23 wrote: > On Dec 8, 3:09 am, Massi wrote: > > in my script I have a dictionary whose items are couples in the form > > (string, integer values), say > > > > D = {'a':1, 'b':2, 'c':3} > > > > This dictionary is passed to a function as a parameter

Re: Dynamic variable creation from string

2011-12-12 Thread 88888 Dihedral
On Monday, December 12, 2011 3:11:18 PM UTC+8, alex23 wrote: > On Dec 8, 3:09 am, Massi wrote: > > in my script I have a dictionary whose items are couples in the form > > (string, integer values), say > > > > D = {'a':1, 'b':2, 'c':3} > > > > This dictionary is passed to a function as a parameter

Re: Dynamic variable creation from string

2011-12-12 Thread alex23
On Dec 8, 3:09 am, Massi wrote: > in my script I have a dictionary whose items are couples in the form > (string, integer values), say > > D = {'a':1, 'b':2, 'c':3} > > This dictionary is passed to a function as a parameter, e.g. : > > def Sum(D) : >     return D['a']+D['b']+D['c'] > > Is there a

Re: Dynamic variable creation from string

2011-12-11 Thread Ethan Furman
alex23 wrote: On Dec 11, 4:42 pm, Nobody wrote: If just you're trying to avoid getting a repetitive strain injury in your right-hand little finger from typing all the [''], you could turn the keys into object attributes, e.g.: class DictObject: def __init__(self, d):

Re: Dynamic variable creation from string

2011-12-11 Thread alex23
On Dec 11, 4:42 pm, Nobody wrote: > If just you're trying to avoid getting a repetitive strain injury in your > right-hand little finger from typing all the [''], you could turn > the keys into object attributes, e.g.: > >         class DictObject: >             def __init__(self, d): >          

Re: Dynamic variable creation from string

2011-12-10 Thread Nobody
On Fri, 09 Dec 2011 01:55:28 -0800, Massi wrote: > Thank you all for your replies, first of all my Sum function was an > example simplifying what I have to do in my real funciton. In general > the D dictionary is complex, with a lot of keys, so I was searching > for a quick method to access all th

Re: Dynamic variable creation from string

2011-12-09 Thread Ethan Furman
Massi wrote: Thank you all for your replies, first of all my Sum function was an example simplifying what I have to do in my real funciton. In general the D dictionary is complex, with a lot of keys, so I was searching for a quick method to access all the variables in it without doing the explici

Re: Dynamic variable creation from string

2011-12-09 Thread Chris Angelico
On Fri, Dec 9, 2011 at 10:59 PM, Steven D'Aprano wrote: > (4) If you think you can make exec safe with a prohibited list of > dangerous strings, you probably can't. If you think that it's even _possible_ to make exec safe with a blacklist, I have a nice padded cell for you over here. Security is

Re: Dynamic variable creation from string

2011-12-09 Thread Steven D'Aprano
On Fri, 09 Dec 2011 11:59:16 +, Steven D'Aprano wrote: > Just the second-most common source of viruses, malware and security > vulnerabilities (behind buffer overflows): code injection attacks. Oops, I forgot to go back and revise this sentence. Code injection attacks are now the most common

Re: Dynamic variable creation from string

2011-12-09 Thread Steven D'Aprano
On Fri, 09 Dec 2011 01:55:28 -0800, Massi wrote: > for k in D : exec "%s = D[k]" %k > > That seems to do the trick, but someone speaks about "dirty code", can > anyone point me out which problems this can generate? Again, thank you > for your help! Just the second-most common source of viruses,

Re: Dynamic variable creation from string

2011-12-09 Thread Peter Otten
Massi wrote: > for k in D : exec "%s = D[k]" %k > > That seems to do the trick, but someone speaks about "dirty code", can > anyone point me out which problems this can generate? exec can run arbitrary code, so everybody reading the above has to go back to the definition of D to verify that it

Re: Dynamic variable creation from string

2011-12-09 Thread Massi
Thank you all for your replies, first of all my Sum function was an example simplifying what I have to do in my real funciton. In general the D dictionary is complex, with a lot of keys, so I was searching for a quick method to access all the variables in it without doing the explicit creation: a,

Re: Dynamic variable creation from string

2011-12-08 Thread Jussi Piitulainen
Terry Reedy writes: > On 12/7/2011 7:03 PM, Steven D'Aprano wrote: > > On Wed, 07 Dec 2011 09:09:16 -0800, Massi wrote: > > > >> Is there a way to create three variables dynamically inside Sum > >> in order to re write the function like this? > > I should have mentioned in my earlier response tha

Re: Dynamic variable creation from string

2011-12-07 Thread Chris Angelico
On Thu, Dec 8, 2011 at 11:03 AM, Steven D'Aprano wrote: >> It is really important that the scope of a,b,c is limited to the Sum >> function, they must not exisit outside it or inside any other nested >> functions. > > The second part is impossible, because that is not how Python works. > Nested fu

Re: Dynamic variable creation from string

2011-12-07 Thread Steven D'Aprano
On Wed, 07 Dec 2011 19:27:43 -0500, Terry Reedy wrote: > On 12/7/2011 7:03 PM, Steven D'Aprano wrote: >> On Wed, 07 Dec 2011 09:09:16 -0800, Massi wrote: >> >>> Is there a way to create three variables dynamically inside Sum in >>> order to re write the function like this? > > I should have menti

Re: Dynamic variable creation from string

2011-12-07 Thread Terry Reedy
On 12/7/2011 7:03 PM, Steven D'Aprano wrote: On Wed, 07 Dec 2011 09:09:16 -0800, Massi wrote: Is there a way to create three variables dynamically inside Sum in order to re write the function like this? I should have mentioned in my earlier response that 'variable' is a bit vague and mislead

Re: Dynamic variable creation from string

2011-12-07 Thread Steven D'Aprano
On Wed, 07 Dec 2011 09:09:16 -0800, Massi wrote: > Is there a way to create three variables dynamically inside Sum in order > to re write the function like this? > > def Sum(D) : > # Here some magic to create a,b,c from D > return a+b+c No magic is needed. a, b, c = D['a'], D['b'], D['

Re: Dynamic variable creation from string

2011-12-07 Thread Terry Reedy
On 12/7/2011 12:09 PM, Massi wrote: in my script I have a dictionary whose items are couples in the form (string, integer values), say D = {'a':1, 'b':2, 'c':3} This dictionary is passed to a function as a parameter, e.g. : def Sum(D) : return D['a']+D['b']+D['c'] Is there a way to crea

Re: Dynamic variable creation from string

2011-12-07 Thread MRAB
On 07/12/2011 17:45, John Gordon wrote: In Massi writes: in my script I have a dictionary whose items are couples in the form (string, integer values), say D = {'a':1, 'b':2, 'c':3} This dictionary is passed to a function as a parameter, e.g. : def Sum(D) : return D['a']+D['b']

Re: Dynamic variable creation from string

2011-12-07 Thread Chris Angelico
On Thu, Dec 8, 2011 at 4:09 AM, Massi wrote: > def Sum(D) : ># Here some magic to create a,b,c from D >return a+b+c Welcome to TMTOWTDI land! We do magic here... several different ways. You _may_ be able to do this, which is roughly equivalent to the extract() function in PHP: locals().

Re: Dynamic variable creation from string

2011-12-07 Thread Waldek M.
On Wed, 7 Dec 2011 09:09:16 -0800 (PST), Massi wrote: > def Sum(D) : > return D['a']+D['b']+D['c'] > > Is there a way to create three variables dynamically inside Sum in > order to re write the function like this? > > def Sum(D) : > # Here some magic to create a,b,c from D > return a+

Re: Dynamic variable creation from string

2011-12-07 Thread John Gordon
In Massi writes: > in my script I have a dictionary whose items are couples in the form > (string, integer values), say > D = {'a':1, 'b':2, 'c':3} > This dictionary is passed to a function as a parameter, e.g. : > def Sum(D) : > return D['a']+D['b']+D['c'] > Is there a way to create th

Dynamic variable creation from string

2011-12-07 Thread Massi
Hi everyone, in my script I have a dictionary whose items are couples in the form (string, integer values), say D = {'a':1, 'b':2, 'c':3} This dictionary is passed to a function as a parameter, e.g. : def Sum(D) : return D['a']+D['b']+D['c'] Is there a way to create three variables dynamic