Sunday, May 29, 2011

Using Django functionalities in a stand-alone application

1. Set django settings.py file

2. Run syncdb to create DB tables
python manage.py syncdb

3. Create a shell script to set environment variables (NOTE : setting required variables to run django somehow cannot be done by python.)



export PYTHONPATH=$PYTHONPATH:/home/djangoprojects
export DJANGO_SETTINGS_MODULE="legacytest.settings"

python ./access_db.py



4. Create a python file with django functions.

from newapp.models import *

# Pull all data from old DB
aa = Aa.objects.all()

Friday, April 15, 2011

Creating an XML document in Python



root = xml.Element('bestsellers')
root.attrib['yyyymm'] = "201012"
root.attrib['topN'] = str(1000)
idx = 0
for i in top1000_list:
idx += 1
child = xml.Element('item')
root.append(child)
child.attrib['rank'] = str(idx)
for k,v in i.items():
child.attrib['isbn'] = k
child.attrib['count'] = v
print xml.tostring(root)



Result is...


































String to JSON in Python



jsonstr = "[ {\"20101231\" : [ {\"ID1234\":\"ISBN1234\"} , {\"ID1234\":\"ISBN1234\"} ] } ]"
j = json.JSONDecoder().decode(jsonstr)

Sunday, January 16, 2011

tweepy (twitter API) Status object structure



{
'contributors': None,
'truncated': False,
'text': 'My Top Followers in 2010: @tkang1 @serin23 @uhrunland @aliassculptor @kor0307 @yunki62. Find yours @ http://mytopfollowersin2010.com',
'in_reply_to_status_id': None,
'id': 21041793667694593,
'_api': ,
'author': ,
'retweeted': False,
'coordinates': None,
'source': 'My Top Followers in 2010',
'in_reply_to_screen_name': None,
'id_str': '21041793667694593',
'retweet_count': 0,
'in_reply_to_user_id': None,
'favorited': False,
'retweeted_status': ,
'source_url': 'http://mytopfollowersin2010.com',
'user': ,
'geo': None,
'in_reply_to_user_id_str': None,
'created_at': datetime.datetime(2011, 1, 1, 3, 15, 29),
'in_reply_to_status_id_str': None,
'place': None
}


tweepy (twitter API) User object structure

1. tweepy User object has following fields.



{
'follow_request_sent': False,
'profile_use_background_image': True,
'id': 132728535,
'_api': ,
'verified': False,
'profile_sidebar_fill_color': 'C0DFEC',
'profile_text_color': '333333',
'followers_count': 80,
'protected': False,
'location': 'Seoul Korea',
'profile_background_color': '022330',
'id_str': '132728535',
'utc_offset': 32400,
'statuses_count': 742,
'description': "Cars, Musics, Games, Electronics, toys, food, etc... I'm just a typical boy!",
'friends_count': 133,
'profile_link_color': '0084B4',
'profile_image_url': 'http://a1.twimg.com/profile_images/1213351752/_2_2__normal.jpg',
'notifications': False,
'show_all_inline_media': False,
'geo_enabled': True,
'profile_background_image_url': 'http://a2.twimg.com/a/1294785484/images/themes/theme15/bg.png',
'screen_name': 'jaeeeee',
'lang': 'en',
'following': True,
'profile_background_tile': False,
'favourites_count': 2,
'name': 'Jae Jung Chung',
'url': 'http://www.carbonize.co.kr',
'created_at': datetime.datetime(2010, 4, 14, 1, 20, 45),
'contributors_enabled': False,
'time_zone': 'Seoul',
'profile_sidebar_border_color': 'a8c7f7',
'is_translator': False,
'listed_count': 2
}