From 4e49da3b19599c03d33fa361e4447b6989ed3b67 Mon Sep 17 00:00:00 2001 From: olliemath Date: Tue, 27 Jul 2021 23:12:23 +0100 Subject: [PATCH] Chore: fix linting on majority of modules --- clickhouse_orm/funcs.py | 4 +-- clickhouse_orm/migrations.py | 11 ++++---- clickhouse_orm/models.py | 47 ++++++++++++++++----------------- clickhouse_orm/system_models.py | 2 +- clickhouse_orm/utils.py | 8 +++--- 5 files changed, 36 insertions(+), 36 deletions(-) diff --git a/clickhouse_orm/funcs.py b/clickhouse_orm/funcs.py index 192b0b3..52be002 100644 --- a/clickhouse_orm/funcs.py +++ b/clickhouse_orm/funcs.py @@ -1,9 +1,9 @@ from functools import wraps -from inspect import signature, Parameter +from inspect import Parameter, signature from types import FunctionType -from .utils import is_iterable, comma_join, NO_VALUE, arg_to_sql from .query import Cond, QuerySet +from .utils import NO_VALUE, arg_to_sql, comma_join, is_iterable def binary_operator(func): diff --git a/clickhouse_orm/migrations.py b/clickhouse_orm/migrations.py index dc37f20..722bf8c 100644 --- a/clickhouse_orm/migrations.py +++ b/clickhouse_orm/migrations.py @@ -1,9 +1,10 @@ -from .models import Model, BufferModel -from .fields import DateField, StringField -from .engines import MergeTree -from .utils import escape, get_subclass_names - import logging + +from .engines import MergeTree +from .fields import DateField, StringField +from .models import BufferModel, Model +from .utils import get_subclass_names + logger = logging.getLogger('migrations') diff --git a/clickhouse_orm/models.py b/clickhouse_orm/models.py index 0fc5e7f..3caa311 100644 --- a/clickhouse_orm/models.py +++ b/clickhouse_orm/models.py @@ -1,4 +1,3 @@ -from __future__ import unicode_literals import sys from collections import OrderedDict from itertools import chain @@ -6,23 +5,22 @@ from logging import getLogger import pytz +from .engines import Distributed, Merge from .fields import Field, StringField -from .utils import parse_tsv, NO_VALUE, get_subclass_names, arg_to_sql, unescape -from .query import QuerySet from .funcs import F -from .engines import Merge, Distributed +from .query import QuerySet +from .utils import NO_VALUE, arg_to_sql, get_subclass_names, parse_tsv logger = getLogger('clickhouse_orm') - class Constraint: ''' Defines a model constraint. ''' - name = None # this is set by the parent model - parent = None # this is set by the parent model + name = None # this is set by the parent model + parent = None # this is set by the parent model def __init__(self, expr): ''' @@ -42,8 +40,8 @@ class Index: Defines a data-skipping index. ''' - name = None # this is set by the parent model - parent = None # this is set by the parent model + name = None # this is set by the parent model + parent = None # this is set by the parent model def __init__(self, expr, type, granularity): ''' @@ -126,7 +124,7 @@ class ModelBase(type): ad_hoc_model_cache = {} - def __new__(cls, name, bases, attrs): + def __new__(metacls, name, bases, attrs): # Collect fields, constraints and indexes from parent classes fields = {} @@ -172,35 +170,36 @@ class ModelBase(type): _defaults=defaults, _has_funcs_as_defaults=has_funcs_as_defaults ) - model = super(ModelBase, cls).__new__(cls, str(name), bases, attrs) + model = super(ModelBase, metacls).__new__(metacls, str(name), bases, attrs) # Let each field, constraint and index know its parent and its own name for n, obj in chain(fields, constraints.items(), indexes.items()): - setattr(obj, 'parent', model) - setattr(obj, 'name', n) + obj.parent = model + obj.name = n return model @classmethod - def create_ad_hoc_model(cls, fields, model_name='AdHocModel'): + def create_ad_hoc_model(metacls, fields, model_name='AdHocModel'): # fields is a list of tuples (name, db_type) # Check if model exists in cache fields = list(fields) cache_key = model_name + ' ' + str(fields) - if cache_key in cls.ad_hoc_model_cache: - return cls.ad_hoc_model_cache[cache_key] + if cache_key in metacls.ad_hoc_model_cache: + return metacls.ad_hoc_model_cache[cache_key] # Create an ad hoc model class attrs = {} for name, db_type in fields: - attrs[name] = cls.create_ad_hoc_field(db_type) - model_class = cls.__new__(cls, model_name, (Model,), attrs) + attrs[name] = metacls.create_ad_hoc_field(db_type) + model_class = metacls.__new__(metacls, model_name, (Model,), attrs) # Add the model class to the cache - cls.ad_hoc_model_cache[cache_key] = model_class + metacls.ad_hoc_model_cache[cache_key] = model_class return model_class @classmethod - def create_ad_hoc_field(cls, db_type): + def create_ad_hoc_field(metacls, db_type): import clickhouse_orm.fields as orm_fields + # Enums if db_type.startswith('Enum'): return orm_fields.BaseEnumField.create_ad_hoc_field(db_type) @@ -219,13 +218,13 @@ class ModelBase(type): ) # Arrays if db_type.startswith('Array'): - inner_field = cls.create_ad_hoc_field(db_type[6 : -1]) + inner_field = metacls.create_ad_hoc_field(db_type[6 : -1]) return orm_fields.ArrayField(inner_field) # Tuples (poor man's version - convert to array) if db_type.startswith('Tuple'): types = [s.strip() for s in db_type[6 : -1].split(',')] assert len(set(types)) == 1, 'No support for mixed types in tuples - ' + db_type - inner_field = cls.create_ad_hoc_field(types[0]) + inner_field = metacls.create_ad_hoc_field(types[0]) return orm_fields.ArrayField(inner_field) # FixedString if db_type.startswith('FixedString'): @@ -239,11 +238,11 @@ class ModelBase(type): return field_class(*args) # Nullable if db_type.startswith('Nullable'): - inner_field = cls.create_ad_hoc_field(db_type[9 : -1]) + inner_field = metacls.create_ad_hoc_field(db_type[9 : -1]) return orm_fields.NullableField(inner_field) # LowCardinality if db_type.startswith('LowCardinality'): - inner_field = cls.create_ad_hoc_field(db_type[15 : -1]) + inner_field = metacls.create_ad_hoc_field(db_type[15 : -1]) return orm_fields.LowCardinalityField(inner_field) # Simple fields name = db_type + 'Field' diff --git a/clickhouse_orm/system_models.py b/clickhouse_orm/system_models.py index 69b67fa..af280c7 100644 --- a/clickhouse_orm/system_models.py +++ b/clickhouse_orm/system_models.py @@ -5,7 +5,7 @@ https://clickhouse.tech/docs/en/system_tables/ from __future__ import unicode_literals from .database import Database -from .fields import * +from .fields import DateTimeField, StringField, UInt8Field, UInt32Field, UInt64Field from .models import Model from .utils import comma_join diff --git a/clickhouse_orm/utils.py b/clickhouse_orm/utils.py index 78701ff..140cbed 100644 --- a/clickhouse_orm/utils.py +++ b/clickhouse_orm/utils.py @@ -1,8 +1,9 @@ import codecs +import importlib +import pkgutil import re from datetime import date, datetime, tzinfo, timedelta - SPECIAL_CHARS = { "\b" : "\\b", "\f" : "\\f", @@ -17,7 +18,6 @@ SPECIAL_CHARS = { SPECIAL_CHARS_REGEX = re.compile("[" + ''.join(SPECIAL_CHARS.values()) + "]") - def escape(value, quote=True): ''' If the value is a string, escapes any special characters and optionally @@ -48,7 +48,7 @@ def arg_to_sql(arg): Supports functions, model fields, strings, dates, datetimes, timedeltas, booleans, None, numbers, timezones, arrays/iterables. """ - from clickhouse_orm import Field, StringField, DateTimeField, DateField, F, QuerySet + from clickhouse_orm import Field, StringField, DateTimeField, F, QuerySet if isinstance(arg, F): return arg.to_sql() if isinstance(arg, Field): @@ -122,7 +122,6 @@ def import_submodules(package_name): """ Import all submodules of a module. """ - import importlib, pkgutil package = importlib.import_module(package_name) return { name: importlib.import_module(package_name + '.' + name) @@ -164,4 +163,5 @@ class NoValue: def __repr__(self): return 'NO_VALUE' + NO_VALUE = NoValue()