Newbie (but improving) - Passing a function name with parameters as a parameter

Discussion in 'Python' started by mosscliffe, May 10, 2007.

  1. mosscliffe

    mosscliffe Guest

    I am trying to time a function's execution, but I get 'TypeError:
    'bool' object is not callable' when I try to run it.

    I suspect it is my calling of 'timeloop' with the function name
    'lookup' and its associated variables or it could just be some stupid
    error on my part.

    function 'lookups' was working OK

    Any help will be appreciated.

    Thanks - Richard

    ===The offending Code =====


    #!/usr/bin/python

    def timeloop(dofunction,iters=10):
    import datetime
    print "->-> Start of test", "LOOPS=", iters,
    datetime.datetime.now().ctime()
    for x in xrange(iters):
    print x
    dofunction()
    print "<-<- End of test", "LOOPS=", iters,
    datetime.datetime.now().ctime()



    def lookup(recs,patterns):
    matchcount = 0
    pattcount = 0
    for patt in patterns:
    if matchcount < pattcount:
    break
    pattcount += 1
    for rec in recs:
    # print "PATT:", patt, " REC:", rec, " PATTCOUNT=", pattcount
    if patt in rec:
    matchcount +=1
    break
    # print"MATCHCOUNT=",matchcount, "PATTCOUNT=", pattcount
    if matchcount == pattcount:
    return True
    else:
    return False



    myrecs = ['This is a title for Brian', 'this is detail one for brian',
    'this is detail two for brian', 'this is another detail one for
    brian']

    test1 = ['one', 'nomatch']
    test2 = ['one', 'two']
    test3 = ['title', 'two', 'nomatcheither']

    mypatts = test1

    timeloop(lookup(myrecs,mypatts), 10)
    mosscliffe, May 10, 2007
    #1
    1. Advertising

  2. mosscliffe

    Asun Friere Guest

    Try again ...

    Just looking over your code quickly ... the function 'lookup' returns
    either True or False (a boolean) depending on whether matchcount ==
    pattcount. Then in the declaration of the function 'timeloop' this
    return value gets bound to 'dofunction.' The subsequent call
    'dofunction()' fails, because a boolean is not callable.

    Asun
    Asun Friere, May 10, 2007
    #2
    1. Advertising

  3. On 10 Mai, 10:27, mosscliffe <> wrote:
    > I am trying to time a function's execution,


    Do you know the timeit module ? : Tool for measuring execution time of
    small code snippets

    Steffen
    Steffen Oschatz, May 10, 2007
    #3
  4. mosscliffe

    Ant Guest

    As Stephan said, you can investigate the timeit module. If you want to
    test it your way, wrap up your function call in another function:

    On May 10, 9:27 am, mosscliffe <> wrote:
    ....
    > def timeloop(dofunction,iters=10):

    ....
    >
    > def lookup(recs,patterns):

    ....

    > myrecs = ...
    >

    def test1():
    lookup(myrecs, ['one', 'nomatch'])

    def test2():
    lookup(myrecs, ['one', 'two'])

    > timeloop(test1, 10)


    Using timeit:

    t = timeit.Timer("lookup(myrecs, ['one', 'nomatch'])", "from __main__
    import *")
    print t.timeit(10)

    --
    Ant.
    Ant, May 10, 2007
    #4
  5. mosscliffe

    mosscliffe Guest

    Many thanks. I think I see what you mean.

    I will try 'timeit' as well.

    Aren't examples wonderful ?

    On 10 May, 11:42, Ant <> wrote:
    > As Stephan said, you can investigate the timeit module. If you want to
    > test it your way, wrap up your function call in another function:
    >
    > On May 10, 9:27 am, mosscliffe <> wrote:
    > ...> def timeloop(dofunction,iters=10):
    > ...
    >
    > > def lookup(recs,patterns):

    >
    > ...
    >
    > > myrecs = ...

    >
    > def test1():
    > lookup(myrecs, ['one', 'nomatch'])
    >
    > def test2():
    > lookup(myrecs, ['one', 'two'])
    >
    > > timeloop(test1, 10)

    >
    > Using timeit:
    >
    > t = timeit.Timer("lookup(myrecs, ['one', 'nomatch'])", "from __main__
    > import *")
    > print t.timeit(10)
    >
    > --
    > Ant.
    mosscliffe, May 10, 2007
    #5
  6. mosscliffe

    Terry Reedy Guest

    Re: Newbie (but improving) - Passing a function name with parametersas aparameter

    "mosscliffe" <> wrote in message
    news:...

    Asun Friere pointed out the central flaw in your program. Since passing
    functions as arguments is an important concept, here, for any newbies who
    did not understand, is a restatement of the problem and the fix.

    | timeloop(lookup(myrecs,mypatts), 10)

    This does not pass the lookup function to timeloop. Rather it calls lookup
    and passes the boolean result. The attempt to 'call' that boolean gives

    |I am trying to time a function's execution, but I get 'TypeError:
    | 'bool' object is not callable' when I try to run it.

    | I suspect it is my calling of 'timeloop' with the function name
    | 'lookup' and its associated variables

    Yes. Make the following changes and all should be well.

    | def timeloop(dofunction,iters=10):

    def timeloop(func, args, iters=10)

    | import datetime
    | print "->-> Start of test", "LOOPS=", iters,
    | datetime.datetime.now().ctime()
    | for x in xrange(iters):
    | print x
    | dofunction()

    func(*args)

    | print "<-<- End of test", "LOOPS=", iters,
    | datetime.datetime.now().ctime()
    |
    | def lookup(recs,patterns):
    | matchcount = 0
    | pattcount = 0
    | for patt in patterns:
    | if matchcount < pattcount:
    | break
    | pattcount += 1
    | for rec in recs:
    | # print "PATT:", patt, " REC:", rec, " PATTCOUNT=", pattcount
    | if patt in rec:
    | matchcount +=1
    | break
    | # print"MATCHCOUNT=",matchcount, "PATTCOUNT=", pattcount
    | if matchcount == pattcount:
    | return True
    | else:
    | return False
    |
    |
    |
    | myrecs = ['This is a title for Brian', 'this is detail one for brian',
    | 'this is detail two for brian', 'this is another detail one for
    | brian']
    |
    | test1 = ['one', 'nomatch']
    | test2 = ['one', 'two']
    | test3 = ['title', 'two', 'nomatcheither']
    |
    | mypatts = test1
    |
    | timeloop(lookup(myrecs,mypatts), 10)

    timeloop(lookup, (myrecs, mypaths), 10)

    Terry Jan Reedy
    Terry Reedy, May 10, 2007
    #6
  7. mosscliffe

    MRAB Guest

    Re: Newbie (but improving) - Passing a function name with parameters as aparameter

    On May 10, 6:33 pm, "Terry Reedy" <> wrote:
    > "mosscliffe" <> wrote in message
    >
    > news:...
    >
    > Asun Friere pointed out the central flaw in your program. Since passing
    > functions as arguments is an important concept, here, for any newbies who
    > did not understand, is a restatement of the problem and the fix.
    >
    > | timeloop(lookup(myrecs,mypatts), 10)
    >
    > This does not pass the lookup function to timeloop. Rather it calls lookup
    > and passes the boolean result. The attempt to 'call' that boolean gives
    >
    > |I am trying to time a function's execution, but I get 'TypeError:
    > | 'bool' object is not callable' when I try to run it.
    >
    > | I suspect it is my calling of 'timeloop' with the function name
    > | 'lookup' and its associated variables
    >
    > Yes. Make the following changes and all should be well.
    >
    > | def timeloop(dofunction,iters=10):
    >
    > def timeloop(func, args, iters=10)
    >
    > | import datetime
    > | print "->-> Start of test", "LOOPS=", iters,
    > | datetime.datetime.now().ctime()
    > | for x in xrange(iters):
    > | print x
    > | dofunction()
    >
    > func(*args)
    >
    > | print "<-<- End of test", "LOOPS=", iters,
    > | datetime.datetime.now().ctime()
    > |
    > | def lookup(recs,patterns):
    > | matchcount = 0
    > | pattcount = 0
    > | for patt in patterns:
    > | if matchcount < pattcount:
    > | break
    > | pattcount += 1
    > | for rec in recs:
    > | # print "PATT:", patt, " REC:", rec, " PATTCOUNT=", pattcount
    > | if patt in rec:
    > | matchcount +=1
    > | break
    > | # print"MATCHCOUNT=",matchcount, "PATTCOUNT=", pattcount
    > | if matchcount == pattcount:
    > | return True
    > | else:
    > | return False
    > |
    > |
    > |
    > | myrecs = ['This is a title for Brian', 'this is detail one for brian',
    > | 'this is detail two for brian', 'this is another detail one for
    > | brian']
    > |
    > | test1 = ['one', 'nomatch']
    > | test2 = ['one', 'two']
    > | test3 = ['title', 'two', 'nomatcheither']
    > |
    > | mypatts = test1
    > |
    > | timeloop(lookup(myrecs,mypatts), 10)
    >
    > timeloop(lookup, (myrecs, mypaths), 10)
    >

    A smaller change would be:

    timeloop(lambda: lookup(myrecs,mypatts), 10)
    MRAB, May 10, 2007
    #7
    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. Boris Sargos
    Replies:
    3
    Views:
    396
    Boris Sargos
    Apr 23, 2004
  2. marc
    Replies:
    3
    Views:
    315
  3. Mister B
    Replies:
    8
    Views:
    516
    Nick Keighley
    Aug 26, 2010
  4. Florian Loitsch
    Replies:
    11
    Views:
    233
    Michael Winter
    Mar 15, 2005
  5. AzamSharp
    Replies:
    2
    Views:
    150
Loading...

Share This Page