[Tutor] Help with packages and namespaces please

2007-10-12 Thread Andrew Wu
Hi all,

I'm new to the list and relatively new to python, and very new to the use
and creation of packages.

Let's say I have these files in one directory:

PrintBase.py
PrintHello.py
PrintBye.py

PrintBase defines a very simple base class PrintBase, whose __init__ method
initializes a member to an optionally provided string (otherwise the string
is empty) and whose PrintMe method prints the string given (or empty string
otherwise).
PrintHello defines a class that inherits from PrintBase and whose PrintMe
method prepends a 'Hello, ' to the provided string.
PrintBye defines a class that inherits from PrintBase and whose PrintMe
method prepends a 'Good-bye' to the provided string.

Since all the files are in the same directory, I can use import statements
like from PrintHello import PrintHello ...

I'd like to reorganize the files so they're like the Sound example in the
Python tutorial:

PrintMe/
PrintMe/PrintBase/PrintBase.py
PrintMe/PrintHello/PrintHello.py
PrintMe/PrintBye/PrintBye.py

I've created empty __init__.py files in each of the subdirectories.  Here I
run into a number of problems and get confused - my goal is to keep the
import statements as they are (the actual files I'd be editing are many and
I'd rather avoid having to edit them all) - is there something I can put in
the __init__.py files so that the modules are brought into namespace w/o
having to use absolute module notation?

As simple tests, I've tried running different import commands in the python
interpreter:

I've tried adding the absolute directory paths to __path__ in each
__init__.py per subdirectory, and adding the paths to sys.path.  When I do
that and run the python intepreter and try 'from PrintMe.PrintHello import
PrintHello' I get an error saying 'Error when calling the metaclass bases,
module.__init__() takes at most 2 arguments (3 given)'.  The same code
functioned fine when all the files were in the same directory, so I'm
confused about why the interpreter thinks I'm passing 3 arguments along.

W/o those directories in sys.path I get an import error (there is no module
named 'PrintBase').  Also, if I'm in one of the subdirectories I get a
similar error when attempting to import the toplevel (PrintMe) package.

(Essentially I would like to structure the directories (er package?) so that
files in one subdirectory can subclass base classes from a sibling (not
parent) directory, ideally in a way that doesn't require each file to state
something like 'from 'PrintMe.PrintHello ... import ...' but instead 'from
PrintHello import ...'.  Is this possible, and if so how can I go about
doing it?)



Many Thanks!

Andrew
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with packages and namespaces please

2007-10-15 Thread Andrew Wu
Hmmm ... thank you very much for the detailed explanations.  This is a very
simplified version of some actual code I'm currently using and attempting to
rework on each individual file (about 60+ of them).  If it's not feasible
then I guess I'm stuck with the rework.  =(

More comments inline ...

On 10/13/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
>
> Andrew Wu wrote:
>
> > Let's say I have these files in one directory:
> >
> > PrintBase.py
> > PrintHello.py
> > PrintBye.py
>
> > I'd like to reorganize the files so they're like the Sound example in
> > the Python tutorial:
> >
> > PrintMe/
> > PrintMe/PrintBase/PrintBase.py
> > PrintMe/PrintHello/PrintHello.py
> > PrintMe/PrintBye/PrintBye.py
>
> Will there be more modules in each subdirectory? I'm not sure if this is
> just for learning or if it is a simplification of a real problem, but
> from your description of the classes it sounds like they belong in the
> same package.


Yes - there will be more modules in each subdirectory (I only had one in the
example for my own testing purposes and for simplification).



> I've created empty __init__.py files in each of the subdirectories.
> > Here I run into a number of problems and get confused - my goal is to
> > keep the import statements as they are (the actual files I'd be editing
> > are many and I'd rather avoid having to edit them all) - is there
> > something I can put in the __init__.py files so that the modules are
> > brought into namespace w/o having to use absolute module notation?
>
> You can bring names into the package namespace, but the actual code will
> still run in the module where it is defined. For example, in
> PrintMe/PrintBase/__init__.py you can say
>from PrintBase import Printbase
>
> Hmm. I'm not sure if this will work the way you want because of the name
> collisions. Using the sound example where the names are unique, suppose
> wavread.py includes a function read_wav(). Then in Formats/__init__.py
> you can say
>from wavread import read_wav




This imports the name read_wav into the Formats package namespace so
> other code will be able to say
>from Sound.Formats import read_wav
>
> In your example, *without* the import in PrintBase/__init__.py it is
> already valid to say
>from PrintMe.PrintBase import PrintBase
> which will access the *module* PrintBase. When you add the line
>from PrintBase import Printbase
> to __init__.py then there are two possible meanings for
> PrintMe.PrintBase.PrintBase and I don't know which one will have
> priority. My guess is that the module import will win but I don't know.
>
> At best this seems like a recipe for endless confusion - you have three
> entities named PrintBase - a package, a module and a class.


Hmmm ... this could be due to the poor naming convention (which also
afflicts the actual code I'm modeling - the use of the same name for the
class and file) ...



If the import in __init__ promotes the class as you want, it still won't
> help with the imports in PrintHello.py - it will still have to say at
> least
>from PrintMe.PrintBase import PrintBase
>
> Why do you want to do this? It looks like a confusing mess to me.
> >
> > As simple tests, I've tried running different import commands in the
> > python interpreter:
> >
> > I've tried adding the absolute directory paths to __path__ in each
> > __init__.py per subdirectory, and adding the paths to sys.path.
>
> Yuck. What is the point of the directories and packages if you are going
> to put every thing on sys.path anyway? But I think it should work if you
> put each of the subdirectories of PrintMe on sys.path.


My hope was to avoid having to edit the individual files but if that leads
to a gobbled mess then I'd rather do it more correctly.


> When I
> > do that and run the python intepreter and try 'from PrintMe.PrintHello
> > import PrintHello' I get an error saying 'Error when calling the
> > metaclass bases, module.__init__() takes at most 2 arguments (3
> > given)'.  The same code functioned fine when all the files were in the
> > same directory, so I'm confused about why the interpreter thinks I'm
> > passing 3 arguments along.
>
> You get an error on the import? Please show the traceback.


It could be I'm doing it all wrong.

Hmmm ... I can't get it to happen again.  If / when I do I'll post the
traceback.  Thanks!



Andrew



Kent
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with packages and namespaces please

2007-10-15 Thread Andrew Wu
After a more careful perusing and sifting of your response, I discovered
this helped my understanding a lot better:


>
> You can bring names into the package namespace, but the actual code will
> still run in the module where it is defined. For example, in
> PrintMe/PrintBase/__init__.py you can say
>from PrintBase import Printbase
>
> Hmm. I'm not sure if this will work the way you want because of the name
> collisions. Using the sound example where the names are unique, suppose
> wavread.py includes a function read_wav(). Then in Formats/__init__.py
> you can say
>from wavread import read_wav
>
> This imports the name read_wav into the Formats package namespace so
> other code will be able to say
>from Sound.Formats import read_wav



This above seems to be closest to what I'm hoping to achieve ... I will try
this out and see how it goes.  Thanks again!


Andrew
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with packages and namespaces please

2007-10-17 Thread Andrew Wu
Thanks again for your help.

I guess I should ask a more basic question about hierarchical directory
structures and packages.  If I have a bunch of files in a flat (single)
directory structure that I want to reorganize into a hierarchical directory
structure, do I necessarily have to turn them into package files (i.e. add
the __init__.py files, or can I simply place them into a subdirectory
structure and do something else so python can find the modules?

Or is it better practice whenever a hierarchical directory structure exists
to treat it as a package?



Thanks!

Andrew
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Regular Expression help - parsing AppleScript Lists as Strings

2007-10-31 Thread Andrew Wu
Hi,

I'm writing utilities to handle return values from AppleScript in python and
one of the steps is recognizing when a returned value from an AppleScript
execution (via popen and osascript) represents a list (in AppleScript) or
not.  For the most part I think I have the correct matching pattern, but I
am hung up on one of the sample strings I was using to test it - AppleScript
allows for one level of nested lists (I believe) and I get tripped up with
attempting to match lists with nested lists.

My second question is, once I have a pattern matching correctly, I need to
convert the AppleScript list into a Python list - I've read a bit about the
findall() method of the re module and was wondering if that would work in
this instance (there's also split() but I've been having issues with that,
probably b/c my pattern matching isn't correct).


Thank you!

Andrew

(source code below)



#!/usr/bin/env python
# Sample script to test if a string represented an AppleScript List or not

import re
import os

def IsASList(thestr=''):
   # AppleScript lists are bracked by curly braces with items separate by
commas
   # Each item is an alphanumeric label(?) or a string enclosed by
   # double quotes or a list itself
   # e.g. {2, True, "hello"}
   #
   # They differ from AppleScript records in that AS records have a key and
value:
   # {name: "Buffy", field: "Slaying", job: true, age: 21}
   #
   # Now the question is how to make the distinction?

   pattern = '''
  ^{# Must start with a curly brace
  (
  \s*? # Group to repeat; clear the whitespace after commas first
  (   # Start of group of alternating match possibilities
  ".+?"   # Match a string
  | \d+? # Match a number
  | true|false  # Match 'true' or 'false' label
  )   # End of group of alternating match possibilities
  ,?)* # Items are comma-delimited, except for the last item
  }$# Must end with a closing curly brace
   '''

   pattern2 = '''
  (
  \s*? # Group to repeat; clear the whitespace after commas first
  (   # Start of group of alternating match possibilities
  ".+?"   # Match a string
  | \d+? # Match a number
  | true|false  # Match 'true' or 'false' label
  )   # End of group of alternating match possibilities
  ,?)* # Items are comma-delimited, except for the last item
   '''

   pattern3 = '''
  ^{
  (
  %s
  | {%s}   # Possible to have 1 level of nested lists
  ,?)* # Items are comma-delimited, except for the last item
  }$
   ''' % (pattern2, pattern2)

   regex = re.compile(pattern3, re.VERBOSE)
   result = regex.match(thestr)

#   print 'Result: ',
#   try:
#  print result.groups()
#   except AttributeError:
#  pass

   if result:
  return True
   else:
  return False


# main()

sample_strs = [
   '{}',  # Empty list
   '{a}', # Should not match
   '{a, b, c}', # Should not match
   '{"hello"}',
   '{"hello", "kitty"}',
   '{true}',
   '{false}',
   '{true, false}',
   '{9}',
   '{9,10, 11}',
   '{93214, true, false, "hello", "kitty"}',
   '{{1, 2, 3}}',  # This matches
   '{{1, 2, "cat"}, 1}',  # This matches

# These don't match:
   '{{1,2,3},1,{4,5,6},2}',
   '{1, {2, 3, 4}, 3}',
   '{{1, 2, 3}, {4, 5, 6}, 1}',
   '{1, {1, 2, 3}}',  # Should match but doesn't
   '{93214, true, false, "hello", "kitty", {1, 2, 3}}',  # Should match but
doesn't
   '{label: "hello", value: false, num: 2}',  # AppleScript dictionary -
should not match
]

for sample in sample_strs:
   print 'Is AppleScript List:  %s;   String:  %s' % (str(IsASList(sample)),
sample)
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Regular Expression help - parsing AppleScript Lists as Strings

2007-11-01 Thread Andrew Wu
Ah - thanks for the correction!  I missed the extra grouping and the
extra spacing ... doh!  Sorry about the HTML-formatted e-mail ...

Thanks also for the pyparsing variant as well - I didn't know the
module existed before!



Andrew

On 11/1/07, Kent Johnson <[EMAIL PROTECTED]> wrote:
> Andrew Wu wrote:
>
> >pattern3 = '''
> >   ^{
> >   (
> >   %s
> >   | {%s}   # Possible to have 1 level of nested lists
> >   ,?)* # Items are comma-delimited, except for the last item
> >   }$
> >''' % (pattern2, pattern2)
>
> The above doesn't allow comma after the first instance of pattern2 and
> it doesn't allow space after either instance. Here is a version that
> passes your tests:
>
> pattern3 = '''
>^{
>(
>(%s
>| {%s})   # Possible to have 1 level of nested lists
>,?\s*)* # Items are comma-delimited, except for the last item
>}$
> ''' % (pattern2, pattern2)
>
> You might want to look at doing this with pyparsing, I think it will
> make it easier to get the data out vs just recognizing the correct pattern.
>
> Kent
>
> PS Please post in plain text, not HTML.
>
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor