Showing posts with label D. Show all posts
Showing posts with label D. Show all posts

Wednesday, September 14, 2016

Func-y D + Python pipeline to generate PDF

By Vasudev Ram



Hi, readers,

Here is a pipeline that generates some output from a D (language) program and passes it to a Python program that converts that output to PDF. The D program makes use of a bit of (simple) template programming / generics and a bit of functional programming (using the std.functional module from Phobos, D's standard library).


D => Python


I'm showing this as yet another example of the uses of xtopdf, my Python toolkit for PDF creation, as well as for the D part, which is fun (pun intended :), and because those D features are powerful.

The D program is derived, with some modifications, from a program in this post by Gary Willoughby,

More hidden treasure in the D standard library .

That program demonstrates, among other things, the pipe feature of D from the std.functional module.

First, the D program, student_grades.d:
/*
student_grades.d
Author: Vasudev Ram
Web site: https://vasudevram.github.io
Blog: http://jugad2.blogspot.com
Product store: https://gumroad.com/vasudevram
Adapts code from:
http://nomad.so/2015/08/more-hidden-treasure-in-the-d-standard-library/
*/

import std.stdio;
import std.algorithm;
import std.array;
import std.conv;
import std.functional;

// Set up the functional pipeline by composing some functions.
alias sumString = pipe!(split, map!(to!(int)), sum);

void main(string[] args)
{
    // Data to be transformed:
    // Each string has the student name followed by 
    // their grade in 5 subjects.
    auto student_grades_list = 
    [
        "A 1 2 3 4 5",
        "B 2 3 4 5 6",
        "C 3 4 5 6 7",
        "D 4 5 6 7 8",
        "E 5 6 7 8 9",
    ];

    // Transform the data for each student.
    foreach(student_grades; student_grades_list) {
        auto student = student_grades[0]; // name
        auto total = sumString(student_grades[2..$]); // grade total
        writeln("Grade total for student ", student, ": ", total);
    }
}
The initial data (which the D program transforms) is hard-coded, but that can easily be changed to read it from a text file, for instance. The program uses pipe() to compose [1] the functions split, map (to int), and sum (some of which are generic / template functions).

So, when it is run, each string (student_grades) of the input array student_grades_list is split (into an array of smaller strings, by space as the delimiter); then each string in the array (except for the first, which is the name), is mapped (converted) to integer; then all the integers are summed up to get the student's grade total; finally these names and totals are written to standard output. That becomes the input to the next stage of the pipeline, the Python program, which does the conversion to PDF.

Build the D program with:
dmd -o- student_grades.d
which gives us the executable, student_grades(.exe).

The Python part of the pipeline is StdinToPDF.py, one of the apps in the xtopdf toolkit, which is designed to be used in pipelines such as the above - basically, any Unix, Windows or Mac OS X pipeline, that generates text as its final output, can be further terminated by StdinToPDF, resulting in conversion of that text to PDF. It is a small Python app written using the core class of the xtopdf library, PDFWriter. Here is the original post about StdinToPDF:

[xtopdf] PDFWriter can create PDF from standard input

Here is the pipeline:
$ student_grades | python StdinToPDF.py sg.pdf

Below is a cropped screenshot of the generated output, sg.pdf, as seen in Foxit PDF Reader.

[1] Speaking of functional composition, you may like to check out this earlier post by me:

fmap(), "inverse" of Python map() function

It's about a different way of composing functions (for certain cases). It also has an interesting comment exchange between me and a reader, who showed both how to do it in Scala, and another way to do it in Python. Also, there is another way to compose functions in D, using a function called compose; it works like pipe, but the composed functions are written in the reverse order. It's in the same D module as pipe, std.functional.

Finally (before I pipe down - for today :), check out this HN comment by me (on another topic), in which I mention and link to multiple other ways of doing pipe-like stuff in Python:

Comment on Streem – a new programming language from Matz

- Enjoy.

- Vasudev Ram - Online Python training and consulting

Get updates on my software products / ebooks / courses.

Jump to posts: Python   DLang   xtopdf

Subscribe to my blog by email

My ActiveState recipes



Wednesday, August 31, 2016

Interview: Ruminations on D: Walter Bright, DLang creator


By Vasudev Ram

I read this interview yesterday:

Ruminations on D: An Interview with Walter Bright (creator of the D programming language).

(Walter Bright (Wikipedia))

The interview also links to this talk between Walter and Andrei Alexandrescu, of Modern C++ fame, who joined Walter to work on D (after working at Facebook for some time)) about a D project, warp (a C++ preprocessor), that Walter did for Facebook; the talk is about the D benefits Walter found while working on the project (such as the ease of changing the data structures to try different approaches, and how the ranges and the algorithms that operate upon them are somewhat decoupled).

(Andrei Alexandrescu (Wikipedia))

HN thread about the interview.

You can also check out this video, about the creators or key team members of 4 languages, C++, D, Golang and Rust, talking at LangNext '14:

Video: C++, Rust, D and Go: Panel at LangNext '14

- Vasudev Ram - Online Python training and consulting

Get updates on my software products / ebooks / courses.

Jump to posts: Python   DLang   xtopdf

Subscribe to my blog by email

My ActiveState recipes




Friday, August 5, 2016

The D Language Playground site

By Vasudev Ram


The D language playground site:


is a web site (created somewhat recently, IIRC), where you can write or paste in a chunk of D code into a text box, click Run, and it will run it on their server, and send back the results to be displayed. It also has a tour of the language, and you can go from one example to another, and just run, or modify and run, each of them. So the same site serves both as an online sandbox to experiment with D language snippets, and as an interactive tutorial for D. In both these ways it is similar to the Go language playground/tour site. Either or both are worth checking out, if you are interested in those languages.

- Vasudev Ram - Online Python training and consulting




My Python posts     Subscribe to my blog by email

My ActiveState recipes



Monday, August 1, 2016

Video: C++, Rust, D and Go: Panel at LangNext '14

By Vasudev Ram

I recently came across this video of a panel discussion (in 2014) on modern systems programming languages. Yes, I know the term is controversial. Andrei and Rob say something about it. See the video.

The languages discussed are: C++, D, Go and Rust, by a panel at LangNext '14.

The panel consisted of key team members from the teams working on those languages:

- Bjarne Stroustrup for C++
- Andrei Alexandrescu for D
- Rob Pike for Go
- Niko Matsakis for Rust

(that's by language name in alphabetical order :)

Here is the video embedded below, and a link to it in case the embed does not work for you (sometimes happens).



I have only watched part of it so far (it is somewhat long), but found it interesting.

You can also download it with youtube-dl (written in Python, BTW) or some other video downloading tool, for watching offline. It's about 1.34 GB in size, so plan accordingly.

- Enjoy.

- Vasudev Ram - Online Python training and consulting
Follow me on Gumroad for product updates:



My Python posts     Subscribe to my blog by email

My ActiveState recipes



Sunday, July 31, 2016

deltildefiles: D language utility to recursively delete vim backup files

By Vasudev Ram

~

Here's a small utility I wrote in D (the D programming language). The utility is called deltildefiles (for "delete tilde files"). It lets you delete all files whose names end in "~", the tilde character (Wikipedia), under a specified directory, recursively, i.e. including such files found in sub-directories.

[ BTW, check out the Wikipedia article about tilde, linked above. It has many interesting uses in various domains, not just computer programming, for example, in physics, mathematics, economics, electronics and even juggling :) ]

