私はあなたがこのようなアイデアを見てきていると思います:
if __name__ == "__main__":
# do something if this script is invoked
# as python scriptname. Otherwise, gets ignored.
スクリプトでpythonを呼び出すと、そのファイルには、python実行可能ファイルによって直接呼び出されるファイルである場合に__name__
設定された属性があります。"__main__"
それ以外の場合(直接呼び出されない場合)、インポートされます。
これで、たとえば次のことが必要な場合に、スクリプトでこのトリックを使用できます。
def somescriptfunc():
# does something
pass
if __name__ == "__main__":
# do something if this script is invoked
# as python scriptname. Otherwise, gets ignored.
import cProfile
cProfile.run('somescriptfunc()')
これにより、スクリプトが変更されます。インポートすると、そのメンバー関数、クラスなどを通常どおりに使用できます。コマンドラインから実行すると、それ自体がプロファイルされます。
これはあなたが探しているものですか?
私が集めたコメントから、おそらくもっと必要なので、ここに行きます:
CGIの変更からスクリプトを実行している場合、その形式は次のとおりです。
# do some stuff to extract the parameters
# do something with the parameters
# return the response.
私が抽象化と言うとき、あなたはこれを行うことができます:
def do_something_with_parameters(param1, param2):
pass
if __name__ = "__main__":
import cProfile
cProfile.run('do_something_with_parameters(param1=\'sometestvalue\')')
そのファイルをPythonパスに配置します。それ自体を実行すると、プロファイリングする関数がプロファイリングされます。
次に、CGIスクリプトに対して、次のようなスクリプトを作成します。
import {insert name of script from above here}
# do something to determine parameter values
# do something with them *via the function*:
do_something_with_parameters(param1=..., param2=...)
# return something
したがって、cgiスクリプトは関数の小さなラッパーになり(とにかく)、関数はセルフテストになります。
次に、本番サーバーから離れたデスクトップで作成された値を使用して、関数のプロファイルを作成できます。
これを達成するためのより良い方法はおそらくありますが、それはうまくいくでしょう。