Re: [Tutor] Help regarding lists, dictionaries and tuples (bob gailer)

2010-11-24 Thread Alan Gauld


Robert Sjöblom robert.sjob...@gmail.com wrote

Why would you want to sum them? You start with 30 points in the 
pool,

then allocate them to the attributes. The sum will still be 30.


Because the user should be able to spend 30 points, or remove points
from an attribute and get them back in the pool. Like so:

attributes { Strength : 0, Health : 0, Wisdom : 0, Dexterity 
: 0)

points = 30 - sum(attributes.values())

Or is there a better way to achieve the same result?


If you write a couple of functions to add/remove points then they can
ensure that the total is correct without the need to sum the values.


attributes { Strength : 0, Health : 0, Wisdom : 0, Dexterity : 
0)

points = 30 - sum(attributes.values())

def addPoints(attribute, number):
   attributes[attribute] += number
   points -= number
   return points

def removePoints(attribute, number):
return addPoints(attribute, -number)

Now you can add and remove the points and the function will
ensure there are only ever 30 points in total use. You would
really need checks to ensure the attribute values and points
never went negative of course... but you need that anyway.

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


Re: [Tutor] if n % 2 == 0 vs. if not n % 2 compared for speed: aesthetics lose

2010-11-24 Thread Richard D. Moores
On Tue, Nov 23, 2010 at 20:09, R. Alan Monroe amon...@columbus.rr.com wrote:

  I've always disliked using if not n % 2  to test for even/odd ints
  because of its convoluted logic. But I ran some speed tests and found
  it was the way to go over if n % 2 == 0.

  Did you try bitwise-and with 1?

 What's that?

 2  1
 0
 3  1
 1

 So what's the connection with the tests I've run?

 I'm wagering it will be faster than a modulo operation. Let us know
 how it turns out :)

You'd win. See http://tutoree7.pastebin.com/QPT9cAEf

My thanks to Emile for the list comprehension suggestion.

Dick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IDEs

2010-11-24 Thread Josep M. Fontana
Great. Thanks Eike and Alan.


Josep M.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Working with interactive Python shell

2010-11-24 Thread Josep M. Fontana
Hi,

When a thread gets too long and new subtopics appear it gets pretty
hard to keep track of what has been said by whom and what has been
answered. Anyway, in one of the threads I started, Steven d'Aprano
gave me a very nice response telling me what his preferred working
environment was. This prompted the following question from me which
was not answered due to the fact that was one of the many subtopics
that emerged from the original question. I'm still interested in
finding out about this in case anybody can give me an answer. I
reproduce my question to Steve:

---
One question for Steve (or for whoever wants to answer): you say you
have a terminal with two tabs (neat, I wonder whether I can get tabs
as well for my terminal in OS X) and when you need to do debugging you
turn to your interactive python terminal and do;

import filename  # first time only
reload(filename)  # all subsequent times

If I do this with a normal python file (filename.py), I get the error:

ImportError: No module named py

This is if I enter the file name with the .py extension. If I just enter the
file name without the extension, everything seems to work fine and I
don't get any error message but then when I call a variable I get a
message saying 'X' is not defined.
--

I'm wondering whether I'm doing something wrong or whether I didn't
properly understand what Steve was saying about working with the
interactive Python shell. If I understood correctly, one can import a
Python script into the interactive shell (not just modules) and then
test different things from the prompt. I've tried to import various
files following Steve's instructions but I get the errors I describe
above.

Josep M.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Working with interactive Python shell

2010-11-24 Thread Evert Rol
snip intro
 
 ---
 One question for Steve (or for whoever wants to answer): you say you
 have a terminal with two tabs (neat, I wonder whether I can get tabs
 as well for my terminal in OS X)

In Terminal.app, just type command-T and you get a new tab. Switch with the 
mouse or command-shift-[  command-shift-]
But sometimes, two separate terminals can be more convenient, so you can see 
both at the same time.

 and when you need to do debugging you
 turn to your interactive python terminal and do;
 
 import filename  # first time only
 reload(filename)  # all subsequent times
 
 If I do this with a normal python file (filename.py), I get the error:
 
 ImportError: No module named py

You're not really showing what exactly you type. That's often more clearer than 
describing what you do, although in this case we can get a pretty good picture 
anyway.

 This is if I enter the file name with the .py extension.

Perhaps try and read through the module section of the standard Python 
tutorial: http://docs.python.org/tutorial/modules.html
The .py extension just signifies the file is a Python file, but the actual 
module name comes before the extension. 
The dot in module names is to divide modules up into submodules, or package 
things (further down in the Python tutorial on modules).

 If I just enter the
 file name without the extension, everything seems to work fine and I
 don't get any error message but then when I call a variable I get a
 message saying 'X' is not defined.

How do you 'call the variable'? 
Btw, you don't really call a variable: access would probably be a more accurate 
term imo. You would call a function though (and that function would then be 
executed).

But if you call a variable that is defined inside the module, you'll need to 
prepend the module name, like:
 import mymodule
 mymodule.X

This is actually a form of namespacing: X is not defined in your global 
environment, just in mymodule.
And again, see the examples in the tutorial.


 --
 
 I'm wondering whether I'm doing something wrong or whether I didn't
 properly understand what Steve was saying about working with the
 interactive Python shell. If I understood correctly, one can import a
 Python script into the interactive shell (not just modules) and then
 test different things from the prompt.

Yes, correct.

 I've tried to import various
 files following Steve's instructions but I get the errors I describe
 above.


Ok, I don't have Steve's instructions handy (they've been stripped of this 
thread now), but I assume that if you apply the above, you should get a bit 
further.
But without actually showing us what you've typed and the errors, we can't give 
you concrete advice (copy-paste is often a very good idea).

Btw, should you want to delve further into using the interactive prompt, there 
are various interactive prompts that provide more conveniences than the 
standard Python prompt. IPython is my favourite, but before you do that, try 
and use the default prompt.

Good luck,

  Evert


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IDEs

2010-11-24 Thread Steven D'Aprano

Alan Gauld wrote:


The basic idea in testing is to try to break your code. Try to think
of every kind of evil input that could possibly come along and see
if your code survives. In amongst all of that you should have a
some valid values too, and know what to expect as out put.


Testing is more than just trying to break code of course. I'm reminded 
of a quote:


I find it amusing when novice programmers believe their main job is
preventing programs from crashing. ... More experienced programmers
realize that correct code is great, code that crashes could use
improvement, but incorrect code that doesn't crash is a horrible
nightmare. -- Chris Smith

Testing is an attempt to ensure that code is *correct* -- that it does 
what it is supposed to do.



There are a number of different types of tests, with different purposes.

Unit tests are for testing that code behaves as expected. If you give 
the function this input, this will be its result. (The result might be 
to return a value, or it might be to raise an exception.) If you have a 
function that's supposed to add two numbers, it's important to be sure 
that it actually, ya know, *adds two numbers*, and not something else. 
(It *almost* adds them, it's just that sometimes the answer is off by 
one or two...)


Doctests are examples that you put into the documentation (usually into 
docstrings of functions and methods). Their primary purpose is to be 
examples for the reader to read, but the secondary aspect is that you 
can run the examples and be sure that they actually work as they are 
supposed to work.


Regression tests are to prevent bugs from re-occurring. For example, 
suppose you have a function spam() and you discover it fails for one 
particular input, aardvark. The first thing you should do, before even 
fixing the bug, is write a test:


assert spam(aardvark) == expected result

This test will fail, because there's a bug in spam(). Now go ahead and 
fix the bug, and the test will then pass. If your code ever has a 
*regression* that returns the bug in spam(), the test will fail again 
and you will immediately notice. Regression tests are to prevent fixed 
bugs from returning.


User Acceptance Tests are mostly relevant when you're building software 
for a client. Both parties need a way to know when the software is 
done. Of course software is never done, but if you're charging $1 
for a software project, you need to have way of saying This is where we 
stop, if you want us to continue, it will cost you more. UATs give both 
parties an objective way of telling that the promised functionality is 
there (e.g. if the user clicks the Stop button, processing must stop 
within one second) and identifying bugs and/or areas that weren't 
specified in enough detail.


Of course, all of these are fuzzy categories -- there's no hard line 
between them.



Test Driven Development tends to focus on the more normal inputs
in my experience, but done properly your test code will usually
be bigger than your production code. In a recent project that we
completed we had 600k lines of production code and over a
million lines of test code.


I can well believe that. I've had a look at a couple of my projects 
(*much* smaller than 600 KLOC!) and I'm averaging about 800 lines in the 
test suite per 1000 lines in the project. That's an over-estimate: the 
1000 lines includes doc tests, so should be counted towards the tests. 
Taking that into account, I get (very roughly) 1:1 ratio of test code to 
production code.




And we still wound up with over 50 reported bugs during Beta test...
But that was much better than the 2000 bugs on an earlier project :-)
But testing is hard.


Maybe so, but nothing beats running your test suite and seeing 
everything pass!




--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Working with interactive Python shell

2010-11-24 Thread Steven D'Aprano

Josep M. Fontana wrote:


One question for Steve (or for whoever wants to answer): you say you
have a terminal with two tabs (neat, I wonder whether I can get tabs
as well for my terminal in OS X) and when you need to do debugging you
turn to your interactive python terminal and do;

import filename  # first time only
reload(filename)  # all subsequent times

If I do this with a normal python file (filename.py), I get the error:

ImportError: No module named py



You're probably writing:

import filename.py

instead of just:

import filename


When you import a module, it doesn't necessarily come from a .py file. 
It could come from a pre-compiled .pyc or .pyo file, or from a Windows 
.pyw file, or a .dll or .so compiled C library, or out of a zip file, to 
mention just a few. There are many different possibilities. So Python 
expects you to just give the module name, filename, without the file 
extension, and it will search for the correct file regardless of the 
extension.


Python uses the dot notation for packages. import filename.py looks 
for a module called py inside a package called filename. Since it 
doesn't find one, it raises ImportError.




This is if I enter the file name with the .py extension. If I just enter the
file name without the extension, everything seems to work fine and I
don't get any error message but then when I call a variable I get a
message saying 'X' is not defined.


Please show the actual line of code you use, and the actual error 
message, in full, copied and pasted, and not retyped from memory, or 
paraphrased, or simplified, or otherwise changed in any way.




--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Working with interactive Python shell

2010-11-24 Thread Josep M. Fontana
Thanks Evert and Steve,

Both of you are right when you say:

 You're not really showing what exactly you type. That's often more clearer 
 than describing what you do, although in this case we can get a pretty good 
 picture anyway.

OK, here's what I do:

import test

I know the shell is importing the file because I can see the following message:

module 'test' from 'test.py'


The file test.py has the following contents (this is a little line of
code I constructed to see how the interactive shell worked importing
the file):

words = 'in the garden on the bank behind the tree'.split()

 How do you 'call the variable'?

Sorry, you are right.  That was a sloppy use of the term 'call'. I
meant to say print the variable.

When I do:

print words

and

print words[3]

In principle I should get:

['in', 'the', 'garden', 'on', 'the', 'bank', 'behind', 'the', 'tree']

and

on

What I do get is:

Traceback (most recent call last):
  File stdin, line 1, in module
NameError: name 'words' is not defined

If the file has the contents that I showed above (and it does),
'words' should be defined, shouldn't it?

By the way Evert, thanks also for the tip on how to get tabs on a Mac
terminal. I didn't know that.


Josep M.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Working with interactive Python shell

2010-11-24 Thread Evert Rol
 You're not really showing what exactly you type. That's often more clearer 
 than describing what you do, although in this case we can get a pretty good 
 picture anyway.
 
 OK, here's what I do:
 
 import test
 
 I know the shell is importing the file because I can see the following 
 message:
 
 module 'test' from 'test.py'
 
 
 The file test.py has the following contents (this is a little line of
 code I constructed to see how the interactive shell worked importing
 the file):
 
 words = 'in the garden on the bank behind the tree'.split()
 
 How do you 'call the variable'?
 
 Sorry, you are right.  That was a sloppy use of the term 'call'. I
 meant to say print the variable.
 
 When I do:
 
 print words
 
 and
 
 print words[3]
 
 In principle I should get:
 
 ['in', 'the', 'garden', 'on', 'the', 'bank', 'behind', 'the', 'tree']
 
 and
 
 on
 
 What I do get is:
 
 Traceback (most recent call last):
  File stdin, line 1, in module
 NameError: name 'words' is not defined
 
 If the file has the contents that I showed above (and it does),
 'words' should be defined, shouldn't it?

Then you haven't read my previous response carefully enough, or I haven't 
phrased it properly. The example I gave was:

 import mymodule
 mymodule.X

where X is defined in a file called mymodule.py
In your case, replace mymodule with test, and X with words.

Also, really, read the tutorial on modules. I know we tutor here, but those 
tutorials are there to explain the most common cases (and the standard Python 
tutorial isn't the only tutorial, but since it comes with Python, it's the one 
I generally point to).
Of course, if something is unclear in the tutorial, let us know!

Cheers,

 Evert

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Working with interactive Python shell

2010-11-24 Thread Josep M. Fontana
On Wed, Nov 24, 2010 at 3:00 PM, Evert Rol evert@gmail.com wrote:


 Then you haven't read my previous response carefully enough, or I haven't 
 phrased it properly. The example I gave was:

 import mymodule
 mymodule.X

 where X is defined in a file called mymodule.py
 In your case, replace mymodule with test, and X with words.

No, you had phrased it perfectly. I just read your answer too fast. My bad.

 Also, really, read the tutorial on modules. I know we tutor here, but those 
 tutorials are there to explain the most common cases (and the standard Python 
 tutorial isn't the only tutorial, but since it comes with Python, it's the 
 one I generally point to).
 Of course, if something is unclear in the tutorial, let us know!

You are right. I didn't read the link because I assumed it would be
general information about modules and I didn't connect that with the
problem I was having using the interactive shell. No I checked it and
saw that, in fact, the relevant information was right there in the
first paragraphs. My apologies. Thanks for your help.


Josep M.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] If os.path.lexists() isn't working properly

2010-11-24 Thread Susana Iraiis Delgado Rodriguez
Hello memebers:

I'm writing a python script to validate if files with extension .prj exist,
if they exist it should write 1 or 0 into an excel cell. I've working to do
this properly, but I'm not getting the results I need. The script doesn't
find all the files, is like the files don't exist but they exist, besides I
think is just reading some of the files that are in C:\Python 26. I really
need a hand on this.

import os, time, socket, pylab
from xlwt import Workbook
from osgeo import ogr,gdal,osr
from dbf import *
#Register GAL drivers
gdal.AllRegister()
#Create an empty list
file_list = []
folders = None
#Code to indicate directory
for root, folders, files in os.walk( C:\\ ):
 file_list.extend(os.path.join(root,fi) for fi in files if
fi.endswith(.shp))
#Open excel book
wrkbk = Workbook()
#Add excel sheet
wksht = wrkbk.add_sheet('shp')
wksht.row(0).write(0,'ruta')
wksht.row(0).write(1,'archivo')
wksht.row(0).write(2,'prj')
for row, filepath in enumerate(file_list, start=1):
wksht.row(row).write(0, filepath)
(ruta, filename) = os.path.split(filepath)
 wksht.row(row).write(1, filename)
 n = os.path.splitext(filename)
 p = n[0]+'.prj'
 if os.path.lexists(p):
  wksht.row(row).write(2, 1)
 else:
  wksht.row(row).write(2, 0)
wrkbk.save('shp3.xls')
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Fw: Installing Pyserial for Python27 on Win 7

2010-11-24 Thread John Smith

Hi, Walter -

Thanks to you, pyserial is installed and imports into Python. Not having 
double backslashes was the latest problem that you got me through.


I am grateful for the support and education you have given me.

Cheers,
John
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] If os.path.lexists() isn't working properly

2010-11-24 Thread Peter Otten
Susana Iraiis Delgado Rodriguez wrote:

 Hello memebers:
 
 I'm writing a python script to validate if files with extension .prj
 exist, if they exist it should write 1 or 0 into an excel cell. I've
 working to do this properly, but I'm not getting the results I need. The
 script doesn't find all the files, is like the files don't exist but they
 exist, besides I think is just reading some of the files that are in
 C:\Python 26. I really need a hand on this.
 
 import os, time, socket, pylab
 from xlwt import Workbook
 from osgeo import ogr,gdal,osr
 from dbf import *
 #Register GAL drivers
 gdal.AllRegister()
 #Create an empty list
 file_list = []
 folders = None
 #Code to indicate directory
 for root, folders, files in os.walk( C:\\ ):
  file_list.extend(os.path.join(root,fi) for fi in files if
 fi.endswith(.shp))
 #Open excel book
 wrkbk = Workbook()
 #Add excel sheet
 wksht = wrkbk.add_sheet('shp')
 wksht.row(0).write(0,'ruta')
 wksht.row(0).write(1,'archivo')
 wksht.row(0).write(2,'prj')
 for row, filepath in enumerate(file_list, start=1):
 wksht.row(row).write(0, filepath)
 (ruta, filename) = os.path.split(filepath)
  wksht.row(row).write(1, filename)
  n = os.path.splitext(filename)
  p = n[0]+'.prj'

Add

   print looking for, p

here. Does it show what you expect/want? In what directory will lexists() 
look for the file?

  if os.path.lexists(p):

lexists()' claim to fame is that it Returns True for broken symbolic 
links. Are you sure you prefer that over good old os.path.exists()?

   wksht.row(row).write(2, 1)
  else:
   wksht.row(row).write(2, 0)
 wrkbk.save('shp3.xls')


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Lambda function, was: Simple counter to determine frequencies of words in adocument

2010-11-24 Thread ALAN GAULD


  Notice that it uses key as a function inside  sorted.
  And lambda creates a function so sorted applies
  the  lambda to each item in turn to retrieve the sort key.
 
 OK. I get it.  Thanks a lot. This should have been clear if I had been
 able to see the code  for the sorted() method. 

OK Good, But I stress the code I posted bears no relation to 
the actual code for sorted! It was purely illustrative.

 Can anyone point me to a URL (or some other  document) 
 where the code for the different built-in methods can be examined? 

The source code for Python, including all the standard modules 
and  the builtin stuff is available on the web site to view or download. 
(The joy of Opensource.) Much of the core code (like sorted) is 
actually written in C but many of the higher level modules are in 
Python - and you probably have it on your PC/Mac if you look 
for the Python modules folder...

The good news is that unlike some opensource projects the code 
quality is pretty good and you can usually figure out how it works 
fairly easily, even the C stuff. (Having said that I only look at the 
source about once a year on average, only if things are getting 
really hairy... :-)

HTH,

Alan G.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] IDEs

2010-11-24 Thread Alan Gauld


Steven D'Aprano st...@pearwood.info wrote

And we still wound up with over 50 reported bugs during Beta 
test...
But that was much better than the 2000 bugs on an earlier project 
:-)

But testing is hard.


Maybe so, but nothing beats running your test suite and seeing 
everything pass!


Yes, I should have added

... but debugging is even harder! :-)

Alan G. 



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] If os.path.lexists() isn't working properly

2010-11-24 Thread Alan Gauld


Susana Iraiis Delgado Rodriguez susana.delgad...@utzmg.edu.mx 
wrote


I'm writing a python script to validate if files with extension .prj 
exist,
if they exist it should write 1 or 0 into an excel cell. I've 
working to do
this properly, but I'm not getting the results I need. The script 
doesn't
find all the files, is like the files don't exist but they exist, 
besides I
think is just reading some of the files that are in C:\Python 26. I 
really

need a hand on this.


I haven't checked the code thoroughly but it looks like you should
maybe put in a print statement to print the root to check it is going
where you think it should.


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


Re: [Tutor] If os.path.lexists() isn't working properly

2010-11-24 Thread Susana Iraiis Delgado Rodriguez
Hello Peter!

I added the line you suggested me and found out that I was just searching
for the filenames without pointing to a specific directory, so Python took
its directory (Python26) as default. After that I need to add a '\' to
separate the path from the filename because it was reading them as a single
string. But finally it worked nicely! here is the corrected script.

import os, time, socket, pylab, fnmatch
from xlwt import Workbook
from osgeo import ogr,gdal,osr
from dbf import *

gdal.AllRegister()
file_list = []
folders = None
for root, folders, files in os.walk( C:\\ ):
 for filename in fnmatch.filter(files, '*.shp'):
   file_list.append(os.path.join(root, filename))
wrkbk = Workbook()
wksht = wrkbk.add_sheet('shp')
wksht.row(0).write(0,'ruta')
wksht.row(0).write(1,'archivo')
wksht.row(0).write(2,'prj')
for row, filepath in enumerate(file_list, start=1):
 wksht.row(row).write(0, filepath)
 (ruta, filename) = os.path.split(filepath)
 wksht.row(row).write(1, filename)
 n = os.path.splitext(filename)
 p = ruta+'\\'+n[0]+'.prj'
#ruta is the variable that saves the pathfile
 print looking for, p
#I changed to os.path.exists()
 if os.path.exists(p):
  wksht.row(row).write(2, 1)
 else:
  wksht.row(row).write(2, 0)
wrkbk.save('shp3.xls')

#Mensajes para mostar en la consola
SEARCH_PATH = os.getcwd()
TARGET_FILE = os.path.realpath('shp3.xls')
print Buscando en, SEARCH_PATH, and writing to, TARGET_FILE
print Encontrando archivos...
print Escribiendo archivo de Excel...
print Listo.

Thank you so much fro your help!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Random Number Question

2010-11-24 Thread Jeff Goodwin
Hello,

I'm trying to find a way to use the random.randint function to generate a
random number, but everytime I run the program it locks up IDLE. Here is
what I have so far:

import random

def main():
 x = input(Enter a number: )
 y = input(Enter a different number: )

 z = random.randint(x,y)

print The random number between , x,  and , y,  is , z

main()

Can you tell me where I'm going wrong here? When I run the program it allows
me to enter the numbers for x and y, then freezes.

Thanks!
Jeff
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Random Number Question

2010-11-24 Thread Adam Bark

On 24/11/10 21:51, Jeff Goodwin wrote:

Hello,
I'm trying to find a way to use the random.randint function to 
generate a random number, but everytime I run the program it locks up 
IDLE. Here is what I have so far:

import random
def main():
 x = input(Enter a number: )
 y = input(Enter a different number: )
 z = random.randint(x,y)
print The random number between , x,  and , y,  is , z
main()
Can you tell me where I'm going wrong here? When I run the program it 
allows me to enter the numbers for x and y, then freezes.


Thanks!
Jeff


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
   
Your print statement isn't indented. It should print the random number 
between and then throw an exception I think. You should either move the 
print statement into the function or have main return x, y, z and print 
the return values of it.


HTH.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Random Number Question

2010-11-24 Thread Jeff Goodwin
Thanks Adam, that was a typo on my part, in the program the print is
actually indented. Any other suggestions?

Thanks again!
Jeff



On Wed, Nov 24, 2010 at 5:00 PM, Adam Bark adam.jt...@gmail.com wrote:

   On 24/11/10 21:51, Jeff Goodwin wrote:

   Hello,

 I'm trying to find a way to use the random.randint function to generate a
 random number, but everytime I run the program it locks up IDLE. Here is
 what I have so far:

 import random

 def main():
  x = input(Enter a number: )
  y = input(Enter a different number: )

  z = random.randint(x,y)

 print The random number between , x,  and , y,  is , z

 main()

 Can you tell me where I'm going wrong here? When I run the program it
 allows me to enter the numbers for x and y, then freezes.

 Thanks!
 Jeff



 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription 
 options:http://mail.python.org/mailman/listinfo/tutor

 Your print statement isn't indented. It should print the random number
 between and then throw an exception I think. You should either move the
 print statement into the function or have main return x, y, z and print the
 return values of it.

 HTH.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Random Number Question

2010-11-24 Thread Jeff Goodwin
Ok, I found the problem, I had saved the file as random.py looks like that
was a no-no. Its working now that I changed the name.

Thanks!
Jeff

On Wed, Nov 24, 2010 at 5:06 PM, Jeff Goodwin jeffbgood...@gmail.comwrote:

 Thanks Adam, that was a typo on my part, in the program the print is
 actually indented. Any other suggestions?

 Thanks again!
 Jeff



   On Wed, Nov 24, 2010 at 5:00 PM, Adam Bark adam.jt...@gmail.com wrote:

   On 24/11/10 21:51, Jeff Goodwin wrote:

   Hello,

 I'm trying to find a way to use the random.randint function to generate a
 random number, but everytime I run the program it locks up IDLE. Here is
 what I have so far:

 import random

 def main():
  x = input(Enter a number: )
  y = input(Enter a different number: )

  z = random.randint(x,y)

 print The random number between , x,  and , y,  is , z

 main()

 Can you tell me where I'm going wrong here? When I run the program it
 allows me to enter the numbers for x and y, then freezes.

 Thanks!
 Jeff



 ___
 Tutor maillist  -  Tutor@python.org
 To unsubscribe or change subscription 
 options:http://mail.python.org/mailman/listinfo/tutor

 Your print statement isn't indented. It should print the random number
 between and then throw an exception I think. You should either move the
 print statement into the function or have main return x, y, z and print the
 return values of it.

 HTH.



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Random Number Question

2010-11-24 Thread Adam Bark

On 24/11/10 22:10, Jeff Goodwin wrote:
Ok, I found the problem, I had saved the file as random.py looks like 
that was a no-no. Its working now that I changed the name.

Thanks!
Jeff

Ah yes always avoid giving your modules names that appear in the 
standard library. It goes wrong, sometimes in unexpected ways.


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Python module structure directories

2010-11-24 Thread Judy Chen

Hi,

I am very new to Python, I worked on C/C++ before.  I would like to know is it 
a good practice to put Python development code under

../src/UI/foo.py
../src/businesslogic/bar.py, etc.

or should we eliminate src' directory since it is not pythonic, or it very 
C/C++ like.

I was told that the above directory/file structure does not apply to
Python, since Python's source code are objects.

What do you say?  Are there any standard for how Python source code to be 
structured?

Thanks a lot and I am looking forward to hearing from you soon.

Best regards,

-- Judy




  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python module structure directories

2010-11-24 Thread Alan Gauld


Judy Chen jgc...@yahoo.com wrote


I am very new to Python, I worked on C/C++ before.
I would like to know is it a good practice to put Python development 
code under


../src/UI/foo.py
../src/businesslogic/bar.py, etc.


Thats fine, especially if its a big project.
src means source code and python is a type of source just as C is.

or should we eliminate src' directory since it is not pythonic, or 
it very C/C++ like.


Who says its not pythonic?
src is a perfectly common name to use on Unix type systems for all 
types

of source code.


I was told that the above directory/file structure does not apply to
Python, since Python's source code are objects.


The source code is not really an object but becaiuse you can import
any python file as a module, and modules are objects (once they are
loaded) you might get away with saying that. But really, python source
files are no different to any other source files when it comes to 
organising

your file system for a project.


Are there any standard for how Python source code to be structured?


Not that I'm aware of. There are some standards for how to create
packages which might restrict things a little but oprovided you have
your PYHONPATH set up tio find the modules all should be well.
And you might want to create a build script that moves the modules
from src to lib in the production file system. But on a project, 
especially

one  with multiple programming languages, having all source files in
one place is a definite plus IMHO.

Another thing to do would be take a look at some of the Python 
projects

on Sourceforge - DIA for example. See how they structure their code.

--
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


Re: [Tutor] Random Number Question

2010-11-24 Thread Alan Gauld


Jeff Goodwin jeffbgood...@gmail.com wrote

Ok, I found the problem, I had saved the file as random.py looks 
like that

was a no-no. Its working now that I changed the name.


Yes that's a bad idea. You probably figured out why, but just in 
case...




import random


It tries to import itself, which then tries to import itself,
which then. infinite loop time...


--
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


Re: [Tutor] Random Number Question

2010-11-24 Thread Jeff Goodwin
Thanks Adam and Alan for responding, I'm very much a non-programmer, but my
14 year old son wants to learn, so I have to learn to teach him...slow
process lol.
Happy Thanksgiving!
Jeff



On Wed, Nov 24, 2010 at 8:27 PM, Alan Gauld alan.ga...@btinternet.comwrote:


 Jeff Goodwin jeffbgood...@gmail.com wrote

  Ok, I found the problem, I had saved the file as random.py looks like
 that
 was a no-no. Its working now that I changed the name.


 Yes that's a bad idea. You probably figured out why, but just in case...


 import random


 It tries to import itself, which then tries to import itself,
 which then. infinite loop time...


 --
 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 maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] If os.path.lexists() isn't working properly

2010-11-24 Thread Dave Angel

On 01/-10/-28163 02:59 PM, Susana Iraiis Delgado Rodriguez wrote:

Hello Peter!

I added the line you suggested me and found out that I was just searching
for the filenames without pointing to a specific directory, so Python took
its directory (Python26) as default. After that I need to add a '\' to
separate the path from the filename because it was reading them as a single
string. But finally it worked nicely! here is the corrected script.

snip
  wksht.row(row).write(1, filename)
  n = os.path.splitext(filename)
  p = ruta+'\\'+n[0]+'.prj'



Use os.path.join() to combine these nodes.  If you use an explicit 
backslash you can trigger two problems:  1) Your code won't be portable 
to other platforms  2) You could have trouble if one of the components 
already has a path separator.



DaveA
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Random Number Question

2010-11-24 Thread Mac Ryan
On Thu, 25 Nov 2010 00:58:23 +
Adam Bark adam.jt...@gmail.com wrote:

 Ah yes always avoid giving your modules names that appear in the 
 standard library. It goes wrong, sometimes in unexpected ways.

I was wondering... apart from checking each name individually, is there
any easy-peasy way to get a list of names used in the standard library
(I am thinking to something like dir()?

Mac.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor