Skip to content

Instantly share code, notes, and snippets.

import xml.etree.ElementTree as ET
import pandas as pd
def make_field(parent_el, attrib={}, text=None):
attrib = dict([e for e in attrib.items() if e[1] is not None])
e = ET.SubElement(parent_el, 'field', attrib)
if not text is None:
e.text = text
return e
@Chris927
Chris927 / generate-secret-and-link.js
Last active September 10, 2020 04:31
otplib, generate uri (for a qr code), which works for both Google Authenticator and Microsoft Authenticator
@Chris927
Chris927 / usePersistentState.js
Created April 30, 2020 13:37
React Native, use persistent state via a hook
import React, { useState, useEffect } from 'react'
import { useAsyncStorage } from '@react-native-community/async-storage'
export default function usePersistedState(initialValue, { key }) {
if (!key) {
throw new Error('key is required');
}
const [localState, setLocalState] = useState(initialValue)
const { getItem, setItem } = useAsyncStorage('@' + key)
@Chris927
Chris927 / main.dart
Created December 31, 2019 06:58
Dart lang, foolin around
// just foolin around
List<int> f() {
return List.generate(20, (i) => i * i);
}
void main() {
for (int i = 0; i < 5; i++) {
print('hello ${i + 1}, $i');
}
[
{
"page": 1,
"pages": 1,
"per_page": "50",
"total": 42
},
[
{
"id": "ABW",
@Chris927
Chris927 / display-duration.js
Created July 8, 2017 10:14
How to use momentjs to reasonably cleanly format a duration
// inspired by https://github.com/moment/moment/issues/1048#issuecomment-23998922
const m = require('moment')
const displayDuration = (start, end) => {
const diff = end.diff(start)
const hours = end.diff(start, 'hour')
return hours === 0
@Chris927
Chris927 / riddle.js
Created May 3, 2016 06:46
A riddle (for a code snippet session, maybe)
// a "generic" function doing some work for us (in this case, just multiply
// value with a factor `n`)
function times(value, n) {
return value * n;
}
// "higher order function" to generate more specific functions
function makeTimes(n) {
return function(value) {
return times(value, n);
@Chris927
Chris927 / server.js
Last active November 25, 2015 14:40
NodeJS Express, use middleware to protect routes
var express = require('express');
var app = express();
var port = 3100;
app.listen(port, function() {
console.log('listening on port ' + port);
});
/* public routes */
- certain endpoints are always blocked
if nginx_uri == "/_access_token" or nginx_uri == "/_me" then
ngx.exit(403)
end
-- import requirements
local cjson = require "cjson"
-- setup some app-level vars
local app_id = "APP_ID"
@Chris927
Chris927 / demo.conf
Created July 24, 2014 20:05
How I got rsyslog to email me when 'demo' or 'test' appears (/etc/rsyslog.d/demo.conf)
$ModLoad /usr/lib/rsyslog/ommail.so
$ActionMailSMTPServer localhost
$ActionMailFrom rsyslog@uber5.com
$template mailSubject,"Rsyslog alert for %hostname% at %timegenerated%"
$template mailBody,"%msg%"
$ActionMailSubject mailSubject
$ActionExecOnlyonceEveryInterval 60
$ActionMailTo chris@uber5.com
if ($msg contains 'demo') or ($msg contains 'test') then :ommail:;mailBody