Skip to content

Instantly share code, notes, and snippets.

@csyhuang
Last active October 24, 2018 14:57
Show Gist options
  • Save csyhuang/7af16f98a2bad9ca62d04e66c5913b4f to your computer and use it in GitHub Desktop.
Save csyhuang/7af16f98a2bad9ca62d04e66c5913b4f to your computer and use it in GitHub Desktop.
Some useful basic bash command for work

Kill all background jobs

kill $(jobs -p)

Run unit-testing and time all procedures

pytest --durations=0

Profiling of unittests

$ python -m cProfile -o profile $(which py.test)

$ python -m cProfile -o profile $(which py.test) tests/script.py::particular_module

Afterwards, in python

import pstats
import sys

with open('readable_format.txt', 'w') as stream:
    stats = pstats.Stats('profile', stream=stream)
    stats.print_stats()

Test only a particular module (with printouts)

pytest --capture=sys script.py::particular_module

Reference from a post on stackOverflow

Pull changes from another branch and sync with my own

Get the base branch:

$ git checkout v1

Pull in any changes to make sure you have the latest version

git pull

Check out your branch

git checkout v1_adminui

Rebase your changes on top of the v1 changes

git rebase v1

Optionally push your rebased branch

git push origin v1_adminui

Reference from a post on StackOverflow

Undo the last commit (while keeping the changes with --soft)

$ git reset --soft HEAD~1

Timing parts of a program

import logging
import time

time_array,chunk_description = list(), list()

time_array.append(time.time())
...
time_array.append(time.time())
chunk_description.append('DimMpMkUser')
...
for i in range(len(chunk_description)):
    if time_array[i+1]-time_array[i]>1.0:
        logging.warning(chunk_description[i])
        logging.warning('Time needed: {}'
            .format(time_array[i+1]-time_array[i]))

Use pytest-profile to profile a single test

Install via

$ pip install pytest-profiling

and then run by

$ py.test tests/script.py::particular_module --profile

Create Symlink in Linux

On terminal

ln -s /folderorfile/link/will/point/to /name/of/the/link
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment