I have a program like this with a class StatFind. This class has three methods. ncount method returns a list of dictionaries: 'finallist'. I need to add each of these dictionaries from the list into a mongodb database.
How do I access the finallist inside of my inserttomongo() method.
The code currently gives a nameerror:
s.inserttomongo(finallist)
#=> NameError: name 'finallist' is not defined
Here is my code:
!/usr/bin/python
import pymongo,json
from datetime import date, timedelta
from collections import defaultdict
import os, sys,time,csv,glob
tsvs = glob.glob(sys.argv[1])
class StatFind:
  def __init__(self,tsvs):
    self.tsvs=tsvs
  def ncount(self, tsvs):
    if True:
      finallist=[]
      for path in tsvs:
         ....Someprocess....
         returns a list
      return finallist
  def other(self):
    samplestring= "something random"
    print samplestring
  def inserttomongo(self, finallist):
     self.finallist=ncount().finallist
     mongo=pymongo.Connection('localhost')
     mongo_db=mongo['sample']
     mongo_collection=mongo_db['users']
     for dictvalue in self.finallist:
      #  for dictvalue in ncount(tsvs):
       insert_id=mongo_collection.insert(dictvalue)
s=StatFind(tsvs)
s.ncount(tsvs)
s.other()
s.inserttomongo(finallist)
 
    