Couple of noobish question

  • Thread starter Catherine Heathcote
  • Start date
C

Catherine Heathcote

Firstly hi, I don't know any of you yet but am picking up Python and
will be lurking here a lot lol. I am a hobbiest coder (did 3 out of 4
years of a comp tech degree, long story) and am learning Python, 'cos I
saw some code and it just looks a really nice language to work with. I
come from C++, so I am bound to trip up trying to do things the wrong way!

I have been working with Project Euler to get the hang of Python, and
all goes well. I have an idea for a small project, an overly simplistic
interactive fiction engine (well more like those old choose your own
adventure books, used to love those!) that uses XML for its map files.
The main issues I see so far is the XML parsing (I should pick that up
ok, I have a blackbelt in google-foo), but more importantly splitting
code files.

In C++ I would obviously split .cpp and .h files, pairing them up and
using #include. How do I do this in Python? I see that you don't tend to
split logic from defenition, but how do I keep different classes in
different files? My google-fu fails me so far.
 
M

Mike Driscoll

Firstly hi, I don't know any of you yet but am picking up Python and
will be lurking here a lot lol. I am a hobbiest coder (did 3 out of 4
years of a comp tech degree, long story) and am learning Python, 'cos I
saw some code and it just looks a really nice language to work with. I
come from C++, so I am bound to trip up trying to do things the wrong way!

I have been working with Project Euler to get the hang of Python, and
all goes well. I have an idea for a small project, an overly simplistic
interactive fiction engine (well more like those old choose your own
adventure books, used to love those!) that uses XML for its map files.
The main issues I see so far is the XML parsing (I should pick that up
ok, I have a blackbelt in google-foo), but more importantly splitting
code files.

In C++ I would obviously split .cpp and .h files, pairing them up and
using #include. How do I do this in Python? I see that you don't tend to
split logic from defenition, but how do I keep different classes in
different files? My google-fu fails me so far.

You just use the keyword "import". Here's a goofy example:

1) foo.py contains a class called Foo
2) bar.py contains a script that imports Foo:

import foo

# create an instance of the Foo class
myFoo = foo.Foo()


I hope that was clear.

Mike
 
C

Catherine Heathcote

Mike said:
You just use the keyword "import". Here's a goofy example:

1) foo.py contains a class called Foo
2) bar.py contains a script that imports Foo:

import foo

# create an instance of the Foo class
myFoo = foo.Foo()


I hope that was clear.

Mike

Perfect, thanks ^^
 
G

Gary Herron

Catherine said:
Firstly hi, I don't know any of you yet but am picking up Python and
will be lurking here a lot lol. I am a hobbiest coder (did 3 out of 4
years of a comp tech degree, long story) and am learning Python, 'cos
I saw some code and it just looks a really nice language to work with.
I come from C++, so I am bound to trip up trying to do things the
wrong way!

Welcome. I suspect you'll enjoy Python. (Far more than C++ ).
I have been working with Project Euler to get the hang of Python, and
all goes well. I have an idea for a small project, an overly
simplistic interactive fiction engine (well more like those old choose
your own adventure books, used to love those!) that uses XML for its
map files. The main issues I see so far is the XML parsing (I should
pick that up ok, I have a blackbelt in google-foo), but more
importantly splitting code files.

Several modules exits to do the parsing of XML: elementtree, xml, and
beautifulsoup come to mind immediately.
In C++ I would obviously split .cpp and .h files, pairing them up and
using #include. How do I do this in Python? I see that you don't tend
to split logic from defenition, but how do I keep different classes in
different files? My google-fu fails me so far.

Use the import statement for this.

If file a.py defines some classes or functions
a.py:
class UsefulClass:
...
def UsefulFn(...):
...

Then your main Python file imports it and uses the things define in a.py
like this:
import a
ob = UsefulClass(...)
a.UsefulFn()

Good luck,

Gary Herron

 
R

rdmurray

Quoth Catherine Heathcote said:
all goes well. I have an idea for a small project, an overly simplistic
interactive fiction engine (well more like those old choose your own
adventure books, used to love those!) that uses XML for its map files.
The main issues I see so far is the XML parsing (I should pick that up
ok, I have a blackbelt in google-foo), but more importantly splitting

Others have answered your other question, but I thought I'd mention
that you will probably want to take a look at the ElementTree module.

--RDM
 
B

Bruno Desthuilliers

Catherine Heathcote a écrit :
Firstly hi, I don't know any of you yet but am picking up Python and
will be lurking here a lot lol. I am a hobbiest coder (did 3 out of 4
years of a comp tech degree, long story) and am learning Python, 'cos I
saw some code and it just looks a really nice language to work with. I
come from C++, so I am bound to trip up trying to do things the wrong way!

I have been working with Project Euler to get the hang of Python, and
all goes well. I have an idea for a small project, an overly simplistic
interactive fiction engine (well more like those old choose your own
adventure books, used to love those!) that uses XML for its map files.

You may have good reasons to choose this format, but FWIW, some (most ?)
of us here tend to prefer "lighter" formats like json or yaml, or even
just plain Python source. But, well, just so you know there are possible
alternatives to XML !-)
The main issues I see so far is the XML parsing (I should pick that up
ok, I have a blackbelt in google-foo), but more importantly splitting
code files.

???

Oh, you mean, how to organize your source code ?
In C++ I would obviously split .cpp and .h files, pairing them up and
using #include. How do I do this in Python? I see that you don't tend to
split logic from defenition,

Ok.

First thing you must know: in Python, almost everything happens at
runtime (the only error you might get at compile time is SyntaxError),
and "import", "class" and "def" are actually executable statements. That
is, when a module (or main program FWIW) is first loaded (directly for
the main program, via an import statement for other modules), all the
top-level code of the corresponding source file is executed
sequentially. One of the implications is that there's no need for
preprocessor directives - you just use Python code (at the module
top-level) to have alternative versions of a function or conditional
imports (which BTW are _not_ the same as #include directives ). Like:


# somemodule.py

import os

if os.uname()[0] == "Linux":
def somefunc():
return "this is the Linux version"
else
def somefunc():
return "this is not the Linux version"

try:
import some_module
something = some_module.something
else:
# some_module not installed
something = "default"

# etc...


To make a long story short: your notions of "definition" and "logic"
don't really apply here.
but how do I keep different classes in
different files?

You don't necessarily have to keep you class in "different files" - it
really depends on the project's complexity. If you only have a couple
classes, functions and (pseudo)constants, you just stick them either in
the main program file (if it's a simple script) or in a module. If it
gets a bit more complex, regroup classes and functions in distinct
modules or packages trying to make each module (or package) as cohesive
and decoupled as possible. Just have at look at the modules and packages
in the stdlib to see what it may looks like.
My google-fu fails me so far.

You were probably lokking for this:
http://docs.python.org/tutorial/modules.html

My two cents...
 
T

Tim Rowe

2009/2/4 Bruno Desthuilliers said:
# somemodule.py

import os

if os.uname()[0] == "Linux":

On an MS Windows system, os.uname()[0] raises an AttributeError -- sys
doesn't seem to contain uname. Is that a Linux thing? Would os.name
work on Linux? Or would one have to use exception handling and catch
the Windows case?

That's the trouble with using anything in os, of course -- it's os
dependent, which is why it's there! :)
 
A

afriere

....
On an MS Windows system, os.uname()[0] raises an AttributeError -- sys
doesn't seem to contain uname. Is that a Linux thing? Would os.name
work on Linux? Or would one have to use exception handling and catch
the Windows case?

It seems to be a Windows thing. My Linux box gives me 'Linux' the
server gives me 'SunOS' and a Mac tells me 'Darwin'. I'm still
running Python2.4 on the Windows box. At least in that version on the
OS, the 'os' module has no attribute 'uname', On both Linux and
SunOS, os.name returns 'posix'.
 
T

Tim Rowe

2009/2/5 said:
...
On an MS Windows system, os.uname()[0] raises an AttributeError -- sys
doesn't seem to contain uname. Is that a Linux thing? Would os.name
work on Linux? Or would one have to use exception handling and catch
the Windows case?

It seems to be a Windows thing. My Linux box gives me 'Linux' the
server gives me 'SunOS' and a Mac tells me 'Darwin'. I'm still
running Python2.4 on the Windows box. At least in that version on the
OS, the 'os' module has no attribute 'uname', On both Linux and
SunOS, os.name returns 'posix'.

Python in a Nutshell states that os.uname "exists only on certain
platforms", and in the code sample wraps it in a try statement. That
seems to be the safe way to go -- except (and I don't know much about
this) wouldn't code have to be digging into some pretty obscure
corners to find a difference between different posix implementations?
 
A

afriere

[snip]
Python in a Nutshell states that os.uname "exists only on certain
platforms", and in the code sample wraps it in a try statement. That
seems to be the safe way to go -- except (and I don't know much about
this) wouldn't code have to be digging into some pretty obscure
corners to find a difference between different posix implementations?

Perhaps not, if the particular posix implementation is Mac OSX. I
would think that trying os.uname() first and then os.name if it throws
will give you the clearest picture of which OS you are facing.
Alternatively you could query os.name and if that says 'posix' proceed
to call os.uname().
 
B

Bruno Desthuilliers

Tim Rowe a écrit :
2009/2/4 Bruno Desthuilliers said:
# somemodule.py

import os

if os.uname()[0] == "Linux":

On an MS Windows system, os.uname()[0] raises an AttributeError

Thanks for the correction - as you may have guessed, I have not used
windows for years !-)
 
T

Tim Rowe

2009/2/5 Bruno Desthuilliers said:
Thanks for the correction - as you may have guessed, I have not used windows
for years !-)

And I can't get Linux running (more precisely, I can't /keep/
X-Windows running). Isn't it a good job that Python is cross-platform
-- as long as we stay clear of the os module :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top