Skip to main content

Develop Python scripts cross platform


This post was an answer to my own question: https://twitter.com/changliao1025/status/1182847707666247680

When you write code for many moons, usually you will develop some sort of shared library/package for various projects.
These Python scripts may not be suitable for deployment as a package but you still want to use them anywhere.

Another issue is that I often need to write and run code cross platform/cluster. So I want to make sure there is an easy way to configure my code so that I don't have to modify things when I update something.

In my early attempt, I put everything into a package and use the following method to use them in any source code:

With these global variables, now I can import any function within the package:

This seems to work well at the beginning, however, I have to copy/paste this about 20 lines of code in lots of functions. And if I want to add new capability, I have to modify all of them!

So I am on a new quest to fix it.

After digging a little bit, I was able to find a better solution. So instead of defining these system wide or global variables in production code, I can place them into a standalone module.
After that, I only need to import them using 3 or 4 lines:

The magic here is that I added one single line in my bash profile.
With the combination of environment variable "PATH" and Python sys.path, I was able to import all the global variables using one single module.

With that, I can significantly reduce and clean up the code I wrote for several projects. Also, if I need to change something, I only need to change the library, nothing else.
Problem solved.

Cheers!

Comments

Popular posts from this blog

Spatial datasets operations: mask raster using region of interest

Climate change related studies usually involve spatial datasets extraction from a larger domain.
In this article, I will briefly discuss some potential issues and solutions.

In the most common scenario, we need to extract a raster file using a polygon based shapefile. And I will focus as an example.

In a typical desktop application such as ArcMap or ENVI, this is usually done with a tool called clip or extract using mask or ROI.

Before any analysis can be done, it is the best practice to project all datasets into the same projection.

If you are lucky enough, you may find that the polygon you will use actually matches up with the raster grid perfectly. But it rarely happens unless you created the shapefile using "fishnet" or other approaches.

What if luck is not with you? The algorithm within these tool usually will make the best estimate of the value based on the location. The nearest re-sample, but not limited to, will be used to calculate the value. But what about the outp…

Numerical simulation: ode/pde solver and spin-up

For Earth Science model development, I inevitably have to deal with ODE and PDE equations. I also have come across some discussion related to this topic, i.e.,

https://www.researchgate.net/post/What_does_one_mean_by_Model_Spin_Up_Time

In an attempt to answer this question, as well as redefine the problem I am dealing with, I decided to organize some materials to illustrate our current state on this topic.

Models are essentially equations. In Earth Science, these equations are usually ODE or PDE. So I want to discuss this from a mathematical perspective.

Ideally, we want to solve these ODE/PDE with initial condition (IC) and boundary condition (BC) using various numerical methods.
https://en.wikipedia.org/wiki/Initial_value_problem
https://en.wikipedia.org/wiki/Boundary_value_problem

Because of the nature of geology, everything is similar to its neighbors. So we can construct a system of equations which may have multiple equation for each single grid cell. Now we have an array of equation…

Lessons I have learnt during E3SM development

I have been involved with the E3SM development since I joined PNNL as a postdoc. Over the course of time, I have learnt a lot from the E3SM model. I also found many issues within the model, which reflects lots of similar struggles in the lifespan of software engineering.

Here I list a few major ones that we all dislike but they are around in almost every project we have worked on.

Excessive usage of existing framework even it is not meant to Working in a large project means that you should NOT re-invent the wheels if they are already there. But more often, developers tend to use existing data types and functions even when they were not designed to do so. The reason is simple: it is easier to use existing ones than to create new ones. For example, in E3SM, there was not a data type to transfer data between river and land. Instead, developers use the data type designed for atmosphere and land to do the job. While it is ok to do so, it added unnecessary confusion for future development a…