Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7839499
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:27:38+00:00 2026-06-02T15:27:38+00:00

In an existing code snippet, I have import sys from code import InteractiveConsole class

  • 0

In an existing code snippet, I have

import sys
from code import InteractiveConsole


class FileCacher:
    "Cache the stdout text so we can analyze it before returning it"
    def __init__(self):
        self.reset()

    def reset(self):
        self.out = []

    def write(self, line):
        self.out.append(line)

    def flush(self):
        output = '\n'.join(self.out)
        self.reset()
        return output


class Shell(InteractiveConsole):
    "Wrapper around Python that can filter input/output to the shell"
    def __init__(self):
        self.stdout = sys.stdout
        self.cache = FileCacher()
        InteractiveConsole.__init__(self)
        return

    def get_output(self):
        sys.stdout = self.cache

    def return_output(self):
        sys.stdout = self.stdout

    def push(self, line):
        self.get_output()
        # you can filter input here by doing something like
        # line = filter(line)
        InteractiveConsole.push(self, line)
        self.return_output()
        output = self.cache.flush()
        # you can filter the output here by doing something like
        # output = filter(output)
        print output  # or do something else with it
        return

if __name__ == '__main__':
    sh = Shell()
    sh.interact()

How do I modify this to use IPython’s interactive shell if IPython is available without changing the rest of the code if possible.

I attempted swapping out line 2 from code import InteractiveConsole with from IPython.core import interactiveshell as InteractiveConsole but obviously, it’s not a directly interchangeable class.

What’s the best way to do this (with minimal change to the rest of the code base) with a try except and using IPython in preference over code module when IPython exists?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:27:39+00:00Added an answer on June 2, 2026 at 3:27 pm

    Here’s my own attempt:-

    import sys
    from code import InteractiveConsole
    
    class FileCacher:
        "Cache the stdout text so we can analyze it before returning it"
        def __init__(self):
            self.reset()
    
        def reset(self):
            self.out = []
    
        def write(self, line):
            self.out.append(line)
    
        def flush(self):
            output = '\n'.join(self.out)
            self.reset()
            return output
    
    
    class Shell(InteractiveConsole):
        "Wrapper around Python that can filter input/output to the shell"
        def __init__(self):
            self.stdout = sys.stdout
            self.cache = FileCacher()
            InteractiveConsole.__init__(self)
            return
    
        def get_output(self):
            sys.stdout = self.cache
    
        def return_output(self):
            sys.stdout = self.stdout
    
        def push(self, line):
            self.get_output()
            # you can filter input here by doing something like
            # line = filter(line)
            InteractiveConsole.push(self, line)
            self.return_output()
            output = self.cache.flush()
            # you can filter the output here by doing something like
            # output = filter(output)
            print output  # or do something else with it
            return
    
    if __name__ == '__main__':
        try:
            import IPython
            IPython.embed()
        except:
            sh = Shell()
            sh.interact()
    

    which seems to work fine but I probably lost the cache and stdout custom methods/functionalities.

    Any criticism, edits and improvement suggestions welcome!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code (snippet) from a .h file: #import <UIKit/UIKit.h> #import ILView.h /**
I have the following code snippet I'm trying to get the text from: <span
I have this snippet of code to parse the URL and add a class
I have a problem with this snippet of code: import optparse parser = optparse.OptionParser(version=__version__,
I regularly have to convert an existing C# code snippet/.CS file to a PowerShell
Reading existing code at work, I wondered how come this could work. I have
I have existing code in c and c++. I have added these c files
Does anyone know of existing code that lets you draw fully justified text in
I have existing code that uses CMNewProfileSearch to find then iterate over the color
I have an existing code base that is not packaged at all. The code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.