Python import探索



Java和Python玩家对import关键字一点都不陌生,本文以python为例,试着理解一下。

##1. 环境变量 可以猜到,在import模块时,python会从环境变量中搜索需要加载的模块,这个列表就存放在sys.path变量中,可以进行修改。想要引入时,首先要将路径放到环境变量中。 对环境变量临时修改,将/home/admin/git添加进来,示例如下:

>>> import sys
>>> '/home/admin/git' in sys.path
False
>>> sys.path.append('/home/admin/git')
>>> '/home/admin/git' in sys.path
True
>>> print sys.path
['', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib/python2.6/site-packages', '/home/admin/git']

类似windows系统变量,在查询时,也是按照列表的顺序进行遍历

阅读全文 »


ChatProject3



This is the final version of the chat project, here we modified the chatclient.java using multithread. Chatlient.java

package Chat;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;

public class ChatClient extends Frame {
    Socket s = null;
    DataOutputStream dos = null;
    DataInputStream dis = null;
    private boolean bConnected = false;

    TextField tfTxt = new TextField();
    TextArea taContent = new TextArea();
    Thread tRecv = new Thread(new RecvThread());

    public static void main(String[] args) {
        new ChatClient().launchFrame();
    }

    public void launchFrame() {
        setLocation(400, 300);
        this.setSize(300, 300);
        add(tfTxt, BorderLayout.SOUTH);
        add(taContent, BorderLayout.NORTH);
        pack();
        this.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent arg0) {
                disconnect();
                System.exit(0);
            }

        });
        tfTxt.addActionListener(new TFListener());
        setVisible(true);
        connect();

        tRecv.start();
    }

    public void connect() {
        try {
            s = new Socket("127.0.0.1", 8888);
            dos = new DataOutputStream(s.getOutputStream());
            dis = new DataInputStream(s.getInputStream());
            System.out.println("connected!");
            bConnected = true;
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void disconnect() {
        try {
            dos.close();
            dis.close();
            s.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private class TFListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            String str = tfTxt.getText().trim();
            // taContent.setText(str);
            tfTxt.setText("");

            try {
                // System.out.println(s);
                dos.writeUTF(str);
                dos.flush();
                // dos.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }

        }

    }

    private class RecvThread implements Runnable {

        @Override
        public void run() {
            try {
                while (bConnected) {
                    String str = dis.readUTF();
                    // System.out.println(str);
                    taContent.setText(taContent.getText() + str + '\n');
                }
            } catch (SocketException e) {
                System.out.println("退出了,bye!");
            } catch (EOFException e) {
                System.out.println("推出了,bye - bye!");
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }
}

阅读全文 »


ChatProject---2



###6. ChatClient send message to ChatServer.

In this version, we try to make the communications between chatclient and chatserver.

ChatClient

package Chat;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
import java.awt.*;
import java.awt.event.*;

public class ChatClient extends Frame {
    TextField tf = new TextField();
    TextArea ta = new TextArea();
    Socket s = null;

    public static void main(String[] args) {
        new ChatClient().launchFrame();
    }

    private void launchFrame() {
        this.setLocation(300, 500);
        this.setSize(300, 300);
        this.add(tf, BorderLayout.SOUTH);
        this.add(ta, BorderLayout.NORTH);
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
                ;
            }
        });
        tf.addActionListener(new TFListener());
        this.pack();
        this.setVisible(true);
        connect();
    }

    private void connect() {
        try {
            s = new Socket("127.0.0.1", 8888);
            System.out.println("Conencted to server!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private class TFListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String str = tf.getText().trim();
            ta.setText(str);
            tf.setText("");
            // Send message
            try {
                DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                dos.writeUTF(str);
                dos.flush();
                dos.close();
            } catch (IOException ee) {
                ee.printStackTrace();
            }
        }
    }
}

阅读全文 »


ChatProject---1



I have not installed the Chinese input method. It is really cool to write this blog in English, lol~ It is a simple chat project implemented by java. I followed the MSB java tutorial and I am here to say thank you to the graceful teather.

1. Init a chat window

The first version is about to init a GUI window, it is so simple that you could not close it on clicking the X button. Based on the package Frame.

import java.awt.*;

public class ChatClient extends Frame{
    TextField tf = new TextField();
    TextArea ta = new TextArea();
    public static void main(String[] args) {
        ChatClient cc = new ChatClient();
        cc.launchFrame();
    }

    public void launchFrame () {
        this.setLocation(400, 300);
        this.setSize(300, 400);
        this.add(tf, BorderLayout.SOUTH);
        this.add(ta, BorderLayout.NORTH);
        pack();
        this.setVisible(true);
    }
}

阅读全文 »


Java学习笔记



工作需求,猛看Java,学习完了马士兵老师的第一阶段视频,做一下记录和总结。

##Hello world

HelloWorld.java

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello world !");
    }
}

##变量

byte int short long float double char boolean

##数组

int a[];
int [] a = new int[100];
int [] b = new int[] {1, 2, 3};

#类

FaceObject.java

interface dot {
    void fishing();
}
public class FaceObject {
    public static void main(String[] args) {
        Fish f = new Fish();
        f.fishing();
    }
}

class Fish implents dot{

    private int amount;

    void fishing() {
        System.out.println("Fsh is fishing");
    }
    
    int getAmount() {
        return amount;
    }

    Fish(int amount) {
        this.amount = amount;
    }

    Fish() {
        this.amount = 0;
    }
}

#END

阅读全文 »