This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def send_file_to_slack(fp, channel, fn = None): | |
| """ | |
| Send a file to a slack channel | |
| - fp - path to file | |
| - channel - channel to send to | |
| - fn - the filename | |
| """ | |
| import requests | |
| import os |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- update a table using a standard query | |
| with t as ( | |
| select <the primary key of the table> as rowid, <value to use to update> as update_value | |
| from <table> | |
| left join .... | |
| where .... | |
| ) | |
| update <table to update> | |
| set <field to update> = t.update_value | |
| from t |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| run_query <- function(conn, f) { | |
| # run the contents of sql file - f- on a given db connection - conn | |
| require(DBI) | |
| q <- trimws(readLines(f)) | |
| # remove lines starting with -- | |
| q <- gsub("^--.*$", "", q) | |
| d <- dbGetQuery(conn, paste(q, collapse = " ")) | |
| cat(paste0(f, ' - ', nrow(d), 'Rx', ncol(d), 'C\n')) | |
| return(d) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import hashlib | |
| def md5(fname): | |
| """ | |
| returns the md5 for a filepath | |
| """ | |
| BLOCKSIZE = 65536 | |
| hasher = hashlib.md5() | |
| with open(fname, 'rb') as afile: | |
| buf = afile.read(BLOCKSIZE) | |
| while len(buf) > 0: |