[Tutor] I am trying to read and decode an emails PDF attachment via python,
I am trying to read and decode an emails PDF attachment via python, its a dedicated mailbox, anything emailed there is to be read by the script, deleted from the mailbox and processed. http://pastebin.com/VA1gwWH3 def get_next_mail() works AOK, I get a nice long string containing the next email and PDF data block but when I execute msg.is_multipart() I get False ??? And when I execute msg.get_payload() I get nothing Any ideas anyone, at a guess its because my string is not formatted correctly, can anyone advise ? Cheers Dave -- Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Modules and Python tutorial by S. Thurlow - opinions please
I have got myself well and truly bogged down. I need to change the angle from which I am looking. I only have a short while left for now in which to make some sort of progress with Python. What do people think of: http://www.sthurlow.com/python/ Is it reasonably accurate and therefore useful? If not, or even in addition, what can people recommend for me to dig myself out of the quagmire? I am having difficulty with the "wrong" things. If statements, while or for loops etc. don't phase me. But I am stuck on something so trivial that it is ridiculous. I think that I understand how to write a basic function, but I can't work out how to save and call it. To show you what I mean: How to save and run a bash script: Write your script save it in the normal manner chmod to x for everyone you want to be able to execute it. (E.g. where owner is root: perhaps 744) Either move the file into a directory on your path, or add the directory that the file is in to your path. It will now run. Can anyone point me to a similar set of basic instructions for Python modules?? Thanks, Lisi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modules and Python tutorial by S. Thurlow - opinions please
Lisi wrote: I have got myself well and truly bogged down. I need to change the angle from which I am looking. I only have a short while left for now in which to make some sort of progress with Python. What do people think of: http://www.sthurlow.com/python/ Is it reasonably accurate and therefore useful? As far as I can tell after a lightning fast read of it (about three minutes to skim a few of the lessons), it seems perfectly fine to me. [...] To show you what I mean: How to save and run a bash script: Write your script save it in the normal manner chmod to x for everyone you want to be able to execute it. (E.g. where owner is root: perhaps 744) Either move the file into a directory on your path, or add the directory that the file is in to your path. It will now run. Can anyone point me to a similar set of basic instructions for Python modules?? More or less exactly the same, except you need a hash-bang line at the top of the script so that the shell knows that it is Python and not a shell script. So you can add a line like: #!/usr/bin/python at the VERY TOP of your script (it MUST be in the first line for the shell to recognise it). (This is exactly the same practice for all scripting languages, including Perl, Bash, Ruby, and many more... if the hash-bang line is missing, the shell will try to execute the file with "sh".) As an alternative, you can call the script from the shell prompt like this: $ python path/to/my/script.py (naturally you don't type the $ that is just the shell prompt) Finally, you can put your script somewhere in the PYTHONPATH and then call it like this: $ python -m script Note that this way you leave the .py off the end -- you're not telling Python to *run* a file, but to *import* a file, which happens to run it as a side-effect. I don't recommend you use this technique until you're more comfortable with Python. I mention it only for completeness. -- Steven ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modules and Python tutorial by S. Thurlow - opinions please
Thanks, Steven. :-) I'll get back to this this evening. Lisi On Saturday 20 August 2011 16:51:07 Steven D'Aprano wrote: > Lisi wrote: > > I have got myself well and truly bogged down. I need to change the angle > > from which I am looking. I only have a short while left for now in which > > to make some sort of progress with Python. What do people think of: > > > > http://www.sthurlow.com/python/ > > > > Is it reasonably accurate and therefore useful? > > As far as I can tell after a lightning fast read of it (about three > minutes to skim a few of the lessons), it seems perfectly fine to me. > > [...] > > > To show you what I mean: > > How to save and run a bash script: > > Write your script > > save it in the normal manner > > chmod to x for everyone you want to be able to execute it. (E.g. where > > owner is root: perhaps 744) > > Either move the file into a directory on your path, or add the directory > > that the file is in to your path. > > It will now run. > > > > Can anyone point me to a similar set of basic instructions for Python > > modules?? > > More or less exactly the same, except you need a hash-bang line at the > top of the script so that the shell knows that it is Python and not a > shell script. So you can add a line like: > > #!/usr/bin/python > > at the VERY TOP of your script (it MUST be in the first line for the > shell to recognise it). > > (This is exactly the same practice for all scripting languages, > including Perl, Bash, Ruby, and many more... if the hash-bang line is > missing, the shell will try to execute the file with "sh".) > > > As an alternative, you can call the script from the shell prompt like this: > > > $ python path/to/my/script.py > > (naturally you don't type the $ that is just the shell prompt) > > > Finally, you can put your script somewhere in the PYTHONPATH and then > call it like this: > > > $ python -m script > > > Note that this way you leave the .py off the end -- you're not telling > Python to *run* a file, but to *import* a file, which happens to run it > as a side-effect. I don't recommend you use this technique until you're > more comfortable with Python. I mention it only for completeness. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Tkinter: Deleting text when clicking in field.
I worked on this for a long time. I did many searches to fix the many error messages I was getting and I finally got this to work. I would now just like to have the text disappear when a person clicks in the box to type something. How can I do that? (This is just a sample of the whole program.) from Tkinter import * class MyGrid(Frame): def __init__(self, win=None): Frame.__init__(self, win) self.grid() self.mkWidgets() [...snip...] self.mytext = StringVar() self.mytext.set("Enter text here") # This text needs to be deleted upon clicking in the field. self.e = Entry(bg='orange', textvariable=self.mytext, relief=SUNKEN, width=50) self.e.grid(row=0, column=0) [...snip...] app = MyGrid() app.mainloop() #==END I have created a method to clear the field. This is not working: def clearBox(self): self.mytext.delete(0, END) return self.mytext = StringVar(None) self.mytext.set("Enter text here") self.e = Entry(bg='orange', textvariable=self.mytext, relief=SUNKEN, width=50, command=self.clearBox) self.e.grid(row=0, column=0) This does not work either: def clearBox(self): self.mytext.get() self.mytext.delete(0, END) return self.mytext = StringVar(None) self.mytext.set("Enter text here") self.e = Entry(bg='orange', textvariable=self.mytext, relief=SUNKEN, width=50, command=self.clearBox) self.e.grid(row=0, column=0) This either: def clearBox(self): #self.mytext.get() self.e.delete(0, END) return ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modules and Python tutorial by S. Thurlow - opinions please
On 20/08/11 15:25, Lisi wrote: ridiculous. I think that I understand how to write a basic function, but I can't work out how to save and call it. If you literally mean how to write,save and use a Python function (from within another script) then I'll have a go: How to save and run a bash script: Write your script Same with Python, create a text file with your favourite editor and save it with a .py extension. This is now a Python module that you can import into any other script. Lets assume you called it mymodule.py. save it in the normal manner Yes, gotta do that with Python too. chmod to x for everyone you want to be able to execute it. (E.g. where owner is root: perhaps 744) You don't need that for a Python module, it only needs to be readable. (But if you do make it executable you can add a shebang line at the top and then run it directly from the command prompt. But since you only want to access the functions within we won't need to do that.) Either move the file into a directory on your path, or add the directory that the file is in to your path. Either save the file in a directory in your PYTHONPATH or add the directory you saved it in to your PYTHONPATH It will now run. It can now be imported. So to use your function, let's call it spam(), in your mymodule.py file: Create a new python script, lets assume its called myfile.py. add the line import mymodule call the function with myVariable = mymodule.spam() Save your new Python script file Execute your new file from Pyhon with $ python /full/path/to/myfile.py There is a full worked example in the Functions and Modules topic in my tutorial... HTH Alan G. 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] Tkinter: Deleting text when clicking in field.
On 20/08/11 20:12, brandon w wrote: I worked on this for a long time. I did many searches to fix the many error messages I was getting and I finally got this to work. I would now just like to have the text disappear when a person clicks in the box to type something. How can I do that? You need to buind the mouse click event to the event handler method. It will not be called unless you tell Tkinter to call it. from Tkinter import * class MyGrid(Frame): def __init__(self, win=None): Frame.__init__(self, win) self.grid() self.mkWidgets() [...snip...] self.mytext = StringVar() self.mytext.set("Enter text here") # This text needs to be deleted upon clicking in the field. self.e = Entry(bg='orange', textvariable=self.mytext, relief=SUNKEN, width=50) self.e.grid(row=0, column=0) You need to add a Bind statement to bind a mouse click to a method that clears the text. Remember that everything in a GUI is event driven, so you have to link your code to the events. I have created a method to clear the field. This is not working: def clearBox(self): self.mytext.delete(0, END) return You need to delete the text in the Entry widget not the StringVar Alternatively you need to set the StringVar to an empty string and wait for the widget to redraw itself. (Or force it to redraw!) Then you need to add a bind statement to tie that method to a mouse click in the Entry. self.mytext = StringVar(None) self.mytext.set("Enter text here") self.e = Entry(bg='orange', textvariable=self.mytext, relief=SUNKEN, width=50, command=self.clearBox) That binds the event to the command parameter of the Entry. BUT the Entry widget does not have a command option - I'm surprised you dont get an error complaining about that? So you need to use the bind() function. The bind function specifies the event type and the method to be called. It expects the method to take an additional event argument: It should look something like: # define an event handler def handleEvent(self, event): print "I'm habdling an event" # bind the button-1 click of the Entry to the handler self.e.bind('Button-1', handleEvent) HTH -- Alan G 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] Tkinter: Deleting text when clicking in field.
On 08/20/2011 07:11 PM, Alan Gauld wrote: On 20/08/11 20:12, brandon w wrote: I worked on this for a long time. I did many searches to fix the many error messages I was getting and I finally got this to work. I would now just like to have the text disappear when a person clicks in the box to type something. How can I do that? You need to buind the mouse click event to the event handler method. It will not be called unless you tell Tkinter to call it. from Tkinter import * class MyGrid(Frame): def __init__(self, win=None): Frame.__init__(self, win) self.grid() self.mkWidgets() [...snip...] self.mytext = StringVar() self.mytext.set("Enter text here") # This text needs to be deleted upon clicking in the field. self.e = Entry(bg='orange', textvariable=self.mytext, relief=SUNKEN, width=50) self.e.grid(row=0, column=0) You need to add a Bind statement to bind a mouse click to a method that clears the text. Remember that everything in a GUI is event driven, so you have to link your code to the events. I have created a method to clear the field. This is not working: def clearBox(self): self.mytext.delete(0, END) return You need to delete the text in the Entry widget not the StringVar Alternatively you need to set the StringVar to an empty string and wait for the widget to redraw itself. (Or force it to redraw!) Then you need to add a bind statement to tie that method to a mouse click in the Entry. self.mytext = StringVar(None) self.mytext.set("Enter text here") self.e = Entry(bg='orange', textvariable=self.mytext, relief=SUNKEN, width=50, command=self.clearBox) That binds the event to the command parameter of the Entry. BUT the Entry widget does not have a command option - I'm surprised you dont get an error complaining about that? So you need to use the bind() function. The bind function specifies the event type and the method to be called. It expects the method to take an additional event argument: It should look something like: # define an event handler def handleEvent(self, event): print "I'm habdling an event" # bind the button-1 click of the Entry to the handler self.e.bind('Button-1', handleEvent) HTH Thanks again for your help. I'll work on this tomorrow. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] I am trying to read and decode an emails PDF attachment via python,
Did a lot more digging and finally sorted it, sorry for bothering you guys, Cheers Dave On 20 August 2011 10:58, dave selby wrote: > I am trying to read and decode an emails PDF attachment via python, > its a dedicated mailbox, anything emailed there is to be read by the > script, deleted from the mailbox and processed. > > http://pastebin.com/VA1gwWH3 > > def get_next_mail() works AOK, I get a nice long string containing the > next email and PDF data block but when I execute > > msg.is_multipart() > I get False ??? > > And when I execute > msg.get_payload() > I get nothing > > Any ideas anyone, at a guess its because my string is not formatted > correctly, can anyone advise ? > > Cheers > > Dave > > -- > > Please avoid sending me Word or PowerPoint attachments. > See http://www.gnu.org/philosophy/no-word-attachments.html > -- Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor