Code Quality Review /u/Specific_Anteater64 Python Education

Hey y’all, I did some pre assessment for a company a while back. I never heard back from them and I think it might have had something to do with my code quality. I just wanted to post my solution to get a fresh and honest perspective on how I write code. The task was to extract unicode characters from a google doc and print out a secret message on some graphic. I used BSU and matplot lib for this task. Please be harsh because I want to improve my code.

# Imports ---------------------------------------------------------------------- import requests from bs4 import BeautifulSoup import matplotlib.pyplot as plt # ------------------------------------------------------------------------------ # Helper Functions ------------------------------------------------------------- def unicode_data_extractor(token): ENCODER_1 = u'u2588' # For rectangle ENCODER_2 = u'u2580' # For box ENCODER_3 = u'u2591' # For grey temp_str = str(token.string) if temp_str.isdigit(): return int(temp_str) elif (temp_str == ENCODER_1 or temp_str == ENCODER_2 or temp_str == ENCODER_3): return temp_str else: return None def dimension_extractor(object_container): x, y = 0, 0 cont_len = len(object_container) for item in range(cont_len): x = max(x, object_container[item][1][0]) y = max(y, object_container[item][1][1]) return (x, y) def plot_builder(grid_dims, data): data_len = len(data) p_unicode = "" # Points to unicode object per iteration of list fig, ax = plt.subplots() plt.tick_params(bottom=False, left=False) ax.set(xlim=(0, grid_dims[0] * 3), ylim=(0, grid_dims[1] * 5)) ax.set_xticklabels([]) ax.set_yticklabels([]) for item in range(data_len): x_cord = data[item][1][0] y_cord = data[item][1][1] p_unicode = data[item][0] plt.text(x_cord, y_cord, p_unicode, fontsize=12) plt.grid() plt.show() # Main function ---------------------------------------------------------------- def letter_decoder(url): temp_list = [] num_list = [] unicode_data = [] try: request = requests.get(url) request.encoding = request.apparent_encoding data = request.text except: print("Url cannot be reached.") return # Parses HTML tag <span> to locate unicode data soup = BeautifulSoup(data, 'html.parser') tokens = soup.find_all("span") for item in tokens: temp_var = unicode_data_extractor(item) if len(temp_list) == 2: unicode_data.append(temp_list) temp_list = [] if temp_var is None: # Passes NoneType for correct inputs continue else: if isinstance(temp_var, int): num_list.append(temp_var) if len(num_list) == 2: temp_list.append(tuple(num_list)) num_list = [] else: temp_list.append(temp_var) plot_builder(dimension_extractor(unicode_data), unicode_data) # ------------------------------------------------------------------------------ # Program run ------------------------------------------------------------------ URL = "https://docs.google.com/document/d/e/2PACX-1vQGUck9HIFCyezsrBSnmENk5ieJuYwpt7YHYEzeNJkIb9OSDdx-ov2nRNReKQyey-cwJOoEKUhLmN9z/pub" if __name__ == "__main__": letter_decoder(URL) # ------------------------------------------------------------------------------ 

submitted by /u/Specific_Anteater64
[link] [comments]

​r/learnpython Hey y’all, I did some pre assessment for a company a while back. I never heard back from them and I think it might have had something to do with my code quality. I just wanted to post my solution to get a fresh and honest perspective on how I write code. The task was to extract unicode characters from a google doc and print out a secret message on some graphic. I used BSU and matplot lib for this task. Please be harsh because I want to improve my code. # Imports ———————————————————————- import requests from bs4 import BeautifulSoup import matplotlib.pyplot as plt # —————————————————————————— # Helper Functions ————————————————————- def unicode_data_extractor(token): ENCODER_1 = u’u2588′ # For rectangle ENCODER_2 = u’u2580′ # For box ENCODER_3 = u’u2591′ # For grey temp_str = str(token.string) if temp_str.isdigit(): return int(temp_str) elif (temp_str == ENCODER_1 or temp_str == ENCODER_2 or temp_str == ENCODER_3): return temp_str else: return None def dimension_extractor(object_container): x, y = 0, 0 cont_len = len(object_container) for item in range(cont_len): x = max(x, object_container[item][1][0]) y = max(y, object_container[item][1][1]) return (x, y) def plot_builder(grid_dims, data): data_len = len(data) p_unicode = “” # Points to unicode object per iteration of list fig, ax = plt.subplots() plt.tick_params(bottom=False, left=False) ax.set(xlim=(0, grid_dims[0] * 3), ylim=(0, grid_dims[1] * 5)) ax.set_xticklabels([]) ax.set_yticklabels([]) for item in range(data_len): x_cord = data[item][1][0] y_cord = data[item][1][1] p_unicode = data[item][0] plt.text(x_cord, y_cord, p_unicode, fontsize=12) plt.grid() plt.show() # Main function —————————————————————- def letter_decoder(url): temp_list = [] num_list = [] unicode_data = [] try: request = requests.get(url) request.encoding = request.apparent_encoding data = request.text except: print(“Url cannot be reached.”) return # Parses HTML tag <span> to locate unicode data soup = BeautifulSoup(data, ‘html.parser’) tokens = soup.find_all(“span”) for item in tokens: temp_var = unicode_data_extractor(item) if len(temp_list) == 2: unicode_data.append(temp_list) temp_list = [] if temp_var is None: # Passes NoneType for correct inputs continue else: if isinstance(temp_var, int): num_list.append(temp_var) if len(num_list) == 2: temp_list.append(tuple(num_list)) num_list = [] else: temp_list.append(temp_var) plot_builder(dimension_extractor(unicode_data), unicode_data) # —————————————————————————— # Program run —————————————————————— URL = “https://docs.google.com/document/d/e/2PACX-1vQGUck9HIFCyezsrBSnmENk5ieJuYwpt7YHYEzeNJkIb9OSDdx-ov2nRNReKQyey-cwJOoEKUhLmN9z/pub” if __name__ == “__main__”: letter_decoder(URL) # —————————————————————————— submitted by /u/Specific_Anteater64 [link] [comments] 

