简单想法

用于记录一些简短的想法

Views: 82871

440 Responses to "简单想法"

  • Python 代码正常, 但一用到管道就报错:

    UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 35-36: ordinal not in range(128)

    可以这样:
    import sys
    reload(sys)
    sys.setdefaultencoding(‘utf-8′) Reply
  • Python 打印异常堆栈:

    import traceback
    s = traceback.format_exc()
    print s Reply
  • vim 配置

    set nocompatible
    set shiftwidth=4
    set tabstop=4
    "set langmenu=english
    set autoindent
    "colors torte
    set nu

    syntax on

    set encoding=utf-8
    set incsearch Reply
  • PHP过滤UTF-8非法字符:


    // 不一定总是可行
    function strip_invalid_utf8($text){
    $text = iconv("UTF-8", "UTF-8//IGNORE", $text);
    return $text;
    }
    Reply
  • php -a 之后, 要先输入<?php 回车才能开始输入PHP代码执行. Reply
  • thrift 是一个网络服务开发框架, 包括协议, Server框架, Client框架. Reply
  • Facebook的观点: 为什么要有"服务(Service)"的概念, 因为要让代码和数据的距离更近. 也就是说, 服务就是运行中的代码和代码所操作的数据. Reply
  • in future = further Reply
  • MySQL 查询 group by 返回的结果, 所有的字段并不是全在一行里(各字段不是一一对应的), 例如 select id, some, max(val) group by som, 返回的 id 并不一定是当 val 取最大值时的那一行的 id. Reply
  • 打印数组列表的函数:


    <?php
    function dump_rows($rows){
    $header = array();
    $lens = array();
    $n = 0;
    foreach($rows as $row){
    if($n ++ == 0){
    foreach($row as $k=>$v){
    if($k == 'time'){
    $v = date('Y-m-d H:i:s', $v);
    }
    $lens[$k] = max(strlen($k), strlen($v));
    }
    }else{
    foreach($row as $k=>$v){
    $lens[$k] = max($lens[$k], strlen($v));
    }
    }
    }
    $n = 0;
    foreach($rows as $row){
    if($n ++ == 0){
    $ps = array();
    foreach($row as $k=>$v){
    $k = substr($k, 0, $lens[$k]);
    $ps[] = str_pad($k, $lens[$k], ' ', STR_PAD_BOTH);
    $header[] = str_pad('', $lens[$k], '-', STR_PAD_BOTH);
    }
    echo '+-' . join('-+-', $header) . "-+\n";
    echo '| ' . join(' | ', $ps) . " |\n";
    echo '+-' . join('-+-', $header) . "-+\n";
    }
    $ps = array();
    foreach($row as $k=>$v){
    if($k == 'time'){
    $v = date('Y-m-d H:i:s', $v);
    }
    $v = substr($v, 0, $lens[$k]);
    if(is_numeric($v)){
    $ps[] = str_pad($v, $lens[$k], ' ', STR_PAD_LEFT);
    }else{
    $ps[] = str_pad($v, $lens[$k], ' ', STR_PAD_RIGHT);
    }
    }
    echo '| ' . join(' | ', $ps) . " |\n";
    }
    echo '+-' . join('-+-', $header) . "-+\n";

    echo count($rows) . " row(s)\n";
    }
    Reply

« [4][5][6][7][8][9][10][11][12] » 8/44