markeyser / cookiecutter-collabora

A Cookiecutter template for collaborative and reproducible AI projects
https://markeyser.github.io/cookiecutter-collabora/
Other
1 stars 0 forks source link

Use the sidebar-markdown-notes for VS Code to include sheet cheats #49

Open markeyser opened 2 months ago

markeyser commented 2 months ago

This is the extension:

https://marketplace.visualstudio.com/items?itemName=assisrMatheus.sidebar-markdown-notes

This a good replacement of the Makefile. Let's use the Make file to really automate stuff...

Bellow the cheat sheets I have so far:

Git Commands Cheat Sheet

Initialization and Configuration

Initialize a new Git repository:

git init

Clone an existing repository:

git clone <repository-url>

Set global username:

git config --global user.name "Your Name"

Set global email:

git config --global user.email "your.email@example.com"

View configured username and email:

git config --global --list

Status and Information

Check the status of the working directory and staging area:

git status

✨Show the changes in the working directory:

git diff

Show the changes between the staging area and the latest commit:

git diff --staged

Show the commit history:

git log

Show a single-line log with graph:

git log --oneline --graph --all

Show detailed information about a specific commit:

git show <commit-id>

Staging and Committing

Add a specific file to the staging area:

git add <file>

Add all changes in the current directory to the staging area:

git add .

Commit changes with a message:

git commit -m "Your commit message"

Commit with a multi-line message:

git commit

Amend the last commit (don't do this if you've already pushed the commit):

git commit --amend -m "Updated commit message"

Branching and Merging

Create a new branch:

git branch <new-branch-name>

✨List all branches:

git branch

Switch to a different branch:

git checkout <branch-name>

✨Create a new branch and switch to it:

git checkout -b <new-branch-name>

Merge a branch into the current branch:

git merge <branch-name>

✨Delete a branch locally:

git branch -d <branch-name>

✨Delete a branch remotely:

git push origin --delete <branch-name>

Remote Repositories

Add a remote repository:

git remote add origin <repository-url>

List all remote repositories:

git remote -v

Remove a remote repository:

git remote remove <name>

✨Fetch changes from a remote repository:

git fetch

✨Pull changes from a remote repository and merge:

git pull

✨Push changes to a remote repository:

git push origin <branch-name>

Force push changes to a remote repository (use with caution):

git push origin <branch-name> --force

Stashing Changes

Stash current changes:

git stash

List stashed changes:

git stash list

Apply the latest stash:

git stash apply

Apply a specific stash:

git stash apply stash@{n}

Drop the latest stash:

git stash drop

Drop a specific stash:

git stash drop stash@{n}

Apply and drop the latest stash:

git stash pop

Rebasing

Start an interactive rebase:

git rebase -i HEAD~n  # n is the number of commits to rebase

Continue a rebase after resolving conflicts:

git rebase --continue

Skip a commit during a rebase:

git rebase --skip

Abort a rebase:

git rebase --abort

Resetting and Reverting

Unstage changes:

git reset <file>

Unstage all changes:

git reset

Reset the working directory to the last commit:

git reset --hard

Reset to a specific commit (keeps changes in working directory):

git reset <commit-id>

Hard reset to a specific commit (discards all changes):

git reset --hard <commit-id>

Revert a specific commit:

git revert <commit-id>

Tagging

Create a new tag:

git tag <tag-name>

Create an annotated tag:

git tag -a <tag-name> -m "Tag message"

List all tags:

git tag

Push a specific tag to a remote repository:

git push origin <tag-name>

Push all tags to a remote repository:

git push origin --tags

Delete a local tag:

git tag -d <tag-name>

Delete a remote tag:

git push origin --delete <tag-name>

Git Aliases

Add these to your .gitconfig to create shortcuts for commonly used commands:

[alias]
    st = status
    co = checkout
    ci = commit
    br = branch
    lg = log --oneline --graph --all
    amend = commit --amend

Advanced Commands

Cherry-pick a commit from another branch:

git cherry-pick <commit-id>

Squash multiple commits into one:

git rebase -i HEAD~n  # Mark commits to be squashed

Clean untracked files and directories:

git clean -f -d

Check out a file from another branch without switching branches:

git checkout <branch-name> -- <file-path>

Cheat sheet for Poetry commands

Installation and Configuration

Install Poetry:

curl -sSL https://install.python-poetry.org | python3 -

Configure Poetry to create virtual environments inside the project directory:

poetry config virtualenvs.in-project true

View current Poetry configuration:

poetry config --list

Project Initialization and Management

Create a new Poetry project:

poetry new <project-name>

Initialize Poetry in an existing project:

poetry init

Install dependencies listed in pyproject.toml:

poetry install

Add a new dependency to the project:

poetry add <package-name>

