migrations = {}

def migrate(old_version, new_version):
    assert abs(new_version - old_version) == 1

def decorator(f):
    direction = 'up' if new_version > old_version else 'down'
if old_version not in migrations:
    migrations[old_version] = {}
migrations[old_version][direction] = f
return f
return decorator

@migrate(1, 2)
def upgrade():
    pass

@migrate(2, 1)
def downgrade():
    pass

@migrate(2, 3)
def upgrade():
    pass
print(migrations)
How to create a dict of functions in Python?