Build a Python function help Query Tool

Source: Internet
Author: User

For example, when learning modules such as list, tuple, dict, str, OS, and sys, you can quickly and comprehensively learn the processing functions using the Python documentation. Therefore, this built-in document function provides great convenience for scholars, so it is better to conduct brief development.

However, when you leave CMD and want to use IDLE or Komodo Edit or other software for "segment programming", it will be a little stretched. For example, the wx library is very large and there are as many as 10 MB help files. If you open it in CMD, you can think about how much time you need to view the help information you want. After you get familiar with Python APIs on a large scale, you will find that this is not as useful as you think. Continuously press any key to flip the page, and the page that can be accommodated in the page is limited. So it is very inconvenient.

There are two solutions.

 First, use the Module Docs Tool

This tool is a method that comes with Python gui idle to query help files. You can use the form of web pages and local functions of the local machine to provide a simulated online query method. It can open a webpage with all functions displayed and standardized classification. It is clear, but it is inconvenient to use. After all, there is no search function. If you save the link of the webpage content, it cannot be used continuously. So there is a big problem. At the same time, the generated webpage file is huge, and the memory is too small, it will put pressure on the system. Therefore, this method is not a convenient method.

Second, develop your own tools

In fact, I do not want to develop tools myself, because it takes time, days or weeks to say less and more, in addition, the pressure on your own heart is not small. After all, you need to be distracted from doing this. I have not found the wxPython API for a long time on the Internet. For wx, I found an English document on wxPython API. The description above is very vague and fuzzy, the functions and specific parameters are listed directly. How to use them is rarely mentioned, and the styles of many controls are not listed in detail. So it is not easy to use. If the name is forgotten, you can check the complete word and detailed parameter list. Other functions are rarely involved.

In view of this, I decided to make a small tool by myself, and spend a small amount of time to conveniently query functions of various functions and modules. Only one available version is provided here. Open source code is provided to everyone. The Code style and control design can be imitated by beginners. I'm all ears. Later versions will also be released here, which may be encapsulated and then released. Now the source code file is released. As you all know, you can double-click the Python source code file to execute it.

Copy codeThe Code is as follows: # coding = UTF-8
# Function Introduction: This software was initially used only for module and function usage queries for quick display.
# Extended function: You can save successfully queried results to a local device,
# Save the comments of some functions in Chinese.
# Put the saved keywords in the list on the right
# Deep Scaling: saves results using databases and provides interfaces for adding, deleting, querying, and modifying results

From Tkinter import *
From StringIO import StringIO
From tkSimpleDialog import *
Import sys
Import Pmw
Import ConfigParser
Import OS
Import wx

Class Finder (Frame ):

Def OnFind (self ):
# Execute and obtain the result
Info = self. inputStr. get ()
If len (info) = 0:
Return True
Buff = StringIO ()
Temp = sys. stdout # Save standard I/O streams
Sys. stdout = buff # redirect the standard I/O stream to the buff object
Self. text. delete (1.0, END)
Try:
Fmt = 'help ('+ info + ')'
Result = eval (fmt)
Self. text. insert (1.0, buff. getvalue ())
Self. savebtn. config (state = NORMAL)
Except t:
Try:
_ Import _ (info)

Fmt = 'help ('+ self. inputStr. get () + ')'
Result = eval (fmt)
Self. text. insert (1.0, buff. getvalue ())
Except t:
Self. text. insert (1.0, "ERROR .")
Sys. stdout = temp # restore standard I/O Stream buff. getvaue ()
Self. helpbtn. config (state = NORMAL)

Def save (self ):
# Search. If not found, save it. Use the INI file to save the data.
# Save the original
Tofind = self. inputStr. get ()
If len (tofind) = 0:
Return
Filename ='s _ '+ tofind +'. ini'
Fout = open (filename, 'w ')
Fout. write (self. text. get (1.0, END). encode ('utf-8 '))
Fout. close ()

Self. items. append (tofind)
Self. items. sort ()
Self. config. add_section (tofind)
Self. config. write (open ('data. ini ', 'r + '))

Nindex = self. items. index (tofind)
Self. box. delete (0, END)
Self. box. insert (0, * self. items)
Self. box. see (nindex)
Self. box. selection_set (nindex)

Self. savebtn. config (state = DISABLED)

Def saveas (self ):
# Save changes
Index = self. box. curselection ()
If index <0:
Return
Tofind = self. box. get (index)
If len (tofind) = 0:
Return
Strinfo = self. text. get (1.0, END)

Filename ='s _ '+ tofind +'. ini'
Fout = open (filename, 'w ')
Fout. write (strinfo. encode ("UTF-8 "))
Fout. close ()

Self. saveasbtn. config (state = DISABLED)

Def _ init _ (self ):
Frame. _ init _ (self)
Self. option_add ('* font', 'verdana 12 bold ')
Self. pack (expand = YES, fill = BOTH)
Self. master. title (u'python function queryer ')
Self. master. iconname ("calc1 ")

# List on the left, place saved entries in alphabetical order
InfoF = Frame (self)
InfoF. pack (side = LEFT, expand = NO, fill = BOTH)

ListF = Frame (infoF)
ListF. pack (side = TOP, expand = YES, fill = BOTH)

# Retrieving Projects
Self. config = ConfigParser. ConfigParser ()
Self. config. read ('data. ini ')
Self. items = self. config. sections ()
Self. items. sort ()
Self. box = Listbox (listF, width = 15, selectmode = SINGLE)
Self. box. insert (0, * self. items)
Self. box. bind ('<ButtonRelease-1>', self. selectionCommand) # Release messages with the mouse
Self. box. bind ('<ButtonRelease-3>', self. boxrightmenu) # use the right-click menu to delete a project

Self. PopupMenu = Menu (listF)
Self. PopupMenu. add_command (label = u'delete', command = self. deleteitem)
Self. PopupMenu. add_command (label = u'rename', command = self. renameitem)
Self. box. pack (side = LEFT, expand = YES, fill = BOTH)

Self. slbar = Scrollbar (listF, orient = VERTICAL, command = self. box. yview)
Self. slbar. pack (side = RIGHT, expand = NO, fill = BOTH)
Self. box. configure (yscrollcommand = self. slbar. set)

Btnf = Frame (infoF)
Btnf. pack (side = BOTTOM, fill = BOTH)
Self. savebtn = Button (btnf, text = u'create save', state = DISABLED, command = self. save)
Self. savebtn. pack (side = LEFT, expand = YES, fill = BOTH)
Self. saveasbtn = Button (btnf, text = u'save the modifie', state = DISABLED, command = self. saveas)
Self. saveasbtn. pack (side = RIGHT, expand = YES, fill = BOTH)

# Including list information and display information
TwoF = Frame (self)
TwoF. pack (side = BOTTOM, expand = YES, fill = BOTH)

# Display information and scroll bars
ShowF = Frame (twoF, relief = SUNKEN)
Self. text = Text (showF, height = 25, width = 65)
Self. text. insert (1.0, 'information ...')
Self. text. pack (side = LEFT, expand = YES, fill = BOTH)
Self. text. bind ("<Key>", self. modify)
Self. text. bind ("<Double-Button-1>", self. tomodify)
Self. ismodified = False
ShowF. pack (side = TOP, expand = YES, fill = BOTH)

Self. scrollbar = Scrollbar (showF, orient = VERTICAL, command = self. text. yview)
Self. scrollbar. pack (side = RIGHT, expand = NO, fill = BOTH)
Self. text. configure (yscrollcommand = self. scrollbar. set)

# Provides input interfaces and functions such as: Search
InputF = Frame (twoF)
InputF. pack (side = BOTTOM, fill = BOTH)
Self. inputStr = StringVar ()
Self. inputStr. set ('')
Self.info = StringVar ()
Self.info. set ('infomation ...')
Self. entry = Entry (inputF, relief = SUNKEN, textvariable = self. inputStr)
Self. entry. bind ("<Return>", self. inputreturn)
Self. entry. pack (side = LEFT, expand = YES, fill = BOTH)

Self. findbtn = Button (inputF, text = u'lookup ', command = self. OnFind)
Self. findbtn. pack (side = LEFT, expand = YES, fill = BOTH)
Self. helpbtn = Button (inputF, text = u'help', command = self. OnHelp)
Self. helpbtn. pack (expand = NO, fill = Y)

Def OnHelp (self ):
Fp = open('readme.txt ')
Buff = fp. read ()
Fp. close ()

Self. text. delete (1.0, END)
Self. text. insert (1.0, buff)
Self. helpbtn. config (state = DISABLED)

