-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:bbelderbos/bobcodesit
- Loading branch information
Showing
4 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# pretty print to a file | ||
|
||
I was in the Python debugger today and I was wondering how to write a sorted dictionary to a file while there. 💪 | ||
|
||
And then I learned that pprint's `PrettyPrinter` can stream to a file, nice! 😎 | ||
|
||
Here is how: | ||
|
||
``` | ||
import pprint | ||
# sort by numeric value descending (here: most listened podcast episodes) | ||
sorted_stats = sorted(stats.items(), key=lambda x: x[1], reverse=True) | ||
# pretty print the dict to a text file | ||
with open('sorted_stats.txt', 'w') as file: | ||
pp = pprint.PrettyPrinter(stream=file, indent=4) | ||
pp.pprint(sorted_stats) | ||
``` | ||
|
||
#pprint |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# setting the package version in your init file | ||
|
||
Automate `__version__` population in your package's `__init__.py` file using importlib.metadata! 📦 🚀 | ||
|
||
✅ Ensures compatibility across Python versions | ||
✅ Automatically sets __version__ from package metadata | ||
|
||
``` | ||
"""A Sample Package for Demonstration Purposes.""" | ||
try: | ||
from importlib.metadata import distribution # Python 3.8+ | ||
except ModuleNotFoundError: | ||
from importlib_metadata import distribution # Backport for older versions | ||
__version__ = distribution("sample-package").version | ||
``` | ||
|
||
#importlib |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# using requests head | ||
|
||
Need to check if a resource exists? 💡 | ||
|
||
Use requests.head() instead of requests.get() for a faster response! 🏃 | ||
|
||
It only fetches the headers, saving time and bandwidth. 🕒 | ||
|
||
I used it the other day to quickly check if a transcript was added to a podcast episode. 😎 | ||
|
||
``` | ||
import requests | ||
url = "https://www.buzzsprout.com/1501156/15129871/transcript" | ||
response = requests.head(url) | ||
if response.status_code == 200: | ||
print("Transcript exists!") | ||
``` | ||
|
||
#requests |