Wednesday, February 10, 2016
Gmail filter for old emails
(category:forums OR category:social OR category:promotions OR category:offers) older_than:30d label:unread
Wednesday, January 11, 2012
Merging content of several sorted files
#!/bin/env python
# This script merges metrics output from different runs
# Does a merge sort of the files and groups together equal keys
import sys, os, time
from heapq import heappush, heappop
DELIM = ", "
END_LINE = "\n"
BLANK = "-"
# Override this method for splitting input time to key and value
def parse_input(line):
parts = map(lambda x : x.strip(), line.split("\t"))
key = " ".join(parts[0:8])
value = parts[8]
return (key, value)
def pop_record(f, split_func):
if f:
try:
line = f.readline()
return split_func(line)
except:
print 'Error:', line, parts, f
return None
else:
return None
def generate_header(files):
fout = sys.stdout
header = []
for fname in files:
header.append(fname.split("/")[-1])
fout.write("KEY" + DELIM)
index = 0
for s in header:
if index != 0:
fout.write(",")
fout.write(s)
index += 1
fout.write(END_LINE)
def merge_datasets(files):
n_count = 0
flist = []
index = 0
for fname in files:
f = open(fname)
flist.append(f)
index += 1
fout = sys.stdout
#Initialize heap
index = 0; heap = []
for f in flist:
(key, value) = pop_record(f, parse_input)
heappush(heap, (n_count, key, index, value, f))
n_count += 1
index += 1
nfiles = index
#Iterate through values
while heap:
tup = []
for i in xrange(nfiles):
if heap:
(_, key, index, value, f) = heap[0]
if index == i:
out_key = key
tup.append(value)
heappop(heap)
(key, value) = pop_record(f, parse_input)
if f:
heappush(heap, (n_count, key, index, value, f))
n_count += 1
else:
tup.append(BLANK)
else:
tup.append(BLANK)
fout.write(out_key)
for t in tup:
fout.write(DELIM + t)
fout.write(END_LINE)
for f in flist:
f.close()
if __name__ == "__main__":
files = sys.argv[1:]
if files:
generate_header(files)
merge_datasets(files)
else:
print "Usage: " + sys.argv[0] + " {list of file names to compare}"
Sunday, May 08, 2011
Formatting output from `date' [Shell]
Formatting output from `date'
Output:
Also
date "+DATE: %Y-%m-%d%nTIME: %H:%M:%S"
Output:
DATE: 2011-05-08
TIME: 09:52:21
Also
echo `date "+%Y-%-m-%-d" -d2012-02-01`
2012-2-1
Tuesday, April 26, 2011
Writing plugins/modules for MySQL Workbench
The example below copies first five rows of the current resultset to the editor below the selected text (goes to the top if no text is selected).
from wb import *
import grt
ModuleInfo = DefineModule(name= "MyModule", author= "My Name", version="1.0")
def generate_script(editor):
from string import strip
rs = editor.activeResultset
buffer = "\n"
if rs:
rows = min(5, rs.rowCount)
cols = len(rs.columns)
rs.goToFirstRow()
buffer += "-- Ans. "
for j in xrange(cols):
if j < cols - 1:
buffer += rs.columns[j].name + ", "
else:
buffer += rs.columns[j].name + "\n"
for i in xrange(rows):
buffer += '-- '
for j in xrange(cols):
if j < cols - 1:
buffer += strip(rs.stringFieldValue(j)) + ", "
else:
buffer += strip(rs.stringFieldValue(j)) + "\n"
rs.nextRow()
buffer += "\n"
return buffer
# because of a bug in the wbinputs.currentSQLEditor() input specifier from the wb module
# in Workbench 5.2.26, we include our own version of it here
def currentSQLEditor():
arg= grt.classes.app_PluginObjectInput()
arg.name= "activeSQLEditor"
arg.objectStructName= "db.query.Editor"
return arg
@ModuleInfo.plugin("my.plugin.fill_random_query", caption= "Copy Results to Editor", input=[currentSQLEditor()], pluginMenu="SQL/Utilities")
@ModuleInfo.export(grt.INT, grt.classes.db_query_Editor)
def fill_random_query(editor):
active_buffer = editor.activeQueryBuffer
script = active_buffer.script
try:
buffer = generate_script(editor)
except Exception, exc:
buffer = "\nError: %s\n" % exc
pos = active_buffer.selectionEnd
new_script = script[:pos] + buffer + script[pos:]
active_buffer.replaceContents(new_script)
return 0
Thursday, April 21, 2011
Eclipse Projects - Many types in one
How to make an Eclipse project be both PyDev and Java project?
File: .project
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>Project-Name</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.python.pydev.pythonNature</nature>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
File: .classpath
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="java-src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="output" path="classes"/>
</classpath>
File: .pydevproject
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?>
<pydev_project>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/Project-Name/src</path>
</pydev_pathproperty>
</pydev_project>
Subscribe to:
Comments (Atom)