Skip to content

Instantly share code, notes, and snippets.

@hnlee
Last active January 8, 2017 21:30
Show Gist options
  • Save hnlee/74ff9a841eb2d29a5d613c751f73a990 to your computer and use it in GitHub Desktop.
Save hnlee/74ff9a841eb2d29a5d613c751f73a990 to your computer and use it in GitHub Desktop.

Python 101

Generators

This is a generator statement:

[ a['name'] for a in actors if a['born'] < datetime.datetime(1976, 1, 1) ]

A generator iterates on a set of data and can select out modified components, sometimes based on criteria. In this case, this code lists out actors from an array of actor dictionaries where the actor was born before 1976.

For the 101 track, this would be be something common at a moderate level of understanding of Python. How would you rate your understanding relative to this concept?

  1. Wow, I don't understand any of that
  2. I get parts of it, but I couldn't do anything with it.
  3. If I had this as a reference, I would feel comfortable making small modifications to this
  4. This is a concept I could use in my code today
  5. This is very easy for me to understand and use frequently

Lambdas

This is an example of a lambda function:

map(lambda x: x + 2, range(10)

Lambda functions allow you to define a function "on the fly" without defining and naming them. They are useful building blocks for composing more complicated functions and are often used with map() and reduce().

For the 101 track, this would be be something common at a moderate level of understanding of Python. How would you rate your understanding relative to this concept?

  1. Wow, I don't understand any of that
  2. I get parts of it, but I couldn't do anything with it.
  3. If I had this as a reference, I would feel comfortable making small modifications to this
  4. This is a concept I could use in my code today
  5. This is very easy for me to understand and use frequently

Decorators

This is an example of a decorator:

def decorator(function):
  print("I am a decorator")
  return function
  
@decorator
def any_function():
  print("I am the decorated function")

Decorators provide a shorthand for quickly wrapping or modifying functions. Some common decorators that are already defined in the standard library include @classmethod, @staticmethod, and @property.

For the 101 track, this would be be something common at a moderate level of understanding of Python. How would you rate your understanding relative to this concept?

  1. Wow, I don't understand any of that
  2. I get parts of it, but I couldn't do anything with it.
  3. If I had this as a reference, I would feel comfortable making small modifications to this
  4. This is a concept I could use in my code today
  5. This is very easy for me to understand and use frequently

Web Development

Model-View-Controller

Django is a popular framework for building web apps in Python. It implements the classic model-view-controller pattern for web development, where models represent the data structure and relationships, views specify how to render what the user will see in their browser, and controllers control what responses are returned to which requests. In Django, the terminology is a little different: Django views function as the controllers and Django templates function as the views.

For the web development track, this would be be something common at a moderate level of understanding of Python. How would you rate your understanding relative to this concept?

  1. Wow, I don't understand any of that
  2. I get parts of it, but I couldn't do anything with it.
  3. If I had this as a reference, I would feel comfortable making small modifications to this
  4. This is a concept I could use in my code today
  5. This is very easy for me to understand and use frequently

Database migrations

When developing a web app that is already in production, you need to be able to make changes to the database schema without disrupting the already existing data on the live site. These are handled through migrations, which can be thought of as a version control system for your database. Django has built-in commands to create and apply migrations that can be run at the command line:

python manage.py makemigrations
python manage.py migrate

For the web development track, this would be be something common at a moderate level of understanding of Python. How would you rate your understanding relative to this concept?

  1. Wow, I don't understand any of that
  2. I get parts of it, but I couldn't do anything with it.
  3. If I had this as a reference, I would feel comfortable making small modifications to this
  4. This is a concept I could use in my code today
  5. This is very easy for me to understand and use frequently

API frameworks

Increasingly, web apps are now creating JSON-based APIs that can be read by Javascript-based front-end frameworks or mobile client apps. While you can use Django by itself to generate JSON responses to requests, there are also libraries that provide API frameworks built on top of Django, such as django-rest-framework. These frameworks provide Serializer classes that can convert the data stored in your Django models into a serialized format like JSON.

For the web development track, this would be be something common at a moderate level of understanding of Python. How would you rate your understanding relative to this concept?

  1. Wow, I don't understand any of that
  2. I get parts of it, but I couldn't do anything with it.
  3. If I had this as a reference, I would feel comfortable making small modifications to this
  4. This is a concept I could use in my code today
  5. This is very easy for me to understand and use frequently

Data Science

Data processing with pandas

A common task during data processing is to filter and sort your data across different variables. The pandas library has a powerful set of indexing tools that make this process very simple. There are two main systems of indexing in pandas data frames. You can index by labels set on the rows and columns of the data frame:

df.loc[["RowLabel1", "RowLabel2"], ["ColumnLabel0", "ColumnLabel5"]]

Or, you can index by the position numbers of the rows and columns you want to retrieve:

df.iloc[[1, 2], [0, 5]]

For the data science track, this would be be something common at a moderate level of understanding of Python. How would you rate your understanding relative to this concept?

  1. Wow, I don't understand any of that
  2. I get parts of it, but I couldn't do anything with it.
  3. If I had this as a reference, I would feel comfortable making small modifications to this
  4. This is a concept I could use in my code today
  5. This is very easy for me to understand and use frequently

Cross-validation with scikit-learn

An important step in machine learning is to use cross-validation within your training set to prevent overfitting. The scikit-learn library has built-in tools for generating multiple randomized subsets of your training set data, according to different cross-validation schemes. The StratifiedKFold class is particularly useful when there is uneven representation among the categories in the target you are trying to predict:

from sklearn.model_selection import StratifiedKFold
skf = StratifiedKFold(n_splits=2)
skf.get_n_splits(X, y)
for train_index, test_index in skf.split(X, y):
   ...

For the data science track, this would be be something common at a moderate level of understanding of Python. How would you rate your understanding relative to this concept?

  1. Wow, I don't understand any of that
  2. I get parts of it, but I couldn't do anything with it.
  3. If I had this as a reference, I would feel comfortable making small modifications to this
  4. This is a concept I could use in my code today
  5. This is very easy for me to understand and use frequently

Data visualization with seaborn

Visualizing a multidimensional data set can be challenging when limited to a single panel plot. The seaborn library's FacetGrid() function allows you to generate multipanel plots conditioned across variables in your data set:

g = sns.FacetGrid(attend, col="subject", col_wrap=5, size=1.5, ylim=(0, 10))
       .map(sns.pointplot, "solutions", "score", scale=.7)

facet grid example

(Image and example from seaborn documentation).

For the data science track, this would be be something common at a moderate level of understanding of Python. How would you rate your understanding relative to this concept?

  1. Wow, I don't understand any of that
  2. I get parts of it, but I couldn't do anything with it.
  3. If I had this as a reference, I would feel comfortable making small modifications to this
  4. This is a concept I could use in my code today
  5. This is very easy for me to understand and use frequently
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment