diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2017-10-02 10:41:58 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2017-10-02 10:41:58 +0200 |
commit | 2ace57ab4fb78ce72798afd3da11c91bc3098e0d (patch) | |
tree | 7dfc6187214f4e6825b86842e3bf2e13babbcdc5 /src/ee/digikey | |
parent | 5cb42a172fa75c8d121c3df293c2bf53f9cad026 (diff) | |
download | ee-python-2ace57ab4fb78ce72798afd3da11c91bc3098e0d.tar.gz ee-python-2ace57ab4fb78ce72798afd3da11c91bc3098e0d.tar.bz2 ee-python-2ace57ab4fb78ce72798afd3da11c91bc3098e0d.tar.xz ee-python-2ace57ab4fb78ce72798afd3da11c91bc3098e0d.zip |
o Making sure attribute type's ids are int.
o Making find_by_mpn return the first item found.
Diffstat (limited to 'src/ee/digikey')
-rw-r--r-- | src/ee/digikey/__init__.py | 15 |
1 files changed, 10 insertions, 5 deletions
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 |