Python

6413 readers
4 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
251
252
108
Python 3.12.0 (www.python.org)
submitted 1 year ago by [email protected] to c/python
253
254
 
 

A blog post on choosing more specific types rather than general ones like list and dict.

255
 
 

I know what I am asking is rather niche, but it has been bugging me for quite a while. Suppose I have the following function:

def foo(return_more: bool):
   ....
    if return_more:
        return data, more_data
   return data

You can imagine it is a function that may return more data if given a flag.

How should I typehint this function? When I use the function in both ways

data = foo(False)

data, more_data = foo(True)

either the first or the 2nd statement would say that the function cannot be assigned due to wrong size of return tuple.

Is having variable signature an anti-pattern? Is Python's typehinting mechanism not powerful enough and thus I am forced to ignore this error?

Edit: Thanks for all the suggestions. I was enlightened by this suggestion about the existence of overload and this solution fit my requirements perfectly

from typing import overload, Literal

@overload
def foo(return_more: Literal[False]) -> Data: ...

@overload
def foo(return_more: Literal[True]) -> tuple[Data, OtherData]: ...

def foo(return_more: bool) -> Data | tuple[Data, OtherData]:
   ....
    if return_more:
        return data, more_data
   return data

a = foo(False)
a,b = foo(True)
a,b = foo(False) # correctly identified as illegal
256
 
 

Constant modules are modules that often contains variables that you want to use in many places without relying on hard coded values. Sometime you store paths (often relatives).

Is it a good thing to use pathlib.Path() object is modules dedicated to constant?

Or is there anything that you should know before choosing to do so?

I would say the same question appears for using re.compile() in constants.

Any advise?

257
258
259
35
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/python
 
 

This is like Interface in Go (or Java, i don't speak Java but the article say so).

260
9
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/python
 
 

I am assisting in making a wiki for an old game, and we ripped the avatar GIFs from the games shop and want to have a catalog of them. What I need to do is to crop all the borders which are identical from 3,170 GIFs and maybe make background transparent.

I haven't used python in years, but I managed to cobble up something that almost works as a single image test, only issue is that it crops and outputs only the first frame:

from PIL import Image

if __name__ == "__main__":
    input_loc = "AvatarShopImages/80001.gif"
    output_loc = "Output/80001.gif"
    im = Image.open(input_loc)
    im = im.crop((4, 4, 94, 94))
    im.save(output_loc)

If it looks weird, it is because I copy/pasted some code and edited a lot out of it.

261
10
Jupyter notebook (self.python)
submitted 1 year ago by bc001 to c/python
 
 

Hi,

Is it ok to use Jupyter notebook exclusively for creating notes. I do not need to run any codes in cells, just taking notes.

Currently, I run notebook using 'jupyter-notebook'. Then, use Kernel > Shutdown option. And, create notes.

I think there will be commandline option, but cannot find any. Any advise on this?

Thanks,

262
 
 

I have seen some people prefer to create a list of strings by using thing = list[str]() instead of thing: list[str] = []. I think it looks kinda weird, but maybe that's just because I have never seen that syntax before. Does that have any downsides?

It is also possible to use this for dicts: thing = dict[str, SomeClass](). Looks equally weird to me. Is that widely used? Would you use it? Would you point it out in a code review?

263
264
 
 

From the docs:


svcs (pronounced services) is a dependency container for Python. It gives you a central place to register factories for types/interfaces and then imperatively acquire instances of those types with automatic cleanup and health checks.

It’s suitable for implementing Inversion of Control using either dependency injection or service location while not requiring global state, decorators, or mangling of function signatures.


Personally I don't know if I will ever need such a tool; I don't really do web framework-y work right now. But I have a ton of respect for Hynek and enjoy his other projects and his blog posts, so if you need something like this I can recommend it on that basis.

265
 
 

With the textual-web command you can publish any Textual app on the web, making it available to anyone you send the URL to. This works without creating a socket server on your machine, so you won't have to configure firewalls and ports to share your applications.

266
267
 
 

I'm working on a tool that aims to do two things:

  • bootstrap Lemmy communities with content from their "equivalent" subreddit

  • help people migrate away from Reddit, by setting up a bot account on Lemmy that can be later taken over by their legitimate reddit owner. The idea is that the bot account would follow the equivalent lemmy communities and "registration" could be as easy as having the reddit user sending a DM to a bot to authenticate themselves.

I'm wondering how the people here would feel about me trying out this tool by mapping /r/python to [email protected] ? My plan would be to set up a Lemmy instance that could exclusively be the home for the bot accounts, and then I would handpick a few posts every day to get them mirrored here, comments included. I also have in the roadmap to have responses to let users on Reddit to be notified of the conversations/replies received on the Lemmy post.

My view of pros/cons:

Pros:

  • Those who are already on Lemmy but stay on Reddit because of specific, niche communities will be able to ditch Reddit entirely.
  • More content in the instance, which would help mitigate the common "I want to move to Lemmy, but the content is not there" complaints.
  • A clearer path to migration and less time discussing "where to go if we are leaving reddit?"
  • Admins who object to this can simply deferate from the mirror instance(s).

Cons:

  • If abused, Lemmy communities might start looking like they are filled with bots only. Not really my intention, this is why I am not planning to fully automate this, but also not a big issue given that admins can easily protect themselves for instances that spam too much.
  • It's a legal grey area (though there are so many repost bots out there and I don't see how anyone would try to enforce copyright claims) whose support is mostly on the hands of reddit users.
  • If people look at it as a tool to help them migrate, we can win them over. If this feels too forced, they will more likely side with Reddit and refuse to migrate.

Anyway, please let me know your thoughts.

(Also, the code is Python/Django so if anyone is interested in contributing just let me know!)

268
11
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/python
 
 

im creating an application using curses and im using the getch() function to get keyboard input, the problem is, that it is painfully slow, it takes several seconds to detect the pressed key and normally i have to press the key several times so the program actually detects it, there is a way to make getch() respond instantly (or at least at a usable speed)?

269
 
 

Use Python and Bash to automatically regenerate your guest Wi-Fi password and display the QR Code for joining the said network on a Raspberry Pi Pico display.

270
271
 
 

Came across this framework from this week's Pycoder weekly newsletter.

272
273
27
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/python
 
 

What's the best way to teach my kid programming. I don't know how. He's interested. He's done well in school with Scratch. He's expressed interest in Python, owns a Thumby, but never gets too far on his own. Instead of a Winter sport, we're leaning towards a Python class, however there's none in person. How can i help my kid embrace his passion and learn this skill which will help him his whole life.

He's got Mu installed and has perused youtube tutorials, but they dont hold his interest. Any help is appreciated. Edit: He's 10.

274
 
 

In this video, we adapt a clumsy, non-Pythonic API into an easy to use, easy to understand Pythonic one. We use magic methods such as getitem_, len, enter, and _exit to make our objects a context manager and support the len() function and square bracket indexing. And in the end, we turn what once was ugly, difficult to maintain code into something that other developers would actually want to use.

275
34
Litestar 2.0 Released (blog.litestar.dev)
submitted 1 year ago* (last edited 1 year ago) by Andy to c/python
 
 

Litestar is a powerful, flexible yet opinionated ASGI framework, focused on building APIs, and offers high-performance data validation and parsing, dependency injection, first-class ORM integration, authorization primitives, and much more that's needed to get applications up and running.

https://github.com/litestar-org/litestar/


I am not personally involved in the project, I just like following its development.


I know Medium is annoying, sorry. The content is also copied on Reddit... where you can enjoy such comments as

Why would they waste their time trying to promote to the few people on Lemmy?

😢

view more: ‹ prev next ›