HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

A large number of math type equations and functions in a python module

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numberequationsmoduletypelargemathpythonandfunctions

Problem

I have roughly 40 or 50 functions that are similar to the one provided by the code below.

I kept all the common calculations between multiple functions separate such as service_coverage_area . I then kept all the specific calculations that are not repeated or needed by any other functions in the function that needs those calculations such as calculating f_sc.

My question is: Am I doing this right? as in is this the best practice to code utility functions? should I be separating all the mini calculations into separate functions but then have to deal with 200 functions instead of the current 50?

Note that the self is there cause this code is part of a class. I just copy and pasted 2 functions into this paste

```
import bisect

def service_coverage_area(self, mode):
#simplified for codereview
return 1

def some_calc(self, mode, street_type, grade, elderly_proportion, f_px):
""" where:
r = r_0 f_sc f_s f_pop f_px

r_0 = ideal transit stop service radius
400m for bus and 800m for rail / express bus and commuter
needed variable: mode

f_sc = street connectivity factor
{'grid':1, 'hybrid':0.85, 'cul_de_sac':0.45}
needed variable: street connectivity tye

f_s = grade fator
0.00 to 0.05 -> 1.00
0.06 to 0.08 -> 0.95
0.09 to 0.11 -> 0.80
0.12 to 0.15 -> 0.65
needed variable: grade

f_pop population factor
1 for normal population
0.85 of 20% is elderly population
needed variable:elderly_proportion

f_px pedestrian crossing factor
provided by another calculation
"""
#calculate r_0
r_0 = self.service_coverage_area(mode)

#calculate f_sc
if street_type == 'grid':
f_sc = 1
elif street_type == 'hybrid':
f_sc = 0.85
elif street_type == 'cul_de_sac':
f_sc = 0.45

#calculate f_s
grades = [0.05, 0.0

Solution

Am I doing this right?

Almost.


as in is this the best practice to code utility functions?

These aren't "utility" functions. They appear to be central to your app. There are rarely "utility" functions in any app.

What we used to call "utility" functions are almost always part of a standard library.


should I be separating all the mini calculations into separate functions

Yes.


but then have to deal with 200 functions instead of the current 50?

You still have the calculations. You're just giving them names and keeping them separate. Giving them separate names makes them more reusable, easier to test and easier to find.

The most important thing here is to avoid creating a (nearly) useless class who's only job is to contain a bunch of functions.

Your code uses no instance variables and simply uses another function that happens to be in the class. Both of these could be @staticmethod method functions.

There's no reason -- in Python -- to create a class unless you have instance variables and a change in state of some kind.

[In Java, you must often create "all-static" classes because there's no other place to put stateless math like this.]

You can simply put these functions into a simple module and avoid the class definitions.

Context

StackExchange Code Review Q#3696, answer score: 5

Revisions (0)

No revisions yet.