Friday, May 6, 2011

Generic-like behavior in PL/SQL procedure parameters

Suppose I have some data types defined in PL/SQL:

TYPE foo_t IS RECORD (...);
TYPE foo_table_t IS TABLE OF foo_t INDEX BY BINARY_INTEGER;

TYPE bar_t IS RECORD (...);
TYPE bar_table_t IS TABLE OF bar_t INDEX BY BINARY_INTEGER;

Is it possible for me to write a procedure capable of accepting any data type derived from TABLE (for example, either a foo_table_t or a bar_table_t) as a parameter? The procedure has no need for knowledge of the table's row types. A trivial example:

PROCEDURE remove_last(some_table ...) IS
BEGIN
    some_table.DELETE(some_table.LAST);
END;
From stackoverflow
  • Not directly. From the PL/SQL programmer's guide:

    "The actual parameter and its corresponding formal parameter must have compatible datatypes."

    PL/SQL does an implicit conversion of actual parameter datatypes to formal parameter datatypes. So, you could pass a number value to a procedure that wants a string, and it would work because you can do an implicit conversion.

    The best you could do would be to write overloaded procedures:

    PROCEDURE generic(foo IN OUT foo_t);

    PROCEDURE generic(bar IN OUT bar_t);

    Then you can call generic with either record type. This loses attractiveness in proportion to the number of record types to handle :-D

Generic List Equivalent of DataTable.Rows.Find using VB.NET?

I am converting DataTables to a generic list and need a quick and easy way to implement a Find function. It seems I am going to have to use a Predicate. Upon further investigation, I still can't seem to re-create the functionality. I have this predicate...

Private Function ByKey(ByVal Instance As MyClass) As Boolean
    Return Instance.Key = "I NEED THIS COMPARISON TO BE DYNAMIC!"
End Function

And then calling it like this...

Dim Blah As MyClass = MyList.Find(AddressOf ByKey)

But I have no way to pass in a key variable to this predicate to do the comparison, as I used to do with DataTable...

Dim MyRow as DataRow = MyTable.Rows.Find(KeyVariable)

How can I setup a predicate delegate function in VB.NET to accomplish this?

Do not recommend LINQ or lambdas because this is question is regarding .NET version 2.0.

From stackoverflow
  • Just put your predicate in a class instance:

    Public Class KeyMatcher
        Public Sub New(ByVal KeyToMatch As String)
           Me.KeyToMatch = KeyToMatch
        End Sub
    
        Private KeyToMatch As String
    
        Public Function Predicate(ByVal Instance As MyClass) As Boolean
           Return Instance.Key = KeyToMatch
        End Function
    End Class
    

    and then:

    Dim Blah As MyClass = MyList.Find(AddressOf New KeyMatcher("testKey").Predicate)
    

    We can even get a little fancy and make this generic:

    Public Interface IKeyed(Of KeyType)
        Public Key As KeyType
    End Interface
    
    Public Class KeyMatcher(Of KeyType)
        Public Sub New(ByVal KeyToMatch As KeyType)
           Me.KeyToMatch = KeyToMatch
        End Sub
    
        Private KeyToMatch As KeyType
    
        Public Function Predicate(ByVal Instance As IKeyed(Of KeyType)) As Boolean
           Return Instance.Key = KeyToMatch
        End Function
    End Class
    

    And then make your MyClass type implement the new IKeyed interface

    Josh Stodola : That looks like it will work. Thanks! I will do some testing and get back to ya.
    Josh Stodola : Thanks again! I ended up writing a generic class that also uses reflection so that it can be used to match any field to any value from any type. It is certainly not as efficient as without reflection, but I needed to flexibility.
  • Thank you great example vb.net list object find. The first example is working for 100%. But then I try make more generic class (your second example) I have a problems with interface defenition.

    Than I try add 'Public Key As KeyType' in interface defenition VS2008 say me what 'Interface members must be methods, properties, events, or type definitions.' In this case the code is not working. I was try add sub or function to interface but expect troubles to use interface in class.

    Could you please help me resolve this problem.

    Thank you for help and your time in advance.

    Regards, Andrejs.

Elementwise ifs in matlab - do they exist?

Say I have the following basic if-statement:

if (A ~= 0)
   % do something like divide your favorite number by A
else
   % do something like return NaN or infinity
end

The problem is that A is not a simple number but a vector. Matlab returns true if no element in A is 0. What I am looking for is a vectorized? way of perforimg the if-statement above for each element in A.

Actually, I simply want to do this as fast as possible.

From stackoverflow
  • Are you looking for all non-zero elements? You can do this a couple of ways.

    nonzero = find(A); % returns indicies to all non-zero elements of A
    y = x./A(nonzero); % divides x by all non-zero elements of A
                       % y will be the same size as nonzero
    

    Or for a one-liner, you can use a conditional in place of indicies

    y = x./A(A~=0); % divides x by all non-zero elements of A
    
    Mark Ruzon : This statement will cause y to be shorter than A if any element of A is zero, which may or may not be desired.
  • What you need to do is identify the elements you want to operate on. I would use FIND. I store the results in VI (Valid Indicies) and use that to populate the matrix.

    clear
    clc
    
    den = [2 0 2; 0 2 0; -2 -2 -2]
    num = ones(size(den));
    frac = nan(size(den));
    
    vi = (den ~=0)
    
    frac(vi) = num(vi)./den(vi)
    
    vi = (den == 0)
    
    frac(vi) = nan %just for good measure...
    
    Jason S : fyi: you don't need to use find, you can use boolean matrices directly. The value of find is to extract particular index values, and/or to save memory space if only a few values are true.
    MatlabDoug : removed FIND as requested by JASON S
  • Vectorized ifs don't exist, but there are some options. If you want to test for all or any elements true, use the all or any function.

    Here's one example of conditionally modifying values of a matrix:

    b = A ~= 0;      % b is a boolean matrix pointing to nonzero indices
                     % (b could be derived from some other condition,
                     %  like b = sin(A)>0
    A(b) = f(A(b))   % do something with the indices that pass
    A(~b) = g(A(~b)) % do something else with the indices that fail
    
    AnnaR : I didn't realise b (the boolean matrix) existed until you pointed it out. Thanks!
  • B = zeros(size(A));
    B(A~=0) = FAV./A(A~=0);  
    B(A==0) = NaN;
    
  • In general, to perform one operation on some elements of a matrix and another operation on the remaining elements, a one-line solution is:

    Z = B .* X + ~B .* Y;
    

    where B is a logical matrix. As an example,

    Z = (A == 0) .* -1 + (A ~= 0) .* A;
    

    copies A but assigns -1 everywhere that A is zero.

    However, because the question deals with infinity or NaNs, it can be done even more succinctly:

    Z = FAV ./ A; % produces inf where A == 0
    Z = (A ~= 0) .* FAV ./ A; % produces NaN where A == 0
    
    gnovice : +1: Clever one-liner!

Floating another div next to a main centered div

Ok so I have a website and the entire thing is wrapped in a container. This container is centered with margin:auto. I would like to float a piece of content to the right of this centered container and have it sort of stick to the side of it no matter if the user resizes the browser window, etc. I'm wondering if there's a real simple way to do this rather than adding another huge div, giving it width and floating the centered portion to the left and the piece of content to the right. Thanks!

From stackoverflow
  • Floating is the way to go for this. They will always stick together, unless the container is smaller than the sum of their widths.

    Tip: make sure your container is wide enough to hold both inner divs; if not, and the user has a narrower window, they will show one below the other.

    Nick Allen - Tungle139 : The only problem with this is that it will push the container div off center
    Seb : It will not, if you set margins accordingly.
  • Give the container div the property position: relative; place your floating div as the first child of the container div and give it

    #floatingDiv
    {
        position: absolute;
        top: 0;
        right: -{widthOfFloatedDiv};
    }
    

    I think that will work, but untested

    Okay so tested it and it works

    <div style="position: relative; width: 980px; margin: 0 auto; border: 1px solid #000; height: 400px;">
        <div style="position: absolute; top: 0; right: -200px; width: 200px;">
            <p>Floated DIV</p>
        </div>
        <p>container div</p>
    </div>
    
  • Piggybacking on @NickAllen, you want to use absolute positioning so that the width of the sidebar isn't included in the centering on the primary container.

    <style type="text/css">
        #container {
         position: relative;
         width: 500px;
         border: 1px solid #f00;
         margin: 0px auto;
        }
        #sidebar {
         position: absolute;
         width: 200px;
         right: -200px;
         border: 1px solid #0f0;
        }
    
    </style>
    
    <div id="container">
        <div id="sidebar">
         [ sidebar content ]<br>
         [ sidebar content ]<br>
        </div>
        [content]<br>
        [content]<br>
        [content]<br>
    </div>
    
    great_llama : ... with a caveat that because the sidebar is taken out of the layout calculations that you'll always want your primary content to be longer than the sidebar!
    Nick Allen - Tungle139 : very weird that we both went for a 200px sidebar in our examples! lol
    great_llama : 200px leaves plenty of padding for those 160 x 600 banner ads... =)
  • Maybe I'm misunderstanding your question, but isn't this what you want:

    <div id="container">
        <div id="sidebar">
        some content
        </div>
    </div>
    
    #container {
        width: 960px;
        margin: 0px auto;
    }
    
    #sidebar {
        float: right;
    }
    

ASP.NET - Internal messaging system

I want to implement a messaging system in my Web application similar to the one Chase and Facebook have on their websites. Should I develop this on my own, or should I find a 3rd party control or other solution? Would google provide some sort of API to make this easy? I'm leaning towards developing this on my own because it probably won't involve too much but wanted to know my options.

All suggestions/comments are appreciated!

From stackoverflow
  • In that case it would be much better to build a messaging system of your own.

Bus error vs Segmentation fault

Difference between a bus error and a segmentation fault? Can it happen that a program gives a seg fault and stops for the first time and for the second time it may give a bus error and exit ?

