Coding in python is known for its readability for other programmers. One important way of creating readable code in python is through using libraries and packages. They have some similarities and important differences so I am going to go through each of them in more detail.
Python Packages
A python package typically consists of several modules and is a parent folder containing multiple other folders inside of it. Similar to how we use file explorer on our computers to save information within a folder. In visual code it is also easy to see folders within folders. One purpose of these packages is to create a more hierarchal structure hence the readability of python but it also allows for the ability to share modules with other packages in your code. You now have the ability to write functions and put them into a more universal scope by sharing those functions to other packages within your code. Take a look at the image below.
One purpose of these packages is to create a more hierarchal structure hence the readability of python but it also allows for the ability to share modules with other packages in your code.
If you wanted to share a function within your Training package with your submission package you could do the following at the top of one of your Submission package files that will be needing the function. From the hierarchy shown here it would be either submit.py or run_context.py. You would then type the following.
from my_model.training import dataset
Python Libraries
Libraries can have a similar feel to packages but also have some key differences. A library is a collection of modules that are frequently used that a developer can take advantage of to make their coding easier. It allows for less redundancy of code and usually the ability to use "shortcuts" to get the same result by taking advantage of a pre made library. Libraries allow us to "stand on the shoulders of giants" as my professor would say by using their pre written code that is being run under the hood in these libraries. You will know you are using a library if it does npt have its own file but it does show up in your Pipfile. An example of this is using flask_restful to create APIs. You can code out each API without using flask_restful but if you import the library there is pre built functionality that makes creating APIs faster and more modular as well as reducing the risk for any errors while typing the code. Importing a library is fairly simple. Within the file you are wanting to use the functionality within you will type the following code at the top of the page.
From "library" import "function"
or in the flask_restful scenario
From Flask_restful import Resource
Now you can use the Resource functionality within your code.
You can google libraries for Python or really any language you are coding in to find great time saving tools to use within your code to increase functionality and readability.
Top comments (0)