Skip to content

CardInventory

Bases: UserDict

Represents an inventory of unique Flesh and Blood cards.

This is essentially a dict[InventoryItem, int], where the value of a particular key denotes the number of copies in the inventory.

Note

This class inherits collections.UserDict, and thus supports any common dict methods.

Warning

Note that since this object is essentially a dict, calling len() on it will not return the total number of cards in the inventory. Instead, it returns the total number of different types of cards in the inventory. To get the overall number of cards in the inventory, use the count() method.

Attributes:

Name Type Description
data dict[InventoryItem, int]

The underlying dict of InventoryItem objects.

Source code in fab/inventory.py
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
class CardInventory(UserDict):
    '''
    Represents an inventory of unique Flesh and Blood cards.

    This is essentially a `dict[InventoryItem, int]`, where the value of a
    particular key denotes the number of copies in the inventory.

    Note:
      This class inherits `collections.UserDict`, and thus supports any common
      `dict` methods.

    Tip: Warning
      Note that since this object is essentially a `dict`, calling `len()` on it
      will _not_ return the total number of cards in the inventory. Instead, it
      returns the total number of _different types_ of cards in the inventory.
      To get the overall number of cards in the inventory, use the `count()`
      method.

    Attributes:
      data: The underlying `dict` of `InventoryItem` objects.
    '''
    data: dict[InventoryItem, int]

    def add(
        self,
        identifier: str,
        rarity: str,
        foiling: str,
        count: int = 1,
        edition: str = 'U',
    ) -> None:
        '''
        Adds one or more copies of a unique card to this inventory.

        This method builds a `InventoryItem` object from the specified
        parameters. To add a pre-built `InventoryItem` object to the collection,
        use `add_item()`. If the inventory already contains the specified card,
        its count is increased by the value specified (to set the count, use
        `set()`).

        Args:
          count: The number of copies to add to the inventory.
          edition: The edition code of the card (see `meta.EDITIONS`).
          foiling: The foiling code of the card (see `meta.FOILINGS`).
          identifier: The identifier of the card to add.
          rarity: The rarity code of the card (see `meta.RARITIES`).
        '''
        if not edition in EDITIONS:
            raise ValueError(f'specified card edition code not one of {list(EDITIONS.keys())}')
        if not foiling in FOILINGS:
            raise ValueError(f'specified card foiling code not one of {list(FOILINGS.keys())}')
        if not rarity in RARITIES:
            raise ValueError(f'specified card rarity code not one of {list(RARITIES.keys())}')
        self.add_item(
            item = InventoryItem(
              identifier = identifier,
              rarity = rarity,
              edition = edition,
              foiling = foiling
            ),
            count = count
        )

    def add_item(self, item: InventoryItem, count: int = 1) -> None:
        '''
        Adds one or more copies of a card inventory item to this inventory.

        If the inventory already contains the specified item, its count is
        increased by the value specified (to set the count, use `set_item()`).

        Args:
          item: The card inventory item to add.
          count: The number of copies to add to the inventory.
        '''
        if count <= 0:
            raise ValueError('specified count is not a positive integer value')
        if not item in self:
            self[item] = count
        else:
            self[item] += count

    def count(self) -> int:
        '''
        Computes the total number of cards in this inventory.

        Return:
          The total number of cards in the inventory.
        '''
        return sum(self.data.values())

    def edition_counts(self) -> dict[str, int]:
        '''
        Returns the counts of each edition code contained in this card inventory.

        Returns:
          A `dict` representing the counts of each edition code contained in the inventory.
        '''
        res = {}
        for k, v in self.data.items():
            if k.edition in res:
                res[k.edition] += v
            else:
                res[k.edition] = v
        return res

    def editions(self) -> list[str]:
        '''
        Returns the set of edition codes contained in this card inventory.

        Returns:
          A unique `list` of all card set edition codes contained in the card inventory.
        '''
        res = []
        for i in self.data.keys():
            res.append(i.edition)
        return list(set(res))

    def foiling_counts(self) -> dict[str, int]:
        '''
        Returns the counts of each foiling code contained in this card inventory.

        Returns:
          A `dict` representing the counts of each foiling code contained in the inventory.
        '''
        res = {}
        for k, v in self.data.items():
            if k.foiling in res:
                res[k.foiling] += v
            else:
                res[k.foiling] = v
        return res

    def foilings(self) -> list[str]:
        '''
        Returns the set of foiling codes contained in this card inventory.

        Returns:
          A unique `list` of all card set foiling codes contained in the card inventory.
        '''
        res = []
        for i in self.data.keys():
            res.append(i.foiling)
        return list(set(res))

    @staticmethod
    def from_dict(data: dict[str, int]) -> CardInventory:
        '''
        Parses a new card inventory from the specified `dict` representation.

        Args:
          data: The `dict` representation to parse.

        Returns:
          A new `CardInventory` from the parsed data.
        '''
        res = {}
        for k, v in data.items():
            res[InventoryItem.from_str(k)] = v
        return CardInventory(res)

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

        Args:
          jsonstr: The JSON string representation to parse.

        Returns:
          A new `CardInventory` from the parsed data.
        '''
        return CardInventory.from_dict(json.loads(jsonstr))

    def identifier_counts(self) -> dict[str, int]:
        '''
        Returns the counts of each identifier contained in this card inventory.

        Returns:
          A `dict` representing the counts of each identifier contained in the inventory.
        '''
        res = {}
        for k, v in self.data.items():
            if k.identifier in res:
                res[k.identifier] += v
            else:
                res[k.identifier] = v
        return res

    def identifiers(self) -> list[str]:
        '''
        Returns the set of identifiers contained in this card inventory.

        Returns:
          A unique `list` of all card set identifiers contained in the card inventory.
        '''
        res = []
        for i in self.data.keys():
            res.append(i.identifier)
        return list(set(res))

    @staticmethod
    def load(file_path: str) -> CardInventory:
        '''
        Loads a card inventory from the specified `.json` file.

        Args:
          file_path: The `.json` file path from which to load the inventory.

        Returns:
          The parsed card inventory object loaded from the specified file path.
        '''
        with open(os.path.expanduser(file_path), 'r') as f:
            if file_path.endswith('.json'):
                return CardInventory.from_json(f.read())
            else:
                raise Exception('specified file path is not a JSON file')

    def lookup(
        self,
        edition: Optional[str] = None,
        foiling: Optional[str] = None,
        identifier: Optional[str] = None,
        rarity: Optional[str] = None,
        set: Optional[str] = None
    ) -> CardInventory:
        '''
        Returns a subset of the card inventory based on the specified search parameters.

        This is akin to filtering the inventory. Any arguments with `None`
        values are ignored.

        Args:
          edition: The edition code of cards to include in the result.
          foiling: The foiling code of cards to include in the result.
          identifier: The identifier of cards to include in the result.
          rarity: The rarity of cards to include in the result.
          set: The card set code of cards to include in the result.

        Returns:
          A subset of the inventory filtered by the specified arguments.
        '''
        res = {}
        for k, v in self.data.items():
            if not edition is None and not k.edition == edition: continue
            if not foiling is None and not k.foiling == foiling: continue
            if not identifier is None and not k.identifier == identifier: continue
            if not rarity is None and not k.rarity == rarity: continue
            if not set is None and not k.identifier.startswith(set): continue
            res[k] = v
        return CardInventory(res)

    def rarities(self) -> list[str]:
        '''
        Returns the set of rarity codes contained in this card inventory.

        Returns:
          A unique `list` of all card set rarity codes contained in the card inventory.
        '''
        res = []
        for i in self.data.keys():
            res.append(i.rarity)
        return list(set(res))

    def rarity_counts(self) -> dict[str, int]:
        '''
        Returns the counts of each rarity code contained in this card inventory.

        Returns:
          A `dict` representing the counts of each rarity code contained in the inventory.
        '''
        res = {}
        for k, v in self.data.items():
            if k.rarity in res:
                res[k.rarity] += v
            else:
                res[k.rarity] = v
        return res

    def save(self, file_path: str) -> None:
        '''
        Saves the card inventory to the specified `.json` file path.

        Args:
          file_path: The `.json` file path to save to.
        '''
        with open(os.path.expanduser(file_path), 'w') as f:
            if file_path.endswith('.json'):
                f.write(self.to_json())
            else:
                raise Exception('specified file path is not a JSON file')

    def set(
        self,
        identifier: str,
        rarity: str,
        foiling: str,
        count: int = 1,
        edition: str = 'U',
    ) -> None:
        '''
        Sets the number of copies associated with a unique card.

        This method builds a `InventoryItem` object from the specified
        parameters. To set a pre-built `InventoryItem` object in the collection,
        use `set_item()`. To add to the current count associated with an item,
        use `add()`.

        Note:
          Setting a card's count to `0` does not delete the item from the
          inventory but rather simply sets the count to `0`. This is useful for
          tracking when you _used_ to own a card, or when _plan_ to obtain a
          copy of a particular card.

        Args:
          count: The number of copies to set the inventory count to.
          edition: The edition code of the card (see `meta.EDITIONS`).
          foiling: The foiling code of the card (see `meta.FOILINGS`).
          identifier: The identifier of the card.
          rarity: The rarity code of the card (see `meta.RARITIES`).
        '''
        if not edition in EDITIONS:
            raise ValueError(f'specified card edition code not one of {list(EDITIONS.keys())}')
        if not foiling in FOILINGS:
            raise ValueError(f'specified card foiling code not one of {list(FOILINGS.keys())}')
        if not rarity in RARITIES:
            raise ValueError(f'specified card rarity code not one of {list(RARITIES.keys())}')
        self.set_item(
            item = InventoryItem(
              identifier = identifier,
              rarity = rarity,
              edition = edition,
              foiling = foiling
            ),
            count = count
        )

    def set_item(self, item: InventoryItem, count: int = 1) -> None:
        '''
        Sets the number of copies of a card inventory item in this inventory.

        If the inventory already contains the specified item, its count is
        increased by the value specified (to set the count, use `set_item()`).

        Note:
          Setting a card's count to `0` does not delete the item from the
          inventory but rather simply sets the count to `0`. This is useful for
          tracking when you _used_ to own a card, or when _plan_ to obtain a
          copy of a particular card.

        Args:
          item: The card inventory item.
          count: The number of copies to set.
        '''
        if count < 0:
            raise ValueError('specified count is not zero or a positive integer value')
        self[item] = count

    def to_cardlist(self, catalog: Optional[CardList] = None) -> CardList:
        '''
        Converts this unique card inventory into a list of generic
        representations of cards.

        Note:
          To instantiate the card list 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 `CardList` object containing the generic represenations of the cards contained within this inventory.
        '''
        cards = []
        for k, v in self.data.items():
            for _ in range(0, v):
                cards.append(k.to_card(catalog=catalog))
        return CardList(cards)

    def to_dict(self) -> dict[str, int]:
        '''
        Converts the inventory object into a raw dictionary of python
        primitives.

        Returns:
          A raw `dict` representation of the card inventory.
        '''
        data = {}
        for k, v in self.data.items():
            data[k.to_str()] = v
        return data

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

        Returns:
          The JSON string representation of the card inventory.
        '''
        return json.dumps(self.to_dict(), indent=JSON_INDENT)