From stackoverflow
  • I assume you're talking about the SIGSEGV and SIGBUS signals defined by Posix.

    SIGSEGV occurs when the program references an invalid address. SIGBUS is an implementation-defined hardware fault. The default action for these two signals is to terminate the program.

    The program can catch these signals, and even ignore them.

  • For instance, a bus error might be caused when your program tries to do something that the hardware bus doesn't support. On SPARCs, for instance, trying to read a multi-byte value (such as an int, 32-bits) from an odd address generated a bus error.

    Segmentation faults happen for instance when you do an access that violate the segmentation rules, i.e. trying to read or write memory that you don't own.

    Thunderboltz : what do mean when you say "read or write memory that you don't own ? " when you do a malloc you have allocated like say 5 bytes of memory. If you read/write memory you don't own it does not give you a Seg fault in C.
    Geek : On the contrary overwriting a memory owned by some other object in the same address space gives a Segmentation fault ??
    David Schmitt : "What you own" on the OS level is typically much more than what the runtime surfaces (e.g. via malloc) to you. Thus there is much space for memory to access which you own, but still shouldn't and there is much address space which you may read, but not write (most mapped libraries) as well as specific functions to write protect memory regions (mprotect).
    David Schmitt : @Geek: the OS has no way of knowing "who" is doing the write within the same address space. Thus it cannot protect you from overwriting memory within the same program. That's the reason why most security exploits work.
    unwind : I obviously suck, compared to Pax and Bastien. :) But yeah, @Thunderboltz, as other commenters (and P&B) explained, segfaults happen when you try to access memory that doesn't belong to you.
  • On most architectures I've used, the distinction is that:

    • a SEGV is caused when you access memory you're not meant to (e.g., outside of your address space).
    • a SIGBUS is caused due to alignment issues with the CPU (e.g., trying to read a long from an address which isn't a multiple of 4).
    bk1e : Memory mapped files can also generate SIGBUS.
  • This would be a dup of What is a bus error?, if it weren't for the

    Can it happen that a program gives a seg fault and stops for the first time and for the second time it may give a bus error and exit ?

    part of the question. You should be able to answer this for yourself with the information found here.


    Insanity: doing the same thing over and over again and expecting different results.
    -- Albert Einstein


    Of course, taking the question literally...

    #include <signal.h>
    #include <stdlib.h>
    #include <time.h>
    #include <unistd.h>
    int main() {
        srand(time(NULL));
        if (rand() % 2)
            kill(getpid(), SIGBUS);
        else
            kill(getpid(), SIGSEGV);
        return 0;
    }
    

    Tada, a program that can exit with a segmentation fault on one run and exit with a bus error on another run.

  • Interpreting your question (possibly incorrectly) as meaning "I am intermittently getting a SIGSEGV or a SIGBUS, why isn't it consistent?", it's worth noting that doing dodgy things with pointers is not guaranteed by the C or C++ standards to result in a segfault; it's just "undefined behaviour", which as a professor I had once put it means that it may instead cause crocodiles to emerge from the floorboards and eat you.

    So your situation could be that you have two bugs, where the first to occur sometimes causes SIGSEGV, and the second (if the segfault didn't happen and the program is still running) causes a SIGBUS.

    I recommend you step through with a debugger, and look out for crocodiles.

  • SIGBUS will also be raised if you mmap() a file and attempt to access part of the mapped buffer that extends past the end of the file, as well as for error conditions such as out of space. If you register a signal handler using sigaction() and you set SA_SIGINFO, it may be possible to have your program examine the faulting memory address and handle only memory mapped file errors.

How to react to Excel events in Ruby?

I can find 10^1000 examples of scripting Excel using Ruby, but I can't for the life of me figure out how to have Ruby react to events in Excel. I'm trying retrieve the contents of a row in a worksheet when it's selected, but such an event-based retrieval I can't find any methods or examples for.

From stackoverflow
  • Use the *WIN32OLE_EVENT.new* method to create an OLE Event object, then call its *on_event* method. Call the *WIN32OLE_EVENT.message_loop* method to launch the event-monitoring loop.

    Here's an example that prints out the selected range's value, then halts event monitoring before the workbook is saved:

    require 'win32ole'
    
    def exit_event_loop
        $LOOP = false
    end
    
    xl = WIN32OLE.connect('Excel.Application')
    wb = xl.ActiveWorkbook
    
    ev = WIN32OLE_EVENT.new(wb, 'WorkbookEvents')
    ev.on_event('SheetSelectionChange') do
        range = xl.Selection
        puts(range.Value)
        STDOUT.flush
    end
    
    ev.on_event('BeforeSave') do
        exit_event_loop
    end
    
    $LOOP = true
    while $LOOP
        WIN32OLE_EVENT.message_loop
        sleep 0.1
    end
    

    Hope that helps.

How to estimate and plan the development of a training?

If a senior software engineer is asked to develop an internal technical training, can she apply the estimation and planning techniques (related to software development) she knows?

From stackoverflow
  • What development methodology do you use? Agile tends to not be documentation-heavy, whereas waterfall methods let you develop documentation while developing the program (since everything is already known). In the past I've written what I thought people might need, handed it off to someone that has never used the program to see if they can use it easily. If not, I fill in the holes or clarify items. Then I push it out.

  • It depends what estimation and planning techniques the senior software engineer knows.

    As a software developer and an instructional designer, I have seen both sides of the coin. Traditional instructional systems design bears a close resemblance software development methodolgies because it leverages the same systems analysis concepts. It must be considered, though, that the development of training and the development of software address solutions in two different domains (i.e. human learning vs. software applications). Senior software engineers and other subject matter experts need to take that into consideration.

    There are many rules of thumb in the instructional design literature that can be used as aids in estimation and planning (see, for example, "Estimating Training Design and Developing Time and Costs".) These averages can then be refined by empirical evidence and the knowledge of subject matter experts. The instructional design process is not a closed system, so any industry specific tools that can be used in the analysis and design phases will make the training program that much better.

    The most important thing to remember is to focus on the learning process. Use some sort of systems approach in order to make sound decisions, resulting in a learning process that is both efficient and effective.

  • How much training (time) should be allocated for a new technology? How do you estimate the appropriate amount (even generally) of resources that should be spent on training non-techs to use the new technology?

SQL - how to create multi row results with no source table

In standard SQL, is there a way to say:

select mubmle as x from mumblemmble

And get more than one line of results, like this

 x
 _ 
 1
 2
 3

without creating temporary tables? I can do it in SQL Server using row_count() if I know some table that has enough rows, like:

  select row_number() over (order by x.SomeColumn) from 
(select top 24 SomeColumn from TableThatHasAtLeast24Rows) x

But wonder if there's a standard (less dumb) way to do it.

From stackoverflow
  • There is no standard way, and no way at all in MySQL.

    In Oracle:

    SELECT  *
    FROM    dual
    CONNECT BY
            level < n
    

    In MS SQL:

    WITH hier(row) AS
            (
            SELECT  1
            UNION ALL
            SELECT  row + 1
            FROM    hier
            WHERE   row < n
            )
    SELECT  *
    FROM    hier
    OPTION (MAXRECURSION 0)
    

    In PostgreSQL:

    SELECT  *
    FROM    generate_series (1, n)
    

    Note that MS SQL, unlike Oracle, cannot swap recursion stack into temporary tablespace, so you may experience troubles when generating large datasets.

    See this answer for more details

    gbn : For MS SQL, you can use the option MAXRECURSION to change it from to may 32767 or infinite (0)
    Quassnoi : Nice point, adding.
  • The SQL 2003 standard defines a way to do it - not all DBMS implement it, though:

    <table value constructor> ::= VALUES <row value expression list>
    
    <row value expression list> ::= <table row value expression>
                                [ { <comma> <table row value expression> }... ]
    
    <row value expression> ::= 
             <row value special case>
         |   <explicit row value constructor>
    
    <table row value expression> ::= 
             <row value special case>
         |   <row value constructor>
    

    And, after wading through much other BNF, you can find:

    <explicit row value constructor> ::= 
             <left paren> <row value constructor element> <comma>
                          <row value constructor element list> <right paren>
         |   ROW <left paren> <row value constructor element list> <right paren>
         |   <row subquery>
    
    <row value constructor element list> ::= 
         <row value constructor element>
         [ { <comma> <row value constructor element> }... ]
    
    <row value constructor element> ::= <value expression>
    

    Which, when translated, means that in some contexts, you can use:

     VALUES (v1a, v1b, v1c), (v2a, v2b, v2c)
    

    to create a table value with two rows and three columns in each row. The INSERT statement is one place you can use the construct. Another is in the FROM clause of a SELECT statement, though showing enough BNF to connect the dots would take more space than SO encourages for an answer.

using a View in a Named Query

I have a named Query that uses a view. I am unable to create a class mapping because this view does not have a Id column. I created a named query and set it to return it as a class, defining all the return values. However, I still receive a KeyNotFound Exception. If I set all of the columns to It returns the List. How can you tell NHibernate to map this to a class.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <sql-query name="GetAvailablePermissions" read-only="true">
    <return alias="perm" class="Domain.AcsAvailablePermission, Domain">
      <return-property name="Id" column="PermissionId"/>
      <return-property name="Code" column="Code"/>
      <return-property name="Category" column="Category"/>
      <return-property name="Description" column="Description"/>      
    </return>
    <![CDATA[
    SELECT
      [PermissionId]
      , [Code]
      , [Category]
      , [Description]
    FROM [dbo].[vw_Permission]
    WHERE
      [SiteId] = :SiteId
    ]]>
  </sql-query>
</hibernate-mapping>
From stackoverflow
  • I did not find a NHibernate way so I changed everything to return-scalar. Then took the IList and converted it to a IList using LINQ.

Guard Clause and Exception handling for the same condition

I ran across the following snippet of code. Names have been changed to protect the innocent:

    public void RunProgram()
 {
  System.IO.FileInfo fInfo = new System.IO.FileInfo(Application.StartupPath + "Program.exe");

  if (!fInfo.Exists)
  {
   System.Windows.Forms.MessageBox.Show("Program could not be found, please verify your installation.\n\nDetails:\n" + fInfo.FullName);
            return;
  }

  try
  {
   System.Diagnostics.Process process = new System.Diagnostics.Process();
   System.Diagnostics.ProcessStartInfo pStart = new System.Diagnostics.ProcessStartInfo(); 
   pStart.FileName = fInfo.FullName;
   pStart.UseShellExecute = true;
   process.StartInfo = pStart;
   process.Start();
  }
  catch
  {
   System.Windows.Forms.MessageBox.Show(string.Format("An error occurred trying to run the program:{0}", fInfo.FullName));
  }
 }

I know there are a few things wrong here:

  • Exception types aren't being handled individually
  • Error message isn't informative enough

Rest assured I'll be addressing those as well but my main question is about the check for the file's existence just before the try/catch block. This strikes me as a bit redundant.

The point of exception handling is to catch unexpected conditions. I fully expect the file to be there so removing the existence check and just letting the exception handling catch it if it isn't seams a reasonable solution to me.

What do you think?

From stackoverflow
  • The file not being there is something that we can reasonably anticipate - and since we are at the UI level (I assume, since MessageBox), it makes reasonable sense to sanity check the request, and just tell the user directly and cancel.

    If we were deep in the bowels of the code (a few levels removed from the UI), then an exception would be the right thing - but I'd still probably check for file existance first and give a sensible error. Of course, any file existance check is immediately a thread race condition ;-p

    codeelegance : That's actually another thing I intend to fix. This is about two levels below the UI so the error handling should really be correcting handling what it can and rethrowing what it can't instead of directly prompting the user.
  • I vote to remove the .Exists() check.

    As for handling the exception types, my normal mode is to start with a generic exception handler to catch everything, but also make sure those exceptions are logged. Then break that apart to handle different exception based on the logs.

  • It is redundant and may give a false sense of security. Please note that you could still get a FileNotFound exception due to concurrency.

    Also see: Is there a case where parameter validation may be considered redundant?

  • I always try to avoid exceptions. This is generally because I tend to run visual studio with break on exceptions enabled, so try to avoid exceptions as much as possible.

    I've also spent a significant amount of time working on embedded systems where throwing an exception was expensive. This may not be the case for C#.

problem with running Ajax Control Toolkit

Hello all, When I run any Ajax control, i am getting the following error:This was written in .net 2.0 and running at .net 3.5.

please help me what is the setting i need to change.

"Error 32 Could not load file or assembly 'System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. D:\DotNet\AJAX\Ajax video\HDI-ACT-Accordion-CS\web.config 30 "

Thanks, Masum

From stackoverflow
  • Looking at the error message, you might have a reference (a mention of) to System.Web.Extensions.dll in your web.config.

    Try and remove that line from web.config.

  • If the machine does not have the 2.0 version of the System.Web.Extensions.dll that could be your issue. You may need to re-download/install the .NET 2.0 version of the Ajax extensions from here:

    http://www.microsoft.com/downloads/details.aspx?FamilyID=ca9d90fa-e8c9-42e3-aa19-08e2c027f5d6&displaylang=en

  • You have said:

    This was written in .net 2.0 and running at .net 3.5.

    I assume that what you mean by this is that the site was originally written using ASP .Net 2.0 but that it was upgraded at some point to ASP .Net 3.5. If this is the case then it could be that you have an incorrect reference to version 1 of System.Web.Extensions. This can be remedied by follwoing the steps bellow:

    1) In web.config add a reference to System.Web.Extensions v3.5:

    <assemblies>
       <add assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </assemblies>
    

    2) In web.config add the following as a child of configuration:

    <configuration>
       <!--Some other config-->
       <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
             <assemblyIdentity name="System.Web.Extensions" publicKeyToken="31bf3856ad364e35"/>
       <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="3.5.0.0"/>
          </dependentAssembly>
       </assemblyBinding>
    </configuration>
    

    As brenden mentions you may also need the correct version of the Ajax Toolkit.

