Categories
Python Answers

How to create threads in Python?

Sometimes, we want to create threads in Python.

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

How to create threads in Python?

To create threads in Python, we can use the Thread class.

For instance, we write

from threading import Thread
from time import sleep

def threaded_function(arg):
    for i in range(arg):
        print("running")
        sleep(1)


if __name__ == "__main__":
    thread = Thread(target = threaded_function, args = (10, ))
    thread.start()
    thread.join()

to create a Thread object that runs the threaded_function function with 10 as its argument.

Then we call start to start the thread.

And we call join to block the calling thread until the thread that called join is terminated.

Conclusion

To create threads in Python, we can use the Thread class.

Categories
Python Answers

How to strip non printable characters from a string in Python?

Sometimes, we want to strip non printable characters from a string in Python.

In this article, we’ll look at how to strip non printable characters from a string in Python.

How to strip non printable characters from a string in Python?

To strip non printable characters from a string in Python, we can call the isprintable method on each character and use list comprehension.

For instance, we write

s = ''.join(c for c in my_string if c.isprintable())

to check if each character in my_string is printable with isprintable.

And we return an iterator with all the printable characters with

c for c in my_string if c.isprintable()

Then we call ''.join with the iterator to join the printable characters in my_string back to a string.

Conclusion

To strip non printable characters from a string in Python, we can call the isprintable method on each character and use list comprehension.

Categories
Python Answers

How to do interprocess communication in Python?

Sometimes, we want to do interprocess communication in Python.

In this article, we’ll look at how to do interprocess communication in Python.

How to do interprocess communication in Python?

To do interprocess communication in Python, we can use the multiprocessing library to send and receive commands.

For instance, in the server, we write

from multiprocessing.connection import Listener

address = ('localhost', 6000)
listener = Listener(address, authkey=b'secret password')
conn = listener.accept()
print('connection accepted from', listener.last_accepted)
while True:
    msg = conn.recv()
    if msg == 'close':
        conn.close()
        break
listener.close()

to call listener.accept to listen for for commands.

We then add an infinite while loop that ends when msg is 'close'.

We get msg from conn.recv which gets the received message.

In the client, we write

from multiprocessing.connection import Client

address = ('localhost', 6000)
conn = Client(address, authkey=b'secret password')
conn.send('close')
conn.close()

to create a Client object and call send to send a message to the server at the given address.

Conclusion

To do interprocess communication in Python, we can use the multiprocessing library to send and receive commands.

Categories
Python Answers

How to convert JSON string to a dictionary not list with Python?

Sometimes, we want to convert JSON string to a dictionary not list with Python.

In this article, we’ll look at how to convert JSON string to a dictionary not list with Python.

How to convert JSON string to a dictionary not list with Python?

To convert JSON string to a dictionary not list with Python, we call json.loads with the JSON string string and get the value by the index of the JSON string is an array string.

For instance, we write

json1_data = json.loads(json1_str)[0]

to call json.loads with the json1_str JSON array string and get the first item in the returned list with [0].

Conclusion

To convert JSON string to a dictionary not list with Python, we call json.loads with the JSON string string and get the value by the index of the JSON string is an array string.

Categories
Python Answers

How to print a string at a fixed width with Python?

Sometimes, we want to print a string at a fixed width with Python.

In this article, we’ll look at how to print a string at a fixed width with Python.

How to print a string at a fixed width with Python?

To print a string at a fixed width with Python, we can use the string format method.

For instance, we write

'{0: <5}'.format('s')

to call ‘{0: <5}’.formatwith‘s’` to print ‘s’ in a space 5 characters long and left aligned.

We use < to left align the string and we specify the length after that.

Conclusion

To print a string at a fixed width with Python, we can use the string format method.