primer commit

This commit is contained in:
Marvin Vallecillo
2024-04-11 02:24:26 -06:00
commit bd0eef8998
19 changed files with 373 additions and 0 deletions
View File
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+53
View File
@@ -0,0 +1,53 @@
import boto3
import os
class Aws3:
def __init__(self, aws_s3_bucket_name, aws_access_key_id, aws_secret_access_key, aws_s3_region):
self.aws_s3_bucket_name = aws_s3_bucket_name
self.aws_access_key_id = aws_access_key_id
self.aws_secret_access_key = aws_secret_access_key
self.aws_s3_region = aws_s3_region
self.client = boto3.client(
's3',
aws_access_key_id=self.aws_access_key_id,
aws_secret_access_key=self.aws_secret_access_key
)
def check_file_existence(self, file, buket_path=None):
file_key = f'{buket_path}/{file}'
self.client.head_object(Bucket=self.aws_s3_bucket_name, Key=file_key)
return True
def create_directory(self, subdirectory):
subdirectory_list = subdirectory.strip('/').split('/')
path = []
for folder in subdirectory_list:
path.append(folder)
path_to_create = '/'.join(path)
try:
self.client.head_object(Bucket=self.aws_s3_bucket_name, Key=path_to_create)
except Exception:
self.client.put_object(Bucket=self.aws_s3_bucket_name, Key=path_to_create)
print(f'\tthe directory {path_to_create} was created')
def load_file(self, file, buket_path=None, local_file_path=None):
try:
# if remote path not exists, upload_file will create it
self.client.upload_file(
Bucket=self.aws_s3_bucket_name,
Filename=os.path.join(local_file_path,file) if local_file_path else file,
Key=f'{buket_path}/{file}' if buket_path else file
)
print(f"\tfile '{file}' was upload to '{buket_path}'")
except Exception as e:
print(f"\tFail to load file: {str(e)}")
def download_file(self, file, buket_path=None, local_file_path=None):
self.client.download_file(
Bucket=self.aws_s3_bucket_name,
Filename=os.path.join(local_file_path,file) if local_file_path else file,
Key=f'{buket_path}/{file}' if buket_path else file
)
print(f"\tfile '{file}' was download to '{local_file_path}'")
+45
View File
@@ -0,0 +1,45 @@
from datetime import date, datetime, timedelta
"""
This class recive two parameters, but only return a list of range of date
for example:
Entry:
start-date 2024-03-01 00:00:00
end-date 2024-03-03 00:00:00
Result:
[datetime.datetime(2024, 3, 1, 0, 0), datetime.datetime(2024, 3, 2, 0, 0), datetime.datetime(2024, 3, 3, 0, 0)]
if don't entry start-date and end-date default date will be today-1
"""
class EntryDateError(Exception):
def __init__(self, msj, code):
super().__init__(msj)
self.code = code
class DateUtil:
def __init__(self, start_date, end_date, return_type='datetime'):
self.default_Date = datetime.combine((date.today() - timedelta(days=1)), datetime.min.time())
self.start_date = start_date if start_date else self.default_Date
self.end_date = self.end_date = end_date if end_date else (start_date if start_date else self.default_Date)
self.return_type = return_type
def date_eval(self):
if self.start_date and self.end_date:
if self.end_date < self.start_date:
raise EntryDateError(f'end-date {self.end_date} can not be less than start-date {self.start_date}', 10001)
def date_range(self):
lista_de_fechas = []
current_date = self.start_date
self.date_eval()
while current_date <= self.end_date:
lista_de_fechas.append(
current_date.date() if self.return_type == 'date' else current_date
)
current_date += timedelta(days=1)
return lista_de_fechas
+15
View File
@@ -0,0 +1,15 @@
import os
class Readsql:
def __init__(self, directory, name_query):
self.name_query = name_query
self.query = None
self.directory = directory
def getQuery(self):
with open(os.path.join(self.directory, self.name_query)) as query_find:
self.query = query_find.read()
return self.query
def show(self):
print(f'Query: {self.query}')
+25
View File
@@ -0,0 +1,25 @@
import csv
import os
class ReadCsv:
def __init__(self, **kwargs):
self.filedir = kwargs.get('filedir')
self.filename = kwargs.get('filename')
self.delimiter = kwargs.get('delimiter',',')
self.lineterminator = kwargs.get('lineterminator','\n')
def readcsv(self, drop_header=False):
result=[]
with open(os.path.join(self.filedir, self.filename), newline='') as file:
reader = csv.reader(file, delimiter=self.delimiter)
if drop_header: next(reader)
for row in reader:
row = tuple(row)
result.append(row)
return result
def rm_csv(self):
file_2_delete = os.path.join(self.filedir, self.filename)
if os.path.exists(file_2_delete):
os.remove(file_2_delete)