Boost library rocks!

26 12 2007

I have been messing with a project through the university and we decided that we would go with the Boost library for the project, so to read up on Boost and how to use it, I decided I would attack Project Euler using Boost. One area that Boost really showed its strength was determining how many Sundays fell on the first day of the month between January 1, 1901 and December 31, 2000. In just a few lines, the answer was apparent. Here is the lines of code that answers this problem, and answers it immediately. I had used other calendar solutions in C++ in the past, but the gregorian.hpp library is fast.

#include <iostream>
#include "boost/date_time/gregorian/gregorian.hpp"
 
int main(void)
{
    using namespace boost::gregorian;
    int count = 0;
    date_period dp(date(1901, Jan, 1), date(2000, Dec, 31));
    day_iterator iter(dp.begin());
 
    while (iter != dp.end())
    {
        if (iter->day() == 1 && iter->day_of_week().as_enum == 0)
            count++;
        ++iter;
    }
 
    std::cout << count << std::endl;
    return 0;
}

The answer takes milliseconds, it is just that fast. Right now I would like to replace my gmpxx libraries for big numbers with a boost library and I think all of my Euler answers will be “boosted.”

::

Opening an Access DB

30 10 2007

I am currently working on a DB API for a Java application, and one of the requests was to be able to query Microsoft Access Databases. OK, no problem I thought. I could easily open up these databases with OpenOffice.org Database or even Kexi. WRONG! Neither of them would open or even import the database. I personally am not a fan of OO.o, so I went over with Kexi. After some fiddling around, I did apt-cache search mdb just to see what there was. Low and behold there were two applications of interest. kexi-mdb-driver and kexi-mdb-plugin. I first tried the plugin and all that did was make Kexi crash. So I removed the plugin and then tried to use the driver instead. Booyah! It worked like a champ. It imported not only the structure 100% correctly, but also all of the data.

/me gets back to hacking now that he knows what is in the database itself. </query_hell>

::

IPv6 and Python question

27 06 2007

OK my lazywebbers with IPv6 and Python experience, this goes out to you!

I am creating an application right now, with Python of course, and I need to set an environment variable to be always on when a user logs in. I have messed with the os.environ and the os.putenv and what not, but it doesn’t work permanently. This application doesn’t do anything more than a few tests and then sets the environment variable if the tests return true. Is there an easy way to write to ~/.bashrc or ~/.bash_profile to enable the variable, and then later on if the user wants they can disable the variable which would either remove the variable all together or would set it to False?

Another question I have is with IPv6. Is there an easy way to test and see if your network is an IPv6 network or an IPv4 network? Right now I am grepping /var/log/syslog for ‘no IPv6 router present’ and printing the ‘no’. If it is ‘no’ then the network is IPv4, and if it returns 0, then it is an IPv6 network. This is rather hackish I feel, but it does work. Just wondering if there is a better method to do this simple task.

Thanks everyone!

::