Posted on Leave a comment

Structure python code with packages – import functions from different files

A modular application is an application composed of loosely coupled, functional units called modules, and these modules can be linked together to form a larger application. When you implement your python applications you should create such structures instead of big monoliths.

In this guide I will demonstrate how you can call python function from different folders and files (called packages). I have created a flask application which has as entrypoint the app.py file.

The solution is structured as shown below. The app folder is the root folder which contains all the code for the application. In the root hierarchy the app.py is placed along with other folders as templates, static and helpers.

As I need to create a helper function that will request data from an external API I created a file named github inside my helpers folder and I defined a function within it.

The function is very simple and returns a simple message. Code for github.py can be found below.

def my_function():
  return "Hello from function"

In order to call your helper functions from your main app or from another python package you should first import package

from helpers import github

and then use the function.

@app.route("/")
def home():
    return github.my_function()

This code will display on my flask main page the message of the function.

You can perform in such way any other activities by specifying your python file and then the function.

pythonFile.Function()
Posted on Leave a comment

Move files to replicated datastore using bat script

You can use a very simple bat script in order to automatically move files from a specific windows disk to another one.

An example for this particular scenario is a case that you want to copy files from a disk to another one that resides in a datastore that is replicated. You can automatically move those files from the first disk to the second one in order to get them replicated.

All you have to do, is to create an automatic task with windows task scheduler that its action will be to run the following bat script.

xcopy /s/e/y C:\Users\USER1\Desktop\folder1 D:\Users\folder2
echo %time% %date% >> D:\Users\folder2\log.txt