Bestand wählen

Metaclasses for fun and profit: Making a declarative GUI implementation

Zitierlink des Filmsegments
Embed Code

Automatisierte Medienanalyse

Erkannte Entitäten
this talk that on understand guest is going to be telling you about using metaclasses to make a declarative theory on and then it's time for lunch there will be questions at the end what by the
way uh as she said I
will be talking about using metaclasses for making a declarative using protocol that we have a making indicate in declarative doing implementation I
will also be telling you about metaclasses it's a sort of an inspirational for stuff for you to use metaclasses on your own uh these work on
the back and that that inspired me to do is talk uh ID together with with my colleagues have explored and there may come from and I but 1st a bit about me uh I motivate my
five-star Python by some rating by bit having been at it since since by the model for uh I have been working at the open and since 2 thousand and 1 and I've been consulting all to label where we this work since 2014 and I do all kinds of stuff because we're
respond companies so I get to do everything but to start with them
does everyone know what American artists
take by half of you and I can tell you that you all use
them if you use the task of the key
word you use the built-in meta-cluster tight the year the
of the other things we will be talking about it is a bit will be seen by the a bit of to which is the going good library which we used and of course I need to tell you why we want this and this is uh water label makes the school labeling printers um you have the printed word printing out the label and has form that sticks all the boxes and they've been it's time to revise the software and and the running you
see the display that's that's all Python in GTK interface the 1 that's honest in this picture is running Python 2 . 5 and G 2 k 2 and where upgrading it to
2 . 7 G T K 3 and this is what the new user
interface will look like once once it's done so we want to be in a nice way of expressing the the layouts um the and
as I said we started with the legacy application is running Python 2 of my both running Python 2 and we had to educate you we decided In this process updated use case 3 they're mostly using the company commodities layer but trying to move wanted to the proper just K Street but this is this is
sort of the syntax that I dreamed up this is where we wanted to
go with the layout very critical back to the you see hierarchical you
have the uh sort of major groups of on the left the you go across go deeper and deeper in the type hierarchy which is I mean that's that's how Greece are believed to start with the window and then you have the dividing up the window in different parts and you have different widgets so this uh I have
some some code that you can download and play with and the code on the left we actually produce the year the when you see up on the right and just for comparison
this is what it would look like in in in the standard interface see syntax and I had to take out all the but for the white space to make the code fit on the slide but for the 1st part I will teach you about middle classes and some sure you've heard many times made excelsis along with some level stuff so it wasn't always done you on the Wizard so the metrics
that's the class of the class
uh as I said the beating metrics also that every class in Python uses is types and if you want to use on many tasks python to you you used Dundar metaclass in Python 3 you added a minute a keyword argument embarked the code examples are art license to are we try to mention the how to how to do it in Python 3 when there is a difference the example code that that you can download if you run it through pipes to up to 2 3 it will it will run fine on vital 3 there's a big Sierra illustration of of
of this what the metaclasses beat everyone knows what instances and model classes I hope so the relationship between the minute loss the clause is exactly the same as between exhaustively instance and of course these 2 questions can you have meta-meta classes and yes you can I haven't figured out the reason
why you would want to use in mathematics also know that a class but you can go on forever it's just down the middle
1 but if you specify the emetics loss in your metaclass definition it would work just just as well but I said
I have I haven't been able to figure out a use case for it but it if you do please tell me so the
message set you will actually be using 1 if you if you write traumatic loss is the London new which is the constructed you know it would if it returns the the object that you're instantiating uh you don't actually have to return an instance of the class period you know that your victory you can return whatever you want if you return 42 instantiating the source will the ability to take integer 42 will try not to do that it can be very confusing and uh
Dundar in it of course once you have your metaclass instantiated that is once you have your actually torso objects you're done in it we call them and you can do stuff there
In Python 3 there is to use this method that would have been useful for us in in implementing this is done to prepare prepare which is called before the clause speculation is that forced to uh returned the predict like object that we use be used to populate the namespace so if you don't want to use a standard Python In Python 3 you can you can dream up any any any dict objects savings for instance if he if you need
ordering which we need you could return ordered from the year number
prepare and then also be a difference here with Python 3 is that kw hours on the on the to prepare the you have the means keyword arguments for foreign new in into also and they come from the clause inheritance list if you if you had keyword arguments there they will get passed into these to the constructor methods
which can be useful in some cases if you need to customize your your medical right so if
you add other methods on your a pneumatic cells which you may want to do because you don't want to have 300 lines of new uh those methods will be visible on the loss but not on all the instances of the cost which it's good to know because you be less
confused about white its white white and I do we call this so
messages on on on the class but not on the instance when how and where to go how it was on the metaclass right um so
thing I wanted to sort of take back from this about medical that class declaration syntax and buy from you see on top it's really only syntactic sugar for instantiating the meta cost so the top and the bottom code the results the fool class it's exactly the same once you run the code you cannot tell the difference between between these 2 so try to
remember that that you can use the class declaration syntax to instantiated instantiated source essentially of the medical
studies the that gives you lots of power due to the use the costs syntax for things that but there's sources but not really this standard Python classes which is what I've been uh that which is which is sort of the idea that I used to to dream up this going the class
declaration syntax and just for comparison again just not
all of it but at the top this is the declarative syntax that would look at how we did bottom is the what it would look like if we have uh I just have a little pause here to see if if you're following along by the any questions on the on the moon automatic sources intuition we
continue to look at the code the right
uh as I said that 1 of the 1st problem we ran into was that In a standard costs you just get the date with with all the attributes in some random order and when years it's very gooey you obviously you want to you want to be sure that whatever you put on top the 1st spot in a new
window it shouldn't randomly appeared somewhere in the middle you want the water well defined so and since we were using part 2 we couldn't use the so the this was really the I'd say was the hardest bit of of making this work figuring out how to do is not that it was very hard but the number of I've been at this for bit so which
I might not be the 1 who should be judging how hard it was a what what I did was that as long as the containing 2 the the actually used in the in the class were also
instance this of our major I would be able to to sort of detect when they were created um and this for our application since if everything was going to be a a 2 we get this this was no problem everything was going to be uh instances of our parliamentary class uh so whenever you have had the crossing the on the in on the inside I would but I think I should actually back up a bit to to this slide um the way if you
look at the at the top of the way that the metaclasses get instantiated is that it it will instantiate from the from the inside out because is going to be an app tribute in the group clocks so the David would have to be created 1st in order to be able to be placed in the namespace of group and so on uh up to the top so therefore
when 1 on the left side since uh when label is created I will remember that out now David was created and then when we get to groups OK let's see what what has been created recently and I'll go through the list of of of the recently created a sources and and and and the and the order they were created in the the op they was created recently is enabling my in my namespace it OK so that 1 must have been 1st and then
I of course once once I
found them I will remove them from this list and if we wasted wasted space was not really because you will be keeping the covers around anyway but if you have the list of of a few of the top-level classes stand still remaining because there's no metics loss for the for the module basically due to clean clean up this list but that's small enough that doesn't
matter uh and I take this opportunity to to have my 1st the GATT quirk that I ran into was and that was that the the G
object also has made a cost and he took the edge and realizes that 1st and also really confused and why can I not inherit the DTK we just in my widgets and it turns out it was because the geological so Hadamard class and they were colliding uh but once I inherited
the the g object meeting minutes as in this you know type it's type both of you it's it's a big amount of magic built-in because if you if you you just use type and as sits in based metaclass but if you call when with with an object even returned its class and since g object is already exhausted will be the Mac the so yes
this is the this is our a bomb cost for are are basic widgets
uh and you see here with 1 I actually I think a mystical were actually add maybe that's on the next slide what with C and so here anyway these this the
attributes class of the attributes of medical such beauty would be in this case that will be populated with with the list of of classes assessed instantiated and there we go through it and look for that whatever is in my name space appended to my local or list of order that use and and then remove them and another as some sure most of you know you cannot remove stuff from the list while you're iterating over can afford of because then you win miss stuff namespace is the big that is uh create
created from let's see we for
instance if you look at the top the the namespaces did that this is created from from the attributes in in the class declaration so it in the case of top here namespace would have titled Low world and it would have group which would be the the instance of the group cost so
you through therefore for for
instances of what classes really instances of the medical so it's a bit confusing with the metaclass because you have met a class instances which are classes so the terminology gets a bit confusing the right and then at the
end I will of course instantiates the according the the the superclass uh gonna new which will instantiate the reaction fast very last year I forgot this code is actually wrong I thank it on the slide and the code that is downloadable is slightly different so that 1 is right this should be it the returned from the from the donor new I should of course of savings someplace because that's the actual cost that needs to be passed on up yeah so what
you've seen this a couple of times but using that that many across you just saw assuming that you actually return
the result from the super governed under new uh this code will will get you a a attributes ordered active uh attribute on the top floors which will be the ordered list of the inside sources that served that we saw the 1st problem that we can actually water stuff in In our classes the next 1 is is we
want to do of course you want to be able to detect properties on the on the gtk we widgets and also on an our own complex with its
we have special widgets that deal with settings and they know what kinds of magic stuff there so in the case of window title there should obviously set the title of the window and indeed to case the title of the window is property uh which is not a standard some property but it's and different than and uh
22 add some code to set it in the proper way and also I wanted to be able to see if our code has a mass of standard by property I wanted to be able to set that property that uh in the Declaration so for instance in normally if you if before we get has a property the so setting so in a in a sub process like this would overwrite the property and you would you would use
whatever centering get the Matisse message you had and just have a standard 42 in the class dictionary but with some magic you can make this syntax instead called the center 1 way
I thought about maybe we use in doing this was to use a central nematic thought because and maybe it it could be called 1 month to set the 42 and and you can do stuff with the minimum loss in in in the property on the metaclass but it turns out that the the namespace the initial namespace doesn't bother about uh calling that doesn't bother about looking for properties so that in March but so basically what
what I do is I got a year and attribute on the on the clause boundary defaults
which I will populate with with any person in a medic tossed when you when you write your this is when you have your your your here's the basic in in this example when you creator full before we get lost
uh it will look through your attributes and C are any of these attributes properties I there is it in the in the case of the some property would look for a Dundar sets a on the attributes so if if if so in that in that case had been a year a property the property will haven't under section to 2 that would get cold when you whenever you change the yet to be property asserted that subject is enough for another
day for an entire other talk show because the you really useful to but often get into them too much here and in the case of the DTK properties there's a props that's a view on the GTK
+ so I looked for instance if suture if the attribute you're trying to set this it's in that 1 and if it is so I would I would take the the atoms to find such and and move it to this thunder default dictionary this and then the
cooperation from from the base we need the and this is the normal class this is not the metaclass uh 1 when you instantiate that I look through this thunder defaults which is in the class
and and if there is anything in there I will set after that and since they remove the attribute from
from the costs if we now call the the property settlement is that some of the work of roundabout way but it works quite nicely and of course if it's if it was a in GTK property I would call the the set after on the on the props attribute instead right I should mention the
the thunder map MRO how many of you know what what Don grammar all is but it's about half an I want to mention it because I used it obviously it's in the quarter the year ordered that you should look through your base classes uh when you're looking for active units or method so for instance here I put up the EU EU funded by moral of strings just as an
example uh she's supposed to start with the first one and go through them and once you have come to the class were that that has the message you're
looking for you should call latch and normally you do that using super if you or involving at award usually if you if you just say
food of more ice and we look for bar for you but in this case I needed to of looks through the myself and it turned out and all my use cases that it was much easier to just over right what I found that it was to to you can see I said to you know is it here always it's here so I did I walked into a water in the reverse and just make sure that the top 1 1 in the end there's it
really when say complicated but it's a really long description of how the MRO is calculated on this Europe forum if you're interested right and
uh then of course the next next order of business is instantiating the with widgets you get traction window and all the action we gets inside the window if you this do nothing
special instantiating talk we get you talk and it would have attributes in at the top but group would but that would still be the the cost group which is not to the the actually books that you want uh a simple way to do this is of course in the base class when you instantiate tall had its intern go through it's attitudes and instantiate whatever they are uh and it I mean and then of course it needs to be since it's to decay the we get sick it obviously needs to put them somewhere also but the base cost cont know where to put them so that that each individual we did would have to figure out where to put the and so that code like looks like
this and again I have I'm working backwards through the MRO to 2 to find all the widgets and because I don't actually know if I needed to do this but I wanted to to instantiate him in the same order that that they were listed and then I replaced the or what I set the attributes ordered list on the class with the instantiated attributes instead of the instead of the courses because I from but what if I needed the class like and go through the of make stuff look a little bit nicer I didn't have to invent new names and
then for the lost order business we have uh we wanted to do make sense and GTK doesn't allow multiple inheritance the I hope you can see the problem here by someone doesn't know that take doesn't polemics and so it would happen if you try things breaking strange and mysterious way sometimes uh but it turns out that you can if you do nasty things with the adjudicated middle-class you can so for work around the fact that you can't subclass you dedicate crosses just through the
code of the basically if we find it in the in America
also from that we have more than 1 base class officers multiple inheritance we may have to do some the redundant G type is
set on on all of geometry classes so if you have a redundant genes have you know this is a this is the GTK OK I'm getting into dangerous territories here and if I do it turns out that I want to put an extra layer in between I don't know why this works but if it helps for certain certain certain books or what features ideas in in integer k and so I quickly indents 1 more level of of source hierarchic hierarchy to to sort of bring in all the all this on my base classes into 1 and then I can add my stuff to to to looks through here and and find stuff and put it all into to my name space eventually instantiate
mind it I don't know why it works but it does this yes finally we
have the UN is I hope you've learned a bit more about what many classes are and what they can be used for it is
obviously how how you're lost object is is investigated you can go customize it you we achieve and I also hope you've been in spite of to 2 trying abuse
Python to doing what you want in give you a nice syntax for your problems not not not everything that you have is this beautiful cold but hopefully you can
make it a little bit more beautiful I have called for you to play with it's it's a little bit bigger than than what I've shown here if we're on on on by some 2 as it is if you posit through part 2 to 3 when Ronald by 3 I have tested
both versions so it should work famous last words you know but figure out your hopefully no 1 was and then I should just put up the
New York forward you find the
code hi so 1st of all I think very much for the book to includes
do 1st talk about that addresses reduced that but section my 2nd talk about
metaclasses not a museum at the conference yeah OK so I think we all agree that each of the 2 classes are 1 of the that's
features in Python of our error and so on so I a bit of a problem all we committed to this is the closest arms In some design patterns are you would want to use multiple with the classes for 1 class my hair and the magic methods of underscore underscore metaclass takes only 1 argument air into the solution produces a breed all conversation then you have to great on on web-search classes from our that takes mixings estimate classes and and so on to roll down a detrimental to your class and I was wondering if you have like a better solution no are not really in book um
I suppose that for some things in in Python 3 you could use a keyword arguments but I'm not sure if it it depends of the but supplies and do not have a the next question and
you not then undue afraid of new develop becomes the year-to-year millions like 2 stars of the stars on his bed so you won't be able to follow what you've been written actually not
because once you once you look at look at it it's it's mean yes you have to grasp that you have 1 more level of of cost instantiation but once you've past that is not really that difficult mean I don't have you ever you ever written mn last
world the only other place I actually actively used them internal but that's a little bit different story areas following
I I hope you take a look at the example code I have here so you can hopefully see that it is not so not so much magic I think mostly slapping is is telling people and document documenting how to how to use the not so much the difficulty but as it as as as always having a good documentation makes hard problem simple hi things so this
presentation I was not aware of many classes and I tried to solve for the same problem with good reason that you did without metaclasses and looks quite agree on I just wondering when you and your code you get any problems with having so many nested classes not known having there is 1
uh 1 small problem with having the nested classes and that is that it's hard to find uh 1 when you're when you're in 1 of the nested sources if you cannot easily gets uh the what if you're if if you're in the bonded new abandoning the init method of 1 of the nested sources it's hard to to find it it's actually uh class instances the so because the class outside of hasn't been completely initialized but that's the only problem with with the with the nested sources I should I can do this just to make it the here what I'm talking about here when you were when you read in
in the dubbed under title uh at the start of the dominant of title group and told don't
exist yet so you can't say topic group of title if you need to call for instance super but other than that it's no problem thank
you very much and I just want to say that I think you should be title your talk to I never missed a class I didn't like how but the book
In this button argued so that
and it looks like what you've done is taken out due to objects was a pretty horrible to use initialize and you've made a much nicer API declarative syntax for writing it so I guess if I was starting to write gtk recovered from I would rather use you system the default 1 so um have you thought about open source in your package and making a sort of adjudicating 0 there and contributing about making the world that I I have
uh what what we're using right now is is a bit too deep in our in our system with all kinds of strange stuff which doesn't really apply to generate system uh I hope that the code that I'm in that would appear it can grow into something like that uh I would happy to not be happy to try and and and sort of have better long and
the coherence of course of 1 more question there is about to unit test culture right now Liu flow we have we have
unit tests were pretty much everything is not it's not and there's units test with the example for the for the implementation to is that has
done thank you very much
Offene Menge
Domain <Netzwerk>
Umsetzung <Informatik>
Desintegration <Mathematik>
Formale Grammatik
Deskriptive Statistik
Reverse Engineering
Ordnung <Mathematik>
App <Programm>
Kategorie <Mathematik>
Reverse Engineering
Güte der Anpassung
Einheit <Mathematik>
Rechter Winkel
Ordnung <Mathematik>
Objekt <Kategorie>
Klasse <Mathematik>
Räumliche Anordnung
Wrapper <Programmierung>
Überlagerung <Mathematik>
Endogene Variable
Ganze Funktion
Attributierte Grammatik
Hierarchie <Mathematik>
Protokoll <Datenverarbeitungssystem>
Open Source
Komplex <Algebra>
Attributierte Grammatik
Wort <Informatik>
Prozess <Physik>
Kartesische Koordinaten
Komplex <Algebra>
Einheit <Mathematik>
Deklarative Programmiersprache
Mixed Reality
Konstruktor <Informatik>
Arithmetisches Mittel
Ganze Zahl
Deklarative Programmiersprache
Kategorie <Mathematik>
Hierarchische Struktur
Zellularer Automat
Kombinatorische Gruppentheorie
Physikalisches System
Leistung <Physik>
NP-hartes Problem
Physikalisches System
Deklarative Programmierung
Objekt <Kategorie>
Mapping <Computergraphik>
Formale Sprache
Innerer Punkt


Formale Metadaten

Titel Metaclasses for fun and profit: Making a declarative GUI implementation
Serientitel EuroPython 2016
Teil 58
Anzahl der Teile 169
Autor Hammarquist, Anders
Lizenz CC-Namensnennung - keine kommerzielle Nutzung - Weitergabe unter gleichen Bedingungen 3.0 Unported:
Sie dürfen das Werk bzw. den Inhalt zu jedem legalen und nicht-kommerziellen Zweck nutzen, verändern und in unveränderter oder veränderter Form vervielfältigen, verbreiten und öffentlich zugänglich machen, sofern Sie den Namen des Autors/Rechteinhabers in der von ihm festgelegten Weise nennen und das Werk bzw. diesen Inhalt auch in veränderter Form nur unter den Bedingungen dieser Lizenz weitergeben
DOI 10.5446/21094
Herausgeber EuroPython
Erscheinungsjahr 2016
Sprache Englisch

Inhaltliche Metadaten

Fachgebiet Informatik
Abstract Anders Hammarquist - Metaclasses for fun and profit: Making a declarative GUI implementation When overhauling the user interface of Autolabel's labeling printers, we wanted a clean way to describe the layout of the settings widgets. The structure we came up with was a declarative class layout that leverages Python's metaclass concept to build the underlying GTK widget structure. I will present the implementation to hopefully inspire you to apply metaclass techniques to problems that standard Python syntax can't quite solve. If that fails, you will at least have a way to declaratively construct GTK GUIs. A short, non-exaustive, summary of concepts I will mention includes metaclasses (obviously), class hierarchies, method resolution order, super(), and anecdotes of dealing with GTK.

Zugehöriges Material

Ähnliche Filme