Categories
Python Answers

How to convert a unix timestamp string to readable date with Python?

Sometimes, we want to convert a unix timestamp string to readable date with Python.

In this article, we’ll look at how to convert a unix timestamp string to readable date with Python.

How to convert a unix timestamp string to readable date with Python?

To convert a unix timestamp string to readable date with Python, we can use the datetime.utcfromtimestamp method.

For instance, we write

from datetime import datetime
ts = 1284101485


print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))

to call datetime.utcfromtimestamp with the ts timestanmp, whgich is in seconds.

to convert ts to a datetime object.

Conclusion

To convert a unix timestamp string to readable date with Python, we can use the datetime.utcfromtimestamp method.

Categories
Python Answers

How to add a countdown timer in Python Pygame?

Sometimes, we want to add a countdown timer in Python Pygame.

In this article, we’ll look at how to add a countdown timer in Python Pygame.

How to add a countdown timer in Python Pygame?

To add a countdown timer in Python Pygame, we can use the time.get_ticks method.

For instance, we write

start_ticks = pygame.time.get_ticks()
while mainloop:
    seconds = (pygame.time.get_ticks() - start_ticks) / 1000
    if seconds > 10:
        break
    print(seconds)

to call get_ticks to get the timestamp at the current datetime.

Then in our while loop, we get the elapsed time in seconds with

(pygame.time.get_ticks() - start_ticks) / 1000

And if seconds is bigger than 10, we end the loop.

Conclusion

To add a countdown timer in Python Pygame, we can use the time.get_ticks method.

Categories
Python Answers

How to get the value of a variable given its name in a string with Python?

Sometimes, we want to get the value of a variable given its name in a string with Python.

In this article, we’ll look at how to get the value of a variable given its name in a string with Python.

How to get the value of a variable given its name in a string with Python?

To get the value of a variable given its name in a string with Python, we can use the globals or getattr functions.

For instance, we write

a = 5

b = globals()['a']

to call globals to return a dict with the global variable names as keys and their values as values.

Therefore, we can get the value of a with globals()['a'].

To get the value of a global variable in a different module, we use getattr.

For instance, we write

import other

print getattr(other, "a")

to get the value of the a global variable in the other module.

Conclusion

To get the value of a variable given its name in a string with Python, we can use the globals or getattr functions.

Categories
Python Answers

How to create a tuple with only one element with Python?

Sometimes, we want to create a tuple with only one element with Python.

In this article, we’ll look at how to create a tuple with only one element with Python.

How to create a tuple with only one element with Python?

To create a tuple with only one element with Python, we should put a comma after the element.

For instance, we write

a = [('a',), ('b',), ('c', 'd')]

to create list a that has 3 tuples inside.

We add a comma after 'a' and 'b' to make them tuples.

Conclusion

To create a tuple with only one element with Python, we should put a comma after the element.

Categories
Python Answers

How to fix converting a list to a set changes element order with Python?

Sometimes, we want to fix converting a list to a set changes element order with Python.

In this article, we’ll look at how to fix converting a list to a set changes element order with Python.

How to fix converting a list to a set changes element order with Python?

To fix converting a list to a set changes element order with Python,. we can use the dict.fromkeys method to create a dictionary from the keys in a list.

For instance, we write

a = dict.fromkeys([1, 2, 20, 6, 210])

to call dict.fromkeys with a list of numbers that we use as keys in the returned array.

The values are all set to None.

Conclusion

To fix converting a list to a set changes element order with Python,. we can use the dict.fromkeys method to create a dictionary from the keys in a list.