Writing a simple MongoDB module in python - python

I have started writing a simple module for mongodb to use. I am new to python and I have been a problem with the code I wrote:
import pymongo
class mongoDB():
conn = object
def __init__(self):
global conn
self.conn = pymongo.Connection("localhost",27017)
def CreateCollection(self,name =""):
self.dbCollection = conn.name
return self.dbCollection
if __name__ == '__main__':
database = mongoDB
collection = database.CreateCollection("Hello")
Firstly I think there are probably few things wrong with my code if you can spot it and correct me. Also I am keep getting this erro:
collection = database.CreateCollection("Hello")
TypeError: unbound method CreateCollection() must be called with mongoDB
instance as first argument (got str instance instead)
I want to be able to create the connection in the constructor of the class and then have a method for creating a collection and returning it, and also another method to insert delete and update the entities

So, syntax wise you have a number of problems. It looks like you're mixing a couple of tutorials in different ways. So, firstly I'll explain what is going on with your code and explain why you're seeing what you're seeing:
import pymongo
class mongoDB(): # you don't need ()'s here - only if you are inheriting classes
# you could inherit from object here, which is a good practice
# by doing class mongoDb(object):, otherwise you can just take
# them out
conn = object # here, you're defining a class member - global for all instances
# generally, you don't instantiate an object pointer like this,
# you would set it to None instead. It won't fail doing this,
# but it's not "right"
def __init__(self):
# the __init__ method is the constructor method - this will
# allow you to initialize a particular instance of your class, represented
# by the self argument. This method is called when you call the class, i.e.
# inst = mongoDb()
# in this case, the conn variable is not a global. Globals are defined
# at the root module level - so in this example, only pymongo is a global
# conn is a class member, and would be accessed by doing mongoDB.conn
global conn
# with that being said, you're initializing a local variable here called conn
# that is not being stored anywhere - when this method finishes, this variable
# will be cleaned up from memory, what you are thinking you're doing here
# should be written as mongoDB.conn = pymongo.Connection("localhost", 27017)
conn = pymongo.Connection("localhost",27017)
def CreateCollection(name =""):
# there is one of two things you are trying to do here - 1, access a class
# level member called conn, or 2, access an instance member called conn
# depending on what you are going for, there are a couple of different ways
# to address it.
# all methods for a class, by default, are instance methods - and all of them
# need to take self as the first argument. An instance method of a class
# will always be called with the instance first. Your error is caused because
# you should declare the method as:
# def CreateCollection(self, name = ""):
# The alternative, is to define this method as a static method of the class -
# which does not take an instance but applies to all instances of the class
# to do that, you would add a #staticmethod decorator before the method.
# either way, you're attempting to access the global variable "conn" here,
# which again does not exist
# the second problem with this, is that you are trying to take your variable
# argument (name) and use it as a property. What python is doing here, is
# looking for a member variable called name from the conn object. What you
# are really trying to do is create a collection on the connection with the
# inputed name
# the pymongo class provides access to your collections via this method as a
# convenience around the method, create_collection. In the case where you
# are using a variable to create the collection, you would call this by doing
# conn.create_collection(name)
# but again, that assumes conn is what you think it is, which it isn't
dbCollection = conn.name
return dbCollection
if __name__ == '__main__':
# here you are just creating a pointer to your class, not instantiating it
# you are looking for:
# database = mongoDB()
database = mongoDB
# this is your error, because of the afore mentioned lack of 'self' argument
collection = database.CreateCollection("Hello")
I'd say have a look through the Pep-8 (http://www.python.org/dev/peps/pep-0008/) coding style guides (very helpful) to learn about how to make your code "flow" pythonically.
Having gone through your code to explain what is going on - this is what you are ultimately trying to do:
import pymongo
class MongoDB: # Classes generally are camel-case, starting with uppercase
def __init__(self, dbname):
# the __init__ method is the class constructor, where you define
# instance members. We'll make conn an instance member rather
# than a class level member
self._conn = pymongo.Connection("localhost", 27017)
self._db = self._conn[dbname]
# methods usually start with lowercase, and are either camel case (less desirable
# by Python standards) or underscored (more desirable)
# All instance methods require the 1st argument to be self (pointer to the
# instance being affected)
def createCollection(self, name=""):
return self._db[name]
if __name__ == '__main__':
# you want to initialize the class
database = MongoDB("Hello")
collection = database.createCollection("MyTable")
Given that tho - what is the goal of writing this class wrapper? The same could be written as:
import pymongo
conn = pymongo.Connection('localhost', 27017)
database = conn["Hello"]
collection = database["MyTable"]
If you're trying to create a larger API wrapped around the pymongo database, then I'd recommend looking into some ORM modules that have already been built. There are some out there - not 100% sure which ones are available for MongoDB, but the one I use (I am biased, I wrote it) is called ORB, and can be found at http://docs.projexsoftware.com/api/orb

This is not a specific answer to how to solve your problem, but instead an answer for how to step through what you want to do and work on simpler problems as they arise:
1) Forget about classes at first, and instead
2) Use the Python command line or a Python program like IDLE,
3) To establish your goals by writing calls to open the MongoDB database to accomplish your task or tasks. In other words, write the simplest code to accomplish your goals before worrying about classes.
4) Once you get that done, and feel good to move on, then write a test class using the documentation. My link is one of many you could find.
5) I think part, but not all, of your problem is your class is not set up correctly. My class -- not shown completely -- is as follows:
class GlobalData:
def set_xfer_date(self, value):
self.xfer_date = value
self.date_str = str(value)
self.xfer_date_base = self.date_str[0:10] + " " + "00:00:00"
# Now build tomorrow.
self.xfer_date_tomorrow = datetime.today() + timedelta(1)
self.date_str_tomorrow = str(self.xfer_date_tomorrow)
self.xfer_date_tomorrow = \
self.date_str_tomorrow[0:10] + " " + "00:00:00"
self.section_totals = {}
self.billable_reads = {}
def set_xfer_fnam_out(self, value):
self.xfer_fnam_out = value
def set_xfer_dir_in(self, value):
self.xfer_dir_in = value
.
.
.
def get_billable_reads(self):
return self.billable_reads
One of the problems I see is you are not referring to data using self.
Good luck.

Related

Is this sound software engineering practice for class construction?

Is this a plausible and sound way to write a class where there is a syntactic sugar #staticmethod that is used for the outside to interact with? Thanks.
###scrip1.py###
import SampleClass.method1 as method1
output = method1(input_var)
###script2.py###
class SampleClass(object):
def __init__(self):
self.var1 = 'var1'
self.var2 = 'var2'
#staticmethod
def method1(input_var):
# Syntactic Sugar method that outside uses
sample_class = SampleClass()
result = sample_class._method2(input_var)
return result
def _method2(self, input_var):
# Main method executes the various steps.
self.var4 = self._method3(input_var)
return self._method4(self.var4)
def _method3(self):
pass
def _method4(self):
pass
Answering to both your question and your comment, yes it is possible to write such a code but I see no point in doing it:
class A:
def __new__(cls, value):
return cls.meth1(value)
def meth1(value):
return value + 1
result = A(100)
print(result)
# output:
101
You can't store a reference to a class A instance because you get your method result instead of an A instance. And because of this, an existing __init__will not be called.
So if the instance just calculates something and gets discarded right away, what you want is to write a simple function, not a class. You are not storing state anywhere.
And if you look at it:
result = some_func(value)
looks exactly to what people expect when reading it, a function call.
So no, it is not a good practice unless you come up with a good use case for it (I can't remember one right now)
Also relevant for this question is the documentation here to understand __new__ and __init__ behaviour.
Regarding your other comment below my answer:
defining __init__ in a class to set the initial state (attribute values) of the (already) created instance happens all the time. But __new__ has the different goal of customizing the object creation. The instance object does not exist yet when __new__is run (it is a constructor function). __new__ is rarely needed in Python unless you need things like a singleton, say a class A that always returns the very same object instance (of A) when called with A(). Normal user-defined classes usually return a new object on instantiation. You can check this with the id() builtin function. Another use case is when you create your own version (by subclassing) of an immutable type. Because it's immutable the value was already set and there is no way of changing the value inside __init__ or later. Hence the need to act before that, adding code inside __new__. Using __new__ without returning an object of the same class type (this is the uncommon case) has the addtional problem of not running __init__.
If you are just grouping lots of methods inside a class but there is still no state to store/manage in each instance (you notice this also by the absence of self use in the methods body), consider not using a class at all and organize these methods now turned into selfless functions in a module or package for import. Because it looks you are grouping just to organize related code.
If you stick to classes because there is state involved, consider breaking the class into smaller classes with no more than five to 7 methods. Think also of giving them some more structure by grouping some of the small classes in various modules/submodules and using subclasses, because a long plain list of small classes (or functions anyway) can be mentally difficult to follow.
This has nothing to do with __new__ usage.
In summary, use the syntax of a call for a function call that returns a result (or None) or for an object instantiation by calling the class name. In this case the usual is to return an object of the intended type (the class called). Returning the result of a method usually involves returning a different type and that can look unexpected to the class user. There is a close use case to this where some coders return self from their methods to allow for train-like syntax:
my_font = SomeFont().italic().bold()
Finally if you don't like result = A().method(value), consider an alias:
func = A().method
...
result = func(value)
Note how you are left with no reference to the A() instance in your code.
If you need the reference split further the assignment:
a = A()
func = a.method
...
result = func(value)
If the reference to A() is not needed then you probably don't need the instance too, and the class is just grouping the methods. You can just write
func = A.method
result = func(value)
where selfless methods should be decorated with #staticmethod because there is no instance involved. Note also how static methods could be turned into simple functions outside classes.
Edit:
I have setup an example similar to what you are trying to acomplish. It is also difficult to judge if having methods injecting results into the next method is the best choice for a multistep procedure. Because they share some state, they are coupled to each other and so can also inject errors to each other more easily. I assume you want to share some data between them that way (and that's why you are setting them up in a class):
So this an example class where a public method builds the result by calling a chain of internal methods. All methods depend on object state, self.offset in this case, despite getting an input value for calculations.
Because of this it makes sense that every method uses self to access the state. It also makes sense that you are able to instantiate different objects holding different configurations, so I see no use here for #staticmethod or #classmethod.
Initial instance configuration is done in __init__ as usual.
# file: multistepinc.py
def __init__(self, offset):
self.offset = offset
def result(self, value):
return self._step1(value)
def _step1(self, x):
x = self._step2(x)
return self.offset + 1 + x
def _step2(self, x):
x = self._step3(x)
return self.offset + 2 + x
def _step3(self, x):
return self.offset + 3 + x
def get_multi_step_inc(offset):
return MultiStepInc(offset).result
--------
# file: multistepinc_example.py
from multistepinc import get_multi_step_inc
# get the result method of a configured
# MultiStepInc instance
# with offset = 10.
# Much like an object factory, but you
# mentioned to prefer to have the result
# method of the instance
# instead of the instance itself.
inc10 = get_multi_step_inc(10)
# invoke the inc10 method
result = inc10(1)
print(result)
# creating another instance with offset=2
inc2 = get_multi_step_inc(2)
result = inc2(1)
print(result)
# if you need to manipulate the object
# instance
# you have to (on file top)
from multistepinc import MultiStepInc
# and then
inc_obj = MultiStepInc(5)
# ...
# ... do something with your obj, then
result = inc_obj.result(1)
print(result)
Outputs:
37
13
22

return a string from a function in a class in python

Cannot return a string from a python class to another python class.
Created two classes and need to get a string from one to the other. I keep getting the message -
'app.classes.xxx' has no attribute 'returnDBString'
Class1.py
from app.classes import DBManager
def CustomerSetup(self, user_id):
db = DBManager.returnDBString()
cnxn = pdc.connect(db)
DBManager.py
class DBManager():
def returnDBString():
return 'Database string and connection info'
def returnConnection():
return pdc.connect(returnDBString())
I was expecting to get back the actual string in the class, but I keep getting the error
app.classes.DBManager' has no attribute 'returnDBString
This has nothing to do with returning strings.
For some reason you have put your function inside a class, which just happens to have the same name as the module it is in. But Python is not Java; functions don't need to be in classes, and if you do define a class then you need to import it specifically, not just the module (or, at least, refer to it via the module).
In any case, you should remove the class declaration and just put your two functions directly at module level.
Decorate the method with #classmethod if it makes sense to have this method under DBManager
This is an issue of usability of module with classes.
There are 2 solution to your problem:
1) If you create class with member function in DBManager.py and want to use it some different file.Then, first thing which should come into you mind that a class is nothing without an class object. You need to create an object of your class and call into returnDBString() with that object. keep in mind you need to create constructor init() as well of you class, its incomplete in your code.
def CustomerSetup(self, user_id):
DBObject = DBManager()
db = DBObject.returnDBString()
2) Don't have class in DBManager.py, delete class and have same function without class. In this case your current code Class1.py would work.
You need to instantiate the class in Class1.py, and make pdc available to it:
db = DBManager.DBManager(pdc).returnDBString()
Note the parens, DBManager().
You'll also need to write your DBManager class like so:
class DBManager():
def __init__(self, pdx):
self.pdx = pdx
def returnDBString(self):
return 'Database string and connection info'
def returnConnection(self):
return self.pdc.connect(self.returnDBString())

python: encapsulate data without a class?

I have a module in python that provides various functions for performing database queries.
It looks like this:
def do_some_query(conn):
## logic goes here
def do_something_else(conn):
## logic goes here
Where conn is a database connection object. Of course, this means a lot of passing around of the conn object.
I'd like to allow the user to just set the connection object once for their session and then call the functions.
OBVIOUSLY putting the whole thing in a class is a good answer, but I dont want to have to make the user initialise a class each time they want to use the functions.
I.E, I dont want the API to be like this:
from database_module import DatabaseClass
db = DatabaseClass()
db.connect(...)
db.do_some_query()
But like this:
import database_module as dm
dm.connect(...)
dm.do_some_query()
Is there a way to either 'hide' the class from a user, or provide encapsulation without the class?
You can have a connect function that puts the connection in a global variable, and the other functions will use it.
_conn = None
def connect():
global _conn
_conn = ...
def query():
_conn.execute("SELECT ...")

SIngleton class does not retain value

First of all i would like to state that i am fairly new to Python programming, therefore my approach to solving the problem maybe incorrect, please let me know if that is the case
I am trying to use a Singleton class to store configuration params in my code and use it when needed. I run into an issue that the data stored in the Singleton in the initial creation is not retained in the subsequent calls to the Object.
Maybe the way i create the Singleton object is incorrect, but i was following the code samples that was on SO.
first here is my Singleton class
class Config(object):
__instance = None
dbserver = ""
def __new__(cls):
if cls.__instance == None:
__instance = object.__new__(cls)
__instance.name = "Configuration Object"
return __instance
Here is my initial creation of the Singleton instance
configurator = Config()
configurator.dbserver = dbserver
then subsequently i do the following and dbserver property returns empty string.
configurator = Config()
print configurator.dbserver
Thanks for your time reading.
You don't seem to be assigning the local variable __instance to the class variable cls.__instance.
However, if you're just doing this to store variables, I don't know why you need an instance at all. Just store them directly on the class - which is itself a first-class object in Python - or even use a module, which is more Pythonic.
Edit
Using the class itself wouldn't cause the code to be re-run each time: code at class level is only executed when the class is defined, which happens on first import.
But since the class has to live in a module (a file) somewhere, you could just as easily put all that code directly in the module, and import that where you need it. Again, code at module level is only executed on first import.

Python: inject new functions into an instance of class?

I tried to manipulate the __mro__ but it it read-only
The use case is as follow:
The Connection object created from pyodbc (a DBAPI) used to provide a property called 'autocommit'. Lately I have wrapped a SQLAlchemy db connection pool around the pyodbc for better resource management. The new db pool will return a _ConnectionFairy, a connection proxy class, which no longer exposes the autocommit property.
I would very much want to leave the thrid party code alone. So inheritance of _ConnectionFairy is not really an option (I might need to override the Pool class to change how it creates a connection proxy. For source code, please see here)
A rather not-so elegant solution is to change all occurance of
conn.autocommit = True
to
# original connection object is accessible via .connection
conn.connection.autocommit = True
So, I would like to know if it is possible at all to inject a set of getter, setter and property to an instance of _ConnectionFairy
You can "extend" almost any class using following syntax:
def new_func(self, param):
print param
class a:
pass
a.my_func = new_func
b = a()
b.my_func(10)
UPDATE
If you want to create some kind of wrappers for some methods you can use getattr and setattr to save original method and replace it with your implementation. I've done it in my project but in a bit different way:
Here is an example:
class A:
def __init__(self):
setattr(self, 'prepare_orig', getattr(self,'prepare'))
setattr(self, 'prepare', getattr(self,'prepare_wrapper'))
def prepare_wrapper(self,*args,**kwargs):
def prepare_thread(*args,**kwargs):
try:
self.prepare_orig(*args,**kwargs)
except:
print "Unexpected error:", sys.exc_info()[0]
t = threading.Thread(target=prepare_thread, args=args, kwargs=kwargs)
t.start()
def prepare(self):
pass
The idea of this code that other developer can just implement prepare method in derived classed and it will be executed in the background. It is not what you asked but I hope it will help you in some way.

Categories