Interesting problem comparing strings with integer values...
clspence at one.net
Thu Jan 16 01:54:11 CET 2003
As I mentioned in my most recent response, these strings are NOT being compared
in the Python interpreter, but by the SQL engine on a database, so all the
subclassing in the world won't help me here.
And though I sometimes don't agree with my boss on design issues, I have to say
that this isn't one of those times. We have very clear design constraints which
REQUIRE us to store strings, integers, floats, dates/times, all in string format
in TEXT blobs in the database.
On Thu, 16 Jan 2003 00:17:09 GMT, Carl Banks <imbosol at vt.edu> wrote:
>Chris Spencer wrote:
>> Due to certain design constraints, I must be able to store
>> both integers and floating point numbers as strings. These strings
>> must be able to be compared correctly, so things like: "999"<"3432"
>> are not possible.
>> One option we thought of was padding the strings with zeros,
>> so things like: "00000999"<"00003432" would work. This seems a bit
>> hack-y to me. I was wondering if anyone has a more elegant solution
>> to the problem?
>1. Go back to your client/boss and tell him/her that his/her design
> contraint sucks because it is counterproductive (politely).
>2. Use Perl. (That's probably what your cognitive dissonant
> client/boss is trying to make you do anyways.)
>3. Subclass str in Python 2.2 and above. Redefine __cmp__ to
> compare strings numerically, and probably you should redefine
> __init__ to enforce an invariant that the string must hold a
> numerical value.
>4. Instead of using operators, define some functions to do string
> comparisons for you.
More information about the Python-list