#1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    Improved Val() Function


    I needed to improve my Val() function because the original was having some major issues. Here is my new copy:
    Code:
    def val(data):
        i_list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '$', '+', '-']
        start_char = [data[0]]
        end_char = [0]
        got_end = [False]
        for i in data:
            if got_end[0] == True:
                break
            else:
                if i not in i_list:
                    end_char[0] = i
                    got_end[0] = True
                else:
                    end_char[0] = data[-1]
                    got_end[0] = True
        if start_char[0] not in i_list:
            return 0
        else:
            if end_char[0] in i_list:
                return int(data)
            else:
                return int(data[data.find(str(start_char[0])):data.find(str(end_char[0]))])
    I would appreciate some comments on this code. Maybe where something could go wrong or a better way of arranging code. I know this can't be as good as it can get.
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Hi,
    I think you lost your way a bit with all the unnecessary list elements.
    end_char = [0]
    got_end = [False]
    are very redundant [] !!!

    You also don't handle the case where none of the data matches your format.

    The code allows any combination of digits +,- and $ in any quantity. If you want to check the format as well as get the number then regular expressions are probably the best way to go - just to prevent the code from getting too complicated to understand

    Here is my spin on the same function using regular expressions (I made a few assumptions about the format you wanted) ...
    Code:
    [001] import re
    [002] ldollar = re.compile("([\+-]?)(\$?)(\d+)") # pre-compile the regular
    [003]                                             # expression for speed
    [004] 
    [005] 
    [006] def DollarVal(data=''): 
    [007]     """ DollarVal(data)
    [008]     Search data string and convert 
    [009]     the first sub-string matching the format:
    [010]     (+,-)($)123456
    [011]     to a long.
    [012]     If a matching sub-string not found return None
    [013]     """
    [014]     sub = ldollar.search(data)
    [015]     if sub: 
    [016]         elements = sub.groups()
    [017]         ans = long(elements[0]+elements[2])
    [018]     else: 
    [019]         ans = None
    [020]     return ans
    [021] 
    [022] print DollarVal("-$1267")
    [023] print DollarVal("-1267")
    [024] print DollarVal("+$1267")
    [025] print DollarVal("1267")
    The regular expression in line 2 can be tweaked to make the match requirements more relaxed or tighter as required.
    For more info on regular expressions check out:
    Python Regular Expressions

    grim
    Last edited by Grim Archon; April 1st, 2005 at 03:53 AM. Reason: unnecessary escape removed
  4. #3
  5. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    I thought an explanation might be helpful:
    Code:
    ldollar = re.compile("([\+-]?)(\$?)(\d+)")
    The string passed to the re.compile function is a series of rules to apply when checking a string. ldollar becomes a regular expression object with those rules built in.

    "([\+-]?)(\$?)(\d+)" can be interpreted as this:

    Find a sub-string that has:

    • 0 or 1 sign characters (no sign or + or - but not both )
    • 0 or 1 Dollar symbol
    • A series of 1 or more digits (as many as you can match)

    The () characters are used to group matching sequences for the ldollar.groups() method

    So in my code ldollar.groups() returns a list:
    ["sign symbol","dollar symbol","the digits"]
    if there is no sign or dollar then the first two elemenst can be "".

    grim
  6. #4
  7. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I like the way the code is arranged in your code. The only problem, is I do the following:
    Code:
    >>> DollarVal('554ghgf$')
    554L
    That 'L' shouldn't be there. With the way I made my function, it does almost exactly what the VB6 Val() function does. It returns all integers, -'s, +'s, and $'s up to the first different (not like the others) character that it finds. If the first character is different, it returns 0. Thanks still.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    Just checking - you know that the L is just there to indicate that the value is of the long data type, and doesn't actually represent a character in the value returned by the function? if you wrote

    >>> print DollarVal('554ghgf$')

    you would just get 554 without the trailing L.
  10. #6
  11. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    I see. Thank you for pointing that out. But why does it only add the L if you are not using print? I don't see why it should make a difference, shouldn't it be returning the same data?
  12. #7
  13. Commie Mutant Traitor
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2004
    Location
    Norcross, GA (again)
    Posts
    1,805
    Rep Power
    1570
    Originally Posted by †Yegg†
    I see. Thank you for pointing that out. But why does it only add the L if you are not using print? I don't see why it should make a difference, shouldn't it be returning the same data?
    When you are executing the function from the listener, it automatically displays the last returned value as a Python literal. This is not a part of the program; it is a behavior of the listener itself. When you use print, however, it actually is the program performing the built-in printing function, which displays the results as text; since numbers don't normally have type indicators in the real world , it does nto showe the 'L'. If you were to run the Python program from the command line (or by double-clicking on the program file's icon, in Windows at least will have the same effect, providing the .py file type is registered), you would only see the text displayed by the print function.
    Last edited by Schol-R-LEA; April 2nd, 2005 at 02:15 PM.
    Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
    #define KINSEY (rand() % 7) λ Scheme is the Red Pill
    Scheme in ShortUnderstanding the C/C++ Preprocessor
    Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

    FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov
  14. #8
  15. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Ok, thank you for clearing that up.

IMN logo majestic logo threadwatch logo seochat tools logo