Skip to content
g21589 edited this page Aug 20, 2024 · 18 revisions

Polars + S3

import s3fs
import numpy as np
import pandas as pd
import polars as pl
import pyarrow.dataset as ds
from pyarrow import fs


storage_options = {
    "key": "",
    "secret": "",
    "client_kwargs": {
        "endpoint_url": "http://127.0.0.1:9000"
    }
}

storage_options2 = {
    "access_key_id": "",
    "secret_access_key": "",
    "endpoint_url": "http://127.0.0.1:9000"
}

source = "s3://data/tripdata/*.parquet"

# fs = fs.S3FileSystem(endpoint_override=storage_options['client_kwargs']['endpoint_url'].replace("http://", "")), 
#                      access_key=storage_options['key'], 
#                      secret_key=storage_options['secret'])
# fs = s3fs.S3FileSystem(endpoint_url=storage_options['client_kwargs']['endpoint_url'], 
#                        key=storage_options['key'], 
#                        secret=storage_options['secret'])

# dset = ds.dataset("s3://data/tripdata/yellow_tripdata_2024-01.parquet", format="parquet", filesystem=fs)
# df = (
#     pl.scan_pyarrow_dataset(dset)
#         .collect()
# )

df = (
    pl.scan_parquet(source, storage_options=storage_options2)
        .select(['VendorID', 'store_and_fwd_flag', 'tip_amount'])
        .filter(pl.col('store_and_fwd_flag') == 'N')
        .group_by(['VendorID'])
        .quantile(0.5)
        .collect()
)

Logging

https://docs.python.org/3/howto/logging-cookbook.html#logging-to-a-single-file-from-multiple-processes

Pandas

https://medium.com/bigdatarepublic/advanced-pandas-optimize-speed-and-memory-a654b53be6c2

Async I/O

import asyncio, nest_asyncio
nest_asyncio.apply() # Fix asyncio bug in IPython (RuntimeError: This event loop is already running)

async def example1(): # 定義一個中間會被中斷的協程
    print("Start example1 coroutin.")
    await asyncio.sleep(1) # 中斷協程一秒
    print("Finish example1 coroutin.")

async def example2(): # 定義一個協程
    print("Start example2 coroutin.")
    # do some process...
    print("Finish example2 coroutin.")

tasks = [
    asyncio.ensure_future(example1()),
    asyncio.ensure_future(example2()),
]
    
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.wait(tasks))

Plotly

https://pypi.org/project/kaleido/#files

SQLAlchemy

FastAPI-with-SqlAlchemy

Kaleido

void OnHeadlessBrowserStarted(headless::HeadlessBrowser* browser)

Time: 26.1649 sec Time: 31.2114 sec

Time: 3.2116 sec Time: 8.1961 sec

Time: 0.412 sec

Clone this wiki locally