Skip to content

Contribute upstream #14

@mslw

Description

@mslw

PyCap does not directly provide two queries (available in API playground) that we needed.

These are easily patched into PyCap's base classes (which lets us preserve all of PyCap's response handling) by changing the payload (which is a dictionary created with self._initialize_payload() and optionally expanded by adding keys), as done for:

  • Export Project XML:
    def export_project_xml(
    self,
    metadata_only: bool = False,
    files: bool = False,
    survey_fields: bool = False,
    dags: bool = False,
    ):
    """Export Project XML
    This function is a patch for PyCap ProjectInfo class
    """
    format_type = "xml"
    payload = self._initialize_payload(
    content="project_xml",
    format_type=format_type,
    )
    payload["returnMetadataOnly"] = metadata_only
    payload["exportFiles"] = files
    payload["exportSurveyFields"] = survey_fields
    payload["exportDataAccessGroups"] = dags
    return_type = self._lookup_return_type(format_type, request_type="export")
    response = self._call_api(payload, return_type)
    return self._return_data(
    response=response,
    content="instrument",
    format_type=format_type,
    df_kwargs=None,
    )
  • Query:
    class MyInstruments(Instruments):
    """An extension of PyCap's Instruments class
    Contains an additional method to export instruments names and labels
    """
    def export_instruments(self):
    """Export instruments names and labels
    PyCap's Instruments class has a field_names property, but lacks
    a method to return matching labels (human-readable). This method
    does that in a simplified way (only supports json format, does not
    support arms).
    """
    format_type = "json" # constant, for now
    payload = self._initialize_payload(
    content="instrument",
    format_type=format_type,
    )
    return_type = self._lookup_return_type(format_type, request_type="export")
    response = self._call_api(payload, return_type)
    return self._return_data(
    response=response,
    content="instrument",
    format_type=format_type,
    df_kwargs=None,
    )

(side note - I use monkey-patching for one and subclassing for another, would be nice to make it consistent)

I should probably open an issue in PyCap asking if they would consider a PR - needs a little bit on top of what was done here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions