Categories
Python Answers

How to access a function variable outside the function without using “global” with Python?

Sometimes, we want to access a function variable outside the function without using "global" with Python.

In this article, we’ll look at how to access a function variable outside the function without using "global" with Python.

How to access a function variable outside the function without using "global" with Python?

To access a function variable outside the function without using "global" with Python, we can add an attribute to the function.

For instance, we write

def hi():
    # ...
    hi.bye = 100
    sigh = 10

hi()
print(hi.bye)

to add the bye attribute to the hi function.

We can do this since functions are objects in Python.

And then we get the value with hi.bye.

Conclusion

To access a function variable outside the function without using "global" with Python, we can add an attribute to the function.

Categories
Python Answers

How to plot dates on the x-axis with Python’s matplotlib?

Sometimes, we want to plot dates on the x-axis with Python’s matplotlib.

In this article, we’ll look at how to plot dates on the x-axis with Python’s matplotlib.

How to plot dates on the x-axis with Python’s matplotlib?

To plot dates on the x-axis with Python’s matplotlib, we convert the date strings to datetime objects with strptime.

For instance, we write

import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

dates = ['01/02/2022','01/03/2022','01/04/2022']
x = [dt.datetime.strptime(d,'%m/%d/%Y').date() for d in dates]
y = range(len(x))

plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%m/%d/%Y'))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
plt.plot(x,y)
plt.gcf().autofmt_xdate()

to convert the date strings in the dates list to datetime objects with strptime and put them in the x list.

Then we call set_major_formatter to DateFormatter to format the dates in the x-axis.

And we call set_major_locator with DayLocator to put the dates on the x-axis in the right location.

Then we call plot to plot iterables x and y.

And we call autofmt_xdate to format the dates with DateFormatter.

Conclusion

To plot dates on the x-axis with Python’s matplotlib, we convert the date strings to datetime objects with strptime.

Categories
Python Answers

How to fix items in JSON object are out of order using Python json.dumps?

Sometimes, we want to fix items in JSON object are out of order using Python json.dumps.

In this article, we’ll look at how to fix items in JSON object are out of order using Python json.dumps.

How to fix items in JSON object are out of order using Python json.dumps?

To fix items in JSON object are out of order using Python json.dumps, we can call json.dumps with a ordered dict.

For instance, we write

from collections import OrderedDict

s = json.dumps(OrderedDict([("a", 1), ("b", 2)]))

to create an ordered dict with the OrderDict class.

And then we call json.dumps with the ordered dict to create a JSON string that has the same order as the items are listed in the list we created the OrderedDict with.

Conclusion

To fix items in JSON object are out of order using Python json.dumps, we can call json.dumps with a ordered dict.

Categories
Python Answers

How to play a sound with Python?

Sometimes, we want to play a sound with Python.

In this article, we’ll look at how to play a sound with Python.

How to play a sound with Python?

To play a sound with Python, we can use the pyglet module.

To install it, we run

pip install pyglet

Then, we write

import pyglet

music = pyglet.resource.media('music.mp3')
music.play()

pyglet.app.run()

to call pyglet.resource.media to open the music.mp3 file.

Then we call music.play to play the file.

Conclusion

To play a sound with Python, we can use the pyglet module.

Categories
Python Answers

How to convert a string of bytes into an int with Python?

Sometimes, we want to convert a string of bytes into an int with Python.

In this article, we’ll look at how to convert a string of bytes into an int with Python.

How to convert a string of bytes into an int with Python?

To convert a string of bytes into an int with Python, we can use the int.from_bytes method.

For instance, we write

n = int.from_bytes(b'y\xcc\xa6\xbb', byteorder='big')

to call int.from_types with a byte string and the byteorder argument set to the string value for the endianness of the byte string.

We can set byteorder to 'little' if we have a little endian byte string.

Conclusion

To convert a string of bytes into an int with Python, we can use the int.from_bytes method.