Commit e83d24d5 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI

Fix deprecations and super usage

parent c5a6141d
Pipeline #14321 passed with stages
in 7 minutes and 57 seconds
......@@ -29,7 +29,8 @@ class FileDatabase(object):
The extension of raw data files, e.g. ``.png``.
"""
def __init__(self, original_directory, original_extension):
  • @amohammadi Do you know what is the reason to put call to super() here when the FileDatabase just extends object? I get a complain that object.__init__() takes no parameters

  • That's how super works ...

    class A(object):
        def __init__(self, A1, **kwargs):
            print('init of A is called with kwargs {}'.format(kwargs))
            super(A, self).__init__(**kwargs)
            self.A1 = A1
    
    
    class B(object):
        def __init__(self, B1, **kwargs):
            print('init of B is called with kwargs {}'.format(kwargs))
            super(B, self).__init__(**kwargs) # <-- this will not call object.__init__ but it will call A.__init__
            self.B1 = B1
    
    
    class C(B, A):
        def __init__(self, **kwargs):
            print('init of C is called with kwargs {}'.format(kwargs))
            super(C, self).__init__(**kwargs)
    
    
    c = C(A1='A1', B1='B1')

    you will get:

    init of C is called with kwargs {'A1': 'A1', 'B1': 'B1'}
    init of B is called with kwargs {'A1': 'A1'}
    init of A is called with kwargs {}
  • As you can see above B just extends object but when super is used it will actually call A.__init__ without having to know about it. So you think B will call object but in reality it will call A when you have diamond shaped inheritance like here:

       object
       /     \
      /       \
    A          B
      \       /
       \     /
          C
    Edited by Amir MOHAMMADI
  • ok, I see. Thanks. It was the problem from ijba but I think you fixed it already.

  • @pkorshunov just make sure you don't pass extra arguments to this class if it is not really needed. You can see how I fixed this issue in bob.bio.base!118 (diffs)

  • Yes it is fixed in bob.bio.face

    Edited by Amir MOHAMMADI
Please register or sign in to reply
def __init__(self, original_directory, original_extension, **kwargs):
super(FileDatabase, self).__init__(**kwargs)
self.original_directory = original_directory
self.original_extension = original_extension
......@@ -57,7 +58,7 @@ class FileDatabase(object):
if self.original_extension is None:
raise ValueError(
'self.original_extension was not provided (must not be None)!')
return self.file_names(
return file_names(
files, self.original_directory, self.original_extension)
def original_file_name(self, file):
......@@ -102,42 +103,48 @@ class FileDatabase(object):
def check_parameters_for_validity(self, parameters, parameter_description,
valid_parameters,
default_parameters=None):
DeprecationWarning("This method is deprecated. Please use the "
"equivalent function in bob.db.base.utils")
warnings.warn("check_parameters_for_validity is deprecated. Please use "
"the equivalent function in bob.db.base.utils",
DeprecationWarning, stacklevel=2)
return check_parameters_for_validity(parameters, parameter_description,
valid_parameters,
default_parameters)
def check_parameter_for_validity(self, parameter, parameter_description,
valid_parameters, default_parameter=None):
DeprecationWarning("This method is deprecated. Please use the "
"equivalent function in bob.db.base.utils")
warnings.warn("check_parameter_for_validity is deprecated. Please use the "
"equivalent function in bob.db.base.utils",
DeprecationWarning, stacklevel=2)
return check_parameter_for_validity(parameter, parameter_description,
valid_parameters,
default_parameter)
def convert_names_to_highlevel(self, names, low_level_names,
high_level_names):
DeprecationWarning("This method is deprecated. Please use the "
"equivalent function in bob.db.base.utils")
warnings.warn("convert_names_to_highlevel is deprecated. Please use the "
"equivalent function in bob.db.base.utils",
DeprecationWarning, stacklevel=2)
return convert_names_to_highlevel(names, low_level_names,
high_level_names)
def convert_names_to_lowlevel(self, names, low_level_names,
high_level_names):
DeprecationWarning("This method is deprecated. Please use the "
"equivalent function in bob.db.base.utils")
warnings.warn("convert_names_to_lowlevel is deprecated. Please use the "
"equivalent function in bob.db.base.utils",
DeprecationWarning, stacklevel=2)
return convert_names_to_lowlevel(names, low_level_names,
high_level_names)
def file_names(self, files, directory, extension):
DeprecationWarning("This method is deprecated. Please use the "
"equivalent function in bob.db.base.utils")
warnings.warn("file_names is deprecated. Please use the "
"equivalent function in bob.db.base.utils",
DeprecationWarning, stacklevel=2)
return file_names(files, directory, extension)
def sort(self, files):
DeprecationWarning("This method is deprecated. Please use the "
"equivalent function (sort_files) in bob.db.base.utils")
warnings.warn("sort is deprecated. Please use "
"sort_files in bob.db.base.utils",
DeprecationWarning, stacklevel=2)
return sort_files(files)
......@@ -145,11 +152,13 @@ class Database(FileDatabase):
"""This class is deprecated. New databases should use the
:py:class:`bob.db.base.FileDatabase` class if required"""
def __init__(self, original_directory=None, original_extension=None):
def __init__(self, original_directory=None, original_extension=None,
**kwargs):
warnings.warn("The bob.db.base.Database class is deprecated. "
"Please use bob.db.base.FileDatabase instead.",
DeprecationWarning)
super(Database, self).__init__(original_directory, original_extension)
DeprecationWarning, stacklevel=2)
super(Database, self).__init__(original_directory, original_extension,
**kwargs)
class SQLiteBaseDatabase(object):
......@@ -180,7 +189,8 @@ class SQLiteBaseDatabase(object):
The `sqlite_file` parameter is kept in this attribute.
"""
def __init__(self, sqlite_file, file_class):
def __init__(self, sqlite_file, file_class, **kwargs):
super(SQLiteBaseDatabase, self).__init__(**kwargs)
self.m_sqlite_file = sqlite_file
if not os.path.exists(sqlite_file):
self.m_session = None
......@@ -312,11 +322,7 @@ class SQLiteBaseDatabase(object):
if not preserve_order:
return file_objects
else:
# path_dict = {f.path : f for f in file_objects} <<-- works fine
# with python 2.7, but not in 2.6
path_dict = {}
for f in file_objects:
path_dict[f.path] = f
path_dict = {f.path: f for f in file_objects}
return [path_dict[path] for path in paths]
def uniquify(self, file_list):
......@@ -355,6 +361,9 @@ class SQLiteDatabase(SQLiteBaseDatabase, FileDatabase):
"""
def __init__(self, sqlite_file, file_class,
original_directory, original_extension):
SQLiteBaseDatabase.__init__(self, sqlite_file, file_class)
FileDatabase.__init__(self, original_directory, original_extension)
original_directory, original_extension, **kwargs):
kwargs['sqlite_file'] = sqlite_file
kwargs['file_class'] = file_class
kwargs['original_directory'] = original_directory
kwargs['original_extension'] = original_extension
super(SQLiteDatabase, self).__init__(**kwargs)
"""You need to run the tests here like this:
$ PYTHONWARNINGS=all bin/nosetests -sv
"""
from bob.db.base import Database
def test_database_deprecations():
database = Database()
low_level_names = ('train', 'dev')
high_level_names = ('world', 'dev')
database.convert_names_to_lowlevel(
'world', low_level_names, high_level_names)
database.convert_names_to_highlevel(
'train', low_level_names, high_level_names)
database.check_parameter_for_validity(
'world', 'groups', high_level_names, None)
database.check_parameters_for_validity(
'world', 'groups', high_level_names, None)
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