Chore: fix linting on majority of modules

This commit is contained in:
olliemath 2021-07-27 23:12:23 +01:00
parent 8fd0f2a422
commit 4e49da3b19
5 changed files with 36 additions and 36 deletions

View File

@ -1,9 +1,9 @@
from functools import wraps from functools import wraps
from inspect import signature, Parameter from inspect import Parameter, signature
from types import FunctionType from types import FunctionType
from .utils import is_iterable, comma_join, NO_VALUE, arg_to_sql
from .query import Cond, QuerySet from .query import Cond, QuerySet
from .utils import NO_VALUE, arg_to_sql, comma_join, is_iterable
def binary_operator(func): def binary_operator(func):

View File

@ -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 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') logger = logging.getLogger('migrations')

View File

@ -1,4 +1,3 @@
from __future__ import unicode_literals
import sys import sys
from collections import OrderedDict from collections import OrderedDict
from itertools import chain from itertools import chain
@ -6,23 +5,22 @@ from logging import getLogger
import pytz import pytz
from .engines import Distributed, Merge
from .fields import Field, StringField 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 .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') logger = getLogger('clickhouse_orm')
class Constraint: class Constraint:
''' '''
Defines a model constraint. Defines a model constraint.
''' '''
name = 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 parent = None # this is set by the parent model
def __init__(self, expr): def __init__(self, expr):
''' '''
@ -42,8 +40,8 @@ class Index:
Defines a data-skipping index. Defines a data-skipping index.
''' '''
name = 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 parent = None # this is set by the parent model
def __init__(self, expr, type, granularity): def __init__(self, expr, type, granularity):
''' '''
@ -126,7 +124,7 @@ class ModelBase(type):
ad_hoc_model_cache = {} ad_hoc_model_cache = {}
def __new__(cls, name, bases, attrs): def __new__(metacls, name, bases, attrs):
# Collect fields, constraints and indexes from parent classes # Collect fields, constraints and indexes from parent classes
fields = {} fields = {}
@ -172,35 +170,36 @@ class ModelBase(type):
_defaults=defaults, _defaults=defaults,
_has_funcs_as_defaults=has_funcs_as_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 # Let each field, constraint and index know its parent and its own name
for n, obj in chain(fields, constraints.items(), indexes.items()): for n, obj in chain(fields, constraints.items(), indexes.items()):
setattr(obj, 'parent', model) obj.parent = model
setattr(obj, 'name', n) obj.name = n
return model return model
@classmethod @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) # fields is a list of tuples (name, db_type)
# Check if model exists in cache # Check if model exists in cache
fields = list(fields) fields = list(fields)
cache_key = model_name + ' ' + str(fields) cache_key = model_name + ' ' + str(fields)
if cache_key in cls.ad_hoc_model_cache: if cache_key in metacls.ad_hoc_model_cache:
return cls.ad_hoc_model_cache[cache_key] return metacls.ad_hoc_model_cache[cache_key]
# Create an ad hoc model class # Create an ad hoc model class
attrs = {} attrs = {}
for name, db_type in fields: for name, db_type in fields:
attrs[name] = cls.create_ad_hoc_field(db_type) attrs[name] = metacls.create_ad_hoc_field(db_type)
model_class = cls.__new__(cls, model_name, (Model,), attrs) model_class = metacls.__new__(metacls, model_name, (Model,), attrs)
# Add the model class to the cache # 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 return model_class
@classmethod @classmethod
def create_ad_hoc_field(cls, db_type): def create_ad_hoc_field(metacls, db_type):
import clickhouse_orm.fields as orm_fields import clickhouse_orm.fields as orm_fields
# Enums # Enums
if db_type.startswith('Enum'): if db_type.startswith('Enum'):
return orm_fields.BaseEnumField.create_ad_hoc_field(db_type) return orm_fields.BaseEnumField.create_ad_hoc_field(db_type)
@ -219,13 +218,13 @@ class ModelBase(type):
) )
# Arrays # Arrays
if db_type.startswith('Array'): 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) return orm_fields.ArrayField(inner_field)
# Tuples (poor man's version - convert to array) # Tuples (poor man's version - convert to array)
if db_type.startswith('Tuple'): if db_type.startswith('Tuple'):
types = [s.strip() for s in db_type[6 : -1].split(',')] 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 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) return orm_fields.ArrayField(inner_field)
# FixedString # FixedString
if db_type.startswith('FixedString'): if db_type.startswith('FixedString'):
@ -239,11 +238,11 @@ class ModelBase(type):
return field_class(*args) return field_class(*args)
# Nullable # Nullable
if db_type.startswith('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) return orm_fields.NullableField(inner_field)
# LowCardinality # LowCardinality
if db_type.startswith('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) return orm_fields.LowCardinalityField(inner_field)
# Simple fields # Simple fields
name = db_type + 'Field' name = db_type + 'Field'

View File

@ -5,7 +5,7 @@ https://clickhouse.tech/docs/en/system_tables/
from __future__ import unicode_literals from __future__ import unicode_literals
from .database import Database from .database import Database
from .fields import * from .fields import DateTimeField, StringField, UInt8Field, UInt32Field, UInt64Field
from .models import Model from .models import Model
from .utils import comma_join from .utils import comma_join

View File

@ -1,8 +1,9 @@
import codecs import codecs
import importlib
import pkgutil
import re import re
from datetime import date, datetime, tzinfo, timedelta from datetime import date, datetime, tzinfo, timedelta
SPECIAL_CHARS = { SPECIAL_CHARS = {
"\b" : "\\b", "\b" : "\\b",
"\f" : "\\f", "\f" : "\\f",
@ -17,7 +18,6 @@ SPECIAL_CHARS = {
SPECIAL_CHARS_REGEX = re.compile("[" + ''.join(SPECIAL_CHARS.values()) + "]") SPECIAL_CHARS_REGEX = re.compile("[" + ''.join(SPECIAL_CHARS.values()) + "]")
def escape(value, quote=True): def escape(value, quote=True):
''' '''
If the value is a string, escapes any special characters and optionally 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, Supports functions, model fields, strings, dates, datetimes, timedeltas, booleans,
None, numbers, timezones, arrays/iterables. 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): if isinstance(arg, F):
return arg.to_sql() return arg.to_sql()
if isinstance(arg, Field): if isinstance(arg, Field):
@ -122,7 +122,6 @@ def import_submodules(package_name):
""" """
Import all submodules of a module. Import all submodules of a module.
""" """
import importlib, pkgutil
package = importlib.import_module(package_name) package = importlib.import_module(package_name)
return { return {
name: importlib.import_module(package_name + '.' + name) name: importlib.import_module(package_name + '.' + name)
@ -164,4 +163,5 @@ class NoValue:
def __repr__(self): def __repr__(self):
return 'NO_VALUE' return 'NO_VALUE'
NO_VALUE = NoValue() NO_VALUE = NoValue()