Skip to content

Instantly share code, notes, and snippets.

View mcrisc's full-sized avatar

Marcelo Criscuolo mcrisc

View GitHub Profile
#!/usr/bin/python
# kills the process holding the TCP port passed as a command line argument
import subprocess
import os, sys
if len(sys.argv) < 2:
print "Missing argument: TCP port"
sys.exit(1)
/**
* Returns the parent directory of a class being run by a web
* container, such as Tomcat. Useful for generating a
* value for iReport's SUBREPORT_DIR parameter.
*
* If using Struts 1.x, call this method this way:
<code>
getRealPath(getServlet().getServletContext(), YourClassName.class);
</code>
*/
def mod10(sequence):
factor = 1
intermediate = ""
for i in range(len(sequence) - 1, -1, -1):
factor = 2 if factor == 1 else 1
intermediate += str(int(sequence[i]) * factor)
remainder = sum([int(d) for d in intermediate]) % 10
return 0 if remainder == 0 else 10 - remainder
def normalizar(nome) {
normalizado = ""
nome.toLowerCase().split().each {parte ->
if (!(parte =~ /^[e]$|^d[a-z]{1,2}$/)) {
parte = parte[0].toUpperCase() + parte.substring(1)
}
normalizado += parte + " "
}
# CVS tag scanner (Ruby)
# collecting all existing tags
tags = []
IO.popen('cvs status -v .') do |p|
while line = p.gets
match = /(\S+)\s+\(revision:.+\)/.match(line)
tags << match[1] if match
end
end
String.prototype.padRight = function(length) {
var padding = "";
for (i = 0; i < length - this.length; i++) {
padding += " ";
}
return this + padding;
}
String.prototype.trim = function() {
def filter(file_name, re_filter)
File.open(file_name) do |f|
f.each_line.with_index {|line, i| yield line, i if line =~ re_filter}
end
end
#re_filter = /HTTP-404/
re_filter = /ora-03113/i
filter('logs/error_log.1271160000', re_filter) do |line, i|
# coding: utf-8
# Python Bitmap (bmp) Image Negation (using module struct)
import struct
with open('in.bmp', 'rb') as fd, open('out.bmp', 'wb') as out:
s = fd.read(1)
count = 1
while s:
if count < 37: # header bytes
#coding: utf-8
# Quicksort in Python
from random import randint
def sort(array):
def partition(array, left, right):
global swaps
# Improving worst case (at the cost of making average case suffer a little!)
#~ p = (left + right) // 2
#coding: utf-8
from time import sleep
print
i = 0
while True:
print "Installing Clipper 5.2...", "|/-\\"[i], "\r",
i = (i + 1) % 4
sleep 0.15