Changeset 3464 for DPPP


Ignore:
Timestamp:
20/02/08 10:22:55 (11 years ago)
Author:
mkochan
Message:

Changed the utils.wget() function -- switched to quicker implementation (urllib2), corrected exception handling, updated testUtils.py.

Location:
DPPP/kml/csml2kml/python/csml2kml/csml2kml
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • DPPP/kml/csml2kml/python/csml2kml/csml2kml/tests/testUtils.py

    r3439 r3464  
    11import utils 
    22 
    3 print utils.toDegreesMinutesSeconds( 123.5375675435435345) 
    4 print utils.toDegreesMinutesSeconds(- 43.0000100000000000) 
    5 print utils.toDegreesMinutesSeconds(  23.3333333333333433) 
     3# print utils.toDegreesMinutesSeconds( 123.5375675435435345) 
     4# print utils.toDegreesMinutesSeconds(- 43.0000100000000000) 
     5# print utils.toDegreesMinutesSeconds(  23.3333333333333433) 
     6 
     7print 'Script loaded. Testing utils.wget() only.' 
     8n = 10 
     9print 'Retrieving ' + str(n) + ' times.' 
     10for i in range(n): 
     11    content = utils.wget('http://bond.badc.rl.ac.uk:8089/dummyGeoServer/GetStationCSMLFeatures?gml_id=TWL1') 
     12    if content: 
     13        # print content 
     14        print 'Retrieval successful (edit testUtils.py to see it).' 
     15    else: 
     16        print 'Retrieval error.' 
  • DPPP/kml/csml2kml/python/csml2kml/csml2kml/utils.py

    r3439 r3464  
    1 import urllib 
     1import urllib2 
    22 
    33def wget(url): 
    44    ''' 
    5     Read data from an URL.  
    6     [TODO] Too slow at the moment -- urllib.urlopen() takes too long to complete; investigate. 
     5    Read data from an URL. 
     6    @param url: The URL. 
     7    @return:    The returned data as a string, or None on retrieval error. 
    78    ''' 
    89    content = None 
    910    try: 
    10         socket = urllib.urlopen(url)               # open a socket (actually a file-like object) 
    11         content = socket.read()                    # read the text in 
     11        try: 
     12            f = urllib2.urlopen(url)         # open a file-like object with the data 
     13            content = f.read()               # read the text in 
     14        except urllib2.HTTPError: 
     15            pass                             # error in retrieval, wget() will return None 
    1216    finally: 
    1317        try: 
    14             socket.close() 
     18            f.close() 
    1519        except NameError: 
    16             pass  # socket undefined -- no need to close 
     20            pass                             # f uninitialised, so no need to close it 
    1721    return content 
    1822 
    1923def toDegreesMinutesSeconds(val): 
     24    ''' 
     25    Determine how many integer degrees, minutes, seconds, and float parts of seconds there are 
     26    in an angle value given in float degrees. 
     27    @param val: An angle in float degrees. 
     28    @return:    A tuple containing (int degrees, int minutes, int seconds, float remainder in seconds). 
     29                The float remainder is a value between 0. (inclusive) and 1. (exclusive). 
     30    ''' 
    2031    if val >= 0:  
    2132        sign = 1 
Note: See TracChangeset for help on using the changeset viewer.