Skip to content

Instantly share code, notes, and snippets.

View vsajip's full-sized avatar

Vinay Sajip vsajip

View GitHub Profile
/* Generated by: JavaCC 21 Parser Generator. JSONParser.java */
package org.parsers.json;
import org.parsers.json.ast.*;
import java.util.*;
import java.util.concurrent.CancellationException;
import java.util.logging.*;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
/* Generated by: JavaCC 21 Parser Generator. Do not edit. JSONConstants.java */
package org.parsers.json;
/**
* Token literal values and constants.
*/
public interface JSONConstants {
public enum TokenType {
EOF, WHITESPACE, COLON, COMMA, OPEN_BRACKET, CLOSE_BRACKET, OPEN_BRACE, CLOSE_BRACE, TRUE, FALSE, NULL, STRING_LITERAL, NUMBER, INVALID
}
@vsajip
vsajip / gist:b5d678fe128c566d4827ed04632eceb3
Last active December 1, 2021 19:08 — forked from dusual/gist:9838932
Heroku Bottle app
mkdir heroku
cd heroku/
virtualenv --no-site-packages env
source env/bin/activate
pip install bottle gevent
pip freeze > requirements.txt
cat >app.py <<EOF
try:
import gevent.monkey
@vsajip
vsajip / client.py
Last active April 30, 2024 03:40
Run a logging socket receiver in a production setting with logging from an example webapp
from concurrent.futures import ThreadPoolExecutor, as_completed
import json
import urllib.request
with open('webapp.json', encoding='utf-8') as f:
config = json.loads(f.read())
URLS = [
'http://localhost:%d/?ident=%d' % (config['port'], ident)
for ident in range(1, 1001)
@vsajip
vsajip / handles.py
Created September 25, 2021 15:33
Duplicate stdio handles on Windows using ctypes
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 Red Dove Consultants Limited. BSD-3-Clause licensed.
#
from ctypes import *
from ctypes.wintypes import BYTE,WORD,DWORD,LPWSTR,LPCWSTR,HANDLE,LPVOID,BOOL
import os
import sys
@vsajip
vsajip / fix_shebang.py
Created August 14, 2021 12:51
A Python script to fix up shebangs in text files and zip archives with/without Windows launchers
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (C) 2021 Red Dove Consultants Limited
#
# Redistribution and use in source and binary forms, with or without modification, are
# permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list
# of conditions and the following disclaimer.
@vsajip
vsajip / test.ftl
Last active June 25, 2021 22:39
Python code generation template and result - how to pass indentation across multiple levels of nested directives?
[#macro BuildCodeSequence expansion indent]
[#var is = ""?right_pad(indent)]
${is}# DBG > BuildCodeSequence ${indent}
[#list expansion.units as subexp]
[@BuildCode subexp indent /]
[/#list]
${is}# DBG < BuildCodeSequence ${indent}
[/#macro]
[#macro BuildExpansionCode expansion indent]
[#var is=""?right_pad(indent)]
@vsajip
vsajip / remove_silence.py
Created April 9, 2021 17:18 — forked from fred9/remove_silence.py
Remove silences
# From https://stackoverflow.com/questions/29547218/
# remove-silence-at-the-beginning-and-at-the-end-of-wave-files-with-pydub
from pydub import AudioSegment
def detect_leading_silence(sound, silence_threshold=-50.0, chunk_size=10):
'''
sound is a pydub.AudioSegment
silence_threshold in dB
chunk_size in ms
@vsajip
vsajip / htpasswd.py
Created October 28, 2020 12:42 — forked from eculver/htpasswd.py
htpasswd script in python (no need to install apache utils)
#!/usr/local/bin/python
"""Replacement for htpasswd"""
# Original author: Eli Carter
import os
import sys
import random
from optparse import OptionParser
# We need a crypt module, but Windows doesn't have one by default. Try to find
@vsajip
vsajip / home-server.md
Created October 26, 2020 12:21 — forked from nileshtrivedi/home-server.md
Home Server setup: Raspberry PI on Internet via reverse SSH tunnel

Raspberry Pi on Internet via reverse SSH tunnel

HackerNews discussed this with many alternative solutions: https://news.ycombinator.com/item?id=24893615

I already have my own domain name: mydomain.com. I wanted to be able to run some webapps on my Raspberry Pi 4B running perpetually at home in headless mode (just needs 5W power and wireless internet). I wanted to be able to access these apps from public Internet. Dynamic DNS wasn't an option because my ISP blocks all incoming traffic. ngrok would work but the free plan is too restrictive.

I bought a cheap 2GB RAM, 20GB disk VM + a 25GB volume on Hetzner for about 4 EUR/month. Hetzner gave me a static IP for it. I haven't purchased a floating IP yet.