Posts

Google App Engine SDK for Python - Using datastore query at local for testing

When I tried to do some test of datastore query in the python console: query = MyModel.all() query.filter("username =", "myusername") result = query.get( ) I got this error" BadArgumentError: app must not be empty. After googling around, I found this solution: before execute the query, run these following commands: import os os.environ['APPLICATION_ID'] = 'dangtrinhnt'                                datastore_file = '/dev/null' from google.appengine.api import apiproxy_stub_map,datastore_file_stub apiproxy_stub_map.apiproxy = apiproxy_stub_map.APIProxyStubMap() stub = datastore_file_stub.DatastoreFileStub('dangtrinhnt', datastore_file, '/') apiproxy_stub_map.apiproxy.RegisterStub('datastore_v3', stub) Then, execute any query I want, for example the query at the beginning of this blog post (the blue lines). Refere...

Google App Engine - Running GAE inside a virtualenv

To be able to work with Google App Engine inside a virtualenv environment, we have to do some tricks: 1. Download and extract the Google App Engine SDK for Python: + Link: https://developers.google.com/appengine/downloads#Google_App_Engine_SDK_for_Python + Extract the zip file to /opt directory. So, the directory path for GAE is something like: /opt/gae 2. Open the activate script of the virtualenv environment at /home/.venv/myenv/bin/activate : + Find the line: PATH="$VIRTUAL_ENV/bin:$PATH" export PATH + Insert the GAE path: PATH="$VIRTUAL_ENV/bin:$PATH" PATH="/opt/gae:$PATH" export PATH Save & exit 3. Create a path configuration file for GAE inside the virtualenv environment name /home/.venv/myenv/lib/python2.7/site-packages/gae.pth with the following content: /opt/gae import dev_appserver; dev_appserver.fix_sys_path() Now you can run your GAE application inside a virtualenv environment. If you get trouble with GAE d...

Python - Sorting a list of objects

Assuming I have a list of JSON objects: obj1 = {'title': 'Super genius', 'modifiedDate': 1334014208.0, 'user_id': 123} obj2 = {'title': 'Awesome', 'modifiedDate': 1333962645.0, 'user_id': 345} obj3 = {'title': 'Gorgeous', 'modifiedDate': 1333894106.0, 'user_id': 531} obj4 = {'title': 'Cool', 'modifiedDate': 1333968061.0, 'user_id': 214} obj5 = {'title': 'Badass', 'modifiedDate': 1334016506.0, 'user_id': 678} unorder_list = [obj4, obj5, obj1, obj2, obj3] And I want to create a list contains those objects in ascending order of modifiedDate . 1. Solution #1 : using lambda to sort by 'modifiedDate' def order_list ():     order_list = unorder_list     order_list.sort(key = lambda x: x['modifiedDate'])     return order_list 2. Solution #2 : do not use lambda def order_list ():     or...

Python - ROT13 encode

"ROT13 ("rotate by 13 places", sometimes hyphenated ROT-13) is a simple letter substitution cipher that replaces a letter with the letter 13 letters after it in the alphabet. ROT13 is an example of the Caesar cipher, developed in ancient Rome." ~Wikipedia. Here are 2 solutions to implement ROT13 in Python: 0. 1st solution: import string ROT13 = string.maketrans(\          "ABCDEFGHIJKLMabcdefghijklmNOPQRSTUVWXYZnopqrstuvwxyz",\          "NOPQRSTUVWXYZnopqrstuvwxyzABCDEFGHIJKLMabcdefghijklm") def rot13_encode (user_text):     return string.translate(user_text, ROT13) This method works smoothly in the console but when using with the web it will raise error because of some special characters in the input: File "/usr/lib/python27/string.py" , line 498 , in translate return s . translate ( table + s [: 0 ]) UnicodeDecodeError : 'ascii' codec can 't decode byte 0x80...

Python - Check if a string is convertible to an integer

To check if a string is convertible to an integer, use this following built-in function: mystring.isdigit() => True: mystring can be converted to an integer. => False: mystring cannot be converted to an integer. Reference: http://stackoverflow.com/questions/5606585/python-test-if-value-can-be-converted-to-an-int-in-a-list-comprehension

Python - Change case with built-in string functions

Case conversion in Python is quite easy with those built-in string functions: swapcase() Return a copy of the string with uppercase characters converted to lowercase and vice versa. upper() Return a copy of the string converted to uppercase. title() Return a titlecased version of the string: words start with uppercase characters, all remaining cased characters are lowercase. lower() Return a copy of the string converted to lowercase. capitalize() Return a copy of the string with only its first character capitalized. References: http://docs.python.org/2/library/stdtypes.html#string-methods

Google App Engine - How to download source code

To download source code from a Google App Engine application, run the following command: appcfg.py download_app -A <your_app_id> -V <your_app_version> <output_dir> Reference : https://developers.google.com/appengine/docs/python/tools/uploadinganapp?csw=1#Python_Downloading_source_code