-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmsglib.py
executable file
·77 lines (67 loc) · 2.09 KB
/
msglib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
Python utility functions used in MSG.
"""
from Queue import Queue
from threading import Thread
class bcolors(object):
OKMAGENTA = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARN = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def disable(self):
self.HEADER = ''
self.OKBLUE = ''
self.OKGREEN = ''
self.WARNING = ''
self.FAIL = ''
self.ENDC = ''
def trace(fn):
"""A decorator to time your functions"""
from time import time
import sys
def trace_func(*args, **kwargs):
print fn.__name__ + '...',
sys.stdout.flush()
beg = time()
ret = fn(*args, **kwargs)
tot = time() - beg
print '%.3f' % tot
return ret
return trace_func
def sort_unique(file_path):
"""
open a file, remove duplicates (by first column) keeping last occurance
and sort by first column.
Note: I also tried calling sort -u as an alternative [1] to this function
however when that dedupes it keeps the first occurance of a duplicate
and we want the last. It also seemed slower.
[1]
#sorts only on first column, dedupes only by first column
out = commands.getoutput('sort -ug -o "%s" "%s"' % (file_path, file_path))
"""
lines = open(file_path,'r').readlines()
outfile = open(file_path,'w')
rows = [line.strip().split('\t') for line in lines]
drows = {}
for row in rows:
drows[int(row[0])]='\t'.join(row[1:])
rows = sorted(drows.items())
kv_items = ["\t".join(map(str, kv)) for kv in rows]
outfile.write('\n'.join(kv_items))
outfile.write('\n')
outfile.close()
def get_free_memory():
"""
Try to figure out how much memory is free on a Unix system.
Returns free memory in mB.
"""
data = open("/proc/meminfo", 'rt').readlines()
free = 0
for line in data:
if line.startswith("MemFree") or line.startswith("Buffers") or line.startswith("Cached"):
items = line.split()
free += int(items[1])
#print "free memory is",free/1000,"mB"
return free/1000