# -*- coding: utf-8 -*-
from setuptools import setup

packages = \
['taos']

package_data = \
{'': ['*']}

install_requires = \
['typing']

setup_kwargs = {
    'name': 'taospy',
    'version': '2.1.1',
    'description': 'TDengine connector for python',
    'long_description': '# TDengine Connector for Python\n\n[TDengine](https://github.com/taosdata/TDengine) connector for Python enables python programs to access TDengine,\n using an API which is compliant with the Python DB API 2.0 (PEP-249). It uses TDengine C client library for client server communications.\n\n## Install\n\nYou can use pip to install from github or pypi:\n\n```sh\npip install git+https://github.com/taosdata/taos-connector-python.git\npip install taospy\n```\n\n## Source Code\n\n[TDengine](https://github.com/taosdata/TDengine) connector for Python source code is hosted on [GitHub](https://github.com/taosdata/taos-connector-python).\n\n## Examples\n\n### Query with PEP-249 API\n\n```python\nimport taos\n\nconn = taos.connect()\ncursor = conn.cursor()\n\ncursor.execute("show databases")\nresults = cursor.fetchall()\nfor row in results:\n    print(row)\ncursor.close()\nconn.close()\n```\n\n### Query with objective API\n\n```python\nimport taos\n\nconn = taos.connect()\nconn.exec("create database if not exists pytest")\n\nresult = conn.query("show databases")\nnum_of_fields = result.field_count\nfor field in result.fields:\n    print(field)\nfor row in result:\n    print(row)\nresult.close()\nconn.exec("drop database pytest")\nconn.close()\n```\n\n### Query with async API\n\n```python\nfrom taos import *\nfrom ctypes import *\nimport time\n\ndef fetch_callback(p_param, p_result, num_of_rows):\n    print("fetched ", num_of_rows, "rows")\n    p = cast(p_param, POINTER(Counter))\n    result = TaosResult(p_result)\n\n    if num_of_rows == 0:\n        print("fetching completed")\n        p.contents.done = True\n        result.close()\n        return\n    if num_of_rows < 0:\n        p.contents.done = True\n        result.check_error(num_of_rows)\n        result.close()\n        return None\n    \n    for row in result.rows_iter(num_of_rows):\n        # print(row)\n        None\n    p.contents.count += result.row_count\n    result.fetch_rows_a(fetch_callback, p_param)\n    \n\n\ndef query_callback(p_param, p_result, code):\n    # type: (c_void_p, c_void_p, c_int) -> None\n    if p_result == None:\n        return\n    result = TaosResult(p_result)\n    if code == 0:\n        result.fetch_rows_a(fetch_callback, p_param)\n    result.check_error(code)\n\n\nclass Counter(Structure):\n    _fields_ = [("count", c_int), ("done", c_bool)]\n\n    def __str__(self):\n        return "{ count: %d, done: %s }" % (self.count, self.done)\n\n\ndef test_query(conn):\n    # type: (TaosConnection) -> None\n    counter = Counter(count=0)\n    conn.query_a("select * from log.log", query_callback, byref(counter))\n\n    while not counter.done:\n        print("wait query callback")\n        time.sleep(1)\n    print(counter)\n    conn.close()\n\n\nif __name__ == "__main__":\n    test_query(connect())\n```\n\n### Statement API - Bind row after row\n\n```python\nfrom taos import *\n\nconn = connect()\n\ndbname = "pytest_taos_stmt"\nconn.exec("drop database if exists %s" % dbname)\nconn.exec("create database if not exists %s" % dbname)\nconn.select_db(dbname)\n\nconn.exec(\n    "create table if not exists log(ts timestamp, bo bool, nil tinyint, \\\n        ti tinyint, si smallint, ii int, bi bigint, tu tinyint unsigned, \\\n        su smallint unsigned, iu int unsigned, bu bigint unsigned, \\\n        ff float, dd double, bb binary(100), nn nchar(100), tt timestamp)",\n)\n\nstmt = conn.statement("insert into log values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")\n\nparams = new_bind_params(16)\nparams[0].timestamp(1626861392589)\nparams[1].bool(True)\nparams[2].null()\nparams[3].tinyint(2)\nparams[4].smallint(3)\nparams[5].int(4)\nparams[6].bigint(5)\nparams[7].tinyint_unsigned(6)\nparams[8].smallint_unsigned(7)\nparams[9].int_unsigned(8)\nparams[10].bigint_unsigned(9)\nparams[11].float(10.1)\nparams[12].double(10.11)\nparams[13].binary("hello")\nparams[14].nchar("stmt")\nparams[15].timestamp(1626861392589)\nstmt.bind_param(params)\n\nparams[0].timestamp(1626861392590)\nparams[15].null()\nstmt.bind_param(params)\nstmt.execute()\n\n\nresult = stmt.use_result()\nassert result.affected_rows == 2\nresult.close()\n\nresult = conn.query("select * from log")\n\nfor row in result:\n    print(row)\nresult.close()\nstmt.close()\nconn.close()\n\n```\n\n### Statement API - Bind multi rows\n\n```python\nfrom taos import *\n\nconn = connect()\n\ndbname = "pytest_taos_stmt"\nconn.exec("drop database if exists %s" % dbname)\nconn.exec("create database if not exists %s" % dbname)\nconn.select_db(dbname)\n\nconn.exec(\n    "create table if not exists log(ts timestamp, bo bool, nil tinyint, \\\n        ti tinyint, si smallint, ii int, bi bigint, tu tinyint unsigned, \\\n        su smallint unsigned, iu int unsigned, bu bigint unsigned, \\\n        ff float, dd double, bb binary(100), nn nchar(100), tt timestamp)",\n)\n\nstmt = conn.statement("insert into log values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")\n\nparams = new_multi_binds(16)\nparams[0].timestamp((1626861392589, 1626861392590, 1626861392591))\nparams[1].bool((True, None, False))\nparams[2].tinyint([-128, -128, None]) # -128 is tinyint null\nparams[3].tinyint([0, 127, None])\nparams[4].smallint([3, None, 2])\nparams[5].int([3, 4, None])\nparams[6].bigint([3, 4, None])\nparams[7].tinyint_unsigned([3, 4, None])\nparams[8].smallint_unsigned([3, 4, None])\nparams[9].int_unsigned([3, 4, None])\nparams[10].bigint_unsigned([3, 4, None])\nparams[11].float([3, None, 1])\nparams[12].double([3, None, 1.2])\nparams[13].binary(["abc", "dddafadfadfadfadfa", None])\nparams[14].nchar(["涛思数据", None, "a long string with 中文字符"])\nparams[15].timestamp([None, None, 1626861392591])\nstmt.bind_param_batch(params)\nstmt.execute()\n\n\nresult = stmt.use_result()\nassert result.affected_rows == 3\nresult.close()\n\nresult = conn.query("select * from log")\nfor row in result:\n    print(row)\nresult.close()\nstmt.close()\nconn.close()\n```\n\n### Statement API - Subscribe\n\n```python\nimport taos\n\nconn = taos.connect()\ndbname = "pytest_taos_subscribe_callback"\nconn.exec("drop database if exists %s" % dbname)\nconn.exec("create database if not exists %s" % dbname)\nconn.select_db(dbname)\nconn.exec("create table if not exists log(ts timestamp, n int)")\nfor i in range(10):\n    conn.exec("insert into log values(now, %d)" % i)\n\nsub = conn.subscribe(True, "test", "select * from log", 1000)\nprint("# consume from begin")\nfor ts, n in sub.consume():\n    print(ts, n)\n\nprint("# consume new data")\nfor i in range(5):\n    conn.exec("insert into log values(now, %d)(now+1s, %d)" % (i, i))\n    result = sub.consume()\n    for ts, n in result:\n        print(ts, n)\n\nprint("# consume with a stop condition")\nfor i in range(10):\n    conn.exec("insert into log values(now, %d)" % int(random() * 10))\n    result = sub.consume()\n    try:\n        ts, n = next(result)\n        print(ts, n)\n        if n > 5:\n            result.stop_query()\n            print("## stopped")\n            break\n    except StopIteration:\n        continue\n\nsub.close()\n\nconn.exec("drop database if exists %s" % dbname)\nconn.close()\n```\n\n### Statement API - Subscribe asynchronously with callback\n\n```python\nfrom taos import *\nfrom ctypes import *\n\nimport time\n\n\ndef subscribe_callback(p_sub, p_result, p_param, errno):\n    # type: (c_void_p, c_void_p, c_void_p, c_int) -> None\n    print("# fetch in callback")\n    result = TaosResult(p_result)\n    result.check_error(errno)\n    for row in result.rows_iter():\n        ts, n = row()\n        print(ts, n)\n\n\ndef test_subscribe_callback(conn):\n    # type: (TaosConnection) -> None\n    dbname = "pytest_taos_subscribe_callback"\n    try:\n        conn.exec("drop database if exists %s" % dbname)\n        conn.exec("create database if not exists %s" % dbname)\n        conn.select_db(dbname)\n        conn.exec("create table if not exists log(ts timestamp, n int)")\n\n        print("# subscribe with callback")\n        sub = conn.subscribe(False, "test", "select * from log", 1000, subscribe_callback)\n\n        for i in range(10):\n            conn.exec("insert into log values(now, %d)" % i)\n            time.sleep(0.7)\n        sub.close()\n\n        conn.exec("drop database if exists %s" % dbname)\n        conn.close()\n    except Exception as err:\n        conn.exec("drop database if exists %s" % dbname)\n        conn.close()\n        raise err\n\n\nif __name__ == "__main__":\n    test_subscribe_callback(connect())\n\n```\n\n### Statement API - Stream\n\n```python\nfrom taos import *\nfrom ctypes import *\n\ndef stream_callback(p_param, p_result, p_row):\n    # type: (c_void_p, c_void_p, c_void_p) -> None\n\n    if p_result == None or p_row == None:\n        return\n    result = TaosResult(p_result)\n    row = TaosRow(result, p_row)\n    try:\n        ts, count = row()\n        p = cast(p_param, POINTER(Counter))\n        p.contents.count += count\n        print("[%s] inserted %d in 5s, total count: %d" % (ts.strftime("%Y-%m-%d %H:%M:%S"), count, p.contents.count))\n\n    except Exception as err:\n        print(err)\n        raise err\n\n\nclass Counter(ctypes.Structure):\n    _fields_ = [\n        ("count", c_int),\n    ]\n\n    def __str__(self):\n        return "%d" % self.count\n\n\ndef test_stream(conn):\n    # type: (TaosConnection) -> None\n    dbname = "pytest_taos_stream"\n    try:\n        conn.exec("drop database if exists %s" % dbname)\n        conn.exec("create database if not exists %s" % dbname)\n        conn.select_db(dbname)\n        conn.exec("create table if not exists log(ts timestamp, n int)")\n\n        result = conn.query("select count(*) from log interval(5s)")\n        assert result.field_count == 2\n        counter = Counter()\n        counter.count = 0\n        stream = conn.stream("select count(*) from log interval(5s)", stream_callback, param=byref(counter))\n\n        for _ in range(0, 20):\n            conn.exec("insert into log values(now,0)(now+1s, 1)(now + 2s, 2)")\n            time.sleep(2)\n        stream.close()\n        conn.exec("drop database if exists %s" % dbname)\n        conn.close()\n    except Exception as err:\n        conn.exec("drop database if exists %s" % dbname)\n        conn.close()\n        raise err\n\n\nif __name__ == "__main__":\n    test_stream(connect())\n```\n\n### Insert with line protocol\n\n```python\nimport taos\n\nconn = taos.connect()\ndbname = "pytest_line"\nconn.exec("drop database if exists %s" % dbname)\nconn.exec("create database if not exists %s precision \'us\'" % dbname)\nconn.select_db(dbname)\n\nlines = [\n    \'st,t1=3i64,t2=4f64,t3="t3" c1=3i64,c3=L"pass",c2=false,c4=4f64 1626006833639000000ns\',\n    \'st,t1=4i64,t3="t4",t2=5f64,t4=5f64 c1=3i64,c3=L"pass it again",c2=true,c4=5f64,c5=5f64,c6=7u64 1626006933640000000\',\n    \'stf,t1=4i64,t3="t4",t2=5f64,t4=5f64 c1=3i64,c3=L"pass it again_stf",c2=false,c5=5f64,c6=7u64 1626006933641000000\',\n]\nconn.schemaless_insert(lines, 0, "ns")\nprint("inserted")\n\nlines = [\n    \'stf,t1=5i64,t3="t4",t2=5f64,t4=5f64 c1=3i64,c3=L"pass it again_stf",c2=false,c5=5f64,c6=7u64 1626006933641000000\',\n]\nconn.schemaless_insert(lines, 0, "ns")\n\nresult = conn.query("show tables")\nfor row in result:\n    print(row)\nresult.close()\n\n\nconn.exec("drop database if exists %s" % dbname)\nconn.close()\n\n```\n\n## License - AGPL-3.0\n\nKeep same with [TDengine](https://github.com/taosdata/TDengine).\n',
    'author': 'Taosdata Inc.',
    'author_email': 'support@taosdata.com',
    'maintainer': 'None',
    'maintainer_email': 'None',
    'url': 'None',
    'packages': packages,
    'package_data': package_data,
    'install_requires': install_requires,
    'python_requires': '>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*',
}


setup(**setup_kwargs)
