No module named Crypto.PublicKey

When I tried to import the paramiko module, I got following error:

No module named Crypto.PublicKey

That means we need to install pycrypto library. Run following command to install pycrypto library by using pip:

In the installation process I got a another error message:

error: Unable to find vcvarsall.bat

That means we need a C compiler to compile this extension. But we can also download a pre-built extension for windows: http://www.voidspace.org.uk/python/modules.shtml#pycrypto

 

After installing this extension and tried to import “paramiko” module, I got the same error “No module named Crypto.PublicKey” again. This is because the installer installs pycrypto library with lowercase name, but paramiko tries to import it with uppercase name. We should rename crypto directory under “Lib/site-packages” to Crypto, then importing paramiko will work.

Drupal user properties (status, hostname, etc)

In Drupal, user_load($uid) function will return an user object. And Drupal has defined a global user object as well (it represents current logged in user).

The user object has following properties:

status -> active/blocked
uid -> user id
mail -> email address
created -> created timestamp
login -> login timestamp
hostname -> ip address
init -> the email address provided at initial registration
roles -> roles assigned to this user

The difference between “mail” property and “init” property is that mail can be changed after registration, but init is the first used email address and cannot be changed. If you want to send email to an user, you need to know his email address ($user->mail).

Unable to find vcvarsall.bat when installing gmpy using pip

GMPY is a multiple-precision arithmetic module, which is a extension module written by C. I tried to install it by using pip, then I got following error message:

Unable to find vcvarsall.bat

It’s because it’s trying to find a c compiler (Visual C++ or MinGW) to compile this extension but failed. To fix this error, one method is to install a C compiler (Visual C++ or MinGW). And another easier way is to download an windows installer: https://code.google.com/p/gmpy/downloads/list

Drupal search without reindex

In Drupal, if you want to let your published node appear in search results, you need to re-index the search first (Administration->Configuration->Search Settings). And the Drupal cron job file (cron.php) will be executed to re-index the site at a specific time on every minute, every hour or every day, etc.

But recently I’m building a project outsourcing site, I need the newly posted project can be searched by other users immediately after it’s published. I searched on Google and Drupal for a while, finally I found this module: Auto-Index.

This module will automatically re-index the nodes every time a new node is created or an existing node is edited. And there is no more configuration needed after installing.

Qt QWidget add menu bar

I want to add menu bar to my window (which is a QWidget instance), I searched on the Google and found that most articles are recommending to use QMainWindow instead of QWidget. Using QMainWindow is a good choice, but in my program the main window is based on QWidget and the code base is huge, so adding menu bar to QWidget is a better choice here.

First we need create a window (QWidget) and set its layout (we use QVBoxLayout here)

Then create a menu bar and add it to the VBox layout.

Now you will see a blank menu bar in your window

Next we need add some menus to our menu bar

addMenu function will take the parameter as menu name and return the menu as return value. We need keep the menu instances because we need add actions (menu items) to them.

In above code we create a QAction (menu item) with title “Exit”, and connect its “triggered” signal to exit function (sys.exit), it means when this menu item is clicked, the exit function will be called. Finally add this menu item to the “File” menu.

 

We can also connect QAction’s triggered signal to custom defined function

Here we add a “Redo” menu item under “Edit” menu. When user clicks “Redo”, a message box will be popped up saying “Redo will be performed”

 

Following is the complete code:

 

remove horizontal header of QTableView

QTableView will show the horizontal header and vertical header by default.

Now I want to use QTableView as a file list to show the files changed recently (I want to list more than one column, so QTableView is a better choice than QListView). we can use following code to remove the horizontal header:

PySide

c++

horizontalHeader() method will return the horizontal header which is an instance of QHeaderView class. Then call the hide method of QHeaderView class to hide the header.

QHeaderView class is inheriting QWidget class, so we can use hide method to make it invisible and use show method to make it visible. You can view the QWidget documentation here