Python Forum
? Headers: Keep-It=Simple; And=Stupid; With=Auto-Completion. Support HTTP, IMAP.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
? Headers: Keep-It=Simple; And=Stupid; With=Auto-Completion. Support HTTP, IMAP.
#1
Hi,

I have a opensource project to share with you guys Smile

It is about : Don't screw up reading Headers, Keep It Simple, Stupid ! HTTP or IMAP !

No matters your religion, IMAP4 or HTTP, you should not worries about accessing easily header and associated attributes, adjectives or values.

But even with modern language like Python we still have those :

charset = headers['Content-Type'].split(';')[-1].split('=')[-1]
I have seen so much chunk of code like this, trying to deal with them, often in a risky way to take care of more important part of the code. In face of that I say no more !

Do not forget that headers are not 1 TO 1. One header can be repeated multiple time and attribute can have multiple value within the same header. So representing them by using a ´dict()´ is not a good idea even if using case insensible dict !

That is why I created a project that would remove the pain of dealing with them no matter where they came from.

It was absolutely clear that they was something missing in the python community. From the project ´psf/requests´ to ´tornado´, each one have it own implementation, each one are also incomplete. The best so far I have seen is the one from ´httpx´ project.

Mine is not perfect, but has its amount of sweetness. Beginning from auto-completion capability in python interpreter or in your beloved IDE. It should dramatically reduce stress and pain when encountering them.

[Image: 76708477-64a96600-66f7-11ea-9d4a-8cc07866e185.png]
Link to the repository : https://github.com/Ousret/kiss-headers

[Image: 76808050-bf6dbb00-67e6-11ea-9799-d2b20068bbb7.gif]

Usage sample :
from requests import get
from kiss_headers import parse_it

response = get('https://www.google.fr')
headers = parse_it(response)

'Content-Type' in headers  # output: True
'Content_type' in headers  # output: True

str(headers.content_type)  # output : text/html; charset=ISO-8859-1
'application/json' in headers.content_type  # output: False
'text/html' in headers.content_type # output: True

str(headers.content_type.charset)  # output : ISO-8859-1
type(headers.set_cookie) # output: list
'Secure' in headers.set_cookie[0] # output: True
'domain' in headers.set_cookie[0] # output: True
headers.set_cookie[0].domain # output: .google.fr
I am pretty confident that those kind of libraries that serve reducing time spent on the little things would actually help make the life of a programmer easier. And spent a lot more time on something else.

So, do not hesitate, when you are encountering a case that respect those criteria : (i) stupid (ii) repeatably reinventing the wheel (iii) everyone is facing this issue at least once. (iv) break the loop by contributing.
Reply
#2
I have released a new major, 2.0
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Simple Auto Surfer (PySAS) Spirit77 0 2,751 May-21-2018, 03:24 AM
Last Post: Spirit77
  Stupid Python Tricks: Q is for Quit ichabod801 1 4,067 Dec-07-2016, 03:26 AM
Last Post: Mekire

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020