Giter VIP home page Giter VIP logo

Comments (8)

sangshuduo avatar sangshuduo commented on June 11, 2024

@modaye 请提供可以重现的简单代码,谢谢

from taos-connector-python.

modaye avatar modaye commented on June 11, 2024

@modaye请提供可以返回的简单代码,谢谢

hellozai/db/base.py

import logging
import functools
import textwrap
from typing import Optional, Callable
from collections import ChainMap

import taos


logger = logging.getLogger(__name__)


def create_taos_connect(
        host: str = "localhost",
        port: int = 6030,
        user: str = "root",
        password: str = "taosdata",
        database: Optional[str] = None,
        **kwargs
) -> taos.TaosConnection:
    params = {
        "host": host,
        "port": port,
        "user": user,
        "password": password,
        "database": database
    }
    params = {k: v for k, v in params.items() if v is not None}
    kwargs.update(params)
    config = ChainMap(kwargs)
    # 将taos连接器进行包装 对调用进行捕获或拦截
    _db_conn = ConnectWrapper(taos.connect(**config))
    logger.info(
        f"taos connect host:%s port: %s database: %s",
        config.get('host'), config.get('port'), config.get('database')
    )
    return _db_conn  # type: ignore


def params_catch(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        if args:
            logger.debug("func: %s, args: %s", func.__name__, textwrap.shorten(args[0], width=300, placeholder="..."))
        return func(*args, **kwargs)

    return wrapper


class ConnectWrapper:
    def __init__(self, conn: taos.TaosConnection):
        self._conn = conn

    def __getattr__(self, item):
        attr = getattr(self._conn, item)
        if isinstance(attr, Callable):
            return params_catch(attr)
        return attr

在base.py 中直接获取连接对象执行任意数据库操作不会报错,但是当在其他模块引用该 create_taos_connect 函数并建立连接就产生上面的报错。

run.py

from hellozai.db import create_taos_connect


db = create_taos_connect()
db.execute("show databases;")

引用相同的 base.py 代码在3个不同的电脑(Ubuntu 20.04)均稳定复现错误。

from taos-connector-python.

zitsen avatar zitsen commented on June 11, 2024

我在多个版本下下均未复现。只能建议检查下环境。

from taos-connector-python.

dingbo8128 avatar dingbo8128 commented on June 11, 2024

我在 windows 上 用多个版本 python 测试,也均无复现。 最好提供项目结构的细节。

from taos-connector-python.

modaye avatar modaye commented on June 11, 2024

确实是特定的项目才会复现该问题, 我在hellozai 目录下还有一个models 包, 包中的models.py 文件中 也导入了taos 用于其中函数的类型注解,经过测试只要在该 models.py 模块中导入 taos 就引发错误。
已经放弃在该模块添加类型提示,使用 models.pyi 作为注解存根不会引发错误
在其他命名的项目下,复制 hellozai 内部所有文件,进行测试也不会产生错误

from taos-connector-python.

modaye avatar modaye commented on June 11, 2024

这是一个奇怪的问题,将代码copy 到其他由Pycharm 创建的项目下,并不会引发报错。

from taos-connector-python.

modaye avatar modaye commented on June 11, 2024

我在多个版本下下均未复现。只能建议检查下环境。

感谢

from taos-connector-python.

modaye avatar modaye commented on June 11, 2024

我在 windows 上 用多个版本 python 测试,也均无复现。 最好提供项目结构的细节。

感谢

from taos-connector-python.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.