Categories
Python Answers

How to fix Python throwing AttributeError: module ‘enum’ has no attribute ‘IntFlag’?

Sometimes, we want to fix Python throwing AttributeError: module ‘enum’ has no attribute ‘IntFlag’.

In this article, we’ll look at how to fix Python throwing AttributeError: module ‘enum’ has no attribute ‘IntFlag’.

How to fix Python throwing AttributeError: module ‘enum’ has no attribute ‘IntFlag’?

To fix Python throwing AttributeError: module ‘enum’ has no attribute ‘IntFlag’, we uninstall the enum34 package.

To uninstall it, we run

pip uninstall -y enum34

Then we can use the enum module that comes with the standard library of Python 3.6 or later.

Conclusion

To fix Python throwing AttributeError: module ‘enum’ has no attribute ‘IntFlag’, we uninstall the enum34 package.

Categories
Python Answers

How to un-escape a backslash-escaped string with Python?

Sometimes, we want to un-escape a backslash-escaped string with Python.

In this article, we’ll look at how to un-escape a backslash-escaped string with Python.

How to un-escape a backslash-escaped string with Python?

To un-escape a backslash-escaped string with Python, we can use the string encode and decode method.

For instance, we write

s = my_string.encode('raw_unicode_escape').decode('unicode_escape')

to call encode with 'raw_unicode_escape' to encode the string as an escaped string.

Then we call decode with 'unicode_escape' to unescape the string.

Conclusion

To un-escape a backslash-escaped string with Python, we can use the string encode and decode method.

Categories
Python Answers

How to get date from week number with Python?

Sometimes, we want to get date from week number with Python.

In this article, we’ll look at how to get date from week number with Python.

How to get date from week number with Python?

To get date from week number with Python, we can use the datetime strptime method.

For instance, we write

import datetime

d = "2022-W26"
r = datetime.datetime.strptime(d + '-1', "%Y-W%W-%w")
print(r)

to call strptime with the d string with '-1' appended to it.

Then we parse the date string as a string with format "%Y-W%W-%w".

%Y is the 4 digit year.

W%W is the week number. %w is the date of the week as a number.

1 is Monday and strptime use that as the first day of the week.

Conclusion

To get date from week number with Python, we can use the datetime strptime method.

Categories
Python Answers

How to get rolling window for 1D arrays in Python Numpy?

Sometimes, we want to get rolling window for 1D arrays in Python Numpy.

In this article, we’ll look at how to get rolling window for 1D arrays in Python Numpy.

How to get rolling window for 1D arrays in Python Numpy?

To get rolling window for 1D arrays in Python Numpy, we can use the sliding_window_view function.

For instance, we write

from numpy.lib.stride_tricks import sliding_window_view

rolling_window = sliding_window_view(np.array([1, 2, 3, 4, 5, 6]), window_shape = 3)

to calli sliding_window_view on the NumPy array that we get by calling np.array on a list.

We set the window_shape argument to 3 to return a rolling window list with 3 numbers in each nested list.

Conclusion

To get rolling window for 1D arrays in Python Numpy, we can use the sliding_window_view function.

Categories
Python Answers

How to set sys.stdout encoding in Python 3?

Sometimes, we want to set sys.stdout encoding in Python.

In this article, we’ll look at how to set sys.stdout encoding in Python.

How to set sys.stdout encoding in Python 3?

To set sys.stdout encoding in Python, we can call the sys.stdout.reconfigure method.

For instance, we write

sys.stdout.reconfigure(encoding='utf-8')

to call sys.stdout.reconfigure with the encoding argument set to 'utf-8'.

This will change the stdoud encoding to utf-8.

Conclusion

To set sys.stdout encoding in Python, we can call the sys.stdout.reconfigure method.