omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Direct Script Output to String

    Pythonista
    2
    2
    1670
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Robert
      Robert last edited by

      So I've been using this Pythonista Project Template to be able to use some SymPy in my iOS App. This is working fine with following code :

      //Run the main script:
          if (scriptPath) {
              NSString *script = @"from sympy import *\ninit_printing(use_unicode=True)\nx = Symbol('x')\nprint(solve(x**2 - 1, x))";
              if (script) {
                  [[PythonInterpreter sharedInterpreter] run:script asFile:scriptPath];
              } else {
                  NSLog(@"Could not load main.py (make sure its encoding is UTF-8)");
              }
          } else {
              NSLog(@"Could not find main.py");
          }
      

      So in this template there is a script called "captureoutput.py" wich looks like the following :

      def _capture_output_main():
      	import _outputcapture
      	import sys
      
      	class StdoutCatcher (object):
      		def __init__(self):
      			self.encoding = 'utf8'
      		def write(self, s):
      			if isinstance(s, str):
      				_outputcapture.CaptureStdout(s)
      			elif isinstance(s, unicode):
      				_outputcapture.CaptureStdout(s.encode('utf8'))
      		def writelines(self, lines):
      			for line in lines:
      				self.write(line + '\n')
      		def flush(self):
      			pass
      
      	class StderrCatcher (object):
      		def __init__(self):
      			self.encoding = 'utf8'
      		def write(self, s):
      			if isinstance(s, str):
      				_outputcapture.CaptureStderr(s)
      			elif isinstance(s, unicode):
      				_outputcapture.CaptureStderr(s.encode('utf8'))
      		def flush(self):
      			pass
      
      	class StdinCatcher (object):
      		def __init__(self):
      			self.encoding = 'utf8'
      		def read(self, len=-1):
      			return _outputcapture.ReadStdin(len)
      		
      		def readline(self):
      			return _outputcapture.ReadStdin()
      
      	sys.stdout = StdoutCatcher()
      	sys.stderr = StderrCatcher()
      	sys.stdin = StdinCatcher()
      
      _capture_output_main()
      del _capture_output_main
      

      This outputs into a OMTextView in the App but I want to have the output accessible without UI use. Like in an NSString or somethingIi can work with without using UI.

      I hope I was able to clarify my problem ! Thanks for your help in advance !

      All my best,
      Robert

      1 Reply Last reply Reply Quote 0
      • JonB
        JonB last edited by

        sys.stdout, and sys.stderr can be any object that looks like a file descriptor. So this fould actually be a file, which might be easiest since you could read it in ObjC, or it could be an StringIO

        oldstdout = sys.stdout
        sys.stdout = io.StringIO()
        

        then you could get the value using getvalue(). To access that from the ObjC side, you might need to use some python c api functions.

        1 Reply Last reply Reply Quote 0
        • First post
          Last post
        Powered by NodeBB Forums | Contributors