The vim editor (if the option :se backup is set) names its backups of the files you edit, with the same file name as the original file, plus a tilde at the end. Of course, you can do the same task as this utility deltildefiles, with just a batch file (or two) which uses DEL (with or without /S) on Windows), so I just wrote this for fun and learning. However, if you want to enhance the task with other conditions, etc., then a program (or at least a script, in Python, Perl or other language) may be the better way to go, rather than writing an awkward batch file (even if that is possible) or using PowerShell (haven't used the latter). Another alternative is to use a shell script on Linux, or if you use Cygwin or other Unix emulation on Windows, to use the find and rm commands, that come with it, like:

$ find $dirName -print -name '*~' -exec rm {} \; # or some variation

Be careful with the above command. If you make a typing mistake, like omitting the ~, it could end up deleting the wrong files, and potentially many of them. This is also one reason to make the command into a binary (like I have done) or a shell script, test it thoroughly with dummy / safe data, and from then onward, only use that, instead of typing the find command each time - at least for commands that can delete data or cause other damage.

Anyway, here is the code for deltildefiles.d:
/****************************************************************
File: deltildefiles.d
Purpose: To delete vim backup files, i.e. files ending with ~.
Compile with:
$ dmd deltildefiles.d

Author: Vasudev Ram
Copyright 2016 Vasudev Ram
Web site: https://vasudevram.github.io
Products: https://gumroad.com/vasudevram

Description: To delete all files whose names end with 
the tilde character (~) in the directory subtree 
specified as the command-line argument. 

When you edit a file abc.txt with the vim editor, it will 
first make a backup in the file abc.txt~ (note ~ character
at end of file name). Over time, these files can accumulate.
This utility helps you to delete all such vim backup files 
in a specified directory and its subdirectories.

Use with caution and at your own risk!!!
On most operating systems, once a file is deleted this way
(versus sending to the Recycle Bin on Windows), it is not 
recoverable, unless you have installed some undelete utility.
****************************************************************/

import std.stdio;
import std.file;

void usage(string[] args) {
    stderr.writeln("Usage: ", args[0], " dirName");
    stderr.writeln(
        "Recursively delete files whose names end with ~ under dirName.");
}

int main(string[] args) {
    if (args.length != 2) {
        usage(args);
        return 1;
    }
    string dirName = args[1];
    // Check if dirName exists.
    if (!exists(dirName)) {
        stderr.writeln("Error: ", dirName, " not found. Exiting.");
        return 1;
    }
    // Check if dirName is not the NUL device and is actually a directory.
    if (dirName == "NUL" || !DirEntry(dirName).isDir()) {
        stderr.writeln("Error: ", dirName, " is not a directory. Exiting.");
        return 1;
    }
    try {
        foreach(DirEntry de; dirEntries(args[1], "*~", SpanMode.breadth)) {
            // The isFile() check may be enough, also need to check for
            // Windows vs POSIX behavior.
            if (de.isFile() && !de.isDir()) {
                writeln("Deleting ", de.name());
                remove(de.name());
            }
        }
    } catch (FileException) {
        stderr.writeln("Caught a FileException. Exiting.");
        return 1;
    } catch (Exception) {
        stderr.writeln("Caught an Exception. Exiting.");
        return 1;
    }
    return 0;
}
Compile it with:
$ dmd deltildefiles.d
And you can run it like this:
$ deltildefiles .
which will delete all files ending in ~, in and under the current directory. If you give a file name instead of a directory name, or if you give a non-existent directory name, it gives an error message and exits.

It seems to work as of now, based on some testing I did. May have a few bugs since I haven't tested it exhaustively. May make a few improvements to it over time, such as generalizing from ~ to filename wildcards, more error handling, verbose / quiet flags, etc.

Translate this post into another language with Google Translate
(and, just for kicks, click the speaker icon below the right hand side text box at the above Translate page :).

- Vasudev Ram - Online Python training and consulting

Follow me on Gumroad to get email updates about my products.


My Python posts     Subscribe to my blog by email

My ActiveState recipes



Sunday, May 15, 2016

[DLang]: A simple file download utility in D

By Vasudev Ram



Download image attribution

Hi readers,

Here is another in my recently started series of posts about D (DLang / the D language).

This is a simple download utility written in D, to download a file from a URL.

It makes use of a high-level function called download (sic) in the std.net.curl module, so the code is very simple. But it only handles basic cases; e.g. no authentication or HTTPS support.
/*
File: download.d
Version: 0.1
Purpose: A simple program to download a file from a given URL 
to a given local file. Only handles simple cases. Does not  
support authentication, HTTPS or other features.
Author: Vasudev Ram - https://vasudevram.github.io
Copyright 2016 Vasudev Ram
*/

import std.stdio;
import std.net.curl;

void usage(string program)
{
    writeln("Usage: ", program, " URL file");
    writeln("Downloads the contents of URL to file.");
}

int main(string[] args)
{
    if (args.length != 3)
    {
        usage(args[0]);
        return 1;
    }
    try
    {
        writeln("Trying to download (URL) ", args[1], " to (file) ", args[2]);
        download(args[1], args[2]);
        writeln("Item at URL ", args[1], " downloaded to file ", args[2]);
        return 0;
    }
    catch(Exception e)
    {
        writeln("Error: ", e.msg);
        debug(1) writeln("Exception info:\n", e);
        return 1;
    }
}
A makefile to build it in both debug and release versions:
$ type Makefile

release: download.d
        dmd -ofdownload.exe download.d

debug: download.d
        dmd -debug -ofdownload.exe download.d
To build the release version:
make release
To build the debug version:
make debug
A test run: download the current HN home page:
download news.ycombinator.com nyc.html
Output is in nyc.html; see screenshot below:


The line:
debug(1) writeln("Exception info:\n", e);
gets compiled into the binary / EXE only if you build the debug version.
That line prints a bit more information about the exception than the release version does.

You can also read a few posts about downloading using Python tools.

- Enjoy.

- Vasudev Ram - Online Python training and consulting

Signup to hear about my new courses and products.

My Python posts     Subscribe to my blog by email

My ActiveState recipes



Thursday, May 12, 2016

Getting CPU info with D (the D language)

By Vasudev Ram



I have been using the D programming language for some time.

From the D home page:

[ D is a systems programming language with C-like syntax and static typing. It combines efficiency, control and modeling power with safety and programmer productivity. ]

[ Some information about D:

- D Language home page
- D Overview
- D on Wikipedia ]

Here is a D program, cpu_info.d, that gets some information about the CPU of your machine.
// cpu_info.d
// Author: Vasudev Ram - https://vasudevram.github.io 
// http://jugad2.blogspot.com
import std.stdio;
import core.cpuid;
void main()
{
    writeln("processor: ", processor());
    writeln("vendor: ", vendor());
    writeln("hyperThreading: ", hyperThreading());
    writeln("threadsPerCPU: ", threadsPerCPU());
    writeln("coresPerCPU: ", coresPerCPU());
}
The program can be compiled and run with:
$ dmd cpu_info.d
dmd is the D compiler (the name stands for Digital Mars D). It creates cpu_info.exe which I can then run:
$ cpu_info
processor: Intel(R) Core(TM) i3-2328M CPU @ 2.20GHz
vendor: GenuineIntel
hyperThreading: true
threadsPerCPU: 4
coresPerCPU: 2
- Vasudev Ram - Online Python training and consulting

Signup to hear about my new courses and products.

My Python posts     Subscribe to my blog by email

My ActiveState recipes