Skip to content

Instantly share code, notes, and snippets.

View pawelrubin's full-sized avatar
🐍

Paweł Rubin pawelrubin

🐍
View GitHub Profile
@pawelrubin
pawelrubin / git rr
Created August 23, 2022 08:55
Reset current local branch to its remote state
[alias] rr = !git branch --show-current | xargs -I{} git reset --hard origin/{}
@pawelrubin
pawelrubin / global_gitignore.md
Created May 6, 2022 09:45
Global .gitignore in VS Code

How to setup global .gitignore file in VS Code

  1. Create a file that will be your global .gitignore, for example ~/.gitignore;
  2. Set core.excludesFile in git config
git config --global core.excludesFile ~/.gitignore
  1. Add the settings below to the global settings file
{
@pawelrubin
pawelrubin / max_length.py
Last active April 7, 2021 11:59
Find the longest length of a field in a Django model.
from django.db.models import Model, Q
from django.db.models.functions import Length
def max_length(field: str, model: Model) -> int:
return getattr(
model.objects.filter(~Q(**{field: None}))
.annotate(**{f"{field}_length": Length(field)})
.order_by(f"{field}_length")
.last(),
@pawelrubin
pawelrubin / either.py
Created April 5, 2021 20:16
Either type in Python
from typing import Generic, TypeVar
VT = TypeVar("VT")
ET = TypeVar("ET")
class Matchable:
__match_args__ = ("__match_self_prop__",)
@property
@pawelrubin
pawelrubin / eratosthenes.py
Last active March 21, 2021 14:38
Sieve of Eratosthenes Python implementation - recursive approach using iterators
from typing import Iterator
def sieve(n: int) -> Iterator[int]:
def _sieve(xs: Iterator[int]) -> Iterator[int]:
if head := next(xs, None):
yield head
yield from _sieve(x for x in xs if x % head != 0)
@pawelrubin
pawelrubin / eratosthenes.py
Last active March 21, 2021 14:35
Sieve of Eratosthenes Python implementation
from typing import Iterator
def sieve(n: int) -> Iterator[int]:
if n < 2:
return
primes = [True for _ in range(n + 1)]
for p in range(2, n + 1):
@pawelrubin
pawelrubin / diagonalDifference.hs
Last active December 22, 2020 18:17
Calculate the absolute difference between the sums of a square matrix diagonals, in Haskell.
diagonalDifference :: [[Int]] -> Int
diagonalDifference arr = abs . sum $ differences
where
differences = map (\(i, row) -> (row !! i) - (row !! (len - 1 - i))) (enumerate arr)
len = length arr
enumerate = zip [0 ..]
@pawelrubin
pawelrubin / comparable.py
Last active December 10, 2020 11:57
Protocol for annotating comparable types.
from __future__ import annotations
from abc import abstractmethod
from typing import TypeVar, Protocol
class Comparable(Protocol):
"""Protocol for annotating comparable types."""
@abstractmethod
import setuptools
setuptools.setup(
name="example-pkg",
version="0.0.1",
author="Example Author",
author_email="author@example.com",
description="A small example package",
packages=setuptools.find_packages(),
[[package]]
name = "certifi"
version = "2020.12.5"
description = "Python package for providing Mozilla's CA Bundle."
category = "main"
optional = false
python-versions = "*"
[[package]]
name = "chardet"