Home > Mobile >  Updating code to better structure in python
Updating code to better structure in python

Time:03-08

I have a code that calculates some stuff and has a lot of hardcoded values and duplicity, I just need help in arranging it in such a way It wouldn't look messy.

This is not my actual code but basic structure of my code

def my_code():
    a = [func1(x),func2(x),func3(x)]
    return a

def func1(x):
    func1_x =  #calculation using x formula
    func1_dict = {}
    func1["name"] = #some f1hardcoded name
    func1["school"] = "some f1hardcoded value"
    func1["school_address"] = "some f1hardcoded value"
    func1["score_in_written"] = f" func1 student scored {func1_x} percentage "
    func1["score_in_perc"] = func1_x

def func2(x):
    func2_x = #some calculation using y formula
    func2_dict = {}
    func2["name"] = #some f2hardcoded name
    func2["school"] = "some f2hardcoded value"
    func2["school_address"] = "some f2hardcoded value"
    func2["score_in_written"] = f" func1 student scored {func2_x} percentage "
    func2["score_in_perc"] = func2_x

def func3(x):
    func3_x = #some calculation using z formula
    func3_dict = {}
    func3["name"] = # f3 related some hardcoded name
    func3["school"] = " f3 related some hardcoded value"
    func3["school_address"] = " f3 related hardcoded value"
    func3["score_in_written"] = f" func1 student scored {func3_x} percentage "
    func3["score_in_perc"] = func3_x

Hardcoded values basically would not be changed for any x value in a particular function, only the score in percentage and score_in_written would change.

As I have many functions like this (till func9), is there any way I can change this into better code structure? Is there any way I can make this code a little tidy and clean?

CodePudding user response:

What you could be looking at is creating multiple classes with having a class for each subsection of similar operations. Then create the main class that combines all these classes. That way it looks clean and structured. Just read yourself into python classes and how to create objects.

CodePudding user response:

You can use a class to structure the repeated values like this:


class StudentAcademics:
  def __init__(self, name, school, schoolAddress, score):
    self.name = name
    self.school = school
    self.schoolAddress = schoolAddress
    self.scoreInWritten = f" func1 student scored {score} percentage "
    self.scoreInPercent = score

score =  #calculation using x formula
student_academics_1 = StudentAcademics("John", "xyz school", "xyz address", score)

score =  #calculation using y formula
student_academics_2 = StudentAcademics("Tom", "abc school", "abc address", score)

score =  #calculation using z formula
student_academics_2 = StudentAcademics("Bill", "jkl school", "jkl address", score)

Now when you want to access the data again you can simply do:

print("name {} studied at {}".format(student_academics_1.name, student_academics_1.school))

Let me know if you have any questions further.

  • Related