Can you use JavaScript as embedded resources with Asp.Net MVC?

In Asp.Net with Ajax I can include my Javascript files as embedded resources in my assembly and create references to the embedded scripts using the ScriptManager.

Is there a way to do the same with Asp.Net MVC?

From stackoverflow

Is Git only for source control?

Is it insane to integrate Git into the project that has nothing to do with source control.

I am thinking about data collector that regularly runs several commands on the client side and commit the output into the local git repository. If remote server is available it pushes the data in origin for further processing on the server side. In this scenario I will have compression, history and various delivery methods for free. What are the drawbacks?

From stackoverflow
  • "Git is a free & open source, distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

    Every Git clone is a full-fledged repository with complete history and full revision tracking capabilities, not dependent on network access or a central server. Branching and merging are fast and easy to do.

    Git is used for version control of files, much like tools such as Mercurial, Bazaar, Subversion, CVS, Perforce, and Visual SourceSafe. "

    drozzy : Haha, you said Perforce!
  • It's not insane, no. Git was originally designed to just be a version controlled file-system, which would be later used as the basis for a source control system; however, things didn't quite go that way and git became the source control system. That being said, it's still very much a file-system design, and there's no reason you couldn't use it for other purposes (in fact I was going to do something very similar myself, but the project never panned out).

  • It depends on what kind of data you want to process. If you're talking about small bits of data, then use a database instead, but if you want to track modifications to documents, then I don't see a problem building your system on top of something like Git.

  • no its not just for source control.

    people use it as a versioning system for all kinds of documents like manuscripts, speeches, etc...

    git doesn't have to be used with software source control, you can use it however you want

  • To me, it would seem easier to collect data in a database. In a database, the information is organized, easily accessible, and can be manipulated easily. Also, if you need accessible to people remotely, allow other domains access to the database.

    Whereas in Git, you're stuck storing everything into flatfiles. It is your job to then parse all that information, and if you have a lot of information, this can take a long time. However, databases are usually pretty quick about returning the exact information you want.

    altCognito : The difference is searchability and speed of access. While I love GIT, it's not a DB.
    Jonathan Leffler : It depends on whether the database is set up to handle tracking the history of the system - assuming that is important, as it seems to be for the questionner. Also, synchronizing databases across systems is non-trivial, especially if they are sometimes disconnected.
    contagious : @Jonathan I've actually run into the problem of syncing db's. Getting everyone dumps of the same db is a pain, and i'm actually not aware of an auto-sync option.
  • There are already several backup systems available based on Git, for example Gibak. So it's definitely able to handle large datasets efficiently/secure.

    A drawback will be, that you won't be able to merge binary data.

  • Sounds like what you are looking for is a messaging queue or service bus of some kind. There are many of those. Of course, if Git works for you, why not? But just make sure you aren't just using it because it is the only tool like it that you know.

  • I can't see how this would be a bad idea. Git is great for managing large and small amounts of information. Binary or text are both allowed and kept revision history of.

    This could probably work just like iFolder, but with much better ability to merge, keep history and share your information.

    One of the problems is the complexity unerlying this kind of setup. Unless your users are well versed in git branching, conflict resolution by merging, and applying patches manually, you will have to make some tough decisions on how to dumb down the system.

    The idea of GUID is also going to be confusing to end users, so you might have to build something simple (version numbering) on top of that. The no-empty-folders allowed dilemma is also one you will have to address.

    The good thing about git (as opposed to svn or iFolder) is that moving a file is easy, and merges are smart.

    Overall, git does good managing our binary image, media and code files all in one repository. I can't think of why it could not be used as a helper for any other kind of project, to keep track of documents, images and other media.

  • I use Subversion for database backups. One row per record with many static records makes svn a quick and easy backup solution. No meaningful drawbacks that I can see. From one random Internet guy to another: you have my support!

Monitoring WLAN Radio Connection in Windows Mobile 6/C#

I am currently developing an app targeted for the HP IPAQ 210. Part of this app requires the WLAN radio to be enabled/powered on to connect to a pre-configured access point. I'm currently using the IPAQ SDK (via P/Invoke) to enable the WLAN radio, but I'm having trouble reliably determining when the radio has established a connection with the preferred access point. I'm currently monitoring the Microsoft.WindowsMobile.Status.SystemState.WiFiStateConnected property, but I would prefer to subscribe to an event to be notified when the connection is established.

I've looked around a bit in the OpenNETCF library, and there seems to be promising things in 2.3, but we're stuck on 2.2 for the moment.

Is there a reliable way to determine the connection status?

From stackoverflow
  • It is ugly, and it is no event, but if all else fails, you could try and check the Wifi hardware state by reading it's registry key:

    int key = (int)Registry.GetValue("HKEY_LOCAL_MACHINE\\System\\State\\Hardware", "WiFi", -1);
    
    bjanaszek : Just to add to this: the value will contain the following bit masked properties: 2 - WiFiStatePowerOn 8 - WiFiStateConnecting 16 - WiFiStateConnected Note that these are standard on WM5, but undocumented for WM6 (but they seem to work). Also, the SystemState wrappers in .NET seem to be unreliable.
    Sam : Yep. And 4 is "Networks available". 1 Seems to be set all the time, maybe it stands for "wifi hardware available"?
  • So, in case anyone else happens upon this, I've found the registry key method described above to mostly reliable, but I needed a more reliable method. I've moved to using the OpenNETCF 2.2 NetworkInformation library to monitor the CurrentIPAddress property of the WirelessZeroConfigInterface. I'm still using the IPAQUtils for managing the WLAN radio power (I've found the OpenNETCF 2.2 radio control to be lacking and the device will only have a single WiFi network entry), but here's how I monitor the IP Address of the interface:

    NetworkInterface[] netIntfs = NetworkInterface.GetAllNetworkInterfaces();
    foreach (NetworkInterface ni in netIntfs)
    {
        if (ni is WirelessZeroConfigNetworkInterface)
        {
           wzcni = (WirelessZeroConfigNetworkInterface)ni;
        }
    }
    
    while (wzcni.CurrentIpAddress.ToString() == "0.0.0.0" && tryCount < 10)
    {
        wzcni.Refresh();
        System.Threading.Thread.Sleep(3000);
        tryCount++;
    }
    

Is Developing Maven war apps in MyEclipse worth it?

My organization has made an upper level decision to move to maven as the standard build tool for Java projects. I have been tasked with helping our local teams migrate projects over to maven.

One of the core tools that is in play is the MyEclipse IDE. MyEclipse seems to have had a "fun" history with the maven team as evidenced in various places, especially when dealing with war projects. I haven't encountered problems with simple jar projects...yet.

After fighting with MyEclipse and failing to make it recognize a war project easily, the question becomes, is MyEclipse worth it for developing maven war apps?

If so, is there a way to get MyEclipse to play nicely with a war project that I've not found? Or, am I simply better off suggesting its time to use Eclipse JEE edition with m2eclipse?

From stackoverflow
  • No. MyEclipse does not support projects that were created outside of it. It is by design only working with projects created using its wizards.

    Mike Cornell : Yet it has Maven integration...or so they say.
    matt b : Given this, I'd ask someone to argue why MyEclipse is better than Eclipse
    Mike Cornell : I agree Matt, but this is a politically sensitive issue, which is why I guess I'm looking to gather some pros and cons that aren't the MyEclipse and maven guys pointing fingers at each other. FWIW, one of the political solutions is not to use Eclipse (or MyEclipse) but to use WSAD, which probably can't handle the m2eclipse plugin properly.
  • Mike,

    Sorry to hear you are fighting MyEclipse and Maven, in the past the most common problem I've seen causing people pain in this area is when they don't have the Web Root, Java source dirs or resource dirs set correctly.

    Using this webpage as reference for a standard Maven2 web project layout, you can easily create a Maven-enabled MyEclipse Web Project. The steps you would want to take are as follows:

    1. File > New > Web Project
    2. Give your project a name, use the Java source dir of "src/main/java" and a Web Root of "src/main/webapp", check the Java EE spec level you want, check "Add Maven support" checkbox and hit Finish (unless you want to setup the artifact/group IDs).
    3. Now, if this is the first time using Maven4MyEclipse, a lot of initialization will take place preparing your local repository and grabbing all the Java EE resources to build your project, but after that's done you should be all set.
    4. You can execute the Maven targets off the Right-click Run As or Debug As menu and even manage custom goal execution using the "Maven build..." shortcut -- this is all similar to m2eclipse.
    5. If you decide you want to use m2eclipse complete, you can navigate to Window > Preferences > MyEclipse > Maven4MyEclipse and check the "Enable all m2eclipse features" checkbox.

    Out of the box we only hide the bits that can make Maven confusing for first-time folks, if you enable all the m2 bits, you can do whatever you want with Maven and MyEclipse. If you keep having trouble stop by our forums and let us know and we'll help out as best we can.

    Mike Cornell : I think the issue is that the project has already been created outside of MyEclipse and worked thru maven using the command line. Importing it is not successful.
  • Mike,

    think the issue is that the project has already been created outside of MyEclipse and >worked thru maven using the command line. Importing it is not successful.

    I saw this post when looking for other Maven resources so I'll chime in.

    Why not import your existing project using File > Import and turn on all the m2eclipse features and continue to develop it as you did before using maven commandline tools? I've done this and it works well for legacy projects, once m2eclipse features are enabled as mentioned in Riyad's #5.

    Personally, I've always thought the maven war format was a bust, but we still have some old projects that use it around. Maven's format is just a default (and a poor one), not some sort of standard. However, we currently leave those old projects "as is" (using the above technique) just because it's easy. But for new work we use the MyEclipse web projects then just enable Maven support on them. The benefit is that you get all the Maven support and it's super easy to use and manage but no more commandline (although that still works too) and all the MyEclipse tools work perfectly on them as well. It's a "best of both" approach, well, at least for us. YMMV.

    Hope that helps, Dave

    Mike Cornell : Interesting, what happens when you have developers who are not using MyEclipse?
  • Mike,

    Interesting, what happens when you have developers who are not using MyEclipse?

    Not an issue here; I work for a very large company that has been standardized on it for quite some time (happily, I might add as we used to be a WSAD shop -- shudder.)

    Anyway, if you have some that use MyEclipse and some that don't I see two options. First, if you create MyEclipse web projects and then "Add Maven capabilities" (or whatever it's called) to them, they'll work in MyEclipse and from the Maven commandline as well. So even if you're not using MyEclipse you can still use the commandline Maven tools. Also, since the MyEclipse structure is the more standard "exploded war" layout, it should work with whatever else you use as well.

    Other thing to consider is that Maven's web layout is simply a default and Maven can easily support any project structure, including the one MyEclipse uses, so you should be able to use the MyEclipse project with Maven in any tool with just a little additional config. That's likely why the Maven commandline tools still work on the MyEclipse Web projects -- the MyEclipse guys just automatically configure Maven to recognize the format.

    You also could just import the externally created Maven web projects as I said in my last post. We don't like to do that because that structure is unique to Maven and just doesn't work with any tools except Maven. As a result, it basically defeats the the tool support you get automatically in MyEclipse, Eclipse Java EE, or pretty much any other tool. It's just a poor default. Exploded WAR format, that's used by MyEclipse, Eclipse Java EE, WSAD, RAD, and everyone else is simply a better solution. Especially when it still works with Maven just fine as well.

    Maven was made to be flexible to project structure. We've just found by using that flexibility a little you can get Maven support and great tool support too.

    Dave

    Mike Cornell : I understand where you're going with this, but both Eclipse EE and NetBeans handle maven war projects without breaking a sweat, it seems that MyEclipse is the one forcing a different layout. I think perhaps I asked the wrong question.
  • I am working as a tech lead, and we recently started moving to maven. I had a couple of issues getting maven to work with myeclipse. First, even when I "Enabled all m2eclipse features" checkbox, I still couldn't check out a project as a maven project, from subversion. That option (that you get from m2eclipse) just wasn't available.

    Also, some of the preferences you get with m2eclipse are not available with maven4myeclipse.

    Finally, I couldn't just uninstall the maven4eclipse plugin and install m2eclipse. That would have been an acceptable workaround.

    I think Genutec tried to make Maven more accesible to newbies, but there are some problems in the impementation, and I don't see them being fixed soon. For us, that will likely delegate MyEclipse to being just a fancy jsp editor.

Does a table with a surrogate key require a unique constraint on a natural key to be in 1NF?

The pragmatists have won the argument of surrogate vs. natural primary keys in favor of surrogate keys*. In my own work I always use SQL Server identity columns without a second thought. But it occurs to me that, for a table to be in 1st normal form, I should be able to identify a natural key and enforce it with a unique constraint. I can't do this for all the tables in my database, so my database doesn't even meet the lowest criteria of normalization.

Do you agree that a table with a surrogate primary key must also have a unique constraint on a natural key in order to be in 1NF?

*I think Joe Celko is still fighting the good fight, see the last paragraph.

Edited to add: Thanks for the responses. My impression is that adding a unique constraint is not a common practice, so I'm somewhat surprised that the responses so far have been unanimous.

From stackoverflow
  • Yes!

    If the table is supposed to record at most instance of the natural key, you need a constraint on the relevant column(s) to enforce that. Otherwise, you can end up with a table of 50,000,000 rows, each with a different ID value but otherwise identical -- which is pathetic or ludicrous or a travesty, depending on your viewpoint.

  • I could fill a book with all of the issues that IDENTITY columns have caused for my clients. On the bright side, it keeps me working. If everyone designed their databases the right way the first time I'd never have to come in and fix them :)

    If you're going to use an IDENTITY for a surrogate key, you need to do exactly that. A surrogate key should be completely behind the scenes. Users should never see them on reports, they should never be able to look up rows with them, etc. In addition, you should have a natural key, which by definition must be unique. That uniqueness should be enforced in your data model.

    My current client has decided that it's going to merge several entities in their database. They of course used IDENTITY columns everywhere and displayed them on all of the records on the front end. Now that the rows have to be merged there are collisions which means renumbering. Of course, since the users know these numbers when an entity gets renumbered it will completely throw the user for a loop.

    Nick DeVore : I disagree with the "never see them on reports...never be able to look up rows with them." In our system, we use an identity column for the requisitions. The "Req ID" is the identity column value. Now, our purchase orders use an incrementing number that we control, but the Req ID has proved quite useful and poses no issues for us.
    Tom H. : The fact that something worked in one situation and hasn't caused a problem *yet* doesn't mean that it's a good idea.
  • If you have a natural key and you don't place a unique constraint on it, you are going to create problems in your data integrity. Unfortunately in the real world, not every table has a genuine natural key.

  • Let me reframe the question. The interesting point isn't whether the table is or is not in 1NF. The interesting point is whether the natural data is in 1NF.

    If you remove the ID column, and then wipe out the duplicates, you've performed what's called a "projection" in relational speak. The result is in 1NF by definition.

    If you remove the ID column, but don't wipe out duplicates, you end up with a bag of rows, not a set. If the application somehow doesn't prevent duplicates from getting into that bag, then the bag is not in 1NF.

    My answer boils down to the same as Jonathan's and Tom's. The net effect is that you've moved the defense of data integrity back out of the DBMS and back into the application. Application programmers tend to do this, in this case and in many others. If they code their applications right, then it's a matter of preference. If they don't, then they've regressed to the state of the art before databases were introduced.

SQL testing best practice

Hi, What is the best practice for SQL testing (stored procedures, views, queries, triggers...) excluding automated testing? Most of the time a query works or returns thousands of rows, so it is a bit difficult to check them. I want to know how you usually do the testing of a SQL query. Can you point me to a good (online) reference?

From stackoverflow
  • Check out DBUnit.

    John Sansom : May seem like a daft questions but have you actually ever used DBUnit?
    Otávio Décio : Sure did, it is perfect for loading a baseline from XML.
  • Try to eliminate extra variables.

    Ways to do this:

    • Each test should test only one feature (a view, a stored procedure, etc.)
    • Use known test data (it should be from a real environment (but not actually the real environment)
    • Use the smallest amount of test data that adequately tests the feature.
  • This is not quite the same question but has many of the answers you are looking for. http://stackoverflow.com/questions/147362/what-is-the-best-way-to-test-a-stored-procedure

  • For inserts, updates, deletes I check the state of the table before and after the proc is run for records which meet the conditions of the inseret update or delete. So if I am adding 7 records, they shouldn't be in the table beforehand and should be there after.

    Selects with thousands of records can be harder. There is no substitute for actaully knowing your data and what you would expect to get. For instance, I know that a certain client has around 2000 sales reps. If I run a query that should have all the reps on it, and there are only about 1000 of them, I know something is wrong. Sometimes I will place ther resutls of a query into a temp table, so I can run statistics on it. If I'm doing an attendee report I can then see that there are 200 distinct meetings in the report inthe time period according to my query. If I look at just that table and see there are 350 meetings in the same time period, I go looking to see what is excluding meetings and usually look at the details of one or more of the excluded meetings and it's related tables to see why it isn't showing up. Usually you will find a stauts that needs to accounted for or bad data of some sort.

    I also look for record that are duplicated. If I expect one record per meeting and the same meeting is in there twice, I know that one of the join tables has more records than I was expecting for the conditions of the query.

    I often ask some of our operations people to look at the results of a query for a report as well. Because they are closer to the use of the data than I am, they will often spot things I don't.

    Another technique is to deliberately limit the where clasue for testing to a smaller subset of data that you can actually manaully examine to see if what you would expect is what you got. This is particularly helpful if you have lots of calculations or complex calulations. Anytime I do a complicated calculation I look at the raw data for one typical case and manually calculate the formula fromthe raw data, then I can check to see if it is correct in my query.

    Triggers I test by writing them as regular queries first (after first creating and poplatung the temp tables for #inserted and #deleted). I make sure to add multiple records to my temp tables because every trigger must be able to correctly handle multiple record inserts/updates or deletes. THen I write code to show the before state and the after state and put it all in a transaction so I can roll it back while testing.

What is the best source for a clear explanation of how to use the Python help?

A couple of days ago Bendin was kind enough to answer a question I had on how to handle some files that were uuencoded. When I described my response to his solution he went even further and showed me how to use a special function from the codecs module to better meet my needs.

import codecs
uudecode=codecs.getdecoder("uu")
decoded_Graphic,m=uudecode('mystring_that_I_need_decoded)

This was great and got me to the finish line. My problem is that I don't know how to do this on my own. I have slowly been trying to understand the help system so I went to the command line and typed

help(codecs)

I read what was there and while it looked important I could make no connections to the code that Bendin wrote. I then tried

help(codecs.getdecoder)

same problem

it looks to me that uudecode is a function that returns two values, one is a string that is the decoded file, the other is something else. Now I can figure out what that something else is because I can simply decode a file, look at the type and get the value (type-integer, value is 8809)

I had a thought as I was trying to work through this and did a

help(uudecode)

That was helpful so now I know that I can get help by calling help on something equal to the result of a function call (is that the right terminology) and get more explanation. But gosh this was a long road to this point.

I have almost every Python book and I use their indexes extensively but I still have not found the piece that I think is missing. Most of the book are written to a programming audience. But Python is so powerful and is some ways so intuitive that I think it should be installed on every desktop of anyone doing data organization and analysis for research. The problem is that there is not a manual that brings the higher level concepts to the reach of someone unschooled in the foundation that comes maybe from the weed out classes for a CS or similar degree.

So what is my question. Is there an intuitive clear explanation of the higher level things one needs to know to better understand Python and its help system? I am guessing it would be related to another language since I can't seem to find it in any of the Python books I have. I am not belaboring any of the books. They all have been helpful I just feel like something is missing and I don't have the time to take a CS curriculum and I don't want to learn C so I can better learn Python.

From stackoverflow
  • I never use the help() function. I find it annoying to use. When I want to know more about something I browse or search the Python Library References. It offers you all the things you get with help() and much, much more. When the library reference fails, use Google (or Stack Overflow :-)

    PyNEwbie : The library reference was also written for someone with a much more sophisticated background. It is helpful but I still feel like there is a missing piece I need to better understand what is written in the reference. I went to the reference after looking at Bendin's comment and I was lost.
  • "Is there an intuitive clear explanation of the higher level things one needs to know to better understand Python and its help system?"

    These are two separate things.

    1. For folks new to computing -- i.e., not professional programmers -- I tried to address this in http://homepage.mac.com/s_lott/books/nonprogrammer.html. The idea is to introduce someone to programming.

    2. The help command simply coughs out the documentation that's part of the Python source. It's often short-hand, and meant for people who already know Python. It's not a tutorial and was not meant to be a tutorial. Indeed, it can't be a tutorial because 80% of people only need a reference.

    If you're looking for a good Python tutorial, start with this search: http://stackoverflow.com/search?q=[python]+tutorial

    Specifically, the answers to http://stackoverflow.com/questions/3088/best-ways-to-teach-a-beginner-to-program might be helpful.