Changes

Jump to: navigation, search

OPS435 Python Lab 1

11 bytes added, 07:55, 16 May 2017
Using Magic Functions
'''Perform the following steps:'''
:#To get into the ipython shell type:<source>
ipython3
</source>Now we are inside the IPython environment. We can run some basic bash commands in here, this is done through by using IPython magic functions. <br>
:#Lets try a few commands out now:<source>
%ls
%pwd
%ls
</source>Now hold on. You are not using Python here. These are aliases, that IPython gives you access to. What you are actually using is bash, but not all bash commands are available in the IPython environment.<br><br>
:#Lets find out which ones are available, type the following command into the IPython shell:<source>
%alias
</source>We should now have a list of all the bash commands available in IPython. Shortly we will go over how to add new bash commands into this environment, but you must remember, these are only here to assist in your python scripting, we are not here to learn bash commands.<br><br>
:#Next lets add a new bash command that seems to be missing from this list:<source>
%alias vim vim
</source>
%vim
</source>You should be seeing an error telling you invalid syntax. This is happening because we need to create a config file to make this alias persist in-between sessions.<br><br>
:#Exit your current IPython session:<source>
exit
</source>
:#Now, create a new file and add the following content to it:<source>
vim ~/.ipython/profile_default/startup/00-alias.ipy
</source>
:#Place our alias inside:<source>
%alias vim vim
</source>
:#Save and quit the file. Now lets return to our IPython shell and confirm that our alias is available right away:<source>
ipython3
%vim
</source>At this point vim should open successfully and you should now understand how to create new IPython aliases and store them persistently. Use these aliases to customize your environment with any bash commands you thing IPython is missing. Exit vim now and head back to the IPython shell. <br><br>
:#Lets setup a directory structures for completing and organizing labs. These should be the locations to store your lab scripts.<source>
%mkdir ~/ops435
%mkdir ~/ops435/lab1
%mkdir ~/ops435/lab8
</source>
:#If you are interested in finding more information about magic functions in IPython, try entering the IPython shell and typing the following:<source>
%magic
</source>
:This should show you a OVERWHELMING amount of information, as we move through the course we will slowly use different magic functions from here, but we will never use all of them. To be continued. Magic functions than just running bash commands. They cover a huge range of different tasks, while we are writing code, allowing us to interactively inspect the Python we are writing and running. Lets move on for now.
<ol><li value="12" style="margin-left:25px;">Perform the following steps to evaluation this unit. Running this evaluation script will give you an error, read this error message to see if you can figure out why. This is happening because we are checking a lab script file that we HAVE NOT created yet, so these errors are ok for now. But run these check scripts regularly as you work through the labs, they may give you hints if you get stuck.
13,420
edits

Navigation menu