Skip to content

Instantly share code, notes, and snippets.

View StephenFordham's full-sized avatar

Stephen Fordham StephenFordham

View GitHub Profile
@StephenFordham
StephenFordham / Serving_data_scrape.py
Created August 7, 2019 12:12
Serving_data_scrape
for i in tqdm(range(1, 21)):
player = WebDriverWait(browser, 20).until(expected_conditions.visibility_of_element_located((By.XPATH, '//*[@id="rankingDetailAjaxContainer"]/table/tbody/tr['+str(i)+']/td[4]/a')))
Tennis_data_collection.write(player.text + ',')
WebDriverWait(browser, 20).until(expected_conditions.visibility_of_element_located((By.XPATH, '//*[@id="rankingDetailAjaxContainer"]/table/tbody/tr['+str(i)+']/td[4]/a'))).click()
turned_pro = WebDriverWait(browser, 20).until(expected_conditions.visibility_of_element_located((By.XPATH, '//*[@id="playerProfileHero"]/div[2]/div[2]/div/table/tbody/tr[1]/td[2]/div/div[2]'))).text
career_length = 2019 - int(turned_pro)
Tennis_data_collection.write(str(career_length) + ',')
WebDriverWait(browser, 20).until(expected_conditions.visibility_of_element_located((By.XPATH, '//*[@id="profileTabs"]/ul/li[6]/a'))).click()
@StephenFordham
StephenFordham / PRC_skillful_model.py
Created October 23, 2023 09:51
Precision-recall curve for a skillful model
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import precision_recall_curve, auc
import seaborn as sns
X, y = make_classification(n_samples=1000, n_classes=2, random_state=1)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=2)
model = LogisticRegression(solver='lbfgs')
@StephenFordham
StephenFordham / AUC_ROC_model.py
Last active October 23, 2023 08:26
AUC ROC Skillful model comparison
import matplotlib.pyplot as plt
# ROC and AUC modules
from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import roc_curve, roc_auc_score
import seaborn as sns
# generate 2 class dataset
X, y = make_classification(n_samples=1000, n_classes=2, weights=[0.5], random_state=1)
@StephenFordham
StephenFordham / default_decorator_arg.py
Created March 24, 2020 18:25
default_decorator_arg
def meta_decorator(arg):
def decorator_list(fnc):
def inner(list_of_tuples):
return [(fnc(val[0], val[1])) ** power for val in list_of_tuples]
return inner
if callable(arg):
power = 2
return decorator_list(arg)
else:
power = arg
@StephenFordham
StephenFordham / method_decorators_example2.py
Created May 13, 2020 09:13
method_decorators_example2
def integer_check(method):
def inner(ref, expo=None):
if expo == None:
if not isinstance(ref._val1, int) or not isinstance(ref._val2, int):
raise TypeError('Please enter numerical numbers for val1 and val2')
else:
return method(ref)
if expo:
if not isinstance(ref._val1, int) or not isinstance(ref._val2, int)\
or not isinstance(expo, int):
# Class Decorators: Using Decorators with methods defined in a Class
def integer_check(method):
def inner(ref):
if not isinstance(ref._val1, int) or not isinstance(ref._val2, int):
raise TypeError('val1 and val2 must be integers')
else:
return method(ref)
return inner
@StephenFordham
StephenFordham / custom_iterator_object.py
Created June 5, 2020 08:33
custom_iterator_object
class CustomIterTeams(object):
def __init__(self, division, teams=[]):
self._mng = division
self._teams = teams
self._index = -1
def __iter__(self):
return self
# def __iter__(self):
@StephenFordham
StephenFordham / init_constructor_decorator.py
Created June 9, 2022 18:08
init_constructor_decorator
# __init__ method with a decorator
def attr_validation(method):
def inner(ref, name, age, location):
for attr in [name, location]:
if not isinstance(attr, str):
raise TypeError('Name and Location attributes must be of type str')
if not isinstance(age, int):
raise TypeError('Age attributes must be of type int')
return method(ref, name, age, location)
return inner
@StephenFordham
StephenFordham / __setattr__part2.py
Created June 9, 2022 17:09
__setattr__catching_exceptions
e1 = Employees(46347832, 30, 'Bournemouth')
for key, value in e1.__dict__.items():
print('{}: {}'.format(key, value))
#Console output
TypeError: Only valid attributes of type string are accepted
@StephenFordham
StephenFordham / __setattr__magic_method.py
Created June 9, 2022 17:04
__setattr__magic_method
class Employees(object):
def __init__(self, name, age, location):
self._name = name
self._age = age
self._location = location
def __setattr__(self, key, value):
if key in ['_name', '_location']:
if not isinstance(value, str):
raise TypeError('Only valid attributes of type string are accepted')