Instruction:
Implement a Python 3 function for
updating the cache stats.

Response:
def _update_cache_stats(self, key, result):
        """
        Update the cache stats.

        If no cache-result is specified, we iniitialize the key.
        Otherwise, we increment the correct cache-result.

        Note the behavior for expired.  A client can be expired and the key
        still exists.
        """
        if result is None:
            self._CACHE_STATS['access_stats'].setdefault(key,
                                         {'hit': 0, 'miss': 0, 'expired': 0})
        else:
            self._CACHE_STATS['access_stats'][key][result] +=1


Instruction:
Can you implement a function in Python 3 that
gets access details in cache.

Response:
def get_access_details(self, key=None):
        """Get access details in cache."""
        if key in self._CACHE_STATS:
            return self._CACHE_STATS['access_stats'][key]
        else:
            return self._CACHE_STATS['access_stats']


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_stats(self):
        expired = sum([x['expired'] for _, x in
                       self._CACHE_STATS['access_stats'].items()])
        miss = sum([x['miss'] for _, x in
                    self._CACHE_STATS['access_stats'].items()])

        hit = sum([x['hit'] for _, x in
                       self._CACHE_STATS['access_stats'].items()])
        return {
            'totals': {
                'keys': len(self._CACHE_STATS['access_stats']),
                'expired': expired,
                'miss': miss,
                'hit': hit,
                }
        }

Response: Get general stats for the cache.


Instruction:
How would you code a function in Python 3 to
build a dictionary of Security Groups from a name ARN or dict of Security Groups.

Response:
def get_security_group(sg_obj, flags=FLAGS.ALL, **conn):
    """
    Orchestrates calls to build a Security Group in the following format:

    {
        "Description": ...,
        "GroupName": ...,
        "IpPermissions" ...,
        "OwnerId" ...,
        "GroupId" ...,
        "IpPermissionsEgress" ...,
        "VpcId" ...
    }
    Args:
        sg_obj: name, ARN, or dict of Security Group
        flags: Flags describing which sections should be included in the return value. Default ALL

    Returns:
        dictionary describing the requested Security Group
    """
    if isinstance(sg_obj, string_types):
        group_arn = ARN(sg_obj)
        if group_arn.error:
            sg_obj = {'GroupId': sg_obj}
        else:
            sg_obj = {'GroupId': group_arn.parsed_name}

    return registry.build_out(flags, sg_obj, **conn)


Instruction:
Can you generate the documentation for the following Python 3 function
def get_user(user, flags=FLAGS.ALL, **conn):
    user = modify(user, output='camelized')
    _conn_from_args(user, conn)
    return registry.build_out(flags, start_with=user, pass_datastructure=True, **conn)

Response: Builds a user dictionary from a user dict


Instruction:
Explain what the following Python 3 code does
def get_all_users(flags=FLAGS.ACCESS_KEYS | FLAGS.MFA_DEVICES | FLAGS.LOGIN_PROFILE | FLAGS.SIGNING_CERTIFICATES,
                  **conn):
    """
    Returns a list of Users represented as dictionary below:

    {
        "Arn": ...,
        "AccessKeys": ...,
        "CreateDate": ...,  # str
        "InlinePolicies": ...,
        "ManagedPolicies": ...,
        "MFADevices": ...,
        "Path": ...,
        "UserId": ...,
        "UserName": ...,
        "SigningCerts": ...
    }

    :param flags:
    :param conn: dict containing enough information to make a connection to the desired account.
    :return: list of dicts containing fully built out user.
    """

    users = []
    account_users = get_account_authorization_details('User', **conn)

    for user in account_users:
        temp_user = {
            'Arn': user['Arn'],
            'CreateDate': get_iso_string(user['CreateDate']),
            'GroupList': user['GroupList'],
            'InlinePolicies': user['UserPolicyList'],
            'ManagedPolicies': [
                {
                  "name": x['PolicyName'],
                  "arn": x['PolicyArn']
                } for x in user['AttachedManagedPolicies']
            ],
            'Path': user['Path'],
            'UserId': user['UserId'],
            'UserName': user['UserName']
        }

        user = modify(temp_user, output='camelized')
        _conn_from_args(user, conn)
        users.append(registry.build_out(flags, start_with=user, pass_datastructure=True, **conn))

    return users

Response: Returns a list of Users represented as dictionary below.


Instruction:
Create a Python 3 function for
getting the Flow Logs for a VPC

Response:
def get_vpc_flow_logs(vpc, **conn):
    """Gets the VPC Flow Logs for a VPC"""
    fl_result = describe_flow_logs(Filters=[{"Name": "resource-id", "Values": [vpc["id"]]}], **conn)

    fl_ids = []
    for fl in fl_result:
        fl_ids.append(fl["FlowLogId"])

    return fl_ids


Instruction:
Can you tell what is the following Python 3 function doing
def get_classic_link(vpc, **conn):
    result = {}

    try:
        cl_result = describe_vpc_classic_link(VpcIds=[vpc["id"]], **conn)[0]
        result["Enabled"] = cl_result["ClassicLinkEnabled"]

        # Check for DNS as well:
        dns_result = describe_vpc_classic_link_dns_support(VpcIds=[vpc["id"]], **conn)[0]
        result["DnsEnabled"] = dns_result["ClassicLinkDnsSupported"]
    except ClientError as e:
        # This is not supported for all regions.
        if 'UnsupportedOperation' not in str(e):
            raise e

    return result

Response: Gets the Classic Link details about a VPC


Instruction:
Can you tell what is the following Python 3 function doing
def get_internet_gateway(vpc, **conn):
    result = {}
    ig_result = describe_internet_gateways(Filters=[{"Name": "attachment.vpc-id", "Values": [vpc["id"]]}], **conn)

    if ig_result:
        # Only 1 IG can be attached to a VPC:
        result.update({
            "State": ig_result[0]["Attachments"][0]["State"],
            "Id": ig_result[0]["InternetGatewayId"],
            "Tags": ig_result[0].get("Tags", [])
        })

    return result

Response: Gets the Internet Gateway details about a VPC


Instruction:
Given the following Python 3 function, write the documentation
def get_vpc_peering_connections(vpc, **conn):
    accepter_result = describe_vpc_peering_connections(Filters=[{"Name": "accepter-vpc-info.vpc-id",
                                                                 "Values": [vpc["id"]]}], **conn)

    requester_result = describe_vpc_peering_connections(Filters=[{"Name": "requester-vpc-info.vpc-id",
                                                                 "Values": [vpc["id"]]}], **conn)

    # Assuming that there will be no duplicates:
    peer_ids = []
    for peering in accepter_result + requester_result:
        peer_ids.append(peering["VpcPeeringConnectionId"])

    return peer_ids

Response: Gets the Internet Gateway details about a VPC


Instruction:
Here you have a function in Python 3, explain what it does
def get_subnets(vpc, **conn):
    subnets = describe_subnets(Filters=[{"Name": "vpc-id", "Values": [vpc["id"]]}], **conn)

    s_ids = []
    for s in subnets:
        s_ids.append(s["SubnetId"])

    return s_ids

Response: Gets the VPC Subnets


Instruction:
Can you tell what is the following Python 3 function doing
def get_route_tables(vpc, **conn):
    route_tables = describe_route_tables(Filters=[{"Name": "vpc-id", "Values": [vpc["id"]]}], **conn)

    rt_ids = []
    for r in route_tables:
        rt_ids.append(r["RouteTableId"])

    return rt_ids

Response: Gets the VPC Route Tables


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_network_acls(vpc, **conn):
    route_tables = describe_network_acls(Filters=[{"Name": "vpc-id", "Values": [vpc["id"]]}], **conn)

    nacl_ids = []
    for r in route_tables:
        nacl_ids.append(r["NetworkAclId"])

    return nacl_ids

Response: Gets the VPC Network ACLs


Instruction:
How would you code a function in Python 3 to
get the base of a single object.

Response:
def get_base(vpc, **conn):
    """
    The base will return:
    - ARN
    - Region
    - Name
    - Id
    - Tags
    - IsDefault
    - InstanceTenancy
    - CidrBlock
    - CidrBlockAssociationSet
    - Ipv6CidrBlockAssociationSet
    - DhcpOptionsId
    - Attributes
    - _version

    :param bucket_name:
    :param conn:
    :return:
    """
    # Get the base:
    base_result = describe_vpcs(VpcIds=[vpc["id"]], **conn)[0]

    # The name of the VPC is in the tags:
    vpc_name = None
    for t in base_result.get("Tags", []):
        if t["Key"] == "Name":
            vpc_name = t["Value"]

    dhcp_opts = None
    # Get the DHCP Options:
    if base_result.get("DhcpOptionsId"):
        # There should only be exactly 1 attached to a VPC:
        dhcp_opts = describe_dhcp_options(DhcpOptionsIds=[base_result["DhcpOptionsId"]], **conn)[0]["DhcpOptionsId"]

    # Get the Attributes:
    attributes = {}
    attr_vals = [
        ("EnableDnsHostnames", "enableDnsHostnames"),
        ("EnableDnsSupport", "enableDnsSupport")
    ]
    for attr, query in attr_vals:
        attributes[attr] = describe_vpc_attribute(VpcId=vpc["id"], Attribute=query, **conn)[attr]

    vpc.update({
        'name': vpc_name,
        'region': conn["region"],
        'tags': base_result.get("Tags", []),
        'is_default': base_result["IsDefault"],
        'instance_tenancy': base_result["InstanceTenancy"],
        'dhcp_options_id': dhcp_opts,
        'cidr_block': base_result["CidrBlock"],
        'cidr_block_association_set': base_result.get("CidrBlockAssociationSet", []),
        'ipv6_cidr_block_association_set': base_result.get("Ipv6CidrBlockAssociationSet", []),
        'attributes': attributes,
        '_version': 1
    })
    return vpc


Instruction:
Explain what the following Python 3 code does
def get_vpc(vpc_id, flags=FLAGS.ALL, **conn):
    # Is the account number that's passed in the same as in the connection dictionary?
    if not conn.get("account_number"):
        raise CloudAuxException({"message": "Must supply account number in the connection dict to construct "
                                            "the VPC ARN.",
                                 "vpc_id": vpc_id})

    if not conn.get("region"):
        raise CloudAuxException({"message": "Must supply region in the connection dict to construct "
                                            "the VPC ARN.",
                                 "vpc_id": vpc_id})

    start = {
        'arn': "arn:aws:ec2:{region}:{account}:vpc/{vpc_id}".format(region=conn["region"],
                                                                    account=conn["account_number"],
                                                                    vpc_id=vpc_id),
        'id': vpc_id
    }

    return registry.build_out(flags, start_with=start, pass_datastructure=True, **conn)

Response: Returns a new VPC with the specified ID.


Instruction:
How would you implement a function in Python 3 that
gets a client from the GOOGLE_CLIENT_MAP dictionary.

Response:
def get_client(service, service_type='client', **conn_args):
    """
    User function to get the correct client.

    Based on the GOOGLE_CLIENT_MAP dictionary, the return will be a cloud or general
    client that can interact with the desired service.

    :param service: GCP service to connect to. E.g. 'gce', 'iam'
    :type service: ``str``

    :param conn_args: Dictionary of connection arguments.  'project' is required.
                      'user_agent' can be specified and will be set in the client
                       returned.
    :type conn_args: ``dict``

    :return: client_details, client
    :rtype: ``tuple`` of ``dict``, ``object``
    """
    client_details = choose_client(service)
    user_agent = get_user_agent(**conn_args)
    if client_details:
        if client_details['client_type'] == 'cloud':
            client = get_gcp_client(
                mod_name=client_details['module_name'],
                pkg_name=conn_args.get('pkg_name', 'google.cloud'),
                key_file=conn_args.get('key_file', None),
                project=conn_args['project'], user_agent=user_agent)
        else:
            client = get_google_client(
                mod_name=client_details['module_name'],
                key_file=conn_args.get('key_file', None),
                user_agent=user_agent, api_version=conn_args.get('api_version', 'v1'))
    else:
        # There is no client known for this service. We can try the standard API.
        try:
            client = get_google_client(
                mod_name=service, key_file=conn_args.get('key_file', None),
                user_agent=user_agent, api_version=conn_args.get('api_version', 'v1'))
        except Exception as e:
            raise e

    return client_details, client


Instruction:
Make a summary of the following Python 3 code
def get_gcp_client(**kwargs):
    return _gcp_client(project=kwargs['project'], mod_name=kwargs['mod_name'],
                       pkg_name=kwargs.get('pkg_name', 'google.cloud'),
                       key_file=kwargs.get('key_file', None),
                       http_auth=kwargs.get('http', None),
                       user_agent=kwargs.get('user_agent', None))

Response: Public GCP client builder.


Instruction:
How would you implement a function in Python 3 that
googles http_auth helper. If key_file is not specified, default credentials will be used. If scopes is specified (and key_file), will be used instead of DEFAULT_SCOPES :param key_file: path to key file to use. Default is None :type key_file: ``str`` :param scopes: scopes to set. Default is DEFAUL_SCOPES :type scopes: ``list`` :param user_agent: User Agent string to use in requests. Default is None. :type http_auth: ``str`` or None :return: HTTPLib2 authorized client. :rtype: :class: `HTTPLib2`

Response:
def _googleauth(key_file=None, scopes=[], user_agent=None):
    """
    Google http_auth helper.

    If key_file is not specified, default credentials will be used.

    If scopes is specified (and key_file), will be used instead of DEFAULT_SCOPES

    :param key_file: path to key file to use. Default is None
    :type key_file: ``str``

    :param scopes: scopes to set.  Default is DEFAUL_SCOPES
    :type scopes: ``list``

    :param user_agent: User Agent string to use in requests. Default is None.
    :type http_auth: ``str`` or None

    :return: HTTPLib2 authorized client.
    :rtype: :class: `HTTPLib2`
    """
    if key_file:
        if not scopes:
            scopes = DEFAULT_SCOPES
        creds = ServiceAccountCredentials.from_json_keyfile_name(key_file,
                                                                 scopes=scopes)
    else:
        creds = GoogleCredentials.get_application_default()
    http = Http()
    if user_agent:
        http = set_user_agent(http, user_agent)
    http_auth = creds.authorize(http)
    return http_auth


Instruction:
Given the following Python 3 function, write the documentation
def _build_google_client(service, api_version, http_auth):
    client = build(service, api_version, http=http_auth)
    return client

Response: Build a Google client for the given service and API version.


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

    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            start_time = time.time()
            result = f(*args, **kwargs)
            end_time = time.time()
            strkey = _build_key(f.__name__, args, kwargs)
            _GCP_STATS.setdefault(strkey, []).append(end_time - start_time)
            return result

        return decorated_function

    return decorator

Response: Decorator to collect stats for a single object in a cluster.


Instruction:
Given the following Python 3 function, write the documentation
def gcp_cache(future_expiration_minutes=15):

    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            strkey = _build_key(f.__name__, args, kwargs)
            cached_result = _GCP_CACHE.get(strkey)
            if cached_result:
                return cached_result
            else:
                result = f(*args, **kwargs)
                _GCP_CACHE.insert(strkey, result, future_expiration_minutes)
            return result

        return decorated_function

    return decorator

Response: Decorator for GCP cache functions.


Instruction:
Here you have a function in Python 3, explain what it does
def iter_project(projects, key_file=None):

    def decorator(func):
        @wraps(func)
        def decorated_function(*args, **kwargs):
            item_list = []
            exception_map = {}
            for project in projects:
                if isinstance(project, string_types):
                    kwargs['project'] = project
                    if key_file:
                        kwargs['key_file'] = key_file
                elif isinstance(project, dict):
                    kwargs['project'] = project['project']
                    kwargs['key_file'] = project['key_file']
                itm, exc = func(*args, **kwargs)
                item_list.extend(itm)
                exception_map.update(exc)
            return (item_list, exception_map)

        return decorated_function

    return decorator

Response: Decorator for iterating over a list of projects.


Instruction:
Can you tell what is the following Python 3 function doing
def get_creds_from_kwargs(kwargs):
    creds = {
        'key_file': kwargs.pop('key_file', None),
        'http_auth': kwargs.pop('http_auth', None),
        'project': kwargs.get('project', None),
        'user_agent': kwargs.pop('user_agent', None),
        'api_version': kwargs.pop('api_version', 'v1')
    }
    return (creds, kwargs)

Response: Helper to get creds out of kwargs.


Instruction:
How would you implement a function in Python 3 that
manipulates connection keywords. Modifieds keywords based on connection type. There is an assumption here that the client has already been created and that these keywords are being passed into methods for interacting with various services. Current modifications: - if conn_type is not cloud and module is 'compute', then rewrite project as name. - if conn_type is cloud and module is 'storage', then remove 'project' from dict. :param conn_type: E.g. 'cloud' or 'general' :type conn_type: ``str`` :param kwargs: Dictionary of keywords sent in by user. :type kwargs: ``dict`` :param module_name: Name of specific module that will be loaded. Default is None. :type conn_type: ``str`` or None :returns kwargs with client and module specific changes :rtype: ``dict``

Response:
def rewrite_kwargs(conn_type, kwargs, module_name=None):
    """
    Manipulate connection keywords.

    Modifieds keywords based on connection type.

    There is an assumption here that the client has
    already been created and that these keywords are being
    passed into methods for interacting with various services.

    Current modifications:
    - if conn_type is not cloud and module is 'compute',
      then rewrite project as name.
    - if conn_type is cloud and module is 'storage',
      then remove 'project' from dict.

    :param conn_type: E.g. 'cloud' or 'general'
    :type conn_type: ``str``

    :param kwargs: Dictionary of keywords sent in by user.
    :type kwargs: ``dict``

    :param module_name: Name of specific module that will be loaded.
                        Default is None.
    :type conn_type: ``str`` or None

    :returns kwargs with client and module specific changes
    :rtype: ``dict``
    """
    if conn_type != 'cloud' and module_name != 'compute':
        if 'project' in kwargs:
            kwargs['name'] = 'projects/%s' % kwargs.pop('project')
    if conn_type == 'cloud' and module_name == 'storage':
        if 'project' in kwargs:
            del kwargs['project']
    return kwargs


Instruction:
Can you tell what is the following Python 3 function doing
def gce_list_aggregated(service=None, key_name='name', **kwargs):
    resp_list = []
    req = service.aggregatedList(**kwargs)

    while req is not None:
        resp = req.execute()
        for location, item in resp['items'].items():
            if key_name in item:
                resp_list.extend(item[key_name])

        req = service.aggregatedList_next(previous_request=req,
                                          previous_response=resp)
    return resp_list

Response: General aggregated list function for the GCE service.


Instruction:
Here you have a function in Python 3, explain what it does
def gce_list(service=None, **kwargs):
    resp_list = []
    req = service.list(**kwargs)

    while req is not None:
        resp = req.execute()
        for item in resp.get('items', []):
            resp_list.append(item)
        req = service.list_next(previous_request=req, previous_response=resp)
    return resp_list

Response: General list function for the GCE service.


Instruction:
Can you implement a function in Python 3 that
retrieves detailed cache information.

Response:
def get_cache_access_details(key=None):
    """Retrieve detailed cache information."""
    from cloudaux.gcp.decorators import _GCP_CACHE
    return _GCP_CACHE.get_access_details(key=key)


Instruction:
How would you code a function in Python 3 to
get default User Agent string.

Response:
def get_user_agent_default(pkg_name='cloudaux'):
    """
    Get default User Agent String.

    Try to import pkg_name to get an accurate version number.

    return: string
    """
    version = '0.0.1'
    try:
        import pkg_resources
        version = pkg_resources.get_distribution(pkg_name).version
    except pkg_resources.DistributionNotFound:
        pass
    except ImportError:
        pass

    return 'cloudaux/%s' % (version)


Instruction:
Write a Python 3 script for
returning a dictionary describing the ALB in the format of a V2 ALB.

Response:
def get_elbv2(alb, flags=FLAGS.ALL, **conn):
    """
    Fully describes an ALB (ELBv2).

    :param alb: Could be an ALB Name, ALB ARN, or a dictionary. Likely the return value from a previous call to describe_load_balancers. At a minimum, must contain a key titled 'LoadBalancerArn'.
    :param flags: Flags describing which sections should be included in the return value. Default is FLAGS.ALL.
    :return: Returns a dictionary describing the ALB with the fields described in the flags parameter.
    """
    # Python 2 and 3 support:
    try:
        basestring
    except NameError as _:
        basestring = str

    if isinstance(alb, basestring):
        from cloudaux.orchestration.aws.arn import ARN
        alb_arn = ARN(alb)
        if alb_arn.error:
            alb = dict(LoadBalancerName=alb)
        else:
            alb = dict(LoadBalancerArn=alb)

    return registry.build_out(flags, start_with=alb, pass_datastructure=True, **conn)


Instruction:
Can you generate the documentation for the following Python 3 function
def get_event(rule, flags=FLAGS.ALL, **conn):
    # Python 2 and 3 support:
    try:
        basestring
    except NameError as _:
        basestring = str

    # If string is passed in, determine if it's a name or ARN. Build a dict.
    if isinstance(rule, basestring):
        rule_arn = ARN(rule)
        if rule_arn.error:
            rule_name = rule
        else:
            rule_name = rule_arn.name

    rule = describe_rule(Name=rule_name, **conn)

    return registry.build_out(flags, rule, **conn)

Response: Given a string or a string representing a CloudWatch Event Rule returns a dict containing all the required information for building an event rule.


Instruction:
How would you implement a function in Python 3 that
lists all rules in the current language.

Response:
def list_rules(client=None, **kwargs):
    """
    NamePrefix='string'
    """
    result = client.list_rules(**kwargs)
    if not result.get("Rules"):
        result.update({"Rules": []})

    return result


Instruction:
Given the following Python 3 function, write the documentation
def list_targets_by_rule(client=None, **kwargs):
    result = client.list_targets_by_rule(**kwargs)
    if not result.get("Targets"):
        result.update({"Targets": []})

    return result

Response: List the targets for a given rule.


Instruction:
How would you code a function in Python 3 to
get a specific image from the registry.

Response:
def get_image(image_id, flags=FLAGS.ALL, **conn):
    """
    Orchestrates all the calls required to fully build out an EC2 Image (AMI, AKI, ARI)

    {
        "Architecture": "x86_64",
        "Arn": "arn:aws:ec2:us-east-1::image/ami-11111111",
        "BlockDeviceMappings": [],
        "CreationDate": "2013-07-11T16:04:06.000Z",
        "Description": "...",
        "Hypervisor": "xen",
        "ImageId": "ami-11111111",
        "ImageLocation": "111111111111/...",
        "ImageType": "machine",
        "KernelId": "aki-88888888",
        "LaunchPermissions": [],
        "Name": "...",
        "OwnerId": "111111111111",
        "ProductCodes": [],
        "Public": false,
        "RamdiskId": {},
        "RootDeviceName": "/dev/sda1",
        "RootDeviceType": "ebs",
        "SriovNetSupport": "simple",
        "State": "available",
        "Tags": [],
        "VirtualizationType": "hvm",
        "_version": 1
    }

    :param image_id: str ami id
    :param flags: By default, set to ALL fields
    :param conn: dict containing enough information to make a connection to the desired account.
    Must at least have 'assume_role' key.
    :return: dict containing a fully built out image.
    """
    image = dict(ImageId=image_id)
    conn['region'] = conn.get('region', 'us-east-1')
    return registry.build_out(flags, image, **conn)


Instruction:
Can you implement a function in Python 3 that
wraps for the function that returns a single instance of the class.

Response:
def call(self, function_expr, **kwargs):
        """
        cloudaux = CloudAux(
            **{'account_number': '000000000000',
               'assume_role': 'role_name',
               'session_name': 'testing',
               'region': 'us-east-1',
               'tech': 'kms',
               'service_type': 'client'
            })

        cloudaux.call("list_aliases")
        cloudaux.call("kms.client.list_aliases")
        """
        if '.' in function_expr:
            tech, service_type, function_name = function_expr.split('.')
        else:
            tech = self.conn_details.get('tech')
            service_type = self.conn_details.get('service_type', 'client')
            function_name = function_expr

        @sts_conn(tech, service_type=service_type)
        def wrapped_method(function_name, **nargs):
            service_type = nargs.pop(nargs.pop('service_type', 'client'))
            return getattr(service_type, function_name)(**nargs)

        kwargs.update(self.conn_details)
        if 'tech' in kwargs:
            del kwargs['tech']
        return wrapped_method(function_name, **kwargs)


Instruction:
Can you generate the documentation for the following Python 3 function
def list_buckets(client=None, **kwargs):
    buckets = client.list_buckets(**kwargs)
    return [b.__dict__ for b in buckets]

Response: List all buckets in a project.


Instruction:
Write a Python 3 function for
listing objects in a bucket.

Response:
def list_objects_in_bucket(**kwargs):
    """
    List objects in bucket.

    :param Bucket: name of bucket
    :type Bucket: ``str``

    :returns list of objects in bucket
    :rtype: ``list``
    """
    bucket = get_bucket(**kwargs)
    if bucket:
        return [o for o in bucket.list_blobs()]
    else:
        return None


Instruction:
Can you implement a function in Python 3 that
modifies each key in the item dictionary based on the func passed in.

Response:
def _modify(item, func):
    """
    Modifies each item.keys() string based on the func passed in.
    Often used with inflection's camelize or underscore methods.

    :param item: dictionary representing item to be modified
    :param func: function to run on each key string
    :return: dictionary where each key has been modified by func.
    """
    result = dict()
    for key in item:
        result[func(key)] = item[key]
    return result


Instruction:
Create a Python 3 function for
modifying the item in the base language.

Response:
def modify(item, output='camelized'):
    """
    Calls _modify and either passes the inflection.camelize method or the inflection.underscore method.

    :param item: dictionary representing item to be modified
    :param output: string 'camelized' or 'underscored'
    :return:
    """
    if output == 'camelized':
        return _modify(item, camelize)
    elif output == 'underscored':
        return _modify(item, underscore)


Instruction:
Write a Python 3 function that can
retrieve the currently active policy version document for every managed policy that is attached to the role.

Response:
def get_role_managed_policy_documents(role, client=None, **kwargs):
    """Retrieve the currently active policy version document for every managed policy that is attached to the role."""
    policies = get_role_managed_policies(role, force_client=client)

    policy_names = (policy['name'] for policy in policies)
    delayed_gmpd_calls = (delayed(get_managed_policy_document)(policy['arn'], force_client=client) for policy
                          in policies)
    policy_documents = Parallel(n_jobs=20, backend="threading")(delayed_gmpd_calls)

    return dict(zip(policy_names, policy_documents))


Instruction:
How would you explain what the following Python 3 function does
def get_managed_policy_document(policy_arn, policy_metadata=None, client=None, **kwargs):
    if not policy_metadata:
        policy_metadata = client.get_policy(PolicyArn=policy_arn)

    policy_document = client.get_policy_version(PolicyArn=policy_arn,
                                                VersionId=policy_metadata['Policy']['DefaultVersionId'])
    return policy_document['PolicyVersion']['Document']

Response: Retrieve the currently active policy version document for a given policy.


Instruction:
Here you have a function in Python 3, explain what it does
def get_group(group_name, users=True, client=None, **kwargs):
    # First, make the initial call to get the details for the group:
    result = client.get_group(GroupName=group_name, **kwargs)

    # If we care about the user details, then fetch them:
    if users:
        if result.get('IsTruncated'):
            kwargs_to_send = {'GroupName': group_name}
            kwargs_to_send.update(kwargs)

            user_list = result['Users']
            kwargs_to_send['Marker'] = result['Marker']

            result['Users'] = user_list + _get_users_for_group(client, **kwargs_to_send)

    else:
        result.pop('Users', None)
        result.pop('IsTruncated', None)
        result.pop('Marker', None)

    return result

Response: Get s the details for a group.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_group_policy_document(group_name, policy_name, client=None, **kwargs):
    return client.get_group_policy(GroupName=group_name, PolicyName=policy_name, **kwargs)['PolicyDocument']

Response: Fetches the specific IAM group inline - policy document.


Instruction:
How would you implement a function in Python 3 that
fetches the base IAM Server Certificate.

Response:
def _get_base(server_certificate, **conn):
    """Fetch the base IAM Server Certificate."""
    server_certificate['_version'] = 1

    # Get the initial cert details:
    cert_details = get_server_certificate_api(server_certificate['ServerCertificateName'], **conn)

    if cert_details:
        server_certificate.update(cert_details['ServerCertificateMetadata'])
        server_certificate['CertificateBody'] = cert_details['CertificateBody']
        server_certificate['CertificateChain'] = cert_details.get('CertificateChain', None)

        # Cast dates from a datetime to something JSON serializable.
        server_certificate['UploadDate'] = get_iso_string(server_certificate['UploadDate'])
        server_certificate['Expiration'] = get_iso_string(server_certificate['Expiration'])

    return server_certificate


Instruction:
Explain what the following Python 3 code does
def get_server_certificate(server_certificate, flags=FLAGS.BASE, **conn):
    if not server_certificate.get('ServerCertificateName'):
        raise MissingFieldException('Must include ServerCertificateName.')

    server_certificate = modify(server_certificate, output='camelized')
    _conn_from_args(server_certificate, conn)
    return registry.build_out(flags, start_with=server_certificate, pass_datastructure=True, **conn)

Response: Builds a new server certificate for IAM Users.


Instruction:
Write a Python 3 function for
getting the item from the given object.

Response:
def get_item(item, **kwargs):
    """
    API versioning for each OpenStack service is independent. Generically capture
        the public members (non-routine and non-private) of the OpenStack SDK objects.

    Note the lack of the modify_output decorator. Preserving the field naming allows
        us to reconstruct objects and orchestrate from stored items.
    """
    _item = {}
    for k,v in inspect.getmembers(item, lambda a:not(inspect.isroutine(a))):
        if not k.startswith('_') and not k in ignore_list:
            _item[k] = v

    return sub_dict(_item)


Instruction:
Given the following Python 3 function, write the documentation
def boto3_cached_conn(service, service_type='client', future_expiration_minutes=15, account_number=None,
                      assume_role=None, session_name='cloudaux', region='us-east-1', return_credentials=False,
                      external_id=None, arn_partition='aws'):
    """
    Used to obtain a boto3 client or resource connection.
    For cross account, provide both account_number and assume_role.

    :usage:

    # Same Account:
    client = boto3_cached_conn('iam')
    resource = boto3_cached_conn('iam', service_type='resource')

    # Cross Account Client:
    client = boto3_cached_conn('iam', account_number='000000000000', assume_role='role_name')

    # Cross Account Resource:
    resource = boto3_cached_conn('iam', service_type='resource', account_number='000000000000', assume_role='role_name')

    :param service: AWS service (i.e. 'iam', 'ec2', 'kms')
    :param service_type: 'client' or 'resource'
    :param future_expiration_minutes: Connections will expire from the cache
        when their expiration is within this many minutes of the present time. [Default 15]
    :param account_number: Required if assume_role is provided.
    :param assume_role:  Name of the role to assume into for account described by account_number.
    :param session_name: Session name to attach to requests. [Default 'cloudaux']
    :param region: Region name for connection. [Default us-east-1]
    :param return_credentials: Indicates if the STS credentials should be returned with the client [Default False]
    :param external_id: Optional external id to pass to sts:AssumeRole.
        See https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_create_for-user_externalid.html
    :param arn_partition: Optional parameter to specify other aws partitions such as aws-us-gov for aws govcloud
    :return: boto3 client or resource connection
    """
    key = (
        account_number,
        assume_role,
        session_name,
        external_id,
        region,
        service_type,
        service,
        arn_partition
    )

    if key in CACHE:
        retval = _get_cached_creds(key, service, service_type, region, future_expiration_minutes, return_credentials)
        if retval:
            return retval

    role = None
    if assume_role:
        sts = boto3.session.Session().client('sts')

        # prevent malformed ARN
        if not all([account_number, assume_role]):
            raise ValueError("Account number and role to assume are both required")

        arn = 'arn:{partition}:iam::{0}:role/{1}'.format(
            account_number,
            assume_role,
            partition=arn_partition
        )

        assume_role_kwargs = {
            'RoleArn': arn,
            'RoleSessionName': session_name
        }

        if external_id:
            assume_role_kwargs['ExternalId'] = external_id

        role = sts.assume_role(**assume_role_kwargs)

    if service_type == 'client':
        conn = _client(service, region, role)
    elif service_type == 'resource':
        conn = _resource(service, region, role)

    if role:
        CACHE[key] = role

    if return_credentials:
        return conn, role['Credentials']

    return conn

