Created
April 21, 2014 14:11
-
-
Save steder/11143891 to your computer and use it in GitHub Desktop.
ipython examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# coding: utf-8 | |
# # this is an ipython notebook | |
# | |
# This is a markdown block that I can use to describe whatever I want to talk about. | |
# In[1]: | |
POSTGRES_URI = "postgresql+psycopg2://localhost:5432/steder" | |
# In[2]: | |
from sqlalchemy import create_engine | |
engine = create_engine(POSTGRES_URI) | |
# In[3]: | |
print engine | |
# In[4]: | |
engine.execute("DROP TABLE IF EXISTS users;") | |
engine.execute("CREATE TABLE users (id SERIAL);") | |
# In[5]: | |
[x for x in engine.execute("SELECT * FROM users")] | |
# In[6]: | |
from matplotlib import pyplot | |
# In[7]: | |
pyplot.plot([1, 2, 3]) | |
# In[8]: | |
# show this inline rather than popping up a new window: | |
get_ipython().magic(u'matplotlib inline') | |
# In[9]: | |
pyplot.plot([1,2,3,4,5,6]) | |
# In[10]: | |
x = [1, 2, 3, 4] | |
y = [i ** 2 for i in x] | |
pyplot.plot(x, y) | |
# In[11]: | |
print "Hello World" | |
# In[12]: | |
def f(x): | |
return x ** 2 | |
x = xrange(1, 1000) | |
y = map(f, x) | |
pyplot.plot(x, y) | |
# # ipython-sql | |
# | |
# Let's try using an extension to make working with SQL more intuitive | |
# | |
# In[13]: | |
get_ipython().magic(u'load_ext sql') | |
# In[14]: | |
get_ipython().magic(u'sql postgresql+psycopg2://localhost:5432/steder') | |
# In[15]: | |
get_ipython().magic(u'config SqlMagic.feedback=True') | |
get_ipython().magic(u'config SqlMagic.autolimit=0') | |
get_ipython().magic(u'config SqlMagic.autopandas=True') | |
get_ipython().magic(u'config SqlMagic.displaylimit=10') | |
# In[16]: | |
get_ipython().run_cell_magic(u'sql', u'', u"DROP TABLE IF EXISTS users;\nCREATE TABLE users (id SERIAL, username VARCHAR(16), email VARCHAR(256), birthdate TIMESTAMP WITH TIME ZONE DEFAULT NOW());\nINSERT INTO users (username, email) VALUES ('mike', '[email protected]');\nINSERT INTO users (username, email) VALUES ('carolyn', '[email protected]');\nSELECT * FROM users;") | |
# In[22]: | |
x = _ | |
# In[23]: | |
print x | |
# In[24]: | |
x.plot() | |
# In[30]: | |
x = get_ipython().magic(u'sql select id, birthdate from users;') | |
# In[31]: | |
x.plot() | |
# In[32]: | |
print x | |
# In[34]: | |
x.stack() | |
# In[ ]: | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment