Posts

Showing posts from December, 2012

MongoDB for Python Developers - online course

10genEducation is offering couple of free online course on mongodb for developers. One is going to start on January (2013) and another will start on February (2013). They are going to use Python for the January course. So if you are a Python programmer and interested in NoSQL (mongodob), just sign up here .

Concatenate Two Dictionaries | Python How To?

If you want to concatenate two dictionaries in Python (add a dictionary to another dictionary), there is a simple way to do it. Use the update() method which is much like the extend() function used to concatenate lists. >>> dict_1 = {1: 'a', 2: 'b', 3: 'c'} >>> dict_2 = {4: 'd', 5: 'e', 6: 'f'} >>> dict_1 {1: 'a', 2: 'b', 3: 'c'} >>> dict_2 {4: 'd', 5: 'e', 6: 'f'} >>> dict_1.update(dict_2) >>> dict_2 {4: 'd', 5: 'e', 6: 'f'} >>> dict_1 {1: 'a', 2: 'b', 3: 'c', 4: 'd', 5: 'e', 6: 'f'} >>> What is your other tricks for concatenating Python dictionaries?

simple web crawler / scraper tutorial using requests module in python

Let me show you how to use the Requests python module to write a simple web crawler / scraper. So, lets define our problem first. In this page: http://cpbook.subeen.com/p/blog-page_11.html, I am publishing some programming problems. So, now I shall write a script to get the links (url) of the problems. So, lets start. First make sure you can get the content of the page. For this write the following code: import requests def get_page(url):     r = requests.get(url)     print r.status_code     with open("test.html", "w") as fp:         fp.write(r.text)                 if __name__ == "__main__":     url = 'http://cpbook.subeen.com/p/blog-page_11.html'     get_page(url)         Now run the program: $ python cpbook_crawler.py 200 Traceback (most recent call last):   File "cpbook_crawler.py", line 15, in     get_page(url)          File "cpbook_crawler.py", line 10, in get_page     fp.write(r.text) UnicodeEncodeError: '

What does __name__ == "__main__" mean?

When you run a python script directly (example: $ python script.py), you want to set a starting point of the script. Python scripts are interpreted from the first line, then goes to the second line and so on ... import module def my_function():   # code here x = my_function() But you want to make your code more structured, so you come up with this: import module def my_function():   # code here def main():   x = my_function() # the program starts from here main() This is good, but problem is, if you import the script from another script (from module import *), the main() function gets executed, but you may not want to do this. You want to call the main() function only when this script is exclusively executed. And you can do this using __name__ == "__main__" . import module def my_function():    # code here def main():    x = my_function() # the program starts from here if __name__ == "__main__":    main() Thus you can make

set comprehension in python

As many of you are familiar with list comprehension in Python, let me inform you about set comprehension. You can create a set from a list or a dictionary. Example: #create set from list >>> s = set([1, 2, 3]) >>> s set([1, 2, 3]) #create set from dictionary (keys) >>> dt = {1: 10, 2: 20, 3: 30} >>> dt {1: 10, 2: 20, 3: 30} >>> type(dt) <type 'dict'> >>> s = set(dt) >>> s set([1, 2, 3]) Now we can create a set without using the set() function: (The feature is available in Python 3.x and Python 2.7) >>> s = {1, 2, 3, 'Bangladesh', 'python', 1.15} >>> type(s) <type 'set'> >>> And here is an example of set comprehension (similar to list comprehension): >>> s = { x for x in range(10) } >>> s set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])