Skip to content
Snippets Groups Projects
Commit e83d24d5 authored by Amir MOHAMMADI's avatar Amir MOHAMMADI
Browse files

Fix deprecations and super usage

parent c5a6141d
No related branches found
No related tags found
1 merge request!34Fix deprecations and super usage
Pipeline #
...@@ -29,7 +29,8 @@ class FileDatabase(object): ...@@ -29,7 +29,8 @@ class FileDatabase(object):
The extension of raw data files, e.g. ``.png``. The extension of raw data files, e.g. ``.png``.
""" """
def __init__(self, original_directory, original_extension): def __init__(self, original_directory, original_extension, **kwargs):
  • Maintainer

    @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

  • Author Owner

    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 {}
  • Author Owner

    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
  • Maintainer

    ok, I see. Thanks. It was the problem from ijba but I think you fixed it already.

  • Author Owner

    @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)

  • Author Owner

    Yes it is fixed in bob.bio.face

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