Instruction:
Can you generate the documentation for the following Python 3 function
def _findPoint(self, name, force_read=True):
        for point in self.points:
            if point.properties.name == name:
                if force_read:
                    point.value
                return point
        raise ValueError("{} doesn't exist in controller".format(name))

Response: Find a single point in the list of points with the given name.


Instruction:
Can you generate the documentation for the following Python 3 function
def connect(self, *, db=None):
        if not self.properties.network:
            self.new_state(DeviceFromDB)
        else:
            try:
                name = self.properties.network.read(
                    "{} device {} objectName".format(
                        self.properties.address, self.properties.device_id
                    )
                )

                segmentation = self.properties.network.read(
                    "{} device {} segmentationSupported".format(
                        self.properties.address, self.properties.device_id
                    )
                )

                if not self.segmentation_supported or segmentation not in (
                    "segmentedTransmit",
                    "segmentedBoth",
                ):
                    segmentation_supported = False
                    self._log.debug("Segmentation not supported")
                else:
                    segmentation_supported = True

                if name:
                    if segmentation_supported:
                        self.new_state(RPMDeviceConnected)
                    else:
                        self.new_state(RPDeviceConnected)

            except SegmentationNotSupported:
                self.segmentation_supported = False
                self._log.warning(
                    "Segmentation not supported.... expect slow responses."
                )
                self.new_state(RPDeviceConnected)

            except (NoResponseFromController, AttributeError) as error:
                if self.properties.db_name:
                    self.new_state(DeviceFromDB)
                else:
                    self._log.warning(
                        "Offline: provide database name to load stored data."
                    )
                    self._log.warning("Ex. controller.connect(db = 'backup')")

Response: Attempts to connect to a controller database. If unable attempts to connect to a controller database. If unable attempts to connect to a controller database. If unable attempts to connect to a controller database. If unable attempts to connect to a controller database. If unable attempts to connect to a controller database.


Instruction:
Given the following Python 3 function, write the documentation
def connect(self, *, network=None, from_backup=None):
        if network and from_backup:
            raise WrongParameter("Please provide network OR from_backup")

        elif network:
            self.properties.network = network
            try:
                name = self.properties.network.read(
                    "{} device {} objectName".format(
                        self.properties.address, self.properties.device_id
                    )
                )

                segmentation = self.properties.network.read(
                    "{} device {} segmentationSupported".format(
                        self.properties.address, self.properties.device_id
                    )
                )

                if not self.segmentation_supported or segmentation not in (
                    "segmentedTransmit",
                    "segmentedBoth",
                ):
                    segmentation_supported = False
                    self._log.debug("Segmentation not supported")
                else:
                    segmentation_supported = True

                if name:
                    if segmentation_supported:
                        self.new_state(RPMDeviceConnected)
                    else:
                        self.new_state(RPDeviceConnected)
                    # self.db.close()

            except NoResponseFromController:
                self._log.error("Unable to connect, keeping DB mode active")

        elif from_backup:
            self.properties.db_name = from_backup.split(".")[0]
            self._init_state()

Response: Connect to a specific device.


Instruction:
Can you implement a function in Python 3 that
updates the log level of all loggers.

Response:
def update_log_level(level=None, *, file=None, stderr=None, stdout=None):
    """
    Typical usage :
        Normal
        BAC0.log_level(file='warning', stdout='warning', stderr='error')
        Info on console....but not in file
        BAC0.log_level(file='warning', stdout='info', stderr='error')
        Debug
        BAC0.log_level(file='debug', stdout='info', stderr='error')
    """
    if level:
        file = level
        stderr = level
        stdout = level
    file = convert_level(file)
    stderr = convert_level(stderr)
    stdout = convert_level(stdout)
    BAC0_logger = logging.getLogger("BAC0")
    #    if console:
    #        BAC0_logger.setLevel(console)
    #        BAC0_logger.warning('Changed log level of console to {}'.format(logging.getLevelName(level)))

    for handler in BAC0_logger.handlers:
        if file and handler.get_name() == "file_handler":
            handler.setLevel(file)
            BAC0_logger.info(
                "Changed log level of file to {}".format(logging.getLevelName(file))
            )
        elif stdout and handler.get_name() == "stdout":
            handler.setLevel(stdout)
            BAC0_logger.info(
                "Changed log level of console stdout to {}".format(
                    logging.getLevelName(stdout)
                )
            )
        elif stderr and handler.get_name() == "stderr":
            handler.setLevel(stderr)
            BAC0_logger.info(
                "Changed log level of console stderr to {}".format(
                    logging.getLevelName(stderr)
                )
            )


Instruction:
Can you generate the documentation for the following Python 3 function
def note_and_log(cls):
    if hasattr(cls, "DEBUG_LEVEL"):
        if cls.DEBUG_LEVEL == "debug":
            file_level = logging.DEBUG
            console_level = logging.DEBUG
        elif cls.DEBUG_LEVEL == "info":
            file_level = logging.INFO
            console_level = logging.INFO
    else:
        file_level = logging.WARNING
        console_level = logging.INFO
    # Notes object
    cls._notes = namedtuple("_notes", ["timestamp", "notes"])
    cls._notes.timestamp = []
    cls._notes.notes = []

    # Defining log object
    cls.logname = "{} | {}".format(cls.__module__, cls.__name__)
    root_logger = logging.getLogger()
    cls._log = logging.getLogger("BAC0")
    if not len(root_logger.handlers):
        root_logger.addHandler(cls._log)

    # Console Handler
    ch = logging.StreamHandler()
    ch.set_name("stderr")
    ch2 = logging.StreamHandler(sys.stdout)
    ch2.set_name("stdout")
    ch.setLevel(console_level)
    ch2.setLevel(logging.CRITICAL)

    formatter = logging.Formatter("{asctime} - {levelname:<8}| {message}", style="{")

    # File Handler
    _PERMISSION_TO_WRITE = True
    logUserPath = expanduser("~")
    logSaveFilePath = join(logUserPath, ".BAC0")

    logFile = join(logSaveFilePath, "BAC0.log")
    if not os.path.exists(logSaveFilePath):
        try:
            os.makedirs(logSaveFilePath)
        except:
            _PERMISSION_TO_WRITE = False
    if _PERMISSION_TO_WRITE:
        fh = FileHandler(logFile)
        fh.set_name("file_handler")
        fh.setLevel(file_level)
        fh.setFormatter(formatter)

    ch.setFormatter(formatter)
    ch2.setFormatter(formatter)
    # Add handlers the first time only...
    if not len(cls._log.handlers):
        if _PERMISSION_TO_WRITE:
            cls._log.addHandler(fh)
        cls._log.addHandler(ch)
        cls._log.addHandler(ch2)

    #    cls._log.setLevel(logging.CRITICAL)

    def log_title(self, title, args=None, width=35):
        cls._log.debug("")
        cls._log.debug("#" * width)
        cls._log.debug("# {}".format(title))
        cls._log.debug("#" * width)
        if args:
            cls._log.debug("{!r}".format(args))
            cls._log.debug("#" * 35)

    def log_subtitle(self, subtitle, args=None, width=35):
        cls._log.debug("")
        cls._log.debug("=" * width)
        cls._log.debug("{}".format(subtitle))
        cls._log.debug("=" * width)
        if args:
            cls._log.debug("{!r}".format(args))
            cls._log.debug("=" * width)

    def log(self, note, *, level=logging.DEBUG):
        """
        Add a log entry...no note
        """
        if not note:
            raise ValueError("Provide something to log")
        note = "{} | {}".format(cls.logname, note)
        cls._log.log(level, note)

    def note(self, note, *, level=logging.INFO, log=True):
        """
        Add note to the object. By default, the note will also
        be logged
        :param note: (str) The note itself
        :param level: (logging.level)
        :param log: (boolean) Enable or disable logging of note
        """
        if not note:
            raise ValueError("Provide something to log")
        note = "{} | {}".format(cls.logname, note)
        cls._notes.timestamp.append(datetime.now())
        cls._notes.notes.append(note)
        if log:
            cls.log(level, note)

    @property
    def notes(self):
        """
        Retrieve notes list as a Pandas Series
        """
        if not _PANDAS:
            return dict(zip(self._notes.timestamp, self._notes.notes))
        return pd.Series(self._notes.notes, index=self._notes.timestamp)

    def clear_notes(self):
        """
        Clear notes object
        """
        cls._notes.timestamp = []
        cls._notes.notes = []

    # Add the functions to the decorated class
    cls.clear_notes = clear_notes
    cls.note = note
    cls.notes = notes
    cls.log = log
    cls.log_title = log_title
    cls.log_subtitle = log_subtitle
    return cls

Response: This is a class decorator that creates a new object that will be used to store the notes and logs in the log file.


Instruction:
Can you tell what is the following Python 3 function doing
def discoverPoints(bacnetapp, address, devID):
    pss = bacnetapp.read(
        "{} device {} protocolServicesSupported".format(address, devID)
    )
    deviceName = bacnetapp.read("{} device {} objectName".format(address, devID))

    # print('Device {}- building points list'.format(deviceName))
    objList = bacnetapp.read("{} device {] objectList".format(address, devID))

    newLine = []
    result = []
    points = []

    for pointType, pointAddr in objList:

        if "binary" in pointType:  # BI/BO/BV
            newLine = [pointType, pointAddr]
            infos = bacnetapp.readMultiple(
                "{} {} {} objectName description presentValue inactiveText activeText".format(
                    address, pointType, pointAddr
                )
            )

            newLine.extend(infos[:-2])
            newLine.extend([infos[-2:]])
            newPoint = BooleanPoint(
                pointType=newLine[0],
                pointAddress=newLine[1],
                pointName=newLine[2],
                description=newLine[3],
                presentValue=newLine[4],
                units_state=newLine[5],
            )

        elif "multiState" in pointType:  # MI/MV/MO
            newLine = [pointType, pointAddr]
            newLine.extend(
                bacnetapp.readMultiple(
                    "{} {} {} objectName description presentValue stateText".format(
                        address, pointType, pointAddr
                    )
                )
            )

            newPoint = EnumPoint(
                pointType=newLine[0],
                pointAddress=newLine[1],
                pointName=newLine[2],
                description=newLine[3],
                presentValue=newLine[4],
                units_state=newLine[5],
            )

        elif "analog" in pointType:  # AI/AO/AV
            newLine = [pointType, pointAddr]
            newLine.extend(
                bacnetapp.readMultiple(
                    "{} {} {} objectName description presentValue units".format(
                        address, pointType, pointAddr
                    )
                )
            )

            newPoint = NumericPoint(
                pointType=newLine[0],
                pointAddress=newLine[1],
                pointName=newLine[2],
                description=newLine[3],
                presentValue=newLine[4],
                units_state=newLine[5],
            )

        else:
            continue  # skip

        result.append(newLine)
        points.append(newPoint)

    if _PANDA:
        df = pd.DataFrame(
            result,
            columns=[
                "pointType",
                "pointAddress",
                "pointName",
                "description",
                "presentValue",
                "units_state",
            ],
        ).set_index(["pointName"])
    else:
        df = result

    # print('Ready!')
    return (deviceName, pss, objList, df, points)

Response: Discover the BACnet points in a BACnet device.


Instruction:
Write a Python 3 function for
checking if this segment would be a smooth segment following the previous segment.

Response:
def is_smooth_from(self, previous):
        """Checks if this segment would be a smooth segment following the previous"""
        if isinstance(previous, CubicBezier):
            return (self.start == previous.end and
                    (self.control1 - self.start) == (previous.end - previous.control2))
        else:
            return self.control1 == self.start


Instruction:
Write a Python 3 function for
calculating the x y position at a certain position of the path

Response:
def point(self, pos):
        """Calculate the x,y position at a certain position of the path"""
        return ((1 - pos) ** 3 * self.start) + \
               (3 * (1 - pos) ** 2 * pos * self.control1) + \
               (3 * (1 - pos) * pos ** 2 * self.control2) + \
               (pos ** 3 * self.end)


Instruction:
Can you generate the documentation for the following Python 3 function
def length(self, error=ERROR, min_depth=MIN_DEPTH):
        start_point = self.point(0)
        end_point = self.point(1)
        return segment_length(self, 0, 1, start_point, end_point, error, min_depth, 0)

Response: Calculate the length of the path up to a certain position


Instruction:
Given the following Python 3 function, write the documentation
def is_smooth_from(self, previous):
        if isinstance(previous, QuadraticBezier):
            return (self.start == previous.end and
                    (self.control - self.start) == (previous.end - previous.control))
        else:
            return self.control == self.start

Response: Checks if this segment would be a smooth segment following the previous segment.


Instruction:
Create a Python 3 function to
register a blueprint on the WebSockets.

Response:
def register_blueprint(self, blueprint, **options):
        '''
        Registers a blueprint on the WebSockets.
        '''
        first_registration = False
        if blueprint.name in self.blueprints:
            assert self.blueprints[blueprint.name] is blueprint, \
                'A blueprint\'s name collision occurred between %r and ' \
                '%r.  Both share the same name "%s".  Blueprints that ' \
                'are created on the fly need unique names.' % \
                (blueprint, self.blueprints[blueprint.name], blueprint.name)
        else:
            self.blueprints[blueprint.name] = blueprint
            first_registration = True
        blueprint.register(self, options, first_registration)


Instruction:
Create a Python 3 function for
adjusting the line number of an import.

Response:
def adjust_lineno(filename, lineno, name):
    """Adjust the line number of an import.

    Needed because import statements can span multiple lines, and our lineno
    is always the first line number.
    """
    line = linecache.getline(filename, lineno)
    # Hack warning: might be fooled by comments
    rx = re.compile(r'\b%s\b' % re.escape(name) if name != '*' else '[*]')
    while line and not rx.search(line):
        lineno += 1
        line = linecache.getline(filename, lineno)
    return lineno


Instruction:
Explain what the following Python 3 code does
def find_imports(filename):
    with open(filename) as f:
        root = ast.parse(f.read(), filename)
    visitor = ImportFinder(filename)
    visitor.visit(root)
    return visitor.imports

Response: Find all imported names in a given file.


Instruction:
Make a summary of the following Python 3 code
def find_imports_and_track_names(filename, warn_about_duplicates=False,
                                 verbose=False):
    """Find all imported names in a given file.

    Returns ``(imports, unused)``.  Both are lists of ImportInfo objects.
    """
    with open(filename) as f:
        root = ast.parse(f.read(), filename)
    visitor = ImportFinderAndNameTracker(filename)
    visitor.warn_about_duplicates = warn_about_duplicates
    visitor.verbose = verbose
    visitor.visit(root)
    visitor.leaveAllScopes()
    return visitor.imports, visitor.unused_names

Response: Find all imported names in a given file.


Instruction:
Implement a function in Python 3 to
parse one or more source files.

Response:
def parsePathname(self, pathname):
        """Parse one or more source files.

        ``pathname`` may be a file name or a directory name.
        """
        if os.path.isdir(pathname):
            for root, dirs, files in os.walk(pathname):
                dirs.sort()
                files.sort()
                for fn in files:
                    # ignore emacsish junk
                    if fn.endswith('.py') and not fn.startswith('.#'):
                        self.parseFile(os.path.join(root, fn))
        elif pathname.endswith('.importcache'):
            self.readCache(pathname)
        else:
            self.parseFile(pathname)


Instruction:
Implement a Python 3 function for
writing the graph to a cache file.

Response:
def writeCache(self, filename):
        """Write the graph to a cache file."""
        with open(filename, 'wb') as f:
            pickle.dump(self.modules, f)


Instruction:
Given the following Python 3 function, write the documentation
def readCache(self, filename):
        with open(filename, 'rb') as f:
            self.modules = pickle.load(f)

Response: Load the graph from a cache file.


Instruction:
Write a Python 3 script for
parsing a single file.

Response:
def parseFile(self, filename):
        """Parse a single file."""
        modname = self.filenameToModname(filename)
        module = Module(modname, filename)
        self.modules[modname] = module
        if self.trackUnusedNames:
            module.imported_names, module.unused_names = \
                find_imports_and_track_names(filename,
                                             self.warn_about_duplicates,
                                             self.verbose)
        else:
            module.imported_names = find_imports(filename)
            module.unused_names = None
        dir = os.path.dirname(filename)
        module.imports = set(
            [self.findModuleOfName(imp.name, imp.level, filename, dir)
             for imp in module.imported_names])


Instruction:
Create a Python 3 function to
convert a filename to a module name.

Response:
def filenameToModname(self, filename):
        """Convert a filename to a module name."""
        for ext in reversed(self._exts):
            if filename.endswith(ext):
                filename = filename[:-len(ext)]
                break
        else:
            self.warn(filename, '%s: unknown file name extension', filename)
        filename = os.path.abspath(filename)
        elements = filename.split(os.path.sep)
        modname = []
        while elements:
            modname.append(elements[-1])
            del elements[-1]
            if not os.path.exists(os.path.sep.join(elements + ['__init__.py'])):
                break
        modname.reverse()
        modname = ".".join(modname)
        return modname


Instruction:
Here you have a function in Python 3, explain what it does
def findModuleOfName(self, dotted_name, level, filename, extrapath=None):
        if dotted_name.endswith('.*'):
            return dotted_name[:-2]
        name = dotted_name

        # extrapath is None only in a couple of test cases; in real life it's
        # always present
        if level and level > 1 and extrapath:
            # strip trailing path bits for each extra level to account for
            # relative imports
            # from . import X has level == 1 and nothing is stripped (the level > 1 check accounts for this case)
            # from .. import X has level == 2 and one trailing path component must go
            # from ... import X has level == 3 and two trailing path components must go
            extrapath = extrapath.split(os.path.sep)
            level -= 1
            extrapath = extrapath[0:-level]
            extrapath = os.path.sep.join(extrapath)

        while name:
            candidate = self.isModule(name, extrapath)
            if candidate:
                return candidate
            candidate = self.isPackage(name, extrapath)
            if candidate:
                return candidate
            name = name[:name.rfind('.')]
        self.warn(dotted_name, '%s: could not find %s', filename, dotted_name)
        return dotted_name

Response: Given a fully qualified name find what module contains it.


Instruction:
Write a Python 3 script to
am the given dotted_name the name of a module?

Response:
def isModule(self, dotted_name, extrapath=None):
        """Is ``dotted_name`` the name of a module?"""
        try:
            return self._module_cache[(dotted_name, extrapath)]
        except KeyError:
            pass
        if dotted_name in sys.modules or dotted_name in self.builtin_modules:
            return dotted_name
        filename = dotted_name.replace('.', os.path.sep)
        if extrapath:
            for ext in self._exts:
                candidate = os.path.join(extrapath, filename) + ext
                if os.path.exists(candidate):
                    modname = self.filenameToModname(candidate)
                    self._module_cache[(dotted_name, extrapath)] = modname
                    return modname
        try:
            return self._module_cache[(dotted_name, None)]
        except KeyError:
            pass
        for dir in self.path:
            if os.path.isfile(dir):
                if dir.endswith('.egg-info'):
                    # distribute creates a setuptools-blah-blah.egg-info
                    # that ends up in sys.path
                    continue
                try:
                    zf = zipfile.ZipFile(dir)
                except zipfile.BadZipfile:
                    self.warn(dir, "%s: not a directory or zip file", dir)
                    continue
                names = zf.namelist()
                for ext in self._exts:
                    candidate = filename + ext
                    if candidate in names:
                        modname = filename.replace(os.path.sep, '.')
                        self._module_cache[(dotted_name, extrapath)] = modname
                        self._module_cache[(dotted_name, None)] = modname
                        return modname
            else:
                for ext in self._exts:
                    candidate = os.path.join(dir, filename) + ext
                    if os.path.exists(candidate):
                        modname = self.filenameToModname(candidate)
                        self._module_cache[(dotted_name, extrapath)] = modname
                        self._module_cache[(dotted_name, None)] = modname
                        return modname
        return None


Instruction:
Explain what the following Python 3 code does
def isPackage(self, dotted_name, extrapath=None):
        candidate = self.isModule(dotted_name + '.__init__', extrapath)
        if candidate:
            candidate = candidate[:-len(".__init__")]
        return candidate

Response: Is dotted_name the name of a package?


Instruction:
Can you create a Python 3 function that
determines the package that contains the dotted_name.

Response:
def packageOf(self, dotted_name, packagelevel=None):
        """Determine the package that contains ``dotted_name``."""
        if '.' not in dotted_name:
            return dotted_name
        if not self.isPackage(dotted_name):
            dotted_name = '.'.join(dotted_name.split('.')[:-1])
        if packagelevel:
            dotted_name = '.'.join(dotted_name.split('.')[:packagelevel])
        return dotted_name


Instruction:
Create a Python 3 function to
return an alphabetical list of all modules.

Response:
def listModules(self):
        """Return an alphabetical list of all modules."""
        modules = list(self.modules.items())
        modules.sort()
        return [module for name, module in modules]


Instruction:
Here you have a function in Python 3, explain what it does
def packageGraph(self, packagelevel=None):
        packages = {}
        for module in self.listModules():
            package_name = self.packageOf(module.modname, packagelevel)
            if package_name not in packages:
                dirname = os.path.dirname(module.filename)
                packages[package_name] = Module(package_name, dirname)
            package = packages[package_name]
            for name in module.imports:
                package_name = self.packageOf(name, packagelevel)
                if package_name != package.modname: # no loops
                    package.imports.add(package_name)
        graph = ModuleGraph()
        graph.modules = packages
        return graph

Response: Convert a module graph to a package graph.


Instruction:
Can you tell what is the following Python 3 function doing
def collapseCycles(self):
        # This algorithm determines Strongly Connected Components.  Look it up.
        # It is adapted to suit our data structures.
        # Phase 0: prepare the graph
        imports = {}
        for u in self.modules:
            imports[u] = set()
            for v in self.modules[u].imports:
                if v in self.modules: # skip external dependencies
                    imports[u].add(v)
        # Phase 1: order the vertices
        visited = {}
        for u in self.modules:
            visited[u] = False
        order = []
        def visit1(u):
            visited[u] = True
            for v in imports[u]:
                if not visited[v]:
                    visit1(v)
            order.append(u)
        for u in self.modules:
            if not visited[u]:
                visit1(u)
        order.reverse()
        # Phase 2: compute the inverse graph
        revimports = {}
        for u in self.modules:
            revimports[u] = set()
        for u in self.modules:
            for v in imports[u]:
                revimports[v].add(u)
        # Phase 3: determine the strongly connected components
        components = {}
        component_of = {}
        for u in self.modules:
            visited[u] = False
        def visit2(u):
            visited[u] = True
            component.append(u)
            for v in revimports[u]:
                if not visited[v]:
                    visit2(v)
        for u in order:
            if not visited[u]:
                component = []
                visit2(u)
                component.sort()
                node = ModuleCycle(component)
                components[node.modname] = node
                for modname in component:
                    component_of[modname] = node
        # Phase 4: construct the condensed graph
        for node in components.values():
            for modname in node.modnames:
                for impname in imports[modname]:
                    other = component_of[impname].modname
                    if other != node.modname:
                        node.imports.add(other)
        graph = ModuleGraph()
        graph.modules = components
        return graph

Response: Create a graph with cycles collapsed.


Instruction:
How would you implement a function in Python 3 that
produces a report of imported names.

Response:
def printImportedNames(self):
        """Produce a report of imported names."""
        for module in self.listModules():
            print("%s:" % module.modname)
            print("  %s" % "\n  ".join(imp.name for imp in module.imported_names))


Instruction:
Can you write a function in Python 3 where it
produces a report of dependencies.

Response:
def printImports(self):
        """Produce a report of dependencies."""
        for module in self.listModules():
            print("%s:" % module.label)
            if self.external_dependencies:
                imports = list(module.imports)
            else:
                imports = [modname for modname in module.imports
                           if modname in self.modules]
            imports.sort()
            print("  %s" % "\n  ".join(imports))


Instruction:
Given the following Python 3 function, write the documentation
def printUnusedImports(self):
        for module in self.listModules():
            names = [(unused.lineno, unused.name)
                     for unused in module.unused_names]
            names.sort()
            for lineno, name in names:
                if not self.all_unused:
                    line = linecache.getline(module.filename, lineno)
                    if '#' in line:
                        # assume there's a comment explaining why it's not used
                        continue
                print("%s:%s: %s not used" % (module.filename, lineno, name))

Response: Produce a report of unused imports.


Instruction:
Make a summary of the following Python 3 code
def printDot(self):
        print("digraph ModuleDependencies {")
        print("  node[shape=box];")
        allNames = set()
        nameDict = {}
        for n, module in enumerate(self.listModules()):
            module._dot_name = "mod%d" % n
            nameDict[module.modname] = module._dot_name
            print("  %s[label=\"%s\"];" % (module._dot_name,
                                           quote(module.label)))
            allNames |= module.imports
        print("  node[style=dotted];")
        if self.external_dependencies:
            myNames = set(self.modules)
            extNames = list(allNames - myNames)
            extNames.sort()
            for n, name in enumerate(extNames):
                nameDict[name] = id = "extmod%d" % n
                print("  %s[label=\"%s\"];" % (id, name))
        for modname, module in sorted(self.modules.items()):
            for other in sorted(module.imports):
                if other in nameDict:
                    print("  %s -> %s;" % (nameDict[module.modname],
                                           nameDict[other]))
        print("}")

Response: Produce a dependency graph in dot format.


Instruction:
How would you code a function in Python 3 to
create all classes and put them in ctx

Response:
def _create_tags(ctx):
    "create all classes and put them in ctx"

    for (tag, info) in _TAGS.items():
        class_name = tag.title()
        quote_, compact, self_closing, docs = info

        def __init__(self, *childs, **attrs):
            TagBase.__init__(self, childs, attrs)

        cls = type(class_name, (TagBase,), {
            "__doc__": docs,
            "__init__": __init__
        })

        cls.QUOTE = quote_
        cls.COMPACT = compact
        cls.SELF_CLOSING = self_closing

        ctx[class_name] = cls


Instruction:
Write a Python 3 function for
converting an Element into a Tag.

Response:
def tag_from_element(el):
    """
    Convert an Element into a Tag.

    ``el`` is an instance of ``Element``. Returns an instance of the
    corresponding subclass of ``TagBase``.
    """
    tag = el.tag
    namespace = None
    if tag.startswith('{'):
        # Strip namespace of the form "{namespace}tag"
        namespace,tag = tag[1:].split('}')
    try:
        cls = globals()[tag.title()]
        if not issubclass(cls, TagBase):
            raise KeyError()
    except KeyError:
        raise ValueError("TagBase doesn't have a subclass for '%s'." % tag)
    children = [tag_from_element(c) for c in el]
    tag = cls(*children, **el.attrib)
    tag.text = el.text
    tag.tail = el.tail
    if namespace:
        tag.attrib['xmlns'] = namespace
    return tag


Instruction:
Can you generate a brief explanation for the following Python 3 code
def html_to_tags(code):
    code = ('<div>' + code + '</div>').encode('utf8')
    el = ET.fromstring(code)
    return [tag_from_element(c) for c in el]

Response: Convert HTML code to tags.


Instruction:
Here you have a function in Python 3, explain what it does
def _init_math_handler(self):
        fields = self.settings.math_output.split(None, 1)
        name = fields[0].lower()
        option = fields[1] if len(fields) > 1 else None
        if name == 'html':
            option = self.settings.math_css or option
            self.math_handler = HTMLMathHandler(css_filename=option)
        elif name == 'mathml':
            if option:
                raise ValueError(('Math handler "%s" does not support ' +
                                 'option "%s".') % (name, option))
            self.math_handler = MathMLMathHandler()
        elif name == 'mathjax':
            # The MathJax handler can be configured via different ways:
            #
            # - By passing an additional JS url to "--math-output"
            #   (to stay backwards-compatible with docutils)
            #
            # - By using "--mathjax-opts" (to stay backwards compatible
            #   with the previous html5css3 mathjax postprocessor)
            #
            # - By using "--mathjax-url" and "--mathjax-config" (the
            #   preferred way)
            js_url = option
            config = None
            if self.settings.mathjax_opts:
                parts = self.settings.mathjax_opts.split(',')
                options = dict(part.split('=', 1) for part in parts)
                js_url = options.get('url', js_url)
                config = options.get('config', config)
            js_url = self.settings.mathjax_url or js_url
            config = self.settings.mathjax_config or config
            self.math_handler = MathJaxMathHandler(js_url=js_url,
                                                   config_filename=config)
        elif name == 'latex':
            if option:
                raise ValueError(('Math handler "%s" does not support ' +
                                 'option "%s".') % (name, option))
            self.math_handler = LaTeXMathHandler()
        else:
            raise ValueError('Unknown math handler "%s".' % name)

Response: Parse math configuration and set up math handler.


Instruction:
Here you have a function in Python 3, explain what it does
def append_default_stylesheets(self):
        for style in utils.get_stylesheet_list(self.settings):
            self.css(style)

Response: Appends the default styles defined on the translator settings.


Instruction:
Given the following Python 3 function, write the documentation
def css(self, path):
        if self.settings.embed_content:
            content = codecs.open(path, 'r', encoding='utf8').read()
            tag = Style(content, type="text/css")
        else:
            tag = Link(href=path, rel="stylesheet", type_="text/css")
        self.head.append(tag)

Response: Add a CSS file to the HTML.


Instruction:
Explain what the following Python 3 code does
def repr_args(args):
    res = []
    for x in args:
        if isinstance(x, tuple) and len(x) == 2:
            key, value = x
            # todo: exclude this key if value is its default
            res += ["%s=%s" % (key, repr_arg(value))]
        else:
            res += [repr_arg(x)]
    return ', '.join(res)

Response: formats a list of function arguments prettily but as working code


Instruction:
Can you generate a brief explanation for the following Python 3 code
def repr_arg(d):
    if isinstance(d, dict):
        # if d can be expressed in key=value syntax:
        return "{%s}" % ", ".join(
            "%s: %s" % (repr_arg(k), repr_arg(v)) for k, v in d.items())
    if isinstance(d, list):
        return "[%s]" % ", ".join(repr_arg(elem) for elem in d)
    if isinstance(d, unicode):
        try:
            return repr(d.encode("ascii"))
        except UnicodeEncodeError:
            return repr(d)

    return repr(d)

Response:
formats a function argument prettily but as working code
Taxonomy unicode encodable as ascii is formatted as str


Instruction:
Create a Python 3 function to
format a list of function arguments prettily not as code

Response:
def str_args(args):
    """formats a list of function arguments prettily not as code

    (kwargs are tuples (argname, argvalue)
    """
    res = []
    for x in args:
        if isinstance(x, tuple) and len(x) == 2:
            key, value = x
            if value and str_arg(value):
                res += ["%s=%s" % (key, str_arg(value))]
        else:
            res += [str_arg(x)]
    return ', '.join(res)


Instruction:
Can you tell what is the following Python 3 function doing
def str_arg(d):
    if not d:
        return None
    if isinstance(d, dict):
        if len(d) == 2 and d.get('type') == 'text' and 'value' in d:
            return str_arg(d['value'])
        if len(d) == 2 and d.get('type') == 'text' and 'subkey' in d:
            return ".%s" % d['subkey']
        if d.get('type') == 'module':
            return None
        return "{%s}" % str_args(d.items())
    if isinstance(d, list):
        if len(d) == 1:
            return str_arg(d[0])
        return "[%s]" % ", ".join(str_arg(elem) for elem in d)
    if isinstance(d, unicode):
        return '"%s"' % d

    return repr(d)

Response:
formats a function argument prettily not as code
Taxonomy


Instruction:
Can you generate the documentation for the following Python 3 function
def asyncPipeHash(context=None, _INPUT=None, conf=None, **kwargs):
    splits = yield asyncGetSplits(_INPUT, conf, **cdicts(opts, kwargs))
    parsed = yield asyncDispatch(splits, *get_async_dispatch_funcs())
    _OUTPUT = yield asyncStarMap(partial(maybeDeferred, parse_result), parsed)
    returnValue(iter(_OUTPUT))

Response: A string module that asynchronously hashes the given text. Loopable.


Instruction:
Make a summary of the following Python 3 code
def pipe_tail(context=None, _INPUT=None, conf=None, **kwargs):
    conf = DotDict(conf)
    limit = conf.get('count', func=int, **kwargs)

    for item in deque(_INPUT, limit):
        yield item

Response: A source that returns a specified number of items from the bottom of a feed. Loopable.


Instruction:
How would you explain what the following Python 3 function does
def get_graph_component(graph):
    components = map(partial(_visit, graph=graph), graph)
    node_component = dict(_gen_node_component(components))
    graph_component = {component: [] for component in components}
    graph_component.update(
        dict(_gen_graph_component(graph, node_component, _gen_graph_value)))

    return graph_component

Response: Identify strongly connected components in a graph using Tarjan s algorithm.


Instruction:
Can you generate the documentation for the following Python 3 function
def pipe_strregex(context=None, _INPUT=None, conf=None, **kwargs):
    splits = get_splits(_INPUT, conf['RULE'], **cdicts(opts, kwargs))
    parsed = utils.dispatch(splits, *get_dispatch_funcs(first=convert_func))
    _OUTPUT = starmap(parse_result, parsed)
    return _OUTPUT

Response: A string module that replaces text using regexes. Loopable.


Instruction:
Here you have a function in Python 3, explain what it does
def extract_dependencies(pipe_def=None, pipe_generator=None):
    if pipe_def:
        pydeps = gen_dependencies(pipe_def)
    elif pipe_generator:
        pydeps = pipe_generator(Context(describe_dependencies=True))
    else:
        raise Exception('Must supply at least one kwarg!')

    return sorted(set(pydeps))

Response: Extract modules used by a pipe


Instruction:
How would you explain what the following Python 3 function does
def extract_input(pipe_def=None, pipe_generator=None):
    if pipe_def:
        pyinput = gen_input(pipe_def)
    elif pipe_generator:
        pyinput = pipe_generator(Context(describe_input=True))
    else:
        raise Exception('Must supply at least one kwarg!')

    return sorted(list(pyinput))

Response: Extract inputs required by a pipe


Instruction:
Can you tell what is the following Python 3 function doing
def pythonise(id, encoding='ascii'):
    replace = {'-': '_', ':': '_', '/': '_'}
    func = lambda id, pair: id.replace(pair[0], pair[1])
    id = reduce(func, replace.iteritems(), id)
    id = '_%s' % id if id[0] in string.digits else id
    return id.encode(encoding)

Response: Return a Python - friendly id


Instruction:
Can you tell what is the following Python 3 function doing
def etree_to_dict(element):
    i = dict(element.items())
    content = element.text.strip() if element.text else None
    i.update({'content': content}) if content else None

    if len(element.getchildren()):
        for child in element.iterchildren():
            tag = child.tag.split('}', 1)[-1]
            new = etree_to_dict(child)
            content = _make_content(i, tag, new)
            i.update({tag: content}) if content else None

            tag = 'content'
            new = child.tail.strip() if child.tail else None
            content = _make_content(i, tag, new)
            i.update({tag: content}) if content else None
    elif content and not set(i).difference(['content']):
        # element is leaf node and doesn't have attributes
        i = content

    return i

Response: Convert an eTree xml into dict imitating how Yahoo Pipes does it.


Instruction:
Write a Python 3 function that can
copy an iterable and delivers the items to multiple functions /--> foo2bar(_INPUT) --> \ / \ _INPUT ---> foo2baz(_INPUT) ---> _OUTPUT \ / \--> foo2qux(_INPUT) --> / One way to construct such a flow in code would be:: _INPUT = repeat('foo', 3) foo2bar = lambda word: word.replace('foo', 'bar') foo2baz = lambda word: word.replace('foo', 'baz') foo2qux = lambda word: word.replace('foo', 'quz') _OUTPUT = broadcast(_INPUT, foo2bar, foo2baz, foo2qux) _OUTPUT == repeat(('bar', 'baz', 'qux'), 3)

Response:
def broadcast(_INPUT, *funcs, **kwargs):
    """copies an iterable and delivers the items to multiple functions

           /--> foo2bar(_INPUT) --> \
          /                          \
    _INPUT ---> foo2baz(_INPUT) ---> _OUTPUT
          \                          /
           \--> foo2qux(_INPUT) --> /

    One way to construct such a flow in code would be::

        _INPUT = repeat('foo', 3)
        foo2bar = lambda word: word.replace('foo', 'bar')
        foo2baz = lambda word: word.replace('foo', 'baz')
        foo2qux = lambda word: word.replace('foo', 'quz')
        _OUTPUT = broadcast(_INPUT, foo2bar, foo2baz, foo2qux)
        _OUTPUT == repeat(('bar', 'baz', 'qux'), 3)
    """
    map_func = kwargs.get('map_func', _map_func)
    apply_func = kwargs.get('apply_func', _apply_func)
    splits = izip(*tee(_INPUT, len(funcs)))
    return map_func(partial(apply_func, funcs), splits)


Instruction:
Explain what the following Python 3 code does
def dispatch(splits, *funcs, **kwargs):
    map_func = kwargs.get('map_func', _map_func)
    apply_func = kwargs.get('apply_func', _apply_func)
    return map_func(partial(apply_func, funcs), splits)

Response: takes multiple iterables and dispatches them to multiple functions returning a single object


Instruction:
Create a Python 3 function to
get a user parameter from the console or from a system Audience

Response:
def get_input(context, conf):
    """Gets a user parameter, either from the console or from an outer
        submodule/system

       Assumes conf has name, default, prompt and debug
    """
    name = conf['name']['value']
    prompt = conf['prompt']['value']
    default = conf['default']['value'] or conf['debug']['value']

    if context.submodule or context.inputs:
        value = context.inputs.get(name, default)
    elif not context.test:
        # we skip user interaction during tests
        raw = raw_input("%s (default=%s) " % (encode(prompt), encode(default)))
        value = raw or default
    else:
        value = default

    return value


Instruction:
Can you generate a brief explanation for the following Python 3 code
def url_quote(url):
    try:
        return quote(url, safe=URL_SAFE)
    except KeyError:
        return quote(encode(url), safe=URL_SAFE)

Response: Ensure url is valid


Instruction:
Here you have a function in Python 3, explain what it does
def multi_substitute(word, rules):
    flags = rules[0]['flags']

    # Create a combined regex from the rules
    tuples = ((p, r['match']) for p, r in enumerate(rules))
    regexes = ('(?P<match_%i>%s)' % (p, r) for p, r in tuples)
    pattern = '|'.join(regexes)
    regex = re.compile(pattern, flags)
    resplit = re.compile('\$(\d+)')

    # For each match, look-up corresponding replace value in dictionary
    rules_in_series = ifilter(itemgetter('series'), rules)
    rules_in_parallel = (r for r in rules if not r['series'])

    try:
        has_parallel = [rules_in_parallel.next()]
    except StopIteration:
        has_parallel = []

    # print('================')
    # pprint(rules)
    # print('word:', word)
    # print('pattern', pattern)
    # print('flags', flags)

    for _ in chain(rules_in_series, has_parallel):
        # print('~~~~~~~~~~~~~~~~')
        # print('new round')
        # print('word:', word)
        # found = list(regex.finditer(word))
        # matchitems = [match.groupdict().items() for match in found]
        # pprint(matchitems)
        prev_name = None
        prev_is_series = None
        i = 0

        for match in regex.finditer(word):
            item = ifilter(itemgetter(1), match.groupdict().iteritems()).next()

            # print('----------------')
            # print('groupdict:', match.groupdict().items())
            # print('item:', item)

            if not item:
                continue

            name = item[0]
            rule = rules[int(name[6:])]
            series = rule.get('series')
            kwargs = {'count': rule['count'], 'series': series}
            is_previous = name is prev_name
            singlematch = kwargs['count'] is 1
            is_series = prev_is_series or kwargs['series']
            isnt_previous = bool(prev_name) and not is_previous

            if (is_previous and singlematch) or (isnt_previous and is_series):
                continue

            prev_name = name
            prev_is_series = series

            if resplit.findall(rule['replace']):
                splits = resplit.split(rule['replace'])
                words = _gen_words(match, splits)
            else:
                splits = rule['replace']
                start = match.start() + i
                end = match.end() + i
                words = [word[:start], splits, word[end:]]
                i += rule['offset']

            # words = list(words)
            word = ''.join(words)

            # print('name:', name)
            # print('prereplace:', rule['replace'])
            # print('splits:', splits)
            # print('resplits:', resplit.findall(rule['replace']))
            # print('groups:', filter(None, match.groups()))
            # print('i:', i)
            # print('words:', words)
            # print('range:', match.start(), '-', match.end())
            # print('replace:', word)

    # print('substitution:', word)
    return word

Response: Apply multiple regex rules to word


Instruction:
Given the following Python 3 function, write the documentation
def asyncPipeItembuilder(context=None, _INPUT=None, conf=None, **kwargs):
    pkwargs = cdicts(opts, kwargs)
    asyncFuncs = yield asyncGetSplits(None, conf['attrs'], **pkwargs)
    _input = yield _INPUT
    finite = utils.finitize(_input)
    inputs = imap(DotDict, finite)
    pieces = yield asyncImap(asyncFuncs[0], inputs)
    results = imap(utils.parse_params, pieces)
    _OUTPUT = imap(DotDict, results)
    returnValue(_OUTPUT)

Response: A source that asynchronously builds an item. Loopable. _INPUT is the source that asynchronously builds an item. Loopable. _OUTPUT is the output of asyncPipeItembuilder.


Instruction:
Given the following Python 3 function, write the documentation
def asyncPipeFetch(context=None, _INPUT=None, conf=None, **kwargs):
    splits = yield asyncGetSplits(_INPUT, conf['URL'], **cdicts(opts, kwargs))
    items = yield asyncStarMap(asyncParseResult, splits)
    _OUTPUT = utils.multiplex(items)
    returnValue(_OUTPUT)

Response:
A source that asynchronously fetches and parses one or more feeds to
    return the feed entries. Loopable.


Instruction:
Here you have a function in Python 3, explain what it does
def asyncImap(asyncCallable, *iterables):
    deferreds = imap(asyncCallable, *iterables)
    return gatherResults(deferreds, consumeErrors=True)

Response: async. imap for deferred callables


Instruction:
How would you explain what the following Python 3 function does
def asyncStarCmap(asyncCallable, iterable):
    results = []
    yield coopStar(asyncCallable, results.append, iterable)
    returnValue(results)

Response: A cooperative multitasking generator for deferred callables using cooperative multitasking


Instruction:
Can you generate a brief explanation for the following Python 3 code
def asyncPipeUniq(context=None, _INPUT=None, conf=None, **kwargs):
    _input = yield _INPUT
    asyncFuncs = yield asyncGetSplits(None, conf, **cdicts(opts, kwargs))
    pieces = yield asyncFuncs[0]()
    _pass = yield asyncFuncs[2]()
    _OUTPUT = _input if _pass else unique_items(_input, pieces.field)
    returnValue(_OUTPUT)

Response: A source that asynchronously filters out non unique items according to the specified field. Not loopable. Not loopable.


Instruction:
Make a summary of the following Python 3 code
def pipe_uniq(context=None, _INPUT=None, conf=None, **kwargs):
    funcs = get_splits(None, conf, **cdicts(opts, kwargs))
    pieces, _pass = funcs[0](), funcs[2]()
    _OUTPUT = _INPUT if _pass else unique_items(_INPUT, pieces.field)
    return _OUTPUT

Response:
A module that returns a source that filters out non unique items according to the specified
    field. Not loopable.


Instruction:
Explain what the following Python 3 code does
def pipe_sort(context=None, _INPUT=None, conf=None, **kwargs):
    test = kwargs.pop('pass_if', None)
    _pass = utils.get_pass(test=test)
    key_defs = imap(DotDict, utils.listize(conf['KEY']))
    get_value = partial(utils.get_value, **kwargs)
    parse_conf = partial(utils.parse_conf, parse_func=get_value, **kwargs)
    keys = imap(parse_conf, key_defs)
    order = ('%s%s' % ('-' if k.dir == 'DESC' else '', k.field) for k in keys)
    comparers = map(get_comparer, order)
    cmp_func = partial(multikeysort, comparers=comparers)
    _OUTPUT = _INPUT if _pass else iter(sorted(_INPUT, cmp=cmp_func))
    return _OUTPUT

Response: A module that returns a source that sorts the items in a sequence according to the specified key. Not loopable. Not lazy.


Instruction:
Can you generate the documentation for the following Python 3 function
def pipe_createrss(context=None, _INPUT=None, conf=None, **kwargs):
    conf = DotDict(conf)

    for item in _INPUT:
        item = DotDict(item)

        yield {
            value: item.get(conf.get(key, **kwargs))
            for key, value in RSS_FIELDS.items()}

Response: A source that converts a source into an RSS stream. Not loopable. Loopable.


Instruction:
Make a summary of the following Python 3 code
def asyncPipeRegex(context=None, _INPUT=None, conf=None, **kwargs):
    splits = yield asyncGetSplits(_INPUT, conf['RULE'], **cdicts(opts, kwargs))
    asyncConvert = partial(maybeDeferred, convert_func)
    asyncFuncs = get_async_dispatch_funcs('pass', asyncConvert)
    parsed = yield asyncDispatch(splits, *asyncFuncs)
    _OUTPUT = yield maybeDeferred(parse_results, parsed)
    returnValue(iter(_OUTPUT))

Response: A source that asynchronously replaces text in items using regexes. Loopable.


Instruction:
Can you tell what is the following Python 3 function doing
def pipe_regex(context=None, _INPUT=None, conf=None, **kwargs):
    splits = get_splits(_INPUT, conf['RULE'], **cdicts(opts, kwargs))
    parsed = utils.dispatch(splits, *get_dispatch_funcs('pass', convert_func))
    _OUTPUT = parse_results(parsed)
    return _OUTPUT

Response: A module that allows you to use regex operator to replace text in items using regexes. Not loopable.


Instruction:
How would you explain what the following Python 3 function does
def asyncPipeUniq(context=None, _INPUT=None, conf=None, **kwargs):
    _input = yield _INPUT
    asyncFuncs = yield asyncGetSplits(None, conf, **cdicts(opts, kwargs))
    pieces = yield asyncFuncs[0]()
    _pass = yield asyncFuncs[2]()

    if _pass:
        _OUTPUT = _input
    else:
        start = int(pieces.start)
        stop = start + int(pieces.count)
        _OUTPUT = islice(_input, start, stop)

    returnValue(_OUTPUT)

Response:
A source that asynchronously returns a specified number of items from
    the top of a feed. Not loopable. Not loopable.


Instruction:
How would you explain what the following Python 3 function does
def pipe_truncate(context=None, _INPUT=None, conf=None, **kwargs):
    funcs = get_splits(None, conf, **cdicts(opts, kwargs))
    pieces, _pass = funcs[0](), funcs[2]()

    if _pass:
        _OUTPUT = _INPUT
    else:
        try:
            start = int(pieces.start)
        except AttributeError:
            start = 0

        stop = start + int(pieces.count)
        _OUTPUT = islice(_INPUT, start, stop)

    return _OUTPUT

Response:
A module that returns a specified number of items from the top of a
    feed. Not loopable.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def asyncPipeExchangerate(context=None, _INPUT=None, conf=None, **kwargs):
    offline = conf.get('offline', {}).get('value')
    # TODO add async rate data fetching
    rate_data = get_offline_rate_data() if offline else get_rate_data()
    rates = parse_request(rate_data)
    splits = yield asyncGetSplits(_INPUT, conf, **cdicts(opts, kwargs))
    parsed = yield asyncDispatch(splits, *get_async_dispatch_funcs())
    _OUTPUT = starmap(partial(parse_result, rates=rates), parsed)
    returnValue(iter(_OUTPUT))

Response: A string module that asynchronously retrieves the current exchange rate for a given currency pair. Loopable.


Instruction:
Can you generate the documentation for the following Python 3 function
def pipe_strtransform(context=None, _INPUT=None, conf=None, **kwargs):
    splits = get_splits(_INPUT, conf, **cdicts(opts, kwargs))
    parsed = utils.dispatch(splits, *get_dispatch_funcs())
    _OUTPUT = starmap(parse_result, parsed)
    return _OUTPUT

Response:
A string module that splits a string into tokens delimited by
    separators. Loopable.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def pipe_dateformat(context=None, _INPUT=None, conf=None, **kwargs):
    conf = DotDict(conf)
    loop_with = kwargs.pop('with', None)
    date_format = conf.get('format', **kwargs)
    # timezone = conf.get('timezone', **kwargs)

    for item in _INPUT:
        _with = item.get(loop_with, **kwargs) if loop_with else item

        try:
            # todo: check that all PHP formats are covered by Python
            date_string = time.strftime(date_format, _with)
        except TypeError as e:
            if context and context.verbose:
                print 'Error formatting date: %s' % item
                print e

            continue
        else:
            yield date_string

Response: A source that formats a datetime value. Loopable.


Instruction:
Can you tell what is the following Python 3 function doing
def pipe_subelement(context=None, _INPUT=None, conf=None, **kwargs):
    path = DotDict(conf).get('path', **kwargs)

    for item in _INPUT:
        element = DotDict(item).get(path, **kwargs)

        for i in utils.gen_items(element):
            yield {'content': i}

        if item.get('forever'):
            # _INPUT is pipeforever and not a loop,
            # so we just yield our item once
            break

Response: A source that returns a list of sub - elements from a feed. Not loopable. Loopable.


Instruction:
Explain what the following Python 3 code does
def pipe_feedautodiscovery(context=None, _INPUT=None, conf=None, **kwargs):
    conf = DotDict(conf)
    urls = utils.listize(conf['URL'])

    for item in _INPUT:
        for item_url in urls:
            url = utils.get_value(DotDict(item_url), DotDict(item), **kwargs)
            url = utils.get_abspath(url)

            if context and context.verbose:
                print "pipe_feedautodiscovery loading:", url

            for entry in autorss.getRSSLink(url.encode('utf-8')):
                yield {'link': entry}
                # todo: add rel, type, title

        if item.get('forever'):
            # _INPUT is pipeforever and not a loop,
            # so we just yield our item once
            break

Response: A source that searches for and returns feed links found in a page. Loopable. Loopable.


Instruction:
Make a summary of the following Python 3 code
def pipe_urlinput(context=None, _INPUT=None, conf=None, **kwargs):
    value = utils.get_input(context, conf)
    value = utils.url_quote(value)

    while True:
        yield value

Response: A source that prompts the user for a url and yields it forever. Not loopable.


Instruction:
How would you explain what the following Python 3 function does
def pipe_yql(context=None, _INPUT=None, conf=None, **kwargs):
    # todo: get from a config/env file
    url = "http://query.yahooapis.com/v1/public/yql"
    conf = DotDict(conf)
    query = conf['yqlquery']

    for item in _INPUT:
        item = DotDict(item)
        yql = utils.get_value(query, item, **kwargs)

        # note: we use the default format of xml since json loses some
        # structure
        # todo: diagnostics=true e.g. if context.test
        # todo: consider paging for large result sets
        r = requests.get(url, params={'q': yql}, stream=True)

        # Parse the response
        tree = parse(r.raw)

        if context and context.verbose:
            print "pipe_yql loading xml:", yql

        root = tree.getroot()

        # note: query also has row count
        results = root.find('results')

        # Convert xml into generation of dicts
        for element in results.getchildren():
            yield utils.etree_to_dict(element)

        if item.get('forever'):
            # _INPUT is pipeforever and not a loop,
            # so we just yield our item once
            break

Response: A source that issues YQL queries. Loopable. _INPUT is a source that returns a list of items or fields. Loopable. _OUTPUT is a generator that yields the results of the YQL query. Loopable.


Instruction:
Explain what the following Python 3 code does
def serialize_number(x, fmt=SER_BINARY, outlen=None):
    ret = b''
    if fmt == SER_BINARY:
        while x:
            x, r = divmod(x, 256)
            ret = six.int2byte(int(r)) + ret
        if outlen is not None:
            assert len(ret) <= outlen
            ret = ret.rjust(outlen, b'\0')
        return ret
    assert fmt == SER_COMPACT
    while x:
        x, r = divmod(x, len(COMPACT_DIGITS))
        ret = COMPACT_DIGITS[r:r + 1] + ret
    if outlen is not None:
        assert len(ret) <= outlen
        ret = ret.rjust(outlen, COMPACT_DIGITS[0:1])
    return ret

Response: Serializes x into a string of length outlen in format fmt.


