diff --git a/docs/conf.py b/docs/conf.py index 97f4245617b234bf607a2a4936583284ea5db525..df7f06709a53f620110163a2727528f17d10947e 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -32,6 +32,9 @@ extensions = [ 'sphinx.ext.autosummary' ] +#autosummary_generate = True # Enable autosummary + + # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] diff --git a/docs/formulaone.rst b/docs/formulaone.rst new file mode 100644 index 0000000000000000000000000000000000000000..35c40f68925a97792edf6b5070b77ac3d57874db --- /dev/null +++ b/docs/formulaone.rst @@ -0,0 +1,61 @@ +formulaone package +================== + +Submodules +---------- + +formulaone.core module +---------------------- + +.. automodule:: formulaone.core + :members: + :undoc-members: + :show-inheritance: + +formulaone.dynamodb\_helpers module +----------------------------------- + +.. automodule:: formulaone.dynamodb_helpers + :members: + :undoc-members: + :show-inheritance: + +formulaone.helpers module +------------------------- + +.. automodule:: formulaone.helpers + :members: + :undoc-members: + :show-inheritance: + +formulaone.load\_latest\_race module +------------------------------------ + +.. automodule:: formulaone.load_latest_race + :members: + :undoc-members: + :show-inheritance: + +formulaone.prepare\_data module +------------------------------- + +.. automodule:: formulaone.prepare_data + :members: + :undoc-members: + :show-inheritance: + +formulaone.tidy\_data module +---------------------------- + +.. automodule:: formulaone.tidy_data + :members: + :undoc-members: + :show-inheritance: + +Module contents +--------------- + +.. automodule:: formulaone + :members: + :undoc-members: + :show-inheritance: diff --git a/docs/generated/dynamodb_helpers.rst b/docs/generated/dynamodb_helpers.rst deleted file mode 100644 index 19260166747828d71ed862a54f030c92c46fd9e3..0000000000000000000000000000000000000000 --- a/docs/generated/dynamodb_helpers.rst +++ /dev/null @@ -1,19 +0,0 @@ -sample.helpers -============== - -.. automodule:: sample.helpers - :members: - :undoc-members: - :show-inheritance: - - .. rubric:: Functions - - .. autosummary:: - :toctree: _autosummary - - get_raw_data_path - get_dynamodb_resource - list_dynamodb_tables - get_movies_table - get_movie_item - query_movies_by_year diff --git a/docs/index.rst b/docs/index.rst index e9dd9c418194927157447103c228a2092d53e8f5..5b22772d3cda7b79f1a58d239cc4811f5629ba2b 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -9,6 +9,6 @@ Welcome to sample's documentation! Contents: .. toctree:: - + modules usage API diff --git a/docs/modules.rst b/docs/modules.rst new file mode 100644 index 0000000000000000000000000000000000000000..b2a413d6ea51d0f722bbff4d7b5d8ad797ea8e56 --- /dev/null +++ b/docs/modules.rst @@ -0,0 +1,7 @@ +formulaone +========== + +.. toctree:: + :maxdepth: 4 + + formulaone diff --git a/formulaone/dynamodb_helpers.py b/formulaone/dynamodb_helpers.py index 822ebf2fc3f264c29516151d8fceec66d33ecfaf..dc18e094033f1331b8d42b548c8a09a6bd348262 100644 --- a/formulaone/dynamodb_helpers.py +++ b/formulaone/dynamodb_helpers.py @@ -1,6 +1,13 @@ -import boto3 + +import os +import sys import configparser +import boto3 +from botocore.exceptions import NoCredentialsError, PartialCredentialsError from boto3.dynamodb.conditions import Key +import pandas as pd +from decimal import Decimal + def get_config(): """ @@ -10,7 +17,17 @@ def get_config(): config: The config object. """ config = configparser.ConfigParser() - config.read('config.ini') + #config_file = os.path.join(os.path.dirname(__file__), 'config.ini') + #config_file = os.path.abspath(os.path.join(os.getcwd(), 'config.ini')) + config_file = 'config.ini' + if not os.path.exists(config_file): + raise FileNotFoundError(f"The configuration file was not found: {config_file}") + + config.read(config_file) + + if 'AWS' not in config or 'DYNAMODB' not in config: + raise KeyError("One or more required sections are missing in the configuration file.") + return config def get_dynamodb_resource(): @@ -21,6 +38,7 @@ def get_dynamodb_resource(): dynamo_resource: The DynamoDB resource. """ config = get_config() + aws_access_key_id = config['AWS']['aws_access_key_id'] aws_secret_access_key = config['AWS']['aws_secret_access_key'] region_name = config['AWS']['region_name'] @@ -34,6 +52,7 @@ def get_dynamodb_resource(): dynamo_resource = session.resource('dynamodb') return dynamo_resource + def list_dynamodb_tables(dynamo_resource): """ Lists all tables in the DynamoDB resource. @@ -91,3 +110,35 @@ def query_movies_by_year(table, year): KeyConditionExpression=Key('year').eq(year) ) return response.get('Items', []) + + +def tidy_movie_data(movies): + """ + Transforms the raw movie data into a tidy DataFrame. + + Args: + movies (list): List of raw movie data. + + Returns: + DataFrame: Tidy DataFrame containing movie data. + """ + tidy_data = [] + for movie in movies: + year = movie['year'] + title = movie['title'] + info = movie['info'] + tidy_data.append({ + 'year': int(year), + 'title': title, + 'actors': ', '.join(info['actors']), + 'release_date': info['release_date'], + 'plot': info['plot'], + 'genres': ', '.join(info['genres']), + 'image_url': info['image_url'], + 'directors': ', '.join(info['directors']), + 'rating': float(info['rating']), + 'rank': int(info['rank']), + 'running_time_secs': int(info['running_time_secs']) + }) + + return pd.DataFrame(tidy_data) \ No newline at end of file diff --git a/tests/test_dynamodb.py b/tests/test_dynamodb.py index 28e6837d464ce934f5d4f962201f677a1f6b0920..8d47739334903808e64d7bd140d88c6f0098cda4 100644 --- a/tests/test_dynamodb.py +++ b/tests/test_dynamodb.py @@ -6,20 +6,18 @@ import boto3 from botocore.exceptions import NoCredentialsError, PartialCredentialsError # Add the formulaone model directory to the Python path -sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) +#sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) -from formulaone.dynamodb_helpers import ( - get_dynamodb_resource, - list_dynamodb_tables, - get_movies_table, - get_movie_item, - query_movies_by_year -) +module_path = os.path.abspath(os.path.join(os.getcwd(), 'formulaone')) +sys.path.insert(0, module_path) +print(sys.path.insert(0, module_path)) + +from formulaone.dynamodb_helpers import * def test_aws_config_keys(): """Test the necessary AWS config keys.""" config = configparser.ConfigParser() - config.read(os.path.join(os.path.dirname(__file__), '../config.ini')) + config.read(os.path.join(os.path.dirname(__file__), 'config.ini')) assert 'AWS' in config, "AWS section is missing" assert 'aws_access_key_id' in config['AWS'], "aws_access_key_id is missing in AWS" assert 'aws_secret_access_key' in config['AWS'], "aws_secret_access_key is missing in AWS" @@ -28,7 +26,7 @@ def test_aws_config_keys(): def test_dynamodb_config_keys(): """Test the necessary DynamoDB config keys.""" config = configparser.ConfigParser() - config.read(os.path.join(os.path.dirname(__file__), '../config.ini')) + config.read(os.path.join(os.path.dirname(__file__), 'config.ini')) assert 'DYNAMODB' in config, "DYNAMODB section is missing" assert 'table_name' in config['DYNAMODB'], "table_name is missing in DYNAMODB" @@ -36,7 +34,7 @@ def test_dynamodb_connection(): """Test the DynamoDB connection using the provided credentials and configuration.""" # Read the config file config = configparser.ConfigParser() - config.read(os.path.join(os.path.dirname(__file__), '../config.ini')) + config.read(os.path.join(os.path.dirname(__file__), 'config.ini')) # Credentials and region aws_access_key_id = config['AWS']['aws_access_key_id'] @@ -71,5 +69,9 @@ def test_dynamodb_resource(): dynamo_resource = get_dynamodb_resource() assert dynamo_resource is not None, "DynamoDB resource should be initialized" -if __name__ == "__main__": - pytest.main() + +## Test ob die Tabelle da ist, noch da? Database da? +# Soll in tidy format bringen +# Doc erstellen lassen mit sphinx ( Aber zunächst soll die Methode gut kommentiert) +# Zip oder url bei gitlab, Prof hinzufügen +# log_transformation machen, testen ggf. \ No newline at end of file