Friday, May 22, 2015

Talk Python To Me podcast; Australia mandates text-based programming

By Vasudev Ram




Today, for a change, a different kind of Python post, but one that I think will be interesting to my readers:

From the horse's, er, snake's mouth :)

Talk Python to Me "is a weekly podcast hosted by Michael Kennedy. The show covers a wide array of Python topics as well as many related topics (e.g. MongoDB, AngularJS, DevOps)."

The format is a casual 30 minute conversation with industry experts.

I just came across it today, and am checking out the site a bit. The site itself looks good, visually, I mean.

Some of the podcasts also have text transcripts.

I'm reading one of the transcripts, Transcript for Episode #8, of the conversation with Dr. James Curran:

Teaching Python at Grok Learning and Classrooms

Excerpt:

"James Curran is an associate professor in computer science at the University of Sidney and co-founder of Grok Learning, which you can find at groklearning.com. James has been teaching computer science to students and teachers for over a decade. In 2010 he was named one of Sidney magazines top 100 influential people for his work in computer science education."

(I guess the 'Sidney' spelling is due to an error in the automated or manual transcription of the podcast.)

Anyway, the transcript is interesting, since is about Python and/in education / training, which are both among my interests.

An interesting point mentioned in the talk, is that Australia is mandating text-based computer programming in its schools, as opposed to only having visual programming with tools like Scratch. Methinks that is a good idea, since only being able to work with GUI's and no skill with text-based tools (such as text editors and the command line), is not a good thing, IMO. I've come across, and once interviewed (for a client), some Java "programmers" who could not write a simple Java program without reaching for Eclipse. What the hell. Not knocking Java. I'm sure there must be people like that for other languages too. [ Dons latest flame shield made of modern composites in advance ... :) ]

Dr. Curran on the topic:

"So we have done a lot of work with teaching students in seventh and eighth grade, and I think that that is the ideal zone in fact the Australian curriculum that I was just involved in writing has mandated that the kids will learn a text based programming language as opposed to something like visual language, like Scratch. So text based programming is mandated for year seventh and eighth, and Python I think is the ideal language to be teaching there."

- Vasudev Ram - Online Python training and programming

Dancing Bison Enterprises

Signup to hear about new products or services that I create.

Posts about Python  Posts about xtopdf

Contact Page

Wednesday, May 13, 2015

Python video: P is better than NP :)

By Vasudev Ram




Another Raymond Hettinger Python video, both entertaining and informative. This one was at the recently concluded PyCon 2015 in Montreal.

Watch this video to find out how P is better than NP, and how to create adapters for code from other languages that has been ported to Python.

The video is also embedded below.



I originally saw the video via a link in this recent HN thread about Peter Norvig's Python IAQ, which I also tweeted about recently:

The Python IAQ: Infrequently Answered Questions (norvig.com)

- Vasudev Ram - Online Python training and programming

Dancing Bison Enterprises

Signup to hear about new products or services that I create.

Posts about Python  Posts about xtopdf

Contact Page


Friday, May 8, 2015

tabtospaces, utility to change tabs to spaces in Python files

By Vasudev Ram




Near the end of a recent blog post:

asciiflow.com: Draw flowcharts online, in ASCII

, I showed how this small snippet of Python code can be used to make a Python program usable as a component in a Unix pipeline:
for lin in sys.stdin:
    sys.stdout.write(process(lin))


Today I saw Raymond Hettinger (@raymondh)'s tweet about the -t and -tt command line options of Python:
#python tip: In Python 2, the -tt option raises an error when you foolishly mix spaces and tabs. In Python 3, that is always an error.
That made me think of writing a simple Python 2 tool to change the tabs in a Python file to spaces. Yes, I know it can be easily done in Unix or Windix [1] with any of sed / awk / tr etc. That's not the point. So here is tabtospaces.py:
import sys
for lin in sys.stdin:
    sys.stdout.write(lin.replace("\t", "    "))
[ Note: this code converts each tab into 4 spaces. It can be parameterized by passing a command-line option that specifies the number of spaces, such as 4 or 8, and then replacing each tab with that many spaces. Also note that I have not tested the program on many sets of data, just one for now. ]

I created a simple Python file, test1.py, that has mixed tabs and spaces to use as input to tabtospaces.py. Then I ran the following commands:
$ py -tt test1.py
  File "test1.py", line 4
    print arg,
              ^
TabError: inconsistent use of tabs and spaces in indentation

$ py tabtospaces.py < test1.py > test2.py

$ py -tt test2.py
0 1 2 3 4 5 6 7 8 9
which shows that tabtospaces.py does convert the tabs to spaces.

And you can see from this diff that the original test1.py and the test2.py generated by tabtospaces.py, differ only in the use of tabs vs. spaces:
$ fc /l test1.py test2.py
Comparing files test1.py and TEST2.PY
***** test1.py
    for arg in args:
                print arg,

***** TEST2.PY
    for arg in args:
        print arg,

*****

[1] Windix is the latest upcoming Unix-compatible OS from M$, due Real Soon Now. You heard it here first - TM.

- Vasudev Ram - Online Python training and programming

Dancing Bison Enterprises

Signup to hear about new software products or info-products that I create.

Posts about Python  Posts about xtopdf

Contact Page

Friday, May 1, 2015

Can a Python data structure reference itself?

By Vasudev Ram




As part of some Python work, I was tinkering with the built-in globals() function of Python, when I noticed this:
>>> g = globals()
>>> g
{'a': 'A', 'A': 'C', 'C': 4, 'b': 'a', 'g': {...}, '__builtins__': <
module '__builtin__' (built-in)>, 'k': '__doc__', '__package__': None, '__name__
': '__main__', '__doc__': None}
>>> # The variable g seems to have got stored inside of itself!
>>> id(g)
31298152L
>>> id(globals())
31298152L
>>> g['g'] == g
True
>>> g['g']['g'] == g
True
>>> g['g']['g']['g'] == g
True
>>> id(g['g']['g']['g']) == id(g)
True
The above does make a kind of sense, when you remember that the globals() function returns a dict representing the current global symbol table, i.e. all the currently defined attributes (objects) in the global scope.

So when I said g = globals(), the variable g got created in the global scope; so when I next print g, it should contain g itself, i.e. g (a list) contains itself as a list item (of g).

But on further thought, it seems like this should depend on the order of the evaluation of the statement g = globals():

Case 1) If the variable g is created first (and hence the global symbol table now contains it), and if only after that is the globals() function called, and its return value assigned to g, then things should work as shown in the above interpreter output.

Case 2) But if the evaluation works in a different order, i.e. the globals() function is first called (before the variable g is created), then at this point its return value (the dict) should not contain the item with key 'g' (and value g), and it is this dict that should get assigned to the variable g. Hence when we print g, we should not see g again within it.

Considering the above output, it seems like Case 1 is what actually happens. Also, I realized that if the globals() function is returning a copy of the dict that represents the global state, the above output should not work. But it seems to be returning the original dict itself, as shown by this:
>>> id(g) == id(globals())
True
To explore this further, I thought of trying to create a similar self-referential structure, but without using globals():
lis = []
lis.append(1)
print 'lis:', lis
print 'id(lis):', id(lis)
lis.append(lis)
print 'lis:', lis
print 'id(lis):', id(lis)
print 'id(lis[1]):', id(lis[1])
print lis == lis[1]
print lis[1] == lis[1][1]
And the output again seems to indicate that the list lis now references itself, that is, contains itself as an item.

I then thought of testing my Python function to flatten an arbitrarily nested list, with the above definition of lis, as the input argument. As expected, it terminated with a run-time error about recursion limit exceeded. See the post linked in the previous sentence for the flatten function - and some variations on it, in the comments on that post.

I'll write more about this in a following post, after fixing the flatten function to handle this case. On first thought, the fix seems straightforward: at any level in the recursion, check if id(lis) == id(lis[i]) - for any i, and if so terminate with an error about this being a self-referential list - but I'll write and test it first, then blog the results.

The more interesting question is whether this is a known Python feature, or an undocumented one (seems unlikely), or a bug (unlikely), and also whether similar behavior exists in other languages. Interested to hear what others think / know about this.

Update:

I looked in the official Python Language Reference, at the Section 3. Data model:

https://docs.python.org/2/reference/datamodel.html

and saw this excerpt:

[ CPython implementation detail: CPython currently uses a reference-counting scheme with (optional) delayed detection of cyclically linked garbage, which collects most objects as soon as they become unreachable, but is not guaranteed to collect garbage containing circular references. ]

Not sure whether it is relevant to the topic at hand, since, on the one hand, it uses the words "cyclically linked", but on the other, it says "garbage collection".

The image at the top of the post is of an ouroboros, which is "ancient symbol depicting a serpent or dragon eating its own tail", according to Wikipedia. Sort of appropriate, I guess, because Python :)

- Vasudev Ram - Online Python and Linux training and programming

Dancing Bison Enterprises

Signup to hear about new products or services that I create.

Posts about Python  Posts about xtopdf

Contact Page

Sunday, April 26, 2015

pyvim, an experimental pure Python vim clone

By Vasudev Ram




Saw this via Twitter:

pyvim, a pure Python vim clone, by Jonathan Slenders from Belgium.

It's new and experimental, so will not have a lot of features, and may have some bugs.

I tried it out briefly after installing it with:
pip install pyvim
(That's all there is to it. It's location even got added to my PATH.)

Here is a screenshot of me editing a file in pyvim:


And here is a screenshot of running pyvim without specifying a filename, i.e. editing a new file (same image as the one at the top of this post, except that the one at the top links to the pyvim site on Github):


The really interesting thing about the editor being written in Python, is that, as they say, it may later be possible to script the editor in Python:

"Further, when the project develops, it should also become possible to write extensions in Python, and use Python as a scripting language. (Instead of vimscript, for instance.)"

Speaking of vim, if you are new to vim or vi (vim's predecessor), you may like to check out my vi quickstart tutorial, first published in Linux For You magazine. I first wrote the tutorial at the request of some Windows sysadmin friends who were transitioning to Linux, and they told me that it helped them to come up to speed with vi quickly, for basic text editing tasks. I then sent it to Linux For You as an article proposal and they published it.

- Vasudev Ram - Online Python training and programming

Dancing Bison Enterprises

Signup to hear about new products or services that I create.

Posts about Python  Posts about xtopdf

Contact Page