Skip to main content

Fast Python Betfair historic data file parser

Project description

Betfair Data

Betfair Data is a very fast Betfair historical data file parsing library for python. It currently supports tar archives containing BZ2 compressed NLJSON files (the standard format provided by Betfair's historic data portal).

The library is written in Rust and uses advanced performance enhancing techniques, like in place json deserialization and decompressing Bz2 encoded data on worker threads and is ideal for parsing large quantities of historic data that could otherwise take hours or days to parse.

Installation

pip install betfair_data

Note: requires Python >= 3.6.

Example

import betfair_data

paths = [
    "data/2021_12_DecRacingAUPro.tar",
    "data/2021_10_OctRacingAUPro.tar",
    "data/2021_11_NovRacingAUPro.tar",
]

market_count = 0
update_count = 0

for market in betfair_data.TarBz2(paths):
    market_count += 1
    update_count += 1
    
    while market.update():
        update_count += 1

    print(f"Markets {market_count} Updates {update_count}", end='\r')
print(f"Markets {market_count} Updates {update_count}")

Types

IDE's should automatically detect the types and provide checking and auto complete. See the pyi stub file for a comprehensive view of the types and method available.


Benchmarks

Betfair Data (this) Betfairlightweight
3m 37sec 1hour 1min 45sec
~101 markets/sec ~6 markets/sec
~768,000 updates/sec ~45,500 updates/sec

Benchmarks were run against 3 months of Australian racing markets comprising roughly 22,000 markets. Benchmarks were run on a M1 Macbook Pro with 32GB ram.

These results should only be used as a rough comparison, different machines, different sports and even different months can effect the performance and overall markets/updates per second.

No disrespect is intended towards betfairlightweight, which remains an amazing library and a top choice for working with the Betfair API. Every effort was made to have its benchmark below run as fast as possible, and any improvements are welcome.


Betfair_Data benchmark show in the example above.

Betfairlightweight Benchmark
from typing import Sequence 

import unittest.mock
import tarfile
import bz2
import betfairlightweight

trading = betfairlightweight.APIClient("username", "password", "appkey")
listener = betfairlightweight.StreamListener(
    max_latency=None, lightweight=True, update_clk=False, output_queue=None, cumulative_runner_tv=True, calculate_market_tv=True
)

paths = [ 
    "data/2021_10_OctRacingAUPro.tar",
    "data/2021_11_NovRacingAUPro.tar",
    "data/2021_12_DecRacingAUPro.tar"
]

def load_tar(file_paths: Sequence[str]):
    for file_path in file_paths:
        with tarfile.TarFile(file_path) as archive:
            for file in archive:
                yield bz2.open(archive.extractfile(file))
    return None

market_count = 0
update_count = 0

for file_obj in load_tar(paths):
    with unittest.mock.patch("builtins.open", lambda f, _: f):  
        stream = trading.streaming.create_historical_generator_stream(
            file_path=file_obj,
            listener=listener,
        )
        gen = stream.get_generator()

        market_count += 1
        for market_books in gen():
            for market_book in market_books:
                update_count += 1

    print(f"Markets {market_count} Updates {update_count}", end='\r')
print(f"Markets {market_count} Updates {update_count}")


Logging

Logging can be enabled and warnings are emitted for IO and JSON errors

import logging

logging.basicConfig(level=logging.WARN, format='%(levelname)s %(name)s %(message)s')

Example logged errors

WARNING betfair_data source: data/2021_10_OctRacingAUPro.tar file: PRO/2021/Oct/4/30970292/1.188542184.bz2 err: (JSON Parse Error) expected value at line 1480 column 1
WARNING betfair_data source: data/2021_10_OctRacingAUPro.tar file: PRO/2021/Oct/8/30985584/1.188739324.bz2 err: (JSON Parse Error) expected `:` at line 1 column 909
WARNING betfair_data source: data/2021_10_OctRacingAUPro.tar file: PRO/2021/Oct/8/30985584/1.188739325.bz2 err: (JSON Parse Error) expected `:` at line 1 column 904
WARNING betfair_data source: data/2021_10_OctRacingAUPro.tar file: PRO/2021/Oct/15/31001342/1.189124831.bz2 err: (JSON Parse Error) expected value at line 1335 column 1

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

betfair_data-0.1.8.tar.gz (27.1 kB view hashes)

Uploaded Source

Built Distributions

betfair_data-0.1.8-cp36-abi3-win_amd64.whl (401.4 kB view hashes)

Uploaded CPython 3.6+ Windows x86-64

betfair_data-0.1.8-cp36-abi3-win32.whl (381.3 kB view hashes)

Uploaded CPython 3.6+ Windows x86

betfair_data-0.1.8-cp36-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (1.2 MB view hashes)

Uploaded CPython 3.6+ manylinux: glibc 2.17+ s390x

betfair_data-0.1.8-cp36-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (877.5 kB view hashes)

Uploaded CPython 3.6+ manylinux: glibc 2.17+ ppc64le

betfair_data-0.1.8-cp36-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (866.4 kB view hashes)

Uploaded CPython 3.6+ manylinux: glibc 2.17+ ppc64

betfair_data-0.1.8-cp36-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (862.3 kB view hashes)

Uploaded CPython 3.6+ manylinux: glibc 2.17+ ARMv7l

betfair_data-0.1.8-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (836.7 kB view hashes)

Uploaded CPython 3.6+ manylinux: glibc 2.17+ ARM64

betfair_data-0.1.8-cp36-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.whl (837.5 kB view hashes)

Uploaded CPython 3.6+ manylinux: glibc 2.5+ x86-64

betfair_data-0.1.8-cp36-abi3-manylinux_2_5_i686.manylinux1_i686.whl (869.6 kB view hashes)

Uploaded CPython 3.6+ manylinux: glibc 2.5+ i686

betfair_data-0.1.8-cp36-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (1.0 MB view hashes)

Uploaded CPython 3.6+ macOS 10.9+ universal2 (ARM64, x86-64) macOS 10.9+ x86-64 macOS 11.0+ ARM64

betfair_data-0.1.8-cp36-abi3-macosx_10_7_x86_64.whl (522.2 kB view hashes)

Uploaded CPython 3.6+ macOS 10.7+ x86-64

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