首页 > 代码库 > Python 构建方便的函数调用

Python 构建方便的函数调用

CODE:

#!/usr/bin/python 
# -*- coding: utf-8 -*-

'''
Created on 2014-7-14
@author: guaguastd
@name: convenient_function_call.py
'''

if __name__ == '__main__':

    # import json
    import json

    # import search
    from search import twitter_search
        
    # import twitter_trends
    from trend import twitter_trends
    
    # import partial
    from functools import partial
    
    # import login, see http://blog.csdn.net/guaguastd/article/details/31706155 
    from login import twitter_login

    # get the twitter access api
    twitter_api = twitter_login()
    
    # sample usage 1
    pp = partial(json.dumps, indent=1)
    twitter_world_trends = partial(twitter_trends, twitter_api, 1)
    print pp(twitter_world_trends())
    
    # sample usage 2
    authenticated_twitter_search = partial(twitter_search, twitter_api)
    results = authenticated_twitter_search("iPhone")
    print pp(results)

    # sample usage 3
    authenticated_iphone_twitter_search = partial(authenticated_twitter_search, "iPhone")
    results = authenticated_iphone_twitter_search()
    print pp(results)

RESULT:

[
 {
  "created_at": "2014-07-13T22:27:11Z", 
  "trends": [
   {
    "url": "http://twitter.com/search?q=%23TeBancoIgual", 
    "query": "%23TeBancoIgual", 
    "name": "#TeBancoIgual", 
    "promoted_content": null
   }, 
   {
    "url": "http://twitter.com/search?q=%23ArgentinaCampeonBrasil2014", 
    "query": "%23ArgentinaCampeonBrasil2014", 
    "name": "#ArgentinaCampeonBrasil2014", 
    "promoted_content": null
   }, 
   {
    "url": "http://twitter.com/search?q=%23HeroesArgentinos", 
    "query": "%23HeroesArgentinos", 
    "name": "#HeroesArgentinos", 
    "promoted_content": null
   }, 
   {
    "url": "http://twitter.com/search?q=%22Rio+Live%22", 
    "query": "%22Rio+Live%22", 
    "name": "Rio Live", 
    "promoted_content": null
   }, 
   {
    "url": "http://twitter.com/search?q=%23CeQueJeRetiensDeCeMondial", 
    "query": "%23CeQueJeRetiensDeCeMondial", 
    "name": "#CeQueJeRetiensDeCeMondial", 
    "promoted_content": null
   }, 
   {
    "url": "http://twitter.com/search?q=%232014D%C3%BCnyaKupas%C4%B1Finali", 
    "query": "%232014D%C3%BCnyaKupas%C4%B1Finali", 
    "name": "#2014D\u00fcnyaKupas\u0131Finali", 
    "promoted_content": null
   }, 
   {
    "url": "http://twitter.com/search?q=%22Diente+de+Oro%22", 
    "query": "%22Diente+de+Oro%22", 
    "name": "Diente de Oro", 
    "promoted_content": null
   }, 
   {
    "url": "http://twitter.com/search?q=%22Deutschland+ist+Weltmeister%22", 
    "query": "%22Deutschland+ist+Weltmeister%22", 
    "name": "Deutschland ist Weltmeister", 
    "promoted_content": null
   }, 
   {
    "url": "http://twitter.com/search?q=%22Pantas+Germany%22", 
    "query": "%22Pantas+Germany%22", 
    "name": "Pantas Germany", 
    "promoted_content": null
   }, 
   {
    "url": "http://twitter.com/search?q=%22Weltmeister+2014%22", 
    "query": "%22Weltmeister+2014%22", 
    "name": "Weltmeister 2014", 
    "promoted_content": null
   }
  ], 
  "as_of": "2014-07-13T22:35:26Z", 
  "locations": [
   {
    "woeid": 1, 
    "name": "Worldwide"
   }
  ]
 }
]

   "profile_sidebar_fill_color": "DDEEF6", 
   "entities": {
    "description": {
     "urls": []
    }
   }, 
   "followers_count": 5, 
   "profile_sidebar_border_color": "C0DEED", 
   "id_str": "2343493766", 
   "profile_background_color": "C0DEED", 
   "listed_count": 0, 
   "is_translation_enabled": false, 
   "utc_offset": null, 
   "statuses_count": 381, 
   "description": "\u307b\u3063\u307b\u3063\u307b\u30fc(OKB)", 
   "friends_count": 161, 
   "location": "", 
   "profile_link_color": "0084B4", 
   "profile_image_url": "http://pbs.twimg.com/profile_images/440994218417205248/VMkeuw2R_normal.jpeg", 
   "following": false, 
   "geo_enabled": true, 
   "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", 
   "screen_name": "kkyu05kk", 
   "lang": "ja", 
   "profile_background_tile": false, 
   "favourites_count": 1, 
   "name": "\u3042\u3063\u306f\u3063\u306f", 
   "notifications": false, 
   "url": null, 
   "created_at": "Fri Feb 14 12:12:51 +0000 2014", 
   "contributors_enabled": false, 
   "time_zone": null, 
   "protected": false, 
   "default_profile": true, 
   "is_translator": false
  }, 
  "geo": null, 
  "in_reply_to_user_id_str": null, 
  "possibly_sensitive": false, 
  "lang": "ja", 
  "created_at": "Sun Jul 13 22:40:13 +0000 2014", 
  "in_reply_to_status_id_str": null, 
  "place": null, 
  "metadata": {
   "iso_language_code": "ja", 
   "result_type": "recent"
  }
 }, 
 {
  "contributors": null, 
  "truncated": false, 
  "text": "Preferably an iPhone", 
  "in_reply_to_status_id": null, 
  "id": 488452866953056257, 
  "favorite_count": 0, 
  "source": "<a href=http://www.mamicode.com/"http://twitter.com/download/android/" rel=/"nofollow/">Twitter for Android", >
    "url": null, 
    "created_at": "Thu May 01 11:57:00 +0000 2014", 
    "contributors_enabled": false, 
    "time_zone": null, 
    "protected": false, 
    "default_profile": true, 
    "is_translator": false
   }, 
   "geo": null, 
   "in_reply_to_user_id_str": null, 
   "possibly_sensitive": false, 
   "lang": "ja", 
   "created_at": "Sun Jul 13 16:07:44 +0000 2014", 
   "in_reply_to_status_id_str": null, 
   "place": null, 
   "metadata": {
    "iso_language_code": "ja", 
    "result_type": "recent"
   }
  }, 
  "user": {
   "follow_request_sent": false, 
   "profile_use_background_image": true, 
   "default_profile_image": false, 
   "id": 913981824, 
   "profile_background_image_url_https": "https://abs.twimg.com/images/themes/theme1/bg.png", 
   "verified": false, 
   "profile_text_color": "333333", 
   "profile_image_url_https": "https://pbs.twimg.com/profile_images/344513261570994986/69ef1d334e04067d0dc1234e5fdec301_normal.jpeg", 
   "profile_sidebar_fill_color": "DDEEF6", 
   "entities": {
    "description": {
     "urls": []
    }
   }, 
   "followers_count": 295, 
   "profile_sidebar_border_color": "C0DEED", 
   "id_str": "913981824", 
   "profile_background_color": "C0DEED", 
   "listed_count": 1, 
   "is_translation_enabled": false, 
   "utc_offset": null, 
   "statuses_count": 5932, 
   "description": "\u7dd1\u5ca1\u4e2d#4\u2192\u6c34\u6238\u4e00\u9ad8\u5143\u91ce\u7403\u90e8 \uff13\u5e74 #8", 
   "friends_count": 283, 
   "location": "\u307f\u3068\u3057", 
   "profile_link_color": "0084B4", 
   "profile_image_url": "http://pbs.twimg.com/profile_images/344513261570994986/69ef1d334e04067d0dc1234e5fdec301_normal.jpeg", 
   "following": false, 
   "geo_enabled": false, 
   "profile_banner_url": "https://pbs.twimg.com/profile_banners/913981824/1400766610", 
   "profile_background_image_url": "http://abs.twimg.com/images/themes/theme1/bg.png", 
   "screen_name": "t_melonman123", 
   "lang": "ja", 
   "profile_background_tile": false, 
   "favourites_count": 123, 
   "name": "\u6d77\u91ce\u667a\u6d0b", 
   "notifications": false, 
   "url": null, 
   "created_at": "Tue Oct 30 05:58:40 +0000 2012", 
   "contributors_enabled": false, 
   "time_zone": null, 
   "protected": false, 
   "default_profile": true, 
   "is_translator": false
  }, 
  "geo": null, 
  "in_reply_to_user_id_str": null, 
  "possibly_sensitive": false, 
  "lang": "ja", 
  "created_at": "Sun Jul 13 22:44:12 +0000 2014", 
  "in_reply_to_status_id_str": null, 
  "place": null, 
  "metadata": {
   "iso_language_code": "ja", 
   "result_type": "recent"
  }
 }, 
 {
  "contributors": null, 
  "truncated": false, 
  "text": "RT @beqyluxoges: \u3048\u3001\u3069\u3046\u3057\u3088\u3046\u3002\n\u5b09\u3057\u3059\u304e\u3066\u56fa\u307e\u3063\u3066\u308b(\u00b0_\u00b0) \n\niPhone\u21d2http://t.co/PC3KkSGWNC\n\u203b\u53f3\u4e0aSafari\u3067\u958b\u304f\n\nAndroid\u21d2http://t.co/3J37mfRLYe\n\n\u521d\u3081\u3066\u9001\u3063\u305f\u30cf\u30c3\u30d4\u30fc\u30e1\u30fc\u30eb\u266a http\u2026", 
  "in_reply_to_status_id": null, 
  "id": 488453868829696000, 
  "favorite_count": 0, 
  "source": "<a href=http://www.mamicode.com/"http://bit.ly/IDJHUI/" rel=/"nofollow/">/u8a71/u984c/u306e/u30cb/u30e5/u30fc/u30b9/uff13", >