22 November, 2010

Use Regular Expression In Project


In python there is a module named re, stands for Regular Expression. I tried to use it in the project. Because the content of each lesson in code is about 10 to 40 lines, but the whole source file is a about 700 lines. So I need to extract the content to make it as normal article, easy to read, easy to translate too.

First, write the regular expression to match the content what I want:
p = re.compile(r'(?<= text=").+(?=" x=")')

then read & match them sentence by sentence:
for line in fin.readlines():
   s_match = re.findall(p,line)

And write them to a new file:
   if s_match:
      fout.write(s_match[0]+ " ")

The new file (fout) has the whole content of this lesson which I need to translate. No matter the content is English or Chinese, it can show properly.

re.findall gets a list of strings, so if you want to use the string, just use the index of the list. If there is more than one string, and you want to read them one by one, you can use for … in s_match to get all of them.
Remember: for other languages (not English), if you read the list, you may find the strings in it were encoded in other format (not ASCII). If you want to get the string, do not use str(s_match), otherwise, you will get the encoding of the characters, such as: \xc4\x87\x56\x82....... 

Read the real string elements in the list, not just convert the list to string.

19 August, 2010

projecteuler.net - Q5

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

i = 1
for k in range(1,21):
    if i%k > 0:
        for j in range(1,21):
            if (i*j) % k == 0:
                i *= j
                break
print i

02 July, 2010

算法之美:求一个数n的最大质因数(Python)

n=600851475143
d=2
while d < n/d:
    if n%d==0:
        n/=d
    else:
        d+=1
print n

28 April, 2010

Counting occurrences of a pattern

Use the :s substitute command with the n flag (counts number of hits; no changes occur) to count the number of occurrences of a pattern. For example:
:%s/pattern//gn

06 April, 2010

工作开始

能力越大,责任越大。
不用害怕,挑战自己。

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.

27 January, 2010

Redesign the whole system

It's not allowed to use cookie in the new test system, although I've realized many functions, I have to redesign them.
Try to use Django to pass the values between different web pages.

23 January, 2010

Eclipse+PyDev+GAE memcache error

 Using memcache in GAE like this in my code:

tenth = memcache.get('tenth') 

There is an error notification in Eclipse+PyDev, but it works well. Checked a website, found other people have the same problem, so that's just a bug of PyDev, not mine, luckily.

Reference: http://stackoverflow.com/questions/1469860/eclipsepydevgae-memcache-error

21 January, 2010

JavaScript passing value to HTML in same page

As mentioned in the previous post Generate a random number from website. There is a variable generator the JavaScript. How to pass this value to an   element of the same page? 
We use:   
document.test.result.value = generator;
test is the form name in the same page's HTML, result is an input element in this form, so this sentence means to pass the value of the variable generator to the input element result in form test.
Then, we can use Python to revoke the element's value for other use.

20 January, 2010

Using the Templates from Python

from google.appengine.ext.webapp import template

class MainHandler(webapp.RequestHandler):
  def get(self):
      temp = os.path.join(
              os.path.dirname(__file__),
             'templates/index.htm')
      outstr = template.render(
              temp,
             {'hint': 'Good luck!'})
             self.response.out.write(outstr)
We add an import statement to make the template library available to our application.

Within the get() method, the first line looks up the full path to the template file by using the path to the currently executing file and adding templates/index.htm to the end of the current file’s path.

The actual template processing is done in the template.render() line. This takes two parameters: the first is the location of the template file from the previous step (stored in the variable temp) and the second is a Python dictionary object, which contains the strings to be placed in the template where the {{ hint }} entries are found. The results f the substitution of the variables into the template are returned as a string in the variable outstr.

Generate a random number from website

<script type="text/javascript">
function randomnumber()
{
var generator = Math.floor(Math.random()*100);
document.test.result.value = generator + ' wpm';
}
</script>

<form name="test">
<input type=button value=Finish onclick="randomnumber()">
Test Result : <input name=result readonly type=text size=6>
</form>

19 January, 2010

Check email in html form

<script type="text/javascript">

function check_email(email_id, err_id){
emailRegExp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9]+(\.[a-z0-9]+)*(\.([a-z]){2,4})$/;
var err_mail='Email address incorrect!';
if(emailRegExp.test(document.getElementById(email_id).value)){
alert('true');
return true;
}else{
document.getElementById(err_id).innerHTML=err_mail;
alert(err_mail);
return false;
}
}
</script>



<span id="err_msg" style="color: red;"></span>
<form id="myForm" name="myForm" action="./register.php" method="post" onsubmit="return check_email('email','err_msg');">
<input type="text" name="email" id="email" size="20"/>
<input type="submit" name=send" value="Send" />
</form>

18 January, 2010

Extending Base Templates

