www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - To begin in D coming from Python

reply "Luis P. Mendes" <luislupeXXXX gmailXXXX.com> writes:
Hi,

I use to program in Python, but I need some programs to run much faster.  
So, D seems to be as the best programming language for my needs.
 
Still, there's a long way to go because I've never programmed in C.

To begin with, is version 2 just a developer version or should I start by 
using it? 

In Python, lists can have variables, functions, text and others as 
elements.  As far as I can recall from reading a C book, arrays in C 
don't have this possibility.  What about it in D?  

And regarding list comprehensions like li = [elem*2 for elem in li]?  is 
there something close in D?

For example, how could I do something like:
valores = []
for c in lista_campos: valores.append(getattr(self,c)), so that I can 
have all the 

Is there also any construct similar to dictionaries?

Are there D libraries to access PostgreSQL database or do I have to use 
C's?

I don't know about the theoretical issues regarding language development, 
so documentation with good examples is a must have. Is there something 
like www.diveintopython.org in the short term horizon?

These are just some topics I need to know about D.  I'd appreciate some 
answers.


Luis
Jul 21 2008
next sibling parent reply Mike <vertex gmx.at> writes:
On Mon, 21 Jul 2008 13:51:23 +0200, Luis P. Mendes  
<luislupeXXXX gmailxxxx.com> wrote:

 To begin with, is version 2 just a developer version or should I start by
 using it?

D2 is a moving target, so stick with D1 for the moment.
 In Python, lists can have variables, functions, text and others as
 elements.  As far as I can recall from reading a C book, arrays in C
 don't have this possibility.  What about it in D?

You can put all kinds of stuff into arrays, including delegates and classes. D's arrays are a completely different thing than C's arrays, just have a look at the documentation.
 And regarding list comprehensions like li = [elem*2 for elem in li]?  is
 there something close in D?

Depends on how you define close. There is foreach and with some template trickery you can get similar syntax: // Please note that this works only if you define the template "each" yourself! [1, 2, 3, 4, 5].each((int value) { Stdout("Value: {0}", value).newline; }); But I would recommend you concentrate on the basic stuff first.
 For example, how could I do something like:
 valores = []
 for c in lista_campos: valores.append(getattr(self,c)), so that I can
 have all the

?
 Is there also any construct similar to dictionaries?

Dictionaries are called "Associative arrays" in D. int[char[]] dict; dict["hello"] = 5;
 Are there D libraries to access PostgreSQL database or do I have to use
 C's?

Don't know, but have a look at dsource.org, there are lots of bindings/libraries up. -Mike -- Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 21 2008
next sibling parent reply "Luis P. Mendes" <luislupeXXXX gmailXXXX.com> writes:
Hi Mike,


Mon, 21 Jul 2008 15:02:17 +0200, Mike wrote:

 On Mon, 21 Jul 2008 13:51:23 +0200, Luis P. Mendes
 <luislupeXXXX gmailxxxx.com> wrote:
 
 To begin with, is version 2 just a developer version or should I start
 by using it?

D2 is a moving target, so stick with D1 for the moment.
 In Python, lists can have variables, functions, text and others as
 elements.  As far as I can recall from reading a C book, arrays in C
 don't have this possibility.  What about it in D?

You can put all kinds of stuff into arrays, including delegates and classes. D's arrays are a completely different thing than C's arrays, just have a look at the documentation.

 
 And regarding list comprehensions like li = [elem*2 for elem in li]? 
 is there something close in D?

Depends on how you define close. There is foreach and with some template trickery you can get similar syntax: // Please note that this works only if you define the template "each" yourself! [1, 2, 3, 4, 5].each((int value) { Stdout("Value: {0}", value).newline; }); But I would recommend you concentrate on the basic stuff first.

would have to offer. As in this example, as in other I wrote I didn't want to mimic Python.
 
 For example, how could I do something like: valores = []
 for c in lista_campos: valores.append(getattr(self,c)), so that I can
 have all the

?

I wanted to know if I can load values for attributes in a concise manner. For example, right now I'm working on a project that reads and writes circa 40 values for each instance of an object and in Python I'm able to load the values on a for loop as mentioned above. Thank you for your time and comments. Luis
Jul 21 2008
parent reply Jason House <jason.james.house gmail.com> writes:
Luis P. Mendes Wrote:
 And regarding list comprehensions like li = [elem*2 for elem in li]? 
 is there something close in D?

Depends on how you define close. There is foreach and with some template trickery you can get similar syntax: // Please note that this works only if you define the template "each" yourself! [1, 2, 3, 4, 5].each((int value) { Stdout("Value: {0}", value).newline; }); But I would recommend you concentrate on the basic stuff first.

would have to offer. As in this example, as in other I wrote I didn't want to mimic Python.

I don't know python well enough to translate, but if you're looking to multiply all elements by two, I'd do something like: foreach(index, elem; li) li[index] = elem*2; Note that I may have reversed the order of index and elem. I never remember which one should come first...
Jul 21 2008
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Jason House" <jason.james.house gmail.com> wrote in message 
news:g62d9m$crt$1 digitalmars.com...
 Luis P. Mendes Wrote:
 And regarding list comprehensions like li = [elem*2 for elem in li]?
 is there something close in D?

