Skip to content

Instantly share code, notes, and snippets.

@christlc
christlc / README.md
Last active September 23, 2021 12:34
To use Front Matter and markdown in Nuxt JS
@christlc
christlc / extract_from_pdf.R
Last active April 17, 2018 13:13
Hong Kong Government budget extract from pdf - semi-auto solution
library(tabulizer)
#### PARAMETERS ####
target_str <- "An analysis of the financial provision under Subhead 000 Operational expenses is as follows"
filename <- "head156.pdf"
year <- 2012
####################
# R version of
#https://hackernoon.com/functional-computational-thinking-what-is-a-monad-2adea91154e
library(magrittr)
readLines("./file1") %>% readLines()
readFileCPS <- function(path, cb){
cb(readLines(path))
}
composeCPS <- function(g,f){
@christlc
christlc / token.R
Created December 10, 2017 08:26
Token cache closure
tokens_pool <- function(tokens){
current_count <- 1
list(get_next_token = function(){
current_count <<- current_count +1
if(current_count>length(tokens)){
current_count <<- 1
}
return(tokens[[current_count]])
},
get_current_token = function(){
@christlc
christlc / example_script_analysis_office.ps1
Created December 5, 2017 09:50
SAP Analysis Office Excel Auto refresh with powershell (without Excel Macro)
param(
[string]$baseDir = "C:\SomeFolderToStoreTheData\",
[string]$bw_client = "000",
[string]$bw_user = "YOURUSERNAME",
[string]$bw_password = "YOURPASSWORD",
[string]$filePath = "Path to Analysis Office Excel.xlsx",
[string]$year_column = "Analysis Technical Name of Year column"
)
# Essentially the Powershell version of https://blogs.sap.com/2016/12/18/automated-updating-of-data-in-excel-files-bex-ao-via-vbavbscript/
@christlc
christlc / task_simulation.py
Created November 1, 2017 00:13
A task simulation tool to illustrate load factor vs wait time in Simpy
"""
Adapted from https://simpy.readthedocs.io/en/latest/examples/bank_renege.html
Bank renege example
Covers:
- Resources: Resource
- Condition events
Scenario:
@christlc
christlc / solve_sudoku.R
Created December 26, 2016 15:58
R code for solving Sudoku
sudoku_board <- rbind(c(9, NA, 6,8,NA, NA, 3, NA, NA),
c(NA, NA, NA, NA, 7, NA, NA, NA, NA),
c(NA, NA, 1, NA, NA, NA, 6, NA, 2),
c(NA, NA, NA, NA, 6, NA, 1, NA, 5),
c(NA, 7, NA, NA, 5, NA, NA, 3, NA),
c(6, NA, 3, NA, 8, NA, NA, NA, NA),
c(4, NA, 2, NA, NA, NA, 5, NA, NA),
c(NA, NA, NA, NA, 1, NA, NA, NA, NA),
c(NA, NA, 5, NA, NA, 4, 9, NA, 1)
)
@christlc
christlc / latlong_to_hk1980.py
Created December 13, 2016 01:22
Convert Lat-Long to HK1980 grid points using pyproj
from pyproj import Proj, transform
# lat-long to HK coordinates
hk_proj = Proj(init='epsg:2326') # HK1980 projection
latlon_proj = Proj(init='epsg:4326') # lat lon projection
x1,y1 = transform(latlon_proj, hk_proj, float(lon), float(lat))
@christlc
christlc / Excel_Range_to_Pandas_Dataframe.py
Created November 7, 2016 04:49
Convert a sub-range of excel to pandas dataframe in python
import os
import pandas as pd
from openpyxl import load_workbook
wb = load_workbook(filename='raw_data/A02.xlsx',
data_only=True)
ws = wb['A02e']
@christlc
christlc / generate_date_hierarchy.py
Created October 28, 2016 07:51
Date Hierarchy for OLAP
import pandas as pd
import numpy as np
START_YEAR = 2007
END_YEAR = pd.datetime.today()
def main():
df = pd.DataFrame()
df['Day'] = pd.date_range(str(START_YEAR), str(END_YEAR))
df['Month'] = df.Day.map(lambda x: "%d-%02d" % (x.year, x.month))