From 9a846094ec282d1fcda8b5a4bf70f2973bae663e Mon Sep 17 00:00:00 2001 From: edeymc Date: Thu, 6 Nov 2025 09:34:41 -0800 Subject: [PATCH] Initial commit --- LICENSE | 9 ++++++ ParaUtils/__init__.py | 4 +++ ParaUtils/utils.py | 67 +++++++++++++++++++++++++++++++++++++++++++ README.md | 3 ++ pyproject.toml | 27 +++++++++++++++++ setup.py | 6 ++++ 6 files changed, 116 insertions(+) create mode 100644 LICENSE create mode 100644 ParaUtils/__init__.py create mode 100644 ParaUtils/utils.py create mode 100644 README.md create mode 100644 pyproject.toml create mode 100644 setup.py diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..4e25542 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2025 Mike + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/ParaUtils/__init__.py b/ParaUtils/__init__.py new file mode 100644 index 0000000..535c12b --- /dev/null +++ b/ParaUtils/__init__.py @@ -0,0 +1,4 @@ +from .utils import * + +__version__ = "0.1.3" +__all__ = 't2f ft2 CSG parse_WPS_rankings'.split() diff --git a/ParaUtils/utils.py b/ParaUtils/utils.py new file mode 100644 index 0000000..8914953 --- /dev/null +++ b/ParaUtils/utils.py @@ -0,0 +1,67 @@ +#Bits and bobs used for many different projects. Junk drawer module +import pandas +import numpy + + +#Useful functions for parsing race-like durations, and normalizing their string reps +def t2f(S): + if not(S or False): + return None + m,s = ('0:%s' % S).split(':')[-2:] + try: + return numpy.round(60*int(m) + float(s),2) + except: + return -1 + +def f2t(S): + if not(S or False) or (int(''.join([c for c in '0'+str(S) if c.isdigit()])) == 0) or (S <= 0): + return '' + S = float(S) + m = int(S/60) + s = S-60*m + hs = s-int(s) + s = int(s) + return f'{m:02}:{s:02}.{round(hs*100):02}' + +#Canada Summer Games eligible events +CSG = pandas.DataFrame([ + {'Stroke':'Butterfly','Distance':50,'SportClass':[1,2,3,4,5,6,7]}, + {'Stroke':'Butterfly','Distance':100,'SportClass':[8,9,10,11,12,13,14]}, + {'Stroke':'Backstroke','Distance':50,'SportClass':[1,2,3,4,5]}, + {'Stroke':'Backstroke','Distance':100,'SportClass':[1,2,6,7,8,9,10,11,12,13,14]}, + {'Stroke':'Breaststroke','Distance':50,'SportClass':[1,2,3]}, + {'Stroke':'Breaststroke','Distance':100,'SportClass':[4,5,6,7,8,9,11,12,13,14]}, + {'Stroke':'Medley','Distance':150,'SportClass':[1,2,3,4]}, + {'Stroke':'Medley','Distance':200,'SportClass':[5,6,7,8,9,10,11,12,13,14]}, + {'Stroke':'Freestyle','Distance':50,'SportClass':[1,2,3,4,5,6,7,8,9,10,11,12,13,14]}, + {'Stroke':'Freestyle','Distance':100,'SportClass':[1,2,3,4,5,6,7,8,9,10,11,12,13,14]}, + {'Stroke':'Freestyle','Distance':200,'SportClass':[1,2,3,4,5,14]}, + {'Stroke':'Freestyle','Distance':400,'SportClass':[6,7,8,9,10,11,12,13]}, +]).explode('SportClass').reset_index(drop=True) + +def parse_WPS_rankings(DF): + '''Takes a pandas dataframe, usually loaded from an excel file, and cleans it up for use''' + return ( + DF + #Makes .dot notation easier + .pipe(lambda df: df.rename(columns={c:c.replace(' ','_') for c in df.columns})) + + #Drop VACANT events and relay results + .pipe(lambda df: df[df.SDMS_ID.notnull()]) + + #Parse out event information into useful columns + .pipe(lambda df: pd.concat([df,df.Event.str.extract(r"'s (?P\d+) m (Individual )*(?P[A-z]+) [SBM]+(?P\d+)")],axis=1)) + + #Cleanup column dtypes + .assign(Date=lambda df: pd.to_datetime(df.Date)) + .assign(Year=lambda df: df.Date.dt.year) + .assign(Seconds=lambda df: df.Result.apply(t2f)) + .astype({'SportClass':int,'Distance':int,'SDMS_ID':int}) + + #Drop excess columns + .pipe(lambda df: df['Gender SportClass Distance Stroke Result Seconds SDMS_ID NPC Given_Name Family_Name Date'.split()]) + ) + + + +__version__ = "0.1.1" \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..111499c --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# ParaUtils + +Utility functions for para swimming related activities \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..8098176 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,27 @@ +[build-system] +requires = ["setuptools>=45", "wheel"] +build-backend = "setuptools.build_meta" + +[project] +name = "ParaUtils" +description = "Bits and bobs useful for many para swimming data manipulation projects" +readme = "README.md" +license = "MIT" +dynamic = ["version"] + +dependencies = [ + "pandas", + "numpy" +] +requires-python = ">=3.8" + +authors = [ + {name = "Michael Edey", email = "mike@edey.org"} +] +maintainers = [ + {name = "Michael Edey", email = "mike@edey.org"} +] + +[project.urls] +Homepage = "https://git.edey.org/Mike/ParaUtils.git" +Repository = "https://git.edey.org/Mike/ParaUtils.git" diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..ba3eb46 --- /dev/null +++ b/setup.py @@ -0,0 +1,6 @@ +from setuptools import setup, find_packages + +setup( + name="ParaUtils", + packages=find_packages(), +) \ No newline at end of file