Marshal Obj is String or Binary?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sat Jan 14 06:28:50 EST 2006


On Sat, 14 Jan 2006 12:36:59 +0200, Max wrote:

>> He can still store the repr of the string into the database, and then
>> reconstruct it with eval:
>> 
> 
> Yes, but len(repr('\x00')) is 4, while len('\x00') is 1. 

Incorrect:

>>> len(repr('\x00'))
6
>>> repr('\x00')
"'\\x00'"



> So if he uses
> BLOB his data will take almost a quarter of the space, compared to your
> method (stored as TEXT).

Also incorrect. That depends utterly on which particular characters end up
in the serialised data. You may or may not be able to predict what that
mix may be.

# nothing but printable data

>>> s = ''.join(['a' for i in range(256)])
>>> len(s)
256
>>> len(repr(s))
258


# nothing but unprintable data

>>> s = ''.join(['\0' for i in range(256)])
>>> len(s)
256
>>> len(repr(s))
1026


# one particular mix of both printable and unprintable data

>>> s = ''.join([chr(i) for i in range(256)])
>>> len(s)
256
>>> len(repr(s))
737


# a different mix of both printable and unprintable data

>>> s = '+'.join([chr(i) for i in range(128)])
>>> len(s)
255
>>> len(repr(s))
352





-- 
Steven.




More information about the Python-list mailing list