Often a class will be defined in a .py file that is separate from the notebook. Luckily, Python (and other languages) provide an easy way to provide the class definition information to your notebook scripts.
The process we will use is called importing. This is an incredibly important concept, because most of your interaction with Python will happen by using pre-existing software packages that have classes, properties, and methods that organize data and perform tasks that make your life easier.
For example, the matplotlib package was designed specifically to provide an easy-to-use interface for making visual representations of data through a process called “plotting”.
In this basic case, however, we are simply importing a file named my_file.py that looks like this:
class Observation:
def __init__(self, high, low, dew, pres):
self.high_temperature = high
self.low_temperature = low
self.dewpoint = dew
self.pressure = pres
def F_to_K(self):
K = (self.high_temperature + 459.67) * (5 / 9)
return K
def F_to_C(self):
C = (self.high_temperature - 32) * (5 / 9)
return CIt has a constructor method that requires the user to define high and low temperatures, a dewpoint, and a pressure. The class also has methods that convert temperatures from Fahrenheit to Kelvin, and from Fahrenheit to Celsius.
Chapter 4.2.1 - importing practice¶
In Colab, upload this notebook and open the project manager menu (looks like three dots and three lines on the left side of the page). Then click on the folder icon. Once the runtime is connected, right click in the area under “sample_data” and select “New File” and name it my_file.py.
Copy the Observation class code above (do not copy the ``````). Double click on my_file.py in Colab and it will open a text editor on the right side of the page. Paste the Observation code into that editor.
Now, run the code below. If you set this up correctly, it should print out the following prompt:
The high temperature is 85
The low temperature is 65
The dewpoint is 64
The pressure is 29.4
High in K = 302.5944444444445
High in C = 29.444444444444446from my_file import Observation
obs = Observation(high=85, low=65, dew=64, pres=29.4)
print("The high temperature is", obs.high_temperature)
print("The low temperature is", obs.low_temperature)
print("The dewpoint is", obs.dewpoint)
print("The pressure is", obs.pressure)
print("High in K =", obs.F_to_K())
print("High in C =", obs.F_to_C())The high temperature is 85
The low temperature is 65
The dewpoint is 64
The pressure is 29.4
High in K = 302.5944444444445
High in C = 29.444444444444446
Practice problems - EX8
These are equivalent to EX8 problems. You can copy/paste your solutions as answers and submit using that notebook. Please also provide a copy of your my_file.py.
Problem 1
Add a method to Observation in the text editor for my_file.py named formatted_conversion that looks like this:
def formatted_conversion(self, conversion):
# your code here
return conversion_strThat provides the str representation of the conversion result with two decimal places using the code below:
print("The high temperature is", obs.high_temperature)
print("The low temperature is", obs.low_temperature)
print("The dewpoint is", obs.dewpoint)
print("The pressure is", obs.pressure)
print("High in K =", obs.formatted_conversion('F_to_K'))
print("High in C =", obs.formatted_conversion('F_to_C'))---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[3], line 1
----> 1 print("The high temperature is", obs.high_temperature)
2 print("The low temperature is", obs.low_temperature)
3 print("The dewpoint is", obs.dewpoint)
NameError: name 'obs' is not definedProblem 2
Add a new property named unit in the constructor that identifies the input unit as ‘F’, ‘C’, or ‘K’. Add the the methods C_to_F and K_to_F and update formatted_conversion to include these functions as options. If the user does not use the correct method based on unit. Provide an error message saying “incorrect unit. Expected ‘F_to_C’ or ‘F_to_K’” depending on the unit. For example, if they try the option “F_to_C”, but the unit is “K”, the user should only be able to use ‘K_to_F’ or ‘K_to_C’.
Provide cases that prove you implemented the requirements correctly.