You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Instantly share code, notes, and snippets.
Laurent Franceschetti
fralau
I studied computer science at a Swiss institute of technology.
I like to share code and I enjoy writing documentation.
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.
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
importos, subprocessdefis_git_repo():
"Check whether this is a git repo"
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:
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.
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.).
In the home directory, copy the attached .bash_profile (or update if already exists).
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.
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:
as a command: you want to print the output as it comes
as a function: you want no printed output, but a result in the form of a string
as a function with side effect: you want to execute the command, watch what it does, and then analyse the output.
This is typically useful when you want to clearly distinguish":
Ordinary arguments for the command-line utility itself (output, input, format, etc.) from
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.