TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Making Python one-liner-fit with the new pwk (Python With Kurly braces)

84 pointsby neycmrtnover 4 years ago

14 comments

gabrielsrokaover 4 years ago
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&#x27;re easier to read). I&#x27;m not sure I see the advantange of curly braces.<p><pre><code> # --- Example 1 # pwk pwk &#x27;if &quot;braces&quot;==&quot;bad&quot;: { print(&quot;Be gone!&quot;); exit(99) } else: { print(&quot;Howdy!&quot;) }&#x27; # vs # python python -c &#x27;if &quot;braces&quot;==&quot;bad&quot;: print(&quot;Be gone!&quot;); exit(99) else: print(&quot;Howdy!&quot;)&#x27; # --- Example 2 pwk &#x27;def s2i(s): { return int(s) } print(s2i(&quot;41&quot;)+1)&#x27; python -c &#x27;def s2i(s): return int(s) print(s2i(&quot;41&quot;)+1)&#x27; # --- Example 3 ls &#x2F; | pwk &#x27;for s in sys.stdin: try: { print(os.listdir(&quot;&#x2F;&quot;+s.strip())) } except: pass&#x27; ls &#x2F; | python -c &#x27;import sys, os for s in sys.stdin: try: print(os.listdir(&quot;&#x2F;&quot;+s.strip())) except: pass&#x27;</code></pre>
评论 #25183394 未加载
评论 #25185174 未加载
cutlerover 4 years ago
Face it - Python just ain&#x27;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&#x27;s deficiencies bleed through when you try to compose something elegant and concise.
评论 #25184992 未加载
xupybdover 4 years ago
I really don&#x27;t understand the love of one liners. Yes it&#x27;s cool to see how much can be done in one line but it&#x27;s so much harder to read. Isn&#x27;t legibility far better than compactness?
评论 #25185068 未加载
评论 #25184013 未加载
评论 #25184081 未加载
c-jiphover 4 years ago
&gt; (To the best of my knowledge, there is as of 2020 no purely pythonic way to write one-lined try-blocks.)<p>You can do this using contexlib. For example the following in pwk:<p><pre><code> try: { print(os.listdir(&quot;&#x2F;&quot;+s.strip())) } except: pass </code></pre> Can be written in pure Python as:<p><pre><code> with contextlib.suppress(Exception): print(os.listdir(&quot;&#x2F;&quot;+s.strip())) </code></pre> (Assuming contextlib is imported, and you&#x27;re okay with letting things which don&#x27;t extend Exception slip through.)<p>You could probably also make a context manager which takes a function&#x2F;lambda to define how to handle exceptions etc.
评论 #25183913 未加载
zmmmmmover 4 years ago
It&#x27;s a shame Python doesn&#x27;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.<p>I&#x27;ve ended up using Groovy for this level of scripting, and it works really well.
评论 #25182431 未加载
评论 #25182483 未加载
davea37over 4 years ago
In a similar vein, pyp: <a href="https:&#x2F;&#x2F;github.com&#x2F;hauntsaninja&#x2F;pyp" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;hauntsaninja&#x2F;pyp</a>
评论 #25184385 未加载
andybakover 4 years ago
&gt; Python With Kurly braces<p>I wasn&#x27;t aware of this. I guess it might silence a few critics.<p>However I&#x27;d be much more interested to try &quot;Javascript with semantic white space&quot;
评论 #25179862 未加载
评论 #25179844 未加载
OJFordover 4 years ago
Not even a nod to __future__.braces?
评论 #25180232 未加载
评论 #25186948 未加载
评论 #25181407 未加载
评论 #25180208 未加载
azhenleyover 4 years ago
There is also Bython: <a href="https:&#x2F;&#x2F;github.com&#x2F;mathialo&#x2F;bython" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mathialo&#x2F;bython</a>
klyrsover 4 years ago
I like python and I enjoy weirdo one-off languages, so I&#x27;m predisposed to like this. But why keep the colons? It would look nicer without.<p>...<p>I just attempted to type out an example and found a reason not to do this... it&#x27;s the same reason c needs parentheses around the conditional. On the balance, I prefer parentheses over the vestigial colon.
corytheboydover 4 years ago
Can someone help me understand the rationale behind semantic whitespace as a replacement for explicit delimiters? Is it purely aesthetic?
评论 #25181811 未加载
评论 #25180226 未加载
评论 #25180877 未加载
评论 #25183195 未加载
评论 #25183398 未加载
评论 #25181623 未加载
评论 #25180270 未加载
shenbergover 4 years ago
been done a while ago by forking Python (2.7.6)and modifying the grammar: <a href="http:&#x2F;&#x2F;python-with-braces.appspot.com&#x2F;" rel="nofollow">http:&#x2F;&#x2F;python-with-braces.appspot.com&#x2F;</a>
cleandreamsover 4 years ago
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.
alskoover 4 years ago
&gt; Just download and chmod it<p>No source? No thanks.<p>Edit: Discard this comment! I thought pwk in the repo was an executable because it did not have a &quot;.py&quot; ending and did not care to check. Sorry!
评论 #25180111 未加载
评论 #25180254 未加载