Skip to content

Conversation

ValeZh
Copy link
Owner

@ValeZh ValeZh commented Aug 3, 2023

I make all

lab 3/main.py Outdated
Comment on lines 14 to 19
logging.debug('Debug message')
logging.info('Info message')
logging.warning('Warning message')
logging.error('Error message')
logging.critical('Critical message')
logging.info('1 step')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unnecessary code

lab 3/main.py Outdated
self.file_out_name = des_fold_path + "\\" + f_name + '.csv'
self.data = self.download_data()
print(self.file_out_name)
logging.info('Made class : set Destination folder and Filename ')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use standard logger. make a custom one

lab 3/main.py Outdated
def __init__(self, des_fold_path, f_name='output.csv'):
self.data_file = 'lab3.csv'
self.des_fold_path = des_fold_path
self.previous_output = f_name + '.csv'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f-string

lab 3/main.py Outdated
self.data_file = 'lab3.csv'
self.des_fold_path = des_fold_path
self.previous_output = f_name + '.csv'
self.file_out_name = des_fold_path + "\\" + f_name + '.csv'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.path.join
don't use double-quotes

lab 3/main.py Outdated
self.previous_output = f_name + '.csv'
self.file_out_name = des_fold_path + "\\" + f_name + '.csv'
self.data = self.download_data()
print(self.file_out_name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need prints in the code

lab 3/main.py Outdated
def download_data(self):
logging.info('1. Downloading data')
respons = requests.get('https://randomuser.me/api/?results=5000&format=csv')
open(self.data_file, mode='w', encoding='utf-8').write(respons.text)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you didn't close write stream!!!
use context manager here

lab 3/main.py Outdated
Comment on lines 36 to 40
data_dicts = []
with open(self.data_file, 'r', encoding='utf-8') as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
data_dicts.append(row)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
data_dicts = []
with open(self.data_file, 'r', encoding='utf-8') as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
data_dicts.append(row)
with open(self.data_file, 'r', encoding='utf-8') as file:
data_dicts = list(csv.DictReader(file))

lab 3/main.py Outdated
Comment on lines 44 to 49
def add_to_output_file(self, save_output):
with open(self.previous_output, 'w', encoding='utf-8') as csv_output:
writer = csv.DictWriter(csv_output, fieldnames=save_output[0].keys())
writer.writeheader()
writer.writerows(save_output)
logging.info('add_to_output_file correct')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the method looks nice

Comment on lines +11 to +15
logging.basicConfig(
filename="../file.log",
level=logging.INFO,
format="%(asctime)s:%(levelname)s:%(name)s:%(message)s",
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use basic logger.
make a custom one

import requests

logging.basicConfig(
filename="../file.log",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't use double quotes


class DataHumans:
def __init__(self, des_fold_path, f_name="output.csv"):
self.data_file = "../lab3.csv"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a constant. make it as a class atribute

def __init__(self, des_fold_path, f_name="output.csv"):
self.data_file = "../lab3.csv"
self.des_fold_path = des_fold_path
self.previous_output = f_name + ".csv"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f-string

self.data_file = "../lab3.csv"
self.des_fold_path = des_fold_path
self.previous_output = f_name + ".csv"
self.file_out_name = os.path.join(des_fold_path, f"{f_name}.csv")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't you use self.previous_output here


def download_data(self):
logging.info("1. Downloading data")
respons = requests.get("https://randomuser.me/api/?results=5000&format=csv")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make this url as a constant

def download_data(self):
logging.info("1. Downloading data")
respons = requests.get("https://randomuser.me/api/?results=5000&format=csv")
with open(self.data_file, mode="w", encoding="utf-8") as file:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make utf-8 another constant

Comment on lines 60 to 61
csv_reader = csv.DictReader(file)
return [row for row in csv_reader]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
csv_reader = csv.DictReader(file)
return [row for row in csv_reader]
return list(csv.DictReader(file))

Comment on lines 32 to 35
with open(self.data_file, "r", encoding="utf-8") as file:
data_dicts = list(csv.DictReader(file))
logging.info(f"downloading correct ,len of file{len(data_dicts)}")
return data_dicts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't you use read_data_from_file method

csv_reader = csv.DictReader(file)
return [row for row in csv_reader]

def numbering_rows(self, idx, value):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name of functions and methods should be verbs/actions not nouns

def file_replace(self):
logging.info("7. Move initial file to the destination folder")
if not os.path.exists(self.file_out_name):
os.makedirs(self.des_fold_path, mode=777)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

google exist_ok=true parameter

return user

def the_most_old(self, user): # использовать max lst
return max([int(f["dob.age"]) for f in user])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did you call variable f

return collections.Counter(lst_for_count).most_common(1)[0][0]

def make_name_of_file(self, basic_path, user_data):
return f'{basic_path}\\max_age_{str(self.the_most_old(user_data))}_avg_registered_{str(self.average_year_of_reg(user_data))}_ popular_id_{str(self.popular_genres(user_data))}_user_data_{str(user_data[0]["global_index"])}.csv'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please check if you really need conversion to str

Comment on lines 133 to 147
logging.info("9. 10 . make dir for decade")
for i in user_data:
for c in user_data[i]:
basic_path = f"{self.des_fold_path}\\{i}\\{c}"
os.makedirs(basic_path) # f-string \\ make variable//make func
with open(
self.make_name_of_file(basic_path, user_data[i][c]),
"w",
encoding="utf-8",
) as csv_output:
writer = csv.DictWriter(
csv_output, fieldnames=user_data[i][c][0].keys()
)
writer.writeheader()
writer.writerows(user_data[i][c])
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks unreadable. let's try to refactor it together

lab3/src/main.py Outdated
Comment on lines 24 to 27
if args.numb_of_rows_filt is True:
output_file.delete_rows_for_filt(args.numb_of_rows_filt)
else:
output_file.filter_by_gender(args.gender_filt)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

try to use ternary operator here

lab3/src/main.py Outdated
pdb.set_trace()

output_file = DataHumans(args.destination_folder, args.file_name)
if args.numb_of_rows_filt is True:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to compare var wtih True.
do this:

Suggested change
if args.numb_of_rows_filt is True:
if args.numb_of_rows_filt:

lab3/src/main.py Outdated
parser.add_argument("--file_name", type=str, required=True)
parser.add_argument("-g", "--gender_filt", type=str)
parser.add_argument("-n", "--numb_of_rows_filt", type=int)
parser.add_argument("--log_level", type=str)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the parameter should be positional

lab3/src/main.py Outdated
Comment on lines 14 to 15
parser.add_argument("-g", "--gender_filt", type=str)
parser.add_argument("-n", "--numb_of_rows_filt", type=int)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they should be mutually exclusive

lab3/src/main.py Outdated
# использовать argparse
parser = argparse.ArgumentParser()
parser.add_argument("--destination_folder", type=str, required=True)
parser.add_argument("--file_name", type=str, required=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the parameter must have default value. not required

# with patch("builtins.open", mock_open(read_data="data")) as mock_file:
# res = dh.read_data_from_file()
# mock_file.assert_called_with(dh.previous_output, "r", encoding="utf-8")
# assert res == ['test'] No newline at end of file
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add blank line at the end of the file

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants