Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contribute upstream #14

Open
mslw opened this issue Feb 6, 2023 · 0 comments
Open

Contribute upstream #14

mslw opened this issue Feb 6, 2023 · 0 comments

Comments

@mslw
Copy link
Collaborator

mslw commented Feb 6, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant