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
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
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
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 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
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] lil help please - updated (fwd)
| I think I would split this into three phases: | - collect the data into groups of HFR | - process each group by rearranging, renumbering, reporting errors | - output the processed groups | | One potential problem is to resynchronize to the next group when | there is a sequence error. If there is always a blank line between | groups it is easy. Otherwise maybe just assume an H is the start of a | group. | Hmm...so Alan could first split the data on the |H values. These *should* contain an |F and and |R, so the next step would be to break these HFR groups into pieces and check to see that all the pieces are there, and perhaps if not, printing those to an error file for review. Alan, regarding the extraction of the parentheticals, what have you tried? One suggestion for this aspect is to get rid of the line breaks in the |H chunk and then you won't have the problem of a broken parenthetical. For example, ## multiLines = '''This (as you ... can see) is multilined.''' multiLines.splitlines() ['This (as you', 'can see) is multilined.'] ' '.join(multiLines.splitlines()) 'This (as you can see) is multilined.' # the above is one line and much easier to handle now. ## How are you reading the data in from the file? /c ___ 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] lil help please - updated (fwd) (Chris or Leslie Smith)
Smile and Kent The logic is so good so far. However, How do we move the (...) in |H to end of |R and before next |H Much respect AD Exceptional team: I like and I agree with your all logic (I have no choice! Smile you are more advanced than me) Kent said: I think I would split this into three phases: - collect the data into groups of HFR - process each group by rearranging, renumbering, reporting errors - output the processed groups One potential problem is to resynchronize to the next group when there is a sequence error. If there is always a blank line between groups it is easy. Otherwise maybe just assume an H is the start of a group. And Smile addressed Kent's concern by saying: Hmm...so Alan could first split the data on the |H values. These *should* contain an |F and and |R, so the next step would be to break these HFR groups into pieces and check to see that all the pieces are there, and perhaps if not, printing those to an error file for review. Alan, regarding the extraction of the parentheticals, what have you tried? One suggestion for this aspect is to get rid of the line breaks in the |H chunk and then you won't have the problem of a broken parenthetical. For example, ## multiLines = '''This (as you ... can see) is multilined.''' multiLines.splitlines() ['This (as you', 'can see) is multilined.'] ' '.join(multiLines.splitlines()) 'This (as you can see) is multilined.' # the above is one line and much easier to handle now. ## How are you reading the data in from the file? I use the 150 line python I do not mind emailing it directly so I do not confuse these cleaning tasks - you just say yes Much respect AD --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.778 / Virus Database: 525 - Release Date: 10/15/2004 ___ 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] lil help please - updated (fwd) (Chris or Leslie Smith)
Thanks Getting Simpler I tested the below on python command line, it is very cool 1. So, how do we translate its re.compile into python? 2. them how to move it to at the end of |R and before next |H 3. I can almost remove all numbers (delete 0-9) and start new numbering ## multiLines = '''This (as you ... can see) is multilined.''' multiLines.splitlines() ['This (as you', 'can see) is multilined.'] ' '.join(multiLines.splitlines()) 'This (as you can see) is multilined.' # the above is one line and much easier to handle now. ## --- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.778 / Virus Database: 525 - Release Date: 10/15/2004 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] TRULY NEWBIE - MENTOR NEEDED (fwd)
-- Forwarded message -- Date: Fri, 25 Nov 2005 08:59:30 -0400 From: mike donato [EMAIL PROTECTED] To: [EMAIL PROTECTED] Subject: Re: [Tutor] TRULY NEWBIE - MENTOR NEEDED Dan, what I meant was that the program does not run. Why? Thanks From: Danny Yoo [EMAIL PROTECTED] To: mike donato [EMAIL PROTECTED] CC: tutor@python.org Subject: Re: [Tutor] TRULY NEWBIE - MENTOR NEEDED Date: Tue, 22 Nov 2005 14:46:14 -0800 (PST) On Tue, 22 Nov 2005, mike donato wrote: Greetings, I am new student to programming and am experimenting with PYTHON. From what I have read, seems to be a very versatile language. In the following excercise I am getting an error class String(str, Object): [class definition cut] Traceback (most recent call last): File pyshell#0, line 11, in -toplevel- class String(str, Object): NameError: name 'Object' is not defined Hi Mike, The error is true: Python doesn't know of any class named 'Object'. What you may be looking for is the 'object' class (lowercase 'o'). Good luck! _ FREE pop-up blocking with the new MSN Toolbar - get it now! http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/ # # This file contains the Python code from Program 4.5 of # Data Structures and Algorithms # with Object-Oriented Design Patterns in Python # by Bruno R. Preiss. # # Copyright (c) 2003 by Bruno R. Preiss, P.Eng. All rights reserved. # # http://www.brpreiss.com/books/opus7/programs/pgm04_05.txt # class Array(object): def __len__(self): return len(self._data) def setLength(self, value): if len(self._data) != value: newData = [ None for i in xrange(value) ] m = min(len(self._data), value) for i in xrange(m): newData[i] = self._data[i] self._data = newData length = property( fget = lambda self: self.__len__(), fset = lambda self, value: self.setLength(value)) # ... ___ 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] TRULY NEWBIE - MENTOR NEEDED (fwd)
Dan, what I meant was that the program does not run. Why? Hi Mike, We don't have enough information. I have to ask more questions to have a better idea what's going on. Also, please send your replies to the tutor list: don't send them to me directly. What do expect to see when your program runs? Are there other programs that you've run that do things? What do you mean when the program doesn't run? What are you doing to run the program? The class definition that you're showing us looks bizarre as a first program example. If you're really starting from scratch, I'd recommend starting with an easier tutorial. If you're starting from scratch, you may want to go through one of the tutorials in: http://wiki.python.org/moin/BeginnersGuide/NonProgrammers I have no familiarity with the book you quote, but from the web site: http://www.brpreiss.com/books/opus7/ and from the statement in the foward here: http://www.brpreiss.com/books/opus7/ the author explicitely says that the book's audience is experienced programmers! It won't go out of it's way to talk about issues that affect beginner programmers, because that book expects you to already know basic Python programming. That is why I'm encouraging you to look at another tutorial from the Non Programmers section. Good luck to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to read a non english text file
hello everyone still my question about if i could read and process a non english language text file using python Yahoo! Music Unlimited - Access over 1 million songs. Try it free.___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to read a non english text file
enas khalil wrote: hello everyone still my question about if i could read and process a non english language text file using python To read the file use import codecs data = codecs.open('myfile.txt', 'r', 'cp1256').read() This will give you a Unicode string in data. I don't know how to use NLTK to tag this, I hope this will get you started. Kent -- http://www.kentsjohnson.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to read a non english text file
enas khalil wrote: hello everyone still my question about if i could read and process a non english language text file using python Hi Enas, I think you mean Arabic language, yes of course you can, and I think you should see the Duali project: http://www.arabeyes.org/project.php?proj=Duali Duali is a simple Arabic spell checker written in Python, Duali will be a good start if you are interested in Arabic language, It will be hard if you are a newbie but try and see the code you will learn a lot. you can see a screenshot for Duali running at : http://art.arabeyes.org/duali/duali_0_1_1 What you need to learn is Unicode with Python, Duali is a very good example. Good Luck Basem Narmok ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] lil help please - updated (fwd)
| The logic is so good so far. However, How do we move the (...) in |H | to end of |R and before next |H Maybe you are thinking too literally about the moving of the parenthetical item from the |H to the end of the |R. Let's say you have the 3 chunks of information in variables h, f, and r: ### h='This (you see) is the header.' f='The Book' r='cool' ### If you could break h apart into 3 pieces, the This , the (you see), and is the header. and call them h1, h2, and h3 then you could add h1 and h3 back together (that's your new value of h) and add h2 to r as your new r: ### h1='This ' h2='(you see)' h3=' is the header' h=h1+h3 r=r+'\n'+h2 #the \n puts the parenthetical on a new line print h This is the header print r cool (you see) ### (There's a double space left over at the place where the h1 and h3 were joined which is something you might want to fix before you add them back together. The string method .strip() is nice for getting rid of leading and trailing space on a string. Here's an example: ### print s space before and after is gone print s.strip() space before and after is gone ### ) What would help maintain the spirit of the tutor list is if you would ask specific questions about problems that you are having getting the script to do what you you want or clarifications about how something is suppose to work. Right now you have a task that is defined but you are asking general questions about designing the program, not specific questions about the *python* related problems. Rather than sending or posting the script you have, why not just post the specific problems you are running into? Thanks, /c ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor