Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Beyond the pip

Daniela
February 23, 2019

Beyond the pip

Making sense of the dependency jungle

Daniela

February 23, 2019
Tweet

More Decks by Daniela

Other Decks in Technology

Transcript

  1. Disclaimer: this is not about Disclaimer: this is not about

    pip (in particular). pip (in particular).
  2. It's the year 2019 It's the year 2019 are we

    supposed to write our are we supposed to write our own code now?? own code now??
  3. Is the library... Is the library... mature? mature? used in

    commercial products? used in commercial products?
  4. Is the library... Is the library... mature? mature? used in

    commercial products? used in commercial products? backed up by other organizations? backed up by other organizations?
  5. Is the library... Is the library... used all through your

    project? used all through your project?
  6. Is the library... Is the library... used all through your

    project? used all through your project? heavily relying on other libraries? heavily relying on other libraries?
  7. Use a pattern... Use a pattern... wrap it up! (the

    code, that is) wrap it up! (the code, that is) from external_dependency import something class SomeExternalDependencyClient: def __init__(self, credentials, name): self.client = something.Client(credentials, name) def get_items(self, ids): self.client.get(ids) def send_items(self, item_list): self.client.batch_insert(item_list)
  8. The standard library is The standard library is awesome! awesome!

    Good resources Good resources Module of the Week by Doug Hellmann PyTricks by Dan Bader
  9. You have You have ... ... magic methods magic methods

    class Hasher(object): def __init__(self, algorithm): self.algorithm = algorithm def __call__(self, file): hash = self.algorithm() with open(file, 'rb') as f: for chunk in iter(lambda: f.read(4096), ''): hash.update(chunk) return hash.hexdigest() md5 = Hasher(hashlib.md5) sha1 = Hasher(hashlib.sha1) md5(somefile)
  10. Awesome Awesome decorators decorators from functools import wraps def retry(count=5,

    exc_type=Exception): def decorator(func): @wraps(func) def result(*args, **kwargs): last_exc = None for _ in range(count): try: return func(*args, **kwargs) except exc_type as e: last_exc = e raise last_exc return result return decorator @retry def might_fail(): # some code here pass
  11. class Cache: def __init__(self): self.memo = {} def store(self, fn):

    def wrapper(*args): if args not in self.memo: self.memo[args] = fn(*args) return self.memo[args] return wrapper def clear(self): self.memo.clear() cache = Cache() @cache.store def somefct(): return expensive_call() cache.clear()
  12. Powerful Powerful containers containers >>> from collections import Counter >>>

    colors = ['blue', 'red', 'blue', 'yellow', 'blue', 'red'] >>> counter = Counter(colors) Counter({'blue': 3, 'red': 2, 'yellow': 1}) >>> counter.most_common()[0][0] 'blue' >>> Point = collections.namedtuple('Point', 'x y') >>> p = Point(1, y=2) Point(x=1, y=2) >>> p.x 1 >>> getattr(p, 'y') 2 >>> p._fields ('x', 'y')
  13. and so many MORE! and so many MORE! utilities utilities

    powerful powerful speci c operations speci c operations dev tools: dev tools: , , , , date & time handling date & time handling regex regex os os pydoc pydoc unittest unittest pdb pdb