pythonはてな記法

はてなブログpythonコードをハイライトする方法。

設定→編集モード→はてな記法モードを選択する。

">|python|"と"||<"をコードの行頭と行末に書くとコードに色が付く。
syntaxhighlighterとかを使わなくても良いのは便利ですね。

import collections

Card = collections.namedtuple('Card',['rank','suit'])

class FrenchDeck:
	ranks = [str(n) for n in range(2,11)] + list('JQKA')
	suits = 'spades diamonds clubs hearts'.split()

	def __init__(self):
		self._cards = [Card(rank, suit) for suit in self.suits for rank in self.ranks]

	def __len__(self):
		return len(self._cards)

	def __getitem__(self, position):
		return self._cards[position]

if __name__=='__main__':

	card = Card('7','diamonds')
	print(card)

	deck = FrenchDeck()
	print(len(deck))

	print(deck[0])
	print(deck[-1])

	from random import choice

	print(choice(deck))
	print(choice(deck))
	print(choice(deck))