Last active
December 18, 2019 12:01
-
-
Save dckc/d58a04b154d44d46de9abcda84b2a2f0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Simple fizzbuzz generator. | |
The game proceeds with players announcing numbers increasing | |
sequentially, except for multiples of 3 and 5: | |
>>> usage = 'fizzbuzz 1 20' | |
>>> from io import StringIO; stdout = StringIO() | |
>>> main(usage.split(), stdout) | |
>>> print(stdout.getvalue()) | |
... #doctest: +ELLIPSIS | |
1 | |
2 | |
fizz | |
4 | |
buzz | |
fizz | |
7 | |
... | |
14 | |
fizzbuzz | |
16 | |
... | |
""" | |
def main(argv, stdout): | |
[lo_, hi_] = argv[1:3] | |
lo, hi = int(lo_), int(hi_) | |
for word in fizzbuzz(lo, hi): | |
print(word, file=stdout) | |
def fizzbuzz(lo, hi): | |
""" | |
>>> list(fizzbuzz(1, 6)) | |
[1, 2, 'fizz', 4, 'buzz'] | |
""" | |
for n in range(lo, hi): | |
if n % 3 == 0 and n % 5 == 0: | |
yield "fizzbuzz" | |
elif n % 3 == 0: | |
yield "fizz" | |
elif n % 5 == 0: | |
yield "buzz" | |
else: | |
yield n | |
if __name__ == '__main__': | |
def _script_io(): | |
from sys import argv, stdout | |
main(argv, stdout) | |
_script_io() |
because then sys
would be visible at module scope and some function in the middle of the module could just refer to sys.stdin
or sys.version
or the like without having that authority passed in explicitly. That would violate ocap discipline, making the code hard to audit and test.
but yes, I neglected to explain that.
Okay, then I get it.
I suppose I could del _script_io
before calling main()
to avoid some risks.
This is still sort of voluntary compliance. Python is nearly impossible to completely secure. Only languages like monte, E, pony, and Joe-E actually enforce ocap discipline for us.
(did you delete this comment?)
anyway... you might want to look at
https://monte.readthedocs.io/en/latest/intro.html#object-capability-discipline
…On Fri, Jul 26, 2019 at 8:21 AM Pablo Fernandez ***@***.***> wrote:
What is ocap discipline?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/d58a04b154d44d46de9abcda84b2a2f0?email_source=notifications&email_token=AABE3SRNRPJTD2KD7FN4N6TQBL26FA5CNFSM4IG42MN2YY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFV7U6#gistcomment-2981711>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AABE3SXAUR6XBZH4RXXXJGTQBL26FANCNFSM4IG42MNQ>
.
--
Dan Connolly
http://www.madmode.com/
Yeah, found out that the explanation was in your post
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I browsed your article but did not understand why you introduce
_script_io()
instead of just use