add(identifier, rarity, foiling, count=1, edition='U')

Adds one or more copies of a unique card to this inventory.

This method builds a InventoryItem object from the specified parameters. To add a pre-built InventoryItem object to the collection, use add_item(). If the inventory already contains the specified card, its count is increased by the value specified (to set the count, use set()).

Parameters:

Name Type Description Default
count int

The number of copies to add to the inventory.

1
edition str

The edition code of the card (see meta.EDITIONS).

'U'
foiling str

The foiling code of the card (see meta.FOILINGS).

required
identifier str

The identifier of the card to add.

required
rarity str

The rarity code of the card (see meta.RARITIES).

required
Source code in fab/inventory.py
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
def add(
    self,
    identifier: str,
    rarity: str,
    foiling: str,
    count: int = 1,
    edition: str = 'U',
) -> None:
    '''
    Adds one or more copies of a unique card to this inventory.

    This method builds a `InventoryItem` object from the specified
    parameters. To add a pre-built `InventoryItem` object to the collection,
    use `add_item()`. If the inventory already contains the specified card,
    its count is increased by the value specified (to set the count, use
    `set()`).

    Args:
      count: The number of copies to add to the inventory.
      edition: The edition code of the card (see `meta.EDITIONS`).
      foiling: The foiling code of the card (see `meta.FOILINGS`).
      identifier: The identifier of the card to add.
      rarity: The rarity code of the card (see `meta.RARITIES`).
    '''
    if not edition in EDITIONS:
        raise ValueError(f'specified card edition code not one of {list(EDITIONS.keys())}')
    if not foiling in FOILINGS:
        raise ValueError(f'specified card foiling code not one of {list(FOILINGS.keys())}')
    if not rarity in RARITIES:
        raise ValueError(f'specified card rarity code not one of {list(RARITIES.keys())}')
    self.add_item(
        item = InventoryItem(
          identifier = identifier,
          rarity = rarity,
          edition = edition,
          foiling = foiling
        ),
        count = count
    )

add_item(item, count=1)

Adds one or more copies of a card inventory item to this inventory.

If the inventory already contains the specified item, its count is increased by the value specified (to set the count, use set_item()).

Parameters:

Name Type Description Default
item InventoryItem

The card inventory item to add.

required
count int

The number of copies to add to the inventory.

1
Source code in fab/inventory.py
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
def add_item(self, item: InventoryItem, count: int = 1) -> None:
    '''
    Adds one or more copies of a card inventory item to this inventory.

    If the inventory already contains the specified item, its count is
    increased by the value specified (to set the count, use `set_item()`).

    Args:
      item: The card inventory item to add.
      count: The number of copies to add to the inventory.
    '''
    if count <= 0:
        raise ValueError('specified count is not a positive integer value')
    if not item in self:
        self[item] = count
    else:
        self[item] += count

count()

Computes the total number of cards in this inventory.

Return

The total number of cards in the inventory.

Source code in fab/inventory.py
296
297
298
299
300
301
302
303
def count(self) -> int:
    '''
    Computes the total number of cards in this inventory.

    Return:
      The total number of cards in the inventory.
    '''
    return sum(self.data.values())

edition_counts()

Returns the counts of each edition code contained in this card inventory.

Returns:

Type Description
dict[str, int]

A dict representing the counts of each edition code contained in the inventory.

Source code in fab/inventory.py
305
306
307
308
309
310
311
312
313
314
315
316
317
318
def edition_counts(self) -> dict[str, int]:
    '''
    Returns the counts of each edition code contained in this card inventory.

    Returns:
      A `dict` representing the counts of each edition code contained in the inventory.
    '''
    res = {}
    for k, v in self.data.items():
        if k.edition in res:
            res[k.edition] += v
        else:
            res[k.edition] = v
    return res

editions()

Returns the set of edition codes contained in this card inventory.

Returns:

Type Description
list[str]

A unique list of all card set edition codes contained in the card inventory.

Source code in fab/inventory.py
320
321
322
323
324
325
326
327
328
329
330
def editions(self) -> list[str]:
    '''
    Returns the set of edition codes contained in this card inventory.

    Returns:
      A unique `list` of all card set edition codes contained in the card inventory.
    '''
    res = []
    for i in self.data.keys():
        res.append(i.edition)
    return list(set(res))

foiling_counts()

Returns the counts of each foiling code contained in this card inventory.

Returns:

Type Description
dict[str, int]

A dict representing the counts of each foiling code contained in the inventory.

Source code in fab/inventory.py
332
333
334
335
336
337
338
339
340
341
342
343
344
345
def foiling_counts(self) -> dict[str, int]:
    '''
    Returns the counts of each foiling code contained in this card inventory.

    Returns:
      A `dict` representing the counts of each foiling code contained in the inventory.
    '''
    res = {}
    for k, v in self.data.items():
        if k.foiling in res:
            res[k.foiling] += v
        else:
            res[k.foiling] = v
    return res

foilings()

Returns the set of foiling codes contained in this card inventory.

Returns:

Type Description
list[str]

