Commit c4c694ac authored by Nicolas Chauvat's avatar Nicolas Chauvat
Browse files

pyupgrade

parent c3fde2006932
......@@ -76,7 +76,7 @@
# print "I'm dying..."
pass
ENV_VAR = re.compile('\$\{(\w+)\}')
ENV_VAR = re.compile(r'\$\{(\w+)\}')
def expand_vars(text, env_vars):
"""replaces variables with their values
......@@ -119,8 +119,7 @@
yield ('TERMINAL', re.compile(left, re.I), right)
elif line.startswith('include '):
filename = line[8:].strip()
for included_line in read_from_stream(file(join(base_dir, filename)), env_context = env_context):
yield included_line
yield from read_from_stream(file(join(base_dir, filename)), env_context = env_context)
else:
raise BadAilRule(line)
......@@ -143,6 +142,6 @@
class BadAilCommand(AilError):
"""exception used when an unknown command was used"""
class AILBrainHandler(object):
class AILBrainHandler:
"""a default command handler used to interactivly play with ail"""
def command(self, cmdname, *args):
......@@ -147,6 +146,6 @@
"""a default command handler used to interactivly play with ail"""
def command(self, cmdname, *args):
return 'command : %s %s' % (cmdname, str(args))
return f'command : {cmdname} {str(args)}'
def random(self, *args):
return choice(args)
......@@ -169,7 +168,7 @@
string = str(string, encoding)
return string
class Brain(object):
class Brain:
"""the ail brain: process text according to loaded rules
"""
......@@ -220,7 +219,7 @@
else:
groups.append(normalize(grp))
# print "NEXT = %r, dict = %s" % (next, dict([(str(key), val) for key, val in enumerate(groups)]))
next = next % dict([(str(key), val) for key, val in enumerate(groups)])
next = next % {str(key): val for key, val in enumerate(groups)}
return target, next
raise NoRewriteError()
......@@ -251,7 +250,7 @@
method = getattr(self._cmds_hdlr, words[0].lower())
except AttributeError:
raise AilError('Unknown action %s' % words[0].lower())
return method(*[type_cast(w, encoding) for w in words[1:]])
return method(*(type_cast(w, encoding) for w in words[1:]))
def split(self, string):
shl = shlex.shlex(string, posix=True)
......
......@@ -83,7 +83,7 @@
if exists(join(absfile, '__init__.py')) or \
package in ('test', 'tests'):
if prefix:
result.append('%s.%s' % (prefix, package))
result.append(f'{prefix}.{package}')
else:
result.append(package)
result += get_packages(absfile, result[-1])
......@@ -108,7 +108,7 @@
continue
if filename[-1] == '~':
continue
src = '%s/%s' % (directory, filename)
src = f'{directory}/{filename}'
dest = to_dir + src[len(from_dir):]
print(src, '->', dest, file=sys.stderr)
if os.path.isdir(src):
......
......@@ -42,7 +42,7 @@
def add(self, subject, predicate, object):
self._kb.append( (subject, predicate, object) )
return 'added %s to the knowledge base' % ((subject, predicate, object),)
return f'added {(subject, predicate, object)} to the knowledge base'
def search(self, subject, predicate, object):
assert (subject, predicate, object) is not (None, None, None)
......@@ -84,7 +84,7 @@
ENV_VARS = {'MYUSER' : "adim",
}
ENV_PATTERNS = [('TERMINAL', 'WHO DO I KNOW \?$', 'search who does adim know'),
ENV_PATTERNS = [('TERMINAL', r'WHO DO I KNOW \?$', 'search who does adim know'),
('TERMINAL', 'adim ${YOU}$', 'pass'),
]
......@@ -158,7 +158,7 @@
def test_empty_regexp_groups(self):
"""tests empty regexp groups"""
self.brain.load_string('DO IT\s*(\!|\.)? :: add adim said doit')
self.brain.load_string(r'DO IT\s*(\!|\.)? :: add adim said doit')
self.assertEqual(self.kb._kb, [])
# -> match.groups() will return (None,) because '!' and '.' are
# optionals => make sure None won't make our brain crash
......@@ -190,9 +190,9 @@
def test_rev_think(self):
self.assertEqual(list(self.brain.rev_think('whatever')), [])
self.assertEqual(list(self.brain.rev_think('add')), ['(.*) WORKS_FOR (.*)\.$', "I\\'M AWAY FOR (\\d+) MINUTES$"])
self.assertEqual(list(self.brain.rev_think('search')), ['WHO WORKS_FOR (.*)\?$'])
self.assertEqual(list(self.brain.rev_think('search', 'None')), ['WHO WORKS_FOR (.*)\?$'])
self.assertEqual(list(self.brain.rev_think('add')), [r'(.*) WORKS_FOR (.*)\.$', "I\\'M AWAY FOR (\\d+) MINUTES$"])
self.assertEqual(list(self.brain.rev_think('search')), [r'WHO WORKS_FOR (.*)\?$'])
self.assertEqual(list(self.brain.rev_think('search', 'None')), [r'WHO WORKS_FOR (.*)\?$'])
self.assertEqual(list(self.brain.rev_think('search', 'whatever')), [])
......@@ -206,7 +206,7 @@
def test_long_input(self):
input_file = 'data/data_input'
f_pointer = open(input_file, 'r')
f_pointer = open(input_file)
from_file = f_pointer.read()
f_pointer.close()
res = self.brain.think(from_file)
......@@ -278,7 +278,7 @@
def test_reload_properties(self):
"""tests source reloading"""
old_sourcelist_id = id(self.brain.source_files)
self._add_rule_for_reload("(.*) WORKS_FOR (.*)\. :: add '\1' 'WORKS_FOR' '\2'")
self._add_rule_for_reload("(.*) WORKS_FOR (.*)\\. :: add '\1' 'WORKS_FOR' '\2'")
self.brain.reload()
# we added only one rule
self.assertEqual(len(self.brain._patterns), 1)
......@@ -290,7 +290,7 @@
def test_think_after_reload(self):
"""make sure the brain is more clever after adding rules"""
self.assertEqual(self.kb._kb, [])
self._add_rule_for_reload("(\S+) WORKS_FOR (\S+)\. :: add '\\1' 'WORKS_FOR' '\\2'")
self._add_rule_for_reload("(\\S+) WORKS_FOR (\\S+)\\. :: add '\\1' 'WORKS_FOR' '\\2'")
self.assertRaises(ail.NoRewriteError, self.brain.think, "adim works_for Logilab.")
self.brain.reload()
self.brain.think("adim WORKS_FOR Logilab.")
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment