Sign In
April 04, 2024
by VIKTOR Team
If you want to learn more about the reusability of your Python code, you first have to understand how code is organized, starting with a script, then a module, followed by a package.
A script is a stand-alone piece of code with clear start and end points. For example, a script can be used to calculate the area of a square-shaped column. While scripts offer low reusability, they can be copied and used by engineers for similar calculations in other structural engineering projects.
Script example:
1square_column.py
2
3# input
4a = ..
5# output
6surface_area = a**2
If you want to automate more parts in the structural design process, you will build a few more scripts for other calculations, such as column volume and cost estimation. As your scripts are adding up, you can bundle them into a module.
A module is a reusable group of logically related scripts. It is usually structured in custom-defined functions. Your colleagues can import your module and use it in their projects. Splitting code into modules enhances code maintainability and scalability, as each module addresses a certain output of your workflow.
Module example:
1square_column.py 2 3def square_column _volume(a,h): 4return a**2*h 5def square_column _cost (a,h,c): 6return a**2*h*c
A package is a collection of related Python modules organized in a directory hierarchy. A package folder usually contains one file named ‘init.py’ that might contain code to run upon package initialization. Packages offer the highest level of reusability by allowing you to structure and easily distribute code.
Looking at our last example, you can have a package that executes the last modules (and maybe more) for any shaped column.
Package example:
1Geometry 2 3 - square_column.py 4 - rectangular_column.py 5 - circular_column.py
Packages allow you to organize code into distinct namespaces. Think of a namespace as a container that holds names (like variables, functions, modules, etc.) and makes sure they don't clash with each other. It's a way to organize and keep track of names so you can use them without accidentally using the wrong one.
For example, if you have two different parts of your program that each have a variable called "x", namespaces help Python know which "x" you're talking about at any given time. This structure simplifies the reusability and maintenance for engineers in large projects, and this is why Packages are very helpful. Additionally, you can find many helpful packages ready for use on PyPI, making packages easy to apply to your projects.
Packages can be valuable for your team in even more ways. In various scenarios, you want to tailor public packages to suit your specific project goals. Or, you might want to include a package that is only available privately (e.g. code stored on a centralized repository within your company). In these situations, you can create private packages instead. You can easily edit a public package and publish it privately to your team! This way, you protect your (company’s) intellectual property without having to rewrite anything.
With VIKTOR, you can easily include public and private packages in your apps.
Packages that are publicly available on PyPI can be specified in your app's requirements.txt file to become available in your app as a dependency.
For your private packages, there are different options to make them available in a (published) app, without the need to copy the code manually into your app folder:
Using and sharing Python packages is extremely useful for your app development and code reusability, visit our documentation to learn more.