mirror of
https://github.com/evennia/evennia.git
synced 2026-03-16 21:06:30 +01:00
issue #2243 -- prefer f-strings over %-interpolation
edited docs to prefer f-strings, then str.format(), and remove %-interpolation additional ad-hoc documentation fixes, as opportunities seen: - replace Built-In Function (BIF) "min" variable with "mins" - prefer BIF str(var) over "%s" % var - reformat some code examples to clarify multiple args passed to functions - change some single-quote strings to double-quotes for consistency - fix mismatched parens misc edits: - add .vscode/ to gitignore
This commit is contained in:
parent
f45051050e
commit
851ca30be5
30 changed files with 207 additions and 193 deletions
|
|
@ -91,13 +91,12 @@ Below is a version of the example file found in `evennia/contrib/tutorial_exampl
|
|||
table = create_object(Object, key="Blue Table", location=limbo)
|
||||
chair = create_object(Object, key="Blue Chair", location=limbo)
|
||||
|
||||
string = "A %s and %s were created."
|
||||
string = f"A {table} and {chair} were created."
|
||||
if DEBUG:
|
||||
table.delete()
|
||||
chair.delete()
|
||||
string += " Since debug was active, " \
|
||||
"they were deleted again."
|
||||
caller.msg(string % (table, chair))
|
||||
string += " Since debug was active, they were deleted again."
|
||||
caller.msg(string)
|
||||
```
|
||||
|
||||
This uses Evennia's Python API to create three objects in sequence.
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ Here is a minimalistic command with no custom parsing:
|
|||
|
||||
def func(self):
|
||||
# echo the caller's input back to the caller
|
||||
self.caller.msg("Echo: {}".format(self.args)
|
||||
self.caller.msg(f"Echo: {self.args}")
|
||||
|
||||
```
|
||||
|
||||
|
|
@ -548,7 +548,7 @@ class CmdTestID(Command):
|
|||
self.xval = 0
|
||||
self.xval += 1
|
||||
|
||||
self.caller.msg("Command memory ID: {} (xval={})".format(id(self), self.xval))
|
||||
self.caller.msg(f"Command memory ID: {id(self)} (xval={self.xval})")
|
||||
|
||||
```
|
||||
|
||||
|
|
@ -651,7 +651,7 @@ thus do so asynchronously, using callbacks.
|
|||
```python
|
||||
# in command class func()
|
||||
def callback(ret, caller):
|
||||
caller.msg("Returned is %s" % ret)
|
||||
caller.msg(f"Returned is {ret}")
|
||||
deferred = self.execute_command("longrunning")
|
||||
deferred.addCallback(callback, self.caller)
|
||||
```
|
||||
|
|
|
|||
|
|
@ -58,7 +58,7 @@ class CmdSetTestAttr(Command):
|
|||
def quit(caller):
|
||||
"Since we define it, we must handle messages"
|
||||
caller.msg("Editor exited")
|
||||
key = "%s/test" % self.caller
|
||||
key = f"{self.caller}/test"
|
||||
# launch the editor
|
||||
eveditor.EvEditor(self.caller,
|
||||
loadfunc=load, savefunc=save, quitfunc=quit,
|
||||
|
|
@ -100,7 +100,7 @@ class CmdSetTestAttr(Command):
|
|||
key = "settestattr"
|
||||
def func(self):
|
||||
"Set up the callbacks and launch the editor"
|
||||
key = "%s/test" % self.caller
|
||||
key = f"{self.caller}/test"
|
||||
# launch the editor
|
||||
eveditor.EvEditor(self.caller,
|
||||
loadfunc=load, savefunc=save, quitfunc=quit,
|
||||
|
|
|
|||
|
|
@ -531,10 +531,10 @@ def _set_attribute(caller, raw_string, **kwargs):
|
|||
|
||||
def node_background(caller):
|
||||
text = \
|
||||
"""
|
||||
{} experienced a traumatic event
|
||||
f"""
|
||||
{caller.key} experienced a traumatic event
|
||||
in their childhood. What was it?
|
||||
""".format(caller.key}
|
||||
"""
|
||||
|
||||
options = ({"key": "death",
|
||||
"desc": "A violent death in the family",
|
||||
|
|
@ -580,7 +580,7 @@ def _set_name(caller, raw_string, **kwargs):
|
|||
# a blank input either means OK or Abort
|
||||
if prev_entry:
|
||||
caller.key = prev_entry
|
||||
caller.msg("Set name to {}.".format(prev_entry))
|
||||
caller.msg(f"Set name to {prev_entry}.")
|
||||
return "node_background"
|
||||
else:
|
||||
caller.msg("Aborted.")
|
||||
|
|
@ -644,7 +644,7 @@ def _set_name(caller, raw_string, **kwargs):
|
|||
|
||||
caller.ndb._menutree.charactersheet = {}
|
||||
caller.ndb._menutree.charactersheet['name'] = raw_string
|
||||
caller.msg("You set your name to {}".format(raw_string)
|
||||
caller.msg(f"You set your name to {raw_string}")
|
||||
return "background"
|
||||
|
||||
def node_set_name(caller):
|
||||
|
|
@ -658,7 +658,7 @@ def node_set_name(caller):
|
|||
|
||||
|
||||
def node_view_sheet(caller):
|
||||
text = "Character sheet:\n {}".format(self.ndb._menutree.charactersheet)
|
||||
text = f"Character sheet:\n {self.ndb._menutree.charactersheet}"
|
||||
|
||||
options = ({"key": "Accept",
|
||||
"goto": "finish_chargen"},
|
||||
|
|
|
|||
|
|
@ -190,7 +190,7 @@ The above could for example be used in a lock function like this:
|
|||
|
||||
```python
|
||||
# we have `obj` and `owner_object` from before
|
||||
obj.locks.add("edit: id(%i)" % owner_object.id)
|
||||
obj.locks.add(f"edit: id({owner_object.id})")
|
||||
```
|
||||
|
||||
We could check if the "edit" lock is passed with something like this:
|
||||
|
|
|
|||
|
|
@ -50,9 +50,7 @@ def _monitor_callback(fieldname="", obj=None, **kwargs):
|
|||
new_value = getattr(obj, fieldname)
|
||||
else: # an attribute
|
||||
new_value = obj.attributes.get(fieldname)
|
||||
|
||||
obj.msg("%s.%s changed to '%s'." % \
|
||||
(obj.key, fieldname, new_value))
|
||||
obj.msg(f"{obj.key}.{fieldname} changed to '{new_value}'.")
|
||||
|
||||
# (we could add _some_other_monitor_callback here too)
|
||||
|
||||
|
|
|
|||
|
|
@ -176,7 +176,7 @@ def red(*args, **kwargs):
|
|||
"""
|
||||
if not args or len(args) > 1:
|
||||
raise ValueError("Must have one argument, the text to color red!")
|
||||
return "|r{}|n".format(args[0])
|
||||
return f"|r{args[0]}|n"
|
||||
```
|
||||
|
||||
> Note that we must make sure to validate input and raise `ValueError` if that fails. Also, it is
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue