Skip to content

Instantly share code, notes, and snippets.

View mirekfranc's full-sized avatar
🌚
Rien

Miroslav Franc mirekfranc

🌚
Rien
  • Prague, Czech Republic
View GitHub Profile
@mirekfranc
mirekfranc / goto_and_destructors.cpp
Created April 24, 2015 21:53
goto before construction causes destruction; guaranteed by the C++ standard
#include <iostream>
class A
{
public:
A () { std::cout << "a"; }
~A () { std::cout << "A"; }
};
int i = 1;
@mirekfranc
mirekfranc / breakpoint.py
Created April 24, 2015 22:00
how to process gdb breakpoints in python
# https://sourceware.org/gdb/onlinedocs/gdb/Breakpoints-In-Python.html
# https://sourceware.org/gdb/onlinedocs/gdb/GDB_002fMI-Breakpoint-Commands.html
def my_breakpoint_handler (event):
if (isinstance(event, gdb.BreakpointEvent) and (event.breakpoint.location == "g")):
print event.breakpoint.hit_count
gdb.events.stop.connect(my_breakpoint_handler)
gdb.execute("start")
gdb.execute("break g")
@mirekfranc
mirekfranc / cap_perm_restore.sh
Created April 27, 2015 06:48
How to restore file based capabilities and file permissions from rpm
# capabilities
rpm2cpio ../iputils-20121221-5.fc20.x86_64.rpm | cpio -ivd
rpm -qp --filecaps ../iputils-20121221-5.fc20.x86_64.rpm | grep '=' | awk '{ print "[ -L " $1 " ] || " "setcap " $3 " " "." $1; }' | bash
# permissions
rpm2cpio ../sudo-1.8.8-1.fc20.x86_64.rpm | cpio -ivd
rpm -qp --qf '[\[ -L %{FILENAMES:shescape} \] || chmod %7{FILEMODES:octal} %{FILENAMES:shescape}\n]' ../sudo-1.8.8-1.fc20.x86_64.rpm | sed -e "s|chmod ...|chmod |" -e "s|'/|'./|g" | bash
@mirekfranc
mirekfranc / profile-and-debug.py
Last active August 29, 2015 14:20
How to profile and debug python script
##################################################
# profiler:
# https://docs.python.org/2/library/profile.html
# python -mcProfile -o profiler.out ./something.py
### profile.py ###
import pstats
p = pstats.Stats('profiler.out')
p.sort_stats('cumtime').print_stats(20)
@mirekfranc
mirekfranc / default_arguments.py
Last active August 29, 2015 14:20
default arguments evaluated once at function definition...
>>> def a(x=False, y=[]):
... if not x:
... y.append (5)
... else:
... print y
...
>>> a()
>>> a()
>>> a()
>>> a()
@mirekfranc
mirekfranc / files_changed_in_git.sh
Created May 6, 2015 12:56
What files changed between some ancient commit and now...
git diff --name-status HEAD~30..HEAD
@mirekfranc
mirekfranc / problem_05_python_vs_pypy
Created May 7, 2015 01:41
the power of just in time...
$ cat problem_05.py
n = 2520
while True:
for i in xrange (20, 0, -1):
if n % i != 0:
break
else:
print (n)
break
#include <stdio.h>
#include <stdlib.h>
static int
factorial_helper (int n, int a)
{
start:
a *= n;
if (n-- > 1)
@mirekfranc
mirekfranc / property_setter.py
Created May 8, 2015 21:57
python class with an attribute that no longer exists...
class Nonsense(object):
def __init__(self, n):
self.a = ['x'] * n
@property
def length(self):
return len(self.a)
@length.setter
def length(self, n):
@mirekfranc
mirekfranc / static_methods_and_alternative_constructors.py
Created May 8, 2015 22:17
Alternative constructors and static methods in python...
class MyList(list):
# alternative constructor
@classmethod
def multiple_of(cls, n, c):
return cls(c * n)
# some random method bundled in the class
@staticmethod
def print_with_numbers(a):
for n, e in enumerate(a):