Fall 2006 Homework 5 questions page
Homework 5 Questions Page:
Are we allowed to use regular expressions and the RSS feed? Robby Thrash
| If you would like to use regular expressions please come see me (Albert). Additionally, I do not think RSS feeds are necessary for this assignment. -Albert d'Heurle |
I am having difficulty obtaining all three headlines. I can get the first one pretty easily, but I am lost how to set up the locations to get the other two headlines since there is a lack of unique text in the source code to help lead you to the other two headlines.
| There is unique text in the source code that commonly occurs just before the article. Look harder, or come to class/recitation/office hours for more help. Amanda Bennett |
What is wrong with my code? It doesn't return anything when I try to print out just the first headline.
loc=source.find("News Articles")
end=source.rfind("</strong><br>")
source1=source[loc:end]
loc1=source1.find("<strong>")
end1=source1.find("</strong>")
headline1=source1[loc1+8:end1]
print headline1
| You're so close! The way you are computing it, end1 comes before loc1 instead of after it, and so headline1 is empty. You should be searching for the first instance of </strong> in source1 after loc1, not the first instance from the beginning of source1. Colin Potts |
This works, but it seems overthought and inefficient, is it right?
loc1=source1.find("<strong>")
end1=source1.find("</strong>")
headline1=source1[loc1+8:loc1+end1-9]
print headline1
| No, I think the first solution was closer. The expression source1[loc1+8:loc1+end1-9] gives the substring of source1 from position loc1+8 up to but not including loc1+end1-9. Presumably the 8 and 9 refer to the lengths of the beginning and ending 'strong' tags, respectively, which makes sense. However, you don't need to be adding loc1 and end1 together. You can test this by inserting print statements. What are the values of loc1, end1 and len(headline1) ? I think you will find that the headline is much longer than it should be. Going back to the previous solution, there is a very small fix that is necessary to make it work. What is the version of the find method that finds a substring from a given starting location rather than from the beginning of the string? Colin Potts |
Do all the tickers need to go across the screen at the same time?
| Not necessarily, just make sure the tickertapes start where they are supposed to (ie the starting locations given by the directions) Toni Walden |
How many frames per second should we make the movie?
And do we grab the headlines under "News Articles" or is there some other list of headlines that I'm missing?
| 16 Frames per second is usually a good choice; Toni Walden |
| As a side note, you don't actually have to make the movie, you just need to produce the frames. But, if you want to make the movie, go for it :) Amanda Bennett |
| The headlines are under "News Articles". Search for that in the source code, then find the first three headlines that appears after that. Amanda Bennett |
So we aren't being graded on whether the movie looks nice? Or just that we produced frames that will work for a movie?
| That's right. By all means put some creative expression into this if you can, but that's not so easy for black tickertape text moving across a blank background! Colin Potts |
addText(canvas,600-(frame*10),100,string)
In this code from the movies powerpoint, what does the 600 represent? Does the 10 represent the number of pixels it is shifting?
The 600 represents the width of the picture, the expression in the parentheses (frame*10) represents the number of pixels that should be subtracted from 600 for each frame. As the frame number increases (as more time passes in your movie), the text will move further to the left in your picture.Liz Helms |
In the actual movie (I'm testing), should the string start off the screen then scroll onto it, or start onscreen and move?
| The first ticker should start at x = 1, y = 100; the second one at x = 1, y = 200; and finally the thrid one x = 1, y = 300 Toni Walden |
are we not supposed to use 'print' in the code for when we turn it in?
| Where do you think it makes sense to use 'print'? Which is more useful to use, 'print' or 'return'?Toni Walden |
So I know about backslash escapes and "raw mode", but I can't get this to work:
dir = pickAFolder()
dir = dir + r"\"
I get this error: "Your code contains at least one syntax error, meaning it is not legal jython." with and without the r preceding the "\". I've got to have the backslash added in there at some point.
well I figured it out, but I still don't know why the above doesn't work.
I'm writing a function to makeMovie...but it always get stopped with an error on the writeFrame line:
def makeMovie(st1,st2,st3,directory):
for frame in range (0,100):
pic=makeEmptyPicture(500,500)
addRectFilled(pic,1,1,500,500,white)
addText(pic,1+ frame5,100,st1)
addText(pic,1+ frame5,200,st2)
addText(pic,1+ frame5,300,st3)
writeFrame(pic,frame,directory)
return
I'm not sure what is wrong with that line....please help :)
| Do you have a function in your code called writeFrame? If not that could be your problem as only writePictureTo and writeSoundTo are predefined in JES. If you do, there could be an error in that code or in the way you are calling the code....it's a little hard to tell when I don't know what the error message is ;p Toni Walden |
I'm having problems turning my headlines into the strings I need for addtext. Should they be in quotes when I return them from my other functions?
| Those function should indeed be strings, so in general quotes are necessary....just make sure you have quotes around an actual string and not something like a variable name. Toni Walden |
CODE DELETED
I'm trying to pull headlines from the website, but it's not working. Help please?
is the spanish website down because it will no longer come up and my code isnt working anymore
never mind, the website is working again
CODE DELETED
My TA showed us this example to get the headlines off of www.digg.com. After I changed certain things on the given function to fit the Spanish Review website, I tried to run it. And the headlines won't print out. PLEASE HELP!
My getHeadlines function does the same thing, i get just blanks in the command area. I'm sure it's the same from the recitation but who knows, those guys go pretty fast in class
hey use the same function but take out the "rt" in con = urllib.urlopen('http://www.digg.com','rt').....it should work now
nope... tried it :(
framenum=str(frame)
if frame10:
writePictureTo(pic, dir + '//frame00' + framenum + '.jpg')
when i run the code, the last line is highlighted with the error message:
An attempt was made to call a function with a parameter of an invalid type. This means that you did something such as trying to pass a string to a method that is expecting an integer.
this is how it looks in the slides!! i can't figure out whats wrong!
| Right now you are trying to add the string '//frame00' to the framenumber. Can we do this?? Or do we have to add strings with strings? And if so, isn't there a tiny little function that will turn a number into a string? Liz Helms |
is the line framenum=str(frame) not what you are referring to?? isn't that making my frame number into a string. i dont understand where the integer is that you say i am adding?
should the function only take in a directory or should it also take in strings?
| Follow the instructions exactly as the assignment page says to do so. Liz Helms |
def findme(directory):
for frame in range (1,101):
canvas=makeEmptyPicture(500,500)
addRectFilled(canvas,1,1,500,500,white)
writeFrame(frame,directory,canvas)
def writeFrame(num,directory,framepict):
framenum=str(num)
if num < 10:
writePictureTo(framepict,directory+"//frame00"+framenum+".jpg")
if num >= 10 and num<100:
writePictureTo(framepict,directory+"//frame0"+framenum+".jpg")
if num >= 100:
writePictureTo(framepict,directory+"//frame0"+framenum+".jpg")
When I run my code I get the following message: "writePictureTo(pict,filename): Path is not valid
An error occurred attempting to pass an argument to a function. Please check line 10."
I'm lost; could someone point me in the right direction?
Since my addtext function, will only take in strings for the text, and I cannot get my getNews function to return a string, what can I do? It just shows up as the word headline1 on the screen. I've tried putting quotes around the code I'm searching for and str(headline), please can I get some real help.
I've made my headlines into strings, so how do I return those headlines back to the findme function??
How in the world were you able to split up each headline?? Come on... just a little hint...
In java you can use a substring function to grab a smaller bit of text out of a larger string, how would you go about this in python? I can't seem to figure it out...
| Something like subString = bigString[startLocation:endLocation] should do the trick. Colin Potts |
I'm trying to make a separate code to find the articles.
import urllib
def findheadline1():
connection = urllib.urlopen("http://www.spanish-review.com/")
headlines = connection.read()
connection.close()
loc=source.find("News Articles")
end=source.rfind("</strong><br>")
source1=source[loc:end]
loc1=source1.find("<strong>")
end1=source1.find("</strong>")
headline1=source1[loc1+8:end1]
print headline1
I can't figure out the problem, the error message I get is: A local or global name could not be found. You need to define the function or variable before you try to use it in any way.
Please check line 7...line 7 being "loc=source.find("News Articles")"
| Well, you're referring to two names in line 7: source and find. So the problem must be with one of them. You're assuming that source is a string and that find is a string method. One of these assumptions has to be wrong. Colin Potts |
We have the directory name as the input to findme function. So, do we have to search for an html file in the given directory and then extract the headlines? Basically, I am having problems with the first initial step where I read the file or webpage.
file = open(?????,"rt")
source = file.read(file)
In other words, once i have read the file, i can do the text searching for headlines, but how do i read the file initially (or what file do i need to read, since we are given a directory and not a file). Thanks.
| Your function takes in the name of a directory that it should write the frames of the movie TO. It isn't reading anything FROM the directory. You should read the headlines from the URL specified in the assignment, not from a local file. Colin Potts |
In the actual movie (I'm testing), should the string start off the screen then scroll onto it, or start onscreen and move?
| The first ticker should start at x = 1, y = 100; the second one at x = 1, y = 200; and finally the thrid one x = 1, y = 300 Toni Walden |
Yeah I'm still confused, If the headline is "GA Tech rocks" should our first frame be "GA Tech rocks" or "s".
...By the way, I'm not the same person who asked the question to begin with...
| If the x location of a string on the first frame is 1, that means the bottom left of the first character ("G" in your example) will be at x=1. Thus, the entire string will be printed at the left edge of the window. Colin Potts |
nobody has given me an answer. why doesnt this code work?? i changed the frame into a string in the first line.
framenum=str(frame)
if frame<10:
writePictureTo(canvas, dir + '//frame00' + framenum + ".jpg")
This is the error message:
An attempt was made to call a function with a parameter of an invalid type. This means that you did something such as trying to pass a string to a method that is expecting an integer.
| This looks fine to me. I assume that the error message is generated by the writePictureTo call. You should check that the line number in the error message is the number of the writePictureTo line. If so, are you sure that canvas is a picture and dir is a string? We know that framenum is a string because you created it two lines above. The other strings are quoted. Insert 'print dir' in the line above the if. If it prints the directory name accurately, email me your code. Colin Potts |
Sorry, I'm having a really hard time finding unique text to locate the second and third headlines that isn't text that is likely to change, i.e. the headlines themselves or the pictures' names.
| Pretend that you are a program, not a human, and look at the HTML source rather than the page as it appears in the browser. There is a recurring pattern of tags before each news story. Colin Potts |
I know all that, is there a way to say "look at the second </strong>" or "look at the 5th </td>"?
| Oh, now I see. If you want something like a version of string.find() that takes an extra 'nth' argument to indicate that you are looking for the nth instance of the target string rather than the first instance, I'm afraid that doesn't exist. You would have to write that yourself using a loop. To find headline number n (n > 1), you have to find headline n-1 and then find the next headline from that character position. Colin Potts |
I was trying to figure out a way to start from the 1st headline position and search from there onwards, and this is what i have but it is not producing error; its just not showing up.
def findHeadline2():
connection = urllib.urlopen("http://www.spanish-review.com/")
news = connection.read()
connection.close
newsArticles = news.find(str(findHeadline1()+"</strong><br>"))
if newsArticles <> -1:
headline2 = news.find("<td><strong>", newsArticles)
restofHeadline2 = news.find("</strong><br>", 0, headline2)
return news[headline2 + 1:restofHeadline2]
if currentlyIndex == -1:
return "Can't find the headline"
| This won't run, because currentlyIndex is undefined. Do you mean newsArticles? Make sure you post code that is giving the behavior you don't understand otherwise we really have to second-guess what is going on! BTW, "not showing up" may mean that it is returning blank text, such as newlines or spaces, or even the null string. You could instrument your code by sticking in a few print statements to tell you what character positions it was calculating for headline2, restofHeadline2, etc. They are certainly wrong if you aren't getting the text out that you expect. Also, a lot depends on findHeadline1, since you findHeadline2 depends on that. Is that working correctly? Colin Potts |
Everytime I try to run this code it puts the frames up one lvl from the directory I choose and adds the filename to the front of the frame001 etc.
def export(image,dir,index):
index=index/5
index=index+1
if (index 10):
writePictureTo(image,(dir+"frame00"+str(index)+".jpg"))
if (index >= 10 and index 100):
writePictureTo(image,(dir+"frame0"+str(index)+".jpg"))
if (index == 100):
writePictureTo(image,(dir+"frame"+str(index)+".jpg"))
Nevermind i got it.
For certain directories, my code only works in raw mode(when I insert "r" before the directory). Is there a way I could integrate that into my code?
| Your TA will do that if it's necessary. (Remember that forward slashes ("/") always work, but backwards slashes ("\") require an 'r' first.) Makr Guzdial |
For the second headline can we use the seq[n:] and just headline one as n???
| If that works for you! It's hard to tell what you're talking about here... Mark Guzdial |
If worse comes to worse, how many points will be taken off our grade if we are only able to retrieve the first headline and have three lines of the first headline scrolling across?
| Don't know yet – I'm going to guess about 20 points. Mark Guzdial |
pg. 219 was very helpful when I was trying to figure out how to find the 2nd and 3rd headline.
Alright this really is pissing me off what is wrong with this???
def findheadline1():
connection = ######
source = connection.read()
connection.close()
loc=source.find("News Articles")
if loc <> -1:
end=source.find("</strong><br>",loc)
beg=source.rfind("<strong>", end)
headline1=source[beg+8:end-14]
return headline1
def findheadline2():
connection = ########
source = connection.read()
connection.close()
loc=source.find("News Articles")
end=source.find("</strong><br>",loc)
if end <> -1:
end2=source.find("</strong><br>",end)
beg2=source.rfind("<strong>",end2)
headline2=source[beg2+8:end2-14]
return headline2
| Why are you doing separate functions for each headline? That seems like a more difficult way to do it – after finding the first headline, you have where to start looking for the second one. But it's certainly do-able the way you're doing it. What's happening? What do you get when you call these functions separately? We've talked alot about debugging skills in lecture – how you go about figuring out what's wrong with your program and how to fix it. Use those and give us some more clues about what's happening. Mark Guzdial |
how do you know that the ticker tape is moving 5 pixels to the right and that its starting at x = 1 y = 100, the second one at x = 1, y = 200, and the thrid one x = 1, y = 300?
| See the posted assignment: "Details, Details", point 6, bullets 3 and 4. Colin Potts |
ok. so I have a function that is pulling out the headlines. at the end of it I returned the headlines. In the main program I get an error on this line "addText(canvas,1+frame5,100,headlines[0])" saying that "A local or global name could not be found. You need to define the function or variable before you try to use it in any way." It seems that JES doesn't recognize the headline althought I returned it. If I put everthing in a big function, the program runs OK. How can I make JES recognize the headlines that I pulled out from the subfunction, into the main program??(sorry this is the best way I could describe my problem.....)
| You're assuming that the problem is with the list headlines. It could be. Are you sure that the function that returns the headlines actually returns a list and that you call it in your main function with a line that has "headlines = ..." on the left hand side? Alternately, the problem could be with the names canvas and frame5. Is canvas really the name of a picture? Has frame5 been initialized with an integer value? Colin Potts |
ok, so everytime i try to run my program, it says there is something wrong with this line....i dont get it?
connection = urllib.urlopen("http://www.spanish-review.com/")
it says that A local or global name could not be found. You need to define the function or variable before you try to use it in any way.
| Did you remember to import urllib? If not, Python doesn't know about urlopen. Colin Potts |
whats the difference between source.find() and source.rfind()?
I am receiving the following error message when I try and run my program: There was a spacing error in the program. It might be from a previous line, but I think the error is in line 9. My code for that part of the program is:
def test():
#Retrieve HTML from the Internet
import urllib
span = urllib.urlopen("http://www.spanish-review.com")
htmlData = span.read()
span.close() with line 9 being the def test(): line
Thanks!
It looks like from this that the lines under def test(): are not indented but in JES on my comput they are...
def findme(directory):
for frame in range (1,101):
canvas=makeEmptyPicture(500,500)
addRectFilled(canvas,1,1,500,500,white)
addText(canvas,(framenum*5)+1, 100, string)
addText(canvas,(framenum*5)+1), 200, string)
addText(canvas,(framenum*5)+1), 300, string)
writeFrame(frame,directory,canvas)
For this piece of code I am getting a A local or global name could not be found. You need to define the function or variable before you try to use it in any way.
Please check line 5 of C:\Documents and Settings\Owner\My Documents\hw5.py error message. Any clues as to why this is happening?
def findme(directory):
for frame in range (1,101):
canvas=makeEmptyPicture(500,500)
addRectFilled(canvas,1,1,500,500,white)
addText(canvas,(framenum*5)+1, 100, string)
addText(canvas,(framenum*5)+1), 200, string)
addText(canvas,(framenum*5)+1), 300, string)
writeFrame(frame,directory,canvas)
You haven't defined what "string" is.
Link to this Page