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 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'src/ee/kicad/__init__.py') 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))) -- cgit v1.2.3