Skip to content

A light module written in Python that converts the console outputs into a Python String.

Notifications You must be signed in to change notification settings

afk-echo/ConsoleToStr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 

Repository files navigation

ConsoleToStr

A light-weight module written in Python that converts the console outputs from a running Python program into a Python String.

Pre-requisites

  • A version of Python 3.x(module tested only on Python 3.8)
  • Familiarity with importing modules manually

Installation

  1. Grab the latest release of the module from the releases page.
  2. Place the ConsoleToStr.py in your working project directory.

Usage

from ConsoleToStr import ConsoleToStrConverter

conv = ConsoleToStrConverter()  # returns an instance of the ConsoleToStrConverter class that can be used in the current program.

Methods

  1. ConsoleToStrConverter.start(): Starts the converter.
conv.start()  # Stops printing any outputs on the console window.
  1. ConsoleToStrConverter.curr(): Returns a string containing all the outputs on the console window after calling the start() method.
conv.curr()  # Does not affect the current state of the converter.
  1. ConsoleToStrConverter.stop(): Stops the converter, and all subsequent console outputs are printed on the console window. Returns a string containing all the outputs on the console window after calling the start() method.
conv.stop()  # All console outputs post will be printed on the console after this call.
  • Object once called can be reused by using conv.start() even after it has been stopped.

Examples

  1. A basic example - helps in understanding how the respective methods work.
from ConsoleToStr import ConsoleToStrConverter

conv = ConsoleToStrConverter()

print("The beginning!")
conv.start() # initialises the converter - all outputs to the console stops while the value is being stored

print("Line 1")
print("Line 2")
mid = conv.curr()  # retrieves the current console string

print("Line 3")
print("Line 4")

final = conv.stop()
print("The end.")  # stops the converter - all future outputs go back to the default console

print("The string returned from the curr method is: ", mid)
print("The string returned the stop method is: ", final)

conv.start()  # demonstrates reuse of the object once initialised
print("Resurrection!")
print("This object is reusable.")
mid = conv.stop()
print("Output on reuse:", mid)

'''OUTPUT:
The beginning!
The end.
The string returned from the curr method is:  Line 1
Line 2
The string returned the stop method is:  Line 1
Line 2
Line 3
Line 4
Output on reuse: Resurrection!
This object is reusable.
'''
  1. Usage of the converter to extract the output from the help() method, which does not return a string as an output.
from ConsoleToStr import ConsoleToStrConverter

conv = ConsoleToStrConverter()

# The inbuilt help() function only prints the output on the console, rather than returning a string.
# We can use help() to demonstrate an application of the converter.

conv.start()

help(list.pop)

result = conv.stop()
print(result)  # Prints the same output from the help() function called earlier, but returns a string that we can manipulate.

'''OUTPUT:
Help on method_descriptor:
pop(self, index=-1, /)
    Remove and return item at index (default last).
    Raises IndexError if list is empty or index is out of range.
'''

To-do

  • Add a mode for displaying the outputs on the console as well as record them into a string object simultaneously.
  • Add a method to return the raw StringIO object in which all the outputs are being stored.

About

A light module written in Python that converts the console outputs into a Python String.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages