SubredditRules¶
- class praw.models.reddit.rules.SubredditRules(subreddit)¶
Provide a set of functions to access a
Subreddit’s rules.For example, to list all the rules for a subreddit:
for rule in reddit.subreddit("test").rules: print(rule)
Moderators can also add rules to the subreddit. For example, to make a rule called
"No spam"in r/test:reddit.subreddit("test").rules.mod.add( short_name="No spam", kind="all", description="Do not spam. Spam bad" )
- Parameters:
subreddit (models.Subreddit)
- __getitem__(short_name)¶
Return the
Rulefor the subreddit with short_nameshort_name.- Parameters:
short_name (
SupportsIndex) – The short_name of the rule, or the rule number.- Return type:
Note
Rules fetched using a specific rule name are lazily loaded, so you might have to access an attribute to get all the expected attributes.
This method is to be used to fetch a specific rule, like so:
rule_name = "No spam" rule = reddit.subreddit("test").rules[rule_name] print(rule)
You can also fetch a numbered rule of a subreddit.
Rule numbers start at
0, so the first rule is at index0, and the second rule is at index1, and so on.- Raises:
IndexErrorif a rule of a specific number does not exist.- Parameters:
short_name (SupportsIndex)
- Return type:
Note
You can use negative indexes, such as
-1, to get the last rule. You can also use slices, to get a subset of rules, such as the last three rules withrules[-3:].For example, to fetch the second rule of r/test:
rule = reddit.subreddit("test").rules[1]
To get the last three rules in a subreddit:
reasons = reddit.subreddit("test").rules[-3:] for rule in rules: print(rule)
- __init__(subreddit)¶
Initialize a
SubredditRulesinstance.- Parameters:
subreddit (
Subreddit) – The subreddit whose rules to work with.- Return type:
None
- __iter__()¶
Iterate through the rules of the subreddit.
This method is used to discover all rules for a subreddit.
For example, to get the rules for r/test:
for rule in reddit.subreddit("test").rules: print(rule)
- mod()¶
Contain methods to moderate subreddit rules as a whole.
To add rule
"No spam"to r/test try:reddit.subreddit("test").rules.mod.add( short_name="No spam", kind="all", description="Do not spam. Spam bad" )
To move the fourth rule to the first position, and then to move the prior first rule to where the third rule originally was in r/test:
subreddit = reddit.subreddit("test") rules = list(subreddit.rules) new_rules = rules[3:4] + rules[1:3] + rules[0:1] + rules[4:] # Alternate: [rules[3]] + rules[1:3] + [rules[0]] + rules[4:] new_rule_list = subreddit.rules.mod.reorder(new_rules)
- Return type: