Skip to main content

Add your description here

Project description

使用教程

安装

pip install pgsqldatatool

date_clean(数据清洗)

# 基础清洗 (去掉列名两端空白字符、去掉数据两段空白字符、删除空行)
lf_basic_clean(df)

# 根据字典重命名列
lf_rename_cols

# 移除重复行
lf_remove_dup_rows

# 删除指定列
lf_remove_cols

# 移除数字千分号
lf_remove_per_mille

# 移除数字百分号
lf_remove_percent

# 添加时间列
lf_add_time

# 删除完全为空的列 -- 这一步不是惰性计算,可能会降低性能
df_drop_empty_cols

示例

from pgsqldatatool import data_clean as dc
df = pl.read_excel(r"D:\manji\Downloads\判断中国域名.xlsx")
df = dc.lf_basic_clean(df)
df = dc.lf_remove_cols(df,["域名22"])
df = dc.lf_remove_dup_rows(df)
df = dc.df_drop_empty_cols(df)
df = dc.lf_remove_dup_rows(df)
df = df.collect()
print(df)

PoolSingleton(连接池单例模式)

归还连接(Release):把连接还给池子,让别的任务继续用。(async with 已经帮你自动做了) 关闭连接池(Close):彻底断开与数据库的所有连接,销毁这个池子。这通常只在整个程序/服务准备退出时才需要做。

from pgsqldatatool import PoolSingleton

# 示例1:使用异步连接池
async def main_test_1():

    # 调用方式1,
    async with  PoolSingleton.acquire() as conn:
        records = await conn.fetch(""" SELECT * FROM public."test20260211" """)
        print(records)

    # 销毁整个连接池
    await PoolSingleton.close()
# 示例2:使用静态方法
async def main_test_2():
    # 调用方式2:使用静态方法
    records2 = await PoolSingleton.fetch(""" SELECT * FROM public."test20260211" """)
    print(records2)

    # 调用方式3:使用静态方法
    records3 = await PoolSingleton.fetchrow(""" SELECT * FROM public."test20260211" """)
    print(records3)

    # 调用方式4:使用静态方法
    records4 = await PoolSingleton.execute(""" SELECT * FROM public."test20260211" """)
    print(records4)
    
    
if __name__ == "__main__":
    asyncio.run(main_test_1())

创建异步数据库链接

推荐方案:asyncpg(性能最好,原生 asyncio)

适合:FastAPI / aiohttp / asyncio 项目

pip install asyncpg
import asyncpg
import asyncio

async def main():
    # 创建连接
    conn = await asyncpg.connect(
        host="localhost",
        port=5432,
        user="postgres",
        password="password",
        database="testdb"
    )

    # 查询
    row = await conn.fetchrow("SELECT NOW()")
    print(row)

    # 参数化查询
    rows = await conn.fetch(
        "SELECT * FROM users WHERE age > $1",
        18
    )

    await conn.close()

asyncio.run(main())

使用连接池(生产必选 ✅)

import asyncpg
import asyncio

async def get_pool():
    return await asyncpg.create_pool(
        dsn="postgresql://postgres:password@localhost:5432/testdb",
        min_size=5,
        max_size=20
    )

async def main():
    pool = await get_pool()

    async with pool.acquire() as conn:
        result = await conn.fetch("SELECT * FROM users")

    await pool.close()

asyncio.run(main())

事务

async with conn.transaction():
    await conn.execute(
        "INSERT INTO users(name) VALUES($1)",
        "Alice"
    )

SQLAlchemy 2.0 + asyncpg(ORM 场景)

适合:需要 ORM、多数据库兼容

pip install sqlalchemy[asyncio] asyncpg
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
from sqlalchemy import select

engine = create_async_engine(
    "postgresql+asyncpg://postgres:password@localhost/testdb",
    pool_size=10,
    max_overflow=20
)

AsyncSessionLocal = sessionmaker(
    engine, class_=AsyncSession, expire_on_commit=False
)

async def query_users():
    async with AsyncSessionLocal() as session:
        result = await session.execute(select(User))
        return result.scalars().all()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgsqldatatool-1.0.1.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pgsqldatatool-1.0.1-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file pgsqldatatool-1.0.1.tar.gz.

File metadata

  • Download URL: pgsqldatatool-1.0.1.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pgsqldatatool-1.0.1.tar.gz
Algorithm Hash digest
SHA256 c746b986b5b653ab99b477d8098688ce249c2e7f820f4b5b82b82fe15f91f5b3
MD5 243022b10b8ad941fe13127dabeeb05f
BLAKE2b-256 5a089ba1eb2b465151824a2010d5b4e5669d9cbe975ec254caa6e4c9898ab9b4

See more details on using hashes here.

File details

Details for the file pgsqldatatool-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pgsqldatatool-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.11 {"installer":{"name":"uv","version":"0.10.11","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for pgsqldatatool-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 505c7d30cbf22e44ddebc279981b111b4e6d12242f2a235588cf68fd39afee38
MD5 fe5a7574db03d21443a9c0a8ced2be3a
BLAKE2b-256 1275396ff487e408ee35c9fdd0f5c6b60d4e72ead64586e36471c7d3682292e3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page