src.main.helpers package

Submodules

src.main.helpers.firestore_init module

class src.main.helpers.firestore_init.FirestoreDB[source]

Bases: object

A class to interact with the firestore database.

db

The firestore client object.

Type:

firestore.Client

create_collection(name)[source]

Create a collection with the given name and return a reference to it.

Parameters:

name (str) – The name of the collection to create.

Returns:

A reference to the created collection.

Return type:

firestore.CollectionReference

Notes

  1. Rationale

    This method creates a collection with the given name in the firestore database, allowing the user to store and retrieve documents in the collection.

  2. Implementation Details
    • The collection method of the firestore client object is used to create and return a collection reference with the given name.

static create_document(collection, doc_id, data)[source]

Create a document with the given id and data in the collection and return a reference to it.

Parameters:
  • collection (firestore.CollectionReference) – A reference to the collection where the document will be created.

  • doc_id (str) – The id of the document to create.

  • data (dict) – The data of the document to create.

Returns:

A reference to the created document.

Return type:

firestore.DocumentReference

Notes

  1. Rationale

    This method creates a document with the given id and data in the collection in the firestore database, allowing the user to store and retrieve the stock price data in the document.

  2. Implementation Details
    • The document method of the collection reference object is used to create and return a document reference with the given id.

    • The set method of the document reference object is used to set the data of the document.

document(ticker, date)[source]

Get a reference to the document with the given ticker and date or None if it does not exist.

Parameters:
  • ticker (str) – The ticker symbol of the stock.

  • date (str) – The date of the stock price data.

Returns:

A reference to the document with the given ticker and date or None if the document does not exist.

Return type:

firestore.DocumentReference or None

Notes

  1. Rationale

    This method gets a reference to the document with the given ticker and date in the firestore database, allowing the user to access and manipulate the stock price data stored in the document.

  2. Implementation Details
    • The document method of the firestore client object is used to get and return a document reference with the given ticker and date as the path.

    • A try-except block is used to handle the possible exception raised if the document does not exist.

get_collection(name)[source]

Get a reference to the collection with the given name or None if it does not exist.

Parameters:

name (str) – The name of the collection to get.

Returns:

A reference to the collection with the given name or None if the collection does not exist.

Return type:

firestore.CollectionReference or None

Notes

  1. Rationale

    This method gets a reference to the collection with the given name in the firestore database, allowing the user to access and manipulate the documents in the collection.

  2. Implementation Details
    • The collection method of the firestore client object is used to get and return a collection reference with the given name.

    • A try-except block is used to handle the possible exception raised if the collection does not exist.

static get_document(collection, doc_id)[source]

Get a reference to the document with the given id in the collection or None if it does not exist.

Parameters:
  • collection (firestore.CollectionReference) – A reference to the collection where the document is located.

  • doc_id (str) – The id of the document to get.

Returns:

A reference to the document with the given id in the collection or None if the document does not exist.

Return type:

firestore.DocumentReference or None

Notes

  1. Rationale

    This method gets a reference to the document with the given id in the collection in the firestore database, allowing the user to access and manipulate the stock price data stored in the document.

  2. Implementation Details
    • The document method of the collection reference object is used to get and return a document reference with the given id.

    • A try-except block is used to handle the possible exception raised if the document does not exist.

static update_document(document, data)[source]

Update the document with the given data and return a reference to it.

Parameters:
  • document (firestore.DocumentReference) – A reference to the document to update.

  • data (dict) – The data to update the document with.

Returns:

A reference to the updated document.

Return type:

firestore.DocumentReference

Notes

  1. Rationale

    This method updates the document with the given data in the firestore database, allowing the user to modify the stock price data stored in the document.

  2. Implementation Details
    • The update method of the document reference object is used to update the data of the document and return a reference to the updated document.

src.main.helpers.firestore_init.firestore_init(path_to_key: str)[source]

Initialise Firestore with the given service account key.

Parameters:

path_to_key (str) – Path to the service account key file.

Returns:

Firestore client object.

Return type:

firestore.client.Client

src.main.helpers.firestore_update module

src.main.helpers.firestore_update.store_data(stock_data_list: list[StockData], firestore_db: FirestoreDB) None[source]

Store the stock price data in the firestore database.

Parameters:
  • stock_data_list (list[StockData]) – A list of StockData objects containing the ticker and the stock price data.

  • firestore_db (FirestoreDB) – A FirestoreDB object representing the firestore database.

Return type:

None

Notes

  1. Rationale

    This function stores the stock price data in the firestore database, creating or updating the documents in the collections corresponding to the tickers.

  2. Implementation Details
    • The function iterates over the stock data list and gets the ticker and the stock price data for each StockData object.

    • The function checks if the ticker collection exists in the firestore database and creates a new collection if not.

    • The function iterates over the stock price data and gets the date for each stock price object.

    • The function uses the document path to get the date document in the ticker collection and tries to get the document snapshot.

    • The function checks if the document exists and updates the existing document or creates a new document with the stock price data.

src.main.helpers.read_csv module

src.main.helpers.read_csv.read_ticker_symbols(file_path: str, ticker_column: str) list[str][source]

Read ticker symbols from a CSV file.

Parameters:
  • file_path (str) – The path to the CSV file.

  • ticker_column (str) – The header of the column containing the ticker symbols.

Returns:

A list of ticker symbols extracted from the specified column in the CSV file.

Return type:

list

Notes

  1. Rationale

    This function reads ticker symbols from a CSV file, providing flexibility in specifying the file path and the column header containing the ticker symbols. The function uses polars to efficiently handle large CSV files and extract the desired information.

  2. Implementation Details
    • The CSV file is read into a LazyFrame using pl.scan_csv().

    • The LazyFrame is materialized into a DataFrame using df.collect().

    • Ticker symbols are extracted from the specified column and returned as a list.

Module contents