Commits

Joel Rivera committed 7bc085b

Add six as a dependency and update database.py and tree_index.py to use six.

  • Participants
  • Parent commits 75acda6
  • Branches py3k

Comments (0)

Files changed (3)

CodernityDB/database.py

 import io
 from inspect import getsource
 
+import six
+from six import next
+
 # for custom indexes
 from CodernityDB.storage import Storage, IU_Storage
 from CodernityDB.hash_index import (IU_UniqueHashIndex,
             code = f.read()
         try:
             obj = compile(code, '<Index: %s' % os.path.join(p, ind), 'exec')
-            exec obj in globals()
+            six.exec_(obj, globals())
             ind_obj = globals()[_class](self.path, name, **ind_kwargs)
             ind_obj._order = int(ind[:2])
         except:
         if ind_kwargs is None:
             ind_kwargs = {}
         p = os.path.join(self.path, '_indexes')
-        if isinstance(new_index, basestring) and not new_index.startswith("path:"):
+        if isinstance(new_index, six.string_types) and not new_index.startswith("path:"):
             if len(new_index.splitlines()) < 4 or new_index.splitlines()[3] != '# inserted automatically':
-                from indexcreator import Parser
+                from .indexcreator import Parser
                 par = Parser()
                 custom_imports, s = par.parse(new_index)
                 s = s.splitlines()
                 name = s[0][2:]
                 c = s[1][2:]
-                comented = ['\n\n#SIMPLIFIED CODE']
-                map(lambda x: comented.append("#" + x), new_index.splitlines())
+                comented = ['#' + line for line in new_index.splitlines()]
+                comented.insert(0, '\n\n#SIMPLIFIED CODE')
                 comented.append('#SIMPLIFIED CODE END\n\n')
-
                 s = header_for_indexes(
                     name, c, ind_custom=custom_imports) + "\n".join(s[2:]) + "\n".join(comented)
                 new_index = s
             if name in self.indexes_names and not edit:
                 raise IndexConflict("Already exists")
             if edit:
-                previous_index = filter(lambda x: x.endswith(
-                    '.py') and x[2:-3] == name, os.listdir(p))
+                previous_index = [x for x in os.listdir(p) if x.endswith(
+                    '.py') and x[2:-3] == name]
                 if not previous_index:
                     raise PreconditionsException(
                         "Can't edit index that's not yet in database")
 
             ind_obj = self._read_index_single(p, ind_path + '.py')
 
-        elif isinstance(new_index, basestring) and new_index.startswith("path:"):
+        elif isinstance(new_index, six.string_types) and new_index.startswith("path:"):
             path = new_index[5:]
             if not path.endswith('.py'):
                 path += '.py'
             # it will first save index as a string, and then compile it
             # it will allow to control the index object on the DB side
             ind = new_index
-            init_arguments = new_index.__class__.__init__.im_func.func_code.co_varnames[
-                3:]  # ignore self, path and name
+            init_arguments = (six.get_function_code(
+                                 six.get_unbound_function(new_index.__class__.__init__))
+                                    .co_varnames[3:]) # ignore self, path and name
             for curr in init_arguments:
                 if curr not in ('args', 'kwargs'):
                     v = getattr(ind, curr, NONE())
                         ind_kwargs[curr] = v
             if edit:
                 # code duplication...
-                previous_index = filter(lambda x: x.endswith(
-                    '.py') and x[2:-3] == ind.name, os.listdir(p))
+                previous_index = [x for x in os.listdir(p) if x.endswith(
+                    '.py') and x[2:-3] == ind.name]
                 if not previous_index:
                     raise PreconditionsException(
                         "Can't edit index that's not yet in database")
         p = os.path.join(self.path, '_indexes')
         if not os.path.exists(p):
             self.initialize()
-        current = sorted(filter(lambda x: x.endswith('.py'), os.listdir(p)))
+        current = sorted([x for x in os.listdir(p) if x.endswith('.py')])
         if current:
             last = int(current[-1][:2])  # may crash... ignore
             _next = last + 1
                 else:
                     s = co[ind:]
                     l = s.splitlines()[1:-2]
-                    ll = map(lambda x: x[1:], l)
+                    ll = [x[1:] for x in l]
                     return '\n'.join(ll)
             if code_switch == 'P':
                 try:
             warnings.warn("Your database is using old rev mechanizm \
 for ID index. You should update that index \
 (CodernityDB.migrate.migrate).")
-            from misc import random_hex_4
+            from .misc import random_hex_4
             self.create_new_rev = random_hex_4
 
     def create(self, path=None, **kwargs):
         :param index: the index to destroy
         :type index: :py:class:`CodernityDB.index.Index`` instance, or string
         """
-        if isinstance(index, basestring):
+        if isinstance(index, six.string_types):
             if not index in self.indexes_names:
                 raise PreconditionsException("No index named %s" % index)
             index = self.indexes_names[index]
         :param index: the index to destroy
         :type index: :py:class:`CodernityDB.index.Index`` instance, or string
         """
-        if isinstance(index, basestring):
+        if isinstance(index, six.string_types):
             if not index in self.indexes_names:
                 raise PreconditionsException("No index named %s" % index)
             index = self.indexes_names[index]
         :param index: the index to reindex
         :type index: :py:class:`CodernityDB.index.Index`` instance, or string
         """
-        if isinstance(index, basestring):
+        if isinstance(index, six.string_types):
             if not index in self.indexes_names:
                 raise PreconditionsException("No index named %s" % index)
             index = self.indexes_names[index]
 
         while True:
             try:
-                curr = all_iter.next()
+                curr = next(all_iter)
             except StopIteration:
                 break
             else:
         while True:
             try:
 #                l_key, start, size, status = gen.next()
-                ind_data = gen.next()
+                ind_data = next(gen)
             except StopIteration:
                 break
             else:
         gen = ind.all(limit, offset)
         while True:
             try:
-                doc_id, unk, start, size, status = gen.next()
+                doc_id, unk, start, size, status = next(gen)
             except StopIteration:
                 break
             else:
         i = 0
         while True:
             try:
-                iter_.next()
+                next(iter_)
                 i += 1
             except StopIteration:
                 break
             raise IndexNotFoundException("Index doesn't exist")
 
         props = {}
-        for key, value in db_index.__dict__.iteritems():
-            if not callable(value):  # not using inspect etc...
+        for key, value in six.iteritems(db_index.__dict__):
+            if not six.callable(value):  # not using inspect etc...
                 props[key] = value
 
         return props
         props = {}
         props['path'] = self.path
         props['size'] = self.__get_size()
-        props['indexes'] = self.indexes_names.keys()
+        props['indexes'] = list(self.indexes_names.keys())
         props['cdb_environment'] = cdb_environment
         return props

CodernityDB/tree_index.py

 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-
-
-from index import Index, IndexException, DocIdNotFound, ElemNotFound
 import struct
 import marshal
 import os
 import io
 import shutil
-from storage import IU_Storage
+
+import six
+from six import next 
+
+from .storage import IU_Storage
+from .index import Index, IndexException, DocIdNotFound, ElemNotFound
 # from ipdb import set_trace
 
 from CodernityDB.env import cdb_environment
         self._count_props()
         if not storage_class:
             storage_class = IU_Storage
-        if storage_class and not isinstance(storage_class, basestring):
+        if storage_class and not isinstance(storage_class, six.string_types):
             storage_class = storage_class.__name__
         self.storage_class = storage_class
         self.storage = None
         gen = self.all()
         while True:
             try:
-                doc_id, key, start, size, status = gen.next()
+                doc_id, key, start, size, status = next(gen)
             except StopIteration:
                 break
             self.storage._f.seek(start)
       packages=['CodernityDB'],
       platforms='any',
       license=__license__,
+      install_requires=['six',],
       classifiers=[
       "License :: OSI Approved :: Apache Software License",
       "Programming Language :: Python",