1
23
Python 3.13.7 (www.python.org)
2
13

I have been working for a while on a easy way to run some javascript tools from Python. Primarily to be able to use transpilers implemented in JS itself in my python applications pipeline.

That lead to the creation of DukPy ( github.com/amol-/dukpy ), which is met my needs because is very straightforward to install without any external dependencies and provides a good support of modern Javascript.

The project is only a few github stars away from the 512 badge, and as useless as that target is I thought that it was a good excuse to share the project here if anyone was looking for something similar

3
3
4
3
5
29
submitted 1 week ago* (last edited 1 week ago) by promitheas@programming.dev to c/python@programming.dev

Hello guys, Ive built a super simple web app to convert google maps urls to the osm format, mainly for my personal use but also for anyone who has this use case as well.

Most link formats work, but there are a couple that have been giving me particular trouble. Here are the formats that work as expected:

https://www.google.com/maps/place/Eiffel+Tower/@48.8584,2.2945,17z
https://google.com/maps/place/Statue+of+Liberty/@40.6892,-74.0445,17z
https://maps.app.goo.gl/SoDFgcPJKVPeZXqd8
https://www.google.co.uk/maps/place/Buckingham+Palace/@51.5014,-0.1419,17z
https://www.google.de/maps/place/Brandenburger+Tor/@52.5163,13.3777,17z

These 2 formats dont work on the server, but if I run the app locally they work and give a correct osm formatted link:

https://maps.app.goo.gl/jXqDkM2NWN55kZcd9?g_st=com.google.maps.preview.copy
https://maps.google.com/maps?q=Big+Ben%2C+London

You can test it on the actual website (gmaps2osm.de), but essentially if you try to convert links of this format on the actual website you get an Internal Server Error after around 10 seconds of it thinking.

Right now Im focusing on the .preview.copy formatted links. As you will see from the code below, I am using playwright to headlessly handle googles annoying redirects and consent forms:

from typing import final
from flask import Flask, request, render_template, jsonify
from datetime import datetime
import re
import urllib.parse
import requests
from playwright.sync_api import sync_playwright

# Test urls for regex:
# https://www.google.com/maps/place/Eiffel+Tower/@48.8584,2.2945,17z
# https://google.com/maps/place/Statue+of+Liberty/@40.6892,-74.0445,17z
# https://maps.google.com/maps?q=Big+Ben%2C+London # has an issue, doesnt work
# https://maps.app.goo.gl/4jnZLELvmpvBmFvx8
# https://maps.app.goo.gl/jXqDkM2NWN55kZcd9?g_st=com.google.maps.preview.copy
# https://www.google.co.uk/maps/place/Buckingham+Palace/@51.5014,-0.1419,17z
# https://www.google.de/maps/place/Brandenburger+Tor/@52.5163,13.3777,17z

debug_enabled = False
is_headless = not debug_enabled
print(f"is_headless: {is_headless}")
log_file_path = ""
app = Flask(__name__)

GMAPS_URL_RE = re.compile(
	r"""(?x)  # Verbose mode
	^https?://
	(
		(www\.)?
		(google\.[a-z.]+/maps)			  |		# www.google.com/maps, google.co.uk/maps, etc.
		(maps\.google\.[a-z.]+)			  |		# maps.google.com
		(goo\.gl/maps)					  |		# goo.gl/maps
		(maps\.app\.goo\.gl)					# maps.app.goo.gl
		# (maps\.app\.goo\.gl.*\.preview\.copy)	# maps.app.goo.gl(...).preview.copy
	)
	""",
	re.IGNORECASE
)

# ---- Dispatcher
def extract_coordinates(url: str):
	if ".preview.copy" in url:
		return handle_preview_copy_url(url)
	if "maps?q=" in url:
		return handle_browser_resolved_url(url)
	else:
		return handle_standard_url(url)

# ---- Handler: preview.copy links
def handle_preview_copy_url(url: str):
	url = url.split('?')[0]
	input_url = resolve_initial_redirect(url)
	if "consent.google.com" in input_url:
		parsed = urllib.parse.urlparse(input_url)
		query = urllib.parse.parse_qs(parsed.query)
		continue_url = query.get("continue", [""])[0]
		if continue_url:
			input_url = urllib.parse.unquote(continue_url)
		else:
			log_msg("ERROR", "No 'continue' parameter found.")
			return None, input_url
	final_url = extract_with_playwright(input_url)
	coord = extract_coords_from_url(final_url)
	return coord, final_url

# ---- Handler: links with only a query containing place names (https://maps.google.com/maps?q=Big+Ben%2C+London)
def handle_browser_resolved_url(url: str):
	input_url = resolve_initial_redirect(url)
	if "consent.google.com" in input_url:
		parsed = urllib.parse.urlparse(input_url)
		query = urllib.parse.parse_qs(parsed.query)
		continue_url = query.get("continue", [""])[0]
		if continue_url:
			input_url = urllib.parse.unquote(continue_url)
		else:
			log_msg("ERROR", "No 'continue' parameter found.")
			return None, input_url
	final_url = extract_with_playwright(input_url)
	coord = extract_coords_from_url(final_url)
	return coord, final_url

# ---- Handler: standard links
def handle_standard_url(url: str):
	try:
		# Follow redirect to get final destination
		response = requests.head(url, allow_redirects=True, timeout=10)
		final_url = response.url
		log_msg("DEBUG", "Final URL:", final_url)
	except requests.RequestException as e:
		raise RuntimeError(f"Failed to resolve standard URL: {e}")
	coords = extract_coords_from_url(final_url)
	return coords, final_url

# ---- Utility: redirect resolver
def resolve_initial_redirect(url: str):
	try:
		response = requests.get(url, allow_redirects=True, timeout=10)
		return response.url
	except Exception as e:
		log_msg("ERROR", "Redirect failed: ", e)
		return url

# ---- Utility: use playwright to get the final rendered URL
def extract_with_playwright(url:str):
	with sync_playwright() as p:
		browser = p.chromium.launch(headless=is_headless)
		page = browser.new_page()
		page.goto(url)
		# Click reject button if necessary
		try:
			page.locator('button:has-text("Reject all")').first.click(timeout=5000)
		except:
			pass # No reject button
		page.wait_for_function(
				"""() => window.location.href.includes('/@')""",
				timeout=15000
		)
		final_url = page.url
		log_msg("DEBUG", "Final URL: ", final_url)
		browser.close()
		return final_url

# ---- Utility: extract coordinates with regex patterns
def extract_coords_from_url(url: str):
	patterns = [
		r'/@([-.\d]+),([-.\d]+)',				 # Matches /@lat,lon
		r'/place/([-.\d]+),([-.\d]+)',			 # Matches /place/lat,lon
		r'/search/([-.\d]+),\+?([-.\d]+)',
		r'[?&]q=([-.\d]+),([-.\d]+)',			 # Matches ?q=lat,lon
		r'[?&]ll=([-.\d]+),([-.\d]+)',			 # Matches ?ll=lat,lon
		r'[?&]center=([-.\d]+),([-.\d]+)',		 # Matches ?center=lat,lon
		r'!3d([-.\d]+)!4d([-.\d]+)'				 # Matches !3dlat!4dlon
	]
	for pattern in patterns:
		match = re.search(pattern, url)
		if match:
			return match.groups()
	return None

# ---- Utility: logging function to output and write to file
def log_msg(level: str, msg: str, optional_arg = None):
	ts = datetime.now()
	iso_ts = ts.isoformat()
	if level == "DEBUG" and debug_enabled == False:
		return
	if optional_arg:
		log_line = f"[{iso_ts}]:[{level}]: {msg} {optional_arg}"
	else:
		log_line = f"[{iso_ts}]:[{level}]: {msg}"
	print(log_line)
	with open(log_file_path+"gmaps2osm_logs.txt", "a") as log_file:
		log_file.write(log_line+"\n")

@app.route("/", methods=["GET", "POST"])
def index():
	result = {}
	if request.method == "POST":
		url = request.form.get("gmaps_url", "").strip()
		log_msg("DEBUG", "URL:", url)
		if not url:
			result["error"] = "Please enter a Google Maps URL."
			log_msg("DEBUG", "Not a URL")
		else:
			try:
				if not GMAPS_URL_RE.search(url):
					result["error"] = "Please enter a valid Google Maps URL."
				else:
					coords, final_url = extract_coordinates(url)
					log_msg("DEBUG", "coords:", coords)
					if coords:
						lat, lon = coords
						result["latitude"] = lat
						result["longitude"] = lon
						result["osm_link"] = f"https://osmand.net/map?pin=%7Blat%7D%2C%7Blon%7D#16/{lat}/{lon}"
					else:
						raise ValueError("No coordinates found in the URL.")
			except ValueError as e:
				result["error"] = f"Error resolving or parsing URL: {e}"
	return render_template("index.html", **result)

if __name__ == "__main__":
	app.run(debug=True)

You can view all the code at the github: https://github.com/promitheas17j/gmaps2osm

But essentially its running as a docker container (my first project using docker woowoo) and this is the output of the logs on the server when I enter such a link:

$ docker logs -f gmaps2osm
[2025-08-15 17:42:00,132] ERROR in app: Exception on / [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/flask/app.py", line 1511, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.10/dist-packages/flask/app.py", line 919, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.10/dist-packages/flask/app.py", line 917, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.10/dist-packages/flask/app.py", line 902, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)  # type: ignore[no-any-return]
  File "/app/app.py", line 164, in index
    coords, final_url = extract_coordinates(url)
  File "/app/app.py", line 42, in extract_coordinates
    return handle_preview_copy_url(url)
  File "/app/app.py", line 61, in handle_preview_copy_url
    final_url = extract_with_playwright(input_url)
  File "/app/app.py", line 113, in extract_with_playwright
    page.wait_for_url("**/@**", timeout=15000)
  File "/usr/local/lib/python3.10/dist-packages/playwright/sync_api/_generated.py", line 9162, in wait_for_url
    self._sync(
  File "/usr/local/lib/python3.10/dist-packages/playwright/_impl/_sync_base.py", line 115, in _sync
    return task.result()
  File "/usr/local/lib/python3.10/dist-packages/playwright/_impl/_page.py", line 584, in wait_for_url
    return await self._main_frame.wait_for_url(**locals_to_params(locals()))
  File "/usr/local/lib/python3.10/dist-packages/playwright/_impl/_frame.py", line 263, in wait_for_url
    async with self.expect_navigation(
  File "/usr/local/lib/python3.10/dist-packages/playwright/_impl/_event_context_manager.py", line 33, in __aexit__
    await self._future
  File "/usr/local/lib/python3.10/dist-packages/playwright/_impl/_frame.py", line 239, in continuation
    event = await waiter.result()
playwright._impl._errors.TimeoutError: Timeout 15000ms exceeded.
=========================== logs ===========================
waiting for navigation to "**/@**" until 'load'
============================================================

This was when I gave this URL as input: https://maps.app.goo.gl/jXqDkM2NWN55kZcd9?g_st=com.google.maps.preview.copy

Anyone have any idea why playwright is throwing errors at me?

Thanks in advance, I really appreciate any help you can give me. Ive been banging my head about this issue on and off the past couple of months.

6
14
how to use async for? (discuss.tchncs.de)

what does async for do? or, how do async iterators in general work, because I'm pretty sure async for just helps you iterate on an async iterator.

I think async iterators have a method __anext__() which "steps through the iterable"? I don't understand what stepping through means.

Also __anext__() returns an awaitable? what does that mean, especially if I have something like an AsyncIterator[Object], what does the awaitable have to do with the object?

7
9
8
50
9
12
10
13
11
16
12
-8

I published a concise guide diving into what’s new and improved compared to Python 3.12—beyond the headlines of JIT and no-GIL. Highlights include: 🔹 A modernized pdb debugger with autocompletion and post-mortem support for syntax errors 🔹 More reliable and faster file system handling with pathlib and shutil improvements 🔹 Expanded annotation capabilities for clearer, more maintainable code 🔹 Performance boosts in JSON serialization and dataclass creation 🔹 Async enhancements that make queue management and completion easier

Why upgrade? Because these subtle but powerful tweaks will simplify your workflow, increase stability, and help you write better Python in 2025.

👉 Unlock the full rundown on Medium: https://blog.devgenius.io/python-3-13-hidden-features-and-improvements-you-might-have-missed-79c06b9bbb8f

If you’re serious about Python and want to stay ahead, this is a must-read.

#Python #Python313 #Programming #SoftwareDevelopment #Asyncio #Debugger #Performance #DevGenius #CodingTips

13
19
Python 3.13.6 (www.python.org)
14
22
submitted 3 weeks ago by lena@gregtech.eu to c/python@programming.dev
15
2
submitted 3 weeks ago* (last edited 3 weeks ago) by logging_strict@programming.dev to c/python@programming.dev

Will commonly see examples of SQLAlchemy or SQLModel Bases that look like:

class DB1Base(SQLModel, registry=registry()):

There are x10^256 examples of this. And so lets take a step backwards and appreciate the prolific nature of

the guy in charge of random database object names

sqlalchemy docs screams to apply a database object naming convention.

alembic docs has a whole section dedicated it.

So what about SQLModel docs or issues? .... (crickets)

Every example ever lacks applying naming convention to the registry metadata!

from sqlalchemy import MetaData
from sqlalchemy.orm import registry

# copy+paste from the sqlalchemy docs. One case don't wanna be creative.
naming_convention_no_schema = {
    "ix": "ix_%(column_0_label)s",
    "uq": "uq_%(table_name)s_%(column_0_name)s",
    "ck": "ck_%(table_name)s_`%(constraint_name)s`",
    "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
    "pk": "pk_%(table_name)s",
}

metadata = MetaData(
        naming_convention=naming_convention_no_schema,
        schema=schema,
)

reg = registry(metadata=metadata)

class DB1Base(SQLModel, registry=reg):

Why not to -- job security!

Can only imagine that folks wanting to give every single database object a name manually would do that as a form of job security. If that is the case then carry on.

For those of us who don't want to micro manage the micro manage of micro managers, would highly suggest not manually explicitly naming each and every database object. In each and every database. Not ever, but almost nearly ever.

For example, what if the guy in charge of random database object names gets a girlfriend? It could happen and what a disaster that would be. For him, for her, and for all the people who have to try to track down why a foreign key or unique constraint doesn't exist when it's clear as day in the code that it should be there.

Might have exaggerated, there is no agency of random database object names. But that won't stop the guy in charge of random database object names. Numerically, he can snap his fingers and poof, that agency will exist. Don't underestimate the power and unflinching resolve of the guy in charge of random database object names

The take away

Every single SQLModel Base ever neglects, when creating a registry, to apply database objects naming convention. This includes the SQLModel source code.

So why not submit a PR? SQLModel seems to be unmaintained. There are 69 PRs in the pipeline. So the options are:

  • wait for hell to start selling ice cream

  • poke fun at it

Only have enough coin for the boat man. So that leaves ...

16
12

if var1 equals 1, and you run var2 = var1, that sets var2 to 1.

if list1 equals [1, 2, 3], and you run list2 = list1, that sets list2 to list1

so if you then run var1 = 2, var2 will still be 1

but if you run list1 = [3, 2, 1], list2 will give [3, 2, 1]

17
17
submitted 3 weeks ago* (last edited 3 weeks ago) by loveknight@programming.dev to c/python@programming.dev

It's currently in its third edition, published November 2024.

ISBN-10: 0-13-817218-8

ISBN-13: 978-0-13-817218-3

I've discovered it (in its second edition) in my local library just yesterday. Even what little I've read so far has significantly improved my understanding, e.g. about decorators.

The testimonials for second edition are really something:

“I have been recommending this book enthusiastically since the first edition appeared in 2015. This new edition, updated and expanded for Python 3, is a treasure trove of practical Python programming wisdom that can benefit pro- grammers of all experience levels.”

—Wes McKinney, Creator of Python Pandas project, Director of Ursa Labs

“If you’re coming from another language, this is your definitive guide to taking full advantage of the unique features Python has to offer. I’ve been working with Python for nearly twenty years and I still learned a bunch of useful tricks, especially around newer features introduced by Python 3. Effective Python is crammed with actionable advice, and really helps define what our community means when they talk about Pythonic code.”

—Simon Willison, Co-creator of Django

“Now that Python 3 has finally become the standard version of Python, it’s already gone through eight minor releases and a lot of new features have been added throughout. Brett Slatkin returns with a second edition of Effective Python with a huge new list of Python idioms and straightforward recommendations, catching up with everything that’s introduced in version 3 all the way through 3.8 that we’ll all want to use as we finally leave Python 2 behind. Early sections lay out an enormous list of tips regarding new Python 3 syntaxes and concepts like string and byte objects, f-strings, assignment expressions (and their special nickname you might not know), and catch-all unpacking of tuples. Later sections take on bigger subjects, all of which are packed with things I either didn’t know or which I’m always trying to teach to others, including ‘Metaclasses and Attributes’ (good advice includes ‘Prefer Class Decorators over Metaclasses’ and also introduces a new magic method ‘init_subclass()’ I wasn’t familiar with), ‘Concurrency’ (favorite advice: ‘Use Threads for Blocking I/O, but not Parallelism,’ but it also covers asyncio and coroutines correctly) and ‘Robustness and Performance’ (advice given: ‘Profile before Optimizing’). It’s a joy to go through each section as everything I read is terrific best practice information smartly stated, and I’m considering quoting from this book in the future as it has such great advice all throughout. This is the definite winner for the ‘if you only read one Python book this year...’ contest. —Mike Bayer, Creator of SQLAlchemy

More testimonials are available under the link above.

Book website: https://effectivepython.com/ (Don't buy from Amazon, of course.)

If you're like me and prefer printed books, look up the ISBN at euro-book (which also offers portals for Brasil, Mexico and the USA) to find any affordable used copies.

18
15
19
6
20
8
21
23
submitted 1 month ago by Arkouda@lemmy.ca to c/python@programming.dev

I am learning both Greek and Python right now, so I decided to make a simple program that quizzes you on the translations by providing it in Greek and asking for English, and if you provide the English, it will output the Greek if it is in the dictionary.

Feel free to take and modify this for your own uses if interested! It would be as simple as replacing the Greek dictionary and references with anything else. :)

import random, sys

greekTranslation = {
    'Γεια σας': 'Hello',
    'Καλημέρα': 'Good morning',
    'Καλησπέρα': 'Good evening',
    'Ευχαριστώ': 'Thank you',
    'Παρακαλώ': 'Please',}

def practiceGreek():
    greekPhrase = random.choice(list(greekTranslation.keys()))
    print('')
    print(f'What is the translation of "{greekPhrase}"?')
    print('')
    answer = input('Your answer: ')
    if answer.strip().lower() == greekTranslation[greekPhrase].lower():
        print('')
        print('That is correct!')
        print('')
    else:
        print('')
        print(f'Incorrect! The correct translation is "{greekTranslation[greekPhrase]}".')
        print('')

def translateToGreek():
    print('')
    print('What would you like to translate into Greek?')
    print('')
    englishPhrase = input().strip().lower()
    reverseTranslation = {v.lower(): k for k, v in greekTranslation.items()}
    greekPhrase = reverseTranslation.get(englishPhrase)
    
    if greekPhrase:
        print('')
        print(f'The Greek phrase is: "{greekPhrase}"')
        print('')
    else:
        print('')
        print('I am sorry, I don\'t have that in my dictionary.')
        print('')
    
        
while True:
    print('Welcome to the Greek Practice Program!')
    print('')
    print('What would you like to do?')
    print('')
    print('[Practice] [Translate] [Exit]')
    print('')
    optionSelection = input().strip().lower()
        
    if optionSelection != 'practice' and optionSelection != 'translate' and \
       optionSelection != 'exit':
        print('')
        print('Please select either practice, translate, or exit')
        optionSelection = ''
        print('')

    if optionSelection == 'practice':
        while optionSelection == 'practice':
            practiceGreek()
            print('')
            print('Would you like another? [yes] [no]')
            print('')
            selection = input().strip().lower()
            if selection == 'yes':
                print('')
                continue
            else:
                print('')
                break
                
            
    elif optionSelection == 'translate':
        while optionSelection == 'translate':
            translateToGreek()
            print('')
            print('Would you like to translate another phrase? [yes] [no]')
            print('')
            selection = input().strip().lower()
            if selection == 'yes':
                print('')
                continue
            else:
                print('')
                break
        
    
    elif optionSelection == 'exit':
        print('')
        print('Thank you for using the Greek Practice Program!')
        print('')
        sys.exit()

22
77
Python f-string quiz (fstrings.wtf)
23
17

Hi, I recently realised one can use immutable default arguments to avoid a chain of:

def append_to(element, to=None):
    if to is None:
        to = []

at the beginning of each function with default argument for set, list, or dict.

24
34
Practice Python Projects (learnbyexample.github.io)

Hello.

I recently updated my "Practice Python Projects" ebook, which presents five beginner-to-intermediate level projects inspired by real world use cases.

You can read it online here: https://learnbyexample.github.io/practice_python_projects/preface.html

Or, you can get pdf/epub versions via https://learnbyexample.gumroad.com/l/py_projects/free (free till this weekend).

More resources related to projects: https://learnbyexample.github.io/py_resources/beginners.html#projects

Happy learning :)

25
69
view more: next ›

Python

7406 readers
1 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