Friday, April 29, 2011

How do I remember which way round PRIOR should go in CONNECT BY queries

Hi everyone. I've a terrible memory. Whenever I do a CONNECT BY query in Oracle - and I do mean every time - I have to think hard and usually through trial and error work out on which argument the PRIOR should go.

I don't know why I don't remember - but I don't.

Does anyone have a handy memory mnemonic so I always remember ?

For example:

To go down a tree from a node - obviously I had to look this up :) - you do something like:

select
    *
from
    node
connect by
    prior node_id = parent_node_id
start with
    node_id = 1

So - I start with a node_id of 1 (the top of the branch) and the query looks for all nodes where the parent_node_id = 1 and then iterates down to the bottom of the tree.

To go up the tree the prior goes on the parent:

select
    *
from
    node
connect by
    node_id = prior parent_node_id
start with
    node_id = 10

So starting somewhere down a branch (node_id = 10 in this case) Oracle first gets all nodes where the parent_node_id is the same as the one for which node_id is 10.

EDIT: I still get this wrong so thought I'd add a clarifying edit to expand on the accepted answer - here's how I remember it now:

select
    *
from
    node
connect by
    prior node_id = parent_node_id
start with
    node_id = 1

The 'english language' version of this SQL I now read as...

In NODE, starting with the row in which node_id = 1, the next row selected has its parent_node_id equal to node_id from the previous (prior) row.

EDIT: Quassnoi makes a great point - the order you write the SQL makes things a lot easier.

select
    *
from
    node
start with
    node_id = 1
connect by
    parent_node_id = prior node_id

This feels a lot clearer to me - the "start with" gives the first row selected and the "connect by" gives the next row(s) - in this case the children of node_id = 1.

From stackoverflow
  • Think about the order in which the records are going to be selected: the link-back column on each record must match the link-forward column on the PRIOR record selected.

  • I always try to put the expressions in JOIN's in the following order:

    joined.column = leading.column
    

    This query:

    SELECT  t.value, d.name
    FROM    transactions t
    JOIN
            dimensions d
    ON      d.id = t.dimension
    

    can be treated either like "for each transaction, find the corresponding dimension name", or "for each dimension, find all corresponding transaction values".

    So, if I search for a given transaction, I put the expressions in the following order:

    SELECT  t.value, d.name
    FROM    transactions t
    JOIN
            dimensions d
    ON      d.id = t.dimension
    WHERE   t.id = :myid
    

    , and if I search for a dimension, then:

    SELECT  t.value, d.name
    FROM    dimensions d
    JOIN
            transactions t
    ON      t.dimension = d.id
    WHERE   d.id = :otherid
    

    Ther former query will most probably use index scans first on (t.id), then on (d.id), while the latter one will use index scans first on (d.id), then on (t.dimension), and you can easily see it in the query itself: the searched fields are at left.

    The driving and driven tables may be not so obvious in a JOIN, but it's as clear as a bell for a CONNECT BY query: the PRIOR row is driving, the non-PRIOR is driven.

    That's why this query:

    SELECT  *
    FROM    hierarchy
    START WITH
            id = :root
    CONNECT BY
            parent = PRIOR id
    

    means "find all rows whose parent is a given id". This query builds a hierarchy.

    This can be treated like this:

    connect_by(row) {
      add_to_rowset(row);
    
      /* parent = PRIOR id */
      /* PRIOR id is an rvalue */
      index_on_parent.searchKey = row->id;
    
      foreach child_row in index_on_parent.search {
        connect_by(child_row);
      }
    }
    

    And this query:

    SELECT  *
    FROM    hierarchy
    START WITH
            id = :leaf
    CONNECT BY
            id = PRIOR parent
    

    means "find the rows whose id is a given parent". This query builds an ancestry chain.

    Always put PRIOR in the right part of the expression.

    Think of PRIOR column as of a constant all your rows will be searched for.

    Nick Pierpoint : I like "Always put PRIOR in the right part of the expression" - it does make it easier. Also putting "start with" at the start.

What bad habits did you learn from one language that you transferred to another?

I know I've sure transferred a lot of habits between languages. I found that I tried transferring Python style to C# a lot. What sorts of habits did transfer between langugages like this?

From stackoverflow
  • Mixins in C++ use multiple inheritance.. Something that's not available in a lot of modern languages. I used to use this kindof approach a lot.

    I wouldn't say it what as a bad habbit though, I just needed to rethink the way I did things :) (If any Mixin reads this, I still love you. You are my one true love <3 )

  • Assembly Language mind set makes me think every now and then, "yeah, but how many clock cycles is that using"

    David Lehavi : which is not necessarily always bad...
  • Well, using hashes like a normal person without thousands of superfluous and stupid key-existence checks, like one does in, say, Perl, is a bad habit in PHP.

    Though I don't exactly feel like it's the habit that's bad, there.

  • After cutting my teeth as a classic VB desktop programmer in the 90's I'm very used to code-behind and the whole event-driven model of programming. So I easily transitioned to ASP.NET webforms with similar events and code-behind, but many people now say that is bad design and doesn't follow the ways of the web (unlike ASP.NET MVC).

    The "bad" way just seems so natural to me...probably due to my VB6 days.

  • In the D programming language: Using template metaprogramming for everything. I've gotten so spoiled by D's templates that I have no idea how anyone gets anything done in any other statically typed language anymore.

  • returning closures in Lua and Python is very natural and convenient. The same in JavaScript works, but it's a great PITA and requires a bit too much extra typing to be readable.

  • In FORTRAN 77, it's very common to use I, J and K as loop control variables in a DO loop. I still use i, j and k when writing FOR loops in Ada, C, C++ or whatever language I'm coding in, even though I haven't written a single line of FORTRAN for over 10 years.

    People often make comments about it but I refuse to accept that there are better loop control variable names! I also feel the compulsion to rewrite other people's FOR loops to use i, j and k because the code looks too verbose otherwise!

  • I learned to program (truly) with C++, which meant I was stuck with the clunky STL, as I didn't know how to use external libraries at the time. Needless to say, I wound up rolling my own solutions to a number of standard api-solved problems.

    The biggest thing I learned in my transition to Java was to trust pre-existing solutions over my own. The Java API has a wealth of obscure classes that fit extraordinarily well to certain problems, with the added bonus of a defined standard usage.

  • After using Haskell and Lua, it bugs me that I can't write this in C/C++:

    int, int foo() {
        return 2, 3;
    }
    int a, b;
    a, b = foo();
    

How to know when to manage resources in Python

I hope I framed the question right. I am trying to force myself to be a better programmer. By better I mean efficient. I want to write a program to identify the files in a directory and read each file for further processing. After some shuffling I got to this:

for file in os.listdir(dir):
    y=open(dir+'\\'+file,'r').readlines()
    for line in y:
        pass
    y.close()

It should be no surprise that I get an AttributeError since y is a list. I didn't think about that when I wrote the snippet.

I am thinking about this and am afraid that I have five open files (there are five files in the directory specified by dir.

I can fix the code so it runs and I explicitly close the files after opening them. I am curious if I need to or if Python handles closing the file in the next iteration of the loop. If so then I only need to write:

for file in os.listdir(dir):
    y=open(dir+'\\'+file,'r').readlines()
    for line in y:
        pass

I am guessing that it(python) does handle this effortlessly. The reason I think that this might be handled is that I have changed the object/thing that y is referencing. When I start the second iteration there are no more memory references to the file that was opened and read using the readlines method.

From stackoverflow
  • Python will close open files when they get garbage-collected, so generally you can forget about it -- particularly when reading.

    That said, if you want to close explicitely, you could do this:

    for file in os.listdir(dir):
        f = open(dir+'\\'+file,'r')
        y = f.readlines()
        for line in y:
            pass
        f.close()
    

    However, we can immediately improve this, because in python you can iterate over file-like objects directly:

    for file in os.listdir(dir):
        y = open(dir+'\\'+file,'r')
        for line in y:
            pass
        y.close()
    

    Finally, in recent python, there is the 'with' statement:

    for file in os.listdir(dir):
        with open(dir+'\\'+file,'r') as y:
            for line in y:
                pass
    

    When the with block ends, python will close the file for you and clean it up.

    (you also might want to look into os.path for more pythonic tools for manipulating file names and directories)

    PyNEwbie : for file in os.listdir(dir): for line in open(dir+'\\'+file,'r'): pass Your suggestion led to this.
    kquinn : On Python 2.5 or better, I'd prefer the `with` statement forms, which clearly communicate the intent of the code.
    John Fouhy : Yup. Note that you have to do a __future__ import to get it in python 2.5.
    Jarret Hardie : +1 for suggesting os.path
  • Don't worry about it. Python's garbage collector is good, and I've never had a problem with not closing file-pointers (for read operations at least)

    If you did want to explicitly close the file, just store the open() in one variable, then call readlines() on that, for example..

    f = open("thefile.txt")
    all_lines = f.readlines()
    f.close()
    

    Or, you can use the with statement, which was added in Python 2.5 as a from __future__ import, and "properly" added in Python 2.6:

    from __future__ import with_statement # for python 2.5, not required for >2.6
    
    with open("thefile.txt") as f:
        print f.readlines()
    
    # or
    
    the_file = open("thefile.txt")
    with the_file as f:
        print f.readlines()
    

    The file will automatically be closed at the end of the block.

    ..but, there are other more important things to worry about in the snippets you posted, mostly stylistic things.

    Firstly, try to avoid manually constructing paths using string-concatenation. The os.path module contains lots of methods to do this, in a more reliable, cross-platform manner.

    import os
    y = open(os.path.join(dir, file), 'r')
    

    Also, you are using two variable names, dir and file - both of which are built-in functions. Pylint is a good tool to spot things like this, in this case it would give the warning:

    [W0622] Redefining built-in 'file'
    
    PyNEwbie : This was useful thanks. My friends think I am a genious but then I show them how this web site makes it all possible.

C#: How to send keyboard scan codes manually?

I'm working on a project that needs to emulate a keypress of the Windows key. I've tried SendKeys.Send to no avail.

Specifically, the windows key needs to come in concert with a button. That is, I want to send Windows Key and plus / minus.

From stackoverflow
  • Try SendInput or the older keybd_event

  • I think your best bet is using keybd_event keydown (called ExtendedKey) with the LWin value of the System.Windows.Forms.Keys enum, then keydown second character (+), and keyup both keys.

    I do not believe SendKeys works with the Windows key as a modifier.

  • I would add that it is often unlikely for you to find lower level functions like these in the .NET framework. If you were confused as to why the suggestions both pointed to "non C#" functions, then you probably could use some details on P/Invoke.

    Basically there are ways to define C# functions that "tie" them to Windows API functions that do not exist within .NET assemblies (Instead they are typically implemented in C++ and available as a standard DLL). This process is considered to be "(Windows) Platform Invoking" (thus P/Invoke).

    It can be a bit wobbly at first to match up all the data types between C++ and C# style calls, but fortunately, there are others out there that have paved the way.

    The suggested function, SendInput, has a PInvoke wrapper over at PInvoke.net. This wrapper class, when available in your assembly, will allow you to call SendInput as if it were a C# function.

    PInvoke.net is basically a PInvoke wiki for well known API calls in windows, and typically has a C#/VB.NET wrapper of API calls.

  • This may be overkill, but you could try using AutoItX which is a way to use AutoIt as a DLL. I've only written standalone scripts, but I know AutoIt makes it very easy to simulate pressing the Windows key.

    For example, to open the run dialog is just:

    Send("#r") ;Win + R = run

how do I add a class to a CodeIgnitor anchor?

I have the following:

'.anchor('','Home').'

and I want to add the following CSS class to it:

class="top_parent"

so that when its rendered in the browser, the code will look something like

<a href="#" class="top_parent">Home</a>

Thanks in advance. Any help is hugely appreciated.

Tom

From stackoverflow
  • You can specify an associative array of attributes for your Anchor. So, for example:

    anchor('', 'Home', array('class' => 'top_parent'));

  • anchor('#', 'Home', array('class' => 'top_parent'));
    
  • The Codeignitor function is defined as such:

    function anchor($uri = '', $title = '', $attributes = '')
    

    I would try sending an array with a class key and value first.

    These functions are found inside the \system\helpers\ folder.

    NTulip : boy all that time i spend finding the function and two very talented people had answered the question already. Oh well - my answer is here to stay.

server configuration questions...

Please pardon my non-understanding here. I have a local mysql server and I need to be able to access that data over an encrypted channel from a java web application running on a web host. Can anyone recommend the best way to do this?

Thank you! Joshua

From stackoverflow
  • AFAIK MySQL does not support encrypted streams (correct me if I am wrong).

    One solution I can see would be to have an encrypted tunnel running between the MySQL server and the web host, and route connections to the database through it.

  • You'll need to set up an SSH tunnel.

  • MySQL does support SSL connections.

    Check this document for assistance: http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-using-ssl.html

  • SSH Port Forwarding

    In this instance, one could port forward db_server:3306 to web_server:3306. Then it would appear as if there were a MySQL database running locally on the web server listening on port 3306. However, localhost:3306 on the web server is really being securely forwarded to localhost:3306 on the database server.

    To set this up, you'll want a password-less key pair to allow the SSH tunnel to be started automagically. Do the following:

    db_serv$ ssh-keygen -t rsa
    db_serv$ scp .ssh/id_rsa.pub webserver:
    web_serv$ cd ~; mkdir .ssh
    web_serv$ cat id_rsa.pub >> .ssh/authorized_keys2
    web_serv$ chmod -R go-rwx .ssh; rm id_rsa.pub
    db_serv$ ssh webserver
    

    The last command should let you SSH from the database server without providing a password. The keypair does the authentication.

    The command to open an SSH tunnel is:

    db_server$ ssh -f -q -N -R3306:db_server:3306 webserver

    You can then test out local database access on the webserver. You'll need to have the permissions set correctly in the MySQL databse for the user and password you're using.

    web_serv$mysql -h 127.0.0.1 -P 3306 -u user -p db_name

    You'll probably want to add the 'ssh' line above to /etc/rc.d/rc.local (on Red Hat) so that the tunnel gets opened on reboots. Remember if the tunnel goes down, your web app can't access the database.

  • Yes, MySQL supports encrypted connections over SSL.

    You need a version of MySQL Server that has been built with either OpenSSL, or the bundled yaSSL. If your MySQL Server wasn't built with SSL support, the --ssl and related options will give errors.

    You need to start the MySQL Server (mysqld) with the --ssl option and related options to specify the SSL key and certificate. See http://dev.mysql.com/doc/refman/5.1/en/secure-connections.html for more information on enabling MySQL Server to support SSL.

    Your Java client also must support SSL. You need to supply a client certificate when you connect. See http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-using-ssl.html for more information on making secure connections to MySQL from Java.

  • This is basically the same as every other answer here, but here goes anyway. Use a VPN tunnel such as openVPN to encrypt the communication. The best part about it is the transparency. When you're on the VPN, you don't need to think about it any more, just send secure communications. Of course, setting it up is NOT the easy part...

WCF Array Serialization

I am using a WCF OperationContract that takes an array of integers as an argument. It is using basicHttpBinding.

I've noticed that the generated SOAP from a client generated using Visual Studio "Add Web Reference" includes the xmlns thus:

<ids>
  <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">100</string>
  <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">101</string>
  <string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">102</string>
   ... etc
</ids>

This will increase the size of the serialized stream with large arrays. Is there any way to eliminate this xmlns attribute?

For a WCF client, the generated SOAP looks more like what I would expect:

<ids xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <a:string>100</a:string>
  <a:string>101</a:string>
  <a:string>102</a:string>
  ... etc..
</ids>
From stackoverflow
  • I'm not too familiar with serialization, but could this be the difference between SOAP 1.1 and 1.2? I'm betting that you can specify either format. Is there a compelling reason to not just use the WCF client?

    Joe : I'm using SOAP 1.1 in both cases (basicHttpBinding). I need so support legacy non-WCF clients (.NET 3.0 is not installed everywhere).
  • That's really a function of the client proxy and not your service, unfortunately. In this example, you are looking at a client using XML Serialization vs. Data Contract Serialization. One is simply better than the other at making the XML more compact.

    You might have better luck with the type generator in WSE 3.0 (link) It is possible there is a set of XML attribute tags you can put on a class to make it serialize better and maybe those were integrated into WSE, but I'm not 100% on that.

    You should let us know what you decide. Very interesting.

How to generate examples of a gettext plural forms expression? In Python?

Given a gettext Plural-Forms line, general a few example values for each n. I'd like this feature for the web interface for my site's translators, so that they know which plural form to put where. For example, given:

"Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%" "10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\n"

... I want the first text field to be labeled "1, 21..", then "2, 3, 4...", then "5, 6..." (not sure if this is exactly right, but you get the idea.)

Right now the best thing I can come up with is to parse the expression somehow, then iterate x from 0 to 100 and see what n it produces. This isn't guaranteed to work (what if the lowest x is over 100 for some language?) but it's probably good enough. Any better ideas or existing Python code?

From stackoverflow
  • Given that it's late, I'll bite.

    The following solution is hacky, and relies on converting your plural form to python code that can be evaluated (basically converting the x ? y : z statements to the python x and y or z equivalent, and changing &&/|| to and/or)

    I'm not sure if your plural form rule is a contrived example, and I don't understand what you mean with your first text field, but I'm sure you'll get where I'm going with my example solution:

    # -*- Mode: Python -*-
    # vi:si:et:sw=4:sts=4:ts=4
    
    p = "Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\n"
    
    # extract rule
    import re
    matcher = re.compile('plural=(.*);')
    match = matcher.search(p)
    rule = match.expand("\\1")
    
    # convert rule to python syntax
    oldrule = None
    while oldrule != rule:
        oldrule = rule
        rule = re.sub('(.*)\?(.*):(.*)', r'(\1) and (\2) or (\3)', oldrule)
    
    rule = re.sub('&&', 'and', rule)
    rule = re.sub('\|\|', 'or', rule)
    
    for n in range(40):
        code = "n = %d" % n
        print n, eval(rule)
    
    Andrew B. : You have a very good approach. I'd thought of converting the ? operator to Python's "x if y else z" format, but that would require more parsing. The code in this solution is susceptible to the main problem of "x and y or z", which is "what if y is false?", but converting 0 to 'zero' works. Hmmm....

Silverlight WCF service acting strange

Hi

I have a silverlight project that calls into a wcf service. Everything works fine on my local machine.

However when I deploy to a virtual machine, with the exact same query the wcf service returns, but the result is empty.

I've tried debugging, but have not been able to get it to break in the wcf service.

Any ideas what the problem could be, or how I could go about debugging it?

Thanks


I figured out what the problem is, but am not sure what the solution is.

In my silverlight project the wcf service I am referencing is http://localhost/.../SilverlightApiService.svc

I used fiddler on my vm to see the request that was made and instead of trying to contact the above service, it was trying to contact:

http:///.../SilverlightApiService.svc

So, for some reason my machine name is getting inserted in there instead of localhost. Any thoughts on this would be appreciated.

From stackoverflow
  • Can you give us a bit more information? What kind of binding are you using? What does the service config and the client config look like? Where do you get your data from that gets returned? Could it be the service on the VM just doesn't get any data? (e.g. queries a database that just doesn't have the data requested?)

    Marc

  • I have had that happen before. I would try this. Set you start page as the web service file and run the app. Then set the start page back to your default page. Then update all the server references in your SL project. Recompile everything and republish. This has helped me a bunch of times in the past.

  • I figured out what the problem is, but am not sure what the solution is.

    In my silverlight project the wcf service I am referencing is http://localhost/.../SilverlightService.svc

    I used fiddler on my vm to see the request that was made and instead of trying to contact the above service, it was trying to contact:

    http:///.../SilverlightService.svc

    So, for some reason my machine name is getting inserted in there instead of localhost. Any thoughts on this would be appreciated.

  • I had this exact problem when deploying to amazon ec2 - The machine name for the service was being returned in the wsdl rather than the dns.

    There were a couple solutions (one involved creating static wsdl - yuck!)

    But the other was creating a sort of factory pattern for the service

    This thread (you can read it all, but the answers are at the bottom.) http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/c7fd51a2-773e-41d4-95a0-244e925597fe/

    The slight downfall with this is that although it works - if you change the location of the server, you will need to remember to update your config - Which although isn't hard, it's easy to forget to do.

  • I figured it out.

    Basically my machine name was hard coded in my ServiceReferences.ClientConfig file in my silverlight project.

    What I had to do was specify programmatically what url to use for the service reference when instantiating my service client:

    System.ServiceModel.EndpointAddress address = new System.ServiceModel.EndpointAddress(new Uri
            (Application.Current.Host.Source, "../WebServices/SilverlightService.svc"));
    
            ServiceClient serviceClient = new ServiceClient("BasicHttpBinding_IService", address);
    

Any good recommendation/resource for doing Branching in TFS (Team Foundation Server)?

Branching always seems to be a complicated thing to do. Not technically but yeah there are some real decisions/planning that goes in to come up with an appropriate branching strategy.

I know of this TFS Branching Guide

Any other resource or guideline which you recommend or use for branching and merge.

Sharing your experience in this area is really appreciated.

From stackoverflow
  • If you only concentrate on resources that specifically reference TFS then you'll miss a huge amount of excellent content that is more general in nature or is even aimed at other source control systems. Given that you seem to be more interested in methodology rather than the technical implementation of TFS branching, it makes sense to look elsewhere - for example:

    • You can find a good general source control tutorial here, which is written by Eric from SourceGear
    • Jeff Atwood (one of the SO founders) talked about branching strategies in his Coding Horror blog
    • SVN has an excellent - if slightly verbose - section on branching in its documentation
    Ray Booysen : I agree with this. The TFS Branching guidelines are pretty good, but subversion's base structure is also good.
    rajesh pillai : Thanks. I'll definitely have a look at this.
  • I've read the patterns and practices branching document and have now started reading the Team Foundation Server 2008 in Action from Manning.

    I haven't got as far as the branching\merging strategy section yet in this publication but what I've read so far (related to the new build features in TFS 2008) have been pretty good!

    rajesh pillai : Thanks, Andrew. I will definitely have a look at this.
  • I think this a pretty refreshing view also infoq

  • Read this: http://oreilly.com/catalog/practicalperforce/chapter/ch07.pdf Also, as a general rule, Microsoft's stuff is overcomplicated and difficult to use. Try something open source first.

Is there a way to determine if a user is using broadband or dial-up

We have a requirement from a customer to provide a "lite" version for dial-up and all the bells-and-whistles for a broadband user.

The solution will use Flex / Flash / Java EJB and some jsp.

Is there a way for the web server to distinguish between the two?

From stackoverflow
  • The simplest and most reliable mechanism is probably to get the user to select their connection type from a drop down. Simple, I know, but it may save you a world of grief!

  • You could check their IP and see if it resolves to/is assigned to a dial up provider, such as AOL, Earthlink, NetZero. Wouldn't guarantee that those that don't resolve to such a provider are broadband users.

  • You don't care about the user's connection type, you care about the download speed.

    Have a tiny flash app that downloads the rest the of the flash, and times how long it takes. Or an HTML page that times how long an Ajax download takes.

    If the download of the rich-featured app takes too long, have the initially downloaded stub page/flash redirect to the slow download page (or download the bare-bones flash app, or whatever).

    ChrisF : +1 for point about the real issue being the download speed not the connection type
    tpdi : Thanks! It's all about how you characterize a problem.
    Beska : This answer is all kinds of slick in one tight little bundle. Easy +1.
    David Zaslavsky : +1, same reason as ChrisF... I have a broadband internet connection at home that's often slower (and less reliable) than dialup.
  • you could ...

    • ask the user
    • perform a speed test and ask the user if the result you found is correct
    • perform a speed test and hope that the result found is correct

    I think a speed test should be enough.

    If you only have a small well known user group it is sometimes possible to determine the connection speed by the ip. (Some providers assign different subnets to dial-up/broadband connections)

  • There's no way to distinguish between a broadband or dial-up as a connection type, but you can make an educated guess by connection speed.

    Gmail does this and provides a link to a basic HTML version of their service if they detect it.

    Gmail Load basic html image

    My guess is that there is some client side javascript polling done on AJAX requests. If the turnaround time surpasses a threshold, the option to switch to "lite" appears.

    The best part about this option is that you allow the user to choose if they want to use the lite version instead of forcing them.

    voyager : +1 for loading the 'heavy' site while providing a way to the 'lite' version
  • I think the typical approach to this is just to ask the user. If you don't feel confidant that your users will provide an accurate answer, I suspect you'll have to write an application that runs a speed test on the client. Typically these record how long it takes the client to receive x number of bytes, and use that to determine bandwidth.

    Actionscript 3 has a library to help you with this task, but I believe it requires you to deploy your flex/flash app on Flash Media Server. See ActionScript 3.0 native bandwidth detection for details.

  • Here's a short code snippet from a code who attempted something similar. It's in C#, but it's pretty short and it's just the concept that's of interest.

    Determine the Connection Speed of your client

    Of course, they could be a temporary speed problem that has nothing to do with the user's connection at the time you test, etc, etc.

    I had a similar problem a couple of years ago and just let the user choose between the hi and lo bandwidth sites. The very first thing I loaded on the page was this option, so they could move on quickly.

  • @Apphacker (I'd comment instead of answering if I had enough reputation...):

    Can't guarantee the reverse, either--I have Earthlink dial-up, soon to upgrade to Earthlink DSL (it's what's available here...).

    Click Upvote : here, now you can comment. +1
    RolandTumble : Thanks! I was hanging just below the threshhold for a while there....

API for developers. What Content-Type should I use?

I'm developing now API for developers feature, which gives users API access to some functions of our product.

All responses are in json format.

HTTP Content-Type header in our server's response is text/plain; charset=utf-8

Should I send one of this:

  • application/json
  • text/x-json
  • text/json

instead of text/plain.

Or for API feature text/plain Content-Type is acceptable?

From stackoverflow

Branching Strategies

The company I work for is starting to have issues with their current branching model, and I was wondering what different kinds of branching strategies the community has been exposed to?

Are there any good ones for different situations? What does your company use? What are the advantages and disadvantages of them?

From stackoverflow
  • I'd highly encourage reading Eric Sink's opinion on the matter:

    Chapter 7: Branches

    I, like Eric, prefer the "folder" style branching that he talks about.

    pablo : Eric said: "Simple changes. As I mentioned above in my "Eddie" scenario, don't branch for every bug fix or feature." But with new SCMs out there, this is not true anymore. SVN guys used to say the same until they implemented proper branching... And GIT folks will never say that... But usually people behind a certain version control will say what they can't do is not worth doing... :-(
    Ryan Duffield : Eric is revising the HOWTO, and I believe he's even making it into a book. The popularity of systems like git and hg is a topic in some of his most recent blog posts as well.
  • This would depend on which Version Control System you're using. Each VCS has different approaches to branching.

    Which VSC do you use?

  • We currently have one branch for ongoing maintenance, one branch for "new initiatives" which just means "stuff that will come out sometime in the future; we're not sure when." We have also occasionally had two maintenance branches going on: one to provide fixes for what is currently in production and one that is still in QA.

    The main advantage we've seen is the ability to react to user requests and emergencies more rapidly. We can do the fix on the branch that is in production and release it without releasing anything extra that may have already been checked in.

    The main disadvantage is that we end up doing a lot of merging between branches, which increases the chance that something will get missed or merged incorrectly. So far, that hasn't been a problem, but it is definitely something to keep in mind.

    Before we instituted this policy, we generally did all development in the trunk and only branched when we released code. We then did fixes against that branch as needed. It was simpler, but not as flexible.

  • @Misha

    While the version control system might determine how you go about branching, it does not necessarily determine your branching strategy. To answer your question though, we use subversion.

    Stephen Darlington : I'm not sure that's entirely true in practice. Git practically encourages branching because it's an easy and quick operation and merging it relatively straight-forward. It's much harder in CVS. Not making a branch because it's hard work would form part of a branching strategy.
  • The philosophy that we follow at work is to keep the trunk in a state where you can push at any time without drastic harm to the site. This is not to say that the trunk will always be in a perfect state. There will of course be bugs in it. But the point is to never, ever leave it broken drastically.

    If you have a feature to add, branch. A design change, branch. There have been so many times where I thought, "oh I can just do this in the trunk it isn't going to take that long", and then 5 hours later when I can't figure out the bug that is breaking things I really wished that I had branched.

    When you keep the trunk clean you allow the opportunity to quickly apply and push out bug fixes. You don't have to worry about the broken code you have that you conveniently branched off.

  • For Subversion, I agree with Ryan Duffield's comment. The chapter he refers to provides a good analyses on which system to use.

    The reason I asked is that Perforce provides a completely different way to create branches from SVN or CVS. Plus, there are all the DVCSs that give it's own philosophy on branching. Your branching strategy would be dictated by which tool(s) you're using.

    FYI, Svnmerge.py is a tool to assist with merging branches in SVN. It works very well as long as you use it frequently ( every 10-30 ) commits, otherwise the tool can get confused.

  • We branch when a release is ready for final QA. If any issues are discovered during the QA process, the bugs are fixed in the branch, validated and then merged to the trunk. Once the branch passes QA we tag it as a release. Any hotfixes for that release are also done to the branch, validated, merged to the trunk and then tagged as a separate release.

    The folder structure would look like this (1 QA line, 2 hotfix releases, and the trunk):

    /branches

    /REL-1.0

    /tags

    /REL-1.0

    /REL-1.0.1

    /REL-1.0.2

    /trunk

  • Here is the method I've used in the past with good success:

    /trunk - bleeding edge. Next major release of the code. May or may not work at any given time.

    /branches/1.0, 1.1, etc. Stable maintenance branches of the code. Used to fix bugs, stabilize new releases. If a maintenance branch, it should compile (if applicable) and be ready for QA/shipping at any given time. If a stabilization branch, it should compile and be feature complete. No new features should be added, no refactoring, and no code cleanups. You can add a pre- prefix to indicate stabilization branches vs maintenance branches.

    /branches/cool_feature. Used for highly experimental or destructive work that may or may not make it into trunk (or a maintenance branch). No guarantees about code compiling, working, or otherwise behaving sanely. Should last the minimum time as possible before merging into the mainline branch.

    /tags/1.0.1, 1.0.2, 1.1.3a, etc. Used for tagging a packaged & shipped release. Never EVER changes. Make as many tags as you want, but they're immutable.

    swilliams : In your branches/cool_feature folders, do you branch the whole trunk or just certain sub-folders?
    jcoby : typically it's the whole trunk. rarely does a feature only touch one directory. i also highly recommend svnmerge.py if you aren't running svn 1.5. makes the whole branch/merge process much nicer.
    RjOllos : You present a very good well though out model. A policy that my company enforces, and I think this is fairly common, is that the trunk should always build and pass all test. So you say under your model that it 'may or may not work'. There are varying degrees of stabilization, and I think that enforcing the rule that the trunk should always build and automated tests should always pass is a good rule in general.
  • We use the wild, wild, west style of git-branches. We have some branches that have well-known names defined by convention, but in our case, tags are actually more important for us to meet our corporate process policy requirements.

    I saw below that you use Subversion, so I'm thinking you probably should check out the section on branching in the Subversion Book. Specifically, look at the "repository layout" section in Branch Maintenance and Common Branch Patterns.

  • Our repository looks like:

    /trunk
    /branches
    /sandbox
    /vendor
    /ccnet
    

    /trunk is your standard, bleeding edge development. We use CI so this must always build and pass tests.

    /branches this is where we put 'sanctioned' large changes, ie something we KNOW will make it into trunk but may need some work and would break CI. Also where we work on maintenance releases, which have their own CI projects.

    /sandbox each developer has their own sandbox, plus a shared sandbox. This is for things like "Lets add a LINQ provider to our product" type of tasks that you do when you are not doing your real work. It may eventually go into trunk, it may not, but it is there and under version control. No CI here.

    /vendor standard vendor branch for projects where we compile but it is not code that we maintain.

    /ccnet this is our CI tags, only the CI server can write in here. Hindsight would have told us to rename this to something more generic such as CI, BUILDS, etc.

  • The alternative I'm not seeing here is a "Branch on Change" philosophy.

    Instead of having your trunk the "Wild West", what if the trunk is the "Current Release"? This works well when there is only one version of the application released at a time - such as a web site. When a new feature or bug fix is necessary a branch is made to hold that change. Often this allows the fixes to be migrated to release individually and prevents your cowboy coders from accidentally adding a feature to release that you didn't intend. (Often it's a backdoor - "Just for development/testing")

    The pointers from Ben Collins are quite useful in determining what style would work well for your situation.

    Joeri Sebrechts : We used to have this model, but the constant merging back of changes from the branches turned out to be prohibitively complex. Now we use trunk for bleeding edge, and branches for stabilization and maintenance. This way no merging of trees is needed.
  • Henrik Kniberg's Version Control for Multiple Agile Teams also has some good points to take into consideration.

  • For the mother lode on branching patterns see Brad Appleton's Streamed Lines: Branching Patterns for Parallel Software Development. It's heavy duty but I haven't seen anything to surpass it in terms of breadth and depth of knowledge about branching.

  • Jeff Atwood wrote about this in a good blog post; that post has got some important links in it.

  • Read this classic: http://oreilly.com/catalog/practicalperforce/chapter/ch07.pdf

    1. One branch for the active development (/main or master, depending on the jargon)
    2. One branch for each maintenance release -> it will receive only really small fixes, while all major development goes to /main
    3. One branch for each new task: create a new branch to work on every new entry on your Bugzilla/Jira/Rally. Commit often, self document the change using inch pebble checkins, and merge it back to its "parent" branch only when it's finished and well tested.

    Take a look at this http://codicesoftware.blogspot.com/2010/03/branching-strategies.html for a better explanation

  • No matter which branching pattern chosen, you should try to keep your branches in a binary tree form like this:

       trunk - tags
         |
        next
       /  \  \
    bugfix  f1  f2
            /   \  \          
           f11    f21 f22
    
    • Child nodes should only merge with the direct parent.
    • Try ur best to merge only the whole branch with the parent branch. never merge subfolders within a branch.
    • You may cherry pick commits when needed as long as you only merge and pick from whole branch.
    • The next branch in the above figure is only for illustration, you may not need it.

Need to build (or otherwise obtain) python-devel 2.3 and add to LD_LIBRARY_PATH

I am supporting an application with a hard dependency on python-devel 2.3.7. The application runs the python interpreter embedded, attempting to load libpython2.3.so - but since the local machine has libpython2.4.so under /usr/lib64, the application is failing.

I see that there are RPMs for python-devel (but not version 2.3.x). Another wrinkle is that I don't want to overwrite the existing python under /usr/lib (I don't have su anyway). What I want to do is place the somewhere in my home directory (i.e. /home/noahz/lib) and use PATH and LD_LIBRARY_PATH to point to the older version for this application.

What I'm trying to find out (but can't seem to craft the right google search for) is:

1) Where do I download python-devel-2.3 or libpython2.3.so.1.0 (if either available)

2a) If I can't download python-devel-2.3, how do I build libpython2.3.so from source (already downloaded Python-2.3.tgz and

2b) Is building libpython2.3.so.1.0 from source and pointing to it with LD_LIBRARY_PATH good enough, or am I going to run into other problems (other dependencies)

3) In general, am I approaching this problem the right way?

ADDITIONAL INFO:

  • I attempted to symlink (ln -s) to the later version. This caused the app to fail silently.

  • Distro is Red Hat Enterprise Linux 5 (RHEL5) - for x86_64

From stackoverflow
  • Can you use one of these rpm's? What specific distro are you on?

  • You can use the python RPM's linked to from the python home page ChristopheD mentioned. You can extract the RPM's using cpio, as they are just specialized cpio archives. Your method of extracting them to your home directory and setting LD_LIBRARY_PATH and PATH should work; I use this all the time for hand-built newer versions of projects I also have installed.

    Don't focus on the -devel package though; you need the main package. You can unpack the -devel one as well, but the only thing you'll actually use from it is the libpython2.3.so symlink that points to the actual library, and you can just as well create this by hand.

    Whether this is the right approach depends on what you are trying to do. If all you're trying to do is to get this one application to run for you personally, then this hack sounds fine.

    If you wanted to actually distribute something to other people for running this application, and you have no way of fixing the actual application, you should consider building an rpm of the older python version that doesn't conflict with the system-installed one.

    noahz : googled for "how to make libpython2.3.so" (no quotes) - this page was the first hit: http://www.czaplinski.net/sun/#python - it worked. Thanks!

Creating a Capistrano task that performs different tasks based on role

I'm looking for a way to call a single Capistrano task to perform different things to different roles. Is Capistrano able to do this, or do I have write a specific task for each role?

From stackoverflow
  • The standard way to do this in Capistrano:

    task :whatever, :roles => [:x, :y, :z] do
      x_tasks
      y_tasks
      z_tasks
    end
    
    task :x_tasks, :roles => :x do
      #...
    end
    
    task :y_tasks, :roles => :y do
      #...
    end
    
    task :z_tasks, :roles => :z do
      #...
    end
    

    So yes, you do need to write separate tasks, but you can call them from a parent task and they will filter appropriately.

Using SQL to determine word count stats of a text field

I've recently been working on some database search functionality and wanted to get some information like the average words per document (e.g. text field in the database). The only thing I have found so far (without processing in language of choice outside the DB) is:

SELECT AVG(LENGTH(content) - LENGTH(REPLACE(content, ' ', '')) + 1)
FROM documents

This seems to work* but do you have other suggestions? I'm currently using MySQL 4 (hope to move to version 5 for this app soon), but am also interested in general solutions.

Thanks!

* I can imagine that this is a pretty rough way to determine this as it does not account for HTML in the content and the like as well. That's OK for this particular project but again are there better ways?

Update: To define what I mean by "better": either more accurate, performs more efficiently, or is more "correct" (easy to maintain, good practice, etc). For the content I have available, the query above is fast enough and is accurate for this project, but I may need something similar in the future (so I asked).

From stackoverflow
  • The text handling capabilities of MySQL aren't good enough for what you want. A stored function is an option, but will probably be slow. Your best bet to process the data within MySQL is to add a user defined function. If you're going to build a newer version of MySQL anyway, you could also add a native function.

    The "correct" way is to process the data outside the DB since DBs are for storage, not processing, and any heavy processing might put too much of a load on the DBMS. Additionally, calculating the word count outside of MySQL makes it easier to change the definition of what counts as a word. How about storing the word count in the DB and updating it when a document is changed?

    Example stored function:

    DELIMITER $$
    CREATE FUNCTION wordcount(str TEXT)
           RETURNS INT
           DETERMINISTIC
           SQL SECURITY INVOKER
           NO SQL
      BEGIN
        DECLARE wordCnt, idx, maxIdx INT DEFAULT 0;
        DECLARE currChar, prevChar BOOL DEFAULT 0;
        SET maxIdx=char_length(str);
        WHILE idx < maxIdx DO
            SET currChar=SUBSTRING(str, idx, 1) RLIKE '[[:alnum:]]';
            IF NOT prevChar AND currChar THEN
                SET wordCnt=wordCnt+1;
            END IF;
            SET prevChar=currChar;
            SET idx=idx+1;
        END WHILE;
        RETURN wordCnt;
      END
    $$
    DELIMITER ;
    

Lowest permission level to see the content of a file?

How can I see the contents of a file with 111 permissions? A thing called Y-combinator, as an input, prints the content of a file. My instinct says that you can run it with 100 permissions. However, I know only the theory, not the practise.

Which is the lowest permission level to see a file with Y-combinator in Bash?

The user nobody_ comments:

You don't make any sense. The Y combinator is used to create recursive functions and has nothing to do with permissions.

A question arises:

Which is the lowest permission level to see a file in Bash?

From stackoverflow
  • To execute a file (script or otherwise), you need to be able to load its content into memory, hence to have read access.

    So only leaving execution rights on your files is not going to allow anybody to read it. However, this is still a bad idea. Nothing that should not be executed should receive execution rights. In your position, I would be much more worried in accidentally executing a text file that begins with rm * than somebody using tricks to peek at my files.

    Masi : My file only has a line "rm *" with 700 permissions. I try to remove the content of the fonder, where the file is lacated. Do I do it like "./file"? It asks me a weird question: "override --------x user/staff for some_file?" What should I do to remove the content?
    Varkhan : This is something you probably should have asked as a separate question. However, the reason is simple: rm sees you don't have write rights on the file some_file, and asks you if you want to bypass that protection. Honestly, I think your whole file permission scheme is very weird...
  • I think you can't, and even the interpreter won't be able to (and therefore won't run it).

    However, you shouldn't be worried about people seeing your code; if there are eg. security flaws, you should fix them instead.

  • You can't read the contents of a file with those permissions.

    Permissions of '111' are 'execute only' and are almost useless on a regular file. In order for a file to be executed it needs at least read and execute by the owner, and in that case only the owner can read and execute it.

    If you are worried about others reading your files you probably want to use '500' that would be read and execute for just you.

    For more information and what these numbers mean (octal notation) you should read this page on Wikipedia: http://en.wikipedia.org/wiki/File_system_permissions#Octal_notation

    Cheers, Darryl

WPF button textwrap style

Hi,

How do I change the default textwrapping style of a button in WPF?

The obvious solution of:

<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="TextWrapping" Value="Wrap"></Setter>
</Style>

doesn't work, because Textwrapping isn't a settable property here, apparently.

If I try:

<Style x:Key="MyButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <TextBlock Text="{Binding}" Foreground="White" FontSize="20" FontFamily="Global User Interface" TextWrapping="Wrap"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

I just get a worthless response from the compiler:

Error   5 After a 'SetterBaseCollection' is in use (sealed), it cannot be modified.

Removing the ControlTemplate tag keeps the error.

The following attempt yields a different error:

    <Setter Property="TextBlock">
        <TextBlock Text="{Binding}" Foreground="White" FontSize="20" FontFamily="Global User Interface" TextWrapping="Wrap"/>
    </Setter>

Error   5 The type 'Setter' does not support direct content.

I see that I can set the textwrapping for each button individually, but that's pretty asinine. How can I do it as a style? What are the magic words?

And for future reference, where can I find a list of these magic words, so I can just do this on my own? The MSDN entry is pretty useless when I try to find out about which properties can be set by the setter.

From stackoverflow
  • Your second version should work, and does for me, with the caveat that you need to change the TextBlock Text binding:

    <!-- in Window.Resources -->
    <Style x:Key="fie" TargetType="Button">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type Button}">
            <TextBlock Text="{TemplateBinding Content}" FontSize="20" TextWrapping="Wrap"/>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
    
    <!-- then -->
    <Button Style="{StaticResource fie}">verylongcaptiongoeshereandwraps/Button>
    

    Note this completely replaces the button style (i.e. you will need to create your own button chrome if you want it).

    Regarding your second question, all writeable dependency properties can be set using a Setter. The reason you were unable to set TextWrapping on a Button via a style is that Button does not have a TextWrapping dependency property (or indeed any TextWrapping property). There are no "magic words," just the names of dependency properties.

    mmr : No, there are magic words. In this case, the magic words were "TemplateBinding Content." Thanks for letting me know.