Skip to main content

K2data内部的数据分析工具包

Project description

k2magic

K2Magic是K2Assets提供的数据分析开发包(以下简称SDK),用于简化Python里访问各类数据库的操作。

安装sdk

执行以下命令安装:

pip install k2magic

使用sdk

基本用法

假设数据库里有关系表table1的结构如下::

CREATE TABLE table1 (
    k_device VARCHAR(255) PRIMARY KEY,
    col1 FLOAT,
    col2 FLOAT,
    col3 FLOAT
);

对上述关系表操作的示例代码:

import pandas as pd
from k2magic.dataframe_db import DataFrameDB

db = DataFrameDB('postgresql+psycopg2://...')
df = pd.DataFrame({'k_device': ['a', 'b', 'c'], 'col1': [1, 2, 3], 'col2': [4, 5, 6]})

db.insert('table1', df)
db.update('table1', df, index_keys=['k_device'])
db.upsert('table1', df, index_keys=['k_device'])
df = db.select('table1', condition='col1 > 1')
df = db.select('table1', limit=3, order_by=['k_device DESC'])
df = db.sql_select('select * from table1')
db.delete('table1')

db.create_table(df, 'table2', primary_keys=['k_device'])
db.drop_table('table2')

切换数据库

SDK兼容多种数据库的方言,用户只需修改初始化DataFrameDB时的连接字符串参数,其余代码不需要修改,即可切换到另一个数据库。 各数据库的连接字符串格式如下:

已经过测试的:

  • postgresql+psycopg2://username:password@hostname/database_name
  • oracle+cx_oracle://username:password@hostname:port/database_name
  • mssql+pymssql://username:password@hostname:port/database_name

未经过测试的:

注意:使用前确保已安装相应的数据库的驱动,例如pip install pymysql

异常处理

SDK进行数据库操作时,如果遇到异常情况会抛出DataFrameDBException,并回滚数据库到原来的状态。用户可以使用try..except捕获此异常进行必要的处理:

try:
    db.insert('table1', df)
except DataFrameDBException as e:
    print('数据插入(部分)失败,操作已回滚')

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

k2magic-0.1.8.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

k2magic-0.1.8-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file k2magic-0.1.8.tar.gz.

File metadata

  • Download URL: k2magic-0.1.8.tar.gz
  • Upload date:
  • Size: 11.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.13

File hashes

Hashes for k2magic-0.1.8.tar.gz
Algorithm Hash digest
SHA256 a410280942d2fef2a18d3cca8c57329227074021cc401241edae936bd234e665
MD5 3ec5a93aece823bd9a29ebc2236e857c
BLAKE2b-256 e8d94ab44159b0c610c56dec2803a2640b42d33d6eb7acf0c16e433f11b475cf

See more details on using hashes here.

File details

Details for the file k2magic-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: k2magic-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.9.13

File hashes

Hashes for k2magic-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 6de81f08aa6968621433f4e0f373b5691eb04c6bb46d9358a29186f25b6de221
MD5 919a346783135aac32178f119f500e26
BLAKE2b-256 122f57dbeb160f8d707821b78c7a37b4d0b3fcc0a728c3857cb7b869a8cc4939

See more details on using hashes here.

Supported by

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