0%

今天來分享如何應用 virtual env 來提高工作環境等效率。如果使用 python library 我想信很多時候都會用 pip 來安裝他的 library 等 module。如果某些套件版本跟你專安不 match 造成無法 code 無法運行怎麼呢?這時你只有降版本,也就是可能要先移除再安特殊版本。另一種方式就是建立一個 virtual env 也就是類似虛擬環境,這裡可以安裝任何 package or library 跟主環境分離不會有相關。

我覺得這個很好用,如果你原本主環境安裝太多 library 你有時還要 debug,你可以一開就建立在乾淨環境,再安裝你要安裝個 library or module 等 package。

可以用這兩終方式建立 virtualenv,如:

  • virtualenv : 需要安裝套件
  • venv:內建就有無須安裝特建

venv

Syntax: python -m venv <my_env_name>
EX: python -m venv venv

  • 如何用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#create new directory
mkdir newproject
cd newproject

#create virtual env
py -m venv env_apitest

#activate
.\.env_apitest\Scripts\activate

#deactivate
deactivate

# upgrade python version
python -m venv /path/to/venv --upgrade

virtualenv

Read more »

今天想要來分享如何在 window 上面設定像 Linux 或 Mac 相關花麗等冬端機 terminal,和以些 linux 不錯的指令。如果你們有用過 Linux,在用 window 很不習慣,尤其是指令。今天想分享如何用 Oh-my-posh 在 window 上面可以跟 Linux 的 Oh-my-zsh 有依樣效果。Window 是用在 powershell 上,他有支援很多 shell bash zsh powershell fish等等。我有寫一篇比較完整,在Medium 因此今天會寫簡短方法。

安裝工具

軟體安裝服務比較

如果你有用過 Ubuntu 都會看到大家常用 apt-get install 這命令,這個就是我稱為的安裝服務工具。他可以很快速幫助你要安裝個套件。我今天要介紹 3 個,但我會以 winget 優先用,如果 winget 沒有套件再選其他兩個。winget 是微軟開發因此我才會優先用他。我會盡可能不去商店下載。

正常來說 winget 預設鏡安裝了,如果你是 win10 都會安裝,如果指令找不到就用下方的方法。如果 winget有,那把 scoop安裝,Chocolatey就看你要挨裝嗎,我目前沒用到。

  • Winget
    先到這裡下載 ,檔案會是 Microsoft.DesktopAppInstaller_8wekyb3d8bbwe.msixbundle然後用這個指令安裝:
1
2
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Add -AppPackage
1
2
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Invoke-RestMethod -Uri https://get.scoop.sh | Invoke-Expression
Read more »

今天有想在這裡分享 Regular Expression,這也是大家不想碰到的,可就是一個很復雜的就越耗用。
我也沒有很勵害,但之前開發自動化,用很多。因此想藉由這編文章來寫下我所寫道的筆記和參考很多教學。

在介紹之前我們在 python 都會用到 re 或是 rex ,但在這我只會分享用 re,也就是要 import re,不然不能用。

常用的 req module

Module 說明
match Determine if the RE matches at the beginning of the string.
search Scan through a string, looking for any location where this RE matches
findall Find all substrings where the RE matches, and returns them as a list.
finditer Find all substrings where the RE matches, and returns them as an iterator.

compile()

1
2
3
4
5
6
import re
text ='https://chenchih.page , Author is ChenChih'
pattern=re.compile('Chenchih',re.I)
print(pattern.match(text)) #None
print(pattern.search(text).group()) #chenchih
print(pattern.findall(text)) #['chenchih', 'ChenChih']

output:

None
cheenchih > > ['chenchih', 'ChenChih']

match()

This function matches a regular expression pattern at the beginning of a string. If the pattern is found, it returns a match object, otherwise, it returns None.

1
2
3
4
5
import re
teststring="123abc456789abc123ABC"
pattern=re.compile(r"abc")
match=pattern.match(teststring)
print(match)
Read more »

我想分享一些基本的DB(MYSQL)語法。之前面試有考不管是在Amazon,或Microsoft 都有考過我DB。我那時有準備筆記,但後來就不知跑哪。今天想在這篇寫關於SQL基本指令,有機會再寫高階的語法。

我住前是用MYSQL的DB軟體,你們可以選用其他的如MogoDB, Microsoft SQL server,orlacle DB等等。基本上語法都差不多,我猜的

  • Table: employee:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    +----+-------+--------+--------------+
    | id | Name | Salary | DepartmentID |
    +----+-------+--------+--------------+
    | 1 | Joe | 85000 | 1 |
    | 2 | Henry | 80000 | 2 |
    | 3 | Sam | 60000 | 1 |
    | 4 | Max | 90000 | 1 |
    | 5 | Janet | 69000 | 1 |
    | 6 | Randy | 85000 | 1 |
    +----+-------+--------+--------------+

SELECT & FROM

select * from

select 可以選擇要印那一欄,如果加*可以把所有內如印出來,以下是指令:

select * from employee;

1
2
3
4
5
6
7
8
9
10
11
Table: employee;
+----+-------+--------+--------------+
| id | Name | Salary | DepartmentID |
+----+-------+--------+--------------+
| 1 | Joe | 85000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 1 |
| 4 | Max | 90000 | 1 |
| 5 | Janet | 69000 | 1 |
| 6 | Randy | 85000 | 1 |
+----+-------+--------+--------------+

select Name 指定欄位

SELECT Name, Salary From Employee;

Read more »

An unorder collection of a unquie object. No duplicate value.
不可以有重複的直,一般都是用在帳號上面。

Create set

1
2
my_set= {1,2,3,4,5}
print(my_set)

output:

{1, 2, 3, 4, 5}

如果加重複會用最前面的

1
2
my_set= {1,2,3,4,5,5}
print(my_set)

output:

{1, 2, 3, 4, 5}

adding set

1
2
3
4
5
my_set= {1,2,3,4,5,5}
my_set.add(100)
my_set.add(2)
print(my_set)

output

‘{1, 2, 3, 4, 5, 100}`

Read more »

今天想分享如何用 dictionary 字典方式存資料。

key and Value 如何使用宣槓取直

  • collection of of {key:value} pairs
  • no duplicate
  • allow changeable and ordered

declare dictionary 宣告 dict

It's an unorder , it's not next to each other, so we can't use index like we use in list. Dictionary will have a a key and value look like this:

syntax:

declare: dictname= {key:value}
get key: print(dictname['key'])

Access and Get value 取我們的直:

1
2
3
4
dict={
'keyA':1,
'keyB':2
}

取 key 的直: print(dict['keyA']) => 1
取所有直 print(dict) => {'keyA': 1, 'keyB': 2}

宣告不同 data type

Read more »

Tuple is like list, but we can;t modify it, they are immutable. Tuple immutable 直,就是他的直不無法修改的,有些語法是用const類似這概念。

Create Tuple 建立tuple

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Empty tuple
my_tuple = ()
print(my_tuple) #()

# Tuple having integers
my_tuple = (1, 2, 3)
print(my_tuple) #(1,2,3)

# tuple with mixed datatypes
my_tuple = (1, "Hello", 3.4)
print(my_tuple) #(1, "Hello", 3.4)

# multiply value
x,y,z, *other = (1,2,3,4,5)
print(f"x: {x}, y:{y}, z:{z}, other:{other}")
#x: 1, y:2, z:3, other:[4, 5]

my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple) #('mouse', [8, 4, 6], (1, 2, 3))

created without using parentheses

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible
a, b, c = my_tuple

print(a) # 3
print(b) # 4.6
print(c) # dog

my_tuple = ("hello")
print(type(my_tuple)) # <class 'str'>

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple)) # <class 'tuple'>

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple)) # <class 'tuple'>

Access Tuple Elements

1
2
3
4
5
6
7
8
9
10
11
12
# Accessing tuple elements using indexing
my_tuple = ('p','e','r','m','i','t')

print(my_tuple[0]) # 'p'
print(my_tuple[5]) # 't'

# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

# nested index
print(n_tuple[0][3]) #'s'
print(n_tuple[1][1]) # 4

# Negative indexing for accessing tuple elements

1
2
3
4
5
6
7
8
my_tuple = ('p', 'e', 'r', 'm', 'i', 't')

# Output: 't'
print(my_tuple[-1])
# elements beginning to 2nd
# Output: ('p', 'r')
print(my_tuple[:-7])

Slicing

Read more »

python 有很多不同 function,因此我想在這篇寫所有 funtion 我會用到。我指的 function 不是傳 function,而是一些 libary function

Math

Datetime 時間

time

程式跑的時間

如果計算程式袍多就可以用這方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import time

# starting time
start = time.time()
# your code
for i in range(1,60):
print(i)

#endtime
end = time.time()
runtime=end - start
print(" Runtime of the program is {runtime:%.8f}")
print(f"Runtime of the program is {runtime:.5f}")
#or
print(f"Runtime of the program is {(end - start):.5f}")

datetime

今天日期

  • date.today 取今天日期
Read more »

Pandas 筆記

今天我想分享我學的 pandas 筆記,我覺得對於分大數據很有幫助,他有點像 mysql。請先下載 module 或 package:pip install pandas`

Part1 基本語法

連線 CSV 檔案

可以去 github 找相關 csv 檔案,然後去 raw 檔案做連線

1
2
3
4
5
import pandas as pd
url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv"
tips = pd.read_csv(url)

print(tips)

pandas 會回傳 DataFrame 格式。<class 'pandas.core.frame.DataFrame'>
下面是印出所有資料表內容。

1
2
3
4
5
6
7
8
9
10
11
12
     total_bill   tip     sex smoker   day    time  size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
.. ... ... ... ... ... ... ...
239 29.03 5.92 Male No Sat Dinner 3
240 27.18 2.00 Female Yes Sat Dinner 2
241 22.67 2.00 Male Yes Sat Dinner 2
242 17.82 1.75 Male No Sat Dinner 2
243 18.78 3.00 Female No Thur Dinner 2

Head and Tail

  • head(N): 是查前面 N 筆資料
Read more »