Wednesday 17 October 2012

convert queue into stack and make list work as queue and stack both

now we will see how one can convert QUEUE into STACK into python -- !


from collections import deque

list =[1,2,3,4,5]

queue_list = deque(list)

now queue_list will work as a queue object .... :)
---------------------------------------------------------------
you can extract any element from queue_list by using--

queue_list.popleft()
----------------------------------------this all thing is working as a queue object
now we just need to use a simple method pop() to work queue_list as a stack
-------------------------------------------------------------------------------------------------
queue_list.pop()
u can use also queue_list.popleft()  to work this list as a queue.


Thank You
Meet Me
Prashant Gaur


Monday 1 October 2012

custom enum field in django to support mysql ENUM datatype :) :)

Hi friends ,

  As we know enum data type is used to reduce memory in database . we can pass string as input and can get output as string ---but in really it will strore in DB as 1 digit number starts from 0  with increase of 1 .
   but django does not provide us default django field so we need to make our own CUSTOM ENUM FIELD . you can go through djnago docs to know about it Django DOCS 

but for enum you just make youe own class



# Class which describe Custom Field of name Enum

class EnumField(models.Field):

    def __init__(self, *args, **kwargs):
        kwargs['max_length'] = 104
        super(EnumField, self).__init__(*args, **kwargs)
        if not self.choices:
            raise AttributeError('EnumField requires `choices` attribute.')

    def db_type(self):
        return "enum(%s)" % ','.join("'%s'" % k for (k, _) in self.choices)


# choices means data in string form to display in mysql and django admin interface

STATE =(('C' , 'C'),
            ('P', 'P),
            ('F, 'F'),
            ('F', 'F'),
            ('R', 'R'),)



# model for orderstate field 


class Table(models.Model):
    state = EnumField(choices=STATE,verbose_name ='orderstate')


now u can make your own enum field and any custom field by just edit some code in custom class
method -- >>  db_type is used for making db data type here it is enum    
and in __init__  --> you can customize your model and field in admin interface



Django Developer