Re: [Tutor] Threading

2008-09-11 Thread Dick Moores


At 05:31 PM 9/11/2008, Kent Johnson wrote:
On Thu, Sep 11, 2008 at 10:56
AM, Oleg Oltar <[EMAIL PROTECTED]> wrote:
> Hi!
>
> I need to open about 1200 urls from my database. And to check that
those
> urls are really exists.
>
> I used urllib2.urlopen for it. But it's a little bit slow. I thought
that
> it's might be a good idea to do it in a threads. So it can add
some
> performance to my code.
>
> Unfortunately I can't get started with the treading module. 
There are no
> simple examples in the python docs. Not sure how to start.
There are some good examples of threading in the Python cookbook, or this
one:

http://www.chrisarndt.de/projects/threadpool/
This may be of use:
<
http://blog.doughellmann.com/2008/01/pymotw-threading_13.html>

Dick Moores



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Threading

2008-09-11 Thread Dick Moores


At 05:31 PM 9/11/2008, Kent Johnson wrote:
On Thu, Sep 11, 2008 at 10:56
AM, Oleg Oltar <[EMAIL PROTECTED]> wrote:
> Hi!
>
> I need to open about 1200 urls from my database. And to check that
those
> urls are really exists.
>
> I used urllib2.urlopen for it. But it's a little bit slow. I thought
that
> it's might be a good idea to do it in a threads. So it can add
some
> performance to my code.
>
> Unfortunately I can't get started with the treading module. 
There are no
> simple examples in the python docs. Not sure how to start.
There are some good examples of threading in the Python cookbook, or this
one:

http://www.chrisarndt.de/projects/threadpool/
This may be of use:
<
http://blog.doughellmann.com/2008/01/pymotw-threading_13.html>

Dick Moores



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Win32 extensions

2008-09-11 Thread Spencer Parker
That was what I was afraid of...I might have to go the virtualization route
instead.  PXE = pre-execution environment

On Thu, Sep 11, 2008 at 5:31 PM, Alan Gauld <[EMAIL PROTECTED]>wrote:

>
> "Spencer Parker" <[EMAIL PROTECTED]> wrote
>
>  2003.  I was wondering if it is possible to create a script on a linux
>> machine and use the Win32 extensions...since I need those to
>> use WMI python module as well.  Is this actually possible?
>>
>
> Yes if you spend a lot of money.
> You can buy a set of libraries from a comany called Bristol
> Technology (I think, it's over 8 years since I used it!) that allows
> you to build Windows software on a Unix box. A bit like cygwin
> in reverse! Some Microsoft developers actually used it during the
> development of the first version of Windows NT I believe!
>
> However a more practical route is probably to just install
> VMWare (or similar virtualisation software) and a cheap copy
> of Windows XP Pro/Home.
>
>  I am deploying Windows Servers from behind
>> a linux PXE.
>>
>
> PXE?
>
> HTH,
>
> Alan G.
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Spencer Parker
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Releasing a File for Access

2008-09-11 Thread Wayne Watson
Title: Signature.html




Thanks. So noted. I suspect I need to use something like sys.exit().
However, I'm in development mode right now with IDLE, and there's a
small penalty for using it with IDLE. I think it's a dialog that asks
if one wants to really exit.

The files are maybe 70 characters per line, and typically no more than
100 lines. I know of no other way to make it faster. It should be
sufficient for these purposes. Although I user might try to rename  500
files at a single shot. 

bob gailer wrote:

  
Wayne Watson wrote:
  Enclosed
is a segment of a program which copies a txt file, but
replaces the first line with a new one. The new one has a suffix of
tmp. After it executed the code and exited the program normally
(running out of code), I couldn't access the new tmp file. It was
listed by Win XP, but I got a "file in use" messge. Do I have to exit
differently to free the tmp file for inspection?
  
  
I don't have the answer - but a suggestion for cleaner code. Note I
added \n at the end of the header!
  
def modify_txt_file(old_fname, new_event_date):
    # Open old txt file and change first line with date & time of
event
    # renaming will occur a little later
    #    old_fname is complete file name
    #    new_event_date is date like: Tue 2008/03/... 14: ...
    old_prefix = old_fname[0:19]
    input_file=open(old_fname,'r')
    #copy to temporary
    output_file=open(old_prefix+'.tmp','w')
  
  
     input_file.readline() # discard first line
         output_file.write("Event time: " + new_event_date
+
"\n") # replace header
         output_file.write(input_file.read()) #
copy rest of file - assuming file is not BIG
  
  
    output_file.close()
    input_file.close()
    print "modified txt file with event info"
    # now copy tmp back to ...
    return

  
  
  
  -- 
Bob Gailer
Chapel Hill NC 
919-636-4239

When we take the time to be aware of our feelings and 
needs we have more satisfying interatctions with others.

Nonviolent Communication provides tools for this awareness.

As a coach and trainer I can assist you in learning this process.

What is YOUR biggest relationship challenge?
  


-- 


   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

  "If voting made any difference they wouldn't let us do it." 
-- Mark Twain

Web Page: 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Releasing a File for Access

2008-09-11 Thread bob gailer




Wayne Watson wrote:
Enclosed
is a segment of a program which copies a txt file, but
replaces the first line with a new one. The new one has a suffix of
tmp. After it executed the code and exited the program normally
(running out of code), I couldn't access the new tmp file. It was
listed by Win XP, but I got a "file in use" messge. Do I have to exit
differently to free the tmp file for inspection?


I don't have the answer - but a suggestion for cleaner code. Note I
added \n at the end of the header!

  def modify_txt_file(old_fname, new_event_date):
    # Open old txt file and change first line with date & time of
event
    # renaming will occur a little later
    #    old_fname is complete file name
    #    new_event_date is date like: Tue 2008/03/... 14: ...
    old_prefix = old_fname[0:19]
    input_file=open(old_fname,'r')
    #copy to temporary
    output_file=open(old_prefix+'.tmp','w')


   input_file.readline() # discard first line
       output_file.write("Event time: " + new_event_date +
"\n") # replace header
       output_file.write(input_file.read()) #
copy rest of file - assuming file is not BIG


      output_file.close()
    input_file.close()
    print "modified txt file with event info"
    # now copy tmp back to ...
    return
  



-- 
Bob Gailer
Chapel Hill NC 
919-636-4239

When we take the time to be aware of our feelings and 
needs we have more satisfying interatctions with others.

Nonviolent Communication provides tools for this awareness.

As a coach and trainer I can assist you in learning this process.

What is YOUR biggest relationship challenge?



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Threading

2008-09-11 Thread Kent Johnson
On Thu, Sep 11, 2008 at 10:56 AM, Oleg Oltar <[EMAIL PROTECTED]> wrote:
> Hi!
>
> I need to open about 1200 urls from my database. And to check that those
> urls are really exists.
>
> I used urllib2.urlopen for it. But it's a little bit slow. I thought that
> it's might be a good idea to do it in a threads. So it can add some
> performance to my code.
>
> Unfortunately I can't get started with the treading module.  There are no
> simple examples in the python docs. Not sure how to start.

There are some good examples of threading in the Python cookbook, or this one:
http://www.chrisarndt.de/projects/threadpool/

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Releasing a File for Access

2008-09-11 Thread Wayne Watson
Title: Signature.html




Enclosed is a segment of a program which copies a txt file, but
replaces the first line with a new one. The new one has a suffix of
tmp. After it executed the code and exited the program normally
(running out of code), I couldn't access the new tmp file. It was
listed by Win XP, but I got a "file in use" messge. Do I have to exit
differently to free the tmp file for inspection?

def modify_txt_file(old_fname, new_event_date):
    # Open old txt file and change first line with date & time of
event
    # renaming will occur a little later
    #    old_fname is complete file name
    #    new_event_date is date like: Tue 2008/03/... 14: ...
    old_prefix = old_fname[0:19]
    input_file=open(old_fname,'r')
    #copy to temporary
    output_file=open(old_prefix+'.tmp','w')
    for j, line in enumerate(input_file):
    if j == 0: # replace header
    output_file.write("Event time: "+new_event_date)
    continue
    output_file.write(line) # copy other lines
    output_file.close()
    input_file.close()
    print "modified txt file with event info"
    # now copy tmp back to ...
    return

-- 


   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

  "If voting made any difference they wouldn't let us do it." 
-- Mark Twain

Web Page: 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] No Blank Separator between Date and Time Valid?

2008-09-11 Thread Martin Walsh
Wayne Watson wrote:
> This program segment allows an invalid date to go undetected. See below.
> 
> def set_time_stamp(d1):
> # /mm/dd hh:mm:ss in, vmmdd_hhmmss.27 out
> formatin = '%Y/%m/%d %H:%M:%S'
> d1 = d1.lstrip()
> try:
> date1 = datetime(*(time.strptime(d1, formatin)[0:6]))
> except ValueError:
> print; print "Invalid date input. Use /mm/dd hh:mm:ss."
> return False

> 
> Enter date and time: 2008/1/100:00:30 <- Why is this valid. The
> fields are not spearated.
> dout:  20080110_30
> prefix:  v20080110_30.27
> OK:  v20080110_30.27
> 


I have confirmed there is a difference in the behavior of time.strptime
between python 2.4 and 2.5, and I assume you're using 2.4.

It is possibly related to this bug (but hard to say for sure without
looking at the source)...

 http://bugs.python.org/issue1340337

... and the subsequent fix for 2.5. But, clearly something changed
between releases.

from http://www.python.org/download/releases/2.5/NEWS.txt
"""
- Bug #1340337: change time.strptime() to always return ValueError when
there is an error in the format string.
- Bug #1290505: Fix clearing the regex cache for time.strptime().
"""

HTH,
Marty
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] No Blank Separator between Date and Time Valid?

2008-09-11 Thread Johan Geldenhuys
It all depends how you specify the format of the time you want, look at the
example. 

 

>>> time.strftime('%Y%M%D%H%M%S')

'200820092030'

>>> 

>>> 

>>> time.strftime('%Y%M%D %H%M%S')

'200820 092055'

>>> 

 

Notice in the second statement, I added the space separator between the %D
and %H.

 

This statement doesn’t fail and the exception doesn’t happen: date1 =
datetime(*(time.strptime(d1, formatin)[0:6]))



 

  _  

From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Wayne
Watson
Sent: Friday, 12 September 2008 2:56 AM
To: tutor@python.org
Subject: [Tutor] No Blank Separator between Date and Time Valid?

 

This program segment allows an invalid date to go undetected. See below.

from datetime import datetime
import time

def set_time_stamp(d1):
# /mm/dd hh:mm:ss in, vmmdd_hhmmss.27 out
formatin = '%Y/%m/%d %H:%M:%S'
d1 = d1.lstrip()
try:
date1 = datetime(*(time.strptime(d1, formatin)[0:6]))
except ValueError:
print; print "Invalid date input. Use /mm/dd hh:mm:ss."
return False
formatout = '%Y%m%d_%H%M%S'
dout = date1.strftime(formatout)
print "dout: ",dout
return 'v'+date1.strftime(formatout)+".27"

keyopt = 0
while keyopt == 0:
print
date_time = raw_input("Enter date and time: ")
if date_time == "end":
break
file_prefix = set_time_stamp(date_time)
print "prefix: ",file_prefix
if file_prefix == False:
continue
print "OK: ", file_prefix
#write_pair(file_prefix, date_time)

print; print "bye..."

Results:

Enter date and time: 2008/1/1 00:00:30 <- Valid input OK
dout:  20080101_30
prefix:  v20080101_30.27
OK:  v20080101_30.27

Enter date and time: 2008/1/100:00:30 <- Why is this valid. The fields
are not spearated.
dout:  20080110_30
prefix:  v20080110_30.27
OK:  v20080110_30.27

Enter date and time: 2008/1/1 x00:00:30  <- Invalid input caught

Invalid date input. Use /mm/dd hh:mm:ss.
prefix:  False

Enter date and time: end

bye ...

-- 



   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
 
 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

  "If voting made any difference they wouldn't let us do it." 
-- Mark Twain

Web Page: 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Win32 extensions

2008-09-11 Thread Alan Gauld


"Spencer Parker" <[EMAIL PROTECTED]> wrote

2003.  I was wondering if it is possible to create a script on a 
linux

machine and use the Win32 extensions...since I need those to
use WMI python module as well.  Is this actually possible?


Yes if you spend a lot of money.
You can buy a set of libraries from a comany called Bristol
Technology (I think, it's over 8 years since I used it!) that allows
you to build Windows software on a Unix box. A bit like cygwin
in reverse! Some Microsoft developers actually used it during the
development of the first version of Windows NT I believe!

However a more practical route is probably to just install
VMWare (or similar virtualisation software) and a cheap copy
of Windows XP Pro/Home.


I am deploying Windows Servers from behind
a linux PXE.


PXE?

HTH,

Alan G. 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to create array of variants?

2008-09-11 Thread Alan Gauld


"Krasyn" <[EMAIL PROTECTED]> wrote



I'm trying to translate the following VB code into Python and not 
sure how

to create an array of variants.


All variables in Python are effectively variants - variables that can
store any type. So an array of variants equates to a Python list


VB Code:
Sub SetXdata()


def SetXData():


Dim lineObj As AcadLine
Set lineObj = ThisDrawing.ModelSpace.Item(0)



lineObj = ThisDrawing.ModelSpace.Item(0)

Where ThisDrawing.ModelSpace.Item(0) is s0ome kind of
data structure you have defined elsewhere. Or fetch using COM.


Dim DataType(0 To 1) As Integer
Dim Data(0 To 1) As Variant


DataType = []
Data = []

But it looks like you are trying to fake a dictionary - although VB
has dictionaries!


DataType(0) = 1001: Data(0) = "Test_Application"
DataType(1) = 1070: Data(1) = 600


Data = {'Test_application' : 1001, 600 : 1070}


lineObj.SetXdata DataType, Data


lineObj.SetXdata( Data )


Python code
import array
import comtypes.client

def SetXData():
activedoc =
comtypes.client.GetActiveObject("AutoCAD.Application").ActiveDocument
line = activedoc.ModelSpace.Item(0)

dataType = array.array('i', [1001, 1070])
dataValue = array.array('?', ['Test_Application', 600]) #What 
should I

use
for the type code?

line.SetXData(dataType, dataValue)


Here's the snag with the dictionary approach so its back to
two lists.  I wouldn't use the array module just lists.

It might be enough to just pass DataType and Data as two lists
into the COM object. I don't know enough about Python's COM
integration to be sure that it will sort it all out though. But I 
suspect

it will.


--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking at a String as a Struct?

2008-09-11 Thread Alan Gauld


"Wayne Watson" <[EMAIL PROTECTED]> wrote


lIs it possible in Python to look at a string as a "struct".
I assume you mean you have a lot of data all stored in a single 
string?
I also assume that either the string has a fixed delimiter - like a 
comma

separated file?
Or it has fixed length fields?


I don't think a struct exists in python.


What is a struct? It is a data record that can hold different field 
types.

So yes struct exists in Python in a multitude of forms:
a) a tuple is an immutable collection of items
b) a list is a mutable collection of items
c) a class without methods is most like a C struct

So you have a choice.
However I suspect that what you want is more like a C union.
And no Python does not have a union concept.

The best we can do is either use someting like a regex to
extract fields based on the separator or use the struct
module (now there's a clue?!) to extract fixed length fields
from a string of bytes. struct can e used for binary data but
it can also be used for fixed length string fields inside a
character string.

Once extracted move the new fields (with type conversion
as needed) into the fields of a class or into a tuple/list.
If you use a class you cancombine the decoding with
the assignment in the constructor (__init__ method)


You can read about all opf these data types in the
Raw Materials topic of my tutor. You can read about
classes in the OOP tiopic and you can read a bit
about struct in the File Handling topic.

HTH,


--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Win32 extensions

2008-09-11 Thread Spencer Parker
I am tasked with a project to use WMI to script something on Windows Server
2003.  I was wondering if it is possible to create a script on a linux
machine and use the Win32 extensions...since I need those to use WMI python
module as well.  Is this actually possible?  Otherwise I would have to use a
windows server in order to run these scripts.  I do not want to use VB is I
don't have or a windows server.  I am deploying Windows Servers from behind
a linux PXE.

-- 
Spencer Parker
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Threading

2008-09-11 Thread Jeff Younker

On Sep 11, 2008, at 7:56 AM, Oleg Oltar wrote:


Hi!

I need to open about 1200 urls from my database. And to check that  
those urls are really exists.


I used urllib2.urlopen for it. But it's a little bit slow. I thought  
that it's might be a good idea to do it in a threads. So it can add  
some performance to my code.


Unfortunately I can't get started with the treading module.  There  
are no simple examples in the python docs. Not sure how to start.



What I want to have:
for my list of urls I want to start few threads, each one of them  
should do some validation.


My code without threads:



The first thing to do is to break your program into phases.  Right now
all your logic is intermixed.   This isn't a good thing when working
with threads.  You want to isolate the parts that need parallelization
from the parts that don't.

A general stylistic note:  Don't start your code with utility and helper
functions.  You're telling a story.  Start with the big overarching  
logic
that orchestrates all the bits in pieces.  That's the plot of your  
story. The

little helper functions are like details about a character.  They get in
the way until you've figured out what's going on.

It seems to me like you have three distinct parts:

- get the list of urls from the database
- check the urls
- report the urls status

So your main method will look something like:

def test_urls():
   urls = urls_from_database()
   checked_urls = check(urls)
   report(checked_urls)

The only method that needs to be parallelized is check(urls).

One approach to threading is to start one thread for every
URL, but if you have too many URLs you could bring your
machine to its knees.  Doing 30 things at once may be fine,
but doing 1000 is quite possibly a problem, so you have to
limit the number of threads.

The classic way of doing this uses a pool of tasks and a number
of worker threads that pull jobs out of the pool.  Your master
thread fills the pool, starts a number of worker threads, and then
waits for them to finish.

Each worker thread pulls a job from the pool, performs the job,
writes the results to another pool. When the pool is empty the
program continues.

Your check method might be something like this:

from Queue import Queue
from threading import Thread
...
def check(urls):
unchecked_urls = Queue()  # a queue full of url strings
checked_urls = Queue()  # a queue full of (url string, is_good  
boolean) tuples

