Recent Python Tips and Gotchas
A grab bag of small issues I ran into recently.
Preserve OrderedDict when dumping/loading JSON
json.dumps()
keeps the order of OrderedDict
. While loading, pass object_pairs_hook=collections.OrderedDict
to coerce objects into ordered dictionaries.
Install a specific package version with pip
pip install lxml==3.4.4
Download without installing:
pip download lxml
Open view-source in Chrome with Selenium
Download Chromedriver: https://sites.google.com/a/chromium.org/chromedriver/downloads
1 | from selenium import webdriver |
WebDriverException: Message: chrome not reachable
Call webdriver.quit()
to clean up the session.
Sending raw strings with requests.post
requests.post(data=...)
accepts dicts or strings. When you pass a string, Requests sends it as-is and will not set Content-Type: application/x-www-form-urlencoded
. Add the header yourself. Passing a dict adds the header automatically and URL-encodes the payload.
Dynamically add and rename PyQt widgets
1 | def add_button_click(self): |