Compare commits

...

2 commits

Author SHA1 Message Date
6660767303
New post 2024-02-01 21:04:33 -05:00
6411ef7272
New post 2024-02-01 18:33:36 -05:00
2 changed files with 128 additions and 0 deletions

View file

@ -0,0 +1,40 @@
---
title: "Mapping the states I visited"
date: 2024-02-01T18:30:00-05:00
draft: false
tags: []
math: false
medium_enabled: false
---
[Henrique's Impossible List](https://hacdias.com/impossible-list/) shows a list of countries that they've visited so far and their goals for the future. I personally have not travelled to many countries, but I have visited a several US states!
Instead of presenting the states in a list, I thought it would be very cool to visually show it on a map. Ideally the map would be encoded in a SVG so that:
- The image will scale regardless of the device size
- I can manually edit the source to show the visited regions
Now imagine my surprise after searching on [Kagi](https://kagi.com/) to come across a SVG in the public domain on [Wikipedia](https://commons.wikimedia.org/wiki/File:Blank_US_Map_(states_only).svg). Even better, the following is shown near the top of the file
```css
/* Individual states can be colored as follows: */
.ks,.mt,.pa {fill:#0000FF}
.ca,.de {fill:#FF0000}
/* In this example, Kansas, Montana and Pennsylvania are colored blue,
and California and Delaware are colored red.
Place this code in the empty space below. */
```
Such an elegant way of coloring in the states! Now it's up to me to come up with some categories. As of the time of writing, I decided on:
- Green: Places I've lived
- Blue: Places I've visited
- Darker Gray: Places I've driven through
Here's how I colored by map:
![Map of US States colored by whether I've visited](/files/images/usa_visited.svg)
For the last couple weeks, I've been brainstorming new pages for my website and came up with https://brandonrozek.com/visited.
At the time of writing, it only has the colored in states and a list of cities I've been to. Though I'm welcome to any suggestions you might have. Feel free to [get in touch!](/contact)

View file

@ -0,0 +1,88 @@
---
title: "Quick Python: Refactoring Exceptions with Context Manager"
date: 2024-02-01T20:48:21-05:00
draft: false
tags: ["Python"]
math: false
medium_enabled: false
---
I generally find exception syntax a little clunky...
```python
try:
for _ in range(5):
sleep(1)
except KeyboardInterrupt:
# Awesome task 1
# Awesome task 2...
pass
```
Especially if you end up capturing the same exceptions and handling it the same way.
```python
try:
for _ in range(5):
sleep(1)
except KeyboardInterrupt:
# Awesome task 1
# Awesome task 2...
pass
try:
for _ in range(2):
sleep(1)
except KeyboardInterrupt:
# Awesome task 1
# Awesome task 2...
pass
```
One way to make our code more DRY (don't-repeat-yourself) is to make use of Python's context managers.
```python
@contextmanager
def handle_sigint():
try:
yield
except KeyboardInterrupt:
# Awesome task 1
# Awesome task 2...
pass
```
Using the context manager, everything within the indented block gets executed within the try block.
```python
with handle_sigint():
for _ in range(5):
sleep(1)
with handle_sigint():
for _ in range(2):
sleep(1)
```
In fact, we can write this in a generic way to give us an alternative syntax for handling exceptions.
```python
@contextmanager
def handle_exception(f, *exceptions):
try:
yield
except exceptions as e:
f(e)
```
For example, let's tell the user that we're explicitly ignoring their exception
```python
def ignore(e):
print("Ignoring", e.__class__.__name__)
with handle_exception(ignore, NotImplementedError, KeyboardInterrupt):
for _ in range(5):
sleep(1)
```