Skip to content

Instantly share code, notes, and snippets.

View oscarychen's full-sized avatar

Oscar Y Chen oscarychen

  • Calgary
View GitHub Profile
@oscarychen
oscarychen / gitbash_on_windows.md
Last active November 2, 2021 21:45
Gitbash for Python on Windows

Setting up Gitbash, Terminal on Windows

Using Python from Anaconda:

  • Download and install Anaconda. Highly recommended to install Anaconda for current user instead of all users.
  • Create or modify your .bashrc file, at a location like:
C:\Users\oscar\.bashrc
  • Add a line that points to the path of Anaconda script, like the following:
@oscarychen
oscarychen / python_on_mac_m1.md
Last active December 8, 2022 00:01
Setting up Python on Apple M1 (July 2021)

Setting up Python, XGBoost, Scipy on M1 Mac (July 2021)

As of July 2021, installing Python Xgboost on the M1 Macs is still confusing. The Python Xgboost library relies on Scipy, which requires a Fortran compiler, which is not available because GCC is not yet available for M1 (while Scipy does not support Flang). You may succeed in installing Python xgboost library without doing what I have described below and yet encounter issue running the library. I find the easiest thing currently is to just run Terminal with Rosetta, and go from there...

Get Terminal to run in Rosetta emulated X86 mode

  • Navigate to the Terminal app in Applications folder (sometimes it might be in Applications/Utilities)
  • Right click on the Terminal app -> Get Info -> Check Open using Rosetta
  • Optionally, you can duplicate the Terminal app, and rename the new one to something different, such as Terminal Rosetta and only make the duplicated app run in Rosetta mode

Install brew:

@oscarychen
oscarychen / ssh_for_git.md
Last active December 16, 2021 19:16
Setting up SSH for git

Setting up SSH for Git

General set-up for Git/Github account

Generally from a development machine, you would want to set up a ssh key that allows you to access your entire Github account, and access all of the repos under that account, here is how you can do that.

Generate a ssh key to be used from your machine to access your entire Github account: ssh-keygen -t rsa -b 4096 -C "your_email@example.com" When prompted "Enter file in which to save the key", you can specify something like: /Users/<username>/.ssh/github This should result in a private and public key pair being created:

@oscarychen
oscarychen / conda_environments_jupyter_notebook.md
Last active February 15, 2022 17:22
Using Conda Environments with Jupiter Notebook

Using Conda environments in Jupyter Notebook

Create a Conda environment conda create -n py38 python=3.8

Activate the environment conda activate py38

Install ipykernel pip install --user ipykernel

@oscarychen
oscarychen / remove_git_history.md
Last active September 30, 2021 17:25
Remove files from git commit history

Removing files from Git commit history

Clone a fresh copy of the repository using ssh and --mirror flag git clone --mirror git://example.com/some-big-repo.git

Download the bfg.jar file from BFG Repo-cleaner

Run BFG on the repo: java -jar bfg.jar --delete-files "*.csv" some-big-repo.git

@oscarychen
oscarychen / Django_custom_query.md
Last active December 6, 2021 19:56
Django custom lookup 'startswith' on model field

Django Custom Lookups

This article is based on my Stackoverflow answer - Django query lookup 'startswith' on table fields.

Let's say you have a table of partial postal codes:

| codes  |
----------
| A1A |
@oscarychen
oscarychen / Django_hierarchical_tree_data.md
Last active April 22, 2024 05:50
Storing and retrieving tree data structure efficiently in Django - the materialized path approach

Working with hierarchical / tree data structure in PostgresSQL and Django

There are two typical models for dealing with tree data structure in SQL databases: adjacency list model and nested set model. There are several packages for Django that implements these models such as django-mptt, django-tree-queries, and django-treebeard.

In this article, I want to explain a different approach I took in a project for storing hierachichal data, as well as search and retrieval of sub-trees. This approach is similar in principle to materialzed path, which django-treebeard package also provides an implementation. This article will show you how I achieved this without any of the specific packages, and using on

@oscarychen
oscarychen / cookies_same_origin_policy.md
Last active May 15, 2024 11:42
Cookies and Same Origin Policy explained

Cookies and Same Origin Policy

Origin

origin

_Origin_ is defined as the protocol-host-port tuple

Same Origin Policy

Ensures host document can only be accessed by JavaScript execution context from the same origin.

@oscarychen
oscarychen / csrf.md
Last active December 9, 2021 19:22
cross site request forgery explained

Cross Site Request Forgery (CSRF)

Session Hijacking

Cookie sent over unencrypted HTTP connection

Mitigation

Use Secure attribute on cookie to prevent it from being sent over unencrypted connection: Set-Cookie: key=value; Secure

@oscarychen
oscarychen / xss.md
Last active December 9, 2021 19:27
cross-site scripting explained

Cross-site scripting (XSS)

What is XSS?

  • Unexpected JavaScript code running in an HTML document
  • Unexpected code in SQL query
  • Any code that combines a command with user data is susceptible

Attacker may: