Limi’s Sphere of Influence

dabbling, frivolling, idling, loafing, loitering, playing and procrastinating

Archive for the ‘Programming’ Category

So yes, I recently got myself a new home server off of eBuyer, and I decided to install Ubuntu on it. After installing various other packages without issue the first to cause problems was Rails (it always would be wouldnt it). Either way, when you try to run…

sudo gem update --system

It goes…

/usr/bin/gem:23: uninitialized constant Gem::GemRunner (NameError)

Which is really useful and does exactly what you want it to.. not. The fix is to go edit the gem file…

/usr/bin/gem

In there, hunt for the line…

require 'rubygems'

…and just after, put this…

require 'rubygems/gem_runner'

Problem solved!

  • 0 Comments
  • Filed under: Rails, Ubuntu
  • So, this morning I finally entertained Tortoise’s incessant whining to update to its latest version (1.5.2) and I took the plunge. The one thing that annoys me the most about the upgrade process is the fact I have to restart the machine in order for the installation to finish, this I find is so archaic and out of date compared to 99% of all other software installation. (Obviously I’m not stupid enough to realise that there’s quite a bit of interaction on the OS level with Tortoise, but still, it’s one annoyance I would rather do without)

    Once I’m restarted and on the go again, I go to browse one of my SVN directories and I’m greeted with some disgusting icons. They’ve gone and replaced the XPStyle icon set with some… quite frankly, god awful ones. They blur horribly at 16×16 size and just don’t look nice at all in my opinion, so it was then finding out how to change the icons.

    1. Right click on the desktop and in “TortoiseSVN” click “Settings”
    2. Select “Icon Set” from the left tree menu
    3. Then using the drop down to change to which icon set suits you.
    4. Restart the machine (*sigh*)
    After I had looked through the other sets I went for “Straight”, however after a days work with this, I just wanted the original icons back. Thankfully I had an old installation of TortoiseSVN on another machine so it was just a case of moving & renaming the icons from one place to another.
    To get the old icons from 1.4.x into 1.5.x simply download the following file, and extract the folder into…
    %commonprogramfiles%\TortoiseOverlays\icons
    You can just put that into the Run box and it should load up the correct directory. Simple extract the zip file into here and then use the step-by-step above to change your icon set to the new one named “LegacyStyle”.
  • 1 Comment
  • Filed under: Programming, SVN
  • If you’re a programmer of some description and you use SVN and develop on Windows then chances are you’ll either be using SVN with TortoiseSVN, or you’ll be like one of those trendy kids using GIT. However, those cool kids don’t yet have something like TortoiseGIT. But I’m sure that once it comes out that these optimizations will still apply.

    The part of TortoiseSVN that really slows down PC’s is the “TSVNCache.exe” file. This basically allows for drawing of the icons on-top of files and folders to indicate their SVN status. However, doing this requires a lot of monitoring of literally all your files and folders. So whats the most obvious thing to do? Restrict what Tortoise monitors as it goes about its work. We do that as follows…

    1. Right click on your desktop and select “Settings” from the Tortoise context menu.
    2. In the tree view on the left select “Icon Overlays”
    3. In the “Exclude Paths” box put in C:\*, this will exclude the entire drive from being monitored. You’ll want to put in the drive names of all your fixed drives to stop those being monitored as well.
    4. In the “Includes Paths” box put in the root of your development directories, I only use one, however you may use many scattered across your drive. I simply put in C:\SVN\*
    5. Press “ok” to apply these changes.
    6. Either reboot your machine, or, if your like me, open up task manager and kill “TSVNCache.exe” and load up a folder associated with SVN and it should re-start itself.

    Other improvements can be made by setting Tortoise to only show the icon overlays when you are browsing directories with explorer. By default whenever you have an open/save dialog it will load in and show you those. These can be disabled by the following…

    1. Right click on your desktop and select “Settings” from the Tortoise context menu.
    2. In the tree view on the left select “Icon Overlays”
    3. Check the box for “Show overlays and context menu only in explorer”
    4. Press “ok” to apply these changes.
    5. Either reboot your machine, or, if your like me, open up task manager and kill “TSVNCache.exe” and load up a folder associated with SVN and it should re-start itself.
    Depending on how much you want to speed up Tortoise you can also disable the icons for files, but keep them on for folders. Done the same as above but just change the “Status cache” to “None”.
  • 0 Comments
  • Filed under: Programming, SVN
  • For those who know me (hi people on facebook) you might know that over the past few weeks I have been slowly making a gallery in Rails for my personal photos. This started off the back end of being annoyed at Flickr’s free account only allowing you to create 3 sets (or albums) before you had to pay for more. This annoyed me as I do like the Flickr layout, but I’m not really prepared to pay for it just yet.

    So instead, I decided to make my own Flickr clone, and this has been aptly named Snapr!

    I started off by looking through their design, seeing how they spaced and laid things out, but one thing in particular that interested me was how pictures seemingly look better on Flickr. Now of course, its not just because of its design that compliments images and that’s enough to make them appear to be better. But as stupid as it sounds, pictures on Flickr do look better, it took some research and testing to finally work out how they do it, but here it is.

    After digging through the code in the Mac Flickr Uploader, which is quite handily open source, it was just a case of attempting to follow the code through to work out what they did. They use GraphicsMagick for image manipulation and this is the code that makes images look great…

    // Find the sharpen sigma as the website does
    double sigma;
    if (base <= 800) { sigma = 1.9; }
    else if (base <= 1600) { sigma = 2.85; }
    else { sigma = 3.8; }
    // Create the actual thumbnail
    img.scale(dim.str());
    img.sharpen(1, sigma);
    img.compressType(Magick::NoCompression);
    img.write(*thumb_s);

    This is the last part of the generation of thumbnails, and as you can see, it scales down the image, then sharpens it by this magical sigma amount. To replicate this I had to actually play around (for quite a while) with that sigma figure so that I could make a like for like replication of their images. Obviously they don’t compress the thumbs, as if you do, they look horrible.

    Now initially, this was easily to replicate in Rails with RMagick, however when I switched over to JRuby there isn’t a RMagick gem, which is more than a slight annoyance. After some help from a friend I had my own implementation of pure Java for image manipulation, and this worked fine. However I had been using the attachment_fu plugin to make uploading and resizing of images a breeze. (Read: if you aren’t using attachment_fu, use it!). So the next step, how to get this new Java code to work with attachment_fu. 2 evenings worth of playing and testing later (and my complete lack of any real Java knowledge) I admitted defeat. So, at this point I have a fully working gallery, minus image uploading and processing, which as far as galleries go, meant it pretty much useless. What was I to do next?

    Some Googling later and ImageVoodoo was my knight in shining armor! ImageVoodoo is a JRuby gem which has like for like API methods for ImageScience (an image processor supported by attachment_fu). Everything looked like the gallery could finally begin working again. However the next stumbling block was going to appear, ImageVoodoo doesn’t support image sharpening. I was wondering at this point just how much more annoying this could get; this project was only meant to be something quick to get me back into writing Ruby again, but was quickly turning into a complete headache. After some more eventual playing I again had to admit defeat, not so much through not wanting this to work or lack of knowledge, but the want of a working gallery was greater at this point.

    Where am I at now then? Well, the gallery is operational, but image sharpening & image compression are out at the moment. So thumbnails look like a 4 year old has been let loose with Photoshop and somehow managed to find the resize controls but on doing so, decided to lower the quality to about 10. Meaning that my lovely photos don’t look as good as they could at this moment. However with attachment_fu storing the original image all is not lost just yet, as when I finally figure it out I can simply regenerate the thumbnails.. (well, I hope I can, haven’t actually looked into how to do that yet).

    It baffles me however that RMagick, which is quite frankly a huge part of Ruby image manipulation, doesn’t have a JRuby gem. Admittedly there is RMagick4J, but it’s lacking the same problems as ImageVoodoo, no sharpening support or image compression controls. If only RMagick worked on JRuby without having to think about it…

    If you’ve ever worked with Firebug you’ll know its possibly the best tool out there for aiding in web development within Firefox; thankfully there are some really handy extensions for it to make it even more indispensable!

    Firecookie

    Allows the same controls that you get with HTML to apply to cookies, so far I had been using the Web Developer toolbar to view/change cookies, however this allows for much easier editing. It also has a feature to show you when cookies are changed, which is great for debugging.

    YSlow

    YSlow

    This is useful to work out either why your site is running slow, or for you to make improvements to make it faster. It has various rule sets which can be found here which define what exceptions it allows.

    It’s going to be that time of year again soon, when a browser gets updated and all developers frantically have to make their websites compatible and all working fantastically. However, this time around the team behind Internet Explorer 8 are going to make this much easier on all us devs.

    They are doing this by allowing the web-site itself to control which rendering engine IE8 will use when you browse. This will mean if you site is already compatible with IE7, it can also be compatible with IE8 by simply including one line of HTML in the header…

    <meta http-equiv=”X-UA-Compatible” content=”IE=7″ />

    This will apparently force newer version of IE8 to render the current page in IE7’s format, which from a standpoint on the priority of getting things upgraded, can allow it to go a few notches down on the list rather than being the number one.

    For once it will be nice to worry about feature sets, improvements and bug fixing rather than worrying if the styling will still work when the next version of IE comes out.

    Thank you Microsoft IE team!

    This is sometihng that baffled me for a good few months until I finally figured out what the hell was going on. If you develop in rails chances are you’ll either be using Mongrel or WEBrick to run it locally, but sometimes when I went to start either of them I would get a “This port is already in use”, which wasn’t exactly fantastic.

    I would look around to try and find out what was using it, but “netstat -a” doesn’t help much, it just says its taken by a local device on the machine, although it wasn’t just port 3000, it’s the entire range of 3000 to 3030 were “in use”.

    Some days it would work, some days it would fail, from what I could tell it was completely random whether I would be able to start up the server. After senses of humours were lost to the pixies I finally worked it out, and it comes down to a little green icon with two arrows…

    ActiveSync

    It’s ActiveSync, and you’ll have this program running if you have a PocketPC phone and if the phone is connected, say goodbye to those port numbers. It steals 3000-3030 straight away when you plug it in. Some solutions I have found are to unplug my phone, start Mongrel, then plug the phone back in. Although again, this working is random and highly frustrating. Currently I have resorted to unplugging my phone when I am developing in rails and plug it back in when I’m done.

    I actually figured this out sometime last year, but saw someone come into #rubyonrails having the same problem and I thought I should probably share it.

  • 0 Comments
  • Filed under: Rails, Random, Ruby
  • Ruby and Screen Scraping

    This was more of a test to see how easy it would be to scrape some data using Ruby as I usually use PHP to do this for me. However when using PHP it always seems to be a mess of explodes and regexp’s to get what I want. So I wanted to see how other languages do it.

    So first things first I found out which packages/libraries were around for Ruby, and admittedly there’s quite a few, however upon first look what appear to be the better ones don’t work out of the box. Which is a big shame, as I spent a good few hours trying to get scRUBYt! working on my Windows XP development machine, but in the end I had to admit defeat. It just wasn’t going to work properly even though I had followed every guide you could find to get it working in a Windows environment. I was a bit disheartened as this appeared to be the best one out there as its first example was exactly what I was looking for.

    Alas, I moved on, had a quick look at Hpricot, but it didn’t seem to do what I wanted easily. After trying to find some examples of how they all worked I found srcAPI, which was very similar to scRUBYt however no matter how much I tried, I could only find one example, which was its own eBay one. It had a nice installation, just a gem, which worked first time (which one usually expects), from there I tried their example. I had read on their website that it uses Tidy to do the HTML cleaning, but you could use it without that if you told it to use the built in one, however it warns you that this should only really be used for testing, and and that was what I was doing I told it exactly that.

    Scraper::Base.parser :html_parser

    To cut to the chase, nearly 3hours, much googling and frustration later and the simplest of process’s it was refusing to scrape part of a table I already had. Telling it to use the built in one instead of Tidy was my downfall, the second I switched it back to Tidy (and a quick re-ordering of the loading code to stop it trying to load the linux version first) and it was working perfectly. This was someones comment about it all…

    Phases of scrAPI usage:

    1. Elation - Wow, this is so easy and powerful. I’m gonna scrape the world!!!!!

    2. Despair - What the hell is the syntax for the selectors, I’m so confused, and there are no docs

    3. Elation - scrAPI has great test coverage, you can learn everything you need to know about the selectors from the tests.

    Couldn’t agree more, although instead of getting help from the test cases I got help within a scrAPI cheat sheet I found. Without that I would have probably given up and tried the next one in line.

    So I’m now testing and doing more complex scraping with srcAPI, but for anyone who is a beginner to Ruby you may find this task quite challenging and probably beyond your scope of knowledge to begin with. The lack of documentation and examples for this made it increasingly hard, and most was done with guess work on how it was interpreting the HTML source code.

    Bottom line, if you can get scRUBYt! working, go with that, it’s very powerful and from the examples I have seen, very programmer friendly and will allow you to get to the data you want fast. But if you can’t get it on the go, then there’s many others available including many I haven’t mentioned, but Google knows all! (apart from examples of srcAPI :P)

  • 2 Comments
  • Filed under: Rails, Ruby
  • Not that the trend around here are DataGridView’s or anything, it just seems that the small tid bits of information about things people I expect do quite often with them are always hidden away on a dead server most of the time.

    I needed to have a column auto sorted when the application loaded, now this can be done several ways, but in my instance I do not have a DataSource as I am populating the grid manually. This is perhaps the only way to specify a sort on a manually populated table (short of clicking the header yourself)…

    dgv.Sort(dgv.Columns.GetLastColumn( DataGridViewElementStates.Visible, DataGridViewElementStates.None), ListSortDirection.Descending);

    Luckily for me this code is quite nice on the one line, however, if the column you want auto sorting isn’t either the first or last one, your going to have to go through each column until you have found the one you want.

  • 0 Comments
  • Filed under: C#
  • Again, another nugget of information I hunted around for and just couldn’t find. To alternate row styles, or more specifically, background colour…

    dgv.RowsDefaultCellStyle.BackColor = Color.White;

    Technically that should already be set from when you added the grid view, but I do it just to make sure. So now that the default is set, we can specify the alternate colour…

    dgv.AlternatingRowsDefaultCellStyle.BackColor = Color.Gray;

    If you don’t want to use the standard set colours, you can always specify your own…

    dgv.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(240, 240, 240);

  • 0 Comments
  • Filed under: C#