unknown function 'build'

This is the place for queries that don't fit in any of the other categories.

unknown function 'build'

Postby eightbits » Mon Jul 01, 2013 9:53 pm

I am a newbee and learning to use Python (2.7.3) and using IDEL as the IDE. I was searching for a solution to print currency. I did locate an example that works and in the process of studying the code
there is a function “build' that I can not locate any reference to. The example code, which depends
on importing 'decimal' works very well at least in the simple test runs.
The example code (moneyfmt) uses the function build in a manner such as build(dp) as one example.
I am thinking that this must be a function. But I have no idea where it is as I don't import any thing
but the decimal class (from decimal import *). If it can help, I can post the code.
Thanks in advance to any comments.
eightbits
 
Posts: 19
Joined: Sun May 19, 2013 2:42 am

Re: unknown function 'build'

Postby Yoriz » Mon Jul 01, 2013 10:26 pm

eightbits wrote:If it can help, I can post the code.

Post the code, without it i dont think anyone can answer your question ;) .
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 837
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: unknown function 'build'

Postby eightbits » Mon Jul 01, 2013 11:51 pm

Code: Select all
from decimal import *
def moneyfmt(value, places=2, curr='', sep=',', dp='.',
             pos='', neg='-', trailneg=''):
    """Convert Decimal to a money formatted string.

    places:  required number of places after the decimal point
    curr:    optional currency symbol before the sign (may be blank)
    sep:     optional grouping separator (comma, period, space, or blank)
    dp:      decimal point indicator (comma or period)
             only specify as blank when places is zero
    pos:     optional sign for positive numbers: '+', space or blank
    neg:     optional sign for negative numbers: '-', '(', space or blank
    trailneg:optional trailing minus indicator:  '-', ')', space or blank

    >>> d = Decimal('-1234567.8901')
    >>> moneyfmt(d, curr='$')
    '-$1,234,567.89'
    >>> moneyfmt(d, places=0, sep='.', dp='', neg='', trailneg='-')
    '1.234.568-'
    >>> moneyfmt(d, curr='$', neg='(', trailneg=')')
    '($1,234,567.89)'
    >>> moneyfmt(Decimal(123456789), sep=' ')
    '123 456 789.00'
    >>> moneyfmt(Decimal('-0.02'), neg='<', trailneg='>')
    '<0.02>'

    """
# code starts from here ......
    q = Decimal(10) ** -places      # 2 places --> '0.01'
    sign, digits, exp = value.quantize(q).as_tuple()
    result = []
    digits = map(str, digits)
    build, next = result.append, digits.pop
    if sign:
        build(trailneg)
    for i in range(places):
        build(next() if digits else '0')
    build(dp)
    if not digits:
        build('0')
    i = 0
    while digits:
        build(next())
        i += 1
        if i == 3 and digits:
            i = 0
            build(sep)
    build(curr)
    build(neg if sign else pos)
    return ''.join(reversed(result))


So, what is the build(neg if sign else pos) , find nothing online that explains build ?????
eightbits
 
Posts: 19
Joined: Sun May 19, 2013 2:42 am

Re: unknown function 'build'

Postby Yoriz » Tue Jul 02, 2013 12:06 am

Build is assigned in this line
Code: Select all
build, next = result.append, digits.pop
Each time a call is made using build ie build(something) its the same as making a call result.append(something)

P.S. This way of importing is bad news and is why you have no clue if build came from decimal or not.
Code: Select all
from decimal import *

see this thread for details http://www.python-forum.org/viewtopic.php?f=12&t=4290#p5317
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 837
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: unknown function 'build'

Postby eightbits » Tue Jul 02, 2013 12:58 am

OK, thanks for the information. BTW, I did navigate to the suggested URL and I think I got some of the story. It did reference
TKinter . I am thinking just use:
Code: Select all
import decimal


I will try that in the mean time and post if I get different or 'bad' results. :)

Also, I don't find any details on the 'build' ststement? SHould it not be in the doc's somewhere? :?:
eightbits
 
Posts: 19
Joined: Sun May 19, 2013 2:42 am

Re: unknown function 'build'

Postby Yoriz » Tue Jul 02, 2013 5:52 am

Build will not be found in the documentaion, its local to moneyfmt's namespace, it was asigned to something in the following line like, i said in my previous post.
Code: Select all
build, next = result.append, digits.pop


i could change it to
Code: Select all
imlocal, next = result.append, digits.pop
and then everywhere build was used , use imlocal instead.
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 837
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: unknown function 'build'

Postby eightbits » Wed Jul 03, 2013 4:31 pm

OK, I think I got it somewhat. :idea:
I will continue my journey into using Python. This seems like a good way to program in a Linux(Ubuntu) environment.
I have noticed that getting easier to understand documentation takes a persistence effort. I did purchase the book
'Learning Python' by Mark Lutz and it seems rather compleat. I am looking for a book that involves some example application type code to reference the tutorial content.
Once again, many thanks for you explanation :D
I am sure I will refer to this web site/user forum many times.
eightbits
 
Posts: 19
Joined: Sun May 19, 2013 2:42 am


Return to General Coding Help

Who is online

Users browsing this forum: buran, W3C [Linkcheck] and 5 guests