Skip to content

Instantly share code, notes, and snippets.

@Makistos
Makistos / validate.py
Last active April 20, 2017 10:02
Check if a string only has valid words. #python
import sys
valid_words = ('opsub', 'opequal', 'opadd', 'opmult', 'oprdiv', 'opidiv')
def is_valid(str):
return all(word in valid_words for word in str.split())
not_validated = sys.argv[1]
if is_valid(not_validated):
@Makistos
Makistos / rec_list_files.c
Created April 5, 2017 12:48
Recursively list files in C. #c #file-handling #recursive
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <libgen.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
static const char short_options [] = "d:";
@Makistos
Makistos / parse_files.py
Created February 1, 2017 14:34
Read files recursively. Useful for e.g. command files. #python #file-handling
def parse_test_file(filename):
retval = []
with open(filename) as f:
lines = f.read().splitlines()
path, script = os.path.split(os.path.abspath(filename))
cwd = os.getcwd()
if cwd != path:
os.chdir(path)
for line in lines:
@Makistos
Makistos / interrupt.py
Created February 1, 2017 14:24
How to interrupt a long-running operation in Python. #python
try:
// Something really long and boring
except (KeyboardInterrupt, SystemExit):
// Get me out of here
exit()
@Makistos
Makistos / subprocess-with-eh.py
Created February 1, 2017 14:21
Run commands in shell with error handling. #python #subprocess
#!/usr/bin/python
def tryUrl(url):
try:
# check_output returns output from command so it could be printed here
subprocess.check_output('ping ' + url, shell=True)
return 1
except subprocess.CalledProcessError, e:
print 'Connection failed, error: %s' % e.output
return 0
@Makistos
Makistos / sqlite3.py
Last active January 23, 2017 11:30
Sqlite3 with Python. #python #sqlite3 #database
import sqlite3
DB_FILE = 'database.db'
def db_init():
"""
Initialize the sqlite3 database if it doesn't exist or the tables are
missing..
:return: n/a
"""
@Makistos
Makistos / cleanlog.pl
Created December 15, 2016 08:07
Remove timestamps and process numbers from Android (both kernel and logcat). This makes it easier to compare logs in e.g. Beyond Compare. #perl #android
#!/usr/bin/perl
# Clean up Android logs so that they are easier to compare.
while(<>)
{
if (m/^\[/) {
# Captured from UART
if (m/^\[.+\]\s\d\d-\d\d/) {
# Logcat
@Makistos
Makistos / hidden_word.py
Last active December 15, 2016 08:10
Given a word, check which lines of text in a file contain all the letters in that word. Takes two parameters: file name with the lines of text and the letters to search. #python #hidden-word #list-comprehension
# Slurp file into list
with open(sys.argv[1]) as f:
names = f.read().splitlines()
to_search = sys.argv[2].lower()
answer = [x for x in names if all(True if to_search.count(item) <= x.lower().count(item) else False for item in set(to_search))]
print answer
@Makistos
Makistos / checkpatch.sh
Created December 14, 2016 12:10
Run checkpatch on latest kernel change. Run in kernel root. #bash #linux
git diff HEAD~1..HEAD | perl scripts/checkpatch.pl --no-signoff --ignore NONBLANK_AFTER_SUMMARY -
@Makistos
Makistos / cmd-args.py
Created October 7, 2016 13:30
Argparse example. #python #argparse #command-line
#!/usr/bin/python
import sys
import argparse
import pprint
def read_params(args):
p = argparse.ArgumentParser(epilog='TAG is required for db operations '
'but can also be used without them. In that case files are named using '
'it. It can also be omitted in which case results are only written to '