Python

7643 readers
32 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 2 years ago
MODERATORS
326
327
 
 

Did you know it takes about 17,000 CPU instructions to print("Hello") in Python? And that it takes ~2 billion of them to import a module?

328
28
submitted 2 years ago* (last edited 2 years ago) by souperk@reddthat.com to c/python@programming.dev
 
 

Context

Being a full stack developer, I have decent experience with both python and Typescript. I often use python for API development and I have been trying to write code that is pep-484 compliant (aka fully typed). However, often I get the feeling that if I was using TypeScript it would be much easier.

That got me wondering why there isn't a fully typed language that compiles to python.

I am aware of some arguments, so I am going to get the conversation started by providing my thoughts on them.

ts2python

ts2python is a TypeScript to python compiler.

Unfortunately, it covers only a small subset of python's capabilities. I am not sure why this hasn't been adopted and/or expanded to cover more of python's capabilities, but I can see possible issues with some python features that are not supported by TypeScript like context managers or operator overloading.

Still wondering if it would be possible to extend the TypeScript compiles so it would support such features?

pep-484

pep-484 describes how to provide type hints for python, it's not ideal but good enough that don't have to invent a new language.

IMO that's a trap, pep-484 (and other typing related peps) are not a good enough solution, on the contrary sometimes they are straight up misleading.

For example, consider the stubs for comparisons with built-in types, you would notice that they are defined as __op__(self, other: Any) -> bool: ... which is not correct as when other implements __opposite_op__ that is called instead of builtin.__op__, and it's return value may be of a different type.

Typing tools have not caught up with it, right now only pyright has full compliance with pep-484 (and other typing related peps). For that reason, SQLAlchemy had to introduce more than a couple of workarounds so MyPy can understand what's is happening behind the scenes, even for features that are pep-484 compliant.

Use Another Language

Python was never meant to be fully typed, and they make it clear.

True, but there are a bunch of libraries unique to python that make it a mandatory choice for many tasks. Things are changing and other options become available, but it's going to take time until there is another viable alternative.

Conclusion

Interested to read your thoughts.

  1. Is there another reason typing support hasn't advanced?
  2. Are you satisfied with typing support for python?
  3. Are you transitioning to another language?
  4. Are you aware of any new and exciting typing tools?

Of course, if typing is not an issue for you, that's okay, every software has different constraints.

329
330
140
submitted 2 years ago* (last edited 2 years ago) by ebits21@lemmy.ca to c/python@programming.dev
 
 

Previously LGPL, now re-licensed as closed-source/commercial. Previous code taken down.

Commercial users pay $99/year, free for personal use but each user has to make a free account after a trial period.

331
332
333
 
 

Been 2 months since the update but I don't see any news about code editor support for the new syntax.

This kind of makes sense, i don't expect code editor support to pop up so fast, but i was at least hoping to hear some news about it.

The PEP in question: PEP 695

334
335
336
 
 

I want to write my python code using thonny, but I seem to be getting some error when trying to open a file in thonny (Ctrl + O) and when instantiating a window, for example with TKinter. The exact error can be found here:

17:21:40.785 WARNING thonny.ui_utils: Zenity returned code 255 and stderr 'Diese Option steht nicht zur Verfügung. Bitte verwenden Sie »--help« für alle Anwendungsmöglichkeiten.\n' The text is german and says "This option is not available. lease use --help for viewing all options". For now I'll just use vim instead, but I really wanna go back to thonny. Has anyone ever experienced something like this before?

In case it matters, here is output I get from the terminal when running the program on startup:


17:25:36.099 INFO    thonny: Thonny version: 4.0.1
17:25:36.099 INFO    thonny: cwd: /home/marty
17:25:36.099 INFO    thonny: original argv: ['/usr/bin/python3', '/usr/bin/thonny']
17:25:36.100 INFO    thonny: sys.executable: /usr/bin/python3
17:25:36.100 INFO    thonny: sys.argv: ['/usr/bin/thonny']
17:25:36.100 INFO    thonny: sys.path: ['/usr/bin', '/usr/lib/python311.zip', '/usr/lib/python3.11', '/usr/lib/python3.11/lib-dynload', '/home/marty/.local/lib/python3.11/site-packages', '/usr/local/lib/python3.11/dist-packages', '/usr/lib/python3/dist-packages', '/usr/lib/python3.11/dist-packages']
17:25:36.100 INFO    thonny: sys.flags: sys.flags(debug=0, inspect=0, interactive=0, optimize=0, dont_write_bytecode=0, no_user_site=0, no_site=0, ignore_environment=0, verbose=0, bytes_warning=0, quiet=0, hash_randomization=1, isolated=0, dev_mode=False, utf8_mode=0, warn_default_encoding=0, safe_path=False, int_max_str_digits=-1)
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/thonny/__init__.py", line 235, in launch
    _delegate_to_existing_instance(sys.argv[1:])
  File "/usr/lib/python3/dist-packages/thonny/__init__.py", line 322, in _delegate_to_existing_instance
    sock, secret = _create_client_socket()
                   ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3/dist-packages/thonny/__init__.py", line 365, in _create_client_socket
    client_socket.connect(get_ipc_file_path())
ConnectionRefusedError: [Errno 111] Connection refused
17:25:37.820 INFO    thonny.plugins.cpython_frontend.cp_front: Creating LocalCPythonProxy
17:25:37.820 INFO    thonny.running: Starting the backend: ['/usr/bin/python3', '-u', '-B', '-m', 'thonny.plugins.cpython_backend.cp_launcher', '/home/marty'] /home/marty
337
338
339
37
RustPython (rustpython.github.io)
submitted 2 years ago by banghida@lemm.ee to c/python@programming.dev
340
7
submitted 2 years ago* (last edited 2 years ago) by mapto@lemmy.world to c/python@programming.dev
 
 

I deploy a FastAPI service with docker (see my docker-compose.yml and app).

My service directory gets filled with files index.html, index.html.1, index.html.2,... that all contain

They seem to be generated any time the docker healthcheck pings the service.

How can I get rid of these?

PS: I had to put a screenshot, because Lemmy stripped my HTML in the code quote.

341
 
 

Smassh is a TUI based typing test application inspired by MonkeyType -- A very popular online web-based typing application

Smassh tries to be a full fledged typing test experience but not missing out on looks and feel! There is a lot of room for improvements/additions and I am open to contributions and suggestions!

Github: https://github.com/kraanzu/smassh

Thank you! <3

342
 
 

I want to scrape a website using python selenium however every time it gives different id to same html element. I don't know how to circumvent it.

maybe XPATH is my saviour? but I don't know how reliable it is.

343
344
 
 

I often find myself defining function args with list[SomeClass] type and think "do I really care that it's a list? No, tuple or Generator is fine, too". I then tend to use Iterable[SomeClass] or Collection[SomeClass]. But when it comes to str, I really don't like that solution, because if you have this function:

def foo(bar: Collection[str]) -> None:
    pass

Then calling foo("hello") is fine, too, because "hello" is a collection of strings with length 1, which would not be fine if I just used list[str] in the first place. What would you do in a situation like this?

345
 
 

I recently picked up the python mastery bundle from humble bundle and one of the books (about larger-scale projects) has its exercises built around using anaconda virtual environments. I am able to create a project using:

% conda create -n project0 —channel=conda-forge python=3.12

But when I try to activate project0 I get an error that “activate” is an invalid choice. When I tried to initialize conda for Bash, my terminal behaved like the default echo for typed characters turned off.

346
 
 

So, you're an advanced beginner ...

347
 
 

Found via Planet Python

348
 
 

Hello,

I made a simple script to scraper threads.net using python and selenium. the script is just few lines long and it's easy to understand.

So what this script does?

first it will open edge browser(which you can change it to firefox or chrome). now you have to enter credentials to log into it. your browsing data and credentials will be stored in user_data which you can move around.

It scroll through threads's feed/hashtag/explore and It will store the src of every image it encounters so at the end we will have a links.txt file containing all the links to the images we have encountered.

now we have links.txt and we can use the following command to download all the images from the links.txt

wget -i links.txt

the script:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.options import Options
import time

options = Options()
options.add_argument("--user-data-dir=user_data")

driver = webdriver.Edge(options=options)

driver.get('https://threads.net')

s = set()

input("Press any key to continue...")
for i in range(30):
    try:
        elements = driver.find_elements(By.XPATH, "//img")
        for e in elements:
            s.add(e.get_attribute("src"))
        driver.execute_script("window.scrollBy(0, 1000);")
        time.sleep(0.2)
    except:
        print("oopsie")

with open("links.txt", 'w') as f:
    links = list(s)
    for l in links:
        f.write(l+"\n")

driver.quit()

I hope it was usefull :D

Edit: here is a link to links.txt https://0x0.st/HGjx.txt

349
 
 

Big update to rye is out: 0.18.0. Lots of bug fixes, lock files with source references for better docker support, global Python shims now pick up .python-version, automatic venv recreation on move. https://github.com/mitsuhiko/rye/discussions/544

via https://hachyderm.io/@mitsuhiko/111790331145932934

350
view more: ‹ prev next ›