Thursday, March 3, 2011

"select * from table" vs "select colA,colB,etc from table" interesting behaviour in SqlServer2005

Apology for a lengthy post but I needed to post some code to illustrate the problem.

Inspired by the question What is the reason not to use select * ? posted a few minutes ago, I decided to point out some observations of the select * behaviour that I noticed some time ago.

So let's the code speak for itself:

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[starTest]') AND type in (N'U'))
DROP TABLE [dbo].[starTest]
CREATE TABLE [dbo].[starTest](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [A] [varchar](50) NULL,
    [B] [varchar](50) NULL,
    [C] [varchar](50) NULL
) ON [PRIMARY]

GO

insert into dbo.starTest(a,b,c)
select 'a1','b1','c1'
union all select 'a2','b2','c2'
union all select 'a3','b3','c3'

go
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[vStartest]'))
DROP VIEW [dbo].[vStartest]
go
create view dbo.vStartest as
select * from dbo.starTest
go

go
IF  EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[vExplicittest]'))
DROP VIEW [dbo].[vExplicittest]
go
create view dbo.[vExplicittest] as
select a,b,c from dbo.starTest
go


select a,b,c from dbo.vStartest
select a,b,c from dbo.vExplicitTest

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[starTest]') AND type in (N'U'))
DROP TABLE [dbo].[starTest]
CREATE TABLE [dbo].[starTest](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [A] [varchar](50) NULL,
    [B] [varchar](50) NULL,
    [D] [varchar](50) NULL,
    [C] [varchar](50) NULL
) ON [PRIMARY]

GO

insert into dbo.starTest(a,b,d,c)
select 'a1','b1','d1','c1'
union all select 'a2','b2','d2','c2'
union all select 'a3','b3','d3','c3'

select a,b,c from dbo.vExplicittest
select a,b,c from dbo.vStartest

If you execute the following query and look at the results of last 2 select statements, the results that you will see will be as follows:

select a,b,c from dbo.vExplicittest
a1  b1 c1
a2  b2 c2
a3  b3 c3

select a,b,c from dbo.vStartest
a1  b1 d1
a2  b2 d2
a3  b3 d3

As you can see in the results of select a,b,c from dbo.vStartest the data of column c has been replaced with the data from colum d.

I believe that is related to the way the views are compiled, my understanding is that the columns are mapped by column indexes (1,2,3,4) as apposed to names.

I though I would post it as a warning for people using select * in their sql and experiencing unexpected behaviour.

Note: If you rebuild the view that uses select * each time after you modify the table it will work as expected

From stackoverflow
  • sp_refreshview to fix the view, or use WITH SCHEMABINDING in the view definition

    If a view is not created with the SCHEMABINDING clause, sp_refreshview should be run when changes are made to the objects underlying the view that affect the definition of the view. Otherwise, the view might produce unexpected results when it is queried.

    kristof : I would add, do not use SELECT * in the production code
    gbn : Good point. I always use SCHEMABINDING which does not allow this
  • This is pretty standard behavior for views under any RDBMS, not just MSSQL, and the reason why the usage of views comprising "select * from" must be treated with caution.

    The SQL Engine will compile each view - which is basically the lexicographical/parsing steps and store the result of that. If you hence change the underlying tables a explicit recompile is always required unless the database has some method of tagging the view as to be checked in such circumstances.

    The issue may (will) also apply to stored procedures and similar database objects too.

LPCSTR, LPCTSTR and LPTSTR

What the difference between LPCSTR, LPCTSTR and LPTSTR?
Why do we need to do this to convert a string into a LV_ITEM structure variable pszText:

LV_DISPINFO dispinfo;  
dispinfo.item.pszText = LPTSTR((LPCTSTR)string);
From stackoverflow
  • quick and dirty:

    LP == long pointer. Just think pointer or char*

    C = const in this case i think they mean the character string is a const, not the pointer being const.

    STR is string

    the T is for a wide character or char (TCHAR) depending on compile options.

    jalf : T is not for wide character, it is for varying character type. W is for wide (as in WCHAR). If UNICODE is defined, TCHAR == WCHAR, otherwise TCHAR == CHAR. So if UNICODE is not defined, LPCTSTR == LPCSTR.
    Tim : that is why I wrote "depending on compile options"
    nXqd : I really love this type of explaining :) . Thanks so much
  • To answer the first part of your question:

    LPCSTR is a const string

    LPCTSTR is a const TCHAR string, (TCHAR being either a wide char or char depending on whether UNICODE is defined)

    LPTSTR is a (non-const) TCHAR string

    This is a great codeproject article describing C++ strings (see 2/3 the way down for a chart comparing the different types)

    nothingMaster : I quickly scanned that article - seems great, adding it to my bookmarks and will read it as soon as I have time.
  • Adding to John and Tim's answer.

    Unless you are coding on Win98, there are only two of the 6+ string types you should be using in your application

    • LPWSTR
    • LPCWSTR

    The rest are meant to support ANSI platforms or dual compiles. Those are not as relevant today as they used to be.

    BlueRaja - Danny Pflughoeft : What about std::string?
    JaredPar : @BlueRaja, I was mainly referring to C based strings in my answer. But for C++ I would avoid `std::string` because it is still an ASCII based string and prefer `std::wstring` instead.
  • Read MSDN where everything is explained , this will avoid noob questions

What do you use to develop for PowerShell?

I don't see a Visual Studio plugin for it (although I didn't look that hard, so I might have missed it), and searches turn up random third-party solutions, but is there something that comes with PowerShell or something that plugs into Visual Studio?

From stackoverflow
  • PowerShell v2 CTP has a nice GUI interface which includes the ability to run multiple runspaces, and edit and run scripts. The script editor has syntax highlighting which is very helpful. PSH v2 also has other useful features such as script cmdlets. It's very cool. PowerGUI is supposed to be pretty neat too although I haven't personally used it. Before PSH v2 thhough, I used to use Textpad with a syntax file that was created for the pre-release version of PowerShell - Monad.

    Thomas Owens : How do I tell what version of PS I have?
    Steven Murawski : From the PowerShell prompt type $host.version
    fatcat1111 : You can also get the version with 'get-host'.
  • Have you seen powershell.com which has a couple of excited reviews.

  • Visual Studio templates are available here. And there are samples in the Server 2008 SDK for developing providers and hosts.

    If you are talking about an IDE for scripting, see this question (How to Get Started with PowerShell)

  • The beauty of PS is the command line. To be honest with you, I do the vast majority of my development at the command line. With the PowerTab snapin from /\/\O\/\/, I have as good intellisense as I have found in any IDE for PS. Once I have all the parts worked out at the command line then I use VIM to put it all into a script.

  • PrimalScript by Sapien is good. It's pay though.

  • Depends on what you mean by "develop."

    PowerShell can be used quite effectively from the command line without scripting. However, if you do get into scripting, products include PowerShell Plus, PrimalScript, and PowerGUI.

    If you're developing cmdlets and snap-ins, Visual Studio doesn't require any plug-ins, although you do need the reference assemblies available in the Windows SDK.

    Visual Studio has no plans to host PowerShell as a script editor - the Visual Studio team feels that most admins wouldn't have Visual Studio, and that their goal is to make better development tools, not scripting tools (which is pretty distinct in terms of features that you need to be more efficient). So don't look for PowerShell scripting to show up in Visual Studio, at least not in the immediate future. There are some third-party stuff to get it in there, but nothing as good as the dedicated script editors I listed above.

  • I mostly use PowerGui Script Editor. It allows me to use multi-file editing and debugging as well as straight command-line stuff.

Is git with cvs faster than cvs alone?

My team works on a project in cvs containing about 20,000 Java files. Because of the number of files, it takes a while to do a cvs update. I typically keep about 5 copies of the entire tree checked out, to make it easy to check in different requests without worrying about which files were modified for each. It's a real pain to keep all 5 trees up to date and in sync with each other.

I've read that it's fairly easy to use git locally with a remote cvs server, and that git is fast. Will git significantly speed up the updating of my local trees?

I realize the lower bound is the time to do one cvs update. But I'm thinking that once the first tree is up to date, it might possible to quickly sync the other 4 with the first, rather than to do 4 more cvs update commands. Do I understand git correctly?

From stackoverflow
  • We do something similar at work. We basically use the master branch in git as a single, updated CVS version of the code; we don't do any development there, just CVS updates. Then, all of our development projects happen on feature branches that we rebase. When we do CVS updates on the master branch we commit those changes to master and then rebase our other development branches against master.

    It's not ideal -- it makes sharing branches with other people difficult. But, we can manage several development projects at once and do branches, merges, and diffs against them easily. And, we only interact with CVS on the one master branch, as needed.

    Craig P. Motlin : Sounds pretty good. Is the rebase step faster than the cvs up?
    Pat Notz : Yeah, it's a ton faster because it's all local. It basically generates diffs from your branch and applies them to the new head of the branch.
  • I use Git as a Subversion client on a large project (on the order of 10k files). Git is fast, really fast. It's so fast that I only keep one working clone, and switch between feature branches within that same clone. Like you, when I used Subversion I would have two or three similar checkouts and would switch between them regularly as I had multiple things in progress simultaneously. It got to be pretty confusing sometimes. With Git's features like lightweight branches, the stash, and "git add -p", I find that I no longer need multiple checkouts. I can do everything in one directory, and not worry as much about losing changes that I either forgot about or accidentally overwrote.

    I haven't used Git with CVS, but if its integration is anything like git-svn then it's going to be no problem.

What is the best way to do a wildcard search in sql server 2005?

So I have a stored procedure that accepts a product code like 1234567890. I want to facilitate a wildcard search option for those products. (i.e. 123456*) and have it return all those products that match. What is the best way to do this?

I have in the past used something like below:

*SELECT @product_code = REPLACE(@product_code, '*', '%')

and then do a LIKE search on the product_code field, but i feel like it can be improved.

From stackoverflow
  • What your doing already is about the best you can do.

    One optimization you might try is to ensure there's an index on the columns you're allowing this on. SQL Server will still need to do a full scan for the wildcard search, but it'll be only over the specific index rather than the full table.

    As always, checking the query plan before and after any changes is a great idea.

  • A couple of random ideas

    It depends, but you might like to consider:

    • Always look for a substring by default. e.g. if the user enters "1234", you search for:

      WHERE product like "%1234%"

    • Allow users full control. i.e. simply take their input and pass it to the LIKE clause. This means that they can come up with their own custom searches. This will only be useful if your users are interested in learning.

      WHERE product like @input

IE's getBoundingClientRect gives different answer in browser than in windows.forms.webbrowser - why?

Take the following html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <title>Basic Layout</title>
    <style type="text/css">
        html,body,div{font-family:Verdana}
        #Head{border-bottom:solid 10px #369}
        #Body{margin-left:200px;background-color:#def}
        #Body h2{float:left;margin-left:-200px;padding:10px;font-size:1em;}
        #Foot{border-top:dashed 1px #369}
        p{page-break-inside:avoid}
    </style>
    <script>
        document.onclick = function() {
            alert(document.getElementById("Body").getBoundingClientRect().left);
        }
    </script>
</head>
<body>
    <div id="Head">
        <h1>Basic Layout Header</h1>
    </div>
    <div id="Body">
        <h2>Body Header</h2>
        <p>
            Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Mauris pretium. Morbi
            nisi velit, lacinia vitae, porttitor nec, laoreet nec, neque. Fusce id dolor. Vivamus
            scelerisque libero. Fusce aliquet gravida dui. Integer vulputate bibendum pede.
            Donec fringilla porta sem. Donec justo pede, fermentum eu, mattis a, malesuada id,
            erat. Maecenas hendrerit justo vitae felis. Praesent ut purus. Pellentesque turpis
            tortor, lobortis sed, fringilla venenatis, mattis nec, ipsum. Nunc non diam. Maecenas
            gravida mollis quam. Aenean nec urna. Quisque gravida, nulla a vulputate mattis,
            mauris justo mollis leo, et pellentesque eros augue sit amet urna. Aliquam rhoncus
            erat in mauris. Nullam ut urna. Nam pharetra, purus ac ultricies consectetuer, est
            leo aliquam augue, vitae pulvinar dui mi scelerisque mi. Integer urna. Praesent
            risus libero, porttitor vel, auctor gravida, posuere vel, felis.
        </p>
    </div>
    <div id="Foot">
        <p>Footer</p>
    </div>
</body>
</html>

Load it into your browser and click anywhere - you get 210 this is the left value for the #Body div

However on a win form with a webbrowser control on it, navigate to the same html

    webBrowser1.Navigate(_Url);

Click again, and you get 10.

Why... why... why?

From stackoverflow

Why does tabbing to a DojoX grid result in a JavaScript "Can't move focus to control" error?

Problem
I've got a number of Dojo components on a page. When the user tries to tab from an input like component to a grid like component, I get a JavaScript "Can't move focus to control" error. The user base uses IE6.

Solution
The first element in the DojoX Grid layout cannot be hidden. If it is hidden, you get a a JavaScript "Can't move focus to control" error. To fix this, I added a row # that displays. See below.

  var gridLayout = [
    new dojox.grid.cells.RowIndex({ name: "row #", 
                                    width: 2, 
                                    styles: "text-align: right;"
                                 }),
    {
      field: "ele_id",
      name: "Element ID",
      styles: "text-align:right;",
      width:5,
      hidden:"true"           
    },
    {
      field: "ele_nm",
      name: "Element Name",
      styles: "text-align:left;",
      width:8          
    }
  ];
From stackoverflow
  • You have to handle the keydown event and listen for character 9 (which is the tab character). To invalidate the event you simply return false when the user presses character 9.

    function handleKeyDown(e)

    {

    var keynum;

    var keychar;

    var numcheck;

    if(window.event) // IE

    {

    keynum = e.keyCode;

    }

    else if(e.which) // Netscape/Firefox/Opera

    {

    keynum = e.which;

    }

    return keynum != 9; ` }`

    Bryan Oakley : You have to deal with shift-tab going in the opposite direction too.
  • <input name="z" onfocus="this.blur()"/>
    
    ARemesal : I think that can be very annoying for the user: he'll try and try again to focus the element, but the focus goes to another element always...
    Diodeus : I agree. I'm just trying to answer the question.
  • I would personally think this type of behavior is a bit annoying. Why are you prohibiting the user from focusing on that field?

    A better solution would be to hide/disable the field until it is ready to have data entered into it. You should also have some text to explain why the field is disabled.

    Edit: The error message you posted has some results on google, but it seems like it could be any number of issues, this one http://www.francoisfaubert.com/2008/03/06/cant-move-focus-to-control/ sounds like it could be a possibility. Check your HTML to see if there are other controls with the same ID on the page.

    happyappa : clarified the issue above. it's to prevent someone from tabbing from an input field to a grid component. what other ways would you suggest handling that?
    Bob : What is this "grid component"?
    happyappa : The Grid I'm using is a DojoX component.
    happyappa : Not an ID issue. Yeah, ineffectively googled, so I thought I'd give it a shot here.
  • You can set Input-Z as a disabled control. Then, when the user tabs into Input-Y and fill it, change Input-Z to enabled. How can you do it:

    <input id="Input-x" type="text" />
    <input id="Input-y" type="text" onChange="document.getElementById('Input_Z').removeAttribute('disabled');" />
    <input id="Input-z" type="text" disabled />
    
  • Preventing tabbing may disrupt partially sighted users who are browsing your site using a screenreader.

    happyappa : i clarified the issue above. thanks for pointing that out. what other ways could you handle something like this?
  • If input Y doesn't accept user input, then don't make it an input field!

    If you're just using this for value display, use a styled <span> or <div> instead.

    Chris : Agreed. There is no reason for a well designed app to have a requirement to disable the tab key. It's like asking "can I disable the back button?" You just don't do it. It's part of the expected standard.
    happyappa : i gave a bad example. the issue is tabbing from an input field to a grid component that can't handle it. i'm trying to prevent the JavaScript error from occurring.
  • Give the component element a tabindex attribute with the value -1

  • After your comments clarifying the issue, I understand you need that user can't focus an element with tab key. Try to add to the element:

    tabindex="-1"

    by example:

    <div id="mygrid" tabindex="-1"> <!-- Some stuff here --> </div>
    

    More information about negative tabindexes: introduction-to-wai-aria

    Edit: More information about ARIA: http://www.w3.org/WAI/intro/aria

  • Regarding the error message: From Fake's tank:

    "odds are you have conflicting ids in your document. Also, remember IE 7 and the previous versions are alone to think a name attribute should be treated like an id."

    So, firstly, this is an IE-specific bug. Secondly, go change your ids. It appears to be something that afflicted some HP html-based software before, and appeared when the users upgraded from IE6 to IE7.

    Regarding the question about disabling focus - just hide any inputs that are unwanted with type=hidden in the tag, and they will cease to be a problem. Never mess about with people's tabbing - it's the one thing that has improved with html over Windows apps.

    Morals of the tale:

    • Do It Properly
    • Microsoft Is Crap At Web Stuff
    • Don't Mess With Standard GUI Behaviour
    happyappa : Thx for pointing out the possibility, but not an id issue.
  • Solution
    The first element in the DojoX Grid layout cannot be hidden. If it is hidden, you get a a JavaScript "Can't move focus to control" error. To fix this, I added a row # that displays. See below.

      var gridLayout = [
        new dojox.grid.cells.RowIndex({ name: "row #", 
                                        width: 2, 
                                        styles: "text-align: right;"
                                     }),
        {
          field: "ele_id",
          name: "Element ID",
          styles: "text-align:right;",
          width:5,
          hidden:"true"           
        },
        {
          field: "ele_nm",
          name: "Element Name",
          styles: "text-align:left;",
          width:8          
        }
      ];
    

ASP.NET MVC Beta 1 - Will My Existing User Controls Work In My Views As-Is?

I understand that you can now create MVC-specific user controls, but will my existing standard ASCX user controls work in an MVC view?

I understand that MVC pages have a completely different lifecycle but will, for example, a Page_Load method be invoked as normal when the control is sitting in a view?

From stackoverflow
  • If your standard ASCX controls do not have control events. There is no viewstate in MVC so that'll have to change.

    The normal page lifecycle is still executed. E.g. Page load, init, prerender, etc. The main thing is viewstate.

  • You can instantiate pre-built controls and call their RenderControl() method in order to use them in MVC views.

    Stackoverflow does this for reCAPTCHA control rendering.

    Also, the validation part is mapped to the route /captcha/post where the control is instantiated and the Validate() method is called.

    So, essentially yes you can reuse your controls but you have to adapt to the architecture of MVC.

C# - WebRequest Doesn't Return Different Pages

Here's the purpose of my console program: Make a web request > Save results from web request > Use QueryString to get next page from web request > Save those results > Use QueryString to get next page from web request, etc.

So here's some pseudocode for how I set the code up.

 for (int i = 0; i < 3; i++)
        {
            strPageNo = Convert.ToString(i);  

            //creates the url I want, with incrementing pages
            strURL = "http://www.website.com/results.aspx?page=" + strPageNo;   

            //makes the web request
            wrGETURL = WebRequest.Create(strURL);

            //gets the web page for me
            objStream = wrGETURL.GetResponse().GetResponseStream();

            //for reading web page
            objReader = new StreamReader(objStream);

            //--------
            // -snip- code that saves it to file, etc.
            //--------

            objStream.Close();
            objReader.Close();

            //so the server doesn't get hammered
            System.Threading.Thread.Sleep(1000); 
         }

Pretty simple, right? The problem is, even though it increments the page number to get a different web page, I'm getting the exact same results page each time the loop runs.

i IS incrementing correctly, and I can cut/paste the url strURL creates into a web browser and it works just fine.

I can manually type in &page=1, &page=2, &page=3, and it'll return the correct pages. Somehow putting the increment in there screws it up.

Does it have anything to do with sessions, or what? I make sure I close both the stream and the reader before it loops again...

From stackoverflow
  • That URL doesn't quite make sense to me unless you are using MVC or something that can interpret the querystring correctly.

    http://www.website.com/results.aspx&page=
    

    should be:

    http://www.website.com/results.aspx?page=
    

    Some browsers will accept poorly formed URLs and render them fine. Others may not which may be the problem with your console app.

    Matt S : I fudged it for the pseudocode. It's correct in my program. What else could be the problem?
    JB King : What is the user agent that the server is seeing from the console program? Maybe the server is handling a request from nothing differently than the request from a specific browser. There is also the question of how well does the program resolve the DNS of the web request that may be something....
  • Have you tried creating a new WebRequest object for each time during the loop, it could be the Create() method isn't adequately flushing out all of its old data.

    Another thing to check is that the ResponseStream is adequately flushed out before the next loop iteration.

    Matt S : I can't make a new WebRequest object because I get the "it's a 'method' but you're using it like a 'type'" error. I put objStream.Flush() method at the end of the loop, but with no success :(
  • Just a suggestion, try disposing the Stream, and the Reader. I've seen some weird cases where not disposing objects like these and using them in loops can yield some wacky results....

    Matt S : I added objStream/objReader.Dispose() to the end of the loop with no luck :( I even took Dillie-O's advice and put objStream.Flush() at the end, but it didn't help either...
  • This code works fine for me:

    var urls = new [] { "http://www.google.com", "http://www.yahoo.com", "http://www.live.com" };
    
    foreach (var url in urls)
    {
        WebRequest request = WebRequest.Create(url);
        using (Stream responseStream = request.GetResponse().GetResponseStream())
        using (Stream outputStream = new FileStream("file" + DateTime.Now.Ticks.ToString(), FileMode.Create, FileAccess.Write, FileShare.None))
        {
            const int chunkSize = 1024;
            byte[] buffer = new byte[chunkSize];
            int bytesRead;
            while ((bytesRead = responseStream.Read(buffer, 0, buffer.Length)) > 0)
            {
                byte[] actual = new byte[bytesRead];
                Buffer.BlockCopy(buffer, 0, actual, 0, bytesRead);
                outputStream.Write(actual, 0, actual.Length);
            }
        }
        Thread.Sleep(1000);
    }
    
  • Here's my terrible, hack-ish, workaround solution:

    Make another console app that calls THIS one, in which the first console app passes an argument at the end of strURL. It works, but I feel so dirty.

    Matt Dawdy : Bad. Find this line in your code (that isn't shown) WebRequest wrGETURL; Put that line INSIDE your loop. All will be right with the world now. By the way, this is what Dillie-O was saying.

How can I simplify a basic arithmetic expression?

How can I simplify a basic arithmetic expression?

e.g.

module ExprOps where 

simplify :: Expr -> Expr
simplify (Plus(Var"x") (Const 0)) = Var "x"

What do I have to do?

From stackoverflow
  • Are we talking rationals here, like GMP's rationals? If so, then one could simplify a division by making the second argument into its reciprocal and then multiplying.

    Apart from that, multiplication is addition done more than once, and division is subtraction done more than once.

    As Mitch has said in the comments, we could do with some more information about what you're trying to simplify.

  • e.g module ExprOps where

    simplify :: Expr -> Expr

    simplify (Plus(Var"x") (Const 0)) = Var "x"

    what do i have to do?

  • module Expr where

    -- Variables are named by strings, assumed to be identifiers. type Variable = String

    -- Representation of expressions. data Expr = Const Integer | Var Variable | Plus Expr Expr | Minus Expr Expr | Mult Expr Expr deriving (Eq, Show)

    The simplifications I have in mind are 0*e = e*0 = 0 1*e = e*1 = 0+e = e+0 = e-0 = e and simplifying constant subexpressions, e.g. Plus (Const 1) (Const 2) would become Const 3. I would not expect variables (or variables and constants) to be concatenated: Var "st" is a distinct variable from Var "s".

    What I want to achieve is to create a module like the one above that uses a function called simplify :: simplify Expr->Expr

  • Well, you have the right general model. You just need more rules and to recursively apply the simplification process.

    simplify :: Expr -> Expr 
    simplify (Mult (Const 0) x) = Const 0 
    simplify (Mult x (Const 0)) = Const 0
    simplify (Plus (Const 0) x) = simplify x
    simplify (Plus x (Const 0)) = simplify x 
    simplify (Mult (Const 1) x) = simplify x 
    simplify (Mult x (Const 1)) = simplify x 
    simplify (Minus x (Const 0)) = simpify x
    simplify (Plus (Const x) (Const y)) = Const (x + y)
    simplify (Minus (Const x) (Const y)) = Const (x - y)
    simplify (Mult (Const x) (Const y)) = Const (x * y)
    simplify x = x
    
  • thanks Edward that was very helpfull do you mean that i need more complicate expression?

Non-web Javascript frameworks

Are there any good JavaScript frameworks out there which primary audience is not web programming? Especially frameworks/libraries which improves the object orientation? The framework should be usable within an desktop application embedding a JavaScript engine (such as Spidermonkey or JavaScriptCore), so no external dependency are allowed.

From stackoverflow
  • Objective-J(avascript) is one. Is that the kind of thing you are looking for?

    Johan Dahlin : Not really. I'm just looking for a framework, not another language.
  • The most widely known one is XULRunner from Mozilla. This is framework that FireFox and Thunderbird are built on.

    https://developer.mozilla.org/en/XULRunner

    Although not strictly javascript only, it incorporates a host of technologies closely tied to web for the purposes of building desktop applications.

    Johan Dahlin : I'm actually using the javascript engine (spidermonkey) from xulrunner, but I don want the rest of the technoligies related to firefox such as gecko,xpcom,xpconnect etc. I have my own replacement for those.
  • Have you looked at Adobe Air? It allows you to use JavaScript/AJAX to write Desktop applications.

    http://www.adobe.com/products/air/

    Johan Dahlin : Not interested in web stuff. Besides, is air really free software?
    OscarRyz : You never mentioned 'free' Johan.
  • As far as "improving object orientation" goes, Javascript is already great. You just need to get used to thinking in prototypes instead of classes.

    After reading Douglas Crawford's great page on prototypal inheritance I really started to enjoy working with javascript. (He also has a page on class inheritance if you must use classes.)

    Edit: If by asking for a framework you also mean, helpful libraries that don't use the DOM, you might be interested in Functional Javascript.

    Johan Dahlin : Well, Javascript as a language is rather chatty, at least for me who is coming from a python background. A framework to reduce the amount of typing needed would be great.
    Matthew Marshall : I'm a Python user too, so I know what you mean :) For me the biggest pain in javascript is the for loop, but the Functional library mentioned in my edit *greatly* helps that.
  • Dojo can be used (and is used) in non-browser environments (e.g., Rhino, Jaxer, SpiderMonkey). It can be easily adapted for other environments too — all DOM-related functions are separated from functions dealing with global language features.

    dojo.declare() (more docs) comes in the Dojo Base (as soon as you load dojo.js) and implements full-blown OOP with single- and multiple- inheritance, automatic constructor chaining, and super-calls. In fact it is the cornerstone of many Dojo facilities.

    Of course there are more low-level facilities like dojo.mixin() to mix objects together and dojo.extend() to extend a prototype dynamically.

    More language-related features can be found in dojox.lang. Following parts of it are thoroughly explained and documented: functional, AOP, recursion combinators.

    Dojo comes with other batteries included from string-related algorithms to the date processing. If you are interested in those you can discover them yourself, or contact the Dojo community.

Grammar Writing Tools

I am trying to write a grammar in EBNF (barring a really good reason, it has to be EBNF) and am looking for a couple of utilities for it - if there's a GUI interface that can make one, that would be great, but the thing I'm looking for most is something that can check the grammar, for instance to see if it is LALR(n), and if so, what the value of n is. Do such utilities exist? Are there any other useful grammar-writing tools I should know about (I'm not looking for parser generators).

From stackoverflow
  • I use notepad(+) for EBNF projects. Normally that is not a problem. Besides that I use my own program to convert the EBNF to xhtml for easy reading.

  • To check whether a grammar allows a certain parser algorithm, just put it into the parser generator. In case of LALR(1), pass the grammar to YACC to see whether it detects conflicts.

  • ANTLR is a GUI parser generator in Java that has extensive capabilities for grammar analysis and factoring. Check the FAQ; it might support what you're looking for.

  • Taking Steven Dee's suggestion one step further, you might want to check out ANTLRWorks, which is an interactive GUI development tool for ANTLR grammars.

  • Use TinyPG! Its an amazing all-in-one compiler compiler with a lightweight GUI, where you build your parser with RegEx, EBNF, and write C#/VB code to do something with the parse tree!

    Here's a short and informative summary that I did earlier, which also links you to the downloads.


    My understandings of TinyPG:

    TinyPG allows you to write & develop:

    It has an in-built IDE, Windows Only (created in C#.NET, open source).

    It allows you to try out & preview Regular Expressions using the inbuilt RegEx tool. RegEx syntax is supported in .NET which means that even your outputted parser code uses .NET's Regular Expressions.

    You write EBNF to describe the language of your choice (Here are some freely available BNF Grammers that describe modern programming languages (C, Java, JavaScript, C#, VB.NET, SQL-89)

    Using TinyPG, your RegEx along with your EBNF outputs a parse-tree (see this diagram).

    Now what do you do with the parse-tree? You write C#/VB code (within TinyPG, per BNF grammer rule) for your Compiler/Interpreter/Translator. (inside TinyPG, open the "simple expression2.tpg" file within the provided Samples to see a demo of a calculator 'compiler')

    TinyPG allows you to Compile and Test your tokenizer+parser+complier, within the TinyPG IDE, and it finally outputs the parser source code in C#.NET or VB.NET (your choice), with ready-to-use classes.

    An excellent All-in-one Compiler Compiler for the masses. Enjoy!

    PS: Correct me if I'm wrong on any of this.

    dr. evil : great answer, cheers.
  • TinyPG also lets you quickly Compile (test) and debug your EBNF grammer and RegEx expressions.

Making functions non override-able

I know python functions are virtual by default. Lets say i have this:

class foo:
 def __init__(self, args):
   do some stuff
 def goo():
   print "You can overload me"
 def roo():
   print "You cannot overload me"

I dont want them to be able to do this:

class aoo(foo):
 def roo():
   print "I dont want you to be able to do this"

Is there a way to prevent users from overloading roo()?

From stackoverflow
  • Since Python has monkey patching, not only can you not make anything "private". Even if you could, someone could still monkeypatch in a new version of the method function.

    You can use this kind of name as a "don't go near" warning.

    class Foo( object ):
        def _roo( self ):
           """Change this at your own risk."""
    

    That's the usual approach. Everyone can read your source. They were warned. If they boldly go where they were warned not to go, they get what they deserve. It doesn't work and you can't help them.

    You can try to make this intentionally obcure with inner classes and "hidden" implementation modules that are called by the "private" methods. But... everyone has your source. You can't prevent anything. You can only advise people of the consequences of their actions.

  • You can use a metaclass:

    class NonOverridable(type):
        def __new__(self, name, bases, dct):
            if bases and "roo" in dct:
                raise SyntaxError, "Overriding roo is not allowed"
            return type.__new__(self, name, bases, dct)
    
    class foo:
        __metaclass__=NonOverridable
        ...
    

    The metatype's new is called whenever a subclass is created; this will cause an error in the case you present. It will accept a definition of roo only if there are no base classes.

    You can make the approach more fancy by using annotations to declare which methods are final; you then need to inspect all bases and compute all final methods, to see whether any of them is overridden.

    This still doesn't prevent somebody monkey-patching a method into a class after it is defined; you can try to catch these by using a custom dictionary as the classes' dictionary (which might not work in all Python versions, as classes might require the class dictionary to be of the exact dict type).

    S.Lott : +1 for a GOOD example of metaclass __new__
    hop : i would hate you, if i had to use your class
    fuzzyman : Couldn't you provide your method as a descriptor that prevented itself from being removed or overriden? Presumably descriptors are only triggered on attribute lookup and not if it they are looked up directly in the class dictionary.
  • def non_overridable(f):
        f.non_overridable = True
        return f
    
    class ToughMeta(type):
        def __new__(cls, name, bases, dct):
            non_overridables = get_non_overridables(bases)
            for name in dct:
                if name in non_overridables:
                    raise Exception ("You can not override %s, it is non-overridable" % name)
            return type.__new__(cls, name, bases, dct)
    
    def get_non_overridables(bases):
        ret = []
        for source in bases:
            for name, attr in source.__dict__.items():
                if getattr(attr, "non_overridable", False):
                    ret.append(name)
            ret.extend(get_non_overridables(source.__bases__))
        return ret
    
    class ToughObject(object):
        __metaclass__ = ToughMeta
        @non_overridable
        def test1():
            pass
    
    # Tests ---------------
    class Derived(ToughObject):
        @non_overridable
        def test2(self):
            print "hello"
    
    class Derived2(Derived):
        def test1(self):
            print "derived2"
    
    # --------------------
    

"Multiple inheritance" (generalisation relationship) in data models

For some school groupwork, I'm making a Generic Pizza Delivery Everything Management System, and got stumped on a problem during data modelling that I can't figure out without using at least several layers of ugly.

The restaurant keeps stock of ingredients and beverages/snacks/etc. (I'll refer to these as "drinks"). Drinks have a selling price. The app stores recipes for dishes - each dish has a collection of ingredients and the amount used, as well as its selling price. An order has a collection of recipes and drinks, but not ingredients - it doesn't make sense to sell flour directly.

From that I have the need for both a "stockable" generalisation between ingredients and drinks, and a "sellable" (or "menu item") generalisation between drinks and recipes; and I can't really recall ERD modelling allowing for multiple generalisations.

(Currently I just went with the menu item one and decided to let the stockable stuff have separate entity hierarchies, and handle it in application code. And am considering dropping the separate hierarchies and not express the ingredient/drink difference in the data model at all, instead using an "is ingredient" flag attribute.)

Is there something I'm missing that would let me model this without having to handle anything (or as little as possible) in application code?

For more fun if someone likes to do this kind of thing as a mental exercise / puzzle, dropped features involve:

  • drinks coming in categories with different tax rates - think soft drinks and liquor being taxed differently; dishes also have a tax rate, but don't belong into a category
  • it makes sense to sell /some/ of the ingredients - the toppings (for people who want extra bacon but no olives); we'd also have to store how many units of the topping is in a single "extra" serving.
From stackoverflow
  • Isn't there a simple solution where you have a table of ingredients or stockable items, which includes drinks and other ingredients that might be listed in their own right. None of these can be ordered directly.

    You then have a table of menu items which are all a collection of ingredients / stockable items. A menu item collection may have only a single item - in the case of drinks this may always be the case, but could also include e.g. a slice of lemon.

  • SQL does not directly support the concept of inheritance or generalisation. The relationship between tables is only that of reference. So there is no "IS-A" mechanism.

    You can use a foreign key to mean "belongs to" (as in "this drink belongs to the category of merchandise with 8% tax rate") or you can use a foreign key to mean "has one" (as in "this ingredient has one parent").

    You can support separate hierarchies, allowing one item to belong to multiple trees, using the Adjacency Relation design.

    Since this is a homework problem, I'll stop there and leave the rest for you. :-)

How do I get rid of the red rectangle when my wpf binding validation has failed and the containing panel is no longer visible?

I have a situation where I am using wpf data binding and validation using the ExceptionValidationRule.

Another part of the solution invovles collapsing some panels and showing others.

If a validation exception is set - i.e. the UI is showing a red border around the UI element with the validation problem, and the containing panel is collapsed, the red border is still displayed. This is clearly not meant to be? Is there a workaround for this? Anyone know if this is by design?

Minimal code example provided (not my actual code, but replicates the problem). Create a new WpfApplication (I called mine WpfDataBindingProblem).

The xaml for window1 is as follows:

<Window x:Class="WpfDataBindingProblem.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <StackPanel Margin="5">

        <StackPanel Name="panel1" Visibility="Visible" Margin="5">
            <TextBox Name="DataBoundTextBox">
                <Binding Path="TextValue">
                    <Binding.ValidationRules>
                        <ExceptionValidationRule/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox>
        </StackPanel>

        <StackPanel Name="panel2" Visibility="Collapsed" Margin="5">
            <TextBlock>
                The quick brown fox jumps over the lazy dog.
            </TextBlock>
        </StackPanel>

        <Button Click="Button_Click" Margin="5">
            Toggle panels
        </Button>

    </StackPanel>
</Window>

The code for window1 is as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfDataBindingProblem {

    public partial class Window1 : Window {
        public Window1() {
            InitializeComponent();

            this.DataContext = new MyClass("default");
        }

        private void Button_Click(object sender, RoutedEventArgs e) {
            panel1.Visibility = panel1.Visibility == Visibility.Collapsed ?
                Visibility.Visible : Visibility.Collapsed;
            panel2.Visibility = panel2.Visibility == Visibility.Collapsed ?
                Visibility.Visible : Visibility.Collapsed;
        }
    }

    public class MyClass : INotifyPropertyChanged {

        private string mTextValue;

        public MyClass(string defaultText) {
            TextValue = defaultText;
        }

        public string TextValue {
            get {
                return mTextValue;
            }
            set {
                mTextValue = value;
                if (string.IsNullOrEmpty(mTextValue)) {
                    throw new ApplicationException("Text value cannot be empty");
                }
                OnPropertyChanged(new PropertyChangedEventArgs("TextValue"));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) {
            if (this.PropertyChanged != null) {
                this.PropertyChanged(this, e);
            }
        }
    }

}

To reproduce the problem, run the application. Delete the default text from the textbox and tab off - red rectangle is shown indicating a validation problem. Click the button. Panel containing control with red rectangle is hidden and another panel is shown, but the red rectangle remains. Aargh!

All help much appreciated.

PS apologies for long question title!

From stackoverflow
  • I have an answer to the problem myself which is to change my button click event which changes the visibility of the panels. This would change to something like this:

    private void Button_Click(object sender, RoutedEventArgs e) {
        if (panel1.Visibility == Visibility.Collapsed) {
            panel1.Visibility = Visibility.Visible;
            DataBoundTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
            panel2.Visibility = Visibility.Collapsed;
        }
        else {
            panel1.Visibility = Visibility.Collapsed;
            DataBoundTextBox.GetBindingExpression(TextBox.TextProperty).UpdateTarget();
            panel2.Visibility = Visibility.Visible;
        }
    }
    

    The UpdateSource() and UpdateTarget() have the effect of reapplying and removing the red rectangle, but this seems like an ugly hack. Surely the wpf framework should be hiding the red rectangle for me when the containing panel is collapsed. Any cleaner fix that doesn't require me to fiddle with the binding expression gets my vote.

    Thanks,

    Sam

  • If I remember correctly, this is a known issue. We re-templated textbox to include the following:

    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate>
                <ControlTemplate.Resources>
                    <BooleanToVisibilityConverter x:Key="converter" />
            </ControlTemplate.Resources>
                <DockPanel LastChildFill="True">
                    <Border 
                        BorderThickness="1"
                        BorderBrush="Red"
                        Visibility="{Binding ElementName=placeholder, Mode=OneWay, Path=AdornedElement.IsVisible, Converter={StaticResource converter}}"
                        >
                        <AdornedElementPlaceholder x:Name="placeholder" />
                    </Border>
                 </DockPanel>
             </ControlTemplate>
        </Setter.Value>
    </Setter>
    

    Sam Meldrum : Donnelle, This is fantastic! Exactly what I need. Many thanks, Sam
    Donnelle : You're welcome. Glad to help!
    Pwninstein : That's amazingly simple. Thanks!!!

MarshalByRefObject and Serialization

When I create a class, say ClassA, that inherits from MarshalByRefObject and then use the RemotingServices.Marshal method to create an ObjRef object does it serialize the all the private fields of ClassA?

Thanks

From stackoverflow
  • No. The idea of MarshalByRefObject is that it doesn't every get serialized for remoting purposes. Instead the CLR generates what is known as a transparent proxy in this scenario. It's called Transparent because it looks and acts just like ClassA but in reality is not actually ClassA. All calls into an instance of ClassA are marshalled accross the remoting boundaries into the original AppDomain where ClassA was created.

    EDIT: Responding to further clarification.

    When passing data to a MarshalByRefObject in this scenario you need to consider what type the data is. Essentially derives from MarshalByRefObject or it doesn't. If it does derive from MarshalByRefObject then the parameter will be passed as a proxy. If it doesn't derive from MarshalByRefObject then it will be serialized, passed across the AppDomain boundary as a series of bytes and then deseriazied in the target AppDomain.

    Your scenario listed strings which are not MarshalByRef (usually I refer to these as MarshalByValue but that's purely convention). So they will be serialized when passed across your remoting boundary.

  • so if i have a large class (lots of state) and use RemotingServices.Marshal to get a proxy and then send that proxy over TCP (remoting boundary) the objects state won't get serialized?

    And then, in another appdomain (another machine on the network) a method on the proxy is given a large amount of data , say a large list of strings, where will the data be processed? In the proxy or the orginal obect? Or will the proxy just send the data to the original object?

    Thanks

    liggett78 : The data will be marshalled to where the original object resides, everything will be processed by this object. The proxy just forwards the calls, no less no more.

JavaScript Parent variables

I have the following code:

var address;
getAddress(0,0);
function getAddress(latlng) 
{
  if (latlng != null) 
  {
    geocoder.getLocations(latlng, 
    function(addresses) 
    {
      if(addresses.Status.code == 200) 
      { 
        address = addresses.Placemark[0].address.toString();
        alert(address);  // Outputs something :)
      }
    });
   }
   return address;   //returns nothing :(
}

I am very new to javascript and don't know why address always returns undefined but the alert does output something.

geocoder is an instance of google maps apis http://code.google.com/apis/maps/documentation/reference.html#GClientGeocoder

From stackoverflow
  • I think that your problems are all related to scope. Generally speaking, you don't want to rely on a global declaration of a variable to be used within the scope of a function.

    This should correct any scope issues with your function:

    var address = getAddress(0,0);
    
    function getAddress(latlng) {
        if (latlng != null) {
         var address = geocoder.getLocations(latlng, function(addresses) {
          if(addresses.Status.code == 200) { 
           return addresses.Placemark[0].address.toString();
          }
         });
        }
    return address;
    }
    
  • I can't see any reason why this won't work.

    To be doubley sure that nothing returning anything make the function call like this

    window.alert(getAddress(0,0));
    

    And then see what is being output

  • If I am not mistaken, it looks like

    geocoder.getLocations
    

    will not return a value, but expect a callback function, in your case:

    function(addresses) {
        if(addresses.Status.code == 200) { 
            return addresses.Placemark[0].address.toString();
        }
    }
    

    This "inner" return won't do much, as it will be returning to the insides of geocoder.getLocations.

    So, the function doing the assignment is probably being called later than the outer return (the callback and the 200 status suggest that there is a slow http call involved).

    That is,unless you can modify geocoder.getLocations the solution will be to make your function functional too, something like:

    function getAddress(latlng, callback) {
        if (latlng != null) {
            geocoder.getLocations(latlng, function(addresses){
                if(addresses.Status.code == 200) { 
                    address = addresses.Placemark[0].address.toString();
                    alert(address);
                    //Outputs something :)
                    callback(address);
                }
            });
        }
    

    }

    and you will call it with something like:

    getAddress(ll, function(address){
       alert(address);
    });
    

    And as a bonus you can get rid of the global variable :-)

    Victor : There is a lot of reading material on functional javascript: http://www.google.com/search?q=functional+javascript
  • I successfully reproduced the error by implementing geocoder.getLocations() to be asynchronous. If geocoder.getLocations() simply executed the parameter function then the change of the variable would become visible before getAddress() returns, so this is not a scope problem.

    var address;
    alert("B: address returned: " + getAddress());
    function getAddress() {
      executeFunction(function() {
        address = "myAddress";
        alert("C: address set to: " + address);
      });
      return address;
    }
    
    function executeFunction(aFunction) {
      alert("A: executing: " + aFunction);
      window.setTimeout(aFunction, 1);
    }
    

    Executing the code above results in the alert order A-B-C, meaning that address is returned before its assignment. Replacing window.setTimeout(aFunction, 1); with aFunction() results in the order A-C-B, the last alert being "B: address returned: myAddress".