Re: [Tutor] line continuation characters
On Monday 20 June 2011 00:03:47 Steven D'Aprano wrote: > Lisi wrote: > > I am having problems with line continuation characters. Error message: > > > > SyntaxError: unexpected character after line continuation character > > Then delete it ;) > > Seriously. You have your editor open, right? Go to the line where the > error is reported. It will look something like this: > > blah blah blah \ > > Press the End key. Your editor's blinking insertion point or cursor will > go to the end of the line: > > blah blah blah \ | > > Backspace until the cursor is *immediately* next to the backslash, with > no other characters INCLUDING SPACES after it. > > Which editor are you using? If it is kwrite or kate, it has a setting to > control whether spaces are left at the end of lines, e.g. in kwrite: > > Settings > Configure Editor > Editing > Remove trailing spaces > > > I have succeeded in moving the position of the complaint, but am > > currently well and truly stuck. > > > > I have read up about it and now (I hope!) know what to do about it in > > general, but at one particular point everywhere seems to be wrong. Is > > there any way I can make these wretched continuation characters visible, > > so that I know where and what I am up against? > > Continuation characters are visible. It is a backslash at the very end > of the line. It must not be followed by *anything* -- no comments, no > spaces, nothing. > > >>> x = 23 + \ > >File "", line 1 > x = 23 + \ >^ > SyntaxError: unexpected character after line continuation character > > > If this error message is not clear, would you like to suggest an > improvement? It is not the error message that is unclear, it was the state of my knowledge. I did not know that \ was a line continuation character, and simply wrote a long line which wrapped. I know how to remove spaces - I just had not got a clue what the message was on about. I found quite a few references to line continuation characters, but nothing that told me what a line continuation character looked like, nor that it must be at the very end of the line. The back-slashes in the script all had other purposes, and I had not realised that where they fell was significant. Thanks for the help. Now to tackle my script. Lisi Lisi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] regex woes in finding an ip and GET string
Gerhardus Geldenhuis wrote: > I am trying to write a small program that will scan my access.conf file > and update iptables to block anyone looking for stuff that they are not > supposed to. > > The code: > #!/usr/bin/python > import sys > import re > > def extractoffendingip(filename): > f = open(filename,'r') > filecontents = f.read() > #193.6.135.21 - - [11/Jun/2011:13:58:01 +] "GET > /admin/pma/scripts/setup.php HTTP/1.1" 404 304 "-" "Mozilla/4.0 > (compatible; MSIE 6.0; Windows 98)" > tuples = re.findall(r'^(\d+\.\d+\.\d+\.\d+).*\"GET(.*)HTTP', > filecontents) If you want to process the whole file at once you have to use the re.MULTILINE flag for the regex to match the start of a line instead of the start of the whole text: tuples = re.compile(r'...', re.MULTILINE).findall(filecontents) But I think it's better to process the file one line at a time. > iplist = [] [snip] > if ip not in iplist: > iplist.append(ip) So you want every unique ip appear only once in iplist. Python offers an efficient data structure for that, the set. With these changes your funtion becomes something like (untested) def extractoffendingips(filename): match = re.compile(r'^(\d+\.\d+\.\d+\.\d+).*\"GET(.*)HTTP').match ipset = set() with open(filename) as f: for line in f: m = match(line) if m is not None: ip, getstring = m.groups() ipset.add(ip) for item in ipset: print item ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] line continuation characters
"Lisi" wrote It is not the error message that is unclear, it was the state of my knowledge. I did not know that \ was a line continuation character, I sympathise. I had been programming for about 5 years before I came across line continuation characters. For me it was while reviewing someone's code, and they used them extensively. I found quite a few references to line continuation characters, Wikipedia is usually your friend for looking up things like this. In this case it would have told you that the characters used depend on the language and specifically: Backslash as last character of line a.. C and C++ preprocessor b.. Python c.. Ruby The back-slashes in the script all had other purposes, Which is one reason I try not to use them, because backslashes already have another purpose, they can be confusing. You can usually avoid using them by incorporating parentheses or brackets in the line and breaking before the closing bracket. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Communicating Between Programs Using Raw Inputs (con'd)
Well, that's a trick me and jake learned in a book to stop the program from changing. On Saturday, June 18, 2011, aditya wrote: > > > On Sat, Jun 18, 2011 at 9:35 PM, Jacob Bender wrote: > > Dear Tutors, > > Alright, I'm using linux (ubuntu) and I took all of your advice and I got > something that works and doesn't work at the same time. Here's the source > code for my two programs called Lock and Key: > > Lock.py: > > password = "a" > > > psswd_try = raw_input("What's the password? ") > > > if psswd_try == password: > > print "correct!!!" > > raw_input() > > else: > > print "wrong" > > raw_input() > > Why do you need to call raw_input() again? You are giving the input only > once so remove raw_input() from both if and else , that should do the work > > Key.py: > > import sys > > > sys.stdout.write("a") > > In the linux terminal I ran this command(they were both in my home folder, so > no directories were needed): > > python Key.py | python Lock.py > > And all went well except for an EOF error caused by the raw_input inside the > "if" statement in my Lock program. However I did get it to print "correct", > so I know sys.stdout.write() works for what I want it to, but I don't want > the EOF error. Here's the output: > > Traceback (most recent call last): > File "Lock.py", line 7, in > raw_input() > EOFError: EOF when reading a line > > Please help me get rid of it because I couldn't find a sys.stdout.close() > command or any other std command that would help. > > Thanks! > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > > > -- > RegardsAditya > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Main Function
Looks all good except for this: while guess == the_number: Since you break out at the end, an if statement would be a more logical choice. and also: if tries == 4: print("\nYou fail!") input("\n\nPress the enter key to exit.") break you don't need to break because the condition already requires the tries to be under 4, so you don't need both the break statement and the tries < 4. Something else you could do is instead of # start the program main() input("\n\nPress the enter key to quit.") you could do if __name__ == __main__: # start the program main() input("\n\nPress the enter key to quit.") which means that if its used as a module (say, you want the number chooser thing) it won't run the game also, I liked the print("\nYou fail!") very nice On Fri, Jun 17, 2011 at 11:21 PM, Vincent Balmori wrote: > > I answered another question that says "Modify the guess_number program so > that the program's code is in a function called main(). Don't forget to > call > main() so you can play the game." It seems to be working fine, but I would > like to have a second opinion if there was a better way to put it together. > > # Guess My Number > # > # The computer picks a random number between 1 and 10 > # The player tries to guess it and the computer lets > # the player know if the guess is too high, too low > # or right on the money > > import random > > def display_instruct(): >print("\tWelcome to 'Guess My Number'!") >print("\nI'm thinking of a number between 1 and 10.") >print("Try to guess it in as few attempts as possible.\n") > > # set ask_number function > def ask_number(question, low, high, step = 1): >"""Ask for a number within a range.""" >response = None >while response not in range(low, high, step): >response = int(input(question)) >return response > > # guessing loop > def num(): ># set the initial values >the_number = random.randint(1, 10) >tries = 0 >guess = None >while guess != the_number and tries < 4: >guess = ask_number("Take a guess:", 1, 10) >if guess > the_number: >print("Lower...") >else: >print("Higher...") >tries += 1 > >if tries == 4: >print("\nYou fail!") >input("\n\nPress the enter key to exit.") >break > >while guess == the_number: >print("\nYou guessed it! The number was", the_number) >print("And it only took you", tries, "tries!\n") >input("\n\nPress the enter key to exit.") >break > > def main(): >display_instruct() >num() > > # start the program > main() > input("\n\nPress the enter key to quit.") > -- > View this message in context: > http://old.nabble.com/Main-Function-tp31873480p31873480.html > Sent from the Python - tutor mailing list archive at Nabble.com. > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] html files to pdf document
HI, i have a some 100 plus html individual files.I want to convert them to a single pdf document programatically. How to convert them in python. Are there any functions,modules or libraries for python to achieve this. -- Thank you Arun Kumar http://clicknscroll.blogspot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] nitinchandra rubbish on list
wait a minute, I clicked the link but didn't submit my info, does that mean my email will start corrupting the list now? On Mon, Jun 20, 2011 at 2:31 AM, nitin chandra wrote: > Thank you. > > Nitin > > On Mon, Jun 20, 2011 at 5:17 AM, Steven D'Aprano > wrote: > > nitin chandra wrote: > >> > >> Hello All, > >> > >> MY Sincerest APOLOGIES i had joined a a mail box management > >> services... > >> But unfortunately It started interfering with my Gmail mail box. > > > > Thanks for fixing this! > > > > > > > > -- > > Steven > > > > ___ > > Tutor maillist - Tutor@python.org > > To unsubscribe or change subscription options: > > http://mail.python.org/mailman/listinfo/tutor > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Communicating Between Programs Using Raw Inputs (con'd)
Also, we tried removing the raw input, but it wouldn't print correct On Sat, Jun 18, 2011 at 9:55 PM, aditya wrote: > > > On Sat, Jun 18, 2011 at 9:35 PM, Jacob Bender wrote: > >> Dear Tutors, >> >> Alright, I'm using linux (ubuntu) and I took all of your advice and I got >> something that works and doesn't work at the same time. Here's the source >> code for my two programs called Lock and Key: >> >> *Lock.py: * >> >> password = "a" >> >> psswd_try = raw_input("What's the password? ") >> >> if psswd_try == password: >> print "correct!!!" >> raw_input() >> else: >> print "wrong" >> raw_input() >> * >> * > > Why do you need to call raw_input() again? You are giving the input only > once so remove raw_input() from both if and else , that should do the work > > > >> *Key.py*: >> >> import sys >> >> sys.stdout.write("a") >> >> In the linux terminal I ran this command(they were both in my home folder, >> so no directories were needed): >> >> python Key.py | python Lock.py >> >> And all went well except for an EOF error caused by the raw_input inside >> the "if" statement in my Lock program. However I did get it to print >> "correct", so I know sys.stdout.write() works for what I want it to, but I >> don't want the EOF error. Here's the output: >> >> Traceback (most recent call last): >> File "Lock.py", line 7, in >> raw_input() >> EOFError: EOF when reading a line >> >> Please help me get rid of it because I couldn't find a sys.stdout.close() >> command or any other std command that would help. >> >> Thanks! >> >> >> ___ >> Tutor maillist - Tutor@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> >> > > > -- > Regards > Aditya > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] html files to pdf document
On 20-06-11 17:34, arun kumar wrote: HI, i have a some 100 plus html individual files.I want to convert them to a single pdf document programatically. How to convert them in python. Are there any functions,modules or libraries for python to achieve this. Just a question, but did you use Google before asking here? Because the first hit when searching for "python html to pdf" looks like it should do what you want. Cheers, Timo -- Thank you Arun Kumar http://clicknscroll.blogspot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] html files to pdf document
On Jun 20, 2011, at 18:58, Timo wrote: > On 20-06-11 17:34, arun kumar wrote: >> HI, i have a some 100 plus html individual files.I want to convert them to >> a single pdf document programatically. How to convert them in python. Are >> there any functions,modules or libraries for python to achieve this. > Just a question, but did you use Google before asking here? Because the first > hit when searching for "python html to pdf" looks like it should do what you > want. Maybe for you, but not for the OP. Google will give different search results depending on your location, nationality, IP address and previous searches from that address. Please post the URL of the first hit you got? Greetings, -- "Freedom: To ask nothing. To expect nothing. To depend on nothing." - Ayn Rand ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] html files to pdf document
Hello, On 20 June 2011 18:11, Michiel Overtoom wrote: > > On Jun 20, 2011, at 18:58, Timo wrote: > > > On 20-06-11 17:34, arun kumar wrote: > >> HI, i have a some 100 plus html individual files.I want to convert them > to a single pdf document programatically. How to convert them in python. > Are there any functions,modules or libraries for python to achieve this. > > > Just a question, but did you use Google before asking here? Because the > first hit when searching for "python html to pdf" looks like it should do > what you want. > > Maybe for you, but not for the OP. Google will give different search > results depending on your location, nationality, IP address and previous > searches from that address. > At the risk of belaboring the point, I think what Timo was gentlying trying to point out to the OP was that Google would've likely yielded many relevant results, including in all likelihood the top hit, notwithstanding Google's tailoring based on location and other factors. (You're not seriously trying to suggest that Google would've yielded *no* useful results, or that the top link wouldn't have been at all relevant to his question, are you?) For reference, for me the top hit is http://www.xhtml2pdf.com/ which does indeed look like it might do the job (and is a pure python solution.) Another possibly quite relevant link is this one (about 20 lines of code): http://www.linux.com/learn/docs/ldp/284676-converting-html-to-pdf-using-python-and-qt Cheers Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] html files to pdf document
On Jun 21, 2011, at 00:19, Walter Prins wrote: > For reference, for me the top hit is http://www.xhtml2pdf.com/ which does > indeed look like it might do the job (and is a pure python solution.) I get five SERP pages with tons of Stackoverflow hits, but not one for www.xhtml2pdf.com! I think this is what is known as an Internet Search Bubble. > Another possibly quite relevant link is this one (about 20 lines of code): > http://www.linux.com/learn/docs/ldp/284676-converting-html-to-pdf-using-python-and-qt Thank you for posting the links. Greetings, -- "If you don't know, the thing to do is not to get scared, but to learn." - Ayn Rand ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Socket Programming issue
This is a small client-server program in which i am using a Vbscript program to check for connectivity of 2 machines and write the output to a text file whether it connectes or not , for example the contents of the file *output.txt* are 192.168.1.2 is connected 192.168.1.10 is not connected Now i am trying to send the contents of this file from a client through a socket to a server which is running on my main server .This is basically created to automate the checking of connectivity of the machines. The issue is that although the Vbscript writes the output to the file correctly but when the client sends the contents of the file to the server via socket , it sometimes prints all the lines of the file on the server and sometimes it doesnt , i dont know whether the issue is with the client or the server , Please Help.. CLIENT.PY import socket import os import sys os.system("C:\Python26\ping.vbs") host = "localhost" port= 2 size = 1024 s=socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host,port)) f=open('output.txt','r') while 1: data = f.readline() if data: s.send(data) else: break f.close() s.close() SERVER.PY import socket host = "" port = 2 size = 1024 backlog=5 s=socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((host,port)) s.listen(backlog) while 1: client, addr =s.accept() data=client.recv(size) if data: print data else: print "client exit" s.close() -- Regards Aditya ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor