From 21972f97987d239c0bd924c090ede852a0427bec Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Wed, 20 Sep 2017 22:43:07 +0200 Subject: o Adding ee.kicad.to_pandas(). --- src/ee/kicad/__init__.py | 41 ++++++++++++++++++++++++++++++++++++++++- src/ee/kicad/bom/__init__.py | 2 +- src/ee/kicad/model.py | 4 ++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/ee/kicad/__init__.py b/src/ee/kicad/__init__.py index c4c3af5..b270ba1 100644 --- a/src/ee/kicad/__init__.py +++ b/src/ee/kicad/__init__.py @@ -1,4 +1,5 @@ -import re +from typing import Any + from ee import EeException from ee.kicad.read_schematic import read_schematic from ee.kicad.to_bom import to_bom @@ -11,4 +12,42 @@ __all__ = [ "Schematic", "read_schematic", "to_bom", + "to_pandas", ] + + +def to_pandas(obj: Any, **kwarg): + import pandas + + def run_filter(filters, obj): + for f in filters: + if not f(obj): + return False + return True + + def to_pandas_schematic(sch: Schematic, **kwarg): + def make_dict(c: Component): + return { + "ref": c.ref, + "ref_type": c.ref_type, + "ref_num": c.ref_num, + "value": c.value + } + + components = sch.components + + filters = [] + + if not kwarg.get("include_pwr", False): + filters.append(lambda c: not c.is_pwr) + + data = [make_dict(c) for c in components if run_filter(filters, c)] + + return pandas.DataFrame(data=data, columns=["ref", "ref_type", "ref_num", "value"]).\ + set_index("ref").\ + sort_index() + + if isinstance(obj, Schematic): + return to_pandas_schematic(obj) + else: + raise EeException("Unsupported type: {}".format(type(obj))) diff --git a/src/ee/kicad/bom/__init__.py b/src/ee/kicad/bom/__init__.py index 2a32521..a36068b 100644 --- a/src/ee/kicad/bom/__init__.py +++ b/src/ee/kicad/bom/__init__.py @@ -67,7 +67,7 @@ class Bom(object): return self._components def all_field_names(self): - fields = set(['ref', 'value']) + fields = {'ref', 'value'} for c in self._components.values(): for f in c.fields: fields.add(f) diff --git a/src/ee/kicad/model.py b/src/ee/kicad/model.py index 7baa4a0..519a978 100644 --- a/src/ee/kicad/model.py +++ b/src/ee/kicad/model.py @@ -96,6 +96,10 @@ class Component(object): r = re.compile("([^0-9]+)(.+)") return int(r.split(self._ref)[2]) + @property + def is_pwr(self) -> bool: + return self.ref_type == "#PWR" + @property def value(self): return self._fields[1].value -- cgit v1.2.3