Skip to content

Instantly share code, notes, and snippets.

View Per48edjes's full-sized avatar
👉
This is a good point.

Ravi Dayabhai Per48edjes

👉
This is a good point.
View GitHub Profile
@Per48edjes
Per48edjes / git_gym__Lesson_2.md
Last active February 27, 2023 19:40
[Git Gym] Lesson 2: The 'Undo'

Lesson 2: The 'Undo'

Git doesn't have a CTRL Z equivalent -- it does have a souped up set of commands that give you finer grained control of how you want to backtrack.

Git: reset vs. checkout vs. restore vs. switch vs. revert

It's certainly understandable if distinguishing these commands is just out of reach or requires a few Google/Stackoverflow searches each time you reach for

@Per48edjes
Per48edjes / git_gym__Lesson_1.md
Created September 1, 2021 21:48
[Git Gym] Lesson 1: The Basics

Lesson 1: The Basics

xkcd git

The goal for this lesson is to bolster your confidence using git. We won't attempt a comprehensive review, but we will try to establish a more holistic understanding of what it is, what it isn't, and how you can begin to make it work for you.

This is not:

@Per48edjes
Per48edjes / game.py
Created May 26, 2021 19:48
Python OOP implementation of Conway's Game of Life
import os
from itertools import product
from random import randint
from time import sleep
class Cell:
def __init__(self, state):
self._state = state
self._future_state = state
@Per48edjes
Per48edjes / longest_runs.py
Last active May 1, 2021 19:39
Counting longest runs of {0, 1} in binary NumPy array
## Source: https://stackoverflow.com/questions/24342047/count-consecutive-occurences-of-values-varying-in-length-in-a-numpy-array/24343375#24343375
def longest_runs_sim(n: int = 50) -> int:
# Random sequence where each n_i is element of {0, 1}
seq = np.random.randint(0, 2, n)
longest_runs = {1: None, 0: None}
for i, outcome in enumerate([~seq, seq]):
runs = \
np.diff( # Diff of indices counts runs
@Per48edjes
Per48edjes / pivot_cases.sql
Created April 10, 2021 22:10
Pivot entries in MySQL using variables and CASE statements
-- Set counter variables
set @r1=0, @r2=0, @r3=0, @r4=0;
with
cte_pivot as (
select
case
when Occupation='Doctor' then (@r1:=@r1+1)
when Occupation='Professor' then (@r2:=@r2+1)
@Per48edjes
Per48edjes / disaggregate_count.sql
Created April 6, 2021 03:53
Disaggregate a COUNT / "expand" a frequencies of a value
with recursive
cte_expand as (
select Number, Frequency as i from histogram
union all
select Number, i-1 as i from cte_expand where i > 1
)
select Number from cte_expand
order by 1
@Per48edjes
Per48edjes / lint_json.sh
Created December 27, 2020 02:30 — forked from nstielau/lint_json.sh
A one-liner for validating json
# Lint JSON with python (the exit 255 stops xargs after the first failed command)
find . -name "*.json" -print | xargs -Ixx bash -c "echo JSON linting xx 1>&2; cat xx | python -mjson.tool > /dev/null || exit 255"
@Per48edjes
Per48edjes / git-pushing-multiple.rst
Created November 13, 2020 17:00 — forked from rvl/git-pushing-multiple.rst
How to push to multiple git remotes at once. Useful if you keep mirrors of your repo.

Pushing to Multiple Git Repos

If a project has to have multiple git repos (e.g. Bitbucket and Github) then it's better that they remain in sync.

Usually this would involve pushing each branch to each repo in turn, but actually Git allows pushing to multiple repos in one go.

If in doubt about what git is doing when you run these commands, just

@Per48edjes
Per48edjes / df_side_by_side.py
Created September 30, 2020 19:03
Display dataframes in a row with captions
from IPython.core.display import display, HTML
def display_side_by_side(dfs:list, captions:list):
"""Display tables side by side to save vertical space
Input:
dfs: list of pandas.DataFrame
captions: list of table captions
"""
output = ""
combined = dict(zip(captions, dfs))
@Per48edjes
Per48edjes / substring_list_match.py
Created July 2, 2020 11:52
Multiple substring search among list of strings
def substring_list_match(string_list, substr_list):
return [target_str for target_str in string_list if any(search_str in target_str for search_str in substr_list)]