Archive for December 2008
Yesterday I decided to play with Gmail’s API in order to grab my contacts. To my surprise, it was a snap. Google has instructions for python Here, I wrote my example using Python 2.5 on OS X. Once I had unzipped the GData contents, I just changed to the GData directory and ran
“sudo python setup.py install”
and I was ready to go.
Python Documentation for GDATA
#################################
# javazquez.com
# Google GMAIL API example
##################################
import gdata.contacts.service
user = 'user@gmail.com'
pwd = 'password'
client2 = gdata.contacts.service.ContactsService()
# Authenticate using your Google Docs email address
# and password.
client2.ClientLogin(user, pwd)
contacts_feed = client2.GetContactsFeed()
################################################
# NOTE: The GetContactsFeed does not give back
# all contacts.
# This 'problem' can be solved by looping
# until the contacts_feed.GetNextLink
# returns None.
################################################
l=[]
while(contacts_feed) :
for x in contacts_feed.entry:
l.append([x.title.text, x.email[0].address])
ret = contacts_feed.GetNextLink()
contacts_feed = client2.GetContactsFeed(ret.href) if(ret) else ret
print "here are your %d contacts" % len(l)
for contact in l:
print "%s -> %s" % (contact[0],contact[1])
pre>
Output
here are your xxxx contacts
ANDREW -> adrew@somewhere.com
...
code · google api · python
If your like me, you bounce around between languages a lot. Lately, I have been writing python code. It’s not Ruby
, but it can get the job done. Here is a quick list of similarities between the two languages. I hope it helps… don’t forget to this list in the comments section ![]()
#-----find object methods-----
s="hello, I am a string"
#ruby
puts s.methods
#python
print dir(s)
#find out more about a method using python
help(s.split)
#-----view object's class-----
#ruby
s.class
#python
s.__class__
#------Iterate hashes-------
#ruby
h.each{|key,value| puts "#{key}, #{value}"}
#python
for key,value in h.iteritems():
print key, value
#---ternary operators
#ruby
condition ? var = x : var = y
#python.. not exactly an operator, but you get the meaning
#---- var = y if condition is false
var = x if condition else y
#----lengths------
#ruby
s="hello, I am a string"
puts "Length of string is #{s.length} or #{s.size}"
h={:one=>2,:three=>4}
puts "Length of hash is same as string, #{h.length} or #{h.size} "
#python
print("This is the length of a string %s" % len("string"))
print("number of key/value pair= %d" % len({'one':1,'two':2}))
#---slicing lists/arrays
l=[1,2,3,4,5]
#ruby
l[1..3] #=>[2,3]
#python
l[1:3] #=>[2,3]
#--print string multiple times-----
#ruby
4.times{print "hello"} #=> hellohellohellohello
#python
print("hello" * 4) #=> hellohellohellohello
code · fun · Programming · python · Ruby · Ruby Questions
So for my own geeky pleasure, I decided to try writing cgi scripts with Ruby, Python, PHP, and Perl. All had readily accessible documentation on how to POST to a https URL but ruby. My first thought was to look at the Net:HTTP documentation found HERE.
The one example I wanted was not listed. I did some searching around and pieced together the following code. I hope this is as big a help to you as it was to me. Looking at it, It seems pretty intuitive….but if your like me, sometimes you need it spelled out
As a side note, setting path to path = ‘/../’ is my work-around for a script that is mapped to www.mysite.com rather than ‘/some_POST_handling_script.rb’
#Juan Vazquez ->javazquez.com
require 'net/http'
require 'net/https'
http = Net::HTTP.new('www.mysite.com', 443)
http.use_ssl = true
#path(a.k.a) ->www.mysite.com/some_POST_handling_script.rb'
path = '/some_POST_handling_script.rb'
data = 'badguy=Gargamel'
headers = {'Content-Type'=> 'application/x-www-form-urlencoded'}
resp, data = http.post(path, data, headers)
puts 'Code = ' + resp.code
puts 'Message = ' + resp.message
resp.each {|key, val| puts key + ' = ' + val}
puts data
