Skip to content

Instantly share code, notes, and snippets.

View namsral's full-sized avatar

Lars Wiegman namsral

View GitHub Profile
@namsral
namsral / Random Password Generator
Created March 18, 2014 15:06
Random password generator with a default entropy of 84 bits.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, string
def random_password(length=14):
"""
Random password generator.
Default generated password will have an entropy of 84 bits.
@namsral
namsral / gist:1716333
Created February 1, 2012 10:10
Mixin for the Djando Rest Framework (DFR) to filter objects by authenticated user.
class AuthenticatedUserResourceMixin(ResourceMixin):
permissions = (IsAuthenticated,)
def build_query(self, *args, **kwargs):
tmp = dict(kwargs)
if BaseRenderer._FORMAT_QUERY_PARAM in tmp:
del tmp[BaseRenderer._FORMAT_QUERY_PARAM]
@namsral
namsral / gist:1717329
Created February 1, 2012 14:33
Mixin for the Djando Rest Framework (DFR) to auto-complete objects on creating.
class AutoCompleteResourceMixin(ResourceMixin):
def validate_request(self, data, files=None):
"""
Auto-complete objects when they are created.
A REST equivalent to auto-populate for Django web forms.
"""
method = self.view._method if hasattr(self.view, '_method') else None
@namsral
namsral / zippy.html
Created February 3, 2012 09:58
Zippy example from the Angular.js talk at HTML5 Dev Conf
<!doctype html>
<html xmlns:ng="http://angularjs.org">
<meta charset="UTF-8" />
<script src="http://code.angularjs.org/angular-0.9.19.min.js" ng:autobind></script>
<body>
<zippy class="open">
<header>Greeting</header>
<pane>Hello World!</pane>
</zippy>
@namsral
namsral / Bindings.plist
Created March 8, 2012 09:31
TexMate icon bindings preference file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>icon-template</key>
<array>
<string>tmpl</string>
</array>
<key>icon-xml</key>
<array>
@namsral
namsral / gist:7673054
Last active December 29, 2015 12:49
Account Authentication Example
POST /api/authenticate
> Content-Type: application/x-www-form-urlencoded
>
user=lars,pass=secret
< 200 OK
<
{
"token": "897ca80f3443de1ad9c0ce8f9d7c4449c68606e0"
}
@namsral
namsral / person.go
Last active September 6, 2016 15:35
Example of a getter and setter in a single method
package person_test
import (
"fmt"
"strings"
)
// Person is a modern human with a name.
type Person struct {
name string
@namsral
namsral / go.snippets
Created October 5, 2016 22:02
Go table-driven tests snippets for vim ultisnips
snippet tt "table-driven tests"
func Test${1:Func}(t *testing.T) {
testCases := []struct {
${2:input} ${3:Type}
want ${4:Type}
}{
{$2: ${5:""}, want: ${6:""}},${0: // comment}
}
for _, tc := range testCases {
if got := $1(tc.$2); got != tc.want {
@namsral
namsral / gist:c535fd258ddf94c272c4824244065986
Created November 21, 2016 11:28
output of systemd-cgls
Control group /:
-.slice
├─624 /sbin/cgmanager -m name=systemd
├─machine.slice
│ └─machine-rkt\x2d6ff19b11\x2d0075\x2d48c6\x2d95e1\x2dc632da796d4e.scope
│ ├─init.scope
│ │ └─13469 /usr/lib/systemd/systemd --default-standard-output=tty --log-target=null --show-status=0
│ └─system.slice
│ ├─nginx.service
│ │ ├─13490 nginx: master process /usr/sbin/nginx -g daemon off
@namsral
namsral / gist:1775417
Created February 8, 2012 23:31
Testing AngularJS
// service.js
angular.service('Address', function($resource, $cookies, $xhr, $log) {
// Add Header to comply with Django's CSRF implementation
token = $cookies['csrftoken'];
$xhr.defaults.headers['delete']['X-CSRFToken'] = token;
return $resource('/api/user/address/:addressId', {addressId:'@id'}, {});
});