104
u/campbellm Dec 22 '18
Ignore the snarky comments here; as an also wannabe woodworker, this is adorable.
25
Dec 22 '18
I agree.
As Pythonic as that snark is, this is purely adorbable and I'd love one.
...May have to steal it.
2
Dec 22 '18
Yes, I can't believe that a smart-arse comment showing a shorter version of the script, as saying look ma, I can write more pythonic code (compared to 3 simple lines written by a teenager) is getting above 300 upvotes. Should expect less of people.
1
u/Theoz Dec 22 '18
Will you explain? I'm new. I know len and random and [ brackets] for lists but that's it. It looks like it's counting the length of cut chop dice but I don't really know
6
u/halooooom Dec 22 '18
The explanation of the snarky comments is that the python code shown is not very Pythonic as its not as readable as possible. In other languages this is the normal solution to select a random item from a list but python already has this function natively.
1
0
62
u/Undercoversongs Dec 22 '18
Not using random.choice smh
11
u/iamapieceofcheese Dec 22 '18
So inefficient
2
u/Luke_myLord Dec 22 '18
Can you elaborate?
8
u/iamapieceofcheese Dec 22 '18
n = random.randint(0, len(action)-1)
print(action[n])takes 2 lines of code, but you can just use
random.choice(action)If I were to write the code in the picture, I would use
import random
print(random.choice(['slice', 'chop', 'dice']))10
u/primitive_screwhead Dec 22 '18
"so inefficient" doesn't usually apply to using an extra line of code. FWIW, the main efficiency benefit of random.choice() is that it's arguments are easier to parse than randint().
3
u/iamapieceofcheese Dec 22 '18
Oops, sorry
4
u/primitive_screwhead Dec 22 '18
No worries; I'm just adding some sizzle. Your suggestions are perfectly fine, though it might make the cuttingboard too "sparse"... I think we all agree that the aesthetic was the most important issue as to how this particular code was written. :)
3
2
u/Ph0X Dec 22 '18
To be clear, it's not so much about using more lines of code. It's about using the right APIs and being more explicit about what you're doing. I often write code out to use more lines of code if it makes the code cleaner and more explicit. The other one is inherently easier to read and follow the logic.
1
44
u/kashmill Dec 22 '18
Requesting changes: Missing spaces after the , in the list and the parameters. Missing spaces around the -. Fails PEP8 check.
-2
u/BRENNEJM Dec 22 '18
PEP8 wants spaces like that? Weird. I never include spaces there.
13
u/zoomertherumour Dec 22 '18
A comma without a space has no place in this universe.
4
u/BRENNEJM Dec 22 '18
Except here apparently:
Yes: foo = (0,)
No: bar = (0, )2
u/Ph0X Dec 22 '18
tbf that's one of my least favorite syntaxes, though I see the need. I always feel strange when I see
items + (item,)Sometimes I wonder how i'd look if tuples had similar .append and .extend but instead returned a new tuple
1
u/primitive_screwhead Dec 22 '18
if tuples had similar .append and .extend but instead returned a new tuple
That breaks the principle of least surprise, imo. (because it's arbitrarily different from lists)
2
3
u/alkasm github.com/alkasm Dec 22 '18
Which one, after the comma or around an operator? PEP8 suggests both but I disregard the one about operators. I do use spaces for add and subtract, but not for multiply and divide...just too far off from mathematics for me at that point.
2
u/BRENNEJM Dec 22 '18
I don’t use them in lists. It sounds like we write math the same.
According to PEP8 I shouldn’t be using mixedCase to name everything either. Maybe this weekend is a good time to finally read through PEP8.
2
u/alkasm github.com/alkasm Dec 22 '18
Yes, lowerCamelCase is super annoying to most Python programmers, lol!
Remember though, PEP 8 is suggestions. For small things like spacing, use what works for you. It is more important to be consistent. But being consistent with other projects is a nice feature, in which case following PEP 8 more strictly is likely to get you there.
1
1
6
3
5
u/beatryder Dec 22 '18
I would buy this as a gift for a colleague
7
Dec 22 '18
Same! Especially with the code as is instead of using
random.choice()so I could watch their eyes twitch between appreciation and desire to change the code
2
2
u/tdobzhansky Dec 22 '18
I don't think a stochastic approach to food preparation is a great idea, but then again at least he's actually preparing his own food.
2
u/lwrightjs Dec 22 '18
went to 12 week python bootcamp
can't get a job so pwns 15 y/o's heartfelt work
Hashtag new-age programmers
1
u/LiquidSubtitles Dec 22 '18
Spent a few hours figuring out that the package I was using used both random and numpy.random so that even that even when I saved the numpy seed I couldn't reproduce my results. I now get mildly angry whenever I see random..
Anyway cool wood!
1
u/sezar13 Dec 22 '18
This is a game i created on python while my professor asked me to make something that i liked : from random import choice
play = True
choices = ["rock", "scissors", "paper"]
Compares two choices, returns true if the first wins
False, if the second wins
None if the two are equal
def compare(a, b): if a == b: return None elif (choices.index(a) + 1) % len(choices) == choices.index(b) or
(choices.index(a) + 3) % len(choices) == choices.index(b): return True else: return False
1
1
1
1
1
-13
0
Dec 22 '18
[deleted]
7
u/nerdmor Dec 22 '18
from random import choice
16
u/artificialignorance Dec 22 '18 edited Dec 22 '18
print(__import__('random').choice([‘slice', 'chop', 'dice’]))4
u/haliax69 Dec 22 '18
TIL I can import modules this way. Thanks!
6
u/Ph0X Dec 22 '18
Please don't. It's good to know, but almost never the right choice.
And if you need dynamic imports, use importlib instead.
1
u/haliax69 Dec 22 '18
Oh, I kinda figured that, but it's nice to use when running commands directly from the command line like:
python3 -c "print(__import__('datetime').datetime.now().strftime('%d, %b %Y'))"3
u/Ph0X Dec 22 '18
Well on there you can use semicolon, that's still probably cleaner
python3 -c "import datetime; print(datetime.now().strftime('%d, %b %Y'))"1
u/haliax69 Dec 22 '18
Learned something else today: semicolon = new line. Thanks man!
Just out of curiosity, is there something that replaces indentation?
0
Dec 22 '18
[deleted]
2
u/wrboyce Dec 22 '18
Not only are your comments excessively verbose, they are incorrect. The second line isn’t casting anything, don’t use words you don’t understand or you’re likely to use them wrong. Same for the third line, no casting.
21
Dec 22 '18
[removed] — view removed comment
14
u/primitive_screwhead Dec 22 '18
programmer her self!
You know you've written a lot of Python when you split "herself" into "her self" out of habit. ;)
20
369
u/lelease Dec 22 '18
print(random.choice(action))