Category Archives: Uncategorized

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 , , , , ,

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;
}