Creating a progress bar in Python using carriage returns

My name is Saito and I am an infrastructure engineer

Today I want to familiarize you with carriage returns (CR) and write some scripts to show you
how to use carriage returns in Python to create a progress bar.

What is a carriage return (CR)?

A carriage return (CR) is a special symbol represented by r. Many people may be familiar with the line feed character n.

What is a carriage return?

Originally, when using a typewriter, the action of manually returning the part of the carriage that prints characters was called a carriage return (CR)

The action of moving to a new line is called a line feed (LF)

All operating systems in use today have a history of being compatible with electronic typewriters, and this 'r' appears in some form

For example, in Windows OS, a line break is rn, which can be interpreted as "go back to the original position (CR) and then move to a new line (LF)."

Unix, Mac, Linux, BSD, etc. use r and n separately based on the POSIX standard

When you use the newline character n, the cursor is set to the beginning of the new line

In other words, n acts as both a CR and a LF, while r acts as a CR that returns the cursor to its original position

Try using r

I have given examples for different operating systems, but even in programming languages, r and n are often used separately

Let's actually try using R in Python

$ python -c "import sys; sys.stdout.write('beyondrBn')" Beyond

- "-c" is an option for writing one-liners in Python.
- Standard output is performed using sys.stdout.write, and the string is written to a buffer (cache).

Well, in 'beyondrBn', how is the display result determined?

1. First, the string "beyond" is displayed. At this time, the cursor is after "d".
2. Because there is an "r", the cursor moves to the beginning of "b".
3. This time, B is printed over "b".
4. Because there is an "n", a line break is started.

In this way, r can write characters on the same line multiple times

Create a progress bar

Using the above characteristics, you can create a progress bar that runs on the command line. Below is a progress bar written in Python

import sys, time def prog_bar(length=100): for i in range(length): sys.stdout.write('#'*i + 'r') sys.stdout.flush() time.sleep(0.01) for i in range(4): prog_bar()

sys.stdout.write caches the data before displaying it on the terminal.
(In fact, print() uses sys.stdout internally.)
Using sys.stdout.flush deletes the cached information.

Trying to write an animation

Let's also write a fun animation where the bar rotates. (The following has been confirmed to work with Python 3.)

import time space = '.' bar = '-|/' length = 100 printset = (('{0}{1}r'.format (space*(i-length//2) if i > length//2 else '', bar[i%4]), time.sleep(0.05)) for i in range(length)) for i in printset: print(i[0], end='') print(' '*length, end='r')

It can be used for a variety of other purposes.
I've also added slot-like animations to a password generation script.
Please try expanding it and playing around with it.

If you want to consult a development professional

At Beyond, we combine our rich track record, technology and know-how in system development that we have cultivated to date with OSS technology and cloud technologies such as AWS to create contract development of web systems with reliable quality and excellent cost performance.

We also work on server-side/backend development and linkage development of our own APIs, using the technology and know-how of the construction and operation of web system/app infrastructure for large-scale, highly loaded games, apps, and digital content.

If you are having trouble with development projects, please visit the website below.

● Web system development
● Server-side development (API/DB)

If you found this article helpful , please give it a like!
0
Loading...
0 votes, average: 0.00 / 10
5,761
X facebook Hatena Bookmark pocket

The person who wrote this article

About the author