Mon Apr 05 2021Journal

Python coding tricks

avatar
Xavier Bruhiere
linkedintwitterfacebook
intro picture
HEY:

Work in progress so considere this more like a brain dump than battle-tested knowledge (although the ideas do come from the trenches).

Tooling

Re-import modules

The issue: Python caches module imports. Therefor reimporting fixed code actually doesn't change anything.

First solution, interpreter agnostic:

import importlib
importlib.reload(my_module)

Ipython shortcut:

# just run, don't import
%run my_file(.py)

# activate autoreload
%load_ext autoreload
%autoreload 2

More info at the source (including a few caveats).

Data Types

Enums

It basically looks like an immutable data bag.

from enum import auto, unique, Enum

@unique  # validate each field is unique
class Ticker(Enum):
    idx = auto()  # auto seq id
    company = 'google'
    price = 1023

Ticker['company'] == Ticker.company  # True
Ticker('google').name  # company

Interesting Links





#coding#python#tips

Read Next

How to retain your team

Startup founders, HR, agencies, and the like hunt engineers. All day long, in all timezones. And the member of your team that sits right next to you has probably received an offer lately. You hired him, you mentored him, and he is now an important part of the implicit knowledge behind the products. So now, what can you do to keep him committed to your company’s goals?