Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Capacity check #11

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**.docx
**.csv
/venv/
.idea
Empty file added Lib/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions Lib/console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

class _Colours:
# default
RESET = '\033[0m'
# warning
YELLOW = '\033[93m'
# error
RED = '\033[91m'

GREEN = '\033[92m'
BLUE = '\033[94m'
PURPLE = '\033[95m'
CYAN = '\033[96m'
WHITE = '\033[97m'


def print_warn(msg: str):
print(_Colours.YELLOW + "Warning: " + msg + _Colours.RESET)


def print_error(msg: str):
print(_Colours.RED + "Error: " + msg + _Colours.RESET)
117 changes: 117 additions & 0 deletions Lib/data_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import os
from typing import Optional, Union
from Lib.console import print_warn, print_error

# Attempt to import csv
try:
import csv
except ImportError:
csv = None
print_warn("Warning: csv module is not available. CSV file handling will not be supported.")

# Attempt to import pandas
try:
import pandas as pd
except ImportError:
pd = None
print_warn("Warning: pandas library is not available. Data frame functionality will not be supported.")

# Attempt to import numpy
try:
import numpy as np
except ImportError:
np = None
print_warn("numpy library is not available. NumPy array functionality will not be supported.")


# Define functions using imported libraries, checking for None before usage
def get_data_as_pandas_data_frame(file_path: str):
"""
Load a CSV file into a pandas DataFrame.

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

Returns:
pd.DataFrame: The loaded DataFrame, or an empty DataFrame if loading fails.
"""
if pd is None:
print_error("pandas library is not available.")
return None

try:
if not os.path.isfile(file_path):
raise FileNotFoundError(f"'{file_path}' is not a valid file path.")

if not file_path.endswith('.csv'):
raise ValueError(f"'{file_path}' does not point to a CSV file.")

df = pd.read_csv(file_path, encoding='utf-8')
return df

except Exception as e:
print_error(f"An error occurred while loading the CSV file: {e}")
return pd.DataFrame()


def get_data_as_numpy_array(file_path: str):
"""
Load data from a text file into a NumPy array.

Parameters:
file_path (str): The path to the text file.

Returns:
np.ndarray: The loaded NumPy array, or an empty array if loading fails.
"""
if np is None:
print_error("numpy library is not available.")
return None

try:
if not os.path.isfile(file_path):
raise FileNotFoundError(f"Error: '{file_path}' is not a valid file path.")

with open(file_path, 'r', encoding='utf-8') as file:
# Read all lines from the file
lines = file.readlines()

# Extract headers and data
headers = lines[0].strip().split(',')
data_lines = [line.strip().split(',') for line in lines[1:]]

# Convert data_lines to a numpy array
numpy_array = np.array(data_lines, dtype=object) # Assuming data is numeric

return numpy_array
except Exception as e:
print_error(f"An error occurred while loading the data: {e}")
return np.array([])


def get_csv_reader(file_path: str):
"""
Load data from a CSV file and return a CSV reader object.

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

Returns:
csv.reader: A CSV reader object. Returns an empty reader if loading fails.
"""
if csv is None:
print_error("csv library is not available.")
return None
try:
if not os.path.isfile(file_path):
raise FileNotFoundError(f"'{file_path}' is not a valid file path.")

# Open the CSV file and return the CSV reader object
with open(file_path, 'r', encoding='utf-8') as file:
reader = csv.reader(file)
return reader

except Exception as e:
print_error(f"An error occurred while loading the CSV file: {e}")
# Return an empty CSV reader if an exception occurs
return csv.reader([]) # Return an empty iterable (empty CSV reader)
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
# Ottawa Food Bank
# Ottawa Food Bank - Quick Start Python

## Research Questions
1. What is the relationship between food security status and health/demographics/needs in our sample?
2. Considering the stated needs of people accessing food programs, do agencies/the network have the capacity to meet those needs?

## Branch Details
You can use this branch to help you get started if you prefer working in Python.

It contains very basic and simple methods to help you load data.

All you need to do is extract the both zip files into the `Data` folder.

- ### Lib
- #### data_loader
- get_data_as_pandas_data_frame(file_path: str)
- get_data_as_numpy_array(file_path: str)
- get_csv_reader(file_path: str)


## Data Details
In this repository, you will find two password-protected ZIP files containing survey data:

Expand Down
Loading