Python Forum
converting scripts to be functions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
converting scripts to be functions
#4
I don't think there is a 'recommended pythonic' way to do it. The simplest way, as the script outputs serialized data, is perhaps to give the function an open file object argument where the output will be written instead of stdout. If the script uses other commands, you could capture their output and write them to this file object as well.

The next level is perhaps to output structured data that client code wouldn't have to parse. For example the 'ls' command outputs a sequence of directory entries. One could imagine a functional version that returns an iterable of Entry objects in a way similar to os.scandir(). It requires more transformation of the original code to parse its output, so you could do it gradually.

Some other issues may arise, for example some programs may assume that they are running in a terminal and behave differently when they are called from a script.
Reply


Messages In This Thread
converting scripts to be functions - by Skaperen - Jul-29-2020, 10:06 PM
RE: converting scripts to be functions - by buran - Jul-30-2020, 05:10 AM
RE: converting scripts to be functions - by Gribouillis - Jul-30-2020, 06:36 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020