Skip to content

Instantly share code, notes, and snippets.

View turicas's full-sized avatar

Álvaro Justen turicas

View GitHub Profile
@turicas
turicas / function_name.py
Created December 19, 2011 16:51
[Python] How to know function name inside it
#!/usr/bin/env python
import inspect
def some_function():
frame = inspect.currentframe()
function_name = frame.f_code.co_name
print 'This function name is:', function_name
@turicas
turicas / attrdict.py
Created December 22, 2011 16:21
Python class with dict-like get/set item
#!/usr/bin/env python
# coding: utf-8
class AttrDict(object):
def __init__(self, init=None):
if init is not None:
self.__dict__.update(init)
def __getitem__(self, key):
return self.__dict__[key]
@turicas
turicas / matplotlib-bug-bar-log.py
Created January 24, 2012 01:52
Bug on matplotlib bar plot when using 'log' in yscale
#!/usr/bin/env python
# coding: utf-8
from numpy import arange
from matplotlib.pyplot import figure
f = figure()
s = f.add_subplot(1, 1, 1)
x = arange(0, 20)
@turicas
turicas / install-needed-packages.sh
Created February 16, 2012 01:08
Simple installation of must-have Debian packages on any of my machines
#!/bin/bash
# This scripts installs my must-have packages on Debian/Ubuntu servers
# Remember to delete all CD/DVD entries on /etc/apt/sources.list
# before running this scripts (or you'll need to have it in the case to
# install some packages)
# By Álvaro Justen <https://github.com/turicas>
# Preparing environment...
apt-get -y install aptitude
@turicas
turicas / vncserver.sh
Created March 21, 2012 18:24
Starts a VNC server, automatically configuring password and display options
#!/bin/bash
# Options:
PASSWORD="mysecret"
DISPLAY=":10"
VNCSERVER_OPTIONS="-geometry 1024x768 -alwaysshared"
PASSWD_PATH="$HOME/.vnc/passwd"
XSTARTUP_PATH="$HOME/.vnc/xstartup"
VNCSERVER="tightvncserver"
VNCPASSWD="tightvncpasswd"
@turicas
turicas / mdb2sql.sh
Created May 4, 2012 07:29
Convert a MDB (Access) file to SQL
#!/bin/bash
# Convert a MDB file (Access) to SQL
# Needs mdbtools[http://mdbtools.sourceforge.net/]
# run 'aptitude install mdbtools' on Debian/Ubuntu
# Created by Álvaro Justen <https://github.com/turicas>
# License: GPLv2
mdb=$1
sql=$2

This bug is really bizarre: I'm doing some operations inside a class and received a "core dumped" (the process is killed) when trying to destroy the context. But when I do the same operations outside the class, the error simply does not happen!

I can reproduce the bug with two scripts:

  • server.py: creates a context, creates and binds to one zmq.REP socket and start a main loop in which it receives and answers requests (using recv_json and send_json). When receives SIGINT (KeyboardInterrupt), destroy the context and exit.
  • client.py: creates a context, starts a process to run server.py (using subprocess.Popen), connects to server's socket, send/recv a JSON, destroys its context and finishes server's process sending a SIGINT.

I've implemented another version of client.py without using a class (client_without_class.py) but doing the same things and the error simply does not happen! The output for the two implementations is shown below:

$ python client_without_class.py 

Cr

@turicas
turicas / monitoring.py
Created June 7, 2012 19:46
Monitoring information about host/OS and some processes (used at PyPLN's broker)
#!/usr/bin/env python
# coding: utf-8
import socket
from time import time
import psutil
def get_outgoing_ip((host, port)):
"""Connect to remote host/port, return local IP used by OS"""
@turicas
turicas / virtualenv_execute.py
Created June 8, 2012 19:23
Execute Python code in a virtualenv, return its stdout and stderr
#!/usr/bin/env python
# coding: utf-8
import os
import shlex
from subprocess import Popen, PIPE
def execute_in_virtualenv(virtualenv_name, commands):
'''Execute Python code in a virtualenv, return its stdout and stderr.'''
@turicas
turicas / zmqbug.py
Created August 13, 2012 20:08
[FIXED (not a bug)] Bug on pyzmq when trying to delete context
#!/usr/bin/env python
# coding: utf-8
# This is not a bug. Just need to set linger: socket.linger = 0
# You need to run it without a socket listening on *:5555 to reproduce the bug
# Maybe it's related to https://github.com/zeromq/pyzmq/issues/211 and https://github.com/zeromq/pyzmq/issues/212
import zmq
def zmq_bug(host_port, data, timeout=1):