Onliner of the Week (1)

Move all the pictures under folder to a new folder, using find and xargs

find . -name "*jpg" -print0 | xargs -0 -I {} mv {} ~/Dropbox/ifttt/
Advertisements
Tagged

JavaScript: Convert string into a XML object

Here is a small function in JavaScript, which converts a string into a XML object.

function StringToXML(text) {
    if (window.ActiveXObject) {
        var doc = new ActiveXObject('Microsoft.XMLDOM');
	doc.async = 'false';
	doc.loadXML(text);
    } else {
        var parser = new DOMParser();
	var doc = parser.parseFromString(text, 'text/xml')
    }
    return doc;
}
Tagged , ,

Process XML using regular expression with Python

I wrote a post about using xml.etree.ElementTree as a xml parser in Python. However, this cause some problem, because xml parser will automatically transfer the HTML escape characters in the xml into unicode characters, and unfortunately this is not what we expected… I haven’t fully understand the different encoding system right now, anyway here is a good post discussing about Unicode and HTML Escape characters in Python.

So I go to the straightforward method: I will use the regular expression (RE) to get the corresponding fields. Let’s look at the example xml file again, which has a crazy ssid attribute. The xml parser will transfer the HTML escape characters, e.g. Ü, into some unicode characters.

<configuration auth=”OPEN” encryption=”NONE” type=”wlan” ssid=”FIkUÜ{uo8wfS&lt;5&amp;MXMqgve&lt;lTG” hwaddr=”68:9f:f2:fd:da:e0″ allocation=”dhcp”/>

If just use RE, we just look for a string starts with ssid=”, and ends with  .


import re
xml_string = open('test.xml').read() #the whole xml file now is a string
match_ssid = re.search('ssid="([^"]+)', xml_string)
print match_ssid.group(1)
#Result: FIkUÜ{uo8wfS&lt;5&amp;MXMqgve&lt;lTG

The RE [^”]+ means any characters except for . RE is so powerful tool when dealing with strings, I will explore RE more in the future.

Tagged , , , ,

Process XML files with Python

In my testing work, I have to read and parse xml a lot, using Python. This post documents my usage of xml.etree.ElementTree package of Python, to Read and parse the information I need.

First of all, let’s look at a typical xml file: test.xml under current directory.

<xml><status>1</status><cmd>SETNETWORK</cmd><data><value><configuration type=”wlan” allocation=”dhcp” ssid=”shVLr1H5UTPkQYWhz’dSHIiuopg” auth=”OPEN” encryption=”NONE”/></value></data></xml>

We begin with import the ElementTree pacage and read the file.

from xml.etree import ElementTree as ET
xml = ET.parse('test.xml')
root = xml.getroot()

The parse() method just read the file in as a xml.etree.ElementTree.ElementTree object, and the getroot() method makes sure that you are at the root element of the XML tree.

You could also make a string into a xml object. The following code shows that and also shows how to read a file into a string using Python.


file_string = open('./test.xml').read()
root = ET.fromstring(file_string)

The next thing we need to do is to find the element we need, and read some attributes from it.

config = root.find('data').find('value').find('configuration')
attributes = config.attrib
print attributes['allocation']
# the output should be 'dhcp'

See, as simple as using the find() method to get the accurate element, and attrib variable to get all the attributes of a element. If there are more than one element, you could use findall() functions.

Edit html/css/javascript files with emacs!

Recently I need to develop some web widgets using html/javascript/css. Of course, I will use my favorite editor: Emacs.

The original emacs mode for html: html-mode and html-helper-mode are not good. They cannot tell the javascipt and css part of the html files a lot of time.

After google-ing these days, I found a very good mode for html editing. It is call multi-web-mode. You can download it like this:

cd ~/.emacs.d/
git clone https://github.com/fgallina/multi-web-mode.git

Then, add the following lines into your ~/.emacs:

(when
    (load
     (expand-file-name "~/.emacs.d/multi-web-mode/multi-web-mode.el"))
  (package-initialize))
(setq mweb-default-major-mode 'html-mode)
(setq mweb-tags '((php-mode "<\\?php\\|<\\? \\|")
		  (espresso-mode "]*>" "")
		  (css-mode "]*>" "")))
(setq mweb-filename-extensions '("php" "htm" "html" "ctp" "phtml" "php4" "php5"))
(multi-web-global-mode 1)

It should just work!

If you don’t have the esspreso-mode with you, which is the mode we use to edit javascript, you can download it from the following address and place it under ~/.emacs.d/

http://download-mirror.savannah.gnu.org/releases/espresso/espresso.el

and add the following to your ~/.emacs:

autoload #’espresso-mode “espresso” “Start espresso-mode” t)
(add-to-list ‘auto-mode-alist ‘(“\\.js$” . espresso-mode))
(add-to-list ‘auto-mode-alist ‘(“\\.json$” . espresso-mode))

Right now, everything should be fine. ::)

Enjoy your web front-end programming!

Tagged , , , , ,

Use emacs to write blogs in WordPress

This is a post send from emacs!

For the one who wants details about configuration:

  1. Install ELMA–A emacs lisp package management system
  2. Go to here to learn how to install a emacs package from ELMA, then, install weblogger.el
  3. Try to post a blog, like what I am doing!

Hello, World!

Convert a string to a list in Python

I use web.py as the framework of a website project. Recently I developed an iPhone app as the client of the website. The app use POST method to send accelerometer data to the website, and when the web.py receive the data:

class sensor:
    def POST(self):
    data = web.data()
    print data

And the data is like this:

[["accelerometer",1303884181462.097,0.1742249,-0.6092377,-0.7658691],["accelerometer",1303884181562.537,0.1266632,-0.5593719,-0.8358765],["accelerometer",1303884181663.125,0.1494141,-0.5847778,-0.7957916],["accelerometer",1303884181763.665,0.1268768,-0.572403,-0.8113861],["accelerometer",1303884181864.305,0.1513062,-0.5777588,-0.7967834],["accelerometer",1303884181964.956,0.1298676,-0.5714264,-0.8062439],["accelerometer",1303884182065.627,0.1447754,-0.5565643,-0.8131866],["accelerometer",1303884182166.559,0.1371918,-0.5825806,-0.7959442],["accelerometer",1303884182266.844,0.1352997,-0.5885773,-0.7918701],["accelerometer",1303884182367.435,0.1406708,-0.6017303,-0.7897797]]

However, when I thought it was a list and try to get data[i], I found that it was

data[0] = '['
data[1] = '['
data[2] = '"'

which means, it is a string, not a list.

I searched online for a solution to convert a string to a list, someone suggested we use string = string[1:-1].split(',') or something. It was a lot of dirty work for my example. Finally, I found an elegant solution for this problem:

list_data = eval(data)

Just as simple as this! Now I can type

for entries in list_data:
    print entries

and get

["accelerometer",1303884181462.097,0.1742249,-0.6092377,-0.7658691]

Of course, the entries itself now is a list, too! 😀

Tagged , ,

How to work on weekends

Recently I get tremendous things to finish, so I have to work on weekends. However, the more I would like to work, the less I could do. Normally I just wasted time here and there, and I could enjoy neither working nor playing.

Before I doing anything this weekend, I tell myself to think through. Here’s some rules I have at heart:

  1. Remember you are working. Quit your QQ, MSN, Gtalk, etc. Close your Facebook, Twitter, Renren, etc.
  2. Make a plan. A plan is like a map, guide you through different tasks you have to finish.
  3. Relax and enjoy working on the weekends! You are doing things on your own! You work because you have goals! Come on man, be passionate!
  4. Give yourself some treats. You can told yourself, I will go to a outdoor concert as long as I can finish the tasks.

Now I am about to work. Wish you all can have a WONDERFUL weekend!

Tagged ,

Re-think of my job hunting

Recent days, I got rejections from several famous investment bank. Some I didn’t pass the online exams, some I got phone interviews but I blew them.

I am upset by this fact. However, it is a good time to re-think what I have done, and to prepare for a better chance in the future:

  1. Make a better resume. I just tried to make the resume beautiful, however, what it contains is much more important than what it looks like.
  2. Prepare for all the basic (as well as most important!) knowledge. Late is better than never. Star preparing right now.
  3. Be mature. Mature means you are a grown man, you are the one others can trust with, you are the one others are willing to work with.
  4. Improve time management skill. There are HUGE amounts of things to do. You can waste time, if you want to regret. Spend your time wisely in building a better yourself.
  5. Don’t give up. Never lose your mind. Never lose your temper. Never lose your confidence. Believe you are smart enough and start trying again.
  6. Keep smiling. You are still you! You can lose when you can afford to lose! Keep running! Keep fighting!

If you are also looking for a job right now, I would like to share above thoughts with you, and to wish you a good luck.

Tagged

Hello world!

Hi all, I am erain. This is my English blog.

I am blogger in Chinese for more than 7 years and I can play around with Chinese words and sentences. Now I am trying to improve my English writing and this is the first step.

This blog will be mainly about:

  1. plan making and fighting with procrastinate;
  2. career path
  3. professional knowledge learning

Hope to know you and chat with you, online or face to face.

#include<iostream>
using namespace std;

int main()
{
cout<<"Hello World!"<<endl;
return 0;
}