두부찌개 파이썬( Tofusoup python)

블로그 이미지

tofusoup429

파이썬에 대한 정보를 공유하는 개인 블로그입니다.

'Python'에 해당되는 글 9건

제목 날짜
  • Ubuntu 에 파이선 3.6.3 설치하기 2017.12.17
  • Django when you want to upgrade your django version in venv 2017.12.12
  • 장고 파이썬 전체내용 중 앞부분만 보여주기 (showing a part of content followed by ... in django)) 2017.12.06
  • 파이썬 format을 이용해 초간단하게 1000의 단위마다 콤마 붙이기 (Putting comma every 3 digit in python) 2017.12.06
  • secrets 모듈을 사용해 리스트에서 무작위 item 추출하는 방법 (Extracting a random member from a list) 2017.12.02
  • 라즈베리파이에서 다 수 콘솔 사용하기 2017.10.24
  • 라즈베리파이 SSH활성화 시키고 연결하기 2017.10.22
  • 우분투나 라즈베리 파이에서 파이썬 3.6 or above 설치하기 2017.10.22
  • ast module for converting a List-type-string into a real python list 2017.05.07

Ubuntu 에 파이선 3.6.3 설치하기

Configuration 2017. 12. 17. 10:10

우분투에는 파이썬 3.6.3 이 딸려나오지 않는다. 따라서 직접 다 깔아줘야 한다.


처음 아래의 명령으로  prerequisties 를 깔고...


$ sudo apt-get install build-essential checkinstall
$ sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

아래의 명령으로 python3.6.3 을 다운받고 압축을 푼다. 물론 그 위의 버전이 나온다면 숫자만 바꿔주면 된다.


$ cd /usr/src
$ sudo wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tgz

아래의 명령으로 해당 파일을 시스템에 컴파일한다. 


$ cd Python-3.6.2
$ sudo ./configure
$ sudo make altinstall

잘 깔렸는지 확인해본다.


# python3.6 -V

저작자표시 비영리 변경금지 (새창열림)

'Configuration' 카테고리의 다른 글

우분투나 라즈베리 파이에서 파이썬 3.6 or above 설치하기  (0) 2017.10.22
Posted by tofusoup429

Django when you want to upgrade your django version in venv

카테고리 없음 2017. 12. 12. 15:18

Sometimes, you want to upgrade only a few module in your virtual environment. If that is the case, you can do it just simply with pip.

First, activate the virtual environment that has the modules you want to upgrade is placed.

Second, you just type

"pip3 install <the module name> --upgrade"

Then, the module is updated to the lastest version.


-end-

저작자표시 비영리 변경금지 (새창열림)
Posted by tofusoup429

장고 파이썬 전체내용 중 앞부분만 보여주기 (showing a part of content followed by ... in django))

Django Python 2017. 12. 6. 17:56

장고로 웹페이지를 만드려고 하는데 제목 부분에 본문의 전체내용을 다 보여주기 보다는 처음 일부분만 보여주는게 좋겠다고 생각했다. 그 방법을 찾으려고  Doc을 뒤져보니 truncatecars이라는 template tag가 있다. 아직 사용해 보지는 않았지만 사용방법은 다음과 같다.

{{ value|truncatechars:9}}


이 경우  value 가 "Joel is a slug"라면 "Joel is..."로 나온다고 한다. 아마 ...까지 포함해서 9자로 인식하는 것 같다. 


-End-

저작자표시 비영리 변경금지 (새창열림)
Posted by tofusoup429

파이썬 format을 이용해 초간단하게 1000의 단위마다 콤마 붙이기 (Putting comma every 3 digit in python)

Regular Python 2017. 12. 6. 14:48

숫자를 보기 좋게 만들기 위해서 천의 단위마다 콤마찍는 것이 좋다. 파이썬 스트링 format 메소드를 사용해 아주 간단하게 이게 가능하다.


Putting comma every 3 digit in a number can be done very easily by python's string format method. This is not only for leaving a record for myself in the future and sharing with those who do not know this)


print("{:,}".format(234234234234))


을 실행해보면,

On execution of the code above


'234,234,234,234'


이렇게 나온다. 물론 반환되는 값은 콤마를 포함하는 string이다. 따라서 계산이 다 끝난 최종값을 넣어주는 것이 좋겠다.

(you get that. Of course it returns a string. Thus, you better use this after every calculation has been done. )


이상

(End)



'Regular Python' 카테고리의 다른 글

secrets 모듈을 사용해 리스트에서 무작위 item 추출하는 방법 (Extracting a random member from a list)  (0) 2017.12.02
ast module for converting a List-type-string into a real python list  (0) 2017.05.07
Posted by tofusoup429

secrets 모듈을 사용해 리스트에서 무작위 item 추출하는 방법 (Extracting a random member from a list)

Regular Python 2017. 12. 2. 11:14

리스트에서 무작위로 배열안의 멤버를 추출하는 법을 소개한다.

(I introduce a simple way to extract one random member from a list. with python)


물론 random모듈로,

(Of course, you could do the same thing with random module.)

import random


alist= [1,3,4,5,3,2,1]

n = random.randint(0, len(alist)-1))

print(alist[n])


으로 해도 큰 문제는 없다마는 다음 세줄의 코드를 두줄로 줄일 수 있다.

(There is no problem doing it with random module as you see. But it requires 3 lines besides the import statement.)


python3.6.3에는 secrets이라는 모듈이 새로 생겼다. 훨씬 좋은 난수 발생기라고 한다.

(In python 3.6.3, the latest version as of this writing, there is a new module called secrets. According to the documentation, it produces much better randomness, which I still do not understand exactly.)


The secrets module is used for generating cryptographically strong random numbers suitable for managing data such as passwords, account authentication, security tokens, and related secrets.


import secrets


alist= [1,3,4,5,3,2,1]

print(secrets.choice(alist))


이렇게 하면 좀 더 깔끔하고 쉬운 코드로 무작위 추출을 할 수 있다.

(You can extract one random member from a list only with 2 lines.) Good.



'Regular Python' 카테고리의 다른 글

파이썬 format을 이용해 초간단하게 1000의 단위마다 콤마 붙이기 (Putting comma every 3 digit in python)  (0) 2017.12.06
ast module for converting a List-type-string into a real python list  (0) 2017.05.07
Posted by tofusoup429

라즈베리파이에서 다 수 콘솔 사용하기

기타 2017. 10. 24. 07:49

라즈베리파이에서 하나의 콘솔에서 어떤 프로그램을 실행한 후 다른 일을 하고자 할 때 즉 멀티태스킹을 해야만 할 때 또 다른 콘솔을 오픈해야 하는 경우가 있다.


이 경우  alt + f2 (f3 or f4 ...)를 입력하면 또 다른 콘솔의 로그인 창이 생긴다.


로그인하고 들어가면 명령어를 입력할 수 있는 또 다른 콘솔이 생긴다.


현재 사용하고 있는 모든 콘솔을 확인하려면 명령어 ''w'를 입력해도 되고 'who -al' 을 입력하면 현재 열려있는 모든 콘솔에 대한 정보를 확인할 수 있다.


열려있는 콘솔을 닫으려면 'sudo kill + 닫고자 하는 콘솔의 pid'를 입력하면 된다.





'기타' 카테고리의 다른 글

라즈베리파이 SSH활성화 시키고 연결하기  (0) 2017.10.22
Posted by tofusoup429

라즈베리파이 SSH활성화 시키고 연결하기

기타 2017. 10. 22. 15:44

2016년 부터 라즈베리파이 SSH설정은 DISABLE 이다. 기본적으로 비활성화 되어 있다는 의미이다. 이걸 활성화 시켜야지 다른 컴퓨터에서 라즈베리파이에 접속할 수 있다. 그래야 라즈베리파이에 직접 연결된 모니터 없이 라즈베르파이를 컨트롤 할 수 있다.


활성화(ENABLE)하는 방법은 매우 간단하다.


우선 라즈베리파이의 터미널에서 아래의 명령어를 치면


~$ sudo raspi-config


설정페이지가 나온다.


설정페이지에서


interfacing options  -> SSH 를 선택한 후 Yes를 해주면 된다.


그리고 OK -> FINISH로 빠져나온다.


그리고 클라이언트 컴퓨터 (라즈베리파이를 조종하려고 하는 컴퓨터)의 터미널에서


~$ ssh <raspberrypi ip address> -l pi


를 입력해주고 라즈베리파이 비번을 입력해주면 다른 컴퓨터에서 라즈베리파이에 접속할 수 있다. 참고로 라즈베리파이의 ip address는 라즈베리파이 터미널에서 ifcoinfig 명령어로 확인하면 된다.

'기타' 카테고리의 다른 글

라즈베리파이에서 다 수 콘솔 사용하기  (0) 2017.10.24
Posted by tofusoup429

우분투나 라즈베리 파이에서 파이썬 3.6 or above 설치하기

Configuration 2017. 10. 22. 15:14

"우분투나 라즈베리 파이에서 파이썬 3.6 or above 설치하기"


우부투나 라즈베리안에서 파이썬을 설치할 때 최신버전 (현재 기준 3.6.3)을 설치하고 싶은데...

sudo apt-get update

sudo apt-get install python3


위의 명령어만 입력하면 최신버전이 설치되는 것이 아니라 3.4 버전이 설치된다. 3.4와 3.6이 큰 차이는 없다마는 그래도 왠만 하면 최신버전을 설치하고 싶었다. 인터넷을 뒤져보니 라즈베리파이와 우분투의 경우 3.4까지만 포함시켜놔서 그렇단다. 그래서 dependency 라는 것을 스스로 설치해줘야 한다고 한다. 그럴 땐 이렇게 하면 된다. 


1. apt-get 업데이트 후 dependency 설치하기


$ sudo apt-get update $ sudo apt-get install build-essential tk-dev\

                                     \libncurses5-dev

\libncursesw5-dev

\libreadline6-dev

\libdb5.3-dev

\libgdbm-dev

\libsqlite3-dev

\libssl-dev

\libbz2-dev

\libexpat1-dev

\liblzma-dev

\zlib1g-dev

 

위에 tk-dev에서 zliblg-dev까지가 dependency 이름이다.




2. 파이선3.6  다운로드 후 설치하기


$ wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz $ tar xf Python-3.6.0.tar.xz $ cd Python-3.6.0 $ ./configure $ make $ sudo make altinstall

3. 파이선3.6 이 잘 설치되었는지 확인하기


$ python3.6

제대로 잘 설치 되었다면 Python3.6.0 어쩌고 하고 나올 겁니다.


<Source>


https://gist.github.com/dschep/24aa61672a2092246eaca2824400d37f

'Configuration' 카테고리의 다른 글

Ubuntu 에 파이선 3.6.3 설치하기  (0) 2017.12.17
Posted by tofusoup429

ast module for converting a List-type-string into a real python list

Regular Python 2017. 5. 7. 09:16

The 'literal_eval()' function in module ast, which stands for Abstract Syntax Trees, is pretty useful when you want to turn "a string-formatted-list" into "a real list".


For example, you have a String aString = "['a','b','c']" and you want aString to be a real list, ['a','b','c'] . That is when you want to use the the 'literal_eval()' in ast module. It's a internal module so you do not need to install it additionally.


Example of Usage

Let me show you simple examples of how to use it.

import ast

aString="[3,4,2,'efe',3]"
aList=ast.literal_eval(aString)

First I defined a list-formatted-string and I want to revert it to a real python list, aList.

print(aList)
>> [3, 4, 2, 'efe', 3]
print(alist[1])
>> 4

Boom. It works.


It also works when a list-format string contains dictionaries-format string such as bString="[{'1':'a'},{'2':'b'}]"

import ast

bString = "[{'1':'a'},{'2':'b'}]"
bList = ast.literal_eval(bString)
print(bList)
>> [{'1': 'a'}, {'2': 'b'}]
print(bList[1])
>> {'2': 'b'}
print(bList[1]['2'])
>> b

Boom. It also works.


The strings were converted into real lists and the dictionaries seamlessly.

Hope this one helps

'Regular Python' 카테고리의 다른 글

파이썬 format을 이용해 초간단하게 1000의 단위마다 콤마 붙이기 (Putting comma every 3 digit in python)  (0) 2017.12.06
secrets 모듈을 사용해 리스트에서 무작위 item 추출하는 방법 (Extracting a random member from a list)  (0) 2017.12.02
Posted by tofusoup429
이전페이지 다음페이지
블로그 이미지

파이썬에 대한 정보를 공유하는 개인 블로그입니다.

by tofusoup429

공지사항

    최근...

  • 포스트
  • 댓글
  • 트랙백
  • 더 보기

태그

  • AST
  • GUI
  • Python
  • 키비
  • converting stringto list
  • kivy
  • 파이썬

글 보관함

«   2025/07   »
일 월 화 수 목 금 토
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

링크

카테고리

Python (9)
Regular Python (3)
Django Python (1)
Configuration (2)
Kivy Project (0)
project1. TimerApp (0)
기타 (2)

카운터

Total
Today
Yesterday
방명록 : 관리자 : 글쓰기
tofusoup429's Blog is powered by daumkakao
Skin info material T Mark3 by 뭐하라
favicon

두부찌개 파이썬( Tofusoup python)

파이썬에 대한 정보를 공유하는 개인 블로그입니다.

  • 태그
  • 링크 추가
  • 방명록

관리자 메뉴

  • 관리자 모드
  • 글쓰기
  • Python (9)
    • Regular Python (3)
    • Django Python (1)
    • Configuration (2)
    • Kivy Project (0)
      • project1. TimerApp (0)
    • 기타 (2)

카테고리

PC화면 보기 티스토리 Daum

티스토리툴바