Interesting problem comparing strings with integer values...
Chris Spencer
clspence at one.net
Thu Jan 16 14:38:38 EST 2003
Okay, I came up with a solution, and my boss refined it (it's nice to
have a boss that "gets it"), and I took it and refined it some more. Pasted
below is the solution to this whole problem.
Note that "".zfill(16) *is not implemented* in the basic string type
methods, but is implemented in the string module. Interesting, that. So
instead of importing the string module, I just rewrote zfill.
I'm posting this here in the hopes that no one else will ever have to
solve this problem again.
Chris.
def zfill(s_Value,i_Padding,i_Ljust=1,s_Padchar="0"):
"""Take a string value and pad it out with a specified character. If
the string is longer than the padding, the whole string will be returned.
Parameters:
s_Value - The string value to be padded out.
i_Padding - The number of spaces to be padded.
i_Ljust - Whether the padding should be to the left of the string or
not. Set to 0 to pad out to the right side of the string. Defaults to padding
out the left-hand side of the string.
s_Padchar - The character used for padding. This will only use the first
character in the pad string as the padding character. Defaults to "0".
Return Values:
The string, padded out to the length specified with the padding
character. If s_Value is longer than the padding, s_Value will be returned
unchanged.
Exceptions:
None
"""
if len(s_Padchar): s_Padchar=s_Padchar[0]
if i_Ljust: return s_Padchar*(i_Padding-len(s_Value))+s_Value
return s_Value+s_Padchar*(i_Padding-len(s_Value))
def int2binary(i_IntVal):
"""Take an integer/long value and return a hexidecimal representation of
it as a string. Used in situations where a number MUST be represented as a
string, but comparison operations on it need to succeed as well.
Parameters:
i_IntVal - The number to be processed.
Return Values:
A hexidecimal representation of the value.
Exceptions:
None
"""
i_IntVal=long(i_IntVal)
return zfill(hex(2**63 + i_IntVal)[2:-1],16)
def float2binary(f_FloatVal):
"""Take a floating point value and return a hexidecimal representation
of it as a string. Used in situations where a number MUST be represented as a
string, but comparison operations on it need to succeed as well.
Parameters:
f_FloatVal - The number to be processed.
Return Values:
A hexidecimal representation of the value.
Exceptions:
None
"""
fval=float(f_FloatVal)
# turn the fraction into a negative value
# if the whole number is a negative value
if fval<0.0: neg=-1
else: neg=1
# split the floating point value into 2 strings
# the whole number, and the fractional part.
parts=`fval`.split(".")
parts[0]=long(parts[0])
# cap the precision to 15 decimal places
# fill any remaining spaces with 0s
parts[1]=neg*long(zfill(parts[1][:15],15,0))
return int2binary(parts[0])+"."+int2binary(parts[1])
On Thu, 16 Jan 2003 04:25:30 GMT, "Jeff Hinrichs" <jlh at cox.net> wrote:
>[...snip...]
>> Programming is about solving problems. I asked a programming question
>> with a certain number of constraints as to what the solution could be. I
>do NOT
>> think it's off topic for this newsgroup just because you don't like the
>> constraints. If you can't help me, that's okay. But please don't flame
>me.
>>
>> Chris.
>The solution to your problem falls back to older language implementation of
>similiar
>constraints (ala COBOL/ISAM) you must define all numeric fields.
>-0000000001.00000
>+0000000000.00000
>+0000000001.00000
>All fields will have to be padded. You can convert them to what ever format
>when you present them
>to the user but in the DB they must all be the same length, storing the
>decimal isn't necessary but you will
>have to define how many there are when you store and retrieve. All numbers
>must carry signs if negatives
>are possible. You won't be able to store arbitrary precision or size. This
>will allow you to index and
>compare them the way that you want. You convert with a function like
>
>def cvtString2Float(dec=5,places=10, stringValue):
> # Here you whack away with stringValue[] to get sign, value right of
>decimal, value left of and then cast and return
> return floatValue
>
>going the other way:
>
>def cvtFloat2String(dec=5,places=10,floatValue):
> # left as exercise for reader
> return stringValue
>
>I've worked on junk like this before and if there was a better way I haven't
>met a grey hair/beard that knew how.<g>
>have fun!
>
More information about the Python-list
mailing list