Python Web Development 2 Questions

1.Why do we use virtual environments? The advantage of virtual environments is avoids the effects of different Python projects,each Python project has its own library and interpreter.

virtualenv is most popular tool of Python virtual environments config.It not only support Python 2 but also Python 3,it still can designate interpreter for each virtual environment,but doesn’t inherit the basic library. From Python 3.3,there is a  buildin virtual environment called venv,it similar to  virtualenv,but it only supports Python 3.3 and the versions after Python 3.3. So,we can use command

python -m venv myenv

to create our virtual environments,actually,the file of the command is Python Root\Tools\scripts\pyvenv.py,and the official website of the venv is

https://docs.python.org/3/library/venv.html,you can take a look at it.

Why do we use Blueprints? A system has many different modules,if we put all of the code into one file,the file size will be getting bigger and bigger,more and more code, it makes the code harder to maintain. It prone to error,and difficult to find the error where.And we have to spend more time to combine the code if multiple people work on the the same file. Blueprints allows us to separate the different routes,it also has different static resources.Another words,Blueprints makes the code more loose coupling,more flexible and code reusability.It still can increases efficiency for locating the errors and reduce the error rate. Look at this websites:

http://www.pythondoc.com/flask/blueprints.html http://flask.pocoo.org/docs/0.10/blueprints/

Setup Python

1.Creating virtual environments.

On Windows,just run the command like this.

if you don’t have the relevant PATH settings,you may have to invoke the pyvenv script as follows.

c:\Python35\python c:\Python35\Tools\Scripts\pyvenv.py venv

A virtural environments directory will be created when you run the command

2.Activate the virtual environments.

Once a venv has been created,it can be “activated” using a script in the venv’s binary directory,on Windows,in the Command-Prompt,we can run

venv\Scripts\activate.bat

to activate the virtual environments directly.

3.Install Flask.

Now,we are in the activated virtual environments,just run

pip install Flask

command,a few seconds later,the installation is complete.

4.Install pypyodbc

Still in virtual environments in Ccommand-Prompt,just run

pip install pypyodbc

to install it,look at the screenshot below.

How to install Python on Windows

Step 1.Download Python from the official Websites https://www.python.org/,i get the latest version of Python 3.5.1

Step 2. Just double click the python-3.5.1-amd64.exe to start the installation,then click Customize installation, select Add Python 3.5 to PATH,so that the install directory will be added to the system PATH.

Step 3. Cause of it is product environment,i deselect the Documention and IDLE,then click next button.

Step 4. Advanced Options,i just change install location to D:\Python\Python35,then click Install button to start installing Python.

Step 5. Setup was successful,just click the Colse button to finished installing.

Step 6. Check whether the installation is successful.Ctrl + R to open command prompt,type in python -V or python command,the installation is successful if what is seen in the below image.

 

Different Countries Have Different Kinds of Englishes

Voyages of people from England play an important part in spreading the English language.At present.English is frequently spoken as an official or common language in many countries,such as America,Singapore,Malaysia and some African countries.All based on British English,the English spoken in these countries can be well understood by native English speakers.But actually,these Englishes have been gradually changing in accents,spellings,expressions and the usage of vocabulary.Because of this fact,you can make use of the differences to tell which country the foreigners of your block are from.For example,if a boss fluently commands his driver,”Come up straight to my apartment by elevator and take some gas for my trucks and cabs”,instead of requesting,”Please come to my flat by lift and take some petrol for my lorries and taxis”,you can recognize his American identity,while the latter suggests that he is British.

New Words:

  • voyages
  • present
  • frequently
  • gradually
  • accents
  • vocabulary
  • foreigners
  • fluently
  • straight
  • flat
  • petrol
  • lorries
  • recognize
  • identity

Fall in Love with English

Hiding behind the loose dusty curtain,a teenager packed up his overcoat into the suitcase.He planned to leave home at dust though there was thunder and lightning outdoors.He had got to do this because he was tried of his parents’ nagging about his English study and didn’t want to go through it any longer.He couldn’t get along well with English and disliked joining in English classes because he thought his teacher ignored him on purpose.As a result,his score in each exam never added up to over 60.

His partner was concerned about him very much.She understood exactly what he was suffering from,but entirely disagreed with his idea.In order to calm him down and settle his problem,she talked with him face to face and swapped a series of learning tips with him.The items she set down helped him find the highway to studying English well.

The teenager was grateful and got great power from his friend’s words.Now,he has recovered from being upset and has fallen in love with English.

New Words:

  • loose
  • curtain
  • suitcase
  • dusk
  • thunder
  • lightning
  • nagging
  • partner
  • concerned
  • suffering
  • entirely
  • calm
  • settle
  • upset

i have a dream

i donn’t konw when i began to love traveing on the Google maps,i want to travel around the world,this is my greatest dream.

i left China for the first time in November 2013 to go to Singarpore,feels like this ongoing dream.Many things for the first time to me on this trip.such as my first flight,my first to go ot another country,a lot of things was new for me,i really like it.In Singarpore,i have to speak English,even though i can only speak a little English.it helps me realize that,English is very important.I must learn English well if i want to realize my dream.

So,i forced myself to reading more English books & technical documentation,learn more English words,listen to English songs,write something in English.i want to improve my English quickly,but it looks very difficult,so i have to make a little progress every day.

Traving on the Google maps,looking at some beautiful scenery,it gives me a lot of motivations.i believe that my dream will surely come true.

Getting started with Wechat

This week is about to end,it didn’t receive any email from DHL customers until now,so i am a little bit not used it.

The good news is I can spend more time to study some courses,such as Android or Wechat.i finished the WeChat Public Platform Development Best Practice,know more about Wechat developement and take some notes below.

How to create a Wechat test account?
Just go to http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login and click the login button,then use Scan QR Code functionality on you mobile phone to scan the QR on the desktop,it’s very easy.

How to set up URL & Token with your own server?
First,we should have our own server,and set up a can-access website as wechat developement project,like this http://wx.webtribe.com.cn.I set the URL to http://wx.webtribe.com.cn/api.php,then set the Token to webtribe,so the codes in api.php are:
[php]
/*
Webtribe http://www.webtribe.com.cn
Pengcheng 2016-05-12
*/

define(“TOKEN”, “webtribe”);
$wechatObj = new wechatCallbackapiTest();
if (isset($_GET[‘echostr’])) {
$wechatObj->valid();
}else{
$wechatObj->responseMsg();
}

class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET[“echostr”];
if($this->checkSignature()){
header(‘content-type:text’);
echo $echoStr;
exit;
}
}

private function checkSignature()
{
$signature = $_GET[“signature”];
$timestamp = $_GET[“timestamp”];
$nonce = $_GET[“nonce”];

$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );

if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}

public function responseMsg()
{
$postStr = $GLOBALS[“HTTP_RAW_POST_DATA”];

if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, ‘SimpleXMLElement’, LIBXML_NOCDATA);
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = “


%s


0
“;
if($keyword == “?” || $keyword == “?”)
{
$msgType = “text”;
$contentStr = date(“Y-m-d H:i:s”,time());
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}
}else{
echo “”;
exit;
}
}
}
[/php]

How to get the Access Token?
Just request https://api.weixin.qq.com/cgi-bin/token with your appid and secret of you wechat test account,the final URL maybe is https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret,ok,let me share the codes to you.
[php]
$appid = “wx97fxxxxxxxx9bbda”;
$appsecret = “3efd14bcxxxxxxx38ade06cf697fc40”;
$url = “https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$appsecret”;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$jsoninfo = json_decode($output, true);
$access_token = $jsoninfo[“access_token”];
echo $access_token;
exit;
[/php]

i have not finished wechat developement,will write more when i get more,so stay tuned!

Set email notification on Git commits

How do i configure the appropoiate Git hook to send a summary email whenever a set of changes is pushed to the repository?

There is a simplest solution:

  1. Go to one of your repository that you want to setup email notification.
  2. On the right menu,click on Settings,go to repository setting page.
  3. On the left options,click on Webhooks & Services.
  4. In the Services tab,click Add services,from available services,choose Email.
  5. Fill in your email address in field Address.
  6. Check the checkbox Sedn from Author & Active.
  7. Click the Add service button to complete the configuration.
  8. After setting,we can click the Test service button to send the test email.

This is my way to setup email notification. 🙂

jQuery Validate addMethod

在用jQuery Validate时,有时它提供的标准的验证规则也不一定能完全满足我们的业务需求。但是它提供了可自定义验证规则的方法addMethod。

如我们的客户DHL的员工号是是以14开头的8位数字,那么我们在使用jQuery Validate验证时,就可以这样自定义验证规则:

[js]
// Custom validation rule for DHL employee email
jQuery.validator.addMethod(“dhlEmpId”, function(value, element) {
return this.optional(element) || /^(14)[0-9]{6}$/.test(value);
}, “请输入8位员工号!”);
[/js]

其中:this.optional(element)的意思是出发这个规则的element的值为空时不触发验证,换句话说就是这个element的值可以不填,如果填了就不能填错格式。

如果想要这个element的值为空也要触发验证,直接把this.optional(element)去掉即可:return /^(14)[0-9]{6}$/.test(value);

当然你也可以在这个方法体内自己定义较复杂的规则,包含自己的业务逻辑,如下是一个天数的验证:

[js]
$.validator.addMethod(“validAvailDays”, function( value, element ) {
var courseAvailOption = $(“#quiz-basic-attr-form input[name=sb_course_avail]:checked”).val();
if(courseAvailOption == 2) {
var cknum = /^[1-9]\d*$/;
if(cknum.test($(“#sb_avail_days”).val())){
return true;
}
else{
return false;
}
}
else {
return true;
}
}, “天数限制必须是大于0的数字!”);
[/js]

js & jQuery验证Email

之前没有jQuery Validate这个插件,我们一般会用正则表达试来验证表单,检查用户输入的数据是否合法。

html代码如下:
[xhtml]



[/xhtml]

那js验证Email时就会像下面这样:

[js]
var emailStr = document.getElementById(“email”).value;
var emailRegex = /^([a-zA-Z0-9]+[|_|.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[|_|.]?)*[a-zA-Z0-9]+.[a-zA-Z]{2,3}$/;
if(emailRegex.test(emailStr)){
console.log(“Your email is valid!”);
}
else{
console.log(“You email is invalid!”);
}
[/js]

接触jQuery Validate之后呢,验证Email就比较简单了:

[js]
$(document).ready(function() {
$(“#user-info-form”).validate({
rules: {
email: {
required: true,
email: true
}
},
messages: {
email: “请输入一个正确的邮箱!”
}
});
});
[/js]