Skip to content

Instantly share code, notes, and snippets.

View simonthompson99's full-sized avatar

Simon Thompson simonthompson99

View GitHub Profile
@simonthompson99
simonthompson99 / temp-zip-file.py
Last active December 5, 2024 05:34
[Zip to temporary file] Make a temporary zip file and write to it #python #file_operations
import tempfile
import zipfile
# make archive.zip in temp directory
tmpdir = tempfile.mkdtemp()
zip_fn = os.path.join(tmpdir, 'archive.zip')
zip_obj = zipfile.ZipFile(zip_fn, 'w')
for f in files:
zip_obj.write(f, os.path.basename(f)) # add file to archive, second argument is the structure to be represented in zip archive, i.e. this just makes flat strucutre
@simonthompson99
simonthompson99 / common-sqlalchemy-queries.py
Last active January 15, 2021 12:05
[SQLAlchemy Query Cheatsheet] Common SQLAlchemy constructs for querying #python #sqlalchemy #cheatsheet
# case when
from sqlalchemy import case
d = s.query(config_db.ClinicalIndication.clinical_indication_uid,
case([(config_db.ClinicalIndication.clinical_indication_code.startswith('M'), 'C'),
(config_db.ClinicalIndication.clinical_indication_code.startswith('R'), 'R')],
else_ = None).label('programme')).all()
# convert query to pandas df
import pandas as pd
pd.DataFrame(q.all(), columns = ['col1', 'col2'])
@simonthompson99
simonthompson99 / read_in_xlsx.r
Last active January 15, 2021 12:04
[Read in xlsx file] Read in first sheet of excel file into dataframe #excel #r
read_in_xlsx <- function(f) {
# read in an Excel file into a dataframe
require(openxlsx)
d <- read.xlsx(f)
cat(paste0(f, " - ", nrow(d), "Rx", ncol(d), "C\n"))
return(d)
}
@simonthompson99
simonthompson99 / psql-connections.sql
Last active January 15, 2021 12:04
[psql Connection strings] Connection strings for psql #sql #database #genomics_england
-- Index from query
psql metrics -h 10.1.24.39 -p 5433 -U cdt_user
-- Index from local
psql metrics -h localhost -p 5441 -U cdt_user
-- local postgres
psql metrics -h localhost -p 5432 -U cdt_user
@simonthompson99
simonthompson99 / pandas-example-df.py
Last active January 15, 2021 11:48
[Example Pandas Dataframe] Test example dataframe for pandas #python #pandas
import pandas as pd
raw_data = {'first_name': ['Jason', 'Molly', 'Tina', 'Jake', 'Amy'],
'last_name': ['Miller', 'Jacobson', 'Ali', 'Milner', 'Cooze'],
'age': [42, 52, 36, 24, 73],
'preTestScore': [4, 24, 31, 2, 3],
'postTestScore': [25, 94, 57, 62, 70]}
df = pd.DataFrame(raw_data, columns = ['first_name', 'last_name', 'age', 'preTestScore', 'postTestScore'])
@simonthompson99
simonthompson99 / view-pandas-df.py
Last active January 15, 2021 11:46
[View Pandas Dataframe in Browser] View pandas df in a datatableesque browser window #python #html
from tempfile import NamedTemporaryFile
import webbrowser
base_html = """
<!doctype html>
<html><head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.css">
<script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.js"></script>
@simonthompson99
simonthompson99 / complex-merge.md
Last active January 15, 2021 11:44
[Complex Merge in git] Complex merging using mergetool #git #cheatsheet #workflow

Merging two very different branches

git checkout master to be in master

git merge <branch> to merge in the other branch, will tell you that can't auto-merge

git mergetool brings up a vim window with:

+--------------------------------+
@simonthompson99
simonthompson99 / pandas-methods.md
Last active April 3, 2021 18:04
[Pandas Dataframe Methods] Pandas dataframe methods #python #cheatsheet #pandas
FUNCTION DESCRIPTION
index() Method returns index (row labels) of the DataFrame
insert() Method inserts a column into a DataFrame
add() Method returns addition of dataframe and other, element-wise (binary operator add)
sub() Method returns subtraction of dataframe and other, element-wise (binary operator sub)
mul() Method returns multiplication of dataframe and other, element-wise (binary operator mul)
div() Method returns floating division of dataframe and other, element-wise (binary operator truediv)
unique() Method extracts the unique values in the dataframe
nunique() Method returns count of the unique values in the dataframe
@simonthompson99
simonthompson99 / git-stash-commands.md
Last active January 15, 2021 11:42
[git stash commands] Commands for using git stash #git #cheatsheet #workflow

View stashes and differences

  • git stash list see what is in the stash list
  • git stash show -p see full diff of the stash (add specific stash, otherwise will be most recent)

Stash stuff

  • git stash stash all changes to tracked files and revert to HEAD, add -m <message> to give an accompany description, and can specify some filepaths to limit the stash to
  • git stash -p go through hunk by hunk and decided what to stash
@simonthompson99
simonthompson99 / include-table-of-values-in-query.sql
Last active January 15, 2021 11:41
[Include table of values in query] Specify table of values in query #sql #database
SELECT * FROM (VALUES (1, 'one'), (2, 'two'), (3, 'three')) AS t (num,letter);