Re: [Tutor] errors in "Python Programming for the Absolute Beginner"??

2011-01-14 Thread Bill Allen
I will agree that it seems odd, but here is a sample run from my system.  I
promise I am not pulling anyone's leg!   :-))

wallenpb@Ubuntu-D810:~$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello world")
hello world
>>> x = input("how many?")
how many?5
>>> x
5
>>>


On Thu, Jan 13, 2011 at 9:31 PM, Corey Richardson  wrote:

> On 01/13/2011 10:29 PM, Bill Allen wrote:
> > That is correct about the difference between Python 2 and Python 3
> > syntax.   However, I am surprised that with 2.7.1 these do not work.   I
> > have found that on my Ubuntu system with Python 2.6.5 these Python 3
> > syntax items do seem to work properly.  I am assuming they were back
> > ported or something.  I would have expected the same for 2.7.1.
> >
> > --Bill
>
> I'm using Python 2.6.6 and I have a feeling you are not using python
> 2.6.5 with Python3 syntax working. I could be very wrong, but just a
> hunch ;)
>
> ~Corey
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] module to parse XMLish text?

2011-01-14 Thread Karim


Hello,

*from xml.etree.ElementTree import ElementTree

_/#Parsing:/_
doc = ElementTree()
doc.parse(xmlFile)
*
/_*#Find tag element:*_/
*doc.find('mytag')*

*_/#iteration over tag element:/_
lname = []
for lib in doc.iter('LibTag'):
 libName = lib.attrib['name']
 lname.append(libName)
*
Regards
Karim

On 01/14/2011 03:55 AM, Terry Carroll wrote:
Does anyone know of a module that can parse out text with XML-like 
tags as in the example below?  I emphasize the "-like" in "XML-like".  
I don't think I can parse this as XML (can I?).


Sample text between the dashed lines::

-
Blah, blah, blah




SOMETHING ELSE
SOMETHING DIFFERENT

-

I'd like to be able to have a dictionary (or any other structure, 
really; as long as I can get to the parsed-out pieces) that would look 
smoothing like:


 {"BING" : "ZEBRA",
  "BANG" : "ROOSTER"
  "BOOM" : "GARBONZO BEAN"
  "BLIP" : "SOMETHING ELSE"
  "BASH" : "SOMETHING DIFFERENT"}

The "Blah, blah, blah" can be tossed away, for all I care.

The basic rule is that the tag either has an operand (e.g., ZEBRA>), in which case the name is the first word and the content is 
everything else that follows in the tag; or else the tag has no 
operand, in which case it is matched to a corresponding closing tag 
(e.g., SOMETHING ELSE), and the content is the material 
between the two tags.


I think I can assume there are no nested tags.

I could write a state machine to do this, I suppose, but life's short, 
and I'd rather not re-invent the wheel, if there's a wheel laying 
around somewhere.


___
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] module to parse XMLish text?

2011-01-14 Thread Stefan Behnel

Terry Carroll, 14.01.2011 03:55:

Does anyone know of a module that can parse out text with XML-like tags as
in the example below? I emphasize the "-like" in "XML-like". I don't think
I can parse this as XML (can I?).

Sample text between the dashed lines::

-
Blah, blah, blah




SOMETHING ELSE
SOMETHING DIFFERENT

-


You can't parse this as XML because it's not XML. The three initial child 
tags are not properly closed.


If the format is really as you describe, i.e. one line per tag, regular 
expressions will work nicely. Something like (untested)


  import re
  parse_tag_and_text = re.compile(
# accept a tag name and then either space+tag or '>'+text+' ]+)(?: ([^>]+)>\s*|>([^<]+)http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] turn a path into nested list

2011-01-14 Thread ingo
On Fri, Jan 14, 2011 at 1:21 AM, Alan Gauld  wrote:

 for t in os.walk('Root'):
>
> ...    print t
> ...
> And the result is:('Root', ['D1', 'D2', 'D3'], ['FA.txt', 'FB.txt'])
> ('Root/D1', ['D1-1'], ['FC.txt'])

As I only need the first result presented I didn't favor os.walk, yet
tried it anyway. Below both versions:

1.
aliaspath = mk_alias(path)
directories = mk_pathlist(aliaspath)
files = []

content = os.listdir(path)
content.sort()
for item in content:
fullpath = os.path.join(path, item)
if os.path.isdir(fullpath):
directories.append((item, mk_aliasURL(fullpath)))
else:
files.append((item, mk_aliasURL(fullpath)))

2.
content = list(os.walk(path).next())
content[0] = mk_alias(content[0])
directories = mk_pathlist(content[0])
directories.extend([(item, mkURL(os.path.join(content[0], item))) for
item in content[1]])
files = [(item, mkURL(os.path.join(content[0], item))) for item in content[2]]


And both with the same output:

 Path
c:\Python25\Lib\site-packages\pygame\examples\macosx\aliens_app_example

 AliasPath
Audio\site-packages\pygame\examples\macosx\aliens_app_example

 Dirs
[('Audio', 'Audio'),
('site-packages', 'Audio/site-packages'),
('pygame', 'Audio/site-packages/pygame'),
('examples', 'Audio/site-packages/pygame/examples'),
('macosx', 'Audio/site-packages/pygame/examples/macosx'),
('aliens_app_example',
'Audio/site-packages/pygame/examples/macosx/aliens_app_example'),
('English.lproj',
'Audio/site-packages/pygame/examples/macosx/aliens_app_example/English.lproj')]

 Files
[('aliens.py', 
'Audio/site-packages/pygame/examples/macosx/aliens_app_example/aliens.py'),
('aliens.pyc', 
'Audio/site-packages/pygame/examples/macosx/aliens_app_example/aliens.pyc'),
('aliens.pyo', 
'Audio/site-packages/pygame/examples/macosx/aliens_app_example/aliens.pyo'),
('README.txt', 
'Audio/site-packages/pygame/examples/macosx/aliens_app_example/README.txt'),
('setup.py', 
'Audio/site-packages/pygame/examples/macosx/aliens_app_example/setup.py'),
('setup.pyc', 
'Audio/site-packages/pygame/examples/macosx/aliens_app_example/setup.pyc'),
('setup.pyo', 
'Audio/site-packages/pygame/examples/macosx/aliens_app_example/setup.pyo')]

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


[Tutor] no luck with sqlinsert

2011-01-14 Thread Tommy Kaas
I get a invalid syntax error when I try to run this script - and it's
con.commit() which is highlighted when I get the error.

I can't see what is wrong. I think it looks like other scripts I'm running
without problems. The scraping part works fine. And the table exists in the
mysql db. I have just separated in an attempt to locate the problem. But no
luck. I hope someone can spot the problem.

(ActivePython 2.6.6. on pc/win)

TIA

 

import urllib2

import MySQLdb

from BeautifulSoup import BeautifulSoup

 

con = MySQLdb.connect(host='mysql2.dicar.dk', user='python1',
passwd='python1', db='python')

cur = con.cursor()

 

sqlinsert = '''

INSERT INTO tkindbtal (kommune, komnr, i2005, i2006, i2007, i2008, i2009,
i2010)

VALUES   (%s,  %s,%s,  %s,   %s, %s, %s,
%s) 

'''

 

soup =
BeautifulSoup(urllib2.urlopen('http://www.kaasogmulvad.dk/unv/python/kom_ind
btal.htm').read())

 

rows = soup.findAll('tr')

print rows

 

for tr in rows[1:]:

cols = tr.findAll('td')

try:

cur.execute(sqlinsert, (cols[0], cols[1], int(cols[2]),
int(cols[3]), int(cols[4]), int(cols[5]), int(cols[6]), int(cols[7]))

con.commit()

except:

con.rollback()

 

con.close()

 

 

Tommy Kaas

 

 

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


[Tutor] Refcount in C extensions

2011-01-14 Thread Izz ad-Din Ruhulessin
Hello,

I am writing a Python C extension and I have some trouble understanding how
reference counting works exactly. Though I think I understand the practice
on simple operations (see my question at stackoverflow:
http://stackoverflow.com/questions/4657764/py-incref-decref-when), but on
more "complex" operations I cannot quite grasp it yet.

For example, in the following helper function, do I need to DECREF or are
all the references automatically destroyed when the function returns?

double Py_GetAttr_DoubleFromFloat(PyObject *obj, const char *attr)
{
if ((PyObject_GetAttrString(obj, attr) == False) ||
(PyObject_HasAttrString(obj, attr) == False)) {
return -.0;
}

return PyFloat_AsDouble(PyNumber_Float(PyObject_GetAttrString(obj, attr)));
}

Please share your thoughts, thanks in advance, kind regards,

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


Re: [Tutor] no luck with sqlinsert

2011-01-14 Thread Jason Staudenmayer
Don't build you sql separate from the execute (or so I was told when I was 
doing something similar)
 
cur.execute(INSERT INTO tkindbtal (kommune, komnr, i2005, i2006, i2007 \
, i2008, i2009, i2010) VALUES (%s,  %s,%s,  %s,   %s, %s,\
%s,  %s) % (cols[0], cols[1], int(cols[2]), int(cols[3]), int(cols[4]), \
int(cols[5]), int(cols[6]), int(cols[7]))



 

Jason
 
 
 
..·><º>

-Original Message-
From: tutor-bounces+jasons=adventureaquarium@python.org 
[mailto:tutor-bounces+jasons=adventureaquarium@python.org] On Behalf Of 
Tommy Kaas
Sent: Friday, January 14, 2011 11:43 AM
To: tutor@python.org
Subject: [Tutor] no luck with sqlinsert



I get a invalid syntax error when I try to run this script - and it's 
con.commit() which is highlighted when I get the error.

I can't see what is wrong. I think it looks like other scripts I'm 
running without problems. The scraping part works fine. And the table exists in 
the mysql db. I have just separated in an attempt to locate the problem. But no 
luck. I hope someone can spot the problem.

(ActivePython 2.6.6. on pc/win)

TIA

 

import urllib2

import MySQLdb

from BeautifulSoup import BeautifulSoup

 

con = MySQLdb.connect(host='mysql2.dicar.dk', user='python1', 
passwd='python1', db='python')

cur = con.cursor()

 

sqlinsert = '''

INSERT INTO tkindbtal (kommune, komnr, i2005, i2006, i2007, i2008, 
i2009, i2010)

VALUES   (%s,  %s,%s,  %s,   %s, %s,
 %s,  %s) 

'''

 

soup = 
BeautifulSoup(urllib2.urlopen('http://www.kaasogmulvad.dk/unv/python/kom_indbtal.htm').read())

 

rows = soup.findAll('tr')

print rows

 

for tr in rows[1:]:

cols = tr.findAll('td')

try:

cur.execute(sqlinsert, (cols[0], cols[1], int(cols[2]), 
int(cols[3]), int(cols[4]), int(cols[5]), int(cols[6]), int(cols[7]))

con.commit()

except:

con.rollback()

 

con.close()

 

 

Tommy Kaas

 

 

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


Re: [Tutor] no luck with sqlinsert

2011-01-14 Thread Alan Gauld


"Tommy Kaas"  wrote


I get a invalid syntax error when I try to run this script - and it's
con.commit() which is highlighted when I get the error.


Aren't you one closing parenthesis short?



   cur.execute(sqlinsert,

  (cols[0],
   cols[1],
   int(cols[2]),
   int(cols[3]),
   int(cols[4]),
   int(cols[5]),
   int(cols[6]),
   int(cols[7])
  )

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] Refcount in C extensions

2011-01-14 Thread Stefan Behnel

Izz ad-Din Ruhulessin, 14.01.2011 17:52:

I am writing a Python C extension and I have some trouble understanding how
reference counting works exactly. Though I think I understand the practice
on simple operations (see my question at stackoverflow:
http://stackoverflow.com/questions/4657764/py-incref-decref-when), but on
more "complex" operations I cannot quite grasp it yet.

For example, in the following helper function, do I need to DECREF or are
all the references automatically destroyed when the function returns?

double Py_GetAttr_DoubleFromFloat(PyObject *obj, const char *attr)
{
if ((PyObject_GetAttrString(obj, attr) == False) ||
(PyObject_HasAttrString(obj, attr) == False)) {
return -.0;
}


This is C, nothing is done automatically. So you need to take care to 
properly DECREF the references. one or two references are leaked in the above.


BTW, what's "False"? Did you mean "Py_False"?



return PyFloat_AsDouble(PyNumber_Float(PyObject_GetAttrString(obj, attr)));


This leaks two references.



Please share your thoughts, thanks in advance, kind regards,


Consider taking a look at Cython. It's an extension language that lets you 
write Python code and generates C code for you. In Cython, your code above 
simply spells


cdef double Py_GetAttr_DoubleFromFloat(obj, attr):
value = getattr(obj, attr, False)
if value is False:
return -.0
return value

Note that I'm using a Python object for 'attr' for performance reasons (and 
for Python 3 portability).


I would expect that the above is at least a bit faster than your code, but 
it handles ref-counting correctly.


Having said that, I'd frown a bit about an API that returns either False or 
a double value ...


Stefan

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


Re: [Tutor] no luck with sqlinsert

2011-01-14 Thread Steve Willoughby

On 14-Jan-11 09:03, Jason Staudenmayer wrote:

Don't build you sql separate from the execute (or so I was told when I
was doing something similar)
cur.execute(INSERT INTO tkindbtal (kommune, komnr, i2005, i2006, i2007 \
, i2008, i2009, i2010) VALUES (%s, %s, %s, %s, %s, %s,\
%s, %s)% (cols[0], cols[1], int(cols[2]), int(cols[3]), int(cols[4]), \
int(cols[5]), int(cols[6]), int(cols[7]))


It's generally bad practice to simply paste string values together to 
include values into SQL, such as the code above (using % formatting).


You need to properly escape the data strings.  Most SQL interfaces in 
Python offer methods for this, either as individual string operation or 
by allowing "place holder" values in the SQL query which discrete values 
are then added to as separate arguments (so the SQL library escapes them 
for you automatically).


The danger is if the data strings include SQL syntax like quotes or 
whatever, it can invalidate your SQL query, making it fail.  A malicious 
user who can feed you those data values can deliberately design them to 
actually do whatever they want to your database.

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


Re: [Tutor] Refcount in C extensions

2011-01-14 Thread Izz ad-Din Ruhulessin
Hi Stefan,

Thanks for your quick reply and clearing the issue up for me. Using your
answer, I rewrote the function to this:

double Py_GetAttr_DoubleFromFloat(PyObject *obj, const char *attr)
>
> {
>
> PyObject *get_attr, *py_float;
>
> int has_attr;
>
>
>> //Check if the given object has the given attribute.
>
> has_attr = PyObject_HasAttrString(obj, attr);
>
> if (has_attr == False) {
>
> return -.0;
>
> }
>
>
>> //Get our attribute and convert it to a double.
>
> get_attr = PyObject_GetAttrString(obj, attr);
>
> py_float = PyNumber_Float(get_attr);
>
> if (py_float == NULL) {
>
> Py_DECREF(get_attr);
>
> Py_XDECREF(py_float);
>
> return -.0;
>
> }
>
> double output = PyFloat_AsDouble(py_float);
>
>
>> //Garbage collect
>
> Py_DECREF(get_attr);
>
> Py_XDECREF(py_float);
>
>
>> return output;
>
> }
>
>
(False is 0)

Regarding your Cython suggestion, as a matter of coincidence I have been
reading about it in the past few days. I'm in doubt of using it however,
because I have a lot of native C code that would require rewriting if I
switched to Cython. On the other hand, your example shows that such a
one-time rewrite will pay-off big time in future development speed.

Kind regards,

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


Re: [Tutor] Refcount in C extensions

2011-01-14 Thread Stefan Behnel

Izz ad-Din Ruhulessin, 14.01.2011 19:49:

Thanks for your quick reply and clearing the issue up for me. Using your
answer, I rewrote the function to this:

double Py_GetAttr_DoubleFromFloat(PyObject *obj, const char *attr)


{

PyObject *get_attr, *py_float;

int has_attr;



//Check if the given object has the given attribute.


has_attr = PyObject_HasAttrString(obj, attr);

if (has_attr == False) {

return -.0;

}



//Get our attribute and convert it to a double.


get_attr = PyObject_GetAttrString(obj, attr);


Note that HasAttr() calls GetAttr() internally, so it's actually faster to 
call GetAttr() and check for an exception (and clear it). That's basically 
how HasAttr() works, except that it doesn't tell you the result if the 
attribute existed.




py_float = PyNumber_Float(get_attr);

if (py_float == NULL) {

Py_DECREF(get_attr);

Py_XDECREF(py_float);


You already know that py_float is NULL, so Py_XDECREF() is a no-op.



return -.0;

}

double output = PyFloat_AsDouble(py_float);



//Garbage collect


Py_DECREF(get_attr);

Py_XDECREF(py_float);


py_float cannot be NULL at this point, so the Py_XDECREF() will compile 
into Py_DECREF().




(False is 0)


In that case, better write 0 instead.



Regarding your Cython suggestion, as a matter of coincidence I have been
reading about it in the past few days. I'm in doubt of using it however,
because I have a lot of native C code that would require rewriting if I
switched to Cython.


No need for that, Cython can call external C code natively. So you can make 
the switch step by step.




On the other hand, your example shows that such a
one-time rewrite will pay-off big time in future development speed.


It usually does, yes. It often even pays off immediately because a rewrite 
tends to be pretty straight forward (basically, read and understand the C 
code, rip out all low-level stuff and replace the rest with simpler code), 
and while doing so, some bugs tend to disappear, the code becomes simpler, 
safer and often also faster, and new features appear while you're at it.


Stefan

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


[Tutor] Object/Class Beginner Questions

2011-01-14 Thread Ben Ganzfried
Hey guys,

I'm using a tutorial geared for a 2.x version of Python and I am currently
using Python 3.1-- so it is possible that my confusion has to do with
different notations between them.  But in any case, here is what I have:

>>> type(Time)

>>> t1 = Time()
>>> type(t1)


where:

class Time:
def __init__(self, hours = 0, minutes = 0, seconds = 0):
   self.hours = hours
   self.minutes = minutes
   self.seconds = seconds

def print_time(t1):
print(t.hours, ":", t.minutes, ":", t.seconds)

Now the book I am working with has the following example:

>>> type(Point)

>>> p = Point()
>>> type(p)


My questions are the following:
1) Why is the type for my class Time : >>> type(Time)
  
when the type for their class Point is: 
Also, what is the difference between "class" and "classobj" in this case?
2) Why does my t1 object give the following as its type: 
And in their p object example the type is: ?
3) What is happening such that when I try to call my print_time(t1) function
I get the following error:
>>> t1 = Time()
>>> t1.hours = 3
>>> t1.minutes = 30
>>> t1.seconds = 45
>>> print_time(t1)
Traceback (most recent call last):
  File "", line 1, in 
print_time(t1)
NameError: name 'print_time' is not defined


Thank you very much.

Sincerely,

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


Re: [Tutor] Object/Class Beginner Questions

2011-01-14 Thread Alex Hall
On 1/14/11, Ben Ganzfried  wrote:
> Hey guys,
>
> I'm using a tutorial geared for a 2.x version of Python and I am currently
> using Python 3.1-- so it is possible that my confusion has to do with
> different notations between them.  But in any case, here is what I have:
>
 type(Time)
> 
 t1 = Time()
 type(t1)
> 
>
> where:
>
> class Time:
> def __init__(self, hours = 0, minutes = 0, seconds = 0):
>self.hours = hours
>self.minutes = minutes
>self.seconds = seconds
>
> def print_time(t1):
> print(t.hours, ":", t.minutes, ":", t.seconds)
>
> Now the book I am working with has the following example:
>
 type(Point)
> 
 p = Point()
 type(p)
> 
>
> My questions are the following:
> 1) Why is the type for my class Time : >>> type(Time)
>   
> when the type for their class Point is: 
> Also, what is the difference between "class" and "classobj" in this case?
I am not sure as I thought something changed about this in 3.x, but try:
class time(object)
instead of simply
class time
and see what happens.
> 2) Why does my t1 object give the following as its type:  '__main__.Time'>
> And in their p object example the type is: ?
I am not sure, and am curious to see the answer as well.
> 3) What is happening such that when I try to call my print_time(t1) function
> I get the following error:
 t1 = Time()
 t1.hours = 3
 t1.minutes = 30
 t1.seconds = 45
 print_time(t1)
> Traceback (most recent call last):
>   File "", line 1, in 
> print_time(t1)
> NameError: name 'print_time' is not defined
You made a time object. This means that everything about the object
(class) is accessed through the dot notation. Currently, you are
calling print_time(t1) where t1 is an object. You have to, as they
say, call print_time on an instance of the time class. You have an
instance, t1 (an instance is just a variable representing the class),
so call print_time on the instance:
t1.print_time()
HTH.
>
>
> Thank you very much.
>
> Sincerely,
>
> Ben
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Object/Class Beginner Questions

2011-01-14 Thread Steven D'Aprano

Ben Ganzfried wrote:

Hey guys,

I'm using a tutorial geared for a 2.x version of Python and I am currently
using Python 3.1-- so it is possible that my confusion has to do with
different notations between them.  But in any case, here is what I have:



My questions are the following:
1) Why is the type for my class Time : >>> type(Time)
  
when the type for their class Point is: 


In Python 2.x, objects are divided into two broad families of objects:

1. "classic classes" and their instances;
2. "new-style" types and their instances (including built-ins like str, 
int, float, and similar).


There are some small, but significant differences between them, but in 
general they do mostly the same kind of things.


In Python 2.x, a class definition like:

class Point:
...

defines a classic class (or "old style class"), while inheriting from 
"object" or a built-in type defines a new-style class:


class Point(object):  # Tells Python to construct a new-style class
...

The type of an instance is the instance's class; the type of a class 
object is "classobj" for old-style classes and (usually) "type" for 
new-style classes and types. Calling type() on old-style classes returns 
, and on new-style, .


But in Python 3, old-style classic classes are gone. Point will be a 
"type" object, which is what you are seeing.




Also, what is the difference between "class" and "classobj" in this case?


Every object has a type, even classes. `class` is the keyword that you 
use for creating new classes, and type() is what you use to find out 
what they are. So:


type(42) -> int  # 42 is an int
type(int) -> type  # int is a new-style class, or "type"

In Python 2.x, the type of a classic class is called "classobj", and the 
type of a new-style class is called "type".




2) Why does my t1 object give the following as its type: 
And in their p object example the type is: ?


Again, this boils down to old-style vs. new-style.



3) What is happening such that when I try to call my print_time(t1) function
I get the following error:

t1 = Time()
t1.hours = 3
t1.minutes = 30
t1.seconds = 45
print_time(t1)

Traceback (most recent call last):
  File "", line 1, in 
print_time(t1)
NameError: name 'print_time' is not defined


You don't have a print_time function, as Python correctly reports. You 
have a print_time *method*, which means it lives inside Time objects. 
You can't access it directly as a global function:


print_time(something)

does not work, because print_time doesn't live in the global namespace. 
You need to call it as a method of a Time instance:


t1.print_time()  # no additional arguments needed



--
Steven

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


Re: [Tutor] Object/Class Beginner Questions

2011-01-14 Thread Ben Ganzfried
I actually just figured it out (since the tutorial talks about the
difference in indentation between a method and a function in a later
chapter).  Basically, a method is within a class and therefore cannot be
called from the command prompt whereas a function that stands by itself in a
script can be called from the command prompt.

Although if this isn't quite right or there's more to it, I would still
definitely appreciate any advice you have.

Thanks again,

Ben

On Fri, Jan 14, 2011 at 1:53 PM, Ben Ganzfried wrote:

> Hey guys,
>
> I'm using a tutorial geared for a 2.x version of Python and I am currently
> using Python 3.1-- so it is possible that my confusion has to do with
> different notations between them.  But in any case, here is what I have:
>
> >>> type(Time)
> 
> >>> t1 = Time()
> >>> type(t1)
> 
>
> where:
>
> class Time:
> def __init__(self, hours = 0, minutes = 0, seconds = 0):
>self.hours = hours
>self.minutes = minutes
>self.seconds = seconds
>
> def print_time(t1):
> print(t.hours, ":", t.minutes, ":", t.seconds)
>
> Now the book I am working with has the following example:
>
> >>> type(Point)
> 
> >>> p = Point()
> >>> type(p)
> 
>
> My questions are the following:
> 1) Why is the type for my class Time : >>> type(Time)
>'type'>
> when the type for their class Point is: 
> Also, what is the difference between "class" and "classobj" in this case?
> 2) Why does my t1 object give the following as its type:  '__main__.Time'>
> And in their p object example the type is: ?
> 3) What is happening such that when I try to call my print_time(t1)
> function I get the following error:
> >>> t1 = Time()
> >>> t1.hours = 3
> >>> t1.minutes = 30
> >>> t1.seconds = 45
> >>> print_time(t1)
> Traceback (most recent call last):
>   File "", line 1, in 
> print_time(t1)
> NameError: name 'print_time' is not defined
>
>
> Thank you very much.
>
> Sincerely,
>
> Ben
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Object/Class Beginner Questions

2011-01-14 Thread Ben Ganzfried
* I meant that*: A method actually can be called from the command prompt,
but the syntax is quite different than that used to call a function from the
command prompt.

On Fri, Jan 14, 2011 at 2:37 PM, Ben Ganzfried wrote:

> I actually just figured it out (since the tutorial talks about the
> difference in indentation between a method and a function in a later
> chapter).  Basically, a method is within a class and therefore cannot be
> called from the command prompt whereas a function that stands by itself in a
> script can be called from the command prompt.
>
> Although if this isn't quite right or there's more to it, I would still
> definitely appreciate any advice you have.
>
> Thanks again,
>
> Ben
>
>
> On Fri, Jan 14, 2011 at 1:53 PM, Ben Ganzfried wrote:
>
>> Hey guys,
>>
>> I'm using a tutorial geared for a 2.x version of Python and I am currently
>> using Python 3.1-- so it is possible that my confusion has to do with
>> different notations between them.  But in any case, here is what I have:
>>
>> >>> type(Time)
>> 
>> >>> t1 = Time()
>> >>> type(t1)
>> 
>>
>> where:
>>
>> class Time:
>> def __init__(self, hours = 0, minutes = 0, seconds = 0):
>>self.hours = hours
>>self.minutes = minutes
>>self.seconds = seconds
>>
>> def print_time(t1):
>> print(t.hours, ":", t.minutes, ":", t.seconds)
>>
>> Now the book I am working with has the following example:
>>
>> >>> type(Point)
>> 
>> >>> p = Point()
>>
>> >>> type(p)
>> 
>>
>> My questions are the following:
>> 1) Why is the type for my class Time : >>> type(Time)
>>   > 'type'>
>> when the type for their class Point is: 
>> Also, what is the difference between "class" and "classobj" in this case?
>> 2) Why does my t1 object give the following as its type: > '__main__.Time'>
>> And in their p object example the type is: ?
>> 3) What is happening such that when I try to call my print_time(t1)
>> function I get the following error:
>> >>> t1 = Time()
>> >>> t1.hours = 3
>> >>> t1.minutes = 30
>> >>> t1.seconds = 45
>> >>> print_time(t1)
>> Traceback (most recent call last):
>>   File "", line 1, in 
>> print_time(t1)
>> NameError: name 'print_time' is not defined
>>
>>
>> Thank you very much.
>>
>> Sincerely,
>>
>> Ben
>>
>>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] module to parse XMLish text?

2011-01-14 Thread Terry Carroll

On Fri, 14 Jan 2011, Stefan Behnel wrote:


Terry Carroll, 14.01.2011 03:55:

Does anyone know of a module that can parse out text with XML-like tags as
in the example below? I emphasize the "-like" in "XML-like". I don't think
I can parse this as XML (can I?).

Sample text between the dashed lines::

-
Blah, blah, blah




SOMETHING ELSE
SOMETHING DIFFERENT

-


You can't parse this as XML because it's not XML. The three initial child 
tags are not properly closed.


Yeah, that's what I figured.

If the format is really as you describe, i.e. one line per tag, regular 
expressions will work nicely.


Now there's an idea!  I hadn't thought of using regexs, probably because 
I'm terrible at all but the most simple ones.


As it happens, I'm only interested in four of the tags' contents, so I
could probably manage to write a seried of regexes that even I could 
maintain, one for each of the pieces of data I want to extract; if I try 
to write a grand unified regex, I'm bound to shoot myself in the foot.


Thanks very much.

On Fri, 14 Jan 2011, Karim wrote:


from xml.etree.ElementTree import ElementTree


I don't think straight XML parsing will work on this, as it's not valid 
XML; it just looks XML-like enough to cause confusion.

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


[Tutor] how to print random number multiply

2011-01-14 Thread walter weston

I have mostly studied python and now I'm ready to start writing code. I want to 
print random numbers a certain ammount of times I am using the code   

 import random
print (random.random())

I tried bind the print statement to a variable and when I call x for example I 
want it to print new numbers when I store the print statement in a variable x 
everytime I call x it's the same number and does change I wanted it to change 
when I call it could someone show me the code to do that thanks!
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] color of "print" function

2011-01-14 Thread Bill DeBroglie

Hello all,

I don't have a problem per se, but have noticed something that I'd  
like to figure out...


Sometimes the "print" function appears orange for me, sometimes it  
appears purple. Why does this happen and what's the difference anyway?  
This seems to be the only function that varies like this, but I'm just  
starting really so perhaps I'll come across more.


The coloring seems to be fairly arbitrary. At least, I haven't noticed  
a pattern yet.


Using Mac OS X 10.5.8 and Python 2.7.1

Thank you for the help,
bdb
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to print random number multiply

2011-01-14 Thread Corey Richardson
On 01/14/2011 07:46 PM, walter weston wrote:
> I have mostly studied python and now I'm ready to start writing code. I
> want to print random numbers a certain ammount of times I am using the
> code  
> 
>  import random
> print (random.random())
> 
> I tried bind the print statement to a variable and when I call x for
> example I want it to print new numbers when I store the print statement
> in a variable x everytime I call x it's the same number and does change
> I wanted it to change when I call it could someone show me the code to
> do that thanks!
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
When you do:
x = print(random.random())
that evaluates random.random() once, and not every time you type in x.
What you want it is for loop:

for number in range(50):
print(random.random())

Coming from other languages you might think:
cntr = 0
while cntr < 50:
print(random.random())
cntr += 1

which is correct, but not pythonic.

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


Re: [Tutor] how to print random number multiply

2011-01-14 Thread David Hutto
>  import random
for x in range(1,10)

> print (random.random())


-- 
Sometimes...my mama...says I get over excited about technology.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to print random number multiply

2011-01-14 Thread Steven D'Aprano

walter weston wrote:
I have mostly studied python and now I'm ready to start writing code. I want to print random numbers a certain ammount of times I am using the code   


 import random
print (random.random())

I tried bind the print statement to a variable and when I call x for example I 
want it to print new numbers when I store the print statement in a variable x 
everytime I call x it's the same number and does change I wanted it to change 
when I call it could someone show me the code to do that thanks!


I'm sorry, I don't understand this. Can you try explaining more clearly 
what you want to do?




--
Steven

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


Re: [Tutor] color of "print" function

2011-01-14 Thread Corey Richardson
On 01/14/2011 07:48 PM, Bill DeBroglie wrote:
> Hello all,
> 
> I don't have a problem per se, but have noticed something that I'd  
> like to figure out...
> 
> Sometimes the "print" function appears orange for me, sometimes it  
> appears purple. Why does this happen and what's the difference anyway?  
> This seems to be the only function that varies like this, but I'm just  
> starting really so perhaps I'll come across more.
> 
> The coloring seems to be fairly arbitrary. At least, I haven't noticed  
> a pattern yet.
> 
> Using Mac OS X 10.5.8 and Python 2.7.1
> 
> Thank you for the help,
> bdb
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Is that specifically with Idle, or some other IDE? AFAIK the interpreter
has no coloring, unless you're using something like bpython or ipython.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] color of "print" function

2011-01-14 Thread Steven D'Aprano

Bill DeBroglie wrote:

Hello all,

I don't have a problem per se, but have noticed something that I'd like 
to figure out...


Sometimes the "print" function appears orange for me, sometimes it 
appears purple. Why does this happen and what's the difference anyway? 


Can you explain the context? Do you mean in your editor? If so, perhaps 
you're using two different editors, and one is configured to show print 
in purple and the other orange.



--
Steven

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


Re: [Tutor] how to print random number multiply

2011-01-14 Thread David Hutto
Sorry , the tab button doesn't work for text in google mail, and it
jumped to send

>>

  import random
> for x in range(1,10):
print (random.random())

This assigns a new random each time, where as outside the for loop it
assigns it once.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] color of "print" function

2011-01-14 Thread bob gailer

On 1/14/2011 7:48 PM, Bill DeBroglie wrote:

Hello all,

I don't have a problem per se, but have noticed something that I'd 
like to figure out...


Sometimes the "print" function appears orange for me, sometimes it 
appears purple. Why does this happen and what's the difference anyway? 
This seems to be the only function that varies like this, but I'm just 
starting really so perhaps I'll come across more.


The coloring seems to be fairly arbitrary. At least, I haven't noticed 
a pattern yet.


Using Mac OS X 10.5.8 and Python 2.7.1


I'm stumped. Could it be the glasses you are wearing.

Or could you tell us just a bit more about your situation - such as 
WHERE does this happen? An editor, IDLE, what?


--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] how to print random number multiply

2011-01-14 Thread bob gailer

On 1/14/2011 7:46 PM, walter weston wrote:
I have mostly studied python and now I'm ready to start writing code. 
I want to print random numbers a certain ammount of times I am using 
the code


 import random
print (random.random())

I tried bind the print statement to a variable and when I call x for 
example I want it to print new numbers when I store the print 
statement in a variable x everytime I call x it's the same number and 
does change I wanted it to change when I call it could someone show me 
the code to do that thanks!

Sorry - but we are not mind readers.

Post the code you wrote, so we have some idea of what you mean by "bind" 
and "call".


I could guess but you should learn to provide sufficient information so 
we don't have to.


--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] color of "print" function

2011-01-14 Thread Alan Gauld


"Bill DeBroglie"  wrote

Sometimes the "print" function appears orange for me, sometimes it 
appears purple. Why does this happen and what's the difference 
anyway?


THere is probably no difference but one of several things could be 
causing it.


First though, the colouring is done by your editing tool not Python. 
So
a lot depends on the tool you use. If you use different editors then 
you

can expect to see different colours - reason 1.

The colouring is derived from the context so if your print command
is accidentally put inside a string it will be coloured the same as 
the

string - uindicating an error! - Reason 2.

You may have mismatched parentheses somewhere which
may cause the editor to consider your print as a value,
hence a different colour - Reason 3.

Some editors are sensitive to their context to the point of allowing
different colour schemes inside class definitions from out, maybe
yours does too. Reason 4

And there are probably others...

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] how to print random number multiply

2011-01-14 Thread David Hutto
On Fri, Jan 14, 2011 at 8:01 PM, David Hutto  wrote:
> Sorry , the tab button doesn't work for text in google mail, and it
> jumped to send
>
>>>
>
>   import random
>> for x in range(1,10):
 num = random.random()
>       print (num)

this should 'bind' your variable to a knew random each time

>
> This assigns a new random each time, where as outside the for loop it
> assigns it once.
>



-- 
Sometimes...my mama...says I get over excited about technology.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to print random number multiply

2011-01-14 Thread Alan Gauld


"walter weston"  wrote


I have mostly studied python and now I'm ready to
start writing code.


Its usually best to study programming languages *by* writing code.
However since you have now reached that stage of readiness
that's irrelevant advice :-)


I want to print random numbers a certain ammount of times
I am using the code

import random
print (random.random())


print just prints once.
It returns None so if you try to assign print to a variable it will 
store None



I tried bind the print statement to a variable and when
I call x for example I want it to print new numbers when
I store the print statement in a variable x everytime I
call x it's the same number and does change I wanted
it to change when I call it


Since we are not psychic it would be more usefuil to send your actual
code and output so that we can see exactly what you did and
what Python produced.

It would also be helpful to tell us which Python version you are using
(and OS too).

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] Python on Ubuntu 10.10?

2011-01-14 Thread Joel Knoll

Hello,

I am new to programming and to Python.  I've been using Python with IDLE on 
Windows Vista for a few weeks now.  
(And I'm loving it!)  However, I'm thinking about switching to Ubuntu 10.10.  
If I download Ubuntu, will I still be able to use the 
IDLE environment?  I am really quite fond of it (mostly because it's what I 
know!).  To use Python in Ubuntu,
will I have to do any additional downloading, or do Python and IDLE come built 
in and ready to use?  

Thanks for your time,

JC Knoll


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


Re: [Tutor] color of "print" function

2011-01-14 Thread Bill DeBroglie


On Jan 14, 2011, at 8:03 PM, bob gailer wrote:


On 1/14/2011 7:48 PM, Bill DeBroglie wrote:

Hello all,

I don't have a problem per se, but have noticed something that I'd  
like to figure out...


Sometimes the "print" function appears orange for me, sometimes it  
appears purple. Why does this happen and what's the difference  
anyway? This seems to be the only function that varies like this,  
but I'm just starting really so perhaps I'll come across more.


The coloring seems to be fairly arbitrary. At least, I haven't  
noticed a pattern yet.


Using Mac OS X 10.5.8 and Python 2.7.1


I'm stumped. Could it be the glasses you are wearing.

Or could you tell us just a bit more about your situation - such as  
WHERE does this happen? An editor, IDLE, what?


--
Bob Gailer
919-636-4239
Chapel Hill NC



It happens in IDLE. If I change the spacing (delete an empty line say)  
the color will change.  Experimenting a little bit, the default seems  
to be orange. When I indent at all, it changes to purple. However,  
occasionally, it's purple without being indented (can't figure out  
anything in particular that causes this), though in every instance  
where this happens, I can change it back to orange by manipulating the  
spacing (adding an empty line, and then deleting it seems to do the  
trick).


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


Re: [Tutor] Python on Ubuntu 10.10?

2011-01-14 Thread Corey Richardson
On 01/14/2011 08:17 PM, Joel Knoll wrote:
> Hello,
> 
> I am new to programming and to Python.  I've been using Python with IDLE
> on Windows Vista for a few weeks now. 
> (And I'm loving it!)  However, I'm thinking about switching to Ubuntu
> 10.10.  If I download Ubuntu, will I still be able to use the
> IDLE environment?  I am really quite fond of it (mostly because it's
> what I know!).  To use Python in Ubuntu,
> will I have to do any additional downloading, or do Python and IDLE come
> built in and ready to use? 
> 
> Thanks for your time,
> 
> JC Knoll
> 
> 
> 
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

Python (2.6.6) comes default with Ubuntu IIRC. You'll need to:
sudo apt-get install idle
to download and install Idle, but it does all the lifting for you.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how come this doesnt work

2011-01-14 Thread walter weston

I generate a random number(supposedly a password, in my case its just a long 
floating point lol),I want the user to reinput that number and I want to print 
a string if the number entered is correct. so if m==num(num is the number 
generated and m is the variable which stores the input ) then I want to print 
'you entered correctly, proceed'.

here is my code..

import random
for x in range(0,1):
num = random.random()
print (num)
m=input('input pass:')
if m==num:
print('you entered correctly, proceed')

It's very noobish dont give me complex replys, and dont be to rude or laugh at 
me, as I am a beginner In the programming domain.
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] module to parse XMLish text?

2011-01-14 Thread Wayne Werner
On Fri, Jan 14, 2011 at 4:42 PM, Terry Carroll  wrote:

> 
>
> On Fri, 14 Jan 2011, Karim wrote:
>
>  from xml.etree.ElementTree import ElementTree
>>
>
> I don't think straight XML parsing will work on this, as it's not valid
> XML; it just looks XML-like enough to cause confusion.
>
>
It's worth trying out - most (good) parsers "do the right thing" even when
they don't have strictly valid code. I don't know if xml.etree is one, but
I'm fairly sure both lxml and BeautifulSoup would probably parse it
correctly. Only one way to find out ;)

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


Re: [Tutor] how come this doesnt work

2011-01-14 Thread Alex Hall
On 1/14/11, walter weston  wrote:
>
> I generate a random number(supposedly a password, in my case its just a long
> floating point lol),I want the user to reinput that number and I want to
> print a string if the number entered is correct. so if m==num(num is the
> number generated and m is the variable which stores the input ) then I want
> to print 'you entered correctly, proceed'.
>
> here is my code..
>
> import random
> for x in range(0,1):
Note that you could just say: for x in range(1) since range() starts
at 0 automatically. Note, too, that this will run once, so you really
do not need a for loop at all.
> num = random.random()
> print (num)
> m=input('input pass:')
"m" is currently a string since it is what is returned by input. Your
num, though, is an integer...
> if m==num:
> print('you entered correctly, proceed')
I assume the problem is that it never prints? If not, this is because
m is a string while num is an integer (int for short). Add:
m=int(m)
just before the if statement. This causes m to turn from a string into
an integer and is what is known as "casting" or "type casting", if I
have my vocabulary correct. Essentially, while you see a number in
both m and num, Python sees two very different binary values and so
they are never equal.

Here goes a very odd example, but it is all I can come up with at the
moment. Imagine two boxes, each the same size and each the same color.
If you weigh them, though, you find one to be much heavier than the
other; they look the same, but they are not. Casting is like adding
weight to the lighter box until it equals the weight of the heavier
box; now they *are* equal, with the same dimensions, color, and
weight. I told you it would not be a great example.
HTH.
>
> It's very noobish dont give me complex replys, and dont be to rude or laugh
> at me, as I am a beginner In the programming domain.
>   


-- 
Have a great day,
Alex (msg sent from GMail website)
mehg...@gmail.com; http://www.facebook.com/mehgcap
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how come this doesnt work

2011-01-14 Thread Noah Hall
> import random
> for x in range(0,1):
>     num = random.random()
>     print (num)
>     m=input('input pass:')
>     if m==num:
>     print('you entered correctly, proceed')


Your problem lines in the differences in between the types - your num
variable is a float, whereas your m variable is a string. In order to
compare them here, you need to either use the float() function or the
str() function to convert one to a comparable data type, for example -

if m==str(num):
print('you entered correctly, proceed')

Might I also note that your for loop does nothing except run once, and
the 0 is unnecessary.

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


Re: [Tutor] how come this doesnt work

2011-01-14 Thread Noah Hall
Adding reply to list -

On Sat, Jan 15, 2011 at 2:55 AM, walter weston  wrote:
> I only want to generate a random number once
>

Then you don't need a for loop. Think of a for loop as something you
need when you want to run a piece of code several times, for example

for x in range(1,6):
   print('This is message number %d' % (x))

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


Re: [Tutor] When I print random.random

2011-01-14 Thread bob gailer

On 1/14/2011 10:02 PM, walter weston wrote:
when I print random.random() it always returns a float why is this? 
how do I change it to a whole number?


That depends on what behavior you want.

If you RTFM you will see:

random.random()
   Return the next random floating point number in the range [0.0, 1.0).

The Python tool for converting float to integer is the int function.

However int(random.random() ) will always return 0.

In what range do you want the integers to fall?

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] how come this doesnt work

2011-01-14 Thread Dave Angel

On 01/-10/-28163 02:59 PM, walter weston wrote:


I generate a random number(supposedly a password, in my case its just a long 
floating point lol),I want the user to reinput that number and I want to print 
a string if the number entered is correct. so if m==num(num is the number 
generated and m is the variable which stores the input ) then I want to print 
'you entered correctly, proceed'.

here is my code..

import random
for x in range(0,1):
 num = random.random()
 print (num)
 m=input('input pass:')
 if m==num:
 print('you entered correctly, proceed')

It's very noobish dont give me complex replys, and dont be to rude or laugh at 
me, as I am a beginner In the programming domain.



You left out a few details, like what version of Python you're using. 
However, i suspect you're using version 3.x, and will respond accordingly.


input() returns a string, while random.random() returns a float.  So 
they'll never be equal.


Because floats are stored in binary floating point, you run a risk in 
comparing them in any case.  So rather than converting the user's input 
to float, I'd suggest converting the random value to a string.  And if 
you do it before printing it, your user should be able to get an exact 
match every time.


Just change the num assignment to:

num = str(random.random())

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


Re: [Tutor] When I print random.random

2011-01-14 Thread Wayne Werner
On Fri, Jan 14, 2011 at 9:25 PM, bob gailer  wrote:

>  On 1/14/2011 10:02 PM, walter weston wrote:
>
> when I print random.random() it always returns a float why is this? how do
> I change it to a whole number?
>
>
> That depends on what behavior you want.
>
> If you RTFM you will see:
>  random.random() Return the next random floating point number in the range
> [0.0, 1.0). The Python tool for converting float to integer is the int
> function.
>
> However int(random.random() ) will always return 0.
>
> In what range do you want the integers to fall?
>

The documentation in general is a fine source for learning new
functionality: http://docs.python.org/library/random.html

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


Re: [Tutor] When I print random.random

2011-01-14 Thread Steven D'Aprano

walter weston wrote:

when I print random.random() it always returns a float why is this? how do I 
change it to a whole number?


Because random.random() is defined to always return a float between 0 
and 1. That's what it does.


If you want a random whole number, you can call random.randint or 
random.randrange. See the Fine Manual for the difference between them, 
or at the interactive interpreter, call:


help(random.randint)
help(random.randrange)

and read what they say. Then if you still have any questions, ask.



--
Steven

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


Re: [Tutor] When I print random.random

2011-01-14 Thread bob gailer

Please always reply to the list, not just to me.

On 1/14/2011 10:42 PM, walter weston wrote:
I want a whole number thats 5 digits long, I want to use it as a 
password and if I just type random.random() I get a long float.


random.randint(a, b)
Return a random integer N such that a <= N <= b.


--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] module to parse XMLish text?

2011-01-14 Thread Stefan Behnel

Wayne Werner, 15.01.2011 03:25:

On Fri, Jan 14, 2011 at 4:42 PM, Terry Carroll wrote:

On Fri, 14 Jan 2011, Karim wrote:

  from xml.etree.ElementTree import ElementTree

I don't think straight XML parsing will work on this, as it's not valid
XML; it just looks XML-like enough to cause confusion.


It's worth trying out - most (good) parsers "do the right thing" even when
they don't have strictly valid code. I don't know if xml.etree is one, but
I'm fairly sure both lxml and BeautifulSoup would probably parse it
correctly.


They wouldn't. For the first tags, the text values would either not come 
out at all or they would be read as attributes and thus loose their order 
and potentially their whitespace as well. The other tags would likely get 
parsed properly, but the parser may end up nesting them as it hasn't found 
a closing tag for the previous tags yet.


So, in any case, you'd end up with data loss and/or a structure that would 
be much harder to handle than the (relatively) simple file structure.


Stefan

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