Depends on how you define close. There is foreach and with some template trickery you can get similar syntax: // Please note that this works only if you define the template "each" yourself! [1, 2, 3, 4, 5].each((int value) { Stdout("Value: {0}", value).newline; }); But I would recommend you concentrate on the basic stuff first.

would have to offer. As in this example, as in other I wrote I didn't want to mimic Python.

I don't know python well enough to translate, but if you're looking to multiply all elements by two, I'd do something like: foreach(index, elem; li) li[index] = elem*2;

foreach(ref elem; li) elem *= 2;
Jul 21 2008
prev sibling parent Jesse Phillips <jessekphillips gmail.com> writes:
On Mon, 21 Jul 2008 12:25:26 -0400, Jason House wrote:

 Luis P. Mendes Wrote:
 And regarding list comprehensions like li = [elem*2 for elem in li]?
 is there something close in D?

Depends on how you define close. There is foreach and with some template trickery you can get similar syntax: // Please note that this works only if you define the template "each" yourself! [1, 2, 3, 4, 5].each((int value) { Stdout("Value: {0}", value).newline; }); But I would recommend you concentrate on the basic stuff first.

would have to offer. As in this example, as in other I wrote I didn't want to mimic Python.

I don't know python well enough to translate, but if you're looking to multiply all elements by two, I'd do something like: foreach(index, elem; li) li[index] = elem*2; Note that I may have reversed the order of index and elem. I never remember which one should come first...

You are correct that index occurs first.
Jul 23 2008
prev sibling next sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Luis P. Mendes:

Welcome to the D language. I think there are other people around here that come
from the Python language.
For more questions like these I suggest you the D.learn newsgroup.

 I use to program in Python, but I need some programs to run much faster.  
 So, D seems to be as the best programming language for my needs.

Most of the times D is faster than CPython, but: - Developing in Python is faster or much faster. - In Python you have more already written libs, and they often are written in C, so you have speed anyway. In D you will probably more often write things by yourself. - D is younger and less used, so you will find 20 times more bugs in the D implementations than in CPython. - In Python there is NumPy, Cython, Psyco, ShedSkin, Inline, Weave and many ways to link external C/Fortran code, so you often don't miss speed. - D too can be used by Python, with Pyd. - Sometimes D code is slower, for example if you use associative arrays (python dicts), and in other situations, so you have to benchmark your code. - If you write a D program the way you write Python, you will often see programs almost as fast as Python ones, or just a bit faster (or sometimes slower). So if you want to go faster you generally must write in a lower-level style. You may need months to understand what this means. - D programs can be quite faster than CPython ones, but if you need speed you may have to use C instead of D, because the D compilers aren't much refined as GCC. I find all the time programs that run 2-3 times faster in C with GCC than in D with DMD (and their source code is exactly the same). When I need more speed I drop to C. - CPython is written in C, so C is much better integrated with Python than D. - If you want to give your code to other people, everyone has a C compiler, while not everyone has a D compiler and knows how to fix bugs in your D code.
 Still, there's a long way to go because I've never programmed in C.

D language is almost multi-level: you can program it almost as Python, almost as C (low level), or more generally in a middle way. If you need speed you have to program in a low-level style, and at this level D looks more like C, so essentially you will have to learn C. The good thing is that D will allow you to learn it progressively, with a softer learning curve.
 To begin with, is version 2 just a developer version or should I start by 
 using it?

If you are on Windows I warmly suggest you to learn the D 1.x version, like the DMD compiler V.1.033. Note that the D world is currently in a mess, because the standard library (Phobos) isn't much standard anymore, lot of people (not me yet) are now using an external almost-de-facto standard lib named Tango, that you may want to use in the future (or even right now). I presume lot of people are just waiting for the developers of D to just give up in developing Phobos and switch to Tango as official built-in std lib.
 In Python, lists can have variables, functions, text and others as 
 elements.  As far as I can recall from reading a C book, arrays in C 
 don't have this possibility.  What about it in D?

Python lists (that are arrays dynamic on the right) use dynamic typing. In D arrays are statically typed, so you can put only one type inside. There are arrays of variants/boxes that allow you to create arrays with mixed types, but I think they are rarely used in D. CPython isn't slow because it's badly written. Its C sources are probably quite more optimized than the C sources of DMD, and some parts of Cpython come from people like Tim Peters and R. Hettinger and more, that have invented first-class algorithms absent in the D implementation (for example to search strings into other strings, to sort items, to hash items, etc). CPython is slower because it's dynamically typed and because most name access require a hashing, etc. That in summary means that if you want to write faster programs (in D) you have to give up something: like the dynamic typing.
 And regarding list comprehensions like li = [elem*2 for elem in li]?  is 
 there something close in D?

List comprehensions are food for Python programmers, but they are not present in D, recently I have written a whole post about this: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=73868 So you can: - write a normal loop - use the map/filter of DMD 2.x - use the things you can find in some libraries, like this large one of mine: http://www.fantascienza.net/leonardo/so/libs_d.zip (You may need the "bud" tool to use this in a simple way, because the D language is dumb still being unable to find modules by itself). Or the Tools lib from Downs. Notes: - But you are a newbie of D, so it may be better for you to exercise yourself in the "basic" D language first, before using external libs (and finding if they are good for you). - If you want to write fast programs you probably can't use high/python-like level of coding in the speed-critical parts of your program (but you can use it in all the other parts to shorten your code, and hopefully to put in less bugs). So the Python code: li = [elem*2 for elem in li] In normal D becomes: foreach (ref elem; li) elem *= 2; But that's in-place. To be closer to the Python version you need something like: typeof(li) li2; li2.length = li.length; foreach (i, elem; li) li2[i] = elem * 2; li = li2; (if "li" isn't an array, but something that behaves like an array, then that code may not work, so you may have to keep the "i" counter manually, but this is a very uncommon situation). Or shorter, but possibly slower (I assume li is really a dynamic array and not something else): li = li.dup; foreach (ref elem; li) elem *= 2; With my libs it may become: li = map((BaseType1!(li) elem){ return elem*2; }, li); If you know that the type of "li" is T it becomes: li = map((T elem){ return elem*2; }, li); With the map() of the std.algorithm of the D 2.x the syntax is probably shorter. With Downs' Tools it will have a different syntax.
 For example, how could I do something like:
 valores = []
 for c in lista_campos: valores.append(getattr(self,c)), so that I can 
 have all the 

Python is slow because it's a dynamic language, while D is a static one, so you will have to give up most of the reflection capabilities of Python (expecially if you need running speed and you don't want to write tons of code), like the getattr(). When you post snippets to be translated it's better to post whole little programs that can be run. I presume you point was to show a Python code like this: class Foo: def __init__(self): self.a = 10 self.b = 20 self.c = 30 def all_attributes(self, attributes): return [getattr(self, c) for c in attributes] f = Foo() print f.all_attributes(["b", "c"]) # prints [20, 30] If the names of the attributes are known at compile time then there are short enough ways to do the same thing in D. If they are only known at compile time you will need much more complex solutions, using external libs that use run-time reflection, but it's for experienced users, I presume (in the future the D standard library may add such run-time things, they are possible in Java too). I can show you a solution for the version where names are known at compile time, if you want, but it may require me a bit of coding. In most D programs you don't do those things.
 Is there also any construct similar to dictionaries?

Thank the Goddess there are, they are named with a quite long but more correct name: "associative arrays" (AAs), they are built-in and you can use them with a simple syntax: int[string] aa; aa["hello".dup] = 10; etc. Note that in many situations Python dicts are faster or much faster than D associative arrays. So in speed-critical points of your code you may have to invent creative ways to avoid using them, like using arrays of bools to represent the set, using enums, using sorted arrays with a binary search, etc. Often what's slow in D is fast in Python and vice versa (but very often what's fast in D is fast in Psyco too). I leave your other questions to other people. Bye, bearophile
Jul 21 2008
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"bearophile" <bearophileHUGS lycos.com> wrote in message 
news:g621m4$2j6a$1 digitalmars.com...

 - CPython is written in C, so C is much better integrated with Python than 
 D.

Uh -- Pyd?
 int[string] aa;
 aa["hello".dup] = 10;
 etc.

I don't know why you're .dupping there.
 Note that in many situations Python dicts are faster or much faster than D 
 associative arrays. So in speed-critical points of your code you may have 
 to invent creative ways to avoid using them, like using arrays of bools to 
 represent the set, using enums, using sorted arrays with a binary search, 
 etc. Often what's slow in D is fast in Python and vice versa (but very 
 often what's fast in D is fast in Psyco too).

Or, you know, use another hash implementation. Like Tango's HashMap, which is consistently faster than the built-in AAs. Or write your own.
Jul 21 2008
parent reply bearophile <bearophileHUGS lycos.com> writes:
Jarrett Billingsley:

bearophile:
 - CPython is written in C, so C is much better integrated with Python than 
 D.


My post refers to Pyd too, do a Find on it. But what I have written there is correct, CPython is written in C, and you have Boost.Python, Weave, Python Inline, ShedSkink, Pyrex, Cython, Swig, etc etc etc. I like Pyd, but for C you can find 20 more things to use it with/through/along Python.
 int[string] aa;
 aa["hello".dup] = 10;
 etc.

I don't know why you're .dupping there.

Maybe it's useless, sorry. But in D V.1.x the management of literal strings is awful, so I often dup them to avoid nasty bugs later. In the past I have described in this newsgroup two different kinds of bugs derived from string literals, that was mostly ignored. In D 2.x strings are immutable so many of those problems may be solved if you use "string".
 Or, you know, use another hash implementation.

I presume the OP was asking about a built-in one, because you can use a hash_map in C++ too (even if it may sometimes be slower than Python dicts). So far I haven't seen external and reliable hash libs that can be used with Phobos that are fast enough. Tango ones too aren't fast enough, I think.
 Like Tango's HashMap, which 
 is consistently faster than the built-in AAs.

They are just a bit faster, their syntax is much worse (and I am using Phobos, but that's not a fault of Tango). Note that I am not saying this to bash D or Tango, I know D is a young language still, I was just stating a fact regarding how I now see things.
 Or write your own.

A really good hash implementation may take few weeks to be written and some years to be tuned/refined :-) If you take a look at the Lua language, you can see that they have recently found ways to speed up their associative arrays significantly. You can't expect a D newbie coming from Python and not knowing C to write a first-class hash implementation. Probably only few (2? 5?) people here are able to do it, and I am not among them :-) Bye, bearophile
Jul 21 2008
parent reply JAnderson <ask me.com> writes:
bearophile wrote:
<snip>
 If you take a look at the Lua language, you can see that they have recently
