Sunday, May 1, 2011

How to automatically reload a python file when it is changed

Hi guys, If I make some changes to one of the files belonging to a running app, is there a way to tell the python runtime to automatically reload the module/file?

Cheers Naren

From stackoverflow

Fatal Error while creating a shell which should connect to the database

Fatal error: Call to undefined function mysql_connect() in
/opt/lampp/htdocs/cake/cake/libs/model/datasources/dbo/dbo_mysql.php on line 370

//shell
<?php

class ReportShell extends Shell
{
    var $uses = array('Customer');
    function main()
    {
            $customers = $this->Customer->find('all');
            var_dump($customers);
            $this->out('lol my first CakePHP shell is baked');
    }
}

?>

//model
<?php

class Customer extends AppModel
{
    var $name = "Customer";
    var $useTable = "customer";
    var $hasMany = array
    ('CustomerPrice',
     'Order' => array('foreignKey' => 'customer_id'));

    // Validation settings
    var $validate = array(
            'customer_id' => array(
                    'rule' => 'numeric',
                    'message' => 'A customer ID should be numeric !'
            ),
            'customer_number' => array(
                    'rule' => 'numeric',
                    'message' => 'A customer number should be numeric !'
            ),
            'customer_name' => array(
                    'rule' => 'notEmpty',
                    'message' => 'A customer number should be numeric !'
            )
    );


    /**
     * Function to retrieve Customer data
     *
     * @param string $response_type - CakePHP find() type
     * @param array $response_setting - CakePHP find() parameter array (set$
     * @param int $customer_id - Find customer by customer ID
     * @param int $customer_number - Find customer by customer number
     * @param string $customer_name - Find customer by customer name
     * @param int $customer_billing_month - Find customer by customer billi$
     * @param int $customer_start_date - Find customer by start date
     * @param int $customer_end_date - Find customer by end date
     * @param string $customer_email - Find customer by customer e-mail
     */
    function FjmCustomerGetCustomer($response_type, $response_setting = arr$

            /* Conditions: Find Customer by customer data */
            if($customer_id)
            {       $response = $this->Customer->findByCustomerId($response$
            if($customer_number)
            {       $response = $this->Customer->findByCustomerNumber($resp$
            if($customer_name)
            {       $response = $this->Customer->findByCustomerName($respon$
            if($customer_billing_month)
            {       $response = $this->Customer->findByCustomerBillingMonth$
            if($customer_start_date)
            {       $response = $this->Customer->findByCustomerStartDate($r$
            if($customer_end_date)
            {       $response = $this->Customer->findByCustomerEndDate($res$
            if($customer_email)
            {       $response = $this->Customer->findByCustomerEmail($respo$

            if(!$customer_id || !$customer_number || !$customer_name || !$c$
            {       $response = $this->Customer->find('all', $response_sett$

            return $response;
    }

    /* FjmCustomerGetCustomerByProduct
     * FjmCustomerGetCustomerByOrder
     * FjmCustomerGetCustomerByBill
     * FjmCustomerGetCustomerByHandle
     */
}

?>

//database config php
    var $default = array(
            'driver' => 'mysql',
            'persistent' => false,
            'host' => 'localhost',
            'login' => 'root',
            'password' => 'pwd',
            'database' => 'fjm_pps',
            'prefix' => 'fjm_',
    );
From stackoverflow
  • The answer is that the extention for mysql in php cli was not activated or installed

  • "Fatal error: Call to undefined function mysql_connect()" means that you don't have the MySQL libraries included in your PHP installation.

    Check your php.ini and be sure that the mysql extension is activated. The "extension=mysql.so" (or "extension=php_mysql.dll") needs to be uncommented.

    Google around for help on getting MySQL libraries included into your PHP install.

  • Also note that on some installations (debian comes mind), if not all, cli-php and apache-php have separate php.ini:s. So having mysql enabled in one doesnt necessarily help with the other. Use phpinfo() to find out where it is looking for php.ini from.

    Gos : yes probably most of the linux distro's have a differend php.ini

Updating a Web Application in IIS - Best practices

What are the best practices for updating a web application in IIS?

The first page you see when you visit our application is a login page.

What I want to achieve is that visitors be redirected to a page stating that the application is being updated. And for users with an admin role being able to login successfully (to check whether everything is working properly)

In web.config we keep track of wheter the application is being updated (updating = [true|false] and then on the authentication_event:

if (updating) 
{

   if (User.IsInRole("admin"))

   {

      redirect to main web app...

   }
   else
   {     

     redirect to web being updated page....

   }

}
else 
{

   redirect to main web app..

}

Any advise will be appreciated immensely..

From stackoverflow
  • Looks like you pretty much have it figured out.

    if (WebConfigurationManager.AppSettings["updating"]=="true") 
       if (User.IsInRole("admin"))
           Response.Redirect("~/Main.aspx");
       else
           Response.Redirect("~/Updating.aspx");
    else
       Response.Redirect("~/Main.aspx");
    
  • instead of doing it via web.config, you should be saving the updating flag in some other xml file or database, as each time you update your web.config file, your application restarts, thus invalidating current application variables, caches, etc etc

    apart from that, you got the other logic right. but avoid touching the web.config as far as possible - i personally only save the connection string in the web.config as that hardly changes

    but rest of the key value pairs which change often, i have them saved in sql database table. so that way i never ever have to do an application restart unless my connection string changes :-)

    JoshJordan : Application Settings are made for just that sort of thing.
    Raj : depends. i do a lot of busy e commerce sites where at any point of time there are 100s of users busy shopping. i cant reset my entire app and loose them just to change the value of 1 app setting :-)
    Raj : instead, what i do is in the admin section provide a form to site admin to update appSettings - and then just repopulate / refresh my class which holds these values - saved in application variable
    JoshJordan : ASP.NET already has that, using the ASP.NET Configuration Settings wizard. You can run this on a prod machine without writing any code. The whole point of Application Settings is not to have to restart :)
    BigBlondeViking : you can edit the appSettings causing an app restart on edit in the machineConfig [ restartOnExternalChanges="false" ]
  • I test everything locally, then I use the built in app_offline.html file on production server. When this file is present it will be served to clients, in meantime I am uploading new content. When done, renaming app_offline.html to something different and the new app starts.

    Dillie-O : +10 I wish I could upvote this more. App_Offline is QUICK and EASY and I LOVE IT!!!! You can even leave the file sitting on the web server as app_offline_disabled.html when not in use. It saved me so many hassles I used to have to try. Just follow these guidelines and you'll be set. http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx
    Dillie-O : Quick note, it is app_offline.htm....not .html

How to test random numbers?

i have written in matlab, a program, which is supossed to generate random numbers between 0 and 1. i have test it only with the runstest in matlab, and te result is that the sequence is random. i have seen the histograms too, and they have a beta distribution. i want to test this rng whith other test, such as diehard, ent, or nist, but i don't know how. can someone explain how to use them, or suggest me some other randomness tests. thank you

From stackoverflow
  • Confine the result to a specific range (possibly using the mod operator), run your code a few million times and count how many times you see each number in the range. Make sure the counts are roughly the same, and that you don't have a bias for any specific values.

    Joey : That's kind of a monobit test. But still very short of a real test suite like the ones he mentioned (ok, ent doesn't count).
    dmckee : Good start. Next up is to generate ordered pairs and see that they are evenly distributed in the plane, then ordered triples...and you still aren't "really" doing it...
  • Here you can find diehard test programs and source code for different operating systems. Another nice link could be this one.

  • With most tests you can supply a large file of random numbers (integer or floating point) and run various tests on that sample file. DIEHARD worked that way, if I remember correctly and some others do, too. If you really want to see your generator fail, you could try using TestU01 by Pierre L'Ecuyer which has enough tests in it to let nearly every generator fail at least one test :-)

    Still, for most test suites there is extensive documentation, at least I know this for DIEHARD, the test suite from NIST SP 800-22 as well as DieHarder and TestU01 (links go to the docs). The methods for supplying random numbers to test are usually different but mentioned in the respective documentation.

  • Do you actually need to write your own random number generator? RNGs are notoriously difficult to get right, largely due to the difficulties in testing them.

    Chris Lively : Most likely this is a school assignment that he needs some help with.
    Joey : Unlikely. (a) most schools probably won't use Matlab, (b) PRNGs are something many students not even have a firm grasp of and (c) s?he mentioned some test suites that are beyond normal school stuff.
    Joey : And yep, s?he does, see comment on the question itself.
  • The route that I would probably go would be to do a visual analysis of the results. The code for this is simple enough, as shown in the following psudo-code based upon this article.

    1. Create an image of size x by y
    2. For ndx = 0 to x
      3. For ndy = 0 to y
        4. Let random be a random number between 0 and 1
        5. If random = 1, set the image point at ndx, ndy as black
    6. Display the generated image
    

    Also, Random.org has more information on the statistical analysis of algorithms, but they also use the aforementioned article as their example of visual analysis.

  • There are many things to test if you want to test your RNG on your own. Here are a few basic features that may reveal your number sequence to be not truly random or maybe indistinguishable from random?

    Take a look at:

    1. The distribution - you have already done some analysis on your distribution. You want each possible number to have the same probability of occurring.

    2. Cyclic behavior - does the same sequence repeat itself over and over again? The repetitive sequence may be quite long.

    3. Occurence of duplicates (...C B B A F F...) , triplets (...C B A A A F...) etc. Statistically in a sequence of random numbers you have a certain probability of dulplicates (the same number generated twice in a row), triplets etc. Calculate this probability and check if your sequence of pseudo random numbers has the same probability of duplicates occurring?

    Note that for most of these tests you need to have a quite long sequences of random numbers in order to be able to get sensible and accurate results from statistical analysis.

    I assumed peudo random number sequences of integers, which is easily fixed by multiplying your [0, 1] numbers by an appropriate constant.

  • alt text

Is it possible to gets sql connection leaks using LINQ?

I belived it was not possible to get sql connection leaks when using LINQ, but perfmon tracing of NumberOfReclaimedConnections shows a high number and on high load we sometimes get exceptions like "Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached".

We do not use Dispose on the datacontexts, sincewe used defered loading. Several articles and blogpost tells me that this should not be a problem.

Still we gets these exceptions sometimes. But it can not be that every linq query we do keep the connection open, then we would have a lot more of the exceptions.

Edited

The application is a WCF service.

If you look at the documentation of Linq and most of the articles, they claim that the Dispose is not necessary to release the connections. They claim that DataCOntext only keep the connection open for the short time it need it.

From stackoverflow
  • Are you getting any deadlocks in your database? A quick look at the Activity Monitor should give you some indication.

    What do you do to manage DataContext lifecycle - what sort of application have you written (a website, a Windows Client, other)?

    Once used in a query or operation, a DataContext will keep a connection so that entities loaded can lazy load & etc, so it's imperative that you plan how you use DataContexts in your application.

    WCF services.. In that case, I am a big fan of the "one context per request" approach. I'd encourage you to wrap your data operations within a using() statement so that the context is disposed when you're done.

    Atle : We have no trouble with deadlocks, at least not I as I know for the moment, and we are monitoring it. The application is WCF services, and a datacontext should never live longer than a service call.
  • When your DataContext is not disposed of and stays alive, the associated connection will stay alive too. Database connections are unmanaged resources and all unmanaged resources must be disposed of properly.

    Even if you use delay-loading and do not have a well-defined scope, you should still clean up database connections at the end of a logical unit of work. In ASP.NET apps, the latest possible moment for this would be at the end of request processing - in the Application_EndRequest method of the Globals.asax file. In a WCF service, any active data context should be disposed of at the end of every service method call.

    The documentation for this vague and while most of the time, you can get away with not disposing your DataContext, there do appear to be some scenarios where the data loaded from a connection is keeping the connection itself alive. The easiest way to confirm that this is happening in your case is to test it.

    Ronald Wildenberg : Nice addition. Thanks :)
  • I found after some more searching I found this question and answer, where it says that linq can be fooled to leave a connection open..

    I made this small test code that reproduces it. If I just replaces the Enumerator with foreach it works fine, but he Enumerator keeps the connections open.

    public Organisation RunTestQuery2() { IEnumerable orgs = base.GetEntities().Take(5);

        var enumerator = orgs.GetEnumerator();
        int i = 0;
    
    
        while (enumerator.MoveNext())
        {
            var org = enumerator.Current;
            Debug.WriteLine(org.DescribingName);
            if (i == 3)
            {
               return org;
            }
            i++;
        }
    
        return null;
    }
    

    If I adds a call to dispose on the context, they dissappear.

What are the real benefits of Visual Studio Team System Database Edition (GDR)?

Interested if anyone has used VSTS Database Edition extensively and, if so, which features did you find the most useful over the standard Visual Studio database projects?

What are the most compelling features as opposed to alternative schema management options or tools like RedGate's SqlCompare etc?

Edit: Microsoft just released the RTM version of Database Edition (GDR) which adds support for SQL Server 2008 - link is here. I've previously blogged (briefly) about it here.

Has anyone had a chance to do any real work with the GDR? It looks like there are some real enhancements including refactoring support. I'd be really interested to hear if people are using it with SQL Server 2008...

Download From: [http://www.microsoft.com/downloads/details.aspx?FamilyID=bb3ad767-5f69-4db9-b1c9-8f55759846ed&displaylang=en]

From stackoverflow
  • You can do database versioning for one. That is useful.

    The other thing that is really useful is the ability to define type of seed data for testing. Through this Visual Studio will populate the database with random data and this is great for testing purposes.

    There are other benefits as well of course.

  • If you compare it to tool like RedGates, that are specifically taylored for SQL Server, the benefits are that if you have the proper MSDN subscription you do not have to spend more money for other tools (but keep in mind that RedGate tools are much more mature) and it covers some points (like regression tests and unit tests at the DB level) that other tools do not cover and it make so in a integrate manner with other testing tool of VSTS, so that you can record results in Team System.
    Compared to a tool like Embarcadero ErStudio (my solution of choice) it misses the cross database features, and this is a big problem, at least for me.
    If you are a "all Microsoft" shop with the proper MSDN subscription it could be worth spending time on it.

  • We use the database edition functionality of Team Suite on Stack Overflow. As Vaibhav said, mostly it is useful because it gives you a one-click way to reverse engineer a database into source control, and keep it up to date.

    Note that it also has decent Data and Schema compare tools as well. You can compare projects to physical databases and vice-versa. This makes it pretty easy to keep your database up to date, no matter where you make changes -- in the filesystem database project, or in the physical database itself.

  • The VSDB test integration is so painful to configure that we abandoned it, and that's the only thing it's got that Red-Gate doesn't.

    Red-Gate's tool is miles more useful. It does live DB and scripts in folders, but also has "snapshots." The aspect of Red-Gate SQL Compare that gives it the win is its Snapshot ability and the fact that your license allows you to deploy their assemblies and use them to perform database maintenance at customer run-time.

    It has made upgrades in the COTS application that I develop a breeze. A Snapshot is a binary schema representation. You can package them as resources in an assembly, then use the snapshot in a customer run-time schema compare to bring an existing database up to the current rev.

  • It is always useful to put everything under the same source control, so your data-dude can be shelving, checking in, compare with history, and even resolve workitems and bugs using the same tools that other team members are using.

    Also to be able to have one versionning mechanism across the whole application, in other words, it doesn't make sense to say that my source control has all the versions of my project while your database can't fit with any of these old versions, unless you take a backup or a snapshot of the database with each build.

  • Probably the best advantages are around being able to version control individual DB schema objects (which you could do with the older "Database Projects"), but have the power to "build"/deploy the project and convert those individual scripts into a complete database.

    The ability to import scripts and have the Wizard covert individual schema items into separate files is very handy if you've inherited a DB schema.

    Given that recently the licensing model changed, it makes it even more enticing because it's included with the Developer edition SKU. It also provised support for "Database Unit Tests" which might be useful.

    From the 2008 GDR, I understand that they now support SQL Server 2008.

  • We are currently using the GDR 2008 projects for managing our entire database development and deployment on a greenfield system. We use a TFS build script to call out to the MSBuild task for deploying the databases along with executing the data generation plans for pre populating the testing environment with data.

    The key with the data generation plans was finding the build task to use which is :

    TaskName="DataGeneratorTask"
    AssemblyName="Microsoft.Data.Schema.Tasks, Version=9.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
    

    All of this gdr project work has been unbelievably helpful and I think it is well worth the learning curve to get to know these project types. The value they provide is astronomical in productivity and visibility.

    It allows us all to view the entire system in a single visual studio solution along with allowing us to start with a clean slate of our system at any point in time with either a click of the deploy command or a custom build configuration.

    This blog will help with getting the TFSBuild script to run if you're interested.

hibernate DAO design

do i have to open and close session and transcation in each function (make object ,delete object ,findbyID)

can u give me a DAO implenetation for findall (lazy initialization ).

From stackoverflow
  • You should have a transaction for each complete business operation. I For instance: The operation includes selecting some values, updating it and inserting others. If each of the elementary operations create their own transaction, you will fail writing a multi-user application.

    You should create the session at the beginning of the business operation, create a transaction, then perform all the operations (you "functions") within that transaction, and commit or rollback them all together.

    Transactions are defined in the business layer.