2017-11-12 11:02:02    287    0    0

废话少说,直接上代码:)

参考: How To Install and Set Up a Local Programming Environment for Python 3

#!/bin/bash
# install and setup python3 on centos

sudo yum -y update
sudo yum -y install yum-utils
sudo yum -y groupinstall development

# install python3.6
sudo yum -y install https://centos7.iuscommunity.org/ius-release.rpm
sudo yum -y install python36u
sudo yum -y install python36u-pip

# install python development tools
sudo yum -y install python36u-devel

# setup virtual environments
mkdir ~/environments
cd ~/environments
python3.6 -m venv python3.6
source python3.6/bin/activate
2017-11-09 21:47:38    481    0    0

Pony是一门非常有趣的编程语言:面向对象 (只有组合,没有继承) + Actor (异步行为) + 基于能力的数据安全 (类型安全+内存安全+异常安全+无数据竞争+无死锁)。

“A programming language is just another tool. It’s not about syntax. It’s not about expressiveness. It’s not about paradigms or models. It’s about managing hard problems.” (Sylvan)

一门编程语言就是另一个工具。她无关乎语法,无关乎表达能力,甚至无关乎编程范式或模型。她就是要管理和解决困难的问题。

Pony是编译型强类型的具有Actor的语言。她的调度器相当简单而调度的代价很轻。具有"偷工作"(work stealing)的特性,能和现代CPU架构尽可能高效地处理工作。因为她的垃圾回收是在Actor里进行的,所以时延相当可预测,比较一致。

尽管是偏研究型的,已经用于开发金融计算技术和高性能并发大数据处理底层框架,如:

Wallaroo - Ultrafast and elastic data processing https://www.wallaroolabs.com

 

What's Pony, anyway?

Pony is an object-oriented, actor-model, capabilities-secure programming language. It's object-oriented because it has classes and objects, like Python, Java, C++, and many other languages. It's actor-model because it has actors (similar to Erlang or Akka). These behave like objects, but they can also execute code asynchronously. Actors make Pony awesome.

When we say Pony is capabilities-secure, we mean a few th

2017-11-05 16:10:16    542    1    0

Source: https://medium.com/gitconnected/react-component-patterns-ab1f09be2c82

Stateful x Stateless, Container x Presentational, HOCs, Render Callbacks and more

It’s been a while since I’ve been working with React — a Facebook library to build user interfaces using JavaScript — and there are a few concepts I wish I knew when I was just starting. This text is an attempt to summarize some patterns I learned during my experience so far — and also may be useful for developers who are just about to enter this awesome component-based world.

 

Stateful x Stateless Components

Just as Stateful and Stateless web services, React components can also hold and manipulate state during application usage (Stateful) — or just be a simple component that takes the input props and returns what to display (Stateless).

A simple Stateless button component that depends on props only:

 

And a Stateful counter component example (using Button component):

 

As you can see, the last one’s constructor holds a component state,

2017-11-05 10:40:41    242    0    0

最近在项目中碰到一个诡异的BUG:前端AJAX请求超时错误,没有从服务器得到返回值,故而页面没有及时更新。经查: 那个POST方法用了异步方法修饰器@asynchronous,却按同步方式直接返回 JSON,所以服务器没有正确返回结果。这里特别记录一下这个低级错误。 (话说Node.js中默认都是异步操作,就没有这样的困扰:)

 

TIPS:Tornado 异步方法一定要用 self.finish() 完成请求才能正确响应。

 

官方参考:http://www.tornadoweb.org/en/stable/web.html

相关内容特别摘录如下,注意粗体部分:

Decorators

tornado.web.asynchronous(method)[source]

Wrap request handler methods with this if they are asynchronous.

This decorator is for callback-style asynchronous methods; for coroutines, use the @gen.coroutine decorator without @asynchronous. (It is legal for legacy reasons to use the two decorators together provided @asynchronous is first, but @asynchronous will be ignored in this case)

This decorator should only be applied to the HTTP verb methods; its behavior is undefined for any other method. This decorator does not make a method asynchronous; it tells the framework that the method is asynchronous. For this decorator to be useful the method must (at least sometimes) do something asynchronous.

If this decor

2017-10-30 21:24:35    1186    0    0

在Windows上配置Pyhton的开发环境还是很有挑战的。这里记录一下碰到的问题和解决办法,仅供参考。

 

A. 首先就碰到Unicode Decode Error。根据错误信息提示修改相应的__init__.py文件, 如下:将utf-8改为gbk。

c:\users\Administrator\appdata\local\programs\python\python36\lib\site-packages\pip\compat\__init__.py

 

B. 运行 pip install pycrypto 遇到一大堆的编译错误,很可能是编译环境没配置好。

在已经安装好Microsoft Visual Studio 2015 Community的前提下(真正需要的是Microsoft Vicual C++ 2015 Build Tools):

    1. 设置Microsoft Visual C++ 2015的环境变量,如图:


    2. 打开cmd,执行如下命令:set CL=/FI"%VCINSTALLDIR%\\INCLUDE\\stdint.h" %CL%

      技术分享

    3. 重新安装pycrypto 2.6.1

pip install pycrypto

 

后记: 2018.5.16

最近Windows 10 升级后碰到缺Winrandom的报错。这要在安装好Pycrypto后找到 crypto\Random\OSRNG\nt.py 文件,编辑里面的 import winrandom 为 from . import winrandom 可以解决。

 

另一个方法是在Windows上用集成的Linux Subsystem。同样碰到一堆编译错误。解决办法: 安装Python开发环境 Python3-dev。

sudo apt-get install python3-dev

 

Reference:

http://www.mamicode.com/info-detail-1928900.html

2017-10-05 12:50:40    431    0    0

Python因为不同版本的原因需要特别设置, 颇费一番功夫。一个办法是直接用python3和pip3。 另一个办法是用pyenv,记录如下:

  

Ubuntu16.04

1. 安装pyenv

curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash

参考:https://github.com/pyenv/pyenv-installer

 

2. 更新 .bashrc, 添加以下内容:

export PATH="/home/jim/.pyenv/bin:$PATH"

eval "$(pyenv init -)"

eval "$(pyenv virtualenv-init -)"

 

3. 安装Python 3.6.3

出现错误

$ pyenv install 3.6.3
Downloading Python-3.6.3.tar.xz...
-> https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz
Installing Python-3.6.3...

BUILD FAILED (Ubuntu 16.04 using python-build 20160602)

Inspect or clean up the working tree at /tmp/python-build.20171005113129.1780
Results logged to /tmp/python-build.20171005113129.1780.log

Last 10 log lines:
    ensurepip._main()
  File "/tmp/python-build.20171005113129.1780/Python-3.6.3/Lib/ensurepip/__init__.py", line 189, in _main
    default_pip=args.default_pip,
  File "/tmp/python-build.20171005113129.1780/Python-3.6.3/Lib/ensurepip/__init__.py", line 102, in bootstrap
    _ru

2017-09-30 21:27:04    366    0    0

因为项目开发的需要,今天开始学用Python,争取尽快上手 :)

 

之前认为,Python的运行效率不高,没认真用。不过,Python的优势就在于专业做胶水,具有稳定的C接口,性能的优化交给C/C++。

 

刚好看到这个Web微框架,刷了三观。当然,速度来自于架构在优秀的底层的C代码之上,充分利用HTTP管道性能优化。暂且拭目以待。

 

Source: https://github.com/squeaky-pl/japronto

Screaming-fast Python 3.5+ web micro-framework integrated with pipelining HTTP server based on uvloop and picohttpparser.

Japronto!


Japronto (from Portuguese "já pronto" /ˈʒa pɾõtu/ meaning "already done") is a screaming-fast, scalable, asynchronous Python 3.5+ HTTP toolkit integrated with pipelining HTTP server based on uvloop and picohttpparser. It's targeted at speed enthusiasts, people who like plumbing and early adopters.

You can read more in the release announcement on medium

Performance

