Skip to content

Instantly share code, notes, and snippets.

#coding: utf-8
import re
import inspect
import operator
from django.db.models import Q, query
import rest_framework
class QSimpleFilter(rest_framework.filters.BaseFilterBackend):
#coding: utf-8
"""Throwaway script.
Usage:
test.py
test.py zip <project>...
"""
import os, sys
@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):
@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)

Overview

class DatabaseVendor(db.Entity):
    name = Required(str)
    commercial = Required(bool)
    features = Optional(Json)
    supported_os = Optional(Json)
    
with db_session:
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
@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
@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

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:

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