Can't connect to MongoDB database and collection through class variables - python

Just as a preface, I understand that there are easier ways to accomplish much of what i'm trying to do, and the following question is for purposes of learning how to build classes and instantiate a database connection within that class.
I'm building a class that right now just takes in two variables; the name of MongoDB database, and the collection name from that database. I am trying to instantiate the connection of this database and the collection in the init function of this class. The problem I am having is that the init function is connecting to the database of the actual name of the variable instead of the variable's actual assignment. More specifically, if I instantiate,
>>>salesChar = MongoDumps("sales","char")
and then I call,
>>>salesChar.db.name
it will instead connect to the "dBase" (the name of the variable) database instead of the "salesChar" (the assignment of the dBase variable) database. Please view code below,
import pymongo
from pymongo import MongoClient
class MongoDumps():
"""Data Dumping into MongoDB"""
def __init__(self, dBase, dumpCollection):
self.dBase = dBase
self.dumpCollection = dumpCollection
client = MongoClient()
self.db = client.dBase
self.collection = self.db.dumpCollection
I've tried a combination of strategies and none seem to work with a similar result in each one. Are there certain limitations to using assignments in a class? Thanks for your help!

Use getattr to get property by string. As documentation says, getattr(x, 'foobar') is equivalent to x.foobar. Your code should look like:
class MongoDumps():
def __init__(self, dBase, dumpCollection):
self.dBase = dBase
self.dumpCollection = dumpCollection
client = MongoClient()
self.db = getattr(client, dBase)
self.collection = getattr(self.db, dumpCollection)
Then you can use this class to get collection by name:
salesChar = MongoDumps("sales", "char")
first = salesChar.collection.find_one()

Related

How to use ccxt methods like fetchticker, etc on user-defined variable, attribute?

I am relatively new to Python and Software development in general.
I have made the following code, and my query at this stage is,
How do I connect to CCXT library using a user defined parameter or variable?
`
class Exchange:
""" docstring goes here """
def __init__(self, ex_name):
self.name = ex_name
ex_obj = getattr(ccxt, ex_name)
self.ex = ex_obj({
'enableRateLimit': True
})
print(self.ex) # just for checking connection
Exchange('binance')
How do I achieve THIS result?
exchange = ccxt.binance()
print(exchange.fetch_ticker('BTC/BUSD'))
`
How do I get to use the ccxt methods on custom/ user-defined variables or attributes.

How to use a var in multiple methods in python

I have a few files in my code that speak to the database
This might look something like this:
def addUser():
# some code
def verifyUser():
# some code
def addStuffToDB():
# some code
In all of the above I need to use a variable - let's call it db - that holds a reference to the database (as opposed to redefining it in every function)
How would I do this? How can I have functions in one or more files that all make use of a variable (in this case db)
Thanks
If you have all this functions inside the same file, it is enough to just define variable db outside any function (this will make it global). Now all functions will be able to see db variable. But if you change db inside a function it will not change outside the function.
If you have this variable in another file you can simple import it like
from file_name import db
As #ddejohn said, you should wrap your functions in a class, so the variable self.db would have a class scope.
class DB():
def __init__(self) -> None:
self.db = "DB_connection or something..."
def addUser(self):
#Some code, acess db variable with self.db
def verifyUser(self):
#Some code, acess db variable with self.db
def addStuffToDB(self):
#Some code, acess db variable with self.db
MyDB = DB()
MyDB.addUser()
Thanks for asking the question.
You need to pass db as argument while calling the funcs like the following
db = "some referenec"
def addUser(database):
## now you can use db
# some code
def verifyUser(database):
# some code
## now you can use db
def addStuffToDB(database):
# some code
## now you can use db
## while calling each func pass db as argument like this
addUser(db)
verifyUser(db)
addStuffToDB(db)
add a db paramenter to yout funcs:
controller.py:
def addUser(db): # some code
obj.add(db)
def verifyUser(db): # some code
obj.verify(db)
def addStuffToDB(db): # some code
obj.add_stuff(db)
Then, you can use as follows:
view.py
import db
from controller import addUser
addUser(db)

How to get data from Object Oriented Programming to mySQL?

How I can pass the data from object oriented programming to mysql in python? Do I need to make connection in every class?
Update:
This is my object orinted
class AttentionDataPoint(DataPoint):
def __init__ (self, _dataValueBytes):
DataPoint._init_(self, _dataValueBytes)
self.attentionValue=self._dataValueBytes[0]
def __str__(self):
if(self.attentionValue):
return "Attention Level: " + str(self.attentionValue)
class MeditationDataPoint(DataPoint):
def __init__ (self, _dataValueBytes):
DataPoint._init_(self, _dataValueBytes)
self.meditationValue=self._dataValueBytes[0]
def __str__(self):
if(self.meditationValue):
return "Meditation Level: " + str(self.meditationValue)
And I try to get the data to mysql using this coding.
import time
import smtplib
import datetime
import MySQLdb
db = MySQLdb.connect("192.168.0.101", "fyp", "123456", "system")
cur = db.cursor()
while True:
Meditation_Level = meditationValue()
Attention_Level = attentionValue()
current_time = datetime.datetime.now()
sql = "INSERT INTO table (id, Meditation_Level, Attention_Level, current_time) VALUES ('test1', %s, %s, %s)"
data = (Meditation_Level, Attention_Level, current_time)
cur.execute(sql, data)
db.commit()
db.close()
Update:
DataPoint
class DataPoint:
def __init__(self, dataValueBytes):
self._dataValueBytes = dataValueBytes
I am thinking of something like a class that has a function for connection and you just pass the instance of that class to every class where you need the connection (here the db instance), or may be not!
It's just an idea that I used in a other languages.
A few options:
Implement a save/load method for each DataPoint child to build the select/insert/update sql commands according with the needs of each class. Here is a basic tutorial: Python MySQL Database Access
Implement a class to build the sql commands above automatically from your class attributes.
The above is called Object Relational Mapping and, so, you don't need to reinvent the wheel: https://www.fullstackpython.com/object-relational-mappers-orms.html
I'd go with the first if it is a simple project with no chance to become big and with an ORM tool for all the other cases.
The most popular for python is SQLAlchemy:
http://docs.sqlalchemy.org/en/latest/orm/tutorial.html

Python descriptors to chain methods

I'm trying to figure out how to chain class methods to improve a utility class I've been writing - for reasons I'd prefer not to get into :)
Now suppose I wanted to chain a chain class methods on a class instance (in this case for setting the cursor) e.g.:
# initialize the class instance
db = CRUD(table='users', public_fields=['name', 'username', 'email'])
#the desired interface class_instance.cursor(<cursor>).method(...)
with sql.read_pool.cursor() as c:
db.cursor(c).get(target='username', where="omarlittle")
The part that's confusing is I would prefer the cursor not to persist as a class attribute after .get(...) has been called and has returned, I'd like to require that .cursor(cursor) must be first called.
class CRUD(object):
def __init__(self, table, public_fields):
self.table = table
self.public_fields = public_fields
def fields(self):
return ', '.join([f for f in self.public_fields])
def get(self, target, where):
#this is strictly for illustration purposes, I realize all
#the vulnerabilities this leaves me exposed to.
query = "SELECT {fields} FROM {table} WHERE {target} = {where}"
query.format(fields=self.fields, table=self.table, target=target,
where=where)
self.cursor.execute(query)
def cursor(self, cursor):
pass # this is where I get lost.
If I understand what you're asking, what you want is for the cursor method to return some object with a get method that works as desired. There's no reason the object it returns has to be self; it can instead return an instance of some cursor type.
That instance could have a back-reference to self, or it could get its own copy of whatever internals are needed to be a cursor, or it could be a wrapper around an underlying object from your low-level database library that knows how to be a cursor.
If you look at the DB API 2.0 spec, or implementations of it like the stdlib's sqlite3, that's exactly how they do it: A Database or Connection object (the thing you get from the top-level connect function) has a cursor method that returns a Cursor object, and that Cursor object has an execute method.
So:
class CRUDCursor(object):
def __init__(self, c, crud):
self.crud = crud
self.cursor = however_you_get_an_actual_sql_cursor(c)
def get(self, target, where):
#this is strictly for illustration purposes, I realize all
#the vulnerabilities this leaves me exposed to.
query = "SELECT {fields} FROM {table} WHERE {target} = {where}"
query.format(fields=self.crud.fields, table=self.crud.table,
target=target, where=where)
self.cursor.execute(query)
# you may want this to return something as well?
class CRUD(object):
def __init__(self, table, public_fields):
self.table = table
self.public_fields = public_fields
def fields(self):
return ', '.join([f for f in self.public_fields])
# no get method
def cursor(self, cursor):
return CRUDCursor(self, cursor)
However, there still seems to be a major problem with your example. Normally, after you execute a SELECT statement on a cursor, you want to fetch the rows from that cursor. You're not keeping the cursor object around in your "user" code, and you explicitly don't want the CRUD object to keep its cursor around, so… how do you expect to do that? Maybe get is supposed to return self.cursor.fetch_all() at the end or something?

Methods on descriptors

I'm trying to implement a wrapper around a redis database that does some bookkeeping, and I thought about using descriptors. I have an object with a bunch of fields: frames, failures, etc., and I need to be able to get, set, and increment the field as needed. I've tried to implement an Int-Like descriptor:
class IntType(object):
def __get__(self,instance,owner):
# issue a GET database command
return db.get(my_val)
def __set__(self,instance,val):
# issue a SET database command
db.set(instance.name,val)
def increment(self,instance,count):
# issue an INCRBY database command
db.hincrby(instance.name,count)
class Stream:
_prefix = 'stream'
frames = IntType()
failures = IntType()
uuid = StringType()
s = Stream()
s.frames.increment(1) # float' object has no attribute 'increment'
Is seems like I can't access the increment() method in my descriptor. I can't have increment be defined in the object that the __get__ returns. This would require an additional db query if all I want to do is increment! I also don't want increment() on the Stream class, as later on when I want to have additional fields like strings or sets in Stream, then I'd need to type check the heck out of everything.
Does this work?
class Stream:
_prefix = 'stream'
def __init__(self):
self.frames = IntType()
self.failures = IntType()
self.uuid = StringType()
Why not define the magic method iadd as well as get and set. This will allow you to do normal addition with assignment on the class. It will also mean you can treat the increment separately from the get function and thereby minimise the database accesses.
So change:
def increment(self,instance,count):
# issue an INCRBY database command
db.hincrby(instance.name,count)
to:
def __iadd__(self,other):
# your code goes here
Try this:
class IntType(object):
def __get__(self,instance,owner):
class IntValue():
def increment(self,count):
# issue an INCRBY database command
db.hincrby(self.name,count)
def getValue(self):
# issue a GET database command
return db.get(my_val)
return IntValue()
def __set__(self,instance,val):
# issue a SET database command
db.set(instance.name,val)

Categories