技术小黑屋

乘手扶梯的正确方法

####起因 之前一直和女朋友有一个讨论,就是搭乘扶梯时究竟时靠左站立,还是靠右站立。起因时从地铁北宫门出来的时候,我要求她站在左侧。
####我的观点

  • 右侧留出来行走,更符合国内靠右行驶的习惯
  • 之前看到过日本的介绍,日本是靠左侧的。

####她的观点

  • 现在很多人都是靠右站立
  • 她见过写着靠右站立的提示

####事实

  • 左行右立(香港普遍称为左行右企)是指在歐美、香港、台灣、西日本等地於乘坐電扶梯时电梯左边空出留给有急事的乘客行走,只是站立在电梯上的人靠右侧站立的行为。與之相反的是,東日本、澳洲、新加坡等地卻採用右行左立的方式。
  • 此方式為一約定俗成之法則。香港地鐵(今港鐵)及台北捷運於從前曾參考歐美等地的習慣,宣傳「左行右立」,疏通繁忙時段的人流,但不奏效。在香港停止宣傳後,「左行右立」卻反而流行起來。
  • 但在電扶梯上步行會導致容易失去平衡,釀成意外,亦容易導致重力分布不均,增加電扶梯容易損壞的機率。[2]香港政府提醒市民盡量不要在電梯上走動[3],而台北捷運公司目前也基於上述之理由,不再宣傳此措施,然而大部分乘客均已養成左行右立的搭乘習慣;相較之下,台灣的另一座大眾運輸系統——高雄捷運即未若台北一般廣泛宣傳左行右立的習慣。
  • 2011年北京地铁动物园站出现电动扶梯事故后,京华时报指出左行右立是不安全的行为,可能是造成电动扶梯事故的诱因之一。
  • 左行右立是上海市迎接中国2010年上海世界博览会宣传倡导的城市文明行为之一。為宣傳“左行右立”标准,上海轨道交通扶手电梯处常见「左行右立」的標語,並指即使乘客站立不穩向後倒下,後面的乘客也可以支撐,不會出現骨牌效應,有利安全。[5]但自2012年12月开始,因为扶梯台阶过大,行走不安全,上海轨道交通不再强调左行右立,一些原先粘贴在扶梯上倡导左行右立的贴纸已被移除 以上事实部分参考维基百科 http://zh.wikipedia.org/wiki/%E5%B7%A6%E8%A1%8C%E5%8F%B3%E7%AB%8B 或者 Google 搜索 左行右立

####总结

  • 就按我输了作为结果吧,哈哈。
  • 靠左靠右并无硬性规定,社会活动中多礼让,不妨碍他人

###Others

Read Output From Shell

Python provides a lot of method to read output from a just executed shell. However many of them has been deprecated(Not recommened). But subprocess works at present compared to other methods.

1
2
3
4
5
6
7
8
9
10
11
12
from subprocess import Popen,PIPE,STDOUT
 
def readFromCommand(command) :
    p = Popen(command, shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT, close_fds=True)
    result = p.stdout.read().strip()
    return result
    
print readFromCommand('ls')
#result
#0001-.patch
#0001-.patch.zip
#0001-Replace-app_name-into-Browser.patch

A detailed description about subprocess has been written down here. http://docs.python.org/2/library/subprocess.html

###Others

How to Share Git Tag

Now I have two tags under my git repository. Let’s take a look at how to push the git tags to Server.

1
2
v2850
v4.1.1.2000_2852

###Push a single tag to the server

1
2
3
#git push origin tag_name
#Take v2850 for example
git push  origin v2850 

###Push all tags to the server

1
git push  origin --tags

###Others

Build Android Packages From Command Line

A few months ago,I dealed with a task:To build a large amount of apk files. The trick I came up with is to build apk file from the command so that I could use Python to glue all the works. Eventually I made it.And so this post is to make some description about the trick.
##Requirements

  • Setup JDK
  • Setup Android SDK ##Steps
    1. Generate R class file
    2. Compile Java codes(.java files) into classes(.class) files
    3. Convert .class files into .dex files
    4. Package Resouces
    5. Build Unsigned APK File
    6. Sign Apk with Jarsigner
    7. The Extra One:Use zipalign for optimization

###Generate R class File In Android,We use R class to refer resources instead of hard-coding the resouces.
For a better understanding,pleae have a look at http://www.satyakomatineni.com/akc/display?url=displaynoteimpurl&ownerUserId=satya&reportId=2883

1
aapt package -f -m -J /home/androidyue/temp/ubuntu/workspace/MxDataProvider/gen/ -S /home/androidyue/temp/ubuntu/workspace/MxDataProvider/res/ -I /home/androidyue/dev_tools/android-sdk-linux_86_backup/platforms/android-17/android.jar -M /home/androidyue/temp/ubuntu/workspace/MxDataProvider/AndroidManifest.xml

####Some descriptions

  • -f force overwrite of existing files
  • -m make package directories under location specified by -J
  • -J specify where to output R.java resource constant definitions
  • -S directory in which to find resources. Multiple directories will be scanned and the first match found (left to right) will take precedence.
  • -I add an existing package to base include set
  • -M specify full path to AndroidManifest.xml to include in zip

###Comiple .java into .class files

1
javac -encoding UTF-8 -source 1.6 -target 1.6 -bootclasspath /home/androidyue/dev_tools/android-sdk-linux_86_backup/platforms/android-17/android.jar -d /home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin/ /home/androidyue/temp/ubuntu/workspace/MxDataProvider/src//coop/channel/provider/*.java /home/androidyue/temp/ubuntu/workspace/MxDataProvider/gen//coop/channel/provider/R.java 

####Some descriptions

  • -encoding encoding Set the source file encoding name, such as EUC-JP and UTF-8. If -encoding is not specified, the platform default converter is used.
  • -source release Specifies the version of source code accepted, Please Do NOT use Java 7(1.7)
  • -target version Generate class files that target a specified version of the VM. Class files will run on the specified target and on later versions, but not on earlier versions of the VM. Valid targets are 1.1, 1.2, 1.3, 1.4, 1.5 (also 5), 1.6(also 6), and 1.7 (also 7).
  • -bootclasspath bootclasspath Cross-compile against the specified set of boot classes. As with the user class path, boot class path entries are separated by colons (:) and can be directories, JAR archives, or ZIP archives.
  • -d directory Set the destination directory for class files. The directory must already exist; javac will not create it. As I have suffered a lot using Java 7, It’s recomended to use Java 6

###Convert .class into .dex files

1
/home/androidyue/dev_tools/android-sdk-linux_86_backup/build-tools/17.0.0/dx --dex --output=/home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin//class.dex /home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin/

To dive into Dalvik, please visit http://source.android.com/devices/tech/dalvik/index.html

###Package Resouces

1
aapt package -f -M /home/androidyue/temp/ubuntu/workspace/MxDataProvider/AndroidManifest.xml -S /home/androidyue/temp/ubuntu/workspace/MxDataProvider/res/ -A /home/androidyue/temp/ubuntu/workspace/MxDataProvider//assets/ -I /home/androidyue/dev_tools/android-sdk-linux_86_backup/platforms/android-17/android.jar -F /home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin//resources.ap_

####Some descriptions

  • -F specify the apk file to output
  • -A additional directory in which to find raw asset files

###Build Unsigned APK File

1
/home/androidyue/temp/ubuntu/dev_tools/adt-bundle-linux_backup/sdk/tools/apkbuilder /tmp/unsignedApkFile.apk -v -u -z /home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin//resources.ap_ -f /home/androidyue/temp/ubuntu/workspace/MxDataProvider/bin//class.dex -rf /home/androidyue/temp/ubuntu/workspace/MxDataProvider/src/

####Some descriptions

  • -u Creates an unsigned package.
  • -z Followed by the path to a zip archive. Adds the content of the application package.
  • -f Followed by the path to a file. Adds the file to the application package.
  • -rf Followed by the path to a source folder. Adds the java resources found in that folder to the application package, while keeping their path relative to the source folder.

Sign Apk with Jarsigner

1
jarsigner -keystore /home/androidyue/temp/ubuntu/myKeystore -storepass storepassValue -keypass keypassValue -signedjar /home/androidyue/Desktop/output/max1111111.apk /tmp/unsignedApkFile.apk maxthon -digestalg SHA1 -sigalg MD5withRSA

####Some descriptions

  • [-keystore ] keystore location
  • [-storepass ] password for keystore integrity
  • [-keypass ] password for private key (if different)
  • [-signedjar ] name of signed JAR file
  • [-digestalg ] name of digest algorithm
  • [-sigalg ] name of signature algorithm

###Use Zipalign for optimization

1
zipalign -f -v 4 /home/androidyue/Desktop/output/max1111111.apk /home/androidyue/Desktop/output/max222222.apk

####Some descriptions

  • The is an integer that defines the byte-alignment boundaries. This must always be 4 (which provides 32-bit alignment) or else it effectively does nothing.
  • For more details, please visit http://developer.android.com/tools/help/zipalign.html

###Others

Start an Android App by ADB

To be more geek,I began to start an Android App by using adb.Thanks to Google.It’s possible and powerful.
The tool we use to make it is ADB(Android Debug Tool),For more detailed use please visit http://developer.android.com/tools/help/adb.html#shellcommands Actually What I use here is

1
start [options] <INTENT>

An example is followed.

1
2
3
4
#Here I want to start MxBrowser app by using ADB
17:04:30-androidyue/tmp$ adb shell am start -n com.mx.browser/com.mx.browser.MxBrowserActivity
Starting: Intent { cmp=com.mx.browser/.MxBrowserActivity  }
Warning: Activity not started, its current task has been brought to the front

the Package name(com.mx.browser) before the slash is provided to determine which app should the intent delivered to.The com.mx.browser.MxBrowserActivity is the destination Activity.
For more explanations about ADB INTENT,please visit http://developer.android.com/tools/help/adb.html#IntentSpec

Could Not Reliably Determine the Server's Fully Qualified Domain Name

An easy trick to solve problem:Could not reliably determine the server’s fully qualified domain name
Let’s take Linux for example.

1
2
#Modify your configuration file.
12:52:21-androidyue/etc/httpd/conf$ sudo vim httpd.conf 

Find out the following sentence

1
2
3
4
5
6
7
8
9
10
11
12
13
#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If this is not set to valid DNS name for your host, server-generated
# redirections will not work.  See also the UseCanonicalName directive.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
# You will have to access it by its address anyway, and this will make 
# redirections work in a sensible way.
#
#ServerName localhost:80

If the ServerName localhost:80 has been commented,it may cause above problem.A simple trick is just uncomment the sentence.Let it be like this

1
ServerName localhost:80

Hope you could resolve your problem,Guys.

Get MD5 Hash of Big Files

Recently I have been dealing with files and I need to get md5 hash of all kinds of files;Some are small and some are big.
For the small files I use this method to get md5 hash value.

1
2
3
4
5
6
7
def getFileMd5(filename):
    file = open(filename, 'rb') 
    m = md5()
    m.update(file.read())
    file.close()
    result =  m.hexdigest()
    return result

However for calculating md5 hash value of big files,the above method will be very Less Efficient.For Big files I use the following method(It’s acquired from stackoverflow)

1
2
3
4
5
6
7
8
9
10
def getBigFileMd5(filename, block_size=2**20):
    f = open(filename, 'rb')
    m = md5()
    while True:
        data = f.read(block_size)
        if not data:
            break
        m.update(data)
    f.close()
    return m.hexdigest()

And I did a test.The cost of Getting md5 hash of a Big file(size:10.7 GiB; 11,455,512,109 bytes) is 213.447s.And I think it’s OK.

How to Create a .db File in Python With Sqlite3

How to create a .db file in Python with sqlite3?Actually It’s really easy.
Run the following code

1
2
import sqlite3
sqlite3.connect('./test.db')

After running the above code,the test.db file will be created if it does not exist.
Of course,you have the proper permission to create the file.

Yum Install Build-essentials

I was once stucked in installing build-essentials which is for Debian/Ubuntu.I could not find any packages like build-essentials in yum.However this helped me out.

1
sudo yum install kernel-devel kernel-headers

Javac:command Not Found

I have been getting well on with my java tool.However When I use the javac command.It says

1
javac : command not found

It’s just because I just only install the JRE(Java Runtime Environment) and do not install JDK(Java Development KIt).I got the answer going for my fedora

1
sudo yum install java-devel

##Others

Manage Httpd Service

To Start

1
sudo /sbin/service httpd start

To Stop

1
sudo /sbin/service httpd stop

To Restart

1
sudo /sbin/service httpd restart

List Ports on Linux

I have often suffered this painful thing.When I start a service but the destination port is always used.So I should list all open ports and kill the occupied application.
So This is a short tip for how to list open ports on Linux

1
sudo netstat -tulpn

Note sometimes you should grant the command for a enough access.
For a better understanding,please

1
man netstat

or navigate to http://linux.about.com/od/commands/l/blcmdl8_netstat.htm

For Mac Users

1
sudo lsof -i -P | grep -i "listen"

###Others

How to Find Out What Linux Distribution You Are Using

How to known what Linux Distribution I am using now.
All right, run this command and you could get what you need in from the output result

