Changes

Jump to: navigation, search

OPS435 Python Lab 1

177 bytes added, 10:58, 8 March 2017
Part 1 - Using IPython
=== Part 1 - Using Common IPython Commands and Features ===
==== Using Magic Functions ====
Lets start with trying to run some python code in a interactive shell. This is a advanced python shell, similar to the bash shell that you have been using throughout the linux courses.
To get into the ipython shell type:
'''Perform the following steps:''' #To get into the ipython shell type:<presource>
ipython3
</presource><br><br> 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:<presource>
%ls
%pwd
%cd ~/
%ls
</presource><br><br> 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: <presource>
%alias
</presource><br><br> We should now have a list of all the bash command 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: <presource>
%alias vim vim
</presource#The vim command will give us our much needed syntax highlighting, while we are editing scripts from within the IPython environment. These magic %alias functions do not save in between sessions, this creates a problem since you would have to create them every time you start IPython. This will create a error. :<presource>
exit
ipython3
vim
</presource><br><br> 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 inbetween in-between sessions.<br><br> #Exit your current IPython session:<presource>
exit
</presourceCreate #Now, create a new file and add the following content to it: <presource>
vim ~/.ipython/profile_default/startup/00-alias.ipy
<presource#Place our alias inside:<blockquotesource> %alias vim vim</blockquotesource#Save and quit the file. Now lets return to our IPython shell and confirm that our alias is available right away. :<presource>
ipython3
%vim
</presource><br><br> At this point vim should open successfully and you should now understand how to create new IPython aliases and store them persistently. 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. <presource>
%mkdir ~/ops435
%mkdir ~/ops435/lab1
%mkdir ~/ops435/lab7
%mkdir ~/ops435/lab8
</presource#If you are interested in finding more information about magic functions in IPython, try entering the IPython shell and typing the following:<presource>
%magic
</presource>
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.
13,420
edits

Navigation menu