decimal and context objects!

Discussion in 'Python' started by MooMaster, Mar 2, 2007.

  1. MooMaster

    MooMaster Guest

    Hey guys, I'm trying to do some black magic voodoo and it's a little
    late, so forgive me if this question seems obvious or has been asked
    before. I tried doing a search on context objects and didn't find
    anything that popped out, and I'm too tired to keep digging.

    I'm making a little program that is trying to do weird and sexy things
    by fully leveraging the power of all the built-in beauty of Python. I
    was trying to play around with the new features added into Python 2.5,
    and ran into an unexpected issue...check this out:

    >>> moo = lambda x, y : decimal.Context(3).sqrt(decimal.Context(3).power(x,2) + decimal.Context(3).power(y,2))
    >>> moo

    <function <lambda> at 0x02CD0EB0>
    >>> row = [1,2,3,4,5]
    >>> weight_vector = .00556
    >>> moo(sum(row), weight_vector)

    Traceback (most recent call last):
    File "<pyshell#5>", line 1, in <module>
    moo(sum(row), weight_vector)
    File "<pyshell#1>", line 1, in <lambda>
    moo = lambda x, y :
    decimal.Context(3).sqrt(decimal.Context(3).power(x,2) +
    decimal.Context(3).power(y,2))
    File "C:\Python25\lib\decimal.py", line 2662, in power
    return a.__pow__(b, modulo, context=self)
    TypeError: wrapper __pow__ doesn't take keyword arguments

    I have no idea what keyword argument is getting passed to __pow__,
    anyone know what's going on?

    This should compute sqrt(5^2 + 3^2)
    MooMaster, Mar 2, 2007
    #1
    1. Advertising

  2. MooMaster

    MooMaster Guest

    On Mar 2, 3:08 am, "MooMaster" <> wrote:
    > Hey guys, I'm trying to do some black magic voodoo and it's a little
    > late, so forgive me if this question seems obvious or has been asked
    > before. I tried doing a search on context objects and didn't find
    > anything that popped out, and I'm too tired to keep digging.
    >
    > I'm making a little program that is trying to do weird and sexy things
    > by fully leveraging the power of all the built-in beauty of Python. I
    > was trying to play around with the new features added into Python 2.5,
    > and ran into an unexpected issue...check this out:
    >
    > >>> moo = lambda x, y : decimal.Context(3).sqrt(decimal.Context(3).power(x,2) + decimal.Context(3).power(y,2))
    > >>> moo

    >
    > <function <lambda> at 0x02CD0EB0>>>> row = [1,2,3,4,5]
    > >>> weight_vector = .00556
    > >>> moo(sum(row), weight_vector)

    >
    > Traceback (most recent call last):
    > File "<pyshell#5>", line 1, in <module>
    > moo(sum(row), weight_vector)
    > File "<pyshell#1>", line 1, in <lambda>
    > moo = lambda x, y :
    > decimal.Context(3).sqrt(decimal.Context(3).power(x,2) +
    > decimal.Context(3).power(y,2))
    > File "C:\Python25\lib\decimal.py", line 2662, in power
    > return a.__pow__(b, modulo, context=self)
    > TypeError: wrapper __pow__ doesn't take keyword arguments
    >
    > I have no idea what keyword argument is getting passed to __pow__,
    > anyone know what's going on?
    >
    > This should compute sqrt(5^2 + 3^2)



    Oh sorry, ignore that last line, that was a copy/paste from another
    example I forgot to remove...OBVIOUSLY it's going to compute something
    else, that's not what I'm asking about...stupid late night hacking! XD
    MooMaster, Mar 2, 2007
    #2
    1. Advertising

  3. MooMaster

    Peter Otten Guest

    MooMaster wrote:

    > Hey guys, I'm trying to do some black magic voodoo and it's a little
    > late, so forgive me if this question seems obvious or has been asked
    > before. I tried doing a search on context objects and didn't find
    > anything that popped out, and I'm too tired to keep digging.
    >
    > I'm making a little program that is trying to do weird and sexy things
    > by fully leveraging the power of all the built-in beauty of Python. I
    > was trying to play around with the new features added into Python 2.5,
    > and ran into an unexpected issue...check this out:
    >
    >>>> moo = lambda x, y :
    >>>> decimal.Context(3).sqrt(decimal.Context(3).power(x,2) +
    >>>> decimal.Context(3).power(y,2)) moo

    > <function <lambda> at 0x02CD0EB0>
    >>>> row = [1,2,3,4,5]
    >>>> weight_vector = .00556
    >>>> moo(sum(row), weight_vector)

    > Traceback (most recent call last):
    > File "<pyshell#5>", line 1, in <module>
    > moo(sum(row), weight_vector)
    > File "<pyshell#1>", line 1, in <lambda>
    > moo = lambda x, y :
    > decimal.Context(3).sqrt(decimal.Context(3).power(x,2) +
    > decimal.Context(3).power(y,2))
    > File "C:\Python25\lib\decimal.py", line 2662, in power
    > return a.__pow__(b, modulo, context=self)
    > TypeError: wrapper __pow__ doesn't take keyword arguments
    >
    > I have no idea what keyword argument is getting passed to __pow__,
    > anyone know what's going on?


    Weird implementation hacks distorting error messages :)
    Context.power() expects a Decimal instance as its first argument:

    >>> from decimal import *
    >>> ctx = getcontext()
    >>> ctx.power(10, 2)

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/local/lib/python2.5/decimal.py", line 2662, in power
    return a.__pow__(b, modulo, context=self)
    TypeError: wrapper __pow__ doesn't take keyword arguments
    >>> ctx.power(Decimal("10"), 2)

    Decimal("100")

    Peter
    Peter Otten, Mar 2, 2007
    #3
    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. Ven
    Replies:
    3
    Views:
    1,320
  2. Gilbert Fine
    Replies:
    8
    Views:
    897
    Zentrader
    Aug 1, 2007
  3. Vitaliy
    Replies:
    1
    Views:
    472
    Peter Otten
    May 29, 2008
  4. valpa
    Replies:
    11
    Views:
    1,512
    Steven D'Aprano
    Mar 24, 2009
  5. Norah Jones
    Replies:
    0
    Views:
    71
    Norah Jones
    Apr 7, 2014
Loading...

Share This Page