Skip to content

Instantly share code, notes, and snippets.

@pipoket
pipoket / udpserver.py
Created November 25, 2011 07:40
UDP server implementation for gevent. Copied from http://code.google.com/p/gevent/issues/detail?id=50
# Copyright (c) 2009-2010 Denis Bilenko. See LICENSE for details.
"""UDP/SSL server"""
import sys
import errno
import traceback
from gevent import socket
from gevent import core
from gevent.baseserver import BaseServer
@sehugg
sehugg / nietz_stateful.py
Last active October 16, 2017 19:45
Stateful LSTM text generation with Keras 1.0
#!/usr/bin/env python
from __future__ import print_function
from keras.models import Sequential
from keras.layers import TimeDistributed
from keras.layers.core import Dense, Activation, Dropout, RepeatVector, TimeDistributedDense
from keras.layers.recurrent import LSTM
from keras.utils.data_utils import get_file
import numpy as np
import random,string
import sys
@plugnburn
plugnburn / README.md
Last active June 1, 2018 21:42
XT.js - DOM construction / templating library in 18 lines of JS, 323 bytes minified

XT.js

Let's close the ultra-small library cycle with some awesome array-based templating. 323 bytes minified.

How to obtain

Just download the minified version here or include it into your code:

@mtesseracted
mtesseracted / numpyrotate.py
Last active March 10, 2019 12:40
NumPy logo in rotating voxels
'''
matplotlib must be developer release for voxel support
install instructions:
https://matplotlib.org/devdocs/users/installing.html
'''
import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as manimation
@nicksam112
nicksam112 / keras_es.py
Last active November 28, 2020 16:02
Evolution Strategies with Keras
#Evolution Strategies with Keras
#Based off of: https://blog.openai.com/evolution-strategies/
#Implementation by: Nicholas Samoray
#README
#Meant to be run on a single machine
#APPLY_BIAS is currently not working, keep to False
#Solves Cartpole as-is in about 50 episodes
#Solves BipedalWalker-v2 in about 1000
@jgomezdans
jgomezdans / hessian.py
Created July 19, 2012 15:21
Finite difference approach to calculating the Hessian
#!/usr/bin/env python
"""
Some Hessian codes
"""
import numpy as np
from scipy.optimize import approx_fprime
def hessian ( x0, epsilon=1.e-5, linear_approx=False, *args ):
"""
@gingerBill
gingerBill / defer.cpp
Last active November 20, 2022 17:45
Golang Defer in C++
////////////////////////////////////////////////////////////////
//
// Defer statement
// - Akin to D's SCOPE_EXIT or similar to Go's defer but scope-based
//
////////////////////////////////////////////////////////////////
#if defined(__cplusplus)
extern "C++" {
// NOTE(bill): Stupid fucking templates
template <typename T> struct gbRemove_Reference { typedef T Type; };
@gregvish
gregvish / chat.py
Last active February 9, 2023 12:33
Python 3.4 asyncio chat server example
from socket import socket, SO_REUSEADDR, SOL_SOCKET
from asyncio import Task, coroutine, get_event_loop
class Peer(object):
def __init__(self, server, sock, name):
self.loop = server.loop
self.name = name
self._sock = sock
self._server = server
Task(self._peer_handler())
@mangecoeur
mangecoeur / concurrent.futures-intro.md
Last active January 9, 2024 16:04
Easy parallel python with concurrent.futures

Easy parallel python with concurrent.futures

As of version 3.3, python includes the very promising concurrent.futures module, with elegant context managers for running tasks concurrently. Thanks to the simple and consistent interface you can use both threads and processes with minimal effort.

For most CPU bound tasks - anything that is heavy number crunching - you want your program to use all the CPUs in your PC. The simplest way to get a CPU bound task to run in parallel is to use the ProcessPoolExecutor, which will create enough sub-processes to keep all your CPUs busy.

We use the context manager thusly:

with concurrent.futures.ProcessPoolExecutor() as executor:
@typehorror
typehorror / Flask-SQLAlchemy Caching.md
Last active February 15, 2024 14:44
Flask SQLAlchemy Caching

Flask-SQLAlchemy Caching

The following gist is an extract of the article Flask-SQLAlchemy Caching. It allows automated simple cache query and invalidation of cache relations through event among other features.

Usage

retrieve one object

# pulling one User object

user = User.query.get(1)