Instruction:
How would you code a function in Python 3 to
convert ints in the 0... 255 range to the standard [ 0... 1 ] range.

Response:
def ints_to_rgb(r, g=None, b=None):
  """Convert ints in the [0...255] range to the standard [0...1] range.

  Parameters:
    :r:
      The Red component value [0...255]
    :g:
      The Green component value [0...255]
    :b:
      The Blue component value [0...255]

  Returns:
    The color as an (r, g, b) tuple in the range:
    r[0...1],
    g[0...1],
    b[0...1]

  >>> '(%g, %g, %g)' % ints_to_rgb((255, 128, 0))
  '(1, 0.501961, 0)'

  """
  if type(r) in [list,tuple]:
    r, g, b = r
  return tuple(float(v) / 255.0 for v in [r, g, b])


Instruction:
Can you tell what is the following Python 3 function doing
def rgb_to_html(r, g=None, b=None):
  if type(r) in [list,tuple]:
    r, g, b = r
  return '#%02x%02x%02x' % tuple((min(round(v*255), 255) for v in (r, g, b)))

Response: Convert the color from rgb to html.


Instruction:
How would you code a function in Python 3 to
convert the HTML color to RGB.

Response:
def html_to_rgb(html):
  """Convert the HTML color to (r, g, b).

  Parameters:
    :html:
      the HTML definition of the color (#RRGGBB or #RGB or a color name).

  Returns:
    The color as an (r, g, b) tuple in the range:
    r[0...1],
    g[0...1],
    b[0...1]

  Throws:
    :ValueError:
      If html is neither a known color name or a hexadecimal RGB
      representation.

  >>> '(%g, %g, %g)' % html_to_rgb('#ff8000')
  '(1, 0.501961, 0)'
  >>> '(%g, %g, %g)' % html_to_rgb('ff8000')
  '(1, 0.501961, 0)'
  >>> '(%g, %g, %g)' % html_to_rgb('#f60')
  '(1, 0.4, 0)'
  >>> '(%g, %g, %g)' % html_to_rgb('f60')
  '(1, 0.4, 0)'
  >>> '(%g, %g, %g)' % html_to_rgb('lemonchiffon')
  '(1, 0.980392, 0.803922)'

  """
  html = html.strip().lower()
  if html[0]=='#':
    html = html[1:]
  elif html in NAMED_COLOR:
    html = NAMED_COLOR[html][1:]

  if len(html)==6:
    rgb = html[:2], html[2:4], html[4:]
  elif len(html)==3:
    rgb = ['%c%c' % (v,v) for v in html]
  else:
    raise ValueError("input #%s is not in #RRGGBB format" % html)

  return tuple(((int(n, 16) / 255.0) for n in rgb))


Instruction:
Here you have a function in Python 3, explain what it does
def rgb_to_pil(r, g=None, b=None):
  if type(r) in [list,tuple]:
    r, g, b = r
  r, g, b = [min(int(round(v*255)), 255) for v in (r, g, b)]
  return (b << 16) + (g << 8) + r

Response: Convert the color from RGB to PIL - compatible integer.


Instruction:
Implement a Python 3 function for
converting the color from a PIL - compatible integer to RGB.

Response:
def pil_to_rgb(pil):
  """Convert the color from a PIL-compatible integer to RGB.

  Parameters:
    pil: a PIL compatible color representation (0xBBGGRR)
  Returns:
    The color as an (r, g, b) tuple in the range:
    the range:
    r: [0...1]
    g: [0...1]
    b: [0...1]

  >>> '(%g, %g, %g)' % pil_to_rgb(0x0080ff)
  '(1, 0.501961, 0)'

  """
  r = 0xff & pil
  g = 0xff & (pil >> 8)
  b = 0xff & (pil >> 16)
  return tuple((v / 255.0 for v in (r, g, b)))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _websafe_component(c, alt=False):
  # This sucks, but floating point between 0 and 1 is quite fuzzy...
  # So we just change the scale a while to make the equality tests
  # work, otherwise it gets wrong at some decimal far to the right.
  sc = c * 100.0

  # If the color is already safe, return it straight away
  d = sc % 20
  if d==0: return c

  # Get the lower and upper safe values
  l = sc - d
  u = l + 20

  # Return the 'closest' value according to the alt flag
  if alt:
    if (sc-l) >= (u-sc): return l/100.0
    else: return u/100.0
  else:
    if (sc-l) >= (u-sc): return u/100.0
    else: return l/100.0

Response: Convert a color component to its web safe equivalent.


Instruction:
Create a Python 3 function for
converting the color from RGB to web safe RGB

Response:
def rgb_to_websafe(r, g=None, b=None, alt=False):
  """Convert the color from RGB to 'web safe' RGB

  Parameters:
    :r:
      The Red component value [0...1]
    :g:
      The Green component value [0...1]
    :b:
      The Blue component value [0...1]
    :alt:
      If True, use the alternative color instead of the nearest one.
      Can be used for dithering.

  Returns:
    The color as an (r, g, b) tuple in the range:
    the range:
    r[0...1],
    g[0...1],
    b[0...1]

  >>> '(%g, %g, %g)' % rgb_to_websafe(1, 0.55, 0.0)
  '(1, 0.6, 0)'

  """
  if type(r) in [list,tuple]:
    r, g, b = r
  websafeComponent = _websafe_component
  return tuple((websafeComponent(v, alt) for v in (r, g, b)))


Instruction:
Write a Python 3 script to
convert the color from RGB to its greyscale equivalent.

Response:
def rgb_to_greyscale(r, g=None, b=None):
  """Convert the color from RGB to its greyscale equivalent

  Parameters:
    :r:
      The Red component value [0...1]
    :g:
      The Green component value [0...1]
    :b:
      The Blue component value [0...1]

  Returns:
    The color as an (r, g, b) tuple in the range:
    the range:
    r[0...1],
    g[0...1],
    b[0...1]

  >>> '(%g, %g, %g)' % rgb_to_greyscale(1, 0.8, 0)
  '(0.6, 0.6, 0.6)'

  """
  if type(r) in [list,tuple]:
    r, g, b = r
  v = (r + g + b) / 3.0
  return (v, v, v)


Instruction:
Implement a function in Python 3 to
map a hue on the RGB color wheel to Itten s RYB wheel.

Response:
def rgb_to_ryb(hue):
  """Maps a hue on the RGB color wheel to Itten's RYB wheel.

  Parameters:
    :hue:
      The hue on the RGB color wheel [0...360]

  Returns:
    An approximation of the corresponding hue on Itten's RYB wheel.

  >>> rgb_to_ryb(15)
  26.0

  """
  d = hue % 15
  i = int(hue / 15)
  x0 = _RybWheel[i]
  x1 = _RybWheel[i+1]
  return x0 + (x1-x0) * d / 15


Instruction:
Make a summary of the following Python 3 code
def ryb_to_rgb(hue):
  d = hue % 15
  i = int(hue / 15)
  x0 = _RgbWheel[i]
  x1 = _RgbWheel[i+1]
  return x0 + (x1-x0) * d / 15

Response: Maps a hue on Itten s RYB color wheel to the standard RGB wheel.


Instruction:
Here you have a function in Python 3, explain what it does
def from_rgb(r, g, b, alpha=1.0, wref=_DEFAULT_WREF):
    return Color((r, g, b), 'rgb', alpha, wref)

Response: Create a new instance based on the specifed RGB values.


Instruction:
Can you generate the documentation for the following Python 3 function
def from_hsl(h, s, l, alpha=1.0, wref=_DEFAULT_WREF):
    return Color((h, s, l), 'hsl', alpha, wref)

Response: Create a new instance based on the specifed HSL values.


Instruction:
Create a Python 3 function for
creating a new instance based on the specifed HSV values.

Response:
def from_hsv(h, s, v, alpha=1.0, wref=_DEFAULT_WREF):
    """Create a new instance based on the specifed HSV values.

    Parameters:
      :h:
        The Hus component value [0...1]
      :s:
        The Saturation component value [0...1]
      :v:
        The Value component [0...1]
      :alpha:
        The color transparency [0...1], default is opaque
      :wref:
        The whitepoint reference, default is 2° D65.

    Returns:
      A grapefruit.Color instance.

    >>> Color.from_hsv(30, 1, 1)
    Color(1.0, 0.5, 0.0, 1.0)
    >>> Color.from_hsv(30, 1, 1, 0.5)
    Color(1.0, 0.5, 0.0, 0.5)

    """
    h2, s, l = rgb_to_hsl(*hsv_to_rgb(h, s, v))
    return Color((h, s, l), 'hsl', alpha, wref)


Instruction:
Can you write a function in Python 3 where it
creates a new instance based on the specifed YIQ values.

Response:
def from_yiq(y, i, q, alpha=1.0, wref=_DEFAULT_WREF):
    """Create a new instance based on the specifed YIQ values.

    Parameters:
      :y:
        The Y component value [0...1]
      :i:
        The I component value [0...1]
      :q:
        The Q component value [0...1]
      :alpha:
        The color transparency [0...1], default is opaque
      :wref:
        The whitepoint reference, default is 2° D65.

    Returns:
      A grapefruit.Color instance.

    >>> Color.from_yiq(0.5922, 0.45885,-0.05)
    Color(0.999902, 0.499955, -6.7e-05, 1.0)
    >>> Color.from_yiq(0.5922, 0.45885,-0.05, 0.5)
    Color(0.999902, 0.499955, -6.7e-05, 0.5)

    """
    return Color(yiq_to_rgb(y, i, q), 'rgb', alpha, wref)


Instruction:
Given the following Python 3 function, write the documentation
def from_yuv(y, u, v, alpha=1.0, wref=_DEFAULT_WREF):
    return Color(yuv_to_rgb(y, u, v), 'rgb', alpha, wref)

Response: Create a new instance based on the specifed YUV values.


Instruction:
Explain what the following Python 3 code does
def from_xyz(x, y, z, alpha=1.0, wref=_DEFAULT_WREF):
    return Color(xyz_to_rgb(x, y, z), 'rgb', alpha, wref)

Response: Create a new Color instance based on the specifed CIE - XYZ values.


Instruction:
Write a Python 3 function for
creating a new Color instance based on the specifed CIE - LAB values.

Response:
def from_lab(l, a, b, alpha=1.0, wref=_DEFAULT_WREF):
    """Create a new instance based on the specifed CIE-LAB values.

    Parameters:
      :l:
        The L component [0...100]
      :a:
        The a component [-1...1]
      :b:
        The a component [-1...1]
      :alpha:
        The color transparency [0...1], default is opaque
      :wref:
        The whitepoint reference, default is 2° D65.

    Returns:
      A grapefruit.Color instance.

    >>> Color.from_lab(66.951823, 0.43084105, 0.73969231)
    Color(1.0, 0.5, -0.0, 1.0)
    >>> Color.from_lab(66.951823, 0.41165967, 0.67282012, wref=WHITE_REFERENCE['std_D50'])
    Color(1.0, 0.5, -0.0, 1.0)
    >>> Color.from_lab(66.951823, 0.43084105, 0.73969231, 0.5)
    Color(1.0, 0.5, -0.0, 0.5)
    >>> Color.from_lab(66.951823, 0.41165967, 0.67282012, 0.5, WHITE_REFERENCE['std_D50'])
    Color(1.0, 0.5, -0.0, 0.5)

    """
    return Color(xyz_to_rgb(*lab_to_xyz(l, a, b, wref)), 'rgb', alpha, wref)


Instruction:
Here you have a function in Python 3, explain what it does
def from_cmy(c, m, y, alpha=1.0, wref=_DEFAULT_WREF):
    return Color(cmy_to_rgb(c, m, y), 'rgb', alpha, wref)

Response: Create a new instance based on the specifed CMY values.


Instruction:
Can you create a Python 3 function that
creates a new instance based on the specifed CMYK values.

Response:
def from_cmyk(c, m, y, k, alpha=1.0, wref=_DEFAULT_WREF):
    """Create a new instance based on the specifed CMYK values.

    Parameters:
      :c:
        The Cyan component value [0...1]
      :m:
        The Magenta component value [0...1]
      :y:
        The Yellow component value [0...1]
      :k:
        The Black component value [0...1]
      :alpha:
        The color transparency [0...1], default is opaque
      :wref:
        The whitepoint reference, default is 2° D65.

    Returns:
      A grapefruit.Color instance.

    >>> Color.from_cmyk(1, 0.32, 0, 0.5)
    Color(0.0, 0.34, 0.5, 1.0)
    >>> Color.from_cmyk(1, 0.32, 0, 0.5, 0.5)
    Color(0.0, 0.34, 0.5, 0.5)

    """
    return Color(cmy_to_rgb(*cmyk_to_cmy(c, m, y, k)), 'rgb', alpha, wref)


Instruction:
Given the following Python 3 function, write the documentation
def from_html(html, alpha=1.0, wref=_DEFAULT_WREF):
    return Color(html_to_rgb(html), 'rgb', alpha, wref)

Response: Create a new instance based on the specifed HTML color definition.


Instruction:
Can you create a Python 3 function that
creates a new instance based on the specified PIL color representation.

Response:
def from_pil(pil, alpha=1.0, wref=_DEFAULT_WREF):
    """Create a new instance based on the specifed PIL color.

    Parameters:
      :pil:
        A PIL compatible color representation (0xBBGGRR)
      :alpha:
        The color transparency [0...1], default is opaque
      :wref:
        The whitepoint reference, default is 2° D65.

    Returns:
      A grapefruit.Color instance.

    >>> Color.from_pil(0x0080ff)
    Color(1.0, 0.501961, 0.0, 1.0)
    >>> Color.from_pil(0x0080ff, 0.5)
    Color(1.0, 0.501961, 0.0, 0.5)

    """
    return Color(pil_to_rgb(pil), 'rgb', alpha, wref)


Instruction:
Given the following Python 3 function, write the documentation
def with_white_ref(self, wref, labAsRef=False):
    if labAsRef:
      l, a, b = self.lab
      return Color.from_lab(l, a, b, self.__a, wref)
    else:
      return Color(self.__rgb, 'rgb', self.__a, wref)

Response: Create a new instance based on this one with a new white reference.


Instruction:
Can you generate the documentation for the following Python 3 function
def desaturate(self, level):
    h, s, l = self.__hsl
    return Color((h, max(s - level, 0), l), 'hsl', self.__a, self.__wref)

Response: Create a new instance based on this one but less saturated.


Instruction:
Here you have a function in Python 3, explain what it does
def websafe_dither(self):
    return (
      Color(rgb_to_websafe(*self.__rgb), 'rgb', self.__a, self.__wref),
      Color(rgb_to_websafe(alt=True, *self.__rgb), 'rgb', self.__a, self.__wref))

Response: Return the two websafe colors nearest to this one.


Instruction:
Write a Python 3 script to
create a new instance which is the complementary color of this one.

Response:
def complementary_color(self, mode='ryb'):
    """Create a new instance which is the complementary color of this one.

    Parameters:
      :mode:
        Select which color wheel to use for the generation (ryb/rgb).

    Returns:
      A grapefruit.Color instance.

    >>> Color.from_hsl(30, 1, 0.5).complementary_color(mode='rgb')
    Color(0.0, 0.5, 1.0, 1.0)
    >>> Color.from_hsl(30, 1, 0.5).complementary_color(mode='rgb').hsl
    (210.0, 1.0, 0.5)

    """
    h, s, l = self.__hsl

    if mode == 'ryb': h = rgb_to_ryb(h)
    h = (h+180)%360
    if mode == 'ryb': h = ryb_to_rgb(h)

    return Color((h, s, l), 'hsl', self.__a, self.__wref)


Instruction:
Make a summary of the following Python 3 code
def make_analogous_scheme(self, angle=30, mode='ryb'):
    h, s, l = self.__hsl

    if mode == 'ryb': h = rgb_to_ryb(h)
    h += 360
    h1 = (h - angle) % 360
    h2 = (h + angle) % 360
    if mode == 'ryb':
      h1 = ryb_to_rgb(h1)
      h2 = ryb_to_rgb(h2)

    return (Color((h1, s,  l), 'hsl', self.__a, self.__wref),
        Color((h2, s,  l), 'hsl', self.__a, self.__wref))

Response: Return two colors analogous to this one.


Instruction:
How would you explain what the following Python 3 function does
def blend(self, other, percent=0.5):
    dest = 1.0 - percent
    rgb = tuple(((u * percent) + (v * dest) for u, v in zip(self.__rgb, other.__rgb)))
    a = (self.__a * percent) + (other.__a * dest)
    return Color(rgb, 'rgb', a, self.__wref)

Response: blends this color with the other one.


Instruction:
Make a summary of the following Python 3 code
def get_parser():
    parser = ArgumentParser(description='a command-line web scraping tool')
    parser.add_argument('query', metavar='QUERY', type=str, nargs='*',
                        help='URLs/files to scrape')
    parser.add_argument('-a', '--attributes', type=str, nargs='*',
                        help='extract text using tag attributes')
    parser.add_argument('-all', '--crawl-all', help='crawl all pages',
                        action='store_true')
    parser.add_argument('-c', '--crawl', type=str, nargs='*',
                        help='regexp rules for following new pages')
    parser.add_argument('-C', '--clear-cache', help='clear requests cache',
                        action='store_true')
    parser.add_argument('--csv', help='write files as csv',
                        action='store_true')
    parser.add_argument('-cs', '--cache-size', type=int, nargs='?',
                        help='size of page cache (default: 1000)',
                        default=1000)
    parser.add_argument('-f', '--filter', type=str, nargs='*',
                        help='regexp rules for filtering text')
    parser.add_argument('--html', help='write files as HTML',
                        action='store_true')
    parser.add_argument('-i', '--images', action='store_true',
                        help='save page images')
    parser.add_argument('-m', '--multiple', help='save to multiple files',
                        action='store_true')
    parser.add_argument('-max', '--max-crawls', type=int,
                        help='max number of pages to crawl')
    parser.add_argument('-n', '--nonstrict', action='store_true',
                        help='allow crawler to visit any domain')
    parser.add_argument('-ni', '--no-images', action='store_true',
                        help='do not save page images')
    parser.add_argument('-no', '--no-overwrite', action='store_true',
                        help='do not overwrite files if they exist')
    parser.add_argument('-o', '--out', type=str, nargs='*',
                        help='specify outfile names')
    parser.add_argument('-ow', '--overwrite', action='store_true',
                        help='overwrite a file if it exists')
    parser.add_argument('-p', '--pdf', help='write files as pdf',
                        action='store_true')
    parser.add_argument('-pt', '--print', help='print text output',
                        action='store_true')
    parser.add_argument('-q', '--quiet', help='suppress program output',
                        action='store_true')
    parser.add_argument('-s', '--single', help='save to a single file',
                        action='store_true')
    parser.add_argument('-t', '--text', help='write files as text',
                        action='store_true')
    parser.add_argument('-v', '--version', help='display current version',
                        action='store_true')
    parser.add_argument('-x', '--xpath', type=str, nargs='?',
                        help='filter HTML using XPath')
    return parser

Response: Parse command - line arguments.


Instruction:
How would you code a function in Python 3 to
write scraped or local file(s ) in desired format.

Response:
def write_files(args, infilenames, outfilename):
    """Write scraped or local file(s) in desired format.

    Keyword arguments:
    args -- program arguments (dict)
    infilenames -- names of user-inputted and/or downloaded files (list)
    outfilename -- name of output file (str)

    Remove PART(#).html files after conversion unless otherwise specified.
    """
    write_actions = {'print': utils.print_text,
                     'pdf': utils.write_pdf_files,
                     'csv': utils.write_csv_files,
                     'text': utils.write_text_files}
    try:
        for action in iterkeys(write_actions):
            if args[action]:
                write_actions[action](args, infilenames, outfilename)
    finally:
        if args['urls'] and not args['html']:
            utils.remove_part_files()


Instruction:
Write a Python 3 script to
write to a single output file and or a subdirectory.

Response:
def write_single_file(args, base_dir, crawler):
    """Write to a single output file and/or subdirectory."""
    if args['urls'] and args['html']:
        # Create a directory to save PART.html files in
        domain = utils.get_domain(args['urls'][0])
        if not args['quiet']:
            print('Storing html files in {0}/'.format(domain))
        utils.mkdir_and_cd(domain)

    infilenames = []
    for query in args['query']:
        if query in args['files']:
            infilenames.append(query)
        elif query.strip('/') in args['urls']:
            if args['crawl'] or args['crawl_all']:
                # Crawl and save HTML files/image files to disk
                infilenames += crawler.crawl_links(query)
            else:
                raw_resp = utils.get_raw_resp(query)
                if raw_resp is None:
                    return False

                prev_part_num = utils.get_num_part_files()
                utils.write_part_file(args, query, raw_resp)
                curr_part_num = prev_part_num + 1
                infilenames += utils.get_part_filenames(curr_part_num, prev_part_num)

    # Convert output or leave as PART.html files
    if args['html']:
        # HTML files have been written already, so return to base directory
        os.chdir(base_dir)
    else:
        # Write files to text or pdf
        if infilenames:
            if args['out']:
                outfilename = args['out'][0]
            else:
                outfilename = utils.get_single_outfilename(args)
            if outfilename:
                write_files(args, infilenames, outfilename)
        else:
            utils.remove_part_files()
    return True


Instruction:
Write a Python 3 function for
writing to multiple output files and or subdirectories.

Response:
def write_multiple_files(args, base_dir, crawler):
    """Write to multiple output files and/or subdirectories."""
    for i, query in enumerate(args['query']):
        if query in args['files']:
            # Write files
            if args['out'] and i < len(args['out']):
                outfilename = args['out'][i]
            else:
                outfilename = '.'.join(query.split('.')[:-1])
            write_files(args, [query], outfilename)
        elif query in args['urls']:
            # Scrape/crawl urls
            domain = utils.get_domain(query)
            if args['html']:
                # Create a directory to save PART.html files in
                if not args['quiet']:
                    print('Storing html files in {0}/'.format(domain))
                utils.mkdir_and_cd(domain)

            if args['crawl'] or args['crawl_all']:
                # Crawl and save HTML files/image files to disk
                infilenames = crawler.crawl_links(query)
            else:
                raw_resp = utils.get_raw_resp(query)
                if raw_resp is None:
                    return False

                # Saves page as PART.html file
                prev_part_num = utils.get_num_part_files()
                utils.write_part_file(args, query, raw_resp)
                curr_part_num = prev_part_num + 1
                infilenames = utils.get_part_filenames(curr_part_num, prev_part_num)

            # Convert output or leave as PART.html files
            if args['html']:
                # HTML files have been written already, so return to base dir
                os.chdir(base_dir)
            else:
                # Write files to text or pdf
                if infilenames:
                    if args['out'] and i < len(args['out']):
                        outfilename = args['out'][i]
                    else:
                        outfilename = utils.get_outfilename(query, domain)
                    write_files(args, infilenames, outfilename)
                else:
                    sys.stderr.write('Failed to retrieve content from {0}.\n'
                                     .format(query))
    return True


Instruction:
Write a Python 3 function that can
split query input into local files and URLs.

Response:
def split_input(args):
    """Split query input into local files and URLs."""
    args['files'] = []
    args['urls'] = []
    for arg in args['query']:
        if os.path.isfile(arg):
            args['files'].append(arg)
        else:
            args['urls'].append(arg.strip('/'))


Instruction:
Make a summary of the following Python 3 code
def detect_output_type(args):
    if not args['single'] and not args['multiple']:
        # Save to multiple files if multiple files/URLs entered
        if len(args['query']) > 1 or len(args['out']) > 1:
            args['multiple'] = True
        else:
            args['single'] = True

Response: Detect whether to save to a single or multiple files.


Instruction:
Write a Python 3 script for
prompting user for filetype if none specified.

Response:
def prompt_filetype(args):
    """Prompt user for filetype if none specified."""
    valid_types = ('print', 'text', 'csv', 'pdf', 'html')
    if not any(args[x] for x in valid_types):
        try:
            filetype = input('Print or save output as ({0}): '
                             .format(', '.join(valid_types))).lower()
            while filetype not in valid_types:
                filetype = input('Invalid entry. Choose from ({0}): '
                                 .format(', '.join(valid_types))).lower()
        except (KeyboardInterrupt, EOFError):
            return
        args[filetype] = True


Instruction:
Can you generate a brief explanation for the following Python 3 code
def prompt_save_images(args):
    if args['images'] or args['no_images']:
        return

    if (args['pdf'] or args['html']) and (args['crawl'] or args['crawl_all']):
        save_msg = ('Choosing to save images will greatly slow the'
                    ' crawling process.\nSave images anyways? (y/n): ')
        try:
            save_images = utils.confirm_input(input(save_msg))
        except (KeyboardInterrupt, EOFError):
            return

        args['images'] = save_images
        args['no_images'] = not save_images

Response: Prompt user to save images when crawling.


Instruction:
How would you explain what the following Python 3 function does
def command_line_runner():
    parser = get_parser()
    args = vars(parser.parse_args())
    if args['version']:
        print(__version__)
        return
    if args['clear_cache']:
        utils.clear_cache()
        print('Cleared {0}.'.format(utils.CACHE_DIR))
        return
    if not args['query']:
        parser.print_help()
        return

    # Enable cache unless user sets environ variable SCRAPE_DISABLE_CACHE
    if not os.getenv('SCRAPE_DISABLE_CACHE'):
        utils.enable_cache()

    # Save images unless user sets environ variable SCRAPE_DISABLE_IMGS
    if os.getenv('SCRAPE_DISABLE_IMGS'):
        args['no_images'] = True

    # Prompt user for filetype if none specified
    prompt_filetype(args)

    # Prompt user to save images when crawling (for pdf and HTML formats)
    prompt_save_images(args)

    # Scrape webpage content
    scrape(args)

Response: Handle command - line interaction.


Instruction:
Write a Python 3 function for
loading a trained classifier from the raw Weka model format.

Response:
def load_raw(cls, model_fn, schema, *args, **kwargs):
        """
        Loads a trained classifier from the raw Weka model format.
        Must specify the model schema and classifier name, since
        these aren't currently deduced from the model format.
        """
        c = cls(*args, **kwargs)
        c.schema = schema.copy(schema_only=True)
        c._model_data = open(model_fn, 'rb').read()
        return c


Instruction:
Can you write a function in Python 3 where it
trains the classifier with new training data and test data.

Response:
def train(self, training_data, testing_data=None, verbose=False):
        """
        Updates the classifier with new data.
        """
        model_fn = None
        training_fn = None
        clean_training = False
        testing_fn = None
        clean_testing = False
        try:

            # Validate training data.
            if isinstance(training_data, basestring):
                assert os.path.isfile(training_data)
                training_fn = training_data
            else:
                assert isinstance(training_data, arff.ArffFile)
                fd, training_fn = tempfile.mkstemp(suffix='.arff')
                os.close(fd)
                with open(training_fn, 'w') as fout:
                    fout.write(training_data.write())
                clean_training = True
            assert training_fn

            # Validate testing data.
            if testing_data:
                if isinstance(testing_data, basestring):
                    assert os.path.isfile(testing_data)
                    testing_fn = testing_data
                else:
                    assert isinstance(testing_data, arff.ArffFile)
                    fd, testing_fn = tempfile.mkstemp(suffix='.arff')
                    os.close(fd)
                    with open(testing_fn, 'w') as fout:
                        fout.write(testing_data.write())
                    clean_testing = True
            else:
                testing_fn = training_fn
            assert testing_fn

            # Validate model file.
            fd, model_fn = tempfile.mkstemp()
            os.close(fd)
            if self._model_data:
                fout = open(model_fn, 'wb')
                fout.write(self._model_data)
                fout.close()

            # Call Weka Jar.
            args = dict(
                CP=CP,
                classifier_name=self.name,
                model_fn=model_fn,
                training_fn=training_fn,
                testing_fn=testing_fn,
                ckargs=self._get_ckargs_str(),
            )
            if self._model_data:
                # Load existing model.
                cmd = (
                    "java -cp %(CP)s %(classifier_name)s -l \"%(model_fn)s\" "
                    "-t \"%(training_fn)s\" -T \"%(testing_fn)s\" -d \"%(model_fn)s\"") % args
            else:
                # Create new model file.
                cmd = (
                    "java -cp %(CP)s %(classifier_name)s -t \"%(training_fn)s\" "
                    "-T \"%(testing_fn)s\" -d \"%(model_fn)s\" %(ckargs)s") % args
            if verbose:
                print(cmd)
            p = Popen(
                cmd,
                shell=True,
                stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=sys.platform != "win32")
            stdin, stdout, stderr = (p.stdin, p.stdout, p.stderr)
            stdout_str = stdout.read()
            stderr_str = stderr.read()

            self.last_training_stdout = stdout_str
            self.last_training_stderr = stderr_str

            if verbose:
                print('stdout:')
                print(stdout_str)
                print('stderr:')
                print(stderr_str)
            # exclude "Warning" lines not to raise an error for a simple warning
            stderr_str = '\n'.join(l for l in stderr_str.decode('utf8').split('\n') if not "Warning" in l)
            if stderr_str:
                raise TrainingError(stderr_str)

            # Save schema.
            if not self.schema:
                self.schema = arff.ArffFile.load(training_fn, schema_only=True).copy(schema_only=True)

            # Save model.
            with open(model_fn, 'rb') as fin:
                self._model_data = fin.read()
            assert self._model_data
        finally:
            # Cleanup files.
            if model_fn:
                os.remove(model_fn)
            if training_fn and clean_training:
                os.remove(training_fn)
            if testing_fn and clean_testing:
                os.remove(testing_fn)


Instruction:
How would you explain what the following Python 3 function does
def predict(self, query_data, verbose=False, distribution=False, cleanup=True):
        model_fn = None
        query_fn = None
        clean_query = False
        stdout = None
        try:

            # Validate query data.
            if isinstance(query_data, basestring):
                assert os.path.isfile(query_data)
                query_fn = query_data
            else:
                #assert isinstance(query_data, arff.ArffFile) #TODO: doesn't work in Python 3.*?
                assert type(query_data).__name__ == 'ArffFile', 'Must be of type ArffFile, not "%s"' % type(query_data).__name__
                fd, query_fn = tempfile.mkstemp(suffix='.arff')
                if verbose:
                    print('writing', query_fn)
                os.close(fd)
                open(query_fn, 'w').write(query_data.write())
                clean_query = True
            assert query_fn

            # Validate model file.
            fd, model_fn = tempfile.mkstemp()
            os.close(fd)
            assert self._model_data, "You must train this classifier before predicting."
            fout = open(model_fn, 'wb')
            fout.write(self._model_data)
            fout.close()

#            print(open(model_fn).read()
#            print(open(query_fn).read()
            # Call Weka Jar.
            args = dict(
                CP=CP,
                classifier_name=self.name,
                model_fn=model_fn,
                query_fn=query_fn,
                #ckargs = self._get_ckargs_str(),
                distribution=('-distribution' if distribution else ''),
            )
            cmd = ("java -cp %(CP)s %(classifier_name)s -p 0 %(distribution)s -l \"%(model_fn)s\" -T \"%(query_fn)s\"") % args
            if verbose:
                print(cmd)
            p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
            stdin, stdout, stderr = (p.stdin, p.stdout, p.stderr)
            stdout_str = stdout.read()
            stderr_str = stderr.read()
            if verbose:
                print('stdout:')
                print(stdout_str)
                print('stderr:')
                print(stderr_str)
            if stderr_str:
                raise PredictionError(stderr_str)

            if stdout_str:
                # inst#     actual  predicted error prediction
                #header = 'inst,actual,predicted,error'.split(',')
                query = arff.ArffFile.load(query_fn)
                query_variables = [
                    query.attributes[i]
                    for i, v in enumerate(query.data[0])
                    if v == arff.MISSING]
                if not query_variables:
                    query_variables = [query.attributes[-1]]
#                assert query_variables, \
#                    "There must be at least one query variable in the query."
                if verbose:
                    print('query_variables:', query_variables)
                header = 'predicted'.split(',')
                # sample line:     1        1:?       4:36   +   1

                # Expected output without distribution:
                #=== Predictions on test data ===
                #
                # inst#     actual  predicted error prediction
                #     1        1:? 11:Acer_tr   +   1

                #=== Predictions on test data ===
                #
                # inst#     actual  predicted      error
                #     1          ?      7              ?

                #=== Predictions on test data ===
                #
                # inst#     actual  predicted error prediction
                #     1        1:?        1:0       0.99
                #     2        1:?        1:0       0.99
                #     3        1:?        1:0       0.99
                #     4        1:?        1:0       0.99
                #     5        1:?        1:0       0.99

                # Expected output with distribution:
                #=== Predictions on test data ===
                #
                # inst#     actual  predicted error distribution
                #     1        1:? 11:Acer_tr   +   0,0,0,0,0,0,0,0,0,0,*1,0,0,0,0,0...

                # Expected output with simple format:
                # inst#     actual  predicted      error
                #     1          ?     -3.417          ?

                q = re.findall(
                    r'J48 pruned tree\s+\-+:\s+([0-9]+)\s+',
                    stdout_str.decode('utf-8'), re.MULTILINE|re.DOTALL)
                if q:
                    class_label = q[0]
                    prob = 1.0
                    yield PredictionResult(
                        actual=None,
                        predicted=class_label,
                        probability=prob,)
                elif re.findall(r'error\s+(?:distribution|prediction)', stdout_str.decode('utf-8')):
                    # Check for distribution output.
                    matches = re.findall(
                        r"^\s*[0-9\.]+\s+[a-zA-Z0-9\.\?\:]+\s+(?P<cls_value>[a-zA-Z0-9_\.\?\:]+)\s+\+?\s+(?P<prob>[a-zA-Z0-9\.\?\,\*]+)",
                        stdout_str.decode('utf-8'),
                        re.MULTILINE)
                    assert matches, ("No results found matching distribution pattern in stdout: %s") % stdout_str
                    for match in matches:
                        prediction, prob = match
                        class_index, class_label = prediction.split(':')
                        class_index = int(class_index)
                        if distribution:
                            # Convert list of probabilities into a hash linking the prob
                            # to the associated class value.
                            prob = dict(zip(
                                query.attribute_data[query.attributes[-1]],
                                map(float, prob.replace('*', '').split(','))))
                        else:
                            prob = float(prob)
                        class_label = query.attribute_data[query.attributes[-1]][class_index-1]
                        yield PredictionResult(
                            actual=None,
                            predicted=class_label,
                            probability=prob,)
                else:
                    # Otherwise, assume a simple output.
                    matches = re.findall(
                        # inst#     actual  predicted
                        r"^\s*([0-9\.]+)\s+([a-zA-Z0-9\-\.\?\:]+)\s+([a-zA-Z0-9\-_\.\?\:]+)\s+",
                        stdout_str.decode('utf-8'),
                        re.MULTILINE)
                    assert matches, "No results found matching simple pattern in stdout: %s" % stdout_str
                    #print('matches:',len(matches)
                    for match in matches:
                        inst, actual, predicted = match
                        class_name = query.attributes[-1]
                        actual_value = query.get_attribute_value(class_name, actual)
                        predicted_value = query.get_attribute_value(class_name, predicted)
                        yield PredictionResult(
                            actual=actual_value,
                            predicted=predicted_value,
                            probability=None,)
        finally:
            # Cleanup files.
            if cleanup:
                if model_fn:
                    self._model_data = open(model_fn, 'rb').read()
                    os.remove(model_fn)
                if query_fn and clean_query:
                    os.remove(query_fn)

Response: Predict the value of a class by running a Weka model and predicting it.


Instruction:
Write a Python 3 function for
returning a ratio of classifiers that were able to be trained successfully.

Response:
def get_training_coverage(self):
        """
        Returns a ratio of classifiers that were able to be trained successfully.
        """
        total = len(self.training_results)
        i = sum(1 for data in self.training_results.values() if not isinstance(data, basestring))
        return i/float(total)


Instruction:
Given the following Python 3 function, write the documentation
def get_new_links(self, url, resp):
        links_on_page = resp.xpath('//a/@href')
        links = [utils.clean_url(u, url) for u in links_on_page]

        # Remove non-links through filtering by protocol
        links = [x for x in links if utils.check_protocol(x)]

        # Restrict new URLs by the domain of the input URL
        if not self.args['nonstrict']:
            domain = utils.get_domain(url)
            links = [x for x in links if utils.get_domain(x) == domain]

        # Filter URLs by regex keywords, if any
        if self.args['crawl']:
            links = utils.re_filter(links, self.args['crawl'])
        return links

Response: Get new links from a URL and filter them.


Instruction:
Given the following Python 3 function, write the documentation
def page_crawled(self, page_resp):
        page_text = utils.parse_text(page_resp)
        page_hash = utils.hash_text(''.join(page_text))
        if page_hash not in self.page_cache:
            utils.cache_page(self.page_cache, page_hash, self.args['cache_size'])
            return False
        return True

Response: Check if a page has been crawled by hashing its text content.


Instruction:
How would you implement a function in Python 3 that
crawls the page responses and creates PART. html files.

Response:
def crawl_links(self, seed_url=None):
        """Find new links given a seed URL and follow them breadth-first.

        Save page responses as PART.html files.
        Return the PART.html filenames created during crawling.
        """
        if seed_url is not None:
            self.seed_url = seed_url

        if self.seed_url is None:
            sys.stderr.write('Crawling requires a seed URL.\n')
            return []

        prev_part_num = utils.get_num_part_files()
        crawled_links = set()
        uncrawled_links = OrderedSet()

        uncrawled_links.add(self.seed_url)
        try:
            while uncrawled_links:
                # Check limit on number of links and pages to crawl
                if self.limit_reached(len(crawled_links)):
                    break
                url = uncrawled_links.pop(last=False)

                # Remove protocol, fragments, etc. to get unique URLs
                unique_url = utils.remove_protocol(utils.clean_url(url))
                if unique_url not in crawled_links:
                    raw_resp = utils.get_raw_resp(url)
                    if raw_resp is None:
                        if not self.args['quiet']:
                            sys.stderr.write('Failed to parse {0}.\n'.format(url))
                        continue

                    resp = lh.fromstring(raw_resp)
                    if self.page_crawled(resp):
                        continue

                    crawled_links.add(unique_url)
                    new_links = self.get_new_links(url, resp)
                    uncrawled_links.update(new_links)
                    if not self.args['quiet']:
                        print('Crawled {0} (#{1}).'.format(url, len(crawled_links)))

                    # Write page response to PART.html file
                    utils.write_part_file(self.args, url, raw_resp, resp, len(crawled_links))
        except (KeyboardInterrupt, EOFError):
            pass

        curr_part_num = utils.get_num_part_files()
        return utils.get_part_filenames(curr_part_num, prev_part_num)


Instruction:
Write a Python 3 script to
get available proxies to use with requests library.

Response:
def get_proxies():
    """Get available proxies to use with requests library."""
    proxies = getproxies()
    filtered_proxies = {}
    for key, value in proxies.items():
        if key.startswith('http://'):
            if not value.startswith('http://'):
                filtered_proxies[key] = 'http://{0}'.format(value)
            else:
                filtered_proxies[key] = value
    return filtered_proxies


Instruction:
Write a Python 3 script to
get webpage response as an lxml. html. HtmlElement object.

Response:
def get_resp(url):
    """Get webpage response as an lxml.html.HtmlElement object."""
    try:
        headers = {'User-Agent': random.choice(USER_AGENTS)}
        try:
            request = requests.get(url, headers=headers, proxies=get_proxies())
        except MissingSchema:
            url = add_protocol(url)
            request = requests.get(url, headers=headers, proxies=get_proxies())
        return lh.fromstring(request.text.encode('utf-8') if PY2 else request.text)
    except Exception:
        sys.stderr.write('Failed to retrieve {0}.\n'.format(url))
        raise


Instruction:
Create a Python 3 function to
enable requests library cache.

Response:
def enable_cache():
    """Enable requests library cache."""
    try:
        import requests_cache
    except ImportError as err:
        sys.stderr.write('Failed to enable cache: {0}\n'.format(str(err)))
        return
    if not os.path.exists(CACHE_DIR):
        os.makedirs(CACHE_DIR)
    requests_cache.install_cache(CACHE_FILE)


Instruction:
Create a Python 3 function to
return MD5 hash of a string.

Response:
def hash_text(text):
    """Return MD5 hash of a string."""
    md5 = hashlib.md5()
    md5.update(text)
    return md5.hexdigest()


Instruction:
Can you create a Python 3 function that
adds a page to the page cache.

Response:
def cache_page(page_cache, page_hash, cache_size):
    """Add a page to the page cache."""
    page_cache.append(page_hash)
    if len(page_cache) > cache_size:
        page_cache.pop(0)


Instruction:
Can you tell what is the following Python 3 function doing
def re_filter(text, regexps):
    if not regexps:
        return text

    matched_text = []
    compiled_regexps = [re.compile(x) for x in regexps]
    for line in text:
        if line in matched_text:
            continue

        for regexp in compiled_regexps:
            found = regexp.search(line)
            if found and found.group():
                matched_text.append(line)

    return matched_text or text

Response: Filter text using regular expressions.


Instruction:
Here you have a function in Python 3, explain what it does
def remove_whitespace(text):
    clean_text = []
    curr_line = ''
    # Remove any newlines that follow two lines of whitespace consecutively
    # Also remove whitespace at start and end of text
    while text:
        if not curr_line:
            # Find the first line that is not whitespace and add it
            curr_line = text.pop(0)
            while not curr_line.strip() and text:
                curr_line = text.pop(0)
            if curr_line.strip():
                clean_text.append(curr_line)
        else:
            # Filter the rest of the lines
            curr_line = text.pop(0)
            if not text:
                # Add the final line if it is not whitespace
                if curr_line.strip():
                    clean_text.append(curr_line)
                continue

            if curr_line.strip():
                clean_text.append(curr_line)
            else:
                # If the current line is whitespace then make sure there is
                # no more than one consecutive line of whitespace following
                if not text[0].strip():
                    if len(text) > 1 and text[1].strip():
                        clean_text.append(curr_line)
                else:
                    clean_text.append(curr_line)

    # Now filter each individual line for extraneous whitespace
    cleaner_text = []
    for line in clean_text:
        clean_line = ' '.join(line.split())
        if not clean_line.strip():
            clean_line += '\n'
        cleaner_text.append(clean_line)
    return cleaner_text

Response: Remove unnecessary whitespace while keeping logical structure.


Instruction:
Can you generate the documentation for the following Python 3 function
def parse_text(infile, xpath=None, filter_words=None, attributes=None):
    infiles = []
    text = []
    if xpath is not None:
        infile = parse_html(infile, xpath)
        if isinstance(infile, list):
            if isinstance(infile[0], lh.HtmlElement):
                infiles = list(infile)
            else:
                text = [line + '\n' for line in infile]
        elif isinstance(infile, lh.HtmlElement):
            infiles = [infile]
        else:
            text = [infile]
    else:
        infiles = [infile]

    if attributes is not None:
        attributes = [clean_attr(x) for x in attributes]
        attributes = [x for x in attributes if x]
    else:
        attributes = ['text()']

    if not text:
        text_xpath = '//*[not(self::script) and not(self::style)]'
        for attr in attributes:
            for infile in infiles:
                if isinstance(infile, lh.HtmlElement):
                    new_text = infile.xpath('{0}/{1}'.format(text_xpath, attr))
                else:
                    # re.split preserves delimiters place in the list
                    new_text = [x for x in re.split('(\n)', infile) if x]
                text += new_text

    if filter_words is not None:
        text = re_filter(text, filter_words)
    return [''.join(x for x in line if x in string.printable)
            for line in remove_whitespace(text) if line]

Response: Parse text using XPath regex keywords and tag attributes.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_parsed_text(args, infilename):
    parsed_text = []
    if infilename.endswith('.html'):
        # Convert HTML to lxml object for content parsing
        html = lh.fromstring(read_files(infilename))
        text = None
    else:
        html = None
        text = read_files(infilename)

    if html is not None:
        parsed_text = parse_text(html, args['xpath'], args['filter'],
                                 args['attributes'])
    elif text is not None:
        parsed_text = parse_text(text, args['xpath'], args['filter'])
    else:
        if not args['quiet']:
            sys.stderr.write('Failed to parse text from {0}.\n'
                             .format(infilename))
    return parsed_text

Response: Parse and return text content of infiles.


Instruction:
Can you generate the documentation for the following Python 3 function
def parse_html(infile, xpath):
    if not isinstance(infile, lh.HtmlElement):
        infile = lh.fromstring(infile)
    infile = infile.xpath(xpath)
    if not infile:
        raise ValueError('XPath {0} returned no results.'.format(xpath))
    return infile

Response: Filter HTML using XPath.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def clean_url(url, base_url=None):
    parsed_url = urlparse(url)

    fragment = '{url.fragment}'.format(url=parsed_url)
    if fragment:
        url = url.split(fragment)[0]

    # Identify internal URLs and fix their format
    netloc = '{url.netloc}'.format(url=parsed_url)
    if base_url is not None and not netloc:
        parsed_base = urlparse(base_url)
        split_base = '{url.scheme}://{url.netloc}{url.path}/'.format(url=parsed_base)
        url = urljoin(split_base, url)
        netloc = '{url.netloc}'.format(url=urlparse(url))

    if 'www.' in netloc:
        url = url.replace(netloc, netloc.replace('www.', ''))
    return url.rstrip(string.punctuation)

Response: Remove www and fragments from internal URLs and remove www.


Instruction:
Implement a function in Python 3 to
add com suffix to URL if none found.

Response:
def add_url_suffix(url):
    """Add .com suffix to URL if none found."""
    url = url.rstrip('/')
    if not has_suffix(url):
        return '{0}.com'.format(url)
    return url


Instruction:
Create a Python 3 function for
constructing the output filename from domain and end of path.

Response:
def get_outfilename(url, domain=None):
    """Construct the output filename from domain and end of path."""
    if domain is None:
        domain = get_domain(url)

    path = '{url.path}'.format(url=urlparse(url))
    if '.' in path:
        tail_url = path.split('.')[-2]
    else:
        tail_url = path

    if tail_url:
        if '/' in tail_url:
            tail_pieces = [x for x in tail_url.split('/') if x]
            tail_url = tail_pieces[-1]

        # Keep length of return string below or equal to max_len
        max_len = 24
        if domain:
            max_len -= (len(domain) + 1)
        if len(tail_url) > max_len:
            if '-' in tail_url:
                tail_pieces = [x for x in tail_url.split('-') if x]
                tail_url = tail_pieces.pop(0)
                if len(tail_url) > max_len:
                    tail_url = tail_url[:max_len]
                else:
                    # Add as many tail pieces that can fit
                    tail_len = 0
                    for piece in tail_pieces:
                        tail_len += len(piece)
                        if tail_len <= max_len:
                            tail_url += '-' + piece
                        else:
                            break
            else:
                tail_url = tail_url[:max_len]

        if domain:
            return '{0}-{1}'.format(domain, tail_url).lower()
        return tail_url
    return domain.lower()


Instruction:
Can you write a function in Python 3 where it
uses first possible entry in query as filename.

Response:
def get_single_outfilename(args):
    """Use first possible entry in query as filename."""
    for arg in args['query']:
        if arg in args['files']:
            return ('.'.join(arg.split('.')[:-1])).lower()
        for url in args['urls']:
            if arg.strip('/') in url:
                domain = get_domain(url)
                return get_outfilename(url, domain)
    sys.stderr.write('Failed to construct a single out filename.\n')
    return ''


Instruction:
Create a Python 3 function for
modifying filename to have a unique numerical identifier.

Response:
def modify_filename_id(filename):
    """Modify filename to have a unique numerical identifier."""
    split_filename = os.path.splitext(filename)
    id_num_re = re.compile('(\(\d\))')
    id_num = re.findall(id_num_re, split_filename[-2])
    if id_num:
        new_id_num = int(id_num[-1].lstrip('(').rstrip(')')) + 1

        # Reconstruct filename with incremented id and its extension
        filename = ''.join((re.sub(id_num_re, '({0})'.format(new_id_num),
                                   split_filename[-2]), split_filename[-1]))
    else:
        split_filename = os.path.splitext(filename)

        # Reconstruct filename with new id and its extension
        filename = ''.join(('{0} (2)'.format(split_filename[-2]),
                            split_filename[-1]))
    return filename


Instruction:
Explain what the following Python 3 code does
def overwrite_file_check(args, filename):
    if not args['overwrite'] and os.path.exists(filename):
        # Confirm overwriting of the file, or modify filename
        if args['no_overwrite']:
            overwrite = False
        else:
            try:
                overwrite = confirm_input(input('Overwrite {0}? (yes/no): '
                                                .format(filename)))
            except (KeyboardInterrupt, EOFError):
                sys.exit()
        if not overwrite:
            new_filename = modify_filename_id(filename)
            while os.path.exists(new_filename):
                new_filename = modify_filename_id(new_filename)
            return new_filename
    return filename

Response: If filename exists overwrite or modify it to be unique.


Instruction:
Create a Python 3 function to
print text content of infiles to stdout.

Response:
def print_text(args, infilenames, outfilename=None):
    """Print text content of infiles to stdout.

    Keyword arguments:
    args -- program arguments (dict)
    infilenames -- names of user-inputted and/or downloaded files (list)
    outfilename -- only used for interface purposes (None)
    """
    for infilename in infilenames:
        parsed_text = get_parsed_text(args, infilename)
        if parsed_text:
            for line in parsed_text:
                print(line)
            print('')


Instruction:
Implement a Python 3 function for
writing pdf files to disk using pdfkit.

Response:
def write_pdf_files(args, infilenames, outfilename):
    """Write pdf file(s) to disk using pdfkit.

    Keyword arguments:
    args -- program arguments (dict)
    infilenames -- names of user-inputted and/or downloaded files (list)
    outfilename -- name of output pdf file (str)
    """
    if not outfilename.endswith('.pdf'):
        outfilename = outfilename + '.pdf'
    outfilename = overwrite_file_check(args, outfilename)

    options = {}
    try:
        if args['multiple']:
            # Multiple files are written one at a time, so infilenames will
            # never contain more than one file here
            infilename = infilenames[0]
            if not args['quiet']:
                print('Attempting to write to {0}.'.format(outfilename))
            else:
                options['quiet'] = None

            if args['xpath']:
                # Process HTML with XPath before writing
                html = parse_html(read_files(infilename), args['xpath'])
                if isinstance(html, list):
                    if isinstance(html[0], str):
                        pk.from_string('\n'.join(html), outfilename,
                                       options=options)
                    else:
                        pk.from_string('\n'.join(lh.tostring(x) for x in html),
                                       outfilename, options=options)
                elif isinstance(html, str):
                    pk.from_string(html, outfilename, options=options)
                else:
                    pk.from_string(lh.tostring(html), outfilename,
                                   options=options)
            else:
                pk.from_file(infilename, outfilename, options=options)
        elif args['single']:
            if not args['quiet']:
                print('Attempting to write {0} page(s) to {1}.'
                      .format(len(infilenames), outfilename))
            else:
                options['quiet'] = None

            if args['xpath']:
                # Process HTML with XPath before writing
                html = parse_html(read_files(infilenames), args['xpath'])
                if isinstance(html, list):
                    if isinstance(html[0], str):
                        pk.from_string('\n'.join(html), outfilename,
                                       options=options)
                    else:
                        pk.from_string('\n'.join(lh.tostring(x) for x in html),
                                       outfilename, options=options)
                elif isinstance(html, str):
                    pk.from_string(html, outfilename, options=options)
                else:
                    pk.from_string(lh.tostring(html), outfilename,
                                   options=options)
            else:
                pk.from_file(infilenames, outfilename, options=options)
        return True
    except (OSError, IOError) as err:
        sys.stderr.write('An error occurred while writing {0}:\n{1}'
                         .format(outfilename, str(err)))
        return False


Instruction:
Implement a Python 3 function for
writing csv files to disk.

Response:
def write_csv_files(args, infilenames, outfilename):
    """Write csv file(s) to disk.

    Keyword arguments:
    args -- program arguments (dict)
    infilenames -- names of user-inputted and/or downloaded files (list)
    outfilename -- name of output text file (str)
    """
    def csv_convert(line):
        """Strip punctuation and insert commas"""
        clean_line = []
        for word in line.split(' '):
            clean_line.append(word.strip(string.punctuation))
        return ', '.join(clean_line)

    if not outfilename.endswith('.csv'):
        outfilename = outfilename + '.csv'
    outfilename = overwrite_file_check(args, outfilename)

    all_text = []  # Text must be aggregated if writing to a single output file
    for i, infilename in enumerate(infilenames):
        parsed_text = get_parsed_text(args, infilename)
        if parsed_text:
            if args['multiple']:
                if not args['quiet']:
                    print('Attempting to write to {0}.'.format(outfilename))

                csv_text = [csv_convert(x) for x in parsed_text]
                print(csv_text)
                write_file(csv_text, outfilename)
            elif args['single']:
                all_text += parsed_text
                # Newline added between multiple files being aggregated
                if len(infilenames) > 1 and i < len(infilenames) - 1:
                    all_text.append('\n')

    # Write all text to a single output file
    if args['single'] and all_text:
        if not args['quiet']:
            print('Attempting to write {0} page(s) to {1}.'
                  .format(len(infilenames), outfilename))

        csv_text = [csv_convert(x) for x in all_text]
        print(csv_text)
        write_file(csv_text, outfilename)


Instruction:
Can you tell what is the following Python 3 function doing
def write_text_files(args, infilenames, outfilename):
    if not outfilename.endswith('.txt'):
        outfilename = outfilename + '.txt'
    outfilename = overwrite_file_check(args, outfilename)

    all_text = []  # Text must be aggregated if writing to a single output file
    for i, infilename in enumerate(infilenames):
        parsed_text = get_parsed_text(args, infilename)
        if parsed_text:
            if args['multiple']:
                if not args['quiet']:
                    print('Attempting to write to {0}.'.format(outfilename))
                write_file(parsed_text, outfilename)
            elif args['single']:
                all_text += parsed_text
                # Newline added between multiple files being aggregated
                if len(infilenames) > 1 and i < len(infilenames) - 1:
                    all_text.append('\n')

    # Write all text to a single output file
    if args['single'] and all_text:
        if not args['quiet']:
            print('Attempting to write {0} page(s) to {1}.'
                  .format(len(infilenames), outfilename))
        write_file(all_text, outfilename)

Response: Write text files to disk.


Instruction:
Write a Python 3 script for
writing a single file to disk.

Response:
def write_file(data, outfilename):
    """Write a single file to disk."""
    if not data:
        return False
    try:
        with open(outfilename, 'w') as outfile:
            for line in data:
                if line:
                    outfile.write(line)
        return True
    except (OSError, IOError) as err:
        sys.stderr.write('An error occurred while writing {0}:\n{1}'
                         .format(outfilename, str(err)))
        return False


Instruction:
Can you write a function in Python 3 where it
gets the number of PART. html files currently saved to disk.

Response:
def get_num_part_files():
    """Get the number of PART.html files currently saved to disk."""
    num_parts = 0
    for filename in os.listdir(os.getcwd()):
        if filename.startswith('PART') and filename.endswith('.html'):
            num_parts += 1
    return num_parts


Instruction:
Can you generate the documentation for the following Python 3 function
def write_part_images(url, raw_html, html, filename):
    save_dirname = '{0}_files'.format(os.path.splitext(filename)[0])
    if not os.path.exists(save_dirname):
        os.makedirs(save_dirname)
    images = html.xpath('//img/@src')
    internal_image_urls = [x for x in images if x.startswith('/')]

    headers = {'User-Agent': random.choice(USER_AGENTS)}
    for img_url in images:
        img_name = img_url.split('/')[-1]
        if "?" in img_name:
            img_name = img_name.split('?')[0]
        if not os.path.splitext(img_name)[1]:
            img_name = '{0}.jpeg'.format(img_name)

        try:
            full_img_name = os.path.join(save_dirname, img_name)
            with open(full_img_name, 'wb') as img:
                if img_url in internal_image_urls:
                    # Internal images need base url added
                    full_img_url = '{0}{1}'.format(url.rstrip('/'), img_url)
                else:
                    # External image
                    full_img_url = img_url
                img_content = requests.get(full_img_url, headers=headers,
                                           proxies=get_proxies()).content
                img.write(img_content)
                raw_html = raw_html.replace(escape(img_url), full_img_name)
        except (OSError, IOError):
            pass
        time.sleep(random.uniform(0, 0.5))  # Slight delay between downloads
    return raw_html

Response: Write image files associated with a PART. html file.


Instruction:
Can you generate the documentation for the following Python 3 function
def write_part_file(args, url, raw_html, html=None, part_num=None):
    if part_num is None:
        part_num = get_num_part_files() + 1
    filename = 'PART{0}.html'.format(part_num)

    # Decode bytes to string in Python 3 versions
    if not PY2 and isinstance(raw_html, bytes):
        raw_html = raw_html.encode('ascii', 'ignore')

    # Convert html to an lh.HtmlElement object for parsing/saving images
    if html is None:
        html = lh.fromstring(raw_html)

    # Parse HTML if XPath entered
    if args['xpath']:
        raw_html = parse_html(html, args['xpath'])
        if isinstance(raw_html, list):
            if not isinstance(raw_html[0], lh.HtmlElement):
                raise ValueError('XPath should return an HtmlElement object.')
        else:
            if not isinstance(raw_html, lh.HtmlElement):
                raise ValueError('XPath should return an HtmlElement object.')

    # Write HTML and possibly images to disk
    if raw_html:
        if not args['no_images'] and (args['pdf'] or args['html']):
            raw_html = write_part_images(url, raw_html, html, filename)
        with open(filename, 'w') as part:
            if not isinstance(raw_html, list):
                raw_html = [raw_html]
                if isinstance(raw_html[0], lh.HtmlElement):
                    for elem in raw_html:
                        part.write(lh.tostring(elem))
                else:
                    for line in raw_html:
                        part.write(line)

Response: Write a PART. html file to disk images in PART_files directory.


Instruction:
How would you explain what the following Python 3 function does
def get_part_filenames(num_parts=None, start_num=0):
    if num_parts is None:
        num_parts = get_num_part_files()
    return ['PART{0}.html'.format(i) for i in range(start_num+1, num_parts+1)]

Response: Get numbered PART. html filenames.


Instruction:
How would you explain what the following Python 3 function does
def read_files(filenames):
    if isinstance(filenames, list):
        for filename in filenames:
            with open(filename, 'r') as infile:
                return infile.read()
    else:
        with open(filenames, 'r') as infile:
            return infile.read()

Response: Read a file into memory.


Instruction:
How would you implement a function in Python 3 that
removes PART ( # )_files directory containing images from disk.

Response:
def remove_part_images(filename):
    """Remove PART(#)_files directory containing images from disk."""
    dirname = '{0}_files'.format(os.path.splitext(filename)[0])
    if os.path.exists(dirname):
        shutil.rmtree(dirname)


Instruction:
Write a Python 3 function for
removing PART. html files and image directories from disk.

Response:
def remove_part_files(num_parts=None):
    """Remove PART(#).html files and image directories from disk."""
    filenames = get_part_filenames(num_parts)
    for filename in filenames:
        remove_part_images(filename)
        remove_file(filename)


Instruction:
Write a Python 3 script to
check user input for yes no or an exit signal.

Response:
def confirm_input(user_input):
    """Check user input for yes, no, or an exit signal."""
    if isinstance(user_input, list):
        user_input = ''.join(user_input)

    try:
        u_inp = user_input.lower().strip()
    except AttributeError:
        u_inp = user_input

    # Check for exit signal
    if u_inp in ('q', 'quit', 'exit'):
        sys.exit()
    if u_inp in ('y', 'yes'):
        return True
    return False


Instruction:
Can you create a Python 3 function that
changes directory and / or create it if necessary.

Response:
def mkdir_and_cd(dirname):
    """Change directory and/or create it if necessary."""
    if not os.path.exists(dirname):
        os.makedirs(dirname)
        os.chdir(dirname)
    else:
        os.chdir(dirname)


Instruction:
Given the following Python 3 function, write the documentation
def convert(data, in_format, out_format, name=None, pretty=False):
    # Decide on a json formatter depending on desired prettiness
    dumps = json.dumps if pretty else json.compress

    # Shortcut for avoiding pybel dependency
    if not has_ob and in_format == 'json' and out_format == 'json':
        return dumps(json.loads(data) if is_string(data) else data)
    elif not has_ob:
        raise ImportError("Chemical file format conversion requires pybel.")

    # These use the open babel library to interconvert, with additions for json
    if in_format == 'json':
        mol = json_to_pybel(json.loads(data) if is_string(data) else data)
    elif in_format == 'pybel':
        mol = data
    else:
        mol = pybel.readstring(in_format, data)

    # Infer structure in cases where the input format has no specification
    if not mol.OBMol.HasNonZeroCoords():
        mol.make3D()

    # Make P1 if that's a thing, recalculating bonds in process
    if in_format == 'mmcif' and hasattr(mol, 'unitcell'):
        mol.unitcell.FillUnitCell(mol.OBMol)
        mol.OBMol.ConnectTheDots()
        mol.OBMol.PerceiveBondOrders()

    mol.OBMol.Center()

    if out_format == 'pybel':
        return mol
    elif out_format == 'object':
        return pybel_to_json(mol, name)
    elif out_format == 'json':
        return dumps(pybel_to_json(mol, name))
    else:
        return mol.write(out_format)

Response: Converts a string representing the chemical file in in_format to out_format.


Instruction:
Explain what the following Python 3 code does
def json_to_pybel(data, infer_bonds=False):
    obmol = ob.OBMol()
    obmol.BeginModify()
    for atom in data['atoms']:
        obatom = obmol.NewAtom()
        obatom.SetAtomicNum(table.GetAtomicNum(str(atom['element'])))
        obatom.SetVector(*atom['location'])
        if 'label' in atom:
            pd = ob.OBPairData()
            pd.SetAttribute('_atom_site_label')
            pd.SetValue(atom['label'])
            obatom.CloneData(pd)

    # If there is no bond data, try to infer them
    if 'bonds' not in data or not data['bonds']:
        if infer_bonds:
            obmol.ConnectTheDots()
            obmol.PerceiveBondOrders()
    # Otherwise, use the bonds in the data set
    else:
        for bond in data['bonds']:
            if 'atoms' not in bond:
                continue
            obmol.AddBond(bond['atoms'][0] + 1, bond['atoms'][1] + 1,
                          bond['order'])

    # Check for unit cell data
    if 'unitcell' in data:
        uc = ob.OBUnitCell()
        uc.SetData(*(ob.vector3(*v) for v in data['unitcell']))
        uc.SetSpaceGroup('P1')
        obmol.CloneData(uc)
    obmol.EndModify()

    mol = pybel.Molecule(obmol)

    # Add partial charges
    if 'charge' in data['atoms'][0]:
        mol.OBMol.SetPartialChargesPerceived()
        for atom, pyatom in zip(data['atoms'], mol.atoms):
            pyatom.OBAtom.SetPartialCharge(atom['charge'])

    return mol

Response: Converts a python data structure to a Python object.


Instruction:
Implement a function in Python 3 to
convert a pybel. Molecule to json.

Response:
def pybel_to_json(molecule, name=None):
    """Converts a pybel molecule to json.

    Args:
        molecule: An instance of `pybel.Molecule`
        name: (Optional) If specified, will save a "name" property
    Returns:
       A Python dictionary containing atom and bond data
    """
    # Save atom element type and 3D location.
    atoms = [{'element': table.GetSymbol(atom.atomicnum),
              'location': list(atom.coords)}
             for atom in molecule.atoms]
    # Recover auxiliary data, if exists
    for json_atom, pybel_atom in zip(atoms, molecule.atoms):
        if pybel_atom.partialcharge != 0:
            json_atom['charge'] = pybel_atom.partialcharge
        if pybel_atom.OBAtom.HasData('_atom_site_label'):
            obatom = pybel_atom.OBAtom
            json_atom['label'] = obatom.GetData('_atom_site_label').GetValue()
        if pybel_atom.OBAtom.HasData('color'):
            obatom = pybel_atom.OBAtom
            json_atom['color'] = obatom.GetData('color').GetValue()

    # Save number of bonds and indices of endpoint atoms
    bonds = [{'atoms': [b.GetBeginAtom().GetIndex(),
                        b.GetEndAtom().GetIndex()],
              'order': b.GetBondOrder()}
             for b in ob.OBMolBondIter(molecule.OBMol)]
    output = {'atoms': atoms, 'bonds': bonds, 'units': {}}

    # If there's unit cell data, save it to the json output
    if hasattr(molecule, 'unitcell'):
        uc = molecule.unitcell
        output['unitcell'] = [[v.GetX(), v.GetY(), v.GetZ()]
                              for v in uc.GetCellVectors()]
        density = (sum(atom.atomicmass for atom in molecule.atoms) /
                   (uc.GetCellVolume() * 0.6022))
        output['density'] = density
        output['units']['density'] = 'kg / L'

    # Save the formula to json. Use Hill notation, just to have a standard.
    element_count = Counter(table.GetSymbol(a.atomicnum) for a in molecule)
    hill_count = []
    for element in ['C', 'H']:
        if element in element_count:
            hill_count += [(element, element_count[element])]
            del element_count[element]
    hill_count += sorted(element_count.items())

    # If it's a crystal, then reduce the Hill formula
    div = (reduce(gcd, (c[1] for c in hill_count))
           if hasattr(molecule, 'unitcell') else 1)

    output['formula'] = ''.join(n if c / div == 1 else '%s%d' % (n, c / div)
                                for n, c in hill_count)
    output['molecular_weight'] = molecule.molwt / div
    output['units']['molecular_weight'] = 'g / mol'

    # If the input has been given a name, add that
    if name:
        output['name'] = name

    return output


Instruction:
Create a Python 3 function to
fire when an unserializable object is hit.

Response:
def default(self, obj):
        """Fired when an unserializable object is hit."""
        if hasattr(obj, '__dict__'):
            return obj.__dict__.copy()
        elif HAS_NUMPY and isinstance(obj, np.ndarray):
            return obj.copy().tolist()
        else:
            raise TypeError(("Object of type {:s} with value of {:s} is not "
                             "JSON serializable").format(type(obj), repr(obj)))


Instruction:
How would you explain what the following Python 3 function does
def draw(data, format='auto', size=(400, 300), drawing_type='ball and stick',
         camera_type='perspective', shader='lambert', display_html=True,
         element_properties=None, show_save=False):
    """Draws an interactive 3D visualization of the inputted chemical.

    Args:
        data: A string or file representing a chemical.
        format: The format of the `data` variable (default is 'auto').
        size: Starting dimensions of visualization, in pixels.
        drawing_type: Specifies the molecular representation. Can be 'ball and
            stick', 'wireframe', or 'space filling'.
        camera_type: Can be 'perspective' or 'orthographic'.
        shader: Specifies shading algorithm to use. Can be 'toon', 'basic',
            'phong', or 'lambert'.
        display_html: If True (default), embed the html in a IPython display.
            If False, return the html as a string.
        element_properites: A dictionary providing color and radius information
            for custom elements or overriding the defaults in imolecule.js
        show_save: If True, displays a save icon for rendering molecule as an
            image.

    The `format` can be any value specified by Open Babel
    (http://openbabel.org/docs/2.3.1/FileFormats/Overview.html). The 'auto'
    option uses the extension for files (ie. my_file.mol -> mol) and defaults
    to SMILES (smi) for strings.
    """
    # Catch errors on string-based input before getting js involved
    draw_options = ['ball and stick', 'wireframe', 'space filling']
    camera_options = ['perspective', 'orthographic']
    shader_options = ['toon', 'basic', 'phong', 'lambert']
    if drawing_type not in draw_options:
        raise Exception("Invalid drawing type! Please use one of: " +
                        ", ".join(draw_options))
    if camera_type not in camera_options:
        raise Exception("Invalid camera type! Please use one of: " +
                        ", ".join(camera_options))
    if shader not in shader_options:
        raise Exception("Invalid shader! Please use one of: " +
                        ", ".join(shader_options))

    json_mol = generate(data, format)
    if element_properties is None:
        element_properties = dict()
    json_element_properties = to_json(element_properties)
    div_id = uuid.uuid4()
    html = """

           """ % (div_id, local_path[:-3], remote_path[:-3],
                           div_id, size[0], size[1], drawing_type,
                           camera_type, shader,
                           'true' if show_save else 'false',
                           json_element_properties,
                           json_mol, size[0], size[1])

    # Execute js and display the results in a div (see script for more)
    if display_html:
        try:
            __IPYTHON__
        except NameError:
            # We're running outside ipython, let's generate a static HTML and
            # show it in the browser
            import shutil
            import webbrowser
            from tempfile import mkdtemp
            from time import time
            try:  # Python 3
                from urllib.parse import urljoin
                from urllib.request import pathname2url
            except ImportError:  # Python 2
                from urlparse import urljoin
                from urllib import pathname2url
            from tornado import template

            t = template.Loader(file_path).load('viewer.template')
            html = t.generate(title="imolecule", json_mol=json_mol,
                              drawing_type=drawing_type, shader=shader,
                              camera_type=camera_type,
                              json_element_properties=json_element_properties)

            tempdir = mkdtemp(prefix='imolecule_{:.0f}_'.format(time()))

            html_filename = os.path.join(tempdir, 'index.html')
            with open(html_filename, 'wb') as f:
                f.write(html)

            libs = (('server', 'css', 'chosen.css'),
                    ('server', 'css', 'server.css'),
                    ('js', 'jquery-1.11.1.min.js'),
                    ('server', 'js', 'chosen.jquery.min.js'),
                    ('js', 'build', 'imolecule.min.js'))
            for lib in libs:
                shutil.copy(os.path.join(file_path, *lib), tempdir)

            html_file_url = urljoin('file:', pathname2url(html_filename))

            print('Opening html file: {}'.format(html_file_url))
            webbrowser.open(html_file_url)
        else:
            # We're running in ipython: display widget
            display(HTML(html))
    else:
        return html

Response: Draws an interactive 3D visualization of the inputted chemical.


Instruction:
Can you generate the documentation for the following Python 3 function
def generate(data, format="auto"):
    # Support both files and strings and attempt to infer file type
    try:
        with open(data) as in_file:
            if format == 'auto':
                format = data.split('.')[-1]
            data = in_file.read()
    except:
        if format == 'auto':
            format = 'smi'
    return format_converter.convert(data, format, 'json')

Response: Converts input chemical formats to json and optimizes structure.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def to_json(data, compress=False):
    return json.compress(data) if compress else json.dumps(data)

Response: Converts the output of generate(... ) to json.


Instruction:
Here you have a function in Python 3, explain what it does
def start_server():
    parser = argparse.ArgumentParser(description="Opens a browser-based "
                                     "client that interfaces with the "
                                     "chemical format converter.")
    parser.add_argument('--debug', action="store_true", help="Prints all "
                        "transmitted data streams.")
    parser.add_argument('--port', type=int, default=8000, help="The port "
                        "on which to serve the website.")
    parser.add_argument('--timeout', type=int, default=5, help="The maximum "
                        "time, in seconds, allowed for a process to run "
                        "before returning an error.")
    parser.add_argument('--workers', type=int, default=2, help="The number of "
                        "worker processes to use with the server.")
    parser.add_argument('--no-browser', action="store_true", help="Disables "
                        "opening a browser window on startup.")
    global args
    args = parser.parse_args()

    if args.debug:
        logging.getLogger().setLevel(logging.DEBUG)

    handlers = [(r'/', IndexHandler), (r'/websocket', WebSocket),
                (r'/static/(.*)', tornado.web.StaticFileHandler,
                 {'path': os.path.normpath(os.path.dirname(__file__))})]
    application = tornado.web.Application(handlers)
    application.listen(args.port)

    if not args.no_browser:
        webbrowser.open('http://localhost:%d/' % args.port, new=2)

    try:
        tornado.ioloop.IOLoop.instance().start()
    except KeyboardInterrupt:
        sys.stderr.write("Received keyboard interrupt. Stopping server.\n")
        tornado.ioloop.IOLoop.instance().stop()
        sys.exit(1)

Response: Starts up the imolecule server.


Instruction:
Write a Python 3 script to
evaluate the function pointed to by json - rpc.

Response:
def on_message(self, message):
        """Evaluates the function pointed to by json-rpc."""
        json_rpc = json.loads(message)
        logging.log(logging.DEBUG, json_rpc)

        if self.pool is None:
            self.pool = multiprocessing.Pool(processes=args.workers)

        # Spawn a process to protect the server against segfaults
        async = self.pool.apply_async(_worker_process, [json_rpc])
        try:
            result = async.get(timeout=args.timeout)
            error = 0
        except multiprocessing.TimeoutError:
            result = ("File format conversion timed out! This is due "
                      "either to a large input file or a segmentation "
                      "fault in the underlying open babel library.")
            error = 1
            self.pool.terminate()
            self.pool = multiprocessing.Pool(processes=args.workers)
        except Exception:
            result = traceback.format_exc()
            error = 1
        logging.log(logging.DEBUG, result)

        self.write_message(json.dumps({'result': result, 'error': error,
                                       'id': json_rpc['id']},
                                      separators=(',', ':')))


Instruction:
Can you tell what is the following Python 3 function doing
def nearly_eq(valA, valB, maxf=None, minf=None, epsilon=None):
    '''
    implementation based on:

    http://floating-point-gui.de/errors/comparison/
    '''

    if valA == valB:
        return True

    if maxf is None:
        maxf = float_info.max

    if minf is None:
        minf = float_info.min

    if epsilon is None:
        epsilon = float_info.epsilon

    absA = abs(valA)
    absB = abs(valB)
    delta = abs(valA - valB)

    if (valA == 0) or (valB == 0) or (delta < minf):
        return delta < (epsilon * minf)

    return (delta / min(absA + absB, maxf)) < (epsilon * 2)

Response: returns True if valA == valB and false otherwise


Instruction:
Given the following Python 3 function, write the documentation
def _convert(cls, other, ignoreScalars=False):
        '''
        :other: Point or point equivalent
        :ignorescalars: optional boolean
        :return: Point

        Class private method for converting 'other' into a Point
        subclasss. If 'other' already is a Point subclass, nothing
        is done. If ignoreScalars is True and other is a float or int
        type, a TypeError exception is raised.
        '''
        if ignoreScalars:
            if isinstance(other, (int, float)):
                msg = "unable to convert {} to {}".format(other, cls.__name__)
                raise TypeError(msg)

        return cls(other) if not issubclass(type(other), cls) else other

Response: Convert other into a Point class.


Instruction:
How would you implement a function in Python 3 that
returns a list of three Point objects representing the head of a unit vector.

Response:
def units(cls, scale=1):
        '''
        :scale: optional integer scaling factor
        :return: list of three Point subclass

        Returns three points whose coordinates are the head of a
        unit vector from the origin ( conventionally i, j and k).

        '''
        return [cls(x=scale), cls(y=scale), cls(z=scale)]


Instruction:
How would you implement a function in Python 3 that
returns a new Point subclass with the coordinates picked from a Gaussian distribution with mean mu and standard deviation sigma.

Response:
def gaussian(cls, mu=0, sigma=1):
        '''
        :mu:     mean
        :sigma:  standard deviation
        :return: Point subclass

        Returns a point whose coordinates are picked from a Gaussian
        distribution with mean 'mu' and standard deviation 'sigma'.
        See random.gauss for further explanation of those parameters.
        '''
        return cls(random.gauss(mu, sigma),
                   random.gauss(mu, sigma),
                   random.gauss(mu, sigma))


Instruction:
Implement a function in Python 3 to
return a new Point with random x y and z coordinates bounded by origin and radius around origin.

Response:
def random(cls, origin=None, radius=1):
        '''
        :origin: optional Point or point equivalent
        :radius: optional float, radius around origin
        :return: Point subclass

        Returns a point with random x, y and z coordinates bounded by
        the sphere defined by (origin,radius).

        If a sphere is not supplied, a unit sphere at the origin is
        used by default.
        '''

        p = cls(origin)

        r = random.uniform(0, radius)
        u = random.uniform(0, Two_Pi)
        v = random.uniform(-Half_Pi, Half_Pi)

        r_cosv = r * math.cos(v)

        p.x += r_cosv * math.cos(u)
        p.y += r_cosv * math.sin(u)
        p.z += radius * math.sin(v)

        return p


Instruction:
Can you tell what is the following Python 3 function doing
def _unary_(self, func, inplace=False):
        '''
        :func: unary function to apply to each coordinate
        :inplace: optional boolean
        :return: Point

        Implementation private method.

        All of the unary operations funnel thru this method
        to reduce cut-and-paste code and enforce consistent
        behavior of unary ops.

        Applies 'func' to self and returns the result.

        The expected call signature of 'func' is f(a)

        If 'inplace' is True, the results are stored in 'self',
        otherwise the results will be stored in a new object.

        Returns a Point.

        '''
        dst = self if inplace else self.__class__(self)
        dst.x = func(dst.x)
        dst.y = func(dst.y)
        dst.z = func(dst.z)
        return dst

Response: Internal function that applies a function to each coordinate and returns the result.


Instruction:
Can you generate the documentation for the following Python 3 function
def cross(self, other):
        '''
        :other: Point or point equivalent
        :return: float

        Vector cross product of points U (self) and V (other), computed:

        U x V = (u1*i + u2*j + u3*k) x (v1*i + v2*j + v3*k)
        s1 = u2v3 - u3v2
        s2 = u3v1 - u1v3
        s3 = u1v2 - u2v1

        U x V = s1 + s2 + s3

        Returns a float.
        '''

        b = self.__class__._convert(other)

        return sum([(self.y * b.z) - (self.z * b.y),
                    (self.z * b.x) - (self.x * b.z),
                    (self.x * b.y) - (self.y * b.x)])

Response: Return the cross product of two sets of points U x V.


Instruction:
Can you tell what is the following Python 3 function doing
def isBetween(self, a, b, axes='xyz'):
        '''
        :a: Point or point equivalent
        :b: Point or point equivalent
        :axis: optional string
        :return: float

        Checks the coordinates specified in 'axes' of 'self' to
        determine if they are bounded by 'a' and 'b'. The range
        is inclusive of end-points.

        Returns boolean.
        '''
        a = self.__class__._convert(a)
        b = self.__class__._convert(b)

        fn = lambda k: (self[k] >= min(a[k], b[k])) and (
            self[k] <= max(a[k], b[k]))

        return all(fn(axis) for axis in axes)

Response:
Checks if the coordinates specified in axes of self to
        are bounded by a and b.


Instruction:
Can you implement a function in Python 3 that
returns the angle of rotation around a given point.

Response:
def ccw(self, b, c, axis='z'):
        '''
        :b: Point or point equivalent
        :c: Point or point equivalent
        :axis: optional string or integer in set('x',0,'y',1,'z',2)
        :return: float

        CCW - Counter Clockwise

        Returns an integer signifying the direction of rotation around 'axis'
        described by the angle [b, self, c].

        > 0 : counter-clockwise
          0 : points are collinear
        < 0 : clockwise

        Returns an integer.

        Raises ValueError if axis is not in 'xyz'.
        '''
        bsuba = b - self
        csuba = c - self

        if axis in ['z', 2]:
            return (bsuba.x * csuba.y) - (bsuba.y * csuba.x)

        if axis in ['y', 1]:
            return (bsuba.x * csuba.z) - (bsuba.z * csuba.x)

        if axis in ['x', 0]:
            return (bsuba.y * csuba.z) - (bsuba.z * csuba.y)

        msg = "invalid axis '{!r}', must be one of {}".format(axis, self._keys)

        raise ValueError(msg)


Instruction:
Here you have a function in Python 3, explain what it does
def isCCW(self, b, c, axis='z'):
        '''
        :b: Point or point equivalent
        :c: Point or point equivalent
        :axis: optional string or integer in set('x',0,'y',1,'z',2)
        :return: boolean

        True if the angle determined by a,self,b around 'axis'
        describes a counter-clockwise rotation, otherwise False.

        Raises CollinearPoints if self, b, c are collinear.
        '''

        result = self.ccw(b, c, axis)

        if result == 0:
            raise CollinearPoints(b, self, c)

        return result > 0

Response: Returns True if the angle determined by a self b c around axis is counter - clockwise rotation otherwise False.


Instruction:
How would you explain what the following Python 3 function does
def isCollinear(self, b, c):
        '''
        :b: Point or point equivalent
        :c: Point or point equivalent
        :return: boolean

        True if 'self' is collinear with 'b' and 'c', otherwise False.
        '''

        return all(self.ccw(b, c, axis) == 0 for axis in self._keys)

Response: Return True if self is collinear with b and c otherwise False.


Instruction:
Write a Python 3 function that can
rotate the current Point around a given origin.

Response:
def rotate2d(self, theta, origin=None, axis='z', radians=False):
        '''
        :theta: float radians to rotate self around origin
        :origin: optional Point, defaults to 0,0,0

        Returns a Point rotated by :theta: around :origin:.
        '''

        origin = Point._convert(origin)

        delta = self - origin

        p = Point(origin)

        if not radians:
            theta = math.radians(theta)

        cosT = math.cos(theta)
        sinT = math.sin(theta)

        if axis == 'z':
            p.x += (cosT * delta.x) - (sinT * delta.y)
            p.y += (sinT * delta.x) + (cosT * delta.y)
            return p

        if axis == 'y':
            p.z += (cosT * delta.z) - (sinT * delta.x)
            p.x += (sinT * delta.z) + (cosT * delta.x)
            return p

        if axis == 'x':
            p.y += (cosT * delta.y) - (sinT * delta.z)
            p.z += (sinT * delta.y) + (cosT * delta.z)
            return p

        raise KeyError('unknown axis {}, expecting x, y or z'.format(axis))


Instruction:
Make a summary of the following Python 3 code
def withAngles(cls, origin=None, base=1, alpha=None,
                   beta=None, gamma=None, inDegrees=False):
        '''
        :origin: optional Point
        :alpha: optional float describing length of the side opposite A
        :beta: optional float describing length of the side opposite B
        :gamma: optional float describing length of the side opposite C
        :return: Triangle initialized with points comprising the triangle
                 with the specified angles.
        '''
        raise NotImplementedError("withAngles")

Response:
Returns a Triangle initialized with points comprising the triangle
                 with the specified angles.


Instruction:
Make a summary of the following Python 3 code
def heronsArea(self):
        '''
        Heron's forumla for computing the area of a triangle, float.

        Performance note: contains a square root.

        '''
        s = self.semiperimeter

        return math.sqrt(s * ((s - self.a) * (s - self.b) * (s - self.c)))

Response: Computes the area of a triangle in the Heron s forumla for computing the area of a triangle.


Instruction:
Can you create a Python 3 function that
returns the radius of the circumcenter of the triangle.

Response:
def circumradius(self):
        '''
        Distance from the circumcenter to all the verticies in
        the Triangle, float.

        '''
        return (self.a * self.b * self.c) / (self.area * 4)


Instruction:
Can you generate the documentation for the following Python 3 function
def altitudes(self):
        '''
        A list of the altitudes of each vertex [AltA, AltB, AltC], list of
        floats.

        An altitude is the shortest distance from a vertex to the side
        opposite of it.

        '''
        A = self.area * 2

        return [A / self.a, A / self.b, A / self.c]

Response: A list of the altitudes of each vertex


Instruction:
Write a Python 3 script for
truing iff two side lengths are equal boolean.

Response:
def isIsosceles(self):
        '''
        True iff two side lengths are equal, boolean.
        '''
        return (self.a == self.b) or (self.a == self.c) or (self.b == self.c)


Instruction:
Create a Python 3 function to
return the center point of the ellipse equidistant from foci Point class.

Response:
def center(self):
        '''
        Center point of the ellipse, equidistant from foci, Point class.\n
        Defaults to the origin.
        '''
        try:
            return self._center
        except AttributeError:
            pass
        self._center = Point()
        return self._center


Instruction:
Implement a function in Python 3 to
return the radius of the ellipse Point class.

Response:
def radius(self):
        '''
        Radius of the ellipse, Point class.
        '''
        try:
            return self._radius
        except AttributeError:
            pass
        self._radius = Point(1, 1, 0)
        return self._radius


Instruction:
Write a Python 3 script for
returning True if the major axis is parallel to the X axis boolean.

Response:
def xAxisIsMajor(self):
        '''
        Returns True if the major axis is parallel to the X axis, boolean.
        '''
        return max(self.radius.x, self.radius.y) == self.radius.x


Instruction:
Can you implement a function in Python 3 that
returns True if the minor axis is parallel to the X axis boolean.

Response:
def xAxisIsMinor(self):
        '''
        Returns True if the minor axis is parallel to the X axis, boolean.
        '''
        return min(self.radius.x, self.radius.y) == self.radius.x


Instruction:
How would you implement a function in Python 3 that
returns True if the major axis is parallel to the Y axis boolean.

Response:
def yAxisIsMajor(self):
        '''
        Returns True if the major axis is parallel to the Y axis, boolean.
        '''
        return max(self.radius.x, self.radius.y) == self.radius.y


Instruction:
Can you create a Python 3 function that
returns True if the minor axis is parallel to the Y axis boolean.

Response:
def yAxisIsMinor(self):
        '''
        Returns True if the minor axis is parallel to the Y axis, boolean.
        '''
        return min(self.radius.x, self.radius.y) == self.radius.y


Instruction:
How would you explain what the following Python 3 function does
def a_neg(self):
        '''
        Negative antipodal point on the major axis, Point class.

        '''
        na = Point(self.center)

        if self.xAxisIsMajor:
            na.x -= self.majorRadius
        else:
            na.y -= self.majorRadius
        return na

Response: Negative antipodal point on the major axis Point class.


Instruction:
Can you tell what is the following Python 3 function doing
def b_neg(self):
        '''
        Negative antipodal point on the minor axis, Point class.
        '''
        nb = Point(self.center)

        if self.xAxisIsMinor:
            nb.x -= self.minorRadius
        else:
            nb.y -= self.minorRadius
        return nb

Response: Negative antipodal point on the minor axis Point class.


Instruction:
Write a Python 3 function that can
return the first focus of the ellipse Point class.

Response:
def focus0(self):
        '''
        First focus of the ellipse, Point class.

        '''
        f = Point(self.center)

        if self.xAxisIsMajor:
            f.x -= self.linearEccentricity
        else:
            f.y -= self.linearEccentricity
        return f


Instruction:
Make a summary of the following Python 3 code
def circumcircleForTriangle(cls, triangle):
        '''
        :param: triangle - Triangle class
        :return: Circle class

        Returns the circle where every vertex in the input triangle is
        on the radius of that circle.

        '''

        if triangle.isRight:
            # circumcircle origin is the midpoint of the hypotenues
            o = triangle.hypotenuse.midpoint
            r = o.distance(triangle.A)
            return cls(o, r)

        # otherwise
        # 1. find the normals to two sides
        # 2. translate them to the midpoints of those two sides
        # 3. intersect those lines for center of circumcircle
        # 4. radius is distance from center to any vertex in the triangle

        abn = triangle.AB.normal
        abn += triangle.AB.midpoint

        acn = triangle.AC.normal
        acn += triangle.AC.midpoint

        o = abn.intersection(acn)
        r = o.distance(triangle.A)
        return cls(o, r)

Response: Returns the circle that is the center of the circumcircle of the input triangle.