Skip to content

Instantly share code, notes, and snippets.

View mpettis's full-sized avatar

Matt Pettis mpettis

  • Personal
  • Minneapolis, MN
View GitHub Profile
@mpettis
mpettis / Graph_Explorations.R
Last active November 30, 2022 21:58
Using igraph, some basic i/o and plotting.
#' ---
#' title: "Graph_Explorations.R"
#' author: "Matt Pettis (Matthew.Pettis@gmail.com)"
#' date: "`r Sys.Date()`"
#' output:
#' html_document:
#' toc: true
#' toc_depth: 3
#' code_folding: hide
#' editor_options:
#;; Transform an input into shape fit for updating
#;; Here, each row has 3 observations, a modelnumber, serialnumber, and
#;; salesordernumber for each object.
library(readxl)
library(janitor)
#>
#> Attaching package: 'janitor'
#> The following objects are masked from 'package:stats':
#>
#> chisq.test, fisher.test
@mpettis
mpettis / localMidnightUTC.R
Last active December 6, 2021 20:08
Calculating UTC midnight during standard and DST
#;; Find the local midnight time in UTC for a given timezone
library(tidyverse)
library(lubridate)
library(glue)
# tgtTimezone <- "America/Chicago"
# tgtTimezone <- "America/Sao_Paulo"
# tgtYear <- 2021
#
@mpettis
mpettis / scoping.py
Last active September 19, 2021 21:28
Local variable scoping in Python. Keeps from polluting namespaces.
# https://pypi.org/project/scoping/
# See also: https://github.com/bskinn/tempvars
from scoping import scoping
aofl = [{"a":1, "b":2}, {"a":3, "b":4}]
with scoping():
for d in aofl:
x = d
scoping.keep('x')
# Plot date ranges with color scheme, using scale_fill_manual()
library(tidyverse)
plot_df <-
tribble(
~start_date, ~end_date, ~cat,
"2021-01-01","2021-01-02","a",
"2021-01-03","2021-01-05","b"
) %>%
mutate(cat = factor(cat)) %>%
mutate(start_date = ymd(start_date)) %>%
@mpettis
mpettis / httr2curl
Created January 27, 2021 21:36
httr2curl POST reprex
``` r
library(httr)
library(httr2curl)
cat(
h2c(
httr::POST("https://api.tis-dev.trane.com/tis-security-context/securityContext",
httr::add_headers(.headers=c("Content-Type"="application/json", "Accept"="application/json")),
body = '{"username":"myusername","password":"mypassword"}',
encode = "json")
@mpettis
mpettis / Lag-function-range-dynamic-create.R
Last active January 12, 2023 16:50
Parsing lag functions from strings
# See: https://gist.github.com/drsimonj/2038ff9f9c67063f384f10fac95de566
# See: https://adv-r.hadley.nz/meta-big-picture.html
# See: https://dplyr.tidyverse.org/reference/tidyeval.html
# See: https://dplyr.tidyverse.org/articles/programming.html
library(dplyr)
# Set up lags
lags <- c(1:30)
@mpettis
mpettis / lubridate-parsing-timestamps.R
Last active October 1, 2020 20:11
Parsing timestamps in R
``` r
# Load libraries
library(lubridate)
#> Warning: package 'lubridate' was built under R version 3.6.3
#>
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#>
#> date, intersect, setdiff, union
library(dplyr)
@mpettis
mpettis / timestamp-conversion-initial-intended-tzone.R
Last active September 2, 2020 16:21
Converting timestamps from a wrong timezone to a right timezone
### Let's say you get timestamps that do not have their tzone info as part of the timestamp string,
### but stored in a separate field. Further, assume your system has read them in using your local
### tzone. How do use knowledge of our local timezone and that of the the intended timezone to
### correct the timestamp to be in the intended timezone?
###
### In this exercise, we have the following:
### - The timestamp, with no explicit timezone which was read into system using default initial
### timezone of the first user.
### - A second system that has timezone different from the first timezone.
### - The intended timezone of the timestamp, which is what the timestamp should be in.
(require '[clara.rules :as r])
;;;; Define 3 rules, where the "priority" order is r1, r2, r3, where the highest priority is first
;;;; and the rest is in descending order of priority.
;;;; :type :rule/result "syntetic" fact is used to hold the final changes that can be queried out
;;;; from a session after `r/fire-rules` via `r/query` on the `find-results` query.
;;;; A namespace qualified keyword is used to avoid collision with externally given :type of
;;;; "real" facts.