News for hackers. Stuff that matters.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by gabrielsroka on Sunday November 22, 2020 @03:23PM (#25180705)
I tried a few (not all) of the examples with plain old Python (granted, these are multi-liners, not one-liners, but I think they're easier to read). I'm not sure I see the advantange of curly braces.

  # --- Example 1
  
  # pwk
  pwk 'if "braces"=="bad": { print("Be gone!"); exit(99) } else: { print("Howdy!") }'
  
  # vs
  
  # python
  python -c 'if "braces"=="bad": print("Be gone!"); exit(99)
  else: print("Howdy!")'
  
  
  # --- Example 2
  
  pwk 'def s2i(s): { return int(s) } print(s2i("41")+1)'
  
  python -c 'def s2i(s): return int(s)
  print(s2i("41")+1)'
  
  
  # --- Example 3
  
  ls / | pwk 'for s in sys.stdin: try: { print(os.listdir("/"+s.strip())) } except: pass'
  
  ls / | python -c 'import sys, os
  for s in sys.stdin:
   try: print(os.listdir("/"+s.strip()))
   except: pass'
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by Wowfunhappy on Sunday November 22, 2020 @10:48PM (#25183394)
The advantage is the one-line versions can be easily written within the Terminal.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by neycmrtn on Monday November 23, 2020 @05:03AM (#25185174)
You're braver than I -- whenever a shell/terminal indicates to me it expects multiline now, I press, in that order, Ctrl+C, Ctrl+D, Ctrl+Z, and Ctrl+Alt+Del.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by cutler on Sunday November 22, 2020 @10:46PM (#25183387)
Face it - Python just ain't fit for one-liners. Give Perl and Ruby some credit as they were designed for conciseness and the command-line. Python may dominate data science, AI and ML but it's deficiencies bleed through when you try to compose something elegant and concise.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by abhgh on Monday November 23, 2020 @04:23AM (#25184992)
I agree about Perl; haven't done a lot of one liners with Ruby when I was using the language. Perl was made to be really compact to the detriment of being hard to read. I think Python strikes a nice balance, and you can compress it to a point. In discussions like these I always like to bring out my one-liner k-nearest neighbor classifier [1]! Explanation here [2] (better on a desktop).

[1] https://twitter.com/AbhishekGhose/status/500564420733304833?...

[2] http://quipu-strands.blogspot.com/2014/08/knn-classifier-in-...

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by xupybd on Monday November 23, 2020 @12:52AM (#25183963)
I really don't understand the love of one liners. Yes it's cool to see how much can be done in one line but it's so much harder to read. Isn't legibility far better than compactness?
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by neycmrtn on Monday November 23, 2020 @04:38AM (#25185068)
Aye, in general legibility >> compactness. Apart from the intellectual exercise (fun; !work), one-liners imo come in handy in:

>some_complex_tool | 'my one liner' | some_other_complex_tool > result.txt

Not putting the one-liner logic into a separate script avoids an additional lookup for someone learning what is going on, along with any deployment/path/.. dependencies to that separate script.

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by pfranz on Monday November 23, 2020 @01:03AM (#25184013)
It depends on the context. I like one-liners because it's much easier to iterate over a command line and building them is a bit of a personal challenge since it's not worth writing a script or saving to a file.

I don't know about most people, but interactively I use short flags and when writing Bash scripts I try hard to use long flags and will often break things up specifically for clarity.

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by xupybd on Monday November 23, 2020 @02:35AM (#25184386)
Yeah the challenge is a lot of fun I have to admit that.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by lordgrenville on Monday November 23, 2020 @01:22AM (#25184081)
Only advantage I can think of is that you can just paste it in a shell prompt, if you're not comfortable with making a script, changing permissions, etc.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by c-jiph on Sunday November 22, 2020 @09:05PM (#25182908)
> (To the best of my knowledge, there is as of 2020 no purely pythonic way to write one-lined try-blocks.)

You can do this using contexlib. For example the following in pwk:

    try: { print(os.listdir("/"+s.strip())) } except: pass
Can be written in pure Python as:

    with contextlib.suppress(Exception): print(os.listdir("/"+s.strip()))
(Assuming contextlib is imported, and you're okay with letting things which don't extend Exception slip through.)

You could probably also make a context manager which takes a function/lambda to define how to handle exceptions etc.

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by uranusjr on Monday November 23, 2020 @12:40AM (#25183913)
`contextlib.suppress(BaseException)` is possible if you really want to (although both this and the bare except clause should be avoided anyway).
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by zmmmmm on Sunday November 22, 2020 @05:16PM (#25181641)
It's a shame Python doesn't natively support easy invocation from command line. It really shoots down a whole class of applications where bash is getting used but Python would be so much better.

I've ended up using Groovy for this level of scripting, and it works really well.

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by jeroenhd on Sunday November 22, 2020 @07:36PM (#25182431)
It doesn't? If I write this into a file (/tmp/test.py):

    #!/usr/bin/env python
    
    print("Hello, world!")
and mark it as executable (chmod +x /tmp/test.py), I can just execute it as /tmp/test.py like I would do with any bash file.

If you mean invocation as "execute a command that I'm passing", 'python -c' does exactly that.

Even as a complete shell it works including changing directories and using standard python to do file IO, though it won't execute any system executables and doesn't do piping like you'd like from a shell. It's not intended for that purpose so I can't really blame python for that.

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by black_puppydog on Sunday November 22, 2020 @08:44PM (#25182819)
> though it won't execute any system executables and doesn't do piping like you'd like from a shell

I think that's what xonsh was made for? :)

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by davea37 on Sunday November 22, 2020 @04:23PM (#25181236)
In a similar vein, pyp: https://github.com/hauntsaninja/pyp
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by BiteCode_dev on Monday November 23, 2020 @02:35AM (#25184385)
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by andybak on Sunday November 22, 2020 @01:43PM (#25179833)
> Python With Kurly braces

I wasn't aware of this. I guess it might silence a few critics.

However I'd be much more interested to try "Javascript with semantic white space"

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by bpicolo on Sunday November 22, 2020 @01:47PM (#25179862)
We tried that for a while, yeah? https://coffeescript.org/
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by mbirth on Sunday November 22, 2020 @03:38PM (#25180836)
And I loved it. So much more convenient than standard JavaScript.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by mistersys on Sunday November 22, 2020 @08:34PM (#25182762)
CoffeeScript code bases are the scariest code bases on the planet, the nightmares. To me CoffeeScript inherited all the inconsistencies of JavaScript, then made the syntax inconsistent as well. It least it had optional chaining.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by irjustin on Sunday November 22, 2020 @10:32PM (#25183332)
It's weird right? I come from Rails and thought CoffeeScript (CS) could make it way more readable, like Python but with cool string interpolations!

But you're absolutely right, it made my code really hard to reason about. I don't know why this is the case but it was always harder to read my own CS than raw JS. I found myself putting blocks into the CS2JS just to read inner workings.

While it made me less verbose and short-term faster, longer term it made me slower.

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by pkage on Monday November 23, 2020 @01:01AM (#25184003)
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by andybak on Sunday November 22, 2020 @01:52PM (#25179913)
Oh god. You're right. I'd blotted that out of my memory.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by bobbylarrybobby on Sunday November 22, 2020 @01:45PM (#25179844)
How would JS with semantic whitespace difference from just auto-indenting a JS file?
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by strombofulous on Sunday November 22, 2020 @02:02PM (#25180013)
Lamdas and first-class functions might make it pretty ugly, I definitely write `func(arg1, arg2, (res) => console.log(res))` from time to time
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by andybak on Sunday November 22, 2020 @01:52PM (#25179918)
It would look nicer.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by OJFord on Sunday November 22, 2020 @02:05PM (#25180041)
Semantic whitespace looks nicer than non-semantic whitespace?
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by andybak on Sunday November 22, 2020 @02:29PM (#25180262)
No curly braces looks nicer than curly braces.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by pindab0ter on Monday November 23, 2020 @01:51AM (#25184189)
And that’s just a fact.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by OJFord on Sunday November 22, 2020 @02:10PM (#25180091)
Not even a nod to __future__.braces?
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by neycmrtn on Sunday November 22, 2020 @02:26PM (#25180232)
Thx, didn't know that one! Added ref to github (and thx to you)! --Martin
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by felixhummel on Monday November 23, 2020 @09:17AM (#25186948)
For the uninitiated:

    $ python -c 'from __future__ import braces'
      File "<string>", line 1
    SyntaxError: not a chance
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by adzm on Sunday November 22, 2020 @02:23PM (#25180208)
Not a chance
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by azhenley on Sunday November 22, 2020 @05:08PM (#25181579)
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by klyrs on Sunday November 22, 2020 @05:03PM (#25181539)
I like python and I enjoy weirdo one-off languages, so I'm predisposed to like this. But why keep the colons? It would look nicer without.

...

I just attempted to type out an example and found a reason not to do this... it's the same reason c needs parentheses around the conditional. On the balance, I prefer parentheses over the vestigial colon.

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by corytheboyd on Sunday November 22, 2020 @02:11PM (#25180100)
Can someone help me understand the rationale behind semantic whitespace as a replacement for explicit delimiters? Is it purely aesthetic?
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by __MatrixMan__ on Sunday November 22, 2020 @05:41PM (#25181811)
Your friends are going to insist on properly indented code no matter what your compiler/interpreter wants, so rather than following one set of rules for your friends and a different set of rules for your compiler, why not teach the compiler to think like your friends so that you only have one set of rules to think about?

I'd call it pragmatic.

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by codetrotter on Sunday November 22, 2020 @02:25PM (#25180226)
It forces consistent formatting within a file.

But I think it was more relevant years ago. These days I see a lot of people using code formatters.

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by rich_sasha on Sunday November 22, 2020 @02:59PM (#25180523)
The flip side is, if you’re indenting you’re code anyway, as you probably should, then you might as well combine the style and meaning.

It works in practice, same difference to me tbh.

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by mtalantikite on Sunday November 22, 2020 @02:32PM (#25180287)
I still think one of Go’s best contributions was formatting with ‘go fmt’. It’s refreshing to see so many language communities just embrace a format on save standard and keep it moving.

Reading Python has always been a bit of a headache for me, but that probably has as much to do with the lack of type annotations as it does the lack of braces.

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by georgyo on Sunday November 22, 2020 @06:33PM (#25182096)
Auto formatters are the future, why should humans ever have to manually format or discuss how it should look ever again.

However, languages that have a line length limit are the blight of them. There isn't a python formatter that won't make everything mush. My if clause that is 81 chars wide doesn't need to be broken up. Similar, my strings don't need to be reflowed.

Go fmt ignores line length, and always produces a formatted file that is pretty as a result.

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by rjmorris on Sunday November 22, 2020 @07:38PM (#25182437)
You can change the max line length in black (the Python formatter I hear mentioned most often). How would you feel about setting it to something really large, like 1000, to emulate having no line length limit?
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by georgyo on Sunday November 22, 2020 @07:58PM (#25182546)
That doesn't work still. It will reflow all your lines to reflow to 1000. Try it out, anything above 88 makes even the cleanest python a nightmare.

I don't like long lines, but reflowing logic is way to aggressive in these formatters.

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by corytheboyd on Sunday November 22, 2020 @02:30PM (#25180268)
Yeah that makes a bit more sense, thinking back to arguments about aligning hash key/value pairs in code reviews and how that just never seems to come up anymore (thank god)
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by throwaway894345 on Sunday November 22, 2020 @03:45PM (#25180901)
That’s a pretty bizarre rationale given that other languages didn’t need semantic whitespace (or a formatter) to maintain consistency, and the consistency was even achievable for larger code units than individual files. Formatters are definitely the way to go, but baking it into the syntax was never necessary or even helpful IMO.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by mhh__ on Sunday November 22, 2020 @09:59PM (#25183195)
It forces you to write "good" code.

I think it's Python's only real strength. The language is ugly, the interpreter is a genuinely deficient way of making software ("Why use a compiler when you can just write thousands of unit tests to check for syntax errors!?"), and the mechanisms for abstraction are fairly weak - even after all that, n00b Python code is fairly readable.

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by aitchnyu on Monday November 23, 2020 @05:30AM (#25185331)
Nowadays tooling will enforce this "goodness" for most languages. Whitespace blocks were a pain when people mixed tabs and spaces.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by markrages on Sunday November 22, 2020 @10:50PM (#25183398)
It's easier to follow a program's execution when you can be sure the whitespace will never lie to you.

You can never have a "goto fail" kind of bug.

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by kevin_thibedeau on Sunday November 22, 2020 @11:47PM (#25183680)
In Py3. Mixed whitespace that Py2 accepts can be very much misleading.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by gnufx on Sunday November 22, 2020 @05:15PM (#25181623)
If you haven't read Landin's "The Next 700 Programming Languages", you should. I don't know if it's still outside the ACM paywall, but you'll find it in a web search if not. It introduced the offside rule -- not actually what Python has -- inspired by mathematical notation. Note "it can be mixed freely with [...] punctuation" (e.g. Haskell). The paper talks about "physical representations", relevant to syntax highlighting mentioned below (or "fontification" in Emacs, reminiscent of typefaces in the publication language of ALGOL).
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by okareaman on Monday November 23, 2020 @01:12AM (#25184045)
I just finished reading it the other day: https://www.cs.cmu.edu/~crary/819-f09/Landin66.pdf I got there from Erik Meijer's excellent "Confessions of a Used Programming Language Salesman: Getting the Masses Hooked on Haskell": http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.72....
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by xapata on Sunday November 22, 2020 @02:30PM (#25180270)
Suppose you didn't have syntax highlighting. Would the braces be more distracting then?
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by corytheboyd on Sunday November 22, 2020 @02:34PM (#25180307)
> Suppose you didn't have syntax highlighting.

That might be a bit TOO contrived, at least for me. I could see this coming up for someone working with jurassic hardware not under their control though.

Anyway, to play along. I think curly braces help tell what is happening in long source files with deep nesting. But also you could just render whitespace characters as something visible to count them to judge indentation at a glance to get your bearings. Of course the real answer here is don’t write long source files with deep nesting, but unfortunately other people are allowed to write code :p

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by xapata on Monday November 23, 2020 @12:10AM (#25183778)
> jurassic hardware

Like the late 1980s, when Guido was creating Python?

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by reificator on Sunday November 22, 2020 @02:38PM (#25180341)
No they'd be even more useful.

I say that from experience: when I was first learning to program I found C much easier than python for this exact reason, and I was not using syntax highlighting.

That might be because I was much more prone to nesting back then. These days I get annoyed when I have two levels of indentation inside a function and treat three as a reminder to refactor. But when I started programming I'd build pyramids 8-10 levels deep and argued with people who tried to help simplify it.

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by ledauphin on Sunday November 22, 2020 @09:40PM (#25183102)
this is interesting, both because I've had a somewhat similar experience (my code is now much less indented than it was 10 years ago), and also because it does seem to hold some explanatory power for why some people seem to react so violently to Python's semantic whitespace.

I hear the argument quite frequently that semantic whitespace makes it hard to move code around (or that it causes code to get messed up in various ways) and I keep wondering why that doesn't describe my own experience as a daily user of Python. Maybe a lot of it is that I tend to avoid more than a couple of levels of indentation like the plague, and therefore it's rare for there to be any confusion about where a particular line of code would "belong".

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by smaudet on Monday November 23, 2020 @01:53AM (#25184198)
Maybe they are using some arcane programming tool from the 80s...

'{}' has the advantage that it expresses structure in a single line - copy a block of code from one place to another and your editor can throw it almost anywhere and the structure is preserved...

Modern day text editors all preserve indent wonderfully and can indent/outdent as a matter of course...but to some troll insisting on using ed or notepad or Microsoft Word I can see the difficulty.

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by anamexis on Sunday November 22, 2020 @02:36PM (#25180324)
No, I think braces are even more useful when you don't have syntax highlighting.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by xapata on Monday November 23, 2020 @12:12AM (#25183792)
Interesting. I find them distracting. I thought the cruft might be more distracting when it doesn't fade into the background via syntax highlighting.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by shenberg on Sunday November 22, 2020 @03:32PM (#25180787)
been done a while ago by forking Python (2.7.6)and modifying the grammar: http://python-with-braces.appspot.com/
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by cleandreams on Sunday November 22, 2020 @01:57PM (#25179967)
This meaningful whitespace thing in python is dreck. Hate it. I am not a python hater; this is from experience on projects (though this is now less of a problem because of IDE help). I like python and I choose it for my personal projects. I will probably use this.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by alsko on Sunday November 22, 2020 @02:09PM (#25180081)
> Just download and chmod it

No source? No thanks.

Edit: Discard this comment! I thought pwk in the repo was an executable because it did not have a ".py" ending and did not care to check. Sorry!

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by OJFord on Sunday November 22, 2020 @02:12PM (#25180111)
What? It's a python script, 'it' there is the source. It's executable via the python interpreter given in its shebang line.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by neycmrtn on Sunday November 22, 2020 @02:28PM (#25180254)
"pwk" is both code and "executable" -- you can open it with any text editor. I didn't want to name it "pwk.py" to save on typing on the command line. --m
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by alsko on Sunday November 22, 2020 @03:19PM (#25180676)
Wow, I feel _really_ dumb now - I just saw the filename without ".py" and assumed it was an executable

Sorry, I take it back!

Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by OJFord on Sunday November 22, 2020 @03:35PM (#25180815)
It's sort of just pedantry because I know what you mean, but FYI it is an executable when you mark it (chmod +x) as such; what you mean to say is that you assumed it was a binary, the output of a compiler, with the input (source) not provided.
Re:Show HN: Making Python one-liner-fit with the new pwk (Py... by pvg on Sunday November 22, 2020 @02:34PM (#25180309)
You don't need to sign your messages on HN (not really obvious since it's not written anywhere but it's one of those HN things)