Re: [Tutor] using easy_install to download eggs

2009-10-05 Thread Rüdiger Wolf
http://stackoverflow.com/questions/529425/easyinstall-cache-downloaded-files

pip (http://pypi.python.org/pypi/pip/) is a drop-in replacement for the
easy_install tool and can do that.

Just run easy_install pip and set an environment variable
PIP_DOWNLOAD_CACHE to the path you want pip to store the files. Note
that the cache won't work with dependencies that checkout from a source
code repository (like svn/git/hg/bzr).

Then use pip install instead of easy_install




On Sun, 04 Oct 2009 18:41 +, "Tim Michelsen"
 wrote:
> Hello,
> I would like to use easy_install to cache packages registered at PyPi
> locally.
> 
> How can I do this for packages?
> 
> I tried the hints from:
> http://peak.telecommunity.com/DevCenter/EasyInstall#installing-on-un-networked-machines
> 
> It worked for some packages.
> But for others, the command
> 
> easy_install -zxad. mercurial
> 
> just creates a subdirectory and not an *.egg file.
> 
> How can I
> 1) use easy_install to download packages from PyPi to a locally saved
> egg-file?
> 2) use easy_install to download archinves (*.tar.gz / *.zip) to download
> the
> respective software package from the link indicated on PyPi?
> 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] using easy_install to download eggs

2009-10-05 Thread Tim Michelsen
Hi,
thanks for the hint.

> pip (http://pypi.python.org/pypi/pip/) is a drop-in replacement for the
> easy_install tool and can do that.
> 
> Just run easy_install pip and set an environment variable
> PIP_DOWNLOAD_CACHE to the path you want pip to store the files. Note
> that the cache won't work with dependencies that checkout from a source
> code repository (like svn/git/hg/bzr).

I tried this.

It saves the downloaded packages in a form of:
http%3A%2F%2Fpypi.python.org%2Fpackages%2Fsource%2FB%2FBareNecessities%
2FBareNecessities-0.2.2.tar.gz.content-type
http%3A%2F%2Fpypi.python.org%2Fpackages%2Fsource%2FB%2FBareNecessities%
2FBareNecessities-0.2.2.tar.gz

I was looking for *.egs or *.exe pachages.

Any ideas?

Regards,
Timmie

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


Re: [Tutor] using easy_install to download eggs

2009-10-05 Thread Rüdiger Wolf
Snip from PIP http://pypi.python.org/pypi/pip/0.4

Differences From easy_install
pip cannot install some packages. Specifically:
* It cannot install from eggs. It only installs from source. (Maybe
this will be changed sometime, but it's low priority.)

If you want to download eggs then you might try basketweaver?

http://pypi.python.org/pypi/basketweaver/
basketweaver is a tool for creating your own package index out of a
directory full of eggs. You can then point to this index with e.g.
zc.buildout or setuptools and thus be independant of PyPI.

Cheers
Rudiger


On Mon, 05 Oct 2009 12:28 +, "Tim Michelsen"
 wrote:
> Hi,
> thanks for the hint.
> 
> > pip (http://pypi.python.org/pypi/pip/) is a drop-in replacement for the
> > easy_install tool and can do that.
> > 
> > Just run easy_install pip and set an environment variable
> > PIP_DOWNLOAD_CACHE to the path you want pip to store the files. Note
> > that the cache won't work with dependencies that checkout from a source
> > code repository (like svn/git/hg/bzr).
> 
> I tried this.
> 
> It saves the downloaded packages in a form of:
> http%3A%2F%2Fpypi.python.org%2Fpackages%2Fsource%2FB%2FBareNecessities%
> 2FBareNecessities-0.2.2.tar.gz.content-type
> http%3A%2F%2Fpypi.python.org%2Fpackages%2Fsource%2FB%2FBareNecessities%
> 2FBareNecessities-0.2.2.tar.gz
> 
> I was looking for *.egs or *.exe pachages.
> 
> Any ideas?
> 
> Regards,
> Timmie
> 
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Using command line tool with python script

2009-10-05 Thread Oleg Oltar
Hi!

I want to try to use a command line script with my python application. The
task is the following, my database stores some initial data for the script
and
I need to execute a command line application in a following way:

$ application -parameter1 -file1

where file 1 is a file which contains my initial data, and parameter1 is
unrelated parameter

the workflow as I see it know is following

initial_data = get_initial_data_from_db()
file = open('temp.txt', 'w+')
file.write(initial_data)
file.save()
os.popen4("application -parameter1 -file temp.txt")

I wonder if that possible to execute this script (called application)
without writing the file with initial data to the hard disk?


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


Re: [Tutor] Using command line tool with python script

2009-10-05 Thread bob gailer

Oleg Oltar wrote:

Hi!

I want to try to use a command line script with my python application. 
The task is the following, my database stores some initial data for 
the script and

I need to execute a command line application in a following way:

$ application -parameter1 -file1

where file 1 is a file which contains my initial data, and parameter1 
is unrelated parameter


the workflow as I see it know is following

initial_data = get_initial_data_from_db()
file = open('temp.txt', 'w+')
file.write(initial_data)
file.save()
os.popen4("application -parameter1 -file temp.txt")

I wonder if that possible to execute this script (called application) 
without writing the file with initial data to the hard disk?


Take a look at os.system()

--
Bob Gailer
Chapel Hill NC
919-636-4239
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Using command line tool with python script

2009-10-05 Thread Kent Johnson
On Mon, Oct 5, 2009 at 10:22 AM, Oleg Oltar  wrote:

> os.popen4("application -parameter1 -file temp.txt")
>
> I wonder if that possible to execute this script (called application)
> without writing the file with initial data to the hard disk?

If "application" can take its input from stdin then you can use the
subprocess module to launch it and pipe data to it. If "application"
requires a file for input then I think you have to write a file.

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


Re: [Tutor] small program

2009-10-05 Thread Andrius
Bye bye.

Regards,
Andrius


On 04/10/2009, Luke Paireepinart  wrote:
> On Sun, Oct 4, 2009 at 1:59 PM, Andrius  wrote:
>
>> Very good. Nice to hear that from another 'untouchable'. Must to
>> confirm for your that I'm usually left wing fella, so, if you have
>> nothing what to say for my in my case - please, fuck off.
>
>
> Re: http://catb.org/~esr/faqs/smart-questions.html#not_losing
> Good luck,
> -Luke
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] if n == 0 vs if not n

2009-10-05 Thread Sander Sweers

Hi Tutors,

I am going through someone's python script and I am seeing a lot of the
following boolean checks.

if not s == ""

if not n == 0

if b == True

if not b == True

etc..

All of these can be written without the == notation like "if n", "if s"
etc.

Now in this case where it is only used as boolean checks which would be
the most pythonic way if writing these checks?

Thanks
Sander

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


Re: [Tutor] Using command line tool with python script

2009-10-05 Thread Rüdiger Wolf
On Mon, 05 Oct 2009 12:59 -0400, "Kent Johnson"  wrote:
> On Mon, Oct 5, 2009 at 10:22 AM, Oleg Oltar 
> wrote:
> 
> > os.popen4("application -parameter1 -file temp.txt")
> >
> > I wonder if that possible to execute this script (called application)
> > without writing the file with initial data to the hard disk?
> 
> If "application" can take its input from stdin then you can use the
> subprocess module to launch it and pipe data to it. If "application"
> requires a file for input then I think you have to write a file.

Matt Harrison have some notes and a video of a talk about good python
scripting practices.
He describes some patterns that enable you to write python scripts that
can be chained together as they process data from one and send it to the
next.

See the following links for more info.
http://panela.blog-city.com/oscon_scripting_with_python_handout.htm
http://en.oreilly.com/oscon2009/public/schedule/detail/8317

He created a tool called poachplate so that it is easier to create these
kind of scripts.
http://pypi.python.org/pypi?%3Aaction=search&term=poachplate

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


Re: [Tutor] if n == 0 vs if not n

2009-10-05 Thread Vern Ceder

Hi Sander,

PEP 8, the "Style Guide for Python Code"
http://www.python.org/dev/peps/pep-0008/ is pretty clear that the
shorter version is preferable:

if s:

if n:

if b:

if not b:

and so on...

Cheers,
Vern

Sander Sweers wrote:

Hi Tutors,

I am going through someone's python script and I am seeing a lot of the
following boolean checks.

if not s == ""

if not n == 0

if b == True

if not b == True

etc..

All of these can be written without the == notation like "if n", "if s"
etc.

Now in this case where it is only used as boolean checks which would be
the most pythonic way if writing these checks?

Thanks
Sander

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


--
This time for sure!
   -Bullwinkle J. Moose
-
Vern Ceder, Director of Technology
Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804
vce...@canterburyschool.org; 260-436-0746; FAX: 260-436-5137

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


Re: [Tutor] Poorly understood error involving class inheritance

2009-10-05 Thread David Perlman
OK, I thought I had this one fixed but it was weirder than I thought.   
I think I understand what's going on, but I wanted to check with the  
experts here.


I have the following class definition, which does not subclass anything:

class oneStim:
def __init__(self, time, mods=[], dur=None, format='%1.2f'):
self.time=time
self.mods=mods
self.dur=dur
self.format=format

def __cmp__(self,other):
return cmp(self.time,other.time)

def __repr__(self):
timestr=self.format % self.time
if self.mods == []:
modstr=''
else:
modstr = '*' + ','.join(self.format % i for i in self.mods)
if self.dur == None:
durstr = ''
else:
durstr = ':' + (self.format % self.dur)
return timestr + modstr + durstr

def __len__(self):
return len(self.__repr__())


>>> a=oneStim(40)
>>> a
40.00
>>> a.mods.append(3)
>>> a
40.00*3.00
>>> a.dur=10
>>> a
40.00*3.00:10.00
>>> a.mods.append(1)
>>> a
40.00*3.00,1.00:10.00

So far so good, that's exactly what it's supposed to do.  But now look:

>>> b=oneStim(50)
>>> b
50.00*3.00,1.00

The mods that were added to the first instance of oneStim also appear  
in the second, newly created instance!


It appears that what is happening here is that the __init__() method  
is being parsed by the interpreter once at initial run, and at that  
time the statement "mods=[]" is being parsed, which means that the []  
object is being instantiated once there at the beginning.  So every  
instantiation of class oneStim ends up sharing a reference to the same  
list object, instead of each one having its own.


I fixed this by changing it to "mods=None" and then setting it in the  
body of the __init__ method.  Works fine now.


My question is, is this just a quirky misbehavior, or is there a  
principled reason why the code I have shown above only instantiates  
the empty list in the arguments once?


Thanks for any insight.  As I said, I got it to work fine now, so this  
isn't critical, but I'm curious to understand why things work the way  
they do.  :)


--
-dave
"Pseudo-colored pictures of a person's brain lighting up are
undoubtedly more persuasive than a pattern of squiggles produced by a
polygraph.  That could be a big problem if the goal is to get to the
truth."  -Dr. Steven Hyman, Harvard



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


Re: [Tutor] if n == 0 vs if not n

2009-10-05 Thread Sander Sweers
Thanks Wesly/Vern for the replies.

On Mon, 2009-10-05 at 21:56 +0200, Luke Paireepinart wrote: 
> if not n == 0
> 
> if b == True can be written as if b.
> 
> 
> However,
> if not n == 0 can be written as if n != 0 but NOT as if n.
> The reason why is that 0 is not equivalent to False even though it
> evaluates to False.
> So 
> if not n:
> would be true for n = 0 and for n = "" and for n = None
> but
> if n != 0:
> would be true for n = "" and n = None but not n = 0.

Ah, have not thought about this one. In this case it checks the return
code of a subprocess command which if not zero means build failure. I am
leaving these as they are because in my opinion "if not returncode == 0"
shows clearer what is going on than "if returncode".

> Whoever wrote your code probably thinks he knows the types of the
> variables beforehand so he's just making assumptions as to whether a
> variable is an int / string / etc. So you can probably safely assume
> that they're boolean checks, but I take no responsibility if you break
> something :)

When I make the changes and send in a patch it will be reviewed. So any
siliness I come up with will be shot down quickly ;-)

Thanks
Sander 

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


Re: [Tutor] New to python: some advises for image processing tool

2009-10-05 Thread Wayne
I think you forgot to hit Reply-all, so forwarding on to the list with my
response

On Mon, Oct 5, 2009 at 11:38 AM, Nicola De Quattro <
lead.express...@gmail.com> wrote:

> Wayne ha scritto:
>
>> The most difficult task would be analyzing the image and possibly some of
>> the graph generation.
>>
>
>
> Yes I has thought so. Probably it is the same for every language.
>
> Now I think I could begin with some simple script to come into the Python
> world, so I'll start building the easiest components of final tool (perhaps
> opening the image and rotate it).
>

That one should be fairly simple - my guess is it should take < 5 lines to
do something like that with PIL or ImageMagick


>
> Another question I did'nt ask to the list is about the IDE. There's
> something like this in Python or not? I've used NetBeans for C. I'm not so
> expert with IDE but I think it might be useful something like this in order
> to share the same platform on both Linux and Windows (I think to develop on
> two different computers).
>

There are /many/ IDEs available, ranging from free (IDLE, included with
python) to over a hundred dollars (the super version of Wing IDE).


> If you suggest to use no IDE, the solution I've adopted since now is to use
> gedit and python bash program under Linux (it recognizes python sintax) and
> notepad++ with... what? under Windows (I think there's something like
> "python" package under Windows).
>

My suggestion is this: Work with what you're comfortable with. If you want
to use gedit and python interpreter, go ahead.

My personal preference (on any/all systems) is vi/vim in one terminal window
for the actual coding, and IPython in another window for testing snippets of
code. I have some fairly extensive python-centric modifications to my .vimrc
and other scripts and plugins.

I'm sure you'll find many different workflows that people use and several
folks will probably offer their setups for consideration.

I hope you find something comfortable for you, and feel free to ask any
questions if you get stuck.

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


Re: [Tutor] small program

2009-10-05 Thread Kent Johnson
This language is not appropriate for this list and I'm sorry to see it
used even in a private reply. Let's keep the discussions here polite
and respectful.

Thanks,
Kent

On Sun, Oct 4, 2009 at 8:40 AM, Luke Paireepinart
 wrote:
>
>
> On Sun, Oct 4, 2009 at 1:59 PM, Andrius  wrote:
>>
>> Very good. Nice to hear that from another 'untouchable'. Must to
>> confirm for your that I'm usually left wing fella, so, if you have
>> nothing what to say for my in my case - please, f--- off.
>
> Re: http://catb.org/~esr/faqs/smart-questions.html#not_losing
> Good luck,
> -Luke
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Poorly understood error involving class inheritance

2009-10-05 Thread bob gailer




David Perlman wrote:
OK, I thought I had this one fixed but it was weirder than
I thought.  I think I understand what's going on, but I wanted to check
with the experts here.
  
  
I have the following class definition, which does not subclass
anything:
  
  
class oneStim:
  
    def __init__(self, time, mods=[], dur=None, format='%1.2f'):
  
    self.time=time
  
    self.mods=mods
  
    self.dur=dur
  
    self.format=format
  
  
    def __cmp__(self,other):
  
    return cmp(self.time,other.time)
  
  
    def __repr__(self):
  
    timestr=self.format % self.time
  
    if self.mods == []:
  
    modstr=''
  
    else:
  
    modstr = '*' + ','.join(self.format % i for i in self.mods)
  
    if self.dur == None:
  
    durstr = ''
  
    else:
  
    durstr = ':' + (self.format % self.dur)
  
    return timestr + modstr + durstr
  
  
    def __len__(self):
  
    return len(self.__repr__())
  
  
  
>>> a=oneStim(40)
  
>>> a
  
40.00
  
>>> a.mods.append(3)
  
>>> a
  
40.00*3.00
  
>>> a.dur=10
  
>>> a
  
40.00*3.00:10.00
  
>>> a.mods.append(1)
  
>>> a
  
40.00*3.00,1.00:10.00
  
  
So far so good, that's exactly what it's supposed to do.  But now look:
  
  
>>> b=oneStim(50)
  
>>> b
  
50.00*3.00,1.00
  
  
The mods that were added to the first instance of oneStim also appear
in the second, newly created instance!
  
  
It appears that what is happening here is that the __init__() method is
being parsed by the interpreter once at initial run, and at that time
the statement "mods=[]" is being parsed, which means that the [] object
is being instantiated once there at the beginning.  So every
instantiation of class oneStim ends up sharing a reference to the same
list object, instead of each one having its own.
  
  
I fixed this by changing it to "mods=None" and then setting it in the
body of the __init__ method.  Works fine now.
  
  
My question is, is this just a quirky misbehavior, or is there a
principled reason why the code I have shown above only instantiates the
empty list in the arguments once?
  


RTFM: (Reference Manual, 7.6 Function definitions)

Default parameter values are evaluated when the function
definition is executed. This means that the _expression_ is
evaluated once, when the function is defined, and that that same
``pre-computed'' value is used for each call. This is especially
important to understand when a default parameter is a mutable object,
such as a list or a dictionary: if the function modifies the object
(e.g. by appending an item to a list), the default value is in effect
modified. This is generally not what was intended. A way around this is
to use None as the default, and explicitly test for it in
the body of the function, e.g.:


def whats_on_the_telly(penguin=None):
if penguin is None:
penguin = []
penguin.append("property of the zoo")
return penguin



Thanks for any insight.  As I said, I got it to work fine now, so this
isn't critical, but I'm curious to understand why things work the way
they do.  :)
  
  
--
  
-dave
  
"Pseudo-colored pictures of a person's brain lighting up are
  
undoubtedly more persuasive than a pattern of squiggles produced by a
  
polygraph.  That could be a big problem if the goal is to get to the
  
truth."  -Dr. Steven Hyman, Harvard
  
  
  
  
___
  
Tutor maillist  -  Tutor@python.org
  
To unsubscribe or change subscription options:
  
http://mail.python.org/mailman/listinfo/tutor
  
  



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


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


[Tutor] Which version to start with?

2009-10-05 Thread Nick Hird
What is the best version of python to start out with? I see some
discussions on the net about not going to 3.1 but staying with the 2.x
releases. But then i see that 3.1 is better if your just starting.
Thanks for any insight on which version to go with.
-Nick
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Poorly understood error involving class inheritance

2009-10-05 Thread Wayne
On Mon, Oct 5, 2009 at 3:41 PM, David Perlman  wrote:

> OK, I thought I had this one fixed but it was weirder than I thought.  I
> think I understand what's going on, but I wanted to check with the experts
> here.
>
> I have the following class definition, which does not subclass anything:
>
> class oneStim:
>def __init__(self, time, mods=[], dur=None, format='%1.2f'):
>self.time=time
>self.mods=mods
>

I think this is where the fun occurs (AFAIK, I'm not an expert on how the
python lists/namespaces work by any means)

You are familiar with the following concept/behavior, correct?

def f1(mylist):
mylist.append(3)

a = [1,2]
>>> f1(a)
>>> a
[1, 2, 3]

because python doesn't actually pass a copy, just the reference of the list.
So I'm betting that when you say

self.mods = mods

the second time you call the function you're creating a reference to the
self.mods that's in the oneStim object namespace.

Ahhh... After some experimentation, I think I see what's going on.

class A:
def __init__(self, mylist = []):
self.mylist = mylist

a = A()
b = A()

In [69]: a.__init__.im_func
Out[69]: 

In [70]: A.__init__.im_func
Out[70]: 

In [71]: b.__init__.im_func
Out[71]: 

The function is at the same address for the parent class and each instance
of the class, so I think the reference it creates is to the list in the same
location as well.
Ah... some enlightenment has struck with what bob posted.

The "initial value" of that function is the /exact same/ each time the
function is called. When you pass a parameter to the function it will be
re-evaluated, but until then the pre-stored reference is used.

I'm not sure if that's terribly clear or makes much sense, it's a bit of a
difficult concept to wrap my head around, but I *think* I've got it.

Hopefully you can get it, too,
Wayne
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Which version to start with?

2009-10-05 Thread Rich Lovely
2009/10/5 Nick Hird :
> What is the best version of python to start out with? I see some
> discussions on the net about not going to 3.1 but staying with the 2.x
> releases. But then i see that 3.1 is better if your just starting.
> Thanks for any insight on which version to go with.
> -Nick
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
I've got to say I think version 2.6 is the one to go with.  Yes, 3.1
has been out for a while now, so support for it is getting better, but
2.6 code is very much the same as 2.5, so support has been around for
much longer, and therefore will be much more stable.  The same applies
to tutorials:  As code has changed very little since 2.0, there are
likely to be less errors, and a much larger archive of questions asked
previously - most of the posts to this list, archived at
http://aspn.activestate.com/ASPN/Mail/Browse/Threaded/python-Tutor
(among a few other places), are about 2.x code.

-- 
Rich "Roadie Rich" Lovely

There are 10 types of people in the world: those who know binary,
those who do not, and those who are off by one.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Which version to start with?

2009-10-05 Thread Wayne
On Mon, Oct 5, 2009 at 4:24 PM, Nick Hird  wrote:

> What is the best version of python to start out with? I see some
> discussions on the net about not going to 3.1 but staying with the 2.x
> releases. But then i see that 3.1 is better if your just starting.
> Thanks for any insight on which version to go with.
> -Nick
>

It used to be that few tutorials were updated to 3.x, but I know at least
Alan's is finished(right?).

The big issue with going to 3.1 is that most of the modules haven't been
ported yet, so if you want to use any cool 3rd party modules you may have
issues.

For beginning there's probably nothing wrong with 3.1, but I think I'd
recommend sticking with 2.6.

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


Re: [Tutor] Poorly understood error involving class inheritance

2009-10-05 Thread wesley chun
>    def __init__(self, time, mods=[], dur=None, format='%1.2f'):
>        :
> The mods that were added to the first instance of oneStim also appear in the
> second, newly created instance!
>
> It appears that what is happening here is that the __init__() method is
> being parsed by the interpreter once at initial run, and at that time the
> statement "mods=[]" is being parsed, which means that the [] object is being
> instantiated once there at the beginning.  So every instantiation of class
> oneStim ends up sharing a reference to the same list object, instead of each
> one having its own.
>
> I fixed this by changing it to "mods=None" and then setting it in the body
> of the __init__ method.  Works fine now.
>
> My question is, is this just a quirky misbehavior, or is there a principled
> reason why the code I have shown above only instantiates the empty list in
> the arguments once?


good eyes david,

you pretty much nailed it. what you describe is *exactly* what's going
on. you've run into one of the stumbling blocks that catch new and
seasoned Python programmers all the time: using a mutable object as a
default value... these objects are assigned early, hence the reason
why Python behaves the way it does. IOW, they are *not* assigned on a
per-call basis. (this is actually a popular interview question.) :-)

there is plenty of documentation online about this phenomenon, as it's
been with Python since the early days, or one or more of the other
tutors can post the appropriate links.

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
   http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-05 Thread wesley chun
> I am going through someone's python script and I am seeing a lot of the
> following boolean checks.
>
> if not s == ""
> if not n == 0
> if b == True
> if not b == True
> etc..
>
> All of these can be written without the == notation like "if n", "if s"
> etc.Now in this case where it is only used as boolean checks which would be
> the most pythonic way if writing these checks?


it would be the same as what you have already described. checking
against Boolean literals follows the same logic, i.e., "if b", "if not
b", etc. of course, the reasoning behind what you said and my
suggestion is that all Python objects evaluate to some sort of Boolean
value. the "== 0" and '== ""' (and their corresponding "not"s) aren't
necessary because both 0 and "" have a Boolean False value, as does
False.

the general rule is that any numeric zero (0, 0.0, 0.0+0.0J, etc.) or
empty "container" (i.e., str, list, tuple, dict, set, etc.), are all
False. all other values are True.

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Which version to start with?

2009-10-05 Thread wesley chun
On Mon, Oct 5, 2009 at 2:24 PM, Nick Hird  wrote:
> What is the best version of python to start out with? I see some
> discussions on the net about not going to 3.1 but staying with the 2.x
> releases. But then i see that 3.1 is better if your just starting.


greetings nick!

ironically, i just gave a talk on this very subject yesterday afternoon(!)
http://www.siliconvalley-codecamp.com/Sessions.aspx?OnlyOne=true&id=227

basically, if you're starting from scratch as a hobby with no
pre-existing code, then learning 3.x is okay. however, since most of
the world still runs on Python 2, most printed and online books and
tutorials are still on Python 2, and the code at most companies using
Python is still on version 2, i would recommended any release 2.6 (and
newer). the reason is because 2.6 is the first release that has
3.x-specific features backported to it, so really, it's the first
Python 2 release that lets you start coding against a 3.x interpreter.

you can learn Python using 2.6+ then absorb the differences and move
to Python 3.x quite easily.

hope this helps!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
http://corepython.com

wesley.j.chun :: wescpy-at-gmail.com
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Poorly understood error involving class inheritance

2009-10-05 Thread Kent Johnson
On Mon, Oct 5, 2009 at 4:41 PM, David Perlman  wrote:

> I fixed this by changing it to "mods=None" and then setting it in the body
> of the __init__ method.  Works fine now.

That is the correct fix.

> My question is, is this just a quirky misbehavior, or is there a principled
> reason why the code I have shown above only instantiates the empty list in
> the arguments once?

This is a FA. Default arguments are shared between all uses:
http://effbot.org/pyfaq/why-are-default-values-shared-between-objects.htm
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Which version to start with?

2009-10-05 Thread Nick Hird
Thanks all! I think i will install the newly released 2.6.3 and go
from there. Its a little intimidating but i guess i gotta jump right
in and get my feet wet.

Thanks again!
-Nick

On Mon, Oct 5, 2009 at 5:59 PM, wesley chun  wrote:
> On Mon, Oct 5, 2009 at 2:24 PM, Nick Hird  wrote:
>> What is the best version of python to start out with? I see some
>> discussions on the net about not going to 3.1 but staying with the 2.x
>> releases. But then i see that 3.1 is better if your just starting.
>
>
> greetings nick!
>
> ironically, i just gave a talk on this very subject yesterday afternoon(!)
> http://www.siliconvalley-codecamp.com/Sessions.aspx?OnlyOne=true&id=227
>
> basically, if you're starting from scratch as a hobby with no
> pre-existing code, then learning 3.x is okay. however, since most of
> the world still runs on Python 2, most printed and online books and
> tutorials are still on Python 2, and the code at most companies using
> Python is still on version 2, i would recommended any release 2.6 (and
> newer). the reason is because 2.6 is the first release that has
> 3.x-specific features backported to it, so really, it's the first
> Python 2 release that lets you start coding against a 3.x interpreter.
>
> you can learn Python using 2.6+ then absorb the differences and move
> to Python 3.x quite easily.
>
> hope this helps!
> -- wesley
> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> "Core Python Programming", Prentice Hall, (c)2007,2001
> "Python Fundamentals", Prentice Hall, (c)2009
>    http://corepython.com
>
> wesley.j.chun :: wescpy-at-gmail.com
> python training and technical consulting
> cyberweb.consulting : silicon valley, ca
> http://cyberwebconsulting.com
>



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


Re: [Tutor] if n == 0 vs if not n

2009-10-05 Thread Dave Angel

Vern Ceder wrote:

Hi Sander,

PEP 8, the "Style Guide for Python Code"
http://www.python.org/dev/peps/pep-0008/ is pretty clear that the
shorter version is preferable:

if s:

if n:

if b:

if not b:

and so on...

Cheers,
Vern

Sander Sweers wrote:

Hi Tutors,

I am going through someone's python script and I am seeing a lot of the
following boolean checks.

if not s == ""

if not n == 0

if b == True

if not b == True

etc..

All of these can be written without the == notation like "if n", "if s"
etc.

Now in this case where it is only used as boolean checks which would be
the most pythonic way if writing these checks?

Thanks
Sander

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


The shorter version may be preferable, but it doesn't generally give the 
same results.  Without knowing the possible data, these substitutions 
are not safe.


For example, replacing   "if not n == 0"with "if n"

will give different results for values of "", []   and so on. It 
WILL work if you know that n is an int or float, however.


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


Re: [Tutor] if n == 0 vs if not n

2009-10-05 Thread Vern Ceder

Dave Angel wrote:


Now in this case where it is only used as boolean checks which would be
the most pythonic way if writing these checks?



The shorter version may be preferable, but it doesn't generally give the 
same results.  Without knowing the possible data, these substitutions 
are not safe.


For example, replacing   "if not n == 0"with "if n"

will give different results for values of "", []   and so on. It 
WILL work if you know that n is an int or float, however.


DaveA


True, I took the OP's statement that they were to be used "only as 
boolean checks" to mean that there was no type mixing going on. 
Personally, I would say that checking a list or string for equality (or 
lack thereof) with 0 is even less "preferable". ;)


Otherwise, one would at least prefer "if n != 0" to "if not n == 0", I 
would think.


Cheers, Vern
--
This time for sure!
   -Bullwinkle J. Moose
-
Vern Ceder, Director of Technology
Canterbury School, 3210 Smith Road, Ft Wayne, IN 46804
vce...@canterburyschool.org; 260-436-0746; FAX: 260-436-5137
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] if n == 0 vs if not n

2009-10-05 Thread Luke Paireepinart
On Mon, Oct 5, 2009 at 9:28 PM, Sander Sweers wrote:

>
> Hi Tutors,
>
> I am going through someone's python script and I am seeing a lot of the
> following boolean checks.
>
> if not s == ""
>
> if not n == 0
>
> if b == True
>
> if not b == True
>
> etc..
>
> All of these can be written without the == notation like "if n", "if s"
>

No, they cannot.  Some of them can be, others cannot.
if b == True can be written as if b.

However,
if not n == 0 can be written as if n != 0 but NOT as if n.
The reason why is that 0 is not equivalent to False even though it evaluates
to False.
So
if not n:
would be true for n = 0 and for n = "" and for n = None
but
if n != 0:
would be true for n = "" and n = None but not n = 0.

The same is true for
if not s == ""

> Now in this case where it is only used as boolean checks which would be
> the most pythonic way if writing these checks?
>
If you're sure they're boolean checks, "if n:" or "if not n:" is usually how
I see it written.
Whoever wrote your code probably thinks he knows the types of the variables
beforehand so he's just making assumptions as to whether a variable is an
int / string / etc. So you can probably safely assume that they're boolean
checks, but I take no responsibility if you break something :)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor