Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

python classes

Name: Anonymous 2010-01-05 23:50

OK, say I need to create an application that simulates a
family tree.  Each member may have a mom dad and kids.

I need to create a family tree class that contains a
collection of these member class objects and methods for
adding branches, removing members, etc...

I'm mainly having a hard time figuring how to join the
members if that makes sense.

I've got something like...

class member(object):
   
    def __init__(self):
       member.kid = self[0]
       member.dad = self[1]
       etc...


class familyTree(object):

    def __init___(self):
        for m in mlist:
            try:
                m = member(m)

blah blah blah


How to I make the familytree class contain a
collection of member class objects?

Name: Anonymous 2010-01-06 0:44

[code] tags.

Name: Anonymous 2010-01-06 0:47



class member(object):
  
    def __init__(self):
       member.kid = self[0]
       member.dad = self[1]
       etc...


class familyTree(object):

    def __init___(self):
        for m in mlist:
            try:
                m = member(m)

Name: Anonymous 2010-01-06 2:20

>>3
class member(object):
You should *always* start class names with a capital letter. This is probably the most widely accepted naming convention ever, across virtually all programming languages.

member.kid = self[0]
self is not a list. What are you doing?

for m in mlist:
Where have you defined mlist? For that matter, what is it meant to be?

try:
Why on earth would you put a try block there? Do you expect member creation to fail somehow? You don't even have a matching except block.

m = member(m)
You don't seem to understand how Python's bindings work. The loop creates a local binding m to an element in mlist. You can modify the object, and it modifies the corresponding object in mlist because it's the same object, but assigning to m resets the binding to a new object. This doesn't replace it in mlist; in your current code, the created member object is just ignored.

Your sample code has an extremely high error per line ratio. You should probably get a better understanding of Python before attempting to use its object oriented features.

Real help on /prog/? It's more likely than you think

Name: Anonymous 2010-01-06 2:35

>>4

I appreciate your help.

Sorry I just was dashing something off as an example,
capitalization is not the same as my actual code.  I
wanted to just give a bare bones representation and I
left a lot out.

|self is not a list. What are you doing?

Typo, should be a second arg like...


    def __init__(self,arg):
       member.kid = arg[0]
       member.dad = arg[1]
       etc...


I'm expecting a tuple or something similar which contains a
list of values for kid, dad, mom etc...

|Where have you defined mlist? For that matter, what is it meant to be?

Sorry, another typo.


    newmlist = []
    def __init___(self,mlist):
        for m in mlist:
            try:
                newmlist = member(m)


I mean my actual code has a bit more ugliness.  I was
just curious if this was a good way to go about this.

Name: BBCoder 2010-01-06 2:45

> This is how you quote properly.
This is how you quote properly.

Name: Anonymous 2010-01-06 5:32

STOP HELPING HIM

Name: Anonymous 2010-01-06 8:28

member
familyTree

I hope you're not trying to build a family tree and find a 2D embedding for it? I tried to do that once and didn't get anywhere.

I mean my actual code has a bit more ugliness.  I was just curious if this was a good way to go about this.
About what? I don't see what you're trying to do.

Name: Anonymous 2010-01-06 10:23

from networkx import DiGraph

Name: Anonymous 2010-01-06 12:07

>>5
Sorry, another typo.
You keep using that word, I don't think you understand what it means.

Name: Anonymous 2010-01-06 12:37

>>You keep using that word, I don't think you understand what it means.

Well let me expand a bit then.


All I mean by typo was that I was trying to jot down
a rough idea of the code I'm using, not the actual
code since it would be much less readable and is not
essential to the question I'm asking, and in the
process dropped some important aspects of the code.


My question regards whether or not building a family
tree type application using two classes, one being a
member object which represents a single person, and
the other being the actual family tree object is a
sensible way to go.

Alternatives might be to only have one class or heck
I don't know.  This is the way I approached the
problem, I'm curious if others with more experience
with python find it unwise, inefficient or too
complicated.

At some point I will want to be able to add various
methods to add/remove branches and do other things
one would obviously want to be able to do with a
family tree.  e.g. Compare two family trees and if
they share members combine them.  Or combine them
given a marriage (i.e. if one person has a spouse
attribute that matches a member id in another tree
the application could combine them). etc....  It
is a family tree application, that is all.

I hope this clears up all the confusion.

Name: Anonymous 2010-01-06 12:54

I'm curious if others with more experience
with python find it unwise, inefficient or too
complicated.
No. This isn't about python. It's about programming skills in general.

Alternatives might be to only have one class or heck
I don't know.
Well then think about it a little. What kinds of data are you going to use?

add/remove branches
Cool, but family trees aren't actually trees, they're digraphs (with cycles, possibly).

BTW, if it were a tree, you usually represent one using a root node which can have children nodes which can have children nodes and so on.

Name: Anonymous 2010-01-06 12:55

>>12
I'm curious if others with more experience with python find it unwise, inefficient or too complicated.
Alternatives might be to only have one class or heck I don't know.
fixed. Damn, why do you put those newlines in your posts?

Name: Anonymous 2010-01-06 15:16

>>13
Because the text reaches the end of the line, and if I don't
insert a line break, it will go off the edge of the screen!

Name: BBCoder 2010-01-06 16:23

>>11 Why don't you listen?

>> This is not how you quote.

>This is now how you god damn quote either.

> Put a [m]>[/m] at the beginning of the line, [b]followed by a space[/b] and then the quote text.
Put a > at the beginning of the line, followed by a space and then the quote text.

And just because I hate when people don't know how to multi-line quote, I'll tell you how to do that too.

> Put it at the end of a line like so: [o]
And then continue the quote on the next line.  You don't need another >.
You must close the tag at the end. [/o]
Put it at the end of a line like so:
And then continue the quote on the next line.  You don't need another >.
You must close each tag at the end.

Name: Anonymous 2010-01-06 16:35

>>15
Some
BBCoder
you
are.

Name: Anonymous 2010-01-06 17:04

Am
I
doing
it
right?

Name: Anonymous 2010-01-06 17:27

>>15
Wait, am I the only one who uses [br ] instead?

Name: Anonymous 2010-01-06 18:33

>>18
Yes.

Name: Anonymous 2010-01-06 19:42

>>14

You are not me.  I am me; the op.

I break my lines because I'm old school.

Ever hear of usenet?

Name: Anonymous 2010-01-06 19:52

The question was about building a tree class with nodes
in it.

Should I have a tree class and a node class, or just a
tree class?  Does anybody have any advice?  Is this a
fairly common task?  Have you coded this in other
languages, etc....

What's so hard about this?  Everybody would rather talk
about how to quote in BB code?  Really?

And before telling me I need to improve my general
programming abilities let me tell you that this is
exactly what I am trying to do.  All of us start
somewhere and increment from that point.  We don't pop
out of zeus' head knowing everything with all volumes
of TAOCP memorized.

A simple question deserves a simple answer dickwads.

Name: Anonymous 2010-01-06 20:20

>>21
(Post truncated.)
Stopped reading right there. And mind you, I was reading from the bottom up.

Stop using redundant linebreaks and maybe I'll help you.

Name: Anonymous 2010-01-06 20:32

Everybody would rather talk about how to quote in BB code? Really?
The question was uninteresting, so we naturally moved on to different topics.

But allow me to give you a piece of advice. Take a piece of paper, step away from the computer and give the problem some real thought. Draw out what it is you want to do, and if your stumped on how to represent it in the computer, just code it up and worry about cleanliness later. A bad solution is still a solution, but a problem never attempted is not. The best way to learn to solve a problem is not to ask the internet, but to think and to think hard.

Name: OP 2010-01-06 20:48

>>22
Ok, now you've fuqin angered an expert programmer. God
fuckign damn.  First of all, you dont fuqin know what a
man page is.  Secondly, this is /prog/, do not demand
useful answers the way you want them to be.

Thirdly, programming is all about philosophy and
``abstract bullshite'' that you will never comprehend.
And fuqin lastly, fuck off with your bullshyt.  Everything
has already been answered in >>6,12,15

Have you read your fan-mail today?  Didn't think so.

Name: Anonymous 2010-01-06 21:24

>>24

With all your fan mail I'm surprised you're so touchy. Anyway, my problem had nothing to do with man pages so that doesn't even make any sense.  Do _you_ know what a man page is? And I'm aware that programming is heavily abstract. I guess I'm just too simple to know about all that fancy philosophy and stuff you get with your stanford phd. I'm a simple guy.  I code.  I want to code better.  That's all.  That you get so upset over a simple question really says a lot about the value of all the stuff you know.  Stuff "I'll never be able to comprehend".

And I don't feel bad for bringing a programming question to prog.  That so many keystrokes are wasted on flaming me for being a learner should make you stop and think though.

Name: Anonymous 2010-01-06 21:35

>>25
stop and think
That 4chan is filled with trolls?  Who'd ever have guessed!

I'd love to help you but I know very little Python, so I'll just remind you why 4chan has been known as the asshole of the Internet for years.

Name: Anonymous 2010-01-06 21:48

>>25
I love it when people respond to classic copypasta.

Name: Anonymous 2010-01-06 21:49

So many OP impersonators...

As >>12 mentioned, a family tree is not a real tree but a directed graph. There are lots of different data structures for representing these.

For a question this simple and abstract, Google is a far better tutor than a bunch of trolling idiots on an unmoderated forum.

Name: Anonymous 2010-01-07 0:37

>>26
THIS IS WORLD4CH NOT 4CHAN YOU INSUFFERABLE FOOL

NEVER POST AGAIN

Name: Anonymous 2010-01-07 2:58

>>26
4chan has been known as the asshole of the Internet
Please. We are civilized here. Even if you are not, you should show some respect and careful consideration in your choice of vocabulary, especially when discussing such meta-topics as the identity of /prog/ itself. Here, allow me to demonstrate: The word you should have used is "anus." Hence: "4chan has been known as the anus of the Internet."

Thank you for patiently reading this post and studying /prog/ etiquette! We hope your anus does not consume too many madnesses during your stay.

Name: Anonymous 2010-01-07 16:34

>>25
Am I being meta-trolled by replying to >>25-kun?

Name: Anonymous 2010-01-07 16:42

>>31
Look at it this way.

On most browsers, you can bring up your browsing history by pressing Control-H. (No, this is not going to become a discussion of werecows.) On Firefox, this brings up a sidebar that shows up on the left side of the window. If you put your mouse over the edge of the sidebar, the cursor will turn into a different kind of arrow. By clicking and dragging it, you can move the edge of the sidebar back and forth. You are, to put it another way, manipulating the border between the normal window and the history window. By moving the mouse, you can increase the portion of the window devoted to either part. In a more extreme view of this situation, you're increasing or decreasing the amount of existence the sidebar has.

Now, let's apply this idea to something more abstract. Look out your window. If you don't live in a highly urbanized area, you should be able to see the horizon. Think of this as the border between the land and the sky. The land and sky are obviously distinguishable thanks to this boundary. Now, if you were to "drag" the sash between the sky and the land, or to manipulate the border between land and sky, you would end up causing the sky to become larger and the land to become smaller, or vice versa. An effect of this might be to cause something that was just on the ground to suddenly be hundreds of feet in the air. Truly a frightening situation to be in. So, look at it this way - manipulating the border between two physical things shifts whatever balance there is in the interaction between those things. Alternatively, by manipulating the border between two things, you can change the manner in which they exist.

Still, this isn't *that* abstract, since it's still dealing with real things in the real world. Many believe that in this world, there are those things that are true, and those that obviously aren't. This divides reality into two extremes: truth and falsehood. But, since we have two extremes, logically one can imagine a boundary between those two extremes - the border between truth and lies. If one were to manipulate this border, suddenly things that were pure fantasy (flying pigs, for the sake of argument) have become reality - or things from reality have ceased to exist. This is how Yukari is said to have invaded the moon - by manipulating the border between truth and lies, as applied to the reflection of the moon on a pond, she was able to make the reflection of the moon into a manifestation of the actual moon, and so send her youkai army onto it. This is what's truly amazing about Yukari's power - the ability to manipulate the border between completely abstract concepts allows her to fundamentally change reality as we know it (at least in terms of two abstract concepts).

Name: Sage Master Sagey 2010-01-07 16:48

>>32
sage for tl;dr

Name: Anonymous 2010-01-07 19:43

>>33
More like sage for a worthless post in a worthless thread.

Name: Anonymous 2010-01-08 4:22

Just for you, OP, even though it probably won't help you much with your family tree application.

This is a tree:

    a
   / \
  b   c
     /|\
    d e f
    |
    g


But notice that this part of it is also a tree:

[/m]  d
  |
  g[/m]

And even g itself is a tree.

As you can see, a tree is just a node with (any number, including 0) children trees (nodes, whatever).

The simplest way would be for each node to only store its children. In Python you could do it like:

class Node(object):
    __slots__ = ['children']
    def __init__(self, *cs):
        self.children = list(cs)

g = Node()
d = Node(g)
e = Node()
f = Node()
c = Node(d,e,f)
b = Node()
a = Node(b,c)


and you could use any of a..g as the root node of a tree.

Now, this is the basic way to represent a tree and you can extend it however you like. You could keep track of the parent node for each node, or add a tag or value field or whatever.
As for a Tree class, you can choose not to make one at all, but essentialy it would be a glorified Node with some additional functionality or built-in checks or whatever (either inheriting from Node or storing an instance of it in a private field).

Name: Anonymous 2010-01-08 4:23

>>36
[/m]
Oh FFS this completely invalidates whatever I've said :(

Name: Anonymous 2010-01-08 11:29

>>36

That's the advice I needed a few days ago!

Sadly I've already invested enough effort in coding it the wrong way that I'll have to see it through for now.  I'll try your way, the right way, later.  I basically did the opposite and have a small little node class that only sets a node name and parent id attributes.  Everything else is done in the tree class.

Now to drop a branch, something that would be simple under your system, is rather complicated under mine since there is no object representing each node and it's children.

Name: Anonymous 2010-01-08 20:33

Python has no class.

Name: Anonymous 2010-01-09 4:56

Not deriving from object considered harmful.

Name: Anonymous 2010-12-09 5:23

Name: Anonymous 2010-12-24 19:58

Name: Anonymous 2013-01-19 23:12

/prog/ will be spammed continuously until further notice. we apologize for any inconvenience this may cause.

Don't change these.
Name: Email:
Entire Thread Thread List