Languages

[Python] Send an email

import smtplib

# user settings
email_addr = 'your@email'
email_pass = 'yourpassword' # you might need to use an application password
msg = "your message"

# send simple email
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(email_addr, email_pass)
server.sendmail(email_addr, email_addr, msg)
server.quit()

[SQL] 匯入.csv檔案以及相關問題

語法:

LOAD DATA INFILE "C:\\ProgramData\\MySQL\\MySQL Server 8.0\\Uploads\\plane-data.csv" INTO TABLE plane
FIELDS TERMINATED BY ',' 
ENCLOSED BY '"' 
LINES TERMINATED BY '\r\n'
IGNORE 1 LINES
(tailnum,type,manufacturer,@issue_date,model,status,aircraft_type,engine_type,year)
SET issue_date=str_to_date(@issue_date, "%m/%d/%Y");

問題1:
出現secure-file-priv限制檔案位置以及LOCAL語法使用

解法: 到my.ini,將此設定修改成空字串

secure-file-priv=""

問題2:
出現空值與attribute型態不符(如””incorrect for integer)

解法: 到my.ini,將此設定修改成空字串

sql-mode=""

問題3:
csv檔案中,空值並沒有表示成,,,而是直接空著

解法: 使用excel,另存成.csv檔案,即可自動使用,,分隔。

[C++] Measure of time elapsed

#include <ctime>
using namespace std;

clock_t begin = clock();
target_func();
clock_t end = clock();
double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
cout << "Elapsed time: " << elapsed_secs << " sec." << endl;

[Python] 虛擬環境簡易用法

建立虛擬環境

python -m venv mytargetdir

啟用虛擬環境

source activate myenv

關閉虛擬環境

source deactivate

export環境:

pip freeze > requirements.txt

import環境:

pip install -r requirements.txt

*conda請使用以下指令:

#新建virtual env:
conda create -n yourenvname python=x.x

#使用環境檔案建立虛擬環境
conda env create -f environment.yml

#查看所有env:
conda info -e

#啟用虛擬環境
conda activate yourenvname

#關閉虛擬環境
conda deactivate

#刪除虛擬環境:
conda env remove -n yourenvname

#匯出虛擬環境:
conda env export > environment.yml

#使用環境檔案安裝套件
conda env update -n yourenvname --file environment.yml

[C++] Using msgpack-c with FIO

Output serialized objects to ofstream:

#include <msgpack.hpp>
 ofstream out("output.txt");
 msgpack::packer<std::ofstream> pk(&out);
 //packing an integer
 int n=10;
 pk.pack(n);
 //packing a integer list
 vector<int> num_list;
 num_list.push_back(1); num_list.push_back(2); num_list.push_back(3);
 pk.pack(num_list);
 //packing a integer list one by one 
 pk.pack_array(num_list.size());
 for(int j = 0;j<num_list.size();j++){
   pk.pack(num_list[i]);
 }

Input serialized object from ifstream:

ifstream in("input.txt",ios::binary);
stringstream buffer;
buffer << in.rdbuf();

msgpack::unpacker pac;
// feeds the buffer.
pac.reserve_buffer(buffer.str().size());
memcpy(pac.buffer(), buffer.str().data(), buffer.str().size());
pac.buffer_consumed(buffer.str().size());

// now starts streaming deserialization.
msgpack::object_handle oh;
while(pac.next(oh)) {
 std::cout << oh.get() << std::endl;
}

[C++] cout formatting

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
 float x = 123.123556;
 //output:123.124
 cout << fixed <<setprecision(3) << x << endl;
 return 0;
}

[C++] log(),log10()

#include<cmath>
//using natural log
cout<<log(10)<<endl;

//using 10 based log
cout<<log10(10)<<endl;

[C++] max_element()&min_element()

#include <algorithm>    // std::min_element, std::max_element
bool myfn(int a,int b){
  return a < b;
}
int myints[] = {3,7,2,5,6,4,9};

// using default comparison:
cout << "The smallest " << *min_element(myints,myints+7) << endl;
cout << "The largest "  << *max_element(myints,myints+7) << endl;

// using function myfn as comp:
cout << "The smallest " << *min_element(myints,myints+7,myfn) << endl;
cout << "The largest "  << *max_element(myints,myints+7,myfn) << endl;