π Reminder about Python map()!
map() β a built-in function that applies the specified function to each element of an iterable object (list, tuple, set, etc.).
The picture shows the basic syntax, an example of use with lambda, and a typical case β data transformation without a manual for loop.
Save it to quickly remember the syntax!
ππ»πΊοΈ #Python #Coding #Programming #LearnToCode #DevTips #Tech
map() β a built-in function that applies the specified function to each element of an iterable object (list, tuple, set, etc.).
The picture shows the basic syntax, an example of use with lambda, and a typical case β data transformation without a manual for loop.
Save it to quickly remember the syntax!
ππ»πΊοΈ #Python #Coding #Programming #LearnToCode #DevTips #Tech
β€7π1
Python allows you to create enumerations that are also regular strings!
Previously, when working with APIs, JSON, and configurations, it was often necessary to manually extract the value from an Enum.
For example:
When serializing, you would get an enumeration object:
rather than a regular string:
In Python 3.11,
Now, the value can be used wherever a string is expected:
The result:
At the same time, the advantages of enumerations are preserved:
You cannot accidentally pass an incorrect value:
will result in an error.
π₯
#Python #Coding #StrEnum #DevTips #Programming #Python311
β¨ Join Best TG Channels https://t.me/addlist/0f6vfFbEMdAwODBk
βοΈ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
Previously, when working with APIs, JSON, and configurations, it was often necessary to manually extract the value from an Enum.
For example:
class Status(Enum):
ACTIVE = "active"
When serializing, you would get an enumeration object:
Status.ACTIVErather than a regular string:
"active"In Python 3.11,
StrEnum was introduced to solve this problem.from enum import StrEnum
class Status(StrEnum):
ACTIVE = "active"
BLOCKED = "blocked"
Now, the value can be used wherever a string is expected:
json.dumps({"status": Status.ACTIVE})The result:
{"status": "active"}At the same time, the advantages of enumerations are preserved:
Status.ACTIVEStatus.BLOCKEDYou cannot accidentally pass an incorrect value:
Status("unknown")will result in an error.
π₯
StrEnum allows you to combine the strict typing of enumerations with the convenience of regular strings, without manual conversion when working with APIs, JSON, and configurations.#Python #Coding #StrEnum #DevTips #Programming #Python311
β¨ Join Best TG Channels https://t.me/addlist/0f6vfFbEMdAwODBk
βοΈ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
β€5
The tell() function in Python π
The tell() function returns the current position of the file pointer within the data stream. It is most often used when working with files. π
The function does not accept any arguments and returns an integer β the position in bytes from the beginning of the stream. π’
#Python #Programming #Coding #FileHandling #DevTips #Tech
β¨ Join Best TG Channels https://t.me/addlist/0f6vfFbEMdAwODBk
βοΈ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
The tell() function returns the current position of the file pointer within the data stream. It is most often used when working with files. π
The function does not accept any arguments and returns an integer β the position in bytes from the beginning of the stream. π’
with open("file.txt", "rb") as f:
print(f.tell())#Python #Programming #Coding #FileHandling #DevTips #Tech
β¨ Join Best TG Channels https://t.me/addlist/0f6vfFbEMdAwODBk
βοΈ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
β€3
π asyncio.Queue.shutdown() β shutting down handlers without special markers
Starting with Python 3.13, the asyncio.Queue can be closed using shutdown(), without the need to use None or other special values.
After shutdown(), no new items are added, and existing items are processed. When the queue is empty, get() will raise asyncio.QueueShutDown.
For a forced shutdown, there is shutdown(immediate=True), which clears the queue but may disrupt the normal behavior of join().
#Python #Asyncio #Programming #DevTips #Coding #Tech
β¨ Join Best TG Channels https://t.me/addlist/0f6vfFbEMdAwODBk
βοΈ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
Starting with Python 3.13, the asyncio.Queue can be closed using shutdown(), without the need to use None or other special values.
import asyncio
async def worker(queue):
while True:
try:
task = await queue.get()
except asyncio.QueueShutDown:
break
print(task)
queue.task_done()
async def main():
queue = asyncio.Queue()
task = asyncio.create_task(worker(queue))
await queue.put("task-1")
await queue.put("task-2")
queue.shutdown()
await queue.join()
await task
asyncio.run(main())
After shutdown(), no new items are added, and existing items are processed. When the queue is empty, get() will raise asyncio.QueueShutDown.
For a forced shutdown, there is shutdown(immediate=True), which clears the queue but may disrupt the normal behavior of join().
#Python #Asyncio #Programming #DevTips #Coding #Tech
β¨ Join Best TG Channels https://t.me/addlist/0f6vfFbEMdAwODBk
βοΈ Join Our WhatsApp Channel https://whatsapp.com/channel/0029VaC7Weq29753hpcggW2A
β€1