Misc Commands¶
- Anaconda
- IPython Notebook
- Git
- Ruby
- Jekyll
- Pelican
- Django
- Docker
Anaconda
Anaconda is a scientific python distribution containing Python, NumPy, SciPy, Pandas, IPython, Matplotlib, Numba, Blaze, Bokeh, and other great Python data analysis tools.
# See Anaconda installed packages !condalist # List environments !condainfo-e # Create Python 3 environment !condacreate-npy3kpython=3anaconda # Activate Python 3 environment !sourceactivatepy3k # Deactivate Python 3 environment !sourcedeactivate # Update Anaconda !condaupdateconda # Update a package with Anaconda !condaupdateipython # Update a package !condaupdatescipy # Update all packages !condaupdateall # Install specific version of a package !condainstallscipy=0.12.0 # Cleanup: Conda can accumulate a lot of disk space # because it doesn’t remove old unused packages !condaclean-p # Cleanup tarballs which are kept for caching purposes !condaclean-t
IPython Notebook
IPython Notebook is a "web-based interactive computational environment where you can combine code execution, text, mathematics, plots and rich media into a single document."
# Start IPython Notebook ipython notebook # Start IPython Notebook with built-in mode to work cleanly # with matplotlib figures ipython notebook --pylab inline # Start IPython Notebook with a profile ipython notebook --profile=dark-bg # Load the contents of a file %load dir/file.py # Time execution of a Python statement or expression %timeit %%time # Activate the interactive debugger %debug # Write the contents of the cell to a file %writefile # Run a cell via a shell command %%script # Run cells with bash in a subprocess # This is a shortcut for %%script bash %%bash # Run cells with python2 in a subprocess %%python2 # Run cells with python3 in a subprocess %%python3 # Convert a notebook to a basic HTML file !ipython nbconvert --to html --template basic file.ipynb
| Command | Description |
|---|---|
| ? | Intro and overview of IPython's features |
| %quickref | Quick reference |
| help | Python help |
| object? | Object details, also use object?? |
Apply css styling based on a css file:
fromIPython.core.displayimport HTML defcss_styling(): styles = open("styles/custom.css", "r").read() return HTML(styles) css_styling()
Git
Git is a distributed revision control system.
# Configure git !gitconfig--globaluser.name'First Last' !gitconfig--globaluser.email'name@domain.com' !gitinit # View status and log !gitstatus !gitlog # Add or remove from staging area !gitadd[target] !gitreset[targetfileorcommit] !gitreset--hardorigin/master # Automatically stage tracked files, # including deleting the previously tracked files # Does not add untracked files !gitadd-u # Delete files and stage them !gitrm[target] # Commit !gitcommit-m"Addcommitmessagehere" # Add new origin !gitremoteaddoriginhttps://github.com/donnemartin/ipython-data-notebooks.git # Set to new origin !gitremoteset-urloriginhttps://github.com/donnemartin/pydatasnippets.git # Push to master, -u saves config so you can just do "git push" afterwards !gitpush-uoriginmaster !gitpush # Diff files !gitdiffHEAD !gitdiff--staged !gitdiff--cached # Show log message of commit and diff !gitshow$COMMIT # Undo a file that has not been added !gitcheckout—[target] # Revert a commit !gitrevert # Undo a push and leave local repo intact !gitpush-foriginHEAD^:master # Undo commit but leave files and index !gitreset--softHEAD~1 # Amend commit message of most recent change !gitcommit--amend !gitpush--force[branch] # Take the dirty state of your working directory # and save it on a stack of unfinished changes !gitstash # Get list of stashes !gitstashlist # Apply the top stash, re-modifying the # uncommitted files when the stash was saved !gitstashapply # Apply a stash at the specified index !gitstashapplystash@{1} # Create a branch !gitbranch[branch] # Check branches !gitbranch # Switch branches !gitcheckout[branch] # Merge branch to master !gitmerge[branch] # Delete branch !gitbranch-d[branch] # Clone !gitclonegit@github.com:repofolder-name !gitclonehttps://donnemartin@bitbucket.org/donnemartin/tutorial.git # Update a local repository with changes from a remote repository # (pull down from master) !gitpulloriginmaster # Configuring a remote for a fork !gitremoteaddupstream[target] # Set remote upstream git branch --set-upstream-to origin/branch # Check remotes !gitremote-v # Syncing a fork !gitfetchupstream !gitcheckoutmaster !gitmergeupstream/master # Create a file containing a patch # git format-patch are like normal patch files, but they also carry information # about the git commit that created the patch: the author, the date, and the # commit log message are all there at the top of the patch. !gitformat-patchorigin/master # Clean up .git folder: !gitrepack-a-d--depth=250--window=250 # GitHub tutorial: http://try.github.io/levels/1/challenges/9 # BitBucket Setup !cd/path/to/my/repo !gitinit !gitremoteaddoriginhttps://donnemartin@bitbucket.org/donnemartin/repo.git !gitpush-uorigin--all#pushesuptherepoanditsrefsforthefirsttime !gitpush-uorigin--tags#pushesupanytags # Open Hatch missions !gitclonehttps://openhatch.org/git-mission-data/git/dmartingit_missions
Ruby
Ruby is used to interact with the AWS command line and for Jekyll, a blog framework that can be hosted on GitHub Pages.
# Update Ruby !rvmgetstable # Reload Ruby (or open a new terminal) !rvmreload # List all known RVM installable rubies !rvmlistknown # List all installed Ruby versions !rvmlist # Install a specific Ruby version !rvminstall2.1.5 # Set Ruby version !rvm--defaultruby-1.8.7 !rvm--defaultruby-2.1.5 # Check Ruby version !ruby-v
Jekyll
Jekyll is a blog framework that can be hosted on GitHub Pages.
In addition to donnemartin.com, I’ve started to build up its mirror site donnemartin.github.io to try out Jekyll. So far I love that I can use my existing developer tools to generate content (SublimeText, Terminal, and GitHub).
Here are other features I like about Jekyll:
- Converts Markdown to produce fast, static pages
- Simple to get started, no backend or manual updates
- Hosted on GitHub Pages
- Open source on GitHub
Many Jekyll themes require a Ruby version of 2 and above. However, the AWS CLI requires Ruby 1.8.7. Run the proper version of Ruby for Jekyll:
!rvm--defaultruby-2.1.5
Build and run the localy Jekyll server:
# => The current folder will be generated into ./_site !bundleexecjekyllbuild # => A development server will run at http://localhost:4000/ # Auto-regeneration: enabled. Use `--no-watch` to disable. !bundleexecjekyllserve
Pelican
I've switched my personal website donnemartin.com to run off Pelican, a python-based alternative to Jekyll. Previous iterations ran off Wordpress and Jekyll.
Setup reference.
# Install !pipinstallpelican !pipinstallmarkdown !pipinstallghp-import # Quick retup !pelican-quickstart # Run server !makedevserver # Stop server !makestopserver # Run ghp-import on output folder # Review https://pypi.python.org/pypi/ghp-import # There's a "Big Fat Warning" section !ghp-importoutput # Update gh-pages (if using a project page) !gitpushorigingh-pages # Update gh-pages (if using a user or org page) !gitmergegh-pagesmaster
Django¶
# Check version of Django !python-c"import django; print(django.get_version())" # Create and setup a project !django-adminstartprojectmysite # Sync db !pythonmanage.pysyncdb # The migrate command looks at the INSTALLED_APPS setting and # creates any necessary database tables according to the database # settings in your mysite/settings.py file and the database # migrations shipped with the app !pythonmanage.pymigrate # Run the dev server !pythonmanage.pyrunserver 1python manage.py runserver 8080 !pythonmanage.pyrunserver0.0.0.0:8000 # Create app !pythonmanage.pystartapp[app_label] # Run tests python manage.py test [app_label] # Tell Django that you’ve made some changes to your models # and that you’d like the changes to be stored as a migration. !pythonmanage.pymakemigrations[app_label] # Take migration names and returns their SQL !pythonmanage.pysqlmigrate[app_label][migration_number] # Checks for any problems in your project without making # migrations or touching the database. !pythonmanage.pycheck # Create a user who can login to the admin site !pythonmanage.pycreatesuperuser # Locate Django source files !python-c" importsys sys.path = sys.path[1:] importdjango print(django.__path__)"