Instruction:
How would you explain what the following Python 3 function does
def mod_issquare(a, p):
    if not a:
        return True
    p1 = p // 2
    p2 = pow(a, p1, p)
    return p2 == 1

Response: Returns whether a is a square modulo p.


Instruction:
Can you tell what is the following Python 3 function doing
def mod_root(a, p):
    if a == 0:
        return 0
    if not mod_issquare(a, p):
        raise ValueError
    n = 2
    while mod_issquare(n, p):
        n += 1
    q = p - 1
    r = 0
    while not q.getbit(r):
        r += 1
    q = q >> r
    y = pow(n, q, p)
    h = q >> 1
    b = pow(a, h, p)
    x = (a * b) % p
    b = (b * x) % p
    while b != 1:
        h = (b * b) % p
        m = 1
        while h != 1:
            h = (h * h) % p
            m += 1
        h = gmpy.mpz(0)
        h = h.setbit(r - m - 1)
        t = pow(y, h, p)
        y = (t * t) % p
        r = m
        x = (x * t) % p
        b = (b * y) % p
    return x

Response: Return a root of a modulo p


Instruction:
Given the following Python 3 function, write the documentation
def encrypt(s, pk, pk_format=SER_COMPACT, mac_bytes=10, curve=None):
    curve = (Curve.by_pk_len(len(pk)) if curve is None
             else Curve.by_name(curve))
    p = curve.pubkey_from_string(pk, pk_format)
    return p.encrypt(s, mac_bytes)

Response: Encrypts s for public key pk


Instruction:
Create a Python 3 function for
encrypting in_file to out_file for pubkey pk

Response:
def encrypt_file(in_path_or_file, out_path_or_file, pk, pk_format=SER_COMPACT,
                 mac_bytes=10, chunk_size=4096, curve=None):
    """ Encrypts `in_file' to `out_file' for pubkey `pk' """
    close_in, close_out = False, False
    in_file, out_file = in_path_or_file, out_path_or_file
    try:
        if stringlike(in_path_or_file):
            in_file = open(in_path_or_file, 'rb')
            close_in = True
        if stringlike(out_path_or_file):
            out_file = open(out_path_or_file, 'wb')
            close_out = True
        _encrypt_file(in_file, out_file, pk, pk_format, mac_bytes, chunk_size,
                      curve)
    finally:
        if close_out:
            out_file.close()
        if close_in:
            in_file.close()


Instruction:
Here you have a function in Python 3, explain what it does
def verify(s, sig, pk, sig_format=SER_COMPACT, pk_format=SER_COMPACT,
           curve=None):
    """ Verifies that `sig' is a signature of pubkey `pk' for the
        message `s'. """
    if isinstance(s, six.text_type):
        raise ValueError("Encode `s` to a bytestring yourself to" +
                         " prevent problems with different default encodings")
    curve = (Curve.by_pk_len(len(pk)) if curve is None
             else Curve.by_name(curve))
    p = curve.pubkey_from_string(pk, pk_format)
    return p.verify(hashlib.sha512(s).digest(), sig, sig_format)

Response:
Verifies that s is a signature of pk for the
        message s.


Instruction:
Implement a Python 3 function for
signing s with passphrase passphrase

Response:
def sign(s, passphrase, sig_format=SER_COMPACT, curve='secp160r1'):
    """ Signs `s' with passphrase `passphrase' """
    if isinstance(s, six.text_type):
        raise ValueError("Encode `s` to a bytestring yourself to" +
                         " prevent problems with different default encodings")
    curve = Curve.by_name(curve)
    privkey = curve.passphrase_to_privkey(passphrase)
    return privkey.sign(hashlib.sha512(s).digest(), sig_format)


Instruction:
Write a Python 3 function for
generating a random keypair.

Response:
def generate_keypair(curve='secp160r1', randfunc=None):
    """ Convenience function to generate a random
        new keypair (passphrase, pubkey). """
    if randfunc is None:
        randfunc = Crypto.Random.new().read
    curve = Curve.by_name(curve)
    raw_privkey = randfunc(curve.order_len_bin)
    privkey = serialize_number(deserialize_number(raw_privkey), SER_COMPACT)
    pubkey = str(passphrase_to_pubkey(privkey))
    return (privkey, pubkey)


Instruction:
Make a summary of the following Python 3 code
def verify(self, h, sig, sig_fmt=SER_BINARY):
        s = deserialize_number(sig, sig_fmt)
        return self.p._ECDSA_verify(h, s)

Response:
Verifies that sig is a signature for a message with
            SHA - 512 hash h.


Instruction:
Write a Python 3 function for
returning a file like object f encrypted for this pubkey and written to f.

Response:
def encrypt_to(self, f, mac_bytes=10):
        """ Returns a file like object `ef'.  Anything written to `ef'
            will be encrypted for this pubkey and written to `f'. """
        ctx = EncryptionContext(f, self.p, mac_bytes)
        yield ctx
        ctx.finish()


Instruction:
Can you generate a brief explanation for the following Python 3 code
def encrypt(self, s, mac_bytes=10):
        if isinstance(s, six.text_type):
            raise ValueError(
                "Encode `s` to a bytestring yourself to" +
                " prevent problems with different default encodings")
        out = BytesIO()
        with self.encrypt_to(out, mac_bytes) as f:
            f.write(s)
        return out.getvalue()

Response: Encrypt s for this pubkey.


Instruction:
Implement a Python 3 function for
signing the message with SHA - 512 hash h with this private key.

Response:
def sign(self, h, sig_format=SER_BINARY):
        """ Signs the message with SHA-512 hash `h' with this private key. """
        outlen = (self.curve.sig_len_compact if sig_format == SER_COMPACT
                  else self.curve.sig_len_bin)
        sig = self._ECDSA_sign(h)
        return serialize_number(sig, sig_format, outlen)


Instruction:
Implement a function in Python 3 to
convert a 32 byte hash to an exponent

Response:
def hash_to_exponent(self, h):
        """ Converts a 32 byte hash to an exponent """
        ctr = Crypto.Util.Counter.new(128, initial_value=0)
        cipher = Crypto.Cipher.AES.new(h,
                                       Crypto.Cipher.AES.MODE_CTR, counter=ctr)
        buf = cipher.encrypt(b'\0' * self.order_len_bin)
        return self._buf_to_exponent(buf)


Instruction:
Create a Python 3 function for
defining how to parse CLI arguments for the DomainTools API Client.

Response:
def parse(args=None):
    """Defines how to parse CLI arguments for the DomainTools API"""
    parser = argparse.ArgumentParser(description='The DomainTools CLI API Client')
    parser.add_argument('-u', '--username', dest='user', default='', help='API Username')
    parser.add_argument('-k', '--key', dest='key', default='', help='API Key')
    parser.add_argument('-c', '--credfile', dest='credentials', default=os.path.expanduser('~/.dtapi'),
                        help='Optional file with API username and API key, one per line.')
    parser.add_argument('-l', '--rate-limit', dest='rate_limit', action='store_true', default=False,
                        help='Rate limit API calls against the API based on per minute limits.')
    parser.add_argument('-f', '--format', dest='format', choices=['list', 'json', 'xml', 'html'], default='json')
    parser.add_argument('-o', '--outfile', dest='out_file', type=argparse.FileType('wbU'), default=sys.stdout,
                        help='Output file (defaults to stdout)')
    parser.add_argument('-v', '--version', action='version', version='DomainTools CLI API Client {0}'.format(version))
    parser.add_argument('--no-https', dest='https', action='store_false', default=True,
                        help='Use HTTP instead of HTTPS.')
    parser.add_argument('--no-verify-ssl', dest='verify_ssl', action='store_false', default=True,
                        help='Skip verification of SSL certificate when making HTTPs API calls')

    subparsers = parser.add_subparsers(help='The name of the API call you wish to perform (`whois` for example)',
                                       dest='api_call')
    subparsers.required = True
    for api_call in API_CALLS:
        api_method = getattr(API, api_call)
        subparser = subparsers.add_parser(api_call, help=api_method.__name__)
        spec = inspect.getargspec(api_method)

        for argument_name, default in reversed(list(zip_longest(reversed(spec.args or []),
                                                                reversed(spec.defaults or []), fillvalue='EMPTY'))):
            if argument_name == 'self':
                continue
            elif default == 'EMPTY':
                subparser.add_argument(argument_name)
            else:
                subparser.add_argument('--{0}'.format(argument_name.replace('_', '-')), dest=argument_name,
                                       default=default, nargs='*')

    arguments = vars(parser.parse_args(args) if args else parser.parse_args())
    if not arguments.get('user', None) or not arguments.get('key', None):
        try:
            with open(arguments.pop('credentials')) as credentials:
                arguments['user'], arguments['key'] = credentials.readline().strip(), credentials.readline().strip()
        except Exception:
            pass

    for key, value in arguments.items():
        if value in ('-', ['-']):
            arguments[key] == (line.strip() for line in sys.stdin.readlines())
        elif value == []:
            arguments[key] = True
        elif type(value) == list and len(value) == 1:
            arguments[key] = value[0]

    return (arguments.pop('out_file'), arguments.pop('format'), arguments)


Instruction:
Explain what the following Python 3 code does
def run(): # pragma: no cover
    out_file, out_format, arguments = parse()
    user, key = arguments.pop('user', None), arguments.pop('key', None)
    if not user or not key:
        sys.stderr.write('Credentials are required to perform API calls.\n')
        sys.exit(1)

    api = API(user, key, https=arguments.pop('https'), verify_ssl=arguments.pop('verify_ssl'),
              rate_limit=arguments.pop('rate_limit'))
    response = getattr(api, arguments.pop('api_call'))(**arguments)
    output = str(getattr(response, out_format) if out_format != 'list' else response.as_list())
    out_file.write(output if output.endswith('\n') else output + '\n')

Response: Defines how to start the CLI for the DomainTools API


