A wonderful story from Python (1)

Source: Internet
Author: User

A wonderful story from Python (1)

Python is a widely acclaimed programming language. She has powerful, concise, and elegant expressiveness, and can support industrial development practices.

However, this article is not an introduction and tutorial for Python.

Today we are going to tell a different story to see what other unknown things of Python are worth exploring.


We want to use the wisdom and power of Python to try a new thought, new design, and new framework. We hope to build a power that can rival the mature model of the hardware industry, so that software development can be efficient, mature and artistic. This is the author's dream.


Synopsis: Suppose we want to write an application with the function of a network-based plain text chat client program named TextChat.
According to the typical design, developers split the system module as follows:
1. Controller: controls the connection of the server.
2. LoginWindow: Enter the user name and password to log on to the server.
2. ChatWindow: displays chat information and the online status of friends. Enter and send chat messages and log out of the server.
3. CommunicationManager: The underlying network module responsible for communication.

The code used for the App subject and main classes (similar to the pseudo code of Python syntax, for the sake of simplicity, it is only intended for use) looks like this:
import CommunicationManagerimport LoginWindowimport ChatWindowimport Controllerdef main():    serverHost = u'192.168.1.100'    serverPort = 8000        ctrl = Controller()    ctrl.StartWork(serverHost, serverPort)


# Connect to the server, create LoginWindow and ChatWindow, and start LoginWindow.
class Controller():    def __init__(self):        pass            def StartWork(self, serverHost, serverPort):        commMgr = CommunicationManager(serverHost, serverPort)        succeeded = commMgr.Connect()        if not succeeded:            return        winChat  = ChatWindow(commMgr)                winLogin = LoginWindow(commMgr, winChat)        winLogin.Show()#Modal window.                commMgr.Disconnect()


# Log on to the server and start ChatWindow.
class LoginWindow():    def __init__(commMgr, winChat):        self.__commMgr = commMgr        self.__winChat = winChat    def Show():        username = xxx.GetValue()        password = xxx.GetValue()        succeeded = self.__commMgr.Login(username, password)if not succeeded:            return        self.__winChat.Show()

# Process users' Chat operations: invite friends, send and receive messages, and log out of the server.
class ChatWindow():    def __init__(commMgr):        self.__commMgr = commMgr    def __OnInviteButtonPressed():        ...        friendName = xxx.GetValue()        succeeded = self.__commMgr.InviteFriend(friendName, self.HandleReceivedMsg)        ...            def __OnSendButtonPressed():        ...        message = xxx.GetValue()        succeeded = self.__commMgr.SendMessage(message)...            def HandleReceivedMsg(receivedMsg):        ...    def __OnWindowClosing(self):        self.__commMgr.Logout()

# Provides all underlying communication functions: connection, disconnection, login, logout, sending and receiving, and friend invitation.
class CommunicationManager():    def __init__(serverHost, serverPort):        self.__serverHost = serverHost        self.__serverPort = serverPort    def Connect(serverHost, serverPort):        pass    def Disonnect(serverHost, serverPort):        pass    def Login(username, password):        pass    def Logout():        pass    def InviteFriend(friendName, callbackListener):        ...        self.__MessageReceiveThread(callbackListener)        ...    def SendMessage(message):        pass    def __MessageReceiveThread(callbackListener):        pass

See the module relationship diagram for the collaboration and interaction between modules.

VcSjv + response/bKizai5/response + Cjxicj4Ku/response +/response/qsq8svrJ + response/response + m7ucO709C/qreio6xBxKO/response/Response /release/pzfnN + release + QcSjv + m1xL + release/go6y63L/release/m0 + MC0tcTNtLP + oaM8L3A + release/T0M/release + MnPzsTL + release + export/export + PGJyPgo8L3A + export/export + export/pvOS1xPHuus/export/ydLUz + u8 + export/b6t0em1xL + export/export +/EqtW5tcS/qrei1d/export + response + PGJyPgo8L3A + response/K83LD8uqzI/bj21 + m8/response + m8/response + O7 + tOyvP7Ptc2ztcS7 + response/Io6y/response + response/t9a0 + sLrysfU9cO00LS1xKGjPC9wPgo8cD48cHJlIGNsYXNzPQ = "brush: java; "> from Softchip import Machineimport CommunicationManagerimport loginort wimport ChatWindowimport Controllerdef main (): protocol = u'chip _ id_comm_mgr 'CHIP_ID_CONTROLLER = u'chip _ id_controller' protocol = u'chip _ id_login_win 'chip_id_chat_win = u'chip _ id_chat_win 'chipList = [{Machine. CI_CREATOR: CommunicationManager, Machine. CI_ARGS: CHIP_ID_COMM_MGR,}, {Machine. CI_CREATOR: Controller, Machine. CI_ARGS: CHIP_ID_CONTROLLER,}, {Machine. CI_CREATOR: LoginWindow, Machine. CI_ARGS: CHIP_ID_LOGIN_WIN,}, {Machine. CI_CREATOR: ChatWindow, Machine. CI_ARGS: CHIP_ID_CHAT_WIN,},] machine = Machine () machine. createChips (chipList) machine. boot () machine. trigger (u'api _ start_work ', U' 192. 168.1.100', 8000)
How is it? Do you think the arrays of the army are neat, rigid, and concise!

This is exactly the code we are looking forward. Of course, although the Code is beautiful, you must be able to really work, not just huafan embroidery leg.

SoftchipThe architecture is silently supported at the underlying layer, and each module needs to be designed according to specific specifications.

In the next article of this series, we will look at the code of each module to seeSoftchipHow to program the architecture.

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.