From 2ace57ab4fb78ce72798afd3da11c91bc3098e0d Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Mon, 2 Oct 2017 10:41:58 +0200 Subject: o Making sure attribute type's ids are int. o Making find_by_mpn return the first item found. --- src/ee/digikey/__init__.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'src/ee') diff --git a/src/ee/digikey/__init__.py b/src/ee/digikey/__init__.py index 003394e..934c0d2 100644 --- a/src/ee/digikey/__init__.py +++ b/src/ee/digikey/__init__.py @@ -91,6 +91,9 @@ class DigikeyProduct(object): def __hash__(self): return self.part_number.__hash__() + def attribute_by_id(self, _id): + return next((a for a in self.attributes if a.attribute_type.id == _id), None) + def to_ini(self, c: configparser.ConfigParser): c["overview"] = {}; overview = c["overview"] @@ -114,7 +117,7 @@ class DigikeyProduct(object): attributes = [] for k, value in c.items("attributes"): (type_id, label) = DigikeyProduct.from_ini_r.match(k).groups() - a_type = digikey.get_attribute_type(type_id, label) + a_type = digikey.get_attribute_type(int(type_id), label) attributes.append(DigikeyAttributeValue(value, a_type)) return DigikeyProduct(overview["part_number"], overview["mpn"], attributes) @@ -299,13 +302,13 @@ class DigikeyRepository(object): self._path = path self._products = {} - def mpn_to_path(self, mpn): + def _mpn_to_path(self, mpn): mpn = mpn.replace("/", "_").replace(" ", "_") return "{}/{}.ini".format(self._path, mpn) def has_product(self, mpn=None, dpn=None): if mpn is not None: - filename = self.mpn_to_path(mpn) + filename = self._mpn_to_path(mpn) return os.path.isfile(filename) if dpn is not None: for p in self.products: @@ -315,7 +318,7 @@ class DigikeyRepository(object): def save(self, product: DigikeyProduct): c = self._make_configparser() y = product.to_ini(c) - filename = self.mpn_to_path(product.mpn) + filename = self._mpn_to_path(product.mpn) mk_parents(filename) with open(filename, "w") as f: y.write(f) @@ -341,4 +344,6 @@ class DigikeyRepository(object): return self._products.values() def find_by_mpn(self, mpn): - return [p for p in self.products if p.mpn == mpn] + for p in self.products: + if p.mpn == mpn: + return p -- cgit v1.2.3