1
2
3
4
5
6
7
8
9
10
11
19:12:17-androidyue~/github/octopress (master)$ cat /etc/*-release
Fedora release 17 (Beefy Miracle)
NAME=Fedora
VERSION="17 (Beefy Miracle)"
ID=fedora
VERSION_ID=17
PRETTY_NAME="Fedora 17 (Beefy Miracle)"
ANSI_COLOR="0;34"
CPE_NAME="cpe:/o:fedoraproject:fedora:17"
Fedora release 17 (Beefy Miracle)
Fedora release 17 (Beefy Miracle)

How to Delete Git Remote Branches

It’s easy to remove a certain remote branch in a Git respository. You could try this

1
git push origin --delete <branchName>

And of course another short command is waiting for You

1
git push origin :<branchName>

Accelerate Yum Installation

Yum is a great tool for manage dependencies and easy to use.However actually there is a way to speed up the installation.
Fastestmirror is a tool that will help yum determin what mirror is fastest and to use.
To install on Fedora 17

1
sudo yum install yum-fastestmirror

Of course you could customize the fastestmirror configuration easily

1
sudo vim /etc/yum/pluginconf.d/fastestmirror.conf

So when you next install a package from the network,I hope you could feel it much faster.

How to Sum the Number of Files or Folders in a Folder

I once had a request to sum the number of files in a specific folder.After googling I found the command wc could help me out.
wc - print newline, word, and byte counts for each file. And I think wc is short for Word Count.Please have a detailed information by visiting http://linux.about.com/library/cmd/blcmdl1_wc.htm if you have the desire.
Ok,Let’ get the total number of files(including folders) in a specific folder

1
20:56:08-androidyue/tmp$ ls | wc

Well,the following command will sum the number of pure files(Not including folders)

1
20:58:27-androidyue/tmp$ ll | grep ^- | wc

However how to sum the number of pure folder?The following command will help.

1
21:00:20-androidyue/tmp$ ll | grep ^d | wc

Install Git Daemon on Fedora

Git-daemon is A really simple server for git repositories.You can take a detailed understanding by reading https://www.kernel.org/pub/software/scm/git/docs/git-daemon.html .
I once often used git-daemon when I was using Ubuntu.But when I turn to Fedora and found it was not installed.And the output is like the following.

1
git: 'daemon' is not a git command. See 'git --help'. 

So I write this post to record How to install and enable git-daemon First Let’s intall the git Daemon:

1
sudo yum install git-daemon

However git daemon is off by default.We should enable it.We should do as the following.

1
vim  /etc/xinetd.d/git

And modify the file as the following configuration. # default: off # description: The git dæmon allows git repositories to be exported using
# the git:// protocol.

service git
{
            disable         = no
            socket_type     = stream
            wait            = no
            user            = nobody
            server          = /usr/libexec/git-core/git-daemon
            server_args     = --base-path=/var/lib/git --export-all --user-path=public_git --syslog --inetd --verbose
            log_on_failure  += USERID
            # xinetd does not enable IPv6 by default
            flags           = IPv6

}

And then,We use this command to start the git Daemon

1
git  daemon--export-all--base-path=your_local_git_path

Mostly we could clone the repositories from the git daemon.
But if you have enabled Firewall and other people want to clone the repository from your git daemon,please let the port 9418 available to the outsite.

BashBites:How to Append Outputs to a File

Is there any way to append output(debugging information) to a existings file? As we all know in bash the command ‘>’ will write ouputs to a file.However this command will also erase the existing data it the file.
Of course bash provides a command that appends outputs to a file.It resembles ‘>’.It’s ‘»‘
The following is a piece of example code. #####Let’s look at >

1
2
3
4
5
6
7
8
[androidyue@androidyue tmp]$ touch a.txt
[androidyue@androidyue tmp]$ cat a.txt 
[androidyue@androidyue tmp]$ echo "ABC" > a.txt 
[androidyue@androidyue tmp]$ cat a.txt 
ABC
[androidyue@androidyue tmp]$ echo "DEF" > a.txt 
[androidyue@androidyue tmp]$ cat a.txt 
DEF

#####Well,Let’s look at the exiciting command »

1
2
3
4
[androidyue@androidyue tmp]$ echo "GHI" >> a.txt 
[androidyue@androidyue tmp]$ cat a.txt 
DEF
GHI

Install Flash Plugin on Fedora 17

#####Addings

1
2
3
4
5
6
#For 32-bit System:
sudo rpm -ivh http://linuxdownload.adobe.com/adobe-release/adobe-release-i386-1.0-1.noarch.rpm
#For 64-bit System 
sudo rpm -ivh http://linuxdownload.adobe.com/adobe-release/adobe-release-x86_64-1.0-1.noarch.rpm
   
sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-adobe-linux

#####Updatings

1
sudo yum update

#####Installings

1
sudo yum install flash-plugin nspluginwrapper alsa-plugins-pulseaudio libcurl
Visit Comet Browser