You have two main options: Let’s go through a quick example of each approach. Using date-fns to get date difference Above, I mentioned luxon, day.js, and date-fns. They’re all great packages, but I personally prefer date-fns due to its easily searchable documentation. date-fns has a ton of other functionality and the docs are very good […]
Latest
How to find files on linux / mac
Command line find is a powerful, underutilized tool. In this post, I’ll go through the following use cases: Find files by name I use this all the time. Let’s say I’m in my ~/Downloads directory. That directory has several hundred files in it—and quite a few subdirectories—so it’s a bit hard to comb through in […]
How to exit Vim
My preferred method is “:x”. It saves the file only if there is a change to be saved, and then exits. You can also use :wq, :q, or :q!—each one behaves slightly differently.
Grep, grep options, and grep’s faster open-source alternative: ripgrep
Tldr: Grep In large codebases just finding the things you need can be hard. The built-in solution on linux/unix is grep. The basic format is grep {search_term} {file_path}. Here are a few simple examples: Here are a few commonly-used useful flags for grep: Flag Effect -i makes search case-insensitive -r recursively search directories -n show […]
Reading CSVs in Python
When reading in CSVs in Python, you have two main options: Let’s go through an example of each using a spreadsheet containing some US political funding data I have around from a personal project. Option 1: Python’s csv module: That’s it! Now you have access to the data in a list of dicts. You might […]
Python Context Managers: the “with” Statement
When reading and writing files in python, you’ll often see the use of with: This is called a Python context manager, and it’s essentially an automatic way to clean up after yourself. The code block above is functionally equivalent to this block: Without a context manager, we have to remember to manually .close() the file, […]
Swapping dict keys and values in Python
Sometimes you need to invert the keys and values in a dictionary. Fortunately, Python gives us a concise way to do exactly that with list comprehensions and dict.items(): However, if you have nested dictionaries this method won’t work: This error is caused by attempting to use the entire “address” dict as a key in the […]
Convert between timezones in Javascript
Javascript dates are frequently stored in UTC, or Coordinated Universal Time, specifically so we don’t have to store time-zone-adjusted dates. You’ll often see ISO-formatted date strings ending in Z like this: 2022-12-15T21:07:49.883Z. That Z means “Zulu”, and is just military jargon for “UTC”. Greenwich Mean Time (GMT) is also exactly the same: GMT === UTC […]
Merging Python Dictionaries
Merging dictionaries in Python is a common task, and there are a few ways to do it: The unpacking operator ** Since Python 3.5 (circa 2015), the simplest way to merge dictionaries has been with Python’s unpacking operator ** : It’s worth noting that if the objects being merged share a key, the last one […]