CuriousY A world with wonder

Debug Javascript using Webstorm

| Comment

Debug Javascript in web page##

Prerequisites###

Install corresponding Webstorm plugin for your browser.

Debug###

  1. Create a html file which import your js file.
  2. Debug the html file:
    1. Configure the URL of the debugger to something like http://localhost:63342/untitled/index.html.
    2. Make sure the browser plugin settings use the same port above.

Debug NodeJS##

Prerequisites###

Install Nodejs interpreter.

Debug###

Debug the js file directly using Webstorm, just like debugging Python with Pycharm.

Read more

神奇的overflow:hidden

| Comment

最近用react-jsonschema-form重写之前写过的一个表单页面,发现布局上出现了很大的问题,表单行与行之间贴在了一起(包裹每一行元素的元素高度为0了),如下图所示:

screenshot1 有问题的表单,外部的元素高度为0

而重写之前的表单是好的:

screenshot2 正常的表单

网上简单搜索了下,发现只要把父级元素的overflow属性设为hidden即可。那么这是怎么样一个机制呢?还有一个问题就是,原先的表单也并没有手动设置过overflow这个属性,为什么就没有问题(都是直接用的bootstrap的样式)?

Read more

Reading <Types & Grammar> - 3

| Comment

Chapter 5: Grammar

Statements & Expressions

var a = 3 * 6;
var b = a;
b;

In this snippet, 3 * 6 is an expression (evaluates to the value 18). But a on the second line is also an expression, as is b on the third line. The a and b expressions both evaluate to the values stored in those variables at that moment, which also happens to be 18.

Moreover, each of the three lines is a statement containing expressions. var a = 3 * 6 and var b = a are called “declaration statements” because they each declare a variable (and optionally assign a value to it). The a = 3 * 6and b = a assignments (minus the vars) are called assignment expressions.

The third line contains just the expression b, but it’s also a statement all by itself (though not a terribly interesting one!). This is generally referred to as an “expression statement.”

“声明”和“表达式”的概念在几乎任何语言都是通用的。


It’s a fairly little known fact that statements all have completion values (even if that value is just undefined).

Let’s consider var b = a. What’s the completion value of that statement?

The b = a assignment expression results in the value that was assigned (18 above), but the var statement itself results in undefined. Why? Because var statements are defined that way in the spec.

The general idea is to be able to treat statements as expressions – they can show up inside other statements – without needing to wrap them in an inline function expression and perform an explicit return ...

For now, statement completion values are not much more than trivia.

这个设计我感觉是有点多余的,如果仅仅是为了省掉一行return语句的话


Note: Would you think ++a++ was legal syntax? If you try it, you’ll get a ReferenceError error, but why? Because side-effecting operators require a variable reference to target their side effects to. For ++a++, the a++ part is evaluated first (because of operator precedence – see below), which gives back the value of a before the increment. But then it tries to evaluate ++42, which (if you try it) gives the same ReferenceError error, since ++ can’t have a side effect directly on a value like 42.

这波解释很精妙。


This behavior that an assignment expression (or statement) results in the assigned value is primarily useful for chained assignments, such as:

var a, b, c;

a = b = c = 42;

Here, c = 42 is evaluated to 42 (with the side effect of assigning 42 to c), then b = 42 is evaluated to 42 (with the side effect of assigning 42 to b), and finally a = 42 is evaluated (with the side effect of assigning 42 to a).

这里之所以可以连续赋值,正是因为c=42除了赋值这个操作外还会将所赋的值返回出来。

Read more

实现Flask中view函数的装饰器

| Comment

Flask中view函数的装饰器和普通的装饰器其实并没有什么区别,但由于Flask本身实现上的一些限制,导致了在view函数上创建和使用装饰器可能会出现一些问题。

问题一

下面的代码会报错:

from flask import Flask, request, Response, redirect
import time

app = Flask(__name__)

def check_cookie(func):
    def wrapper():
        if not request.cookies:
            return redirect('/login')
        else:
            func()
    return wrapper

@app.route('/')
@check_cookie
def hello_world():
    return 'hello world'

@app.route('/show')
@check_cookie
def show():
    return request.cookies.__str__()

错误提示为:

AssertionError: View function mapping is overwriting an existing endpoint function: wrapper

原因在于,上述经过@check_cookie装饰过的函数的函数名称都叫做wrapper了,而Flask会维护一个dict存放rule和对应的endpoint,这里所有的endpoint在Flask看来都叫做wrapper。问题是我这里只是函数名称叫wrapper,怎么就变成了endpoint也叫wrapper了?

Read more

Linux网络和权限相关的坑

| Comment

最近在服务器部署一个发送数据的程序,结果不是很顺利。这里简单记一些遇到的坑。

测试两台机器之间的网络带宽,建议使用iperf命令。

监听方:

iperf -s -p 8000

发送方:

iperf -c {target_ip} -p 8000

如果iperf出现connect failed: No route to host,很可能是防火墙把端口禁用了,(CentOS)可以参考centos 7 - open firewall port开启端口。


要监测每个进程的收、发数据的速度,可以使用nethogs命令。


想在Linux中安装某个命令行工具(比如上面提到的iperf),却发现没有root权限(比如是在一个受限的Docker容器中),应该怎么办(apt-getyum都需要root权限)?

一个思路是下载所需工具的源代码,再进行编译安装,比如:

git clone https://github.com/esnet/iperf
cd iPerf
./configure
make
make install

另一个思路是直接复制编译好的可执行文件过去。


使用docker的-v选项挂载一个文件夹到容器中,却发现容器中对应的文件夹内容是空的,而docker run命令也并没有报错。是什么问题,该如何解决?

问题出在权限上,解决方法是把挂载文件夹的模式设为zZ(参考Permission denied on accessing host directory in docker),比如:

docker run -v /var/db:/var/db:Z rhel7 /bin/sh
| Page 9 of 25 |