Skip to content

Instantly share code, notes, and snippets.

@anddam
anddam / xsrf.interceptor.ts
Last active April 11, 2024 20:43
Angular HttpClientXsrfModule example
import { Injectable } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpXsrfTokenExtractor } from '@angular/common/http';
import { Observable } from 'rxjs';
@Injectable()
export class HttpXSRFInterceptor implements HttpInterceptor {
constructor(private tokenExtractor: HttpXsrfTokenExtractor) {
}
# /etc/udev/rules.d/80-keychron.rules
SUBSYSTEMS=="input", ATTRS{name}=="Keychron K2", RUN+="/usr/bin/touch /tmp/FOUNDKEYBOARD; /bin/echo 2 | /usr/bin/tee /sys/module/hid_apple/parameters/fnmode"
@anddam
anddam / get_address_from_ripemd160.py
Created February 14, 2014 08:46
Get bitcoin address from RIPEMD-160 hash in python
from hashlib import sha256
from base58 import b58encode
def get_address_from_ripemd160(ripemd_hash):
# steps from https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses
h3 = ripemd_hash
h4 = '00' + h3
h5 = sha256(h4.decode('hex')).hexdigest()
h6 = sha256(h5.decode('hex')).hexdigest()
@anddam
anddam / results
Last active December 9, 2022 14:45 — forked from anonymous/untitled.py
SQLAlchemy aggregate functions example
In [29]: my_sub = db.session.query(TestModel.name, func.count(TestModel.id).label('count')).group_by(TestModel.name).subquery()
In [30]: db.session.query(TestModel, my_sub.c.count).outerjoin(my_sub, TestModel.name==my_sub.c.name).all()
Out[30]: [(<1: Bob>, 1), (<2: Alice>, 2), (<3: Alice>, 2)]
@anddam
anddam / sample.py
Last active April 26, 2022 17:14
split, strip and get unique tokens from a string
def get_flags(key, sep=" "):
return list({stripped for item in get_config_var(key).split(sep) if (stripped := item.strip())})
@anddam
anddam / astylerc
Created March 30, 2022 12:02
astyle configuration
style=google
add-braces
indent-namespaces
pad-oper
pad-header
break-blocks
delete-empty-lines
indent-switches
# Objective-C
align-method-colon
@anddam
anddam / gist:7276579
Last active December 6, 2021 06:02
Programmatically dispatch KeyEvent
KeyEvent powerKeyUp = new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_POWER);
KeyEvent powerKeyDown = new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_POWER);
powerKeyDown = powerKeyDown.changeTimeRepeat(powerKeyDown, SystemClock.uptimeMillis() - 100, 1);
dispatchKeyEvent(powerKeyDown);
if (dispatchKeyEvent(powerKeyUp))
Log.d("PML", "The event was consumed in dispatchKeyEvent() ACTION_UP");
else
Log.d("PML", "The event was NOT consumed in dispatchKeyEvent() ACTION_UP");
@anddam
anddam / logger.cpp
Last active November 29, 2021 20:14
C++ logger class excerpt
Logger * Logger::m_Instance = 0;
Logger::Logger() {
char* buf = nullptr;
size_t sz = 0, l = 0;
// No `m_Instance` declared here
Logger * Logger::getInstance() throw () {
if (m_Instance == 0) {
m_Instance = new Logger();
@anddam
anddam / gist:2ccf9f227e0ae817919c15c66c98c10b
Created August 31, 2021 22:45
virt-manager error saving
Error saving domain: operation failed: domain save job: unexpectedly failed
Traceback (most recent call last):
File "/usr/share/virt-manager/virtManager/asyncjob.py", line 65, in cb_wrapper
callback(asyncjob, *args, **kwargs)
File "/usr/share/virt-manager/virtManager/vmmenu.py", line 182, in cb
vm.save(meter=asyncjob.get_meter())
File "/usr/share/virt-manager/virtManager/object/libvirtobject.py", line 57, in newfn
ret = fn(self, *args, **kwargs)
File "/usr/share/virt-manager/virtManager/object/domain.py", line 1377, in save
@anddam
anddam / sample.py
Created November 8, 2021 22:20
PySide Qt issue with shifted chars in string when placing a TableWidgetItem in a TableWidget
from PySide2.QtCore import Qt
from PySide2.QtWidgets import QMainWindow, QTableWidgetItem
class CMTableWidgetItemNonEditable(QTableWidgetItem):
"""Unset editing flags
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)