forked from glamp/bashplotlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
110 lines (95 loc) · 3.11 KB
/
helpers.py
File metadata and controls
110 lines (95 loc) · 3.11 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""core/helpers.py
"""
import os
import sys
import collections
bcolours = {
"white": '\033[97m',
"aqua": '\033[96m',
"pink": '\033[95m',
"blue": '\033[94m',
"yellow": '\033[93m',
"green": '\033[92m',
"red": '\033[91m',
"grey": '\033[90m',
"ENDC": '\033[0m'
}
def isiterable(obj):
"""
Return True if you can loop over obj otherwise False
"""
return isinstance(obj, collections.Iterable)
def get_colour(colour):
return bcolours.get(colour, bcolours['white'])
def printcolour(text, sameline=False, colour=get_colour("ENDC")):
"""
Print color text using escape codes
"""
if sameline:
sep = ''
else:
sep = '\n'
sys.stdout.write(get_colour(colour) + text + bcolours["ENDC"] + sep)
printcolor = printcolour
def drange(start, stop, step=1.0):
"generate between 2 numbers w/ optional step"
if step==0:
step = 0.01
r = start
while r < stop:
yield r
r += step
def box_text(text, width, offset=0):
box = " "*offset + "-"*(width+2) + "\n"
box += " "*offset + "|"+ text.center(width) + "|" + "\n"
box += " "*offset + "-"*(width+2)
return box
def try_cast_str_to_number(number_str, verbose=False):
"takes txt and tries to coerce it to float"
try:
return float(number_str.strip())
except Exception, err:
if verbose:
sys.stderr.write(err.message)
sys.stderr.flush()
return None
def read_xy_pairs_from_csv(filename, X=0, y=1, sep=',', header=False):
"""Grab 2 columns from a csv file by name or col index
Arguments:
filename Name of the csv file to read
X Index of the X column in the file [default: 0]
y Index of the y column in the file [default: 0]
sep Delimiter of the file [default: ,]
header Does the file have headers in row #1 [default: False]
"""
with open(filename, 'r') as f:
data = [line.strip() for line in f.readlines()]
data = [line.split(sep) for line in data]
data = [map(str.strip, row) for row in data]
if isinstance(X, int) and isinstance(y, int):
X_idx, y_idx = X, y
elif isinstance(X, basestring) and isinstance(y, basestring) \
and X.strip().isdigit() and y.strip().isdigit():
X_idx = int(try_cast_str_to_number(X))
y_idx = int(try_cast_str_to_number(y))
else:
X_idx, y_idx = None, None
X_y_pairs = []
for i, row in enumerate(data):
if i == 0 and header:
for j, col in enumerate(row):
if col.strip().lower() == X.strip().lower():
X_idx = j
if col.strip().lower() == y.strip().lower():
y_idx = j
if (X_idx is None or y_idx is None):
raise Exception("Column not found.")
try:
row = [try_cast_str_to_number(item) for item in row]
if row[0] and row[1]:
X_y_pairs.append(row)
except Exception, err:
pass
return X_y_pairs