VirtualBox

อัพเดท VirtualBox บน Windows 10 จากเวอร์ชัน 6.1.26 เป็น เวอร์ชัน 6.1.28 แล้วพอเปิด VM จะได้ Error

Call to NEMR0InitVMPart2 failed: VERR_NEM_INIT_FAILED (VERR_NEM_VM_CREATE_FAILED).

Result Code: E_FAIL (0x80004005)
Component: ConsoleWrap
Interface: IConsole {872da645-4a9b-1727-bee2-5585105b9eed}

ทางแก้ก็กลับมาใช้ Virtualbox 6.1.26

Python Regular expression

  • docs.python.org – re — Regular expression operations

ค้นหาตัวอักษรระหว่าง AAA และ ZZZ

import re

text = 'gfgfdAAA1234ZZZuijjk'

try:
    found = re.search('AAA(.+?)ZZZ', text).group(1)
except AttributeError:
    # AAA, ZZZ not found in the original string
    found = '' # apply your error handling
print (found)
# found: 1234
import re

text = 'page-0188482009-20211001-0002.json'

try:
    found = re.search('page-\w+-(.+?)-', text).group(1)
except AttributeError:
    found = ''
print (found)
# found: 20211001

หาชื่อไฟล์ที่อยู่หลัง folder ที่กำหนด

import re

text = 'content/ptn_year=2021/ptn_month=10/ptn_day=01/content-20211001.json'

try:
    found = re.search('ptn_day=\w+/(.*)', text).group(1)
except AttributeError:
    found = ''
print (found)
# found: content-20211001.json

การเรียก POST ด้วย HttpWebRequest

ถ้าเรียก HttpWebRequest แล้วเจอ error

The request was aborted: Could not create SSL/TLS secure channel.  

[Main] System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel.
   at System.Net.HttpWebRequest.GetResponse()

ให้แก้ไขด้วยการวาง ServicePointManager

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
// SecurityProtocolType.Tls
// SecurityProtocolType.Tls11
// SecurityProtocolType.Ssl3;

ไว้ก่อน HttpWebRequest

//createing HttpWebRequest after ServicePointManager settings
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://google.com/api/")

ตัวอย่าง

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
// ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
// ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11
// ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://example.com/api/")
request.ContentType = "application/json";
request.Method = "POST";

using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
    People movReq = new People();

    People.username = "user1";
    People.password = "paas1";
    People.xxx = "xxx";
    
    string jsonReq = JsonConvert.SerializeObject(movReq);
    log.Info(string.Format("Request: jsonString='{0}'", jsonReq));

    streamWriter.Write(jsonReq);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
MOVRealTimeResponse resp;
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
    var result = streamReader.ReadToEnd();
    log.Info(string.Format("Response: string='{0}'", result));

    resp = JsonConvert.DeserializeObject<MOVRealTimeResponse>(result);
}
Posted in C#

Python3 ติดต่อดาต้าเบส MySQL

Create Connection

import mysql.connector

mydb = mysql.connector.connect(
    host="localhost",
    user="yourusername",
    password="yourpassword"
)

print(mydb)
# <mysql.connector.connection_cext.CMySQLConnection object at 0x000002A56602E948>

หรือระบุ database

import mysql.connector

mydb = mysql.connector.connect(
    host="localhost",
    user="yourusername",
    password="yourpassword",
    database="mydatabase"
)

Creating a Database

import mysql.connector

mydb = mysql.connector.connect(
    host="localhost",
    user="yourusername",
    password="yourpassword"
)

mycursor = mydb.cursor()

mycursor.execute("CREATE DATABASE mydatabase")

Show databases

mycursor = mydb.cursor()

mycursor.execute("SHOW DATABASES")

for x in mycursor:
    print(x)

Creating a Table

mycursor.execute("CREATE TABLE customers (name VARCHAR(255), address VARCHAR(255))")
mycursor.execute("CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))")

Show table

mycursor.execute("SHOW TABLES")

for x in mycursor:
  print(x)

Alter table

mycursor.execute("ALTER TABLE customers ADD COLUMN id INT AUTO_INCREMENT PRIMARY KEY")

Delete table

mycursor.execute("DROP TABLE customers")

Insert Into Table

sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = ("John", "Highway 21")
mycursor.execute(sql, val)

mydb.commit()

print(mycursor.rowcount, "record inserted.")

Insert Multiple Rows

sql = "INSERT INTO customers (name, address) VALUES (%s, %s)"
val = [
    ('Peter', 'Lowstreet 4'),
    ('Amy', 'Apple st 652'),
    ('Hannah', 'Mountain 21'),
    ('Michael', 'Valley 345'),
    ('Sandy', 'Ocean blvd 2'),
    ('Betty', 'Green Grass 1'),
    ('Richard', 'Sky st 331'),
    ('Susan', 'One way 98'),
    ('Vicky', 'Yellow Garden 2'),
    ('Ben', 'Park Lane 38'),
    ('William', 'Central st 954'),
    ('Chuck', 'Main Road 989'),
    ('Viola', 'Sideway 1633')
]

mycursor.executemany(sql, val)

mydb.commit()

print(mycursor.rowcount, "was inserted.")

Select From a Table

We use the fetchall() method, which fetches all rows from the last executed statement.

mycursor.execute("SELECT * FROM customers")

myresult = mycursor.fetchall()

for x in myresult:
    print(x)

fetchone()

The fetchone() method will return the first row of the result:

mycursor.execute("SELECT * FROM customers")

myresult = mycursor.fetchone()

print(myresult)

					

Case Styles: Camel, Pascal, Snake

The most popular ways to combine words into a single string

  • camelCase
  • PascalCase
  • snake_case
  • kebab-case
Case StylesText
Rawuser login count
Camel CaseuserLoginCount
Pascal CaseUserLoginCount
Snake Caseuser_login_count
Snake Case (All Caps)USER_LOGIN_COUNT
Kebab Caseuser-login-count
Case Styles

Snake case to camel case

def snake_to_camel(text):
    text = ''.join(word.title() for word in text.split('_'))
    return text

st = 'user_login_count'
print(snake_to_camel(st))
# UserLoginCount

Camel case to snake case

  • docs.python.org – re — Regular expression operations
import re

def camel_to_snake(text):
  return re.sub(r'(?<!^)(?=[A-Z])', '_', text).lower()

print(camel_to_snake('userLoginCount'))
print(camel_to_snake('HTTPResponseCodeXYZ'))
# user_login_count
# h_t_t_p_response_code_x_y_z
import re

def camel_to_snake(name):
    name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
    return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()

def to_snake_case(name):
    name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
    name = re.sub('__([A-Z])', r'_\1', name)
    name = re.sub('([a-z0-9])([A-Z])', r'\1_\2', name)
    return name.lower()

print(camel_to_snake('UserLoginCount'))
print(camel_to_snake('camel2_camel2_case'))
print(camel_to_snake('getHTTPResponseCode'))
print(camel_to_snake('HTTPResponseCodeXYZ'))
# user_login_count
# camel2_camel2_case
# get_http_response_code
# http_response_code_xyz

NumPy

ตรวจสอบเวอร์ชันของ numpy

import numpy
print(numpy.version.version)
# 1.19.5
import numpy as np
print(np.__version__)
# 1.19.5
> conda list

Arrays

The array object in NumPy is called ndarray

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
print(arr)
print(type(arr))
# [1 2 3 4 5]
# <class 'numpy.ndarray'>

0-D Arrays

import numpy as np
arr = np.array(5)
print(arr)
print(type(arr))
# 5
# <class 'numpy.ndarray'>

1-D Arrays

import numpy as np
arr = np.array([1,2,3])
print(arr)
print(type(arr))
# [1 2 3]
# <class 'numpy.ndarray'>

2-D Arrays

import numpy as np

arr = np.array([[1, 2, 3], [4, 5, 6]])
print(arr)
print(type(arr))
# [[1 2 3]
#  [4 5 6]]
# <class 'numpy.ndarray'>

3-D Arrays

import numpy as np
arr = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])
print(arr)
print(type(arr))
# [[[1 2 3]
#   [4 5 6]]
# 
#  [[ 7  8  9]
#   [10 11 12]]]
# <class 'numpy.ndarray'>

Number of Dimensions

import numpy as np
a = np.array(5)
b = np.array([1, 2, 3])
c = np.array([[1, 2, 3], [4, 5, 6]])
d = np.array([[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])

print(a.ndim)
print(b.ndim)
print(c.ndim)
print(d.ndim)
# 0
# 1
# 2
# 3

Define the number of dimensions by using the ndmin argument.

import numpy as np
arr = np.array([1, 2, 3, 4], ndmin=5)

print(arr)
print('number of dimensions :', arr.ndim)
# [[[[[1 2 3 4]]]]]
# number of dimensions : 5

NumPy Data Types

NumPy has some extra data types, and refer to data types with one character, like i for integers, u for unsigned integers etc.

  • i – integer
  • b – boolean
  • u – unsigned integer
  • f – float
  • c – complex float
  • m – timedelta
  • M – datetime
  • O – object
  • S – string
  • U – unicode string
  • V – fixed chunk of memory for other type ( void )
import numpy as np
arr = np.array([1, 2, 3, 4])
print(arr.dtype)
# int32
import numpy as np
arr = np.array(['apple', 'banana', 'cherry'])
print(arr.dtype)
# <U6
import numpy as np
arr = np.array([1, 2, 3, 4], dtype='S')
print(arr)
print(arr.dtype)
# [b'1' b'2' b'3' b'4']
# |S1
import numpy as np
arr = np.array([1, 2, 3, 4], dtype='i4')
print(arr)
print(arr.dtype)
# [1 2 3 4]
# int32

Converting Data Type

import numpy as np
arr = np.array([1.1, 2.1, 3.1])
newarr = arr.astype('i') # newarr = arr.astype(int)
print(newarr)
print(newarr.dtype)
# [1 2 3]
# int32
import numpy as np
arr = np.array([1, 0, 3])
newarr = arr.astype(bool)
print(newarr)
print(newarr.dtype)
# [ True False  True]
# bool

Array Copy vs View

The main difference between a copy and a view of an array is that the copy is a new array, and the view is just a view of the original array.

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.copy()
arr[0] = 42
print(arr)
print(x)
# [42  2  3  4  5]
# [1 2 3 4 5]
import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.view()
arr[0] = 42
print(arr)
print(x)
# [42  2  3  4  5]
# [42  2  3  4  5]

Check if Array Owns it’s Data

copies owns the data, and views does not own the data

Every NumPy array has the attribute base that returns None if the array owns the data.

import numpy as np
arr = np.array([1, 2, 3, 4, 5])
x = arr.copy()
y = arr.view()

print(arr.base)
print(x.base)
print(y.base)
# None
# None
# [1 2 3 4 5]

Array Shape

Array Shape

import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(arr.shape)
print(type(arr.shape))
# (2, 4)
# <class 'tuple'>
import numpy as np
arr = np.array([1, 2, 3, 4], ndmin=5)

print(arr)
print('shape of array :', arr.shape)
# [[[[[1 2 3 4]]]]]
# shape of array : (1, 1, 1, 1, 4)

Array Reshaping

Reshape From 1-D to 2-D

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(4, 3)
print(newarr)
# [[ 1  2  3]
#  [ 4  5  6]
#  [ 7  8  9]
#  [10 11 12]]

Reshape From 1-D to 3-D

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
newarr = arr.reshape(2, 3, 2)
print(newarr)
# [[[ 1  2]
#   [ 3  4]
#   [ 5  6]]
# 
#  [[ 7  8]
#   [ 9 10]
#   [11 12]]]

Reshape return view

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
print(arr.reshape(2, 4).base)
# [1 2 3 4 5 6 7 8]

Unknown Dimension

You are allowed to have one “unknown” dimension.

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8])
newarr = arr.reshape(2, 2, -1) # newarr = arr.reshape(2, 2, 2)
print(newarr)
# [[[1 2]
#   [3 4]]
# 
#  [[5 6]
#   [7 8]]]

Flattening the arrays

Flattening array means converting a multidimensional array into a 1D array.

import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
newarr = arr.reshape(-1)
print(newarr)
# [1 2 3 4 5 6]

Array Iterating

import numpy as np
arr = np.array([1, 2, 3])
for x in arr:
    print(x)
# 1
# 2
# 3
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
for x in arr:
    print(x)
# [1 2 3]
# [4 5 6]
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
for x in arr:
    for y in x:
        print(y)
# 1
# 2
# 3
# 4
# 5
# 6

nditer() – Iterating Arrays Using nditer()

import numpy as np
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
for x in arr:
    print(x)
# [[1 2]
#  [3 4]]
# [[5 6]
#  [7 8]]
import numpy as np
arr = np.array([[[1, 2], [3, 4]], [[5, 6], [7, 8]]])
for x in np.nditer(arr):
    print(x)
# 1
# 2
# 3
# 4
# 5
# 6
# 7
# 8

nditer() – Iterating Array With Different Data Types

import numpy as np
arr = np.array([1, 2, 3])
for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S']):
    print(x)
# b'1'
# b'2'
# b'3'

nditer() – Iterating With Different Step Size

import numpy as np
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
for x in np.nditer(arr[:, ::2]):
    print(x)
# 1
# 3
# 5
# 7

ndenumerate()

import numpy as np
arr = np.array([1, 2, 3])
for idx, x in np.ndenumerate(arr):
    print(idx, x)
# (0,) 1
# (1,) 2
# (2,) 3
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
for idx, x in np.ndenumerate(arr):
    print(idx, x)
# (0, 0) 1
# (0, 1) 2
# (0, 2) 3
# (1, 0) 4
# (1, 1) 5
# (1, 2) 6

NumPy Joining Array

Joining NumPy Arrays

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.concatenate((arr1, arr2))
print(arr)
# [1 2 3 4 5 6]

Join two 2-D arrays along cols (axis=0)

import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
arr = np.concatenate((arr1, arr2), axis=0)
print(arr)
# [[1 2]
#  [3 4]
#  [5 6]
#  [7 8]]

Join two 2-D arrays along rows (axis=1)

import numpy as np
arr1 = np.array([[1, 2], [3, 4]])
arr2 = np.array([[5, 6], [7, 8]])
arr = np.concatenate((arr1, arr2), axis=1)
print(arr)
# [[1 2 5 6]
#  [3 4 7 8]]

stack()

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.stack((arr1, arr2), axis=0)
print(arr)
# [[1 2 3]
#  [4 5 6]]
import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.stack((arr1, arr2), axis=1)
print(arr)
# [[1 4]
#  [2 5]
#  [3 6]]

hstack()

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.hstack((arr1, arr2))
print(arr)
# [1 2 3 4 5 6]

vstack()

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.vstack((arr1, arr2))
print(arr)
# [[1 2 3]
#  [4 5 6]]

dstack()

import numpy as np
arr1 = np.array([1, 2, 3])
arr2 = np.array([4, 5, 6])
arr = np.dstack((arr1, arr2))
print(arr)
# [[[1 4]
#   [2 5]
#   [3 6]]]

Splitting Array

import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 3)

print(type(newarr))
print(newarr)
# <class 'list'>
# [array([1, 2]), array([3, 4]), array([5, 6])]

print(newarr[0])
print(type(newarr[0]))
# [1 2]
# <class 'numpy.ndarray'>
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 4)
print(newarr)
# [array([1, 2]), array([3, 4]), array([5]), array([6])]
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6])
newarr = np.array_split(arr, 5)
print(newarr)
# [array([1, 2]), array([3]), array([4]), array([5]), array([6])]

Splitting 2-D Arrays

import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])
print(arr)
# [[ 1  2]
#  [ 3  4]
#  [ 5  6]
#  [ 7  8]
#  [ 9 10]
#  [11 12]]

newarr = np.array_split(arr, 3) # newarr = np.array_split(arr, 3, axis=0)
print(newarr[0])
print(newarr[1])
print(newarr[2])
# [[1 2]
#  [3 4]]
# [[5 6]
#  [7 8]]
# [[ 9 10]
#  [11 12]]
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])
print(arr)
newarr = np.array_split(arr, 3, axis=1)
print(newarr[0])
print(newarr[1])
# [[ 1]
#  [ 3]
#  [ 5]
#  [ 7]
#  [ 9]
#  [11]]
# [[ 2]
#  [ 4]
#  [ 6]
#  [ 8]
#  [10]
#  [12]]
import numpy as np
arr = np.array([[1, 2], [3, 4], [5, 6], [7, 8], [9, 10], [11, 12]])
newarr = np.hsplit(arr, 2)
print(newarr[0])
print(newarr[1])
# [[ 1]
#  [ 3]
#  [ 5]
#  [ 7]
#  [ 9]
#  [11]]
# [[ 2]
#  [ 4]
#  [ 6]
#  [ 8]
#  [10]
#  [12]]




Sed Command

รันแล้ว Error ว่า ^M: bad interpreter

$ ./scriptname.sh
-bash: ./scriptname.sh: /usr/local/bin/python3.7^M: bad interpreter: No such file or directory

ให้แก้ไขด้วย

sed -i -e 's/\r$//' scriptname.sh

พอรัน ./scriptname.sh อีกครั้งจะใช้ได้ปกติละ

Python OOP

Create a Class

class MyClass:
    x = 5
print(MyClass.x)
# 5

p1 = MyClass()
print(p1.x)
# 5
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    
    def myfunc(self):
        print("Hello my name is " + self.name)

p1 = Person("Jack", 36)
print(p1.name)
print(p1.age)
p1.myfunc()
# Jack
# 36
# Hello my name is Jack

Python Inheritance

class Person:
  def __init__(self, fname, lname):
    self.firstname = fname
    self.lastname = lname

  def printname(self):
    print(self.firstname, self.lastname)

x = Person("John", "Doe")
x.printname()
# John Doe
class Student(Person):
    def __init__(self, fname, lname, year):
        super().__init__(fname, lname)
        self.graduationyear = year

    def welcome(self):
        print("Welcome", self.firstname, self.lastname
              , "to the class of", self.graduationyear)

x = Student("Mike", "Olsen", 2019)
x.printname()
x.welcome()
# Mike Olsen
# Welcome Mike Olsen to the class of 2019




ตัวอย่าง Python

แสดงชื่อไฟล์ในไดเร็กทอรี

เก็บเฉพาะชื่อไฟล์

from os import walk

mypath = 'C:\\'
liFile = []
for (dirpath, dirnames, filenames) in walk(mypath):
    liFile.extend(filenames)
    break # comment this line

for filename in liFile:
    print(filename)
import glob, os
os.chdir(".")
for file in glob.glob("*.*"):
    print(file)

เก็บ full path

from os import walk

mypath = 'C:\\'
filepaths = []
for (dirpath, dirnames, filenames) in walk(mypath):
    for filename in filenames:
        filepaths.append(dirpath + '\\' + filename)
    break # comment this line
for filepath in filepaths:
    print(filepath)

การเขียนไฟล์ text

text = 'abcde'
filename = 'test.txt'
with open(filename, "w", encoding="utf-8") as f:
    f.write(text)

การอ่านไฟล์ text

filename = 'test.txt'
f = open(filename, "r")
text = f.read()
print(text)
f = open(filename, "r", encoding="utf-8")

การอ่านไฟล์ json

import json

filename = 'test.json'
f = open(filename, "r")
response = json.load(f)
print(response)
print(type(response))
print(response.keys())

date

from datetime import date
date.today()
print(type(date.today()))
print(date.today())
# <class 'datetime.date'>
# 2021-10-14

การรวมไฟล์ .csv ที่มี header แบบง่ายๆ

import os
import glob

merge_file = 'people' # folder name
path = "path_to_csv_folder/" + merge_file

os.chdir(path)
all_files = glob.glob("*.*")
# for file in all_files:
#     print(file)

new_text = ''
for f in range(len(all_files)):
    print(f, all_files[f])

    filename = all_files[f]
    file = open(filename, "r", encoding="utf-8")
    text = file.read()
    lines = text.split('\n')

    if f == 0:
        # print('header of the first file')
        new_text = lines[0] + '\n'

    for j in range(1, len(lines)):
        new_text += lines[j] + '\n'

f = open(merge_file + '.csv', "w", encoding="utf-8")
f.write(new_text.strip('\n'))
f.close()

print('Finish')