accepts decorator

Discussion in 'Python' started by urielka, Sep 25, 2006.

  1. urielka

    urielka Guest

    i want to make a decorator that make a function accept only types,if
    the types are wrong but the number of types is equal to arguments try
    to convert them to the type.

    for example:

    @Accept(int,int)
    def Sum(a,b):
    return a+b

    print Sum("2",2.0)

    this should print 4,coz the decorator sees that there is the same
    amount of arguments in Accept and Sum,then it will try to cast them to
    the types(a to int and also b to int),then he will send them to the
    normal function so Sum will get a=2,b=2 instead of a="2"(string)
    b=2.0(float).
    i made this but it doesn`t work:
    def Accept(*types):
    def check_accepts(f):
    assert len(types) == f.func_code.co_argcount-1,"Not
    Enougth/Too much Types"
    Types=types
    def new_f(*args, **kwds):
    newArgs=[]
    for (a, t) in zip(kwds.values(), Types):
    try:
    newArgs.append(t(a))
    except:
    raise Exception("Convertion of %s to type %s
    failed" %(a,t))
    nargs=tuple(newArgs)
    return f(*nargs, **kwds)
    new_f.func_name = f.func_name
    return new_f
    return check_accepts

    it doesn`t work when i decorate a instance method.
    i get in *args the instance and in **kwds the arguments,but if it is
    just a function not a instance method i get in *args the arguments and
    in **kwds a empty dict.

    can someone also explain me what is * and ** ,and how they are called.

    Thanks,
    -Uriel Katz
     
    urielka, Sep 25, 2006
    #1
    1. Advertising

  2. urielka

    urielka Guest

    i think i got what the * and ** mean.
    *args mean arguments that are assigned by position(that is why *arg is
    a tuple)
    **kwds mean arguments that are assigned using equals a=2,b=3 (that is
    why **kwds i a dict)
    but why self is in *args and other thing goes to **kwds? even if not
    assigned using equals
     
    urielka, Sep 26, 2006
    #2
    1. Advertising

  3. At Monday 25/9/2006 21:58, urielka wrote:

    >i think i got what the * and ** mean.
    >*args mean arguments that are assigned by position(that is why *arg is
    >a tuple)
    >**kwds mean arguments that are assigned using equals a=2,b=3 (that is
    >why **kwds i a dict)
    >but why self is in *args and other thing goes to **kwds? even if not
    >assigned using equals


    For these elemental things you should start by reading the Python
    tutorial included with the documentation - it's easy.
    And for decorators, this is a good text
    http://www.phyast.pitt.edu/~micheles/python/documentation.html which
    supplements the Python docs.



    Gabriel Genellina
    Softlab SRL





    __________________________________________________
    Preguntá. Respondé. Descubrí.
    Todo lo que querías saber, y lo que ni imaginabas,
    está en Yahoo! Respuestas (Beta).
    ¡Probalo ya!
    http://www.yahoo.com.ar/respuestas
     
    Gabriel Genellina, Sep 26, 2006
    #3
  4. urielka

    urielka Guest

    hehe i saw that,that is what made my understand it.
    the decorator now works.
    let say i have a function decorated with two decorators:

    @Accept(int,int)
    @OtherDecorator
    def myfunc(a,b):
    pass

    how can i make the two decorators into one(note:eek:ne get parameters and
    the other doesn`t)
     
    urielka, Sep 26, 2006
    #4
    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. Egbert Nierop \(MVP for IIS\)

    Using viewstate Accepts data changes :<

    Egbert Nierop \(MVP for IIS\), Aug 20, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    361
    Egbert Nierop \(MVP for IIS\)
    Aug 20, 2003
  2. Replies:
    1
    Views:
    379
  3. =?Utf-8?B?SldoaXR0ZWQ=?=

    Custom Control That Accepts Inline Code

    =?Utf-8?B?SldoaXR0ZWQ=?=, Nov 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    382
    =?Utf-8?B?SldoaXR0ZWQ=?=
    Nov 2, 2004
  4. SHarris
    Replies:
    8
    Views:
    11,725
    clintonG
    Feb 15, 2005
  5. glomde
    Replies:
    5
    Views:
    531
    glomde
    Mar 29, 2007
Loading...

Share This Page