Instruction:
Make a summary of the following Python 3 code
def permission_required(perm, *lookup_variables, **kwargs):
    login_url = kwargs.pop('login_url', settings.LOGIN_URL)
    redirect_field_name = kwargs.pop('redirect_field_name', REDIRECT_FIELD_NAME)
    redirect_to_login = kwargs.pop('redirect_to_login', True)

    def decorate(view_func):
        def decorated(request, *args, **kwargs):
            if request.user.is_authenticated():
                params = []
                for lookup_variable in lookup_variables:
                    if isinstance(lookup_variable, string_types):
                        value = kwargs.get(lookup_variable, None)
                        if value is None:
                            continue
                        params.append(value)
                    elif isinstance(lookup_variable, (tuple, list)):
                        model, lookup, varname = lookup_variable
                        value = kwargs.get(varname, None)
                        if value is None:
                            continue
                        if isinstance(model, string_types):
                            model_class = apps.get_model(*model.split("."))
                        else:
                            model_class = model
                        if model_class is None:
                            raise ValueError(
                                "The given argument '%s' is not a valid model." % model)
                        if (inspect.isclass(model_class) and
                                not issubclass(model_class, Model)):
                            raise ValueError(
                                'The argument %s needs to be a model.' % model)
                        obj = get_object_or_404(model_class, **{lookup: value})
                        params.append(obj)
                check = get_check(request.user, perm)
                granted = False
                if check is not None:
                    granted = check(*params)
                if granted or request.user.has_perm(perm):
                    return view_func(request, *args, **kwargs)
            if redirect_to_login:
                path = urlquote(request.get_full_path())
                tup = login_url, redirect_field_name, path
                return HttpResponseRedirect('%s?%s=%s' % tup)
            return permission_denied(request)
        return wraps(view_func)(decorated)
    return decorate

Response: Decorator for views that checks whether a user has a particular permission on a particular object.


Instruction:
Write a Python 3 function for
returning all permissions associated with the given obj and user .

Response:
def get_permissions(parser, token):
    """
    Retrieves all permissions associated with the given obj and user
    and assigns the result to a context variable.

    Syntax::

        {% get_permissions obj %}
        {% for perm in permissions %}
            {{ perm }}
        {% endfor %}

        {% get_permissions obj as "my_permissions" %}
        {% get_permissions obj for request.user as "my_permissions" %}

    """
    return PermissionsForObjectNode.handle_token(parser, token, approved=True,
                                                 name='"permissions"')


Instruction:
Explain what the following Python 3 code does
def get_permission_requests(parser, token):
    return PermissionsForObjectNode.handle_token(parser, token,
                                                 approved=False,
                                                 name='"permission_requests"')

Response:
Returns all permissions requests associated with the given obj and user
   .


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_permission(parser, token):
    return PermissionForObjectNode.handle_token(parser, token,
                                                approved=True,
                                                name='"permission"')

Response: Returns a permission for the given token.


Instruction:
Make a summary of the following Python 3 code
def get_permission_request(parser, token):
    return PermissionForObjectNode.handle_token(
        parser, token, approved=False, name='"permission_request"')

Response: Returns a permission request for the given token.


Instruction:
Create a Python 3 function to
render a html link to the delete view of the given permission.

Response:
def permission_delete_link(context, perm):
    """
    Renders a html link to the delete view of the given permission. Returns
    no content if the request-user has no permission to delete foreign
    permissions.
    """
    user = context['request'].user
    if user.is_authenticated():
        if (user.has_perm('authority.delete_foreign_permissions') or
                user.pk == perm.creator.pk):
            return base_link(context, perm, 'authority-delete-permission')
    return {'url': None}


Instruction:
Can you generate the documentation for the following Python 3 function
def permission_request_delete_link(context, perm):
    user = context['request'].user
    if user.is_authenticated():
        link_kwargs = base_link(context, perm,
                                'authority-delete-permission-request')
        if user.has_perm('authority.delete_permission'):
            link_kwargs['is_requestor'] = False
            return link_kwargs
        if not perm.approved and perm.user == user:
            link_kwargs['is_requestor'] = True
            return link_kwargs
    return {'url': None}

Response: Renders a html link to the delete view of the given permission request.


Instruction:
Can you generate the documentation for the following Python 3 function
def permission_request_approve_link(context, perm):
    user = context['request'].user
    if user.is_authenticated():
        if user.has_perm('authority.approve_permission_requests'):
            return base_link(context, perm, 'authority-approve-permission-request')
    return {'url': None}

Response: Renders a html link to the approve view of the given permission request.


Instruction:
Create a Python 3 function for
resolving a variable out of quotes

Response:
def resolve(self, var, context):
        """Resolves a variable out of context if it's not in quotes"""
        if var is None:
            return var
        if var[0] in ('"', "'") and var[-1] == var[0]:
            return var[1:-1]
        else:
            return template.Variable(var).resolve(context)


Instruction:
Create a Python 3 function to
get objects that have Group perm permission on

Response:
def group_permissions(self, group, perm, obj, approved=True):
        """
        Get objects that have Group perm permission on
        """
        return self.get_for_model(obj).select_related(
            'user', 'group', 'creator').filter(group=group, codename=perm,
                                               approved=approved)


Instruction:
Explain what the following Python 3 code does
def delete_user_permissions(self, user, perm, obj, check_groups=False):
        user_perms = self.user_permissions(user, perm, obj, check_groups=False)
        if not user_perms.filter(object_id=obj.id):
            return
        perms = self.user_permissions(user, perm, obj).filter(object_id=obj.id)
        perms.delete()

Response: Remove granular permission perm from user on an object instance


Instruction:
Write a Python 3 script to
return a flattened version of the parsed whois data

Response:
def flattened(self):
        """Returns a flattened version of the parsed whois data"""
        parsed = self['parsed_whois']
        flat = OrderedDict()
        for key in ('domain', 'created_date', 'updated_date', 'expired_date', 'statuses', 'name_servers'):
            value = parsed[key]
            flat[key] = ' | '.join(value) if type(value) in (list, tuple) else value

        registrar = parsed.get('registrar', {})
        for key in ('name', 'abuse_contact_phone', 'abuse_contact_email', 'iana_id', 'url', 'whois_server'):
            flat['registrar_{0}'.format(key)] = registrar[key]

        for contact_type in ('registrant', 'admin', 'tech', 'billing'):
            contact = parsed.get('contacts', {}).get(contact_type, {})
            for key in ('name', 'email', 'org', 'street', 'city', 'state', 'postal', 'country', 'phone', 'fax'):
                value = contact[key]
                flat['{0}_{1}'.format(contact_type, key)] = ' '.join(value) if type(value) in (list, tuple) else value

        return flat


Instruction:
Can you create a Python 3 function that
defaults 403 handler. Templates: `403.html` Context: request_path The path of the requested URL (e.g., '/app/pages/bad_page/')

Response:
def permission_denied(request, template_name=None, extra_context=None):
    """
    Default 403 handler.

    Templates: `403.html`
    Context:
        request_path
            The path of the requested URL (e.g., '/app/pages/bad_page/')
    """
    if template_name is None:
        template_name = ('403.html', 'authority/403.html')
    context = {
        'request_path': request.path,
    }
    if extra_context:
        context.update(extra_context)
    return HttpResponseForbidden(loader.render_to_string(
        template_name=template_name,
        context=context,
        request=request,
    ))


Instruction:
Write a Python 3 function for
approving granular permission request setting a Permission entry as approved = True for a specific action from an object instance on an object instance. creator is the creator of the object instance.

Response:
def approve(self, creator):
        """
        Approve granular permission request setting a Permission entry as
        approved=True for a specific action from an user on an object instance.
        """
        self.approved = True
        self.creator = creator
        self.save()


Instruction:
Can you create a Python 3 function that
goes and imports the permissions submodule of every app in INSTALLED_APPS to make sure the permission set classes are registered correctly.

Response:
def autodiscover_modules():
    """
    Goes and imports the permissions submodule of every app in INSTALLED_APPS
    to make sure the permission set classes are registered correctly.
    """
    import imp
    from django.conf import settings

    for app in settings.INSTALLED_APPS:
        try:
            __import__(app)
            app_path = sys.modules[app].__path__
        except AttributeError:
            continue
        try:
            imp.find_module('permissions', app_path)
        except ImportError:
            continue
        __import__("%s.permissions" % app)
        app_path = sys.modules["%s.permissions" % app]
    LOADING = False


Instruction:
Here you have a function in Python 3, explain what it does
def _get_user_cached_perms(self):
        if not self.user:
            return {}, {}
        group_pks = set(self.user.groups.values_list(
            'pk',
            flat=True,
        ))
        perms = Permission.objects.filter(
            Q(user__pk=self.user.pk) | Q(group__pk__in=group_pks),
        )
        user_permissions = {}
        group_permissions = {}
        for perm in perms:
            if perm.user_id == self.user.pk:
                user_permissions[(
                    perm.object_id,
                    perm.content_type_id,
                    perm.codename,
                    perm.approved,
                )] = True
            # If the user has the permission do for something, but perm.user !=
            # self.user then by definition that permission came from the
            # group.
            else:
                group_permissions[(
                    perm.object_id,
                    perm.content_type_id,
                    perm.codename,
                    perm.approved,
                )] = True
        return user_permissions, group_permissions

Response: Get the user and group caches.


Instruction:
Can you create a Python 3 function that
gets the group cache.

Response:
def _get_group_cached_perms(self):
        """
        Set group cache.
        """
        if not self.group:
            return {}
        perms = Permission.objects.filter(
            group=self.group,
        )
        group_permissions = {}
        for perm in perms:
            group_permissions[(
                perm.object_id,
                perm.content_type_id,
                perm.codename,
                perm.approved,
            )] = True
        return group_permissions


Instruction:
Can you generate the documentation for the following Python 3 function
def _prime_user_perm_caches(self):
        perm_cache, group_perm_cache = self._get_user_cached_perms()
        self.user._authority_perm_cache = perm_cache
        self.user._authority_group_perm_cache = group_perm_cache
        self.user._authority_perm_cache_filled = True

Response: Prime both the user and group caches and put them on the self. user.


Instruction:
How would you explain what the following Python 3 function does
def _prime_group_perm_caches(self):
        perm_cache = self._get_group_cached_perms()
        self.group._authority_perm_cache = perm_cache
        self.group._authority_perm_cache_filled = True

Response: Prime the group cache and put them on the self. group.


Instruction:
Given the following Python 3 function, write the documentation
def _user_perm_cache(self):
        # Check to see if the cache has been primed.
        if not self.user:
            return {}
        cache_filled = getattr(
            self.user,
            '_authority_perm_cache_filled',
            False,
        )
        if cache_filled:
            # Don't really like the name for this, but this matches how Django
            # does it.
            return self.user._authority_perm_cache

        # Prime the cache.
        self._prime_user_perm_caches()
        return self.user._authority_perm_cache

Response: Return the name of the cache that the user has permission to generate.


Instruction:
Here you have a function in Python 3, explain what it does
def _group_perm_cache(self):
        # Check to see if the cache has been primed.
        if not self.group:
            return {}
        cache_filled = getattr(
            self.group,
            '_authority_perm_cache_filled',
            False,
        )
        if cache_filled:
            # Don't really like the name for this, but this matches how Django
            # does it.
            return self.group._authority_perm_cache

        # Prime the cache.
        self._prime_group_perm_caches()
        return self.group._authority_perm_cache

Response: Returns the name of the cache that will be used for the group.


