Bash: trim affixes from variable values

${variable%pattern} Trim the shortest match from the end ${variable##pattern} Trim the longest match from the beginning ${variable%%pattern} Trim the longest match from the end ${variable#pattern} Trim the shortest match from the beginning $ s="basics-01-understanding-the-course-project-starting-code.zip" $ echo ${s%.zip} basics-01-understanding-the-course-project-starting-code $ mkdir ${s%.zip} $ unset s https://tldp.org/LDP/LG/issue18/bash.html

January 19, 2023 · Fernando Costa Bertoldi

Manage multiple virtual environments with pew

IMO pew is better than virtualenvwrapper because it doesn’t depend on the activate/deactivate scripts inside each virtual environment. When activating a virtualenv with pew, instead of sourcing it into the existing shell process, it launches a sub-shell, thus providing better isolation. https://github.com/berdario/pew Prefer Subshells for context

November 3, 2022 · Fernando Costa Bertoldi

Git history of a single line

TIL: git history of a single line: git log -L<linenum>,+1:<path> f.ex. to see version changes of a package in line 40 of a pipenv config, git log -L40,+1:Pipfile

October 28, 2022 · Fernando Costa Bertoldi

Check Secure Boot in Linux

TIL: use the mokutil command to check if Secure Boot is enabled. secureBoot:~$ mokutil --sb-state SecureBoot disabled “The mokutil command is used to manage Machine Owner Keys (MOK). These keys are used by the shim layer to validate grub2 and kernel images and can also be used to verify that Secure Boot is enabled.” https://www.kvaser.com/developer-blog/secure-boot-linux-systems/

October 28, 2022 · Fernando Costa Bertoldi

Pytest flags for shorter output

TIL: pytest –tb=short –disable-warnings –no-cov

September 14, 2022 · Fernando Costa Bertoldi

Git and SSH

TIL: git clone [email protected]:fcbertoldi/todo_django.git uses the SSH protocol, so it is a shortcut to git clone ssh://[email protected]:fcbertoldi/todo_django.git https://git-scm.com/book/en/v2/Git-on-the-Server-The-Protocols $ git clone [user@]server:project.git The SSH service uses the same user git for all GitHub, as it is a convention to have the user name the same as the service. https://stackoverflow.com/questions/47664768/why-does-git-using-ssh-use-git-as-a-username

August 31, 2022 · Fernando Costa Bertoldi

Diffing two tables

To diff specific columns of two .csv files, using miller CLI tool and process substitution <() $ colordiff -u <(mlr --icsv --opprint cut -f 'id,avg_sourcing_spread' then format-values -f '%.4lf' then put '$avg_sourcing_spread=round($avg_sourcing_spread*100.0)' service.csv) <(mlr --icsv --opprint cut -f 'id,sourcing_spread' then format-values -f '%.4lf' then put '$sourcing_spread=round($sourcing_spread*10000.0)' superset.csv) We can also use the spreadsheet to diff two tables: highlight the first table select the conditional formatting tool add the rule A1<>Sheet2!A1 In LibreOffice Calc there is a specific tool to compare tables....

August 10, 2022 · Fernando Costa Bertoldi

Diff long lines

TIL: To compare long lines with diff, break each of them in multiple lines https://www.johndcook.com/blog/2021/04/22/moby-diff/ $ diff -u <(fold -s -w 20 temp1.txt) <(fold -s -w 20 temp2.txt)

August 9, 2022 · Fernando Costa Bertoldi

Django custom constructor/factory

TIL: to add a custom constructor to a Django model, use a custom manager method: https://docs.djangoproject.com/en/3.2/ref/models/instances/#django.db.models.Model class BookManager(models.Manager): def create_book(self, title): book = self.create(title=title) # do something with the book return book class Book(models.Model): title = models.CharField(max_length=100) objects = BookManager() book = Book.objects.create_book("Pride and Prejudice")

June 3, 2022 · Fernando Costa Bertoldi

Make uses topological sorting

TIL: Make does topological sorting of the target and its dependencies # Makefile d: a b c @echo 'd ' c: a b @echo -n 'c ' b: a @echo -n 'b ' a: @echo -n 'a ' $ make a b c d

May 26, 2022 · Fernando Costa Bertoldi