Skip to content

Instantly share code, notes, and snippets.

View fralau's full-sized avatar

Laurent Franceschetti fralau

  • SettleNext
  • Geneva, Switzerland
View GitHub Profile
@fralau
fralau / key-pairs-cmdline.md
Last active December 6, 2023 15:10
Command line: create dictionary from key-value pairs

Converting an arbitrary list of key-value pairs from the command-line into a Python dictionary

Problem

You want to create a series of key-value pairs from the command line, using the argparse library, e.g.:

command par1 par2 --set foo=hello bar="hello world" baz=5

This is typically useful when you want to clearly distinguish":

  1. Ordinary arguments for the command-line utility itself (output, input, format, etc.) from
  2. A set of key-value pairs you want to pass to the python application. This is especially valid when you do not want that set of values to be predetermined, as this can save a lot of code.
@fralau
fralau / readme.md
Last active August 4, 2023 13:35
Create a standard banner for every machine (terminal)

Create a standard banner for every machine (terminal)

In /etc/profile:

# Banner
echo ${HOSTNAME%%.*} | figlet | lolcat
export PATH="/home/pi/.local/bin:$PATH" # for pyarchey
pyarchey
@fralau
fralau / os_command.md
Last active July 7, 2021 13:30
Python: A simple and general solution for calling OS commands

A simple and general solution for calling OS commands from Python

Problem

With Python, calling an OS command in a simple way has long been a pain in the neck, with multiple solutions continuously asked, answered, counter-answered and sometimes deprecated (os.system which was phased out, subprocess,etc.).

Diagnosis

The key is to realize that there are, really, four ways of calling an OS command from a high level language:

  1. as a command: you want to print the output as it comes
  2. as a function: you want no printed output, but a result in the form of a string
  3. as a function with side effect: you want to execute the command, watch what it does, and then analyse the output.
@fralau
fralau / !Login banner.md
Last active November 21, 2020 09:22
Login banner on restricted host (e.g. on Infomaniak web hosting)

Login banner + system info on restricted host

This will typically work on Infomaniak's standard webhosting.

It is useful to get a host banner and system information when logging in, even on a barebone remote host (few install options are available, no package manager, etc.).

  1. In the home directory, copy the attached .bash_profile (or update if already exists).
  2. Using figlet (on your local machine), create a banner with the nickname you want to give to the host, e.g. figlet MyHost, and copy the string into a banner.txt file in the home directory of the remote host.
@fralau
fralau / traceback.md
Created September 17, 2018 09:07
How to get a "traceback" without exiting Python

Issue

Find which "path" the stack has used to call a procedure (this may be useful in frameworks with callbacks)

Solution

Use the inspect function

import inspect

def show_stack():
@fralau
fralau / file_wrapper.md
Last active August 30, 2018 09:46
Creating a file object, without immediately opening the file descriptor

Problem: How to defer the allocation of a file descriptor in Python?

The problem with an open statement, is that it immediately allocates the file descriptor (a commodity in limited supply).

Sometimes you want something else: a file object that you want to keep in store, and to be used later or not used at all. The file descriptor should be allocated only when actually opening the file and then it should be closed as soon as possible. The benefit is that you could have an unlimited supply of file objects (not filenames), without having to worry too much about allocated file descriptors.

Idea

@fralau
fralau / excel_lookup.md
Last active August 30, 2018 09:42
Finally a simple table lookup function in Excel!

Problem: how to make a simple lookup by key/field name in Excel?

I was puzzled by this question in StackOverflow: In Excel there is NO simple way to lookup a value in a named table, where the first column is the key (see how to name a table).

I find it hard to believe that Microsoft has not gone all the way to provide a solution to its customers!

Suppose, that, in your Excel grid, you have a table like this, named sales:

A B C D
1 Name Amount Country Quarter
@fralau
fralau / readme.md
Last active April 11, 2018 10:54
Python: know whether the current directory is part of a git repo

How to know wether current directory is part of a git repo

Idea

The git rev-parse command returns 0 if the current directory is in a git repo and an error code otherwise. It is thus necessary to call the bash command, suppressing stdout and stderr, in order to get the return code.

Code

import os, subprocess
def is_git_repo():
 "Check whether this is a git repo"
@fralau
fralau / GoldenString.md
Last active March 29, 2018 13:25
Golden String (pattern):

Golden String: A Python design pattern to salvage a function returning a string

Problem:

What to do if you have a well-used API with a function returning a string, and you want to return other information without breaking the calling code?

Solution

Principle

Pass that information as attributes to the string itself! Since the standard str type does not allow it, a new class GoldenStr must be created.

In the called function

In the called function, you could use the code below to write:

@fralau
fralau / wipe_git_files.md
Last active March 29, 2018 13:21
git: Scripts to wipe a (large) file from a repository

Wiping a (large) file from a git repository

Issue

You might have a file in a repository, which you want to delete because it is too large (or because it contains passwords, etc.).

Solution

General principle

The following procedure will wipe a file from your repository and compress it, so your repository will be reduced in size. It will not delete your work copy.

Procedure