From 7f86cfe1ccaf9b2c5fc719336c4b990004cd1c62 Mon Sep 17 00:00:00 2001 From: Bertrand Bordage Date: Thu, 18 Jun 2015 03:07:04 +0200 Subject: [PATCH] Python optimisations. --- cachalot/monkey_patch.py | 8 +++++--- cachalot/utils.py | 10 +++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/cachalot/monkey_patch.py b/cachalot/monkey_patch.py index 1f06970..f89e11f 100644 --- a/cachalot/monkey_patch.py +++ b/cachalot/monkey_patch.py @@ -47,6 +47,9 @@ def _unset_raw_connection(original): return inner +TUPLE_OR_LIST = (tuple, list) + + def _get_result_or_execute_query(execute_query_func, cache_key, table_cache_keys): cache = cachalot_caches.get_cache() @@ -68,8 +71,7 @@ def _get_result_or_execute_query(execute_query_func, cache_key, return result result = execute_query_func() - if isinstance(result, Iterable) \ - and not isinstance(result, (tuple, list)): + if isinstance(result, Iterable) and result.__class__ not in TUPLE_OR_LIST: result = list(result) cache.set(cache_key, (time(), result), None) @@ -83,7 +85,7 @@ def _patch_compiler(original): def inner(compiler, *args, **kwargs): execute_query_func = lambda: original(compiler, *args, **kwargs) if not cachalot_settings.CACHALOT_ENABLED \ - or isinstance(compiler, WRITE_COMPILERS): + or compiler.__class__ in WRITE_COMPILERS: return execute_query_func() try: diff --git a/cachalot/utils.py b/cachalot/utils.py index 8434805..88c6e96 100644 --- a/cachalot/utils.py +++ b/cachalot/utils.py @@ -70,8 +70,8 @@ def _get_tables_from_sql(connection, lowercased_sql): def _find_subqueries(children): for child in children: - if isinstance(child, SubqueryConstraint): - if isinstance(child.query_object, Query): + if child.__class__ is SubqueryConstraint: + if child.query_object.__class__ is Query: yield child.query_object else: yield child.query_object.query @@ -80,9 +80,9 @@ def _find_subqueries(children): if DJANGO_GTE_1_7: if hasattr(child, 'rhs'): rhs = child.rhs - elif isinstance(child, tuple): + elif child.__class__ is tuple: rhs = child[-1] - if isinstance(rhs, Query): + if rhs.__class__ is Query: yield rhs elif hasattr(rhs, 'query'): yield rhs.query @@ -102,7 +102,7 @@ def _get_tables(query, db_alias): for subquery in subquery_constraints: tables.update(_get_tables(subquery, db_alias)) if query.extra_select or hasattr(query, 'subquery') \ - or any(isinstance(c, ExtraWhere) for c in query.where.children): + or any(c.__class__ is ExtraWhere for c in query.where.children): sql = query.get_compiler(db_alias).as_sql()[0].lower() additional_tables = _get_tables_from_sql(connections[db_alias], sql) tables.update(additional_tables)