วันเสาร์ที่ 24 เมษายน พ.ศ. 2564

Function def python ส่ง line ความแตกต่างระหว่า text และ picture

import urllib.parse

import requests


def line_text(message):

    msg = urllib.parse.urlencode({"message":message})

    LINE_HEADERS = {'Content-Type':'application/x-www-form-urlencoded',"Authorization":"Bearer "+LINE_ACCESS_TOKEN}

    session = requests.Session()

    session_post = session.post(URL_LINE, headers=LINE_HEADERS, data=msg)

    print(session_post.text)

 

def line_picture(message, path_file):

    file_img = {'imageFile': open(path_file, 'rb')}

    msg = ({'message': message})

    LINE_HEADERS = {"Authorization":"Bearer "+LINE_ACCESS_TOKEN}

    session = requests.Session()

    session_post = session.post(URL_LINE, headers=LINE_HEADERS, files=file_img, data=msg)

วันอาทิตย์ที่ 4 เมษายน พ.ศ. 2564

สิ่งนี้ที่มีอยู่

 






ทำตัวโรแมนติก ไปกับคนที่รักกัน อบอุ่นสุขใจ รู้สึกดีกับทุกๆ สิ่ง 

ความรักที่สุขล้น ลืมความเศร้าทั้งปวงที่เคยผ่านพบมา 

ขอบคุณทุกสิ่งทุกอย่างที่ผ่านมา

 ขอบคุณประสบการณ์ที่เคยฝัน และไม่ตั้งใจ

ขอบคุณเพื่อนคุยที่ดี เพื่อนที่เคยช่วยเหลือกัน

ขอบคุณคนที่เคยคิดร้าย หวังไม่ดี เอาเปรียบ ที่ให้ประสบการณ์ว่าแบบไหนควรคบ ควรเข้าใกล้ หรือถอยห่าง ทำให้ต้องระวังและไม่ประมาทกับชีวิต

ขอบคุณ ความเสี่ยง ความหวัง ความตื่นเต้น เมื่อได้อย่างหวังย่อมปิติยินดี เมื่อผิดหวังย่อมเสียใจ หัวเสียเป็นธรรมดา

ขอบคุณปัญหาต่างๆ ที่ทำให้คิด ทั้งคิดมาก จิตตก คิดแก้ปัญหา ทั้งแก้ได้และแก้ไม่ได้

ขอบคุณชะตาชีวิต ที่มีขึ้นมีลง แม้ไม่เคยขึ้นสุด แต่เวลาลงนั้นลงสุดๆ

ขอบคุณความเจ็บไข้ได้ป่วย แม้เจ็บจวนตาย

ขอบคุณคนที่เคยรัก และคนทีาเคยเกลียด

ขอบคุณที่ยังมีวันนี้ ที่ยังต้องไปต่อ

ขอบคุณความหวังและความฝัน

ขอบคุณธรรมชาติ มีทั้งสดใสและ มีทั้งโหดร้าย

ขอบคุณวันนี้ วันที่ผ่านมาและวันข้างหน้า

วันศุกร์ที่ 2 เมษายน พ.ศ. 2564

เปลี่ยนความคิด

 รถบิ๊กไบต์ใหม่

รถยนต์ห้าประตู

บ้านเดี่ยว

ตั้งโปรแกรมบริหารบัญชี

เงินก้อนใหญ่

ความรัก

ท่องเที่ยว

คุณต้องอยู่แนวเดียวกับจักรวาล

ทิ้งความคิดเดิม หาทางเข้าถึงสิ่งที่เป็นอยู่ด้วยมุมมองอื่น



วันอังคารที่ 15 กันยายน พ.ศ. 2563

 NS3 วิธีติดตั้งและทดสอบ

Installation

1.) ติดตั้งโปรแกรม VirtualBox จาก https://www.virtualbox.org/wiki/Downloads

2.) ติดตั้ง Ubuntu 17.04 

3.) Start VM log-in 

4.) เปิด Shell

- Update package lists จาก repositories 

    $ sudo apt-get update

- Install Compiler และ Python 

    $ sudo apt-get -y install gcc g++ python python-dev

- Install Mecurial, Git Source Control และ Python Tools 

    $ sudo apt-get -y install mercurial python-setuptools git

- Install Qt สาหรับ NetAnim Animator 

    $ sudo apt-get -y install qt4-dev-tools libqt4-dev

- Install Library สาหรับสร้าง Python Binding 

    $ sudo apt-get -y install cmake libc6-dev libc6-dev-i386 g++-multilib

- Install Debugger 

    $ sudo apt-get -y install gdb valgrind

