{"id":9674,"date":"2022-09-28T21:59:00","date_gmt":"2022-09-28T19:59:00","guid":{"rendered":"https:\/\/monodes.com\/predaelli\/?p=9674"},"modified":"2022-09-28T15:00:00","modified_gmt":"2022-09-28T13:00:00","slug":"10-powerful-python-one-liners","status":"publish","type":"post","link":"https:\/\/monodes.com\/predaelli\/2022\/09\/28\/10-powerful-python-one-liners\/","title":{"rendered":"10 Powerful Python One-Liners."},"content":{"rendered":"<p>I shall Eiffellize those, one day or another: <em><a href=\"https:\/\/python.plainenglish.io\/10-powerful-python-one-liners-d8c9b86c3cf0\">10 Powerful Python One-Liners. Python one-liners can be just as\u2026 | by Ishaan Gupta | Sep, 2022 | Python in Plain English<\/a><\/em><\/p>\n<p><!--more--><!--nextpage--><\/p>\n<blockquote>\n<div class=\"il im in io ip\">\n<div class=\"\">\n<h1 id=\"5563\" class=\"pw-post-title iq ir is bm it iu iv iw ix iy iz ja jb jc jd je jf jg jh ji jj jk jl jm jn jo fu\" data-selectable-paragraph=\"\">10 Powerful Python One-Liners<\/h1>\n<\/div>\n<div class=\"\">\n<h2 id=\"a864\" class=\"pw-subtitle-paragraph jp ir is bm b jq jr js jt ju jv jw jx jy jz ka kb kc kd ke kf kg cn\" data-selectable-paragraph=\"\"><em class=\"kh\">Python one-liners can be just as powerful as a long and tedious program written in another language designed to do the same thing.<\/em><\/h2>\n<\/div>\n<figure class=\"kj kk kl km gr kn gf gg paragraph-image\">\n<div class=\"gf gg ki\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"ce ko kp c\" role=\"presentation\" src=\"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2022\/09\/1jLSxNQvNqsYr02VB_wr96A.jpeg?resize=640%2C480&#038;ssl=1\" sizes=\"auto, (min-resolution: 4dppx) and (max-width: 700px) 50vw, (-webkit-min-device-pixel-ratio: 4) and (max-width: 700px) 50vw, (min-resolution: 3dppx) and (max-width: 700px) 67vw, (-webkit-min-device-pixel-ratio: 3) and (max-width: 700px) 65vw, (min-resolution: 2.5dppx) and (max-width: 700px) 80vw, (-webkit-min-device-pixel-ratio: 2.5) and (max-width: 700px) 80vw, (min-resolution: 2dppx) and (max-width: 700px) 100vw, (-webkit-min-device-pixel-ratio: 2) and (max-width: 700px) 100vw, 640px\" srcset=\"https:\/\/miro.medium.com\/max\/640\/1*jLSxNQvNqsYr02VB_wr96A.jpeg 640w, https:\/\/miro.medium.com\/max\/720\/1*jLSxNQvNqsYr02VB_wr96A.jpeg 720w, https:\/\/miro.medium.com\/max\/750\/1*jLSxNQvNqsYr02VB_wr96A.jpeg 750w, https:\/\/miro.medium.com\/max\/786\/1*jLSxNQvNqsYr02VB_wr96A.jpeg 786w, https:\/\/miro.medium.com\/max\/828\/1*jLSxNQvNqsYr02VB_wr96A.jpeg 828w, https:\/\/miro.medium.com\/max\/1100\/1*jLSxNQvNqsYr02VB_wr96A.jpeg 1100w, https:\/\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2022\/09\/1jLSxNQvNqsYr02VB_wr96A.jpeg 1280w\" alt=\"\" width=\"640\" height=\"480\"\/><\/div><figcaption class=\"kq bl gh gf gg kr ks bm b bn bo cn\" data-selectable-paragraph=\"\">Photo by <a class=\"au kt\" href=\"https:\/\/unsplash.com\/@artturijalli?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" target=\"_blank\" rel=\"noopener ugc nofollow\">Artturi Jalli<\/a> on <a class=\"au kt\" href=\"https:\/\/unsplash.com\/s\/photos\/python?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText\" target=\"_blank\" rel=\"noopener ugc nofollow\">Unsplash<\/a><\/figcaption><\/figure>\n<h2 id=\"b744\" class=\"ku kv is bm kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr fu\" data-selectable-paragraph=\"\">1) Share your Wi-Fi password By Printing it as QR Code<\/h2>\n<pre class=\"kj kk kl km gr ls bs lt\"><span id=\"793d\" class=\"fu ku kv is lu b dm lv lw l lx\" data-selectable-paragraph=\"\">import wifi_qrcode_generator as qr\nqr.wifi_qrcode('wifi name ', False, 'WPA', 'password')<\/span><\/pre>\n<h2 id=\"4d7e\" class=\"ku kv is bm kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr fu\" data-selectable-paragraph=\"\">2) CSV to JSON<\/h2>\n<pre class=\"kj kk kl km gr ls bs lt\"><span id=\"8ee4\" class=\"fu ku kv is lu b dm lv lw l lx\" data-selectable-paragraph=\"\">import csv,json\nprint (json.dumps(list(csv.reader(open('csv_file.csv')))))<\/span><\/pre>\n<h2 id=\"5992\" class=\"ku kv is bm kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr fu\" data-selectable-paragraph=\"\">3) Apply regular expression to lines from stdin<\/h2>\n<pre class=\"kj kk kl km gr ls bs lt\"><span id=\"d534\" class=\"fu ku kv is lu b dm lv lw l lx\" data-selectable-paragraph=\"\">import sys,re\n[sys.stdout.write(re.sub('PATTERN', 'SUBSTITUTION', line)) for line in sys.stdin]<\/span><\/pre>\n<h2 id=\"ce8a\" class=\"ku kv is bm kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr fu\" data-selectable-paragraph=\"\">4) Profile a Python script<\/h2>\n<pre class=\"kj kk kl km gr ls bs lt\"><span id=\"da96\" class=\"fu ku kv is lu b dm lv lw l lx\" data-selectable-paragraph=\"\">python3 -m cProfile foo.py<\/span><\/pre>\n<h2 id=\"2cb4\" class=\"ku kv is bm kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr fu\" data-selectable-paragraph=\"\">5) Start a Webserver on your current directory<\/h2>\n<pre class=\"kj kk kl km gr ls bs lt\"><span id=\"c183\" class=\"fu ku kv is lu b dm lv lw l lx\" data-selectable-paragraph=\"\">python -m SimpleHTTPServer 8000<\/span><\/pre>\n<h2 id=\"f9cc\" class=\"ku kv is bm kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr fu\" data-selectable-paragraph=\"\">6) Finding all subsets of a set in one line<\/h2>\n<pre class=\"kj kk kl km gr ls bs lt\"><span id=\"7724\" class=\"fu ku kv is lu b dm lv lw l lx\" data-selectable-paragraph=\"\"><strong class=\"lu it\">from<\/strong> itertools <strong class=\"lu it\">import<\/strong> combinations\nprint(list(combinations([1, 2, 3, 4], 2)))<\/span><\/pre>\n<h2 id=\"19e5\" class=\"ku kv is bm kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr fu\" data-selectable-paragraph=\"\">7) Decode a base64 encoded file<\/h2>\n<pre class=\"kj kk kl km gr ls bs lt\"><span id=\"7e8a\" class=\"fu ku kv is lu b dm lv lw l lx\" data-selectable-paragraph=\"\">import base64, sys \nbase64.decode(open(sys.argv[1], \"rb\"), open(sys.argv[2], \"wb\"))<\/span><\/pre>\n<h2 id=\"aa0e\" class=\"ku kv is bm kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr fu\" data-selectable-paragraph=\"\">8) Display List of all users on Unix-like systems<\/h2>\n<pre class=\"kj kk kl km gr ls bs lt\"><span id=\"a062\" class=\"fu ku kv is lu b dm lv lw l lx\" data-selectable-paragraph=\"\">print '\\n'.join(line.split(\":\",1)[0] for line in open(\"\/etc\/passwd\"))<\/span><\/pre>\n<h2 id=\"dc52\" class=\"ku kv is bm kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr fu\" data-selectable-paragraph=\"\">9) Largest 8-Bytes Number<\/h2>\n<pre class=\"kj kk kl km gr ls bs lt\"><span id=\"02b8\" class=\"fu ku kv is lu b dm lv lw l lx\" data-selectable-paragraph=\"\">print '\\n'.join(\"%i Byte = %i Bit = largest number: %i\" % (j, j*8, 256**j-1) for j in (1 &lt;&lt; i for i in range(8)))<\/span><\/pre>\n<h2 id=\"99da\" class=\"ku kv is bm kw kx ky kz la lb lc ld le lf lg lh li lj lk ll lm ln lo lp lq lr fu\" data-selectable-paragraph=\"\">10) Retrieve content text from HTTP data<\/h2>\n<pre class=\"kj kk kl km gr ls bs lt\"><span id=\"f0fc\" class=\"fu ku kv is lu b dm lv lw l lx\" data-selectable-paragraph=\"\">import sys \nprint sys.stdin.read().replace('\\r','').split('\\n\\n',2)[1]<\/span><\/pre>\n<\/div>\n<div class=\"o dx ly lz hx ma\" role=\"separator\"><\/div>\n<div class=\"il im in io ip\">\n<h1 id=\"ecca\" class=\"mf kv is bm kw mg mh mi la mj mk ml le jy mm jz li kb mn kc lm ke mo kf lq mp fu\" data-selectable-paragraph=\"\">Final Thoughts<\/h1>\n<p id=\"2d3c\" class=\"pw-post-body-paragraph mq mr is ms b mt mu jt mv mw mx jw my lf mz na nb lj nc nd ne ln nf ng nh ni il fu\" data-selectable-paragraph=\"\">Well, here are my <em class=\"nj\">10 Powerful Python One-Liners<\/em> that every developer must-have. I hope you find this article helpful and learned some new things. Share this article with your developer friends or who are just starting development.<\/p>\n<h1 id=\"eea8\" class=\"mf kv is bm kw mg nk mi la mj nl ml le jy nm jz li kb nn kc lm ke no kf lq mp fu\" data-selectable-paragraph=\"\">Have a good day\u2026<\/h1>\n<\/div>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">I shall Eiffellize those, one day or another: 10 Powerful Python One-Liners. Python one-liners can be just as\u2026 | by Ishaan Gupta | Sep, 2022 | Python in Plain English<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"https:\/\/monodes.com\/predaelli\/2022\/09\/28\/10-powerful-python-one-liners\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[238,98,113,278],"tags":[],"class_list":["post-9674","post","type-post","status-publish","format-standard","hentry","category-agenda","category-liberty-eiffel","category-python","category-tricks"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6daft-2w2","jetpack-related-posts":[{"id":8194,"url":"https:\/\/monodes.com\/predaelli\/2021\/02\/24\/python-packages-and-one-liners\/","url_meta":{"origin":9674,"position":0},"title":"Python Packages and one-liners","author":"Paolo Redaelli","date":"2021-02-24","format":false,"excerpt":"That Makes You Life Easier 20 Python Packages That You Must Try | by Abhay Parashar | Level Up Coding 25 Useful Python One-Liners That You Should know","rel":"","context":"In &quot;Documentations&quot;","block_context":{"text":"Documentations","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9532,"url":"https:\/\/monodes.com\/predaelli\/2022\/08\/09\/7-python-one-liners-that-will-blow-your-mind\/","url_meta":{"origin":9674,"position":1},"title":"7 Python One-Liners that will Blow Your Mind","author":"Paolo Redaelli","date":"2022-08-09","format":false,"excerpt":"Xiaoxu Gao wrote 7 Python One-Liners that will Blow Your Mind Less is more? Photo by Photos by Lanty from Unsplash The term one-liner comes from comedy where a joke is delivered in a single line. A good one-liner is said to be meaningful and concise. This concept also exists\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"https:\/\/monodes.com\/predaelli\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9373,"url":"https:\/\/monodes.com\/predaelli\/2022\/05\/12\/one-liner-static-web-server\/","url_meta":{"origin":9674,"position":2},"title":"One liner, static web-server","author":"Paolo Redaelli","date":"2022-05-12","format":false,"excerpt":"willurd on web-servers.md show quite a few one-liner (or mostly) commands that will run an ad hoc http static server in your current (or specified) directory, available at http:\/\/localhost:8000. I think I will use only the Python 3.x version which is as simple as python -m http.server 8000","rel":"","context":"In &quot;Tricks&quot;","block_context":{"text":"Tricks","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/tricks\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":11033,"url":"https:\/\/monodes.com\/predaelli\/2023\/12\/17\/textual\/","url_meta":{"origin":9674,"position":3},"title":"Textual","author":"Paolo Redaelli","date":"2023-12-17","format":false,"excerpt":"Textual is a TUI framework for Python, inspired by modern web development. Textual is a Rapid Application Development framework for Python, built by Textualize.io. Build sophisticated user interfaces with a simple Python API. Run your apps in the terminal or a web browser! Well, I just wish I could have\u2026","rel":"","context":"In &quot;Eiffel&quot;","block_context":{"text":"Eiffel","link":"https:\/\/monodes.com\/predaelli\/category\/eiffel\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":6318,"url":"https:\/\/monodes.com\/predaelli\/2019\/12\/15\/eiffels-conditional-expressions\/","url_meta":{"origin":9674,"position":4},"title":"Eiffel&#8217;s conditional expressions","author":"Paolo Redaelli","date":"2019-12-15","format":false,"excerpt":"The ternary conditional operator is a short-hand method for writing an if\/else statement. Does Python Have a Ternary Conditional Operator? Yes, it does. This remids me that Eiffel, at least \"ISE Eiffel\" does actually have conditional expressions that are the same: answer := if time < noon then \"Good morning\"\u2026","rel":"","context":"In &quot;Eiffel&quot;","block_context":{"text":"Eiffel","link":"https:\/\/monodes.com\/predaelli\/category\/eiffel\/"},"img":{"alt_text":"Jonathan Hsu","src":"https:\/\/i0.wp.com\/miro.medium.com\/fit\/c\/58\/58\/2%2A_KGzadiy9s83D4vzhsCyyg.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":9725,"url":"https:\/\/monodes.com\/predaelli\/2022\/10\/15\/python-is-inefficient\/","url_meta":{"origin":9674,"position":5},"title":"Python is inefficient","author":"Paolo Redaelli","date":"2022-10-15","format":false,"excerpt":"The most significant damage caused by Python Python is Destroying the Planet.writes Mohammed Ayar of course, I shall say as it is much less efficient, i.e. it's a lot slower than C. It's quite simple, and it's clearly showed by The\u00a0Computer Language Benchmarks Game, once known as \"The great language\u2026","rel":"","context":"In &quot;Senza categoria&quot;","block_context":{"text":"Senza categoria","link":"https:\/\/monodes.com\/predaelli\/category\/senza-categoria\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/9674","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/comments?post=9674"}],"version-history":[{"count":0,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/9674\/revisions"}],"wp:attachment":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/media?parent=9674"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/categories?post=9674"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/tags?post=9674"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}