Feedback
Got a suggestion for improvement? Anything goes.
Found a bug? Let us know. For other inquries feel free to contact us about anything at all.
Call to action
Depvana's reach is 100% powered by word of mouth. If you want to help: tell a friend! Let your group chats know, let people know of Depvana.
Depvana •
about •
legal •
contact •
Depvana is independent. Help keep it that way.
© 2024 Depvana aps.

python essentials

Public room public room
No. 262
124
4
0
More
Copy link
Report
Moderators  • sys

Essential python knowledge sharing.

Login to add a hashtag.
Hashtags
Bookmark
Rss
Subtopics
Visible to the public Public post
Attachments • images • video webm/mp4 • max size 4096KiB.
Attachments • images • video • max 4MB.
Filter  •  Newest
Newest
Sort posts in decending order by date
Oldest
Sort posts in ascending order by date
Compact View Mode
No.4770 • 
anon@277 
More
Options
Copy link
Report
Objects: bytes vs BytesIO

Feature:             Type
`bytes` Object:      Immutable sequence of bytes
`BytesIO` Object:    In-memory file-like object for binary data

Feature:             Mutability
`bytes` Object:      Immutable (cannot be modified)
`BytesIO` Object:    Mutable (you can write and modify data)

Feature:             Use Case
`bytes` Object:      Fixed, unchangeable binary data
`BytesIO` Object:    Temporary, in-memory file-like operations

Feature:             Memory Storage
`bytes` Object:      Stores data as a sequence of bytes
`BytesIO` Object:    Stores data in memory, can read/write like a file

Feature:             Methods
`bytes` Object:      Supports indexing, slicing, and iteration
`BytesIO` Object:    Supports `.read()`, `.write()`, `.seek()` file-like methods

Feature:             Size
`bytes` Object:      Fixed size once created
`BytesIO` Object:    Size can change as you write/read from it
No.4769 • 
anon@277 
More
Options
Copy link
Report
 This post has been removed by moderators 
No.4720 • 
anon@277 
More
Options
Copy link
Report
Note: recv() and Thread Behavior in Python

recv() is a low-level, protocol-independent socket function called by C extensions (e.g., psycopg2) to receive data from a TCP socket, like a DB response. 

Python thread runs a DB call -> C extension releases GIL -> calls recv(), which blocks and thread stays blocked in the OS wait queue during recv().

Network data arrives -> NIC interrupts (per packet or batch) -> OS network stack processes TCP headers, moves payload to socket buffer, updates socket state based on TCP state for instance FIN/RST. OS wakes threads that are waiting on that socket, moving them to the run queue. 

When thread run again -> C extension reacquires GIL -> recv() checks buffer and state, deciding to return or wait again.

recv() Wake and Decision:
-> Checks socket buffer. If data’s there, returns it (e.g., recv(1024) returns up to 1024, but x if that's what' there).
-> If buffer’s empty, checks socket state:
    	-> Open (no FIN): Blocks again if data’s partial and more’s expected, or waits if nothing’s there. Never returns 0—reserved for closure.
    	-> Closed (FIN, buffer empty): Returns 0, “end of stream.”
    	-> Errored (RST): Returns -1 with errno (e.g., ECONNRESET).
	-> Non-blocking mode returns -1/EAGAIN if no data; timeouts error if set.

DB libs loop recv() for protocol completeness.
Key Point: OS wakes thread on data or state change; recv() checks buffer then state, returning data/0/error or waiting based on need.
No.3074 • 
sys@335 
More
Options
Copy link
Report
Existence of variables!

To check the existence of a local variable:

if 'myVar' in locals():
  # myVar exists.

To check the existence of a global variable:

if 'myVar' in globals():
  # myVar exists.

To check if an object has an attribute:

if hasattr(obj, 'attr_name'):
  # obj.attr_name exists.