found ways to speed up their associative arrays significantly.

Do you have a website about this? How recent? What version? Cheers, JAnderson
Jul 21 2008
parent reply bearophile <bearophileHUGS lycos.com> writes:
JAnderson:
 Do you have a website about this?  How recent?  What version?

I don't think that's much recent, here, from chapter 4, page 6-7, from lua 5.0: http://www.tecgraf.puc-rio.br/~lhf/ftp/doc/jucs05.pdf Bye, bearophile
Jul 22 2008
parent reply JAnderson <ask me.com> writes:
bearophile wrote:
 JAnderson:
 Do you have a website about this?  How recent?  What version?

I don't think that's much recent, here, from chapter 4, page 6-7, from lua 5.0: http://www.tecgraf.puc-rio.br/~lhf/ftp/doc/jucs05.pdf Bye, bearophile

ok cheers. I was just wondering if I should upgrade Lua since its one of my bottlenecks however I'm using 5. Cheers, -Joel
Jul 22 2008
parent reply bearophile <bearophileHUGS lycos.com> writes:
JAnderson:
 ok cheers.  I was just wondering if I should upgrade Lua since its one 
 of my bottlenecks however I'm using 5.

Lua is a fast dynamic languages, so if you find bottlenecks in your Lua code you may be doing something wrong (like using Lua for the wrong purpose, etc). Anyway, can't you use the JIT? (http://luajit.org/ ), with that Lua becomes very fast :-) Bye, bearophile
Jul 22 2008
parent JAnderson <ask me.com> writes:
bearophile wrote:
 JAnderson:
 ok cheers.  I was just wondering if I should upgrade Lua since its one 
 of my bottlenecks however I'm using 5.

Lua is a fast dynamic languages, so if you find bottlenecks in your Lua code you may be doing something wrong (like using Lua for the wrong purpose, etc). Anyway, can't you use the JIT? (http://luajit.org/ ), with that Lua becomes very fast :-) Bye, bearophile

Lua is fast but not as fast as C++ even with the JIT. If things become slow in lua (or any scripting language) the typical solution is to move the slow bits over to C++. Of course then designers can't tweak those parts anymore. Aside from communication bottlenecks between C++ and lua, lua has all this extra overhead for allowing things like nil to be safely passed around and having objects that can be anything at anytime. Lua's pretty fast for a scripting language. As far as using it for the wrong purpose; typically I don't like general purpose scripting languages at all for game programming but that's just me. However its not my decision to use it. I like constrained DSL languages (that are tool specific) that meet the task and I'm not one to do unnecessary work if I don't need to. I've only come to that concussion from seeing the results both approaches applied many times. The one argument that half makes sense to me is the cost of VS argument, however I still think a constrained lib is better. A few years ago when I was using it Lua was so untype safe that I've spend more time fighting issues brought up by designers then I think its actually saved. I think there may be a good use for something like lua in game programming however I haven't seen it yet. -Joel
Jul 22 2008
prev sibling next sibling parent reply "Luis P. Mendes" <luislupeXXXX gmailXXXX.com> writes:
Hello bearophile,

thank you for your time.  Please see comments below.


Mon, 21 Jul 2008 09:07:16 -0400, bearophile wrote:

 Luis P. Mendes:
 
 Welcome to the D language. I think there are other people around here
 that come from the Python language. For more questions like these I
 suggest you the D.learn newsgroup.

 
 I use to program in Python, but I need some programs to run much
 faster. So, D seems to be as the best programming language for my
 needs.

Most of the times D is faster than CPython, but: - Developing in Python is faster or much faster. - In Python you have more already written libs, and they often are written in C, so you have speed anyway. In D you will probably more often write things by yourself. - D is younger and less used, so you will find 20 times more bugs in the D implementations than in CPython. - In Python there is NumPy, Cython, Psyco, ShedSkin, Inline, Weave and many ways to link external C/Fortran code, so you often don't miss speed.

- D too can be used by Python, with
 Pyd. - Sometimes D code is slower, for example if you use associative
 arrays (python dicts), and in other situations, so you have to benchmark
 your code. - If you write a D program the way you write Python, you will
 often see programs almost as fast as Python ones, or just a bit faster
 (or sometimes slower). So if you want to go faster you generally must
 write in a lower-level style. 

 Still, there's a long way to go because I've never programmed in C.

D language is almost multi-level: you can program it almost as Python, almost as C (low level), or more generally in a middle way. If you need speed you have to program in a low-level style, and at this level D looks more like C, so essentially you will have to learn C. The good thing is that D will allow you to learn it progressively, with a softer learning curve.

Python, this was what I needed to know.
 To begin with, is version 2 just a developer version or should I start
 by using it?

If you are on Windows I warmly suggest you to learn the D 1.x version, like the DMD compiler V.1.033.

 In Python, lists can have variables, functions, text and others as
 elements.  As far as I can recall from reading a C book, arrays in C
 don't have this possibility.  What about it in D?

Python lists (that are arrays dynamic on the right) use dynamic typing. In D arrays are statically typed, so you can put only one type inside. There are arrays of variants/boxes that allow you to create arrays with mixed types, but I think they are rarely used in D.

[type char, type int, type float,...] ?
 CPython is slower because it's dynamically typed and
 because most name access require a hashing, etc. That in summary means
 that if you want to write faster programs (in D) you have to give up
 something: like the dynamic typing.

that I could use in Python if it provided that possibility. The trade-off between dynamic typing and speed, for me, is in favor of speed. Type safety is also another advantage for me.
 And regarding list comprehensions like li = [elem*2 for elem in li]? 
 is there something close in D?

List comprehensions are food for Python programmers, but they are not present in D, recently I have written a whole post about this: http://www.digitalmars.com/webnews/newsgroups.php?

 
 So you can:
 - write a normal loop
 - use the map/filter of DMD 2.x
 - use the things you can find in some libraries, like this large one of
 mine: http://www.fantascienza.net/leonardo/so/libs_d.zip (You may need
 the "bud" tool to use this in a simple way, because the D language is
 dumb still being unable to find modules by itself). Or the Tools lib
 from Downs.
 
 Notes:
 - But you are a newbie of D, so it may be better for you to exercise
 yourself in the "basic" D language first, before using external libs
 (and finding if they are good for you). - If you want to write fast
 programs you probably can't use high/python-like level of coding in the
 speed-critical parts of your program (but you can use it in all the
 other parts to shorten your code, and hopefully to put in less bugs).

 When you post snippets to be translated it's better to post whole little
 programs that can be run. I presume you point was to show a Python code
 like this:
 
 class Foo:
     def __init__(self):
         self.a = 10
         self.b = 20
         self.c = 30
     def all_attributes(self, attributes):
         return [getattr(self, c) for c in attributes]
 f = Foo()
 print f.all_attributes(["b", "c"])
 # prints [20, 30]
 
 If the names of the attributes are known at compile time then there are
 short enough ways to do the same thing in D. If they are only known at
 compile time you will need much more complex solutions, using external
 libs that use run-time reflection, but it's for experienced users, I
 presume (in the future the D standard library may add such run-time
 things, they are possible in Java too).

and defined in the script.
 Is there also any construct similar to dictionaries?

Thank the Goddess there are, they are named with a quite long but more correct name: "associative arrays" (AAs), they are built-in and you can use them with a simple syntax: int[string] aa; aa["hello".dup] = 10; etc. Note that in many situations Python dicts are faster or much faster than D associative arrays. So in speed-critical points of your code you may have to invent creative ways to avoid using them, like using arrays of bools to represent the set, using enums, using sorted arrays with a binary search, etc. Often what's slow in D is fast in Python and vice versa (but very often what's fast in D is fast in Psyco too).

are probably faster, but less intuitive. But my point was to know if D had such kind of tool and if it was much faster. Thank you again bearophile
Jul 21 2008
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Luis P. Mendes" <luislupeXXXX gmailXXXX.com> wrote in message 
news:g62b67$299b$3 digitalmars.com...

 If you are on Windows I warmly suggest you to learn the D 1.x version,
 like the DMD compiler V.1.033.


I'm not sure what connection bearophile was making between what OS you use and what version of D to use. D2 is in alpha, no matter what OS you're using, and most/all libraries are written for D1, so you're probably better off using D1 unless you really, really want some of the new features in D2.
 So can I define something as this kind of list:
 [type char, type int, type float,...] ?

No. There are structures and classes if you want to group multiple different types, as well as tuples, but they are not first-class objects as in Python and are strictly compile-time entities. It's not that lists of varying types aren't useful, it's that the way you'd do it in D would be (possibly very) different from the way you'd do it in Python, is all. Syntax is sugar. It's not enough to say that "language X doesn't have feature Y so it's not possible to do algorithm Z." It probably is possible, just not in the same way as if you had feature Y.
Jul 21 2008
prev sibling parent reply bearophile <bearophileHUGS lycos.com> writes:
Luis P. Mendes:
 So can I define something as this kind of list:
 [type char, type int, type float,...] ?

There are arrays of boxes/variants that allows you to used mixed types, but you generally don't want to use them in your D code (and boxes have some bugs too). You may want to use a struct for that. There are ways to create them on the fly too (Tuple!(), toTuple() in std.typecons and in my libs), etc.
I wanted to know if I can load values for attributes in a concise manner.<

Can you show a working Python example of what you mean? So I/we can avoid guessing many times.
For example, right now I'm working on a project that reads and writes circa 40
values for each instance of an object and in Python I'm able to load the values
on a for loop as mentioned above.<

For example objects and structs have the .tupleof that allows you to refer to attributes as items of a untyped array. And for example in my libs you can find HasMethod!() and autoAssign(), that can be used like (I don't use them much, but they are there, I'll prune away little used stuff in the future, I'm in the inflation phase still): assert( HasMethod!(__LINE__, MyClass, "foo", int, float) ); assert( HasMethod!(__LINE__, MyClass, "foo") ); (I'd like to remove that __LINE__). // And: this(int first, int second, int third) { mixin(autoAssign("first second third")); } // Is the same as: this(int first, int second, int third) { this.first = first; this.second = second; this.third = third; } So if you know attribute names at compile time you probably need "just" some template trickery to solve your problem (but despite D templates being simpler than C++ ones it may require you some time to learn to use them to perform such tricks). ----------------- Jarrett Billingsley:
I'm not sure what connection bearophile was making between what OS you use and
what version of D to use.<

Generally I talk only about things I know. Not knowing much about D compilers on Linux, I have restricted my suggestion to the Win case :-) Sorry for not being more explicit in my post. Bye, bearophile
Jul 21 2008
parent reply "Nick Sabalausky" <a a.a> writes:
"Luis P. Mendes" <luislupeXXXX gmailXXXX.com> wrote in message 
news:g62lca$1c7p$1 digitalmars.com...
 Mon, 21 Jul 2008 13:10:54 -0400, bearophile wrote:

I wanted to know if I can load values for attributes in a concise
manner.<

Can you show a working Python example of what you mean? So I/we can avoid guessing many times.

self.lista_campos: ['indice', 'emac_periodos', 'emal_bool', 'emal_periodos', 'tr1_bool', 'tr1_periodos', 'tr1_sinal', 'tr1_percent', 'tr2_bool', 'tr2_periodos', 'tr2_sinal', 'tr2_percent', 'tr2_vezes'] individuo_sql: (2264, 42, True, 88, True, 15, '>', Decimal("0.49"), False, 6, '<', Decimal("0.84"), 4] for atributo, valor in zip(self.lista_campos, individuo_sql): setattr (self, atributo, valor) so that: print self.indice 2264 print self.emac_periodos 42 and so on. In this one I also used a zip function to pack values from two lists together.

IMO, that approach is very bad style since it makes it easy to accidentally misalign the elements of the arrays. That makes it difficult to keep the arrays synchronized and can cause hidden bugs. Here are a few alternate approaches that I use in D (I would choose one of these based on what I was trying to do with the data. Also, note I haven't tried to compile these, so there may be some typos and such. Consider them semi-psuedo-code. Also, they might look much more readable in a fixed-width font.) Methods 1-3 force the values to be strings, which might be acceptable if you're not going to do any further processing on them aside from just sticking them into SQL statements. Methods 4-6 retain the correct types for the values and also give you varying levels of protection against accidentally misspelling the names or using names that are unaccounted for elsewhere (at the cost of not being able to create new names/fields at runtime - which might not be a problem depending on what you're doing). // Method 1: Array of structs that each contain two strings struct Pair { char[] name; char[] value; } Pair[] pairs = [Pair("indice", "2264"), Pair("emal_bool", "True"), Pair("tr1_sinal", ">" )]; Stdout.formatln("Name 0: {}", pairs[0].name); Stdout.formatln("Value 0: {}", pairs[0].value); // Method 2: Array of "length-2-arrays" of strings enum Pair { Name=0, Value=1; } char[][][] pairs = [["indice", "2264"], ["emal_bool", "True"], ["tr1_sinal", ">" ]]; Stdout.formatln("Name 0: {}", pairs[0][Pair.Name]); Stdout.formatln("Value 0: {}", pairs[0][Pair.Value]); // Method 3: Associative array of strings char[char[]] pairs; pairs["indice"] = "2264"; pairs["emal_bool"] = "True"; pairs["tr1_sinal"] = ">"; Stdout.formatln("Name: indice"); Stdout.formatln("Value: {}", pairs["indice"]); // Method 4: Struct struct MyData { int indice; bool emal_bool; char[] tr1_sinal; } // Not sure if "data" needs to be static MyData data = {indice: 2264, emal_bool: true, tr1_sinal: ">"}; Stdout.formatln("Name: indice"); Stdout.formatln("Value: {}", data.indice); // Method 5: Class, without using constructor class MyData { int indice; bool emal_bool; char[] tr1_sinal; } auto data = new MyData(); data.indice = 2264; data.emal_bool = true; data.tr1_sinal = ">"; Stdout.formatln("Name: indice"); Stdout.formatln("Value: {}", data.indice); // Method 6: Class, using constructor class MyData { int indice; bool emal_bool; char[] tr1_sinal; this(int indice, bool emal_bool, char[] tr1_sinal) { //I have a template mixin that can clean this up somewhat: this.indice = indice; this.emal_bool = emal_bool; this.tr1_sinal = tr1_sinal; } } auto data = new MyData(2264, true, ">"); Stdout.formatln("Name: indice"); Stdout.formatln("Value: {}", data.indice);
Jul 24 2008
parent "Nick Sabalausky" <a a.a> writes:
"Nick Sabalausky" <a a.a> wrote in message 
news:g6arnr$b44$1 digitalmars.com...
 // Method 3: Associative array of strings
 char[char[]] pairs;

Sorry, that should be: char[][char[]] pairs;
Jul 24 2008
prev sibling next sibling parent "Luis P. Mendes" <luislupeXXXX gmailXXXX.com> writes:
Mon, 21 Jul 2008 13:10:54 -0400, bearophile wrote:

I wanted to know if I can load values for attributes in a concise
manner.<

Can you show a working Python example of what you mean? So I/we can avoid guessing many times.

self.lista_campos: ['indice', 'emac_periodos', 'emal_bool', 'emal_periodos', 'tr1_bool', 'tr1_periodos', 'tr1_sinal', 'tr1_percent', 'tr2_bool', 'tr2_periodos', 'tr2_sinal', 'tr2_percent', 'tr2_vezes'] individuo_sql: (2264, 42, True, 88, True, 15, '>', Decimal("0.49"), False, 6, '<', Decimal("0.84"), 4] for atributo, valor in zip(self.lista_campos, individuo_sql): setattr (self, atributo, valor) so that: print self.indice 2264 print self.emac_periodos 42 and so on. In this one I also used a zip function to pack values from two lists together.
 
 
For example, right now I'm working on a project that reads and writes
circa 40 values for each instance of an object and in Python I'm able to
load the values on a for loop as mentioned above.<

For example objects and structs have the .tupleof that allows you to refer to attributes as items of a untyped array. And for example in my libs you can find HasMethod!() and autoAssign(), that can be used like (I don't use them much, but they are there, I'll prune away little used stuff in the future, I'm in the inflation phase still): assert( HasMethod!(__LINE__, MyClass, "foo", int, float) ); assert( HasMethod!(__LINE__, MyClass, "foo") ); (I'd like to remove that __LINE__). // And: this(int first, int second, int third) { mixin(autoAssign("first second third")); } // Is the same as: this(int first, int second, int third) { this.first = first; this.second = second; this.third = third; } So if you know attribute names at compile time you probably need "just" some template trickery to solve your problem (but despite D templates being simpler than C++ ones it may require you some time to learn to use them to perform such tricks).

Luis
Jul 21 2008
prev sibling parent "Luis P. Mendes" <luislupeXXXX gmailXXXX.com> writes:
On Mon, 21 Jul 2008 12:32:12 -0400, Jarrett Billingsley wrote:

 "Luis P. Mendes" <luislupeXXXX gmailXXXX.com> wrote in message
 So can I define something as this kind of list: [type char, type int,
 type float,...] ?

No. There are structures and classes if you want to group multiple different types, as well as tuples, but they are not first-class objects as in Python and are strictly compile-time entities. It's not that lists of varying types aren't useful, it's that the way you'd do it in D would be (possibly very) different from the way you'd do it in Python, is all. Syntax is sugar. It's not enough to say that "language X doesn't have feature Y so it's not possible to do algorithm Z." It probably is possible, just not in the same way as if you had feature Y.

One of the things that caught my eye on D website is that it is said that D combines power and performance of C, C++ with programmer productivity of modern languages like Ruby and Python. This is the main reason I posted to this group. I wanted to know from very knowledgeable peoplein D whether there were similar structures between D and Python (with increased performance), at least the ones I keep on using in Python. Luis
Jul 22 2008
prev sibling next sibling parent JAnderson <ask me.com> writes:
Luis P. Mendes wrote:
 Hi,
 
 I use to program in Python, but I need some programs to run much faster.  
 So, D seems to be as the best programming language for my needs.
  
 Still, there's a long way to go because I've never programmed in C.
 
 To begin with, is version 2 just a developer version or should I start by 
 using it? 
 
 In Python, lists can have variables, functions, text and others as 
 elements.  As far as I can recall from reading a C book, arrays in C 
 don't have this possibility.  What about it in D?  
 
 And regarding list comprehensions like li = [elem*2 for elem in li]?  is 
 there something close in D?
 
 For example, how could I do something like:
 valores = []
 for c in lista_campos: valores.append(getattr(self,c)), so that I can 
 have all the 
 
 Is there also any construct similar to dictionaries?
 
 Are there D libraries to access PostgreSQL database or do I have to use 
 C's?
 
 I don't know about the theoretical issues regarding language development, 
 so documentation with good examples is a must have. Is there something 
 like www.diveintopython.org in the short term horizon?
 
 These are just some topics I need to know about D.  I'd appreciate some 
 answers.
 
 
 Luis

Ok, I assume you might know some of this but I'm going to talk about some of the basics. Also note that I have the barest knowledge about python. D should be faster then Python in the general case. Its very easy to drop down to the bare metal when necessary. Its a systems language after all. You'll find that you won't always get as concise syntax as python. I don't think that's necessarily a bad thing as it provides extra type safely that you don't get in python. Note: typesafty also means you'll need to write less unit checks because the compiler will do a lot of that testing for you, which I think is an actual time saver. Having said that D is certainly easier to write then C and provides nicer lists. Note lists are typesafe as well so you can only put one type of thing into them. That's not a bad thing (in my option) because normally you need to know what you are going to pull out in the first place. Instead of Duck typing you'll need to learn use inheritance and polymorphism. This is yet another form of compiletime/typesafety. A website you might find useful is the wiki: http://prowiki.org/wiki4d/wiki.cgi -Joel
Jul 21 2008
prev sibling parent reply Jesse Phillips <jessekphillips gmail.com> writes:
On Mon, 21 Jul 2008 11:51:23 +0000, Luis P. Mendes wrote:

 Hi,
 
 I use to program in Python, but I need some programs to run much faster.
 So, D seems to be as the best programming language for my needs.
  
 Still, there's a long way to go because I've never programmed in C.
 
 To begin with, is version 2 just a developer version or should I start
 by using it?
 
 In Python, lists can have variables, functions, text and others as
 elements.  As far as I can recall from reading a C book, arrays in C
 don't have this possibility.  What about it in D?
 
 And regarding list comprehensions like li = [elem*2 for elem in li]?  is
 there something close in D?
 
 For example, how could I do something like: valores = []
 for c in lista_campos: valores.append(getattr(self,c)), so that I can
 have all the
 
 Is there also any construct similar to dictionaries?
 
 Are there D libraries to access PostgreSQL database or do I have to use
 C's?
 
 I don't know about the theoretical issues regarding language
 development, so documentation with good examples is a must have. Is
 there something like www.diveintopython.org in the short term horizon?
 
 These are just some topics I need to know about D.  I'd appreciate some
 answers.
 
 
 Luis

I don't have a lot to add to what was already said, but I do want to note that in using D you are likely going to lean quite a bit of C. This might be directly (something is explain using C), or indirectly (D constructs are similar to C's) I don't have much experience with speed performance tackling, but have read many discussions. And one thing that is not pointed out is that DMD has not had huge development done in the area of optimizing code, and there is GDC that will let you use the GCC backend to do more optimization which supposedly is better than DMD, but still not the same as native C. And Hashtables are currently very inefficient compared to Python's. On a note of when bearophile said that if you write code as you do in Python, you only get about Python speed. I'm not sure how close to the "Python" construct he is referring, but through my lack of experience with speed comparisons, if you right code that follows the D "style" you should see good performance. Hopefully bearophile or someone else will correct me if this is off base. I would definitely suggest sticking with D1, yes even for Linux. Final thoughts, D is a very clean language with a few hiccups and provides a range of tools. It may not suit your needs, but at the current time the best way to know is to try it yourself. If do use it, whether a success or failure, a report on how it went is always useful. Hope you add yourself to the community, and if not for this task, for your own joy.
Jul 23 2008
next sibling parent JAnderson <ask me.com> writes:
Jesse Phillips wrote:
 On Mon, 21 Jul 2008 11:51:23 +0000, Luis P. Mendes wrote:
 
 Hi,

 I use to program in Python, but I need some programs to run much faster.
 So, D seems to be as the best programming language for my needs.
  
 Still, there's a long way to go because I've never programmed in C.

 To begin with, is version 2 just a developer version or should I start
 by using it?

 In Python, lists can have variables, functions, text and others as
 elements.  As far as I can recall from reading a C book, arrays in C
 don't have this possibility.  What about it in D?

 And regarding list comprehensions like li = [elem*2 for elem in li]?  is
 there something close in D?

 For example, how could I do something like: valores = []
 for c in lista_campos: valores.append(getattr(self,c)), so that I can
 have all the

 Is there also any construct similar to dictionaries?

 Are there D libraries to access PostgreSQL database or do I have to use
 C's?

 I don't know about the theoretical issues regarding language
 development, so documentation with good examples is a must have. Is
 there something like www.diveintopython.org in the short term horizon?

 These are just some topics I need to know about D.  I'd appreciate some
 answers.


 Luis

I don't have a lot to add to what was already said, but I do want to note that in using D you are likely going to lean quite a bit of C. This might be directly (something is explain using C), or indirectly (D constructs are similar to C's) I don't have much experience with speed performance tackling, but have read many discussions. And one thing that is not pointed out is that DMD has not had huge development done in the area of optimizing code, and there is GDC that will let you use the GCC backend to do more optimization which supposedly is better than DMD, but still not the same as native C. And Hashtables are currently very inefficient compared to Python's. On a note of when bearophile said that if you write code as you do in Python, you only get about Python speed. I'm not sure how close to the "Python" construct he is referring, but through my lack of experience with speed comparisons, if you right code that follows the D "style" you should see good performance. Hopefully bearophile or someone else will correct me if this is off base. I would definitely suggest sticking with D1, yes even for Linux. Final thoughts, D is a very clean language with a few hiccups and provides a range of tools. It may not suit your needs, but at the current time the best way to know is to try it yourself. If do use it, whether a success or failure, a report on how it went is always useful. Hope you add yourself to the community, and if not for this task, for your own joy.

Some very good points. Cheers! -Joel
Jul 23 2008
prev sibling parent bearophile <bearophileHUGS lycos.com> writes:
Jesse Phillips:
 On a note of when bearophile said that if you write code as you do in 
 Python, you only get about Python speed. I'm not sure how close to the 
 "Python" construct he is referring, but through my lack of experience 
 with speed comparisons, if you right code that follows the D "style" you 
 should see good performance. Hopefully bearophile or someone else will 
 correct me if this is off base.

From my experience if you write code in Python style (using my libs, mostly) you often have code that is about 1.5-4 times faster than the Python one. As you say, if you program in a "D" style the speed can be quite higher. Finally if you program in C style, the speed is generally only 1-10 times of the C, on average about 2 time slower in many small programs I have written :-) Here you can find a discussion about a similar topic, in the zip you can find three D versions that show you what I am talking about (but here the D C-style version is about as fast as the D D-style version): http://leonardo-m.livejournal.com/66171.html Bye, bearophile
Jul 24 2008