Thursday, August 5, 2010

c++: generic pointers and void *

When a variable is declared as being a pointer to type void it is known as a generic pointer. A pointer to void can store an address to any data type. Since you cannot have a variable of type void, the pointer will not point to any data and therefore cannot be dereferenced. It is still a pointer though, to use it you just have to cast it to another kind of pointer first. Hence the term Generic pointer.
This is very useful when you want a pointer to point to data of different types at different times.
Here is some code using a void pointer:
int main()
{
    int i;
    char c;
    void *the_data;

    i = 6;
    c = 'a';

    the_data = &i;
    printf("the_data points to int %d\n", *(int*)the_data);

    the_data = &c;
    printf("the_data points to char %c\n", *(char*) the_data);

    return 0;
}
source

Tuesday, August 3, 2010

c++: const functions

Declaring a member function with the const keyword specifies that the function is a "read-only" function that does not modify the object for which it is called.
To declare a constant member function, place the const keyword after the closing parenthesis of the argument list. The const keyword is required in both the declaration and the definition. A constant member function cannot modify any data members or call any member functions that aren't constant.
// constant_member_function.cpp
class Date
{
 public:
   Date( int mn, int dy, int yr );
   int getMonth() const;     // A read-only function
   void setMonth( int mn );  // A write function can't be const

 private:
   int month;
};

int Date::getMonth() const
{
   return month;        // Doesn't modify anything
}


Monday, August 2, 2010

python: understanding the "with" statement/keyword

Consider some typical boilerplate code that opens a file (whose filename is passed as a command-line argument) in read-only mode, reads the file line-by-line and performs some operation on each line thus read:

fileIN = open(sys.argv[1], "r")
line = fileIN.readline()
while line:
    [do something with line]
    line = fileIN.readline()

From Python 2.5 onwards, this can be done more easily via the "with" statement:
with open(sys.argv[1], "r") as fileIN: 
    for line in fileIN: 
        [do something with line] 

The "with" statement reduces the line count from 5 to 3, and also makes the code more readable and human-friendly. It's more pythonic and intuitive.

source

HTTP: Chunked Encoding

In chunked encoding, the content is broken up into a number of chunks; each of which is prefixed by its size in bytes. A zero size chunk indicates the end of the response message. If a server is using chunked encoding it must set the Transfer-Encoding header to "chunked".

Chunked-encoding is not the same as Content-Encoding header. The Content-Encoding header is an entity-body header. since transfer-encodings are a property of the message, not of the entity-body. ("Entity-body" refers to the body or payload [e.g. a JPG image] of an HTTP request [e.g. POST or PUT request] or response).

 

Q: When is chunked encoding really useful?

A: Chunked encoding is useful when a large amount of data is being returned to the client and the total size of the response may not be known until the request has been fully processed. An example of this is generating an HTML table of results from a database query. If you wanted to use the Content-Length header you would have to buffer the whole result set before calculating the total content size. However, with chunked encoding you could just write the data one row at a time back to the client. At the end, you could write a zero-sized chunk when the end of the SQL query is reached.

This is the HTTP header that is sent by the server:

Transfer-Encoding: chunked

In the HTTP 1.1 specification, chunked is the only encoding method supported by the "Transfer-Encoding" header.

 

source

apache: what are .htaccess files typically used for?

.htaccess files allow us to make configuration changes on a per-directory basis. .htaccess files work in Apache Web Server on both Linux/Unix and Windows operating system. In Apache, the format of .htaccess is the same as the server's global configuration file.

There are several things that developers, site owners and webmasters can do with .htaccess files, for example:

  • Prevent directory browsing
  • Redirect visitors from one page or directory to another
  • Password protection for sensitive directories
  • URL-Rewriting (e.g. rewriting long, unwieldy URLs to shorter ones)
  • Change the default index page of a directory
  • Prevent hot-linking of images from your website
  • per-directory cache control

 

 sources:

http://www.bloghash.com/2006/11/beginners-guide-to-htaccess-file-with-examples/

http://en.wikipedia.org/wiki/Htaccess

Saturday, July 31, 2010

python: using the glob module to perform simple wildcard expansion on a filesystem

import glob
glob.glob('c:\\music\\_singles\\*.mp3')

['c:\\music\\_singles\\a_time_long_forgotten_con.mp3',
'c:\\music\\_singles\\hellraiser.mp3',
'c:\\music\\_singles\\kairo.mp3',
'c:\\music\\_singles\\long_way_home1.mp3',
'c:\\music\\_singles\\sidewinder.mp3',
'c:\\music\\_singles\\spinning.mp3']
glob.glob('c:\\music\\_singles\\s*.mp3')

['c:\\music\\_singles\\sidewinder.mp3',
'c:\\music\\_singles\\spinning.mp3']



You can get a list of all of those with a single call to glob, by using two wildcards at once. One wildcard is the "*.mp3" (to match .mp3 files), and one wildcard is within the directory path itself, to match any subdirectory within "c:\music" like this:



glob.glob('c:\\music\\*\\*.mp3')



source

web services vs REST

The fundamental difference, therefore, between REST and document-style Web services is how the service consumer knows what to expect out of the service. Web services have contracts, defined in WSDL. Since Web services focus on the service, rather than on the resource, the consumer has clear visibility into the behavior of the various operations of the service, whereas in REST's resource-oriented perspective, we have visibility into the resources, but the behavior is implicit, since there is no contract that governs the behavior of each URI-identified resource.

For a very good introductory article on REST, check out the article here.