fill_job_pool(unchecked_urls, urls)
start_worker_threads(unchecked_urls, checked_urls,  
number_workers=10)

# waits until all the jobs have been emptied from the queue
unchecked_urls.join()
return results_from(checked_urls)

def fill_job_pool(unchecked_urls, urls):
for url in urls:
unchecked_urls.put(url)

def start_worker_threads(unchecked_urls, checked_urls, number_workers):
for x in range(0, number_workers):
	# Creates a thread object that will call  
worker_thread(unchecked_urls, checked_urls)

# when it is started.
worker = Thread(target=worker_thread, args=(unchecked_urls,  
checked_urls))
	# Python will terminate even if this thread is still alive. This  
means that the

# thread doesn't need to kill itself.
worker.setDaemon(True)
# Start the worker thread
worker.start()

def results_from(checked_urls):
results = []
while not checked_urls.empty():
 results.append(checked_urls.get())
return results

def worker_thread(job_pool, result_pool):
while True:
url = job_pool.get()
is_good = check_url(url)
result = (url, is_good)
result_pool.put(result)
job_pool.task_done()

def check_url(url):
# YOUR URL CHECK HERE
return True

Once you plug this into your program, you'll start finding ways that
you can shorten the whole program.   Instead of passing around
arrays or urls and results you can pass around the queues directly.
In addition you can run the report function as another thread. It prints
the jobs from the result pool as they're completed.  These will make
the code more elegant, but the solution here gets at the heart of the
problem.


- Jeff Younker - [EMAIL PROTECTED] -

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] importing strings

2008-09-11 Thread Jeff Younker

On Sep 11, 2008, at 2:15 PM, Patrick wrote:


I have been able to import a string from another module threw a
dictionary but I can't seem to figure out how to do it so that I can
insert a value into the %s placeholder afterwards.


The % operator is probably what you are looking for.

format = "this is a %s"
subst = 'test'
print format % subst

FYI: You shouldn't reply to a message when you have a new
question.  That attaches it to the previous topic.

- Jeff Younker - [EMAIL PROTECTED] -
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] importing strings

2008-09-11 Thread Patrick
I would like to import a string with a "placeholder" in it.

for example  "this is a %s"

I would then like to insert the value into it after importation, "this
is a %s" (test)

I have been able to import a string from another module threw a
dictionary but I can't seem to figure out how to do it so that I can
insert a value into the %s placeholder afterwards.

I can send over more code if this is an incomplete description.

Thanks in advance-Patrick
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to create array of variants?

2008-09-11 Thread Krasyn


Kelie-2 wrote:
> 
> Hello group,
> 
> I'm trying to translate the following VB code into Python and not sure how
> to
> create an array of variants. 
> 
> Thanks for your help!
> 
> VB Code:
> Sub SetXdata()
> Dim lineObj As AcadLine
> Set lineObj = ThisDrawing.ModelSpace.Item(0)
> 
> Dim DataType(0 To 1) As Integer
> Dim Data(0 To 1) As Variant
> 
> DataType(0) = 1001: Data(0) = "Test_Application"
> DataType(1) = 1070: Data(1) = 600
>
> lineObj.SetXdata DataType, Data
> End Sub
> 
> Python code
> import array
> import comtypes.client
> 
> def SetXData():
> activedoc =
> comtypes.client.GetActiveObject("AutoCAD.Application").ActiveDocument
> line = activedoc.ModelSpace.Item(0)
> 
> dataType = array.array('i', [1001, 1070])
> dataValue = array.array('?', ['Test_Application', 600]) #What should I
> use
> for the type code?
> 
> line.SetXData(dataType, dataValue)
> 
> if __name__ == "__main__":
> SetXData()
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
> 
> 
dataType = array("h",[1001,1070])
dataValue = VARIANT(['Test_Application', 600])
-- 
View this message in context: 
http://www.nabble.com/How-to-create-array-of-variants--tp18331322p19441514.html
Sent from the Python - tutor mailing list archive at Nabble.com.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] No Blank Separator between Date and Time Valid?

2008-09-11 Thread Wayne Watson
Title: Signature.html




This program segment allows an invalid date to go undetected. See below.

from datetime import datetime
import time
  
def set_time_stamp(d1):
    # /mm/dd hh:mm:ss in, vmmdd_hhmmss.27 out
    formatin = '%Y/%m/%d %H:%M:%S'
    d1 = d1.lstrip()
    try:
    date1 = datetime(*(time.strptime(d1, formatin)[0:6]))
    except ValueError:
    print; print "Invalid date input. Use /mm/dd hh:mm:ss."
    return False
    formatout = '%Y%m%d_%H%M%S'
    dout = date1.strftime(formatout)
    print "dout: ",dout
    return 'v'+date1.strftime(formatout)+".27"
  
keyopt = 0
while keyopt == 0:
    print
    date_time = raw_input("Enter date and time: ")
    if date_time == "end":
    break
    file_prefix = set_time_stamp(date_time)
    print "prefix: ",file_prefix
    if file_prefix == False:
    continue
    print "OK: ", file_prefix
    #write_pair(file_prefix, date_time)
  
print; print "bye..."

Results:
Enter date and time: 2008/1/1 00:00:30 <- Valid
input OK
dout:  20080101_30
prefix:  v20080101_30.27
OK:  v20080101_30.27
  
Enter date and time: 2008/1/100:00:30 <- Why is this valid. The
fields are not spearated.
dout:  20080110_30
prefix:  v20080110_30.27
OK:  v20080110_30.27
  
Enter date and time: 2008/1/1 x00:00:30  <- Invalid input caught
  
Invalid date input. Use /mm/dd hh:mm:ss.
prefix:  False
  
Enter date and time: end
  
bye ...

-- 


   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

  "If voting made any difference they wouldn't let us do it." 
-- Mark Twain

Web Page: 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking at a String as a Struct?

2008-09-11 Thread Wayne Watson
Title: Signature.html






Wayne Watson wrote:

  
Thanks. I had a hunch that might be a good way to do it. I saw
something like this in other s/w. Now I know what they were up to. 
  
Omer wrote:
  
Class Person:
    def __init__(str):
        self.Firstname = str[0:4]
        self.Surname = str[5:7]
        (...)

If your string separates the values of each person using tags rather
than fixed lengthes, build it like:

        or:
        self.Firstname = str[0:str.find("Last name:")]
        self.Surname = str[str.find("Last name:")+len("Last
name:"):str.find("date_of_birth")]

And just create a list of these, adding various get methods for easy
information retrieval.

(Here's for wandering whether my way of doing it counts as Pythonic.)
HTH.
Omer.

On Thu, Sep 11, 2008 at 4:58 PM, Wayne
Watson <[EMAIL PROTECTED]>
wrote:

  True enough, but that gets
messy. I'd have to keep them perhaps as
global variables or pass then around a lot from function to function as
a collection.  I see WW posted above you about  dictionaries. Maybe
that's the way to do it.  I'll look into it. 
  
Kent Johnson wrote:
  
On Thu, Sep 11, 2008 at 6:58 AM, Wayne Watson
<[EMAIL PROTECTED]> wrote:
  

  Is it possible in Python to look at a string as a "struct". I don't think a
struct exists in python. Actually, is there something analogous to a record.
In the case of strings, suppose I have string that is composed of
sub-strings like, first_name, last-name, date_of birth, which consists of
month, day, and year, and finally SSN, street_address, state, city, and
zip_code. I'd like to access these fields directly instead of lastname =
record[38:55]. What if fields are not just strings, but some numeric values?


For numeric fields, just convert as needed:
quantity = int(record[55:60])
price = float(record[60::70])

If the numbers are binary, rather than ascii, see the struct module.

Kent

  
  
  
  
  
  -- 
 Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

  "If voting made any difference they wouldn't let us do it." 
-- Mark Twain

Web Page: 
  
  
  
  
___
Tutor maillist  -  Tutor@python.org
  http://mail.python.org/mailman/listinfo/tutor
  




  
  
  -- 
  
  
 Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

  "If voting made any difference they wouldn't let us do it." 
-- Mark Twain

Web Page: 
  


-- 

Signature.html
   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

  "If voting made any difference they wouldn't let us do it." 
-- Mark Twain

Web Page: 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking at a String as a Struct?

2008-09-11 Thread Marc Tompkins
On Thu, Sep 11, 2008 at 3:58 AM, Wayne Watson
<[EMAIL PROTECTED]>wrote:

>  Is it possible in Python to look at a string as a "struct". I don't think
> a struct exists in python. Actually, is there something analogous to a
> record. In the case of strings, suppose I have string that is composed of
> sub-strings like, first_name, last-name, date_of birth, which consists of
> month, day, and year, and finally SSN, street_address, state, city, and
> zip_code. I'd like to access these fields directly instead of lastname =
> record[38:55]. What if fields are not just strings, but some numeric values?
>
>

I had to do a similar thing in my first real Python program (actually, I
ended up making it into a library of modules that I reuse over and over.)  I
do a lot of work with a legacy cTree database application - fixed-length
data records, C strings, lots of stuff stored as packed decimal, etc.  The
database is normalized into 45 files, each with its own record length and
layout.  I wrote 45 separate classes; here's "Patient".
Note: this is OLD code, and I haven't revisited it to optimize.  If I ever
find the time, I'll rework these into generators - as it stands, opening
each file and reading records from it and loading the resulting objects into
a list or dictionary (or SQL database) is left to the calling program.
Remember, this was a beginner effort.  (But the programs that use this are
still going strong...)

You'll notice that I call, and use, "struct" - it doesn't do quite what you
think it does, I think...  I believe that the entire class is closer to what
you meant by "struct".

import struct
from utils import B2int, 2date, 2ssn, 2zip, raw2phone
# some utility functions - convert a single byte to integer, 4 bytes BCD to
a date,
#   4 bytes BCD to a Social Security number, 4 bytes BCD to a Zip code,
#   format a 10-digit number into "(999) 999-"

class Patient(object):
RecordLength = 1024
TLA = "pat"
ID = ""
def __init__(self, rec=False):
"""Extract fields from patient records"""
self.Valid  = False
if rec:
if not (rec[5] == "\x00"):  # skip deleted and invalid records
self.Valid = True
self.ChartNumber=
rec[5:16].split("\x00")[0].rstrip()  # these are C strings,
self.LastName   = "" +
rec[16:31].split("\x00")[0].rstrip() # therefore null-terminated -
self.FirstName  = "" +
rec[32:44].split("\x00")[0].rstrip()  # but also might
self.MI = "" + rec[45]
self.AccountNumber  =
rec[16:31].split("\x00")[0].rstrip()  # contain spaces
self.Address1   =
rec[982:1010].split("\x00")[0].rstrip()
self.Street =
rec[72:100].split("\x00")[0].rstrip()
self.City   =
rec[101:116].split("\x00")[0].rstrip()
self.State  =
rec[117:119].split("\x00")[0].rstrip()
self.Zip= 2zip(rec[120:124])
self.HomePhone  =
raw2phone(rec[124:134].split("\x00")[0])
self.DrivLicNum =
rec[135:147].split("\x00")[0].rstrip()
self.SSN= 2ssn(rec[148:152])
self.Birthdate  = 2date(rec[152:156])
self.Sex= rec[156].split("\x00")[0]
self.EmergContact   =
rec[157:187].split("\x00")[0].rstrip()
self.EmergPhone =
raw2phone(rec[188:198].split("\x00")[0])
self.EmergRelCode   =
rec[199:205].split("\x00")[0].rstrip()
self.Message1   =
rec[206:238].split("\x00")[0].rstrip()
self.Message2   =
rec[239:271].split("\x00")[0].rstrip()
self.NrstRelName=
rec[272:302].split("\x00")[0].rstrip()
self.NrstRelStreet  =
rec[303:331].split("\x00")[0].rstrip()
self.NrstRelCity=
rec[332:347].split("\x00")[0].rstrip()
self.NrstRelState   =
rec[348:350].split("\x00")[0].rstrip()
self.NrstRelZip = 2zip(rec[351:355])
self.NrstRelPhone   =
raw2phone(rec[355:365].split("\x00")[0])
self.NrstRelRelCode =
rec[366:372].split("\x00")[0].rstrip()
self.EmpRefSrcCode  = struct.unpack('B',rec[979])[0]
self.EmpName=
rec[373:403].split("\x00")[0].rstrip()
self.EmpContact =
rec[947:975].split("\x00")[0].rstrip()
self.EmpStreet  =
rec[404:432].split("\x00")[0].rstrip()
self.EmpCity=
rec[433:448].split("\x00")[0].rstrip()
self.EmpState   =
rec[455:457].split("\x00")[0].rstrip()
self.EmpZip = 2zip(rec[458:462])
self.EmpPhone   =
raw2pho

Re: [Tutor] Fwd: Looking at a String as a Struct?

2008-09-11 Thread Wayne Watson
Title: Signature.html




I've done far worse by hitting the wrong key! ;-) I see a post above
gets into the class idea. 

Jaggo wrote:

  Silly google sent in the middle of my editing. Good
thing it only sent to you then. Please, ignore these 2 messages. Sorry
'bout the mess.
  
  -- Forwarded message --
From: Jaggo <[EMAIL PROTECTED]>
Date: Thu, Sep 11, 2008 at 6:25 PM
Subject: Re: [Tutor] Looking at a String as a Struct?
To: Wayne Watson <[EMAIL PROTECTED]>
  
  
  I think had I faced this sort of a problem I would've
created a Class for 'em.
  
Class Person:
  
  
  
  On Thu, Sep 11, 2008 at 1:58 PM, Wayne Watson <[EMAIL PROTECTED]>
wrote:
  
  


Is it possible in Python to
look at a string as a "struct". I don't
think a struct exists in python. Actually, is there something analogous
to a record. In the case of strings, suppose I have string that is
composed of sub-strings like, first_name, last-name, date_of birth,
which consists of month, day, and year, and finally SSN,
street_address, state, city, and zip_code. I'd like to access these
fields directly instead of lastname = record[38:55]. What if fields are
not just strings, but some numeric values? 
-- 
   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

  "If voting made any difference they wouldn't let us do it." 
-- Mark Twain

Web Page: 





___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


  
  
  
  
  
  
  


-- 


   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

  "If voting made any difference they wouldn't let us do it." 
-- Mark Twain

Web Page: 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking at a String as a Struct?

2008-09-11 Thread Omer
Class Person:
def __init__(str):
self.Firstname = str[0:4]
self.Surname = str[5:7]
(...)

If your string separates the values of each person using tags rather than
fixed lengthes, build it like:

or:
self.Firstname = str[0:str.find("Last name:")]
self.Surname = str[str.find("Last name:")+len("Last
name:"):str.find("date_of_birth")]

And just create a list of these, adding various get methods for easy
information retrieval.

(Here's for wandering whether my way of doing it counts as Pythonic.)
HTH.
Omer.

On Thu, Sep 11, 2008 at 4:58 PM, Wayne Watson
<[EMAIL PROTECTED]>wrote:

>  True enough, but that gets messy. I'd have to keep them perhaps as global
> variables or pass then around a lot from function to function as a
> collection.  I see WW posted above you about  dictionaries. Maybe that's the
> way to do it.  I'll look into it.
>
> Kent Johnson wrote:
>
> On Thu, Sep 11, 2008 at 6:58 AM, Wayne Watson<[EMAIL PROTECTED]> <[EMAIL 
> PROTECTED]> wrote:
>
>
>  Is it possible in Python to look at a string as a "struct". I don't think a
> struct exists in python. Actually, is there something analogous to a record.
> In the case of strings, suppose I have string that is composed of
> sub-strings like, first_name, last-name, date_of birth, which consists of
> month, day, and year, and finally SSN, street_address, state, city, and
> zip_code. I'd like to access these fields directly instead of lastname =
> record[38:55]. What if fields are not just strings, but some numeric values?
>
>
>  For numeric fields, just convert as needed:
> quantity = int(record[55:60])
> price = float(record[60::70])
>
> If the numbers are binary, rather than ascii, see the struct module.
>
> Kent
>
>
>
>
> --
>
>Wayne Watson (Watson Adventures, Prop., Nevada City, CA)
>
>  (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
>   Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet   
>"If voting made any difference they wouldn't let us do it."
> -- Mark Twain
>
> Web Page: 
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] absolute beginner

2008-09-11 Thread Richard Lovely
I'd absolutely recommend "Python for Dummies" published by Wileys.

I've not used that actual book, but I've found other books in the
series extremely helpful.

http://www.amazon.com/Python-Dummies-Computer-Tech/dp/0471778648/ref=pd_bbs_sr_1?ie=UTF8&s=books&qid=1221146720&sr=8-1

On Thu, Sep 11, 2008 at 3:54 PM,  <[EMAIL PROTECTED]> wrote:

>  - Original Message -
>  From: Johnny
>  To: Python
>  Sent: Thursday, September 11, 2008 4:29 AM
>  Subject: [Tutor] absolute beginner
>
>
>  Anyone have any advice for an all out beginner?
>  Advice as in... The best book?...best tutor web page?
>  I am wanting so badly to learn Python.
>
>  I have went to this site...
>  http://www.awaretek.com/tutorials.html
>
>  This gave me lots of info,, but with so many books to choose from, I can't 
> seem to find that special one that will give me (a complete dummy) the info I 
> need.
>
>  thanks
>  Johnny
-- 
Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
www.theJNP.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] body extraction from mbox file - emails

2008-09-11 Thread grishma govani
The Code is take from the last example on the example page. When I run  
the command i see the msg lenght is 25 but it run through it only  
once, updates and counter and does not run through the rest of  
messages. I want to extract the body irrespective of whether there is  
an attachment or not.

I am not sure how exactly to do this. Can anybody help me out.

Thanks in advance.

-Grishma

Command:
python EmailParserNew.py -d /Users/tweek/Documents/ /Users/tweek/ 
Desktop/tmp/automated/Feedback

25
Counter: 2

Code:
print msg.__len__()
counter = 1
for part in msg.walk():
# multipart/* are just containers
##if part.get_content_maintype() == 'multipart':
##continue
# Applications should really sanitize the given filename so  
that an

# email message can't be used to overwrite important files
filename = part.get_filename()
if not filename:
ext = mimetypes.guess_extension(part.get_content_type())
if not ext:
# Use a generic bag-of-bits extension
ext = '.bin'
filename = 'part-%03d%s' % (counter, ext)
counter += 1
print 'Counter:',counter
fp = open(os.path.join(opts.directory, filename), 'wb')
fp.write(part.get_payload(decode=True))
fp.close()



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Threading

2008-09-11 Thread Oleg Oltar
Hi!

I need to open about 1200 urls from my database. And to check that those
urls are really exists.

I used urllib2.urlopen for it. But it's a little bit slow. I thought that
it's might be a good idea to do it in a threads. So it can add some
performance to my code.

Unfortunately I can't get started with the treading module.  There are no
simple examples in the python docs. Not sure how to start.


What I want to have:
for my list of urls I want to start few threads, each one of them should do
some validation.

My code without threads:

def queryDb(query, db_name = sys.argv[1]):
db = MySQLdb.connect(db = db_name, read_default_file="~/.my.cnf")
c = db.cursor()
c.execute(query)
return c


def openUrl(url):
error = ''

try:
urllib2.urlopen(url)
except (urllib2.URLError, urllib2.HTTPError,httplib.BadStatusLine),
error:
pass

return str(error)

def isValuePresent(dbField):
if str(dbField).find('None') is not -1:
return False
else:
return True

def displayResults(resultTable):
for query in resultTable:
print query

def processUrl(url, guid, parent_guid):


def testUrls():
error = ''
resultTable = []
errorTable = ['None']
products = queryDb(query = """SELECT url, guid, parent from product;""")
i = 0

for product in products.fetchall():

url = product[0]
guid = product[1]
parent_guid = product[2]
error_code = ''
#resultTable = {}

#print product[0]


if isValuePresent(url) is False:
#print "!", "this one is none", product[0]

"""Checking if this is a variant and it has parent with url"""
if isValuePresent(parent_guid) is True:
#print parent_guid, isValuePresent(parent_guid)
for parent in queryDb(query="""SELECT url from product where
guid = '%s';""" %parent_guid).fetchall():
#print parent[0]
'''Checking if parent product contains url'''
if isValuePresent(parent[0]) is True:
url = guid + "the parent url is: " + parent[0]
error = openUrl(parent[0])
else:

error = 'The variant with guid ' + guid + ' and his
parent do not contain urls!'
"""So the product is parent, and it's expected that it's child
will contain guids"""
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] absolute beginner

2008-09-11 Thread Evans
I'd start by going through Alan's marvellous material. They are very good for 
beginners like yourself. If you run into any problems, just post a message here.

http://www.freenetpages.co.uk/hp/alan.gauld

Happy coding!

--
Evans
http://www.javawug.org

  - Original Message - 
  From: Johnny 
  To: Python 
  Sent: Thursday, September 11, 2008 4:29 AM
  Subject: [Tutor] absolute beginner


  Anyone have any advice for an all out beginner?
  Advice as in... The best book?...best tutor web page?
  I am wanting so badly to learn Python.

  I have went to this site...
  http://www.awaretek.com/tutorials.html

  This gave me lots of info,, but with so many books to choose from, I can't 
seem to find that special one that will give me (a complete dummy) the info I 
need.

  thanks
  Johnny




--


  ___
  Tutor maillist  -  Tutor@python.org
  http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking at a String as a Struct?

2008-09-11 Thread Wayne Watson
Title: Signature.html




True enough, but that gets messy. I'd have to keep them perhaps as
global variables or pass then around a lot from function to function as
a collection.  I see WW posted above you about  dictionaries. Maybe
that's the way to do it.  I'll look into it. 

Kent Johnson wrote:

  On Thu, Sep 11, 2008 at 6:58 AM, Wayne Watson
<[EMAIL PROTECTED]> wrote:
  
  
Is it possible in Python to look at a string as a "struct". I don't think a
struct exists in python. Actually, is there something analogous to a record.
In the case of strings, suppose I have string that is composed of
sub-strings like, first_name, last-name, date_of birth, which consists of
month, day, and year, and finally SSN, street_address, state, city, and
zip_code. I'd like to access these fields directly instead of lastname =
record[38:55]. What if fields are not just strings, but some numeric values?

  
  
For numeric fields, just convert as needed:
quantity = int(record[55:60])
price = float(record[60::70])

If the numbers are binary, rather than ascii, see the struct module.

Kent

  


-- 


   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

  "If voting made any difference they wouldn't let us do it." 
-- Mark Twain

Web Page: 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking at a String as a Struct?

2008-09-11 Thread Kent Johnson
On Thu, Sep 11, 2008 at 6:58 AM, Wayne Watson
<[EMAIL PROTECTED]> wrote:
> Is it possible in Python to look at a string as a "struct". I don't think a
> struct exists in python. Actually, is there something analogous to a record.
> In the case of strings, suppose I have string that is composed of
> sub-strings like, first_name, last-name, date_of birth, which consists of
> month, day, and year, and finally SSN, street_address, state, city, and
> zip_code. I'd like to access these fields directly instead of lastname =
> record[38:55]. What if fields are not just strings, but some numeric values?

For numeric fields, just convert as needed:
quantity = int(record[55:60])
price = float(record[60::70])

If the numbers are binary, rather than ascii, see the struct module.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Looking at a String as a Struct?

2008-09-11 Thread W W
On Thu, Sep 11, 2008 at 5:58 AM, Wayne Watson
<[EMAIL PROTECTED]>wrote:

>  Is it possible in Python to look at a string as a "struct". I don't think
> a struct exists in python. Actually, is there something analogous to a
> record. In the case of strings, suppose I have string that is composed of
> sub-strings like, first_name, last-name, date_of birth, which consists of
> month, day, and year, and finally SSN, street_address, state, city, and
> zip_code. I'd like to access these fields directly instead of lastname =
> record[38:55]. What if fields are not just strings, but some numeric
> values?
>

My guess is you really want to use a dict. Perhaps a list of dicts... or a
dict of lists.

Some combination of the two should give you the functionality you're looking
for.

HTH,
Wayne
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Email and MIME

2008-09-11 Thread Kent Johnson
On Wed, Sep 10, 2008 at 10:30 PM, grishma govani <[EMAIL PROTECTED]> wrote:
> Yes, I used the part of the code from the second link.
> I am using the mailbox modules too.
>
> I have the e-mails from gmail in a file on my computer. I have used the code
> below extract all the headers. As you can see for now I am using text stored
> in document as my body. I just want to extract the plain text and leave out
> all the html, duplicates of plain text and all the other information like
> content type, from etc.

If you have an mbox format file, I suggest using mboxMailbox instead
of UnixMailbox. UnixMailbox is perhaps obsolete - it is not documented
and it uses the deprecated rfc822.Message to return the messages.
rfc822.Message doesn't seem to have any ability to decode the message
body, so you are getting the raw message data.

mboxMailbox returns email.Message objects which understand encoding
and multipart messages. Instead of msg.fp.read() you would use the
richer email.Message methods to retrieve the data. See the last
example at
http://docs.python.org/lib/node161.html
for some hints.

Kent

PS Please use Reply All to reply on list.

>
> mb = mailbox.UnixMailbox(file('tmp/automated/Feedback', 'r'))
> fout = file('Feedback.txt', 'w')
> msg = mb.next()
>
> while msg is not None:
>document = msg.fp.read()
>document = passthrough_filter(msg, document)
>msg = mb.next()
>
>
> def passthrough_filter(msg, document):
>"""This prints the 'from' address of the message and
>returns the document unchanged.
>"""
>from_addr = msg.getaddr('From')[0]
>Sub = msg.get('Subject')
>ContentType = msg.get('Content-Type')
>ContentDisp = msg.get('Content-Disposition')
>print "From:",from_addr
>print "Subject:",Sub
>print "Attachment:",None
>print "Body:",document
>print '\n'
>return document
>
>
>
>
> On 10 Sep 2008, at 22:09, Kent Johnson wrote:
>
>> On Wed, Sep 10, 2008 at 4:06 PM, grishma govani <[EMAIL PROTECTED]>
>> wrote:
>>>
>>> Hello Everybody,
>>>
>>> I have been trying to extract the body of all the email messages from an
>>> mbox file.
>>
>> How are you doing this? Have you seen the mailbox module and this recipe:
>> http://docs.python.org/lib/mailbox-mbox.html
>> http://code.activestate.com/recipes/157437/
>>
>> Kent
>
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Looking at a String as a Struct?

2008-09-11 Thread Wayne Watson
Title: Signature.html




Is it possible in Python to look at a string as a "struct". I don't
think a struct exists in python. Actually, is there something analogous
to a record. In the case of strings, suppose I have string that is
composed of sub-strings like, first_name, last-name, date_of birth,
which consists of month, day, and year, and finally SSN,
street_address, state, city, and zip_code. I'd like to access these
fields directly instead of lastname = record[38:55]. What if fields are
not just strings, but some numeric values? 
-- 


   Wayne Watson (Watson Adventures, Prop., Nevada City, CA)

 (121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
  Obz Site:  39° 15' 7" N, 121° 2' 32" W, 2700 feet

  "If voting made any difference they wouldn't let us do it." 
-- Mark Twain

Web Page: 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Inserting an Indent in IDLE?

2008-09-11 Thread Jaggo
In Pywin, and so I assume IDLE follows, you can just select more than One
line and press tab.

On Sun, Sep 7, 2008 at 5:41 PM, Alan Gauld <[EMAIL PROTECTED]>wrote:

>
> "Wayne Watson" <[EMAIL PROTECTED]> wrote
>
>  Signature.htmlOccasionally I would like to indent 20-30 lines of code.
>> I don't see a way to do this in IDLE other than brute force.
>>
>
> Format->Indent region?
>
> Ctrl-]
>
> Format Dedent region
>
> Ctrl-[
>
> Seems to work for me.
>
> Alan G
>
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] NetBeans IDE

2008-09-11 Thread Gmail

Hi Mike,

I use Eclipse for Java development with a very nice plugin for Python 
programming. Check out the PyDev plugin I think its for eclipse only - not 
sure if there's something for Netbeans.

http://pydev.sourceforge.net/

--
Evans
http://www.javawug.com

- Original Message - 
From: "Hansen, Mike" <[EMAIL PROTECTED]>

To: 
Sent: Wednesday, September 10, 2008 9:22 PM
Subject: [Tutor] NetBeans IDE


Being an editor/IDE junkie, I'm curious about the NetBeans IDE. Is anyone 
using it? If so, how is it? I believe it has a add-on for 
python.(NBPython)




Mike
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Extracting body of all email messages from an mbox file on computer

2008-09-11 Thread grishma govani

Yes, I used the part of the code from the second link.
I am using the mailbox modules too.

I have the e-mails from gmail in a file on my computer. I have used  
the code below extract all the headers. As you can see for now I am  
using text stored in document as my body. I just want to extract the  
plain text and leave out all the html, duplicates of plain text and  
all the other information like content type, from etc. Can anyone help  
me out?


mb = mailbox.UnixMailbox(file('tmp/automated/Feedback', 'r'))
fout = file('Feedback.txt', 'w')
msg = mb.next()

while msg is not None:
   document = msg.fp.read()
   document = passthrough_filter(msg, document)
   msg = mb.next()


def passthrough_filter(msg, document):
   """This prints the 'from' address of the message and
   returns the document unchanged.
   """
   from_addr = msg.getaddr('From')[0]
   Sub = msg.get('Subject')
   ContentType = msg.get('Content-Type')
   ContentDisp = msg.get('Content-Disposition')
   print "From:",from_addr
   print "Subject:",Sub
   print "Attachment:",None
   print "Body:",document
   print '\n'
   return document




On 10 Sep 2008, at 22:09, Kent Johnson wrote:

On Wed, Sep 10, 2008 at 4:06 PM, grishma govani  
<[EMAIL PROTECTED]> wrote:

Hello Everybody,

I have been trying to extract the body of all the email messages  
from an

mbox file.


How are you doing this? Have you seen the mailbox module and this  
recipe:

http://docs.python.org/lib/mailbox-mbox.html
http://code.activestate.com/recipes/157437/

Kent


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor