ygo 1.0.1__tar.gz → 1.0.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of ygo might be problematic. Click here for more details.

@@ -1,13 +1,12 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ygo
3
- Version: 1.0.1
3
+ Version: 1.0.2
4
4
  Project-URL: homepage, https://github.com/link-yundi/ygo
5
5
  Project-URL: repository, https://github.com/link-yundi/ygo
6
6
  Requires-Python: >=3.8
7
7
  Description-Content-Type: text/markdown
8
8
  License-File: LICENSE
9
9
  Requires-Dist: clickhouse-driver>=0.2.9
10
- Requires-Dist: duckdb>=1.2.2
11
10
  Requires-Dist: dynaconf>=3.2.11
12
11
  Requires-Dist: joblib>=1.4.2
13
12
  Requires-Dist: loguru>=0.7.3
@@ -4,13 +4,12 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "ygo"
7
- version = "1.0.1"
7
+ version = "1.0.2"
8
8
  description = ""
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.8"
11
11
  dependencies = [
12
12
  "clickhouse-driver>=0.2.9",
13
- "duckdb>=1.2.2",
14
13
  "dynaconf>=3.2.11",
15
14
  "joblib>=1.4.2",
16
15
  "loguru>=0.7.3",
@@ -7,16 +7,28 @@ Created on 2025/5/14 18:29
7
7
  ---------------------------------------------
8
8
  """
9
9
 
10
- from .client import HOME, CATDB, SETTINGS, sql, put, create_engine_ck, create_engine_mysql, read_mysql, read_ck
10
+ from .client import (
11
+ HOME,
12
+ CATDB,
13
+ get_settings,
14
+ sql,
15
+ put,
16
+ create_engine_ck,
17
+ create_engine_mysql,
18
+ read_mysql,
19
+ read_ck,
20
+ tb_path,
21
+ )
11
22
 
12
23
  __all__ = [
13
24
  "HOME",
14
25
  "CATDB",
15
- "SETTINGS",
26
+ "get_settings",
16
27
  "sql",
17
28
  "put",
18
29
  "create_engine_ck",
19
30
  "create_engine_mysql",
20
31
  "read_mysql",
21
32
  "read_ck",
33
+ "tb_path",
22
34
  ]
@@ -8,21 +8,21 @@ Created on 2024/7/1 09:44
8
8
  """
9
9
  import os
10
10
  import re
11
+ from functools import partial
11
12
  from typing import Optional
12
- from .yck import connect, query_polars
13
13
 
14
- import duckdb
15
14
  import polars as pl
16
- import ylog
17
15
  from dynaconf import Dynaconf
18
16
  from sqlalchemy import create_engine
19
- from functools import partial
20
17
 
18
+ import ylog
21
19
  from .parse import extract_table_names_from_sql
20
+ from .yck import connect, query_polars
22
21
 
23
22
  # 配置文件在 “~/.catdb/setting.toml”
24
23
  USERHOME = os.path.expanduser('~') # 用户家目录
25
- CONFIG_PATH = os.path.join(USERHOME, ".catdb", "settings.toml")
24
+ NAME = "catdb"
25
+ CONFIG_PATH = os.path.join(USERHOME, f".{NAME}", "settings.toml")
26
26
  if not os.path.exists(CONFIG_PATH):
27
27
  try:
28
28
  os.makedirs(os.path.dirname(CONFIG_PATH))
@@ -30,9 +30,9 @@ if not os.path.exists(CONFIG_PATH):
30
30
  ...
31
31
  except Exception as e:
32
32
  ylog.error(f"配置文件生成失败: {e}")
33
- catdb_path = os.path.join(USERHOME, "catdb")
33
+ catdb_path = os.path.join(USERHOME, NAME)
34
34
  template_content = f"""[paths]
35
- catdb="{catdb_path}" # 本地数据库,默认家目录
35
+ {NAME}="{catdb_path}" # 本地数据库,默认家目录
36
36
 
37
37
  ## 数据库配置:
38
38
  [database]
@@ -54,17 +54,19 @@ catdb="{catdb_path}" # 本地数据库,默认家目录
54
54
 
55
55
  def get_settings():
56
56
  try:
57
- return Dynaconf(settings_files=[CONFIG_PATH])
57
+ return Dynaconf(settings_files=[CONFIG_PATH]).as_dict()
58
58
  except:
59
- return
59
+ return {}
60
60
 
61
61
 
62
62
  HOME = USERHOME
63
- CATDB = os.path.join(HOME, "catdb")
63
+ CATDB = os.path.join(HOME, NAME)
64
64
  # 读取配置文件覆盖
65
65
  SETTINGS = get_settings()
66
66
  if SETTINGS is not None:
67
- CATDB = SETTINGS.paths.catdb
67
+ CATDB = SETTINGS["PATHS"][NAME]
68
+ if not CATDB.endswith(NAME):
69
+ CATDB = os.path.join(CATDB, NAME)
68
70
 
69
71
 
70
72
  # ======================== 本地数据库 catdb ========================
@@ -82,19 +84,12 @@ def tb_path(tb_name: str) -> str:
82
84
  """
83
85
  return os.path.join(CATDB, tb_name)
84
86
 
85
- def put(df: pl.DataFrame, tb_name: str, partitions: Optional[list[str]] = None):
86
- """
87
- 将数据写入duck_db支持的parquet格式文件
88
- Parameters
89
- ----------
90
- df: pandas.DataFrame | pandas.Series | polars.DataFrame
91
- 写入的数据
92
- tb_name: str
93
- 表名,支持路径写法, a/b/c
94
- partitions: Optional[List[str]]
95
- 根据哪些字段进行分区,默认不分区
96
- """
97
- tbpath = tb_path(tb_name)
87
+
88
+ def put(df: pl.DataFrame, tb_name: str, partitions: Optional[list[str]] = None, abs_path: bool = False):
89
+ if not abs_path:
90
+ tbpath = tb_path(tb_name)
91
+ else:
92
+ tbpath = tb_name
98
93
  if not os.path.exists(tbpath):
99
94
  try:
100
95
  os.makedirs(tbpath)
@@ -106,41 +101,30 @@ def put(df: pl.DataFrame, tb_name: str, partitions: Optional[list[str]] = None):
106
101
  df.write_parquet(tbpath, partition_by=partitions)
107
102
 
108
103
 
109
- def sql(query: str):
110
- """
111
- 从duckdb中读取数据, query语法与mysql一致,特殊语法请查duckdb官网: https://duckdb.org/docs/sql/query_syntax/select
112
- Parameters
113
- ----------
114
- query: str
115
- 查询语句
116
- Returns
117
- -------
118
- result: duckdb.DuckDBPyRelation
119
- 查询结果
120
- """
104
+ def sql(query: str, abs_path: bool = False):
121
105
  tbs = extract_table_names_from_sql(query)
122
106
  convertor = dict()
123
107
  for tb in tbs:
124
- db_path = tb_path(tb)
125
- format_tb = f"read_parquet('{db_path}/**/*.parquet', hive_partitioning = true)"
108
+ if not abs_path:
109
+ db_path = tb_path(tb)
110
+ else:
111
+ db_path = tb
112
+ format_tb = f"read_parquet('{db_path}/**/*.parquet')"
126
113
  convertor[tb] = format_tb
127
114
  pattern = re.compile("|".join(re.escape(k) for k in convertor.keys()))
128
115
  new_query = pattern.sub(lambda m: convertor[m.group(0)], query)
129
- conn = duckdb.connect()
130
- conn.execute("PRAGMA disable_progress_bar;")
131
- conn.execute("PRAGMA threads=1;")
132
- res = conn.execute(new_query).fetch_arrow_table()
133
- res = pl.from_arrow(res)
134
- conn.close()
135
- return res
116
+ return pl.sql(new_query).collect()
117
+
136
118
 
137
119
  def create_engine_ck(urls: list[str], user: str, password: str):
138
120
  return partial(connect, urls, user, password)
139
121
 
140
- def read_ck(sql, eng)->pl.DataFrame:
122
+
123
+ def read_ck(sql, eng) -> pl.DataFrame:
141
124
  with eng() as conn:
142
125
  return query_polars(sql, conn)
143
126
 
127
+
144
128
  def create_engine_mysql(url, user, password, database):
145
129
  """
146
130
  :param url: <host>:<port>
@@ -152,6 +136,7 @@ def create_engine_mysql(url, user, password, database):
152
136
  engine = create_engine(f"mysql+pymysql://{user}:{password}@{url}/{database}")
153
137
  return engine
154
138
 
139
+
155
140
  def read_mysql(sql, eng) -> pl.DataFrame:
156
141
  with eng.connect() as conn:
157
- return pl.read_database(sql, conn)
142
+ return pl.read_database(sql, conn)
@@ -33,7 +33,7 @@ def extract_table_names_from_sql(sql_query):
33
33
  # 遍历解析后的语句块
34
34
  for statement in parsed:
35
35
  # 转换为字符串
36
- statement_str = str(statement).lower()
36
+ statement_str = str(statement)# .lower()
37
37
 
38
38
  # 将字符串中的特殊语法置空
39
39
  statement_str = re.sub(r'(substring|extract)\s*\(((.|\s)*?)\)', '', statement_str)
@@ -1,13 +1,12 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: ygo
3
- Version: 1.0.1
3
+ Version: 1.0.2
4
4
  Project-URL: homepage, https://github.com/link-yundi/ygo
5
5
  Project-URL: repository, https://github.com/link-yundi/ygo
6
6
  Requires-Python: >=3.8
7
7
  Description-Content-Type: text/markdown
8
8
  License-File: LICENSE
9
9
  Requires-Dist: clickhouse-driver>=0.2.9
10
- Requires-Dist: duckdb>=1.2.2
11
10
  Requires-Dist: dynaconf>=3.2.11
12
11
  Requires-Dist: joblib>=1.4.2
13
12
  Requires-Dist: loguru>=0.7.3
@@ -1,5 +1,4 @@
1
1
  clickhouse-driver>=0.2.9
2
- duckdb>=1.2.2
3
2
  dynaconf>=3.2.11
4
3
  joblib>=1.4.2
5
4
  loguru>=0.7.3
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes