Category Archives: python

The Hacker’s Healthy Guide, a simple 3 steps algorithm

I love to eat! In fact I eat a lot! And I have the great fortune to be the partner of one marvelous chef
that cooks amazing meal almost every day!

But I’m a hacker, I like coding even more then I like to eat. So I spent most of my time sitting in front of a computer without any apparent movement, eating stuff and drinking a lot.

So what I eat everyday is important to me, but I’m not a grano-hippie carrying my bag of nuts and dried fruits. And I believe any diet or restriction on the aliments you can eat are the biggest bullshit ever. What I do is a quick check of the nutrition facts label of each product I buy at the grocery.

Fat

fat fat fatI don’t really check the fat quantity. Saturated fat should be low. But the important thing is Trans fat should be 0. Almost no product with trans fat ever enter my house. This type of fat is transformed in a way your body can not recognize it as fat, so it won’t end up in the right place and it’s not the type of fat you can loose by exercising. It’s there forever, like Microsoft hidden libraries when you install Office!

Sodium

salty!This one is important because it’s high on several products that are low fat or low calories. If it’s low in fat but has more then half your daily value in sodium (often the case for dried noodle soup) it’s a no-no, it would be like eating salt!

Protein

juiceEver wondered why drinking a glass full of fruit juice will keep you hungry and only one apple can sustain you for a couple of hours ? That’s because the juice doesn’t hold any proteins. Proteins expand in your stomach and make you feel satisfied. If a meal hold lots of proteins you won’t need to eat as much to get going!

Code samples

If you find this boring just read the code bellow, it’s a simplified version of the algorithm in the language of your choice:

Ruby:


  def eat?(product)
    product.trans_fat == 0 &&
      product.sodium < 1000 &&
        !product.proteins.empty?
  end

Python:


  def eatable(product):
    return product.transFat == 0 and product.sodium < 1000 \
                                 and len(product.proteins) > 0

C#:


  public bool IsEatable(Product product) {
    return product.TransFat == 0 && product.Sodium < 1000
                                 && product.Proteins.Length > 0;
  }

Lisp:


  (defun eat? (product)
    (and (= (trans-fat product) 0)
         (< (sodium product) 1000)
         (/= (array-total-size (protein product)) 0)))

Closing tips

asian pearEven if you have to eat frozen food for lunch there are some good choices out there, just check out those 3 things. Plus, fruits and vegetables are always a winner. But cutting, wrapping the thing can be pretty annoying. To bring back excitement, joy and extaze to your meal think about trying new fruits like: asian pear, dragon fruit, kaki, kiwi, mango… OK that’s enough I’m starting to dribble.

Now what are your tips on being an healthier hacker ? I’d really like to know! Or maybe you’d like to contribute to the translation of the (really complex!) algorithm (or correct my poor lisp coding skills). Please do, I’ll add it with due credit!

Bon appétit!

Warning if you’re gonna submit some code in the comments:
My friend wordpress.com seems to eat some part of the content when we use < or >. This is stupid, but if you’re going to submit some code in a comment, replace the < by &lt; and > by &gt; or just send me your code by email at macournoyer_AT_gmail.com. Sorry for the inconvenient.

12 Comments

Filed under C#, Misc, python, ruby, tutorial

Finally some good moves from MS

Some Microsoft insiders recently announced the upcoming release of a Dynamic Language Runtime (DLR) built on top of CLR.

I’m a little confused by the Silverlight project web site, on the front page you can read:

Silverlight offers a flexible programming model that supports AJAX, VB, C#, Python, and Ruby, and integrates with existing Web applications.

But browsing the quickstart section I bumped into some XAML… oho! I hope this is not what I’m thinking about, Webforms, XML, AJAX-Atlas-style.

Anyways, DLR is supposed to be released as an open-source project, plus being built on top of the CLR, it should run on Mono. That’s a great plus for all the dynamic languages to have a common base, so all annoying problems (performance, GC, Unicode) are solved once and we can focus on the real thing: the syntax.

Meanwhile, I’ll be checking the latest source of IronPython which is supposed to be built on top of this new thing.

4 Comments

Filed under C#, python, ruby

Python take 3

Unless you’ve been under a rock for some time, you’ve heard of the ruby-python war. I wasn’t sure in what side I was. Ruby is elegant, compact, flexible and full OO. I like python indentation rules and flexibility too, but dislike it’s lack of consistency in the core libs (part object, part procedural).

But now that I’ve watched this video of M. Python in which he admit he made some mistakes and conclude that a big incompatible new revision is necessary. New features will include, among other things, more verbosity. Read: no more statement! print 'something' will become print('something'). Why ? Because (the example he uses) if you want to redirect all your console output to a file or logger it will be easier like this then

replacing all the print statements in your code by hand

What ? Am I dreaming or he’s driving the language syntax from the technical limitations !?!

In ruby parenthesizes are optional so print 'something' is the same as print('something'). And if you want to redirect all the prints to somewhere else you simply override the Kernel print method.

That’s enough for me to move definitely to the ruby camp.

Leave a comment

Filed under python, ruby

Code generator : the languages comparison

I’ve been hacking a code generator in various languages just to get the fell of each languages, none is the worst, but some are so much cooler. Here’s the results of my experimentation.

C#

Goods

  • Fast
  • Should run on any win32 machine with .NET

Bads

  • Too much of LOC
  • Had to use NVelocity has a template engine
  • Code to inject template bindings was ugly

Python

Goods

  • Easy to read class and attributes metadata
  • Scripting language so the developement cycle was faster, no compile step
  • Easely multiplateforme

Bads

  • Had to use Cheetah has the template engine

Boo

Goods

  • Fast
  • Implemented the generator code has a script in Boo

Bads

  • Playing with the compiler pipeline and stuff is complexe
  • lack of docs (but yeah Boo has not hit 1.0 yet)
  • No template engine usable for this with Boo’s syntax

Ruby

Goods

  • OMG!
  • Implemented in 2 LOC
    require 'erb'
    print ERB::new('mytemplate').run(binding)
    

Bads

  • Too easy 😉

Leave a comment

Filed under C#, Generator, python, ruby