626
4
submitted 3 years ago* (last edited 3 years ago) by breadcrumb@lemmy.world to c/python@programming.dev

I'm writing a python package that I would like to distribute as a standalone terminal app. The structure of the project folder is the following:

energy-monitor/
-- config/
-- doc/
-- tests/
-- energymonitor/
---- init.py -> (empty)
---- main.py -> def main()
---- data/
---- ..other packages..
-- project.toml

I'm using setuptools to generate a .tar.gz archive, some relevant parts of the project.toml file are:

[build-system]
requires = ["setuptools>=68.0"]
build-backend = "setuptools.build_meta"

[project]
name = "energy-monitor"
version = "0.0.1"

...

[tool.setuptools.packages.find]
where = ["energymonitor"]

[tool.setuptools.package-data]
data = ["data/*"]

[project.scripts]
energy-monitor = "energymonitor.main:main"

I generate the .tar.gz and the .whl files with the command python -m build, then I run pipx install path/to/energy-monitor.tar.gz. The installation is succesful, but when calling energy-monitor from the command line I get:

Traceback (most recent call last):
  File "/home/mattia/.local/bin/energy-monitor", line 5, in <module>
    from energymonitor.main import main
ModuleNotFoundError: No module named 'energymonitor'

Why is this happening? I was not able to find any helpful solution online. It's the first that I build a python package so sorry if the issue is trivial.

  • python version: 3.11.3
627
14

ok, so I've just started to learn python so very sorry for being a absolute dumbell. I'm doing some test stuff and noticed when I build and run my programs, they run yes, but they don't actually do anything. I've made sure to set the right language in sublime editor. perhaps it's something I've done wrong? when the console prints out the first question, i type in a number, but then nothing else happens. it seems to only print the first line and that's it. it's supposed to prompt the "second:" but it does nothing. I would really appreciate your help. thank you.

628
23
Permanently Deleted (git.disroot.org)
submitted 3 years ago* (last edited 2 years ago) by CoderSupreme@programming.dev to c/python@programming.dev

I've been working on some Python scripts for Lemmy that you might find useful. Here's what's included:

  • Reposting invidious feed
  • Purging your user posts and comments
  • Reposting GitHub issues and comments
  • Checking which instances block a given instance
  • Pinging instances multiple times and sorting them by latency
  • Awesome Lemmy Instances has an added column that shows total blocked users for each instance.

Thanks to @db0@lemmy.dbzer0.com for making Pythörhead.

You can find other useful Lemmy tools in Awesome Lemmy

629
15

I just found this plotting library by JetBrains that has a grammar of graphics interface (like plotnine). This will make a nice alternative for matplotlib for my research.

630
9

At least I posted it to !programming_horror@programming.dev first!

631
6
submitted 3 years ago* (last edited 3 years ago) by wesker@lemmy.sdf.org to c/python@programming.dev

I have a use case where I'd like to store a handful of strings with static values, alongside my code that references them. The general reason for not hard coding them where they're called, is that I'd like to make it easy for the end user to customize and modify them.

Are there any suggestions or comments about the best ways to do this? Storing them in a python file as vars seems reasonable. I've also considered saving them as JSON, though I don't know if there's any benefit to that in this case.

Thoughts are appreciated.

632
16

I've found this answer but in another answer the header was different. So what is this correct? Is there anyway to know from the output whether or not I've authenticated correctly?

import requests

# Replace <personal-access-token> with your personal access token
headers = {
    'Authorization': 'Bearer <personal-access-token>',
}

url = 'https://api.github.com/repos/LemmyNet/lemmy/issues'

response = requests.get(url, headers=headers)

# Print the response content
print(response.content)
633
40
submitted 3 years ago* (last edited 3 years ago) by GodOfThunder@lemm.ee to c/python@programming.dev

I've seen two approaches which I'm going to post in the comments to see which one is considered best. Feel free to suggest others.

634
14
submitted 3 years ago by BoneALisa@lemm.ee to c/python@programming.dev

I have done plenty of basic scripting for work, but this is my first "real" project, and wanted to see what real python experts think of it and see if there is anything I might be doing horribly wrong lol.

Im deploying documentation automatically here: https://ogs-python.dakotamarshall.net/

And the PyPI package is here: https://pypi.org/project/ogsapi/

I am very much a beginner, and its still very much a work in progress. Let me know if there is anything that could be drastically improved!

635
28
submitted 3 years ago* (last edited 3 years ago) by GodOfThunder@lemm.ee to c/python@programming.dev

I won't know if this script works until I run it and see the errors but the comments won't start to generate until after all the posts so I can't debug that part until I've already created too much content.

import sqlite3
import requests
from pythorhead import Lemmy
import schedule
import time
import logging
from config import *

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(name)s %(message)s",
    handlers=[logging.FileHandler("debug.log"), logging.StreamHandler()],
)


def initialize_database():
    conn = sqlite3.connect(DB_FILE)
    cursor = conn.cursor()
    cursor.execute("""
        CREATE TABLE IF NOT EXISTS posts (
            github_url TEXT PRIMARY KEY,
            lemmy_post_id INTEGER,
            lemmy_post_name TEXT,
            lemmy_post_body TEXT
        )
    """)
    cursor.execute("""
        CREATE TABLE IF NOT EXISTS comments (
            github_comment_id INTEGER PRIMARY KEY,
            lemmy_comment_id INTEGER,
            comment_user TEXT,
            comment_body TEXT
        )
    """)
    conn.commit()
    return conn


def initialize_lemmy_instance():
    lemmy = Lemmy(LEMMY_INSTANCE_URL)
    lemmy.log_in(LEMMY_USERNAME, LEMMY_PASSWORD)
    logging.info("Initialized Lemmy instance")
    return lemmy


def discover_community(lemmy, community_name):
    community_id = lemmy.discover_community(community_name)
    logging.info(f"Discovered community {community_name} with ID {community_id}")
    return community_id


def fetch_github_issues(repo):
    url = f"{GITHUB_API_BASE}/repos/{repo}/issues"
    headers = {"Accept": "application/vnd.github+json"}
    response = requests.get(url, headers=headers)
    logging.info(f"Fetched issues from {url}")
    return response.json()


def extract_issue_info(issue, repo):
    issue_url = issue["html_url"]
    issue_state = "[Closed]" if issue["state"] == "closed" else ""
    repo_abbr = "[BE]" if "lemmy" in repo else "[UI]"
    issue_title = f"{issue_state}{repo_abbr} {issue['title']} #{issue['number']}"
    issue_body = issue["body"]
    return issue_url, issue_title, issue_body


def post_issues_to_lemmy(lemmy, community_id, repo):
    conn = sqlite3.connect(DB_FILE)
    cursor = conn.cursor()
    
    issues = fetch_github_issues(repo)
    for issue in issues:
        issue_url, issue_title, issue_body = extract_issue_info(issue, repo)
        
        cursor.execute("SELECT lemmy_post_id FROM posts WHERE github_url=?", (issue_url,))
        existing_post = cursor.fetchone()
        
        if not existing_post:
            post = lemmy.post.create(community_id, issue_title, url=issue_url, body=issue_body)["post_view"]["post"]
            lemmy_post_id = post["id"]
            lemmy_post_name = post["name"]
            lemmy_post_body = post["body"]
            cursor.execute("INSERT INTO posts (github_url, lemmy_post_id, lemmy_post_name, lemmy_post_body) VALUES (?, ?, ?, ?)", (issue_url, lemmy_post_id, lemmy_post_name, lemmy_post_body))
            conn.commit()
            logging.info(f"Posted issue {issue_title} to community {community_id}")


def fetch_github_comments(repo, issue_number):
    url = f"{GITHUB_API_BASE}/repos/{repo}/issues/{issue_number}/comments"
    headers = {"Accept": "application/vnd.github+json"}
    response = requests.get(url, headers=headers)
    logging.info(f"Fetched comments for issue #{issue_number}")
    return response.json()


def post_comments_to_lemmy(lemmy, post_id, repo, issue_number):
    conn = sqlite3.connect(DB_FILE)
    cursor = conn.cursor()
    
    github_comments = fetch_github_comments(repo, issue_number)
    for comment in github_comments:
        github_comment_id = comment["id"]
        cursor.execute("SELECT lemmy_comment_id FROM comments WHERE github_comment_id=?", (github_comment_id,))
        existing_comment = cursor.fetchone()
        
        if not existing_comment:
            comment_user = comment["user"]["login"]
            comment_body = comment["body"]
            lemmy_comment_id = lemmy.comment.create(post_id, comment_body)["comment"]["id"]

            cursor.execute("INSERT INTO comments (github_comment_id, lemmy_comment_id, comment_user, comment_body) VALUES (?, ?, ?, ?)", (github_comment_id, lemmy_comment_id, comment_user, comment_body))
            conn.commit()
            logging.info(f"Posted comment {github_comment_id} to lemmy post {post_id}")


# Fetch the GitHub issue number and Lemmy post ID for each issue
def fetch_issue_data(repo):
    conn = sqlite3.connect(DB_FILE)
    cursor = conn.cursor()
    cursor.execute("SELECT github_url, lemmy_post_id FROM posts WHERE github_url LIKE ?", (f"https://github.com/{repo}/issues/%",))
    issue_data = cursor.fetchall()
    return issue_data


def extract_issue_number(github_url):
    return int(github_url.split("/")[-1])


def main():
    logging.info("Running main function")
    initialize_database()
    lemmy = initialize_lemmy_instance()
    community_id = discover_community(lemmy, LEMMY_COMMUNITY_NAME)
    for repo in REPOSITORIES:
        post_issues_to_lemmy(lemmy, community_id, repo)
        issue_data = fetch_issue_data(repo)
        for github_url, lemmy_post_id in issue_data:
            issue_number = extract_issue_number
            post_comments_to_lemmy(lemmy, lemmy_post_id, repo, issue_number)


def run_periodically():
    main()
    schedule.every(2).hours.do(main)

    while True:
        schedule.run_pending()
        time.sleep(60)


if __name__ == "__main__":
    logging.info("Starting script")
    run_periodically()
636
11
637
30
submitted 3 years ago* (last edited 3 years ago) by GodOfThunder@lemm.ee to c/python@programming.dev

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?

638
10

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.

639
6

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!

640
9

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.

641
14
submitted 3 years ago by Leon@lemm.ee to c/python@programming.dev
642
26
643
14

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

644
2

Mypy is a type checker for python.

645
10
646
14
647
-10

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.

648
36
649
25
650
11
submitted 3 years ago* (last edited 3 years ago) by const_void@lemmy.world to c/python@programming.dev

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

view more: ‹ prev next ›

Python

8022 readers
2 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 3 years ago
MODERATORS