Skip to content

Instantly share code, notes, and snippets.

View kissgyorgy's full-sized avatar

György Kiss kissgyorgy

View GitHub Profile
@kissgyorgy
kissgyorgy / redmine.conf
Created September 4, 2013 21:40
Redmine upstart script
description "Redmine with standalone Passenger"
author "Walkman <[email protected]>"
start on started-postgresql
stop on stopped-postgresql
expect fork
respawn
@kissgyorgy
kissgyorgy / timer.py
Created August 30, 2013 12:21
Python: timing context manager
# Blog post: http://www.huyng.com/posts/python-performance-analysis/
import time
class Timer(object):
def __init__(self, verbose=False):
self.verbose = verbose
def __enter__(self):
self.start = time.time()
@kissgyorgy
kissgyorgy / tmux_takeover.sh
Created August 27, 2013 00:52
tmux: take over session from others
# http://stackoverflow.com/a/7819465/720077
takeover() {
# create a temporary session that displays the "how to go back" message
tmp='takeover temp session'
if ! tmux has-session -t "$tmp"; then
tmux new-session -d -s "$tmp"
tmux set-option -t "$tmp" set-remain-on-exit on
tmux new-window -kt "$tmp":0 \
'echo "Use Prefix + L (i.e. ^B L) to return to session."'
@kissgyorgy
kissgyorgy / list_foreign_keys.sql
Created August 20, 2013 10:32
SQL: List foreign keys in a given table in PostgreSQL
-- http://bryan-murdock.blogspot.hu/2013/04/list-foreign-keys-in-your-postgresql.html
select confrelid::regclass, af.attname as fcol,
conrelid::regclass, a.attname as col
from pg_attribute af, pg_attribute a,
(select conrelid,confrelid,conkey[i] as conkey, confkey[i] as confkey
from (select conrelid,confrelid,conkey,confkey,
generate_series(1,array_upper(conkey,1)) as i
from pg_constraint where contype = 'f') ss) ss2
where af.attnum = confkey and af.attrelid = confrelid and
@kissgyorgy
kissgyorgy / get_next_primary_key.py
Last active January 11, 2022 09:49
Django: Get next primary key for object.
from djangobb_forum.models import Post
from django.db.models import Max
# id__max is None if there are no Posts in the database
id_max = Post.objects.all().aggregate(Max('id'))['id__max']
id_next = id_max + 1 if id_max else 1
@kissgyorgy
kissgyorgy / filename_from_url.py
Created July 29, 2013 08:13
Python: Get file name from url
# http://stackoverflow.com/a/300533/720077
import urllib2
file_name = urllib2.unquote(url).decode('utf8').split('/')[-1]
@kissgyorgy
kissgyorgy / download_file.py
Last active August 24, 2024 11:48
Python: Download file, get file size and Content type.
import requests
r = requests.get("http://download.thinkbroadband.com/10MB.zip",
cookies={'cookie1': 'working'}
)
open('10MB.zip', 'wb').write(r.content)
@kissgyorgy
kissgyorgy / urllib2_cookies.py
Created July 29, 2013 08:03
Python: Add cookies to a request with urllib2
# see: http://stackoverflow.com/a/3334959/720077
# if there are more cookies, they should go all in one "Cookie" header in this format:
# "cookie1name=cookie1value;cookie2name=cookie2value;"
import urllib2
opener = urllib2.build_opener()
opener.addheaders.append(('Cookie', 'cookiename=cookievalue'))
f = opener.open("http://example.com/")
@kissgyorgy
kissgyorgy / TableWidgetCenteredItem.py
Created July 18, 2013 13:55
PyQt: Read only centered cells in QTableWidget.
from PyQt4.QtGui import QTableWidgetItem
from PyQt4.QtCore import Qt
class TableWidgetCenteredItem(QTableWidgetItem):
""" Read only centered cells in QTableWidget. """
def __init__(self, *args, **kwargs):
super(TableWidgetCenteredItem, self).__init__(*args, **kwargs)
self.setTextAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
self.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
@kissgyorgy
kissgyorgy / generate_id.py
Created June 8, 2013 08:06
Python: Generate random string
# http://stackoverflow.com/questions/2257441/python-random-string-generation-with-upper-case-letters-and-digits/2257449#2257449
def id_generator(size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for x in range(size))