Skip to content

Instantly share code, notes, and snippets.

@dcki
dcki / error_handling.py
Created June 7, 2024 05:31
Customize SanicException response body
from sanic import response, Sanic
from sanic.exceptions import SanicException
from sanic.request import Request
from sanic.response import HTTPResponse
# NOTE: This error should only be raised after add_custom_error_handler()
# has been called to register a custom error handler on the
# currently-running app.
class CustomSanicException(SanicException):
# Example: ruby read_multiple_streams.rb a <(for x in $(seq 10); do echo $x; sleep 1; done) <(echo bs98dfh8dhf9dshfdshf9shdf98shd98fshd98fhs98fhds) asdf <(sleep 2; echo ababababababababababababababababababababab)
io_objects = []
ARGV.each do |arg|
if arg =~ /^\/dev\/fd\/[0-9]+$/
io_objects << IO.new(IO.sysopen(arg))
end
end
ARGV.clear
@dcki
dcki / p2p_chat.rb
Created May 10, 2017 06:58
p2p chat (with NAT hole punching, I think)
require 'socket'
require 'io/wait' # Provides socket#ready?
require 'timeout'
if ARGV.length != 1
abort 'Usage: ruby p2p_chat.rb their_ip_address'
end
my_port = 2000
their_host = ARGV[0]
cat > bar.c << EOF
#include<stdio.h>
void bar() { printf("hello!\n"); }
EOF
gcc bar.c -fpic -shared -o libbar.so
cat > bar.h << EOF
void bar();
EOF
cat > foo.c << EOF
#include "bar.h"
# Below, "telnet curl" means curl telnet://localhost:8080/
# "http curl" means curl http://localhost:8080/
# telnet curl is sometimes used here because it approximates a raw tcp mode for
# curl. On OS X telnet curl waits for more input on the client side, not sure
# why. Linux telnet curl exits immediately, which is desirable for these tests.
require 'socket'
file_counter = 0
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
require 'open-uri'
require 'csv'
require 'pry'
file1 = '/tmp/1-1000.txt'
file2 = '/tmp/4000-most-common-english-words-csv.csv'
if File.exist?(file1)
words1 = File.read(file1).split
else
uri1 = URI.parse('https://gist.githubusercontent.com/deekayen/4148741/raw/01c6252ccc5b5fb307c1bb899c95989a8a284616/1-1000.txt')
@dcki
dcki / .rb
Created February 13, 2016 04:49
class A
attr_accessor :b
def initialize
b = 1
end
def foo
p b
end
end
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.github.vinietskyzilla</groupId>
<artifactId>my-app</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>my-app</name>
<url>http://maven.apache.org</url>
<build>
$ RAILS_ENV=development bundle exec pumactl -F /var/www/puma_config.rb start
Puma starting in single mode...
* Version 2.11.2 (rbx 2.1.0), codename: Intrepid Squirrel
* Min threads: 0, max threads: 16
* Environment: development
* Daemonizing...
$ RAILS_ENV=development bundle exec pumactl -F /var/www/puma_config.rb start
Puma starting in single mode...
* Version 2.11.2 (rbx 2.1.0), codename: Intrepid Squirrel
* Min threads: 0, max threads: 16