Showing posts with label one-liners. Show all posts
Showing posts with label one-liners. Show all posts

Saturday, December 29, 2018

The Zen of Python is well sed :)





- By Vasudev Ram - Online Python training / SQL training / Linux training

$ python -c "import this" | sed -n "4,4p;15,16p"
Explicit is better than implicit.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.


- Vasudev Ram - Online Python training and consulting

I conduct online courses on Python programming, Unix / Linux commands and shell scripting and SQL programming and database design, with course material and personal coaching sessions.

The course details and testimonials are here.

Contact me for details of course content, terms and schedule.

Or if you're a self-starter, check out my Python programming course by email.

Try FreshBooks: Create and send professional looking invoices in less than 30 seconds.

Learning Linux? Hit the ground running with my vi quickstart tutorial.

Sell your digital products via DPD: Digital Publishing for Ebooks and Downloads.

Posts about: Python * DLang * xtopdf

My ActiveState Code recipes

Follow me on:


Friday, April 14, 2017

Quick CMD one-liner like "which python" command - and more

By Vasudev Ram


Today, while browsing StackOverflow, I saw this question:

Is there an equivalent of 'which' on the Windows command line?

And I liked the second answer.

The first part of that answer had this solution:
c:\> for %i in (cmd.exe) do @echo.   %~$PATH:i
   C:\WINDOWS\system32\cmd.exe

c:\> for %i in (python.exe) do @echo.   %~$PATH:i
   C:\Python25\python.exe
Then I modified the one-liner above to add a few more commands to search for, and ran it:
$ for %i in (cmd.exe python.exe metapad.exe file_sizes.exe tp.exe alarm_clock.py
) do @echo.   %~$PATH:i
   C:\Windows\System32\cmd.exe
   D:\Anaconda2-4.2.0-32bit\python.exe
   C:\util\metapad.exe
   C:\util\file_sizes.exe
   C:\util\tp.exe
   C:\util\alarm_clock.py
Notice that I also included a .py file in the list of commands to search for, and it worked for that too. This must be because .py files are registered as executable files (i.e. executable by the Python interpreter) at the time of installing Python on Windows.

Of course, as a user comments in that SO post, this is not exactly the same as the which command, since you have to specify the .exe extension (for the commands you search for), and there are other extensions for executable files, such as .bat and others. But since the Python interpeter is normally named python.exe, this can be used as a quick-and-dirty way to find out which Python executable is going to be run when you type "python", if you have more than one of them installed on your system.

I had also written a simple Python tool roughly like the Unix which command, earlier:
A simple UNIX-like "which" command in Python

- Vasudev Ram - Online Python training and consulting

Get updates (via Gumroad) on my forthcoming apps and content.

Jump to posts: Python * DLang * xtopdf

Subscribe to my blog by email

My ActiveState Code recipes

Follow me on: LinkedIn * Twitter

Are you a blogger with some traffic? Get Convertkit:

Email marketing for professional bloggers



Friday, February 3, 2017

Time to Upgrade Your Python: TLS v1.2 Will Soon Be Mandatory (pyfound.blogspot.com)

By Vasudev Ram

Saw this blog post recently via an email from the PSF (Python Software Foundation):

Time to Upgrade Your Python: TLS v1.2 Will Soon Be Mandatory

and then also saw this HN thread (about the same post):

Time to Upgrade Your Python: TLS v1.2 Will Soon Be Mandatory (pyfound.blogspot.com)

The currently top comment in that thread, (by HN user jwilk), had examples of how to do the check for your Python's TLS version - for both Py 2 and Py 3, without using the 3rd-party requests library (which was used in the PSF post), just using Python's urllib* libraries:

From jwilk's comment:

Test procedure that doesn't require 3rd-party libs:
* For Python 2:
$ python -c "import json, urllib2; print json.load(urllib2.urlopen('https://www.howsmyssl.com/a/check'))['tls_version']"
* For Python 3:
$ python3 -c "import json, urllib.request; print(json.loads(urllib.request.urlopen('https://www.howsmyssl.com/a/check').read().decode('UTF-8'))['tls_version'])"

Tried them both out on my machine, they worked and showed the TLS version.

Speaking of one-liners, here are a few of my own, some by others, and in both Python and Unix:

Python one-liner to get the filename and line number of the caller of the current function

Python one-liner to compare two files (conditions apply)

Python one-liner to open a web site from the command line

And you can always get all the Python one-liners on my blog, both past and future [1], with this URL:

https://jugad2.blogspot.in/search/label/Python-one-liners

[1] Future one-liners, after they are written, not now :)

And the same for general one-liners (could include Python, Unix or other):

https://jugad2.blogspot.in/search/label/one-liners

- Vasudev Ram - Online Python training and consulting

Get updates (via Gumroad) on my forthcoming apps and content.

Jump to posts: Python * DLang * xtopdf

Subscribe to my blog by email

My ActiveState Code recipes

Follow me on: LinkedIn * Twitter

Managed WordPress Hosting by FlyWheel



Tuesday, March 29, 2016

Python one-liner to compare two files (conditions apply)

By Vasudev Ram

In my previous post a couple of days back:

A basic file compare utility in Python

I said that it was possible to write a shorter version of this program, subject to certain limitations.

You can do it with a one-liner. The limitation is that the sum of the sizes of both files being compared should be less than your computer's free memory [1]. This because I read both files fully into memory [2] to compare them [3].

First, the input files, 3 of the same ones as in the previous post I linked to above.
$ type f0.txt
file 1

$ type f1.txt
file 1

$ type f2.txt
file 2
And here is the Python one-liner, run on a pair of identical files and then on a pair of differing files:
$  python -c "print open('f0.txt', 'rb').read() == open('f1.txt', 'rb').read()"
True

$  python -c "print open('f0.txt', 'rb').read() == open('f2.txt', 'rb').read()"
False
Voila!

Note that you have to use the file opening mode of 'rb' (for 'read' and 'binary'), not just 'r', because otherwise Python will do newline translation and your result can be wrong, at least on Windows. I actually had this happen and then corrected the script.

[1] The free memory is what is left after subtracting from your total RAM, the space taken by the OS, buffer cache, other running apps and their data, the Python interpreter, and your script. So if you have 4 GB RAM, and the sum of the megabytes used by those items is 2.4 MB, you have 1.6 MB free, so the total size of files you can compare, if they are of equal size, is two files of 0.8 MB each. [4]

[2] Perl people call this technique slurping a file, and use it a lot. when feasible.

[3] Of course, with this technique we lose the extra info that the original program (file_compare.py) gives us, such as why the input files differ (e.g. in size or content).

[4] Not being 100% precise here, because Python data structures have some overhead. See the sys.getsizeof() function.

If you like one-liners, here are some more, some by me, some by others:

UNIX one-liner to kill a hanging Firefox process
(This one has some interesting comments on Unix processes.)

Python one-liner to open a web site from the command line

A first Python one-liner

Multiple Python one-liners

Python one-liner to get the filename and line number of the caller of the current function

- Vasudev Ram - Online Python training and programming

Signup to hear about new products and services I create.

Posts about Python  Posts about xtopdf

My ActiveState recipes

Wednesday, November 26, 2014

A first Python one-liner

By Vasudev Ram

1. Install Python 2.7 (if you don't have it already).

2. Run this at the command line:

python -c "print ''.join(list(reversed('!dlrow olleH')))"

- Vasudev Ram - Dancing Bison Enterprises

Contact Page

Friday, November 1, 2013

Python one-liner to open a web site from the command line


By Vasudev Ram



While importing the antigravity module, I came up with this Python one-liner to open a web site from the command line:

import sys, os.path, webbrowser; webbrowser.open("http://" + os.path.splitext(os.path.basename(sys.argv[0]))[0])

If you save the above Python code as, say, a.site.com.py, you can run it from the command line with:
C:> a.site.com.py

where a.site.com could be any existing site, like ibm.com, oreilly.com, python.org, mit.edu, etc. (You have to add the extension .py to the site name to create the file name.)

This should open that web site in your default web browser.

To do the same for other sites, just copy, for example, ibm.com.py to python.org.py .

The content of the file does not need to be edited for different sites, since there is no hard-coding of the site name in the code, only in the file name.

Though I haven't tested this on UNIX or Linux yet, you should be able to make a link from the first site filename to any number of others (one at a time), and achieve the same result as the copy on Windows. The command would be of the form:

$ ln ibm.com.py python.org.py

I also haven't tried the one-liner on UNIX or Linux (yet), but it is likely to work, since the webbrowser module of Python that it uses, is supposed to work there too. But check.

If you like one-liners, you may also find this one (which I wrote a while ago) interesting:

UNIX one-liner to kill a hanging Firefox process

(The comments on that one are also interesting and go into some detail on UNIX processes.)

- Vasudev Ram - Dancing Bison Enterprises

Consulting / training inquiry




DiscountMags.com


Saturday, July 28, 2012

Python REPL one-liner (Read-Eval-Print-Loop) by Raymond Hettinger and Cameron Laird

By Vasudev Ram


(Reformatted the stuff below a bit for clarity of the rendered HTML - no material changes made.)

@raymondh (Raymond Hettinger) tweeted:

Fun #python one-liner:
while True: request=raw_input('! '); result=eval(request); print repr(result)
# Read–eval–print_loop (REPL)

@Phaseit (Cameron Laird) replied:

@raymondh #python Why not compose all the evaluations?

I guess Cameron meant this:

while True: print repr(eval(raw_input('! ')))

Nice improvement - makes it a bit more functional) and shorter, without reducing clarity much.

To actually run it, the full command is:

python -c "while True: print repr(eval(raw_input('! ')))"

- Vasudev Ram - Dancing Bison Enterprises

Sunday, July 15, 2012

sed and awk one-liners - two good pages

By Vasudev Ram


I saw these sed and awk one-liner pages via this post on Rajiv Eranki's blog (he works at Dropbox):

Scaling lessons learned at Dropbox, part 1

(The post is about scaling at Dropbox and is interesting in itself.)

The sed and awk one-liner pages:

sed one-liners page

awk one-liners page

For a one-liner that happens to use both sed and awk, check this older post of mine:

UNIX one-liner to kill a hanging Firefox process

The comments on it relating to UNIX processes may be of interest (orphan processes, etc.)



- Vasudev Ram - Dancing Bison Enterprises