473,326 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

noob help request - how to make a list of defined class?



I have defined two classes with one common field (called code) and
several different fields.
In class A there is only one instance of any given code as all items
are individual.
In class B, there may be none, one or many instances of each code, as
there can be any number of Bs referring to a single A.

I need to make a list of Bs, remove dupes, and then create a list of
As that have Bs.

(I hope this makes sense!)

with much research and reading of my book I managed to get this
working, but at one stage I had errors that talked about being unable
to concatenate type A. So I converted my As to string, and then did a
split on commas to make a list. This worked fine for all the As that
didn't have a comma in a field (about 85%) but I can't help feeling
that this is a kludge, and that if Icould find a way to get a proper
list of As instead of a list of lists created by converting to string
and splitting, then my app would work properly.

So I am hoping some of the nice folk here will help me out.

The relevent function looks like this:
def gen_B_A_list():
Bcodes = get_codes()
all_As = read_A("A.csv")
B_A = []
for i in range(len(codes)):
Bcode = Bcodes[i]
for A in all_As:
filename = getattr(A, 'code')
if filename == Bcode:
BA = str(A)
BA = string.split(BA, ',')
B_A.append(BA)
return B_A


The element in question is after if filename == Bcode. How do I
construct a list of my defined class A objects here instead of this?
( I can post the full code if needed, just thought it was better
netiquette not to)

TIA

nuffi
Sep 9 '08 #1
1 1150
On Sep 9, 3:11*pm, nuffno...@gmail.com wrote:
I have defined two classes with one common field (called code) and
several different fields.
In class A there is only one instance of any given code as all items
are individual.
In class B, there may be none, one or many instances of each code, as
there can be any number of Bs referring to a single A.

I need to make a list of Bs, remove dupes, and then create a list of
As that have Bs.

(I hope this makes sense!)

with much research and reading of my book I managed to get this
working, *but at one stage I had errors that talked about being unable
to concatenate type A. *So I converted my As to string, and then did a
split on commas to make a list. *This worked fine for all the As that
didn't have a comma in a field (about 85%) but I can't help feeling
that this is a kludge, and that if Icould find a way to get a proper
list of As instead of a list of lists created by converting to string
and splitting, then my app would work properly.

So I am hoping some of the nice folk here will help me out.

The relevent function looks like this:

def gen_B_A_list():
* * Bcodes = get_codes()
* * all_As = read_A("A.csv")
* * B_A = []
* * for i in range(len(codes)):
* * * * Bcode = Bcodes[i]
* * * * for A in all_As:
* * * * * * filename = getattr(A, 'code')
* * * * * * if filename == Bcode:
* * * * * * * * BA = str(A)
* * * * * * * * BA = string.split(BA, ',')
* * * * * * * * B_A.append(BA)
* * return B_A

The element in question is after if filename == Bcode. * How do I
construct a list of my defined class A objects here instead of this?
( I can post the full code if needed, *just thought it was better
netiquette not to)

TIA

nuffi
Looks like homework, but as you've made some kind of effort, some
hints to where to refer to in the manual.

1) Use the csv module for reading CSV files
2) A dict maps one value to another... (check it out)
3) Don't use getattr, use member access (the . symbol, eg: A.code)
4) The for statement can loop over each item in term without an
explicit range (eg, for bcode in codes...)
5) Read the help for str, and read up on how to create objects, for
instance str(A) returns A's string representation, while A() creates a
new A object...

Work your way through a python tutorial and make sure you understand
the examples and principals... It might take you a while though, so I
hope the assignment's not due soon!

Jon.
Sep 9 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
6
by: Igorati | last post by:
Ok, this is what I have so far: #This program will ask for a user to imput numbers. The numbers will then be calculated #to find the statistical mean, mode, and median. Finallly the user will be...
8
by: Rich Grise | last post by:
I think I've finally found a tutorial that can get me started: http://www.zib.de/Visual/people/mueller/Course/Tutorial/tutorial.html and I've been lurking for awhile as well. What happened is,...
0
by: Anders Borum | last post by:
Hello! I would like to request a new method on the XsltArgumentList class, allowing developers to check the presense of a key/value pair. If you try to add a key/value pair that has already been...
42
by: Holger | last post by:
Hi guys Tried searching for a solution to this, but the error message is so generic, that I could not get any meaningfull results. Anyways - errormessage:...
11
by: Brian Blazer | last post by:
OK, I have a very simple class here: class Student: """Defines the student class""" def __init__(self, lName, fName, mi): self.lName = lName self.fName = fName self.mi = mi
5
by: jason | last post by:
While I wait for my books to arrive.. anticipating the RTFM responses... I am trying to understand the C# code below. The code is suppose to be scraping stoke quote info off the MSN site. To help...
9
by: davetelling | last post by:
I am not a programmer, I'm an engineer trying to make an interface to a product I'm designing. I have used C# to make a form that interrogates the unit via the serial port and receives the data. I...
4
by: .nLL | last post by:
Hi, im am a classic asp developer and started to learn asp.net but got stuck with a simple problem even before i step in to further. to learn i have started from a simple project (a login system...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.