Skip to content

Instantly share code, notes, and snippets.

View mgronhol's full-sized avatar

Markus Grönholm mgronhol

View GitHub Profile
@mgronhol
mgronhol / Python (x)html templating with context manager
Created December 12, 2011 07:37
Referring to https://gist.github.com/1461441 , a pure python (no extra modules) version (NOT thread safe).
#!/usr/bin/env python
class HtmlDocument( object ):
TAGS = ['html', 'head', 'body', 'div', 'link', 'script',
'title', 'meta', 'p', 'a', 'span', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
'ul', 'li', 'ol', 'pre', 'code', 'footer', 'article', 'canvas', 'img', 'text' ]
def __init__( self ):
@mgronhol
mgronhol / DijkstraDemo.java
Created March 6, 2012 08:54
Dijkstra shortest path
import java.io.*;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Collections;
// Linkki kahden noden välillä
class Edge {
private String fromNode, toNode;
private double weight; // weight == etäisyys noiden kahden noden välillä
@mgronhol
mgronhol / KnnDemo.java
Created March 6, 2012 11:23
k-NN luokitin
import java.io.*;
import java.util.Iterator;
import java.util.HashMap;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.ArrayList;
import java.util.Collections;
@mgronhol
mgronhol / stdinReader1.java
Created April 11, 2012 10:02
Java simple read line from stdin.
import java.io.*;
public class stdinReader1 {
public static void main( String args[] ){
System.out.println( "Moi kaikki!" );
try {
BufferedReader reader = new BufferedReader( new InputStreamReader( System.in ) );
@mgronhol
mgronhol / gist:2658316
Created May 11, 2012 08:18
Näppiksen käsittely C:llä.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
int main(){
@mgronhol
mgronhol / trie.py
Created May 13, 2012 16:10
Trie in Python
#!/usr/bin/env python
class Node( object ):
def __init__( self, end_node = False ):
self.end_node = end_node
self.prefix_count = 0
self.children = {}
@mgronhol
mgronhol / gist:2936176
Created June 15, 2012 12:14
Nginx fastcgi cachcing
## Taa ensin tonne HTTP tai HTTPS blockin sisalle (esim. virtual.conf tai sitten sites-enables/default tms filuun)
fastcgi_cache_path /var/cache/nginx
levels=1:2 keys_zone=ZONE_KEY_TAHAN:10m
inactive=5m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
# ja taa sitten locationiin
@mgronhol
mgronhol / gist:6553af49ff4d173c29b2
Created July 17, 2012 09:34
Small stack virtual machine
/*
Commands in the virtual machine
+ - * / :Pop two numbers from the stack, [add|sub|mul|div] them and push the result back
pop :Pops a value from the stack
print :Pops a value from the stack and prints it to stdout
@mgronhol
mgronhol / hello-world.c
Created August 29, 2012 12:14
Random hello world
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char **argv ){
int i;
srand( 22315606 ); rand();
for( i = 0 ; i < 5 ; ++i ){ printf( "%c", 'A' + rand() % 32 ); }
@mgronhol
mgronhol / php-append-log.php
Created September 6, 2012 08:13
Append log for PHP [4|5]
<?php
function append_log_write( $filename, $parametres ){
// Return FALSE if no parametres given
if( count( $parametres ) < 1 ){
return FALSE;
}
// Timestamp in Zebra Time