Skip to content

Instantly share code, notes, and snippets.

View dbrgn's full-sized avatar

Danilo Bargen dbrgn

View GitHub Profile
@dbrgn
dbrgn / numbering_mode.md
Last active February 9, 2024 19:55
RPLCD numbering mode notes

In RPLCD version 1.0, some APIs were slightly changed. Previously the CharLCD instance would provide default values for the pin numbers and the numbering mode. But that was changed, since it may be dangerous if these pins are connected to other hardware.

So if you have installed version 1.0 of RPLCD, you need to provide the pin numbering mode yourself. If your code previously looked like this:

from RPLCD import CharLCD
lcd = CharLCD(cols=16, rows=2, pin_rs=37, pin_e=35, pins_data=[40, 38, 36, 32, 33, 31, 29, 23])

...and if you use the BOARD numbering scheme, then change the code like this:

@dbrgn
dbrgn / rst.video.py
Created June 13, 2012 07:56
ReStructuredText Youtube / Vimeo video embed directive
# -*- coding: utf-8 -*-
"""
ReST directive for embedding Youtube and Vimeo videos.
There are two directives added: ``youtube`` and ``vimeo``. The only
argument is the video id of the video to include.
Both directives have three optional arguments: ``height``, ``width``
and ``align``. Default height is 281 and default width is 500.
@dbrgn
dbrgn / state.rs
Created April 3, 2017 14:49
Custom Text based Diesel type for an enum
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(rename_all="snake_case")]
pub enum State {
Pending,
Sending,
Sent,
Failed,
}
impl fmt::Display for State {
use std::sync::Arc;
use tokio::sync::Semaphore;
/// An async latch.
///
/// Internally this is implemented using a semaphore with an initial value of 0.
/// The semaphore can be closed to "trigger" the latch. A "Closed" error when
/// awaiting the semaphore is treated as if the latch is unlocked.
#[derive(Clone)]
@dbrgn
dbrgn / create_django_session.py
Last active October 31, 2022 15:20
Manually create a Django session
from django.contrib import auth
from django.contrib.sessions.backends.db import SessionStore
session = SessionStore(None)
session.clear()
session.cycle_key()
session[auth.SESSION_KEY] = user._meta.pk.value_to_string(user)
session[auth.BACKEND_SESSION_KEY] = 'django.contrib.auth.backends.ModelBackend'
session[auth.HASH_SESSION_KEY] = user.get_session_auth_hash()
session.save()
def get_free_port():
s = socket.socket()
s.bind(('', 0))
port = s.getsockname()[1]
s.close()
return port
@dbrgn
dbrgn / boyer-moore.py
Created August 18, 2011 12:55
Boyer-Moore-Algorithm in Python
class last_occurrence(object):
"""Last occurrence functor."""
def __init__(self, pattern, alphabet):
"""Generate a dictionary with the last occurrence of each alphabet
letter inside the pattern.
Note: This function uses str.rfind, which already is a pattern
matching algorithm. There are more 'basic' ways to generate this
dictionary."""
@dbrgn
dbrgn / gist:cf30e2524ea21d80c853
Last active December 11, 2021 05:08
PostgreSQL Backup Script
#!/bin/bash
TIMESTAMP=$(date +\%Y\%m\%d_\%H\%M\%S)
DEST=/var/backups/db/
OWNER=backuppc
GROUP=backuppc
echo -n "Creating temp dir..."
TMPDIR=$(mktemp -d)
echo " Done."
@dbrgn
dbrgn / scan.sh
Last active October 9, 2021 15:20
Determine whether subdirectories containing git repos are dirty or diverged
#!/bin/bash
function git_is_dirty {
[[ $(git diff --shortstat 2> /dev/null | tail -n1) != "" ]] && echo "yes"
}
function git_is_diverged {
LOCAL=$(git rev-parse --abbrev-ref HEAD 2> /dev/null || echo "none")
REMOTE=$(git rev-parse --abbrev-ref --symbolic-full-name @{u} 2> /dev/null || echo "none")
if [[ "$LOCAL" == "none" ]]; then # No commits yet
@dbrgn
dbrgn / in.ts
Last active September 7, 2021 07:58
TypeScript + async / await + throw
async function foo() {
await sleep(1000);
console.log(1);
await sleep(1000);
console.log(2);
console.log("throwing now");
if (1 == 1) {
throw "oh shitshit";
}
await sleep(1000);