aboutsummaryrefslogtreecommitdiff
path: root/src/ee/_utils.py
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2019-05-28 09:46:41 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2019-05-28 09:46:41 +0200
commit2e2956823c9cd02c766b296cbcbea9130bd07b36 (patch)
tree29be58764f33374cd768a5151182d1a2aa75b041 /src/ee/_utils.py
parentd108963a31f726ec8e1f471695addbbabb0db312 (diff)
downloadee-python-2e2956823c9cd02c766b296cbcbea9130bd07b36.tar.gz
ee-python-2e2956823c9cd02c766b296cbcbea9130bd07b36.tar.bz2
ee-python-2e2956823c9cd02c766b296cbcbea9130bd07b36.tar.xz
ee-python-2e2956823c9cd02c766b296cbcbea9130bd07b36.zip
digikey: Better search when getting multiple results back. Instead of
doing a new search with the selected digikey part number, do a direct lookup with the product's URL instead. This ensures that we always get a match and don't get confused when multiple part numbers are returned.
Diffstat (limited to 'src/ee/_utils.py')
-rw-r--r--src/ee/_utils.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/ee/_utils.py b/src/ee/_utils.py
index 08e75fa..5960161 100644
--- a/src/ee/_utils.py
+++ b/src/ee/_utils.py
@@ -77,3 +77,24 @@ class EmptyHttpCache(object):
def maybe_cache(path: Optional[Path], **kwargs) -> HttpCache:
return HttpCache(path, **kwargs) if path is not None else EmptyHttpCache()
+
+
+def gen_rst_table(header: List[str], data: List[List[str]]):
+ column_widths = []
+ for i in range(len(header)):
+ w = len(header[i])
+ for row in data:
+ w = max(w, len(row[i]))
+ column_widths.append(w)
+
+ import io
+ buf = io.StringIO()
+ sep = "+-" + "-+-".join(["-" * w for i, w in enumerate(column_widths)]) + "-+"
+ print(sep, file=buf)
+ print("| " + " | ".join([header[i].ljust(w) for i, w in enumerate(column_widths)]) + " |", file=buf)
+ print(sep.replace("-", "="), file=buf)
+ for row in data:
+ print("| " + " | ".join([row[i].ljust(w) for i, w in enumerate(column_widths)]) + " |", file=buf)
+ print(sep, file=buf)
+
+ return buf.getvalue()