Skip to content

Instantly share code, notes, and snippets.

View tkutcher's full-sized avatar
🤔

Tim Kutcher tkutcher

🤔
View GitHub Profile
@tkutcher
tkutcher / homework_template.tex
Created February 5, 2018 15:32
Generic template for homework assignments
\documentclass[11pt]{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{setspace}
\usepackage{listings}
\usepackage{alltt}
\makeatletter
@tkutcher
tkutcher / anchor-routing-hack.app.component.ts
Last active October 26, 2020 15:18
Angular Anchor Routing
import { Component, Inject, OnInit, PLATFORM_ID } from '@angular/core';
import { Router, Event, Scroll } from '@angular/router';
import { isPlatformBrowser } from '@angular/common';
// https://gomakethings.com/how-to-test-if-an-element-is-in-the-viewport-with-vanilla-javascript/
const isInViewport = (elem) => {
const bounding = elem.getBoundingClientRect();
return (
bounding.top >= 0 &&
bounding.left >= 0 &&
@tkutcher
tkutcher / add-envs-angular.json
Created January 10, 2022 02:05
Environment-Specific Configuration for Angular Applications
"configurations": {
"production": {
// ...
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
]
},
"configurations": {
"production": {
"fileReplacements": [
{
"replace": "src/environments/environment.ts",
"with": "src/environments/environment.prod.ts"
}
]
},
"development": {
/** A configuration object with environment-specific properties. */
export interface AppEnvironment {
/** True if the @angular/core enableProdMode should be called. */
production: boolean;
/** The api server address. */
apiAddress: string;
}
import { environment } from "../../environments/environment";
@Injectable({ providedIn: "root" })
export class MyApiService {
constructor(private http: HttpClient) {}
getFoo(): Observable<any> {
return this.http.get(`${environment.apiAddress}/foo`);
}
}
@tkutcher
tkutcher / app.module.ts
Last active January 10, 2022 02:20
Environment-Specific Angular Configuration - Add Injection Token
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { MY_API_BASE } from './my-api.service';
import { environment } from '../environments/environment';
@NgModule({
declarations: [ AppComponent ],
imports: [ BrowserModule ],
providers: [
@tkutcher
tkutcher / buffered_iterator.py
Last active February 9, 2022 17:28
Python BufferedIterator
"""
buffered_iterator - Decorates an iterator with "has_next" by buffering values.
Example use case for this is if you have a base interface/ABC that has methods
`has_next` and `get_next`, and several classes realizing this interface already
would have iterators internally, you wouldn't want them to each have to
implement the logic for `has_next` - so it would be better to decorate an
iterator with some sort of `has_next`.
Inspiration from
@tkutcher
tkutcher / my_config.py
Created February 19, 2022 17:45
File IO Demo 1
@dataclasses.dataclass
class MyConfig:
x: str
y: str
def read_from_file() -> MyConfig:
with open("my-config.json", "r") as f:
d = json.load(f)
return MyConfig(d["x"], d["y"])
@tkutcher
tkutcher / my_config.py
Last active February 19, 2022 17:48
Testing File IO demo 2
def read_from_file(src_path="my-config.json") -> MyConfig:
with open(src_path, "r") as f:
d = json.load(f)
return MyConfig(d["x"], d["y"])
def write_to_file(c: MyConfig, dest_path="my-config.json") -> None:
with open(dest_path, "w") as f:
f.write({"x": c.x, "y": c.y})