Imports are sorted -- nice!
optional type hinting
def int_sq(n):
Consider using this signature:
def int_sq(n: float) -> int:
(Recall that float
includes
int
for hinting purposes.)
sum, map
sq_sum = sum(int_sq(i) for i in [a, b])
Consider phrasing this as
sq_sum = sum(map(int_sq, [a, b]))
since the temp var i
is uninteresting and could be anonymous.
Also, delete the meaningless "#check int" comment.
And maybe just return
the expression without giving it a name.
docstring
The identifier square_hyp_triangle
is fine.
But do write an English sentence for it:
def square_hyp_triangle(a: float, b: float) -> float:
"""Finds the squared length of a triangle's hypotenuse.
"""
Consider writing another few lines so that
$ python -m doctest *.py
does an automated test to verify things work as intended.
"""Finds the squared length of a triangle's hypotenuse.
>>> square_hyp_triangle(3, 5)
34
"""
typer
In main
the parser is nice enough,
though "import" perhaps is a slightly odd verb.
I'm glad you're using that library; it reduces the documentation burden for end users.
Consider switching to typer,
which gives you similar functionality "for free" (if you're using type hints).
I'm slightly surprised by the absence of ... , default=sys.stdin)
,
but I guess your defaulting must work fine.
The filename
identifier seems to have the wrong name,
given that you want it be an open file descriptor rather than a name.
In line.rstrip().split()
maybe you had some CR/LF trouble?
I don't see why you need to strip, given that split will discard whitespace for you.
tuple unpack
Avoid cryptic [0]
/ [1]
subscripts,
preferring a, b = line.split()
.
Or better, convert from str
right here using:
a, b = map(float, line.split())
Aha, now I understand that return int(n)**2
expression.
That doesn't seem a very sensible place to do the conversion.
I wouldn't really expect that a function could square "3"
.
And even if it did, should I get back "9"
instead of 9
?
Best to not even explore such territory, and stick to numeric values,
so the squaring function has a
single responsibility
and is not also responsible for type conversions.
Better to allocate that responsibility to the CLI interface called main
.
Rejecting fractional inputs like 3.1
is maybe an unusual design choice.
Guess it stems from the "Given:" and "Return:" parts of the spec, OK.
Rosalind's "download a one-line text file dataset" requirement
pushes this code in a slightly odd direction.
old debugs
#print(line[0], line[1])
It served a purpose at one time.
But now that your code is "done" and you're requesting review,
it is the right time to tidy up and delete debug statements.
Which parts can be improved?
Adhere to Single Responsibility.
Recommend you adopt black
auto-formatting, so expressions have a uniform appearance
without even having to think about it.