Wednesday, May 15, 2019

print(5 * '=' * 5 == '=' * 5 * 5 == 5 * 5 * '=')


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


Hi readers,

I was reviewing some of my Python training material, and saw these lines:
# Multiplying a string by a number results in another string
# with that many copies of the original string.
print "-" * 10
print 10 * '-'

print "=" * 10
print 10 * '='
Notice that the integer can be either to the left or right of the asterisk, and the same for the string, in the expressions in the print statements above.

Thought of experimenting a bit more, and came up with these snippets:
print(5 * '=' == '=' * 5)
True
print(5 * '=' * 5 == '=' * 5 * 5 == 5 * 5 * '=')
True
Which I initially found a little surprising, but if you think about, if comparisons using the less-than or greater-than signs (or variations like less-than-or-equal and greater-than-or-equal) can be chained in Python, why can't comparisons using the equals sign be chained too?

This works in both Python 2 and Python 3.

- Enjoy.

- 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.

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

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

Learning Linux? Hit the ground running with my vi quickstart tutorial. I wrote it at the request of two Windows system administrator friends who were given additional charge of some Unix systems. They later told me that it helped them to quickly start using vi to edit text files on Unix. Of course, vi/vim is one of the most ubiquitous text editors around, and works on most other common operating systems and on some uncommon ones too, so the knowledge of how to use it will carry over to those systems too.

Check out WP Engine, powerful WordPress hosting.

Teachable: feature-packed course creation platform, with unlimited video, courses and students.

Posts about: Python * DLang * xtopdf

My ActiveState Code recipes

Follow me on:


Thursday, April 18, 2019

Python's dynamic nature: sticking an attribute onto an object


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



Hi, readers,

[This is a beginner-level Python post.]

Python, being a dynamic language, has some interesting features that some static languages may not have (and vice versa too, of course).

One such feature, which I noticed a while ago, is that you can add an attribute to a Python object even after it has been created. (Conditions apply.)

I had used this feature some time ago to work around some implementation issue in a rudimentary RESTful server that I created as a small teaching project. It was based on the BaseHTTPServer module.

Here is a (different) simple example program, stick_attrs_onto_obj.py, that demonstrates this Python feature.
My informal term for this feature is "sticking an attribute onto an object" after the object is created.

Since the program is simple, and there are enough comments in the code, I will not explain it in detail.
# stick_attrs_onto_obj.py

# A program to show:
# 1) that you can "stick" attributes onto a Python object after it is created, and
# 2) one use of this technique, to count the number# of calls to a function.

# Copyright 2019 Vasudev Ram
# Web site: https://vasudevram.github.io
# Blog: https://jugad2.blogspot.com
# Training: https://jugad2.blogspot.com/p/training.html
# Product store: https://gumroad.com/vasudevram
# Twitter: https://twitter.com/vasudevram

from __future__ import print_function

# Define a function.
def foo(arg):
    # Print something to show that the function has been called.
    print("in foo: arg = {}".format(arg))
    # Increment the "stuck-on" int attribute inside the function.
    foo.call_count += 1

# A function is also an object in Python.
# So we can add attributes to it, including after it is defined.
# I call this "sticking" an attribute onto the function object.
# The statement below defines the attribute with an initial value, 
# which is changeable later, as we will see.
foo.call_count = 0

# Print its initial value before any calls to the function.
print("foo.call_count = {}".format(foo.call_count))

# Call the function a few times.
for i in range(5):
    foo(i)

# Print the attribute's value after those calls.
print("foo.call_count = {}".format(foo.call_count))

# Call the function a few more times.
for i in range(3):
    foo(i)

# Print the attribute's value after those additional calls.
print("foo.call_count = {}".format(foo.call_count))

And here is the output of the program:
$ python stick_attrs_onto_obj.py
foo.call_count = 0
in foo: arg = 0
in foo: arg = 1
in foo: arg = 2
in foo: arg = 3
in foo: arg = 4
foo.call_count = 5
in foo: arg = 0
in foo: arg = 1
in foo: arg = 2
foo.call_count = 8

There may be other ways to get the call count of a function, including using a profiler, and maybe by using a closure or decorator or other way. But this way is really simple. And as you can see from the code, it is also possible to use it to find the number of calls to the function, between any two points in the program code. For that, we just have to store the call count in a variable at the first point, and subtract that value from the call count at the second point. In the above program, that would be 8 - 5 = 3, which matches the 3 that is the number of calls to function foo made by the 2nd for loop.

Enjoy.

- 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.

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

Getting a new web site or blog, and want to help preserve the environment at the same time? Check out GreenGeeks.com web hosting.

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

Learning Linux? Hit the ground running with my vi quickstart tutorial. I wrote it at the request of two Windows system administrator friends who were given additional charge of some Unix systems. They later told me that it helped them to quickly start using vi to edit text files on Unix. Of course, vi/vim is one of the most ubiquitous text editors around, and works on most other common operating systems and on some uncommon ones too, so the knowledge of how to use it will carry over to those systems too.

Check out WP Engine, powerful WordPress hosting.

Creating online products for sale? Check out ConvertKit, email marketing for online creators.

Teachable: feature-packed course creation platform, with unlimited video, courses and students.

Posts about: Python * DLang * xtopdf

My ActiveState Code recipes

Follow me on:


Thursday, April 11, 2019

The Vindhyas as seen from Bhimbetka




I was browsing some Wikipedia sites about Indian rivers and mountain ranges, and via a chain of links, came across this page, about the Vindhyas as seen from Bhimbetka.

The Vindhyas are a large mountain range in central India.

I lived in Madhya Pradesh for some years as a teenager. Madhya Pradesh is called M.P. for short; the name means, loosely, middle state, referring to its geographic position in India.

The Vindhyas are partly in Madhya Pradesh, and Bhimbetka is in M.P. too.

I had visited Bhimbetka, and also traveled to, or through, many other places in M.P. generally, and the Vindhyas in particular (both within and outside of M.P., such as in Maharashtra, a neighboring state, for example) as a kid and teenager. I loved many of those places.

Tuesday, April 9, 2019

The Blogger issue (unable to use its UI) seems solved now

By Vasudev Ram

Hi, readers,


The Google Blogger issue that I referred to, here and here, seems to be solved now.

I suspected that it was either an issue at Google's end (e.g. an introduced bug that had not yet been fixed) or some configuration / cookie / cache issue at my end.

I did some stuff to debug it, like logging out and back in, logging in to a different Gmail account and then logging back into the previous one, clearing my browser cache, etc. One or other of those things seems to have worked.

I am writing this current post via the regular Blogger UI, not via the email-to-blog interface that I used in the interim. So if the post appears okay, it likely means that the issue has been resolved. Will write a small Python test post or two in the next few days to verify this, and then resume normal blogging.

Thanks for your patience.

- Vasudev


- Vasudev Ram - Online Python training and consulting

Hit the ground running with my vi quickstart tutorial, vetted by two Windows system administrator friends.

Jump to posts: Python * DLang * xtopdf

Interested in a Python, SQL or Linux course?

Get WP Engine, powerful managed WordPress hosting.

Subscribe to my blog (jugad2.blogspot.com) by email

My ActiveState Code recipes


Follow me on:

* Gumroad  * LinkedIn

  * Twitter

Do you create online products? Get Convertkit:

Email marketing for digital product creators


Monday, April 8, 2019