Posts

Showing posts from 2018

Python 3.4 - Reading/ Writing Files

In python you may have a program or some data that you would like to store. You may have a file or bunch of files that you may need to work with. You can read files and write new files with your programs. By saving or linking to files in the same directory or elsewhere you can do this.  https://www.youtube.com/watch?v=Olq86NN6-Ko https://www.youtube.com/watch?v=Uh2ebFW8OYM

UML - Use Case Diagram

Image
UML stands for unified modeling language. It is used to create a blueprint for a project. A use case diagram is a list of steps needed to reach a goal. Its defines what is required and how those requirements are met. You will narrow down what you have to have. Before you create a use case diagram you need to create a description of the use case diagram. For example the above would look something like this: Use case descriptions includes who the actors are, what the triggers are, and other elements. Beta versions of a project have Shall requirements, should requirements can be done but can be done later. A use case diagram uses stereotypes to represent an element of your system. https://whatis.techtarget.com/definition/use-case-diagram https://www.youtube.com/watch?v=OkC7HKtiZC0

2nd Game.

import random from time import sleep def displayintro (): print ( "It's been a long time fighting." ) sleep( 1 ) print ( "You come to a crossroads." ) sleep( 1 ) print ( "You have two paths you can choose." ) sleep( 1 ) print ( 'One will lead to victory one will lead to dismay.' ) print () def choosepath (): path = "" while path != "1" and path != "2" : path = input ( 'What path do you choose? (1 or 2)' ) return path def checkpath (chosenpath): print ( "You head to down the path." ) sleep( 1 ) print ( "There is a battle ahead." ) correctPath = random.randint( 1 , 2 ) if chosenpath == str (correctPath): print ( "You win the battle and head home!" ) else : print ( "You lost and are forgotten in time." ) playagain = "Yes" while playagain == "Yes" ...

Django Practice

Oh Django, I understand you sort of. Basically the idea is a one-stop shop to make and develop a website. You are using Python, HTML, and CSS to create the things(apps) you want your website to have. Steps: Getting the necessary programs, software (PyCharm, Django, etc) Setting up your environment and setting up all of the apps. Creating your URLs and Templates. Creating your classes and what they will contain, type of info.

Python Practice

name = 'Greedy' condition = 'special' time = 'all of the time' print ( '{} is {} {}.' .format(name , condition , time)) def calc (x , y , operation): if operation == 'add' : return x + y elif operation == 'sub' : return x - y elif operation == 'mul' : return x * y elif operation == 'div' : return x / y running = calc( 14 , 2 , 'div' ) print (running) running2 = calc( 12 , 2 , 'add' ) print (running2) print (running + running2) xyz = [] names = [ 'Mud' , 'Jebidiyah' , 'Florian' , 'Hans' ] for i in list (names): xyz.append(i) for i in range ( 20 ): xyz.append(i) print (xyz) num_list = [ 10 , 3 , 45 , 65 , 40 , 7 , 6 , 9 , 100 ] def div_five (num): if num % 5 == 0 : return True else : return False # So this is basically saying that fives equals any number in the list that passes the # ...

Python 3.4 - Pep 8 Style Guide

This is a link to a style guide for Python. It is not mandatory, but if you are looking to see how it might be done this could point you in the right direction. https://www.python.org/dev/peps/pep-0008/?

Python 3.4 - Caesar Cipher

Caesar ciphers take a list of values and rotates through them to give them a different value. The program below takes from the alphabet and creates a cipher of the message. It returns many variations of the cipher then returns the actual message. import string import collections def caesar (rotate_string , number_to_rotate_by): upper = collections.deque(string.ascii_uppercase) lower = collections.deque(string.ascii_lowercase) upper.rotate(number_to_rotate_by) lower.rotate(number_to_rotate_by) upper = '' .join( list (upper)) lower = '' .join( list (lower)) return rotate_string.translate( str .maketrans(string.ascii_uppercase , upper)).translate( str .maketrans(string.ascii_lowercase , lower)) my_string = 'This is the message' my_string2 = caesar(my_string , 1 ) for i in range ( len (string.ascii_uppercase)): print (i , "|" , caesar(my_string2 , i)) /Users/mattfassnacht/PycharmProjects/untitled12/venv/bin/pyt...

Coding for UWAV - Views

Creating the apps for your website you need to have the site direct the user to certain pages. By adding what the website should do when a page is requested is stored in views. It gives a function response. def searchpage ( request ): return HttpResponse( "<h1>This is the searchpage!</h1>" ) https://www.youtube.com/watch?v=nAn1KpPlN2w

Coding for UWAV - Django Apps

In Django it seems like each functionality of a site, each page that does something different, is handled as an app. It is to be treated separately but part of the whole.  The map and search function is an app. The submitting page is an app.The user-page is an app. The joining page is an app. The setting page is an app. The event page is an app. Once installed the app contains several pre-made python files for specific purposes: __init__.py is the initializer (leave it alone) admin.py is the place where you have your admin controls apps.py is where you have your settings for this specific app models.py is the database and how you will store the data (i.e. classes, dictionaries, tables) tests.py is for tests to see if things are working views.py is for functions for user requests and responses https://www.youtube.com/watch?v=lcD0CDurxas&list=PL6gx4Cwl9DGBlmzzFcLgDhKTTfNLfX1IK

Django and coding for UWAV

I am first installing Django and all the necessary things that go with it. So, it's a complex process to install Django, but go to Django.   Actually you don't reeeeally need to go there. I would recommend finding a decent video on the steps to install, here's one: https://www.youtube.com/watch?v=cdkH6iQJrO0 From there it's a matter of learning what you can do with it. 

Python 3.4 - Global and Local Functions

https://www.youtube.com/watch?v=r9LtArXOYjk

Python 3.4 - Coding Practice

from collections import defaultdict my_dict = { 'name' : 'Jeff' , 'job' : 'teacher' , 'age' : 49 , 'salary' : 50000 } print (my_dict.get( 'job' )) print (my_dict[ 'name' ]) print (my_dict.get( 'rank' , 'not found' )) ##this will print the second value if the first is not found print ( sorted (my_dict.items())) print ( sorted (my_dict.keys())) my_dict[ 'last_name' ] = 'Johnson' my_dict[ 'middle name' ] = 'Bozo' # notice that the key doesn't have to be connected print (my_dict) del my_dict[ 'age' ] print (my_dict) my_dict.clear() print (my_dict) print ( '------------------------------' ) my_dict2 = { 'name' : 'Jeff' , 'job' : 'teacher' , 'age' : 49 , 'salary' : 50000 } for i in my_dict2.keys(): print (i) print ( '------------------------------' ) for i in my_dict2.values(): ...

Python 3.4 - String Formatting

String formatting is super useful when working with lists, tuples, and dictionaries. It allows you to do many things like calling on information and or doing things like datetime, etc. See below: import datetime person = {'name': 'Matt', 'age': 38} list_person = ['Matt', 38] sentence = 'My name is {} and I am {} years old.'.format (person['name'], person['age']) print(sentence) sentence2 = 'My name is {0} and I am {1} years old.'.format (person['name'], person['age']) print(sentence2) tag = 'h1' text = 'This is a header' sentence3 = '<{0}>{1}<{0}>'.format(tag, text) print(sentence3) sentence4 = 'My name is {0[name]} and I am {1[age]} years old.'.format (person, person) print(sentence4) sentence5 = 'My name is {0[name]} and I am {0[age]} years old.'.format (person) print(sentence5) sentence6 = 'My name is {0[0]} and I am {...

Python 3.4 - Decorators

 You can not only run messages through other functions but you can run other functions through functions. And you can run multiple, other functions through functions. def outer_func (msg): message = msg def inner_func (): print (message) return inner_func #this is important, notcie how the inner function isn't returning anything yet hi_func = outer_func( 'hi' ) # now the variable hi_func is loaded with the return value of the inner function # and has th message stored hi_func() # this now prints or runs the functions with the () included print ( '-------------------------------' ) def decor_func (orig_function): # so now this is going to runa function instead of a message. def wrapper_function (): # this will return the orig_function within the decor_func return orig_function() return wrapper_function def display (): # this is the function begin returned within the other functions print ( 'display function i...

Python 3.4 - Closures

Closures have access to the outer functions arguments and variables.  def outer_func (): message = 'hi' def inner_func (): print (message) return inner_func my_func = outer_func() # this creates a function my_func() # this runs the functon using the out_func my_func() # three times my_func() print ( '---------------------------------' ) def outer_func (msg): #this is a bit different because it takes a message now message = msg # an message is equal to the message variable set def inner_func (): print (message) return inner_func hi_func = outer_func( 'hi sucka' ) #these need an argument to work hello_func = outer_func( 'hello' ) hi_func() hello_func() print ( '---------------------------------' )  /Users/mattfassnacht/PycharmProjects/untitled10/venv/bin/python /Users/mattfassnacht/PycharmProjects/untitled10/math.py hi hi hi --------------------------------- hi sucka hello Process finishe...

Python 3.4 - First-Class Functions

 You can assign functions to variables. And you can have functions within functions. def square (x): # this function will return the value of x times itself return x * x f = square( 5 ) # this is the variable and the value of x is determined print (square) # just printing out the position of the function print (square( 3 )) # this is a stand-alone print statement calling on the function, not creating a variable print (f) # this prints the results of the function which will take 5 and multiply it by itself h = square # this makes the variable h the equivalent of the function square. print (h) # just printing out the position of the function print (h( 4 )) # now h is being treated as the function square print ( '--------------------------------------------' ) def my_map (func , arg_list): # this sets up the function to return a result, # which will be using the function on the array result = [] for i in arg_list: result.append(func(i)) ...

Python 3.4 - Dictionaries

my_dict = { 'employee_name' : 'Jeff' , 'job' : 'Teacher' , 'rank' : 10 , 'salary' : 6000 , 'favorite things' : [ 'polo' , 'horses' , 'movies about horses' ]} # dictionaries are made of keys with values print (my_dict) print (my_dict[ 'job' ]) # this prints out the item in the dict print (my_dict[ 'favorite things' ]) # this prints out the key which is a list within the dict my_dict2 = { 1 : 'Jeff' , 'job' : 'Teacher' , 'rank' : 10 , 'salary' : 6000 , 'favorite things' : [ 'polo' , 'horses' , 'movies about horses' ]} print (my_dict2[ 1 ]) # an integer can be a key print (my_dict2.get( 'job' )) # this will get the value of the key specified. my_dict2[ 'phone' ] = '555-5555' print (my_dict2) # notice that the phone value has been added to the dictionary my_di...

Python 3.4 - Roman Numerals

This program prints out the equivalent roman numeral: roman = [ 'I' , 'II' , 'III' , 'IV' , 'V' , 'VI' , 'VII' , 'VIII' , 'IX' , 'X' ] num_input = '' while num_input != 'q' : print ( " \n [1] Enter a number (1-10) to convert into a roman numeral." ) print ( ' \n press q to exit' ) num_input = input ( " \n Enter your number: " ) if num_input == '1' : print ( ' \n That is the roman numeral: ' + roman[ 0 ]) elif num_input == '2' : print ( ' \n That is the roman numeral: ' + roman[ 1 ]) elif num_input == '3' : print ( ' \n That is the roman numeral: ' + roman[ 2 ]) elif num_input == '4' : print ( ' \n That is the roman numeral: ' + roman[ 3 ]) elif num_input == '5' : print ( ' \n That is the roman numeral: ' + r...

Python 3.4 - My first game

https://repl.it/repls/WhiteIncompleteCalculators from time import sleep print ( ' \n Welcome to the game! \n ' ) sleep( 1 ) print ( '------------------------------ \n ' ) sleep( 1 ) choice = '' straightchoice = '' leftchoice = '' rightchoice = '' while choice != 'q' : print ( ' \n You have entered the Spooky Castle. \n ' ) sleep( 1 ) print ( 'There are three ways to go. \n ' ) sleep( 1 ) print ( 'Straight (1), \n ' ) sleep( .5 ) print ( 'Left (2) or, \n ' ) sleep( .5 ) print ( 'Right (3). \n ' ) sleep( 1 ) print ( 'Choose your path by entering the number. \n ' ) sleep( 1 ) print ( 'press q to quit.' ) choice = input ( ' \n Enter the number:' ) if choice == '1' : sleep( 1 ) print ( ' \n Uh oh...you chose to go straight. \n ' ) (sleep( 1 )) print ( ...

Python 3.4 - List Comprehension Practice

## values = expression for value in collection ## squares = x * x for x in range(10) squares = [x * x for x in range ( 10 )] print (squares) ##values = [] ## for value in colleciton ##values.append(expression) squares = [] for x in range ( 10 ): squares.append(x*x) print (squares) print ( '-------------------------------' ) ## this will only do the expression if the remainder is True. even_squares = [x*x for x in range ( 10 ) if x % 2 == 0 ] print (even_squares) ##this does the same thing as the above, but in a for loop even_squares = [] for x in range ( 10 ): if x % 2 == 0 : even_squares.append(x*x) print (even_squares) print ( '-------------------------------' ) employees = [ 'jeff' , 'rick' , 'nick' , 'matt' ] new_employees = [] for x in employees: new_employees.append(x) print (new_employees) print ( '-------------------------------' ) ##notice the difference with the print statement and ...

Python 3.4 - Threading Module

 The threading module allows a program to run multiple operations at the same time in the same process space. import threading from queue import Queue import time print_lock = threading.Lock() def exampleJob (worker): time.sleep( 0.5 ) with print_lock: print (threading.current_thread().name , worker) def threader (): while True : worker = q.get() exampleJob(worker) q.task_done() q = Queue() for x in range ( 10 ): t = threading.Thread( target =threader) t.daemon = True t.start() start = time.time() for worker in range ( 20 ): q.put(worker) q.join() print ( 'entire job took:' , time.time() - start) /Users/mattfassnacht/PycharmProjects/untitled6/venv/bin/python /Users/mattfassnacht/PycharmProjects/untitled6/functions.py Thread-1 0 Thread-2 1 Thread-8 7 Thread-6 5 Thread-7 6 Thread-10 9 Thread-4 3 Thread-9 8 Thread-5 4 Thread-3 2 Thread-2 11 Thread-8 12 Thread-10 15 Thread-4 16 Thread-9 17 Thread-7 14 Th...

Python 3.4 - Array Module

Arrays are much like lists but they are confined to one type of data.  The example linked needs parentheses around the print statements, but you can see below some of the differences between lists and arrays: import array a = array.array( "B" , range ( 16 )) # unsigned char b = array.array( "h" , range ( 16 )) # signed short print (a) print ( repr (a.tostring())) print (b) print ( repr (b.tostring())) /Users/mattfassnacht/PycharmProjects/untitled6/venv/bin/python /Users/mattfassnacht/PycharmProjects/untitled6/functions.py array('B', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) b'\x00\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f' array('h', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) b'\x00\x00\x01\x00\x02\x00\x03\x00\x04\x00\x05\x00\x06\x00\x07\x00\x08\x00\t\x00\n\x00\x0b\x00\x0c\x00\r\x00\x0e\x00\x0f\x00' Process finished with exit code 0 http://effbot.org/librarybook/array.htm

Python 3.4 - Bisect Module

The Bisect Module "provides support for maintaining a list in sorted order without having to sort the list after each insertion. For long lists of items with expensive comparison operations, this can be an improvement over the more common approach." import bisect mylist = [ 1 , 3 , 4 , 4 , 4 , 6 , 7 ] print ( "the rightmost index is : " , end = "" ) print (bisect.bisect(mylist , 4 )) print ( "the rightmost index is : " , end = "" ) print (bisect.bisect_left(mylist , 4 )) mylist2 = [ 1 , 3 , 4 , 4 , 4 , 6 , 7 ] bisect.insort(mylist2 , 5 , 0 , 5 ) print ( "the list is now:" ) for i in range ( 0 , 8 ): print (mylist2[i] , end = "" ) print ( ' \r ' ) bisect.insort_left(mylist2 , 2 ) print ( "the list is now:" ) for i in range ( 0 , 9 ): print (mylist2[i] , end = "" )  /Users/mattfassnacht/PycharmProjects/untitled6/venv/bin/python /Users/mattfassnacht/PycharmProjects/...

Python - Practice

Creating classes and subclasses allows you to inherit both or override the other. By including the super.()__init__() attribute you can use both! class Contact: def __init__ ( self , first_name , last_name): self .first_name = first_name self .last_name = last_name self .full_name = first_name + "" + last_name def print_contact ( self ): print ( self .full_name) class EmailContact(Contact): def __init__ ( self , first_name , last_name , email): super (). __init__ (first_name , last_name) self .email = email self .full_name = email email_contact = EmailContact( 'test' , 'person' , "test@person.com" ) print (email_contact.print_contact()) /Users/mattfassnacht/PycharmProjects/untitled6/venv/bin/python /Users/mattfassnacht/PycharmProjects/untitled6/prac3.py test@person.com None Process finished with exit code 0

Python - Practice

This is some practice on string manipulation, for and while loops, and guess and check. s = 'abc' print ( len (s)) print (s[ 0 ]) print (s[ 1 ]) print (s[ 2 ]) print (s[- 1 ]) print (s[- 2 ]) print (s[- 3 ]) t = 'hdfjlhdjlfh' print (t[::]) print (t[ 0 : 4 ]) h = 'hello' print (h) h = 'y' + h[ 1 : len (h)] print (h) for var in range ( 5 ): print ( 'this will print 5 times, starting with 0 and ending with 4' ) for var2 in range ( 1 , 5 ): print ( 'this will print 4 times, starting with 1 and ending with 4' ) for letter in t: if letter == 'h' : print ( 'this is an h!' ) else : print ( 'this is not an h!' ) an_letters = 'abcdefghijklmnopqrstuvwxyz' word = input ( 'Enter a word:' ) times = int ( input ( 'How many times?' )) i = 0 while i < len (word): letter = word[i] if letter in an_letters: print ( 'give me an ' + letter + ...