Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Saturday, December 20, 2008

Python Encoding

Python uses UTF-8 for encoding python files. But for some reason Google App Engine does not.

You MUST include this right in the very first line of your .py file:

#
# -*- coding: utf-8 -*-


Remember the it MUST be in the very first line of the text so the encoding comment: # -*- coding: utf-8 -*-
Will be positioned exactly in the 2nd line with is mandatory according to Python specification.

Hope it helps.

Sunday, December 14, 2008

Python Time Comparison Tricks

Cool operator overflow feature in Python brings a lot of abstraction.
When comparing Date and Time this brings an easy way to do it.


# Imports
import os
import datetime
from datetime import timedelta

# Get current time value:
current=datetime.datetime.now()

# Advancing Time (examples hours, minutes, days, seconds):
# Advancing 1 hour and 30 seconds
adv=current + timedelta(hours=1, seconds=30)

# Getting Delta Time
diff = adv - current
# Comparing
if diff > timedelta(hours=1):
bigger=true
else:
bigger=False


Very easy!

Tuesday, December 9, 2008

UnMount Python

After years of personal resistance finally I did it.
I took one of my nights to learn Python.
The main reason was:
  • Check how Google App Engine is being able to support so much Transactions.
Other reasons:
  • Check if it really throws Identation Error?
  • Will I take 50% of my time doing Identation?
  • Does it really count my Whitespaces?
  • WHY!?!?
The conclusion was that Python it is very easy and full of well implemented frameworks with a quite good performance.
What really called my attention was the high-level data structures with a simple but effective approach to object-oriented programming.
It totally worth it. Specially for replacing the my old fashioned Perl Scripts.

* About the WEIRD whitespace stuff: I would say it sucks. But maybe it is my brain that is not yet prepared to process code snippets without my old friends "{}"

Hello World Code Example:
print "Be careful not to fall off!"