Cloth with homemade Python function to help query gadgets _python

Source: Internet
Author: User
Tags eval exception handling pack in python
For example, when learning lists, tuple, dict, str, OS, SYS, and other modules, using Python's own documentation can quickly and comprehensively learn the functions that are processed. So this self-contained document function can give a lot of convenience to the scholar, it is better to do a short development.

However, when you leave CMD, to use idle or to use Komodo Edit and other software for "segment-type programming", it is a bit stretched. For example, the WX library is so large that there are 10MB of Help files, and if you open it in cmd, you can think about how long it will take you to find the help you want. After a massive familiarity with Python's various APIs, you will find that this is not as good as it might seem. Constantly press any key to page, while the page can accommodate a limited number of pages. So it shows a lot of inconvenience.

There are probably two ways of solving this.

  first, use the module Docs tool

This tool is a method of querying the Help file with the Python GUI idle. In the form of Web pages, we can use the local functions of this machine to provide a simulation online query method. It can open a Web page, which can display all the functions, and with the specification of the classification, more clearly, but it is still inconvenient to use, after all, there is no search function. If you keep a link to the page you are looking for, you cannot use it consistently. So this has a big problem, at the same time, its generated Web page file is huge, memory is too small will bring pressure to the system. So this method is not a very convenient way to use.

second, develop your own tools

In fact, I also do not want to develop their own tools, because this takes time, a few days or weeks of time to say less, say more and more, and give their own inner pressure is not small, after all, need a lot of distraction to do this thing. I've been searching the internet for a long time, did not find, for WX, I found a Wxpython API in English documents, described above is not clear, very vague, directly listed functions and specific parameters, how to use the basic rarely mentioned, and many of the control of the various style is not detailed list. So it is very difficult to use, if the name is forgotten, it can also check the complete word and detailed list of parameters. The other features are rarely involved at all.

In view of this, I decided to make myself a small tool, the cost of a small system to easily query the functions of various functions and modules. Only one available version is given here. Open source code for everyone, code style and control design for beginners to imitate. The master is willing to criticize me, I am all ears. Later versions will also be released here, the time may be first encapsulated in the release, now released this is the source code file, we all know that Python code file double-click can be executed.

Copy Code code as follows:

#coding =utf-8
#功能介绍: The software was initially used only for modules and function usage queries for quick display
#扩展功能: You can save the results of a successful query to a local,
# Save some of the functions in Chinese with comments
# put the saved keyword on the right list
#深度扩展: Use database to save results, and provide additions and deletions to check the interface

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):
#执行, and get the results
info = Self.inputStr.get ()
If Len (info) ==0:
Return True
Buff =stringio ()
temp = sys.stdout #保存标准I/o stream
Sys.stdout = Buff #将标准I/o stream redirected to 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
Try
__IMPORT__ (Info)

FMT = ' Help (' +self.inputstr.get () + ') '
result = eval (FMT)
Self.text.insert (1.0, Buff.getvalue ())
Except
Self.text.insert (1.0, "ERROR.")
Sys.stdout =temp #恢复标准I/O stream Buff.getvaue ()
Self.helpbtn.config (State=normal)


def save (self):
#搜索, save if not found, use INI file, save data
#保存原始
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):
#保存修改
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 bold ')
Self.pack (Expand=yes, Fill=both)
Self.master.title (U ' python function finder ')
Self.master.iconname ("Calc1")

#左侧列表, place the 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)

#获取项目
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) #使用鼠标释放消息
Self.box.bind (' <ButtonRelease-3> ', Self.boxrightmenu) #使用右键菜单删除项目

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 ' new save ', state=disabled, Command=self.save)
Self.savebtn.pack (Side=left, Expand=yes, Fill=both)
SELF.SAVEASBTN = Button (BTNF, text=u ' save modifications ', state=disabled, Command=self.saveas)
Self.saveasbtn.pack (Side=right, Expand=yes, Fill=both)


#包括列表信息和显示信息
TWOF = Frame (self)
Twof.pack (Side=bottom, Expand=yes, Fill=both)

#显示信息, scroll bar
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)

#提供输入接口, and features such as: find
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):
#右键菜单, removing features
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]) #是引用效果
Self.text.delete (1.0, end)
Self.text.insert (1.0, ' delete success. ')


def renameitem (Self,event=none,en=none):
#邮件菜单, renaming features
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
#数组/Table/configuration file
SEC = Self.items[int (Sels[0])]

Self.box.delete (0, end)
Self.items[int (sels[0]) = retval #数组
Self.items.sort ()
Self.box.insert (0, *self.items) #表

Self.config.remove_section (SEC)
Self.config.add_section (retval)
Self.config.write (Open (' Data.ini ', ' W ')) #配置文件

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


def boxrightmenu (Self,event=none,en=none):
#弹出右键菜单
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 the list is selected
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 ()

Note, source code file if you want to execute correctly, please create a Readme.txt file, and Data.ini file. Otherwise, there will be an error because these two files are not open. Why not use exception handling? I haven't been perfect to that step. Now this version is ready to use, so let's not try.
To complete all the files, please click: Download the attachment.
"Note": Open the Software, please enter the list, tuple, dict and other types of inquiries, click New Save to save to the list for the next quick open.
Some packages are not included in the source code, such as codecs. This time you need to manually add this package to the beginning of the source file to be able to query, such as: Import codecs.
If you have modified the text in the Display box, please click Save to save it in time so that you can see the effect of your changes the next time you open it.
This version is not perfect and has limited functionality, and is based on Python, so its value is as a Python development application example.
Please leave a message if you have any amendment.
2011-03-10 12:15:05
A netizen reflects not to be able to execute this source code correctly. I think after thinking that the version should have no relationship, these are very basic functions and usage. If you are not able to execute, please try to properly install PMW and WX these two packages.
1, PMW installation mode is:
Download: pmw.1.3.2.tar.gz. This thing is very easy to find, please Baidu can be. After downloading in situ decompression, find the "PMW" directory, copy this directory to your Pyhton installation directory, do not need other specific directory. Directly under the C:\Python27\ can be, the other Python version please make the appropriate adjustments.
2, WX installation method is:
Download: Wxpython2.8-win32-unicode-2.8.11.0-py27.exe. This thing please Baidu, it is estimated to wxpython the official English web site to download the best, many Chinese websites also provide downloads, so find it is not difficult. Double-click on the default installation, you do not have to change the directory or other. It will automatically install to your installation directory, my directory is: C:\Python27\Lib\site-packages\wx-2.8-msw-unicode.
3, Tkinter Library has been system integrated, do not need to install. The main is the top two libraries can be.
4, how to verify the installation is correct?
After installation, enter the command line in Python: import wx or import PMW try to enter correctly. There is no hint of information that is correct. You can also take a closer look at the information in the package, typing such as: Dir (wx) or dir (PMW). It is not recommended to use the Help () function, as I said above, WX information as many as 10MB, you are not finished, the effect of constantly brushing the screen you will not like.
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.