A unique list of all card set foiling codes contained in the card inventory.

Source code in fab/inventory.py
347
348
349
350
351
352
353
354
355
356
357
def foilings(self) -> list[str]:
    '''
    Returns the set of foiling codes contained in this card inventory.

    Returns:
      A unique `list` of all card set foiling codes contained in the card inventory.
    '''
    res = []
    for i in self.data.keys():
        res.append(i.foiling)
    return list(set(res))

from_dict(data) staticmethod

Parses a new card inventory from the specified dict representation.

Parameters:

Name Type Description Default
data dict[str, int]

The dict representation to parse.

required

Returns:

Type Description
CardInventory

A new CardInventory from the parsed data.

Source code in fab/inventory.py
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
@staticmethod
def from_dict(data: dict[str, int]) -> CardInventory:
    '''
    Parses a new card inventory from the specified `dict` representation.

    Args:
      data: The `dict` representation to parse.

    Returns:
      A new `CardInventory` from the parsed data.
    '''
    res = {}
    for k, v in data.items():
        res[InventoryItem.from_str(k)] = v
    return CardInventory(res)

from_json(jsonstr) staticmethod

Parses a new card inventory from the specified JSON string representation.

Parameters:

Name Type Description Default
jsonstr str

The JSON string representation to parse.

required

Returns:

Type Description
CardInventory

A new CardInventory from the parsed data.

Source code in fab/inventory.py
375
376
377
378
379
380
381
382
383
384
385
386
@staticmethod
def from_json(jsonstr: str) -> CardInventory:
    '''
    Parses a new card inventory from the specified JSON string representation.

    Args:
      jsonstr: The JSON string representation to parse.

    Returns:
      A new `CardInventory` from the parsed data.
    '''
    return CardInventory.from_dict(json.loads(jsonstr))

identifier_counts()

Returns the counts of each identifier contained in this card inventory.

Returns:

Type Description
dict[str, int]

A dict representing the counts of each identifier contained in the inventory.

Source code in fab/inventory.py
388
389
390
391
392
393
394
395
396
397
398
399
400
401
def identifier_counts(self) -> dict[str, int]:
    '''
    Returns the counts of each identifier contained in this card inventory.

    Returns:
      A `dict` representing the counts of each identifier contained in the inventory.
    '''
    res = {}
    for k, v in self.data.items():
        if k.identifier in res:
            res[k.identifier] += v
        else:
            res[k.identifier] = v
    return res

identifiers()

Returns the set of identifiers contained in this card inventory.

Returns:

Type Description
list[str]

A unique list of all card set identifiers contained in the card inventory.

Source code in fab/inventory.py
403
404
405
406
407
408
409
410
411
412
413
def identifiers(self) -> list[str]:
    '''
    Returns the set of identifiers contained in this card inventory.

    Returns:
      A unique `list` of all card set identifiers contained in the card inventory.
    '''
    res = []
    for i in self.data.keys():
        res.append(i.identifier)
    return list(set(res))

load(file_path) staticmethod

Loads a card inventory from the specified .json file.

Parameters:

Name Type Description Default
file_path str

The .json file path from which to load the inventory.

required

Returns:

Type Description
CardInventory

The parsed card inventory object loaded from the specified file path.

Source code in fab/inventory.py
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
@staticmethod
def load(file_path: str) -> CardInventory:
    '''
    Loads a card inventory from the specified `.json` file.

    Args:
      file_path: The `.json` file path from which to load the inventory.

    Returns:
      The parsed card inventory object loaded from the specified file path.
    '''
    with open(os.path.expanduser(file_path), 'r') as f:
        if file_path.endswith('.json'):
            return CardInventory.from_json(f.read())
        else:
            raise Exception('specified file path is not a JSON file')

lookup(edition=None, foiling=None, identifier=None, rarity=None, set=None)

Returns a subset of the card inventory based on the specified search parameters.

This is akin to filtering the inventory. Any arguments with None values are ignored.

Parameters:

Name Type Description Default
edition Optional[str]

The edition code of cards to include in the result.

None
foiling Optional[str]

The foiling code of cards to include in the result.

None
identifier Optional[str]

The identifier of cards to include in the result.

None
rarity Optional[str]

The rarity of cards to include in the result.

None
set Optional[str]

The card set code of cards to include in the result.

None

Returns:

Type Description
CardInventory

A subset of the inventory filtered by the specified arguments.

Source code in fab/inventory.py
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
def lookup(
    self,
    edition: Optional[str] = None,
    foiling: Optional[str] = None,
    identifier: Optional[str] = None,
    rarity: Optional[str] = None,
    set: Optional[str] = None
) -> CardInventory:
    '''
    Returns a subset of the card inventory based on the specified search parameters.

    This is akin to filtering the inventory. Any arguments with `None`
    values are ignored.

    Args:
      edition: The edition code of cards to include in the result.
      foiling: The foiling code of cards to include in the result.
      identifier: The identifier of cards to include in the result.
      rarity: The rarity of cards to include in the result.
      set: The card set code of cards to include in the result.

    Returns:
      A subset of the inventory filtered by the specified arguments.
    '''
    res = {}
    for k, v in self.data.items():
        if not edition is None and not k.edition == edition: continue
        if not foiling is None and not k.foiling == foiling: continue
        if not identifier is None and not k.identifier == identifier: continue
        if not rarity is None and not k.rarity == rarity: continue
        if not set is None and not k.identifier.startswith(set): continue
        res[k] = v
    return CardInventory(res)

rarities()

Returns the set of rarity codes contained in this card inventory.

Returns:

Type Description
list[str]

A unique list of all card set rarity codes contained in the card inventory.

Source code in fab/inventory.py
466
467
468
469
470
471
472
473
474
475
476
def rarities(self) -> list[str]:
    '''
    Returns the set of rarity codes contained in this card inventory.

    Returns:
      A unique `list` of all card set rarity codes contained in the card inventory.
    '''
    res = []
    for i in self.data.keys():
        res.append(i.rarity)
    return list(set(res))

rarity_counts()

Returns the counts of each rarity code contained in this card inventory.

Returns:

Type Description
dict[str, int]

A dict representing the counts of each rarity code contained in the inventory.

Source code in fab/inventory.py
478
479
480
481
482
483
484
485
486
487
488
489
490
491
def rarity_counts(self) -> dict[str, int]:
    '''
    Returns the counts of each rarity code contained in this card inventory.

    Returns:
      A `dict` representing the counts of each rarity code contained in the inventory.
    '''
    res = {}
    for k, v in self.data.items():
        if k.rarity in res:
            res[k.rarity] += v
        else:
            res[k.rarity] = v
    return res

save(file_path)

Saves the card inventory to the specified .json file path.

Parameters:

Name Type Description Default
file_path str

The .json file path to save to.

required
Source code in fab/inventory.py
493
494
495
496
497
498
499
500
501
502
503
504
def save(self, file_path: str) -> None:
    '''
    Saves the card inventory to the specified `.json` file path.

    Args:
      file_path: The `.json` file path to save to.
    '''
    with open(os.path.expanduser(file_path), 'w') as f:
        if file_path.endswith('.json'):
            f.write(self.to_json())
        else:
            raise Exception('specified file path is not a JSON file')

set(identifier, rarity, foiling, count=1, edition='U')

Sets the number of copies associated with a unique card.

This method builds a InventoryItem object from the specified parameters. To set a pre-built InventoryItem object in the collection, use set_item(). To add to the current count associated with an item, use add().

Note

Setting a card's count to 0 does not delete the item from the inventory but rather simply sets the count to 0. This is useful for tracking when you used to own a card, or when plan to obtain a copy of a particular card.

Parameters:

Name Type Description Default
count int

The number of copies to set the inventory count to.

1
edition str

The edition code of the card (see meta.EDITIONS).

'U'
foiling str

The foiling code of the card (see meta.FOILINGS).

required
identifier str

The identifier of the card.

required
rarity str

The rarity code of the card (see meta.RARITIES).

required
Source code in fab/inventory.py
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
def set(
    self,
    identifier: str,
    rarity: str,
    foiling: str,
    count: int = 1,
    edition: str = 'U',
) -> None:
    '''
    Sets the number of copies associated with a unique card.

    This method builds a `InventoryItem` object from the specified
    parameters. To set a pre-built `InventoryItem` object in the collection,
    use `set_item()`. To add to the current count associated with an item,
    use `add()`.

    Note:
      Setting a card's count to `0` does not delete the item from the
      inventory but rather simply sets the count to `0`. This is useful for
      tracking when you _used_ to own a card, or when _plan_ to obtain a
      copy of a particular card.

    Args:
      count: The number of copies to set the inventory count to.
      edition: The edition code of the card (see `meta.EDITIONS`).
      foiling: The foiling code of the card (see `meta.FOILINGS`).
      identifier: The identifier of the card.
      rarity: The rarity code of the card (see `meta.RARITIES`).
    '''
    if not edition in EDITIONS:
        raise ValueError(f'specified card edition code not one of {list(EDITIONS.keys())}')
    if not foiling in FOILINGS:
        raise ValueError(f'specified card foiling code not one of {list(FOILINGS.keys())}')
    if not rarity in RARITIES:
        raise ValueError(f'specified card rarity code not one of {list(RARITIES.keys())}')
    self.set_item(
        item = InventoryItem(
          identifier = identifier,
          rarity = rarity,
          edition = edition,
          foiling = foiling
        ),
        count = count
    )

set_item(item, count=1)

Sets the number of copies of a card inventory item in this inventory.

If the inventory already contains the specified item, its count is increased by the value specified (to set the count, use set_item()).

Note

Setting a card's count to 0 does not delete the item from the inventory but rather simply sets the count to 0. This is useful for tracking when you used to own a card, or when plan to obtain a copy of a particular card.

Parameters:

Name Type Description Default
item InventoryItem

The card inventory item.

required
count int

The number of copies to set.

1
Source code in fab/inventory.py
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
def set_item(self, item: InventoryItem, count: int = 1) -> None:
    '''
    Sets the number of copies of a card inventory item in this inventory.

    If the inventory already contains the specified item, its count is
    increased by the value specified (to set the count, use `set_item()`).

    Note:
      Setting a card's count to `0` does not delete the item from the
      inventory but rather simply sets the count to `0`. This is useful for
      tracking when you _used_ to own a card, or when _plan_ to obtain a
      copy of a particular card.

    Args:
      item: The card inventory item.
      count: The number of copies to set.
    '''
    if count < 0:
        raise ValueError('specified count is not zero or a positive integer value')
    self[item] = count

to_cardlist(catalog=None)

Converts this unique card inventory into a list of generic representations of cards.

Note

To instantiate the card list 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
CardList

A CardList object containing the generic represenations of the cards contained within this inventory.

Source code in fab/inventory.py
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
def to_cardlist(self, catalog: Optional[CardList] = None) -> CardList:
    '''
    Converts this unique card inventory into a list of generic
    representations of cards.

    Note:
      To instantiate the card list 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 `CardList` object containing the generic represenations of the cards contained within this inventory.
    '''
    cards = []
    for k, v in self.data.items():
        for _ in range(0, v):
            cards.append(k.to_card(catalog=catalog))
    return CardList(cards)

to_dict()

Converts the inventory object into a raw dictionary of python primitives.

Returns:

Type Description
dict[str, int]

A raw dict representation of the card inventory.

Source code in fab/inventory.py
593
594
595
596
597
598
599
600
601
602
603
604
def to_dict(self) -> dict[str, int]:
    '''
    Converts the inventory object into a raw dictionary of python
    primitives.

    Returns:
      A raw `dict` representation of the card inventory.
    '''
    data = {}
    for k, v in self.data.items():
        data[k.to_str()] = v
    return data

to_json()

Computes the JSON string representation of the inventory.

Returns:

Type Description
str

The JSON string representation of the card inventory.

Source code in fab/inventory.py
606
607
608
609
610
611
612
613
def to_json(self) -> str:
    '''
    Computes the JSON string representation of the inventory.

    Returns:
      The JSON string representation of the card inventory.
    '''
    return json.dumps(self.to_dict(), indent=JSON_INDENT)