NCCOOS Trac Projects: Top | Web | Platforms | Processing | Viz | Sprints | Sandbox | (Wind)

Changeset 345

Show
Ignore:
Timestamp:
08/30/10 14:24:24
Author:
cbc
Message:

Pose file handlers as with statements.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • spongenet/trunk/spongenet/expand.py

    r344 r345  
    268268    >>> ref_path = xml_subdir + os.extsep + "ref" 
    269269    >>> xml_path = xml_subdir + os.extsep + "xml" 
    270     >>> ref_handle = open(ref_path) 
    271     >>> xml_handle = open(xml_path) 
    272     >>> reffile = ref_handle.read() 
    273     >>> xmlfile = xml_handle.read() 
    274     >>> ref_handle.close() 
    275     >>> xml_handle.close() 
     270    >>> with open(ref_path) as ref_handle: 
     271    ...    reffile = ref_handle.readlines() 
     272    >>> with open(xml_path) as xml_handle: 
     273    ...    xmlfile = xml_handle.readlines() 
    276274    >>> reffile == xmlfile 
    277275    True 
     
    293291    # Write a combined xml sponge data file. 
    294292    path = xml_subdir + os.extsep + "xml" 
    295     handle = open(path, "w") 
    296     handle.write(header) 
    297     handle.write("<root>\r\n") 
    298     handle.writelines(lines) 
    299     handle.write("\r\n</root>") 
    300     handle.close() 
     293    with open(path, "w") as handle: 
     294        handle.write(header) 
     295        handle.write("<root>\r\n") 
     296        handle.writelines(lines) 
     297        handle.write("\r\n</root>") 
    301298 
    302299    # Remove the xml sponge data file subdirectory. 
     
    331328    >>> truths = [] 
    332329    >>> for ref_path,xml_path in paths: 
    333     ...     ref_handle = open(ref_path) 
    334     ...     xml_handle = open(xml_path) 
    335     ...     reffile = ref_handle.read() 
    336     ...     xmlfile = xml_handle.read() 
    337     ...     ref_handle.close() 
    338     ...     xml_handle.close() 
     330    ...     with open(ref_path) as ref_handle: 
     331    ...        reffile = ref_handle.readlines() 
     332    ...     with open(xml_path) as xml_handle: 
     333    ...        xmlfile = xml_handle.readlines() 
    339334    ...     truths.extend((reffile == xmlfile, 
    340335    ...                    os.path.exists(os.path.splitext(xml_path)[0]),))