月度归档:2014年08月

Fedora 20安装Fcitx输入法并安装搜狗资源包

原文链接:http://yanue.net/post-140.html

1、先卸载系统自带的Ibus输入法

sudo yum remove ibus
gsettings set org.gnome.settings-daemon.plugins.keyboard active false

2、安装Fcitx输入法

yum install fcitx fcitx-devel fcitx-configtool fcitx-cloudpinyin fcitx-pinyin

3、配置一下Fcitx、在~/.bashrc中添加:如下内容

export GTK_IM_MODULE=fcitx  
export QT_IM_MODULE=fcitx  
export XMODIFIERS="@im=fcitx"

4、注销或重启后完成安装

上面步骤完成之后其实就可以使用Fcitx输入法

不过看到搜狗输入法都已经出deb包了

所以就想着安装个搜狗输入法到Fcitx上去。

5、安装搜狗资源包

从下面的连接地址下载对应的deb包、32位系统用i386.deb、64位系统用amd64.deb

http://mirrors.ustc.edu.cn/deepin/pool/non-free/f/fcitx-sogoupinyin-release/

随便在从如下地址下载一个搜狗输入法的皮肤

http://mirrors.ustc.edu.cn/deepin/pool/main/f/fcitx-skins/

6、下载完成之后双击打开amd64.deb、将里面的data.tar.bz提取出来、然后到解压压缩文件

接着我们把解压出来的.so文件移动到Fcitx的指定目录即可(64位为例)

cp /usr/lib/x86_64-linux-gnu/fcitx/fcitx-sogoupinyin.so /usr/lib64/fcitx/

最后加上执行权限

chmod +x /usr/lib64/fcitx/fcitx-sogoupinyin.so

fedora20下使用sublime text 3

原文链接:http://fewspider.github.io/linux/Sublime-Text-Chinese-input-under-Linux.html

编译库

复制以下内容到sublime-imfix.c

/*
sublime-imfix.c
Use LD_PRELOAD to interpose some function to fix sublime input method support for linux.
By Cjacker Huang

gcc -shared -o libsublime-imfix.so sublime-imfix.c `pkg-config --libs --cflags gtk+-2.0` -fPIC
LD_PRELOAD=./libsublime-imfix.so subl
*/
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
typedef GdkSegment GdkRegionBox;

struct _GdkRegion
{
  long size;
  long numRects;
  GdkRegionBox *rects;
  GdkRegionBox extents;
};

GtkIMContext *local_context;

void
gdk_region_get_clipbox (const GdkRegion *region,
            GdkRectangle    *rectangle)
{
  g_return_if_fail (region != NULL);
  g_return_if_fail (rectangle != NULL);

  rectangle->x = region->extents.x1;
  rectangle->y = region->extents.y1;
  rectangle->width = region->extents.x2 - region->extents.x1;
  rectangle->height = region->extents.y2 - region->extents.y1;
  GdkRectangle rect;
  rect.x = rectangle->x;
  rect.y = rectangle->y;
  rect.width = 0;
  rect.height = rectangle->height;
  //The caret width is 2;
  //Maybe sometimes we will make a mistake, but for most of the time, it should be the caret.
  if(rectangle->width == 2 && GTK_IS_IM_CONTEXT(local_context)) {
        gtk_im_context_set_cursor_location(local_context, rectangle);
  }
}

//this is needed, for example, if you input something in file dialog and return back the edit area
//context will lost, so here we set it again.

static GdkFilterReturn event_filter (GdkXEvent *xevent, GdkEvent *event, gpointer im_context)
{
    XEvent *xev = (XEvent *)xevent;
    if(xev->type == KeyRelease && GTK_IS_IM_CONTEXT(im_context)) {
       GdkWindow * win = g_object_get_data(G_OBJECT(im_context),"window");
       if(GDK_IS_WINDOW(win))
         gtk_im_context_set_client_window(im_context, win);
    }
    return GDK_FILTER_CONTINUE;
}

void gtk_im_context_set_client_window (GtkIMContext *context,
          GdkWindow    *window)
{
  GtkIMContextClass *klass;
  g_return_if_fail (GTK_IS_IM_CONTEXT (context));
  klass = GTK_IM_CONTEXT_GET_CLASS (context);
  if (klass->set_client_window)
    klass->set_client_window (context, window);

  if(!GDK_IS_WINDOW (window))
    return;
  g_object_set_data(G_OBJECT(context),"window",window);
  int width = gdk_window_get_width(window);
  int height = gdk_window_get_height(window);
  if(width != 0 && height !=0) {
    gtk_im_context_focus_in(context);
    local_context = context;
  }
  gdk_window_add_filter (window, event_filter, context);
}

执行以下命令生成SO文件:

gcc -shared -o libsublime-imfix.so sublime-imfix.c `pkg-config --libs --cflags gtk+-2.0` -fPIC

编译时如果提示 gtk/gtk.h: No such file or directory,需要先安装编译工具

yum install gtk2-devel

运行方式

LD_PRELOAD=./libsublime-imfix.so subl

生成启动快捷方式

复制subl目录下的sublime_text.desktop到/usr/share/applications/sublime_text.desktop并修改exec与icon

Icon=/opt/sublime_text_3/Icon/256x256/sublime-text.png
Exec=bash -c 'LD_PRELOAD=/opt/sublime_text_3/libsublime-imfix.so /opt/sublime_text_3/sublime_text' %F

现在,使用sublime可以支持fcitx输入法输入中文了,要将ibus替换成fcitx请参考我的另外一篇文章Fedora 20安装Fcitx输入法并安装搜狗资源包

又拍云python sdk的一个bug

upyun的pysdk存在bug,会导致部分文件上传失败。测试图片

问题原因

部分图像处理软件会去掉图片的description,而python的fileno如果没有取到description会抛出一个异常,程序没有处理这个异常,从而导致了无法上传。 pysdk有判断这个方法是否存在,但是没有处理这个异常。 https://docs.python.org/3/library/io.html#io.IOBase.fileno

解决办法

打开sdk的upyun.py中的173行处,修改

    if hasattr(value, 'fileno'):
        length = os.fstat(value.fileno()).st_size
    elif hasattr(value, '__len__'):
        length = len(value)
        headers['Content-Length'] = length
    elif value is not None:
        raise UpYunClientException('object type error')

    if hasattr(value, 'getvalue'):
        length = len(value.getvalue())
        headers['Content-Length'] = length
    elif hasattr(value, '__len__'):
        length = len(value)
        headers['Content-Length'] = length
    elif hasattr(value, 'fileno'):
        length = os.fstat(value.fileno()).st_size
        headers['Content-Length'] = length
    elif value is not None:
        raise UpYunClientException('object type error')

python面试题

  • 介绍你最熟悉的一个py框架,以及他的主要功能及类库说明,开发流程,以及项目结构?

  • 请写一段py异常及日志处理的代码

  • 请写一段py读写数据库的代码

  • 用文字或者代码介绍下py中的列表生成式、装饰器、匿名函数以及函数式编程

  • 设计一个新闻数据库,有100万记录,分为50个分类,分类列表页面需要显示该分类的最新20条新闻,请设计出新闻表结构以及索引,请写出建表以及查询SQL;

sphinx-for-chinese快速配置

建立累计表

CREATE TABLE sph_counter
(
    counter_id INTEGER PRIMARY KEY NOT NULL,
    max_doc_id bigint NOT NULL
);

下载并安装sphinx-for-chinese

下载地址:http://www.sphinx-search.com/

wget http://www.sphinx-search.com/downloads/sphinx-for-chinese-2.2.1-dev-r4311.tar.gz
tar -xvf sphinx-for-chinese-2.2.1-dev-r4311.tar.gz
cd sphinx-for-chinese-2.2.1-dev-r4311
./configure --prefix=/usr/local/sphinx-for-chinese --with-mysql --enable-id64
make -j4 install

下载词典:

wget https://sphinx-for-chinese.googlecode.com/files/xdict_1.1.tar.gz
tar -xzf xdict_1.1.tar.gz
/usr/local/sphinx-for-chinese/bin/mkdict xdict_1.1.txt /usr/local/sphinx-for-chinese/etc/xdict

在索引配置项中添加以下两项:

    charset_type = utf-8
    chinese_dictionary = /usr/local/sphinx-for-chinese/etc/xdict

全局配置

indexer
{
    mem_limit            = 512M
}
searchd
{
    listen          = 9312
    listen          = 9306:mysql41
    log         = /usr/local/sphinx-for-chinese/var/log/searchd.log
    query_log       = /usr/local/sphinx-for-chinese/var/log/query.log
    read_timeout        = 5
    max_children        = 30
    pid_file        = /usr/local/sphinx-for-chinese/var/log/searchd.pid
    max_matches     = 1000
    seamless_rotate     = 1
    preopen_indexes     = 1
    unlink_old      = 1
    workers         = threads # for RT to work
#   binlog_path     = /usr/local/sphinx-for-chinese/var/data 关闭binlog日志
}

测试代码

<?php

$s = new SphinxClient;
$s->setServer("192.168.2.101", 9312);
$s->setMatchMode(SPH_MATCH_ALL);
$s->setMaxQueryTime(3);

$keywords = array(
    'the',
    '一分钱补差价',
    '一分钱 补差价',
    '补差价 一分钱',
    '一分钱乱补差价',
    '一分钱 乱 补差价',
    '一分钱',
    '补差价',
);
foreach ($keywords as $k) {
 $result = $s->query($k);
 echo $k,"\n";
 if(isset($result['matches']) ) {
    print_r($result['matches']);
 }
}

ssh config常用配置

vim ~/.ssh/config

Host    别名
    HostName        主机名
    Port            端口
    User            用户名
    IdentityFile    密钥文件的路径

有了这些配置,就可以这样用 ssh 登陆服务器了:

ssh 别名

如果遇到Bad owner or permissions错误,执行以下语句:

sudo chmod 600 config

拷贝ssh key

ssh-copy-id 别名

gedit中文乱码解决

终端配置

gsettings set org.gnome.gedit.preferences.encodings auto-detected "['UTF-8', 'GB18030', 'GB2312', 'GBK', 'BIG5', 'CURRENT', 'UTF-16']"
gsettings set org.gnome.gedit.preferences.encodings shown-in-menu "['UTF-8', 'GB18030', 'GB2312', 'GBK', 'BIG5', 'CURRENT', 'UTF-16']"

使用dconf-tools工具进行配置

yum install dconf-tools,然后运行dconf-editor(ctrl+f2),依次点开->org->gnome->gedit->preferences->encodings设置如上即可