Author: jpuyy

  • centos4机器被ansible管理

    使用 ansible 管理 centos4 的机器会报错, 默认安装的 python 是 2.3.4 ,需要在 centos4 上的机器添加 python2.7 的环境,以便于被 ansible 管理。

    下载安装

    wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tgz --no-check-certificate
    tar xzf Python-2.7.5.tgz 
    cd Python-2.7.5
    ./configure --enable-shared
    make
    make altinstall

    接下来创建一个记录 python lib 目录的配置文件,/etc/ld.so.conf.d/python27.conf 里记录

    /usr/local/lib

    并用 ldconfig 加载到系统

    ldconfig -v -f /etc/ld.so.conf.d/python27.conf

    保证 yum 可用

    cp /usr/bin/python /usr/bin/python2.3.4
    vim /usr/bin/yum 第一行改为
    #!/usr/bin/python2.3.4
    

    系统级别

    ln -nfs /usr/local/bin/python2.7 /usr/bin/python

    参考:
    http://stivesso.blogspot.jp/2014/08/ansible-hosts-install-alternate.html

  • python dict sort

    python 字典(dict)的特点就是无序的,按照键(key)来提取相应值(value),如果我们需要字典按值排序的话,那可以用下面的方法来进行:

    1 下面的是按照value的值从大到小的顺序来排序。

    dic = {'a':31, 'bc':5, 'c':3, 'asd':4, 'aa':74, 'd':0}
    dict= sorted(dic.iteritems(), key=lambda d:d[1], reverse = True)
    print dict

    输出的结果:

    [('aa', 74), ('a', 31), ('bc', 5), ('asd', 4), ('c', 3), ('d', 0)]

    下面我们分解下代码
    print dic.iteritems() 得到[(键,值)]的列表。
    然后用sorted方法,通过key这个参数,指定排序是按照value,也就是第一个元素d[1的值来排序。reverse = True表示是需要翻转的,默认是从小到大,翻转的话,那就是从大到小。

    2 对字典按键(key)排序:

    dic = {'a':31, 'bc':5, 'c':3, 'asd':4, 'aa':74, 'd':0}

    dict= sorted(dic.iteritems(), key=lambda d:d[0]) d[0]表示字典的键
    print dict

    http://www.cnpythoner.com/post/266.html

  • dd清除GPT信息

    方法一

    dd if=/dev/zero of=/dev/sdb bs=512 count=2

    方法二

    parted /dev/sdb mklabel gpt

    参考:
    http://unix.stackexchange.com/questions/167500/using-dd-to-zero-a-gpt-table-from-disc-how-many-bytes-to-write

  • javascript中的object

    “In C++ or C#, when we’re talking about objects, we’re referring to instances of classes or structs. Objects have different properties and methods, depending on which templates (that is, classes) they are instantiated from. That’s not the case with JavaScript objects. In JavaScript, objects are just collections of name/value pairs – think of a JavaScript object as a dictionary with string keys.”

    摘录来自: Manuel Kiessling. “The Node Beginner Book”

    自己在 node 中尝试

    > var handle = {}
    undefined
    > handle["/"] = 'aaa';
    'aaa'
    > handle["/start"] = 'aaa/start';
    'aaa/start'
    > handle["/upload"] = 'aaa/upload';
    'aaa/upload'
    > handle
    { '/': 'aaa',
    '/start': 'aaa/start',
    '/upload': 'aaa/upload' }
    > typeof(handle)
    'object'
    
  • Model-View-Controller

    Model-View-Controller

    CodeIgniter is based on the Model-View-Controller development pattern. MVC is a software approach that separates application logic from presentation. In practice, it permits your web pages to contain minimal scripting since the presentation is separate from the PHP scripting.

    The Model represents your data structures. Typically your model classes will contain functions that help you retrieve, insert, and update information in your database.
    The View is the information that is being presented to a user. A View will normally be a web page, but in CodeIgniter, a view can also be a page fragment like a header or footer. It can also be an RSS page, or any other type of “page”.
    The Controller serves as an intermediary between the Model, the View, and any other resources needed to process the HTTP request and generate a web page.

    CodeIgniter has a fairly loose approach to MVC since Models are not required. If you don’t need the added separation, or find that maintaining models requires more complexity than you want, you can ignore them and build your application minimally using Controllers and Views. CodeIgniter also enables you to incorporate your own existing scripts, or even develop core libraries for the system, enabling you to work in a way that makes the most sense to you.
    • 模型 包含与您的数据库和其他数据结构相关的所有代码。如果您具有一个名为 pages 的表,则您具有一个模型,其中具有用于从表中选择、创建、更新和删除记录的函数。
    • 视图 包含所有显示和 UI 元素 — JavaScript 代码、Cascading Style Sheets (CSS)、HTML 甚至 PHP。
    • 控制器 将一切联系在一起。控制器中的每个函数表示一个目的地或路线。如果您具有一个名为 /about 的目的地,则控制器将具有一个名为 about() 的函数。
  • python sleep

    在调试代码的时候要分析 block

    可以用 sleep 使程序 block 一段时间

    #!/usr/bin/python
    import time
    
    print "Start : %s" % time.ctime()
    time.sleep( 5 )
    print "End : %s" % time.ctime()