Question: As part of my learning from this Python book I’ve been reading, one of the challenges is to refactor some code from a previous challenge. The challenge is to refactor the loop that reiterates over the list ‘all_eq_data’ that contains data loaded into it from a JSON file. The for loop that does the ..
I am expected to ensure the code is written without unnecessary lines of code. Is there a way to refactor this and get the same output? def format_name(first_name, last_name): if len(first_name) > 0 and len(last_name) > 0: return("Name: " + last_name + ", " + first_name) elif len(first_name) > 0 or len(last_name) > 0: return("Name: ..
I am a scientist trying to learn better software engineering practices, so that my analysis scripts are more robust and easily useable by others. I am having trouble finding the best pattern for the following problem. Is there an OOP framework for easy subsetting by instance attributes? For instance: I have a large table of ..
I’m currently planning a new bot and want to change my approach this time. Previously, I code just to make it work. Then refactor and refactor until I get the best version of the code. But I see this approach is time consuming thought it might be better to start planning ahead then at least ..
I know this is not good coding and I’m looking to improve it. I want to get the first name by default if no name is supplied. My hack still enters the for loop and if it would be a big one it would be innefficient. But if I do all the attributions that I’m ..
I have many ifs and elses that assign a variable an X value. Does anyone have any idea how to try to improve this code? I’ve searched for several ideas on the internet, but I haven’t found anything that would help me in this specific case. date1 = ” date2 = ” name = ” ..
We are refactoring our code base. Old: from a.b import foo_method New: from b.d import bar_method Both methods (foo_method() and bar_method()) are the same. It just changed the name an the package. Since above example is just one example of many ways a method can be imported, I don’t think a simple regular expression can ..
I want to try refactoring this method in my code: def get_params_in_string(params): obj_name = params.PrefetchMethod.split(‘.’) right_method_name = ‘BuhReports.’ if obj_name in (‘AccountAnalysisW’, ‘АнализСчетаW’): obj = AccountAnalysis(None, params, None, None) method_name = right_method_name + ‘GetAccountAnalysis’ elif obj_name in (‘TurnoverSheetW’, ‘ОборотнаяВедомостьW’): obj = TurnoverSheet(None, params, None, None) method_name = right_method_name + ‘GetTurnoverSheet’ elif obj_name in (‘CardAccountW’, ‘КарточкаСчетаW’): ..
Need some advice on how to refactoring this kind of code, as you can see, the basic code for all, left, right is the same, all what is changing is .strip(), .lstrip(), .rstrip(), is it possible to refactor this in a "beautiful" manner? def clean_whitespaces(input, mode=’all’, ): result = None modes = (None, ‘all’, ‘left’, ..
Here is my long if statement: if i % 2 == 1 or i == 0: if x1 > x: ny = y + ceil(shift * (i / 2)) ny1 = y1 + ceil(shift * (i / 2)) elif x1 == x: ny = y ny1 = y1 elif x1 < x: ny = y ..