Re: [Tutor] Modifying Source Code while Program is Running
Ed Singleton wrote: > On 26/11/05, Alan Gauld <[EMAIL PROTECTED]> wrote: >>>I'm sure Python is quite possibly the least worst at this, but that >>>doesn't make it good at it. >> >>Which brings me back to my original question, what environment >>do you think is good at it? Are you aware of such an environment >>or merely wishing that such a thing could be invented? > > > I know that such a thing could be invented. It's very > straightforward, it's just a lot of work. If enough other people > think that it would be useful then it will probably get done at some > point (and I think the rise of web applications will bring it about as > an inevitability). You might be interested in this thread on the jython-dev list; Paul Fernhout is trying something similar to what you want: http://sourceforge.net/mailarchive/forum.php?forum_id=5587&max_rows=25&style=flat&viewmonth=200511&viewday=26 (read from the bottom up) Kent -- http://www.kentsjohnson.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
> However, I'm still slightly uncomfortable with it. It smells to me a > bit like the "as long as it works, its fine" school of thought. FRom the client point of view thats true, but from the class designers point of view its very important that the internals be we well designed. And that includes how the internal objects and ultimately data are represented, but its a much smaller (and therefore easier) problem than trying to solve the total data picture. In this regard objects are an abstraction layer on top of the data, we focus on the entities and let the fields take care of themselves (or let the entity designers do it for us!)... > defined, as at the end of the day it's the data that matters Only at one level. You are right that the inputs and outputs of a program will be expressed in terms of data, but the program itself is an expression of behaviour. If I feed this in you transform it to that out. You can feed the data in as one big tuple ands feed it out in the same way, what happens to it in between is not important so long as the output is correctly matched to the input. > data is given to me by other people and can rarely be gotten back). The trick is not to change the inpuit data but to generate new output from the input. This is standard functional programming practice and good to apply in procedural, OOP or functional programming styles. >> Whereas in OOP you simply say > >> obj.doThing() >> > But writing a different doThing() for each object can be a huge waste > of time. You want to be able to write one doThing() that is going to > work on each object (of a particular type). This requires either > knowing that all your objects are going to be similar in some respect, > or writing a huge if..elseif as you mentioned. On the contrary its nearly alwys more efficient to write a separate function that gets called polymorphically than have long if/else chains. Not least because: a) you wind up with lots of if/else chains - one for every invocation of the polymorphic method b) the if/elif chains become a big maintenance headache requiring a revisit to each one every time you introduce a new type c) the invoking function becomes vulnerable to changes in the internals of the called objects, introducing high levels of coupling which is "a bad thing" Avoiding these kinds of chained if/else structures is one of the primary goals of good OO design, and one of the biggest improvements in reliability and readability brough about by OOP. But it does require a wee bit of faith initially to trust it! :-) However the many functions are not as bad as they sound because you don't have to write the whole function out again, only the differences. For example: class C: # some data bits here... def doit(self) # process the data bits pass class D(C): # some new data bits here def doit(self): C.doit(self)# get C to do its bits first # process my data bits pass class E(D): # data def doit(self): D.doit(self) # do my data bits pass So each subclass only has to process its own data and call the superclass method. The superclass call can be at the start, end or in the middle of the method - in Lisp terms this is called BEFORE, AFTER and AROUND methods... And if you think about it the data all has to be handled anyway so the extra lines of code is actually very few, much fewer than the accumulation of multiple if/else chains, and much easier to maintain because each object is responsible for its own data and nobody elses! > Even just saying every object has a doThing() is starting > to create a data structure. No its creating a behaviour structure, the two things are very different. Data structures are by definition static, behavour stuctures are dynamic. It is the difference between static data structures and dynamic behaviour structures that gives OOP its power. Every time you rely on a static data structure within an object you are limiting its ability to be reused. >> But that's behaviour of the Page, just let it create its own form >> and do its own searches, not your problem. Create the right >> kind of Page and it will do the work for you... > > But then I've got to create lots of different behaviours instead of > one simple generalised behaviour. Correct, thats what OOP is all about, lots of very short simple, (and thus reliable) methods. But you can parameterise them but often thats doine by passing other objects with their own behaviour. Thus class C: def meth(self, startObj, endObj) startObj.doit() doSomeOtherStuff() endObj.finish() So by passing in different objects we can completely change the behaviour of meth, but not by relying on data just passing in the objects we are working with andletting them do whatever it is they do... > > So maybe attributes are objects too? Maybe you page needs >> to know how to handle attributes and you can create pages by >Th
Re: [Tutor] Modifying Source Code while Program is Running
> I had the misfortune a couple of days ago of having to knock up a couple > of web pages in ASP. A very basic search form, results page and details > page. I had to open up a connection to the database, open a recordset, > concatenate a strings to make an SQL query, etc, etc. > > It was horrible. I copied and pasted a lot of the code, but even so it > involved doing ridiculous amounts of unnecessary work. Hi Ed, I don't know how well ASP supports abstraction, but copying and pasting is usually the wrong solution: that's exactly where we should be thinking about writing functions and organizing those functions in libraries. If a block of code is being copied around, that unit of work captures some concept we have, and functions are a good way to formally capture that concept as an abstraction. We can --- and often must! --- lift ourselves up by using abstractions, or risk drowning in the tedious details. Guy Lewis Steele Jr. wrote a hilarious talk about this in "Growing a Language": http://www.brics.dk/~hosc/local/HOSC-12-3-pp221-236.pdf > > Basically I just wonder what your background is that you seem to > > expect these kind of dynamic facilities? I'd certainly be interested > > in investigating it further. > > Maybe it's just knowing what'll be possible in 10 years time and being > impatient for it. If we're chasing for a "silver bullet" in programming languages, we might wait for a very long time indeed. *grin* ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
>From this and what Alan said, I think I'm starting to get it. Each > attribute could be an object or an entry in a list. Each type would > be a collection or list of a particular set of attributes. The > collection of types would be an object which is easily persisted. Absolutely! > Can you create an object whose default action is to return a certain > value? For example could I have a header object that where when you > call page.header it returns a value but I can also have > page.header.update(). Absolutely, in fact its quite normal. > Or (I think) I know that methods can have > attributes, but can attributes have methods? They can if the attributes are objects in their own right... HTH Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
On 26/11/05, Alan Gauld <[EMAIL PROTECTED]> wrote: > >> point. Classes express behaviour, the data is only there to support > >> the behaviour. Thats why methods are polymorphic but not attributes. > > > >If classes express behaviour, then what expresses the structure of the > > data? > > Why do you care? If the program behaves as you expect it what does > it matter what data it uses or how. That should be hidden from you > inside the classes. Worrying about data is a feature of traditional > Information Modelling/Structured Analysis style programming, > OOP is all about inter-communicating objects sending messages to > each other, each requesting and providing services to the others. I get this now. It goes back to what I said earlier about the "I don't care how it works" philosophy. Each object doesn't care how any other works. You could completely re-engineer the internals of an object and that would be fine as long as the interface is the same. As long as the methods and attributes keep returning sensible values then everything is fine. However, I'm still slightly uncomfortable with it. It smells to me a bit like the "as long as it works, its fine" school of thought. For my own sanity and peace of mind I like to have my data structures well defined, as at the end of the day it's the data that matters (I can rebuild functionality but data is given to me by other people and can rarely be gotten back). > > (ie what attributes there are going to be and what values they > > are likely to accept). > > You need to think about the objects that you pass around as part > of the messages, but those obnjects are in turn accessed via messages > so their structure is not important. What does matter is the > relationships between the objects, and they are related by message > paths not fixed data relationships. This switch in thinking is the > fundamental difference between traditional and OOP design. > > > You need (the option of) a data definition in order to generalise. > > No, you need the ability to modify behaviour. > Most traditional programmers think in terms of modifying > behaviour in terms of big if/elif trees > > if obj.type == sometype > doSomeTypeThing() > elif obj.type == another > doAnotherTypeTHing > etc... > > Whereas in OOP you simply say > > obj.doThing() > > And the right kind of doThing will happen because obj > knows how to respond to that message in the appropriate way. But writing a different doThing() for each object can be a huge waste of time. You want to be able to write one doThing() that is going to work on each object (of a particular type). This requires either knowing that all your objects are going to be similar in some respect, or writing a huge if..elseif as you mentioned. Even just saying every object has a doThing() is starting to create a data structure. > > Using my web server example from earlier, you need to be able to say > > that for any type of page, whatever it's attributes, you can create a > > web form to search for that type of page by iterating through it's > > attributes and creating a relevant form field for each type. > > So you want, for a given page to create a Form and search. > > But that's behaviour of the Page, just let it create its own form > and do its own searches, not your problem. Create the right > kind of Page and it will do the work for you... But then I've got to create lots of different behaviours instead of one simple generalised behaviour. > > I definitely had the opposite in mind. The behaviours of all the > > classes would be the same (and would be fairly simple, a method for > > rendering the page with a template, a method to update the data based > > on a form submission). > > Thats fair enough and the parent Page class would do just that, > but the template class would have methods that the Page called, > and you can subclass or data drive the templates to return the > appropriate strings to the page. (Since web pages after all are > just collections of strings - or evenone big string...) > > > Users would basically be able to change the > > data structure (add and remove attributes). > > So maybe attributes are objects too? Maybe you page needs > to know how to handle attributes and you can create pages by > adding attributes from a pick list, each attribute knowing how > to render itself and how to respond to searches? This combined with what Kent said, is what really solved the problem for me. > There are lots of ways to address this is we think of the world > in terms of intercommunicating objects not static data structures > with a few overeaching functions controlling it all. I've really gained a huge amount just from this one topic. Not least of which is that classes aren't necessarily a good answer to OOP problems. > > I'm sure Python is quite possibly the least worst at this, but that > > doesn't make it good at it. > > Which brings me back to my original question, what environment > do you think is go
Re: [Tutor] Modifying Source Code while Program is Running
Hi Ed, > Maybe it's just knowing what'll be possible in 10 years time and being > impatient for it. The problem is that over twenty years ago when I was at university the Japanese anounced that they would have launched a 5th generation computer language by 1990. They gave up and we are still waiting. Part of the problem is that we are familiar with how easily our brains can perform certain types of thought process so we asssume it must be easy to do on a computer. But our brains work very differently to computers and attempts to mimic the brain have so far been dismal failures. Sadly, I think 10 years will see less progress in software than you hope! Pessimistically, Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
On 26/11/05, Kent Johnson <[EMAIL PROTECTED]> wrote: > Ed Singleton wrote: > >>>This immediately seemed to me to be a case for classes. > >>>You provide a way for a user to create a new class by > >>>subclassing the page class (from their point of view > >>>probably through adding a few new fields to > >>>a form). > > > > The behaviours of all the > > classes would be the same (and would be fairly simple, a method for > > rendering the page with a template, a method to update the data based > > on a form submission). Users would basically be able to change the > > data structure (add and remove attributes). > > It seems to me that rather than having a new class for each type of data, you > need a class whose attributes are dynamic. I imagine you could have a list of > page types, each of which has a list of attributes. A new page type is > created not by defining a new class but by making a new entry in the types > table. The data for a page might be as simple as just a dict, or maybe it > makes sense to wrap it in a class, but the class would be the same for all > pages. A page would be rendered by combining a template with the dict > containing its data. Form submissions would be handled by referring to the > list of attributes for the type and extracting the corresponding data. > > You might have a class to wrap the page data and another one to wrap the > types table. These would provide operations on the contained data and be > common to all types. > > For persistence you could take a couple of approaches. You could persist the > lists and dicts directly using pickle or shelve or an object database. You > could persist them in a relational database by using a generic table with one > row for each data item - the columns would simply be object id, column name > and data. SQLite might work very well for this as it allows multiple data > types within a single column. > > The shift is from looking at the problem as one of dynamic classes, to one of > dynamic data. Python is excellent for working with dynamic data! > > BTW have you looked at Django? I don't think it is quite as dynamic as you > want on the model side but it has a lot of support for quickly generating a > presentation of a changing model. > http://www.djangoproject.com/ >From this and what Alan said, I think I'm starting to get it. Each attribute could be an object or an entry in a list. Each type would be a collection or list of a particular set of attributes. The collection of types would be an object which is easily persisted. I could pass the type of the page as a parameter when I create it, and could have a method in the class that looks up the particular type and adds all the relevant attributes to the object with sensible default values. Adding new pagetypes would be easy enough cause they're just an entry in the types list. Can you create an object whose default action is to return a certain value? For example could I have a header object that where when you call page.header it returns a value but I can also have page.header.update(). Or (I think) I know that methods can have attributes, but can attributes have methods? I need a bit more time to sit down and think this through (I've gained a lot of information and at least a little enlightenment over the last few days), but I think this is really starting to come together. Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
On 25/11/05, Alan Gauld <[EMAIL PROTECTED]> wrote: > > Hi Ed, > > This is a longish response because you are raising some > very interesting (and deep) issues from a computer science > point of view. > > > Well, self-modifying isn't inherently necessary. What I guess I > > really need is persistent classes as well as persistent objects. > > Python classes can be persistent just like any other object, > theres nothing intrinsically different between a class object > and any other kind of object (except that classes as objects > are kind of mind bending as a concept!) > > > I always tend to think of classes as templates for objects rather > > than factories. > > They are both. > > > In my mind, object methods are just calls to the class which > > are evaluated every time they are called. > > The methjods are function attributes of a class which are selected > by the class dispatch mechanism in response to messages sent to > instances. In Python that means the getattr() methods look up > the right method in response to receipt of a message. > > > Objects should be strict instances of classes that can't be > > modified except for the values of their attributes. > > Attributes in a pure OOP language include both the contained data > and functions. In other workds methods can be added/removed and > modified as well as the data. In fact many would argue that the > internal data not only cannot be modified externally but should not > even be seen - the Data Hiding school of thought. > > > Class Page(object): > >def print(self): > > printPage(self) > > What does that gain you? > > > And have all my methods call functions (passing on parameters as > > necessary). That way if I change a function, it will be changed for > > every instance of every object of that class. > > You mean like this: > > >>> class C: > ... def m(self): print 'm' > ... > >>> def f(o): print 'other m' > ... > >>> c = C() > >>> c.m() > m > >>> C.m = f > >>> c.m() > other m > >>> > > > And couldn't I write a function that would add functions or attributes > > to classes and objects? > > Of course, just like I did there. > > > Am I trying to use the wrong language for this? I love Python but I > > seem to keep coming up against lots of practical issues with it > > Python is a pretty good language for this kind of thing, > its just that this kind of thing is intrinsically difficult. > The only lanuages I know of which are better than Python > at it are Lisp and Smalltalk (and even Smalltalk > has some limitations compared to Python). Do you have > experience of a language which is more flexible in this regard? > I'm curious because you seem to assume that its almost > normal to be able to do these things, whereas in my > experience its usually impossible rather than just difficult... My background is in languages that are horrifically worse than python, but I'm overly demanding I guess. I think I have a good instinct for what computers should be doing for me. Everytime I'm doing something repetitious I get exasperated because I know the computer should be performing the repetitions rather than me. I had the misfortune a couple of days ago of having to knock up a couple of web pages in ASP. A very basic search form, results page and details page. I had to open up a connection to the database, open a recordset, concatenate a strings to make an SQL query, etc, etc. It was horrible. I copied and pasted a lot of the code, but even so it involved doing ridiculous amounts of unnecessary work. Maybe I'm just lazy, but then most progress has been made by people who were either curious or lazy. > > define the behaviours I want without having to bother with how the > > computer is actually going to handle them. > > Believe it or not thats been the holy grail of language > designers since the days of COBOL - The Common Business > Oriented Language - which allowed business people to > write theitr own programmes in "standard, everyday English", > like: > > PROGRAM ID - HELLO > > PROCEDURE DIVISION >MAIN-LINE-MODULE >MOVE 0 TO COUNTER >OPEN INPUT IN-FILE >PERFORM READ IN-FILE UNTIL END-OF-FILE = "YES" >ADD 1 TO COUNTER >DISPLAY "LINE NUMBER ", COUNTER >CLOSE IN-FILE >STOP RUN. > > At the time it was considered revolutionary and programmers > trembled at the prospect of instant redundancy. As it happened > your average businessman found COBOL about as readable as pigin > English! But that hasn't stopped us trying... > > One thing we have discovered is that the more natural the > language seems to get the less flexible the language gets. > Python is the closest I've seen yet to a general purpose > language that is truly readable, but thats still a long > way from being plain English! But I digress :-) I can quite imagine that natural language isn't the way forward, though I can also imagine that for business reasons readability won't be the way forward (however I do love readab
Re: [Tutor] Modifying Source Code while Program is Running
>> point. Classes express behaviour, the data is only there to support >> the behaviour. Thats why methods are polymorphic but not attributes. > >If classes express behaviour, then what expresses the structure of the > data? Why do you care? If the program behaves as you expect it what does it matter what data it uses or how. That should be hidden from you inside the classes. Worrying about data is a feature of traditional Information Modelling/Structured Analysis style programming, OOP is all about inter-communicating objects sending messages to each other, each requesting and providing services to the others. > (ie what attributes there are going to be and what values they > are likely to accept). You need to think about the objects that you pass around as part of the messages, but those obnjects are in turn accessed via messages so their structure is not important. What does matter is the relationships between the objects, and they are related by message paths not fixed data relationships. This switch in thinking is the fundamental difference between traditional and OOP design. > You need (the option of) a data definition in order to generalise. No, you need the ability to modify behaviour. Most traditional programmers think in terms of modifying behaviour in terms of big if/elif trees if obj.type == sometype doSomeTypeThing() elif obj.type == another doAnotherTypeTHing etc... Whereas in OOP you simply say obj.doThing() And the right kind of doThing will happen because obj knows how to respond to that message in the appropriate way. > Using my web server example from earlier, you need to be able to say > that for any type of page, whatever it's attributes, you can create a > web form to search for that type of page by iterating through it's > attributes and creating a relevant form field for each type. So you want, for a given page to create a Form and search. But that's behaviour of the Page, just let it create its own form and do its own searches, not your problem. Create the right kind of Page and it will do the work for you... > I definitely had the opposite in mind. The behaviours of all the > classes would be the same (and would be fairly simple, a method for > rendering the page with a template, a method to update the data based > on a form submission). Thats fair enough and the parent Page class would do just that, but the template class would have methods that the Page called, and you can subclass or data drive the templates to return the appropriate strings to the page. (Since web pages after all are just collections of strings - or evenone big string...) > Users would basically be able to change the > data structure (add and remove attributes). So maybe attributes are objects too? Maybe you page needs to know how to handle attributes and you can create pages by adding attributes from a pick list, each attribute knowing how to render itself and how to respond to searches? There are lots of ways to address this is we think of the world in terms of intercommunicating objects not static data structures with a few overeaching functions controlling it all. > I'm sure Python is quite possibly the least worst at this, but that > doesn't make it good at it. Which brings me back to my original question, what environment do you think is good at it? Are you aware of such an environment or merely wishing that such a thing could be invented? > Again, I'm sure it is better than C, but I definitely have had the > feeling that this is not something the language was designed for, > and that not many other people seem to be trying it. The language is a general purpose language like C and as such is designed to cover that type of operation, however the reason it's not generally used for long running server processes is the same reason that most of these are written in C: speed and resource usage. Server processes are usually required to run in the background, getting in the way as little as possible (think print spoolers), or to serve large numbers of clients requesting services(think databases) In the first case they need to consume as few resources as possible and in the latter they need to be low resource and very high speed. Because python is interpreted it tends to be a lot slower than C and because it needs the interpreter to be loaded in memory its also quite hih resource usage, so in that sense its not ideal for server or daemoin usage, but the language itself is as good as any other if speed/resources are not the key issues. Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
Ed Singleton wrote: >>>This immediately seemed to me to be a case for classes. >>>You provide a way for a user to create a new class by >>>subclassing the page class (from their point of view >>>probably through adding a few new fields to >>>a form). > > The behaviours of all the > classes would be the same (and would be fairly simple, a method for > rendering the page with a template, a method to update the data based > on a form submission). Users would basically be able to change the > data structure (add and remove attributes). It seems to me that rather than having a new class for each type of data, you need a class whose attributes are dynamic. I imagine you could have a list of page types, each of which has a list of attributes. A new page type is created not by defining a new class but by making a new entry in the types table. The data for a page might be as simple as just a dict, or maybe it makes sense to wrap it in a class, but the class would be the same for all pages. A page would be rendered by combining a template with the dict containing its data. Form submissions would be handled by referring to the list of attributes for the type and extracting the corresponding data. You might have a class to wrap the page data and another one to wrap the types table. These would provide operations on the contained data and be common to all types. For persistence you could take a couple of approaches. You could persist the lists and dicts directly using pickle or shelve or an object database. You could persist them in a relational database by using a generic table with one row for each data item - the columns would simply be object id, column name and data. SQLite might work very well for this as it allows multiple data types within a single column. The shift is from looking at the problem as one of dynamic classes, to one of dynamic data. Python is excellent for working with dynamic data! BTW have you looked at Django? I don't think it is quite as dynamic as you want on the model side but it has a lot of support for quickly generating a presentation of a changing model. http://www.djangoproject.com/ Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
On 25/11/05, Alan Gauld <[EMAIL PROTECTED]> wrote: > > Just had a quick look at Smalltalk, and at first glance the overview > > of the ideas behind it seems amazing, but the language seems quite > > ugly, and it seems to be very IDE led. > > Adherents will defend its simplicity but I confess I strugglred for > a long time with SmallTalk before learning to love it :-) And yes > it is IDE led although the IDE can be 'removed' when deploying > applications. But much depends on the implementation, my favourite > for PCs is Dolphin SmallTalk from ObjectArts. Its not a traditional > SmallTalk (ala Xerox SmallTalk 80) but very much one for the '90's I'm definitely going to investigate it further, as it does seem interesting, but I remember the first time I looked into Python, I read the tutorial and was quite bemused as it didn't seem like I'd learned anything. It was all so obvious that I didn't feel there was anything to learn. As it was I was able to very quickly plunge much deeper and end up asking lots of difficult questions that in any other language you wouldn't approach for many years. > > This immediately seemed to me to be a case for classes. > > You provide a way for a user to create a new class by > > subclassing the page class (from their point of view > > probably through adding a few new fields to > > a form). > > That might be part of the problem, if you think of a class in terms > of its data attributes then that is nearly always the wrong starting > point. Classes express behaviour, the data is only there to support > the behaviour. Thats why methods are polymorphic but not attributes. If classes express behaviour, then what expresses the structure of the data? (ie what attributes there are going to be and what values they are likely to accept). You need (the option of) a data definition in order to generalise. Using my web server example from earlier, you need to be able to say that for any type of page, whatever it's attributes, you can create a web form to search for that type of page by iterating through it's attributes and creating a relevant form field for each type. > So you think of a class having an interface and users extending > or modifying the behaviour, not the data. If you follow that route > you might find you don't need to write self modifying code, > you simply load new classes with a common interface into an > existing hook structure. I definitely had the opposite in mind. The behaviours of all the classes would be the same (and would be fairly simple, a method for rendering the page with a template, a method to update the data based on a form submission). Users would basically be able to change the data structure (add and remove attributes). > > However it doesn't really seem that Python is suited to this. Indeed > > it doesn't really seem that Python is suited to a persistent > > environment. > > What makes you think that? Most persistent environments > (ie long running server processs) are written in C/C++ (or > maybe Java nowadays) which are far less dynamic than Python. I'm sure Python is quite possibly the least worst at this, but that doesn't make it good at it. > > Having a program running for a long time (months) is > > possible certainly, but it seems to be fighting against the language > > Not at all, at least no more than in languages like C which require > recoding, recompiling, stopping and then restarting the server to > make changes. Thats why a simple framework with loadable > modules it usally preferred to self modifying code! Again, I'm sure it is better than C, but I definitely have had the feeling that this is not something the language was designed for, and that not many other people seem to be trying it. Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
Ed Singleton wrote: > Can a python program change a class, change all the objects already > created by that class and save the modified class definition, so that > if the program were restarted it would return to exactly the same > state? (assuming all objects were saved to a database or somesuch). I asked on comp.lang.python about persisting classes. David Wahler posted an interesting solution. You can read the thread here: http://groups.google.com/group/comp.lang.python/browse_thread/thread/10a03f094303a91c/7d65bd344d4e0063?hl=en#7d65bd344d4e0063 (David's reply hasn't appeared on Google as I post this but it should be there eventually...) Kent -- http://www.kentsjohnson.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
Hi Ed, This is a longish response because you are raising some very interesting (and deep) issues from a computer science point of view. > Well, self-modifying isn't inherently necessary. What I guess I> really need is persistent classes as well as persistent objects. Python classes can be persistent just like any other object, theres nothing intrinsically different between a class object and any other kind of object (except that classes as objects are kind of mind bending as a concept!) > I always tend to think of classes as templates for objects rather > than factories. They are both. > In my mind, object methods are just calls to the class which > are evaluated every time they are called. The methjods are function attributes of a class which are selected by the class dispatch mechanism in response to messages sent to instances. In Python that means the getattr() methods look up the right method in response to receipt of a message. > Objects should be strict instances of classes that can't be > modified except for the values of their attributes. Attributes in a pure OOP language include both the contained data and functions. In other workds methods can be added/removed and modified as well as the data. In fact many would argue that the internal data not only cannot be modified externally but should not even be seen - the Data Hiding school of thought.> Class Page(object):> def print(self):> printPage(self) What does that gain you? > And have all my methods call functions (passing on parameters as> necessary). That way if I change a function, it will be changed for> every instance of every object of that class. You mean like this: >>> class C:... def m(self): print 'm'...>>> def f(o): print 'other m'...>>> c = C()>>> c.m()m>>> C.m = f>>> c.m()other m>>> > And couldn't I write a function that would add functions or attributes> to classes and objects? Of course, just like I did there. > Am I trying to use the wrong language for this? I love Python but I> seem to keep coming up against lots of practical issues with it Python is a pretty good language for this kind of thing, its just that this kind of thing is intrinsically difficult. The only lanuages I know of which are better than Python at it are Lisp and Smalltalk (and even Smalltalk has some limitations compared to Python). Do you have experience of a language which is more flexible in this regard? I'm curious because you seem to assume that its almost normal to be able to do these things, whereas in my experience its usually impossible rather than just difficult... > define the behaviours I want without having to bother with how the> computer is actually going to handle them. Believe it or not thats been the holy grail of language designers since the days of COBOL - The Common Business Oriented Language - which allowed business people to write theitr own programmes in "standard, everyday English", like: PROGRAM ID - HELLO PROCEDURE DIVISION MAIN-LINE-MODULE MOVE 0 TO COUNTER OPEN INPUT IN-FILE PERFORM READ IN-FILE UNTIL END-OF-FILE = "YES" ADD 1 TO COUNTER DISPLAY "LINE NUMBER ", COUNTER CLOSE IN-FILE STOP RUN. At the time it was considered revolutionary and programmers trembled at the prospect of instant redundancy. As it happened your average businessman found COBOL about as readable as pigin English! But that hasn't stopped us trying... One thing we have discovered is that the more natural the language seems to get the less flexible the language gets. Python is the closest I've seen yet to a general purpose language that is truly readable, but thats still a long way from being plain English! But I digress :-) Basically I just wonder what your background is that you seem to expect these kind of dynamic facilities? I'd certainly be interested in investigating it further. Alan GAuthor of the learn to program web tutorhttp://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
> Just had a quick look at Smalltalk, and at first glance the overview > of the ideas behind it seems amazing, but the language seems quite > ugly, and it seems to be very IDE led. Adherents will defend its simplicity but I confess I strugglred for a long time with SmallTalk before learning to love it :-) And yes it is IDE led although the IDE can be 'removed' when deploying applications. But much depends on the implementation, my favourite for PCs is Dolphin SmallTalk from ObjectArts. Its not a traditional SmallTalk (ala Xerox SmallTalk 80) but very much one for the '90's > This immediately seemed to me to be a case for classes. > You provide a way for a user to create a new class by > subclassing the page class (from their point of view > probably through adding a few new fields to > a form). That might be part of the problem, if you think of a class in terms of its data attributes then that is nearly always the wrong starting point. Classes express behaviour, the data is only there to support the behaviour. Thats why methods are polymorphic but not attributes. So you think of a class having an interface and users extending or modifying the behaviour, not the data. If you follow that route you might find you don't need to write self modifying code, you simply load new classes with a common interface into an existing hook structure. > However it doesn't really seem that Python is suited to this. Indeed > it doesn't really seem that Python is suited to a persistent > environment. What makes you think that? Most persistent environments (ie long running server processs) are written in C/C++ (or maybe Java nowadays) which are far less dynamic than Python. > Having a program running for a long time (months) is > possible certainly, but it seems to be fighting against the language Not at all, at least no more than in languages like C which require recoding, recompiling, stopping and then restarting the server to make changes. Thats why a simple framework with loadable modules it usally preferred to self modifying code! Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
Ed Singleton wrote: > Weirdly, I'd just realised that SQLObject does most of what I'm > thinking about. Ahhh. (sigh of relief :-) Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
On 25/11/05, Ismael Garrido <[EMAIL PROTECTED]> wrote: > Ed Singleton wrote: > > >I want to create a small simple CMS for my website. Users will be > >able to add and edit basic pages. Pages can have sub-pages (no need > >for folders cause a folder and an index.html can be unified into one > >concept). > > > >Users will also be able to create new types of pages, maybe a > >PressReleasePage for example. PressReleases would be based on a > >normal page but might have extra attributes such as Author, Abstract > >or DateToBeReleased. > > > > > > > Store the fields in a database, make a factory class that gets those > fields and generates a page accordingly. > > I agree with Kent, what you're trying seems way too abstract. Weirdly, I'd just realised that SQLObject does most of what I'm thinking about. It allows you to update a class and that will affect all the objects already instantiated. It has class persistence in that it will rebuild the class on startup from the columns in your table so your 'live' changes will be there after you restart the server. Though all that is only for the data in the class. Each method could just contain a call to a function (as I mentioned above) all the functions could be contained in a module, which would make it easy to update them as it appears to be easy enough to reload a module. The only thing you wouldn't really be able to do is add or remove methods, which I guess is fine for the moment. The only thing I'm not sure of is whether SQLObject can store properties (as in managed attributes) which I need to do for the 'inherit from parent' attribute that I mentioned is a previous thread: class Page(object): def __init__(self, parent): self.__header = None self.parent = parent def getheader(self): if not self._header and self.parent: return self.parent.header else: return self.__header def setheader(self, header): self.__header = header header = property(getheader, setheader) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
Ed Singleton wrote: >I want to create a small simple CMS for my website. Users will be >able to add and edit basic pages. Pages can have sub-pages (no need >for folders cause a folder and an index.html can be unified into one >concept). > >Users will also be able to create new types of pages, maybe a >PressReleasePage for example. PressReleases would be based on a >normal page but might have extra attributes such as Author, Abstract >or DateToBeReleased. > > > Store the fields in a database, make a factory class that gets those fields and generates a page accordingly. I agree with Kent, what you're trying seems way too abstract. Ismael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
Ed Singleton wrote: > On 24/11/05, Kent Johnson <[EMAIL PROTECTED]> wrote: >> It sounds like maybe you come from a background in Smalltalk, or >> maybe you should look at Smalltalk. In Smalltalk the whole >> environment is dynamic and can be saved and restored easily.> > > Just had a quick look at Smalltalk, and at first glance the overview > of the ideas behind it seems amazing, but the language seems quite > ugly, and it seems to be very IDE led. I guess the language grows on you, it certainly has some strong supporters. It is very oriented to a single comprehensive environment. I think this is one reason it never became generally popular. But it also is what gives the language its extreme malleability. It does do what you want - it allows you to change objects and classes dynamically at run-time and save the state of the system at any time. The down side is that all interaction with the system is from within. >>For Python, I think you will do better if you narrow your requirements. >>Python is very dynamic - classes can be changed at runtime, or reloaded if >>you are careful - and there are several good ways to persist state. If you >>can be more specific about what you really need there may be a solution for >>you. > > > What I want to do seems quite simple to me in concept, but is seeming > more and more as if it would be hard to implement. > > I want to create a small simple CMS for my website. Users will be > able to add and edit basic pages. Pages can have sub-pages (no need > for folders cause a folder and an index.html can be unified into one > concept). Have you looked at Plone and PyLucid? http://plone.org/ http://www.pylucid.org/ > Indeed > it doesn't really seem that Python is suited to a persistent > environment. Having a program running for a long time (months) is > possible certainly, but it seems to be fighting against the language > rather than working with it. Python provides mechanisms for persistence but not a way to persist the entire environment. You have to design and build your program to support persistence. Long-running Python programs are certainly possible. PS Please reply on list. Kent -- http://www.kentsjohnson.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
Ed Singleton wrote: > I think I can actually achieve this to some degree by doing: > > Class Page(object): > def print(self): > printPage(self) > > And have all my methods call functions (passing on parameters as > necessary). That way if I change a function, it will be changed for > every instance of every object of that class. Yes, that will work. You can change the definition of a class method at runtime. > > And couldn't I write a function that would add functions or attributes > to classes and objects? > > def addAttribute(class, attribute, starting value): > # add it to the class > # iterate through all objects already created by the class > # add attribute to object Yes, you can add attributes to classes and objects. Did you look at this recipe? http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/160164 The MetaInstanceTracker metaclass gives an easy way for classes to keep track of their instances. The MetaAutoReloader metaclass uses MetaInstanceTracker to update class definitions when a module is reloaded. > > Am I trying to use the wrong language for this? I love Python but I > seem to keep coming up against lots of practical issues with it and I > really don't want to bother with practical issues. I just want to > define the behaviours I want without having to bother with how the > computer is actually going to handle them. That seems rather idealistic! > > I guess it's very much a "I don't care how it works!" attitude, which > is probably a corollary to "premature optimisation is the root of all > evil". Ignore all issues of memory and speed and create something > highly abstract that allows you to define your solution. Then work > down from there and start worrying about speed and memory and > practical issues later (or hopefully never). I still think you are shooting for too high a level of abstraction. Kent -- http://www.kentsjohnson.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
On 24/11/05, Alan Gauld <[EMAIL PROTECTED]> wrote: > There are many ways of doing this, few of them very nice IMHO. > > > without having to restart the program? Is it feasible to get a > > program to change it's own source code while it is running? > > Yes, you can overwrite an existing module then call reload > from within the program. > > > For example, if you have a web server such as CherryPy that will > > (hopefully) be running for months at a time and you want to be able to > > change classes without having to restart the server. > > Yes that would be feasible., preferrably in response to an admin > page where you could fill in a list of modules to be reloaded... > But you neeed to be very careful not to break any of the interfaces, > the Liskov Substitution Principle must be strictly observed. > > > to allow users of the site to edit a class through the web and > > see the changes to the site immediately? > > Extremely dangerous but the same principle would apply, > simply load the existing module into an editor pane then save > the modified version and reload it. > > > Can a python program change a class, > > Yes as above. > > > change all the objects already created by that class > > Trickier and potentially involves some low level poking that > should not be encouraged IMHO! :-) > > and save the modified class definition, so that > > if the program were restarted it would return to exactly the same > > state? (assuming all objects were saved to a database or somesuch). > > If the LSP is adhered to its feasible but I don't intend to try any > such thing! It would be full of pitfalls and an almosyt certain recipe > for reliability problems that would be impossible to find. > Self modifying code sounds like a good idea but there is a very > good reason why its almost never used in production software! Well, self-modifying isn't inherently necessary. What I guess I really need is persistent classes as well as persistent objects. I always tend to think of classes as templates for objects rather than factories. In my mind, object methods are just calls to the class which are evaluated every time they are called. Objects should be strict instances of classes that can't be modified except for the values of their attributes. I think I can actually achieve this to some degree by doing: Class Page(object): def print(self): printPage(self) And have all my methods call functions (passing on parameters as necessary). That way if I change a function, it will be changed for every instance of every object of that class. And couldn't I write a function that would add functions or attributes to classes and objects? def addAttribute(class, attribute, starting value): # add it to the class # iterate through all objects already created by the class # add attribute to object Am I trying to use the wrong language for this? I love Python but I seem to keep coming up against lots of practical issues with it and I really don't want to bother with practical issues. I just want to define the behaviours I want without having to bother with how the computer is actually going to handle them. I guess it's very much a "I don't care how it works!" attitude, which is probably a corollary to "premature optimisation is the root of all evil". Ignore all issues of memory and speed and create something highly abstract that allows you to define your solution. Then work down from there and start worrying about speed and memory and practical issues later (or hopefully never). Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
On 24/11/05, Kent Johnson <[EMAIL PROTECTED]> wrote: > Ed Singleton wrote: > > Is it feasible to change a program's source code whilst it is running > > without having to restart the program? Is it feasible to get a > > program to change it's own source code while it is running? > > You can change a class while it is running. > > > > For example, if you have a web server such as CherryPy that will > > (hopefully) be running for months at a time and you want to be able to > > change classes without having to restart the server. Or if you want > > to allow users of the site to edit a class through the web and see the > > changes to the site immediately? > > The auto-restart feature of CherryPy might do this for you. Also if the > changes to the site are to a template such as Cheetah, those usually > autoreload. > > > > Can a python program change a class, change all the objects already > > created by that class and save the modified class definition, so that > > if the program were restarted it would return to exactly the same > > state? (assuming all objects were saved to a database or somesuch). > > You can have persistent objects using for example SQLObject or ZODB, > > > > Does anyone have any good links to implementations of this? I assume > > someone's already done it before. > > It sounds like maybe you come from a background in Smalltalk, or maybe you > should look at Smalltalk. In Smalltalk the whole environment is dynamic and > can be saved and restored easily. Just had a quick look at Smalltalk, and at first glance the overview of the ideas behind it seems amazing, but the language seems quite ugly, and it seems to be very IDE led. > For Python, I think you will do better if you narrow your requirements. > Python is very dynamic - classes can be changed at runtime, or reloaded if > you are careful - and there are several good ways to persist state. If you > can be more specific about what you really need there may be a solution for > you. What I want to do seems quite simple to me in concept, but is seeming more and more as if it would be hard to implement. I want to create a small simple CMS for my website. Users will be able to add and edit basic pages. Pages can have sub-pages (no need for folders cause a folder and an index.html can be unified into one concept). Users will also be able to create new types of pages, maybe a PressReleasePage for example. PressReleases would be based on a normal page but might have extra attributes such as Author, Abstract or DateToBeReleased. This immediately seemed to me to be a case for classes. You provide a way for a user to create a new class by subclassing the page class (from their point of view probably through adding a few new fields to a form). Later if they change their mind they can edit the class by adding or removing attributes. However it doesn't really seem that Python is suited to this. Indeed it doesn't really seem that Python is suited to a persistent environment. Having a program running for a long time (months) is possible certainly, but it seems to be fighting against the language rather than working with it. Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
> For example if your program is running, and you make a change to a > class, is it possible to create the necessary class definition and > save it to a file? You can do that but even if you do follow the LSP closely, you still can have problems, especially when you subtract functions or data rather than add them. Once they are gone you can't easily put them back if things go wrong! There is nearly always a better solution... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
There are many ways of doing this, few of them very nice IMHO. > without having to restart the program? Is it feasible to get a > program to change it's own source code while it is running? Yes, you can overwrite an existing module then call reload from within the program. > For example, if you have a web server such as CherryPy that will > (hopefully) be running for months at a time and you want to be able to > change classes without having to restart the server. Yes that would be feasible., preferrably in response to an admin page where you could fill in a list of modules to be reloaded... But you neeed to be very careful not to break any of the interfaces, the Liskov Substitution Principle must be strictly observed. > to allow users of the site to edit a class through the web and > see the changes to the site immediately? Extremely dangerous but the same principle would apply, simply load the existing module into an editor pane then save the modified version and reload it. > Can a python program change a class, Yes as above. > change all the objects already created by that class Trickier and potentially involves some low level poking that should not be encouraged IMHO! :-) > and save the modified class definition, so that > if the program were restarted it would return to exactly the same > state? (assuming all objects were saved to a database or somesuch). If the LSP is adhered to its feasible but I don't intend to try any such thing! It would be full of pitfalls and an almosyt certain recipe for reliability problems that would be impossible to find. Self modifying code sounds like a good idea but there is a very good reason why its almost never used in production software! HTH, Alan G Author of the learn to program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
Ed Singleton wrote: > Is it feasible to change a program's source code whilst it is running > without having to restart the program? Is it feasible to get a > program to change it's own source code while it is running? You can change a class while it is running. > > For example, if you have a web server such as CherryPy that will > (hopefully) be running for months at a time and you want to be able to > change classes without having to restart the server. Or if you want > to allow users of the site to edit a class through the web and see the > changes to the site immediately? The auto-restart feature of CherryPy might do this for you. Also if the changes to the site are to a template such as Cheetah, those usually autoreload. > > Can a python program change a class, change all the objects already > created by that class and save the modified class definition, so that > if the program were restarted it would return to exactly the same > state? (assuming all objects were saved to a database or somesuch). You can have persistent objects using for example SQLObject or ZODB, > > Does anyone have any good links to implementations of this? I assume > someone's already done it before. It sounds like maybe you come from a background in Smalltalk, or maybe you should look at Smalltalk. In Smalltalk the whole environment is dynamic and can be saved and restored easily. For Python, I think you will do better if you narrow your requirements. Python is very dynamic - classes can be changed at runtime, or reloaded if you are careful - and there are several good ways to persist state. If you can be more specific about what you really need there may be a solution for you. Kent -- http://www.kentsjohnson.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
Ed Singleton wrote: > Hmmm, that's discouraging. Do you know if it's feasible to just keep > changes to code in memory synchronised with changes nto the source > code? So rather than reload source code, make sure that the source > code reflects what is running in memory? > > For example if your program is running, and you make a change to a > class, is it possible to create the necessary class definition and > save it to a file? I think that would be hard too. How would you be making the changes to the class in the running program? I guess you could have a metaclass that looks for changes to a class, decompiles it and writes the result to a file. Of course there would be no comments in the result, and I don't think there is a good free Python decompiler available... Kent -- http://www.kentsjohnson.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
On 24/11/05, Kent Johnson <[EMAIL PROTECTED]> wrote: > Ed Singleton wrote: > > Is it feasible to change a program's source code whilst it is running > > without having to restart the program? Is it feasible to get a > > program to change it's own source code while it is running? > > > > For example, if you have a web server such as CherryPy that will > > (hopefully) be running for months at a time and you want to be able to > > change classes without having to restart the server. Or if you want > > to allow users of the site to edit a class through the web and see the > > changes to the site immediately? > > This is hard. IIRC CherryPy has a way to automatically restart the server > when a module changes, which is not what you ask for but at least it is > automatic. A couple of recent threads on comp.lang.python have talked about > this: > > http://groups.google.com/group/comp.lang.python/browse_frm/thread/8bb4efbe726c4ab5/848860f76210be69 > http://groups.google.com/group/comp.lang.python/browse_frm/thread/7b34c30c5833a9b0/4ed71bb7c5a97b57 > > Kent Hmmm, that's discouraging. Do you know if it's feasible to just keep changes to code in memory synchronised with changes nto the source code? So rather than reload source code, make sure that the source code reflects what is running in memory? For example if your program is running, and you make a change to a class, is it possible to create the necessary class definition and save it to a file? Ed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Modifying Source Code while Program is Running
Ed Singleton wrote: > Is it feasible to change a program's source code whilst it is running > without having to restart the program? Is it feasible to get a > program to change it's own source code while it is running? > > For example, if you have a web server such as CherryPy that will > (hopefully) be running for months at a time and you want to be able to > change classes without having to restart the server. Or if you want > to allow users of the site to edit a class through the web and see the > changes to the site immediately? This is hard. IIRC CherryPy has a way to automatically restart the server when a module changes, which is not what you ask for but at least it is automatic. A couple of recent threads on comp.lang.python have talked about this: http://groups.google.com/group/comp.lang.python/browse_frm/thread/8bb4efbe726c4ab5/848860f76210be69 http://groups.google.com/group/comp.lang.python/browse_frm/thread/7b34c30c5833a9b0/4ed71bb7c5a97b57 Kent -- http://www.kentsjohnson.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor