Skip to content

Instantly share code, notes, and snippets.

@eliangcs
eliangcs / ensure_json_encodable.js
Created May 17, 2023 05:25
How to ensure a value is JSON encodable (Node.js)
const assert = require('assert');
const _ = require('lodash');
const ensureJSONEncodable = (obj, path = null, visited = null) => {
if (obj === null || _.isBoolean(obj) || _.isNumber(obj) || _.isString(obj)) {
return;
}
path = path || [];
@eliangcs
eliangcs / genfile_to_s3.py
Created August 15, 2022 04:49
Generate a file dynamically and upload it to S3
import os
from io import BytesIO
import boto3
BUCKET = 'bucket-name'
def gen_int():
a = 0
@eliangcs
eliangcs / httplogger.sh
Created July 27, 2018 03:48
A minimal netcat web server that prints everything it gets
httplogger() {
while true; do
echo -e "HTTP/1.1 200 OK\r\n\r\nok" | nc -vl 8989
test $? -gt 128 && break
echo
echo '----------------------------------------'
done
echo
}
@eliangcs
eliangcs / search_github_issues_and_prs.py
Last active November 2, 2017 10:02
Script Filter for Alfred listing all the issues and PRs assigned to or created by you
#!/Users/YOUR_USERNAME/.pyenv/versions/3.6.2/envs/alfred/bin/python
import functools
import json
import os
from datetime import datetime
from threading import Thread
import requests
@eliangcs
eliangcs / http-prompt-story.md
Last active June 3, 2020 06:49
How I created HTTP Prompt and got 5000+ stars on GitHub

How I Created HTTP Prompt and Got 5000+ Stars on GitHub

Two months ago, I published an open source project, which went viral by gaining 1200+ stars in the first 24 hours. As of this writing, it has attracted 5000+ stars. The project is called HTTP Prompt:

https://github.com/eliangcs/http-prompt

Here I want to share its development story.

It all began with Vertica. Not long ago, I used Vertica every day for my work. Vertica is a powerful database, but the official client (vsql) is not powerful at all. The GUI alternative, DbVisualizer, is bad as well.

@eliangcs
eliangcs / guess_encoding.py
Created January 22, 2016 06:37
Guess encoding by looking at the content
def guess_encoding(s):
"""Guess the character encoding from a byte string."""
# Check BOM
# 0xFF, 0xFE -> UTF-16 LE
# 0xFE, 0xFF -> UTF-16 BE
# 0xEF, 0xBB, 0xBF -> UTF-8
bom = s[0:2]
if bom == '\xff\xfe':
return 'utf-16'
elif bom == '\xfe\xff':
@eliangcs
eliangcs / index.html
Last active January 18, 2016 06:44
Integrating sine with Monte Carlo / Metropolis algorithm
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.svg {
border: 1px solid #000;
}
.result {
color: #05c;
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.background {
fill: #e7e7e7;
}
.axis path,
.axis line {
@eliangcs
eliangcs / tornado_subprocess.py
Last active January 7, 2020 08:48
A minimal web server that runs shell commands, powered by Tornado and its Subprocess module
"""
A minimal web server that runs shell commands, powered by Tornado and its
Subprocess module. It does non-blocking IO and streams the response.
To start the server:
$ python tornado_subprocess.py
To send a shell command using httpie:
@eliangcs
eliangcs / shortcuts.sh
Last active January 18, 2016 06:29
Shell shortcuts
# Clear local merged branches
function clearbranches(){
cur_branch=`git rev-parse --abbrev-ref HEAD`
if [[ "$cur_branch" == "master" ]]; then
git branch --merged | grep -v "\*" | xargs -n 1 git branch -d
else
echo "Not on master branch!"
fi
}