Friday, September 17, 2010
Display resolution of LCD monitors.
Monday, June 14, 2010
The origin of the term Endianness
"On Holy Wars and a Plea for Peace"[2] by Danny Cohen ends with: "Swift's point is that the difference between breaking the egg at the little-end and breaking it at the big-end is trivial. Therefore, he suggests, that everyone does it in his own preferred way. We agree that the difference between sending eggs with the little- or the big-end first is trivial, but we insist that everyone must do it in the same way, to avoid anarchy. Since the difference is trivial we may choose either way, but a decision must be made."
Saturday, June 12, 2010
A simple program for FTP directory listing using pycurl
- #import pycurl
- import pycurl
- import StringIO
- # lets create a pycurl object
- c = pycurl.Curl()
- # lets specify the details of FTP server
- c.setopt(pycurl.URL, r'ftp://ftp.ncbi.nih.gov/refseq/release/')
- # lets create a buffer in which we will write the output
- output = StringIO.StringIO()
- # lets assign this buffer to pycurl object
- c.setopt(pycurl.WRITEFUNCTION, output.write)
- # lets perform the LIST operation
- c.perform()
- # lets get the output in a string
- result = output.getvalue()
- # lets print the string on screen
- print result
- # FTP LIST output is separated by \r\n
- # lets split the output in lines
- lines = result.split('\r\n')
- # lets print the number of lines
- print len(lines)
- # lets walk through each line
- for line in lines:
- # lets print each part separately
- parts = line.split()
- # we can print the parts now
- print parts
- # the individual fields in this list of parts
- if not parts: continue
- permissions = parts[0]
- group = parts[2]
- user = parts[3]
- size = parts[4]
- month = parts[5]
- day = parts[6]
- yearortime = parts[7]
- name = parts[8]
The above program
- Creates a pycurl object
- Specifies the URL of an FTP server (anonymous account)
- Creates a StringIO buffer to store the results of FTP LIST command
- Associates the pycurl object with the StringIO buffer for writing output received from FTP server
- Performs the curl operation
- Extracts the output
- Breaks the output in lines (considering \r\n as separator)
- Walks through the lines one by one
- Splits the line based on whitespace into different parts
- Extracts different fields from the directory listing (permissions, group, user, size, filename etc.)
Notes about processing the output of FTP LIST command
The response of FTP LIST command is very much non-standard. Different flavors of FTP servers simply display the directory listing differently. So the parsing of this output may be easy for one FTP server but a code for parsing directory listings which works across all kinds of FTP servers is difficult to write. This is probably the reason why this functionality is not provided in ftplib (Python Standard Library). In
the FTP standard, the output of FTP LIST command was intended for human consumption rather than computer interpretation which led to all the variations over the years.
FTPPARSE http://cr.yp.to/ftpparse.html is a library for parsing FTP LIST command responses for a variety of FTP servers. ftpparse currently understands the LIST output from any UNIX server, Microsoft FTP Service, Windows NT FTP Server, VMS, WFTPD, NetPresenz, NetWare, and MSDOS. Its easy to write a Python wrapper for this library using ctypes.
Even this library doesn't work for a number of situations:
- When the size of a file is bigger than 2 GB.
- FTP servers of various video servers (I have seen GUI FTP clients like FileZilla or Windows explorer suck on some of them)
Thursday, May 27, 2010
Visual Studio (VS) 2005 IDE for IronPython (Python) - Yang Li's blog - Site Home - MSDN Blogs
Sandcastle (software) - Wikipedia, the free encyclopedia
Thursday, May 20, 2010
Pretty printing boolean values in C++
By default boolean values (type bool, values true and false) are printed as 1 and 0 in C++ iostreams. There is a simple way to print them as their names (true/false). The following code-example demonstrates how to do that using boolalpha manipulator.
1: #include <iostream>2: using namespace std;3:4: int main()5: {6: // By default true prints as 1 and false prints as 07: cout << true << " " << false << endl;8: // We can enable boolalpha formatting flag9: cout << boolalpha;10: // Now true and false will be inserted as their names instead11: // of integral values12: cout << true << " " << false << endl;13: // We can later on unset boolalpha with noboolalpha format manipulator14: cout << noboolalpha << endl;15: cout << true << " " << false << endl;16: // The boolalpha flag is not set in standard streams on initialization17: return 0;18: }19:20:
The output of the program looks as follows:
1: 1 02: true false3: 1 0
We can notice that on the first line the output is integral. After setting boolalpha, the output becomes textual. Later on the output becomes integral again after setting noboolalpha.
Input streams
The manipulator can similarly be used on input streams for reading boolean values as true/false or 1/0.
Wednesday, May 19, 2010
Adding custom draw functionality to an MFC CButton
We will start by defining a subclass CBDButton to the MFC class CButton.
1: class CBDButton : public CButton2: {3: public:4: // overwrite void DrawItem function and draw your button5: void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);6: };7:
As you can notice above, we have overridden one function DrawItem in this class. Lets now look at its implementation.
1: void CBDButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)2: {3: // TODO: Add your code to draw the specified item4: UINT uStyle = DFCS_BUTTONPUSH;5:6: // This code only works with buttons.7: ASSERT(lpDrawItemStruct->CtlType == ODT_BUTTON);8:9: // If drawing selected, add the pushed style to DrawFrameControl.10: if (lpDrawItemStruct->itemState & ODS_SELECTED)11: {12: uStyle |= DFCS_PUSHED;13: }14:15: CDC dc;16: dc.Attach(lpDrawItemStruct->hDC);17:18: // Draw the button frame.19: dc.DrawFrameControl(&lpDrawItemStruct->rcItem, DFC_BUTTON, uStyle);20:21: CRect rect = lpDrawItemStruct->rcItem;22: rect.left = rect.left + 1;23: rect.right = rect.right - 2;24: rect.top = rect.left + 1;25: rect.bottom = rect.bottom - 2;26: dc.FillSolidRect(rect, m_clrBk);27:28: // Get the button's text.29: CString strText;30: GetWindowText(strText);31: // Draw the button text using the text color.32: COLORREF crOldColor = dc.SetTextColor(m_crText);33: dc.DrawText(strText, strText.GetLength(), &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);34: dc.SetTextColor(crOldColor);35:36: dc.Detach();37: }
Wednesday, May 5, 2010
Testing windows live writer
Windows Live Writer is expected to help me write source code examples easily and post them to my blog seamlessly. In this post I am trying to test its functions.
Here is some C++ code:
1: class A
2: {3: public:4: A();5: ~A();6: void f();7: };8:9: // an example of implementation inheritance10: class B : private A11: {12: public:13: B();14: ~B();15: void g();16: };17:18:
Following is some Python code:
1: import os2: import sys3:4: def main():5: filepath = sys.argv[1]6: contents = open(filepath).readlines()7: print 'Number of lines: ' , len(contents)8:9: if __name__ == '__main__':10: main()
A Haskell code example:
1: double:: Integer -> Integer2: double x = x + x3:4: square :: Integer -> Integer5: square x = x * x6:7: main = do8: print $ square 109: print $ double 2010:11:
Some JavaScript:
1: fucntion f()2: {3: alert('Hello World');4: }5:
Some HTML:
1: <html>2: <head>3: <title>Hello World </title>4: </head>5: <body>6: <p> Don't Panic </p>7: </body>8: </html>9:
Some CSS:
1: .header2: {3: forground-color: black;4: background-color: red;5: text-size: 20;6: }7:
I guess this is more than enough for me. I am happy it works nicely.
Tuesday, May 4, 2010
Side effects and functional programming
- The function has some local variables which are lost after the function execution completes (no side effect)
- The function may compute a value which it returns (no side effect)
- If the arguments are references/pointers to some objects, the function may modify them (a side effect to the input arguments)
- The function may modify some global variable (a side effect)
- The function may output something on output or to a file or a socket (a side effect to the outside world with which the program is associated)
- The function may read something from input or from a file or a socket (a side effect to the outside world with which the program is associated)
- A member function may alter the state of the object with which it is associated (a side effect)
Are C++ Raw Pointers First Class Objects?
Monday, January 11, 2010
Unloading DLLs in Python through CTYPES
Sunday, January 10, 2010
SCC - Trac
Certified Programming with Dependent Types
Saturday, January 9, 2010
Writer - Windows Live
GLFW - An OpenGL library
A Techie Tech Writer Blog � Single-sourcing Training Exercises with Sphinx
Thursday, January 7, 2010
Tooltips in dijit.layout.TabContainer
- dijit_layout__TabButton_0
- dijit_layout__TabButton_1
- dijit_layout__TabButton_2
- and so on...
- Call a function 1 second after the page has been loaded. By this time, the ids for different titles for tabs inside the tab container are expected to be associated.
- Create the tooltips at run time based on the generated ids.