Archive for the 'Tech Stuff' Category

There are multiple ways of making your app the default web2py application, they are detailed here in the section called ‘Application Init’. When an app is the default web2py app, you don’t have to specify its name in its url when you visit it in your browser.

 

Here is a summary of the method that I prefer.

 

First create a file in your web2py directory called routes.py, you can rename routes.example.py to routes.py but if you do, remember to delete any example code that it may contain.

 

Add the following code to routes.py:

Replace your_app_name_here with the name of your web application, and the restart web2py. How you restart web2py depends on how you run it, for example in my case I restarted apache.

 

When web2py has restarted you should now be able to access your app via a naked url.

 

Q: I have a nice accordion in my web app set-up in twitter bootstrap but how can I programmatically open or close a section using javascript?

 

A: Use the .collapse() function to open, close or toggle a collapsible accordion section.

 

Consider this 2 section accordion:

Then, as an example, to automatically open the second accordion section (identified by ‘#category-2′) when the page loads you could do the following:

This technique can be used to automatically re-open a previously open accordion section on page refresh, to do this first use the history api to update the current page location whenever a user clicks to open an accordion section – store an id to the open section in the page arguments.

 

Then when the page loads, for example on a refresh, if this argument exists parse it from the page loaction and then call collapse(‘show’) to open the section…

 

More details on .collapse() can be found here.

 

In Web2py if you want to change a model’s definition, you can first of all change the model’s software definition via its python code and then, (depending on your migration settings), the next time you launch your web app, web2py will attempt to alter your database structure to match your updated model.

 

If this works, then it works and your database will reflect your new model, however sometimes web2py can’t make the necessary changes and your app may not subsequently load any more at all. If this happens then I have found the best way to fix the app is to delete all of the files in the app’s database directory – the next time you launch your app web2py will recreate your DB from scratch and it should launch again.

 

Now this is fine assuming that your database didn’t have loads of important data in it that you didn’t want to loose. So, to get around this problem, before trying to change a model’s definition I export all of the data in the database to a file (using a tool like this) so that if the problem happens and I have to get web2py to recreate the database ‘in its own image’ I can later reimport the previously saved data…

 

Here is some code that shows how to create an 8bit grayscale cognex image (CogImage8Grey) from an 8bit raw image stored as a byte array (byte[]). This type of memory messing is difficult in .NET at the best of times and it’s just a pity the the cognex library doesn’t help much more than it does.

 

I also have a feeling that the cognex library is doing more copying than it strictly needs to, but in true style its software documentation does not detail if, or when, it copies image data (or much else for that matter!) So I copy the image data from the byte array into a malloc’ed buffer before creating the cognex image.

 

First we have to define a SafeBuffer through which cognex can free up the allocated memory when it is finished with it, to do this we can derive a class from SafeBuffer like this:

 

Its constructor mallocs the memory and when it is disposed ReleaseHandle() is called, and this frees the memory. I also added a cast to IntPtr so that we can pass it into functions that expect an IntPtr.

 

Now that we have SafeMalloc we can write function to create the cognex image like this:

 

This function allocates memory via SafeMalloc, it then copies the raw image data from the input array into this memory. Then CogImage8Root.Initialize() is called passing in a pointer to this memory. not that in this case the image’s stride is the same as its width. Once the CogImage8Root has been initialised we can create a CogImage8Grey image and set the root via a call to SetRoot()!

 

They certainly make you work for it!!

 

If you know that nobody else will be using your image array and are willing to go ‘unsafe’(!) then you could avoid this extra memory copy by pinning the array and getting a pointer to it, you could then pass this pointer directly to the root Initialize() function. In this case you won’t need the SafeMalloc class etc.

 

Thanks to all on this thread for hints on SafeBuffer!

 

Here is a quick, dirty and inefficient example of how to save an 8bit Grey scale image stored in a C# byte array as a 32bit bitmap file (.bmp). Saving bitmaps can be quite suprisingly difficult in .NET so I am posting this for future reference!

 

This code copies each byte (8 bit pixel) in the 8bit image into an array of 32bit pixels (4 bytes per pixel) and then saves it to disk. Note that you have to build your project with the ‘Allow unsafe code’ checkbox checked (go to project properties / Build and you will see the ‘Allow unsafe code’ checkbox.)

 

 

Thanks to all on this thread for the pointers!

 

Over the last few hours I started seeing an annoying problem with Visual Studio and TFS on a geographically distributed software project that I am working on, Visual Studio kept on automatically checking out files that hadn’t been edited, I also started getting the following message before the files were automatically checked out:

 

“There appears to be a discrepancy between the solution’s source control information about some project(s) and the information in the project file(s).
Inbox”

 

After some investigation we figured out what the problem was – it was annoying problem, but in this case quite easy to fix.

 

When the TFS server was set-up originally we were all told to access it by its IP address over a partially set-up VPN. Now once the VPN was properly set-up, project contributors started to use the TFS server’s host-name instead of the IP address (as they should).

 

However, not all of the contributors were notified of the change and continued to use the IP address. Visual studio would update any files that had TFS info within them, checking them out and altering them to use either the TFS host name or IP address depending on the particulars of the local Visual Studio TFS set-up – hence all the trouble and all of the annoying error messages!

 

As soon as we changed from using the IP address to using the host name the problem went away.

 

It may well appear again however, as soon as someone else who uses the IP address instead of the host name checks in some files…

 

TFS is quite a good source control system but every now and again you can hear worrying echoes of its ignoble Visual Source Safe history and some of the hair-brained and crazy stuff that comes with it!

 

If you are trying to build some code with Visual Studio 2010 and get build errors like this:

 

The type or namespace name ‘CodedUITest’ could not be found …

 

Then it is probably because you are building with an edition of Visual Studio that is not Premium or Ultimate – the professional edition does not have the UI Test Tools built in.

 

If you are lucky you will be able to just remove the offending test project(s) with the broken references from the solution and build the rest – typically it will be only the test projects that won’t build.

 

To do this, choose the Build / Configuration manager… menu and un-check under the ‘Build’ column for any test projects that aren’t building.

 

Or shell out for an upgrade of Visual Studio!

 

Lots of software development time wasted yesterday due to a rubbish error and associated error message produced by the Visual Studio Unit test framework!

 

The agent process was stopped while the test was running.

 

No resolution as of yet – but I think it is due to a worker thread in a library that outlives the test’s existence… The problem doesn’t happen if I run the unit test as debug via the ‘Test / Debug / Run Tests in Current Context’ menu. Apparently, from my research it seems that this problem will also not happen in Visual Studio 2012, cold comfort indeed….

 

If I get it fixed Ill let you know…

 

If you are doing modern C++ development these days you will be using the boost libraries. I have started using the Raspberry Pi as a stand-in ARM platform for some smart-cam development while I wait for the actual hardware to become available and as such need to install boost. To this end, the boost 1.50 libs can be installed on wheezy as follows:

Now for a little example of using and building with the libs, consider this little test program that uses the boost regex library:

If we stick this code into a file called it.cpp then we can build it using C++0x (C++11 ish), the standard c++ lib and the boost regex lib like this:

 

This will output to a.out, which can then be executed like this:

 

On the whole Visual Studio is a great software development tool with a relatively low number of annoying bugs. However, there is one annoying bug that Microsoft doesn’t seem to be bothered enough to fix…

 

The problem happens if you remove and re-add a file reference to a DLL. When you re-add the reference (same file name in a different folder) by browsing to a different file location, Visual Studio will for some reason insist on using the previous file path to the DLL and not the new one!!!

 

You can find some more information about the problem here.

 

We were trying to change a reference’s path by removing and re-add it but it just didn’t work due to this bug!

 

So how can we get around this problem? Well one way is to change the reference path directly by editing the project file.

 

To do this, right click on the project in the solution explorer and choose ‘Unload Project.’ Once the project is unloaded, right-click on it again and then choose ‘Edit’, the project’s XML will be displayed in the editor.

 

Now search for references to the incorrect reference path and change it to your new path. To do this, look near the following elements:

<ItemGroup/> and <Content/>

and:

<Reference> and <HintPath>
 

Finally, right-click on the project again and choose ‘Reload Project’ – check that the references are correct….

 

I hope this helps someone, it was the only way we could figure out how to get around the problem…