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.

from nbstata.config import launch_stata

source

StataSession

 StataSession ()

source

StataSession.refresh_suggestions

 StataSession.refresh_suggestions ()
run_sfi("""\
local varlist = 5
local varlist1 = 5""")

source

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']}

source

StataSession.get_local_dict

 StataSession.get_local_dict ()
run_sfi('''\
macro drop _all
local test1 "blah blah" ''')
test_instance.clear_suggestions()
test_eq(test_instance.get_local_dict(), {'test1': 'blah blah'})
run_sfi('local test1 ""')

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)

source

warn_re_unclosed_comment_block_if_needed

 warn_re_unclosed_comment_block_if_needed (code)

source

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
code = dedent('''\
    python:
    print("hello")
    end
    ''')
test_instance.dispatch_run(code, noecho=True)
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()
run_noecho(code, run_as_prog=test_instance._run_as_program_w_locals)
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"'''
test_instance.dispatch_run(code2, noecho=True)
line continuation comment
test_instance.clear_suggestions()
code2 = '''\
display "line continuation " /// commented out
    "comment"'''
test_instance.dispatch_run(code2, noecho=True)
line continuation comment
test_instance.clear_suggestions()
code2 = '''\
disp c(version)
version 15.1
disp 1'''
test_instance.dispatch_run(code2, noecho=True)
test_instance.dispatch_run('disp c(version)', noecho=True)
17
1
15.1
test_instance.clear_suggestions()
code2 = '''\
disp c(version)
version 17
disp 1'''
test_instance.dispatch_run(code2)
test_instance.dispatch_run('disp c(version)', echo=True)

. capture version 16

. disp c(version)
16

. version 17

. disp 1
1

. 
. disp c(version)
17