## 10 December, 2009

### Minix 3 has FPU(x87)/SSEx hardware support now

As I promised before here are some news about my activity at minix 3 project. My implementation of "Floating-point driver for MINIX 3" (aka "FPU hardware support"), which I had done in summer and added just few recent modifications later, was committed last week, so now minix' userland can forget about using software implementations of FP and SSEx instructions and switch to hardware.

So my first OS project is officially accepted and finished :-)

## 01 October, 2009

### Integrals with exponent and polynomial: Pn(x)e^{ax}

I've found a very cute way to work with integrals like $$\int_{}^{}P_n(x)e^{ax}dx$$ Before I always integrated it by parts, for example: $$\int_{}^{}xe^xdx=xe^x-\int_{}^{}e^xdx=(x-1)e^x$$ In case you need to take by parts only once it's ok, but when Pn(x) is x^2 or higher degree you need more time to integrate by parts several times.
There is another better way, let's take above integral again:
$$\int_{}^{}xe^xdx=(Ax+C)e^x$$ So now you should take a derivative of the right side and solve it against left side: $$xe^x=Ae^x+(Ax+C)e^x$$ If you divide by e^x, you see that A=1 and C=-1 so you get the same correct answer as above (but easier!) So in common case: $$\int_{}^{}P_n(x)e^{ax}dx=Q_n(x)e^{ax}$$ Where $P_n(x)$ is polynomial of degree $n$ and $Q_n(x)$ is polynomial of degree $n$ with unknown coefficients to find using equation: $$P_n(x)e^{ax}=Q'_n(x)e^{ax} + Q_n(x)ae^{ax}$$

## 30 September, 2009

### Using mplayer as alarm in linux

Some time ago my stereo died, so I started to use my PC as alarm (I like to listen some music during wake-up). For some time I used it in this way:
echo "mplayer -ao alsa -shuffle /docs/music/unsorted/*" | at 10:00

"-shuffle" option makes mplayer to play files in random order.
Everything worked fine, but some time ago I didn't here my sweet alarm. I wasn't able to figure out the reason, so asked at #mplayer. Lobs suggested to use "-noconsolecontrols" option to fix it and it helped. Also "-really-quiet" option can be useful if you don't want at to produce big mails with mplayer's output. I guess it can be also useful for using mplayer with cron.
So the final solution is:
echo "mplayer -ao alsa -noconsolecontrols -really-quiet -shuffle /docs/music/unsorted/*" | at 10:00

## 19 August, 2009

### I'm back...

Now I'm back. It was a very hard year for me, but now I'm better and do everything to keep going.
To raise my spirits I've done two things:
• I've bought eivanov.com. It will be used as alias for this blog until I have something special.
• Installed SyntaxHighlighter, which should be useful for posts containing snippets (see previous post for example). If you want the same I recommend to use this Howto.
Very soon I will post about adding view permission and simple ACL support to Django Admin application. I've implemented it in February, when I worked in Gamekeeper company.

Also during last month I've implemented support for Hardware FPU in Minix. Probably I will just summarize this small OS dev experience, since this project mostly is about reading Intel doce, reading minix book, writing piece of code and playing with nasty ASM bugs (I was new to ASM, when started the project).

## 05 February, 2009

### Simulating post-request (using form-data) with file in C/C++

First I started to use WinInet. God, it sucks like most Windows API. I played about a day with http://support.microsoft.com/kb/165298
And I failed to post anything, from what django could make request. Aslo wierdshark shown that MS does a bit strange request (it looses some header about multipart form-data).
Today I took libcurl, it has a rich example who you can perform your POST-request.
Note, that it's just example and you have to define a record and a record_length.

As for me, I took such piece of code from there:
 CURL *curl; CURLcode res; // curl_global_init(CURL_GLOBAL_ALL);  curl = curl_easy_init(); struct curl_httppost* post = NULL;   struct curl_httppost* last = NULL;   struct curl_forms forms[2];   char file1[] = "d:\\backup\\thumb.jpg";   forms[0].option = CURLFORM_FILE;   forms[0].value = file1;   forms[1].option = CURLFORM_END; curl_formadd(&post, &last, CURLFORM_COPYNAME,               "pictures",                 CURLFORM_ARRAY, forms, CURLFORM_END); res = curl_easy_perform(curl); curl_easy_cleanup(curl);

Pretty enough. But python is always prettier: snippet
;)

## 02 February, 2009

### Storing files in database with django

Yesterday I got much pleasure implementing DatabaseStorage class for django. Nothing special and a very simple thing, but I like it: http://www.djangosnippets.org/snippets/1305/

Thanks to python and django developers for marvelous duet: python and django :) It really rocks and doesn't have boundaries!

## 23 January, 2009

### ManyToMany relations in Django.

I didn't write anything for some time, since was very busy with my work and family problems.
But today I have some interesting thing about django, so I can't keep silence (I'm new to python/django, so it can be interesting for newbies only, but I hope it is not).

First of all I want to say sorry for indentation in quoting blocks... Will fix it later...

In django to define ManyToMany relation you should use ManyToManyField in your model. Consider folowing code:

class User(models.Model):    #...    groups = ManyToManyField('Group')class Group(models.Model):    #...

If you use admin or just manually create a form from the model, you will get a widget to edit M2M just for User. And it's normal since according to docs "By default, admin widgets for many-to-many relations will be displayed inline on whichever model contains the actual reference to the ManyToManyField". But in some rare cases you might need widgets be displayed on both models (some people think it's abnormal in common and some think you shouldn't tune your models for such tasks, but forms instead).

Since I was a newbie I had written:
class User(models.Model):    #...    groups = ManyToManyField('Group', related_name='groups')class Group(models.Model):    #...    users = ManyToManyField(User, related_name='users')

And before I looked into database I thought I got expected result... Yes, django has created two different tables for two relations: User_Group and Group_User. And it's pretty logical for models.

Then you can try:

class User(models.Model):    #...    groups = ManyToManyField('Group', related_name='groups',            db_table=u'USERS_TO_GROUPS')class Group(models.Model):    #...    users = ManyToManyField(User, related_name='users',            db_table=u'USERS_TO_GROUPS')

If you use it with existing db then it will be fine, but if you try syncdb it will try to create two identical tables. So we came to the final snippet:

class ManyToManyField_NoSyncdb(models.ManyToManyField):    def __init__(self, *args, **kwargs):        super(ManyToManyField_NoSyncdb, self).__init__(*args, **kwargs)        self.creates_table = Falseclass User(models.Model):    #...    groups = ManyToManyField('Group',related_name='groups',db_table=u'USERS_TO_GROUPS')class Group(models.Model):    #...    users = ManyToManyField_NoSyncdb(User,related_name='users',db_table=u'USERS_TO_GROUPS')

Here it is: now ManyToManyField widget is displayed on both forms and of cource on admin add/change pages for both models.

In case you use Django Admin you can try to inline intemediary model, but it will be just a kind of a many-to-many relation (no multiselect, but lot of inlines models). In our case it's not a good solution.

Since you work with m-2-m I want to point you to a good snippet for integrating legacy databases: http://www.djangosnippets.org/snippets/962/
In my code I have combined it with my snippet.