Re: web.py & postgresql error

2007-10-23 Thread Adam Atlas
On Oct 22, 9:06 pm, [EMAIL PROTECTED] wrote:
> hi everyone, i'm very new to python and to this forum.  i'm actually
> just trying to work through the tutorial on webpy.org.  so far, so
> good, but as i tried to incorporate a postgresql database into the
> demo web app i'm receiving this error print out:
>
> [...]
> ImportError: No module named pgdb
>
> any thoughts would be greatly appreciated.
> thanks,
> doug

It looks like you just haven't installed PyGreSQL yet. 

Future web.py questions should probably be directed to the web.py
group , though.

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


Re: Python - why don't this script work?

2007-10-22 Thread Adam Atlas
On Oct 22, 9:47 pm, Ohmster <[EMAIL PROTECTED]> wrote:
> I am trying to use this cool script that some MIT guy wrote and it just
> does not work, I get a stream of errors when I try to run it. It is
> supposed to visit a URL and snag all of the pictures on the site. Here is
> the script:http://web.mit.edu/pgbovine/www/image-harvester/image-harvester.py
>
> Here is my output when I try to run it on my Fedora 6 machine:
>
> [EMAIL PROTECTED] bench]$ 
> image-harvester.pyhttp://public.fotki.com/DaGennelman/
> /home/ohmster/scripts/image-harvester.py: line 59: from: command not found
> [EMAIL PROTECTED] bench]$
>
> The script is to be folowed up with another one to weed out the small
> thumbnails and banner images, here is the base 
> URL:http://web.mit.edu/pgbovine/www/image-harvester/
>
> Line 59 in image-harvester.py reads as follows:
>
> 59: from sgmllib import SGMLParser
> 60: import urllib
> 70: from urlparse import urlparse, urljoin
> 71: import re
> 72: import os
>
> Can anyone tell me what is wrong with this script and why it will not run?
> It does not like the command "from", is there such a command in python?
> Does this mean that python has the "import" command but not the "from"
> command or do we not know this yet as it hangs right away when it hits the
> very first word of the script, "from"? Maybe this is not a Linux script or
> something? I wonder why it needs the x-server anyway, I tried running it
> from an ssh term window and it had a fit about no x-server so now I am
> doing this in a gnome term window. This looked so cool too. :(
>
> Please be patient with me, I do not know python at all, I just want for
> this script to work and if I see enough working examples of python, I may
> just take up study on it, but for right now, I do not know the language.
> Total newbie.
>
> Thanks.

I think you're executing it as a shell script. Run "python image-
harvester.py", or add "#!/usr/bin/env python" to the top of the file.

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


Re: Convert string to command..

2007-10-18 Thread Adam Atlas
On Oct 18, 10:23 am, Abandoned <[EMAIL PROTECTED]> wrote:
> I want to convert a string to command..
> For example i have a string:
> a="['1']"
> I want to do this list..
> How can i do ?

Use the builtin function "eval".

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


Re: Noob questions about Python

2007-10-17 Thread Adam Atlas
On Oct 17, 3:37 pm, Ixiaus <[EMAIL PROTECTED]> wrote:
> I have recently (today) just started learning/playing with Python. So
> far I am excited and impressed (coming from PHP background).
>
> I have a few questions regarding Python behavior...
>
> val = 'string'
> li = list(val)
> print li.reverse()
>
> returns nothing, but,
>
> val = 'string'
> li = list(val)
> li.reverse()
> print li
>
> returns what I want. Why does Python do that?

Because list.reverse() modifies a list, it doesn't create and return a
new one.

A common idiom for returning a reversed copy of a list is:
  li[::-1]

You can also use the builtin "reversed" -- it doesn't return a list,
but rather a reverse iterator. You can create a list from an iterator
by passing it to the list() initializer, like list(reversed(li)).

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


Re: Python on imac

2007-10-13 Thread Adam Atlas
On Oct 13, 7:21 pm, John Velman <[EMAIL PROTECTED]> wrote:
> I'm considering moving from Linux to imac.   I've recently returned to
> Python (was never very expert) to develop a small gui application.  At
> present I plan to use PyGTK with Pango and Cairo.
>
> What surprises may I be in for :-)
>
> (Currently using slackware 11.0 on an old (8 years) slow (400mhz) machine.)
>
> Thanks,
>
> John Velman

Well... I think OS X currently comes with Python 2.3 or 2.4. I
recommend getting Fink (fink.sourceforge.net), which is basically a
DPKG/APT repository for OS X plus an added system for automatically
building packages from source. It'll keep the system up to date with
the latest 2.5 release (and future versions).

If you're using PyGTK, you will need to install Apple X11 (can't
remember if it's installed by default yet), since OS X's window
manager is not X11-based. Fink can also install GTK+, etc. for you.
Other than that, most things should work as on Linux, more or less.

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


Python process automatically restarting itself

2007-10-11 Thread Adam Atlas
What is the best way for a Python process (presumed to be a script run
by the interpreter binary, not embedded in some other program) to
restart itself? This is what I've been trying:

import __main__

for path in sys.path:
path += '/' + __main__.__file__
if os.access(path, os.F_OK): break
else:
raise Exception('WTF?')

#Then, something like...
os.execl(sys.executable, sys.executable, path, *sys.argv[1:])

BUT! This is a multi-threaded program, and at least on OS X, trying to
exec*() in a threaded process raises OSError [Errno 45] Operation not
supported. So I tried having it fork, and having the child process
wait for the parent process to die (in various ways) and then exec*().
That seemed to work, sort of, but the child would not be attached to
the original terminal, which is a must.

Any ideas?

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


Plugging a pseudo-memory leak

2007-07-03 Thread Adam Atlas
I have a program that seemed to be leaking memory, but after
debugging, it seemed it just wasn't getting around to collecting the
objects in question often enough. The objects are very long-lived, so
they probably end up in generation 2, and don't get collected for a
long time. Is there any way I can force collection of these objects? I
know in Python 2.5 there's gc.collect(2), but I want to keep it
compatible with previous versions of Python.

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


PyKQueue

