'소개와 설치/Python'에 해당되는 글 2건

  1. 2021.12.17 Python 3.10.1 릴리즈 출시
  2. 2014.08.08 Python 2.7.8 에 PyGTK 를 설치한 후 GTK 와 PyGTK 의 버전 알아내기

지난 2021년 12월 6일에 Python 3.10.1 이 출시되었다.

 

설치하자 마자 인터프리터 쉘로 간단히 테스트해 보았다.

 

Python 3.10.1 (tags/v3.10.1:2cd268a, Dec  6 2021, 19:10:37) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello~")
Hello~
>>> for i in range(11):
...     print(2**i)
...
1
2
4
8
16
32
64
128
256
512
1024
>>> import math
>>> math.factorial(10)
3628800
>>> math.gamma(11)
3628800.0
>>> math.lgamma(11)
15.104412573075514
>>> math.log(math.gamma(11))
15.104412573075516
>>> math.exp(10)
22026.465794806718
>>> 10**10
10000000000
>>> 10.0**10.0
10000000000.0
>>> math.pow(10.0, 10.0)
10000000000.0
>>> math.exp(10.0*math.log(10.0))
10000000000.00004
>>> math.sqrt(10)
3.1622776601683795
>>> math.cbrt(10)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'math' has no attribute 'cbrt'
>>> math.tan(1)
1.5574077246549023
>>> math.atan(1)
0.7853981633974483
>>> 4*(math.atan(1.0/5)*4 - math.atan(1.0/239))
3.1415926535897936
>>> math.pi
3.141592653589793
>>> math.e
2.718281828459045
>>> math.exp(1)
2.718281828459045
>>> (math.exp(1) - math.exp(-1))/2
1.1752011936438014             ^
SyntaxError: unmatched ')'
>>> math.sinh(1)
1.1752011936438014
>>> math.atan(1)
0.7853981633974483
>>> math.gcd(625, 1000)
125
>>> quit()

 

아직 세제곱근(cube root) 구하는 math.cbrt(float) 함수는 구현되지 않았다.

 

-----------------------------------------------------------------------------

Union 타입 생성  연산자 '|' 기 Python 3.10.x 에 추가되었다.


>>> # Union 타입을 매개변수로 방아 Union 타입을 리턴허눈 험수
>>> # def f(list: list[Union[int, str]], param: Optional[int]) -> Union[float, str]

>>>  # 대신, 다음 구문으로 가능

>>> def f(list: list[int | str], param: int | None) -> float | str:
.>>> ..     pass
...


>> int 타입과 list[int] 타입의 Union 타입

>>> str(int | list[str])
'int | list[str]'



>>> 두 int 타입의 Union 타입
>>> str(int | int)
"<class 'int'>"


>>> Union 타입을 이용헌 인스턴스 확인

>>> isinstance("", int | str)
True


>>> Union 타입을 이용헌 상속 확인

>>> issubclass(bool, int | float)
True

 

>>> # 클래스 상속과 Union 타입 생성 연산자 확인
>>> class M(type):
...         def __or__(self, other): return "Hello"
...
>>> class C(metaclass=M): pass
...

>>> C | int
'Hello'


>>> int | C
int | __main__.C

 

-------------------------------------------------

다음은 아직은 아니지만 다음 릴리즈에 추가될 것 중의 하나이다.

우선 다음 내용을 test,py 라는 파일명으로 자장하고,

x = {'a':{'b':{'c':None}}}
x['a']['b']['c']['d'] = 1

 

프롬프트> python test,py

명령으로 실행합니다. 그러면 다음 에러 메시지가 나온다.

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    x['a']['b']['c']['d'] = 1
TypeError: 'NoneType' object does not support item assignment

 

이 에러 메시지 만으로는 시퀀스 타입 자료에 어느 것이 None 인지 알 수가 없다.

다음 릴리즈에서는 에러 메시지가

Traceback (most recent call last):
  File "test.py", line 2, in <module>
    x['a']['b']['c']['d'] = 1
    ~~~~~~~~~~~^^^^^
TypeError: 'NoneType' object is not subscriptable

로 출력된다. (이는 핸재 Python Pre-Release 3.11.0a3 에서 확인되옸다.)

 

 

 

 

 

 

Posted by Scripter
,

Python 2.7.8 에 PyGTK 를 설치한 후, 

GTK 와 PyGTK 의 버전 알아내기

 

1. 윈도우에서 32 비트 Python 2.7.8 에 PyGTK 밑 모든 관련 파일을 한 번에 내려받아 설치하기:

http://ftp.gnome.org/pub/GNOME/binaries/win32/pygtk/2.22/pygtk-all-in-one-2.22.6.win32-py2.7.msi

 

프롬프트> python
Python 2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.version
'2.7.8 (default, Jun 30 2014, 16:03:49) [MSC v.1500 32 bit (Intel)]'
>>> import pygtk
>>> pygtk.require('2.0')
>>> import gtk
>>> gtk.gtk_version
(2, 22, 1)
>>> gtk.pygtk_version
(2, 22, 0)

 

2. 윈도우에서 64 비트 Python 2.7.8 에 PyGTK 밑 관련 파일들을 내려받아 설치하기:

1) http://www.gtk.org/download/win64.php 에사 하나의 파일로 압축 2.22 버전울 내려 받아 폴더 C:\gtk2 에 압축  해제한후 C:\gtk2\bin 을 환경변수 PATH 에 걸어 둔다.

2) http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygtk 에서 64 비트용 pygtk, pygobject, py2cairo 들을 각각 내려받아 각가의 exe 파일을 더블클릭하여 설치한다.

 

프롬프트> python
Python 2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygtk
>>> pygtk.require("2.0")
>>> import gtk
>>> gtk.gtk_version
(2, 22, 1)
>>> gtk.pygtk_version
(2, 22, 0)
>>> import sys
>>> sys.version
'2.7.8 (default, Jun 30 2014, 16:08:48) [MSC v.1500 64 bit (AMD64)]'

 

* PyGTK 의 태스트 예제: button.py

#!/usr/bin/env python

# example-start buttons buttons.py

import pygtk
pygtk.require('2.0')
import gtk

# Create a new hbox with an image and a label packed into it
# and return the box.

def xpm_label_box(parent, xpm_filename, label_text):
    # Create box for xpm and label
    box1 = gtk.HBox(False, 0)
    box1.set_border_width(2)

    # Now on to the image stuff
    image = gtk.Image()
    image.set_from_file(xpm_filename)

    # Create a label for the button
    label = gtk.Label(label_text)

    # Pack the pixmap and label into the box
    box1.pack_start(image, False, False, 3)
    box1.pack_start(label, False, False, 3)

    image.show()
    label.show()
    return box1

class Buttons:
    # Our usual callback method
    def callback(self, widget, data=None):
        print "Hello again - %s was pressed" % data

    def __init__(self):
        # Create a new window
        self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)

        self.window.set_title("Image'd Buttons!")

        # It's a good idea to do this for all windows.
        self.window.connect("destroy", lambda wid: gtk.main_quit())
        self.window.connect("delete_event", lambda a1,a2:gtk.main_quit())

        # Sets the border width of the window.
        self.window.set_border_width(10)

        # Create a new button
        button = gtk.Button()

        # Connect the "clicked" signal of the button to our callback
        button.connect("clicked", self.callback, "cool button")

        # This calls our box creating function
        box1 = xpm_label_box(self.window, "info.xpm", "cool button")

        # Pack and show all our widgets
        button.add(box1)

        box1.show()
        button.show()

        self.window.add(button)
        self.window.show()

def main():
    gtk.main()
    return 0    

if __name__ == "__main__":
    Buttons()
    main()

 

프롬프트> python button.py

 

 

 

'소개와 설치 > Python' 카테고리의 다른 글

Python 3.10.1 릴리즈 출시  (0) 2021.12.17
Posted by Scripter
,