views:

459

answers:

3

Are there any standalone type conversion libraries?

I have a data storage system that only understands bytes/strings, but I can tag metadata such as the type to be converted to.

I could hack up some naive system of type converters, as every other application has done before me, or I could hopefully use a standalone library, except I can't find one. Odd for such a common activity.

Just to clarify, I will have something like:

('123', 'integer') and I want to get out 123

+4  A: 

You've got two options, either use the struct or pickle modules.

With struct you specify a format and it compacts your data to byte array. This is useful for working with C structures or writing to networked apps that require are binary protocol.

pickle can automatically serialise and deserialise complex Python structures to a string. There are some caveats so it's best read the documentation. I think this is the most likely the library you want.

>>> import pickle
>>> v = pickle.dumps(123)
>>> v
'I123\n.'
>>> pickle.loads(v)
123
>>> v = pickle.dumps({"abc": 123})
>>> v
"(dp0\nS'abc'\np1\nI123\ns."
>>> pickle.loads(v)
{'abc': 123}
Andrew Wilkinson
Pickle won't do it, the format already exists in the database, and if I store pickles I lose the db's querying abilities. Struct could work though.
Ali A
+3  A: 

Consider this.

import datetime

def toDate( someString ):
    return datetime.datetime.strptime( someString, "%x" ).date()

typeConversionMapping = { 'integer': int, 'string': str, 'float': float, 'date': toDate }
def typeConversionFunction( typeConversionTuple ):
    theStringRepresentation, theTypeName = typeConversionTuple
    return typeConversionMapping[theTypeName](theStringRepresentation)

Is that a good enough standalone library for such a common activity? Would that be enough of a well-tested, error-resilient library? Or is there something more that's required?

If you need more or different date/time conversions, you simply add new toDate functions with different formats.

S.Lott
Maybe I am missing something, but that would break on passing 'abc' as an integer
Ali A
@Ali A: Shouldn't it break? 'abc' isn't an integer.
Deestan
@Ali A: It throws an exception: TypeError. That's what is designed to happen. Is there something more that's required? Be specific.
S.Lott
Accepting, though I think the answer to the question is "no". These simple ones are great, but then you will get to dates/times etc.
Ali A
@Ali A. Dates and times are NEVER simple conversions because of the formatting issues. However, they're trivial to add to this example.
S.Lott
@Ali A. The answer remains "yes". There are standard conversions for all Python types. They're all there. Already defined. Already part of the language or libraries.
S.Lott
S.Lott: You are actually illustrating my point, that since it is so apparently simple, naive implementations exist everywhere. It's not part of the language, it's easily doable with the language.
Ali A
@Ali A: I can't understand the distinctions between no part of the language and easily doable with the language and whatever it is you want that's different from this.
S.Lott
+1  A: 

Flatland does this well. http://discorporate.us/projects/flatland/

Ali A