Re: xlrd 1.0.0 released!

2016-06-07 Thread derek

Gmail and the Gmail Digest only ever show the 0.9.4 tag - not sure if this 
a big issue or not (but I know I would like to the 1.0.0 tag to be proudly 
displayed!)

On Friday, 3 June 2016 03:57:35 UTC+2, Chris Withers wrote:
>
> Ugh, and once again, this time with a corrected title... 
>
>
> On 02/06/2016 18:56, Chris Withers wrote: 
> > Hi All, 
> > 
> > Well, I've finally called it and tagged current master of xlrd as 1.0.0: 
> > 
> > http://pypi.python.org/pypi/xlrd/1.0.0 
> > 
> > This release includes the following changes since the last release: 
> > 
> > - Official support, such as it is, is now for 2.6, 2.7, 3.3+ 
> > 
> > - Fixes a bug in looking up non-lowercase sheet filenames by ensuring 
> > that the sheet targets are transformed the same way as the 
> > component_names dict keys. 
> > 
> > - Fixes a bug for ragged_rows=False when merged cells increases the 
> > number of columns in the sheet. This requires all rows to be extended 
> > to ensure equal row lengths that match the number of columns in the 
> > sheet. 
> > 
> > - Fixes to enable reading of SAP-generated .xls files. 
> > 
> > - support BIFF4 files with missing FORMAT records. 
> > 
> > - support files with missing WINDOW2 record. 
> > 
> > - Empty cells are now always unicode strings, they were a bytestring 
> > on Python2 and a unicode string on Python3. 
> > 
> > - Fix for  inlineStr attribute without  child. 
> > 
> > - Fix for a zoom of None causes problems on Python 3. 
> > 
> > - Fix parsing of bad dimensions. 
> > 
> > - Fix xlsx sheet->comments relationship. 
> > 
> > Thanks to the following for their contributions to this release: 
> > 
> > - Lars-Erik Hannelius 
> > - Deshi Xiao 
> > - Stratos Moro 
> > - Volker Diels-Grabsch 
> > - John McNamara 
> > - Ville Skyttä 
> > - Patrick Fuller 
> > - Dragon Dave McKee 
> > - Gunnlaugur Þór Briem 
> > 
> > If you find any problems, please ask about them on the 
> > python...@googlegroups.com  list, or submit an issue on 
> GitHub: 
> > 
> > https://github.com/python-excel/xlrd/issues 
> > 
> > Full details of all things Python and Excel related can be found here: 
> > 
> > http://www.python-excel.org/ 
> > 
> > NB: If you would like to become the maintainer of xlwt, please get in 
> > touch! Neither myself nor John Machin have much time to drive things 
> > forward nowadays, hence the year or so between each release... 
> > 
> > cheers, 
> > 
> > Chris 
> > 
>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Good python reference?

2005-11-10 Thread derek
Hello! I'm new to the group and am looking for a decent reference for
information about the history / evolution of the Python language and
its features.  Typing, scoping, etc...  I'd appreciate any good links.
Thanks!

- Derek

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


Python on an embedded platform

2006-01-19 Thread Derek
Hi,

I am looking to port Python to an embedded platform (an ARM7 device with 
fairly limited memory, capable of running an RTOS, but not an OS, such as 
Linux). I came across DePython from a few years ago, but it seems to have 
died a death.

Does anybody have advice? I am looking for any tricks, features I can 
disable, etc so I can get the python core to be a small as possible (<100k 
would be good).

Thanks
-- 
Derek 


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


Install python with custom tk-tcl installation

2012-12-17 Thread Derek Thomas
Hello, 

I seem to have a problem because it seems Tkinter assumes relative paths for 
TCL_LIBRARY and TK_LIBRARY.

I am working with the homebrew group to get python27 to install nicely with a 
custom installation of tk and tcl 
(https://github.com/mxcl/homebrew/pull/16626).  However, this breaks because 
homebrew installs tk and tcl in separate directories and then soft-links them 
to /usr/local/lib. When I install python with this method, it breaks tkinter 
with the following error:

```
>>> import Tkinter; root = Tkinter.Tk()
Traceback (most recent call last):
  File "", line 1, in 
  File
"/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py",
line 1685, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive,
wantobjects, useTk, sync, use)
_tkinter.TclError: Can't find a usable tk.tcl in the following directories:
/usr/local/Cellar/tcl/8.5.9/lib/tcl8.5/tk8.5
/usr/local/Cellar/tcl/8.5.9/lib/tcl8.5/tk8.5/Resources/Scripts
/usr/local/Cellar/tcl/8.5.9/lib/tk8.5
/usr/local/Cellar/tcl/8.5.9/lib/tk8.5/Resources/Scripts
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/lib/tk8.5
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/lib/tk8.5/Resources/Scripts
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/lib/tk8.5
/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/library

This probably means that tk wasn't installed properly.
```

It is looking in the wrong library directory for the tk.tcl file, which is  in 
/usr/local/Cellar/tk/8.5.9/lib/tk8.5/tk.tcl . In fact, it is assuming that the 
tk8.5 lib-directory is installed relative to the tcl8.5 lib-directory which is 
incorrect. It fails because it cannot find the file in the relative directory.

I can fix this by setting the environment variable TCL_LIBRARY to a path that 
is relative to a soft-linked tk-library (/usr/local/lib/tcl8.5), but is there 
someway to set tkinter to search the correct path during installation without 
having to set the environment after installation? I looked in _tkinter.c and 
tkappinit.c but could not find where it was determining the tcl library. 

If you have any advice or thoughts, I would be very appreciative. Thank you in 
advance.
-- 
http://mail.python.org/mailman/listinfo/python-list


Optimizing Memory Allocation in a Simple, but Long Function

2016-04-23 Thread Derek Klinge
I have been writing a python script to explore Euler's Method of
approximating Euler's Number. I was hoping there might be a way to make
this process work faster, as for sufficiently large eulerSteps, the process
below becomes quite slow and sometimes memory intensive. I'm hoping someone
can give me some insight as to how to optimize these algorithms, or ways I
might decrease memory usage. I have been thinking about finding a way
around importing the math module, as it seems a bit unneeded except as an
easy reference.

## Write a method to approximate Euler's Number using Euler's Method
import math

class EulersNumber():
def __init__(self,n):
self.eulerSteps = n
self.e = self.EulersMethod(self.eulerSteps)

def linearApproximation(self,x,h,d): # f(x+h)=f(x)+h*f'(x)
return x + h * d

def EulersMethod(self, numberOfSteps): # Repeat linear approximation over
an even range
e = 1 # e**0 = 1
for step in range(numberOfSteps):
e = self.linearApproximation(e,1.0/numberOfSteps,e) # if f(x)= e**x,
f'(x)=f(x)
return e


def EulerStepWithGuess(accuracy,guessForN):
n = guessForN
e = EulersNumber(n)
while abs(e.e -math.e) > abs(accuracy):
n +=1
e = EulersNumber(n)
print('n={} \te= {} \tdelta(e)={}'.format(n,e.e,abs(e.e-math.e)))
return e

def EulersNumberToAccuracy(PowerOfTen):
x = 1
theGuess = 1
thisE = EulersNumber(1)
while x <= abs(PowerOfTen):
thisE = EulerStepWithGuess(10**(-1*x),theGuess)
theGuess = thisE.eulerSteps * 10
x += 1
return thisE

Thanks,
Derek
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Optimizing Memory Allocation in a Simple, but Long Function

2016-04-24 Thread Derek Klinge
Sorry about the code indentation, I was using Pythonista (iOS), and it did
not have any problem with that indentation...

Here is a new set of the code:
## Write a method to approximate Euler's Number using Euler's Method
import math

class EulersNumber():
def __init__(self,n):
self.eulerSteps = n
self.e = self.EulersMethod(self.eulerSteps)
def linearApproximation(self,x,h,d): # f(x+h)=f(x)+h*f'(x)
return x + h * d
def EulersMethod(self, numberOfSteps): # Repeate linear approximation over
an even range
e = 1 # e**0 = 1
for step in range(numberOfSteps):
e = self.linearApproximation(e,1.0/numberOfSteps,e) # if f(x)= e**x,
f'(x)=f(x)
return e

def EulerStepWithGuess(accuracy,guessForN):
n = guessForN
e = EulersNumber(n)
while abs(e.e - math.e) > abs(accuracy):
n +=1
e = EulersNumber(n)
print('n={} \te= {} \tdelta(e)={}'.format(n,e.e,abs(e.e - math.e)))
return e

def EulersNumberToAccuracy(PowerOfTen):
x = 1
theGuess = 1
thisE = EulersNumber(1)
while x <= abs(PowerOfTen):
thisE = EulerStepWithGuess(10**(-1*x),theGuess)
theGuess = thisE.eulerSteps * 10
x += 1
return thisE

My problem is this: my attempt at Euler's Method involves creating a list
of numbers that is n long. Is there a way I can iterate over the linear
approximation method without creating a list of steps (maybe recursion, I
am a bit new at this). Ideally I'd like to perform the linearApproximation
method a arbitrary number of times (hopefully >10**10) and keep feeding the
answers back into itself to get the new answer. I know this will be
computationally time intensive, but how do I minimize memory usage (limit
the size of my list)? I also may be misunderstanding the problem, in which
case I am open to looking at it from a different perspective.

Thanks,
Derek

On Sun, Apr 24, 2016 at 9:22 AM Chris Angelico  wrote:

> On Sun, Apr 24, 2016 at 1:05 PM, Derek Klinge 
> wrote:
> > I have been writing a python script to explore Euler's Method of
> > approximating Euler's Number. I was hoping there might be a way to make
> > this process work faster, as for sufficiently large eulerSteps, the
> process
> > below becomes quite slow and sometimes memory intensive. I'm hoping
> someone
> > can give me some insight as to how to optimize these algorithms, or ways
> I
> > might decrease memory usage. I have been thinking about finding a way
> > around importing the math module, as it seems a bit unneeded except as an
> > easy reference.
>
> Are you sure memory is the real problem here?
>
> (The first problem you have, incidentally, is a formatting one. All
> your indentation has been lost. Try posting your code again, in a way
> that doesn't lose leading spaces/tabs, and then we'll be better able
> to figure out what's going on.)
>
> If I'm reading your code correctly, you have two parts:
>
> 1) class EulersNumber, which iterates up to some specific count
> 2) Module-level functions, which progressively increase the count of
> constructed EulersNumbers.
>
> Between them, you appear to have an O(n*n) algorithm for finding a
> "sufficiently-accurate" representation. You're starting over from
> nothing every time. If, instead, you were to start from the previous
> approximation and add another iteration, that ought to be immensely
> faster.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Optimizing Memory Allocation in a Simple, but Long Function

2016-04-24 Thread Derek Klinge
I think my e-mail client may be stripping the indentation, here it is with
4-space indentation

## Write a method to approximate Euler's Number using Euler's Method
import math

class EulersNumber():
def __init__(self,n):
self.eulerSteps = n
self.e = self.EulersMethod(self.eulerSteps)
def linearApproximation(self,x,h,d): # f(x+h)=f(x)+h*f'(x)
return x + h * d
def EulersMethod(self, numberOfSteps): # Repeate linear approximation over
an even range
e = 1 # e**0 = 1
for step in range(numberOfSteps):
e = self.linearApproximation(e,1.0/numberOfSteps,e) # if f(x)= e**x,
f'(x)=f(x)
return e

def EulerStepWithGuess(accuracy,guessForN):
n = guessForN
e = EulersNumber(n)
while abs(e.e - math.e) > abs(accuracy):
n +=1
e = EulersNumber(n)
print('n={} \te= {} \tdelta(e)={}'.format(n,e.e,abs(e.e - math.e)))
return e

def EulersNumberToAccuracy(PowerOfTen):
x = 1
theGuess = 1
thisE = EulersNumber(1)
while x <= abs(PowerOfTen):
thisE = EulerStepWithGuess(10**(-1*x),theGuess)
theGuess = thisE.eulerSteps * 10
x += 1
return thisE
On Sun, Apr 24, 2016 at 10:02 AM Derek Klinge  wrote:

> Sorry about the code indentation, I was using Pythonista (iOS), and it did
> not have any problem with that indentation...
>
> Here is a new set of the code:
> ## Write a method to approximate Euler's Number using Euler's Method
> import math
>
> class EulersNumber():
> def __init__(self,n):
> self.eulerSteps = n
> self.e = self.EulersMethod(self.eulerSteps)
> def linearApproximation(self,x,h,d): # f(x+h)=f(x)+h*f'(x)
> return x + h * d
> def EulersMethod(self, numberOfSteps): # Repeate linear approximation over
> an even range
> e = 1 # e**0 = 1
> for step in range(numberOfSteps):
> e = self.linearApproximation(e,1.0/numberOfSteps,e) # if f(x)= e**x,
> f'(x)=f(x)
> return e
>
> def EulerStepWithGuess(accuracy,guessForN):
> n = guessForN
> e = EulersNumber(n)
> while abs(e.e - math.e) > abs(accuracy):
> n +=1
> e = EulersNumber(n)
> print('n={} \te= {} \tdelta(e)={}'.format(n,e.e,abs(e.e - math.e)))
> return e
>
> def EulersNumberToAccuracy(PowerOfTen):
> x = 1
> theGuess = 1
> thisE = EulersNumber(1)
> while x <= abs(PowerOfTen):
> thisE = EulerStepWithGuess(10**(-1*x),theGuess)
> theGuess = thisE.eulerSteps * 10
> x += 1
> return thisE
>
> My problem is this: my attempt at Euler's Method involves creating a list
> of numbers that is n long. Is there a way I can iterate over the linear
> approximation method without creating a list of steps (maybe recursion, I
> am a bit new at this). Ideally I'd like to perform the linearApproximation
> method a arbitrary number of times (hopefully >10**10) and keep feeding the
> answers back into itself to get the new answer. I know this will be
> computationally time intensive, but how do I minimize memory usage (limit
> the size of my list)? I also may be misunderstanding the problem, in which
> case I am open to looking at it from a different perspective.
>
> Thanks,
> Derek
>
> On Sun, Apr 24, 2016 at 9:22 AM Chris Angelico  wrote:
>
>> On Sun, Apr 24, 2016 at 1:05 PM, Derek Klinge 
>> wrote:
>> > I have been writing a python script to explore Euler's Method of
>> > approximating Euler's Number. I was hoping there might be a way to make
>> > this process work faster, as for sufficiently large eulerSteps, the
>> process
>> > below becomes quite slow and sometimes memory intensive. I'm hoping
>> someone
>> > can give me some insight as to how to optimize these algorithms, or
>> ways I
>> > might decrease memory usage. I have been thinking about finding a way
>> > around importing the math module, as it seems a bit unneeded except as
>> an
>> > easy reference.
>>
>> Are you sure memory is the real problem here?
>>
>> (The first problem you have, incidentally, is a formatting one. All
>> your indentation has been lost. Try posting your code again, in a way
>> that doesn't lose leading spaces/tabs, and then we'll be better able
>> to figure out what's going on.)
>>
>> If I'm reading your code correctly, you have two parts:
>>
>> 1) class EulersNumber, which iterates up to some specific count
>> 2) Module-level functions, which progressively increase the count of
>> constructed EulersNumbers.
>>
>> Between them, you appear to have an O(n*n) algorithm for finding a
>> "sufficiently-accurate" representation. You're starting over from
>> nothing every time. If, instead, you were to start from the previous
>> approximation and add another iteration, that ought to be immensely
>> faster.
>>
>> ChrisA
>> --
>> https://mail.python.org/mailman/listinfo/python-list
>>
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Optimizing Memory Allocation in a Simple, but Long Function

2016-04-24 Thread Derek Klinge
Doesn't range(n) create a list n long?
On Sun, Apr 24, 2016 at 10:21 AM Chris Angelico  wrote:

> On Mon, Apr 25, 2016 at 3:02 AM, Derek Klinge 
> wrote:
> > My problem is this: my attempt at Euler's Method involves creating a
> list of
> > numbers that is n long. Is there a way I can iterate over the linear
> > approximation method without creating a list of steps (maybe recursion,
> I am
> > a bit new at this). Ideally I'd like to perform the linearApproximation
> > method a arbitrary number of times (hopefully >10**10) and keep feeding
> the
> > answers back into itself to get the new answer. I know this will be
> > computationally time intensive, but how do I minimize memory usage (limit
> > the size of my list)? I also may be misunderstanding the problem, in
> which
> > case I am open to looking at it from a different perspective.
>
> def EulersMethod(self, numberOfSteps): # Repeate linear approximation
> over an even range
> e = 1 # e**0 = 1
> for step in range(numberOfSteps):
> e = self.linearApproximation(e,1.0/numberOfSteps,e) # if f(x)=
> e**x, f'(x)=f(x)
> return e
>
> This is your code, right?
>
> I'm not seeing anywhere in here that creates a list of numbers. It
> does exactly what you're hoping for: it feeds the answer back to
> itself for the next step.
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: Optimizing Memory Allocation in a Simple, but Long Function

2016-04-24 Thread Derek Klinge
Actually, I'm not trying to speed it up, just be able to handle a large
number of n.
(Thank you Chris for the suggestion to use xrange, I am on a Mac using the
stock Python 2.7)

I am looking at the number of iterations of linear approximation that are
required to get a more accurate representation.
My initial data suggest that to get 1 more digit of e (the difference
between the calculated and expected value falls under 10**-n), I need a
little more than 10 times the number of iterations of linear approximation.

I actually intend to compare these to other methods, including limit
definition that you provided, as well as the geometric series definition.

I am trying to provide some real world data for my students to prove the
point that although there are many ways to calculate a value, some are much
more efficient than others.

I tried your recommendation (Oscar) of trying a (1+1/n)**n approach, which
gave me very similar values, but when I took the difference between your
method and mine I consistently got differences of ~10**-15. Perhaps this is
due the binary representation of the decimals?

Also, it seems to me if the goal is to use the smallest value of n to get a
particular level of accuracy, changing your guess of N by doubling seems to
have a high chance of overshoot. I found that I was able to predict
relatively accurately a value of N for achieving a desired accuracy. By
this I mean, that I found that if I wanted my to be accurate to one
additional decimal place I had to multiply my value of N by approximately
10 (I found that the new N required was always < 10N +10).

Derek

On Sun, Apr 24, 2016 at 4:45 PM, Derek Klinge  wrote:

> Actually, I'm not trying to speed it up, just be able to handle a large
> number of n.
> (Thank you Chris for the suggestion to use xrange, I am on a Mac using the
> stock Python 2.7)
>
> I am looking at the number of iterations of linear approximation that are
> required to get a more accurate representation.
> My initial data suggest that to get 1 more digit of e (the difference
> between the calculated and expected value falls under 10**-n), I need a
> little more than 10 times the number of iterations of linear approximation.
>
> I actually intend to compare these to other methods, including limit
> definition that you provided, as well as the geometric series definition.
>
> I am trying to provide some real world data for my students to prove the
> point that although there are many ways to calculate a value, some are much
> more efficient than others.
>
> Derek
>
> On Sun, Apr 24, 2016 at 2:55 PM, Oscar Benjamin <
> oscar.j.benja...@gmail.com> wrote:
>
>> On 24 April 2016 at 19:21, Chris Angelico  wrote:
>> > On Mon, Apr 25, 2016 at 4:03 AM, Derek Klinge 
>> wrote:
>> >> Ok, from the gmail web client:
>> >
>> > Bouncing this back to the list, and removing quote markers for other
>> > people's copy/paste convenience.
>> >
>> > ## Write a method to approximate Euler's Number using Euler's Method
>> > import math
>> >
>> > class EulersNumber():
>> > def __init__(self,n):
>> > self.eulerSteps = n
>> > self.e= self.EulersMethod(self.eulerSteps)
>> > def linearApproximation(self,x,h,d): # f(x+h)=f(x)+h*f'(x)
>> > return x + h * d
>> > def EulersMethod(self, numberOfSteps): # Repeate linear
>> > approximation over an even range
>> > e = 1# e**0
>> = 1
>> > for step in range(numberOfSteps):
>> > e = self.linearApproximation(e,1.0/numberOfSteps,e) # if
>> > f(x)= e**x, f'(x)=f(x)
>> > return e
>> >
>> >
>> > def EulerStepWithGuess(accuracy,guessForN):
>> > n = guessForN
>> > e = EulersNumber(n)
>> > while abs(e.e - math.e) > abs(accuracy):
>> > n +=1
>> > e = EulersNumber(n)
>> > print('n={} \te= {} \tdelta(e)={}'.format(n,e.e,abs(e.e -
>> math.e)))
>> > return e
>> >
>> >
>> > def EulersNumberToAccuracy(PowerOfTen):
>> > x = 1
>> > theGuess = 1
>> > thisE = EulersNumber(1)
>> > while x <= abs(PowerOfTen):
>> > thisE = EulerStepWithGuess(10**(-1*x),theGuess)
>> > theGuess = thisE.eulerSteps * 10
>> > x += 1
>> > return thisE
>> >
>> >
>> >> To see an example of my problem try something like
>> EulersNumberToAccuracy(-10)
>>
>> Now that I can finally see your code I can see what t

Re: Optimizing Memory Allocation in a Simple, but Long Function

2016-04-24 Thread Derek Klinge
So I tried the recommended limit approach and got some interesting results.

## Write a method to approximate Euler's Number using Euler's Method
import math

class EulersNumber():
def __init__(self,n):
self.n = n
self.e = 2
def linearApproximation(self,x,h,d): # f(x+h)=f(x)+h*f'(x)
return x + h * d
def EulersMethod(self): # Repeat linear approximation over an even range
e = 1 # e**0 = 1
for step in xrange(self.n):
e = self.linearApproximation(e,1.0/self.n,e) # if f(x)= e**x, f'(x)=f(x)
self.e = e
return e
def LimitMethod(self):
self.e = (1 + 1.0/self.n) ** self.n
return self.e
def SeriesMethod(self):
self.e = sum([1.0/math.factorial(i) for i in range(self.n+1)])
return self.e

I found that the pattern of an additional digit of accuracy corresponding
to 10*n did not hold as strongly for that value (I can post data if
desired). I also got some results that seem to contradict the mathematical
definition. For example try EulersNumber(10**15).LimitMethod(), the
definition places this limit at e, and yet the (python) answer is >3.035.
Please let me know if I've fouled up the implementation somehow.

Also my reasoning for writing this up as a class was to be able to get the
value of n used to generate that value e. If there is some other way to do
that, I'd be happy to try it out.

Thanks,
Derek

Derek

On Sun, Apr 24, 2016 at 8:12 PM, Derek Klinge  wrote:

> Actually, I'm not trying to speed it up, just be able to handle a large
> number of n.
> (Thank you Chris for the suggestion to use xrange, I am on a Mac using the
> stock Python 2.7)
>
> I am looking at the number of iterations of linear approximation that are
> required to get a more accurate representation.
> My initial data suggest that to get 1 more digit of e (the difference
> between the calculated and expected value falls under 10**-n), I need a
> little more than 10 times the number of iterations of linear approximation.
>
> I actually intend to compare these to other methods, including limit
> definition that you provided, as well as the geometric series definition.
>
> I am trying to provide some real world data for my students to prove the
> point that although there are many ways to calculate a value, some are much
> more efficient than others.
>
> I tried your recommendation (Oscar) of trying a (1+1/n)**n approach, which
> gave me very similar values, but when I took the difference between your
> method and mine I consistently got differences of ~10**-15. Perhaps this is
> due the binary representation of the decimals?
>
> Also, it seems to me if the goal is to use the smallest value of n to get
> a particular level of accuracy, changing your guess of N by doubling seems
> to have a high chance of overshoot. I found that I was able to predict
> relatively accurately a value of N for achieving a desired accuracy. By
> this I mean, that I found that if I wanted my to be accurate to one
> additional decimal place I had to multiply my value of N by approximately
> 10 (I found that the new N required was always < 10N +10).
>
> Derek
>
> On Sun, Apr 24, 2016 at 4:45 PM, Derek Klinge 
> wrote:
>
>> Actually, I'm not trying to speed it up, just be able to handle a large
>> number of n.
>> (Thank you Chris for the suggestion to use xrange, I am on a Mac using
>> the stock Python 2.7)
>>
>> I am looking at the number of iterations of linear approximation that are
>> required to get a more accurate representation.
>> My initial data suggest that to get 1 more digit of e (the difference
>> between the calculated and expected value falls under 10**-n), I need a
>> little more than 10 times the number of iterations of linear approximation.
>>
>> I actually intend to compare these to other methods, including limit
>> definition that you provided, as well as the geometric series definition.
>>
>> I am trying to provide some real world data for my students to prove the
>> point that although there are many ways to calculate a value, some are much
>> more efficient than others.
>>
>> Derek
>>
>> On Sun, Apr 24, 2016 at 2:55 PM, Oscar Benjamin <
>> oscar.j.benja...@gmail.com> wrote:
>>
>>> On 24 April 2016 at 19:21, Chris Angelico  wrote:
>>> > On Mon, Apr 25, 2016 at 4:03 AM, Derek Klinge 
>>> wrote:
>>> >> Ok, from the gmail web client:
>>> >
>>> > Bouncing this back to the list, and removing quote markers for other
>>> > people's copy/paste convenience.
>>> >
>>> > ## Write a method to approximate Euler's Number using Euler's Method
>>> > import math
>>> >
>>> > class EulersNumber():
>>&

Re: Optimizing Memory Allocation in a Simple, but Long Function

2016-04-25 Thread Derek Klinge
A couple thoughts. I think my original approach would be faster than binary
search for finding the minimum value of N needed to get a decimal level of
absolute accuracy from Euler's number. Here is my reasoning:
EulerlersNumber(13).LimitMethod() - math.e < .1 and
EulersNumber(135).LimitMethod - math.e < .01. N increased by a little more
than a factor of 10. My method would use 130, 131, 132, 133, 134, and 135
as guesses after 13. Using the double + binary search the guesses would be
26, 52, 104, 208, 156, 130, 143, 137, 134, 136, and then 136 after using
the information that n=13 gives an tolerance of < .1. If the trend holds,
then to get the next decimal of accuracy, you would need to do less than 10
searches each time. When I was using my linear approximation method I found
that I never had to do more than 10 additional guesses of N to get the next
digit of accuracy. When I was using EulersMethod() I found this trend held
for as long as I would allow my computer to do the calculations (though
accuracy to 10**-10).

What I find very interesting is that although the limit method is
mathematically equivalent to the linear approximation method, they give
different results, given sufficiently high values of N (at least in
Python). The limit method does not follow my predictions as accurately,
which leads to the question of whether or not the phenomenon I observed was
an artifact of rounding error or not.

Also, my explicit goal is to be able to handle large numbers of N, and to
reduce rounding error to a minimum. Using the fractions module to perform
the limit method with rational numbers rather than binary represented
decimals and got an error that the integer was too long to convert to float
and even when using smaller values of n (10**14) I seem to get values that
are not the same as when using decimals. It seems to me (I'm just thinking
about this at a low level and haven't written out any math to justify it
yet) that because the linear approximation method only multiplies and adds
the rounding error is smaller than in the limit method where numbers are
being taken to exponents that have rounding errors.

Although I see the value of relative error, I am just as interested in
absolute error (though admittedly they are directly related values).

Are there modules or libraries I can/should use to minimize rounding error
and use very large values of N and get an accurate answer? How does the
math module calculate the vale of e?

Thanks,
Derek

On Mon, Apr 25, 2016 at 6:49 AM Oscar Benjamin 
wrote:

> On 25 April 2016 at 08:39, Gregory Ewing 
> wrote:
> > Derek Klinge wrote:
> >>
> >> Also, it seems to me if the goal is to use the smallest value of n to
> get
> >> a
> >> particular level of accuracy, changing your guess of N by doubling seems
> >> to
> >> have a high chance of overshoot.
> >
> >
> > If you want to find the exact n required, once you overshoot
> > you could use a binary search to narrow it down.
>
> Also you can calculate the truncation error for Euler's method. Since
>
>f(t) = f(t0) + f'(t0)*(t - t0) + (1/2)f''(t0)*(t - t0)**2 + O((t -
> t0)**3)
>
> Euler's method just uses the first two terms so
>
> x[n+1] = x[n] + dt*f(x[n])
>
> the next term would be
>
>(1/2)*f'(x[n])*dt**2
>
> Since in your case f'(x) = x and dt = 1/N that's
>
> (1/2)*x[n]*(1/N)**2
>
> As a relative error (divide by x[n]) that's
>
> (1/2)*(1/N)**2
>
> Let's add the relative error from N steps to get
>
> N*(1/2)*(1/N)**2 = 1/(2*N)
>
> So the relative error integrating from 0 to 1 with N steps is 1/(2*N).
> If we want a relative error of epsilon then the number of steps needed
> is 1/(2*epsilon).
>
> That is to say that for a relative error of 1e-4 we need N =
> 1/(2*1e-4) = 1e4/2 = 5e3 = 5000.
>
> >>> import math
> >>> N = 5000
> >>> error = math.e - (1 + 1.0/N)**N
> >>> relative_error = error / math.e
> >>> relative_error
> 9.998167027596845e-05
>
> Which is approximately 1e-4 as required.
>
> --
> Oscar
> --
> https://mail.python.org/mailman/listinfo/python-list
>
-- 
https://mail.python.org/mailman/listinfo/python-list


SMB Authentication Module

2005-10-07 Thread Derek Perriero
This may be a simple question to answer, but is there any module that
will let you authenticate against a SMB server?  An equivalent
package in perl would be the Authen::SMB.  That is the realms of
what I am looking for, but in Python.

-Derek Perriero-- Perriero, Derek[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list

Best way to handle cgi sessions

2005-10-14 Thread Derek Perriero
What would be the best way to create a cgi session that contains the
basic elements of a cookie and can also hold secure data, such as a
username/password.  I've explored the possibilities of using
SmartCookie, but that doesn't encrypt my parameters.Here's some background info on what I'm trying to do:form = cgi.FieldStorage()
try: username = form["username"].value
 password  = form["password"].value
except KeyError: print "Please Enter Username/Password"
## authenticate against LDAP serverif not authen.ldap(username,password):

cookie =
0  
#Cookie dies and page goes back to re-login display = functions.display_html_admin("main_temp.html")
 functions.print_html_header(cookie) print display
 exitelse:
 cookie = authen.genCookie(username) display = functions.display_html_admin("main_temp.html")
 functions.print_html_header(cookie) print display
 exit

...
## From authen.py

def genCookie(username):
    cookie = SmartCookie()

    cookie["CGISESSID"] = generate_hash()
    cookie["CGISESSID"]["path"] = "/tmp"
    cookie["logintype"] = "admin"
    cookie["username"]  = username
    cookie["loggedin"]  = "verified"
   
    return(cookie)

#end:
 -- Perriero, Derek[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Best way to handle cgi sessions

2005-10-15 Thread Derek Perriero
Christoph,



I really appreciate the effort.  I've been searching for something
of this magnitude for awhile.  See, I would normally use
CGI::Session for the equivalent in Perl.  That is what I am trying
to achieve.  I'll give that script a go and see what I can come up
with.  Luckily, the data I store is kept in MySQL, so that's even
better.  Regards to your efforts to the community!



Cheers,

Derek

P.S. Sorry for the repeat, this e-mail was never sent  to the list, Thanks! 
On 10/15/05, Derek Perriero <[EMAIL PROTECTED]> wrote:
Christoph,

I really appreciate the effort.  I've been searching for something
of this magnitude for awhile.  See, I would normally use
CGI::Session for the equivalent in Perl.  That is what I am trying
to achieve.  I'll give that script a go and see what I can come up
with.  Luckily, the data I store is kept in MySQL, so that's even
better.  Regards to your efforts to the community!

Cheers,
DerekOn 10/14/05, Christoph Haas <
[EMAIL PROTECTED]> wrote:
On Friday 14 October 2005 21:22, Derek Perriero wrote:> What would be the best way to create a cgi session that contains the> basic elements of a cookie and can also hold secure data, such as a> username/password. [...]
Said. Done. I just tidied up a module we will be using for a web sitehere. It uses the promised session-cookie handling (with a lot of securitychecks) and a MySQL database backend. I tried to document as well as
possibleso I hope it will even help you if you use it or not. To try it out justgrab the two files from 
http://workaround.org/pysessions/ and copy theminto a
cgi-bin/ directory. Create a database and the two tables following thescheme described on top of the MySessions.py module. Then access the/cgi-bin/mypage CGI. Hope to have helped.Everyone:This is my first "bigger" Python script. I would really like to hear
commentson it. If it's deemed to be decent I could use some help making it apackagethat can be used by others as well. There is probably a bit of perlishnessthat I'd like to get rid of to make it look more snake-like.
Regards Christoph--~~".signature"
[Modified] 1 line
--100%--1,48    
All--http://mail.python.org/mailman/listinfo/python-list
-- Perriero, Derek

[EMAIL PROTECTED]

-- Perriero, Derek[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list

LDAP Authentication

2005-12-02 Thread Derek Perriero
Any help would be great on this.  I've been trying to bind a
username and password to the ldap server for authentication, but when I
locally run this script:

#!/usr/bin/python
import ldap

## Connect to LDAP host
try:
    ldapcn = ldap.initialize('ldap://xxx.xxx.xxx.xxx')
    ldapcn.bind('cn=username,o=domain','password',ldap.AUTH_SIMPLE)
    print "Successful."
except ldap.LDAPError, e:
    print e
    print "Declined.\n"

I always get a successful return even if the password or username is
incorrect.  Or, if I even leave the username and password field in
the initialize function blank, I get a successful return.
I need some guidance in the correct direction.

Thanks!
Derek-- Perriero, Derek[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list

CGI, anydbm.open() and linux file permissions

2005-01-11 Thread Derek Basch
Hello,

I have a CGI script which uses anydb.open() to create a DBM. However I get this
traceback:

 /usr/lib/python2.3/bsddb/__init__.py in
hashopen(file='/var/www/bp/predictor/tools.dbm', flag='c', mode=438,
pgsize=None, ffactor=None, nelem=None, cachesize=None, lorder=None, hflags=0)
  190 if ffactor is not None:   d.set_h_ffactor(ffactor)
  191 if nelem is not None: d.set_h_nelem(nelem)
  192 d.open(file, db.DB_HASH, flags, mode)
  193 return _DBWithCursor(d)
  194 
d = , d.open = , file =
'/var/www/bp/predictor/tools.dbm', global db = , db.DB_HASH = 2, flags = 65, mode =
438

DBAccessError: (13, 'Permission denied')
  args = (13, 'Permission denied')

The permissions on the CGI script (/usr/lib/cgi-bin/evaluator.py) are:

-rwxr-xr-x   1 bpeters bpeters  2446 Jan 11 14:42 evaluator.py

and the permissions on the target DBM creation directory
(/var/www/bp/predictor/) are:

drwxr-xr-x  2 bpeters bpeters  4096 Jan 11 14:45 predictor

Can anyone tell me what I need to do to allow the CGI script to create the DBM
in the target directory? That is short of changing everything to root.

Thanks everyone,
Derek Basch





__ 
Do you Yahoo!? 
Meet the all-new My Yahoo! - Try it today! 
http://my.yahoo.com 
 

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


spawn syntax + os.P_WAIT mode behavior + spawn stdout redirection

2005-01-20 Thread Derek Basch
Hello,

*First question*

If the syntax of spawnl is:

spawnl(mode, path, ...)

Why does everyone write it like:

os.spawnlp(os.P_WAIT, 'cp', 'cp', 'index.html', '/dev/null')

or:

os.spawnl(os.P_WAIT, "/var/www/db/smm/smm_train", "smm_train",
"SMMTrainInput.xml")

How is the first 'cp' a path to a file? why does the desired executable have to
be named again as the first parameter?

*Second question*

I have a script test.py which calls another script sleep.py using a spawn.

--
#test.py
import os

os.spawnv(os.P_WAIT, "/var/www/db/cgi-bin/sleep.py", ["python", "sleep.py"])
#pid = os.spawnl(os.P_WAIT, 'sh', 'sh', '-cv', 'sleep 10; echo fark >
/tmp/test.out')
--

--
#sleep.py
import time

time.sleep(10)
--

I would expect that the test.py script should take 10sec to return. However it
returns immediatly. Perhaps I am calling the sleep.py script incorrectly?
Shouldn't it take 10sec to execute since the spawn mode argument is os.P_WAIT?

*Third question*

If I uncomment the second spawn call in test.py I do not get any output to
/tmp/test.out and it also returns immediatly. Can anyone tell me why? 

Thank You Mighty Python Guru's,
Derek Basch



__ 
Do you Yahoo!? 
Yahoo! Mail - now with 250MB free storage. Learn more.
http://info.mail.yahoo.com/mail_250
-- 
http://mail.python.org/mailman/listinfo/python-list


CGI and HTTP Header Location redirects

2005-02-03 Thread Derek Basch
Hello,

I have been dealing with some strange behavior using CGI, python and an HTTP
Header "Location:" redirect on an Apache 1.3 server.

If I call a CGI script and perform a "Location:" redirect the script seems to
silently run off the tracks immediately after the redirect. For example "0.xml"
and "1.xml" are created when there is no redirect and only "0.xml" is created
when a redirect does occur (see below).

Also, after enabling suEXEC on the apache server the script executes perfectly
with the redirect. Can anyone explain this behavior? I would guess that it is
related to apache user rights but I can't find any reference to such problems
via Google.

Thanks everyone!,
Derek Basch

-

#! /usr/bin/python

import sys
import cgitb

class Trainer:

def train(self):
fs = open("/tmp/0.xml", "w")
fs.close()
##  print "Location: " + "http://www.yahoo.com"; + "\n\n"
sys.stdout.flush()
fs = open("/tmp/1.xml", "w")
fs.close()

def main():
try:
cgitb.enable()  
trainer = Trainer()
trainer.train()

except Exception, inst:
cgitb.handler()

if __name__ == '__main__':
main()




__ 
Do you Yahoo!? 
Take Yahoo! Mail with you! Get it on your mobile phone. 
http://mobile.yahoo.com/maildemo 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: CGI and HTTP Header Location redirects

2005-02-04 Thread Derek Basch
A... I should have been more specific before. The Apache error log doesn't
produce an error. You are probably correct that it is most likely an
Apache/Unix problem. I thought perhaps someone had maybe run into this before
since it seems like such a basic function to not work. Thanks for the help.

Paul Rubin wrote:
> Derek Basch <[EMAIL PROTECTED]> writes:
> 
>>Also, after enabling suEXEC on the apache server the script executes
>>perfectly with the redirect. Can anyone explain this behavior? I
>>would guess that it is related to apache user rights but I can't
>>find any reference to such problems via Google.
> 
> 
> Apache probably isn't able to run the cgi because of lack of exec
> permission or something like that.  The best way to diagnose such
> probs is by checking the apache error log, if you have access to it.
> 
> Best newsgroup for this type of question is
> comp.infosystems.www.servers.unix.  Lots of apache experts hang out there.



__ 
Do you Yahoo!? 
Yahoo! Mail - now with 250MB free storage. Learn more.
http://info.mail.yahoo.com/mail_250
-- 
http://mail.python.org/mailman/listinfo/python-list


A few SWIG/Python questions

2005-03-06 Thread Derek Allen
I'm using SWIG to generate glue code for my embedded/extended python app. 
There are a couple of very frustrating issues that I would like to see if 
anyone else has solutions for:

- Once I have created and initialized a module with SWIG I can call it by 
executing a python file with no problems. However I would like to be able to 
type in a single-line python function and have it execute from my module. 
One line cannot contain both "import _mypackage" and 
"_mypackage.DoFunction("hello")" (and even if it could it would be 
cumbersome). I cannot find a way to manually import (in code) the methods 
from my package prior to executing the single-line instruction. I can get 
the dictionary but that doesn't give me the methods. How do I do this?

- For some reason whatever name I give the module (via %module name) ends up 
with an underscore at its beginning - e.g., _name. I don't know why this is, 
and it was the source of some serious hair-pulling. Is there a way to turn 
this off (without recompiling the swig executable)?

- SWIG in general generates a TON of code for the glue module. Is there a 
way to reduce this?

- SWIG wraps everything with extern "C", which is unnecessary for my code 
and requires me to decorate functions unnecessarily. Is there a way to turn 
this off?

Thanks. 


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


Minidom empty script element bug

2005-03-15 Thread Derek Basch
Hello All,

I ran into a problem while dynamically constructing XHTML documents using
minidom. If you create a script tag such as:

script_node_0 = self.doc.createElement("script")
script_node_0.setAttribute("type", "text/javascript")
script_node_0.setAttribute("src", "../test.js")

minidom renders it as:



Which is incorrect because:

XHTML 1.0 specs, Appendix C
[EMAIL PROTECTED]
C.3 Element Minimization and Empty Element Content

Given an empty instance of an element whose content model is not EMPTY (for
example, an empty title or paragraph) do not use the minimized form (e.g.
use   and not )
[EMAIL PROTECTED]

reference for further explanation:
http://lists.evolt.org/archive/Week-of-Mon-20020304/105951.html

So, the rendered page completely fails on IE6 because it actually handles the
empty script element correctly. Mozilla handles the element incorrectly and
instantiates the javascript.

How do I get minidom to NOT render an empty script element? Should I submit a
bug report?

Thanks for the help,
Derek Basch






__ 
Do you Yahoo!? 
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/ 
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Minidom empty script element bug

2005-03-15 Thread Derek Basch

Martin v. Löwis wrote:
> Derek Basch wrote:
> > XHTML 1.0 specs, Appendix C
> > [EMAIL PROTECTED]
> > C.3 Element Minimization and Empty Element Content
> >
> > Given an empty instance of an element whose content model is not
EMPTY (for
> > example, an empty title or paragraph) do not use the minimized form
(e.g.
> > use   and not )
> > [EMAIL PROTECTED]
>
> I'd like to point out that this is *not* a minidom bug. minidom
cannot
> possibly know that the document type is XHTML, and that strange,
non-XML
> rules apply to XHTML (i.e. rules which are not present in XML
itself).
>
> I'd also like to point out that XHTML Appendix C is informative (i.e.
> non-normative), meaning that failure to comply to it does not imply
> non-compliance with XHTML. An XML file which uses the minimized form
> for the script element is still proper, well-formed, valid XHTML.
>
> > How do I get minidom to NOT render an empty script element? Should
I submit a
> > bug report?
>
> That said, I think there is a simple solution: add an empty Text node
> to the script element:
>
> script_node_0.appendChild(doc.createText(u""))
>
> [Disclaimer: this is untested; from reading the source, I think it
> should work]
>
> Regards,
> Martin


Thanks Martin. That fixed it. I had to change your code a bit to this:

script_node_0.appendChild(self.doc.createTextNode(""))

maybe you meant createTextNode?

I started digging through the dom modules on this path:

XHTMLPrettyPrint -> XHTMLPrinter -> Printer

and found this comment:

try:
#The following stanza courtesy Martin von Loewis
import codecs # Python 1.6+ only
from types import UnicodeType

So I guess you are pretty qualified to answer my question! You are
correct that this is not a minidom bug now that I think about it.

However, it seems proper that XHTMLPrinter (or some other module)
should allow the developer to use either normative or non-normative
XHTML design guidlines to achieve some sane degree of HTML user agent
compatablilty. Maybe something like this in Printer.py:

def visitElement(self, node):
...
if len(node.childNodes):
self._write('>')
self._depth = self._depth + 1
self.visitNodeList(node.childNodes)
self._depth = self._depth - 1
if not self._html or (node.tagName not in
HTML_FORBIDDEN_END):
not (self._inText and inline) and self._tryIndent()
self._write('' % node.tagName)
elif not self._html and node.tagName not in
XHTML_NON_NORMATIVES:
self._write('/>')
elif node.tagName not in HTML_FORBIDDEN_END:
self._write('>' % node.tagName)
else:
self._write('>')

of course this would only take care of the "C.3. Element Minimization
and Empty Element Content" guideline but you get the general idea.

Anyways, thanks for the help again and feel free to shoot down my
suggestions :)

Derek Basch

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


Re: Minidom empty script element bug

2005-03-16 Thread Derek Basch
Cross post from XML-SIG:

--- Walter Dörwald <[EMAIL PROTECTED]> wrote:
> Martin v. Löwis sagte:
> > Derek Basch wrote:
> > >[...]
> >> How do I get minidom to NOT render an empty script element? Should
I
> submit a bug report?
> >
> > That said, I think there is a simple solution: add an empty Text
node to
> the script element:
> >
> > script_node_0.appendChild(doc.createText(u""))
> >
> > [Disclaimer: this is untested; from reading the source, I think it
should
> work]
>
> If this doesn't work, you might want to try XIST
> (http://www.livinglogic.de/Python/xist)
> instead of minidom. XIST knows that the script element is not EMPTY,
and when
> the
> output is in HTML compatible XML an end tag will be produced:
>
> >> from ll.xist.ns import html
> >>> print html.script(type="text/javascript",
> src="../test.js").asBytes(xhtml=1)
> 
>
> Using pure XML mode gives:
>
> >>> print html.script(type="text/javascript",
> src="../test.js").asBytes(xhtml=2)
> 
>
> Bye,
>Walter Dörwald

Wow! XIST is very elegant. Perfectly designed for what it is supposed
to do.

"XIST is an extensible HTML/XML generator written in Python."

I guess there isn't much point in "fixing" the pyXML XHTMLPrinter when
something as cool as XIST exists (pun intended).

Kid also seems really neat. I like the TAL like features. However, it
seems less mature than XIST.

There seems to be lots of functionality crossover between the two but
it is good that there is enough demand for XML output functionality in
python to support two distinct modules.

Thanks Everyone!,
Derek Basch

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


string join() method

2005-03-23 Thread Derek Basch
Can anyone tell me why this CGI code outputs a blank page?

self.output = []
self.setContentType("text/plain")
ascii_temp.seek(0)
self.output.extend(ascii_temp.read())
print ''.join(self.output)

def setContentType(self, type="text/xml"):
self.output.extend(["Content-type: ", type, "\n\r"])
-

but this code works?:
-
self.output = []
self.setContentType("text/plain")
print ''.join(self.output)
ascii_temp.seek(0)
print ascii_temp.read()

def setContentType(self, type="text/xml"):
self.output.extend(["Content-type: ", type, "\n\r"])
-

ascii_temp is just some tab seperated data:
-
Allele  Seq:Start-End   Length  SequenceScore
A01 1: 1-8  8   1410538.0
A01 1: 2-9  8   1410538.0
-

Thanks everyone.
Derek Basch

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


Minidom output of XML escaped characters

2005-03-24 Thread Derek Basch
Hello,

If I want minidom to output XHTML that includes normally XML escaped
characters how would I do it?

For instance if I use doc.createCDATASection() I get:



and without the CDATASection:

<!--#include virtual="/top.html" -->

when I really need:



Any suggestions?

Thanks,
Derek Basch

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


Re: Minidom output of XML escaped characters

2005-03-25 Thread Derek Basch
Thanks effbot. I haven't had much use for XML comments so far and I
guess other people haven't either because it seems they are hardly ever
mentioned.

http://groups-beta.google.com/groups?hl=en&lr=&c2coff=1&q=xml+comment+python&qt_s=Search+Groups

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


Counting iterations

2005-04-08 Thread Derek Basch
Is there a better way to count iterations that this?:

pets = 0
for i in pets:
pets += 1
print "pet" + "#" + pets

Thanks,
Derek Basch

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


Re: Counting iterations

2005-04-08 Thread Derek Basch
ooops you are right. Should have been:

pets = ["cat", "dog", "bird"]
num_pets = 0
for i in pets:
num_pets += 1
print "pet" + "#" + num_pets

That's the problem with one offs. I don't read them :).

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


Re: Counting iterations

2005-04-10 Thread Derek Basch
Interesting stuff Andrew. I do generally avoid string concantination
for the reason listed in the Python Performance Tips but your analysis
kinda puts that in question. Such a dense discussion for me just trying
to find the enumerate() function :). I guess that is why the python
list is so great. You guys always rip my code apart and correct my
style. Even if it is just a stupid one off example.

Thanks everyone,
Derek Basch

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


List comprehension and FieldStorage

2005-04-26 Thread Derek Basch
Given this FieldStorage object:

FieldStorage(None, None, [MiniFieldStorage('Checkbox1', 'on'),
MiniFieldStorage('Checkbox2', 'on')])

I am trying to cgi.urlencode the FieldStorage object to POST to another
cgi script. The documentation for urlencode,
http://docs.python.org/lib/module-urllib.html , says that urlencode
takes a mapping object or a list of double tuples. I see that I could
use cgi.parse_qs and cgi.parse_qsl to create these object types. Should
I just use that or should I continue using FieldStorage? Also how would
I create an object of either type from a FieldStorage object? Here is
my attempt at creating the double tuple list (It sucksI know):

print [(key, value) for key, value in form.keys() and form[key].value]

Thanks for the help,
Derek Basch

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


Re: List comprehension and FieldStorage

2005-04-27 Thread Derek Basch
bump

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


Re: List comprehension and FieldStorage

2005-04-27 Thread Derek Basch
Sorry Peter. I will refrain from nudging in the future. I did spend
time at the interactive prompt and got nothing. Maybe I will have
better luck next time.

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


Motion Tracking with Python

2011-09-29 Thread Derek Simkowiak

Hello,
I have a neat Python project I'd like to share. It does real-time motion 
tracking, using the Python bindings to the OpenCV library:


http://derek.simkowiak.net/motion-tracking-with-python/

There is a YouTube video showing the script in action.

It's especially neat because my daughter and I worked together on this 
project. We used it to track her two pet gerbils, as part of her science 
fair project. She wrote her own (separate) Python script to read the 
motion tracking log files, compute distance and velocity, and then 
export those values in a CSV file. Like I say on the web page: "I’m 
convinced that Python is the best language currently available for 
teaching kids how to program."


I also use Python professionally, and it's worked out great every time. 
There's no job Python can't handle.



Thanks,
Derek Simkowiak
http://derek.simkowiak.net

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


Re: Motion Tracking with Python

2011-09-30 Thread Derek Simkowiak
> /I am having problems with getting data from the CCD (basically i 
don't know how to do it :), can you give me a tip for doing this? Or 
explain how you did it please?/


I am using the OpenCV library to grab images.  Here are the 
specific lines of code:


self.capture = cv.CaptureFromCAM(0)cv.SetCaptureProperty( self.capture, 
cv.CV_CAP_PROP_FRAME_WIDTH, 320 );cv.SetCaptureProperty( self.capture, 
cv.CV_CAP_PROP_FRAME_HEIGHT, 240 );frame = cv.QueryFrame(self.capture) # 
...this is done repeatedly in the main loop.



These were copied from the examples that came with OpenCV.  I don't 
know if this will work under Windows.


The source code to my script is available online; I recommend 
downloading it and playing with it.  Also, check out the OpenCV Python 
documentation.



Thanks,
Derek

On 09/30/2011 07:06 AM, Ricardo Mansilla wrote:


On Thursday 29 September 2011 21:16:52 you wrote:

> Hello,

> I have a neat Python project I'd like to share. It does real-time motion

> tracking, using the Python bindings to the OpenCV library:

>

> http://derek.simkowiak.net/motion-tracking-with-python/

>

> There is a YouTube video showing the script in action.

>

> It's especially neat because my daughter and I worked together on this

> project. We used it to track her two pet gerbils, as part of her science

> fair project. She wrote her own (separate) Python script to read the

> motion tracking log files, compute distance and velocity, and then

> export those values in a CSV file. Like I say on the web page: "I’m

> convinced that Python is the best language currently available for

> teaching kids how to program."

>

> I also use Python professionally, and it's worked out great every time.

> There's no job Python can't handle.

>

>

> Thanks,

> Derek Simkowiak

> http://derek.simkowiak.net

Hi, this is awesome!!

I'm currently working in something similar, but I am having problems 
with getting data from the CCD (basically i don't know how to do it 
:), can you give me a tip for doing this? Or explain how you did it 
please?


I not a newbie at python but not as experienced as evidently you are.

Thanks a lot in advance.


--

(...)Also, since that same law states that any system able to prove 
its consistency to itself must be inconsistent; any mind that believes 
it can prove its own sanity is, therefore, insane.(...)


Kurt Gödel.



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


Re: Motion Tracking with Python

2011-09-30 Thread Derek Simkowiak

 /How was her project received at school?/



She got an "Outstanding" blue ribbon award.  The teachers were 
impressed.


(It was only a mandatory school project; it was not part of a 
regional competition or anything fancy like that.)


--Derek

On 09/30/2011 09:40 AM, Irmen de Jong wrote:

On 30-9-2011 4:16, Derek Simkowiak wrote:

Hello,
I have a neat Python project I'd like to share. It does real-time motion 
tracking, using
the Python bindings to the OpenCV library:

http://derek.simkowiak.net/motion-tracking-with-python/

There is a YouTube video showing the script in action.

It's especially neat because my daughter and I worked together on this project. 
We used
it to track her two pet gerbils, as part of her science fair project. She wrote 
her own
(separate) Python script to read the motion tracking log files, compute 
distance and
velocity, and then export those values in a CSV file. Like I say on the web page: 
"I’m
convinced that Python is the best language currently available for teaching 
kids how to
program."

Wow. Very impressive and very enjoyable to read about it. How was her project 
received
at school?

Thanks a lot for sharing this.

Irmen de Jong


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


Re: recommend a graphics library for plotting by the pixel?

2011-10-04 Thread Derek Simkowiak
If this is strictly for 2D pixel graphics, I recommend using PyGame 
(aka SDL).  Why do you not think it's the way to go?  It was built for 
this type of thing.


You may also want to use PIL (Python Imaging Library) for various 
image manipulation tasks, but PIL doesn't handle mouse clicks and screen 
blitting the way PyGame does.


--Derek Simkowiak
  http://derek.simkowiak.net

On 10/04/2011 07:53 AM, Alec Taylor wrote:

Sounds like a job for Processing...

On Tue, Oct 4, 2011 at 8:56 PM, Adam Funk  wrote:

I'd like to create a window with a "pause" button and a large plotting
area, in which I'd like to draw a polygon, detect the pixel
coördinates of a mouse click, and then start setting the colors of
pixels by (x,y) coördinates.  (This is just for my own amusement, to
play with some formulas for generating fractals using random numbers.)

There seems to be a large number of different python GUI libraries,
and I wonder if someone can recommend the easiests one to learn for
this purpose?  (The only python graphics library I've used is PyGame,
which I don't think is the way to go here.)


--
In the 1970s, people began receiving utility bills for
-£999,999,996.32 and it became harder to sustain the
myth of the infallible electronic brain.  (Stob 2001)
--
http://mail.python.org/mailman/listinfo/python-list



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


Changing UNIX tty driver keys -- Suggested changes to "termios" module

2006-08-07 Thread Derek Peschel
Should I add an RFE to SourceForge too?  I'd like a wide audience in case
someone has enough experience to comment or is solving the same problem.

I'm using the urwid library which uses curses.  On my system (Mac OS 10.3.7)
I specifically have ncurses.  The programs I'm running turn off echoing and
set raw mode but don't disable interrupts.  For development purposes I like
having interrupts, but my preferred keystrokes (WordStar) conflict with the
tty driver's use of ^C, ^Z, ^V, and maybe other keys.

Here's a piece of code based on the example in section 8.8.1 of the Python
Library Reference.  It doesn't handle ^V yet.
--
termios_cc = 6 # magic index -- not 4 which is position
   #  in the C struct
termios__POSIX_VDISABLE = '\xff' # signals are set to this
 #  when they don't corres-
 #  pond to any char.

fd = sys.stdin.fileno()
oldterm = termios.tcgetattr(fd)
oldterm_int = oldterm[termios_cc][termios.VINTR]
oldterm_quit = oldterm[termios_cc][termios.VQUIT]
if ord(oldterm_int) != 3: # ^C
sys.exit("interrupt char isn't ^C")
if ord(oldterm_quit) != 28: # ^\
sys.exit("quit char isn't ^\\")
# no way to check whether applications (telnet, screen)
#  are looking for ^^
# no check yet for any signals set to ^^

newterm = termios.tcgetattr(fd)
newterm[termios_cc][termios.VQUIT] = chr(30) # ^^
newterm[termios_cc][termios.VINTR] = chr(28) # ^\
try:
termios.tcsetattr(fd, termios.TCSADRAIN, newterm)
self.ui.run_wrapper(self.run)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, oldterm)
--

I'd like to handle errors and race conditions better, but I don't know what
kinds can happen in practice.  I'd also like to make the code work on other
versions of UNIX.

Easy suggested improvements to the library:  Define _POSIX_VDISABLE and names
for the fields of the struct, so that termios__POSIX_VDISABLE and termios_cc
become termios._POSIX_VDISABLE and termios.cc.

Harder improvements: Some functions that abstract the things I'm doing
(checking the current characters, changing a group of them in one operation).
I assume two signals should never be set to the same character, unless they
are disabled.  Is it possible to make the state variables invisible?  Is that
a good idea?

Thanks,

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


Finding the type of indexing supported by an object?

2006-08-23 Thread Derek Peschel
Here are two functions.

def invert_dict_to_lists(dict):
lists = {}
for key in dict:
value = dict[key]
if not value in lists:
lists[value] = [key]
else:
lists[value].append(key)
return lists

def invert_list_to_lists(list):
lists = {}
for key in range(len(list)):
value = list[key]
if not value in lists:
 lists[value] = [key]
else:
lists[value].append(key)
return lists

They are the same except for the expression in "for key in ...".  Can they
be combined into one function?  How can I determine if the argument is
like a list (with numeric indices that are not stored in the list) or a dict
(with arbitrary keys that are stored)?  I said "object" in the subject,
but I want to support Python primitive types, class instances, extension
module types (array, dictproxy, dbm, gdbm, etc.), and any future types.

I've thought about looking for keys(), looking for the special method names
that allow you to override indexing behavior, and looking at the class or
type of the object.  I could be wrong, but I don't think any of those
strategies will work with all arguments.

Thanks,

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


Creating a subpackage of a built-in module?

2006-08-23 Thread Derek Peschel
Can I add my own subpackages to modules that are written in C?

I've put /Users/dpeschel/lib/python into my PYTHONPATH variable.
Inside there, I've created a termios directory, and inside termios
I've created __init__.py and signals.py.  Now "import termios.signals"
works, but "import termios" only appears to work (it has no effect).
Do I have to do something sneaky in __init__.py to find the original
module, or is there a better way?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Finding the type of indexing supported by an object?

2006-08-26 Thread Derek Peschel
In article <[EMAIL PROTECTED]>, Alex Martelli wrote:
>Derek Peschel <[EMAIL PROTECTED]> wrote:

>> They are the same except for the expression in "for key in ...".  Can they
>> be combined into one function?  How can I determine if the argument is
>
>They can easily be refactored, if that's what you mean:

No, that isn't what I mean.  I wanted _one_ function that works with a wide
variety of objects -- anything that has a finite number of keys you can
iterate over, with each key mapping to a finite number of values, and the
key iteration and value lookup referentially transparent.  This hypothetical
function would have to do some checking of the argument type, but hopefully
as little as possible.  It should work with object types invented after it
was written.

Reading everyone's replies, especially yours and Fredrik Lundh's, I realized
I've been thinking of the whole problem in Smalltalk (or possibly Ruby)
terms.  Smalltalk and Ruby use inheritance to describe some properties of
objects.  Python has many primitive types that aren't related to eaach other.
I thought that testing for methods might get the properties I wanted, but
you two pointed out that the method writer has too much latitude.  Do you
think the generic-function idea is still useful?

At the moment I only need to invert dicts and lists.  Is subclassing dict
and list considred good style?  (I see I can't add methods to dict and list
directly.)

>I've also performed a few other minor enhancements (never name things
>dict or list because that hides the builtin types, use xrange vs range).

OK, I'll remember those points.  The argument names are a style I got
from my Ruby code, and possibly not a good idea there either.

>I have not changed the 4 lines in the if/else though I don't like them
>(if not.../else is a very confusing construct -- at a minimum I'd
>rephrase it as if/else swapping the bodies of the two clauses).

It used if/else originally.  Then I swapped the parts of the conditional
to make the inversion function match another function (that takes a key,
old value, and new value, and makes the change in a sequence and in its
inverted form).  To me the swapped version made some sense in the second
function, because of the layout of the function as a whole, but you have
a point that if not/else is rarely (never?) clear.

>If you want to add a generic form accepting either lists or dicts you
>need a try/except statement inside it, e.g.:

Is that a reliable way to get the properties I wanted?

RADLogic Pty. Ltd. added a two-way dict package to the Cheese Shop.  It
requires that the mapping be one-to-one, which won't work for me.  It sub-
classes dict, and requires that keys and values be hashable.

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


Re: Finding the type of indexing supported by an object?

2006-08-26 Thread Derek Peschel
In article <[EMAIL PROTECTED]>, Peter Otten wrote:

>Instead of the (unreliable) introspection approach you could let the client
>code decide:

See my reply to Alex Martelli's post, where I explain my original desire
for one function that works with a wide variety of present and future
object types.  Your solution accomplishes that, but only by forcing the
caller to convert the argument to a list of pairs.  If the caller knows the
type it's going to pass down, that's easy.  If the caller doesn't know,
your approach doesn't seem any easier than mine.

In practice, with my needs of inverting dicts and lists, your solution might
not be a bad one.

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


except clause not catching IndexError

2006-02-22 Thread Derek Schuff
I'm sorry if this is a FAQ or on an easily-accesible "RTFM" style page, but
i couldnt find it.

I have some code like this:
for line in f:
toks = line.split()
try:
if int(toks[2],16) == qaddrs[i]+0x1000 and toks[0] == 
"200": #producer
write
prod = int(toks[3], 16)
elif int(toks[2],16) == qaddrs[i]+0x1002 and toks[0] == 
"200":
#consumer write
cons = int(toks[3], 16)
else:
continue
except IndexError: #happens if theres a partial line at the end 
of file
print "indexerror"
break

However, when I run it, it seems that I'm not catching the IndexError:
Traceback (most recent call last):
  File "/home/dschuff/bin/speeds.py", line 202, in ?
if int(toks[2],16) == qaddrs[i]+0x1000 and toks[0] == "200": #producer
write
IndexError: list index out of range

If i change the except IndexError to except Exception, it will catch it (but
i believe it's still an IndexError).
this is python 2.3 on Debian sarge.

any ideas?

thanks, 
-derek
 
  
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: except clause not catching IndexError

2006-02-22 Thread Derek Schuff
Erwin S. Andreasen wrote:

> Did you by any chance do something like:
> 
> except ValueError, IndexError:
> 
> at some point earlier in this function? That, when catching ValueError
> assigns the resulting exception to IndexError (and so the following
> except IndexError: wouldn't work as IndexError is no longer what you
> think it is). The correct syntax for catching multiple exceptions is:
> 
> except (ValueError, IndexError), targetVariable:
> 
> You could verify this by doing a print repr(IndexError) before your
> line 202, to see that it really is the IndexError builtin.
> 

hey, nice catch. In fact I did exactly that. in my search for a solution for
this problem i discovered the catch-a-tuple-of-exceptions error, and in
fact fixed it, but didn't realize that it was related to the one I posted.
(the idea that exceptions can be redefined caught me off guard).

thanks (to both of you who responded),
-derek
-- 
http://mail.python.org/mailman/listinfo/python-list


Matplotlib logarithmic scatter plot

2006-02-27 Thread Derek Basch
Can anyone give any suggestions on how to make a logarithmic (base 10)
x and y axis (loglog) plot in matplotlib? The scatter function doesn't
seem to have any log functionality built into it.

Thanks,
Derek Basch

P.S. I suck at math so feel free to make me feel stupid if it is really
easy to do :).

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


Re: Matplotlib logarithmic scatter plot

2006-02-27 Thread Derek Basch
Thanks for the reply. I need a scatter plot though. Can that be done?

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


Re: Matplotlib logarithmic scatter plot

2006-02-27 Thread Derek Basch
Great! That worked fine after I played with it for a bit. One last
question though. How do I label the ticks with the product of the
exponentiation? For instance:

100 

instead of 

10**2

Thanks for all the help,
Derek Basch

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


Re: Matplotlib logarithmic scatter plot

2006-02-28 Thread Derek Basch
Thanks again. Here is the finished product. Maybe it will help someone
in the future:

from pylab import *

def log_10_product(x, pos):
"""The two args are the value and tick position.
Label ticks with the product of the exponentiation"""
return '%1i' % (x)

ax = subplot(111)
# Axis scale must be set prior to declaring the Formatter
# If it is not the Formatter will use the default log labels for ticks.
ax.set_xscale('log')
ax.set_yscale('log')

formatter = FuncFormatter(log_10_product)
ax.xaxis.set_major_formatter(formatter)
ax.yaxis.set_major_formatter(formatter)

ax.scatter( [3, 5, 70, 700, 900], [4, 8, 120, 160, 200], s=8, c='b',
marker='s', faceted=False)
ax.scatter( [1000, 2000, 3000, 4000, 5000], [2000, 4000, 6000, 8000,
1000], s=8, c='r', marker='s', faceted=False)

ax.set_xlim(1e-1, 1e5)
ax.set_ylim(1e-1, 1e5)
grid(True)
xlabel(r"Result", fontsize = 12)
ylabel(r"Prediction", fontsize = 12)

show()

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


Rounding up to the nearest exact logarithmic decade

2006-02-28 Thread Derek Basch
Given a value (x) that is within the range (1e-1, 1e7) how do I round
(x) up to the closest exact logarithmic decade? For instance:

10**3 = 1000
x = 4978
10**4 = 1

x = 1

Thanks Everyone!
Derek Basch

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


Re: Rounding up to the nearest exact logarithmic decade

2006-02-28 Thread Derek Basch
Thanks effbot. I knew their had to be something buried in the math
module that could help. ceil() it is!



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


Is it better to use class variables or pass parameters?

2006-03-01 Thread Derek Basch
This one has always bugged me. Is it better to just slap a "self" in
front of any variable that will be used by more than one class method
or should I pass around variable between the methods?

FlamewarNOW!

jk, I really do want other opinions.

Thanks,
Derek

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


Re: Matplotlib logarithmic scatter plot

2006-03-01 Thread Derek Basch
Good tip John. Hopefully it will help someone out. Thanks again.
Derek Basch

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


Debugging threaded Python code

2005-05-01 Thread Derek Thomson
Hi,

I frequently have to debug some fairly tricky Python multi-threaded 
code, and I need some help using the debugger to help me diagnose the 
problems when they occur. Yes, I know that the best option with threaded 
code that is problematic is to rewrite it, but that's not really an 
option that I have (right now).

What would really help me is to be able to see a list of all the 
currently active threads, and the Python stack trace for each. At least 
then I could see where the failures are happening - deadlocks in 
particular. I have to spend a lot of time right now just to reach that 
point.

I spent some time trying to achieve this with the Python debugger and 
couldn't. This has been bugging me for quite a while now, and I'm 
probably just missing the obvious as usual. Is there some simple way I 
can do this?

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


Multiple Inheritence and data attributes

2005-05-06 Thread Derek Basch
Hello Everyone,

Given:

class A:
def __init__(self):
super(A, self).__init__()
self.dog = "fluffy"
def changeDog(self):
self.dog = "spike"

class B:
def __init__(self):
super(B, self).__init__()

class C(object, A, B):
def __init__(self):
super(C, self).__init__()
def printDog(self, cls):
print cls.dog

c = C()
c.printDog(c)

How can I access data attributes of superclasses? I can see the
function attributes of the superclasses:

['__class__', '__delattr__', '__dict__', '__doc__', '__getattribute__',
'__hash__', '__init__', '__module__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__str__', '__weakref__',
'changeDog', 'printDog']

but not the data attributes. Shouldn't the derived class have that data
attributes of superclasses? I guess I am not understanding the python
implementation.

Thanks,
Derek

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


Remove duplicates from list

2005-06-09 Thread Derek Perriero
I've been un-triumphantly trying to get a list of mine to have no
repeats in it.   First, I'm pulling attributes from Zope and forming a
list.  Next, I'm  pulling those same values and comparing them against
the same list and if the values equal each other and are not already in
the list, they append to my 'nodupes' list.  My current result is
showing what I put in I am getting out.  Below the code is my output of
'nodupes' list.  Here's the snippet.  



regHours  = context.getMainPrint();  <---attributes from Zope



libslist       = []

nodupes    = []



#collect libraries

for libs in regHours:

  cache = libs.Monday + libs.Tuesday + libs.Wednesday + libs.Thursday + libs.Friday + libs.Saturday + libs.Sunday

  libslist.append(cache)



#pull repeated values

for item in regHours:

  collect = item.Monday + item.Tuesday + item.Wednesday + item.Thursday + item.Friday + item.Saturday + item.Sunday

  libName = item.libraryName



  for libs in libslist:    

    if collect == libs and libs not in nodupes:

  nodupes.append(libs)



My Current Output:

['8am - 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pm6pm -
10pm', '8am - 9pm8am - 9pm8am - 9pmClosed8am - 5pm9am - 5pm6pm - 10pm',
'8am - 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pmClosed', '9am -
8pm9am - 8pm9am - 8pm9am - 8pm9am - 8pm9am - 6pm12pm - 6pm', '9am -
5pm9am - 5pm9am - 5pm9am - 5pm9am - 5pmClosedClosed', '10am - 5pm10am -
5pm10am - 5pm10am - 5pm10am - 5pm10am - 5pmClosed']

Thanks





-- Perriero, Derek[EMAIL PROTECTED]
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Remove duplicates from list

2005-06-09 Thread Derek Perriero
Sorry for not being more clear.  I'm using Zope to store the hours
of each library on the campus.  The hours of each library will be
set on a basis of Monday - Friday. i.e. Monday for a specific library,
let's say Downtown Campus Library will stored as an attribute of 8am
- 9pm, in Zope, and each day till Friday will be stored as the hours
dictate.  I'm generating a print-out based on these hours and info
for the general public.  The goal of mine is to group all the
libraries under one heading if they have the exact same hours, to cut
back on redundancy when a user looks at it.  
So when I say: collect = item.Monday + item.Tuesday + item.Wednesday + item.Thursday + item.Friday + item.Saturday + item.Sunday,  
the
order is already this preset configuration.  I want 'collect' to
be static so it can compare it against another libraries hours and
group it if necessary.  The libraries that fail to be duplicates
of other libraries will be generated as usual under the grouped
libraries.  They will have a single heading.

An example can be seen here of what I am trying to achieve: http://www.libraries.wvu.edu/hours/summer.pdf
These are the outputs I failed to mention last time.
What I want:
['8am - 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pm6pm -
10pm', '8am - 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pmClosed', '9am -
8pm9am - 8pm9am - 8pm9am - 8pm9am - 8pm9am - 6pm12pm - 6pm', '9am -
5pm9am - 5pm9am - 5pm9am - 5pm9am - 5pmClosedClosed', '10am - 5pm10am -
5pm10am - 5pm10am - 5pm10am - 5pm10am - 5pmClosed']

What I am getting now:

['8am - 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pm6pm -
10pm', '8am - 9pm8am - 9pm8am - 9pmClosed8am - 5pm9am - 5pm6pm - 10pm',
'8am - 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pmClosed', '9am -
8pm9am - 8pm9am - 8pm9am - 8pm9am - 8pm9am - 6pm12pm - 6pm', '9am -
5pm9am - 5pm9am - 5pm9am - 5pm9am - 5pmClosedClosed', '10am - 5pm10am -
5pm10am - 5pm10am - 5pm10am - 5pm10am - 5pmClosed']

Thanks,
-Derek
On 6/9/05, Chris Lambacher <[EMAIL PROTECTED]> wrote:
It is very unclear what you are trying to do.  Why not explain whatyou want the output to be.  You will get better answers.As a first stab at what you are doing wrong:collect = item.Monday + item.Tuesday + 
item.Wednesday + item.Thursday+ item.Friday + item.Saturday + item.SundayThe above is string addition and the result is a string.  The ouputyou provide is in fact a list with no duplicates, i.e. there are no
two strings the same.If order is not important to you a structure that will give you an'unordered list with no duplicates' is a set (available in the stdlibrary in Python 2.3 and 2.4, see the cookbook for recipies for
earlier versions of Python).  Note that sets are unordered, i.e. noguarentee is made about what order the elements are accessed in whenyou iterate over them.-ChrisOn 6/9/05, Derek Perriero <
[EMAIL PROTECTED]> wrote:> I've been un-triumphantly trying to get a list of mine to have no repeats in> it.   First, I'm pulling attributes from Zope and forming a list.  Next, I'm
>  pulling those same values and comparing them against the same list and if> the values equal each other and are not already in the list, they append to> my 'nodupes' list.  My current result is showing what I put in I am getting
> out.  Below the code is my output of 'nodupes' list.  Here's the snippet.>>  regHours  = context.getMainPrint();  <---attributes from Zope>>  libslist   = []>  nodupes= []
>>  #collect libraries>  for libs in regHours:>cache = libs.Monday + libs.Tuesday + libs.Wednesday + libs.Thursday +> libs.Friday + libs.Saturday + libs.Sunday>libslist.append
(cache)>>  #pull repeated values>  for item in regHours:>collect = item.Monday + item.Tuesday + item.Wednesday + item.Thursday +> item.Friday + item.Saturday + item.Sunday>libName = 
item.libraryName>>for libs in libslist:>  if collect == libs and libs not in nodupes:>nodupes.append(libs)>>  My Current Output:>  ['8am - 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pm6pm - 10pm', '8am -
> 9pm8am - 9pm8am - 9pmClosed8am - 5pm9am - 5pm6pm - 10pm', '8am - 9pm8am -> 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pmClosed', '9am - 8pm9am - 8pm9am -> 8pm9am - 8pm9am - 8pm9am - 6pm12pm - 6pm', '9am - 5pm9am - 5pm9am - 5pm9am -
> 5pm9am - 5pmClosedClosed', '10am - 5pm10am - 5pm10am - 5pm10am - 5pm10am -> 5pm10am - 5pmClosed']>>  Thanks>>>>>> --> Perriero, Derek> 
[EMAIL PROTECTED]>> --> http://mail.python.org/mailman/listinfo/python-list>
>--Christopher Lambacher[EMAIL PROTECTED]-- Perriero, Derek[EMAIL PROTECTED]

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

Re: Remove duplicates from list

2005-06-10 Thread Derek Perriero
This is just a follow up from your help Chris.  Everything worked
out beautifully.  The method you suggested really cut down on the
script time and allowed me to reduce a lot of redundant areas. 
Thanks again.

-DerekOn 6/9/05, Chris Lambacher <[EMAIL PROTECTED]> wrote:
You are probably going about solving the problem from the wrong direction:Try something like this, overly verbose variable names used on purpose:regHours  = context.getMainPrint();#let a dictionary do the grouping for us
hours_to_libraries_dic = {}for lib in regHours:   key = lib.Monday + lib.Tuesday + lib.Wednesday + lib.Thursday +lib.Friday + lib.Saturday + lib.Sunday   try:  # if the key already exists add to the list of libraries
  hours_to_libraries_dic[key].append(lib)   except KeyError:  # if the key does not exists, create a new library list  hours_to_libraries_dic[key] = [lib]#print out the timetable
for lib_group in hours_to_libraries_dic.values():print " and ".join([lib.libraryName for lib in lib_group])a_lib = lib_group[0]print "Monday", a_lib.Mondayprint "Tuesday", a_lib.Tuesday
print "Wednesday", a_lib.Wednesday(you get the idea)print "Sunday", a_lib.Sundayprint-ChrisOn 6/9/05, Derek Perriero <
[EMAIL PROTECTED]> wrote:> Sorry for not being more clear.  I'm using Zope to store the hours of each> library on the campus.  The hours of each library will be set on a basis of> Monday - Friday. 
i.e. Monday for a specific library, let's say Downtown> Campus Library will stored as an attribute of 8am - 9pm, in Zope, and each> day till Friday will be stored as the hours dictate.  I'm generating a> print-out based on these hours and info for the general public.  The goal of
> mine is to group all the libraries under one heading if they have the exact> same hours, to cut back on redundancy when a user looks at it.>  So when I say: collect = item.Monday + item.Tuesday + item.Wednesday
 +> item.Thursday + item.Friday + item.Saturday + item.Sunday,  the order is> already this preset configuration.  I want 'collect' to be static so it can> compare it against another libraries hours and group it if necessary.  The
> libraries that fail to be duplicates of other libraries will be generated as> usual under the grouped libraries.  They will have a single heading.>>  An example can be seen here of what I am trying to achieve:
> http://www.libraries.wvu.edu/hours/summer.pdf>  These are the outputs I failed to mention last time.>  What I want:>  ['8am - 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pm6pm - 10pm', '8am -
> 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pmClosed', '9am - 8pm9am -> 8pm9am - 8pm9am - 8pm9am - 8pm9am - 6pm12pm - 6pm', '9am - 5pm9am - 5pm9am -> 5pm9am - 5pm9am - 5pmClosedClosed', '10am - 5pm10am - 5pm10am - 5pm10am -
> 5pm10am - 5pm10am - 5pmClosed']>>  What I am getting now:>>  ['8am - 9pm8am - 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pm6pm - 10pm', '8am -> 9pm8am - 9pm8am - 9pmClosed8am - 5pm9am - 5pm6pm - 10pm', '8am - 9pm8am -
> 9pm8am - 9pm8am - 9pm8am - 5pm9am - 5pmClosed', '9am - 8pm9am - 8pm9am -> 8pm9am - 8pm9am - 8pm9am - 6pm12pm - 6pm', '9am - 5pm9am - 5pm9am - 5pm9am -> 5pm9am - 5pmClosedClosed', '10am - 5pm10am - 5pm10am - 5pm10am - 5pm10am -
> 5pm10am - 5pmClosed']>>  Thanks,>  -Derek>>> On 6/9/05, Chris Lambacher <[EMAIL PROTECTED]> wrote:> > It is very unclear what you are trying to do.  Why not explain what
> > you want the output to be.  You will get better answers.> >> > As a first stab at what you are doing wrong:> > collect = item.Monday + item.Tuesday + item.Wednesday + item.Thursday
> > + item.Friday + item.Saturday + item.Sunday> >> > The above is string addition and the result is a string.  The ouput> > you provide is in fact a list with no duplicates, i.e. there are no
> > two strings the same.> >> > If order is not important to you a structure that will give you an> > 'unordered list with no duplicates' is a set (available in the std> > library in Python 
2.3 and 2.4, see the cookbook for recipies for> > earlier versions of Python).  Note that sets are unordered, i.e. no> > guarentee is made about what order the elements are accessed in when> > you iterate over them.
> >> > -Chris> >> > On 6/9/05, Derek Perriero < [EMAIL PROTECTED]> wrote:> > > I've been un-triumphantly trying to get a list of mine to have no
> repeats in> > > it.   First, I'm pulling attributes from Zope and forming a list.  Next,> I'm> > >  pulling t

sudoku solver in Python ...

2008-01-23 Thread Derek Marshall
This is just for fun, in case someone would be interested and because
I haven't had the pleasure of posting anything here in many years ...

 http://derek.marshall.googlepages.com/pythonsudokusolver

Appreciate any feedback anyone who takes the time to have a look would
want to give ...

Yours with-too-much-time-to-kill-on-the-train'ly,
Derek
-- 
http://mail.python.org/mailman/listinfo/python-list


Python 2.2.1 and select()

2008-03-24 Thread Derek Martin
Hi kids!

I've got some code that uses select.select() to capture all the output
of a subprocess (both stdout and stderr, see below).  This code works
as expected on a variety of Fedora systems running Python > 2.4.0, but
on a Debian Sarge system running Python 2.2.1 it's a no-go.  I'm
thinking this is a bug in that particular version of Python, but I'd
like to have confirmation if anyone can provide it.

The behavior I see is this:  the call to select() returns:
[] [] []

If and only if the total amount of output is greater than the
specified buffer size, then reading on this file hangs indefinitely.
For what it's worth, the program whose output I need to capture with
this generates about 17k of output to STDERR, and about 1k of output
to STDOUT, at essentially random intervals.  But I also ran it with a
test shell script that generates roughly the same amount of output to
each file object, alternating between STDOUT and STDERR, with the same
results.

Yes, I'm aware that this version of Python is quite old, but I don't
have a great deal of control over that (though if this is indeed a
python bug, as opposed to a problem with my implementation, it might
provide some leverage to get it upgraded)...  Thanks in advance for
any help you can provide.  The code in question (quite short) follows:

def capture(cmd):
buffsize = 8192
inlist = []
inbuf = ""
errbuf = ""

io = popen2.Popen3(cmd, True, buffsize)
inlist.append(io.fromchild)
inlist.append(io.childerr)
while True:
ins, outs, excepts = select.select(inlist, [], [])
for i in ins:
x = i.read()
if not x:
inlist.remove(i)
else:
if i == io.fromchild:
inbuf += x
if i == io.childerr:
errbuf += x
if not inlist:
break
if io.wait():
raise FailedExitStatus, errbuf
return (inbuf, errbuf)

If anyone would like, I could also provide a shell script and a main
program one could use to test this function...

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpuxR0RACuHg.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Python 2.2.1 and select()

2008-03-24 Thread Derek Martin
On Mon, Mar 24, 2008 at 05:52:54PM -0700, Noah wrote:
> On Mar 24, 2:58 pm, Derek Martin <[EMAIL PROTECTED]> wrote:
> > If and only if the total amount of output is greater than the
> > specified buffer size, then reading on this file hangs indefinitely.

> I think this is more of a limitation with the underlying clib.
> Subprocess buffering defaults to block buffering instead of
> line buffering. 

That's an interesting thought, but I guess I'd need you to elaborate
on how the buffering mode would affect the operation of select().  I
really don't see how your explanation can cover this, given the
following:

1. The subprocess used to test, in both the case where it worked, and
the case where it did not, was the very same shell script -- not a
compiled program (well, bash technically).  As far as I'm aware, there
haven't been any significant changes to the buffering mode defaults in
glibc...  But I could easily be mistaken.

2. By default, STDERR is always unbuffered, whether or not STDOUT is a
terminal device or not.

3. The actual subproc I care about is a perl script.

4. Most importantly, the whole point of using select() is that it
should only return a list of file objects which are ready for reading
or writing.  In this case, in both the working case (Python 2.4+ on
Red Hat) and the non-working case (Python 2.2.1 on Debian 3.1),
select() returns the file object corresponding to the subprocess's
STDOUT, which *should* mean that there is data ready to be read on
that file descriptor.  However, the actual read blocks, and both the
parent and the child go to sleep.

This should be impossible.  That is the very problem select() is
designed to solve...

Moreover, we've set the buffer size to 8k.  If your scenario were
correct, then at the very least, as soon as the process wrote 8k to
STDOUT, there should be data ready to read.  Assuming full buffering
is enabled for the pipe that connects STDOUT of the subprocess to the
parent, the call to select() should block until one of the following
conditions occur: 

 - 8k of data is written by the child into the pipe

 - any amount of data is written to STDERR

 - the child process terminates

The last point is important; if the child process only has 4k of data
to write to STDOUT, and never writes anything to STDERR, then the
buffer will never fill.  However, the program will terminate, at which
point (assuming there was no explicit call to close() previously) the
operating system will close all open file descriptors, and flush all
of the child's I/O buffers.  At that point, the parent process, which
would be sleeping in select(), will wake up, read the 4k of data, and
(eventually) close its end of the pipe (an additional iteration
through the select() loop will be required, I believe).

Should the program write output to STDERR before the 8k STDOUT buffer
is full, then again, the parent, sleeping in select(), will awaken, and
select will return the file object corresponding to the parent's end
of the pipe connecting to the child's STDERR.  Again, all of this is the
essence of what select() does.  It is supposed to guarantee that any
file descriptors (or objects) it returns are in fact ready for data to
be read or written.

So, unless I'm missing something, I'm pretty certain that buffering
mode has nothing to do with what's going on here.  I think there are
only a few possibilities:

1. My implementation of the select() loop is subtlely broken.  This
   seems like the most likely case to me; however I've been over it a
   bunch of times, and I can't find anything wrong with it.  It's
   undenyable that select is returning a file object, and that reads
   on that file object immediately after the call to select block.  I
   can't see how this could be possible, barring a bug somewhere else.

2. select.select() is broken in the version of Python I'm using.  

3. The select() system call is somehow broken in the Linux kernel I'm
   using.  I tend to rule this out, because I'm reasonably certain
   someone would have noticed this before I did.  The kernel in
   question is being used on thousands of machines (I'm not
   exaggerating) which run a variety of network-oriented programs.  I
   can't imagine that none of them uses select() (though perhaps its
   possible that none use it in quite the manner I'm using it here).
   But it may be worth looking at...  I could write an implementation
   of a select() loop in C and see how that works.

If you can see any flaw in my analysis, by all means point it out!
Thanks for your response.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpzol0mPJUfy.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Python 2.2.1 and select()

2008-03-26 Thread Derek Martin
On Wed, Mar 26, 2008 at 09:49:51AM -0700, Noah Spurrier wrote:
> On 2008-03-24 22:03-0400, Derek Martin wrote:
> >That's an interesting thought, but I guess I'd need you to elaborate
> >on how the buffering mode would affect the operation of select().  I
> >really don't see how your explanation can cover this, given the
> >following:
> 
> I might be completely off the mark here. I have not tested your code or even
> closely examined it. I don't mean to waste your time. I'm only giving a
> reflex response because your problem seems to exactly match a very common
> situation where someone tries to use select with a pipe to a subprocess
> created with popen and that subprocess uses C stdio. 

Yeah, you're right, more or less.  I talked to someone much smarter
than I here in the office, who pointed out that the behavior of
Python's read() without a specified size is to attempt to read until
EOF.  This will definitely cause the read to block (if there's I/O
waiting from STDERR), if you're allowing I/O to block... :(

The solution is easy though... 

def set_nonblock(fd):
flags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, flags | os.O_NONBLOCK)

Then in the function, after calling popen:
set_nonblock(io.fromchild.fileno())
set_nonblock(io.childerr.fileno())

Yay for smart people.

--
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgptoUxGdeV6b.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Python 2.2.1 and select()

2008-03-26 Thread Derek Martin
if Python provides a way to do this, but I
   assume it does).  Since the python program is the one opening the
   pipe, it controls the buffering mode, and you have only to change
   it in your program, and the child will honor that.  

   The way it works is because popen() opens the pipe, and then forks
   a child process, which inherits all of the parent's open files.
   Changing the properties on the files can be done in the parent, and
   will be honored in the child, because *it's the same file*. :)
 
 - It states: "[when writing to a pipe] In this mode the currently
   buffered data is flushed when the buffer is full. This causes most
   interactive programs to deadlock."  That's misleading/false.

   Deadlocks can easily occur in this case, but it's definitely
   avoidable, and it's not *necessarily* because of buffering, per se.
   It's because you're trying to read or write to a file descriptor
   which is not ready to be read from or written to, which can be
   caused by a few things.  STDIO buffers could be the cause, or it
   could simply be that the parent is reading from one file
   descriptor, but the child is writing to a different one.  Or
   (perhaps even more likely) it is trying to read from the parent, so
   both are reading and no one is writing.  Non-blocking I/O allows
   you to recover from all such I/O synchronization problems, though
   you'll still need (your program) to figure out where you should be
   reading and/or writing in order to avoid an infinite loop.  But
   non-blocking I/O may not help with interactive programs that make
   use of the stdio library, unless you also explicitly set the
   buffering mode.

All of the above are explained in much more detail in W. Richard
Stevens' "Advanced Programming in the Unix Environment" than I can
possibly go into here.  See chapter 3 for the stdio stuff, chapter 12
for non-blocking I/O, and chapter 14 for discussions about pipes,
popen(), and how buffering modes can be controlled by your program and
how that affects the child.

Don't get me wrong... pexpect is useful.  But some of the problems
you're trying to solve with it have other solutions, which in some
cases might be more efficiently done in those other ways.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpO2XZFMeC5p.pgp
Description: PGP signature
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Official definition of call-by-value (Re: Finding the instance reference...)

2008-11-23 Thread Derek Martin
s the value of [1, 2, 3] if 
> it isn't "the list consisting of ints 1, 2 and 3"?
> 
> And recursive objects?
> 
> class Parrot:
> def __init__(self):
> self.me = self
> 
> p = Parrot()
> 
> a = []
> a.append(a)
> 
> 
> 
> 
> > I don't understand Python well enough to defend any of these
> > descriptions (I now realize, despite my previous postings to the
> > contrary. :-)
> > 
> > But what I do defend is the concept of intrinsic value which I have not
> > ever seen explicitly stated anywhere and which clarifies a lot of things
> > for me.
> 
> Think about lists. The "intrinsic" value of a list is the items in the 
> list itself, if it is anything, but you have an interface for looking at 
> them individually:
> 
> alist[i]
> 
> Here's another problem with your idea of "intrinsic value". Consider a 
> list. The list object in CPython is an array of cells, containing data. 
> Some of those cells are in use, some of them are free, but regardless of 
> whether they are free or in use they still have data in them. (The data 
> may be obsolete in the case of the free cells. The list object also 
> includes a cell which records the number of cells in the array, and 
> another cell recording private information of use to the Python garbage 
> collector. These cells are not necessarily the same size.
> 
> Are they part of the "intrinsic value"? You can't access them via 
> attributes.
> 
> What if we use another implementation of list, say, a linked list? What 
> if we use PyPy, an implementation of Python in Python, and (for the sake 
> of the argument) lists are implemented as follows:
> 
> class list(object):  # untested
> def __init__(self, values):
> for i, x in enumerate(values):
> setattr(self, i, x)
> 
> (This would make a lousy implementation. Don't try it!) Does that mean 
> that the value of [1, 2, 3] depends on the implementation of Python you 
> create it under?
> 
> 
>  
> > For example, you can define the value of None however you want, but it
> > seems clear that it has (and needs) no intrinsic-value.
> 
> To me, that seems just as silly as arguing that zero is not a number, or 
> that white pixels are "nothing" and black pixels are "something".  Or 
> maybe they should be the other way around?
> 
> None is None. It is what it is, and that is it's value. What *meaning* 
> you put to that is up to your program. It is a convention, a useful 
> convention but still merely a convention, that None is used to represent 
> values which otherwise would be missing, if there could be actual holes 
> in Python code.
> 
> 
> > Same with
> > object(). I now understand why the only way to describe the object
> > int(2) is requires using str/repr 
> 
> Not true.
> 
> >>> x == int(3)
> >>> if x == 2:
> ... print "x has the value two"
> ... elif x == 3:
> ... print "x has the value three"
> ...
> x has the value three
> 
> 
> At no stage did I call str() or repr() on x.
> 
> 
> > whereas with objects without an
> > intrinsic value, I can describe without needing str/repr.
> >
> > I can think of expressions as always returning objects, never "values".
> 
> Expressions always evaluate to objects (unless they don't return at all). 
> The value of an expression is the object it evaluates to. What is wrong 
> with that claim? Why do you believe that we need a more complicated, 
> convoluted understanding of value? What problem are you trying to solve?
> 
> 
> 
> -- 
> Steven
> --
> http://mail.python.org/mailman/listinfo/python-list

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgphIaFEajP6r.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-02 Thread Derek Martin
On a mostly not related note: 

On Tue, Dec 30, 2008 at 07:52:26AM -0800, Aaron Brady wrote:
> According to some rules, these are ungrammatical sentences, due to
> plurality disagreement.  Ex:
> 
> The Morning Star is ...
> The Evening Star is ...
> *The Morning Star and The Evening Star is...
> *The Morning Star and The Evening Star are...
> 
> Neither of the latter two is correct.  (* marks ungrammatical.)   As
> such, the listener isn't sure what meaning to take.

This statement is false.  The latter of the two is grammatically
correct.  The subject is a compound subject joined by the conjunction
"and" which indicates that there are two subjects, and thus the plural
form of the verb is necessary and correct.
 
> Identity isn't defined on math objects, only on Python objects; there
> is no notion of 'is' in math.  

This is also false, it even has its own operator (which requires
Unicode to display): ≡

Still, the point you're trying to make is right: this stuff is hard to
talk about, and the model actually encourages the use of ambiguous or
even contradictory explanations.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpvXlslLQieE.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-02 Thread Derek Martin
On Tue, Dec 30, 2008 at 02:21:29PM +, John O'Hagan wrote:
> Fortunately, unlike the murky world of philosophy, Python (AIUI)
> simplifies this question by simply declaring that yes, in the case
> of mutable objects, we may say that we are still referring to the
> same object although we've changed it, and no, in the case of
> immutable objects, we may not, and must exchange it if we want a
> different "value" (a word too fraught with ambiguity in this context
> to use unquoted!).

That's sort of true; it would seem to be more accurate to say that
whenever a name is assigned to an object and subsequently reassigned,
the name no longer is associated with the original object.  In the
case of mutable objects, the object can be changed by performing an
assignment of *part* of the object through its original name, i.e.
strings may be mutable, but the following code still produces two
different objects:

  a = 'hello'
  a = 'goodbye'

The first object so created is orphaned; it's been given the Russian
non-person treatment.  It still exists, but the authorities (i.e. the
python interpreter) don't acknowledge it and provide the rest of the
world no way to communicate with it, and eventually it is reaped by
the garbage collector. :)

What the Python community often overlooks, when this discussion again
rears its ugly head (as it seems to every other hour or so), is that
its assignment model is BIZARRE, as in it's conceptually different
from virtually all other languages substantially taught in
undergraduate computer science programs.  And for that matter, it's
pretty unintuitive generally.

That is, in what I'll call "normal" computer languages, a variable
name is thought of as the address of a bin where some data is stored,
and the name is inexorably tied to that bin.  You can change what's in
the bin, but the name you gave the bin always points to the same bin.
This tends to be conceptually true even if it might technically not be
true in a given implementation of a language.

Python is very different from this.  Names are not addresses of bins;
they are instead simply ephemeral labels which are given to bins,
where the bin is a Python object which contains specific data at the
time of assignment.  A second assignment of that name doesn't change
what's in the original bin; it actually (probably) first creates a new
bin, then removes the name from the original bin and assigns it to 
the new one.  Intuitively, it's a bit like saying your kitchen table
is no longer a kitchen table, and now the thing where you wash your
dishes is a kitchen table.  It doesn't really make a lot of sense
(whether or not it's so for good reason), and it makes describing the
assignment model necessarily convoluted, whereas the "named bins"
model from the majority of other languages people are likely to have
been exposed to is simple and sensible.  

It's small wonder that neophytes try to cram Python behaviors into
terms and computing concepts they already understand from learning
other languages, and that they fail to do so.  What's mystifying is
that when Pythonistas reply to their messages, they universally seem
confused at how this could possibly happen, and often enough actually
seem offended (or at least offensive) when it inevitably does happen...

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgp6S7INF1qUN.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-02 Thread Derek Martin
On Fri, Jan 02, 2009 at 11:43:30AM -0500, Steve Holden wrote:
> Derek Martin wrote:
> > What the Python community often overlooks, when this discussion again
> > rears its ugly head (as it seems to every other hour or so), is that
> > its assignment model is BIZARRE, as in it's conceptually different
> > from virtually all other languages substantially taught in
> > undergraduate computer science programs.  And for that matter, it's
> > pretty unintuitive generally.
> > 
> I'd definitely argue against bizarre. It's actually very easy to
> understand, and Python is by no means the only language to have used it.

Clearly the first and third are true. :)  But CS programs still
generally teach primarily C/C++, Java, and (some form of) assembly
AFAICT.  A few odd ones pop up here and there along the way (I studied
with scheme, for example), but they vary and are highly
program-dependent.  What the average CS grad sees is, AFAICT, still
very much what I described.  Those languages also behave similarly to
what students see in mathematics (e.g. algebra etc.).  With only that
to go on, Python seems rather weird, and I think from the frequency
with which these discussions occur on this list, clearly it *IS*
difficult for a neophyte Python programmer to understand the
assignment model.  And this is, in part, because it's kind of
difficult to explain precisely, as has oft been demonstrated in this
forum.

> I'd argue that this approach is out of date and overly-restrictive,

Sure, I already hinted that it's useful... but it's still uncommon, in
the experience of most CS students -- not even taking into account the
number of people who program who have never studied in a formal CS
program.

> I'd instead say that Python uses ephemeral names for long-lived objects,
> where other languages use the addresses of ephemeral objects. Your ideas
> of "simple" and "sensible" are being conditioned by your experience.

Of course...  I'd argue that our experience is a large part of what
makes things simple or sensible.  Things that behave like other things
we are very familiar will be simple to understand.  Python's
assignment model is probably new to most people when they first start
using it.  To look at Python's code, at first glance assignment seems
to be the same as everywhere else you've encountered it...  Only once
someone starts to write "real" programs does the difference really
matter.

> > It's small wonder that neophytes try to cram Python behaviors into
> > terms and computing concepts they already understand from learning
> > other languages, and that they fail to do so.  What's mystifying is
> > that when Pythonistas reply to their messages, they universally seem
> > confused at how this could possibly happen, and often enough actually
> > seem offended (or at least offensive) when it inevitably does happen...
> > 
> Generally speaking we try not to be offensive first on this list.

Perhaps I've misused the term Pythonista... I meant roughly, "people
who frequent this list/news group who seem to consider themselves 
experts at programming Python (and for the most part, are)."
I consider myself pretty well informed about Python (though not an
expert by any means), and I still read this list often (I use the
mailing list interface), because I still find that I learn useful
things from the posts to it from time to time.  But I often see python
"experts" lambasting people who clearly from their posts are new at
python, because their code is bad, their understanding is bad, or in
this case even accusing the learning materials of being sub-par.  I
realize that some of this is meant in jest, but a lot of it isn't, and
it can be quite difficult for someone who doesn't speak your language
natively (or even one who does, given the medium) to tell the
difference.  There are better ways to foster understanding... ;-)

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpmcWKAkQA4R.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-02 Thread Derek Martin
On Fri, Jan 02, 2009 at 09:05:51PM +0100, Bruno Desthuilliers wrote:
>> Python seems rather weird, and I think from the frequency
>> with which these discussions occur on this list, clearly it *IS*
>> difficult for a neophyte Python programmer to understand the
>> assignment model.
>
> Took me about half an hour to grasp, not even being "CS grad" (nor  
> whathever "grad" FWIW). By that time, I had a couple monthes working  
> experience with VB, and had learned (but never seriously used) bits of  
> C, C++, Java and Pascal.

It took me about a half a second to grasp the "named bins" concept --
i.e. as soon as I was finished reading the words that explained it I
understood it, so I'd say that based on your half-hour number,
Python's model is substantially more complicated.  

My own experience was kind of similar...  When I read about Python's
model I didn't understand it the first time around, had to re-read the
section I read that described it, and then had to play with it to see
for myself how it worked.  I'd estimate it took 10 minutes.  I'm not a
CS grad either (my degree is in information technology) but I did take
the first two years of CS classes at my local college (as a bridge to
a masters degree in CS, which I never completed), and I've been
programming as a hobbyist, in school, and in my profession for 25
years.  I would argue that ideally, it should not take an experienced
programmer 10 minutes to understand variable assignment. :)  [Note
that I'm including the semantics for passing arguments to functions as
part of "assignment" for purposes of this discussion.]

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpkihamKsYya.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-02 Thread Derek Martin
On Fri, Jan 02, 2009 at 12:50:44PM -0800, Erik Max Francis wrote:
>>> Identity isn't defined on math objects, only on Python objects; there
>>> is no notion of 'is' in math.  
>>
>> This is also false, it even has its own operator (which requires
>> Unicode to display): ≡
>
> That can mean a number of things, one of which means "is identically  
> equal to," 

Quite so.

> but identity means something different in mathematics than it  means
> here.  

But for non-mutable objects, aren't they essentially the same?
Mathematics has no concept of "objects" in the sense that computer
science does, so of course the best you can really do is draw
parallels.

> In mathematics, identity means a relationship that is true  
> regardless of the value of the variables involved (as opposed to  
> equality which is only true under more specific circumstances).  

Does 2 = 2 not qualify?  Isn't it true that 2 ≡ 2 and 2 is 2? :)
Yet there are no variables at all...  The "objects" of mathematics are
numbers, which are constants, which as such I would argue always have
the same "identity" as themselves.  Other components of mathematics
are "expressions", which may or may not evaluate to constants,
depending on the set conditions.  Python has those too, and they are
not the same as objects.  

> In  computer science, identity means that two expressions are
> represented by  the same object, something which not only has no
> meaning in mathematics,  

We're getting way off track here, but I would argue this is also
false.  Take sets, for example:

A = { 1, 2, 3 }
B = { 1, 2, 3 }

Is it not true that A ≡ B and in fact these two sets are the same,
i.e. they are not actually two different sets at all; the have the
same identity, even considering a definition of "identity" which
reflects that in Python?  A and B are in fact simply two different
names we've given to the same mathematical entity.  The major
difference between mathematics and Python is that mathematical objects
are essentially unique, i.e. the constant 1 is arguably always the
same 1 wherever it appears, because there is no mathematical need to
have multiple instances of the constant 1:  Wherever you see the
symbol '1' OR an expression which evaluates to the constant 1, it
refers to a *concept* of the numerical value representing mathematical
singularity and wholeness.  In python, you can have multiple instances
of objects which are identical to each other (though for this simple
case, even python only creates one instance of the object).

> but which should also be clear since
> mathematical identities need not  have any individual variables on
> either side of the triple bar; take,  for instance, the
> trigonometric identity
>
>   cos^2 theta + sin^2 theta = 1.

Is theta not a variable? :)  Not that it matters...

> Even if you write this equation with the triple bar to represent a  
> mathematical identity (which it is), it obviously doesn't say anything  
> about which "objects" are the same as each other.

I don't imagine I would agree, based on what I just said.  To elaborate,
each side of the expression contain symbols which always evaluate to
the same constant.  The identity of a constant is constant. :)  Thus
the objects on both sides are indeed the same identical mathematical
entity... they are just expressed differently.  It's just like if you
refered to your kitchen table (assuming you have only one kitchen
table) as "the table" or as "the large table I eat on in the
kitchen..."  No matter what you call it, it's still the same table.

In the case where the identity can not be reduced to constants, the
two expressions still evaluate to the same mathematical entity...
except that you need to set the conditions (i.e. give values to the
variables) to find out what that actually is.  It seems exactly
analogous to Python to me, except that again, unlike Python, there is
no possibility that there can ever be two instances of the same object
and thus applying the term "identity" to mathematical objects is not
useful.  It's not that it is meaningless, it just isn't very interesting.
Clearly though, 2 is not 3, and these two mathematical objects do not
have the same identity.  Perhaps there is no concept of identity in
mathematics precisely because it is unnecessary: 1 is always 1, by
definition.  But that is the definition of "is"... :)

But the discussion is bordering on philosophy, and I will resign from
it at this point, having previously made the points I intended to.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpAiEkpMH3gD.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-04 Thread Derek Martin
On Sat, Jan 03, 2009 at 10:15:51AM +, Marc 'BlackJack' Rintsch wrote:
> On Fri, 02 Jan 2009 04:39:15 -0600, Derek Martin wrote:
> 
> > On Tue, Dec 30, 2008 at 02:21:29PM +, John O'Hagan wrote:
> > What the Python community often overlooks, when this discussion again
> > rears its ugly head (as it seems to every other hour or so), is that its
> > assignment model is BIZARRE, as in it's conceptually different from
> > virtually all other languages substantially taught in undergraduate
> > computer science programs.
> 
> What's the difference between Python and Java or C# here!?  Or are they 
> also "BIZARRE"!?

I am happily ignorant of C#.  As for Java, take the following code:

  a = 6;
  a = 5;

In Python, when you execute the equivalent code, it causes two
different objects to spring into existence, the first of which may be
cleaned up by the GC (except that since we're using small integers,
that's not likely to happen).  Unless I'm misinformed (which is very
possible, my experience with Java has been extremely limited) in Java
that's not the case...  the storage is allocated to the name a when
you execute its declaration, and the *same storage* is reused upon
subsequent assignment.

That behaves exactly like named bins.

> > And for that matter, it's pretty unintuitive generally.
> 
> Names and objects are quite "natural" IMHO.  There are many real world 
> objects which we attach one or more names to, or refer to in sequences 
> like "please give me the third book on that shelve" (``shelve[2]``).

Indeed, but the way we assign names to them does not behave as it does
in Python.  Nor does Python's assignment work like it does in algebra,
or anywhere else the Python student is particularly likely to have
seen variable assignment before encountering it in Python.  Let's
define intuitive, shall we?  From dictionary.com (choosing the
definition which most easily makes my point):

  intuitive: adj.  capable of being perceived or known by intuition.

I'm going to go out on a limb and assert that there's NO POSSIBLE WAY
a student could intuit Python's variable assignment behavior, having
never been exposed to that same behavior prior.  It needs to be
taught.

> > That is, in what I'll call "normal" computer languages, a variable name
> > is thought of as the address of a bin where some data is stored, and the
> > name is inexorably tied to that bin.
> 
> You just call that "normal" or "intuitive" because that's what you 
> learned first.

In a sense, yes... but isn't that what intuition really is?  You can
figure something out whithout being told how it works...  That's
either because it's immediately obvious from observing it, or it
behaves like something you've seen before.  That is what intitive is.

> I think the "bin model" is more complex because you don't just have a 
> name and an object but always that indirection of the "bin".

I cheerfully disagree. :)  "Named bins" is essentially how algebra
works, and how several generations of computer languages, not to
mention the actual machine language those generated, behaved, before
the current crop.  Those interpretations came first, because, much as
in the evolution of any other science, that was the model which was
most intuitive or easily explained.

But you need not take my word for it.  Simply read the archives and
see for yourself how much confusion this has caused on this list.
[Please include the closely related behavior of parameter passing in
your search.]

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpilB7yH4CC6.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-04 Thread Derek Martin
On Sat, Jan 03, 2009 at 11:38:46AM -0600, Grant Edwards wrote:
> > Or are they also "BIZARRE"!?
> 
> One presumes that Mr. Martin finds anything different from his
> first computer language to be BIZARRE.  He should try out
> Prolog or something genuinely different.

One's presumption would be mistaken.  However thank you for
illustrating my point so precisely, which was after all the
condescending and insulting way people "communicate" with people whom
(they think) know less than they do in this forum, and not actually
how difficult or easy the assignment model of Python is to understand.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgp7GzFxWB35W.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-04 Thread Derek Martin
On Sun, Jan 04, 2009 at 09:30:20PM -0500, Steve Holden wrote:
> > I'm going to go out on a limb and assert that there's NO POSSIBLE WAY
> > a student could intuit Python's variable assignment behavior, having
> > never been exposed to that same behavior prior.  It needs to be
> > taught.
> > 
> As does assignment of any kind. 

I'm not sure that's true.  Having taken algebra prior to learning Basic,
I intuitively understood what this program would do when I executed
it, the first time I saw the code, and before I read the explanation:

10 let x = 10
20 print x

[Well, to be honest it's been a very long time since I've programmed
in Pet BASIC, and the syntax may be wrong.  The point is, just as I
did then, I am positive that you intuitively understand what the above
is intended to do, even if it is not valid BASIC syntax -- because if
you did not, we would not be having this discussion.]

> You can't argue that one semantic or another is more intuitive
> without offering evidence.

I think I have though, not that it matters, since that was never
really my point.  Python's assignment model requires more explanation
than the traditional one, simply to state what it does.  That alone is
evidence (but not proof).  I haven't proven it scientifically, and I'm
not willing to jump through the necessary hoops (which would require a
psychological study) to do so simply to win an argument on Usenet.
It's not about winning... it's about illumination. ;-)
 
> You're perhaps familiar with some algebra that I didn't study. 

Not unless you never studied it at all... ;-)

> In algebra and number theory identity and equality are equivalent. 

Indeed.  The point of bringing up algebra is that it provides a
background against which someone might be very likely to intuit what
variable assignment does in traditional programming languages -- at
least accurately enough to use it effectively without needing to
understand the underlying implementation and worry about any corner
cases.  It really doesn't need to be explained, unless the student has
no prior background in either math or computers.  In the case of
passing parameters things unavoidably get hairy, but at least at the
moment we're not discussing that, and also that has no analogous in
(at least high school) mathematics (at least, that I can think of) or
much of anywhere else from whence a student might draw any insights.

As for there being no assignment in algebra, is that not really what
variable substitution is?  They have different names, but in my
estimation they perform exactly the same function.  You're assigning
specific values to the variables in an expression so that you can
solve for a constant... just like in a computer program.  There is
even an allocation of memory: it's in your brain. :D

> This is far from the case in programming languages, so any analogy
> based on it is specious to some degree. Programming isn't
> mathematics (except perhaps for functional programming).

I agree; but I wasn't making an analogy.  I was pointing out (an
extremely likely) basis for intuition.

> It's difficult to think of a single aspect of Python that doesn't cause
> confusion in a typical year. 

Surely some cause more than others... and surely some are more
surprising than others. ;-)

> The confusion is sometimes caused by ill-informed comment. While
> well-informed, you appear to feel that everyone else should share
> your idea of what's intuitive and what's BIZARRE.

Alright, perhaps I exaggerated when I said bizarre, but I did explain
exactly what I meant, and I believe that what I said in my explanation
is at least arguably true.  I stand by the idea that it's much less
intuitive than the traditional assignment model, and despite your
protestations of lack of proof, I'm pretty sure you agree. ;-)

Ultimately, none of this really matters, as perhaps my point is that
Python *is different* from what A LOT of folks learning it have
already seen (if anything), and it's often easy for them to
misunderstand the Python way.  As you said, let's provide light, not
heat, when we come across people who get confused between Python and
something else.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpE0oLCW41pi.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-04 Thread Derek Martin
On Sun, Jan 04, 2009 at 09:56:33PM -0600, Grant Edwards wrote:
> On 2009-01-05, Derek Martin  wrote:
> > On Sat, Jan 03, 2009 at 11:38:46AM -0600, Grant Edwards wrote:
> >> One presumes that Mr. Martin finds anything different from his
> >> first computer language to be BIZARRE.  He should try out
> >> Prolog or something genuinely different.
> >
> > One's presumption would be mistaken.  However thank you for
> > illustrating my point so precisely, which was after all the
> > condescending and insulting way people "communicate" with
> > people whom (they think) know less than they do in this forum,
> > and not actually how difficult or easy the assignment model of
> > Python is to understand.
> 
> I'm sorry, but I really don't see how Python's assignment model
> could be considered bizarre by anybody who's familiar with more
> than one or two languages.  

And... what if one wasn't?  The OP of this thread clearly didn't
understand... Whereas if you've read the thread, clearly I do.
Of course, had you read my post, you probably would have understood
that my comment about the model being bizarre was intended to be
viewed from the perspective of someone who *had not* seen anything
like it before, which is LOTS of relatively new programmers, whether
or not it might be old hat to anyone here.   The ultimate point of my
post was not so much about whether the assignment model of Python was
or wasn't easy to understand; it was about the idea that when someone
doesn't understand, we should try to help them instead of making snide
remarks about how stupid or small-minded they are.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpVxUxyCvEmP.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-04 Thread Derek Martin
Forgive my indulgence, I find this rather academic discussion kind of
interesting, as it turns out.

On Sun, Jan 04, 2009 at 10:55:09PM -0600, Derek Martin wrote:
> > You can't argue that one semantic or another is more intuitive
> > without offering evidence.
> 
> I think I have though, not that it matters, since that was never
> really my point.  Python's assignment model requires more explanation
> than the traditional one, simply to state what it does.  That alone is
> evidence (but not proof).  

Here's (I think) a better argument, FWIW:  The traditional model ties
a name to some (usually predefined and static) storage (a memory
address, or at least the start of a string of them).  This is a very
concrete thing to represent, and the conceptualization of variables as
named bins in such languages captures this very succinctly, and
requires no understanding of the underlying implementation for the
programmer to use (at least with primitive data types, which are all
that are used generally when variables are first introduced).

The Python model binds a name to a particular Python object, which is
itself an abstraction; understanding requires understanding first what
an "object" is, and I think at least in some measure some knowledge
about how Python is implemented (primitive data types are actually
objects; and also recall the recent discussion about what constitutes
a "value" in Python).  The abstraction, and the requirement to
partially understand the underlying implemenation, make the Python
model inherently more complicated, and therefore also inherently less
intuitive.

That also is not proof, but as I said, real proof is rather hard to
come by...  And FWIW, remember that I never suggested that all this
was without good reason, and I'm not attempting to advocate for
simplifying the model. :)

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgphDXw471YZc.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: why cannot assign to function call

2009-01-05 Thread Derek Martin
On Mon, Jan 05, 2009 at 01:23:04PM -0500, Steve Holden wrote:
> Even if they really are small-minded or stupid I agree this wouldn't be
> helpful behavior. But neither would your characterization of Python's
> assignment model as "bizarre" (even ignoring that you SHOUTED IT AT US),
> and I have yet to see you admit that such a characterization was, shall
> we say, inappropriate.

Actually I did, in one of my two most recent posts.  But as Steve
D'Arpano just pointed out (even though he clearly disagreed with me),
such a characterization is subjective, and as such you can't rightly
say it's inappropriate.  That's the largest part of my point in
posting in this thread.  Many folks do exactly that, very often.
Someone disagrees with you, tries to shed some light on a different
perspective, or simply fails to understand something, and some members
of this community treat them like heretics, fools, or criminals.

I understand why the assignment model works the way it does, and it's
quite sensible, *when you understand it*.  However, I do also think
that to someone who has not encountered such a model before, and who
has not had it explained to them, and/or who has not the background to
understand why it is implemented that way, it very likely might seem
"markedly unusual in appearance, style, or general character and often
involving incongruous or unexpected elements;" as dictionary.com
defines the term bizarre.  So no, I don't think that's a
mischaracterization at all. 

As for using the term in all caps, I did so precisely because it was
clear to me that many people here think that it could not be unusual,
and I wanted to emphasize the fact that other perspectives exist...
That they are not the same as yours does not invalidate them!

> It takes little to admit one is in the wrong even when one isn't. I've
> had to learn to do it because I often *am* wrong about things. Could you
> be persuaded to consider the possibility that you met with a somewhat
> hostile reaction (whether or not such a reaction was useful or
> necessary) because you were, in a small way, poking people in the side
> with a sharp stick?

I fully expected to receive a hostile reaction, because I am
criticising the behavior of the collective, and supplying a dissenting
perspective -- something I knew from the start would trigger such
hostility *because it always does*.  I have witnessed hostile
reactions time and time again in this forum, from some of the same
people who are dumping on me for suggesting that the assignment model
might be something other than obvious, and from others, for much less:
I expect it because I see it in response to nothing more than asking a
simple question, when the question displays a clear indication that
the poster has missed something critical preventing them from
understanding how to achieve their goals.  My intent was exactly to
point out this behavior, in an attempt to call to people's attention
that it is what they are doing, and thereby discourage it.  I fully
expected a negative response.  You in particular have responded quite
well, but the rest of the community by and large has sadly not failed
to live up to my expectations, even in the face of me saying that that
is exactly what they are doing.  Quite impressive.

Some of the comments from people include the idea that the assignment
model is nothing special, if you've encountered any one of a dozen
other languages.  I didn't realize programming in any of those
languages was a prerequisite for posting questions here, or for
programming with Python.  And that speaks to my ultimate point:  Some
members of the community seem to make assumptions about what people
know or should know, or have experienced, and talk down to people who
haven't met their expectations.  They meet different perspectives with
hostility.  Posts which phrase questions in terms commonly used in
other programming paradigms are generally even more likely to be met
with that same hostility, when they could simply instead explain
politely that Python behaves according to a different model than what
they are used to.  Often this happens, but too often not without
someone also letting the OP know what a mindless jerk he is...

*This* is the "common understanding" which I'd hoped could be
reached...  But you were right... it's very difficult for people to
admit that they might be wrong.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpdBC9eN0T5h.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using subprocess module to launch a shell shell script that itself forks a process

2008-10-09 Thread Derek Martin
On Tue, Oct 07, 2008 at 05:43:41PM -0700, Samuel A. Falvo II wrote:
> p = subprocess.Popen(
> command,
> shell=True,
> stdin=subprocess.PIPE,
> stdout=subprocess.PIPE,
> stderr=subprocess.STDOUT,
> close_fds=True
> )
> 
> outputChannel = p.stdout
> output = outputChannel.read()

You call read with no arguments.  This is highly problematic in the
context of interprocess communication, unless you can be 100% positive
that none of the children will write anywhere besides STDOUT, and
won't try to read from STDIN in the meanwhile.

Python's read() with no args reads until the end of file, which in IPC
contexts is bad...  Normally the child process won't close stdout
until it exits.  So, if it did any other output in between, say, to
STDERR, the child will block waiting for the parent to read STDERR,
meanwhile the parent is blocked waiting for input from the child's
STDOUT, which results in a deadlock.  Both processes sleep forever.
The exact same thing can happen if either the shell script or a
process started by the shell script tries to read from STDIN.

Since Java is launched by the shell script, it inherits the shell
script's STDIN, STDOUT, and STDERR file descriptors (i.e. the two
processes share the same STDIO).  Thus if the java process writes to
STDERR, that also could be causing your deadlock.


On Wed, Oct 08, 2008 at 11:24:39AM -0700, Samuel A. Falvo II wrote:
> On Oct 7, 6:23 pm, "Gabriel Genellina" <[EMAIL PROTECTED]> wrote:
> > Is your shell script doing something else, apart from invoking the java  
> > process?
> 
> Obviously, yes.  

It's far from obvious.  I can't count the number of scripts I've seen
whose sole purpose was to launch a Java program (with the right
environment)...  You would do well to avoid being dismissive and
listen to those who respond with help,  when you are the one who
obviously doesn't understand the behavior you're getting, and you're
the one asking for help.

> The script is some 150 lines long.  But the hang-up
> occurs because of the forked Java process, not the other lines.

I'm betting it's because the Java program is writing warnings to
STDERR (more data than will fit in the STDIO buffer), which you're not
reading...

> > If not, you could just invoke java directly from Python. Also,  
> > you set stdin=PIPE - is your java process expecting some input? you're not  
> > writing anything to stdin.
> 
> It does not expect input from stdin.  However, this does not affect
> any OTHER scripts or commands I run.

Irrelevant...  Unless "any OTHER scripts" encompases all possible
combinations of process interactions, and you can demontstrate that it
does so, this proves nothing.

> Let's remember to look at the objective facts: for shell scripts that
> launch child processes of their own, Python hangs.  For all other
> types of commands, it works 100% as expected.

These are not facts which are in evidence.  We don't know what your
script is doing, and it's clear that you yourself are not able to
explain the behavior you are seeing, therefore there is no reason for
us to conclude that the above statements are true and correct.  Most
likely, they are not.

> > Anyway, it's better to use the communicate method instead (it uses select  
> > to read from both stdout and stderr):
> 
> That doesn't help me.

Please explain why it doesn't.

The most likely cause of the behavior you are seeing is the deadlock I
described, above.   Using select() (i.e. using communicate()) should
generally fix about half the cases...  The rest would be fixed by
redirecting STDIN of the child (or at least the Java process) from
/dev/null, most likely.

Then of course, there could be other explanations.  But this being
overwhelmingly the most likely one, if you don't try those solutions,
there's no point in trying to help you further...

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgp8zdVYVQEhV.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: Set Environment for java based tools thru python script

2008-10-14 Thread Derek Martin
On Mon, Oct 13, 2008 at 05:07:16PM -0700, [EMAIL PROTECTED] wrote:
> I run a Java app with subprocess from Python script. This python
> script is called from another Python Wrapper.
> 
> 
> python = subprocess.Popen(["toolname.sh", "-args", arg1, arg2],
>   stdout=subprocess.PIPE,
> stderr=subprocess.PIPE)

This (toolname.sh) looks like a shell script (though technically,
there's no reason it couldn't be a python script).  

Unfortunately, from what you've written here, it's neither clear what
processes start what processes, nor what the intended result is.  You
have said you have 3 programs, but you've only shown the interactions
between two of them.  My suggestion would be to rewrite your request,
explicitly name the processes (even if it is just with letters, A, B,
and C), and show which processes start which other processes, and
probably explain a little about what each one is supposed to do.  That
said, see below.

> I can run it manually from the command line. But fails when I execute
> the wrapper Python script
> 
> How do I source the java environment from the wrapper. It fails with
> the following message.
> 
> ...16605 Segmentation fault  $JAVA_HOME/bin/java $JAVA_OPTIONS -
> classpath $CLASSPATH xx "$@"

Again, it's not clear what you're trying to do, but I'm assuming you
have some script that sets environment variables, and that's what you
mean by "source the java environment..."

If so, you can't.  You either need to source the environment before
running the Python program, or have the python program read a file
that contains the environment and do its own parsing, setting the
environment variables appropriately.  A child process, in general, can
not insert environment variables into the environment of its parent.

If what you're trying to do isn't covered by the above, then I think
you'll need to try to explain it better.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpBooNZ2b3rY.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: indentation

2008-10-19 Thread Derek Martin
On Sun, Oct 19, 2008 at 06:05:08PM +, Jorgen Grahn wrote:
> Doesn't pretty much everyone use spaces and a four-position indent? 

I can't speak for everyone, or even "pretty much everyone"... but I
know of several people who favor the idea of "indent with tab, align
with space."  The advantage to this scheme is that anyone using a
half-sane editor can very easily change the level of indentation to
their preference, meanwhile keeping the rest of the code aligned
properly (though this may well interfere with keeping line lengths to
80 columns, or some other decided-upon number).  I favor this myself
actually, though I rarely use it for Python code I write, because that
almost invariably needs to work with someone else's code who insists
on the "standard" you mentioned.  

I know plenty of people who prefer a full 8-column indent, feeling that
it makes indentations (and therefore the logical blocks wich the
indentation is meant to indicate) much clearer, though most of them
are primarily C coders.  Some switch to 4 for python, and some prefer
to keep 8 for pretty much everything they write.

> I don't think I've ever come across any half-decent Python code
> which didn't follow that convention.

I have. :) Unless one defines a lack of tabs as a criteria of
"half-decent Python code" -- which I obviously don't.

> [0] This is an old and tedious topic ... 

This is very true... though clearly to anyone who hasn't encountered
it before, it is rather new.

> my view on TABs is that they are useless iff they aren't
> rendered the same way everywhere. The size 8 is hard-coded into
> terminals, printers and programs since ancient times; thus
> anything else is wrong.

This, on the other hand, is quite false -- not your opinion, perhaps,
but all of the facts you've put forth in support of it.  The tab size
of nearly every tty device I've interacted with in the last 25 years
*defaulted* to 8, but is configurable using any of various terminal
control programs, such as tabs, stty, etc. (though I wouldn't know how
to do this on Windows, or if it's even possible/relevant)...  The
utility of adjustable tabs is what I already stated above.  I'm not
saying you should change it... just that it is very much *not*
hard-coded.  In fact, most of the terminal devices I've used let you
set arbitrary tab stops at whatever column positions you like.
Occasionally useful, though not to me personally.

One thing is for sure: it's essential that whatever formatting you
decide to use, everyone touching that code needs to use the same one,
or else the result is an annoying mess.  Vim (and quite probably other
editors) solves this by providing a way to set the options in the file
you're editing, which is one of many reasons why I favor it over
anything else.  For example, at the top of your file:

  #!/usr/bin/python
  # vim:ts=4:sw=4:expandtab

Though of course, using this kind of mechanism quickly becomes gross
if everyone is using a different editor, and they all support a
similar but different mechanism for doing so.



-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpWonPLlq6C1.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Python equivalent for C module

2008-10-20 Thread Derek Martin
I'd like to know if it's possible to code something in Python which
would be equivalent to the following C:

[Assume bool is typedef'd to int, and TRUE and FALSE are #defined to 1
and 0, respectively]

 debug.c 
#include 

bool DEBUG;

void dprint(char *msg)
{
if (DEBUG){
printf("DEBUG: %s", msg);
}
}

 end of debug.c 

The idea being that all modules of the program would "import" this
code via the header file:

 debug.h 
extern bool DEBUG;
void dprint(char *msg);
 end of debug.h 

I'm specifically trying to avoid having to create a debug object and
pass it around... All modules should have visibility into the state of
whether DEBUG is turned on or off, and be able to use dprint().  Can
Python do this?

I tried creating debug.py as such:

 debug.py 
DEBUG = True
def dprint(msg):
if DEBUG:
print("DEBUG: %s" % msg)
 end 

Then in the modules that wanted to use it, I did:

from debug import DEBUG, dprint

But I got some weird behavior.  The imported copy of DEBUG is
read-only; if you update it, the name DEBUG points to a different
object which the other modules can't see.  After doing some reading of
the docs, this behavior is explained and understood (though obviously
not what I want).  It just occured to me that I might be able to get
around that by using a setter function in the module itself... I'll
try this later. 

The other weird behavior was, once I changed the value of DEBUG,
dprint() started to behave oddly.  No matter what I passed as an
argument (and no matter what I set the value of DEBUG to be), it
started printing the exact literal string:

DEBUG: %s

whenever it was called.  It was as if the function couldn't see the
parameter msg, which was passed via the call.  Most unexpected, and
definitely undesirable.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgp4DKmvYHFbt.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python equivalent for C module

2008-10-20 Thread Derek Martin
On Mon, Oct 20, 2008 at 07:29:16PM +0200, Bruno Desthuilliers wrote:
> This should have been:
> 
> fprintf(STDERR, "DEBUG: %s", msg);

No, it shouldn't have.  If I turn on debugging, I  want the debug
messages to go to stdout, so that they can be captured along with the
output (of which there is almost none anyway) to clearly indicate when
they happened.

> STDOUT is for *normal* program outputs. Debug informations, warnings, 
> and all verbosity should go to STDERR.

That's your opinion, and I disagree.  Besides which, if you're running
a program in debug mode, you're DEBUGGING... "normal" does not apply.
You're being rather presumptuous... you don't even know how my program
is being used.

> >Then in the modules that wanted to use it, I did:
> >
> >from debug import DEBUG, dprint
> >But I got some weird behavior.  The imported copy
> 
> It's not a copy.

Actually, I'm pretty sure it is; i.e. there are two copies of the
name: one in the namespace of the module, and one in the namespace of
the file into which I imported it.  At the time they are created, they
both point to the same object.  Is that not the very definition of a
copy?  The object itself may exist only in one place, but it has two
names; one in each namespace. 

> >of DEBUG is
> >read-only;
> 
> It's not read-only.

The *object* very much is: it is immutable.  The name of that object
is DEBUG, and thus DEBUG is read-only.  You can make DEBUG point to a
different object by binding a different value to it, but if that value
is of an immutable type, it will still be a read-only object.

In the sentence I wrote, as well as in general, "DEBUG" actually
refers to two different things: the object bound to the name, and the
name itself.  It's up to the reader to infer which sense is correct
given the thing being said about it.  It just so happens that the
English sentence I wrote refers to both simultaneously.

> Just use a fully qualified name, so you dont make DEBUG local:
> 
> import debug
> print debug.DEBUG
> debug.DEBUG = True
> print debug.DEBUG

Right, several people have already pointed this out.  Which leads me
to believe that the point of your reply was to berate me into
following your conventions, which I have no interest in doing, in part
because they are counterproductive to my goals, and in part because
they are counter to the way I've been programming for 25 years.
Fortunately, it's not your call how I write my code.

> Now note that ALL_UPPER names are - by convention - considered 
> 'constants'. If this is supposed to be altered, don't write it ALL_UPPER.

YOUR convention, not mine.

> Also and FWIW, Python has a logging module in it's stdlib. Please use it 
> instead of any half-backed squared-wheel homegrown solution.

Note that the correct possessive form of "it" is "its" with no
apostrophe.  This was the only thing of value which you contributed,
though really, using that is way overkill for my needs.  If I've
written bad code, by all means, please correct it.  If I've written
code in a style that you happen not to like, please feel free to keep
that to yourself.

> My 2 cents

Must be Zimbabwe currency...

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpcR9dOjsCO1.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python equivalent for C module

2008-10-20 Thread Derek Martin
On Mon, Oct 20, 2008 at 10:43:55PM +, Steven D'Aprano wrote:
> All of this is just splitting hairs, 

Indeed... :)

> because you don't really mean Python is making a copy of the name
> 'DEBUG', but of the *data* that DEBUG refers to, namely the object
> True. 

Well, as long as we're having semantic discussions...  If you reread
my post, it should be clear that what you wrote above can not possibly
be the case.  If you recall, my intent was to make a copy of a means
of accessing the value known by the name "DEBUG" contained in the
debug module, which could be accessed from any module in the program.
Clearly the data itself *must not* be a copy, or else what I was
trying to do would never have worked.  What I was refering to as a
copy was in fact essentially the name, or more accurately (as regards
my conception of purpose) a reference to the data.

> > The *object* very much is: it is immutable.
> 
> So what? The *name* DEBUG is not read-only.

You may have missed where I explained that the name refers to two
different things, and that I, speaking in loose terms, was refering to
both things simultaneously but in different contexts.  I was speaking
loosely -- I was using "read-only" as a figure of speech of sorts, and
elaborated *correctly* what actually happens.   Again, if you reread
my original post with that explanation in mind, I think you'll find
that this is the only sensible interpretation for what I wrote.

> Actually it is a very common convention in Python circles. I often use it 
> myself. However it's not the only one, and from time to time I use 
> others. I would consider using DEBUG unusual, but not excessively so.

To be honest, same here.  My post contained throw-away C code that I
typed up on the fly, and the same goes for the Python -- I simply
translated the C code literally, so to speak.  As has been pointed out
in a different thread (by Bruno himself, if I'm not mistaken), the
code hastily posted here need not represent the code that one would
write in "real" programs.  But I find it offensive that certain people
here can't resist lambasting some posters who have simple questions,
because the code they posted isn't up to their particular favorite
coding "standards" (i.e. conventions), and I will speak out about it,
especially when it's done to me.  I didn't ask about coding
conventions, but Bruno's response was roughly 75% about how my code
sucks, and maybe 25% about answering my question.  And what part did
answer my question was completely redundant.  It was roughly 95% a
waste of time and energy for both him and me, though I did learn about
the logging module...
 
> Deary deary me... you come along here, asking noob Python questions, and 
> then get shirty when people give you friendly and perfectly good answers. 

Well, we disagree that the answer Bruno provided was either friendly
or perfectly good.  I did receive perfectly good answers, and if you
read the whole thread, you saw me accept such answers graciously.
Bruno's answer smacked of a superiority and arrogance that is not
uncommon among certain frequent posters.  He has no idea what my
program does, or what my real code looks like, but apparently deigns
himself the Code Police, and finds it necessary to punnish people for
posting code which does not conform to his idea of Programming Law.  

I can't deny that I should have been able to answer my own question
with only a few more moments thought...  Though I don't think it's
quite right to characterize questions about scope as "noob" questions.
I suspect actual noobs don't yet know enough to ask such questions.

> Are you trying to ensure that the next question you ask remains 
> unanswered?

Not at all, just trying to preempt the pound of attitude that often
goes with the ounce of answers.  But if the choice is between no
answer, and an answer that barely manages to avoid calling me an idiot
(especially over coding style), I'd rather have no answer.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgp1spA6WQhn4.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python equivalent for C module

2008-10-20 Thread Derek Martin
On Mon, Oct 20, 2008 at 10:28:15AM -0700, Gary Herron wrote:
> > The other weird behavior was, once I changed the value of DEBUG,
> > dprint() started to behave oddly.  No matter what I passed as an
> > argument (and no matter what I set the value of DEBUG to be), it
> > started printing the exact literal string:
> >
> > DEBUG: %s
[...]
> I don't believe it -- send your *actual* code, and we'll all have a look.

When I finally had access to my code again, my error was immediately
obvious.  I'd typed: 

  print("DEBUG: %s")

Weird thing was, I remembered it actually working.  And it had... In
between testing the two cases, I'd accidentally deleted the module and
had to recreate it.  The first time no bug, second time, well,
resutled in this thread.  I'm chalking the whole thing up to coding
when not sufficiently awake to do so. ;-)

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpPHO8fo3dXL.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to examine the inheritance of a class?

2008-10-24 Thread Derek Martin
On Fri, Oct 24, 2008 at 11:59:46AM +1000, James Mills wrote:
> On Fri, Oct 24, 2008 at 11:36 AM, John Ladasky <[EMAIL PROTECTED]> wrote:
> > etc.  The list of subclasses is not fully defined.  It is supposed to
> > be extensible by the user.
> 
> Developer. NOT User.

It's a semantic argument, but John's semantics are fine.  A library is
code intended to be consumed by developers.  The developers *are* the
users of the library.  *End users* use applications, not libraries.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpug97BBp01J.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python suitable for Midi ?

2008-10-28 Thread Derek Martin
On Tue, Oct 28, 2008 at 06:54:57PM +0200, Chuckk Hubbard wrote:
> The problem I've run into is that I can't set the audio to a higher
> priority than the GUI (Tkinter).  If I move the mouse over the app, no
> matter what, I get audio dropouts.  AFAICT this is the same for all
> Python, regardless of what modules one uses: you can't assign system
> priorities to different threads.  If you're planning to pipe MIDI to
> another app for playback, maybe it won't be an issue for you.

FWIW...  You could take your own advice, and devide your application
in two: one process manages the GUI, and the second is a back-end
process that plays the MIDI.  Your GUI can even launch the back end,
which will inherit the priority of the GUI, after which the GUI can
reduce its own priority (the priority of the back end will not be
affected by the change)...


-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpIZwcieNKvY.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: open a shell prompt froma python program

2008-10-30 Thread Derek Martin
On Thu, Oct 30, 2008 at 03:53:52AM -0700, gaurav kashyap wrote:
> HI,
> I am getting the following error:
> 
> konsole: cannot connect to X server
> 
> do i need to install the related files.

Maybe, but given that error message, probably not.

You would do yourself a great favor by providing a lot more detail
about what you are trying to do...  On a Unix/Linux system, unlike
Windows, there is no one single "shell prompt window" -- there are
lots of them.  They all need the X Window System (a suite of software
which provides a GUI interface to Unix systems -- it's not "built in"
like it is in Windows).  X works as a client-server model, and you
need to make sure X authentication is handled properly.  Depending on
what you are doing, this can be either very easy, or very complicated.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpnBx41jvOnI.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: open a shell prompt froma python program

2008-10-30 Thread Derek Martin
On Thu, Oct 30, 2008 at 02:47:48AM -0700, gaurav kashyap wrote:
> Simply i want to open a shell prompt from a python program.

If this is literally true, then you just need to figure out what
command will open a terminal window from the shell prompt.  Once you
figure that out, it's as simple as:

cmd = "whatever your shell command is"
os.system(cmd)

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpjESdNVsDLA.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: Official definition of call-by-value (Re: Finding the instance reference...)

2008-11-16 Thread Derek Martin
On Thu, Nov 13, 2008 at 11:58:18AM -0800, [EMAIL PROTECTED] wrote:
> I have yet to see any reasonable definition of a Python value in the
> Python docs or elsewhere, despite the fact that a value is one of
> the three defining characteristics of an object, a central concept
> in Python.

Why does it need to be defined in the Python docs?  Is this really
even an important question to answer?  Are you unable to write correct
functional programs in Python without having it answered?  I suspect
it's not an issue...

Let's assume for the moment that it is, though.  The term "value"
already has a meaning... the one ascribed to it by its use in natural
language.  One on-line dictionary includes this among its definitions:

  magnitude; quantity; number represented by a figure, symbol, or the
  like: the value of an angle; the value of x; the value of a sum.

It seems clear that this, or something extremely close to this, is
what is meant in the Python docs by the unqualified use of the term.

So, then, what is the value of a Python object?  As has been alluded by
others, it is not possible to formally define or enumerate what such a
value is, in general terms, because the term "object" refers to 
to a thing with neither a deterministic nor static identity; in the
abstract an object has no inherent or intrinsic value.  

The best you can hope to do is define it in context.  To illustrate:
In natural language and the physical world, an object has any number
of values; for example a book has a title, a topic, a weight, a
height, a length, a width, a page count, a word count, a purchase
price, a printing cost, a number of copies sold, a profit per unit
sold, etc. to the limits of your imagination regarding ways to
describe books.  Which of these is its "value" depends upon the
context in which you are discussing the book.  To the reader, the
value is the price he pays for the book, or perhaps some measure of
the enjoyment he derives from reading it (possibly the amount he would
be willing to pay to buy it).  To the seller, the value is perhaps
best represented by the profit per unit sold.  To the publisher's
shipper (think FedEx), the value might best be described in terms of
its weight...

Just as in the physical world, in Python an object can be defined such
that it evaluates to different values in different contexts, even
though its state may not have changed between those different
contexts.  Therefore the value of an object is dependent upon its data
attributes AND its behaviors defined as methods, as well as the
context in which it is accessed, and is the value to which the object
evaluates in a given expression or context.

If you like, you could think of the value of an object as the set of
all possible values to which the object may evaluate in every possible
context, given a particular state of the object.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpbIUrXOwI9A.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: Official definition of call-by-value (Re: Finding the instance reference...)

2008-11-16 Thread Derek Martin
On Sun, Nov 16, 2008 at 06:06:20AM +, Steven D'Aprano wrote:
> >>> * Do all objects have values? (Ignore the Python
> >>>  docs if necessary.)
> >>
> >> If one allows null values, I am current thinking yes.
> > 
> > I don't see a difference between a "null value" and not having a value.
> 
[...]
> It wasn't until the fifth century C.E. that Indian mathematicians
> invented the concept of zero, and it took many centuries for the
> idea to get to Europe via the Arabs.

I think he meant None...  Or at least, I personally see a distinction
between zero and None (and so do the Python docs).  Zero is a value,
whereas None is specifically intended to denote the lack of any value.
I would, FWIW, only make such a distinction in the context of a
computer program...  Clearly in mathematics and elsewhere, zero is the
lack of a value (it is the value of nothingness).

> "The value of the object is the number of sheep in the paddock, unless 
> the number of sheep is zero, in which case the object has no value..."
> which is needlessly complicated.

For conversation, yes... but technically correct.

> I say that 0 is a perfectly fine value. So is None, [], {}, and any other 
> null-value. I recommend you don't complicate and confuse matters by 
> trying to treat them differently.
> 
http://www.python.org/doc/1.5.2/api/noneObject.html

7.1.2 The None Object

PyObject * Py_None
The Python None object, denoting lack of value. This object has no
methods. 

> > The value of a class is it's attributes? Are you saying that attributes
> > of an object are part of its value?  That would mean that 'a' and b'
> > below have different values?
> > 
> > class My_int(int):
> > def __init__(self): self.foo = None
> 
> That won't work you know.

Perhaps not, but it illustrates the point.  This *does* work:

>>> class myint(int):
... def __init__(self, val):
... int.__init__(val)
... self.foo = None
... 
>>> b=myint(3)
>>> b
3
>>> b.foo
>>> print b.foo
None
>>> a=3
>>> a==b
True

So, your description of value is not consistent with Python's
behavior...  Python says the two objects I just created have the same
value.  But by your definition, they don't.  One of you is wrong... ;-)

> That depends on whether the existence of foo makes a difference to you or 
> not. Consider pickle. Since pickle can't predict what aspects of the 
> object are important, it must treat *everything* as significant, and 
> pickle will absolutely treat a and b as having different values.

I don't think that's clear...  pickle will treat a and b as having
different *data*...  For what it's worth, I think the statement in
the language reference that all objects have a type, an ID, and a
value is quite a poor choice of words.  Back in 2000, Frederik Lundh
put it much more accurately, I think:

  http://effbot.org/zone/python-objects.htm

I think it's up for debate whether the value of attribute of an object 
is part of the object's value, if that attribute can never be the
evaluated value of the object itself in an expression -- though only
because it's a semantic argument, and the semantics haven't been
defined.  I don't think choosing to say that it is or isn't makes
any practical difference, at all.

> But other functions may have weaker constraints. Consider sum([a, b]). 
> The function sum makes no promises that it will return the same type as 
> it's arguments. Since, *for the purposes of addition*, the foo attribute 
> has no significance, sum() makes no promise whether the sum of a and b 
> will include the foo attribute. In fact it does not. As far as addition 
> is concerned, a and b have the same value, and the foo attribute is lost.

You seem to be making my point, that the value of an object is
context-specific...

> > I propose that attributes are not part of a class' (or any other
> > object's) value and that a class object has no value.

Both of these suggestions are clearly problematical, as when used in
an expression, an object can (and usually does) evaluate to some value
for the purpose of evaluating the expression, and that value may be an
attribute of the class, depending on what we decided is the right
answer to the question above.  

> I see you are still insisting that value is something that objects
> "have" rather than "are". 

This falls down, say, for a date object which has the value of the
string representation of the date when printed, and a numeric value
(or some other time object) when used in other expressions, both from
a philisophical and practical standpoint.

Furthermore it falls down seman

Re: Official definition of call-by-value (Re: Finding the instance reference...)

2008-11-16 Thread Derek Martin
On Sun, Nov 16, 2008 at 08:38:25AM +, Steven D'Aprano wrote:
> I believe that the language reference says that objects have an identity, 
> a type and state, but I'm too lazy too look it up. I'd be happy with that 
> definition.

They do indeed say value, not state.  As I said in a different
message, I'd agree that it's not a very clear definition.

> > I don't see how saying "the value of an object is itself" is
> > particularly useful.  We already have a word for what an object is, it
> > is "object". :-) 
> 
> I didn't say it was very useful. As far as I'm concerned, asking what the 
> value of an object is is not a useful question.

Now we agree. :)

> > The result of x==y depends solely on the behavior (methods) of x.
> 
> Nonsense.

It's wrong to say *solely*, but the value of x==y does indeed depend
on the behavior of the methods.

> I think the value of x is "a thing which claims to be equal to
> everything on Tuesdays, and equal to nothing every other day".

That isn't its *VALUE* -- it's its *IDENTITY*.  My weight is not my
identity... but in a certain context, it could be considered my value.
 
-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpVW1VzHngyK.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Re: Official definition of call-by-value (Re: Finding the instance reference...)

2008-11-16 Thread Derek Martin
On Sun, Nov 16, 2008 at 09:30:45AM +, Arnaud Delobelle wrote:
> [...]
> > If you like, you could think of the value of an object as the set of
> > all possible values to which the object may evaluate in every possible
> > context, given a particular state of the object.
> 
> This definition looks a bit circular to me ;)

Why, because it has the word "value" in the definition?  It's not
circular.  The thing being defined is "value of an object".  The word
"value" has a pre-existing well-understood natural language definition.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpJNlOWqxfA7.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list


Unix Change Passwd Python CGI

2009-02-24 Thread Derek Tracy
I am using python version 2.5.1 and need to create a python cgi application
to allow a user to change their unix password.

Apache is running on the same system that needs the password changed.  I
need to keep security high and can not install additional modules at this
time.

I just need a general direction to start looking, and I do not have expect
installed on the system.

Any ideas would be wonderful!

R/S --
-
Derek Tracy
trac...@gmail.com
-
--
http://mail.python.org/mailman/listinfo/python-list


Re: python confusion possibly related to pickle

2008-05-18 Thread Derek Martin
On Sun, May 18, 2008 at 08:28:34PM +0100, Dennis wrote:
> The problem that's got me annoyed is that after getting said error I
> close the shell window, open a new one, run the python interpreter
> and type "import pickle" and get the error that the script I'd run
> earlier caused. Why is this ?

Well, what's the error?  Sounds like your system could be b0rked (or
at least your python installation)... but depending on the error,
there could be other explanations.  

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpkARlxW8Y91.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list

Re: Python CGI Upload from Server Status

2008-06-06 Thread Derek Tracy
On Fri, Jun 6, 2008 at 9:16 AM, John Dohn <[EMAIL PROTECTED]> wrote:

> On Sat, Jun 7, 2008 at 12:50 AM, Derek Tracy <[EMAIL PROTECTED]> wrote:
>
>> I am trying to create a simple python cgi app that allows the user to kick
>> off an ftp from the server the cgi is on to another server; I have that
>> piece working using ftplib but since the files in question are usually very
>> large (500mb to 2gb) in size I want to be able to display some sort of
>> status to the user, preferrably a progress bar of some sort.
>
>
> You'll need some AJAX progress bar (hint: google for this term ;-) that
> will be getting updates from the server or request an update every second or
> so.
>
> The question is if your upload code can provide progress tracking? If it's
> just a call to some xyz.upload("/here/is/my-500M-file.bin") that only
> returns after several minutes of uploading without giving you any updates on
> how fast things go you're probably out of luck.
> OTOH if it can do e.g.callbacks for progress reporting or if it can run in
> a separate thread that you could query somehow you can hook that to that
> AJAX thing of your choice.
>
> JDJ
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>

I will look for the AJAX Progress Bar, but I will admit I have never touched
AJAX and haven't written javascript in years.

I patched Pythons ftplib.py storbinary() to send callbacks to the specified
method, so I have the callbacks locked down.  The thing to note is that this
app isn't allowing the user to upload to the server the cgi is on but rather
allowing the user to kick off an ftp process on the server to another
server.

Would there be a way to do this with python cgi and automatically append or
update information on the page it is displaying?

-- 
-
Derek Tracy
[EMAIL PROTECTED]
-
--
http://mail.python.org/mailman/listinfo/python-list

Re: Does '!=' equivelent to 'is not'

2008-06-17 Thread Derek Martin
On Tue, Jun 17, 2008 at 04:33:03AM -0300, Gabriel Genellina wrote:
> > Basically 'a is b' and 'not(a is b)' is similar to 'id(a) == id(b)'
> > and 'not(id(a) == id(b))'
> 
> No.

Sure it is... he said "similar"... not identical.  They are not the
same, but they are similar.  

Saying a flat "no" alone, without qualifying your statement is
generally interpreted as rude in English...  It's kind of like how you
talk to children when they're too young to understand the explanation.
Yucky.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpg79fnwMq5d.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list

Re: Does '!=' equivelent to 'is not'

2008-06-18 Thread Derek Martin
Yaieee!

On Wed, Jun 18, 2008 at 01:32:28AM -0400, Terry Reedy wrote:
> > >Saying a flat "no" alone, without qualifying your statement is
> > >generally interpreted as rude in English...  

> As a very much native English speaker I disagree that 'No' is 
> necessarily rude.  

I never said it was necessarily anything.  Generalities generally have
lots of exceptions. :D  It definitely isn't *necessarily* rude, and I
didn't interpret Gabriel's message as rude.  I was merely pointing out
that such statements are often interpreted as rude, whether or not
they were intended that way.  FWIW, my post wasn't intended to be a
post at all, but instead a private message to Gabriel.  I guess I
zigged when I should have zagged... ;-)

That said, what he did do, was to contradict a statement which was
literally true, in an abrupt manner.  Lots of people would interpret
this as rude demeanor.  His commentary was spot on, but the way he
went about making it has a tendency to make some (perhaps many)
responees defensive, if not belligerent.  But, if I actually thought
Gabriel was intentionally being rude, I wouldn't have bothered to say
anything, and just deleted all his posts. :)  I don't even think an
apology was warranted...

On Wed, Jun 18, 2008 at 07:01:23AM -0700, Paul McGuire wrote:
> Geez, man, this is Usenet.  If you want rude or condescending, the
> answer would have been "No, you flatulent moron."  Or maybe the
> alarmist, "No! No! No!"

Sure, those statements would generally be considered *blatantly* rude
(but still sometimes may not be, in context).  This does not mean that
less blatant statements are not also rude.  Geez indeed...

> I see the unqualified "No." often on this list, 

I see it lots of places, and maybe as much as 1/3 of the time, I see
it start flame wars.  It seemed clear to me that Gabriel had no
intention of being offensive...  All I'm saying is that if you want to
avoid offending some people unintentionally and needlessly, it's a
good idea to avoid making curt statements, especially curt negative
statements.

If the intention is to signal that more is to come, a simple
improvement is to add an elipsis, whose purpose is exactly that:
"No..."  But even more effective at avoiding the appearance of being
rude are statements like "Not exactly..."  "I don't think so..." etc.
They're not even all that much extra typing.

There are lots of times when a simple "no" is exactly what's called
for.  "Do you like dark Chocolate?" "No."  "Are you watching the
Celtics game?"  "No."  Or even, "Is the baby's new shirt blue?"  "No,
it's green."  

Being concise is not the same as being curt.  Tone also plays a big
role, but conveying the appropriate tone of a simple "no" is pretty
much impossible in an e-mail.  In written communication, it should be
avoided like the plague.

> Back in my college days, I would not be surprised for a professor to
> respond "No." 

Sure, lots of professors are arrogant, insensitive jerks.  Does that
make it OK?  But, depending on the context and the professor's tone,
even the situation you describe isn't necessarily rude.  It often
isn't.

The world is full of Jerks with a capital 'J'.  Imagine if it weren't?
How nice that would be...  But, all I was offering here was a
suggestion regarding how to not appear like a Jerk when one isn't
intending to.

> but as one of the most informed and careful posters on this list,
> I'm inclined to give Gabriel a little slack.

Sure.  But not everyone here knows Gabriel.  Not everyone here has
seen his informed contributions.  Not everyone here has been here more
than a day...  More than a few people have posted on this list
complaining about the sort of responses people provide on this list,
and many such complaints are quite reasonable (though sometimes the
person doing the complaining is himself rather unreasonable, if not
completely bonkers, I admit). 

I am somewhat incredulous that this required explanation...  In the
end what I thought would be a nice little, "hey, avoid this pot hole"
kind of note seems to mostly have generated a lot of silly noise.  I
now retire from this discussion, and crawl back into my happy
lurk-spot. :)

Cheers

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpCV8qGT1EYK.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list

Re: Using Python To Launch Python

2008-07-14 Thread Derek Martin
On Mon, Jul 14, 2008 at 02:01:04PM -0700, aha wrote:
> Since my application has it's own version of Python installed with
> it how should I use the system Python to launch the version of
> Python that launches my Application.  Yes, this is a convoluted
> process, but not all Pythons are built the same :)

/usr/local/bin/$APPNAME:

#!/bin/sh

INSTALLPATH=
PATH=$INSTALLPATH/bin:$PATH
exec $INSTALLPATH/bin/python $APPNAME "$@"

Doesn't get much simpler than that. :)  You can certainly do the
equivalent in Python... there's not much difference.  Slightly less
typing in bourne/bash shell, I guess...

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgplmkg6rt2dJ.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list

Re: Using Python To Launch Python

2008-07-14 Thread Derek Martin
On Mon, Jul 14, 2008 at 05:40:43PM -0400, Aquil H. Abdullah wrote:
> You've hit the proverbial nail with the hammer.  The problem is that my
> application needs to run under both the Linux and Windows OSs, so while I
> would love to use a nice sh, csh, or bash shell script. My hands are tied
> because Windows does not provide such wonderful shells.

*Provides*, no... neither does it provide Python, for what that's
worth.  But you can certainly get it (bash):

  http://win-bash.sourceforge.net/

I suppose it's not worth installing just for this purpose though...
But you can provide with your application a DoS batch file that does
exactly the same thing (in addition to a shell script).  The user
would quite intuitively use whichever were appropriate, or follow your
provided directions otherwise.  Or, the equivalent in (hopefully
OS-agnostic) Python:

import os, sys

# I believe this gets the name of the root in all major OSes
def root_dir(path):
if os.path.dirname(path) == path:
return path
return (root_dir(os.path.dirname(path)))

appname = 
root = root_dir(os.getcwd())
install_path = os.path.join(root, "usr")
bin_path = os.path.join(install_path, "bin")
os.environ["PATH"] = bin_path + os.pathsep + os.environ["PATH"]
python_path = os.path.join(bin_path, "python")
args = sys.argv[1:]
args.insert(0, os.path.join(bin_path, appname))
args.insert(0, python_path)
args.insert(0, python_path)
os.execv(python_path, args)





pgpQG92HCsITg.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list

Re: Multiple variable control in for loops. Doable in Python?

2008-07-18 Thread Derek Martin
On Fri, Jul 18, 2008 at 12:21:49PM -0700, mark floyd wrote:
> I'm new to Python and have been doing work converting a few apps
> from Perl to Python.  I can not figure out the comparable Python
> structures for multi-variable for loop control.
[...]
> I spent a good part of yesterday looking for a way to handle this
> style for loop in Python and haven't been able to find an
> appropriate way to handle this control style.  

One wonders why... :)

> We have this style for loop all over the place and not being able to
> find a similar structure in Python could be a problem.  Any pointers
> to a Python equivalent structure would be much appreciated

Even if Python didn't offer a way to write a for loop in a similar
fashion (someone else replied about that already), why should it be a
problem?  In general control structures can be rewritten as some other
kind of control structure.  For example, this does exactly what your
for loop examples do:

  i = 0
  j = 0
  while i < 5 and j < 10:
  print i, j
  i += 1 
  j += 1

Though, this example is silly, as it will always terminate after the
5th iteration of the loop, and there is no need to have j being used
as a control variable... it's termination condition will never be met.
Though the example illustrates the techique, even if the example is
bogus.

Another way is to use functions to modify the values of i and j.
Writing your loops this way, you can have as many control variables as
you need, and your formula for incrementing those control variables
can be as varied as complicated as you can imagine.

  def some_increment_function(i):
  # Do some complicated processing of i
  i = ...
  return i

  def other_incrjmental_function(j):
  # Do some complicated processing of j
  j = ...
  return j

  i = 0
  j = 0
  while i < 5 and j < 10:
  print i, j
  i = some_increment_function(i)
  j = other_increment_function(j)

And of course, you could also replace the loop terminating conditions
with functions that return a value which can be interpreted as a truth
value.  If you wanted to get really crazy, you could even code the
control structure as a recursive function:

  def control(i, j):
  print i,j
  if not (i < 5 or j < 10):
  return
  else:
  control(some_increment_function(i), other_increment_function(j))


Should you really write control structures this way, generally?
Absolutely not (unless you're writing LISP or Scheme :)).  But the
point is, even if a given language doesn't have a particular syntactic
element that you're looking for, it's pretty much guaranteed to
provide a way to do what you're trying to do.  You just need to stop
thinking about your problem in terms of a particular syntactic
element, and start thinking about it in more general terms of what you
are actually trying to accomplish, and apply whatever syntax (usually
one of several) your language provides to do that.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpiKhYnuJoAw.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list

Re: Multiple variable control in for loops. Doable in Python?

2008-07-18 Thread Derek Martin
On Fri, Jul 18, 2008 at 05:28:32PM -0400, Derek Martin wrote:
>   def control(i, j):
>   print i,j
>   if not (i < 5 or j < 10):

Rather, if not (i < 5 and j < 10):

>   return
>   else:
>   control(some_increment_function(i), other_increment_function(j))

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpizP6MkhvZ8.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list

Re: Change PC to Win or Windows

2008-07-18 Thread Derek Martin
On Fri, Jul 18, 2008 at 03:46:13PM -0700, Joel Teichroeb wrote:
> Calling Windows PC seems to be something that Apple did so they would 
> not have to directly mention Windows. 

Actually it's something IBM did when they created the IBM PC.  Of
course, all IBM PCs ran MS-DOS, since that's how IBM sold them...
Then others started to build copies the IBM PC based on Intel
hardware, and the resulting class of computers was called,
collectively, "PC Clones" -- shortened to PCs -- by the industry and
its market.  Then companies like AMD and Cyrix started building
Intel-compatible CPUs, and the term PC was extended to include systems
built using those architectures.  Eventually Windows was released, and
PCs became Windows boxen running on Intel-compatible hardware, and I
personally know no one who doesn't use the term that way...

Much like the English word "bank" (and numerous others), the term "PC"
has come to have several meanings, one of which is the above.  You may
not like it, but we're pretty much stuck with the term, so you may as
well get used to it.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpGebEnJc6Ql.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list

Re: Change PC to Win or Windows

2008-07-21 Thread Derek Martin
On Fri, Jul 18, 2008 at 10:34:41PM -0700, Dennis Lee Bieber wrote:
> On Fri, 18 Jul 2008 19:14:43 -0400, Derek Martin <[EMAIL PROTECTED]>
> declaimed the following in comp.lang.python:
> 
> > On Fri, Jul 18, 2008 at 03:46:13PM -0700, Joel Teichroeb wrote:
> > > Calling Windows PC seems to be something that Apple did so they would 
> > > not have to directly mention Windows. 
> > 
> > Actually it's something IBM did when they created the IBM PC.  Of
> 
> Bah... PC was short for Personal Computer... 

I'm well aware... congratulations on completely missing the point.  I
was describing how the term PC has become synonimous with Windows
machines.

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpbl4K02hAFc.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list

Re: Change PC to Win or Windows

2008-07-21 Thread Derek Martin
On Sat, Jul 19, 2008 at 02:56:07AM -0700, Lie wrote:
> On Jul 19, 6:14 am, Derek Martin <[EMAIL PROTECTED]> wrote:
> > On Fri, Jul 18, 2008 at 03:46:13PM -0700, Joel Teichroeb wrote:
> > Much like the English word "bank" (and numerous others), the term "PC"
> > has come to have several meanings, one of which is the above.  You may
> > not like it, but we're pretty much stuck with the term, so you may as
> > well get used to it.
>
> That's not the point, 

It very much IS the point.  Language evolves based on common usage
patterns of the people who use it.  The term "PC" is commonly used in
English, in the United States and other English speaking countries, to
mean a computer running Microsoft Windows.  That's a simple fact that
you can not escape, no matter how much you may not like it (it just so
happens that I also don't like it, but I realized long ago the
futility of arguing against its usage).  It's still a fact, and I
described roughly how that fact came to be.  It wasn't something that
Apple started; it's been used this way in increasingly common usage
for at least 20 years, although exactly what combination of hardware
and software was being refered to as a "PC" has evolved over that
timeframe.

PC was a short form of "personal computer", which is how IBM came up
with the name.  Nevertheless, with the ubiquity of IBM hardware, and
subsequent popularity of clones running Microsoft operating systems,
the term "PC" has, in the present day, come to mean "a personal
computer based on Intel-compatible hardware running a flavor of
Microsoft Windows."  It is used this way by the consumer computer
industry, and it is used this way by the common population.  Ipso
facto "PC" means a windows box, in common English usage today.

You don't have to like it, and you don't even have to acknowledge it.
But if you choose not to, or argue against using it that way, you're
in denial, plain and simple.  

-- 
Derek D. Martin
http://www.pizzashack.org/
GPG Key ID: 0x81CFE75D



pgpLN8hSeYEOH.pgp
Description: PGP signature
--
http://mail.python.org/mailman/listinfo/python-list

  1   2   >