-3

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)
user1189851
  • 4,861
  • 15
  • 47
  • 69
  • Guess http://stackoverflow.com/questions/156767/whats-the-difference-between-an-argument-and-a-parameter might be helpful to you. finallist is a parameter. You need to pass an argument to your method, which, if an identifier instead of a constant (in your case, an identifier named 'finallist'), needs to be defined first! – Atmaram Shetye Jun 25 '13 at 16:17

1 Answers1

3

Please read the Python tutorial in particular, the section about classes.

You will find tons of great OO Python tutorials online, like here or here.

Getting the return value of a function

 return_value = my_function(my_argument)

For example, using the interpreter by running python on the command line:

➤ python
Python 2.7.2+ (default, Jul 20 2012, 22:15:08) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> def square(x):
...     return x*x
... 
>>> four_squared = square(4)
>>> four_squared
16

Cleaning up the code:

Your code doesn't make sense.

  1. What is nmaidcount().finallist?
  2. Where do you define finallist in your program?
  3. Why do you have if True? This will always be the case!

This is what I think you mean:

tsvs = glob.glob(sys.argv[1])

class StatFind:
  # This is the instance initialiser. 
  def __init__(self,tsvs):
    self.tsvs=tsvs 

    # here we define every instance of StatFind to have an attribute
    # called 'finallist' which will be accessible by all methods
    self.finallist = [] 

    # We do our initialisation here, when we initialise our object,
    # instead of in a separate method.
    for path in self.tsvs:
      finalist.append(do_something(path))

  def inserttomongo(self): 
    # The 'self' parameter is automagically set up by python to 
    # refer to the instance when 'insertmongo()' is called on an instance
    # for example, myInstance.insertmongo(), in which case 
    # self will be 'myInstance'
    mongo=pymongo.Connection('localhost')
    mongo_db=mongo['sample']
    mongo_collection=mongo_db['users']

    for dictvalue in self.finallist: 
      insert_id=mongo_collection.insert(dictvalue)

s=StatFind(tsvs) #This will call __init__() behind the scenes.
s.inserttomongo()
brice
  • 24,329
  • 7
  • 79
  • 95
  • Hi. The process and appending to finallist is in another method. nmaidcount is a typo, it was supposed to be ncount. I still dont seem to get the solution. Please help – user1189851 Jun 25 '13 at 17:02
  • 1
    @user1189851 it sounds like you're new to Python and to programming in general. The best advice I can give you (besides walking you through it myself) is to run the command line interpreter (`python` on the command line) and use it to go through a tutorial. You could also try [this online tutorial](http://www.learnstreet.com/lessons/study/python) – brice Jun 25 '13 at 17:12
  • the idea is that functions can be replaced by their return values. for example, if you have a function `def foo(x): return x*x` then you can replace any call to this function by the result. for example: `my_variable = foo(4)` would be the same as `my_variable = 16` – brice Jun 25 '13 at 17:15