from nbstata.config import launch_stata
stata_session
A class for representing a Stata session
Some parts adapted from the stata_kernel version, limited for now to variables, globals, locals, scalars, matrices, and file names.
StataSession
StataSession ()
StataSession.refresh_suggestions
StataSession.refresh_suggestions ()
"""\
run_sfi(local varlist = 5
local varlist1 = 5""")
StataSession.get_suggestions
StataSession.get_suggestions ()
test_instance.refresh_suggestions() test_instance.suggestions
{'varlist': [],
'globals': ['S_level',
'F1',
'F2',
'F7',
'F8',
'S_ADO',
'S_StataMP',
'S_StataSE',
'S_CONSOLE',
'S_FLAVOR',
'S_OS',
'S_OSDTL',
'S_MACH'],
'scalars': [],
'matrices': [],
'locals': ['varlist1', 'varlist']}
StataSession.get_local_dict
StataSession.get_local_dict ()
'''\
run_sfi(macro drop _all
local test1 "blah blah" ''')
test_instance.clear_suggestions()'test1': 'blah blah'})
test_eq(test_instance.get_local_dict(), {'local test1 ""') run_sfi(
dispatch_run
We incorporate run_noecho
within a dispatch_run
wrapper that can serve as an alternative to the official pystata.stata.run
command, supporting any value of the echo
or quietly
parameters. The ordinary run_direct
(for echo != None
) is also prefaced to manage delimiters and prevent certain quirks of pystata.stata.run
from biting.
'''\
_run_simple(dedent( capture program drop ender
program define ender
disp "ender output"
end
capture program drop display2
program define display2
ender
end
display2
'''), quietly=True)
warn_re_unclosed_comment_block_if_needed
warn_re_unclosed_comment_block_if_needed (code)
StataSession.dispatch_run
StataSession.dispatch_run (code, quietly=False, echo=False, noecho=False)
'''\
test_instance.dispatch_run(dedent( capture program drop ender
program define ender
disp "ender output"
end
capture program drop display2
program define display2
ender
end
display2
'''), quietly=True)
'''\
test_instance.dispatch_run(dedent( capture program drop ender
program define ender
disp "ender output"
end
capture program drop display2
program define display2
ender
end
display2
'''), noecho=True)
ender output
= dedent('''\
code python:
print("hello")
end
''')
=True) test_instance.dispatch_run(code, noecho
hello
"""\
run_noecho(dedent( disp `"`local1' `local2' `local3'"'
disp `"`local1' `local2' `local3' `test1'"'
"""), run_as_prog=test_instance._run_as_program_w_locals)
1 two "3"
1 two "3" blah blah
= """\
code local local1 "foo"
local local2 "bar"
local abcd "foo bar"
"""
test_instance.clear_suggestions()=test_instance._run_as_program_w_locals)
run_noecho(code, run_as_prog
test_instance.clear_suggestions()"""\
run_noecho(dedent( disp `"`local1' `local2' `local3'"'
disp `"`local1' `local2' `local3' `test1'"'
"""), run_as_prog=test_instance._run_as_program_w_locals)
foo bar "3"
foo bar "3" blah blah
test_instance.clear_suggestions()= '''\
code2 display "line continuation " /// commented out
"comment"'''
=True) test_instance.dispatch_run(code2, noecho
line continuation comment
test_instance.clear_suggestions()= '''\
code2 display "line continuation " /// commented out
"comment"'''
=True) test_instance.dispatch_run(code2, noecho
line continuation comment
test_instance.clear_suggestions()= '''\
code2 disp c(version)
version 15.1
disp 1'''
=True)
test_instance.dispatch_run(code2, noecho'disp c(version)', noecho=True) test_instance.dispatch_run(
17
1
15.1
test_instance.clear_suggestions()= '''\
code2 disp c(version)
version 17
disp 1'''
test_instance.dispatch_run(code2)'disp c(version)', echo=True) test_instance.dispatch_run(
. capture version 16
. disp c(version)
16
. version 17
. disp 1
1
.
. disp c(version)
17