- Install GNU Scientific Library Support 

    $ sudo apt-get -y install gsl-bin libgsl2 libgsl-dev

- Install Component สาหรับ support Network Simulation Cradle (NSC) 

    $ sudo apt-get -y install flex bison libfl-dev

- Install TCP Dump สาหรับอ่าน traces 

    $ sudo apt-get -y install tcpdump

- Install Database สาหรับ statistics framework 

    $ sudo apt-get -y install sqlite sqlite3 libsqlite3-dev

- Install XML Tools 

    $ sudo apt-get -y install libxml2 libxml2-dev

- Install GTK 

    $ sudo apt-get -y install libgtk2.0-0 libgtk2.0-dev

- Install Tools สาหรับ Virtual Machine กับ ns3 

    $ sudo apt-get -y install vtun lxc

- Install Code Styles Checker 

    $ sudo apt-get -y install uncrustify

- Install Doxygen และ In-Line documentation tools อื่นๆ 

    $ sudo apt-get -y install doxygen graphviz imagemagick 

    $ sudo apt-get -y install texlive texlive-extra-utils texlive-latex-extra texlive-font-utils texlive-lang-portuguese dvipng

- Install Tools สาหรับ reStructuredText ใน manual และ tutorial 

    $ sudo apt-get -y install python-sphinx dia

- Install Tools สาหรับ ns-3-pyviz visualizer 

    $ sudo apt-get -y install python-pygraphviz python-kiwi python-pygoocanvas libgoocanvas-dev ipython

- Install Tools สาหรับโมดูล Openflow 

    $ sudo apt-get -y install libboost-signals-dev libboost-filesystem-dev

- Install Tools สาหรับ MPI-based distributed emulation 

    $ sudo apt-get -y install openmpi-bin openmpi-common openmpi-doc libopenmpi-dev

- Install Tools อื่นๆ 

    $ sudo apt-get -y install autoconf cvs bzr unrar

- Install 7z 

    $ sudo apt-get -y install p7zip-full

- Install Synaptic 

    $ sudo apt-get -y install synaptic


5.) เปลี่ยนมาใช้ GCC Version 4.9

    $ sudo apt-get install gcc-4.9 g++-4.9

    $ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 2 --slave /usr/bin/g++ g++ /usr/bin/g++-4.9

ตรวจสอบ GCC Version

    $ gcc -v


6.) สร้างโฟล์เดอร์ ns3 ดาวน์โหลดและ unzip ns3 

    $ cd ~/ 

    $ mkdir ns3 

    $ cd ns3 

    $ wget http://www.nsnam.org/release/ns-allinone-3.26.tar.bz2

    $ tar xjf ns-allinone-3.26.tar.bz2


7.) Build ด้วยสคริปต์ 

    $ cd ~/ns3/ns-allinone-3.26/ 

    $ ./build.py --enable-examples --enable-tests

    รอ build จนเสร็จ บาง module อาจไม่ได้รับการ build เนื่องจากไม่ได้ระบุไว้ในออปชั่น


8.) Test เพื่อ verify การทางาน 

    $ cd ~/ns3/ns-allinone-3.26/ns-3.26 

    $ ./test.py


Test Example

1.) Copy ไฟล์ตัวอย่างออกมา run ในโฟล์เดอร์ scratch 

    $ cd ~/ns3/ns-allinone-3.26/ns-3.26/examples/tutorial 

    $ cp first.cc ~/ns3/ns-allinone-3.26/ns-3.26/scratch/


2.) พิจารณาไฟล์ first.cc 

    $ cd ~/ns3/ns-allinone-3.26/ns-3.26/scratch/ 

    $ gedit first.cc 

    ไฟล์ first.cc นี้มีการสร้าง node ขึ้นมา 2 โหนด บน point-to-point network โดยมี Server Application run ที่ port 9 บนโหนดหนึ่ง และ Echo client run ที่อีกโหนดหนึ่ง โดย client ทาการส่ง echo ไปที่ server 1 ครั้ง ให้ server ตอบกลับ


3.) ทดลอง Run 

    $ cd ~/ns3/ns-allinone-3.26/ns-3.26 

    $ ./waf --run scratch/first


Install NetAnim

1.) ติดตั้ง qmake 

    $ sudo apt-get -y install qt4-qmake


2.) ติดตั้ง Build NetAnim 

    $ cd ~/ns3/ns-allinone-3.26/netanim-3.107/

    $ make clean

    $ qmake NetAnim.pro

    $ make


3.) แก้ไข first.cc ให้สร้างไฟล์สาหรับแสดงการทางานใน Net Anim 

    $ cd ~/ns3/ns-allinone-3.26/ns-3.26/scratch/ 

    $ gedit first.cc 

    เพิ่มการ include Library






    เพิ่มการสร้าง Animation Interface ให้ output file เป็น first_anim.xml

    โดยวางโหนด 0 ไว้ที่ตาแหน่ง x-=1.0 y=1.0 และวางโหนด 1 ไว้ที่ตาแหน่ง x-=2.0 y=1.0 โดยทั้งสองโหนดมีขนาด 0.5


4.) ลอง run ไฟล์ first.cc อีกครั้ง 

    $ cd ~/ns3/ns-allinone-3.26/ns-3.26 

    $ ./waf --run scratch/first

    ไฟล์ first_anim.xml จะถูก save ไว้ที่ ~/ns3/ns-allinone-3.26/ns-3.26/


5.) เปิด NetAnim 

    $ cd ~/ns3/ns-allinone-3.26/netanim-3.107/ 

    $ ./NetAnim

    คลิกไอคอนเปิดไฟล์ browse ไปเลือกไฟล์ ~/ns3/ns-allinone-3.26/ns-3.26/first_anim.xml แล้วกดปุ่ม run simulation

วันพฤหัสบดีที่ 23 มกราคม พ.ศ. 2563

python ดึงข้อมูล .csv แล้ว insert เข้า mongoDB

import os
import csv
from pymongo import MongoClient


connection = MongoClient("mongodb://myuser:mypassword@localhost:27017")
# insert your data for database_name and collection_name
db = connection.database_name.collection_name


path="E:/TESTING/mongo_test"            
list_of_files = {}
for filename in os.listdir(path):
     # if the element is a csv file then..
     if filename[-4:] == ".csv":
         list_of_files[filename] = path + "\\" + filename
         print   (list_of_files[filename])
         with open(list_of_files[filename], encoding="utf8" ) as f:
            csv_f = csv.reader(f)
            for i, row in enumerate(csv_f):
               #if i > 5 and len(row) > 1 :
                 print(row)
                 db.insert({'F1': row[0], 'F2': row[1]})
                
# find all documents
results = db.find()

print()
print('==============================')

# display documents from collection
for record in results:
   # print out the document
   print(record['F1'] + ',',record['F2'])

print()

# close the connection to MongoDB
connection.close()

วันอังคารที่ 21 มกราคม พ.ศ. 2563

Python กราฟ ดึงข้อมูล mySQL mariaDB โดยเงี่อนไขวันที่

# -*- coding: utf-8 -*-
import csv
from datetime import datetime
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np

#### Import Code Files #####
import test_connect_mysql
cursor = test_connect_mysql.cursor
mydb = test_connect_mysql.mydb

cursor.execute('select date_in, SUM(no_tag) as t_no_tag, SUM(barcode)  as t_barcode  from error_bar_tag GROUP BY date_in');
rows = cursor.fetchall()
#str(rows)[0:300]
cursor.close
mydb.close

df = pd.DataFrame( [[ij for ij in i] for i in rows] )
df.rename(columns={0: 'date_in', 1:'t_no_tag', 2:'t_barcode'}, inplace=True);
df = df.sort_values(['date_in'], ascending=[1]);

#print(df['no_tag'])
#tag_error = len(df['no_tag'])
#print(tag_error)

date_show = []
date_in = df['date_in']
for i in range(len(date_in)):
    try:
        date_show.append(str(datetime.date(date_in[i])))
    except:
        date_in[i] = 'date_in[i] decode error'

print("count date_show = " + str(len(date_show)))
for x in date_show:
  print(x)
############## Polt Grash ###############
fp = mpl.font_manager.FontProperties(family='Tahoma',size=10)
x = date_show
no_tag = df['t_no_tag']
barcode = df['t_barcode']
ax = plt.gca()
ax.set_title(u'ทดสอบแสดงข้อมูลไม่อ่าน Tag',fontname='Tahoma',fontsize='13')
ax.set_xlabel(u'ป/ด/ว',fontname='Tahoma',fontsize='10')
ax.set_ylabel(u'จำนวนครั้ง',labelpad=-1,fontname='Tahoma',fontsize='10')
ax.plot(x,barcode,'-og' ,alpha=1)
ax.plot(x,no_tag,'-or' ,alpha=1)
ax.legend([u'บาร์โค๊ต',u'ไม่อ่าน Tag',],prop=fp,loc=0,fancybox=1,shadow=1)
plt.grid()
ax.xaxis.set_label_coords(1.05, -0.05)
ax.yaxis.set_label_coords(1.05, 0.5)
ax.tick_params(axis='x',rotation=35 ,labelsize=7, length=5)
plt.show()