{% extends "_base.htm" %}
{% block bodycontent %}
<h2>App Engine: About</h2>
<p>
Welcome to the site dedicated to
learning Google App Engine.
We hope you find www.appenginelearn.com useful.
</p>
{% endblock %}

The template language uses curly braces and percent signs to indicate our commands to the render engine. The first line says this page starts with the text contained in the file _base.htm. We are starting with _base.htm and then extending it.
The second line says, “When you find an area marked as the bodycontent block in the _base.htm file, replace that block with the text in between the block and endblock template commands.”

The _base.htm file is placed in the templates directory, along with all the rest of the template files.

16 January, 2010

What is ' if __name__ == "__main__": ' for?

The if __name__ == "__main__": ... trick exists in Python so that our Python files can act as either reusable modules, or as standalone programs. As a toy example, let’s say that we have two files:

$ cat mymath.py
def square(x):
return x * x
if __name__ == '__main__':
print "test: square(42) ==", square(42)

$ cat mygame.py
import mymath
print "this is mygame."
print mymath.square(17)

In this example, we’ve written mymath.py to be both used as a utility module, as well as a standalone program. We can run mymath standalone by doing this:

$ python mymath.py
test: square(42) == 1764

But we can also use mymath.py as a module; let’s see what happens when we run mygame.py:

$ python mygame.py
this is mygame.
289

Notice that here we don’t see the ‘test’ line that mymath.py had near the bottom of its code. That’s because, in this context, mymath is not the main program. That’s what the if __name__ == "__main__": ... trick is used for.

[From a post to Python Tutor by Danny Yoo]

Use google app engine webapp application

def main():
application = webapp.WSGIApplication([
('/.*', MainHandler)],
debug=True)
wsgiref.handlers.CGIHandler().run(application)

This bit of code is best translated as “Create a new web application, and route all the URLs that come to this application (/.*) to the MainHandler class; once the application has been created and configured with its routing, run the application.”

Google app engine-Python in Eclipse with PyDev

If you have more than one project run in Eclipse, when you want to switch one to another, you should terminate the previous project in the "Console" window in Eclipse. Otherwise, Eclipse will run the previous one instead of the one you actually want to run.

15 January, 2010

Get user IP address and put in the Datastore

x = TaggedIp()

x.IpAddress = os.environ["REMOTE_ADDR"]

x.put()

14 January, 2010

HTTP tool- Firebug

An essential tool to help you understand and debug many aspects of web programming in the browser is the Firebug plugin for Firefox, written by Joe Hewitt.

Adding Navigation Hints for Users---CSS

Change the link to the current page from blue to black and remove the underline. To do this, we add a class attribute to the link on the currently selected page.

from 'Using goole app engine' p39-p40

Web page design by CSS ---- tool

Another important tool for working with increasingly complex CSS is the Web Developer plug-in for the Firefox browser that is freely available from Chris Pedrick. Web developers have pretty much standardized on Firefox as their browser because of its support for web development. You can find and install the Web Developer plug-in at http://addons.mozilla.org/.

When the Web Developer plug-in is installed, you will have an additional toolbar when you start Firefox. There are many useful options in this toolbar. The first one we will use is under Outline→Outline Block Elements.† When you enable the outlining of block elements, you can see the structure of your web page, including which elements are contained within other elements. If you have made a mistake in the structure or nesting of your HTML, you may be able to quickly identify and fix the problem just by looking over the block elements.

from 'using google app engine'

13 January, 2010

Some terminology



1. CSS, or Cascading Styles Sheets, is a way to style HTML. Whereas the HTML is the content, the style sheet is the presentation of that document.

2. MVP, Model-view-presenter

3. GWT, Google Web Toolkit
4. The MVC pattern splits the code of a web application into three basic areas:
Controller
The code that does the thinking and decision making.
View
The  HTML,  CSS,  and  other  elements  that  make  up  the look  and  feel  of  the application.
Model
The persistent data that we keep in the datastore.






What is Memcahe Entries?

One part of the new Test Subsystem. I need to design the Memcache Entries for the system.

From book 'Developing with Google App Engine', Chapter 7, Memcache and Session Data, it shows:

What Is Memcache?
Memcache is a service that provides a key-value caching mechanism for
efficient in-memory data retrieval across multiple instances of an App
Engine application. The Memcache API enables
􀂃 A reduction in the number of Datastore queries
􀂃 A reduction in the Datastore quota usage for popular pages
􀂃 Caching of expensive query results
􀂃 Implementation of transient counters
Data in Memcache are available to every instance of an application and
only discarded due to cache exhaustion.

Checked the book ' Using Google App Engine ', Chapter 11- Memory Cache, P193-P203.

Questions in view.py

What's the meaning of " Option = "21" Access = "2" "?
A: They are the access authority of the datastore, mainly for the admin screen, nothing to do with the student account users.

Why I can log in by using Account " PIT", but cannot log in by another group of users?

10 January, 2010

About 3 years have passed!

3 years................