Google+ Pieces o' Eight: October 2011

Friday, 7 October 2011

Setup Virtual Box shared folders with Linux Mint guest

Here's how to setup Virtual Box shared folders with a Windows 7 host and a Linux Mint 11 guest and have it automatically mount in Linux Mint on boot.

Software used:
Windows 7 (host OS)
Linux Mint 11 (guest OS)
VirtualBox v4.1.4

Instructions
1) Create a folder on your host machine - I created mine in the root of C: and called it Shared.
2) Start your Linux Mint virtual machine.
3) Once booted, click the Virtual Box Devices > Shared Folders... menu option:


4) Make sure Machine Folders is selected and click the Add button:


5) Navigate to the shared folder on the host and tick the Make Permanent box. 


6) In Linux Mint we now want to create a folder where the shared folder will be "mounted". Open a terminal and type:
sudo mkdir /mnt/shared
and enter your password when prompted.

7) We'll now make the share mount on boot. In the terminal type:
sudo gedit /etc/fstab

8) At the bottom of the file add a new line that reads:
Shared    /mnt/Shared           vboxsf           default               0        1

9) Save the file and reboot Linux Mint.

10) If everything went to plan, you should now be able to navigate to /mnt/Shared and view the shared folder!

How to: Install FreeBSD on Windows Virtual PC

DON'T BOTHER! YOU'LL ONLY WASTE YOUR TIME!

It took me about four hours to realise this, but Microsoft really don't want you hosting non-Microsoft operating systems in Windows Virtual PC. 95, 98, XP, Vista and hell even 7 hosting 7 works fine. Try a *nix O/S though and you're out of luck.

By the time I'd finally got FreeBSD installed - after much piss-balling about with vhd's - I thought I was on to a winner. But no. The cocking thing wouldn't boot and entered an eternal "Kernel Panic" reboot mode. Then, for fun, it did boot but wouldn't reboot properly. And finally, just for the kicks, it decided that the vhd was no longer a valid boot device!

I don't blame the FreeBSD lot for this. I installed VirtualBox and FreeBSD into it on the same machine and that worked fine. I'd suggest you do the same if you fell into the same Microsoft trap I fell into.

Thursday, 6 October 2011

List comprehensions are cool!

Lists - or arrays - are an essential part of programming. While their usage varies depending on the problem being solved, the typical method of constructing one is to create a loop and append each new item to an array or list, as the following pseudo-code shows:

myList = new List

for i = 0 to 9

    myList.append(i)

end for

While this is perfectly ok, Python - and indeed other languages - provide an alternate method of creating lists based on existing lists known as list comprehensions. Lets see how the above pseudo-code example could be implemented in a Python list comprehension:

[i for i in range(10)]

The first and most immediate benefit is that the list comprehension took only one line of code to implement. This cuts down on the amount of boiler-plate required to do something as common as creating a list and also has the effect of making the code easier to read.

What if we wanted to build a list containing only the even numbers in the range? Well this is catered for by including an if clause in the comprehension:

[i for i in range(10) if i%2 == 0]

(Here I used the modulo division operator to only append the item (i) to the list if the remainder of the division i/2 is equal to 0)

List comprehensions aren't just for ranges of numbers:

[c for c in "abjkfghi" if c in "abcdef"]

In this example I'm scanning through a string and only appending the character to the list if it appears in the string "abcdef", and:

[d for d in os.listdir(r"C:\Photos") if d.startswith("Holiday")]

here I'm looking though the directories in my Photos directory and only appending those directories that start with the word Holiday.

As you can probably see from those few trivial examples, list comprehensions are pretty flexible and powerful and they save a considerable amount of time!