0

I have a list r which contains 12 lists as elements. Each list contains some elements (i[0] to i[5]) which are the same compare to other lists. I tried only to insert unique documents so as result I should get 5 documents in the DB. If the document already exist then append i[6] and i[7] to chr.

import couchdb

# $ sudo systemctl start couchdb
# http://localhost:5984/_utils/

server = couchdb.Server()
try:
    db = server.create("test")
except couchdb.http.ResourceConflict:
    db = server["test"]

r = [["Test", "A", "B01", 828288,  1,    7, 'C', 5],
    ["Test", "A", "B01", 828288,  1,    7, 'T', 6],
    ["Test", "A", "B01", 171878,  3,    8, 'C', 5],
    ["Test", "A", "B01", 171878,  3,    8, 'T', 6],
    ["Test", "A", "B01", 871963,  3,    9, 'A', 5],
    ["Test", "A", "B01", 871963,  3,    9, 'G', 6],
    ["Test", "A", "B01", 1932523, 1,   10, 'T', 4],
    ["Test", "A", "B01", 1932523, 1,   10, 'A', 5],
    ["Test", "A", "B01", 1932523, 1,   10, 'X', 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]]

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

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

    a = db.save(doc)

UPDATE The first two lists in r contains the same fields (i[0] to i[5]). The final document for the two lists should like this:

{
  "_id": "7",
  "_rev": "1-bc0b4e6f3aa855a486225f4a0dcd76c8",
  "sub_name": "B01",
  "name": "A",
  "pos": 828288,
  "s_type": 1,
  "chr": [
      {
          "letter": "C",
          "no": 5
      },
      {
          "letter": "T",
          "no": 6
      }
  ],
  "type": "Test"
}

How is possible to update a document and append to a list a dict?

user977828
  • 7,259
  • 16
  • 66
  • 117

1 Answers1

0

Is this the most efficient solution?

import couchdb

# $ sudo systemctl start couchdb
# http://localhost:5984/_utils/

server = couchdb.Server()
db = server.create("test")
# except couchdb.http.ResourceConflict:
#db = server["test"]

r = [["Test", "A", "B01", 828288,  1,    7, 'C', 5],
    ["Test", "A", "B01", 828288,  1,    7, 'T', 6],
    ["Test", "A", "B01", 171878,  3,    8, 'C', 5],
    ["Test", "A", "B01", 171878,  3,    8, 'T', 6],
    ["Test", "A", "B01", 871963,  3,    9, 'A', 5],
    ["Test", "A", "B01", 871963,  3,    9, 'G', 6],
    ["Test", "A", "B01", 1932523, 1,   10, 'T', 4],
    ["Test", "A", "B01", 1932523, 1,   10, 'A', 5],
    ["Test", "A", "B01", 1932523, 1,   10, 'X', 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]]

# _id = None
for i in r:
    _id = str(i[5])
    doc = db.get(_id)
    if doc is None:
        doc = {
            'type': i[0],
            'name': i[1],
            'sub_name': i[2],
            'pos': i[3],
            's_type': i[4],
            '_id': _id,
            'chr':[]
        }

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

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

    db.save(doc)
user977828
  • 7,259
  • 16
  • 66
  • 117