Skip to content

Instantly share code, notes, and snippets.

View smahs's full-sized avatar

Shams Madhani smahs

  • Eindhoven, Netherlands
View GitHub Profile
@smahs
smahs / gist:5637eef8fe4d6593b26f
Created March 23, 2015 07:47
Emulating try-catch in #golang with multi-return statements
var hids [][]string
db, err := sql.Open("postgres", "user=myuser dbname=mydb sslmode=disable")
if err != nil {
log.Fatal(err)
os.Exit(1)
} else {
defer db.Close()
rows, err := db.Query(my_query)
if err != nil {
log.Println(err)
@smahs
smahs / map_n_merge.go
Last active August 29, 2015 14:25
Generate a hashmap from a struct (without reflection magic), and then merge it with another map and return a merged json.
/* The last I checked, this gist can be run at:
http://play.golang.org/p/5keNQZLqfR
*/
package main
import (
"fmt"
"encoding/json"
)
@smahs
smahs / db_query_stringify.go
Created August 9, 2015 15:14
Returns SQL select query data as [][]string, like dynamic languages. Time and memory complexities are, of course, not optimal.
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
// Connection pool as a singleton
var dbConn *sql.DB
/* Returns an open connection to the database
@smahs
smahs / django_login_wrapper.py
Last active September 8, 2015 08:39
Modified login_required middleware for performing custom auth tasks in django.
from django.http import HttpResponse
from json import dumps
def errorify(message):
return HttpResponse(dumps({
'error': message
}), content_type = 'application/json')
@smahs
smahs / mfrootdaemon.patch
Created September 22, 2010 19:43
Patch for Mint-FM2 menu sub-system to enable tuxonice support on Arch Linux
diff -c /etc/rc.d/mfmrootdaemon /etc/rc.d/mfmrootdaemon.new
*** /etc/rc.d/mfmrootdaemon 2010-09-21 20:52:50.000000000 +0200
--- /etc/rc.d/mfmrootdaemon.new 2010-09-22 21:27:34.382440785 +0200
***************
*** 95,101 ****
then
rm -rf $CIAODIR/*
# aplay /usr/share/sounds/mintfb-logout.wav &
! pm-suspend
fi
@smahs
smahs / gist:7133657
Last active December 26, 2015 09:59 — forked from pjenvey/gist:3808830
# This fork removes the request parameter for use with Flask framework
class JsonSerializableMixin(object):
def __json__(self):
"""
Converts all the properties of the object into a dict for use in json.
You can define the following in your class
_json_eager_load :
@smahs
smahs / dj_rest.py
Last active March 28, 2016 11:49
Micro REST Framework for Django: A Prototype
from django.views.generic import View
from django.http import (
QueryDict, HttpResponse, HttpResponseBadRequest,
HttpResponseNotFound, HttpResponseNotAllowed,
)
from django.core.serializers.json import DjangoJSONEncoder
from django.core.exceptions import (
ObjectDoesNotExist, ValidationError,
)
from django.db import transaction
@smahs
smahs / tetris.py
Created August 9, 2015 08:30
A full and text only, yet very much playable, version of Tetris written in Python without any dependencies. No curses or ncurses!
from random import choice
from copy import deepcopy
from sys import exit
# Initial shape matrices for the five Tetrominos
shapes = [[[0, 0, 0, 0], [1, 1, 1, 1], [0, 0, 0, 0], [0, 0, 0, 0]],
[[1, 0, 0], [1, 0, 0], [1, 1, 0]],
[[0, 0, 1], [0, 0, 1], [0, 1, 1]],
[[0, 0, 1], [0, 1, 1], [0, 1, 0]], [[1, 1], [1, 1]]]
@smahs
smahs / merge_sort.py
Created June 28, 2018 09:07
Merge Sort - the Pythonic way
def sort(l):
if len(l) <= 1:
return l
mid = len(l) // 2
return merge(sort(l[:mid]), sort(l[mid:]))
def merge(l, r):
out = []
while l and r:
if l[0] <= r[0]: