FizzBuzz Ruby
Well since I was in the mood writing FizzBuzz and since I've been working on learning Ruby, I figured, why not do recursive fizzbuzz in ruby?
Blog of Titus Stone; Part programmer, part... wait... what?
Well since I was in the mood writing FizzBuzz and since I've been working on learning Ruby, I figured, why not do recursive fizzbuzz in ruby?
Posted by titus at 3:17 PM 0 comments
It's funny how times changes. 9 years ago I purchased a Toshiba laptop for college. It set me back $1800 and came "loaded" with a Pentium 3 @ 600Mhz and 64MB RAM. I promptly maxed out the RAM to 192MB. I ended up hating it and vowed never to buy a laptop again.
Posted by titus at 2:59 PM 0 comments
I'm a "city boy" when it comes to programming languages. 90% of my experience in programming is in ridiculously high level languages like VB.NET, C#, and Python. I haven't "roughed it out" much in the wilderness of some low level language.
Write a program that prints the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers which are multiples of both three and five print "FizzBuzz".
Posted by titus at 9:06 PM 0 comments
I answered a question (#1626326) on Stack Overflow recently about Django setttings.py files which reminded me I'm still undecided on what the best way to handle this issue.
The issue is -- How do you handle Django settings which change based on which environment the web app is running in? The Django documentation recommends using something to this effect.
if DEBUG:I used this approach for a while, but found that it was becoming limiting. The reason why is that it doesn't actually address the issue of environment. Where the "if DEBUG" trick is used, we're really only looking to see if we're in DEBUG mode, not if we're running in a development or production environment.
VALUE = 'something'
else:
VALUE = 'something else'
PRODUCTION_SERVERS = ['WEBSERVER1','WEBSERVER2',]This is easily an improvement over "if DEBUG". At least now we have some control over values based on environment. I used this for a while and then realized...
if os.environ['COMPUTERNAME'] in PRODUCTION_SERVERS:
PRODUCTION = True
else:
PRODUCTION = False
DEBUG = not PRODUCTION
TEMPLATE_DEBUG = DEBUG
# ...
if PRODUCTION:
VALUE = 'something'
else:
VALUE = 'something else'
if PRODUCTION:Bad things would follow. The testing copy of the web app (testing.domain.com) would load up, and mark PRODUCTION=True as it is technically on the production server. It then uses the production MySQL database. Fail.
DATABASE_HOST = '192.168.1.1' # Production MySQL
else:
DATABASE_HOST = 'localhost'
Posted by titus at 9:03 AM 0 comments
Labels: django, settings.py
About django admin in a sec. First a bit of setup:
As I've been organizing our office into using an issue tracker for keeping track of our Django app, I haven't really found one that 'fit' yet. The issue trackers I looked at had 1 of 2 problems:
class IssueAdmin(admin.ModelAdmin):The easiest way to get the 'priority' column in color would be to render it as HTML. If you were to try this, you'd find that the Django admin does not escape that value, and you'd end up with < >'s showing. However, there is an API to get around that. In Python all functions are objects. Because of this, we can add a property '.allow_tags' at runtime to signal to Django that the value contains tags.
list_display = ('title', 'priority', 'completed', 'assigned_to', 'last_modified')
# models.pyOur value now shows in color, but the column header renders as "Priority html". We can add one more property 'short_description' which would allow us to specify the equivalent of 'verbose_name' for a field.
def priority_html(self):
return u'<span >%s</span>' % self.get_priority_display()
priority_html.allow_tags = True
# models.py
class Issue(models.Model):
PRIORITY_CHOICES = (
(1, 'Low'),
(2, 'Normal'),
(3, 'High'),
)
priority = models.IntegerField(choices=PRIORITY_CHOICES, help_text='Default: Normal Priority', default=2)
def priority_html(self):
if self.priority == 3:
color = "652D90"
elif self.priority == 2:
color = "37B34A"
else:
color = "26A9E0"
return u'<span style="color:#%s">%s</span>' % (color, self.get_priority_display())
priority_html.allow_tags = True
priority_html.short_description = 'priority'
# admin.py
class IssueAdmin(admin.ModelAdmin):
list_display = ('title', 'priority_html', 'completed', 'assigned_to', 'last_modified')
Posted by titus at 4:16 PM 1 comments
Labels: django, django-admin
One of the things that's been somewhat of a small hassle in working with Django/Python is managing dependencies across multiple developer machines, especially through an SVN managed project. Whenever a new dependency is needed, we've ended up doing 1 of 2 things:
# manage.py (before Django loads itself)
import os
import sys
def set_dependency_path():
dep_root = os.path.join(os.path.abspath(os.path.dirname(__file__)), '_dependencies')
if not dep_root in sys.path:
sys.path.append(dep_root)
set_dependency_path()
# __init__.py (the main, root init)
from manage import set_dependency_path
set_dependency_path()
Posted by titus at 6:22 PM 0 comments
Oh man, I feel accomplished. I just finished hacking wmd editor to use a custom image gallery being generated server-side.
If you don't know, WMD editor is a WYSIWYG editor written in Javascript.
By default, when you click on the 'Insert Image' button you get a prompt allowing you to enter a URL.
Insert Image Screen
The functionality I was looking for was instead of showing you a text box where you could paste in the URL, I wanted to see more of a gallery type interface, that showed media that both you and others had uploaded. I also wanted to have a system where multiple files could be uploaded at once, and multiple files could be inserted at once.
There were primarily two challenges in attempting to accomplish that plan:
command.doLinkOrImage = function(chunk, postProcessing, isImage){
// ...
if (isImage) {
// OLD: util.prompt(imageDialogText, imageDefaultText, makeLinkMarkdown);
// WMD_IMAGE_GALLERY_URL loaded from a global settings elsewhere
util.imageGallery(WMD_IMAGE_GALLERY_URL, makeLinkMarkdown);
}
else {
util.prompt(linkDialogText, linkDefaultText, makeLinkMarkdown);
}
}
util.imageGallery = function(gallery_url, makeLinkFunction){
gallery = $('#wmd-media-gallery');
if (gallery.html()) {
gallery.dialog('open');
}
else {
gallery.css('visibility', 'visible');
gallery.load(gallery_url, function(){
gallery.dialog({
title: 'Media Gallery',
bgiframe: true,
height: 550,
width: 900,
modal: true,
buttons: {
'Insert': function(){
// Enumerate images; insert those checked.
$('#wmd-media-gallery .media-insert').each(function(){
if ($(this).is(':checked')) {
img_path = $(this).attr('rel');
console.log(img_path);
makeLinkFunction(img_path);
}
});
$(this).dialog('close');
},
'Cancel': function() {$(this).dialog('close'); }
}
});
});
}
};
![alt text][1]What it was rendering was this:
![alt text][2]
[1]: /some/url.jpg
[2]: /some/image.jpg
![alt text![alt text][1]][1]The particular code in question lies int he command.addLinkDef() function. What needs to happen is that before that function processes the 'chunk', it must first clear the settings from the previous image out of it.
var getLink = function(wholeMatch, link, id, end){
// ...
};
// This goes after the getLink function
if (chunk.after.substring(0,2) == "][") {
chunk.before += chunk.selection + chunk.after.substring(0,2);
chunk.after = chunk.after.substring(2);
var linkEnd = chunk.after.indexOf(']');
linkEnd++;
chunk.before += chunk.after.substring(0, linkEnd);
chunk.selection = "";
chunk.after = chunk.after.substring(linkEnd + 1);
}
// ...and before the chunk.before [...] code
chunk.before = chunk.before.replace(regex, getLink);
Posted by titus at 4:27 PM 0 comments
Labels: jquery, wmd-editor
© Blogger template 'Minimalist G' by Ourblogtemplates.com 2008
Back to TOP