Unsupported format character warning - string-formatting

The following code works as intended:
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())
My question is that PyCharm is crying at me regarding this piece of code:
print("Your cart contains: \n {0:long}".format(cart))
PyCharm says I am using an "unsupported format character '|' " <-- Looks like a vertical bar inside of PyCharm. Since everything is working I am not sure what PyCharm is complaining about. I would like to know what PyCharm is objecting to.
Output from above code:
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

That vertical bar is the lowercase letter L, which it found in the beginning of the format string long. It's complaining since PyCharm only recognises a certain subset of characters (namely https://docs.python.org/2/library/string.html#format-specification-mini-language), as it doesn't know to search for operator overloading of __format__.
Unfortunately, my best advice is to add a noinspection clause for PyCharm to pick up:
# noinspection PyStringFormat
print("Your cart contains: \n {0:long}".format(cart))

Related

How to run code in only for selected checkboxes in pyqt5?

I'm trying to make a menu GUI using pyqt5, the menu includes drinks and other stuff.
In front of each menu item there is a checkbox, when it is checked its price will be added to the bill.
self.latte_box = QtWidgets.QCheckBox(self.horizontalLayoutWidget) #latte_box is the name of the checkbox
self.latte_box.setText("")
self.latte_box.setObjectName("latte_box")
self.horizontalLayout.addWidget(self.latte_box)
self.latte_box.stateChanged.connect(self.order)
self.cookie_box = QtWidgets.QCheckBox(self.horizontalLayoutWidget_3)
self.cookie_box.setText("")
self.cookie_box.setObjectName("cookie_box")
self.horizontalLayout_3.addWidget(self.cookie_box)
self.cookie_box.stateChanged.connect(self.order)
bill = 0 #the bill variable
def order(self):
if self.latte_box.isChecked():
bill += 2.85
else:
bill -= 2.85
if self.cookie_box.isChecked():
bill += 1.50
else:
bill -= 1.50
latte_box and cookie_box are the checkboxes of 2 items on the list with the prices of $2.85 and $1.50.
So when the user check the box, the price of the item will be added to the bill, but in case of an error the user will just uncheck the box and the price of the item will be removed from the bill.
The problem here is that all the items run through the method (order), and whether the box is checked the price is added, and if its not checked the price is removed.
How can only the boxes that are checked or unchecked run through the method, and the untouched ones remain still.. ?
Try it:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.Qt import *
class Window(QWidget):
def __init__(self):
super().__init__()
self.latte_box = QtWidgets.QCheckBox()
self.latte_box.setText("2.85")
self.latte_box.stateChanged.connect(lambda state, cb=self.latte_box: self.order(state, cb))
self.cookie_box = QtWidgets.QCheckBox()
self.cookie_box.setText("1.50")
self.cookie_box.stateChanged.connect(lambda state, cb=self.cookie_box: self.order(state, cb))
self.label = QLabel()
self.layout = QGridLayout(self)
self.layout.addWidget(self.latte_box, 0, 0)
self.layout.addWidget(self.cookie_box, 0, 1)
self.layout.addWidget(self.label, 1, 0)
self.bill = 0
def order(self, state, cb):
if cb is self.latte_box:
if state:
self.bill += 2.85
else:
self.bill -= 2.85
elif cb is self.cookie_box:
if state:
self.bill += 1.50
else:
self.bill -= 1.50
self.label.setText(f'{abs(self.bill):.2f}')
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
form = Window()
form.show()
sys.exit(app.exec_())

Having Trouble printing out .txt in ruby, ruby file handling

the problem is it cannot print all the text of the from a .txt file. I am able to print the first 3 lines of the txt file but not the rest. So far, I am getting an error which is in print_album': undefined local variable or methodtracks' for main:Object (NameError).
Here's the code:
*I know using global variable is no good in Ruby but this exercise ask me to do it.
module Genre
POP, CLASSIC, JAZZ, ROCK = *1..4
end
$genre_names = ['Null', 'Pop', 'Classic', 'Jazz', 'Rock']
class Album
# NB: you will need to add tracks to the following and the initialize()
attr_accessor :title, :artist, :genre, :tracks
# complete the missing code:
def initialize (atitle, aartist, agenre, arrtrk)
# insert lines here
#genre = agenre
#tracks = arrtrk
#title = atitle
#artist = aartist
end
end
class Track
attr_accessor :ttitle, :tlocation
def initialize (tname, tloc)
#ttitle = tname
#tlocation = tloc
end
end
# Reads in and returns a single track from the given file
def read_track music_file
mytrk_name = music_file.gets
mytrk_location = music_file.gets
mytrk = Track.new(mytrk_name, mytrk_location)
mytrk
end
# Returns an array of tracks read from the given file
def read_tracks music_file
count = music_file.gets().to_i
tracks = Array.new
$i = 0
# Put a loop here which increments an index to read the tracks
while $i < count do
track = read_track(music_file)
tracks << track
$i += 1
end
tracks
end
# Takes an array of tracks and prints them to the terminal
def print_tracks tracks
# print all the tracks use: tracks[x] to access each track.
$i = 0
while $i < tracks.length do
print_track(tracks[$i])
$i +=1
end
tracks
end
# Reads in and returns a single album from the given file, with all its tracks
def read_album music_file
# read in all the Album's fields/attributes including all the tracks
# complete the missing code
album_title = music_file.gets
album_artist = music_file.gets
album_genre = music_file.gets.to_i
tracks = read_tracks(music_file)
album = Album.new(album_title, album_artist, album_genre, tracks)
album
end
# Takes a single album and prints it to the terminal along with all its tracks
def print_album album
# print out all the albums fields/attributes
# Complete the missing code.
puts 'Album title is '+ album.title
puts 'Artist is ' + album.artist
puts 'Genre is ' + album.genre.to_s
puts $genre_names[album.genre]
# print out the tracks
print_tracks(tracks)
end
# Takes a single track and prints it to the terminal
def print_track track
puts('Track title is: ' + track.ttitle)
puts('Track file location is: ' + track.tlocation)
end
# Reads in an album from a file and then print the album to the terminal
def main
music_file = File.new("album.txt", "r")
album = read_album(music_file)
music_file.close()
print_album(album)
end
main
Here's is the album.txt
Greatest Hits
Neil Diamond
1
3
Crackling Rose
sounds/01-Cracklin-rose.wav
Soolaimon
sounds/06-Soolaimon.wav
Sweet Caroline
sounds/20-Sweet_Caroline.wav
Currently my output is :
Album title is Greatest Hits
Artist is Neil Diamond
Genre is 1
Pop
Expected output is :
Album title is Greatest Hits
Artist is Neil Diamond
Genre is 1
Pop
Track title is: Crackling Rose
Track file location is: sounds/01-Cracklin-rose.wav
Track title is: Soolaimon
Track file location is: sounds/06-Soolaimon.wav
Track title is: Sweet Caroline
Track file location is: sounds/20-Sweet_Caroline.wav
The problem is inside your def print_album album method. On the last line of the method it uses print_tracks(tracks), but tracks variable is undefined (that's exactly what error tells you).
You need to call print_tracks(album.tracks)

Array remains empty after elements were pushed in a method

class Player
def initialize(hp, attack, defence, gold)
#hp = hp
#attack = attack
#defence = defence
#gold = gold
#inventory = inventory
end
def inventory
#inventory = []
end
def buy(item)
if #gold >= item.price
#gold-=item.price
puts "You hand over #{item.price} gold, and get the #{item.name}."
puts "You have #{gold} gold left over."
#inventory.push([item.name,item.price,item.attack,item.defence])
puts "ITEMS IN INV: #{#inventory}" # output: ITEMS IN INV: [["Dagger", 4, 1, 0], ["Mucky Tunic", 2, 0, 2]]
else
puts "You can't afford this item."
end
end
end
player = Player.new(10,1,2,6)
puts player.inventory.inspect # output: []
The inventory.push line pushes the element to the array while it is inside the method, but when returned outside the method, the inventory array is empty. This is confusing because other variables that were changed inside that method in the same way come back as altered.
sample output when printed from inside the buy method:
ITEMS IN INV: [["Dagger", 4, 1, 0], ["Mucky Tunic", 2, 0, 2]]
output with player.inventory.inspect outside of the method:
[]
Whenever you call your inventory method:
def inventory
#inventory = []
end
... it assigns a new (empty) array to #inventory, thus overwriting any existing items.
The correct way is to either assign #inventory in initialize and just return it from within the getter:
class Player
def initialize(hp, attack, defence, gold)
#hp = hp
#attack = attack
#defence = defence
#gold = gold
#inventory = []
end
def inventory
#inventory
end
# ...
end
or to not assign it at all in initialize and use the conditional assignment operator:
class Player
def initialize(hp, attack, defence, gold)
#hp = hp
#attack = attack
#defence = defence
#gold = gold
end
def inventory
#inventory ||= []
end
# ...
end
which will assign [] to #inventory only if it was nil or false (i.e. the first time you call inventory).
A getter that just returns the corresponding instance variable (as in the former example) can also be created via attr_reader:
class Player
attr_reader :inventory
# ...
end
I figured it out 10 seconds after posting this, after messing about with it for an hour.
I needed to add inventory to def initialize, and then pass an empty array to player = Player.new(10,1,2,6) so it became player = Player.new(10,1,2,6,[]).
I still don't know why this works.

Using Array.Count and match cases F#

I am not sure yet what the problem is, I am trying to go through a ResizeArray and matching the item with the data type, and depending on this, take away the value in a specific field (iSpace) from thespace(which is how much space the inventory has), before returning the final value.
A snippet of my code :
let spaceleft =
let mutable count = 0 //used to store the index to get item from array
let mutable thespace = 60 //the space left in the inventory
printf "Count: %i \n" inventory.Count //creates an error
while count < inventory.Count do
let item = inventory.[count]
match item with
|Weapon weapon ->
thespace <- (thespace - weapon.iSpace)
|Bomb bomb ->
thespace <-(thespace - bomb.iSpace)
|Potion pot ->
thespace <- (thespace - pot.iSpace)
|Armour arm ->
thespace <- (thespace - arm.iSpace)
count <- count+1
thespace
I get an error about Int32, that has to do with the
printf "Count: %i \n" inventory.Count
line
Another problem is that thespace doesn't seem to change, and always returns as 60, although I have checked and inventory is not empty, it always has at least two items, 1 weapon and 1 armour, so thespace should atleast decrease yet it never does.
Other snippets that may help:
let inventory = ResizeArray[]
let initialise =
let mutable listr = roominit
let mutable curroom = 3
let mutable dead = false
inventory.Add(Weapon weap1)
inventory.Add(Armour a1)
let spacetogo = spaceleft //returns 60, although it should not
Also, apart from the iniitialise function, other functions seem not to be able to add items to the inventory properly, eg:
let ok, input = Int32.TryParse(Console.ReadLine())
match ok with
|false ->
printf "The weapon was left here \n"
complete <- false
|true ->
if input = 1 && spaceleft>= a.iSpace then
inventory.Add(Weapon a)
printf "\n %s added to the inventory \n" a.name
complete <- true
else
printf "\n The weapon was left here \n"
complete <- false
complete
You have spaceLeft as a constant value. To make it a function you need to add unit () as a parameter. Here's that change including a modification to make it much simpler (I've included my dummy types):
type X = { iSpace : int }
type Item = Weapon of X | Bomb of X | Potion of X | Armour of X
let inventory = ResizeArray [ Weapon {iSpace = 2}; Bomb {iSpace = 3} ]
let spaceleft () =
let mutable thespace = 60 //the space left in the inventory
printf "Count: %i \n" inventory.Count
for item in inventory do
let itemSpace =
match item with
| Weapon w -> w.iSpace
| Bomb b -> b.iSpace
| Potion p -> p.iSpace
| Armour a -> a.iSpace
thespace <- thespace - itemSpace
thespace
spaceleft () // 55
The above code is quite imperative. If you want to make it more functional (and simpler still) you can use Seq.sumBy:
let spaceleft_functional () =
printf "Count: %i \n" inventory.Count
let spaceUsed =
inventory
|> Seq.sumBy (function
| Weapon w -> w.iSpace
| Bomb b -> b.iSpace
| Potion p -> p.iSpace
| Armour a -> a.iSpace)
60 - spaceUsed
Just adding to the accepted answer: you can also match against record labels, as long as your inner types are records. Combine with an intrinsic type extension on the outer DU:
type X = { iSpace : int }
type Y = { iSpace : int }
type Item = Weapon of X | Bomb of Y | Potion of X | Armour of X
let inventory = ResizeArray [ Weapon {iSpace = 2}; Bomb {iSpace = 3} ]
let itemSpace = function
| Weapon { iSpace = s } | Bomb { iSpace = s }
| Potion { iSpace = s } | Armour { iSpace = s } -> s
type Item with static member (+) (a, b) = a + itemSpace b
60 - (Seq.fold (+) 0 inventory)
// val it : int = 55
Otherwise, you could resort to member constraint invocation expressions.
let inline space (x : ^t) = (^t : (member iSpace : int) (x))

Padding printed output of tabular data

I know this is probably dead simple, but I've got some data such as this in one file:
Artichoke
Green Globe, Imperial Star, Violetto
24" deep
Beans, Lima
Bush Baby, Bush Lima, Fordhook, Fordhook 242
12" wide x 8-10" deep
that I'd like to be able to format into a nice TSV type of table, to look something like this:
Name | Varieties | Container Data
----------|------------- |-------
some data here nicely padded with even spacing and right aligned text
Try String#rjust(width):
"hello".rjust(20) #=> " hello"
I wrote a gem to do exactly this: http://tableprintgem.com
No one has mentioned the "coolest" / most compact way -- using the % operator -- for example: "%10s %10s" % [1, 2]. Here is some code:
xs = [
["This code", "is", "indeed"],
["very", "compact", "and"],
["I hope you will", "find", "it helpful!"],
]
m = xs.map { |_| _.length }
xs.each { |_| _.each_with_index { |e, i| s = e.size; m[i] = s if s > m[i] } }
xs.each { |x| puts m.map { |_| "%#{_}s" }.join(" " * 5) % x }
Gives:
This code is indeed
very compact and
I hope you will find it helpful!
Here is the code made more readable:
max_lengths = xs.map { |_| _.length }
xs.each do |x|
x.each_with_index do |e, i|
s = e.size
max_lengths[i] = s if s > max_lengths[i]
end
end
xs.each do |x|
format = max_lengths.map { |_| "%#{_}s" }.join(" " * 5)
puts format % x
end
This is a reasonably full example that assumes the following
Your list of products is contained in a file called veg.txt
Your data is arranged across three lines per record with the fields on consecutive lines
I am a bit of a noob to rails so there are undoubtedly better and more elegant ways to do this
#!/usr/bin/ruby
class Vegetable
##max_name ||= 0
##max_variety ||= 0
##max_container ||= 0
attr_reader :name, :variety, :container
def initialize(name, variety, container)
#name = name
#variety = variety
#container = container
##max_name = set_max(#name.length, ##max_name)
##max_variety = set_max(#variety.length, ##max_variety)
##max_container = set_max(#container.length, ##max_container)
end
def set_max(current, max)
current > max ? current : max
end
def self.max_name
##max_name
end
def self.max_variety
##max_variety
end
def self.max_container()
##max_container
end
end
products = []
File.open("veg.txt") do | file|
while name = file.gets
name = name.strip
variety = file.gets.to_s.strip
container = file.gets.to_s.strip
veg = Vegetable.new(name, variety, container)
products << veg
end
end
format="%#{Vegetable.max_name}s\t%#{Vegetable.max_variety}s\t%#{Vegetable.max_container}s\n"
printf(format, "Name", "Variety", "Container")
printf(format, "----", "-------", "---------")
products.each do |p|
printf(format, p.name, p.variety, p.container)
end
The following sample file
Artichoke
Green Globe, Imperial Star, Violetto
24" deep
Beans, Lima
Bush Baby, Bush Lima, Fordhook, Fordhook 242
12" wide x 8-10" deep
Potatoes
King Edward, Desiree, Jersey Royal
36" wide x 8-10" deep
Produced the following output
Name Variety Container
---- ------- ---------
Artichoke Green Globe, Imperial Star, Violetto 24" deep
Beans, Lima Bush Baby, Bush Lima, Fordhook, Fordhook 242 12" wide x 8-10" deep
Potatoes King Edward, Desiree, Jersey Royal 36" wide x 8-10" deep
another gem: https://github.com/visionmedia/terminal-table
Terminal Table is a fast and simple, yet feature rich ASCII table generator written in Ruby.
I have a little function to print a 2D array as a table. Each row must have the same number of columns for this to work. It's also easy to tweak to your needs.
def print_table(table)
# Calculate widths
widths = []
table.each{|line|
c = 0
line.each{|col|
widths[c] = (widths[c] && widths[c] > col.length) ? widths[c] : col.length
c += 1
}
}
# Indent the last column left.
last = widths.pop()
format = widths.collect{|n| "%#{n}s"}.join(" ")
format += " %-#{last}s\n"
# Print each line.
table.each{|line|
printf format, *line
}
end
Kernel.sprintf should get you started.

Resources