Def deleteitem (self ):
# Right-click the menu and delete it
Sels = self. box. curselection ()
If len (sels) = 0:
Pass # print 'no selection'
Else:
Sec = self. items [int (sels [0])]
Self. config. remove_section (sec)
Self. config. write (open ('data. ini ', 'w '))
Self. box. delete (sels [0])
# Self. items. remove (sels [0]) # reference effect
Self. text. delete (1.0, END)
Self. text. insert (1.0, 'delete success .')

Def renameitem (self, event = None, en = None ):
# Mail menu, rename Function
Retval = askstring ("input ",
"Input the new name :")
If len (retval) = 0:
Return
Sels = self. box. curselection ()
If len (sels) = 0:
Pass # print 'no selection'
Else:
# Array/table/configuration file
Sec = self. items [int (sels [0])]

Self. box. delete (0, END)
Self. items [int (sels [0])] = retval # Array
Self. items. sort ()
Self. box. insert (0, * self. items) # Table

Self. config. remove_section (sec)
Self. config. add_section (retval)
Self. config. write (open ('data. ini ', 'w') # configuration file

Self. text. delete (1.0, END)
Self. text. insert (1.0, 'rename success .')

Def boxrightmenu (self, event = None, en = None ):
# Right-click the menu
Self. PopupMenu. tk_popup (* self. winfo_pointerxy ())

Def tomodify (self, event = None, en = None ):
If self. ismodified = True:
Self. saveasbtn. config (state = DISABLED)
Self. ismodified = False
Else:
Self. saveasbtn. config (state = NORMAL)
Self. ismodified = True
Return True

Def modify (self, event = None, en = None ):
Self. saveasbtn. config (state = NORMAL)
Return True

Def inputreturn (self, event = None, en = None ):
Self. OnFind ()
Return True

Def selectionCommand (self, event = None, en = None ):
# Show Details when selecting a list
Sels = self. box. curselection ()
If len (sels) = 0:
Pass
Else:
Filename ='s _ '+ self. box. get (sels [0]) +'. ini'
Fp = open (filename)
Strinfo = fp. read ()
Fp. close ()
Self. text. delete (1.0, END)
Self. text. insert (1.0, strinfo)
Self. helpbtn. config (state = NORMAL)

If _ name _ = '_ main __':
Finder (). mainloop ()

If you want to upload the source code file, create a readme.txt file and a data. ini file. Otherwise, the two files cannot be opened, causing an error. Why didn't I use exception handling? I have not completed that step. Now this version is ready for use.
To download all the complete files, click Download Attachment.
[Note]: After you open the software, enter the list, tuple, dict, and Other types for query. Click "new" to save it to the list for the next Quick Start.
Some packages are not included in the source code, such as codecs. In this case, you must manually add the package to the beginning of the source file to query the package, for example, import codecs.
If you have modified the text in the display box, click Save changes in time to save the changes so that you can see the effect of your changes next time.
This version is not complete and has limited functions. It is based on Python, so its value is mainly used as an example for Python development and application.
If you have any modification suggestions, please leave a message.
2011-03-10 12:15:05
Some netizens reported that the source code could not be correctly executed. I thought that it should have nothing to do with the version. These are basic functions and usage. If you cannot execute the command, please install the Pmw and wx packages correctly.
1. Pmw installation method:
Download pm1_1.3.2.tar.gz. This item is very easy to find. Please click Baidu. After the download, unzip the package locally, find the "Pmw" directory, and copy the directory to your Pyhton installation directory. No other directories are required. It can be directly under C: \ Python27 \. Make adjustments for other Python versions.
2. the wx installation method is as follows:
Download: wxPython2.8-win32-unicode-2.8.11.0-py27.exe. Please refer to Baidu for this article. It is estimated that wxPython's official English website is the best to download. Many Chinese websites also provide download, so it is not difficult to find it. Double-click the default installation method. You do not need to change the directory or other settings. It will automatically install to your installation directory, my directory is: C: \ Python27 \ Lib \ site-packages \ wx-2.8-msw-unicode.
3. The Tkinter library has been integrated into the system and does not need to be installed. The above two databases are used.
4. How can I check whether the installation is correct?
After installation, enter import wx or import Pmw in command line of Python to check whether the input is correct. The error message is correct. You can also look at the information in the package, such as dir (wx) or dir (Pmw ). It is not recommended to use the help () function. As I mentioned above, the wx information is as much as 10 MB. You cannot finish the display. You may not like the effects of constantly refreshing the screen.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.