使Python模块可调用

时间:2017-07-11 12:35:06

标签: python-2.7 module callable

需要一些帮助,使下面的代码成为另一个脚本的可调用对象,例如scrape.run()

我不知道从哪里开始指点?

scrape.py

from clint.textui import puts, colored
import subprocess, sys
from datetime import datetime
from time import sleep as sleep
today = datetime.now().strftime("%H:%M:%S")
multimon_ng = subprocess.Popen("rtl_fm -A lut -s 22050  -f 148.81250M | multimon-ng -t raw -a FLEX -f alpha /dev/stdin", 
 stdout=subprocess.PIPE,
 stderr=subprocess.PIPE, 
 shell=True) 
while True: 
 try:
  nextline = multimon_ng.stdout.readline() 
  if not nextline: 
   pass 
  else: 
   try: 
    flex, mdate, mtime, bitrate, other, capcode, o2, msg = nextline.split(" ", 7) 
   except ValueError: 
    print "invalid line", nextline 
   else: 
    puts(colored.yellow(capcode), newline=False)
    puts(" [", newline=False)
    puts(colored.green(mdate + " " + str(datetime.now().strftime('%H:%M'))), newline=False)
    puts(" ]", newline=False)
    puts(msg)
 except KeyboardInterrupt:
    print "exit"
 multimon_ng.poll()

1 个答案:

答案 0 :(得分:0)

将其添加到脚本的底部。

if __name__ == "__main__":
    function_you_wish_to_call_here()

if语句下的任何内容都将在您直接调用脚本时运行。