Instruction:
Create a Python 3 function to
return the cache_permissions for the user and group.

Response:
def _user_group_perm_cache(self):
        """
        cached_permissions will generate the cache in a lazy fashion.
        """
        # Check to see if the cache has been primed.
        if not self.user:
            return {}
        cache_filled = getattr(
            self.user,
            '_authority_perm_cache_filled',
            False,
        )
        if cache_filled:
            return self.user._authority_group_perm_cache

        # Prime the cache.
        self._prime_user_perm_caches()
        return self.user._authority_group_perm_cache


Instruction:
Write a Python 3 function that can
invalidate the cache of the permissions for the current user and group.

Response:
def invalidate_permissions_cache(self):
        """
        In the event that the Permission table is changed during the use of a
        permission the Permission cache will need to be invalidated and
        regenerated. By calling this method the invalidation will occur, and
        the next time the cached_permissions is used the cache will be
        re-primed.
        """
        if self.user:
            self.user._authority_perm_cache_filled = False
        if self.group:
            self.group._authority_perm_cache_filled = False


Instruction:
Here you have a function in Python 3, explain what it does
def has_group_perms(self, perm, obj, approved):
        if not self.group:
            return False

        if self.use_smart_cache:
            content_type_pk = Permission.objects.get_content_type(obj).pk

            def _group_has_perms(cached_perms):
                # Check to see if the permission is in the cache.
                return cached_perms.get((
                    obj.pk,
                    content_type_pk,
                    perm,
                    approved,
                ))

            # Check to see if the permission is in the cache.
            return _group_has_perms(self._group_perm_cache)

        # Actually hit the DB, no smart cache used.
        return Permission.objects.group_permissions(
            self.group,
            perm, obj,
            approved,
        ).filter(
            object_id=obj.pk,
        ).exists()

Response: Check if group has the permission for the given object.


Instruction:
Write a Python 3 function that can
check if user has the permission for the given object

Response:
def has_perm(self, perm, obj, check_groups=True, approved=True):
        """
        Check if user has the permission for the given object
        """
        if self.user:
            if self.has_user_perms(perm, obj, approved, check_groups):
                return True
        if self.group:
            return self.has_group_perms(perm, obj, approved)
        return False


Instruction:
Can you create a Python 3 function that
checks if user requested a permission for the given object.

Response:
def requested_perm(self, perm, obj, check_groups=True):
        """
        Check if user requested a permission for the given object
        """
        return self.has_perm(perm, obj, check_groups, False)


Instruction:
Write a Python 3 function for
assigning a permission to a user.

Response:
def assign(self, check=None, content_object=None, generic=False):
        """
        Assign a permission to a user.

        To assign permission for all checks: let check=None.
        To assign permission for all objects: let content_object=None.

        If generic is True then "check" will be suffixed with _modelname.
        """
        result = []

        if not content_object:
            content_objects = (self.model,)
        elif not isinstance(content_object, (list, tuple)):
            content_objects = (content_object,)
        else:
            content_objects = content_object

        if not check:
            checks = self.generic_checks + getattr(self, 'checks', [])
        elif not isinstance(check, (list, tuple)):
            checks = (check,)
        else:
            checks = check

        for content_object in content_objects:
            # raise an exception before adding any permission
            # i think Django does not rollback by default
            if not isinstance(content_object, (Model, ModelBase)):
                raise NotAModel(content_object)
            elif isinstance(content_object, Model) and not content_object.pk:
                raise UnsavedModelInstance(content_object)

            content_type = ContentType.objects.get_for_model(content_object)

            for check in checks:
                if isinstance(content_object, Model):
                    # make an authority per object permission
                    codename = self.get_codename(
                        check,
                        content_object,
                        generic,
                    )
                    try:
                        perm = Permission.objects.get(
                            user=self.user,
                            codename=codename,
                            approved=True,
                            content_type=content_type,
                            object_id=content_object.pk,
                        )
                    except Permission.DoesNotExist:
                        perm = Permission.objects.create(
                            user=self.user,
                            content_object=content_object,
                            codename=codename,
                            approved=True,
                        )

                    result.append(perm)

                elif isinstance(content_object, ModelBase):
                    # make a Django permission
                    codename = self.get_django_codename(
                        check,
                        content_object,
                        generic,
                        without_left=True,
                    )
                    try:
                        perm = DjangoPermission.objects.get(codename=codename)
                    except DjangoPermission.DoesNotExist:
                        name = check
                        if '_' in name:
                            name = name[0:name.find('_')]
                        perm = DjangoPermission(
                            name=name,
                            codename=codename,
                            content_type=content_type,
                        )
                        perm.save()
                    self.user.user_permissions.add(perm)
                    result.append(perm)

        return result


Instruction:
Can you generate a brief explanation for the following Python 3 code
def delimited(items, character='|'):
    return '|'.join(items) if type(items) in (list, tuple, set) else items

Response: Returns a character delimited version of the provided list as a Python string


Instruction:
Implement a Python 3 function for
pulling in and enforces the latest rate limits for the specified user

Response:
def _rate_limit(self):
        """Pulls in and enforces the latest rate limits for the specified user"""
        self.limits_set = True
        for product in self.account_information():
            self.limits[product['id']] = {'interval': timedelta(seconds=60 / float(product['per_minute_limit']))}


Instruction:
Write a Python 3 function for
returning a new instance of Result class with the specified API path with the specified kwargs parameters

Response:
def _results(self, product, path, cls=Results, **kwargs):
        """Returns _results for the specified API path with the specified **kwargs parameters"""
        if product != 'account-information' and self.rate_limit and not self.limits_set and not self.limits:
            self._rate_limit()

        uri = '/'.join(('{0}://api.domaintools.com'.format('https' if self.https else 'http'), path.lstrip('/')))
        parameters = self.default_parameters.copy()
        parameters['api_username'] = self.username
        if self.https:
            parameters['api_key'] = self.key
        else:
            parameters['timestamp'] = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
            parameters['signature'] = hmac(self.key.encode('utf8'), ''.join([self.username, parameters['timestamp'],
                                                                             path]).encode('utf8'),
                                           digestmod=sha1).hexdigest()
        parameters.update(dict((key, str(value).lower() if value in (True, False) else value) for
                               key, value in kwargs.items() if value is not None))
        return cls(self, product, uri, **parameters)


Instruction:
Create a Python 3 function to
mark an alert for a brand monitor.

Response:
def brand_monitor(self, query, exclude=[], domain_status=None, days_back=None, **kwargs):
        """Pass in one or more terms as a list or separated by the pipe character ( | )"""
        return self._results('mark-alert', '/v1/mark-alert', query=delimited(query), exclude=delimited(exclude),
                            domain_status=domain_status, days_back=days_back, items_path=('alerts', ), **kwargs)


Instruction:
Write a Python 3 function for
searching for a domain record.

Response:
def domain_search(self, query, exclude_query=[], max_length=25, min_length=2, has_hyphen=True, has_number=True,
                      active_only=False, deleted_only=False, anchor_left=False, anchor_right=False, page=1, **kwargs):
        """Each term in the query string must be at least three characters long.
           Pass in a list or use spaces to separate multiple terms.
        """
        return self._results('domain-search', '/v2/domain-search', query=delimited(query, ' '),
                            exclude_query=delimited(exclude_query, ' '),
                            max_length=max_length, min_length=min_length, has_hyphen=has_hyphen, has_number=has_number,
                            active_only=active_only, deleted_only=deleted_only, anchor_left=anchor_left,
                            anchor_right=anchor_right, page=page, items_path=('results', ), **kwargs)


Instruction:
Write a Python 3 script for
getting suggestions for a domain.

Response:
def domain_suggestions(self, query, **kwargs):
        """Passed in name must be at least two characters long. Use a list or spaces to separate multiple terms."""
        return self._results('domain-suggestions', '/v1/domain-suggestions', query=delimited(query, ' '),
                            items_path=('suggestions', ), **kwargs)


Instruction:
How would you code a function in Python 3 to
return the hosting history from the given domain name

Response:
def hosting_history(self, query, **kwargs):
        """Returns the hosting history from the given domain name"""
        return self._results('hosting-history', '/v1/{0}/hosting-history'.format(query), cls=GroupedIterable, **kwargs)


Instruction:
Can you generate a brief explanation for the following Python 3 code
def ip_monitor(self, query, days_back=0, page=1, **kwargs):
        return self._results('ip-monitor', '/v1/ip-monitor', query=query, days_back=days_back, page=page,
                            items_path=('alerts', ), **kwargs)

Response: Monitor the IP Address for alerts.


Instruction:
Given the following Python 3 function, write the documentation
def ip_registrant_monitor(self, query, days_back=0, search_type="all", server=None, country=None, org=None, page=1,
                              include_total_count=False, **kwargs):
        """Query based on free text query terms"""
        return self._results('ip-registrant-monitor', '/v1/ip-registrant-monitor', query=query,
                            days_back=days_back, search_type=search_type, server=server, country=country, org=org,
                            page=page, include_total_count=include_total_count, **kwargs)

Response: Query for registrations of a given set of free text terms.


Instruction:
Create a Python 3 function to
query the Name Server for alerts.

Response:
def name_server_monitor(self, query, days_back=0, page=1, **kwargs):
        """Pass in the hostname of the Name Server you wish to query ( i.e. dynect.net )."""
        return self._results('name-server-monitor', '/v1/name-server-monitor', query=query, days_back=days_back,
                            page=page, items_path=('alerts', ), **kwargs)


Instruction:
Write a Python 3 function for
getting a list of parsed Whois objects.

Response:
def parsed_whois(self, query, **kwargs):
        """Pass in a domain name"""
        return self._results('parsed-whois', '/v1/{0}/whois/parsed'.format(query), cls=ParsedWhois, **kwargs)


Instruction:
How would you code a function in Python 3 to
get the reputation score for a domain name

Response:
def reputation(self, query, include_reasons=False, **kwargs):
        """Pass in a domain name to see its reputation score"""
        return self._results('reputation', '/v1/reputation', domain=query, include_reasons=include_reasons,
                             cls=Reputation, **kwargs)


Instruction:
Make a summary of the following Python 3 code
def reverse_ip(self, domain=None, limit=None, **kwargs):
        return self._results('reverse-ip', '/v1/{0}/reverse-ip'.format(domain), limit=limit, **kwargs)

Response: Get the reverse IP of a domain.


Instruction:
Given the following Python 3 function, write the documentation
def host_domains(self, ip=None, limit=None, **kwargs):
        return self._results('reverse-ip', '/v1/{0}/host-domains'.format(ip), limit=limit, **kwargs)

Response: Get a list of domains for a given IP address.