Add a specific version of a dependency:

poetry add <package-name>@<version>

Add a development dependency:

poetry add --dev <package-name>

Remove a dependency from the project:

poetry remove <package-name>

Update all dependencies to the latest version:

poetry update

Update a specific dependency:

poetry update <package-name>

Virtual Environments

Create a new virtual environment:

poetry env use <python-version>

List all virtual environments managed by Poetry:

poetry env list

Remove a specific virtual environment:

poetry env remove <virtualenv-name>

Dependency Management

Show all installed dependencies:

poetry show

Show details about a specific dependency:

poetry show <package-name>

Generate a requirements.txt file from pyproject.toml:

poetry export -f requirements.txt --output requirements.txt

Lock dependencies (generate/update poetry.lock file):

poetry lock

Running and Building

Run a script or command in the context of the project's virtual environment:

poetry run <command>

Open a shell within the project's virtual environment:

poetry shell

Build the project (create source and wheel distributions):

poetry build

Publish the package to a repository (e.g., PyPI):

poetry publish

Publish to a repository with credentials:

poetry publish --username <username> --password <password>

Scripts and Plugins

Run a custom script defined in pyproject.toml:

poetry run <script-name>

Install a Poetry plugin:

poetry self add <plugin-name>

List installed Poetry plugins:

poetry self show plugins

Configuration and Settings

Set a global Poetry configuration value:

poetry config <key> <value> --global

Unset a global Poetry configuration value:

poetry config --unset <key> --global

List all configuration values:

poetry config --list

Miscellaneous

Check for Poetry updates:

poetry self update

Get the current Poetry version:

poetry --version

Cheat sheet for Conda commands

Installation and Configuration

Install Conda (via Miniconda or Anaconda):

# Install Miniconda (recommended for minimal installation)
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

# Install Anaconda (includes many scientific packages by default)
curl -O https://repo.anaconda.com/archive/Anaconda3-latest-Linux-x86_64.sh
bash Anaconda3-latest-Linux-x86_64.sh

Update Conda to the latest version:

conda update conda

Environment Management

Create a new environment:

conda create --name <env-name>

Create a new environment with specific Python version:

conda create --name <env-name> python=<version>

Create a new environment from an environment.yml file:

conda env create -f environment.yml

Activate an environment:

conda activate <env-name>

Deactivate the current environment:

conda deactivate

List all environments:

conda env list

Export an environment to an environment.yml file:

conda env export > environment.yml

Remove an environment:

conda env remove --name <env-name>

Package Management

Install a package:

conda install <package-name>

Install a specific version of a package:

conda install <package-name>=<version>

Install packages from a specific channel:

conda install -c <channel-name> <package-name>

Update all packages in the current environment:

conda update --all

Update a specific package:

conda update <package-name>

Remove a package:

conda remove <package-name>

List all installed packages in the current environment:

conda list

Searching for Packages

Search for a package in the default or specific channel:

conda search <package-name>

Conda Configuration

Add a new channel to the Conda configuration:

conda config --add channels <channel-name>

Set the default environment path:

conda config --set envs_dirs <path>

List all Conda configuration settings:

conda config --show

Miscellaneous

Check for Conda updates:

conda update conda

Get the current Conda version:

conda --version

Clean up unused packages and caches:

conda clean --all

Verify the integrity of the Conda environment:

conda check

List package revisions in the current environment:

conda list --revisions

Restore the environment to a previous revision:

conda install --revision <revision-number>

Environment Cloning

Clone an existing environment:

conda create --name <new-env-name> --clone <existing-env-name>

Cheat sheet for Pytest

Installation

Install Pytest:

pip install pytest

Verify the installation:

pytest --version

Running Tests

Run all tests in the current directory and subdirectories:

pytest

Run tests in a specific file:

pytest <test_file.py>

Run a specific test function within a file:

pytest <test_file.py>::<test_function>

Run tests with detailed output:

pytest -v

Test Discovery

Automatically discover and run all tests:

pytest

Specify a custom test discovery pattern:

pytest --maxfail=3

Markers and Fixtures

Run tests with a specific marker:

pytest -m <marker_name>

Run tests that do not have a specific marker:

pytest -m "not <marker_name>"

Fixtures

Define a fixture in conftest.py or a test module:

import pytest

@pytest.fixture
def my_fixture():
    return "fixture data"

Use a fixture in a test:

def test_with_fixture(my_fixture):
    assert my_fixture == "fixture data"

Parametrized Tests

Parametrize a test function:

@pytest.mark.parametrize("input,expected", [(1, 2), (2, 3), (3, 4)])
def test_increment(input, expected):
    assert input + 1 == expected

Assertions

Use standard Python assertions:

def test_example():
    assert 1 + 1 == 2
    assert "pytest" in "pytest is awesome"

XFail and Skip

Mark a test as expected to fail:

@pytest.mark.xfail
def test_will_fail():
    assert 0

Skip a test:

@pytest.mark.skip(reason="Not implemented yet")
def test_not_implemented():
    pass

Capturing Output

Capture and test stdout/stderr output:

def test_output(capsys):
    print("hello")
    captured = capsys.readouterr()
    assert captured.out == "hello\n"

Configuration

Create a pytest.ini or tox.ini file for configuration:

[pytest]
addopts = -v
testpaths = tests

Plugins

List all installed plugins:

pytest --trace-config

Install a Pytest plugin:

pip install pytest-cov

Use a Pytest plugin (e.g., coverage):

pytest --cov=<package_name>

Coverage

Install the coverage plugin:

pip install pytest-cov

Run tests with coverage:

pytest --cov=<source_directory>

Generate a coverage report:

pytest --cov=<source_directory> --cov-report=html

Command Line Options

Run tests with a specific configuration file:

pytest -c <config_file>

Stop after the first failure:

pytest -x

Show the durations of the slowest tests:

pytest --durations=10

Debugging

Enter the Python debugger on test failure:

pytest --pdb

Run tests with the debugger:

pytest --trace

Logging

Capture log messages:

import logging

def test_logging(caplog):
    logging.warning("This is a warning")
    assert "This is a warning" in caplog.text

Test Naming and Structuring

Name test files starting with test_ or ending with _test.py. Name test functions starting with test_.

Advanced Usage

Use multiple test directories:

pytest dir1 dir2

Run tests in parallel (requires pytest-xdist):

pip install pytest-xdist
pytest -n 4

Ignoring and Selecting Tests

Ignore specific files or directories:

pytest --ignore=<path>

Select specific files or directories:

pytest <path>

Cheat sheet for Ruff (Lint)

Installation

Install Ruff:

pip install ruff

Verify the installation:

ruff --version

Basic Usage

Lint all Python files in the current directory and subdirectories:

ruff .

Lint a specific file or directory:

ruff <path>

Lint and autofix issues:

ruff --fix <path>

Show detailed output (including codes and explanations):

ruff --show-source <path>

Configuration

Create a pyproject.toml file for configuration:

[tool.ruff]
line-length = 88
select = ["E", "W", "F"]
ignore = ["E501", "W503"]

Specify a configuration file:

ruff --config <config_file> <path>

Ignoring Rules

Ignore specific rules in a configuration file:

[tool.ruff]
ignore = ["E501", "W503"]

Ignore specific rules in a file using comments:

# ruff: noqa: E501
def my_function():
    pass

Ignore a specific line:

def my_function():  # noqa: E501
    pass

Autofixing Issues

Autofix issues in the code:

ruff --fix <path>

Autofix specific rules:

ruff --fix --select <rule> <path>

Advanced Usage

Show statistics of linting errors:

ruff --statistics <path>

Show a summary of linting errors:

ruff --summary <path>

Integration with Other Tools

Integrate Ruff with pre-commit: Create a .pre-commit-config.yaml file:

repos:
  - repo: https://github.com/charliermarsh/ruff-pre-commit
    rev: v0.0.252
    hooks:
      - id: ruff

Install pre-commit hooks:

pre-commit install

Selective Linting

Lint only specific rules:

ruff --select E9,F63,F7,F82 <path>

Exclude specific rules from linting:

ruff --ignore F401 <path>

Excluding Files and Directories

Exclude specific files or directories:

ruff --exclude <path>

Example with multiple exclusions:

ruff --exclude .git,__pycache__,build,dist <path>

Output Options

Show only errors and warnings:

ruff --quiet <path>

Show verbose output:

ruff --verbose <path>

Caching

Enable caching to speed up linting:

ruff --cache-dir <cache_directory> <path>

Disable caching:

ruff --no-cache <path>

Running Specific Checks

Run specific checks by specifying their codes:

ruff --select E101,E111 <path>

Run all checks except specified ones:

ruff --ignore E101,E111 <path>

Ignoring Files in Configuration

Ignore files and directories in the configuration file:

[tool.ruff]
exclude = ["build", "dist", ".git"]

Checking for Updates

Check for updates to Ruff:

pip list --outdated | grep ruff

Miscellaneous

Show help and available options:

ruff --help

Show Ruff version:

ruff --version

Pre-commit hooks

Installation

Install pre-commit:

pip install pre-commit

Verify the installation:

pre-commit --version

Basic Usage

Initialize a pre-commit configuration in your repository:

pre-commit init

Create a .pre-commit-config.yaml file:

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.0.1
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml

Install pre-commit hooks:

pre-commit install

Run all hooks on all files:

pre-commit run --all-files

Run hooks on changed files:

pre-commit run

Run specific hooks:

pre-commit run <hook-id>

Managing Hooks

Add a hook to .pre-commit-config.yaml:

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.0.1
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml

  - repo: https://github.com/charliermarsh/ruff-pre-commit
    rev: v0.0.252
    hooks:
      - id: ruff

Remove a hook by deleting its entry from .pre-commit-config.yaml.

Update hooks to their latest versions:

pre-commit autoupdate

Configuration Options

Skip specific hooks during a run:

SKIP=<hook-id> pre-commit run

Temporarily disable pre-commit:

pre-commit uninstall

Re-enable pre-commit:

pre-commit install

Advanced Usage

Manually install hooks:

pre-commit install-hooks

Show installed hooks:

pre-commit run --all-files --show-diff-on-failure

Run hooks on a specific file:

pre-commit run --files <file>

Run hooks on a range of commits:

pre-commit run --from-ref <start_ref> --to-ref <end_ref>

Ignoring Files

Ignore files in a .pre-commit-ignore file:

# Ignore all Markdown files
*.md

# Ignore specific file
path/to/specific_file.py

Custom Hooks

Define a custom hook in .pre-commit-config.yaml:

repos:
  - repo: local
    hooks:
      - id: custom-hook
        name: Custom Hook
        entry: ./path/to/script.sh
        language: script
        files: \.py$

Integrations

Integrate pre-commit with CI/CD: Add the following to your CI configuration to run pre-commit hooks:

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: '3.x'
      - name: Install dependencies
        run: pip install pre-commit
      - name: Run pre-commit
        run: pre-commit run --all-files

Frequently Used Hooks

Some common hooks you might find useful:

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.0.1
    hooks:
      - id: trailing-whitespace
      - id: end-of-file-fixer
      - id: check-yaml
      - id: check-json
      - id: check-added-large-files
      - id: check-merge-conflict

  - repo: https://github.com/psf/black
    rev: 21.12b0
    hooks:
      - id: black

  - repo: https://github.com/PyCQA/flake8
    rev: 3.9.2
    hooks:
      - id: flake8

  - repo: https://github.com/charliermarsh/ruff-pre-commit
    rev: v0.0.252
    hooks:
      - id: ruff

Miscellaneous

Check the status of pre-commit hooks:

pre-commit status

Manually clean up pre-commit caches:

pre-commit clean

Black

Installation

Install Black:

pip install black

Verify the installation:

black --version

Basic Usage

Format all Python files in the current directory and subdirectories:

black .

Format a specific file or directory:

black <path>

Configuration

Create a pyproject.toml file for configuration:

[tool.black]
line-length = 88
target-version = ['py37', 'py38']
include = '\.pyi?$'
exclude = '''
/(
    \.eggs|
    \.git|
    \.hg|
    \.mypy_cache|
    \.nox|
    \.tox|
    \.venv|
    _build|
    buck-out|
    build|
    dist
)/
'''

Command Line Options

Check if files would be reformatted without making changes:

black --check <path>

Show changes that would be made without making changes:

black --diff <path>

Force reformatting even if the file contains syntax errors:

black --fast <path>

Set the line length for code formatting:

black --line-length 120 <path>

Set the target Python versions for compatibility:

black --target-version py37 --target-version py38 <path>

Exclude specific files or directories:

black --exclude <pattern> <path>

Integration with Other Tools

Integrate Black with pre-commit: Create a .pre-commit-config.yaml file:

repos:
  - repo: https://github.com/psf/black
    rev: 21.12b0
    hooks:
      - id: black
        language_version: python3.7

Install pre-commit hooks:

pre-commit install

Run pre-commit hooks:

pre-commit run --all-files

Using Black with Editors

VS Code

Install the Black formatter extension or add it to your settings.json:

"python.formatting.provider": "black",
"editor.formatOnSave": true

PyCharm

Advanced Usage

Run Black in verbose mode:

black --verbose <path>

Exclude specific cells in Jupyter notebooks from formatting:

# black: skip
def example():
    pass

Ignoring Files

Add a .gitignore-style file named .blackignore to exclude files or directories from being formatted by Black:

# Exclude all .pyc files
*.pyc

# Exclude a specific directory
path/to/dir/

Git Integration

Automatically run Black before each commit using a pre-commit hook:

echo '#!/bin/sh\nblack .' > .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

Additional Options

Show help and available options:

black --help

Performance Options

Limit the number of concurrent jobs:

black --workers 4 <path>

Skipping Formatting

Skip formatting of specific lines or blocks in your code:

# fmt: off
some_code_that_will_not_be_formatted()
# fmt: on
Divine-123810 commented 3 weeks ago

How can I push my code on GitHub repository