Categories
Python Answers

How to check which OS a script is running on with Python?

Sometimes, we want to check which OS a script is running on with Python.

In this article, we’ll look at how to check which OS a script is running on with Python.

How to check which OS a script is running on with Python?

To check which OS a script is running on with Python, we can use the os.name property or platform.system method.

For instance, we write:

import os

print(os.name)

And we see 'posix' printed when we run the script in a Unix like OS.

We can get more specific OS info with platform.system and platform.release.

For instance, we write:

import platform

print(platform.system())
print(platform.release())

We call platform.system to get the name of the OS the script is currently running on.

And we call platform.release to get the release info of the OS the script is currently running on.

Therefore, we get something like:

Linux
5.11.0-1021-gcp

printed.

Conclusion

To check which OS a script is running on with Python, we can use the os.name property or platform.system method.

We call platform.system to get the name of the OS the script is currently running on.

And we call platform.release to get the release info of the OS the script is currently running on.

Categories
Python Answers

How to get the current time in Python?

Sometimes, we want to get the current time in Python.

In this article, we’ll look at how to get the current time in Python.

How to get the current time in Python?

To get the current time in Python, we can use the datetime.datetime.now method.

For instance, we write:

import datetime

t = datetime.datetime.now().time()
print(t)

We call datetime.datetime.now to get the current date and time.

And then we call time to return the current time.

Conclusion

To get the current time in Python, we can use the datetime.datetime.now method.

Categories
Python Answers

How to use Python dict.fromkeys without all values pointing to same list?

Sometimes, we want to use Python dict.fromkeys without all values pointing to same list.

In this article, we’ll look at how to use Python dict.fromkeys without all values pointing to same list.

How to use Python dict.fromkeys without all values pointing to same list?

To use Python dict.fromkeys without all values pointing to same list, we can make a copy of the list with list and use dictionary comprehension.

For instance, we write:

keys = ['a', 'b', 'c']
value = [0, 0]
d = {key: list(value) for key in keys}
print(d)

We call list on value to return a copy of value for each entry.

And we use key in keys as the keys.

Therefore, d is {'a': [0, 0], 'b': [0, 0], 'c': [0, 0]} where [0, 0] in each entry is different.

Conclusion

To use Python dict.fromkeys without all values pointing to same list, we can make a copy of the list with list and use dictionary comprehension.

Categories
Python Answers

How to remove multiple spaces in a string with Python?

Sometimes, we want to remove multiple spaces in a string with Python.

In this article, we’ll look at how to remove multiple spaces in a string with Python.

How to remove multiple spaces in a string with Python?

To remove multiple spaces in a string with Python. we can use the re.sub method.

For instance, we write:

import re

s = "The   fox jumped   over    the log."
new_s = re.sub("\s\s+", " ", s)
print(new_s)

We call re.sub with the "\s\s+" regex string to look for 2 or more spaces.

And we replace them with a single space character.

s is the 3rd argument of sub so we’re doing the replacement on s and returning the new string.

We assign the returned string to new_s.

Therefore, new_s is 'The fox jumped over the log.'.

Conclusion

To remove multiple spaces in a string with Python. we can use the re.sub method.

Categories
Python Answers

How to parallelize a simple Python loop?

Sometimes, we want to parallelize a simple Python loop.

In this article, we’ll look at how to parallelize a simple Python loop.

How to parallelize a simple Python loop?

To parallelize a simple Python loop, we can use the joblib module.

For instance, we write:

from joblib import Parallel, delayed


def process(i):
    return i * i


results = Parallel(n_jobs=2)(delayed(process)(i) for i in range(10))
print(results)

We define our loop header with for i in range(10).

And in the loop body, we run delayed(process)(i).

Finally, we use the Parallel constructor with n_jobs set to 2 to use 2 CPU cores.

And then we assign the return results to results.

Therefore, results is [0, 1, 4, 9, 16, 25, 36, 49, 64, 81].

Conclusion

To parallelize a simple Python loop, we can use the joblib module.