code and command snippets


out-of-context lines of python

if __name__ == "__main__":
    l = ["a", "b", "c"]
    for (i, v) in enumerate(l): print(i, v)

tweaking gnome without gnome-tweaks

gsettings list-recursively | sort | less
gsettings list-keys org.gnome.desktop.interface | sort
gsettings get org.gnome.shell.overrides attach-modal-dialogs
# etc

gsettings set org.gnome.desktop.interface clock-show-seconds true
gsettings set org.gnome.desktop.calendar show-weekdate true
gsettings set org.gnome.desktop.wm.preferences button-layout ':minimize,maximize,close'
gsettings set org.gnome.shell.overrides attach-modal-dialogs false
gsettings set org.gnome.settings-daemon.plugins.media-keys max-screencast-length 180

.desktop files

[Desktop Entry]
Name=Thing
Type=Application
Comment=Program, v.1.8.1
Exec=/home/user/place/subdir/thing.x86_64
Terminal=false
Categories=Game
Icon=/home/user/place/subdir/logo.png

GNOME 3 monitors ~/.local/share/applications/ for new .desktop files; you'll probably want to copy your .desktop file there. The commands desktop-file-validate thing.desktop and desktop-file-install --dir=DIR thing.desktop exist, but I'm not sure they're necessary.

screenshot from the terminal with imagemagick

import filename (creates it as a postscript file, which is readable text (albeit with pixel data written out), actually pretty interesting)

import filename.png (also try .jpg etcetera)

if import doesn't work as is, try magick import .... doc.

ssl cert/cert-sign-request viewing commands

viewing a request (csr): openssl req -in thing.csr -noout -text

viewing a public certificate: openssl x509 -in thing.crt -noout -text

viewing a private key: openssl rsa -in thing.crt -noout -text

viewing certificates etc. in an smtp connection: openssl s_client -connect mail.example.com:587 -showcerts -starttls smtp

vowel reduction for linguistic fun

perl -n -E 's/[aeiouy]/e/g;s/[AEIOUY]/E/;s/ï/y/;s/Ï/Y/;print' < input — turn english vowel letters (including Y) into E; if the input text file is edited beforehand and has its consonantal Y's replaced with Ï and ï then those will get turned back to y.

perl -n -E 's/ä|ö/e/g;s/Ä|Ö/E/g;s/[eiy]/e/g;s/[EIY]/E/g;s/[aou]/o/g;s/[AOU]/O/g;print' < input — a variant for finnish that preserves vowel frontness and backness.

charfreq.py

import fileinput
cfq = {}
for l in fileinput.input():
    for c in l:
        if c not in cfq:
            cfq[c] = 1
        else:
            cfq[c] += 1
for (k, f) in map(lambda k: (k, cfq[k]), sorted(cfq.keys())):
    print(repr(k), f)

gcc's unsigned integer division magic

dividing by a constant d is implemented as multiplying by a big number that is the inverse of the constant multiplied by a big power of two (2k), then dividing the extra power of two out with a right-shift. (explanatory source.) if d is a power of two, this can just be a straight right-shift. for non-powers of 2, k should be word-width+⌈log₂ d⌉ so that multiplication with every integer of word-width works

n d = n d × 2 k 2 k = 2 k d × n 2 k

allbytes.py and highbytes.py

import sys
def out(bb):
    sys.stdout.buffer.write(bb)
out(b"Under the line of hyphens will be every byte,\n")
out(b"arranged 32 to a line, except that the first\n")
out(b"line's tenth character (0x0A) will be 0x20.\n")
out(b"---------------\n")
#sys.stdout.buffer.flush()
out(bytes.fromhex("00010203 04050607 0809200B 0C0D0E0F"))
out(bytes(range(0x10, 0x20)) + b"\n")
out(bytes(range(0x20, 0x40)) + b"\n")
out(bytes(range(0x40, 0x60)) + b"\n")
out(bytes(range(0x60, 0x80)) + b"\n")
out(bytes(range(0x80, 0xA0)) + b"\n")
out(bytes(range(0xA0, 0xC0)) + b"\n")
out(bytes(range(0xC0, 0xE0)) + b"\n")
out(bytes(range(0xE0, 0x100)) + b"\n")
import sys
def out(bb):
    sys.stdout.buffer.write(bb)
out(b"After the next line will be every byte from 0x80 to 0xFF,\n")
out(b"as two lines of 64 bytes.--------------------------------\n")
out(bytes(range(0x80, 0xC0)) + b"\n")
out(bytes(range(0xC0, 0x100)) + b"\n")

forth: ackermann 3-variable φ function

: 3dup ( n1 n2 n3 - n1 n2 n3 n1 n2 n3 ) { a b c } a b c a b c ;

: ackermann_phi ( n n n - n ) ( m n p ) dup 0 = if drop + else swap dup 0 = if drop dup 1 = if drop drop 0 else 2 = if drop 1 endif endif else swap 3dup swap 1 - swap recurse rot drop swap 1 - recurse endif endif ;

python: playing with tracery

Using this python port. Some classical context-free grammars from comp-sci classes.

import tracery
rules = {
    'original': ['#palindrome#', '#parens#', '#anbn#'],
    # palindromes are length-limited but others can in principle recurse forever
    'palindrome': ['#palin1#', '#palin2#', '#palin3#', '#palin4#'],
    'palin1': ['0#palin2#0', '1#palin2#1'],
    'palin2': ['0#palin3#0', '1#palin3#1'],
    'palin3': ['0#palin4#0', '1#palin4#1'],
    'palin4': ['00', '11', '0', '1'],
    'parens': ['#paren1#', '#paren2#', '#paren3#', '#paren4#'],
    'paren1': ['#paren2##paren2#', '(#paren2#)', '[#paren2#]'],
    'paren2': ['#paren3##paren3#', '(#paren3#)', '[#paren3#]'],
    'paren3': ['#paren4##paren4#', '(#paren4#)', '[#paren4#]'],
    'paren4': ['a', 'b', '[#parens#]', '(#parens#)'],  
    'anbn': ['#a4b4#', '#a3b3#', '#a2b2#', '#a1b1#'],
    'a4b4': '<#a3b3#>',
    'a3b3': '<#a2b2#>',
    'a2b2': '<#a1b1#>',
    'a1b1': ['<>', '< #anbn# >']
}
grammar = tracery.Grammar(rules)
print(grammar.flatten("#original#")

chroot rescue

/boot = 7a52...
/boot/efi = 5be9...
ls /dev/disk/by-uuid -l

mkdir /mnt/sysimage
mount /dev/mapper/vg-root /mnt/sysimage
mount /dev/sda5 /mnt/sysimage/boot
mount /dev/sda4 /mnt/sysimage/boot/efi
chroot /mnt/sysimage
mount -t proc proc /proc # (if warned to do so)

python: random hex colour triplet

from random import randint
print(f"{randint(0, 256**3):06x}")

strip whitespace from beginnings of lines

sed -Ee 's/^( +)//'

ssh

force login with password: ssh -o PreferredAuthentications=password ...