patternpythonMinor
Using an Equation from another Equation based on **kwargs
Viewed 0 times
kwargsanotherusingbasedfromequation
Problem
What my code does is that it takes input as to which function to use, and then plugs in the appropriate data in to the given function.
For example:
The 1 is the equation ID, and the rest are user parameters
```
import Constants as C
import Errors as E
def force_bouyant( density , volume ):
'''Returns the Buoyant Force Given Density and Volume'''
return C.F_GRAVITY density volume
def force_centripetal( mass , velocity, radius ):
'''Returns the Centripetal Force Given Mass, Velocity, and Radius'''
return ( ( mass velocity velocity ) / float(radius) )
def force_electrical( q1, q2, radius ):
'''Returns the Electrical Attraction Force Given the Charges of the Two Particles, and the Distance Between Them'''
if radius == 0:
raise E.InputError("Radius Cannot Be Zero")
else:
top = C.K_CONSTANT q1 q2
return ( top / float( radius * radius ) )
def force_friction( nForce , Rcoeff ):
'''Returns the Force of Friction Given the Normal Force, and the Coefficient of Friction'''
return ( nForce * Rcoeff)
def force_friction_nNF( mass , Rcoeff):
'''Returns the Force of Friction When the Normal Force isn't Given, but Mass is'''
return ( mass C.F_GRAVITY Rcoeff )
def force_given_ma( mass , acceleration ):
'''Returns the Force Given the Mass and the Acceleration'''
return ( mass * acceleration )
def force_given_pt( momentum , time ):
'''Returns the Force Given the Change in Momentum and the Change in Time'''
return ( momentum / float(time) )
def force_gravity( mass1, mass2, radius ):
'''Returns the Force of Gravity Given the Masses of the two Objects, and the Distance Between Them'''
top = ( C.GRAV_CONSTANT mass1 mass2 )
return ( -top / float( radius ) )
def force(equation_id, **kwargs):
'''
Returns the Force with the Kwargs being the Type of Force Equation
Valid argumen
For example:
- User input: force(1, density = 10.0, volume = 5.0)
- Result: 490.5
The 1 is the equation ID, and the rest are user parameters
```
import Constants as C
import Errors as E
def force_bouyant( density , volume ):
'''Returns the Buoyant Force Given Density and Volume'''
return C.F_GRAVITY density volume
def force_centripetal( mass , velocity, radius ):
'''Returns the Centripetal Force Given Mass, Velocity, and Radius'''
return ( ( mass velocity velocity ) / float(radius) )
def force_electrical( q1, q2, radius ):
'''Returns the Electrical Attraction Force Given the Charges of the Two Particles, and the Distance Between Them'''
if radius == 0:
raise E.InputError("Radius Cannot Be Zero")
else:
top = C.K_CONSTANT q1 q2
return ( top / float( radius * radius ) )
def force_friction( nForce , Rcoeff ):
'''Returns the Force of Friction Given the Normal Force, and the Coefficient of Friction'''
return ( nForce * Rcoeff)
def force_friction_nNF( mass , Rcoeff):
'''Returns the Force of Friction When the Normal Force isn't Given, but Mass is'''
return ( mass C.F_GRAVITY Rcoeff )
def force_given_ma( mass , acceleration ):
'''Returns the Force Given the Mass and the Acceleration'''
return ( mass * acceleration )
def force_given_pt( momentum , time ):
'''Returns the Force Given the Change in Momentum and the Change in Time'''
return ( momentum / float(time) )
def force_gravity( mass1, mass2, radius ):
'''Returns the Force of Gravity Given the Masses of the two Objects, and the Distance Between Them'''
top = ( C.GRAV_CONSTANT mass1 mass2 )
return ( -top / float( radius ) )
def force(equation_id, **kwargs):
'''
Returns the Force with the Kwargs being the Type of Force Equation
Valid argumen
Solution
According to PEP8, Python's official style guide, your code has 255 violations of best practice, primarily cause of the extraneous whitespace:
The extraneous whitespace violates best practice; it should look like this:
It may look really nice to indent everything like that, but it's just plain wrong.
And also, there shouldn't be a space before the comma in an argument list.
On a more technical note, the following can and should be improved:
First,
Don't swap usage of
The magic numbers in the
Correct the grammar in all your doc strings and return messages, consider your usage of capitalisation and pronouns.
If you're working out one of a few equations, only generate the variables needed for that equation, don't generate all of them in a block.
Your usage of
def force_given_ma( mass , acceleration ):
'''Returns the Force Given the Mass and the Acceleration'''
return ( mass * acceleration )The extraneous whitespace violates best practice; it should look like this:
def force_given_ma(mass , acceleration):
'''Returns the Force Given the Mass and the Acceleration'''
return (mass * acceleration)accel = kwargs.get( 'acceleration' , 0 )
charge = kwargs.get( 'charge' , 0 )
charge2 = kwargs.get( 'charge2' , 0 )
dense = kwargs.get( 'density' , 0 )
fcoeff = kwargs.get( 'friction_coefficient' , 0 )
mass = kwargs.get( 'mass' , 0 )
mass2 = kwargs.get( 'mass2', 0 )
moment = kwargs.get( 'momentum' , 0 )
nForce = kwargs.get( 'normal_force' , 0 )
radius = kwargs.get( 'radius' , 0 )
time = kwargs.get( 'time' , 0 )
veloc = kwargs.get( 'velocity' , 0 )
volume = kwargs.get( 'volume' , 0 )It may look really nice to indent everything like that, but it's just plain wrong.
And also, there shouldn't be a space before the comma in an argument list.
On a more technical note, the following can and should be improved:
if equation_id == 1:
ret = force_bouyant( dense , volume )
elif equation_id == 2:
ret = force_centripetal( mass, veloc, radius )
elif equation_id == 3:
ret = force_electrical( charge , charge2, radius )
elif equation_id == 4:
ret = force_gravity( mass , mass2 , radius )
elif equation_id == 5:
if ( ( nForce == 0 ) and ( mass != 0 ) ):
ret = force_friction_nNF( mass , fcoeff )
elif ( nForce != 0 ):
ret = force_friction( nForce , fcoeff )
else:
raise E.InputError("No Normal Force Data was input")
elif equation_id == 6:
ret = force_given_ma( mass, accel )
elif equation_id == 7:
ret = force_given_pt( moment , time )
else:
raise E.InputError("Invalid or No Equation Input")
return retFirst,
ret shouldn't need to be used, return the result directly.Don't swap usage of
return between in brackets and not.The magic numbers in the
if-else conditionals, should be moved to a dictionary.Correct the grammar in all your doc strings and return messages, consider your usage of capitalisation and pronouns.
If you're working out one of a few equations, only generate the variables needed for that equation, don't generate all of them in a block.
Your usage of
E and C as libraries is a little... misleading. Keep them as Errors and Constants, no need to simplify them.Code Snippets
def force_given_ma( mass , acceleration ):
'''Returns the Force Given the Mass and the Acceleration'''
return ( mass * acceleration )def force_given_ma(mass , acceleration):
'''Returns the Force Given the Mass and the Acceleration'''
return (mass * acceleration)accel = kwargs.get( 'acceleration' , 0 )
charge = kwargs.get( 'charge' , 0 )
charge2 = kwargs.get( 'charge2' , 0 )
dense = kwargs.get( 'density' , 0 )
fcoeff = kwargs.get( 'friction_coefficient' , 0 )
mass = kwargs.get( 'mass' , 0 )
mass2 = kwargs.get( 'mass2', 0 )
moment = kwargs.get( 'momentum' , 0 )
nForce = kwargs.get( 'normal_force' , 0 )
radius = kwargs.get( 'radius' , 0 )
time = kwargs.get( 'time' , 0 )
veloc = kwargs.get( 'velocity' , 0 )
volume = kwargs.get( 'volume' , 0 )if equation_id == 1:
ret = force_bouyant( dense , volume )
elif equation_id == 2:
ret = force_centripetal( mass, veloc, radius )
elif equation_id == 3:
ret = force_electrical( charge , charge2, radius )
elif equation_id == 4:
ret = force_gravity( mass , mass2 , radius )
elif equation_id == 5:
if ( ( nForce == 0 ) and ( mass != 0 ) ):
ret = force_friction_nNF( mass , fcoeff )
elif ( nForce != 0 ):
ret = force_friction( nForce , fcoeff )
else:
raise E.InputError("No Normal Force Data was input")
elif equation_id == 6:
ret = force_given_ma( mass, accel )
elif equation_id == 7:
ret = force_given_pt( moment , time )
else:
raise E.InputError("Invalid or No Equation Input")
return retContext
StackExchange Code Review Q#106782, answer score: 4
Revisions (0)
No revisions yet.