Re: [Tutor] Converting a Py2exe Script to a Windows Installer
Quite a while ago I had success in using the Inno Installer. Relatively painless and free. Can't remember all the details but you can start at http://www.jrsoftware.org/isinfo.php All the best, Fred Milgrom On Sun, Jun 28, 2009 at 5:40 AM, Paras K. wrote: > I have been writing many scripts using python and then making them a > standalone script using Py2exe. I have the source code for the script. > > What I am trying to do is, take this script and make it into a windows > installer and have a shortcut within the All Programs or on the desktop. > > Any tutorials or sites out there that will show me how to do this? > > Let me know. > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > -- All the best, Fred ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] freeze
col speed wrote: HI Guys, I have a small programme, called shop.py, that I wish to make into a "frozen binary" ( I think that's right - I'm using Linux Ubuntu 9.04 and I want the programme to work on a windows computer that doesn't have Python installed). I used freeze.py from examples/Tools and everything seemed to be going well (I won't post the output as there is so much), but the last lines are: o M_xml__sax__xmlreader.o M_xmllib.o M_xmlrpclib.o /usr/lib/python2.6/config/libpython2.6.a -L/usr/lib -lz -lpthread -ldl -lutil -lm -o shop /usr/bin/ld: cannot find -lz collect2: ld returned 1 exit status make: *** [shop] Error 1 Any ideas of what is going wrong? The linker cannot link against the 'z' library :) At my Linux system, that library is at "/usr/lib/libz.a", which comes from the 'zlib-devel' RPM. No doubt Ubuntu has a similar name for the z library. I don't know what you are doing exactly, but it seems to me that a program linked against Linux libraries is not going to work at a non-linux system (much like you cannot run Windows binaries natively at a Linux system). You may need to do the freezing at a Windows system or use a cross-compiler. I would also like to ask your opinion - the programme is very small (only 1.2kb!). Is there another way ? Am I totally wasting my time? Install Python at the Windoes machine, and run shop.py in natively would seem like an alternative. Even with freeze, you are basically installing Python, except it has a different name. Albert ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] intefaces in python
On Sun, Jun 28, 2009 at 5:00 PM, Amit Sethi wrote: > Hi , I don't suppose python has a concept of interfaces. But can somebody > tell me if their is a way i can implement something like a java interface > in python. Sure. Interfaces are just Java's compensation for not having multiple inheritance. Python does have multiple inheritance, so that's what one would use. Although one could also use duck typing, and then use 'nothing' as an implementation... More specific: Java Interface: public interface MyInterface { string doSomething(string line); string doSomethingElse(string line); } Java Implementation: public class MyImplementation { string doSomething(string line) { return "I did something with" + line; } string doSomethingElse(string line) { return "I did something else." } } == Python Interface: class MyInterface(object): doSomething(line): raise NotImplementedError doSomethingElse(line): raise NotImplementedError Python Implementation: class MyImplementation(MyInterface): doSomething(line): return "I did something with "+line doSomethingElse(line): return "I did something else." == Python interface using duck typing: # Hey guys, when you call something a 'MyInterface', it needs methods doSomething and doSomethingElse Python Implementation using duck typing: class MyImplementation(object): # These things implement MyInterface doSomething(line): return "I did something with "+line doSomethingElse(line): return "I did something else." -- André Engels, andreeng...@gmail.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] intefaces in python
class MyInterface(object): doSomething(line): raise NotImplementedError doSomethingElse(line): raise NotImplementedError I think that is exactly the kind of structure i was looking for ... On Mon, Jun 29, 2009 at 12:58 PM, Andre Engels wrote: > On Sun, Jun 28, 2009 at 5:00 PM, Amit Sethi > wrote: > > Hi , I don't suppose python has a concept of interfaces. But can somebody > > tell me if their is a way i can implement something like a java > interface > > in python. > > Sure. Interfaces are just Java's compensation for not having multiple > inheritance. Python does have multiple inheritance, so that's what one > would use. Although one could also use duck typing, and then use > 'nothing' as an implementation... > > More specific: > > > Java Interface: > public interface MyInterface { >string doSomething(string line); >string doSomethingElse(string line); > } > > Java Implementation: > public class MyImplementation { > string doSomething(string line) { > return "I did something with" + line; > } > string doSomethingElse(string line) { > return "I did something else." > } > } > > == > Python Interface: > > class MyInterface(object): >doSomething(line): >raise NotImplementedError >doSomethingElse(line): >raise NotImplementedError > > Python Implementation: > class MyImplementation(MyInterface): >doSomething(line): >return "I did something with "+line >doSomethingElse(line): >return "I did something else." > > == > Python interface using duck typing: > > # Hey guys, when you call something a 'MyInterface', it needs methods > doSomething and doSomethingElse > > Python Implementation using duck typing: > > class MyImplementation(object): ># These things implement MyInterface >doSomething(line): >return "I did something with "+line >doSomethingElse(line): >return "I did something else." > > > -- > André Engels, andreeng...@gmail.com > -- A-M-I-T S|S ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] intefaces in python
"Amit Sethi" wrote class MyInterface(object): doSomething(line): raise NotImplementedError doSomethingElse(line): raise NotImplementedError I think that is exactly the kind of structure i was looking for ... As a matter of interest, why? What do you anticipate using this for? I have found a few cases where abstract interfaces are useful but they are very few and far between. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] intefaces in python
Well I want to implement plug-in like mechanism for an application . I want to define some minimum functions that any body writing a plugin has to implement. For that i thought an interface would be best because in a scenario where the function is not implemented some kind of error would occur. I would love to hear if you think their is a better way to achieve this On Mon, Jun 29, 2009 at 2:49 PM, Alan Gauld wrote: > "Amit Sethi" wrote > >> class MyInterface(object): >> doSomething(line): >> raise NotImplementedError >> doSomethingElse(line): >> raise NotImplementedError >> >> I think that is exactly the kind of structure i was looking for ... >> > > As a matter of interest, why? What do you anticipate using this for? > I have found a few cases where abstract interfaces are useful but they are > very few and far between. > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- A-M-I-T S|S ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] character counter
2009/6/27 julie : > file = open("/Users/meitalamitai/Documents/Computer > Science/Python/Homework/Lorem_Ipsum.py") > lines = 0 > for line in file: > lines=lines+1 > print '%r has %r lines' % ("Lorem_Ipsum.py", lines) > if char >= 1000: > break You can do something like below (untested). The below also includes white space and punctiation. If you want to exclude those use strip(). - filename = "/Users/meitalamitai/Documents/Computer Science/Python/Homework/Lorem_Ipsum.py" file = open(filename, "r") # Explicitely open the file readonly lines = file.split() # Split the file into a list so we can use len() on it and iterate over the lines linecount = len(lines) # Get the number of lines totalcharcount = 0 # Set initial total count to zero count = 1 # Starting count at one print "The file has %s lines" % linecount for line in lines: charcount = len(line) # Get character count print "Line %s has %s character (including punctuation and white space)." % (count, charcount) totalcharcount += charcount #Add the charcount to the total charcount print "The total file character count is %s" % totallinecount - Hope this helps. Greets Sander ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] intefaces in python
"Amit Sethi" wrote Well I want to implement plug-in like mechanism for an application . I want to define some minimum functions that any body writing a plugin has to implement. For that i thought an interface would be best because in a scenario where the function is not implemented some kind of error would occur. I would love to hear if you think their is a better way to achieve this Well you could define a real default plugin class that actually does something. Then anyone who wants to create a plugin can inherit from that and either extend the default functions or override them with something different. That will reduce the code that plug-in writers need to create and prevent any error messages appearing for the user. It also means the default plug in acts as sample code for the interface too. HTH, Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] "Print" on 3.1 for Mac?
Have I installed something incorrectly in Python 3.1 for Mac if I get a syntax error on print "hello world" ? My Python 2.3.5 executes this just fine. What have I missed? Thanks much for anything, Lev ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] "Print" on 3.1 for Mac?
l...@ithstech.com wrote: Have I installed something incorrectly in Python 3.1 for Mac if I get a syntax error on print "hello world" ? My Python 2.3.5 executes this just fine. What have I missed? Thanks much for anything, Lev ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor Please take a read through http://docs.python.org/3.0/whatsnew/3.0.html for a list of changes from the 2.x series to 3.x. It includes the fact that the print command is now a function and needs to be called accordingly, ie. print("Hello world!") -- Kind Regards, Christian Witts ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] intefaces in python
Amit Sethi wrote: Well I want to implement plug-in like mechanism for an application . I want to define some minimum functions that any body writing a plugin has to implement. For that i thought an interface would be best because in a scenario where the function is not implemented some kind of error would occur. I would love to hear if you think their is a better way to achieve this In Java, deriving a class from such an interface, but neglecting to implement those methods will cause a compile error (if I recall correctly, it's been several years). In Python, the error will happen at run time. But an existing runtime error will occur even without such an interface, so it wouldn't seem you gain much. In Python, the interface does two things: 1) it's a comment, a common place to look for certain behavior. 2) it's potentially a source for an IDE to provide tool-tips or code completion 3) it can generate a different error, which is perhaps more useful to the developer unsure of how the method is spelled or used. This way he/she knows whether to fix the caller or the implementation. #3 seems valid to me. However, for the particular use-case, you might want to stretch a bit further. Since you've got one "user" (your code), and many "providers" (the plug-in writers) perhaps you could arrange that when the plugin is first encountered, you validate that it has all the required methods and data members. Not by calling them, but by scanning the object for their existence. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] [Fwd: Re: "Print" on 3.1 for Mac?]
Forwarding to the list. Original Message Subject:Re: [Tutor] "Print" on 3.1 for Mac? Date: Mon, 29 Jun 2009 12:38:43 +0100 From: andré palma To: Christian Witts References: <4a489e92.2040...@compuscan.co.za> try to find out if is there any space before "print". Sometimes some text editors put an empty space in the beginning of the lines and sometimes that causes some erros. On Mon, 2009-06-29 at 12:59 +0200, Christian Witts wrote: l...@ithstech.com wrote: > Have I installed something incorrectly in Python 3.1 for Mac if I get a > syntax error on > > print "hello world" > > ? > > My Python 2.3.5 executes this just fine. What have I missed? > > Thanks much for anything, Lev > > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > > Please take a read through http://docs.python.org/3.0/whatsnew/3.0.html for a list of changes from the 2.x series to 3.x. It includes the fact that the print command is now a function and needs to be called accordingly, ie. print("Hello world!") -- Kind Regards, Christian Witts ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] intefaces in python
I think ideally i want a compile Error just like java .. but from the discussion here ... i wrote this little example: class a(object): def __init__(self): self.query() try: if self.query_not_implemented==True: raise NotImplementedError except AttributeError: pass def query(self): self.query_not_implemented=True At least by this when ever the derived class object is created without implementing a particular function ,it raises a NotImplementedError which i can later use. @Dave Angel You have said "you could arrange that when the plugin is first encountered, you validate that it has all the required methods and data members. Not by calling them, but by scanning the object for their existence." that would be ideal ... can you enlighten me on this how may one do that. On Mon, Jun 29, 2009 at 5:01 PM, Dave Angel wrote: > > Amit Sethi wrote: > >> Well I want to implement plug-in like mechanism for an application . I want >> to define some minimum functions that any body writing a plugin has to >> implement. For that i thought an interface would be best because in a >> scenario where the function is not implemented some kind of error would >> occur. I would love to hear if you think their is a better way to achieve >> this > > In Java, deriving a class from such an interface, but neglecting to implement > those methods will cause a compile error (if I recall correctly, it's been > several years). In Python, the error will happen at run time. But an > existing runtime error will occur even without such an interface, so it > wouldn't seem you gain much. In Python, the interface does two things: > > 1) it's a comment, a common place to look for certain behavior. > 2) it's potentially a source for an IDE to provide tool-tips or code > completion > 3) it can generate a different error, which is perhaps more useful to the > developer unsure of how the method is spelled or used. This way he/she knows > whether to fix the caller or the implementation. > > #3 seems valid to me. > > > > However, for the particular use-case, you might want to stretch a bit > further. Since you've got one "user" (your code), and many "providers" (the > plug-in writers) perhaps you could arrange that when the plugin is first > encountered, you validate that it has all the required methods and data > members. Not by calling them, but by scanning the object for their existence. > > > ___ > Tutor maillist - tu...@python.org > http://mail.python.org/mailman/listinfo/tutor -- A-M-I-T S|S ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] intefaces in python
wait even in the above example i would have to run all the functions in __init__ that is plain stupid ... i was just brain storming On Mon, Jun 29, 2009 at 5:34 PM, Amit Sethi wrote: > I think ideally i want a compile Error just like java .. but from the > discussion here ... i wrote this little example: > > class a(object): > def __init__(self): > self.query() > try: > if self.query_not_implemented==True: > raise NotImplementedError > except AttributeError: > pass > def query(self): > self.query_not_implemented=True > > At least by this when ever the derived class object is created without > implementing a particular function ,it raises a NotImplementedError > which i can later use. > > > @Dave Angel > > You have said > "you could arrange that when the plugin is first encountered, you > validate that it has all the required methods and data members. Not > by calling them, but by scanning the object for their existence." > > that would be ideal ... can you enlighten me on this how may one do that. > > > On Mon, Jun 29, 2009 at 5:01 PM, Dave Angel wrote: >> >> Amit Sethi wrote: >> >>> Well I want to implement plug-in like mechanism for an application . I want >>> to define some minimum functions that any body writing a plugin has to >>> implement. For that i thought an interface would be best because in a >>> scenario where the function is not implemented some kind of error would >>> occur. I would love to hear if you think their is a better way to achieve >>> this >> >> In Java, deriving a class from such an interface, but neglecting to >> implement those methods will cause a compile error (if I recall correctly, >> it's been several years). In Python, the error will happen at run time. >> But an existing runtime error will occur even without such an interface, so >> it wouldn't seem you gain much. In Python, the interface does two things: >> >> 1) it's a comment, a common place to look for certain behavior. >> 2) it's potentially a source for an IDE to provide tool-tips or code >> completion >> 3) it can generate a different error, which is perhaps more useful to the >> developer unsure of how the method is spelled or used. This way he/she >> knows whether to fix the caller or the implementation. >> >> #3 seems valid to me. >> >> >> >> However, for the particular use-case, you might want to stretch a bit >> further. Since you've got one "user" (your code), and many "providers" (the >> plug-in writers) perhaps you could arrange that when the plugin is first >> encountered, you validate that it has all the required methods and data >> members. Not by calling them, but by scanning the object for their >> existence. >> >> >> ___ >> Tutor maillist - tu...@python.org >> http://mail.python.org/mailman/listinfo/tutor > > > > -- > A-M-I-T S|S > -- A-M-I-T S|S ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] intefaces in python
Amit Sethi wrote: I think ideally i want a compile Error just like java .. Why? For developers, so they'll know if their plugin meets the interface requirements? Have you considered just making a unit test that will call all interface functions with appropriate parameters? Then they can just run the test on their code and if it passes they will know they implemented the minimum required functions (whether they do what they're supposed to do is a different story...) It would provide the same level of interface security as Java would, except it would allow them to decide how much of the contract they need to enforce for their specific application. That's one of the things I like about python... for example, if I want to override stdout so all my print statements go to a file, I just have to create a new class that has a write() method that writes anything it's passed to a file... I don't have to implement all of the other functionality that whatever object resides there before has. So in that case, if I ran your unit test on my replacement stdout, it may say "Hey, you failed the writeline() test, your object doesn't have this!" but since I know I'm not going to use writeline, I can just ignore the warning. Or if I meant for it to have writeline, I can say "oh snap I need to go implement writeline!" But as I said, that wouldn't enforce the interface, just inform them of it if they desired that, which I would personally prefer but you may not. That's really the idea behind duck typing. Assume that they wrote sufficient code to handle whatever you use it for and if they didn't it'll blow up in their face. @Dave Angel You have said "you could arrange that when the plugin is first encountered, you validate that it has all the required methods and data members. Not by calling them, but by scanning the object for their existence." that would be ideal ... can you enlighten me on this how may one do that. Not sure if it's what he meant, but you can just do a dir(object) and it will return a list with all method / member names. then you could just confirm that the names are there. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] intefaces in python
well dir(object) , how would that help . All the functions in base class would automatically be inherited by the objects of plug-in class ... so they would come in the list even if it was not implemented... On Mon, Jun 29, 2009 at 5:58 PM, Luke Paireepinart wrote: > Amit Sethi wrote: >> >> I think ideally i want a compile Error just like java .. > > Why? For developers, so they'll know if their plugin meets the interface > requirements? > Have you considered just making a unit test that will call all interface > functions with appropriate parameters? Then they can just run the test on > their code and if it passes they will know they implemented the minimum > required functions (whether they do what they're supposed to do is a > different story...) It would provide the same level of interface security > as Java would, except it would allow them to decide how much of the contract > they need to enforce for their specific application. That's one of the > things I like about python... for example, if I want to override stdout so > all my print statements go to a file, I just have to create a new class that > has a write() method that writes anything it's passed to a file... I don't > have to implement all of the other functionality that whatever object > resides there before has. So in that case, if I ran your unit test on my > replacement stdout, it may say "Hey, you failed the writeline() test, your > object doesn't have this!" but since I know I'm not going to use writeline, > I can just ignore the warning. Or if I meant for it to have writeline, I > can say "oh snap I need to go implement writeline!" But as I said, that > wouldn't enforce the interface, just inform them of it if they desired that, > which I would personally prefer but you may not. > That's really the idea behind duck typing. Assume that they wrote > sufficient code to handle whatever you use it for and if they didn't it'll > blow up in their face. >> >> @Dave Angel >> >> You have said >> "you could arrange that when the plugin is first encountered, you >> validate that it has all the required methods and data members. Not >> by calling them, but by scanning the object for their existence." >> >> that would be ideal ... can you enlighten me on this how may one do that. >> >> > > Not sure if it's what he meant, but you can just do a dir(object) and it > will return a list with all method / member names. then you could just > confirm that the names are there. > -- A-M-I-T S|S ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] does python have something like "#include" in C?
... or any pre-processing at all? I'm looking for a way to get boiler plate code into the main program file. Of course I could copy and paste it with an editor but I was hoping for something more pythonic. I know about import but that's not the same. -- Robert Lummis ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 64, Issue 128
Amit Sethi wrote: . On Mon, Jun 29, 2009 at 5:01 PM, Dave Angel wrote: > > Amit Sethi ?wrote: > >> Well I want to implement plug-in like mechanism for an application . I want >> to define some minimum functions that any body writing a plugin has to >> implement. For that i thought an interface would be best because in a >> scenario where the function is not implemented some kind of error would >> occur. I would love to hear if you think their is a better way to achieve >> this > > In Java, deriving a class from such an interface, but neglecting to implement those methods will cause a compile error (if I recall correctly, it's been several years). ?In Python, the error will happen at run time. ?But an existing runtime error will occur even without such an interface, so it wouldn't seem you gain much. ?In Python, the interface does two things: > > 1) it's a comment, a common place to look for certain behavior. > 2) it's potentially a source for an IDE to provide tool-tips or code completion > 3) it can generate a different error, which is perhaps more useful to the developer unsure of how the method is spelled or used. ?This way he/she knows whether to fix the caller or the implementation. > > #3 seems valid to me. > > > > However, for the particular use-case, you might want to stretch a bit further. ?Since you've got one "user" (your code), and many "providers" (the plug-in writers) ?perhaps you could arrange that when the plugin is first encountered, you validate that it has all the required methods and data members. ?Not by calling them, but by scanning the object for their existence. > It'd sure be easier if you had a sample of what you're already expecting to do. Then I could use similar names, and refer to things much more easily. It also might matter which version/implementation of Python you're using. I'm using CPython 2.6 on XP. I'll guess that you're using __import__ to import a module based on its name or location. Further, I'll guess that the class name the user is supposed to implement is called Doit, and that if you had used interfaces, they would derive from your interface. So you have code something like: #interfacemodule.py class MyInterface(object): def __init__(self, modulename): for method in ["doSomething", "doSomethingElse"]: m = getattr(self, method, None) if not m: print "Missing attribute %s in module %s, class Doit" % (method, modulename) raise NotImplementedError else: print "Found :", method, m and they have code like: import interfacemodule class Doit(MyInterface): def doSomething(self, line): print "line=", line def doSomethingElse42(self, line): #method name deliberately spelled wrong, to trigger the error print "line=", line After doing the __import__, you instantiate the new class. Something like (untested): x = __import__(filename) newobject = x.Doit(filename) Instantiating that object will test the attributes, making sure everything in your list exists. Once you're sure they do, add the newobject to your list of plugins. There's lots of other choices, but I suspect most will be trickier than this. You may need them, depending on how much you trust your plugin writers. For example, this doesn't help if they neglect to derive from MyInterface. That's no big deal, since you could use a regular function instead of __init__ method, and run it on the object. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] does python have something like "#include" in C?
On Mon, Jun 29, 2009 at 10:03 AM, Robert Lummis wrote: > ... or any pre-processing at all? > > I'm looking for a way to get boiler plate code into the main program > file. Of course I could copy and paste it with an editor but I was > hoping for something more pythonic. I know about import but that's not > the same. > actually import is precisely the same. wa...@x61:test$ vi import1.py wa...@x61:test$ vi import2.py wa...@x61:test$ python import1.py Imported The contents of import1? import import2 The contents of import2? print "Imported" You can define functions, classes, etc. you call them as whatever.function(), unless you say from module import * - then it willl import everything into the namespace of your original file. HTH, Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] does python have something like "#include" in C?
Robert Lummis wrote: ... or any pre-processing at all? I'm looking for a way to get boiler plate code into the main program file. Of course I could copy and paste it with an editor but I was hoping for something more pythonic. I know about import but that's not the same. Python is very good at eliminating boilerplating, so import should be enough normally. Could you give an example of what you want to include that cannot be done with import? Albert ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] does python have something like "#include" in C?
On 6/29/2009 8:03 AM Robert Lummis said... ... or any pre-processing at all? I'm looking for a way to get boiler plate code into the main program file. Of course I could copy and paste it with an editor but I was hoping for something more pythonic. I know about import but that's not the same. import is the pythonic way. Most boilerplate code I've come across provides basic functionality which is then accessed and used locally to complete the task at hand. In Python you'd write library code to provide that functionality and import and invoke those functions. Emile ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] intefaces in python
"Amit Sethi" wrote I think ideally i want a compile Error just like java .. I think you are trying to make Python act like Java which is always a really bad mistake when using a programming language. As Bjarne Stroustrup used to say (repeatedly) "C++ is not Smalltalk". And Python is not Java. If you stop trying to design like Java and start using the extra power of Python you will find your solutions are both more flexible and more simple. Python does not compile so trying to build in "compile time" checking makes no sense. You could do some checks immediately after class definition or more likely class assignment but how would it really benefit you? Get used to how interpreted dynamic languages work and use their features to your benefit instead of trying to force them to act like statically typed compiled languages. It will give you a great sense of freedom! -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] "Print" on 3.1 for Mac?
wrote Have I installed something incorrectly in Python 3.1 for Mac if I get a syntax error on print "hello world" My Python 2.3.5 executes this just fine. What have I missed? The "Whats New" document on V3 V3 is radically different to previous versiions and you need to read the Whats New document carefully and change all your code to match. Only change to V3 if you have a really good reason and know what that is, otherwise you are probably better sticking with 2.X for now. Moving to 2.6 is a good migratiion step since there are tools to convert most of your code from 2.6 to 3. But if you are moving from 2.3 to 3 that is a big jump and you should definitely go to 2.6 as an interim step. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] does python have something like "#include" in C?
"Robert Lummis" wrote I'm looking for a way to get boiler plate code into the main program file. Of course I could copy and paste it with an editor but I was hoping for something more pythonic. I know about import but that's not the same. Others have already pointed out import. Its also worth pointing out that #include is a pretty horrible hack in C that most modern commentators agree would be better removed. Bjarne Stroustrup tried very hard to remove it completely from C++ (apart from including class headers obviously which is akin to Pythons import). But #define and #ifdef etc are all better done in other ways. Pythons import mechanism with namespaces is a far more elegant solution all round. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] does python have something like "#include" in C?
Here's an example that seems not possible in python. I'm probably missing something so please enlighten me. I only tried doing this as an exercise to show myself how name references work. I'm not saying it's needed or that it's good practice. I can write the following as a single file and it works as expected: ===snip=== #!/usr/bin/python def show(*args): print for arg in args: print arg + ':', exec('print ' + arg) a=15 b='hello' x=['bob',3] show('a') show('a','b') show('a','b','x') ===snip=== The calls to 'show' output lines like "a: 15" which could be useful for debugging or some such purpose. However, it seems that I can't put the function definition in a file and import it because I can't find a way to refer to an object in the main program file from within a module file. I understand that it's a good thing to contol which namespaces are referenced by which code but isn't there sometimes a need for code in a module to access the main program file's namespace? My example may be a little contrived but isn't this ability legitimately needed at times? On Mon, Jun 29, 2009 at 11:22 AM, A.T.Hofkamp wrote: > Robert Lummis wrote: >> >> ... or any pre-processing at all? >> >> I'm looking for a way to get boiler plate code into the main program >> file. Of course I could copy and paste it with an editor but I was >> hoping for something more pythonic. I know about import but that's not >> the same. > > Python is very good at eliminating boilerplating, so import should be enough > normally. > > Could you give an example of what you want to include that cannot be done > with import? > > Albert > > -- Robert Lummis ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] does python have something like "#include" in C?
> However, it seems that I can't put the function definition in a file > and import it because I can't find a way to refer to an object in the > main program file from within a module file. I understand that it's a > good thing to contol which namespaces are referenced by which code but > isn't there sometimes a need for code in a module to access the main > program file's namespace? My example may be a little contrived but > isn't this ability legitimately needed at times? robert, you are wrong and right. :-) take your function definition and store in a file called, say showmodule.py. then put the remaining code inside something like foo.py. at the top of foo.py, you'll need: from showmodule import show then everything else will work. there is another syntax that is less recommended, and that is, "from showmodule import *" -- with the danger being exactly what you said above... you are "polluting" your (global) namespace by bringing in *everything* from the other module without much knowledge (necessarily) of what those names are, which may potentially conflict with variables with the same names (if they exist) in your local module. hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] does python have something like "#include" in C?
On Mon, Jun 29, 2009 at 12:23 PM, Robert Lummis wrote: > Here's an example that seems not possible in python. I'm probably > missing something so please enlighten me. I only tried doing this as > an exercise to show myself how name references work. I'm not saying > it's needed or that it's good practice. > > I can write the following as a single file and it works as expected: > > ===snip=== > #!/usr/bin/python > > def show(*args): >print >for arg in args: >print arg + ':', >exec('print ' + arg) > > a=15 > b='hello' > x=['bob',3] > > show('a') > show('a','b') > show('a','b','x') > ===snip=== > > The calls to 'show' output lines like "a: 15" which could be useful > for debugging or some such purpose. > > However, it seems that I can't put the function definition in a file > and import it because I can't find a way to refer to an object in the > main program file from within a module file. I understand that it's a > good thing to contol which namespaces are referenced by which code but > isn't there sometimes a need for code in a module to access the main > program file's namespace? My example may be a little contrived but > isn't this ability legitimately needed at times? I can't think of a single legitimate reason the module would need to access the object in the namespace in any way that isn't already provided. Especially in python it breaks the model of what objects are. Strings (and integers AFAIK) are literals. Consider: >>> x[0] = 'c' Traceback (most recent call last): File "", line 1, in ? TypeError: object doesn't support item assignment When you create a string in c/c++ you have a string of characters, and you can edit it as such. When you say knights = 'who say ni' in python, you are referencing knights to the literal string 'who say ni'. As far as python is considered, every single string combination already exists as an object, and your variable just references that object. At least that's the way I've understood it. So when you're trying to access the namespaces name for the object it's useless because there's nothing you can do with it. (And really it shouldn't be the responsibility of an outside function to modify the value of a variable anyway! It's also bad from a data security POV. You don't want functions messing with your data just by accident) Anyway... that's as far as I've been taught (and taught myself)... anyone notices any errors, please correct them! HTH, Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] "Print" on 3.1 for Mac?
On Mon, Jun 29, 2009 at 3:59 AM, Christian Witts wrote: > l...@ithstech.com wrote: >> >> Have I installed something incorrectly in Python 3.1 for Mac if I get a >> syntax error on >> >> print "hello world" >> >> My Python 2.3.5 executes this just fine. What have I missed? > > Please take a read through http://docs.python.org/3.0/whatsnew/3.0.html for > a list of changes from the 2.x series to 3.x. It includes the fact that the > print command is now a function and needs to be called accordingly, ie. > print("Hello world!") in addition to reading the "What's New" document, you should also be aware that there is a "2to3" tool that comes with Python that helps port your existing Python 2 scripts to be as 3.x-compliant as they can. hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] About the vertical bar input
Hello, I'm trying to write a script that simply execute a command line like: C:\...(path)..\Devenv solution /build "Debug|Win32" However, in Python the "|" symbol is reserved thus I just can't make the command line above working once I added the "|" sign in it. How can I put the "original" vertical bar in a string? Thanks! Shawn ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] does python have something like "#include" in C?
On Mon, Jun 29, 2009 at 1:23 PM, Robert Lummis wrote: > Here's an example that seems not possible in python. I'm probably > missing something so please enlighten me. I only tried doing this as > an exercise to show myself how name references work. I'm not saying > it's needed or that it's good practice. > > I can write the following as a single file and it works as expected: > > ===snip=== > #!/usr/bin/python > > def show(*args): > print > for arg in args: > print arg + ':', > exec('print ' + arg) > > a=15 > b='hello' > x=['bob',3] > > show('a') > show('a','b') > show('a','b','x') > ===snip=== > > The calls to 'show' output lines like "a: 15" which could be useful > for debugging or some such purpose. > > However, it seems that I can't put the function definition in a file > and import it because I can't find a way to refer to an object in the > main program file from within a module file. Right. We did recently discuss ways to implement this function: http://www.mail-archive.com/tutor@python.org/msg35873.html > I understand that it's a > good thing to contol which namespaces are referenced by which code but > isn't there sometimes a need for code in a module to access the main > program file's namespace? My example may be a little contrived but > isn't this ability legitimately needed at times? Generally no, that would be a design smell. Module dependencies should be one-way; if main needs module foo, then foo should not have to know about main. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] does python have something like "#include" in C?
On Mon, Jun 29, 2009 at 1:46 PM, wesley chun wrote: > take your function definition and store in a file called, say > showmodule.py. then put the remaining code inside something like > foo.py. at the top of foo.py, you'll need: > > from showmodule import show > > then everything else will work. I don't think so. The exec in show() must be run in the namespace in which the symbols being displayed are defined. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] About the vertical bar input
On Mon, Jun 29, 2009 at 1:24 PM, hyou wrote: > I’m trying to write a script that simply execute a command line like: > > C:\...(path)..\Devenv solution /build “Debug|Win32” > > > > However, in Python the “|” symbol is reserved thus I just can’t make the > command line above working once I added the “|” sign in it. > > How can I put the “original” vertical bar in a string? You can put a vertical bar in a string; In [1]: s = "Debug|Win32" In [2]: print s Debug|Win32 Can you show us the code you tried? My guess is that you are having trouble getting the quoting correct. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] does python have something like "#include" in C?
>> from showmodule import show >> >> then everything else will work. > > I don't think so. The exec in show() must be run in the namespace in > which the symbols being displayed are defined. yep, you're right. i didn't see the exec. it will only work if the called function somehow had access to the same object references. the OP would have to put the assignments into showmodule.py in addition to show(), leaving only the calls to show() in foo.py. that *should* work. cheers, -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Python Web Development with Django", Addison Wesley, (c) 2009 http://withdjango.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] About the vertical bar input
"hyou" wrote: Hello, I'm trying to write a script that simply execute a command line like: C:\...(path)..\Devenv solution /build "Debug|Win32" However, in Python the "|" symbol is reserved thus I just can't make the command line above working once I added the "|" sign in it. How can I put the "original" vertical bar in a string? Thanks! It would help if you mentioned which version of python you're using, and in what OS environment. I can guess you're running some Windows version because of the C:\ but the problem could also be different between XP and Vista, and certainly different between Windows95 and Windows 7. It also would help if you actually showed what you tried, and what the response was. Cut and paste the command window text into your message. I'm going to guess you did not mean "write a script that simply execute a command line like" but instead meant that the command line would be entered in a batch file, or at the command prompt, and that the python script was the first argument to the command line. In your case, the script is called "Devenv.py" and that you replaced most of its path with ellipses. As far as I know, the vertical bar isn't a special character in Python at all. It is, however, special in CMD.EXE, the command interpreter for recent Windows versions. It is used to specify multiple programs on the same command line, where the stdout of the first is piped into the stdin of the next. Similarly, the > and < symbols are used to specify redirection, and the & symbol is used to separate two commands that are to be run sequentially. CMD.EXE isn't the only shell available for Windows, but it's the standard one built into all the recent versions. So I'm guessing that's what you're using. If you're running a different one, such as 4NT, let us know. In some versions of Windows, for example in XP, the | character is not special inside a quoted string. So the example you sort-of supplied would have no problem. To demonstrate, I created a trivial Python script echo2.py: import sys print "Args are ---", sys.argv, "---" Then the following is pasted from a CMD window: M:\Programming\Python\sources\dummy>echo2 How are you? Args are --- ['M:\\Programming\\Python\\sources\\dummy\\echo2.py', 'How', 'are', 'you?'] --- M:\Programming\Python\sources\dummy>echo2 This has a "vertical|bar" Args are --- ['M:\\Programming\\Python\\sources\\dummy\\echo2.py', 'This', 'has' , 'a', 'vertical|bar'] --- M:\Programming\Python\sources\dummy>echo2 This is not|quoted 'quoted' is not recognized as an internal or external command, operable program or batch file. Note that if you cannot do matching quotes, you can get some funny groupings of text. For example, if you have leading quotes but no trailing quotes, then the entire rest of the line will be one element of sys.argv. DaveA ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] GASP on OSX 10.5.6
Hi, I am a complete python beginner. I have been going through How to Think Like A Computer Scientist 2nd edition online and have come across installing GASP in chapter 4. Unfortunately, I can't seem to find much documentation on how to install GASP when running OSX and Python 2.6.2. Any help would be greatly appreciated. Thanks! -Daniel -- Daniel Sato http://www.danielsato.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Pythonmac-SIG] GASP on OSX 10.5.6
On Mon, Jun 29, 2009 at 3:59 PM, Daniel Sato wrote: > Hi, > > I am a complete python beginner. I have been going through How to Think > Like A Computer Scientist 2nd edition online and have come across installing > GASP in chapter 4. Unfortunately, I can't seem to find much documentation > on how to install GASP when running OSX and Python 2.6.2. Any help would be > greatly appreciated. Mac OS X apparently not a supported platform: https://answers.launchpad.net/gasp-code/+faq/42 Cheers, Chris -- http://blog.rebertia.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] does python have something like "#include" in C?
Thanks very much for all your responses. It's pretty clear now that what I thought I could somehow do is not "pythonic" and for good reason. Kent Johnson says it well: "Module dependencies should be one-way" I can buy that. On Mon, Jun 29, 2009 at 2:47 PM, Kent Johnson wrote: > On Mon, Jun 29, 2009 at 1:23 PM, Robert Lummis wrote: >> Here's an example that seems not possible in python. I'm probably >> missing something so please enlighten me. I only tried doing this as >> an exercise to show myself how name references work. I'm not saying >> it's needed or that it's good practice. >> >> I can write the following as a single file and it works as expected: >> >> ===snip=== >> #!/usr/bin/python >> >> def show(*args): >> print >> for arg in args: >> print arg + ':', >> exec('print ' + arg) >> >> a=15 >> b='hello' >> x=['bob',3] >> >> show('a') >> show('a','b') >> show('a','b','x') >> ===snip=== >> >> The calls to 'show' output lines like "a: 15" which could be useful >> for debugging or some such purpose. >> >> However, it seems that I can't put the function definition in a file >> and import it because I can't find a way to refer to an object in the >> main program file from within a module file. > > Right. We did recently discuss ways to implement this function: > http://www.mail-archive.com/tutor@python.org/msg35873.html > >> I understand that it's a >> good thing to contol which namespaces are referenced by which code but >> isn't there sometimes a need for code in a module to access the main >> program file's namespace? My example may be a little contrived but >> isn't this ability legitimately needed at times? > > Generally no, that would be a design smell. Module dependencies should > be one-way; if main needs module foo, then foo should not have to know > about main. > > Kent > -- Robert Lummis ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] About the vertical bar input
"hyou" wrote in message news:blu143-ds478a37dc2b1db050e5b96c4...@phx.gbl... Hello, I'm trying to write a script that simply execute a command line like: C:\...(path)..\Devenv solution /build "Debug|Win32" However, in Python the "|" symbol is reserved thus I just can't make the command line above working once I added the "|" sign in it. How can I put the "original" vertical bar in a string? Without an example of the code you are using, my guess is you are using os.system to execute a command on Windows. The Windows shell (cmd.exe) uses veritical bar for the pipe command, which sends output from one program into the input of another. The caret(^) is used in the Windows to escape special characters. Example: import os os.system('echo Hello|World') 'World' is not recognized as an internal or external command, operable program or batch file. 255 os.system('echo Hello^|World') Hello|World 0 -Mark ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor