PIXNET Logo登入

夢多了,就會是現實

跳到主文

分享一路上做過的點點滴滴, 記錄種種曾經遇到難關與解決方法 受前人幫忙之餘, 也希望自己能夠盡一點心力幫助他人

部落格全站分類:心情日記

  • 相簿
  • 部落格
  • 留言
  • 名片
  • 9月 29 週三 202111:47
  • Android 讓物件動起來 : animatorSet


本篇來簡單示範如何讓一個底線活潑的動起來,讓使用者直接感受到切換的感覺
 
(繼續閱讀...)
文章標籤

Chris 發表在 痞客邦 留言(0) 人氣(229)

  • 個人分類:Android
▲top
  • 5月 14 週五 202116:01
  • [Python] IOError: [Errno 24] Too many open files:

今天對一隻程式進行壓力測試的時候,跑一個晚上發現Python 拋出這樣一個Exception:
IOError: [Errno 24] Too many open files:
 
這樣的問題很有可能就是某個地方沒有正確的釋放file handle
這時候如果可以知道當下握住哪些檔案就可以很容易知道是哪段程式碼有問題了
在此紀錄兩款方便的小工具
 
psutil (process and system utilities)

可以很簡單地透過程式碼列出當前開啟的檔案

import psutil
proc = psutil.Process()
print proc.open_files()

 

ProcessExplorer

在windows作業系統上,也可以透過工具直接把該exe目前正在使用的檔案一次列出來

打開 Process explorer後,先切換至 View Handles

 

ViewHandles


 


接著再選擇你想要觀察的程式,就可以發現這個程式當下握住那些檔案囉!


FindFiles


 


 
(繼續閱讀...)
文章標籤

Chris 發表在 痞客邦 留言(0) 人氣(749)

  • 個人分類:Python
▲top
  • 6月 17 週日 201815:37
  • [Python] ** 雙星號(double star/asterisk) vs *單星號(star/asterisk) 用法

在看開源碼的時候,很容易看到像這樣的function parameter,附帶一個星號的參數 *args ,或是附帶兩個星號的參數 **kwargs,這是python 裡面很常用的語法,可以讓python接收任意數量的參數
def foo(param1, *args, **kwargs): # 順序不可對調!!
pass
(繼續閱讀...)
文章標籤

Chris 發表在 痞客邦 留言(1) 人氣(19,505)

  • 個人分類:Python
▲top
  • 2月 25 週日 201822:37
  • Python 傳值(pass by value) vs 傳址(pass by address) vs 傳物件(pass by object)?

曾經學過C++的,回頭過來看Python,可能就會誤認為Python 也有pass-by-value, pass by reference的概念就怕會不會因此在參數傳遞時產生大量不必要的運算量而拖慢了系統效能
 
(繼續閱讀...)
文章標籤

Chris 發表在 痞客邦 留言(1) 人氣(12,219)

  • 個人分類:Python
▲top
  • 1月 28 週日 201801:43
  • [C++] 評估程式碼執行時間

#include <stdio.h>
#include <future>
using namespace std;
int main()
{
double START, END;
START = clock();
// [Your code here..]
END = clock();
char szTime[100];
sprintf_s(szTime, 100, "Execute time : %lf - %lf = %lf \n",END, START, END - START);
printf_s(szTime);
system("PAUSE");
}
(繼續閱讀...)
文章標籤

Chris 發表在 痞客邦 留言(0) 人氣(436)

  • 個人分類:C++
▲top
  • 1月 07 週日 201817:19
  • [C++] static_cast 用法說明 (基礎篇)


語法(Syntax) :


static_cast < new_type > ( expression )

Returns a value of type new_type.


 


1.  可用在很多種情況 但是無法去除(constness or volatility. ),最常見的是implicit conversition
例如將float 轉型成 int
 
int main()
{
// initializing conversion
int n = static_cast<int>(3.14);
std::cout << "n = " << n << '\n';
   
    // 一般來說,我們通常都會直接偷懶,寫成這樣
    // int n = (int)3.14;
std::vector<int> v = static_cast<std::vector<int>>(10);
std::cout << "v.size() = " << v.size() << '\n';
system("PAUSE");
}

 
輸出:
n = 3 
v.size() = 10

 
 2.  Downcast: 向下轉型
這樣的需求在C++開發當中常常需要用到,但是必須要特別注意,若使用static_cast則於runtime期間並不會自行做檢查,必須仰賴開法者自行處理(參考static polymorphism),所以是可能發生致命錯誤哦,一般都會建議使用比較安全的 dynamic_cast 作法來進行downcast 。
#include <iostream>
struct CShape {
int m = 0;
void hello() const {
std::cout << "Hello world, this is CShape!\n";
}
};
struct CCircle : CShape {
void hello() const {
std::cout << "Hello world, this is CCircle!\n";
}
};
int main()
{
// static downcast
CShape shape;
CShape& rshape = shape; // upcast via implicit conversion
rshape.hello();
CCircle& another_Circle = static_cast<CCircle&>(rshape); // downcast
another_Circle.hello();
system("PAUSE");
}
輸出:
Hello world, this is CShape!
Hello world, this is CCircle!

 
 
3.  轉換 lvalue-to-rvalue, array-to-pointer, or function-to-pointer
struct B {
int m = 0;
void hello() const {
std::cout << "Hello world, this is B!\n";
}
};
struct D : B {
void hello() const {
std::cout << "Hello world, this is D!\n";
}
};
int main()
{
//array - to - pointer followed by upcast
D a[10];
B* dp = static_cast<B*>(a);
system("PAUSE");
}

 
 
4. 轉換 scoped enum 為 int 或是 float
enum class E { ONE = 1, TWO, THREE };
enum EU { ONE = 1, TWO, THREE };
int main()
{
E e = E::ONE;
int one = static_cast<int>(e);
std::cout << one << '\n';
// int to enum, enum to another enum
E e2 = static_cast<E>(one);
EU eu = static_cast<EU>(e2);
system("PAUSE");
}
 
 
Ref : http://en.cppreference.com/w/cpp/language/static_cast
(繼續閱讀...)
文章標籤

Chris 發表在 痞客邦 留言(0) 人氣(19,510)

  • 個人分類:C++
▲top
  • 8月 11 週四 201600:59
  • Python 基礎系列 map() 用法解說


現在要來看一下map()函式的用法!!
簡單看一下官方文件的簽名:
https://docs.python.org/2/library/functions.html?highlight=map#map
 

  • map(function, iterable, ...)

  • Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended with None items. If function is None, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.

  •  

  • 簡單的來說,就是定義一個function。接著用這個function來對一個iterable 的物件內每一個元素做處理,

  • 光看文字可能還是很難懂,直接看例子吧~!!


 
我們可以輕易地使用map來對list裡面每一個元素的資料型態做轉換,
原本有一個資料型態為字串(str)的listA , 只要設定map函式第一個參數int ,第二個參數為要轉換的list
意思就是將list裡面的每一個元素都轉型成整數(int)

>>> listA = ['1','2','3']
>>> print map(int,listA)
[1, 2, 3]


 
當然,也可以自訂函式來做各種不一樣的運算
例如定義一個multiple2的函式,透過map我們就能夠對list每一個元素做 '乘2'
其實map這個函式的功能也能夠用list comprehensive 來達成。(第8行)

>>> def multiple2(x):
... return x*2
...
>>> list1 = [1,3,5,7,9]
>>> print map(multiple2,list1)
[2, 6, 10, 14, 18]
>>>
>>> print [multiple2(x) for x in list1]
[2, 6, 10, 14, 18]


 
map也可以同時接收多個list做處裡
下面宣告三組list,一個三個參數的函式 adder(x,y,z),加總後回傳。
理所當然的,也能夠使用list comprehensive來完成。(第11行)

>>> def adder(x,y,z):
... return x+y+z
...
>>> list1 = [1,3,5,7,9]
>>> list2 = [2,4,6,8,10]
>>> list3 = [100,100,100,100,100]
>>>
>>> print map(adder,list1,list2,list3)
[103, 107, 111, 115, 119]
>>>
>>> print [adder(x,y,z) for x,y,z in zip(list1,list2,list3)]
[103, 107, 111, 115, 119]


(繼續閱讀...)
文章標籤

Chris 發表在 痞客邦 留言(1) 人氣(60,611)

  • 個人分類:Python
▲top
  • 7月 19 週二 201615:55
  • C# 陣列排序


在C# 裡面有提供內建的排序方法,內部使用Quicksort algorithm,這邊介紹三種解法對不同的需求進行排序。
  • Primitive types

  • Custom types using delegate

  • Custom type using ICompareable

  • Array.Sort()

     
    int,double,string 都能夠輕易地使用Array.Sort(Array)來做排序,因為他們都繼承IComparable interface.
    現在來看第一個例子
     
    排序整數

    // sort
    int arrayint[] intArray = new int[5] { 8, 10, 2, 6, 3 };
    Array.Sort(intArray);
    // write array
    foreach (int i in intArray) Console.Write(i + " "); // output: 2 3 6 8 10


     
    排序字串

    // sort string array
    string [] stringArray = new string[ 5 ] { "Banana" , "Watermelon" , "Tomato" , "Water" , "Apple" };
    Array.Sort( stringArray );
    // write array
    foreach ( string str in stringArray) Console .Write ( str + " " ); // output: Apple Banana Tomato Water Watermelon


     
     
     
    有時候我們需要排序的不僅只是單一個型態,而是一具有多屬性的物件,並且針對其中一個屬性作排序。
    這時候就需要用到 delegate 搭配 anonymous method 來完成,使用Comparison<T>的delegate,定義如下
     

    public delegate int Comparison<T> (T x, T y)


     
    可以看到該泛型Comparesion 傳入兩個相同型態的參數,回傳值為:
    <0 : X < Y
    0:X = Y
    >0 : X > Y
     
    接下來示範排序一個自訂義型態物件的陣列。我們會用到上面提到的匿名委派方法。
     
    定義一個類別具有兩個屬性Name,Height

    class Student
    {
    public string Name { get ; set ; }
    public int Height { get ; set ; }
    }


     
     
    使用delegate搭配 anonymous method 來進行排序,這裡依照Height屬性由小到大來排序

    Student [] studentAry = new Student[]{
    new Student { Name = "A1" , Height= 180 },
    new Student { Name = "B1" , Height= 170 },
    new Student { Name = "C1" , Height= 175 }
    };
    Array.Sort( studentAry , delegate ( Student s1 , Student s2)
    {
    return s1.Height.CompareTo(s2 .Height );
    });
    foreach ( Student str in studentAry)
    {
    Console.WriteLine( str. Name + " " + str .Height );
    }
    //B1,C1,A1


     
     
  • 使用IComparable來排序陣列

  • 延續上一個議題,我們還可以實作IComparable interface來對一個具有多屬性的物件進行排序,當我們實作IComparable後,Sort內部會呼叫IComparable.ComareTo方法,所以我們必續自己去實現裡面的功能。
     
    把原本的Student實作IComparable,實作一個CompareTo方法。

    class Student : IComparable
    {
    public string Name { get ; set ; }
    public int Height { get ; set ; }
    public int CompareTo( object obj )
    {
    if (obj is Student )
    {
    return this.Height.CompareTo ((obj as Student ).Height );
    }
    throw new ArgumentException ("object is not Student" );
    }
    }


     
     
    而在呼叫端的部分,用法上就跟最初提到的Primitive type 一樣,一行解決!!!

    Student [] studentAry = new Student[]{
    new Student { Name = "A1" , Height= 180 },
    new Student { Name = "B1" , Height= 170 },
    new Student { Name = "C1" , Height= 175 }
    };
    Array.Sort( studentAry );
    foreach ( Student str in studentAry)
    {
    Console .WriteLine ( str. Name + " " + str . Height);
    }
    //B1,C1,A1




     


     
    翻譯自 :http://www.csharp-examples.net/sort-array/
     
     
     
     
    (繼續閱讀...)
    文章標籤

    Chris 發表在 痞客邦 留言(0) 人氣(31,910)

    • 個人分類:C# 小筆
    ▲top
    • 6月 25 週六 201604:51
    • 如何使用 C# 取得 Arduino Yun 的輸出資料

    Arduino Yun是一個非常強大的開發版,人人都會用~!
    但是總是透過C#來取得傳出來的資訊就絕對不是人人都曉得囉
    這裡要來介紹怎麼樣使用 C# 來取得 Arduino 使用Serial.print() 傳出來的資料
    (繼續閱讀...)
    文章標籤

    Chris 發表在 痞客邦 留言(0) 人氣(5,132)

    • 個人分類:C# 小筆
    ▲top
    • 6月 04 週六 201614:33
    • 在visual studio2013 / 2015 使用XNA


    在原生的Visual 2013並沒辦法直接開始XNA的專案,不曉得是什麼原因,難道是因為XNA要被放棄了嗎?
     
    網路上還是有許多不同的方式能夠讓Visaul2013/2015使用XNA
     
    很幸運地已經有人寫出一個腳本,
     
    輕輕鬆鬆滑鼠左鍵按兩下,等它安裝完後就可以讓你的Visaul2013/2015操控XNA囉~!!!    
    >> 點我下載

     
    腳本分成2015版/2013版且各有32/64為原版本,依各自不同的需求取用囉~!
     
     
    注意:
  • 我的作業系統是Win10 / X64,下載完後要先對安全性解除封鎖,對該檔案按右鍵 > 內容 > 一般 > 安全性 > 解除封鎖


  • 建議XnaFor2013移到D:\ ,(筆者在C:\執行時會出現錯誤,猜想是因為win10權限的關係),接著再以系統管理員打開PowerShell,執行下面兩行指令

  • 輸入 cd D:\

  • 輸入 .\XnaFor2013.ps1

  • 等候幾分鐘後看到 Installation Complete就完成啦~!!,夠簡單了吧!!

  •  

     

     


    How can I use XNA with Visual Studio Express 2013 for Windows?


    (繼續閱讀...)
    文章標籤

    Chris 發表在 痞客邦 留言(0) 人氣(122)

    • 個人分類:Visual studio 技巧
    ▲top
    12»

    熱門文章

    • (308)SQL 重點語法快速上手(一)
    • (10,877)C# DataTable 合併兩張資料表
    • (3,678)C/C++ : signed int 和 int 宣告差別在哪裡?
    • (9,011)WPF C# DispatcherTimer 用法
    • (628)WPF - 使用DrawingBrush繪製色彩相間的地板/棋盤格
    • (5,132)如何使用 C# 取得 Arduino Yun 的輸出資料
    • (31,910)C# 陣列排序
    • (60,611)Python 基礎系列 map() 用法解說
    • (19,510)[C++] static_cast 用法說明 (基礎篇)
    • (12,219)Python 傳值(pass by value) vs 傳址(pass by address) vs 傳物件(pass by object)?

    文章分類

    • Android (1)
    • Python (4)
    • Visual studio 技巧 (1)
    • C++ (3)
    • C# 小筆 (5)
    • 心情小筆 (0)
    • SQL 零基礎上手 (5)
    • 未分類文章 (1)

    最新文章

    • Android 讓物件動起來 : animatorSet
    • [Python] IOError: [Errno 24] Too many open files:
    • [Python] ** 雙星號(double star/asterisk) vs *單星號(star/asterisk) 用法
    • Python 傳值(pass by value) vs 傳址(pass by address) vs 傳物件(pass by object)?
    • [C++] 評估程式碼執行時間
    • [C++] static_cast 用法說明 (基礎篇)
    • Python 基礎系列 map() 用法解說
    • C# 陣列排序
    • 如何使用 C# 取得 Arduino Yun 的輸出資料
    • 在visual studio2013 / 2015 使用XNA

    動態訂閱

    文章搜尋

    參觀人氣

    • 本日人氣:
    • 累積人氣: