Title: Runtime error in Example A-2 · Issue #52 · fluentpython/example-code · GitHub
Open Graph Title: Runtime error in Example A-2 · Issue #52 · fluentpython/example-code
X Title: Runtime error in Example A-2 · Issue #52 · fluentpython/example-code
Description: I'm trying to run the Example A-2 (page 689 in the First Edition). I'm not sure if it's something wrong that I'm doing.. but I read the code 10 times. I'm sure it is the same of the book. I'm running this code as: $python example_a_2__ge...
Open Graph Description: I'm trying to run the Example A-2 (page 689 in the First Edition). I'm not sure if it's something wrong that I'm doing.. but I read the code 10 times. I'm sure it is the same of the book. I'm runni...
X Description: I'm trying to run the Example A-2 (page 689 in the First Edition). I'm not sure if it's something wrong that I'm doing.. but I read the code 10 times. I'm sure it is the same of...
Opengraph URL: https://github.com/fluentpython/example-code/issues/52
X: @github
Domain: redirect.github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Runtime error in Example A-2","articleBody":"I'm trying to run the `Example A-2` (page 689 in the First Edition).\r\nI'm not sure if it's something wrong that I'm doing.. but I read the code 10 times. I'm sure it is the same of the book.\r\n\r\nI'm running this code as:\r\n```\r\n$python example_a_2__generate_array.py\r\n```\r\nwhere my Python version is `3.8.5`. Then I see this error:\r\n```\r\ninitial sample: 10500000 elements\r\nTraceback (most recent call last):\r\n File \"example_a_2__generate_array.py\", line 19, in \u003cmodule\u003e\r\n with len(sample) \u003c SAMPLE_LEN:\r\nAttributeError: __enter__\r\n```\r\n\r\n**Some reasoning:**\r\nI'm not reasigning the `open` function (as suggested [here](https://stackoverflow.com/questions/53564755/python-error-attributeerror-enter) for one possible cause).\r\n\r\n**Possible solution:**\r\nMaybe line 19 should be replaced by:\r\n```\r\nif len(sample) \u003c SAMPLE_LEN:\r\n```\r\n\r\nI tried that and got a good looking output:\r\n```\r\ninitial sample: 10500000 elements\r\ncomplete sample: 10500000 elements\r\nnot selected: 500000 samples\r\n writing not_selected.arr\r\nselected: 10000000 samples\r\n writing selected.arr\r\n ```\r\n \r\n And they seem to have been created correctly:\r\n```\r\n$ls -la *selected*\r\n-rw-rw-r-- 1 lucas lucas 4000000 mai 13 14:57 not_selected.arr\r\n-rw-rw-r-- 1 lucas lucas 80000000 mai 13 14:57 selected.arr\r\n```\r\n\r\n\r\nPS.: Thank you for the book and sharing your knowledge!","author":{"url":"https://github.com/CavalcanteLucas","@type":"Person","name":"CavalcanteLucas"},"datePublished":"2021-05-13T18:55:21.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":0},"url":"https://github.com/52/example-code/issues/52"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:2edeacc5-52a4-881b-79b6-34a29a4b28bd |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | AA96:3C62DE:4E31402:6681A3C:696D70BF |
| html-safe-nonce | bfbcbc3611c3798945b42c8e1933e862f3ad17bca0edb5a751a433b2d2b33d81 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBQTk2OjNDNjJERTo0RTMxNDAyOjY2ODFBM0M6Njk2RDcwQkYiLCJ2aXNpdG9yX2lkIjoiMzEzNzE0NTY5NTAzODY5NzY2MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 956015bb27f3a4bd05f5687a6366f1e2ed28a8ab3827d9897c1028fd45df9f66 |
| hovercard-subject-tag | issue:891288530 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/fluentpython/example-code/52/issue_layout |
| twitter:image | https://opengraph.githubassets.com/7460fdcbcacf1c2b74d5ddebe3e8fa4e90c2b8792c5d8831219671340a85d05d/fluentpython/example-code/issues/52 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/7460fdcbcacf1c2b74d5ddebe3e8fa4e90c2b8792c5d8831219671340a85d05d/fluentpython/example-code/issues/52 |
| og:image:alt | I'm trying to run the Example A-2 (page 689 in the First Edition). I'm not sure if it's something wrong that I'm doing.. but I read the code 10 times. I'm sure it is the same of the book. I'm runni... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | CavalcanteLucas |
| hostname | github.com |
| expected-hostname | github.com |
| None | 4922b452d03cd8dbce479d866a11bc25b59ef6ee2da23aa9b0ddefa6bd4d0064 |
| turbo-cache-control | no-preview |
| go-import | github.com/fluentpython/example-code git https://github.com/fluentpython/example-code.git |
| octolytics-dimension-user_id | 9216311 |
| octolytics-dimension-user_login | fluentpython |
| octolytics-dimension-repository_id | 25216588 |
| octolytics-dimension-repository_nwo | fluentpython/example-code |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 25216588 |
| octolytics-dimension-repository_network_root_nwo | fluentpython/example-code |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7e5ae23c70136152637ceee8d6faceb35596ec46 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width