Categories
Python Answers

How to capture SIGINT in Python?

Sometimes, we want to capture SIGINT in Python.

In this article, we’ll look at how to capture SIGINT in Python.

How to capture SIGINT in Python?

To capture SIGINT in Python, we can call the signal.signal method.

For instance, we write:

import signal
import sys

def signal_handler(sig, frame):
    print('You pressed Ctrl+C!')
    sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)
print('Press Ctrl+C')
signal.pause()

We have the signal_handler function that runs when the SIGINT signal is emitted.

We call signal.signal with signal.SIGINT to listen for the SIGINT signal and run signal_handler when it’s emitted.

Then we call signal.pause to pause the script and let us watch for the signal.

Now when we press Ctrl+C, we should see 'You pressed Ctrl+C!' printed.

Conclusion

To capture SIGINT in Python, we can call the signal.signal method.

Categories
Python Answers

How to call a function within class with Python?

Sometimes, we want to call a function within class with Python.

In this article, we’ll look at how to call a function within class with Python.

How to call a function within class with Python?

To call a function within class with Python, we call the function with self before it.

For instance, we write:

class Coordinates:
    def distToPoint(self, p):
        """
        Use pythagoras to find distance
        (a^2 = b^2 + c^2)
        """

    def isNear(self, p):
        self.distToPoint(self, p)

We call the distToPoint instance method within the Coordinates class by calling self.distToPoint.

self is variable storing the current Coordinates class instance.

Conclusion

To call a function within class with Python, we call the function with self before it.

Categories
Python Answers

How to print everything in one line dynamically with Python?

Sometimes, we want to print everything in one line dynamically with Python.

In this article, we’ll look at how to print everything in one line dynamically with Python.

How to print everything in one line dynamically with Python?

To print everything in one line dynamically with Python, we can set the end parameter to an empty string and the sep parameter to a string with one space.

And we set flush to True.

For instance, we write:

for item in range(1, 10):
    print(item, sep=' ', end='', flush=True)

We loop through the iterator that returns 1 to 9.

Then we call print in the loop body with the options set.

We replaced new line with an empty string for the end character for each print call.

Now we should see:

123456789

printed on the screen.

Conclusion

To print everything in one line dynamically with Python, we can set the end parameter to an empty string and the sep parameter to a string with one space.

And we set flush to True.

Categories
Python Answers

How to print number with commas as thousands separators with Python?

Sometimes, we want to print number with commas as thousands separators with Python.

In this article, we’ll look at how to print number with commas as thousands separators with Python.

How to print number with commas as thousands separators with Python?

To print number with commas as thousands separators with Python, we can use the {:n} format code.

For instance, we write:

import locale

locale.setlocale(locale.LC_ALL, '')

value = 10000000

curr_1 = '{:n}'.format(value)
curr_2 = f'{value:n}'
print(curr_1)
print(curr_2)

to call locale.setlocale to set the locale.

Then we have the value that we want to format into a comma separated number.

Next, we call format with value to format the number into a comma separated number by using '{:n}' as the placeholder.

And we do the same with the f-string by passing in value before the colon.

Therefore, curr_1 and curr_2 are both 10,000,000.

Conclusion

To print number with commas as thousands separators with Python, we can use the {:n} format code.

Categories
Python Answers

How to clear the interpreter console with Python?

Sometimes, we want to clear the interpreter console with Python.

In this article, we’ll look at how to clear the interpreter console with Python.

How to clear the interpreter console with Python?

To clear the interpreter console with Python, we can run the cls command if the script is run on Windows and clear otherwise.

We can do this with the os.system method.

For instance, we write:

import os

def cls():
    os.system('cls' if os.name=='nt' else 'clear')

cls()

We have the cls function that uses os.name to check for the platform that it’s running.

If os.name returns 'nt' then it’s running on Windows.

We use os.system to run cls or clear depending on the platform.

Conclusion

To clear the interpreter console with Python, we can run the cls command if the script is run on Windows and clear otherwise.

We can do this with the os.system method.