Skip to content

Instantly share code, notes, and snippets.

View simonthompson99's full-sized avatar

Simon Thompson simonthompson99

View GitHub Profile
@simonthompson99
simonthompson99 / create-new-db-on-local.sh
Last active January 15, 2021 12:35
[Create new db on local postgres] Adds a new database to local postgres installation #sql #database #genomics_england
psql postgres -U simonthompson
create database <db_name>;
grant all privileges on database <db_name> to simon;
grant all privileges on database <db_name> to cdt_user;
\q
@simonthompson99
simonthompson99 / pg-dates.sql
Last active January 15, 2021 12:34
[PostgreSQL date operations and formatting] Different commands to work with dates in PG #sql #oneliner
-- extract single part of date
extract(year from date) -- can be month, day, hour etc.
--e.g.
extract(hour from '1979-02-01 12:03:01')
-- returns 12
-- reduce date to unit of date, but return full date/timestamp
date_trunc('hour', date/timestamp) -- can be month, day hour, year etc.
--e.g.
date_trunc('hour', timestamp '1979-02-01 12:03:01')
@simonthompson99
simonthompson99 / list-jira-issues.r
Last active January 15, 2021 12:33
[List JIRA issues matching a query] List JIRA issues that match a jql statement #r #jira #api
list_jira_issues <- function(jql, u, p, base_url = 'https://jira.extge.co.uk', fields = c('key'), ignore = c('expand', 'self', 'id')){
# list issues on jira, returns a dataframe of results
# jql - jira query language string (copy from advanced search url)
# u, p - username and password
# base_url - url of service
# fields - vector of fields of interest
# ignore - vector of column names that can be removed from the output
list_jira_issues_pg <- function(jql, u, p, st, npp, base_url, fields){
# get a single page of search results
require(httr)
@simonthompson99
simonthompson99 / create-jira-ticket.r
Last active January 15, 2021 12:32
[Create JIRA ticket] Creates a JIRA ticket using given JSON #python #jira #api
create_jira_issue <- function(d, u, p, base_url = 'https://jira.extge.co.uk'){
# create a new jira issue
# d - list equating to the json required to create the ticket
# u, p - username and password to use
# base_url - url of the service to create ticket in
require(httr)
require(jsonlite)
r_url = paste0(base_url, '/rest/api/2/issue')
# make the request
r = POST(r_url, authenticate(u, p, type = 'basic'), body = d, encode = 'json')
@simonthompson99
simonthompson99 / vim-config.md
Last active January 15, 2021 12:32
[Vim configuration cheatsheet] Cheatsheet for Vim configuration, commands, snippets etc. #vim #environment #cheatsheet

Navigation

H: to go back in tabs L: to go forward in tabs \zz: to focus cursor in the middle of the screen \b: select buffer by number \e: open up a explorer Tree

neoterm

@simonthompson99
simonthompson99 / dl-cnfl-attachments.py
Last active June 2, 2022 19:44
[Download Confluence Attachment by upload date] Downloads the latest upload to a Confluence page #python #api #confluence
#!/usr/bin/env python3
import requests
import sys
import pandas as pd
import xlrd
import io
import os
import csv
u = <username>
@simonthompson99
simonthompson99 / send-df-to-slack.r
Last active January 15, 2021 12:30
[Send Dataframe to Slack] Send a dataframe to be rendered as table in Slack #r #slack
#-- use the CDT Bot API token see https://cnfl.extge.co.uk/display/CDT/Generic+Login+Credentials+Information#GenericLoginCredentialsInformation-SlackCDTBotUser
#-- function to send df (d) to a specific channel with optional message, will break up if there are too many rows
send_df_to_slack_as_msg <- function(d, channel, api_token, msg = NA){
require(knitr)
require(slackr)
slackr_setup(channel = channel,
api_token = api_token)
d <- kable(d, format = 'rst')
if(!is.na(msg)){
slackr_msg(msg)
@simonthompson99
simonthompson99 / read-template-file.py
Last active January 15, 2021 12:28
[Read template file] Read a text file and return it as a Template object #python
# Template objects allows you to add ${FIELD_NAME} tags into the file
# and then values can be substituted with
# template_object.substitute(FIELD_NAME = <field_value>)
def read_template(filename):
"""
Returns a Template object comprising the contents of the
file specified by filename.
"""
import Template
with open(filename, 'r', encoding='utf-8') as template_file:
@simonthompson99
simonthompson99 / send-email.py
Last active January 15, 2021 12:27
[Send Email Over SMTP] Sends email over smtp, works for qmul email, but not NHS #python #email
# set up the SMTP server
# for qmul:
# host = smtp-mail.outlook.com
# port = 587
# username, password = full hh..@qmul.ac.uk email address
import smtplib
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
@simonthompson99
simonthompson99 / imagemagick-one-liners.sh
Last active February 12, 2021 17:13
[Imagemagick One-liners] Onelines for Imagemagick for image manipulation #imagemagick #oneliner
# make white transparent
convert <in>.png -transparent white <out>.png
# change image to a4-sized pdf
convert -density 300 -page a4 <in>.png <out>.pdf
# remove large white backgrounds from multiple images
mogrify -trim +repage *.png
# make image grayscale