Editing a function in-memory and in-place

Discussion in 'Python' started by Sori Schwimmer, Apr 27, 2006.

  1. Generate your function as a string. Be careful to
    indent correctly and append \n at line's end. Then
    execute the string with exec(name_of_string). Then
    edit your string as necessary, and execute again.

    An example follows, directly from one of my projects:

    # create a "function" to apply on prtbl
    if d1[j][3]==';': # if value >
    prfunc=('l_vl=0\nfor l_i in range(%s):\n if
    l_vl>%s:' % (len(prtbl),lowm))
    elif lowm>0.0:
    prfunc=('for l_i in range(%s' % lowm)
    if hiwm>0.0:
    prfunc+=(',%s):' % hiwm)
    else:
    prfunc+=(',%s):' % len(prtbl))
    else:
    prfunc=('for l_i in range(%s):' %
    len(prtbl))
    if d1[j][4]=='-':
    prfunc+='\n prtbl[l_i]=prtbl[l_i-1]'
    if d1[j][1]=='$' and d1[j][2]==chr(255):
    prfunc+=('\n prtbl[l_i]=%s' % dsz)
    elif d1[j][1]=='$':
    prfunc+=('\n prtbl[l_i]=prtbl[l_i-1]-%s' %
    dsz)
    elif d1[j][1]=='%':
    prfunc+=('\n prtbl[l_i]*=(1-%s/100)' % dsz)
    if d1[j][3]==';': # if value >
    prfunc+='\n l_vl+=prtbl[l_i]'
    #print prfunc
    #print prtbl
    exec(prfunc)

    Hope it helps,
    Sorin Schwimmer

    __________________________________________________
    Do You Yahoo!?
    Tired of spam? Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com
     
    Sori Schwimmer, Apr 27, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kai

    Datagrid in-place editing

    kai, Oct 30, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    455
  2. Ian Bicking
    Replies:
    4
    Views:
    345
    Ian Bicking
    Apr 28, 2006
  3. Replies:
    0
    Views:
    590
  4. Hrvoje Vrbanc
    Replies:
    9
    Views:
    833
    Ronelle Griffiths
    Sep 16, 2008
  5. Xavier Hanin
    Replies:
    11
    Views:
    207
    Mauricio Fernandez
    Jul 18, 2006
Loading...

Share This Page