Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Stuck newbie

Reply
Thread Tools

Stuck newbie

 
 
Mike Silva
Guest
Posts: n/a
 
      12-02-2003
Hi all,

I'm a Python newbie trying to write a simple program to do some
processing of a text file. So far I'm able to extract all the data I
need from the file into a list (fileinput is very nice!). Here's my
problem. I want the list ("namelist") to contain records, with each
record consisting of a string ("name"), an integer ("flags"), and two
variable-length lists of integers ("list1" and "list2"). I gather
that the corresponding data object in Python would be a sequence of
those four items, correct? So what I want is a list of sequences?

If that is correct, what I don't understand is how to reference and
modify elements of an individual object. For example, I want to do
things like (in pseudocode):

namelist.append( new_record )
.....
namelist[ index ].flags = 99
namelist[ index ].list1.append( 100 )

But I don't see how to reference the individual elements of a data
object. Any tips greatly appreciated!

Mike
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      12-02-2003
> If that is correct, what I don't understand is how to reference and
> modify elements of an individual object. For example, I want to do
> things like (in pseudocode):


The nice thing is - your pseudocode is quite close python-code

> namelist.append( new_record )
> ....
> namelist[ index ].flags = 99
> namelist[ index ].list1.append( 100 )
>


namelist = []
new_record = [0, [], []]
namelist.append(new_record)
namelist[-1][0] = 99
namelist[-1][1].append(100)

However, if you prefer to access the records fields using names, you can go
with a dicionary:

new_record = {'flags': 0, 'list1':[], 'list2':[]}

Then access is like this

namelist[-1]['flags'] = 99
namelist[-1]['list1'].append(100)

The last approach would be to create a class new_record:

class new_record:
def __init__(self):
self.flags = 0
self.list1 = []
self.list2 = []

Then your code above should work.

Diez
 
Reply With Quote
 
 
 
 
r.e.s.
Guest
Posts: n/a
 
      12-02-2003
"Diez B. Roggisch" <(E-Mail Removed)> wrote ...

> The last approach would be to create a class new_record:
>
> class new_record:
> def __init__(self):
> self.flags = 0
> self.list1 = []
> self.list2 = []


[another beginner here]

Suppose I do the above, followed by this:

records = []
rec = new_record()
rec.flags = 1
rec.list1 = [10]
rec.list2 = ['a']
records.append(rec)
print records
# [<Script1.new_record instance at 0x00BCBDA0>]
print records[-1]
# <Script1.new_record instance at 0x00BCBDA0>
print records[-1].flags, records[-1].list1, records[-1].list2
# 1 [10] ['a']

Is there a simpler way to accomplish the last line
(as I expected the simplest "print records" to do)?

Thanks.
--
r.e.s.
 
Reply With Quote
 
Diez B. Roggisch
Guest
Posts: n/a
 
      12-02-2003
> print records[-1].flags, records[-1].list1, records[-1].list2
> # 1 [10] ['a']
>
> Is there a simpler way to accomplish the last line
> (as I expected the simplest "print records" to do)?


First, you could assign records[-1] to a variable, like this

foo = records[-1]
print foo.flags, foo.list1, foo.list2

Another way would be to implement __repr__ on record:

class record:
.... # the old stuff
__repr__(self):
return repr((self.flags, self.list1, self.list2))

Then

print records[-1]
The __repr__ method is automagically called when print is used on an object.

Diez

 
Reply With Quote
 
sdd
Guest
Posts: n/a
 
      12-03-2003
Diez B. Roggisch wrote:

>... Lotsa good stuff ... Then
>
> print records[-1]
> The __repr__ method is automagically called when print is used on an object.


Well, it tries to call the __str__ method, which defaults to the
__repr__ method if no __str__ is defined.

pedantically yours,
-Scott David Daniels
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
VHDL newbie- stuck just weeks before project submission :(..pleasehelp Sheetal VHDL 3 08-10-2010 06:59 PM
Re: Newbie, Cisco 877, ipv6, IOS, completely stuck News Reader Cisco 10 04-19-2008 04:51 PM
Newbie - Stuck r3bol@yahoo.com Python 8 10-10-2006 09:36 AM
Newbie and stuck! pete Cisco 1 02-24-2006 04:50 PM
Newbie stuck posting to an ASP page as mellow as a horse HTML 6 05-10-2004 07:35 PM



Advertisments