To begin in D coming from Python

Nick Sabalausky a at a.a
Thu Jul 24 14:20:55 PDT 2008


"Luis P. Mendes" <luislupeXXXX at gmailXXXX.com> wrote in message 
news:g62lca$1c7p$1 at 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.
> Sure, for example:
>
> 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);





More information about the Digitalmars-d mailing list