Re: metaclasses (beginner question)

2007-02-21 Thread Peter Otten
Laszlo Nagy wrote: I would like to create a hierarchy classes, where the leaves have a special attribute called producer_id. In addition, I would like to have a function that can give me back the class assigned to any producer_id value. I tried to implement this with a metaclass, but I

Re: metaclasses (beginner question)

2007-02-21 Thread Laszlo Nagy
Without looking into the details -- the (subclass of) type is meant to be the class of the class, or the other way round, your normal classes are instances of (a subclass of) type. You determine the factory Python uses to make a class by adding __metaclass__ = factory to the class body,

Beginner question on text processing

2006-12-29 Thread Doran, Harold
I am beginning to use python primarily to organize data into formats needed for input into some statistical packages. I do not have much programming experience outside of LaTeX and R, so some of this is a bit new. I am attempting to write a program that reads in a text file that contains some

Re: Beginner question on text processing

2006-12-29 Thread skip
Harold To illustrate, assume I have a text file, call it test.txt, with Harold the following information: Harold X11 .32 Harold X22 .45 Harold My goal in the python program is to manipulate this file such Harold that a new file would be created that looks like:

beginner question about range with list and list

2006-11-23 Thread erik gartz
Hello, I'm new to python and I'm having difficulty understanding the following code. Why doesn't the variable a contain [[{}, {'x': 0}, {}], [{}, {'x': 1}, {}]] instead. Doesn't {} allocate new memory for the dictionary each time? It almost appears as if the 2nd dictionary created overwrites the

Re: beginner question about range with list and list

2006-11-23 Thread Ben Finney
erik gartz [EMAIL PROTECTED] writes: Doesn't {} allocate new memory for the dictionary each time? It almost appears as if the 2nd dictionary created overwrites the first one. URL:http://effbot.org/pyfaq/how-do-i-create-a-multidimensional-list.htm -- \There are only two ways to

Re: beginner question about range with list and list

2006-11-23 Thread Fredrik Lundh
erik gartz wrote: I'm new to python and I'm having difficulty understanding the following code. Why doesn't the variable a contain [[{}, {'x': 0}, {}], [{}, {'x': 1}, {}]] instead. Doesn't {} allocate new memory for the dictionary each time? each time it's *executed*, yes. [{}]*3 doesn't

Beginner question? Classes, variables, ...

2006-06-28 Thread Ángel Gutiérrez Rodríguez
The problem: I have two classes: class X: def __init__(self): pass class Y: def __init__(self): self.a=1 self.b=X() and I would like to make 'a' visible inside 'x'. Is there a way to refer to the Y class from the X? To make things easier :), each class is in a different file,

Re: Beginner question? Classes, variables, ...

2006-06-28 Thread Diez B. Roggisch
Ángel Gutiérrez Rodríguez wrote: The problem: I have two classes: class X: def __init__(self): pass class Y: def __init__(self): self.a=1 self.b=X() and I would like to make 'a' visible inside 'x'. Is there a way to refer to the Y class from the X? To make

Re: Beginner question? Classes, variables, ...

2006-06-28 Thread Diez B. Roggisch
Dennis Lee Bieber wrote: On Wed, 28 Jun 2006 10:35:10 +0200, Diez B. Roggisch [EMAIL PROTECTED] declaimed the following in comp.lang.python: class X: def __init__(self, my_y): self.my_y self.my_y = my_y *argl* Thanks :) No tea so far... Diez --

Re: Beginner question? Classes, variables, ...

2006-06-28 Thread Ángel Gutiérrez Rodríguez
That wa sneat! Thanks! -- Ángel Gutiérrez Rodríguez - [EMAIL PROTECTED] Instituto de Ciencia de los Materiales de Madrid - CSIC SpLine - European Syncrothorn Radiation Facility - Grenoble - France Postal adress: Departamento de Química Física y Analítica Universidad de Oviedo - c/Julián

Beginner question: use function to read text file

2006-06-26 Thread Luke
I'm pretty stuck at the moment and wondering if anyone can spot the problem. Trying to create a function that will read a text file into a list and return that list. I wrote the following function and saved it as 'fileloader.py' def fileload(fname): infile=open(fname) dates =[]

Re: Beginner question: use function to read text file

2006-06-26 Thread James Stroud
Luke wrote: I'm pretty stuck at the moment and wondering if anyone can spot the problem. Trying to create a function that will read a text file into a list and return that list. I wrote the following function and saved it as 'fileloader.py' def fileload(fname): infile=open(fname)

Re: Beginner question: use function to read text file

2006-06-26 Thread Gary Herron
Luke wrote: I'm pretty stuck at the moment and wondering if anyone can spot the problem. Trying to create a function that will read a text file into a list and return that list. I wrote the following function and saved it as 'fileloader.py' def fileload(fname): infile=open(fname)

Re: Beginner question: use function to read text file

2006-06-26 Thread Luke
Thanks to both of you for the help, much appreciated! Luke -- http://mail.python.org/mailman/listinfo/python-list

Beginner question. Exceptions

2006-04-12 Thread Jose Carlos Balderas Alberico
Hello. I'm having my way with exceptions in a little program I've done, and I'm wondering... What is a decent way of dealing with them? By a decent way I mean, what should I do when I see that an exception has occurred? I've seen that Python's default action is to print a stack trace. As far as I

beginner question on dinamin buiding of arg list

2006-03-05 Thread Sandro Dentella
I need to build-up an arg list to pass to a function. Suppose I have a dictionary: opts = { 'user' : 'jack', 'addr' : 'Green Str.'} and I want to build a cmd line like this: select( user='jack', addr='Green Str.' ) I'm clueless... TIA sandro *:-) -- Sandro Dentella *:-)

Re: beginner question on dinamin buiding of arg list

2006-03-05 Thread Alex Martelli
Sandro Dentella [EMAIL PROTECTED] wrote: I need to build-up an arg list to pass to a function. Suppose I have a dictionary: opts = { 'user' : 'jack', 'addr' : 'Green Str.'} and I want to build a cmd line like this: select( user='jack', addr='Green Str.' ) select(**opts) should

beginner question fibonacci

2005-07-17 Thread Joon
# Fibonacci series: ... # the sum of two elements defines the next ... a, b = 0, 1 while b 10: ... print b ... a, b = b, a+b ... 1 1 2 3 5 8 a, b = 0, 1 while b 10: print b a = b b = a+b 1 2 4 8 Why a, b = b, a+b isn't a = b; b = a+b ?

Re: beginner question fibonacci

2005-07-17 Thread Robert Kern
Joon wrote: # Fibonacci series: ... # the sum of two elements defines the next ... a, b = 0, 1 while b 10: ... print b ... a, b = b, a+b ... 1 1 2 3 5 8 a, b = 0, 1 while b 10: print b a = b b = a+b 1 2 4 8 Why a, b = b,

Re: beginner question fibonacci

2005-07-17 Thread Michael Hoffman
Joon wrote: a, b = 0, 1 while b 10: print b a = b b = a+b 1 2 4 8 Why a, b = b, a+b isn't a = b; b = a+b ? Because you changed a before you added it to b. Let's call your existing a and b a0 and b0, and the next a and b a1 and b1. When you do a, b = b, a+b

Re: beginner question fibonacci

2005-07-17 Thread ralobao
The case is that Python in attribution commands solves first the right side, so he atributes the vars. So the a+b expression is executed first. Joon escreveu: # Fibonacci series: ... # the sum of two elements defines the next ... a, b = 0, 1 while b 10: ... print b ... a, b

Re: beginner question fibonacci

2005-07-17 Thread Joon
Yes, i see. Thank you very much for the fast help! -- http://mail.python.org/mailman/listinfo/python-list

Re: Beginner question: Converting Single-Element tuples to list

2005-06-29 Thread Paul McGuire
Steve - Good catch - in v1.3, I added some Unicode support for pyparsing, although I have not gotten much feedback that anyone is using it, or how well it works. So it is preferable to test against basestring instead of str. -- Paul -- http://mail.python.org/mailman/listinfo/python-list

Re: Beginner question: Converting Single-Element tuples to list

2005-06-27 Thread Reinhold Birkenfeld
[EMAIL PROTECTED] wrote: Hi, Thanks for your reply! A new thing learned Allow me to follow that up with another question: Let's say I have a result from a module called pyparsing: Results1 = ['abc', 'def'] Results2 = ['abc'] They are of the ParseResults type: type(Results1)

Re: Beginner question: Converting Single-Element tuples to list

2005-06-27 Thread John Roth
Reinhold Birkenfeld [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] [EMAIL PROTECTED] wrote: Hi, Thanks for your reply! A new thing learned Allow me to follow that up with another question: Let's say I have a result from a module called pyparsing: Results1 = ['abc', 'def']

Re: Beginner question: Converting Single-Element tuples to list

2005-06-27 Thread Paul McGuire
David - I'm not getting the same results. Run this test program: --- import pyparsing as pp import sys def test(s): results = pp.OneOrMore( pp.Word(pp.alphas) ).parseString( s ) print repr(s),-,list(results) print Python version:, sys.version print pyparsing version:,

Re: Beginner question: Converting Single-Element tuples to list

2005-06-27 Thread Paul McGuire
John - I just modified my test program BNF to use ZeroOrMore instead of OneOrMore, and parsed an empty string. Calling list() on the returned results gives an empty list. What version of pyparsing are you seeing this None/object/list behavior? -- Paul --

Re: Beginner question: Converting Single-Element tuples to list

2005-06-27 Thread Jeff Epler
On Mon, Jun 27, 2005 at 08:21:41AM -0600, John Roth wrote: Unfortunately, I've seen that behavior a number of times: no output is None, one output is the object, more than one is a list of objects. That forces you to have checks for None and list types all over the place. maybe you can at

Re: Beginner question: Converting Single-Element tuples to list

2005-06-27 Thread Paul McGuire
Modified version of test program, to handle empty strings - empty lists. import pyparsing as pp import sys def test(s): results = pp.ZeroOrMore( pp.Word(pp.alphas) ).parseString( s ) print repr(s),-,list(results) print Python version:, sys.version print pyparsing version:,

Re: Beginner question: Converting Single-Element tuples to list

2005-06-27 Thread vdavidster
Hi Paul and everyone else, I ran the script and here's what I got: Python version: 2.4.1 (#2, Mar 31 2005, 00:05:10) [GCC 3.3 20030304 (Apple Computer, Inc. build 1666)] pyparsing version: 1.3 'abc def' - ['abc', 'def'] 'abc' - ['abc'] It seems to work fine. I figured out what my problem was:

Re: Beginner question: Converting Single-Element tuples to list

2005-06-27 Thread Steven Bethard
[EMAIL PROTECTED] wrote: if type(input) == str: You might consider writing this as: if isinstance(input, basestring): I don't know if pyparsing ever produces unicode objects, but in case it does (or it starts to in the future), isinstance is a better call here. STeVe --

Beginner question: Converting Single-Element tuples to list

2005-06-26 Thread vdavidster
Hello everyone, I want to convert a tuple to a list, and I expected this behavior: list(('abc','def')) - ['abc','def'] list(('abc')) - ['abc'] But Python gave me this behavior: list(('abc','def')) - ['abc','def'] list(('abc')) - ['a','b','c'] How do I do get Python to work like the in former

Re: Beginner question: Converting Single-Element tuples to list

2005-06-26 Thread Paul McGuire
('abc') is not a tuple - this is an unfortunate result of using ()'s as expression grouping *and* as tuple delimiters. To make ('abc') a tuple, you must add an extra comma, as ('abc',). list( ('abc',) ) ['abc'] -- Paul -- http://mail.python.org/mailman/listinfo/python-list

Re: Beginner question: Converting Single-Element tuples to list

2005-06-26 Thread vdavidster
Hi, Thanks for your reply! A new thing learned Allow me to follow that up with another question: Let's say I have a result from a module called pyparsing: Results1 = ['abc', 'def'] Results2 = ['abc'] They are of the ParseResults type: type(Results1) class 'pyparsing.ParseResults'

Re: Beginner question: Logs?

2005-06-03 Thread Peter Hansen
Robert Kern wrote: Greg Ewing wrote: [about the from xxx import * syntax] Better still, don't even *mention* it to a beginner. They don't need to know about it. At all. Really. Well, the OP's use is precisely why from xxx import * exists: the interactive prompt. In that case (and, really,

Re: Beginner question: Logs?

2005-06-02 Thread Peter Hansen
Elliot Temple wrote: from math import * log10(15625) It's always a good idea, especially when answering a beginner's question, to add the caution that this form (from xxx import *) has certain dangers** associated with it, and is widely considered poor style, and should really only rarely be

Re: Beginner question: Logs?

2005-06-02 Thread Terry Reedy
import math math.log10(15625) To find out the names of function in the math module without checking the docs, do dir(math) #same for any other module To get more info, do help(math) # same for any other module with a doc string Terry J. Reedy --

Re: Beginner question: Logs?

2005-06-02 Thread Terry Reedy
Peter Hansen [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Elliot Temple wrote: from math import * log10(15625) It's always a good idea, especially when answering a beginner's question, to add the caution that this form (from xxx import *) has certain dangers** associated with

Re: Beginner question: Logs?

2005-06-02 Thread Greg Ewing
Peter Hansen wrote: It's always a good idea, especially when answering a beginner's question, to add the caution that this form (from xxx import *) has certain dangers** associated with it, and is widely considered poor style, and should really only rarely be used. Better still, don't

Re: Beginner question: Logs?

2005-06-02 Thread Robert Kern
Greg Ewing wrote: Peter Hansen wrote: It's always a good idea, especially when answering a beginner's question, to add the caution that this form (from xxx import *) has certain dangers** associated with it, and is widely considered poor style, and should really only rarely be used.

Beginner question: Logs?

2005-06-01 Thread Svens
Hey everyone! I'm a math student working on a short script involving logs. I have a function on my scientific calculator, and was wondering if there was a similar funtion in python. For example: (log65536)/(log4)= 8 I've searched around a bit and haven't been able to find anything. Thanks!

Re: Beginner question: Logs?

2005-06-01 Thread Robert Kern
Svens wrote: Hey everyone! I'm a math student working on a short script involving logs. I have a function on my scientific calculator, and was wondering if there was a similar funtion in python. For example: (log65536)/(log4)= 8 I've searched around a bit and haven't been able to find

Re: Beginner question: Logs?

2005-06-01 Thread Svens
Hey thanks... Still getting an error message though. Here's what i'm doing: -- import math log10(15625) -- -It says that log10 is not defined, but it is since the module is imported, right? -- http://mail.python.org/mailman/listinfo/python-list

Re: Beginner question: Logs?

2005-06-01 Thread Robert Kern
Svens wrote: Hey thanks... Still getting an error message though. Here's what i'm doing: -- import math log10(15625) -- -It says that log10 is not defined, but it is since the module is imported, right? No, read the tutorial. import math math.log10(15625) -- Robert Kern

Re: Beginner question: Logs?

2005-06-01 Thread Stephen Prinster
Svens wrote: Hey thanks... Still getting an error message though. Here's what i'm doing: -- import math log10(15625) -- -It says that log10 is not defined, but it is since the module is imported, right? try this: import math math.log10(15625) --

Re: Beginner question: Logs?

2005-06-01 Thread Elliot Temple
On Jun 1, 2005, at 9:04 PM, Svens wrote: Hey thanks... Still getting an error message though. Here's what i'm doing: -- import math log10(15625) -- -It says that log10 is not defined, but it is since the module is imported, right? do either import math math.log10(15625)

Beginner question: Python types

2005-05-31 Thread Uppal, Deepali
Hello, I am facing a bit of a problem due to python implicitly attaching a type to an object. I will briefly tell you the problem that I am facing. I am trying to print the docstring of a test case in my pyUnit base test class. I accept the name of the test class as a command

Re: Beginner question: Python types

2005-05-31 Thread Paul McNett
Uppal, Deepali wrote: Hello, Hello, and welcome to the world of Python. Don't take anything we say too personally, it is meant to help. I am facing a bit of a problem due to python implicitly attaching a type to an object. Ooh. In general, don't say 'implicit'. For the most part, Python

Re: sort of a beginner question about globals

2005-04-16 Thread fred.dixon
just read the ne stuuf. i went to DIP right after work anf found in nice intro to unittest. not to bad (i thinK) now that I can see it work) -- http://mail.python.org/mailman/listinfo/python-list

Re: sort of a beginner question about globals

2005-04-15 Thread Thomas Heller
fred.dixon [EMAIL PROTECTED] writes: :) unit test is something on my to-learn list. seems involved and i haven't seen any straight forward tutorials yet. as yet i still consider myself a hobbyist at best. I would recommend Test Driven Development by Kent Beck (Addison-Wesley). Thomas --

Re: sort of a beginner question about globals

2005-04-15 Thread Duncan Booth
fred.dixon wrote: :) unit test is something on my to-learn list. seems involved and i haven't seen any straight forward tutorials yet. as yet i still consider myself a hobbyist at best. Hmm, I believe you are right. I can't see any straight-forward tutorials which use Python. I found a

Re: sort of a beginner question about globals

2005-04-15 Thread Scott David Daniels
Duncan Booth wrote: fred.dixon wrote: :) unit test is something on my to-learn list. seems involved and i haven't seen any straight forward tutorials yet. as yet i still consider myself a hobbyist at best. Hmm, I believe you are right. I can't see any straight-forward tutorials which use Python.

Re: sort of a beginner question about globals

2005-04-15 Thread alex23
Duncan Booth wrote: Hmm, I believe you are right. I can't see any straight-forward tutorials which use Python. I found a tutorial at onlamp.com, but it doesn't seem to me to explain TDD at all clearly. Mark Pilgrim's 'Dive Into Python' (http://diveintopython.org/) has a couple of chapters on

Re: sort of a beginner question about globals

2005-04-14 Thread fred.dixon
when i am roughing out my functions and classes i out a pass statement as my first line just as a place holder and a convenient place to put a break when i am testing. no other good reason. -- http://mail.python.org/mailman/listinfo/python-list

Re: sort of a beginner question about globals

2005-04-14 Thread Duncan Booth
fred.dixon wrote: when i am roughing out my functions and classes i out a pass statement as my first line just as a place holder and a convenient place to put a break when i am testing. no other good reason. A better idea when roughing out functions and classes is to insert a docstring

Re: sort of a beginner question about globals

2005-04-13 Thread fred.dixon
I want to use OPTIONS as a global var. In this particular case I am trying to set a global debug constant so I can have some debug logging happen when my program is run with a -debug option. what will actuall end up in OPTIONS is OPTIONS.debug = True as i am using optparse module. --

Re: sort of a beginner question about globals

2005-04-13 Thread Steven Bethard
fred.dixon wrote: I want to use OPTIONS as a global var. In this particular case I am trying to set a global debug constant so I can have some debug logging happen when my program is run with a -debug option. what will actuall end up in OPTIONS is OPTIONS.debug = True as i am using optparse

Re: sort of a beginner question about globals

