Categories
Python Answers

How to get the week number in Python?

Sometimes, we want to get the week number in Python.

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

How to get the week number in Python?

To get the week number in Python, we can use the datetime’s isocalendar method.

For instance, we write

import datetime

w = datetime.date(2021, 6, 16).isocalendar().week

to create a date with datetime.date.

Then we call isocalendar to return a named tuple with the week property.

And we use that to get the week number.

Conclusion

To get the week number in Python, we can use the datetime’s isocalendar method.

Categories
Python Answers

How to plot logarithmic axes with matplotlib in Python?

Sometimes, we want to plot logarithmic axes with matplotlib in Python.

In this article, we’ll look at how to plot logarithmic axes with matplotlib in Python.

How to plot logarithmic axes with matplotlib in Python?

To plot logarithmic axes with matplotlib in Python, we can use the set_yscale method.

For instance, we write

import pylab
import matplotlib.pyplot as plt
a = [pow(10, i) for i in range(10)]
fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)

line, = ax.plot(a, color='blue', lw=2)

ax.set_yscale('log')

pylab.show()

to call set_yscale with 'log' to make the y-axis a logarithmic axis.

We lot the values in list a with with the lot.

Conclusion

To plot logarithmic axes with matplotlib in Python, we can use the set_yscale method.

Categories
Python Answers

How to detach matplotlib plots so that the computation can continue with Python?

Sometimes, we want to detach matplotlib plots so that the computation can continue with Python.

In this article, we’ll look at how to detach matplotlib plots so that the computation can continue with Python.

How to detach matplotlib plots so that the computation can continue with Python?

To detach matplotlib plots so that the computation can continue with Python, we can call draw.

For instance, we write

from matplotlib.pyplot import plot, draw, show

plot([1,2,3])
draw()

# ...

show()

to call plot to plot a graph.

Then we call draw to draw it on screen and be allowed to continue running the rest of the script.

Finally, we call show at the end to show the final plot.

Conclusion

To detach matplotlib plots so that the computation can continue with Python, we can call draw.

Categories
Python Answers

How to fix max retries exceeded with URL in Python requests?

Sometimes, we want to fix max retries exceeded with URL in Python requests.

In this article, we’ll look at how to fix max retries exceeded with URL in Python requests.

How to fix max retries exceeded with URL in Python requests?

To fix max retries exceeded with URL in Python requests, we can set the retries when making a request with requests.

For instance, we write

import requests
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry


session = requests.Session()
retry = Retry(connect=3, backoff_factor=0.5)
adapter = HTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)

session.get(url)

to create a HTTPAdapter with the max_retries set to the Retry object.

We set the Retry to max 3 retries and backoff_factor is the delay between retries in seconds.

Then we call session.mount with adapter to use the retry settings.

And then we call session.get with url to make the GET request.

Conclusion

To fix max retries exceeded with URL in Python requests, we can set the retries when making a request with requests.

Categories
Python Answers

How to read and write to Unicode (UTF-8) files in Python?

Sometimes, we want to read and write to Unicode (UTF-8) files in Python.

In this article, we’ll look at how to read and write to Unicode (UTF-8) files in Python.

How to read and write to Unicode (UTF-8) files in Python?

To read and write to Unicode (UTF-8) files in Python, we can use the io.open function.

For instance, we write

import io

f = io.open("test", mode="r", encoding="utf-8")

to call io.open with the path of the file to open, the permission mode and the encoding of the file.

We set the encoding to 'utf-8' to read the file as a Unicode file.

Conclusion

To read and write to Unicode (UTF-8) files in Python, we can use the io.open function.