# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# # Explanation
#
# This routine changes the coding convention of aaa_appcodes.do
#
# Former convention : 
# ```Stata
# replace isApp = 0  if rootdom0 == "hello.edu"
# replace isApp = 0  if rootdom1 == "hello.edu"
# replace isApp = 0  if rootdom2 == "hello.edu"
# ```
#
# Newer convention :
# ```Stata
# replace isApp = 0  if rootdom == "hello.edu0"
# replace isApp = 0  if rootdom == "hello.edu1"
# replace isApp = 0  if rootdom == "hello.edu2"
# ```

# Move the digit in the do file.
def move_digit(string):
    new_string = string[0:string.find('rootdom') + 7]
    new_string = new_string + string[string.find('rootdom') + 8:len(string) - 1]
    new_string = new_string + string[string.find('rootdom') + 7]
    new_string = new_string + '"'
    return new_string


# Test string
mystring = 'replace isApp = 0  if rootdom0 == "hello.edu"'

# Test output
print(move_digit(mystring))
print(move_digit('replace isApp = 1  if rootdom0 == "aacc.edu"'))

dofile = open('aaa_appcodes.do', mode='r')

# +
newdo = []

for lines in dofile.readlines()[29:]:
    if lines.find('rootdom') == -1:
        newdo.append(lines)
    elif lines.find('rootdom') > -1:
        newdo.append(move_digit(lines))

newdo
# -

# This cell writes newdo to a new file that can be pasted in App_Rec_Train/aaa_appcodes.do
with open('aaa_appcodes_fixed' + '.do', mode='w') as newdofile:
            for do_line in newdo:
                print(do_line, file = newdofile)
newdofile.close







StarcoderdataJupyterScriptsDedupFiltered