Skip Navigation

Search

Python @programming.dev
Ms. ArmoredThirteen @lemmy.zip

Using markdown library to convert to html pages isn't formatting how I would like it to

Hello if anyone knows of a way to get python-markdown to behave in the way I'd like, or of an alternative way to do it, I'd love some help! My use case is I'm converting .md files made with Obsidian into html files. Obsidian has tags that are a pound sign followed by the tag (so like "#TagName"). When the tag is the first item on a line the pound sign is confused for a heading, even though there is no space after it.

Is there a way that I can avoid this so it only reads it as a heading if there is a space between the pound and the next word? I'm even considering some kind of find/replace logic so I can swap it out with like a link to a page that lists all the pages with that tag or something that gets run before the markdown to html conversion.

Edit: The solution I'm going for is a regex find/replace. Currently the string pattern looks like "#[^\s#][^\s" + string.punctuation + "#]*" which can find tags but ignores headers. Since the ultimate goal is to have the tags link to a tag p

Python @programming.dev
thingsiplay @beehaw.org

Subclassing pathlib.PosixPath broken since Python 3.12 (actually its fixed, but workaround broken)

Solved: Thanks to a user with this reply: https://programming.dev/comment/10034690


cross-posted from: https://beehaw.org/post/13901165

Hi all. I have a little problem and don't know how to solve. A CLI program in Python is broken since Python 3.12. It was working in Python 3.11. The reason is, that Python 3.12 changed how subclassing of a pathlib.Path works (basically fixed an issue), which now breaks a workaround.

The class in question is:

 python
    
class File(PosixPath):
    def __new__(cls, *args: Any, **kwargs: Any) -> Any:
        return cls._from_parts(args).expanduser().resolve()  # type: ignore

    def __init__(self, source: str | Path, *args: Any) -> None:
        super().__init__()
        self.__source = Path(source)

    @property
    def source(self) -> Path:
        return self.__source

    @property
    def modified(self) -> Time:
        return Time.fromtimestamp(os.path.getmtime(self))

    @property
    d
  
Python @programming.dev
yo_scottie_oh @lemmy.ml

Why won't my Python unit tests run?

Hello! I'm attempting to follow some tutorials on unit testing with Python. One of them is a video tutorial Unit Tests in Python on the Socratica channel. Everyone in the comments seems to be making out just fine, and I’m following the instructor’s directions to the letter, yet I get a different result. It’s driving me mad lol.

In the video, the instructor creates two text files, one called circles.py in which she defines a function circle_area(r), and another called test_circles.py in which she writes some unit tests. In my attempt to follow along, I've ended up with two files structured like so:

 undefined
    
/home/yo_scottie_oh/Projects/PythonTutorials/Socratica/Circles
├── circles.py
└── test_circles.py

  

circles.py:

 undefined
    
from math import pi

def circle_area(r):
   return pi*(r**2)

# Test function
radii = [2, 0, -3, 2 + 5j, True, "radius"]
message = "Area of circles with r = {radius} is {area}."

for r in radii:
   A = circle_area(r)
   print(mes
  
Python @programming.dev
driving_crooner @lemmy.eco.br

how to send the icecream output to a log file?

Hi, I'm currently using this to log my python process

logging.basicConfig(filename='filename.log', level=logging.DEBUG)

logger = logging.getLogger()

sys.stderr.write = logger.error

sys.stdout.write = logger.info

And then using print(f'{datetime.now()} log message') where I want to log.

It's working OK, buy I would like to live it to ic, but can't find any info on how to send the ic output to the logger.

Thanks for any help.

Python @programming.dev
crunchpaste @lemmy.dbzer0.com

Is there any way to run a Python GTK3 apps on Windows?

Edit: The key was using msys2. After installing Gtk3 and PyGObject following the PyGObject guide for msys2 everything worked fine. Packaging with PyInstaller and nuitka was fine.

I've been developing an image halftoning app for a while using GTK on Linux, thinking GTK is cross platform, and delaying testing it on Windows for quite some time.

Today I decided to finally install Windows 10 (for the first time in more than a decade) on a separate machine and see what works and what does not.

For the past few hours I've been through hell trying to get GTK3 to work. I've followed multiple guides, none of which worked, including wingtk.

Furthermore, even if I successfully compile (or install) GTK, would it be possible to package it all up using something like PyInstaller or nuitka.

At this point I'm thinking of keeping the functions and writing a whole ne

Python @programming.dev
GodOfThunder @lemm.ee

How would you debug this script without creating many posts?

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.

 python
    
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 INTEGE