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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for betfair_data-0.2.1-cp36-abi3-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 52f5d6326c9ede8084f9c89fb967dd46b4760a8660e6016634164f7d7db36b72 |
|
MD5 | 6881cb6ad80e4a8446486849c93816f3 |
|
BLAKE2b-256 | 276ec593e101addca3e7ff3b454034cf2bb3cf84f1c01773d2880f70d8833323 |
Hashes for betfair_data-0.2.1-cp36-abi3-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d39036fe2db41546b82edf7075e63e1b4653d3ef7e020fdab09984becf889e5c |
|
MD5 | f1baf288cf31cdc711011b3b783aa75e |
|
BLAKE2b-256 | 6e3ab77a44d01cf37136cea76275bd33f14df6061366bd2675d57fbfac8f04b9 |
Hashes for betfair_data-0.2.1-cp36-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 75d3bb72af7eeafc82048bf851c303fbe1f77b203b374ee695c138639fc202f6 |
|
MD5 | 0d13a72f7a1c40465c4724c79086940e |
|
BLAKE2b-256 | 002e5c2d7ccacd6b4f0c1439e2da457c2975ef57cdd65d0ab93421ba44728216 |
Hashes for betfair_data-0.2.1-cp36-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 87d394650960a19116f1a368f6f8782b04cb09ef9256874b63f9d6a10379f284 |
|
MD5 | 036606a19ac6c890867134871d747ed9 |
|
BLAKE2b-256 | d249d10c116a6ae5840a389e739bdace527b5145890c28e1e431b9de585e66ef |
Hashes for betfair_data-0.2.1-cp36-abi3-manylinux_2_17_ppc64.manylinux2014_ppc64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 62eb32996f7d9c22313dbd41d901d9cb8eb7f59f7ba13cb9968a4667e0f81e77 |
|
MD5 | 1fde46856d7d931491bc9984ae3df340 |
|
BLAKE2b-256 | 35432a6e98bef7472d0deb0cd61411612cc9b093fb2e64edc4eb86ef477fa129 |
Hashes for betfair_data-0.2.1-cp36-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3bf276691248de1c7bfe99fefb48b127bbccd7959c529faad84b29196a4da21a |
|
MD5 | b639303e715199cc6e8f15d23b2aa17b |
|
BLAKE2b-256 | c549984b2aec4e33a8da68c61e520f4820e4069879be90e3730c7dce95e66f9e |
Hashes for betfair_data-0.2.1-cp36-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a56cd46d922c8b2a7d72640f3bb78890687d7fd16cee6093f67c1feb856aeea4 |
|
MD5 | 9d31eac0b46e6d4407454acb2847377a |
|
BLAKE2b-256 | 2cebd11711fc2f9089e3f1178dc2fd0ddff122e521b2670bc65776e189ecbb3d |
Hashes for betfair_data-0.2.1-cp36-abi3-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 207f66c3405072ffc527232208435f2c5bc682c56d789cce706091846d2b1d40 |
|
MD5 | 94795d01fb2e67f1fbd1fc7c0cf06650 |
|
BLAKE2b-256 | ba3d6864ec0be4c2d59bf2cc6f9e32bc2e70b8fefe14ccca587b325520e2a27d |
Hashes for betfair_data-0.2.1-cp36-abi3-manylinux_2_5_i686.manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3fda86f33905cd09024ec8ca9ac7258b699839657573c5fc05cde5cdc55fa3f |
|
MD5 | bad11a74d1d86baa7500f9b28c1c2b3e |
|
BLAKE2b-256 | 054d3d623cf9ccb9a959f6b09b038ff801a9e033a5a5064096d3a2b53b31bb57 |
Hashes for betfair_data-0.2.1-cp36-abi3-macosx_10_9_x86_64.macosx_11_0_arm64.macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 183ecb3adf02097cb2ae109b571fc3b6366b7c1c03f15fdcba489476ff4dc716 |
|
MD5 | 67db2083906dd82ea3073d385a78872c |
|
BLAKE2b-256 | bcccd2181aebb890fc579578eb7ee922cb7785e7b44356d47a10d7781f77383e |
Hashes for betfair_data-0.2.1-cp36-abi3-macosx_10_7_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51c1bd321ef26f2d96b7c31f25eac7778cf26011fc1f68f16d42587a2c055bbe |
|
MD5 | d64ff40da9eb10bb5ba410f69792e6c2 |
|
BLAKE2b-256 | 0d94d25f65bc86e50c09601a3656aa648865940cafb51f347e8b500595f85cbf |