You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Yes, I can also confirm this. I think the method the python script uses disregards some bytes. I double checked using the Linux's wc command @kyclark
do you have any input on this?
I just saw in the book the solution is correct on page 111, when it uses the linux's wc command it has the 714 total, but the test.py in the repo is wrong.
However, when we enter solution.py into the test.py in the repo it passes all the test.
when we compare the two:
>>> import os
>>> len(open('../inputs/sonnet-29.txt').read())
661
>>> os.path.getsize('../inputs/sonnet-29.txt')
669
>>>
We can see that the len of the file's string isn't the same as a byte size.
Basically the different between the two is explained here:
A. To count number of characters in str object, you can use len() function:
>>> print(len('please anwser my question'))
25
B. To get memory size in bytes allocated to store str object, you can use sys.getsizeof() function
>>> from sys import getsizeof
>>> print(getsizeof('please anwser my question'))
50
Well, first off, my apologies for taking so long to address the original bug @safeisrisky. Yes, I can see there is a discrepancy between how I'm counting bytes using the length of a string and the actual size of the string. At this point, I can't fix the book, so I think it will just have to remain "wrong" where here "wrong" means "not the same as wc." It's unfortunate, but the point of the exercise is to get the reader to think about lines, words, and characters. I hate that I missed the distinction between characters and memory! Thanks for pointing this out.
Hi!
I wanted to say one last thing, which is mostly for anyone curious about this. There isn't anything "wrong" with the code. The "issue" occurs because Python strings are in latin1, and the text file (sonnet-29.txt) has UTF8 characters (the apostrophes). So when we read the string, it returns a slightly smaller number. The easiest way to "fix" this is to change one variable in the solution code to:
num_bytes += len(line.encode('UTF8'))
With this change, there should be a change in the test.py
Also, thank you @kyclark for all the work you did on this book. I'm really enjoying it.
Activity
AlbertUlysses commentedon Jan 29, 2021
Yes, I can also confirm this. I think the method the python script uses disregards some bytes. I double checked using the Linux's wc command
@kyclark
do you have any input on this?
AlbertUlysses commentedon Jan 29, 2021
I just saw in the book the solution is correct on page 111, when it uses the linux's wc command it has the 714 total, but the
test.pyin the repo is wrong.However, when we enter
solution.pyinto thetest.pyin the repo it passes all the test.when we compare the two:
We can see that the len of the file's string isn't the same as a byte size.
Basically the different between the two is explained here:
source: https://stackoverflow.com/questions/4967580/how-to-get-the-size-of-a-string-in-python
kyclark commentedon Jan 30, 2021
Well, first off, my apologies for taking so long to address the original bug @safeisrisky. Yes, I can see there is a discrepancy between how I'm counting bytes using the length of a string and the actual size of the string. At this point, I can't fix the book, so I think it will just have to remain "wrong" where here "wrong" means "not the same as wc." It's unfortunate, but the point of the exercise is to get the reader to think about lines, words, and characters. I hate that I missed the distinction between characters and memory! Thanks for pointing this out.
AlbertUlysses commentedon Feb 4, 2021
Hi!
I wanted to say one last thing, which is mostly for anyone curious about this. There isn't anything "wrong" with the code. The "issue" occurs because Python strings are in latin1, and the text file (sonnet-29.txt) has UTF8 characters (the apostrophes). So when we read the string, it returns a slightly smaller number. The easiest way to "fix" this is to change one variable in the solution code to:
num_bytes += len(line.encode('UTF8'))With this change, there should be a change in the
test.pyAlso, thank you @kyclark for all the work you did on this book. I'm really enjoying it.
completed and updated projects kyclark#1-3
Refactor source code