반응형
  • 이미 가지고 있는 테이블을 기반으로 장고 마이그레이션이 필요할 경우 아래의 명령어로 테이블 스키마 기준 models 데이터를 생성 할 수 있다.
python manage.py inspectdb > models.py

 

반응형

반응형

Windows에서 pip install mysqlclient 설치시 아래와 같은 에러 메시지가 반환된다면

fatal error C1083: 포함 파일을 열 수 없습니다. 'mysql.h': No such file or directory
fatal error C1083: Cannot open file: 'mysql.h': No such file or directory

 

아래의 URL에 접속 해서 비공식 바이너리를 받아서 pip로 설치하면 해결이 가능하다.

https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysqlclient

 

접속해서 본인의 python 버전과 OS에 맞는 바이너리를 다운로드 후

Mysqlclient: a fork of the MySQL-python interface for the MySQL database.

mysqlclient‑1.4.6‑pp373‑pypy36_pp73‑win32.whl

mysqlclient‑1.4.6‑cp38‑cp38‑win_amd64.whl

mysqlclient‑1.4.6‑cp38‑cp38‑win32.whl...

 

whl파일로 수동설치

pip install C:\Users\xxx\Downloads\mysqlclient-1.4.6-cp37-cp37m-win32.whl

수동 설치 진행시 아래의 에러메시지가 반환 된다면,

OS 또는 pip 버전의 차이가 있을 수 있으니 pip를 최신버전으로 업데이트 하자.

mysqlclient-1.4.6-cp37-cp37m-win32.whl is not a supported wheel on this platform.

 

pip 최신 버전 업데이트

pip install --upgrade pip

 

pip 설치 시 아래와 같은 에러가 발생 한다면 호환성 이슈이므로 강제 재설치로 최신 버전으로 업데이트 하자

Exception:
Traceback (most recent call last):
  File "c:\workon\mysite3.8\lib\site-packages\pip-19.0.3-py3.8.egg\pip\_internal\cli\base_command.py", line 179, in main
    status = self.run(options, args)
  File "c:\workon\mysite3.8\lib\site-packages\pip-19.0.3-py3.8.egg\pip\_internal\commands\install.py", line 384, in run
    installed = install_given_reqs(
  File "c:\workon\mysite3.8\lib\site-packages\pip-19.0.3-py3.8.egg\pip\_internal\req\__init__.py", line 53, in install_given_reqs
    requirement.install(
  File "c:\workon\mysite3.8\lib\site-packages\pip-19.0.3-py3.8.egg\pip\_internal\req\req_install.py", line 910, in install
    self.move_wheel_files(
  File "c:\workon\mysite3.8\lib\site-packages\pip-19.0.3-py3.8.egg\pip\_internal\req\req_install.py", line 437, in move_wheel_files
    move_wheel_files(
  File "c:\workon\mysite3.8\lib\site-packages\pip-19.0.3-py3.8.egg\pip\_internal\wheel.py", line 544, in move_wheel_files
    generated.extend(maker.make(spec))
  File "c:\workon\mysite3.8\lib\site-packages\pip-19.0.3-py3.8.egg\pip\_vendor\distlib\scripts.py", line 405, in make
    self._make_script(entry, filenames, options=options)
  File "c:\workon\mysite3.8\lib\site-packages\pip-19.0.3-py3.8.egg\pip\_vendor\distlib\scripts.py", line 309, in _make_script
    self._write_script(scriptnames, shebang, script, filenames, ext)
  File "c:\workon\mysite3.8\lib\site-packages\pip-19.0.3-py3.8.egg\pip\_vendor\distlib\scripts.py", line 245, in _write_script
    launcher = self._get_launcher('t')
  File "c:\workon\mysite3.8\lib\site-packages\pip-19.0.3-py3.8.egg\pip\_vendor\distlib\scripts.py", line 384, in _get_launcher
    result = finder(distlib_package).find(name).bytes
AttributeError: 'NoneType' object has no attribute 'bytes'

 

pip 강제 최신 업데이트

easy_install -U pip
반응형

반응형

오랜만에 AWS EC2에 Python을 설치하는데 아래와 같은 에러가 발생한다.

 

$ pyenv install 3.6.9
Downloading Python-3.6.9.tar.xz...
-> https://www.python.org/ftp/python/3.6.9/Python-3.6.9.tar.xz
Installing Python-3.6.9...

BUILD FAILED (Amazon Linux AMI 2018.03 using python-build 1.2.13-4-g6563b64d)

Inspect or clean up the working tree at /tmp/python-build.20190901070310.7044
Results logged to /tmp/python-build.20190901070310.7044.log

Last 10 log lines:
checking for --with-universal-archs... no
checking MACHDEP... linux
checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/tmp/python-build.20190901070310.7044/Python-3.6.9':
configure: error: no acceptable C compiler found in $PATH

 

아래와 같이 gcc 설치로 해결하자. 

$sudo yum install gcc

반응형

반응형

배열의 특징

  • 자신과 동일한 타입으로 초기화 될 수 없다.
  • 배열의 이름은 배열의 첫번째 요소의 주소로 암시적 형 변환 된다.
  • 배열을 가리키는 참조를 만들 수 있다.

함수 템플릿을 만들때

  • 배열을 값으로 받으면 T는 요소 타입의 포인터로 결정된다.
  • 배열을 참조로 받으면 T는 배열 타입으로 결정된다.
#include <iostream>

template<typename T>
void foo(T a)
{

}

template<typename T>
void goo(T& a)
{

}

int main()
{
    int x[3] = { 1,2,3 };

    foo(x); 
    goo(x);
}

 

관련 예제

  • 문자열의 타입 : char 배열
  • 문자열을 값으로 받으면 T는 const char* 결정되고, 참조로 받으면  const char[]로 결정된다.
  • 크기가 다른 배열은 다른 타입이다.
#include <iostream>

template<typename T>
void foo(T a, T b)
{

}

template<typename T>
void goo(T& a, T& b)
{

}

int main()
{
    foo("orange", "apple"); // ok
    goo("orange", "apple"); // error
}

 

반응형

반응형

배열의 이름과 주소

일반적으로 데이터 타입을 뺀 나머지는 변수명이라 할 수 있다.

int i에서 int를 빼면 i가 변수명이다. 그리고 데이터 타입에 *붙여서 선언하면 주소를 담을 수 있는 포인터 변수를 선언 할 수 있다. 그러면 배열을 어떨까?

 

int x[3] 에서 int[3] 뺀 나머지인 x가 동일하게 변수명이다. 그러면 포인터 변수는 마찬가지로 x 변수에 *만 붙이면 배열의 포인터 주소가 되는 것일까?

 

대부분 배열의 주소는 첫번째 요소의 주소로 많이 알고 있다. 하지만 배열의 주소와 첫번째 요소의 주소는 같으면서도

다르므로 명확히 구분해서 사용할 필요가 있다.

#include <iostream>

int main()
{
    int i = 1;
    int *px = &i;

    int x[3] = { 1, 2, 3 };
    int *p1 = x; // 일반적으로 생각하는 배열의 주소(묵시적으로 첫번째 요소의 주소로 형변환 됨)
    int(*p2)[3] = &x; // 정확한 배열의 주소(요소가 모두 포함된 전체 컨테이너의 주소)

    // + 1 연산을 하면 기본적으로 데이터 타입 사이즈 만큼 증가된 메모리 주소를 가르침
    printf("%p, %p\n", p1, p1 + 1); // 배열의 첫번째 요소의 주소에서 데이터 사이즈인 4바이트 증가
    printf("%p, %p\n", p2, p2 + 1); // 요소가 3개인 배열의 주소에서 데이터 사이즈인 12바이트 증가
}

 

반응형

반응형

Template Argument Type Deduction

  • 컴파일러가 함수 인자를 보고 템플릿의 타입을 결정하는 것을 말한다.
  • 함수 인자의 타입과 완전히 동일한 타입으로 결정되지는 않는다.
#include <iostream>
using namespace std;

// 함수 템플릿 인자가 값 타입(T a) 일때
template<typename T> void foo(T a)
{
    ++a;
}

int main()
{
    int n = 0;
    int& r = n;
    const int c = n;
    const int& cr = c;

    foo(n);  // T : int
    foo(c);  // T : const int ?
    foo(r);  // T : int& ?
    foo(cr); // T : const int& ?
}

Template Argument Type Deduction 원리 1

  • 템플릿 인자가 값 타입일때 (T a)
    • 함수 인자가 가진 const, volatile, reference 속성을 제거하고 T의 타입을 결정한다.
    • 주의 - 인자가 가진 const 속성만 제거 된다.
#include <iostream>
using namespace std;

// 함수 템플릿 인자가 값 타입(T a) 일때
template<typename T> void foo(T a)
{
}

int main()
{
    int n = 0;
    int& r = n;
    const int c = n;
    const int& cr = c;

    foo(n);  // T : int
    foo(c);  // T : int
    foo(r);  // T : int
    foo(cr); // T : int
    
    const char* s1 = "hello";
    foo(s1); // T : char const* // s1이 아닌 char*가 const 이므로 제거 되지 않음
    
    const char* const s2 = "hello";
    foo(s2); // T : char const*  // s2가 const 이므로 인자에 대한 const만 제거됨
}

Template Argument Type Deduction 원리 2

  • 템플릿 인자가 참조 타입일때 (T& a)
    • 함수 인자가 가진 reference 속성을 제거하고 T의 타입을 결정한다.
    • const, volatile 속성은 유지한다.
#include <iostream>
using namespace std;

// 함수 템플릿 인자가 참조 타입(T& a) 일때
template<typename T> void foo(T& a)
{
    ++a;
}


int main()
{
    int n = 0;
    int& r = n;
    const int c = n;
    const int& cr = c;

    foo(n);  // T : int
    foo(c);  // T : const int
    foo(r);  // T : int
    foo(cr); // T : const int
}

 

Template Argument Type Deduction 정리

  • 템플릿 인자가 값 타입(T a)
    • 함수 인자가 가진 const, volatile, reference 속성 제거 후 T 타입 결정
    • 인자의 const 속성만 제거
  • 템플릿 인자가 참조 타입(T& a)
    • 함수 인자가 가진 reference 속성만 제거 후 T 타입 결정
    • const, volatile 속성 유지
    • 인자가 (const T& a)경우 const를 제거하고 T 타입 결정
  • 템플릿 인자가 forwarding 레퍼런스 타입(T&& a)
    • lvalue, rvalue 모두 전달 받음
  • 템플릿 인자가 배열
    • argument decay 발생
반응형

반응형

std::typeid 활용

  • C++ 표준의 typeid() 연산자 사용
    • const, volatile, reference 구분하여 조사할 수 없음
#include <iostream>
using namespace std;

template<typename T> void foo(const T a)
{
    cout << "T : " << typeid(T).name() << endl;
    cout << "a : " << typeid(a).name() << endl;
}

int main()
{
    foo(3);
    foo(3.3);
}

결과

int
int
double
double

 

boost:type_index 활용

  • boost::type_id_with_scv<T>().pretty_name() 사용
  • const, volatile, reference 구분하여 조사할 수 있음
  • <boost/type_index.hpp>
  • namespace boost::typeindex 안에 포함
  • 변수의 타입을 조사 할때는 decltype()을 이용
    • type_id_with_cvr<decltype(a)>().pretty_name()
  • boost 사용법 참고 : https://www.devoops.kr/77
#include <iostream>
#include <boost/type_index.hpp>
using namespace std;
using namespace boost::typeindex;

template<typename T> void foo(const T a) // 실제 템플릿 타입 T와 변수타입은 다를 수 있음
{
    //cout << "T : " << typeid(T).name() << endl; // int, double
    //cout << "a : " << typeid(a).name() << endl; // int, double : std의 typeid로는 구분 불가
    cout << type_id_with_cvr<T>().pretty_name() << endl;
    cout << type_id_with_cvr<decltype(a)>().pretty_name() << endl;
}

int main()
{
    foo(3);
    foo(3.3);
}

결과

int
int const
double
double const

 

 

 

반응형

반응형

boost 설치

아래의 boost 사이트에 접속하여 런타임 플랫폼에 맞는 설치 파일 다운로드

https://www.boost.org/users/download/

압축파일을 적당한 위치에 해제한다

대부분의 기능은 헤더파일 참조로 사용 가능하지만 일부 기능은 사전 빌드가 필요하므로 제공하는 배치 파일과 빌드 실행파일을 이용하여 빌드를 진행하자.

  • 빌드 실행 파일 생성 : bootstrap.bat(실행 시 b2.exe 생성됨)
  • 빌드 실행 : b2.exe(성공적으로 빌드가 완료되면 stage/lib 디렉토리에 다양한 파일이 생성됨)

비주얼 스튜디오 > 프로젝트 > 속성

속성 > VC++ 디렉터리 > 일반 > 포함 디렉터리 > 편집

포함 디렉터리 > 줄 추가 > ...찾기 > boost 폴더 선택

속성 > VC++ 디렉터리 > 일반 > 라이브러리 디렉터리 > 편집

라이브러리 디렉터리 > 줄 추가 > ...찾기 > boost 폴더/stage/lib 선택

 

이제 boost 라이브러리에 포함된 기능을 include하여 사용하자

#include "pch.h"
#include <iostream>
#include <boost/any.hpp> // 관행상 boost 폴더를 포함
using namespace std;

int main()
{
    boost::any a1 = 1;
    boost::any a2 = 1.1;
    boost::any a3 = "aaa";

    const char* s = boost::any_cast<const char*>(a3);
}

 

 

 

 

 

 

 

 

반응형

+ Recent posts