Python - while else statement

14. August 2012 07:56

 

In python most people are familure with a combination of if / else or a while loop. Did you know you can combine a while with an else statement. the obvious main advantage here is to prevent using extra variables and nested statement which makes the code shorter and clearer to understand.

 

This short example demonstrates this functionality.

 

 

lst = []
while lst:
    print "There is a list"
    break
else:
    print "Empty List"

 

 

In the case above because the contents of the while loop are no executed because the list is empty it will print "Empty List" instead. This can also work with for loops like in the following example.

 

 

for x in lst:
    if x == "Something":
        DoSomething()
        break
else:
    print "Not Found"

 

You should probably take notice of the break statements in the above code as the else statements runs on the last evaluation of the loop condition so when a loop terminates because its condition is false the else will always run. The break statement changes this so that the loop conditition is true and the else will be skipped.

E-mail Kick it! DZone it! del.icio.us Permalink


C++ - Check an IP Address is in a IP/Mask range

9. August 2012 08:00

 

This is a short guide on how to check if an ip address is inside a certain ip / mask combination. An example would be to ensure that an ip address like "192.168.0.10" is inside "192.168.0.0/255.255.255.0"

 

One of the problems that you will face is that the standard functions for converting ip addresses to an unsigned int typically will also convert the address to network byte order which will not make. You could use the ntohl function to convert it back or a simple parsing functions like below to convert it to an unsigned long using sscanf.

 

uint32_t IPToUInt(const std::string ip) {
    int a, b, c, d;
    uint32_t addr = 0;

    if (sscanf(ip.c_str(), "%d.%d.%d.%d", &a, &b, &c, &d) != 4)
        return 0;

    addr = a << 24;
    addr |= b << 16;
    addr |= c << 8;
    addr |= d;
    return addr;
}

 

The rest is the very stright forward since an ip address is really just a 32bit number if you convert the ip address the subnet and the mask a stright check can be performed like this.

 

bool IsIPInRange(const std::string ip, const std::string network, const std::string mask) {
    uint32_t ip_addr = IPToUInt(ip);
    uint32_t network_addr = IPToUInt(network);
    uint32_t mask_addr = IPToUInt(mask);

    uint32_t net_lower = (network_addr & mask_addr);
    uint32_t net_upper = (net_lower | (~mask_addr));

    if (ip_addr >= net_lower &&
        ip_addr <= net_upper)
        return true;
    return false;
}

And just to make sure that it works here is some test code.

 

void test(const std::string ip, const std::string network, const std::string mask, bool expected) {
    if (IsIPInRange(ip, network, mask) != expected) {
        printf("Failed! %s %s %s %s\n", ip.c_str(), network.c_str(), mask.c_str(), expected ? "True" : "False");
    } else {
        printf("Success! %s %s %s %s\n", ip.c_str(), network.c_str(), mask.c_str(), expected ? "True" : "False");
    }
}


int main(int argc, char **argv) {
    std::string ip(argv[1]);

    test("192.168.1.1", "192.168.1.0", "255.255.255.0", true);
    test("192.168.1.1", "192.168.1.2", "255.255.255.255", false);
    test("192.168.1.3", "192.168.1.2", "255.255.255.255", false);

    test("220.1.1.22", "192.168.1.0", "255.255.255.0", false);
    test("220.1.1.22", "220.1.1.22", "255.255.255.255", true);
    test("220.1.1.22", "220.1.1.23", "255.255.255.255", false);
    test("220.1.1.22", "220.1.1.21", "255.255.255.255", false);

    test("0.0.0.1", "0.0.0.0", "0.0.0.0", true);
    test("192.168.1.2", "10.0.0.1", "255.255.255.255", false);

    return 0;
}

 

E-mail Kick it! DZone it! del.icio.us Permalink


MSSQL - Finding the database restore history

7. August 2012 21:12

 

Did you know you can view the database restore history from MSSQL server. The following SQL will provide a list of all database restores that have been performed on a server at some point.

 

 

SELECT 
	destination_database_name, server_name AS 'SourceServer', database_name AS 'SourceDB', physical_device_name, type, backup_start_date, restore_date
	FROM msdb.dbo.restorehistory AS rh
		INNER JOIN msdb.dbo.backupset AS bs ON bs.backup_set_id = rh.backup_set_id
		INNER JOIN msdb.dbo.backupmediafamily AS bmf ON bmf.media_set_id = bs.media_set_id
	ORDER BY restore_date DESC;

 

 

It will produce output that looks like this.

 

 

Stev	DC01	Stev	E:\MSSQL-Backup\Stev.bak	D	2011-09-17 08:43:58.000	2011-09-17 09:10:25.283

 

E-mail Kick it! DZone it! del.icio.us Permalink