How to break a line of chained methods in Python?
Question:
I have a line of the following code (don’t blame for naming conventions, they are not mine):
subkeyword = Session.query(
Subkeyword.subkeyword_id, Subkeyword.subkeyword_word
).filter_by(
subkeyword_company_id=self.e_company_id
).filter_by(
subkeyword_word=subkeyword_word
).filter_by(
subkeyword_active=True
).one()
I don’t like how it looks like (not too readable) but I don’t have any better idea to limit lines to 79 characters in this situation. Is there a better way of breaking it (preferably without backslashes)?
Answers:
Just store the intermediate result/object and invoke the next method on it,
e.g.
q = Session.query(Subkeyword.subkeyword_id, Subkeyword.subkeyword_word)
q = q.filter_by(subkeyword_company_id=self.e_company_id)
q = q.filter_by(subkeyword_word=subkeyword_word)
q = q.filter_by(subkeyword_active=True)
subkeyword = q.one()
You could use additional parentheses:
subkeyword = (
Session.query(Subkeyword.subkeyword_id, Subkeyword.subkeyword_word)
.filter_by(subkeyword_company_id=self.e_company_id)
.filter_by(subkeyword_word=subkeyword_word)
.filter_by(subkeyword_active=True)
.one()
)
According to Python Language Reference
You can use a backslash.
Or simply break it. If a bracket is not paired, python will not treat that as a line. And under such circumstance, the indentation of following lines doesn’t matter.
You seems using SQLAlchemy, if it is true, sqlalchemy.orm.query.Query.filter_by()
method takes multiple keyword arguments, so you could write like:
subkeyword = Session.query(Subkeyword.subkeyword_id,
Subkeyword.subkeyword_word)
.filter_by(subkeyword_company_id=self.e_company_id,
subkeyword_word=subkeyword_word,
subkeyword_active=True)
.one()
But it would be better:
subkeyword = Session.query(Subkeyword.subkeyword_id,
Subkeyword.subkeyword_word)
subkeyword = subkeyword.filter_by(subkeyword_company_id=self.e_company_id,
subkeyword_word=subkeyword_word,
subkeyword_active=True)
subkeuword = subkeyword.one()
My personal choice would be:
subkeyword = Session.query(
Subkeyword.subkeyword_id,
Subkeyword.subkeyword_word,
).filter_by(
subkeyword_company_id=self.e_company_id,
subkeyword_word=subkeyword_word,
subkeyword_active=True,
).one()
This is a case where a line continuation character is preferred to open parentheses. The need for this style becomes more obvious as method names get longer and as methods start taking arguments:
subkeyword = Session.query(Subkeyword.subkeyword_id, Subkeyword.subkeyword_word)
.filter_by(subkeyword_company_id=self.e_company_id)
.filter_by(subkeyword_word=subkeyword_word)
.filter_by(subkeyword_active=True)
.one()
PEP 8 is intend to be interpreted with a measure of common-sense and an eye for both the practical and the beautiful. Happily violate any PEP 8 guideline that results in ugly or hard to read code.
That being said, if you frequently find yourself at odds with PEP 8, it may be a sign that there are readability issues that transcend your choice of whitespace 🙂
It’s a bit of a different solution than provided by others but a favorite of mine since it leads to nifty metaprogramming sometimes.
base = [Subkeyword.subkeyword_id, Subkeyword_word]
search = {
'subkeyword_company_id':self.e_company_id,
'subkeyword_word':subkeyword_word,
'subkeyword_active':True,
}
subkeyword = Session.query(*base).filter_by(**search).one()
This is a nice technique for building searches. Go through a list of conditionals to mine from your complex query form (or string-based deductions about what the user is looking for), then just explode the dictionary into the filter.
I like to indent the arguments by two blocks, and the statement by one block, like these:
for image_pathname in image_directory.iterdir():
image = cv2.imread(str(image_pathname))
input_image = np.resize(
image, (height, width, 3)
).transpose((2,0,1)).reshape(1, 3, height, width)
net.forward_all(data=input_image)
segmentation_index = net.blobs[
'argmax'
].data.squeeze().transpose(1,2,0).astype(np.uint8)
segmentation = np.empty(segmentation_index.shape, dtype=np.uint8)
cv2.LUT(segmentation_index, label_colours, segmentation)
prediction_pathname = prediction_directory / image_pathname.name
cv2.imwrite(str(prediction_pathname), segmentation)
I have a line of the following code (don’t blame for naming conventions, they are not mine):
subkeyword = Session.query(
Subkeyword.subkeyword_id, Subkeyword.subkeyword_word
).filter_by(
subkeyword_company_id=self.e_company_id
).filter_by(
subkeyword_word=subkeyword_word
).filter_by(
subkeyword_active=True
).one()
I don’t like how it looks like (not too readable) but I don’t have any better idea to limit lines to 79 characters in this situation. Is there a better way of breaking it (preferably without backslashes)?
Just store the intermediate result/object and invoke the next method on it,
e.g.
q = Session.query(Subkeyword.subkeyword_id, Subkeyword.subkeyword_word)
q = q.filter_by(subkeyword_company_id=self.e_company_id)
q = q.filter_by(subkeyword_word=subkeyword_word)
q = q.filter_by(subkeyword_active=True)
subkeyword = q.one()
You could use additional parentheses:
subkeyword = (
Session.query(Subkeyword.subkeyword_id, Subkeyword.subkeyword_word)
.filter_by(subkeyword_company_id=self.e_company_id)
.filter_by(subkeyword_word=subkeyword_word)
.filter_by(subkeyword_active=True)
.one()
)
According to Python Language Reference
You can use a backslash.
Or simply break it. If a bracket is not paired, python will not treat that as a line. And under such circumstance, the indentation of following lines doesn’t matter.
You seems using SQLAlchemy, if it is true, sqlalchemy.orm.query.Query.filter_by()
method takes multiple keyword arguments, so you could write like:
subkeyword = Session.query(Subkeyword.subkeyword_id,
Subkeyword.subkeyword_word)
.filter_by(subkeyword_company_id=self.e_company_id,
subkeyword_word=subkeyword_word,
subkeyword_active=True)
.one()
But it would be better:
subkeyword = Session.query(Subkeyword.subkeyword_id,
Subkeyword.subkeyword_word)
subkeyword = subkeyword.filter_by(subkeyword_company_id=self.e_company_id,
subkeyword_word=subkeyword_word,
subkeyword_active=True)
subkeuword = subkeyword.one()
My personal choice would be:
subkeyword = Session.query( Subkeyword.subkeyword_id, Subkeyword.subkeyword_word, ).filter_by( subkeyword_company_id=self.e_company_id, subkeyword_word=subkeyword_word, subkeyword_active=True, ).one()
This is a case where a line continuation character is preferred to open parentheses. The need for this style becomes more obvious as method names get longer and as methods start taking arguments:
subkeyword = Session.query(Subkeyword.subkeyword_id, Subkeyword.subkeyword_word)
.filter_by(subkeyword_company_id=self.e_company_id)
.filter_by(subkeyword_word=subkeyword_word)
.filter_by(subkeyword_active=True)
.one()
PEP 8 is intend to be interpreted with a measure of common-sense and an eye for both the practical and the beautiful. Happily violate any PEP 8 guideline that results in ugly or hard to read code.
That being said, if you frequently find yourself at odds with PEP 8, it may be a sign that there are readability issues that transcend your choice of whitespace 🙂
It’s a bit of a different solution than provided by others but a favorite of mine since it leads to nifty metaprogramming sometimes.
base = [Subkeyword.subkeyword_id, Subkeyword_word]
search = {
'subkeyword_company_id':self.e_company_id,
'subkeyword_word':subkeyword_word,
'subkeyword_active':True,
}
subkeyword = Session.query(*base).filter_by(**search).one()
This is a nice technique for building searches. Go through a list of conditionals to mine from your complex query form (or string-based deductions about what the user is looking for), then just explode the dictionary into the filter.
I like to indent the arguments by two blocks, and the statement by one block, like these:
for image_pathname in image_directory.iterdir():
image = cv2.imread(str(image_pathname))
input_image = np.resize(
image, (height, width, 3)
).transpose((2,0,1)).reshape(1, 3, height, width)
net.forward_all(data=input_image)
segmentation_index = net.blobs[
'argmax'
].data.squeeze().transpose(1,2,0).astype(np.uint8)
segmentation = np.empty(segmentation_index.shape, dtype=np.uint8)
cv2.LUT(segmentation_index, label_colours, segmentation)
prediction_pathname = prediction_directory / image_pathname.name
cv2.imwrite(str(prediction_pathname), segmentation)