Difference between revisions of "Programming in D for Python Programmers"
Revision as of 12:45, 15 April 2015
"Often, programming teams will resort to a hybrid approach, where they will mix Python and C++, trying to get the productivity of Python and the performance of C++. The frequency of this approach indicates that there is a large unmet need in the programming language department.
D intends to fill that need. It combines the ability to do low-level manipulation of the machine with the latest technologies in building reliable, maintainable, portable, high-level code. D has moved well ahead of any other language in its abilities to support and integrate multiple paradigms like imperative, OOP, and generic programming"
-- Walter Bright
This section is under development - feel free to suggest improvements or additions.
D is Like Native Python
- D for the Win A former Python programmer explores the benefits from moving to D
- A hedge fund quant finds that Python begins to choke on the data volumes requiring processing, but that D can cope and is productive.
- D is a Dragon: why D matters for bioinformatics - summary: strong typing, speed, parallelisation with high-performance message passing, productivity, immutability, and high-level abstractions.
- Sambamba: a Google Summer of Code Project involving the Open Bioinformatics Foundation that is the fastest BAM parser. No Python-specific content, but this addresses a common-use domain for Python.
AdRoll is known for their use of Python elsewhere, but their data scientists use D. According to Andrew Pascoe, senior data scientist at AdRoll, "One of the clearest advantages of using D compared to other typical data science workflows is that it compiles down into machine code. Without an interpreter or virtual machine layer, we can rip through data significantly faster than other tools like a Java hadoop framework, R, or python would allow. But D’s compiler is fast enough that in many cases it can be run as if it were a scripting language....The key thing here that separates D from other efficient languages like the oft-suggested C or C++ is that D frees you to program in the style you feel most comfortable with at the given time". He says that they have found that they "can rapidly prototype new infrastructure and analysis tasks, and when efficiency becomes a core concern, we have the ability to refactor that same code base to squeeze as much performance out as possible".
Generators and List Comprehensions
Python's generators and list comprehensions have been thought to be two of the most difficult concepts to replicate in other languages. For the D solution to the problem solved by Python generators, see D Ranges and lazy evaluation. For list comprehensions, see UFCS.
- Theoretical article on ranges as a development of the iterator concept by the C++ guru, Dr Andrei Alexandrescu
- Introduction to Ranges by Ali Çehreli - Part I
- Introduction to Ranges by Ali Çehreli - Part II
- Introduction to D slices
- std.range structures and functions in the Phobos standard library
- std.algorithm generic functions in Phobos
Parallel programming has become increasingly in focus as we approach the beginning of the end of the free lunch from Moore's Law. D makes multiprocessing and threading as simple as possible, but not simpler:
- Free chapter from Dr Andrei Alexandrescu's book on parallelism in D
- Ali Cehreli on parallelism
- Ali Cehreli on message-passing concurrency
- Ali Cehreli on data-sharing concurrency
Interfacing D with an existing codebase
- C interfacing is simple and complete
- C++ interfacing is a key priority of the D core team but much can already be done - in fact more than is described here
- PyD creates seamless interoperation between D and CPython, including for numpy arrays. "It just works". Make sure you visit the github code, and not the old version up at bitbucket.
- Other options are to use cython wrappings to connect to D, or to write in D directly to the Python API. There are examples of this in the PyD examples directory within PyD
Web development, concurrency and JSON/BSON/XML
Phobos includes bindings to the widely-used external curl library within Phobos (std.net.curl) and does include provision within std.csv, std.json and std.xml for processing structured data. The JSON and XML implementations in Phobos could be better, and many people choose to use an external library. The most popular solution for this is Vibe D, and this comes with a useful framework for web development, networking, fiber-based concurrency, JSON and BSON. One can write fiber-oriented code without having to deal with callbacks. (See CyberShadow's presentation at Dconf 2013 for an excellent review of the differences).
Atila Neaves has done one benchmark study on Vibed vs Go vc C vs Erlang in a MQTT broker implementation
Officially vibed is in beta, but the author seems to have high standards, and for many purposes you may find that this is good enough to be production-ready. (Of course, caveat emptor, it goes without saying).
Other general solutions include those available in Adam Ruppe's ARSD micro-framework, and CyberShadow's AE library. See the forums for some further possibilities on the JSON and XML front. D's slices facilitate fast and efficient parsing, and as of some time back, the XML parser in the Tango library was possibly the fastest in the world.
The standard library has SMTP support in std.net.curl.SMTP.
Some options in ARSD and AE Utils. No IMAP as yet, but bindings are under development.
- D Floating Point Features
- arbitrary precision - 'bignum' - arithmetic
- Sargon half-precision fast floating point
- Scientific Computing Projects at code.dlang.org
Libraries - what is the D equivalent of pypi and pip?
See code.dlang.org and the DUB package manager.
Since D provides type inference, high-level constructs, and fast compile-time it is a great language for writing scripts. The first line of the file is ignored if it begins with #! - by combining this with rdmd which handles dependency resolution, D becomes a leader in machine-code scripting language.
- Download and install D for your platform
- Play with the D REPL or Pastebin
- Consider purchasing Dr Andrei Alexandrescu's The D Programming Language, Adam Ruppe's D Cookbook, and reading the free book 'Programming in D' by Ali Çehreli
- Read the D solutions to Rosetta Code problems
- Take a look at the D language videos
- Subscribe to This Week in D
- Attend a meeting of a D User Group near you
- Follow D on LinkedIn, Google+, Facebook, Xing, Reddit, Quora
- Follow some D blogs
- Post in the forums - don't be shy; this is a very helpful community