Categories
Python Answers

How to request UAC elevation from within a Python script?

Sometimes, we want to request UAC elevation from within a Python script.

In this article, we’ll look at how to request UAC elevation from within a Python script.

How to request UAC elevation from within a Python script?

To request UAC elevation from within a Python script, we call ctypes.windll.shell32.ShellExecuteW.

For instance, we write

import ctypes, sys

def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False

if is_admin():
    # ...
else:
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)

to define the is_admin to check if the current user has admin permission with

ctypes.windll.shell32.IsUserAnAdmin()

If is_admin returns False, then we run the script again with

ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)

to run runas with the script path and the command line arguments for the script.

Conclusion

To request UAC elevation from within a Python script, we call ctypes.windll.shell32.ShellExecuteW.

Categories
Python Answers

How to fix UnicodeDecodeError, invalid continuation byte with Python?

Sometimes, we want to fix UnicodeDecodeError, invalid continuation byte with Python.

In this article, we’ll look at how to fix UnicodeDecodeError, invalid continuation byte with Python.

How to fix UnicodeDecodeError, invalid continuation byte with Python?

To fix UnicodeDecodeError, invalid continuation byte with Python, we call decode to decode the byte string with the right encoding.

For instance, we write

s = b'\xe9\x80\x80'.decode('utf-8')

to call decode with 'utf-8' on the byte string to decode it as a Unicode string.

Conclusion

To fix UnicodeDecodeError, invalid continuation byte with Python, we call decode to decode the byte string with the right encoding.

Categories
Python Answers

How to create Python Matplotlib scatter plot with different text at each data point?

Sometimes, we want to create Python Matplotlib scatter plot with different text at each data point

In this article, we’ll look at how to create Python Matplotlib scatter plot with different text at each data point.

How to create Python Matplotlib scatter plot with different text at each data point?

To create Python Matplotlib scatter plot with different text at each data point, we call annotate with the value we want to label the point with.

For instance, we write

import matplotlib.pyplot as plt

y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199]
z = [0.15, 0.3, 0.45, 0.6, 0.75]
n = [58, 651, 393, 203, 123]

fig, ax = plt.subplots()
ax.scatter(z, y)

for i, txt in enumerate(n):
    ax.annotate(txt, (z[i], y[i]))

to call enumerate with n to loop through the points in n with the index i.

Then we call annotate with the txt label, and the point coordinates in a tuple to label each point with txt.

Conclusion

To create Python Matplotlib scatter plot with different text at each data point, we call annotate with the value we want to label the point with.

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 =  re.sub(' +', ' ', 'The     quick brown    fox')

to call re.sub with the ' +' to match more than one space in the string and replace them with one space in 'The quick brown fox'.

Conclusion

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

Categories
Python Answers

How to call a parent class’s method from a child class in Python?

Sometimes, we want to call a parent class’s method from a child class in Python.

In this article, we’ll look at how to call a parent class’s method from a child class in Python.

How to call a parent class’s method from a child class in Python?

To call a parent class’s method from a child class in Python, we can use super to access the parent class from the child class.

For instance, we write

class Foo(Bar):
    def baz(self, **kwargs):
        return super().baz(**kwargs)

to create the Foo class that’s a subclass of the Bar class.

Then we can call the Bar class’ baz method in the Foo class’ baz method with super().baz(**kwargs).

kwargs has all the keyword arguments in a dict.

And we use ** to unpack them as arguments.

Conclusion

To call a parent class’s method from a child class in Python, we can use super to access the parent class from the child class.