Tuesday, July 14, 2009

Make widgets Enable and/or Disable in pygtk

Many a times while you are working pygtk, You would like to make some widgets like buttons, checkboxes etc etc.. to be disabled for a action to complete, And again would like to enable it, after the action is complete. This simple function is all you would need to accomplish this.


def make_sensitive(state, widget_list):
   """
   Enables/disables widgets
   state: either True or False, True to Enable, False to Disable
   widget_list: A list of widgets that you want to enable or disable collectively.
   """
   s = state and 1 or 0
   for w in widget_list:
      w.set_sensitive(s)

Thursday, March 26, 2009

Convert time zone from US to India

This shows the steps to convert time zones from EST to IST

>>> from datetime import datetime
>>> from pytz import timezone
>>> us_tz = timezone("US/Eastern")
>>> in_tz = timezone("Asia/Calcutta")
>>> now = datetime.now()
>>> dt_in = in_tz.localize(now)
>>> dt_us = dt_in.astimezone(us_tz)
>>> dt_in
datetime.datetime(2009, 3, 26, 12, 58, 17, 909000, tzinfo=)
>>> dt_us
datetime.datetime(2009, 3, 26, 3, 28, 17, 909000, tzinfo=)
>>> dt_in.year
2009
>>> dt_in.month
3
>>> datetime.weekday(dt_in)
3
>>> dt_in.hour
12
>>> dt_in.minute
58
>>> dt_in.second
17
>>>

Friday, October 3, 2008

What is a Slug?

SlugField
"Slug" is a newspaper term. A slug is a short label for something, containing only letters, numbers, underscores or hyphens. They're generally used in URLs.

Implies maxlength=50 and db_index=True.

Accepts an extra option, prepopulate_from, which is a list of fields from which to auto-populate the slug, via JavaScript, in the object's admin form:

meta.SlugField(prepopulate_from=("pre_name", "name"))
prepopulate_from doesn't accept DateTimeFields.

The admin represents SlugField as an (a single-line input).

Tuesday, September 30, 2008

The date filter in template

Template use:
{{ article.pub_date|date:"F j, Y" }}

http://docs.djangoproject.com/en/dev/intro/overview/?from=olddocs#design-your-templates

PHP reference for formating can be fount in
http://php.about.com/od/learnphp/ss/php_functions_3.htm

getattr()

Python’s getattr function is used to fetch an attribute from an object, using a string object instead of an identifier to identify the attribute. In other words, the following two statements are equivalent:

value = obj.attribute
value = getattr(obj, "attribute")


The function takes an optional default value, which is used if the attribute doesn’t exist. The following example only calls the method if it exists:

func = getattr(obj, "method", None)
if func:
func(args)

http://effbot.org/zone/python-getattr.htm

format strings for datetime strftime

The Information on formating of date is available here.