Thursday, March 24, 2011

Trimming Mako output

I really like the Mako templating system that's used in Pylons and a couple other Python frameworks, and my only complaint is how much WS leaks through even a simple inheritance scheme.

Is there anyway to accomplish below, without creating such huge WS gaps... or packing my code in like I started to do with base.mako?

Otherwise to get a grip on what I'm trying to accomplish with below.

Base is kind of like interface class for all views for the entire application, layout is just a prototype idea for 3-4 different layout files ( tables, pure CSS, etc ), and controller/action is a test to make sure my idea's are sane.

Short summary of question: How to cut out the WS created in my Mako scheme?

Update: Is not a solution because it involves seeding all of my mako files with \'s http://www.makotemplates.org/docs/syntax.html#syntax_newline

/base.mako

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head><%def name="headtags()"></%def>${self.headtags()}</head>
  <body>
    <%def name="header()"></%def>${self.header()}${next.body()}<%def name="footer()"></%def>${self.footer()}
  </body>
</html>

/layout.mako

<%inherit file="/base.mako"/>
<%def name="headtags()">
   ${parent.headtags()}
   <script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js"></script>
</%def>
<%def name="header()">
  <h1>My Blogination</h1>
</%def>
<div id="content">${next.body()}</div>

/controller/action.mako

<%inherit file="/layout.mako" />
<%def name="headtags()">    
    <title> Hello world, templating system is 1 percent done</title>
    ${parent.headtags()}
</%def>
Hello ${c.name}!

rendered output:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>    
    <title> Hello world, templating system is 1 percent done</title>

   <script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js"></script>


</head>
  <body>

  <h1>My Blogination</h1>


<div id="content">

Hello Anonymous!

</div>

  </body>
</html>
From stackoverflow
  • Why does whitespace matter? The resulting HTML is still quite readable and the amount of WS has no effect on page size if your HTTP server is configured for gzip/compress.

    David : I'm trying to avoid issues with MSIE and other non-standard browsers. I've had serious problems with MSIE and layouts due to unintended WS.
  • Found my own answer http://www.makotemplates.org/docs/filtering.html

    Casey : so what technique are using to solve this?

Xpath query and time

Hi, this is the content of my XML file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<mainNode>
    <sub time="08:00">
     <status id="2">On</status>
     <status id="3">Off</status>
    </sub>
    <sub time="13:00">
     <status id="4">On</status>
     <status id="7">On</status>
    </sub>
    <sub time="16:00">
     <status id="5">On</status>
     <status id="6">On</status>
     <status id="7">Off</status>
     <status id="8">On</status>
    </sub>
    <sub time="20:00">
     <status id="4">Off</status>
     <status id="7">On</status>
    </sub>
    <sub time="23:59">
     <status id="4">On</status>
     <status id="7">On</status>
    </sub>
</mainNode>

My program gets the current time: if I get 15.59, I must retrieve all the status id of the next sub time (16.00):

<sub time="16:00">
     <status id="5">On</status>
     <status id="6">On</status>
     <status id="7">Off</status>
     <status id="8">On</status>
    </sub>

With a second XPath query I must get all the status id of the previous sub time (13.00). How to do it? I know SQL but I'm quite new to XPath. I accept urls to serious XPath resources too, if any. Thanks!

From stackoverflow
  • If you generate the xml yourself, you could change the way you store the time attribute using an integer value (ticks, for example), then you could do an easy numerical comparison using something like

    //sub[@time > 1389893892]
    
  • Well as long as the time is HH:MM something like the following should work: (I must excuse my syntax since I'm just dabbling without running, consider this pseudo-xpath):

    xmlns:fn="http://www.w3.org/2005/02/xpath-functions"
    
    //sub[fn:compare(@time,'12:59') > 0][1]/status
    

    This should select all the elements where time is greater than 12:59 and then select the first of those elements.

    You could also pass the value '12:59' as an external parameter into the xpath evaluation.

    Dimitre Novatchev : It is simpler to just use "lt" and "gt" when comparing two xs:time values.
  • Here is the ugly Xpath 1.0 solution:-

    sub[number((substring-before(@time, ':')) * 60 + number(substring-after(@time, ':'))) &gt; 959][1]
    

    Note 959 = 15 * 60 + 59 which I'm sure you can do in your calling code.

    Give that node the previous node can be accessed as:-

    preceding-sibling::sub[1]
    

    However a pragmatic, common sense solution would be to load the XML data into a set of data structures and use a language more suited to this task to look the items up.

    Tomalak : For an XPath solution, that's the way to go. It's not even especially ugly, IMHO. +1
    AnthonyWJones : @Tomalak: I must admit I was expecting it to be much uglier but you're right its not actually that bad.
    Dimitre Novatchev : No need to calculate the total seconds -- see my answer.
  • Here are two solutions:

    I. XPath 1.0

    This is one pair of XPath 1.0 expressions that select the required nodes:

    /*/*
        [translate(@time, ':','') 
        > 
         translate('15:59',':','')
        ][1]
    

    selects the first sub node with time later than 15:59.

    /*/*
        [translate(@time, ':','') 
        < 
         translate('15:59',':','')
        ][last()]
    

    selects selects the first sub node with the previous than 15:59 sub time.

    We can include these in an XSLT transformation and check that the really wanted result is produced:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output omit-xml-declaration="yes"/>
    
        <xsl:template match="/">
          First time after 15:59: 
          <xsl:copy-of select=
           "/*/*
              [translate(@time, ':','') 
             > 
               translate('15:59',':','')
              ][1]
          "/>
    
          First time before 15:59: 
          <xsl:copy-of select=
           "/*/*
              [translate(@time, ':','') 
             &lt; 
               translate('15:59',':','')
              ][last()]
          "/>
      </xsl:template>
    </xsl:stylesheet>
    

    When the above transformation is applied on the originally provided XML document:

    <mainNode>
        <sub time="08:00">
         <status id="2">On</status>
         <status id="3">Off</status>
        </sub>
        <sub time="13:00">
         <status id="4">On</status>
         <status id="7">On</status>
        </sub>
        <sub time="16:00">
         <status id="5">On</status>
         <status id="6">On</status>
         <status id="7">Off</status>
         <status id="8">On</status>
        </sub>
        <sub time="20:00">
         <status id="4">Off</status>
         <status id="7">On</status>
        </sub>
        <sub time="23:59">
         <status id="4">On</status>
         <status id="7">On</status>
        </sub>
    </mainNode>
    

    the wanted result is produced:

      First time after 15:59: 
    
    
    <sub time="16:00">
         <status id="5">On</status>
         <status id="6">On</status>
         <status id="7">Off</status>
         <status id="8">On</status>
    </sub>
    
      First time before 15:59: 
    
     <sub time="13:00">
         <status id="4">On</status>
         <status id="7">On</status>
     </sub>
    

    Do note the following:

    1. The use of the XPath translate() function to get rid of the colons

    2. The use of the last() function in the second expression

    3. There is no need to convert the time to seconds before the comparison

    4. When used as part of an XML document (such as an XSLT stylesheet, the < operator must be escaped.

    II. XPath 2.0

    In XPath 2.0 we can use the following two expressions to produce select the desired nodes:

    /*/*[xs:time(concat(@time,':00')) 
        gt 
         xs:time('15:59:00')
        ][1]
    

    selects the first sub node with time later than 15:59.

    /*/*[xs:time(concat(@time,':00')) 
       lt 
         xs:time('15:59:00')
        ][last()]
    

    selects selects the first sub node with the previous than 15:59 sub time.

    We can include these in an XSLT 2.0 transformation and check that the really wanted result is produced:

    <xsl:stylesheet version="2.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xsl:output omit-xml-declaration="yes"/>
    
        <xsl:template match="/">
          First time after 15:59: 
          <xsl:copy-of select=
           "/*/*[xs:time(concat(@time,':00')) 
               gt 
                 xs:time('15:59:00')
                 ][1]
          "/>
    
          First time before 15:59: 
          <xsl:copy-of select=
           "/*/*[xs:time(concat(@time,':00')) 
               lt 
                 xs:time('15:59:00')
              ][last()]
          "/>
        </xsl:template>
    </xsl:stylesheet>
    

    When the above transformation is applied on the originally provided XML document (the same as in the first solution), the same wanted result is produced.

    Do note the following:

    1. In XPath 2.0 xs:time is a native data type. However, in order to construct an xs:time() from the values in the xml document, we have to concat to them the missing seconds part.
    2. In XPath 2.0 xs:time values can be compared with the "atomic-value comarison operators" such as lt or gt.
    Dimitre Novatchev : @vyger Thanks for pointing this -- some weird bug in the "code" button. The indexes in the XSLT 2 code were shown as 7 and 8. Just edited it, if this continues, I will edit further and will not use the code button.
    Dimitre Novatchev : @vyger See: http://tinyurl.com/dd4tsy This is about XSLT books, but they cover by necessity XPath very well. My favourite is Michael Kay and his latest book on both XSLT 2.0 and XPath 2.0. BTW, if you think and answer is what you wanted, you can select it (click the check mark)
    AnthonyWJones : +1. Nice answer. Good to see you on SO as well ;)
    Dimitre Novatchev : @AnthonyWJones Thanks, SO is a very nice Q&A site.

C++ Why is this vector access giving a Runtime Error?

Hi, I've singled out a runtime error on this line of my code:

for (synsAuxCopyIndex=1; synsAuxCopyIndex<synsAux.size(); synsAuxCopyIndex++)

Which is runnhing inside the pushSynonyms(string synline, vector<WordInfo> &wordInfoVector) function. I don't get why is this particular line generating the error, as I don't think I'm indexing anything out of range.

The debugger is saying:

Uncontrolled Exception 0x00411cbf in program.exe: 0xC0000005: Infracción de acceso al leer la ubicación 0x00000000.

I guess "Infracción de acceso" will translate as Unauthorized access on an English debugger.

The input file is

dictionary.txt

1 cute
2 hello
3 ugly
4 easy
5 difficult
6 tired
7 beautiful
synonyms
1 7
7 1
antonyms
1 3
3 1 7
4 5
5 4
7 3


#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

class WordInfo{
 public:                
  WordInfo() {}

  ~WordInfo() {}

  int id() const { return myId; }

  void readWords(istream &in) {
    in >> myId >> word;     
  }

  vector<int>& getSynonyms () {
    return mySynonyms;
  }

  vector<int>& getAntonyms() {
     return myAntonyms;
  }

  string getWord() {
    return word;
  }

  void pushSynonyms (string synline, vector<WordInfo>& wordInfoVector) {
    stringstream synstream(synline);
    vector<int> synsAux;
    int num;
    while (synstream >> num)
      synsAux.push_back(num);
    int wordInfoVectorIndex;
    int synsAuxCopyIndex;
    for (wordInfoVectorIndex=0;
         wordInfoVectorIndex < wordInfoVector.size();
         wordInfoVectorIndex++) {
      if (synsAux[0] == wordInfoVector[wordInfoVectorIndex].id()) {
        // this is the line that's generating a Runtime Error, Why?                                                       
        for (synsAuxCopyIndex = 1;
             synsAuxCopyIndex < synsAux.size();
             synsAuxCopyIndex++) {
//        wordInfoVector[wordInfoVectorIndex].mySynonyms.push_back(
//            synsAux[synsAuxCopyIndex]);
        }                                                          
      }     
    }
  }

  void pushAntonyms (string antline, vector<WordInfo> wordInfoVector) {
    stringstream antstream(antline);
    vector<int> antsAux;
    int num;
    while (antstream >> num)
      antsAux.push_back(num);
//    for (int i=0; i<antsAux.size(); i++){
//      cout<<antsAux[i]<<endl;  
//    }
  }

  //--dictionary output function
  void printWords (ostream &out) {
    out<<myId<< " "<<word;     
  }

  //--equals operator for String
  bool operator == (const string &aString) const {
    return word ==aString; 
  }

  //--less than operator
  bool operator < (const WordInfo &otherWordInfo) const {
    return word<otherWordInfo.word;
  }

  //--more than operator
  bool operator > (const WordInfo &otherWordInfo) const {
    return word>otherWordInfo.word;
  }

 public: 
  vector<int> mySynonyms;
  vector <int> myAntonyms;

 private:
//vector <int> mySynonyms;
//vector <int> myAntonyms;
  string word;
  int myId;
};

//--Definition of input operator for WordInfo
istream & operator >> (istream &in, WordInfo &word) {
  word.readWords(in); 
}

//--Definition of output operator
ostream & operator << (ostream &out, WordInfo &word) {
  word.printWords(out);  
}

int main() {
  string wordFile;
  cout << "enter name of dictionary file: ";
  getline (cin, wordFile);
  ifstream inStream(wordFile.data());
  if (!inStream.is_open()) {
    cerr << "cannot open " << wordFile << endl; 
    exit(1);                      
  }

  vector <WordInfo> wordInfoVector; 
  WordInfo aword;

  while (inStream >> aword && (!(aword == "synonyms")))
    wordInfoVector.push_back(aword);

  inStream.clear();

  int i=0;          
  while (i < wordInfoVector.size()) {
    cout << wordInfoVector[i] << endl;
    i++;
  }

  vector <int> intVector;
  string synLine; 

  while (getline(inStream, synLine) && (synLine != ("antonyms")))
    aword.pushSynonyms(synLine, wordInfoVector);

  int theIndex;
  for (theIndex=0;
       theIndex < wordInfoVector[0].mySynonyms.size();
       theIndex++)
    cout << "the synonyms of 1 are " <<
        wordInfoVector[0].mySynonyms[theIndex] << endl;
  cout << "the size of mySynonyms of 1 is " <<
      wordInfoVector[0].mySynonyms.size() << endl;

  for (theIndex=0;
       theIndex < wordInfoVector[0].mySynonyms.size();
       theIndex++)  
    cout << "the synonyms of 7 are " <<
        wordInfoVector[6].mySynonyms[theIndex] << endl;
  cout << " the size of mySynonyms of 7 is " <<
      wordInfoVector[6].mySynonyms.size() << endl;

  string antLine;
  while (getline(inStream, antLine))
    aword.pushAntonyms(antLine, wordInfoVector);    

  wordInfoVector[0].mySynonyms.push_back(1);
  system("PAUSE");
  return 0;          
}
From stackoverflow
  • If you add some debugging output to your program, you'd notice you never enter or even reach the "culprit" for-loop.

    The problem is actually the test against "synsAux[0]" right above it - synxAux.size() is zero, so accessing the zeroth element is an index out-of-bounds error.

Access to a single pixel in C#

Hello, I'm working on a school project right now. It's meant to be a 3D editing software, (like a very minimized version of Maya). I have to write it in C#, using the 'Windows Application Project'. I intend to implement all the 3D stuff myself, from projections to shading. My question is, how can I get direct access to a single pixel in the C# windows application? I know I'm going to have a main view port in the window. But I haven't decided yet how it will be made. Is there a built in object that I can use, that will let me define the boundaries of the view port, and then paint each pixel individually? (I'm just shooting in the dark here, I don't know much about C#. I mostly program in C)

Thanks, Malki.

From stackoverflow
  • Christian Graus has several good graphic-manipulation articles on www.CodeProject.com. They should answer your questions.

  • Using the CreateGraphics() method, this should work:

    Bitmap b = new Bitmap(this.CreateGraphics(), Width, Height);
    //pixel is:
    Color c = b.GetPixel(x, y);
    

    To set a pixel to a specific colour, use this instead of Color c = b.GetPixel(x,y):

    b.SetPixel(x, y, c); // where c is a Color
    

    If you want a viewport, place a panel or a PictureBox (maybe with Dock: Fill), then use:

    Bitmap b = new Bitmap(viewport.CreateGraphics(), viewport.Width, viewport.Height);
    

    instead of the first line used previously.

    But from what you want to do, I think it would be better to use the OnPaint event:

    void pnlViewport_Paint(object sender, PaintEventArgs e) {
        if ( e.ClipRectange.Width < 1 || e.ClipRectangle.Height < 1 ) return;
        Bitmap b = new Bitmap(e.Graphics, e.ClipRectangle.Width, e.ClipRectangle.Height)
        // ...
    }
    

    This event fires every time the control needs painted. The first line checks whether the area being drawn is empty - this will not just save CPU time, but your application may crash - you are getting it to make a 0x0 bitmap.

    EDIT: Yes, this is resizable, if Dock = DockStyle.Fill; As your window resizes, the control expands to fill the space. It is then repainted - firing the event.

    EDIT 2: As pointed out by others, this is still slow. It does sound like it is a requirement to do the 3D drawing yourself, so maybe SDL.NET (which I think can use hardware acceleration) is the way to go. It even has a (slow) SurfaceControl to use.

  • If you are doing 3d it would make sense to make use of the hardware acceleration provided by almost every GPU under the sun these days. IF you are happy to stay with windows specific apis then DirectX's 3D API's are exposed via managed wrappers now. Also there is the XNA toolset designed primarily with games in mind but it provides a wealth of examples of how to do Direct-X 3D accelerated drawing from c#.

  • A 3D editor application needs not only to draw on the screen - it needs to do it damn fast. The standard approach in .NET would be using Bitmap and Graphics classes, like described by person-b. But this will be slow. You can improve speed by using Bitmap.LockBits() and clever optimizations, but all in all I doubt you will still get the performance you need. You'll eat away all the CPU and will still be stuck with less than some 10-20 FPS (number is a wild guess). Hardware acceleration is the real way to go. shuggycouk has links in the right direction.

    Unfortunately the hardware approach will also take away the projections and shading, because that's what the hardware is there for. So I don't know if you can use that... Perhaps seek information on "overlays" in .NET. That's the way all the media player software display movies. It's definately way faster than standard GDI/GDI+ and is still 2D. Or perhaps you could do a tricky workaround with DirectX - use it for drawing 2D shapes you define yourself. ;)

  • If you have a need for pixel speed, high-speed-graphing-control-for-net-or-mfc, may provide some tips.

  • i'm trying to do my project.this one to detect color but i have some idea. the first i read a pixel and compare it to color i want.but i don't know how to acess a pixel.My image is captured from mycamera.help meeeeeeeeeeeeeeeeee! thanks!

  • OMG MS realy fup this one, I want my pixelcontrol...

Import to the same Subversion repository directory multiple times?

For a number of reasons, the canonical source of some files I have can't be a working copy of the repository (the Subversion server is behind an inaccessible firewall, and the data doesn't natively exist in the file system in my preferred structure). So, I wrote a tool that downloads the data, generates a directory structure from it, and then uses SharpSvn to import the result into the repository. This works… the first time. The second time, of course, a directory of that name already exists in the repository, and I can't import again.

Instead, I could:

  1. Delete the directory, then import. I haven't tested this, but aside from sounding stupid, it probably also deletes the revision history of the respective files, regarding the new ones as entirely different.
  2. See if the directory exists, check it out, remotely delete the files that no longer exist in the canonical source, then replace the remainder and commit. This would probably work, but sounds error-prone and needlessly difficult.

Is my assumption correct that deleting files will mark new ones of the same ways as different files? Is there an easier way than the second approach?

From stackoverflow
  • Subversion has a loose connection to the files. For files inside a folder, you can easily do a get/update, make massive changes (including deleting, replacing, or adding files), then commit the differences. That file-level behavior is typical Subversion usage.

    The directories are slightly different. Subversion stores the repository information at the folder level. So, if you create a new folder, it won't automatically have a connection to Subversion. (Using something like TortoiseSvn, it takes care of much of that for you.)

    If you are going to be adding and deleting directories during the generation process, you'll have some slightly different issues than with the files themselves. But, you can still accomplish your goal through the command-line, SharpSvn, TortoiseSvn, or other similar tools.

  • For 1), it won't delete the revision history, but the new files will be treated as completely unrelated to the old ones. You still could get the old files back though.

    For 2), that would be the recommended way. But after 'svn delete'ing the existing files and adding the new ones, you also have to 'svn add' those new files before committing.

    But it seems you should consider using the svn-load-dirs.pl script. You can read about this in the Subversion book, chapter "Vendor branches".

  • Since you can't overwrite the directories or you'll destroy the .svn directory and lose all the repository information, you have to copy the files over but only create new directories. This is how I would do that, from within the subversion working copy you want to update:

    (cd <newdirectory> ; tar -cf - * ) | tar -xf -
    

    Granted you need a Unix-y system. It should work with Cygwin, unless Windows does something particularly bizarre with overwriting folders at the system level.

What are the important rules in Object Model Design

Hi

We are developing an extension (in C# .NET env.) for a GIS application, which will has predefined types for modeling the real world objects, start from GenericObject, and goes to more specific types like Pipe and Road with their detailed properties and methods like BottomOfPipe, Diameter and so on.

Surely, there will be an Object Model, Interfaces, Inheritance and lots of other essential parts in the TypeLibrary, and by now we fixed some of them. But as you may know, designing an Object Model is a very ambiguous work, and (I as much as I know), can be done in many different ways and many different results and weaknesses.

Is there any distinct rules in designing O.M.: the Hierarchy, the way of defining Interfaces, abstract and coclasses enums?

Any suggestion, reference or practice?

From stackoverflow
  • Check out the "principles" of Object oriented design. These have guidelines for all the questions you ask.

    References:

    Checkout the "Design Principles" articles at the above site. They are the best references available.

  • A couple of good ones:

    SOLID

    Single responsibility principle
    Open/closed principle
    Liskoff substitution principle
    Interface segregation principle
    Dependency inversion principle

    More information and more principles here: http://mmiika.wordpress.com/oo-design-principles/

    duffymo : Excellent recommendation. Anything by Bob Martin will be helpful.
  • I suggest you to read the following series of blog posts for Brad Adams, FrameWork Design Guidelines

  • "BottomOfPipe"? Is that another way of saying the depth of the Pipe below the Road?

    Any kind of design is difficult and can be done different ways. There are no guarantees that your design will work when you create it.

    The advantage that people who design ball bearings and such have is many more years of experience and data to determine what works and what does not. Software doesn't have as much time or hard data.

    Here's some advice:

    1. Inheritance means IS-A. If that doesn't hold, don't use inheritance.
    2. A deep hierarchy is probably a sign of trouble.
    3. From Scott Meyers: Make non-leaf classes interfaces or abstract.
    4. Prefer composition to inheritance.
  • Check out Domain-Driven Design: Tackling Complexity in the Heart of Software. I think it will answer your questions.

  • Thanks all I'm working on your answers Thanks again

  • what they said, plus it looks like you are modeling real-world entities, so:

    • restrict your object model to exactly match the real-world entities.

    You can use inheritance and components to reduce the code/model, but only in ways that make sense with the underlying domain.

    For example, a Pipe class with a Diameter property would make sense, while a DiameterizedObject class (with a Diameter property) with a GeometryType property of GeometryType.Pipe would not. Both models could be made to work, but the former clearly corresponds to the problem domain, while the latter implements an artificial (non-real-world) perspective.

    One additional clue: you know you've got the model right when you find yourself discovering new features in the code that you didn't plan from the start - they just 'naturally' fall out of the model. For example, your model may have Pipe and Junction classes (as connectivity adapters) sufficient to solve the immediate problem of (say) joining different-diameter pipes to each other and calculating flow rates, maximum pressures, and structural integrity. You later realize that since you modeled the structural and connectivity properties of the Pipes and Junctions accurately (within the requirements of the domain) you can also create a JungleGym object from connected pipes and correctly calculate how much structural load it will bear.

    This is an extreme example, but it should get the point across: correct object models support extension and often manifest beneficial unexpected properties and features (not bugs!).

  • The Liskov Substitution Principle, often expressed in terms of "is-a". Many examples of OOP would be better off making use of "has-a" (in c++ private inheritance or explicit composition) rather than public inheritance ("is-a")

    Getting Inheritance right is hard. Doing so with interfaces (pure virtual classes) is often easier than for base/sub classes

What time of day are you most proficient at programming?

For some reason I find that I am at my best, in terms of programming, late at night. I'm not sure if it's because I lose my inhibition that keeps me from pushing forward or if there is something else to it but either way I get the most done after the sun goes down.

What time of day do you find yourself the most proficient in terms of programming?

(Feel free to help fix these tags...)

From stackoverflow
  • Mostly the "non core" hours when people aren't coming in to ask questions, socialize, or discuss something. So mostly before 9:00AM and after 5:00 PM. I try to structure my work day to get in early or leave later.

  • I used to code best in the middle of the night(sometime after midnight). Maybe I am turning into an old man. I get my best work done early in the morning now before my co-workers(and myself!) get real chatty and I get tired.

    Ryan Thames : +1 - I get my best code done in the morning before my coworkers get there as well :)
    Unkwntech : +1 for LATE!!! at night, well after my phone stops ringing and after my messengers stop buzzing, and the interesting stuff goes off the TV.
    Chad Braun-Duin : +1 for early in the morning, too
    Joe Philllips : +1 for late at night
  • On the train at 7am where no one can bug me, and after a coffee.

    I am lucky to have a 1 hour commute, and get my best work done in that time. The afternoon is also productive returning home, but I am sometimes a bit more frazzled than in the morning, and can sometimes switch off (psychologically as well as technologically).

  • I depends on person's biorhythm. AFAIK there are 2 most common patterns - high mental activity "early in the morning + midnight" and "second half of the day". I'm good at morning/midnight and less productive after 2 p.m. Between 3 p.m and 5 p.m I'm almost useless :)

    Joe Philllips : Do you track your biorhythm somehow?
    aku : no, but I try to schedule my tasks in a way to solve hard tasks in the morning
    Unkwntech : +1, my schedule is identical, I usualy just sleep between 2pm-6pm, which is all the sleep Ill get.
  • If it's while I'm at work usually from 9 to 2 since I've woken up and haven't started to get mental burnout. If it's for school when ever I get in the mood. Since I'm not on a strict schedule like work and prioritys of what needs to get done when are constently changing I find it harder to set aside a given block of time to code so I have no most productive time.

  • I work best in 3-4 hour blocks. Those seem to happen most late at night.

    I have found during the day my noise canceling headphones help me get into the 'flow' a lot more. I find I need to be able to hear my thoughts for certain problems, while things I have done already I can do pretty much any time.

  • i work best during early morning. try to get up early, have some cofee and then some non-stop work..

  • I usually code between 9AM and 1PM. In the afternoon I will have meetings or peer code reviews.

  • After lunch. I'm sluggish in the mornings, and usually look for light work to do until noon. Then in the afternoon I'm ready to really concentrate.

    Joe Philllips : That seems like it may be the opposite of what most people would say. Interesting :)
  • I find that I'm better at creative problem solving and doing something small but complex late in the day when ideas have had a chance to accumulate in my mind and I can hold a lot in my head at once. However, I'm better at implementation, detail-oriented work, and just "getting things done" early in the day when I have just had a good night's sleep and my mind is clear.

  • About 15 minutes after a good strong coffee

  • I must also answer that it depends on my biorhythm and that is affected by light and what time of year it is. But mainly since my personal day is approx 25h and not 24h, the peak moves from month to month...

    But normally after the right amount of sleep (not to much and not to little), I get up grab a cup of coffee and start the computer... and there we usually have a very good hour or so.

    Then depending on my biorhythm the other "flows" moves around.

    /Johan

  • I tend to be best in the late afternoon to early evening hours. Though I don't have the urge to program while I'm thinking about it and I don't have the urge to think about it while programming.

  • Historically, my best and most prolific coding times have been after dark and well into the night. However, I think this is mostly a function of having less distractions. While the rest of the world (in my time zone/country) is not busy making news or getting their jobs accomplished, I have more of an opportunity to find the "zone". Even so, in certain cases, I will wake up the next day totally consumed by a problem and continue where I left off, even in the morning, depending on if others don't stop me from reconnecting with the zone.

    I think a useful twist to this question would be:

    What helps you or detracts you from finding your coding zone?

  • the times when i am not on stackoverflow.com

  • Weekend afternoons. Not week*days*. Please don't ask me to do programming after school. I just can't concentrate - my attention just...look! Is that a flying pig?

  • In the morning hours - 6-12 - where I can put the ideas I've had in my sleep into practice

Queryset API distinct() does not work?

class Message(models.Model):
    subject = models.CharField(max_length=100)
    pub_date = models.DateTimeField(default=datetime.now())

class Topic(models.Model):
    title = models.CharField(max_length=100)
    message = models.ManyToManyField(Message, verbose_name='Discussion')

I want to get order all the topics according to the latest message object attached to that topic. I executed this query but this does not give the distinct queryset.

>> Topic.objects.order_by('-message__pub_date').distinct()
From stackoverflow
  • What is your query? Try <QuerySet>.query.as_sql() to see raw SQL

  • You'll find the explanation in the documentation for .distinct().

    I would de-normalize by adding a modified_date field to the Topic model and updating it whenever a Message is saved or deleted.

  • You don't need distinct() here, what you need is aggregation. This query will do what you want:

    from django.db.models import Max
    Topic.objects.annotate(Max('message__pub_date')).order_by('-message__pub_date__max')
    

    Though if this is production code, you'll probably want to follow akaihola's advice and denormalize "last_message_posted" onto the Topic model directly.

    Also, there's an error in your default value for Message.pub_date. As you have it now, whenever you first run the server and this code is loaded, datetime.now() will be executed once and that value will be used as the pub_date for all Messages. Use this instead to pass the callable itself so it isn't called until each Message is created:

    pub_date = models.DateTimeField(default=datetime.now)
    

Copy an object in Java

I have an object that I need to copy in Java. I need to create a copy and run some tests on it without changing the original object itself.

I assumed that I needed to use the clone() method, but this is protected. Having done some research on the net, I can see that this can be overrided with a public method in my class, but I cannot find an explanation of how to do this. How would this be done?

Also, is this the best way of achieving what I need?

From stackoverflow
  • Some options:

    • You can implement Cloneable for your object and put clone() method as public. See full explanation here: http://www.cafeaulait.org/course/week4/46.html However, this produces a shallow copy and might be not something you want.
    • You can serialize and deserialize your object. You will need to implement Serializable interface for the object and all its fields.
    • You can use XStream to perform serialization via XML - you won't have to implement anything here.
  • For test code Serialization is maybe the safest answer, especially if the object is already Serializable try Apache Commons SerializationUtils for an implementation.

  • Another option by using Copy Constructor

    public final class Galaxy {
    
    public Galaxy (double aMass, String aName) {
     fMass = aMass;
     fName = aName;
    }
    
    /**
    * Copy constructor.
    */
    public Galaxy(Galaxy aGalaxy) {
    this(aGalaxy.getMass(), aGalaxy.getName());
    //no defensive copies are created here, since 
    //there are no mutable object fields (String is immutable)
    }
    
    /**
    * Alternative style for a copy constructor, using a static newInstance
    * method.
    */
    public static Galaxy newInstance(Galaxy aGalaxy) {
      return new Galaxy(aGalaxy.getMass(), aGalaxy.getName());
    }
    
    public double getMass() {
      return fMass;
    }
    
    /**
    * This is the only method which changes the state of a Galaxy
    * object. If this method were removed, then a copy constructor
    * would not be provided either, since immutable objects do not
    * need a copy constructor.
    */
    public void setMass( double aMass ){
      fMass = aMass;
    }
    
    public String getName() {
    return fName;
    }
    
    // PRIVATE /////
    private double fMass;
    private final String fName;
    
    /**
    * Test harness.
    */
    public static void main (String... aArguments){
    Galaxy m101 = new Galaxy(15.0, "M101");
    
    Galaxy m101CopyOne = new Galaxy(m101);
    m101CopyOne.setMass(25.0);
    System.out.println("M101 mass: " + m101.getMass());
    System.out.println("M101Copy mass: " + m101CopyOne.getMass());
    
    Galaxy m101CopyTwo = Galaxy.newInstance(m101);
    m101CopyTwo.setMass(35.0);
    System.out.println("M101 mass: " + m101.getMass());
    System.out.println("M101CopyTwo mass: " + m101CopyTwo.getMass());
    }
    }
    

    from: http://www.javapractices.com/topic/TopicAction.do?Id=12

    Norbert Hartl : I wouldn't use getter in the copy constructor. First you don't need them because you have always access to instance variables. Second if you make this yourself a practice you might lower encapsulation by creating too much getters. And finally it can be error prone. Imagine you tweak the value in getMass() than you would copy the tweaked value into the instance variable of the new object which would tweak the value a second time if you call getMass on the new object.
  • Joshua Bloch has some interesting things to say about cloneable. Depending on the size/construction of the object, I'd add a copy constructor to the object, or serialise/deserialise using one of the solutions mentioned above.

  • There are two popular approaches. One is to provide a clone method as you mentioned, like so.

    public class C implements Cloneable {
        @Override public C clone() {
            try {
                final C result = (C) super.clone();
                // copy fields that need to be copied here!
                return result;
            } catch (final CloneNotSupportedException ex) {
                throw new AssertionError();
            }
    }
    

    Pay attention to the "copy fields ... here!" part. The initial result is only a shallow copy, meaning that if there's a reference to an object, both the original and result will share the same object. For example, if C contains private int[] data you'd probably want to copy that.

    ...
    final C result = (C) super.clone();
    result.data = data.clone();
    return result;
    ...
    

    Note that you don't need to copy primitive fields, as their content is already copied, or immutable objects, as they can't change anyways.

    The second approach is to provide a copy constructor.

    public class C {
        public C(final C c) {
            // initialize this with c
        }
    }
    

    Or a copy factory.

    public class C {
        public static C newInstance(final C c) {
            return new C(c);
        }
    
        private C(final C c) {
            // initialize this with c
        }
    }
    

    Both approaches have their respective properties. clone is nice because its a method, so you don't have to know the exact type. In the end, you should always end up with a "perfect" copy. The copy constructor is nice because the caller has a chance to decide, as can be seen by the Java Collections.

    final List c = ... 
    // Got c from somewhere else, could be anything.
    // Maybe too slow for what we're trying to do?
    
    final List myC = new ArrayList(c);
    // myC is an ArrayList, with known properties
    

    I recommend choosing either approach, whichever suits you better.

    I'd use the other approaches, like reflective copying or immediate serializing/deserializing, in unit tests only. To me, they feel less appropriate for production code, mainly because of performance concerns.

Add ado.net data tables to a blank MDB file

I'm wondering how to add several data tables that I've built in code, to a blank MDB file that ships with my app.

I find it easy enough to connect to the MDB file via the OledbConnection object, but then I'm at a loss for an easy way to add my tables to it. There it sits...open...and empty, but what next?

I can add my tables to a dataset easily enough, but I still don't see an easy way to map the dataset over to my open MDB file.

Am I missing an easy fix for this? I'd like to avoid using ADOX if possible. I also see that it is very easy to write the dataset out as XML, but find no similar functionality for writing out an MDB file.

Any help would be appreciated.

From stackoverflow
  • DataSet intriniscally understands the XML storage, you might think of it (very loosely mind) as its native data store. In fact XML storage is meant for temporary or transistory storage so that a DataSet can be re-constituted between application re-starts or transference between machines.

    On the other hand, true persistance stores such as MDB, SQL Server, Oracle or whatever are entirely alien to the Dataset, thats why you need provider specific Adapters to transfer data to and from the Dataset.

    Unless you can find a project out in web-land where someone has done this before you are going to need to create your own code to analyse the dataset schema then send DDL to you MDB connection to create the tables.

how can I program in ASP.net using PHP like methods

I am a PHP programmer and I want to develop a website using visual web developer .net I knew that asp.net has many different approaches in dealing with database. However, I am familer with PHP way in programming for example:

$query = "select * from table where user_id > 2"
$result = mysql_query($query);

while($row = mysql_fetch_array($result)){

echo "username is: ".$row["username"]."<br>"; }

the question is "can I deal with asp.net in a PHP way?"

From stackoverflow
  • Sure...learn C# (somewhat similar syntax) Or develop using one of the following:

    Other .NET Powered Languages

    My honest suggestion to you is that if you want to develop ASP.NET or .NET in general, you should learn C#. I did, and I am happy having done it.

    roe : +1 for Phalanger, interesting project.
    ahmed : thanks for this useful information, I think I will try to learn C# because I like to adopt a mature technologies and these project you have listed here should be fine as a solution for PHP programmers however It seems that it is not well supported, thanks again
  • Maybe you should consider the way you are programming. Mixing logic, presentation and data access is not a good idea in any language. The example you showed is not "PHP way". Yes, it is possible to write like that in php but it's not a good practice. I think that by learning asp.net you may also learn a lot about programming and you really shouldn't try to program like that in asp.

    ahmed : thanks for the good advise, I believe you are right however sometimes I need to get things done no matter which way I use - esp if I have to hand in a university assignment :), thanks
  • There are a lot of ways in asp.net. It's a little confusing, but they are all basically the same. The difference is the type of data access you use.

    Using Enterprise Library (you need to download this) is probably one of the closest ways:

    Database database = DatabaseFactory.CreateDatabase();
    string sqlCommand = @"SELECT * FROM Users WHERE UserID=@UserID";
    System.Data.Common.DbCommand dbCommand = database.GetSqlStringCommand(sqlCommand);
    
    DataSet dataSetGroup = database.ExecuteDataSet(dbCommand);
    DataTable dt = dataSetGroup.Tables[0];
    
    foreach (DataRow dr in dt.Rows)
    {
        Response.Write("User name is: " + dr["Username"].ToString() + "<br/>");
    }
    

    Enterprise Library is just a Microsoft library that simplifies data access. If you do not use it you can access the database directly but it takes a few more lines that handle the opening and closing of a data connection and you have to be more careful about opening and closing connections.

    Other ways to do it are:

    1. Use strongly typed datasets
    2. Use LinqToSql or LinqToEntities and the ADO.net Framework

    They are all different but similar in structure where you'll query the database and then do a foreach (you can also use while) to loop through your data.

    I've been using LinqToSql recently and prefer that method. Scott Gu's blog has a good series of posts on how to use LinqToSql (linked to the last post so you can see all the previous ones): http://weblogs.asp.net/scottgu/archive/2007/09/07/linq-to-sql-part-9-using-a-custom-linq-expression-with-the-lt-asp-linqdatasource-gt-control.aspx

    ahmed : thanks for this useful information, and for the blog
  • C# 3.0 offers very powerful syntax that allows you to interact with the database in a way that is both natural and safe (using parameterized queries). Your query in C# with LINQ2SQL would look like:

    var query = from t in context.table
                where t.user_id > 2
                select t;
    

    You'd probably use a Repeater in your view so you could just provide your query as the datasource (there are ways to do this in mark up, too) in your codebehind.

    // use ToList so context can be disposed
    nameRepeater.DataSource = query.ToList();
    

    Your mark up would look like:

    <asp:Repeater runat="server" ID="nameRepeater">
       <ItemTemplate>
       username is:
       <asp:Label runat="server" 
                  ID="nameLabel"
                  Text='<%= Bind("username") %>' />
       <br />
    </asp:Repeater>
    

    (lines split for readability)

    Or you could use ASP.NET MVC in which case the selection code would be in your controller and you could use something more like your PHP-syntax in a strongly-typed view.

    <% foreach (Table t in ViewData.Model) { %>
       username is: <%= t.username %><br/>
    <% } %>
    
    ahmed : thanks, LINQ2SQL and MVC both are great, I think that is what I am looking for, Thanks

How do you post the contents of form to the page in which it is?

In Yahoo or Google and in many websites when you fill up a form and if your form has any errors it gets redirected to the same page. Note that the data in the form remains as it is. I mean the data in the text fields remains the same. I tried ‹form action="(same page here)" method="post or get"›. It gets redirected to the page, but the contents of the form gets cleared. I want the data to be displayed.

You know how tiresome it will be for the user if he has to fill up the entire form once again if he just forgets to check the accept terms and conditions checkbox. Need help!

From stackoverflow
  • You'll have to check the data within the same file, and if it is correct, then you redirect to the correct location. Then you can use the $_POST or $_GET information the user posted and he can fix the error(s).

  • You need to do this yourself. When the page gets posted you'll have access to all the form values the user entered via $POST['...']. You can then re-populate the form fields with this data.

  • This is not done automatically. They get values from post / get and then assign the values the user typed to the template. What you see is html that was generated from the script that handled user values.

  • You can use two approachs (they're not mutually exclusive):

    • Use JavaScript to help the user before he submits the form. That way, you save a roundtrip to the server. What you asked for:
    • In the form, fill the value attributes of the fields with the data sent back from the server. For example: you send a field name, which you get as $_POST['name'] in PHP (assuming you used method='post'. If you send back the data and modify that field adding value='<?php $_POST['name']; ?> you should get your data back.
  • If you put your form and the form data processing in the same script, you can easily print out the data that has already been entered in the form, e.g.:

    $valid = false;
    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        if (isset($_POST['name']) && $_POST['name'] == 'Hugo') {
            $valid = true;
        } else {
            echo '<p>Seriously, you have to enter "Hugo"!</p>';
        }
        // more data processing
        if ($valid) {
            echo '<p>Everything’s fine!</p>';
        }
    }
    if (!$valid) {
        echo '<form action="" method="post">';
        echo '<p>Please enter "Hugo": <input type="text" name="name" value="', (isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ''), '"></p>';
        echo '<p><input type="submit"></p>';
        echo '</form>';
    }
    

    Well this is not nice example but that’s how it works.

  • a lot of frameworks do this job for you, so dont waste your time doing this manually

  • Here is a modified version of what I use for very simple websites where I don't want/need an entire framework to get the job done.

    function input($name, $options = array()) {
     if(!isset($options['type'])) $options['type'] = 'text';
    
     $options['name'] = $name;
    
     if(isset($_POST[$name]) && $options['type'] != 'password') {
      $options['value'] = htmlspecialchars($_POST[$name]);
     }
    
     $opts = array(); 
     foreach($options as $key => $value) {
      $opts[] = $key . '="' . $value . '"';
     }
    
     return '<input ' . implode(' ', $opts) . '/>';
    }
    

    (I have a few similar functions for <select> and <textarea> and so on)

    When you're building fields you can do something like:

    First Name: <?=input('first_name')?>
    Last Name: <?=input('last_name')?>
    Password: <?=input('password', array('type' => 'password'))?>
    

    If you process your forms in the same page as the form itself, they will get auto filled if there are any errors. Most frameworks, though, do all of this for you (and in a much better way than the code above), I personally suggest CakePHP or CodeIgniter.

  • If you're using a template or framework system (I've incorporated the Smarty engine into several projects of mine), you can usually tweak the templates so they automatically fill fields with values if they detect that the $_POST[$variable] value corresponding to their field is set.

    As for the passwords, as far as I understand it (I could be wrong): it's a convention that minimizes the amount of time that password is being sent over the wire, hence shrinking the window for anyone who may be sniffing to pick up on the text. It's just good practice to leave password fields blank, is all.

jQuery- Call function when textarea wraps to a new line?

I'd like to increase the height of a textarea when the text wraps to a new line, so it dynamically expands to whatever it needs to be. I don't want to use the onscroll event because I want the expansion to always be one step ahead of the scrolling, so the textarea never scrolls until it reaches a max height of x.

Anyway I could do this?

From stackoverflow
  • I dont know of any event that happens on wrap, you might be able to just use a keypress or change function and count the characters entered since the last wrap, and if it's over a given amount you can increase the size of the textarea.

  • Numerous plug-ins already exist that do this. They probably don't do the maxheight deal, but you could add that in:

    if ($(this).height() >= maxHeight) {
      return false;
    }
    

How can I manually enter my credentials in SQL Server management Studio 2008 when using Windows Authentication ?

Usually, when using Windows Authentication, the software (eg Internet Explorer) tries to use Windows Authentication, and if it doesn't succeed (because the domain is untrusted, or because the credentials are not ok for example), it just popups a login window so that you can input your domain, login and password.

With sql Server Management Studio 2008, if I select "Windows Authentication", the login/password boxes are autopopulated with my current credentials, and if I try to login, it says :


Login failed. The login is from an untrusted domain and cannot be used with Windows authentication. (Microsoft SQL Server, Error: 18452)


I never get a chance to enter my credentials on the server domain...

I know I can fix this by either joining the domain on my computer, or enabling "sql server authentication", but that's not what I want :)

Any ideas on how to solve this?

From stackoverflow
  • Trusted Authentication will use the credentials that you are logged into the machine with to try and connect, there’s not a way round this, if you want to use trusted authentication for management studio, you need to be logged on to your machine with an account that is allowed access to SQL, if not then you will have to use SQL authentication.

    Edit: As posted by Gulzar, RunAs would be a definate solution to this.

    Brann : If I connect using internet explorer to IIS configured with Window Authentication, if my current credentials doesn't get me into the website, IE will popup a window, giving me a chance to enter other credentials... In other words, it seems to me the limitation is in SQLStudion, not NTLM. Am I wrong?
    Sam Cogan : Well its a limition in MSSQL itself, rather than Managment studio.
  • try this: Use RUNAS to set your Windows Auth domain for database connections

    runas /user:domain\user “C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\ssmsee.exe” 
    
    runas /user:domain\user “C:\WINDOWS\system32\mmc.exe /s \”C:\Program Files\Microsoft SQL Server\80\Tools\BINN\SQL Server Enterprise Manager.MSC\”" 
    
    runas /user:domain\user isqlw
    
    Brann : although the user I created on my local computer is not on the same domain as the server, it worked ! thanks :)

Database column sizes for character based data

I've just come across a database where all the character based columns have their size specified in multiples of 8 (e.g. Username = varchar(32), Forename = nvarchar(128) etc.) Would this have any bearing on the performance of the database or is it completely pointless?

Thanks.

N.B. This is in a SQL 2000 database.

From stackoverflow
  • Looks like premature optimisation to me.

    I don't think this will make any performance change on the database.

  • Since they are VARchars the actual space used is based on the content. I would start to worry if they were CHARs.

    -Edoode

  • It's probably just a habit of some old school developer. As it's said - varchar is as long as it needs to be, so 32 or 33 doesn't matter when string length is for example 22.

  • "Would this have any bearing on the performance of the database or is it completely pointless?"

    It has little bearing on performance (not none, but little). It can have an impact on spare space allocation, since the "largest" row size can be quite large with this scheme. Few rows per page can slow down large table retrievals.

    The difference, however, is usually microscopic compared to improper indexing.

    And getting the sizes "right" is not worth the time. In the olden days, old-school DBA's sweated over every character. Disks used to be expensive, and every byte had a real $$$ impact on cost.

    Now that disk is so cheap, DBA time wasted in fussing over sizes costs more than the disk.

  • Would you have the same concern if they were all multiples of 5 or 10, which is what happens in the normal case.?

  • I've never seen it make a difference in the table storage, joins or basic operations.

    But I have seen it make a difference where string processing is involved.

    In SQL Server 2005, the only case I've seen where varchar size makes significant differences is when things are cast to varchar(MAX) or in UDFs. There appears to be some difference there - for instance, I have a system/process I'm porting with a number of key columns that have to be concatenated together into another pseudo keyfield (until I am able to refactor out this abomination) and the query performed significantly better casting the result as varchar(45) as soon as possible in the intermediate CTEs or nested queries.

    I've seen cases where a UDF taking and returning a varchar(MAX) performs significantly more poorly than one taking and returning varchar(50), say. For instance, a trimming or padding function which someone (perhaps me!) was trying to make future proof. varchar(MAX) has it's place, but in my experience it can be dangerous to performance.

    I do not think I profiled a difference between varchar(50) and varchar(1000).