diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2017-09-20 22:43:07 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2017-09-20 22:44:43 +0200 |
commit | 21972f97987d239c0bd924c090ede852a0427bec (patch) | |
tree | a32563f8dde9023ef1752afbe144736e2a8b9640 /src/ee/kicad | |
parent | f67caec531259900a74fbd35eca77d1427700a36 (diff) | |
download | ee-python-21972f97987d239c0bd924c090ede852a0427bec.tar.gz ee-python-21972f97987d239c0bd924c090ede852a0427bec.tar.bz2 ee-python-21972f97987d239c0bd924c090ede852a0427bec.tar.xz ee-python-21972f97987d239c0bd924c090ede852a0427bec.zip |
o Adding ee.kicad.to_pandas().
Diffstat (limited to 'src/ee/kicad')
-rw-r--r-- | src/ee/kicad/__init__.py | 41 | ||||
-rw-r--r-- | src/ee/kicad/bom/__init__.py | 2 | ||||
-rw-r--r-- | 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 @@ -97,6 +97,10 @@ class Component(object): 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 |