Sunday, March 19, 2017

Difference between mutable and immutable in Python

Difference between mutable and immutable in Python
1.      Meaning of mutable is changeable, reverse of mutable is immutable. It means not changeable.
2.      These mutable and immutable terms are used to refer Python objects.
3.      Every language has their own definition for their usage.
In Python following objects are immutable
·        int
·        float
·        decimal
·        complex
·        bool
·        string
·        tuple
·        range
·        frozenset
·        bytes

Following objects are mutable
·        list
·        dict
·        set
·        bytearray
·        user-defined classes (unless specifically made immutable)

Sample:

1.     Trying to change value of the immutable string object

sample_str = 'mutable test?'
sample_str[11] = '!'

The above written program throws following error since we can’t modify the immutable objects

>>>
Traceback (most recent call last):
  File "C://Python_Practice/mutablevsimmutable.py", line 2, in
    sample_str[11] = '!'
TypeError: 'str' object does not support item assignment >>>

2.     Trying to modify the mutable object
         int_list_sample  = [10, 6,4]
         int_tuple_sample = (10, 6,4)
         print("list before modify :", int_list_sample)
         int_list_sample[0] = 1
         # list is now [10, 6,4]
         print("list after modify :", int_list_sample)
         int_tuple_sample[0] = 1
         # should raise: TypeError: 'tuple' object does not support item assignment

Output for the above program is

                        list before modify : [10, 6, 4]
list after modify : [1, 6, 4]

Trackback (most recent call last):
  File "C:/Jeyanthi/Python_Practice/mutablelist.py", line 10, in
    int_tuple_sample [0] = 1
TypeError: 'tuple' object does not support item assignment

 In this example, list is mutable so it allows the user to the modify the object. Tuple is immutable so it raises error when the user tries to modify it.

Note: # is commented line in Python

To remember easily we can define that Primitive data types are immutable. Container data types are mutable in python.



Wednesday, February 19, 2014

Create Simple clock using C

#include
#include
#include
int main()
{
struct time t;
clrscr();
while(1)
{
printf("\n\t\t\t\t============\n\t\t\t\t=\t =\n\t\t\t\t= MY CLOCK =");
printf("\n\t\t\t\t=\t =\n\t\t\t\t============");
gettime(&t);
printf("\n\n\n\n\t\t\t\t ");
printf("%d:%d:%d",t.ti_hour,t.ti_min,t.ti_sec);
sleep(1);
clrscr();
}
return 0;
}

C Program without main

#include
#define decode(s,t,u,m,p,e,d) m##s##u##t
#define begin decode(a,n,i,m,a,t,e)
int begin()
{
printf(” hello “);
}

C Program which delete itself

#include
#include
#include
void main()
{
printf(“This program will destroy itself if u press any key!!!\n”);
getch();
remove(_argv[0]);/*array of pointers to command line arguments*/
}

C Program to print map of india

#include
main()
{
int a,b,c;
int count = 1;
for (b = c = 10; a = "- FIGURE?, UMKC,XYZHello Folks,\
TFy!QJu ROo TNn(ROo)SLq SLq ULo+\
UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\
NBELPeHBFHT}TnALVlBLOFAkHFOuFETp\
HCStHAUFAgcEAelclcn^r^r\\tZvYxXy\
T|S~Pn SPm SOn TNn ULo0ULo#ULo-W\
Hq!WFs XDt!"[b+++21]; )
for(; a-- > 64 ; )
putchar ( ++c=='Z' ? c = c / 9 : 33 ^ b & 1 );

getch();
return 0;
}

Monday, February 17, 2014

Interop between C++ and C or LabVIEW

Interop between C++ and C or LabVIEW
One of the questions that recur from time to time is: ‘How do I use C++ classes in a language that has only C bindings, like C itself or LabVIEW?'
The short answer: you can’t.
The long answer: you can’t, but you can wrap the C++ code in C functions, and export those in a wrapper dll, thus allowing you to manipulate C++ objects through a C interface.
The simple solution
The general way to do this is to export a function for creating objects that returns the object pointer to the caller. Another function will take that pointer to do something with it, and a third function will take the pointer to delete it.
You have to cast the pointer to something that can be recognized by the caller. You cannot export a class pointer, because C or LabVIEW have no idea what a class is. To be safe, you have to cast it to an INT_PTR to insure that it is always large enough to hold a pointer.

#define OBJPTR INT_PTR
The 3 essential functions look like this:
int simple_CreateObject(OBJPTR *ObjPtr)
{
*ObjPtr = (OBJPTR) new SomeClass();
if(NULL == *ObjPtr)
return 1;
return NO_ERROR;
}
int simple_UseObject(OBJPTR ObjPtr)
{
return ((SomeClass*)ObjPtr)->DoSomething();
}
int simple_DeleteObject(OBJPTR ObjPtr)
{
delete (SomeClass*)ObjPtr;
return NO_ERROR;
}
This approach is simple and it works. It has at least one serious problem though: the application will crash if it accidentally asks the wrapper to delete the same pointer twice, or to dereference an already deleted pointer.
Especially if you develop this wrapper for use by LabVIEW, I consider it bad taste if you do not shield the LabVIEW program against crashes caused by trivial mistakes.
A better solution
A better solution is to map all object pointers to a unique integer value. The calling program will only receive the integer values that it can supply to other functions to manipulate the C++ object.
The other wrapper functions have to find the pointer that is mapped to that integer value. If there is no mapping, it can simply return an error code instead of crashing the application.
When the object is deleted it is removed from the map so that the application cannot use it anymore.
The integer reference is incremented with each object that is created so that there will never be a duplicate reference.
map g_PtrMap;
int g_Index = 0;

int better_CreateObject(BETTER_OBJ_REF *ObjRef)
{
SomeClass* ptr = NULL;
ptr = new SomeClass();
if(NULL == ptr)
return 1;
if(NO_ERROR == errorCode)
{
g_PtrMap[++g_Index] = ptr;
*ObjRef = g_Index;
}
return NO_ERROR;
}

int better_UseObject(BETTER_OBJ_REF ObjRef)
{
map::iterator iter;
iter = g_PtrMap.find(ObjRef);
if(iter != g_PtrMap.end())
return (iter->second)->DoSomething();
else
return 1;
}
int better_DeleteObject(BETTER_OBJ_REF ObjRef)
{
map::iterator iter;
iter = g_PtrMap.find(ObjRef);
if(iter != g_PtrMap.end())
{
SomeClass* ptr = iter->second;
g_PtrMap.erase(iter);
delete ptr;
return NO_ERROR;
}
else
return 1;
}

Afterthoughts
These 2 examples are proof of concept examples to demonstrate how to use C++ objects in any language that has C bindings.
This is not production quality code though, because of the following reasons:
• It does not catch C++ exceptions that could be thrown somewhere in the C++ code.
• It does not properly handle the overflow of the g_Index variable after 2^32-1 object allocations.
• There is no synchronization of access to the global map. This means the better_xxx functions are not safe for multithreaded usage.
• There is no NULL pointer checking in the simple_xxx functions yet.
You can download the demo project with C++ and LabVIEW code. It is licensed under the MIT license so you can use it any way you want.


Add radio button to existing group - MFC

I am trying to add a new radio button to an existing group box (MFC application). Already there are four buttons. I have added new radio button to this group but application is treating it as different group and does not synchronize with other buttons. I have check GROUP property which is set to false.


Answer:
You are correct that you need to use the 'Group' property but in conjuntion with Tab Order (from the menu .

According to the Tab Order, the first button that has the Group property checked is deemed to be the first control in the group, the following controls are deemed to be part of the Group. A new Group is started when a control with Group checked is found.

Your new button is probably not in the right Tab Order and therefore not part of the Group.