Update Handler with CouchDB-Python

34 views
Skip to first unread message

Mic L

unread,
Oct 21, 2014, 2:56:27 AM10/21/14
to couchdb...@googlegroups.com
Hello,
I have a list `r` which contains 7 lists as elements. Each list contains some elements (`i[0]` to `i[5]`) which might be the the same compare to other lists. 

    r = [["Test", "A", "B01", 828288,  1,    7, 'C', 5],
        ["Test", "A", "B01", 828288,  1,    7, 'T', 6],
        ["Test", "A", "B01", 171878,  3,    7, 'C', 5],
        ["Test", "A", "B01", 171878,  3,    7, 'T', 6],
        ["Test", "A", "B01", 667214,  1,   14, 'T', 4],
        ["Test", "A", "B01", 667214,  1,   14, 'G', 5],
        ["Test", "A", "B01", 667214,  1,   14, 'G', 6]]

I tried only to insert unique documents so as result I should get 3 documents in the DB. If the document already exist then append `i[6]` and `i[7]` to `chr`. The final document for the two lists should like this:

   {
      "_id": "14cf531ddd22eab99538e85d1d000ee8",
      "t_id":"7",
      "_rev":"2-1516ba547bdd21724158bc854f39f66b",
      "sub_name":"B01",
      "name":"A",
      "pos":828288,
      "s_type":1,
      "chr":[
          {
            "letter":"C",
            "no":5
          },
          {
            "letter":"T",
            "no":6
          }
      ],
      "type":"Test"
    }{
      "_id": "14cf531ddd22eab99538e85d1d0025d8",
      "t_id":"7",
      "_rev":"2-750078ccc9e74616f33a2537e41b8414",
      "sub_name":"B01",
      "name":"A",
      "pos":171878,
      "s_type":3,
      "chr":[
          {
            "letter":"C",
            "no":5
          },
          {
            "letter":"T",
            "no":6
          }
      ],
      "type":"Test"
    }{
      "_id": "14cf531ddd22eab99538e85d1d005719",
      "t_id":"14",
      "_rev":"3-21300d06c31224416b8ff71b71b304d8",
      "sub_name":"B01",
      "name":"A",
      "pos":667214,
      "s_type":1,
      "chr":[
          {
            "letter":"T",
            "no":4
          },
          {
            "letter":"G",
            "no":5
          },
          {
            "letter":"G",
            "no":6
          }
      ],
      "type":"Test"
    }

The current code looks like this:

    import couchdb

    # $ sudo systemctl start couchdb

    server = couchdb.Server()
    db = server.create("test3")

    r = [["Test", "A", "B01", 828288,  1,    7, 'C', 5],
        ["Test", "A", "B01", 828288,  1,    7, 'T', 6],
        ["Test", "A", "B01", 171878,  3,    7, 'C', 5],
        ["Test", "A", "B01", 171878,  3,    7, 'T', 6],
        ["Test", "A", "B01", 667214,  1,   14, 'T', 4],
        ["Test", "A", "B01", 667214,  1,   14, 'G', 5],
        ["Test", "A", "B01", 667214,  1,   14, 'G', 6]]


    for i in r:
        doc = {
            'type': i[0],
            'name': i[1],
            'sub_name': i[2],
            'pos': i[3],
            's_type': i[4],
            't_id': str(i[5]),
            'chr':[]
        }

        doc['chr'].append({
            "letter":i[6],
            "no":i[7]
        })

        db.save(doc)

How is is possible to  use Update Handler?
Thank you in advance,
Mic
Reply all
Reply to author
Forward
0 new messages