1 CS 177 Week 11 Recitation Slides Writing out programs, Reading from the Internet and Using Modules.

Slides:



Advertisements
Similar presentations
CATHERINE AND ANNIE Python: Part 3. Intro to Loops Do you remember in Alice when you could use a loop to make a character perform an action multiple times?
Advertisements

Internet Services and Web Authoring (CSET 226) Lecture # 5 HyperText Markup Language (HTML) 1.
This Week More Types boolean string Modules print statement Writing programs if statement Type boolean.
CS2984: Introduction to Media Computation Drawing directly on images.
Week 7: Input and Output 1.  Now we are going to talk a little bit about output  You have a lot of experience with System.out.println() and System.out.print()
Speed CS A109. Big speed differences Many of the techniques we’ve learned take no time at all in other applications Select a figure in Word. –It’s automatically.
Html: getting started HTML is hyper text markup language. It is what web browsers look at on the Internet. HTML documents should be created in a simple.
Hypertext markup language.  Client asks for an html file  Server returns the html file  Client parses and displays it  This display is what most people.
Today CSS HTML A project.
© Keith Vander Linden, How did you ever do without it? - from the first VisiCalc ad, 1979.
CS1315: Introduction to Media Computation Introduction to Programming.
Created by Mark Guzdial, Georgia Institute of Technology; modified by Robert H. Sloan, University of Illinois at Chicago, For Educational Use. CS.
1 CS 177 Week 6 Recitation Slides Scaling Drawing on images Vector-based Vs. Bitmap graphical representation.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Basic Web Publishing BA209 Arturo Perez-Reyes. Web is a communication protocol The Internet is A net of nets That use the TCP/IP protocol To publish on.
Python and Web Programming
Guide To UNIX Using Linux Third Edition
1 The World Wide Web. 2  Web Fundamentals  Pages are defined by the Hypertext Markup Language (HTML) and contain text, graphics, audio, video and software.
Other Features Index and table of contents Macros and VBA.
Chapter 10: Creating and Modifying Text. Chapter Objectives.
Introduction to Computing and Programming in Python: A Multimedia Approach Chapter 10: Creating and Modifying Text.
Review HTML  What is HTML?  HTML is a language for describing web pages.  HTML stands for Hyper Text Markup Language  HTML is not a programming language,
1 Spidering the Web in Python CSC 161: The Art of Programming Prof. Henry Kautz 11/23/2009.
Preparation and processing of in-session documents.
Creating a Simple Page: HTML Overview
IPAD Set Up. Gmail Account First, you need to set up a free gmail account with your name. If you have an existing one you may use that as long as: – A)
Georgia Institute of Technology Creating and Modifying Text part 1 Barb Ericson Georgia Institute of Technology Oct 2005.
.  Entertain  Inform  Educate  Blogs  Sell  Date  Gamble  Religion.
Uploading Image Files. Introduction – Click on Control Panel Button Typically, most instructors will post the majority of their images under the “Course.
Lecture # 29 Python III: Client  Server. Motivation: How the Internet Works Static HTML Pages ApacheApache ApacheApache BrowserBrowser BrowserBrowser.
1 Essential HTML coding By Fadi Safieddine (Week 2)
Announcements All groups have been assigned Homework: By this evening everyone in your group and set up a meeting time to discuss project 4 Project.
Learning Web Design: Chapter 4. HTML  Hypertext Markup Language (HTML)  Uses tags to tell the browser the start and end of a certain kind of formatting.
 This presentation introduces the following: › 3 types of CSS › CSS syntax › CSS comments › CSS and color › The box model.
 A style sheet is a single page of formatting instructions that can control the appearance of many HTML pages at once.  If style sheets accomplished.
Programming in HTML.  Programming Language  Used to design/create web pages  Hyper Text Markup Language  Markup Language  Series of Markup tags 
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
Introduction to HTML. What is a HTML File?  HTML stands for Hyper Text Markup Language  An HTML file is a text file containing small markup tags  The.
1 CS 177 Week 12 Recitation Slides Advanced Text Techniques and Making Text for the Web + (some examII questions)
15.1 Fundamentals of HTML.
IS1811 Multimedia Development for Internet Applications Lecture 4: Introduction to HTML Rob Gleasure
Unit 2, cont. September 12 More HTML. Attributes Some tags are modifiable with attributes This changes the way a tag behaves Modifying a tag requires.
Week 1 – Beginners Content McAfee & Big Fish Games CoderDojo.
CS1315: Introduction to Media Computation Introduction to Programming.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
Introduction to Flash Animation CS 318. Topics Introduction to Flash and animation The Flash development environment Creating Flash animations  Layers.
1 CS 177 Week 5 Recitation Slides Mirroring and copying images, Using for Loop, if statement, and range.
 To begin you first need to sign up to Weebly by going to or alternatively and we will create an account.
HTML Basics. HTML Coding HTML Hypertext markup language The code used to create web pages.
CS Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
Word 2007® Business and Personal Communication How can Microsoft Word 2007 help you work with others?
Objective: To describe the evolution of the Internet and the Web. Explain the need for web standards. Describe universal design. Identify benefits of accessible.
LBSC 690 Session 4 Programming. Languages How do we learn a language? Learn by listening Then reading Then writing How do we teach programming? Learn.
CS1315: Introduction to Media Computation Introduction to Programming.
CS1315: Introduction to Media Computation Transforming pictures by index number.
Behind every site is a mix of special languages that your web browser understands The main way of describing any website is HTML HTML stands for Hyper.
1 CS 177 Week 7 Recitation Slides Modifying Sounds using Loops + Discussion of some Exam Questions.
Python: File Directories What is a directory? A hierarchical file system that contains folders and files. Directory (root folder) Sub-directory (folder.
Text2PTO: Modernizing Patent Application Filing A Proposal for Submitting Text Applications to the USPTO.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
HTML 5 (Part 1) – Start from SCRATCH. HTML 5 – Start from SCRATCH.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Lecture # 29 Python III: Editing a Text File in Python.
Introduction to HTML.
HTML Basics.
A guide to HTML.
Chapter 10: Creating and Modifying Text
Basic HTML and Embed Codes
University of Warith AL-Anbiya’a
Presentation transcript:

1 CS 177 Week 11 Recitation Slides Writing out programs, Reading from the Internet and Using Modules

2 ANY QUESTIONS?

Writing a program to write programs First, a function that will automatically change the text string that the program “littlepicture” draws As input, we’ll take a new filename and a new string. We’ll find() the addText, then look for the first double quote, and then the final double quote. Then we’ll write out the program as a new string to a new file 3

changeLittle("sample.py","Here is a sample of changing a program") 4 Original: def littlepicture(): canvas=makePicture(getMediaPat h("640x480.jpg")) addText(canvas,10,50,"This is not a picture") addLine(canvas,10,20,300,50) addRectFilled(canvas,0,200,300,5 00,yellow) addRect(canvas,10,210,290,490) return canvas Modified: def littlepicture(): canvas=makePicture(getMediaPat h("640x480.jpg")) addText(canvas,10,50,"Here is a sample of changing a program") addLine(canvas,10,20,300,50) addRectFilled(canvas,0,200,300,5 00,yellow) addRect(canvas,10,210,290,490) return canvas

Changing the little program automatically 5 def changeLittle(filename,newstring): # Get the original file contents programfile=r"C:\Documents and Settings\Mark Guzdial\My Documents\py- programs\littlepicture.py" file = open(programfile,"rt") contents = file.read() file.close() # Now, find the right place to put our new string addtext = contents.find("addText") firstquote = contents.find('"',addtext) #Double quote after addText endquote = contents.find('"',firstquote+1) #Double quote after firstquote # Make our new file newfile = open(filename,"wt") newfile.write(contents[:firstquote+1]) # Include the quote newfile.write(newstring) newfile.write(contents[endquote:]) newfile.close()

That’s how vector-based drawing programs work! Editing a line in AutoCAD doesn’t change the pixels. It changes the underlying representation of what the line should look like. It then runs the representation and creates the pixels all over again. 6

Finding data on the Internet The Internet is filled with wonderful data, and almost all of it is in text! Later, we’ll write functions that directly grab files from the Internet, turn them into strings, and pull information out of them. For now, let’s assume that the files are on your disk, and let’s process them from there. 7

Example: Get the temperature The weather is always available on the Internet. Can we write a function that takes the current temperature out of a source like or 8

The Internet is mostly text Text is the other unimedia. Web pages are actually text in the format called HTML (HyperText Markup Language)  HTML isn’t a programming language, it’s an encoding language.  It defines a set of meanings for certain characters, but one can’t program in it. We can ignore the HTML meanings for now, and just look at patterns in the text. 9

Where’s the temperature? 10 The word “temperature” doesn’t really show up. But the temperature always follows the word “Currently”, and always comes before the “ ° ” <img src="/shared- local/weather/images/ps.gif" width="48" height="48" border="0"> <font size="-1" face="Arial, Helvetica, sans- serif"> Currently Partly sunny 54 ° F

We can use the same algorithm we’ve seen previously Grab the content out of a file in a big string.  (We’ve saved the HTML page previously.  Soon, we’ll see how to grab it directly.) Find the starting indicator (“Currently”) Find the ending indicator (“ °”) Read the previous characters 11

Finding the temperature 12 def findTemperature(): weatherFile = getMediaPath("ajc-weather.html") file = open(weatherFile,"rt") weather = file.read() file.close() # Find the Temperature curloc = weather.find("Currently") if curloc <> -1: # Now, find the " °" following the temp temploc = weather.find(" °",curloc) tempstart = weather.rfind(">",0,temploc) print "Current temperature:",weather[tempstart+1:temploc] if curloc == -1: print "They must have changed the page format -- can't find the temp"

Adding new capabilities: Modules What we need to do is to add capabilities to Python that we haven’t seen so far. We do this by importing external modules. A module is a file with a bunch of additional functions and objects defined within it.  Some kind of module capability exists in virtually every programming language. By importing the module, we make the module’s capabilities available to our program.  Literally, we are using the module, as if we’d typed it into our file 13

Accessing pieces of a module We access the additional capabilities of a module using dot notation, after we import the module. How do you know what pieces are there?  Check the documentation.  Python comes with a Library Guide.  There are books like Python Standard Library that describe the modules and provide examples 14

The OS Module The OS module offers a number of powerful capabilities for dealing with files, e.g., renaming files, finding out when a file was last modified, and so on. We start accessing the OS module by typing:  import os The function that knows about directories is listdir(), used as os.listdir()  listdir takes a path to a directory as input. 15

Using os.listdir 16 >>> import os >>> print getMediaPath("barbara.jpg") C:\Documents and Settings\Mark Guzdial\My Documents\mediasources\barbara.jpg >>> print getMediaPath("pics") Note: There is no file at C:\Documents and Settings\Mark Guzdial\My Documents\mediasources\pics C:\Documents and Settings\Mark Guzdial\My Documents\mediasources\pics >>> print os.listdir("C:\Documents and Settings\Mark Guzdial\My Documents\mediasources\pics") ['students1.jpg', 'students2.jpg', 'students5.jpg', 'students6.jpg', 'students7.jpg', 'students8.jpg']

Another interesting module: Random 17 >>> import random >>> for i in range(1,10):... print random.random()

Many other Python Standard Libraries datetime and calendar know about dates. What day of the week was the US Declaration of Independence signed? Thursday. math knows about sin() and sqrt() zipfile knows how to make and read.zip files lets you (really!) build your own spam program, or filter spam, or build an tool for yourself. SimpleHTTPServer is a complete working Web server. 18

19 Final QUESTIONS???