fix database _add_single_index, add source code dedent process

#2 Merged at 39d5a49
Repository
dreampuf
Branch
fix_code_compile
Repository
codernity
Branch
default
Author
  1. dreampuf
Reviewers
Description
No description

Comments (5)

  1. dreampuf author

    @codernity I'm use the minitwit demo(for flask), and modify some code to one python script file.

    if cdb.exists():
        cdb.open()
        cdb.reindex()
    else:
        class UserIndex(HashIndex):
        def __init__(self, *args, **kwargs):
            kwargs['key_format'] = '16s'
            super(UserIndex, self).__init__(*args, **kwargs)
        def make_key_value(self, data):
            if data['t'] == 'user':
                username = data['username']
                # if not isinstance(login, basestring):
                #     login = str(login)
                return md5(username).digest(), {'user_id': data['user_id'], 'email': data['email']}
        def make_key(self, key):
            return md5(key).digest()
    
        cdb.create()
        cdb.add_index(UserIndex(cdb.path, 'user'))
    

    when i run these code, it's throw an error with inspect.getsource include the indent. and i find the code was just concat the source code in a template. so, I add this fix.

    In this case(to concat the source code in a template), Could I just delete the code indent?

  2. codernity repo owner

    Hey again,

    Ok it seems as a good use case then.

    But we have to think how to do it correctly because that patch will solve just part of a "problem" (http://labs.codernity.com/codernitydb/database_indexes.html#how-an-index-code-is-processed-by-codernitydb). Because for example \t instead of spaces is also the problem, wrong code indent also ( 2 x instead of 4 x etc). Probably that dedent patch should be more intelligent and try to fix more than this.

    Thank you for idea @dreampuf

    -- Jedrzej

  3. dreampuf author

    @codernity Ok, here is the solution. the textwraps module include some method of text process. the dedent method docsting was "Remove any common leading whitespace from every line in text.". It will deal with the "\t", " "(space) nice. and, I don't think when I get an error input, I should return an right output. otherwise, when the framework get an Index code that right code indent, remove it's indent(can be understood as remove the origin namespace), and do it.

  4. codernity repo owner

    Ok, we will merge that approach in near future (maybe with detection if texwraps import is needed to not load 100 opional modules into memory)

    Thank you very much.