• 18 Posts
  • 16 Comments
Joined 1 year ago
cake
Cake day: July 10th, 2023

help-circle







  • Edward Snowden, a former National Security Agency contractor and government whistleblower, has been credited with the quote “Arguing that you don’t care about the right to privacy because you have nothing to hide is no different than saying you don’t care about free speech because you have nothing to say”. Snowden has argued that privacy is a fundamental right and that without it, individuals cannot have anything for themselves. The “nothing to hide” argument has been used to defend the collection and use of government data beyond surveillance and disclosure, but critics argue that it is inherently paradoxical and that what is hidden is not necessarily relevant. Snowden has also stated that the burden of justification falls on those seeking to infringe upon human rights, and that nobody needs to justify why they “need” a right.











  • A function decorator: You can create a decorator that handles the connection and cursor creation and passes the cursor to the decorated function.

    import sqlite3
    from functools import wraps
    
    DB_FILE = "your_database_file.db"
    
    def with_cursor(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            conn = sqlite3.connect(DB_FILE)
            cursor = conn.cursor()
    
            result = func(cursor, *args, **kwargs)
    
            conn.commit()
            cursor.close()
            conn.close()
    
            return result
    
        return wrapper
    
    @with_cursor
    def insert_post_to_db(cursor: sqlite3.Cursor, issue: Issue, lemmy_post_id: int) -> None:
        cursor.execute(
            "INSERT INTO posts (issue_url, lemmy_post_id, issue_title, issue_body) VALUES (?, ?, ?, ?)",
            (issue.url, lemmy_post_id, issue.title, issue.formatted_body),
        )
    

  • A context manager: Create a context manager that handles the connection and cursor creation, as well as closing the connection when done. This way, you can use the with statement to manage the connection and cursor in your functions.

    import sqlite3
    
    DB_FILE = "your_database_file.db"
    
    class DatabaseConnection:
        def __enter__(self):
            self.conn = sqlite3.connect(DB_FILE)
            self.cursor = self.conn.cursor()
            return self.cursor
    
        def __exit__(self, exc_type, exc_val, exc_tb):
            self.conn.commit()
            self.cursor.close()
            self.conn.close()
    
    def insert_post_to_db(issue: Issue, lemmy_post_id: int) -> None:
        with DatabaseConnection() as cursor:
            cursor.execute(
                "INSERT INTO posts (issue_url, lemmy_post_id, issue_title, issue_body) VALUES (?, ?, ?, ?)",
                (issue.url, lemmy_post_id, issue.title, issue.formatted_body),
            )
    




  • Github style:

    > If you need to find a post, don't use search engines or indexers, use lemmy's own search page, which allows you to filter by community if you wish. Its been a concious decision from the beginning to not favor SEO over simple urls.
    > 
    > *Originally posted by @dessalines in [#839 (comment)](https://github.com/LemmyNet/lemmy-ui/issues/839#issuecomment-1304368786)*
    

    Result:

    If you need to find a post, don’t use search engines or indexers, use lemmy’s own search page, which allows you to filter by community if you wish. Its been a concious decision from the beginning to not favor SEO over simple urls.

    Originally posted by @dessalines in #839 (comment)