Thursday, September 1, 2016

Python Objects. The Data Layer

Preliminary Grousing


Let me just say from the outset that I hate the dummy self variable used in Python classes. Rather than give a class variable class scope you have to pass the self variable to every method and then do

self.variable

to access that variable within a method. I suppose it functions a lot like the this key word in C# and Java, but that is only required when you are trying to disambiguate two variables of the same name, one local and one at class scope. One reason for the complaint is it took me forever to figure this out--one of the problems of learning a language as you are writing it.

I am also not sure what to make of the "_init_" method which is not quite a constructor. . .

And then there is the loose typing which makes me uncomfortable...and is every array actually a list?

Anyway, with some struggles I made progress. The goal was to not only make a Graphical interface, but to do the whole program in an object oriented way.

The Data Objects


First I made a simple object for the product itself

class ProductClass(object):
    """mongo product class"""
    #This class stores product information
    def __init__(self, product, price, quantityAvailable):
        self.product = product
        self.price = price
        self.quantityAvailable=quantityAvailable

Then I create a class to handle the product class. It converts the json into objects and stores the objects in a list. Then it provides methods for returning product names and an individual project

import json
import Product
class ProductManagement(object):
    """manages productclass objects"""
    #This class manages the productClass objects
    #The gets the json results and converts
    #them to ProductClass objects
    #which are then stored in a list
    #It has methods to create the object
    #and store it.
    #Return a list of just the product names
    #and one to return just an individual
    #project that matches a product name
    prodList=[]
    def _init_(self):
        prodlist=self.prodList
         
    
    #method for converting json to objects
    def jsonToObject(self,prod):
        
        product=prod["product"]
        price=prod["price"]
        quantityAvailable=prod["quantityAvailable"]
        productObj = Product.ProductClass(product, price,quantityAvailable)
        self.prodList.append(productObj)

    #method for returning product names
    def getProducts(self):
        products=[]
        
        for index in range(len(self.prodList)):
           
            self.prodList[index].product
            products.append(self.prodList[index].product)

        return products
            
    #method for returning single product info by name
    def getProductsByName(self, productName):
        info
        for index in range(len(self.prodList)):
            if self.prodList[index].product==productName:
                info=self.prodList[index].product + " "
                + self.prodList[index].price + " " 
                + self.prodList[index].quantityAvailable
                break
        return info

So here is my review object.

class Review(object):
    """This class store review info as an object"""
def _init_(self, product, date, email, rating, reviewText):
    self.product=product
    self.date=date
    self.email=email
    self.rating=rating
    self.reviewText=reviewText

Here is the ReviewManager class. For the moment it is quite simple. It just converts the Json into objects and adds them to a list. It has one additional method that returns the list.

import Review
class ReviewManager(object):
    """description of class"""
    reviewList=[]

    def jsonToObject(self,rev):
        
        product=rev["product"]
        date=rev["date"]
        email=rev["email"]
        rating=rev["rating"]
        text=rev["reviewText"]
        revObj = Review.Review(product,date,email,rating,reviewText)
        self.reviewList.append(revObj)

    def getReviews(self):
        return self.reviewList

Here is the data class that handles all the actual contact with the Mongo Database. It is really quite simple once you get the hang of it.

from pymongo import MongoClient

class MongoData(object):
    """Handles all Mongo transactions"""
    #this class handles all direct Mongo transactions
    #It has methods to return all products
    #only those product based on criteria
    #and a method to insert a new product
    #in addition it has methods for adding
    #reviews and getting all reviews
    #for a particular product

    client=MongoClient()
    db=client.TechStore

    def _init_(self):
        self.client=MongoClient()
        self.db=client.TechStore

    #get all products
    def getCursorAllProduct(self):
        cursor=self.db.productCollection.find()
        self.client.close()
        return cursor
    
    #get products based on some criteria
    def getCursorProductCriteria(self, criteria):
        cursor=self.db.productCollection.find(criteria)
        self.client.close()
        return cursor

    #insert a new product
    def insertProduct(self, document):
        result=self.db.productCollection.insert_one(document)
        self.client.close()
        return result

    #insert a review
    def insertReview(self, document):
         result=self.db.reviewCollection.insert_one(document)
         self.client.close()
         return result

     #return reviews based on product
    def getReviewbyProduct(self, productName):
        criteria = { "product" : productName}
        cursor = self.db.reviewCollection.find(criteria)
        return cursor

Next I will develop a forms or forms with Tkinter

No comments:

Post a Comment