need hint for refactoring

Discussion in 'Python' started by GHUM, Aug 9, 2006.

  1. GHUM

    GHUM Guest

    I have a bunch of function like:

    def p2neufrage(_):
    """ create new element"""
    anfrage,ergebnis=getanfrage()
    if ergebnis.get("status","ok") == "ok":
    wert=anfrage["feld"]
    # do something
    # unique here


    ergebnis["innerHTML"]=..... something ....

    #
    return simplejson.dumps(ergebnis, skipkeys=False,
    ensure_ascii=False, check_circular=True, allow_nan=True)


    so, everywhere there is the same beginning:

    anfrage,ergebnis=getanfrage()

    I analyze some transmitted jason-document; check for errors

    then I take the values out of the request, process it and fill the
    slots of a result ("ergebnis") dictionary, which is returned.


    So the beginning and the end of the function is allways repeated. It
    would be great to factor it out ... i startet with that ...getanfrage()
    call.

    Is there anything more possible?

    Thanks for any hint

    Harald
    GHUM, Aug 9, 2006
    #1
    1. Advertising

  2. GHUM wrote:

    > I have a bunch of function like:
    >
    > def p2neufrage(_):
    > """ create new element"""
    > anfrage,ergebnis=getanfrage()
    > if ergebnis.get("status","ok") == "ok":
    > wert=anfrage["feld"]
    > # do something
    > # unique here
    >
    >
    > ergebnis["innerHTML"]=..... something ....
    >
    > #
    > return simplejson.dumps(ergebnis, skipkeys=False,
    > ensure_ascii=False, check_circular=True, allow_nan=True)
    >
    >
    > so, everywhere there is the same beginning:
    >
    > anfrage,ergebnis=getanfrage()
    >
    > I analyze some transmitted jason-document; check for errors
    >
    > then I take the values out of the request, process it and fill the
    > slots of a result ("ergebnis") dictionary, which is returned.
    >
    >
    > So the beginning and the end of the function is allways repeated. It
    > would be great to factor it out ... i startet with that ...getanfrage()
    > call.
    >
    > Is there anything more possible?


    Use a decorator, out of my head:

    def foo(f):
    def _w(*args, **kwargs):
    anfrage,ergebnis=getanfrage()
    new_args = (args[0],) + (anfrage, ergebnis) + args[1:]
    f(*new_args, **kwargs)
    return simplejson.dumps(ergebnis, skipkeys=False,
    ensure_ascii=False, check_circular=True, allow_nan=True)

    return _w

    Then do

    @foo
    def p2neufrage(_, anfrage, ergebnis):
    """ create new element"""
    if ergebnis.get("status","ok") == "ok":
    wert=anfrage["feld"]
    # do something
    # unique here
    ergebnis["innerHTML"]=..... something ....


    Diez
    Diez B. Roggisch, Aug 9, 2006
    #2
    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. Marek

    How to create a hint?

    Marek, Dec 27, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    1,539
    =?Utf-8?B?a2VybWl0?=
    Dec 27, 2004
  2. F. GEIGER
    Replies:
    0
    Views:
    246
    F. GEIGER
    Jun 8, 2004
  3. Podrzut_z_Laweczki

    need a hint

    Podrzut_z_Laweczki, Aug 5, 2006, in forum: C++
    Replies:
    5
    Views:
    383
    Jerry Coffin
    Aug 6, 2006
  4. n179911
    Replies:
    2
    Views:
    259
  5. Gerr
    Replies:
    4
    Views:
    123
Loading...

Share This Page