Tyrel's Blog

Code, Flying, Tech, Automation

Oct 15, 2023

Djangocon 2023

I am at DjangoCon 2023 this year!

I live in Durham, NC and have been avoiding conferences for the past few years because of Covid and not wanting to travel. This year for, what ever reason, they picked Durham!

So I am breaking my "Don't go to conferences" for multiple reasons.

  1. The DSF has pretty great Covid rules, and mask requirements.
  2. Testing requirements.
  3. It's local, so I get to sleep in my own bed.

I'm not guaranteed to get to see my daughter, but I hope she knows I still love her even if I'm not around. I'm leaving at 7:30 tomorrow as I'm biking in, and she may be asleep.

Already I've gone to one of the local breweries I haven't gone to yet (See above: Covid), and met some great people, can't wait for the rest of the week.

While technically a "Tech Blog" I am by no means a great note taker, so don't expect any quality information from me in blog form, I'm mostly going for the HallwayTrack, and to watch talks.

 · · ·  django  conferences

Oct 03, 2023

Rotate a Matrix in Python

I've been doing Advent of Code for a few years now, and every year I do it in my favorite language, Python. One thing that comes up a lot, is rotating matrices.

One way to do this, is to use Numpy, using np.rot90(mat), but not everyone wants to install Numpy just to do one small task. I know I don't always.

The way I always do it, that will support non-square matrixes, is to use zip.

>>> matrix = [
 [1,2,3],
 [4,5,6],
 [7,8,9]
]
>>> rotated = list(zip(*matrix[::-1]))
# And result is
[[7, 4, 1],
 [8, 5, 2],
 [9, 6, 3]]

We can break this down bit by bit.

This will copy the list, with a -1 step, resulting in a reverse order

>>> matrix[::-1]
[[7,8,9],
 [4,5,6],
 [1,2,3]]

Next we need to call zip in order to get the x-th item from each inner list, but first, we need to unpack it. If you'll notice, the unpacked version isn't wrapped with another list, which is what zip needs from us.

# Too many lists
>>> print(matrix[::-1])
[[7, 8, 9], [4, 5, 6], [1, 2, 3]]

# Just right
>>> print(*matrix[::-1])
[7, 8, 9] [4, 5, 6] [1, 2, 3]

From there, we can pass this unpacked list of - in our case - three lists, to zip (and in Python 3 this returns a generator, so we need to call list again on it, or just use it)

>>> # Again, we get the rotated matrix
>>> list(zip(*matrix[::-1]))
[[7, 4, 1],
 [8, 5, 2],
 [9, 6, 3]]

Notes

Small note: If you run this, you will actually get a list of tuples, so you can map those back to a list, if you need to update them for any reason. I just wanted square brackets in my examples.

# This is just messy looking, so I didn't mention it until now
>>> list(map(list, zip(*matrix[::-1])))

As I mentioned, due to using zip this will work with non-square examples as well.

>>> matrix = [
... [1,2,3,4,5,6,7,8,9],
... [9,8,7,6,5,4,3,2,1],
... ]
>>> print(list(zip(*matrix[::-1])))
[(9, 1),
 (8, 2),
 (7, 3),
 (6, 4),
 (5, 5),
 (4, 6),
 (3, 7),
 (2, 8),
 (1, 9)]
 · · ·  python

Sep 26, 2023

Which which is which?

I had a bit of a "Tyrel you know nothing" moment today with some commandline tooling.

I have been an avid user of ZSH for a decade now, but recently I tried to swap to fish shell. Along the years, I've maintained a lot of different iterations of dotfiles, and shell aliases/functions. I was talking to a friend [citation needed] about updating from exa to eza and then noticed I didn't have my aliases loaded, so I was still using ls directly, as I have alias ls="exa -lhFgxUm --git --time-style long-iso --group-directories-first" in my .shell_aliases file.

I did this by showing the following output:

$ which ls
/usr/bin/ls

Because I expected it to show me which alias was being pointed to by ls.

My friend pointed out that "Which doesn't show aliases, it only points to files" to which I replied along the lines of "What? No way, I've used which to show me aliases and functions loads of times."

And promptly sent a screenshot of my system NOT showing that for other aliases I have set up. Things then got conversational and me being confused, to the point of me questioning if "Had I ever successfully done that? Maybe my macbook is set up differrently" and went and grabbed that.

Friend then looked at the man page for which, and noticed that there's the --read-alias and --read-functions flags on which, and I didn't have those set. I then swapped over to bash "Maybe it's a bash thing only? I'm using Fish".

Nope, still nothing! Then went to google, and it turns out that ZSH is what has this setup by default. Thank you "Althorion" from Stackoverflow for settling my "Yes you've done this before" confusion.

It turns out that ZSH's which is equivalent to the ZSH shell built-in whence -c which shows aliases and functions.

After running /usr/bin/zsh and sourcing my aliases (I don't have a zshrc file anymore, I need to set that back up), I was able to settle my fears and prove to myself that I wasn't making things up. There is a which which shows you which aliases you have set up, which is default for ZSH.

$ which ls
ls: aliased to exa -lhFgxUm --git --time-style long-iso --group-directories-first
 · · ·  linux  macos  zsh

Jun 19, 2023

Next → Page 1 of 8