Re: [Tutor] making a list of a custom object

2014-09-20 Thread Steven D'Aprano
Hi Kate, and welcome!

My replies are interleaved between your questions.


On Fri, Sep 19, 2014 at 04:25:50PM -0500, Kate Reeher wrote:

> I have a custom class called Game, and it has a variable called 
> "goals". I'd like to make this a list of custom objects, with various 
> information about the goals.

Unfortunately, either your email, or my email, mangled your code below. 
I've tried to reconstruct it as well I can, but please don't hesitate to 
correct me if I got it wrong.

> class Game: 
> goals = {} 
>
> class Goal(object): 
> def __init__(self,time,goal_by,assist_by,team,is_powerplay ): 
> self.time=time 
> self.goal_by=goal_by 
> self.assist_by=assist_by 
> self.team=team 
> self.is_powerplay=is_powerplay
> 
> Is that Goal class set up correctly?

I can't be absolutely sure, because I'm not sure of your intention, but 
it looks correct to me. That's certainly the usual way to set up a 
class.


> For an instance of Game called game, is this how you'd set a variable 
> of a goal?
> game.goals[i].time= time

You could do it that way to modify an existing goal. (By the way, in 
Python circles, we prefer to talk about "attributes" of instances, not 
instance variables. If you are interested, I'll give you my standard 
rant about this later :-)

Your Game class is a little unusual though. It's not *wrong*, just 
unusual, perhaps you intended to do it this way. You have set the Game 
class to use a single "class attribute", which means that all 
instances will share it. (Actually, the rules are a little more complex 
than that, but for now thinking of it as a shared attribute isn't far 
wrong.) Was that intentional?

If you're familiar with Java, I think that a class attribute is close to 
a Java static variable. (I'm not a Java expert, so I may be wrong.)

If you intend to have multiple Game instances, each with their own 
independent set of goals, you would write it like this:

class Game:
def __init__(self):
goals = {}

but I'm not sure why the goals are kept in a dict. That would require 
you to keep track of whether each goal is the first, second, third... 
goal yourself. I think you want an ordered list:

class Game:
def __init__(self):
goals = []

red_vs_blue = Game()
shirts_vs_skins = Game()

# Add new goals.
red_vs_blue.goals.append(Goal(...))  # Fill in the appropriate args.
shirts_vs_skins.goals.append(Goal(...))
red_vs_blue.goals.append(Goal(...))

# Modify existing goals (but why would you do this?)
red_vs_blue.goals[0].team = 'green'

Because each game has its own list of goals, you can run multiple games 
at the same time. You don't have to track the index of the latest goal, 
you just append a new goal. You only need to care about the index if you 
want to modify an existing goal.

On the other hand, what you originally wrote, with a shared class 
attribute, isn't *wrong*. If you absolutely know that there will never 
be more than one game at a time, there is no need to bother creating a 
Game instance:

class Game:
goals = []

Game.goals.append(Goal(...))
Game.goals.append(Goal(...))
Game.goals[0].team = 'green'

In this case, you can think of the Game class as being almost like a 
singleton instance. (Not quite the same, but it does the same job.)

As I said, this is fairly unusual in Python though. Normally you would 
stick to standard "one class, multiple instances, each with their own 
set of attributes" design.


> I'm finding how python does data classes INCREDIBLY confusing, for 
> whatever reason, so any help would be appreciated.

Sorry to hear that. Please don't hesitate to ask about anything 
confusing. Giving concrete examples is good, and if you are familiar 
with some other language, feel free to say so.



-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question 1

2014-09-20 Thread Steven D'Aprano
Hi Clayton, and welcome.

My responses are interleaved between your questions below.

On Sat, Sep 20, 2014 at 03:20:09PM -0700, Clayton Kirkwood wrote:
> I'm ramping slowly unfortunately. How does one go about knowing which module
> to import to make certain functions work?

Experience, practice, reading the documentation, experimentation.


> I have a read() that fails because
> there is no definition for it. I am using the Wing IDE. I have traversed
> much of the developer's guide and can't find any certainty.

That's a hard question to answer. Where did you get the read() from? I 
can think of three scenarios:

(1) You thought to yourself, "I want to read some data from somewhere, 
hmmm, this sounds like a job for a function called read(), maybe if I 
just try it, it will work!"

Sadly, no it doesn't. What sort of data are you trying to read, and from 
where? If you tell us that, we may be able to advise on the correct way 
to read.


(2) You are copying some code from somewhere else, and it includes 
something like:

result = read(abc, xyz)  # or whatever

You need to go back through the rest of the code until you find a line 
that looks like either:

from something import read

or perhaps:

read = something

or even:

def read(this, that, another):
...


(3) You're reading a tutorial or book, and it suggests using:

result = read(abc, xyz)

In which case, read the tutorial a bit more closely. Perhaps earlier in 
the book they told you where read() comes from? Or maybe they didn't, 
and the tutorial is just broken, mistaken, buggy or confused.

If you show us some of the code around the read() line, we may be able 
to guess what they meant to say.


> Secondarily, why can you import a module without it importing all of its
> daughters? And why do you have to use a 'for in to import submodule', why
> not ' mport module.sub'?

A concrete example might help, because if I'm understanding you 
correctly, you *can* do exactly that.

When you say:

import fe.fi.fo.fum

Python starts off by importing fe, then fe.fi, then fe.fi.fo, then 
fe.fi.fo.fum, so that *all* of those dotted names will work. Here's an 
actual example:

>>> import os.path
>>> os

>>> os.path



So even though I only *manually* imported the os.path submodule, Python 
automatically imported its parent os for me.


> Lastly, in some tutorials and else-sourced docs certain lines have a ';' at
> the end. This seems to be most often with 'opens' and 'fopen' kind of calls.

Are you sure you're reading Python tutorials, not C tutorials? :-)

It's not *wrong* to end lines with a semi-colon, but it is poor style. A 
bit like saying "Um" at the beginning of every sentence. You would have 
to ask the tutorial author why they are doing such a thing. Do you have 
a link to an online tutorial that does that, I'm curious to see it for 
myself.


-- 
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question 1

2014-09-20 Thread Danny Yoo
>> Secondarily, why can you import a module without it importing all of its
>> daughters?
>
> The act of importing a module is "recursive": if you import a module,
> and that module itself has import statements, then Python will do the
> import of the child modules too.  And so forth.

Hi Deb,

Oh!  By the way, this echoes that point about recursion from a week or so ago.

https://mail.python.org/pipermail/tutor/2014-September/102715.html

So yes, recursion shows up at the heart of the Python module import
system too.  That should have been an obvious example, but I forgot.
:P
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] question 1

2014-09-20 Thread Danny Yoo
On Sat, Sep 20, 2014 at 3:20 PM, Clayton Kirkwood  wrote:
> I’m ramping slowly unfortunately. How does one go about knowing which module
> to import to make certain functions work? I have a read() that fails because
> there is no definition for it.

Specific details may help here.  Can you tell us more?  What are you
trying to do?  If you are seeing error messages, please copy exact
error message content and present it.  Error messages have more than
one bit of information: they usually encode some information that
folks here can try to interpret.


> Secondarily, why can you import a module without it importing all of its
> daughters?

The act of importing a module is "recursive": if you import a module,
and that module itself has import statements, then Python will do the
import of the child modules too.  And so forth.

(You might imagine a system where the "main" module must do all this
linkage by itself, rather than do this recursive walk.  And there are
systems that do work like this!  See:
https://talks.golang.org/2012/splash.article for Rob Pike's discussion
on the Plan 9 compiler, which worked this way.

But Python does not do this.)



> And why do you have to use a ‘for in to import submodule’, why
> not ’import module.sub’?

If I'm not mistaken, you _can_ do this.  Can you point us to a source
where it says you can't?

(There are particular style guidelines from certain organizations that
prohibit this kind of import, but that prohibition is by convention.
That is, it's not because the language disallows it, but because it
doesn't fit with the house coding style.)


> Lastly, in some tutorials and else-sourced docs certain lines have a ‘;’ at
> the end. This seems to be most often with ‘opens’ and ‘fopen’ kind of calls.

Huh.  Can you point to a reference?  Note that third-party
documentation (and official documentation!) might be buggy or weird.
:P  If you can point us to an example, maybe one of us can investigate
what's going on there.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Subprocess help

2014-09-20 Thread Danny Yoo
> The command I need to run is "BYPASSROOT=yes 
> ./octosetupBROADCASTER-linux_i386.bin"

Semantically, the command above means:

execute "./octosetupBROADCASTER-linux_i386.bin" in an environment
that binds BYPASSROOT to "yes".

The subprocess.Popen command takes in an optional "env" argument, so
that's what you want to provide when creating this new subprocess.
Here's a link to the documentation.

https://docs.python.org/2/library/subprocess.html#subprocess.Popen

So you'll probably want to do something like:

envCopy = os.environ.copy()
envCopy['BYPASSROOT'] = 'yes'
subprocess.Popen(["./octosetupBROADCASTER-linux_i386.bin"], env=envCopy)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Subprocess help

2014-09-20 Thread Crush
Hey all, hope everyone is well. 

I am trying to write a script that automates a certain task I have recently 
found myself doing a lot lately.

The command I need to run is "BYPASSROOT=yes 
./octosetupBROADCASTER-linux_i386.bin"

I know how to use subprocess to execute the "./octosetup..." command, however 
the "BYPASSROOT=yes" is stumping me seeing how it is not really a unix command. 
I assume it is changing a variable within "octosetup...?" How do I get the 
"BYPASSROOT=yes" to be included in the subprocess.call? 

I know i know "root" is bad, but with this, I have no choice. 

Thanks 

Bo 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] question 1

2014-09-20 Thread Clayton Kirkwood
I'm ramping slowly unfortunately. How does one go about knowing which module
to import to make certain functions work? I have a read() that fails because
there is no definition for it. I am using the Wing IDE. I have traversed
much of the developer's guide and can't find any certainty.

 

Secondarily, why can you import a module without it importing all of its
daughters? And why do you have to use a 'for in to import submodule', why
not ' mport module.sub'?

 

Lastly, in some tutorials and else-sourced docs certain lines have a ';' at
the end. This seems to be most often with 'opens' and 'fopen' kind of calls.

 

Thanks,

 

Clayton

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] making a list of a custom object

2014-09-20 Thread Dave Angel
Kate Reeher  Wrote in message:


> I have a custom class called Game, and it has a variable called "goals". I'd 
> like
> to make this a list of custom objects, with various information about the 
> goals.

> class Game:
>   goals = {}

That's a class attribute; the others below are instance
 attributes. And it's a dict not a list. Perhaps you
 want
  goals = []

But more likely you're expecting to have more than one game, each
 with its own list of goals. In this case you'd want an
 initializer method, and within it you'd say 

 self.goals = []

You'd also want to instantiate at least one game, perhaps like
 (outside of the class definition)

game = Game ()

> class Goal(object):
>   def __init__(self,time,goal_by,assist_by,team,is_powerplay ):
>   self.time=time
>   self.goal_by=goal_by
>   self.assist_by=assist_by
>   self.team=team
>   self.is_powerplay=is_powerplay

> This might be where you should append self to the class attrinute.


> Is that Goal class set up correctly? 

One of many possibilities. 

> For an instance of Game called game, is this how you'd set a 
> variable of a goal?
> game.goals[i].time= time 

First you have to create the goal.

my_goal = Goal ()

Then you probably need to add that goal to a particular Game.

game.goals.append (my_goal)

Then if you have appended several, and you need to patch the time
 of a particular one (the ith one),  you might use your statement.
  But typically in a game, I'd figure the time for a particular
 goal wouldn't change. 




-- 
DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help

2014-09-20 Thread diliup gabadamudalige
http://cscircles.cemc.uwaterloo.ca/run-at-home/

On Fri, Sep 19, 2014 at 11:04 AM, Danny Yoo  wrote:

> On Wed, Sep 17, 2014 at 4:36 PM, Art Pelletier  wrote:
> >
> > I am a beginner with pythons programming   I would like to see if their
> is a site that has samples programs that I can practice on.
>
> Hi Art,
>
>
> Yes, there are some good resources you can check out.  Here's a link
> to some of them:
>
> https://wiki.python.org/moin/BeginnersGuide/NonProgrammers
>
> Personally, I like How to Think Like a Computer Scientist:
>
>  http://openbookproject.net/thinkcs/python/english2e/
>
> but any of the tutorials in the beginner's guide should be helpful.
>
>
>
> Feel free to ask questions here on this mailing list too.
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Diliup Gabadamudalige

http://www.diliupg.com
http://soft.diliupg.com/

**
This e-mail is confidential. It may also be legally privileged. If you are
not the intended recipient or have received it in error, please delete it
and all copies from your system and notify the sender immediately by return
e-mail. Any unauthorized reading, reproducing, printing or further
dissemination of this e-mail or its contents is strictly prohibited and may
be unlawful. Internet communications cannot be guaranteed to be timely,
secure, error or virus-free. The sender does not accept liability for any
errors or omissions.
**
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] making a list of a custom object

2014-09-20 Thread Kate Reeher
I have a custom class called Game, and it has a variable called "goals". I'd 
like to make this a list of custom objects, with various information about the 
goals.
class Game: goals = {}  class Goal(object): def 
__init__(self,time,goal_by,assist_by,team,is_powerplay ):   
self.time=time  self.goal_by=goal_by
self.assist_by=assist_byself.team=team  
self.is_powerplay=is_powerplay

Is that Goal class set up correctly? 
For an instance of Game called game, is this how you'd set a variable of a 
goal?game.goals[i].time= time 

I'm finding how python does data classes INCREDIBLY confusing, for whatever 
reason, so any help would be appreciated.
Thanks,Kate
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor