Python

6287 readers
39 users here now

Welcome to the Python community on the programming.dev Lemmy instance!

📅 Events

PastNovember 2023

October 2023

July 2023

August 2023

September 2023

🐍 Python project:
💓 Python Community:
✨ Python Ecosystem:
🌌 Fediverse
Communities
Projects
Feeds

founded 1 year ago
MODERATORS
1
29
Python 3.14.0 alpha 1 (discuss.python.org)
submitted 2 days ago by norambna to c/python
2
17
submitted 2 days ago* (last edited 2 days ago) by jnovinger to c/python
3
102
submitted 1 week ago by norambna to c/python
4
20
submitted 2 weeks ago by norambna to c/python
5
 
 

I am working on a rudimentary Breakout clone, and I was doing the wall collision. I have a function that I initially treated as a Boolean, but I changed it to return a different value depending on which wall the ball hit. I used the walrus operator to capture this value while still treating the function like a bool. I probably could have just defined a variable as the function's return value, then used it in an if statement. But it felt good to use this new thing I'd only heard about, and didn't really understand what it did. I was honestly kind of surprised when it actually worked like I was expecting it to! Pretty cool.

6
7
 
 

(For context, I'm basically referring to Python 3.12 "multiprocessing.Pool Vs. concurrent.futures.ThreadPoolExecutor"...)

Today I read that multiple cores (parallelism) help in CPU bound operations. Meanwhile, multiple threads (concurrency) is due when the tasks are I/O bound.

Is this correct? Anyone cares to elaborate for me?

At least from a theorethical standpoint. Of course, many real work has a mix of both, and I'd better start with profiling where the bottlenecks really are.

If serves of anything having a concrete "algorithm". Let's say, I have a function that applies a map-reduce strategy reading data chunks from a file on disk, and I'm computing some averages from these data, and saving to a new file.

8
8
submitted 2 days ago by jnovinger to c/python
9
42
I know Python basics, what next? (learnbyexample.github.io)
submitted 3 days ago by learnbyexample to c/python
10
11
11
submitted 1 week ago* (last edited 1 week ago) by Rick_C137 to c/python
 
 

Hi,

I'm already using

from smtplib import SMTP_SSL
from email.message import EmailMessage

To send emails.

Now I would like to be able to encrypt them with the public key of the recipient. ( PublicKey.asc )

an A.I provide me this

import smtplib
from email.message import EmailMessage
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.ciphers.aead import AESGCM

# Load the ECC public key from the .asc file
with open('recipient_public_key.asc', 'rb') as key_file:
    public_key_bytes = key_file.read()
public_key = ec.EllipticCurvePublicKey.from_public_bytes(
    ec.SECP384R1(),
    public_key_bytes
)

# Create the email message
msg = EmailMessage()
msg.set_content('This is the encrypted email.')
msg['Subject'] = 'Encrypted Email'
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'

# Encrypt the email message using the ECC public key
nonce = bytes.fromhex('000102030405060708090a0b0c0d0e0f')
cipher = AESGCM(public_key.public_key().secret_key_bytes)
ciphertext = cipher.encrypt(nonce, msg.as_bytes(), None)

# Send the encrypted email
server = smtplib.SMTP('smtp.example.com')
server.send_message(msg, from_addr='[email protected]', to_addr='[email protected]')
server.quit()

# Save the encrypted email to a file
with open('encrypted_email.bin', 'wb') as f:
    f.write(ciphertext)

I like the approach, only one "low level" import cryptography

but the code seem wrong. if the body has been encrypted as ciphertext I don't see this one included while sending the email.

How are you doing it ? or do you have good tutorial, documentations ? because I found nothing "pure and simple" meaning not with of unnecessary stuff.

Thanks.

12
35
submitted 1 week ago by norambna to c/python
13
7
submitted 1 week ago* (last edited 1 week ago) by gukkey to c/python
 
 

I am trying to follow this tutorial (Announcing py2wasm: A Python to Wasm compiler · Blog · Wasmer) and run py2wasm but I am getting this weird problem.

First is that I believe py2wasm might be just an executable like other pip packages I install, or a bat file. (I am fairly new to python and I just want to convert a python code to wasm). But when I head over to C:\Users\USER\AppData\Local\Programs\Python\Python312\Scripts where the pip packages are located, I can't seem to find any file related to py2wasm.

Running dir C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\site-packages\py2wasm* to check any related files about the py2wasm folder only leads to this

Directory: C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\site-packages

Mode LastWriteTime Length Name

***

d----- 04-10-2024 19:54 py2wasm-2.6.2.dist-info

Also, before you could ask yeah I could run other pip packages such as yt-dlp.

14
15
19
Every dunder method in Python (www.pythonmorsels.com)
submitted 2 weeks ago by norambna to c/python
16
17
62
Developing with Docker (danielquinn.org)
submitted 3 weeks ago* (last edited 2 weeks ago) by [email protected] to c/python
 
 

I've been writing code professionally for 24 years, 15 of which has been Python and 9 years of that with Docker. I got tired of running into the same complications every time I started a new job, so I wrote this. Maybe you'll find it useful, or it could even start a conversation, but this post has been a long time coming.

Update: I had a few requests for a demo repo as a companion to this post, so I wrote one today. It includes a very small Django demo user Docker, Compose, and GitLab CI.

18
19
 
 

A library for creating fully typed and declarative API clients, quickly and easily.

What would an API client with this library look like?

For a single API endpoint over HTTP GET, it could look something like this:

from dataclasses import dataclass
import quickapi


# An example type that will be part of the API response
@dataclass
class Fact:
    fact: str
    length: int


# What the API response should look like
@dataclass
class ResponseBody:
    current_page: int
    data: list[Fact]


# Now we can define our API
class MyApi(quickapi.BaseApi[ResponseBody]):
    url = "https://catfact.ninja/facts"
    response_body = ResponseBody

And you would use it like this:

response = MyApi().execute()

# That's it! Now `response` is fully typed (including IDE support) and conforms to our `ResponseBody` definition
assert isinstance(response.body, ResponseBody)
assert isinstance(response.body.data[0], Fact)

It also supports attrs or pydantic (or dataclasses as above) for your model/type definitions, including validation and types/data conversion.

I have a lot more examples (e.g. POST requests, query string params, authentication, error handling, model validation and conversion, multiple API endpoints) on the repo's README.

I've shared this one here before but it's been a while and I've added a lot of features since.

Github repo: https://github.com/martinn/quickapiclient

20
21
45
submitted 1 month ago by norambna to c/python
22
42
Python in Excel – Available Now (techcommunity.microsoft.com)
submitted 1 month ago by [email protected] to c/python
23
24
35
submitted 1 month ago by norambna to c/python
25
0
submitted 1 month ago* (last edited 1 month ago) by [email protected] to c/python
view more: next ›