[Tutor] Need help printing a pickled data

eryksun eryksun at gmail.com
Wed Jun 26 04:33:30 CEST 2013


On Tue, Jun 25, 2013 at 8:35 PM, Matt D <md123 at nycap.rr.com> wrote:
> so that code above is the wrapper of what the code below makes, which is
> pickled data?:
>
> string
> pickle::to_string() const
> {
>    size_t n = 1;
>    ostringstream os;
>    os << "(dp" << n++ << endl;
>    for(stringmap::const_iterator i(map_.begin()); i != map_.end(); ++i) {
>       os << "S'" << i->first << "'" << endl;
>       os << "p" << n++ << endl;
>       os << "S'" << i->second << "'" << endl;
>       os << "p" << n++ << endl << "s";
>    }
>    os << "." << endl;
>    return os.str();
> }

That's a protocol 0 dict. Between MARK "(" and STOP "." is a DICT "d".
Each key/value is a STRING "S". After pushing a key/value pair on the
stack they're added to the dict by SETITEM "s". The PUT "p" operations
store the stack top into the 'memo' (scratch array), but they seem
superfluous since there's no matching GET (g).

For example:

    >>> pkl = "(dS'key1'\nS'val1'\nsS'key2'\nS'val2'\ns."

    >>> pickle.loads(pkl)
    {'key2': 'val2', 'key1': 'val1'}

    >>> pickletools.dis(pkl)
        0: (    MARK
        1: d        DICT       (MARK at 0)
        2: S    STRING     'key1'
       10: S    STRING     'val1'
       18: s    SETITEM
       19: S    STRING     'key2'
       27: S    STRING     'val2'
       35: s    SETITEM
       36: .    STOP
    highest protocol among opcodes = 0


FYI, this appears to be the repository for the above C++
(pickle::to_string, hdu::snapshot):

http://op25.osmocom.org/trac/wiki.png/browser/trunk/blocks/src/lib

But I've only glanced at the code and don't know GNU Radio from GNU Gnews.


More information about the Tutor mailing list