Response: Creates a boto3 client or resource connection.


Instruction:
Make a summary of the following Python 3 code
def sts_conn(service, service_type='client', future_expiration_minutes=15):
    def decorator(f):
        @wraps(f)
        def decorated_function(*args, **kwargs):
            if kwargs.get("force_client"):
                kwargs[service_type] = kwargs.pop("force_client")
                kwargs.pop("account_number", None)
                kwargs.pop("region", None)
            else:
                kwargs[service_type] = boto3_cached_conn(
                    service,
                    service_type=service_type,
                    future_expiration_minutes=future_expiration_minutes,
                    account_number=kwargs.pop('account_number', None),
                    assume_role=kwargs.pop('assume_role', None),
                    session_name=kwargs.pop('session_name', 'cloudaux'),
                    external_id=kwargs.pop('external_id', None),
                    region=kwargs.pop('region', 'us-east-1'),
                    arn_partition=kwargs.pop('arn_partition', 'aws')
                )
            return f(*args, **kwargs)
        return decorated_function
    return decorator

Response: Decorator for creating STS AssumeRole objects.


Instruction:
How would you code a function in Python 3 to
list the analytics configurations for a bucket.

Response:
def list_bucket_analytics_configurations(client=None, **kwargs):
    """
    Bucket='string'
    """
    result = client.list_bucket_analytics_configurations(**kwargs)
    if not result.get("AnalyticsConfigurationList"):
        result.update({"AnalyticsConfigurationList": []})

    return result


Instruction:
Here you have a function in Python 3, explain what it does
def list_bucket_metrics_configurations(client=None, **kwargs):
    result = client.list_bucket_metrics_configurations(**kwargs)
    if not result.get("MetricsConfigurationList"):
        result.update({"MetricsConfigurationList": []})

    return result

Response: List all metrics configurations for a bucket.


Instruction:
Make a summary of the following Python 3 code
def list_bucket_inventory_configurations(client=None, **kwargs):
    result = client.list_bucket_inventory_configurations(**kwargs)
    if not result.get("InventoryConfigurationList"):
        result.update({"InventoryConfigurationList": []})

    return result

Response: List the inventory configurations for a bucket.


Instruction:
Create a Python 3 function to
build a dictionary containing all the required information about an SQS Queue.

Response:
def get_queue(queue, flags=FLAGS.ALL, **conn):
    """
    Orchestrates all the calls required to fully fetch details about an SQS Queue:

    {
        "Arn": ...,
        "Region": ...,
        "Name": ...,
        "Url": ...,
        "Attributes": ...,
        "Tags": ...,
        "DeadLetterSourceQueues": ...,
        "_version": 1
    }

    :param queue: Either the queue name OR the queue url
    :param flags: By default, set to ALL fields.
    :param conn: dict containing enough information to make a connection to the desired account. Must at least have
                 'assume_role' key.
    :return: dict containing a fully built out SQS queue.
    """
    # Check if this is a Queue URL or a queue name:
    if queue.startswith("https://") or queue.startswith("http://"):
        queue_name = queue
    else:
        queue_name = get_queue_url(QueueName=queue, **conn)

    sqs_queue = {"QueueUrl": queue_name}

    return registry.build_out(flags, sqs_queue, **conn)


Instruction:
Create a Python 3 function to
get a bucket from the registry.

Response:
def get_bucket(bucket_name, include_created=None, flags=FLAGS.ALL ^ FLAGS.CREATED_DATE, **conn):
    """
    Orchestrates all the calls required to fully build out an S3 bucket in the following format:

    {
        "Arn": ...,
        "Name": ...,
        "Region": ...,
        "Owner": ...,
        "Grants": ...,
        "GrantReferences": ...,
        "LifecycleRules": ...,
        "Logging": ...,
        "Policy": ...,
        "Tags": ...,
        "Versioning": ...,
        "Website": ...,
        "Cors": ...,
        "Notifications": ...,
        "Acceleration": ...,
        "Replication": ...,
        "CreationDate": ...,
        "AnalyticsConfigurations": ...,
        "MetricsConfigurations": ...,
        "InventoryConfigurations": ...,
        "_version": 9
    }

    NOTE: "GrantReferences" is an ephemeral field that is not guaranteed to be consistent -- do not base logic off of it

    :param include_created: legacy param moved to FLAGS.
    :param bucket_name: str bucket name
    :param flags: By default, set to ALL fields except for FLAGS.CREATED_DATE as obtaining that information is a slow
                  and expensive process.
    :param conn: dict containing enough information to make a connection to the desired account. Must at least have
                 'assume_role' key.
    :return: dict containing a fully built out bucket.
    """
    if type(include_created) is bool:
        # coerce the legacy param "include_created" into the flags param.
        if include_created:
            flags = flags | FLAGS.CREATED_DATE
        else:
            flags = flags & ~FLAGS.CREATED_DATE

    region = get_bucket_region(Bucket=bucket_name, **conn)
    if not region:
        return dict(Error='Unauthorized')

    conn['region'] = region
    return registry.build_out(flags, bucket_name, **conn)


Instruction:
Explain what the following Python 3 code does
def _get_base(role, **conn):
    base_fields = frozenset(['Arn', 'AssumeRolePolicyDocument', 'Path', 'RoleId', 'RoleName', 'CreateDate'])
    needs_base = False

    for field in base_fields:
        if field not in role:
            needs_base = True
            break

    if needs_base:
        role_name = _get_name_from_structure(role, 'RoleName')
        role = CloudAux.go('iam.client.get_role', RoleName=role_name, **conn)
        role = role['Role']

    # cast CreateDate from a datetime to something JSON serializable.
    role.update(dict(CreateDate=get_iso_string(role['CreateDate'])))
    role['_version'] = 3

    return role

Response: Determine whether the boto get_role call needs to be made or not.


Instruction:
Given the following Python 3 function, write the documentation
def get_role(role, flags=FLAGS.ALL, **conn):
    role = modify(role, output='camelized')
    _conn_from_args(role, conn)
    return registry.build_out(flags, start_with=role, pass_datastructure=True, **conn)

Response: Given a role name and arn returns a dictionary containing the full IAM role.


Instruction:
Here you have a function in Python 3, explain what it does
def get_all_roles(**conn):

    roles = []
    account_roles = get_account_authorization_details('Role', **conn)

    for role in account_roles:
        roles.append(
            {
                'Arn': role['Arn'],
                'AssumeRolePolicyDocument': role['AssumeRolePolicyDocument'],
                'CreateDate': get_iso_string(role['CreateDate']),
                'InlinePolicies': role['RolePolicyList'],
                'InstanceProfiles': [{
                                        'path': ip['Path'],
                                        'instance_profile_name': ip['InstanceProfileName'],
                                        'create_date': get_iso_string(ip['CreateDate']),
                                        'instance_profile_id': ip['InstanceProfileId'],
                                        'arn': ip['Arn']
                                    } for ip in role['InstanceProfileList']],
                'ManagedPolicies': [
                    {
                      "name": x['PolicyName'],
                      "arn": x['PolicyArn']
                    } for x in role['AttachedManagedPolicies']
                ],
                'Path': role['Path'],
                'RoleId': role['RoleId'],
                'RoleName': role['RoleName']
            }
        )

    return roles

Response: Returns a list of Roles represented as the dictionary below.


Instruction:
Create a Python 3 function for
getting the Lambda Function Policies.

Response:
def _get_policy(lambda_function, **conn):
    """Get LambdaFunction Policies.  (there can be many of these!)

    Lambda Function Policies are overly complicated.  They can be attached to a label,
    a version, and there is also a default policy.

    This method attempts to gather all three types.

    AWS returns an exception if the policy requested does not exist.  We catch and ignore these exceptions.
    """
    policies = dict(Versions=dict(), Aliases=dict(), DEFAULT=dict())

    for version in [v['Version'] for v in lambda_function['versions']]:
        try:
            policies['Versions'][version] = get_policy(FunctionName=lambda_function['FunctionName'], Qualifier=version, **conn)
            policies['Versions'][version] = json.loads(policies['Versions'][version])
        except Exception as e:
            pass

    for alias in [v['Name'] for v in lambda_function['aliases']]:
        try:
            policies['Aliases'][alias] = get_policy(FunctionName=lambda_function['FunctionName'], Qualifier=alias, **conn)
            policies['Aliases'][alias] = json.loads(policies['Aliases'][alias])
        except Exception as e:
            pass

    try:
        policies['DEFAULT'] = get_policy(FunctionName=lambda_function['FunctionName'], **conn)
        policies['DEFAULT'] = json.loads(policies['DEFAULT'])
    except Exception as e:
        pass

    return policies


Instruction:
Make a summary of the following Python 3 code
def get_lambda_function(lambda_function, flags=FLAGS.ALL, **conn):
    # Python 2 and 3 support:
    try:
        basestring
    except NameError as _:
        basestring = str

    # If STR is passed in, determine if it's a name or ARN and built a dict.
    if isinstance(lambda_function, basestring):
        lambda_function_arn = ARN(lambda_function)
        if lambda_function_arn.error:
            lambda_function = dict(FunctionName=lambda_function)
        else:
            lambda_function = dict(FunctionName=lambda_function_arn.name, FunctionArn=lambda_function)

    # If an ARN is available, override the account_number/region from the conn dict.
    if 'FunctionArn' in lambda_function:
        lambda_function_arn = ARN(lambda_function['FunctionArn'])
        if not lambda_function_arn.error:
            if lambda_function_arn.account_number:
                conn['account_number'] = lambda_function_arn.account_number
            if lambda_function_arn.region:
                conn['region'] = lambda_function_arn.region

    return registry.build_out(flags, start_with=lambda_function, pass_datastructure=True, **conn)

Response: Fully describes a lambda function.


Instruction:
Can you generate the documentation for the following Python 3 function
def list_items(conn=None, **kwargs):
    return [x for x in getattr( getattr( conn, kwargs.pop('service') ),
                kwargs.pop('generator'))(**kwargs)]

Response: list items in a sequence


Instruction:
Can you tell what is the following Python 3 function doing
def get_serviceaccount(client=None, **kwargs):
    service_account=kwargs.pop('service_account')
    resp = client.projects().serviceAccounts().get(
        name=service_account).execute()
    return resp

Response: Get a single service account.


Instruction:
Implement a function in Python 3 to
get a list of service account keys.

Response:
def get_serviceaccount_keys(client=None, **kwargs):
    """
    service_account='string'
    """
    service_account=kwargs.pop('service_account')
    kwargs['name'] = service_account
    return service_list(client.projects().serviceAccounts().keys(),
                        key_name='keys', **kwargs)


Instruction:
Can you implement a function in Python 3 that
gets IAM policy for a service account.

Response:
def get_iam_policy(client=None, **kwargs):
    """
    service_account='string'
    """
    service_account=kwargs.pop('service_account')
    resp = client.projects().serviceAccounts().getIamPolicy(
        resource=service_account).execute()
    # TODO(supertom): err handling, check if 'bindings' is correct
    if 'bindings' in resp:
        return resp['bindings']
    else:
        return None


Instruction:
Explain what the following Python 3 code does
def get_vault(vault_obj, flags=FLAGS.ALL, **conn):
    if isinstance(vault_obj, string_types):
        vault_arn = ARN(vault_obj)
        if vault_arn.error:
            vault_obj = {'VaultName': vault_obj}
        else:
            vault_obj = {'VaultName': vault_arn.parsed_name}

    return registry.build_out(flags, vault_obj, **conn)

Response: Builds a list of Glacier Vault objects


Instruction:
Make a summary of the following Python 3 code
def get_rules(security_group, **kwargs):
    rules = security_group.pop('security_group_rules',[])
    for rule in rules:
        rule['ip_protocol'] = rule.pop('protocol')
        rule['from_port'] = rule.pop('port_range_max')
        rule['to_port'] = rule.pop('port_range_min')
        rule['cidr_ip'] = rule.pop('remote_ip_prefix')
        rule['rule_type'] = rule.pop('direction')
    security_group['rules'] = sorted(rules)
    return security_group

Response: format the rules to match AWS to support auditor reuse


Instruction:
Write a Python 3 script to
get a security group

Response:
def get_security_group(security_group, flags=FLAGS.ALL, **kwargs):
    result = registry.build_out(flags, start_with=security_group,  pass_datastructure=True, **kwargs)
    """ just store the AWS formatted rules """
    result.pop('security_group_rules', [])
    return result


Instruction:
Explain what the following Python 3 code does
def _conn_from_arn(arn):
    arn = ARN(arn)
    if arn.error:
        raise CloudAuxException('Bad ARN: {arn}'.format(arn=arn))
    return dict(
        account_number=arn.account_number,
    )

Response: Extracts the account number from an ARN and returns a dictionary containing fields such as assume_role session_name region.


Instruction:
Make a summary of the following Python 3 code
def _get_name_from_structure(item, default):
    if item.get(default):
        return item.get(default)

    if item.get('Arn'):
        arn = item.get('Arn')
        item_arn = ARN(arn)
        if item_arn.error:
            raise CloudAuxException('Bad ARN: {arn}'.format(arn=arn))
        return item_arn.parsed_name

    raise MissingFieldException('Cannot extract item name from input: {input}.'.format(input=item))

Response: Try to retrieve the item name from a dict containing the item name and the ARN. If that fails try to parse the from the ARN. If that fails try to retrieve the item name.


Instruction:
Create a Python 3 function for
describing listeners for a load balancer.

Response:
def describe_listeners(load_balancer_arn=None, listener_arns=None, client=None):
    """
    Permission: elasticloadbalancing:DescribeListeners
    """
    kwargs = dict()
    if load_balancer_arn:
        kwargs.update(dict(LoadBalancerArn=load_balancer_arn))
    if listener_arns:
        kwargs.update(dict(ListenerArns=listener_arns))
    return client.describe_listeners(**kwargs)


Instruction:
How would you code a function in Python 3 to
describe rules in a single resource.

Response:
def describe_rules(listener_arn=None, rule_arns=None, client=None):
    """
    Permission: elasticloadbalancing:DescribeRules
    """
    kwargs = dict()
    if listener_arn:
        kwargs.update(dict(ListenerArn=listener_arn))
    if rule_arns:
        kwargs.update(dict(RuleArns=rule_arns))
    return client.describe_rules(**kwargs)['Rules']


Instruction:
Explain what the following Python 3 code does
def describe_target_health(target_group_arn, targets=None, client=None):
    kwargs = dict(TargetGroupArn=target_group_arn)
    if targets:
        kwargs.update(Targets=targets)
    return client.describe_target_health(**kwargs)['TargetHealthDescriptions']

Response: Describe target health for a target group.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_inline_policies(group, **conn):
    policy_list = list_group_policies(group['GroupName'])

    policy_documents = {}

    for policy in policy_list:
        policy_documents[policy] = get_group_policy_document(group['GroupName'], policy, **conn)

    return policy_documents

Response: Get the inline policies for the group.


Instruction:
Create a Python 3 function to
get a list of the managed policy names that are attached to the group.

Response:
def get_managed_policies(group, **conn):
    """Get a list of the managed policy names that are attached to the group."""
    managed_policies = list_attached_group_managed_policies(group['GroupName'], **conn)

    managed_policy_names = []

    for policy in managed_policies:
        managed_policy_names.append(policy['PolicyName'])

    return managed_policy_names


Instruction:
Create a Python 3 function for
getting a list of the usernames that are a part of this group.

Response:
def get_users(group, **conn):
    """Gets a list of the usernames that are a part of this group."""
    group_details = get_group_api(group['GroupName'], **conn)

    user_list = []
    for user in group_details.get('Users', []):
        user_list.append(user['UserName'])

    return user_list


Instruction:
Can you tell what is the following Python 3 function doing
def _get_base(group, **conn):
    group['_version'] = 1

    # Get the initial group details (only needed if we didn't grab the users):
    group.update(get_group_api(group['GroupName'], users=False, **conn)['Group'])

    # Cast CreateDate from a datetime to something JSON serializable.
    group['CreateDate'] = get_iso_string(group['CreateDate'])
    return group

Response: Fetch the base IAM Group.


Instruction:
Can you create a Python 3 function that
gives a dict containing the ARN GroupName Path ManagedPolicies and Users returns a dictionary containing the names of the Managed Policies and the names of the Users.

Response:
def get_group(group, flags=FLAGS.BASE | FLAGS.INLINE_POLICIES | FLAGS.MANAGED_POLICIES, **conn):
    """
    Orchestrates all the calls required to fully build out an IAM Group in the following format:

    {
        "Arn": ...,
        "GroupName": ...,
        "Path": ...,
        "GroupId": ...,
        "CreateDate": ...,  # str
        "InlinePolicies": ...,
        "ManagedPolicies": ...,  # These are just the names of the Managed Policies.
        "Users": ...,  # False by default -- these are just the names of the users.
        "_version": 1
    }

    :param flags: By default, Users is disabled. This is somewhat expensive as it has to call the `get_group` call
                  multiple times.
    :param group: dict MUST contain the GroupName and also a combination of either the ARN or the account_number.
    :param output: Determines whether keys should be returned camelized or underscored.
    :param conn: dict containing enough information to make a connection to the desired account.
                 Must at least have 'assume_role' key.
    :return: dict containing fully built out Group.
    """
    if not group.get('GroupName'):
        raise MissingFieldException('Must include GroupName.')

    group = modify(group, output='camelized')
    _conn_from_args(group, conn)
    return registry.build_out(flags, start_with=group, pass_datastructure=True, **conn)


Instruction:
Given the following Python 3 function, write the documentation
def get_base(managed_policy, **conn):
    managed_policy['_version'] = 1

    arn = _get_name_from_structure(managed_policy, 'Arn')
    policy = get_policy(arn, **conn)
    document = get_managed_policy_document(arn, policy_metadata=policy, **conn)

    managed_policy.update(policy['Policy'])
    managed_policy['Document'] = document

    # Fix the dates:
    managed_policy['CreateDate'] = get_iso_string(managed_policy['CreateDate'])
    managed_policy['UpdateDate'] = get_iso_string(managed_policy['UpdateDate'])

    return managed_policy

Response: Fetch the base Managed Policy.


Instruction:
Here you have a function in Python 3, explain what it does
def get_managed_policy(managed_policy, flags=FLAGS.ALL, **conn):
    _conn_from_args(managed_policy, conn)
    return registry.build_out(flags, start_with=managed_policy, pass_datastructure=True, **conn)

Response: Given a dict of IAM Managed Policy ARN returns a new dict of IAM Managed Policy objects.


Instruction:
Create a Python 3 function to
get the name out of a configuration object.

Response:
def _name(named):
    """Get the name out of an object.  This varies based on the type of the input:
       * the "name" of a string is itself
       * the "name" of None is itself
       * the "name" of an object with a property named name is that property -
         as long as it's a string
       * otherwise, we raise a ValueError
    """
    if isinstance(named, basestring) or named is None:
        return named
    elif hasattr(named, 'name') and isinstance(named.name, basestring):
        return named.name
    else:
        raise ValueError("Can't interpret %s as a name or a configuration object" % named)


Instruction:
Can you create a Python 3 function that
obtains the version or just 2. x

Response:
def get_version(self):
        '''obtain the version or just 2.2.x if < 2.3.x
        Raises:
            FailedRequestError: If the request fails.
        '''
        if self._version:
            return self._version
        url = "{}/about/version.xml".format(self.service_url)
        resp = self.http_request(url)
        version = None
        if resp.status_code == 200:
            dom = XML(resp.content)
            resources = dom.findall("resource")
            for resource in resources:
                if resource.attrib["name"] == "GeoServer":
                    try:
                        version = resource.find("Version").text
                        break
                    except AttributeError:
                        pass

        # This will raise an exception if the catalog is not available
        # If the catalog is available but could not return version information,
        # it is an old version that does not support that
        if version is None:
            # just to inform that version < 2.3.x
            version = "2.2.x"
        self._version = version
        return version


Instruction:
Create a Python 3 function for
obtaining the shory geoserver version

Response:
def get_short_version(self):
        '''obtain the shory geoserver version
        '''
        gs_version = self.get_version()
        match = re.compile(r'[^\d.]+')
        return match.sub('', gs_version).strip('.')


Instruction:
Implement a function in Python 3 to
send a delete request XXX [ more here

Response:
def delete(self, config_object, purge=None, recurse=False):
        """
        send a delete request
        XXX [more here]
        """
        rest_url = config_object.href
        params = []

        # purge deletes the SLD from disk when a style is deleted
        if purge:
            params.append("purge=" + str(purge))

        # recurse deletes the resource when a layer is deleted.
        if recurse:
            params.append("recurse=true")

        if params:
            rest_url = rest_url + "?" + "&".join(params)

        headers = {
            "Content-type": "application/xml",
            "Accept": "application/xml"
        }

        resp = self.http_request(rest_url, method='delete', headers=headers)
        if resp.status_code != 200:
            raise FailedRequestError('Failed to make DELETE request: {}, {}'.format(resp.status_code, resp.text))

        self._cache.clear()

        # do we really need to return anything other than None?
        return (resp)


Instruction:
Create a Python 3 function to
save an object to the Geoserver catalog.

Response:
def save(self, obj, content_type="application/xml"):
        """
        saves an object to the REST service
        gets the object's REST location and the data from the object,
        then POSTS the request.
        """
        rest_url = obj.href
        data = obj.message()

        headers = {
            "Content-type": content_type,
            "Accept": content_type
        }

        logger.debug("{} {}".format(obj.save_method, obj.href))
        resp = self.http_request(rest_url, method=obj.save_method.lower(), data=data, headers=headers)

        if resp.status_code not in (200, 201):
            raise FailedRequestError('Failed to save to Geoserver catalog: {}, {}'.format(resp.status_code, resp.text))

        self._cache.clear()
        return resp


Instruction:
Write a Python 3 function that can
return a list of stores in the catalog.

Response:
def get_stores(self, names=None, workspaces=None):
        '''
          Returns a list of stores in the catalog. If workspaces is specified will only return stores in those workspaces.
          If names is specified, will only return stores that match.
          names can either be a comma delimited string or an array.
          Will return an empty list if no stores are found.
        '''

        if isinstance(workspaces, Workspace):
            workspaces = [workspaces]
        elif isinstance(workspaces, list) and [w for w in workspaces if isinstance(w, Workspace)]:
            # nothing
            pass
        else:
            workspaces = self.get_workspaces(names=workspaces)

        stores = []
        for ws in workspaces:
            ds_list = self.get_xml(ws.datastore_url)
            cs_list = self.get_xml(ws.coveragestore_url)
            wms_list = self.get_xml(ws.wmsstore_url)
            stores.extend([datastore_from_index(self, ws, n) for n in ds_list.findall("dataStore")])
            stores.extend([coveragestore_from_index(self, ws, n) for n in cs_list.findall("coverageStore")])
            stores.extend([wmsstore_from_index(self, ws, n) for n in wms_list.findall("wmsStore")])

        if names is None:
            names = []
        elif isinstance(names, basestring):
            names = [s.strip() for s in names.split(',') if s.strip()]

        if stores and names:
            return ([store for store in stores if store.name in names])

        return stores


Instruction:
Write a Python 3 function for
returning a single store object.

Response:
def get_store(self, name, workspace=None):
        '''
          Returns a single store object.
          Will return None if no store is found.
          Will raise an error if more than one store with the same name is found.
        '''

        stores = self.get_stores(workspaces=workspace, names=name)
        return self._return_first_item(stores)


Instruction:
Write a Python 3 script to
create a coveragestore for the specified workspace and path.

Response:
def create_coveragestore(self, name, workspace=None, path=None, type='GeoTIFF',
                             create_layer=True, layer_name=None, source_name=None, upload_data=False, contet_type="image/tiff"):
        """
        Create a coveragestore for locally hosted rasters.
        If create_layer is set to true, will create a coverage/layer.
        layer_name and source_name are only used if create_layer ia enabled. If not specified, the raster name will be used for both.
        """
        if path is None:
            raise Exception('You must provide a full path to the raster')

        if layer_name is not None and ":" in layer_name:
            ws_name, layer_name = layer_name.split(':')

        allowed_types = [
            'ImageMosaic',
            'GeoTIFF',
            'Gtopo30',
            'WorldImage',
            'AIG',
            'ArcGrid',
            'DTED',
            'EHdr',
            'ERDASImg',
            'ENVIHdr',
            'GeoPackage (mosaic)',
            'NITF',
            'RPFTOC',
            'RST',
            'VRT'
        ]

        if type is None:
            raise Exception('Type must be declared')
        elif type not in allowed_types:
            raise Exception('Type must be one of {}'.format(", ".join(allowed_types)))

        if workspace is None:
            workspace = self.get_default_workspace()
        workspace = _name(workspace)

        if upload_data is False:
            cs = UnsavedCoverageStore(self, name, workspace)
            cs.type = type
            cs.url = path if path.startswith("file:") else "file:{}".format(path)
            self.save(cs)

            if create_layer:
                if layer_name is None:
                    layer_name = os.path.splitext(os.path.basename(path))[0]
                if source_name is None:
                    source_name = os.path.splitext(os.path.basename(path))[0]

                data = "<coverage><name>{}</name><nativeName>{}</nativeName></coverage>".format(layer_name, source_name)
                url = "{}/workspaces/{}/coveragestores/{}/coverages.xml".format(self.service_url, workspace, name)
                headers = {"Content-type": "application/xml"}

                resp = self.http_request(url, method='post', data=data, headers=headers)
                if resp.status_code != 201:
                    FailedRequestError('Failed to create coverage/layer {} for : {}, {}'.format(layer_name, name,
                                                                                resp.status_code, resp.text))
                self._cache.clear()
                return self.get_resources(names=layer_name, workspaces=workspace)[0]
        else:
            data = open(path, 'rb')
            params = {"configure": "first", "coverageName": name}
            url = build_url(
                self.service_url,
                [
                    "workspaces",
                    workspace,
                    "coveragestores",
                    name,
                    "file.{}".format(type.lower())
                ],
                params
            )

            headers = {"Content-type": contet_type}
            resp = self.http_request(url, method='put', data=data, headers=headers)

            if hasattr(data, "close"):
                data.close()

            if resp.status_code != 201:
                FailedRequestError('Failed to create coverage/layer {} for : {}, {}'.format(layer_name, name, resp.status_code, resp.text))

        return self.get_stores(names=name, workspaces=workspace)[0]


Instruction:
Given the following Python 3 function, write the documentation
def add_granule(self, data, store, workspace=None):
        '''Harvest/add a granule into an existing imagemosaic'''
        ext = os.path.splitext(data)[-1]
        if ext == ".zip":
            type = "file.imagemosaic"
            upload_data = open(data, 'rb')
            headers = {
                "Content-type": "application/zip",
                "Accept": "application/xml"
            }
        else:
            type = "external.imagemosaic"
            upload_data = data if data.startswith("file:") else "file:{data}".format(data=data)
            headers = {
                "Content-type": "text/plain",
                "Accept": "application/xml"
            }

        params = dict()
        workspace_name = workspace
        if isinstance(store, basestring):
            store_name = store
        else:
            store_name = store.name
            workspace_name = store.workspace.name

        if workspace_name is None:
            raise ValueError("Must specify workspace")

        url = build_url(
            self.service_url,
            [
                "workspaces",
                workspace_name,
                "coveragestores",
                store_name,
                type
            ],
            params
        )

        try:
            resp = self.http_request(url, method='post', data=upload_data, headers=headers)
            if resp.status_code != 202:
                FailedRequestError('Failed to add granule to mosaic {} : {}, {}'.format(store, resp.status_code, resp.text))
            self._cache.clear()
        finally:
            if hasattr(upload_data, "close"):
                upload_data.close()

        # maybe return a list of all granules?
        return None

Response: Harvest / add a granule into an existing imagemosaic


Instruction:
How would you explain what the following Python 3 function does
def delete_granule(self, coverage, store, granule_id, workspace=None):
        '''Deletes a granule of an existing imagemosaic'''
        params = dict()

        workspace_name = workspace
        if isinstance(store, basestring):
            store_name = store
        else:
            store_name = store.name
            workspace_name = store.workspace.name

        if workspace_name is None:
            raise ValueError("Must specify workspace")

        url = build_url(
            self.service_url,
            [
                "workspaces",
                workspace_name,
                "coveragestores",
                store_name,
                "coverages",
                coverage,
                "index/granules",
                granule_id,
                ".json"
            ],
            params
        )

        # DELETE /workspaces/<ws>/coveragestores/<name>/coverages/<coverage>/index/granules/<granule_id>.json
        headers = {
            "Content-type": "application/json",
            "Accept": "application/json"
        }

        resp = self.http_request(url, method='delete', headers=headers)
        if resp.status_code != 200:
            FailedRequestError('Failed to delete granule from mosaic {} : {}, {}'.format(store, resp.status_code, resp.text))
        self._cache.clear()

        # maybe return a list of all granules?
        return None

Response: Deletes a granule of an existing imagemosaic


Instruction:
Implement a function in Python 3 to
list all granules of an imagemosaic

Response:
def list_granules(self, coverage, store, workspace=None, filter=None, limit=None, offset=None):
        '''List granules of an imagemosaic'''
        params = dict()

        if filter is not None:
            params['filter'] = filter
        if limit is not None:
            params['limit'] = limit
        if offset is not None:
            params['offset'] = offset

        workspace_name = workspace
        if isinstance(store, basestring):
            store_name = store
        else:
            store_name = store.name
            workspace_name = store.workspace.name

        if workspace_name is None:
            raise ValueError("Must specify workspace")

        url = build_url(
            self.service_url,
            [
                "workspaces",
                workspace_name,
                "coveragestores",
                store_name,
                "coverages",
                coverage,
                "index/granules.json"
            ],
            params
        )

        # GET /workspaces/<ws>/coveragestores/<name>/coverages/<coverage>/index/granules.json
        headers = {
            "Content-type": "application/json",
            "Accept": "application/json"
        }

        resp = self.http_request(url, headers=headers)
        if resp.status_code != 200:
            FailedRequestError('Failed to list granules in mosaic {} : {}, {}'.format(store, resp.status_code, resp.text))

        self._cache.clear()
        return resp.json()


Instruction:
Implement a Python 3 function for
returning all coverages in a coverage store

Response:
def mosaic_coverages(self, store):
        '''Returns all coverages in a coverage store'''
        params = dict()
        url = build_url(
            self.service_url,
            [
                "workspaces",
                store.workspace.name,
                "coveragestores",
                store.name,
                "coverages.json"
            ],
            params
        )
        # GET /workspaces/<ws>/coveragestores/<name>/coverages.json
        headers = {
            "Content-type": "application/json",
            "Accept": "application/json"
        }

        resp = self.http_request(url, headers=headers)
        if resp.status_code != 200:
            FailedRequestError('Failed to get mosaic coverages {} : {}, {}'.format(store, resp.status_code, resp.text))

        self._cache.clear()
        return resp.json()


Instruction:
How would you explain what the following Python 3 function does
def publish_featuretype(self, name, store, native_crs, srs=None, jdbc_virtual_table=None, native_name=None):
        '''Publish a featuretype from data in an existing store'''
        # @todo native_srs doesn't seem to get detected, even when in the DB
        # metadata (at least for postgis in geometry_columns) and then there
        # will be a misconfigured layer
        if native_crs is None:
            raise ValueError("must specify native_crs")

        srs = srs or native_crs
        feature_type = FeatureType(self, store.workspace, store, name)
        # because name is the in FeatureType base class, work around that
        # and hack in these others that don't have xml properties
        feature_type.dirty['name'] = name
        feature_type.dirty['srs'] = srs
        feature_type.dirty['nativeCRS'] = native_crs
        feature_type.enabled = True
        feature_type.advertised = True
        feature_type.title = name

        if native_name is not None:
            feature_type.native_name = native_name

        headers = {
            "Content-type": "application/xml",
            "Accept": "application/xml"
        }

        resource_url = store.resource_url
        if jdbc_virtual_table is not None:
            feature_type.metadata = ({'JDBC_VIRTUAL_TABLE': jdbc_virtual_table})
            params = dict()
            resource_url = build_url(
                self.service_url,
                [
                    "workspaces",
                    store.workspace.name,
                    "datastores", store.name,
                    "featuretypes.xml"
                ],
                params
            )

        resp = self.http_request(resource_url, method='post', data=feature_type.message(), headers=headers)
        if resp.status_code not in (200, 201, 202):
            FailedRequestError('Failed to publish feature type {} : {}, {}'.format(name, resp.status_code, resp.text))

        self._cache.clear()
        feature_type.fetch()
        return feature_type

Response: Publish a featuretype from data in an existing store


Instruction:
Create a Python 3 function for
getting all resources in a given set of names and stores and workspaces.

Response:
def get_resources(self, names=None, stores=None, workspaces=None):
        '''
        Resources include feature stores, coverage stores and WMS stores, however does not include layer groups.
        names, stores and workspaces can be provided as a comma delimited strings or as arrays, and are used for filtering.
        Will always return an array.
        '''

        stores = self.get_stores(
            names = stores,
            workspaces = workspaces
        )

        resources = []
        for s in stores:
            try:
                resources.extend(s.get_resources())
            except FailedRequestError:
                continue

        if names is None:
            names = []
        elif isinstance(names, basestring):
            names = [s.strip() for s in names.split(',') if s.strip()]

        if resources and names:
            return ([resource for resource in resources if resource.name in names])

        return resources


Instruction:
How would you explain what the following Python 3 function does
def get_resource(self, name=None, store=None, workspace=None):
        '''
          returns a single resource object.
          Will return None if no resource is found.
          Will raise an error if more than one resource with the same name is found.
        '''

        resources = self.get_resources(names=name, stores=store, workspaces=workspace)
        return self._return_first_item(resources)

Response: Returns a single resource object.


Instruction:
Write a Python 3 function for
getting a list of layer groups in the catalog.

Response:
def get_layergroups(self, names=None, workspaces=None):
        '''
        names and workspaces can be provided as a comma delimited strings or as arrays, and are used for filtering.
        If no workspaces are provided, will return all layer groups in the catalog (global and workspace specific).
        Will always return an array.
        '''

        layergroups = []

        if workspaces is None or len(workspaces) == 0:
            # Add global layergroups
            url = "{}/layergroups.xml".format(self.service_url)
            groups = self.get_xml(url)
            layergroups.extend([LayerGroup(self, g.find("name").text, None) for g in groups.findall("layerGroup")])
            workspaces = []
        elif isinstance(workspaces, basestring):
            workspaces = [s.strip() for s in workspaces.split(',') if s.strip()]
        elif isinstance(workspaces, Workspace):
            workspaces = [workspaces]

        if not workspaces:
            workspaces = self.get_workspaces()

        for ws in workspaces:
            ws_name = _name(ws)
            url = "{}/workspaces/{}/layergroups.xml".format(self.service_url, ws_name)
            try:
                groups = self.get_xml(url)
            except FailedRequestError as e:
                if "no such workspace" in str(e).lower():
                    continue
                else:
                    raise FailedRequestError("Failed to get layergroups: {}".format(e))

            layergroups.extend([LayerGroup(self, g.find("name").text, ws_name) for g in groups.findall("layerGroup")])

        if names is None:
            names = []
        elif isinstance(names, basestring):
            names = [s.strip() for s in names.split(',') if s.strip()]

        if layergroups and names:
            return ([lg for lg in layergroups if lg.name in names])

        return layergroups


Instruction:
How would you explain what the following Python 3 function does
def get_layergroup(self, name, workspace=None):
        '''
          returns a single layergroup object.
          Will return None if no layergroup is found.
          Will raise an error if more than one layergroup with the same name is found.
        '''

        layergroups = self.get_layergroups(names=name, workspaces=workspace)
        return self._return_first_item(layergroups)

Response: Returns a single layergroup object.


Instruction:
Here you have a function in Python 3, explain what it does
def get_styles(self, names=None, workspaces=None):
        '''
        names and workspaces can be provided as a comma delimited strings or as arrays, and are used for filtering.
        If no workspaces are provided, will return all styles in the catalog (global and workspace specific).
        Will always return an array.
        '''

        all_styles = []

        if workspaces is None:
            # Add global styles
            url = "{}/styles.xml".format(self.service_url)
            styles = self.get_xml(url)
            all_styles.extend([Style(self, s.find('name').text) for s in styles.findall("style")])
            workspaces = []
        elif isinstance(workspaces, basestring):
            workspaces = [s.strip() for s in workspaces.split(',') if s.strip()]
        elif isinstance(workspaces, Workspace):
            workspaces = [workspaces]

        if not workspaces:
            workspaces = self.get_workspaces()

        for ws in workspaces:
            url = "{}/workspaces/{}/styles.xml".format(self.service_url, _name(ws))
            try:
                styles = self.get_xml(url)
            except FailedRequestError as e:
                if "no such workspace" in str(e).lower():
                    continue
                elif "workspace {} not found".format(_name(ws)) in str(e).lower():
                    continue
                else:
                    raise FailedRequestError("Failed to get styles: {}".format(e))

            all_styles.extend([Style(self, s.find("name").text, _name(ws)) for s in styles.findall("style")])

        if names is None:
            names = []
        elif isinstance(names, basestring):
            names = [s.strip() for s in names.split(',') if s.strip()]

        if all_styles and names:
            return ([style for style in all_styles if style.name in names])

        return all_styles

Response: Get all styles in the catalog.


Instruction:
Can you generate the documentation for the following Python 3 function
def get_style(self, name, workspace=None):
        '''
          returns a single style object.
          Will return None if no style is found.
          Will raise an error if more than one style with the same name is found.
        '''

        styles = self.get_styles(names=name, workspaces=workspace)
        return self._return_first_item(styles)

Response: Returns a single style object.


Instruction:
Can you write a function in Python 3 where it
returns a list of workspaces in the catalog.

Response:
def get_workspaces(self, names=None):
        '''
          Returns a list of workspaces in the catalog.
          If names is specified, will only return workspaces that match.
          names can either be a comma delimited string or an array.
          Will return an empty list if no workspaces are found.
        '''
        if names is None:
            names = []
        elif isinstance(names, basestring):
            names = [s.strip() for s in names.split(',') if s.strip()]

        data = self.get_xml("{}/workspaces.xml".format(self.service_url))
        workspaces = []
        workspaces.extend([workspace_from_index(self, node) for node in data.findall("workspace")])

        if workspaces and names:
            return ([ws for ws in workspaces if ws.name in names])

        return workspaces


Instruction:
Here you have a function in Python 3, explain what it does
def get_workspace(self, name):
        '''
          returns a single workspace object.
          Will return None if no workspace is found.
          Will raise an error if more than one workspace with the same name is found.
        '''

        workspaces = self.get_workspaces(names=name)
        return self._return_first_item(workspaces)

Response: Returns a single workspace object.


Instruction:
Can you create a Python 3 function that
extracts a metadata link tuple from an xml node

Response:
def md_link(node):
    """Extract a metadata link tuple from an xml node"""
    mimetype = node.find("type")
    mdtype = node.find("metadataType")
    content = node.find("content")
    if None in [mimetype, mdtype, content]:
        return None
    else:
        return (mimetype.text, mdtype.text, content.text)


Instruction:
Explain what the following Python 3 code does
def build_url(base, seg, query=None):

    def clean_segment(segment):
        """
        Cleans the segment and encodes to UTF-8 if the segment is unicode.
        """
        segment = segment.strip('/')
        if isinstance(segment, basestring):
            segment = segment.encode('utf-8')
        return segment

    seg = (quote(clean_segment(s)) for s in seg)
    if query is None or len(query) == 0:
        query_string = ''
    else:
        query_string = "?" + urlencode(query)
    path = '/'.join(seg) + query_string
    adjusted_base = base.rstrip('/') + '/'
    return urljoin(str(adjusted_base), str(path))

Response: Build a URL from a list of path segments and a dictionary of query parameters.


Instruction:
Create a Python 3 function for
preparing the bundle for uploading.

Response:
def prepare_upload_bundle(name, data):
    """GeoServer's REST API uses ZIP archives as containers for file formats such
    as Shapefile and WorldImage which include several 'boxcar' files alongside
    the main data.  In such archives, GeoServer assumes that all of the relevant
    files will have the same base name and appropriate extensions, and live in
    the root of the ZIP archive.  This method produces a zip file that matches
    these expectations, based on a basename, and a dict of extensions to paths or
    file-like objects. The client code is responsible for deleting the zip
    archive when it's done."""
    fd, path = mkstemp()
    zip_file = ZipFile(path, 'w')
    for ext, stream in data.items():
        fname = "%s.%s" % (name, ext)
        if (isinstance(stream, basestring)):
            zip_file.write(stream, fname)
        else:
            zip_file.writestr(fname, stream.read())
    zip_file.close()
    os.close(fd)
    return path


Instruction:
Can you tell what is the following Python 3 function doing
def md_dimension_info(name, node):
    def _get_value(child_name):
        return getattr(node.find(child_name), 'text', None)

    resolution = _get_value('resolution')
    defaultValue = node.find("defaultValue")
    strategy = defaultValue.find("strategy") if defaultValue is not None else None
    strategy = strategy.text if strategy is not None else None
    return DimensionInfo(
        name,
        _get_value('enabled') == 'true',
        _get_value('presentation'),
        int(resolution) if resolution else None,
        _get_value('units'),
        _get_value('unitSymbol'),
        strategy,
        _get_value('attribute'),
        _get_value('endAttribute'),
        _get_value('referenceValue'),
        _get_value('nearestMatchEnabled')
    )

Response: Extract metadata Dimension Info from an xml node


Instruction:
Write a Python 3 script to
extract metadata Dynamic Default Values from an xml node

Response:
def md_dynamic_default_values_info(name, node):
    """Extract metadata Dynamic Default Values from an xml node"""
    configurations = node.find("configurations")
    if configurations is not None:
        configurations = []
        for n in node.findall("configuration"):
            dimension = n.find("dimension")
            dimension = dimension.text if dimension is not None else None
            policy = n.find("policy")
            policy = policy.text if policy is not None else None
            defaultValueExpression = n.find("defaultValueExpression")
            defaultValueExpression = defaultValueExpression.text if defaultValueExpression is not None else None

            configurations.append(DynamicDefaultValuesConfiguration(dimension, policy, defaultValueExpression))

    return DynamicDefaultValues(name, configurations)


Instruction:
Given the following Python 3 function, write the documentation
def md_jdbc_virtual_table(key, node):
    name = node.find("name")
    sql = node.find("sql")
    escapeSql = node.find("escapeSql")
    escapeSql = escapeSql.text if escapeSql is not None else None
    keyColumn = node.find("keyColumn")
    keyColumn = keyColumn.text if keyColumn is not None else None
    n_g = node.find("geometry")
    geometry = JDBCVirtualTableGeometry(n_g.find("name"), n_g.find("type"), n_g.find("srid"))
    parameters = []
    for n_p in node.findall("parameter"):
        p_name = n_p.find("name")
        p_defaultValue = n_p.find("defaultValue")
        p_defaultValue = p_defaultValue.text if p_defaultValue is not None else None
        p_regexpValidator = n_p.find("regexpValidator")
        p_regexpValidator = p_regexpValidator.text if p_regexpValidator is not None else None
        parameters.append(JDBCVirtualTableParam(p_name, p_defaultValue, p_regexpValidator))

    return JDBCVirtualTable(name, sql, escapeSql, geometry, keyColumn, parameters)

Response: Extract metadata JDBC Virtual Tables from an xml node


Instruction:
Implement a Python 3 function for
extracting metadata entries from an xml node

Response:
def md_entry(node):
    """Extract metadata entries from an xml node"""
    key = None
    value = None
    if 'key' in node.attrib:
        key = node.attrib['key']
    else:
        key = None

    if key in ['time', 'elevation'] or key.startswith('custom_dimension'):
        value = md_dimension_info(key, node.find("dimensionInfo"))
    elif key == 'DynamicDefaultValues':
        value = md_dynamic_default_values_info(key, node.find("DynamicDefaultValues"))
    elif key == 'JDBC_VIRTUAL_TABLE':
        value = md_jdbc_virtual_table(key, node.find("virtualTable"))
    else:
        value = node.text

    if None in [key, value]:
        return None
    else:
        return (key, value)


Instruction:
Make a summary of the following Python 3 code
def resolution_millis(self):
        '''if set, get the value of resolution in milliseconds'''
        if self.resolution is None or not isinstance(self.resolution, basestring):
                return self.resolution
        val, mult = self.resolution.split(' ')
        return int(float(val) * self._multipier(mult) * 1000)

Response: get the value of resolution in milliseconds


Instruction:
Write a Python 3 script for
getting the value of resolution as "<n > s for example : 8 seconds

Response:
def resolution_str(self):
        '''if set, get the value of resolution as "<n> <period>s", for example: "8 seconds"'''
        if self.resolution is None or isinstance(self.resolution, basestring):
            return self.resolution
        seconds = self.resolution / 1000.
        biggest = self._lookup[0]
        for entry in self._lookup:
            if seconds < entry[1]:
                break
            biggest = entry
        val = seconds / biggest[1]
        if val == int(val):
            val = int(val)
        return '%s %s' % (val, biggest[0])


Instruction:
Can you create a Python 3 function that
initializes the internal cache.

Response:
def _init(self):
        """Read resource information into self._cache, for cached access.

        See DAVResource._init()
        """
        # TODO: recalc self.path from <self._file_path>, to fix correct file system case
        #       On windows this would lead to correct URLs
        self.provider._count_get_resource_inst_init += 1
        tableName, primKey = self.provider._split_path(self.path)

        display_type = "Unknown"
        displayTypeComment = ""
        contentType = "text/html"

        #        _logger.debug("getInfoDict(%s), nc=%s" % (path, self.connectCount))
        if tableName is None:
            display_type = "Database"
        elif primKey is None:  # "database" and table name
            display_type = "Database Table"
        else:
            contentType = "text/csv"
            if primKey == "_ENTIRE_CONTENTS":
                display_type = "Database Table Contents"
                displayTypeComment = "CSV Representation of Table Contents"
            else:
                display_type = "Database Record"
                displayTypeComment = "Attributes available as properties"

        # Avoid calling is_collection, since it would call isExisting -> _init_connection
        is_collection = primKey is None

        self._cache = {
            "content_length": None,
            "contentType": contentType,
            "created": time.time(),
            "display_name": self.name,
            "etag": hashlib.md5().update(self.path).hexdigest(),
            # "etag": md5.new(self.path).hexdigest(),
            "modified": None,
            "support_ranges": False,
            "display_info": {"type": display_type, "typeComment": displayTypeComment},
        }

        # Some resource-only infos:
        if not is_collection:
            self._cache["modified"] = time.time()
        _logger.debug("---> _init, nc=%s" % self.provider._count_initConnection)


Instruction:
Implement a function in Python 3 to
return list of ( direct ) collection member names ( UTF - 8 byte strings.

Response:
def get_member_list(self):
        """Return list of (direct) collection member names (UTF-8 byte strings).

        See DAVResource.get_member_list()
        """
        members = []
        conn = self.provider._init_connection()
        try:
            tableName, primKey = self.provider._split_path(self.path)
            if tableName is None:
                retlist = self.provider._list_tables(conn)
                for name in retlist:
                    members.append(
                        MySQLBrowserResource(
                            self.provider,
                            util.join_uri(self.path, name),
                            True,
                            self.environ,
                        )
                    )
            elif primKey is None:
                pri_key = self.provider._find_primary_key(conn, tableName)
                if pri_key is not None:
                    retlist = self.provider._list_fields(conn, tableName, pri_key)
                    for name in retlist:
                        members.append(
                            MySQLBrowserResource(
                                self.provider,
                                util.join_uri(self.path, name),
                                False,
                                self.environ,
                            )
                        )
                members.insert(
                    0,
                    MySQLBrowserResource(
                        self.provider,
                        util.join_uri(self.path, "_ENTIRE_CONTENTS"),
                        False,
                        self.environ,
                    ),
                )
        finally:
            conn.close()
        return members


Instruction:
Here you have a function in Python 3, explain what it does
def get_content(self):
        filestream = compat.StringIO()

        tableName, primKey = self.provider._split_path(self.path)
        if primKey is not None:
            conn = self.provider._init_connection()
            listFields = self.provider._get_field_list(conn, tableName)
            csvwriter = csv.DictWriter(filestream, listFields, extrasaction="ignore")
            dictFields = {}
            for field_name in listFields:
                dictFields[field_name] = field_name
            csvwriter.writerow(dictFields)

            if primKey == "_ENTIRE_CONTENTS":
                cursor = conn.cursor(MySQLdb.cursors.DictCursor)
                cursor.execute("SELECT * from " + self.provider._db + "." + tableName)
                result_set = cursor.fetchall()
                for row in result_set:
                    csvwriter.writerow(row)
                cursor.close()
            else:
                row = self.provider._get_record_by_primary_key(conn, tableName, primKey)
                if row is not None:
                    csvwriter.writerow(row)
            conn.close()

        # this suffices for small dbs, but
        # for a production big database, I imagine you would have a FileMixin that
        # does the retrieving and population even as the file object is being read
        filestream.seek(0)
        return filestream

Response: Open content as a stream for reading.


Instruction:
Can you generate a brief explanation for the following Python 3 code
def get_property_names(self, is_allprop):
        # Let default implementation return supported live and dead properties
        propNames = super(MySQLBrowserResource, self).get_property_names(is_allprop)
        # Add fieldnames as properties
        tableName, primKey = self.provider._split_path(self.path)
        if primKey is not None:
            conn = self.provider._init_connection()
            fieldlist = self.provider._get_field_list(conn, tableName)
            for fieldname in fieldlist:
                propNames.append("{%s:}%s" % (tableName, fieldname))
            conn.close()
        return propNames

Response: Return list of supported property names in Clark Notation.


Instruction:
Implement a Python 3 function for
returning the value of a property.

Response:
def get_property_value(self, name):
        """Return the value of a property.

        The base implementation handles:

        - ``{DAV:}lockdiscovery`` and ``{DAV:}supportedlock`` using the
          associated lock manager.
        - All other *live* properties (i.e. name starts with ``{DAV:}``) are
          delegated to self.getLivePropertyValue()
        - Finally, other properties are considered *dead*, and are handled  using
          the associated property manager, if one is present.
        """
        # Return table field as property
        tableName, primKey = self.provider._split_path(self.path)
        if primKey is not None:
            ns, localName = util.split_namespace(name)
            if ns == (tableName + ":"):
                conn = self.provider._init_connection()
                fieldlist = self.provider._get_field_list(conn, tableName)
                if localName in fieldlist:
                    val = self.provider._get_field_by_primary_key(
                        conn, tableName, primKey, localName
                    )
                    conn.close()
                    return val
                conn.close()
        # else, let default implementation return supported live and dead properties
        return super(MySQLBrowserResource, self).get_property_value(name)


Instruction:
Can you implement a function in Python 3 that
sets or remove a protected property value.

Response:
def set_property_value(self, name, value, dry_run=False):
        """Set or remove property value.

        See DAVResource.set_property_value()
        """
        raise DAVError(
            HTTP_FORBIDDEN, err_condition=PRECONDITION_CODE_ProtectedProperty
        )


Instruction:
Can you write a function in Python 3 where it
splits a request path into tableName and primaryKey tuples.

Response:
def _split_path(self, path):
        """Return (tableName, primaryKey) tuple for a request path."""
        if path.strip() in (None, "", "/"):
            return (None, None)
        tableName, primKey = util.save_split(path.strip("/"), "/", 1)
        #        _logger.debug("'%s' -> ('%s', '%s')" % (path, tableName, primKey))
        return (tableName, primKey)