Skip to content
Snippets Groups Projects
Commit ab59046e authored by André Anjos's avatar André Anjos :speech_balloon:
Browse files

[python3] Syntax fixes

parent 83d4adac
No related branches found
No related tags found
No related merge requests found
...@@ -19,7 +19,7 @@ def add_clients(session, protodir, verbose): ...@@ -19,7 +19,7 @@ def add_clients(session, protodir, verbose):
if not s: continue #empty line if not s: continue #empty line
id = int(s[0]) id = int(s[0])
set = s[1] set = s[1]
if verbose: print "Adding client %d on '%s' set..." % (id, set) if verbose: print("Adding client %d on '%s' set..." % (id, set))
session.add(Client(id, set)) session.add(Client(id, set))
def add_real_lists(session, protodir, verbose): def add_real_lists(session, protodir, verbose):
...@@ -108,7 +108,7 @@ def define_protocols(session, protodir, verbose): ...@@ -108,7 +108,7 @@ def define_protocols(session, protodir, verbose):
attack = os.path.join(protodir, 'attack-%s-allsupports-%s.txt' % (s[1], grp)) attack = os.path.join(protodir, 'attack-%s-allsupports-%s.txt' % (s[1], grp))
if not os.path.exists(attack): if not os.path.exists(attack):
if verbose: if verbose:
print "Not considering protocol %s as attack list '%s' was not found" % (s[1], attack) print("Not considering protocol %s as attack list '%s' was not found" % (s[1], attack))
consider = False consider = False
# check real file # check real file
...@@ -117,7 +117,7 @@ def define_protocols(session, protodir, verbose): ...@@ -117,7 +117,7 @@ def define_protocols(session, protodir, verbose):
alt_real = os.path.join(protodir, 'real-%s.txt' % (grp,)) alt_real = os.path.join(protodir, 'real-%s.txt' % (grp,))
if not os.path.exists(alt_real): if not os.path.exists(alt_real):
if verbose: if verbose:
print "Not considering protocol %s as real list '%s' or '%s' were not found" % (s[1], real, alt_real) print("Not considering protocol %s as real list '%s' or '%s' were not found" % (s[1], real, alt_real))
consider = False consider = False
else: else:
real = alt_real real = alt_real
...@@ -126,13 +126,13 @@ def define_protocols(session, protodir, verbose): ...@@ -126,13 +126,13 @@ def define_protocols(session, protodir, verbose):
if consider: valid[s[1]] = files if consider: valid[s[1]] = files
for protocol, groups in valid.iteritems(): for protocol, groups in valid.items():
if verbose: print "Creating protocol '%s'..." % protocol if verbose: print("Creating protocol '%s'..." % protocol)
# create protocol on the protocol table # create protocol on the protocol table
obj = Protocol(name=protocol) obj = Protocol(name=protocol)
for grp, flist in groups.iteritems(): for grp, flist in groups.items():
counter = 0 counter = 0
for fname in open(flist[0], 'rt'): for fname in open(flist[0], 'rt'):
...@@ -140,7 +140,7 @@ def define_protocols(session, protodir, verbose): ...@@ -140,7 +140,7 @@ def define_protocols(session, protodir, verbose):
q = session.query(Attack).join(File).filter(File.path == s).one() q = session.query(Attack).join(File).filter(File.path == s).one()
q.protocols.append(obj) q.protocols.append(obj)
counter += 1 counter += 1
if verbose: print " -> %5s/%-6s: %d files" % (grp, "attack", counter) if verbose: print(" -> %5s/%-6s: %d files" % (grp, "attack", counter))
counter = 0 counter = 0
for fname in open(flist[1], 'rt'): for fname in open(flist[1], 'rt'):
...@@ -148,7 +148,7 @@ def define_protocols(session, protodir, verbose): ...@@ -148,7 +148,7 @@ def define_protocols(session, protodir, verbose):
q = session.query(RealAccess).join(File).filter(File.path == s).one() q = session.query(RealAccess).join(File).filter(File.path == s).one()
q.protocols.append(obj) q.protocols.append(obj)
counter += 1 counter += 1
if verbose: print " -> %5s/%-6s: %d files" % (grp, "real", counter) if verbose: print(" -> %5s/%-6s: %d files" % (grp, "real", counter))
session.add(obj) session.add(obj)
...@@ -175,7 +175,7 @@ def create(args): ...@@ -175,7 +175,7 @@ def create(args):
if args.recreate: if args.recreate:
if args.verbose and os.path.exists(dbfile): if args.verbose and os.path.exists(dbfile):
print('unlinking %s...' % dbfile) print(('unlinking %s...' % dbfile))
if os.path.exists(dbfile): os.unlink(dbfile) if os.path.exists(dbfile): os.unlink(dbfile)
if not os.path.exists(os.path.dirname(dbfile)): if not os.path.exists(os.path.dirname(dbfile)):
......
...@@ -155,13 +155,13 @@ class File(Base): ...@@ -155,13 +155,13 @@ class File(Base):
def get_realaccess(self): def get_realaccess(self):
"""Returns the real-access object equivalent to this file or raise""" """Returns the real-access object equivalent to this file or raise"""
if len(self.realaccess) == 0: if len(self.realaccess) == 0:
raise RuntimeError, "%s is not a real-access" % self raise RuntimeError("%s is not a real-access" % self)
return self.realaccess[0] return self.realaccess[0]
def get_attack(self): def get_attack(self):
"""Returns the attack object equivalent to this file or raise""" """Returns the attack object equivalent to this file or raise"""
if len(self.attack) == 0: if len(self.attack) == 0:
raise RuntimeError, "%s is not an attack" % self raise RuntimeError("%s is not an attack" % self)
return self.attack[0] return self.attack[0]
def load(self, directory=None, extension='.hdf5'): def load(self, directory=None, extension='.hdf5'):
......
...@@ -49,7 +49,7 @@ class Database(object): ...@@ -49,7 +49,7 @@ class Database(object):
"""Raise a RuntimeError if the database backend is not available""" """Raise a RuntimeError if the database backend is not available"""
if not self.is_valid(): if not self.is_valid():
raise RuntimeError, "Database '%s' cannot be found at expected location '%s'. Create it and then try re-connecting using Database.connect()" % (INFO.name(), SQLITE_FILE) raise RuntimeError("Database '%s' cannot be found at expected location '%s'. Create it and then try re-connecting using Database.connect()" % (INFO.name(), SQLITE_FILE))
def objects(self, support=Attack.attack_support_choices, def objects(self, support=Attack.attack_support_choices,
protocol='grandtest', groups=Client.set_choices, cls=('attack', 'real'), protocol='grandtest', groups=Client.set_choices, cls=('attack', 'real'),
...@@ -101,7 +101,7 @@ class Database(object): ...@@ -101,7 +101,7 @@ class Database(object):
return check_validity((l,), obj, valid, default) return check_validity((l,), obj, valid, default)
for k in l: for k in l:
if k not in valid: if k not in valid:
raise RuntimeError, 'Invalid %s "%s". Valid values are %s, or lists/tuples of those' % (obj, k, valid) raise RuntimeError('Invalid %s "%s". Valid values are %s, or lists/tuples of those' % (obj, k, valid))
return l return l
# check if groups set are valid # check if groups set are valid
...@@ -120,8 +120,8 @@ class Database(object): ...@@ -120,8 +120,8 @@ class Database(object):
if not protocol: protocol = 'grandtest' #default if not protocol: protocol = 'grandtest' #default
VALID_PROTOCOLS = [k.name for k in self.protocols()] VALID_PROTOCOLS = [k.name for k in self.protocols()]
if protocol not in VALID_PROTOCOLS: if protocol not in VALID_PROTOCOLS:
raise RuntimeError, 'Invalid protocol "%s". Valid values are %s' % \ raise RuntimeError('Invalid protocol "%s". Valid values are %s' % \
(protocol, VALID_PROTOCOLS) (protocol, VALID_PROTOCOLS))
# checks client identity validity # checks client identity validity
VALID_CLIENTS = [k.id for k in self.clients()] VALID_CLIENTS = [k.id for k in self.clients()]
......
#!/usr/bin/env python #!/usr/bin/env python
# vim: set fileencoding=utf-8 : # vim: set fileencoding=utf-8 :
# Tiago de Freitas Pereira <tiagofrepereira@gmail.com> # Tiago de Freitas Pereira <tiagofrepereira@gmail.com>
# Tue 01 Oct 2012 16:48:44 CEST # Tue 01 Oct 2012 16:48:44 CEST
"""Replay attack database implementation as antispoofing.utils.db.Database""" """Replay attack database implementation as antispoofing.utils.db.Database"""
...@@ -78,7 +78,7 @@ class Database(DatabaseBase): ...@@ -78,7 +78,7 @@ class Database(DatabaseBase):
## remove '.. ' lines from rst ## remove '.. ' lines from rst
desc = '\n'.join([k for k in self.long_description().split('\n') if k.strip().find('.. ') != 0]) desc = '\n'.join([k for k in self.long_description().split('\n') if k.strip().find('.. ') != 0])
p = subparser.add_parser(entry_point_name, p = subparser.add_parser(entry_point_name,
help=self.short_description(), help=self.short_description(),
description=desc, description=desc,
formatter_class=RawDescriptionHelpFormatter) formatter_class=RawDescriptionHelpFormatter)
...@@ -100,7 +100,7 @@ class Database(DatabaseBase): ...@@ -100,7 +100,7 @@ class Database(DatabaseBase):
p.set_defaults(name=entry_point_name) p.set_defaults(name=entry_point_name)
p.set_defaults(cls=Database) p.set_defaults(cls=Database)
return return
create_subparser.__doc__ = DatabaseBase.create_subparser.__doc__ create_subparser.__doc__ = DatabaseBase.create_subparser.__doc__
...@@ -113,11 +113,11 @@ class Database(DatabaseBase): ...@@ -113,11 +113,11 @@ class Database(DatabaseBase):
from .driver import Interface from .driver import Interface
i = Interface() i = Interface()
return i.version() return i.version()
def short_description(self): def short_description(self):
return "Anti-Spoofing database with 1300 videos produced at Idiap, Switzerland" return "Anti-Spoofing database with 1300 videos produced at Idiap, Switzerland"
short_description.__doc__ = DatabaseBase.short_description.__doc__ short_description.__doc__ = DatabaseBase.short_description.__doc__
def long_description(self): def long_description(self):
return Database.__doc__ return Database.__doc__
long_description.__doc__ = DatabaseBase.long_description.__doc__ long_description.__doc__ = DatabaseBase.long_description.__doc__
...@@ -127,12 +127,12 @@ class Database(DatabaseBase): ...@@ -127,12 +127,12 @@ class Database(DatabaseBase):
return 'video' in propname return 'video' in propname
elif propname is None: elif propname is None:
return True return True
elif isinstance(propname, (str,unicode)): elif isinstance(propname, str):
return 'video' == propname return 'video' == propname
# does not implement the given access protocol # does not implement the given access protocol
return False return False
def get_data(self, group): def get_data(self, group):
"""Returns either all objects or objects for a specific group""" """Returns either all objects or objects for a specific group"""
real = dict(self.__kwargs) real = dict(self.__kwargs)
...@@ -158,7 +158,7 @@ class Database(DatabaseBase): ...@@ -158,7 +158,7 @@ class Database(DatabaseBase):
def get_test_filters(self): def get_test_filters(self):
return ('device', 'support', 'light') return ('device', 'support', 'light')
def get_filtered_test_data(self, filter): def get_filtered_test_data(self, filter):
def device_filter(obj, filter): def device_filter(obj, filter):
...@@ -189,9 +189,8 @@ class Database(DatabaseBase): ...@@ -189,9 +189,8 @@ class Database(DatabaseBase):
'adverse': (real, [k for k in attack if light_filter(k, 'adverse')]), 'adverse': (real, [k for k in attack if light_filter(k, 'adverse')]),
} }
raise RuntimeError, \ raise RuntimeError("filter parameter should specify a valid filter among `%s'" % \
"filter parameter should specify a valid filter among `%s'" % \ self.get_test_filters())
self.get_test_filters()
def get_all_data(self): def get_all_data(self):
return self.get_data(None) return self.get_data(None)
......
...@@ -4,16 +4,16 @@ ...@@ -4,16 +4,16 @@
# Mon Aug 8 12:40:24 2011 +0200 # Mon Aug 8 12:40:24 2011 +0200
# #
# Copyright (C) 2011-2012 Idiap Research Institute, Martigny, Switzerland # Copyright (C) 2011-2012 Idiap Research Institute, Martigny, Switzerland
# #
# This program is free software: you can redistribute it and/or modify # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License. # the Free Software Foundation, version 3 of the License.
# #
# This program is distributed in the hope that it will be useful, # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of # but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details. # GNU General Public License for more details.
# #
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
...@@ -25,6 +25,12 @@ import unittest ...@@ -25,6 +25,12 @@ import unittest
from .query import Database from .query import Database
from .models import * from .models import *
authenticate_str = 'authenticate'
if sys.version_info[0] < 3: authenticate_str = authenticate_str.encode('utf8')
enroll_str = 'enroll'
if sys.version_info[0] < 3: enroll_str = enroll_str.encode('utf8')
class ReplayDatabaseTest(unittest.TestCase): class ReplayDatabaseTest(unittest.TestCase):
"""Performs various tests on the replay attack database.""" """Performs various tests on the replay attack database."""
...@@ -36,8 +42,8 @@ class ReplayDatabaseTest(unittest.TestCase): ...@@ -36,8 +42,8 @@ class ReplayDatabaseTest(unittest.TestCase):
for v in f[:10]: #only the 10 first... for v in f[:10]: #only the 10 first...
self.assertTrue(isinstance(v.get_realaccess(), RealAccess)) self.assertTrue(isinstance(v.get_realaccess(), RealAccess))
o = v.get_realaccess() o = v.get_realaccess()
self.assertEqual(o.purpose, u'authenticate') self.assertEqual(o.purpose, authenticate_str)
train = db.objects(cls='real', groups='train') train = db.objects(cls='real', groups='train')
self.assertEqual(len(train), 60) self.assertEqual(len(train), 60)
...@@ -77,34 +83,34 @@ class ReplayDatabaseTest(unittest.TestCase): ...@@ -77,34 +83,34 @@ class ReplayDatabaseTest(unittest.TestCase):
def test02_queryAttacks(self): def test02_queryAttacks(self):
self.queryAttackType('grandtest', 1000) self.queryAttackType('grandtest', 1000)
def test03_queryPrintAttacks(self): def test03_queryPrintAttacks(self):
self.queryAttackType('print', 200) self.queryAttackType('print', 200)
def test04_queryMobileAttacks(self): def test04_queryMobileAttacks(self):
self.queryAttackType('mobile', 400) self.queryAttackType('mobile', 400)
def test05_queryHighDefAttacks(self): def test05_queryHighDefAttacks(self):
self.queryAttackType('highdef', 400) self.queryAttackType('highdef', 400)
def test06_queryPhotoAttacks(self): def test06_queryPhotoAttacks(self):
self.queryAttackType('photo', 600) self.queryAttackType('photo', 600)
def test07_queryVideoAttacks(self): def test07_queryVideoAttacks(self):
self.queryAttackType('video', 400) self.queryAttackType('video', 400)
def test08_queryEnrollments(self): def test08_queryEnrollments(self):
db = Database() db = Database()
f = db.objects(cls='enroll') f = db.objects(cls='enroll')
self.assertEqual(len(f), 100) #50 clients, 2 conditions self.assertEqual(len(f), 100) #50 clients, 2 conditions
for v in f: for v in f:
self.assertEqual(v.get_realaccess().purpose, u'enroll') self.assertEqual(v.get_realaccess().purpose, enroll_str)
def test09_queryClients(self): def test09_queryClients(self):
...@@ -139,13 +145,13 @@ class ReplayDatabaseTest(unittest.TestCase): ...@@ -139,13 +145,13 @@ class ReplayDatabaseTest(unittest.TestCase):
self.assertEqual(main('replay dumplist --self-test'.split()), 0) self.assertEqual(main('replay dumplist --self-test'.split()), 0)
def test13_manage_dumplist_2(self): def test13_manage_dumplist_2(self):
from bob.db.script.dbmanage import main from bob.db.script.dbmanage import main
self.assertEqual(main('replay dumplist --class=attack --group=devel --support=hand --protocol=highdef --self-test'.split()), 0) self.assertEqual(main('replay dumplist --class=attack --group=devel --support=hand --protocol=highdef --self-test'.split()), 0)
def test14_manage_dumplist_client(self): def test14_manage_dumplist_client(self):
from bob.db.script.dbmanage import main from bob.db.script.dbmanage import main
self.assertEqual(main('replay dumplist --client=117 --self-test'.split()), 0) self.assertEqual(main('replay dumplist --client=117 --self-test'.split()), 0)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment