Skip to content

Instantly share code, notes, and snippets.

View werediver's full-sized avatar
💭
🦀

Raman Fedaseyeu werediver

💭
🦀
View GitHub Profile
@werediver
werediver / validate_py3.py
Created November 5, 2012 09:41
10gen M101 course week 2 homework 2.3 validation script ported to python3
import base64
code="aW1wb3J0IHB5bW9uZ28KaW1wb3J0IHVybGxpYi5yZXF1ZXN0LCB1cmxsaWIuZXJyb3IsIHVybGxpYi5wYXJzZQppbXBvcnQgaHR0cC5jb29raWVqYXIKaW1wb3J0IHJhbmRvbQppbXBvcnQgcmUKaW1wb3J0IHN0cmluZwoKIyBtYWtlcyBhIGxpdHRsZSBzYWx0CmRlZiBtYWtlX3NhbHQobik6CiAgICBzYWx0ID0gIiIKICAgIGZvciBpIGluIHJhbmdlKG4pOgogICAgICAgIHNhbHQgPSBzYWx0ICsgcmFuZG9tLmNob2ljZShzdHJpbmcuYXNjaWlfbGV0dGVycykKICAgIHJldHVybiBzYWx0CgoKIyB0aGlzIGlzIGEgdmFsaWRhdGlvbiBwcm9ncmFtIHRvIG1ha2Ugc3VyZSB0aGF0IHRoZSBibG9nIHdvcmtzIGNvcnJlY3RseS4KCmRlZiBjcmVhdGVfdXNlcih1c2VybmFtZSwgcGFzc3dvcmQpOgogICAgdHJ5OgogICAgICAgIHByaW50KCJUcnlpbmcgdG8gY3JlYXRlIGEgdGVzdCB1c2VyICIsIHVzZXJuYW1lKQogICAgICAgIGNqID0gaHR0cC5jb29raWVqYXIuQ29va2llSmFyKCkKICAgICAgICB1cmwgPSAiaHR0cDovL2xvY2FsaG9zdDo4MDgyL3NpZ251cCIKCiAgICAgICAgZGF0YSA9IHVybGxpYi5wYXJzZS51cmxlbmNvZGUoWygiZW1haWwiLCIiKSwoInVzZXJuYW1lIix1c2VybmFtZSksICgicGFzc3dvcmQiLHBhc3N3b3JkKSwgKCJ2ZXJpZnkiLHBhc3N3b3JkKV0pCiAgICAgICAgcmVxdWVzdCA9IHVybGxpYi5yZXF1ZXN0LlJlcXVlc3QodXJsPXVybCwgZGF0YT1kYXRhLmVuY29kZSgidXRmOCIpKQogICAgICAgIG9wZW5
@werediver
werediver / websse.py
Last active June 1, 2023 14:17
Simple demonstration of how to implement Server-sent events (SSE) in Python using Bottle micro web-framework. SSE require asynchronous request handling, but it's tricky with WSGI. One way to achieve that is to use gevent library as shown here.
"""
Simple demonstration of how to implement Server-sent events (SSE) in Python
using Bottle micro web-framework.
SSE require asynchronous request handling, but it's tricky with WSGI. One way
to achieve that is to use gevent library as shown here.
Usage: just start the script and open http://localhost:8080/ in your browser.
Based on:
@werediver
werediver / singleton.py
Created December 28, 2012 09:51
A thread safe implementation of singleton pattern in Python. Based on tornado.ioloop.IOLoop.instance() approach.
import threading
# Based on tornado.ioloop.IOLoop.instance() approach.
# See https://github.com/facebook/tornado
class SingletonMixin(object):
__singleton_lock = threading.Lock()
__singleton_instance = None
@classmethod
@werediver
werediver / wing_ar.hs
Created July 5, 2013 11:44
Wing aspect ratio calculator in Haskell.
module WingAR where
import Data.Maybe
import Text.Printf
-- See http://en.wikipedia.org/wiki/Aspect_ratio_%28wing%29
wing_ar :: Float -> Float -> Float
wing_ar l s = l^2 / s
readMaybe s = listToMaybe $ fmap fst $ reads s
//-->fill(3,5)
// ans =
// 1. 3. 4. 9. 10.
// 2. 5. 8. 11. 14.
// 6. 7. 12. 13. 15.
function [x] = fill(N, M)
x = zeros(N, M)
Z = N + M - 1
i = 1
@werediver
werediver / adiag.sci
Created March 12, 2014 13:09
Scilab function returning 2D matrix antidiagonal by index.
//-->adiag([1 2 3 4; 5 6 7 8; 9 10 11 12],3)'
// ans =
// 9. 6. 3.
function [v] = adiag(x, z)
// 2D matrix antidiagonal
[N, M] = size(x)
// The total diagonals count
@werediver
werediver / ssa.sci
Last active August 29, 2015 13:57
Basic SSA (Singular Spectrum Analysis) implementation for Scilab.
function [y_out, m, sig] = ssa_normalize(y)
// http://en.wikipedia.org/wiki/Student%27s_t-statistic
m = mean(y)
sig = sqrt(variance(y)) // stdev(y)
y_out = (y - m) / sig
endfunction
function [y_out] = ssa(y, L, I)
[LAMBDA, U, V] = ssa_decompose(y, L)
y_out = ssa_reconstruct(LAMBDA, U, V, I)
@werediver
werediver / ssa-octave.m
Last active May 5, 2016 14:17
ssa.sci ( https://gist.github.com/werediver/9785544 ) adapted for GNU Octave
% Force GNU Octave to interpret this file as a script
1;
function [y_out, m, sig] = ssa_normalize(y)
% http://en.wikipedia.org/wiki/Student%27s_t-statistic
m = mean(y);
sig = sqrt(variance(y)) % stdev(y);
y_out = (y - m) / sig;
endfunction
@werediver
werediver / CircularQueue.h
Created August 22, 2014 11:45
Simple implementation of circular queue in Objective-C (for ARC-enabled environment).
//
// CircularQueue.h
//
// Created on 9/21/13.
//
#import <Foundation/Foundation.h>
@interface CircularQueue : NSObject <NSFastEnumeration>
@werediver
werediver / xcorr-mess.py
Last active May 1, 2016 13:23
Demonstration of different approaches to calculating cross-correlation (code written in IPython Notebook).
from __future__ import division, print_function
import numpy as np
import matplotlib.pyplot as pp
%matplotlib inline
def xcorr(a, b, mode='same'):
a = np.asarray(a)
b = np.asarray(b)
a = (a - a.mean()) / np.sqrt(np.correlate(a, a))