-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.py
47 lines (39 loc) · 1.02 KB
/
utils.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
import bz2
import sys
import gzip
def openr(fn, mode="r"):
if fn is None:
return sys.stdin
if fn.endswith(".bz2"):
if sys.version_info[0] < 3:
return bz2.BZ2File(fn)
else:
return bz2.open(fn, 'rt')
elif fn.endswith(".gz"):
if sys.version_info[0] < 3:
return None # need to check if gzip is different in Python2
else:
return gzip.open(fn, 'rt')
else:
return open(fn, mode)
def openw(fn):
if fn is None:
return sys.stdout
if fn.endswith(".bz2"):
if sys.version_info[0] < 3:
return bz2.BZ2File(fn, "w")
else:
return bz2.open(fn, 'wt')
elif fn.endswith(".gz"):
if sys.version_info[0] < 3:
return None # need to check if gzip is different in Python2
else:
return gzip.open(fn, 'wt')
else:
return open(fn, "w")
def is_number(s):
try:
int(s)
return True
except ValueError:
return False