[Tutor] number distribution
Hey everbody, If we have 100 apples, for example, and we need to distrubte the 100 apples randomly in 10 boxes, how can we do this in python? Cheers ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] number distribution
I suggest you take a look at the random module. There should be something in there that suits your needs. On Thu, Apr 22, 2010 at 2:13 PM, Bill Jordan wrote: > Hey everbody, > > If we have 100 apples, for example, and we need to distrubte the 100 apples > randomly in 10 boxes, how can we do this in python? > > Cheers > > > ___ > Tutor maillist - tu...@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] number distribution
"Bill Jordan" wrote If we have 100 apples, for example, and we need to distrubte the 100 apples randomly in 10 boxes, how can we do this in python? This sounds like homework, and we don't do homework for you. But if you'd like to tell us your ideas, or even better show us whatever code you have tried to work, we can offer comments and ideas. In the meantime try looking at the documentation for the "random" module. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] sys.path and the path order
My question is so simple I'm surprised I can't find an answer somewhere. I'm interested if I can rely on the order of the directories in the sys.path list. When I'm running a file from the comand line like python tellusim.py The string in entry sys.path[0] appears to be the full path to the location of the file I'm running in this case tellusim ... i.e. it looks like '/Volumes/scone2/codes/tellusim0006'. This is good because for my code I need to create a search path for modules that is relative to the location of this file irrespective of the location I'm in when I invoke the script file (i.e. I could be in /Volumes/scone2 and invoke it by 'python codes/tellusim0006/tellusim.py'). The question is can I rely on entry [0] in sys.path always being the directory in which the original file resides (& across linux, OSX and Windows)? If not what is the reliable way of getting that information? ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] sys.path and the path order
On Fri, 23 Apr 2010 10:34:02 am Garry Willgoose wrote: > My question is so simple I'm surprised I can't find an answer > somewhere. Did you read the Fine Manual? > I'm interested if I can rely on the order of the > directories in the sys.path list. [...] > The question is can I rely on entry [0] in sys.path always being the > directory in which the original file resides (& across linux, OSX and > Windows)? The documentation says: sys.path A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default. As initialized upon program startup, the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH. http://docs.python.org/library/sys.html#sys.path So the answer to your question is, Yes. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Class Inheritance
Hello List! While experimenting with Tkinter(python2.6), when from Tkinter import* is used I came across the following error: C:\Users\ascent>c:\python26/Script3.py Traceback (most recent call last): File "C:\python26\Script3.py", line 1, in from Tkinter import * File "C:\Python26\lib\lib-tk\Tkinter.py", line 44, in from turtle import * File "C:\Python26\lib\lib-tk\turtle.py", line 374, in class ScrolledCanvas(Tkinter.Frame): AttributeError: 'module' object has no attribute 'Frame' * Which stems from the below in turtle.py: class ScrolledCanvas(TK.Frame) I know that ScrolledCanvas is trying to use class TK.Frame as it's base class to build from, and the class Frame is what is trying to be called from the Tkinter module. So I tried to alter the turtle.py. When I try to just 'from Tkinter import *, such as: from Tkinter import * class ScrolledCanvas(Tkinter.Frame): I get: * C:\Users\ascent>c:\python26/Script3.py Traceback (most recent call last): File "C:\python26\Script3.py", line 1, in from Tkinter import * File "C:\Python26\lib\lib-tk\Tkinter.py", line 44, in from turtle import * File "C:\Python26\lib\lib-tk\turtle.py", line 373, in class ScrolledCanvas(Tkinter.Frame): NameError: name 'Tkinter' is not defined *** I know pretty much what is going on there. But when I try to use: import Tkinter from Tkinter import * class ScrolledCanvas(Tkinter.Frame): It takes me back to the first error. Which means in both instances both directly called by me, and when called from the original turtle.py call, it's not finding the Frame class. >From the docs (9.5. Inheritance) it states: "The name BaseClassName must be defined in a scope containing the derived class definition. In place of a base class name, other arbitrary expressions are also allowed. This can be useful, for example, when the base class is defined in another module: class DerivedClassName(modname.BaseClassName) " So why does the above, from turtle.py, a standard module, not allow this, or is their something the module writer got wrong, or more likely, that I'm not understanding about what it's doing? As a sidenote, I ended up removing the from turtle import * line from Tkinter which resolved the problem(the example I was using didn't have a canvas, and I'm pretty sure Tkinter was defaulting to the ScrolledCanvas). TIA, David ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Class Inheritance
On Fri, 23 Apr 2010 03:11:36 pm David Hutto wrote: > Hello List! > > While experimenting with Tkinter(python2.6), when from Tkinter > import* is used I came across the following error: > > C:\Users\ascent>c:\python26/Script3.py > Traceback (most recent call last): > File "C:\python26\Script3.py", line 1, in > from Tkinter import * > File "C:\Python26\lib\lib-tk\Tkinter.py", line 44, in > from turtle import * > File "C:\Python26\lib\lib-tk\turtle.py", line 374, in > class ScrolledCanvas(Tkinter.Frame): > AttributeError: 'module' object has no attribute 'Frame' Something is screwy there. I believe you have broken your installation by making changes to files without having any understanding of what you are doing. The turtle module does this: import Tkinter as TK so that line should be "class ScrolledCanvas(TK.Frame)", and in fact that's what I find when I go and look at the source code: class ScrolledCanvas(TK.Frame): The line number is different too. How many breakages have you introduced to the library? > Which stems from the below in turtle.py: > > class ScrolledCanvas(TK.Frame) Note the difference between Tkinter.Frame and TK.Frame. > I know that ScrolledCanvas is trying to use class TK.Frame as it's > base class to build from, and the class Frame is what is trying to be > called from the Tkinter module. > > So I tried to alter the turtle.py. Please don't try to "fix" library files when you don't understand what they are doing. Confine your experiments to your own code, so that when things break, you know that the cause is in your code, not some random change you have done to the library. > When I try to just 'from Tkinter > import *, such as: > > from Tkinter import * > class ScrolledCanvas(Tkinter.Frame): That won't work, because there is no Tkinter defined. One wrong way to do it is: from Tkinter import * class ScrolledCanvas(Frame): but that risks stomping over the top of other variables with great big hob-nailed boots. Better to avoid import *, and do this: import Tkinter class ScrolledCanvas(Tkinter.Frame): but that's still a silly thing to do, because the turtle module has already imported Tkinter. The right way is to leave the module alone, it was working before you changed it: import Tkinter as TK class ScrolledCanvas(TK.Frame): > I get: > * > C:\Users\ascent>c:\python26/Script3.py > Traceback (most recent call last): > File "C:\python26\Script3.py", line 1, in > from Tkinter import * > File "C:\Python26\lib\lib-tk\Tkinter.py", line 44, in > from turtle import * > File "C:\Python26\lib\lib-tk\turtle.py", line 373, in > class ScrolledCanvas(Tkinter.Frame): > NameError: name 'Tkinter' is not defined Now you have two errors. (1) You have introduced a circular import dependency, where turtle tries to import Tkinter which tries to import Tkinter which tries to import turtle... (2) You have no Tkinter object, since you import it's contents, not the module itself. > I know pretty much what is going on there. I doubt it, or else you wouldn't have done what you did. > But when I try to use: > > import Tkinter > from Tkinter import * Why would you do that when turtle has already imported Tkinter under the name TK? > class ScrolledCanvas(Tkinter.Frame): > > It takes me back to the first error. Which means > in both instances both directly called by me, and > when called from the original turtle.py call, > it's not finding the Frame class. I suspect you've broken it. I recommend you re-install the Tkinter library, including turtle.py, in order to get it back to a known working state. > >From the docs (9.5. Inheritance) it states: > > "The name BaseClassName must be defined in a > scope containing the derived class definition. > In place of a base class name, other arbitrary > expressions are also allowed. This can be useful, > for example, when the base class is defined in another module: > > class DerivedClassName(modname.BaseClassName) > " > > > So why does the above, from turtle.py, a standard module, > not allow this, or is their something > the module writer got wrong, or more likely, that I'm not > understanding about what it's doing? I don't have this problem with an unmodified version of turtle and Tkinter in Python 2.6. I get: >>> import turtle >>> turtle.TK.Frame >>> turtle.ScrolledCanvas > As a sidenote, I ended up removing the from turtle import * > line from Tkinter which resolved the problem(the example I was using > didn't have a canvas, and I'm pretty sure Tkinter was defaulting > to the ScrolledCanvas). I should say so! Tkinter doesn't have a "from turtle import *" line, as that would set up a circular import dependency. No wonder you have had problems, making random changes to libraries without understanding them. -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscri