Skip to content

Revise the way outputs are printed to the console #25

Closed

Description

@matthewcarbone

This has been causing me some headaches:

def redirect_stdout(dst):

Every time PdfFit is instantiated, there is a block of text printed to the console. This similarly happens in other situations. I cannot catch this output because within the PdfFit.__init__ there is another line that globally overwrites stdout, making it impossible for me to redirect using e.g. a context manager. I'm not sure this is good practice: is there any particular reason this is done? I understand that it's important to the authors to display the copyright statements and whatnot, but surely it's reasonable to allow the user to disable this?

I have two suggestions:

  1. Simply use print, instead of redirecting stdout, allowing users to catch this in an outer context
  2. Allow the user to disable this block when initializing PdfFit.

Thanks!

Activity

  1. sbillinge commented on May 14, 2024

    @sbillinge
    Contributor
  2. sbillinge commented on Jul 31, 2024

    @sbillinge
    Contributor

    Note to selves, consult with @8bitsam when we turn to this. He played with this a bit and i think found issues in resolving it.

  3. bobleesj commented on Dec 5, 2024

    @bobleesj
    Contributor

    @8bitsam Could you please share any information that might could be helpful? We want to address this issue.

  4. 8bitsam commented on Dec 6, 2024

    @8bitsam
    Contributor

    Yes, I think it would require a refactoring of the pypdffit2_redirect_stdout PyObject in misc.cc to redirect the same way it does now as a default, but allow for the user to also specify another stream to redirect to as a new (optional) argument. The default being the same as it is now is necessary to ensure other users' code doesn't break.

    After modifying the C++, the Python code in output.py with redirect_stdout() would also need to be modified to add functionality for the new argument.

  5. Tieqiong commented on Jan 9, 2025

    @Tieqiong
    Contributor

    @8bitsam Any updates? Thanks!

    @matthewcarbone Hello! Just to confirm, are you asking for:

    1. An option to remove the copyright statement when creating a PdfFit object?
    2. Confirmation that the default behavior uses sys.stdout for output?

    Regarding the second point, it seems redirect_stdout() isn’t automatically invoked, so any output should already default to sys.stdout. Let me know if I misunderstood!

  6. matthewcarbone commented on Jan 9, 2025

    @matthewcarbone
    Author

    @Tieqiong it's been a long time so I don't quite remember. I think though that you're right, redirect_stdout() does not appear to be called anywhere. I don't recall if it was when I initially opened the issue.

    Anyways, I think this can be easily resolved by just getting rid of all of output.py entirely, and just printing normally here (I have no idea if there's actually anything wrong anymore or not, but this should definitely resolve the issue). Alternatively, just add a parameter that the user can change that disables that printing.

  7. 8bitsam commented on Jan 9, 2025

    @8bitsam
    Contributor

    @Tieqiong I think I agree with @matthewcarbone, we could just add an option in the Python code to not print if we want. I never really touched this issue since I was weary about messing with the C++ code. @Tieqiong if you think just modifying the Python code is fine, let me know and I can put out a PR.

  8. Tieqiong commented on Jan 9, 2025

    @Tieqiong
    Contributor

    @8bitsam yes please. Thanks

  9. Tieqiong commented on Jan 11, 2025

    @Tieqiong
    Contributor

    Note for future use: If the output cannot be captured, the safest approach is to use diffpy.pdffit2.output.stdout

  10. sbillinge commented on Jan 12, 2025

    @sbillinge
    Contributor

    Note for future use: If the output cannot be captured, the safest approach is to use diffpy.pdffit2.output.stdout

    @Tieqiong can we add this comment somewhere in the pdffit2 docs?

  11. sbillinge commented on Jan 12, 2025

    @sbillinge
    Contributor

    Let's test this on the 1.5.0rc release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions