Practical Encoding in Python2

We all know how a python script gets run. The python interpreter reads the source file, trans-compiles it into machine code and runs it. Mainly we write codes in ASCII, but what if we need a string containing unicode characters like: s = “你好哇”? The default encoding of source code is ASCII, and python interpreter will throw an error since it doesn’t know how to parse it. Therefore, the magic comment # coding=utf-8 shows at the first line. It tells the interpreter that the source code is encoded in UTF-8, and please parse it in this encoding. PEP explains it very well, and magic comment like # -*- coding: utf-8 -*- also works.

阅读更多

What will happen when fetch meets a 302 status code?

Yesterday, I was integrating SSO into a project. Originally, the backend would return 302 and with a location header to tell the browser to jump to the SSO page if the use is not logged in. It seems not a big deal, redicting to the right login page to save user a click. However, when I need to fetch some data from endpoint, the fetch will fail and the redirection wno’t happen.

阅读更多

CSS Animation and the Usage with React

I have been busy with hacking React in the last week. React is declarative, which means that all you need to do is to define the state, implement the render() method, and React will make the component be what it shoud be. No DOM manipulation like find, insert, and delete. But there’s a problem if you want to make some animation when the component shows. When used with jQuery, you can add a CSS class to an element to change some style after the element shows. However, it’s hard to do so because React performs all the DOM operation and it’s better not to access the DOM node directly. Good news is that React does offer some hooks to help you implement transitions and animaitions, and this post will discuss them by examples.

阅读更多

Server Side Rendering with Redux and React-Server

Isomorphic JavaScript, or universal JavaScript, is more and more popular nowadays. Basicly, it means JavaScript applications that can run on both client and server side. It has lots of advantages over traditional web development paradigms (e.g, Ruby on Rails, Django, etc.), in terms of SEO, response speed and code maintainability. The key part of isomporhic JavaScript is Server Side Rendering (SSR). This post will introduce the basic concepts of SSR and discuss some SSR implementations.

阅读更多

Setting Up HTTP Proxy in Terminal

Recently, I have rent a server on Aliyun to set up websites and play around with web service, and it’s very exciting to have a virtual host to show some awesome stuff. However, due to some government regulations, you cannot visit some websites, invoke some web service abroad or even install some python packages if you’re in China. This post details the process of setting up a HTTP proxy in the terminal and speed up git, pip, curl, etc.

阅读更多

Javascript Closure: Typical Usages and Gotchas

You can see closures nearly everywhere in JavaScript, and this post will discuss closure. We’ll start from the definition, then some typical usages will be given and finally we’ll talk about some common mistakes with closure.

阅读更多

Python re Grammar Guide

In previous post some common usage of the re module are introduced, and this post will detail the grammar. After walking through this post, you can write regular expressions to obtain the comments in /* coments */, get the local part and domain part of an e-mail, filter out e-mail addresses by some criteria, and etc.

阅读更多

Quick Guide to Python re module

This post will give a quick guide to the common usages of python re module. Python re module is mainly comprised of some handy module functions, such as search, match etc, two useful classes: RegexObject for matching and MatchObject for the result, and serval constant flags. This post won’t detail all the usages and grammars of regular expression; instead, it focuses on how to use the functions and classes re offers to achieve our purpose.

阅读更多

Customize Browser Back and Forward Button Behavior

Nowadays, lots of web applications are springing up. It’s common to make the browser back and forward button work as expected to improve the user experience. This post discusses some hash related topics and introduces serveral practical methods to manipulate the browser history.

阅读更多

Deploy Django Project in nginx with uWSGI on Mac OS X

I have just (or maybe not yet) gone through the panic dealing with all the configuration of Django framework on Mac OS X. This post details the steps to create a Django project and deploy it with uWSGI in nginx. Since the documentations and tutorials online mainly relate to Linux system, I hope this post will help you if you love Mac and want deploy Django apps on it.

阅读更多