Syntax for extracting multiple items from a dictionary
row = {"fname" : "Frank", "lname" : "Jones", "city" : "Hoboken", "state" :
"Alaska"}
cols = ("city", "state")
Is there a best-practices way to ask for an object containing only the keys
named in cols out of row? In other words, to get this:
{"city" : "Hoboken", "state" : "Alaska"}
Thanks,
shark
--
http://mail.python.org/mailman/listinfo/python-list
how to call this dll in python
I have a windows dll1.dll with a export function:
int f1(char filename,char **buf,int *bufLen)
{
int len;
//got the length of file anyway,such as 100
len = 100;//len = getLen(filename);
*buf = (char*)calloc(100);
*bufLen = len;
return 0;
}
then how can I call the f1 function with python.
thanks for your response.
--
http://mail.python.org/mailman/listinfo/python-list
Re: how to call this dll in python
On Nov 3, 4:22 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> Shark schrieb:
>
> > I have a windows dll1.dll with a export function:
>
> > int f1(char filename,char **buf,int *bufLen)
> > {
> > int len;
> > //got the length of file anyway,such as 100
> > len = 100;//len = getLen(filename);
> > *buf = (char*)calloc(100);
> > *bufLen = len;
> > return 0;
> > }
>
> > then how can I call the f1 function with python.
> > thanks for your response.
>
> If the above is *really* what you want to access from python, you
> shouldn't bother & write it new in python itself. You could either use
>
> bufLen = len(open(filename).read())
>
> or make a os.stat-call.
>
> If it serves only as a rather ugly illustrative example, then you should
> investigate the ctypes-module of python, which is made for accessing
> arbitrary dlls from python.
>
> Diez
Yes,the function is rather ugly, but how can python get the memory
alloced by a dll through the dll function's parameter.
I am facing the problem in my project, I try ctypes all over, but can
not get the correct solution.
--
http://mail.python.org/mailman/listinfo/python-list
Re: how to call this dll in python
On Nov 4, 2:16 pm, "Mark Tolonen" <[EMAIL PROTECTED]> wrote:
> "Shark" <[EMAIL PROTECTED]> wrote in message
>
> news:[EMAIL PROTECTED]
>
> >I have a windows dll1.dll with a export function:
>
> > int f1(char filename,char **buf,int *bufLen)
> > {
> > int len;
> > //got the length of file anyway,such as 100
> > len = 100;//len = getLen(filename);
> > *buf = (char*)calloc(100);
> > *bufLen = len;
> > return 0;
> > }
>
> > then how can I call the f1 function with python.
> > thanks for your response.
>
> Here's a quick, tested example. It's not pretty but demonstrates what you
> want:
>
> = begin C DLL code ===
> #include
> #include
> #include
>
> __declspec(dllexport)
> int get(const char* filename,char **buf,int *bufLen)
> {
> int i,len;
> printf("*buf before malloc=%p\n",*buf);
> len = strlen(filename);
> *buf = (char*)malloc(len);
> *bufLen = len;
> for(i=0;i (*buf)[i]='a';
> (*buf)[i]=0;
> printf("*buf after malloc=%p\n",*buf);
> return 5;
>
> }
>
> __declspec(dllexport)
> void release(char* buf)
> {
> printf("buf[0]=%d\n",buf[0]);
> printf("buf=%p\n",buf);
> free(buf);}
>
> = end C DLL code ===
>
> = begin Python code ===
> from ctypes import *
> x = CDLL('x.dll')
> x.get.argtypes=[c_char_p,POINTER(POINTER(c_byte)),POINTER(c_int)]
> buf = POINTER(c_byte)()
> length = c_int()
> print x.get("hello.txt",byref(buf),byref(length))
> print repr(string_at(buf,length))
> buf[0]=55 # change first byte
> x.release.argtypes=[POINTER(c_byte)]
> x.release(buf)
> = end Python code ===
>
> sample output:
>
> *buf before malloc=
> *buf after malloc=00C92940
> 5
> '\x00'
> buf[0]=55
> buf=00C92940
>
> -Mark
thank you, Mark. I got it.
--
http://mail.python.org/mailman/listinfo/python-list
