Skip to content

InventoryItem

Represents a particular instance of a Flesh and Blood card, for inventory purposes.

Note

Unlike normal Card objects, InventoryItem objects represent a card's unique identifier, edition, rarity, and foiling.

Attributes:

Name Type Description
edition str

The edition code associated with the card (see meta.EDITIONS).

foiling str

The foiling code associated with the card (see meta.FOILINGS).

identifier str

The unique identifier associated with the card.

rarity str

The rarity code associated with the card (see meta.RARITIES).

Source code in fab/inventory.py
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
@dataclasses.dataclass
class InventoryItem:
    '''
    Represents a particular instance of a Flesh and Blood card, for inventory
    purposes.

    Note:
      Unlike normal `Card` objects, `InventoryItem` objects represent a
      card's unique identifier, edition, rarity, and foiling.

    Attributes:
      edition: The edition code associated with the card (see `meta.EDITIONS`).
      foiling: The foiling code associated with the card (see `meta.FOILINGS`).
      identifier: The unique identifier associated with the card.
      rarity: The rarity code associated with the card (see `meta.RARITIES`).
    '''
    identifier: str
    rarity: str
    edition: str = 'U'
    foiling: str = 'S'

    def __getitem__(self, key: str) -> str:
        '''
        Allows one to access fields of an inventory item via dictionary syntax.

        Args:
          key: The name of the class attribute to fetch.

        Returns:
          The value associated with the specified field.
        '''
        return self.__dict__[key]

    def __hash__(self) -> Any:
        '''
        Computes the hash representation of the inventory item.

        Returns:
          The hash representation of the inventory item.
        '''
        return hash((self.edition, self.foiling, self.identifier, self.rarity))

    def __str__(self) -> str:
        '''
        Computes the string representation of the inventory item.

        This is an alias to the `to_str()` method.

        Returns:
          The string representation of the inventory item.
        '''
        return self.to_str()

    @staticmethod
    def from_json(jsonstr: str) -> InventoryItem:
        '''
        Creates a new inventory card from the specified JSON string.

        Args:
          jsonstr: The JSON string representation to parse.

        Returns:
          The parsed `InventoryItem` object.
        '''
        return InventoryItem.from_str(json.loads(jsonstr))

    @staticmethod
    def from_list(data: list[str]) -> InventoryItem:
        '''
        Creates a new inventory card from its list representation.

        Returns:
          The new inventory item.
        '''
        if not len(data) == 4:
            raise ValueError('input list does not contain exactly four elements')
        return InventoryItem(
            edition = data[0],
            foiling = data[1],
            identifier = data[2],
            rarity = data[3]
        )

    @staticmethod
    def from_tuple(data: tuple[str, str, str, str]) -> InventoryItem:
        '''
        Creates a new inventory card from its tuple representation.

        Returns:
          The new inventory item.
        '''
        if not len(data) == 4:
            raise ValueError('input list does not contain exactly four elements')
        return InventoryItem(
            edition = data[0],
            foiling = data[1],
            identifier = data[2],
            rarity = data[3]
        )

    @staticmethod
    def from_str(data: str) -> InventoryItem:
        '''
        Creates a new inventory card from its string representation.

        Returns:
          The new inventory item.
        '''
        parts = data.split('-')
        if not len(parts) == 4:
            raise ValueError('specified string not of the form "EDITION-IDENTIFIER-RARITY-FOILING"')
        return InventoryItem(
            edition = parts[0],
            foiling = parts[3],
            identifier = parts[1],
            rarity = parts[2]
        )

    def keys(self) -> list[str]:
        '''
        Returns the dictionary keys associated with this inventory item.

        Returns:
          The `dict` keys as `list[str]`, corresponding to the possible fields of the inventory item.
        '''
        return list(self.__dict__.keys())

    def to_card(self, catalog: Optional[CardList] = None) -> Card:
        '''
        Converts this unique card representation into a generic card
        representation.

        Note:
          To instantiate the card this way, a card catalog (`CardList`) must be
          provided, defaulting to `card.CARD_CATALOG`.

        Args:
          catalog: The card catalog to use as a reference, defaulting to `card.CARD_CATALOG` if `None`.

        Returns:
          A `Card` object associated with this inventory item.
        '''
        return Card.from_identifier(self.identifier, catalog=catalog)

    def to_dict(self) -> dict[str, str]:
        '''
        Converts this inventory item into a raw python dictionary.

        Returns:
          A copy of the raw `dict` representation of the inventory item.
        '''
        return copy.deepcopy(self.__dict__)

    def to_json(self) -> str:
        '''
        Computes the inventory item's JSON string representation.

        Returns:
          A JSON string representation of the inventory item.
        '''
        return json.dumps(self.to_str(), indent=JSON_INDENT)

    def to_list(self) -> list[str]:
        '''
        Converts this inventory item into a list of field strings.

        Returns:
          A `list` representation of the inventory item.
        '''
        return [self.edition, self.foiling, self.identifier, self.rarity]

    def to_str(self) -> str:
        '''
        Computes the string representation of the inventory item.

        This is a string of the form:

        ```
        {edition}-{identifier}-{rarity}-{foiling}
        ```

        Returns:
          The string representation of the inventory item.
        '''
        return f'{self.edition}-{self.identifier}-{self.rarity}-{self.foiling}'

    def to_tuple(self) -> tuple[str, str, str, str]:
        '''
        Converts this inventory item into a tuple of field strings.

        Returns:
          A `tuple` representation of the inventory item.
        '''
        return (self.edition, self.foiling, self.identifier, self.rarity)

__getitem__(key)

Allows one to access fields of an inventory item via dictionary syntax.

Parameters:

Name Type Description Default
key str

The name of the class attribute to fetch.

required

Returns:

Type Description
str

The value associated with the specified field.

Source code in fab/inventory.py
41
42
43
44
45
46
47
48
49
50
51
def __getitem__(self, key: str) -> str:
    '''
    Allows one to access fields of an inventory item via dictionary syntax.

    Args:
      key: The name of the class attribute to fetch.

    Returns:
      The value associated with the specified field.
    '''
    return self.__dict__[key]

__hash__()

Computes the hash representation of the inventory item.

Returns:

Type Description
Any

The hash representation of the inventory item.

Source code in fab/inventory.py
53
54
55
56
57
58
59
60
def __hash__(self) -> Any:
    '''
    Computes the hash representation of the inventory item.

    Returns:
      The hash representation of the inventory item.
    '''
    return hash((self.edition, self.foiling, self.identifier, self.rarity))

__str__()

Computes the string representation of the inventory item.

This is an alias to the to_str() method.

Returns:

Type Description
str

The string representation of the inventory item.

Source code in fab/inventory.py
62
63
64
65
66
67
68
69
70
71
def __str__(self) -> str:
    '''
    Computes the string representation of the inventory item.

    This is an alias to the `to_str()` method.

    Returns:
      The string representation of the inventory item.
    '''
    return self.to_str()

from_json(jsonstr) staticmethod

Creates a new inventory card from the specified JSON string.

Parameters:

Name Type Description Default
jsonstr str

The JSON string representation to parse.

required

Returns:

Type Description
InventoryItem

The parsed InventoryItem object.

Source code in fab/inventory.py
73
74
75
76
77
78
79
80
81
82
83
84
@staticmethod
def from_json(jsonstr: str) -> InventoryItem:
    '''
    Creates a new inventory card from the specified JSON string.

    Args:
      jsonstr: The JSON string representation to parse.

    Returns:
      The parsed `InventoryItem` object.
    '''
    return InventoryItem.from_str(json.loads(jsonstr))

from_list(data) staticmethod

Creates a new inventory card from its list representation.

Returns:

Type Description
InventoryItem

The new inventory item.

Source code in fab/inventory.py
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
@staticmethod
def from_list(data: list[str]) -> InventoryItem:
    '''
    Creates a new inventory card from its list representation.

    Returns:
      The new inventory item.
    '''
    if not len(data) == 4:
        raise ValueError('input list does not contain exactly four elements')
    return InventoryItem(
        edition = data[0],
        foiling = data[1],
        identifier = data[2],
        rarity = data[3]
    )

from_str(data) staticmethod

Creates a new inventory card from its string representation.

Returns:

Type Description
InventoryItem

The new inventory item.

Source code in fab/inventory.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
@staticmethod
def from_str(data: str) -> InventoryItem:
    '''
    Creates a new inventory card from its string representation.

    Returns:
      The new inventory item.
    '''
    parts = data.split('-')
    if not len(parts) == 4:
        raise ValueError('specified string not of the form "EDITION-IDENTIFIER-RARITY-FOILING"')
    return InventoryItem(
        edition = parts[0],
        foiling = parts[3],
        identifier = parts[1],
        rarity = parts[2]
    )

from_tuple(data) staticmethod

Creates a new inventory card from its tuple representation.

Returns:

Type Description
InventoryItem

The new inventory item.

Source code in fab/inventory.py
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
@staticmethod
def from_tuple(data: tuple[str, str, str, str]) -> InventoryItem:
    '''
    Creates a new inventory card from its tuple representation.

    Returns:
      The new inventory item.
    '''
    if not len(data) == 4:
        raise ValueError('input list does not contain exactly four elements')
    return InventoryItem(
        edition = data[0],
        foiling = data[1],
        identifier = data[2],
        rarity = data[3]
    )

keys()

Returns the dictionary keys associated with this inventory item.

Returns:

Type Description
list[str]

The dict keys as list[str], corresponding to the possible fields of the inventory item.

Source code in fab/inventory.py
138
139
140
141
142
143
144
145
def keys(self) -> list[str]:
    '''
    Returns the dictionary keys associated with this inventory item.

    Returns:
      The `dict` keys as `list[str]`, corresponding to the possible fields of the inventory item.
    '''
    return list(self.__dict__.keys())

to_card(catalog=None)

Converts this unique card representation into a generic card representation.

Note

To instantiate the card this way, a card catalog (CardList) must be provided, defaulting to card.CARD_CATALOG.

Parameters:

Name Type Description Default
catalog Optional[CardList]

The card catalog to use as a reference, defaulting to card.CARD_CATALOG if None.

None

Returns:

Type Description
Card

A Card object associated with this inventory item.

Source code in fab/inventory.py
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
def to_card(self, catalog: Optional[CardList] = None) -> Card:
    '''
    Converts this unique card representation into a generic card
    representation.

    Note:
      To instantiate the card this way, a card catalog (`CardList`) must be
      provided, defaulting to `card.CARD_CATALOG`.

    Args:
      catalog: The card catalog to use as a reference, defaulting to `card.CARD_CATALOG` if `None`.

    Returns:
      A `Card` object associated with this inventory item.
    '''
    return Card.from_identifier(self.identifier, catalog=catalog)

to_dict()

Converts this inventory item into a raw python dictionary.

Returns:

Type Description
dict[str, str]

A copy of the raw dict representation of the inventory item.

Source code in fab/inventory.py
164
165
166
167
168
169
170
171
def to_dict(self) -> dict[str, str]:
    '''
    Converts this inventory item into a raw python dictionary.

    Returns:
      A copy of the raw `dict` representation of the inventory item.
    '''
    return copy.deepcopy(self.__dict__)

to_json()

Computes the inventory item's JSON string representation.

Returns:

Type Description
str

A JSON string representation of the inventory item.

Source code in fab/inventory.py
173
174
175
176
177
178
179
180
def to_json(self) -> str:
    '''
    Computes the inventory item's JSON string representation.

    Returns:
      A JSON string representation of the inventory item.
    '''
    return json.dumps(self.to_str(), indent=JSON_INDENT)

to_list()

Converts this inventory item into a list of field strings.

Returns:

Type Description
list[str]

A list representation of the inventory item.

Source code in fab/inventory.py
182
183
184
185
186
187
188
189
def to_list(self) -> list[str]:
    '''
    Converts this inventory item into a list of field strings.

    Returns:
      A `list` representation of the inventory item.
    '''
    return [self.edition, self.foiling, self.identifier, self.rarity]

to_str()

Computes the string representation of the inventory item.

This is a string of the form:

{edition}-{identifier}-{rarity}-{foiling}

Returns:

Type Description
str

The string representation of the inventory item.

Source code in fab/inventory.py
191
192
193
194
195
196
197
198
199
200
201
202
203
204
def to_str(self) -> str:
    '''
    Computes the string representation of the inventory item.

    This is a string of the form:

    ```
    {edition}-{identifier}-{rarity}-{foiling}
    ```

    Returns:
      The string representation of the inventory item.
    '''
    return f'{self.edition}-{self.identifier}-{self.rarity}-{self.foiling}'

to_tuple()

Converts this inventory item into a tuple of field strings.

Returns:

Type Description
tuple[str, str, str, str]

A tuple representation of the inventory item.

Source code in fab/inventory.py
206
207
208
209
210
211
212
213
def to_tuple(self) -> tuple[str, str, str, str]:
    '''
    Converts this inventory item into a tuple of field strings.

    Returns:
      A `tuple` representation of the inventory item.
    '''
    return (self.edition, self.foiling, self.identifier, self.rarity)