TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Programming Languages / Python / Software Development

How To Get Started With Native Python

This tutorial shows how to use native Python to create a to-do list, which is a great starting point for developing apps.
Sep 27th, 2024 8:00am by
Featued image for: How To Get Started With Native Python
Featured image via Unsplash.

Starting with native Python is an excellent way to begin developing applications. It allows you to concentrate on the functional and logical aspects of your application without the need to adhere to the specific rules of a framework. In this guide, we’ll build a to-do list in native Python.

Creating a to-do list is a great starting point for developing apps because it incorporates elements similar to those found in production-level web applications. A to-do list is essentially a CRUD application, meaning it allows you to Create, Read, Update, and Delete elements. Most web applications have some or all of these CRUD functionalities.

By working with native Python for this application, you’ll be able to focus on building the core aspects of a Python application without worrying about the complexities of  a server or framework. Let’s get started on our to-do list project!

Getting Started

First, make sure you have Python installed on your machine. If you don’t, then running pip install python will add the language to your machine. Next, create a new project folder and open your IDE. I use VS Code. Once you’re in your editor create a main Python file. This is where we’ll write the complete application. You can do that by manually adding a file or running touch file_name.py. I named my file todo_main.py.

To create a fully functioning to-do list, we’ll need to add the following functions:

  • help: Prints the usage instructions in the terminal.
  • add: Adds and saves a new task to the task file.
  • list: Lists all the tasks in the task file with their status.
  • complete: Marks a task as done.
  • remove: Removes a task from the task file.
  • save: Saves the tasks to a task file.
  • load: Loads the tasks from a task file.
  • main: The application’s main function. Main creates usable command-line arguments and processes them while calling the corresponding functional logic.

Let’s Start Building!

At the top of our Python file, we’ll need to create one import statement. This import statement will be import sys. The sys module in Python provides various functions and variables that are used to manipulate different parts of the Python runtime environment. It allows operating on the interpreter as it provides access to the variables and functions that interact strongly with the interpreter.

Function Order

The order in which we build these functions isn’t the only way we could have ordered them. This is just the way I chose to order the functions based on what made logical sense to me. If you’re stepping away from this order or building your own application, it’s important to define functions before they’re called. It’s also important to build application logic in a way that makes sense to you, the developer, but also will make sense to anyone else reading and operating on this code with the appropriate baseline knowledge.

In this application, we have two classifications for functions: task functions and utility functions. Task functions directly manipulate the to-do list tasks. They handle operations like adding tasks, revealing the list of tasks, marking tasks as done, and removing tasks. These functions are the core functionalities of the application. Utility functions support the task functions by handling operations that are not specific to any single function. They provide general-purpose functionality that can be reused across the task functions. The utility functions in this application focus on loading and saving tasks as well as providing the help interface.

Building the Utility Functions

We’re going to build the utility functions first since those have parts that the task functions will use.

save  Function

The main purpose of the save function is to save the tasks as they’re added by the user. The save function takes in the tasks parameter. The tasks parameter includes the dictionary containing all tasks. The saving process will include opening the tasks.txt file in write mode. It will then iterate through each task in the tasks dictionary. Once it gets to the end of the list, it will write each task’s ID, description, and done status to a file in a specific format.

load Function

Next, let’s tackle the load tasks functionality. The purpose of the load function is to load tasks from a file into the tasks dictionary. It doesn’t need any parameters. load will initialize an empty dictionary to store tasks. It will then try to open the tasks.txt file in read mode. Since we’re building logic that’s contingent on something, we’ll need to add error handling. For this error handling, we’re going to use a try block. If the file is found, the function will read each line, split the line into task_id, description, and done. If the file is not found, then we’ll handle FileNotFoundError by doing nothing. We’ll then return the populated or empty tasks dictionary.

help Function

The last utility function we’ll need to build is the help function. The help function will display the usage instructions for the application in the terminal. It doesn’t need any parameters as it will simply detail the available commands and their syntax.

Now we’re ready to move on to the…

Task Functions

The task functions will handle operations such as adding tasks, listing the tasks, removing tasks, and making a task complete.

add Function

The purpose of this function is to add a new task to the tasks dictionary that we created in the utility functions. The parameters required for this function to complete are the tasks dictionary and the task_description . We’ll add the task descriptions later through the terminal. This function will generate a new task ID based on the number of tasks currently in the list. It will then add the task to the tasks dictionary with its done status set to False. Then it will save the uploaded task to the file using the save function.

list Function

Next, let’s add the functionality to display the previously uploaded tasks and their status in the console. The parameters needed for this task are the tasks dictionary. This function will iterate through each task in the tasks dictionary and determine the status of each task. The options are Done or Pending.

complete Function

The purpose of this next function, complete, is to mark a specific task as done. For this, we’ll need to take in two parameters: the tasks dictionary and the specific task_id. To complete this function, we’ll need to first check to see if the task_id exists in the tasks dictionary. If it doesn’t exist, we’ll need to return an error to the console. For error handling, we’re going to use an if-else statement. If the task_id exists, then we can set the done status to True. If it doesn’t exist, then we’ll return the error.

remove Function

The remove logic is designed to remove a specified task from the tasks dictionary. It will need to take in the tasks dictionary and the task_id as parameters. Similarly to the complete function, we’ll need to perform different operations if thetask_id is found in the dictionary or if it’s not. If the task ID exists, then we can delete the task from the dictionary and use the save  function to upload the new list of tasks to the file and print a confirmation message. If it doesn’t exist, then we’ll need to return the error message to the console.

main Function

This is the most complicated functionality in our application. It puts everything we already wrote together and handles command-line arguments to execute the appropriate task operation. The main function is the entry point of the application, handles command-line arguments, and executes the appropriate operation. By following the structure detailed below, the application can handle various user commands to manage a simple to-do list.

The first thing the main function will do is load the existing tasks from the file using load.

Next, it’s going to use the sys.argv module to check to make sure there are enough command-line arguments. If there are not, then it will  call help. sys.argv is a list in Python, provided by the sys module, that contains the command-line arguments passed to a script. The first element, sys.argv[0], is the name of the script. The following elements are the arguments passed to the script.

Next, main is going to assign a value to the command-line arguments. main will use a variable to match the command line input to the corresponding function. For this, we’ll use a large if/ else statement. Let’s build and check for add first. It will look like this:

Next, let’s build one for list.

Now let’s create the ability to trigger the complete function.

Lastly, we’ll remove the task.

If the command is not recognized, an error message will display on the console.

Running the Application

You can run this application using the terminal. The following command will add a task:

python todo_main.py add "Task description"

Once you add your first task, you’ll see a tasks.txt`file pop up in your file tree.

You can see all the files on that list using:

python todo_main.py lis

The list with the task ID will appear. You will need the task_id to perform any of the task-specific operations.

You can mark a task done using this command:

python todo_main.py done TASK_ID

You can find more details about the terminal commands and see how they match up to the functional logic in the main function. The command == section defines the terminal commands. You can use the main function to see more about how to run this code.

The full code file will look like this:

After working on this tutorial, you’ve learned to manipulate files, handle command-line arguments, and structure your code into task and utility functions. This project is a strong example of how to apply basic Python concepts to create a real-world application.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.