2007-06-26 Thread Adam Atlas
Does anyone have a copy of PyKQueue 2.0 around? The site it's supposed
to be on (http://python-hpio.net/trac/wiki/PyKQueue) is down.

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


Re: How do you htmlentities in Python

2007-06-04 Thread Adam Atlas
As far as I know, there isn't a standard idiom to do this, but it's
still a one-liner. Untested, but I think this should work:

import re
from htmlentitydefs import name2codepoint
def htmlentitydecode(s):
return re.sub('&(%s);' % '|'.join(name2codepoint), lambda m:
name2codepoint[m.group(1)], s)

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


Re: `yield` in a `try/finally` block, pre-Python 2.5

2007-06-04 Thread Adam Atlas
On Jun 4, 1:49 am, yuce <[EMAIL PROTECTED]> wrote:
> I had the same problem, you can 
> see:http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/130004
> for a solution.
>
> Happy hacking,
>
> Yuce

Thanks. I thought of doing something like that, but in my program,
it's important that the order follow the actual nesting order. That
is, I have a few nested generator each of which has its own 'finally',
and I need the innermost ones to run first. How can I deal with that?

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


`yield` in a `try/finally` block, pre-Python 2.5

2007-06-03 Thread Adam Atlas
I'm trying to emulate the Python 2.5 behaviour (PEP 342) of generator
functions where the `yield` statement is in a `try/finally` block.
Basically, where the `finally` block is guaranteed to run even if the
generator doesn't finish running: it simply runs when the generator is
garbage-collected. Does anyone know a good way of doing this? I'm
looking to see if there's a way to bring about an exception in another
frame in pure Python, but I haven't found anything yet.

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


Re: How to cleanly pause/stop a long running function?

2007-05-12 Thread Adam Atlas
On May 12, 4:51 pm, Basilisk96 <[EMAIL PROTECTED]> wrote:
> Suppose I have a function that may run for a long time - perhaps from
> several minutes to several hours. An example would be this file
> processing function:
>
> import os
> def processFiles(startDir):
> for root, dirs, files in os.walk(startDir):
> for fname in files:
> if fname.lower().endswith(".zip"):
> # ... do interesting stuff with the file here ...
>
> Imagine that there are thousands of files to process. This could take
> a while. How can I implement this so that the caller can pause or
> interrupt this function, and resume its program flow? Doing a Ctrl+C
> interrupt would be a not-so-clean-way of performing such a thing, and
> it would quit the application altogether. I'd rather have the function
> return a status object of what it has accomplished thus far.
>
> I have heard about threads, queues, and asynchronous programming, but
> am not sure which is appropriate for this and how to apply it. Perhaps
> the above function should be a method of a class that inherits from
> the appropriate handler class? Any help will be appreciated.
>
> -Basilisk96

Consider using generators.
http://docs.python.org/tut/node11.html#SECTION0011100

This way, whatever part of your program calls this function can
completely control the iteration. Maybe you can have it yield status
information each time.

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


Re: Newbie question about string(passing by ref)

2007-05-10 Thread Adam Atlas
On May 10, 6:19 pm, lazy <[EMAIL PROTECTED]> wrote:
> So, just to make sure even if I return a value, there is no copy done.
> Is it correct?
> For eg:
>
> def blah:
>long_str=""
>return long_str
>
> my_str=blah() <=== So here there is no copy done but, my_str points to
> the same memory where long_str was created.

Exactly. As Bruno said, Python does not copy objects unless you
specifically tell it to.

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


Re: Newbie question about string(passing by ref)

2007-05-10 Thread Adam Atlas
On May 10, 5:47 pm, Adam Atlas <[EMAIL PROTECTED]> wrote:
> On May 10, 5:43 pm, lazy <[EMAIL PROTECTED]> wrote:
>
> > I want to pass a string by reference.
>
> Don't worry, all function parameters in Python are passed by reference.

Actually, just to clarify a little bit if you're understanding "pass
by reference" in the sense used in PHP, sort of C, etc.: In Python,
you have objects and names. When I say all function parameters are
passed by reference, I don't mean you're actually passing a reference
to the *variable*. (Like in PHP where you pass a variable like &$foo
and the function can change that variable's value in the caller's
scope.) You can never pass a reference to a variable in Python. But on
the other hand, passing a parameter never causes the value to be
copied. Basically, you have a variable that's a reference to an object
somewhere in memory, and passing it to a function gives that
function's scope a new pointer to that same location in memory. So if
the function you pass it to assigns some other value to the variable,
that's all it's doing: reassigning a local name to point to somewhere
else in memory.

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


Re: Newbie question about string(passing by ref)

2007-05-10 Thread Adam Atlas

On May 10, 5:43 pm, lazy <[EMAIL PROTECTED]> wrote:
> I want to pass a string by reference.

Don't worry, all function parameters in Python are passed by reference.

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


WSGI spec clarification regarding exceptions

2007-05-09 Thread Adam Atlas
I'm trying to figure out if there's any defined behaviour in PEP 333
for instances where an application returns an iterable as usual
without error, but that iterable's next() method eventually raises an
exception. Since any data theretofore returned by the iterable must be
assumed to have already been written to the client, thus making it
impossible to replace the response with a 500 error or somesuch, does
the gateway just absorb the exception and cut off the response there?
It seems like that's pretty much all it could do, but I'm wondering if
that's explicitly specified anywhere (I couldn't find anything about
that in the PEP).

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


Re: __dict__s and types and maybe metaclasses...

2007-05-02 Thread Adam Atlas
On May 2, 5:24 pm, Larry Bates <[EMAIL PROTECTED]> wrote:
> I think that most people accomplish this by:
>
> class blah:
> __initial_values={'a': 123, 'b': 456}
>
> def __init__(self):
> self.__dict__.update(self.__initialvalues)

That's not really what I'm talking about... I'm talking about
replacing the __dict__ with a SUBCLASS of dict (not just default
values), and that's at the time of the class declaration (the creation
of `blah` as a `type` instance), not at instance creation.

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


Re: Can I use Python instead of Joomla?

2007-05-02 Thread Adam Atlas
On May 2, 4:48 pm, walterbyrd <[EMAIL PROTECTED]> wrote:
> If I wanted to build a website with forums, news feeds, galleries,
> event calander, document managment, etc. I do so in Joomla easily.
>
> But, I would perfer to use django/python, if that would be at all
> practical.
>
> I suppose I could put python scripts into django, if those scripts
> exist.

