0

I am trying to capture majic (%timeit) command results to text file, same executing individually in ipython but calling in .py file, it's throwing syntax error. please help me what exactly missing in my script.

from scripts.download_sftp_extract_fun import sftp_extract
import sys
import timeit
times = %timeit -o sftp_extract.download_sftp_extract(extract_name="RE_AMS_SUBD_SLS_WKLY_")
print(times,file=open("logs/log_re_ams_subd_sls_wkly.txt",'w'))

error message:-

In [26]: exec(open('scripts/extract_download_re_ams_subd_sls_wkly_.py').read())
Traceback (most recent call last):

  File "/usr/local/lib/python3.8/dist-packages/IPython/core/interactiveshell.py", line 3417, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-26-21b570da7ee2>", line 1, in <module>
    exec(open('scripts/extract_download_re_ams_subd_sls_wkly_.py').read())

  File "<string>", line 4
    times=%timeit -o sftp_extract.download_sftp_extract(extract_name="RE_AMS_SUBD_SLS_WKLY_")
          ^
SyntaxError: invalid syntax
Koppula
  • 95
  • 7
  • magics work in an `ipython` session, not in a regular python script. There are ways of importing and running them, but the `%...` syntax is not one of them. – hpaulj Oct 21 '20 at 02:53

1 Answers1

0

The proper usage of timeit is as follows:

import timeit
timeit.timeit("sftp_extract.download_sftp_extract(extract_name=\"RE_AMS_SUBD_SLS_WKLY_\")")

Check out it's documentation for more details

Seth
  • 2,214
  • 1
  • 7
  • 21
  • but below statement working fine in ipython3, i just copied that to .py file and calling again. Was there any difference calling python scripts from shell and .py file? ` In [48]: from scripts.download_sftp_extract_fun import sftp_extract In [49]: import sys In [50]: import timeit In [51]: times = %timeit -o sftp_extract.download_sftp_extract(extract_name="RE_AMS_SUBD_SLS_WKLY_") 14 s ± 2.33 s per loop (mean ± std. dev. of 7 runs, 1 loop each) In [52]: print(times,file=open("logs/log_re_ams_subd_sls_wkly.txt",'w')) ` – Koppula Oct 21 '20 at 08:48
  • @Koppula ipython's % is special, it's not actual python syntax. – Seth Oct 21 '20 at 15:00
  • Umm bit confused, however will try to work with your solution. Thank you very much for your inputs. – Koppula Oct 22 '20 at 08:47
  • https://ipython.readthedocs.io/en/stable/interactive/magics.html @Koppula – Seth Oct 22 '20 at 15:12