Skip to content
This repository has been archived by the owner on May 19, 2019. It is now read-only.

Commit

Permalink
FilterUnnecessaryEvents (#102)
Browse files Browse the repository at this point in the history
* Added func exclude_unpopular: removes unnecessary events that was
referred in issue #87

* Less patterns

* Redundant parameter "r"
  • Loading branch information
nadyr-mg authored Feb 15, 2018
1 parent 84cf301 commit 6415d29
Show file tree
Hide file tree
Showing 9 changed files with 498 additions and 2 deletions.
25 changes: 25 additions & 0 deletions surebet/handling/excluding.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from re import search

from surebet import find_in_iter, reverse_enum
from surebet.handling.surebets import Surebets, Surebet

women_patterns = ["\([Ww]\)"]
other_patterns = ["\([Rr]\)", "U\d+", "\(U\d+\)", "U-\d+", "Reserves"]


def _del_equal(found_obj, posit_obj) -> None:
for attr, found_iter in found_obj.__dict__.items():
Expand All @@ -20,3 +25,23 @@ def _del_equal(found_obj, posit_obj) -> None:
def exclude_posit(found_surebets: Surebets, posit_surebets: Surebets) -> None:
_del_equal(found_surebets, posit_surebets)
found_surebets.format()


def exclude_unpopular(surebets):
for book in surebets.books_surebets:
for sport_name, sport in book.attrs_dict().items():
patterns = list(other_patterns)
if sport_name != "volley": # volleyball women teams are allowed
patterns += women_patterns

for idx, event in reverse_enum(sport):
if is_unpopular(patterns, event):
del sport[idx]


def is_unpopular(patterns, event):
for pattern in patterns:
for teams in (event.teams1, event.teams2):
if search(pattern, teams[0]) or search(pattern, teams[1]):
return True
return False
4 changes: 3 additions & 1 deletion surebet/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from surebet.bookmakers import Posit, Fonbet, Marat, Olimp
from surebet.handling.detailed_surebets import convert_to_detailed
from surebet.handling.excluding import exclude_posit
from surebet.handling.excluding import exclude_posit, exclude_unpopular
from surebet.handling.searching import find_surebets
from surebet.handling.surebets import Surebets
from surebet.loading.selenium import SeleniumService
Expand Down Expand Up @@ -29,6 +29,8 @@ def start_scanning(iter_num):

exclude_posit(surebets, posit_surebets)

exclude_unpopular(surebets)

surebets.set_timestamps(old_surebets)
old_surebets = surebets

Expand Down
2 changes: 1 addition & 1 deletion surebet/tests/handling/test_exclude.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_known_result():
surebets_other, surebets_posit = read_sample("known.pkl")
exclude_posit(surebets_other, surebets_posit)

with open(path.join(resource_dir, "knownResult.json"), "r") as file_result:
with open(path.join(resource_dir, "knownResult.json")) as file_result:
surebets_result = json.load(file_result)

assert obj_dumps(surebets_other) == json_dumps(surebets_result)
Expand Down
37 changes: 37 additions & 0 deletions surebet/tests/handling/test_unpopular.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import logging
import pickle
import json
from os import path

from surebet.tests.handling import package_dir
from surebet.json_funcs import json_dumps, obj_dumps
from surebet.handling.excluding import exclude_unpopular

resource_dir = path.join(package_dir, "unpopular")


def read_sample(filename):
with open(path.join(resource_dir, filename), "rb") as found_file:
obj = pickle.load(found_file)
return obj


def test_sample():
for sample_num in range(3):
filename = 'sample{}.pkl'.format(sample_num)
surebets = read_sample(filename)
exclude_unpopular(surebets)

logging.info("PASS: samples")


def test_known_result():
surebets = read_sample("known.pkl")
exclude_unpopular(surebets)

with open(path.join(resource_dir, "knownResult.json")) as file_result:
surebets_result = json.load(file_result)

assert obj_dumps(surebets) == json_dumps(surebets_result)

logging.info("PASS: known")
Binary file added surebet/tests/handling/unpopular/known.pkl
Binary file not shown.
Loading

0 comments on commit 6415d29

Please sign in to comment.