Jul 8 2011

PyDev – automated refactoring problems

Béres Botond

Update: This has been resolved in PyDev 2.2.1!

My usual development tool for Python and Django projects is Eclipse + PyDev. This combination is pretty powerful, I’ve been using it for a long time and I like it, but there are some pain points that I wish would be improved upon.

For example PyDev’s automated refactoring support could use some improvement. I’ve just noticed the following “fail” when applying Extract Local Variable on some instance variable data like self.data['0']['values'] below. After extracting and assigning it to a local variable, it will just rename the first occurrence of self.data['0']['values'], in the same function!.

Before

def do_something(self):
 self.instance_method(self.data['0']['values'])
 
 ...
 
 self.another_instance_method(self.data['0']['values'])
 self.yet_another_instance_method(self.data['0']['values'])

After

def do_something(self):
 values_node = self.data['0']['values']
 self.instance_method(values_node)
 
 ...
 
 self.another_instance_method(self.data['0']['values'])
 self.yet_another_instance_method(self.data['0']['values'])

Now I understand that the reason for this must be that we are talking about an instance variable. PyDev cannot know for sure if in the section we modify the value of our instance variable indirectly or not. Or maybe another_instance_method modifies/overwrites it and then replacing all occurences would be a bad idea. Of course changing instance variables like that is a very bad practice but PyDev cannot know if we are using best practices or not in our code logic, so it stays on the safe side.

However, when doing the Extract Local Variable refactoring, it would be simple to have an option of  also replacing all occurrences or not. Let the programmer decide! I believe that’s the correct solution in this case and exactly what PyCharm does in this situation. So that’s a +1 point to PyCharm for this. I realize this is just one small bonus point to PyCharm, I’ve been playing around with it for some time and looks like a really nice IDE, but I’m not quite convinced yet if it’s completely superior to Eclipse + PyDev. We’ll see …

Which one do you like better? Or what other IDE are you using and how does it compare to Pydev/PyCharm?


Nov 13 2010

The 3 most important things to do after a fresh Windows install

Béres Botond

… and how to do it quickly.

I always dread the prospect of installing (or reinstalling) Windows. Why? The problem is not about installing Windows itself, but reinstalling everything that you need for everyday use. This depends on the person of course, but even if you don’t use a lot of programs, there’s still a fairly consistent list of essential or very useful software that most PCs running Windows should have installed. I’ve never had this list written down somewhere, so I’d just end up getting them one by one when I wanted to use something and realized it’s not yet installed  -  ”oh I need this too… ah and this” – and waste a lot of time with it.

So it’s best to have a good basic todo list to be more efficient. We need to…

1. Install updates

Yeah I know, this is kinda obvious. The first step I always do after the basic installation is done is to go through with the installation of all important updates. This takes a while and might involve a few reboots, but should be a straightforward process. Windows 7 and Vista will probably offer you also optional updates with drivers for some of your hardware. It’s ok to install them if you want, but usually if you’d like the latest drivers you should check directly at the hardware manufacturer’s site. (see next step)

2. Install drivers

Ok, this is important! Even if you have the driver CDs at hand for your hardware, odds are those drivers are fairly out of date, and you’ll find newer ones on the web. First you need to check your motherboard manufacturer and exact model. If you don’t have the package or invoice at hand, there are various free system information tools that will show all necessary information about your hardware. I recommend Astra32.

Once you have the info you can search for the model on the manufacturer’s site (ex. Gigabyte.com) and download all the drivers/utilites you find there for your specific model. In most cases, besides chipset drivers you will also find sound/network/graphics drivers, depending on what is included on your motherboard.

For dedicated graphics you will probably either have Nvidia or Ati:

http://www.nvidia.co.uk/Download/index.aspx?lang=en-uk

http://support.amd.com/us/gpudownload/Pages/index.aspx

3. Install essential or useful programs

