Instruction:
Can you create a Python 3 function that
parses an integer. Args: text: The text to parse. is_signed: True if a signed integer must be parsed. is_long: True if a long integer must be parsed. Returns: The integer value. Raises: ValueError: Thrown Iff the text is not a valid integer.

Response:
def ParseInteger(text, is_signed=False, is_long=False):
  """Parses an integer.

  Args:
    text: The text to parse.
    is_signed: True if a signed integer must be parsed.
    is_long: True if a long integer must be parsed.

  Returns:
    The integer value.

  Raises:
    ValueError: Thrown Iff the text is not a valid integer.
  """
  # Do the actual parsing. Exception handling is propagated to caller.
  try:
    # We force 32-bit values to int and 64-bit values to long to make
    # alternate implementations where the distinction is more significant
    # (e.g. the C++ implementation) simpler.
    if is_long:
      result = long(text, 0)
    else:
      result = int(text, 0)
  except ValueError:
    raise ValueError('Couldn\'t parse integer: %s' % text)

  # Check if the integer is sane. Exceptions handled by callers.
  checker = _INTEGER_CHECKERS[2 * int(is_long) + int(is_signed)]
  checker.CheckValue(result)
  return result


Instruction:
Can you generate the documentation for the following Python 3 function
def PrintMessage(self, message):
    fields = message.ListFields()
    if self.use_index_order:
      fields.sort(key=lambda x: x[0].index)
    for field, value in fields:
      if _IsMapEntry(field):
        for key in sorted(value):
          # This is slow for maps with submessage entires because it copies the
          # entire tree.  Unfortunately this would take significant refactoring
          # of this file to work around.
          #
          # TODO(haberman): refactor and optimize if this becomes an issue.
          entry_submsg = field.message_type._concrete_class(
              key=key, value=value[key])
          self.PrintField(field, entry_submsg)
      elif field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
        for element in value:
          self.PrintField(field, element)
      else:
        self.PrintField(field, value)

Response: Convert protobuf message to text format.


Instruction:
Create a Python 3 function to
print a single value for a single field.

Response:
def PrintFieldValue(self, field, value):
    """Print a single field value (not including name).

    For repeated fields, the value should be a single element.

    Args:
      field: The descriptor of the field to be printed.
      value: The value of the field.
    """
    out = self.out
    if self.pointy_brackets:
      openb = '<'
      closeb = '>'
    else:
      openb = '{'
      closeb = '}'

    if field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
      if self.as_one_line:
        out.write(' %s ' % openb)
        self.PrintMessage(value)
        out.write(closeb)
      else:
        out.write(' %s\n' % openb)
        self.indent += 2
        self.PrintMessage(value)
        self.indent -= 2
        out.write(' ' * self.indent + closeb)
    elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_ENUM:
      enum_value = field.enum_type.values_by_number.get(value, None)
      if enum_value is not None:
        out.write(enum_value.name)
      else:
        out.write(str(value))
    elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_STRING:
      out.write('\"')
      if isinstance(value, six.text_type):
        out_value = value.encode('utf-8')
      else:
        out_value = value
      if field.type == descriptor.FieldDescriptor.TYPE_BYTES:
        # We need to escape non-UTF8 chars in TYPE_BYTES field.
        out_as_utf8 = False
      else:
        out_as_utf8 = self.as_utf8
      out.write(text_encoding.CEscape(out_value, out_as_utf8))
      out.write('\"')
    elif field.cpp_type == descriptor.FieldDescriptor.CPPTYPE_BOOL:
      if value:
        out.write('true')
      else:
        out.write('false')
    elif field.cpp_type in _FLOAT_TYPES and self.float_format is not None:
      out.write('{1:{0}}'.format(self.float_format, value))
    else:
      out.write(str(value))


Instruction:
Can you write a function in Python 3 where it
converts an text representation of a protocol buffer message into a protocol buffer message.

Response:
def _ParseOrMerge(self, lines, message):
    """Converts an text representation of a protocol message into a message.

    Args:
      lines: Lines of a message's text representation.
      message: A protocol buffer message to merge into.

    Raises:
      ParseError: On text parsing problems.
    """
    tokenizer = _Tokenizer(lines)
    while not tokenizer.AtEnd():
      self._MergeField(tokenizer, message)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _MergeMessageField(self, tokenizer, message, field):
    is_map_entry = _IsMapEntry(field)

    if tokenizer.TryConsume('<'):
      end_token = '>'
    else:
      tokenizer.Consume('{')
      end_token = '}'

    if field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
      if field.is_extension:
        sub_message = message.Extensions[field].add()
      elif is_map_entry:
        # pylint: disable=protected-access
        sub_message = field.message_type._concrete_class()
      else:
        sub_message = getattr(message, field.name).add()
    else:
      if field.is_extension:
        sub_message = message.Extensions[field]
      else:
        sub_message = getattr(message, field.name)
      sub_message.SetInParent()

    while not tokenizer.TryConsume(end_token):
      if tokenizer.AtEnd():
        raise tokenizer.ParseErrorPreviousToken('Expected "%s".' % (end_token,))
      self._MergeField(tokenizer, sub_message)

    if is_map_entry:
      value_cpptype = field.message_type.fields_by_name['value'].cpp_type
      if value_cpptype == descriptor.FieldDescriptor.CPPTYPE_MESSAGE:
        value = getattr(message, field.name)[sub_message.key]
        value.MergeFrom(sub_message.value)
      else:
        getattr(message, field.name)[sub_message.key] = sub_message.value

Response: Merges a single scalar field into a message.


Instruction:
Given the following Python 3 function, write the documentation
def ConsumeIdentifier(self):
    result = self.token
    if not self._IDENTIFIER.match(result):
      raise self._ParseError('Expected identifier.')
    self.NextToken()
    return result

Response: Consumes protocol message field identifier.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def ConsumeInt32(self):
    try:
      result = ParseInteger(self.token, is_signed=True, is_long=False)
    except ValueError as e:
      raise self._ParseError(str(e))
    self.NextToken()
    return result

Response: Consumes a signed 32bit integer number.


Instruction:
Make a summary of the following Python 3 code
def ConsumeFloat(self):
    try:
      result = ParseFloat(self.token)
    except ValueError as e:
      raise self._ParseError(str(e))
    self.NextToken()
    return result

Response: Consumes a floating point number.


Instruction:
Make a summary of the following Python 3 code
def ConsumeBool(self):
    try:
      result = ParseBool(self.token)
    except ValueError as e:
      raise self._ParseError(str(e))
    self.NextToken()
    return result

Response: Consumes a boolean value.


Instruction:
Implement a Python 3 function for
consuming one token of a string literal.

Response:
def _ConsumeSingleByteString(self):
    """Consume one token of a string literal.

    String literals (whether bytes or text) can come in multiple adjacent
    tokens which are automatically concatenated, like in C or Python.  This
    method only consumes one token.

    Returns:
      The token parsed.
    Raises:
      ParseError: When the wrong format data is found.
    """
    text = self.token
    if len(text) < 1 or text[0] not in _QUOTES:
      raise self._ParseError('Expected string but found: %r' % (text,))

    if len(text) < 2 or text[-1] != text[0]:
      raise self._ParseError('String missing ending quote: %r' % (text,))

    try:
      result = text_encoding.CUnescape(text[1:-1])
    except ValueError as e:
      raise self._ParseError(str(e))
    self.NextToken()
    return result


Instruction:
How would you explain what the following Python 3 function does
def timestamp(t = None, forfilename=False):

    datetimesep = ' '
    timesep     = ':'
    if forfilename:
        datetimesep = '-'
        timesep     = '-'

    return time.strftime('%Y-%m-%d' + datetimesep +
                         '%H' + timesep + '%M' + timesep + '%S',
                         time.localtime(t))

Response: Returns a human - readable timestamp given a Unix timestamp t.


Instruction:
Write a Python 3 function for
returning a human - readable timestamp given an Ark timestamp arct.

Response:
def arktimestamp(arkt, forfilename=False):
    """Returns a human-readable timestamp given an Ark timestamp 'arct'.
    An Ark timestamp is the number of seconds since Genesis block,
    2017:03:21 15:55:44."""

    t = arkt + time.mktime((2017, 3, 21, 15, 55, 44, 0, 0, 0))
    return '%d %s' % (arkt, timestamp(t))


Instruction:
Write a Python 3 script to
convert ark timestamp to unix timestamp

Response:
def arkt_to_unixt(ark_timestamp):
    """ convert ark timestamp to unix timestamp"""
    res = datetime.datetime(2017, 3, 21, 15, 55, 44) + datetime.timedelta(seconds=ark_timestamp)
    return res.timestamp()


Instruction:
Make a summary of the following Python 3 code
def get_version():
    with open(VERSION_FILE, encoding='utf-8') as fp:
        content = fp.read()
    match = re.search(r'^__version__ = [\'"]([^\'"]*)[\'"]', content, re.M)
    if match:
        return match.group(1)
    raise RuntimeError("Could not extract package __version__")

Response: Extract version from VERSION_FILE


Instruction:
Can you tell what is the following Python 3 function doing
def de(output_basename, parameter_names, transform, loglikelihood, prior, nsteps=40000, vizfunc=None, printfunc=None, **problem):
	import json
	import inspyred
	import random
	prng = random.Random()
	if 'seed' in problem:
		prng.seed(problem['seed'])

	n_params = len(parameter_names)
	seeds = problem.get('seeds', [])
	if 'start' in problem:
		seeds.append(problem['start'])
	prefix = output_basename

	def viz(candidate, args):
		if vizfunc is not None:
			vizfunc(candidate)
	def print_candidate(candidate, l, args):
		if printfunc is not None:
			printfunc(cube=candidate, loglikelihood=l)
		else:
			print l, candidate
	def eval_candidate(candidate):
		params = transform(candidate)
		l = loglikelihood(params)
		p = prior(params)
		if numpy.isinf(p) and p < 0:
			print '    prior rejection'
			return -1e300
		if numpy.isnan(l):
			return -1e300
		return l, p
	@inspyred.ec.utilities.memoize
	@inspyred.ec.evaluators.evaluator
	def fitness(candidate, args):
		l, p = eval_candidate(candidate)
		#print_candidate(candidate, (l + p), args)
		return (l + p)

	cutoff_store = 10
	def solution_archiver(random, population, archive, args):
		psize = len(population)
		population.sort(reverse=True)
		best = population[0].fitness
		#print 'BEST: ', best,
		all_candidates = sorted(population + archive, reverse=True)
		all_fitness = numpy.array([c.fitness for c in all_candidates])
		mask = best - all_fitness > cutoff_store / 3
		if mask.sum() < 20:
			mask = best - all_fitness > cutoff_store
		newarchive = [c for i, c in enumerate(all_candidates) if i == 0 or all_fitness[i - 1] != c.fitness]
		print 'ARCHIVE: ', len(archive), len(newarchive)
		json.dump([{'candidate': [float(f) for f in c.candidate], 'fitness':c.fitness} for c in newarchive],
			open(prefix + '_values.json', 'w'), indent=4)
		return newarchive

	def observer(population, num_generations, num_evaluations, args):
		population.sort(reverse=True)
		candidate = population[0]
		print ('{0} evaluations'.format(num_evaluations)), ' best:',
		print_candidate(candidate.candidate, candidate.fitness, args)
		if num_evaluations % len(population) == 0 or num_evaluations < len(population) or args.get('force_viz', False):
			# for each turnaround of a full generation
			viz(candidate.candidate, args)

	def generator(random, args):
		u = [random.uniform(0, 1) for _ in range(n_params)]
		u = [random.gauss(0.5, 0.1) for _ in range(n_params)]
		return bounder(u, args)

	ea = inspyred.ec.DEA(prng)
	ea.terminator = inspyred.ec.terminators.evaluation_termination
	ea.archiver = solution_archiver
	bounder = inspyred.ec.Bounder(lower_bound=1e-10, upper_bound=1-1e-10)
	#bounder = inspyred.ec.Bounder(lower_bound=-20, upper_bound=20)
	import copy
	from math import log
	@inspyred.ec.variators.mutator
	def double_exponential_mutation(random, candidate, args):
		mut_rate = args.setdefault('mutation_rate', 0.1)
		mean = args.setdefault('gaussian_mean', 0.0)
		stdev = args.setdefault('gaussian_stdev', 1.0)
		scale = log(0.5) / - (stdev)
		bounder = args['_ec'].bounder
		mutant = copy.copy(candidate)
		for i, m in enumerate(mutant):
			dice = random.random()
			if dice < mut_rate:
				sign = (dice < mut_rate / 2) * 2 - 1
				delta = -log(random.random()) / scale
				mutant[i] += delta * sign
			mutant = bounder(mutant, args)
		return mutant

	def minute_gaussian_mutation(random, candidates, args):
		args = dict(args)
		args['mutation_rate'] = 1
		args['gaussian_stdev'] = 1e-6
		return inspyred.ec.variators.gaussian_mutation(random, candidates, args)
	ea.variator = [inspyred.ec.variators.n_point_crossover, inspyred.ec.variators.gaussian_mutation, minute_gaussian_mutation]
	#ea.variator = [inspyred.ec.variators.n_point_crossover, double_exponential_mutation]

	ea.replacer = inspyred.ec.replacers.steady_state_replacement
	ea.observer = observer

	pop_size = 20

	final_pop = ea.evolve(pop_size=pop_size,
		max_evaluations=nsteps, maximize=True, seeds=seeds,
		gaussian_stdev=0.01, #mutation_rate=0.3,
		bounder=bounder, generator=generator, evaluator=fitness,
		)

	best = max(final_pop)
	seeds = [c.candidate for c in ea.archive]
	print 'final candidate:', best

	return {'start': best.candidate, 'value': best.fitness,
		'seeds': seeds, 'method': 'DE'}

Response: de - function to do differential evolution of a current best solution.


Instruction:
Create a Python 3 function to
replace macros with content defined in the config.

Response:
def process_macros(self, content: str) -> str:
        '''Replace macros with content defined in the config.

        :param content: Markdown content

        :returns: Markdown content without macros
        '''

        def _sub(macro):
            name = macro.group('body')
            params = self.get_options(macro.group('options'))

            return self.options['macros'].get(name, '').format_map(params)

        return self.pattern.sub(_sub, content)


Instruction:
Make a summary of the following Python 3 code
def _request(self, method, *relative_path_parts, **kwargs):
    uri = self._create_api_uri(*relative_path_parts)
    response = get(uri, params=kwargs.get('params', None))
    return self._handle_response(response).json()

Response: Sends an HTTP request to the REST API and receives the requested data.


Instruction:
How would you explain what the following Python 3 function does
def get_historic_trades(self, start, end, granularity, product_id='BTC-USD'):
    params = {
      'start':self._format_iso_time(start),
      'end':self._format_iso_time(end),
      'granularity':granularity
    }
    return self._get('products', product_id, 'candles', params=params)

Response: Get historical trades from a given product.


Instruction:
Can you tell what is the following Python 3 function doing
def _request(self, method, *relative_path_parts, **kwargs):
    uri = self._create_api_uri(*relative_path_parts)
    response = get(uri, params=self._get_params(**kwargs))
    self.is_initial = False
    self.before_cursor = response.headers.get('cb-before', None)
    self.after_cursor = response.headers.get('cb-after', None)
    return self._handle_response(response).json()

Response: Sends an HTTP request to the REST API and receives the requested data.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_unique_pathname(path, root=''):
	path = os.path.join(root, path)
	# consider the path supplied, then the paths with numbers appended
	potentialPaths = itertools.chain((path,), __get_numbered_paths(path))
	potentialPaths = six.moves.filterfalse(os.path.exists, potentialPaths)
	return next(potentialPaths)

Response: Return a unique pathname possibly with a number appended to it so that it is unique in the directory.


Instruction:
Write a Python 3 script for
returning a list of the numbered paths in sequential order to the filename or folder name

Response:
def __get_numbered_paths(filepath):
	"""Append numbers in sequential order to the filename or folder name
	Numbers should be appended before the extension on a filename."""
	format = '%s (%%d)%s' % splitext_files_only(filepath)
	return map(lambda n: format % n, itertools.count(1))


Instruction:
Can you tell what is the following Python 3 function doing
def set_time(filename, mod_time):
	log.debug('Setting modified time to %s', mod_time)
	mtime = calendar.timegm(mod_time.utctimetuple())
	# utctimetuple discards microseconds, so restore it (for consistency)
	mtime += mod_time.microsecond / 1000000
	atime = os.stat(filename).st_atime
	os.utime(filename, (atime, mtime))

Response: Set the modified time of a file.


Instruction:
Given the following Python 3 function, write the documentation
def get_time(filename):
	ts = os.stat(filename).st_mtime
	return datetime.datetime.utcfromtimestamp(ts)

Response: Get the modified time for a file as a datetime instance


Instruction:
Given the following Python 3 function, write the documentation
def insert_before_extension(filename, content):
	parts = list(os.path.splitext(filename))
	parts[1:1] = [content]
	return ''.join(parts)

Response: Insert the content just before the extension of the file.


Instruction:
Here you have a function in Python 3, explain what it does
def recursive_glob(root, spec):
	specs = (
		os.path.join(dirpath, dirname, spec)
		for dirpath, dirnames, filenames in os.walk(root)
		for dirname in dirnames
	)

	return itertools.chain.from_iterable(
		glob.iglob(spec)
		for spec in specs
	)

Response:
Like iglob, but recurse directories

	>>> any('path.py' in result for result in recursive_glob('.', '*.py'))
	True

	>>> all(result.startswith('.') for result in recursive_glob('.', '*.py'))
	True

	>>> len(list(recursive_glob('.', '*.foo')))
	0


Instruction:
Given the following Python 3 function, write the documentation
def encode(name, system='NTFS'):
	assert system == 'NTFS', 'unsupported filesystem'
	special_characters = r'<>:"/\|?*' + ''.join(map(chr, range(32)))
	pattern = '|'.join(map(re.escape, special_characters))
	pattern = re.compile(pattern)
	return pattern.sub('_', name)

Response: Encode a name for a suitable name in the given filesystem


Instruction:
Write a Python 3 function for
wrapping a function that returns a dir making sure it exists

Response:
def ensure_dir_exists(func):
	"wrap a function that returns a dir, making sure it exists"
	@functools.wraps(func)
	def make_if_not_present():
		dir = func()
		if not os.path.isdir(dir):
			os.makedirs(dir)
		return dir
	return make_if_not_present


Instruction:
Create a Python 3 function for
reading file in chunks of size chunk_size.

Response:
def read_chunks(file, chunk_size=2048, update_func=lambda x: None):
	"""
	Read file in chunks of size chunk_size (or smaller).
	If update_func is specified, call it on every chunk with the amount
	read.
	"""
	while(True):
		res = file.read(chunk_size)
		if not res:
			break
		update_func(len(res))
		yield res


Instruction:
Can you generate the documentation for the following Python 3 function
def is_hidden(path):
	full_path = os.path.abspath(path)
	name = os.path.basename(full_path)

	def no(path):
		return False
	platform_hidden = globals().get('is_hidden_' + platform.system(), no)
	return name.startswith('.') or platform_hidden(full_path)

Response: Check if a file is presumed hidden


Instruction:
How would you implement a function in Python 3 that
gets the age of the current entry.

Response:
def age(self):
        """
        Get closer to your EOL
        """
        # 0 means this composer will never decompose
        if self.rounds == 1:
            self.do_run = False
        elif self.rounds > 1:
            self.rounds -= 1


Instruction:
Write a Python 3 function that can
open a connection to the serial line and receive data lines

Response:
def run(self):
        """
        Open a connection over the serial line and receive data lines
        """
        if not self.device:
            return
        try:
            data = ""
            while (self.do_run):
                try:
                    if (self.device.inWaiting() > 1):
                        l = self.device.readline()[:-2]
                        l = l.decode("UTF-8")

                        if (l == "["):
                            # start recording
                            data = "["
                        elif (l == "]") and (len(data) > 4) and (data[0] == "["):
                            # now parse the input
                            data = data + "]"
                            self.store.register_json(data)
                            self.age()
                        elif (l[0:3] == "  {"):
                            # this is a data line
                            data = data + " " + l
                    else:
                        # this is a slow interface - give it some time
                        sleep(1)
                        # then count down..
                        self.age()
                except (UnicodeDecodeError, ValueError):
                    # only accepting unicode: throw away the whole bunch
                    data = ""
                    # and count down the exit condition
                    self.age()

        except serial.serialutil.SerialException:
            print("Could not connect to the serial line at " + self.device_name)


Instruction:
Here you have a function in Python 3, explain what it does
def append_main_thread(self):
        thread = MainThread(main_queue=self.main_queue,
                            main_spider=self.main_spider,
                            branch_spider=self.branch_spider)
        thread.daemon = True
        thread.start()

Response: create & start main thread


Instruction:
Write a Python 3 script for
getting the text of a node.

Response:
def getTextFromNode(node):
    """
    Scans through all children of node and gathers the
    text. If node has non-text child-nodes then
    NotTextNodeError is raised.
    """
    t = ""
    for n in node.childNodes:
        if n.nodeType == n.TEXT_NODE:
            t += n.nodeValue
        else:
            raise NotTextNodeError
    return t


Instruction:
Given the following Python 3 function, write the documentation
def getbalance(self, url='http://services.ambientmobile.co.za/credits'):
        postXMLList = []
        postXMLList.append("<api-key>%s</api-key>" % self.api_key)
        postXMLList.append("<password>%s</password>" % self.password)
        postXML = '<sms>%s</sms>' % "".join(postXMLList)
        result = self.curl(url, postXML)

        if result.get("credits", None):
            return result["credits"]
        else:
            raise AmbientSMSError(result["status"])

Response: Get the number of credits remaining at AmbientSMS


Instruction:
How would you explain what the following Python 3 function does
def sendmsg(self,
                message,
                recipient_mobiles=[],
                url='http://services.ambientmobile.co.za/sms',
                concatenate_message=True,
                message_id=str(time()).replace(".", ""),
                reply_path=None,
                allow_duplicates=True,
                allow_invalid_numbers=True,
                ):

        """
        Send a mesage via the AmbientSMS API server
        """
        if not recipient_mobiles or not(isinstance(recipient_mobiles, list) \
                or isinstance(recipient_mobiles, tuple)):
            raise AmbientSMSError("Missing recipients")

        if not message or not len(message):
            raise AmbientSMSError("Missing message")

        postXMLList = []
        postXMLList.append("<api-key>%s</api-key>" % self.api_key)
        postXMLList.append("<password>%s</password>" % self.password)
        postXMLList.append("<recipients>%s</recipients>" % \
                "".join(["<mobile>%s</mobile>" % \
                m for m in recipient_mobiles]))
        postXMLList.append("<msg>%s</msg>" % message)
        postXMLList.append("<concat>%s</concat>" % \
                (1 if concatenate_message else 0))
        postXMLList.append("<message_id>%s</message_id>" % message_id)
        postXMLList.append("<allow_duplicates>%s</allow_duplicates>" % \
                (1 if allow_duplicates else 0))
        postXMLList.append(
            "<allow_invalid_numbers>%s</allow_invalid_numbers>" % \
                    (1 if allow_invalid_numbers else 0)
        )
        if reply_path:
            postXMLList.append("<reply_path>%s</reply_path>" % reply_path)

        postXML = '<sms>%s</sms>' % "".join(postXMLList)
        result = self.curl(url, postXML)

        status = result.get("status", None)
        if status and int(status) in [0, 1, 2]:
            return result
        else:
            raise AmbientSMSError(int(status))

Response: Send a message via the AmbientSMS API.


Instruction:
Implement a function in Python 3 to
send a curl request to the AmbientSMS API

Response:
def curl(self, url, post):
        """
        Inteface for sending web requests to the AmbientSMS API Server
        """
        try:
            req = urllib2.Request(url)
            req.add_header("Content-type", "application/xml")
            data = urllib2.urlopen(req, post.encode('utf-8')).read()
        except urllib2.URLError, v:
            raise AmbientSMSError(v)
        return dictFromXml(data)


Instruction:
Given the following Python 3 function, write the documentation
def execute(self, correlation_id, args):
        return self._intercepter.execute(_next, correlation_id, args)

Response: Executes the command given specific arguments as an input.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def contents(self, f, text):
        text += self._read(f.abs_path) + "\r\n"
        return text

Response: Returns the contents of the file f


Instruction:
Can you tell what is the following Python 3 function doing
def is_date_type(cls):
    if not isinstance(cls, type):
        return False
    return issubclass(cls, date) and not issubclass(cls, datetime)

Response: Return True if the class is a date type.


Instruction:
Here you have a function in Python 3, explain what it does
def to_datetime(when):
    if when is None or is_datetime(when):
        return when
    if is_time(when):
        return datetime.combine(epoch.date(), when)
    if is_date(when):
        return datetime.combine(when, time(0))
    raise TypeError("unable to convert {} to datetime".format(when.__class__.__name__))

Response: Convert a date or time to a datetime.


Instruction:
Explain what the following Python 3 code does
def totz(when, tz=None):
    if when is None:
        return None
    when = to_datetime(when)
    if when.tzinfo is None:
        when = when.replace(tzinfo=localtz)
    return when.astimezone(tz or utc)

Response: Return a date time or datetime converted to a date or time object in the given timezone.


Instruction:
How would you code a function in Python 3 to
return a datetime so much time ago. Takes the same arguments as timedelta.

Response:
def timeago(tz=None, *args, **kwargs):
    """Return a datetime so much time ago. Takes the same arguments as timedelta()."""
    return totz(datetime.now(), tz) - timedelta(*args, **kwargs)


Instruction:
Make a summary of the following Python 3 code
def ts(when, tz=None):
    if not when:
        return None
    when = totz(when, tz)
    return calendar.timegm(when.timetuple())

Response: Returns a Unix timestamp in seconds for the provided datetime.


Instruction:
Create a Python 3 function for
returning a Unix timestamp in milliseconds for the provided datetime.

Response:
def tsms(when, tz=None):
    """
    Return a Unix timestamp in milliseconds for the provided datetime. The `totz` function is
    called on the datetime to convert it to the provided timezone. It will be converted to UTC if
    no timezone is provided.
    """
    if not when:
        return None
    when = totz(when, tz)
    return calendar.timegm(when.timetuple()) * 1000 + int(round(when.microsecond / 1000.0))


Instruction:
Explain what the following Python 3 code does
def fromts(ts, tzin=None, tzout=None):
    if ts is None:
        return None
    when = datetime.utcfromtimestamp(ts).replace(tzinfo=tzin or utc)
    return totz(when, tzout)

Response: Returns a datetime representation of the provided Unix timestamp.


Instruction:
Can you tell what is the following Python 3 function doing
def fromtsms(ts, tzin=None, tzout=None):
    if ts is None:
        return None
    when = datetime.utcfromtimestamp(ts / 1000).replace(microsecond=ts % 1000 * 1000)
    when = when.replace(tzinfo=tzin or utc)
    return totz(when, tzout)

Response: Returns the Unix timestamp in milliseconds as a datetime object.


Instruction:
How would you explain what the following Python 3 function does
def truncate(when, unit, week_start=mon):
    if is_datetime(when):
        if unit == millisecond:
            return when.replace(microsecond=int(round(when.microsecond / 1000.0)) * 1000)
        elif unit == second:
            return when.replace(microsecond=0)
        elif unit == minute:
            return when.replace(second=0, microsecond=0)
        elif unit == hour:
            return when.replace(minute=0, second=0, microsecond=0)
        elif unit == day:
            return when.replace(hour=0, minute=0, second=0, microsecond=0)
        elif unit == week:
            weekday = prevweekday(when, week_start)
            return when.replace(year=weekday.year, month=weekday.month, day=weekday.day,
                                hour=0, minute=0, second=0, microsecond=0)
        elif unit == month:
            return when.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
        elif unit == year:
            return when.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
    elif is_date(when):
        if unit == week:
            return prevweekday(when, week_start)
        elif unit == month:
            return when.replace(day=1)
        elif unit == year:
            return when.replace(month=1, day=1)
    elif is_time(when):
        if unit == millisecond:
            return when.replace(microsecond=int(when.microsecond / 1000.0) * 1000)
        elif unit == second:
            return when.replace(microsecond=0)
        elif unit == minute:
            return when.replace(second=0, microsecond=0)
    return when

Response: Returns the datetime truncated to the precision of the provided unit.


Instruction:
Can you write a function in Python 3 where it
returns the date for the day of this week.

Response:
def weekday(when, weekday, start=mon):
    """Return the date for the day of this week."""
    if isinstance(when, datetime):
        when = when.date()

    today = when.weekday()
    delta = weekday - today
    if weekday < start and today >= start:
        delta += 7
    elif weekday >= start and today < start:
        delta -= 7
    return when + timedelta(days=delta)


Instruction:
Given the following Python 3 function, write the documentation
def prevweekday(when, weekday, inclusive=True):
    if isinstance(when, datetime):
        when = when.date()
    delta = weekday - when.weekday()
    if (inclusive and delta > 0) or (not inclusive and delta >= 0):
        delta -= 7
    return when + timedelta(days=delta)

Response: Return the date for the most recent weekday.


Instruction:
How would you code a function in Python 3 to
function to optimize the parameters in a 1D grid

Response:
def opt_grid(params, func, limits, ftol=0.01, disp=0, compute_errors=True):
	"""
	see :func:`optimize1d.optimize`, considers each parameter in order

	:param ftol:
		difference in values at which the function can be considered flat
	:param compute_errors:
	 	compute standard deviation of gaussian around optimum
	"""
	caches = [[] for p in params]
	newparams = numpy.copy(params)
	errors = [[] for p in params]
	for i, p in enumerate(params):
		cache = []
		def func1(x0):
			newparams[i] = x0
			v = func(newparams)
			cache.append([x0, v])
			return v
		lo, hi = limits[i]
		bestval = optimize(func1, x0=p,
			cons=[lambda x: x - lo, lambda x: hi - x],
			ftol=ftol, disp=disp - 1)
		beststat = func1(bestval)
		if compute_errors:
			errors[i] = cache2errors(func1, cache, disp=disp - 1)

		newparams[i] = bestval
		caches[i] = cache
		if disp > 0:
			if compute_errors:
				print '\tnew value of %d: %e [%e .. %e] yielded %e' % (i, bestval, errors[i][0], errors[i][1], beststat)
			else:
				print '\tnew value of %d: %e yielded %e' % (i, bestval, beststat)
	beststat = func(newparams)
	if disp > 0:
		print 'optimization done, reached %.3f' % (beststat)

	if compute_errors:
		return newparams, errors
	else:
		return newparams


Instruction:
Explain what the following Python 3 code does
def Cwd(directory):
    '''
    Context manager for current directory (uses with_statement)

    e.g.:
        # working on some directory
        with Cwd('/home/new_dir'):
            # working on new_dir

        # working on some directory again

    :param unicode directory:
        Target directory to enter
    '''
    old_directory = six.moves.getcwd()
    if directory is not None:
        os.chdir(directory)
    try:
        yield directory
    finally:
        os.chdir(old_directory)

Response: Context manager for working on current directory.


Instruction:
Can you write a function in Python 3 where it
normalizes a path maintaining the final slashes.

Response:
def NormalizePath(path):
    '''
    Normalizes a path maintaining the final slashes.

    Some environment variables need the final slash in order to work.

    Ex. The SOURCES_DIR set by subversion must end with a slash because of the way it is used
    in the Visual Studio projects.

    :param unicode path:
        The path to normalize.

    :rtype: unicode
    :returns:
        Normalized path
    '''
    if path.endswith('/') or path.endswith('\\'):
        slash = os.path.sep
    else:
        slash = ''
    return os.path.normpath(path) + slash


Instruction:
How would you implement a function in Python 3 that
returns a version of a path that is unique.

Response:
def CanonicalPath(path):
    '''
    Returns a version of a path that is unique.

    Given two paths path1 and path2:
        CanonicalPath(path1) == CanonicalPath(path2) if and only if they represent the same file on
        the host OS. Takes account of case, slashes and relative paths.

    :param unicode path:
        The original path.

    :rtype: unicode
    :returns:
        The unique path.
    '''
    path = os.path.normpath(path)
    path = os.path.abspath(path)
    path = os.path.normcase(path)

    return path


Instruction:
Can you write a function in Python 3 where it
replaces all slashes and backslashes with the target separator StandardPath: We are defining that the standard-path is the one with only back-slashes in it, either on Windows or any other platform. :param bool strip: If True, removes additional slashes from the end of the path.

Response:
def StandardizePath(path, strip=False):
    '''
    Replaces all slashes and backslashes with the target separator

    StandardPath:
        We are defining that the standard-path is the one with only back-slashes in it, either
        on Windows or any other platform.

    :param bool strip:
        If True, removes additional slashes from the end of the path.
    '''
    path = path.replace(SEPARATOR_WINDOWS, SEPARATOR_UNIX)
    if strip:
        path = path.rstrip(SEPARATOR_UNIX)
    return path


Instruction:
Here you have a function in Python 3, explain what it does
def NormStandardPath(path):
    '''
    Normalizes a standard path (posixpath.normpath) maintaining any slashes at the end of the path.

    Normalize:
        Removes any local references in the path "/../"

    StandardPath:
        We are defining that the standard-path is the one with only back-slashes in it, either
        on Windows or any other platform.
    '''
    import posixpath
    if path.endswith('/'):
        slash = '/'
    else:
        slash = ''
    return posixpath.normpath(path) + slash

Response: Normalizes a standard path.


Instruction:
Given the following Python 3 function, write the documentation
def CreateMD5(source_filename, target_filename=None):
    '''
    Creates a md5 file from a source file (contents are the md5 hash of source file)

    :param unicode source_filename:
        Path to source file

    :type target_filename: unicode or None
    :param target_filename:
        Name of the target file with the md5 contents

        If None, defaults to source_filename + '.md5'
    '''
    if target_filename is None:
        target_filename = source_filename + '.md5'

    from six.moves.urllib.parse import urlparse
    source_url = urlparse(source_filename)

    # Obtain MD5 hex
    if _UrlIsLocal(source_url):
        # If using a local file, we can give Md5Hex the filename
        md5_contents = Md5Hex(filename=source_filename)
    else:
        # Md5Hex can't handle remote files, we open it and pray we won't run out of memory.
        md5_contents = Md5Hex(contents=GetFileContents(source_filename, binary=True))

    # Write MD5 hash to a file
    CreateFile(target_filename, md5_contents)

Response: Creates a md5 file from a source file.


Instruction:
Can you create a Python 3 function that
copies a file from source to target.

Response:
def CopyFile(source_filename, target_filename, override=True, md5_check=False, copy_symlink=True):
    '''
    Copy a file from source to target.

    :param  source_filename:
        @see _DoCopyFile

    :param  target_filename:
        @see _DoCopyFile

    :param bool md5_check:
        If True, checks md5 files (of both source and target files), if they match, skip this copy
        and return MD5_SKIP

        Md5 files are assumed to be {source, target} + '.md5'

        If any file is missing (source, target or md5), the copy will always be made.

    :param  copy_symlink:
        @see _DoCopyFile

    :raises FileAlreadyExistsError:
        If target_filename already exists, and override is False

    :raises NotImplementedProtocol:
        If file protocol is not accepted

        Protocols allowed are:
            source_filename: local, ftp, http
            target_filename: local, ftp

    :rtype: None | MD5_SKIP
    :returns:
        MD5_SKIP if the file was not copied because there was a matching .md5 file

    .. seealso:: FTP LIMITATIONS at this module's doc for performance issues information
    '''
    from ._exceptions import FileNotFoundError

    # Check override
    if not override and Exists(target_filename):
        from ._exceptions import FileAlreadyExistsError
        raise FileAlreadyExistsError(target_filename)

    # Don't do md5 check for md5 files themselves.
    md5_check = md5_check and not target_filename.endswith('.md5')

    # If we enabled md5 checks, ignore copy of files that haven't changed their md5 contents.
    if md5_check:
        source_md5_filename = source_filename + '.md5'
        target_md5_filename = target_filename + '.md5'
        try:
            source_md5_contents = GetFileContents(source_md5_filename)
        except FileNotFoundError:
            source_md5_contents = None

        try:
            target_md5_contents = GetFileContents(target_md5_filename)
        except FileNotFoundError:
            target_md5_contents = None

        if source_md5_contents is not None and \
           source_md5_contents == target_md5_contents and \
           Exists(target_filename):
            return MD5_SKIP

    # Copy source file
    _DoCopyFile(source_filename, target_filename, copy_symlink=copy_symlink)

    # If we have a source_md5, but no target_md5, create the target_md5 file
    if md5_check and source_md5_contents is not None and source_md5_contents != target_md5_contents:
        CreateFile(target_md5_filename, source_md5_contents)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _DoCopyFile(source_filename, target_filename, copy_symlink=True):
    '''
    :param unicode source_filename:
        The source filename.
        Schemas: local, ftp, http

    :param unicode target_filename:
        Target filename.
        Schemas: local, ftp

    :param  copy_symlink:
        @see _CopyFileLocal

    :raises FileNotFoundError:
        If source_filename does not exist
    '''
    from six.moves.urllib.parse import urlparse

    source_url = urlparse(source_filename)
    target_url = urlparse(target_filename)

    if _UrlIsLocal(source_url):
        if not Exists(source_filename):
            from ._exceptions import FileNotFoundError
            raise FileNotFoundError(source_filename)

        if _UrlIsLocal(target_url):
            # local to local
            _CopyFileLocal(source_filename, target_filename, copy_symlink=copy_symlink)
        elif target_url.scheme in ['ftp']:
            from ._exceptions import NotImplementedProtocol
            raise NotImplementedProtocol(target_url.scheme)
        else:
            from ._exceptions import NotImplementedProtocol
            raise NotImplementedProtocol(target_url.scheme)

    elif source_url.scheme in ['http', 'https', 'ftp']:
        if _UrlIsLocal(target_url):
            # HTTP/FTP to local
            from ._exceptions import NotImplementedProtocol
            raise NotImplementedProtocol(target_url.scheme)
        else:
            # HTTP/FTP to other ==> NotImplemented
            from ._exceptions import NotImplementedProtocol
            raise NotImplementedProtocol(target_url.scheme)
    else:
        from ._exceptions import NotImplementedProtocol  # @Reimport
        raise NotImplementedProtocol(source_url.scheme)

Response: Copy a file from source_filename to target_filename.


Instruction:
Implement a function in Python 3 to
copy a file locally to a directory.

Response:
def _CopyFileLocal(source_filename, target_filename, copy_symlink=True):
    '''
    Copy a file locally to a directory.

    :param unicode source_filename:
        The filename to copy from.

    :param unicode target_filename:
        The filename to copy to.

    :param bool copy_symlink:
        If True and source_filename is a symlink, target_filename will also be created as
        a symlink.

        If False, the file being linked will be copied instead.
    '''
    import shutil
    try:
        # >>> Create the target_filename directory if necessary
        dir_name = os.path.dirname(target_filename)
        if dir_name and not os.path.isdir(dir_name):
            os.makedirs(dir_name)

        if copy_symlink and IsLink(source_filename):
            # >>> Delete the target_filename if it already exists
            if os.path.isfile(target_filename) or IsLink(target_filename):
                DeleteFile(target_filename)

            # >>> Obtain the relative path from link to source_filename (linkto)
            source_filename = ReadLink(source_filename)
            CreateLink(source_filename, target_filename)
        else:
            # shutil can't copy links in Windows, so we must find the real file manually
            if sys.platform == 'win32':
                while IsLink(source_filename):
                    link = ReadLink(source_filename)
                    if os.path.isabs(link):
                        source_filename = link
                    else:
                        source_filename = os.path.join(os.path.dirname(source_filename), link)

            shutil.copyfile(source_filename, target_filename)
            shutil.copymode(source_filename, target_filename)
    except Exception as e:
        reraise(e, 'While executiong _filesystem._CopyFileLocal(%s, %s)' % (source_filename, target_filename))


Instruction:
Can you create a Python 3 function that
copies files from source to target.

Response:
def CopyFiles(source_dir, target_dir, create_target_dir=False, md5_check=False):
    '''
    Copy files from the given source to the target.

    :param unicode source_dir:
        A filename, URL or a file mask.
        Ex.
            x:\coilib50
            x:\coilib50\*
            http://server/directory/file
            ftp://server/directory/file

    :param unicode target_dir:
        A directory or an URL
        Ex.
            d:\Temp
            ftp://server/directory

    :param bool create_target_dir:
        If True, creates the target path if it doesn't exists.

    :param bool md5_check:
        .. seealso:: CopyFile

    :raises DirectoryNotFoundError:
        If target_dir does not exist, and create_target_dir is False

    .. seealso:: CopyFile for documentation on accepted protocols

    .. seealso:: FTP LIMITATIONS at this module's doc for performance issues information
    '''
    import fnmatch

    # Check if we were given a directory or a directory with mask
    if IsDir(source_dir):
        # Yes, it's a directory, copy everything from it
        source_mask = '*'
    else:
        # Split directory and mask
        source_dir, source_mask = os.path.split(source_dir)

    # Create directory if necessary
    if not IsDir(target_dir):
        if create_target_dir:
            CreateDirectory(target_dir)
        else:
            from ._exceptions import DirectoryNotFoundError
            raise DirectoryNotFoundError(target_dir)

    # List and match files
    filenames = ListFiles(source_dir)

    # Check if we have a source directory
    if filenames is None:
        return

    # Copy files
    for i_filename in filenames:
        if md5_check and i_filename.endswith('.md5'):
            continue  # md5 files will be copied by CopyFile when copying their associated files

        if fnmatch.fnmatch(i_filename, source_mask):
            source_path = source_dir + '/' + i_filename
            target_path = target_dir + '/' + i_filename

            if IsDir(source_path):
                # If we found a directory, copy it recursively
                CopyFiles(source_path, target_path, create_target_dir=True, md5_check=md5_check)
            else:
                CopyFile(source_path, target_path, md5_check=md5_check)


Instruction:
Write a Python 3 function for
copying files into directories according to a file mapping.

Response:
def CopyFilesX(file_mapping):
    '''
    Copies files into directories, according to a file mapping

    :param list(tuple(unicode,unicode)) file_mapping:
        A list of mappings between the directory in the target and the source.
        For syntax, @see: ExtendedPathMask

    :rtype: list(tuple(unicode,unicode))
    :returns:
        List of files copied. (source_filename, target_filename)

    .. seealso:: FTP LIMITATIONS at this module's doc for performance issues information
    '''
    # List files that match the mapping
    files = []
    for i_target_path, i_source_path_mask in file_mapping:
        tree_recurse, flat_recurse, dirname, in_filters, out_filters = ExtendedPathMask.Split(i_source_path_mask)

        _AssertIsLocal(dirname)

        filenames = FindFiles(dirname, in_filters, out_filters, tree_recurse)
        for i_source_filename in filenames:
            if os.path.isdir(i_source_filename):
                continue  # Do not copy dirs

            i_target_filename = i_source_filename[len(dirname) + 1:]
            if flat_recurse:
                i_target_filename = os.path.basename(i_target_filename)
            i_target_filename = os.path.join(i_target_path, i_target_filename)

            files.append((
                StandardizePath(i_source_filename),
                StandardizePath(i_target_filename)
            ))

    # Copy files
    for i_source_filename, i_target_filename in files:
        # Create target dir if necessary
        target_dir = os.path.dirname(i_target_filename)
        CreateDirectory(target_dir)

        CopyFile(i_source_filename, i_target_filename)

    return files


Instruction:
Given the following Python 3 function, write the documentation
def IsFile(path):
    '''
    :param unicode path:
        Path to a file (local or ftp)

    :raises NotImplementedProtocol:
        If checking for a non-local, non-ftp file

    :rtype: bool
    :returns:
        True if the file exists

    .. seealso:: FTP LIMITATIONS at this module's doc for performance issues information
    '''
    from six.moves.urllib.parse import urlparse
    url = urlparse(path)

    if _UrlIsLocal(url):
        if IsLink(path):
            return IsFile(ReadLink(path))
        return os.path.isfile(path)

    elif url.scheme == 'ftp':
        from ._exceptions import NotImplementedProtocol
        raise NotImplementedProtocol(url.scheme)
    else:
        from ._exceptions import NotImplementedProtocol
        raise NotImplementedProtocol(url.scheme)

Response: Checks if a file exists in the base directory.


Instruction:
Given the following Python 3 function, write the documentation
def GetDriveType(path):
    '''
    Determine the type of drive, which can be one of the following values:
        DRIVE_UNKNOWN = 0
            The drive type cannot be determined.

        DRIVE_NO_ROOT_DIR = 1
            The root path is invalid; for example, there is no volume mounted at the specified path.

        DRIVE_REMOVABLE = 2
            The drive has removable media; for example, a floppy drive, thumb drive, or flash card reader.

        DRIVE_FIXED = 3
            The drive has fixed media; for example, a hard disk drive or flash drive.

        DRIVE_REMOTE = 4
            The drive is a remote (network) drive.

        DRIVE_CDROM = 5
            The drive is a CD-ROM drive.

        DRIVE_RAMDISK = 6
            The drive is a RAM disk

    :note:
        The implementation is valid only for Windows OS
        Linux will always return DRIVE_UNKNOWN

    :param path:
        Path to a file or directory
    '''
    if sys.platform == 'win32':
        import ctypes
        kdll = ctypes.windll.LoadLibrary("kernel32.dll")

        return kdll.GetDriveType(path + '\\')

        import win32file
        if IsFile(path):
            path = os.path.dirname(path)

        # A trailing backslash is required.
        return win32file.GetDriveType(path + '\\')

    else:
        return DRIVE_UNKNOWN

Response: Returns the type of drive in the specified path.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def IsDir(directory):
    '''
    :param unicode directory:
        A path

    :rtype: bool
    :returns:
        Returns whether the given path points to an existent directory.

    :raises NotImplementedProtocol:
        If the path protocol is not local or ftp

    .. seealso:: FTP LIMITATIONS at this module's doc for performance issues information
    '''
    from six.moves.urllib.parse import urlparse
    directory_url = urlparse(directory)

    if _UrlIsLocal(directory_url):
        return os.path.isdir(directory)
    elif directory_url.scheme == 'ftp':
        from ._exceptions import NotImplementedProtocol
        raise NotImplementedProtocol(target_url.scheme)
    else:
        from ._exceptions import NotImplementedProtocol
        raise NotImplementedProtocol(directory_url.scheme)

Response: Returns True if the given path points to an existent directory.


Instruction:
Make a summary of the following Python 3 code
def Exists(path):
    '''
    :rtype: bool
    :returns:
        True if the path already exists (either a file or a directory)

    .. seealso:: FTP LIMITATIONS at this module's doc for performance issues information
    '''
    from six.moves.urllib.parse import urlparse
    path_url = urlparse(path)

    # Handle local
    if _UrlIsLocal(path_url):
        return IsFile(path) or IsDir(path) or IsLink(path)
    return IsFile(path) or IsDir(path)

Response:
Checks if a path exists in the
   .


Instruction:
Make a summary of the following Python 3 code
def CopyDirectory(source_dir, target_dir, override=False):
    '''
    Recursively copy a directory tree.

    :param unicode source_dir:
        Where files will come from

    :param unicode target_dir:
        Where files will go to

    :param bool override:
        If True and target_dir already exists, it will be deleted before copying.

    :raises NotImplementedForRemotePathError:
        If trying to copy to/from remote directories
    '''
    _AssertIsLocal(source_dir)
    _AssertIsLocal(target_dir)

    if override and IsDir(target_dir):
        DeleteDirectory(target_dir, skip_on_error=False)

    import shutil
    shutil.copytree(source_dir, target_dir)

Response: Recursively copy a directory tree.


Instruction:
How would you code a function in Python 3 to
delete a file from the specified local filename.

Response:
def DeleteFile(target_filename):
    '''
    Deletes the given local filename.

    .. note:: If file doesn't exist this method has no effect.

    :param unicode target_filename:
        A local filename

    :raises NotImplementedForRemotePathError:
        If trying to delete a non-local path

    :raises FileOnlyActionError:
        Raised when filename refers to a directory.
    '''
    _AssertIsLocal(target_filename)

    try:
        if IsLink(target_filename):
            DeleteLink(target_filename)
        elif IsFile(target_filename):
            os.remove(target_filename)
        elif IsDir(target_filename):
            from ._exceptions import FileOnlyActionError
            raise FileOnlyActionError(target_filename)
    except Exception as e:
        reraise(e, 'While executing filesystem.DeleteFile(%s)' % (target_filename))


Instruction:
Make a summary of the following Python 3 code
def AppendToFile(filename, contents, eol_style=EOL_STYLE_NATIVE, encoding=None, binary=False):
    '''
    Appends content to a local file.

    :param unicode filename:

    :param unicode contents:

    :type eol_style: EOL_STYLE_XXX constant
    :param eol_style:
        Replaces the EOL by the appropriate EOL depending on the eol_style value.
        Considers that all content is using only "\n" as EOL.

    :param unicode encoding:
        Target file's content encoding.
        Defaults to sys.getfilesystemencoding()

    :param bool binary:
        If True, content is appended in binary mode. In this case, `contents` must be `bytes` and not
        `unicode`

    :raises NotImplementedForRemotePathError:
        If trying to modify a non-local path

    :raises ValueError:
        If trying to mix unicode `contents` without `encoding`, or `encoding` without
        unicode `contents`
    '''
    _AssertIsLocal(filename)

    assert isinstance(contents, six.text_type) ^ binary, 'Must always receive unicode contents, unless binary=True'

    if not binary:
        # Replaces eol on each line by the given eol_style.
        contents = _HandleContentsEol(contents, eol_style)

        # Handle encoding here, and always write in binary mode. We can't use io.open because it
        # tries to do its own line ending handling.
        contents = contents.encode(encoding or sys.getfilesystemencoding())

    oss = open(filename, 'ab')
    try:
        oss.write(contents)
    finally:
        oss.close()

Response: Append content to a local file.


Instruction:
Create a Python 3 function for
moving a file from source_filename to target_filename.

Response:
def MoveFile(source_filename, target_filename):
    '''
    Moves a file.

    :param unicode source_filename:

    :param unicode target_filename:

    :raises NotImplementedForRemotePathError:
        If trying to operate with non-local files.
    '''
    _AssertIsLocal(source_filename)
    _AssertIsLocal(target_filename)

    import shutil
    shutil.move(source_filename, target_filename)


Instruction:
Create a Python 3 function for
moving a directory. :param unicode source_dir: :param unicode target_dir: :raises NotImplementedError: If trying to move anything other than: Local dir -> local dir FTP dir -> FTP dir (same host)

Response:
def MoveDirectory(source_dir, target_dir):
    '''
    Moves a directory.

    :param unicode source_dir:

    :param unicode target_dir:

    :raises NotImplementedError:
        If trying to move anything other than:
            Local dir -> local dir
            FTP dir -> FTP dir (same host)
    '''
    if not IsDir(source_dir):
        from ._exceptions import DirectoryNotFoundError
        raise DirectoryNotFoundError(source_dir)

    if Exists(target_dir):
        from ._exceptions import DirectoryAlreadyExistsError
        raise DirectoryAlreadyExistsError(target_dir)

    from six.moves.urllib.parse import urlparse
    source_url = urlparse(source_dir)
    target_url = urlparse(target_dir)

    # Local to local
    if _UrlIsLocal(source_url) and _UrlIsLocal(target_url):
        import shutil
        shutil.move(source_dir, target_dir)

    # FTP to FTP
    elif source_url.scheme == 'ftp' and target_url.scheme == 'ftp':
        from ._exceptions import NotImplementedProtocol
        raise NotImplementedProtocol(target_url.scheme)
    else:
        raise NotImplementedError('Can only move directories local->local or ftp->ftp')


Instruction:
Can you tell what is the following Python 3 function doing
def GetFileContents(filename, binary=False, encoding=None, newline=None):
    '''
    Reads a file and returns its contents. Works for both local and remote files.

    :param unicode filename:

    :param bool binary:
        If True returns the file as is, ignore any EOL conversion.

    :param unicode encoding:
        File's encoding. If not None, contents obtained from file will be decoded using this
        `encoding`.

    :param None|''|'\n'|'\r'|'\r\n' newline:
        Controls universal newlines.
        See 'io.open' newline parameter documentation for more details.

    :returns str|unicode:
        The file's contents.
        Returns unicode string when `encoding` is not None.

    .. seealso:: FTP LIMITATIONS at this module's doc for performance issues information
    '''
    source_file = OpenFile(filename, binary=binary, encoding=encoding, newline=newline)
    try:
        contents = source_file.read()
    finally:
        source_file.close()

    return contents

Response: Reads a file and returns its contents.


Instruction:
Create a Python 3 function to
read a file and returns its contents as a list of lines.

Response:
def GetFileLines(filename, newline=None, encoding=None):
    '''
    Reads a file and returns its contents as a list of lines. Works for both local and remote files.

    :param unicode filename:

    :param None|''|'\n'|'\r'|'\r\n' newline:
        Controls universal newlines.
        See 'io.open' newline parameter documentation for more details.

    :param unicode encoding:
        File's encoding. If not None, contents obtained from file will be decoded using this
        `encoding`.

    :returns list(unicode):
        The file's lines

    .. seealso:: FTP LIMITATIONS at this module's doc for performance issues information
    '''
    return GetFileContents(
        filename,
        binary=False,
        encoding=encoding,
        newline=newline,
    ).split('\n')


Instruction:
Write a Python 3 function for
opening a file and returns it.

Response:
def OpenFile(filename, binary=False, newline=None, encoding=None):
    '''
    Open a file and returns it.
    Consider the possibility of a remote file (HTTP, HTTPS, FTP)

    :param unicode filename:
        Local or remote filename.

    :param bool binary:
        If True returns the file as is, ignore any EOL conversion.
        If set ignores univeral_newlines parameter.

    :param None|''|'\n'|'\r'|'\r\n' newline:
        Controls universal newlines.
        See 'io.open' newline parameter documentation for more details.

    :param unicode encoding:
        File's encoding. If not None, contents obtained from file will be decoded using this
        `encoding`.

    :returns file:
        The open file, it must be closed by the caller

    @raise: FileNotFoundError
        When the given filename cannot be found

    .. seealso:: FTP LIMITATIONS at this module's doc for performance issues information
    '''
    from six.moves.urllib.parse import urlparse
    filename_url = urlparse(filename)

    # Check if file is local
    if _UrlIsLocal(filename_url):
        if not os.path.isfile(filename):
            from ._exceptions import FileNotFoundError
            raise FileNotFoundError(filename)

        mode = 'rb' if binary else 'r'
        return io.open(filename, mode, encoding=encoding, newline=newline)

    # Not local
    from ._exceptions import NotImplementedProtocol
    raise NotImplementedProtocol(target_url.scheme)


Instruction:
Can you implement a function in Python 3 that
lists the files in the given directory.

Response:
def ListFiles(directory):
    '''
    Lists the files in the given directory

    :type directory: unicode | unicode
    :param directory:
        A directory or URL

    :rtype: list(unicode) | list(unicode)
    :returns:
        List of filenames/directories found in the given directory.
        Returns None if the given directory does not exists.

        If `directory` is a unicode string, all files returned will also be unicode

    :raises NotImplementedProtocol:
        If file protocol is not local or FTP

    .. seealso:: FTP LIMITATIONS at this module's doc for performance issues information
    '''
    from six.moves.urllib.parse import urlparse
    directory_url = urlparse(directory)

    # Handle local
    if _UrlIsLocal(directory_url):
        if not os.path.isdir(directory):
            return None
        return os.listdir(directory)

    # Handle FTP
    elif directory_url.scheme == 'ftp':
        from ._exceptions import NotImplementedProtocol
        raise NotImplementedProtocol(directory_url.scheme)
    else:
        from ._exceptions import NotImplementedProtocol
        raise NotImplementedProtocol(directory_url.scheme)


Instruction:
Here you have a function in Python 3, explain what it does
def CreateFile(filename, contents, eol_style=EOL_STYLE_NATIVE, create_dir=True, encoding=None, binary=False):
    '''
    Create a file with the given contents.

    :param unicode filename:
        Filename and path to be created.

    :param unicode contents:
        The file contents as a string.

    :type eol_style: EOL_STYLE_XXX constant
    :param eol_style:
        Replaces the EOL by the appropriate EOL depending on the eol_style value.
        Considers that all content is using only "\n" as EOL.

    :param bool create_dir:
        If True, also creates directories needed in filename's path

    :param unicode encoding:
        Target file's content encoding. Defaults to sys.getfilesystemencoding()
        Ignored if `binary` = True

    :param bool binary:
        If True, file is created in binary mode. In this case, `contents` must be `bytes` and not
        `unicode`

    :return unicode:
        Returns the name of the file created.

    :raises NotImplementedProtocol:
        If file protocol is not local or FTP

    :raises ValueError:
        If trying to mix unicode `contents` without `encoding`, or `encoding` without
        unicode `contents`

    .. seealso:: FTP LIMITATIONS at this module's doc for performance issues information
    '''
    # Lots of checks when writing binary files
    if binary:
        if isinstance(contents, six.text_type):
            raise TypeError('contents must be str (bytes) when binary=True')
    else:
        if not isinstance(contents, six.text_type):
            raise TypeError('contents must be unicode when binary=False')

        # Replaces eol on each line by the given eol_style.
        contents = _HandleContentsEol(contents, eol_style)

        # Encode string and pretend we are using binary to prevent 'open' from automatically
        # changing Eols
        encoding = encoding or sys.getfilesystemencoding()
        contents = contents.encode(encoding)
        binary = True

    # If asked, creates directory containing file
    if create_dir:
        dirname = os.path.dirname(filename)
        if dirname:
            CreateDirectory(dirname)

    from six.moves.urllib.parse import urlparse
    filename_url = urlparse(filename)

    # Handle local
    if _UrlIsLocal(filename_url):
        # Always writing as binary (see handling above)
        with open(filename, 'wb') as oss:
            oss.write(contents)

    # Handle FTP
    elif filename_url.scheme == 'ftp':
        # Always writing as binary (see handling above)
        from ._exceptions import NotImplementedProtocol
        raise NotImplementedProtocol(directory_url.scheme)
    else:
        from ._exceptions import NotImplementedProtocol
        raise NotImplementedProtocol(filename_url.scheme)

    return filename

Response: Creates a file with the given contents.


Instruction:
Create a Python 3 function for
replacing all occurrences of old by new in the given file.

Response:
def ReplaceInFile(filename, old, new, encoding=None):
    '''
    Replaces all occurrences of "old" by "new" in the given file.

    :param unicode filename:
        The name of the file.

    :param unicode old:
        The string to search for.

    :param unicode new:
        Replacement string.

    :return unicode:
        The new contents of the file.
    '''
    contents = GetFileContents(filename, encoding=encoding)
    contents = contents.replace(old, new)
    CreateFile(filename, contents, encoding=encoding)
    return contents


Instruction:
Implement a Python 3 function for
creating a directory including any missing intermediate directories.

Response:
def CreateDirectory(directory):
    '''
    Create directory including any missing intermediate directory.

    :param unicode directory:

    :return unicode|urlparse.ParseResult:
        Returns the created directory or url (see urlparse).

    :raises NotImplementedProtocol:
        If protocol is not local or FTP.

    .. seealso:: FTP LIMITATIONS at this module's doc for performance issues information
    '''
    from six.moves.urllib.parse import urlparse
    directory_url = urlparse(directory)

    # Handle local
    if _UrlIsLocal(directory_url):
        if not os.path.exists(directory):
            os.makedirs(directory)
        return directory

    # Handle FTP
    elif directory_url.scheme == 'ftp':
        from ._exceptions import NotImplementedProtocol
        raise NotImplementedProtocol(directory_url.scheme)
    else:
        from ._exceptions import NotImplementedProtocol
        raise NotImplementedProtocol(directory_url.scheme)


Instruction:
Here you have a function in Python 3, explain what it does
def DeleteDirectory(directory, skip_on_error=False):
    '''
    Deletes a directory.

    :param unicode directory:

    :param bool skip_on_error:
        If True, ignore any errors when trying to delete directory (for example, directory not
        found)

    :raises NotImplementedForRemotePathError:
        If trying to delete a remote directory.
    '''
    _AssertIsLocal(directory)

    import shutil
    def OnError(fn, path, excinfo):
        '''
        Remove the read-only flag and try to remove again.
        On Windows, rmtree fails when trying to remove a read-only file. This fix it!
        Another case: Read-only directories return True in os.access test. It seems that read-only
        directories has it own flag (looking at the property windows on Explorer).
        '''
        if IsLink(path):
            return

        if fn is os.remove and os.access(path, os.W_OK):
            raise

        # Make the file WRITEABLE and executes the original delete function (osfunc)
        import stat
        os.chmod(path, stat.S_IWRITE)
        fn(path)

    try:
        if not os.path.isdir(directory):
            if skip_on_error:
                return
            from ._exceptions import DirectoryNotFoundError
            raise DirectoryNotFoundError(directory)
        shutil.rmtree(directory, onerror=OnError)
    except:
        if not skip_on_error:
            raise

Response:
Deletes a directory.

    :param unicode directory:

    :param bool skip_on_error:
        If True, ignore any errors when trying to delete directory (for example, directory not
        found)

    :raises NotImplementedForRemotePathError:
        If trying to delete a remote directory.


Instruction:
Given the following Python 3 function, write the documentation
def GetMTime(path):
    '''
    :param unicode path:
        Path to file or directory

    :rtype: float
    :returns:
        Modification time for path.

        If this is a directory, the highest mtime from files inside it will be returned.

    @note:
        In some Linux distros (such as CentOs, or anything with ext3), mtime will not return a value
        with resolutions higher than a second.

        http://stackoverflow.com/questions/2428556/os-path-getmtime-doesnt-return-fraction-of-a-second
    '''
    _AssertIsLocal(path)

    if os.path.isdir(path):
        files = FindFiles(path)

        if len(files) > 0:
            return max(map(os.path.getmtime, files))

    return os.path.getmtime(path)

Response: Get the modification time of a file or directory.


Instruction:
Explain what the following Python 3 code does
def ListMappedNetworkDrives():
    '''
    On Windows, returns a list of mapped network drives

    :return: tuple(string, string, bool)
        For each mapped netword drive, return 3 values tuple:
            - the local drive
            - the remote path-
            - True if the mapping is enabled (warning: not reliable)
    '''
    if sys.platform != 'win32':
        raise NotImplementedError
    drives_list = []
    netuse = _CallWindowsNetCommand(['use'])
    for line in netuse.split(EOL_STYLE_WINDOWS):
        match = re.match("(\w*)\s+(\w:)\s+(.+)", line.rstrip())
        if match:
            drives_list.append((match.group(2), match.group(3), match.group(1) == 'OK'))
    return drives_list

Response: Returns a list of mapped network drives on Windows.


Instruction:
Can you implement a function in Python 3 that
creates a symbolic link at link_path pointing to target_path.

Response:
def CreateLink(target_path, link_path, override=True):
    '''
    Create a symbolic link at `link_path` pointing to `target_path`.

    :param unicode target_path:
        Link target

    :param unicode link_path:
        Fullpath to link name

    :param bool override:
        If True and `link_path` already exists as a link, that link is overridden.
    '''
    _AssertIsLocal(target_path)
    _AssertIsLocal(link_path)

    if override and IsLink(link_path):
        DeleteLink(link_path)

    # Create directories leading up to link
    dirname = os.path.dirname(link_path)
    if dirname:
        CreateDirectory(dirname)

    if sys.platform != 'win32':
        return os.symlink(target_path, link_path)  # @UndefinedVariable
    else:
        #import ntfsutils.junction
        #return ntfsutils.junction.create(target_path, link_path)

        import jaraco.windows.filesystem
        return jaraco.windows.filesystem.symlink(target_path, link_path)

        from ._easyfs_win32 import CreateSymbolicLink
        try:
            dw_flags = 0
            if target_path and os.path.isdir(target_path):
                dw_flags = 1
            return CreateSymbolicLink(target_path, link_path, dw_flags)
        except Exception as e:
            reraise(e, 'Creating link "%(link_path)s" pointing to "%(target_path)s"' % locals())


Instruction:
Can you generate the documentation for the following Python 3 function
def IsLink(path):
    '''
    :param unicode path:
        Path being tested

    :returns bool:
        True if `path` is a link
    '''
    _AssertIsLocal(path)

    if sys.platform != 'win32':
        return os.path.islink(path)

    import jaraco.windows.filesystem
    return jaraco.windows.filesystem.islink(path)

Response: Returns True if path is a link.


Instruction:
How would you code a function in Python 3 to
read the target of the symbolic link at path.

Response:
def ReadLink(path):
    '''
    Read the target of the symbolic link at `path`.

    :param unicode path:
        Path to a symbolic link

    :returns unicode:
        Target of a symbolic link
    '''
    _AssertIsLocal(path)

    if sys.platform != 'win32':
        return os.readlink(path)  # @UndefinedVariable

    if not IsLink(path):
        from ._exceptions import FileNotFoundError
        raise FileNotFoundError(path)

    import jaraco.windows.filesystem
    result = jaraco.windows.filesystem.readlink(path)
    if '\\??\\' in result:
        result = result.split('\\??\\')[1]
    return result


Instruction:
Can you generate the documentation for the following Python 3 function
def _AssertIsLocal(path):
    '''
    Checks if a given path is local, raise an exception if not.

    This is used in filesystem functions that do not support remote operations yet.

    :param unicode path:

    :raises NotImplementedForRemotePathError:
        If the given path is not local
    '''
    from six.moves.urllib.parse import urlparse
    if not _UrlIsLocal(urlparse(path)):
        from ._exceptions import NotImplementedForRemotePathError
        raise NotImplementedForRemotePathError

Response: Checks if a given path is local raises an exception if not.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _HandleContentsEol(contents, eol_style):
    '''
    Replaces eol on each line by the given eol_style.

    :param unicode contents:
    :type eol_style: EOL_STYLE_XXX constant
    :param eol_style:
    '''
    if eol_style == EOL_STYLE_NONE:
        return contents

    if eol_style == EOL_STYLE_UNIX:
        return contents.replace('\r\n', eol_style).replace('\r', eol_style)

    if eol_style == EOL_STYLE_MAC:
        return contents.replace('\r\n', eol_style).replace('\n', eol_style)

    if eol_style == EOL_STYLE_WINDOWS:
        return contents.replace('\r\n', '\n').replace('\r', '\n').replace('\n', EOL_STYLE_WINDOWS)

    raise ValueError('Unexpected eol style: %r' % (eol_style,))

Response: Handles the contents of a single line.


Instruction:
Write a Python 3 script to
call Windows NET command.

Response:
def _CallWindowsNetCommand(parameters):
    '''
    Call Windows NET command, used to acquire/configure network services settings.

    :param parameters: list of command line parameters

    :return: command output
    '''
    import subprocess
    popen = subprocess.Popen(["net"] + parameters, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    stdoutdata, stderrdata = popen.communicate()
    if stderrdata:
        raise OSError("Failed on call net.exe: %s" % stderrdata)
    return stdoutdata


Instruction:
Can you create a Python 3 function that
checks if the given source filename should be re - generated because the target filename has changed.

Response:
def CheckForUpdate(source, target):
    '''
    Checks if the given target filename should be re-generated because the source has changed.
    :param source: the source filename.
    :param target: the target filename.
    :return bool:
        True if the target is out-dated, False otherwise.
    '''
    return \
        not os.path.isfile(target) or \
        os.path.getmtime(source) > os.path.getmtime(target)


Instruction:
Make a summary of the following Python 3 code
def MatchMasks(filename, masks):
    '''
    Verifies if a filename match with given patterns.

    :param str filename: The filename to match.
    :param list(str) masks: The patterns to search in the filename.
    :return bool:
        True if the filename has matched with one pattern, False otherwise.
    '''
    import fnmatch

    if not isinstance(masks, (list, tuple)):
        masks = [masks]

    for i_mask in masks:
        if fnmatch.fnmatch(filename, i_mask):
            return True
    return False

Response: Tests if a filename matches with a list of masks.


Instruction:
Can you tell what is the following Python 3 function doing
def FindFiles(dir_, in_filters=None, out_filters=None, recursive=True, include_root_dir=True, standard_paths=False):
    '''
    Searches for files in a given directory that match with the given patterns.

    :param str dir_: the directory root, to search the files.
    :param list(str) in_filters: a list with patterns to match (default = all). E.g.: ['*.py']
    :param list(str) out_filters: a list with patterns to ignore (default = none). E.g.: ['*.py']
    :param bool recursive: if True search in subdirectories, otherwise, just in the root.
    :param bool include_root_dir: if True, includes the directory being searched in the returned paths
    :param bool standard_paths: if True, always uses unix path separators "/"
    :return list(str):
        A list of strings with the files that matched (with the full path in the filesystem).
    '''
    # all files
    if in_filters is None:
        in_filters = ['*']

    if out_filters is None:
        out_filters = []

    result = []

    # maintain just files that don't have a pattern that match with out_filters
    # walk through all directories based on dir
    for dir_root, directories, filenames in os.walk(dir_):

        for i_directory in directories[:]:
            if MatchMasks(i_directory, out_filters):
                directories.remove(i_directory)

        for filename in directories + filenames:
            if MatchMasks(filename, in_filters) and not MatchMasks(filename, out_filters):
                result.append(os.path.join(dir_root, filename))

        if not recursive:
            break

    if not include_root_dir:
        # Remove root dir from all paths
        dir_prefix = len(dir_) + 1
        result = [file[dir_prefix:] for file in result]

    if standard_paths:
        result = map(StandardizePath, result)

    return result

Response: Searches for files in a given directory that match with the given patterns.


Instruction:
Can you generate the documentation for the following Python 3 function
def DumpDirHashToStringIO(directory, stringio, base='', exclude=None, include=None):
    '''
    Helper to iterate over the files in a directory putting those in the passed StringIO in ini
    format.

    :param unicode directory:
        The directory for which the hash should be done.

    :param StringIO stringio:
        The string to which the dump should be put.

    :param unicode base:
        If provided should be added (along with a '/') before the name=hash of file.

    :param unicode exclude:
        Pattern to match files to exclude from the hashing. E.g.: *.gz

    :param unicode include:
        Pattern to match files to include in the hashing. E.g.: *.zip
    '''
    import fnmatch
    import os

    files = [(os.path.join(directory, i), i) for i in os.listdir(directory)]
    files = [i for i in files if os.path.isfile(i[0])]
    for fullname, filename in files:
        if include is not None:
            if not fnmatch.fnmatch(fullname, include):
                continue

        if exclude is not None:
            if fnmatch.fnmatch(fullname, exclude):
                continue

        md5 = Md5Hex(fullname)
        if base:
            stringio.write('%s/%s=%s\n' % (base, filename, md5))
        else:
            stringio.write('%s=%s\n' % (filename, md5))

Response: Dump the contents of a directory into a stringio.


Instruction:
How would you implement a function in Python 3 that
calculates the md5 of the contents of the file.

Response:
def Md5Hex(filename=None, contents=None):
    '''
    :param unicode filename:
        The file from which the md5 should be calculated. If the filename is given, the contents
        should NOT be given.

    :param unicode contents:
        The contents for which the md5 should be calculated. If the contents are given, the filename
        should NOT be given.

    :rtype: unicode
    :returns:
        Returns a string with the hex digest of the stream.
    '''
    import io
    import hashlib
    md5 = hashlib.md5()

    if filename:
        stream = io.open(filename, 'rb')
        try:
            while True:
                data = stream.read(md5.block_size * 128)
                if not data:
                    break
                md5.update(data)
        finally:
            stream.close()

    else:
        md5.update(contents)

    return six.text_type(md5.hexdigest())


Instruction:
How would you code a function in Python 3 to
return an iterator that yields random hexadecimal hashes.

Response:
def IterHashes(iterator_size, hash_length=7):
    '''
    Iterator for random hexadecimal hashes

    :param iterator_size:
        Amount of hashes return before this iterator stops.
        Goes on forever if `iterator_size` is negative.

    :param int hash_length:
        Size of each hash returned.

    :return generator(unicode):
    '''
    if not isinstance(iterator_size, int):
        raise TypeError('iterator_size must be integer.')

    count = 0
    while count != iterator_size:
        count += 1
        yield GetRandomHash(hash_length)


Instruction:
Can you tell what is the following Python 3 function doing
def PushPopItem(obj, key, value):
    '''
    A context manager to replace and restore a value using a getter and setter.

    :param object obj: The object to replace/restore.
    :param object key: The key to replace/restore in the object.
    :param object value: The value to replace.

    Example::

      with PushPop2(sys.modules, 'alpha', None):
        pytest.raises(ImportError):
          import alpha
    '''
    if key in obj:
        old_value = obj[key]
        obj[key] = value
        yield value
        obj[key] = old_value

    else:
        obj[key] = value
        yield value
        del obj[key]

Response: A context manager that replaces and restore a value using a getter and setter.


Instruction:
How would you explain what the following Python 3 function does
def Split(cls, extended_path_mask):
        '''
        Splits the given path into their components: recursive, dirname, in_filters and out_filters

        :param str: extended_path_mask:
            The "extended path mask" to split

        :rtype: tuple(bool,bool,str,list(str),list(str))
        :returns:
            Returns the extended path 5 components:
            - The tree-recurse flag
            - The flat-recurse flag
            - The actual path
            - A list of masks to include
            - A list of masks to exclude
        '''
        import os.path
        r_tree_recurse = extended_path_mask[0] in '+-'
        r_flat_recurse = extended_path_mask[0] in '-'

        r_dirname, r_filters = os.path.split(extended_path_mask)
        if r_tree_recurse:
            r_dirname = r_dirname[1:]

        filters = r_filters.split(';')
        r_in_filters = [i for i in filters if not i.startswith('!')]
        r_out_filters = [i[1:] for i in filters if i.startswith('!')]

        return r_tree_recurse, r_flat_recurse, r_dirname, r_in_filters, r_out_filters

Response: Splits the given path into its components recursive dirname in_filters and out_filters.


Instruction:
Explain what the following Python 3 code does
def GetMessages(self, files):
    result = {}
    for file_name in files:
      file_desc = self.pool.FindFileByName(file_name)
      for name, msg in file_desc.message_types_by_name.items():
        if file_desc.package:
          full_name = '.'.join([file_desc.package, name])
        else:
          full_name = msg.name
        result[full_name] = self.GetPrototype(
            self.pool.FindMessageTypeByName(full_name))

      # While the extension FieldDescriptors are created by the descriptor pool,
      # the python classes created in the factory need them to be registered
      # explicitly, which is done below.
      #
      # The call to RegisterExtension will specifically check if the
      # extension was already registered on the object and either
      # ignore the registration if the original was the same, or raise
      # an error if they were different.

      for name, extension in file_desc.extensions_by_name.items():
        if extension.containing_type.full_name not in self._classes:
          self.GetPrototype(extension.containing_type)
        extended_class = self._classes[extension.containing_type.full_name]
        extended_class.RegisterExtension(extension)
    return result

Response: Gets all the messages from a specified file.


Instruction:
Can you tell what is the following Python 3 function doing
def specifier_to_db(db_spec):
    local_match = LOCAL_RE.match(db_spec)
    remote_match = REMOTE_RE.match(db_spec)
    plain_match = PLAIN_RE.match(db_spec)
    # If this looks like a local specifier:
    if local_match:
        return local_match.groupdict()['database']
    # If this looks like a remote specifier:
    elif remote_match:
        # A fancy 'unpacking'...
        hostname, portnum, database = map(remote_match.groupdict().get,
            ('hostname', 'portnum', 'database'))
        local_url = settings._('COUCHDB_SERVER', 'http://127.0.0.1:5984/')
        localhost, localport = urlparse.urlparse(local_url)[1].split(':')
        # If it's local, return a local DB string.
        if (localhost == hostname) and (localport == portnum):
            return database
        # Otherwise, get a remote URL.
        return 'http://%s:%s/%s' % (hostname, portnum, database)
    # If this looks like a plain database name, return it.
    elif plain_match:
        return plain_match.groupdict()['database']
    # Throw a wobbly.
    raise ValueError('Invalid database spec: %r' % (db_spec,))

Response: Converts a database specifier into a database string.


Instruction:
Create a Python 3 function to
convert a database string to a database specifier.

Response:
def db_to_specifier(db_string):
    """
    Return the database specifier for a database string.

    This accepts a database name or URL, and returns a database specifier in the
    format accepted by ``specifier_to_db``. It is recommended that you consult
    the documentation for that function for an explanation of the format.
    """
    local_match = PLAIN_RE.match(db_string)
    remote_match = URL_RE.match(db_string)
    # If this looks like a local specifier:
    if local_match:
        return 'local:' + local_match.groupdict()['database']
    # If this looks like a remote specifier:
    elif remote_match:
        # Just a fancy way of getting 3 variables in 2 lines...
        hostname, portnum, database = map(remote_match.groupdict().get,
            ('hostname', 'portnum', 'database'))
        local_url = settings._('COUCHDB_SERVER', 'http://127.0.0.1:5984/')
        localhost, localport = urlparse.urlparse(local_url)[1].split(':')
        # If it's the local server, then return a local specifier.
        if (localhost == hostname) and (localport == portnum):
            return 'local:' + database
        # Otherwise, prepare and return the remote specifier.
        return 'remote:%s:%s:%s' % (hostname, portnum, database)
    # Throw a wobbly.
    raise ValueError('Invalid database string: %r' % (db_string,))


Instruction:
Write a Python 3 script for
returning a CouchDB database instance from a database string.

Response:
def get_db_from_db(db_string):
    """Return a CouchDB database instance from a database string."""
    server = get_server_from_db(db_string)
    local_match = PLAIN_RE.match(db_string)
    remote_match = URL_RE.match(db_string)
    # If this looks like a local specifier:
    if local_match:
        return server[local_match.groupdict()['database']]
    elif remote_match:
        return server[remote_match.groupdict()['database']]
    raise ValueError('Invalid database string: %r' % (db_string,))


Instruction:
How would you implement a function in Python 3 that
makes sure a DB specifier exists creating it if necessary.

Response:
def ensure_specifier_exists(db_spec):
    """Make sure a DB specifier exists, creating it if necessary."""
    local_match = LOCAL_RE.match(db_spec)
    remote_match = REMOTE_RE.match(db_spec)
    plain_match = PLAIN_RE.match(db_spec)
    if local_match:
        db_name = local_match.groupdict().get('database')
        server = shortcuts.get_server()
        if db_name not in server:
            server.create(db_name)
        return True
    elif remote_match:
        hostname, portnum, database = map(remote_match.groupdict().get,
            ('hostname', 'portnum', 'database'))
        server = shortcuts.get_server(
            server_url=('http://%s:%s' % (hostname, portnum)))
        if database not in server:
            server.create(database)
        return True
    elif plain_match:
        db_name = plain_match.groupdict().get('database')
        server = shortcuts.get_server()
        if db_name not in server:
            server.create(db_name)
        return True
    return False


Instruction:
Given the following Python 3 function, write the documentation
def coerce(value1, value2, default=None):
    if value1 is not NoSet:
        return value1
    elif value2 is not NoSet:
        return value2
    else:
        return default

Response: Coerce two values into a base64 encoded object.


Instruction:
Explain what the following Python 3 code does
def get_events_vote_cluster(self, delegate_address):
        ''' Returns all transactions and forged blocks by voters clustered around a single delegate_address'''

        delegate_pubkey = self.account_details(address=delegate_address)['public_key']

        plusvote = '+{delegate_pubkey}'.format(delegate_pubkey=delegate_pubkey)

        resultset = self._cursor.execute_and_fetchall("""
            SELECT *
             FROM (
            SELECT
            trs."{transactions[id]}" AS a,
            'transaction' AS b,
            trs."{transactions[amount]}" AS c,
            trs."{transactions[timestamp]}" AS d,
            trs."{transactions[recipient_id]}" AS e,
            trs."{transactions[sender_id]}" AS f,
            trs."{transactions[rawasset]}" AS g,
            trs."{transactions[type]}" AS h,
            trs."{transactions[fee]}" AS i,
            trs."{transactions[block_id]}" AS j,
            blocks."{blocks[height]}" AS k
            FROM {transactions[table]} AS trs
            INNER JOIN {blocks[table]} AS blocks
                 ON (blocks."{blocks[id]}" = trs."{transactions[block_id]}")
            WHERE trs."{transactions[sender_id]}" IN
              (SELECT trs."{transactions[sender_id]}"
               FROM {transactions[table]} AS trs, {votes[table]} AS votes
               WHERE trs."{transactions[id]}" = votes."{votes[transaction_id]}"
               AND votes."{votes[votes]}" = '{plusvote}')
            OR trs."{transactions[recipient_id]}" IN
              (SELECT trs."{transactions[sender_id]}"
               FROM {transactions[table]} AS trs, {votes[table]} AS votes
               WHERE trs."{transactions[id]}" = votes."{votes[transaction_id]}"
               AND votes."{votes[votes]}" = '{plusvote}')
            UNION
            SELECT
            blocks."{blocks[id]}" AS a,
            'block' AS b,
            blocks."{blocks[reward]}"as c,
            blocks."{blocks[total_fee]}" AS d,
            ENCODE(mem."{mem_accounts[public_key]}"::BYTEA, 'hex') AS e,
            mem."{mem_accounts[address]}" AS f,
            mem."{mem_accounts[username]}" AS g,
            NULL AS h,
            blocks."{blocks[timestamp]}" AS i,
            NULL AS j,
            blocks."{blocks[height]}" AS k
            FROM blocks
              INNER JOIN {mem_accounts[table]} AS mem
              ON (mem."{mem_accounts[public_key]}" = blocks."{blocks[generator_public_key]}")
            WHERE
            blocks."{blocks[generator_public_key]}" IN (
                    SELECT mem2."{mem_accounts[public_key]}"
                    FROM {mem_accounts[table]} mem2
                    WHERE mem2."{mem_accounts[address]}" IN
                    (SELECT trs."{transactions[sender_id]}"
                     FROM {transactions[table]} AS trs, {votes[table]} AS votes
                     WHERE trs."{transactions[id]}" = votes."{votes[transaction_id]}"
                     AND votes."{votes[votes]}" = '{plusvote}')
               )) total

            ORDER BY total.k ASC;""".format(
                address=delegate_address,
                transactions=self.scheme['transactions'],
                blocks=self.scheme['blocks'],
                mem_accounts=self.scheme['mem_accounts'],
                mem_accounts2delegates=self.scheme['mem_accounts2delegates'],
                votes=self.scheme['votes'],
                plusvote=plusvote))
        res = {}
        for i in resultset:

            if i[1] == 'transaction':
                res.update({i[0]: {
                   'tx_id': i[0],
                   'event_type': i[1],
                   'amount': i[2],
                   'timestamp': i[3],
                   'recipient_id': i[4],
                   'sender_id': i[5],
                   'rawasset': i[6],
                   'type': i[7],
                   'fee': i[8],
                   'block_id': i[9],
                   'height': i[10]
                }})

            elif i[1] == 'block':
                res.update({i[0]: {
                    'block_id': i[0],
                    'event_type': i[1],
                    'reward': i[2],
                    'total_fee': i[3],
                    'timestamp': i[8],
                    'address': i[5],
                    'username': i[6],
                    'public_key': i[4],
                    'height': i[10]
                }})

        return res

Response: Returns all transactions and forged blocks by voters clustered around a single delegate_address


Instruction:
Here you have a function in Python 3, explain what it does
def classical(transform, loglikelihood, parameter_names, prior,
	start = 0.5, ftol=0.1, disp=0, nsteps=40000,
	method='neldermead', **args):
	"""
	**Classic optimization methods**

	:param start: start position vector (before transform)
	:param ftol: accuracy required to stop at optimum
	:param disp: verbosity
	:param nsteps: number of steps
	:param method: string
		neldermead, cobyla (via `scipy.optimize <http://docs.scipy.org/doc/scipy/reference/tutorial/optimize.html>`_)
		bobyqa, ralg, algencan, ipopt, mma, auglag and many others from the OpenOpt framework (via `openopt.NLP <http://openopt.org/NLP>`_)
		minuit (via `PyMinuit <https://code.google.com/p/pyminuit/>`_)
	"""
	import scipy.optimize

	n_params = len(parameter_names)

	def minfunc(params):
		l = loglikelihood(params)
		p = prior(params)
		if numpy.isinf(p) and p < 0:
			print '    prior rejection'
			return -1e300
		if numpy.isnan(l):
			return -1e300
		return -l - p
	def minfunc_cube(cube):
		cube = numpy.array(cube)
		if (cube <= 1e-10).any() or (cube >= 1-1e-10).any():
			return 1e100
		params = transform(cube)
		l = loglikelihood(params)
		p = prior(params)
		if numpy.isinf(p) and p < 0:
			print '    prior rejection'
			return -1e300
		if numpy.isnan(l):
			return -1e300
		return -l - p

	start = start + numpy.zeros(n_params)
	ret = {}
	if method == 'neldermead':
		final, value, _niter, neval, warnflag = scipy.optimize.fmin(minfunc_cube, start, ftol=ftol, disp=disp, maxfun=nsteps, full_output=True)
	elif method == 'cobyla':
		cons =  [lambda params: params[i]     for i in range(n_params)]
		cons += [lambda params: 1 - params[i] for i in range(n_params)]
		final = scipy.optimize.fmin_cobyla(minfunc_cube, start, cons, rhoend=ftol / 10, disp=disp, maxfun=nsteps)
		neval = nsteps
	elif method == 'minuit' or method == 'hesse':
		"""
		We use eval here, and it is a dangerous thing to do.
		But Minuit probes the objective function for parameter names,
		and there is no way to create the objective function
		dynamically with an unknown number of parameters other than
		through eval.
		"""
		s = ', '.join(parameter_names)
		s = """lambda %s: minfunc([%s])""" % (s, s)
		if method == 'hesse':
			f = eval(s, dict(minfunc=minfunc, numpy=numpy))
			start = transform(start)
		else:
			f = eval(s, dict(minfunc=minfunc_cube, numpy=numpy))
		import minuit
		m = minuit.Minuit(f)
		for i, p in enumerate(parameter_names):
			m.values[p] = start[i]
			if method == 'minuit':
				m.limits[p] = (1e-10, 1 - 1e-10)
		m.up = 0.5
		m.tol = ftol * 100
		m.printMode = disp
		if method == 'minuit':
			m.migrad()
		elif method == 'hesse':
			m.hesse()
		final = [m.values[p] for p in parameter_names]
		neval = m.ncalls
		errors = [m.errors[p] for p in parameter_names]

		if method == 'minuit':
			c0 = final
			p0 = transform(c0)
			stdev = numpy.zeros(n_params)
			lower = numpy.zeros(n_params)
			upper = numpy.zeros(n_params)
			for i, w in enumerate(errors):
				c1 = numpy.copy(c0)
				c1[i] -= w
				c2 = numpy.copy(c0)
				c2[i] += w
				p1 = transform(c1)
				p2 = transform(c2)
				stdev[i] = numpy.abs(p2[i] - p1[i]) / 2
				lower[i] = min(p2[i], p1[i])
				upper[i] = max(p2[i], p1[i])
			ret['stdev'] = stdev
			ret['upper'] = upper
			ret['lower'] = lower
		elif method == 'hesse':
			ret['stdev'] = errors
			ret['cov'] = numpy.matrix([[m.covariance[(a, b)] for b in parameter_names] for a in parameter_names])

	else:
		from openopt import NLP
		lo = [1e-10] * n_params
		hi = [1-1e-10] * n_params
		iprint = 0 if disp == 0 else 10 if disp == 1 else 1
		p = NLP(f=minfunc_cube, x0=start, lb=lo, ub=hi,
			maxFunEvals=nsteps, ftol=ftol, iprint=iprint)
		r = p.solve(method)
		final = r.xf
		neval = r.evals['f']

	ret.update(dict(start=final, maximum=transform(final), method=method, neval=neval))
	return ret

Response: Classical optimization for the current state of the model.


Instruction:
Write a Python 3 script for
parsing a hub key into a dictionary of component parts

Response:
def parse_hub_key(key):
    """Parse a hub key into a dictionary of component parts

    :param key: str, a hub key
    :returns: dict, hub key split into parts
    :raises: ValueError
    """
    if key is None:
        raise ValueError('Not a valid key')

    match = re.match(PATTERN, key)
    if not match:
        match = re.match(PATTERN_S0, key)
        if not match:
            raise ValueError('Not a valid key')

        return dict(map(normalise_part, zip([p for p in PARTS_S0.keys()], match.groups())))

    return dict(zip(PARTS.keys(), match.groups()))


Instruction:
Explain what the following Python 3 code does
def match_part(string, part):
    if not string or not re.match('^(' + PARTS[part] + ')$', string):
        raise ValueError('{} should match {}'.format(part, PARTS[part]))

Response: Raise an exception if string doesn t match a part s regex