Python

7707 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
26
13
submitted 2 months ago* (last edited 2 months ago) by bterwijn@programming.dev to c/python@programming.dev
 
 

An exercise to help build the right mental model for Python data. The “Solution” link uses memory_graph to visualize execution and reveals what’s actually happening:

27
28
29
 
 

With a project structure like this:

├─main.py
└─src
    ├─dep1.py
    └─dep2.py

where the contents of each file is as follows:

main.py: import src.dep1 as firstdep; print("Total success")

dep1.py: import dep2 as seconddeb; print("success 1/3")

dep2.py: print("success 2/3")

Is the best way to do this creating an __init__.py file in src and importing src.dep2 in dep1.py? or is this a bad idea?

30
 
 

Barry Warsaw, writing for the Python steering council, has announced that PEP 810 ("Explicit lazy imports") has been approved, unanimously, by the four who could vote. Since Pablo Galindo Salgado was one of the PEP authors, he did not vote. The PEP provides a way to defer importing modules until the names defined in a module are needed by other parts of the program. We covered the PEP and the discussion around it a few weeks back. The council also had "recommendations about some of the PEP's details, a few suggestions for filling a couple of small gaps", including:

Use lazy as the keyword. We debated many of the given alternatives (and some we came up with ourselves), and ultimately agreed with the PEP's choice of the lazy keyword. The closest challenger was defer, but once we tried to use that in all the places where the term is visible, we ultimately didn't think it was as good an overall fit. The same was true with all the other alternative keywords we could come up with, so... lazy it is!

What about from foo lazy import bar? Nope! We like that in both module imports and from-imports that the lazy keyword is the first thing on the line. It helps to visually recognize lazy imports of both varieties.

31
 
 

Python already has several ways to run programs concurrently — including asynchronous functions, threads, subinterpreters, and multiprocessing — but all of those options have drawbacks of one kind or another. PEP 703 ("Making the Global Interpreter Lock Optional in CPython") removed a major barrier to running Python threads in parallel, but also exposed Python programmers to the same tricky synchronization problems found in other languages supporting multithreaded programs. A new draft proposal by Mark Shannon, PEP 805 ("Safe Parallel Python"), suggests a way for the CPython runtime to cut down on concurrency bugs, making it more practical for Python programmers to use versions of the language without the global interpreter lock (GIL).

32
 
 

Understanding and debugging Python data structures gets easier with memory_graph visualization. Here's a Multiway Tree example. A Multiway Tree is similar to a Binary Tree but has an arbitrary number of children making the tree less deep and more efficient.

33
 
 

An exercise to help build the right mental model for Python data. The “Solution” link uses memory_graph to visualize execution and reveals what’s actually happening:

34
 
 

Though I still consider myself active, the other maintainers are... not really. We require approval from a maintainer who didn't author the PR to merge a PR, and thus 4 PRs have been collecting dust waiting for review for half a year now, including the quite critical #376 addressing more indent_namespace false positives.

While maintainers don't necessarily need to commit code, they do need to learn how the code works so they may check code quality when reviewing PRs. Some Python experience would be preferred.

35
36
 
 

The Python Software Foundation, earlier this year, successfully obtained a $1.5 million grant from the US National Science Foundation "to address structural vulnerabilities in Python and PyPI". The actual grant came with some strings attached though, in the form of a requirement not to pursue diversity, equity, and inclusion programs. So the Foundation has withdrawn the proposal rather than agree to terms that run counter to its own mission.

We're disappointed to have been put in the position where we had to make this decision, because we believe our proposed project would offer invaluable advances to the Python and greater open source community, protecting millions of PyPI users from attempted supply-chain attacks. The proposed project would create new tools for automated proactive review of all packages uploaded to PyPI, rather than the current process of reactive-only review.

37
38
39
40
 
 

I'm looking for a way to generate a single Markdown (.md) file that includes all the file names, function definitions, and docstrings from my Python files. Ideally, this should traverse a directory recursively while respecting the .gitignore rules.

Does anyone know of a package that accomplishes this?

41
42
 
 

An exercise to help build the right mental model for Python data. The “Solution” link uses memory_graph to visualize execution and reveals what’s actually happening:

43
44
5
Python bitwise operators (programming.dev)
submitted 3 months ago* (last edited 3 months ago) by bterwijn@programming.dev to c/python@programming.dev
 
 

