Skip to content

Instantly share code, notes, and snippets.

@CRImier
CRImier / pybssort.py
Last active December 27, 2015 13:09
This Python program is used to manage scanning directories while batch-scanning so that all the files get automatically sorted. Its main objective is to provide path to current chosen directory when called like "pybssort dir" and let users choose, create and manage directories using "pybssort [list|add|del|set]". It requires python-webpy solely …
#!/usr/bin/env python
import os
import sys
import web
web.config.debug = False
from web import database
BASEPATH = "/home/user/Scans"
DIR = "/var/lib/pybssort/"
DB_NAME = os.path.join(DIR, "pybssort.db")
@CRImier
CRImier / firewall
Created March 23, 2014 01:36
This is a Bash script (wrapper around iptables) that creates a simple firewall from one interface to another. It is designed for portable servers that might be getting Internet internet from 3G/wireless/wired connection and, therefore, uplink interface changes, but local does not, so local interface name is hard-coded. It is invoked like "firewa…
#!/bin/bash
#NAT script from !!!!!!!!!!!!!!!, modified by CRImier
# Exit status 0 if operation is correct
# Exit status 1 if trying to use last interface used when running for the first time
# Exit status 2 if interface doesn't exist
EIF=''
IIF='eth0'
PATH=/usr/sbin:/sbin:/bin:/usr/bin
LOGFILE=/etc/nat-if.conf
touch $LOGFILE
@CRImier
CRImier / export.py
Last active October 26, 2021 01:15
This script is for exporting your playlist items to a single folder. Save, change two variables to suit your needs and run =) One use case is: you have a music collection and a playlist containing the best songs of this collection, and you want to load only the best songs to your MP3 player with limited space. This also doesn't overwrite files, …
import os
import shutil
directory_name = "playlist/"
playlist_name = "playlist.m3u"
f = open(playlist_name, "r")
files = []
for line in f:
line = line.strip().strip("\n").strip("\r")
@CRImier
CRImier / mcp28003.ino
Created May 13, 2015 23:03
This Arduino script is used to communicate to HD44780 LCD attached to WIDE.HK small I2C LCD backpack using MCP23008 IO expander. It's been taken from http://playground.arduino.cc/Code/I2CPortExpanderAndLCDs , seemingly modified by WIDE.HK and then finished by me. I had to concatenate it all in one script (stupid Arduino IDE cannot find .h files …
#include <Wire.h>
class LCDI2C4Bit {
public:
LCDI2C4Bit(int devI2CAddress, int num_lines, int lcdwidth);
void commandWrite(int command);
void init();
void print(int value);
void printIn(char value[]);
void clear();
import smbus
from time import sleep
class ButtonPanel():
previous_data = 0; #Storage variable so that we can compare current and previous states
def __init__(self, addr = 0x27, bus = 1, int_pin = None):
self.bus_num = bus
self.bus = smbus.SMBus(self.bus_num)
self.addr = addr
@CRImier
CRImier / parser.py
Created April 10, 2018 21:19
Swedbank Latvia CSV parser script, creates a nice dictionary
import csv
f = open("statement.csv", "r", encoding="iso8859_13")
#def reencode(file):
# for line in file:
# yield line.decode('iso8859_13').encode('utf-8')
r = csv.reader(f, delimiter=";", quotechar='"')
l = [line for line in r]
# This code is available on 192.168.88. :8000 (you can download it using your web browser)
# All examples are written to work on Python 3
#########################################################
# Basic syntax, "while" and "if"
#########################################################
# REPL works well as a simple calculator
2+2*2
# Answer: 6
@CRImier
CRImier / smbus_eeprom_16bit.py
Last active September 8, 2023 19:01
Read and write 8-bit and 16-bit EEPROM using Python's smbus and smbus2 libraries respectively
"""This code works with both 24c02 and 24c16 (though you'll want to also iterate through addresses for using a 24c16 fully):"""
from smbus import SMBus
from math import ceil
def write_to_eeprom(bus, address, data, bs=16):
"""
Writes to an EEPROM. Only supports starting from 0x00, for now.
(to add other start addresses, you'll want to improve the block splitting mechanism)
Will raise an IOError with e.errno=121 if the EEPROM is write-protected.
@CRImier
CRImier / hexdump.py
Created August 24, 2021 17:21
i2cdetect-style hexdump in Python
def hexdump(data=None, col_width=16):
#This function follows the i2cdump output format, as it's the most readable out there
if data is None: data = d
# character conversion function
conv = lambda x: chr(x) if x in range(0x20, 0x7f) else '.'
# getting row count
row_count = ceil(len(data)/col_width)
max_row_num_len_hex = len(hex(row_count)[2:])
# first line - header
hex_digits = [hex(i)[2:] for i in range(16)]
@CRImier
CRImier / listen_keys.py
Created November 16, 2021 04:59
Software that shows you all input events. apt install python3-evdev .
#!/usr/bin/sudo /usr/bin/python3
from evdev import InputDevice, list_devices, categorize, ecodes
from time import sleep
import threading
def listen_for_events(dev):
for event in dev.read_loop():
#if event.type == ecodes.EV_KEY:
print (dev.name, ": ", str(categorize(event)))