Instruction:
Can you generate the documentation for the following Python 3 function
def init_from_file(cls,
                       catalog,
                       name=None,
                       path=None,
                       clean=False,
                       merge=True,
                       pop_schema=True,
                       ignore_keys=[],
                       compare_to_existing=True,
                       try_gzip=False,
                       filter_on={}):
        """Construct a new `Entry` instance from an input file.

        The input file can be given explicitly by `path`, or a path will
        be constructed appropriately if possible.

        Arguments
        ---------
        catalog : `astrocats.catalog.catalog.Catalog` instance
            The parent catalog object of which this entry belongs.
        name : str or 'None'
            The name of this entry, e.g. `SN1987A` for a `Supernova` entry.
            If no `path` is given, a path is constructed by trying to find
            a file in one of the 'output' repositories with this `name`.
            note: either `name` or `path` must be provided.
        path : str or 'None'
            The absolutely path of the input file.
            note: either `name` or `path` must be provided.
        clean : bool
            Whether special sanitization processing should be done on the input
            data.  This is mostly for input files from the 'internal'
            repositories.

        """
        if not catalog:
            from astrocats.catalog.catalog import Catalog
            log = logging.getLogger()
            catalog = Catalog(None, log)

        catalog.log.debug("init_from_file()")
        if name is None and path is None:
            err = ("Either entry `name` or `path` must be specified to load "
                   "entry.")
            log.error(err)
            raise ValueError(err)

        # If the path is given, use that to load from
        load_path = ''
        if path is not None:
            load_path = path
            name = ''
        # If the name is given, try to find a path for it
        else:
            repo_paths = catalog.PATHS.get_repo_output_folders()
            for rep in repo_paths:
                filename = cls.get_filename(name)
                newpath = os.path.join(rep, filename + '.json')
                if os.path.isfile(newpath):
                    load_path = newpath
                    break

        if load_path is None or not os.path.isfile(load_path):
            # FIX: is this warning worthy?
            return None

        # Create a new `Entry` instance
        new_entry = cls(catalog, name)

        # Check if .gz file
        if try_gzip and not load_path.endswith('.gz'):
            try_gzip = False

        # Fill it with data from json file
        new_entry._load_data_from_json(
            load_path,
            clean=clean,
            merge=merge,
            pop_schema=pop_schema,
            ignore_keys=ignore_keys,
            compare_to_existing=compare_to_existing,
            gzip=try_gzip,
            filter_on=filter_on)

        return new_entry

Response: Construct an Entry instance from an input file.


Instruction:
Here you have a function in Python 3, explain what it does
def add_alias(self, alias, source, clean=True):
        if clean:
            alias = self.catalog.clean_entry_name(alias)
        self.add_quantity(self._KEYS.ALIAS, alias, source)
        return alias

Response: Adds an alias to the store.


Instruction:
Explain what the following Python 3 code does
def add_error(self, value, **kwargs):
        kwargs.update({ERROR.VALUE: value})
        self._add_cat_dict(Error, self._KEYS.ERRORS, **kwargs)
        return

Response: Add an Error instance to this entry.


Instruction:
Implement a Python 3 function for
adding a Photometry instance to this entry.

Response:
def add_photometry(self, compare_to_existing=True, **kwargs):
        """Add a `Photometry` instance to this entry."""
        self._add_cat_dict(
            Photometry,
            self._KEYS.PHOTOMETRY,
            compare_to_existing=compare_to_existing,
            **kwargs)
        return


Instruction:
How would you implement a function in Python 3 that
merges two entries that correspond to the same entry.

Response:
def merge_dupes(self):
        """Merge two entries that correspond to the same entry."""
        for dupe in self.dupe_of:
            if dupe in self.catalog.entries:
                if self.catalog.entries[dupe]._stub:
                    # merge = False to avoid infinite recursion
                    self.catalog.load_entry_from_name(
                        dupe, delete=True, merge=False)
                self.catalog.copy_entry_to_entry(self.catalog.entries[dupe],
                                                 self)
                del self.catalog.entries[dupe]
        self.dupe_of = []


Instruction:
Make a summary of the following Python 3 code
def add_quantity(self,
                     quantities,
                     value,
                     source,
                     check_for_dupes=True,
                     compare_to_existing=True,
                     **kwargs):
        """Add an `Quantity` instance to this entry."""
        success = True
        for quantity in listify(quantities):
            kwargs.update({QUANTITY.VALUE: value, QUANTITY.SOURCE: source})
            cat_dict = self._add_cat_dict(
                Quantity,
                quantity,
                compare_to_existing=compare_to_existing,
                check_for_dupes=check_for_dupes,
                **kwargs)
            if isinstance(cat_dict, CatDict):
                self._append_additional_tags(quantity, source, cat_dict)
                success = False

        return success

Response: Adds an Quantity instance to this entry.


Instruction:
Can you tell what is the following Python 3 function doing
def add_self_source(self):
        return self.add_source(
            bibcode=self.catalog.OSC_BIBCODE,
            name=self.catalog.OSC_NAME,
            url=self.catalog.OSC_URL,
            secondary=True)

Response: Add a source that refers to the catalog itself.


Instruction:
Write a Python 3 script to
add a Source instance to this entry.

Response:
def add_source(self, allow_alias=False, **kwargs):
        """Add a `Source` instance to this entry."""
        if not allow_alias and SOURCE.ALIAS in kwargs:
            err_str = "`{}` passed in kwargs, this shouldn't happen!".format(
                SOURCE.ALIAS)
            self._log.error(err_str)
            raise RuntimeError(err_str)

        # Set alias number to be +1 of current number of sources
        if SOURCE.ALIAS not in kwargs:
            kwargs[SOURCE.ALIAS] = str(self.num_sources() + 1)
        source_obj = self._init_cat_dict(Source, self._KEYS.SOURCES, **kwargs)
        if source_obj is None:
            return None

        for item in self.get(self._KEYS.SOURCES, ''):
            if source_obj.is_duplicate_of(item):
                return item[item._KEYS.ALIAS]

        self.setdefault(self._KEYS.SOURCES, []).append(source_obj)
        return source_obj[source_obj._KEYS.ALIAS]


Instruction:
Write a Python 3 function that can
add a Model instance to this entry.

Response:
def add_model(self, allow_alias=False, **kwargs):
        """Add a `Model` instance to this entry."""
        if not allow_alias and MODEL.ALIAS in kwargs:
            err_str = "`{}` passed in kwargs, this shouldn't happen!".format(
                SOURCE.ALIAS)
            self._log.error(err_str)
            raise RuntimeError(err_str)

        # Set alias number to be +1 of current number of models
        if MODEL.ALIAS not in kwargs:
            kwargs[MODEL.ALIAS] = str(self.num_models() + 1)
        model_obj = self._init_cat_dict(Model, self._KEYS.MODELS, **kwargs)
        if model_obj is None:
            return None

        for item in self.get(self._KEYS.MODELS, ''):
            if model_obj.is_duplicate_of(item):
                return item[item._KEYS.ALIAS]

        self.setdefault(self._KEYS.MODELS, []).append(model_obj)
        return model_obj[model_obj._KEYS.ALIAS]


Instruction:
Can you generate the documentation for the following Python 3 function
def add_spectrum(self, compare_to_existing=True, **kwargs):
        spec_key = self._KEYS.SPECTRA
        # Make sure that a source is given, and is valid (nor erroneous)
        source = self._check_cat_dict_source(Spectrum, spec_key, **kwargs)
        if source is None:
            return None

        # Try to create a new instance of `Spectrum`
        new_spectrum = self._init_cat_dict(Spectrum, spec_key, **kwargs)
        if new_spectrum is None:
            return None

        is_dupe = False
        for item in self.get(spec_key, []):
            # Only the `filename` should be compared for duplicates. If a
            # duplicate is found, that means the previous `exclude` array
            # should be saved to the new object, and the old deleted
            if new_spectrum.is_duplicate_of(item):
                if SPECTRUM.EXCLUDE in new_spectrum:
                    item[SPECTRUM.EXCLUDE] = new_spectrum[SPECTRUM.EXCLUDE]
                elif SPECTRUM.EXCLUDE in item:
                    item.update(new_spectrum)
                is_dupe = True
                break

        if not is_dupe:
            self.setdefault(spec_key, []).append(new_spectrum)
        return

Response: Add a Spectrum instance to this entry.


Instruction:
Create a Python 3 function for
checking that the entry has the required fields.

Response:
def check(self):
        """Check that the entry has the required fields."""
        # Make sure there is a schema key in dict
        if self._KEYS.SCHEMA not in self:
            self[self._KEYS.SCHEMA] = self.catalog.SCHEMA.URL
        # Make sure there is a name key in dict
        if (self._KEYS.NAME not in self or len(self[self._KEYS.NAME]) == 0):
            raise ValueError("Entry name is empty:\n\t{}".format(
                json.dumps(
                    self, indent=2)))
        return


Instruction:
Can you create a Python 3 function that
returns the list of aliases of this object.

Response:
def get_aliases(self, includename=True):
        """Retrieve the aliases of this object as a list of strings.

        Arguments
        ---------
        includename : bool
            Include the 'name' parameter in the list of aliases.
        """
        # empty list if doesnt exist
        alias_quanta = self.get(self._KEYS.ALIAS, [])
        aliases = [aq[QUANTITY.VALUE] for aq in alias_quanta]
        if includename and self[self._KEYS.NAME] not in aliases:
            aliases = [self[self._KEYS.NAME]] + aliases
        return aliases


Instruction:
Implement a Python 3 function for
retrieving the raw text from a file.

Response:
def get_entry_text(self, fname):
        """Retrieve the raw text from a file."""
        if fname.split('.')[-1] == 'gz':
            with gz.open(fname, 'rt') as f:
                filetext = f.read()
        else:
            with codecs.open(fname, 'r') as f:
                filetext = f.read()
        return filetext


Instruction:
Can you implement a function in Python 3 that
gives an alias find the corresponding source in this entry.

Response:
def get_source_by_alias(self, alias):
        """Given an alias, find the corresponding source in this entry.

        If the given alias doesn't exist (e.g. there are no sources), then a
        `ValueError` is raised.

        Arguments
        ---------
        alias : str
            The str-integer (e.g. '8') of the target source.

        Returns
        -------
        source : `astrocats.catalog.source.Source` object
            The source object corresponding to the passed alias.

        """
        for source in self.get(self._KEYS.SOURCES, []):
            if source[self._KEYS.ALIAS] == alias:
                return source
        raise ValueError("Source '{}': alias '{}' not found!".format(self[
            self._KEYS.NAME], alias))


Instruction:
How would you code a function in Python 3 to
get a new Entry which contains the stub of this one.

Response:
def get_stub(self):
        """Get a new `Entry` which contains the 'stub' of this one.

        The 'stub' is only the name and aliases.

        Usage:
        -----
        To convert a normal entry into a stub (for example), overwrite the
        entry in place, i.e.
        >>> entries[name] = entries[name].get_stub()

        Returns
        -------
        stub : `astrocats.catalog.entry.Entry` subclass object
            The type of the returned object is this instance's type.

        """
        stub = type(self)(self.catalog, self[self._KEYS.NAME], stub=True)
        if self._KEYS.ALIAS in self:
            stub[self._KEYS.ALIAS] = self[self._KEYS.ALIAS]
        if self._KEYS.DISTINCT_FROM in self:
            stub[self._KEYS.DISTINCT_FROM] = self[self._KEYS.DISTINCT_FROM]
        if self._KEYS.RA in self:
            stub[self._KEYS.RA] = self[self._KEYS.RA]
        if self._KEYS.DEC in self:
            stub[self._KEYS.DEC] = self[self._KEYS.DEC]
        if self._KEYS.DISCOVER_DATE in self:
            stub[self._KEYS.DISCOVER_DATE] = self[self._KEYS.DISCOVER_DATE]
        if self._KEYS.SOURCES in self:
            stub[self._KEYS.SOURCES] = self[self._KEYS.SOURCES]
        return stub


Instruction:
Can you tell what is the following Python 3 function doing
def is_erroneous(self, field, sources):
        if self._KEYS.ERRORS in self:
            my_errors = self[self._KEYS.ERRORS]
            for alias in sources.split(','):
                source = self.get_source_by_alias(alias)
                bib_err_values = [
                    err[ERROR.VALUE] for err in my_errors
                    if err[ERROR.KIND] == SOURCE.BIBCODE and
                    err[ERROR.EXTRA] == field
                ]
                if (SOURCE.BIBCODE in source and
                        source[SOURCE.BIBCODE] in bib_err_values):
                    return True

                name_err_values = [
                    err[ERROR.VALUE] for err in my_errors
                    if err[ERROR.KIND] == SOURCE.NAME and err[ERROR.EXTRA] ==
                    field
                ]
                if (SOURCE.NAME in source and
                        source[SOURCE.NAME] in name_err_values):
                    return True

        return False

Response: Check if the attribute has been marked as being erroneous.


Instruction:
Can you tell what is the following Python 3 function doing
def is_private(self, key, sources):
        # aliases are always public.
        if key == ENTRY.ALIAS:
            return False
        return all([
            SOURCE.PRIVATE in self.get_source_by_alias(x)
            for x in sources.split(',')
        ])

Response: Check if attribute is private.


Instruction:
Can you tell what is the following Python 3 function doing
def sanitize(self):
        name = self[self._KEYS.NAME]

        aliases = self.get_aliases(includename=False)
        if name not in aliases:
            # Assign the first source to alias, if not available assign us.
            if self._KEYS.SOURCES in self:
                self.add_quantity(self._KEYS.ALIAS, name, '1')
                if self._KEYS.ALIAS not in self:
                    source = self.add_self_source()
                    self.add_quantity(self._KEYS.ALIAS, name, source)
            else:
                source = self.add_self_source()
                self.add_quantity(self._KEYS.ALIAS, name, source)

        if self._KEYS.ALIAS in self:
            self[self._KEYS.ALIAS].sort(
                key=lambda key: alias_priority(name, key[QUANTITY.VALUE]))
        else:
            self._log.error(
                'There should be at least one alias for `{}`.'.format(name))

        if self._KEYS.PHOTOMETRY in self:
            self[self._KEYS.PHOTOMETRY].sort(
                key=lambda x: ((float(x[PHOTOMETRY.TIME]) if
                                isinstance(x[PHOTOMETRY.TIME],
                                           (basestring, float, int))
                                else min([float(y) for y in
                                          x[PHOTOMETRY.TIME]])) if
                               PHOTOMETRY.TIME in x else 0.0,
                               x[PHOTOMETRY.BAND] if PHOTOMETRY.BAND in
                               x else '',
                               float(x[PHOTOMETRY.MAGNITUDE]) if
                               PHOTOMETRY.MAGNITUDE in x else ''))

        if (self._KEYS.SPECTRA in self and list(
                filter(None, [
                    SPECTRUM.TIME in x for x in self[self._KEYS.SPECTRA]
                ]))):
            self[self._KEYS.SPECTRA].sort(
                key=lambda x: (float(x[SPECTRUM.TIME]) if
                               SPECTRUM.TIME in x else 0.0,
                               x[SPECTRUM.FILENAME] if
                               SPECTRUM.FILENAME in x else '')
            )

        if self._KEYS.SOURCES in self:
            # Remove orphan sources
            source_aliases = [
                x[SOURCE.ALIAS] for x in self[self._KEYS.SOURCES]
            ]
            # Sources with the `PRIVATE` attribute are always retained
            source_list = [
                x[SOURCE.ALIAS] for x in self[self._KEYS.SOURCES]
                if SOURCE.PRIVATE in x
            ]
            for key in self:
                # if self._KEYS.get_key_by_name(key).no_source:
                if (key in [
                        self._KEYS.NAME, self._KEYS.SCHEMA, self._KEYS.SOURCES,
                        self._KEYS.ERRORS
                ]):
                    continue
                for item in self[key]:
                    source_list += item[item._KEYS.SOURCE].split(',')
            new_src_list = sorted(
                list(set(source_aliases).intersection(source_list)))
            new_sources = []
            for source in self[self._KEYS.SOURCES]:
                if source[SOURCE.ALIAS] in new_src_list:
                    new_sources.append(source)
                else:
                    self._log.info('Removing orphaned source from `{}`.'
                                   .format(name))

            if not new_sources:
                del self[self._KEYS.SOURCES]

            self[self._KEYS.SOURCES] = new_sources

Response: Sanitize the data before writing it to disk.


Instruction:
Can you generate the documentation for the following Python 3 function
def save(self, bury=False, final=False):
        outdir, filename = self._get_save_path(bury=bury)

        if final:
            self.sanitize()

        # FIX: use 'dump' not 'dumps'
        jsonstring = json.dumps(
            {
                self[self._KEYS.NAME]: self._ordered(self)
            },
            indent='\t' if sys.version_info[0] >= 3 else 4,
            separators=(',', ':'),
            ensure_ascii=False)
        if not os.path.isdir(outdir):
            raise RuntimeError("Output directory '{}' for event '{}' does "
                               "not exist.".format(outdir, self[
                                   self._KEYS.NAME]))
        save_name = os.path.join(outdir, filename + '.json')
        with codecs.open(save_name, 'w', encoding='utf8') as sf:
            sf.write(jsonstring)

        if not os.path.exists(save_name):
            raise RuntimeError("File '{}' was not saved!".format(save_name))

        return save_name

Response: Save the current object to a JSON file.


Instruction:
Implement a function in Python 3 to
use to sort keys when writing Entry to JSON format.

Response:
def sort_func(self, key):
        """Used to sort keys when writing Entry to JSON format.

        Should be supplemented/overridden by inheriting classes.
        """
        if key == self._KEYS.SCHEMA:
            return 'aaa'
        if key == self._KEYS.NAME:
            return 'aab'
        if key == self._KEYS.SOURCES:
            return 'aac'
        if key == self._KEYS.ALIAS:
            return 'aad'
        if key == self._KEYS.MODELS:
            return 'aae'
        if key == self._KEYS.PHOTOMETRY:
            return 'zzy'
        if key == self._KEYS.SPECTRA:
            return 'zzz'
        return key


Instruction:
Given the following Python 3 function, write the documentation
def set_pd_mag_from_counts(photodict,
                           c='',
                           ec='',
                           lec='',
                           uec='',
                           zp=DEFAULT_ZP,
                           sig=DEFAULT_UL_SIGMA):
    """Set photometry dictionary from a counts measurement."""
    with localcontext() as ctx:
        if lec == '' or uec == '':
            lec = ec
            uec = ec
        prec = max(
            get_sig_digits(str(c), strip_zeroes=False),
            get_sig_digits(str(lec), strip_zeroes=False),
            get_sig_digits(str(uec), strip_zeroes=False)) + 1
        ctx.prec = prec
        dlec = Decimal(str(lec))
        duec = Decimal(str(uec))
        if c != '':
            dc = Decimal(str(c))
        dzp = Decimal(str(zp))
        dsig = Decimal(str(sig))
        photodict[PHOTOMETRY.ZERO_POINT] = str(zp)
        if c == '' or float(c) < float(sig) * float(uec):
            photodict[PHOTOMETRY.UPPER_LIMIT] = True
            photodict[PHOTOMETRY.UPPER_LIMIT_SIGMA] = str(sig)
            photodict[PHOTOMETRY.MAGNITUDE] = str(dzp - (D25 * (dsig * duec
                                                                ).log10()))
            dnec = Decimal('10.0') ** (
                (dzp - Decimal(photodict[PHOTOMETRY.MAGNITUDE])) / D25)
            photodict[PHOTOMETRY.E_UPPER_MAGNITUDE] = str(D25 * (
                (dnec + duec).log10() - dnec.log10()))
        else:
            photodict[PHOTOMETRY.MAGNITUDE] = str(dzp - D25 * dc.log10())
            photodict[PHOTOMETRY.E_UPPER_MAGNITUDE] = str(D25 * (
                (dc + duec).log10() - dc.log10()))
            photodict[PHOTOMETRY.E_LOWER_MAGNITUDE] = str(D25 * (
                dc.log10() - (dc - dlec).log10()))

Response: Set the photometry dictionary from a counts measurement.


Instruction:
Can you tell what is the following Python 3 function doing
def set_pd_mag_from_flux_density(photodict,
                                 fd='',
                                 efd='',
                                 lefd='',
                                 uefd='',
                                 sig=DEFAULT_UL_SIGMA):
    """Set photometry dictionary from a flux density measurement.

    `fd` is assumed to be in microjanskys.
    """
    with localcontext() as ctx:
        if lefd == '' or uefd == '':
            lefd = efd
            uefd = efd
        prec = max(
            get_sig_digits(str(fd), strip_zeroes=False),
            get_sig_digits(str(lefd), strip_zeroes=False),
            get_sig_digits(str(uefd), strip_zeroes=False)) + 1
        ctx.prec = prec
        dlefd = Decimal(str(lefd))
        duefd = Decimal(str(uefd))
        if fd != '':
            dfd = Decimal(str(fd))
        dsig = Decimal(str(sig))
        if fd == '' or float(fd) < DEFAULT_UL_SIGMA * float(uefd):
            photodict[PHOTOMETRY.UPPER_LIMIT] = True
            photodict[PHOTOMETRY.UPPER_LIMIT_SIGMA] = str(sig)
            photodict[PHOTOMETRY.MAGNITUDE] = str(Decimal('23.9') - D25 * (
                dsig * duefd).log10())
            if fd:
                photodict[PHOTOMETRY.E_UPPER_MAGNITUDE] = str(D25 * (
                    (dfd + duefd).log10() - dfd.log10()))
        else:
            photodict[PHOTOMETRY.MAGNITUDE] = str(Decimal('23.9') - D25 *
                                                  dfd.log10())
            photodict[PHOTOMETRY.E_UPPER_MAGNITUDE] = str(D25 * (
                (dfd + duefd).log10() - dfd.log10()))
            photodict[PHOTOMETRY.E_LOWER_MAGNITUDE] = str(D25 * (
                dfd.log10() - (dfd - dlefd).log10()))

