« Earlier 2 items total Later »

On this page: 

Enable arrow keys in python interpreter in OS X

Something that has always bothered me is that in OS X's default python install, typing an arrow enters the escape sequence # ex: [^[[A, ^[[D]; instead of moving the cursor or going to the previous commands. To solve this issue, one needs to install the readline module.

From Bob Ippolito:
In the terminal, enter:
python `python -c “import pimp; print pimp.__file__”` -i readline

----
This works with the standard installation of python 2.3 included with OS X. I have not yet gotten readline to work with python 2.4.2.

Python dictionary to PHP array

Pass any python dictionary into convertArray.
It will return an array that PHP can read.

Example:
>>> convertArray({"One":1, "Two":[2,"Two"], "Three":[{"ThreeAgain":[3,3,3]} ,[1,2,3], "Three"]})

Returns: array('Three'=>array(array('ThreeAgain'=>array(3, 3, 3)), array(1, 2, 3), 'Three'), 'Two'=>array(2, 'Two'), 'One'=>1)


## Converts a python dictionary to a php array.
def convertArray(arr):
        ret = ""
        list = []
        
        if isinstance(arr, type([]) ):          ## If the instance is an array
                
                for ele in arr:
                        if isinstance(ele, ( type([]), type({})) ):     ## If the instance is an array
                                list.append( convertArray(ele))                                 ## recursive it
                        
                        elif isinstance(ele, (type(1), type(1.0))):     
                                list.append(str(ele))           ## if an int or float, no quotes
                        else:
                                list.append("'%s'" % str(ele))
        
        elif isinstance(arr, type({}) ):                ## If the instance is an array
                for (k,v) in arr.items():
                        item = "'" + str(k) + "'=>"
                        if isinstance(v, ( type([]), type({})) ):
                                item += ( convertArray(v))
                        else:
                                if isinstance(v, (type(1), type(1.0))): 
                                        item += (str(v))                ## if an int or float, no quotes
                                else:
                                        item += ("'%s'" % str(v))
                        list.append(item)
        else:
                raise NameError, "Error - neither a array or a dictionary was passed to this function"
        
        
        if len(list) > 0:
                ret = "array(" + ", ".join(list) + ")"
        else:
                ret = "array()"
                
        return ret

« Earlier 2 items total Later »