Python

6288 readers
3 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
351
 
 

I've written a script to repost Github posts to Lemmy but now I'm trying to repost comments too and I don't know how to get the corresponding post_id that I need to do that.

lemmy.comment.create(post_id, comment_body)

Is there a way to associate the Github URL to the post_id when creating it with Pythorhead and then figuring out the Github URL from the comment and getting the post_id that way?

Alternatively, would something like this work? If so how would I limit the search to a specific community?

import requests

# GitHub URL of the post
github_url = "https://github.com/user/repo/issues/123"

# Lemmy API endpoint to search for posts
lemmy_search_url = "https://lemmy.ml/api/v3/search"

# Query parameters for the search endpoint
params = {
    "q": github_url,
    "type": "link",
    "sort": "relevance",
    "limit": 1
}

# Send a GET request to the search endpoint
response = requests.get(lemmy_search_url, params=params)

# Parse the response JSON to get the post_id
post_id = response.json()["posts"][0]["id"]

# Use the post_id to create a comment in Lemmy
lemmy.comment.create(post_id, comment_body)

Are there any other ways?

352
 
 

Long time coder here with a STEM degree, but not a CS degree. I've mostly used programming as a tool to help me with my job rather than as my job, so I'm conscious that I have some gaps in my programming skillset.

To close a couple of those gaps, I'm trying become competent at Github and take my Python skills to the next level.

If you kind people could provide suggestions on improvements I could make to this repo and the code in it I'd be ever so grateful. :)

It's a bot to run Lemmy posts and comments through the pretrained Detoxify transformer model and to report toxic comments for the Mods to action.

353
 
 

Hello everybody!

I wanted to share with you a small project I started recently.

TootNotify is a command line utility written in Python, which let's you send private toots (direct messages) to a given user, with the possibility of attaching media, flagging it as sensitive, and including content warnings.

This project was born out of a small program I had written some time ago, to notify me when long-running processes and tasks finished. It originally worked with Twitter, and worked for text-based messages only. Sadly, Twitter killed the DM functionality shortly after I originally coded this tool, and it got abandoned a few years ago.

Since then, I have moved away from Twitter, and with the prospect of having some long-running tasks in the horizon for my PhD dissertation, I figured I should implement this same functionality for Mastodon. I ended leveling-up my effort: deciding to make TootNotify into a full python package, and challenging myself to include broader functionality regarding attaching media to the notifications.

I hope some of you may find this tool interesting and/or useful, and please do drop comments or Issues on github if you find any problems/bugs.

Thanks!

354
 
 

Yesterday, I submitted a project to HN which ended up being ranked 6th for the day. My Django backend and database shared a single CPU. Check out how well it handled the load spike.

355
356
357
 
 

A mega tutorial with dozens of examples on how to use the pathlib module in Python 3

358
 
 

Mypy is a type checker for python.

359
360
361
 
 

I stumbled upon this while researching package management options for python, and found it a really interesting read.

I like python as a language but this mess is something that needs to be addressed for me to consider python for future projects. I can't imagine how confusing it must be for new users.

362
 
 

Break free from the burden of maintaining your GitHub streak manually! Introducing 'GitHub streak maintainer'. Say goodbye to missed days and hello to an effortless streak. With everything automated, this innovative project ensures your GitHub streak stays unbroken. Let the streak remain till the end of time!

Repo Link: GitHub

Motivation: I am Lazy.

363
364
365
12
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/python
 
 

A brief, code based cookbook for writing command-line path processors in python 3.latest, from a MacOS POV.

366
 
 

Are you doing data science? Statistics? No?

Then for god's sake don't use pandas, you just look dumb af when you pull several MB of a package just to load csv. If you find yourself doing that, just stop programming and look for another job

Thanks for attention

367
368
 
 

Hi everyone,

I'm pleasantly surprised and very thankful for the traction Plemmy has received in a few short weeks since its initial release!

The primary goal of Plemmy is simple: offer access to the LemmyHttp API in Python, allowing users to interact with any Lemmy instance using Python.

Plemmy's LemmyHttp object does just this, returning Python request.Response objects resulting from Lemmy API calls. All LemmyHttp functions have been implemented!

With release 0.3.0, Plemmy now offers a way to parse request.Response objects, extracting all information and placing them in easy-to-use Python objects. The design of these functions/objects closely mirrors the objects and data types defined in the lemmy-js-client.

These additions should make interacting with Lemmy in Python easier than ever. Check out Plemmy's repository for example usage (more documentation to come!).

Thanks for the continued support,

Travis

369
 
 

Featuring:

  • Identifying bundled libraries on Python distributions to help scanning for vulnerabilities.
  • Trusted Publisher adoption metrics.
  • GitHub push protection for PyPI API tokens.

Lots of great stuff!

370
 
 

I am writing an object-oriented app to help our developers manage some cloud systems. I'd like to make the configuration information available to all the classes, but I'm not sure of a good way to do that. Everything I can think of seems to fall under the category of "global variables" which as far as I know is a Very Bad Thing.

I already have a logging Mixin class that enables logging for every class that inherits it, and I was wondering if that's the right way to approach the configuration data:

class LoggingMixin:
    @classmethod
    @property
    def log(cls):
        return logging.getLogger(cls.__name__)

class TestClassA(LoggingMixin):
    def testmethod1(self):
        self.log.debug("debug message from test class A")

if __name__ == "__main__":
    logging.basicConfig(
        format="{created:<f} {levelname:>5s} {name}.{funcName:>8s} |{message}|",
        level=logging.DEBUG,
        style="{",
    )

    a = TestClassA()
    a.testmethod1()

Outputs (in case you are curious)

1688494741.449282 DEBUG TestClassA.testmethod1 |debug message from test class A|

What's a good way of making data from a class available to all classes/objects? It wouldn't be static, it'd be combined from a JSON file and any command line parameters.

If I copied the example above but changed it to a ConfigMixin, would that work? With the logging example, each class creates its own logger object when it first calls self.log.debug, so that might not work because each object needs to get the same config data.

Is there a pattern or other design that could help? How do you make configuration data available to your whole app? Do you have a config object that can get/set values and saves to disk, etc?

Thank you for reading, my apologies for poorly worded questions.

371
 
 

An exploration of partial() and partialmethod() in functools

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

VS Code is dropping Python 3.7 support in September 2023 and with the following Python releases they are planning to drop support of a Python version in the first extension release of the year following after EOL.

373
 
 

Once you've installed it, pytest will produce nice colourised diffs for any assert == :

😍

Via https://mastodon.social/@hynek/110479665200902390

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

Last month, I developed a script because lemmy.ml had become too slow. Unfortunately, I have the same problem again, but this time there are too many instances to evaluate, causing the script to take an excessively long time to complete. I'm seeking advice on how to enhance the script to simultaneously ping multiple instances. Are there any alternative scripts available that might provide a more efficient solution?

git clone https://github.com/LemmyNet/lemmy-stats-crawler
cd lemmy-stats-crawler
cargo run -- --json > stats.json
#!/usr/bin/env python3
import json
import time
import requests
import requests.exceptions

from typing import List, Dict

TIME_BETWEEN_REQUESTS = 5  # 10 * 60 = 10 minutes
TIME_TOTAL = 60  # 8 * 60 * 60 = 8 hours


def get_latency(domain):
    try:
        start = time.time()
        if not domain.startswith(("http://", "https://")):
            domain = "https://" + domain
        requests.get(domain, timeout=3)
        end = time.time()
        return end - start
    except requests.exceptions.Timeout:
        return float("inf")


def measure_latencies(domains, duration):
    latencies = {}
    start_time = time.time()
    end_time = start_time + duration
    while time.time() < end_time:
        latencies = measure_latencies_for_domains(domains, latencies)
        time.sleep(TIME_BETWEEN_REQUESTS)
    return latencies


def measure_latencies_for_domains(domains, latencies):
    for domain in domains:
        latency = get_latency(domain)
        latencies = add_latency_to_domain(domain, latency, latencies)
    return latencies


def add_latency_to_domain(domain, latency, latencies):
    if domain not in latencies:
        latencies[domain] = []
    latencies[domain].append(latency)
    return latencies


def average_latencies(latencies):
    averages = []
    for domain, latency_list in latencies.items():
        avg_latency = sum(latency_list) / len(latency_list)
        averages.append((domain, avg_latency))
    return averages


def sort_latencies(averages):
    return sorted(averages, key=lambda x: x[1])


def get_latency_report(domains, duration):
    latencies = measure_latencies(domains, duration)
    averages = average_latencies(latencies)
    return sort_latencies(averages)


def get_instances(data: Dict) -> List[Dict]:
    instances = []
    for instance_details in data["instance_details"]:
        instances.append(instance_details)
    return instances


def get_domains(instances: List[Dict]) -> List[str]:
    return [instance["domain"] for instance in instances]


def load_json_data(filepath: str) -> Dict:
    with open(filepath) as json_data:
        return json.load(json_data)


def main():
    data = load_json_data('stats.json')
    instances = get_instances(data)
    domains = get_domains(instances)
    report = get_latency_report(domains, TIME_TOTAL)
    for domain, avg_latency in report:
        print(f"{domain}: {avg_latency:.2f} seconds")


if __name__ == "__main__":
    main()
375
9
submitted 1 year ago* (last edited 1 year ago) by [email protected] to c/python
 
 

After learning about TYPE_CHECKING i made it a habit to put all imports that were only needed for type checking into an if TYPE_CHECKING: guard. But now I am wondering if that is actually intended to be used like that. Checking whether an import is only needed at type checking time can get quite tedious and sometimes you run into situations were you introduced some code that made the import a requirement at runtime.

How do you use TYPE_CHECKING? Whenever it is possible or only when using it actually solves a circular import?

view more: ‹ prev next ›