March 31st, 2005, 07:25 PM
Improved Val() Function
I needed to improve my Val() function because the original was having some major issues. Here is my new copy:
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.
i_list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '$', '+', '-']
start_char = [data]
end_char = 
got_end = [False]
for i in data:
if got_end == True:
if i not in i_list:
end_char = i
got_end = True
end_char = data[-1]
got_end = True
if start_char not in i_list:
if end_char in i_list:
April 1st, 2005, 03:36 AM
I think you lost your way a bit with all the unnecessary list elements.
end_char = 
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) ...
The regular expression in line 2 can be tweaked to make the match requirements more relaxed or tighter as required.
 import re
 ldollar = re.compile("([\+-]?)(\$?)(\d+)") # pre-compile the regular
 # expression for speed
 def DollarVal(data=''):
 """ DollarVal(data)
 Search data string and convert
 the first sub-string matching the format:
 to a long.
 If a matching sub-string not found return None
 sub = ldollar.search(data)
 if sub:
 elements = sub.groups()
 ans = long(elements+elements)
 ans = None
 return ans
 print DollarVal("-$1267")
 print DollarVal("-1267")
 print DollarVal("+$1267")
 print DollarVal("1267")
For more info on regular expressions check out:
Python Regular Expressions
Last edited by Grim Archon; April 1st, 2005 at 04:53 AM.
Reason: unnecessary escape removed
April 1st, 2005, 04:50 AM
I thought an explanation might be helpful:
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.
ldollar = re.compile("([\+-]?)(\$?)(\d+)")
"([\+-]?)(\$?)(\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 "".
April 1st, 2005, 08:32 PM
I like the way the code is arranged in your code. The only problem, is I do the following:
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.
April 2nd, 2005, 05:25 AM
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.
April 2nd, 2005, 10:25 AM
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?
April 2nd, 2005, 03:09 PM
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.
Originally Posted by †Yegg†
Last edited by Schol-R-LEA; April 2nd, 2005 at 03:15 PM.
April 2nd, 2005, 04:12 PM
Ok, thank you for clearing that up.