Have you looked at Zope/Plone?

It's not Django, but they're the de facto standards in the Python
world for that sort of thing.

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


__dict__s and types and maybe metaclasses...

2007-05-02 Thread Adam Atlas
Suppose I want to create a type (i.e. a new-style class via the usual
`class blah(...)` mechanism) but, during the process of creating the
type, I want to replace its __dict__ so I can override some behaviors
during the initial assignment of its members. That is, I have `class
blah(...): a = 123; b = 456; ...`, and I want to substitute my own
dict subclass which will thus receive __setitem__(a, 123),
__setitem__(b, 456), and so on.

Is this possible? Maybe with metaclasses? I've experimented with them
a bit, but I haven't found any setup that works.

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


Re: editing scripts on a mac

2007-04-29 Thread Adam Atlas
On Apr 27, 12:08 pm, Steve Holden <[EMAIL PROTECTED]> wrote:
> Now, frankly, I don't think this answer is correct, since I know OS X is
> a UNIX derivative, but I am loathe to involve a programming noob with vi
> or something similar. So I wondered if one of the c.l.py mac users could
> give brief instructions for firing up a visual text editor of some sort
> and saving a file somewhere it can easily be accessed from a terminal
> window (which I presume starts up in his home directory).

I strongly recommend TextMate (http://www.macromates.com/). It's not
free, but it rocks.

There's also TextWrangler (http://www.barebones.com/products/
textwrangler/), a free (as in beer) lite version of BBEdit, the most
venerable Mac text editor. I used to use BBEdit; it's quite excellent,
though TextMate feels a bit more modern and OS X-like.

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


Tracebacks for `exec`ed code?

2007-04-28 Thread Adam Atlas
Is it possible to make more traceback information available for
exceptions code dynamically run via `exec`? Normally it just says
things like "File '', line 3, in ?", which is not very
helpful. I'm looking for a way for it to show the line of source code
below it, like it would for an exception in a physical file. Is this
possible?

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


Re: Python for Vcard Parsing in UTF16

2007-04-24 Thread Adam Atlas
On Apr 21, 7:28 pm, R Wood <[EMAIL PROTECTED]> wrote:
> To me this was a natural task for Perl.  Turns out however, there's a catch.  
> Apple exports the file in UTF-16 to ensure anyone with Chinese characters in
> their addressbook gets a legitimate Vcard file.

Here's a function that, given a `str` containing a vcard in some
encoding, guesses the encoding and returns a canonical representation
as a `unicode` object.

def fix_encoding(s):
m = u'BEGIN:VCARD'
for c in ('ascii', 'utf_16_be', 'utf_16_le', 'utf_8'):
try: u = unicode(s, c)
except UnicodeDecodeError: continue
if m in u: return u
return None

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


Re: Python for Vcard Parsing in UTF16

2007-04-24 Thread Adam Atlas
On Apr 21, 7:28 pm, R Wood <[EMAIL PROTECTED]> wrote:
> I know nothing about Python except that it interests me and has interested me
> since I first learned the Rekall database frontend (Linux) runs on it.  I just
> ordered Learning Python and if that works out satisfactorily I'm going to go
> back for Programming Python.  In the meantime, I thought I would pose the
> question to this newsgroup: would Python be useful for a parsing exercise like
> this one?

Here's a little function that takes some `str`-type data (i.e. what
you'd get from doing open(...).read()) and, assuming it's a Vcard,
detects its encoding and converts it to a canonical `unicode` object.

def fix_encoding(s):
m = u'BEGIN:VCARD'
for c in ('ascii', 'utf_16_be', 'utf_16_le', 'utf_8'):
try: u = unicode(s, c)
except UnicodeDecodeError: continue
if m in u: return u
return None

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


Re: Better dict of dicts

2007-04-19 Thread Adam Atlas
On Apr 19, 5:24 pm, Bill Jackson <[EMAIL PROTECTED]> wrote:
> I have a dictionary of dictionaries where the keys are typically very
> long tuples and repeated in each inner dictionary.  The dictionary
> representation is nice because it handles sparseness well...and it is
> nice to be able to look up values based on a string rather than a
> number.  However, since my keys are quite long, I worry that I am
> wasting a lot of memory.

I wouldn't worry about it. Try doing hash('string_2') in the
interpreter -- the output thereof is what's really being used as the
key. It doesn't use up any more memory than the integer 2.

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


Re: Compare regular expressions

2007-04-16 Thread Adam Atlas
On Apr 16, 1:50 pm, "Diez B. Roggisch" <[EMAIL PROTECTED]> wrote:
> It's not. For the simplest of expressions one might come up with a
> relation between them, but even that would be hard. General case? No chance.

I wouldn't say there's 'no chance'. It would require external parsing,
for sure, but if anything, it may only be impossible for unusual
cases.

(Maybe I'll try this the next time I feel like writing parsers for fun
(which is surprisingly frequently).)

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


Re: Breaking up Strings correctly:

2007-04-09 Thread Adam Atlas
On Apr 9, 8:19 am, "Michael Yanowitz" <[EMAIL PROTECTED]> wrote:
> Hello:
>
>I have been searching for an easy solution, and hopefully one
> has already been written, so I don't want to reinvent the wheel:

Pyparsing is indeed a fine package, but if Paul gets to plug his
module, then so do I! :)

I have a package called ZestyParser... a lot of it is inspired by
Pyparsing, actually, but I'm going in a different direction in many
areas. (One major goal is to be crazily dynamic and flexible on the
inside. And it hasn't failed me thus far; I've used it to easily parse
grammars that would make lex and yacc scream in horror.)

Here's how I'd do it...

from ZestyParser import *
from ZestyParser.Helpers import *

varName = Token(r'\$(\w+)', group=1)
varVal = QuoteHelper() | Int
sp = Skip(Token(r'\s*'))
comparison = sp.pad(varName + CompositeToken([RawToken(sym) for sym in
('=','<','>','>=','<=','!=')]) + varVal)
#Maybe I should "borrow" PyParsing's OneOf idea :)

expr = ExpressionHelper((
comparison,
(RawToken('(') + Only(_top_) + RawToken(')')),
oper('NOT', ops=UNARY),
oper('AND'),
oper('OR'),
))

