0

I'm pickling a python dictonary, and storing it to a GAE BlobProperty. The BlobProperty has a 1MB size limit. I want to programmatically check that my object will 'fit' in that 1MB limit

import pickle
p = pickle.dumps(some_object)
print len(p)
>>>42000

But what is len(pickled_object) measuring? (the number of characters?), and how do I convert that to bytes, to check against the 1MB limit? I cant find the info on how the data in encoded to be able to compare the len() to the 1MB (e.g. bytes per 'character')

thanks

tom
  • 2,189
  • 2
  • 15
  • 27

1 Answers1

2

The size of a serialized pickle is in bytes, so len works fine. See this answer to a very similar question: How to get the size of a python object in bytes on Google AppEngine?

Community
  • 1
  • 1
Jaime Gómez
  • 6,961
  • 3
  • 40
  • 41
  • Thanks. Yes, I saw the other question, but bizarrely the accepted answer does not state that len() returns the number of *bytes*, so I figured it was worth getting it on the record. – tom Jul 23 '14 at 21:49
  • For my education: is it stated anywhere in the python docs that len() of a pickled object returns the length in bytes? I don't see it, but maybe I'm looking in the wrong place? https://docs.python.org/2/library/pickle.html – tom Jul 23 '14 at 21:52
  • 1
    Good question, we can infer that from the fact that in python 2.x strings are just bytes, unless working with unicode (default in 3.x, in which case one would have to decode them first). – Jaime Gómez Jul 23 '14 at 22:18