Wednesday, April 12, 2017
Interesting programming links of the week
I've been seeing a good number of interesting posts about programming topics lately, on various forums, particularly HN, so I thought of sharing them here (both for my readers and for myself to look up again later). Here are some selected posts or threads that I think are interesting to programmers:
Electron is flash for the desktop (2016) (josephg.com)
Web Development in Go (inburke.com)
Ask HN: Will Rust ever become a mainstream systems programming language?
Computer Programming Using GNU Smalltalk (2009) (canol.info)
Ask HN: What would you use to make cross-platform desktop application?
The reference D compiler is now open source (dlang.org)
The Power of Prolog (metalevel.at)
I also commented on some of the threads.
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 recipesFollow me on: LinkedIn * Twitter Are you a blogger with some traffic? Get Convertkit:Email marketing for professional bloggers
Thursday, January 5, 2017
Give your Python function a {web,CLI} hug!
I came across this interesting Python framework called hug recently:
www.hug.rest
Hug is interesting because it allows you to create a function in Python and then expose it via both the web and the command-line. It also does some data type validation using Python 3's annotations (not shown in my example, but see the hug quickstart below). Hug is for Python 3 only, and builds upon on the Falcon web framework (which is "a low-level high performance framework" for, among other things, "building other frameworks" :).
Here is the hug quickstart.
The hug site says it is "compiled with Cython" for better performance. It makes some claims about being one of the faster Python frameworks. Haven't checked that out.
Here is an HN thread about hug from about a year ago, with some interesting comments, in which the author of hug also participated, replying to questions by readers, explaining some of his claims, etc. Some benchmark results for hug vs. other tools are also linked to in that thread:
I tried out some of the features of hug, using Python 3.5.2, with a small program I wrote.
Below is the test program I wrote, hug_pdp.py. The pdp in the filename stands for psutil disk partitions, because it uses the psutil disk_partitions() function that I blogged about here recently:
Using psutil to get disk partition information with Python
Here is hug_pdp.py:
"""
hug_pdp.py
Use hug with psutil to show disk partition info
via Python, CLI or Web interfaces.
Copyright 2017 Vasudev Ram
Web site: https://vasudevram.github.io
Blog: http://jugad2.blogspot.com
Product store: https://gumroad.com/vasudevram
"""
import sys
import psutil
import hug
def get_disk_partition_data():
dps = psutil.disk_partitions()
fmt_str = "{:<8} {:<7} {:<7}"
result = {}
result['header'] = fmt_str.format("Drive", "Type", "Opts")
result['detail'] = {}
for i in (0, 2):
dp = dps[i]
result['detail'][str(i)] = fmt_str.format(dp.device, dp.fstype, dp.opts)
return result
@hug.cli()
@hug.get(examples='drives=0,1')
@hug.local()
def pdp():
"""Get disk partition data"""
result = get_disk_partition_data()
return result
@hug.cli()
@hug.get(examples='')
@hug.local()
def pyver():
"""Get Python version"""
pyver = sys.version[:6]
return pyver
if __name__ == '__main__':
pdp.interface.cli()
Note the use of hug decorators in the code to enable different kinds of user interfaces and HTTP methods.Here are some different ways of running this hug-enabled program, with their outputs:
As a regular Python command-line program, using the python command:
$ python hug_pdp.py
{'detail': {'0': 'C:\\ NTFS rw,fixed', '2': 'E:\\ CDFS ro,cdrom'
}, 'header': 'Drive Type Opts '}
As a command-line program, using the hug command:
$ hug -f hug_pdp.py -c pdp
{'detail': {'2': 'E:\\ CDFS ro,cdrom', '0': 'C:\\ NTFS rw,fixed'},
'header': 'Drive Type Opts '}
You can see that this command gives the same output as the previous one.But you can also run the above command with the "-c pyver" argument instead of "-c pdp", giving:
$ hug -f hug_pdp.py -c pyver 3.5.2(I added the pyver() function to the program later, after the initial runs with just the pdp() function, to figure out how using the hug command to run the program was different from using the python command to run it. The answer can be seen from the above output, though there is another difference too, shown below (the web interface). Next, I ran it this way:
$ hug -f hug_pdp.pywhich started a web server (running on port 8000), giving this output on the console:
/#######################################################################\
`.----``..-------..``.----.
:/:::::--:---------:--::::://.
.+::::----##/-/oo+:-##----:::://
`//::-------/oosoo-------::://. ## ## ## ## #####
.-:------./++o/o-.------::-` ``` ## ## ## ## ##
`----.-./+o+:..----. `.:///. ######## ## ## ##
``` `----.-::::::------ `.-:::://. ## ## ## ## ## ####
://::--.``` -:``...-----...` `:--::::::-.` ## ## ## ## ## ##
:/:::::::::-:- ````` .:::::-.` ## ## #### ######
``.--:::::::. .:::.`
``..::. .:: EMBRACE THE APIs OF THE FUTURE
::- .:-
-::` ::- VERSION 2.2.0
`::- -::`
-::-` -::-
\########################################################################/
Copyright (C) 2016 Timothy Edmund Crosley
Under the MIT License
Serving on port 8000...
Then I went to this URL in my browser:http://localhost:8000/pdpwhich gave me this browser output:
{"detail": {"0": "C:\\ NTFS rw,fixed", "2": "E:\\ CDFS ro,cdrom"},
"header": "Drive Type Opts "}
which is basically the same as the earlier command-line interface output I got.Next I went to this URL:
http://localhost:8000/pyverwhich gave me this:
"3.5.2 "which again is the same as the earlier corresponding command-line output of the hug command.
Of course, the output from both the web and CLI interfaces is either JSON or a dict, so in a real life app, we would have to get that output and use it in some way, such as (process it further before we) format it better for human consumption. If using a JavaScript front-end, it can easily be done; if using the code as is with the command-line mode, we need to figure out a way to do it. The hug module may have some support for that.
What is also interesting is that when I run it this way:
http://localhost:8000/I get this browser output:
{
"404": "The API call you tried to make was not defined. Here's a definition
of the API to help you get going :)",
"documentation": {
"overview": "\nhug_pdp.py\nUse hug with psutil to show disk partition
info \nvia Python, CLI or Web interfaces.\nCopyright 2017 Vasudev Ram\nWeb site:
https://vasudevram.github.io\nBlog: http://jugad2.blogspot.com\nProduct store:
https://gumroad.com/vasudevram\n",
"handlers": {
"/pdp": {
"GET": {
"usage": "Get disk partition data",
"examples": [
"http://localhost:8000/pdp?drives=0,1"
],
"outputs": {
"format": "JSON (Javascript Serialized Object Notation)",
"content_type": "application/json"
}
}
},
"/pyver": {
"GET": {
"usage": "Get Python version",
"examples": [
"http://localhost:8000/pyver"
],
"outputs": {
"format": "JSON (Javascript Serialized Object Notation)",
"content_type": "application/json"
}
}
}
}
}
}
which shows that trying to access an unsupported route, gives as output, this:an overview, supported URLs/routes, HTTP methods, and documentation about how to use it and the output formats - almost none of which code was written for, mind.
Go give your Python code a hug!
- 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 recipesFollow me on: LinkedIn * Twitter Managed WordPress Hosting by FlyWheel
Thursday, February 18, 2016
Web dev utilities: HTML Encode
HTML Encode, an online HTML encoding utility.
Saw it on the Browserling site, via a post on catonmat.net.
Tried it a little. Useful.
- 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
Saturday, April 11, 2015
Google integrates Blogger with Google Domains
The Google Domains service was started recently. (It is US-only as of now, but you can sign up to be notified when it is available in your own country.)
Today I saw on Blogger that Google has integrated Google Domains with Blogger. So you can now buy a domain from Google via Google Domains, from within Blogger.
Here are a couple of posts about this:
Custom domains for your blog made easy
Making it easier to get your business online with Google Domains
- Vasudev Ram - Online Python training and programming Dancing Bison EnterprisesSignup to hear about new products or services from me. Posts about Python Posts about xtopdf Contact Page
Friday, December 12, 2014
Project Enferno: RAD framework with Python / Flask / MongoDB / Redis
Just saw this via Python Weekly's email newsletter. Enferno seems to be a RAD (Rapid Application Development) framework of sorts, using Python, Flask, some Flask extensions, MongoDB AND MongoEngine, and Redis.
Enferno framework
I've worked on a project or two using Python, Flask and MongoDB (which is a NoSQL database), and found it a useful combination for developing apps relatively quickly. Enferno may be worth checking out, which I will do sometime later and may then blog about it.
Here is an article about NoSQL by Martin Fowler that may be of interest to newcomers to the field. Fowler and Pramod Sadalage have also written a book, NoSQL Distilled, which is linked to from Fowler's NoSQL article page. I've read Fowler's earlier book UML Distilled, a small and useful summary of UML, and it looks from the title that their NoSQL book may be on the same lines.
- Vasudev Ram - Dancing Bison EnterprisesSignup for email about interesting new stuff from me. Contact Page
Other Python posts | Posts about xtopdf
Thursday, March 20, 2014
JSONLint.com, an online JSON validator
JSON page on Wikipedia.
JSON, as most developers nowadays know, has become useful as a data format both for web client-server communication and for data interchange between different languages, since most popular programming languages have support for it (see the lower part of the JSON home page linked above in this sentence).
While searching for information about some specific aspects of JSON for some Python consulting work, I came across this site:
JSONLint.com
JSONLint.com is an online JSON validator. It is from the Arc90 Lab. (Arc90 is the creator of Readability, a tool that removes the clutter from web pages and makes a clean view for reading now or later on your computer, smartphone, or tablet.)
You paste some JSON data into a text box on the site and then click the Validate button, and it tells you whether the JSON is valid or not.
JSONLint.com is a useful resource for any language with JSON support, including Python.
P.S. Arc90 is being acquired by SFX Entertainment, Inc. (NASDAQ:SFXE).
- Vasudev Ram - Python consulting and training
Contact Page
Friday, February 7, 2014
Interesting site - Full Stack Python
Saw this on the Net today.
Full Stack Python.
The creator of the site, Matt Makai, is a developer evangelist for Twilio and describes himself as a full stack developer.
Browsed it for a bit. Full Stack Python is a site that talks briefly (with links to more information) about various aspects and layers of the stack (from hardware/servers up to web frameworks, with VPS, IaaS and PaaS in between) that you need to know about / use when developing applications or products with Python for the Web.
Full Stack Python does seem to cover many of the topics that a developer or an organization using a Python stack for Web development needs to know about, but the amount of information under some of the topics seems to be a bit low or sketchy, considering how many options there are at many layers of the stack. However, it is a good effort.
Update: Here is a Python Reddit thread about Full Stack Python. There are many comments on it.
- Vasudev Ram - Dancing Bison Enterprises
Contact Page
Friday, November 15, 2013
A basic WSGI PDF server
By Vasudev Ram
While browsing the Python Reddit, I saw this post:
Python website tuts that don't use Django, in which the poster (user amnion) asked (excerpted for brevity):
"I'm trying to get a feel for how Python can be used for websites ... I want to familiarize myself with everything under the hood more. ... any resources that show how to make a website without using a framework?"
Many of the answers were interesting (and some had useful links to related reading), but I found this one particularly of interest (user name showed as [deleted] for some reason):
Here's a basic wsgi server (aka "enough to make a website without using a framework").
The variables path and method will contain the path portion of the requested url and the http verb (GET, POST...). "Real" web frameworks are built up around this interface, adding features for url routing, authentication, session management, persistence, etc.
Sans framework, you're free to produce html in any way you see fit. This ten line example runs on python's simple http server, but it will also run on apache/ mod_wsgi, google app engine, or anything supporting wsgi.
"""
basic_wsgi_server.py
Basic WSGI server in Python.
From: http://www.reddit.com/r/Python/comments/1eboql/python_website_tuts_that_dont_use_django/c9z3qyz
"""
from wsgiref.simple_server import make_server
host = 'localhost'
port = 8888
def app(environ, start_response):
path = environ['PATH_INFO']
method = environ['REQUEST_METHOD']
response = 'This is the page for "{}"'.format(path)
start_response('200 OK', [('Content-type', 'text/html')])
return [response]
make_server(host, port, app).serve_forever()
I tried the above code (I named it basic_wsgi_server.py), and it worked.
Then, out of interest, I thought of modifying that basic WSGI server to make it serve PDF, using my xtopdf toolkit for PDF creation. Constant PDF content, though, not dynamically generated stuff. So here is the code of basic_wsgi_pdf_server.py:
# Basic WSGI PDF server in Python.
# Adapted from:
# http://www.reddit.com/r/Python/comments/1eboql/python_website_tuts_that_dont_use_django/c9z3qyz
from debug1 import debug1
from PDFWriter import PDFWriter
from wsgiref.simple_server import make_server
host = 'localhost'
port = 8888
def app(environ, start_response):
debug1("Entered app")
path = environ['PATH_INFO']
method = environ['REQUEST_METHOD']
print "path:", path
print "method:", method
#response = 'This is the page for "{}"'.format(path)
lines = [
"Jack and Jill went up the hill",
"Humpty Dumpty sat on a wall,",
"'You are old, Father William,' the young man said,",
"Master of all masters"
]
debug1("Before creating PDFWriter and setting its fields")
pdf_filename = "Nursery-rhymes-and-stories.pdf"
pw = PDFWriter(pdf_filename)
pw.setFont("Courier", 12)
pw.setHeader("Excerpts from nursery rhymes and stories")
pw.setFooter("Generated by xtopdf and basic_wsgi_pdf_server")
debug1("Before for loop to write the lines")
for line in lines:
pw.writeLine(line)
pw.writeLine(" ")
pw.close()
debug1("Before with statement to read file contents")
with open(pdf_filename, "rb") as fil:
response = fil.read()
#start_response('200 OK', [('Content-type', 'text/html')])
debug1("Before start_response")
start_response('200 OK', [('Content-type', 'application/pdf')])
debug1("Before returning response")
return [response]
debug1("Before make_server and serve_forever()")
make_server(host, port, app).serve_forever()
Ran it with:
python basic_wsgi_pdf_server.py
Note that I added a few calls to a function called debug1, which is a slightly improved version of the debugging function I mentioned in this post:
A simple Python debugging function
, in order to proactively get information about any problem with the program. But it turned out to have no issues - worked straightaway.
Here is a screenshot of the output:
References:
[1] Jack and Jill
[2] Humpty Dumpty
[3] Father William
[4] Master of all masters
Isn't my test data better than Lorem ipsum? :-)
- Vasudev Ram - Dancing Bison Enterprises
Tuesday, March 12, 2013
Netflix's many uses of Python
The Netflix Tech Blog: Python at Netflix
Interesting article.
They talk about using Python for many areas at Netflix, including for the cloud (AWS), caching, process management, web interfaces, data crunching and REST.
I've come across many different uses of Python before, like others have (after all, it is a versatile language, with many "batteries included"), but this may be the first post I've seen that talks about so many uses of Python at one place.
- Vasudev
dancingbison.com
Monday, February 25, 2013
codepad.org, executable multi-language pastebin, in Python
codepad - http://codepad.org - is an online pastebin. It lets you enter your code, written in Python or one of some other popular languages, into a text box. You then click a button on the page to run the code. Codepad shows the output from the run.
You don't have to register on the site to edit and run code, only to save code. You also get a unique URL for your code, which you can share via chat, email or on your web site or blog.
I tried out Codepad with a simple Python function definition and two calls to it. It worked:
http://codepad.org/dkAfNoF8
The Codepad site is written in Python using the Pylons web framework and the SQLAlchemy ORM, and some other techniques such as a supervisor and virtual machines, which are of interest too, IMO.
http://pylonshq.com/
http://www.sqlalchemy.org/
Steven Hazel is the creator of Codepad, and also founder of Sauce Labs, a Selenium testing company.
https://saucelabs.com/
http://en.m.wikipedia.org/wiki/Selenium_(software)
http://docs.seleniumhq.org/
- Vasudev
dancingbison.com
Sunday, February 24, 2013
vitess - scalable RPC interface to MySQL, used by YouTube
vitess - Scaling MySQL databases for the web - Google Project Hosting
Vitess, used at YouTube, is a scalable RPC interface to MySQL, with sharding, limited ACID support, Python and Go client interfaces, and other features.
.
Saturday, February 23, 2013
Dashboard gauges for JavaScript from TechOctave
Beautiful dashboard gauges for the sophisticated developer.
Just saw this.
TechOctave, a US company, makes dashboard gauges for JavaScript using Raphael, the JavaScript graphics library. The gauges look very good. It is a paid product.
I had blogged about Raphael.js some time ago on my older blog, jugad.livejournal.com
- Vasudev Ram
dancingbison.com
jugad2.blogspot.com
twitter.com/vasudevram
Wednesday, January 2, 2013
Using Go to create web apps - post and threads
Thoughts on Go after writing 3 websites : programming
First saw it on Hacker News.
The above thread is on Reddit.
Interesting stuff.
Saturday, November 17, 2012
RequestBin: collect and inspect HTTP requests (service / CLI tool)
RequestBin — Collect and inspect HTTP requests, debug webhooks
Looks interesting.
The site says the CLI (command-line interface) tool is coming soon.
I am trying out the hosted service.
Sunday, October 7, 2012
Stylizer, real-time CSS editor integrated with 9 browsers
Stylizer is a CSS editor that integrates with 9 browsers and lets you make and see changes in real time. They seem to have Sony, IBM and Thomson Reuters as customers, among others (going by the logos at the bottom of their home page).
On their site: "Real-time CSS Editing Saves Time and Money."
The site is visually impressive, IMO.
Stylizer features.
It seems to be a paid app, but you can download Stylizer to try it out.
- Vasudev Ram - Dancing Bison Enterprises
Wednesday, June 6, 2012
Bottle Python framework is useful for small web apps
Bottle is a small Python web framework.
It can be useful for creating small / experimental web apps in Python.
It's easy to get started with. Installation is simple.
Creating small apps is also fairly easy.
The Hello World app is just this:
from bottle import route, run
@route('/hello/:name')
def index(name='World'):
return 'Hello %s!' % name
run(host='localhost', port=8080)
I have been trying it out over the last few days and found it a good fit for the uses described above.
Like many web frameworks these days, it comes with a built-in development web server, so you don't have to spend time fiddling around trying to make it work with Apache or other servers - at least, not until you are ready to deploy your app in production.
Actually, as per the docs, running Bottle with CherryPy, Fapws3, Flup or Paste is simple. For example, this is how to use Paste (and it's similar for the others):
from bottle import PasteServer ... run(server=PasteServer)
And the docs specify how to make it work on Apache with mod_wsgi.
I like the way the routes (URL-to-function mappings) are specified in Bottle.
- Vasudev Ram - Dancing Bison Enterprises
Saturday, May 5, 2012
Opa, new "unified" language for web app dev
See the comments both, pro and con, on Hacker News. Also google for meanings of opa :)
Thursday, April 12, 2012
Meteor real-time Javascript framework getting noticed
http://docs.meteor.com/#concepts
There was a thread about it on Hacker News recently. Concepts seem good (see above link), time will tell.
- Vasudev Ram
www.dancingbison.com


