跳到主要內容

發表文章

目前顯示的是 6月, 2019的文章

Python 上使用 .env 管理環境變數

前言 為了不讓帳號密碼等敏感資訊被寫死在程式碼中,  一般的作法通常是選擇在程式runtime時再去讀環境變數 app.py PYENV = os.environ.get( 'PYENV' ) if PYENV == 'dev' :    print('develop mode')     #do something ... ACCOUNT = os.environ.get( 'ACCOUNT' ) PWD = os.environ.get( 'PWD' ) .bashrc export PYENV=dev export ACCOUNT=andy export PWD=123456 ... 但在開發環境裡, 相同的變數卻同時被多個不同的程式使用時, 直接去使用環境變數就會變成一個問題 使用 python-dotenv 針對這種情況, 可以將所有的變數寫到.env裡面 python-dotenv 會自動幫我們把.env裏頭的值讀進來變成環境變數 .env PYENV=dev ACCOUNT=andy PWD=123456 安裝 可以使用以下指令安裝 pip install -U python-dotenv

[簡易教學]快速建立深度學習的環境 CUDA + Tensorflow + nvidia docker

前言 若想把深度學習的程式碼跑在獨立的環境裡面, 最好的方法就是用docker container來隔離每隻程式, docker的好處這便就不再贅述, 以下示範完整安裝過程 Step 1. 安裝NVidia驅動程式 一開始可以先用ubuntu-drivers devices列出你的顯卡需要裝什麼驅動, 如果沒有特別的偏好, 直接用 autoinstall來裝就可以了 ubuntu-drivers devices sudo ubuntu-drivers autoinstall 安裝完後建議重開機 Step 2. 檢查是否安裝成功 nvidia-smi 可以用來監測GPU的使用率 nvidia-smi 如果驅動有安裝成功, 基本上可以到目前GPU的使用狀況 Step 3. 安裝 CUDA & CUDNN # Add NVIDIA package repository sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/7fa2af80.pub wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1604/x86_64/cuda-repo-ubuntu1604_9.1.85-1_amd64.deb sudo apt install ./cuda-repo-ubuntu1604_9.1.85-1_amd64.deb wget http://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64/nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.deb sudo apt install ./nvidia-machine-learning-repo-ubuntu1604_1.0.0-1_amd64.deb sudo apt update # Install CUDA and tools

nuget restore failed

前言 Nuget 是 .NET 開發人員常用的套件管理工具, 透過 nuget restore 可以很便利地把Visual Studio 專案中參考到的套件抓下來 但執行的過程中若出現以下的錯誤訊息時 MSBuild auto-detection: using msbuild version '16.1.76.45076' from 'C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\bin'. nuget : C:\Users\andy\Documents\ava-deploy\0701\VoiceAssistant\VoiceAssistant\VoiceAssistant.csproj(328,11): error MSB4226: The imported project "C:\Pr ogram Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Microsoft\VisualStudio\v16.0\WebApplications\Microsoft.WebApplication.targets" was not found. Also, tried to find "WebApplications\Microsoft.WebApplication.targets" in the fallback search path(s) for $(VSToolsPath) - "C:\Program Files (x86)\MSBuild\Microsoft\VisualS tudio\v16.0" . These search paths are defined in "C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\bin\msbuild.exe.Config". Confirm that  the path in the <

為什麼執行完npm run test之後畫面會卡住, 每次都要用 ctl+ c 離開

為什麼執行完npm run test之後畫面會卡住, 每次都要用 ctl+ c 離開 到package.json中檢查一下script的test指令 mocha後面是否有帶 --exit的參數 "scripts" : {  "test": " mocha    --exit"   }, Ref: https://github.com/mochajs/mocha/issues/3044

在Hyper V的VM上使用Docker

打開Window Powershell然後輸入以下指令, 就能讓vm也同樣享有虛擬化的功能, 有了虛擬話功能, 就能在VM上面建置Docker 容器 Set-VMProcessor -VMName "Ubuntu_az_cli" -ExposeVirtualizationExtensions $true

Code First Migration: 什麼是Automatic Migration

Automatic Migration 這個功能可以在Configuration.cs中設定 若下所式     internal sealed class Configuration :  DbMigrationsConfiguration<AcerVoiceAssistant.Models.AvaBotDataContext>     {         public Configuration()         {             AutomaticMigrationsEnabled = true;         }         protected override void Seed(AcerVoiceAssistant.Models.AvaBotDataContext  context)         {             //  This method will be called after migrating to the latest version.             //  You can use the DbSet<T>.AddOrUpdate() helper extension method             //  to avoid creating duplicate seed data.        或是在Enable Migration 時代參數如下  enable-migrations  –EnableAutomaticMigration:$true 那Automatic Migration有什麼作用呢? 通常要作Migration首先要執行Add Migration, 把目前跟資料庫中的差異寫到DbMigration裡面(如下)     public partial class Init : DbMigration     {         public override void Up()         {             CreateTable(                 "dbo.Activities",

MigrateDatabaseToLatestVersion: 發布程式時同時更新資料庫

若想要在發布程式的時候連同資料庫一起更新那該如何作? 方法一 在Global.asax檔案裡, 找到protected void Application_Start 加入 initializer MigrateDatabaseToLatestVersion 如下所示    protected void Application_Start()  {     Database.SetInitializer(new MigrateDatabaseToLatestVersion<AvaBotDataContext, Configuration>());     new AvaBotDataContext().Database.Initialize(true); 加入之後, runtime時在資料庫初始化的階段, initializer會去查資料庫中的migration紀錄, 當程式裡的migration比較新就會去套用他來更新資料庫 方法二 直接在發布時勾選 Execute Code First Migration 他的效果跟方法一一樣, 只是不需要額外寫code而是透過config的方式去告訴initializer作跟MigrateDatabaseToLatestVersion一樣的事

快速了解什麼是Code First Migration

什麼是Code First Migration 簡單的說Code First Migration是一種建置管理資料庫的方式, 以往要建立或修改資料庫時, 通常是先寫SQL去作增刪修, 然後再去修改程式裡映射的類別  而Code First Migration的作法恰好相反 先建立映射的類別或是直接修改映射的類別中的屬性,  然後再透過Entity Framework幫我們把差異的部分直接同步回資料庫 基本上有三個步驟, 依序執行底下三個指令 Enable-Migration 初始化Migration 的環境, 指令執行完後專案底下會多 Configuration.cs 用來設定Migration的方式 如: AutomaticMigrationsEnabled = false; Add-Migration <名稱> 這個指令會去抓Web.config中的 connection string, 然後連到DB去檢查Table的schema跟目前程式中映射的類別有無衝突, 然後建立DbMigration類別, 所有的衝突都會被填到DbMigration類別中 以下的例子是執行了Add-Migration Init,     public partial class Init : DbMigration     {         public override void Up()         {             CreateTable(                 "dbo.Employees" ,                 c => new                     {                         Id = c.Int(nullable: false , identity: true ),                         Name = c.String(nullable: false , maxLength: 40),