Thursday, November 30, 2017

dictionary

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> myList=[1,2,3,4,5]
>>> import random
>>> random.shuffle(myList)
>>> print (myList)
[2, 4, 3, 5, 1]
>>> #dictionary
>>> #hash
>>> #key value pair
>>> products={"IPad" : 850, "Surface" : 1200, "Kindle" : 100, "Galaxy" : 500}
>>> print(products["Kindle"])
100
>>> 

Wednesday, November 29, 2017

Security SQL from Afternoon Class

select * from Person

Create login bob with password='password'
use Community_Assist
Create user bobuser for login bob
Create role grantapplicationrole
Grant select, insert on GrantRequest to grantapplicationRole
Grant exec on usp_Register to grantapplicationrole
Grant exec on usp_login to grantApplicationrole
Grant select on GrantType to grantApplicationrole
alter role grantApplicationrole add member bobuser

Tuesday, November 28, 2017

Classes and lists

This shows a partial deck class using the Card class from earlier in this blog

from CardClass import Card

class deck:
    def __init__ (self):
        self.cards=[]
        self.populateDeck()

    def populateDeck(self):
        suits=["h","d", "c", "s"]
        for s in suits:
            for i in range(1,14):
                card=Card(i,s)
                self.cards.append(card)

    def getDeck(self):
        return self.cards


def main():
    d=deck()
    myDeck=d.getDeck()
    for c in myDeck:
        print(c)

main();
    

        

here are the grade, and student classes.

#Get GPA
#The grade class stores the information
#for a grade
class grade:
    def __init__(self,course,gpoint,credit):
        self.course=course
        self.gpoint=gpoint
        self.credit=credit
    #allow another class to get the values
    def getCourse(self):
        return self.course
    def getGradePoint(self):
        return self.gpoint
    def getCredits(self):
        return self.credit
    def __str__(self):
        return self.course + " " + str(self.gpoint) + " " + str(self.credits)

#student class includes GPA generated from a list of grades
class student:
    def __init__(self,sid,name,email):
        self.sid=sid
        self.name=name
        self.email=email
        self.grades=[]#empty list for grades

    #this method adds grade objects to the grades list
    def addGrades(self, gradepoint):
        self.grades.append(gradepoint)

    def calculateGPA(self):
        #initialize variables
        self.gpa=0
        weight=0
        total=0
        #make sure the list is not empty
        if len(self.grades) != 0:
            #loop throug the objects in the grades list
            #get the weight by multiplying credits and grades
            for g in self.grades:
               weight += g.credit * g.gpoint
               total += g.credit #get total credits
            #caluclate GPA
            self.gpa=weight / total

    def __str__(self):
        self.calculateGPA() #call the calculate method
        #string to be output if you print the class
        return self.sid + " " + self.name + " " + self.email + " " + str(self.gpa)


def main():
    #initialize the student object
    s=student("122453","John Depp",'jd@gmail.com')
    stop="n"
    #loop to enter grades
    while stop=="n":
        course = input("Enter the course name: ")
        gp = float(input("Enter the final grade: "))
        cr=int(input("Enter the number credits: "))
        #create a grade object
        g=grade(course, gp, cr)
        #add it to the student's grade list
        s.addGrades(g)
        stop = input("Do you want to stop? 'n' to continue ")
    print(s)#print the __str__ from student
        
            
main() 














    

Monday, November 20, 2017

Mongo Creating the Database and adding records--morning

Here are the JSON records we created

zoo
animals
[{
  name : "Zack the Zebra",
  type : "Zebra",
  habitat : "African Savannah",
  age : "5 years",
  diet : "grass"
},
{
  name : "Ellie the Elephant",
  type : "Indian Elephant",
  habitat : "East Asian Jungle",
  age : "22 years",
  diet : "Peanuts"
},
{
  name : "Gennie the Giraff",
  type : "Giraff",
  habitat : "African Savannah",
  diet : "leaves"
}]

habitats
[{
   name : "African Savanah",
   description : "grass trees and exotic animals",
   inhabitants : ["Zack the Zebra", "Gennie the Giraff"]
},
  
{
   name : "East Asian Jungle",
   description : "Wet tropical, palms and ferns",
   inhabitants : "Ellie the Elephant"
}]
  

A picture of the insert of animals

Afternoon class

productReviews
products
[
{
   name : "Ipad",
   maker : "Apple",
   price : 830.99,
   color : "silver",
   quantity : 14, 
   rating : [4,3,5,3, 5]
},
{
   name : "Apple Watch",
   maker : "Apple",
   price : 400.99,
   color : ["silver","white", "black"],
   quantity : 20,
   rating : [4,3,5,3,5,2,5]
},
{
   name : "Surface",
   maker : "Microsoft",
   price : 1200.99,
   color : ["blue","white", "black"],
   quantity : 5 
}
]

reviews
[{
    email : "spconger@gmail.com",
    product : "Ipad",
    date : "11/20/2017",
    rating : 4,
    review : "Ipads are fun but hard to do real work on" 
},
{
   email : "Joe@msn.com",
   product : "Surface",
   date : "11/20/2017",
   rating : 4
}]

Thursday, November 16, 2017

Mileage and Card Classes

Mileage class

#miles per gallon
# as class

class Mileage:
    def __init__(self, miles, gallons):
        self.miles=miles
        self.gallons=gallons
        

    def setMiles(self, miles):
        self.miles=miles

    def mpg(self):
        self.milesPerGallon=self.miles / self.gallons

    def getMPG(self):
        return self.milesPerGallon

    def __str__(self):
        self.mpg()
        return "Your mpg is " + str(self.getMPG())
    

def main():
    mls = float(input("How many total miles "))
    gals = float(input("How many gallons "))
    mileage=Mileage(mls, gals)
    print (mileage)

main()











    

Class version of Card Class

#Card
class Card:
    def __init__(self, rank, suit):
        self.rank=rank
        self.suit=suit
        self.value=0
        
    def getRank(self):
        return self.rank

    def getSuit(self):
        return self.suit

    def getValue(self):
        if self.rank > 10:
            self.value=10
        else:
            self.value=self.rank
        return self.value
    
    def __str__(self):
        self.name=""
        su=""
        if self.suit =="d":
            su="diamonds"
        elif self.suit=="h":
            su="hearts"
        elif self.suit=="s":
            su="spades"
        else:
            su ="clubs"
        if self.rank >1 and self.rank< 11:
            self.name=str(self.rank) + " of " + su
        if self.rank==1:
            self.name="the ace of " + su
        if self.rank==11:
            self.name="the jack of " + su
        if self.rank==12:
             self.name="the queen of " + su
        if self.rank==13:
             self.name="the king of " + su
        return self.name
        
        
def main():
    
    c=Card(13,"h")
    print(c)
    print(c.getValue())

main()

    
    





    

Modified version of Card Class

#Card
class Card:
    def __init__(self, rank, suit):
        self.rank=rank
        self.suit=suit
        self.value=0
       
        
    def getRank(self):
        return self.rank

    def getSuit(self):
        return self.suit

    def getValue(self):
        if self.rank > 10:
            self.value=10
        else:
            self.value=self.rank
        return self.value

    def getSuit(self):
        self.su=""
        if self.suit =="d":
            self.su="diamonds"
        elif self.suit=="h":
            self.su="hearts"
        elif self.suit=="s":
            self.su="spades"
        else:
            self.su ="clubs"
        return self.su

    
    
    def __str__(self):
        
        if self.rank >1 and self.rank< 11:
            self.name=str(self.rank) + " of " + self.getSuit()
        if self.rank==1:
            self.name="the ace of " + self.getSuit()
        if self.rank==11:
            self.name="the jack of " + self.getSuit()
        if self.rank==12:
             self.name="the queen of " + self.getSuit()
        if self.rank==13:
             self.name="the king of " + self.getSuit()
        return self.name
        
        
def main():
    
    c=Card(13,"h")
    print(c)
    print(c.getValue())

main()

    
    





    

Wednesday, November 15, 2017

json Examples

--Books and Book reviews-Reviewer Registered
Books, Authors, Reviewers, Reviews, publishers

Books
{
  _id : 1,
  title : "Grapes of Wrath",
  author : "John Steinbeck",
  pubYear : "1939",
  descript : "A story of one family's journey to  California during the dustbowl"
}
{
  _id : 2,
  title : "Best Scifi and Fantasy 2015",
  author : ["Neil Gaimen", "C. S. Lewis", "Ray Bradbury"],
  pubYear : "2015",
  descript : "A collection of classic scifi stories"
}

Author
{
   _id : 1,
   authorName : "Neil Gaimen",
   nationality : "English",
   book : [
            {
      title : "Neverwhere",
             descript: "A man becomes lost in the London underground"
      },
             {
               title : "American Gods",
               descript : "Ancient gods try to survive in modern America"
       }
            ]
}

  

Afternoon Example

movie
{
  _id : 1,
  title : "Step Brothers",
  year : 2008,
  actor : ["John C. Riley", "Will Ferrell"],
  director : "Adam McKay"
}

actor
{
   _id : 1,
   actor_name : "Will Ferrell",
   movies :[
            {title: "Step Brothers", year : 2008},
            {title: "Elf", year : 2003},
            {title: "Anchorman", year : 2004}
           ]

}

Thursday, November 9, 2017

First Classes

#First class example
#dice

from random import randrange

class die:
    def __init__(self, sides):
        self.sides=sides
        self.value=1

    def roll(self):
        self.value=randrange(1,self.sides+1)

    def getValue(self):
        return self.value

    def setValue(self, value):
        self.value=value


def main():
    again="y"
    num=int(input("How many sides "))
    while again=="y":
    
        die1=die(num)
        die1.roll()
        die2=die(num)
        die2.roll()
        print (die1.getValue(),die2.getValue())
        again=input("Enter y to roll again ")

main()
                            
    
    

Two classes

#item and sale and receipt

class item:
    def __init__(self, name, price):
        self.name=name
        self.price=price
        
    def getName(self):
        return self.name

    def getPrice(self):
        return self.price
    

class cart:
    def __init__(self):
        self.items=[]
        
    def addItem(self, product):
        self.items.append(product)

    def getTotal(self):
        total=0
        for i in self.items:
            total+=i.price
        return total

def main():
    more = "y"
    c=cart()
    while more=="y":
        name = input("Item Name ")
        price = float(input("Item Price "))
        product=item(name, price)
        c.addItem(product)
        more = input("y to continue")
    print (c.getTotal())

main()
        
            
        
            
        

Wednesday, November 8, 2017

MySQL SQL Morning

Use Sakila;
Select * from Actor;
Select * from Film;
Select * from film_actor;

Select first_name, last_name,
title, description, release_year,
rating
From actor
inner join film_actor
on actor.actor_id=film_actor.actor_id
inner join film
on film.film_id=film_actor.film_id
Where first_name like 'Pen%';

Insert into Actor(first_name,last_name,last_update)
values('Bobo','Dodo', current_timestamp());

Select * from Actor where actor_id=201;

Select last_insert_id();

Update Actor 
Set Last_name = 'Dodson'
Where Actor_id=201;

Update Actor 
Set Last_name = 'Dodson',
first_name='Bob',
last_update=current_timestamp()
Where Actor_id=201;

CRUD  /*Create Read Update Delete*/

Delete from actor;
Delete from actor where actor_id = 201;

start transaction;

Update actor 
Set last_name = 'Smith'
Where actor_id =1;

Select * from Actor;

rollback;
Commit;

Thursday, November 2, 2017

Guessing Game Python

here is the github version

#guessing game.
#The game gets a random number between 1 and 1000.
#The player gets 10 guesses.
#With each guess the game tells them if
#they are too high or too low or correct.
#They start off with 10 points.
#Each wrong guess subtracts one point.
#When they are done it displays their score and
#asks if they want to play again.
#If they say yes it restarts the game. I
#f not it shows their previous scores and the
#average of those scores and says goodbye. 
#You should break the game into functions.
#It involves for loops, while loops and ifs.
#***************steps*****************
#Get random number
#get a guess
# check if guess is lower, higher or equal to random
#Do 10 times
#Get Score
#Ask if they want to play again
#if yes do game again
#if no exit and give overall score

from random import randrange

def getRandom():
    number = randrange(1,1001)
    return number

def getGuess():
    guess = int(input("Enter a guess between 1 and 1000 "))
    return guess
                
def evaluateGuess(guess,number):
    correct=0
    if guess > number:
        print ("Too high")
    elif guess < number:
        print ("Too low")
    else:
        print ("Got it!")
        correct=1
    return correct

def getScore(number):
    
    score=10
    for i in range(10):
        guess=getGuess()
        correct=evaluateGuess(guess, number)
        if correct==1:
            break
        score=score-1
    return score


def gamePlay():
    scores=[]
    playAgain="y"
    while playAgain=="y":
        number = getRandom()
        #print(number)
        score=getScore(number)
        scores.append(score)
        print("Your score is",score)
        playAgain=input("Do you want to play again? y/n ")
        playAgain=playAgain.lower()
    return scores

def finalScores():
    scores=gamePlay()
    total=0
    average=0
    print ("your scores are: ")
    for item in scores:
        print(item)
        total += item
    average=total/len(scores)
    print ("your average is ", average)
        

def main():
    finalScores()

main()