Teaching and learning Python bitwise operators gets much easier after showing the binary representations of integers using memory_graph: bitwise operators in Memory Graph Web Debugger

Understanding of the inverse ~ operator is helped by showing the two’s complement representation.

45
 
 

I have a list of communities, each with total votes, upvote percentage, and the community name. I want to sort the list by 'engagement,' which would be some combination of total votes and upvote percentage. What is the best way to do this? What would be the best measure of 'engagement' with each community given this data?

46
 
 

Hi everyone,

I’m trying to write a Python script to fetch all the communities a specific Lemmy user is subscribed to, with one community per line.

I’ve seen examples using raw requests (like this post), but I’d like to do it using Pythorhead instead.

Has anyone done this before or can provide a simple example of how to list a user’s subscriptions using Pythorhead?

Similarly, I'd also like to get the list of communities that the user has blocked.

Thanks!

47
48
49
 
 

cross-posted from: https://lemmy.dbzer0.com/post/55501944

Hey, I’ve been kicking around an idea for a bot: it would look for fanfiction links shared in a server and keep track of which ones get shared the most.

The concept:

  • Track sites like AO3, FanFiction.net, ScribbleHub, etc.
  • Count how often each link gets posted
  • Commands to see the “top” links or which domains are tracked

It’s just a rough idea, I haven’t built it yet. Curious if anyone thinks this would actually be useful or has tips for implementing it without overcomplicating things.

import re
import json
import discord
from collections import Counter
from discord.ext import commands

TOKEN = "YOUR_BOT_TOKEN"
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents)

# Domain list you want to track (lower-case)
TRACK_DOMAINS = {
    "archiveofourown.org",
    "fanfiction.net",
    "forum.questionablequesting.com",
    "forums.spacebattles.com",
    "forums.sufficientvelocity.com",
    "webnovel.com",
    "hentai-foundry.com",
    "scribblehub.com",
}

link_pattern = re.compile(r'https?://\S+')

link_counter = Counter()

def domain_of_url(url: str) -> str | None:
    try:
        # Extract domain part
        from urllib.parse import urlparse
        parsed = urlparse(url)
        domain = parsed.netloc.lower()
        # remove leading “www.”
        if domain.startswith("www."):
            domain = domain[4:]
        return domain
    except Exception:
        return None

def save_links():
    with open("links.json", "w") as f:
        # convert counts to dict
        json.dump(dict(link_counter), f)

def load_links():
    try:
        with open("links.json") as f:
            data = json.load(f)
        for link, cnt in data.items():
            link_counter[link] = cnt
    except FileNotFoundError:
        pass

@bot.event
async def on_ready():
    load_links()
    print(f"Bot is ready. Logged in as {bot.user}")

@bot.event
async def on_message(message):
    if message.author.bot:
        return
    links = link_pattern.findall(message.content)
    for link in links:
        dom = domain_of_url(link)
        if dom in TRACK_DOMAINS:
            link_counter[link] += 1
    await bot.process_commands(message)

@bot.command(name="links")
async def links(ctx, top: int = 10):
    if not link_counter:
        await ctx.send("No links recorded.")
        return
    sorted_links = sorted(link_counter.items(), key=lambda x: x[1], reverse=True)
    display = sorted_links[:top]
    lines = [f"{link} — {count}" for link, count in display]
    await ctx.send("**Top links:**\n" + "\n".join(lines))

@bot.command(name="domains")
async def domains(ctx):
    """Show which domains are tracked."""
    await ctx.send("Tracked domains: " + ", ".join(sorted(TRACK_DOMAINS)))

@bot.command(name="dump")
async def dump(ctx):
    """For admin: dump full counts (might be large)."""
    if not link_counter:
        await ctx.send("No data.")
        return
    lines = [f"{link} — {cnt}" for link, cnt in sorted(link_counter.items(), key=lambda x: x[1], reverse=True)]
    chunk = "\n".join(lines)
    # Discord message length limit; you may need to split
    await ctx.send(f"All counts:\n{chunk}")

@bot.event
async def on_disconnect():
    save_links()

bot.run(TOKEN)
50
 
 

I've noticed that a lot of my projects follow the same basic architecture and I often like to write some simple tools for myself where it takes much longer to set up the project than it does to write the domain specific code. So I've been looking into scaffolding tools to help with this. I'm aware of Cookiecutter and Copier. Are there others? How do they compare? What are your preferences and why?

view more: ‹ prev next ›