A few things from my week

Time to get back to some blogging! I thought I’d dip my toes in slowly and collect a few of the smaller things I’ve come across this week. Some of these might be very obvious, some might not be. My main hope with this is that by writing these out, I might actually remember one or two before the third or fourth google, and not the seventh or eighth.

Git --prune

Being a good Git user, you make branches and don’t work directly on main. You merge a branch and delete it on Github (your remote). You pull down to your machine, delete the local branch and run git branch -a to make sure everything is alright. If you’ve been following this pattern for a while, you might see something like this:

A command prompt displaying a local, remote, and many remote tracking branches

Hmmm well all of those remote tracking branches aren’t really hurting anything, but they’re a little annoying to look at, and it feels like there should be a good reason to get rid of them. Well, just remember to add the --prune flag the next time when you pull or fetch after deleting a remote branch. You can also more explicitly call this, but a flag on a command is a little more convenient I think.

A command prompt after pruning branches

Black formatter --diff

I’ve recently started using Black. Undoing the formatting is of course possible if you commit right before but wouldn’t it be nicer to see the changes before executing them? black example.py --diff will display all the changes black would have made to example.py. You can add highlighting with the --color option, but I like piping to bat for a little more navigability.

self isn’t special

When creating classes, self isn’t actually a special word. Obviously you should use self, but you could use whatever you want. What is special about self is it’s position. Without getting into class vs instance methods and other very interesting OOP topics, for simple method definitions when you do object.method(), your object is an argument for the method. This makes sense as we have to use self (or whatever word you want!) as a parameter in the class definition. The only reason I’m making a big deal about this is that one annoying error in particular does not give the message I initially expected it to. If you forget to call the class when declaring the object, foo = Class instead of foo = Class(), when you call a method from foo, you’ll get something like “missing positional argument baz”. Whatever argument you pass as baz is getting assigned as self and the interpreter stops on too few arguments before finding out that baz isn’t an object.

Python’s json deserializes how you expect it to

I’ve been using json save files for a small project. At some point late in the night, I somehow became convinced that the builtin json module would only deserialize elements to strings, so you would need to convert to a numeric type when loading back into the program. This is not correct. json will deserialize int as int and float as float. I’m not entirely sure how I managed to convince myself of the wrong thing, though I suspect I passed a string of a number when I created the file, which does get deserialized back to the same thing. This definitely falls in the, “should be very obvious” category, but just wait until next week when I’m convinced that duck typing actually means type(quack) == duck

In [1]: import json

In [2]: foo = json.loads('{"bar":5, "baz": 7}')

In [3]: foo["bar"] + foo["baz"]
Out[3]: 12

For next time

There are a few items I haven’t had time to play around with this week, but I want to make a note of them as a starting point for hopefully discussing them next week.

Ale

Ale is a linter for Vim/NeoVim that allows linting while typing, similar to a more recent editor like VS Code. I use Vim when I can, as I’m dissatisfied with the VS Code vim extension, so I’m excited about anything that can add to the real time Vim experience.

Wing IDE

I saw Wing mentioned in the Black documentation. I’ve used PyCharm and Spyder a bit before, but I always like checking out a new tool. It does seem to have payment tiers, which makes me a little less likely to fully dive in. I haven’t given it a try yet, but it’s on my list for next week.

Knitting Counter

I picked up this older, finished, project thinking it would be a good candidate for adding a Textual interface. Turns out this was an older, half finished, project. Or more accurately, it was mostly finished, but done badly and I had to rip half of it out. I think I’m at the point where I can start adding in new things, so hopefully I’ll have something interesting in the future.

Hopefully another blog

I’m planning on being at least somewhat more regular with posting, so hopefully a follow up next week. Please let me know what you think! I’m happy for suggestions and corrections.