Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > need hint for refactoring

Reply
Thread Tools

need hint for refactoring

 
 
GHUM
Guest
Posts: n/a
 
      08-09-2006
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

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      08-09-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help in refactoring my python script n179911 Python 2 08-08-2009 05:09 PM
need a hint Podrzut_z_Laweczki C++ 5 08-06-2006 06:09 AM
First ruby code, need hint on iterator Gerr Ruby 4 07-17-2006 07:46 PM
PIX VPN debugging hint Walter Roberson Cisco 0 10-18-2005 04:56 AM
Need a hint on customizing import of modules F. GEIGER Python 0 06-08-2004 07:55 AM



Advertisments