Response: Set the photometry dictionary from a flux density measurement.


Instruction:
Write a Python 3 script for
checking that the entry attributes are legal.

Response:
def _check(self):
        """Check that entry attributes are legal."""
        # Run the super method
        super(Photometry, self)._check()

        err_str = None
        has_flux = self._KEYS.FLUX in self
        has_flux_dens = self._KEYS.FLUX_DENSITY in self
        has_u_flux = self._KEYS.U_FLUX in self
        has_u_flux_dens = self._KEYS.U_FLUX_DENSITY in self

        has_freq = self._KEYS.FREQUENCY in self
        has_band = self._KEYS.BAND in self
        has_ener = self._KEYS.ENERGY in self
        has_u_freq = self._KEYS.U_FREQUENCY in self
        has_u_ener = self._KEYS.U_ENERGY in self

        if has_flux or has_flux_dens:
            if not any([has_freq, has_band, has_ener]):
                err_str = ("Has `{}` or `{}`".format(self._KEYS.FLUX,
                                                     self._KEYS.FLUX_DENSITY) +
                           " but None of `{}`, `{}`, `{}`".format(
                               self._KEYS.FREQUENCY, self._KEYS.BAND,
                               self._KEYS.ENERGY))
            elif has_flux and not has_u_flux:
                err_str = "`{}` provided without `{}`.".format(
                    self._KEYS.FLUX, self._KEYS.U_FLUX)
            elif has_flux_dens and not has_u_flux_dens:
                err_str = "`{}` provided without `{}`.".format(
                    self._KEYS.FLUX_DENSITY, self._KEYS.U_FLUX_DENSITY)
            elif has_freq and not has_u_freq:
                err_str = "`{}` provided without `{}`.".format(
                    self._KEYS.FREQUENCY, self._KEYS.U_FREQUENCY)
            elif has_ener and not has_u_ener:
                err_str = "`{}` provided without `{}`.".format(
                    self._KEYS.ENERGY, self._KEYS.U_ENERGY)

        if err_str is not None:
            raise ValueError(err_str)

        return


Instruction:
Write a Python 3 function that can
specify order for attributes.

Response:
def sort_func(self, key):
        """Specify order for attributes."""
        if key == self._KEYS.TIME:
            return 'aaa'
        if key == self._KEYS.MODEL:
            return 'zzy'
        if key == self._KEYS.SOURCE:
            return 'zzz'
        return key


Instruction:
Can you generate a brief explanation for the following Python 3 code
def run_migrations_offline():
    url = get_url()
    context.configure(
        url=url,
        version_table="alembic_ziggurat_foundations_version",
        transaction_per_migration=True,
    )

    with context.begin_transaction():
        context.run_migrations()

Response: Run migrations in offline mode.


Instruction:
Create a Python 3 function to
run migrations in online mode.

Response:
def run_migrations_online():
    """Run migrations in 'online' mode.

    In this scenario we need to create an Engine
    and associate a connection with the context.

    """
    engine = create_engine(get_url())

    connection = engine.connect()
    context.configure(
        connection=connection,
        target_metadata=target_metadata,
        version_table="alembic_ziggurat_foundations_version",
        transaction_per_migration=True,
    )

    try:
        with context.begin_transaction():
            context.run_migrations()
    finally:
        connection.close()


Instruction:
Write a Python 3 function that can
return all instances by user name perm name and resource id

Response:
def by_resource_user_and_perm(
        cls, user_id, perm_name, resource_id, db_session=None
    ):
        """
        return all instances by user name, perm name and resource id

        :param user_id:
        :param perm_name:
        :param resource_id:
        :param db_session:
        :return:
        """
        db_session = get_db_session(db_session)

        query = db_session.query(cls.model).filter(cls.model.user_id == user_id)
        query = query.filter(cls.model.resource_id == resource_id)
        query = query.filter(cls.model.perm_name == perm_name)
        return query.first()


Instruction:
Can you create a Python 3 function that
gets the next sensor while iterating.

Response:
def tdSensor(self):
        """Get the next sensor while iterating.

        :return: a dict with the keys: protocol, model, id, datatypes.
        """
        protocol = create_string_buffer(20)
        model = create_string_buffer(20)
        sid = c_int()
        datatypes = c_int()

        self._lib.tdSensor(protocol, sizeof(protocol), model, sizeof(model),
                           byref(sid), byref(datatypes))
        return {'protocol': self._to_str(protocol),
                'model': self._to_str(model),
                'id': sid.value, 'datatypes': datatypes.value}


Instruction:
Create a Python 3 function to
get the sensor value for a given sensor.

Response:
def tdSensorValue(self, protocol, model, sid, datatype):
        """Get the sensor value for a given sensor.

        :return: a dict with the keys: value, timestamp.
        """
        value = create_string_buffer(20)
        timestamp = c_int()

        self._lib.tdSensorValue(protocol, model, sid, datatype,
                                value, sizeof(value), byref(timestamp))
        return {'value': self._to_str(value), 'timestamp': timestamp.value}


Instruction:
Here you have a function in Python 3, explain what it does
def tdController(self):
        cid = c_int()
        ctype = c_int()
        name = create_string_buffer(255)
        available = c_int()

        self._lib.tdController(byref(cid), byref(ctype), name, sizeof(name),
                               byref(available))
        return {'id': cid.value, 'type': ctype.value,
                'name': self._to_str(name), 'available': available.value}

Response: Get the next controller while iterating.


Instruction:
Can you create a Python 3 function that
creates a cryptcontext object for use with the password manager.

Response:
def make_passwordmanager(schemes=None):
    """
    schemes contains a list of replace this list with the hash(es) you wish
    to support.
    this example sets pbkdf2_sha256 as the default,
    with support for legacy bcrypt hashes.

    :param schemes:
    :return: CryptContext()
    """
    from passlib.context import CryptContext

    if not schemes:
        schemes = ["pbkdf2_sha256", "bcrypt"]
    pwd_context = CryptContext(schemes=schemes, deprecated="auto")
    return pwd_context


Instruction:
Can you tell what is the following Python 3 function doing
def messages(request, year=None, month=None, day=None,
             template="gnotty/messages.html"):
    """
    Show messages for the given query or day.
    """

    query = request.REQUEST.get("q")
    prev_url, next_url = None, None
    messages = IRCMessage.objects.all()
    if hide_joins_and_leaves(request):
        messages = messages.filter(join_or_leave=False)
    if query:
        search = Q(message__icontains=query) | Q(nickname__icontains=query)
        messages = messages.filter(search).order_by("-message_time")
    elif year and month and day:
        messages = messages.filter(message_time__year=year,
                                   message_time__month=month,
                                   message_time__day=day)
        day_delta = timedelta(days=1)
        this_date = date(int(year), int(month), int(day))
        prev_date = this_date - day_delta
        next_date = this_date + day_delta
        prev_url = reverse("gnotty_day", args=prev_date.timetuple()[:3])
        next_url = reverse("gnotty_day", args=next_date.timetuple()[:3])
    else:
        return redirect("gnotty_year", year=datetime.now().year)

    context = dict(settings)
    context["messages"] = messages
    context["prev_url"] = prev_url
    context["next_url"] = next_url
    return render(request, template, context)

Response: Show messages for the given year month and day.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def calendar(request, year=None, month=None, template="gnotty/calendar.html"):

    try:
        year = int(year)
    except TypeError:
        year = datetime.now().year
    lookup = {"message_time__year": year}
    if month:
        lookup["message_time__month"] = month
    if hide_joins_and_leaves(request):
        lookup["join_or_leave"] = False

    messages = IRCMessage.objects.filter(**lookup)
    try:
        dates = messages.datetimes("message_time", "day")
    except AttributeError:
        dates = messages.dates("message_time", "day")
    days = [d.date() for d in dates]
    months = []

    if days:
        min_date, max_date = days[0], days[-1]
        days = set(days)
        calendar = Calendar(SUNDAY)
        for m in range(1, 13) if not month else [int(month)]:
            lt_max = m <= max_date.month or year < max_date.year
            gt_min = m >= min_date.month or year > min_date.year
            if lt_max and gt_min:
                weeks = calendar.monthdatescalendar(year, m)
                for w, week in enumerate(weeks):
                    for d, day in enumerate(week):
                        weeks[w][d] = {
                            "date": day,
                            "in_month": day.month == m,
                            "has_messages": day in days,
                        }
                months.append({"month": date(year, m, 1), "weeks": weeks})

    context = dict(settings)
    context["months"] = months
    return render(request, template, context)

Response: Show calendar months for the given year and month.


Instruction:
Make a summary of the following Python 3 code
def decorate_client(api_client, func, name):
    client_attr = getattr(api_client, name)
    if not callable(client_attr):
        return client_attr

    return OperationDecorator(client_attr, functools.partial(func, name))

Response: A helper for creating a class which wraps all calls to the specified API client.


Instruction:
Can you write a function in Python 3 where it
deletes all expired mutex locks if a ttl is provided.

Response:
def delete_expired_locks(self):
        """
        Deletes all expired mutex locks if a ttl is provided.
        """
        ttl_seconds = self.get_mutex_ttl_seconds()
        if ttl_seconds is not None:
            DBMutex.objects.filter(creation_time__lte=timezone.now() - timedelta(seconds=ttl_seconds)).delete()


Instruction:
Given the following Python 3 function, write the documentation
def start(self):
        # Delete any expired locks first
        self.delete_expired_locks()
        try:
            with transaction.atomic():
                self.lock = DBMutex.objects.create(lock_id=self.lock_id)
        except IntegrityError:
            raise DBMutexError('Could not acquire lock: {0}'.format(self.lock_id))

Response: Acquires the db mutex lock. Takes the necessary steps to delete any stale locks. Returns a DBMutexError if it can t acquire the lock.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def stop(self):
        if not DBMutex.objects.filter(id=self.lock.id).exists():
            raise DBMutexTimeoutError('Lock {0} expired before function completed'.format(self.lock_id))
        else:
            self.lock.delete()

Response: Removes the db mutex lock.


Instruction:
How would you explain what the following Python 3 function does
def decorate_callable(self, func):
        def wrapper(*args, **kwargs):
            try:
                with self:
                    result = func(*args, **kwargs)
                return result
            except DBMutexError as e:
                if self.suppress_acquisition_exceptions:
                    LOG.error(e)
                else:
                    raise e
        functools.update_wrapper(wrapper, func)
        return wrapper

Response:
Decorates a function with the db_mutex decorator by using this class as a context manager around
       .


Instruction:
Can you generate the documentation for the following Python 3 function
def groupfinder(userid, request):
    if userid and hasattr(request, "user") and request.user:
        groups = ["group:%s" % g.id for g in request.user.groups]
        return groups
    return []

Response: Default groupfinder implementaion for pyramid applications


Instruction:
Can you create a Python 3 function that
positions nodes using ForceAtlas2 force-directed algorithm Parameters ---------- graph: NetworkX graph A position will be assigned to every node in G. pos_list : dict or None optional (default=None) Initial positions for nodes as a dictionary with node as keys and values as a coordinate list or tuple. If None, then use random initial positions. node_masses : dict or None optional (default=None) Predefined masses for nodes with node as keys and masses as values. If None, then use degree of nodes. iterations : int optional (default=50) Number of iterations outbound_attraction_distribution : boolean Distributes attraction along outbound edges. Hubs attract less and thus are pushed to the borders. This mode is meant to grant authorities (nodes with a high indegree) a more central position than hubs (nodes with a high outdegree). This is useful for social networks and web networks, where authorities are sometimes considered more important than hubs lin_log_mode: boolean Switch ForceAtlas model from lin-lin to lin-log (tribute to Andreas Noack). Makes clusters more tight prevent_overlapping: boolean With this mode enabled, the repulsion is modified so that the nodes do not overlap. The goal is to produce a more readable and aesthetically pleasing image. edge_weight_influence: float How much influence you give to the edges weight. 0 is “no influence” and 1 is “normal”. jitter_tolerance: float How much swinging you allow. Above 1 discouraged. Lower gives less speed and more precision barnes_hut_optimize: boolean Barnes Hut optimization: n² complexity to n.ln(n) ; allows larger graphs. barnes_hut_theta: float Theta of the Barnes Hut optimization scaling_ratio: float How much repulsion you want. More makes a more sparse graph. strong_gravity_mode: boolean The “Strong gravity” option sets a force that attracts the nodes that are distant from the center more ( is this distance). This force has the drawback of being so strong that it is sometimes stronger than the other forces. It may result in a biased placement of the nodes. However, its advantage is to force a very compact layout, which may be useful for certain purposes. multithread: boolean gravity: float Attracts nodes to the center. Prevents islands from drifting away. Returns ------- pos : dict A dictionary of positions keyed by node

Response:
def force_atlas2_layout(graph,
                        pos_list=None,
                        node_masses=None,
                        iterations=100,

                        outbound_attraction_distribution=False,
                        lin_log_mode=False,
                        prevent_overlapping=False,
                        edge_weight_influence=1.0,

                        jitter_tolerance=1.0,
                        barnes_hut_optimize=False,
                        barnes_hut_theta=1.2,

                        scaling_ratio=2.0,
                        strong_gravity_mode=False,
                        multithread=False,
                        gravity=1.0):
    """
    Position nodes using ForceAtlas2 force-directed algorithm

    Parameters
    ----------
    graph: NetworkX graph
        A position will be assigned to every node in G.

    pos_list : dict or None  optional (default=None)
        Initial positions for nodes as a dictionary with node as keys
        and values as a coordinate list or tuple.  If None, then use
        random initial positions.

    node_masses : dict or None  optional (default=None)
        Predefined masses for nodes with node as keys and masses as values.
        If None, then use degree of nodes.

    iterations : int  optional (default=50)
        Number of iterations

    outbound_attraction_distribution : boolean
        Distributes attraction along outbound edges. Hubs attract less and thus are pushed to the borders.
        This mode is meant to grant authorities (nodes with a high indegree) a more central position than hubs (nodes with a high outdegree).
        This is useful for social networks and web networks, where authorities are sometimes considered more important than hubs

    lin_log_mode: boolean
        Switch ForceAtlas model from lin-lin to lin-log (tribute to Andreas Noack). Makes clusters more tight

    prevent_overlapping: boolean
        With this mode enabled, the repulsion is modified so that the nodes do not overlap.
        The goal is to produce a more readable and aesthetically pleasing image.

    edge_weight_influence: float
        How much influence you give to the edges weight. 0 is “no influence” and 1 is “normal”.

    jitter_tolerance: float
        How much swinging you allow. Above 1 discouraged. Lower gives less speed and more precision

    barnes_hut_optimize: boolean
        Barnes Hut optimization: n² complexity to n.ln(n) ; allows larger graphs.

    barnes_hut_theta: float
        Theta of the Barnes Hut optimization

    scaling_ratio: float
        How much repulsion you want. More makes a more sparse graph.

    strong_gravity_mode: boolean
        The “Strong gravity” option sets a force that attracts the nodes that are distant from the center more ( is this distance).
        This force has the drawback of being so strong that it is sometimes stronger than the other forces.
        It may result in a biased placement of the nodes.
        However, its advantage is to force a very compact layout, which may be useful for certain purposes.

    multithread: boolean

    gravity: float
        Attracts nodes to the center. Prevents islands from drifting away.

    Returns
    -------
    pos : dict
        A dictionary of positions keyed by node
    """

    assert isinstance(graph, networkx.classes.graph.Graph), "Not a networkx graph"
    assert isinstance(pos_list, dict) or (pos_list is None), "pos must be specified as a dictionary, as in networkx"

    assert multithread is False, "Not implemented yet"

    G = numpy.asarray(networkx.to_numpy_matrix(graph))

    pos = None

    if pos_list is not None:
        pos = numpy.asarray([pos_list[i] for i in graph.nodes()])

    masses = None

    if node_masses is not None:
        masses = numpy.asarray([node_masses[node] for node in graph.nodes()])

    assert G.shape == (G.shape[0], G.shape[0]), "G is not 2D square"
    assert numpy.all(G.T == G), "G is not symmetric."

    # speed and speed efficiency describe a scaling factor of dx and dy
    # before x and y are adjusted.  These are modified as the
    # algorithm runs to help ensure convergence.
    speed = 1
    speed_efficiency = 1

    nodes = []
    for i in range(0, G.shape[0]):
        n = Node()
        if node_masses is None:
            n.mass = 1 + numpy.count_nonzero(G[i])
        else:
            n.mass = masses[i]
        n.old_dx = 0
        n.old_dy = 0
        n.dx = 0
        n.dy = 0
        if pos is None:
            n.x = random.random()
            n.y = random.random()
        else:
            n.x = pos[i][0]
            n.y = pos[i][1]
        nodes.append(n)

    edges = []
    es = numpy.asarray(G.nonzero()).T
    for e in es:
        if e[1] <= e[0]: continue  # Avoid duplicate edges
        edge = Edge()
        edge.node1 = e[0]  # The index of the first node in `nodes`
        edge.node2 = e[1]  # The index of the second node in `nodes`
        edge.weight = G[tuple(e)]
        edges.append(edge)

    repulsion = get_repulsion(prevent_overlapping, scaling_ratio)

    if strong_gravity_mode:
        gravity_force = get_strong_gravity(scaling_ratio)
    else:
        gravity_force = repulsion

    if outbound_attraction_distribution:
        outbound_att_compensation = numpy.mean([n.mass for n in nodes])

    attraction_coef = outbound_att_compensation if outbound_attraction_distribution else 1
    attraction = get_attraction(lin_log_mode, outbound_attraction_distribution, prevent_overlapping,
                                attraction_coef)
    # Main loop

    for _i in range(0, iterations):
        for n in nodes:
            n.old_dx = n.dx
            n.old_dy = n.dy
            n.dx = 0
            n.dy = 0

        # Barnes Hut optimization
        root_region = None

        if barnes_hut_optimize:
            root_region = Quadtree(nodes)
            root_region.build()

        apply_repulsion(repulsion, nodes, barnes_hut_optimize=barnes_hut_optimize, barnes_hut_theta=barnes_hut_theta,
                        region=root_region)
        apply_gravity(gravity_force, nodes, gravity, scaling_ratio)

        apply_attraction(attraction, nodes, edges, edge_weight_influence)

        # Auto adjust speed.
        total_swinging = 0.0  # How much irregular movement
        total_effective_traction = 0.0  # How much useful movement
        for n in nodes:
            swinging = math.sqrt((n.old_dx - n.dx) * (n.old_dx - n.dx) + (n.old_dy - n.dy) * (n.old_dy - n.dy))
            total_swinging += n.mass * swinging
            total_effective_traction += .5 * n.mass * math.sqrt(
                (n.old_dx + n.dx) * (n.old_dx + n.dx) + (n.old_dy + n.dy) * (n.old_dy + n.dy))

        # Optimize jitter tolerance.
        # The 'right' jitter tolerance for this network.
        # Bigger networks need more tolerance. Denser networks need less tolerance.
        # Totally empiric.

        estimated_optimal_jitter_tolerance = .05 * math.sqrt(len(nodes))
        min_jt = math.sqrt(estimated_optimal_jitter_tolerance)
        max_jt = 10
        jt = jitter_tolerance * max(min_jt, min(max_jt, estimated_optimal_jitter_tolerance * total_effective_traction /
                                                (len(nodes) ** 2)))

        min_speed_efficiency = 0.05

        # Protective against erratic behavior
        if total_swinging / total_effective_traction > 2.0:
            if speed_efficiency > min_speed_efficiency:
                speed_efficiency *= .5
            jt = max(jt, jitter_tolerance)

        target_speed = jt * speed_efficiency * total_effective_traction / total_swinging

        if total_swinging > jt * total_effective_traction:
            if speed_efficiency > min_speed_efficiency:
                speed_efficiency *= .7
        elif speed < 1000:
            speed_efficiency *= 1.3

        # But the speed shoudn't rise too much too quickly, since it would
        # make the convergence drop dramatically.
        max_rise = .5
        speed = speed + min(target_speed - speed, max_rise * speed)

        # Apply forces.
        if prevent_overlapping:
            for n in nodes:
                swinging = n.mass * math.sqrt(
                    (n.old_dx - n.dx) * (n.old_dx - n.dx) + (n.old_dy - n.dy) * (n.old_dy - n.dy))
                factor = 0.1 * speed / (1 + math.sqrt(speed * swinging))

                df = math.sqrt(math.pow(n.dx, 2) + n.dy ** 2)
                factor = min(factor * df, 10.) / df

                x = n.dx * factor
                y = n.dy * factor
        else:
            for n in nodes:
                swinging = n.mass * math.sqrt(
                    (n.old_dx - n.dx) * (n.old_dx - n.dx) + (n.old_dy - n.dy) * (n.old_dy - n.dy))
                factor = speed / (1.0 + math.sqrt(speed * swinging))
                n.x = n.x + (n.dx * factor)
                n.y = n.y + (n.dy * factor)
    positions = [(n.x, n.y) for n in nodes]
    return dict(zip(graph.nodes(), positions))


