Python Packages, A Reusable Tool for Engineers

by VIKTOR Team

Engineers across various domains use Python to automate workflows and tackle complex challenges. What used to take days to calculate can now be accomplished in minutes, thanks to Python's robust automation features. Beyond automation, Python's reusability is a powerful asset for all engineers. With basic Python knowledge, you can develop helpful logic to save time. And your fellow engineers can reuse it across different projects. In this article, you’ll learn more about how Python scripts, modules, and packages enable reusability and protect your intellectual property.

Python scripts, modules, and packages

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.

Python scripts

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:

square_column.py 

# input 
a = .. 
# output 
surface_area =  a**2 

Python modules

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:

square_column.py 

def square_column _volume(a,h): 
return a**2*h 
def square_column _cost (a,h,c): 
return a**2*h*c 

Python packages

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:

Geometry 

  - square_column.py 
  - rectangular_column.py 
  - 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.

Protect your IP with private packages

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.

Using packages in your VIKTOR apps

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:

  • Git submodules,
  • Private Git repositories,
  • Private PyPI packages.

Using and sharing Python packages is extremely useful for your app development and code reusability, visit our documentation to learn more.

illustration of start now

Start building apps for free

Start now
Share

Related Blog Posts

Visual Builder: Click to create your UI

Read more

What's new in VIKTOR (April 2024)

Read more

3 tips to increase the adoption of your application

Read more
Start building apps for free
Start now