Usually quite a lot of time is spent with hunting down and installing useful apps one by one. However there is a cool online service that makes things much easier: http://ninite.com/. There you can find a collection of some of the most used and useful free Windows apps, it’s quite comprehensive. Just select all you need and install with a single installer.

For the lazy here is a pre-defined selection I’ve made: http://ninite.com/installer/f561fbceda0ef6518aa4a7e76d01c82df9b1bd33

If you have a commercial antivirus license, of course you can skip the free antiviruses from Ninite’s collection. But always install an antivirus. I highly recommend Ad-Aware besides an antivirus, I have it on all my computers and it’s proven to be effective in multiple instances.

Other useful programs not included in Ninite:

  • Daemon Tools:  excellent for working with virtual CDs/DVDs  (ex. .iso files)
  • MMD3 Winamp skin: THE best skin ever made for Winamp.
  • DAP: My favourite download manager.
  • Speedfan: A great tool for monitoring your system’s temperatures.

And now you have a PC that’s ready to be used for most tasks. Of course there are still a lot of useful programs to be installed eventually, as you need them. But this depends on each user’s needs. The above is a pretty good baseline to start with. I’ll probably add more to this list, if you have any good suggestions.


May 4 2010

Add foreign key constraints in South migration

Béres Botond

Recently I needed to do something specific in a migration. I wanted to add foreign key constraints, but without any other change to models. Basically there was no change to “frozen” models, those were already correct, but somehow the database itself was missing a constraint (legacy database – app has only been converted to South fairly recently). I wanted to avoid raw SQL if possible, but initially looking at the docs I saw a db API method for deleting FK constraint but not for adding one. But after a quick look at the source it turns out there is a simple method:

def forwards(self, orm):
        sql = db.foreign_key_sql('from_table', 'from_field', 'to_table', 'to_field')
        db.execute(sql)
 
def backwards(self, orm):
        db.delete_foreign_key('from_table', 'from_field')

By the way if you develop applications with Django and are not using South yet (or at least some other migration tool), you better check it out, as it is currently the most up to date and stable migration app for it.


Apr 29 2010

Dynamic translation with django-datatrans

Béres Botond

A couple weeks ago I’ve faced the task of adding full i18n support to a large django project, with a lot of legacy code.  Besides dealing with full static i18n coverage, which is not really difficult just tedious and time-consuming, there remained the matter of translating texts stored in the database,  for a lot of models. I expected this to be non-trivial, given my desired requirements:

  • No significant changes required to existing code (ex. querying data, form processing, referring to model fields etc.).  Querying of data based on current language should be transparent.
  • Has a decent interface so that normal users can fill out translations.
  • Ideally it should be fairly recently updated and working well with Django 1.0 – 1.1.
  • Also as few changes to models and/or database structure as possible.  So most likely an app that implements the registration pattern.

What I love about django, besides django itself, is the fact that it has a strong and active community that produces many very useful and stable 3rd party apps.  So the first step was to look around for dynamic translation apps and sure enough there is quite a few of them out there. This great article provides an excellent overview of existing dynamic i18n apps for django. Unfortunately most of them are outdated or do not look too promising. Only one that looked like it could be  a candidate to be deployed on a production environment was django-modeltranslation at first. But then I noticed django-datatrans in the first comment, a fairly new app by Jef Geskens and it soon became the top pick. Although django-modeltranslation is also a good option,  I didn’t like the fact that it adds extra fields for each language on each model, even if it’s done dynamically with registration pattern. Especially that I had a lot of models to work with.

With django-datatrans you have your translations in a single table so your existing database structure is untouched. It’s also stored by hash, so you don’t have multiple records for the same string. The transparent model API works perfectly, I’ve encountered no issues at all so far, after applying it to 50+ models , nor did I have to modify a single line of existing code to make it work. The admin-like interface for editing your translations also will fit most needs, although there is some room for improvement there.  So if you need to translate some of your dynamic data as painlessly as possible, I strongly suggest you check out django-datatrans.