Skip to content

Commit

Permalink
Merge branch 'main' of github.com:bbelderbos/bobcodesit
Browse files Browse the repository at this point in the history
  • Loading branch information
bbelderbos committed Jun 1, 2024
2 parents 06972ca + 7ccf95a commit 0ca77fd
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ This file gets generated by [this script](index.py).
## Importlib

- [Get package metadata](notes/20220922083512.md)
- [Setting the package version in your init file](notes/20240530111719.md)

## Impostersyndrome

Expand Down Expand Up @@ -326,6 +327,10 @@ This file gets generated by [this script](index.py).

- [Make a terminal plot](notes/20240119105430.md)

## Pprint

- [Pretty print to a file](notes/20240529114443.md)

## Property

- [Computed (and readonly) fields / attributes](notes/20230829122437.md)
Expand Down Expand Up @@ -372,6 +377,7 @@ This file gets generated by [this script](index.py).

- [Caching api calls](notes/20230130103011.md)
- [Getting exchange rates](notes/20231221192642.md)
- [Using requests head](notes/20240531100650.md)

## Rich

Expand Down
21 changes: 21 additions & 0 deletions notes/20240529114443.md
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
19 changes: 19 additions & 0 deletions notes/20240530111719.md
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
20 changes: 20 additions & 0 deletions notes/20240531100650.md
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

0 comments on commit 0ca77fd

Please sign in to comment.