Skip to content

Instantly share code, notes, and snippets.

Laurent Franceschetti fralau

  • SettleNext
Block or report user

Report or block fralau

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@fralau
fralau / traceback.md
Created Sep 17, 2018
How to get a "traceback" without exiting Python
View traceback.md

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 / excel_lookup.md
Last active Aug 30, 2018
Finally a simple table lookup function in Excel!
View excel_lookup.md

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 / key-pairs-cmdline.md
Last active Sep 30, 2019
Command line: create dictionary from key-value pairs
View key-pairs-cmdline.md

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 / file_wrapper.md
Last active Aug 30, 2018
Creating a file object, without immediately opening the file descriptor
View file_wrapper.md

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 / readme.md
Last active Apr 11, 2018
Python: know whether the current directory is part of a git repo
View readme.md

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 / os_command.md
Last active Mar 29, 2018
Python: A simple and general solution for calling OS commands
View os_command.md

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 / wipe_git_files.md
Last active Mar 29, 2018
git: Scripts to wipe a (large) file from a repository
View wipe_git_files.md

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

@fralau
fralau / GoldenString.md
Last active Mar 29, 2018
Golden String (pattern):
View GoldenString.md

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:

You can’t perform that action at this time.