Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Python 3 1a) Define a function called escape_URL_char() which takes a single-cha

ID: 3665266 • Letter: P

Question

Python 3

1a) Define a function called escape_URL_char() which takes a single-character* string and checks if it needs to be escaped or not. If it does not need to be escaped, the function should just return the given string. If it does need to be escaped, the function should return an excape sequence that represents that character.

For the purposes of this problem*, the only characters that need to be escaped are the space " " (which should be replaced by a plus sign "+"), the forward slash "/" (which should be replaced by "%2F"), the hash sign "#" (which should be replaced by "%23"), and the equals sign "=" (which should be replaced by "%3D").

So for example, escape_URL_char(" ") should return "+", escape_URL_char("=")should return "%3D", and escape_URL_char("X") should return "X".

1b) Define a function called escape_URL() which takes a string (typically with more than one character, but not necessarily) and returns an escaped version of that string, usingescape_URL_char() to escape all the necessary characters. So for example,escape_URL("12/3 = 4") should return "12%2F3+%3D+4" and escape_URL("foobar")should return "foobar".

Explanation / Answer

----------------------------
import urllib
from django import template
from django.utils.safestring import mark_safe

register = template.Library()

'''
Function   : escape_url
Description   : escape a string so its safe in the url
Parameter(s)   : value - the string to be escaped
Return       : string that is safe to put into a url
'''
@register.filter
def escape_url( value ):

   result = urllib.quote_plus(value.replace("/", "%2F"))
  
   return mark_safe(result)

----------------------


import os
import string
import sys
import time

def iif(cond, true, false):
    '''A functional equivalent to C's ?: operator.

    This function returns the value of 'true' if 'cond' evaluates to non-zero,
    or the value of 'false' otherwise. Since it's a function, both 'true' and
    'false' are unconditionally evaluated (unlike 'cond?true:false' in C).
    '''
    if cond:
        return true
    else:
        return false

def escape(str, escapes):
    for (needle, replacement) in escapes:
        str = string.replace(str, needle, replacement)
    return str

html_escapes = ( ('&', '&'),
                 ('<', '&lt;'),
                 ('>', '&gt;'),
                 ('"', '&quot;') )

def escape_html(str): return escape(str, html_escapes)

url_escapes = ( ('%', '%25'),
                (' ', '%20'),
                ('&', '%26'),
                ('?', '%3f'),
                (':', '%3a'),
                (';', '%3b'),
                ('+', '%2b') )

def escape_url(str): return escape(str, url_escapes)

def nl2br(str): return string.replace(str, ' ', '<br>')

try:
   uri = os.environ['REQUEST_URI']
   try: base_uri = uri[string.rindex(uri, '/')+1:]
   except ValueError: base_uri = uri
   try: base_uri = base_uri[:string.index(base_uri, '?')]
   except ValueError: pass
except KeyError:
   uri = None
   base_uri = 'None'

def relink(_base_=base_uri, **args):
   args = args.items()
   args.sort()
   args = map(lambda val:"%s=%s"%(val[0],escape_url(str(val[1]))), args)
   return "%s?%s" % (_base_, string.join(args, '&'))

exports = {
    'iif': iif,
    'escape': escape,
    'html': escape_html,
   'nl2br': nl2br,
   'relink': relink,
    'url': escape_url,
   'sys': sys,
   'time': time,
    }