<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <id>tag:www.refactormycode.com,2007:users765</id>
  <link type="application/atom+xml" href="http://www.refactormycode.com/users/765" rel="self"/>
  <title>Auron</title>
  <updated>Wed Oct 22 08:20:01 -0700 2008</updated>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor55911</id>
    <published>2008-10-22T08:20:01-07:00</published>
    <title>[Python] On Computing permutations with a recursive generator expression</title>
    <content type="html">&lt;p&gt;Your code works great but addresses a different problem than this one (&lt;a href="http://refactormycode.com/codes/523-permutation-of-values" target="_blank"&gt;http://refactormycode.com/codes/523-permutation-of-values&lt;/a&gt;), which is really not a permutation (it was my fault).&lt;/p&gt;

&lt;p&gt;I like your one-line solution, although I find it difficult to understand :)&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Auron</name>
      <email>julian.lamas@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/551-computing-permutations-with-a-recursive-generator-expression/refactors/55911" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor54361</id>
    <published>2008-10-08T16:55:10-07:00</published>
    <title>[Python] On Permutation of values</title>
    <content type="html">&lt;p&gt;My second proposal is based in an idea from a peer at work, so credits for him, please. I've just adapted it to Python. I think it uses a very intelligent point of view, and resolves the problem elegantly and without recursion, which is great.&lt;/p&gt;

&lt;pre&gt;def cart(maxs):
    &amp;quot;&amp;quot;&amp;quot;
    Compute each new result as it if was a number where digit i is of
    base maxs[i]. As it happens with natural numbers, we add one to the
    least significant digit in order to obtain the next number. If the
    digit is above a limit, it turns out zero, and we instead add one to
    the next significant digit, and so on and so for:
    
    ... 1 8    ---&amp;gt;    1 9    ---&amp;gt;    2 0 (In base 10, we reset 9 to 0
               (+1)           (+1)        and add one to the next digit)
    
    The results of this fuction are very similar, but each digit is of
    a different base. 
    &amp;quot;&amp;quot;&amp;quot;
    # The first result will be a list of zeroes, that's for sure.
    result = [0 for i in xrange(len(maxs))]
    
    yield result[:]         # You can't yield the result as it is, because the
                            # result reference will be used along the function.
                            # Hence, we return a copy.
    
    # Get the number of different results we will obtain.
    numResults = reduce(lambda x,y: x * y, maxs)
    
    # Now, get each result.
    for n in xrange(1, numResults):
        for i in xrange(len(maxs) - 1, -1, -1):
            if i == len(maxs) - 1:
                result[i] = result[i] + 1
                
            elif result[i + 1] &amp;gt;= maxs[i + 1]:
                result[i + 1] = 0
                result[i] = result[i] + 1
                
            else:
                break
        
        yield result[:]
        
if __name__ == &amp;quot;__main__&amp;quot;:
    l = [3, 4]
    print list(cart(l))&lt;/pre&gt;</content>
    <author>
      <name>Auron</name>
      <email>julian.lamas@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/523-permutation-of-values/refactors/54361" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor53265</id>
    <published>2008-10-07T14:44:17-07:00</published>
    <title>[Python] On Permutation of values</title>
    <content type="html">&lt;p&gt;Excellent! I have to recognize that using both recursion and the yield statement is a bit tricky to me, and I'm having a bad time trying to fully understand your code. I've changed the variable names in order to clarify it a bit, but is not trivial at all :)&lt;/p&gt;

&lt;pre&gt;def cart(posValuesHead, *posValuesTail):
    if not posValuesTail:
        for posValue in posValuesHead:
            yield [posValue]
    else:
        for posValue in posValuesHead:
            for subresult in cart(posValuesTail[0], *posValuesTail[1:]):
                yield [posValue] + subresult

def perm(maxList):
    posValues = []                    # posValues stands for possible values.
    
    for max in maxList:
        posValues.append(xrange(max))
        
    return cart(*posValues)

if __name__ == &amp;quot;__main__&amp;quot;:
    l = [2, 3]
    print list(perm(l))&lt;/pre&gt;</content>
    <author>
      <name>Auron</name>
      <email>julian.lamas@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/523-permutation-of-values/refactors/53265" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code523</id>
    <published>2008-10-07T12:18:49-07:00</published>
    <updated>2010-03-05T22:38:34-08:00</updated>
    <title>[Python] Permutation of values</title>
    <content type="html">&lt;p&gt;Boys and girls, it's time to PYTHON!!! The following piece of code computes, given a list of numbers where each value represent the upper limit an element of a list of the same size can reach, all lists that is possible to build.&lt;/p&gt;

&lt;p&gt;Sorry, my English is a bit crappy, but the problem is not as difficult to understand as it may seem. Let's say I have the following list of size 2:&lt;/p&gt;

&lt;p&gt;[2, 3]&lt;/p&gt;

&lt;p&gt;I want all the possible lists of size 2 (the same length of the original list) where the first element can be 0 or 1 and the second and last element can be 0, 1 or 2. The result should be:&lt;/p&gt;

&lt;p&gt;[[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]]&lt;/p&gt;

&lt;p&gt;That's what the following function does. It is actually based on recursion, but I think it can be do better. What do you think?&lt;/p&gt;

&lt;pre&gt;def perm(ni):
    result = []
    
    # Base case: The list length is zero. The only possible permutation is as well 
    # an empty array.
    if len(ni) == 0:
        result.append([])
        return result 

    # Get all possible permutations for the tail of the list.
    subresult = perm(ni[1:])
    
    # Get each possible value for the head and concat with each permutation
    # previously computed.
    for i in range(ni[0]):    
        for array in subresult:
            result.append([i] + array)

    return result

if __name__ == &amp;quot;__main__&amp;quot;:
    l = [2, 3, 4]
    print perm(l)&lt;/pre&gt;</content>
    <author>
      <name>Auron</name>
      <email>julian.lamas@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/523-permutation-of-values" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor28450</id>
    <published>2008-09-22T12:21:09-07:00</published>
    <title>[C#] On TableAdapter and CommandTimeout</title>
    <content type="html">&lt;p&gt;I think our solutions did take into account stored procedures (in the TableAdapter's CommandCollection). Nevertheless, your approach seems also correct to me (although without the beauty of generics :P).&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Auron</name>
      <email>julian.lamas@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/346-tableadapter-and-commandtimeout/refactors/28450" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor12426</id>
    <published>2008-07-03T13:59:19-07:00</published>
    <title>[C#] On TableAdapter and CommandTimeout</title>
    <content type="html">&lt;p&gt;You see, I don't find using generics as bad. Between your code and mine, I think I (and anyone) can reach to a consensus according to our needs. Thank you very much :).&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Auron</name>
      <email>julian.lamas@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/346-tableadapter-and-commandtimeout/refactors/12426" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor12407</id>
    <published>2008-07-03T10:19:10-07:00</published>
    <title>[C#] On TableAdapter and CommandTimeout</title>
    <content type="html">&lt;p&gt;Oh, yes, you're right. I didn't see the whole point of your code and I realize I finally made a mess. My apologies but...&lt;/p&gt;

&lt;p&gt;As you put it, you have to implement Accessor as a public static class and use it every time you want to set the command timeout and then fill the associated table. At the same time, you could create TableAdapters on your own, but you couldn't modify the command timeout.&lt;/p&gt;

&lt;p&gt;So, it's like if you have two flavours of TableAdapters for each MyTableAdapter implemented in your code:&lt;/p&gt;

&lt;p&gt;* A kind of &amp;quot;singleton&amp;quot; instance MyTableAdapter stored in the Accessor&amp;lt;MyTableAdapter&amp;gt; class: you can access this TableAdapter's SQLCommands through the accessor.
&lt;br /&gt;* Every time you make new MyTableAdapter in your code aouside the Accessor(with the 'new' keyword), you'll have a new &amp;quot;instance&amp;quot; of MyTableAdapter. You can't access to its SQLCommands.&lt;/p&gt;

&lt;p&gt;That's a big downside in my opinion. It would be great if all TableAdapters could be automatically extended, but &amp;quot;there is no silver bullet&amp;quot;, I suppose :)&lt;/p&gt;

&lt;p&gt;Another downside is that you give access to all the internal commands of the singleton TableAdapter, and I think this commands shouldn't be reachable at all. My following code solves this issue.&lt;/p&gt;

&lt;p&gt;&amp;gt; You've added a lot of explicit namespaces. Maybe you have copied it from the dataset generated classes? You don't need it, and they uglify the code a lot.&lt;/p&gt;

&lt;p&gt;Yes, I know. I did it because it makes the code more fail-proof, if someone copy that code in his codebase, it will work, no matter what namespaces he had created :)&lt;/p&gt;

&lt;p&gt;&amp;gt; But most of all, you have copied and pasted the reflecting accessor method for both the properties.
&lt;br /&gt;&amp;gt; That's BAD, I'm sorry.
&lt;br /&gt;&amp;gt; Did you recognize that the GetDataAdapter and GetCommandCollection are the same method, except for the name of the property and the type you cast it. Use the parameters!&lt;/p&gt;

&lt;p&gt;Yes, you're right. I've already solved that (see code below).&lt;/p&gt;

&lt;p&gt;&amp;gt; I'm sorry, but I will give you a 1 star to your refactoring, but I'll wait until you tell me what I can't understand.&lt;/p&gt;

&lt;p&gt;I'm OK with that. In fact, I don't know if I should delete it or not.&lt;/p&gt;

&lt;p&gt;Thanks.&lt;/p&gt;

&lt;pre&gt;## This is the code.

using System.Data;
using System.Data.SqlClient;
using System.Reflection;

namespace MyDataSetTableAdapters {

    internal static class TableAdapterAccessor&amp;lt;TableAdapter&amp;gt; where TableAdapter : new() {

        private static TableAdapter instance = new TableAdapter();

        public static TableAdapter Adapter { get { return instance; } }

        public static int InsertCommandTimeout {
            get { return InsertCommand.CommandTimeout;  }
            set { InsertCommand.CommandTimeout = value; }
        }

        public static int UpdateCommandTimeout {
            get { return UpdateCommand.CommandTimeout; }
            set { UpdateCommand.CommandTimeout = value; }
        }

        public static int DeleteCommandTimeout {
            get { return DeleteCommand.CommandTimeout;  }
            set { DeleteCommand.CommandTimeout = value; }
        }

        public static int SelectCommandTimeout {
            get { return SelectCommand.CommandTimeout; }
            set { SelectCommand.CommandTimeout = value; }
        }

        private static SqlCommand InsertCommand {
            get { return DataAdapter.InsertCommand; }
        }

        private static SqlCommand UpdateCommand {
            get { return DataAdapter.UpdateCommand; }
        }

        private static SqlCommand DeleteCommand {
            get { return DataAdapter.DeleteCommand; }
        }

        private static SqlCommand SelectCommand {
            get { return CommandCollection[0]; }
        }

        private static SqlDataAdapter DataAdapter {
            get { return GetPropertyValue(&amp;quot;Adapter&amp;quot;) as SqlDataAdapter; }
        }

        private static SqlCommand[] CommandCollection {
            get { return GetPropertyValue(&amp;quot;CommandCollection&amp;quot;) as SqlCommand[]; }
        }

        private static object GetPropertyValue(string propertyName) {
            return typeof(TableAdapter).GetProperty(propertyName, BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance).GetValue(Adapter, null);
        }
    }
}

## How to use it.
TableAdapterAccessor&amp;lt;MyTableAdapter&amp;gt;.Adapter.ConnectionString = &amp;quot;...&amp;quot;;                 // whatever...
TableAdapterAccessor&amp;lt;MyTableAdapter&amp;gt;.SelectCommandTimeout = 300;
TableAdapterAccessor&amp;lt;MyTableAdapter&amp;gt;.Adapter.Fill(...);
&lt;/pre&gt;</content>
    <author>
      <name>Auron</name>
      <email>julian.lamas@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/346-tableadapter-and-commandtimeout/refactors/12407" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor12332</id>
    <published>2008-07-02T16:55:24-07:00</published>
    <title>[C#] On Cache SQL Server Views</title>
    <content type="html">&lt;p&gt;This code seems to work pretty straight forward. I can't see an easy way to increase performace. &lt;/p&gt;

&lt;p&gt;You could add an extra column to cCustomers and cPayments with a boolean value. This column would indicate if the row is dirty or not. A dirty row can mean two things:&lt;/p&gt;

&lt;p&gt;* The row has been modified. Changes are not cached yet.
&lt;br /&gt;* The row has been added. Row is not cached yet.&lt;/p&gt;

&lt;p&gt;You'll need a trigger or two in order to automatically update the value of this row. Then, whenever you cache the views, don't drop the cache tables, just update or add the dirty rows to the cache.&lt;/p&gt;

&lt;p&gt;Bye.&lt;/p&gt;

&lt;pre&gt;&lt;/pre&gt;</content>
    <author>
      <name>Auron</name>
      <email>julian.lamas@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/347-cache-sql-server-views/refactors/12332" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Refactor12331</id>
    <published>2008-07-02T16:23:21-07:00</published>
    <title>[C#] On TableAdapter and CommandTimeout</title>
    <content type="html">&lt;p&gt;Thank you very much for your contribution. You made me check some advanced concepts on generics and reflection :D I find using reflection a bit like &amp;quot;hacking your own code&amp;quot;, but it seems to be the only way to do a little of refactoring in this case. I've just adapted your solution to the philosophy behind the initial code (I hope you don't mind!). &lt;/p&gt;

&lt;pre&gt;
namespace DataSetTableAdapters {

    partial class MyTableAdapter {
        public int InsertCommandTimeout {
            get { return TableAdapterAccessor&amp;lt;MyTableAdapter &amp;gt;.GetInsertCommand(this).CommandTimeout;  }
            set { TableAdapterAccessor&amp;lt;MyTableAdapter &amp;gt;.GetInsertCommand(this).CommandTimeout = value; }
        }

        public int UpdateCommandTimeout {
            get { return TableAdapterAccessor&amp;lt;MyTableAdapter &amp;gt;.GetUpdateCommand(this).CommandTimeout;  }
            set { TableAdapterAccessor&amp;lt;MyTableAdapter &amp;gt;.GetUpdateCommand(this).CommandTimeout = value; }
        }

        public int DeleteCommandTimeout {
            get { return TableAdapterAccessor&amp;lt;MyTableAdapter &amp;gt;.GetDeleteCommand(this).CommandTimeout;  }
            set { TableAdapterAccessor&amp;lt;MyTableAdapter &amp;gt;.GetDeleteCommand(this).CommandTimeout = value; }
        }

        public int SelectCommandTimeout {
            get { return TableAdapterAccessor&amp;lt;MyTableAdapter &amp;gt;.GetSelectCommand(this).CommandTimeout;  }
            set { TableAdapterAccessor&amp;lt;MyTableAdapter &amp;gt;.GetSelectCommand(this).CommandTimeout = value; }
        }
    }

    internal static class TableAdapterAccessor&amp;lt;TableAdapter&amp;gt; {

        public static global::System.Data.SqlClient.SqlCommand GetInsertCommand(TableAdapter tableAdapter) {
            return GetDataAdapter(tableAdapter).InsertCommand;
        }

        public static global::System.Data.SqlClient.SqlCommand GetUpdateCommand(TableAdapter tableAdapter) {
            return GetDataAdapter(tableAdapter).UpdateCommand; 
        }

        public static global::System.Data.SqlClient.SqlCommand GetDeleteCommand(TableAdapter tableAdapter) {
            return GetDataAdapter(tableAdapter).DeleteCommand; 
        }

        public static global::System.Data.SqlClient.SqlCommand GetSelectCommand(TableAdapter tableAdapter) {
            return GetCommandCollection(tableAdapter)[0];
        }

        private static global::System.Data.SqlClient.SqlDataAdapter GetDataAdapter(TableAdapter tableAdapter) {
            return typeof(TableAdapter).GetProperty(&amp;quot;Adapter&amp;quot;, global::System.Reflection.BindingFlags.NonPublic | global::System.Reflection.BindingFlags.GetProperty | global::System.Reflection.BindingFlags.Instance).GetValue(tableAdapter, null) as global::System.Data.SqlClient.SqlDataAdapter;
        }

        private static global::System.Data.SqlClient.SqlCommand[] GetCommandCollection(TableAdapter tableAdapter) {
            return typeof(TableAdapter).GetProperty(&amp;quot;CommandCollection&amp;quot;, global::System.Reflection.BindingFlags.NonPublic | global::System.Reflection.BindingFlags.GetProperty | global::System.Reflection.BindingFlags.Instance).GetValue(tableAdapter, null) as global::System.Data.SqlClient.SqlCommand[];
        }
    }
}&lt;/pre&gt;</content>
    <author>
      <name>Auron</name>
      <email>julian.lamas@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/346-tableadapter-and-commandtimeout/refactors/12331" rel="alternate"/>
  </entry>
  <entry>
    <id>tag:www.refactormycode.com,2007:Code346</id>
    <published>2008-07-02T10:19:06-07:00</published>
    <updated>2010-04-29T07:38:04-07:00</updated>
    <title>[C#] TableAdapter and CommandTimeout</title>
    <content type="html">&lt;p&gt;As TableAdapters are are automatically generated by DataSet editor, setting the CommandTimeout property for TableAdapter's internal commands is a common problem (see &lt;a href="http://www.google.com/search?q=tableadapter+commandtimeout" target="_blank"&gt;http://www.google.com/search?q=tableadapter+commandtimeout&lt;/a&gt;). The solution is a partial class of the TableAdapter that adds new functionality, just as the code below. &lt;/p&gt;

&lt;p&gt;My problem with this is that it doesn't scale well. I have several TableAdapters in my DataSet, and I don't want to repeat this code for each of them. I've been thinking for a while but I haven't found a solution: the main problem to refactor this code is that the Adapter and CommandCollection properties are private.&lt;/p&gt;

&lt;p&gt;Anybody has a good idea? If I find an elegant solution, I'll post it here.&lt;/p&gt;

&lt;p&gt;Thanks.&lt;/p&gt;

&lt;pre&gt;namespace DataSetTableAdapters {

    partial class MyTableAdapter {

        public int InsertCommandTimeout {
            get { return (this.Adapter.InsertCommand.CommandTimeout); }
            set { this.Adapter.InsertCommand.CommandTimeout = value;  }
        }

        public int UpdateCommandTimeout {
            get { return (this.Adapter.UpdateCommand.CommandTimeout); }
            set { this.Adapter.UpdateCommand.CommandTimeout = value;  }
        }

        public int DeleteCommandTimeout {
            get { return (this.Adapter.DeleteCommand.CommandTimeout); }
            set { this.Adapter.DeleteCommand.CommandTimeout = value;  }
        }

        public int SelectCommandTimeout {
            get { return (this.CommandCollection[0].CommandTimeout); }

            set {
                for (int i = 0; (i &amp;lt; this.CommandCollection.Length); i = (i + 1)) {
                    if ((this.CommandCollection[i] != null)) {
                        ((global::System.Data.SqlClient.SqlCommand)(this.CommandCollection[i])).CommandTimeout = value;
                    }
                }
            }
        }
    }
}&lt;/pre&gt;</content>
    <author>
      <name>Auron</name>
      <email>julian.lamas@gmail.com</email>
    </author>
    <link type="text/html" href="http://www.refactormycode.com/codes/346-tableadapter-and-commandtimeout" rel="alternate"/>
  </entry>
</feed>

