Today I learned how to encode a string in Python using base64
built-in module. This question posted on stackoverflow helps me through it. Following is my main usage:
>>> import base64
>>> encode = 'but you have to decode it'.encode()
>>> base64.b64encode(encode)
b'YnV0IHlvdSBoYXZlIHRvIGRlY29kZSBpdA=='
master
branch¶Today I learned how to initiate main
as a default branch on git! This article from GitHub helps me from updating git to version 2.28 to creating a new repository with main
branch, instead of master
.
In addition, I managed to changed the default branch from existing project such as this repository to main
. This article and this StackOverflow post help me to make this happen. A small step worth doing indeed!
Following is the snippet from the command line:
ledwindra@R90SEMHV:~/today-i-learned$ git status
On branch main
Your branch is up to date with 'origin/main'.
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: README.md
ledwindra@R90SEMHV:~/today-i-learned$ git add README.md
ledwindra@R90SEMHV:~/today-i-learned$ git commit -m "Today I learned"
[main e1f5f04] Today I learned
1 file changed, 25 insertions(+), 8 deletions(-)
ledwindra@R90SEMHV:~/today-i-learned$ git push origin main
Username for 'https://github.com': ledwindra
Password for 'https://ledwindra@github.com':
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 1.42 KiB | 485.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/ledwindra/today-i-learned.git
d371988..e1f5f04 main -> main
Besides, today I learned about Audrey Tang, Taiwan's Digital Minister, who's also--according to her Wikipedia page--the first transgender official in the top executive cabinet. This afternoon while I was doing my work, I saw this tweet, which basically tells Audrey is a well known hacker in the Haskell and Perl communities.
Later I dig deeper about her, from this article (also from that tweet). It's totally a worth read on how she and the Taiwanese government handled the COVID-19 pandemic so well. "She's really fantastic", my mind said. Then I surfed to her talk and interview.
What's really fascinating about her is not only that she's technically savvy, but she also shows us that being a prodigy doesn't necessarily mean she loses her human touch. Basically she's not a typical brilliant asshole which is ubiquitous in the tech scene and also how popular culture depicts it (like Elon Musk or Mark Zuckerberg). As a closing remark for today's session, below I quote her job description, which is totally beautiful:
From today, I declare that I'm her fan. 🥰
I don't think I learned something new today. Mostly I was just continue watching and reading stuffs about Audrey Tang (both related to his work as a minister and his effort in Raku development). Besides, I started reading Break 'em Up, which I find quite interesting. Nothing particular from the it that caught my attention so far, but will update a snippet here if it does.
Today I learned how to make a progress bar using Python. So that when I run the program, it will appear on the console. Not only that, I tried to modify it so that it resembles a drum roll 😎. Check this out:
This StackOverflow post helps to walk through the process, though I didn't copy and paste it thoroughly 🙏🏽. The code snippet is as follows:
def problem_one(self, n=1000, interval=0.001, dash='-', equal='='):
"""
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000.
"""
# set initial numbers
i = 0
answer = 0
# as long as i is below N (10, 1000, etc)
# if it's divisible by 3 or 5
# add that number with the answer
while i < n:
progress = int((i / n) * 101)
progress_dash = dash * int((100 - progress) / 2)
progress_equal = equal * int(progress / 2)
print(f'Progress: |{progress_equal}{progress_dash}| {progress}%', end='\r')
if i % 3 == 0 or i % 5 == 0:
answer += i
i += 1
sleep(interval)
print("\n")
return answer
Oh, you can also see my Project Euler repository too! The unit tests in the repository still don't include the progress bar, though. 😄
I learned nothing particular today but apparently I'm enjoying New Girls (TV series)(available on Netflix). Today I just finished its first (out of seven) season.
Alright, alright, here's another one. Today I learned that in Python, a list of tuples can be converted to dictionary, which I find it useful to solve a certain problem, for example this Project Euler problem
>>> import string # it's built-in module, no need to pip install
>>> alphabet = [x for x in string.ascii_uppercase] # can be lowercase too
>>> alphabet
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
>>> alphabet = [(alphabet[x], alphabet[x].replace(alphabet[x], str(x+1))) for x in range(len(alphabet))] # a list of tuples
>>> alphabet
[('A', '1'), ('B', '2'), ('C', '3'), ('D', '4'), ('E', '5'), ('F', '6'), ('G', '7'), ('H', '8'), ('I', '9'), ('J', '10'), ('K', '11'), ('L', '12'), ('M', '13'), ('N', '14'), ('O', '15'), ('P', '16'), ('Q', '17'), ('R', '18'), ('S', '19'), ('T', '20'), ('U', '21'), ('V', '22'), ('W', '23'), ('X', '24'), ('Y', '25'), ('Z', '26')]
>>> alphabet = dict(alphabet) # convert to a dictionary in just one nice line
>>> alphabet
{'A': '1', 'B': '2', 'C': '3', 'D': '4', 'E': '5', 'F': '6', 'G': '7', 'H': '8', 'I': '9', 'J': '10', 'K': '11', 'L': '12', 'M': '13', 'N': '14', 'O': '15', 'P': '16', 'Q': '17', 'R': '18', 'S': '19', 'T': '20', 'U': '21', 'V': '22', 'W': '23', 'X': '24', 'Y': '25', 'Z': '26'}
For what it's worth, I think this can be handy in many situations. 😀
Whoa, we're now entering the 8th month already 😱! Anyway, today I learned that at the end of New Girl's first season, there's the scene which has the Clash's Death or Glory) as its background song. Then I'm reminded that in another movie where Zooey Deschanel plays a big role, (500) Days of Summer, there's a scene where another the Clash song, Train in Vain was sung by Joseph Gordon-Levitt. See the clip here.
Today I also learned how make a permutation given a value in Python in a simple way. I think performing such task is not going to be frequent in my day-to-day job, but it's really good to know that it's this easy. This StackOverflow post helps me to walk through the problem. Here's the snippet:
>>> import itertools # this built-in module is awesome!
>>> import string
>>> digit = string.digits
>>> digit
'0123456789' # a given value
>>> permutation = list(itertools.permutations(digit, len(digit))) # make permutation of the given value, each has length of it (in this case 10)
>>> permutation[:5]
[('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'), ('0', '1', '2', '3', '4', '5', '6', '7', '9', '8'), ('0', '1', '2', '3', '4', '5', '6', '8', '7', '9'), ('0', '1', '2', '3', '4', '5', '6', '8', '9', '7'), ('0', '1', '2', '3', '4', '5', '6', '9', '7', '8')]
>>> # complete list will be longer to print
This method helps me to solve 43rd's Project Euler problem.
Today I learned that writing mathematical notation without installing and compiling LaTeX can just be easily done using Jupyter in markdown mode. For example consider the following Project Euler problem:
Euler discovered the remarkable quadratic formula:
$$n^2 + n + 41$$
It turns out that the formula will produce 40 primes for the consecutive integer values $0 \leq n \leq 39$. However, when is divisible by 41, and certainly when $n = 40, 40^2 + 40 + 41 = 40(40 + 1) + 41$ is clearly divisible by 41.
The incredible formula $n^2 - 79n + 1601$ was discovered, which produces 80 primes for the consecutive values $0 \leq n \leq 79$. The product of the coefficients, −79 and 1601, is −126479.
Considering quadratics of the form:
$n^2 + an + b$, where $|a| \lt 1000$ and $|b| \leq 1000$
where $|n|$ is the modulus/absolute value of $n$ <br>
e.g. $|11| = 11$ and $|-4| = 4$
Find the product of the coefficients, $a$ and $b$ for the quadratic expression that produces the maximum number of primes for consecutive values of $n$, starting with $n = 0$.
However it's not rendered in this document. See below when I tried doing it on Jupyter:
Then so what? For me, it's really awesome if some serious documents such as academic papers which have numerous mathematical formula can just be written inside Jupyter, including the graphs, tables, and of course codes! Not only we have to compile something for a document (which can be tiresome), it can also be shared online so easily and more readable (which are totally important for general readers).
Future me or other readers might want to see that presentations can be done in Jupyter and hosted on GitHub such as this (made by me, but not totally a serious stuff). 😀
Today I learned how to make a blog using Jupyter instead of simple README markdown, thanks to this post which makes the process easier (I've used it as a reference for several times but haven't had a chance to promote them here). In addition, we can also see the slides page-by-page for each day instead of a vertical long read, which makes the current version more like a daily journal to me (and it is!). 😎
In my day-to-day work, I used GitLab instead of GitHub. Basically it's the same thing, but there are slight differences (otherwise it's gonna be f__ing boring) 🤣. One key feature that I like is the project migration. In any case for whatever reason you want to migrate your project from somewhere else (e.g. GitHub, Bitbucket), it's very seamless and convenient to do it on GitLab. Oh by the way here's my page 🖥. This makes me wonder, though, why is it as widely used as GitHub?
I'm not a heavy user when it comes to emails, but I like it when they're organized! So today I spent quite a lot of time creating new labels and assigning which emails that need to be sorted (e.g. ads, updates, forums, etc). The initial investment of time may be long (but not that loooooong!), and the returns (measured by peace of mind) is yuuuge (hopefully). Here's the short reading (but I didn't use it as a reference anyway).
Today I learned something very practical and I feel it very useful in my everyday job: automating jobs using GitHub actions. I've been doing it several times mainly for unit testing, but today I found it can be used way more than that! Like how? Suppose I have scheduled job at certain times where mostly I'm not logged on, and suppose each job makes changes to the repository. How can we automate that job and push to the repository? YES! GitHub actions!
There are several links that I could throw upon here which help me get through it, but mainly the official documentation and the official repository are really helpful!
One important thing that we need to pay attention to is the YAML file as it can be tricky. Here's one example of mine for my NBER project:
# name of the actions is build (can be anything)
name: build
# it triggers the action when there's a push or pull request to master (or any other) branch
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
build:
# runs on Ubuntu
runs-on: ubuntu-latest
# install Python and the dependencies
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
# runs the main job which is to scrape papers from NBER
- name: Run script
run: |
python src/get_paper.py --initial_id 1 --timeout 10 --sleep 10
# performs unit testing
- name: Test with pytest
run: |
pytest
# this is the tricky part
# when there are changes, GitHub bot will push to the master (or any other) branch
# automatically!
git config user.name github-actions
git config user.email github-actions@github.com
git config --global pull.ff only
git add .
git commit -m "Hopefully works out"
git push
When triggered, we can see the log in real time (how awesome is that!). The sample workflow was successful and can be seen here. On the other hand, as of this writing, the full automatic scraper still runs here. I'm not sure for how long it will run, hence fingers crossed! 🤞🏽
Today I learned that GitHub actions have maximum execution time, which is six hours. This happened to me because my NBER script crashed during building 😭. Following is the message:
Fortunately, I also wrote another YAML file that executes cron job, where each job scrapes only one NBER paper. Consequently, the execution time is way shorter than the build. Following is the message:
Lessons learned? Frequent small progresses are better than one big but inconsistent progress. Yes, I'm talking about life 🙃. As a consequence, I would see my daily contributions skyrocket starting today 🚀.
So yesterday Working in Public: The Making and Maintenance of Open Source Software, one of my 2020 book lists, was launched. However, it seemed that it doesn't have the Kindle version. To me, reading Kindle book is not more about convenience or functional, but it usually offers free sample, so I can decide whether to proceed to purchase or not 📖.
Fortunately I saw the author tweeting about the book launched. So I asked here whether the Kindle version will be available or not. She said it already is. I was confused then I showed my above pic but apparently it's the non-US issue. The solution which was suggested by Amazon themselves was to change my current location from Indonesia to anywhere else which Amazon offers (apparently the country that I'm living is not, albeit having done Kindle purchases several times without problems). But I guess it also depends on the publishers hence this thing happened to me. Finally, though, the book is on my device and I can't wait to spend time reading it!
Today I learned that GitHub actions can be triggered manually. It's really helpful as I don't have to wait until the cron job starts. I've read about this too yesterday but haven't had a chance to implement it straight away 😝. Here's what the documentation says. It's apparently still a relatively new feature! Here's what the YAML example looks like:
name: cron-audit-committee
on:
workflow_dispatch:
# below, you can just continue the YAML as usual
I also learned that the activity color in GitHub can change when by any metric within certain period it increases quite significantly than usual. Here's the comparison between last week and today:
It's almost likely due to the increased activity of my cron jobs 🤣.