Creating a COM object with comtypes - python

I am trying to port to Python some old VBA code.
In VBA I add Geo3D.dll as a project reference, then I use this:
Set P1 = New GEO3DLib.Point
P1.Set 1, 2, 3, 0.001
In Python I tried this:
import comtypes
import comtypes.client as cc
cc.GetModule('C:\\Program Files (x86)\\think3\\2009.3\\thinkdesign\\bin\\Geo3d.dll')
import comtypes.gen.GEO3DLib as Geo3d
pt = cc.CreateObject('Geo3d.Point', None, None, Geo3d.Point)
But I get this error:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
pt = cc.CreateObject('Geo3d.Point', None, None, Geo3d.Point)
File "C:\Anaconda3\lib\site-packages\comtypes\client\__init__.py", line 238, in CreateObject
obj = comtypes.CoCreateInstance(clsid, clsctx=clsctx, interface=interface)
File "C:\Anaconda3\lib\site-packages\comtypes\__init__.py", line 1217, in CoCreateInstance
iid = interface._iid_
AttributeError: type object 'Point' has no attribute '_iid_'
Replacing the last line with:
pt = Geo3d.Point
pt.Set(1., 2., 3., 0.001)
I get this error:
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
pt.Set(1., 2., 3., 0.001)
AttributeError: type object 'Point' has no attribute 'Set'
Any idea?

Enure that the python implementation and the thinkdesign library are compiled for the same platform type: 64 bit or 32 bit.
I tested this as follows:
>>> p=Dispatch('Geo3d.Point')
>>> p
<win32com.gen_py.think3 thinkdesign Type Library.IPoint instance at 0x59554312>
>>> p.Set(0,0,0)
>>> p.X
0.0
or with comtypes:
>>> point=comtypes.CoCreateInstance(comtypes.GUID.from_progid('Geo3d.Point'))
>>> ipoint=point.QueryInterface(Geo3d.IPoint)
>>> ipoint.Set(0,0,0)
0
>>> ipoint.X
0.0

Related

AttributeError: 'ZipInfo' object has no attribute 'filemode'

How to access filemode attribute of zipfile?
>>> info = zin.infolist()[1]
>>> info
<ZipInfo filename='test_dir/' filemode='drwxr-xr-x' external_attr=0x10>
>>> info.filename
'test_dir/'
>>> info.external_attr
1106051088
>>> info.filemode
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'ZipInfo' object has no attribute 'filemode'
The file attributes are stored in the upper bits of external_attr (above the lower 16 bits):
oct(1106051088 >> 16)
#'0o40755'
To check specific permissions, use functions from the module stat, e.g.:
stat.S_ISDIR(1106051088 >> 16)
# True, a directory
stat.S_IRUSR & (1106051088>>16)
# 256, user-readable
stat.S_IWGRP&(1106051088>>16)
# 0, not group-writable

how can i make ~[0] plus to ~[1] in django?

codes:
>>> from test2.models import Member_info
>>> member = Member_info.objects.all()
>>> member
<QuerySet [<Member_info: John2>, <Member_info: John1>, <Member_info: John3>]>
>>> member = Member_info.objects.all()[0]
>>> member
<Member_info: gun2>
>>> member += Member_info.obejcts.all()[1]
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: type object 'Member_info' has no attribute 'obejcts'
>>> member += Member_info.objects.all()[1]
Traceback (most recent call last):
File "<console>", line 1, in <module>
TypeError: unsupported operand type(s) for +=: 'Member_info' and 'Member_info'
>>> member.append(Member_info.objects.all()[1])
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: 'Member_info' object has no attribute 'append'
2.How can i append,,,,?
You can append to list, but you are trying to append to Member_info. Try this one:
member = Member_info.objects.all()[0]
members = [member]
members.append(Member_info.objects.all()[1])

error in function: 'str' object is not an iterator

I have a problem with the following function in python (where swap is a function that I have previously created and that works fine):
def swap (cards):
"""
>>> swap('FBFFFBFFBF')
'BFBBBFBBFB'
>>> swap('BFFBFBFFFBFBBBFBBBBFF')
'FBBFBFBBBFBFFFBFFFFBB'
>>> swap('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
'BBFBFBFBFBFBFBFFBFBFBFBFFBFBFFBFB'
"""
invert=""
for i in cards:
if i is "B":
invert+="F"
else:
invert+="B"
return (invert)
def swap2 (cards):
"""
>>> next('FBFFFBFFBF')
'FFBBBFBBFF'
>>> next('BFFBFBFFFBFBBBFBBBBFF')
'FBBFBFBBBFBFFFBFFFFFF'
>>> next('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
'FFFBFBFBFBFBFBFFBFBFBFBFFBFBFFBFF'
"""
indices=""
for pos, i in enumerate(cards):
if i =="B":
indices+=str(pos)
first= int(indices[0])
last= int(indices[-1])
prefix= cards [:first]
middle= cards [first:last+1]
suffix= cards [last+1:]
middle2=swap(middle)
return (prefix+middle2+suffix)
def turns (cards):
"""
>>> turns('FBFFFBFFBF')
3
>>> turns('BFFBFBFFFBFBBBFBBBBFF')
6
>>> turns('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
14
"""
turn=0
while cards != 'F'*len(cards):
cards=swap2(cards)
turn+=1
return (turn)
if __name__ == '__main__':
import doctest
doctest.testmod()
when I run this function it works fine but if I use doctest to see if there are mistakes it tells me:
TypeError: 'str' object is not an iterator
I don't know where this error comes from.
Can anyone help me?
complete output of the doctest:
File "C:\Users\manuel\Documents\Gent MaStat\programming and algorithms\workspace_python\homeworks\Week 5\looking_up.py", line 25, in __main__.swap2
Failed example:
next('FBFFFBFFBF')
Exception raised:
Traceback (most recent call last):
File "C:\Users\manuel\Anaconda3\lib\doctest.py", line 1321, in __run
compileflags, 1), test.globs)
File "<doctest __main__.swap2[0]>", line 1, in <module>
next('FBFFFBFFBF')
TypeError: 'str' object is not an iterator
**********************************************************************
File "C:\Users\manuel\Documents\Gent MaStat\programming and algorithms\workspace_python\homeworks\Week 5\looking_up.py", line 27, in __main__.swap2
Failed example:
next('BFFBFBFFFBFBBBFBBBBFF')
Exception raised:
Traceback (most recent call last):
File "C:\Users\manuel\Anaconda3\lib\doctest.py", line 1321, in __run
compileflags, 1), test.globs)
File "<doctest __main__.swap2[1]>", line 1, in <module>
next('BFFBFBFFFBFBBBFBBBBFF')
TypeError: 'str' object is not an iterator
**********************************************************************
File "C:\Users\manuel\Documents\Gent MaStat\programming and algorithms\workspace_python\homeworks\Week 5\looking_up.py", line 29, in __main__.swap2
Failed example:
next('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
Exception raised:
Traceback (most recent call last):
File "C:\Users\manuel\Anaconda3\lib\doctest.py", line 1321, in __run
compileflags, 1), test.globs)
File "<doctest __main__.swap2[2]>", line 1, in <module>
next('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
TypeError: 'str' object is not an iterator
def swap2 (cards):
"""
>>> next('FBFFFBFFBF')
'FFBBBFBBFF'
>>> next('BFFBFBFFFBFBBBFBBBBFF')
'FBBFBFBBBFBFFFBFFFFFF'
>>> next('FFBFBFBFBFBFBFBBFBFBFBFBBFBFBBFBF')
'FFFBFBFBFBFBFBFFBFBFBFBFFBFBFFBFF'
"""
# …
The function is called swap2 but within the doctests, you are using next which happens to be a built-in function that does something completely different. That’s why you are seeing that error.
At times like this, it’s really important to actually look at the error messages. It clearly told you what was called:
File "<doctest __main__.swap2[0]>", line 1, in <module>
next('FBFFFBFFBF')
So if you don’t know where that was supposed to come from, then check out the error message. Doctest will tell you what it is executing: swap2[0], swap2[1], etc. tells you the function name the docstring that is being executed is by doctest and which test case it is (0 is the first, 1 the second etc.). It even gives you the line number (within the doctest case) where the error appeared, and of course the line that was causing the error. So use that information to go to the problematic code, and figure out what the problem is.

Deep-learning library Neon DataIterator / ArrayIterator init error

When trying to set up the DataIterator as explained on neon tutorial.
from neon.data import DataIterator
import numpy as np
X = np.random.rand(10000, 3072)
y = np.random.randint(1, 11, 10000)
train = DataIterator(X=X, y=y, nclass=10, lshape=(3, 32, 32))
I encountered a weird error:
ERROR:neon.data.dataiterator:DataIterator class has been deprecated and renamed"ArrayIterator" please use that name.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "neon/data/dataiterator.py", line 168, in __init__
super(DataIterator, self).__init__(*args, **kwargs)
File "neon/data/dataiterator.py", line 82, in __init__
self.Xdev = [self.be.array(x) for x in X]
AttributeError: 'NoneType' object has no attribute 'array'
I then tried with ArrayIterator, keeping X, y the same.
ArrayIterator(X=X, y=y, nclass=10, lshape=(3,32,32))
With the same NoneType error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "neon/data/dataiterator.py", line 82, in __init__
self.Xdev = [self.be.array(x) for x in X]
AttributeError: 'NoneType' object has no attribute 'array'
Why would this be the case? Is there an easy fix?
Fixed the problem by generating backend.
from neon.backends import gen_backend
be = gen_backend()
(...)

Implementing offsetof() for structures in Python ctypes

I cannot seem to implement offsetof for a structure in ctypes. I have seen the
FAQ for ctypes, but either it doesn't work, or
I cannot figure out the details.
Python 2.6.4 (r264:75706, Dec 19 2010, 13:04:47) [C] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> from ctypes import *
>>> class Dog(Structure):
... _fields_ = [('name', c_char_p), ('weight', c_int)]
... def offsetof(self, field):
... return addressof(field) - addressof(self)
...
>>> d = Dog('max', 80)
>>> d.offsetof(d.weight)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in offsetof
TypeError: invalid type
>>> d.offsetof(weight)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'weight' is not defined
>>> d.offsetof('weight')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in offsetof
TypeError: invalid type
It seems addressof() does not work on structure members (e.g. d.weight). I have tried
other things involving pointer() and byref(), but no luck.
Of course I want this to work on all architectures, regardless of the size of a pointer,
and regardless of the effects of padding, so please don't say to just sum the sizeof()
for all previous elements, unless you can ensure that you're taking any padding the C
compiler adds into account.
Any ideas? Thanks!
class Dog(Structure):
_fields_ = [('name', c_char_p), ('weight', c_int)]
Dog.name.offset
# 0
Dog.weight.offset
# 4 (on my 32-bit system)
The task of turning this into a method is left to the reader :)
The trouble is that structure members are sometimes returned as plain python types.
For example
class Test(Structure):
_fields_ = [('f1', c_char), ('f2', c_char * 0)]
type(Test().f1) is type(Test().f2) is str

Categories

Resources