background preloader

Python

Facebook Twitter

Virtualenv's `bin/activate` is Doing It Wrong. I'm a Python programmer and frequently work with the excellent virtualenv tool by Ian Bicking. Virtualenv is a great tool on the whole but there is one glaring problem: the activate script that virtualenv provides as a convenience to enable its functionality requires you to source it with your shell to invoke it. The activate script sets some environment variables in your current environment and defines for you a deactivate shell function which will (attempt to) help you to undo those changes later. This pattern is abhorrently wrong and un-unix-y. activate should instead do what ssh-agent does, and launch a sub-shell or sub-command with a modified environment. Problems The approach of modifying the user's current environment suffers from a number of problems: It breaks if you don't use a supported shell.A separate activate script must be maintained for each supported shell syntax.What do you do if you use no shell at all?

Virtualenv's activate suffers from a number of other warts as well: #! Python Programming: A Training Regimen | Pypix. Welcome to Python Regimen, where I’m going to teach you the ins and outs of Python development… from scratch. In this first section, we’re going to choose a version, install Python, and then create the obligatory “Hello world” script. If you’re already familiar with Python, feel free to skip ahead to a later sections in the article. Choosing a Version There are two versions of Python that are currently being developed: 2.x and 3.x. To make things easier, a lot of features that are being added to version three have also being added to version two, so there’s less need to worry about the differences. Installing the Interpreter Once you’ve chosen a version, it’s time to install. Python Shell- lets you run commands line by line.IDLE GUI – lets you write more complex scripts, and run them in one go.Text Editor – any text editor that runs on you system.

For now, launch the shell to test if it works correctly. Hello World! Now we’re all set up; let’s write your first bit of Python! Variables Numbers. Python Tools for Machine Learning. Python is one of the best programming languages out there, with an extensive coverage in scientific computing: computer vision, artificial intelligence, mathematics, astronomy to name a few. Unsurprisingly, this holds true for machine learning as well. Of course, it has some disadvantages too; one of which is that the tools and libraries for Python are scattered. If you are a unix-minded person, this works quite conveniently as every tool does one thing and does it well. However, this also requires you to know different libraries and tools, including their advantages and disadvantages, to be able to make a sound decision for the systems that you are building. Tools by themselves do not make a system or product better, but with the right tools we can work much more efficiently and be more productive.

Therefore, knowing the right tools for your work domain is crucially important. Scikit-Learn Scikit Learn is our machine learning tool of choice at CB Insights. Statsmodels PyMC Shogun Gensim. Design Patterns for Beginners in Python | Pypix. Ever wondered what design patterns are? In this article, we will see why design patterns are important, and will provide some examples, in Python, of when and why they should be used. What are Design Patterns? Design patterns are optimized, reusable solutions to the programming problems that we encounter every day. A design pattern is not a class or a library that we can simply plug into our system; it’s much more than that. It is a template that has to be implemented in the correct situation. It’s not language-specific either. A good design pattern should be implementable in most—if not all—languages, depending on the capabilities of the language. Initially, you can think of a pattern as an especially clever and insightful way of solving a particular class of problems.

Although they’re called “design patterns,” they really aren’t tied to the realm of design. The basic concept of a pattern can also be seen as the basic concept of program design: adding a layer of abstraction. 1. 2. 3. Getting Started With Python for Data Science. As the product manager at Kaggle, I'm fortunate enough to hang around with some of the world's top machine learning experts. And working at a place that specializes in running predictive modelling competitions means that I inevitably got the itch to learn some of this stuff myself.

I don't have a super-strong math or machine learning background, but I figured I'm a decent-enough programmer and have all these smart people around me, so I figured I should be able to figure out how to use some off-the-shelf tools to build some predictive models of my own. I wrote the following tutorial after my experience hacking away on a Kaggle competition for a weekend. I hope it serves to get other programming-literate but machine learning-ignorant people like me up and running, building their own models, and learning data science. Who is this for and what will I learn? All the code from this tutorial is available on github. Here's what you'll learn: Excited? 1. 2. Here's the code: Did you do it? 3. Python Data Mining Resources. Up And Running With Python - My First Kaggle Entry. About two months ago I joined Kaggle as product manager, and was immediately given a hard time by just about everyone because I hadn't ever made a real submission to a Kaggle competition.

I had submitted benchmarks, sure, but I hadn't really competed. Suddenly, I had the chance to not only geek out on cool data science stuff, but to do it alongside the awesome machine learning and data experts in our company and community. But where to start? I didn't have much of a stats background beyond a few undergraduate courses, and I had never done any machine learning. Once a week our data science team gets together and discusses the latest academic machine learning paper, but that certainly wasn't the straightest path to getting me up and running in a Kaggle competition.

For anyone else who, like me, might have some programming ability but doesn't have a clue about machine learning, I have good news: Someone has already done most of the hard work. Here's what you'll learn: Excited? 1. The Django Book. Useful Python Functions and Features You Need to Know. Even after using Python for years, we stumble upon functions and features that we did not know about. Some of these can be quite useful, yet underused.

With that in mind, I’ve compiled a list of incredibly useful Python functions and features that you should be familiar with. Functions with Arbitrary Number of Arguments You may already know that Python allows you to define functions with optional arguments. But there is also a method for allowing completely arbitrary number of function arguments. First, here is an example with just optional arguments: Now, let’s see how we can build a function that accepts any number of arguments. Using Glob() to Find Files Many Python functions have long and descriptive names.

You can fetch multiple file types like this: If you want to get the full path to each file, you can just call the realpath() function on the returned values: Debugging Some of the examples below make use of the inspect module. Generating Unique ID’s Serialization Compressing Strings. A Python App to See What People Are Saying About You Posted on Jan 31, 2014.

I've been taking stock of the digital services I use (and pay for) but am unhappy with. Digital goods sales (for my book) has already been taken care of by bull. Next on my list is tracking mentions of my site across the Internet. In this article, we'll build a simple (but fully functional) web application that searches for and displays mentions of a particular keyword (in my case, "jeffknupp.com"). I should mention that I use a service to do this already: mention. Twitter I'll focus initially on Twitter, as much/most of the commentary on my site likely occurs there (as opposed to blogs or newsgroups). At the very least, I need to be able to persist mentions of my site in a database. Nothing very surprising here. After creating a models.py file, I usually follow up with a populate_db.py file to insert initial data into the database.

Again, nothing crazy here. Tweet collection, the Python way We're now ready to begin the application proper. Easy enough. Back to the app That gives me this: Django is an incredibly-powerful web framework, but using it can be a bit daunting for new users. I found this out the hard way over a year ago, spending hours struggling with even the most basic of concepts. If you're new to Django, (or MVC frameworks in general), you're probably going to have to shift your thinking a bit to use this robust framework. Here are some of the issues that I had to struggle with before I fully understood them. Hopefully, this will save you hours of struggling. 1. Django isn't a CMS. If you have a background with, for example, a platform like WordPress, it's going to take a while to wrap your head around the fact that you're going to have to do a lot more things manually, such as creating urls or modifying settings.

While Django does come with a pretty nifty admin area straight out of the box, configuring Django sometimes isn't as easy as a standard CMS. 2. 3. With Django, a Project is just a collection of apps. 4. This extends the base.html. 5. 6. 7. 8. 9. Getting Started with Python for Data Scientists - Data Community DC. With the R Users DC Meetup broadening its topic base to include other statistical programming tools, it seemed only reasonable to write a meta post highlighting some of the best Python tutorials and resources available for data science and statistics.

What you don’t know is often the hardest part of picking up a new skill, so hopefully these resources will help make learning Python a little easier. Prepare yourself for code indentation heaven. Python is such an incredible language because it can do practically anything, from high performance scientific computing to web frameworks such as Django or Flask. Python is heavily used at Google so the language must be doing something right. And, similar to R, Python has a fantastic community around it and, luckily for you, this community can write. Don’t just take my word for it, watch the following video to fully understand. Distributions Python is available for free from and there are two popular versions, 2.7 or 3.x. Of being lazy. What’s lazy evaluation about ? Some functional programming languages (like Haskell) offers a functionality called lazy evaluation by default. It consists of defering evaluation of functions to the moment their results are actually used. Instead of results, everything works as if your function call are returning the recipe to compute the actual result.

In python, it is actually pretty straightforward to hack __getattr__ and __setattr__ to implement an hackish lazy evaluation as a decorator. class LazyObject(object): __slots__ = [ "_recipe", "_result", "_evaluated" ] def __init__(self, recipe): object. __setattr__(self, "_recipe", recipe) object.

Let’s now check out that the evaluation is done at the last moment with a couple of “print statement”. @lazydef returns_two(): print "evaluation for good" return 2 result = returns_two()print "lazy evaluation"print result + 1 This should result in the following output lazy evaluation evaluation for good 3 # 2+1 A simple example : The k-smallest elements. Advanced Data Structures in Python. The aim of this tutorial is to show off Advanced design structures in Python and the best way to use them. Depending on what you need from a data structure, whether it’s fast lookup, immutability, indexing, etc, you can choose the best data structure for the job and most of the time, you will be combining data structures together to get a logical and easy to understand data model.

Python data structures are very intuitive from a syntax point of view and they offer a large choice of operations. This tutorial tries to put together the most common and useful information about each data structure and offer a guide on when it is best to use one structure or another. Comprehensions If you’ve used Python for very long, you’ve at least heard of list comprehensions. They’re a way to fit a for loop, an if statement, and an assignment all in one line. In other words, you can map and filter a list in one expression. A list comprehension consists of the following parts: Pretty simple, right? Decorators. How to Create a Python Library. In this tutorial, our goal is to create a Python Library which is a FTP class that is well written, useful, and expandable.

Outlining our Objective It’s always important to first outline exactly what functionality your class should include. In our case: connecting to a servercreate a folder on the serverupload a filechange directoryretrieving the directory listingdownload a file When Would I Use an FTP Class? There are several instances when one might use this sort of class. Automate uploading of images, such as a gallery, to a client’s website.Perform off-site backups by transferring a database backup file from your server to another. Note: It’s easy to run into issues with FTP due to different server configurations.

What is FTP? FTP: ”A standard network protocol used to copy a file from one host to another.” Essentially, it allows you to copy a file(s) from one computer to another. Step 1 - Preparation We’ll start off as easy as possible. Step 2 - Setting up the Class Step 3 - Class Variables. Scaling Django to 8 Billion Page Views. Python Basics: Understanding The Functions. Introduction In this article we will learn about functions in Python. We will learn how to create functions in the Python language and how to call the functions. At the end of the article you will be able to create a program that uses functions. This article is divided into sections so that we can understand functions better.

What a function isDefining a function in PythonAdding a docstring in PythonFunction executionFunction AssignmentReturn from functionInner functionDefault values of function parametersKeyword argumentArbitrary number of argumentsLambda expression What a function is A function is a part of your code for doing some specific task and whenever you want to perform that task you can use that function. Defining function in Python Defining a function in Python is very easy. Def functionNameHere(parameters): #your code here def is used for telling the interpreter that it is a user defined function. Adding docstring in function Function execution Function Assignment Syntax Example. Using python and k-means to find the dominant colors in images.

October 23, 2012 17:23 /17 comments/algorithmspython I'm working on a little photography website for my Dad and thought it would be neat to extract color information from photographs. I tried a couple of different approaches before finding one that works pretty well. This approach uses k-means clustering to cluster the pixels in groups based on their color. The center of those resulting clusters are then the "dominant" colors. k-means is a great fit for this problem because it is (usually) fast.

A warning I'm no expert on data-mining -- almost all my experience comes from reading Toby Segaran's excellent book Programming Collective Intelligence. How it works The way I understand it to work is you start with a bunch of data points. Now loop over every point in the data and calculate its distance to each of the "k" clusters. When the centers stop moving very much you can stop looping. Applying it to photographs I made a few optimizations along the way: Looking at some results The results: Links. Using Django for (mostly) static sites.

Using Python for Computer Vision. Python: A Simple Step-by-Step SQLite Tutorial « The Mouse Vs. The Python Th. Building a Flask Blog: Part 1. Deploying Django with Salt Stack. Deploying Django apps on Heroku. Learn 90% of Python in 90 Minutes. Introduction to Pandas and Vincent | Neckbeard Republic — bite-sized python. Will Scientists Ever Move to Python 3? Python Packaging: Hate, hate, hate everywhere. Wireless Attacks With Python: Part One.

Python's documentation at your fingertips. Profiling Python Like a Boss. Introduction to Functions in Python. Bootstrapping a Python software project. Python 101: An Introduction to Python’s Debugger. The Python and The Compiled Python. Python 101: An Intro to logging « The Mouse Vs. The Python The Mouse Vs. Th. Python 101 « The Mouse Vs. The Python. Deep into Python. Python 101: Exception Handling. How to Send Email with Python « The Mouse Vs. The Python The Mouse Vs. The. Using Python and the NLTK to Find Haikus in the Public Twitter Stream.