曲曲的秘密学术基地

纯化欲望、坚持严肃性

欢迎!我是曲泽慧(@zququ),目前在深圳(ICBI,BCBDI,SIAT)任职助理研究员。


病毒学、免疫学及结构生物学背景,可以在 RG 上找到我已发表的论文

本站自2019年7月已访问web counter

A Pythonic Card Deck

Referenced from Luciano Ramalho, ‘Fluent Python’

Example:

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 heart'.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, _cards)
        return self._cards[positions]

collections.namedtuple()

Create a class named with ‘Card’, which has two attributes, ‘rank’ and ‘suit’, when you call this class and transfer the atrributes, the order of the atrribute should be the same with defined atrribute.

Which is used to build classes of objects that are just the bundles of attributes with no custom methods, like a database record.

# Card = collections.namedtuple('Card', ['rank', 'suit'])
>>> beer_card = Card('7', 'diamonds')
>>> beer_card
Card(rank='7', suit='diamonds')

len() of the ‘FrenchDeck’

>>> deck = FrechDeck()
>>> len(deck)
52

Reading specific cards from the deck

# suits = 'spades diamonds clubs hearts'.split()
# ranks = [str(n) for n in range(2, 11) + list('JQKA')]
>>> deck[0]
Card(rank='2', suit='spades')
>>> deck[-1]
Card(rank='A', suit='hearts')

Using random.choice to pick a random card

>>> from random import choice
>>> choice(deck)
Card(rank='3', suit='hearts')
>>> choice(deck)
Card(rank='K', suit='spades')

__getitem__() makes FrenchDeck implement the slice and iterable

>>> deck[:3]
[Card(rank='2', suit='spades'), Card(rank='3', suit='spades')]
>>> for card in deck:
        print(card)
...
Card(rank='2', suit='spades')
...
>>> for card in reversed(deck):
        print(card)
...
Card(rank='A', suit='hearts')

‘in’ instead of __contains__method

‘in’ operator does a sequential scan, and **‘in’ works with FrenchDeck class because it it iterable.

>>> Card('Q', 'hearts') in deck
True
>>> Card('7', 'beasts') in deck
False

Sorting of FrenchDeck

suit_values = dict(spades=3, hearts=2, diamonds=1, clubs=0)

def spades_high(card):
    rank_value = FrenchDeck.ranks.index(card.rank)
    return rank_value * len(suit_values) + suit_values[card.suit]

>>> for card in sorted(deck, key=spades_high):
        print(card)
...

Summary

By implementing the special methods __len__ and __getitem__, our FrenchDeck behaves like a standard Python sequence, allowing it to benefit from core language features (e.g., iteration and slicing) and from the standard library, as shown by the examples using random.choice, reversed and sorted. Thanks to composition, the __len__ and __getitem__ implementations can hand off all the work to a list object, self._cards.

Last One

python 魔法方法 (6) 描述符,property()函数的原理

整理自小甲鱼鱼C论坛描述符的概念描述符就是将某种特殊类型的类的实例指派给另一个类的属性。而这个特殊类型的类,就是至少要再这个类里面定义__get__()、__set__()或__delete__()三个特殊方法中的任意一个。 魔法方法 含义 __get__(self, instance, owner) 用于访问属性,它返回属性的值 __set__(self, instance, value...…

pythonMore
Next One

汇编语言 实验10 (1) 显示字符串

问题编写一个通用的子程序来实现这个功能。我们应该提供灵活的调用接口,使调用者可以决定显示的位置(行,列)、内容和颜色。子程序描述 名称:show_str 功能:在指定的位置,用指定的颜色,显示一个用0结束的字符串。 参数:(dh)=行号(取值范围0~24),(dl)=列号(取值范围0~79),(cl)=颜色, ds:si指向字符串的首地址。 返回:无 应用举例:在屏幕的8行3列,用绿色显示data段中的字符串。assume cs:codedata segment db 'Wel...…

汇编语言More