2017-10-18 157 views
1

下面的代码按预期工作:不支持的格式字符警示

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()) 

我的问题是,PyCharm哭我就这段代码:

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

PyCharm说我使用的是“不受支持的格式字符'|' “< - 看起来像PyCharm内部的竖条。由于一切正常,我不确定PyCharm在抱怨什么。我想知道PyCharm反对什么。从上面的代码

输出:

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 

回答

1

即竖条是小写字母L,它在格式字符串long的开头找到。这是抱怨,因为PyCharm只识别字符的某个子集(即https://docs.python.org/2/library/string.html#format-specification-mini-language),因为它不知道搜索__format__的运算符超载。 不幸的是,我最好的建议是为PyCharm添加一个noinspection子句来提取:

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