<jupyter_start><jupyter_text>The below imports will only work if you have ros and baxter tools installed and working, which isn't the case on my laptop.<jupyter_code>import rospy
import baxter_interface
from baxter_interface import CHECK_VERSION<jupyter_output><empty_output><jupyter_text>I made this function when I was trying to make you type 'quit' to exit the command-line tool.  But I just caved and said 'Press &lt;CTRL&gt;-C to exit'<jupyter_code>def tryGetLine(inStream):
    'Returns a line if there is one, else an empty string'
    line = ''
    fd = inStream.fileno()
    timeout = 0.01 # seconds
    readyToRead, _, _ = select.select([fd], [], [], timeout)
    if fd in readyToRead:
        line = inStream.readline()
    return line[:-1]  # Remove the newline
def connectToBaxter():
    rospy.init_node('play')
    rs = baxter_interface.RobotEnable(CHECK_VERSION)
    rs.enable()
    pass
def waitForButtonPress():
    pass<jupyter_output><empty_output><jupyter_text>This function will save basically any dictionary to a file in python notation.<jupyter_code>def saveJointAngles(filename, angles):
    '''
    Saves the angles dictionary to filename in python notation
    
    Will save it into an array of dictionaries called data.
    For example
    
    saveJointAngles('out.py', {'a': 1, 'b': 2})
    
    will write to 'out.py':
    data = []
    data.append({
        'a': 1,
        'b': 2,
        })
    '''
    string = str(angles)
    string = string.replace('{', '')
    string = string.replace('}', '')
    split = string.split(', ')
    split.append('})')
    isDataDefined = False
    try:
        with open(filename, 'r') as infile:
            for line in infile:
                if re.match('data =', line):
                    isDataDefined = True
                    break
    except IOError:
        pass # Ignore the problem that the file doesn't yet exist
    with open(filename, 'a') as outfile:
        if not isDataDefined:
            outfile.write('data = []\n')
        outfile.write('\n')
        outfile.write('data.append({\n    ')
        outfile.write(',\n    '.join(split))
        outfile.write('\n')
def main():
    ''
    # Connect to Baxter
    leftArm, rightArm = connectToBaxter()
    
    # Record arm positions
    print 'Press <CTRL>-C to quit'
    while True:
        # Read button presses and see if we should record
        whichArm, action = waitForButtonPress()
        currentArm = leftArm if whichArm == 'left' else rightArm
        jointAngles = currentArm.joint_angles()
        saveJointAngles(whichArm + 'SavedJoints.py', jointAngles)
if __name__ == '__main__':
    sys.exit(main())<jupyter_output><empty_output><jupyter_text>First, let's make sure that out.py doesn't exist<jupyter_code>import os
try:
    os.remove('out.py')
except OSError:
    pass # Ignore if the file doesn't exist<jupyter_output><empty_output><jupyter_text>Now, check to see that I can call this multiple times and it creates only one data array with multiple dictionaries in it.<jupyter_code>saveJointAngles('out.py', {'a': 1.02, 'b': 3.333, 'c': True})
saveJointAngles('out.py', {'abc': 'jumbo shrimp', 'tree': 'tall', 'seed': True})
with open('out.py', 'r') as infile:
    print infile.read()<jupyter_output>data = []

data.append({
    'a': 1.02,
    'c': True,
    'b': 3.333,
    })

data.append({
    'abc': 'jumbo shrimp',
    'tree': 'tall',
    'seed': True,
    })

data.append({
    'a': 1.02,
    'c': True,
    'b': 3.333,
    })

data.append({
    'abc': 'jumbo shrimp',
    'tree': 'tall',
    'seed': True,
    })

data.append({
    'a': 1.02,
    'c': True,
    'b': 3.333,
    })

data.append({
    'abc': 'jumbo shrimp',
    'tree': 'tall',
    'seed': True,
    })<jupyter_text>Let's see if we can import it and use it<jupyter_code>import out
reload(out)
out.data<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13478486262312078, 'fraction_numerical': 0.00933125972006221, 'mean_word_length': 2.945807770961145, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '94344', 'license': 'MIT', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1285, 'n_words': 369, 'path': 'sandbox/play.ipynb', 'repo_name': 'mikebentley15/baxter_projectyouth', 'size': '3858'}