Async PRAW

repository·main·Indexed 41 days ago

https://github.com/praw-dev/asyncpraw

An asynchronous Python wrapper for the Reddit API designed for non-blocking access to Reddit's features. It automatically handles API rate limits and rules, providing a `Reddit` client class to interact with subreddits, submissions, comments, and other entities. The library includes a `Config` system for managing credentials via keyword arguments, environment variables, or `praw.ini` files, and a comprehensive exception hierarchy including `AsyncPRAWException`, `RedditAPIException`, and `ClientException`.

Tokens
30.3K
Snippets
116
Records
181
Agent score
100%

What's inside Async PRAW

  1. Use the Preferences model in asyncpraw

    main
    The asyncpraw.models.Preferences class is used to manage user-specific settings and preferences within the Reddit API. It allows you to interact with and modify settings that govern how your Reddit account behaves or how the API returns certain data. Because this is part of the asyncpraw models, interactions with this object are asynchronous.
  2. Understand Lazy Loading vs. Full Fetching in Async PRAW

    main

    A major change in Async PRAW is how objects are initialized. In the original PRAW, most objects were lazily loaded (fetched only when an attribute was accessed). In Async PRAW, many objects are now fully fetched upon initialization by default.

    Default Fetching Behavior

    Objects that are now fully fetched on initialization:

    • .Collection
    • .Comment
    • .Emoji
    • .LiveUpdate
    • .Preferences
    • .RemovalReason
    • .Rule
    • .Submission
    • .WikiPage

    Objects that remain lazily loaded by default:

    • .LiveThread
    • .Multireddit
    • .Redditor
    • .Subreddit

    Controlling Fetch Behavior

    • To lazily load a fully-fetched object (like a .Submission), pass fetch=False to its helper method.
    • To fully load a lazily-loaded object (like a .Subreddit), pass fetch=True to its helper method.
    # Async PRAW: network request made and object is fully loaded
    submission = await reddit.submission("id")
    print(submission.score)
    
    # Async PRAW: lazily load a Submission to avoid immediate network request
    submission = await reddit.submission("id", fetch=False)
    # ... later ...
    await submission.load()
    print(submission.score)
  3. How CommentForest iteration works in v8

    main

    In v8, CommentForest.list() no longer needs to be awaited; it returns the list directly. Additionally, CommentForest can no longer be used as an asynchronous iterator. Instead, iterate over the result of the .list() method using a standard for loop.

    # Old
    submission = await reddit.submission("id")
    comments = await submission.comments.list()
    
    # New
    submission = await reddit.submission("id")
    comments = submission.comments.list()
    
    for comment in submission.comments.list():
        ...
  4. Follow Async PRAW specific style guidelines

    main

    When contributing code, adhere to these specific organizational rules:

    • Class Sorting: Sort classes alphabetically within a file where inheritance allows.
    • Method Sorting: Within a class, sort methods alphabetically within these specific groups (in order):
      1. Static methods
      2. Class methods
      3. Cached properties
      4. Properties
      5. Instance Methods
    • Argument Naming: Use descriptive names for catch-all keyword arguments (e.g., **other_options) instead of **kwargs.
    • Keyword-Only Arguments: Methods with multiple arguments should generally have them sorted alphabetically and marked as keyword-only using the * separator.
      • Exception: Mandatory positional arguments can remain positional if their purpose is obvious (e.g., def pair(self, left, right)).
      • Exception: If there are 1-2 mandatory arguments and several optional ones, mandatory arguments can be positional, but optional arguments must be keyword-only and sorted alphabetically.
    class ExampleClass:
        async def example_method(
            self,
            *,
            arg1,
            arg2,
            optional_arg1=None,
        ): ...
    
    class Subreddit:
        async def submit(
            self,
            title,
            *,
            collection_id=None,
            discussion_type=None,
            # ... other arguments sorted alphabetically
        ): ...
  5. Access subreddit resources via reddit.subreddit

    main

    In asyncpraw, you can access a specific subreddit instance using the reddit.subreddit(name) method. This returns a SubredditHelper object, which serves as the entry point for interacting with a subreddit's content, such as its submissions, comments, and moderator listings. Most operations on a subreddit are asynchronous and must be awaited.

    # Example of accessing a subreddit
    subreddit = await reddit.subreddit("test")
  6. Use PartialRedditor for lightweight user data

    main
    The PartialRedditor class is a lightweight version of the standard Redditor object. It is used when you only need access to a subset of a user's data or when you want to avoid the overhead of a full Redditor object. It typically contains basic information like the username and potentially some cached attributes, but lacks the full suite of methods and data available to a standard Redditor instance.
  7. Use SubredditCollections to access collections of subreddits

    main
    The SubredditCollections class in asyncpraw.models.reddit.collections provides access to various collections of subreddits available via the Reddit API. This class is typically accessed through a Reddit instance or a Subreddit instance, allowing you to iterate over or retrieve specific groups of subreddits (such as popular, growing, or top subreddits) asynchronously.
  8. Configure Submission Fetch Parameters in Async PRAW 8

    main

    Because a .Submission is fetched upon initialization, attributes like comment_sort, comment_limit, and methods like .add_fetch_param() must be set before the submission is fetched.

    To do this, initialize the submission with fetch=False, configure your parameters, and then call .load(). Setting these after the initial fetch will now raise a .ClientException instead of a warning.

    # Correct way to set fetch parameters
    submission = await reddit.submission("id", fetch=False)
    submission.comment_sort = "new"
    await submission.load()
  9. Use Modmail to manage community messages

    main

    The asyncpraw.models.reddit.subreddit.Modmail class provides an interface for interacting with Reddit's Modmail system. It allows moderators to view, send, and manage messages sent to a subreddit's moderation queue.

    Note: Accessing Modmail features requires appropriate moderator permissions for the subreddit in question.

  10. Discovering available attributes on Async PRAW objects

    main
    Async PRAW dynamically provides attributes based on the data returned by the Reddit API. Because Reddit's API response structure can change, the library does not maintain a static, exhaustive list of all possible attributes. To find out which attributes are currently available on a specific object (such as a Submission, Comment, or Subreddit), you should use introspection techniques to inspect the object's properties at runtime.