2017-10-18 2 views
1

Le code suivant fonctionne comme prévu:caractère Unsupported format avertissement

class Item(object): 
    def __init__(self, unq_id, name, price, qty, measure): 
     self.unq_id = unq_id 
     self.product_name = name 
     self.price = price 
     self.qty = qty 
     self.measure = measure 


class Cart(object): 
    def __init__(self): 
     self.content = dict() 

    def __format__(self, format_type): 
     if format_type == 'short': 
      return ', '.join(item.product_name for item in self.content.values()) 

     elif format_type == 'long': 
      return '\n'.join(f'\t\t{item.qty:2} {item.measure:7} {item.product_name:12} @ ' 
          f'${item.price:1.2f} ... ${item.qty * item.price:1.2f}' 
          for item in self.content.values()) 

    def add(self, item): 
     if item.unq_id not in self.content: 
      self.content.update({item.unq_id: item}) 
      return 
     for k, v in self.content.get(item.unq_id).items(): 
      if k == 'unq_id': 
       continue 
      elif k == 'qty': 
       total_qty = v.qty + item.qty 
       if total_qty: 
        v.qty = total_qty 
        continue 
       self.remove_item(k) 
      else: 
       v[k] = item[k] 

    def get_total(self): 
     return sum([v.price * v.qty for _, v in self.content.items()]) 

    def get_num_items(self): 
     return sum([v.qty for _, v in self.content.items()]) 

    def remove_item(self, key): 
     self.content.pop(key) 


if __name__ == '__main__': 
    item1 = Item(1, "Cucumbers", 1., 1, 'kg') 
    item2 = Item(2, "Tissues", 2., 2, 'dozen') 
    item3 = Item(3, "Tomatoes", 3., 5, 'pound') 
    item4 = Item(4, "Toothpaste", 1., 5, 'box') 
    cart = Cart() 
    cart.add(item1) 
    cart.add(item2) 
    cart.add(item3) 
    cart.add(item4) 
    print("Your cart contains: {0:short}".format(cart)) 
    # cart.remove_item(1) 
    print() 
    print("Your cart contains: \n {0:long}".format(cart)) 
    print() 
    print("The total number of items in your cart is: ", cart.get_num_items()) 
    print() 
    print("The total cost of the items in your cart is: ", cart.get_total()) 
    print() 
    cart.remove_item(3) 
    print("Your cart contains: {0:short}".format(cart)) 
    print() 
    print("Your cart contains: \n {0:long}".format(cart)) 
    print() 
    print("The total number of items in your cart is: ", cart.get_num_items()) 
    print() 
    print("The total cost of the items in your cart is: ", cart.get_total()) 

Ma question est que PyCharm pleure moi au sujet de ce morceau de code:

print("Your cart contains: \n {0:long}".format(cart)) 

PyCharm dit que je suis en utilisant un " caractère de format non pris en charge '|' "< - On dirait une barre verticale à l'intérieur de PyCharm. Puisque tout fonctionne, je ne suis pas sûr de ce que PyCharm se plaint. J'aimerais savoir à quoi PyCharm s'oppose.

sortie du code ci-dessus:

Your cart contains: Cucumbers, Tissues, Tomatoes, Toothpaste 

Your cart contains: 
     1 kg  Cucumbers @ $1.00 ... $1.00 
     2 dozen Tissues  @ $2.00 ... $4.00 
     5 pound Tomatoes  @ $3.00 ... $15.00 
     5 box  Toothpaste @ $1.00 ... $5.00 

The total number of items in your cart is: 13 

The total cost of the items in your cart is: 25.0 

Your cart contains: Cucumbers, Tissues, Toothpaste 

Your cart contains: 
     1 kg  Cucumbers @ $1.00 ... $1.00 
     2 dozen Tissues  @ $2.00 ... $4.00 
     5 box  Toothpaste @ $1.00 ... $5.00 

The total number of items in your cart is: 8 

The total cost of the items in your cart is: 10.0 

Process finished with exit code 0 

Répondre

1

Cette barre verticale est la lettre minuscule L, qu'il trouve au début de la chaîne de format long. Il se plaint puisque PyCharm reconnaît seulement un certain sous-ensemble de caractères (à savoir https://docs.python.org/2/library/string.html#format-specification-mini-language), car il ne sait pas rechercher une surcharge d'opérateur de __format__. Malheureusement, mon meilleur conseil est d'ajouter une clause noinspection pour PyCharm ramasser:

# noinspection PyStringFormat 
print("Your cart contains: \n {0:long}".format(cart))