Friday, February 17, 2017

import this as comic # THE ZEN OF PYTHON ILLUSTRATED

By Vasudev Ram

I'm blogging this as a special case. That's the only way to do it ... [1]

Saw this via Twitter. Nice one:

THE ZEN OF PYTHON ILLUSTRATED

[1] :)

- 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



Saturday, February 11, 2017

tp, a simple text pager in Python

By Vasudev Ram

Yesterday I got this idea of writing a simple text file pager in Python.

Here it is, in file tp.py:
'''
tp.py
Purpose: A simple text pager.
Version: 0.1
Platform: Windows-only.
Can be adapted for Unix using tty / termios calls.
Only the use of msvcrt.getch() needs to be changed.
Author: Vasudev Ram
Copyright 2017 Vasudev Ram
Web site: https://vasudevram.github.io
Blog: https://jugad2.blogspot.com
Product store: https://gumroad.com/vasudevram
'''

import sys
import string
from msvcrt import getch

def pager(in_fil=sys.stdin, lines_per_page=10, quit_key='q'):
    assert lines_per_page > 1 and lines_per_page == int(lines_per_page)
    assert len(quit_key) == 1 and \
        quit_key in (string.ascii_letters + string.digits)
    lin_ctr = 0
    for lin in in_fil:
        sys.stdout.write(lin)
        lin_ctr += 1
        if lin_ctr >= lines_per_page:
            c = getch().lower()
            if c == quit_key.lower():
                break
            else:
                lin_ctr = 0

def main():
    try:
        sa, lsa = sys.argv, len(sys.argv)
        if lsa == 1:
            pager()
        elif lsa == 2:
            with open(sa[1], "r") as in_fil:
                pager(in_fil)
        else:
            sys.stderr.write
            ("Only one input file allowed in this version")
                    
    except IOError as ioe:
        sys.stderr.write("Caught IOError: {}".format(repr(ioe)))
        sys.exit(1)

    except Exception as e:
        sys.stderr.write("Caught Exception: {}".format(repr(e)))
        sys.exit(1)

if __name__ == '__main__':
    main()
I added a couple of assertions for sanity checking.

The logic of the program is fairly straightforward:

- open (for reading) the filename given as command line argument, or just read (the already-open) sys.stdin
- loop over the lines of the file, lines-per-page lines at a time
- read a character from the keyboard (without waiting for Enter, hence the use of msvcrt.getch [1])
- if it is the quit key, quit, else reset line counter and print another batch of lines
- do error handling as needed

[1] The msvcrt module is on Windows only, but there are ways to get equivalent functionality on Unixen; google for phrases like "reading a keypress on Unix without waiting for Enter", and look up Unix terms like tty, termios, curses, cbreak, etc.

And here are two runs of the program that dogfood it, one directly with a file (the program itself) as a command-line argument, and the other with the program at the end of a pipeline; output is not shown since it is the same as the input file, in both cases; you just have to press some key (other than q (which makes it quit), repeatedly, to page through the content):
$ python tp.py tp.py
$type tp.py | python tp.py

I could have golfed the code a bit, but chose not to, in the interest of the Zen of Python. Heck, Python is already Zen enough.

- 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



Friday, February 10, 2017

Nice one: Problem exists between keyboard and chair

By Vasudev Ram


Good one by Andy Brice (blogger at SuccessfulSoftware.net and founder of Perfect Table Plan and HyperPlan):

Problem exists between keyboard and chair

As a person who has done a good amount of both software development and troubleshooting, and as a user too, the different viewpoints resonate with me (as they still say in Silicon Valley :).

Another good one on similar lines: the best debugging tool is the one between your ears.

Another one (I've heard it attributed to the Pennsylvania Dutch):

We get too soon old and too late schmart

- 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, February 7, 2017

Video (2010): Python vs. Ruby: A Battle to the Death: Gary Bernhardt

By Vasudev Ram

Came across this video recently via a chain of links:

Video (2010): Python vs. Ruby: A Battle to the Death: Gary Bernhardt

His blog post about it:

Both video and post are from 2010.

Started watching the video, seems interesting.

The video is also embedded below.


Python vs. Ruby: A Battle to The Death from Gary Bernhardt on Vimeo.


Enjoy.

- 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



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