Here's a chart to help you imagine what kind of things you can do with Japronto:

Requests per second

As user @heppu points out Go’s stdlib HTTP server can be 12% faster than the graph shows when written more carefully. Also there is the awesome fasthttp server for Go that apparently is only 18% slower than Japront

2017-09-28 20:08:34    507    0    0

今天试用FireFox开发者版本 FireFox Quantum,感觉真不错。秒开网页,丝滑般的体验。界面也很现代,完全超越上代产品,很是喜欢。这是Mozilla用Rust语言重新开发的浏览器内核,充分利用了多核CPU的性能。赞一个!


https://www.rust-lang.org


2017-09-25 22:13:50    277    0    0

NanoID: 一个极小的,安全的ID生成器。UUID的替代品。压缩后仅179字节。

Source:https://github.com/ai/nanoid

 

A tiny, secure URL-friendly unique string ID generator for JavaScript.

var nanoid = require('nanoid')
model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqLJ"

Safe. It uses cryptographically strong random APIs and guarantees a proper distribution of symbols.

Small. Only 179 bytes (minified and gzipped). No dependencies. It uses Size Limit to control size.

Compact. It uses more symbols than UUID (A-Za-z0-9_~) and has the same number of unique options in just 22 symbols instead of 36.

The generator supports Node.js and all browsers starting from IE 11.

Security

See a good article about random generators theory: Secure random values (in Node.js)

Unpredictability

Instead of unsafe Math.random() Nano ID uses crypto module in Node.js and Web Crypto API in browsers.

Uniformity

random % alphabet is a popular mistake to make when coding an ID generator. The spread will not be even; there will be a lower chance for some symbols to appea

2017-09-21 21:46:25    470    0    0

Source: http://yafeilee.me/blogs/88


现在有很多人用 Rails 第一件事就是关闭 Turbolinks, 为了少遇到一些 "古怪" 的问题而选择关闭它, 这是否是因噎废食?

实际上, Turbolinks 用的好, 能够让 Rails 应用比 AngularJS 或 ReactJS 构建的单页应用还要快. 而学习成本又极低. 说不定你看完此文就明白了. 本文尝试解答以下问题:

  1. Turbolinks5 的出现背景
  2. Turbolinks5 是否应该使用, 在什么样的情况下使用?
  3. Turbolinks5 与 Turbolinks-classic 有哪些区别?
  4. Turbolinks5 典型问题与解决方法
  5. Turbolinks5 技术内幂

Turbolinks 背景

首先, 应该先解读一下 Turbolinks5 出现的背景与意义, 以此可以基本推出它解决问题的思路与采用的技术.

Turbolinks5 是一个 "简单" 的单页应用 JS 客户端框架, 能够让你轻易地实现 "单页面" 应用, 换言之, 它某种程度与 AngularJSReactJS 一样, 都是为了提升客户端体验更快. 不同的地方是, 它是足够 "简单" 的方案, 几乎只需要提供一行代码即可:

//=require 'turbolinks'

它解决问题的思路与 Rails 的理念是一致的: 通过简单直白的思路解决 80% 的问题, 剩下的交给你. 我们来看看它如何解决问题.

一个直白的公式: 网页加载速度 = 下载资源速度 + 解析资源速度

"单页面应用" 快的秘诀就在于它同时减少了下载资源的大小( 除却第一次加载模板后, 后续全部使用 JSON API), 以及极大提高了解析资源的速度( 通过 JSON 数据就能更新页面).

Turbolinks 无法解决下载资源的大小的问题, 却可以通过几乎不影响原有网页架构的情况下极大提高解析资源的速度.

为了理解 Turbolinks 的工作原理, 我们先来看一下在 chrome 浏览器中, 网页是如何被加载的.

  1. 下载 index.html
  2. 解析 head 标签中的 link 与 script 标签, 如果是带有 src 属性, 阻塞其他逻辑执行, 继续去下载对应的资源并执行. 如果没带, 则直接执行其中的代码逻辑.
  3. 渲染 body 标签的内容, 并解析执行 bod