Skip to content

Instantly share code, notes, and snippets.

@jmahmood
Created July 9, 2012 03:54
Show Gist options
  • Save jmahmood/3074148 to your computer and use it in GitHub Desktop.
Save jmahmood/3074148 to your computer and use it in GitHub Desktop.
3 years of python
A few years ago:
def parseBugzillaCookies(self,s):
if s is None: return {self.SESSION_ID_STRING:None}
ret = {}
tmp = s.split(';')
for t in tmp:
t = t.replace('HttpOnly, ','')
coppia = t.split('=')
if coppia[0].strip() in ['Bugzilla_logincookie','Bugzilla_login']:
k = coppia[0].strip()
v = coppia[1].strip()
ret[k]=v
return ret
Now:
(Probably less efficient, but much easier to read)
def parseBugzillaCookies(self,s):
if s is None:
return {self.SESSION_ID_STRING:None}
BugzillaCookieKeys = ['Bugzilla_logincookie','Bugzilla_login']
headers = [t.replace('HttpOnly, ','').split("=") for t in s.split(';')]
return dict((k.strip(),v.strip()) for (k,v) in headers if k in BugzillaCookieKeys)
@ksze
Copy link

ksze commented Jul 9, 2012

What part of the newer implementation do you suspect is less efficient? And it depends on what kind of efficiency you are talking about. Memory or speed?
The new implementation can be made to have better memory efficiency, given a large enough s: just turn headers into a generator instead of expanding it immediately into a list.

By the way, the newer implementation is possibly wrong because you're not stripping k in the end:

for (k,v) in headers if k in BugzillaCookieKeys

should be

for (k,v) in headers if k.strip() in BugzillaCookieKeys)

@jmahmood
Copy link
Author

Thanks for the bugfix - I applied it in py-bugziller :D

I was concerned that I was running through the same array twice when I could do everything in a single loop previously. However, in retrospect, that wasn't really such a big deal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment