Problem with write binary data to OLE field in Access

2008-03-26 Thread lialie
Hi~

I would like to save images in OLE field in Microsoft Access. It writes
the binary data which read from an JPEG/BMP file.
But seems I meet an encoding problem.
The following code demos that.
Any sugguestion?

---
import win32com.client as wc

conn = wc.Dispatch(r'ADODB.Connection')
recordset = wc.Dispatch(r'ADODB.RecordSet')

dsn = 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=test.mdb;'
conn.Open(dsn)
print conn

k = recordset.Open('[tblImageDataTypes]', conn, 1, 3)

#recordset.AddNew()
#recordset.Fields('Field3_GENERAL').AppendChunk(open('tt.jpg', 'rb').read())
#recordset.Update()
#print  Actual Size: , recordset.Fields('Field3_GENERAL').ActualSize

recordset.MoveFirst()
kk = recordset.Fields('Field3_GENERAL').GetChunk(
recordset.Fields('Field3_GENERAL').ActualSize)
print len(str(kk).decode('utf-16'))
print len(open('tt.jpg', 'rb').read())

recordset.Close
conn.Close()
###
One of the results:
the length of original file is : 2598
the actual size of the field is: 3658
the length decode from the chunk is: 1829

I try to write some text files into the filed and read it out, it is OK.
Windows XP sp2; Python 2.4.4; Pywin32 210; Microsoft Access 2002
---
Regards,
lialie
-- 
http://mail.python.org/mailman/listinfo/python-list


Dispatch(Excel.Application) failed

2008-03-13 Thread lialie
Hi,
Maybe it 's quite simple, but I can't fix it. Do I make some mistakes in
my env setting? My excel version is 2003.
any suggestion? Thanks.

Traceback (most recent call last):
File testexcel.py, line 3, in ?
excel = Dispatch(Excel.Application)
File C:\Python24\Lib\site-packages\win32com\client\__init__.py, line
95, in
Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch,userName,c
lsctx)
File C:\Python24\Lib\site-packages\win32com\client\dynamic.py, line
98, in _
GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
File C:\Python24\Lib\site-packages\win32com\client\dynamic.py, line
78, in _
GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(IDispatch, None, clsctx, pythoncom.II
D_IDispatch)
pywintypes.com_error: (-2147221005,
'\xce\xde\xd0\xa7\xb5\xc4\xc0\xe0\xb1\xf0\xd7\xd6\xb7\xfb\xb4\xae',
None, None)
-- 
http://mail.python.org/mailman/listinfo/python-list


RE: Dispatch(Excel.Application) failed

2008-03-13 Thread lialie


On Behalf Of John Machin
  

   '\xce\xde\xd0\xa7\xb5\xc4\xc0\xe0\xb1\xf0\xd7\xd6\xb7\xfb\xb4\xae',
   None, None)
 
 
 Googling for 2147221005 gives some clues.
 
 That string of hex stuff appears where an error message is 
 expected -- it's not utf8, and makes no sense when I attempt 
 to decode it with cp1250 to cp1258 inclusive. If you start 
 IDLE and type:
  


The hex stuff is Chinese. It appears to be a standard Windows
error message.
无效的类别字符串 (Which I believe meaans invalid class string)

I wrote in another post (that doesn't appear to have made it to
the list)
that the call to Dispatch on Excel will fail if the formula bar
edit box is
active. Just another idea.

Regards,
Ryan Ginstrom

Thanks.
I did try to google the question, but the answer wasn't properly. 
I am using Windows XP sp2 which language is Chinese simp. At first, I thought 
the hex string was coding in cp936 or utf8, then failed. second, try to run my 
script on the other computer, and it run well without any error! At last, I 
give up.After Windows Excel reinstalled, it work, but still confused.:(
The net speed is horribly slow when connect to Pywin32 project at SF.

Regards,
Lialie


-- 
http://mail.python.org/mailman/listinfo/python-list

How to control the creation of an instance?

2007-06-02 Thread lialie
Hi,

suppose i have a free_object list[Sample1, Smaple2]. when create a
new object sample(*args, **kwds), if free_object_list isn't empty, just
pop one from free_object_list instead of creating a new instance.

any way to do this?

I do some work as follows:

class Sample(object):
used_object = []
free_object = []

def __init__(self, *args, **kwds):
pass


def __new__(self, *args, **kwds):
if Sample.free_object:
obj = Sample.free_object.pop(0)
else:
obj = object.__new__(Sample, *args, **kwds)
Sample.used_object.append(obj)
return obj

 still get a new instance :(


def Release(self):
Sample.used_object.remove(self)
Sample.free_object.append(self)
return True

-- 
http://mail.python.org/mailman/listinfo/python-list

Why does my callback funtion collapse?

2007-04-05 Thread lialie
Hi,all

I try to callback from a thread in C. But it collapsed. I can't figure
it out.
My test files attached.
Thanks.

#include windows.h
#include Python.h
#include stdio.h

static PyObject *my_callback = NULL;
DWORD threadId;
HANDLE thread;


static PyObject *
callback_set_callback(PyObject *dummy, PyObject *args)
{
PyObject *result = NULL;
PyObject *temp;

if (PyArg_ParseTuple(args, O:set_callback, temp))
{
if (!PyCallable_Check(temp))
{
PyErr_SetString(PyExc_TypeError, Must be callable);
return NULL;
}

my_callback = temp;
Py_XINCREF(temp);
Py_XDECREF(my_callback);
Py_INCREF(Py_None);
result = Py_None;
}
return result;
}


PyObject* Test()
{
PyObject *result;
PyObject *arglist;

int i = 0;
for (; i10; ++i)
{
   printf(Callback from Test()\n);
   arglist = Py_BuildValue((i), i*i);
   result = PyEval_CallObject(my_callback, arglist);
   Py_DECREF(arglist);
}
   
   return result;
}


static PyObject *
callback_test(PyObject *dummy, PyObject *args)
{
thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)Test, NULL, 0, 
threadId);
//FIXME
//WaitForMultipleObjects(1, thread, TRUE, INFINITE);
//
return Py_BuildValue(b, TRUE);
}


static PyMethodDef CameraModuleMethods[] = 
{
{Test, callback_test, METH_NOARGS, Test},
{SetCallback, callback_set_callback, METH_VARARGS, Set callback func},
{NULL, NULL, 0, NULL}
};


void initCallbackTest(void)
{
Py_InitModule(CallbackTest, CameraModuleMethods);
};


#
# usage: python setup.py install
#


from distutils.core import setup, Extension

my_module = Extension(CallbackTest, 
  sources=['callback.c'])

setup(name=CallbackTest, version=1.0,
  ext_modules=[my_module])


import CallbackTest as cb
import time

def print_msg(long_number):
print   * 3, long_number
return True

cb.SetCallback(print_msg)
cb.Test()

time.sleep(1)

-- 
http://mail.python.org/mailman/listinfo/python-list

Too slow to read an entire table from Microsoft Access

2007-03-14 Thread lialie
Hi, all:
I read a table with 4500 rows and 12 columns using win32com.client.
Reading and updating records are OK, but cost too much time.Especially
making a dict as line(***).
All follows done may take nearly 90s!
Is there any good idea? Thanks.

conn = win32com.client.Dispatch(r'ADODB.Connection')
DSN='Provider=Microsoft.Jet.OLEDB.4.0;Data
Source='+DB_FILE_PATH+';Persist Security Info=False;'
conn.Open(DSN)
record_set = win32com.client.Dispatch(r'ADODB.Recordset')
record_set.Open('[Detail]', conn, 1, 3)
record_set.MoveFirst()

while not record_set.EOF:
tmp = ((record_set.Fields(i).Name, record_set.Fields.Item(i).Value)
for i in range(record_set.Fields.count))
print dict(tmp)
-(***)
record_set.MoveNext()


-- 
http://mail.python.org/mailman/listinfo/python-list

quickly read a formated file?

2007-03-01 Thread lialie
Hi,
Is there a fine way to read a formated file like:
%HEADER
title = Untilted
username = User

%DATA
..
..
The formated file may be very popularly, but the module ConfigPaser
doesn't handle it. Is there a way to process it freely?



-- 
http://mail.python.org/mailman/listinfo/python-list

Is it good to create a thread in a non gui thread?

2006-12-14 Thread Lialie - KingMax
Hi,
I create a thread in a non gui thread, and it does well. But it seems
strange. Somebody told me better not for it may cause something hard to
imagine.
Is there any different between them?

THX
-- 
http://mail.python.org/mailman/listinfo/python-list

how to read a list from python in C?

2006-05-19 Thread Lialie KingMax

Hi, all

I am writing a C extension with .Net.
Now I have a list of points, like [(0.0, 0.0), (2.0, 3.0), (random x,
random y)].
Is there a better way to translate it to an array than doing it one by one?

Thanks


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: how to read a list from python in C?

2006-05-19 Thread Lialie


Thanks for your reply.
yes, I pass a list as an argument to the function in C.
The list has a random length.
Do you mean to do it in this way?
use PyTuple_Size(args), in a loop
use PyTuple_GetItem(args, i) 
or

use PyArg_VaParse?

Best Regards


-- 
http://mail.python.org/mailman/listinfo/python-list

Re: hyperthreading locks up sleeping threads

2006-05-09 Thread Lialie KingMax

I haven't try the files yet. But have got a similar problem before. The
situation is nearly the same.
Always at random time , it reported that the memory read or write
violently.But I use Windows 2000(Python 2.3, wxPython 2.4). Windows
issue says 2000 doesn't support HP, so I simply turn it off. It didn't
appear.

-- 
http://mail.python.org/mailman/listinfo/python-list

how to write special value to a config file with ConfigParser

2006-04-23 Thread Lialie


Hello,all
I found it easy to read configures from a config file.
But how can I set a special value to an item or write it into the 
original file?

I have tried this:

import ConfigParser
config = ConfigParser.ConfigParser()
config.read('a.conf')
config.get('Main', 'Something') # That is OK.
config.set('Main', 'Something', '2') # without any fault
#fp = open('a.conf') # can not be done, raise Error
#fp = open('a.conf', 'a') # Something set to 2, but it copies the all items
#config.write(fp)


# How can I do it?

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: how to write special value to a config file with ConfigParser

2006-04-23 Thread Lialie

Lialie write:
OK.I find out myself.
Excuse me.
Just open ini file in write mode.
---BeginMessage---


Lialie write:
OK.I find out myself.
Excuse me.
Just open ini file in write mode.



---End Message---
-- 
http://mail.python.org/mailman/listinfo/python-list