Re: [Gimp-developer] PF_ENUM, SF_ENUM, dynamically defined enums for plugins
- From: Ofnuts <ofnuts gmx com>
- To: Nathan Summers <rockwalrus gmail com>
- Cc: gimp-developer-list gnome org
- Subject: Re: [Gimp-developer] PF_ENUM, SF_ENUM, dynamically defined enums for plugins
- Date: Mon, 15 Aug 2022 12:18:03 +0200
Not really. With the current interface, I manage it that way:
I have these two functions:
def createOpts(name,pairs):
optsclass=namedtuple(name+'Type',[symbol for symbol,label in
pairs]+['labels','labelTuples'])
opts=optsclass(*(
range(len(pairs))
+[[label for symbol,label in pairs]]
+[[(label,i) for i,(symbol,label) in enumerate(pairs)]]
))
return opts
def createValuedOpts(name,triplets):
optsclass=namedtuple(name+'Type',[symbol for symbol,_,_ in
triplets]+['labels','labelTuples','values'])
opts=optsclass(*(
range(len(triplets))
+[[label for _,label,_ in triplets]]
+[[(label,i) for i,(_,label,_) in enumerate(triplets)]]
+[[value for _,_,value in triplets]]
))
return opts
"createOpts" lets one define a namedtuple where members have the proper
values, so for instance, after
GuidesShow=createOpts('GuidesShow',[('NO','No'),('YES','Yes')])
I can use "GuidesShow.NO" in the code and I don't really care if it was
defined as 1 or 0.
"createValuedOpts" adds a "values" array which is indexed by the other
members. "values" elements can be anything: integer/float constants,
strings, but also functions/lambdas so for instance I can define a
bunch of conversions:
Units=createValuedOpts('Units',[
('MM',"Millimetres",lambda x: x/25.4),
('CM',"Centimetres",lambda x: x/2.54),
('INCH',"Inches",lambda x: x),
])
So later in the code, assuming "units" is the value of the corresponding
parameter, converting whatever values to inches is just
inches=Units.values[units](input)
The "labels" member is also generated to be used in the registration:
optionsUnits=(PF_OPTION,'unit','Units', Units.MM, Units.labels)
Here also, I never have to worry about some behind-the-scenes integer
value.
On 15/08/2022 02:52, Nathan Summers wrote:
Sounds like a nightmare for scripting if I'm understanding correctly.
Rockwalrus
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]