.-----. .----. .-----. .-----. / ,-. \ / .. \ / ,-. \/ ,-. \ '-' | |. / \ .'-' | |'-' | | .' / | | ' | .' / .' / .' /__ ' \ / ' .' /__ .' /__ | | \ `' / | || | `-------' `---'' `-------'`-------'
December
28
14:21 Python operator precedence
() ** (exponent) +x, -x, ~x (unary plus, unary minus, bitwise NOT) *, /, //, % +, - <<, >> (bitwise shift) & (bitwise AND) ^ (bitwise XOR) | (bitwise OR) ==, !=, >, >=, <, <=, is, is not, in, not in not and or
23
15:35 bash - trim leading zeros
$ echo $((10#02)) 2
07
20:37 - vim window splits reference
I keep forgetting!
no splits - current window as [o]nly window
ctrl w o
swap windos
ctrl w x
split opened buffers (horz)
ctrl w s
split opened buffers (vert)
ctrl w v
go to split
ctrl w h/j/k/l
open new file as split
:sp <file>
:vsp <file>
20:40 Advent of Code
I attempted python solutions daily, with most ones having golf solutions.
Some have shell, golang, and lua solutions too.
https://github.com/hedyhli/adventofcode
https://sr.ht/hedy/adventofcode
Currently up to:
2022: day 7 (ongoing!)
2020: day 9
2021: day 2
November
10
21:06
My plant on astrobotany is still alive despite my idleness of 4+ months! Damn
20:43 Advent of Code revival, (n)vim macros
finishing up Advent of Code 2020
https://sr.ht/~hedy/adventofcode/
https://github.com/hedyhli/adventofcode
Had to go through each puzzle page, copy its plain-text, paste it in a new file, then apply formatting manually.
There were a LOT of wrapping `code blocks` and **bold** around words. Had to make use of 5 macros (personal record!)
@a = i**<Esc>Eli**<Esc> # bold word @b = i`<Esc>Ei`<Esc> # code word @c = i**<Esc>2Wi**<Esc> # 2 bold words @d = i`<Esc>Eli`<Esc> # code word that ends with a space @i = i`<Right>`<Esc> # single-char inline code
(Note I should probably write that code block in Vimscript syntax, can't remember at the moment...)
A lot of batch string parsing and validation in the first four days.
September
18
08:13 Vim debug logs
Noting down things that had saved me so I (hopefully) don't have to search up stuff every time
https://stackoverflow.com/questions/3025615/is-there-a-vim-runtime-log
August
31
15:55
Read this yesterday:
Dictatorships are good at concealing the problems they create while democracy is good at advertising its defects.
Daily dose of Vsauce wonder
[YouTube] "Is Earth Actually Flat?"
[YouTube] "Messages For The Future"
May
13
18:36 TIL - diff unsaved changes in vim
:w !diff % -
08
18:06 journal meta
My script that parses this journal page and dumps out the content in tinylog format had hardcoded the year to 2021. Can't believe I'd only found out about this 5 months into 2022, anyways it's fixed now.
The CGI scripts that is supposed to run that ^ on demand and deploy any changes to my tinylog.gmi feed doesn't work still because CGI is run by a single user in gemserv. I'll think about how I can (safely) fix that soon.
07
15:00 spartan:// on lagrange
Lagrange v1.13 supports spartan!
12:34 TIL - indexing substring and list elements in python
I always thought there were two list methods to find the index of an element in python - find and index. find would raise an IndexError when the item isn't found in the list, whereas index would return -1 in that same case.
Nope, that's completely wrong.
First, there's only .index for lists:
>>> ['a', 'b', 'c'].index('a')0
And it raises a ValueError if the item isn't found:
>>> ['a', 'b', 'c'].index(404)Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: 404 is not in list
Method signature:
index(self, value, start=0, stop=9223372036854775807, /) Return first index of value. Raises ValueError if the value is not present.
Second, the "find vs. index" is for strings, not lists - both find and index methods exist for strings:
>>> 'abc'.index('a')0>>> 'abc'.find('a')0
So what are the differences? Turns out my previous understanding of how find/index handles non-existent items had been mixed up. When attempting to index a substring that isn't actually in the string, index raises a ValueError, and find returns -1:
>>> 'abc'.index('z')Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found>>> 'abc'.find('z')-1
(Note that you can use either to search for "substrings" - i.e: `'abc'.index('bc')`)
Method signatures:
index(...) S.index(sub[, start[, end]]) -> int Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation. Raises ValueError when the substring is not found.
find(...) S.find(sub[, start[, end]]) -> int Return the lowest index in S where substring sub is found, such that sub is contained within S[start:end]. Optional arguments start and end are interpreted as in slice notation. Return -1 on failure.
Just for fun, I also went ahead and tested out the performance of the two methods
$ python3 -m timeit "'abcdefghijklmnopqrstuvwxyz'.index('p')" 2000000 loops, best of 5: 131 nsec per loop $ python3 -m timeit "'abcdefghijklmnopqrstuvwxyz'.find('p')" 2000000 loops, best of 5: 103 nsec per loop
For cases where the item isn't found:
$ python3 -m timeit -s "s='abcdefghijklmnopqrstuvwxyz'" \ "try: s.index('Z')" \ "except: pass" 1000000 loops, best of 5: 310 nsec per loop $ python3 -m timeit -s "s='abcdefghijklmnopqrstuvwxyz'" \ "try: s.find('Z')" \ "except: pass" 2000000 loops, best of 5: 110 nsec per loop
python3 -m timeit -s "s='abcdefghijklmnopqrstuvwxyz'" \ "if s.find('Z') == -1: pass" 2000000 loops, best of 5: 119 nsec per loop
Seems like .find() clearly wins on speed.
Here's an example of searching for the index of a substring in a string, then doing something with it:
$ python3 -m timeit -s "s = 'abcdefghijklmnopqrstuvwxyz'" \ "if 'z' in s:" \ " index = s.find('z')" \ " print('do stuff with', index)" \ "else:" \ " print('not found')" |tail -n1 500000 loops, best of 5: 700 nsec per loop
$ python3 -m timeit -s "s = 'abcdefghijklmnopqrstuvwxyz'" \ "if index := s.find('z') != -1:" \ " print('do stuff with', index)" \ "else:" \ " print('not found')" | tail -n1 500000 loops, best of 5: 686 nsec per loop
Interesting! The second method does seem more idiomatic and "clean", but can .find() truly beat python's native operator?
$ python3 -m timeit -s "s='abcdefghijklmnopqrstuvwxyz'" \ "if 'Z' not in s: pass" 10000000 loops, best of 5: 31 nsec per loop $ python3 -m timeit -s "s='abcdefghijklmnopqrstuvwxyz'" \ "if 'xyz' not in s: pass" 5000000 loops, best of 5: 40.3 nsec per loop $ python3 -m timeit -s "s='abcdefghijklmnopqrstuvwxyz'" \ "if 'xyZ' not in s: pass" 5000000 loops, best of 5: 39.6 nsec per loop
$ python3 -m timeit -s "s='abcdefghijklmnopqrstuvwxyz'" \ "if s.find('Z') == -1: pass" 2000000 loops, best of 5: 119 nsec per loop $ python3 -m timeit -s "s='abcdefghijklmnopqrstuvwxyz'" \ "if s.find('xyz') == -1: pass" 2000000 loops, best of 5: 137 nsec per loop $ python3 -m timeit -s "s='abcdefghijklmnopqrstuvwxyz'" \ "if s.find('xyZ') == -1: pass" 2000000 loops, best of 5: 140 nsec per loop
Nope :) It does cost a method call.
---
Python version used: 3.8.12
Further exploration:
gemini://hedy.tilde.cafe/help/py?list.index
gemini://hedy.tilde.cafe/help/py?str.index
gemini://hedy.tilde.cafe/help/py?str.find
https://docs.python.org/3/tutorial/datastructures.html
https://docs.python.org/3/library/stdtypes.html#str.index
https://docs.python.org/3/library/stdtypes.html#str.find
https://docs.python.org/3/library/timeit.html
January
02
21:34 hello!
I took quite a long (forced, unavoidable) break to focus on my studies. Thankfully I've updated by tinylog-gen script so it can merge my 2021 and 2022 files now. Speaking of which, happy new year :)
I generally don't care about it though -- I mean it's just another revolution around the sun plus about 3/4th of a day, right? Also more or less beginning from a rather arbitrary point in the orbit anyway.
It's amazing coming back to my inbox reading mailing list archives to read what I'd missed. By the way, I'm surprised I still remember all my shortcuts and keybinds I've set up in nvim, tmux etc.
Most likely for another day to start working on my projects again, sadly. Bye!
/journal/