Practice

Apparently this book Art & Fear: Observations On the Perils (and Rewards) of Artmaking is the source of one of my favorite parables:

The ceramics teacher announced on opening day that he was dividing the class into two groups. All those on the left side of the studio, he said, would be graded solely on the quantity of work they produced, all those on the right solely on its quality.

His procedure was simple: on the final day of class he would bring in his bathroom scales and weigh the work of the “quantity” group: fifty pound of pots rated an “A”, forty pounds a “B”, and so on. Those being graded on “quality”, however, needed to produce only one pot – albeit a perfect one – to get an “A”.

Well, came grading time and a curious fact emerged: the works of highest quality were all produced by the group being graded for quantity. It seems that while the “quantity” group was busily churning out piles of work – and learning from their mistakes – the “quality” group had sat theorizing about perfection, and in the end had little more to show for their efforts than grandiose theories and a pile of dead clay.

Setting colors with an NSString

I wanted to color some letters in an NSAttributed string using a random list of colors that I generated. What I wanted to do was something like this:


NSString colorName = [NSString stringWithFormat:@"%@Color", colorList[i] ];
imageColor = [UIColor colorName];

But you can’t feed an NSString to the UIColor method. Use a selector to do it.
Like this:
 
NSString *colorname = [NSString stringWithFormat:@"%@Color", colorList[i] ];
SEL labelColor = NSSelectorFromString(colorname);
UIColor *imageColor = [Utilities uiColorFromColorName:colorname];
imageColor = [UIColor performSelector:labelColor];

I had to create pink separately since it is not in the Apple supplied color list. However, I don’t really like the other colors so I created a Utility method to create a UIColor from our RGB color values. It generates a UIColor that I can use for my attributed string.


UIColor *imageColor = [Utilities uiColorFromColorName:[lettersExploded[2] lowercaseString] ];


+ (UIColor *)uiColorFromColorName:(NSString *)colorName {

UIColor *imageColor = nil;

    if ( [colorName isEqualToString:@"black"] ) {
        imageColor = [UIColor colorWithRed:0.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1];
    } else if ( [colorName isEqualToString:@"blue"] ) {
        imageColor = [UIColor colorWithRed:8.0f/255.0f green:41.0f/255.0f blue:247.0f/255.0f alpha:1];
    } else if ( [colorName isEqualToString:@"brown"] ) {
        imageColor = [UIColor colorWithRed:95.0f/255.0f green:47.0f/255.0f blue:0.0f/255.0f alpha:1];
    } else if ( [colorName isEqualToString:@"gray"] ) {
        imageColor = [UIColor colorWithRed:140.0f/255.0f green:140.0f/255.0f blue:140.0f/255.0f alpha:1];
    } else if ( [colorName isEqualToString:@"green"] ) {
        imageColor = [UIColor colorWithRed:51.0f/255.0f green:153.0f/255.0f blue:51.0f/255.0f alpha:1];
    } else if ( [colorName isEqualToString:@"orange"] ) {
        imageColor = [UIColor colorWithRed:255.0f/255.0f green:116.0f/255.0f blue:0.0f/255.0f alpha:1];
    } else if ( [colorName isEqualToString:@"pink"] ) {
        imageColor = [UIColor colorWithRed:255.0f/255.0f green:90.0f/255.0f blue:148.0f/255.0f alpha:1];
    } else if ( [colorName isEqualToString:@"purple"] ) {
        imageColor = [UIColor colorWithRed:140.0f/255.0f green:0.0f/255.0f blue:140.0f/255.0f alpha:1];
    } else if ( [colorName isEqualToString:@"red"] ) {
        imageColor = [UIColor colorWithRed:233.0f/255.0f green:17.0f/255.0f blue:0.0f/255.0f alpha:1];
    } else if ( [colorName isEqualToString:@"white"] ) {
        imageColor = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:255.0f/255.0f alpha:1];
    } else if ( [colorName isEqualToString:@"yellow"] ) {
        imageColor = [UIColor colorWithRed:255.0f/255.0f green:255.0f/255.0f blue:0.0f/255.0f alpha:1];
    }

  return imageColor;
}

mySQL: Subqueries Part 2

You can use subqueries as column expressions. The most common use of them as column expressions is probably as averages or counts, but you can use any expression that returns a row. Here I want to select only the rows that correspond to apps.


SELECT 
(SELECT app.name FROM app WHERE app.id = (log.product_id -100)) AS name,
 product_id, COUNT( * ) AS `count`
FROM  log
GROUP BY name

mySQL: UNION Part 2

I track visitors to my app and CD pages by storing each visit in a log file. I can get the total visits for CDs with this code. I use a RIGHT OUTER JOIN because I only want to count visits for CDs. They are in the product table, so if I use a RIGHT OUTER JOIN I will not get any rows that are from the apps.


SELECT product.name, product_id, COUNT(*) FROM log 
RIGHT OUTER JOIN product
ON product.id = log.product_id
GROUP BY product_id
ORDER BY COUNT(*) DESC

Total visits for apps is from this code. I need to subtract 100 from the product_id because apps and CDs both start their ids with 1. To differentiate them, I add 100 to apps before putting them into the log table.


SELECT app.name, product_id, COUNT(*) FROM log 
RIGHT OUTER JOIN app
ON app.id = (log.product_id - 100)
GROUP BY product_id
ORDER BY COUNT(*) DESC

To combine the two results into one table, I use a UNION. I renamed the first column of each table so that combined table will display the column header as name. I want to sort on COUNT(*) but that isn’t available in the UNION so I rename COUNT(*) to count. Now I have a named column in the resulting table and I can sort on it.


SELECT app.name AS name, product_id, COUNT(*) AS count
FROM  log 
RIGHT OUTER JOIN app ON app.id = ( log.product_id -100 ) 
GROUP BY product_id

UNION 

SELECT product.name AS name, product_id, COUNT(*) AS count
FROM  log 
RIGHT OUTER JOIN product ON product.id = log.product_id
GROUP BY product_id
ORDER BY count DESC

One caveat. Because I used RIGHT OUTER JOINs, there may be rows in the log table that were not summarized. In fact there are several from the landing page that are not counted.