Header logo.
Tonghe's Notes

Method list.sort() vs function sorted()

The more I program, the more I find myself realizing the why of some of the basics.

You can use either a method or a function to sort or reverse a list in Python. One works in-place, meaning the order of items in the original list is changed. The other works on a copy of the list and returns an ordered version of that copy as a new list.

list.sort() and list.reverse() are methods of the list class. They are both verbs, suggesting they take actions on an instance of the list class. And as methods that are internal to a list, they are understandably “allowed” to change the internal order of list items. ⋯

Week 47

I spent some time last week to get myself more familiar with TypeScript.

Although I'd known the syntax, how to type things (props, components, etc) was at times still confusing. I probably saw more than a bunch of errors thrown at me.

Hopefully I'd love it when I get the hang of it. People on the internet say, all pros and cons considered, it's worth the while.

Week 46 gave me surprises

A few things:

Handling date and time is more complicated than I expected. python-dateutil is quite helpful when you need to parse a datetime string.

I tried updating a field in a MongoDB document using MongoEngine. The value of that field was an embedded document that contained four sub-fields. I intended to change the value of subkey2. But the entire embedded document was overwritten. ⋯

So much fun in week 45

These are the fun I had in week 45, 2021.

I ended up in a situation where I decided to use Djongo to bridge MongoDB and Django.

MongoEngine team made a Django connector. But it does not seem to be actively maintained. So they pointed to Djongo and described it as “promising”. (Spoiler: If something is “promising”, the universe hasn't decided whether to resolve it or reject it.) ⋯

Reading Books in Swedish

It took me more than a year to finish reading my first Swedish novel Tio över ett, a teenage romance story set in mining town Kiruna. I got this paperback from a shelf at Uppsala University where people leave their used books. You'd most often see conference proceedings and PhD dissertations – dozens of brand new copies at once – left there.

I also bought the audio version from Bokus.com, because you can download DRM-free MP3 files from there.

Once you have the MP3 files, you can practice dictation with them. It's handy to listen to the recordings using a piece of audio software, such as Audacity or Tenacity. ⋯

Don't Edit Hosts File With Nano on a Mac

TLDR: If you are using a Mac, don't edit your hosts file with Nano and save it in Mac format. It will cause bizarre bugs in places you won't expect. If you are seeing bugs on a Mac with errors saying localhost is missing, try opening the hosts file and save it with Unix line ending.

I learned this while working on a React Native project, where Expo's development server just wouldn't start up. After a long and patient wait, an elaborate error message showed up. In which the most eye-catching line said:

RangeError [ERR_SOCKET_BAD_PORT]: options.port should be >= 0 and < 65536. Received 65536.

Comparing Arrays by Value in JavaScript

I didn't know I didn't know the answer when my friend showed me a simple piece of JavaScript code and asked me what would happen.

He said this caused a bug in a project he was working on and it took him hours to debug. Since I knew arrays in JavaScript are objects. And objects are only equal when they refer to the same thing in memory. If you do it as shown in ex. {2}, the result will certainly be true, because x and y indeed refer to the same thing.

In ex. {1}, a and b are defined differently. And I felt very clever when I correctly said the code on line 🕶 will produce a false. But — ⋯

Creating My Own Static Site Generator

Since I take a lot of notes, rcently I thought I could edit some of my notes and turn them into a blog. I wasn't satisfied with WordPress, because it's bloated. I didn't feel like to get familiar with the settings and configurations of other static site generators either. So I decided to write my own. I named it “Lysekil”, where I visited in late March and loved it there. I published it on GitHub and provided a brief documentation.

The color scheme used in the template was borrowed from the default “Red Graphite” theme of Bear, my favorite note-taking app. (It also happens to be Andy Matuschak's favorite note-taking app. I used to dither over a few other options, including Obsidian, RemNote and RoamResearch. In the end I would always come back to Bear.)

I used third-party Python packages to process markdown, generate the Atom feed and highlight code syntax. ⋯

Emulating Python's zip() and zip_longest() in JavaScript

This is essentially how you emulate the way Python's zip() function works using JavaScript.

What makes this possible is the fact that JavaScript array methods — map(), forEach() and even filter() — take up to three arguments (element, index, array) while they iterate through the array.

In which element is the element that the iterator points to at each step, index is the index of this item in the original array. And it's worth noting the third argument is the original array. ⋯