Skip to content

Commit

Permalink
Flake8 fixes (#99)
Browse files Browse the repository at this point in the history
* args.py flake8 and copyright header

* constants.py flake8 + copyright header

* db_utils.py flake8 comments + copyright header

* event_message_type.py + game.py flake8 + copywrite header

* gui.py flake8 + copyright header

* nba_sql.py flake8 + copywrite header

* play_by_play, player, player_game_log

* more

* fixing user agent c:

* model headers
  • Loading branch information
mpope9 committed Nov 5, 2023
1 parent 9325a0d commit c46dc47
Show file tree
Hide file tree
Showing 32 changed files with 796 additions and 71 deletions.
54 changes: 42 additions & 12 deletions stats/args.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
from utils import generate_valid_seasons

"""
Creates a parser.
------------------------------------------------------------------------------
Copyright 2023 Matthew Pope
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
------------------------------------------------------------------------------
Creates an argument parser for the commandline version.
"""


Expand All @@ -15,15 +30,18 @@ def create_parser(parser):
dest='database_type',
default='sqlite',
choices=['mysql', 'postgres', 'sqlite'],
help='The database flag specifies which database protocol to use. Defaults to "sqlite", but also accepts "postgres" and "mysql".')
help='''
The database flag specifies which database protocol to use.
Defaults to "sqlite", but also accepts "postgres" and "mysql".
''')

parser.add_argument(
'--database_name',
'--database_name',
help="Database Name (Not Needed For SQLite)",
default='nba')

parser.add_argument(
'--database_host',
'--database_host',
help="Database Hostname (Not Needed For SQLite)",
default=None)

Expand All @@ -32,26 +50,34 @@ def create_parser(parser):
help="Database Username (Not Needed For SQLite)",
default=None)


parser.add_argument(
'--create-schema',
dest='create_schema',
action="store_true",
default=True,
help='Flag to initialize the database schema before loading data. If the schema already exists then nothing will happen.')
help='''
Flag to initialize the database schema before loading data.
If the schema already exists then nothing will happen.
''')

parser.add_argument(
'--time-between-requests',
dest='request_gap',
default='.7',
help='This flag exists to prevent rate limiting, and injects the desired amount of time inbetween requesting resources.')
help='''
This flag exists to prevent rate limiting, and injects the
desired amount of time inbetween requesting resources.
''')

#To fix issue https://github.com/mpope9/nba-sql/issues/56
# To fix issue https://github.com/mpope9/nba-sql/issues/56
parser.add_argument(
'--batch_size',
default='10000',
type=int,
help="Inserts BATCH_SIZE chunks of rows to the database. This value is ignored when selecting database 'sqlite'.")
help='''
Inserts BATCH_SIZE chunks of rows to the database.
This value is ignored when selecting database 'sqlite'.
''')

parser.add_argument(
'--sqlite-path',
Expand All @@ -63,6 +89,10 @@ def create_parser(parser):
'--quiet',
dest='quiet',
action='store_true',
help='Setting to define stdout logging level. If set, only "ok" will be printed if ran successfully. This currently only applies to refreshing a db, and not loading one.')
help='''
Setting to define stdout logging level. If set, only
"ok" will be printed if ran successfully.
This currently only applies to refreshing a db, and not loading one.
''')

return parser
26 changes: 22 additions & 4 deletions stats/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
"""
------------------------------------------------------------------------------
Copyright 2023 Matthew Pope
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
------------------------------------------------------------------------------
Constants used in the application.
"""

Expand All @@ -10,10 +27,11 @@
'Accept': 'application/json, text/plain, */*',
'x-nba-stats-token': 'true',
'User-Agent': (
#'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) '
#'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130'
#'Safari/537.36'
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'
# 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) '
# 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130'
# 'Safari/537.36'
'''Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)\
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36'''
),
'x-nba-stats-origin': 'stats',
'Sec-Fetch-Site': 'same-origin',
Expand Down
22 changes: 20 additions & 2 deletions stats/db_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
from utils import chunk_list
"""
------------------------------------------------------------------------------
Copyright 2023 Matthew Pope
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
------------------------------------------------------------------------------
"""
Database utilities (future middleware layer if we decide to use DuckDB by default.)
"""

from utils import chunk_list


def insert_many(settings, table, rows):
"""
Expand All @@ -19,6 +35,7 @@ def insert_many(settings, table, rows):
for row in chunked_rows:
table.insert_many(row).execute()


def __insert_many_sqlite(settings, table, rows):
"""
SQLite has a limit on number of rows. Chunk the rows and batch insert.
Expand All @@ -29,6 +46,7 @@ def __insert_many_sqlite(settings, table, rows):
for row in chunked_rows:
table.insert_many(row).execute()


def insert_many_on_conflict_ignore(settings, table, rows):
"""
Entry function on insert_many, ignoring conflicts on key issues.
Expand Down
21 changes: 21 additions & 0 deletions stats/event_message_type.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
"""
------------------------------------------------------------------------------
Copyright 2023 Matthew Pope
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
------------------------------------------------------------------------------
Initializer for the EventMessageType object.
"""

from models import EventMessageType
from constants import event_message_types

Expand Down
23 changes: 22 additions & 1 deletion stats/game.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
"""
------------------------------------------------------------------------------
Copyright 2023 Matthew Pope
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
------------------------------------------------------------------------------
Game builder.
"""

from models import Game
from constants import team_abbrev_mapping
from collections import namedtuple
Expand Down Expand Up @@ -25,7 +46,7 @@ def game_id_predicate(self):
"""
return Game.select(Game.game_id)

def populate_table(self, game_set, ignore_dups = False):
def populate_table(self, game_set, ignore_dups=False):
"""
Takes a set of tuples and builds the game table.
@params:
Expand Down
91 changes: 68 additions & 23 deletions stats/gui.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
"""
------------------------------------------------------------------------------
Copyright 2023 Matthew Pope
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
------------------------------------------------------------------------------
This is a wrapper, to allow building the cmdline executable without
having to include the full GUI libs.
"""

from nba_sql import main
from args import create_parser

Expand All @@ -10,37 +32,38 @@
import sys


"""
This is a wrapper, to allow building the cmdline executable without
having to include the full GUI libs.
"""

# This fixes an issue with Gooey and PyInstaller.
if sys.stdout.encoding != 'UTF-8':
sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer, 'strict')
if sys.stderr.encoding != 'UTF-8':
sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer, 'strict')


# This 'fixes' an issue with printing in the Gooey console, kinda sorta not really.
class Unbuffered(object):
def __init__(self, stream):
self.stream = stream
def write(self, data):
self.stream.write(data)
self.stream.flush()
def writelines(self, datas):
self.stream.writelines(datas)
self.stream.flush()
def __getattr__(self, attr):
return getattr(self.stream, attr)

def __init__(self, stream):
self.stream = stream

def write(self, data):
self.stream.write(data)
self.stream.flush()

def writelines(self, datas):
self.stream.writelines(datas)
self.stream.flush()

def __getattr__(self, attr):
return getattr(self.stream, attr)


sys.stdout = Unbuffered(sys.stdout)


## Bad practice? Yes. Any other alternative? Not at this point.
## Only enable Gooey if there are no arguments passed to the script.
if len(sys.argv)>=2:
if not '--ignore-gooey' in sys.argv:
# Bad practice? Yes. Any other alternative? Not at this point.
# Only enable Gooey if there are no arguments passed to the script.
if len(sys.argv) >= 2:
if '--ignore-gooey' not in sys.argv:
sys.argv.append('--ignore-gooey')


Expand All @@ -60,11 +83,18 @@ def gui_main():
})
mode_parser.add_argument(
'--default_mode',
help='Mode to create the database and load historic data. Use this mode when creating a new database or when trying to load a specific season or a range of seasons.',
help='''
Mode to create the database and load historic data.
Use this mode when creating a new database or when
trying to load a specific season or a range of seasons.
''',
action='store_true')
mode_parser.add_argument(
'--current_season_mode',
help='Mode to refresh the current season. Use this mode on an existing database to update it with the latest data.',
help='''
Mode to refresh the current season. Use this mode on an
existing database to update it with the latest data.
''',
action='store_true')

parser.add_argument(
Expand All @@ -83,14 +113,29 @@ def gui_main():
choices=valid_seasons,
widget='Listbox',
nargs="*",
help='The seasons flag loads the database with the specified season. The format of the season should be in the form "YYYY-YY". The default behavior is loading the current season.')
help='''
The seasons flag loads the database with the specified season.
The format of the season should be in the form "YYYY-YY".
The default behavior is loading the current season.
''')

parser.add_argument(
'--skip-tables',
action='store',
nargs="*",
default='',
choices=['player_season', 'player_game_log', 'play_by_play', 'pgtt', 'shot_chart_detail', 'game', 'event_message_type', 'team', 'player', ''],
choices=[
'player_season',
'player_game_log',
'play_by_play',
'pgtt',
'shot_chart_detail',
'game',
'event_message_type',
'team',
'player',
''
],
widget='Listbox',
help='Use this option to skip loading certain tables.')

Expand Down
Loading

0 comments on commit c46dc47

Please sign in to comment.