Wednesday, March 12, 2014

Install and Run Python in Aptana




Aptana studio is open-source web development IDE, can be downloaded from:
 http://aptana.com/
When you create a Pydev project, you may find python interpreter not found.
1) To install  python interpreter:
http://www.python.org/downloads/
2)Then start Aptana, go to Aptana window menu, select preferences, select interpreter - python
under Pydev in left menu, then go to right menu, click new, browse python.ext for Python executable, for example C:\python33\python.exe
click apply
3) restart Aptana,
Click new Pydev project using python interpreter we just created
 create new python file, example1.py
print("Hello, World!")
click run,  in the first time, there may popup one dialog, select python -run in first choice
Video: Install and Run Python in Aptana




Tuesday, March 11, 2014

Load json data and json url in Python 3.3



Load json data in Python 3.3 a little from Python 2.7
Here I gave two examples using Json data from an array and from url.
Example1.py: load json data from an array in Python3.3

 import json
data = [ { 'a':'A', 'b':(2, 4), 'c':3.0 } ]
data_string = json.dumps(data)
print(data_string)

data_load = json.loads(data_string)
print(data_load)
print(data_load[0]['a'])

return:
 [{"a": "A", "b": [2, 4], "c": 3.0}]
[{'a': 'A', 'c': 3.0, 'b': [2, 4]}]
A

Reference:
http://pymotw.com/2/json/
Example2.py, load json data from url (earthquake data)
#http://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php
#past 30days M4.5 earthquake
#http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_month.geojson

import urllib.request
import json

geourl = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/4.5_month.geojson"
response = urllib.request.urlopen(geourl)
content = response.read()
data = json.loads(content.decode("utf8"))
print(data)
print(data['type'])


Video:Load json data in Python 3.3


 

Monday, March 10, 2014

Fork or clone a repository in Gist or Github




GitHub is a web-based hosting service using
 the Git revision control system
https://github.com/

Gist is a simple way to share snippets and pastes with others
https://gist.github.com/

For the following examples, first sign in my Github account

Example 1   fork  Gist repository in other users:
Go to the  gist I want to fork, example
 https://gist.github.com/jrmoran/3938567
at right top, click  fork
Now it is cloned to my account
https://gist.github.com/jiansenlu/9443876
Now in gist, click user name still in gist. Switch back to Github,
click github  at the top right menu,
now back to github, click use name, now you can see you gitnub repository, not gist,
 https://github.com/jiansenlu

Example 2:  fork a github (not gist) repository, for example
https://github.com/segmentio/metalsmith
click fork at right top, then it will be shown in my github (in gist)
https://github.com/jiansenlu/metalsmith
Note: it is shown  under repositories, not contribution
Video: Fork or clone a repository in Gist or Github

Saturday, March 1, 2014

JSON Examples in PHP



JSON: JavaScript Object Notation.  
JSON is smaller than XML and easy to parse.
JSON format:
a name followed by colon, followed by a value, such as
"fruit":"apple"
i.e fruit = "apple"
JSON object using curly bracket
var  temp={"fruit":"apple", "juice":"orange"}
To access JSON object value:
temp.fruit, temp.juice
JSON array using square bracket:
var JSONarray= {
"food":[
{"fruit":"apple", "juice":"orange"},
{"fruit":"banana", "juice":"watermelon"}
]
}


To access the value  apple in array
JSONarray.food[0].fruit
Example 1: convert.php - Convert PHP associative array to javascript associative array

 <?php
 //Example 1: Convert PHP associative array to javascript associative array
$arr = array('price' => 2, 'high' => 3, 'low' => 1);
echo json_encode($arr);
?>
<script>
    var quote=<?php echo json_encode($arr); ?>;
    alert(quote["price"]);
    alert(quote.price);
</script>

Example 2: jQuery Ajax Jason example in PHP form
test.php
<html>
    <head>
       <title>jQuery Ajax Jason example</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
     <script type="text/javascript">
     $(document).ready(function() {

            $("#submit").click(function(){

            $.ajax({
            url: "post.php",
            type: "POST",
            data: {
                message: $("#message").val(),
                firstName: $("#firstName").val(),
                lastName: $("#lastName").val(),
                email: $("#email").val()
            },
            dataType: "JSON",
            success: function (jsonStr) {
                $("#result").text(JSON.stringify(jsonStr));
            }
        });

    });

    });
    </script>
    </head>
    <body>
    <h1>jQuery Ajax Jason example</h1>
    <div id="result"></div>
        <form name="contact" id="contact" method="post">
         Message : <textarea name="message" id="message"></textarea><br/>
         firstName : <input type="text" name="firstName" id="firstName"/><br/>
         lastName : <input type="text" name="lastName" id="lastName"/><br/>
         email : <input type="text" name="email" id="email"/><br/>
        <input type="button" value="Submit" name="submit" id="submit"/>
        </form>

    </body>
</html>

post.php
<?php
    $message      = $_POST["message"];
    $firstName   = $_POST["firstName"];
    $lastName    = $_POST["lastName"];
    $email       = $_POST["email"];
    if(isset($message)){
        $data = array(
            "User message"     => $message,
            "User firstName"  => $firstName,
            "User lastName"   => $lastName,
            "User email"      => $email
        );
        echo json_encode($data);
    }
?>
Video: JSON Examples in PHP