Tuesday, July 5, 2011

Why do we sleep

People think we sleep to take rest. But we can rest without sleeping. Not all of us rest while we sleep. Not surely  the heart or respiratory muscles. And if your brain rested, we won't have any dreams.

The purpose of sleep always intrigued me. And I was not alone. At least a few other people were there too. I found these two sites (big think and physorg.com) where the neuroscientists' opinion about the matter is discussed. Basically they think the reason for sleep is either :
  • information processing theory - to sort out the informations that have come in during the day (brain defragmentation?)
  • damage reversal theory - to remove free radicals and other noxious substances generated by 'heated thinking' during the day
  • adaptive inactivity theory - by UCLA neuroscientist Jerome Siegel, which says that sleep optimizes the timing of our activity.
I prescribe to the last POV. Sleep is not a physiologically necessary process. Our body can rest, take care of free radicals and organize memory without sleeping, if it needed to.

Evolutionarily it takes a lot to adapt to a particular environment. So all organisms, are adapted to one or the other kind of environment (called ecological niche). Then, at the same place, the environment changes vastly during the 24 hours a day. Animals behavior is expected to vary greatly during the day and night. For one animal, it may be difficult to be this versatile, so, nature makes it adapted to one of the time period in a day, and sleep during the other.
Pretty neat?

Sunday, July 3, 2011

android scripting using python

Android has a beautiful scripting platform 'sl4a'. You can script using lot of languages perl, python, shell etc in it. It has a good API for system calls like sending sms, making calls etc.

I've written a small script in python to send a group sms with individualised content.



import android, os, re

droid = android.Android()
docdir = "/mnt/sdcard/document"




Android calls are in android module. other modules like sys, os, re are available.



message =droid.dialogGetInput('message', ' extras: 0 name,1 mail', None).result
subject =droid.dialogGetInput('subject', '', None).result


message windows can be created using API calls.



dirfile = os.listdir (docdir)
dirfiles =[]
for f in dirfile :
 if f[-4:]==".sms":
  dirfiles.append (f)
d=droid.dialogCreateAlert("select file")
droid.dialogSetSingleChoiceItems(dirfiles)
droid.dialogSetPositiveButtonText('ok')
droid.dialogSetNegativeButtonText('cancel')
droid.dialogShow()
r=droid.dialogGetResponse().result
if r['which']== "negative":
 os._exit (1)
result = droid.dialogGetSelectedItems().result[0]
selfile = dirfiles [result]

All files in a directory ending with .sms is presented and the user is asked to select one of the files.




file = open (docdir + os.sep+ selfile, "r")
lines = file.readlines ()
contacts = []
numbers =[]
extras =[]
mail =""
for line in lines:
 f = line.split (",")
 if len (f[0])>0 :
  numbers.append (f[0])
  contacts.append (f[1])
  extras.append (f[1:])
 if len (f[1])>0 :
  mail = mail + f[2].replace(" ","").replace ("\n","") + ","

The .sms file is a comma seperated file whose first field is the contact number, second contact name, third his email, and any optional field which can be referred as $n in the message.



d=droid.dialogCreateAlert("select contacts")
n = len (contacts)
droid.dialogSetMultiChoiceItems(contacts, range (n))
droid.dialogSetPositiveButtonText('ok')
droid.dialogSetNegativeButtonText('cancel')
droid.dialogShow()
r=droid.dialogGetResponse().result
if r['which']== "negative":
 os._exit (1)
send_contacts= droid.dialogGetSelectedItems().result

for s in send_contacts :
 m= message
 for d in range(len(extras [s])):
  m = m.replace ("$"+str(d), extras[s][d])
 m=re.sub('\$\d','',m)
 droid.smsSend(numbers [s], m)
 print contacts[s] , m
droid.sendEmail(mail, subject, message, None)

The user is presented  a list of contacts to select from, to whom to send the message. The script also sends an email with the same message (without personalisation)