Categories
PHP

Symfony2 & Doctrine: Custom Entity Repositories

In Symfony2, we can easily map a custom repository class for an entity. This allows us to extend the functionality of the repository. For example, we can add custom methods for even more customizable data retrieval. Without any ado, let’s jump into codes.

First, create the class which will act as the repository. It must extend “Doctrine\ORM\EntityRepository”. Here, we’re creating a repository for “User” objects inside “WeCodePHP\HomeBundle\Entity”. You can get the entity manager using “$this->getEntityManager()” inside a method.

Now, in the original entity, we need to define which class should be used as the repository class. We do that like this:

The repositoryClass tells Symfony2 about the custom repository.

Now, we can call our custom methods with ease:

Custom repository is what we very often do in models in other frameworks.

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: