Skip to content

Instantly share code, notes, and snippets.

@abetkin
abetkin / introspection.md
Last active September 13, 2017 08:43
Database introspection instead of defining schema with language structures

There has been some time since web frameworks like django or rails become popular. Usually they had an ORM integrated in or, more generally, a relational database toolkit. What I want to discuss is relevant to most of database toolkits: django ORM, sqlalchemy, rails active record, sequelizejs and many others.

One common thing about their design is that the database schema is defined with language structures, be it classes or structs or dictionaries, and not by making requests to the database for existing tables.

In other words, like this

Interfaces for web servers. Universal url handlers.

First, some dictionary. What are url handlers? Functions containing logic that takes place after a specified url has been requested. In the simplest case it's the function that takes in the http request and returns the http response. In django they are called views.

Although this post touches more broad issues than just url handlers for simplicity let's talk about universal url handlers for now.

def handler(params):
 return {'some': 'result'}

Django ORM

Django ORM is not the only good part of django, but personally I'm a fan exactly of that part. It's been critisized for the fact that it abstracts sql concepts away from the developer. While that's true, I think the main value of a database toolkit is not educational.

I think to a degree Django ORM can be viewed as a higher level query language that uses sql as implementation. For example it's concept of hierarchical attributes (stuff like books__author__fans__name), is a neat abstraction but is absent from the sql language itself.

Besides the query language what I like in django ORM is django applications. As far as I know it is its unique feature and I think indispensable for the distribution and updates of applications that will use external database.

Django ORM was designed to be used from the Python language but let's have a closer look at it. For the most part Python is used just for declaration. models.py? You can declare it with a json as well. Querysets? They are composed from

Hi everyone!

I am coming from Python background, and in Python we have context managers.

These are objects encapsulating the try-catch-finally block, and usually are used to cleanup resources, like closing the open file. In Python community, most developers consider context managers a very neat feature of the language and you really can find them often in the code.

So, my question is: why is this feature missing completely in the js world? I could not find a single library. I think I can imagine what it could look like:

@abetkin
abetkin / debug.py
Last active October 14, 2016 16:32
# Python 3
from functools import wraps
from contextlib import ExitStack
def debug(f):
@wraps(f)
def wrapper(*args, **kw):
with ExitStack() as stack:
if 'debug_option':
import ipdb
@abetkin
abetkin / template_context.py
Created September 30, 2016 18:05
Function-defined context
class Context(object):
'''
Function-defined context
'''
class _Obj(object):
def __init__(self, obj):
self.obj = obj
import sys
import os
import logging
from pony.py23compat import PY2
from ponytest import with_cli_args, pony_fixtures, ValidationError, Fixture, provider_validators, provider
from functools import wraps, partial
import click
from contextlib import contextmanager, closing

Overview

class DatabaseVendor(db.Entity):
    name = Required(str)
    commercial = Required(bool)
    features = Optional(Json)
    supported_os = Optional(Json)
    
with db_session:
@abetkin
abetkin / bound_args.py
Created February 19, 2015 09:24
BoundArguments
class BoundArguments:
'''
Bound arguments of a function (like `inspect.BoundArguments`),
exposed with an interface of named tuple.
'''
def __init__(self, function, *args, **kwargs):
self.signature = inspect.signature(function)
@abetkin
abetkin / ObjectFormatter.py
Created October 19, 2014 16:40
dot access object formatter
class ObjectFormatter(string.Formatter):
'''
Usage:
formatter.format("Here is {my.nested.attribute}", obj)
Attribute look-up understands dictionary keys and object attributes.
'''
def get_value(self, key, args, kwds):
if not isinstance(key, str):