Instruction:
Can you generate a brief explanation for the following Python 3 code
def apply_repulsion(repulsion, nodes, barnes_hut_optimize=False, region=None, barnes_hut_theta=1.2):
    if not barnes_hut_optimize:
        for i in range(0, len(nodes)):
            for j in range(0, i):
                repulsion.apply_node_to_node(nodes[i], nodes[j])
    else:
        for i in range(0, len(nodes)):
            region.apply_force(nodes[i], repulsion, barnes_hut_theta)

Response: Apply forces to the given nodes.


Instruction:
Create a Python 3 function to
apply the gravity to the nodes or edges of a given node.

Response:
def apply_gravity(repulsion, nodes, gravity, scaling_ratio):
    """
    Iterate through the nodes or edges and apply the gravity directly to the node objects.
    """
    for i in range(0, len(nodes)):
        repulsion.apply_gravitation(nodes[i], gravity / scaling_ratio)


Instruction:
Can you generate the documentation for the following Python 3 function
def get(cls, external_id, local_user_id, provider_name, db_session=None):
        db_session = get_db_session(db_session)
        return db_session.query(cls.model).get(
            [external_id, local_user_id, provider_name]
        )

Response: Fetch a specific entry from the database.


Instruction:
How would you implement a function in Python 3 that
returns an ExternalIdentity instance based on search params

Response:
def by_external_id_and_provider(cls, external_id, provider_name, db_session=None):
        """
        Returns ExternalIdentity instance based on search params

        :param external_id:
        :param provider_name:
        :param db_session:
        :return: ExternalIdentity
        """
        db_session = get_db_session(db_session)
        query = db_session.query(cls.model)
        query = query.filter(cls.model.external_id == external_id)
        query = query.filter(cls.model.provider_name == provider_name)
        return query.first()


Instruction:
Can you write a function in Python 3 where it
returns User instance based on search params

Response:
def user_by_external_id_and_provider(
        cls, external_id, provider_name, db_session=None
    ):
        """
        Returns User instance based on search params

        :param external_id:
        :param provider_name:
        :param db_session:
        :return: User
        """
        db_session = get_db_session(db_session)
        query = db_session.query(cls.models_proxy.User)
        query = query.filter(cls.model.external_id == external_id)
        query = query.filter(cls.model.provider_name == provider_name)
        query = query.filter(cls.models_proxy.User.id == cls.model.local_user_id)
        return query.first()


Instruction:
How would you explain what the following Python 3 function does
def by_user_and_perm(cls, user_id, perm_name, db_session=None):
        db_session = get_db_session(db_session)
        query = db_session.query(cls.model).filter(cls.model.user_id == user_id)
        query = query.filter(cls.model.perm_name == perm_name)
        return query.first()

Response: return a record set by user and permission name


Instruction:
Given the following Python 3 function, write the documentation
def node_is_subclass(cls, *subclass_names):
    if not isinstance(cls, (ClassDef, Instance)):
        return False

    # if cls.bases == YES:
    #     return False
    for base_cls in cls.bases:
        try:
            for inf in base_cls.inferred():  # pragma no branch
                if inf.qname() in subclass_names:
                    return True

                if inf != cls and node_is_subclass(  # pragma no branch
                        inf, *subclass_names):
                    # check up the hierarchy in case we are a subclass of
                    # a subclass of a subclass ...
                    return True
        except InferenceError:  # pragma no cover
            continue

    return False

Response: Checks if cls node has parent with subclass_name.


Instruction:
Given the following Python 3 function, write the documentation
def is_field_method(node):
    name = node.attrname
    parent = node.last_child()
    inferred = safe_infer(parent)

    if not inferred:
        return False

    for cls_name, inst in FIELD_TYPES.items():
        if node_is_instance(inferred, cls_name) and hasattr(inst, name):
            return True

    return False

Response: Checks if a call to a field instance method is valid.


Instruction:
Make a summary of the following Python 3 code
def get_node_parent_class(node):

    while node.parent:  # pragma no branch
        if isinstance(node, ClassDef):
            return node

        node = node.parent

Response: Gets the parent class of a node


Instruction:
Explain what the following Python 3 code does
def get_field_embedded_doc(node):

    definition = get_field_definition(node)
    cls_name = definition.last_child().last_child()
    cls = next(cls_name.infer())
    return cls

Response:
Returns de ClassDef for the related embedded document in a
    embedded document field.


Instruction:
Write a Python 3 function for
checking if a node is a valid field or method in an embedded document.

Response:
def node_is_embedded_doc_attr(node):
    """Checks if a node is a valid field or method in a embedded document.
    """
    embedded_doc = get_field_embedded_doc(node.last_child())
    name = node.attrname
    try:
        r = bool(embedded_doc.lookup(name)[1][0])
    except IndexError:
        r = False

    return r


Instruction:
Make a summary of the following Python 3 code
def message_channel(self, message):
        self.log(None, message)
        super(BaseBot, self).message_channel(message)

Response: Log a message to the log file.


Instruction:
Write a Python 3 script to
log any public messages and also handle the command event.

Response:
def on_pubmsg(self, connection, event):
        """
        Log any public messages, and also handle the command event.
        """
        for message in event.arguments():
            self.log(event, message)
            command_args = filter(None, message.split())
            command_name = command_args.pop(0)
            for handler in self.events["command"]:
                if handler.event.args["command"] == command_name:
                    self.handle_command_event(event, handler, command_args)


Instruction:
Explain what the following Python 3 code does
def handle_command_event(self, event, command, args):
        argspec = getargspec(command)
        num_all_args = len(argspec.args) - 2  # Ignore self/event args
        num_pos_args = num_all_args - len(argspec.defaults or [])
        if num_pos_args <= len(args) <= num_all_args:
            response = command(self, event, *args)
        elif num_all_args == num_pos_args:
            s = "s are" if num_all_args != 1 else " is"
            response = "%s arg%s required" % (num_all_args, s)
        else:
            bits = (num_pos_args, num_all_args)
            response = "between %s and %s args are required" % bits
        response = "%s: %s" % (self.get_nickname(event), response)
        self.message_channel(response)

Response: Handles a command event.


Instruction:
Write a Python 3 function for
running each timer handler in a separate greenlet thread.

Response:
def handle_timer_event(self, handler):
        """
        Runs each timer handler in a separate greenlet thread.
        """
        while True:
            handler(self)
            sleep(handler.event.args["seconds"])


Instruction:
Make a summary of the following Python 3 code
def DeviceFactory(id, lib=None):
    lib = lib or Library()
    if lib.tdGetDeviceType(id) == const.TELLSTICK_TYPE_GROUP:
        return DeviceGroup(id, lib=lib)
    return Device(id, lib=lib)

Response: Create a correct Device instance based on device type and return it.


Instruction:
Implement a function in Python 3 to
dispatch a single callback in the current thread.

Response:
def process_callback(self, block=True):
        """Dispatch a single callback in the current thread.

        :param boolean block: If True, blocks waiting for a callback to come.
        :return: True if a callback was processed; otherwise False.
        """
        try:
            (callback, args) = self._queue.get(block=block)
            try:
                callback(*args)
            finally:
                self._queue.task_done()
        except queue.Empty:
            return False
        return True


Instruction:
Given the following Python 3 function, write the documentation
def devices(self):
        devices = []
        count = self.lib.tdGetNumberOfDevices()
        for i in range(count):
            device = DeviceFactory(self.lib.tdGetDeviceId(i), lib=self.lib)
            devices.append(device)
        return devices

Response: Return all known devices.


Instruction:
Can you write a function in Python 3 where it
returns all known sensors.

Response:
def sensors(self):
        """Return all known sensors.

        :return: list of :class:`Sensor` instances.
        """
        sensors = []
        try:
            while True:
                sensor = self.lib.tdSensor()
                sensors.append(Sensor(lib=self.lib, **sensor))
        except TelldusError as e:
            if e.error != const.TELLSTICK_ERROR_DEVICE_NOT_FOUND:
                raise
        return sensors


Instruction:
Explain what the following Python 3 code does
def controllers(self):
        controllers = []
        try:
            while True:
                controller = self.lib.tdController()
                del controller["name"]
                del controller["available"]
                controllers.append(Controller(lib=self.lib, **controller))
        except TelldusError as e:
            if e.error != const.TELLSTICK_ERROR_NOT_FOUND:
                raise
        return controllers

Response: Return all known controllers. Requires Telldus core library version >= 2. 1. 2.


Instruction:
Write a Python 3 function for
adding a new device.

Response:
def add_device(self, name, protocol, model=None, **parameters):
        """Add a new device.

        :return: a :class:`Device` or :class:`DeviceGroup` instance.
        """
        device = Device(self.lib.tdAddDevice(), lib=self.lib)
        try:
            device.name = name
            device.protocol = protocol
            if model:
                device.model = model
            for key, value in parameters.items():
                device.set_parameter(key, value)

            # Return correct type
            return DeviceFactory(device.id, lib=self.lib)
        except Exception:
            import sys
            exc_info = sys.exc_info()
            try:
                device.remove()
            except:
                pass

            if "with_traceback" in dir(Exception):
                raise exc_info[0].with_traceback(exc_info[1], exc_info[2])
            else:
                exec("raise exc_info[0], exc_info[1], exc_info[2]")


Instruction:
Create a Python 3 function to
add a new group.

Response:
def add_group(self, name, devices):
        """Add a new device group.

        :return: a :class:`DeviceGroup` instance.
        """
        device = self.add_device(name, "group")
        device.add_to_group(devices)
        return device


Instruction:
Implement a function in Python 3 to
get dict with all set parameters.

Response:
def parameters(self):
        """Get dict with all set parameters."""
        parameters = {}
        for name in self.PARAMETERS:
            try:
                parameters[name] = self.get_parameter(name)
            except AttributeError:
                pass
        return parameters


Instruction:
Here you have a function in Python 3, explain what it does
def add_to_group(self, devices):
        ids = {d.id for d in self.devices_in_group()}
        ids.update(self._device_ids(devices))
        self._set_group(ids)

Response: Add devices to the group.


Instruction:
Implement a function in Python 3 to
remove devices from the group.

Response:
def remove_from_group(self, devices):
        """Remove device(s) from the group."""
        ids = {d.id for d in self.devices_in_group()}
        ids.difference_update(self._device_ids(devices))
        self._set_group(ids)


Instruction:
Write a Python 3 script for
fetching list of devices in group.

Response:
def devices_in_group(self):
        """Fetch list of devices in group."""
        try:
            devices = self.get_parameter('devices')
        except AttributeError:
            return []

        ctor = DeviceFactory
        return [ctor(int(x), lib=self.lib) for x in devices.split(',') if x]


Instruction:
Make a summary of the following Python 3 code
def value(self, datatype):
        value = self.lib.tdSensorValue(
            self.protocol, self.model, self.id, datatype)
        return SensorValue(datatype, value['value'], value['timestamp'])

Response: Return the sensor value for the given data type.


Instruction:
Can you tell what is the following Python 3 function doing
def _prepPointsForSegments(points):
    while 1:
        point = points[-1]
        if point.segmentType:
            break
        else:
            point = points.pop()
            points.insert(0, point)
            continue
        break

Response: Move any off curves at the end of the contour to the beginning of the contour. This makes sure that the contour is not empty.


Instruction:
Write a Python 3 script for
reversing the points in a .

Response:
def _reversePoints(points):
    """
    Reverse the points. This differs from the
    reversal point pen in RoboFab in that it doesn't
    worry about maintaing the start point position.
    That has no benefit within the context of this module.
    """
    # copy the points
    points = _copyPoints(points)
    # find the first on curve type and recycle
    # it for the last on curve type
    firstOnCurve = None
    for index, point in enumerate(points):
        if point.segmentType is not None:
            firstOnCurve = index
            break
    lastSegmentType = points[firstOnCurve].segmentType
    # reverse the points
    points = reversed(points)
    # work through the reversed remaining points
    final = []
    for point in points:
        segmentType = point.segmentType
        if segmentType is not None:
            point.segmentType = lastSegmentType
            lastSegmentType = segmentType
        final.append(point)
    # move any offcurves at the end of the points
    # to the start of the points
    _prepPointsForSegments(final)
    # done
    return final


Instruction:
Can you generate the documentation for the following Python 3 function
def _convertPointsToSegments(points, willBeReversed=False):
    # get the last on curve
    previousOnCurve = None
    for point in reversed(points):
        if point.segmentType is not None:
            previousOnCurve = point.coordinates
            break
    assert previousOnCurve is not None
    # gather the segments
    offCurves = []
    segments = []
    for point in points:
        # off curve, hold.
        if point.segmentType is None:
            offCurves.append(point)
        else:
            segment = InputSegment(
                points=offCurves + [point],
                previousOnCurve=previousOnCurve,
                willBeReversed=willBeReversed
            )
            segments.append(segment)
            offCurves = []
            previousOnCurve = point.coordinates
    assert not offCurves
    return segments

Response: Convert a list of points into InputSegment objects.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _tValueForPointOnCubicCurve(point, cubicCurve, isHorizontal=0):
    pt1, pt2, pt3, pt4 = cubicCurve
    a, b, c, d = bezierTools.calcCubicParameters(pt1, pt2, pt3, pt4)
    solutions = bezierTools.solveCubic(a[isHorizontal], b[isHorizontal], c[isHorizontal],
        d[isHorizontal] - point[isHorizontal])
    solutions = [t for t in solutions if 0 <= t < 1]
    if not solutions and not isHorizontal:
        # can happen that a horizontal line doens intersect, try the vertical
        return _tValueForPointOnCubicCurve(point, (pt1, pt2, pt3, pt4), isHorizontal=1)
    if len(solutions) > 1:
        intersectionLenghts = {}
        for t in solutions:
            tp = _getCubicPoint(t, pt1, pt2, pt3, pt4)
            dist = _distance(tp, point)
            intersectionLenghts[dist] = t
        minDist = min(intersectionLenghts.keys())
        solutions = [intersectionLenghts[minDist]]
    return solutions

Response: Find a t value on a curve from a point.


Instruction:
Create a Python 3 function for
scaling the points in a node tree.

Response:
def _scalePoints(points, scale=1, convertToInteger=True):
    """
    Scale points and optionally convert them to integers.
    """
    if convertToInteger:
        points = [
            (int(round(x * scale)), int(round(y * scale)))
            for (x, y) in points
        ]
    else:
        points = [(x * scale, y * scale) for (x, y) in points]
    return points


Instruction:
How would you implement a function in Python 3 that
scales a single point in a node.

Response:
def _scaleSinglePoint(point, scale=1, convertToInteger=True):
    """
    Scale a single point
    """
    x, y = point
    if convertToInteger:
        return int(round(x * scale)), int(round(y * scale))
    else:
        return (x * scale, y * scale)


Instruction:
Write a Python 3 function that can
flatten a curve segment into a list of points.

Response:
def _flattenSegment(segment, approximateSegmentLength=_approximateSegmentLength):
    """
    Flatten the curve segment int a list of points.
    The first and last points in the segment must be
    on curves. The returned list of points will not
    include the first on curve point.
    false curves (where the off curves are not any
    different from the on curves) must not be sent here.
    duplicate points must not be sent here.
    """
    onCurve1, offCurve1, offCurve2, onCurve2 = segment
    if _pointOnLine(onCurve1, onCurve2, offCurve1) and _pointOnLine(onCurve1, onCurve2, offCurve2):
        return [onCurve2]
    est = _estimateCubicCurveLength(onCurve1, offCurve1, offCurve2, onCurve2) / approximateSegmentLength
    flat = []
    minStep = 0.1564
    step = 1.0 / est
    if step > .3:
        step = minStep
    t = step
    while t < 1:
        pt = _getCubicPoint(t, onCurve1, offCurve1, offCurve2, onCurve2)
        # ignore when point is in the same direction as the on - off curve line
        if not _pointOnLine(offCurve2, onCurve2, pt) and not _pointOnLine(onCurve1, offCurve1, pt):
            flat.append(pt)
        t += step
    flat.append(onCurve2)
    return flat


Instruction:
Write a Python 3 function for
estimating the length of this curve by iterating through it averaging the length of the flat bits.

Response:
def _estimateCubicCurveLength(pt0, pt1, pt2, pt3, precision=10):
    """
    Estimate the length of this curve by iterating
    through it and averaging the length of the flat bits.
    """
    points = []
    length = 0
    step = 1.0 / precision
    factors = range(0, precision + 1)
    for i in factors:
        points.append(_getCubicPoint(i * step, pt0, pt1, pt2, pt3))
    for i in range(len(points) - 1):
        pta = points[i]
        ptb = points[i + 1]
        length += _distance(pta, ptb)
    return length


Instruction:
Explain what the following Python 3 code does
def _mid(pt1, pt2):
    (x0, y0), (x1, y1) = pt1, pt2
    return 0.5 * (x0 + x1), 0.5 * (y0 + y1)

Response: Return the midpoint of a segment between two input points.


Instruction:
Can you tell what is the following Python 3 function doing
def split(self, tValues):
        if self.segmentType == "curve":
            on1 = self.previousOnCurve
            off1 = self.points[0].coordinates
            off2 = self.points[1].coordinates
            on2 = self.points[2].coordinates
            return bezierTools.splitCubicAtT(on1, off1, off2, on2, *tValues)
        elif self.segmentType == "line":
            segments = []
            x1, y1 = self.previousOnCurve
            x2, y2 = self.points[0].coordinates
            dx = x2 - x1
            dy = y2 - y1
            pp = x1, y1
            for t in tValues:
                np = (x1+dx*t, y1+dy*t)
                segments.append([pp, np])
                pp = np
            segments.append([pp, (x2, y2)])
            return segments
        elif self.segmentType == "qcurve":
            raise NotImplementedError
        else:
            raise NotImplementedError

Response: Split the segment according the t values.


Instruction:
Create a Python 3 function for
getting a t value for a given point

Response:
def tValueForPoint(self, point):
        """
        get a t values for a given point
        required:
            the point must be a point on the curve.
            in an overlap cause the point will be an intersection points wich is alwasy a point on the curve
        """
        if self.segmentType == "curve":
            on1 = self.previousOnCurve
            off1 = self.points[0].coordinates
            off2 = self.points[1].coordinates
            on2 = self.points[2].coordinates
            return _tValueForPointOnCubicCurve(point, (on1, off1, off2, on2))
        elif self.segmentType == "line":
            return _tValueForPointOnLine(point, (self.previousOnCurve, self.points[0].coordinates))
        elif self.segmentType == "qcurve":
            raise NotImplementedError
        else:
            raise NotImplementedError


Instruction:
Can you create a Python 3 function that
returns a list of normalized InputPoint objects for the contour drawn with this pen.

Response:
def getData(self):
        """
        Return a list of normalized InputPoint objects
        for the contour drawn with this pen.
        """
        # organize the points into segments
        # 1. make sure there is an on curve
        haveOnCurve = False
        for point in self._points:
            if point.segmentType is not None:
                haveOnCurve = True
                break
        # 2. move the off curves to front of the list
        if haveOnCurve:
            _prepPointsForSegments(self._points)
        # 3. ignore double points on start and end
        firstPoint = self._points[0]
        lastPoint = self._points[-1]
        if firstPoint.segmentType is not None and lastPoint.segmentType is not None:
            if firstPoint.coordinates == lastPoint.coordinates:
                if (firstPoint.segmentType in ["line", "move"]):
                    del self._points[0]
                else:
                    raise AssertionError("Unhandled point type sequence")
        # done
        return self._points


Instruction:
How would you explain what the following Python 3 function does
def reCurveFromEntireInputContour(self, inputContour):
        if self.clockwise:
            inputFlat = inputContour.clockwiseFlat
        else:
            inputFlat = inputContour.counterClockwiseFlat
        outputFlat = []
        for segment in self.segments:
            # XXX this could be expensive
            assert segment.segmentType == "flat"
            outputFlat += segment.points
        # test lengths
        haveMatch = False
        if len(inputFlat) == len(outputFlat):
            if inputFlat == outputFlat:
                haveMatch = True
            else:
                inputStart = inputFlat[0]
                if inputStart in outputFlat:
                    # there should be only one occurance of the point
                    # but handle it just in case
                    if outputFlat.count(inputStart) > 1:
                        startIndexes = [index for index, point in enumerate(outputFlat) if point == inputStart]
                    else:
                        startIndexes = [outputFlat.index(inputStart)]
                    # slice and dice to test possible orders
                    for startIndex in startIndexes:
                        test = outputFlat[startIndex:] + outputFlat[:startIndex]
                        if inputFlat == test:
                            haveMatch = True
                            break
        if haveMatch:
            # clear out the flat points
            self.segments = []
            # replace with the appropriate points from the input
            if self.clockwise:
                inputSegments = inputContour.clockwiseSegments
            else:
                inputSegments = inputContour.counterClockwiseSegments
            for inputSegment in inputSegments:
                self.segments.append(
                    OutputSegment(
                        segmentType=inputSegment.segmentType,
                        points=[
                            OutputPoint(
                                coordinates=point.coordinates,
                                segmentType=point.segmentType,
                                smooth=point.smooth,
                                name=point.name,
                                kwargs=point.kwargs
                            )
                            for point in inputSegment.points
                        ],
                        final=True
                    )
                )
                inputSegment.used = True
            # reset the direction of the final contour
            self.clockwise = inputContour.clockwise
            return True
        return False

Response: Re - curve from an entire input contour to the output contour.


Instruction:
Can you generate the documentation for the following Python 3 function
def _is_custom_qs_manager(funcdef):

    decors = getattr(funcdef, 'decorators', None)
    if decors:
        for dec in decors.get_children():
            try:
                if dec.name == 'queryset_manager':  # pragma no branch
                    return True
            except AttributeError:
                continue

    return False

Response:
Checks if a function definition is a queryset manager created
    with the queryset_manager decorator.


Instruction:
Given the following Python 3 function, write the documentation
def _is_call2custom_manager(node):
    called = safe_infer(node.func)
    funcdef = getattr(called, '_proxied', None)
    return _is_custom_qs_manager(funcdef)

Response: Checks if the call is being done to a custom queryset manager.


Instruction:
How would you explain what the following Python 3 function does
def _is_custom_manager_attribute(node):

    attrname = node.attrname
    if not name_is_from_qs(attrname):
        return False

    for attr in node.get_children():
        inferred = safe_infer(attr)
        funcdef = getattr(inferred, '_proxied', None)
        if _is_custom_qs_manager(funcdef):
            return True

    return False

Response: Checks if the attribute is a valid attribute for a queryset manager.


Instruction:
Given the following Python 3 function, write the documentation
def by_group_and_perm(cls, group_id, perm_name, db_session=None):
        db_session = get_db_session(db_session)
        query = db_session.query(cls.model).filter(cls.model.group_id == group_id)
        query = query.filter(cls.model.perm_name == perm_name)
        return query.first()

Response: return by group_id and perm_name and permission name


Instruction:
Explain what the following Python 3 code does
def serve_forever(django=False):
    logger = getLogger("irc.dispatch")
    logger.setLevel(settings.LOG_LEVEL)
    logger.addHandler(StreamHandler())
    app = IRCApplication(django)
    server = SocketIOServer((settings.HTTP_HOST, settings.HTTP_PORT), app)
    print "%s [Bot: %s] listening on %s:%s" % (
        settings.GNOTTY_VERSION_STRING,
        app.bot.__class__.__name__,
        settings.HTTP_HOST,
        settings.HTTP_PORT,
    )
    server.serve_forever()

Response: Starts the gevent - socketio server.


Instruction:
Can you implement a function in Python 3 that
kills a previously started daemon.

Response:
def kill(pid_file):
    """
    Attempts to shut down a previously started daemon.
    """
    try:
        with open(pid_file) as f:
            os.kill(int(f.read()), 9)
        os.remove(pid_file)
    except (IOError, OSError):
        return False
    return True


Instruction:
Given the following Python 3 function, write the documentation
def run():
    settings.parse_args()
    pid_name = "gnotty-%s-%s.pid" % (settings.HTTP_HOST, settings.HTTP_PORT)
    pid_file = settings.PID_FILE or os.path.join(gettempdir(), pid_name)
    if settings.KILL:
        if kill(pid_file):
            print "Daemon killed"
        else:
            print "Could not kill any daemons"
        return
    elif kill(pid_file):
        print "Running daemon killed"
    if settings.DAEMON:
        daemonize(pid_file)
    serve_forever()

Response: Main entry point for the gevent - socketio server.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def on_start(self, host, port, channel, nickname, password):
        self.client = WebSocketIRCClient(host, port, channel, nickname,
                                         password, self)
        self.spawn(self.client.start)

Response: Called when the IRC client is started.


Instruction:
Create a Python 3 function to
disconnect from the IRC server.

Response:
def disconnect(self, *args, **kwargs):
        """
        WebSocket was disconnected - leave the IRC channel.
        """
        quit_message = "%s %s" % (settings.GNOTTY_VERSION_STRING,
                                  settings.GNOTTY_PROJECT_URL)
        self.client.connection.quit(quit_message)
        super(IRCNamespace, self).disconnect(*args, **kwargs)


Instruction:
Can you implement a function in Python 3 that
threads that will try and reconnect the bot if it s not connected.

Response:
def bot_watcher(self):
        """
        Thread (greenlet) that will try and reconnect the bot if
        it's not connected.
        """
        default_interval = 5
        interval = default_interval
        while True:
            if not self.bot.connection.connected:
                if self.bot.reconnect():
                    interval = default_interval
                else:
                    interval *= 2
            sleep(interval)


Instruction:
Create a Python 3 function to
pass the request onto a bot with a webhook.

Response:
def respond_webhook(self, environ):
        """
        Passes the request onto a bot with a webhook if the webhook
        path is requested.
        """
        request = FieldStorage(fp=environ["wsgi.input"], environ=environ)
        url = environ["PATH_INFO"]
        params = dict([(k, request[k].value) for k in request])
        try:
            if self.bot is None:
                raise NotImplementedError
            response = self.bot.handle_webhook_event(environ, url, params)
        except NotImplementedError:
            return 404
        except:
            self.logger.debug(format_exc())
            return 500
        return response or 200


Instruction:
Can you create a Python 3 function that
serves a static file when Django isn t being used.

Response:
def respond_static(self, environ):
        """
        Serves a static file when Django isn't being used.
        """
        path = os.path.normpath(environ["PATH_INFO"])
        if path == "/":
            content = self.index()
            content_type = "text/html"
        else:
            path = os.path.join(os.path.dirname(__file__), path.lstrip("/"))
            try:
                with open(path, "r") as f:
                    content = f.read()
            except IOError:
                return 404
            content_type = guess_type(path)[0]
        return (200, [("Content-Type", content_type)], content)


Instruction:
Given the following Python 3 function, write the documentation
def index(self):
        root_dir = os.path.dirname(__file__)
        template_dir = os.path.join(root_dir, "templates", "gnotty")
        with open(os.path.join(template_dir, "base.html"), "r") as f:
            base = f.read()
        with open(os.path.join(template_dir, "chat.html"), "r") as f:
            base = base.replace("{% block content %}", f.read())
        replace = {
            "{% block content %}": "",
            "{% block extrahead %}": "",
            "{% endblock %}": "",
            "{% load gnotty_tags %}": "",
            "{% extends \"gnotty/base.html\" %}": "",
            "{% url gnotty_chat %}": "/",
            "{% gnotty_nav %}": "",
            "{% templatetag openvariable %}": "{{",
            "{% templatetag closevariable %}": "}}",
        }
        for k, v in replace.items():
            base = base.replace(k, v)
        for k, v in settings.items():
            base = base.replace("{{ %s }}" % k, unicode(v or ""))
        return base

Response:
Loads the chat interface template when Django isn t being
        used manually dealing with the Django template bits.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def authorized(self, environ):
        if self.django and settings.LOGIN_REQUIRED:
            try:
                from django.conf import settings as django_settings
                from django.contrib.auth import SESSION_KEY
                from django.contrib.auth.models import User
                from django.contrib.sessions.models import Session
                from django.core.exceptions import ObjectDoesNotExist
                cookie = SimpleCookie(environ["HTTP_COOKIE"])
                cookie_name = django_settings.SESSION_COOKIE_NAME
                session_key = cookie[cookie_name].value
                session = Session.objects.get(session_key=session_key)
                user_id = session.get_decoded().get(SESSION_KEY)
                user = User.objects.get(id=user_id)
            except (ImportError, KeyError, ObjectDoesNotExist):
                return False
        return True

Response: Check if the user is authenticated.


Instruction:
Here you have a function in Python 3, explain what it does
def forwards(self, orm):
        "Write your forwards methods here."
        # Note: Remember to use orm['appname.ModelName'] rather than "from appname.models..."
        if not db.dry_run:
            orm['gnotty.IRCMessage'].objects.filter(message="joins").update(join_or_leave=True)
            orm['gnotty.IRCMessage'].objects.filter(message="leaves").update(join_or_leave=True)

Response: Write your forwards methods here.


Instruction:
Can you implement a function in Python 3 that
converts a list of permissions to legacy acl format kept for bw. compatibility

Response:
def permission_to_04_acls(permissions):
    """
    Legacy acl format kept for bw. compatibility
    :param permissions:
    :return:
    """
    acls = []
    for perm in permissions:
        if perm.type == "user":
            acls.append((perm.user.id, perm.perm_name))
        elif perm.type == "group":
            acls.append(("group:%s" % perm.group.id, perm.perm_name))
    return acls


Instruction:
Can you create a Python 3 function that
converts a list of permissions in a format understood by pyramid

Response:
def permission_to_pyramid_acls(permissions):
    """
    Returns a list of permissions in a format understood by pyramid
    :param permissions:
    :return:
    """
    acls = []
    for perm in permissions:
        if perm.type == "user":
            acls.append((Allow, perm.user.id, perm.perm_name))
        elif perm.type == "group":
            acls.append((Allow, "group:%s" % perm.group.id, perm.perm_name))
    return acls


Instruction:
Can you generate a brief explanation for the following Python 3 code
def ChunkBy(self, f=None):
    if f is None:
        return _chunk(self)
    if is_to_destruct(f):
        f = destruct_func(f)
    return _chunk(self, f)

Response: Returns a list of items in a sequence that is a list of items.


Instruction:
Can you tell what is the following Python 3 function doing
def GroupBy(self: Iterable, f=None):
    if f and is_to_destruct(f):
        f = destruct_func(f)
    return _group_by(self, f)

Response: Groups the elements of the list by a function.


Instruction:
How would you explain what the following Python 3 function does
def Take(self: Iterable, n):

    for i, e in enumerate(self):
        if i == n:
            break
        yield e

Response: Take the n elements from the list.


Instruction:
Can you create a Python 3 function that
yields the elements from self while f returns True.

Response:
def TakeWhile(self: Iterable, f):
    """
    [
        {
            'self': [1, 2, 3, 4, 5],
            'f': lambda x: x < 4,
            'assert': lambda ret: list(ret)  == [1, 2, 3]
         }
    ]
    """
    if is_to_destruct(f):
        f = destruct_func(f)

    for e in self:
        if not f(e):
            break
        yield e


Instruction:
Implement a Python 3 function for
dropping n items from the list.

Response:
def Drop(self: Iterable, n):
    """
    [
        {
            'self': [1, 2, 3, 4, 5],
            'n': 3,
            'assert': lambda ret: list(ret) == [1, 2]
         }
    ]
    """
    con = tuple(self)
    n = len(con) - n
    if n <= 0:
        yield from con
    else:
        for i, e in enumerate(con):
            if i == n:
                break
            yield e


Instruction:
Can you create a Python 3 function that
skips the first n items from the list.

Response:
def Skip(self: Iterable, n):
    """
        [
            {
                'self': [1, 2, 3, 4, 5],
                'n': 3,
                'assert': lambda ret: list(ret) == [4, 5]
             }
        ]
        """
    con = iter(self)
    for i, _ in enumerate(con):
        if i == n:
            break
    return con


Instruction:
Explain what the following Python 3 code does
def Shift(self, n):
    headn = tuple(Take(self, n))
    yield from self
    yield from headn

Response: Yields the n most recent elements of the sequence.


Instruction:
Create a Python 3 function to
return a generator that returns the elements of self and others.

Response:
def Concat(self: Iterable, *others):
    """
    [
        {
            'self': [1, 2, 3],
            ':args': [[4, 5, 6], [7, 8, 9]],
            'assert': lambda ret: list(ret) == [1, 2, 3, 4, 5, 6, 7, 8, 9]
         }
    ]
    """
    return concat_generator(self, *[unbox_if_flow(other) for other in others])


Instruction:
Can you create a Python 3 function that
checks if an attribute is being accessed throught the default queryset manager ie : MyClass. objects. filter ( some = value

Response:
def _called_thru_default_qs(self, node):
        """Checks if an attribute is being accessed throught the default
        queryset manager, ie: MyClass.objects.filter(some='value')"""
        last_child = node.last_child()
        if not last_child:
            return False

        # the default qs manager is called 'objects', we check for it here
        attrname = getattr(last_child, 'attrname', None)
        if attrname != 'objects':
            return False

        base_cls = last_child.last_child()
        base_classes = DOCUMENT_BASES
        for cls in base_cls.inferred():
            if node_is_subclass(cls, *base_classes):
                return True

        return False


Instruction:
Implement a function in Python 3 to
get all objects of specific type

Response:
def all(cls, klass, db_session=None):
        """
        returns all objects of specific type - will work correctly with
        sqlalchemy inheritance models, you should normally use models
        base_query()  instead of this function its for bw. compat purposes

        :param klass:
        :param db_session:
        :return:
        """
        db_session = get_db_session(db_session)
        return db_session.query(klass)


Instruction:
Explain what the following Python 3 code does
def base_query(cls, db_session=None):
        db_session = get_db_session(db_session)
        return db_session.query(cls.model)

Response: returns base query for specific service


Instruction:
Can you generate the documentation for the following Python 3 function
def on(event, *args, **kwargs):
    def wrapper(func):
        for i, arg in args:
            kwargs[i] = arg
        func.event = Event(event, kwargs)
        return func
    return wrapper

Response: A decorator for bot mixins.


Instruction:
Can you write a function in Python 3 where it
returns a dictionary of keys and values corresponding to this model s neccesary necces

Response:
def get_dict(self, exclude_keys=None, include_keys=None):
        """
        return dictionary of keys and values corresponding to this model's
        data - if include_keys is null the function will return all keys

        :param exclude_keys: (optional) is a list of columns from model that
        should not be returned by this function
        :param include_keys: (optional) is a list of columns from model that
        should be returned by this function
        :return:
        """
        d = {}
        exclude_keys_list = exclude_keys or []
        include_keys_list = include_keys or []
        for k in self._get_keys():
            if k not in exclude_keys_list and (
                k in include_keys_list or not include_keys
            ):
                d[k] = getattr(self, k)
        return d


Instruction:
Here you have a function in Python 3, explain what it does
def get_appstruct(self):
        result = []
        for k in self._get_keys():
            result.append((k, getattr(self, k)))
        return result

Response:
return list of tuples keys and values corresponding to this model s
        data


Instruction:
How would you code a function in Python 3 to
populate the instance properties of the object with the values from the passed dictionary.

Response:
def populate_obj(self, appstruct, exclude_keys=None, include_keys=None):
        """
        updates instance properties *for column names that exist*
        for this model and are keys present in passed dictionary

        :param appstruct: (dictionary)
        :param exclude_keys: (optional) is a list of columns from model that
        should not be updated by this function
        :param include_keys: (optional) is a list of columns from model that
        should be updated by this function
        :return:
        """
        exclude_keys_list = exclude_keys or []
        include_keys_list = include_keys or []
        for k in self._get_keys():
            if (
                k in appstruct
                and k not in exclude_keys_list
                and (k in include_keys_list or not include_keys)
            ):
                setattr(self, k, appstruct[k])


Instruction:
How would you code a function in Python 3 to
populate the instance properties with the values from the passed object.

Response:
def populate_obj_from_obj(self, instance, exclude_keys=None, include_keys=None):
        """
        updates instance properties *for column names that exist*
        for this model and are properties present in passed dictionary

        :param instance:
        :param exclude_keys: (optional) is a list of columns from model that
        should not be updated by this function
        :param include_keys: (optional) is a list of columns from model that
        should be updated by this function
        :return:
        """
        exclude_keys_list = exclude_keys or []
        include_keys_list = include_keys or []
        for k in self._get_keys():
            if (
                hasattr(instance, k)
                and k not in exclude_keys_list
                and (k in include_keys_list or not include_keys)
            ):
                setattr(self, k, getattr(instance, k))


Instruction:
Can you generate the documentation for the following Python 3 function
def delete(self, db_session=None):
        db_session = get_db_session(db_session, self)
        db_session.delete(self)

Response: Deletes the object from storage


Instruction:
Write a Python 3 script to
turn off the HX711

Response:
def power_down(self):
        """
        turn off the HX711
        :return: always True
        :rtype bool
        """
        GPIO.output(self._pd_sck, False)
        GPIO.output(self._pd_sck, True)
        time.sleep(0.01)
        return True


Instruction:
How would you implement a function in Python 3 that
powers up the HX711

Response:
def power_up(self):
        """
        power up the HX711

        :return: always True
        :rtype bool
        """
        GPIO.output(self._pd_sck, False)
        time.sleep(0.01)
        return True


Instruction:
How would you explain what the following Python 3 function does
def reset(self):
        logging.debug("power down")
        self.power_down()
        logging.debug("power up")
        self.power_up()
        logging.debug("read some raw data")
        result = self.get_raw_data(6)
        if result is False:
            raise GenericHX711Exception("failed to reset HX711")
        else:
            return True

Response: reset the HX711 and prepare it for the next reading


Instruction:
Here you have a function in Python 3, explain what it does
def _validate_measure_count(self, times):
        if not self.min_measures <= times <= self.max_measures:
            raise ParameterValidationError(
                "{times} is not within the borders defined in the class".format(
                    times=times
                )
            )

Response:
check if the count of the class is within the borders defined in the class
COOKIE


Instruction:
Given the following Python 3 function, write the documentation
def _validate_gain_A_value(self, gain_A):
        if gain_A not in self._valid_gains_for_channel_A:
            raise ParameterValidationError("{gain_A} is not a valid gain".format(gain_A=gain_A))

Response: Validate a given value for gain_A.


Instruction:
Can you write a function in Python 3 where it
checks if the data is ready for reading.

Response:
def _ready(self):
        """
        check if ther is som data is ready to get read.
        :return True if there is some date
        :rtype bool
        """
        # if DOUT pin is low, data is ready for reading
        _is_ready = GPIO.input(self._dout) == 0
        logging.debug("check data ready for reading: {result}".format(
            result="YES" if _is_ready is True else "NO"
        ))
        return _is_ready


Instruction:
Can you create a Python 3 function that
sets the channel gain for the current HX711.

Response:
def _set_channel_gain(self, num):
        """
        Finish data transmission from HX711 by setting
        next required gain and channel

        Only called from the _read function.
        :param num: how often so do the set (1...3)
        :type num: int
        :return True on success
        :rtype bool
        """
        if not 1 <= num <= 3:
            raise AttributeError(
                """"num" has to be in the range of 1 to 3"""
            )

        for _ in range(num):
            logging.debug("_set_channel_gain called")
            start_counter = time.perf_counter()  # start timer now.
            GPIO.output(self._pd_sck, True)  # set high
            GPIO.output(self._pd_sck, False)  # set low
            end_counter = time.perf_counter()  # stop timer
            time_elapsed = float(end_counter - start_counter)
            # check if HX711 did not turn off...
            # if pd_sck pin is HIGH for 60 µs and more the HX 711 enters power down mode.
            if time_elapsed >= 0.00006:
                logging.warning(
                    'setting gain and channel took more than 60µs. '
                    'Time elapsed: {:0.8f}'.format(time_elapsed)
                )
                # hx711 has turned off. First few readings are inaccurate.
                # Despite this reading was ok and data can be used.
                result = self.get_raw_data(times=6)  # set for the next reading.
                if result is False:
                    raise GenericHX711Exception("channel was not set properly")
        return True


Instruction:
Can you generate a brief explanation for the following Python 3 code
def _read(self, max_tries=40):
        # start by setting the pd_sck to false
        GPIO.output(self._pd_sck, False)
        # init the counter
        ready_counter = 0

        # loop until HX711 is ready
        # halt when maximum number of tires is reached
        while self._ready() is False:
            time.sleep(0.01)  # sleep for 10 ms before next try
            ready_counter += 1  # increment counter
            # check loop count
            # and stop when defined maximum is reached
            if ready_counter >= max_tries:
                logging.debug('self._read() not ready after 40 trials\n')
                return False

        data_in = 0  # 2's complement data from hx 711
        # read first 24 bits of data
        for i in range(24):
            # start timer
            start_counter = time.perf_counter()
            # request next bit from HX711
            GPIO.output(self._pd_sck, True)
            GPIO.output(self._pd_sck, False)
            # stop timer
            end_counter = time.perf_counter()
            time_elapsed = float(end_counter - start_counter)

            # check if the hx 711 did not turn off:
            # if pd_sck pin is HIGH for 60 us and more than the HX 711 enters power down mode.
            if time_elapsed >= 0.00006:
                logging.debug('Reading data took longer than 60µs. Time elapsed: {:0.8f}'.format(time_elapsed))
                return False

            # Shift the bits as they come to data_in variable.
            # Left shift by one bit then bitwise OR with the new bit.
            data_in = (data_in << 1) | GPIO.input(self._dout)

        if self.channel == 'A' and self.channel_a_gain == 128:
            self._set_channel_gain(num=1)  # send one bit
        elif self.channel == 'A' and self.channel_a_gain == 64:
            self._set_channel_gain(num=3)  # send three bits
        else:
            self._set_channel_gain(num=2)  # send two bits

        logging.debug('Binary value as it has come: ' + str(bin(data_in)))

        # check if data is valid
        # 0x800000 is the lowest
        # 0x7fffff is the highest possible value from HX711
        if data_in == 0x7fffff or data_in == 0x800000:
            logging.debug('Invalid data detected: ' + str(data_in))
            return False

        # calculate int from 2's complement
        signed_data = 0
        if (data_in & 0x800000):  # 0b1000 0000 0000 0000 0000 0000 check if the sign bit is 1. Negative number.
            signed_data = -((data_in ^ 0xffffff) + 1)  # convert from 2's complement to int
        else:  # else do not do anything the value is positive number
            signed_data = data_in

        logging.debug('Converted 2\'s complement value: ' + str(signed_data))

        return signed_data

Response: read the bit stream from HX711 and convert it to an int.


Instruction:
Write a Python 3 script for
getting the raw data of the current log entry

Response:
def get_raw_data(self, times=5):
        """
        do some readings and aggregate them using the defined statistics function

        :param times: how many measures to aggregate
        :type times: int
        :return: the aggregate of the measured values
        :rtype float
        """

        self._validate_measure_count(times)

        data_list = []
        while len(data_list) < times:
            data = self._read()
            if data not in [False, -1]:
                data_list.append(data)

        return data_list