''' Module url: provide function(s) in support of getting web-based data into the needed information structures. ''' # need help to get web data import urllib.request def get_contents( link ) : ''' returns the contents of the web resource indicated by parameter link ''' stream = urllib.request.urlopen( link ) # connect to resource stream bytes = stream.read() # read stream to gets its contents content = bytes.decode( 'UTF-8' ) # decode contents to get text return content def get_lines( link ) : ''' returns the lines of text stored at the web resource indicated by parameter link ''' # get the contents of the page data = get_contents( link ) # strip the data of surrounding whitespace data = data.strip() # split data into lines lines = data.split( '\n' ) # return what they asked for return lines def get_strings( link ) : ''' returns the strings stored at the web resource indicated by parameter link ''' # get the contents of the page data = get_contents( link ) # strip the data of surrounding whitespace data = data.strip() # split data into strings strings = data.split( ) # return what they asked for return strings def get_csv_dataset( link ) : ''' returns the contents of the web resource indicated by parameter link as a list of lists of strings ''' contents = get_contents( link ) # get contents into dataset form dataset = contents.strip() dataset = dataset.split( '\n' ) nbr_rows = len( dataset ) for i in range( 0, nbr_rows ) : row = dataset[ i ] row = row.strip() row = row.split( ',' ) dataset[ i ] = row return dataset def get_and_convert_csv_dataset( link ) : ''' the contents of the web resource indicated by parameter link as a list of lists. The elements of the lists will be converted to int, float, or bool as appropriate ''' dataset = get_csv_dataset( link ) # get element contents into proper form nbr_rows = len( dataset ) for r in range( 0, nbr_rows ) : row = dataset[ r ] nbr_columns = len( row ) for c in range( 0, nbr_columns) : cell = row[ c ] if ( cell.isnumeric() ) : cell = int( cell ) elif ( cell.capitalize() == 'True' ) : cell = True elif ( cell.capitalize() == 'False' ) : cell = False else : try : float( cell ) cell = float( cell ) int( cell ) cell = int( cell ) except : pass row[ c ] = cell dataset[ r ] = row return dataset # constants BLACK = ( 0, 0, 0 ) WHITE = ( 255, 255, 255 ) from PIL import Image # needed for web support import urllib.request, io # process image acquistion and display def get_web_image( link ) : ''' returns the image at the web resource indicated by link ''' # get access to module Image from PIL import Image ''' Returns a pil image of the image named by link ''' # get a connection to the web resource name by link stream = urllib.request.urlopen( link ) # get the conents of the web resource data = stream.read() # convert the data to bytes bytes = io.BytesIO( data ) # get the image represented by the bytes image = Image.open( bytes ) # convert the image to RGB format image = image.convert('RGB') # hand back the image return image def get_student_image( id ) : ''' returns the image indicated web resource indicated by parameter id ''' STUDENT_REPOSITORY = 'http://www.cs.virginia.edu/~cs1112/people/' link = STUDENT_REPOSITORY + id + '/photo.jpg' return get_web_image( link ) def get_image( source ) : ''' returns an image from online, local source, or an existing Image ''' try : if ( str( type( source ) ) == "" ) : # check to if source is an existing Image image = source elif ( 'http://' == source[ 0 : 7 ].lower() ) : # look at the initial characters of source to see if its on the web # initial characters indicate the image is out on the web image = get_web_image( source ) else : # initial characters indicate the image is a local file image = Image.open( source ) image = image.convert( 'RGB' ) except : image = None return image def get_dictionary( link ) : ''' return the contents of the page indicated by parameter link as a dictionary ''' dataset = get_csv_dataset( link ) # initialize the dictionary dictionary = {} # accumulate the dictionary entries from the sheet for entry in dataset : key, value = entry dictionary[ key ] = value # return what they asked for return dictionary def scrub( drawing, bg=(255,255,255), fg=(0,0,0) ) : w, h = drawing.size for y in range( 0, h ) : for x in range( 0, w ) : spot = ( x, y ) r, g, b = drawing.getpixel( spot ) if ( ( r + g + b ) < 2.5 * 255 ) : drawing.putpixel( spot, fg ) else : drawing.putpixel( spot, bg ) return drawing