Categories
Python

Mac OSX, homebrew & Python: Creating Virtual Environments

Assumptions:
— The system is running Mac OSX
— You have installed Python using homebrew
— You have “pip” installed. (Try “easy_install pip” if not installed)

Install Virtualenvwrapper:

virtualenvwrapper is a set of extensions to Ian Bicking’s virtualenv tool. The extensions include wrappers for creating and deleting virtual environments and otherwise managing your development workflow, making it easier to work on more than one project at a time without introducing conflicts in their dependencies.

It’s cooler in the sense that the virtualenvs are generated in a seperate directory than the project directory. It’s an excellent improvement over virtualenv tool. It also provides some handy commands to generate, remove and switch environments.

Install it with this command:

After it is installed, we should create the dedicated directory for the virtualenvs:

Now add these lines to your bash profile (~/.bash_profile):

Creating an environment:

Removing an environment:

Switching to an environment:
Use the workon command:

Using hooks:
You can use predefined scripts to customize the behavior when an environment is activated. For example, to change directory to “~/codes/test” when “test” environment is activated, we shall edit add this line to $VIRTUAL_ENV/test/postactivate:

Similarly, we can use the postdeactivate script to define a behavior when the script is deactivated.

Using pip:
While using pip, you can use an extra argument to define which environment you want the package to be installed:

Cool, eh?

Categories
Python

Django management command: cleanup .pyc files

While distributing a django app, the pyc files could become very annoying. So I wrote this django management command to quickly clean up the *.pyc files from all the directories. The file should be placed under “commands” directory under the “management” directory of an application. So basically the file structure should be like: < appname >/management/commands/rmpyc.py. Note that both the management and commands directories must be python packages, that is they should both have their own __init__.py.

Here is the code:

Usage:
When you have put the application in the “INSTALLED_APPS”, you can now use commands like this to cleanup the python bytecode files:

Categories
PHP

Using annotations in PHP with Doctrine Annotation Reader

Ever since I have seen the use of Annotations in Symfony2 framework, I have been looking for a way to use them in my own projects as well. After looking into the Symfony2’s source code, I noticed that they are indirectly using Doctrine’s Annotation Reader. Reading annotation from your own class is pretty easy. PHP includes the docblocks in the different reflection objects. You just have to read the docblock and parse it. Doctrine does it pretty nicely. Let’s see an example!

To get started, you first need to install doctrine-common package. I recommend you install using composer. Here’s my composer.json for the project:

Now, we need to define the annotation classes. Every annotation you use should have a class with the same name. Doctrine Annotation Reader will map those annotations with these classes and pass the values to the public properties of these classes.

In our example, we shall use one annotation class named – “AnnotatedDescription”. It can be directly passed a value, like:

or in an associative fashion –

We shall allow two keys – “desc” and “type” in these example. But feel free to add your own. Again, in real life, I would declare separate classes for capturing single value and key-value pairs.

Lets declare the AnnotatedDescription class in annotations.php:

Note that, the $value property is used for annotations which accept just a value. For key value pairs, we must declare properties with the key names. You *must* annotate the classes with @Annotation to let Doctrine’s Annotation Reader know that this class should be used for annotations mapping.

Now, lets see the parser file (parse_annotations.php):

The code is pretty much self explanatory. We instantiate “Doctrine\Common\Annotations\AnnotationReader” and use it’s different methods to get the annotations. You might want to study the source code of “Doctrine\Common\Annotations” namespace for even detailed usages.

Note that we must pass reflection objects for the class, properties and methods (eg. ReflectionClass, ReflectionObject, ReflectionProperty, ReflectionMethod). For the properties , instead of directly constructing ReflectionProperty object, we can use getProperty() on a ReflectionObject object in runtime.