Showing posts with label data-structures. Show all posts
Showing posts with label data-structures. Show all posts

Saturday, February 16, 2019

pprint.isrecursive: Check if object requires recursive representation



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


Tree image attribution

Hi, readers,

I was using the pprint module to pretty-print some Python data structures in a program I was writing. Then saw that it has a function called isrecursive.

The docstring for pprint.isrecursive says:
>>> print pprint.isrecursive.__doc__
Determine if object requires a recursive representation.
Here is a Python 3 shell session that shows what the isrecursive function does, with a list:
>>> import pprint
>>> print(pprint.pprint.__doc__)
Pretty-print a Python object to a stream [default is sys.stdout].
>>> a = []
>>> a
[]
>>> pprint.isrecursive(a)
False
>>>
>>> a.append(a)
>>> a
[[...]]
>>>
>>> pprint.isrecursive(a)
True
How about for a dict?
>>> b = {}
>>> pprint.isrecursive(b)
False
>>>
>>> b[1] = b
>>> b
{1: {...}}
>>> id(b) == id(b[1])
True
>>> pprint.isrecursive(b)
True
How about if an object is recursive, but not directly, like in the above two examples? Instead, it is recursive via a chain of objects:
>>> c = []
>>> d = []
>>> e = []
>>> c.append(d)
>>> d.append(e)
>>> c
[[[]]]
>>> pprint.isrecursive(c)
False
>>> e.append(c)
>>> c
[[[[...]]]]
>>> pprint.isrecursive(c)
True
So we can see that isrecursive is useful to detect some recursive Python object structures.
Interestingly, if I compare c with c[0] (after making c a recursive structure), I get:
>>> c == c[0]
Traceback (most recent call last):
  File "", line 1, in 
RecursionError: maximum recursion depth exceeded in comparison
>>>
In Python 2, I get:
RuntimeError: maximum recursion depth exceeded in cmp

Also, relevant XKCD-clone.

The image at the top of the post is of a tree created in the LOGO programming language using recursion.

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

Get a fast web site with A2 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:



Sunday, October 28, 2018

BASE CS: Exploring the basics of computer science, every Monday, for a year.

By Vasudev Ram

Saw this recently.

basecs: Exploring the basics of computer science, every Monday, for a year.

It's by Vaidehi Joshi, a software engineer who works at Tilde, a company that includes Yehuda Katz, who worked or works on Ember.js, Rails, JQuery and Rust.

Browsed a few of the posts there. Seems good.


- 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 personal coaching sessions.

Here are the course details and some testimonials.

DPD: Digital Publishing for Ebooks and Downloads.

Learning Linux or any other Unix-like OS, like macOS or *BSD?
Hit the ground running with my vi quickstart tutorial. I wrote it for two Windows system administrator friends who were given charge of Unix systems. They said it made it easy for them to start using vi on Unix. Since vim is a superset of vi, it works for vim too.

Check out WP Engine, powerful WordPress hosting.

Creating or want to create 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, June 14, 2018

VIDEO: Good article and video about asymptotic complexity of algorithms

By Vasudev Ram

I saw this nice tutorial about the asympotic complexity of algorithms (Big-Oh notation etc.) via this HN thread:

A Gentle Introduction to Algorithm Complexity Analysis (discrete.gr)

It is by Dionysis Zindros of the University of Athens, Greece.

The HN thread has only a few comments as of now, but one of them led to another good resource on this topic:

Asymptotic Notation, by Jackson Steinkamp of Harvard's CS50 program

The video is also embedded below. It is short, but gives a good idea of the topic.

In my next post, I'll do that analysis of the prime number checker that I ported from Q to Python.

Here is the embedded video:



- Enjoy.

- My Codementor profile

Get fast web hosting with A2Hosting.com

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



Saturday, January 12, 2013

graph-tool. Python module for graph (networks) manipulation and display

graph-tool is a Python module for the creation, manipulation, analysis and display of graphs (the computer science data structure, not computer graphics, though it can display graphs as graphics).

I took a look at some of the code examples of graph-tool. It is fairly easy to understand and use for simple cases, e.g. there are simple library calls to create a new graph, add vertices to it, add edges between vertices, etc. Quick excerpt:

from graph_tool.all import *
g = Graph()
v1 = g.add_vertex()
v2 = g.add_vertex()
e = g.add_edge(v1, v2)
And to draw the above graph:
graph_draw(g, vertex_text=g.vertex_index, vertex_font_size=18,
output_size=(200, 200), output="two-nodes.pdf")
The above code creates this image:



It supports both directed and undirected graphs.

Though exposed as a Python module, graph-tool is internally written in C++ using the Boost Graph Library. It also makes extensive use of template metaprogramming for performance.

The graphics drawing is based on the Cairo Graphics library, which I blogged about recently.

The creator of graph-tool is Tiago de Paula Peixoto, currently a postdoc at the University of Bremen, Germany.


Vasudev Ram