Can't get properties of GstBaseSink on GstXvImageSink in pygst - python

I'm trying to access the "last-sample" property from GstXvImageSink which is derived from GstBaseSink.
I'm getting:
TypeError: object of type 'GstXvImageSink' does not have property 'last-sample'
Is there a special way to get properties from base classes in pygtk or is this property somehow hidden?
Code:
def take_snapshoot(self):
sample = self.__snapshot_source_video_sink.get_property("last-sample")

My fault. I'm using gstreamer 0.1 and I was reading documentation for gstreamer 1.0
Everything that was reffering to GstBuffer is now GstSample.
After http://gstreamer.freedesktop.org/data/doc/gstreamer/head/manual/html/chapter-porting-1.0.html
GST_TAG_IMAGE, GST_TAG_PREVIEW_IMAGE, GST_TAG_ATTACHMENT: many tags that used to be of type GstBuffer are now of type GstSample (which is basically a struct containing a buffer alongside caps and some other info).
So in my case the answer is to use:
def take_snapshoot(self):
sample = self.__snapshot_source_video_sink.get_property("last-buffer")

Related

Setting value of a iterable property of a Python COM object

I am using pywin32 to automate some tasks in software that has an Automation Server technology interface (formerly OLE Automation Server).
This software comes with a somewhat detailed manual with code examples in VBA, C++ or Matlab but no Python. I have built a Python library that can do most of the functionalities built into the software but there are some parts I cannot do in Python.
I cannot change the value of a property if this property is contained in a iterable COM object.
What I can do:
[Documentation for Visibility property]
import win32com.client
app = win32com.client.Dispatch('NAME_OF_APP')
app.Visibility = True
As an example, with this code, I can change the visibility parameter of the software: if it runs with or without GUI.
What I cannot do:
[Documentation for getting and setting current device]
import win32com.client
app = win32com.client.Dispatch('NAME_OF_APP')
app.CurrentDevice(0) = 'NAME OF DEVICE'
I then get the following error:
SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
This error makes sense to me but I cannot find a way to set any of these software properties when they come in the form of an iterable object. As soon as I have to specify an index, I don't know how to set the value.
From what I understand, in C++ we are able to change the value because of pointers but how can we achieve the same thing in Python? Is it possible or do I have to use some C++ code in parallel to my Python to run my library? I don't know anything in C++ so if I could avoid doing that, it would be good.
What I have tried
Of course, the 1st thing I tried was to change () to [] or {} which logically didn't work.
Then I used the Evaluate function in PyCharms to see what was hiding behind my app.CurrentDevice. I was hoping to find sub-attributes that I could then set but I don't see anything inside the object:
[Result of Evaluate on the CurrentDevice object]
Finally, I have tried the following:
import win32com.client
app = win32com.client.Dispatch('NAME_OF_APP')
curr_device = app.CurrentDevice(0)
curr_device = 'NAME OF DEVICE'
I wanted to affect the object to a variable and then change the value but of course, this only rewrites the variable curr-device with 'NAME OF DEVICE' but loses any link to COM Object.
I feel like my questions are similar to the following unanswered question:
How can I set the value of an indexed property of a COM object in Python?
It looks as if win32com is struggling to set the property if there is an additional argument to the put function, which is a little surprising.
First thing to do is to use
app = win32com.client.gencache.EnsureDispatch('NAME_OF_APP')
This creates a Python wrapper for the COM object (rather than just firing function calls at the object and hoping). This may in itself clear up your issue.
If not, here is a quite ugly way of working around. As you have identified, the relevant part of the type library is:
[id(0x00000018),propput, helpstring("property CurrentDevice")]
HRESULT CurrentDevice([in] long lAcq, [in] VARIANT pVal);
And you can use this to set the property at a low level.
win32com dispatch objects are a wrapper for the PyIDispatch object. All dispatch objects support the Invoke method, and you can use this to call the function yourself. NB. Since I don't have access to your COM object, I can't test, so this answer may need some tweaking (!).
The PyIDispatch documentation
Try:
import win32com.client as wc
import pythoncom
app = wc.gencache.EnsureDispatch('NAME OF APP')
app.Visibility=TRUE
newVal = wc.VARIANT(pythoncom.VT_VARIANT,'NAME OF DEVICE')
app._oleobj_.Invoke(24,0,pythoncom.INVOKE_PROPERTYPUT,0,0,newVal)
There are a lot of 'magic' numbers here, but basically:
24 = 0x00000018 in decimal: this is the Id of the property
0 = the LCID, the Locale Id ... I always set it to 0
pythoncom.INVOKE_PROPERTYPUT = the type of call.
0 = whether you care about the return type (you probably don't = False)
0 = first parameter, lAcq, as in CurrentDevice(0)
newVal = second paramter,pVal, the new device name as a VARIANT
I haven't tried this, but pythoncom is pretty good about converting VARIANT types, so you might not need the VARIANT creation, and can just use NAME OF DEVICE directly as the parameter.

How can I access the custom property (Visibility1)of a Dynamicblock in CAD

I have a project which need some blocks defined with different forms (visibility), I need to know coordinates. Numbers of those different visibility of each dynamicblock.
I had tried GetDynamicBlockProperties method via pyautocad, it returned:
<bound method GetDynamicBlockProperties of <POINTER(IAcadBlockReference) ptr=0xb861dd4 at c62f3d0>>
In the manual help of autodesk Active Xreference Guide, I find those:
GetDynamicBlockProperties Method (ActiveX)
Gets the properties of the dynamic block.
Supported platforms: Windows only
Signature
VBA:
RetVal = object.GetDynamicBlockPropertiesobject
Type: BlockReference, MInsertBlock
The objects this method applies to.
Return Value (RetVal)
Type: Variant (array of DynamicBlockReferenceProperty objects)
Array of dynamic properties assigned to the block.
Remarks
A collection of DynamicBlockReferenceProperty objects is returned only if the block reference is a dynamic block containing custom properties.
Examples
VBA:
Not availableVisual LISP:
Not available
and my code is :
{
for i in acad.iter_objects('BlockRef'):
if i.IsDynamicBlock:
print (i.EffectiveName)
print(i.GetDynamicBlockProperties)
}
it returned :
'''
those names of each dynamicblock
<bound method GetDynamicBlockProperties of <POINTER(IAcadBlockReference) ptr=0xb35d23c at c01f6e8>>
'''
i have been stucked here for two days, can somebody help me to know what this return means and how to access the custom properties of a dynamicnlock, thx.
You have to iterate through the DynamicBlockProperties collection, something like this (not tested, I do not use Python):
for i in acad.iter_objects('BlockRef'):
if i.IsDynamicBlock:
print (i.EffectiveName)
for p in i.GetDynamicBlockProperties:
print (p.PropertyName)
print (p.Value)
bro. Has solved this problem about GetDynamicBlockProperties

access native QT-types with python in pyqt

I was wondering if it is possible to access native Qt-types in PyQt with an easy constructor?
Why is this an issue?
Recently, I had an unexpected error for setting a Flag:
opts = 0 # in this case, no flag was set
myQMainWindow.setDockOptions( opts )
QMainWindow.setDockOptions() expects a QMainWindow.DockOption: unexpected type 'int'
I expected the argument to have type int, because when you look at PyQt5\QtWidgets\QMainWindow.py, you will see these dockOptions defined in the same pattern:
class QMainWidget(QWidget):
AllowNestedDocks = 2
AllowTabbedDocks = 4
AnimatedDocks = 1
[...]
How I solved this:
I abused the type-function to get the underlaying class, from one of the allready known Flags, using:
dockOptionFlag = type(QMainWindow.AnimatedDocks) # not 'int'
opts = dockOptionFlag() # in Debug-Mode this is still shown as 'int':0
myQMainWindow.setDockOptions( opts )
Why would you need something like this in general?
When I recently was translating qt-code from c++ to python I realised that some Qt-classes, that are simular to python-classes, have additional functionality.
For example:
a QMap is essentialy a dict in python ... but further than that, it also provieds a functionality to add entries with an existing key, without overwriting (insertMulti()).
a QString is essentialy a str in python... but the function of left(n), which is simular to 'someText'[n:] is returning the entire string, if n is negative, which wouldn't be the case in my python translation.
I know these are simple examples and their functionality could be reimplemented in more or less time, but this is not needed since it's allready existing in the c++-wrapped pyqt.
Is their a pythonic way to access these native-types in python with an easy-constructor? something like: temp = qtType("QMap", *args, **kwargs)

convert python2.6 cantera1.8 to python2.7 cantera2.2

I am new on chemical network model. Currently I am converting a previous student python code to adapt the new version in the lab as titled.
firstly, a gas mixture from mechanism (pre defined) is defined
gas_mix = ct.import_phases(mech,['gas'])
then, I want to get the number of the species and use cantera nSpecies
nsp = gas_mix.nSpecies()
and I got the error message as
AttributeError: 'list' object has no attribute 'nSpecies'
Also I tried:
nsp = gas_mix.n_species
and it also shows
AttributeError: 'list' object has no attribute
Would you please kindly help me on this ?
Thank you and best regards,
YouBe
It looks like import_phases returns a list of objects--either a list of "gas mix" or just "gas" objects. I'm not really sure because this is very specific to the program you're working with.
Anyway, try looping over the values in the gas_mix and see if you can call the nSpecies() method or access the n_species attribute:
gas_mix = ct.import_phases(mech,['gas'])
for gm in gas_mix:
print(gm.nSpecies())
# or you can try this:
print(gm.n_species)
Maybe that will get you closer to what you want.
The function import_phases returns a list, which is useful for the case where you want to import multiple phase definitions from the same file, e.g.
mixtures = ct.import_phases(mech, ['gas1', 'gas2'])
where both mixtures[0] and mixtures[2] will then be a single phase definition. If you only want to define a single phase, it is easier to write:
gas_mix = ct.Solution(mech,'gas')
Or, if the mechanism file only contains one phase definition, just
gas_mix = ct.Solution(mech)
From here, you should be able to access the number of species as
gas_mix.n_species
Many of the details of migrating from the old to new Cantera interfaces are described in the documentation page "Migrating from the Old Python Module".

How to access unknown fields - python protobuf

I am working with a large number of message types with similar but not identical structure. All the stuff that's common among these is in another message. When a message comes in, I parse it using the common message type. However, I can't seem to find a way to access the fields outside of this type (i.e. the non-common fields). Is there a way to access the unknown field set in python?
Edit:
I just saw this in the documentation:
"If a message has unknown fields, the current Java and C++ implementations write them in arbitrary order after the sequentially-ordered known fields. The current Python implementation does not track unknown fields."
Does this mean that if I parse using the common type, eg:
proto = msg_pb2.Common()
proto.ParseFromString(raw_msg)
Any fields not defined in message Common are thrown away?
To someone looking for an answer to this, the reflection module helped me:
https://developers.google.com/protocol-buffers/docs/reference/python/google.protobuf.reflection-module
The relevant sample code:
Sample usage:
file_descriptor = descriptor_pb2.FileDescriptorProto()
file_descriptor.ParseFromString(proto2_string)
msg_descriptor = descriptor.MakeDescriptor(file_descriptor.message_type[0])
msg_class = reflection.MakeClass(msg_descriptor)
msg = msg_class()
Args:
descriptor: A descriptor.Descriptor object describing the protobuf.
Returns:
The Message class object described by the descriptor.

Categories

Resources