Now you can scan for `expr` and get a return value like [[['IP', '=',
'127.1.2.3'], ['AX', '<', 15]], [['IP', '=', '127.1.2.4'], ['AY', '!
=', 0]]] (for the example you gave).

Note that this example uses several features that won't be available
until the next release, but it's coming soon. So Michael, though you'd
still be able to parse this with the current version, the code
wouldn't look as nice as this or the Pyparsing version. Maybe just add
it to your watchlist. :)

- Adam

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


Re: RFC: Assignment as expression (pre-PEP)

2007-04-09 Thread Adam Atlas
Hasn't this been discussed many many times before? I think Guido has
been favourable to the idea of allowing :=, but that was a long time
ago, and I don't think anything ever came of it.

Personally, if anything, I'd like to see more use of the 'as' keyword
as in Python 2.5's new 'with' statement. Assignment is basically what
it adds to the statement, so if anything we should reuse it in other
statements for consistency.

if my_re1.match(exp) as temp:
  # do something with temp
elif my_re2.match(exp) as temp:
  # do something with temp
elif my_re3.match(exp) as temp:
  # do something with temp
elif my_re4.match(exp) as temp:
  # do something with temp

As others have mentioned, your particular instance is probably
evidence that you need to restructure your code a little bit, but I do
agree that "x = y; if x: ..." is a common enough idiom that it
warrants a shortcut. And reusing "as", I think, is nice and readable,
and it's an advantage that it doesn't require adding any new keywords
or symbols.

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


Re: Python C extension providing... Python's own API?

2007-03-26 Thread Adam Atlas
Wow! I'll have to read through that tomorrow when I'm (hopefully) less
tired. :D

Anyway, I somehow already managed to get this working. I'm calling it
DoublePy.
Here's the alpha or proof-of-concept or whatever we're to call it.
http://adamatlas.org/2007/03/doublepy-0.1.tar.gz

Not bad for an 0.1 written in a couple of hours, methinks. This could
have some really interesting possibilities when it's more mature.

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


Re: Python C extension providing... Python's own API?

2007-03-26 Thread Adam Atlas
On Mar 26, 4:55 pm, "Matimus" <[EMAIL PROTECTED]> wrote:
> I think that is what the "code" module is for. Maybe not exactly what
> you were expecting, but the capability you describe is already there.
> Being able to access its own interpreter is one of the things that
> makes Python a  dynamic language.

That's not really what I'm after. Believe me, I've searched the
standard library in length and breadth. :) The `code` module runs in
the same interpreter, basically executing as though it were just a
separate module (except interactively). What I'm interested in is
accessing Python's C API for CREATING interpreters -- i.e. what
mod_python or anything else embedding the Python interpreter would do.
Creating a whole environment, not just a module; it has its own `sys`
parameters, its own `__builtin__`, and so on, so you can safely mess
with those without changing them in the parent interpreter.

So far, I've tried ctypes, and it doesn't work; then I noticed the
docs said it wouldn't anyway. But I think I'll try writing a C
extension module. This could be interesting for sandboxing &c.

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


Python C extension providing... Python's own API?

2007-03-26 Thread Adam Atlas
Does anyone know if it would be possible to create a CPython extension
-- or use the ctypes module -- to access Python's own embedding API
(http://docs.python.org/api/initialization.html &c.)? Could a Python
program itself create a sub-interpreter, and work with it with all the
privileges and capabilities that an actual C program would have?

I realize that this may be a bit too... mystical? ... for a lot of
people's tastes, but I'm just curious if it's possible. :)

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


[ANN] squisher 0.3

2007-03-17 Thread Adam Atlas
http://cheeseshop.python.org/pypi/squisher/0.3

I've posted the third version of my experimental packaging program
Squisher. Squisher can take a directory representing a Python package
(i.e. a directory with __init__.py) and "squish" it into a single .pyc
file that you can import, or run on the command line, just like any
other .py/.pyc file.

The hook is that it can be thus imported *without* necessarily having
Squisher itself installed. All you do is run Squisher on a directory
(or an existing zip file if you wish), and you get a single file you
can import with any normal Python installation.

It is complementary to Eggs in a way. They're good for having packages
globally installed and keeping them up-to-date, but very often you may
want the simple convenience of dropping a .pyc in a directory and
importing it. Furthermore, since Squished packages are just zip files
with a special Python bytecode header (and Eggs are just zipfiles with
internal metadata added), you can actually run it on an Egg and get a
file that can be used as an Egg *or* a Squished package just by
renaming it.

Try it out... I've found it useful so far. It is still pretty
experimental, so I wouldn't advise using it as a serious packaging
mechanism yet, but I hope that with time it will become very stable
and complete. Bug reports/patches welcome.

--

Changelog for anyone who tried the previous releases:
- Added a couple of patches courtesy of Gary Duzan -- a Python 2.3
compatibility fix, and a fix for importing compiled modules when
running a .pyc from the command line.
- Changed from GPL to BSD license. Also clarified that the generated
bytecode is considered public domain.
- It now detects if you pass it an already-squished module -- if so,
it will simply update the bytecode header if necessary.

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


Re: Squisher -- a lightweight, self-contained alternative to eggs?

2007-03-06 Thread Adam Atlas
I updated it.
http://adamatlas.org/2007/03/Squisher-0.2.py

New Things:
- It supports C extensions within squished packages.
- It supports including squished packages within other squished
packages. (That is, you can have a package that includes a .pyc
generated by this, and turn that whole package into one .pyc.)
- If you import it after you import setuptools in a setup.py, it will
override a bit of setuptools so that bdist_egg will result in an egg
that can also be used as a squished package (if you rename it to
something ending in .pyc).
- It puts a helpful docstring at the top of generated .pyc files,
mainly for the purpose of being easy to see if you open the file in a
text editor. (It tells you what can be done with the file -- name it
something .pyc, or unzip it, or, if applicable, use it as an egg.)

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


Re: Squisher -- a lightweight, self-contained alternative to eggs?

2007-03-06 Thread Adam Atlas
Doesn't seem to work. I guess zipimport doesn't support that by
default... but if I remember correctly, Setuptools adds that. Maybe
I'll take a look at how it does it (I think by extracting the .so to /
tmp?) and see how easy it would be to integrate it here.

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


Re: Squisher -- a lightweight, self-contained alternative to eggs?

2007-03-05 Thread Adam Atlas
Okay, here's the prototype...

It's meant to be run as a command line program (pass it a directory or
a zip file as an argument). By default it will save the new file to
the argument's base name plus '.pyc'. You can override this with -o.

