class DirTreeFilter(object):
    def __init__(self, show_hidden=False, show_files=True, show_dirs=True):
        self.show_hidden = show_hidden
        self.show_files = show_files
        self.show_dirs = show_dirs
        self.hidden_exts = [".pyc", ".pyo", ".o", ".a", ".obj", ".lib", ".swp", "~"]
        self.hidden_dirs = ["CVS", "__pycache__"]

    def __call__(self, info):
        if info.hidden and not self.show_hidden:
            return False
        if info.is_file and not self.show_files:
            return False
        elif info.is_dir:
            if not self.show_dirs:
                return False
            if info.filename in self.hidden_dirs:
                return False
        for ext in self.hidden_exts:
            if info.filename.endswith(ext):
                return False
        if info.filename.startswith(".#"):
            return False
        return True

import re

def compile_file_patterns(patterns):
    return re.compile("$%s^" % "|".join("(%s)" % re.escape(p).replace("\\*", ".*") for p in patterns))

hidden_files = [".*", "*~", "*.swp", "*.pyc", "*.pyo", "*.o", "*.a", "*.obj", "*.lib", "*.class"]
hidden_dirs = ["CVS", "__pycache__"]

class DirTreeFilter(object):
    def __init__(self, show_hidden=False, show_files=True, show_dirs=True,
                 hidden_files=hidden_files, hidden_dirs=hidden_dirs):
        self.show_hidden = show_hidden
        self.show_files = show_files
        self.show_dirs = show_dirs
        self.r_hidden_file = compile_file_patterns(hidden_files)
        self.r_hidden_dir = compile_file_patterns(hidden_dirs)

    def __call__(self, info):
        if info.hidden and not self.show_hidden:
            return False
        if info.is_file and not self.show_files:
            return False
        if info.is_dir:
            if not self.show_dirs:
                return False
            if self.r_hidden_dir.match(info.filename):
                return False
        else:
            if self.r_hidden_file.match(info.filename):
                return False
        return True

Use file patterns compiled to regular expressions to match hidden files.
Use file patterns compiled to regular expressions to match hidden files.

+ import re
+ 
+ def compile_file_patterns(patterns):
+     return re.compile("$%s^" % "|".join("(%s)" % re.escape(p).replace("\\*", ".*") for p in patterns))
+ 
+ hidden_files = [".*", "*~", "*.swp", "*.pyc", "*.pyo", "*.o", "*.a", "*.obj", "*.lib", "*.class"]
+ hidden_dirs = ["CVS", "__pycache__"]
+ 
  class DirTreeFilter(object):
-     def __init__(self, show_hidden=False, show_files=True, show_dirs=True):
+     def __init__(self, show_hidden=False, show_files=True, show_dirs=True,
+                  hidden_files=hidden_files, hidden_dirs=hidden_dirs):
          self.show_hidden = show_hidden
          self.show_files = show_files
          self.show_dirs = show_dirs
-         self.hidden_exts = [".pyc", ".pyo", ".o", ".a", ".obj", ".lib", ".swp", "~"]
-         self.hidden_dirs = ["CVS", "__pycache__"]
+         self.r_hidden_file = compile_file_patterns(hidden_files)
+         self.r_hidden_dir = compile_file_patterns(hidden_dirs)
  
      def __call__(self, info):
          if info.hidden and not self.show_hidden:
              return False
          if info.is_file and not self.show_files:
              return False
-         elif info.is_dir:
+         if info.is_dir:
              if not self.show_dirs:
                  return False
-             if info.filename in self.hidden_dirs:
+             if self.r_hidden_dir.match(info.filename):
                  return False
-         for ext in self.hidden_exts:
-             if info.filename.endswith(ext):
+         else:
+             if self.r_hidden_file.match(info.filename):
                  return False
-         if info.filename.startswith(".#"):
-             return False
          return True
  
Use file patterns compiled to regular expressions to match hidden files.
## Code Before:
class DirTreeFilter(object):
    def __init__(self, show_hidden=False, show_files=True, show_dirs=True):
        self.show_hidden = show_hidden
        self.show_files = show_files
        self.show_dirs = show_dirs
        self.hidden_exts = [".pyc", ".pyo", ".o", ".a", ".obj", ".lib", ".swp", "~"]
        self.hidden_dirs = ["CVS", "__pycache__"]

    def __call__(self, info):
        if info.hidden and not self.show_hidden:
            return False
        if info.is_file and not self.show_files:
            return False
        elif info.is_dir:
            if not self.show_dirs:
                return False
            if info.filename in self.hidden_dirs:
                return False
        for ext in self.hidden_exts:
            if info.filename.endswith(ext):
                return False
        if info.filename.startswith(".#"):
            return False
        return True

## Instruction:
Use file patterns compiled to regular expressions to match hidden files.
## Code After:
import re

def compile_file_patterns(patterns):
    return re.compile("$%s^" % "|".join("(%s)" % re.escape(p).replace("\\*", ".*") for p in patterns))

hidden_files = [".*", "*~", "*.swp", "*.pyc", "*.pyo", "*.o", "*.a", "*.obj", "*.lib", "*.class"]
hidden_dirs = ["CVS", "__pycache__"]

class DirTreeFilter(object):
    def __init__(self, show_hidden=False, show_files=True, show_dirs=True,
                 hidden_files=hidden_files, hidden_dirs=hidden_dirs):
        self.show_hidden = show_hidden
        self.show_files = show_files
        self.show_dirs = show_dirs
        self.r_hidden_file = compile_file_patterns(hidden_files)
        self.r_hidden_dir = compile_file_patterns(hidden_dirs)

    def __call__(self, info):
        if info.hidden and not self.show_hidden:
            return False
        if info.is_file and not self.show_files:
            return False
        if info.is_dir:
            if not self.show_dirs:
                return False
            if self.r_hidden_dir.match(info.filename):
                return False
        else:
            if self.r_hidden_file.match(info.filename):
                return False
        return True
