Update list_to_string to handle generators. Resolve #2120

This commit is contained in:
Griatch 2020-06-28 11:09:59 +02:00
parent c8b056d06d
commit a848960819
2 changed files with 20 additions and 10 deletions

View file

@ -58,6 +58,9 @@ without arguments starts a full interactive Python console.
required by Django. required by Django.
- Fixes to `spawn`, make updating an existing prototype/object work better. Add `/raw` switch - Fixes to `spawn`, make updating an existing prototype/object work better. Add `/raw` switch
to `spawn` command to extract the raw prototype dict for manual editing. to `spawn` command to extract the raw prototype dict for manual editing.
- `list_to_string` is now `iter_to_string` (but old name still works as legacy alias). It will
now accept any input, including generators and single values.
## Evennia 0.9 (2018-2019) ## Evennia 0.9 (2018-2019)

View file

@ -9,6 +9,7 @@ be of use when designing your own game.
import os import os
import gc import gc
import sys import sys
import copy
import types import types
import math import math
import re import re
@ -342,14 +343,16 @@ def columnize(string, columns=2, spacing=4, align="l", width=None):
return "\n".join(rows) return "\n".join(rows)
def list_to_string(inlist, endsep="and", addquote=False): def iter_to_string(initer, endsep="and", addquote=False):
""" """
This pretty-formats a list as string output, adding an optional This pretty-formats an iterable list as string output, adding an optional
alternative separator to the second to last entry. If `addquote` alternative separator to the second to last entry. If `addquote`
is `True`, the outgoing strings will be surrounded by quotes. is `True`, the outgoing strings will be surrounded by quotes.
Args: Args:
inlist (list): The list to print. initer (any): Usually an iterable to print. Each element must be possible to
present with a string. Note that if this is a generator, it will be
consumed by this operation.
endsep (str, optional): If set, the last item separator will endsep (str, optional): If set, the last item separator will
be replaced with this value. be replaced with this value.
addquote (bool, optional): This will surround all outgoing addquote (bool, optional): This will surround all outgoing
@ -374,16 +377,20 @@ def list_to_string(inlist, endsep="and", addquote=False):
endsep = "," endsep = ","
else: else:
endsep = " " + endsep endsep = " " + endsep
if not inlist: if not initer:
return "" return ""
initer = tuple(str(val) for val in make_iter(initer))
if addquote: if addquote:
if len(inlist) == 1: if len(initer) == 1:
return '"%s"' % inlist[0] return '"%s"' % initer[0]
return ", ".join('"%s"' % v for v in inlist[:-1]) + "%s %s" % (endsep, '"%s"' % inlist[-1]) return ", ".join('"%s"' % v for v in initer[:-1]) + "%s %s" % (endsep, '"%s"' % initer[-1])
else: else:
if len(inlist) == 1: if len(initer) == 1:
return str(inlist[0]) return str(initer[0])
return ", ".join(str(v) for v in inlist[:-1]) + "%s %s" % (endsep, inlist[-1]) return ", ".join(str(v) for v in initer[:-1]) + "%s %s" % (endsep, initer[-1])
# legacy alias
list_to_string = iter_to_string
def wildcard_to_regexp(instring): def wildcard_to_regexp(instring):