
import re
import UserDict


class Textsub(UserDict.UserDict):
    def __init__(self, dict=None):
        self.re = None
        self.regex = None
        UserDict.UserDict.__init__(self, dict)

    def compile(self):
        if len(self.data) > 0:
            tmp = "(%s)" % "|".join(map(re.escape,
                                        self.data.keys()))
            if self.re != tmp:
                self.re = tmp
                self.regex = re.compile(self.re)

    def __call__(self, match):
        return self.data[match.string[match.start():match.end()]]

    def sub(self, s):
        if len(self.data) == 0:
            return s
        return self.regex.sub(self, s)


import re

try:
    from UserDict import UserDict
except ImportError:
    from collections import UserDict


class Textsub(UserDict):
    def __init__(self, dict=None):
        self.re = None
        self.regex = None
        UserDict.__init__(self, dict)

    def compile(self):
        if len(self.data) > 0:
            tmp = "(%s)" % "|".join(map(re.escape,
                                        self.data.keys()))
            if self.re != tmp:
                self.re = tmp
                self.regex = re.compile(self.re)

    def __call__(self, match):
        return self.data[match.string[match.start():match.end()]]

    def sub(self, s):
        if len(self.data) == 0:
            return s
        return self.regex.sub(self, s)

Make UserDict usage compatible with Python3
Make UserDict usage compatible with Python3

  
  import re
- import UserDict
+ 
+ try:
+     from UserDict import UserDict
+ except ImportError:
+     from collections import UserDict
  
  
- class Textsub(UserDict.UserDict):
+ class Textsub(UserDict):
      def __init__(self, dict=None):
          self.re = None
          self.regex = None
-         UserDict.UserDict.__init__(self, dict)
+         UserDict.__init__(self, dict)
  
      def compile(self):
          if len(self.data) > 0:
              tmp = "(%s)" % "|".join(map(re.escape,
                                          self.data.keys()))
              if self.re != tmp:
                  self.re = tmp
                  self.regex = re.compile(self.re)
  
      def __call__(self, match):
          return self.data[match.string[match.start():match.end()]]
  
      def sub(self, s):
          if len(self.data) == 0:
              return s
          return self.regex.sub(self, s)
  
Make UserDict usage compatible with Python3
## Code Before:

import re
import UserDict


class Textsub(UserDict.UserDict):
    def __init__(self, dict=None):
        self.re = None
        self.regex = None
        UserDict.UserDict.__init__(self, dict)

    def compile(self):
        if len(self.data) > 0:
            tmp = "(%s)" % "|".join(map(re.escape,
                                        self.data.keys()))
            if self.re != tmp:
                self.re = tmp
                self.regex = re.compile(self.re)

    def __call__(self, match):
        return self.data[match.string[match.start():match.end()]]

    def sub(self, s):
        if len(self.data) == 0:
            return s
        return self.regex.sub(self, s)

## Instruction:
Make UserDict usage compatible with Python3
## Code After:

import re

try:
    from UserDict import UserDict
except ImportError:
    from collections import UserDict


class Textsub(UserDict):
    def __init__(self, dict=None):
        self.re = None
        self.regex = None
        UserDict.__init__(self, dict)

    def compile(self):
        if len(self.data) > 0:
            tmp = "(%s)" % "|".join(map(re.escape,
                                        self.data.keys()))
            if self.re != tmp:
                self.re = tmp
                self.regex = re.compile(self.re)

    def __call__(self, match):
        return self.data[match.string[match.start():match.end()]]

    def sub(self, s):
        if len(self.data) == 0:
            return s
        return self.regex.sub(self, s)
