%%html
<!--Script block to left align Markdown Tables-->
<style>
table {margin-left: 0 !important;}
</style>
In object-oriented programming, a class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions or methods). In many languages, the class name is used as the name for the class (the template itself), the name for the default constructor of the class (a subroutine that creates objects), and as the type of objects generated by instantiating the class; these distinct concepts are easily conflated.
When an object is created by a constructor of the class, the resulting object is called an instance of the class, and the member variables specific to the object are called instance variables, to contrast with the class variables shared across the class.
Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to it for maintaining its state. Class instances can also have methods (defined by its class) for modifying its state.
Class definitions, like function definitions (def statements) must be executed before they have any effect. (You could conceivably place a class definition in a branch of an if statement, or inside a function.)
In practice, the statements inside a class definition will usually be function definitions, but other statements are allowed, and sometimes useful — we’ll come back to this later. The function definitions inside a class normally have a peculiar form of argument list, dictated by the calling conventions for methods — again, this is explained later.
When a class definition is entered, a new namespace is created, and used as the local scope — thus, all assignments to local variables go into this new namespace. In particular, function definitions bind the name of the new function here.
When a class definition is left normally (via the end), a class object is created. This is basically a wrapper around the contents of the namespace created by the class definition; we’ll learn more about class objects in the next section. The original local scope (the one in effect just before the class definition was entered) is reinstated, and the class object is bound here to the class name given in the class definition header (ClassName in the example).
An object is simply a collection of data (variables) and methods (functions) that act on those data. Similarly, a class is a blueprint for that object.
We can think of class as a sketch (prototype) of a house. It contains all the details about the floors, doors, windows etc. Based on these descriptions we build the house. House is the object.
As many houses can be made from a house's blueprint, we can create many objects from a class. An object is also called an instance of a class and the process of creating this object is called instantiation
Learn more at
Write a class named 'Tax' to calculate the state tax (in dollars) of Employees at Texas Tech University based on their annual salary.
The state tax is 16% if the annual salary is below 80,000 dollars and 22% if the salary is more than 80,000 dollars.
Employee | Annual salary (dollars) |
---|---|
Bob | 1,50,000 |
Mary | 78,000 |
John | 55,000 |
Danny | 1,75,000 |
Bob's tax amount (in dollars): AMOUNT
Mary's tax amount (in dollars): AMOUNT
John's tax amount (in dollars): AMOUNT
Danny's tax amount (in dollars): AMOUNT
class Tax:
"""This class calculates the tax amount based on the annual salary and the state tax %"""
def __init__(self, salary): # here is the instantiation constructor
self.salary = salary
def taxamount(self): # here is a method (function) that can operate on the class once created
if self.salary < 80000:
return self.salary*(16/100)
else:
return self.salary*(22/100)
bob = Tax(150000) # objects constructed using Tax class
mary = Tax(78000)
john = Tax(55000)
danny = Tax(175000)
dir(Tax)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'taxamount']
dir(mary)
['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'salary', 'taxamount']
print("Bob's tax amount (in dollars):", bob.taxamount())
print("Mary's tax amount (in dollars):", mary.taxamount())
print("John's tax amount (in dollars):", john.taxamount())
print("Danny's tax amount (in dollars):", danny.taxamount())
Bob's tax amount (in dollars): 33000.0 Mary's tax amount (in dollars): 12480.0 John's tax amount (in dollars): 8800.0 Danny's tax amount (in dollars): 38500.0
Numbers, strings, lists, and dictionaries are all objects that are instances of a parent class
print(type(0))
print(type(""))
print(type([1, 2, 3, 4]))
To get more information about the built-in classes and objects, use dir( ) and help( ) functions
print(dir(int))
print(help(""))
User-defined classes: Defining docstrings
class Dog:
"""This class enables the dog to say its name and age in dog years"""
def __init__(self, name, years):
"""This function contains all the necessary attributes"""
self.name = name
self.years = years
self.dog_age = years*9
def sound(self):
"""This function enables the dog to speak"""
print("woof! I am {} and I am {} dog years old! woof!".format(self.name, self.dog_age))
fudge = Dog("Fudge", 2)
maple = Dog("Maple", 1.5)
fudge.sound()
maple.sound()
woof! I am Fudge and I am 18 dog years old! woof! woof! I am Maple and I am 13.5 dog years old! woof!
help(Dog)
Help on class Dog in module __main__: class Dog(builtins.object) | Dog(name, years) | | This class enables the dog to say its name and age in dog years | | Methods defined here: | | __init__(self, name, years) | This function contains all the necessary attributes | | sound(self) | This function enables the dog to speak | | ---------------------------------------------------------------------- | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined)
A computer file is a computer resource for recording data discretely (not in the secretive context, but specifically somewhere on a piece of hardware) in a computer storage device. Just as words can be written to paper, so can information be written to a computer file. Files can be edited and transferred through the internet on that particular computer system.
There are different types of computer files, designed for different purposes. A file may be designed to store a picture, a written message, a video, a computer program, or a wide variety of other kinds of data. Some types of files can store several types of information at once.
By using computer programs, a person can open, read, change, save, and close a computer file. Computer files may be reopened, modified, and copied an arbitrary number of times.
Typically, files are organised in a file system, which keeps track of where the files are located on disk and enables user access.
In computing, a file system or filesystem, controls how data is stored and retrieved. Without a file system, data placed in a storage medium would be one large body of data with no way to tell where one piece of data stops and the next begins. By separating the data into pieces and giving each piece a name, the data is isolated and identified. Taking its name from the way paper-based data management system is named, each group of data is called a “file”. The structure and logic rules used to manage the groups of data and their names is called a “file system”.
A path, the general form of the name of a file or directory, specifies a unique location in a file system. A path points to a file system location by following the directory tree hierarchy expressed in a string of characters in which path components, separated by a delimiting character, represent each directory. The delimiting character is most commonly the slash (”/”), the backslash character (”\”), or colon (”:”), though some operating systems may use a different delimiter. Paths are used extensively in computer science to represent the directory/file relationships common in modern operating systems, and are essential in the construction of Uniform Resource Locators (URLs). Resources can be represented by either absolute or relative paths. As an example consider the following two files:
They both have the same file name, but are located on different paths. Failure to provide the path when addressing the file can be a problem. Another way to interpret is that the two unique files actually have different names, and only part of those names is common (Guest.conf) The two names above (including the path) are called fully qualified filenames (or absolute names), a relative path (usually relative to the file or program of interest depends on where in the directory structure the file lives. If we are currently in the .git directory (the first file) the path to the file is just the filename.
We have experienced path issues with dependencies on .png files - in general your JupyterLab notebooks on CoCalc can only look at the local directory which is why we have to copy files into the directory for things to work.
Text Files. Text files are regular files that contain information readable by the user. This information is stored in ASCII. You can display and print these files. The lines of a text file must not contain NULL characters, and none can exceed a prescribed (by architecture) length, including the new-line character. The term text file does not prevent the inclusion of control or other nonprintable characters (other than NUL). Therefore, standard utilities that list text files as inputs or outputs are either able to process the special characters gracefully or they explicitly describe their limitations within their individual sections.
Binary Files. Binary files are regular files that contain information readable by the computer. Binary files may be executable files that instruct the system to accomplish a job. Commands and programs are stored in executable, binary files. Special compiling programs translate ASCII text into binary code. The only difference between text and binary files is that text files have lines of less than some length, with no NULL characters, each terminated by a new-line character.
Directory Files. Directory files contain information the system needs to access all types of files, but they do not contain the actual file data. As a result, directories occupy less space than a regular file and give the file system structure flexibility and depth. Each directory entry represents either a file or a subdirectory. Each entry contains the name of the file and the file's index node reference number (i-node). The i-node points to the unique index node assigned to the file. The i-node describes the location of the data associated with the file. Directories are created and controlled by a separate set of commands.
For this lesson we examine just a handfull of file manipulations which are quite useful. Files can be "created","read","updated", or "deleted" (CRUD).
Below is an example of creating a file that does not yet exist. The script is a bit pendandic on purpose.
First will use some system commands to view the contents of the local directory
import sys
! rm -rf myfirstfile.txt # delete file if it exists
! pwd # list name of working directory, note it includes path, so it is an absolute path
/home/compthink/engr-1330-webroot/1-Lessons/Lesson06/OriginalPowerpoint
! ls -l # list contents of working directory
total 116 -rw-rw-r-- 1 compthink compthink 9353 Feb 7 01:33 ClassObjects_FileHandling_LabSession.ipynb -rw-rw-r-- 1 compthink compthink 38589 Feb 7 02:07 ClassObjects_FileHandling_LectureSession.ipynb -rw-rw-r-- 1 compthink compthink 46643 Feb 7 02:19 ENGR-1330-Lesson6-Dev.ipynb -rw-rw-r-- 1 compthink compthink 524 Feb 7 01:33 ReadingFile.txt -rw-rw-r-- 1 compthink compthink 111 Feb 7 01:33 sample-Copy1.txt -rw-rw-r-- 1 compthink compthink 153 Feb 7 01:33 sample.txt drwxrwxr-x 3 compthink compthink 4096 Feb 7 01:33 src
# create file example
externalfile = open("myfirstfile.txt",'w') # create connection to file, set to write (w), file does not need to exist
mymessage = 'message in a bottle' #some object to write, in this case a string
externalfile.write(mymessage)# write the contents of mymessage to the file
externalfile.close() # close the file connection
At this point our new file should exist, lets list the directory and see if that is so
! ls -l # list contents of working directory
total 120 -rw-rw-r-- 1 compthink compthink 9353 Feb 7 01:33 ClassObjects_FileHandling_LabSession.ipynb -rw-rw-r-- 1 compthink compthink 38589 Feb 7 02:07 ClassObjects_FileHandling_LectureSession.ipynb -rw-rw-r-- 1 compthink compthink 46643 Feb 7 02:19 ENGR-1330-Lesson6-Dev.ipynb -rw-rw-r-- 1 compthink compthink 524 Feb 7 01:33 ReadingFile.txt -rw-rw-r-- 1 compthink compthink 19 Feb 7 02:20 myfirstfile.txt -rw-rw-r-- 1 compthink compthink 111 Feb 7 01:33 sample-Copy1.txt -rw-rw-r-- 1 compthink compthink 153 Feb 7 01:33 sample.txt drwxrwxr-x 3 compthink compthink 4096 Feb 7 01:33 src
Sure enough, its there, we will use a bash
command cat
to look at the contents of the file.
! cat myfirstfile.txt
message in a bottle
We will continue using the file we just made, and read from it the example is below
# read file example
externalfile = open("myfirstfile.txt",'r') # create connection to file, set to read (r), file must exist
silly_string = externalfile.read() # read the contents
externalfile.close() # close the file connection
print(silly_string)
message in a bottle
This example continues with our same file, but we will now add contents without destroying existing contents. The keyword is append
externalfile = open("myfirstfile.txt",'a') # create connection to file, set to append (a), file does not need to exist
externalfile.write('\n') # adds a newline character
what_to_add = 'I love rock-and-roll, put another dime in the jukebox baby ... \n'
externalfile.write(what_to_add) # add a string including the linefeed
what_to_add = '... the waiting is the hardest part \n'
externalfile.write(what_to_add) # add a string including the linefeed
mylist = [1,2,3,4,5] # a list of numbers
what_to_add = ','.join(map(repr, mylist)) + "\n" # one way to write the list
externalfile.write(what_to_add)
what_to_add = ','.join(map(repr, mylist[0:len(mylist)])) + "\n" # another way to write the list
externalfile.write(what_to_add)
externalfile.close()
As before we can examine the contents using a shell command sent from the notebook.
! cat myfirstfile.txt
message in a bottle I love rock-and-roll, put another dime in the jukebox baby ... ... the waiting is the hardest part 1,2,3,4,5 1,2,3,4,5
Delete can be done by a system call as we did above to clear the local directory
In a JupyterLab notebook, we can either use
import sys
! rm -rf myfirstfile.txt # delete file if it exists
or
import os
os.remove("myfirstfile.txt")
they both have same effect, both equally dangerous to your filesystem.
Learn more about CRUD with text files at https://www.guru99.com/reading-and-writing-files-in-python.html
Learn more about file delete at https://www.dummies.com/programming/python/how-to-delete-a-file-in-python/
import os
file2kill = "myfirstfile.txt"
try:
os.remove(file2kill) # file must exist or will generate an exception
except:
pass # example of using pass to improve readability
print(file2kill, " missing or deleted !")
myfirstfile.txt missing or deleted !
A little discussion on the part where we wrote numbers
what_to_add = ','.join(map(repr, mylist[0:len(mylist)])) + "\n"
Here are descriptions of the two functions map
and repr
map(function, iterable, ...)
Apply function
to every item of iterable and return a list of the results.
If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel.
If one iterable is shorter than another it is assumed to be extended with None items.
If function is None, the identity function is assumed; if there are multiple arguments, map()
returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation).
The iterable arguments may be a sequence or any iterable object; the result is always a list.
repr(object)
Return a string containing a printable representation of an object.
This is the same value yielded by conversions (reverse quotes).
It is sometimes useful to be able to access this operation as an ordinary function.
For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval()
, otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object.
A class can control what this function returns for its instances by defining a repr()
method.
What they do in this script is important. The statement:
what_to_add = ’,’.join(map(repr, mylist[0:len(mylist)])) + "\n"
is building a string that will be comprised of elements of mylist[0:len(mylist)].
The repr()
function gets these elements as they are represented in the computer, the delimiter a comma is added using the join method in Python, and because everything is now a string the
... + "\n"
puts a linefeed character at the end of the string so the output will start a new line the next time something is written.
# create the "My Favorite Quotation" file:
externalfile = open("MyFavoriteQuotation.txt",'w') # create connection to file, set to write (w)
myquotation = 'The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness. For he is truly his brother’s keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon you.' #My choice: quotation from Pulp Fiction
externalfile.write(myquotation)# write the contents of mymessage to the file
externalfile.close() # close the file connection
#Let's read the file
! cat MyFavoriteQuotation.txt
# Let's add the string
externalfile = open("MyFavoriteQuotation.txt",'a') #create connection to file, set to append (a)
externalfile.write('\n') # adds a newline character
what_to_add = "And that's something I wish I had said ... \n"
externalfile.write(what_to_add)
externalfile.close()
#Let's read the file one last time
! cat MyFavoriteQuotation.txt
The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness. For he is truly his brother’s keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon you.The path of the righteous man is beset on all sides by the inequities of the selfish and the tyranny of evil men. Blessed is he who, in the name of charity and good will, shepherds the weak through the valley of darkness. For he is truly his brother’s keeper and the finder of lost children. And I will strike down upon thee with great vengeance and furious anger those who attempt to poison and destroy my brothers. And you will know my name is the Lord when I lay my vengeance upon you. And that's something I wish I had said ...
Overland, B. (2018). Python Without Fear. Addison-Wesley ISBN 978-0-13-468747-6.
Grus, Joel (2015). Data Science from Scratch: First Principles with Python O’Reilly Media. Kindle Edition.
Precord, C. (2010) wxPython 2.8 Application Development Cookbook Packt Publishing Ltd. Birmingham , B27 6PA, UK ISBN 978-1-849511-78-0.
# Preamble script block to identify host, user, and kernel
import sys
! hostname
! whoami
print(sys.executable)
print(sys.version)
print(sys.version_info)
atomickitty compthink /opt/jupyterhub/bin/python3 3.8.5 (default, Jul 28 2020, 12:59:40) [GCC 9.3.0] sys.version_info(major=3, minor=8, micro=5, releaselevel='final', serial=0)