Re: What's going on here?

2016-06-01 Thread Lawrence D’Oliveiro
On Monday, May 23, 2016 at 9:47:15 AM UTC+12, DFS wrote:
> def splitrange(b,e,g):
>   sr=[]
>   for i in range(b,e,g):
>   bg=i;eg=min(e,bg+g-1)
>   sr.append((bg,eg))
>   return sr

To be more in keeping with the Python ethos, I would take out the “-1”.
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: function with list argument defaulting to [] - what's going on here???

2007-04-15 Thread Tim Leslie
On 14 Apr 2007 20:20:42 -0700, Paddy [EMAIL PROTECTED] wrote:
 On Apr 15, 3:58 am, Steven D'Aprano
 [EMAIL PROTECTED] wrote:
  On Sat, 14 Apr 2007 17:33:11 -0800, Troy Melhase wrote:
   On 4/14/07, Mike [EMAIL PROTECTED] wrote:
   While trying to write a recursive function involving lists, I came
   across some (to me) odd behavior which I don't quite understand. Here's
   a trivial function showing the problem.
 
   fromhttp://docs.python.org/ref/function.html:
 
   Default parameter values are evaluated when the function definition is
   executed. This means that the expression is evaluated once, when the
   function is defined, and that that same ``pre-computed'' value is used
   for each call. This is especially important to understand when a
   default parameter is a mutable object, such as a list or a dictionary:
   if the function modifies the object (e.g. by appending an item to a
   list), the default value is in effect modified.
 
  This comes up so often that I wonder whether Python should issue a warning
  when it sees [] or {} as a default argument.
 
  What do people think? A misuse or good use of warnings?
 
  --
  Steven.

 I wonder if it is a check done by Pylint or PyChecker?

It is a check done by pylint

Tim


 - Paddy.

 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


function with list argument defaulting to [] - what's going on here???

2007-04-14 Thread Mike
While trying to write a recursive function involving lists, I came 
across some (to me) odd behavior which I don't quite understand. Here's 
a trivial function showing the problem.

  def f(l, r = []):
for itm in l:
r.append(itm)
print r


  a = [1,2,3]
  f(a)
[1, 2, 3]
  f(a)
[1, 2, 3, 1, 2, 3]
  f(a)
[1, 2, 3, 1, 2, 3, 1, 2, 3]

I know the function is quite artificial, but it's for illustration only. 
Why is r not being reset to the empty list on subsequent calls? It 
seems like it should be reinitialized when not explicitly provided.

Thanks in advance.
Mike
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: function with list argument defaulting to [] - what's going on here???

2007-04-14 Thread Troy Melhase
On 4/14/07, Mike [EMAIL PROTECTED] wrote:
 While trying to write a recursive function involving lists, I came
 across some (to me) odd behavior which I don't quite understand. Here's
 a trivial function showing the problem.

from http://docs.python.org/ref/function.html :

Default parameter values are evaluated when the function definition is
executed. This means that the expression is evaluated once, when the
function is defined, and that that same ``pre-computed'' value is used
for each call. This is especially important to understand when a
default parameter is a mutable object, such as a list or a dictionary:
if the function modifies the object (e.g. by appending an item to a
list), the default value is in effect modified.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: function with list argument defaulting to [] - what's going on here???

2007-04-14 Thread Mike
Thanks, Troy. I never cease to be amazed at what can be discovered by 
reading the manual! self bangs head on wall

Mike

Troy Melhase wrote:
 On 4/14/07, Mike [EMAIL PROTECTED] wrote:
 While trying to write a recursive function involving lists, I came
 across some (to me) odd behavior which I don't quite understand. Here's
 a trivial function showing the problem.
 
 from http://docs.python.org/ref/function.html :
 
 Default parameter values are evaluated when the function definition is
 executed. This means that the expression is evaluated once, when the
 function is defined, and that that same ``pre-computed'' value is used
 for each call. This is especially important to understand when a
 default parameter is a mutable object, such as a list or a dictionary:
 if the function modifies the object (e.g. by appending an item to a
 list), the default value is in effect modified.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: function with list argument defaulting to [] - what's going on here???

2007-04-14 Thread Steven D'Aprano
On Sat, 14 Apr 2007 17:33:11 -0800, Troy Melhase wrote:

 On 4/14/07, Mike [EMAIL PROTECTED] wrote:
 While trying to write a recursive function involving lists, I came
 across some (to me) odd behavior which I don't quite understand. Here's
 a trivial function showing the problem.
 
 from http://docs.python.org/ref/function.html :
 
 Default parameter values are evaluated when the function definition is
 executed. This means that the expression is evaluated once, when the
 function is defined, and that that same ``pre-computed'' value is used
 for each call. This is especially important to understand when a
 default parameter is a mutable object, such as a list or a dictionary:
 if the function modifies the object (e.g. by appending an item to a
 list), the default value is in effect modified.



This comes up so often that I wonder whether Python should issue a warning
when it sees [] or {} as a default argument.


What do people think? A misuse or good use of warnings?



-- 
Steven.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: function with list argument defaulting to [] - what's going on here???

2007-04-14 Thread Alex Martelli
Mike [EMAIL PROTECTED] wrote:
   ...
 Why is r not being reset to the empty list on subsequent calls? It 
 seems like it should be reinitialized when not explicitly provided.

http://www.python.org/doc/faq/general/#why-are-default-values-shared-be
tween-objects


Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: function with list argument defaulting to [] - what's going on here???

2007-04-14 Thread Paddy
On Apr 15, 3:58 am, Steven D'Aprano
[EMAIL PROTECTED] wrote:
 On Sat, 14 Apr 2007 17:33:11 -0800, Troy Melhase wrote:
  On 4/14/07, Mike [EMAIL PROTECTED] wrote:
  While trying to write a recursive function involving lists, I came
  across some (to me) odd behavior which I don't quite understand. Here's
  a trivial function showing the problem.

  fromhttp://docs.python.org/ref/function.html:

  Default parameter values are evaluated when the function definition is
  executed. This means that the expression is evaluated once, when the
  function is defined, and that that same ``pre-computed'' value is used
  for each call. This is especially important to understand when a
  default parameter is a mutable object, such as a list or a dictionary:
  if the function modifies the object (e.g. by appending an item to a
  list), the default value is in effect modified.

 This comes up so often that I wonder whether Python should issue a warning
 when it sees [] or {} as a default argument.

 What do people think? A misuse or good use of warnings?

 --
 Steven.

I wonder if it is a check done by Pylint or PyChecker?

- Paddy.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: function with list argument defaulting to [] - what's going on here???

2007-04-14 Thread BJörn Lindqvist
 This comes up so often that I wonder whether Python should issue a warning
 when it sees [] or {} as a default argument.


 What do people think? A misuse or good use of warnings?

I think Python should reevaluate the default values.

-- 
mvh Björn
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: function with list argument defaulting to [] - what's going on here???

2007-04-14 Thread Steven D'Aprano
On Sun, 15 Apr 2007 05:29:01 +0200, BJörn Lindqvist wrote:

 This comes up so often that I wonder whether Python should issue a warning
 when it sees [] or {} as a default argument.


 What do people think? A misuse or good use of warnings?
 
 I think Python should reevaluate the default values.

That would break code that relies on the current behaviour. That makes it
a maybe for Python 3.0, and an absolute NO!!! for Python 2.x.



-- 
Steven.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: function with list argument defaulting to [] - what's going on here???

2007-04-14 Thread Alex Martelli
Steven D'Aprano [EMAIL PROTECTED] wrote:

 On Sun, 15 Apr 2007 05:29:01 +0200, BJörn Lindqvist wrote:
 
  This comes up so often that I wonder whether Python should issue a warning
  when it sees [] or {} as a default argument.
 
 
  What do people think? A misuse or good use of warnings?
  
  I think Python should reevaluate the default values.
 
 That would break code that relies on the current behaviour. That makes it
 a maybe for Python 3.0, and an absolute NO!!! for Python 2.x.

If you hope to get any change in Python 3.0, your PEP had better be in
before the end of April -- that's the 3.0 deadline for PEPs.


Alex
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: X class missing in Python :-) - Re: What's going on here?

2006-11-23 Thread robert
John Machin wrote:
 robert wrote:
 Dale Strickland-Clark wrote:
 Python 2.4.2 (#1, Oct 13 2006, 17:11:24)
 [GCC 4.1.0 (SUSE Linux)] on linux2
 Type help, copyright, credits or license for more information.
 a = object()
 a
 object object at 0xb7bbd438
 a.spam = 1
 Traceback (most recent call last):
   File stdin, line 1, in ?
 AttributeError: 'object' object has no attribute 'spam'
 class b(object):
 ...pass
 ...
 a = b()
 a
 __main__.b object at 0xb7b4dcac
 a.spam = 1

 What is subclassing adding to the class here? Why can't I assign to
 attributes of an instance of object?

 Python so dynamic, but it lacks a (builtin) X-class ready for ad-hoc 
 usage just like dict() :-)
 I have in almost every app/toolcore-module this one:

 --

 class X(object):
 def __init__(self,_d={},**kwargs):
 kwargs.update(_d)
 self.__dict__=kwargs
 class Y(X):
 def __repr__(self):
 return 'Y:%s'%self.__dict__

 --

 x=X(spam=1)

 Maybe X should be renamed to __builtin__.Object ...


 
 Have you considered putting it in one file and *importing* it into
 almost every app/toolcore-module?

(yes its in my core python language extension module, which I import 
frequently in apps)

 Have you considered that others may like to have something a little
 more elaborate, like maybe using the pprint module, or that the amount
 of data that would spew out might in some cases be so great that they
 wouldn't want that every time from repr(), preferring a dump-style
 method that wrote to a logfile?

(in X is no repr so far. of course one could make a default repr with short 
output. had no frequent needs so far)

 IMHO that's one of the enormous number of good points about Python; you
 can easily lash up something like that to suit yourself and inject it
 into any class you like; there's no central authority tying your hands
 behind your back.

its more about the general case, trying things out on the interactive etc. 
always - thus when I want speed not suit  :-)

very often I need a dummy object and find me always typing class X:pass or 
import above tools.
Think this trivial but needed Object() thing is possibly more than a private 
sitecustomize-thing. Thats why I wrote here upon seeing others trying object() 
which doesn't do what one expects at first.
It wouldn't really tie hands or ? but possibly converse

Robert
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What's going on here?

2006-11-23 Thread Dale Strickland-Clark
Thanks for the answers. I am informed but I don't feel enlightened. 

It does strike me as odd that an apparently empty subclass should add extra
function to the base class. 

Not at all obvious.
-- 
Dale Strickland-Clark
We are recruiting Python programmers. Please see the web site.
Riverhall Systems www.riverhall.co.uk

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What's going on here?

2006-11-23 Thread robert
Dale Strickland-Clark wrote:
 Thanks for the answers. I am informed but I don't feel enlightened. 
 
 It does strike me as odd that an apparently empty subclass should add extra
 function to the base class. 
 
 Not at all obvious.

Yes. As said, there is missing a __builtin__.Object

object is not an empty class, but the base builtin-type:

 isinstance(int,object)
True

built-in type instances are basically read-only because if ...


 (1).spam=1
Traceback (most recent call last):
  File interactive input, line 1, in ?
AttributeError: 'int' object has no attribute 'spam'
 


..would work, that would be very strange. 
Maybe in a mud place language like Ruby, where you can jam and scribble 
everywhere in the system and in builtins, such things are possible.  


I'd propose to add something trivial like


class Object(object):
   def __init__(self,_d={},**kwargs):
   kwargs.update(_d)
   self.__dict__=kwargs 
   ...

to Python. I use such empty (common) class since all time I can think of - and 
frequently. 
If there would be a common such Empty class in Python you'd have a lot of 
advantanges. From ad-hoc instances, OO-dicts to reliable pickling of bunches 
of variables etc.


Robert


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What's going on here?

2006-11-23 Thread Carl Banks
Dale Strickland-Clark wrote:
 Thanks for the answers. I am informed but I don't feel enlightened.

 It does strike me as odd that an apparently empty subclass should add extra
 function to the base class.

 Not at all obvious.

Remember that a class definition is syntax sugar for a direct call to
type:

type(name,bases,clsdict)

This constructs and returns the class.  It's free to add behaviors even
if the clsdict is empty, and that's exactly what it does in this case.
type considers the absence of __slots__ in clsdict to be a request for
an instance dict, and so it adds one.  (Types defined in C aren't
created that way, however.  I believe C-defined types request an
instance dict by reserving space for it in the object structure and
setting tp_dictoffset to indicate its offset.)

This is not obvious, but it's really the only reasonable way.  You
can't have the base class of all objects creating dicts for its
instances--you wouldn't want every int object to have it's own dict.
And you can't have Python class instances not have a dict by default.


Carl Banks

-- 
http://mail.python.org/mailman/listinfo/python-list


What's going on here?

2006-11-22 Thread Dale Strickland-Clark
Python 2.4.2 (#1, Oct 13 2006, 17:11:24)
[GCC 4.1.0 (SUSE Linux)] on linux2
Type help, copyright, credits or license for more information.
 a = object()
 a
object object at 0xb7bbd438
 a.spam = 1
Traceback (most recent call last):
  File stdin, line 1, in ?
AttributeError: 'object' object has no attribute 'spam'
 class b(object):
...pass
...
 a = b()
 a
__main__.b object at 0xb7b4dcac
 a.spam = 1


What is subclassing adding to the class here? Why can't I assign to
attributes of an instance of object?
-- 
Dale Strickland-Clark
Riverhall Systems - www.riverhall.co.uk

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What's going on here?

2006-11-22 Thread Scott David Daniels
Dale Strickland-Clark wrote:
 Python 2.4.2 (#1, Oct 13 2006, 17:11:24)
 a = object()
 a.spam = 1
 Traceback (most recent call last):
   File stdin, line 1, in ?
 AttributeError: 'object' object has no attribute 'spam'
 class B(object): pass
 a = B()
 a.spam = 1

 
 What is subclassing adding to the class here? Why can't I assign to
 attributes of an instance of object?

object itself doesn't have a __dict__ slot, so that very small objects
(such as points) can be built without that overhead.

--Scott David Daniels
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What's going on here?

2006-11-22 Thread Richie Hindle

 What is subclassing adding to the class here?

A __dict__:

 o = object()
 dir(o)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
'__init__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__str__']
 class C(object): pass
...
 c = C()
 dir(c)
['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__']

-- 
Richie Hindle
[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What's going on here?

2006-11-22 Thread Fredrik Lundh
Dale Strickland-Clark wrote:

 Why can't I assign to attributes of an instance of object?

it doesn't have any attribute storage.

/F 



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: What's going on here?

2006-11-22 Thread Gerald Klix
Perhaps this piece of code might explain the behaviour:


  class C( object ):
... __slots__ = ()
...
  o = C()
  o.a = 1
Traceback (most recent call last):
   File input, line 1, in ?
AttributeError: 'C' object has no attribute 'a'


object behaves like having an implict __slots__ attribute.


HTH,
Gerald


Dale Strickland-Clark schrieb:
 Python 2.4.2 (#1, Oct 13 2006, 17:11:24)
 [GCC 4.1.0 (SUSE Linux)] on linux2
 Type help, copyright, credits or license for more information.
 
a = object()
a
 
 object object at 0xb7bbd438
 
a.spam = 1
 
 Traceback (most recent call last):
   File stdin, line 1, in ?
 AttributeError: 'object' object has no attribute 'spam'
 
class b(object):
 
 ...pass
 ...
 
a = b()
a
 
 __main__.b object at 0xb7b4dcac
 
a.spam = 1

 
 
 What is subclassing adding to the class here? Why can't I assign to
 attributes of an instance of object?

-- 
http://mail.python.org/mailman/listinfo/python-list


X class missing in Python :-) - Re: What's going on here?

2006-11-22 Thread robert
Dale Strickland-Clark wrote:
 Python 2.4.2 (#1, Oct 13 2006, 17:11:24)
 [GCC 4.1.0 (SUSE Linux)] on linux2
 Type help, copyright, credits or license for more information.
 a = object()
 a
 object object at 0xb7bbd438
 a.spam = 1
 Traceback (most recent call last):
   File stdin, line 1, in ?
 AttributeError: 'object' object has no attribute 'spam'
 class b(object):
 ...pass
 ...
 a = b()
 a
 __main__.b object at 0xb7b4dcac
 a.spam = 1

 
 What is subclassing adding to the class here? Why can't I assign to
 attributes of an instance of object?


Python so dynamic, but it lacks a (builtin) X-class ready for ad-hoc usage 
just like dict() :-)
I have in almost every app/toolcore-module this one:

--

class X(object):
def __init__(self,_d={},**kwargs):
kwargs.update(_d)
self.__dict__=kwargs
class Y(X):
def __repr__(self):
return 'Y:%s'%self.__dict__

--

x=X(spam=1)

Maybe X should be renamed to __builtin__.Object ...


Robert
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: X class missing in Python :-) - Re: What's going on here?

2006-11-22 Thread John Machin

robert wrote:
 Dale Strickland-Clark wrote:
  Python 2.4.2 (#1, Oct 13 2006, 17:11:24)
  [GCC 4.1.0 (SUSE Linux)] on linux2
  Type help, copyright, credits or license for more information.
  a = object()
  a
  object object at 0xb7bbd438
  a.spam = 1
  Traceback (most recent call last):
File stdin, line 1, in ?
  AttributeError: 'object' object has no attribute 'spam'
  class b(object):
  ...pass
  ...
  a = b()
  a
  __main__.b object at 0xb7b4dcac
  a.spam = 1
 
 
  What is subclassing adding to the class here? Why can't I assign to
  attributes of an instance of object?


 Python so dynamic, but it lacks a (builtin) X-class ready for ad-hoc 
 usage just like dict() :-)
 I have in almost every app/toolcore-module this one:

 --

 class X(object):
 def __init__(self,_d={},**kwargs):
 kwargs.update(_d)
 self.__dict__=kwargs
 class Y(X):
 def __repr__(self):
 return 'Y:%s'%self.__dict__

 --

 x=X(spam=1)

 Maybe X should be renamed to __builtin__.Object ...



Have you considered putting it in one file and *importing* it into
almost every app/toolcore-module?

Have you considered that others may like to have something a little
more elaborate, like maybe using the pprint module, or that the amount
of data that would spew out might in some cases be so great that they
wouldn't want that every time from repr(), preferring a dump-style
method that wrote to a logfile?

IMHO that's one of the enormous number of good points about Python; you
can easily lash up something like that to suit yourself and inject it
into any class you like; there's no central authority tying your hands
behind your back.

HTH,
John

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what's going on here?

2006-04-04 Thread Ant
You are along the right lines. Try printing out the content of each URL
- one of the pages will match your expression, but has additional
instructions... I think you are reaching the end of their false trail
when you get None returned from the url.

The set of pages themselves are the linked list - each contains a
reference to the next element in the sequence. You don't need one to
solve the problem, the problem *is* the linked list!

An example of a linked list in Python is the following if you are
interested:


class Element (object):
next = None
def __init__(self, content):
self.content = content

class LinkedList (object):
first = None
last = None

def add(self, item):
elmnt = Element(item)
if not self.first:
self.first = elmnt
if self.last:
self.last.next = elmnt
self.last = elmnt
def __iter__(self):
current = self.first
while current:
yield current.content
current = current.next

ll = LinkedList()

for i in range(10):
ll.add(i)

for x in ll:
print Res: %s % x

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what's going on here?

2006-04-04 Thread John Salerno
Ant wrote:
 You are along the right lines. Try printing out the content of each URL
 - one of the pages will match your expression, but has additional
 instructions... I think you are reaching the end of their false trail
 when you get None returned from the url.

But the weird thing is that when I tried the same script earlier in the 
day, it went through about 200+ links before encountering a situation 
that my script didn't handle. But now when I get this latest error that 
I posted, it's only going through about 150 links before stopping.

 The set of pages themselves are the linked list - each contains a
 reference to the next element in the sequence. You don't need one to
 solve the problem, the problem *is* the linked list!

Interesting! That's good news, and I'm glad I didn't spend hours trying 
to use one to solve it! :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what's going on here?

2006-04-04 Thread John Salerno
John Salerno wrote:
 Ant wrote:
 You are along the right lines. Try printing out the content of each URL
 - one of the pages will match your expression, but has additional
 instructions... I think you are reaching the end of their false trail
 when you get None returned from the url.
 
 But the weird thing is that when I tried the same script earlier in the 
 day, it went through about 200+ links before encountering a situation 
 that my script didn't handle. But now when I get this latest error that 
 I posted, it's only going through about 150 links before stopping.
 
 The set of pages themselves are the linked list - each contains a
 reference to the next element in the sequence. You don't need one to
 solve the problem, the problem *is* the linked list!
 
 Interesting! That's good news, and I'm glad I didn't spend hours trying 
 to use one to solve it! :)

Ok, I'm confused. I ran the script again today (this time at work again) 
and it worked! I made no changes, so I'm not sure what the issue was. 
But thank god I'm passed this problem, although I'm sure it only gets 
worse now!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what's going on here?

2006-04-04 Thread Roel Schroeven
John Salerno schreef:
 But thank god I'm passed this problem, although I'm sure it only gets 
 worse now!

Yes, I'm afraid it does. I got stuck at puzzle 27 and gave up 
temporarily. I'm going to try again though when I feel I need a challenge :)

-- 
If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

Roel Schroeven
-- 
http://mail.python.org/mailman/listinfo/python-list


what's going on here?

2006-04-03 Thread John Salerno
Ok, long story: I'm trying to solve level 4 of the Python Challenge. I 
hate to post here, but the hint forum over there is dead. Here's the 
link: http://www.pythonchallenge.com/pc/def/linkedlist.php

Apparently you need to use a linked list to solve it, so I read up on 
them but I still don't understand how to implement one to solve this 
problem. (Any hints there would be appreciated too.) So I wrote this 
code instead. It goes to each URL, reads the source code, gets the next 
number, etc. I realize it uses some terrible tricks, like the regex and 
the try/except clause, which is way too specific to solve the problem in 
a general way. Anyway, here's the code:

# Python Challenge, level 4

import urllib
import re

url = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
pattern = re.compile(r'\bnext nothing is (\d+)')

def getNextNum(url):
 global pattern
 global nextNum
 site = urllib.urlopen(url)
 try:
 source = site.read()
 site.close()
 number = re.search(pattern, source).group(1)
 except:
 print nextNum
 number = str(int(nextNum) / 2)
 return number

nextNum = '12345'
f = open(r'C:\Python24\myscripts\challenges\numbers.txt', 'w')
try:
 for x in range(300):
 f.write(nextNum + '\n')
 nextNum = getNextNum(url + nextNum)
finally:
 f.close()

print url + nextNum

Now, I tried this earlier on my work computer and it got as far as 
printing out two 'nextNum's in the except block, then it appeared that 
the website timed out and I could no longer access it for a while.

I tried later on my home computer and now I keep getting this error:

Traceback (most recent call last):
   File C:\Python24\myscripts\challenges\linked_list.py, line 27, in 
-toplevel-
 nextNum = getNextNum(url + nextNum)
   File C:\Python24\myscripts\challenges\linked_list.py, line 12, in 
getNextNum
 site = urllib.urlopen(url)
   File C:\Python24\lib\urllib.py, line 82, in urlopen
 return opener.open(url)
   File C:\Python24\lib\urllib.py, line 190, in open
 return getattr(self, name)(url)
   File C:\Python24\lib\urllib.py, line 322, in open_http
 return self.http_error(url, fp, errcode, errmsg, headers)
   File C:\Python24\lib\urllib.py, line 339, in http_error
 return self.http_error_default(url, fp, errcode, errmsg, headers)
   File C:\Python24\lib\urllib.py, line 579, in http_error_default
 return addinfourl(fp, headers, http: + url)
   File C:\Python24\lib\urllib.py, line 871, in __init__
 addbase.__init__(self, fp)
   File C:\Python24\lib\urllib.py, line 818, in __init__
 self.read = self.fp.read
AttributeError: 'NoneType' object has no attribute 'read'

I didn't get this before, so I wonder if it's a website error. There 
seems to be a problem with getting the source code. It also appears that 
the script, when run at home, doesn't get as far as the same script when 
I ran it earlier today at work.

So I figure it's either a website problem, or some kind of strange 
difference between the two computers I'm using (but both use 2.4.3 and 
nothing else seems different).

I hope someone can point me in the right direction. I'm curious why it 
fails in a different way at home than at work, but also I'd like to know 
if it's even possible to solve the problem in this way, or if I *have* 
to use a linked list.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what's going on here?

2006-04-03 Thread John Salerno
John Salerno wrote:
 Ok, long story

Ok, I guess I should have used a better title for the thread. I hope 
someone still sees this post! :)
-- 
http://mail.python.org/mailman/listinfo/python-list


what's going on here?

2006-03-16 Thread John Salerno
This might be confusing to explain, because it's a question about an 
example in Beginning Python and I'll try to provide all the info I can.

First off, I'm reading a chapter on using the ReportLab modules to 
create a line graph from a set of data. The first implementation of the 
program uses a hard-coded list as the data source, the second 
implementation pulls the data from a URL. In either case, the data is of 
this format:

# year month predicted high low
200412  34.235.233.2
200501  31.534.528.5
(repeated many times)

In the first implementation, the data was a list of tuples, each tuple 
being one row of the data. So grabbing the data was done like this:

pred = [row[2]-40 for row in data]
high = [row[3]-40 for row in data]
etc...

We can safely ignore the '-40', that was just for positioning. So the 
variable for predicted would grab 34.2, 31.5, etc., high would get 35.2, 
etc. Easy enough.

Now, the second implementation does this:

for line in urlopen(URL).readlines():
if not line.isspace() and not line[0] in COMMENT_CHARS:
data.append(map(float, line.split()))

pred = [row[2] for row in data]
high = [row[3] for row in data]
etc.

(URL is the location of the data file online. The if statement just 
checks for blank lines and lines beginning with certain comment 
characters, so they can be ignored.)

So finally here's my question: If you are using data.append(), doesn't 
that just put all the numbers into one long list? How are the tuples 
still being created in this case so that the list comprehensions still 
work? It seems like there is no longer any 'row' to refer to in data.

The only thing I can think of is that each time through the for loop, a 
new item (tuple or list) is being created in the data list, so that each 
row of data really is being separated as its own element in the larger 
list, but that doesn't seem right.

Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what's going on here?

2006-03-16 Thread Schüle Daniel
[...]

 So finally here's my question: If you are using data.append(), doesn't 
 that just put all the numbers into one long list? 

no, append appends
extend does what you think

How are the tuples
 still being created in this case so that the list comprehensions still 
 work? It seems like there is no longer any 'row' to refer to in data.

why not to fire interpreter to see what happens

  line1 = 1 2 3 4
  line2 = 5 6 7 8
  lst = []
  lst.append(map(float, line1.split()))
  lst
[[1.0, 2.0, 3.0, 4.0]]
  lst.append(map(float, line2.split()))
  lst
[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]]
 


hth, Daniel

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: what's going on here?

2006-03-16 Thread Felipe Almeida Lessa
Em Qui, 2006-03-16 às 16:31 +, John Salerno escreveu:
 So finally here's my question: If you are using data.append(), doesn't 
 that just put all the numbers into one long list? How are the tuples 
 still being created in this case so that the list comprehensions still 
 work? It seems like there is no longer any 'row' to refer to in data.

Look the line data.append(map(float, line.split()))

In other words:

# Suppose line is 200412  34.235.233.2
# for our comments

# Creates a list, like [2004, 12, 34.2, 35.2, 33.2]
splitted = line.split() 

# Convert all numbers to floats
numbers = map(float, splitted)

# It could also be
# numbers = [float(x) for x in splitted]

# Append the list to the data
# ReportLab accept both lists and tuples, so it doesn't matter
data.append(numbers)


Hope this clarifies your mind,
Felipe.

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: what's going on here?

2006-03-16 Thread John Salerno
Felipe Almeida Lessa wrote:

 # Suppose line is 200412  34.235.233.2
 # for our comments
 
 # Creates a list, like [2004, 12, 34.2, 35.2, 33.2]
 splitted = line.split() 

Thanks guys! I think what I forgot was that split() returns a list, so 
that's when the 'rows' were being created.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: mod_python: what's going on here?

2005-09-05 Thread Steve Holden
Robert J. Hansen wrote:
 I'm not entirely certain comp.lang.python is the proper newsgroup for
 mod_python questions, but comp.lang.python.web doesn't seem to exist,
 so... my apologies in advance if this is considered off-topic.
 
 I'm attempting to get mod_python 3.1.4/python 2.4.1 working on Apache
 2.0.54 running under OS X.  Apache was compiled from source with a
 simple
 
 /configure --enable-so --with-mpm=worker
 
 ... followed by the make/make install dance.  mod_python was almost as
 simple:
 
 ./configure --with-apxs=/usr/local/apache2/bin/apxs \
   --with-python=/sw/bin/python2.4
 
 ... followed by the requisite dance.
 
 At this point, all's well.  The following bits were added to
 httpd.conf:
 
 LoadModule python_module /usr/local/apache2/modules/mod_python.so
 Directory /usr/local/apache2/htdocs/test
AddHandler mod_python .py
PythonHandler mptest
PythonDebug on
 /Directory
 
 ... one apachectl restart later, Apache was running fine and serving
 pages.  The version string at the bottom of some static pages listed
 mod_python as being present, so I reckoned that meant it was all
 installed all right.  However, any attempt to serve a mod_python
 script, mptest.py, from the test subdirectory results in a 500 Internal
 Server Error.  Nothing gets written to error_log, but access_log
 confirms the 500 was sent.
 
 Does anyone have any experience with mod_python on OS X/Apache
 environments?  Can anyone shed some light on 500s that don't leave
 traces in the error logs, or what precise incantation I need to make
 mod_python start serving up scripts?
 
 Also, if this is not appropriate for this group, does anyone know of a
 Python group for which this is more appropriate?
 
You will probably get help on this newsgroup, but the mod-python list is 
pretty helpful, and there's a Python Web-Sig mailing list you can find 
out about at http://www.python.org/sigs/ if you want.

I'm afraid I am new to OS X (and 3,000 miles away from my Mac Mini), so 
I can't be any help directly with any Mac-dependent issues. But when I 
first started using mod_python someone (I'm afraid I don't remember who) 
advised me to use SetHandler rather than AddHandler.

Looking at my Windows httpd.conf (2.0.52, so close enough to yours) I see

#
#   #
#   MOD_PYTHON TESTING AREA #
#   #
#
#
#   Request handler
#
Directory C:/apache/htdocs/modpy
 AllowOverride FileInfo
 SetHandler mod_python
 PythonHandler mptest
 PythonDebug On
/Directory
#
#   Filter handler
#
Directory C:/Apache/htdocs/test
 AllowOverride All
 SetHandler mod_python
 #PythonHandler mptest1
 #PythonFixupHandler mptest1
 PythonLogHandler mptest1
 PythonOutputFilter mptest1 CAPITALIZE
 AddOutputFilter CAPITALIZE .txt .py
 PythonDebug On
/Directory

This might at least be enough to start generating sensible error messages.

One further possibility is that Apache is using an older Python by 
default: byte codes vary from version to version, so something horrible 
might be happening as mod_python tries to execute, but that seems a bit 
far-fetched when you've got so far. I seem to remember that OS X doesn't 
supply 2.4 by default: have you added it or overwritten the default 
Python? Just another base to cover ...

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

-- 
http://mail.python.org/mailman/listinfo/python-list


mod_python: what's going on here?

2005-09-04 Thread Robert J. Hansen
I'm not entirely certain comp.lang.python is the proper newsgroup for
mod_python questions, but comp.lang.python.web doesn't seem to exist,
so... my apologies in advance if this is considered off-topic.

I'm attempting to get mod_python 3.1.4/python 2.4.1 working on Apache
2.0.54 running under OS X.  Apache was compiled from source with a
simple

/configure --enable-so --with-mpm=worker

... followed by the make/make install dance.  mod_python was almost as
simple:

./configure --with-apxs=/usr/local/apache2/bin/apxs \
  --with-python=/sw/bin/python2.4

... followed by the requisite dance.

At this point, all's well.  The following bits were added to
httpd.conf:

LoadModule python_module /usr/local/apache2/modules/mod_python.so
Directory /usr/local/apache2/htdocs/test
   AddHandler mod_python .py
   PythonHandler mptest
   PythonDebug on
/Directory

... one apachectl restart later, Apache was running fine and serving
pages.  The version string at the bottom of some static pages listed
mod_python as being present, so I reckoned that meant it was all
installed all right.  However, any attempt to serve a mod_python
script, mptest.py, from the test subdirectory results in a 500 Internal
Server Error.  Nothing gets written to error_log, but access_log
confirms the 500 was sent.

Does anyone have any experience with mod_python on OS X/Apache
environments?  Can anyone shed some light on 500s that don't leave
traces in the error logs, or what precise incantation I need to make
mod_python start serving up scripts?

Also, if this is not appropriate for this group, does anyone know of a
Python group for which this is more appropriate?

-- 
http://mail.python.org/mailman/listinfo/python-list