2005-04-13 Thread fred.dixon
how would i use the following if OPTIONS was in a module ? --- from optparse import OptionParser usage = usage: %prog [options] arg parser = OptionParser(usage) parser.add_option(-d, --debug, ction=store_true, dest=verbose) (OPTIONS = parser.parse_args() ps Im not anywhere

Re: sort of a beginner question about globals

2005-04-13 Thread Steven Bethard
fred.dixon wrote: how would i use the following if OPTIONS was in a module ? --- from optparse import OptionParser usage = usage: %prog [options] arg parser = OptionParser(usage) parser.add_option(-d, --debug, ction=store_true, dest=verbose) (OPTIONS = parser.parse_args() Just

Re: sort of a beginner question about globals

2005-04-13 Thread fred.dixon
ok I'm sorry, I'm not sure what your doing there. if i have to guess it looks like yo might me modifying the imported modules dict with another dict. isn't there a way i can just assign to a var and have it seen no matter what part of the code is currently executing ? --

Re: sort of a beginner question about globals

2005-04-13 Thread fred.dixon
it seems that i can import __main__ where i set the global and then access the var from my local func. is ther any gotcha's involveld in this ? -- http://mail.python.org/mailman/listinfo/python-list

Re: sort of a beginner question about globals

2005-04-13 Thread Steven Bethard
fred.dixon wrote: ok I'm sorry, I'm not sure what your doing there. if i have to guess it looks like yo might me modifying the imported modules dict with another dict. Yes, this takes all the atrributes of the options object and sets them as attributes of the module. If you're afraid of

Re: sort of a beginner question about globals

2005-04-13 Thread fred.dixon
this is what i am using to test with. my program is not much more involved just longer and with a GUI. I did test it and everything works, just wondered if there was something evil and bad about importing main. # #global1.py import global2 import

Re: sort of a beginner question about globals

2005-04-13 Thread Steven Bethard
fred.dixon wrote: #global1.py import global2 import global3 import sys constant = 'dumb' option = sys.argv[1:] pass #global2.func1() a=global2.class1() a.func1() print newvar #global2.py import __main__ pass class class1: def func1(self): __main__.newvar = string pass The other

sort of a beginner question about globals

2005-04-12 Thread fred.dixon
i have read the book and searched the group too -- im not gettin it. i want to read a global (OPTIONS) from file1 from a class method (func1) in file2 i want to understand how this works. -- #file1.py import

Re: sort of a beginner question about globals

2005-04-12 Thread Steven Bethard
fred.dixon wrote: i have read the book and searched the group too -- im not gettin it. i want to read a global (OPTIONS) from file1 from a class method (func1) in file2 i want to understand how this works. --

Re: Beginner Question - Very Easy I'm Sure...

2005-03-25 Thread Todd_Calhoun
Thanks for the tip. I knew it was something easy like that. Brian van den Broek [EMAIL PROTECTED] wrote in message news:[EMAIL PROTECTED] Todd_Calhoun said unto the world upon 2005-03-24 16:13: I'm trying to generate a random number, and then concetate it to a word to create a password.

Beginner Question - Very Easy I'm Sure...

2005-03-24 Thread Todd_Calhoun
I'm trying to generate a random number, and then concetate it to a word to create a password. I get the number and assign it to a variable: + word = dog import random rannum = random.randrange(100,999) str(rannum) word + rannum + But

Re: Beginner Question - Very Easy I'm Sure...

2005-03-24 Thread Jaime Wyant
str() returns a string, it doesn't change rannum which is still a number... try - rannum = str(rannum) jw On Thu, 24 Mar 2005 13:13:25 -0800, Todd_Calhoun [EMAIL PROTECTED] wrote: I'm trying to generate a random number, and then concetate it to a word to create a password. I get the number

Re: Beginner Question - Very Easy I'm Sure...

2005-03-24 Thread Brian van den Broek
Todd_Calhoun said unto the world upon 2005-03-24 16:13: I'm trying to generate a random number, and then concetate it to a word to create a password. I get the number and assign it to a variable: + word = dog import random rannum = random.randrange(100,999)

<    1   2   3