Categories
Python Answers

How to create list of single item repeated N times with Python?

Sometimes, we want to create list of single item repeated N times with Python.

In this article, we’ll look at how to create list of single item repeated N times with Python.

How to create list of single item repeated N times with Python?

To create list of single item repeated N times with Python, we can use the * operator.

For instance, we write

l = [5] * 4

to create the list l with 4 5’s in it.

Conclusion

To create list of single item repeated N times with Python, we can use the * operator.

Categories
Python Answers

How to create a daemon in Python?

Sometimes, we want to create a daemon in Python.

In this article, we’ll look at how to create a daemon in Python.

How to create a daemon in Python?

To create a daemon in Python, we can use the python-daemon library.

To install it, we run

pip install python-daemon

Then we can use it by writing

import time
from daemon import runner

class App():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path =  '/tmp/foo.pid'
        self.pidfile_timeout = 5

    def run(self):
        while True:
            print("hello world")
            time.sleep(10)

app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()

to create a runner.DaemonRunner class with app to start the daemon with app.

And then we call do_axction to call the app‘s run method.

How to create a daemon in Python?

To create a daemon in Python, we can use the python-daemon library.

Categories
Python Answers

How to add a progress bar with Python?

Sometimes, we want to add a progress bar with Python.

In this article, we’ll look at how to add a progress bar with Python.

How to add a progress bar with Python?

To add a progress bar with Python, we can use the tqdm library.

To install it, we run

pip install tqdm

Then we use it by writing

from time import sleep
from tqdm import tqdm

for i in tqdm(range(10)):
    sleep(3)

to call tqdm with range(10) to update the progress bar 10 times.

And we call sleep in the loop to pause for 3 seconds between each update.

Conclusion

To add a progress bar with Python, we can use the tqdm library.

Categories
Python Answers

How to set environment variables in Python?

Sometimes, we want to set environment variables in Python.

In this article, we’ll look at how to set environment variables in Python.

How to set environment variables in Python?

To set environment variables in Python, we can put the variable os.environ dict.

For instance, we write

os.environ["FOO"] = "1"

to set the FOO environment variable to '1‘.

We can get the environment variable’s value with

print(os.environ["DEBUSSY"])

Conclusion

To set environment variables in Python, we can put the variable os.environ dict.

Categories
Python Answers

How to calculate number of days between two given dates with Python?

Sometimes, we want to calculate number of days between two given dates with Python.

In this article, we’ll look at how to calculate number of days between two given dates with Python.

How to calculate number of days between two given dates with Python?

To calculate number of days between two given dates with Python, we can subtract the 2 dates and use the days property from the returned object.

For instance, we write

from datetime import date

d0 = date(2022, 8, 18)
d1 = date(2022, 9, 26)
delta = d1 - d0
print(delta.days)

to subtract the 2 dates d0 and d1.

And then we get the days difference from delta.days.

Conclusion

To calculate number of days between two given dates with Python, we can subtract the 2 dates and use the days property from the returned object.