Giới thiệu Virtual Environment trong Python

x-post from kipalog.com Virtual Environments là gì và dùng để làm gì? Virtual Environment dịch nôm na là môi trường ảo :D Cũng giống như máy ảo (Virtual Machine), Virtual Environment thiết lập một môi trường ảo, cho phép bạn nghịch ngợm lung tung với các packages của Python mà không làm ảnh hưởng đến những packages đã được cài đặt sẵn trên Python. Ví dụ bạn muốn thử nghiệm với Django 1.8 trong khi trên hệ thống đang cài đặt Django 1.4 LTS. Cũng giống như việc bạn dùng Virtual Machine để thử nghiệm phiên bản Chrome beta mới nhất mà không muốn làm ảnh hưởng đến phiên bản đang có trên máy. Công cụ tạo ra Virtual Environment trên Python là virtualenv. virtualenv tạo ra một thư mục chứa tất cả những thứ cần thiết (executables, libraries). Sử dụng Virtual Environment như thế nào? Trước hết để sử dụng được Virtual Environment thì bạn phải cài virtualenv cái đã. $ pip install virtualenv Tạo Virtual Environment $ virtualenv [project_name] virtualenv sẽ tạo ra một thư mục có tên là [project_name] chứa tất cả những gì cần thiết. Hãy thử xem nội dung của thư mục này: ./lib ./lib/python2.6 ./lib/python2.6/site-packages ./lib/python2.6/distutils ./include ./bin Nếu trên server có nhiều phiên bản Python (2.x, 3.x) bạn hoàn toàn có thể khởi tạo Virtual Environment với một phiên bản chỉ định: virtualenv -p /usr/bin/python2.7 [project_name] Hoặc bạn có thể tạo một Virtual Environment mà không có các packages đã được cài đặt sẵn (trong trường hợp bạn muốn làm mọi thứ từ đầu: virtualenv –no-site-packages [project_name] Sử dụng Virtual Environment Khởi động Virtual Environment bằng câu lệnh: $ source [project_name]/bin/activate Tên của Virtual Environment sẽ xuất hiện ở phía trước command prompt, cho ta thấy Python đang sử dụng Virtual Environment. Từ

WordPress Permalink Settings on Windows Hosting

My 2 cents: You should not host WordPress (or any script other than ASP, .NET) on Windows hosting. Try Linux. You will not be disappointed. In case you still want to use Windows hosting for your WordPress, here is a piece of code that will save you lots of time trying to figure out how to update Permalink Settings on WordPress. You will probably got this message: You should update your web.config now. Open FTP and put the following into web.config file (create a new one if you can’t see it) <?xml version=”1.0″ encoding=”UTF-8″?> <configuration> <system.webServer> <rewrite> <rules> <rule name=”wordpress” patternSyntax=”Wildcard”> <match url=”*”/> <conditions> <add input=”{REQUEST_FILENAME}” matchType=”IsFile” negate=”true”/> <add input=”{REQUEST_FILENAME}” matchType=”IsDirectory” negate=”true”/> </conditions> <action type=”Rewrite” url=”index.php”/> </rule></rules> </rewrite> </system.webServer> </configuration> That’s it. You’re welcome.

New API: Expand URL

Today, the Internet is flooded with shortened URL. All links shared on Twitter are shortened (start with t.co). There is nothing wrong with it. I just don’t feel comfortable clicking a shortened URL without knowing where it will bring me to. How paranoid I am! There are several reason I don’t trust shortened URL: They might contain tracking or referral code. SlickDeals heavily uses it (to make money, of course). Again, there is nothing wrong with it. They provide you a service and they deserve to get some money out of it. They might hide malicious URL, like XSS I hate them! There are several expand URL services out there, however they just support a very limited set of URL shorten service. What I want is something more. I want to be able to track the final URL after a bunch of redirections. So I wrote this API: http://api.codepie.org/expand_url/<your_url_here_no_escape_needed> For example: http://api.codepie.org/expand_url/http://slickdeals.net/?pno=128958&lno=1&afsrc=1&trd=Newegg+com Here is the API result. You will be amazed how many hops it take to the final destination: http://slickdeals.net/?pno=128958&lno=1&afsrc=1&trd=Newegg+com http://www.jdoqocy.com/click-1225267-10440897?sid=1fea0918ac5e454fa2fc89be4e70d43b&url=http%3A%2F%2Fwww.newegg.com%2FProduct%2FProduct.aspx%3Fsdtid%3D7164970%26SID%3D1fea0918ac5e454fa2fc89be4e70d43b%26AID%3D10440897%26PID%3D1225267%26nm_mc%3DAFC-C8Junction%26cm_mmc%3DAFC-C8Junction-_-cables-_-na-_-na%26Item%3D03V-003G-00433 http://cj.dotomi.com/a6106ar-zH/ry2/GFJJFNOM/GHHKHLM/F/F/F?c=v5vq%3DEsrnDMELnpIrHIHsnFspLMorHrKDqHGo%2674y%3Du662%25GN%25FS%25FS999.0r9rtt.p1z%25FSc41q7p6%25FSc41q7p6.n52A%25GS5q6vq%25GQKEJHMKD%25FJfVQ%25GQEsrnDMELnpIrHIHsnFspLMorHrKDqHGo%25FJNVQ%25GQEDHHDLMK%25FJcVQ%25GQEFFIFJK%25FJ0z_zp%25GQNSP-PLW70p6v10%25FJpz_zzp%25GQNSP-PLW70p6v10-_-pnoyr5-_-0n-_-0n%25FJV6rz%25GQDGi-DDGT-DDHGG%3C%3Cu662%3A%2F%2F999.wq131pB.p1z%3ALD%2Fpyvpx-EFFIFJK-EDHHDLMK%3C%3CT%3Cu6625%3A%2F%2F999.t11tyr.p1z%2F%3C http://www.emjcd.com/h4102tenm6/elq/32662AB9/3447489/2/39292468268596B574/87Ad4g9d4ggd33g6A24f3A25952f57g5?c=zC2x%3DLzyuKTLSuwPyOPOzuMzwSTvyOyRKxONv%26EB5%3D1DD9%25NU%25MZ%25MZGGG.7yGy00.w86%25MZjB8xEwD%25MZjB8xEwD.uC9H%25NZCxD2x%25NXRLQOTRK%25MQmcX%25NXLzyuKTLSuwPyOPOzuMzwSTvyOyRKxONv%25MQUcX%25NXLKOOKSTR%25MQjcX%25NXLMMPMQR%25MQ76_6w%25NXUZW-WSdE7wD287%25MQw6_66w%25NXUZW-WSdE7wD287-_-wuv5yC-_-7u-_-7u%25MQcDy6%25NXKNp-KKNa-KKONN%3Cw38!GQ96-PAKTEzy%3C1DD9%3A%2F%2FGGG.3x8A8wI.w86%3ASK%2Fw52w4-LMMPMQR-LKOOKSTR%3C%3Ca%3C1DD9C%3A%2F%2FGGG.08805y.w86%2F%3C http://www.newegg.com/Common/AdvancedLinkLanding.aspx?nm_mc=AFC-C8Junction&cm_mmc=AFC-C8Junction-_-na-_-na-_-na&AID=10440897&PID=1225267&SID=1fea0918ac5e454fa2fc89be4e70d43b&URL=http%3A%2F%2Fwww.newegg.com%2FProduct%2FProduct.aspx%3Fsdtid%3D7164970%26SID%3D1fea0918ac5e454fa2fc89be4e70d43b%26AID%3D10440897%26PID%3D1225267%26nm_mc%3DAFC-C8Junction%26cm_mmc%3DAFC-C8Junction-_-cables-_-na-_-na%26Item%3D03V-003G-00433 http://www.newegg.com/Product/Product.aspx?sdtid=7164970&SID=1fea0918ac5e454fa2fc89be4e70d43b&AID=10440897&PID=1225267&nm_mc=AFC-C8Junction&cm_mmc=AFC-C8Junction-_-cables-_-na-_-na&Item=03V-003G-00433&cm_sp= Suggestions welcome!

Free course: The Complete iOS 7 Course – Learn by Building 14 Apps (was $499)

I always want to learn how use build an iOS app, and I have some idea in mind. However, I’m just lazy and procrastinating to do so. Today I found a free course (via Slickdeal) to build an iOS app, and already registered (and you should). It’s free (was $499). Link: http://bitfountain.io/course/the-complete-ios-7-course-learn-by-building-14-apps/?couponCode=COUPON111677

Markdown is available for self-hosted WordPress through Jetpack

Jay! I’ve just noticed that. It’s funny when searching for Markdown on WordPress return this article: Write (More) Effortlessly With Markdown Basically, they mentioned that Markdown is only available for WordPress.com blog, by a simple switch in configuration. I am skeptical, so I always try to use “official” version of plugins. Yes, there are many Markdown plugins out there http://wordpress.org/plugins/tags/markdown When I check my Jetpack version, it’s already there! So if you want to write in Markdown style (like Github and StackOverflow), just enable it (why not?).

Today I Leaned (the hard way): The difference between dict and list and Python

Lookup time for dict() is O(1) and for list() is O(n) no matter what the number of elements is. Given a list of elements in a file, this piece of code: from itertools import izip lists = map(str.strip, open(‘dict.txt’).readlines()) i = iter(lists) j = range(len(lists)) dicts = dict(izip(i, j)) index = dicts[w] is n time faster than this piece of code: dicts = map(str.strip, open(‘dict.txt’).readlines()) index = dicts.index(w) I learned that the hard way. My dict has 5,000,000 elements, which mean my code will run 5 millions time slower than it should be. As such, I ran my code on 20 machines, each with 64 cores and it took ~10 hours without finish. Changing from list to dict, it took about 900 seconds on a single machine (of course 64 cores) Next time, when you want to do lookup in Python, use dict(). Happy coding!

Thử một bài viết có rất nhiều tiếng Việt

Bài viết này lẽ ra đã không được publish. Mục đích của mình chỉ là kiểm tra xem WordPress 3.4 xử lý tiếng Việt trong Permalink như thế nào. Trước 3.4, WordPress xử lý việc này không tốt, cụ thể là với tiêu đề như trên, WordPress sẽ xử lý thành “thử-một-bai-viết-co-rất-nhiều-tiếng-việt”. WordPress 3.4 đã xử lý việc này rất tốt, URL trở thành “thu-mot-bai-viet-co-rat-nhieu-tieng-viet”. Dưới đây là đoạn code được mình viết và đặt trong file wp-includes/formatting.php để xử lý tiếng Việt trong Permalink. WordPress 3.4 cũng có một đoạn xử lý tương tự nhưng đã tối ưu và loại bỏ các ký tự trùng với các phần sẵn có trong WordPress. [code] // Decompositions for Vietnamese chr(65) => ‘A’,chr(97) => ‘a’, chr(196).chr(130) => ‘A’,chr(196).chr(131) => ‘a’, chr(195).chr(130) => ‘A’,chr(195).chr(162) => ‘a’, chr(195).chr(128) => ‘A’,chr(195).chr(160) => ‘a’, chr(225).chr(186).chr(176) => ‘A’,chr(225).chr(186).chr(177) => ‘a’, chr(225).chr(186).chr(166) => ‘A’,chr(225).chr(186).chr(167) => ‘a’, chr(195).chr(129) => ‘A’,chr(195).chr(161) => ‘a’, chr(225).chr(186).chr(174) => ‘A’,chr(225).chr(186).chr(175) => ‘a’, chr(225).chr(186).chr(164) => ‘A’,chr(225).chr(186).chr(165) => ‘a’, chr(225).chr(186).chr(162) => ‘A’,chr(225).chr(186).chr(163) => ‘a’, chr(225).chr(186).chr(178) => ‘A’,chr(225).chr(186).chr(179) => ‘a’, chr(225).chr(186).chr(168) => ‘A’,chr(225).chr(186).chr(169) => ‘a’, chr(195).chr(131) => ‘A’,chr(195).chr(163) => ‘a’, chr(225).chr(186).chr(180) => ‘A’,chr(225).chr(186).chr(181) => ‘a’, chr(225).chr(186).chr(170) => ‘A’,chr(225).chr(186).chr(171) => ‘a’, chr(225).chr(186).chr(160) => ‘A’,chr(225).chr(186).chr(161) => ‘a’, chr(225).chr(186).chr(182) => ‘A’,chr(225).chr(186).chr(183) => ‘a’, chr(225).chr(186).chr(172) => ‘A’,chr(225).chr(186).chr(173) => ‘a’, chr(69) => ‘E’,chr(101) => ‘e’, chr(195).chr(138) => ‘E’,chr(195).chr(170) => ‘e’, chr(195).chr(136) => ‘E’,chr(195).chr(168) => ‘e’, chr(225).chr(187).chr(128) => ‘E’,chr(225).chr(187).chr(129) => ‘e’, chr(195).chr(137) => ‘E’,chr(195).chr(169) => ‘e’, chr(225).chr(186).chr(190) => ‘E’,chr(225).chr(186).chr(191) => ‘e’, chr(225).chr(186).chr(186) => ‘E’,chr(225).chr(186).chr(187) => ‘e’, chr(225).chr(187).chr(130) => ‘E’,chr(225).chr(187).chr(131) => ‘e’, chr(225).chr(186).chr(188) => ‘E’,chr(225).chr(186).chr(189) => ‘e’, chr(225).chr(187).chr(132) => ‘E’,chr(225).chr(187).chr(133) => ‘e’, chr(225).chr(186).chr(184) => ‘E’,chr(225).chr(186).chr(185) => ‘e’, chr(225).chr(187).chr(134) => ‘E’,chr(225).chr(187).chr(135) => ‘e’, chr(73) => ‘I’,chr(105) => ‘i’, chr(195).chr(140) => ‘I’,chr(195).chr(172) => ‘i’, chr(195).chr(141) => ‘I’,chr(195).chr(173) => ‘i’, chr(225).chr(187).chr(136) => ‘I’,chr(225).chr(187).chr(137) => ‘i’, chr(225).chr(187).chr(138) => ‘I’,chr(225).chr(187).chr(139) => ‘i’, chr(79) => ‘O’,chr(111) => ‘o’, chr(195).chr(148) =>

How to disable comment box from Facebook like widget

It have been a long time since the last time I used Facebook like widget on my blog. At that time, I feel that Facebook scripts take a lot of time to load, hence slowdown my site and affect its performance. Today, I changed my mind and add it back. With just 2 pieces of code (one load all.js at bottom and one load the like button), Facebook like button appears again on my site. Sweet and simple! But wait. When I test by click on Like button, an ugly comment box appear: As this is unexpected, I want to disable it. But Facebook Developer page does not give me any information about this. Fortunately, with just one simple CSS trick, I can hide that ugly comment box so the Facebook like become beautiful (like it should) .fb_edge_widget_with_comment span.fb_edge_comment_widget iframe.fb_ltr { display: none !important; } Happy hacking!

How to format bytes to kilobytes, megabytes, gigabytes in PHP

This is the best function I found on StackOverflow: function formatBytes($bytes, $precision = 2) { $units = array(‘B’, ‘KB’, ‘MB’, ‘GB’, ‘TB’); $bytes = max($bytes, 0); $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); $pow = min($pow, count($units) – 1); // Uncomment one of the following alternatives // If 1 kilobytes = 1024 bytes // $bytes /= pow(1024, $pow); // If 1 kilobytes = 1000 bytes // $bytes /= (1 < < (10 * $pow)); return round($bytes, $precision) . ' ' . $units[$pow]; }

CSS: How to add width and height attributes to anchor tag

You might face this problem sometimes: You want to add width and height to an anchor instead of giving these attributes to its container. Surprisingly, width and height attribute are not works. For example, consider the following codes: a.ansok { width:86px; height:41px; line-height:41px; background-image: url(../images/layout/button.png); } And here is what you will get (instead of beautiful buttons): So, what is the reason and how to fix it? Well, anchor tags are not block level HTML elements but rather inline elements. So what is the difference between the two? Here is the definition of the two items: Inline Element definition: Inline elements typically may only contain text and other inline elements. When rendered visually, inline elements do not usually begin on a new line. Block Element: Block-level elements typically contain inline elements and other block-level elements. When rendered visually, block-level elements usually begin on a new line. So, all you have to do is just add display:block to CSS class of anchor. And here is the result: Some useful links: HTML inline elements HTML Block Elements