Skip to content

Instantly share code, notes, and snippets.

View sergray's full-sized avatar

Sergey Panfilov sergray

View GitHub Profile
#!/usr/bin/env python
"""Describe EC2 instances bound to DNS name. Lookup CNAME of DNS,
which can point to ELB or EC2 instance. Get info from AWS API.
DNS names should be passed as arguments to the script.
Valid AWS API credentials must be configured, see
http://docs.pythonboto.org/en/latest/getting_started.html#configuring-boto-credentials
"""
from __future__ import print_function
@sergray
sergray / rrdenergy.py
Created May 26, 2014 02:45
Store energy consumption in RRD files
#!/usr/bin/env python
from datetime import datetime
from subprocess import call
from mercury206 import commands, communications, config
def update_rrd(path, values):
#!/bin/bash
rrdtool graph power.png --vertical-label=W*h --font=DEFAULT:11:Arial -w 900 -h 600 \
DEF:apower=pcv.rrd:power:AVERAGE CDEF:whpower=apower,1000,\* LINE2:whpower#FF0000
@sergray
sergray / gico.py
Last active August 29, 2015 14:09
Python command gico checking out the latest version of git branch by name substring
#!/usr/bin/env python
"""Checkout the latest version of git branch by name substring"""
from __future__ import print_function
import sys
from subprocess import check_output
@sergray
sergray / gist:a7cf81e2061b0b591e8a
Last active August 29, 2015 14:19
Save commits from multiple GitHub pull requests into text files just in one bash line (requires Python flatdict)
$ for p in NUM1 NUM2; do curl -H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token OAUTH-TOKEN" \
https://api.github.com/repos/:ORG:/:REPO:/pulls/$p/commits | \
python -c "from flatdict import FlatDict; import json, csv, sys; rows = json.load(sys.stdin); \
writer = csv.DictWriter(sys.stdout, [u'commit:author:name', u'commit:author:date', u'commit:tree:sha', u'commit:message'], extrasaction='ignore', delimiter='\t'); writer.writerows(FlatDict(row) for row in rows)" \
> GH-PR-$p.txt; done
@sergray
sergray / gist:649788
Created October 27, 2010 19:39
legacy antigravity
>>> import antigravity
>>> antigravity.start()
'http://xkcd.com/353/'
# the source of http://pypi.python.org/pypi/antigravity/0.1
# is plain and there is no any magic of Python
#!/usr/bin/env python
STRIP_URL = "http://xkcd.com/353/"
@sergray
sergray / gist:649833
Created October 27, 2010 20:00
proposed antigravity
__doc__ = "Add fly function to namespace where module is imported"
class RealWorldLevitation(Exception):
pass
def expose(obj):
import inspect
frame = inspect.currentframe()
frame.f_back.f_back.f_globals[obj.__name__] = obj
return obj
@sergray
sergray / remem.py
Created January 17, 2011 17:59
Extend arbitrary interactive unix command with readline features
import sys
import subprocess
import readline
import select
import os
class Popen(subprocess.Popen):
def _communicate(self, input):
read_set = []
@sergray
sergray / gist:786384
Created January 19, 2011 16:17
Amazing Django ORM extra SQL for Postgres
>>> tasks = Task.objects.\
... extra(select = {'t': "date_trunc('minute', task_task.date_created)"}).\
... values('t').annotate(Count('id')).order_by('t')
>>> print "\n".join("%(t)s | %(id__count)d" % t for t in tasks)
2011-01-19 11:26:00 | 767
2011-01-19 11:27:00 | 613
2011-01-19 11:28:00 | 522
2011-01-19 11:29:00 | 721
2011-01-19 11:30:00 | 701
2011-01-19 11:31:00 | 679
@sergray
sergray / .vimrc
Created February 4, 2011 16:41
add tree of directories to vim path
python << END_PATH
import os
import vim
walker = os.walk(os.getcwd())
dir_list = ['.']
for root, dirs, paths in walker:
dir_list += map(lambda d: os.path.join(root, d), dirs)
vim.command('set path=%s' % ','.join(dir_list))
END_PATH