>>> import json
>>>
>>> jsonString = """[
... {"firstName":"John", "lastName":"Doe"},
... {"firstName":"Anna", "lastName":"Smith"},
... {"firstName":"Angel", "lastName":"Cool"}
... ]"""
>>>
>>>
>>> print jsonString
[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Angel", "lastName":"Cool"}
]
>>>
>>> data = json.loads(jsonString)
>>>
>>> print data[2]['firstName']
Angel
>>> print data[2]['lastName']
Cool
>>>
>>> type(data)
<type 'list'>
>>>
>>>
>>> for person in data:
... print person['firstName'] + ' ' + person['lastName']
...
John Doe
Anna Smith
Angel Cool
>>>
>>> data.reverse()
>>>
>>> for person in data:
... print person['firstName'] + ' ' + person['lastName']
...
Angel Cool
Anna Smith
John Doe
>>>
Reversing strings:
>>> print 'Donald Trump'[::-1]
pmurT dlanoD
>>> print 'lion oil'[::-1]
lio noil
>>> print 'A car, a man, a maraca.'[::-1]
.acaram a ,nam a ,rac A
[ view entry ] ( 1247 views ) | print article
# install virutalenv
[aesteban@localhost ~]$ sudo pip install virtualenv
# create virtual environment
[aesteban@localhost ~]$ mkdir virt_env
[aesteban@localhost ~]$ virtualenv virt_env/virt1 --no-site-packages
New python executable in /home/aesteban/virt_env/virt1/bin/python
Installing setuptools, pip, wheel...done.
[aesteban@localhost ~]$
# load environment
[aesteban@localhost ~]$ source virt_env/virt1/bin/activate
(virt1) [aesteban@localhost ~]$
# deactivate environment
(virt1) [aesteban@localhost ~]$ deactivate
[aesteban@localhost ~]$
[aesteban@localhost ~]$
# listing installed packages with yolk
[aesteban@localhost ~]$ sudo pip install yolk
[aesteban@localhost ~]$ yolk -l
# installing yolk in our virutal environment
[aesteban@localhost ~]$ source virt_env/virt1/bin/activate
(virt1) [aesteban@localhost ~]$ pip install yolk
(virt1) [aesteban@localhost ~]$ yolk -l
# let's create another environment
(virt1) [aesteban@localhost ~]$ deactivate
[aesteban@localhost ~]$ virtualenv virt_env/virt2 --no-site-packages
# let's switch back to virt1 and install Pylons and SqlAlchemy
(virt1) [aesteban@localhost ~]$ pip install Pylons
...
(virt1) [aesteban@localhost ~]$ pip install SqlAlchemy
# compare virt1 and virt2 using: yolk -l
Big shout-out to:
http://www.simononsoftware.com/virtualenv-tutorial/
Thank you guys!
[ view entry ] ( 1268 views ) | print article
Looping through a dictionary (aka map):
Python 2.7.5
>>> map = {50+i:i for i in range(10)}
>>> map
{50: 0, 51: 1, 52: 2, 53: 3, 54: 4, 55: 5, 56: 6, 57: 7, 58: 8, 59: 9}
>>> for key, value in map.iteritems():
... print key,'-',value
...
50 - 0
51 - 1
52 - 2
53 - 3
54 - 4
55 - 5
56 - 6
57 - 7
58 - 8
59 - 9
>>>
Python 3.3.2
>>> map = {50+i:i for i in range(10)}
>>> map
{50: 0, 51: 1, 52: 2, 53: 3, 54: 4, 55: 5, 56: 6, 57: 7, 58: 8, 59: 9}
>>> for key, value in map.items():
... print(key,' - ',value)
...
50 - 0
51 - 1
52 - 2
53 - 3
54 - 4
55 - 5
56 - 6
57 - 7
58 - 8
59 - 9
>>>
Fetching url data:
Python 2
[aesteban@localhost corpemployees-filtered]$ python
Python 2.7.5 (default, Apr 10 2015, 08:09:05)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import urllib
>>> urllib.urlopen('https://graph.facebook.com/?id=http://www.barney.com').read()
'{"id":"http:\\/\\/www.barney.com","shares":792}'
Python 3
[aesteban@localhost corpemployees-filtered]$ python3
Python 3.3.2 (default, Dec 4 2014, 12:49:00)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import urllib.request
>>>
>>> urllib.request.urlopen('https://graph.facebook.com/?id=http://www.barney.com').read()
b'{"id":"http:\\/\\/www.barney.com","shares":792}'
[ view entry ] ( 1552 views ) | print article
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |