subclassing list

2005-07-31 Thread spike
I've googled like crazy and can't seem to find an answer to why this
isn't working.

I want to create a custom list class that acts as a circular list.

ie: my_list = (0, 1, 2)

how I want it to behave:

my_list[0] -> 0
my_list[1] -> 1
my_list[2] -> 2
my_list[3] -> 0
my_list[4] -> 1
...etc

so, what I've tried to do is:

def circular_list(list):
def __getitem__(self, i):
if (i >= len(self)):
return self[i % len(self)]
else:
return self[i]

items = circular_list(range(8))

however, when I want to iterate over it with a for statement, I get:

TypeError: iteration over non-sequence

what am I missing?

thanks!

-- 
http://mail.python.org/mailman/listinfo/python-list


Does anyone have a Python Logic Map/Flow Chart? (Example Provided)

2010-02-08 Thread spike
Has anyone been able to come across a Python logic map or flow chart?

An example can be seen here on the right: http://en.wikipedia.org/wiki/Usenet

This would be very helpful for users.
-- 
http://mail.python.org/mailman/listinfo/python-list


Python Logic Map/Logic Flow Chart. (Example Provided)

2010-02-08 Thread spike
Has anyone been able to come across a Python logic map or Python logic
flow chart?

An example can be seen on the right under History:
http://en.wikipedia.org/wiki/Usenet#History

This would be very helpful for all users.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Python Logic Map/Logic Flow Chart. (Example Provided)

2010-02-08 Thread spike
On Feb 8, 1:35 pm, Gary Herron  wrote:
> spike wrote:
> > Has anyone been able to come across a Python logic map or Python logic
> > flow chart?
>
> > An example can be seen on the right under History:
> >http://en.wikipedia.org/wiki/Usenet#History
>
> > This would be very helpful for all users.
>
> Huh???  What aspect of Python were you thinking of mapping out?
>
> Your example us a bad ascii art graph of -- I've no clue what?
>
> Gary Herron

Do you know what "Logic" means? Guess not. Before you comedians quit
your day job some people are looking for information. If you have
nothing positive to say, go pester somewhere else.

Grow up.
-- 
http://mail.python.org/mailman/listinfo/python-list


opening a file using a relative path from a subclass in a package

2005-12-07 Thread spike grobstein
So, I've got this project I'm working on where the app defines various
classes that are subclassed by module packages that act like plugins...

I'd like the packages to define a file path for supporting files
(graphics, etc) that are stored inside the package. The problem is that
the superclass's definition (stored elsewhere) has all of the code for
actually opening the files, so when I use the
os.path.dirname(os.path.abspath(__file__)) trick, it's finding the
superclass, not the package's path.

is this possible? or do I have to add code to each subclassing package
to calculate the absolute path of it and pass that as a variable?

I'd rather not have to repeat myself for each package that I make, and
I've got a feeling that I'm missing something really obvious.

thanks in advance!


...spike

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: opening a file using a relative path from a subclass in a package

2005-12-07 Thread spike grobstein
oh, wow. that works!!!

thanks for the help!

so, since python supports module packages like it does, you'd think
that it would have ways of making add-on or extension modules to be
more self contained.

Thanks, again!

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: opening a file using a relative path from a subclass in a package

2005-12-07 Thread spike grobstein
I understand why it wasn't working and it makes sense based on the
structure of namespaces that python defines, however, I'm just
surprised that there isn't some kind of built-in facility for dealing
with these types of things.

Module packages are a spectacular idea, it is just kinda easy to get
confused when you start spaghettifying your imports with multiple
directories and whatnot.

My whole reason for wanting to do this is that I've written a program
that contains a framework for extending the application. It's got a
plugin-like module package framework that allows endusers to add new
functionality, and I'd like it to not only be as trivial as possible to
create new plugins (by simply plugging in values to a new subclass),
but to also repeat as little code as possible (the DRY principal; don't
repeat yourself)

I toyed with the idea of using generic filenames (icon.png,
description.rtf, etc), but ultimately decided against it when I thought
about things I'd want to implement in the future.

-- 
http://mail.python.org/mailman/listinfo/python-list


importing files from a directory

2005-07-09 Thread spike grobstein
I'm a total Python newbie, so bear with me here...

I'm writing a program that has a user-configurable, module-based
architecture. it's got a directory where modules are stored (.py files)
which subclass one of several master classes.

My plan is to have the program go into the folder called "Modules" and
load up each file, run the code, and get the class and append it to an
array so I can work with all of the objects later (when I need to
actually use them and whatnot).

What I'm currently doing is:

import os

print "Loading modules..."
mod_dir = "Modules"

module_list = [] #empty list...

dir_list = os.listdir(mod_dir)

for item in dir_list:
# strip off the extensions...
if (item == "__init__.py"):
continue
elif (item[-3:] == '.py'):
mod_name = item[:-3]
elif (item[-4:] == '.pyc'):
mod_name = item[:-4]
else:
continue

print "Loading %s..." % mod

module_list.append(__import__("Modules.%s" % mod))

print "Done."


it works more or less like I expect, except that...

A. the first time it runs, blah.py then has a blah.pyc counterpart.
When I run the program again, it imports it twice. Not horrible, but
not what I want. is there any way around this?

B. module_list winds up consisting of items called 'Modules.blah' and
I'd like to have just blah. I realize I could say:

my_module = __import__("Modules.%s" % mod)
module_list.append(getattr(my_module, mod))

but...

is there a better way to accomplish what I'm trying to do?

tia.



...spike

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: importing files from a directory

2005-07-11 Thread spike grobstein
my reason for loading both the .py and .pyc files was just in case
compiled files were supplied as modules... but I'm gonna disallow that,
so yeah.

I also got a response in email and I've been dabbling with my code
since I posted this and found a slightly better way of handling this
plugin system...

I stuck the import code into the Modules/__init__.py file, so it can
act as a kind of manager (instead of moving the files to a
Modules(disabled) directory) and appended the __import__s to an array.

like this:

[EMAIL PROTECTED] ~/Aphex $ cat Modules/__init__.py
module_list = []

def load_module(mod_name):
mod = __import__("Modules.%s" % mod_name)
mod = getattr(mod, mod_name)
module_list.append(mod.module())

def load_modules():
load_module("nes")
load_module("snes")
load_module("mame")

load_modules()

[end code]

I then just have to 'import Modules' from my main program and the whole
module import thing is encapsulated and invisible to my main program.

I'm gonna add some functions to the Modules module to make fetching
plugins a little less ambiguous (get_module(index) and
get_named_module(module_name), etc).

Thanks for the help. Python's making me have to think a little
backwards. I have to make sure I declare my functions before I call
them, but it's a very cool language overall.

...spike

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: subclassing list

2005-07-31 Thread spike grobstein
>You also need to post the code that raises the error, or no one else can
debug it.

sorry, I thought I had pasted that line in there, but I guess I missed
it. Here's the full code (after modifying it slightly based on your
post):

#! /usr/bin/env python

def circular_list(list):
def __getitem__(self, i):
return list.__getitem__(self, i % len(self))

items = circular_list(range(8))

for i in items:
print i

[/end code]

[EMAIL PROTECTED] ~/tests/afx-view $ ./circular.py
Traceback (most recent call last):
  File "./circular.py", line 10, in ?
for selected in items:
TypeError: iteration over non-sequence

if I just try to call:

print items[12]

I get this error:
[EMAIL PROTECTED] ~/tests/afx-view $ ./circular.py
Traceback (most recent call last):
  File "./circular.py", line 9, in ?
print items[12]
TypeError: unsubscriptable object

I'm using python 2.3.5

-- 
http://mail.python.org/mailman/listinfo/python-list