Skip to content

Instantly share code, notes, and snippets.

View simonthompson99's full-sized avatar

Simon Thompson simonthompson99

View GitHub Profile
@simonthompson99
simonthompson99 / create-indx-db.sql
Last active January 15, 2021 11:50
[Create new Indx Db] Create new database on index and grant priviliges to cdt_user, needs [login details from Confluence](https://cnfl.extge.co.uk/display/CDT/_Logins) #sql #database #workflow #genomics_england
/* use user:password for superuser on https://cnfl.extge.co.uk/display/CDT/_Logins */
psql metrics -h localhost -p 5441 -U postgres
create database <db_name>;
grant all privileges on database <db_name> to cdt_user;
@simonthompson99
simonthompson99 / extract-excel-cell-color.vba
Last active January 15, 2021 11:32
[Extract Excel cell color] Convert the fill color in excel to a numeric value #excel
Function GetFillColor(Rng As Range) As Long
GetFillColor = Rng.Interior.ColorIndex
End Function
# to use this:
# 1. open the visual basic editor (alt + F11 or fn + alt + f11)
# 2. paste the above code
# 3. save then quit and return to the workbook
@simonthompson99
simonthompson99 / concat-flags.r
Last active January 15, 2021 11:33
[Concatenate flag columns] Concatenate flag columns into a single column #r
# take a load of boolean flag columns (names beginning with f_) and produce a column that lists which flags the row is true for
concat_flags <- function(x){
separator = ';'
a <- paste0(names(x)[as.logical(x)], sep = separator)
ifelse(a == separator, NA, a)
}
data$concat_flags <- apply(data[,colnames(data)[startsWith(colnames(data), "f_")]], 1, concat_flags)
@simonthompson99
simonthompson99 / random-data-query.sql
Last active January 15, 2021 11:35
[Random data query] Generate random data query #sql #database
-- generates 5000000 rows of random data
select i
,now() - (random() * (interval '100 years'))
,(array['1', '2'])[floor(random() * 2 + 1)]
from pg_catalog.generate_series(100000001, 105000000, 1) s(i)
;
@simonthompson99
simonthompson99 / python-one-liners.py
Last active January 15, 2021 11:36
[Python One-liners] One liner python snippets #python #oneliner
# get listing of files in file tree with glob-like searching
files = [f for f in glob.glob(SOURCE_DIR + '/**/af*.xlsx', recursive=True)]
@simonthompson99
simonthompson99 / close-postgres-connections.sql
Last active January 15, 2021 11:39
[Close postgres connections] Terminate postgres connections to database #sql #database
select pg_terminate_backend(pg_stat_activity.pid)
from pg_stat_activity
where pg_stat_activity.datname = '<db_name>' -- ← change this to your db
and pid <> pg_backend_pid() and usename = '<user>'
;
@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);
@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 / 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 / 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:

+--------------------------------+