Skip to content

Commit

Permalink
Fix up some Python2-isms using pyupgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
akx committed May 10, 2022
1 parent 128322c commit 53a74f1
Show file tree
Hide file tree
Showing 49 changed files with 47 additions and 106 deletions.
1 change: 0 additions & 1 deletion babel/__init__.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
babel
~~~~~
Expand Down
3 changes: 1 addition & 2 deletions babel/core.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
babel.core
~~~~~~~~~~
Expand Down Expand Up @@ -105,7 +104,7 @@ def __init__(self, identifier):
self.identifier = identifier


class Locale(object):
class Locale:
"""Representation of a specific locale.
>>> locale = Locale('en', 'US')
Expand Down
12 changes: 5 additions & 7 deletions babel/dates.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
babel.dates
~~~~~~~~~~~
Expand All @@ -16,7 +15,6 @@
:license: BSD, see LICENSE for more details.
"""

from __future__ import division

import re
import warnings
Expand Down Expand Up @@ -262,7 +260,7 @@ def get_next_timezone_transition(zone=None, dt=None):
)


class TimezoneTransition(object):
class TimezoneTransition:
"""A helper object that represents the return value from
:func:`get_next_timezone_transition`.
Expand Down Expand Up @@ -1207,7 +1205,7 @@ def parse_date(string, locale=LC_TIME, format='medium'):

indexes = [(year_idx, 'Y'), (month_idx, 'M'), (day_idx, 'D')]
indexes.sort()
indexes = dict([(item[1], idx) for idx, item in enumerate(indexes)])
indexes = {item[1]: idx for idx, item in enumerate(indexes)}

# FIXME: this currently only supports numbers, but should also support month
# names, both in the requested locale, and english
Expand Down Expand Up @@ -1253,7 +1251,7 @@ def parse_time(string, locale=LC_TIME, format='medium'):

indexes = [(hour_idx, 'H'), (min_idx, 'M'), (sec_idx, 'S')]
indexes.sort()
indexes = dict([(item[1], idx) for idx, item in enumerate(indexes)])
indexes = {item[1]: idx for idx, item in enumerate(indexes)}

# TODO: support time zones

Expand All @@ -1274,7 +1272,7 @@ def parse_time(string, locale=LC_TIME, format='medium'):
return time(hour, minute, second)


class DateTimePattern(object):
class DateTimePattern:

def __init__(self, pattern, format):
self.pattern = pattern
Expand All @@ -1299,7 +1297,7 @@ def apply(self, datetime, locale):
return self % DateTimeFormat(datetime, locale)


class DateTimeFormat(object):
class DateTimeFormat:

def __init__(self, value, locale):
assert isinstance(value, (date, datetime, time))
Expand Down
1 change: 0 additions & 1 deletion babel/languages.py
@@ -1,4 +1,3 @@
# -- encoding: UTF-8 --
from babel.core import get_global


Expand Down
1 change: 0 additions & 1 deletion babel/lists.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
babel.lists
~~~~~~~~~~~
Expand Down
3 changes: 1 addition & 2 deletions babel/localedata.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
babel.localedata
~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -184,7 +183,7 @@ def merge(dict1, dict2):
dict1[key] = val1


class Alias(object):
class Alias:
"""Representation of an alias in the locale data.
An alias is a value that refers to some other part of the locale data,
Expand Down
1 change: 0 additions & 1 deletion babel/localtime/__init__.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
babel.localtime
~~~~~~~~~~~~~~~
Expand Down
4 changes: 1 addition & 3 deletions babel/localtime/_unix.py
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
from __future__ import with_statement
import os
import re
import sys
Expand Down Expand Up @@ -107,7 +105,7 @@ def _get_localzone(_root='/'):
tzpath = os.path.join(_root, filename)
if not os.path.exists(tzpath):
continue
with open(tzpath, 'rt') as tzfile:
with open(tzpath) as tzfile:
for line in tzfile:
match = timezone_re.match(line)
if match is not None:
Expand Down
1 change: 0 additions & 1 deletion babel/messages/__init__.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
babel.messages
~~~~~~~~~~~~~~
Expand Down
11 changes: 5 additions & 6 deletions babel/messages/catalog.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
babel.messages.catalog
~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -69,7 +68,7 @@ def _parse_datetime_header(value):
return dt


class Message(object):
class Message:
"""Representation of a single message in a catalog."""

def __init__(self, id, string=u'', locations=(), flags=(), auto_comments=(),
Expand Down Expand Up @@ -227,7 +226,7 @@ class TranslationError(Exception):
#"""


class Catalog(object):
class Catalog:
"""Representation of a message catalog."""

def __init__(self, locale=None, domain=None, header_comment=DEFAULT_HEADER,
Expand Down Expand Up @@ -755,10 +754,10 @@ def update(self, template, no_fuzzy_matching=False, update_header_comment=False,
# Prepare for fuzzy matching
fuzzy_candidates = []
if not no_fuzzy_matching:
fuzzy_candidates = dict([
(self._key_for(msgid), messages[msgid].context)
fuzzy_candidates = {
self._key_for(msgid): messages[msgid].context
for msgid in messages if msgid and messages[msgid].string
])
}
fuzzy_matches = set()

def _merge(message, oldkey, newkey):
Expand Down
1 change: 0 additions & 1 deletion babel/messages/checkers.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
babel.messages.checkers
~~~~~~~~~~~~~~~~~~~~~~~
Expand Down
6 changes: 2 additions & 4 deletions babel/messages/extract.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
babel.messages.extract
~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -163,7 +162,7 @@ def extract_from_dir(
for filename in filenames:
filepath = os.path.join(root, filename).replace(os.sep, '/')

for message_tuple in check_and_call_extract_file(
yield from check_and_call_extract_file(
filepath,
method_map,
options_map,
Expand All @@ -172,8 +171,7 @@ def extract_from_dir(
comment_tags,
strip_comment_tags,
dirpath=absname,
):
yield message_tuple
)


def check_and_call_extract_file(filepath, method_map, options_map,
Expand Down
10 changes: 4 additions & 6 deletions babel/messages/frontend.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
babel.messages.frontend
~~~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -8,7 +7,6 @@
:copyright: (c) 2013-2022 by the Babel Team.
:license: BSD, see LICENSE for more details.
"""
from __future__ import print_function

import fnmatch
import logging
Expand Down Expand Up @@ -883,7 +881,7 @@ def run(self):
return


class CommandLineInterface(object):
class CommandLineInterface:
"""Command-line interface.
This class provides a simple command-line interface to the message
Expand Down Expand Up @@ -937,7 +935,7 @@ def run(self, argv=None):
self._configure_logging(options.loglevel)
if options.list_locales:
identifiers = localedata.locale_identifiers()
longest = max([len(identifier) for identifier in identifiers])
longest = max(len(identifier) for identifier in identifiers)
identifiers.sort()
format = u'%%-%ds %%s' % (longest + 1)
for identifier in identifiers:
Expand Down Expand Up @@ -974,7 +972,7 @@ def _configure_logging(self, loglevel):
def _help(self):
print(self.parser.format_help())
print("commands:")
longest = max([len(command) for command in self.commands])
longest = max(len(command) for command in self.commands)
format = " %%-%ds %%s" % max(8, longest + 1)
commands = sorted(self.commands.items())
for name, description in commands:
Expand Down Expand Up @@ -1094,7 +1092,7 @@ def parse_mapping(fileobj, filename=None):
if section == 'extractors':
extractors = dict(parser.items(section))
else:
method, pattern = [part.strip() for part in section.split(':', 1)]
method, pattern = (part.strip() for part in section.split(':', 1))
method_map.append((pattern, method))
options_map[pattern] = dict(parser.items(section))

Expand Down
1 change: 0 additions & 1 deletion babel/messages/jslexer.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
babel.messages.jslexer
~~~~~~~~~~~~~~~~~~~~~~
Expand Down
5 changes: 2 additions & 3 deletions babel/messages/mofile.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
babel.messages.mofile
~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -48,7 +47,7 @@ def read_mo(fileobj):
version, msgcount, origidx, transidx = unpack('>4I', buf[4:20])
ii = '>II'
else:
raise IOError(0, 'Bad magic number', filename)
raise OSError(0, 'Bad magic number', filename)

# Now put all messages from the .mo file buffer into the catalog
# dictionary
Expand All @@ -61,7 +60,7 @@ def read_mo(fileobj):
msg = buf[moff:mend]
tmsg = buf[toff:tend]
else:
raise IOError(0, 'File is corrupt', filename)
raise OSError(0, 'File is corrupt', filename)

# See if we're looking at GNU .mo conventions for metadata
if mlen == 0:
Expand Down
1 change: 0 additions & 1 deletion babel/messages/plurals.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
babel.messages.plurals
~~~~~~~~~~~~~~~~~~~~~~
Expand Down
10 changes: 4 additions & 6 deletions babel/messages/pofile.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
babel.messages.pofile
~~~~~~~~~~~~~~~~~~~~~
Expand All @@ -10,7 +9,6 @@
:license: BSD, see LICENSE for more details.
"""

from __future__ import print_function
import os
import re

Expand Down Expand Up @@ -75,13 +73,13 @@ def denormalize(string):
class PoFileError(Exception):
"""Exception thrown by PoParser when an invalid po file is encountered."""
def __init__(self, message, catalog, line, lineno):
super(PoFileError, self).__init__('{message} on {lineno}'.format(message=message, lineno=lineno))
super().__init__(f'{message} on {lineno}')
self.catalog = catalog
self.line = line
self.lineno = lineno


class _NormalizedString(object):
class _NormalizedString:

def __init__(self, *args):
self._strs = []
Expand Down Expand Up @@ -128,7 +126,7 @@ def __ne__(self, other):



class PoFileParser(object):
class PoFileParser:
"""Support class to read messages from a ``gettext`` PO (portable object) file
and add them to a `Catalog`
Expand Down Expand Up @@ -170,7 +168,7 @@ def _add_message(self):
"""
self.translations.sort()
if len(self.messages) > 1:
msgid = tuple([m.denormalize() for m in self.messages])
msgid = tuple(m.denormalize() for m in self.messages)
else:
msgid = self.messages[0].denormalize()
if isinstance(msgid, (list, tuple)):
Expand Down
5 changes: 2 additions & 3 deletions babel/numbers.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
babel.numbers
~~~~~~~~~~~~~
Expand Down Expand Up @@ -661,7 +660,7 @@ class NumberFormatError(ValueError):
"""Exception raised when a string cannot be parsed into a number."""

def __init__(self, message, suggestions=None):
super(NumberFormatError, self).__init__(message)
super().__init__(message)
#: a list of properly formatted numbers derived from the invalid input
self.suggestions = suggestions

Expand Down Expand Up @@ -872,7 +871,7 @@ def parse_precision(p):
exp_prec, exp_plus)


class NumberPattern(object):
class NumberPattern:

def __init__(self, pattern, prefix, suffix, grouping,
int_prec, frac_prec, exp_prec, exp_plus):
Expand Down
11 changes: 5 additions & 6 deletions babel/plural.py
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
babel.numbers
~~~~~~~~~~~~~
Expand Down Expand Up @@ -75,7 +74,7 @@ def extract_operands(source):
return n, i, v, w, f, t, c, e


class PluralRule(object):
class PluralRule:
"""Represents a set of language pluralization rules. The constructor
accepts a list of (tag, expr) tuples or a dict of `CLDR rules`_. The
resulting object is callable and accepts one parameter with a positive or
Expand Down Expand Up @@ -149,9 +148,9 @@ def rules(self):
{'one': 'n is 1'}
"""
_compile = _UnicodeCompiler().compile
return dict([(tag, _compile(ast)) for tag, ast in self.abstract])
return {tag: _compile(ast) for tag, ast in self.abstract}

tags = property(lambda x: frozenset([i[0] for i in x.abstract]), doc="""
tags = property(lambda x: frozenset(i[0] for i in x.abstract), doc="""
A set of explicitly defined tags in this rule. The implicit default
``'other'`` rules is not part of this set unless there is an explicit
rule for it.""")
Expand Down Expand Up @@ -385,7 +384,7 @@ def negate(rv):
return 'not', (rv,)


class _Parser(object):
class _Parser:
"""Internal parser. This class can translate a single rule into an abstract
tree of tuples. It implements the following grammar::
Expand Down Expand Up @@ -521,7 +520,7 @@ def _unary_compiler(tmpl):
compile_zero = lambda x: '0'


class _Compiler(object):
class _Compiler:
"""The compilers are able to transform the expressions into multiple
output formats.
"""
Expand Down

0 comments on commit 53a74f1

Please sign in to comment.