26 March, 2010

Restrict the input in textarea in actionscript3

Without restriction, user can input anything in the textarea.

In my internship program, users need according to the test text to input text in the textarea, after test we give them the typing speed and accuracy results.

But, user can input anything they want, even some html tags or javascript, so we need to restrict their input.

In actionscript3, there is a method of textarea to do this. For example:
var my_ta:mx.controls.TextArea;
my_ta.restrict = "^<>{}@"; 

That means user cannot input"<>{}@" these characters. "^" means characters after it are unacceptable. Without "^" means only characters in the my_ta.restrict are allowed.

20 March, 2010

无知很可怕

绞尽脑汁想了大半个月的设计难题,终于被解决了。
前天本来想了一个自以为很巧妙的设计,当时很是觉得自己聪明,都忍不住高兴的笑了,但到最后又被自己给推翻了。
后来仔细研究发现,原来一直认为python 的return HttpResponse()对ActionScript3是无用的,后者不能接收到任何的response,所以不能在python 中做判断,然后传结果让flash执行相应的function。
当我发现自己的想法是错误的时候,豁然开朗,这个难题不是设计难题,而是知识的缺陷。有时候一个巧妙的设计确实可以让代码简单化,从另一个方面解决难题,但是,如果没有足够的知识和经验的积累,很可能一句简单的代码就可以解决的问题,非要用另一种设计就会更麻烦。

原来Django里的{{ %for% }}有这个模板变量 {{ forloop.counter }},让我在views中到处安插的 
           #i = 1
            #for user in toplist:
            #  user.rank = i
            #  i = i + 1
顿时显得很丑陋。

无知很可怕,还是多学习吧!!


notes from django book--ch4

Dot lookups can be summarized like this: when the template system encounters a dot in a variable name, it tries the following lookups, in this order:
  • Dictionary lookup (e.g., foo["bar"])

  • Attribute lookup (e.g., foo.bar)

  • Method call (e.g., foo.bar())

  • List-index lookup (e.g., foo[2])

The system uses the first lookup type that works. It’s short-circuit logic.
Say, for instance, you have a BankAccount object that has a delete() method. If a template includes something like {{ account.delete }}, where account is a BankAccount object, the object would be deleted when the template is rendered!

To prevent this, set the function attribute alters_data on the method:

def delete(self):
    # Delete the account
delete.alters_data = True


The template system won’t execute any method marked in this way. Continuing the above example, if a template includes {{ account.delete }} and the delete() method has the alters_data=True, then thedelete() method will not be executed when the template is rendered. Instead, it will fail silently
.

16 February, 2010

Difference between fetch() and get() in GAE datastore

fetch() gets the list, get() gets the result data from datastore. fetch() can get many results and put them in a list, get() just gets one result's data.

13 February, 2010

Install SSL Module for Python 2.5.4 on Windows 7


Just upgrade the Google app engine SDK to 1.3.1, when I upload my app, there was a warning shows that I don't have SSL module, so it may not safe when I upload app. The following from Google app engine homepage:

Do tools like appcfg use SSL (HTTPS)?
The SDK tools which use login (appcfg, remote_api, appengine_rpc) all use SSL when communicating your email address and password. In addition, appcfg uses SSL when connecting to the Admin Console by default, unless the --insecure flag is passed.
The Python SDK has the ability to validate the SSL certificate over the remote connection as of version 1.3.1. To do this you must have the ssl Python module installed on your system. If you are using Python 2.5, you can install the module from http://pypi.python.org/pypi/ssl/. If there is an error validating the SSL certificate, the Python tools will raise anInvalidCertificateException explaining what went wrong.

I used easy_install to install simplejson before, so I thought I could install ssl in the same way, just input : easy_install ssl  . But I was wrong,  the cmd just gave me heaps of errors, like this:

Error: extensions must be built with a compiler than can generate compatible binaries. Visual Studio 2003 was not found on this system. If you have Cygwin installed, you can try compiling with MingW32, by passing "-c mingw32" to setup.py.