Hey y’all, I did some pre assessment for a company a while back. I never heard back from them and I think it might have had something to do with my code quality. I just wanted to post my solution to get a fresh and honest perspective on how I write code. The task was to extract unicode characters from a google doc and print out a secret message on some graphic. I used BSU and matplot lib for this task. Please be harsh because I want to improve my code.

# Imports ---------------------------------------------------------------------- import requests from bs4 import BeautifulSoup import matplotlib.pyplot as plt # ------------------------------------------------------------------------------ # Helper Functions ------------------------------------------------------------- def unicode_data_extractor(token): ENCODER_1 = u'u2588' # For rectangle ENCODER_2 = u'u2580' # For box ENCODER_3 = u'u2591' # For grey temp_str = str(token.string) if temp_str.isdigit(): return int(temp_str) elif (temp_str == ENCODER_1 or temp_str == ENCODER_2 or temp_str == ENCODER_3): return temp_str else: return None def dimension_extractor(object_container): x, y = 0, 0 cont_len = len(object_container) for item in range(cont_len): x = max(x, object_container[item][1][0]) y = max(y, object_container[item][1][1]) return (x, y) def plot_builder(grid_dims, data): data_len = len(data) p_unicode = "" # Points to unicode object per iteration of list fig, ax = plt.subplots() plt.tick_params(bottom=False, left=False) ax.set(xlim=(0, grid_dims[0] * 3), ylim=(0, grid_dims[1] * 5)) ax.set_xticklabels([]) ax.set_yticklabels([]) for item in range(data_len): x_cord = data[item][1][0] y_cord = data[item][1][1] p_unicode = data[item][0] plt.text(x_cord, y_cord, p_unicode, fontsize=12) plt.grid() plt.show() # Main function ---------------------------------------------------------------- def letter_decoder(url): temp_list = [] num_list = [] unicode_data = [] try: request = requests.get(url) request.encoding = request.apparent_encoding data = request.text except: print("Url cannot be reached.") return # Parses HTML tag <span> to locate unicode data soup = BeautifulSoup(data, 'html.parser') tokens = soup.find_all("span") for item in tokens: temp_var = unicode_data_extractor(item) if len(temp_list) == 2: unicode_data.append(temp_list) temp_list = [] if temp_var is None: # Passes NoneType for correct inputs continue else: if isinstance(temp_var, int): num_list.append(temp_var) if len(num_list) == 2: temp_list.append(tuple(num_list)) num_list = [] else: temp_list.append(temp_var) plot_builder(dimension_extractor(unicode_data), unicode_data) # ------------------------------------------------------------------------------ # Program run ------------------------------------------------------------------ URL = "https://docs.google.com/document/d/e/2PACX-1vQGUck9HIFCyezsrBSnmENk5ieJuYwpt7YHYEzeNJkIb9OSDdx-ov2nRNReKQyey-cwJOoEKUhLmN9z/pub" if __name__ == "__main__": letter_decoder(URL) # ------------------------------------------------------------------------------ 

submitted by /u/Specific_Anteater64
[link] [comments] 

Leave a Reply

Your email address will not be published. Required fields are marked *