Last active
February 1, 2018 16:06
-
-
Save msszczep/a8fa3b5b8f23265ab226ac647ef18971 to your computer and use it in GitHub Desktop.
Python Toolbox files (with some shell and Perl as well)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
text = sys.argv[1] | |
print text |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys | |
file = sys.argv[1] | |
for entry in open(file): | |
i = 1 | |
results = entry.split(',') | |
print results | |
for result in results: | |
print i, results[i-1] | |
i = i + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
more file.txt | awk '{print "\""$0"\","}' | perl -pi -e 's/\n//g' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
text = sys.argv[1] | |
print ",".join(["'" + line[:-1].strip() + "'" for line in open(text)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
reader = csv.reader(open("samples/sample.csv")) | |
for title, year, director in reader: | |
print year, title |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# http://www.pythonforbeginners.com/systems-programming/using-the-csv-module-in-python/ | |
import csv | |
ifile = open('test.csv', "rb") | |
reader = csv.reader(ifile) | |
ofile = open('ttest.csv', "wb") | |
writer = csv.writer(ofile, delimiter='\t', quotechar='"', quoting=csv.QUOTE_ALL) | |
for row in reader: | |
writer.writerow(row) | |
ifile.close() | |
ofile.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mysql_firing(pk): | |
import MySQLdb | |
conn = MySQLdb.connect(host="xxx", db="xxx", user="xxx", passwd="xxx") | |
curs = conn.cursor() | |
sql_str = """select language_id from comments where pk = %s""" % (pk) | |
curs.execute(sql_str) | |
rows = curs.fetchall() | |
curs.close() | |
conn.close() | |
return rows | |
import MySQLdb | |
conn = MySQLdb.connect(host="xxx", db="xxx", user="xxx", passwd="xxx") | |
curs = conn.cursor() | |
file_to_test = 'test.txt' | |
for line in open(file_to_test).readlines(): | |
entry = line.split('\t') | |
comment = entry[1].replace('""', '\"') | |
sql_str = "INSERT INTO my_table (lang, text, uat) VALUES ('%s', %s, %s)" % (entry[0], unescape(comment), entry[2][:-1]) | |
curs.execute(sql_str) | |
conn.commit() | |
break | |
curs.close() | |
conn.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
t = datetime.strptime(end_time, "Run end time: %Y-%m-%d %H:%M:%S.%f\n") | |
now = datetime.now() | |
diff = now - t | |
if diff.seconds > 3600: | |
print 'hello world' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
for x in `ls`; do cd $x; git log > $x-git.log ; echo $x; cd ..; done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_reverse_binary_result(n): | |
return int("{0:b}".format(n)[::-1], 2) | |
if __name__ == "__main__": | |
print get_reverse_binary_result(4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Given a list of numbers, one number per line, transform the list into a single line, with each number | |
# surrounded by single quotes and delimited by commas. | |
more numbers.txt | awk '{print "\""$0"\","}' | sed 's/\"/''/g' | perl -pi -e 's/\n//g' | |
echo '\n' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print [elem for elem in a if not emails_regex.search(elem)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
for line in open(sys.argv[1]): | |
print line[:-1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
final_answer = {} | |
import sys | |
for line in open(sys.argv[1]): | |
v = line[:-1].split() | |
user = v[2] | |
d = datetime.strptime(v[3], "[%d/%b/%Y:%H:%M:%S]") | |
try: | |
if d > final_answer[user]: | |
final_answer[user] = d | |
except: | |
final_answer[user] = d | |
for k, v in sorted(final_answer.iteritems()): | |
print k + "," + str(v) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import sys, re | |
file = sys.argv[1] | |
for entry in open(file): | |
i = 1 | |
results = entry.split('|') | |
for result in results: | |
print i, result | |
i = i + 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
perl -pi -e 's/\n/ /g' <file name> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Source: http://www.fredshack.com/docs/python.html | |
import os, sys, time | |
import apsw | |
print "Using APSW file",apsw.__file__ | |
print "APSW version",apsw.apswversion() | |
print "SQLite version",apsw.sqlitelibversion() | |
if os.path.exists("dbfile"): | |
os.remove("dbfile") | |
connection=apsw.Connection("dbfile") | |
cursor=connection.cursor() | |
cursor.execute("begin") | |
cursor.execute("create table foo(x,y,z)") | |
cursor.execute("insert into foo values(1,2,3)") | |
cursor.execute("insert into foo values(4, 'five', 6.0)") | |
cursor.execute("commit") | |
for row in cursor.execute("select * from foo"): | |
print row | |
for m,n,o in cursor.execute("select x,y,z from foo"): | |
print m,n,o | |
connection.close(True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
t = time.strptime(str(t_str), "%Y-%m-%d %H:%M:%S") | |
secs = time.mktime(t) | |
return time.strftime("%Y-%m-%d%%20%H:%M:%S", time.gmtime(secs)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment