Re: [Tutor] problem with canvas.postscript()
On Mon, 22 Jan 2007 20:14:31 + "Asrarahmed Kadri" <[EMAIL PROTECTED]> wrote: > Hi folks, > > I am trying to save a tkinter canvas using canvas.postscript() method... > > The thing works fine when the canvas size is small; but it gets cut (from > the top and left side) when the size increases.. > Here is the code: > > c.postscript(file=filename,height = canvas_height,width = > canvas_width,colormode='color',pagex=200,pagey=250) > > The image also gets printed very low on the page in the postscript file..?? > > Can anybody help me to fix the problem..? > > Thanks in anticipation. > Have you tried to omit the pagex and pagey options, if you call postscript without any options (except filename) the whole canvas should be centered on the page. Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Some "type" confusion...
Hello I am learning Python via the excellent Dive Into Python book. I have little question 1) import os type(os.environ) Why is this considered "instance" ? Should'nt it be "dict" type? I have a feeling I am missing something deeper here. 2) What would happen if I did this os.environ = {} Hopefully, I wont be allowed to do that...but I am too scared to try. Sincere Regards, ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Some "type" confusion...
Gizmo wrote: > Hello > I am learning Python via the excellent Dive Into Python book. I have > little question > > 1) > >>> import os > >>> type(os.environ) > > > Why is this considered "instance" ? Should'nt it be "dict" type? > I have a feeling I am missing something deeper here. 'instance' is the type of instances of old-style classes. For example: In [4]: class Foo: pass ...: In [6]: f=Foo() In [7]: type(f) Out[7]: On the other hand, the type of an instance of a new-style class reflects its actual class: In [9]: class Bar(object): pass ...: In [10]: b=Bar() In [11]: type(b) Out[11]: You can find out the class of an instance by looking at its __class__ attribute: In [3]: os.environ.__class__ Out[3]: You can also see the base classes of the class: In [8]: os.environ.__class__.__bases__ Out[8]: (,) So os.environ is not a dict or a subclass of dict. It is a subclass of UserDict.IterableUserDict. This is a class that is used to make custom mapping objects. If you look at the docs for os.environ you see that it does not say it is a dict, it says it is a mapping object; that is, it is something that acts like a dict. See also http://docs.python.org/ref/node33.html > 2) What would happen if I did this > >>> os.environ = {} > Hopefully, I wont be allowed to do that...but I am too scared to try. You can do it. That would replace the os.environ object with an empty dict, which would have a bad effect on any code that reads values from os.environ. On the other hand you can restore the old value just by restarting Python so it is not such a disaster. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Difference between filter and map
i want to know the difference between filter(function,sequence) and map(function,sequence).I tried for a simple script with an function which finds the square of the number,after including separately filter and map in the script i am getting the same results for instance def squ(x): return x*x filter(squ,range(1,3))->1,4(output) map(squ,range(1,3)->1,4(output) -- Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between filter and map
vanam wrote: > i want to know the difference between filter(function,sequence) and > map(function,sequence).I tried for a simple script with an function > which finds the square of the number,after including separately filter > and map in the script i am getting the same results for instance > def squ(x): > return x*x > filter(squ,range(1,3))->1,4(output) > map(squ,range(1,3)->1,4(output) Are you sure about that? I get In [1]: def sq(x): return x*x ...: In [2]: filter(sq, range(3)) Out[2]: [1, 2] In [3]: map(sq, range(3)) Out[3]: [0, 1, 4] map(fn, lst) returns a new list with fn applied to each element of lst. In terms of list comprehensions, it is [ fn(x) for x in lst ]. filter(fn, lst) returns a new list containing all elements of the original list for which fn(x) is true. As a list comprehension, it is [ x for x in lst if fn(x) ] Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between filter and map
ya i am sure about that i am using python editor which has python intrepreter attached to it i got the same output for both filter and map def squ(n): y = n*n print y filter(y,range(3))->0 1 4 map(y,range(3))->0 1 4 On 1/23/07, Kent Johnson <[EMAIL PROTECTED]> wrote: vanam wrote: > i want to know the difference between filter(function,sequence) and > map(function,sequence).I tried for a simple script with an function > which finds the square of the number,after including separately filter > and map in the script i am getting the same results for instance > def squ(x): > return x*x > filter(squ,range(1,3))->1,4(output) > map(squ,range(1,3)->1,4(output) Are you sure about that? I get In [1]: def sq(x): return x*x ...: In [2]: filter(sq, range(3)) Out[2]: [1, 2] In [3]: map(sq, range(3)) Out[3]: [0, 1, 4] map(fn, lst) returns a new list with fn applied to each element of lst. In terms of list comprehensions, it is [ fn(x) for x in lst ]. filter(fn, lst) returns a new list containing all elements of the original list for which fn(x) is true. As a list comprehension, it is [ x for x in lst if fn(x) ] Kent -- Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between filter and map
On Tue, 23 Jan 2007, vanam wrote: > i want to know the difference between filter(function,sequence) and > map(function,sequence). Hi Vanam, They may both take functions as input, but the intention of the functions is different. In the case of filter(), the input function is used to cull the good elements of the sequence out. >>> def starts_with_ab(word): ... return word[:2] == 'ab' ... >>> filter(starts_with_ab, ["abracadabra", "open sesame", "abraham lincoln"] ... ) ['abracadabra', 'abraham lincoln'] > i am getting the same results for instance > def squ(x): >return x*x > filter(squ,range(1,3))->1,4(output) > map(squ,range(1,3)->1,4(output) This doesn't look right. Please copy and paste exactly what you typed, and exactly what the program outputted. You need to do this carefully or we can't reproduce what you see. In fact, concretely, when you reply to Kent, you show a different definition of squ(): ### def squ(n): y = n*n print y ### Be more careful next time. In this case, squ() isn't even returning back a value, so you can't expect good things to come out of filter() and map(): filter and map depend on the input function actually giving values back. What you're seeing on screen is the "side-effects" of calling squ() on every element. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between filter and map
vanam wrote: > ya i am sure about that i am using python editor which has python > intrepreter attached to it i got the same output for both filter and map > def squ(n): >y = n*n > print y > filter(y,range(3))->0 1 4 > map(y,range(3))->0 1 4 This is quite different that what you posted the first time. This function squ() *prints* n*n but *returns* None (since it has no explicit return statement). The previous squ() actually returned n*n. But the results are still different if you look carefully: In [2]: def sq(n): ...: y=n*n ...: print y ...: ...: In [3]: map(sq, range(3)) 0 1 4 Out[3]: [None, None, None] The function sq() is called for each element of range(3) and prints the square. This is why 0, 1, 4 are printed. But the value returned from map() is the list [None, None, None] which is the accumulated return values from calling sq(). In [4]: filter(sq, range(3)) 0 1 4 Out[4]: [] Here, sq() is still called for each element of range(3). Since the printing is from sq(), 0, 1 and 4 are still printed. But the return value is an empty list [] because None is not true so sq(n) is not true for any elements of range(3). Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between filter and map
vanam wrote: > i want to know the difference between filter(function,sequence) and > map(function,sequence). >>> print filter.__doc__ filter(function or None, sequence) -> list, tuple, or string Return those items of sequence for which function(item) is true. If function is None, return the items that are true. If sequence is a tuple or string, return the same type, else return a list. >>> print map.__doc__ map(function, sequence[, sequence, ...]) -> list Return a list of the results of applying the function to the items of the argument sequence(s). If more than one sequence is given, the function is called with an argument list consisting of the corresponding item of each sequence, substituting None for missing values when not all sequences have the same length. If the function is None, return a list of the items of the sequence (or a list of tuples if more than one sequence). >>> filter returns a subsequence of a sequence based on passing each item in the sequence to a function which returns a *boolean context*. If the returns value's boolean context is true, the item is placed in the new subsequence. map returns a sequence of the same length based on the return value of passing each item in the sequence to a function. One literally filters a sequence. The other literally maps a sequence. filter can return a tuple, string, or list. map only returns a list. I tried for a simple script with an function > which finds the square of the number,after including separately filter > and map in the script i am getting the same results for instance > def squ(x): > return x*x > filter(squ,range(1,3))->1,4(output) > map(squ,range(1,3)->1,4(output) The boolean context of the return value of squ is true for all items of the sequence which you passed it in filter. Also, the filter you showed above does *not* return [1,4]. It returns [1,2], which is every item in range(1,3), because every item in that list passes the filter function's boolean context (is x*x true?). -- Sincerely, Chris Calloway http://www.seacoos.org office: 332 Chapman Hall phone: (919) 962-4323 mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between filter and map
vanam wrote: > ya i am sure about that i am using python editor which has python > intrepreter attached to it i got the same output for both filter and map > def squ(n): >y = n*n > print y > filter(y,range(3))->0 1 4 > map(y,range(3))->0 1 4 You are not printing the result of either the filter or map function here. You have the print statement embedded in squ. In fact you wouldn't print anything but a NameError exception here because you haven't passed filter or map a function, just an identifier which isn't in their scope: >>> def squ(n): ...y = n*n ...print y ... >>> filter(y, range(3)) Traceback (most recent call last): File "", line 1, in ? NameError: name 'y' is not defined >>> Also not, the function squ as defined here always returns None, so it is useless as either a filtering or mapping function. Printing a value is not the same as returning a value. -- Sincerely, Chris Calloway http://www.seacoos.org office: 332 Chapman Hall phone: (919) 962-4323 mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Variables of Variables
*** Stuff deleted Wanted to give you an update. It is working now. Thank you ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Question about structuring my pygtk program
Hi Everybody, I have a question about structuring first pygtk program now that I have the basics working, I am moving to the next step of organizing my program so it doesn't turn into spaghetti code. Right now, what I'm doing is if a component of the GUI is used in more than one spot, I make that into a separate little function returning a GUI fragment that I can call several times throughout the program. I have been doing this type of "non-redundant code" in a non-GUI environment for many years, and it's worked well. Also, I read about this technique from the Pragmatic Programmer - having one section of code that does a specific task - instead of having it in multiple locations. Does this translate to the GUI environment? If so, is there an easy of accessing the different input boxes easily? If you need a code exxample, just ask. Thanks, -Tino ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Thanks
Hello, I want to thank all you for the all help that you have been lending to me for the past months. My masters thesis is now finished and I have to say that it has been very succesfull. This is something that would have been impossible if not for all the help that you gave to me. I was thinking in mentioning those of you who helped me, but really fear to miss someone, so thanks to all of you who contributed. Thank you very much. This doesnt means that you will get rid of me, I want to get into OO now. :) Cheers, Carlos ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] OOP - have I done it right or could it be better?
Hi, I'm fairly new to using OOP and very new to Python, my first program is coming along well and I have split the code into 2 modules. The program finds stream urls, downloads them, re-encodes them to othe formats, all this functionality I have created in one module with one class called Streamrip and methods to handle this. The second module contains all the code to handle the pygtk interface and written as a separate class called Windowapp() You call the Windowapp() routine, in its' init method I have a call to create an instance of the streamrip class: self.UC = Streamrip(var=) from then on within the Windowapp class i refer to the methods in streamrip like self.UC.Dumpstream(...) etc. I've got this far and now I am thinking all though it works would it be better another way for example should I have used inheritence and allowed the windowapp class to inherit the methods of streamrip()? if you want to see the code I can upload it. It would be good to get some advice, thanks. Wayne. ___ New Yahoo! Mail is the ultimate force in competitive emailing. Find out more at the Yahoo! Mail Championships. Plus: play games and win prizes. http://uk.rd.yahoo.com/evt=44106/*http://mail.yahoo.net/uk ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OOP - have I done it right or could it be better?
Original Brownster wrote: > Hi, > I'm fairly new to using OOP and very new to Python, my first program is > coming along well and I have split the code into 2 modules. > > The program finds stream urls, downloads them, re-encodes them to othe > formats, all this functionality I have created in one module with one > class called Streamrip and methods to handle this. > The second module contains all the code to handle the pygtk interface > and written as a separate class called Windowapp() > > You call the Windowapp() routine, in its' init method I have a call to > create an instance of the streamrip class: > > self.UC = Streamrip(var=) > > from then on within the Windowapp class i refer to the methods in > streamrip like > > self.UC.Dumpstream(...) etc. This all sounds good. You have a module, Streamrip, that implements the core functionality you need. From your description, this module doesn't know about the GUI so it could be reused by another kind of client, tested, etc. You also have a module that implements a GUI front-end to Streamrip. All good. Just make sure the Streamrip module doesn't know anything about the Windowapp module - keep the dependencies one-way. > I've got this far and now I am thinking all though it works would it be > better another way for example should I have used inheritence and > allowed the windowapp class to inherit the methods of streamrip()? No, that is not better. Windowapp is not a kind of streamrip (which is what an inheritance relationship implies), it is a client of streamrip. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] metaclass question
Hi, what i've ended up doing is the following define a EmpiricalScore class that has all the methods for computing results define a single method in the evaluation class that is called Score. This method simply walks though a list and executes the methods in that list. There may be one or many. def score(self): """ this function computes the interaction energy and returns a dict of key=value pairs for the scoring functions requested """ for x in self.functionList: x() return(self.scoreResults) The factory class takes as agument some input data and determine which of the methods in the Evaluation class should be called. it then produces an instance of the EmpiricalScore class, (called scoreObject,) and then append the methods from that instance to its internal list. scoreObject=EmpiricalScore() #for each valid scoring function in the functions #tuple we call the method which adds the attribute for x in self.functionsToUse: #we need to check if we want both smog and smogh if x == "smog": if bothSmog == True: continue for y in self.functionsToUse: if y == "smog_h": scoreObject.functionList.append (scoreObject.calBothSmog) This is possibly not the best way to approach this, the factory class is possibly not required and could be added to the EmpiricalScore class. I think its probably better to separate these to keep things clean. In a way this method is constructing a decorator for the EmpiricalScore.score method. Is there a way of appending a method to the class after it is initialized. Or adding decorators to the method after it is defined? kim On Jan 22, 2007, at 5:14 PM, Kent Johnson wrote: > Kim Branson wrote: >> Hi i'm interested in implementing a factoryclass in python >> What i'd like to do is have my factoryClass produce an instance of >> a class with some methods defined in arguments to the factory class. >> The classes that are produced have many common methods, but a >> single unique method. This method actually is a series of calls >> to a c++ api. >> Depending on what we are doing with the produced class, i'd like >> the unique method to call api function A, or api function B >> etc. Alternatively the unique method might call A and the B and >> return a dict of the results. >> I'm doing this because i'd like all my produced class instances >> to simply have a calculateResults method which will then go and >> do the right thing. I don't want to set some values in the init, >> like A== True and have a if A: call methodA etc statement. > > Do you need to be passing in the unique method, or can you just > make a base class with the common methods and subclasses that > define their unique methods? For example, > > class Base(object): > def a(self): > pass > def b(self): > pass > def calculateResults(self): > raise NotImplementedError > > class A(Base): > def calculateResults(self): > return self.a() * self.b() > > class B(Base): > def calculateResults(self): > return dict(a=self.a(), b=self.b()) > > Kent > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] OOP - have I done it right or could it be better?
"Original Brownster" <[EMAIL PROTECTED]> wrote > The program finds stream urls, downloads them, re-encodes them to > othe > formats, all this functionality I have created in one module with > one > class called Streamrip and methods to handle this. Good so far. One nitpick is that class names should be nouns. They refer to objects not actions. So your class could maybe be called just Stream or RippingStream or somesuch. This has a knock on effect further down... > The second module contains all the code to handle the pygtk > interface > and written as a separate class called Windowapp() Thats fine. > You call the Windowapp() routine, in its' init method I have a call > to > create an instance of the streamrip class: > > self.UC = Streamrip(var=) And thats fine too, although UC isn't an entirely obvious name for the StreamRipper (to me anway!) > from then on within the Windowapp class i refer to the methods in > streamrip like > > self.UC.Dumpstream(...) etc. One of the features of OOP is that you don't need to include the class name in the method name since the object itself tells you what you are acting on. Thus we know that UC is the object and you just want to dump it. So just call the method dump... It keeps things shorter, avoids duplication and generally improves readability. But this is a cosmetic issue and others may disagree. But the general notion is that we are aiming for a semantic form of object.action it's only in procedural programming that you need to embed the type into the name. > better another way for example should I have used inheritence and > allowed the windowapp class to inherit the methods of streamrip()? Absolutely not. The WindowApp ius one thing the stream another. If you want to use the stream elsewhere(in a web app say) you would be stuck. You should always try to keep presentation and application logic separate (even in non OOP programs!) -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Difference between filter and map
Yes i did a mistake in expressing my problem below are the instances of the script and its corresponding output,for each instance its giving contrasting result i want explanation for that [1]:def squ(n): return n*n filter(squ,range(3))>output is not seen on the interpreter map(squ,range(3))->output not seen on the interpreter print filter(squ,range(3))->output is [1,2] print map(squ,range(3))-->output is [0,1,4] [2]:def squ(n): y = n*n print y filter(squ,range(3))-->Below is the output 0 1 4 map(squ,range(3))-->Below is the output 0 1 4 print filter(squ,range(3))--->Below is the output 0 1 4 [] print map(squ,range(3))-->Below is the output 0 1 4 [None,None,None] I want to know why in each case its giving different results and diff between filter and map On 1/23/07, Kent Johnson <[EMAIL PROTECTED]> wrote: vanam wrote: > ya i am sure about that i am using python editor which has python > intrepreter attached to it i got the same output for both filter and map > def squ(n): >y = n*n > print y > filter(y,range(3))->0 1 4 > map(y,range(3))->0 1 4 This is quite different that what you posted the first time. This function squ() *prints* n*n but *returns* None (since it has no explicit return statement). The previous squ() actually returned n*n. But the results are still different if you look carefully: In [2]: def sq(n): ...: y=n*n ...: print y ...: ...: In [3]: map(sq, range(3)) 0 1 4 Out[3]: [None, None, None] The function sq() is called for each element of range(3) and prints the square. This is why 0, 1, 4 are printed. But the value returned from map() is the list [None, None, None] which is the accumulated return values from calling sq(). In [4]: filter(sq, range(3)) 0 1 4 Out[4]: [] Here, sq() is still called for each element of range(3). Since the printing is from sq(), 0, 1 and 4 are still printed. But the return value is an empty list [] because None is not true so sq(n) is not true for any elements of range(3). Kent -- Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python Branches
i started slowly learning python as i dont have much exposure to it and i wanted to know what are all branches there in python actually i installed py 2.5 recently i come across question in groups about the gui where in pygtk should be installed.My question is without this installation could i able to develop any application using py2.5?One more thing is what is tkinter?Can any one explain what are all branches in python and how it could be used? -- Vanam ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Branches
vanam wrote: > > i started slowly learning python as i dont have much exposure to it > and i wanted to know what are all branches there in python actually i > installed py 2.5 recently i come across question in groups about the > gui where in pygtk should be installed.My question is without this > installation could i able to develop any application using py2.5? Well, that depends what the meaning of the word 'any' is. Do you mean 'any' to be one or more possible applications or do you mean it to be all possible applications? Yes, you can write many programs with the base python install. Potentially you could write any program; however, the point is not to rewrite code that someone has already done. That's the purpose of libraries. For example, python comes with a 'math' library which allows you to calculate sines and cosines of angles without having to understand the process by which someone calculates these values. > One more thing is what is tkinter?Can any one explain what are all > branches in python and how it could be used? pygtk and tkinter are libraries that people have written to let you write applications containing GUIs. pygtk is a separate download; tkinter comes with most Python distributions. > -- > >Vanam > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor