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/Gzip encoded data on worker threads and is ideal for parsing large quantities of historic data that could otherwise take hours or days to parse.

This library is a work in progress and is still subject to breaking changes.

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).mutable():
    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}")

Loading Files

You can read in self recorded stream files. Make sure to set cumulative_runner_tv to False for self recorded files to make sure you get the correct runner and market volumes.

import betfair_data
import glob

paths = glob.glob("data/*.gz")
files = betfair_data.Files(paths, cumulative_runner_tv=False)

Or you can read official Betfair Tar archives with bz2 encoded market files.

import betfair_data
import glob

paths = glob.glob("data/*.tar")
files = betfair_data.TarBz2(paths, cumulative_runner_tv=True)

Or load the file through any other means and pass the bytes and name into the object constructors.

# generator to read in files
def load_files(paths: str):
    for path in glob.glob(paths, recursive=True):
        with open(path, "rb") as file:
            yield (path, file.read())

# iterate over the files and convert into bflw iterator
for name, bs in load_files("markets/*.json"):
    for market_books in bflw.BflwIter(name, bs):
        for market_book in market_books:
            # do stuff
            pass

Object Types

You can use differnt styles of objects, with pros or depending on your needs

Mutable objects, generally the fastest, but can be hard to use. If you find yourself calling market.copy a lot, you may find immutable faster

# where files is loaded from a TarBz2 or Files source like above
mut_iter = files.mutable()
for market in mut_iter: # different markets per file
    while market.update(): # update the market in place
        pass

Immutable objects, slightly slower but can be easier to use. Equilivent of calling market.copy() on every update but faster, as only objects that change make new copies. NOT YET FINISHED

immut_iter = files.immutable()
for market_iter in immut_iter: # different files
    for market in market_iter: # each update of a market/file
        pass

Betfairlightweight compatible version, drop in replacement for bflw objects.

bflw_iter = files.bflw()
for file in bflw_iter: # different files
    for market_books in file: # different books per update
        for market in market_books: # each update of a market
            pass

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.

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.2.1.tar.gz (9.2 MB view hashes)

Uploaded Source

Built Distributions

betfair_data-0.2.1-cp36-abi3-win_amd64.whl (464.4 kB view hashes)

Uploaded CPython 3.6+ Windows x86-64

betfair_data-0.2.1-cp36-abi3-win32.whl (438.9 kB view hashes)

Uploaded CPython 3.6+ Windows x86

betfair_data-0.2.1-cp36-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (1.3 MB view hashes)

Uploaded CPython 3.6+ manylinux: glibc 2.17+ s390x

betfair_data-0.2.1-cp36-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (940.2 kB view hashes)

Uploaded CPython 3.6+ manylinux: glibc 2.17+ ppc64le

betfair_data-0.2.1-cp36-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl (932.6 kB view hashes)

Uploaded CPython 3.6+ manylinux: glibc 2.17+ ppc64

betfair_data-0.2.1-cp36-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (929.3 kB view hashes)

Uploaded CPython 3.6+ manylinux: glibc 2.17+ ARMv7l

betfair_data-0.2.1-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (895.3 kB view hashes)

Uploaded CPython 3.6+ manylinux: glibc 2.17+ ARM64

betfair_data-0.2.1-cp36-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.whl (901.9 kB view hashes)

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

betfair_data-0.2.1-cp36-abi3-manylinux_2_5_i686.manylinux1_i686.whl (928.1 kB view hashes)

Uploaded CPython 3.6+ manylinux: glibc 2.5+ i686

betfair_data-0.2.1-cp36-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl (1.2 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.2.1-cp36-abi3-macosx_10_7_x86_64.whl (595.5 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