Related content:
As you can see from the output there's a problem. And the problem is the C extensions could not be built. Now, to me the mention of Visual Studio 2003 seems to be a benign error. I'm not savvy enough or rich enough to buy or know how to setup VS2k3's C++ compiler to work with easy_install. The easiest/cheapest way I've found is to go the MingW32 route. So the next step is to install MingW32.
To install MingW32, I download the "Automated MingW installer" from here on sourceforge, but if it no longer exists, go to http://mingw.org. And look around until you find it.
Once downloaded and run, you are presented with several screens that you can just click through until you get to "Choose Components".

By default minimal is selected, which installs no compilers whatsoever. What we want is the "g++ compiler" and "MingW Make", or if you are fealing really sassy just select "full" and forget about it. After clicking next you are presented with an install directory option. The default is fine, but if you change it, keep in mind you will need it for the next step because we need to add the location of all the compilers to the path. After clicking finish, the installer pulls down everything it needs to and sets things up as you requested.
When the install completes, you will have a c++ compiler that easy_install can be told to use. However if you aren't sensing a common theme yet, try typing "g++" at the command prompt. Obviously this is not good, but you know by now that you can fix this by adding it to the path. The path for the compliers is by default "C:\MinGW\bin", so go ahead and add it to the path. You'll know you're good to go when you can type "g++" and something actually happens.
Ok there's only one last thing we need to do before we can get pycrypto to install via easy_install. And that is by telling easy_install to use MingW instead of punting to a non-existent Visual Studio 2003 install.
This is done by creating a file called "distutils.cfg" at C:\Python25\Lib\distutils that contains following.
[build]
compiler=mingw32
If you did that correctly, and you now run "easy_install pycrypto" you should get a successful install of pycrypto. To check you can launch python and type...
C:\Documents and Settings\twillis>python
Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import Crypto
>>> 

If you get no error message, PyCrypto is installed!!!!


Unfortunately, I got error message again!!  Keep googling, another page has the solution:

Useful content:
Install MinGW
§  Go and get binary installer at http://www.mingw.org/
The dependencies
To build the SSL, you need additional library and include file. I have tried to compile and fail and look at the error and fix the problem like trial and error, no magic here!
Below are libraries you will need,
§  OpenSSL
Once you get all of them, unzip it to the directory you like and please don't forget it because we will need it later on.
For me, I unzip and place it at C:GnuWin32
Get the sorce code
Go to download the source code at http://pypi.python.org/pypi/ssl/ and extract to somewhere you want.
Modify setup.py
The original setup.py is assume that you place library at C:UtilsGnuWin32
    if sys.platform == 'win32':

        # Assume the openssl libraries from GnuWin32 are installed in the
        # following location:
        gnuwin32_dir = os.environ.get("GNUWIN32_DIR", r"C:\GnuWin32")
Go to line 152 and change you path you have extracted the files in.
Let build it
Go to directory you have extracted the source code,
§  To build the only, run command python setup.py build
§  To build and install, run command python setup.py install
§  To build the installer, run command python setup.py bdist_wininst

I didn’t change the source code to another location, just unzip OpenSSL libgw32c  to C:\Utils\GnuWin32 according to the source code, and run easy_install again, but another error message said that easy_install not allowed to change file in another place or something(can’t remember clearly). Then python setup.py install, solve the problem finally.

07 February, 2010

Read More, You will get More

First after I read a chapter of the book Flash ActionScript 3.  I have run the  examples and I thought I totally understand it, but today when I read it again,  I found there are more useful stuff I totally was not aware.

So, if you have a good book, not just read it once and throw it. Read it more, especially for technical books. (I think this just for the beginning learners.)

03 February, 2010

Embed Flash into your website

If you use AC_FL_RunContent to embed your flash to your website, you can ignore the 
     <object> 
     <embed>
      </embed>
     </object>
these elements. 
But make sure you have  AC_OETags.js (or other name) imported in your HTML to support the AC_FL_RunContent function.