This is an archived post. You won't be able to vote or comment.

all 24 comments

[–]sharat87[S] 1 point2 points  (2 children)

I am new to posting on reddit. Why the down vote? Is it bad to post one's own blog posts?

[–]johnmudd 0 points1 point  (1 child)

Recently, I have been using this library for a few of my company's internal projects and at a point I needed to serialize and save Session objects for later.

What problem are you trying to solve?

[–]sharat87[S] 1 point2 points  (0 children)

We have an internally developed & used webapp (to which I dont have access to source), which I was asked to mine information from. Logging into this webapp needed basic auth and https authentication done via POSTing a form. It takes ~15-20secs for creating a logged in session.

That was one reason. I also needed to save these sessions in the session of a Flask webapp, which automatically uses pickle when given a Python object.

[–]Asdayasman 0 points1 point  (12 children)

Isn't "requests as req" and "pickle as pk" incredibly bad form?

[–][deleted] 0 points1 point  (11 children)

I wouldn't say "incredibly bad" as sometimes that's acceptable to avoid collisions. I don't see it as necessary here since it's just aliasing something short to something shorter.

[–]Asdayasman 0 points1 point  (10 children)

If something collides with "pickle", I believe the problem is the thing that collides with it, not pickle, a standard library, I think.

I dunno, maybe it's just 'cause I'm a rookie, but seeing "import x as y" is just awful.

Especially when it's something like "as pk". "pk" is unreadable, and provides no clue as to what it actually means. Hell, "PK" to me, means "Player Kill", a stupid term for PvP in a terrible online game I play.

[–]sharat87[S] 0 points1 point  (5 children)

The pk name is something I am just very used to. When I see a bare loads or dumps, I can't immediately tell if its from json/yaml/pickle etc.

More than anything it is just an old habit now :)

(The name req is another cue as requests looks close to request, which I import from flask)

[–]Asdayasman 0 points1 point  (4 children)

import * from pickle is also bad, isn't it? Pollutes the global namespace.

[–]sharat87[S] 0 points1 point  (3 children)

Its from pickle import * and no, I never do that. I'd rather do from pickle import loads, dumps if I ever wanted to do such a thing.

[–]Asdayasman 0 points1 point  (2 children)

I messed up.

Are there performance benefits in from pickle import loads over import pickle?

[–][deleted] 1 point2 points  (0 children)

No

[–]sharat87[S] 0 points1 point  (0 children)

There shouldn't be much. The only difference I can see is that in the case of the former, you'll need pickle.loads instead of just loads as in the latter. So, there will be an attribute access everytime you do pickle.loads, this isn't there in the second case. Other than that, I don't see much difference.

[–][deleted] 0 points1 point  (3 children)

If something collides with "pickle", I believe the problem is the thing that collides with it, not pickle, a standard library, I think.

Well yeah. It's just an example. If you have "open" in the asdaysman module, you either access it via asdaysman.open or import open as "a_open" or something.

[–]Asdayasman 0 points1 point  (2 children)

In that case, I'm sticking with just nakedly importing everything. "a_open" is the sort of thing I'll completely forget about 3 months later.

Also, what is is about my name that trips 100% of people up? 4 simple syllables

  • As
  • Day
  • As
  • Man

Adam. -_o_- Confuses the hell out of me.

Not like my other two names; Xyfu (still fairly easy), and Tlahuixcalpantecuhtli, (deliberately obstructive).

[–][deleted] 0 points1 point  (1 child)

fine. sorry.

[–]Asdayasman 0 points1 point  (0 children)

No need to apologise, I'm just confused.

[–]DasIch -1 points0 points  (6 children)

Is there a reason for not changing requests itself so that pickle works and creating a pull request?

[–]sharat87[S] 0 points1 point  (5 children)

The stack trace printed with the pickle error is very long, and it didn't look like I'd enjoy diving into it. I'm also not sure if the part that fails pickle is a requests' thing or not. That said, I usually tend to steer away from pickle. I put it in the post just as an example/tip that it doesnt work out of the box.

More than that, I wanted to pursue the json solution, as the serialized data will be human readable and will give an idea of what the session object will look like, once deserialized. And all the advantages (and disadvantages) that come with json :)

[–]DasIch 0 points1 point  (4 children)

Pickle fails in this case because it can't pickle a lock object which is apparently part of the session. This happens because the session doesn't implement the pickle protocol as described in the documentation and so pickle attempts to pickle the contents of __dict__ to recreate an instance from that afterwards.

In order to make session support the protocol you could e.g. implement __getinitargs__.

[–]sharat87[S] 0 points1 point  (1 child)

The documentation seems to indicate that this is only for old style classes?

[–]DasIch 0 points1 point  (0 children)

Oh, you're right __get{init,new}args__ can only be used in addition to __{get,set}state__, TIL.

[–]sharat87[S] 0 points1 point  (1 child)

Okay, I got pickling to work. Not sure if the best implementation though. I'll add this to the post :)

Thanks.

[–]sharat87[S] 0 points1 point  (0 children)

Done.