[V3/Config] New config methods #2185

Closed
Redjumpman wants to merge 7 commits from V3/develop into V3/feature/config_methods
Redjumpman commented 2018-10-06 06:11:31 +02:00 (Migrated from github.com)

Type

  • Bugfix
  • Enhancement
  • New feature

Description of the changes

This adds some quality of life methods to config. The two new operations are inc and toggle. These work like set, but for specific value types. They also return the new value. This aims to solve repetition in calling.

Examples for Incrementing

# Add 3 to a number
new = await config.foo.inc(3)
await ctx.send(f"Incremented foo from {new - 3} to {new}.")

# Subtract 5
new = await config.foo.inc(-5)
await ctx.send(f"Incremented foo from {new - 5} to {new}.")

Example for Toggle

new = await config.foo.toggle()
await ctx.send(f"Changed foo from {not new} to {new}.")

Example of doing this without these methods

current = await config.foo()
await config.foo.set(value=(not current))
await ctx.send(f"Changed foo from {current} to {not current}.")

I included some tests, and this is working with the both the json and mongo drivers. This shouldn't break any current usage of config.

DISCLAIMER
I'm setting this up to be implemented in 3.1 and not prior to release.

### Type - [ ] Bugfix - [ ] Enhancement - [X] New feature ### Description of the changes This adds some quality of life methods to config. The two new operations are `inc` and `toggle`. These work like `set`, but for specific value types. They also return the new value. This aims to solve repetition in calling. **Examples for Incrementing** ```py # Add 3 to a number new = await config.foo.inc(3) await ctx.send(f"Incremented foo from {new - 3} to {new}.") # Subtract 5 new = await config.foo.inc(-5) await ctx.send(f"Incremented foo from {new - 5} to {new}.") ``` **Example for Toggle** ```py new = await config.foo.toggle() await ctx.send(f"Changed foo from {not new} to {new}.") ``` **Example of doing this without these methods** ```py current = await config.foo() await config.foo.set(value=(not current)) await ctx.send(f"Changed foo from {current} to {not current}.") ``` I included some tests, and this is working with the both the json and mongo drivers. This shouldn't break any current usage of config. **DISCLAIMER** I'm setting this up to be implemented in 3.1 and not prior to release.
Twentysix26 (Migrated from github.com) reviewed 2018-10-06 06:11:31 +02:00
Tobotimus (Migrated from github.com) reviewed 2018-10-08 11:32:58 +02:00
Tobotimus (Migrated from github.com) left a comment

Thanks for this PR, these new methods are awesome 👍 All I found from browsing the code was 1 thing, I'll do more local tests in the next few days 😃

Once I've approved this I'll merge it into a feature branch as you suggested to me earlier.

Thanks for this PR, these new methods are awesome :+1: All I found from browsing the code was 1 thing, I'll do more local tests in the next few days :smiley: Once I've approved this I'll merge it into a feature branch as you suggested to me earlier.
@ -388,0 +416,4 @@
with pytest.raises(TypeError):
await config.foo.inc(10)
with pytest.raises(TypeError):
await config.baz.inc(10)
Tobotimus (Migrated from github.com) commented 2018-10-08 11:28:49 +02:00

These two operations should be in separate with statements; pytest.raises will catch the first exception then exit the context manager

These two operations should be in separate `with` statements; `pytest.raises` will catch the first exception then exit the context manager
Redjumpman (Migrated from github.com) reviewed 2018-10-08 15:34:04 +02:00
@ -388,0 +416,4 @@
with pytest.raises(TypeError):
await config.foo.inc(10)
with pytest.raises(TypeError):
await config.baz.inc(10)
Redjumpman (Migrated from github.com) commented 2018-10-08 15:34:04 +02:00

Done.

Done.
Redjumpman commented 2018-10-18 05:47:14 +02:00 (Migrated from github.com)

Anything left for me to do on this?

Anything left for me to do on this?
mikeshardmind commented 2018-12-12 05:12:17 +01:00 (Migrated from github.com)

The only additional thing I'd request is on the subtraction/increment operators, allow a conditional to be passed for a minimum start/end value, which would raise an error and not commit the change.

This would allow some inlining of single access to those types while still keeping validation for the result.

The only additional thing I'd request is on the subtraction/increment operators, allow a conditional to be passed for a minimum start/end value, which would raise an error and not commit the change. This would allow some inlining of single access to those types while still keeping validation for the result.
Tobotimus (Migrated from github.com) requested changes 2019-02-08 01:51:41 +01:00
Tobotimus (Migrated from github.com) left a comment

The Mongo driver with these new methods is making 3 queries for each inc/toggle (except for one case). This is actually more than we would otherwise use for a simple get and set.

Because of the way we store data in mongo, the use of the $inc and $bit operators isn't worth it. It would make more sense if each member/user/channel/guild etc.'s data included all defaults on initial set, but unfortunately that simply isn't the case.

The Mongo driver with these new methods is making 3 queries for each inc/toggle (except for one case). This is actually more than we would otherwise use for a simple get and set. Because of the way we store data in mongo, the use of the `$inc` and `$bit` operators isn't worth it. It would make more sense if each member/user/channel/guild etc.'s data included all defaults on initial set, but unfortunately that simply isn't the case.
@ -154,0 +179,4 @@
"""
if isinstance(self.default, bool) or not isinstance(self.default, (int, float)):
raise TypeError("Config inc method only supports integer or float values.")
Tobotimus (Migrated from github.com) commented 2019-02-08 01:39:12 +01:00

Might also want to say that the method only supports registered integer or float values

Might also want to say that the method only supports *registered* integer or float values
@ -154,0 +204,4 @@
"""
if not isinstance(self.default, bool):
raise TypeError("Config toggle method can only toggle Boolean values.")
Tobotimus (Migrated from github.com) commented 2019-02-08 01:39:23 +01:00

Same here

Same here
tekulvw (Migrated from github.com) requested changes 2019-04-03 05:01:27 +02:00
tekulvw (Migrated from github.com) left a comment

The Base driver class should also be updated with these new methods. Mongo driver will be updated as necessary in #2536

The `Base` driver class should also be updated with these new methods. Mongo driver will be updated as necessary in #2536
tekulvw commented 2019-04-30 22:07:42 +02:00 (Migrated from github.com)

Superseded by #2635 due to the underlying driver changes and addition of MongoDBV2

Superseded by #2635 due to the underlying driver changes and addition of MongoDBV2

Pull request closed

Sign in to join this conversation.
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!2185
No description provided.