python: Replace silly uses of filter().

The test_management_commands use in particular was causing pickling
errors when the test failed, because Python 3 filter returns an
iterator, not a list.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2020-06-09 22:28:15 -07:00 committed by Anders Kaseorg
parent 5c9d56d2f7
commit d3e8af4ad2
4 changed files with 16 additions and 23 deletions

View File

@ -42,10 +42,7 @@ def check_html_templates(templates: Iterable[str], all_dups: bool, fix: bool) ->
# there are also cases where Casper deliberately uses invalid HTML, # there are also cases where Casper deliberately uses invalid HTML,
# so we exclude them from our linter. # so we exclude them from our linter.
logging.basicConfig(format='%(levelname)s:%(message)s') logging.basicConfig(format='%(levelname)s:%(message)s')
templates = filter( templates = sorted(fn for fn in templates if 'casperjs' not in fn)
lambda fn: ('casperjs' not in fn),
templates)
templates = sorted(list(templates))
# Use of underscore templates <%= %>. # Use of underscore templates <%= %>.
if 'templates/zerver/team.html' in templates: if 'templates/zerver/team.html' in templates:
templates.remove('templates/zerver/team.html') templates.remove('templates/zerver/team.html')
@ -99,12 +96,8 @@ def check_html_templates(templates: Iterable[str], all_dups: bool, fix: bool) ->
return bad_ids_dict return bad_ids_dict
bad_ids_list: List[str] = [] bad_ids_list: List[str] = []
archive_templates = list(filter( archive_templates = [fn for fn in templates if 'templates/zerver/archive' in fn]
lambda fn: ('templates/zerver/archive' in fn), templates = [fn for fn in templates if 'templates/zerver/archive' not in fn]
templates))
templates = list(filter(
lambda fn: ('templates/zerver/archive' not in fn),
templates))
bad_ids_list += list(check_for_duplicate_ids(archive_templates).keys()) bad_ids_list += list(check_for_duplicate_ids(archive_templates).keys())
bad_ids_list += list(check_for_duplicate_ids(templates).keys()) bad_ids_list += list(check_for_duplicate_ids(templates).keys())

View File

@ -497,10 +497,10 @@ def get_mobile_push_content(rendered_content: str) -> str:
return elem.text or '' return elem.text or ''
def format_as_quote(quote_text: str) -> str: def format_as_quote(quote_text: str) -> str:
quote_text_list = filter(None, quote_text.split('\n')) # Remove empty lines return "".join(
quote_text = '\n'.join(map(lambda x: "> "+x, quote_text_list)) f"> {line}\n" for line in quote_text.splitlines()
quote_text += '\n' if line # Remove empty lines
return quote_text )
def render_olist(ol: lxml.html.HtmlElement) -> str: def render_olist(ol: lxml.html.HtmlElement) -> str:
items = [] items = []

View File

@ -297,8 +297,9 @@ def send_custom_email(users: List[UserProfile], options: Dict[str, Any]) -> None
inline_template(email_filename) inline_template(email_filename)
# Finally, we send the actual emails. # Finally, we send the actual emails.
for user_profile in filter(lambda user: for user_profile in users:
not options.get('admins_only') or user.is_realm_admin, users): if options.get('admins_only') and not user_profile.is_realm_admin:
continue
context = { context = {
'realm_uri': user_profile.realm.uri, 'realm_uri': user_profile.realm.uri,
'realm_name': user_profile.realm.name, 'realm_name': user_profile.realm.name,

View File

@ -174,13 +174,12 @@ class TestCommandsCanStart(TestCase):
def setUp(self) -> None: def setUp(self) -> None:
super().setUp() super().setUp()
self.commands = filter( self.commands = [
lambda filename: filename != '__init__', command
map( for filename in glob.iglob('*/management/commands/*.py')
lambda file: os.path.basename(file).replace('.py', ''), for command in [os.path.basename(filename).replace('.py', '')]
glob.iglob('*/management/commands/*.py') if command != '__init__'
) ]
)
@slow("Aggregate of runs dozens of individual --help tests") @slow("Aggregate of runs dozens of individual --help tests")
def test_management_commands_show_help(self) -> None: def test_management_commands_show_help(self) -> None: