Skip to content

Commit

Permalink
Refactore massmart placement implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Heizenburg committed Nov 17, 2023
1 parent 8b6f585 commit f7e3406
Show file tree
Hide file tree
Showing 2 changed files with 309 additions and 220 deletions.
94 changes: 53 additions & 41 deletions notebooks/EST/react.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@
"metadata": {},
"outputs": [],
"source": [
"from dotenv import load_dotenv\n",
"import pdb\n",
"\n",
"from collections import defaultdict\n",
"from pathlib import Path\n",
"\n",
"import logging\n",
"import os\n",
"import pandas as pd\n",
"import glob\n",
"from ftplib import FTP"
"\n",
"from dotenv import load_dotenv\n",
"from ftplib import FTP, error_perm"
]
},
{
Expand All @@ -33,7 +30,9 @@
"metadata": {},
"outputs": [],
"source": [
"directory = r'R:\\RawData\\Elite Star\\React Stores\\Client Download Folders'"
"# Configure the logger\n",
"logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')\n",
"logger = logging.getLogger(__name__) "
]
},
{
Expand All @@ -42,49 +41,30 @@
"metadata": {},
"outputs": [],
"source": [
"\n",
"def fetch_files_from_ftp(ftp_host, ftp_port, ftp_user, ftp_password, folders_to_download, download_path):\n",
" try:\n",
" # Connect to the FTP server\n",
" with FTP() as ftp:\n",
" ftp.connect(ftp_host, ftp_port)\n",
" ftp.login(user=ftp_user, passwd=ftp_password)\n",
" pdb.set_trace()\n",
"\n",
" # Ensure download path exists\n",
" if not os.path.exists(download_path):\n",
" os.makedirs(download_path)\n",
"\n",
" # Fetch files from the specified folders\n",
" for folder in folders_to_download:\n",
" ftp.cwd(folder)\n",
" file_list = ftp.nlst()\n",
" for file_name in file_list:\n",
" local_file_path = os.path.join(download_path, file_name)\n",
" with open(local_file_path, 'wb') as file:\n",
" ftp.retrbinary('RETR ' + file_name, file.write)\n",
" \n",
" except error_perm as e:\n",
" logger.error(f\"Permission error: {e}\")\n",
" except Exception as e:\n",
" import traceback\n",
" print(\"An error occurred:\")\n",
" traceback.print_exc()\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Load environment variables.\n",
"load_dotenv()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
" logger.error(f\"An error occurred: {e}\", exc_info=True)\n",
"\n",
"# Load environment variables and fetch files from FTP\n",
"load_dotenv()\n",
"ftp_host = os.getenv('FTP_HOST')\n",
"ftp_port = int(os.getenv('FTP_PORT'))\n",
"ftp_user = os.getenv('FTP_USERNAME')\n",
Expand All @@ -96,13 +76,18 @@
" \"euetrac1ftp/Up/Premjee\"\n",
"]\n",
"\n",
"fetch_files_from_ftp(ftp_host, ftp_port, ftp_user, ftp_password, folders_to_download, directory)"
"directory = r'R:\\RawData\\Elite Star\\React Stores\\Client Download Folders'\n",
"fetch_files_from_ftp(ftp_host, ftp_port, ftp_user, ftp_password, folders_to_download, directory)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"execution_count": 33,
"metadata": {
"notebookRunGroups": {
"groupValue": ""
}
},
"outputs": [],
"source": [
"def delete_all_except_recent(directory):\n",
Expand Down Expand Up @@ -162,18 +147,45 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 34,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"(Empty DataFrame\n",
" Columns: [Deleted Files]\n",
" Index: [],\n",
" Empty DataFrame\n",
" Columns: [Kept Files]\n",
" Index: [])"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"delete_all_except_recent(directory)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 35,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"_, dataframe_remainders = delete_all_except_recent(directory)\n",
"len(dataframe_remainders)"
Expand Down
Loading

0 comments on commit f7e3406

Please sign in to comment.