Isr Gish | 1 Feb 2004 03:14

Re: [Tutor] Converting string or list to sum


Kalle Svensson" wrote:
   >>    >I'd write
   >>    >
   >>    >  d = ''.join(map(str, a))
   >>    >
   >>    >since that's probably much faster for large lists.
   >>  
   >> I think that should be:
   >> import __builtin__
   >> d = ''.join(map(__builtin__.str, a))
   >
   >Why?
   >

When I try doing.
map(str, a)
I got a traceback 
TypeError: 'str' object is not callable
But when I do,
map(__builtin__.str, a)
It works fine.

I'm using ver. 2.3.2 on a Pocket PC.

Isr

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor
(Continue reading)

moore william | 1 Feb 2004 03:35
Picon
Favicon

Buttons

I have a cash program that I am trying to create
buttons for instead of a simple number menu. I
understand that I need to import the Tkinter, create
and position the widgets, and enter the main loop
(Python in a nut shell). But I am still having
problems. I have attached the code I have to this
point. 

import shelve

s = shelve.open('d:/College/numberdata')

if s.has_key('numbers'):
    numbers = s['numbers']
else:
    numbers = {}

class AccountTransaction(object):
    def __init__(self, amount, time):
        self.amount = amount
        self.time = time

    def __repr__(self):
        description = time.ctime(self.time) + ' '
        if self.amount < 0:
            description += '-'
            description += '$' + str(abs(self.amount))
            return description
    def __init__(self, initial):
         self.balance = amount
(Continue reading)

Terry Carroll | 1 Feb 2004 03:47

Re: Re: [Tutor] Converting string or list to sum

On Sat, 31 Jan 2004, Isr Gish wrote:

> When I try doing.
> map(str, a)
> I got a traceback 
> TypeError: 'str' object is not callable
> But when I do,
> map(__builtin__.str, a)
> It works fine.

did you rebind the variable name str elsewhere in your program, e.g.,

>>> a=['1','2','3']
>>> a
['1', '2', '3']
>>> d=''.join(map(str,a))
>>> d
'123'
>>> str="xyz"
>>> d=''.join(map(str,a))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'str' object is not callable
>>>

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor

(Continue reading)

Danny Yoo | 1 Feb 2004 06:03
Picon

Re: "=" invalid syntax ?


On Sat, 31 Jan 2004, Tim Johnson wrote:

> >  File "./test.py", line 8
> >    while (line=fi.readline()):
> >               ^
> > SyntaxError: invalid syntax
> >
> > what's the problem?
> > while (line=fi.readline()):
>
>     '=' is an assignment operator in python.
>     '==' is the comparison operator in python.
>     use '=='
>
>     happens to me all the time ....

Hi Tim,

Changing '=' to '==' will make it syntactically correct, but it won't
preserve the intended meaning.

Xuer appears to be using a C-ish syntax:  in the C language, it's legal to
say something like:

    while((ch = getc(stdin)) != EOF) { ... }

In the C language, assignment is treated as an "expression" that can be
nested within other expressions, so the above C code mixes together an
assigment with a comparison.  Very succinct, but also very error prone.
(Continue reading)

Danny Yoo | 1 Feb 2004 06:19
Picon

Re: "=" invalid syntax ? [Assignment grammar stuff]


On Sat, 31 Jan 2004 orbitz <at> ezabel.com wrote:

> I just learned this recently, but if you say = is a statement then you
> would imagine:
>
> a = b = 2 to not work. = is actually special cased for this to work.
> however
> a = (b = 2) does not work.  There is some 'magic' some people might say
> to =, but I think a = b = .. is the only excpetion to the = is a
> statement operator rule.

Hi orbitz,

If people are interested in how it's special cased, the Reference Manual
has the gory details.  *grin*


The grammar definition in:

    http://www.python.org/doc/ref/assignment.html

shows that

    assignment_stmt ::= (target_list "=")+   expression_list

That is, an assignment statement is made up of a number of targets,
followed by the expression that will be assigned to those targets.
Usually, we define a single target, but nothing stops us from defining
multiple targets.  If we're looking at somthing like:
(Continue reading)

Isr Gish | 1 Feb 2004 07:03

Re: Re: Converting string or list to sum

