num = -20 print(abs(num))
num = -30.33 などにしても結果は同じ
ソフトウェアエンジニアの技術ブログ:Software engineer tech blog
随机应变 ABCD: Always Be Coding and … : хороший
num = -20 print(abs(num))
num = -30.33 などにしても結果は同じ
def OP_NOP(): print("This is OP_NOP, 76") def OP_IF(): print("This is OP_IF, 99") def OP_NOTIF(): print("This is OP_NOTIF, 100") OP_CODE = { 76: OP_NOP, 99: OP_IF, 100: OP_NOTIF } func = OP_CODE[76] func()
$ python3 test.py
This is OP_NOP, 76
bitcoinのOPcode一覧はこちら
https://en.bitcoin.it/wiki/Script
めちゃくちゃお洒落な書き方だな~
a = True if not a: print('a is false')
in String
a = '' if not a: print('a is empty')
def mul(num): if type(num) == int: print(num * 2) return True else: return False n = 'a' if not mul(n): print('please input number')
if notは基本的にTrue or Falseを判定している
シンプルなログ出力はお馴染みのprint文
print("I am the simplest log.")
ファイル出力、メール送信、HTTP通信など動作ログをよりフレキシブルに出力したい場合にはloggingモジュールを使用してログを出力する。
### ログレベル
DEBUG, INFO, WARNING, ERROR, CRITICAL
出力
import logging logging.info("info log") logging.warning("warning log")
$ python3 test.py
WARNING:root:warning log
info はコマンドラインに出力されない
loggerオブジェクトを取得してログを出力する
import logging logger = logging.getLogger("sample") logger.info("info log") logger.warning("warning log")
$ python3 test.py
warning log
import logging logger = logging.getLogger("sample") logger.setLevel(logging.DEBUG) logger.info("info log") logger.warning("warning log")
### ハンドラ
StreamHandler, FileHandler, SMTPHandler, HTTPHandlerがある
import logging logger = logging.getLogger("sample") logger.setLevel(logging.DEBUG) st_handler = logging.StreamHandler() logger.addHandler(st_handler) logger.info("info log") logger.warning("warning log")
handlerで指定することでログ出力をカスタマイズできる。
a = [0, 1, 4, 9, 16, 25, 36, 49] print(a[:])
$ python3 test.py
[0, 1, 4, 9, 16, 25, 36, 49]
[:]の場合は始点から終点まで全てをスライスする。
class MyStack: def __init__(self): self.stack = [] def push(self, item): self.stack.append(item) def pop(self): pass mystack = MyStack() mystack.push(0) mystack.push(1) mystack.push(2) print(mystack.stack)
$ python3 test.py
[0, 1, 2]
pop
def pop(self): result = self.stack[-1] del self.stack[-1] return result
print(mystack.pop())
print(mystack.pop())
取り出して削除なので、[-1]の後にdelを実行している。
上記と同じことをpop()と書くことができる。
def pop(self): return self.stack.pop()
stackが0の場合があるので、例外処理も書かなければならない。
def pop(self): if len(self.stack) == 0: return None return self.stack.pop()
BytesIOはメモリ上でバイナリデータを扱うための機能
同様な機能としてStringIO, cStringIOがある
import io import urllib.request from PIL import Image url = "https://www.python.org/static/img/python-logo@2x.png" img_in = urllib.request.urlopen(url).read() img_bin = io.BytesIO(img_in) img = Image.open(img_bin) img.save("logo.png","PNG") print(img_bin.getvalue())
I/O : 入出力input/outputのこと
stream: データが流れる通り道
Pythonおいてストリームを作り簡単な方法はファイルをopenすること
読み取りのストリームにはread(), tell(), seekなどのインターフェイスが用意されている
tellは読み取り位置取得、seekは読み取り位置変更
text_file.txt
1234567890abcdefg
f = open("text_file.txt", "r") print("tell 1 ->", f.tell()) first4 = f.read(4) print("first4 ->", first4) print("tell2 ->", f.tell()) f.seek(10) last = f.read() print("last ->", last) f.close()
$ python3 test.py
tell 1 -> 0
first4 -> 1234
tell2 -> 4
last -> abcdefg
クラスメソッドの第1引数はclsで、クラス自身を示す
import datetime class Reserve: dt_now = datetime.datetime.now() def __init__(self, dep, dest, time): self.dep = dep self.dest = dest self.time = time def plan(self): print(f"予約内容は、出発地:{self.dep}、目的地:{self.dest}、出発時間:{self.time}です") @classmethod def clock(cls): print(f"只今の時刻は、{cls.dt_now}です") osakaTrip = Reserve("東京","大阪","16時") osakaTrip.plan() Reserve.clock()
$ python3 test.py
予約内容は、出発地:東京、目的地:大阪、出発時間:16時です
只今の時刻は、2023-11-16 02:47:03.822162です
print(f”只今の時刻は、{dt_now}です”) とすると、以下のようにエラーになります。
Traceback (most recent call last):
File “/home/vagrant/dev/test/test.py”, line 23, in
Reserve.clock()
File “/home/vagrant/dev/test/test.py”, line 18, in clock
print(f”只今の時刻は、{dt_now}です”)
NameError: name ‘dt_now’ is not defined
I = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] print(I[::-1])
$ python3 test.py
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
配列のリトルエンディアンみたいなものか。