Shell Snippets

Linting & Project Setup

Python trinity

flake8
pytype
black --check --line-length 99 --exclude '/(\.git|\.pytype|venv|venv_test)/' .

setup.cfg

[flake8]
exclude = venv/*,
    .git,
    __pycache__,
    *_test.py

# conflicts with black
ignore = W503, E203, E501

[pytype]
exclude =
    **/*_test.py
    venv/*
inputs = **/*.py
python_version = 3.6
pythonpath = .
keep_going = True

Clang-Format

#!/bin/bash

shopt -s globstar

clang-format-x -style=file -i src/**/*.cc src/**/*.h

.clang-format

---
Language:            Cpp
BasedOnStyle:        Google
BreakStringLiterals: false
ColumnLimit:         99

C++ Build

.bazelrc

# force using clang-x
build --action_env=CC=/usr/bin/clang-x
build --action_env=CXX=/usr/bin/clang++-x

build --workspace_status_command=status.sh

COPTS.bzl

COPTS = [
    "-std=c++14",
    "-Wall",
    "-Werror",
    "-Wextra",
    "-Wpedantic",
    "-Wshadow",
    "-Wsign-compare",
    "-Wunreachable-code",
    "-g",
    "-iquotesrc/",
]

load("//src:COPTS.bzl", "COPTS")

cc_binary(
    copts = COPTS,
    ...
)

Tests

C++ Test structure

*_test.cc

TEST(NameOfClassOrFnTest, Simple) {
  ...
}

TEST(NameOfClassOrFnTest, DoesSomething) {
  ...
}

TEST(NameOfClassOrFnTest, DoesSomethingElse) {
  ...
}

Python Test structure

*_test.py

import unittest
from unittest.mock import MagicMock, Mock, patch
from truth.truth import AssertThat

class ClassNameTest(unittest.TestCase):
    def setUp(self):
        self.x = MagicMock(spec=ClassOrFunctionBeingTested)

    def testSomeMethod_simple(self):
        ...

    def testSomeMethod_handlesException(self):
        ...

    def testSomeMethod_doesSomething(self):
        ...

Run using

python -m unittest discover -p '*_test.py'

python -m unittest my_module.my_submodule_test
python -m unittest my_module.my_submodule_test.ClassOrFunctionNameTest
python -m unittest my_module.my_submodule_test.ClassOrFunctionNameTest.testSomeMethod_simple

Various

Equivalent Dockerignore/Gitignore

.gitignore

# dependencies
node_modules
.pnp
.pnp.js

# misc
.vscode
.DS_Store

.dockerignore

# dependencies
**/node_modules
**/.pnp
**/.pnp.js

# misc
**/.vscode
**/.DS_Store
**/Dockerfile
**/.dockerignore
**/docker-compose.yml
**/README.md

Creating a private fork on Github

git clone git@github.com:org/repo.git
cd repo
git remote rm origin
git remote add origin git@github.com:private-org/repo.git
git remote add upstream git@github.com:org/repo.git
git push --all
git push --tags
git fetch
git checkout -b upstream-master upstream/master
git checkout master
git checkout -b my-master
git push --set-upstream origin my-master

Updating from upstream:

git checkout upstream-master
git pull
git checkout -b master origin/master # first time only
git checkout master
git merge upstream-master
git checkout my-master
git merge upstream-master
git push --all

Ruin somebodys day (in C)

Sneak into his headers:

# define struct union
# define else

Cmake: print all variables

get_cmake_property(_variableNames VARIABLES)
foreach (_variableName ${_variableNames})
    message(STATUS "${_variableName}=${${_variableName}}")
endforeach()

Pull requests

  • as small as possible
  • squash-merge only
  • first submit - first merge
  • never merge a PR manually on your own machine
  • only the person who submitted may merge

Documentation

  • cardinality
  • input/output examples
  • run time, O(…)
  • data storage, life cycle
  • blocking, async
  • naming
  • consistent & idiot-proof API