Thanks I had rebound the variable str.

Isr

-----Original Message-----
   >From: "Terry Carroll"<carroll <at> tjc.com>
   >Sent: 1/31/04 9:47:23 PM
   >To: "tutor <at> python.org"<tutor <at> python.org>
   >Subject: Re: Re: [Tutor] Converting string or list to sum
   >
   >On Sat, 31 Jan 2004, Isr Gish wrote:
   >
   >> When I try doing.
   >> map(str, a)
   >> I got a traceback 
   >> TypeError: 'str' object is not callable
   >> But when I do,
   >> map(__builtin__.str, a)
   >> It works fine.
   >
   >did you rebind the variable name str elsewhere in your program, e.g.,
   >
   >>>> a=['1','2','3']
   >>>> a
   >['1', '2', '3']
   >>>> d=''.join(map(str,a))
   >>>> d
   >'123'
   >>>> str="xyz"
   >>>> d=''.join(map(str,a))
(Continue reading)

Andrei | 1 Feb 2004 13:10

Re: .tar.gz in Py

Nick Lunt wrote on Sat, 31 Jan 2004 22:21:26 +0000:

Thanks, Nick. That's a lot easier than I expected. The possible errors
aren't a problem for me because I am pretty certain that my dirs and their
contents are "safe" since the script where I'll use it creates those dirs
and files itself.

> I asked a tar question myself yesterday and included my problematic program. Here it is -
> 
> # CODE
> 
> """
> tar.py:
> Usage: 
>         tar.py filetocreate.tar list_of_files_to_tar
> 
> imports:
>         tarfile -        to do the tarring
>         sys -            to manipulate command line args        
> """
> 
> # Fails in the if block on os.access when dirs are submitted as args.
> # ie the dir submitted is readable but some files below it may not be,
> # which the if os.access() does not pick up.
> # So create a new function to walk the dir and only return the files it can
> # backup successfully, but print a list of the ones it cant.
> 
> 
<snip>
> As you can see from the comments in my code it is not perfect yet, but it will hopefully show you how to use the
(Continue reading)

moore william | 1 Feb 2004 22:11
Picon
Favicon

Activating a box

I am adding boxes to my program and am looking for
informatioin to get my box to activate diffrent parts
of my program. Anyone have a toutorial on this?

Example:

(BOX)
# File: toolbar1.py

from Tkinter import *

root = Tk()

def Deposite():
    print "Amount:"

# create a toolbar
toolbar = Frame(root)

b = Button(toolbar, text="Deposite", width=10,
command=Deposite)
b.pack(side=LEFT, padx=2, pady=2)

b = Button(toolbar, text="Withdraw", width=10,
command=Deposite)
b.pack(side=LEFT, padx=2, pady=2)

toolbar.pack(side=TOP, fill=X)

mainloop()
(Continue reading)

Alan Gauld | 2 Feb 2004 00:05
Picon
Favicon

Re: Buttons

> I have a cash program that I am trying to create
> buttons for instead of a simple number menu. I
> understand that I need to import the Tkinter, create
> and position the widgets, and enter the main loop

Yes, but of course you aren't doing any of that here...

> (Python in a nut shell). But I am still having
> problems. I have attached the code 

It would help if you gave us a clue as to what the 
problems were? They obviously are not related to the 
Buttons since there is no Tkinter code here.

I'll try to add some comments....
> class AccountTransaction(object):
>     def __init__(self, amount, time):
>         self.amount = amount
>         self.time = time

Not sure why you are using a new style class here, 
especially given that you use old stuyle classes later on.
But it shouldn't cause any real problems.

>     def __repr__(self):
>         description = time.ctime(self.time) + ' '
>         if self.amount < 0:
>             description += '-'
>             description += '$' + str(abs(self.amount))
>             return description
(Continue reading)

Harm_Kirchhoff | 2 Feb 2004 01:54
Picon

How to close a file opened with csv.write

Thanks Gerrit, that was a good tip. I had been wondering about this for 
some time and I know others, too. 

Is there a way this could be included into the python docu for the csv 
module ?

_______________________________________________
Tutor maillist  -  Tutor <at> python.org
http://mail.python.org/mailman/listinfo/tutor


Gmane