RSS

Distinguishing between False and 0 (zero) in a Django template

You may occasionally find yourself in a Django template attempting to distinguish if False is not equal to 0 (bool vs. int).

There are multiple ways to accomplish this, but if you don’t want to create a new template tag, and you don’t want to add logic to the view (because you’re a rebel), you can accomplish it using the “add” filter.

Take the variable “my_var”

If my_var is a ‘bool’ False:

my_var|add:"f" == "f"

 

If my_var is an ‘int’ 0:

my_var|add:"f" == ""

 

And all together now:

{% if my_var|add:"f" == "f" %}
    I have a boolean False
{% else %}
    I have a 0
{% endif %}

 

Keep in mind that this logic may vary depending what you’re comparing.  It’s useful as is ONLY if you know you’re trying to distinguish between False and zero (0).  If you can have values for my_var like “” or [] or other data types that have certain scenarios for equating with False, you’ll have to play around, but I personally had difficulty finding answers that address this that didn’t involve building logic outside of the template file itself.

 

Posted by on June 27, 2014 in Computers, Programming, Uncategorized

Leave a comment

Tags: , , , , , ,

Reading Compound File Binary Format Files generated by PHPExcel with Python/Django xlrd

This was a monster for me to track down, so I’m hoping it helps many others.

The issue I ran into was that when trying to read an Excel file using Python’s xlrd package that was generated by PHPExcel, the following error was generated:

File read error [row 1]: Workbook corruption: seen[2] == 4

In search of a fix, preferably one that didn’t assume PHPExcel was just buggy (which it may be, but that’s another topic), I perused dozens of online articles with the actual developer of xlrd (John Machin) commenting on threads from people with nearly identical issues, but all the solutions were just specific workarounds that didn’t seem to apply to me.

Somewhere, I caught the tiniest glimpse of the phrase “Compound File Binary.” At first it didn’t lead to much, but it ultimately led me here:

OleFileIO_PL – a Python module to read MS OLE2 files

A quick “pip install OleFileIO-PL” and the deployment of this code:


data = file.read()
try:
f = StringIO.StringIO(data)
ole = OleFileIO_PL.OleFileIO(f)
    if ole.exists('Workbook'):
         d = ole.openstream('Workbook')
         data = d.read()

except Exception as e:
     # try to read it the normal way
     pass

And I was on my way.

Note that “Workbook” stream might be specific to PHPExcel, but there is a list_dir() method on the ole object that will show you the streams available.  There’s also decent documentation in the zip download from the above link.

 

Posted by on September 19, 2013 in Computers, How-To, Programming

Leave a comment

crimulus’ weekly Twitter updates — 2012-10-07

Powered by Twitter Tools

 

Posted by on October 7, 2012 in Twitter Updates

Leave a comment

Tags: , ,

crimulus’ weekly Twitter updates — 2012-09-30

Powered by Twitter Tools

 

Posted by on September 30, 2012 in Twitter Updates

Leave a comment

Tags: , ,

crimulus’ weekly Twitter updates — 2012-09-09

  • You can write the music, build the chairs, and gather the group and still be the one without a seat when the music stops. #
  • Rest assured as soon as the bathroom is built, we'll see a self pic of Curiosity in its underwear too! http://t.co/h0bzfjON #

Powered by Twitter Tools

 

Posted by on September 9, 2012 in Twitter Updates

Leave a comment

Tags: , ,

crimulus’ weekly Twitter updates — 2012-08-12

  • Is ransom money tax deductible? #

Powered by Twitter Tools

 

Posted by on August 12, 2012 in Twitter Updates

Leave a comment

Tags: , ,

crimulus’ weekly Twitter updates — 2012-05-20

  • Regret is akin to failure, and yet it is poignantly optional; it’s the abandoning of the future to commiserate with the immediate. #
  • So the broken clock walks into the second hand store … #
  • RT @Funfacts: 10% of all the photos ever taken were taken last year.. #

Powered by Twitter Tools

 

Posted by on May 20, 2012 in Twitter Updates

Leave a comment

Tags: , ,

crimulus’ weekly Twitter updates — 2012-05-06

  • @ProfBrianCox At least you'll know if it's raining in France. #
  • @ewg118 Careful … that sounds like a battle cry for the dumb idea mongers … #

Powered by Twitter Tools

 

Posted by on May 6, 2012 in Twitter Updates

Leave a comment

Tags: , ,

crimulus’ weekly Twitter updates — 2012-04-29

  • RT @slashdot: First Full Observable-Universe Simulation http://t.co/VNa6vY0J . #
  • My dearest cold weather, F*ck Off. Love, Me #
  • In one word or less, post your favorite two words. #
  • @slashdot Totally sounds like an Onion headline … #
  • RT @Geekosystem: Research Firm Says 1 in 5 Macs Contain Windows Malware, 1 in 36 Macs Contain Mac Malware http://t.co/KgDbtrGf . #
  • @ewg118 A likely story … #
  • MT @SeanGrandePBP: The Streak continues. Rondo 4th player in NBA history with 10+ assists in 24 straight games. First since Stockton in '92. #

Powered by Twitter Tools

 

Posted by on April 29, 2012 in Twitter Updates

Leave a comment

Tags: , ,

crimulus’ weekly Twitter updates — 2012-04-22

Powered by Twitter Tools

 

Posted by on April 22, 2012 in Twitter Updates

Leave a comment

Tags: , ,