Skip to content

Instantly share code, notes, and snippets.

View loretoparisi's full-sized avatar
🐍
NightShift

Loreto Parisi loretoparisi

🐍
NightShift
View GitHub Profile
@loretoparisi
loretoparisi / basehandler.py
Created October 25, 2019 10:55
Tornado BaseHandler for IOLoop run in executor and error handling
class BaseHandler(tornado.web.RequestHandler):
@tornado.gen.coroutine
def post_async(self, *args):
resp_dict = yield ioloop.IOLoop.current().run_in_executor(None, self.post_handler, args)
return resp_dict
@tornado.web.asynchronous
@tornado.gen.coroutine
def post(self, *args):
@loretoparisi
loretoparisi / tornado_webapp.py
Created October 24, 2019 10:36
Tornado Catch allError Handler example
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
import handlers as th
from tornado import web
class ErrorHandler(web.RequestHandler):
"""Generates an error response with status_code for all requests."""
@loretoparisi
loretoparisi / ec2_ip_address_docker_host.js
Created June 25, 2019 14:14
Get Docker Host IP Address for EC2 instances
const cp = require('child_process');
const ec2 = function (callback) {
const URL = 'http://169.254.169.254/latest/meta-data/local-ipv4';
// we make it silent and timeout to 1 sec
const args = [URL, '-s', '--max-time', '1'];
const opts = {};
cp.execFile('curl', args, opts, (error, stdout) => {
if (error) return callback(new Error('ec2 ip error'));
else return callback(null, stdout);
})
@loretoparisi
loretoparisi / dehyphen.py
Last active June 5, 2019 08:52
De hyphenate a word with hyphens to matched word - Repl: https://repl.it/@loretoparisi/DeHyphen
import re
text = "oh-oh-oh c-c-c-c-come to home today c-c-c-c-come to me fine-tuning"
print(re.sub(r'(\w+(?:-))+(\w+)', '\\2', text))
print(re.sub(r'(?<!\S)(\w{1,3})(?:-\1)*-(\w+)(?!\S)', '\\2', text))
#pattern = r"(?<=-)\w+(?=[^-\w])"
pattern = r"(?<!\S)(\w{1,3})(?:-\1)*-(\w+)(?!\S)"
r = re.compile(pattern, flags=re.I | re.X | re.UNICODE)
@loretoparisi
loretoparisi / longest_subsequence.py
Last active May 30, 2019 12:25
Longest Increasing Subsequence with Linear and Bisect (Binary) Search - http://research.variancia.com/unl-aligner/
def longest_subsequence_bisect(seq, mode='strictly', order='increasing',
key=None, index=False):
"""
>>> longest_subsequence_bisect([1,2,3,4,5,6,7,2,2,2,2,2,5,1,7,8])
Return the longest increasing subsequence of `seq`.
Parameters
----------
@loretoparisi
loretoparisi / find.js
Last active April 30, 2019 16:15
Find and Head with Node.js
function scanDirStream(needle, params) {
var options = {
// find -type
type: 'f',
// find -name
name: '*',
limit: 100
};
for (var attrname in params) { options[attrname] = params[attrname]; }
return new Promise((resolve, reject) => {
@loretoparisi
loretoparisi / icu4c_macos.sh
Created April 29, 2019 13:59
Install icu4c on macOS
export ICU_VERSION=63.1
export PYICU_INCLUDES=/usr/local/Cellar/icu4c/63.1/include
export PYICU_LFLAGS=-L/usr/local/Cellar/icu4c/63.1/lib
export PYICU_CFLAGS=-std=c++11
pip install pyicu
@loretoparisi
loretoparisi / asyncio_python.py
Created April 18, 2019 15:44
Python Async Await with AsyncIO
import asyncio
async def demo():
print("foo")
await asyncio.sleep(5)
return "bar"
def main():
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
result = loop.run_until_complete(demo())
@loretoparisi
loretoparisi / awscat.sh
Last active April 18, 2019 11:08
AWS CAT - Cat a file from Aws Bucket in Bash
function __awscat {
if [ "$1x" != 'x' ]; then
aws s3 cp --quiet "$1" /dev/stdout
fi
}
alias awscat='__awscat'
@loretoparisi
loretoparisi / emoticons.js
Created April 17, 2019 18:19
Node.js JavaScript Emoticons text codes To Icons
module.exports = {
100: "💯",
1234: "🔢",
grinning: "😀",
smiley: "😃",
smile: "😄",
grin: "😁",
laughing: "😆",
satisfied: "😆",
sweat_smile: "😅",