不支持的格式字符警告
Unsupported format character warning
以下代码按预期工作:
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 说我正在使用 "unsupported format character '|' " <-- 看起来像 PyCharm 内的竖线。由于一切正常,我不确定 PyCharm 在抱怨什么。我想知道 PyCharm 反对什么。
以上代码的输出:
Your cart contains: Cucumbers, Tissues, Tomatoes, Toothpaste
Your cart contains:
1 kg Cucumbers @ .00 ... .00
2 dozen Tissues @ .00 ... .00
5 pound Tomatoes @ .00 ... .00
5 box Toothpaste @ .00 ... .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 @ .00 ... .00
2 dozen Tissues @ .00 ... .00
5 box Toothpaste @ .00 ... .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
竖线是小写字母 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))
以下代码按预期工作:
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 说我正在使用 "unsupported format character '|' " <-- 看起来像 PyCharm 内的竖线。由于一切正常,我不确定 PyCharm 在抱怨什么。我想知道 PyCharm 反对什么。
以上代码的输出:
Your cart contains: Cucumbers, Tissues, Tomatoes, Toothpaste
Your cart contains:
1 kg Cucumbers @ .00 ... .00
2 dozen Tissues @ .00 ... .00
5 pound Tomatoes @ .00 ... .00
5 box Toothpaste @ .00 ... .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 @ .00 ... .00
2 dozen Tissues @ .00 ... .00
5 box Toothpaste @ .00 ... .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
竖线是小写字母 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))