Obviously it's very much a work in progress... try it out and let me
know the results.

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


Re: Squisher -- a lightweight, self-contained alternative to eggs?

2007-03-05 Thread Adam Atlas
Ah... heh, sorry, I misread your message as "a much more convenient
way" rather than "much more than a convenient way". Anyway, I
understand that, and I do indeed find setuptools useful and use it on
a regular basis.

But my other points still stand. This would be a moot point if
setuptools were part of the standard library, but it's not, and I
don't see why people should have to bother installing it if they
simply want to try a small package. Look at Beautiful Soup, for
example. It's distributed as a single .py file, and that's great. With
most modules, all I want to do is download them and plop them into my
project directory. You can always copy it into site-packages if you
want to access it globally, and you can always unzip it if you need to
see the source.

So I *will* retract my statement that this could be an "alternative"
to eggs -- ideally, it would be an addition, since it doesn't break
compatibility at all. You could download an egg and rename it to .pyc,
and import it like any other module, and at any point later, you could
rename it back to .egg and use setuptools to install it if you wish.

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


Re: Squisher -- a lightweight, self-contained alternative to eggs?

2007-03-05 Thread Adam Atlas
This could be easily made into a distutils extension (which was my
intention all along, though that's not implemented yet). That's not
the point. This is not intended as a "way to package source code".
It's analogous to bdist, not sdist. The convenience gain is for the
users, not (primarily) the developers, of the package.

To use an Egg, you have to either put it in your sys.path manually or
install setuptools and use it to install the Egg in some global scope.
The advantage of Squisher is that you can take a whole package, squish
it into one .pyc file, and distribute that. Then, anyone who downloads
it can get a simple module file that can be used anywhere a usual .py
file can, without editing sys.path or installing it somewhere.
(Actually, this could be combined with eggs, since they're just ZIP
files. You could use the same file as an egg or as a squished module
simply by changing its extension.)

...Or am I missing something obvious about setuptools that already
does this and making a fool of myself? :)

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


Squisher -- a lightweight, self-contained alternative to eggs?

2007-03-04 Thread Adam Atlas
I wrote this little program called Squisher that takes a ZIP file
containing Python modules and generates a totally self-contained .pyc
file that imports a specified module therein. (Conveniently, Python's
bytecode parser ignores anything after an end marker, and the
zipimport mechanism skips any non-ZIP data at the beginning of a
file!) For example, say you have a directory called 'foo', which
contains an __init__.py, which contains "print 'hello'; x = 123", and
a thing.py, which contains "y = 456". If you make a ZIP archive of
this and run it through Squisher, you'll get a single .pyc file which
can be imported by any Python installation anywhere just like any
other module, without requiring users to install any supporting
mechanisms (like setuptools), and giving all the flexibility and
stability of zipimport. In other words, you can do this simply by
dropping the foo.pyc file into a directory:

>>> import foo
hello
>>> foo.x
123
>>> from foo.thing import y
>>> y
456

Of course, this is a stupid and useless example, but you can do it
with any package you could use as an egg, yet without requiring people
to install setuptools (or any other external glue) to use it. I've
tested it with several large packages, including SQLAlchemy.

Right now I'm just testing and polishing up the code... in the
meantime, any comments?

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


Re: Announcement -- ZestyParser

2007-01-11 Thread Adam Atlas
Thanks for the responses; you're right, and I have now posted the
examples online. I just released version 0.6.0, by the way, which has
several worthwhile improvements and much better documentation. It also
includes an example for parsing RDF Notation3, to demonstrate a parsing
task a bit more complex than the other examples.

That's all posted at  now.

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


Announcement -- ZestyParser

2007-01-09 Thread Adam Atlas
This has been on Cheese Shop for a few weeks now, being updated now and
then, but I never really announced it. I just now put up a real web
page for it, so I thought I'd take the opportunity to mention it here.

ZestyParser is my attempt at a flexible toolkit for creating concise,
precise, and Pythonic parsers. None of the existing packages really met
my needs; Pyparsing came the closest, but I find it still has some
shortcomings. ZestyParser is simply an abstract implementation of how I
think about parsing; I don't expect it'll meet everyone's parsing
needs, but I hope it'll make typical parsing tasks more joyful.

Here's the web page:
http://adamatlas.org/2006/12/ZestyParser/
Here's the Cheese Shop page:
http://cheeseshop.python.org/pypi/ZestyParser
(I recommend you get the source package, as it includes the examples.)

Check it out and let me know what you think!

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


Re: Getting the name of an assignment

2006-12-23 Thread Adam Atlas
Thanks, Steven and Steven.

@Bethard:
Isn't it a bit convoluted to use metaclasses?
someinstance.__class__.__name__ does the same thing.

@D'Aprano:
Thanks for the advice to rethink my data model. I'm doing so right now,
and I've already come up with a way that makes more sense. :)

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


Re: Getting the name of an assignment

2006-12-23 Thread Adam Atlas
On Dec 23, 5:58 pm, "BJörn Lindqvist" <[EMAIL PROTECTED]> wrote:
> On 23 Dec 2006 14:38:19 -0800, Adam Atlas <[EMAIL PROTECTED]> wrote:
>
> > Is it possible for an object, in its __init__ method, to find out if it
> > is being assigned to a variable, and if so, what that variable's name
> > is? I can think of some potentially ugly ways of finding out using
> > sys._getframe, but if possible I'd prefer something less exotic.
> > (Basically I have a class whose instances, upon being created, need a
> > 'name' property, and if it's being assigned to a variable immediately,
> > that variable's name would be the best value of 'name'; to make the
> > code cleaner and less redundant, it would be best if it knew its own
> > name upon creation, just like functions and classes do, without the
> > code having to pass it its own name as a string.)I guess you mean something 
> > like this:
>
> >>> olle = Person()
> >>> olle.name"olle"
>
> Instead of:
>
> >>> olle = Person("olle")
> >>> olle.name"olle"
>
> It is not possible without ugly hacks. What you could use instead is
> some kind of registry approach:
>
> reg = {}
> class Person:
> def __init__(self, name):
> self.name = name
> reg[name] = self
>
> >>> Person("olle")
> >>> reg["olle"].name"olle"
>
> I think there are thousand different ways you could solve it.

Yeah, I've thought of ways like that. I was just hoping to make the
syntax as minimal and Pythonic as possible.

I have the following working:

> import sys
>
> class c:
> def __init__(self):
> f = sys._getframe(1)
> names = [n for n in f.f_code.co_names if n not in f.f_locals]
> if len(names) > 0:
> name = names[0]
> print name
>
> a = c() # prints 'a'
> b = 'blah'
> b = c() # prints nothing

Question: too evil?

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


Getting the name of an assignment

2006-12-23 Thread Adam Atlas
Is it possible for an object, in its __init__ method, to find out if it
is being assigned to a variable, and if so, what that variable's name
is? I can think of some potentially ugly ways of finding out using
sys._getframe, but if possible I'd prefer something less exotic.
(Basically I have a class whose instances, upon being created, need a
'name' property, and if it's being assigned to a variable immediately,
that variable's name would be the best value of 'name'; to make the
code cleaner and less redundant, it would be best if it knew its own
name upon creation, just like functions and classes do, without the
code having to pass it its own name as a string.)

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


Re: Multiple (threaded?) connections to BaseHTTPServer

2005-08-23 Thread Adam Atlas
Never mind... I've found a workable solution:
http://mail.python.org/pipermail/python-list/2001-August/062214.html

Robert, thanks, I'll still check that out for any future projects; but
for my current purpose, it seems overkill. But I don't know for sure;
assuming BaseHTTPServer is good enough for my current needs aside from
its lack of threading, does PooledThreadedServer have any advantages
over the method I linked above?

- Adam

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


Multiple (threaded?) connections to BaseHTTPServer

2005-08-23 Thread Adam Atlas
Is there any built-in way for BaseHTTPServer to handle multiple
connections at once, e.g. with threads? If not, can this existing
module be easily extended to do this, or will I have to do lower-level
socket things (and probably thus have to write my own HTTP code)?

Thanks.

Adam

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