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: patch-diff.githubusercontent.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:af4c8806-0d4c-1a08-f102-a97dcb955a8f |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 94D2:9E1F1:38C10D8:4F53928:697128F8 |
| html-safe-nonce | e4ec718621a2126055966c372be2df52fcbacaa3ee78b1e9e45da9884ba50829 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5NEQyOjlFMUYxOjM4QzEwRDg6NEY1MzkyODo2OTcxMjhGOCIsInZpc2l0b3JfaWQiOiIyMjc1MDg1MjI5NzU3NjM4OTA1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 0e06d13f88f084e4230649e8f959a284ac27dfa3ca85ca0b459368ab4c376baf |
| 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 | d8bf5373bf714a973a48ff6e39919055ffeb8fb275ee80b009c0532d6e0f0c68 |
| 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 | 4fbcc41ab94dbb05f665e4608286c36baa99ad43 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width