Improve everyday.

defaultdict.md

|

Default dicts in Python

  • Initialize the values with predefined element upon calling the key.
from collections import defaultdict

# Basic
d = defaultdict(int)
d['a']
>> 0

d = defaultdict(list)
# This does not fail with a defaultdict
d['a'].append('coucou')
d['a']
>> ['coucou']

# Advanced 
d = defaultdict(lambda: {'state': 4, 'weather': 'sunny'})
d['a']
>> {'state': 4, 'weather': 'sunny'}

sorting-special-chars.md

|

Sorting with special characters

import PyICU

list = ['é', 'è', 'a', 'b', 'e', 'à']
print(sorted(list))
# >> ['a', 'b', 'e', '\xc3\xa0', '\xc3\xa8', '\xc3\xa9']

COLLATOR = PyICU.Collator.createInstance(PyICU.Locale('fr_FR.UTF-8'))
print(sorted(cmp=COLLATOR.compare))
# >> ['a', '\xc3\xa0', 'b', 'e', '\xc3\xa9', '\xc3\xa8']

general.md

|

General tips for tmux utilisation

Swap panes

PREFIX { - Swap active pane left PREFIX } - Swap active pane right

PREFIX : swap-pane -s {start} -t {end} - Swap panes

### Rebind keys

-n - rebind a key without prefix M - Alt key

Use Alt-vim keys without prefix key to switch panes

Remember to disable the menu access key from your terminal settings (edit -> keyboard shortcuts -> disable menu access keys)

bind -n M-h select-pane -L
bind -n M-j select-pane -D
bind -n M-k select-pane -U
bind -n M-l select-pane -R

Shift arrow to switch windows

bind -n S-Left  previous-window
bind -n S-Right next-window

Split panes conserving the directory

bind '%' split-window -h -c '#{pane_current_path}'  - Split panes horizontal
bind '"' split-window -v -c '#{pane_current_path}'  - Split panes vertically
bind 'c' new-window -c '#{pane_current_path}'       - Split panes vertically

Removing_spaces.md

|

Add an auto regex to your ~/.vimrc, so that each time you write a file with vim (:w or :x), it deletes all the useless spaces at the end of the lines.

" Remove useless spaces at the end of the line
autocmd BufWritePre * %s/\s\+$//e

find-and-execute-them-all.md

|

Problem : You want to execute the same command into a lot of subdirectories ?

Goal : We are going to use find

Search your files with find like this :

find . -name "fileToBeFound"

and add your command ({} is the path to the occurence of the file found)!

find . -name "fileToBeFound" -exec the command to execute {} \;

Note you can use wildcards to search file, add more than one directory, search by type (directory, file …)