Re: Are there any AOP project in python community?

2006-08-02 Thread hiaips
steve wrote:
> I mean Aspect-Oriented Programming.
> If any please give me some of links.
> Thanks a lot.

See http://en.wikipedia.org/wiki/Aspect-oriented_programming.
There is a list of AOP implementations for a number of languages
(including Python) near the bottom of the page.

--hiaips

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


Re: exception handling; python program that interacts with postgresql db

2006-08-02 Thread hiaips

damacy wrote:
> hi, there. i have this question which might sound quite stupid to some
> people, but here we go anyway.
>
> i have written a python program which interacts with a postgresql
> database. what it does is simply drops an existing database called
> 'mytempdb'.
>
> the code looks like below;
>
> link = subprocess.Popen(command, stdin = subprocess.PIPE, stdout =
> subprocess.PIPE, shell = True)
> link.communicate(password)
> link.wait()
>
> where command looks like "psql -h 127.0.0.1 -U postgres -W -f filename"
> and
> filename is the name of the file which contains a single SQL command
> which is "drop database mytempdb".
>
> the program works fine as long as a correct password is supplied,
> however, i have a problem if the password is incorrect since this
> exception is *not* handled within the scope of my program, instead,
> what is does is showing some error messages in the prompt. so my
> program, without knowing whether an errors has taken place or not, goes
> on to execute the next task.
>
> any clue? please let me know if you think the problem is not well
> addressed. =)
>
> thanks. have a nice one.

Hi, damacy,

Maybe I'm not understanding your code 100%, but have you tried catching
the return value of the psql process that you're launching? Just a
thought...

--hiaips

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


Re: exception handling; python program that interacts with postgresql db

2006-08-02 Thread hiaips

Another option would be to use the psycopg module to connect to
postgres from within your Python code. See
http://www.initd.org/projects/psycopg1 for more information.

--hiaips

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


Re: Running queries on large data structure

2006-08-03 Thread hiaips
Christoph,

Several possibilities come to mind...

>From your description, maybe something like Postgres, MySql, or sqlite
would not be the best option. (However, I'm wondering what your query
requirements are -- for example, if you really need the power of SQL,
maybe you should just bite the bullet and map to an RDBMS schema, as
painful as that may be. However, if you need to provide a minimal query
interface, this may not be an issue.) Pickle seems like an option, but
I don't have much experience with it and can't say.

A couple of other possibilities:
1. What about storing your data in XML and using XQuery to facilitate
queries? If your data is deeply nested, as you say, this may be a good
match.
 2. What about storing your data in the same syntax as a Python
dictionary? Is that possibile? (If it is, then your data *is* your
code, and you don't have to worry about parsing it.)

I don't know whether any of this makes sense for your problem, but in
any case, good luck.
--hiaips

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


Re: Running queries on large data structure

2006-08-03 Thread hiaips
Christoph,

Well, if you format the data as a Python dictionary and give the data
file a .py extension, it becomes a Python module that you can load and
reload dynamically. That's sort of what I was thinking. 

--Dave

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


Re: Which Python API for PostgreSQL?

2006-08-04 Thread hiaips

I also recommend psycopg.

--Dave

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


Re: newb question: file searching

2006-08-08 Thread hiaips

> [EMAIL PROTECTED] wrote:
> > I'm new at Python and I need a little advice.  Part of the script I'm
> > trying to write needs to be aware of all the files of a certain
> > extension in the script's path and all sub-directories.  Can someone
> > set me on the right path to what modules and calls to use to do that?
> > You'd think that it would be a fairly simple proposition, but I can't
> > find examples anywhere.  Thanks.

dir_name = 'mydirectory'
extension = 'my extension'
import os
files = os.listdir(dir_name)
files_with_ext = [file for file in files if file.endswith(extension)]

That will only do the top level (not subdirectories), but you can use
the os.walk procedure (or some of the other procedures in the os and
os.path modules) to do that.

--Dave

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


Re: newb question: file searching

2006-08-08 Thread hiaips

> I'm thinking os.walk() could definitely be a big part of my solution,
> but I need a little for info.  If I'm reading this correctly, os.walk()
> just goes file by file and serves it up for your script to decide what
> to do with each one.  Is that right?  So, for each file it found, I'd
> have to decide if it met the criteria of the filetype I'm searching for
> and then add that info to whatever datatype I want to make a little
> list for myself?  Am I being coherent?
>
> Something like:
>
> for files in os.walk(top, topdown=False):
> for name in files:
>  (do whatever to decide if criteria is met, etc.)
>
> Does this look correct?

IIRC, repeated calls to os.walk would implement a depth-first search on
your current directory. Each call  returns a list:
[, ]

--dave

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


Re: newb question: file searching

2006-08-08 Thread hiaips

[EMAIL PROTECTED] wrote:
> Thanks, Dave.  That's exactly what I was looking for, well, except for
> a few small alterations I'll make to achieve the desired effect.  I
> must ask, in the interest of learning, what is
>
> [file for file in files if file.endswith(extension)]
>
> actually doing?  I know that 'file' is a type, but what's with the set
> up and the brackets and all?  Can someone run down the syntax for me on
> that?  And also, I'm still not sure I know exactly how os.walk() works.
>  And, finally, the python docs all note that symbols like . and ..
> don't work with these commands.  How can I grab the directory that my
> script is residing in?

[file for file in files if file.endswith(extension)] is called a list
comprehension. Functionally, it is equivalent to something like this:
files_with_ext = []
for file in files:
  if file.endswith(extension):
files_with_ext.append(file)
However, list comprehensions provide a much more terse, declarative
syntax without sacrificing readability.

To get your current working directory (i.e., the directory in which
your script is residing):
cwd = os.getcwd()

As far as os.walk...
This is actually a generator (effectively, a function that eventually
produces a sequence, but rather than returning the entire sequence, it
"yields" one value at a time). You might use it as follows:
for x in os.walk(mydirectory):
  for file in x[1]:
 

You might want to read up on the os and os.path modules - these
probably have many of the utilities you're looking for.

Good luck,
dave

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


Re: newb question: file searching

2006-08-08 Thread hiaips
Oops, what I wrote above isn't quite correct. As another poster pointed
out, you'd want to do
for file in x[2]:
...

--dave

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


Re: Best IDE for Python

2006-08-14 Thread hiaips

I'm assuming that FOS = "free open source"...

In any case, what operating system do you run? If you're on OS X, I
highly recommend TextMate. It's not free, but it has good support
(either via built-in or third-party plugins) for Python as well as
HTML, SQL, XML, Django templates, and the like. A lot of Rails folks
use it (as well as Django and TurboGears developers, I might add).

The best general-purpose IDE for Python, IMO, is WingIDE. Again, it's
not free (personal license will cost you $30). It runs on each of the
major platforms - Windows, Linux, OS X - and has some nice features,
including code completion, syntax highlighting, a built-in Python
shell, etc. I don't think it has any features built-in specifically for
web dev, however; if you have to do lots of HTML, XML, and SQL as part
of your project, you might want something a bit more general-purpose.

If you're determined to go the FOSS route, you can always use VIM,
Emacs, Eric, or SPE.

Just my two cents...

--dave

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


Re: Beginner Textbook

2006-08-15 Thread hiaips

M_M wrote:
> Michiel Sikma wrote:
> > Introducing 13 year olds to a programming language? You're gonna have a
> > hard time finding good literature for that. Even if you do, it's going
> > to cost a lot of time to guide them.
> >
> > "Beginning Python: From Novice to Professional" by Magnus Lee Hetland
> > might be a good choice. ISBN: 159059519X.
> >
> > Michiel
> >
> > Op 15-aug-2006, om 14:48 heeft M_M het volgende geschreven:
> >
> >> Hi,
> >>
> >> I am looking for a simple text book to introduce 13 to 18 year olds to
> >> python programming. Suggestion?
> >>
> >> New to python.
> >> --http://mail.python.org/mailman/listinfo/python-list
> >
> Thanks - a very bright lot of 13 year olds- need the challenge!

There's a book called "Learning with Python: How to Think Like a
Computer Scientist" that's geared towards high schoolers. It may be
perfect for your students, depending on what you're looking for. In any
case, here's a link:
http://greenteapress.com/thinkpython/

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


Re: MySQLdb installation error

2006-08-15 Thread hiaips

Yi Xing wrote:
> Hi,
>
> I met the following error when I tried to install MySQLdb. I had no
> problem installing numarray, Numeric, Rpy, etc.  Does anyone know
> what's the problem? Thanks!
>
> running install
> running build
> running build_py
> creating build
> creating build/lib.darwin-7.9.0-Power_Macintosh-2.4
> copying _mysql_exceptions.py ->
> build/lib.darwin-7.9.0-Power_Macintosh-2.4
> creating build/lib.darwin-7.9.0-Power_Macintosh-2.4/MySQLdb
> copying MySQLdb/__init__.py ->
> build/lib.darwin-7.9.0-Power_Macintosh-2.4/MySQLdb
> copying MySQLdb/converters.py ->
> build/lib.darwin-7.9.0-Power_Macintosh-2.4/MySQLdb
> copying MySQLdb/connections.py ->
> build/lib.darwin-7.9.0-Power_Macintosh-2.4/MySQLdb
> copying MySQLdb/cursors.py ->
> build/lib.darwin-7.9.0-Power_Macintosh-2.4/MySQLdb
> copying MySQLdb/release.py ->
> build/lib.darwin-7.9.0-Power_Macintosh-2.4/MySQLdb
> copying MySQLdb/times.py ->
> build/lib.darwin-7.9.0-Power_Macintosh-2.4/MySQLdb
> creating build/lib.darwin-7.9.0-Power_Macintosh-2.4/MySQLdb/constants
> copying MySQLdb/constants/__init__.py ->
> build/lib.darwin-7.9.0-Power_Macintosh-2.4/MySQLdb/constants
> copying MySQLdb/constants/CR.py ->
> build/lib.darwin-7.9.0-Power_Macintosh-2.4/MySQLdb/constants
> copying MySQLdb/constants/FIELD_TYPE.py ->
> build/lib.darwin-7.9.0-Power_Macintosh-2.4/MySQLdb/constants
> copying MySQLdb/constants/ER.py ->
> build/lib.darwin-7.9.0-Power_Macintosh-2.4/MySQLdb/constants
> copying MySQLdb/constants/FLAG.py ->
> build/lib.darwin-7.9.0-Power_Macintosh-2.4/MySQLdb/constants
> copying MySQLdb/constants/REFRESH.py ->
> build/lib.darwin-7.9.0-Power_Macintosh-2.4/MySQLdb/constants
> copying MySQLdb/constants/CLIENT.py ->
> build/lib.darwin-7.9.0-Power_Macintosh-2.4/MySQLdb/constants
> running build_ext
> building '_mysql' extension
> creating build/temp.darwin-7.9.0-Power_Macintosh-2.4
> gcc -fno-strict-aliasing -Wno-long-double -no-cpp-precomp
> -mno-fused-madd -fno-common -dynamic -DNDEBUG -g -O3 -Wall
> -Wstrict-prototypes -I/usr/include/mysql
> -I/Library/Frameworks/Python.framework/Versions/2.4/include/python2.4
> -c _mysql.c -o build/temp.darwin-7.9.0-Power_Macintosh-2.4/_mysql.o
> -fno-omit-frame-pointer -arch i386 -arch ppc -pipe
> -Dversion_info="(1,2,1,'final',2)" -D__version__="1.2.1_p2"
> gcc: cannot read specs file for arch `i386'
> error: command 'gcc' failed with exit status 1

What version of OSX are you running?

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


Re: How to delete a directory tree in FTP

2006-08-16 Thread hiaips

T wrote:
> I connect to a FTP server which can be either unix or windows server.
> Once in the FTP session, I would like to delete a directory tree on the
> server.  Is there a command that will do this?  If not, can someone
> point me to a right direction?
>
> Thanks!

Try using an "FTP" object from the ftplib module. There's a "delete"
method there (along with several other useful things.)

--dave

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


Re: How to delete a directory tree in FTP

2006-08-16 Thread hiaips

T wrote:
> I connect to a FTP server which can be either unix or windows server.
> Once in the FTP session, I would like to delete a directory tree on the
> server.  Is there a command that will do this?  If not, can someone
> point me to a right direction?
>
> Thanks!

Oops...just noticed that you wanted to delete a directory tree, not
just a directory. Sorry, I don't know of a single method that does
this.

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


Re: MySQLdb installation error

2006-08-17 Thread hiaips

What I'm getting at is that it looks like one of these arch flags needs
to be removed, as a previous poster said. I remember having a similar
issue with an arch flag when installing some Python module (don't
remember whether it was MySQLdb or not), and I fixed it by installing
the Universal SDK that comes with XCode on OS 10.4. If this is not an
option for you (and it sounds as though it may not be), you might also
try editing the Makefile to remove the flag corresponding to the
architecture that does not match your system.

"uname -a" should tell you which architecture you're on as well as what
version of the OS you're running.

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


Re: Permission Denied

2006-08-19 Thread hiaips

Tom Strickland wrote:
> Hopefully this is a simple question. I've started to program in Python
> after an absence of about a year, so I'm very rusty. I wrote a short
> program and tried to run it using Python2.4 in Linux. I keep getting
> "permission denied" messages after entering the path to the program. I
> switched to the root directory and tried again, but got the same
> result.I ran a very similar program earlier and it ran fine.
>
> What am I doing wrong? The program is:
>
>
> #!/usr/bin/python2.4
> i=1
> while i<1:
> print 'step 1',i
> i+=1
> raw_input()
> print 'step 2'
> 
> 
> Thank you.
> 
> Tom


Is your script executable?

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


Re: key not found in dictionary

2006-08-22 Thread hiaips

KraftDiner wrote:
> I have a dictionary and sometime the lookup fails...
> it seems to raise an exception when this happens.
> What should I do to fix/catch this problem?
>
> desc = self.numericDict[k][2]
> KeyError: 589824   < This is the error that is being produced,
> because there is no key
> 589824.

It raises a KeyError, as you are seeing. Just use a try/except
construction and handle the error as required by your application:

try:
desc = self.numericDict[k][2]
except KeyError, ke:


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


Re: Python + Java Integration

2006-08-23 Thread hiaips


> Java itself never deserved to be the 'next' anything anyway. It was
> sold on hype and has never lived up to it. I can see your point from a
> business perspective but I like to think Python is sold on its merits
> and not on being the new panacea for middle managers to deploy.

Bravo. I could not have said it any better.

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


Re: string splitting

2006-10-16 Thread hiaips

[EMAIL PROTECTED] wrote:
> Hello,
> I have thousands of files that look something like this:
>
> wisconsin_state.txt
> french_guiana_district.txt
> central_african_republic_province.txt
>
> I need to extract the string between the *last* underscore and the
> extention.
> So based on the files above, I want returned:
> state
> district
> province
>
> My plan was to use .split or .find but I can't figure out how locate
> only the last underscore in the filename.
>
> Anyone have any ideas?
>
> Thanks.
> R.D.

Hi,

Try splitting the string on "." and using rfind to find the last
instance of "_".

i.e.,
myStr = "wisconsin_state.txt"
pieces = myStr.split(".")
substr = pieces[0][pieces[0].rfind("_") + 1:]

--hiaips

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


Re: OS X and Python - what is your install strategy?

2006-08-24 Thread hiaips

metaperl wrote:
> I'm about to get a new OS X box on which I will rewrite a bunch of data
> munging scripts from Perl to Python. I know that there are several port
> services for OS X (fink, darwin ports, opendarwin). So I am not sure
> whether to use their port of Python or whether to build from scratch or
> use the Python image file. Also ActivePython is something of a choice
> but for some reason not a front-running one for me. I tend to like
> Gentoo-style compile from source over pre-bundled all-in-one solutions
> like ActivePython.
>
> I'm not going to do much other than maybe install Plone and do some XML
> and CSV processing. Most everything I need is in the Python stdlib.
> Maybe down the road some graphics and web stuff (I like Clever Harold
> or Pylons for that... but I still ahve not examined the 900 other web
> app options in Python :)

These days, I install the OS X universal binary provided on the Python
language web site. You can find it at
http://www.python.org/download/releases/2.4.3. It's more comprehensive
and much more up-to-date than the version included in OS X.

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


Re: Is this a good idea or a waste of time?

2006-08-24 Thread hiaips

asincero wrote:
> Would it be considered good form to begin every method or function with
> a bunch of asserts checking to see if the parameters are of the correct
> type (in addition to seeing if they meet other kinds of precondition
> constraints)?  Like:
>
> def foo(a, b, c, d):
>assert type(a) == str
>assert type(b) == str
>assert type(c) == int
>assert type(d) == bool
># rest of function follows
>
> This is something I miss from working with more stricter languages like
> C++, where the compiler will tell you if a parameter is the wrong type.
>  If anything, I think it goes a long way towards the code being more
> self documenting.  Or is this a waste of time and not really "the
> Python way"?
>
> -- Arcadio

Many developers who move from a statically-typed languages to dynamic
ones go through this same sort of thought process. I was no different
in that regard, but as I've done more and more Python, I've found that
I just don't encounter type issues all that often. Above all, I see
duck typing as a net benefit - its inherent flexibility far outweighs
the lack of "compile-time" type safety, in my mind.

Along these lines, I'll point you to this article by Bruce Eckel called
"Strong Typing vs. Strong Testing":
http://www.mindview.net/WebLog/log-0025. The bottom line: Focus on unit
tests rather than explicit type checks when you want to verify the
runtime safety of your code. You'll find many more errors this way.

Hope this helps...
--dave

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


Re: Newbie Question. Class definitions on the fly.

2006-08-27 Thread hiaips

ishtar2020 wrote:
> Hi everyone
>
> I'm sure this question is kinda stupid and has been answered a few
> times before... but I need your help!
>
>  I'm writing a small application where the user can analyze some text
> based on a set of changing conditions , and right now I'm stuck on a
> point where I'd like to automatically generate new classes that operate
> based on those user-defined conditions.
>
> Is there a way in python to define a class in runtime? For instance,
> can I define a class which extends another(that I have previously
> defined in some module) , create some instance completely on the fly
> and then add/redefine methods to it?
>
> If affirmative, I've thought of a problem I would maybe have to face:
> as the class has been defined by direct input to the python
> interpreter, I could only create instances of it on the same session I
> entered the definition(because it's not on a module I can load on
> future uses) but not afterwards.  Is there a way to keep that code?
>
> Even more newbie paranoia: what would happen if I make that 'on the
> fly" object persist via pickle? Where would Python find the code to
> handle it once unpickled on another session (once again, i take that no
> code with the definition of that instance would exist, as it was never
> stored on a module).
>
> Hope it wasn't too ridiculous an idea.
>
> Thank you for your time, guys.

You might check out the following link on metaclass programming:

http://www-128.ibm.com/developerworks/linux/library/l-pymeta.html

The author gives at least one short example of creating a class
dynamically and using an instance of it. There are also links to other,
related articles and topics at the bottom.

--dave

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


Re: OS X and Python - wxPython has forced a rehash of my approach

2006-09-04 Thread hiaips

[EMAIL PROTECTED] wrote:
> Earlier I asked about how people installed Python on OS X, given that
> one can choose from Xcode, Fink, Darwin, ActiveState and source builds:
>
> http://groups.google.com/group/comp.lang.python/browse_thread/thread/5c4cde4206d1fbb7/37de06a6bb6b2361?lnk=gst&q=OS+X+and+Python+-+what+is+your+install+strategy%3F&rnum=1#37de06a6bb6b2361
>
> but before I knew it, I had been sucked in by the ease and vastness of
> the Fink repository.
>
> Now my bliss run has come to a full halt due to these instructions at
> `wxPython.org `_
>
> Note to Fink users: Versions of Python installed by Fink cannot run
> wxPython (unless you install and run X11...). You need to use Apple's
> Framework builds, or a third-party Framework build.
>
> And reading a bit further:
>
> Python 2.4 Framework builds are available for download from the
> undefined.org MacPython site. ActiveState also distributes a Framework
> build.
>
> So I guess I will look into a Framework build as my starting point.

I'd really recommend using the universal build from python.org. As a
previous poster mentioned, it is a framework build. I've never had any
trouble using wxPython in conjunction with it, either.

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


Re: OS X and Python - wxPython has forced a rehash of my approach

2006-09-04 Thread hiaips

Kevin Walzer wrote:
> hiaips wrote:
> > [EMAIL PROTECTED] wrote:
> >> Earlier I asked about how people installed Python on OS X, given that
> >> one can choose from Xcode, Fink, Darwin, ActiveState and source builds:
> >>
> >> http://groups.google.com/group/comp.lang.python/browse_thread/thread/5c4cde4206d1fbb7/37de06a6bb6b2361?lnk=gst&q=OS+X+and+Python+-+what+is+your+install+strategy%3F&rnum=1#37de06a6bb6b2361
> >>
> >> but before I knew it, I had been sucked in by the ease and vastness of
> >> the Fink repository.
> >>
> >> Now my bliss run has come to a full halt due to these instructions at
> >> `wxPython.org <http://www.wxpython.org/download.php>`_
> >>
> >> Note to Fink users: Versions of Python installed by Fink cannot run
> >> wxPython (unless you install and run X11...). You need to use Apple's
> >> Framework builds, or a third-party Framework build.
> >>
> >> And reading a bit further:
> >>
> >> Python 2.4 Framework builds are available for download from the
> >> undefined.org MacPython site. ActiveState also distributes a Framework
> >> build.
> >>
> >> So I guess I will look into a Framework build as my starting point.
> >
> > I'd really recommend using the universal build from python.org. As a
> > previous poster mentioned, it is a framework build. I've never had any
> > trouble using wxPython in conjunction with it, either.
> >
> The universal build from Python.org is outdated (IIRC). A better, more
> optimized one is available from
> http://pythonmac.org/packages/py24-fat/dmg/Universal-MacPython-2.4.3-2006-04-07.dmg
>
>
>
> --
> Kevin Walzer
> Poetic Code
> http://www.kevin-walzer.com

Outdated??? It's Python 2.4.3 (and they already have a build for
2.5rc1), so I'm not sure what you mean.

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